- 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]);
-}
-
-// Use type-punning
-union b_union {
-
- Bitboard dummy;
- struct {
-#if defined (BIGENDIAN)
- uint32_t h;
- uint32_t l;
-#else
- uint32_t l;
- uint32_t h;
-#endif
- } b;
-};
-
-Square pop_1st_bit(Bitboard* b) {
-
- const b_union u = *((b_union*)b);
-
- if (u.b.l)
- {
- ((b_union*)b)->b.l = u.b.l & (u.b.l - 1);
- return Square(BSFTable[((u.b.l ^ (u.b.l - 1)) * 0x783A9B23) >> 26]);
- }
-
- ((b_union*)b)->b.h = u.b.h & (u.b.h - 1);
- return Square(BSFTable[((~(u.b.h ^ (u.b.h - 1))) * 0x783A9B23) >> 26]);
-}
-
-Square last_1(Bitboard b) {
-
- unsigned b32;
- int result = 0;
-
- if (b > 0xFFFFFFFF)
- {
- b >>= 32;
- result = 32;
+ unsigned popcount16(unsigned u) {
+ u -= (u >> 1) & 0x5555U;
+ u = ((u >> 2) & 0x3333U) + (u & 0x3333U);
+ u = ((u >> 4) + u) & 0x0F0FU;
+ return (u * 0x0101U) >> 8;