/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
- Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
+ Copyright (C) 2008-2013 Marco Costalba, Joona Kiiski, 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
namespace Bitboards {
-extern void init();
-extern void print(Bitboard b);
+void init();
+void print(Bitboard b);
}
-CACHE_LINE_ALIGNMENT
+namespace Bitbases {
+
+void init_kpk();
+bool probe_kpk(Square wksq, Square wpsq, Square bksq, Color us);
-extern Bitboard RMasks[64];
-extern Bitboard RMagics[64];
-extern Bitboard* RAttacks[64];
-extern unsigned RShifts[64];
-
-extern Bitboard BMasks[64];
-extern Bitboard BMagics[64];
-extern Bitboard* BAttacks[64];
-extern unsigned BShifts[64];
-
-extern Bitboard SquareBB[64];
-extern Bitboard FileBB[8];
-extern Bitboard RankBB[8];
-extern Bitboard AdjacentFilesBB[8];
-extern Bitboard ThisAndAdjacentFilesBB[8];
-extern Bitboard InFrontBB[2][8];
-extern Bitboard StepAttacksBB[16][64];
-extern Bitboard BetweenBB[64][64];
-extern Bitboard ForwardBB[2][64];
-extern Bitboard PassedPawnMask[2][64];
-extern Bitboard AttackSpanMask[2][64];
-extern Bitboard PseudoAttacks[6][64];
+}
+CACHE_LINE_ALIGNMENT
+
+extern Bitboard RMasks[SQUARE_NB];
+extern Bitboard RMagics[SQUARE_NB];
+extern Bitboard* RAttacks[SQUARE_NB];
+extern unsigned RShifts[SQUARE_NB];
+
+extern Bitboard BMasks[SQUARE_NB];
+extern Bitboard BMagics[SQUARE_NB];
+extern Bitboard* BAttacks[SQUARE_NB];
+extern unsigned BShifts[SQUARE_NB];
+
+extern Bitboard SquareBB[SQUARE_NB];
+extern Bitboard FileBB[FILE_NB];
+extern Bitboard RankBB[RANK_NB];
+extern Bitboard AdjacentFilesBB[FILE_NB];
+extern Bitboard InFrontBB[COLOR_NB][RANK_NB];
+extern Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
+extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
+extern Bitboard DistanceRingsBB[SQUARE_NB][8];
+extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
+extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
+extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
+extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
+
+const Bitboard BlackSquares = 0xAA55AA55AA55AA55ULL;
/// 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.
}
+/// shift_bb() moves bitboard one step along direction Delta. Mainly for pawns.
+
+template<Square Delta>
+inline Bitboard shift_bb(Bitboard b) {
+
+ return Delta == DELTA_N ? b << 8 : Delta == DELTA_S ? b >> 8
+ : Delta == DELTA_NE ? (b & ~FileHBB) << 9 : Delta == DELTA_SE ? (b & ~FileHBB) >> 7
+ : Delta == DELTA_NW ? (b & ~FileABB) << 7 : Delta == DELTA_SW ? (b & ~FileABB) >> 9
+ : 0;
+}
+
+
/// rank_bb() and file_bb() take a file or a square as input and return
/// a bitboard representing all squares on the given file or rank.
}
-/// this_and_adjacent_files_bb takes a file as input and returns a bitboard
-/// representing all squares on the given and adjacent files.
-
-inline Bitboard this_and_adjacent_files_bb(File f) {
- return ThisAndAdjacentFilesBB[f];
-}
-
-
/// in_front_bb() takes a color and a rank or square as input, and returns a
/// bitboard representing all the squares on all ranks in front of the rank
/// (or square), from the given color's point of view. For instance,
/// when it moves along its file starting from the given square. Definition is:
/// AttackSpanMask[c][s] = in_front_bb(c, s) & adjacent_files_bb(s);
-inline Bitboard attack_span_mask(Color c, Square s) {
- return AttackSpanMask[c][s];
+inline Bitboard pawn_attack_span(Color c, Square s) {
+ return PawnAttackSpan[c][s];
}
/// the same color of the given square.
inline Bitboard same_color_squares(Square s) {
- return Bitboard(0xAA55AA55AA55AA55ULL) & s ? 0xAA55AA55AA55AA55ULL
- : ~0xAA55AA55AA55AA55ULL;
+ return BlackSquares & s ? BlackSquares : ~BlackSquares;
}
}
-/// 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.
+/// lsb()/msb() finds the least/most significant bit in a nonzero bitboard.
+/// pop_lsb() finds and clears the least significant bit in a nonzero bitboard.
#if defined(USE_BSFQ)
-#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
+# if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
-FORCE_INLINE Square first_1(Bitboard b) {
+FORCE_INLINE Square lsb(Bitboard b) {
unsigned long index;
_BitScanForward64(&index, b);
return (Square) index;
}
-FORCE_INLINE Square last_1(Bitboard b) {
+FORCE_INLINE Square msb(Bitboard b) {
unsigned long index;
_BitScanReverse64(&index, b);
return (Square) index;
}
-#else
-FORCE_INLINE Square first_1(Bitboard b) { // Assembly code by Heinz van Saanen
- Bitboard dummy;
- __asm__("bsfq %1, %0": "=r"(dummy): "rm"(b) );
- return (Square) dummy;
+# elif defined(__arm__)
+
+FORCE_INLINE int lsb32(uint32_t v) {
+ __asm__("rbit %0, %1" : "=r"(v) : "r"(v));
+ return __builtin_clz(v);
}
-FORCE_INLINE Square last_1(Bitboard b) {
- Bitboard dummy;
- __asm__("bsrq %1, %0": "=r"(dummy): "rm"(b) );
- return (Square) dummy;
+FORCE_INLINE Square msb(Bitboard b) {
+ return (Square) (63 - __builtin_clzll(b));
}
-#endif
-FORCE_INLINE Square pop_1st_bit(Bitboard* b) {
- const Square s = first_1(*b);
- *b &= ~(1ULL<<s);
+FORCE_INLINE Square lsb(Bitboard b) {
+ return (Square) (uint32_t(b) ? lsb32(uint32_t(b)) : 32 + lsb32(uint32_t(b >> 32)));
+}
+
+# else
+
+FORCE_INLINE Square lsb(Bitboard b) { // Assembly code by Heinz van Saanen
+ Bitboard index;
+ __asm__("bsfq %1, %0": "=r"(index): "rm"(b) );
+ return (Square) index;
+}
+
+FORCE_INLINE Square msb(Bitboard b) {
+ Bitboard index;
+ __asm__("bsrq %1, %0": "=r"(index): "rm"(b) );
+ return (Square) index;
+}
+
+# endif
+
+FORCE_INLINE Square pop_lsb(Bitboard* b) {
+ const Square s = lsb(*b);
+ *b &= *b - 1;
return s;
}
#else // if !defined(USE_BSFQ)
-extern Square first_1(Bitboard b);
-extern Square last_1(Bitboard b);
-extern Square pop_1st_bit(Bitboard* b);
+extern Square msb(Bitboard b);
+extern Square lsb(Bitboard b);
+extern Square pop_lsb(Bitboard* b);
#endif