bb: Rewrite bitboard.cpp and bitboard.h

This commit is contained in:
Calcitem 2020-12-19 01:23:45 +08:00
parent 511113e773
commit 777a91520e
2 changed files with 71 additions and 129 deletions

View File

@ -53,10 +53,13 @@ const std::string Bitboards::pretty(Bitboard b)
void Bitboards::init()
{
for (unsigned i = 0; i < (1 << 16); ++i)
PopCnt16[i] = (uint8_t)std::bitset<16>(i).count();
for (Square s = SQ_BEGIN; s < SQ_END; ++s)
SquareBB[s] = (1UL << s);
for (Square s1 = SQ_A1; s1 <= SQ_C8; ++s1)
for (Square s2 = SQ_A1; s2 <= SQ_C8; ++s2)
SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));
}

View File

@ -16,21 +16,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BITBOARD_H
#define BITBOARD_H
#ifndef BITBOARD_H_INCLUDED
#define BITBOARD_H_INCLUDED
#include <string>
#include "types.h"
namespace Bitbases
{
void init();
bool probe(Square wksq, Square wpsq, Square bksq, Color us);
}
namespace Bitboards
{
@ -42,34 +34,18 @@ const std::string pretty(Bitboard b);
constexpr Bitboard AllSquares = ~Bitboard(0);
//constexpr Bitboard starSquares12 = 0xAA55AA55AA55AA55UL; // TODO
constexpr Bitboard FileABB = 0xE0000000;
constexpr Bitboard FileBBB = 0x00E00000;
constexpr Bitboard FileCBB = 0x0000E000;
constexpr Bitboard FileDBB = 0x11111100;
constexpr Bitboard FileEBB = 0x00000E00;
constexpr Bitboard FileFBB = 0x000E0000;
constexpr Bitboard FileGBB = 0x0E000000;
constexpr Bitboard FileABB = 0x0000FF00;
constexpr Bitboard FileBBB = FileABB << (8 * 1);
constexpr Bitboard FileCBB = FileABB << (8 * 2);
constexpr Bitboard Rank1BB = 0x38000000;
constexpr Bitboard Rank2BB = 0x00380000;
constexpr Bitboard Rank3BB = 0x00003800;
constexpr Bitboard Rank4BB = 0x44444400;
constexpr Bitboard Rank5BB = 0x00008300;
constexpr Bitboard Rank6BB = 0x00830000;
constexpr Bitboard Rank7BB = 0x83000000;
constexpr Bitboard Ring1 = 0xFF00;
constexpr Bitboard Ring2 = Ring1 << (8 * 1);
constexpr Bitboard Ring3 = Ring1 << (8 * 2);
constexpr Bitboard Seat1 = 0x01010100;
constexpr Bitboard Seat2 = Seat1 << 1;
constexpr Bitboard Seat3 = Seat1 << 2;
constexpr Bitboard Seat4 = Seat1 << 3;
constexpr Bitboard Seat5 = Seat1 << 4;
constexpr Bitboard Seat6 = Seat1 << 5;
constexpr Bitboard Seat7 = Seat1 << 6;
constexpr Bitboard Seat8 = Seat1 << 7;
constexpr Bitboard Rank1BB = 0x01010100;
constexpr Bitboard Rank2BB = Rank1BB << 1;
constexpr Bitboard Rank3BB = Rank1BB << 2;
constexpr Bitboard Rank4BB = Rank1BB << 3;
constexpr Bitboard Rank5BB = Rank1BB << 4;
constexpr Bitboard Rank6BB = Rank1BB << 5;
constexpr Bitboard Rank7BB = Rank1BB << 6;
constexpr Bitboard Rank8BB = Rank1BB << 7;
extern uint8_t PopCnt16[1 << 16];
extern uint8_t SquareDistance[SQ_32][SQ_32];
@ -77,12 +53,17 @@ extern uint8_t SquareDistance[SQ_32][SQ_32];
extern Bitboard SquareBB[SQ_32];
extern Bitboard LineBB[EFFECTIVE_SQUARE_NB][SQ_32];
// TODO
const Bitboard Star9 = SquareBB[17] | SquareBB[19] | SquareBB[21] | SquareBB[23];
const Bitboard Star12 = SquareBB[16] | SquareBB[18] | SquareBB[20] | SquareBB[22];
inline Bitboard square_bb(Square s)
{
assert(SQ_BEGIN <= s && s < SQ_END);
return SquareBB[s];
}
/// Overloads of bitwise operators between a Bitboard and a Square for testing
/// whether a given bit is set in a bitboard, and for setting and clearing bits.
@ -90,35 +71,59 @@ inline Bitboard operator&(Bitboard b, Square s)
{
return b & square_bb(s);
}
inline Bitboard operator|(Bitboard b, Square s)
{
return b | square_bb(s);
}
inline Bitboard operator^(Bitboard b, Square s)
{
return b ^ square_bb(s);
}
inline Bitboard &operator|=(Bitboard &b, Square s)
{
return b |= square_bb(s);
}
inline Bitboard &operator^=(Bitboard &b, Square s)
{
return b ^= square_bb(s);
}
inline Bitboard operator&(Square s, Bitboard b)
{
return b & s;
}
inline Bitboard operator|(Square s, Bitboard b)
{
return b | s;
}
inline Bitboard operator^(Square s, Bitboard b)
{
return b ^ s;
}
inline Bitboard operator|(Square s1, Square s2)
{
return square_bb(s1) | s2;
}
constexpr bool more_than_one(Bitboard b)
{
return b & (b - 1);
}
# if 0
/// rank_bb() and file_bb() return a bitboard representing all the squares on
/// the given file or rank.
inline Bitboard rank_bb(Rank r)
{
return Rank1BB << (8 * r);
return Rank1BB << (r - 1);
}
inline Bitboard rank_bb(Square s)
@ -128,131 +133,64 @@ inline Bitboard rank_bb(Square s)
inline Bitboard file_bb(File f)
{
return FileABB << f;
return FileABB << (f - 1);
}
inline Bitboard file_bb(Square s)
{
return file_bb(file_of(s));
}
#endif
inline Bitboard ring_bb(File file)
{
return Ring1 << (8 * (file - 1));
}
inline Bitboard seat_bb(Rank rank)
{
return Seat1 << (rank - 1);
}
#if 0
/// shift() moves a bitboard one step along direction D
template<MoveDirection D>
constexpr Bitboard shift(Bitboard b)
{
return D == NORTH ? b << 8 : D == SOUTH ? b >> 8
: D == NORTH + NORTH ? b << 16 : D == SOUTH + SOUTH ? b >> 16
: D == EAST ? (b & ~FileHBB) << 1 : D == WEST ? (b & ~FileABB) >> 1
: D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7
: D == SOUTH_EAST ? (b & ~FileHBB) >> 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
: 0;
}
/// adjacent_files_bb() returns a bitboard representing all the squares on the
/// adjacent files of the given one.
inline Bitboard adjacent_files_bb(Square s)
{
return shift<EAST>(file_bb(s)) | shift<WEST>(file_bb(s));
}
/// between_bb() returns squares that are linearly between the given squares
/// If the given squares are not on a same file/rank/diagonal, return 0.
inline Bitboard between_bb(Square s1, Square s2)
{
return LineBB[s1][s2] & ((AllSquares << (s1 + (s1 < s2)))
^ (AllSquares << (s2 + !(s1 < s2))));
}
/// forward_ranks_bb() returns a bitboard representing the squares on the ranks
/// in front of the given one, from the point of view of the given color. For instance,
/// forward_ranks_bb(BLACK, SQ_12_R1S5_D3) will return the 16 squares on ranks 1 and 2.
inline Bitboard forward_ranks_bb(Color c, Square s)
{
return c == WHITE ? ~Rank1BB << 8 * (rank_of(s) - RANK_1)
: ~Rank8BB >> 8 * (RANK_8 - rank_of(s));
}
/// forward_file_bb() returns a bitboard representing all the squares along the
/// line in front of the given one, from the point of view of the given color.
inline Bitboard forward_file_bb(Color c, Square s)
{
return forward_ranks_bb(c, s) & file_bb(s);
}
/// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
/// straight or on a diagonal line.
inline bool aligned(Square s1, Square s2, Square s3)
{
return LineBB[s1][s2] & s3;
}
/// distance() functions return the distance between x and y, defined as the
/// number of steps for a king in x to reach y.
template<typename T1 = Square> inline int distance(Square x, Square y);
template<> inline int distance<File>(Square x, Square y)
{
return std::abs(file_of(x) - file_of(y));
}
template<> inline int distance<Rank>(Square x, Square y)
{
return std::abs(rank_of(x) - rank_of(y));
}
template<> inline int distance<Square>(Square x, Square y)
{
return SquareDistance[x][y];
}
template<class T> constexpr const T &clamp(const T &v, const T &lo, const T &hi)
inline int edge_distance(File f)
{
return v < lo ? lo : v > hi ? hi : v;
return std::min(f, File(FILE_C - f));
}
inline int edge_distance(Rank r)
{
return std::min(r, Rank(RANK_8 - r));
}
#endif
/// popcount() counts the number of non-zero bits in a bitboard
inline int popcount(Bitboard b)
{
#ifndef USE_POPCNT
union
{
Bitboard bb; uint16_t u[4];
Bitboard bb; uint16_t u[2];
} v = { b };
return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
return PopCnt16[v.u[0]] + PopCnt16[v.u[1]];
#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
return (int)_mm_popcnt_u64(b);
return (int)_mm_popcnt_u32(b);
#else // Assumed gcc or compatible compiler
return __builtin_popcountll(b);
return __builtin_popcount(b);
#endif
}
@ -265,13 +203,13 @@ inline int popcount(Bitboard b)
inline Square lsb(Bitboard b)
{
assert(b);
return Square(__builtin_ctzll(b));
return Square(__builtin_ctz(b));
}
inline Square msb(Bitboard b)
{
assert(b);
return Square(63 ^ __builtin_clzll(b));
return Square(31 ^ __builtin_clz(b));
}
#elif defined(_MSC_VER) // MSVC
@ -282,7 +220,7 @@ inline Square lsb(Bitboard b)
{
assert(b);
unsigned long idx;
_BitScanForward64(&idx, b);
_BitScanForward(&idx, b);
return (Square)idx;
}
@ -290,7 +228,7 @@ inline Square msb(Bitboard b)
{
assert(b);
unsigned long idx;
_BitScanReverse64(&idx, b);
_BitScanReverse(&idx, b);
return (Square)idx;
}
@ -343,10 +281,11 @@ inline Square pop_lsb(Bitboard *b)
}
/// frontmost_sq() returns the most advanced square for the given color
/// frontmost_sq() returns the most advanced square for the given color,
/// requires a non-zero bitboard.
inline Square frontmost_sq(Color c, Bitboard b)
{
return c == WHITE ? msb(b) : lsb(b);
}
#endif // BITBOARD_H
#endif // #ifndef BITBOARD_H_INCLUDED