Get rid of macros and use templates instead,
this is safer and allows us fix the warning:
ISO C++ forbids braced-groups within expressions
That broke compilation with -pedantic flag under
gcc and POPCNT enabled.
No functional and no performance change.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
CXXFLAGS = -g -Wall -Wcast-qual -ansi -fno-exceptions -fno-rtti $(EXTRACXXFLAGS)
ifeq ($(comp),gcc)
CXXFLAGS = -g -Wall -Wcast-qual -ansi -fno-exceptions -fno-rtti $(EXTRACXXFLAGS)
ifeq ($(comp),gcc)
- CXXFLAGS += -Wno-long-long -Wextra
+ CXXFLAGS += -pedantic -Wno-long-long -Wextra
}
for (Bitboard b = 0ULL; b < 256ULL; b++)
}
for (Bitboard b = 0ULL; b < 256ULL; b++)
- BitCount8Bit[b] = (uint8_t)count_1s(b);
+ BitCount8Bit[b] = (uint8_t)count_1s<CNT32>(b);
}
int remove_bit_8(int i) { return ((i & ~15) >> 1) | (i & 7); }
}
int remove_bit_8(int i) { return ((i & ~15) >> 1) | (i & 7); }
Bitboard index_to_bitboard(int index, Bitboard mask) {
Bitboard result = 0ULL;
Bitboard index_to_bitboard(int index, Bitboard mask) {
Bitboard result = 0ULL;
- int bits = count_1s(mask);
+ int bits = count_1s<CNT32>(mask);
for (int i = 0; i < bits; i++)
{
for (int i = 0; i < bits; i++)
{
-// Select type of intrinsic bit count instruction to use, see
-// README.txt on how to pgo compile with POPCNT support.
-#if !defined(USE_POPCNT)
-#define POPCNT_INTRINSIC(x) 0
-#elif defined(_MSC_VER)
-#define POPCNT_INTRINSIC(x) (int)__popcnt64(x)
-#elif defined(__GNUC__)
-
-#define POPCNT_INTRINSIC(x) ({ \
- unsigned long __ret; \
- __asm__("popcnt %1, %0" : "=r" (__ret) : "r" (x)); \
- __ret; })
-
-#endif
+enum BitCountType {
+ CNT64,
+ CNT64_MAX15,
+ CNT32,
+ CNT32_MAX15,
+ CNT_POPCNT
+};
-
-/// Software implementation of bit count functions
-
-#if defined(IS_64BIT)
-
-inline int count_1s(Bitboard b) {
+/// 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);
+
+template<>
+inline int count_1s<CNT64>(Bitboard b) {
b -= ((b>>1) & 0x5555555555555555ULL);
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL;
b -= ((b>>1) & 0x5555555555555555ULL);
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL;
-inline int count_1s_max_15(Bitboard b) {
+template<>
+inline int count_1s<CNT64_MAX15>(Bitboard b) {
b -= (b>>1) & 0x5555555555555555ULL;
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
b *= 0x1111111111111111ULL;
return int(b >> 60);
}
b -= (b>>1) & 0x5555555555555555ULL;
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
b *= 0x1111111111111111ULL;
return int(b >> 60);
}
-#else // if !defined(IS_64BIT)
-
-inline int count_1s(Bitboard b) {
+template<>
+inline int count_1s<CNT32>(Bitboard b) {
unsigned w = unsigned(b >> 32), v = unsigned(b);
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
w -= (w >> 1) & 0x55555555;
unsigned w = unsigned(b >> 32), v = unsigned(b);
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
w -= (w >> 1) & 0x55555555;
-inline int count_1s_max_15(Bitboard b) {
+template<>
+inline int count_1s<CNT32_MAX15>(Bitboard b) {
unsigned w = unsigned(b >> 32), v = unsigned(b);
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
w -= (w >> 1) & 0x55555555;
unsigned w = unsigned(b >> 32), v = unsigned(b);
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
w -= (w >> 1) & 0x55555555;
-#endif // BITCOUNT
-
-
-/// count_1s() counts the number of nonzero bits in a bitboard.
-/// If template parameter is true an intrinsic is called, otherwise
-/// we fallback on a software implementation.
-
-template<bool UseIntrinsic>
-inline int count_1s(Bitboard b) {
-
- return UseIntrinsic ? POPCNT_INTRINSIC(b) : count_1s(b);
-}
-
-template<bool UseIntrinsic>
-inline int count_1s_max_15(Bitboard b) {
-
- return UseIntrinsic ? POPCNT_INTRINSIC(b) : count_1s_max_15(b);
+template<>
+inline int count_1s<CNT_POPCNT>(Bitboard b) {
+#if !defined(USE_POPCNT)
+ return int(b != 0); // Avoid 'b not used' warning
+#elif defined(_MSC_VER)
+ return __popcnt64(b);
+#elif defined(__GNUC__)
+ unsigned long ret;
+ __asm__("popcnt %1, %0" : "=r" (ret) : "r" (b));
+ return ret;
+#endif
-// Detect hardware POPCNT support
+/// cpu_has_popcnt() detects support for popcnt instruction at runtime
inline bool cpu_has_popcnt() {
int CPUInfo[4] = {-1};
inline bool cpu_has_popcnt() {
int CPUInfo[4] = {-1};
-// Global constant initialized at startup that is set to true if
-// CPU on which application runs supports POPCNT intrinsic. Unless
-// USE_POPCNT is not defined.
+/// CpuHasPOPCNT is a global constant initialized at startup that
+/// is set to true if CPU on which application runs supports popcnt
+/// hardware instruction. Unless USE_POPCNT is not defined.
#if defined(USE_POPCNT)
const bool CpuHasPOPCNT = cpu_has_popcnt();
#else
#if defined(USE_POPCNT)
const bool CpuHasPOPCNT = cpu_has_popcnt();
#else
-// Global constant used to print info about the use of 64 optimized
-// functions to verify that a 64 bit compile has been correctly built.
+/// CpuIs64Bit is a global constant initialized at compile time that
+/// is set to true if CPU on which application runs is a 64 bits.
-const bool CpuHas64BitPath = true;
+const bool CpuIs64Bit = true;
-const bool CpuHas64BitPath = false;
+const bool CpuIs64Bit = false;
#endif
#endif // !defined(BITCOUNT_H_INCLUDED)
#endif
#endif // !defined(BITCOUNT_H_INCLUDED)
result += Value(square_distance(bksq, nsq) * 32);
// Bonus for restricting the knight's mobility
result += Value(square_distance(bksq, nsq) * 32);
// Bonus for restricting the knight's mobility
- result += Value((8 - count_1s_max_15(pos.attacks_from<KNIGHT>(nsq))) * 8);
+ result += Value((8 - count_1s<CNT32_MAX15>(pos.attacks_from<KNIGHT>(nsq))) * 8);
return strongerSide == pos.side_to_move() ? result : -result;
}
return strongerSide == pos.side_to_move() ? result : -result;
}
template<Color Us, bool HasPopCnt>
void init_eval_info(const Position& pos, EvalInfo& ei) {
template<Color Us, bool HasPopCnt>
void init_eval_info(const Position& pos, EvalInfo& ei) {
+ const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? 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));
const Color Them = (Us == WHITE ? BLACK : WHITE);
Bitboard b = ei.attackedBy[Them][KING] = pos.attacks_from<KING>(pos.king_square(Them));
if (ei.updateKingTables[Us])
{
b &= ei.attackedBy[Us][PAWN];
if (ei.updateKingTables[Us])
{
b &= ei.attackedBy[Us][PAWN];
- ei.kingAttackersCount[Us] = b ? count_1s_max_15<HasPopCnt>(b) / 2 : EmptyBoardBB;
+ ei.kingAttackersCount[Us] = b ? count_1s<Max15>(b) / 2 : EmptyBoardBB;
ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = EmptyBoardBB;
}
}
ei.kingAdjacentZoneAttacksCount[Us] = ei.kingAttackersWeight[Us] = EmptyBoardBB;
}
}
File f;
Score bonus = SCORE_ZERO;
File f;
Score bonus = SCORE_ZERO;
+ const BitCountType Full = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64 : CNT32;
+ const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15;
const Color Them = (Us == WHITE ? BLACK : WHITE);
const Square* ptr = pos.piece_list_begin(Us, Piece);
const Color Them = (Us == WHITE ? BLACK : WHITE);
const Square* ptr = pos.piece_list_begin(Us, Piece);
ei.kingAttackersWeight[Us] += KingAttackWeights[Piece];
Bitboard bb = (b & ei.attackedBy[Them][KING]);
if (bb)
ei.kingAttackersWeight[Us] += KingAttackWeights[Piece];
Bitboard bb = (b & ei.attackedBy[Them][KING]);
if (bb)
- ei.kingAdjacentZoneAttacksCount[Us] += count_1s_max_15<HasPopCnt>(bb);
+ ei.kingAdjacentZoneAttacksCount[Us] += count_1s<Max15>(bb);
- mob = (Piece != QUEEN ? count_1s_max_15<HasPopCnt>(b & mobilityArea)
- : count_1s<HasPopCnt>(b & mobilityArea));
+ mob = (Piece != QUEEN ? count_1s<Max15>(b & mobilityArea)
+ : count_1s<Full >(b & mobilityArea));
mobility += MobilityBonus[Piece][mob];
mobility += MobilityBonus[Piece][mob];
template<Color Us, bool HasPopCnt>
Score evaluate_king(const Position& pos, EvalInfo& ei, Value& margin) {
template<Color Us, bool HasPopCnt>
Score evaluate_king(const Position& pos, EvalInfo& ei, Value& margin) {
+ const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15;
const Color Them = (Us == WHITE ? BLACK : WHITE);
Bitboard undefended, b, b1, b2, safe;
const Color Them = (Us == WHITE ? BLACK : WHITE);
Bitboard undefended, b, b1, b2, safe;
// attacked and undefended squares around our king, the square of the
// king, and the quality of the pawn shelter.
attackUnits = Min(25, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2)
// attacked and undefended squares around our king, the square of the
// king, and the quality of the pawn shelter.
attackUnits = Min(25, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2)
- + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + count_1s_max_15<HasPopCnt>(undefended))
+ + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + count_1s<Max15>(undefended))
+ InitKingDanger[relative_square(Us, ksq)]
- mg_value(ei.pi->king_shelter<Us>(pos, ksq)) / 32;
+ InitKingDanger[relative_square(Us, ksq)]
- mg_value(ei.pi->king_shelter<Us>(pos, ksq)) / 32;
| ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][ROOK]);
if (b)
attackUnits += QueenContactCheckBonus
| ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][ROOK]);
if (b)
attackUnits += QueenContactCheckBonus
- * count_1s_max_15<HasPopCnt>(b)
* (Them == pos.side_to_move() ? 2 : 1);
}
* (Them == pos.side_to_move() ? 2 : 1);
}
| ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][QUEEN]);
if (b)
attackUnits += RookContactCheckBonus
| ei.attackedBy[Them][BISHOP] | ei.attackedBy[Them][QUEEN]);
if (b)
attackUnits += RookContactCheckBonus
- * count_1s_max_15<HasPopCnt>(b)
* (Them == pos.side_to_move() ? 2 : 1);
}
* (Them == pos.side_to_move() ? 2 : 1);
}
// Enemy queen safe checks
b = (b1 | b2) & ei.attackedBy[Them][QUEEN];
if (b)
// Enemy queen safe checks
b = (b1 | b2) & ei.attackedBy[Them][QUEEN];
if (b)
- attackUnits += QueenCheckBonus * count_1s_max_15<HasPopCnt>(b);
+ attackUnits += QueenCheckBonus * count_1s<Max15>(b);
// Enemy rooks safe checks
b = b1 & ei.attackedBy[Them][ROOK];
if (b)
// Enemy rooks safe checks
b = b1 & ei.attackedBy[Them][ROOK];
if (b)
- attackUnits += RookCheckBonus * count_1s_max_15<HasPopCnt>(b);
+ attackUnits += RookCheckBonus * count_1s<Max15>(b);
// Enemy bishops safe checks
b = b2 & ei.attackedBy[Them][BISHOP];
if (b)
// Enemy bishops safe checks
b = b2 & ei.attackedBy[Them][BISHOP];
if (b)
- attackUnits += BishopCheckBonus * count_1s_max_15<HasPopCnt>(b);
+ attackUnits += BishopCheckBonus * count_1s<Max15>(b);
// Enemy knights safe checks
b = pos.attacks_from<KNIGHT>(ksq) & ei.attackedBy[Them][KNIGHT] & safe;
if (b)
// Enemy knights safe checks
b = pos.attacks_from<KNIGHT>(ksq) & ei.attackedBy[Them][KNIGHT] & safe;
if (b)
- attackUnits += KnightCheckBonus * count_1s_max_15<HasPopCnt>(b);
+ attackUnits += KnightCheckBonus * count_1s<Max15>(b);
// To index KingDangerTable[] attackUnits must be in [0, 99] range
attackUnits = Min(99, Max(0, attackUnits));
// To index KingDangerTable[] attackUnits must be in [0, 99] range
attackUnits = Min(99, Max(0, attackUnits));
template<Color Us, bool HasPopCnt>
int evaluate_space(const Position& pos, EvalInfo& ei) {
template<Color Us, bool HasPopCnt>
int evaluate_space(const Position& pos, EvalInfo& ei) {
+ const BitCountType Max15 = HasPopCnt ? CNT_POPCNT : CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15;
const Color Them = (Us == WHITE ? BLACK : WHITE);
// Find the safe squares for our pieces inside the area defined by
const Color Them = (Us == WHITE ? BLACK : WHITE);
// Find the safe squares for our pieces inside the area defined by
behind |= (Us == WHITE ? behind >> 8 : behind << 8);
behind |= (Us == WHITE ? behind >> 16 : behind << 16);
behind |= (Us == WHITE ? behind >> 8 : behind << 8);
behind |= (Us == WHITE ? behind >> 16 : behind << 16);
- return count_1s_max_15<HasPopCnt>(safe) + count_1s_max_15<HasPopCnt>(behind & safe);
+ return count_1s<Max15>(safe) + count_1s<Max15>(behind & safe);
const string engine_name() {
const string engine_name() {
- const string cpu64(CpuHas64BitPath ? " 64bit" : "");
+ const string cpu64(CpuIs64Bit ? " 64bit" : "");
if (!EngineVersion.empty())
return AppName + " " + EngineVersion + cpu64;
if (!EngineVersion.empty())
return AppName + " " + EngineVersion + cpu64;
Rank r;
bool passed, isolated, doubled, opposed, chain, backward, candidate;
Score value = SCORE_ZERO;
Rank r;
bool passed, isolated, doubled, opposed, chain, backward, candidate;
Score value = SCORE_ZERO;
+ const BitCountType Max15 = CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15;
const Square* ptr = pos.piece_list_begin(Us, PAWN);
// Initialize halfOpenFiles[]
const Square* ptr = pos.piece_list_begin(Us, PAWN);
// Initialize halfOpenFiles[]
// Test for candidate passed pawn
candidate = !(opposed | passed)
&& (b = attack_span_mask(opposite_color(Us), s + pawn_push(Us)) & ourPawns) != EmptyBoardBB
// Test for candidate passed pawn
candidate = !(opposed | passed)
&& (b = attack_span_mask(opposite_color(Us), s + pawn_push(Us)) & ourPawns) != EmptyBoardBB
- && count_1s_max_15(b) >= count_1s_max_15(attack_span_mask(Us, s) & theirPawns);
+ && count_1s<Max15>(b) >= count_1s<Max15>(attack_span_mask(Us, s) & theirPawns);
// In order to prevent doubled passed pawns from receiving a too big
// bonus, only the frontmost passed pawn on each file is considered as
// In order to prevent doubled passed pawns from receiving a too big
// bonus, only the frontmost passed pawn on each file is considered as
// Is there more than 2 checkers?
if (failedStep) (*failedStep)++;
// Is there more than 2 checkers?
if (failedStep) (*failedStep)++;
- if (debugCheckerCount && count_1s(st->checkersBB) > 2)
+ if (debugCheckerCount && count_1s<CNT32>(st->checkersBB) > 2)
return false;
// Bitboards OK?
return false;
// Bitboards OK?
if (debugPieceCounts)
for (Color c = WHITE; c <= BLACK; c++)
for (PieceType pt = PAWN; pt <= KING; pt++)
if (debugPieceCounts)
for (Color c = WHITE; c <= BLACK; c++)
for (PieceType pt = PAWN; pt <= KING; pt++)
- if (pieceCount[c][pt] != count_1s(pieces(pt, c)))
+ if (pieceCount[c][pt] != count_1s<CNT32>(pieces(pt, c)))
return false;
if (failedStep) (*failedStep)++;
return false;
if (failedStep) (*failedStep)++;