5 Commits

Author SHA1 Message Date
8d46ff9829 move-do: use castling flags, other improvements (~12% on perft)
- always set K square and castling flags (remove expensive tests on K and castling flags)
- simplify castling rook move
- more restrictive sq_castle[]
2024-09-12 08:37:30 +02:00
8bf4262e11 position: use XOR in square clr/set, for castling flags to 8 bits 2024-09-11 08:14:55 +02:00
9dd7c7064d perft-test: improve displayed info, add selective tests
- print total #tests, #tests to do and VERSION string
- add starting point for tests, and #tests to run
2024-09-11 08:09:40 +02:00
7e65fbc205 change some bit macros, fix pst_init bug (thx gcc!), TT mask 64 bits 2024-09-04 20:07:19 +02:00
cf77a78aa6 Makefile: Fix dev/debug flags (issue with -Og & -g) 2024-09-04 20:01:18 +02:00
13 changed files with 188 additions and 127 deletions

View File

@@ -126,7 +126,7 @@ else # ifeq ($(build),dev)
# fen.c
#CPPFLAGS += -DDEBUG_FEN # FEN decoding
# hash / TT
#CPPFLAGS += -DZOBRIST_VERIFY # double chk zobrist
CPPFLAGS += -DZOBRIST_VERIFY # double chk zobrist
#CPPFLAGS += -DPERFT_MOVE_HISTORY # perft, keep prev moves
# attack.c
#CPPFLAGS += -DDEBUG_ATTACK_ATTACKERS # sq_attackers
@@ -159,22 +159,22 @@ ifeq ($(build),release)
#CFLAGS += -g
#CFLAGS += -ginline-points # inlined funcs debug info
LDFLAGS += -flto
else ifeq ($(build),perf)
CFLAGS += -O3
CFLAGS += -ggdb0 # symbols (gdb, perf, etc.)
CFLAGS += -ginline-points # inlined funcs debug info
CFLAGS += -funroll-loops
else ifeq ($(build),dev)
CFLAGS += -Og
CFLAGS += -g # symbols (gdb, perf, etc.)
CFLAGS += -O1
CFLAGS += -ggdb # symbols (gdb, perf, etc.)
CFLAGS += -ginline-points # inlined funcs debug info
#CFLAGS += -pg # gprof
# Next one may be useful for valgrind (when invalid instructions)
#CFLAGS += -mno-tbm
else ifeq ($(build),perf)
CFLAGS += -O3
CFLAGS += -g # symbols (gdb, perf, etc.)
CFLAGS += -ginline-points # inlined funcs debug info
CFLAGS += -funroll-loops
else ifeq ($(build),debug)
CFLAGS += -Og
CFLAGS += -g # symbols (gdb, perf, etc.)
CFLAGS += -ginline-points # inlined funcs debug info
CFLAGS += -O0 # -Og hides some variables
CFLAGS += -ggdb3 # symbols (including macro)
#CFLAGS += -ginline-points # inlined funcs debug info
# for gprof
#CFLAGS += -pg
# Next one may be useful for valgrind (when invalid instructions)
@@ -229,16 +229,16 @@ else
all: libs testing $(TARGET)
release:
$(MAKE) BUILD=release clean all
$(MAKE) build=release clean all
dev:
$(MAKE) BUILD=dev clean all
$(MAKE) build=dev clean all
perf:
$(MAKE) BUILD=perf clean all
$(MAKE) build=perf clean all
debug:
$(MAKE) BUILD=debug clean all
$(MAKE) build=debug clean all
compile: brlib objs

View File

@@ -182,19 +182,6 @@ static __always_inline bitboard_t bb_file(int file)
}
*/
/**
* bb_first_bb() - return bitboard of first square of a bitboard.
* @bb: bitboard
*
* bb must be non-zero.
*
* @return: bitboard of first square (lsb) of @bb.
*/
static __always_inline square_t bb_first_bb(bitboard_t bb)
{
return bb & -bb;
}
/**
* bb_next() - clear and return next (lsb) square of a bitboard.
* @bb: &bitboard
@@ -205,7 +192,7 @@ static __always_inline square_t bb_first_bb(bitboard_t bb)
*/
static __always_inline square_t bb_next(bitboard_t *bb)
{
square_t sq = ctz64(*bb);
square_t sq = lsb64(*bb);
*bb &= *bb - 1;
return sq;
}

View File

@@ -16,9 +16,10 @@
#include <brlib.h> /* brlib types */
#define ONE 1ul
#define U64(const_u64) const_u64##UL
#define BIT(i) ( (u64) (ONE << (i)) )
#define BIT(i) ( U64(1) << (i) )
#define BIT_NONE ( U64(0) )
#define BIT_ALL ( ~BIT_NONE )
#define BOARDSIZE (8*8)
#define GAMESIZE 1024 /* max game size (512 moves) */
@@ -50,7 +51,7 @@
/* castle_t bits structure
*/
typedef enum {
enum {
CASTLE_NONE = 0,
CASTLE_WK = (1 << 0), /* 0001 */
CASTLE_WQ = (1 << 1), /* 0010 */
@@ -64,7 +65,9 @@ typedef enum {
CASTLE_K = (1 << 0), /* generic K/Q, bits 0 and 1 */
CASTLE_Q = (1 << 1),
CASTLE_KQ = (CASTLE_K |CASTLE_Q),
} castle_rights_t;
};
typedef u8 castle_rights_t;
/* determine is oo or ooo is possible with castle flags f and color c
*/

View File

@@ -256,8 +256,7 @@ static const struct pst {
* A1 ..... H1
*/
[PAWN] = {
{ /* H1 H8 */
/* midgame */
{ /* midgame */
+0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
@@ -575,8 +574,8 @@ void pst_init(int set)
eval_t mid_pc = piece_midval(pt);
eval_t end_pc = piece_endval(pt);
for (square_t sq = 0; sq < SQUARE_NB; ++sq) {
eval_t mid_pst = pst->val[MIDGAME][pt][sq];
eval_t end_pst = pst->val[ENDGAME][pt][sq];
eval_t mid_pst = pst->val[pt][MIDGAME][sq];
eval_t end_pst = pst->val[pt][ENDGAME][sq];
pst_mg[BLACK][pt][sq] = (mid_pc * wmat + mid_pst * wpst) / 100;
pst_eg[BLACK][pt][sq] = (end_pc * wmat + end_pst * wpst) / 100;

View File

@@ -41,7 +41,7 @@ s16 calc_phase(pos_t *pos)
phase = max(phase, 0);
# ifdef DEBUG_EVAL
printf("calc phase:%d\n", phase);
printf("calculated phase:%d\n", phase);
# endif
return phase;
}

View File

@@ -202,7 +202,7 @@ int tt_create(s32 sizemb)
hash_tt.bytes = hash_tt.nbuckets * sizeof(bucket_t);
hash_tt.mb = hash_tt.bytes / 1024 / 1024;
hash_tt.mask = -1ull >> (64 - nbits);
hash_tt.mask = BIT_ALL >> (64 - nbits);
hash_tt.keys = safe_alloc_aligned_hugepage(hash_tt.bytes);
@@ -295,6 +295,7 @@ hentry_t *tt_probe_perft(const hkey_t key, const u16 depth)
/* find key in buckets */
for (i = 0; i < ENTRIES_PER_BUCKET; ++i) {
entry = bucket->entry + i;
bug_on(entry->key && (key & hash_tt.mask) != (entry->key & hash_tt.mask));
if (key == entry->key && HASH_PERFT_DEPTH(entry->data) == depth) {
hash_tt.hits++;
/*
@@ -390,7 +391,7 @@ hentry_t *tt_store_perft(const hkey_t key, const u16 depth, const u64 nodes)
void tt_info()
{
if (hash_tt.keys) {
printf("TT: Mb:%d buckets:%'lu (bits:%u mask:%#x) entries:%'lu\n",
printf("TT: Mb:%d buckets:%'lu (bits:%u mask:%#lx) entries:%'lu\n",
hash_tt.mb, hash_tt.nbuckets, hash_tt.nbits,
hash_tt.mask, hash_tt.nkeys);
} else {

View File

@@ -76,15 +76,15 @@ typedef struct {
/* memory size in bytes/mb */
size_t bytes;
u32 mb;
u32 mb;
/* size in buckets/keys */
size_t nbuckets;
size_t nkeys; /* nbuckets * NBUCKETS */
/* internal representation */
u32 nbits; /* #buckets in bits, power of 2 */
u32 mask; /* nbuckets - 1, key mask */
u32 nbits; /* #buckets in bits, power of 2 */
u64 mask; /* nbuckets - 1, bucket mask */
/* stats - unsure about usage */
//size_t used_buckets;
@@ -106,8 +106,8 @@ typedef struct {
extern hkey_t zobrist_pieces[16][64];
extern hkey_t zobrist_castling[4 * 4 + 1];
extern hkey_t zobrist_turn; /* for black, XOR each ply */
extern hkey_t zobrist_ep[9]; /* 0-7: ep file, 8: SQUARE_NONE */
extern hkey_t zobrist_turn; /* for black, XOR each ply */
extern hkey_t zobrist_ep[9]; /* 0-7: ep file, 8: SQUARE_NONE */
extern hasht_t hash_tt; /* main transposition table */

View File

@@ -25,6 +25,20 @@
#include "move-do.h"
#include "hash.h"
/**
* sq_castle - castling rights per square.
*
* position castling rights mask has to be AND'ed with this array 'from' and 'to'.
*/
castle_rights_t sq_castle[64] = {
[A1] = ~CASTLE_WQ, [B1 ... D1] = CASTLE_ALL,
[E1] = ~CASTLE_W, [F1 ... G1] = CASTLE_ALL,
[H1] = ~CASTLE_WK, [A2 ... H7] = CASTLE_ALL,
[A8] = ~CASTLE_BQ, [B8 ... D8] = CASTLE_ALL,
[E8] = ~CASTLE_B, [F8 ... G8] = CASTLE_ALL,
[H8] = ~CASTLE_BK,
};
/**
* move_do() - do move.
* @pos: &pos_t position
@@ -60,9 +74,8 @@ pos_t *move_do(pos_t *pos, const move_t move, state_t *state)
*state = pos->state; /* save irreversible changes */
/* update key: switch turn, reset castling and ep */
/* update key: switch turn, reset ep */
key ^= zobrist_turn;
key ^= zobrist_castling[pos->castle];
key ^= zobrist_ep[EP_ZOBRIST_IDX(pos->en_passant)];
++pos->clock_50;
@@ -74,80 +87,69 @@ pos_t *move_do(pos_t *pos, const move_t move, state_t *state)
bug_on(COLOR(piece) != us);
if (is_promotion(move)) {
bug_on(sq_rank(to) != sq_rel_rank(RANK_8, us));
new_piece = MAKE_PIECE(move_promoted(move), us);
}
if (captured != EMPTY) {
/* special moves: captures, pawn double push, en-passant, castling.
* All these moves are exclusive (so we use if ... else)
*/
if (captured != EMPTY) { /* capture: remove piece */
pos->clock_50 = 0;
bug_on(pos->board[to] == EMPTY || COLOR(pos->captured) != them);
key ^= zobrist_pieces[captured][to];
pos_clr_sq(pos, to); /* clear square */
} else if (is_castle(move)) { /* handle rook move */
square_t rookfrom, rookto;
if (to > from) {
rookfrom = sq_rel(H1, us);
rookto = sq_rel(F1, us);
} else {
rookfrom = sq_rel(A1, us);
rookto = sq_rel(D1, us);
}
key ^= zobrist_pieces[pos->board[rookfrom]][rookto] ^
zobrist_pieces[pos->board[rookfrom]][rookfrom];
pos_set_sq(pos, rookto, pos->board[rookfrom]);
pos_clr_sq(pos, rookfrom);
pos->castle = clr_castle(pos->castle, us);
} else if (ptype == PAWN) { /* pawn non capture or e.p. */
pos_clr_sq(pos, to);
} else if (ptype == PAWN) {
pos->clock_50 = 0;
if (from + up + up == to) { /* if pawn double push, set e.p. */
if (from + up + up == to) { /* double push: set e.p. */
square_t ep = from + up;
/* check that opponent can e.p. */
if (bb_pawn_attacks[us][ep] & pos->bb[them][PAWN]) {
pos->en_passant = ep;
key ^= zobrist_ep[EP_ZOBRIST_IDX(pos->en_passant)];
}
} else if (is_enpassant(move)) { /* clear grabbed pawn */
} else if (is_enpassant(move)) { /* e.p.: clear grabbed pawn */
square_t grabbed = to - up;
piece_t pc = pos->board[grabbed];
key ^= zobrist_pieces[pc][grabbed];
pos_clr_sq(pos, grabbed);
}
} else if (is_castle(move)) { /* castling: handle rook move */
square_t rookfrom, rookto;
if (to > from) { /* o-o */
rookfrom = to + 1;
rookto = to - 1;
} else { /* o-o-o */
rookfrom = to - 2;
rookto = to + 1;
}
key ^= zobrist_pieces[pos->board[rookfrom]][rookto] ^
zobrist_pieces[pos->board[rookfrom]][rookfrom];
pos_set_sq(pos, rookto, pos->board[rookfrom]);
pos_clr_sq(pos, rookfrom);
}
/* common part: captures and non captures
* We could improve slightly, as en-passant and double push will make
* the following tests.
* Improvement could be to add promotion test and king test twice above.
* (in capture and after ).
*/
if (is_promotion(move)) {
bug_on(sq_rank(to) != sq_rel_rank(RANK_8, us));
new_piece = MAKE_PIECE(move_promoted(move), us);
}
key ^= zobrist_pieces[piece][from] ^ zobrist_pieces[new_piece][to];
pos_clr_sq(pos, from); /* clear "from" and set "to" */
pos_clr_sq(pos, from);
pos_set_sq(pos, to, new_piece);
if (ptype == KING)
pos->king[us] = to;
/* update castling flags
* As we always consider flags are valid, we :
* - adjust our flags if relative from is "E1", "A1", H1"
* - adjust opp flags if relative to if "A8", H8"
*/
if (can_castle(pos->castle, us)) { /* do we save time with this test ? */
square_t rel_e1 = sq_rel(E1, us);
square_t rel_a1 = sq_rel(A1, us);
square_t rel_h1 = sq_rel(H1, us);
if (from == rel_e1)
pos->castle = clr_castle(pos->castle, us);
else if (from == rel_a1)
pos->castle = clr_ooo(pos->castle, us);
else if (from == rel_h1)
pos->castle = clr_oo(pos->castle, us);
}
if (can_castle(pos->castle, them)) {
square_t rel_a8 = sq_rel(A8, us);
square_t rel_h8 = sq_rel(H8, us);
if (to == rel_a8)
pos->castle = clr_ooo(pos->castle, them);
else if (to == rel_h8)
pos->castle = clr_oo(pos->castle, them);
}
/* update castling rights key */
/* update castling flags */
key ^= zobrist_castling[pos->castle];
pos->castle &= sq_castle[from] & sq_castle[to];
key ^= zobrist_castling[pos->castle];
//if (ptype == KING) {
// pos->king[us] = to;
//} else
/* do this always, cheaper than test on K move */
pos->king[us] = ctz64(pos->bb[us][KING]);
pos->key = key;
@@ -180,33 +182,43 @@ pos_t *move_undo(pos_t *pos, const move_t move, const state_t *state)
piece_t piece = pos->board[to];
int up = sq_up(them);
/* common part: captures and non captures
* We could improve slightly, as en-passant and double push will make
* the following tests.
* Improvement could be to add promotion test and king test twice above.
* (in capture and after ).
*/
if (is_promotion(move))
piece = MAKE_PIECE(PAWN, us);
pos_clr_sq(pos, to); /* always clear "to" ... */
pos_set_sq(pos, from, piece); /* ... and set "from" */
pos_clr_sq(pos, to);
pos_set_sq(pos, from, piece);
if (PIECE(piece) == KING)
pos->king[us] = from;
if (pos->captured != EMPTY) {
pos_set_sq(pos, to, pos->captured); /* restore captured piece */
} else if (is_castle(move)) { /* make reverse rook move */
/* special moves: capture, king (+ castling), en-passant
*/
if (pos->captured != EMPTY) { /* captured: restore piece */
pos_set_sq(pos, to, pos->captured);
} else if (is_castle(move)) { /* castle: rook move */
square_t rookfrom, rookto;
if (to > from) {
rookfrom = sq_rel(F1, us);
rookto = sq_rel(H1, us);
} else {
rookfrom = sq_rel(D1, us);
rookto = sq_rel(A1, us);
if (to > from) { /* o-o */
rookfrom = to - 1;
rookto = to + 1;
} else { /* o-o-o */
rookfrom = to + 1;
rookto = to - 2;
}
pos_set_sq(pos, rookto, pos->board[rookfrom]);
pos_clr_sq(pos, rookfrom);
} else if (is_enpassant(move)) { /* restore grabbed pawn */
} else if (is_enpassant(move)) { /* e.p.: restore grabbed pawn */
square_t grabbed = to + up;
pos_set_sq(pos, grabbed, MAKE_PIECE(PAWN, them));
}
/* do this always, cheaper than test on K move */
pos->king[us] = ctz64(pos->bb[us][KING]);
//if (PIECE(piece) == KING)
// pos->king[us] = from;
pos->state = *state; /* restore irreversible changes */
pos->turn = us;
return pos;
@@ -284,7 +296,7 @@ pos_t *move_do_alt(pos_t *pos, const move_t move, state_t *state)
}
key ^= zobrist_pieces[piece][from] ^ zobrist_pieces[new_piece][to];
pos_clr_sq(pos, from); /* clear "from" and set "to" */
pos_clr_sq(pos, from);
pos_set_sq(pos, to, new_piece);
if (ptype == KING)

View File

@@ -14,8 +14,11 @@
#ifndef MOVE_DO_H
#define MOVE_DO_H
#include "chessdefs.h"
#include "position.h"
extern castle_rights_t sq_castle[64]; /* to adjust castling rights */
pos_t *move_do(pos_t *pos, const move_t move, state_t *state);
pos_t *move_undo(pos_t *pos, const move_t move, const state_t *state);

View File

@@ -88,8 +88,8 @@ static __always_inline void pos_set_sq(pos_t *pos, square_t square, piece_t piec
bug_on(pos->board[square] != EMPTY);
pos->board[square] = piece;
pos->bb[color][type] |= BIT(square);
pos->bb[color][ALL_PIECES] |= BIT(square);
pos->bb[color][type] ^= BIT(square);
pos->bb[color][ALL_PIECES] ^= BIT(square);
//if (type == KING)
// pos->king[color] = square;
@@ -114,12 +114,38 @@ static __always_inline void pos_clr_sq(pos_t *pos, square_t square)
//pos->key ^= zobrist_pieces[piece][square];
pos->board[square] = EMPTY;
pos->bb[color][type] &= ~BIT(square);
pos->bb[color][ALL_PIECES] &= ~BIT(square);
pos->bb[color][type] ^= BIT(square);
pos->bb[color][ALL_PIECES] ^= BIT(square);
//if (type == KING)
// pos->king[color] = SQUARE_NONE;
}
/**
* pos_mv_sq - move a piece from a square to another one.
* @pos: position
* @from: source square
* @to: dest square
*
* Both position board and bitboards are modified.
* This function is equivalent to:
* pos_set_sq(pos, to, pos->board[from]);
* pos_clr_sq(pos, from);
*/
static __always_inline void pos_mv_sq(pos_t *pos, square_t from, square_t to)
{
piece_t pc = pos->board[from];
piece_type_t pt = PIECE(pc);
color_t color = COLOR(pc);
bitboard_t bb_squares = BIT(from) | BIT(to);
bug_on(pc == EMPTY || pos->board[to] != EMPTY);
pos->board[from] = EMPTY;
pos->board[to] = pc;
pos->bb[color][pt] ^= bb_squares;
pos->bb[color][ALL_PIECES] ^= bb_squares;
}
/**
* pos_occ() - get position occupation (all pieces)
* @pos: position

View File

@@ -198,6 +198,10 @@ struct fentest {
/*****************************************************
* tests from talkchess *
*****************************************************/
{ __LINE__, FEN | MOVEGEN | MOVEDO | PERFT,
"https://talkchess.com/viewtopic.php?t=21343",
"8/k7/3p4/p2P1p2/P2P1P2/8/8/K7 w - - 0 1"
},
{ __LINE__, FEN | MOVEGEN | MOVEDO | PERFT,
"https://www.talkchess.com/forum3/viewtopic.php?f=7&t=71379",
"8/6kR/8/8/8/bq6/1rqqqqqq/K1nqnbrq b - - 0 1"
@@ -447,7 +451,12 @@ struct fentest {
static int fentest_cur = -1;
static char *next_fen(uint module)
static __unused void restart_fen(void)
{
fentest_cur = -1;
}
static __unused char *next_fen(uint module)
{
fentest_cur++;

View File

@@ -52,8 +52,8 @@ int main(int __unused ac, __unused char**av)
for (move = movelist.move; move < last; ++move) {
//pos_print(pos);
//printf("i=%d j=%d turn=%d move=[%s]\n", i, j, pos->turn,
// move_str(movebuf, move, 0));
//move_p
// move_to_str(movebuf, *move, 0));
move_do(pos, *move, &state);
//pos_print(pos);
//fflush(stdout);
@@ -67,6 +67,7 @@ int main(int __unused ac, __unused char**av)
move_undo(pos, *move, &state);
pos->state = state;
if (!pos_ok(pos, false)) {
//pos_print(pos);
printf("*** fen %d [%s] move %d [%s] invalid position after move_undo\n",
test_line, fen, j, movebuf);
exit(0);

View File

@@ -242,7 +242,9 @@ static int usage(char *prg)
fprintf(stderr, "Usage: %s [-cms][-d depth] [-p version] [-t size:\n", prg);
fprintf(stderr, "\t-c: do *not* print FEN comments\n");
fprintf(stderr, "\t-d depth: perft depth (default: 6)\n");
fprintf(stderr, "\t-l line: start from 'line' test\n");
fprintf(stderr, "\t-m: print moves details\n");
fprintf(stderr, "\t-n number: do 'number' tests (default: all)\n");
fprintf(stderr, "\t-s: use Stockfish to validate perft result\n");
fprintf(stderr, "\t-t size: Transposition Table size (Mb). Default: 32\n");
fprintf(stderr,
@@ -252,7 +254,7 @@ static int usage(char *prg)
int main(int ac, char**av)
{
int curtest = 0;
int curtest = 0, totalfen = 0;
u64 sf_count = 0, my_count;
bool comment = true, sf_run = false, divide = false;
char *fen;
@@ -262,6 +264,8 @@ int main(int ac, char**av)
FILE *outfd = NULL;
s64 ms, lps;
int opt, depth = 6, run = 3, tt, newtt = HASH_SIZE_DEFAULT;
int startline = 1, ntests = INT_MAX;
struct {
s64 count, countskipped, ms;
s64 minlps, maxlps;
@@ -273,7 +277,8 @@ int main(int ac, char**av)
{ .minlps = LONG_MAX },
};
while ((opt = getopt(ac, av, "cd:mp:st:")) != -1) {
printf("Perft " VERSION "\n");
while ((opt = getopt(ac, av, "cd:l:mn:p:st:")) != -1) {
switch (opt) {
case 'c':
comment = false;
@@ -283,9 +288,15 @@ int main(int ac, char**av)
if (depth <= 0)
depth = 6;
break;
case 'l':
startline = atoi(optarg);
break;
case 'm':
divide = true;
break;
case 'n':
ntests = atoi(optarg);
break;
case 'p':
run = atoi(optarg);
break;
@@ -325,14 +336,23 @@ int main(int ac, char**av)
if (sf_run)
outfd = open_stockfish();
/* count total fen tests we will do */
while (next_fen(PERFT | MOVEDO))
totalfen++;
restart_fen();
CLOCK_DEFINE(clock, CLOCK_MONOTONIC);
while ((fen = next_fen(PERFT | MOVEDO))) {
if (cur_line() < startline)
continue;
if (curtest >= ntests)
break;
if (!(fenpos = fen2pos(pos, fen))) {
printf("wrong fen line:%d fen:%s\n\n", cur_line(), fen);
continue;
}
curtest++;
printf("test:%d line:%d fen:%s\n", curtest, cur_line(), fen);
printf("test:%d/%d line:%d fen:%s\n", curtest, totalfen, cur_line(), fen);
if (comment)
printf("\t\"%s\"\n",
*cur_comment()? cur_comment(): "no test desc");