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

View File

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

View File

@@ -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<<pos, pos = ctz64(tmp))
#endif /* BITS_H */

View File

@@ -20,23 +20,37 @@
*/
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 BLACK 1 /* 0x01 00000001 */
#define PAWN (1 << 1) /* 0x02 00000010 */
#define KNIGHT (1 << 2) /* 0x04 00000100 */
#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_PIECE 0x3F /* 00111111 */
#define MASK_COLOR 0x80 /* 10000000 */
#define MASK_COLOR 0x01 /* 00000001 */
#define MASK_PIECE 0x7E /* 01111110 */
#define COLOR(p) ((p) & MASK_COLOR)
#define COLOR(p) ((p) & MASK_COLOR) /* bitmask */
#define VCOLOR(p) (!!COLOR(p)) /* WHITE/BLACK */
#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_BLACK(p) (COLOR(p))

View File

@@ -56,7 +56,7 @@ pos_t *fen2pos(pos_t *pos, char *fen)
/* 1) get piece placement information
*/
for (rank = 7, file = 0; *p && *p != ' '; ++p) {
color = !!islower(*p);
color = isupper(*p)? WHITE: BLACK;
char cp = toupper(*p);
switch (cp) {
case CHAR_PAWN:
@@ -84,9 +84,10 @@ pos_t *fen2pos(pos_t *pos, char *fen)
file, rank, *p, cp, color);
# endif
pos->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) {

View File

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

View File

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

View File

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

View File

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