From 022daf0a89423cdcce3d47dc0bebfd2d8a7fbd45 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Wed, 15 May 2024 09:42:33 +0200 Subject: [PATCH] typedef key -> #define key_t - can't use typedef, due to --- Makefile | 2 +- src/hash.c | 4 ++-- src/hash.h | 17 +++++++++++++---- src/move-do.c | 14 ++++++++++++-- src/position.h | 8 +++++--- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index bae6a13..0928bf6 100644 --- a/Makefile +++ b/Makefile @@ -346,7 +346,7 @@ FEN_OBJS := $(PIECE_OBJS) fen.o position.o bitboard.o board.o \ BB_OBJS := $(FEN_OBJS) MOVEGEN_OBJS := $(BB_OBJS) move.o move-gen.o ATTACK_OBJS := $(MOVEGEN_OBJS) -MOVEDO_OBJS := $(ATTACK_OBJS) move-do.o +MOVEDO_OBJS := $(ATTACK_OBJS) move-do.o hash.o PERFT_OBJS := $(MOVEDO_OBJS) search.o misc.o TEST := $(addprefix $(BINDIR)/,$(TEST)) diff --git a/src/hash.c b/src/hash.c index 6ca0e0b..befa61c 100644 --- a/src/hash.c +++ b/src/hash.c @@ -59,9 +59,9 @@ void zobrist_init(void) * - To verify incremental Zobrist calculation is correct * */ -key zobrist_calc(pos_t *pos) +key_t zobrist_calc(pos_t *pos) { - key key = 0; + key_t key = 0; if (pos->turn == BLACK) key ^= zobrist_turn; diff --git a/src/hash.h b/src/hash.h index 9fc014f..76cc880 100644 --- a/src/hash.h +++ b/src/hash.h @@ -24,7 +24,7 @@ #define HASH_SIZE_MIN 4 #define HASH_SIZE_MAX 32768 /* 32Gb */ -typedef u64 key; +#define key_t u64 /* cannot use typedef for key_t */ /** * hentry_t: hashtable bucket. @@ -33,7 +33,7 @@ typedef u64 key; * 16 bytes in future, it should be updated to be exactly 32 bytes. */ typedef struct { - key key; /* zobrist */ + key_t key; /* zobrist */ union { u64 data; struct { @@ -81,9 +81,18 @@ typedef struct { */ #define EP_ZOBRIST_IDX(ep) ( ( (ep) >> 3 ) | sq_file(ep) ) +extern key_t zobrist_pieces[16][64]; +extern key_t zobrist_castling[4 * 4 + 1]; +extern key_t zobrist_turn; /* for black, XOR each ply */ +extern key_t zobrist_ep[9]; /* 0-7: ep file, 8: SQUARE_NONE */ + +extern hasht_t hash_tt; /* main transposition table */ + void zobrist_init(void); +key_t zobrist_calc(pos_t *pos); + int hash_create(int Mb); -void hash_clear(); -void hash_delete(); +void hash_clear(void); +void hash_delete(void); #endif /* HASH_H */ diff --git a/src/move-do.c b/src/move-do.c index fe62ea2..6a27021 100644 --- a/src/move-do.c +++ b/src/move-do.c @@ -23,6 +23,7 @@ #include "move.h" #include "position.h" #include "move-do.h" +#include "hash.h" /** * move_do() - do move. @@ -39,8 +40,12 @@ * - castling * - en-passant * - captured piece (excl. en-passant) + * - tt hash values are updated for: + * - side-to-move + * - en-passant + * - castling rights. * - * @return: pos. + * @return: updated pos. */ pos_t *move_do(pos_t *pos, const move_t move) //, state_t *state) { @@ -57,6 +62,9 @@ pos_t *move_do(pos_t *pos, const move_t move) //, state_t *state) piece_t new_piece = piece; int up = sq_up(us); + /* update turn, reset castling and ep */ + pos->key ^= zobrist_turn; + ++pos->clock_50; ++pos->plycount; pos->en_passant = SQUARE_NONE; @@ -119,7 +127,8 @@ pos_t *move_do(pos_t *pos, const move_t move) //, state_t *state) else if (from == rel_h1) pos->castle = clr_oo(pos->castle, us); } - if (can_castle(pos->castle, them)) { /* do we save time with this test ? */ + + 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) @@ -128,6 +137,7 @@ pos_t *move_do(pos_t *pos, const move_t move) //, state_t *state) pos->castle = clr_oo(pos->castle, them); } + return pos; } diff --git a/src/position.h b/src/position.h index c859188..3885710 100644 --- a/src/position.h +++ b/src/position.h @@ -16,11 +16,12 @@ #include -#include "brlib.h" -#include "bitops.h" -#include "struct-group.h" +#include +#include +#include #include "chessdefs.h" +#include "hash.h" #include "bitboard.h" #include "piece.h" #include "move.h" @@ -41,6 +42,7 @@ typedef struct __pos_s { * This allows a memcpy on this data (to save/restore position state). */ struct_group_tagged(state_s, state, + key_t key; square_t en_passant; castle_rights_t castle; int clock_50;