From 8bf4262e1186713dc7614d863f6551774fba23e7 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Wed, 11 Sep 2024 08:14:55 +0200 Subject: [PATCH] position: use XOR in square clr/set, for castling flags to 8 bits --- src/bitboard.h | 15 +-------------- src/chessdefs.h | 6 ++++-- src/eval.c | 2 +- src/position.h | 34 ++++++++++++++++++++++++++++++---- test/movedo-test.c | 5 +++-- 5 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/bitboard.h b/src/bitboard.h index 4848f7a..31e1fb8 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -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; } diff --git a/src/chessdefs.h b/src/chessdefs.h index 869cf4d..62fbbad 100644 --- a/src/chessdefs.h +++ b/src/chessdefs.h @@ -51,7 +51,7 @@ /* castle_t bits structure */ -typedef enum { +enum { CASTLE_NONE = 0, CASTLE_WK = (1 << 0), /* 0001 */ CASTLE_WQ = (1 << 1), /* 0010 */ @@ -65,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 */ diff --git a/src/eval.c b/src/eval.c index c736c7e..c1bf653 100644 --- a/src/eval.c +++ b/src/eval.c @@ -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; } diff --git a/src/position.h b/src/position.h index 38a0e3c..d8c35fd 100644 --- a/src/position.h +++ b/src/position.h @@ -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 diff --git a/test/movedo-test.c b/test/movedo-test.c index 897db58..8d6d21a 100644 --- a/test/movedo-test.c +++ b/test/movedo-test.c @@ -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);