From: Marco Costalba Date: Sun, 24 May 2009 09:15:23 +0000 (+0100) Subject: Use compiler name lookup to simplify code X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=76024ac40ea45e44a8389cb4206d40b884e259a5 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 --- diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 3dfa040b..a73c5a2d 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -461,7 +461,7 @@ namespace { Bitboard index_to_bitboard(int index, Bitboard mask) { - int i, j, bits = count_1s(mask); + int i, j, bits = count_1s(mask); Bitboard result = 0ULL; for(i = 0; i < bits; i++) { j = pop_1st_bit(&mask); diff --git a/src/bitcount.h b/src/bitcount.h index 41a1446e..871247ab 100644 --- a/src/bitcount.h +++ b/src/bitcount.h @@ -86,8 +86,8 @@ inline bool cpu_has_popcnt() { inline bool cpu_has_popcnt() { return false; } -#define POPCNT_INTRINSIC(x) sw_count_1s(x) -#define BITSCAN_INTRINSIC(idx, x) sw_count_1s(x) // dummy +#define POPCNT_INTRINSIC(x) count_1s(x) +#define BITSCAN_INTRINSIC(idx, x) count_1s(x) // dummy #endif @@ -96,19 +96,19 @@ inline bool cpu_has_popcnt() { return false; } #if defined(BITCOUNT_LOOP) -inline int sw_count_1s(Bitboard b) { +inline int count_1s(Bitboard b) { int r; for(r = 0; b; r++, b &= b - 1); return r; } -inline int sw_count_1s_max_15(Bitboard b) { +inline int count_1s_max_15(Bitboard b) { return count_1s(b); } #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); v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits w -= (w >> 1) & 0x55555555; @@ -120,7 +120,7 @@ inline int sw_count_1s(Bitboard b) { 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); v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits w -= (w >> 1) & 0x55555555; @@ -133,7 +133,7 @@ inline int sw_count_1s_max_15(Bitboard b) { #elif defined(BITCOUNT_SWAR_64) -inline int sw_count_1s(Bitboard b) { +inline int count_1s(Bitboard b) { b -= ((b>>1) & 0x5555555555555555ULL); b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL); b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL; @@ -141,7 +141,7 @@ inline int sw_count_1s(Bitboard b) { 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>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL); b *= 0x1111111111111111ULL; @@ -158,35 +158,16 @@ inline int sw_count_1s_max_15(Bitboard b) { template inline int count_1s(Bitboard b) { - return UseIntrinsic ? POPCNT_INTRINSIC(b) : sw_count_1s(b); + return UseIntrinsic ? POPCNT_INTRINSIC(b) : count_1s(b); } template 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 /// nonzero bitboard. If template parameter is true an intrinsic is called, /// otherwise we fallback on a software implementation. @@ -207,4 +188,23 @@ inline Square pop_1st_bit(Bitboard *b) { 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) diff --git a/src/endgame.cpp b/src/endgame.cpp index 90898e83..3258c6fc 100644 --- a/src/endgame.cpp +++ b/src/endgame.cpp @@ -378,7 +378,7 @@ Value EvaluationFunction::apply(const Position& pos) { result += Value(square_distance(bksq, nsq) * 32); // Bonus for restricting the knight's mobility - result += Value((8 - count_1s_max_15(pos.piece_attacks(nsq))) * 8); + result += Value((8 - count_1s_max_15(pos.piece_attacks(nsq))) * 8); return (strongerSide == pos.side_to_move() ? result : -result); } diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 1d4be67b..9fd4040e 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -494,8 +494,8 @@ void init_eval(int threads) { for (Bitboard b = 0ULL; b < 256ULL; b++) { - assert(count_1s(b) == int(uint8_t(count_1s(b)))); - BitCount8Bit[b] = (uint8_t)count_1s(b); + assert(count_1s(b) == int(uint8_t(count_1s(b)))); + BitCount8Bit[b] = (uint8_t)count_1s(b); } } @@ -757,7 +757,7 @@ namespace { // quality of the pawn shelter. int attackUnits = Min((ei.kingAttackersCount[them] * ei.kingAttackersWeight[them]) / 2, 25) - + (ei.kingAdjacentZoneAttacksCount[them] + count_1s_max_15(undefended)) * 3 + + (ei.kingAdjacentZoneAttacksCount[them] + count_1s_max_15(undefended)) * 3 + InitKingDanger[relative_square(us, s)] - (shelter >> 5); // Analyse safe queen contact checks @@ -773,7 +773,7 @@ namespace { { // The bitboard b now contains the squares available for safe queen // contact checks. - int count = count_1s_max_15(b); + int count = count_1s_max_15(b); attackUnits += QueenContactCheckBonus * count * (sente ? 2 : 1); // Is there a mate threat? @@ -813,12 +813,12 @@ namespace { // Queen checks b2 = b & ei.attacked_by(them, QUEEN); if( b2) - attackUnits += QueenCheckBonus * count_1s_max_15(b2); + attackUnits += QueenCheckBonus * count_1s_max_15(b2); // Rook checks b2 = b & ei.attacked_by(them, ROOK); if (b2) - attackUnits += RookCheckBonus * count_1s_max_15(b2); + attackUnits += RookCheckBonus * count_1s_max_15(b2); } if (QueenCheckBonus > 0 || BishopCheckBonus > 0) { @@ -827,12 +827,12 @@ namespace { // Queen checks b2 = b & ei.attacked_by(them, QUEEN); if (b2) - attackUnits += QueenCheckBonus * count_1s_max_15(b2); + attackUnits += QueenCheckBonus * count_1s_max_15(b2); // Bishop checks b2 = b & ei.attacked_by(them, BISHOP); if (b2) - attackUnits += BishopCheckBonus * count_1s_max_15(b2); + attackUnits += BishopCheckBonus * count_1s_max_15(b2); } if (KnightCheckBonus > 0) { @@ -841,7 +841,7 @@ namespace { // Knight checks b2 = b & ei.attacked_by(them, KNIGHT); if (b2) - attackUnits += KnightCheckBonus * count_1s_max_15(b2); + attackUnits += KnightCheckBonus * count_1s_max_15(b2); } // Analyse discovered checks (only for non-pawns right now, consider @@ -850,7 +850,7 @@ namespace { { b = p.discovered_check_candidates(them) & ~p.pawns(); if (b) - attackUnits += DiscoveredCheckBonus * count_1s_max_15(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 @@ -985,7 +985,7 @@ namespace { if (d < 0) { int mtg = RANK_8 - relative_rank(us, s); - int blockerCount = count_1s_max_15(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; d += blockerCount; if (d < 0) @@ -1146,8 +1146,8 @@ namespace { behindFriendlyPawns |= (behindFriendlyPawns << 16); } - int space = count_1s_max_15(safeSquares) - + count_1s_max_15(behindFriendlyPawns & safeSquares); + int space = count_1s_max_15(safeSquares) + + count_1s_max_15(behindFriendlyPawns & safeSquares); ei.mgValue += Sign[us] * apply_weight(Value(space * ei.mi->space_weight()), WeightSpace); } diff --git a/src/movegen.cpp b/src/movegen.cpp index dbdb0875..a76d9f7c 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -352,7 +352,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { // The checking pawn cannot be a discovered (bishop) check candidate // otherwise we were in check also before last double push move. assert(!bit_is_set(pos.discovered_check_candidates(them), checksq)); - assert(count_1s(b1) == 1 || count_1s(b1) == 2); + assert(count_1s(b1) == 1 || count_1s(b1) == 2); b1 &= ~pinned; while (b1) diff --git a/src/pawns.cpp b/src/pawns.cpp index 08b0e7ba..19bf6c51 100644 --- a/src/pawns.cpp +++ b/src/pawns.cpp @@ -330,8 +330,8 @@ PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) { // Test for candidate passed pawn candidate = !passed && pos.file_is_half_open(them, f) - && ( count_1s_max_15(neighboring_files_bb(f) & (behind_bb(us, r) | rank_bb(r)) & ourPawns) - - count_1s_max_15(neighboring_files_bb(f) & in_front_bb(us, r) & theirPawns) + && ( count_1s_max_15(neighboring_files_bb(f) & (behind_bb(us, r) | rank_bb(r)) & ourPawns) + - count_1s_max_15(neighboring_files_bb(f) & in_front_bb(us, r) & theirPawns) >= 0); // In order to prevent doubled passed pawns from receiving a too big diff --git a/src/position.cpp b/src/position.cpp index 6e6bf38b..dd6ec05b 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -2091,7 +2091,7 @@ bool Position::is_ok(int* failedStep) const { // Is there more than 2 checkers? if (failedStep) (*failedStep)++; - if (debugCheckerCount && count_1s(st->checkersBB) > 2) + if (debugCheckerCount && count_1s(st->checkersBB) > 2) return false; // Bitboards OK? @@ -2166,7 +2166,7 @@ bool Position::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(pieces_of_color_and_type(c, pt))) + if (pieceCount[c][pt] != count_1s(pieces_of_color_and_type(c, pt))) return false; if (failedStep) (*failedStep)++;