X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fbitboard.cpp;h=7d86884d12ee51caab40e70e1b14eac56b3f019c;hp=9bd16d2f66a96e60be496258ea550fc6bcb6277a;hb=4d9e9ac3d45190f4d92030fe9de2637344018f0a;hpb=080a4995a3977a0fe7071e62151dc4d716a13302 diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 9bd16d2f..7d86884d 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -297,6 +297,7 @@ void init_bitboards() { #if defined(IS_64BIT) && !defined(USE_BSFQ) +CACHE_LINE_ALIGNMENT static 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, @@ -316,7 +317,8 @@ Square pop_1st_bit(Bitboard* b) { #elif !defined(USE_BSFQ) -static const int BitTable[64] = { +static CACHE_LINE_ALIGNMENT +const int BitTable[64] = { 63, 30, 3, 32, 25, 41, 22, 33, 15, 50, 42, 13, 11, 53, 19, 34, 61, 29, 2, 51, 21, 43, 45, 10, 18, 47, 1, 54, 9, 57, 0, 35, 62, 31, 40, 4, 49, 5, 52, 26, 60, 6, 23, 44, 46, 27, 56, 16, 7, 39, 48, 24, 59, 14, 12, 55, 38, 28, @@ -334,36 +336,59 @@ union b_union { Bitboard b; struct { +#if defined (BIGENDIAN) + uint32_t h; + uint32_t l; +#else uint32_t l; uint32_t h; +#endif } dw; }; -// WARNING: Needs -fno-strict-aliasing compiler option Square pop_1st_bit(Bitboard* bb) { - b_union u; - uint32_t b; + b_union* u; + Square ret; - u.b = *bb; + u = (b_union*)bb; - if (u.dw.l) - { - b = u.dw.l; - *((uint32_t*)bb) = b & (b - 1); - b ^= (b - 1); - } - else - { - b = u.dw.h; - *((uint32_t*)bb+1) = b & (b - 1); // Little endian only? - b = ~(b ^ (b - 1)); - } - return Square(BitTable[(b * 0x783a9b23) >> 26]); + if (u->dw.l) + { + ret = Square(BitTable[((u->dw.l ^ (u->dw.l - 1)) * 0x783a9b23) >> 26]); + u->dw.l &= (u->dw.l - 1); + return ret; + } + ret = Square(BitTable[((~(u->dw.h ^ (u->dw.h - 1))) * 0x783a9b23) >> 26]); + u->dw.h &= (u->dw.h - 1); + return ret; } #endif +int bitScanReverse32(uint32_t b) +{ + int result = 0; + + if (b > 0xFFFF) { + b >>= 16; + result += 16; + } + if (b > 0xFF) { + b >>= 8; + result += 8; + } + if (b > 0xF) { + b >>= 4; + result += 4; + } + if (b > 0x3) { + b >>= 2; + result += 2; + } + return result + (b > 0) + (b > 1); +} + namespace { // All functions below are used to precompute various bitboards during