- void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
- Bitboard masks[], unsigned shifts[], Square deltas[], Fn index);
-}
-
-/// first_1() finds the least significant nonzero bit in a nonzero bitboard.
-/// pop_1st_bit() finds and clears the least significant nonzero bit in a
-/// nonzero bitboard.
-
-#if defined(IS_64BIT) && !defined(USE_BSFQ)
-
-Square first_1(Bitboard b) {
- return Square(BSFTable[((b & -b) * 0x218A392CD3D5DBFULL) >> 58]);
-}
-
-Square pop_1st_bit(Bitboard* b) {
- Bitboard bb = *b;
- *b &= (*b - 1);
- return Square(BSFTable[((bb & -bb) * 0x218A392CD3D5DBFULL) >> 58]);
-}
-
-#elif !defined(USE_BSFQ)
-
-Square first_1(Bitboard b) {
- b ^= (b - 1);
- uint32_t fold = unsigned(b) ^ unsigned(b >> 32);
- return Square(BSFTable[(fold * 0x783A9B23) >> 26]);
-}
-
-Square pop_1st_bit(Bitboard* b) {
-
- Bitboard bb = *b;
- *b = bb & (bb - 1);
- bb ^= (bb - 1);
- uint32_t fold = unsigned(bb) ^ unsigned(bb >> 32);
- return Square(BSFTable[(fold * 0x783A9B23) >> 26]);
-}
-
-Square last_1(Bitboard b) {
-
- unsigned b32;
- int result = 0;
-
- if (b > 0xFFFFFFFF)
- {
- b >>= 32;
- result = 32;
- }
-
- b32 = unsigned(b);
-
- if (b32 > 0xFFFF)
- {
- b32 >>= 16;
- result += 16;
+ unsigned popcount16(unsigned u) {
+ u -= (u >> 1) & 0x5555U;
+ u = ((u >> 2) & 0x3333U) + (u & 0x3333U);
+ u = ((u >> 4) + u) & 0x0F0FU;
+ return (u * 0x0101U) >> 8;