From 7c98f0d4c5255a05b2584ededc5b306857d452e9 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Wed, 10 Nov 2021 15:54:13 +0100 Subject: [PATCH] piece_t change, pack piece_details, conversion piece: mask<=>number --- Makefile | 14 ++++++++++---- src/bits.c | 6 +++++- src/bits.h | 7 ++++++- src/chessdefs.h | 36 +++++++++++++++++++++++++----------- src/fen.c | 11 ++++++----- src/move.c | 26 ++++++++++++++++++++------ src/piece.c | 22 ++++++++++------------ src/piece.h | 14 +++++++------- src/position.c | 6 +++--- 9 files changed, 92 insertions(+), 50 deletions(-) diff --git a/Makefile b/Makefile index d206fb9..176ef4a 100644 --- a/Makefile +++ b/Makefile @@ -38,20 +38,27 @@ CFLAGS += -Wmissing-declarations ##################################### DEBUG flags CFLAGS += -DDEBUG # global -CFLAGS += -DDEBUG_POOL # memory pools management +#CFLAGS += -DDEBUG_POOL # memory pools management CFLAGS += -DDEBUG_FEN # FEN decoding CFLAGS += -DDEBUG_MOVE # move generation CFLAGS += -DDEBUG_EVAL # eval functions -CFLAGS += -DDEBUG_BITS # bits functions +CFLAGS += -DDEBUG_PIECE # piece list management +#CFLAGS += -DDEBUG_BITS # bits functions (take care !) #CFLAGS += -DDEBUG_EVAL # sleep 1 sec within main loop (SIGINTR test) #CFLAGS += -DDEBUG_EVAL2 # eval 2 #CFLAGS += -DDEBUG_EVAL3 # eval 3 #CFLAGS += -DDEBUG_MEM # malloc -all: $(OBJ) $(BIN) +.PHONY: all cflags clean + +compile: cflags $(OBJ) $(BIN) + +cflags: @echo CFLAGS used: $(CFLAGS) +all: clean compile + $(DEPS): $(SRC) $(INC) @echo generating dependancies. @$(CC) -MM $(SRC) > $@ @@ -59,7 +66,6 @@ $(DEPS): $(SRC) $(INC) include $(DEPS) -.PHONY: clean clean: rm -rf $(OBJ) core $(BIN) $(DEPS) diff --git a/src/bits.c b/src/bits.c index 0e28872..47e567e 100644 --- a/src/bits.c +++ b/src/bits.c @@ -72,7 +72,11 @@ int main(int ac, char **av) printf("\n"); bit_for_each64(curbit, _tmp, u) { - printf("loop: curbit=%d\n", curbit); + printf("loop: curbit=%d tmp=%ld\n", curbit, _tmp); + } + printf("\n"); + bit_for_each64_2(curbit, _tmp, u) { + printf("loop2: curbit=%d tmp=%ld\n", curbit, _tmp); } } diff --git a/src/bits.h b/src/bits.h index c3c1dc7..1995390 100644 --- a/src/bits.h +++ b/src/bits.h @@ -153,6 +153,11 @@ static inline int popcount64(u64 n) * I should probably re-think the implementation... */ #define bit_for_each64(pos, tmp, ul) \ - for (tmp = ul, pos = ffs64(tmp); pos; tmp &= (tmp - 1), pos = ffs64(tmp)) + for (tmp = ul, pos = ffs64(tmp); tmp; tmp &= (tmp - 1), pos = ffs64(tmp)) + +/** or would it be more useful (counting bits from zero instead of 1) ? + */ +#define bit_for_each64_2(pos, tmp, ul) \ + for (tmp = ul, pos = ctz64(tmp); tmp; tmp ^= 1<occupied[color] |= (1LL << BB(file, rank)); - piece |= color; + SET_COLOR(piece, color); board[SQ88(file, rank)].piece = piece; - board[SQ88(file, rank)].s_piece = piece_add(pos, piece, SQUARE(file, rank)); + board[SQ88(file, rank)].s_piece = + piece_add(pos, piece, SQUARE(file, rank)); file++; break; case '/': @@ -112,7 +113,7 @@ pos_t *fen2pos(pos_t *pos, char *fen) /* 2) next move color */ SKIP_BLANK(p); - pos->turn = *p == 'w' ? WHITE : BLACK; + SET_COLOR(pos->turn, *p == 'w' ? WHITE : BLACK); p++; /* 3) castle status @@ -164,7 +165,7 @@ int main(int ac, char**av) { pos_t *pos; - debug_init(3); + debug_init(5); piece_pool_init(); pos = pos_create(); if (ac == 1) { diff --git a/src/move.c b/src/move.c index 4f55d93..4c00144 100644 --- a/src/move.c +++ b/src/move.c @@ -180,6 +180,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece) { piece_t piece = PIECE(ppiece->piece); unsigned char color = COLOR(ppiece->piece); + unsigned char vcolor = VCOLOR(ppiece->piece); square_t square = ppiece->square, new; board_t *board = pos->board; unsigned char rank2, rank7, rank5; @@ -188,7 +189,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece) int count = 0; /* setup direction */ - if (color == WHITE) { + if (IS_WHITE(color)) { dir = 1; rank2 = 1; rank7 = 6; @@ -200,6 +201,18 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece) rank5 = 3; } +# ifdef DEBUG_MOVE + log_f(4, "pos:%p turn:%s piece:%d [%s %s] dir:%d at %#04x[%c%c]\n", + pos, + IS_WHITE(pos->turn)? "white": "black", + piece, + IS_WHITE(color)? "white": "black", + P_NAME(piece), + dir, + square, + FILE2C(GET_F(square)), RANK2C(GET_R(square))); +# endif + /* normal push. We do not test for valid destination square here, * assuming position is valid. Is that correct ? */ @@ -251,7 +264,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece) # endif if (SQ88_NOK(new)) continue; - pos->controlled[color] |= (1ULL << BB(FILE88(new), RANK88(new))); + pos->controlled[vcolor] |= (1ULL << BB(FILE88(new), RANK88(new))); if (board[new].piece && COLOR(board[new].piece) != color) { if ((move = move_pawn_add(pos, piece | color, square, new, rank7))) count++; @@ -268,7 +281,7 @@ int pseudo_moves_castle(pos_t *pos) move_t *move = NULL; unsigned short count=0; - if (color == WHITE) { + if (IS_WHITE(color)) { rank1 = 0; castle_K = pos->castle & CASTLE_WK; castle_Q = pos->castle & CASTLE_WQ; @@ -310,6 +323,7 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece) { piece_t piece = PIECE(ppiece->piece); unsigned char color = COLOR(ppiece->piece); + unsigned char vcolor = VCOLOR(ppiece->piece); struct vector *vector = vectors+piece; square_t square = ppiece->square; unsigned char ndirs = vector->ndirs; @@ -321,9 +335,9 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece) # ifdef DEBUG_MOVE log_f(4, "pos:%p turn:%s piece:%d [%s %s] at %#04x[%c%c]\n", pos, - pos->turn ==WHITE? "white": "black", + IS_WHITE(pos->turn)? "white": "black", piece, - color==WHITE? "white": "black", P_NAME(piece), + IS_WHITE(color)? "white": "black", P_NAME(piece), square, FILE2C(GET_F(square)), RANK2C(GET_R(square))); log_i(5, "vector=%ld ndirs=%d slide=%d\n", vector-vectors, ndirs, slide); @@ -343,7 +357,7 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece) log_i(4, "trying %c%c\n", FILE2C(GET_F(new)), RANK2C(GET_R(new))); # endif - pos->controlled[color] |= (1ULL << BB(FILE88(new), RANK88(new))); + pos->controlled[vcolor] |= (1ULL << BB(FILE88(new), RANK88(new))); if (board[new].piece) { # ifdef DEBUG_MOVE log_i(5, "color=%d color2=%d\n", color, COLOR(board[new].piece)); diff --git a/src/piece.c b/src/piece.c index e246b49..0b0e0f1 100644 --- a/src/piece.c +++ b/src/piece.c @@ -20,20 +20,19 @@ static pool_t *pieces_pool; struct piece_details piece_details[] = { - [EMPTY] = { ' ', ' ', " ", " ", "", 0 }, - [PAWN] = { 'P', 'p', "♙", "♟", "Pawn", PAWN_VALUE }, - [KNIGHT] = { 'N', 'n', "♘", "♞", "Knight", KNIGHT_VALUE }, - [BISHOP] = { 'B', 'b', "♗", "♝", "Bishop", BISHOP_VALUE }, - [ROOK] = { 'R', 'r', "♖", "♜", "Rook", ROOK_VALUE }, - [QUEEN] = { 'Q', 'q', "♕", "♛", "Queen", QUEEN_VALUE }, - [KING] = { 'K', 'k', "♔", "♚", "King", KING_VALUE } + [E_EMPTY] = { ' ', ' ', " ", " ", "", 0 }, + [E_PAWN] = { 'P', 'p', "♙", "♟", "Pawn", PAWN_VALUE }, + [E_KNIGHT] = { 'N', 'n', "♘", "♞", "Knight", KNIGHT_VALUE }, + [E_BISHOP] = { 'B', 'b', "♗", "♝", "Bishop", BISHOP_VALUE }, + [E_ROOK] = { 'R', 'r', "♖", "♜", "Rook", ROOK_VALUE }, + [E_QUEEN] = { 'Q', 'q', "♕", "♛", "Queen", QUEEN_VALUE }, + [E_KING] = { 'K', 'k', "♔", "♚", "King", KING_VALUE } }; void piece_list_print(struct list_head *list) { struct list_head *p_cur, *tmp; piece_list_t *piece; - int i = 0; list_for_each_safe(p_cur, tmp, list) { piece = list_entry(p_cur, piece_list_t, list); @@ -41,9 +40,8 @@ void piece_list_print(struct list_head *list) printf("%s%c%c ", P_SYM(piece->piece), FILE2C(GET_F(piece->square)), RANK2C(GET_R(piece->square))); - i++; } - printf("Total pieces = %d\n", i); + printf("\n"); } pool_t *piece_pool_init() @@ -65,12 +63,12 @@ static eval_t pieces_values[] = { piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square) { piece_list_t *new; - short color = COLOR(piece); + short color = VCOLOR(piece); # ifdef DEBUG_PIECE log_f(2, "piece=%02x square=%02x\n", piece, square); log_f(5, "Adding %s %s on %c%c\n", color? "Black": "White", - piece2string(piece), FILE2C(GET_F(square)), RANK2C(GET_R(square))); + P_NAME(piece), FILE2C(GET_F(square)), RANK2C(GET_R(square))); # endif if ((new = pool_get(pieces_pool))) { list_add_tail(&new->list, &pos->pieces[color]); diff --git a/src/piece.h b/src/piece.h index 5859354..357a8ab 100644 --- a/src/piece.h +++ b/src/piece.h @@ -51,13 +51,13 @@ extern struct piece_details { int64_t value; } piece_details[]; -#define P_NAME(p) piece_details[PIECE(p)].name -#define P_LETTER(p) piece_details[PIECE(p)].abbrev_w -#define P_SYM(p) piece_details[PIECE(p)].symbol_b -#define P_CSHORT(p) (COLOR(p) == WHITE? piece_details[PIECE(p)].abbrev_w: \ - piece_details[PIECE(p)].abbrev_b) -#define P_CSYM(p) (COLOR(p) == WHITE? piece_details[PIECE(p)].symbol_w: \ - piece_details[PIECE(p)].symbol_b) +#define P_NAME(p) piece_details[E_PIECE(p)].name +#define P_LETTER(p) piece_details[E_PIECE(p)].abbrev_w +#define P_SYM(p) piece_details[E_PIECE(p)].symbol_b +#define P_CSHORT(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].abbrev_w: \ + piece_details[E_PIECE(p)].abbrev_b) +#define P_CSYM(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].symbol_w: \ + piece_details[E_PIECE(p)].symbol_b) /* use short name or symbol - no effect */ #define P_USE_UTF 1 diff --git a/src/position.c b/src/position.c index 1685a3e..28124ac 100644 --- a/src/position.c +++ b/src/position.c @@ -53,9 +53,9 @@ inline void bitboard_print2(bitboard_t bb1, bitboard_t bb2) void pos_pieces_print(pos_t *pos) { - printf("White pieces : \n\t"); + printf("White pieces (%d): \t", popcount64(pos->occupied[WHITE])); piece_list_print(&pos->pieces[WHITE]); - printf("Black pieces : \n\t"); + printf("Black pieces (%d): \t", popcount64(pos->occupied[BLACK])); piece_list_print(&pos->pieces[BLACK]); } @@ -129,7 +129,7 @@ pos_t *pos_init(pos_t *pos) } } - pos->turn = WHITE; + SET_WHITE(pos->turn); pos->castle = 0; pos->clock_50 = 0; pos->curmove = 0;