From bd73367c88d521e1f7649aa33b901895a6563106 Mon Sep 17 00:00:00 2001 From: lucasart Date: Fri, 9 Jul 2021 10:08:30 +0800 Subject: [PATCH] Automate code formatting --- .clang-format | 4 + src/bitboard.c | 158 ++++++++++++---------------- src/bitboard.h | 5 +- src/eval.c | 95 ++++++++--------- src/eval.h | 2 +- src/gen.c | 51 ++++----- src/gen.h | 4 +- src/htable.c | 36 +++---- src/htable.h | 10 +- src/main.c | 27 +++-- src/platform.h | 39 +++---- src/position.c | 151 +++++++++++---------------- src/position.h | 32 +++--- src/pst.c | 10 +- src/pst.h | 2 +- src/search.c | 141 ++++++++++++------------- src/search.h | 13 +-- src/sort.c | 55 +++++----- src/sort.h | 2 +- src/tune.c | 278 ++++++++++++++++++++++++++----------------------- src/tune.h | 4 +- src/types.c | 56 ++++------ src/types.h | 102 ++++++++++++++---- src/uci.c | 85 ++++++--------- src/uci.h | 2 +- src/util.c | 16 ++- src/util.h | 2 +- src/workers.c | 21 ++-- src/workers.h | 14 +-- src/zobrist.c | 34 ++---- src/zobrist.h | 4 +- 31 files changed, 687 insertions(+), 768 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..29a4527 --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +BasedOnStyle: LLVM +ColumnLimit: '100' +IndentPPDirectives: BeforeHash +IndentWidth: 4 diff --git a/src/bitboard.c b/src/bitboard.c index 604140a..5e5c59c 100644 --- a/src/bitboard.c +++ b/src/bitboard.c @@ -11,7 +11,7 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #include "bitboard.h" #include "util.h" @@ -23,8 +23,7 @@ bitboard_t Rank[NB_RANK], File[NB_FILE]; bitboard_t PawnAttacks[NB_COLOR][NB_SQUARE], KnightAttacks[NB_SQUARE], KingAttacks[NB_SQUARE]; bitboard_t Segment[NB_SQUARE][NB_SQUARE], Ray[NB_SQUARE][NB_SQUARE]; -static void safe_set_bit(bitboard_t *b, int rank, int file) -{ +static void safe_set_bit(bitboard_t *b, int rank, int file) { if (0 <= rank && rank < NB_RANK && 0 <= file && file < NB_FILE) bb_set(b, square_from(rank, file)); } @@ -32,40 +31,38 @@ static void safe_set_bit(bitboard_t *b, int rank, int file) static const bitboard_t RookMagic[NB_SQUARE] = { 0x808000645080c000, 0x208020001480c000, 0x4180100160008048, 0x8180100018001680, 0x4200082010040201, 0x8300220400010008, 0x3100120000890004, 0x4080004500012180, - 0x1548000a1804008, 0x4881004005208900, 0x480802000801008, 0x2e8808010008800, - 0x8cd804800240080, 0x8a058002008c0080, 0x514000c480a1001, 0x101000282004d00, + 0x1548000a1804008, 0x4881004005208900, 0x480802000801008, 0x2e8808010008800, + 0x8cd804800240080, 0x8a058002008c0080, 0x514000c480a1001, 0x101000282004d00, 0x2048848000204000, 0x3020088020804000, 0x4806020020841240, 0x6080420008102202, - 0x10050011000800, 0xac00808004000200, 0x10100020004, 0x1500020004004581, - 0x4c00180052080, 0x220028480254000, 0x2101200580100080, 0x407201200084200, - 0x18004900100500, 0x100200020008e410, 0x81020400100811, 0x12200024494, - 0x8006c002808006a5, 0x4201000404000, 0x5402202001180, 0x81001002100, - 0x100801000500, 0x4000020080800400, 0x4005050214001008, 0x810100118b000042, - 0xd01020040820020, 0x140a010014000, 0x420001500210040, 0x54210010030009, - 0x4000408008080, 0x2000400090100, 0x840200010100, 0x233442820004, - 0x800a42002b008200, 0x240200040009080, 0x242001020408200, 0x4000801000480480, - 0x2288008044000880, 0xa800400020180, 0x30011002880c00, 0x41110880440200, - 0x2001100442082, 0x1a0104002208101, 0x80882014010200a, 0x100100600409, - 0x2011048204402, 0x12000168041002, 0x80100008a000421, 0x240022044031182 -}; + 0x10050011000800, 0xac00808004000200, 0x10100020004, 0x1500020004004581, + 0x4c00180052080, 0x220028480254000, 0x2101200580100080, 0x407201200084200, + 0x18004900100500, 0x100200020008e410, 0x81020400100811, 0x12200024494, + 0x8006c002808006a5, 0x4201000404000, 0x5402202001180, 0x81001002100, + 0x100801000500, 0x4000020080800400, 0x4005050214001008, 0x810100118b000042, + 0xd01020040820020, 0x140a010014000, 0x420001500210040, 0x54210010030009, + 0x4000408008080, 0x2000400090100, 0x840200010100, 0x233442820004, + 0x800a42002b008200, 0x240200040009080, 0x242001020408200, 0x4000801000480480, + 0x2288008044000880, 0xa800400020180, 0x30011002880c00, 0x41110880440200, + 0x2001100442082, 0x1a0104002208101, 0x80882014010200a, 0x100100600409, + 0x2011048204402, 0x12000168041002, 0x80100008a000421, 0x240022044031182}; static const bitboard_t BishopMagic[NB_SQUARE] = { - 0x88b030028800d040, 0x18242044c008010, 0x10008200440000, 0x4311040888800a00, - 0x1910400000410a, 0x2444240440000000, 0xcd2080108090008, 0x2048242410041004, - 0x8884441064080180, 0x42131420a0240, 0x28882800408400, 0x204384040b820200, - 0x402040420800020, 0x20910282304, 0x96004b10082200, 0x4000a44218410802, - 0x808034002081241, 0x101805210e1408, 0x9020400208010220, 0x820050c010044, - 0x24005480a00000, 0x200200900890, 0x808040049c100808, 0x9020202200820802, - 0x410282124200400, 0x90106008010110, 0x8001100501004201, 0x104080004030c10, - 0x80840040802008, 0x2008008102406000, 0x2000888004040460, 0xd0421242410410, - 0x8410100401280800, 0x801012000108428, 0x402080300b04, 0xc20020080480080, + 0x88b030028800d040, 0x18242044c008010, 0x10008200440000, 0x4311040888800a00, + 0x1910400000410a, 0x2444240440000000, 0xcd2080108090008, 0x2048242410041004, + 0x8884441064080180, 0x42131420a0240, 0x28882800408400, 0x204384040b820200, + 0x402040420800020, 0x20910282304, 0x96004b10082200, 0x4000a44218410802, + 0x808034002081241, 0x101805210e1408, 0x9020400208010220, 0x820050c010044, + 0x24005480a00000, 0x200200900890, 0x808040049c100808, 0x9020202200820802, + 0x410282124200400, 0x90106008010110, 0x8001100501004201, 0x104080004030c10, + 0x80840040802008, 0x2008008102406000, 0x2000888004040460, 0xd0421242410410, + 0x8410100401280800, 0x801012000108428, 0x402080300b04, 0xc20020080480080, 0x40100e0201502008, 0x4014208200448800, 0x4050020607084501, 0x1002820180020288, - 0x800610040540a0c0, 0x301009014081004, 0x2200610040502800, 0x300442011002800, - 0x1022009002208, 0x110011000202100, 0x1464082204080240, 0x21310205800200, - 0x814020210040109, 0xc102008208c200a0, 0xc100702128080000, 0x1044205040000, - 0x1041002020000, 0x4200040408021000, 0x4004040c494000, 0x2010108900408080, - 0x820801040284, 0x800004118111000, 0x203040201108800, 0x2504040804208803, - 0x228000908030400, 0x10402082020200, 0xa0402208010100, 0x30c0214202044104 -}; + 0x800610040540a0c0, 0x301009014081004, 0x2200610040502800, 0x300442011002800, + 0x1022009002208, 0x110011000202100, 0x1464082204080240, 0x21310205800200, + 0x814020210040109, 0xc102008208c200a0, 0xc100702128080000, 0x1044205040000, + 0x1041002020000, 0x4200040408021000, 0x4004040c494000, 0x2010108900408080, + 0x820801040284, 0x800004118111000, 0x203040201108800, 0x2504040804208803, + 0x228000908030400, 0x10402082020200, 0xa0402208010100, 0x30c0214202044104}; static bitboard_t RookDB[0x19000], BishopDB[0x1480]; static bitboard_t *BishopAttacks[NB_SQUARE], *RookAttacks[NB_SQUARE]; @@ -75,16 +72,14 @@ static unsigned BishopShift[NB_SQUARE], RookShift[NB_SQUARE]; // Compute (from scratch) the squares attacked by a sliding piece, moving in directions dir, given // board occupancy occ. -static bitboard_t slider_attacks(int square, bitboard_t occ, const int dir[4][2]) -{ +static bitboard_t slider_attacks(int square, bitboard_t occ, const int dir[4][2]) { bitboard_t result = 0; for (int i = 0; i < 4; i++) { int dr = dir[i][0], df = dir[i][1]; for (int rank = rank_of(square) + dr, file = file_of(square) + df; - 0 <= rank && rank < NB_RANK && 0 <= file && file < NB_FILE; - rank += dr, file += df) { + 0 <= rank && rank < NB_RANK && 0 <= file && file < NB_FILE; rank += dr, file += df) { const int sq = square_from(rank, file); bb_set(&result, sq); @@ -96,10 +91,9 @@ static bitboard_t slider_attacks(int square, bitboard_t occ, const int dir[4][2] return result; } -static int slider_index(bitboard_t occ, bitboard_t mask, bitboard_t magic, unsigned shift) -{ +static int slider_index(bitboard_t occ, bitboard_t mask, bitboard_t magic, unsigned shift) { #ifdef PEXT - (void)magic, (void)shift; // Silence compiler warnings (unused variables) + (void)magic, (void)shift; // Silence compiler warnings (unused variables) return _pext_u64(occ, mask); #else return ((occ & mask) * magic) >> shift; @@ -107,11 +101,10 @@ static int slider_index(bitboard_t occ, bitboard_t mask, bitboard_t magic, unsig } static void init_slider_attacks(int square, bitboard_t mask[NB_SQUARE], - const bitboard_t magic[NB_SQUARE], unsigned shift[NB_SQUARE], bitboard_t *attacksPtr[NB_SQUARE], - const int dir[4][2]) -{ + const bitboard_t magic[NB_SQUARE], unsigned shift[NB_SQUARE], + bitboard_t *attacksPtr[NB_SQUARE], const int dir[4][2]) { bitboard_t edges = ((Rank[RANK_1] | Rank[RANK_8]) & ~Rank[rank_of(square)]) | - ((File[RANK_1] | File[RANK_8]) & ~File[file_of(square)]); + ((File[RANK_1] | File[RANK_8]) & ~File[file_of(square)]); mask[square] = slider_attacks(square, 0, dir) & ~edges; shift[square] = (unsigned)(64 - bb_count(mask[square])); @@ -121,19 +114,20 @@ static void init_slider_attacks(int square, bitboard_t mask[NB_SQUARE], // Loop over the subsets of mask[square] bitboard_t occ = 0; do { - attacksPtr[square][slider_index(occ, mask[square], magic[square], shift[square])] - = slider_attacks(square, occ, dir); - occ = (occ - mask[square]) & mask[square]; // Carry-Rippler trick + attacksPtr[square][slider_index(occ, mask[square], magic[square], shift[square])] = + slider_attacks(square, occ, dir); + occ = (occ - mask[square]) & mask[square]; // Carry-Rippler trick } while (occ); } -static __attribute__((constructor)) void bb_init(void) -{ - static const int PawnDir[2][2] = {{1,-1}, {1,1}}; - static const int KnightDir[8][2] = {{-2,-1}, {-2,1}, {-1,-2}, {-1,2}, {1,-2}, {1,2}, {2,-1}, {2,1}}; - static const int KingDir[8][2] = {{-1,-1}, {-1,0}, {-1,1}, {0,-1}, {0,1}, {1,-1}, {1,0}, {1,1}}; - static const int BishopDir[4][2] = {{-1,-1}, {-1,1}, {1,-1}, {1,1}}; - static const int RookDir[4][2] = {{-1,0}, {0,-1}, {0,1}, {1,0}}; +static __attribute__((constructor)) void bb_init(void) { + static const int PawnDir[2][2] = {{1, -1}, {1, 1}}; + static const int KnightDir[8][2] = {{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, + {1, -2}, {1, 2}, {2, -1}, {2, 1}}; + static const int KingDir[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, + {0, 1}, {1, -1}, {1, 0}, {1, 1}}; + static const int BishopDir[4][2] = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}}; + static const int RookDir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; // Initialise Rank[] and File[] for (int i = 0; i < 8; i++) { @@ -147,8 +141,8 @@ static __attribute__((constructor)) void bb_init(void) bitboard_t mask = 0; for (int r = rank_of(square), f = file_of(square); - 0 <= r && r < NB_RANK && 0 <= f && f < NB_FILE; - r += KingDir[d][0], f += KingDir[d][1]) { + 0 <= r && r < NB_RANK && 0 <= f && f < NB_FILE; + r += KingDir[d][0], f += KingDir[d][1]) { const int s = square_from(r, f); bb_set(&mask, s); Segment[square][s] = mask; @@ -209,77 +203,61 @@ static __attribute__((constructor)) void bb_init(void) #endif } -bitboard_t bb_bishop_attacks(int square, bitboard_t occ) -{ +bitboard_t bb_bishop_attacks(int square, bitboard_t occ) { BOUNDS(square, NB_SQUARE); return BishopAttacks[square][slider_index(occ, BishopMask[square], BishopMagic[square], - BishopShift[square])]; + BishopShift[square])]; } -bitboard_t bb_rook_attacks(int square, bitboard_t occ) -{ +bitboard_t bb_rook_attacks(int square, bitboard_t occ) { BOUNDS(square, NB_SQUARE); - return RookAttacks[square][slider_index(occ, RookMask[square], RookMagic[square], - RookShift[square])]; + return RookAttacks[square] + [slider_index(occ, RookMask[square], RookMagic[square], RookShift[square])]; } -bool bb_test(bitboard_t b, int square) -{ +bool bb_test(bitboard_t b, int square) { BOUNDS(square, NB_SQUARE); return b & (1ULL << square); } -void bb_clear(bitboard_t *b, int square) -{ +void bb_clear(bitboard_t *b, int square) { BOUNDS(square, NB_SQUARE); assert(bb_test(*b, square)); *b ^= 1ULL << square; } -void bb_set(bitboard_t *b, int square) -{ +void bb_set(bitboard_t *b, int square) { BOUNDS(square, NB_SQUARE); assert(!bb_test(*b, square)); *b ^= 1ULL << square; } -bitboard_t bb_shift(bitboard_t b, int i) -{ - assert(-63 <= i && i <= 63); // oversized shift is undefined +bitboard_t bb_shift(bitboard_t b, int i) { + assert(-63 <= i && i <= 63); // oversized shift is undefined return i > 0 ? b << i : b >> -i; } -int bb_lsb(bitboard_t b) -{ - assert(b); // lsb(0) is undefined +int bb_lsb(bitboard_t b) { + assert(b); // lsb(0) is undefined return __builtin_ctzll(b); } -int bb_msb(bitboard_t b) -{ - assert(b); // msb(0) is undefined +int bb_msb(bitboard_t b) { + assert(b); // msb(0) is undefined return 63 - __builtin_clzll(b); } -int bb_pop_lsb(bitboard_t *b) -{ +int bb_pop_lsb(bitboard_t *b) { int square = bb_lsb(*b); *b &= *b - 1; return square; } -bool bb_several(bitboard_t b) -{ - return b & (b - 1); -} +bool bb_several(bitboard_t b) { return b & (b - 1); } -int bb_count(bitboard_t b) -{ - return __builtin_popcountll(b); -} +int bb_count(bitboard_t b) { return __builtin_popcountll(b); } -void bb_print(bitboard_t b) -{ +void bb_print(bitboard_t b) { for (int rank = RANK_8; rank >= RANK_1; rank--) { char line[] = ". . . . . . . ."; diff --git a/src/bitboard.h b/src/bitboard.h index a1779bc..c251ea3 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -11,14 +11,15 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once #include "types.h" typedef uint64_t bitboard_t; extern bitboard_t Rank[NB_RANK], File[NB_FILE]; -extern bitboard_t PawnAttacks[NB_COLOR][NB_SQUARE], KnightAttacks[NB_SQUARE], KingAttacks[NB_SQUARE]; +extern bitboard_t PawnAttacks[NB_COLOR][NB_SQUARE], KnightAttacks[NB_SQUARE], + KingAttacks[NB_SQUARE]; extern bitboard_t Segment[NB_SQUARE][NB_SQUARE]; extern bitboard_t Ray[NB_SQUARE][NB_SQUARE]; diff --git a/src/eval.c b/src/eval.c index aef5740..e5cade9 100644 --- a/src/eval.c +++ b/src/eval.c @@ -11,14 +11,14 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ -#include -#include -#include "bitboard.h" + */ #include "eval.h" +#include "bitboard.h" #include "position.h" #include "tune.h" #include "util.h" +#include +#include // Pre-calculated in eval_init() static int StartPieceTotal; @@ -28,15 +28,13 @@ static bitboard_t AdjacentFiles[NB_FILE]; static int KingDistance[NB_SQUARE][NB_SQUARE]; static int SafetyCurve[4096]; -static bitboard_t pawn_attacks(const Position *pos, int color) -{ +static bitboard_t pawn_attacks(const Position *pos, int color) { const bitboard_t pawns = pos_pieces_cp(pos, color, PAWN); - return bb_shift(pawns & ~File[FILE_A], push_inc(color) + LEFT) - | bb_shift(pawns & ~File[FILE_H], push_inc(color) + RIGHT); + return bb_shift(pawns & ~File[FILE_A], push_inc(color) + LEFT) | + bb_shift(pawns & ~File[FILE_H], push_inc(color) + RIGHT); } -static eval_t mobility(const Position *pos, int us, bitboard_t attacks[NB_COLOR][NB_PIECE + 1]) -{ +static eval_t mobility(const Position *pos, int us, bitboard_t attacks[NB_COLOR][NB_PIECE + 1]) { const int them = opposite(us); eval_t result = {0, 0}; @@ -53,7 +51,7 @@ static eval_t mobility(const Position *pos, int us, bitboard_t attacks[NB_COLOR] // Lateral mobility bitboard_t rookMovers = pos_pieces_cpp(pos, us, ROOK, QUEEN); - bitboard_t occ = pos_pieces(pos) ^ rookMovers; // RQ see through each other + bitboard_t occ = pos_pieces(pos) ^ rookMovers; // RQ see through each other while (rookMovers) { const int from = bb_pop_lsb(&rookMovers), piece = pos->pieceOn[from]; @@ -64,7 +62,7 @@ static eval_t mobility(const Position *pos, int us, bitboard_t attacks[NB_COLOR] // Diagonal mobility bitboard_t bishopMovers = pos_pieces_cpp(pos, us, BISHOP, QUEEN); - occ = pos_pieces(pos) ^ bishopMovers; // BQ see through each other + occ = pos_pieces(pos) ^ bishopMovers; // BQ see through each other while (bishopMovers) { const int from = bb_pop_lsb(&bishopMovers), piece = pos->pieceOn[from]; @@ -76,16 +74,15 @@ static eval_t mobility(const Position *pos, int us, bitboard_t attacks[NB_COLOR] return result; } -static eval_t pattern(const Position *pos, int us) -{ +static eval_t pattern(const Position *pos, int us) { const bitboard_t WhiteSquares = 0x55AA55AA55AA55AAULL; const bitboard_t ourPawns = pos_pieces_cp(pos, us, PAWN); const bitboard_t theirPawns = pos->byPiece[PAWN] ^ ourPawns; const bitboard_t ourBishops = pos_pieces_cp(pos, us, BISHOP); // Bishop pair - eval_t result = (ourBishops & WhiteSquares) && (ourBishops & ~WhiteSquares) - ? BishopPair : (eval_t){0, 0}; + eval_t result = + (ourBishops & WhiteSquares) && (ourBishops & ~WhiteSquares) ? BishopPair : (eval_t){0, 0}; // Rook on open file bitboard_t b = pos_pieces_cp(pos, us, ROOK); @@ -106,8 +103,7 @@ static eval_t pattern(const Position *pos, int us) return result; } -static eval_t hanging(const Position *pos, int us, bitboard_t attacks[NB_COLOR][NB_PIECE + 1]) -{ +static eval_t hanging(const Position *pos, int us, bitboard_t attacks[NB_COLOR][NB_PIECE + 1]) { const int them = opposite(us); eval_t result = {0, 0}; @@ -115,8 +111,8 @@ static eval_t hanging(const Position *pos, int us, bitboard_t attacks[NB_COLOR][ bitboard_t b = attacks[them][PAWN] & (pos->byColor[us] ^ pos_pieces_cp(pos, us, PAWN)); b |= (attacks[them][KNIGHT] | attacks[them][BISHOP]) & pos_pieces_cpp(pos, us, ROOK, QUEEN); b |= pos_pieces_cp(pos, us, QUEEN) & attacks[them][ROOK]; - b |= pos_pieces_cp(pos, us, PAWN) & attacks[them][NB_PIECE] - & ~(attacks[us][PAWN] | attacks[us][KING] | attacks[us][NB_PIECE]); + b |= pos_pieces_cp(pos, us, PAWN) & attacks[them][NB_PIECE] & + ~(attacks[us][PAWN] | attacks[us][KING] | attacks[us][NB_PIECE]); while (b) { const int piece = pos->pieceOn[bb_pop_lsb(&b)]; @@ -125,8 +121,8 @@ static eval_t hanging(const Position *pos, int us, bitboard_t attacks[NB_COLOR][ } // Penalize hanging pawns in the endgame - b = pos_pieces_cp(pos, us, PAWN) & attacks[them][KING] - & ~(attacks[us][PAWN] | attacks[us][KING]); + b = pos_pieces_cp(pos, us, PAWN) & attacks[them][KING] & + ~(attacks[us][PAWN] | attacks[us][KING]); if (b) result.eg -= Hanging[PAWN] * bb_count(b); @@ -134,8 +130,7 @@ static eval_t hanging(const Position *pos, int us, bitboard_t attacks[NB_COLOR][ return result; } -static int safety(const Position *pos, int us, bitboard_t attacks[NB_COLOR][NB_PIECE + 1]) -{ +static int safety(const Position *pos, int us, bitboard_t attacks[NB_COLOR][NB_PIECE + 1]) { const int them = opposite(us); int weight = 0, count = 0; @@ -155,17 +150,16 @@ static int safety(const Position *pos, int us, bitboard_t attacks[NB_COLOR][NB_P // Check threats const int king = pos_king_square(pos, us); const bitboard_t occ = pos_pieces(pos); - const bitboard_t checks[] = { - KnightAttacks[king] & attacks[them][KNIGHT], - bb_bishop_attacks(king, occ) & attacks[them][BISHOP], - bb_rook_attacks(king, occ) & attacks[them][ROOK], - (bb_bishop_attacks(king, occ) | bb_rook_attacks(king, occ)) & attacks[them][QUEEN] - }; + const bitboard_t checks[] = {KnightAttacks[king] & attacks[them][KNIGHT], + bb_bishop_attacks(king, occ) & attacks[them][BISHOP], + bb_rook_attacks(king, occ) & attacks[them][ROOK], + (bb_bishop_attacks(king, occ) | bb_rook_attacks(king, occ)) & + attacks[them][QUEEN]}; for (int piece = KNIGHT; piece <= QUEEN; piece++) if (checks[piece]) { - const bitboard_t b = checks[piece] & ~(pos->byColor[them] | attacks[us][PAWN] - | attacks[us][KING]); + const bitboard_t b = + checks[piece] & ~(pos->byColor[them] | attacks[us][PAWN] | attacks[us][KING]); if (b) { count++; @@ -175,8 +169,8 @@ static int safety(const Position *pos, int us, bitboard_t attacks[NB_COLOR][NB_P } // X-Ray threats: sliding pieces with potential for pins or discovered checks - bitboard_t xrays = (bb_bishop_attacks(king, 0) & pos_pieces_cpp(pos, them, BISHOP, QUEEN)) - | (bb_rook_attacks(king, 0) & pos_pieces_cpp(pos, them, ROOK, QUEEN)); + bitboard_t xrays = (bb_bishop_attacks(king, 0) & pos_pieces_cpp(pos, them, BISHOP, QUEEN)) | + (bb_rook_attacks(king, 0) & pos_pieces_cpp(pos, them, ROOK, QUEEN)); while (xrays) { const int xray = bb_pop_lsb(&xrays); @@ -191,8 +185,7 @@ static int safety(const Position *pos, int us, bitboard_t attacks[NB_COLOR][NB_P return -SafetyCurve[min(idx, 4095)]; } -static eval_t passer(int us, int pawn, int ourKing, int theirKing) -{ +static eval_t passer(int us, int pawn, int ourKing, int theirKing) { const int n = relative_rank_of(us, pawn) - RANK_2; // score based on rank @@ -209,8 +202,7 @@ static eval_t passer(int us, int pawn, int ourKing, int theirKing) } static eval_t do_pawns(const Position *pos, int us, bitboard_t attacks[NB_COLOR][NB_PIECE + 1], - bitboard_t *passed) -{ + bitboard_t *passed) { const int them = opposite(us); const bitboard_t ourPawns = pos_pieces_cp(pos, us, PAWN); const bitboard_t theirPawns = pos_pieces_cp(pos, them, PAWN); @@ -239,8 +231,8 @@ static eval_t do_pawns(const Position *pos, int us, bitboard_t attacks[NB_COLOR] const bitboard_t besides = ourPawns & AdjacentFiles[file]; const bool exposed = !(PawnPath[us][square] & pos->byPiece[PAWN]); - const int d = KingDistance[stop][theirKing] * Distance[1] - - KingDistance[stop][ourKing] * Distance[0]; + const int d = + KingDistance[stop][theirKing] * Distance[1] - KingDistance[stop][ourKing] * Distance[0]; maxDistance = max(maxDistance, d); if (besides & (Rank[rank] | Rank[us == WHITE ? rank - 1 : rank + 1])) @@ -295,22 +287,20 @@ static eval_t pawns(Worker *worker, const Position *pos, bitboard_t attacks[NB_C const int n = relative_rank_of(us, square) - RANK_4; if (n >= 0 && !bb_test(occ, square + push_inc(us))) - e.eg += us == WHITE ? FreePasser[n] : -FreePasser[n]; + e.eg += us == WHITE ? FreePasser[n] : -FreePasser[n]; } return e; } -static int blend(const Position *pos, eval_t e) -{ +static int blend(const Position *pos, eval_t e) { const int pieceTotal = pos->pieceMaterial[WHITE] + pos->pieceMaterial[BLACK]; return (e.op * pieceTotal + e.eg * (StartPieceTotal - pieceTotal)) / StartPieceTotal; } -void eval_init() -{ - StartPieceTotal = 4 * (PieceValue[KNIGHT] + PieceValue[BISHOP] + PieceValue[ROOK]) - + 2 * PieceValue[QUEEN]; +void eval_init() { + StartPieceTotal = + 4 * (PieceValue[KNIGHT] + PieceValue[BISHOP] + PieceValue[ROOK]) + 2 * PieceValue[QUEEN]; for (int square = H8; square >= A1; square--) { if (rank_of(square) == RANK_8) @@ -331,8 +321,8 @@ void eval_init() } for (int file = FILE_A; file <= FILE_H; file++) - AdjacentFiles[file] = (file > FILE_A ? File[file - 1] : 0) - | (file < FILE_H ? File[file + 1] : 0); + AdjacentFiles[file] = + (file > FILE_A ? File[file - 1] : 0) | (file < FILE_H ? File[file + 1] : 0); for (int s1 = A1; s1 <= H8; s1++) for (int s2 = A1; s2 <= H8; s2++) { @@ -355,8 +345,7 @@ void eval_init() } } -int evaluate(Worker *worker, const Position *pos) -{ +int evaluate(Worker *worker, const Position *pos) { worker->nodes++; assert(!pos->checkers); @@ -376,8 +365,8 @@ int evaluate(Worker *worker, const Position *pos) eval_add(&e[color], mobility(pos, color, attacks)); // Aggregate attacks pieces only (NBRQ) - attacks[color][NB_PIECE] = attacks[color][KNIGHT] | attacks[color][BISHOP] - | attacks[color][ROOK] | attacks[color][QUEEN]; + attacks[color][NB_PIECE] = attacks[color][KNIGHT] | attacks[color][BISHOP] | + attacks[color][ROOK] | attacks[color][QUEEN]; } for (int color = WHITE; color <= BLACK; color++) { diff --git a/src/eval.h b/src/eval.h index bbe7aed..1c5006b 100644 --- a/src/eval.h +++ b/src/eval.h @@ -11,7 +11,7 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once #include "position.h" #include "workers.h" diff --git a/src/gen.c b/src/gen.c index a756031..35dfeac 100644 --- a/src/gen.c +++ b/src/gen.c @@ -11,21 +11,19 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ -#include "bitboard.h" + */ #include "gen.h" +#include "bitboard.h" #include "position.h" -static move_t *serialize_moves(int from, bitboard_t targets, move_t *mList) -{ +static move_t *serialize_moves(int from, bitboard_t targets, move_t *mList) { while (targets) *mList++ = move_build(from, bb_pop_lsb(&targets), NB_PIECE); return mList; } -static move_t *serialize_pawn_moves(bitboard_t pawns, int shift, move_t *mList) -{ +static move_t *serialize_pawn_moves(bitboard_t pawns, int shift, move_t *mList) { while (pawns) { const int from = bb_pop_lsb(&pawns); *mList++ = move_build(from, from + shift, NB_PIECE); @@ -34,8 +32,7 @@ static move_t *serialize_pawn_moves(bitboard_t pawns, int shift, move_t *mList) return mList; } -static move_t *gen_all_moves(const Position *pos, move_t *mList) -{ +static move_t *gen_all_moves(const Position *pos, move_t *mList) { if (pos->checkers) return gen_check_escapes(pos, mList, true); else { @@ -47,15 +44,14 @@ static move_t *gen_all_moves(const Position *pos, move_t *mList) } } -move_t *gen_pawn_moves(const Position *pos, move_t *mList, bitboard_t filter, bool subPromotions) -{ +move_t *gen_pawn_moves(const Position *pos, move_t *mList, bitboard_t filter, bool subPromotions) { const int us = pos->turn, them = opposite(us); const int push = push_inc(us); const bitboard_t capturable = (pos->byColor[them] | pos_ep_square_bb(pos)) & filter; // ** Non promotions ** - const bitboard_t nonPromotingPawns = pos_pieces_cp(pos, us, PAWN) - & ~Rank[relative_rank(us, RANK_7)]; + const bitboard_t nonPromotingPawns = + pos_pieces_cp(pos, us, PAWN) & ~Rank[relative_rank(us, RANK_7)]; // Left captures bitboard_t b = nonPromotingPawns & ~File[FILE_A] & bb_shift(capturable, -(push + LEFT)); @@ -70,8 +66,8 @@ move_t *gen_pawn_moves(const Position *pos, move_t *mList, bitboard_t filter, bo mList = serialize_pawn_moves(b, push, mList); // Double pushes - b = nonPromotingPawns & Rank[relative_rank(us, RANK_2)] & bb_shift(~pos_pieces(pos), -push) - & bb_shift(~pos_pieces(pos) & filter, -2 * push); + b = nonPromotingPawns & Rank[relative_rank(us, RANK_2)] & bb_shift(~pos_pieces(pos), -push) & + bb_shift(~pos_pieces(pos) & filter, -2 * push); mList = serialize_pawn_moves(b, 2 * push, mList); // ** Promotions ** @@ -101,8 +97,7 @@ move_t *gen_pawn_moves(const Position *pos, move_t *mList, bitboard_t filter, bo return mList; } -move_t *gen_piece_moves(const Position *pos, move_t *mList, bitboard_t filter, bool kingMoves) -{ +move_t *gen_piece_moves(const Position *pos, move_t *mList, bitboard_t filter, bool kingMoves) { const int us = pos->turn; int from; @@ -139,8 +134,7 @@ move_t *gen_piece_moves(const Position *pos, move_t *mList, bitboard_t filter, b return mList; } -move_t *gen_castling_moves(const Position *pos, move_t *mList) -{ +move_t *gen_castling_moves(const Position *pos, move_t *mList) { assert(!pos->checkers); const int king = pos_king_square(pos, pos->turn); @@ -151,16 +145,15 @@ move_t *gen_castling_moves(const Position *pos, move_t *mList) const int kto = square_from(rank_of(rook), rook > king ? FILE_G : FILE_C); const int rto = square_from(rank_of(rook), rook > king ? FILE_F : FILE_D); - if (bb_count((Segment[king][kto] | Segment[rook][rto]) & pos_pieces(pos)) == 2 - && !(pos->attacked & Segment[king][kto])) + if (bb_count((Segment[king][kto] | Segment[rook][rto]) & pos_pieces(pos)) == 2 && + !(pos->attacked & Segment[king][kto])) *mList++ = move_build(king, rook, NB_PIECE); } return mList; } -move_t *gen_check_escapes(const Position *pos, move_t *mList, bool subPromotions) -{ +move_t *gen_check_escapes(const Position *pos, move_t *mList, bool subPromotions) { assert(pos->checkers); bitboard_t ours = pos->byColor[pos->turn]; const int king = pos_king_square(pos, pos->turn); @@ -175,8 +168,8 @@ move_t *gen_check_escapes(const Position *pos, move_t *mList, bool subPromotions // sliding check: cover the checking segment, or capture the slider bitboard_t targets = BISHOP <= checkerPiece && checkerPiece <= QUEEN - ? Segment[king][checkerSquare] - : pos->checkers; + ? Segment[king][checkerSquare] + : pos->checkers; mList = gen_piece_moves(pos, mList, targets & ~ours, false); @@ -191,8 +184,7 @@ move_t *gen_check_escapes(const Position *pos, move_t *mList, bool subPromotions return mList; } -bool gen_is_legal(const Position *pos, bitboard_t pins, move_t m) -{ +bool gen_is_legal(const Position *pos, bitboard_t pins, move_t m) { const int from = move_from(m), to = move_to(m); const int piece = pos->pieceOn[from]; const int king = pos_king_square(pos, pos->turn); @@ -217,15 +209,14 @@ bool gen_is_legal(const Position *pos, bitboard_t pins, move_t m) bb_clear(&occ, from); bb_set(&occ, to); bb_clear(&occ, to + push_inc(them)); - return !(bb_rook_attacks(king, occ) & pos_pieces_cpp(pos, them, ROOK, QUEEN)) - && !(bb_bishop_attacks(king, occ) & pos_pieces_cpp(pos, them, BISHOP, QUEEN)); + return !(bb_rook_attacks(king, occ) & pos_pieces_cpp(pos, them, ROOK, QUEEN)) && + !(bb_bishop_attacks(king, occ) & pos_pieces_cpp(pos, them, BISHOP, QUEEN)); } else return true; } } -uint64_t gen_perft(const Position *pos, int depth, int ply) -{ +uint64_t gen_perft(const Position *pos, int depth, int ply) { if (depth <= 0) return 1; diff --git a/src/gen.h b/src/gen.h index 204ebd4..4ad227c 100644 --- a/src/gen.h +++ b/src/gen.h @@ -11,12 +11,12 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once #include "position.h" // Max number of pseudo-legal moves allowed -enum {MAX_MOVES = 192}; +enum { MAX_MOVES = 192 }; // Generate pseudo-legal moves (ie. legal modulo self-check) move_t *gen_pawn_moves(const Position *pos, move_t *mList, bitboard_t filter, bool subPromotions); diff --git a/src/htable.c b/src/htable.c index a463a48..54cd8d2 100644 --- a/src/htable.c +++ b/src/htable.c @@ -11,19 +11,18 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ -#include -#include + */ #include "htable.h" #include "platform.h" #include "search.h" +#include +#include unsigned hashDate = 0; HashEntry *HashTable = NULL; size_t HashCount = 0; -static int score_to_hash(int score, int ply) -{ +static int score_to_hash(int score, int ply) { if (score >= mate_in(MAX_PLY)) score += ply; else if (score <= mated_in(MAX_PLY)) @@ -34,8 +33,7 @@ static int score_to_hash(int score, int ply) return score; } -static int score_from_hash(int hashScore, int ply) -{ +static int score_from_hash(int hashScore, int ply) { if (hashScore >= mate_in(MAX_PLY)) hashScore -= ply; else if (hashScore <= mated_in(MAX_PLY)) @@ -46,14 +44,10 @@ static int score_from_hash(int hashScore, int ply) return hashScore; } -static __attribute__((destructor)) void hash_free(void) -{ - free(HashTable); -} +static __attribute__((destructor)) void hash_free(void) { free(HashTable); } -void hash_prepare(uint64_t hashMB) -{ - assert(bb_count(hashMB) == 1); // must be a power of 2 +void hash_prepare(uint64_t hashMB) { + assert(bb_count(hashMB) == 1); // must be a power of 2 free(HashTable); HashTable = malloc(hashMB << 20); @@ -67,8 +61,7 @@ void hash_prepare(uint64_t hashMB) memset(HashTable, 0, hashMB << 20); } -HashEntry hash_read(uint64_t key, int ply) -{ +HashEntry hash_read(uint64_t key, int ply) { HashEntry e = HashTable[key & (HashCount - 1)]; if (e.key == key) @@ -79,8 +72,7 @@ HashEntry hash_read(uint64_t key, int ply) return e; } -void hash_write(uint64_t key, HashEntry *e, int ply) -{ +void hash_write(uint64_t key, HashEntry *e, int ply) { HashEntry *slot = &HashTable[key & (HashCount - 1)]; e->date = hashDate; @@ -93,13 +85,9 @@ void hash_write(uint64_t key, HashEntry *e, int ply) } } -void hash_prefetch(uint64_t key) -{ - __builtin_prefetch(&HashTable[key & (HashCount - 1)]); -} +void hash_prefetch(uint64_t key) { __builtin_prefetch(&HashTable[key & (HashCount - 1)]); } -int hash_permille() -{ +int hash_permille() { int result = 0; for (int i = 0; i < 1000; i++) diff --git a/src/htable.h b/src/htable.h index 12c60c2..da3913b 100644 --- a/src/htable.h +++ b/src/htable.h @@ -11,13 +11,13 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once +#include "position.h" #include #include -#include "position.h" -enum {LBOUND, EXACT, UBOUND}; +enum { LBOUND, EXACT, UBOUND }; typedef struct { uint64_t key; @@ -27,12 +27,12 @@ typedef struct { int16_t score, eval; move_t move; int8_t depth; - uint8_t bound: 2, date: 6; + uint8_t bound : 2, date : 6; }; }; } HashEntry; -void hash_prepare(uint64_t hashMB); // realloc + clear +void hash_prepare(uint64_t hashMB); // realloc + clear HashEntry hash_read(uint64_t key, int ply); void hash_write(uint64_t key, HashEntry *e, int ply); void hash_prefetch(uint64_t key); diff --git a/src/main.c b/src/main.c index f761510..aab26c1 100644 --- a/src/main.c +++ b/src/main.c @@ -11,9 +11,7 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ -#include -#include + */ #include "bitboard.h" #include "eval.h" #include "htable.h" @@ -23,13 +21,13 @@ #include "uci.h" #include "util.h" #include "workers.h" +#include +#include -void bench(int depth) -{ +void bench(int depth) { static const char *fens[] = { - #include "test.csv" - NULL - }; +#include "test.csv" + NULL}; uint64_t nodes = 0, seal = 0; uciChess960 = true; @@ -55,16 +53,15 @@ void bench(int depth) const int64_t elapsed = system_msec() - start; - hash_blocks(HashTable, HashCount * sizeof(HashEntry), &seal); // sign entire hash table + hash_blocks(HashTable, HashCount * sizeof(HashEntry), &seal); // sign entire hash table - printf("seal : %" PRIx64 "\n", seal); // strong functionality signature + printf("seal : %" PRIx64 "\n", seal); // strong functionality signature printf("time : %" PRIu64 "ms\n", elapsed); - printf("nodes : %" PRIu64 "\n", nodes); // total nodes = weak functionality signature - printf("nps : %.0f\n", nodes * 1000.0 / max(elapsed, 1)); // avoid div/0 + printf("nodes : %" PRIu64 "\n", nodes); // total nodes = weak functionality signature + printf("nps : %.0f\n", nodes * 1000.0 / max(elapsed, 1)); // avoid div/0 } -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { eval_init(); search_init(); @@ -76,7 +73,7 @@ int main(int argc, char **argv) WorkersCount = (size_t)atoll(argv[3]); if (argc > 4) - uciHash = 1ULL << bb_msb((uint64_t)atoll(argv[4])); // must be a power of 2 + uciHash = 1ULL << bb_msb((uint64_t)atoll(argv[4])); // must be a power of 2 workers_prepare(WorkersCount); hash_prepare(uciHash); diff --git a/src/platform.h b/src/platform.h index d668fcd..39c6551 100644 --- a/src/platform.h +++ b/src/platform.h @@ -8,8 +8,8 @@ #define WIN32_LEAN_AND_MEAN #include - // Locks - typedef CRITICAL_SECTION mtx_t; +// Locks +typedef CRITICAL_SECTION mtx_t; #define mtx_init(m, t) InitializeCriticalSection(m) #define mtx_destroy(m) DeleteCriticalSection(m) #define mtx_lock(m) EnterCriticalSection(m) @@ -18,29 +18,30 @@ // Threads #define sleep_msec(msec) Sleep(msec) - // Timer - static inline int64_t system_msec(void) { - LARGE_INTEGER t, f; - QueryPerformanceCounter(&t); - QueryPerformanceFrequency(&f); - return 1000LL * t.QuadPart / f.QuadPart; - } +// Timer +static inline int64_t system_msec(void) { + LARGE_INTEGER t, f; + QueryPerformanceCounter(&t); + QueryPerformanceFrequency(&f); + return 1000LL * t.QuadPart / f.QuadPart; +} #else - // Locks - typedef pthread_mutex_t mtx_t; +// Locks +typedef pthread_mutex_t mtx_t; #define mtx_init(m, t) pthread_mutex_init(m, NULL) #define mtx_destroy(m) pthread_mutex_destroy(m) #define mtx_lock(m) pthread_mutex_lock(m) #define mtx_unlock(m) pthread_mutex_unlock(m) // Threads - #define sleep_msec(msec) nanosleep(&(struct timespec){.tv_sec = msec / 1000, \ - .tv_nsec = (msec % 1000) * 1000000LL}, NULL) + #define sleep_msec(msec) \ + nanosleep(&(struct timespec){.tv_sec = msec / 1000, .tv_nsec = (msec % 1000) * 1000000LL}, \ + NULL) - // Timer - static inline int64_t system_msec(void) { - struct timespec t; - clock_gettime(CLOCK_MONOTONIC, &t); - return t.tv_sec * 1000LL + t.tv_nsec / 1000000; - } +// Timer +static inline int64_t system_msec(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return t.tv_sec * 1000LL + t.tv_nsec / 1000000; +} #endif diff --git a/src/position.c b/src/position.c index 8fac277..dc90031 100644 --- a/src/position.c +++ b/src/position.c @@ -11,27 +11,25 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ -#include -#include -#include -#include "bitboard.h" + */ #include "position.h" +#include "bitboard.h" #include "tune.h" #include "uci.h" #include "util.h" #include "zobrist.h" +#include +#include +#include // Sets the position in its empty state (no pieces, white to play, rule50=0, etc.) -static void clear(Position *pos) -{ +static void clear(Position *pos) { *pos = (Position){0}; memset(pos->pieceOn, NB_PIECE, sizeof(pos->pieceOn)); } // Remove 'piece' of 'color' on 'square'. Such a piece must be there first. -static void clear_square(Position *pos, int color, int piece, int square) -{ +static void clear_square(Position *pos, int color, int piece, int square) { BOUNDS(color, NB_COLOR); BOUNDS(piece, NB_PIECE); BOUNDS(square, NB_SQUARE); @@ -49,8 +47,7 @@ static void clear_square(Position *pos, int color, int piece, int square) } // Put 'piece' of 'color' on 'square'. Square must be empty first. -static void set_square(Position *pos, int color, int piece, int square) -{ +static void set_square(Position *pos, int color, int piece, int square) { BOUNDS(color, NB_COLOR); BOUNDS(piece, NB_PIECE); BOUNDS(square, NB_SQUARE); @@ -68,8 +65,7 @@ static void set_square(Position *pos, int color, int piece, int square) } // Squares attacked by pieces of 'color' -static bitboard_t attacked_by(const Position *pos, int color) -{ +static bitboard_t attacked_by(const Position *pos, int color) { BOUNDS(color, NB_COLOR); // King and Knight attacks @@ -100,14 +96,14 @@ static bitboard_t attacked_by(const Position *pos, int color) } // Helper function used to facorize common tasks, after setting up a position -static void finish(Position *pos) -{ +static void finish(Position *pos) { const int us = pos->turn, them = opposite(us); const int king = pos_king_square(pos, us); pos->attacked = attacked_by(pos, them); pos->checkers = bb_test(pos->attacked, king) - ? pos_attackers_to(pos, king, pos_pieces(pos)) & pos->byColor[them] : 0; + ? pos_attackers_to(pos, king, pos_pieces(pos)) & pos->byColor[them] + : 0; #ifndef NDEBUG // Verify that byColor[] and byPiece[] do not collide, and are consistent @@ -137,8 +133,8 @@ static void finish(Position *pos) // Verify castle rooks if (pos->castleRooks) { - assert(!(pos->castleRooks & ~((Rank[RANK_1] & pos_pieces_cp(pos, WHITE, ROOK)) - | (Rank[RANK_8] & pos_pieces_cp(pos, BLACK, ROOK))))); + assert(!(pos->castleRooks & ~((Rank[RANK_1] & pos_pieces_cp(pos, WHITE, ROOK)) | + (Rank[RANK_8] & pos_pieces_cp(pos, BLACK, ROOK))))); for (int color = WHITE; color <= BLACK; color++) { const bitboard_t b = pos->castleRooks & pos->byColor[color]; @@ -188,8 +184,8 @@ static void finish(Position *pos) } assert(kingPawnKey == pos->kingPawnKey); - key ^= ZobristEnPassant[pos->epSquare] ^ (pos->turn == BLACK ? ZobristTurn : 0) - ^ zobrist_castling(pos->castleRooks); + key ^= ZobristEnPassant[pos->epSquare] ^ (pos->turn == BLACK ? ZobristTurn : 0) ^ + zobrist_castling(pos->castleRooks); assert(pos->key == key); assert(pieceMaterial[WHITE] == pos->pieceMaterial[WHITE]); assert(pieceMaterial[BLACK] == pos->pieceMaterial[BLACK]); @@ -203,8 +199,7 @@ static void finish(Position *pos) const char *PieceLabel[NB_COLOR] = {"NBRQKP.", "nbrqkp."}; -void square_to_string(int square, char *str) -{ +void square_to_string(int square, char *str) { BOUNDS(square, NB_SQUARE + 1); if (square == NB_SQUARE) @@ -217,16 +212,12 @@ void square_to_string(int square, char *str) *str = '\0'; } -int string_to_square(const char *str) -{ - return *str != '-' - ? square_from(str[1] - '1', str[0] - 'a') - : NB_SQUARE; +int string_to_square(const char *str) { + return *str != '-' ? square_from(str[1] - '1', str[0] - 'a') : NB_SQUARE; } // Set position from FEN string -void pos_set(Position *pos, const char *fen) -{ +void pos_set(Position *pos, const char *fen) { clear(pos); char *str = strdup(fen), *strPos = NULL; char *token = strtok_r(str, " ", &strPos); @@ -237,7 +228,7 @@ void pos_set(Position *pos, const char *fen) while ((ch = *token++)) { if ('1' <= ch && ch <= '8') { - file += ch -'0'; + file += ch - '0'; assert(file <= NB_FILE); } else if (ch == '/') { rank--; @@ -246,7 +237,7 @@ void pos_set(Position *pos, const char *fen) assert(strchr("nbrqkpNBRQKP", ch)); const bool color = islower(ch); set_square(pos, color, strchr(PieceLabel[color], ch) - PieceLabel[color], - square_from(rank, file++)); + square_from(rank, file++)); } } @@ -293,8 +284,7 @@ void pos_set(Position *pos, const char *fen) } // Get FEN string of position -void pos_get(const Position *pos, char *fen) -{ +void pos_get(const Position *pos, char *fen) { // Piece placement for (int rank = RANK_8; rank >= RANK_1; rank--) { int cnt = 0; @@ -350,8 +340,7 @@ void pos_get(const Position *pos, char *fen) } // Play a move on a position copy (original 'before' is untouched): pos = before + play(m) -void pos_move(Position *pos, const Position *before, move_t m) -{ +void pos_move(Position *pos, const Position *before, move_t m) { *pos = *before; pos->rule50++; @@ -392,8 +381,8 @@ void pos_move(Position *pos, const Position *before, move_t m) pos->rule50 = 0; // Set ep square upon double push, only if catpturably by enemy pawns - if (to == from + 2 * push - && (PawnAttacks[us][from + push] & pos_pieces_cp(pos, them, PAWN))) + if (to == from + 2 * push && + (PawnAttacks[us][from + push] & pos_pieces_cp(pos, them, PAWN))) pos->epSquare = from + push; // handle ep-capture and promotion @@ -429,8 +418,7 @@ void pos_move(Position *pos, const Position *before, move_t m) } // Play a null move (ie. switch sides): pos = before + play(null) -void pos_switch(Position *pos, const Position *before) -{ +void pos_switch(Position *pos, const Position *before) { *pos = *before; pos->epSquare = NB_SQUARE; @@ -442,23 +430,20 @@ void pos_switch(Position *pos, const Position *before) } // All pieces -bitboard_t pos_pieces(const Position *pos) -{ +bitboard_t pos_pieces(const Position *pos) { assert(!(pos->byColor[WHITE] & pos->byColor[BLACK])); return pos->byColor[WHITE] | pos->byColor[BLACK]; } // Pieces of color 'color' and type 'piece' -bitboard_t pos_pieces_cp(const Position *pos, int color, int piece) -{ +bitboard_t pos_pieces_cp(const Position *pos, int color, int piece) { BOUNDS(color, NB_COLOR); BOUNDS(piece, NB_PIECE); return pos->byColor[color] & pos->byPiece[piece]; } // Pieces of color 'color' and type 'p1' or 'p2' -bitboard_t pos_pieces_cpp(const Position *pos, int color, int p1, int p2) -{ +bitboard_t pos_pieces_cpp(const Position *pos, int color, int p1, int p2) { BOUNDS(color, NB_COLOR); BOUNDS(p1, NB_PIECE); BOUNDS(p2, NB_PIECE); @@ -466,51 +451,45 @@ bitboard_t pos_pieces_cpp(const Position *pos, int color, int p1, int p2) } // En passant square, in bitboard format -bitboard_t pos_ep_square_bb(const Position *pos) -{ +bitboard_t pos_ep_square_bb(const Position *pos) { return pos->epSquare < NB_SQUARE ? 1ULL << pos->epSquare : 0; } // Detect insufficient material configuration (draw by chess rules only) -bool pos_insufficient_material(const Position *pos) -{ - return bb_count(pos_pieces(pos)) <= 3 && !pos->byPiece[PAWN] && !pos->byPiece[ROOK] - && !pos->byPiece[QUEEN]; +bool pos_insufficient_material(const Position *pos) { + return bb_count(pos_pieces(pos)) <= 3 && !pos->byPiece[PAWN] && !pos->byPiece[ROOK] && + !pos->byPiece[QUEEN]; } // Square occupied by the king of color 'color' -int pos_king_square(const Position *pos, int color) -{ +int pos_king_square(const Position *pos, int color) { assert(bb_count(pos_pieces_cp(pos, color, KING)) == 1); return bb_lsb(pos_pieces_cp(pos, color, KING)); } // Color of piece on square 'square'. Square is assumed to be occupied. -int pos_color_on(const Position *pos, int square) -{ +int pos_color_on(const Position *pos, int square) { assert(bb_test(pos_pieces(pos), square)); return bb_test(pos->byColor[WHITE], square) ? WHITE : BLACK; } // Attackers (or any color) to square 'square', using occupancy 'occ' for rook/bishop attacks -bitboard_t pos_attackers_to(const Position *pos, int square, bitboard_t occ) -{ +bitboard_t pos_attackers_to(const Position *pos, int square, bitboard_t occ) { BOUNDS(square, NB_SQUARE); - return (pos_pieces_cp(pos, WHITE, PAWN) & PawnAttacks[BLACK][square]) - | (pos_pieces_cp(pos, BLACK, PAWN) & PawnAttacks[WHITE][square]) - | (KnightAttacks[square] & pos->byPiece[KNIGHT]) - | (KingAttacks[square] & pos->byPiece[KING]) - | (bb_rook_attacks(square, occ) & (pos->byPiece[ROOK] | pos->byPiece[QUEEN])) - | (bb_bishop_attacks(square, occ) & (pos->byPiece[BISHOP] | pos->byPiece[QUEEN])); + return (pos_pieces_cp(pos, WHITE, PAWN) & PawnAttacks[BLACK][square]) | + (pos_pieces_cp(pos, BLACK, PAWN) & PawnAttacks[WHITE][square]) | + (KnightAttacks[square] & pos->byPiece[KNIGHT]) | + (KingAttacks[square] & pos->byPiece[KING]) | + (bb_rook_attacks(square, occ) & (pos->byPiece[ROOK] | pos->byPiece[QUEEN])) | + (bb_bishop_attacks(square, occ) & (pos->byPiece[BISHOP] | pos->byPiece[QUEEN])); } // Pinned pieces for the side to move -bitboard_t calc_pins(const Position *pos) -{ +bitboard_t calc_pins(const Position *pos) { const int us = pos->turn, them = opposite(us); const int king = pos_king_square(pos, us); - bitboard_t pinners = (pos_pieces_cpp(pos, them, ROOK, QUEEN) & bb_rook_attacks(king, 0)) - | (pos_pieces_cpp(pos, them, BISHOP, QUEEN) & bb_bishop_attacks(king, 0)); + bitboard_t pinners = (pos_pieces_cpp(pos, them, ROOK, QUEEN) & bb_rook_attacks(king, 0)) | + (pos_pieces_cpp(pos, them, BISHOP, QUEEN) & bb_bishop_attacks(king, 0)); bitboard_t result = 0; while (pinners) { @@ -526,20 +505,17 @@ bitboard_t calc_pins(const Position *pos) return result; } -bool pos_move_is_capture(const Position *pos, move_t m) -{ +bool pos_move_is_capture(const Position *pos, move_t m) { const int from = move_from(m), to = move_to(m); - return bb_test(pos->byColor[opposite(pos->turn)], to) - || (pos->pieceOn[from] == PAWN && (to == pos->epSquare || move_prom(m) < NB_PIECE)); + return bb_test(pos->byColor[opposite(pos->turn)], to) || + (pos->pieceOn[from] == PAWN && (to == pos->epSquare || move_prom(m) < NB_PIECE)); } -bool pos_move_is_castling(const Position *pos, move_t m) -{ +bool pos_move_is_castling(const Position *pos, move_t m) { return bb_test(pos->byColor[pos->turn], move_to(m)); } -void pos_move_to_string(const Position *pos, move_t m, char *str) -{ +void pos_move_to_string(const Position *pos, move_t m, char *str) { const int from = move_from(m), to = move_to(m), prom = move_prom(m); if (!(from | to | prom)) { @@ -548,8 +524,8 @@ void pos_move_to_string(const Position *pos, move_t m, char *str) } const int _to = !uciChess960 && pos_move_is_castling(pos, m) - ? (to > from ? from + 2 : from - 2) // e1h1 -> e1g1, e1a1 -> e1c1 - : to; + ? (to > from ? from + 2 : from - 2) // e1h1 -> e1g1, e1a1 -> e1c1 + : to; square_to_string(from, str); square_to_string(_to, str + 2); @@ -560,16 +536,16 @@ void pos_move_to_string(const Position *pos, move_t m, char *str) } } -move_t pos_string_to_move(const Position *pos, const char *str) -{ - const int prom = str[4] ? (int)(strchr(PieceLabel[BLACK], str[4]) - PieceLabel[BLACK]) : NB_PIECE; +move_t pos_string_to_move(const Position *pos, const char *str) { + const int prom = + str[4] ? (int)(strchr(PieceLabel[BLACK], str[4]) - PieceLabel[BLACK]) : NB_PIECE; const int from = square_from(str[1] - '1', str[0] - 'a'); int to = square_from(str[3] - '1', str[2] - 'a'); if (!uciChess960 && pos->pieceOn[from] == KING) { - if (to == from + 2) // e1g1 -> e1h1 + if (to == from + 2) // e1g1 -> e1h1 to++; - else if (to == from - 2) // e1c1 -> e1a1 + else if (to == from - 2) // e1c1 -> e1a1 to -= 2; } @@ -577,8 +553,7 @@ move_t pos_string_to_move(const Position *pos, const char *str) } // Static Exchange Evaluator -int pos_see(const Position *pos, move_t m) -{ +int pos_see(const Position *pos, move_t m) { const int from = move_from(m), to = move_to(m), prom = move_prom(m); int us = pos->turn; bitboard_t occ = pos_pieces(pos); @@ -642,16 +617,16 @@ int pos_see(const Position *pos, move_t m) } // Prints the position in ASCII 'art' (for debugging) -void pos_print(const Position *pos) -{ +void pos_print(const Position *pos) { for (int rank = RANK_8; rank >= RANK_1; rank--) { char line[] = ". . . . . . . ."; for (int file = FILE_A; file <= FILE_H; file++) { const int square = square_from(rank, file); line[2 * file] = bb_test(pos_pieces(pos), square) - ? PieceLabel[pos_color_on(pos, square)][pos->pieceOn[square]] - : square == pos->epSquare ? '*' : '.'; + ? PieceLabel[pos_color_on(pos, square)][pos->pieceOn[square]] + : square == pos->epSquare ? '*' + : '.'; } puts(line); diff --git a/src/position.h b/src/position.h index 589feca..66527f4 100644 --- a/src/position.h +++ b/src/position.h @@ -11,30 +11,30 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once #include "bitboard.h" #include "pst.h" -enum {MATE = 32000}; +enum { MATE = 32000 }; // Max number of bytes needed to store a FEN -enum {MAX_FEN = 64 + 8 + 2 + 5 + 3 + 4 + 4 + 1}; +enum { MAX_FEN = 64 + 8 + 2 + 5 + 3 + 4 + 4 + 1 }; typedef struct { - bitboard_t byColor[NB_COLOR]; // eg. byColor[WHITE] = squares occupied by white's army - bitboard_t byPiece[NB_PIECE]; // eg. byPiece[KNIGHT] = squares occupied by knights (any color) - bitboard_t castleRooks; // rooks with castling rights (eg. A1, A8, H1, H8 in start pos) - bitboard_t attacked; // squares attacked by enemy - bitboard_t checkers; // if in check, enemy piece(s) giving check(s), otherwise empty - uint64_t key; // hash key encoding all information of the position (except rule50) - uint64_t kingPawnKey; // hash key encoding only king and pawns - int pieceMaterial[NB_COLOR]; // endgame piece material value by color (excluding pawns) - eval_t pst; // PST (Piece on Square Tables) total. Net sum, from white's pov + bitboard_t byColor[NB_COLOR]; // eg. byColor[WHITE] = squares occupied by white's army + bitboard_t byPiece[NB_PIECE]; // eg. byPiece[KNIGHT] = squares occupied by knights (any color) + bitboard_t castleRooks; // rooks with castling rights (eg. A1, A8, H1, H8 in start pos) + bitboard_t attacked; // squares attacked by enemy + bitboard_t checkers; // if in check, enemy piece(s) giving check(s), otherwise empty + uint64_t key; // hash key encoding all information of the position (except rule50) + uint64_t kingPawnKey; // hash key encoding only king and pawns + int pieceMaterial[NB_COLOR]; // endgame piece material value by color (excluding pawns) + eval_t pst; // PST (Piece on Square Tables) total. Net sum, from white's pov uint8_t pieceOn[NB_SQUARE]; // eg. pieceOn[D1] = QUEEN in start pos - int turn; // turn of play (WHITE or BLACK) - int epSquare; // en-passant square (NB_SQUARE if none) - int rule50; // ply counter for 50-move rule, ranging from 0 to 100 = draw (unless mated) + int turn; // turn of play (WHITE or BLACK) + int epSquare; // en-passant square (NB_SQUARE if none) + int rule50; // ply counter for 50-move rule, ranging from 0 to 100 = draw (unless mated) } Position; extern const char *PieceLabel[NB_COLOR]; @@ -48,7 +48,7 @@ void pos_get(const Position *pos, char *fen); void pos_move(Position *pos, const Position *before, move_t m); void pos_switch(Position *pos, const Position *before); -bitboard_t pos_pieces(const Position* pos); +bitboard_t pos_pieces(const Position *pos); bitboard_t pos_pieces_cp(const Position *pos, int color, int piece); bitboard_t pos_pieces_cpp(const Position *pos, int color, int p1, int p2); diff --git a/src/pst.c b/src/pst.c index 6bc04cb..74191be 100644 --- a/src/pst.c +++ b/src/pst.c @@ -11,23 +11,23 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #include "pst.h" #include "tune.h" eval_t PST[NB_COLOR][NB_PIECE][NB_SQUARE]; -void __attribute__((constructor)) pst_init() -{ +void __attribute__((constructor)) pst_init() { for (int color = WHITE; color <= BLACK; color++) for (int piece = KNIGHT; piece < NB_PIECE; piece++) for (int square = A1; square <= H8; square++) { const int rr = relative_rank_of(color, square); const int file = file_of(square), file4 = file > FILE_D ? FILE_H - file : file; - PST[color][piece][square] = piece == PAWN ? (eval_t){2 * PieceValue[PAWN] - 200, 200} + PST[color][piece][square] = + piece == PAWN ? (eval_t){2 * PieceValue[PAWN] - 200, 200} : piece == KING ? (eval_t){0, 0} - : (eval_t){PieceValue[piece], PieceValue[piece]}; + : (eval_t){PieceValue[piece], PieceValue[piece]}; if (piece == KNIGHT) { eval_add(&PST[color][piece][square], KnightPstSeed[file4]); diff --git a/src/pst.h b/src/pst.h index 769672c..edbb400 100644 --- a/src/pst.h +++ b/src/pst.h @@ -11,7 +11,7 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once #include "bitboard.h" diff --git a/src/search.c b/src/search.c index 78d0541..6fa741b 100644 --- a/src/search.c +++ b/src/search.c @@ -11,34 +11,30 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ -#include -#include + */ +#include "search.h" #include "eval.h" #include "htable.h" #include "position.h" -#include "search.h" #include "sort.h" #include "uci.h" #include "workers.h" +#include +#include Position rootPos; ZobristStack rootStack; Limits lim; -atomic_bool Stop; // Stop signal raised by timer or master thread, and observed by workers +atomic_bool Stop; // Stop signal raised by timer or master thread, and observed by workers int Contempt = 10; -int draw_score(int ply) -{ - return (ply & 1 ? Contempt : -Contempt) * 2; -} +int draw_score(int ply) { return (ply & 1 ? Contempt : -Contempt) * 2; } int Reduction[MAX_DEPTH + 1][MAX_MOVES]; -void search_init() -{ +void search_init() { for (int d = 1; d <= MAX_DEPTH; d++) for (int cnt = 1; cnt < MAX_MOVES; cnt++) Reduction[d][cnt] = 0.4 * log(min(d, 31)) + 1.057 * log(min(cnt, 31)); @@ -47,12 +43,11 @@ void search_init() const int Tempo = 17; static int qsearch(Worker *worker, const Position *pos, int ply, int depth, int alpha, int beta, - bool pvNode, move_t pv[]) -{ + bool pvNode, move_t pv[]) { assert(depth <= 0); assert(zobrist_back(&worker->stack) == pos->key); assert(-MATE <= alpha && alpha < beta && beta <= MATE); - assert(pvNode || (alpha+1 == beta)); + assert(pvNode || (alpha + 1 == beta)); const int oldAlpha = alpha; int bestScore = -MATE; @@ -75,21 +70,22 @@ static int qsearch(Worker *worker, const Position *pos, int ply, int depth, int int refinedEval; if (he.data) { - if (!pvNode && ((he.score <= alpha && he.bound >= EXACT) - || (he.score >= beta && he.bound <= EXACT))) { + if (!pvNode && + ((he.score <= alpha && he.bound >= EXACT) || (he.score >= beta && he.bound <= EXACT))) { assert(he.depth >= depth); return he.score; } refinedEval = worker->eval[ply] = he.eval; - if ((he.score > refinedEval && he.bound <= EXACT) - || (he.score < refinedEval && he.bound >= EXACT)) + if ((he.score > refinedEval && he.bound <= EXACT) || + (he.score < refinedEval && he.bound >= EXACT)) refinedEval = he.score; } else refinedEval = worker->eval[ply] = pos->checkers ? -MATE - : zobrist_move_key(&worker->stack, 0) == ZobristTurn ? -worker->eval[ply - 1] + 2 * Tempo - : evaluate(worker, pos) + Tempo; + : zobrist_move_key(&worker->stack, 0) == ZobristTurn + ? -worker->eval[ply - 1] + 2 * Tempo + : evaluate(worker, pos) + Tempo; if (ply >= MAX_PLY) return refinedEval; @@ -140,7 +136,7 @@ static int qsearch(Worker *worker, const Position *pos, int ply, int depth, int // Recursion (plain alpha/beta) if (depth <= MIN_DEPTH && !pos->checkers) { - score = worker->eval[ply] + see; // guard against QSearch explosion + score = worker->eval[ply] + see; // guard against QSearch explosion if (pvNode) childPv[0] = 0; @@ -192,13 +188,12 @@ static int qsearch(Worker *worker, const Position *pos, int ply, int depth, int } static int search(Worker *worker, const Position *pos, int ply, int depth, int alpha, int beta, - move_t pv[], move_t singularMove) -{ + move_t pv[], move_t singularMove) { static const int EvalMargin[] = {0, 130, 264, 410, 510, 672, 840}; static const int RazorMargin[] = {0, 229, 438, 495, 878, 1094}; static const int SEEMargin[2][6] = { - {0, 0, 0, 0, -179, -358}, // quiet - {0, -33, -132, -297, -528, -825} // capture + {0, 0, 0, 0, -179, -358}, // quiet + {0, -33, -132, -297, -528, -825} // capture }; static const int ProbcutMargin = 300; @@ -230,19 +225,20 @@ static int search(Worker *worker, const Position *pos, int ply, int depth, int a int refinedEval; if (he.data) { - if (he.depth >= depth && !pvNode && ((he.score <= alpha && he.bound >= EXACT) - || (he.score >= beta && he.bound <= EXACT))) + if (he.depth >= depth && !pvNode && + ((he.score <= alpha && he.bound >= EXACT) || (he.score >= beta && he.bound <= EXACT))) return he.score; refinedEval = worker->eval[ply] = he.eval; - if ((he.score > refinedEval && he.bound <= EXACT) - || (he.score < refinedEval && he.bound >= EXACT)) + if ((he.score > refinedEval && he.bound <= EXACT) || + (he.score < refinedEval && he.bound >= EXACT)) refinedEval = he.score; } else refinedEval = worker->eval[ply] = pos->checkers ? -MATE - : zobrist_move_key(&worker->stack, 0) == ZobristTurn ? -worker->eval[ply - 1] + 2 * Tempo - : evaluate(worker, pos) + Tempo; + : zobrist_move_key(&worker->stack, 0) == ZobristTurn + ? -worker->eval[ply - 1] + 2 * Tempo + : evaluate(worker, pos) + Tempo; // At Root, ensure that the last best move is searched first. This is not guaranteed, // as the HT entry could have got overriden by other search threads. @@ -253,8 +249,8 @@ static int search(Worker *worker, const Position *pos, int ply, int depth, int a return refinedEval; // Eval pruning - if (depth <= 6 && !pos->checkers && !pvNode && pos->pieceMaterial[us] - && refinedEval >= beta + EvalMargin[depth]) + if (depth <= 6 && !pos->checkers && !pvNode && pos->pieceMaterial[us] && + refinedEval >= beta + EvalMargin[depth]) return refinedEval; // Razoring @@ -275,10 +271,9 @@ static int search(Worker *worker, const Position *pos, int ply, int depth, int a // Null search int nextDepth = depth - (3 + depth / 4) - (refinedEval >= beta + 167); - if (depth >= 2 && !pvNode && !pos->checkers - && worker->eval[ply] >= beta && pos->pieceMaterial[us] - && !(he.key == pos->key && he.depth >= nextDepth && he.bound >= EXACT - && he.score < beta)) { + if (depth >= 2 && !pvNode && !pos->checkers && worker->eval[ply] >= beta && + pos->pieceMaterial[us] && + !(he.key == pos->key && he.depth >= nextDepth && he.bound >= EXACT && he.score < beta)) { // Normally worker->eval[ply] >= beta excludes the in check case (eval is -MATE). But with // HT collisions or races, HT data can't be trusted. Doing a null move in check crashes for // obvious reasons, so it must be explicitely prevented. @@ -286,9 +281,10 @@ static int search(Worker *worker, const Position *pos, int ply, int depth, int a pos_switch(&nextPos, pos); zobrist_push(&worker->stack, nextPos.key); - score = nextDepth <= 0 - ? -qsearch(worker, &nextPos, ply + 1, nextDepth, -beta, -(beta - 1), false, childPv) - : -search(worker, &nextPos, ply + 1, nextDepth, -beta, -(beta - 1), childPv, 0); + score = + nextDepth <= 0 + ? -qsearch(worker, &nextPos, ply + 1, nextDepth, -beta, -(beta - 1), false, childPv) + : -search(worker, &nextPos, ply + 1, nextDepth, -beta, -(beta - 1), childPv, 0); zobrist_pop(&worker->stack); @@ -370,7 +366,7 @@ static int search(Worker *worker, const Position *pos, int ply, int depth, int a if (depth <= 5 && !pvNode && !nextPos.checkers) { // SEE pruning if (see < SEEMargin[capture][depth]) - continue; + continue; // Late Move Pruning if (!capture && depth <= 4 && moveCount >= 3 * depth + 2 * improving) @@ -386,13 +382,14 @@ static int search(Worker *worker, const Position *pos, int ply, int depth, int a // Search extension int ext = 0; - if (currentMove == he.move && ply > 0 && depth >= 5 && he.bound <= EXACT - && he.depth >= depth - 4) { + if (currentMove == he.move && ply > 0 && depth >= 5 && he.bound <= EXACT && + he.depth >= depth - 4) { // Singular Extension Search const int lbound = he.score - 2 * depth; if (abs(lbound) < MATE) { - score = search(worker, pos, ply, depth - 4, lbound, lbound + 1, childPv, currentMove); + score = + search(worker, pos, ply, depth - 4, lbound, lbound + 1, childPv, currentMove); ext = score <= lbound; } } else @@ -425,19 +422,20 @@ static int search(Worker *worker, const Position *pos, int ply, int depth, int a // Reduced depth, zero window score = nextDepth - reduction <= 0 - ? -qsearch(worker, &nextPos, ply + 1, nextDepth - reduction, -(alpha + 1), - -alpha, false, childPv) - : -search(worker, &nextPos, ply + 1, nextDepth - reduction, -(alpha + 1), - -alpha, childPv, 0); + ? -qsearch(worker, &nextPos, ply + 1, nextDepth - reduction, + -(alpha + 1), -alpha, false, childPv) + : -search(worker, &nextPos, ply + 1, nextDepth - reduction, + -(alpha + 1), -alpha, childPv, 0); // Fail high: re-search zero window at full depth if (reduction && score > alpha) score = -search(worker, &nextPos, ply + 1, nextDepth, -(alpha + 1), -alpha, - childPv, 0); + childPv, 0); // Fail high at full depth for pvNode: re-search full window if (pvNode && alpha < score && score < beta) - score = -search(worker, &nextPos, ply + 1, nextDepth, -beta, -alpha, childPv, 0); + score = + -search(worker, &nextPos, ply + 1, nextDepth, -beta, -alpha, childPv, 0); } } @@ -460,8 +458,8 @@ static int search(Worker *worker, const Position *pos, int ply, int depth, int a if (!(pv[i + 1] = childPv[i])) break; - // Best move has changed since last completed iteration. Update the best move and - // PV immediately, because we may not have time to finish this iteration. + // Best move has changed since last completed iteration. Update the best move + // and PV immediately, because we may not have time to finish this iteration. if (ply == 0 && moveCount > 1 && depth > 1) info_update(&ui, depth, score, workers_nodes(), pv, true); } @@ -505,8 +503,7 @@ static int search(Worker *worker, const Position *pos, int ply, int depth, int a return bestScore; } -static int aspirate(Worker *worker, int depth, move_t pv[], int score) -{ +static int aspirate(Worker *worker, int depth, move_t pv[], int score) { assert(depth > 0); if (depth == 1) @@ -516,7 +513,7 @@ static int aspirate(Worker *worker, int depth, move_t pv[], int score) int alpha = max(score - delta, -MATE); int beta = min(score + delta, MATE); - for ( ; ; delta += delta / 2) { + for (;; delta += delta / 2) { score = search(worker, &rootPos, 0, depth, alpha, beta, pv, 0); if (score <= alpha) { @@ -530,8 +527,7 @@ static int aspirate(Worker *worker, int depth, move_t pv[], int score) } } -void *iterate(void *_worker) -{ +void *iterate(void *_worker) { Worker *worker = _worker; move_t pv[MAX_PLY + 1]; int volatile score = 0; @@ -540,7 +536,7 @@ void *iterate(void *_worker) if (!setjmp(worker->jbuf)) score = aspirate(worker, depth, pv, score); else { - worker->stack.idx = rootStack.idx; // Restore stack position + worker->stack.idx = rootStack.idx; // Restore stack position break; } @@ -555,24 +551,16 @@ void *iterate(void *_worker) return NULL; } -int mated_in(int ply) -{ - return ply - MATE; -} +int mated_in(int ply) { return ply - MATE; } -int mate_in(int ply) -{ - return MATE - ply; -} +int mate_in(int ply) { return MATE - ply; } -bool is_mate_score(int score) -{ +bool is_mate_score(int score) { assert(abs(score) < MATE); return abs(score) >= MATE - MAX_PLY; } -uint64_t search_go() -{ +uint64_t search_go() { int64_t start = system_msec(); info_create(&ui); @@ -582,7 +570,7 @@ uint64_t search_go() pthread_t threads[WorkersCount]; workers_new_search(); - int minTime = 0, maxTime = 0; // Silence bogus gcc warning (maybe uninitialized) + int minTime = 0, maxTime = 0; // Silence bogus gcc warning (maybe uninitialized) if (!lim.movetime && (lim.time || lim.inc)) { const int movesToGo = lim.movestogo ? lim.movestogo : 26; @@ -594,7 +582,7 @@ uint64_t search_go() for (size_t i = 0; i < WorkersCount; i++) // Start searching thread - pthread_create(&threads[i], NULL, (void*(*)(void*))iterate, &Workers[i]); + pthread_create(&threads[i], NULL, (void *(*)(void *))iterate, &Workers[i]); do { sleep_msec(5); @@ -602,8 +590,8 @@ uint64_t search_go() // Check for search termination conditions, but only after depth 1 has been // completed, to make sure we do not return an illegal move. if (!lim.infinite && info_last_depth(&ui) > 0) { - if ((lim.movetime && system_msec() - start >= lim.movetime - uciTimeBuffer) - || (lim.nodes && workers_nodes() >= lim.nodes)) + if ((lim.movetime && system_msec() - start >= lim.movetime - uciTimeBuffer) || + (lim.nodes && workers_nodes() >= lim.nodes)) atomic_store_explicit(&Stop, true, memory_order_release); else if (lim.time || lim.inc) { const double x = 1 / (1 + exp(-info_variability(&ui))); @@ -624,9 +612,8 @@ uint64_t search_go() return workers_nodes(); } -void *search_posix(void *dummy) -{ - (void)dummy; // silence compiler warning (unused variable) +void *search_posix(void *dummy) { + (void)dummy; // silence compiler warning (unused variable) search_go(); return NULL; } diff --git a/src/search.h b/src/search.h index dcebc35..c0498d3 100644 --- a/src/search.h +++ b/src/search.h @@ -11,14 +11,15 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once -#include #include "position.h" #include "zobrist.h" +#include enum { - MAX_DEPTH = 127, MIN_DEPTH = -8, + MAX_DEPTH = 127, + MIN_DEPTH = -8, MAX_PLY = MAX_DEPTH - MIN_DEPTH + 2, }; @@ -26,14 +27,14 @@ typedef struct { int64_t movetime, time, inc; uint64_t nodes; int depth, movestogo; - atomic_bool infinite; // IO thread can change this while Timer thread is checking it + atomic_bool infinite; // IO thread can change this while Timer thread is checking it } Limits; int mated_in(int ply); int mate_in(int ply); bool is_mate_score(int score); -extern atomic_bool Stop; // set this to true to stop the search +extern atomic_bool Stop; // set this to true to stop the search extern Position rootPos; extern ZobristStack rootStack; @@ -42,4 +43,4 @@ extern int Contempt; void search_init(void); uint64_t search_go(void); -void *search_posix(void *); // POSIX wrapper for pthread_create() +void *search_posix(void *); // POSIX wrapper for pthread_create() diff --git a/src/sort.c b/src/sort.c index d5abf58..bd55b31 100644 --- a/src/sort.c +++ b/src/sort.c @@ -11,21 +11,17 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ -#include -#include + */ +#include "sort.h" #include "bitboard.h" #include "position.h" #include "search.h" -#include "sort.h" +#include +#include -enum { - HISTORY_MAX = MAX_DEPTH * MAX_DEPTH, - SEPARATION = 3 * HISTORY_MAX + 1 -}; +enum { HISTORY_MAX = MAX_DEPTH * MAX_DEPTH, SEPARATION = 3 * HISTORY_MAX + 1 }; -void sort_generate(Sort *sort, const Position *pos, int depth) -{ +void sort_generate(Sort *sort, const Position *pos, int depth) { move_t *it = sort->moves; if (pos->checkers) @@ -33,8 +29,8 @@ void sort_generate(Sort *sort, const Position *pos, int depth) else { const int us = pos->turn; const bitboard_t pieceFilter = depth > 0 ? ~pos->byColor[us] : pos->byColor[opposite(us)]; - const bitboard_t pawnFilter = pieceFilter | pos_ep_square_bb(pos) | Rank[relative_rank(us, - RANK_8)]; + const bitboard_t pawnFilter = + pieceFilter | pos_ep_square_bb(pos) | Rank[relative_rank(us, RANK_8)]; it = gen_piece_moves(pos, it, pieceFilter, true); it = gen_pawn_moves(pos, it, pawnFilter, depth > 0); @@ -46,8 +42,7 @@ void sort_generate(Sort *sort, const Position *pos, int depth) sort->cnt = (size_t)(it - sort->moves); } -void sort_score(Worker *worker, Sort *sort, const Position *pos, move_t ttMove) -{ +void sort_score(Worker *worker, Sort *sort, const Position *pos, move_t ttMove) { const size_t rhIdx = zobrist_move_key(&worker->stack, 0) % NB_REFUTATION; const size_t fuhIdx = zobrist_move_key(&worker->stack, 1) % NB_FOLLOW_UP; @@ -62,36 +57,33 @@ void sort_score(Worker *worker, Sort *sort, const Position *pos, move_t ttMove) sort->scores[i] = see >= 0 ? see + SEPARATION : see - SEPARATION; } else { const int from = move_from(m), to = move_to(m); - sort->scores[i] = worker->history[pos->turn][from][to] - + worker->refutationHistory[rhIdx][pos->pieceOn[from]][to] - + worker->followUpHistory[fuhIdx][pos->pieceOn[from]][to]; + sort->scores[i] = worker->history[pos->turn][from][to] + + worker->refutationHistory[rhIdx][pos->pieceOn[from]][to] + + worker->followUpHistory[fuhIdx][pos->pieceOn[from]][to]; } } } } -void history_update(int16_t *t, int bonus) -{ +void history_update(int16_t *t, int bonus) { // Do all calculations on 32-bit, and only convert back to 16-bits once we are certain that // there can be no overflow (signed int overflow is undefined in C). int v = *t; v += 32 * bonus - v * abs(bonus) / 128; v = min(v, HISTORY_MAX); // cap - v = max(v, -HISTORY_MAX); // floor + v = max(v, -HISTORY_MAX); // floor *t = v; } -void sort_init(Worker *worker, Sort *sort, const Position *pos, int depth, move_t ttMove) -{ +void sort_init(Worker *worker, Sort *sort, const Position *pos, int depth, move_t ttMove) { sort_generate(sort, pos, depth); sort_score(worker, sort, pos, ttMove); sort->idx = 0; } -move_t sort_next(Sort *sort, const Position *pos, int *see) -{ +move_t sort_next(Sort *sort, const Position *pos, int *see) { int maxScore = INT_MIN; size_t maxIdx = sort->idx; @@ -101,14 +93,19 @@ move_t sort_next(Sort *sort, const Position *pos, int *see) maxIdx = i; } - #define swap(x, y) do { typeof(x) tmp = x; x = y; y = tmp; } while (0); +#define swap(x, y) \ + do { \ + typeof(x) tmp = x; \ + x = y; \ + y = tmp; \ + } while (0); if (maxIdx != sort->idx) { swap(sort->moves[sort->idx], sort->moves[maxIdx]); swap(sort->scores[sort->idx], sort->scores[maxIdx]); } - #undef swap +#undef swap const int score = sort->scores[sort->idx]; const move_t m = sort->moves[sort->idx]; @@ -117,11 +114,11 @@ move_t sort_next(Sort *sort, const Position *pos, int *see) // Deduce SEE from the sort score if (score >= SEPARATION) *see = score == INT_MAX - ? pos_see(pos, m) // special case: HT move is scored as INT_MAX - : score - SEPARATION; // Good captures are scored as SEE + SEPARATION + ? pos_see(pos, m) // special case: HT move is scored as INT_MAX + : score - SEPARATION; // Good captures are scored as SEE + SEPARATION else { assert(score < -SEPARATION); - *see = score + SEPARATION; // Bad captures are scored as SEE - SEPARATION + *see = score + SEPARATION; // Bad captures are scored as SEE - SEPARATION } assert(*see == pos_see(pos, m)); diff --git a/src/sort.h b/src/sort.h index e53d0ea..9c84f7f 100644 --- a/src/sort.h +++ b/src/sort.h @@ -11,7 +11,7 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once #include "gen.h" #include "workers.h" diff --git a/src/tune.c b/src/tune.c index 0fc8366..f93a3d7 100644 --- a/src/tune.c +++ b/src/tune.c @@ -11,83 +11,115 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ -#include -#include -#include + */ +#include "tune.h" #include "eval.h" #include "pst.h" #include "search.h" -#include "tune.h" #include "workers.h" +#include +#include +#include int PieceValue[NB_PIECE] = {640, 640, 1036, 1970, MATE, 169}; -eval_t KnightPstSeed[4+8] = { - {-56, -19}, {-32, -14}, {-15, -4}, {7, 4}, - {-31, -12}, {-11, -2}, {15, 1}, {33, 10}, {28, 9}, {13, 3}, {-9, -1}, {-37, -7} -}; +eval_t KnightPstSeed[4 + 8] = {{-56, -19}, {-32, -14}, {-15, -4}, {7, 4}, {-31, -12}, {-11, -2}, + {15, 1}, {33, 10}, {28, 9}, {13, 3}, {-9, -1}, {-37, -7}}; -eval_t RookPstSeed[4+8] = { - {-13, 0}, {-2, 3}, {2, 4}, {5, 4}, - {-6, -6}, {-2, -7}, {-2, -2}, {-6, -3}, {0, 0}, {-2, 0}, {13, 12}, {-1, -3} -}; +eval_t RookPstSeed[4 + 8] = {{-13, 0}, {-2, 3}, {2, 4}, {5, 4}, {-6, -6}, {-2, -7}, + {-2, -2}, {-6, -3}, {0, 0}, {-2, 0}, {13, 12}, {-1, -3}}; -eval_t QueenPstSeed[4+8] = { - {-3, -28}, {-4, -14}, {-2, -3}, {1, 2}, - {-6, -15}, {5, -8}, {4, 8}, {0, 17}, {-2, 13}, {2, 4}, {-2, -4}, {1, -13} -}; +eval_t QueenPstSeed[4 + 8] = {{-3, -28}, {-4, -14}, {-2, -3}, {1, 2}, {-6, -15}, {5, -8}, + {4, 8}, {0, 17}, {-2, 13}, {2, 4}, {-2, -4}, {1, -13}}; eval_t BishopPstSeed[8][4] = { - {{ -6,-2}, {-2, 2}, {-11, 4}, { 1,-5}}, - {{ -3,-3}, {15, 2}, { 9, 9}, {-9, 5}}, - {{ -2,-6}, { 8,-2}, { 3, 4}, {13, 3}}, - {{ 19,-7}, {-1, 7}, { 0, 0}, {-1, 4}}, - {{-11,-1}, {-2,-2}, { 0, 2}, {-2, 1}}, - {{ -3, 5}, {-4,-1}, { 1, 1}, { 0, 6}}, - {{ 2,-3}, {-5,-3}, { 3,-1}, {-5, 4}}, - {{ 1,10}, {-8, 1}, { 1, 4}, { 5,-3}} -}; - -eval_t KingPstSeed[8][4] = { - {{ 59,-138}, {91,-102}, { 59,-64}, { 26,-56}}, - {{ 54, -83}, {76, -53}, { 44,-17}, { 4, -3}}, - {{ 32, -69}, {50, -17}, { 7, 1}, {-46, 26}}, - {{ 17, -24}, {24, 2}, { 1, 23}, {-40, 43}}, - {{ 2, -27}, {15, 3}, {-19, 26}, {-53, 59}}, - {{ -8, -72}, {16, -33}, {-29, 1}, {-56, 20}}, - {{-16, -75}, {17, -39}, {-12,-29}, {-53, 1}}, - {{ -4,-119}, { 3,-115}, {-21,-57}, {-55,-27}} -}; + {{-6, -2}, {-2, 2}, {-11, 4}, {1, -5}}, {{-3, -3}, {15, 2}, {9, 9}, {-9, 5}}, + {{-2, -6}, {8, -2}, {3, 4}, {13, 3}}, {{19, -7}, {-1, 7}, {0, 0}, {-1, 4}}, + {{-11, -1}, {-2, -2}, {0, 2}, {-2, 1}}, {{-3, 5}, {-4, -1}, {1, 1}, {0, 6}}, + {{2, -3}, {-5, -3}, {3, -1}, {-5, 4}}, {{1, 10}, {-8, 1}, {1, 4}, {5, -3}}}; + +eval_t KingPstSeed[8][4] = {{{59, -138}, {91, -102}, {59, -64}, {26, -56}}, + {{54, -83}, {76, -53}, {44, -17}, {4, -3}}, + {{32, -69}, {50, -17}, {7, 1}, {-46, 26}}, + {{17, -24}, {24, 2}, {1, 23}, {-40, 43}}, + {{2, -27}, {15, 3}, {-19, 26}, {-53, 59}}, + {{-8, -72}, {16, -33}, {-29, 1}, {-56, 20}}, + {{-16, -75}, {17, -39}, {-12, -29}, {-53, 1}}, + {{-4, -119}, {3, -115}, {-21, -57}, {-55, -27}}}; eval_t PawnPstSeed[6][4] = { - {{-5,-4}, { 8, 3}, { 7, 4}, {-5, 6}}, - {{-9,-4}, { 3,-1}, {-5, 0}, {11,-1}}, - {{-8, 6}, { 3, 0}, { 5,-1}, {39,-6}}, - {{ 2, 3}, {-1, 4}, { 4, 3}, {22,-1}}, - {{-1, 8}, {12, 3}, {-9,-4}, {-2, 5}}, - {{-6,-3}, { 2, 1}, {-4,-2}, { 2, 1}}, + {{-5, -4}, {8, 3}, {7, 4}, {-5, 6}}, {{-9, -4}, {3, -1}, {-5, 0}, {11, -1}}, + {{-8, 6}, {3, 0}, {5, -1}, {39, -6}}, {{2, 3}, {-1, 4}, {4, 3}, {22, -1}}, + {{-1, 8}, {12, 3}, {-9, -4}, {-2, 5}}, {{-6, -3}, {2, 1}, {-4, -2}, {2, 1}}, }; eval_t Mobility[5][15] = { // Knight - {{-38, -48}, {-18, -30}, {-15, -21}, {4, 0}, {9, 16}, {20, 34}, {34, 28}, {35, 46}, - {29, 47}}, + {{-38, -48}, {-18, -30}, {-15, -21}, {4, 0}, {9, 16}, {20, 34}, {34, 28}, {35, 46}, {29, 47}}, // Bishop - {{-66, -70}, {-42, -35}, {-16, -30}, {-7, -19}, {12, -2}, {24, 16}, {37, 33}, {27, 50}, - {41, 55}, {45, 65}, {55, 37}, {71, 65}, {71, 56}, {120, 95}}, - // Rook - {{-60, -46}, {-33, -51}, {-19, -28}, {-6, -22}, {-5, -3}, {-5, 1}, {9, 2}, {19, 5}, - {27, 19}, {41, 38}, {57, 33}, {56, 40}, {48, 43}, {57, 48}, {72, 56}}, + {{-66, -70}, + {-42, -35}, + {-16, -30}, + {-7, -19}, + {12, -2}, + {24, 16}, + {37, 33}, + {27, 50}, + {41, 55}, + {45, 65}, + {55, 37}, + {71, 65}, + {71, 56}, + {120, 95}}, + // Rook + {{-60, -46}, + {-33, -51}, + {-19, -28}, + {-6, -22}, + {-5, -3}, + {-5, 1}, + {9, 2}, + {19, 5}, + {27, 19}, + {41, 38}, + {57, 33}, + {56, 40}, + {48, 43}, + {57, 48}, + {72, 56}}, // Queen diagonal - {{-27, -41}, {-9, -29}, {-12, -4}, {-4, -8}, {0, -1}, {11, 13}, {11, 23}, {17, 41}, - {13, 13}, {23, 47}, {28, 50}, {34, 24}, {16, 32}, {24, 87}}, + {{-27, -41}, + {-9, -29}, + {-12, -4}, + {-4, -8}, + {0, -1}, + {11, 13}, + {11, 23}, + {17, 41}, + {13, 13}, + {23, 47}, + {28, 50}, + {34, 24}, + {16, 32}, + {24, 87}}, // Queen orthogonal - {{-24, -64}, {-19, -25}, {-16, -11}, {-9, -4}, {-2, -10}, {4, -10}, {2, 16}, {9, 14}, - {13, 14}, {21, 26}, {8, 37}, {21, 43}, {25, 48}, {19, 45}, {24, 54}} -}; - -int RookOpen[2] = {20, 33}; // 0: semi-open, 1: fully-open + {{-24, -64}, + {-19, -25}, + {-16, -11}, + {-9, -4}, + {-2, -10}, + {4, -10}, + {2, 16}, + {9, 14}, + {13, 14}, + {21, 26}, + {8, 37}, + {21, 43}, + {25, 48}, + {19, 45}, + {24, 54}}}; + +int RookOpen[2] = {20, 33}; // 0: semi-open, 1: fully-open eval_t BishopPair = {77, 122}; int Ahead = 20; int Hanging[NB_PIECE] = {119, 71, 118, 233, 0, 42}; @@ -102,12 +134,10 @@ int SafetyCurveParam[2] = {1062, 800}; eval_t Isolated[2] = {{15, 25}, {41, 25}}; eval_t Backward[2] = {{12, 18}, {37, 15}}; eval_t Doubled = {28, 36}; -int Shield[4][6] = { - {21, 19, 8, 12, 17, 11}, - {31, 23, 5, 7, 5, 3}, - {25, 17, 12, 14, 17, 13}, - {25, 18, 16, 10, 7, 6} -}; +int Shield[4][6] = {{21, 19, 8, 12, 17, 11}, + {31, 23, 5, 7, 5, 3}, + {25, 17, 12, 14, 17, 13}, + {25, 18, 16, 10, 7, 6}}; eval_t Connected[] = {{8, -4}, {18, 2}, {20, 7}, {42, 21}, {34, 58}, {48, 68}}; int Distance[2] = {9, 9}; @@ -115,7 +145,7 @@ eval_t PasserBonus[6] = {{-3, 7}, {2, 15}, {18, 20}, {57, 61}, {150, 155}, {264, int PasserAdjust[6] = {-1, 4, 12, 50, 72, 91}; int FreePasser[4] = {13, 14, 35, 97}; -enum {NAME_MAX_CHAR = 64}; +enum { NAME_MAX_CHAR = 64 }; typedef struct { char name[NAME_MAX_CHAR]; @@ -123,59 +153,55 @@ typedef struct { int count; } Entry; -Entry Entries[] = { - {"PieceValue", PieceValue, NB_PIECE}, +Entry Entries[] = {{"PieceValue", PieceValue, NB_PIECE}, - {"KnightPstSeed", KnightPstSeed, 12 * 2}, - {"RookPstSeed", RookPstSeed, 12 * 2}, - {"QueenPstSeed", QueenPstSeed, 12 * 2}, + {"KnightPstSeed", KnightPstSeed, 12 * 2}, + {"RookPstSeed", RookPstSeed, 12 * 2}, + {"QueenPstSeed", QueenPstSeed, 12 * 2}, - {"BishopPstSeed", BishopPstSeed, 8 * 4 * 2}, - {"KingPstSeed", KingPstSeed, 8 * 4 * 2}, - {"PawnPstSeed", PawnPstSeed, 6 * 4 * 2}, + {"BishopPstSeed", BishopPstSeed, 8 * 4 * 2}, + {"KingPstSeed", KingPstSeed, 8 * 4 * 2}, + {"PawnPstSeed", PawnPstSeed, 6 * 4 * 2}, - {"MobilityKnight", Mobility[KNIGHT], 9 * 2}, - {"MobilityBishop", Mobility[BISHOP], 14 * 2}, - {"MobilityRook", Mobility[ROOK], 15 * 2}, - {"MobilityQueenDiagonal", Mobility[QUEEN], 14 * 2}, - {"MobilityQueenOrthogonal", Mobility[QUEEN + 1], 15 * 2}, + {"MobilityKnight", Mobility[KNIGHT], 9 * 2}, + {"MobilityBishop", Mobility[BISHOP], 14 * 2}, + {"MobilityRook", Mobility[ROOK], 15 * 2}, + {"MobilityQueenDiagonal", Mobility[QUEEN], 14 * 2}, + {"MobilityQueenOrthogonal", Mobility[QUEEN + 1], 15 * 2}, - {"RookOpen", RookOpen, 2}, - {"BishopPair", &BishopPair, 2}, - {"Ahead", &Ahead, 1}, + {"RookOpen", RookOpen, 2}, + {"BishopPair", &BishopPair, 2}, + {"Ahead", &Ahead, 1}, - {"Hanging", Hanging, NB_PIECE}, + {"Hanging", Hanging, NB_PIECE}, - {"RingAttack", RingAttack, NB_PIECE}, - {"RingDefense", RingDefense, NB_PIECE}, - {"CheckAttack", CheckAttack, 4}, - {"CheckDefense", CheckDefense, 4}, - {"XRay", &XRay[BISHOP], 3}, - {"SafetyCurveParam", SafetyCurveParam, 2}, + {"RingAttack", RingAttack, NB_PIECE}, + {"RingDefense", RingDefense, NB_PIECE}, + {"CheckAttack", CheckAttack, 4}, + {"CheckDefense", CheckDefense, 4}, + {"XRay", &XRay[BISHOP], 3}, + {"SafetyCurveParam", SafetyCurveParam, 2}, - {"Isolated", Isolated, 2 * 2}, - {"Backward", Backward, 2 * 2}, - {"Doubled", &Doubled, 2}, - {"Shield", Shield, 4 * 6}, + {"Isolated", Isolated, 2 * 2}, + {"Backward", Backward, 2 * 2}, + {"Doubled", &Doubled, 2}, + {"Shield", Shield, 4 * 6}, - {"Connected", Connected, 6 * 2}, - {"Distance", Distance, 2}, + {"Connected", Connected, 6 * 2}, + {"Distance", Distance, 2}, - {"PasserBonus", PasserBonus, 6 * 2}, - {"PasserAdjust", PasserAdjust, 6}, - {"FreePasser", FreePasser, 4} -}; + {"PasserBonus", PasserBonus, 6 * 2}, + {"PasserAdjust", PasserAdjust, 6}, + {"FreePasser", FreePasser, 4}}; -void tune_declare() -{ +void tune_declare() { for (size_t i = 0; i < sizeof(Entries) / sizeof(Entry); i++) for (int j = 0; j < Entries[i].count; j++) - printf("option name %s_%d type spin default %d min %d max %d\n", - Entries[i].name, j, ((int *)Entries[i].values)[j], -1000000, 1000000); + printf("option name %s_%d type spin default %d min %d max %d\n", Entries[i].name, j, + ((int *)Entries[i].values)[j], -1000000, 1000000); } -void tune_parse(const char *fullName, int value) -{ +void tune_parse(const char *fullName, int value) { // split fullName = "fooBar_12", into name = "fooBar" and idx = 12 char name[NAME_MAX_CHAR]; int idx; @@ -189,8 +215,7 @@ void tune_parse(const char *fullName, int value) ((int *)Entries[i].values)[idx] = value; } -void tune_refresh() -{ +void tune_refresh() { search_init(); pst_init(); eval_init(); @@ -204,10 +229,9 @@ typedef struct { static Sample *samples = NULL; static size_t sampleCount = 0; -extern const int Tempo; // in search.c +extern const int Tempo; // in search.c -void tune_load(const char *fileName) -{ +void tune_load(const char *fileName) { size_t allocated = 1024; samples = malloc(allocated * sizeof(Sample)); @@ -234,21 +258,18 @@ void tune_load(const char *fileName) printf("loaded %zu samples from '%s'\n", sampleCount, fileName); } -void tune_free() -{ +void tune_free() { free(samples); samples = NULL; sampleCount = 0; } -void tune_param_list(void) -{ +void tune_param_list(void) { for (size_t i = 0; i < sizeof(Entries) / sizeof(Entry); i++) puts(Entries[i].name); } -void tune_param_get(const char *name) -{ +void tune_param_get(const char *name) { for (size_t i = 0; i < sizeof(Entries) / sizeof(Entry); i++) if (!strcmp(Entries[i].name, name)) { for (int j = 0; j < Entries[i].count; j++) @@ -258,8 +279,7 @@ void tune_param_get(const char *name) } } -void tune_param_set(const char *name, const char *values) -{ +void tune_param_set(const char *name, const char *values) { for (size_t i = 0; i < sizeof(Entries) / sizeof(Entry); i++) if (!strcmp(Entries[i].name, name)) { char *copy = strdup(values), *pos = NULL; @@ -274,8 +294,7 @@ void tune_param_set(const char *name, const char *values) } // Calculate evals[] for the samples[] -static int16_t *tune_run_evals(void) -{ +static int16_t *tune_run_evals(void) { // Make sure there is no persistance workers_clear(); tune_refresh(); @@ -291,8 +310,7 @@ static int16_t *tune_run_evals(void) return evals; } -static double tune_logit_err(const int16_t *evals, double lambda) -{ +static double tune_logit_err(const int16_t *evals, double lambda) { double sumErr = 0; for (size_t i = 0; i < sampleCount; i++) @@ -303,18 +321,17 @@ static double tune_logit_err(const int16_t *evals, double lambda) return err; } -double tune_logitreg(const char *strLambda) -{ +double tune_logitreg(const char *strLambda) { int16_t *evals = tune_run_evals(); - const double lambda = strLambda ? atof(strLambda) : 0.00515; // FIXME? tune lambda (newton raphson) + const double lambda = + strLambda ? atof(strLambda) : 0.00515; // FIXME? tune lambda (newton raphson) const double err = tune_logit_err(evals, lambda); free(evals); return err; } // Fit y = alpha + beta.x; x = samples[].eval, y = evals[] -double tune_linereg() -{ +double tune_linereg() { int16_t *evals = tune_run_evals(); int64_t sum_y = 0, sum_x = 0; @@ -341,22 +358,21 @@ double tune_linereg() sum_err += fabs(alpha + beta * samples[i].eval - evals[i]); const double mean_err = sum_err / sampleCount; - printf("evals[] = %f + %f * samples[].eval + err;\tmean(|err|) = %f\n", alpha, beta, - mean_err); + printf("evals[] = %f + %f * samples[].eval + err;\tmean(|err|) = %f\n", alpha, beta, mean_err); free(evals); return mean_err; } -void tune_param_fit(const char *name, int nbIter) -{ +void tune_param_fit(const char *name, int nbIter) { double best = tune_logitreg(NULL); for (size_t i = 0; i < sizeof(Entries) / sizeof(Entry); i++) if (!strcmp(Entries[i].name, name)) { // Initialise firstBump[j] int firstBump[Entries[i].count]; - for (int j = 0; j < Entries[i].count; firstBump[j++] = -1); + for (int j = 0; j < Entries[i].count; firstBump[j++] = -1) + ; for (int it = 0; it < nbIter; it++) { for (int j = 0; j < Entries[i].count; j++) @@ -376,7 +392,7 @@ void tune_param_fit(const char *name, int nbIter) // Success: establish new best printf("new best: %s=", name); tune_param_get(name); - firstBump[j] = bump; // try the winner first in next iteration + firstBump[j] = bump; // try the winner first in next iteration best = new; break; } diff --git a/src/tune.h b/src/tune.h index 67f0991..3804891 100644 --- a/src/tune.h +++ b/src/tune.h @@ -11,13 +11,13 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once #include "types.h" extern int PieceValue[NB_PIECE]; -extern eval_t KnightPstSeed[4+8], RookPstSeed[4+8], QueenPstSeed[4+8]; +extern eval_t KnightPstSeed[4 + 8], RookPstSeed[4 + 8], QueenPstSeed[4 + 8]; extern eval_t BishopPstSeed[8][4], KingPstSeed[8][4], PawnPstSeed[6][4]; extern eval_t Mobility[5][15]; diff --git a/src/types.c b/src/types.c index b76bb69..2c65006 100644 --- a/src/types.c +++ b/src/types.c @@ -11,91 +11,71 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #include "types.h" int64_t dbgCnt[2] = {0, 0}; -void eval_add(eval_t *e1, eval_t e2) -{ +void eval_add(eval_t *e1, eval_t e2) { e1->op += e2.op; e1->eg += e2.eg; } -void eval_sub(eval_t *e1, eval_t e2) -{ +void eval_sub(eval_t *e1, eval_t e2) { e1->op -= e2.op; e1->eg -= e2.eg; } -bool eval_eq(eval_t e1, eval_t e2) -{ - return e1.op == e2.op && e1.eg == e2.eg; -} +bool eval_eq(eval_t e1, eval_t e2) { return e1.op == e2.op && e1.eg == e2.eg; } -int opposite(int color) -{ +int opposite(int color) { BOUNDS(color, NB_COLOR); - return color ^ BLACK; // branchless for: color == WHITE ? BLACK : WHITE + return color ^ BLACK; // branchless for: color == WHITE ? BLACK : WHITE } -int push_inc(int color) -{ +int push_inc(int color) { BOUNDS(color, NB_COLOR); - return UP - color * (UP - DOWN); // branchless for: color == WHITE ? UP : DOWN + return UP - color * (UP - DOWN); // branchless for: color == WHITE ? UP : DOWN } -int square_from(int rank, int file) -{ +int square_from(int rank, int file) { BOUNDS(rank, NB_RANK); BOUNDS(file, NB_FILE); return NB_FILE * rank + file; } -int rank_of(int square) -{ +int rank_of(int square) { BOUNDS(square, NB_SQUARE); return square / NB_FILE; } -int file_of(int square) -{ +int file_of(int square) { BOUNDS(square, NB_SQUARE); return square % NB_FILE; } -int relative_rank(int color, int rank) -{ +int relative_rank(int color, int rank) { BOUNDS(color, NB_COLOR); BOUNDS(rank, NB_RANK); - return rank ^ (RANK_8 * color); // branchless for: color == WHITE ? rank : RANK_8 - rank + return rank ^ (RANK_8 * color); // branchless for: color == WHITE ? rank : RANK_8 - rank } -int relative_rank_of(int color, int square) -{ +int relative_rank_of(int color, int square) { BOUNDS(square, NB_SQUARE); return relative_rank(color, rank_of(square)); } -int move_from(move_t m) -{ - return m & 077; -} +int move_from(move_t m) { return m & 077; } -int move_to(move_t m) -{ - return (m >> 6) & 077; -} +int move_to(move_t m) { return (m >> 6) & 077; } -int move_prom(move_t m) -{ +int move_prom(move_t m) { const unsigned prom = m >> 12; assert(prom <= QUEEN || prom == NB_PIECE); return (int)prom; } -move_t move_build(int from, int to, int prom) -{ +move_t move_build(int from, int to, int prom) { BOUNDS(from, NB_SQUARE); BOUNDS(to, NB_SQUARE); assert((unsigned)prom <= QUEEN || prom == NB_PIECE); diff --git a/src/types.h b/src/types.h index f8e0b52..15c444e 100644 --- a/src/types.h +++ b/src/types.h @@ -11,7 +11,7 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once #include #include @@ -20,35 +20,93 @@ #define BOUNDS(v, ub) assert((unsigned)(v) < (ub)) -#define min(x, y) ({ \ - typeof(x) _x = (x), _y = (y); \ - _x < _y ? _x : _y; \ -}) +#define min(x, y) \ + ({ \ + typeof(x) _x = (x), _y = (y); \ + _x < _y ? _x : _y; \ + }) -#define max(x, y) ({ \ - typeof(x) _x = (x), _y = (y); \ - _x > _y ? _x : _y; \ -}) +#define max(x, y) \ + ({ \ + typeof(x) _x = (x), _y = (y); \ + _x > _y ? _x : _y; \ + }) -enum {RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, NB_RANK}; -enum {FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, NB_FILE}; +enum { RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, NB_RANK }; +enum { FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, NB_FILE }; enum { - A1, B1, C1, D1, E1, F1, G1, H1, - A2, B2, C2, D2, E2, F2, G2, H2, - A3, B3, C3, D3, E3, F3, G3, H3, - A4, B4, C4, D4, E4, F4, G4, H4, - A5, B5, C5, D5, E5, F5, G5, H5, - A6, B6, C6, D6, E6, F6, G6, H6, - A7, B7, C7, D7, E7, F7, G7, H7, - A8, B8, C8, D8, E8, F8, G8, H8, + A1, + B1, + C1, + D1, + E1, + F1, + G1, + H1, + A2, + B2, + C2, + D2, + E2, + F2, + G2, + H2, + A3, + B3, + C3, + D3, + E3, + F3, + G3, + H3, + A4, + B4, + C4, + D4, + E4, + F4, + G4, + H4, + A5, + B5, + C5, + D5, + E5, + F5, + G5, + H5, + A6, + B6, + C6, + D6, + E6, + F6, + G6, + H6, + A7, + B7, + C7, + D7, + E7, + F7, + G7, + H7, + A8, + B8, + C8, + D8, + E8, + F8, + G8, + H8, NB_SQUARE }; -enum {UP = 8, DOWN = -8, LEFT = -1, RIGHT = 1}; +enum { UP = 8, DOWN = -8, LEFT = -1, RIGHT = 1 }; -enum {WHITE, BLACK, NB_COLOR}; -enum {KNIGHT, BISHOP, ROOK, QUEEN, KING, PAWN, NB_PIECE}; +enum { WHITE, BLACK, NB_COLOR }; +enum { KNIGHT, BISHOP, ROOK, QUEEN, KING, PAWN, NB_PIECE }; // Evaluation in 2D (opening, endgame) typedef struct { diff --git a/src/uci.c b/src/uci.c index fd3d2f6..206627f 100644 --- a/src/uci.c +++ b/src/uci.c @@ -11,10 +11,8 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ -#include -#include -#include + */ +#include "uci.h" #include "bitboard.h" #include "eval.h" #include "gen.h" @@ -22,7 +20,9 @@ #include "position.h" #include "search.h" #include "tune.h" -#include "uci.h" +#include +#include +#include #define uci_printf(...) printf(__VA_ARGS__), fflush(stdout) #define uci_puts(str) puts(str), fflush(stdout) @@ -33,22 +33,21 @@ size_t uciHash = 2; int64_t uciTimeBuffer = 60; bool uciChess960 = false; -static void uci_format_score(int score, char str[17]) -{ +static void uci_format_score(int score, char str[17]) { if (is_mate_score(score)) sprintf(str, "mate %d", score > 0 ? (MATE - score + 1) / 2 : -(score + MATE + 1) / 2); else sprintf(str, "cp %d", score / 2); } -static void intro(void) -{ +static void intro(void) { uci_puts("id name Demolito " VERSION "\nid author lucasart"); uci_printf("option name Contempt type spin default %d min -100 max 100\n", Contempt); uci_printf("option name Hash type spin default %zu min 1 max 1048576\n", uciHash); uci_puts("option name Ponder type check default false"); uci_printf("option name Threads type spin default %zu min 1 max 256\n", WorkersCount); - uci_printf("option name Time Buffer type spin default %" PRId64 " min 0 max 1000\n", uciTimeBuffer); + uci_printf("option name Time Buffer type spin default %" PRId64 " min 0 max 1000\n", + uciTimeBuffer); uci_printf("option name UCI_Chess960 type check default %s\n", uciChess960 ? "true" : "false"); #ifdef TUNE tune_declare(); @@ -56,8 +55,7 @@ static void intro(void) uci_puts("uciok"); } -static void setoption(char **linePos) -{ +static void setoption(char **linePos) { const char *token = strtok_r(NULL, " \n", linePos); char name[32] = ""; @@ -73,7 +71,7 @@ static void setoption(char **linePos) uciChess960 = !strcmp(token, "true"); else if (!strcmp(name, "Hash")) { uciHash = (size_t)atoll(token); - uciHash = 1ULL << bb_msb(uciHash); // must be a power of two + uciHash = 1ULL << bb_msb(uciHash); // must be a power of two hash_prepare(uciHash); } else if (!strcmp(name, "Threads")) workers_prepare((size_t)atoll(token)); @@ -88,8 +86,7 @@ static void setoption(char **linePos) } } -static void position(char **linePos) -{ +static void position(char **linePos) { Position pos[NB_COLOR]; int idx = 0; @@ -98,7 +95,7 @@ static void position(char **linePos) if (!strcmp(token, "startpos")) { strcpy(fen, "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); - strtok_r(NULL, " \n", linePos); // consume "moves" token (if present) + strtok_r(NULL, " \n", linePos); // consume "moves" token (if present) } else if (!strcmp(token, "fen")) { while ((token = strtok_r(NULL, " \n", linePos)) && strcmp(token, "moves")) strcat(strcat(fen, token), " "); @@ -112,7 +109,7 @@ static void position(char **linePos) // Parse moves (if any) while ((token = strtok_r(NULL, " \n", linePos))) { move_t m = pos_string_to_move(&pos[idx], token); - pos_move(&pos[idx^1], &pos[idx], m); + pos_move(&pos[idx ^ 1], &pos[idx], m); idx ^= 1; zobrist_push(&rootStack, pos[idx].key); } @@ -120,8 +117,7 @@ static void position(char **linePos) rootPos = pos[idx]; } -static void go(char **linePos) -{ +static void go(char **linePos) { lim = (Limits){0}; lim.depth = MAX_DEPTH; @@ -136,11 +132,11 @@ static void go(char **linePos) lim.movetime = atoll(strtok_r(NULL, " \n", linePos)); else if (!strcmp(token, "movestogo")) lim.movestogo = atoi(strtok_r(NULL, " \n", linePos)); - else if ((rootPos.turn == WHITE && !strcmp(token, "wtime")) - || (rootPos.turn == BLACK && !strcmp(token, "btime"))) + else if ((rootPos.turn == WHITE && !strcmp(token, "wtime")) || + (rootPos.turn == BLACK && !strcmp(token, "btime"))) lim.time = atoll(strtok_r(NULL, " \n", linePos)); - else if ((rootPos.turn == WHITE && !strcmp(token, "winc")) - || (rootPos.turn == BLACK && !strcmp(token, "binc"))) + else if ((rootPos.turn == WHITE && !strcmp(token, "winc")) || + (rootPos.turn == BLACK && !strcmp(token, "binc"))) lim.inc = atoll(strtok_r(NULL, " \n", linePos)); else if (!strcmp(token, "infinite") || !strcmp(token, "ponder")) lim.infinite = true; @@ -154,15 +150,13 @@ static void go(char **linePos) pthread_create(&Timer, NULL, search_posix, NULL); } -static void eval(void) -{ +static void eval(void) { char str[17]; uci_format_score(evaluate(&Workers[0], &rootPos), str); uci_printf("score %s\n", str); } -static void perft(char **linePos) -{ +static void perft(char **linePos) { const int depth = atoi(strtok_r(NULL, " \n", linePos)); const char *last = strtok_r(NULL, " \n", linePos); uci_printf("%" PRIu64 "\n", gen_perft(&rootPos, depth, !last || strcmp(last, "div"))); @@ -170,8 +164,7 @@ static void perft(char **linePos) Info ui; -void uci_loop() -{ +void uci_loop() { char line[8192], *linePos; while (fgets(line, 8192, stdin)) { @@ -198,7 +191,7 @@ void uci_loop() lim.infinite = false; Stop = true; } else if (!strcmp(token, "ponderhit")) - lim.infinite = false; // switch from pondering to normal search + lim.infinite = false; // switch from pondering to normal search else if (!strcmp(token, "d")) pos_print(&rootPos); else if (!strcmp(token, "eval")) @@ -234,8 +227,7 @@ void uci_loop() } } -void info_create(Info *info) -{ +void info_create(Info *info) { info->lastDepth = 0; info->variability = 0.5; info->best = info->ponder = 0; @@ -243,13 +235,9 @@ void info_create(Info *info) mtx_init(&info->mtx, mtx_plain); } -void info_destroy(Info *info) -{ - mtx_destroy(&info->mtx); -} +void info_destroy(Info *info) { mtx_destroy(&info->mtx); } -void info_update(Info *info, int depth, int score, uint64_t nodes, move_t pv[], bool partial) -{ +void info_update(Info *info, int depth, int score, uint64_t nodes, move_t pv[], bool partial) { mtx_lock(&info->mtx); if (depth > info->lastDepth) { @@ -257,7 +245,7 @@ void info_update(Info *info, int depth, int score, uint64_t nodes, move_t pv[], char str[17]; uci_format_score(score, str); uci_printf("info depth %d score %s time %" PRId64 " nodes %" PRIu64 " hashfull %d pv", - depth, str, system_msec() - info->start, nodes, hash_permille()); + depth, str, system_msec() - info->start, nodes, hash_permille()); // Pring the moves. Because of e1g1 notation when Chess960 = false, we need to play the PV // to print it correctly. This is a design flaw of the UCI protocol, which should have @@ -278,22 +266,20 @@ void info_update(Info *info, int depth, int score, uint64_t nodes, move_t pv[], // Update variability depending on whether the bestmove has changed or is confirmed // - changed: increase variability (rescale for %age of partial updates = f(threads)) // - confirmed: reduce variability (discard partial) - info->variability += info->best != pv[0] - ? 0.6 * pow(WorkersCount, -0.08) - : -0.24 * !partial; + info->variability += + info->best != pv[0] ? 0.6 * pow(WorkersCount, -0.08) : -0.24 * !partial; if (!partial) info->lastDepth = depth; info->best = pv[0]; - info->ponder = pv[1]; // May be zero (not a bug, inevitable consequence of partial updates) + info->ponder = pv[1]; // May be zero (not a bug, inevitable consequence of partial updates) } mtx_unlock(&info->mtx); } -void info_print_bestmove(Info *info) -{ +void info_print_bestmove(Info *info) { mtx_lock(&info->mtx); char best[6]; @@ -311,8 +297,7 @@ void info_print_bestmove(Info *info) mtx_unlock(&info->mtx); } -move_t info_best(Info *info) -{ +move_t info_best(Info *info) { mtx_lock(&info->mtx); const move_t best = info->best; mtx_unlock(&info->mtx); @@ -320,8 +305,7 @@ move_t info_best(Info *info) return best; } -int info_last_depth(Info *info) -{ +int info_last_depth(Info *info) { mtx_lock(&info->mtx); const int lastDepth = info->lastDepth; mtx_unlock(&info->mtx); @@ -329,8 +313,7 @@ int info_last_depth(Info *info) return lastDepth; } -double info_variability(Info *info) -{ +double info_variability(Info *info) { mtx_lock(&info->mtx); const double variability = info->variability; mtx_unlock(&info->mtx); diff --git a/src/uci.h b/src/uci.h index 25d5336..09b0734 100644 --- a/src/uci.h +++ b/src/uci.h @@ -11,7 +11,7 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once #include "platform.h" #include "position.h" diff --git a/src/util.c b/src/util.c index ba00b8f..b52d706 100644 --- a/src/util.c +++ b/src/util.c @@ -11,20 +11,18 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ -#include + */ #include "util.h" +#include -static uint64_t hash_mix(uint64_t block) -{ +static uint64_t hash_mix(uint64_t block) { block ^= block >> 23; block *= 0x2127599bf4325c37ULL; return block ^= block >> 47; } // SplitMix64 PRNG, based on http://xoroshiro.di.unimi.it/splitmix64.c -uint64_t prng(uint64_t *state) -{ +uint64_t prng(uint64_t *state) { uint64_t rnd = (*state += 0x9E3779B97F4A7C15); rnd = (rnd ^ (rnd >> 30)) * 0xBF58476D1CE4E5B9; rnd = (rnd ^ (rnd >> 27)) * 0x94D049BB133111EB; @@ -32,16 +30,14 @@ uint64_t prng(uint64_t *state) return rnd; } -void hash_block(uint64_t block, uint64_t *hash) -{ +void hash_block(uint64_t block, uint64_t *hash) { *hash ^= hash_mix(block); *hash *= 0x880355f21e6d1965ULL; } // Based on FastHash64, without length hashing, to make it capable of incremental updates. Assumes // length is divisible by 8, and buffer is 8-byte aligned. -void hash_blocks(const void *buffer, size_t length, uint64_t *hash) -{ +void hash_blocks(const void *buffer, size_t length, uint64_t *hash) { assert((uintptr_t)buffer % 8 == 0 && length % 8 == 0); const uint64_t *blocks = (const uint64_t *)buffer; diff --git a/src/util.h b/src/util.h index 6e1abde..cda31bd 100644 --- a/src/util.h +++ b/src/util.h @@ -11,7 +11,7 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once #include #include diff --git a/src/workers.c b/src/workers.c index 4d1bce2..036d94c 100644 --- a/src/workers.c +++ b/src/workers.c @@ -11,42 +11,35 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ -#include + */ #include "workers.h" #include "search.h" +#include Worker *Workers = NULL; size_t WorkersCount = 1; -static void __attribute__((destructor)) workers_free(void) -{ - free(Workers); -} +static void __attribute__((destructor)) workers_free(void) { free(Workers); } -void workers_clear() -{ +void workers_clear() { for (size_t i = 0; i < WorkersCount; i++) Workers[i] = (Worker){0}; } -void workers_prepare(size_t count) -{ +void workers_prepare(size_t count) { Workers = realloc(Workers, count * sizeof(Worker)); WorkersCount = count; workers_clear(); } -void workers_new_search() -{ +void workers_new_search() { for (size_t i = 0; i < WorkersCount; i++) { Workers[i].stack = rootStack; Workers[i].nodes = 0; } } -uint64_t workers_nodes() -{ +uint64_t workers_nodes() { uint64_t total = 0; for (size_t i = 0; i < WorkersCount; i++) diff --git a/src/workers.h b/src/workers.h index f9c4a0a..eb34daf 100644 --- a/src/workers.h +++ b/src/workers.h @@ -11,18 +11,14 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once -#include #include "bitboard.h" -#include "zobrist.h" #include "search.h" +#include "zobrist.h" +#include -enum { - NB_PAWN_HASH = 16384, - NB_REFUTATION = 1024, - NB_FOLLOW_UP = 1024 -}; +enum { NB_PAWN_HASH = 16384, NB_REFUTATION = 1024, NB_FOLLOW_UP = 1024 }; typedef struct { uint64_t key; @@ -45,7 +41,7 @@ extern Worker *Workers; extern size_t WorkersCount; void workers_clear(void); -void workers_prepare(size_t count); // realloc + clear +void workers_prepare(size_t count); // realloc + clear void workers_new_search(void); uint64_t workers_nodes(void); diff --git a/src/zobrist.c b/src/zobrist.c index ff11daa..38e6b80 100644 --- a/src/zobrist.c +++ b/src/zobrist.c @@ -11,18 +11,17 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ +#include "zobrist.h" #include "gen.h" #include "util.h" -#include "zobrist.h" uint64_t ZobristKey[NB_COLOR][NB_PIECE][NB_SQUARE]; uint64_t ZobristCastling[NB_SQUARE]; uint64_t ZobristEnPassant[NB_SQUARE + 1]; uint64_t ZobristTurn; -static __attribute__((constructor)) void zobrist_init(void) -{ +static __attribute__((constructor)) void zobrist_init(void) { uint64_t state = 0; for (int color = WHITE; color <= BLACK; color++) @@ -39,8 +38,7 @@ static __attribute__((constructor)) void zobrist_init(void) ZobristTurn = prng(&state); } -uint64_t zobrist_castling(bitboard_t castleRooks) -{ +uint64_t zobrist_castling(bitboard_t castleRooks) { bitboard_t k = 0; while (castleRooks) @@ -49,39 +47,29 @@ uint64_t zobrist_castling(bitboard_t castleRooks) return k; } -void zobrist_clear(ZobristStack *st) -{ - st->idx = 0; -} +void zobrist_clear(ZobristStack *st) { st->idx = 0; } -void zobrist_push(ZobristStack *st, uint64_t key) -{ +void zobrist_push(ZobristStack *st, uint64_t key) { assert(0 <= st->idx && st->idx < MAX_GAME_PLY); st->keys[st->idx++] = key; } -void zobrist_pop(ZobristStack *st) -{ +void zobrist_pop(ZobristStack *st) { assert(0 < st->idx && st->idx <= MAX_GAME_PLY); st->idx--; } -uint64_t zobrist_back(const ZobristStack *st) -{ +uint64_t zobrist_back(const ZobristStack *st) { assert(0 < st->idx && st->idx <= MAX_GAME_PLY); return st->keys[st->idx - 1]; } -uint64_t zobrist_move_key(const ZobristStack *st, int back) -{ +uint64_t zobrist_move_key(const ZobristStack *st, int back) { assert(0 < st->idx && st->idx <= MAX_GAME_PLY); - return st->idx - 1 - back > 0 - ? st->keys[st->idx - 1 - back] ^ st->keys[st->idx - 2 - back] - : 0; + return st->idx - 1 - back > 0 ? st->keys[st->idx - 1 - back] ^ st->keys[st->idx - 2 - back] : 0; } -bool zobrist_repetition(const ZobristStack *st, const Position *pos) -{ +bool zobrist_repetition(const ZobristStack *st, const Position *pos) { // 50 move rule if (pos->rule50 >= 100) { // If're not mated here, it's draw diff --git a/src/zobrist.h b/src/zobrist.h index 9899aaa..a3f9ece 100644 --- a/src/zobrist.h +++ b/src/zobrist.h @@ -11,11 +11,11 @@ * * You should have received a copy of the GNU General Public License along with this program. If * not, see . -*/ + */ #pragma once #include "position.h" -enum {MAX_GAME_PLY = 2048}; +enum { MAX_GAME_PLY = 2048 }; typedef struct { uint64_t keys[MAX_GAME_PLY];