1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 00:33:09 +00:00

Use compiler name lookup to simplify code

We don't need different names between a function and a
template. Compiler will know when use one or the other.

This let use restore original count_1s_xx() names instead of
sw_count_1s_xxx so to simplify a bit the code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2009-05-24 10:15:23 +01:00
parent 6c9a64124a
commit 76024ac40e
7 changed files with 49 additions and 49 deletions

View file

@ -461,7 +461,7 @@ namespace {
Bitboard index_to_bitboard(int index, Bitboard mask) { Bitboard index_to_bitboard(int index, Bitboard mask) {
int i, j, bits = count_1s<false>(mask); int i, j, bits = count_1s(mask);
Bitboard result = 0ULL; Bitboard result = 0ULL;
for(i = 0; i < bits; i++) { for(i = 0; i < bits; i++) {
j = pop_1st_bit(&mask); j = pop_1st_bit(&mask);

View file

@ -86,8 +86,8 @@ inline bool cpu_has_popcnt() {
inline bool cpu_has_popcnt() { return false; } inline bool cpu_has_popcnt() { return false; }
#define POPCNT_INTRINSIC(x) sw_count_1s(x) #define POPCNT_INTRINSIC(x) count_1s(x)
#define BITSCAN_INTRINSIC(idx, x) sw_count_1s(x) // dummy #define BITSCAN_INTRINSIC(idx, x) count_1s(x) // dummy
#endif #endif
@ -96,19 +96,19 @@ inline bool cpu_has_popcnt() { return false; }
#if defined(BITCOUNT_LOOP) #if defined(BITCOUNT_LOOP)
inline int sw_count_1s(Bitboard b) { inline int count_1s(Bitboard b) {
int r; int r;
for(r = 0; b; r++, b &= b - 1); for(r = 0; b; r++, b &= b - 1);
return r; return r;
} }
inline int sw_count_1s_max_15(Bitboard b) { inline int count_1s_max_15(Bitboard b) {
return count_1s(b); return count_1s(b);
} }
#elif defined(BITCOUNT_SWAR_32) #elif defined(BITCOUNT_SWAR_32)
inline int sw_count_1s(Bitboard b) { inline int count_1s(Bitboard b) {
unsigned w = unsigned(b >> 32), v = unsigned(b); unsigned w = unsigned(b >> 32), v = unsigned(b);
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
w -= (w >> 1) & 0x55555555; w -= (w >> 1) & 0x55555555;
@ -120,7 +120,7 @@ inline int sw_count_1s(Bitboard b) {
return int(v >> 24); return int(v >> 24);
} }
inline int sw_count_1s_max_15(Bitboard b) { inline int count_1s_max_15(Bitboard b) {
unsigned w = unsigned(b >> 32), v = unsigned(b); unsigned w = unsigned(b >> 32), v = unsigned(b);
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
w -= (w >> 1) & 0x55555555; w -= (w >> 1) & 0x55555555;
@ -133,7 +133,7 @@ inline int sw_count_1s_max_15(Bitboard b) {
#elif defined(BITCOUNT_SWAR_64) #elif defined(BITCOUNT_SWAR_64)
inline int sw_count_1s(Bitboard b) { inline int count_1s(Bitboard b) {
b -= ((b>>1) & 0x5555555555555555ULL); b -= ((b>>1) & 0x5555555555555555ULL);
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL); b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL; b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL;
@ -141,7 +141,7 @@ inline int sw_count_1s(Bitboard b) {
return int(b >> 56); return int(b >> 56);
} }
inline int sw_count_1s_max_15(Bitboard b) { inline int count_1s_max_15(Bitboard b) {
b -= (b>>1) & 0x5555555555555555ULL; b -= (b>>1) & 0x5555555555555555ULL;
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL); b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
b *= 0x1111111111111111ULL; b *= 0x1111111111111111ULL;
@ -158,35 +158,16 @@ inline int sw_count_1s_max_15(Bitboard b) {
template<bool UseIntrinsic> template<bool UseIntrinsic>
inline int count_1s(Bitboard b) { inline int count_1s(Bitboard b) {
return UseIntrinsic ? POPCNT_INTRINSIC(b) : sw_count_1s(b); return UseIntrinsic ? POPCNT_INTRINSIC(b) : count_1s(b);
} }
template<bool UseIntrinsic> template<bool UseIntrinsic>
inline int count_1s_max_15(Bitboard b) { inline int count_1s_max_15(Bitboard b) {
return UseIntrinsic ? POPCNT_INTRINSIC(b) : sw_count_1s_max_15(b); return UseIntrinsic ? POPCNT_INTRINSIC(b) : count_1s_max_15(b);
} }
// Global variable initialized at startup that is set to true if
// CPU on which application runs supports POPCNT intrinsic. Unless
// DISABLE_POPCNT_SUPPORT is defined.
#if defined(DISABLE_POPCNT_SUPPORT)
const bool CpuHasPOPCNT = false;
#else
const bool CpuHasPOPCNT = cpu_has_popcnt();
#endif
// Global variable used to print info about the use of 64 optimized
// functions to verify that a 64bit compile has been correctly built.
#if defined(BITCOUNT_SWAR_64)
const bool CpuHas64BitPath = true;
#else
const bool CpuHas64BitPath = false;
#endif
/// pop_1st_bit() finds and clears the least significant nonzero bit in a /// pop_1st_bit() finds and clears the least significant nonzero bit in a
/// nonzero bitboard. If template parameter is true an intrinsic is called, /// nonzero bitboard. If template parameter is true an intrinsic is called,
/// otherwise we fallback on a software implementation. /// otherwise we fallback on a software implementation.
@ -207,4 +188,23 @@ inline Square pop_1st_bit<true>(Bitboard *b) {
return Square(idx); return Square(idx);
} }
// Global variable initialized at startup that is set to true if
// CPU on which application runs supports POPCNT intrinsic. Unless
// DISABLE_POPCNT_SUPPORT is defined.
#if defined(DISABLE_POPCNT_SUPPORT)
const bool CpuHasPOPCNT = false;
#else
const bool CpuHasPOPCNT = cpu_has_popcnt();
#endif
// Global variable used to print info about the use of 64 optimized
// functions to verify that a 64bit compile has been correctly built.
#if defined(BITCOUNT_SWAR_64)
const bool CpuHas64BitPath = true;
#else
const bool CpuHas64BitPath = false;
#endif
#endif // !defined(BITCOUNT_H_INCLUDED) #endif // !defined(BITCOUNT_H_INCLUDED)

View file

@ -378,7 +378,7 @@ Value EvaluationFunction<KBBKN>::apply(const Position& pos) {
result += Value(square_distance(bksq, nsq) * 32); result += Value(square_distance(bksq, nsq) * 32);
// Bonus for restricting the knight's mobility // Bonus for restricting the knight's mobility
result += Value((8 - count_1s_max_15<false>(pos.piece_attacks<KNIGHT>(nsq))) * 8); result += Value((8 - count_1s_max_15(pos.piece_attacks<KNIGHT>(nsq))) * 8);
return (strongerSide == pos.side_to_move() ? result : -result); return (strongerSide == pos.side_to_move() ? result : -result);
} }

View file

@ -494,8 +494,8 @@ void init_eval(int threads) {
for (Bitboard b = 0ULL; b < 256ULL; b++) for (Bitboard b = 0ULL; b < 256ULL; b++)
{ {
assert(count_1s<false>(b) == int(uint8_t(count_1s<false>(b)))); assert(count_1s(b) == int(uint8_t(count_1s(b))));
BitCount8Bit[b] = (uint8_t)count_1s<false>(b); BitCount8Bit[b] = (uint8_t)count_1s(b);
} }
} }
@ -757,7 +757,7 @@ namespace {
// quality of the pawn shelter. // quality of the pawn shelter.
int attackUnits = int attackUnits =
Min((ei.kingAttackersCount[them] * ei.kingAttackersWeight[them]) / 2, 25) Min((ei.kingAttackersCount[them] * ei.kingAttackersWeight[them]) / 2, 25)
+ (ei.kingAdjacentZoneAttacksCount[them] + count_1s_max_15<false>(undefended)) * 3 + (ei.kingAdjacentZoneAttacksCount[them] + count_1s_max_15(undefended)) * 3
+ InitKingDanger[relative_square(us, s)] - (shelter >> 5); + InitKingDanger[relative_square(us, s)] - (shelter >> 5);
// Analyse safe queen contact checks // Analyse safe queen contact checks
@ -773,7 +773,7 @@ namespace {
{ {
// The bitboard b now contains the squares available for safe queen // The bitboard b now contains the squares available for safe queen
// contact checks. // contact checks.
int count = count_1s_max_15<false>(b); int count = count_1s_max_15(b);
attackUnits += QueenContactCheckBonus * count * (sente ? 2 : 1); attackUnits += QueenContactCheckBonus * count * (sente ? 2 : 1);
// Is there a mate threat? // Is there a mate threat?
@ -813,12 +813,12 @@ namespace {
// Queen checks // Queen checks
b2 = b & ei.attacked_by(them, QUEEN); b2 = b & ei.attacked_by(them, QUEEN);
if( b2) if( b2)
attackUnits += QueenCheckBonus * count_1s_max_15<false>(b2); attackUnits += QueenCheckBonus * count_1s_max_15(b2);
// Rook checks // Rook checks
b2 = b & ei.attacked_by(them, ROOK); b2 = b & ei.attacked_by(them, ROOK);
if (b2) if (b2)
attackUnits += RookCheckBonus * count_1s_max_15<false>(b2); attackUnits += RookCheckBonus * count_1s_max_15(b2);
} }
if (QueenCheckBonus > 0 || BishopCheckBonus > 0) if (QueenCheckBonus > 0 || BishopCheckBonus > 0)
{ {
@ -827,12 +827,12 @@ namespace {
// Queen checks // Queen checks
b2 = b & ei.attacked_by(them, QUEEN); b2 = b & ei.attacked_by(them, QUEEN);
if (b2) if (b2)
attackUnits += QueenCheckBonus * count_1s_max_15<false>(b2); attackUnits += QueenCheckBonus * count_1s_max_15(b2);
// Bishop checks // Bishop checks
b2 = b & ei.attacked_by(them, BISHOP); b2 = b & ei.attacked_by(them, BISHOP);
if (b2) if (b2)
attackUnits += BishopCheckBonus * count_1s_max_15<false>(b2); attackUnits += BishopCheckBonus * count_1s_max_15(b2);
} }
if (KnightCheckBonus > 0) if (KnightCheckBonus > 0)
{ {
@ -841,7 +841,7 @@ namespace {
// Knight checks // Knight checks
b2 = b & ei.attacked_by(them, KNIGHT); b2 = b & ei.attacked_by(them, KNIGHT);
if (b2) if (b2)
attackUnits += KnightCheckBonus * count_1s_max_15<false>(b2); attackUnits += KnightCheckBonus * count_1s_max_15(b2);
} }
// Analyse discovered checks (only for non-pawns right now, consider // Analyse discovered checks (only for non-pawns right now, consider
@ -850,7 +850,7 @@ namespace {
{ {
b = p.discovered_check_candidates(them) & ~p.pawns(); b = p.discovered_check_candidates(them) & ~p.pawns();
if (b) if (b)
attackUnits += DiscoveredCheckBonus * count_1s_max_15<false>(b) * (sente? 2 : 1); attackUnits += DiscoveredCheckBonus * count_1s_max_15(b) * (sente? 2 : 1);
} }
// Has a mate threat been found? We don't do anything here if the // Has a mate threat been found? We don't do anything here if the
@ -985,7 +985,7 @@ namespace {
if (d < 0) if (d < 0)
{ {
int mtg = RANK_8 - relative_rank(us, s); int mtg = RANK_8 - relative_rank(us, s);
int blockerCount = count_1s_max_15<false>(squares_in_front_of(us,s) & pos.occupied_squares()); int blockerCount = count_1s_max_15(squares_in_front_of(us,s) & pos.occupied_squares());
mtg += blockerCount; mtg += blockerCount;
d += blockerCount; d += blockerCount;
if (d < 0) if (d < 0)
@ -1146,8 +1146,8 @@ namespace {
behindFriendlyPawns |= (behindFriendlyPawns << 16); behindFriendlyPawns |= (behindFriendlyPawns << 16);
} }
int space = count_1s_max_15<false>(safeSquares) int space = count_1s_max_15(safeSquares)
+ count_1s_max_15<false>(behindFriendlyPawns & safeSquares); + count_1s_max_15(behindFriendlyPawns & safeSquares);
ei.mgValue += Sign[us] * apply_weight(Value(space * ei.mi->space_weight()), WeightSpace); ei.mgValue += Sign[us] * apply_weight(Value(space * ei.mi->space_weight()), WeightSpace);
} }

View file

@ -352,7 +352,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
// The checking pawn cannot be a discovered (bishop) check candidate // The checking pawn cannot be a discovered (bishop) check candidate
// otherwise we were in check also before last double push move. // otherwise we were in check also before last double push move.
assert(!bit_is_set(pos.discovered_check_candidates(them), checksq)); assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
assert(count_1s<false>(b1) == 1 || count_1s<false>(b1) == 2); assert(count_1s(b1) == 1 || count_1s(b1) == 2);
b1 &= ~pinned; b1 &= ~pinned;
while (b1) while (b1)

View file

@ -330,8 +330,8 @@ PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) {
// Test for candidate passed pawn // Test for candidate passed pawn
candidate = !passed candidate = !passed
&& pos.file_is_half_open(them, f) && pos.file_is_half_open(them, f)
&& ( count_1s_max_15<false>(neighboring_files_bb(f) & (behind_bb(us, r) | rank_bb(r)) & ourPawns) && ( count_1s_max_15(neighboring_files_bb(f) & (behind_bb(us, r) | rank_bb(r)) & ourPawns)
- count_1s_max_15<false>(neighboring_files_bb(f) & in_front_bb(us, r) & theirPawns) - count_1s_max_15(neighboring_files_bb(f) & in_front_bb(us, r) & theirPawns)
>= 0); >= 0);
// In order to prevent doubled passed pawns from receiving a too big // In order to prevent doubled passed pawns from receiving a too big

View file

@ -2091,7 +2091,7 @@ bool Position::is_ok(int* failedStep) const {
// Is there more than 2 checkers? // Is there more than 2 checkers?
if (failedStep) (*failedStep)++; if (failedStep) (*failedStep)++;
if (debugCheckerCount && count_1s<false>(st->checkersBB) > 2) if (debugCheckerCount && count_1s(st->checkersBB) > 2)
return false; return false;
// Bitboards OK? // Bitboards OK?
@ -2166,7 +2166,7 @@ bool Position::is_ok(int* failedStep) const {
if (debugPieceCounts) if (debugPieceCounts)
for (Color c = WHITE; c <= BLACK; c++) for (Color c = WHITE; c <= BLACK; c++)
for (PieceType pt = PAWN; pt <= KING; pt++) for (PieceType pt = PAWN; pt <= KING; pt++)
if (pieceCount[c][pt] != count_1s<false>(pieces_of_color_and_type(c, pt))) if (pieceCount[c][pt] != count_1s(pieces_of_color_and_type(c, pt)))
return false; return false;
if (failedStep) (*failedStep)++; if (failedStep) (*failedStep)++;