Skip to content

Commit 28b6a45

Browse files
gvreulsmcostalba
authored andcommitted
Use constexpr when makes sense
No functional change.
1 parent ccd6bad commit 28b6a45

File tree

3 files changed

+47
-49
lines changed

3 files changed

+47
-49
lines changed

src/bitboard.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ inline Bitboard& operator^=(Bitboard& b, Square s) {
126126
return b ^= SquareBB[s];
127127
}
128128

129-
inline bool more_than_one(Bitboard b) {
129+
constexpr bool more_than_one(Bitboard b) {
130130
return b & (b - 1);
131131
}
132132

@@ -154,7 +154,7 @@ inline Bitboard file_bb(Square s) {
154154
/// shift() moves a bitboard one step along direction D. Mainly for pawns
155155

156156
template<Square D>
157-
inline Bitboard shift(Bitboard b) {
157+
constexpr Bitboard shift(Bitboard b) {
158158
return D == NORTH ? b << 8 : D == SOUTH ? b >> 8
159159
: D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == SOUTH_EAST ? (b & ~FileHBB) >> 7
160160
: D == NORTH_WEST ? (b & ~FileABB) << 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9

src/movegen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct ExtMove {
4545

4646
// Inhibit unwanted implicit conversions to Move
4747
// with an ambiguity that yields to a compile error.
48-
operator float() const;
48+
operator float() const = delete;
4949
};
5050

5151
inline bool operator<(const ExtMove& f, const ExtMove& s) {

src/types.h

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ enum CastlingRight {
146146
};
147147

148148
template<Color C, CastlingSide S> struct MakeCastling {
149-
static const CastlingRight
149+
static constexpr CastlingRight
150150
right = C == WHITE ? S == QUEEN_SIDE ? WHITE_OOO : WHITE_OO
151151
: S == QUEEN_SIDE ? BLACK_OOO : BLACK_OO;
152152
};
@@ -262,41 +262,39 @@ enum Rank : int {
262262
/// care to avoid left-shifting a signed int to avoid undefined behavior.
263263
enum Score : int { SCORE_ZERO };
264264

265-
inline Score make_score(int mg, int eg) {
265+
constexpr Score make_score(int mg, int eg) {
266266
return Score((int)((unsigned int)eg << 16) + mg);
267267
}
268268

269269
/// Extracting the signed lower and upper 16 bits is not so trivial because
270270
/// according to the standard a simple cast to short is implementation defined
271271
/// and so is a right shift of a signed integer.
272272
inline Value eg_value(Score s) {
273-
274273
union { uint16_t u; int16_t s; } eg = { uint16_t(unsigned(s + 0x8000) >> 16) };
275274
return Value(eg.s);
276275
}
277276

278277
inline Value mg_value(Score s) {
279-
280278
union { uint16_t u; int16_t s; } mg = { uint16_t(unsigned(s)) };
281279
return Value(mg.s);
282280
}
283281

284-
#define ENABLE_BASE_OPERATORS_ON(T) \
285-
inline T operator+(T d1, T d2) { return T(int(d1) + int(d2)); } \
286-
inline T operator-(T d1, T d2) { return T(int(d1) - int(d2)); } \
287-
inline T operator-(T d) { return T(-int(d)); } \
288-
inline T& operator+=(T& d1, T d2) { return d1 = d1 + d2; } \
289-
inline T& operator-=(T& d1, T d2) { return d1 = d1 - d2; } \
290-
291-
#define ENABLE_FULL_OPERATORS_ON(T) \
292-
ENABLE_BASE_OPERATORS_ON(T) \
293-
inline T operator*(int i, T d) { return T(i * int(d)); } \
294-
inline T operator*(T d, int i) { return T(int(d) * i); } \
295-
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
296-
inline T& operator--(T& d) { return d = T(int(d) - 1); } \
297-
inline T operator/(T d, int i) { return T(int(d) / i); } \
298-
inline int operator/(T d1, T d2) { return int(d1) / int(d2); } \
299-
inline T& operator*=(T& d, int i) { return d = T(int(d) * i); } \
282+
#define ENABLE_BASE_OPERATORS_ON(T) \
283+
constexpr T operator+(T d1, T d2) { return T(int(d1) + int(d2)); } \
284+
constexpr T operator-(T d1, T d2) { return T(int(d1) - int(d2)); } \
285+
constexpr T operator-(T d) { return T(-int(d)); } \
286+
inline T& operator+=(T& d1, T d2) { return d1 = d1 + d2; } \
287+
inline T& operator-=(T& d1, T d2) { return d1 = d1 - d2; } \
288+
289+
#define ENABLE_FULL_OPERATORS_ON(T) \
290+
ENABLE_BASE_OPERATORS_ON(T) \
291+
constexpr T operator*(int i, T d) { return T(i * int(d)); } \
292+
constexpr T operator*(T d, int i) { return T(int(d) * i); } \
293+
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
294+
inline T& operator--(T& d) { return d = T(int(d) - 1); } \
295+
constexpr T operator/(T d, int i) { return T(int(d) / i); } \
296+
constexpr int operator/(T d1, T d2) { return int(d1) / int(d2); } \
297+
inline T& operator*=(T& d, int i) { return d = T(int(d) * i); } \
300298
inline T& operator/=(T& d, int i) { return d = T(int(d) / i); }
301299

302300
ENABLE_FULL_OPERATORS_ON(Value)
@@ -314,14 +312,14 @@ ENABLE_BASE_OPERATORS_ON(Score)
314312
#undef ENABLE_BASE_OPERATORS_ON
315313

316314
/// Additional operators to add integers to a Value
317-
inline Value operator+(Value v, int i) { return Value(int(v) + i); }
318-
inline Value operator-(Value v, int i) { return Value(int(v) - i); }
315+
constexpr Value operator+(Value v, int i) { return Value(int(v) + i); }
316+
constexpr Value operator-(Value v, int i) { return Value(int(v) - i); }
319317
inline Value& operator+=(Value& v, int i) { return v = v + i; }
320318
inline Value& operator-=(Value& v, int i) { return v = v - i; }
321319

322320
/// Only declared but not defined. We don't want to multiply two scores due to
323321
/// a very high risk of overflow. So user should explicitly convert to integer.
324-
inline Score operator*(Score s1, Score s2);
322+
Score operator*(Score s1, Score s2) = delete;
325323

326324
/// Division of a Score must be handled separately for each term
327325
inline Score operator/(Score s, int i) {
@@ -340,39 +338,39 @@ inline Score operator*(Score s, int i) {
340338
return result;
341339
}
342340

343-
inline Color operator~(Color c) {
341+
constexpr Color operator~(Color c) {
344342
return Color(c ^ BLACK); // Toggle color
345343
}
346344

347-
inline Square operator~(Square s) {
345+
constexpr Square operator~(Square s) {
348346
return Square(s ^ SQ_A8); // Vertical flip SQ_A1 -> SQ_A8
349347
}
350348

351-
inline Piece operator~(Piece pc) {
349+
constexpr Piece operator~(Piece pc) {
352350
return Piece(pc ^ 8); // Swap color of piece B_KNIGHT -> W_KNIGHT
353351
}
354352

355-
inline CastlingRight operator|(Color c, CastlingSide s) {
353+
constexpr CastlingRight operator|(Color c, CastlingSide s) {
356354
return CastlingRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
357355
}
358356

359-
inline Value mate_in(int ply) {
357+
constexpr Value mate_in(int ply) {
360358
return VALUE_MATE - ply;
361359
}
362360

363-
inline Value mated_in(int ply) {
361+
constexpr Value mated_in(int ply) {
364362
return -VALUE_MATE + ply;
365363
}
366364

367-
inline Square make_square(File f, Rank r) {
365+
constexpr Square make_square(File f, Rank r) {
368366
return Square((r << 3) + f);
369367
}
370368

371-
inline Piece make_piece(Color c, PieceType pt) {
369+
constexpr Piece make_piece(Color c, PieceType pt) {
372370
return Piece((c << 3) + pt);
373371
}
374372

375-
inline PieceType type_of(Piece pc) {
373+
constexpr PieceType type_of(Piece pc) {
376374
return PieceType(pc & 7);
377375
}
378376

@@ -381,27 +379,27 @@ inline Color color_of(Piece pc) {
381379
return Color(pc >> 3);
382380
}
383381

384-
inline bool is_ok(Square s) {
382+
constexpr bool is_ok(Square s) {
385383
return s >= SQ_A1 && s <= SQ_H8;
386384
}
387385

388-
inline File file_of(Square s) {
386+
constexpr File file_of(Square s) {
389387
return File(s & 7);
390388
}
391389

392-
inline Rank rank_of(Square s) {
390+
constexpr Rank rank_of(Square s) {
393391
return Rank(s >> 3);
394392
}
395393

396-
inline Square relative_square(Color c, Square s) {
394+
constexpr Square relative_square(Color c, Square s) {
397395
return Square(s ^ (c * 56));
398396
}
399397

400-
inline Rank relative_rank(Color c, Rank r) {
398+
constexpr Rank relative_rank(Color c, Rank r) {
401399
return Rank(r ^ (c * 7));
402400
}
403401

404-
inline Rank relative_rank(Color c, Square s) {
402+
constexpr Rank relative_rank(Color c, Square s) {
405403
return relative_rank(c, rank_of(s));
406404
}
407405

@@ -410,27 +408,27 @@ inline bool opposite_colors(Square s1, Square s2) {
410408
return ((s >> 3) ^ s) & 1;
411409
}
412410

413-
inline Square pawn_push(Color c) {
411+
constexpr Square pawn_push(Color c) {
414412
return c == WHITE ? NORTH : SOUTH;
415413
}
416414

417-
inline Square from_sq(Move m) {
415+
constexpr Square from_sq(Move m) {
418416
return Square((m >> 6) & 0x3F);
419417
}
420418

421-
inline Square to_sq(Move m) {
419+
constexpr Square to_sq(Move m) {
422420
return Square(m & 0x3F);
423421
}
424422

425-
inline int from_to(Move m) {
423+
constexpr int from_to(Move m) {
426424
return m & 0xFFF;
427425
}
428426

429-
inline MoveType type_of(Move m) {
427+
constexpr MoveType type_of(Move m) {
430428
return MoveType(m & (3 << 14));
431429
}
432430

433-
inline PieceType promotion_type(Move m) {
431+
constexpr PieceType promotion_type(Move m) {
434432
return PieceType(((m >> 12) & 3) + KNIGHT);
435433
}
436434

@@ -439,11 +437,11 @@ inline Move make_move(Square from, Square to) {
439437
}
440438

441439
template<MoveType T>
442-
inline Move make(Square from, Square to, PieceType pt = KNIGHT) {
440+
constexpr Move make(Square from, Square to, PieceType pt = KNIGHT) {
443441
return Move(T + ((pt - KNIGHT) << 12) + (from << 6) + to);
444442
}
445443

446-
inline bool is_ok(Move m) {
444+
constexpr bool is_ok(Move m) {
447445
return from_sq(m) != to_sq(m); // Catch MOVE_NULL and MOVE_NONE
448446
}
449447

0 commit comments

Comments
 (0)