Skip to content

Commit 13f90a3

Browse files
committed
Get rid of ReducedStateInfo struct
ReducedStateInfo is a redundant struct that is also prone to errors, indeed must be updated any time is updated StateInfo. It is a trick to partial copy a StateInfo object in do_move(). This patch takes advantage of builtin macro offsetof() to directly calculate the number of quad words to copy. Note that we still use memcpy to do the actual job of copying the (48 bytes) of data. Idea by Richard Vida. No functional and no performance change.
1 parent ad1941f commit 13f90a3

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

src/position.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,9 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
770770
Key k = st->key;
771771

772772
// Copy some fields of old state to our new StateInfo object except the ones
773-
// which are recalculated from scratch anyway, then switch our state pointer
774-
// to point to the new, ready to be updated, state.
775-
memcpy(&newSt, st, sizeof(ReducedStateInfo));
773+
// which are going to be recalculated from scratch anyway, then switch our state
774+
// pointer to point to the new, ready to be updated, state.
775+
memcpy(&newSt, st, StateCopySize64 * sizeof(uint64_t));
776776

777777
newSt.previous = st;
778778
st = &newSt;

src/position.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define POSITION_H_INCLUDED
2222

2323
#include <cassert>
24+
#include <cstddef>
2425

2526
#include "bitboard.h"
2627
#include "types.h"
@@ -44,7 +45,7 @@ struct CheckInfo {
4445

4546
/// The StateInfo struct stores information we need to restore a Position
4647
/// object to its previous state when we retract a move. Whenever a move
47-
/// is made on the board (by calling Position::do_move), an StateInfo object
48+
/// is made on the board (by calling Position::do_move), a StateInfo object
4849
/// must be passed as a parameter.
4950

5051
struct StateInfo {
@@ -60,13 +61,10 @@ struct StateInfo {
6061
StateInfo* previous;
6162
};
6263

63-
struct ReducedStateInfo {
64-
Key pawnKey, materialKey;
65-
Value npMaterial[COLOR_NB];
66-
int castleRights, rule50, pliesFromNull;
67-
Score psqScore;
68-
Square epSquare;
69-
};
64+
65+
/// When making a move the current StateInfo up to 'key' excluded is copied to
66+
/// the new one. Here we calculate the quad words (64bits) needed to be copied.
67+
const size_t StateCopySize64 = offsetof(StateInfo, key) / sizeof(uint64_t) + 1;
7068

7169

7270
/// The position data structure. A position consists of the following data:

0 commit comments

Comments
 (0)