Skip to content

Commit 7b7a948

Browse files
AliceRoseliavondele
authored andcommitted
Simplify indexing
Removes 1 function (from_to) from the Move type, change them to simpler raw. Remove the "and" use in the correction history structure calculation. Adjust the indexing. Passed simplification STC: LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 42880 W: 11327 L: 11113 D: 20440 Ptnml(0-2): 161, 4852, 11212, 5042, 173 https://tests.stockfishchess.org/tests/view/690593b8ea4b268f1fac1e8a Passed simplification LTC: LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 43560 W: 11276 L: 11079 D: 21205 Ptnml(0-2): 17, 4679, 12197, 4864, 23 https://tests.stockfishchess.org/tests/view/6906f819ea4b268f1fac216b closes #6391 Bench: 2523092
1 parent df88db1 commit 7b7a948

File tree

4 files changed

+21
-28
lines changed

4 files changed

+21
-28
lines changed

src/history.h

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,28 @@
3333

3434
namespace Stockfish {
3535

36-
constexpr int PAWN_HISTORY_SIZE = 8192; // has to be a power of 2
37-
constexpr int CORRECTION_HISTORY_SIZE = 32768; // has to be a power of 2
36+
constexpr int PAWN_HISTORY_SIZE = 8192; // has to be a power of 2
37+
constexpr int UINT_16_HISTORY_SIZE = std::numeric_limits<uint16_t>::max() + 1;
3838
constexpr int CORRECTION_HISTORY_LIMIT = 1024;
3939
constexpr int LOW_PLY_HISTORY_SIZE = 5;
4040

4141
static_assert((PAWN_HISTORY_SIZE & (PAWN_HISTORY_SIZE - 1)) == 0,
4242
"PAWN_HISTORY_SIZE has to be a power of 2");
4343

44-
static_assert((CORRECTION_HISTORY_SIZE & (CORRECTION_HISTORY_SIZE - 1)) == 0,
44+
static_assert((UINT_16_HISTORY_SIZE & (UINT_16_HISTORY_SIZE - 1)) == 0,
4545
"CORRECTION_HISTORY_SIZE has to be a power of 2");
4646

4747
inline int pawn_history_index(const Position& pos) {
4848
return pos.pawn_key() & (PAWN_HISTORY_SIZE - 1);
4949
}
5050

51-
inline int pawn_correction_history_index(const Position& pos) {
52-
return pos.pawn_key() & (CORRECTION_HISTORY_SIZE - 1);
53-
}
51+
inline uint16_t pawn_correction_history_index(const Position& pos) { return pos.pawn_key(); }
5452

55-
inline int minor_piece_index(const Position& pos) {
56-
return pos.minor_piece_key() & (CORRECTION_HISTORY_SIZE - 1);
57-
}
53+
inline uint16_t minor_piece_index(const Position& pos) { return pos.minor_piece_key(); }
5854

5955
template<Color c>
60-
inline int non_pawn_index(const Position& pos) {
61-
return pos.non_pawn_key(c) & (CORRECTION_HISTORY_SIZE - 1);
56+
inline uint16_t non_pawn_index(const Position& pos) {
57+
return pos.non_pawn_key(c);
6258
}
6359

6460
// StatsEntry is the container of various numerical statistics. We use a class
@@ -102,12 +98,11 @@ using Stats = MultiArray<StatsEntry<T, D>, Sizes...>;
10298
// during the current search, and is used for reduction and move ordering decisions.
10399
// It uses 2 tables (one for each color) indexed by the move's from and to squares,
104100
// see https://www.chessprogramming.org/Butterfly_Boards
105-
using ButterflyHistory = Stats<std::int16_t, 7183, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)>;
101+
using ButterflyHistory = Stats<std::int16_t, 7183, COLOR_NB, UINT_16_HISTORY_SIZE>;
106102

107103
// LowPlyHistory is addressed by play and move's from and to squares, used
108104
// to improve move ordering near the root
109-
using LowPlyHistory =
110-
Stats<std::int16_t, 7183, LOW_PLY_HISTORY_SIZE, int(SQUARE_NB) * int(SQUARE_NB)>;
105+
using LowPlyHistory = Stats<std::int16_t, 7183, LOW_PLY_HISTORY_SIZE, UINT_16_HISTORY_SIZE>;
111106

112107
// CapturePieceToHistory is addressed by a move's [piece][to][captured piece type]
113108
using CapturePieceToHistory = Stats<std::int16_t, 10692, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB>;
@@ -139,7 +134,7 @@ namespace Detail {
139134

140135
template<CorrHistType>
141136
struct CorrHistTypedef {
142-
using type = Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, CORRECTION_HISTORY_SIZE, COLOR_NB>;
137+
using type = Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, UINT_16_HISTORY_SIZE, COLOR_NB>;
143138
};
144139

145140
template<>
@@ -155,7 +150,7 @@ struct CorrHistTypedef<Continuation> {
155150
template<>
156151
struct CorrHistTypedef<NonPawn> {
157152
using type =
158-
Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, CORRECTION_HISTORY_SIZE, COLOR_NB, COLOR_NB>;
153+
Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, UINT_16_HISTORY_SIZE, COLOR_NB, COLOR_NB>;
159154
};
160155

161156
}

src/movepick.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ ExtMove* MovePicker::score(MoveList<Type>& ml) {
158158
else if constexpr (Type == QUIETS)
159159
{
160160
// histories
161-
m.value = 2 * (*mainHistory)[us][m.from_to()];
161+
m.value = 2 * (*mainHistory)[us][m.raw()];
162162
m.value += 2 * (*pawnHistory)[pawn_history_index(pos)][pc][to];
163163
m.value += (*continuationHistory[0])[pc][to];
164164
m.value += (*continuationHistory[1])[pc][to];
@@ -176,7 +176,7 @@ ExtMove* MovePicker::score(MoveList<Type>& ml) {
176176

177177

178178
if (ply < LOW_PLY_HISTORY_SIZE)
179-
m.value += 8 * (*lowPlyHistory)[ply][m.from_to()] / (1 + ply);
179+
m.value += 8 * (*lowPlyHistory)[ply][m.raw()] / (1 + ply);
180180
}
181181

182182
else // Type == EVASIONS
@@ -185,9 +185,9 @@ ExtMove* MovePicker::score(MoveList<Type>& ml) {
185185
m.value = PieceValue[capturedPiece] + (1 << 28);
186186
else
187187
{
188-
m.value = (*mainHistory)[us][m.from_to()] + (*continuationHistory[0])[pc][to];
188+
m.value = (*mainHistory)[us][m.raw()] + (*continuationHistory[0])[pc][to];
189189
if (ply < LOW_PLY_HISTORY_SIZE)
190-
m.value += (*lowPlyHistory)[ply][m.from_to()];
190+
m.value += (*lowPlyHistory)[ply][m.raw()];
191191
}
192192
}
193193
}

src/search.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ Value Search::Worker::search(
824824
if (((ss - 1)->currentMove).is_ok() && !(ss - 1)->inCheck && !priorCapture)
825825
{
826826
int evalDiff = std::clamp(-int((ss - 1)->staticEval + ss->staticEval), -200, 156) + 58;
827-
mainHistory[~us][((ss - 1)->currentMove).from_to()] << evalDiff * 9;
827+
mainHistory[~us][((ss - 1)->currentMove).raw()] << evalDiff * 9;
828828
if (!ttHit && type_of(pos.piece_on(prevSq)) != PAWN
829829
&& ((ss - 1)->currentMove).type_of() != PROMOTION)
830830
pawnHistory[pawn_history_index(pos)][pos.piece_on(prevSq)][prevSq] << evalDiff * 14;
@@ -1066,7 +1066,7 @@ Value Search::Worker::search(
10661066
if (history < -4312 * depth)
10671067
continue;
10681068

1069-
history += 76 * mainHistory[us][move.from_to()] / 32;
1069+
history += 76 * mainHistory[us][move.raw()] / 32;
10701070

10711071
// (*Scaler): Generally, a lower divisor scales well
10721072
lmrDepth += history / 3220;
@@ -1196,7 +1196,7 @@ Value Search::Worker::search(
11961196
ss->statScore = 803 * int(PieceValue[pos.captured_piece()]) / 128
11971197
+ captureHistory[movedPiece][move.to_sq()][type_of(pos.captured_piece())];
11981198
else
1199-
ss->statScore = 2 * mainHistory[us][move.from_to()]
1199+
ss->statScore = 2 * mainHistory[us][move.raw()]
12001200
+ (*contHist[0])[movedPiece][move.to_sq()]
12011201
+ (*contHist[1])[movedPiece][move.to_sq()];
12021202

@@ -1414,7 +1414,7 @@ Value Search::Worker::search(
14141414
update_continuation_histories(ss - 1, pos.piece_on(prevSq), prevSq,
14151415
scaledBonus * 400 / 32768);
14161416

1417-
mainHistory[~us][((ss - 1)->currentMove).from_to()] << scaledBonus * 220 / 32768;
1417+
mainHistory[~us][((ss - 1)->currentMove).raw()] << scaledBonus * 220 / 32768;
14181418

14191419
if (type_of(pos.piece_on(prevSq)) != PAWN && ((ss - 1)->currentMove).type_of() != PROMOTION)
14201420
pawnHistory[pawn_history_index(pos)][pos.piece_on(prevSq)][prevSq]
@@ -1862,10 +1862,10 @@ void update_quiet_histories(
18621862
const Position& pos, Stack* ss, Search::Worker& workerThread, Move move, int bonus) {
18631863

18641864
Color us = pos.side_to_move();
1865-
workerThread.mainHistory[us][move.from_to()] << bonus; // Untuned to prevent duplicate effort
1865+
workerThread.mainHistory[us][move.raw()] << bonus; // Untuned to prevent duplicate effort
18661866

18671867
if (ss->ply < LOW_PLY_HISTORY_SIZE)
1868-
workerThread.lowPlyHistory[ss->ply][move.from_to()] << bonus * 761 / 1024;
1868+
workerThread.lowPlyHistory[ss->ply][move.raw()] << bonus * 761 / 1024;
18691869

18701870
update_continuation_histories(ss, pos.moved_piece(move), move.to_sq(), bonus * 955 / 1024);
18711871

src/types.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,6 @@ class Move {
448448
return Square(data & 0x3F);
449449
}
450450

451-
constexpr int from_to() const { return data & 0xFFF; }
452-
453451
constexpr MoveType type_of() const { return MoveType(data & (3 << 14)); }
454452

455453
constexpr PieceType promotion_type() const { return PieceType(((data >> 12) & 3) + KNIGHT); }

0 commit comments

Comments
 (0)