From 7e65fbc2058b2661efd42d04fa7a1fad85570e64 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Wed, 4 Sep 2024 20:07:19 +0200 Subject: [PATCH] change some bit macros, fix pst_init bug (thx gcc!), TT mask 64 bits --- src/chessdefs.h | 5 +++-- src/eval-defs.c | 7 +++---- src/hash.c | 5 +++-- src/hash.h | 10 +++++----- test/common-test.h | 4 ++++ 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/chessdefs.h b/src/chessdefs.h index 39b6822..869cf4d 100644 --- a/src/chessdefs.h +++ b/src/chessdefs.h @@ -16,9 +16,10 @@ #include /* 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) */ diff --git a/src/eval-defs.c b/src/eval-defs.c index 62332ac..8370c5b 100644 --- a/src/eval-defs.c +++ b/src/eval-defs.c @@ -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; diff --git a/src/hash.c b/src/hash.c index 989005a..d0b61f9 100644 --- a/src/hash.c +++ b/src/hash.c @@ -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 { diff --git a/src/hash.h b/src/hash.h index db56fc6..978229b 100644 --- a/src/hash.h +++ b/src/hash.h @@ -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 */ diff --git a/test/common-test.h b/test/common-test.h index 6523a73..dad6aad 100644 --- a/test/common-test.h +++ b/test/common-test.h @@ -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"