Skip to content

Commit

Permalink
Merge branch 'main' into shallow-again
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonnold committed Apr 12, 2024
2 parents eff4be3 + 9439b87 commit 23d4d39
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 187 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ dkms.conf
berserk
berserk-popcnt
.vscode
.idea
dist

*.log
Expand Down
7 changes: 3 additions & 4 deletions resources/update.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -uex

# Select a compiler, prioritize Clang
if hash clang 2>/dev/null; then
cc=clang
Expand All @@ -11,7 +13,6 @@ else
fi

# Clone Berserk
echo "Cloning Berserk"
git clone --depth 1 --branch main https://github.com/jhonnold/berserk.git
success=$?

Expand All @@ -23,8 +24,7 @@ else
fi;

cd berserk/src
echo "Compiling Berserk with make pgo CC=$cc"
make pgo CC=$cc
make build CC=$cc ARCH=native

if test -f berserk; then
echo "Compilation complete..."
Expand All @@ -34,7 +34,6 @@ else
fi

EXE=$PWD/berserk
echo "Setting EXE to $EXE"

$EXE bench 13 > bench.log 2 >&1
grep Results bench.log
79 changes: 33 additions & 46 deletions src/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void ParseFen(char* fen, Board* board) {
OccBB(BOTH) = OccBB(WHITE) | OccBB(BLACK);

SetSpecialPieces(board);
SetThreatsAndEasyCaptures(board);
SetThreats(board);

board->zobrist = Zobrist(board);
board->pawnZobrist = PawnZobrist(board);
Expand Down Expand Up @@ -266,58 +266,45 @@ inline void SetSpecialPieces(Board* board) {
}
}

inline void SetThreatsAndEasyCaptures(Board* board) {
inline void SetThreats(Board* board) {
// Invert these to be confusing.
const int stm = board->xstm;
const int xstm = board->stm;

board->threatened = board->easyCapture = 0;

// Take the enemy king off for through threats.
BitBoard occ = OccBB(BOTH) ^ PieceBB(KING, xstm);

BitBoard opponentPieces = OccBB(xstm) ^ PieceBB(PAWN, xstm);

BitBoard pawnAttacks = stm == WHITE ? ShiftNW(PieceBB(PAWN, WHITE)) | ShiftNE(PieceBB(PAWN, WHITE)) :
ShiftSW(PieceBB(PAWN, BLACK)) | ShiftSE(PieceBB(PAWN, BLACK));
board->threatened |= pawnAttacks;
board->easyCapture |= pawnAttacks & opponentPieces;

// remove minors
opponentPieces ^= PieceBB(KNIGHT, xstm) | PieceBB(BISHOP, xstm);

BitBoard knights = PieceBB(KNIGHT, stm);
while (knights) {
BitBoard atx = GetKnightAttacks(PopLSB(&knights));

board->threatened |= atx;
board->easyCapture |= opponentPieces & atx;
}

BitBoard bishops = PieceBB(BISHOP, stm);
while (bishops) {
BitBoard atx = GetBishopAttacks(PopLSB(&bishops), occ);

board->threatened |= atx;
board->easyCapture |= opponentPieces & atx;
}

// remove rooks
opponentPieces ^= PieceBB(ROOK, xstm);

BitBoard rooks = PieceBB(ROOK, stm);
while (rooks) {
BitBoard atx = GetRookAttacks(PopLSB(&rooks), occ);

board->threatened |= atx;
board->easyCapture |= opponentPieces & atx;
}

BitBoard queens = PieceBB(QUEEN, stm);
BitBoard pawnAttacks = stm == WHITE ? ShiftNW(PieceBB(PAWN, WHITE)) | ShiftNE(PieceBB(PAWN, WHITE)) :
ShiftSW(PieceBB(PAWN, BLACK)) | ShiftSE(PieceBB(PAWN, BLACK));
board->threatenedBy[PAWN] = pawnAttacks;
board->threatened = board->threatenedBy[PAWN];

board->threatenedBy[KNIGHT] = 0;
BitBoard knights = PieceBB(KNIGHT, stm);
while (knights)
board->threatenedBy[KNIGHT] |= GetKnightAttacks(PopLSB(&knights));
board->threatened |= board->threatenedBy[KNIGHT];

board->threatenedBy[BISHOP] = 0;
BitBoard bishops = PieceBB(BISHOP, stm);
while (bishops)
board->threatenedBy[BISHOP] |= GetBishopAttacks(PopLSB(&bishops), occ);
board->threatened |= board->threatenedBy[BISHOP];

board->threatenedBy[ROOK] = 0;
BitBoard rooks = PieceBB(ROOK, stm);
while (rooks)
board->threatenedBy[ROOK] |= GetRookAttacks(PopLSB(&rooks), occ);
board->threatened |= board->threatenedBy[ROOK];

board->threatenedBy[QUEEN] = 0;
BitBoard queens = PieceBB(QUEEN, stm);
while (queens)
board->threatened |= GetQueenAttacks(PopLSB(&queens), occ);
board->threatenedBy[QUEEN] |= GetQueenAttacks(PopLSB(&queens), occ);
board->threatened |= board->threatenedBy[QUEEN];

board->threatened |= GetKingAttacks(LSB(PieceBB(KING, stm)));
board->threatenedBy[KING] = GetKingAttacks(LSB(PieceBB(KING, stm)));
board->threatened |= board->threatenedBy[KING];
}

void MakeMove(Move move, Board* board) {
Expand Down Expand Up @@ -427,7 +414,7 @@ void MakeMoveUpdate(Move move, Board* board, int update) {
// special pieces must be loaded after the stm has changed
// this is because the new stm to move will be the one in check
SetSpecialPieces(board);
SetThreatsAndEasyCaptures(board);
SetThreats(board);

if (update) {
board->accumulators->move = move;
Expand Down Expand Up @@ -513,7 +500,7 @@ void MakeNullMove(Board* board) {
board->xstm ^= 1;

SetSpecialPieces(board);
SetThreatsAndEasyCaptures(board);
SetThreats(board);
}

void UndoNullMove(Board* board) {
Expand Down
17 changes: 14 additions & 3 deletions src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#ifndef BOARD_H
#define BOARD_H

#include <math.h>

#include "types.h"
#include "util.h"

Expand All @@ -45,7 +43,7 @@ void BoardToFen(char* fen, Board* board);
void PrintBoard(Board* board);

void SetSpecialPieces(Board* board);
void SetThreatsAndEasyCaptures(Board* board);
void SetThreats(Board* board);

int DoesMoveCheck(Move move, Board* board);

Expand All @@ -66,6 +64,19 @@ int IsLegal(Move move, Board* board);
void InitCuckoo();
int HasCycle(Board* board, int ply);

INLINE BitBoard OpponentsEasyCaptures(Board* board) {
const int stm = board->stm;
const BitBoard queens = PieceBB(QUEEN, stm);
const BitBoard rooks = queens | PieceBB(ROOK, stm);
const BitBoard minors = rooks | PieceBB(BISHOP, stm) | PieceBB(KNIGHT, stm);

const BitBoard pawnThreats = board->threatenedBy[PAWN];
const BitBoard minorThreats = pawnThreats | board->threatenedBy[KNIGHT] | board->threatenedBy[BISHOP];
const BitBoard rookThreats = minorThreats | board->threatenedBy[ROOK];

return (queens & rookThreats) | (rooks & minorThreats) | (minors & pawnThreats);
}

INLINE int HasNonPawn(Board* board, const int color) {
return !!(OccBB(color) ^ PieceBB(KING, color) ^ PieceBB(PAWN, color));
}
Expand Down
4 changes: 1 addition & 3 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ Score Evaluate(Board* board, ThreadData* thread) {

int score = board->stm == WHITE ? Propagate(acc, WHITE) : Propagate(acc, BLACK);

// static contempt
score += thread->contempt[board->stm];

// 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));
}
Expand Down
2 changes: 1 addition & 1 deletion src/history.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void UpdateHistories(SearchStack* ss,
// Only increase the best move history when it
// wasn't trivial. This idea was first thought of
// by Alayan in Ethereal
if (nQ > 1 || depth > 3) {
if (nQ > 1 || depth > 4) {
AddHistoryHeuristic(&HH(stm, bestMove, board->threatened), inc);
UpdateCH(ss, bestMove, inc);
}
Expand Down
2 changes: 1 addition & 1 deletion src/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ INLINE void AddCounterMove(ThreadData* thread, Move move, Move parent) {
}

INLINE int16_t HistoryBonus(int depth) {
return Min(1896, 4 * depth * depth + 120 * depth - 120);
return Min(1729, 4 * depth * depth + 164 * depth - 113);
}

INLINE void AddHistoryHeuristic(int16_t* entry, int16_t inc) {
Expand Down
35 changes: 22 additions & 13 deletions src/makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
# General
EXE = berserk
SRC = *.c nn/*.c pyrrhic/tbprobe.c
CC = gcc
VERSION = 20240201
MAIN_NETWORK = berserk-da85741a5582.nn
SRC = attacks.c bench.c berserk.c bits.c board.c eval.c history.c move.c movegen.c movepick.c perft.c random.c \
search.c see.c tb.c thread.c transposition.c uci.c util.c zobrist.c nn/accumulator.c nn/evaluate.c pyrrhic/tbprobe.c
CC = clang
VERSION = 13
MAIN_NETWORK = berserk-d43206fe90e4.nn
EVALFILE = $(MAIN_NETWORK)
DEFS = -DVERSION=\"$(VERSION)\" -DEVALFILE=\"$(EVALFILE)\" -DNDEBUG

# Flags
STD = -std=gnu11
LIBS = -pthread -lm
WARN = -Wall -Wextra -Wshadow -Werror
WARN = -Wall -Wextra -Wshadow

FLAGS = $(STD) $(WARN) -g -O3 -flto $(PGOFLAGS) $(DEFS)
CFLAGS = $(FLAGS) -m64 -mpopcnt -msse -msse2
FLAGS = $(STD) $(WARN) -g -O3 -flto $(PGOFLAGS) $(DEFS)
M64 = -m64 -mpopcnt
MSSE2 = $(M64) -msse -msse2
MSSSE3 = $(MSSE2) -mssse3
MAVX2 = $(MSSSE3) -msse4.1 -mbmi -mfma -mavx2
MAVX512 = $(MAVX2) -mavx512f -mavx512bw

# Detecting windows
ifeq ($(shell echo "test"), "test")
CFLAGS += -static
FLAGS += -static
endif

# Detecting Apple Silicon (ARM64)
Expand All @@ -32,15 +37,19 @@ ifeq ($(ARCH), )
endif

ifeq ($(ARCH), native)
CFLAGS = $(FLAGS) -march=native
CFLAGS = $(FLAGS) -march=native
else ifeq ($(ARCH), arm64)
CFLAGS = $(FLAGS) -Wno-unused-function -arch arm64
CFLAGS = $(FLAGS) -arch arm64
else ifeq ($(findstring x86-64, $(ARCH)), x86-64)
CFLAGS = $(FLAGS) $(M64)
else ifeq ($(findstring sse2, $(ARCH)), sse2)
CFLAGS = $(FLAGS) $(MSSE2)
else ifeq ($(findstring ssse3, $(ARCH)), ssse3)
CFLAGS += -mssse3
CFLAGS = $(FLAGS) $(MSSSE3)
else ifeq ($(findstring avx2, $(ARCH)), avx2)
CFLAGS += -mssse3 -msse4.1 -mbmi -mfma -mavx2
CFLAGS = $(FLAGS) $(MAVX2)
else ifeq ($(findstring avx512, $(ARCH)), avx512)
CFLAGS += -mssse3 -msse4.1 -mbmi -mfma -mavx2 -mavx512f -mavx512bw
CFLAGS = $(FLAGS) $(MAVX512)
endif

ifeq ($(ARCH), native)
Expand Down
Loading

0 comments on commit 23d4d39

Please sign in to comment.