Skip to content

Commit 7a1ff6d

Browse files
committed
Fix operator++ definition
ENABLE_OPERATORS_ON has incorrect definitions of post-increment and post-decrement operators. In particularly the returned value is the variable already incremented/decremented, while instead they should return the variable _before_ inc/dec. This has no real effect because are only used in loops and where the returned value is never used, neverthless it is wrong. The fix would be to copy the variable to a dummy, then inc/dec the variable, then return the dummy. So instead, rename to pre-increment that can be implemented without the dummy, actually the current implementation it is already the correct pre-increment, with the only change to return a reference (an l-value) and not a copy, so to properly mimic the pre-increment on native integers. Spotted by Kojirion. No functional change.
1 parent 82f6779 commit 7a1ff6d

File tree

4 files changed

+49
-49
lines changed

4 files changed

+49
-49
lines changed

src/bitboard.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ void Bitboards::print(Bitboard b) {
132132

133133
sync_cout;
134134

135-
for (Rank rank = RANK_8; rank >= RANK_1; rank--)
135+
for (Rank rank = RANK_8; rank >= RANK_1; --rank)
136136
{
137137
std::cout << "+---+---+---+---+---+---+---+---+" << '\n';
138138

139-
for (File file = FILE_A; file <= FILE_H; file++)
139+
for (File file = FILE_A; file <= FILE_H; ++file)
140140
std::cout << "| " << (b & (file | rank) ? "X " : " ");
141141

142142
std::cout << "|\n";
@@ -157,7 +157,7 @@ void Bitboards::init() {
157157
for (int i = 0; i < 64; i++)
158158
BSFTable[bsf_index(1ULL << i)] = Square(i);
159159

160-
for (Square s = SQ_A1; s <= SQ_H8; s++)
160+
for (Square s = SQ_A1; s <= SQ_H8; ++s)
161161
SquareBB[s] = 1ULL << s;
162162

163163
FileBB[FILE_A] = FileABB;
@@ -169,22 +169,22 @@ void Bitboards::init() {
169169
RankBB[i] = RankBB[i - 1] << 8;
170170
}
171171

172-
for (File f = FILE_A; f <= FILE_H; f++)
172+
for (File f = FILE_A; f <= FILE_H; ++f)
173173
AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0);
174174

175-
for (Rank r = RANK_1; r < RANK_8; r++)
175+
for (Rank r = RANK_1; r < RANK_8; ++r)
176176
InFrontBB[WHITE][r] = ~(InFrontBB[BLACK][r + 1] = InFrontBB[BLACK][r] | RankBB[r]);
177177

178-
for (Color c = WHITE; c <= BLACK; c++)
179-
for (Square s = SQ_A1; s <= SQ_H8; s++)
178+
for (Color c = WHITE; c <= BLACK; ++c)
179+
for (Square s = SQ_A1; s <= SQ_H8; ++s)
180180
{
181181
ForwardBB[c][s] = InFrontBB[c][rank_of(s)] & FileBB[file_of(s)];
182182
PawnAttackSpan[c][s] = InFrontBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
183183
PassedPawnMask[c][s] = ForwardBB[c][s] | PawnAttackSpan[c][s];
184184
}
185185

186-
for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
187-
for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++)
186+
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
187+
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
188188
{
189189
SquareDistance[s1][s2] = std::max(file_distance(s1, s2), rank_distance(s1, s2));
190190
if (s1 != s2)
@@ -194,9 +194,9 @@ void Bitboards::init() {
194194
int steps[][9] = { {}, { 7, 9 }, { 17, 15, 10, 6, -6, -10, -15, -17 },
195195
{}, {}, {}, { 9, 7, -7, -9, 8, 1, -1, -8 } };
196196

197-
for (Color c = WHITE; c <= BLACK; c++)
198-
for (PieceType pt = PAWN; pt <= KING; pt++)
199-
for (Square s = SQ_A1; s <= SQ_H8; s++)
197+
for (Color c = WHITE; c <= BLACK; ++c)
198+
for (PieceType pt = PAWN; pt <= KING; ++pt)
199+
for (Square s = SQ_A1; s <= SQ_H8; ++s)
200200
for (int k = 0; steps[pt][k]; k++)
201201
{
202202
Square to = s + Square(c == WHITE ? steps[pt][k] : -steps[pt][k]);
@@ -211,14 +211,14 @@ void Bitboards::init() {
211211
init_magics(RTable, RAttacks, RMagics, RMasks, RShifts, RDeltas, magic_index<ROOK>);
212212
init_magics(BTable, BAttacks, BMagics, BMasks, BShifts, BDeltas, magic_index<BISHOP>);
213213

214-
for (Square s = SQ_A1; s <= SQ_H8; s++)
214+
for (Square s = SQ_A1; s <= SQ_H8; ++s)
215215
{
216216
PseudoAttacks[QUEEN][s] = PseudoAttacks[BISHOP][s] = attacks_bb<BISHOP>(s, 0);
217217
PseudoAttacks[QUEEN][s] |= PseudoAttacks[ ROOK][s] = attacks_bb< ROOK>(s, 0);
218218
}
219219

220-
for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
221-
for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++)
220+
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
221+
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
222222
if (PseudoAttacks[QUEEN][s1] & s2)
223223
{
224224
Square delta = (s2 - s1) / square_distance(s1, s2);
@@ -281,7 +281,7 @@ namespace {
281281
// attacks[s] is a pointer to the beginning of the attacks table for square 's'
282282
attacks[SQ_A1] = table;
283283

284-
for (Square s = SQ_A1; s <= SQ_H8; s++)
284+
for (Square s = SQ_A1; s <= SQ_H8; ++s)
285285
{
286286
// Board edges are not considered in the relevant occupancies
287287
edges = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));

src/evaluate.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,11 +622,11 @@ Value do_evaluate(const Position& pos, Value& margin) {
622622
// type of attacking piece, from knights to queens. Kings are not
623623
// considered because are already handled in king evaluation.
624624
if (weakEnemies)
625-
for (PieceType pt1 = KNIGHT; pt1 < KING; pt1++)
625+
for (PieceType pt1 = KNIGHT; pt1 < KING; ++pt1)
626626
{
627627
b = ei.attackedBy[Us][pt1] & weakEnemies;
628628
if (b)
629-
for (PieceType pt2 = PAWN; pt2 < KING; pt2++)
629+
for (PieceType pt2 = PAWN; pt2 < KING; ++pt2)
630630
if (b & pos.pieces(pt2))
631631
score += Threat[pt1][pt2];
632632
}
@@ -908,7 +908,7 @@ Value do_evaluate(const Position& pos, Value& margin) {
908908

909909
// Step 1. Hunt for unstoppable passed pawns. If we find at least one,
910910
// record how many plies are required for promotion.
911-
for (c = WHITE; c <= BLACK; c++)
911+
for (c = WHITE; c <= BLACK; ++c)
912912
{
913913
// Skip if other side has non-pawn pieces
914914
if (pos.non_pawn_material(~c))

src/position.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ void Position::init() {
120120

121121
RKISS rk;
122122

123-
for (Color c = WHITE; c <= BLACK; c++)
124-
for (PieceType pt = PAWN; pt <= KING; pt++)
125-
for (Square s = SQ_A1; s <= SQ_H8; s++)
123+
for (Color c = WHITE; c <= BLACK; ++c)
124+
for (PieceType pt = PAWN; pt <= KING; ++pt)
125+
for (Square s = SQ_A1; s <= SQ_H8; ++s)
126126
Zobrist::psq[c][pt][s] = rk.rand<Key>();
127127

128-
for (File f = FILE_A; f <= FILE_H; f++)
128+
for (File f = FILE_A; f <= FILE_H; ++f)
129129
Zobrist::enpassant[f] = rk.rand<Key>();
130130

131131
for (int cr = CASTLES_NONE; cr <= ALL_CASTLES; cr++)
@@ -141,14 +141,14 @@ void Position::init() {
141141
Zobrist::side = rk.rand<Key>();
142142
Zobrist::exclusion = rk.rand<Key>();
143143

144-
for (PieceType pt = PAWN; pt <= KING; pt++)
144+
for (PieceType pt = PAWN; pt <= KING; ++pt)
145145
{
146146
PieceValue[MG][make_piece(BLACK, pt)] = PieceValue[MG][pt];
147147
PieceValue[EG][make_piece(BLACK, pt)] = PieceValue[EG][pt];
148148

149149
Score v = make_score(PieceValue[MG][pt], PieceValue[EG][pt]);
150150

151-
for (Square s = SQ_A1; s <= SQ_H8; s++)
151+
for (Square s = SQ_A1; s <= SQ_H8; ++s)
152152
{
153153
psq[WHITE][pt][ s] = (v + PSQT[pt][s]);
154154
psq[BLACK][pt][~s] = -(v + PSQT[pt][s]);
@@ -233,7 +233,7 @@ void Position::set(const string& fenStr, bool isChess960, Thread* th) {
233233
else if ((p = PieceToChar.find(token)) != string::npos)
234234
{
235235
put_piece(sq, color_of(Piece(p)), type_of(Piece(p)));
236-
sq++;
236+
++sq;
237237
}
238238
}
239239

@@ -255,10 +255,10 @@ void Position::set(const string& fenStr, bool isChess960, Thread* th) {
255255
token = char(toupper(token));
256256

257257
if (token == 'K')
258-
for (rsq = relative_square(c, SQ_H1); type_of(piece_on(rsq)) != ROOK; rsq--) {}
258+
for (rsq = relative_square(c, SQ_H1); type_of(piece_on(rsq)) != ROOK; --rsq) {}
259259

260260
else if (token == 'Q')
261-
for (rsq = relative_square(c, SQ_A1); type_of(piece_on(rsq)) != ROOK; rsq++) {}
261+
for (rsq = relative_square(c, SQ_A1); type_of(piece_on(rsq)) != ROOK; ++rsq) {}
262262

263263
else if (token >= 'A' && token <= 'H')
264264
rsq = File(token - 'A') | relative_rank(c, RANK_1);
@@ -317,11 +317,11 @@ void Position::set_castle_right(Color c, Square rfrom) {
317317
Square kto = relative_square(c, cs == KING_SIDE ? SQ_G1 : SQ_C1);
318318
Square rto = relative_square(c, cs == KING_SIDE ? SQ_F1 : SQ_D1);
319319

320-
for (Square s = std::min(rfrom, rto); s <= std::max(rfrom, rto); s++)
320+
for (Square s = std::min(rfrom, rto); s <= std::max(rfrom, rto); ++s)
321321
if (s != kfrom && s != rfrom)
322322
castlePath[c][cs] |= s;
323323

324-
for (Square s = std::min(kfrom, kto); s <= std::max(kfrom, kto); s++)
324+
for (Square s = std::min(kfrom, kto); s <= std::max(kfrom, kto); ++s)
325325
if (s != kfrom && s != rfrom)
326326
castlePath[c][cs] |= s;
327327
}
@@ -334,17 +334,17 @@ const string Position::fen() const {
334334

335335
std::ostringstream ss;
336336

337-
for (Rank rank = RANK_8; rank >= RANK_1; rank--)
337+
for (Rank rank = RANK_8; rank >= RANK_1; --rank)
338338
{
339-
for (File file = FILE_A; file <= FILE_H; file++)
339+
for (File file = FILE_A; file <= FILE_H; ++file)
340340
{
341341
Square sq = file | rank;
342342

343343
if (is_empty(sq))
344344
{
345345
int emptyCnt = 1;
346346

347-
for ( ; file < FILE_H && is_empty(sq++); file++)
347+
for ( ; file < FILE_H && is_empty(++sq); ++file)
348348
emptyCnt++;
349349

350350
ss << emptyCnt;
@@ -1210,9 +1210,9 @@ Key Position::compute_material_key() const {
12101210

12111211
Key k = 0;
12121212

1213-
for (Color c = WHITE; c <= BLACK; c++)
1214-
for (PieceType pt = PAWN; pt <= QUEEN; pt++)
1215-
for (int cnt = 0; cnt < pieceCount[c][pt]; cnt++)
1213+
for (Color c = WHITE; c <= BLACK; ++c)
1214+
for (PieceType pt = PAWN; pt <= QUEEN; ++pt)
1215+
for (int cnt = 0; cnt < pieceCount[c][pt]; ++cnt)
12161216
k ^= Zobrist::psq[c][pt][cnt];
12171217

12181218
return k;
@@ -1248,7 +1248,7 @@ Value Position::compute_non_pawn_material(Color c) const {
12481248

12491249
Value value = VALUE_ZERO;
12501250

1251-
for (PieceType pt = KNIGHT; pt <= QUEEN; pt++)
1251+
for (PieceType pt = KNIGHT; pt <= QUEEN; ++pt)
12521252
value += pieceCount[c][pt] * PieceValue[MG][pt];
12531253

12541254
return value;
@@ -1302,7 +1302,7 @@ void Position::flip() {
13021302
string f, token;
13031303
std::stringstream ss(fen());
13041304

1305-
for (Rank rank = RANK_8; rank >= RANK_1; rank--) // Piece placement
1305+
for (Rank rank = RANK_8; rank >= RANK_1; --rank) // Piece placement
13061306
{
13071307
std::getline(ss, token, rank > RANK_1 ? '/' : ' ');
13081308
f.insert(0, token + (f.empty() ? " " : "/"));
@@ -1366,7 +1366,7 @@ bool Position::pos_is_ok(int* failedStep) const {
13661366
{
13671367
int kingCount[COLOR_NB] = {};
13681368

1369-
for (Square s = SQ_A1; s <= SQ_H8; s++)
1369+
for (Square s = SQ_A1; s <= SQ_H8; ++s)
13701370
if (type_of(piece_on(s)) == KING)
13711371
kingCount[color_of(piece_on(s))]++;
13721372

@@ -1393,8 +1393,8 @@ bool Position::pos_is_ok(int* failedStep) const {
13931393
return false;
13941394

13951395
// Separate piece type bitboards must have empty intersections
1396-
for (PieceType p1 = PAWN; p1 <= KING; p1++)
1397-
for (PieceType p2 = PAWN; p2 <= KING; p2++)
1396+
for (PieceType p1 = PAWN; p1 <= KING; ++p1)
1397+
for (PieceType p2 = PAWN; p2 <= KING; ++p2)
13981398
if (p1 != p2 && (pieces(p1) & pieces(p2)))
13991399
return false;
14001400
}
@@ -1420,21 +1420,21 @@ bool Position::pos_is_ok(int* failedStep) const {
14201420
return false;
14211421

14221422
if ((*step)++, debugPieceCounts)
1423-
for (Color c = WHITE; c <= BLACK; c++)
1424-
for (PieceType pt = PAWN; pt <= KING; pt++)
1423+
for (Color c = WHITE; c <= BLACK; ++c)
1424+
for (PieceType pt = PAWN; pt <= KING; ++pt)
14251425
if (pieceCount[c][pt] != popcount<Full>(pieces(c, pt)))
14261426
return false;
14271427

14281428
if ((*step)++, debugPieceList)
1429-
for (Color c = WHITE; c <= BLACK; c++)
1430-
for (PieceType pt = PAWN; pt <= KING; pt++)
1429+
for (Color c = WHITE; c <= BLACK; ++c)
1430+
for (PieceType pt = PAWN; pt <= KING; ++pt)
14311431
for (int i = 0; i < pieceCount[c][pt]; i++)
14321432
if ( board[pieceList[c][pt][i]] != make_piece(c, pt)
14331433
|| index[pieceList[c][pt][i]] != i)
14341434
return false;
14351435

14361436
if ((*step)++, debugCastleSquares)
1437-
for (Color c = WHITE; c <= BLACK; c++)
1437+
for (Color c = WHITE; c <= BLACK; ++c)
14381438
for (CastlingSide s = KING_SIDE; s <= QUEEN_SIDE; s = CastlingSide(s + 1))
14391439
{
14401440
CastleRight cr = make_castle_right(c, s);

src/types.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@ inline T& operator-=(T& d1, const T d2) { d1 = d1 - d2; return d1; } \
279279
inline T& operator*=(T& d, int i) { d = T(int(d) * i); return d; }
280280

281281
#define ENABLE_OPERATORS_ON(T) ENABLE_SAFE_OPERATORS_ON(T) \
282-
inline T operator++(T& d, int) { d = T(int(d) + 1); return d; } \
283-
inline T operator--(T& d, int) { d = T(int(d) - 1); return d; } \
282+
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
283+
inline T& operator--(T& d) { return d = T(int(d) - 1); } \
284284
inline T operator/(const T d, int i) { return T(int(d) / i); } \
285-
inline T& operator/=(T& d, int i) { d = T(int(d) / i); return d; }
285+
inline T& operator/=(T& d, int i) { return d = T(int(d) / i); }
286286

287287
ENABLE_OPERATORS_ON(Value)
288288
ENABLE_OPERATORS_ON(PieceType)

0 commit comments

Comments
 (0)