Skip to content

Commit

Permalink
Bench: 23470399
Browse files Browse the repository at this point in the history
  • Loading branch information
TerjeKir committed Jan 3, 2025
1 parent 54e8064 commit 4319935
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
28 changes: 27 additions & 1 deletion src/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ static Key GenMaterialKey(const Position *pos) {
return key;
}

static Key GenMinorKey(const Position *pos) {

Key key = 0;

for (Square sq = A1; sq <= H8; ++sq)
if (PieceTypeOf(pieceOn(sq)) == KNIGHT || PieceTypeOf(pieceOn(sq)) == BISHOP || PieceTypeOf(pieceOn(sq)) == KING)
key ^= PieceKeys[pieceOn(sq)][sq];

return key;
}

static Key GenMajorKey(const Position *pos) {

Key key = 0;

for (Square sq = A1; sq <= H8; ++sq)
if (PieceTypeOf(pieceOn(sq)) == ROOK || PieceTypeOf(pieceOn(sq)) == QUEEN || PieceTypeOf(pieceOn(sq)) == KING)
key ^= PieceKeys[pieceOn(sq)][sq];

return key;
}

// Generates a hash key from scratch
static Key GenNonPawnKey(const Position *pos, const Color color) {

Expand Down Expand Up @@ -253,6 +275,8 @@ void ParseFen(const char *fen, Position *pos) {
pos->checkers = Checkers(pos);
pos->key = GenPosKey(pos);
pos->materialKey = GenMaterialKey(pos);
pos->minorKey = GenMinorKey(pos);
pos->majorKey = GenMajorKey(pos);
pos->nonPawnKey[WHITE] = GenNonPawnKey(pos, WHITE);
pos->nonPawnKey[BLACK] = GenNonPawnKey(pos, BLACK);
pos->phase = UpdatePhase(pos->phaseValue);
Expand Down Expand Up @@ -540,8 +564,10 @@ bool PositionOk(const Position *pos) {
assert(pos->castlingRights >= 0 && pos->castlingRights <= 15);

assert(GenPosKey(pos) == pos->key);
assert(GenMaterialKey(pos) == pos->materialKey);
assert(GenPawnKey(pos) == pos->pawnKey);
assert(GenMaterialKey(pos) == pos->materialKey);
assert(GenMinorKey(pos) == pos->minorKey);
assert(GenMajorKey(pos) == pos->majorKey);
assert(GenNonPawnKey(pos, WHITE) == pos->nonPawnKey[WHITE]);
assert(GenNonPawnKey(pos, BLACK) == pos->nonPawnKey[BLACK]);

Expand Down
2 changes: 2 additions & 0 deletions src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ typedef struct Position {
Key key;
Key materialKey;
Key pawnKey;
Key minorKey;
Key majorKey;
Key nonPawnKey[COLOR_NB];

uint64_t nodes;
Expand Down
10 changes: 10 additions & 0 deletions src/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#define ContEntry(offset, move) (&(*(ss-offset)->continuation)[piece(move)][toSq(move)])
#define PawnCorrEntry() (&thread->pawnCorrHistory[thread->pos.stm][PawnCorrIndex(&thread->pos)])
#define MatCorrEntry() (&thread->matCorrHistory[thread->pos.stm][MatCorrIndex(&thread->pos)])
#define MinorCorrEntry() (&thread->minorCorrHistory[thread->pos.stm][MinorCorrIndex(&thread->pos)])
#define MajorCorrEntry() (&thread->majorCorrHistory[thread->pos.stm][MajorCorrIndex(&thread->pos)])
#define ContCorrEntry(offset) (&(*(ss-offset)->contCorr)[piece((ss-1)->move)][toSq((ss-1)->move)])
#define NonPawnCorrEntry(color) (&thread->nonPawnCorrHistory[color][thread->pos.stm][NonPawnCorrIndex(&thread->pos, color)])

Expand All @@ -41,13 +43,17 @@
#define ContHistoryUpdate(offset, move, bonus) (HistoryBonus(ContEntry(offset, move), bonus, 21250))
#define PawnCorrHistoryUpdate(bonus) (HistoryBonus(PawnCorrEntry(), bonus, 1662))
#define MatCorrHistoryUpdate(bonus) (HistoryBonus(MatCorrEntry(), bonus, 1077))
#define MinorCorrHistoryUpdate(bonus) (HistoryBonus(MinorCorrEntry(), bonus, 1024))
#define MajorCorrHistoryUpdate(bonus) (HistoryBonus(MajorCorrEntry(), bonus, 1024))
#define ContCorrHistoryUpdate(offset, bonus) (HistoryBonus(ContCorrEntry(offset), bonus, 1220))
#define NonPawnCorrHistoryUpdate(bonus, color) (HistoryBonus(NonPawnCorrEntry(color), bonus, 1024))


INLINE int PawnStructure(const Position *pos) { return pos->pawnKey & (PAWN_HISTORY_SIZE - 1); }
INLINE int PawnCorrIndex(const Position *pos) { return pos->pawnKey & (CORRECTION_HISTORY_SIZE - 1); }
INLINE int MatCorrIndex(const Position *pos) { return pos->materialKey & (CORRECTION_HISTORY_SIZE - 1); }
INLINE int MinorCorrIndex(const Position *pos) { return pos->minorKey & (CORRECTION_HISTORY_SIZE - 1); }
INLINE int MajorCorrIndex(const Position *pos) { return pos->majorKey & (CORRECTION_HISTORY_SIZE - 1); }
INLINE int NonPawnCorrIndex(const Position *pos, Color c) { return pos->nonPawnKey[c] & (CORRECTION_HISTORY_SIZE - 1); }


Expand Down Expand Up @@ -122,6 +128,8 @@ INLINE void UpdateCorrectionHistory(Thread *thread, Stack *ss, int bestScore, in
int bonus = CorrectionBonus(bestScore, eval, depth);
PawnCorrHistoryUpdate(bonus);
MatCorrHistoryUpdate(bonus);
MinorCorrHistoryUpdate(bonus);
MajorCorrHistoryUpdate(bonus);
NonPawnCorrHistoryUpdate(bonus, WHITE);
NonPawnCorrHistoryUpdate(bonus, BLACK);
ContCorrHistoryUpdate(2, bonus);
Expand Down Expand Up @@ -151,6 +159,8 @@ INLINE int GetHistory(const Thread *thread, Stack *ss, Move move) {
INLINE int GetCorrectionHistory(const Thread *thread, const Stack *ss) {
int c = 6554 * *PawnCorrEntry()
+ 4520 * *MatCorrEntry()
+ 6800 * *MinorCorrEntry()
+ 3700 * *MajorCorrEntry()
+ 7000 * (*NonPawnCorrEntry(WHITE) + *NonPawnCorrEntry(BLACK))
+ 3121 * *ContCorrEntry(2)
+ 2979 * *ContCorrEntry(3)
Expand Down
33 changes: 30 additions & 3 deletions src/makemove.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,18 @@ static void ClearPiece(Position *pos, const Square sq, const bool hash) {

if (PieceTypeOf(piece) == PAWN)
pos->pawnKey ^= PieceKeys[piece][sq];
else
else {
pos->nonPawnKey[color] ^= PieceKeys[piece][sq];

if (pt == KING) {
pos->minorKey ^= PieceKeys[piece][sq];
pos->majorKey ^= PieceKeys[piece][sq];
} else if (pt >= ROOK)
pos->majorKey ^= PieceKeys[piece][sq];
else
pos->minorKey ^= PieceKeys[piece][sq];
}

// Set square to empty
pieceOn(sq) = EMPTY;

Expand Down Expand Up @@ -84,9 +93,18 @@ static void AddPiece(Position *pos, const Square sq, const Piece piece, const bo

if (PieceTypeOf(piece) == PAWN)
pos->pawnKey ^= PieceKeys[piece][sq];
else
else {
pos->nonPawnKey[color] ^= PieceKeys[piece][sq];

if (pt == KING) {
pos->minorKey ^= PieceKeys[piece][sq];
pos->majorKey ^= PieceKeys[piece][sq];
} else if (pt >= ROOK)
pos->majorKey ^= PieceKeys[piece][sq];
else
pos->minorKey ^= PieceKeys[piece][sq];
}

// Update square
pieceOn(sq) = piece;

Expand Down Expand Up @@ -123,9 +141,18 @@ static void MovePiece(Position *pos, const Square from, const Square to, const b

if (PieceTypeOf(piece) == PAWN)
pos->pawnKey ^= PieceKeys[piece][from] ^ PieceKeys[piece][to];
else
else {
pos->nonPawnKey[color] ^= PieceKeys[piece][from] ^ PieceKeys[piece][to];

if (pt == KING) {
pos->minorKey ^= PieceKeys[piece][from] ^ PieceKeys[piece][to];
pos->majorKey ^= PieceKeys[piece][from] ^ PieceKeys[piece][to];
} else if (pt >= ROOK)
pos->majorKey ^= PieceKeys[piece][from] ^ PieceKeys[piece][to];
else
pos->minorKey ^= PieceKeys[piece][from] ^ PieceKeys[piece][to];
}

// Set old square to empty, new to piece
pieceOn(from) = EMPTY;
pieceOn(to) = piece;
Expand Down
2 changes: 2 additions & 0 deletions src/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ void ResetThreads() {
memset(Threads[i].continuation, 0, sizeof(Threads[i].continuation)),
memset(Threads[i].pawnCorrHistory, 0, sizeof(Threads[i].pawnCorrHistory)),
memset(Threads[i].matCorrHistory, 0, sizeof(Threads[i].matCorrHistory)),
memset(Threads[i].minorCorrHistory,0, sizeof(Threads[i].minorCorrHistory)),
memset(Threads[i].majorCorrHistory,0, sizeof(Threads[i].majorCorrHistory)),
memset(Threads[i].contCorrHistory, 0, sizeof(Threads[i].contCorrHistory));
}

Expand Down
2 changes: 2 additions & 0 deletions src/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ typedef struct Thread {
ContinuationHistory continuation[2][2];
CorrectionHistory pawnCorrHistory;
CorrectionHistory matCorrHistory;
CorrectionHistory minorCorrHistory;
CorrectionHistory majorCorrHistory;
CorrectionHistory nonPawnCorrHistory[COLOR_NB];
ContiuationCorrectionHistory contCorrHistory;

Expand Down

0 comments on commit 4319935

Please sign in to comment.