Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
- Copyright (C) 2015-2017 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
+ Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
return b ^= SquareBB[s];
}
-inline bool more_than_one(Bitboard b) {
+constexpr bool more_than_one(Bitboard b) {
return b & (b - 1);
}
-
/// rank_bb() and file_bb() return a bitboard representing all the squares on
/// the given file or rank.
}
-/// shift() moves a bitboard one step along direction D. Mainly for pawns
+/// make_bitboard() returns a bitboard from a list of squares
+
+constexpr Bitboard make_bitboard() { return 0; }
+
+template<typename ...Squares>
+constexpr Bitboard make_bitboard(Square s, Squares... squares) {
+ return (1ULL << s) | make_bitboard(squares...);
+}
-template<Square D>
-inline Bitboard shift(Bitboard b) {
+
+/// shift() moves a bitboard one step along direction D (mainly for pawns)
+
+template<Direction D>
+constexpr Bitboard shift(Bitboard b) {
return D == NORTH ? b << 8 : D == SOUTH ? b >> 8
: D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == SOUTH_EAST ? (b & ~FileHBB) >> 7
: D == NORTH_WEST ? (b & ~FileABB) << 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
}
+/// pawn_attacks_bb() returns the pawn attacks for the given color from the
+/// squares in the given bitboard.
+
+template<Color C>
+constexpr Bitboard pawn_attacks_bb(Bitboard b) {
+ return C == WHITE ? shift<NORTH_WEST>(b) | shift<NORTH_EAST>(b)
+ : shift<SOUTH_WEST>(b) | shift<SOUTH_EAST>(b);
+}
+
+
/// adjacent_files_bb() returns a bitboard representing all the squares on the
/// adjacent files of the given one.
}
-/// forward_ranks_bb() returns a bitboard representing all the squares on all the ranks
-/// in front of the given one, from the point of view of the given color. For
-/// instance, forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
+/// forward_ranks_bb() returns a bitboard representing the squares on all the ranks
+/// in front of the given one, from the point of view of the given color. For instance,
+/// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
inline Bitboard forward_ranks_bb(Color c, Square s) {
return ForwardRanksBB[c][rank_of(s)];
/// lsb() and msb() return the least/most significant bit in a non-zero bitboard
-#if defined(__GNUC__)
+#if defined(__GNUC__) // GCC, Clang, ICC
inline Square lsb(Bitboard b) {
assert(b);
return Square(63 ^ __builtin_clzll(b));
}
-#elif defined(_WIN64) && defined(_MSC_VER)
+#elif defined(_MSC_VER) // MSVC
+
+#ifdef _WIN64 // MSVC, WIN64
inline Square lsb(Bitboard b) {
assert(b);
return (Square) idx;
}
-#else
+#else // MSVC, WIN32
+
+inline Square lsb(Bitboard b) {
+ assert(b);
+ unsigned long idx;
+
+ if (b & 0xffffffff) {
+ _BitScanForward(&idx, int32_t(b));
+ return Square(idx);
+ } else {
+ _BitScanForward(&idx, int32_t(b >> 32));
+ return Square(idx + 32);
+ }
+}
+
+inline Square msb(Bitboard b) {
+ assert(b);
+ unsigned long idx;
+
+ if (b >> 32) {
+ _BitScanReverse(&idx, int32_t(b >> 32));
+ return Square(idx + 32);
+ } else {
+ _BitScanReverse(&idx, int32_t(b));
+ return Square(idx);
+ }
+}
+
+#endif
-#define NO_BSF // Fallback on software implementation for other cases
+#else // Compiler is neither GCC nor MSVC compatible
-Square lsb(Bitboard b);
-Square msb(Bitboard b);
+#error "Compiler not supported."
#endif