position: use XOR in square clr/set, for castling flags to 8 bits

This commit is contained in:
2024-09-11 08:14:55 +02:00
parent 9dd7c7064d
commit 8bf4262e11
5 changed files with 39 additions and 23 deletions

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

@@ -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
*/

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

@@ -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

@@ -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);