mirror of
https://github.com/sockspls/badfish
synced 2025-04-29 16:23:09 +00:00
Unify BitCountType selection
Now that HasPopCnt is a compile time constant we can centralize and unify the BitCountType selection. Also rename count_1s() in the more standard popcount() No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
fb4b4f772e
commit
e9296d694c
6 changed files with 50 additions and 44 deletions
|
@ -154,7 +154,7 @@ Square pop_1st_bit(Bitboard* bb) {
|
|||
void bitboards_init() {
|
||||
|
||||
for (Bitboard b = 0; b < 256; b++)
|
||||
BitCount8Bit[b] = (uint8_t)count_1s<CNT32_MAX15>(b);
|
||||
BitCount8Bit[b] = (uint8_t)popcount<Max15>(b);
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
{
|
||||
|
@ -321,7 +321,7 @@ namespace {
|
|||
// the number of 1s of the mask. Hence we deduce the size of the shift to
|
||||
// apply to the 64 or 32 bits word to get the index.
|
||||
masks[s] = sliding_attacks(pt, s, 0) & ~edges;
|
||||
shifts[s] = (Is64Bit ? 64 : 32) - count_1s<CNT32_MAX15>(masks[s]);
|
||||
shifts[s] = (Is64Bit ? 64 : 32) - popcount<Max15>(masks[s]);
|
||||
|
||||
// Use Carry-Rippler trick to enumerate all subsets of masks[s] and
|
||||
// store the corresponding sliding attacks bitboard in reference[].
|
||||
|
|
|
@ -21,25 +21,29 @@
|
|||
#if !defined(BITCOUNT_H_INCLUDED)
|
||||
#define BITCOUNT_H_INCLUDED
|
||||
|
||||
#include <cassert>
|
||||
#include "types.h"
|
||||
|
||||
enum BitCountType {
|
||||
CNT64,
|
||||
CNT64_MAX15,
|
||||
CNT32,
|
||||
CNT32_MAX15,
|
||||
CNT_POPCNT
|
||||
CNT_64,
|
||||
CNT_64_MAX15,
|
||||
CNT_32,
|
||||
CNT_32_MAX15,
|
||||
CNT_HW_POPCNT
|
||||
};
|
||||
|
||||
/// count_1s() counts the number of nonzero bits in a bitboard.
|
||||
/// We have different optimized versions according if platform
|
||||
/// is 32 or 64 bits, and to the maximum number of nonzero bits.
|
||||
/// We also support hardware popcnt instruction. See Readme.txt
|
||||
/// on how to pgo compile with popcnt support.
|
||||
template<BitCountType> inline int count_1s(Bitboard);
|
||||
/// Determine at compile time the best popcount<> specialization according if
|
||||
/// platform is 32 or 64 bits, to the maximum number of nonzero bits to count or
|
||||
/// use hardware popcnt instruction when available.
|
||||
const BitCountType Full = HasPopCnt ? CNT_HW_POPCNT : Is64Bit ? CNT_64 : CNT_32;
|
||||
const BitCountType Max15 = HasPopCnt ? CNT_HW_POPCNT : Is64Bit ? CNT_64_MAX15 : CNT_32_MAX15;
|
||||
|
||||
|
||||
/// popcount() counts the number of nonzero bits in a bitboard
|
||||
template<BitCountType> inline int popcount(Bitboard);
|
||||
|
||||
template<>
|
||||
inline int count_1s<CNT64>(Bitboard b) {
|
||||
inline int popcount<CNT_64>(Bitboard b) {
|
||||
b -= ((b>>1) & 0x5555555555555555ULL);
|
||||
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
|
||||
b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL;
|
||||
|
@ -48,7 +52,7 @@ inline int count_1s<CNT64>(Bitboard b) {
|
|||
}
|
||||
|
||||
template<>
|
||||
inline int count_1s<CNT64_MAX15>(Bitboard b) {
|
||||
inline int popcount<CNT_64_MAX15>(Bitboard b) {
|
||||
b -= (b>>1) & 0x5555555555555555ULL;
|
||||
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
|
||||
b *= 0x1111111111111111ULL;
|
||||
|
@ -56,7 +60,7 @@ inline int count_1s<CNT64_MAX15>(Bitboard b) {
|
|||
}
|
||||
|
||||
template<>
|
||||
inline int count_1s<CNT32>(Bitboard b) {
|
||||
inline int popcount<CNT_32>(Bitboard b) {
|
||||
unsigned w = unsigned(b >> 32), v = unsigned(b);
|
||||
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
|
||||
w -= (w >> 1) & 0x55555555;
|
||||
|
@ -69,7 +73,7 @@ inline int count_1s<CNT32>(Bitboard b) {
|
|||
}
|
||||
|
||||
template<>
|
||||
inline int count_1s<CNT32_MAX15>(Bitboard b) {
|
||||
inline int popcount<CNT_32_MAX15>(Bitboard b) {
|
||||
unsigned w = unsigned(b >> 32), v = unsigned(b);
|
||||
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
|
||||
w -= (w >> 1) & 0x55555555;
|
||||
|
@ -81,17 +85,27 @@ inline int count_1s<CNT32_MAX15>(Bitboard b) {
|
|||
}
|
||||
|
||||
template<>
|
||||
inline int count_1s<CNT_POPCNT>(Bitboard b) {
|
||||
inline int popcount<CNT_HW_POPCNT>(Bitboard b) {
|
||||
|
||||
#if !defined(USE_POPCNT)
|
||||
|
||||
assert(false);
|
||||
return int(b != 0); // Avoid 'b not used' warning
|
||||
|
||||
#elif defined(_MSC_VER) && defined(__INTEL_COMPILER)
|
||||
|
||||
return _mm_popcnt_u64(b);
|
||||
|
||||
#elif defined(_MSC_VER)
|
||||
|
||||
return (int)__popcnt64(b);
|
||||
#elif defined(__GNUC__)
|
||||
|
||||
#else
|
||||
|
||||
unsigned long ret;
|
||||
__asm__("popcnt %1, %0" : "=r" (ret) : "r" (b));
|
||||
return ret;
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -369,7 +369,7 @@ Value Endgame<KBBKN>::operator()(const Position& pos) const {
|
|||
result += Value(square_distance(bksq, nsq) * 32);
|
||||
|
||||
// Bonus for restricting the knight's mobility
|
||||
result += Value((8 - count_1s<CNT32_MAX15>(pos.attacks_from<KNIGHT>(nsq))) * 8);
|
||||
result += Value((8 - popcount<Max15>(pos.attacks_from<KNIGHT>(nsq))) * 8);
|
||||
|
||||
return strongerSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
|
|
@ -422,7 +422,6 @@ namespace {
|
|||
template<Color Us>
|
||||
void init_eval_info(const Position& pos, EvalInfo& ei) {
|
||||
|
||||
const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : Is64Bit ? CNT64_MAX15 : CNT32_MAX15;
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
Bitboard b = ei.attackedBy[Them][KING] = pos.attacks_from<KING>(pos.king_square(Them));
|
||||
|
@ -434,7 +433,7 @@ namespace {
|
|||
{
|
||||
ei.kingRing[Them] = (b | (Us == WHITE ? b >> 8 : b << 8));
|
||||
b &= ei.attackedBy[Us][PAWN];
|
||||
ei.kingAttackersCount[Us] = b ? count_1s<Max15>(b) / 2 : 0;
|
||||
ei.kingAttackersCount[Us] = b ? popcount<Max15>(b) / 2 : 0;
|
||||
ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = 0;
|
||||
} else
|
||||
ei.kingRing[Them] = ei.kingAttackersCount[Us] = 0;
|
||||
|
@ -478,8 +477,6 @@ namespace {
|
|||
File f;
|
||||
Score score = SCORE_ZERO;
|
||||
|
||||
const BitCountType Full = HasPopCnt ? CNT_POPCNT : Is64Bit ? CNT64 : CNT32;
|
||||
const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : Is64Bit ? CNT64_MAX15 : CNT32_MAX15;
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
const Square* pl = pos.piece_list(Us, Piece);
|
||||
|
||||
|
@ -507,12 +504,12 @@ namespace {
|
|||
ei.kingAttackersWeight[Us] += KingAttackWeights[Piece];
|
||||
Bitboard bb = (b & ei.attackedBy[Them][KING]);
|
||||
if (bb)
|
||||
ei.kingAdjacentZoneAttacksCount[Us] += count_1s<Max15>(bb);
|
||||
ei.kingAdjacentZoneAttacksCount[Us] += popcount<Max15>(bb);
|
||||
}
|
||||
|
||||
// Mobility
|
||||
mob = (Piece != QUEEN ? count_1s<Max15>(b & mobilityArea)
|
||||
: count_1s<Full >(b & mobilityArea));
|
||||
mob = (Piece != QUEEN ? popcount<Max15>(b & mobilityArea)
|
||||
: popcount<Full >(b & mobilityArea));
|
||||
|
||||
mobility += MobilityBonus[Piece][mob];
|
||||
|
||||
|
@ -667,7 +664,6 @@ namespace {
|
|||
template<Color Us, bool Trace>
|
||||
Score evaluate_king(const Position& pos, EvalInfo& ei, Value margins[]) {
|
||||
|
||||
const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : Is64Bit ? CNT64_MAX15 : CNT32_MAX15;
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
Bitboard undefended, b, b1, b2, safe;
|
||||
|
@ -695,7 +691,7 @@ namespace {
|
|||
// attacked and undefended squares around our king, the square of the
|
||||
// king, and the quality of the pawn shelter.
|
||||
attackUnits = std::min(25, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2)
|
||||
+ 3 * (ei.kingAdjacentZoneAttacksCount[Them] + count_1s<Max15>(undefended))
|
||||
+ 3 * (ei.kingAdjacentZoneAttacksCount[Them] + popcount<Max15>(undefended))
|
||||
+ InitKingDanger[relative_square(Us, ksq)]
|
||||
- mg_value(ei.pi->king_shelter<Us>(pos, ksq)) / 32;
|
||||
|
||||
|
@ -709,7 +705,7 @@ namespace {
|
|||
| ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][ROOK]);
|
||||
if (b)
|
||||
attackUnits += QueenContactCheckBonus
|
||||
* count_1s<Max15>(b)
|
||||
* popcount<Max15>(b)
|
||||
* (Them == pos.side_to_move() ? 2 : 1);
|
||||
}
|
||||
|
||||
|
@ -727,7 +723,7 @@ namespace {
|
|||
| ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][QUEEN]);
|
||||
if (b)
|
||||
attackUnits += RookContactCheckBonus
|
||||
* count_1s<Max15>(b)
|
||||
* popcount<Max15>(b)
|
||||
* (Them == pos.side_to_move() ? 2 : 1);
|
||||
}
|
||||
|
||||
|
@ -740,22 +736,22 @@ namespace {
|
|||
// Enemy queen safe checks
|
||||
b = (b1 | b2) & ei.attackedBy[Them][QUEEN];
|
||||
if (b)
|
||||
attackUnits += QueenCheckBonus * count_1s<Max15>(b);
|
||||
attackUnits += QueenCheckBonus * popcount<Max15>(b);
|
||||
|
||||
// Enemy rooks safe checks
|
||||
b = b1 & ei.attackedBy[Them][ROOK];
|
||||
if (b)
|
||||
attackUnits += RookCheckBonus * count_1s<Max15>(b);
|
||||
attackUnits += RookCheckBonus * popcount<Max15>(b);
|
||||
|
||||
// Enemy bishops safe checks
|
||||
b = b2 & ei.attackedBy[Them][BISHOP];
|
||||
if (b)
|
||||
attackUnits += BishopCheckBonus * count_1s<Max15>(b);
|
||||
attackUnits += BishopCheckBonus * popcount<Max15>(b);
|
||||
|
||||
// Enemy knights safe checks
|
||||
b = pos.attacks_from<KNIGHT>(ksq) & ei.attackedBy[Them][KNIGHT] & safe;
|
||||
if (b)
|
||||
attackUnits += KnightCheckBonus * count_1s<Max15>(b);
|
||||
attackUnits += KnightCheckBonus * popcount<Max15>(b);
|
||||
|
||||
// To index KingDangerTable[] attackUnits must be in [0, 99] range
|
||||
attackUnits = std::min(99, std::max(0, attackUnits));
|
||||
|
@ -879,8 +875,6 @@ namespace {
|
|||
|
||||
Score evaluate_unstoppable_pawns(const Position& pos, EvalInfo& ei) {
|
||||
|
||||
const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : Is64Bit ? CNT64_MAX15 : CNT32_MAX15;
|
||||
|
||||
Bitboard b, b2, blockers, supporters, queeningPath, candidates;
|
||||
Square s, blockSq, queeningSquare;
|
||||
Color c, winnerSide, loserSide;
|
||||
|
@ -918,7 +912,7 @@ namespace {
|
|||
assert((queeningPath & pos.occupied_squares()) == (queeningPath & pos.pieces(c)));
|
||||
|
||||
// Add moves needed to free the path from friendly pieces and retest condition
|
||||
movesToGo += count_1s<Max15>(queeningPath & pos.pieces(c));
|
||||
movesToGo += popcount<Max15>(queeningPath & pos.pieces(c));
|
||||
|
||||
if (movesToGo >= oppMovesToGo && !pathDefended)
|
||||
continue;
|
||||
|
@ -1046,7 +1040,6 @@ namespace {
|
|||
template<Color Us>
|
||||
int evaluate_space(const Position& pos, EvalInfo& ei) {
|
||||
|
||||
const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : Is64Bit ? CNT64_MAX15 : CNT32_MAX15;
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
// Find the safe squares for our pieces inside the area defined by
|
||||
|
@ -1062,7 +1055,7 @@ namespace {
|
|||
behind |= (Us == WHITE ? behind >> 8 : behind << 8);
|
||||
behind |= (Us == WHITE ? behind >> 16 : behind << 16);
|
||||
|
||||
return count_1s<Max15>(safe) + count_1s<Max15>(behind & safe);
|
||||
return popcount<Max15>(safe) + popcount<Max15>(behind & safe);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -116,7 +116,6 @@ template<Color Us>
|
|||
Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
|
||||
Bitboard theirPawns, PawnInfo* pi) {
|
||||
|
||||
const BitCountType Max15 = Is64Bit ? CNT64_MAX15 : CNT32_MAX15;
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
Bitboard b;
|
||||
|
@ -183,7 +182,7 @@ Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
|
|||
// enemy pawns in the forward direction on the neighboring files.
|
||||
candidate = !(opposed | passed | backward | isolated)
|
||||
&& (b = attack_span_mask(Them, s + pawn_push(Us)) & ourPawns) != 0
|
||||
&& count_1s<Max15>(b) >= count_1s<Max15>(attack_span_mask(Us, s) & theirPawns);
|
||||
&& popcount<Max15>(b) >= popcount<Max15>(attack_span_mask(Us, s) & theirPawns);
|
||||
|
||||
// Passed pawns will be properly scored in evaluation because we need
|
||||
// full attack info to evaluate passed pawns. Only the frontmost passed
|
||||
|
|
|
@ -1691,7 +1691,7 @@ bool Position::pos_is_ok(int* failedStep) const {
|
|||
|
||||
// Is there more than 2 checkers?
|
||||
if (failedStep) (*failedStep)++;
|
||||
if (debugCheckerCount && count_1s<CNT32>(st->checkersBB) > 2)
|
||||
if (debugCheckerCount && popcount<Full>(st->checkersBB) > 2)
|
||||
return false;
|
||||
|
||||
// Bitboards OK?
|
||||
|
@ -1760,7 +1760,7 @@ bool Position::pos_is_ok(int* failedStep) const {
|
|||
if (debugPieceCounts)
|
||||
for (Color c = WHITE; c <= BLACK; c++)
|
||||
for (PieceType pt = PAWN; pt <= KING; pt++)
|
||||
if (pieceCount[c][pt] != count_1s<CNT32>(pieces(pt, c)))
|
||||
if (pieceCount[c][pt] != popcount<Full>(pieces(pt, c)))
|
||||
return false;
|
||||
|
||||
if (failedStep) (*failedStep)++;
|
||||
|
|
Loading…
Add table
Reference in a new issue