From afb50d69691d9daba84caf9051918fe174adb490 Mon Sep 17 00:00:00 2001 From: Eren Araz Date: Wed, 25 Jun 2025 01:46:23 +0300 Subject: [PATCH] add and try major correction history bench: 8586549 --- src/board_constants.c | 1 + src/board_constants.h | 1 + src/fen.c | 1 + src/move.c | 35 ++++++++++++++++++++++++++++++++++- src/search.c | 22 ++++++++++++++++++++-- src/search.h | 4 ++++ src/structs.h | 2 ++ src/table.c | 22 ++++++++++++++++++++++ src/table.h | 1 + src/uci.c | 1 + 10 files changed, 87 insertions(+), 3 deletions(-) diff --git a/src/board_constants.c b/src/board_constants.c index 52eabbf3..8b016d39 100644 --- a/src/board_constants.c +++ b/src/board_constants.c @@ -76,6 +76,7 @@ int mvvLva[12][12] = { }; int minorPieces[6] = {B, b, N, n, K, k}; +int majorPieces[4] = {R, r, Q, q}; int whiteNonPawnPieces[5] = { R, N, B, Q, K }; int blackNonPawnPieces[5] = {r, n, b, q, k }; diff --git a/src/board_constants.h b/src/board_constants.h index 97016c88..647889f3 100644 --- a/src/board_constants.h +++ b/src/board_constants.h @@ -52,6 +52,7 @@ extern char promotedPieces[]; extern char *unicodePieces[12]; extern int charPieces[]; extern int minorPieces[6]; +extern int majorPieces[4]; extern int whiteNonPawnPieces[5]; extern int blackNonPawnPieces[5]; extern int mvvLva[12][12]; diff --git a/src/fen.c b/src/fen.c index 6c953495..98677b97 100644 --- a/src/fen.c +++ b/src/fen.c @@ -95,6 +95,7 @@ void parseFEN(char *fen, board* position) { position->hashKey = generateHashKey(position); position->pawnKey = generatePawnKey(position); position->minorKey = generateMinorKey(position); + position->majorKey = generateMajorKey(position); position->whiteNonPawnKey = generate_white_np_hash_key(position); position->blackNonPawnKey = generate_black_np_hash_key(position); } diff --git a/src/move.c b/src/move.c index afe35b3d..02fa4557 100644 --- a/src/move.c +++ b/src/move.c @@ -43,6 +43,7 @@ void copyBoard(board *p, struct copyposition *cp) { cp->hashKeyCopy = p->hashKey; cp->pawnKeyCopy = p->pawnKey; cp->minorKeyCopy = p->minorKey; + cp->majorKeyCopy = p->majorKey; cp->whiteNonPawnKeyCopy = p->whiteNonPawnKey; cp->blackNonPawnKeyCopy = p->blackNonPawnKey; cp->sideCopy = p->side, cp->enpassantCopy = p->enpassant, cp->castleCopy = p->castle; @@ -68,6 +69,7 @@ void takeBack(board *p, struct copyposition *cp) { p->hashKey = cp->hashKeyCopy; p->pawnKey = cp->pawnKeyCopy; p->minorKey = cp->minorKeyCopy; + p->majorKey = cp->majorKeyCopy; p->whiteNonPawnKey = cp->whiteNonPawnKeyCopy; p->blackNonPawnKey = cp->blackNonPawnKeyCopy; p->side = cp->sideCopy, p->enpassant = cp->enpassantCopy, p->castle = cp->castleCopy; @@ -162,6 +164,13 @@ bool isMinor (int piece) { return false; } +bool isMajor (int piece) { + if (piece == Q || piece == q || piece == R || piece == r) { + return true; + } + return false; +} + // make move on chess board int makeMove(int move, int moveFlag, board* position) { int isLegalCapture = getMoveCapture(move); @@ -212,6 +221,12 @@ int makeMove(int move, int moveFlag, board* position) { position->minorKey ^= pieceKeys[piece][targetSquare]; } + if (isMajor(piece)) { + position->majorKey ^= pieceKeys[piece][sourceSquare]; + position->majorKey ^= pieceKeys[piece][targetSquare]; + + } + // handling capture moves if (capture) { @@ -248,6 +263,11 @@ int makeMove(int move, int moveFlag, board* position) { if (isMinor(bbPiece)) { position->minorKey ^= pieceKeys[bbPiece][targetSquare]; } + + if (isMajor(bbPiece)) { + position->majorKey ^= pieceKeys[bbPiece][targetSquare]; + } + break; } } @@ -316,6 +336,11 @@ int makeMove(int move, int moveFlag, board* position) { if (isMinor(promotedPiece)) { position->minorKey ^= pieceKeys[promotedPiece][targetSquare]; } + + if (isMajor(promotedPiece)) { + position->majorKey ^= pieceKeys[promotedPiece][targetSquare]; + + } } @@ -351,6 +376,8 @@ int makeMove(int move, int moveFlag, board* position) { position->hashKey ^= pieceKeys[R][f1]; // put rook on f1 into a hash key position->whiteNonPawnKey ^= pieceKeys[R][h1]; position->whiteNonPawnKey ^= pieceKeys[R][f1]; + position->majorKey ^= pieceKeys[R][h1]; + position->majorKey ^= pieceKeys[R][f1]; break; // white castles queen side @@ -366,6 +393,8 @@ int makeMove(int move, int moveFlag, board* position) { position->hashKey ^= pieceKeys[R][d1]; // put rook on d1 into a hash key position->whiteNonPawnKey ^= pieceKeys[R][a1]; position->whiteNonPawnKey ^= pieceKeys[R][d1]; + position->majorKey ^= pieceKeys[R][a1]; + position->majorKey ^= pieceKeys[R][d1]; break; // black castles king side @@ -381,6 +410,8 @@ int makeMove(int move, int moveFlag, board* position) { position->hashKey ^= pieceKeys[r][f8]; // put rook on f8 into a hash key position->blackNonPawnKey ^= pieceKeys[r][h8]; position->blackNonPawnKey ^= pieceKeys[r][f8]; + position->majorKey ^= pieceKeys[r][h8]; + position->majorKey ^= pieceKeys[r][f8]; break; // black castles queen side @@ -396,6 +427,8 @@ int makeMove(int move, int moveFlag, board* position) { position->hashKey ^= pieceKeys[r][d8]; // put rook on d8 into a hash key position->blackNonPawnKey ^= pieceKeys[r][a8]; position->blackNonPawnKey ^= pieceKeys[r][d8]; + position->majorKey ^= pieceKeys[r][a8]; + position->majorKey ^= pieceKeys[r][d8]; break; } } @@ -442,7 +475,7 @@ int makeMove(int move, int moveFlag, board* position) { takeBack(position, ©Position); // return illegal move return 0; - } + } return 1; } diff --git a/src/search.c b/src/search.c index ac5e1b0a..1ce989e8 100644 --- a/src/search.c +++ b/src/search.c @@ -109,7 +109,8 @@ int PAWN_CORRECTION_HISTORY[2][16384]; int MINOR_CORRECTION_HISTORY[2][16384]; - int NON_PAWN_CORRECTION_HISTORY[2][2][16384]; + int MAJOR_CORRECTION_HISTORY[2][16384]; + int NON_PAWN_CORRECTION_HISTORY[2][2][16384]; /*╔═══════════════════════════════╗ @@ -431,6 +432,20 @@ void updateMinorCorrectionHistory(board *position, const int depth, const int di MINOR_CORRECTION_HISTORY[position->side][minorKey % CORRHIST_SIZE] = entry; } +void updateMajorCorrectionHistory(board *position, const int depth, const int diff) { + U64 majorKey = generateMajorKey(position); + + int entry = MAJOR_CORRECTION_HISTORY[position->side][majorKey % CORRHIST_SIZE]; + + const int scaledDiff = diff * CORRHIST_GRAIN; + const int newWeight = 2 * myMIN(depth + 1, 16); + + entry = (entry * (CORRHIST_WEIGHT_SCALE - newWeight) + scaledDiff * newWeight) / CORRHIST_WEIGHT_SCALE; + entry = clamp(entry, -CORRHIST_MAX, CORRHIST_MAX); + + MAJOR_CORRECTION_HISTORY[position->side][majorKey % CORRHIST_SIZE] = entry; +} + void update_non_pawn_corrhist(board *position, const int depth, const int diff) { U64 whiteKey = position->whiteNonPawnKey; U64 blackKey = position->blackNonPawnKey; @@ -455,9 +470,11 @@ void update_non_pawn_corrhist(board *position, const int depth, const int diff) int adjustEvalWithCorrectionHistory(board *position, const int rawEval) { U64 pawnKey = position->pawnKey; U64 minorKey = position->minorKey; + U64 majorKey = position->majorKey; int pawnEntry = PAWN_CORRECTION_HISTORY[position->side][pawnKey % CORRHIST_SIZE]; int minorEntry = MINOR_CORRECTION_HISTORY[position->side][minorKey % CORRHIST_SIZE]; + int majorEntry = MAJOR_CORRECTION_HISTORY[position->side][majorKey % CORRHIST_SIZE]; U64 whiteNPKey = position->whiteNonPawnKey; int whiteNPEntry = NON_PAWN_CORRECTION_HISTORY[white][position->side][whiteNPKey % CORRHIST_SIZE]; @@ -467,7 +484,7 @@ int adjustEvalWithCorrectionHistory(board *position, const int rawEval) { int mateFound = mateValue - maxPly; - int adjust = pawnEntry + minorEntry + whiteNPEntry + blackNPEntry; + int adjust = pawnEntry + minorEntry + majorEntry + whiteNPEntry + blackNPEntry; return clamp(rawEval + adjust / CORRHIST_GRAIN, -mateFound + 1, mateFound - 1); } @@ -1406,6 +1423,7 @@ int negamax(int alpha, int beta, int depth, board* pos, time* time, bool cutNode int corrhistBonus = clamp(bestScore - static_eval, -CORRHIST_LIMIT, CORRHIST_LIMIT); updatePawnCorrectionHistory(pos, depth, corrhistBonus); updateMinorCorrectionHistory(pos, depth, corrhistBonus); + updateMajorCorrectionHistory(pos, depth, corrhistBonus); update_non_pawn_corrhist(pos, depth, corrhistBonus); } diff --git a/src/search.h b/src/search.h index 08c14ecc..fa07140a 100644 --- a/src/search.h +++ b/src/search.h @@ -33,6 +33,7 @@ extern int counterMoves[2][maxPly][maxPly]; extern int PAWN_CORRECTION_HISTORY[2][16384]; extern int MINOR_CORRECTION_HISTORY[2][16384]; extern int NON_PAWN_CORRECTION_HISTORY[2][2][16384]; +extern int MAJOR_CORRECTION_HISTORY[2][16384]; int isRepetition(board* position); uint8_t isMaterialDraw(board *pos); @@ -48,6 +49,9 @@ int SEE(board *pos, int move, int threshold); uint64_t all_attackers_to_square(board *pos, uint64_t occupied, int sq); void updatePawnCorrectionHistory(board *position, const int depth, const int diff); void updateMinorCorrectionHistory(board *position, const int depth, const int diff); +void updateMajorCorrectionHistory(board *position, const int depth, const int diff); +int adjustEvalWithCorrectionHistory(board *position, const int rawEval); +void update_non_pawn_corrhist(board *position, const int depth, const int diff); int quiescence(int alpha, int beta, board* position, time* time); void quiescence_sort_moves(moves *moveList, board* position); int quiescenceScoreMove(int move, board* position); diff --git a/src/structs.h b/src/structs.h index 28c42a93..39da7873 100644 --- a/src/structs.h +++ b/src/structs.h @@ -50,6 +50,7 @@ typedef struct { U64 hashKey; U64 pawnKey; U64 minorKey; + U64 majorKey; U64 whiteNonPawnKey; U64 blackNonPawnKey; @@ -85,6 +86,7 @@ struct copyposition { U64 hashKeyCopy; U64 pawnKeyCopy; U64 minorKeyCopy; + U64 majorKeyCopy; U64 whiteNonPawnKeyCopy; U64 blackNonPawnKeyCopy; diff --git a/src/table.c b/src/table.c index 63449c73..d65b316e 100644 --- a/src/table.c +++ b/src/table.c @@ -114,6 +114,28 @@ U64 generateMinorKey(board *position) { return final_key; } +U64 generateMajorKey(board *position) { + uint64_t final_key = 0ULL; + uint64_t bitboard; + + + for (int i = 0; i < 4; i++) { + + int piece = majorPieces[i]; + bitboard = position->bitboards[piece]; + + while (bitboard) { + + int square = getLS1BIndex(bitboard); + + final_key ^= pieceKeys[piece][square]; + popBit(bitboard, square); + } + } + + return final_key; +} + // generates white non pawn hashing key U64 generate_white_np_hash_key(board *position) { uint64_t final_key = 0ULL; diff --git a/src/table.h b/src/table.h index cdcdc85c..af413331 100644 --- a/src/table.h +++ b/src/table.h @@ -38,6 +38,7 @@ int readHashEntry(board *position, int *move, int16_t *tt_score, uint8_t *tt_depth, uint8_t *tt_flag, bool *tt_pv); U64 generatePawnKey(board* position); U64 generateMinorKey(board *position); +U64 generateMajorKey(board *position); U64 generate_white_np_hash_key(board *position); U64 generate_black_np_hash_key(board *position); void clearHashTable(void); diff --git a/src/uci.c b/src/uci.c index 6aa75772..b0681c0f 100644 --- a/src/uci.c +++ b/src/uci.c @@ -469,6 +469,7 @@ void uciProtocol(int argc, char *argv[], board *position, time *time_ctrl) { memset(PAWN_CORRECTION_HISTORY, 0, sizeof(PAWN_CORRECTION_HISTORY)); memset(continuationHistory, 0, sizeof(continuationHistory)); memset(MINOR_CORRECTION_HISTORY, 0, sizeof(PAWN_CORRECTION_HISTORY)); + memset(MAJOR_CORRECTION_HISTORY, 0, sizeof(MAJOR_CORRECTION_HISTORY)); memset(NON_PAWN_CORRECTION_HISTORY, 0, sizeof(NON_PAWN_CORRECTION_HISTORY)); // call parse position function