From d852a9195ea92ae7b5699077f358ca102f79fedc Mon Sep 17 00:00:00 2001 From: KazApps Date: Wed, 7 Jan 2026 19:45:50 +0900 Subject: [PATCH] Make enums unsigned Speed up by using unsigned enums. Passed STC: LLR: 2.98 (-2.94,2.94) <0.00,2.00> Total: 49248 W: 12894 L: 12568 D: 23786 Ptnml(0-2): 119, 5353, 13397, 5593, 16 https://tests.stockfishchess.org/tests/view/695e3e5002d0182a589fe965 closes https://github.com/official-stockfish/Stockfish/pull/6532 No functional change --- src/bitboard.cpp | 5 ++++- src/movegen.cpp | 8 ++++---- src/position.cpp | 17 ++++++++++++----- src/timeman.h | 2 +- src/types.h | 18 +++++++++--------- src/uci.h | 2 +- 6 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 4decb8d66..0861222cf 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -49,12 +49,15 @@ std::string Bitboards::pretty(Bitboard b) { std::string s = "+---+---+---+---+---+---+---+---+\n"; - for (Rank r = RANK_8; r >= RANK_1; --r) + for (Rank r = RANK_8;; --r) { for (File f = FILE_A; f <= FILE_H; ++f) s += b & make_square(f, r) ? "| X " : "| "; s += "| " + std::to_string(1 + r) + "\n+---+---+---+---+---+---+---+---+\n"; + + if (r == RANK_1) + break; } s += " a b c d e f g h\n"; diff --git a/src/movegen.cpp b/src/movegen.cpp index d22faad25..e63707a1f 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -47,10 +47,10 @@ template inline Move* splat_pawn_moves(Move* moveList, Bitboard to_bb) { alignas(64) static constexpr auto SPLAT_TABLE = [] { std::array table{}; - for (int8_t i = 0; i < 64; i++) + for (int i = 0; i < 64; i++) { - Square from{std::clamp(i - offset, 0, 63)}; - table[i] = {Move(from, Square{i})}; + Square from{uint8_t(std::clamp(i - offset, 0, 63))}; + table[i] = {Move(from, Square{uint8_t(i)})}; } return table; }(); @@ -68,7 +68,7 @@ inline Move* splat_pawn_moves(Move* moveList, Bitboard to_bb) { inline Move* splat_moves(Move* moveList, Square from, Bitboard to_bb) { alignas(64) static constexpr auto SPLAT_TABLE = [] { std::array table{}; - for (int8_t i = 0; i < 64; i++) + for (uint8_t i = 0; i < 64; i++) table[i] = {Move(SQUARE_ZERO, Square{i})}; return table; }(); diff --git a/src/position.cpp b/src/position.cpp index d8b02e8ab..907dfde40 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -66,12 +66,15 @@ std::ostream& operator<<(std::ostream& os, const Position& pos) { os << "\n +---+---+---+---+---+---+---+---+\n"; - for (Rank r = RANK_8; r >= RANK_1; --r) + for (Rank r = RANK_8;; --r) { for (File f = FILE_A; f <= FILE_H; ++f) os << " | " << PieceToChar[pos.piece_on(make_square(f, r))]; os << " | " << (1 + r) << "\n +---+---+---+---+---+---+---+---+\n"; + + if (r == RANK_1) + break; } os << " a b c d e f g h\n" @@ -416,7 +419,7 @@ string Position::fen() const { int emptyCnt; std::ostringstream ss; - for (Rank r = RANK_8; r >= RANK_1; --r) + for (Rank r = RANK_8;; --r) { for (File f = FILE_A; f <= FILE_H; ++f) { @@ -430,8 +433,9 @@ string Position::fen() const { ss << PieceToChar[piece_on(make_square(f, r))]; } - if (r > RANK_1) - ss << '/'; + if (r == RANK_1) + break; + ss << '/'; } ss << (sideToMove == WHITE ? " w " : " b "); @@ -1477,10 +1481,13 @@ void Position::flip() { string f, token; std::stringstream ss(fen()); - for (Rank r = RANK_8; r >= RANK_1; --r) // Piece placement + for (Rank r = RANK_8;; --r) // Piece placement { std::getline(ss, token, r > RANK_1 ? '/' : ' '); f.insert(0, token + (f.empty() ? " " : "/")); + + if (r == RANK_1) + break; } ss >> token; // Active color diff --git a/src/timeman.h b/src/timeman.h index e72cc102a..08e8da10d 100644 --- a/src/timeman.h +++ b/src/timeman.h @@ -26,7 +26,7 @@ namespace Stockfish { class OptionsMap; -enum Color : int8_t; +enum Color : uint8_t; namespace Search { struct LimitsType; diff --git a/src/types.h b/src/types.h index b5a0498a8..10a0d1ac5 100644 --- a/src/types.h +++ b/src/types.h @@ -117,13 +117,13 @@ using Bitboard = uint64_t; constexpr int MAX_MOVES = 256; constexpr int MAX_PLY = 246; -enum Color : int8_t { +enum Color : uint8_t { WHITE, BLACK, COLOR_NB = 2 }; -enum CastlingRights : int8_t { +enum CastlingRights : uint8_t { NO_CASTLING, WHITE_OO, WHITE_OOO = WHITE_OO << 1, @@ -139,7 +139,7 @@ enum CastlingRights : int8_t { CASTLING_RIGHT_NB = 16 }; -enum Bound : int8_t { +enum Bound : uint8_t { BOUND_NONE, BOUND_UPPER, BOUND_LOWER, @@ -190,13 +190,13 @@ constexpr Value QueenValue = 2538; // clang-format off -enum PieceType : std::int8_t { +enum PieceType : std::uint8_t { NO_PIECE_TYPE, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING, ALL_PIECES = 0, PIECE_TYPE_NB = 8 }; -enum Piece : std::int8_t { +enum Piece : std::uint8_t { NO_PIECE, W_PAWN = PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING, B_PAWN = PAWN + 8, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING, @@ -227,7 +227,7 @@ constexpr Depth DEPTH_UNSEARCHED = -2; constexpr Depth DEPTH_ENTRY_OFFSET = -3; // clang-format off -enum Square : int8_t { +enum Square : uint8_t { SQ_A1, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1, SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2, SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3, @@ -255,7 +255,7 @@ enum Direction : int8_t { NORTH_WEST = NORTH + WEST }; -enum File : int8_t { +enum File : uint8_t { FILE_A, FILE_B, FILE_C, @@ -267,7 +267,7 @@ enum File : int8_t { FILE_NB }; -enum Rank : int8_t { +enum Rank : uint8_t { RANK_1, RANK_2, RANK_3, @@ -406,7 +406,7 @@ constexpr Key make_key(uint64_t seed) { } -enum MoveType { +enum MoveType : uint16_t { NORMAL, PROMOTION = 1 << 14, EN_PASSANT = 2 << 14, diff --git a/src/uci.h b/src/uci.h index c9b594393..ebc04fc3c 100644 --- a/src/uci.h +++ b/src/uci.h @@ -33,7 +33,7 @@ namespace Stockfish { class Position; class Move; class Score; -enum Square : int8_t; +enum Square : uint8_t; using Value = int; class UCIEngine {