chess:programming:msb_most_significant_bit
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
chess:programming:msb_most_significant_bit [2021/10/30 12:08] – created peter | chess:programming:msb_most_significant_bit [2021/10/30 12:29] (current) – [Using Built-in for GCC] peter | ||
---|---|---|---|
Line 6: | Line 6: | ||
* Due to the number of Chess positions possible, many millions, even a small saving for the lookup of the MSB can provide much further lookups in the same time. | * Due to the number of Chess positions possible, many millions, even a small saving for the lookup of the MSB can provide much further lookups in the same time. | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Using Bit Manipulation ===== | ||
+ | |||
+ | <code cpp> | ||
+ | int msbPerformanceJunkie32(u32 x) | ||
+ | { | ||
+ | static const unsigned int bval[] = | ||
+ | { 0, | ||
+ | unsigned int r = 0; | ||
+ | if (x & 0xFFFF0000) | ||
+ | { | ||
+ | r += 16 / 1; | ||
+ | x >>= 16 / 1; | ||
+ | } | ||
+ | if (x & 0x0000FF00) | ||
+ | { | ||
+ | r += 16 / 2; | ||
+ | x >>= 16 / 2; | ||
+ | } | ||
+ | if (x & 0x000000F0) | ||
+ | { | ||
+ | r += 16 / 4; | ||
+ | x >>= 16 / 4; | ||
+ | } | ||
+ | return r + bval[x]; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Using Built-in for GCC ===== | ||
+ | |||
+ | <code cpp> | ||
+ | unsigned BSR64(uint64_t x) | ||
+ | { | ||
+ | return 63-__builtin_clzll(x); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | |||
+ | |||
+ | * The returned value is undefined for 0 inputs. | ||
+ | * See https:// | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Using Built-in for Microsoft Compiler ===== | ||
+ | |||
+ | <code cpp> | ||
+ | #ifdef MICROSOFT_COMPILER | ||
+ | u32 msbNative32(u32 val) | ||
+ | { | ||
+ | unsigned long result; | ||
+ | _BitScanReverse(& | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | u32 msbNative64(u64 val) | ||
+ | { | ||
+ | unsigned long result; | ||
+ | _BitScanReverse64(& | ||
+ | return result; | ||
+ | } | ||
+ | #endif // MICROSOFT_COMPILER | ||
+ | </ | ||
---- | ---- | ||
Line 11: | Line 86: | ||
===== Using de Bruijn for 32-bit ===== | ===== Using de Bruijn for 32-bit ===== | ||
+ | <code cpp> | ||
u32 msbDeBruijn32(u32 v) | u32 msbDeBruijn32(u32 v) | ||
{ | { | ||
Line 28: | Line 104: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | |||
+ | * The De Bruijn lookup table needs to be pre-calculated. | ||
+ | * See: https:// | ||
+ | |||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Using a Loop ===== | ||
+ | |||
+ | <code cpp> | ||
+ | unsigned int msbLoop32(u32 x) | ||
+ | { | ||
+ | int r = 0; | ||
+ | if (x < 1) return 0; | ||
+ | while (x >>= 1) r++; | ||
+ | return r; | ||
+ | } | ||
+ | |||
+ | unsigned int msbLoop64(u64 x) | ||
+ | { | ||
+ | int r = 0; | ||
+ | if (x < 1) return 0; | ||
+ | while (x >>= 1) r++; | ||
+ | return r; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <WRAP info> | ||
+ | **NOTE: | ||
+ | </ | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Using MS C++ Compiler Built-in ===== | ||
+ | |||
+ | <code cpp> | ||
+ | #include < | ||
+ | unsigned BSR32(unsigned long x) | ||
+ | { | ||
+ | bsr_idx_t idx; | ||
+ | _BitScanReverse(& | ||
+ | return idx; | ||
+ | } | ||
+ | |||
+ | unsigned BSR64(uint64_t x) | ||
+ | { | ||
+ | bsr_idx_t idx; | ||
+ | _BitScanReverse64(& | ||
+ | return idx; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== References ===== | ||
+ | |||
+ | https:// | ||
+ | |||
+ | http:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
chess/programming/msb_most_significant_bit.1635595689.txt.gz · Last modified: 2021/10/30 12:08 by peter