diff --git a/src/attack.c b/src/attack.c index 12bec1b..5ab5cc7 100644 --- a/src/attack.c +++ b/src/attack.c @@ -109,7 +109,7 @@ bool is_in_check(const pos_t *pos, const color_t color) bitboard_t sq_attackers(const pos_t *pos, const bitboard_t occ, const square_t sq, const color_t c) { bitboard_t attackers = 0, tmp; - bitboard_t sqbb = mask(sq); + bitboard_t sqbb = BIT(sq); bitboard_t to; color_t opp = OPPONENT(c); @@ -186,7 +186,7 @@ bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t color) bitboard_t between = bb_between_excl[maybe_pinner][sq]; /* keep only squares between AND on sq diag/anti */ if (popcount64(between & lines) == 1) - pinners |= mask(maybe_pinner); + pinners |= BIT(maybe_pinner); } /* same for rook type */ @@ -195,7 +195,7 @@ bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t color) bit_for_each64(maybe_pinner, tmp, attackers) { bitboard_t between = bb_between_excl[maybe_pinner][sq]; if (popcount64(between & lines) == 1) - pinners |= mask(maybe_pinner); + pinners |= BIT(maybe_pinner); } # ifdef DEBUG_ATTACK_ATTACKERS1 char str[32]; diff --git a/src/bitboard.c b/src/bitboard.c index ad2e3c7..83d55b8 100644 --- a/src/bitboard.c +++ b/src/bitboard.c @@ -53,8 +53,8 @@ static int king_vector[8] = { bitboard_t bitboard_between_excl(square_t sq1, square_t sq2) { const bitboard_t m1 = -1; - const bitboard_t a2a7 = C64(0x0001010101010100); - const bitboard_t b7h1 = C64(0x0002040810204080); + const bitboard_t a2a7 = U64(0x0001010101010100); + const bitboard_t b7h1 = U64(0x0002040810204080); bitboard_t btwn_bits, ray_bits; u32 rank_diff, file_diff, anti_diff, diag_diff; @@ -109,7 +109,7 @@ void bitboard_init(void) * in-between, sq2 excluded */ for (square_t sq1 = A1; sq1 <= H8; ++sq1) { - bb_sq[sq1] = mask(sq1); + bb_sq[sq1] = BIT(sq1); for (square_t sq2 = A1; sq2 <= H8; ++sq2) bb_between_excl[sq1][sq2] = bitboard_between_excl(sq1, sq2); } @@ -121,15 +121,15 @@ void bitboard_init(void) file_t f = sq_file(sq); rank_t r = sq_rank(sq); for (int vec = 0; vec < 4; ++vec) { - tmpbb[sq][vec] |= mask(sq_make(f, r)); + tmpbb[sq][vec] |= BIT(sq_make(f, r)); for (int dir = -1; dir <= 1; dir += 2) { file_t df = dir * vecs[vec].df, f2 = f + df; rank_t dr = dir * vecs[vec].dr, r2 = r + dr; bitboard_t mask_between = 0; while (sq_coord_ok(f2) && sq_coord_ok(r2)) { square_t dest = sq_make(f2, r2); - tmpbb[sq][vec] |= mask(dest); - mask_between |= mask(dest); + tmpbb[sq][vec] |= BIT(dest); + mask_between |= BIT(dest); bb_between[sq][dest] = mask_between; f2 += df, r2 += dr; } @@ -161,9 +161,9 @@ void bitboard_init(void) */ for (square_t sq = A1; sq <= H8; ++sq) { if (sq >= A2) - bb_pawn_attacks[BLACK][sq] = pawn_attacks_bb(mask(sq), BLACK); + bb_pawn_attacks[BLACK][sq] = pawn_attacks_bb(BIT(sq), BLACK); if (sq <= H7) - bb_pawn_attacks[WHITE][sq] = pawn_attacks_bb(mask(sq), WHITE); + bb_pawn_attacks[WHITE][sq] = pawn_attacks_bb(BIT(sq), WHITE); for (int vec = 0; vec < 8; ++vec) { int dst = sq + knight_vector[vec]; @@ -281,7 +281,7 @@ char *bb_rank_sprint(char *str, const uchar bb8) { file_t f; for (f = FILE_A; f <= FILE_H; ++f) { - *(str + f) = bb8 & mask(f) ? '1': '.'; + *(str + f) = bb8 & BIT(f) ? '1': '.'; } *(str + f) = 0; //printf(" 0 1 2 3 4 5 6 7\n"); diff --git a/src/bitboard.h b/src/bitboard.h index 608066e..2a60d87 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -260,7 +260,7 @@ static __always_inline bool bb_sq_aligned(square_t sq1, square_t sq2) */ static __always_inline bool bb_sq_aligned3(square_t sq1, square_t sq2, square_t sq3) { - return bb_line[sq1][sq2] & mask(sq3); + return bb_line[sq1][sq2] & BIT(sq3); } /** @@ -273,7 +273,7 @@ static __always_inline bool bb_sq_aligned3(square_t sq1, square_t sq2, square_t */ static __always_inline bitboard_t bb_sq_between(square_t sq, square_t sq1, square_t sq2) { - return bb_between_excl[sq1][sq2] & mask(sq); + return bb_between_excl[sq1][sq2] & BIT(sq); } /* TODO: when OK, replace with macros */ diff --git a/src/board.c b/src/board.c index aade98d..8acfc4d 100644 --- a/src/board.c +++ b/src/board.c @@ -93,7 +93,7 @@ void board_print_mask(const piece_t *board, const bitboard_t mask) for (int file = 0; file < 8; ++file) { square_t sq = sq_make(file, rank); piece_t pc = board[sq]; - bitboard_t set = mask(sq) & mask; + bitboard_t set = BIT(sq) & mask; printf("%s", set? REVERSE : " "); # ifdef DIAGRAM_SYM printf("%s", pc? piece_to_sym(pc): " "); diff --git a/src/chessdefs.h b/src/chessdefs.h index 476ff1e..19d2382 100644 --- a/src/chessdefs.h +++ b/src/chessdefs.h @@ -17,8 +17,8 @@ #include "brlib.h" /* brlib types */ #define ONE 1ull -#define C64(const_u64) const_u64##ULL -#define mask(i) ( (u64) (ONE << (i)) ) +#define U64(const_u64) const_u64##ULL +#define BIT(i) ( (u64) (ONE << (i)) ) #define BOARDSIZE (8*8) @@ -50,17 +50,19 @@ /* castle_t bits structure */ typedef enum { - CASTLE_WK = (1 << 0), /* 0x01 00000001 */ - CASTLE_WQ = (1 << 1), /* 0x02 00000010 */ - CASTLE_BK = (1 << 2), /* 0x04 00000100 */ - CASTLE_BQ = (1 << 3), /* 0x08 00001000 */ + CASTLE_NONE = 0, + CASTLE_WK = (1 << 0), /* 0001 */ + CASTLE_WQ = (1 << 1), /* 0010 */ + CASTLE_BK = (1 << 2), /* 0100 */ + CASTLE_BQ = (1 << 3), /* 1000 */ - CASTLE_W = (CASTLE_WK | CASTLE_WQ), /* 00000011 W castle mask */ - CASTLE_B = (CASTLE_BK | CASTLE_BQ), /* 00001100 B castle mask */ + CASTLE_W = (CASTLE_WK | CASTLE_WQ), /* 0011 = 3 = W castle mask */ + CASTLE_B = (CASTLE_BK | CASTLE_BQ), /* 1100 = 12 = B castle mask */ + CASTLE_ALL = (CASTLE_W | CASTLE_B), /* 1111 = 15 */ CASTLE_K = (1 << 0), /* generic K/Q, bits 0 and 1 */ CASTLE_Q = (1 << 1), - CASTLE_KQ = (CASTLE_K |CASTLE_Q) + CASTLE_KQ = (CASTLE_K |CASTLE_Q), } castle_rights_t; /* determine is oo or ooo is possible with castle flags f and color c @@ -171,4 +173,7 @@ s64 clock_elapsed_μs(mclock_t *clock); s64 clock_elapsed_ms(mclock_t *clock); double clock_elapsed_sec(mclock_t *clock); +void rand_init(u64 seed); +u64 rand64(void); + #endif /* _CHESSDEFS_H */ diff --git a/src/fen.c b/src/fen.c index 741b2ed..2818000 100644 --- a/src/fen.c +++ b/src/fen.c @@ -162,7 +162,7 @@ pos_t *fen2pos(pos_t *pos, const char *fen) char *p; short rank, file, tmp; piece_t piece; - int consumed, err_line = 0, err_pos, err_char; + int consumed, err_line = 0, err_pos = 0, err_char = 0; pos_t tmppos; pos_clear(&tmppos); @@ -324,7 +324,7 @@ char *pos2fen(const pos_t *pos, char *fen) fen[cur++] = '-'; } else { for (int i = 0; i < 4; ++i) - if (pos->castle & mask(i)) + if (pos->castle & BIT(i)) fen[cur++] = castle_str[i]; } fen[cur++] = ' '; diff --git a/src/hyperbola-quintessence.c b/src/hyperbola-quintessence.c index 2346748..7bd1ec8 100644 --- a/src/hyperbola-quintessence.c +++ b/src/hyperbola-quintessence.c @@ -129,8 +129,8 @@ bitboard_t hyperbola_moves(const bitboard_t pieces, const square_t sq, bitboard_t r = bswap64(o); square_t r_sq = FLIP_V(sq); - return ( (o - 2 * mask(sq) ) - ^ bswap64(r - 2 * mask(r_sq))) + return ( (o - 2 * BIT(sq) ) + ^ bswap64(r - 2 * BIT(r_sq))) & mask; } diff --git a/src/move-gen.c b/src/move-gen.c index 0113ca7..fed7d94 100644 --- a/src/move-gen.c +++ b/src/move-gen.c @@ -41,7 +41,7 @@ bool pseudo_is_legal(const pos_t *pos, const move_t move) square_t king = pos->king[us]; bitboard_t kingbb = pos->bb[us][KING]; bitboard_t occ = pos_occ(pos); - u64 pinned = mask(from) & pos->blockers; + u64 pinned = BIT(from) & pos->blockers; u64 checkers = pos->checkers; /* (1) - Castling & King @@ -86,8 +86,8 @@ bool pseudo_is_legal(const pos_t *pos, const move_t move) /* (3) - pinned pieces * We verify here that pinned piece P stays on line King-P. */ - if (mask(from) & pos->blockers) { - return bb_line[from][king] & mask(to); /* is to on pinner line ? */ + if (BIT(from) & pos->blockers) { + return bb_line[from][king] & BIT(to); /* is to on pinner line ? */ } /* (4) - En-passant @@ -99,7 +99,7 @@ bool pseudo_is_legal(const pos_t *pos, const move_t move) bitboard_t rank5 = us == WHITE? RANK_5bb: RANK_4bb; if ((pos->bb[us][KING] & rank5)) { - bitboard_t exclude = mask(pos->en_passant - sq_up(us)) | mask(from); + bitboard_t exclude = BIT(pos->en_passant - sq_up(us)) | BIT(from); bitboard_t rooks = (pos->bb[them][ROOK] | pos->bb[them][QUEEN]) & rank5; while (rooks) { diff --git a/src/move.h b/src/move.h index 0a85e22..368fdbb 100644 --- a/src/move.h +++ b/src/move.h @@ -47,13 +47,13 @@ enum { }; typedef enum { - M_CAPTURE = mask(M_OFF_FLAGS + 0), - M_ENPASSANT = mask(M_OFF_FLAGS + 1), - M_PROMOTION = mask(M_OFF_FLAGS + 2), - M_CASTLE_K = mask(M_OFF_FLAGS + 3), /* maybe only one ? */ - M_CASTLE_Q = mask(M_OFF_FLAGS + 5), /* maybe only one ? */ - M_CHECK = mask(M_OFF_FLAGS + 6), /* maybe unknown/useless ? */ - M_DPUSH = mask(M_OFF_FLAGS + 7) /* pawn double push */ + M_CAPTURE = BIT(M_OFF_FLAGS + 0), + M_ENPASSANT = BIT(M_OFF_FLAGS + 1), + M_PROMOTION = BIT(M_OFF_FLAGS + 2), + M_CASTLE_K = BIT(M_OFF_FLAGS + 3), /* maybe only one ? */ + M_CASTLE_Q = BIT(M_OFF_FLAGS + 5), /* maybe only one ? */ + M_CHECK = BIT(M_OFF_FLAGS + 6), /* maybe unknown/useless ? */ + M_DPUSH = BIT(M_OFF_FLAGS + 7) /* pawn double push */ } move_flags_t; #define move_set_flags(move, flags) ((move) | (flags)) diff --git a/src/position.c b/src/position.c index 580c3f2..ab277ec 100644 --- a/src/position.c +++ b/src/position.c @@ -208,7 +208,7 @@ void pos_set_checkers_pinners_blockers(pos_t *pos) /* blockers = we find occupied squares between pinner and king */ while (tmppinners) { pinner = bb_next(&tmppinners); - pinners |= mask(pinner); + pinners |= BIT(pinner); blockers |= bb_between[pinner][king] & maybeblockers; } } @@ -226,7 +226,7 @@ void pos_set_checkers_pinners_blockers(pos_t *pos) tmppinners = targets & attackers; while (tmppinners) { pinner = bb_next(&tmppinners); - pinners |= mask(pinner); + pinners |= BIT(pinner); blockers |= bb_between[pinner][king] & maybeblockers; } } @@ -367,7 +367,7 @@ bool pos_ok(const pos_t *pos, const bool strict) continue; color_t c = COLOR(piece); piece_type_t p = PIECE(piece); - match = pos->bb[c][p] & mask(sq); + match = pos->bb[c][p] & BIT(sq); error += warn_on(!match); count++; } diff --git a/src/position.h b/src/position.h index 3e5f40e..c859188 100644 --- a/src/position.h +++ b/src/position.h @@ -30,9 +30,12 @@ typedef struct __pos_s { u64 node_count; /* evaluated nodes */ int turn; /* WHITE or BLACK */ - /* data which cannot be recovered by move_undo (like castle_rights, ...), - * or would be expensive to recover (checkers, ...) - * following data can be accessed either directly, either via "state" + /* data which cannot be recovered by move_undo (like castle_rights, ...). + * + * Attention: checkers/pinners/blockers are not included here, and + * are not available in move_undo or any following legality check. + * + * Following data can be accessed either directly, either via "state" * structure name. * For example, pos->en_passant and pos->state.en_passant are the same. * This allows a memcpy on this data (to save/restore position state). @@ -42,17 +45,15 @@ typedef struct __pos_s { castle_rights_t castle; int clock_50; int plycount; /* plies so far, start from 1 */ - piece_t captured; /* only for move_undo */ - bitboard_t checkers; /* opponent checkers */ - bitboard_t pinners; /* opponent pinners */ - bitboard_t blockers; /* pieces blocking pin */ + piece_t captured; /* only used in move_undo */ ); + bitboard_t checkers; /* opponent checkers */ + bitboard_t pinners; /* opponent pinners */ + bitboard_t blockers; /* pieces blocking pin */ piece_t board[BOARDSIZE]; bitboard_t bb[2][PIECE_TYPE_MAX]; /* bb[0][PAWN], bb[1][ALL_PIECES] */ - //bitboard_t controlled[2]; /* unsure */ square_t king[2]; /* dup with bb, faster retrieval */ - //movelist_t moves; } pos_t; typedef struct state_s state_t; @@ -72,8 +73,8 @@ static __always_inline void pos_set_sq(pos_t *pos, square_t square, piece_t piec color_t color = COLOR(piece); piece_type_t type = PIECE(piece); pos->board[square] = piece; - pos->bb[color][type] |= mask(square); - pos->bb[color][ALL_PIECES] |= mask(square); + pos->bb[color][type] |= BIT(square); + pos->bb[color][ALL_PIECES] |= BIT(square); if (type == KING) pos->king[color] = square; } @@ -91,8 +92,8 @@ static __always_inline void pos_clr_sq(pos_t *pos, square_t square) piece_type_t type = PIECE(piece); color_t color = COLOR(piece); pos->board[square] = EMPTY; - pos->bb[color][type] &= ~mask(square); - pos->bb[color][ALL_PIECES] &= ~mask(square); + pos->bb[color][type] &= ~BIT(square); + pos->bb[color][ALL_PIECES] &= ~BIT(square); if (type == KING) pos->king[color] = SQUARE_NONE; }