-inline Bitboard bishop_attacks_bb(Square s, Bitboard blockers) {
- Bitboard b = blockers & BMask[s];
- return BAttacks[BAttackIndex[s] +
- (unsigned(int(b) * int(BMult[s]) ^ int(b >> 32) * int(BMult[s] >> 32)) >> BShift[s])];
+
+/// distance() functions return the distance between x and y, defined as the
+/// number of steps for a king in x to reach y. Works with squares, ranks, files.
+
+template<typename T> inline int distance(T x, T y) { return x < y ? y - x : x - y; }
+template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
+
+template<typename T1, typename T2> inline int distance(T2 x, T2 y);
+template<> inline int distance<File>(Square x, Square y) { return distance(file_of(x), file_of(y)); }
+template<> inline int distance<Rank>(Square x, Square y) { return distance(rank_of(x), rank_of(y)); }
+
+
+/// attacks_bb() returns a bitboard representing all the squares attacked by a
+/// piece of type Pt (bishop or rook) placed on 's'. The helper magic_index()
+/// looks up the index using the 'magic bitboards' approach.
+inline unsigned magic_index(const Magic& m, Bitboard occupied) {
+
+ if (HasPext)
+ return unsigned(pext(occupied, m.mask));
+
+ if (Is64Bit)
+ return unsigned(((occupied & m.mask) * m.magic) >> m.shift);
+
+ unsigned lo = unsigned(occupied) & unsigned(m.mask);
+ unsigned hi = unsigned(occupied >> 32) & unsigned(m.mask >> 32);
+ return (lo * unsigned(m.magic) ^ hi * unsigned(m.magic >> 32)) >> m.shift;