From 7295914bb6875e8e3201603331c6b2fc67490853 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Thu, 7 Mar 2024 10:50:19 +0100 Subject: [PATCH] temp commit for machine change. Does not compile ! --- Makefile | 18 ++- src/attack.c | 6 +- src/bitboard.c | 44 +++---- src/bitboard.h | 223 ++++++++++++++++++++++++++++------- src/board.h | 7 ++ src/chessdefs.h | 32 ++--- src/fen.c | 6 +- src/hyperbola-quintessence.c | 6 +- src/move-gen.c | 212 +++++++++++++++++++++------------ src/move.c | 3 +- src/move.h | 11 +- src/position.h | 43 ++++++- test/bitboard-test.c | 16 +-- test/fen-test.c | 3 +- 14 files changed, 444 insertions(+), 186 deletions(-) diff --git a/Makefile b/Makefile index 45e63d8..beedb33 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ SHELL := /bin/bash CC := gcc +#CC := clang LD := ld BEAR := bear TOUCH := touch @@ -40,6 +41,7 @@ OBJ := $(addprefix $(OBJDIR)/,$(SRC_FN:.c=.o)) TSTSRC := $(wildcard $(TSTDIR)/*.c) LIB := br_$(shell uname -m) # library name +LIBS := $(strip -l$(LIB) -lreadline) DEP_FN := $(SRC_FN) DEP := $(addprefix $(DEPDIR)/,$(DEP_FN:.c=.d)) @@ -47,9 +49,6 @@ DEP := $(addprefix $(DEPDIR)/,$(DEP_FN:.c=.d)) TARGET_FN := brchess TARGET := $(addprefix $(BINDIR)/,$(TARGET_FN)) -LDFLAGS := -L$(BRLIBDIR) -LIBS := $(strip -l$(LIB) -lreadline) - ASMFILES := $(SRC:.c=.s) $(TSTSRC:.c=.s) CPPFILES := $(SRC:.c=.i) $(TSTSRC:.c=.i) @@ -79,6 +78,7 @@ CPPFLAGS := $(strip $(CPPFLAGS)) ##################################### compiler flags CFLAGS := -std=gnu11 +#CFLAGS += -flto CFLAGS += -O1 CFLAGS += -g CFLAGS += -Wall @@ -92,6 +92,12 @@ CFLAGS += -Wmissing-declarations CFLAGS := $(strip $(CFLAGS)) +##################################### linker flags +LDFLAGS := -L$(BRLIBDIR) +#LDFLAGS += -flto + +LDFLAGS := $(strip $(LDFLAGS)) + ##################################### archiver/dependency flags ARFLAGS := rcs DEPFLAGS = -MMD -MP -MF $(DEPDIR)/$*.d @@ -193,7 +199,7 @@ cleanobjdir: cleanobj # "normal" ones, but do not imply to rebuild target. $(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) $(DEPDIR) @echo compiling brchess module: $< "->" $@. - @$(CC) -c $(ALL_CFLAGS) $< -o $@ + $(CC) -c $(ALL_CFLAGS) $< -o $@ ##################################### brlib libraries .PHONY: cleanbrlib cleanallbrlib brlib @@ -279,9 +285,9 @@ FEN_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintesse BB_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \ attack.o MOVEGEN_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \ - attack.o move.o movegen.o + attack.o move.o move-gen.o ATTACK_OBJS := fen.o position.o piece.o bitboard.o board.o hyperbola-quintessence.o \ - attack.o move.o movegen.o + attack.o move.o move-gen.o TEST := $(addprefix $(BINDIR)/,$(TEST)) diff --git a/src/attack.c b/src/attack.c index a3e2998..11e0033 100644 --- a/src/attack.c +++ b/src/attack.c @@ -41,8 +41,7 @@ bitboard_t sq_attackers(const pos_t *pos, const square_t sq, const color_t c) { bitboard_t attackers = 0; bitboard_t sqbb = mask(sq); - bitboard_t c_pieces = pos->bb[c][ALL_PIECES]; - bitboard_t occ = c_pieces | pos->bb[OPPONENT(c)][ALL_PIECES]; + bitboard_t occ = pos_occ(pos); bitboard_t to; color_t opp = OPPONENT(c); @@ -78,9 +77,6 @@ bitboard_t sq_attackers(const pos_t *pos, const square_t sq, const color_t c) /* rook / queen */ to = pos->bb[c][ROOK] | pos->bb[c][QUEEN]; -# ifdef DEBUG_ATTACK - bitboard_print("sq_attackers after queen", attackers); -# endif attackers |= hyperbola_rook_moves(occ, sq) & to; # ifdef DEBUG_ATTACK bitboard_print("sq_attackers after rook/queen", attackers); diff --git a/src/bitboard.c b/src/bitboard.c index 03e4eaf..479488d 100644 --- a/src/bitboard.c +++ b/src/bitboard.c @@ -21,6 +21,13 @@ #include "board.h" #include "bitboard.h" +bitboard_t bb_sq[64]; +bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64]; +bitboard_t bb_between_excl[64][64]; +bitboard_t bb_between[64][64]; + +bitboard_t bb_knight[64], bb_king[64]; + /* vectors are clockwise from N */ static int knight_vector[] = { NORTH_EAST + NORTH, NORTH_EAST + EAST, @@ -33,14 +40,6 @@ static int king_vector[8] = { SOUTH, SOUTH_WEST, WEST, NORTH_WEST }; -bitboard_t bb_sq[64]; -bitboard_t bb_rank[64], bb_file[64], bb_diag[64], bb_anti[64]; -bitboard_t bb_between_excl[64][64]; -bitboard_t bb_between[64][64]; - -bitboard_t bb_knight[64], bb_king[64]; -bitboard_t bb_pawn_push[2][64], bb_bpawn_attack[2][64], bb_pawn_ep[2][64]; - /** * bitboard_between_excl() - get bitboard of squares between two squares. * @sq1, @sq2: The two square_t squares @@ -81,12 +80,13 @@ bitboard_t bitboard_between_excl(square_t sq1, square_t sq2) * * Generate the following bitboards : * bb_sq[64]: square to bitboard + * bb_sqrank[64]: square to rank + * bb_sqfile[64]: square to file + * bb_sqdiag[64]: square to diagonal + * bb_sqanti[64]: square to antidiagonal + * * bb_between_excl[64][64]: strict squares between two squares * bb_between[64][64]: squares between two squares including second square - * bb_rank[64]: square to rank - * bb_file[64]: square to file - * bb_diagonal[64]: square to diagonal - * bb_antidiagonal[64]: square to antidiagonal * * And the following pseudo move masks: * bb_knight[64]: knight moves @@ -134,10 +134,10 @@ void bitboard_init(void) } } for (square_t sq = 0; sq < 64; ++sq) { - bb_file[sq] = tmpbb[sq][0]; - bb_rank[sq] = tmpbb[sq][1]; - bb_diag[sq] = tmpbb[sq][2]; - bb_anti[sq] = tmpbb[sq][3]; + bb_sqfile[sq] = tmpbb[sq][0]; + bb_sqrank[sq] = tmpbb[sq][1]; + bb_sqdiag[sq] = tmpbb[sq][2]; + bb_sqanti[sq] = tmpbb[sq][3]; } /* 3) knight and king moves */ @@ -189,11 +189,11 @@ bitboard_t bb_king_moves(bitboard_t notmine, square_t sq) } /** - * bitboard_print() - print simple bitboard representation + * bb_print() - print simple bitboard representation * @title: a string or NULL * @bitboard: the bitboard */ -void bitboard_print(const char *title, const bitboard_t bitboard) +void bb_print(const char *title, const bitboard_t bitboard) { //char c = p? p: 'X'; if (title) @@ -210,14 +210,14 @@ void bitboard_print(const char *title, const bitboard_t bitboard) } /** - * bitboard_print_multi() - print multiple bitboards horizontally + * bb_print_multi() - print multiple bitboards horizontally * @title: a string or NULL * @n: number of bitboards * @bb_ptr...: pointers to bitboards * * @n is the number of bitboards to print. If @n > 10, it is reduced to 10 */ -void bitboard_print_multi(const char *title, int n, ...) +void bb_print_multi(const char *title, int n, ...) { bitboard_t bb[8]; va_list ap; @@ -252,13 +252,13 @@ void bitboard_print_multi(const char *title, int n, ...) } /** - * bitboard_rank_sprint() - print an u8 rank binary representation + * bb_rank_sprint() - print an u8 rank binary representation * @str: the destination string * @bb8: the uchar to print * * @return: @str, filled with ascii representation */ -char *bitboard_rank_sprint(char *str, const uchar bb8) +char *bb_rank_sprint(char *str, const uchar bb8) { file_t f; for (f = FILE_A; f <= FILE_H; ++f) { diff --git a/src/bitboard.h b/src/bitboard.h index a3c950a..52ab51f 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -30,53 +30,192 @@ extern bitboard_t bb_between_excl[64][64]; /* squares between sq1 and sq2, including sq2 */ extern bitboard_t bb_between[64][64]; -/* bb_rank[64]: square to rank - * bb_file[64]: square to file - * bb_diag[64]: square to diagonal - * bb_anti[64]: square to antidiagonal +/* bb_sqrank[64]: square to rank + * bb_sqfile[64]: square to file + * bb_sqdiag[64]: square to diagonal + * bb_sqanti[64]: square to antidiagonal */ -extern bitboard_t bb_rank[64], bb_file[64], bb_diag[64], bb_anti[64]; +extern bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64]; /* knight and king moves */ extern bitboard_t bb_knight[64], bb_king[64]; -/* Unsure if it will work with all compilers. Use #define instead ? + +/* TODO (maybe C23?) when we can ensure an enum can be u64 + * + * enum { + * A1bb = mask(A1), A2bb = mask(A2), A3bb = mask(A3), A4bb = mask(A4), + * A5bb = mask(A5), A6bb = mask(A6), A7bb = mask(A7), A8bb = mask(A8), + * B1bb = mask(B1), B2bb = mask(B2), B3bb = mask(B3), B4bb = mask(B4), + * B5bb = mask(B5), B6bb = mask(B6), B7bb = mask(B7), B8bb = mask(B8), + * C1bb = mask(C1), C2bb = mask(C2), C3bb = mask(C3), C4bb = mask(C4), + * C5bb = mask(C5), C6bb = mask(C6), C7bb = mask(C7), C8bb = mask(C8), + * D1bb = mask(D1), D2bb = mask(D2), D3bb = mask(D3), D4bb = mask(D4), + * D5bb = mask(D5), D6bb = mask(D6), D7bb = mask(D7), D8bb = mask(D8), + * E1bb = mask(E1), E2bb = mask(E2), E3bb = mask(E3), E4bb = mask(E4), + * E5bb = mask(E5), E6bb = mask(E6), E7bb = mask(E7), E8bb = mask(E8), + * F1bb = mask(F1), F2bb = mask(F2), F3bb = mask(F3), F4bb = mask(F4), + * F5bb = mask(F5), F6bb = mask(F6), F7bb = mask(F7), F8bb = mask(F8), + * G1bb = mask(G1), G2bb = mask(G2), G3bb = mask(G3), G4bb = mask(G4), + * G5bb = mask(G5), G6bb = mask(G6), G7bb = mask(G7), G8bb = mask(G8), + * H1bb = mask(H1), H2bb = mask(H2), H3bb = mask(H3), H4bb = mask(H4), + * H5bb = mask(H5), H6bb = mask(H6), H7bb = mask(H7), H8bb = mask(H8), + * }; + * + * enum { + * FILE_Abb = 0x0101010101010101ull, FILE_Bbb = 0x0202020202020202ull, + * FILE_Cbb = 0x0404040404040404ull, FILE_Dbb = 0x0808080808080808ull, + * FILE_Ebb = 0x1010101010101010ull, FILE_Fbb = 0x2020202020202020ull, + * FILE_Gbb = 0x4040404040404040ull, FILE_Hbb = 0x8080808080808080ull, + * + * RANK_1bb = 0x00000000000000ffull, RANK_2bb = 0x000000000000ff00ull, + * RANK_3bb = 0x0000000000ff0000ull, RANK_4bb = 0x00000000ff000000ull, + * RANK_5bb = 0x000000ff00000000ull, RANK_6bb = 0x0000ff0000000000ull, + * RANK_7bb = 0x00ff000000000000ull, RANK_8bb = 0xff00000000000000ull + * }; */ -enum { - A1bb = mask(A1), A2bb = mask(A2), A3bb = mask(A3), A4bb = mask(A4), - A5bb = mask(A5), A6bb = mask(A6), A7bb = mask(A7), A8bb = mask(A8), - B1bb = mask(B1), B2bb = mask(B2), B3bb = mask(B3), B4bb = mask(B4), - B5bb = mask(B5), B6bb = mask(B6), B7bb = mask(B7), B8bb = mask(B8), - C1bb = mask(C1), C2bb = mask(C2), C3bb = mask(C3), C4bb = mask(C4), - C5bb = mask(C5), C6bb = mask(C6), C7bb = mask(C7), C8bb = mask(C8), - D1bb = mask(D1), D2bb = mask(D2), D3bb = mask(D3), D4bb = mask(D4), - D5bb = mask(D5), D6bb = mask(D6), D7bb = mask(D7), D8bb = mask(D8), - E1bb = mask(E1), E2bb = mask(E2), E3bb = mask(E3), E4bb = mask(E4), - E5bb = mask(E5), E6bb = mask(E6), E7bb = mask(E7), E8bb = mask(E8), - F1bb = mask(F1), F2bb = mask(F2), F3bb = mask(F3), F4bb = mask(F4), - F5bb = mask(F5), F6bb = mask(F6), F7bb = mask(F7), F8bb = mask(F8), - G1bb = mask(G1), G2bb = mask(G2), G3bb = mask(G3), G4bb = mask(G4), - G5bb = mask(G5), G6bb = mask(G6), G7bb = mask(G7), G8bb = mask(G8), - H1bb = mask(H1), H2bb = mask(H2), H3bb = mask(H3), H4bb = mask(H4), - H5bb = mask(H5), H6bb = mask(H6), H7bb = mask(H7), H8bb = mask(H8), -}; -enum { - FILE_Abb = 0x0101010101010101ULL, FILE_Bbb = 0x0202020202020202ULL, - FILE_Cbb = 0x0404040404040404ULL, FILE_Dbb = 0x0808080808080808ULL, - FILE_Ebb = 0x1010101010101010ULL, FILE_Fbb = 0x2020202020202020ULL, - FILE_Gbb = 0x4040404040404040ULL, FILE_Hbb = 0x8080808080808080ULL, - - RANK_1bb = 0x00000000000000ffULL, RANK_2bb = 0x000000000000ff00ULL, - RANK_3bb = 0x0000000000ff0000ULL, RANK_4bb = 0x00000000ff000000ULL, - RANK_5bb = 0x000000ff00000000ULL, RANK_6bb = 0x0000ff0000000000ULL, - RANK_7bb = 0x00ff000000000000ULL, RANK_8bb = 0xff00000000000000ULL -}; - -/* https://www.chessprogramming.org/Flipping_Mirroring_and_Rotating#Rotation +/* generated with bash: + * R="ABCDEFGH" + * F="12345678" + * for i in {0..63}; do + * printf "#define %c%cbb %#018llxull\n" ${R:i/8:1} ${F:i%8:1} $((1 << i)) + * done */ -static __always_inline bitboard_t bb_rotate_90(bitboard_t b) +#define A1bb 0x0000000000000001ull +#define A2bb 0x0000000000000002ull +#define A3bb 0x0000000000000004ull +#define A4bb 0x0000000000000008ull +#define A5bb 0x0000000000000010ull +#define A6bb 0x0000000000000020ull +#define A7bb 0x0000000000000040ull +#define A8bb 0x0000000000000080ull +#define B1bb 0x0000000000000100ull +#define B2bb 0x0000000000000200ull +#define B3bb 0x0000000000000400ull +#define B4bb 0x0000000000000800ull +#define B5bb 0x0000000000001000ull +#define B6bb 0x0000000000002000ull +#define B7bb 0x0000000000004000ull +#define B8bb 0x0000000000008000ull +#define C1bb 0x0000000000010000ull +#define C2bb 0x0000000000020000ull +#define C3bb 0x0000000000040000ull +#define C4bb 0x0000000000080000ull +#define C5bb 0x0000000000100000ull +#define C6bb 0x0000000000200000ull +#define C7bb 0x0000000000400000ull +#define C8bb 0x0000000000800000ull +#define D1bb 0x0000000001000000ull +#define D2bb 0x0000000002000000ull +#define D3bb 0x0000000004000000ull +#define D4bb 0x0000000008000000ull +#define D5bb 0x0000000010000000ull +#define D6bb 0x0000000020000000ull +#define D7bb 0x0000000040000000ull +#define D8bb 0x0000000080000000ull +#define E1bb 0x0000000100000000ull +#define E2bb 0x0000000200000000ull +#define E3bb 0x0000000400000000ull +#define E4bb 0x0000000800000000ull +#define E5bb 0x0000001000000000ull +#define E6bb 0x0000002000000000ull +#define E7bb 0x0000004000000000ull +#define E8bb 0x0000008000000000ull +#define F1bb 0x0000010000000000ull +#define F2bb 0x0000020000000000ull +#define F3bb 0x0000040000000000ull +#define F4bb 0x0000080000000000ull +#define F5bb 0x0000100000000000ull +#define F6bb 0x0000200000000000ull +#define F7bb 0x0000400000000000ull +#define F8bb 0x0000800000000000ull +#define G1bb 0x0001000000000000ull +#define G2bb 0x0002000000000000ull +#define G3bb 0x0004000000000000ull +#define G4bb 0x0008000000000000ull +#define G5bb 0x0010000000000000ull +#define G6bb 0x0020000000000000ull +#define G7bb 0x0040000000000000ull +#define G8bb 0x0080000000000000ull +#define H1bb 0x0100000000000000ull +#define H2bb 0x0200000000000000ull +#define H3bb 0x0400000000000000ull +#define H4bb 0x0800000000000000ull +#define H5bb 0x1000000000000000ull +#define H6bb 0x2000000000000000ull +#define H7bb 0x4000000000000000ull +#define H8bb 0x8000000000000000ull + +#define FILE_Abb 0x0101010101010101ull +#define FILE_Bbb 0x0202020202020202ull +#define FILE_Cbb 0x0404040404040404ull +#define FILE_Dbb 0x0808080808080808ull +#define FILE_Ebb 0x1010101010101010ull +#define FILE_Fbb 0x2020202020202020ull +#define FILE_Gbb 0x4040404040404040ull +#define FILE_Hbb 0x8080808080808080ull + +#define RANK_1bb 0x00000000000000ffull +#define RANK_2bb 0x000000000000ff00ull +#define RANK_3bb 0x0000000000ff0000ull +#define RANK_4bb 0x00000000ff000000ull +#define RANK_5bb 0x000000ff00000000ull +#define RANK_6bb 0x0000ff0000000000ull +#define RANK_7bb 0x00ff000000000000ull +#define RANK_8bb 0xff00000000000000ull + + +/* +static __always_inline bitboard_t bb_rank(int rank) { - return b; + return RANK_1bb << (rank * 8); +} +static __always_inline bitboard_t bb_file(int file) +{ + return FILE_Abb << file; +} +static __always_inline bitboard_t bb_rel_rank(int rank, color) +{ + return RANK_1bb << (rank * 8); +} +static __always_inline bitboard_t bb_file(int file) +{ + return FILE_Abb << file; +} +*/ + +#define BB_RANK(r) ((u64) RANK_1bb << ((r) * 8)) +#define BB_FILE(f) ((u64) FILE_Abb << (f)) + +#define BB_REL_RANK(r, c) (RANK_1bb << (SQ_REL_RANK((r), (c)) * 8)) +#define BB_REL_FILE(f, c) (FILE_Abb << (SQ_REL_RANK((f), (c)))) + +/** + * bb_sq_aligned() - check if two squares are aligned (same file or rank). + * @sq1: square 1 + * @sq2: square 2 + * + * @sq2 is included in return value, to be non zero if the two squares + * are neighbors. + * + * @return: bitboard of squares between @sq1 and @sq2, including @sq2. + */ +static __always_inline bitboard_t bb_sq_aligned(square_t sq1, square_t sq2) +{ + return bb_between[sq1][sq2]; +} + +/** + * bb_sq_between() - check if a square is between two squares + * @sq: the possibly "in-between" square + * @sq1: square 1 + * @sq2: square 2 + * + * @return: bitboard of @betw if between @sq1 and @sq2. + */ +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); } /* TODO: when OK, replace with macros */ @@ -131,8 +270,8 @@ extern void bitboard_init(void); extern bitboard_t bb_knight_moves(bitboard_t occ, square_t sq); extern bitboard_t bb_king_moves(bitboard_t occ, square_t sq); -extern void bitboard_print(const char *title, const bitboard_t bitboard); -extern void bitboard_print_multi(const char *title, const int n, ...); -extern char *bitboard_rank_sprint(char *str, const uchar bb8); +extern void bb_print(const char *title, const bitboard_t bitboard); +extern void bb_print_multi(const char *title, const int n, ...); +extern char *bb_rank_sprint(char *str, const uchar bb8); #endif /* _BITBOARD_H */ diff --git a/src/board.h b/src/board.h index 3e28cd2..9e5db17 100644 --- a/src/board.h +++ b/src/board.h @@ -20,6 +20,13 @@ #include "piece.h" #include "bitboard.h" +/* from human to machine */ +#define C2FILE(c) (tolower(c) - 'a') +#define C2RANK(c) (tolower(c) - '1') +/* from machine to human */ +#define FILE2C(f) ((f) + 'a') +#define RANK2C(r) ((r) + '1') + /* a square is defined as * rrrfff */ diff --git a/src/chessdefs.h b/src/chessdefs.h index 7bae0b1..e4c317d 100644 --- a/src/chessdefs.h +++ b/src/chessdefs.h @@ -16,21 +16,14 @@ #include "brlib.h" /* brlib types */ -#define ONE 1ull -#define C64(const_u64) const_u64##ULL -#define mask(i) ( (unsigned long long) (ONE << (i)) ) -//typedef ushort board; - -#define BOARDSIZE (8*8) -/* from human to machin e */ -#define C2FILE(c) (tolower(c) - 'a') -#define C2RANK(c) (tolower(c) - '1') -/* from machine to huma n */ -#define FILE2C(f) ((f) + 'a') -#define RANK2C(r) ((r) + '1') +#define ONE 1ull +#define C64(const_u64) const_u64##ULL +#define mask(i) ( (u64) (ONE << (i)) ) +#define BOARDSIZE (8*8) /* relative rank */ -#define REL_RANK(r, c) ((7 * (c)) ^ r) +#define SQ_REL_RANK(r, c) (rank_t)((7 * (c)) ^ r) + /* castle_t bits structure */ typedef enum { @@ -38,10 +31,19 @@ typedef enum { CASTLE_WQ = (1 << 1), /* 0x02 00000010 */ CASTLE_BK = (1 << 2), /* 0x04 00000100 */ CASTLE_BQ = (1 << 3), /* 0x08 00001000 */ + + CASTLE_W = (CASTLE_WK | CASTLE_WQ), /* 00000011 W castle mask */ + CASTLE_B = (CASTLE_BK | CASTLE_BQ), /* 00001100 B castle mask */ + + CASTLE_K = (1 << 0), /* generic K/Q, after doing : */ + CASTLE_Q = (1 << 1), /* flags >> (2 * color) */ } castle_rights_t; -#define CASTLE_W (CASTLE_WK | CASTLE_WQ) /* 00000011 W castle mask */ -#define CASTLE_B (CASTLE_BK | CASTLE_BQ) /* 00001100 B castle mask */ +/* determine is oo or ooo is possible with castle flags f and color c + */ +#define CAN_CASTLE(f, c) ((f) >> (2 * (c))) /* normalize flags to bits 0 & 1 */ +#define CAN_OO(f, c) (CAN_CASTLE(f, c) & CASTLE_K) +#define CAN_OOO(f, c) (CAN_CASTLE(f, c) & CASTLE_Q) /* game phases */ diff --git a/src/fen.c b/src/fen.c index 605b015..0833b7f 100644 --- a/src/fen.c +++ b/src/fen.c @@ -81,9 +81,9 @@ static int fen_check(pos_t *pos) if (pos->en_passant != SQUARE_NONE) { rank_t eprank = sq_rank(pos->en_passant); file_t epfile = sq_file(pos->en_passant); - rank_t rank5 = REL_RANK(RANK_5, pos->turn); - rank_t rank6 = REL_RANK(RANK_6, pos->turn); - rank_t rank7 = REL_RANK(RANK_7, pos->turn); + rank_t rank5 = SQ_REL_RANK(RANK_5, pos->turn); + rank_t rank6 = SQ_REL_RANK(RANK_6, pos->turn); + rank_t rank7 = SQ_REL_RANK(RANK_7, pos->turn); piece_t pawn = pos->turn == WHITE? B_PAWN: W_PAWN; if (warn(eprank != rank6 || pos->board[sq_make(epfile, rank5)] != pawn || diff --git a/src/hyperbola-quintessence.c b/src/hyperbola-quintessence.c index c04953e..ec8f85b 100644 --- a/src/hyperbola-quintessence.c +++ b/src/hyperbola-quintessence.c @@ -132,17 +132,17 @@ static bitboard_t hyperbola_moves(const bitboard_t pieces, const square_t sq, static bitboard_t hyperbola_file_moves(bitboard_t occ, square_t sq) { - return hyperbola_moves(occ, sq, bb_file[sq]); + return hyperbola_moves(occ, sq, bb_sqfile[sq]); } static bitboard_t hyperbola_diag_moves(bitboard_t occ, square_t sq) { - return hyperbola_moves(occ, sq, bb_diag[sq]); + return hyperbola_moves(occ, sq, bb_sqdiag[sq]); } static bitboard_t hyperbola_anti_moves(bitboard_t occ, square_t sq) { - return hyperbola_moves(occ, sq, bb_anti[sq]); + return hyperbola_moves(occ, sq, bb_sqanti[sq]); } bitboard_t hyperbola_bishop_moves(bitboard_t occ, square_t sq) diff --git a/src/move-gen.c b/src/move-gen.c index 8bc5c68..077dbb1 100644 --- a/src/move-gen.c +++ b/src/move-gen.c @@ -14,42 +14,89 @@ #include #include "bitops.h" +#include "bug.h" +#include "chessdefs.h" #include "board.h" #include "bitboard.h" #include "piece.h" #include "position.h" #include "move.h" #include "hyperbola-quintessence.h" +#include "attack.h" #include "move-gen.h" /** - * gen_pawn_push() - generate pawn push + * gen_castle() - generate c * @pos: position * * Generate all pseudo pawn pushes. */ //int gen_pawn_push(pos_t *pos, bitboard_t occ) - //{ +//{ //} +/** + * pseudo_is_legal() - check if a move is legal. + * @pos: position + * @move: move_t + * + * inspired from Demolito engine (https://github.com/lucasart/Demolito) + * + * @return: true if move is valid. + */ +bool pseudo_is_legal(pos_t *pos, move_t move) +{ + int from = move_from(move); + int to = move_to(move); + int us = pos->turn; + int them = OPPONENT(us); + int piece = pos->board[from]; + int king = pos->king[us]; + + if (piece == KING) { + /* We already intermediate squares attacks in pseudo move generation, + * so we only care destination square here. + */ + return sq_attackers(pos, to, them) ? false: true; + } + + /* + * if (bb_test(pins, from) && !bb_test(Ray[king][from], to)) + * return false; + * // En-passant special case: also illegal if self-check through the en-passant captured pawn + * if (to == pos->epSquare && piece == PAWN) { + * const int us = pos->turn, them = opposite(us); + * bitboard_t occ = pos_pieces(pos); + * bb_clear(&occ, from); + * bb_set(&occ, to); + * bb_clear(&occ, to + push_inc(them)); + * return !(bb_rook_attacks(king, occ) & pos_pieces_cpp(pos, them, ROOK, QUEEN)) && + * !(bb_bishop_attacks(king, occ) & pos_pieces_cpp(pos, them, BISHOP, QUEEN)); + * } else + * return true; + */ +} + /** * gen_all_pseudomoves() - generate all pseudo moves * @pos: position * * Generate all moves, no check is done on validity due to castle rules, * or check (pinned pieces, etc...). + * + * @return: The number of moves. */ int gen_all_pseudomoves(pos_t *pos) { - color_t me = pos->turn, enemy = OPPONENT(me); + color_t us = pos->turn, them = OPPONENT(us); - bitboard_t my_pieces = pos->bb[me][ALL_PIECES]; + bitboard_t my_pieces = pos->bb[us][ALL_PIECES]; bitboard_t not_my_pieces = ~my_pieces; - bitboard_t enemy_pieces = pos->bb[enemy][ALL_PIECES]; + bitboard_t enemy_pieces = pos->bb[them][ALL_PIECES]; bitboard_t occ = my_pieces | enemy_pieces; bitboard_t empty = ~occ; @@ -62,78 +109,75 @@ int gen_all_pseudomoves(pos_t *pos) int from, to; + /* king */ + from = pos->king[us]; + movebits = bb_king_moves(not_my_pieces, from); + bit_for_each64(to, tmp2, movebits) { + moves[nmoves++] = move_make(from, to); + } + if (popcount64(pos->checkers) > 1) /* double check, we stop here */ + return (pos->moves.nmoves = nmoves); + /* sliding pieces */ - bit_for_each64(from, tmp1, pos->bb[me][BISHOP]) { + bit_for_each64(from, tmp1, pos->bb[us][BISHOP]) { movebits = hyperbola_bishop_moves(occ, from) & not_my_pieces; bit_for_each64(to, tmp2, movebits) { moves[nmoves++] = move_make(from, to); } } - bit_for_each64(from, tmp1, pos->bb[me][ROOK]) { + bit_for_each64(from, tmp1, pos->bb[us][ROOK]) { // printf("rook=%d/%s\n", from, sq_to_string(from)); movebits = hyperbola_rook_moves(occ, from) & not_my_pieces; bit_for_each64(to, tmp2, movebits) { moves[nmoves++] = move_make(from, to); } } - /* TODO: remove this one */ - bit_for_each64(from, tmp1, pos->bb[me][QUEEN]) { + /* TODO: remove this one, after movegen is validated */ + bit_for_each64(from, tmp1, pos->bb[us][QUEEN]) { movebits = hyperbola_queen_moves(occ, from) & not_my_pieces; - //bitboard_print("bishop", movebits); - //movebits = hyperbola_rook_moves(occ, from); - //bitboard_print("rook", movebits); - //bitboard_print("diag", bb_diag[] movebits); - // & not_my_pieces; bit_for_each64(to, tmp2, movebits) { moves[nmoves++] = move_make(from, to); } } /* knight */ - //bitboard_print("not_my_pieces", not_my_pieces); - bit_for_each64(from, tmp1, pos->bb[me][KNIGHT]) { - //printf("Nfrom=%d=%s\n", from, sq_to_string(from)); + bit_for_each64(from, tmp1, pos->bb[us][KNIGHT]) { movebits = bb_knight_moves(not_my_pieces, from); - //printf("%lx\n", movebits); - //bitboard_print("knight_moves", movebits); - bit_for_each64(to, tmp2, movebits) { - //printf("Nto=%d=%s\n", to, sq_to_string(to)); moves[nmoves++] = move_make(from, to); } } - /* king */ - from = pos->king[me]; - movebits = bb_king_moves(not_my_pieces, from); - bit_for_each64(to, tmp2, movebits) { - moves[nmoves++] = move_make(from, to); - } - /* pawn: relative rank and files */ - bitboard_t rel_rank7 = me == WHITE ? RANK_7bb : RANK_2bb; - bitboard_t rel_rank3 = me == WHITE ? RANK_3bb : RANK_6bb; + bitboard_t rel_rank7 = us == WHITE ? RANK_7bb : RANK_2bb; + bitboard_t rel_rank3 = us == WHITE ? RANK_3bb : RANK_6bb; + //printf("r7_o = %016lx\nr7_n = %016lx\nsize=%lu\n", rel_rank7, BB_REL_RANK(RANK_7, me), + // sizeof(RANK_3bb)); + //printf("r3_o = %016lx\nr3_n = %016lx\nsize=%lu\n", rel_rank3, BB_REL_RANK(RANK_3, me), + // sizeof(RANK_3bb)); + //printf("fc_o = %016lx\nfc_n = %016lx\nsize=%lu\n", FILE_Cbb, BB_REL_FILE(FILE_C, me), + // sizeof(RANK_3bb)); //bitboard_t rel_filea = (me == WHITE ? FILE_Abb : FILE_Hbb); //bitboard_t rel_fileh = (me == WHITE ? FILE_Hbb : FILE_Abb); int en_passant = pos->en_passant == SQUARE_NONE? 0: pos->en_passant; bitboard_t enemy_avail = bb_sq[en_passant] | enemy_pieces; /* pawn: ranks 2-6 push 1 and 2 squares */ - movebits = pawn_shift_up(pos->bb[me][PAWN] & ~rel_rank7, me) & empty; + movebits = pawn_shift_up(pos->bb[us][PAWN] & ~rel_rank7, us) & empty; bit_for_each64(to, tmp1, movebits) { - from = pawn_push_up(to, enemy); /* reverse push */ + from = pawn_push_up(to, them); /* reverse push */ //printf("push %d->%d %s->%s", from, to, sq_to_string(from), sq_to_string(to)); moves[nmoves++] = move_make(from, to); } - movebits = pawn_shift_up(movebits & rel_rank3, me) & empty; + movebits = pawn_shift_up(movebits & rel_rank3, us) & empty; bit_for_each64(to, tmp1, movebits) { - from = pawn_push_up(pawn_push_up(to, enemy), enemy); + from = pawn_push_up(pawn_push_up(to, them), them); moves[nmoves++] = move_make(from, to); } /* pawn: rank 7 push */ - movebits = pawn_shift_up(pos->bb[me][PAWN] & rel_rank7, me) & empty; + movebits = pawn_shift_up(pos->bb[us][PAWN] & rel_rank7, us) & empty; bit_for_each64(to, tmp1, movebits) { - from = pawn_push_up(to, enemy); /* reverse push */ + from = pawn_push_up(to, them); /* reverse push */ moves[nmoves++] = move_make_promote(from, to, QUEEN); moves[nmoves++] = move_make_promote(from, to, ROOK); moves[nmoves++] = move_make_promote(from, to, BISHOP); @@ -141,17 +185,17 @@ int gen_all_pseudomoves(pos_t *pos) } /* pawn: ranks 2-6 captures left, including en-passant */ - from_pawns = pos->bb[me][PAWN] & ~rel_rank7; // & ~rel_filea; - movebits = pawn_shift_upleft(from_pawns, me) & enemy_avail; + from_pawns = pos->bb[us][PAWN] & ~rel_rank7; // & ~rel_filea; + movebits = pawn_shift_upleft(from_pawns, us) & enemy_avail; bit_for_each64(to, tmp1, movebits) { - from = pawn_push_upleft(to, enemy); /* reverse capture */ + from = pawn_push_upleft(to, them); /* reverse capture */ moves[nmoves++] = move_make(from, to); } /* pawn: rank 7 captures left */ - from_pawns = pos->bb[me][PAWN] & rel_rank7; // & ~rel_filea; - movebits = pawn_shift_upleft(from_pawns, me) & enemy_avail; + from_pawns = pos->bb[us][PAWN] & rel_rank7; // & ~rel_filea; + movebits = pawn_shift_upleft(from_pawns, us) & enemy_avail; bit_for_each64(to, tmp1, movebits) { - from = pawn_push_upleft(to, enemy); /* reverse capture */ + from = pawn_push_upleft(to, them); /* reverse capture */ moves[nmoves++] = move_make_promote(from, to, QUEEN); moves[nmoves++] = move_make_promote(from, to, ROOK); moves[nmoves++] = move_make_promote(from, to, BISHOP); @@ -159,17 +203,17 @@ int gen_all_pseudomoves(pos_t *pos) } /* pawn: ranks 2-6 captures right, including en-passant */ - from_pawns = pos->bb[me][PAWN] & ~rel_rank7; // & ~rel_fileh; - movebits = pawn_shift_upright(from_pawns, me) & enemy_avail; + from_pawns = pos->bb[us][PAWN] & ~rel_rank7; // & ~rel_fileh; + movebits = pawn_shift_upright(from_pawns, us) & enemy_avail; bit_for_each64(to, tmp1, movebits) { - from = pawn_push_upright(to, enemy); + from = pawn_push_upright(to, them); moves[nmoves++] = move_make(from, to); } /* pawn: rank 7 captures right */ - from_pawns = pos->bb[me][PAWN] & rel_rank7; // & ~rel_fileh; - movebits = pawn_shift_upright(from_pawns, me) & enemy_pieces; + from_pawns = pos->bb[us][PAWN] & rel_rank7; // & ~rel_fileh; + movebits = pawn_shift_upright(from_pawns, us) & enemy_pieces; bit_for_each64(to, tmp1, movebits) { - from = pawn_push_upright(to, enemy); /* reverse capture */ + from = pawn_push_upright(to, them); /* reverse capture */ moves[nmoves++] = move_make_promote(from, to, QUEEN); moves[nmoves++] = move_make_promote(from, to, ROOK); moves[nmoves++] = move_make_promote(from, to, BISHOP); @@ -177,32 +221,52 @@ int gen_all_pseudomoves(pos_t *pos) } /* castle - Attention ! We consider that castle flags are correct, - * only empty squares between K ans R are tested. */ - static struct { - bitboard_t ooo; - bitboard_t oo; - } castle_empty[2] = { - { B1bb | C1bb | D1bb, F1bb | G1bb }, - { B8bb | C8bb | D8bb, F8bb | G8bb } - }; - static struct { - square_t from; - square_t ooo_to; - square_t oo_to; - } king_move[2] = { - { .from=E1, .ooo_to=C1, .oo_to=G1 }, - { .from=E8, .ooo_to=C8, .oo_to=G8 }, - }; - /* so that bit0 is K castle flag, bit1 is Q castle flag */ - int castle_msk = pos->castle >> (2 * me); - - //int castle_msk = pos->castle >> (2 * color); - if (castle_msk & CASTLE_WK && !(occ & castle_empty[me].oo)) { - moves[nmoves++] = move_make(king_move[me].from, king_move[me].oo_to); - } - if (castle_msk & CASTLE_WQ && !(occ & castle_empty[me].ooo)) { - moves[nmoves++] = move_make(king_move[me].from, king_move[me].ooo_to); + if (!pos->checkers) { + bitboard_t rel_rank1 = BB_REL_RANK(RANK_1, us); + //bitboard_t ooo = rel_rank1 & (FILE_Bbb | FILE_Cbb | FILE_Dbb); + /* + * static struct { + * bitboard_t ooo; + * bitboard_t oo; + * } castle_empty[2] = { + * { B1bb | C1bb | D1bb, F1bb | G1bb }, + * { B8bb | C8bb | D8bb, F8bb | G8bb } + * }; + */ + /* + * static struct { + * square_t from; + * //square_t ooo_to; + * //square_t oo_to; + * } king_move[2] = { + * { .from=E1, }, // .ooo_to=C1, .oo_to=G1 }, + * { .from=E8, } //.ooo_to=C8, .oo_to=G8 }, + * }; + */ + /* so that bit0 is K castle flag, bit1 is Q castle flag */ + //int castle_msk = pos->castle >> (2 * us); + //int can_oo = pos->castle >> (2 * me); + from = pos->king[us]; + square_t from_square[2] = { E1, E8 }; /* verify king is on E1/E8 */ + bug_on(from != from_square[us]); + /* For castle, we check the opponent attacks on squares between from and to. + * To square attack check will be done in gen_is_legal. + */ + if (CAN_OO(pos->castle, us)) { + bitboard_t occmask = rel_rank1 & (FILE_Fbb | FILE_Gbb); + if (!(occ & occmask) && + !sq_attackers(pos, from+1, them)) { /* f1/f8 */ + moves[nmoves++] = move_make_flags(from, from + 2, M_CASTLE_K); + } + } + if (CAN_OOO(pos->castle, us)) { + bitboard_t occmask = rel_rank1 & (FILE_Bbb | FILE_Cbb | FILE_Dbb); + if (!(occ & occmask) && + !sq_attackers(pos, from-1, them)) { /* d1/d8 */ + moves[nmoves++] = move_make_flags(from, from + 2, M_CASTLE_Q); + } + } } /* TODO * DONE. pawn ranks 2-6 advance (1 push, + 2 squares for rank 2) @@ -210,7 +274,7 @@ int gen_all_pseudomoves(pos_t *pos) * DONE. pawns ranks 2-6 captures, left and right * DONE. pawns en-passant (with capture) * DONE. pawns rank 7 capture + promotion - * castle + * DONE. castle * * add function per piece, and type, for easier debug * diff --git a/src/move.c b/src/move.c index 6660c7b..4f69035 100644 --- a/src/move.c +++ b/src/move.c @@ -19,6 +19,7 @@ #include "move.h" #include "position.h" + /* * /\** * * move_print() - print a move @@ -114,7 +115,6 @@ void moves_print(pos_t *pos, __unused int flags) printf("\n"); } - static int _moves_cmp_bysquare(const void *p1, const void *p2) { move_t m1 = *(move_t *)p1; @@ -131,6 +131,7 @@ static int _moves_cmp_bysquare(const void *p1, const void *p2) if (t1 > t2) return 1; return 0; } + /** * move_sort_by_sq() - sort moves by from/to squares ascending * @pos: position. diff --git a/src/move.h b/src/move.h index 06b038a..68e3a73 100644 --- a/src/move.h +++ b/src/move.h @@ -37,8 +37,8 @@ #define M_FLAGS_BEG 18 #define M_HAS_FLAGS mask(M_FLAGS_BEG + 0) /* probably unused */ #define M_CAPTURE mask(M_FLAGS_BEG + 1) -#define M_ENPASSANT mask(M_FLAGS_BEG + 2) -#define M_PROMOTION mask(M_FLAGS_BEG + 3) +#define M_EPASSANT mask(M_FLAGS_BEG + 2) +#define M_PROMOTE mask(M_FLAGS_BEG + 3) #define M_CASTLE_K mask(M_FLAGS_BEG + 4) #define M_CASTLE_Q mask(M_FLAGS_BEG + 5) #define M_CHECK mask(M_FLAGS_BEG + 6) /* probably unknown/useless */ @@ -54,9 +54,14 @@ static inline move_t move_make(square_t from, square_t to) return (to << 6) | from; } +static inline move_t move_make_flags(square_t from, square_t to, int flags) +{ + return move_make(from, to) | flags; +} + static inline move_t move_make_promote(square_t from, square_t to, piece_type_t piece) { - return move_make(from, to) | M_ENPASSANT | (piece << 15); + return move_make(from, to) | M_PROMOTE | (piece << 15); } static inline square_t move_from(move_t move) diff --git a/src/position.h b/src/position.h index 3e9add9..f82bb1d 100644 --- a/src/position.h +++ b/src/position.h @@ -50,7 +50,7 @@ typedef struct __pos_s { * * Both position board and bitboards are modified. */ -static inline void pos_set_sq(pos_t *pos, square_t square, piece_t piece) +static __always_inline void pos_set_sq(pos_t *pos, square_t square, piece_t piece) { color_t color = COLOR(piece); piece_type_t type = PIECE(piece); @@ -68,7 +68,7 @@ static inline void pos_set_sq(pos_t *pos, square_t square, piece_t piece) * * Both position board and bitboards are modified. */ -static inline void pos_clr_sq(pos_t *pos, square_t square) +static __always_inline void pos_clr_sq(pos_t *pos, square_t square) { piece_t piece = pos->board[square]; piece_type_t type = PIECE(piece); @@ -78,6 +78,45 @@ static inline void pos_clr_sq(pos_t *pos, square_t square) pos->bb[color][ALL_PIECES] &= ~mask(square); } +/** + * pos_occ() - get position occupation (all pieces) + * @pos: position + * + * @return: occupation bitboard. + */ +static __always_inline bitboard_t pos_occ(const pos_t *pos) +{ + return pos->bb[WHITE][ALL_PIECES] | pos->bb[BLACK][ALL_PIECES]; +} + +/** + * pos_between_occ() - find occupation between two squares. + * @pos: position + * @sq1: square 1 + * @sq2: square 2 + * + * @return: bitboard of @betw if between @sq1 and @sq2. + */ +static __always_inline bitboard_t pos_between_occ(const pos_t *pos, + const square_t sq1, const square_t sq2) +{ + return bb_between_excl[sq1][sq2] & pos_occ(pos); +} + +/** + * pos_between_count() - count occupied squares between two squares. + * @pos: position + * @sq1: square 1 + * @sq2: square 2 + * + * @return: bitboard of @betw if between @sq1 and @sq2. + */ +static __always_inline int pos_between_count(const pos_t *pos, + const square_t sq1, const square_t sq2) +{ + return bb_between_excl[sq1][sq2] & pos_occ(pos); +} + //void bitboard_print(bitboard_t bb, char *title); //void bitboard_print2(bitboard_t bb1, bitboard_t bb2, char *title); diff --git a/test/bitboard-test.c b/test/bitboard-test.c index 0f4758d..762056d 100644 --- a/test/bitboard-test.c +++ b/test/bitboard-test.c @@ -30,29 +30,29 @@ int main(int __unused ac, __unused char**av) "sliding", "diagonal", "antidiagonal", "file", "rank", "knight", "king" ); - bitboard_print_multi(str, 7, - bb_file[i] | bb_rank[i] | - bb_diag[i] | bb_anti[i], - bb_diag[i], bb_anti[i], - bb_file[i], bb_rank[i], + bb_print_multi(str, 7, + bb_sqfile[i] | bb_sqrank[i] | + bb_sqdiag[i] | bb_sqanti[i], + bb_sqdiag[i], bb_sqanti[i], + bb_sqfile[i], bb_sqrank[i], bb_knight[i], bb_king[i]); } sprintf(str, "between: %-22s%-22s%-22s%-22s%-22s%-22s", "a1-a8", "a1-h8", "a1-h1", "a2-a7", "a2-g7", "a2-g2"); - bitboard_print_multi(str, 6, + bb_print_multi(str, 6, bb_between[A1][A8], bb_between[A1][H8], bb_between[A1][H1], bb_between[A2][A7], bb_between[A2][G7], bb_between[A2][G2]); sprintf(str, "between: %-22s%-22s%-22s%-22s%-22s%-22s%-22s%-22s", "c3-c6", "c3-f6", "c3-f3", "c3-e1", "c3-c1", "c3-a1", "c3-a3", "c3-a5"); - bitboard_print_multi(str, 8, + bb_print_multi(str, 8, bb_between[C3][C6], bb_between[C3][F6], bb_between[C3][F3], bb_between[C3][E1], bb_between[C3][C1], bb_between[C3][A1], bb_between[C3][A3], bb_between[C3][A5]); sprintf(str, "between: %-22s%-22s%-22s%-22s%-22s%-22s%-22s%-22s", "c4-c6", "c4-f6", "c4-f3", "c4-e1", "c4-c1", "c4-a1", "c4-a3", "c4-a5"); - bitboard_print_multi(str, 8, + bb_print_multi(str, 8, bb_between[C4][C6], bb_between[C4][F6], bb_between[C4][F3], bb_between[C4][E1], bb_between[C4][C1], bb_between[C4][A1], diff --git a/test/fen-test.c b/test/fen-test.c index 8526f63..a0c5722 100644 --- a/test/fen-test.c +++ b/test/fen-test.c @@ -35,8 +35,7 @@ int main(__unused int ac, __unused char**av) pos_print_raw(pos, 1); pos2fen(pos, revfen); printf("fen = [%s]\nrev = [%s]", fen, revfen); - if (strcmp(fen, revfen)) - printf(" **FIXED\n"); + printf("%s\n", strcmp(fen, revfen)? "**FIXED\n": ""); pos_del(pos); } }