X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fbitboard.cpp;h=dd8023e9bd5770eef292f87301750dc55b1ccbcf;hp=6f1569250a7fe6681c080733c86370fc32b46f0c;hb=b651e5334bd94af6358c5e8ca0e282a20997a77c;hpb=8a116ce6910529457a2b82dfa8606cce36415852 diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 6f156925..dd8023e9 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -297,8 +297,8 @@ void init_bitboards() { #if defined(IS_64BIT) && !defined(USE_BSFQ) -CACHE_LINE_ALIGNMENT -static const int BitTable[64] = { +static CACHE_LINE_ALIGNMENT +const int BitTable[64] = { 0, 1, 2, 7, 3, 13, 8, 19, 4, 25, 14, 28, 9, 34, 20, 40, 5, 17, 26, 38, 15, 46, 29, 48, 10, 31, 35, 54, 21, 50, 41, 57, 63, 6, 12, 18, 24, 27, 33, 39, 16, 37, 45, 47, 30, 53, 49, 56, 62, 11, 23, 32, 36, 44, 52, 55, 61, 22, 43, @@ -348,24 +348,60 @@ union b_union { Square pop_1st_bit(Bitboard* bb) { - b_union* u; + b_union u; Square ret; - u = (b_union*)bb; + u.b = *bb; - if (u->dw.l) + if (u.dw.l) { - ret = Square(BitTable[((u->dw.l ^ (u->dw.l - 1)) * 0x783a9b23) >> 26]); - u->dw.l &= (u->dw.l - 1); + ret = Square(BitTable[((u.dw.l ^ (u.dw.l - 1)) * 0x783a9b23) >> 26]); + u.dw.l &= (u.dw.l - 1); + *bb = u.b; return ret; } - ret = Square(BitTable[((~(u->dw.h ^ (u->dw.h - 1))) * 0x783a9b23) >> 26]); - u->dw.h &= (u->dw.h - 1); + ret = Square(BitTable[((~(u.dw.h ^ (u.dw.h - 1))) * 0x783a9b23) >> 26]); + u.dw.h &= (u.dw.h - 1); + *bb = u.b; return ret; } #endif +// Optimized bitScanReverse32() implementation by Pascal Georges. Note +// that first bit is 1, this allow to differentiate between 0 and 1. +static CACHE_LINE_ALIGNMENT +const char MsbTable[256] = { + 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, + 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 +}; + +int bitScanReverse32(uint32_t b) +{ + int result = 0; + + if (b > 0xFFFF) + { + b >>= 16; + result += 16; + } + if (b > 0xFF) + { + b >>= 8; + result += 8; + } + return result + MsbTable[b]; +} + namespace { // All functions below are used to precompute various bitboards during