piece_t change, pack piece_details, conversion piece: mask<=>number

This commit is contained in:
2021-11-10 15:54:13 +01:00
parent bc07a949b0
commit 7c98f0d4c5
9 changed files with 92 additions and 50 deletions

View File

@@ -38,20 +38,27 @@ CFLAGS += -Wmissing-declarations
##################################### DEBUG flags ##################################### DEBUG flags
CFLAGS += -DDEBUG # global CFLAGS += -DDEBUG # global
CFLAGS += -DDEBUG_POOL # memory pools management #CFLAGS += -DDEBUG_POOL # memory pools management
CFLAGS += -DDEBUG_FEN # FEN decoding CFLAGS += -DDEBUG_FEN # FEN decoding
CFLAGS += -DDEBUG_MOVE # move generation CFLAGS += -DDEBUG_MOVE # move generation
CFLAGS += -DDEBUG_EVAL # eval functions 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_EVAL # sleep 1 sec within main loop (SIGINTR test)
#CFLAGS += -DDEBUG_EVAL2 # eval 2 #CFLAGS += -DDEBUG_EVAL2 # eval 2
#CFLAGS += -DDEBUG_EVAL3 # eval 3 #CFLAGS += -DDEBUG_EVAL3 # eval 3
#CFLAGS += -DDEBUG_MEM # malloc #CFLAGS += -DDEBUG_MEM # malloc
all: $(OBJ) $(BIN) .PHONY: all cflags clean
compile: cflags $(OBJ) $(BIN)
cflags:
@echo CFLAGS used: $(CFLAGS) @echo CFLAGS used: $(CFLAGS)
all: clean compile
$(DEPS): $(SRC) $(INC) $(DEPS): $(SRC) $(INC)
@echo generating dependancies. @echo generating dependancies.
@$(CC) -MM $(SRC) > $@ @$(CC) -MM $(SRC) > $@
@@ -59,7 +66,6 @@ $(DEPS): $(SRC) $(INC)
include $(DEPS) include $(DEPS)
.PHONY: clean
clean: clean:
rm -rf $(OBJ) core $(BIN) $(DEPS) rm -rf $(OBJ) core $(BIN) $(DEPS)

View File

@@ -72,7 +72,11 @@ int main(int ac, char **av)
printf("\n"); printf("\n");
bit_for_each64(curbit, _tmp, u) { 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);
} }
} }

View File

@@ -153,6 +153,11 @@ static inline int popcount64(u64 n)
* I should probably re-think the implementation... * I should probably re-think the implementation...
*/ */
#define bit_for_each64(pos, tmp, ul) \ #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<<pos, pos = ctz64(tmp))
#endif /* BITS_H */ #endif /* BITS_H */

View File

@@ -20,23 +20,37 @@
*/ */
typedef u8 piece_t; typedef u8 piece_t;
#define EMPTY 0 enum {
E_EMPTY = 0,
E_PAWN,
E_KNIGHT,
E_BISHOP,
E_ROOK,
E_QUEEN,
E_KING,
E_COLOR = 8
};
enum {
EMPTY = 0,
PAWN = 1 << (E_PAWN - 1), /* 0x01 00000001 */
KNIGHT = 1 << (E_KNIGHT - 1), /* 0x02 00000010 */
BISHOP = 1 << (E_BISHOP - 1), /* 0x04 00000100 */
ROOK = 1 << (E_ROOK - 1), /* 0x08 00001000 */
QUEEN = 1 << (E_QUEEN - 1), /* 0x10 00010000 */
KING = 1 << (E_KING - 1), /* 0x20 00100000 */
};
#define WHITE 0 /* 0x00 00000000 */ #define WHITE 0 /* 0x00 00000000 */
#define BLACK 1 /* 0x01 00000001 */ #define BLACK 1 /* 0x01 00000001 */
#define PAWN (1 << 1) /* 0x02 00000010 */ #define MASK_PIECE 0x3F /* 00111111 */
#define KNIGHT (1 << 2) /* 0x04 00000100 */ #define MASK_COLOR 0x80 /* 10000000 */
#define BISHOP (1 << 3) /* 0x08 00001000 */
#define ROOK (1 << 4) /* 0x10 00010000 */
#define QUEEN (1 << 5) /* 0x20 00100000 */
#define KING (1 << 6) /* 0x40 01000000 */
#define MASK_COLOR 0x01 /* 00000001 */ #define COLOR(p) ((p) & MASK_COLOR) /* bitmask */
#define MASK_PIECE 0x7E /* 01111110 */ #define VCOLOR(p) (!!COLOR(p)) /* WHITE/BLACK */
#define COLOR(p) ((p) & MASK_COLOR)
#define PIECE(p) ((p) & MASK_PIECE) #define PIECE(p) ((p) & MASK_PIECE)
#define E_PIECE(p) (ffs64(PIECE(p))) /* convert mask to E_XX */
#define IS_WHITE(p) (!COLOR(p)) #define IS_WHITE(p) (!COLOR(p))
#define IS_BLACK(p) (COLOR(p)) #define IS_BLACK(p) (COLOR(p))

View File

@@ -56,7 +56,7 @@ pos_t *fen2pos(pos_t *pos, char *fen)
/* 1) get piece placement information /* 1) get piece placement information
*/ */
for (rank = 7, file = 0; *p && *p != ' '; ++p) { for (rank = 7, file = 0; *p && *p != ' '; ++p) {
color = !!islower(*p); color = isupper(*p)? WHITE: BLACK;
char cp = toupper(*p); char cp = toupper(*p);
switch (cp) { switch (cp) {
case CHAR_PAWN: case CHAR_PAWN:
@@ -84,9 +84,10 @@ pos_t *fen2pos(pos_t *pos, char *fen)
file, rank, *p, cp, color); file, rank, *p, cp, color);
# endif # endif
pos->occupied[color] |= (1LL << BB(file, rank)); pos->occupied[color] |= (1LL << BB(file, rank));
piece |= color; SET_COLOR(piece, color);
board[SQ88(file, rank)].piece = piece; 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++; file++;
break; break;
case '/': case '/':
@@ -112,7 +113,7 @@ pos_t *fen2pos(pos_t *pos, char *fen)
/* 2) next move color /* 2) next move color
*/ */
SKIP_BLANK(p); SKIP_BLANK(p);
pos->turn = *p == 'w' ? WHITE : BLACK; SET_COLOR(pos->turn, *p == 'w' ? WHITE : BLACK);
p++; p++;
/* 3) castle status /* 3) castle status
@@ -164,7 +165,7 @@ int main(int ac, char**av)
{ {
pos_t *pos; pos_t *pos;
debug_init(3); debug_init(5);
piece_pool_init(); piece_pool_init();
pos = pos_create(); pos = pos_create();
if (ac == 1) { if (ac == 1) {

View File

@@ -180,6 +180,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece)
{ {
piece_t piece = PIECE(ppiece->piece); piece_t piece = PIECE(ppiece->piece);
unsigned char color = COLOR(ppiece->piece); unsigned char color = COLOR(ppiece->piece);
unsigned char vcolor = VCOLOR(ppiece->piece);
square_t square = ppiece->square, new; square_t square = ppiece->square, new;
board_t *board = pos->board; board_t *board = pos->board;
unsigned char rank2, rank7, rank5; unsigned char rank2, rank7, rank5;
@@ -188,7 +189,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece)
int count = 0; int count = 0;
/* setup direction */ /* setup direction */
if (color == WHITE) { if (IS_WHITE(color)) {
dir = 1; dir = 1;
rank2 = 1; rank2 = 1;
rank7 = 6; rank7 = 6;
@@ -200,6 +201,18 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece)
rank5 = 3; 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, /* normal push. We do not test for valid destination square here,
* assuming position is valid. Is that correct ? * assuming position is valid. Is that correct ?
*/ */
@@ -251,7 +264,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece)
# endif # endif
if (SQ88_NOK(new)) if (SQ88_NOK(new))
continue; 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 (board[new].piece && COLOR(board[new].piece) != color) {
if ((move = move_pawn_add(pos, piece | color, square, new, rank7))) if ((move = move_pawn_add(pos, piece | color, square, new, rank7)))
count++; count++;
@@ -268,7 +281,7 @@ int pseudo_moves_castle(pos_t *pos)
move_t *move = NULL; move_t *move = NULL;
unsigned short count=0; unsigned short count=0;
if (color == WHITE) { if (IS_WHITE(color)) {
rank1 = 0; rank1 = 0;
castle_K = pos->castle & CASTLE_WK; castle_K = pos->castle & CASTLE_WK;
castle_Q = pos->castle & CASTLE_WQ; 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); piece_t piece = PIECE(ppiece->piece);
unsigned char color = COLOR(ppiece->piece); unsigned char color = COLOR(ppiece->piece);
unsigned char vcolor = VCOLOR(ppiece->piece);
struct vector *vector = vectors+piece; struct vector *vector = vectors+piece;
square_t square = ppiece->square; square_t square = ppiece->square;
unsigned char ndirs = vector->ndirs; unsigned char ndirs = vector->ndirs;
@@ -321,9 +335,9 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece)
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_f(4, "pos:%p turn:%s piece:%d [%s %s] at %#04x[%c%c]\n", log_f(4, "pos:%p turn:%s piece:%d [%s %s] at %#04x[%c%c]\n",
pos, pos,
pos->turn ==WHITE? "white": "black", IS_WHITE(pos->turn)? "white": "black",
piece, piece,
color==WHITE? "white": "black", P_NAME(piece), IS_WHITE(color)? "white": "black", P_NAME(piece),
square, square,
FILE2C(GET_F(square)), RANK2C(GET_R(square))); FILE2C(GET_F(square)), RANK2C(GET_R(square)));
log_i(5, "vector=%ld ndirs=%d slide=%d\n", vector-vectors, ndirs, slide); 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))); log_i(4, "trying %c%c\n", FILE2C(GET_F(new)), RANK2C(GET_R(new)));
# endif # endif
pos->controlled[color] |= (1ULL << BB(FILE88(new), RANK88(new))); pos->controlled[vcolor] |= (1ULL << BB(FILE88(new), RANK88(new)));
if (board[new].piece) { if (board[new].piece) {
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(5, "color=%d color2=%d\n", color, COLOR(board[new].piece)); log_i(5, "color=%d color2=%d\n", color, COLOR(board[new].piece));

View File

@@ -20,20 +20,19 @@
static pool_t *pieces_pool; static pool_t *pieces_pool;
struct piece_details piece_details[] = { struct piece_details piece_details[] = {
[EMPTY] = { ' ', ' ', " ", " ", "", 0 }, [E_EMPTY] = { ' ', ' ', " ", " ", "", 0 },
[PAWN] = { 'P', 'p', "", "", "Pawn", PAWN_VALUE }, [E_PAWN] = { 'P', 'p', "", "", "Pawn", PAWN_VALUE },
[KNIGHT] = { 'N', 'n', "", "", "Knight", KNIGHT_VALUE }, [E_KNIGHT] = { 'N', 'n', "", "", "Knight", KNIGHT_VALUE },
[BISHOP] = { 'B', 'b', "", "", "Bishop", BISHOP_VALUE }, [E_BISHOP] = { 'B', 'b', "", "", "Bishop", BISHOP_VALUE },
[ROOK] = { 'R', 'r', "", "", "Rook", ROOK_VALUE }, [E_ROOK] = { 'R', 'r', "", "", "Rook", ROOK_VALUE },
[QUEEN] = { 'Q', 'q', "", "", "Queen", QUEEN_VALUE }, [E_QUEEN] = { 'Q', 'q', "", "", "Queen", QUEEN_VALUE },
[KING] = { 'K', 'k', "", "", "King", KING_VALUE } [E_KING] = { 'K', 'k', "", "", "King", KING_VALUE }
}; };
void piece_list_print(struct list_head *list) void piece_list_print(struct list_head *list)
{ {
struct list_head *p_cur, *tmp; struct list_head *p_cur, *tmp;
piece_list_t *piece; piece_list_t *piece;
int i = 0;
list_for_each_safe(p_cur, tmp, list) { list_for_each_safe(p_cur, tmp, list) {
piece = list_entry(p_cur, piece_list_t, 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), printf("%s%c%c ", P_SYM(piece->piece),
FILE2C(GET_F(piece->square)), FILE2C(GET_F(piece->square)),
RANK2C(GET_R(piece->square))); RANK2C(GET_R(piece->square)));
i++;
} }
printf("Total pieces = %d\n", i); printf("\n");
} }
pool_t *piece_pool_init() 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 *piece_add(pos_t *pos, piece_t piece, square_t square)
{ {
piece_list_t *new; piece_list_t *new;
short color = COLOR(piece); short color = VCOLOR(piece);
# ifdef DEBUG_PIECE # ifdef DEBUG_PIECE
log_f(2, "piece=%02x square=%02x\n", piece, square); log_f(2, "piece=%02x square=%02x\n", piece, square);
log_f(5, "Adding %s %s on %c%c\n", color? "Black": "White", 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 # endif
if ((new = pool_get(pieces_pool))) { if ((new = pool_get(pieces_pool))) {
list_add_tail(&new->list, &pos->pieces[color]); list_add_tail(&new->list, &pos->pieces[color]);

View File

@@ -51,13 +51,13 @@ extern struct piece_details {
int64_t value; int64_t value;
} piece_details[]; } piece_details[];
#define P_NAME(p) piece_details[PIECE(p)].name #define P_NAME(p) piece_details[E_PIECE(p)].name
#define P_LETTER(p) piece_details[PIECE(p)].abbrev_w #define P_LETTER(p) piece_details[E_PIECE(p)].abbrev_w
#define P_SYM(p) piece_details[PIECE(p)].symbol_b #define P_SYM(p) piece_details[E_PIECE(p)].symbol_b
#define P_CSHORT(p) (COLOR(p) == WHITE? piece_details[PIECE(p)].abbrev_w: \ #define P_CSHORT(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].abbrev_w: \
piece_details[PIECE(p)].abbrev_b) piece_details[E_PIECE(p)].abbrev_b)
#define P_CSYM(p) (COLOR(p) == WHITE? piece_details[PIECE(p)].symbol_w: \ #define P_CSYM(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].symbol_w: \
piece_details[PIECE(p)].symbol_b) piece_details[E_PIECE(p)].symbol_b)
/* use short name or symbol - no effect /* use short name or symbol - no effect
*/ */
#define P_USE_UTF 1 #define P_USE_UTF 1

View File

@@ -53,9 +53,9 @@ inline void bitboard_print2(bitboard_t bb1, bitboard_t bb2)
void pos_pieces_print(pos_t *pos) 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]); 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]); 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->castle = 0;
pos->clock_50 = 0; pos->clock_50 = 0;
pos->curmove = 0; pos->curmove = 0;