Skip to content

Commit

Permalink
Merge branch 'main' into 20bit-move
Browse files Browse the repository at this point in the history
Bench: 3680773
  • Loading branch information
jhonnold committed Oct 5, 2023
2 parents 16e2618 + 05769c3 commit f852fdc
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 233 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Below is a list of many chess engine lists throughout the web (*variance in Elo

- [TCEC](https://tcec-chess.com/)
- [CCC](https://www.chess.com/computer-chess-championship)
- [Graham's Broadcasts](https://tlcv.net)
- [Graham's Broadcasts](https://ccrl.live/)

## Funcational Details

Expand Down Expand Up @@ -58,8 +58,6 @@ Below is a list of many chess engine lists throughout the web (*variance in Elo
- [Extensions](https://www.chessprogramming.org/Extensions)
- [Singular](https://www.chessprogramming.org/Singular_Extensions)
- [Check](https://www.chessprogramming.org/Check_Extensions)
- [Recapture](https://www.chessprogramming.org/Recapture_Extensions)
- History

### Evaluation

Expand Down
Binary file removed src/bitbases/KBPvK.bb
Binary file not shown.
Binary file removed src/bitbases/KPvK.bb
Binary file not shown.
194 changes: 1 addition & 193 deletions src/endgame.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,165 +16,10 @@

#include "endgame.h"

#include <stdlib.h>

#include "bits.h"
#include "board.h"
#include "search.h"
#include "see.h"
#include "types.h"
#include "util.h"

#define INCBIN_PREFIX
#define INCBIN_STYLE INCBIN_STYLE_CAMEL
#include "incbin.h"

INCBIN(KPK, "bitbases/KPvK.bb");
INCBIN(KBPK, "bitbases/KBPvK.bb");

INLINE int PushTogether(int sq1, int sq2) {
return 70 - Distance(sq1, sq2);
}

INLINE int PushToEdge(int sq) {
int rankDistance = Min(Rank(sq), 7 - Rank(sq));
int fileDistance = Min(File(sq), 7 - File(sq));

return 20 - (rankDistance * rankDistance) - (fileDistance * fileDistance);
}

INLINE int MaterialValue(Board* board, const int side) {
int staticScore = 0;
for (int piece = PAWN; piece <= QUEEN; piece++)
staticScore += BitCount(PieceBB(piece, side)) * SEE_VALUE[piece];

return staticScore;
}

// The following KPK code is modified for my use from Cheng (as is the dataset)
INLINE uint32_t KPKIndex(int winningKing, int losingKing, int pawn, int stm) {
int file = File(pawn);
int x = file > 3 ? 7 : 0;

winningKing ^= x;
losingKing ^= x;
pawn ^= x;
file ^= x;

uint32_t p = (((pawn & 0x38) - 8) >> 1) | file;

return (uint32_t) winningKing | ((uint32_t) losingKing << 6) | ((uint32_t) stm << 12) | ((uint32_t) p << 13);
}

INLINE uint8_t KPKDraw(int winningSide, int winningKing, int losingKing, int pawn, int stm) {
uint32_t x = (winningSide == WHITE) ? 0 : 56;
uint32_t idx = KPKIndex(winningKing ^ x, losingKing ^ x, pawn ^ x, winningSide ^ stm);

return (uint8_t) (KPKData[idx >> 3] & (1U << (idx & 7)));
}

INLINE int EvaluateKPK(Board* board, const int winningSide) {
const int losingSide = winningSide ^ 1;
int score = WINNING_ENDGAME + MaterialValue(board, winningSide);

int winningKing = LSB(PieceBB(KING, winningSide));
int losingKing = LSB(PieceBB(KING, losingSide));
int pawn = winningSide == WHITE ? LSB(PieceBB(PAWN, WHITE)) : MSB(PieceBB(PAWN, BLACK));

if (KPKDraw(winningSide, winningKing, losingKing, pawn, board->stm))
return 0;

score += winningSide == WHITE ? (7 - Rank(pawn)) : Rank(pawn);

return winningSide == board->stm ? score : -score;
}

INLINE int EvaluateKXK(Board* board, const int winningSide) {
const int losingSide = winningSide ^ 1;
int score = WINNING_ENDGAME + MaterialValue(board, winningSide);

int winningKing = LSB(PieceBB(KING, winningSide));
int losingKing = LSB(PieceBB(KING, losingSide));

score += PushTogether(winningKing, losingKing) + PushToEdge(losingKing);

score = winningSide == board->stm ? score : -score;
return Min(TB_WIN_BOUND - 1, Max(-TB_WIN_BOUND + 1, score));
}

INLINE int EvaluateKBNK(Board* board, const int winningSide) {
const int losingSide = winningSide ^ 1;
int score = WINNING_ENDGAME + MaterialValue(board, winningSide);

int winningKing = LSB(PieceBB(KING, winningSide));
int losingKing = LSB(PieceBB(KING, losingSide));

score += PushTogether(winningKing, losingKing);

if (DARK_SQS & PieceBB(BISHOP, winningSide)) {
score += 50 * (7 - Min(MDistance(losingKing, A1), MDistance(losingKing, H8)));
} else {
score += 50 * (7 - Min(MDistance(losingKing, A8), MDistance(losingKing, H1)));
}

score = winningSide == board->stm ? score : -score;
return Min(TB_WIN_BOUND - 1, Max(-TB_WIN_BOUND + 1, score));
}

INLINE uint32_t KBPKIndex(int wking, int lking, int bishop, int pawn, int stm) {
int file = File(pawn);
int x = file > 3 ? 7 : 0;

wking ^= x, lking ^= x, bishop ^= x, pawn ^= x;

uint32_t p = (pawn >> 3) - 1;
uint32_t b = bishop >> 1;

return (uint32_t) wking | ((uint32_t) lking << 6) | (b << 12) | ((uint32_t) stm << 17) | (p << 18);
}

INLINE uint8_t KBPKDraw(int winningSide, int winningKing, int losingKing, int bishop, int pawn, int stm) {
int x = winningSide == WHITE ? 0 : 56;
uint32_t idx = KBPKIndex(winningKing ^ x, losingKing ^ x, bishop ^ x, pawn ^ x, winningSide ^ stm);

return !(uint8_t) (KBPKData[idx >> 3] & (1U << (idx & 7)));
}

INLINE int EvaluateKBPK(Board* board, const int winningSide) {
const int losingSide = winningSide ^ 1;

int pawn = winningSide == WHITE ? LSB(PieceBB(PAWN, WHITE)) : MSB(PieceBB(PAWN, BLACK));
int promotionSq = winningSide == WHITE ? File(pawn) : A1 + File(pawn);
int darkPromoSq = !!GetBit(DARK_SQS, promotionSq);
int darkBishop = !!(DARK_SQS & PieceBB(BISHOP, winningSide));

uint8_t files = PawnFiles(PieceBB(PAWN, winningSide));

// "Winning" if not a rook pawn or have right bishop
if ((files != 0x01 && files != 0x80) || darkPromoSq == darkBishop) {
int score = WINNING_ENDGAME + MaterialValue(board, winningSide);

score += winningSide == WHITE ? (7 - Rank(pawn)) : Rank(pawn);
score = winningSide == board->stm ? score : -score;

return Min(TB_WIN_BOUND - 1, Max(-TB_WIN_BOUND + 1, score));
}

// Utilize bitbase for everything else
int winningKing = LSB(PieceBB(KING, winningSide));
int losingKing = LSB(PieceBB(KING, losingSide));
int bishop = LSB(PieceBB(BISHOP, winningSide));

if (KBPKDraw(winningSide, winningKing, losingKing, bishop, pawn, board->stm))
return 0;

int score = WINNING_ENDGAME + MaterialValue(board, winningSide);

score += winningSide == WHITE ? (7 - Rank(pawn)) : Rank(pawn);
score = winningSide == board->stm ? score : -score;

return Min(TB_WIN_BOUND - 1, Max(-TB_WIN_BOUND + 1, score));
}

int EvaluateKnownPositions(Board* board) {
switch (board->piecesCounts) {
// See IsMaterialDraw
Expand All @@ -190,44 +35,7 @@ int EvaluateKnownPositions(Board* board) {
case 0x100100: // KNkb
case 0x110000: // KBkb
return 0;
// case 0x1: // KPk
// return EvaluateKPK(board, WHITE);
// case 0x10: // Kkp
// return EvaluateKPK(board, BLACK);
// case 0x10100: // KBNk
// return EvaluateKBNK(board, WHITE);
// case 0x101000: // Kkbn
// return EvaluateKBNK(board, BLACK);
default: break;
}

// if (BitCount(OccBB(BLACK)) == 1) {
// // KBPK
// if ((board->piecesCounts ^ 0x10000) <= 0xF)
// return EvaluateKBPK(board, WHITE);

// // stacked rook pawns
// if (board->piecesCounts <= 0xF) {
// uint8_t files = PawnFiles(PieceBB(PAWN, WHITE));
// if (files == 0x1 || files == 0x80)
// return EvaluateKPK(board, WHITE);
// }

// return EvaluateKXK(board, WHITE);
// } else if (BitCount(OccBB(WHITE)) == 1) {
// // Kkbp
// if ((board->piecesCounts ^ 0x100000) <= 0xF0)
// return EvaluateKBPK(board, BLACK);

// // stacked rook pawns
// if (board->piecesCounts <= 0xF0) {
// uint8_t files = PawnFiles(PieceBB(PAWN, BLACK));
// if (files == 0x1 || files == 0x80)
// return EvaluateKPK(board, BLACK);
// }

// return EvaluateKXK(board, BLACK);
// }

return UNKNOWN;
}
2 changes: 0 additions & 2 deletions src/endgame.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#include "types.h"
#include "util.h"

#define WINNING_ENDGAME 25000

int EvaluateKnownPositions(Board* board);

#endif
10 changes: 6 additions & 4 deletions src/history.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ void UpdateHistories(SearchStack* ss,
int nQ,
Move captures[],
int nC) {
Board* board = &thread->board;
int stm = board->stm;
Board* board = &thread->board;
int stm = board->stm;

int16_t inc = Min(1896, 4 * depth * depth + 120 * depth - 120);

Expand All @@ -76,9 +76,10 @@ void UpdateHistories(SearchStack* ss,
} else {
int piece = Moving(bestMove);
int to = To(bestMove);
int defended = !GetBit(board->threatened, to);
int captured = IsEP(bestMove) ? PAWN : PieceType(board->squares[to]);

AddHistoryHeuristic(&TH(piece, to, captured), inc);
AddHistoryHeuristic(&TH(piece, to, defended, captured), inc);
}

// Update quiets
Expand All @@ -101,8 +102,9 @@ void UpdateHistories(SearchStack* ss,

int piece = Moving(m);
int to = To(m);
int defended = !GetBit(board->threatened, to);
int captured = IsEP(m) ? PAWN : PieceType(board->squares[to]);

AddHistoryHeuristic(&TH(piece, to, captured), -inc);
AddHistoryHeuristic(&TH(piece, to, defended, captured), -inc);
}
}
11 changes: 7 additions & 4 deletions src/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@
#include "util.h"

#define HH(stm, m, threats) (thread->hh[stm][!GetBit(threats, From(m))][!GetBit(threats, To(m))][FromTo(m)])
#define TH(p, e, c) (thread->caph[p][e][c])
#define TH(p, e, d, c) (thread->caph[p][e][d][c])

INLINE int GetQuietHistory(SearchStack* ss, ThreadData* thread, Move move) {
return (int) HH(thread->board.stm, move, thread->board.threatened) + //
(int) (*(ss - 1)->ch)[Moving(move)][To(move)] + //
(int) (*(ss - 2)->ch)[Moving(move)][To(move)] + //
(int) (*(ss - 1)->ch)[Moving(move)][To(move)] + //
(int) (*(ss - 2)->ch)[Moving(move)][To(move)] + //
(int) (*(ss - 4)->ch)[Moving(move)][To(move)];
}

INLINE int GetCaptureHistory(ThreadData* thread, Move move) {
Board* board = &thread->board;

return TH(Moving(move), To(move), IsEP(move) ? PAWN : PieceType(board->squares[To(move)]));
return TH(Moving(move),
To(move),
!GetBit(board->threatened, To(move)),
IsEP(move) ? PAWN : PieceType(board->squares[To(move)]));
}

INLINE int GetHistory(SearchStack* ss, ThreadData* thread, Move move) {
Expand Down
4 changes: 2 additions & 2 deletions src/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
EXE = berserk
SRC = *.c nn/*.c pyrrhic/tbprobe.c
CC = gcc
VERSION = 20230911
MAIN_NETWORK = networks/berserk-71bdca2676c4.nn
VERSION = 20230927
MAIN_NETWORK = networks/berserk-d1b801c65262.nn
EVALFILE = $(MAIN_NETWORK)
DEFS = -DVERSION=\"$(VERSION)\" -DEVALFILE=\"$(EVALFILE)\" -DNDEBUG

Expand Down
6 changes: 0 additions & 6 deletions src/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,3 @@ char* MoveToStr(Move move, Board* board) {

return buffer;
}

inline int IsRecapture(SearchStack* ss, Move move) {
return IsCap(move) && //
((IsCap((ss - 1)->move) && To((ss - 1)->move) == To(move)) || //
(IsCap((ss - 3)->move) && To((ss - 3)->move) == To(move)));
}
1 change: 0 additions & 1 deletion src/move.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,5 @@ extern const int CASTLING_ROOK[64];

Move ParseMove(char* moveStr, Board* board);
char* MoveToStr(Move move, Board* board);
int IsRecapture(SearchStack* ss, Move move);

#endif
2 changes: 1 addition & 1 deletion src/networks
Loading

0 comments on commit f852fdc

Please sign in to comment.