// Berserk is a UCI compliant chess engine written in C // Copyright (C) 2026 Jay Honnold // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . #include "eval.h" #include #include #include "bits.h" #include "board.h" #include "move.h" #include "nn/accumulator.h" #include "nn/evaluate.h" #include "options.h" #include "thread.h" #include "util.h" const int PHASE_VALUES[6] = {0, 3, 3, 5, 10, 0}; void SetContempt(Score* dest, Color stm) { Score contempt = CONTEMPT; dest[stm] = contempt; dest[stm ^ 1] = -contempt; } // Main evalution method Score Evaluate(Board* board, ThreadData* thread) { if (IsMaterialDraw(board)) return 0; Accumulator* acc = board->accumulators; for (Color c = WHITE; c <= BLACK; c++) { if (!acc->correct[c]) { if (CanEfficientlyUpdate(acc, c)) ApplyLazyUpdates(acc, board, c); else RefreshAccumulator(acc, board, c); } } const int bucket = OutputBucket(board); Score score = Propagate(acc, board->stm, bucket); // scaled based on phase [1, 1.5] score = (128 + board->phase) * score / 128; score += board->phase * thread->contempt[board->stm] / 64; return Min(EVAL_UNKNOWN - 1, Max(-EVAL_UNKNOWN + 1, score)); } void EvaluateTrace(Board* board) { // The UCI board has no guarantee of accumulator allocation // so we have to set that up here. board->accumulators = AlignedMalloc(sizeof(Accumulator), 64); ResetAccumulator(board->accumulators, board, WHITE); ResetAccumulator(board->accumulators, board, BLACK); int base = Propagate(board->accumulators, board->stm, OutputBucket(board)); base = board->stm == WHITE ? base : -base; int scaled = (128 + board->phase) * base / 128; printf("\nNNUE derived piece values:\n"); for (int r = 0; r < 8; r++) { printf("+-------+-------+-------+-------+-------+-------+-------+-------+\n"); printf("|"); for (int f = 0; f < 16; f++) { if (f == 8) printf("\n|"); Square sq = r * 8 + (f > 7 ? f - 8 : f); Piece pc = board->squares[sq]; if (pc == NO_PIECE) { printf(" |"); } else if (f < 8) { printf(" %c |", PIECE_TO_CHAR[pc]); } else if (TypeOf(pc) == KING) { printf(" |"); } else { // To calculate the piece value, we pop it // reset the accumulators and take a diff // threats read the piece bitboards, so it has to leave those too PopBit(board->pieces[pc], sq); PopBit(OccBB(board, pc & 1), sq); PopBit(OccBB(board, BOTH), sq); ResetAccumulator(board->accumulators, board, WHITE); ResetAccumulator(board->accumulators, board, BLACK); int new = Propagate(board->accumulators, board->stm, OutputBucket(board)); new = board->stm == WHITE ? new : -new; SetBit(board->pieces[pc], sq); SetBit(OccBB(board, pc & 1), sq); SetBit(OccBB(board, BOTH), sq); int diff = base - new; Score normalized = Normalize(diff); int v = abs(normalized); char buffer[6]; buffer[5] = '\0'; buffer[0] = diff > 0 ? '+' : diff < 0 ? '-' : ' '; if (v >= 1000) { buffer[1] = '0' + v / 1000; v %= 1000; buffer[2] = '0' + v / 100; v %= 100; buffer[3] = '.'; buffer[4] = '0' + v / 10; } else { buffer[1] = '0' + v / 100; v %= 100; buffer[2] = '.'; buffer[3] = '0' + v / 10; v %= 10; buffer[4] = '0' + v; } printf(" %s |", buffer); } } printf("\n"); } printf("+-------+-------+-------+-------+-------+-------+-------+-------+\n\n"); // the piece loop left the accumulators on a board missing a piece ResetAccumulator(board->accumulators, board, WHITE); ResetAccumulator(board->accumulators, board, BLACK); printf("+------------+------------+------------+\n"); printf("| Bucket | NNUE | Final |\n"); printf("+------------+------------+------------+\n"); for (int b = 0; b < N_OUTPUT_BUCKETS; b++) { int v = Propagate(board->accumulators, board->stm, b); v = board->stm == WHITE ? v : -v; double nnue = Normalize(v) / 100; double final = Normalize((128 + board->phase) * v / 128) / 100; printf("| %-9d | %c %5.2f | %c %5.2f |", b, nnue < 0 ? '-' : '+', fabs(nnue), final < 0 ? '-' : '+', fabs(final)); printf(b == OutputBucket(board) ? " <-- this bucket is used\n" : "\n"); } printf("+------------+------------+------------+\n\n"); printf(" NNUE Score: %dcp (white)\n", (int) Normalize(base)); printf("Final Score: %dcp (white)\n", (int) Normalize(scaled)); AlignedFree(board->accumulators); }