Skip to content

Commit 76024ac

Browse files
committed
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>
1 parent 6c9a641 commit 76024ac

7 files changed

Lines changed: 49 additions & 49 deletions

File tree

src/bitboard.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ namespace {
461461

462462

463463
Bitboard index_to_bitboard(int index, Bitboard mask) {
464-
int i, j, bits = count_1s<false>(mask);
464+
int i, j, bits = count_1s(mask);
465465
Bitboard result = 0ULL;
466466
for(i = 0; i < bits; i++) {
467467
j = pop_1st_bit(&mask);

src/bitcount.h

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ inline bool cpu_has_popcnt() {
8686

8787
inline bool cpu_has_popcnt() { return false; }
8888

89-
#define POPCNT_INTRINSIC(x) sw_count_1s(x)
90-
#define BITSCAN_INTRINSIC(idx, x) sw_count_1s(x) // dummy
89+
#define POPCNT_INTRINSIC(x) count_1s(x)
90+
#define BITSCAN_INTRINSIC(idx, x) count_1s(x) // dummy
9191

9292
#endif
9393

@@ -96,19 +96,19 @@ inline bool cpu_has_popcnt() { return false; }
9696

9797
#if defined(BITCOUNT_LOOP)
9898

99-
inline int sw_count_1s(Bitboard b) {
99+
inline int count_1s(Bitboard b) {
100100
int r;
101101
for(r = 0; b; r++, b &= b - 1);
102102
return r;
103103
}
104104

105-
inline int sw_count_1s_max_15(Bitboard b) {
105+
inline int count_1s_max_15(Bitboard b) {
106106
return count_1s(b);
107107
}
108108

109109
#elif defined(BITCOUNT_SWAR_32)
110110

111-
inline int sw_count_1s(Bitboard b) {
111+
inline int count_1s(Bitboard b) {
112112
unsigned w = unsigned(b >> 32), v = unsigned(b);
113113
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
114114
w -= (w >> 1) & 0x55555555;
@@ -120,7 +120,7 @@ inline int sw_count_1s(Bitboard b) {
120120
return int(v >> 24);
121121
}
122122

123-
inline int sw_count_1s_max_15(Bitboard b) {
123+
inline int count_1s_max_15(Bitboard b) {
124124
unsigned w = unsigned(b >> 32), v = unsigned(b);
125125
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
126126
w -= (w >> 1) & 0x55555555;
@@ -133,15 +133,15 @@ inline int sw_count_1s_max_15(Bitboard b) {
133133

134134
#elif defined(BITCOUNT_SWAR_64)
135135

136-
inline int sw_count_1s(Bitboard b) {
136+
inline int count_1s(Bitboard b) {
137137
b -= ((b>>1) & 0x5555555555555555ULL);
138138
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
139139
b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL;
140140
b *= 0x0101010101010101ULL;
141141
return int(b >> 56);
142142
}
143143

144-
inline int sw_count_1s_max_15(Bitboard b) {
144+
inline int count_1s_max_15(Bitboard b) {
145145
b -= (b>>1) & 0x5555555555555555ULL;
146146
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
147147
b *= 0x1111111111111111ULL;
@@ -158,35 +158,16 @@ inline int sw_count_1s_max_15(Bitboard b) {
158158
template<bool UseIntrinsic>
159159
inline int count_1s(Bitboard b) {
160160

161-
return UseIntrinsic ? POPCNT_INTRINSIC(b) : sw_count_1s(b);
161+
return UseIntrinsic ? POPCNT_INTRINSIC(b) : count_1s(b);
162162
}
163163

164164
template<bool UseIntrinsic>
165165
inline int count_1s_max_15(Bitboard b) {
166166

167-
return UseIntrinsic ? POPCNT_INTRINSIC(b) : sw_count_1s_max_15(b);
167+
return UseIntrinsic ? POPCNT_INTRINSIC(b) : count_1s_max_15(b);
168168
}
169169

170170

171-
// Global variable initialized at startup that is set to true if
172-
// CPU on which application runs supports POPCNT intrinsic. Unless
173-
// DISABLE_POPCNT_SUPPORT is defined.
174-
#if defined(DISABLE_POPCNT_SUPPORT)
175-
const bool CpuHasPOPCNT = false;
176-
#else
177-
const bool CpuHasPOPCNT = cpu_has_popcnt();
178-
#endif
179-
180-
181-
// Global variable used to print info about the use of 64 optimized
182-
// functions to verify that a 64bit compile has been correctly built.
183-
#if defined(BITCOUNT_SWAR_64)
184-
const bool CpuHas64BitPath = true;
185-
#else
186-
const bool CpuHas64BitPath = false;
187-
#endif
188-
189-
190171
/// pop_1st_bit() finds and clears the least significant nonzero bit in a
191172
/// nonzero bitboard. If template parameter is true an intrinsic is called,
192173
/// otherwise we fallback on a software implementation.
@@ -207,4 +188,23 @@ inline Square pop_1st_bit<true>(Bitboard *b) {
207188
return Square(idx);
208189
}
209190

191+
192+
// Global variable initialized at startup that is set to true if
193+
// CPU on which application runs supports POPCNT intrinsic. Unless
194+
// DISABLE_POPCNT_SUPPORT is defined.
195+
#if defined(DISABLE_POPCNT_SUPPORT)
196+
const bool CpuHasPOPCNT = false;
197+
#else
198+
const bool CpuHasPOPCNT = cpu_has_popcnt();
199+
#endif
200+
201+
202+
// Global variable used to print info about the use of 64 optimized
203+
// functions to verify that a 64bit compile has been correctly built.
204+
#if defined(BITCOUNT_SWAR_64)
205+
const bool CpuHas64BitPath = true;
206+
#else
207+
const bool CpuHas64BitPath = false;
208+
#endif
209+
210210
#endif // !defined(BITCOUNT_H_INCLUDED)

src/endgame.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ Value EvaluationFunction<KBBKN>::apply(const Position& pos) {
378378
result += Value(square_distance(bksq, nsq) * 32);
379379

380380
// Bonus for restricting the knight's mobility
381-
result += Value((8 - count_1s_max_15<false>(pos.piece_attacks<KNIGHT>(nsq))) * 8);
381+
result += Value((8 - count_1s_max_15(pos.piece_attacks<KNIGHT>(nsq))) * 8);
382382

383383
return (strongerSide == pos.side_to_move() ? result : -result);
384384
}

src/evaluate.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,8 @@ void init_eval(int threads) {
494494

495495
for (Bitboard b = 0ULL; b < 256ULL; b++)
496496
{
497-
assert(count_1s<false>(b) == int(uint8_t(count_1s<false>(b))));
498-
BitCount8Bit[b] = (uint8_t)count_1s<false>(b);
497+
assert(count_1s(b) == int(uint8_t(count_1s(b))));
498+
BitCount8Bit[b] = (uint8_t)count_1s(b);
499499
}
500500
}
501501

@@ -757,7 +757,7 @@ namespace {
757757
// quality of the pawn shelter.
758758
int attackUnits =
759759
Min((ei.kingAttackersCount[them] * ei.kingAttackersWeight[them]) / 2, 25)
760-
+ (ei.kingAdjacentZoneAttacksCount[them] + count_1s_max_15<false>(undefended)) * 3
760+
+ (ei.kingAdjacentZoneAttacksCount[them] + count_1s_max_15(undefended)) * 3
761761
+ InitKingDanger[relative_square(us, s)] - (shelter >> 5);
762762

763763
// Analyse safe queen contact checks
@@ -773,7 +773,7 @@ namespace {
773773
{
774774
// The bitboard b now contains the squares available for safe queen
775775
// contact checks.
776-
int count = count_1s_max_15<false>(b);
776+
int count = count_1s_max_15(b);
777777
attackUnits += QueenContactCheckBonus * count * (sente ? 2 : 1);
778778

779779
// Is there a mate threat?
@@ -813,12 +813,12 @@ namespace {
813813
// Queen checks
814814
b2 = b & ei.attacked_by(them, QUEEN);
815815
if( b2)
816-
attackUnits += QueenCheckBonus * count_1s_max_15<false>(b2);
816+
attackUnits += QueenCheckBonus * count_1s_max_15(b2);
817817

818818
// Rook checks
819819
b2 = b & ei.attacked_by(them, ROOK);
820820
if (b2)
821-
attackUnits += RookCheckBonus * count_1s_max_15<false>(b2);
821+
attackUnits += RookCheckBonus * count_1s_max_15(b2);
822822
}
823823
if (QueenCheckBonus > 0 || BishopCheckBonus > 0)
824824
{
@@ -827,12 +827,12 @@ namespace {
827827
// Queen checks
828828
b2 = b & ei.attacked_by(them, QUEEN);
829829
if (b2)
830-
attackUnits += QueenCheckBonus * count_1s_max_15<false>(b2);
830+
attackUnits += QueenCheckBonus * count_1s_max_15(b2);
831831

832832
// Bishop checks
833833
b2 = b & ei.attacked_by(them, BISHOP);
834834
if (b2)
835-
attackUnits += BishopCheckBonus * count_1s_max_15<false>(b2);
835+
attackUnits += BishopCheckBonus * count_1s_max_15(b2);
836836
}
837837
if (KnightCheckBonus > 0)
838838
{
@@ -841,7 +841,7 @@ namespace {
841841
// Knight checks
842842
b2 = b & ei.attacked_by(them, KNIGHT);
843843
if (b2)
844-
attackUnits += KnightCheckBonus * count_1s_max_15<false>(b2);
844+
attackUnits += KnightCheckBonus * count_1s_max_15(b2);
845845
}
846846

847847
// Analyse discovered checks (only for non-pawns right now, consider
@@ -850,7 +850,7 @@ namespace {
850850
{
851851
b = p.discovered_check_candidates(them) & ~p.pawns();
852852
if (b)
853-
attackUnits += DiscoveredCheckBonus * count_1s_max_15<false>(b) * (sente? 2 : 1);
853+
attackUnits += DiscoveredCheckBonus * count_1s_max_15(b) * (sente? 2 : 1);
854854
}
855855

856856
// Has a mate threat been found? We don't do anything here if the
@@ -985,7 +985,7 @@ namespace {
985985
if (d < 0)
986986
{
987987
int mtg = RANK_8 - relative_rank(us, s);
988-
int blockerCount = count_1s_max_15<false>(squares_in_front_of(us,s) & pos.occupied_squares());
988+
int blockerCount = count_1s_max_15(squares_in_front_of(us,s) & pos.occupied_squares());
989989
mtg += blockerCount;
990990
d += blockerCount;
991991
if (d < 0)
@@ -1146,8 +1146,8 @@ namespace {
11461146
behindFriendlyPawns |= (behindFriendlyPawns << 16);
11471147
}
11481148

1149-
int space = count_1s_max_15<false>(safeSquares)
1150-
+ count_1s_max_15<false>(behindFriendlyPawns & safeSquares);
1149+
int space = count_1s_max_15(safeSquares)
1150+
+ count_1s_max_15(behindFriendlyPawns & safeSquares);
11511151

11521152
ei.mgValue += Sign[us] * apply_weight(Value(space * ei.mi->space_weight()), WeightSpace);
11531153
}

src/movegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
352352
// The checking pawn cannot be a discovered (bishop) check candidate
353353
// otherwise we were in check also before last double push move.
354354
assert(!bit_is_set(pos.discovered_check_candidates(them), checksq));
355-
assert(count_1s<false>(b1) == 1 || count_1s<false>(b1) == 2);
355+
assert(count_1s(b1) == 1 || count_1s(b1) == 2);
356356

357357
b1 &= ~pinned;
358358
while (b1)

src/pawns.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ PawnInfo *PawnInfoTable::get_pawn_info(const Position &pos) {
330330
// Test for candidate passed pawn
331331
candidate = !passed
332332
&& pos.file_is_half_open(them, f)
333-
&& ( count_1s_max_15<false>(neighboring_files_bb(f) & (behind_bb(us, r) | rank_bb(r)) & ourPawns)
334-
- count_1s_max_15<false>(neighboring_files_bb(f) & in_front_bb(us, r) & theirPawns)
333+
&& ( count_1s_max_15(neighboring_files_bb(f) & (behind_bb(us, r) | rank_bb(r)) & ourPawns)
334+
- count_1s_max_15(neighboring_files_bb(f) & in_front_bb(us, r) & theirPawns)
335335
>= 0);
336336

337337
// In order to prevent doubled passed pawns from receiving a too big

src/position.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,7 @@ bool Position::is_ok(int* failedStep) const {
20912091

20922092
// Is there more than 2 checkers?
20932093
if (failedStep) (*failedStep)++;
2094-
if (debugCheckerCount && count_1s<false>(st->checkersBB) > 2)
2094+
if (debugCheckerCount && count_1s(st->checkersBB) > 2)
20952095
return false;
20962096

20972097
// Bitboards OK?
@@ -2166,7 +2166,7 @@ bool Position::is_ok(int* failedStep) const {
21662166
if (debugPieceCounts)
21672167
for (Color c = WHITE; c <= BLACK; c++)
21682168
for (PieceType pt = PAWN; pt <= KING; pt++)
2169-
if (pieceCount[c][pt] != count_1s<false>(pieces_of_color_and_type(c, pt)))
2169+
if (pieceCount[c][pt] != count_1s(pieces_of_color_and_type(c, pt)))
21702170
return false;
21712171

21722172
if (failedStep) (*failedStep)++;

0 commit comments

Comments
 (0)