simplify 0x88 macros / remove redundant chessdefs.h equivalent ones

This commit is contained in:
2021-11-20 16:15:45 +01:00
parent 6b2c1702f6
commit 05a64ec742
6 changed files with 61 additions and 61 deletions

View File

@@ -27,9 +27,12 @@ typedef struct board_s {
/* definitions for 0x88 representation /* definitions for 0x88 representation
*/ */
#define SQ88(f, r) (16 * (r) + (f)) /* from rank,file to sq88 */ #define SQ88(f, r) (((r) << 4) | (f)) /* from rank,file to sq88 */
#define FILE88(s) ((s) & 7) /* from sq88 to file */ #define F88(s) ((s) & 0x0f) /* from sq88 to file */
#define RANK88(s) ((s) >> 4) /* from sq88 to rank */ #define R88(s) ((s) >> 4) /* from sq88 to rank */
#define SETF88(s, r) ((s) &= 0xf0, (s) |= (r))
#define SETR88(s, f) ((s) &= 0x0f, (s) |= (f)<<4)
#define SQ88_NOK(s) ((s) & 0x88) /* invalid square */ #define SQ88_NOK(s) ((s) & 0x88) /* invalid square */
#define SQ88_OK(s) (!SQ88_NOK(s)) #define SQ88_OK(s) (!SQ88_NOK(s))
@@ -37,7 +40,7 @@ typedef struct board_s {
/* definitions for bitboard representation /* definitions for bitboard representation
*/ */
#define BB(f, r) (1ULL << (8 * (r) + (f))) /* from rank,file to bitboard */ #define BB(f, r) (1ULL << (8 * (r) + (f))) /* from rank,file to bitboard */
#define SQ88_2_BB(s) (BB(FILE88(s), RANK88(s))) /* from sq88 to bitboard */ #define SQ88_2_BB(s) (BB(F88(s), R88(s))) /* from sq88 to bitboard */
#define FILEBB(b) ((b) % 8) /* from sq88 to file */ #define FILEBB(b) ((b) % 8) /* from sq88 to file */
#define RANKBB(b) ((b) / 8) /* from sq88 to rank */ #define RANKBB(b) ((b) / 8) /* from sq88 to rank */

View File

@@ -36,7 +36,7 @@ enum {
*/ */
enum { enum {
EMPTY = 0, EMPTY = 0,
PAWN = 1 << (E_PAWN - 1), /* 0x02 00000010 */ PAWN = 1 << (E_PAWN - 1), /* 1<<(2-1) = 0x02 00000010 */
KNIGHT = 1 << (E_KNIGHT - 1), /* 0x04 00000100 */ KNIGHT = 1 << (E_KNIGHT - 1), /* 0x04 00000100 */
BISHOP = 1 << (E_BISHOP - 1), /* 0x08 00001000 */ BISHOP = 1 << (E_BISHOP - 1), /* 0x08 00001000 */
ROOK = 1 << (E_ROOK - 1), /* 0x10 00010000 */ ROOK = 1 << (E_ROOK - 1), /* 0x10 00010000 */
@@ -68,12 +68,6 @@ enum {
*/ */
typedef unsigned char square_t; typedef unsigned char square_t;
#define GET_R(s) ((s) >> 4)
#define GET_F(s) ((s) & 0x0f)
#define SET_R(s, f) ((s) &= 0x0f, (s) |= (f)<<4)
#define SET_F(s, r) ((s) &= 0xf0, (s) |= (r))
#define SQUARE(f, r) ((r) << 4 | (f))
/* castle_t bits structure /* castle_t bits structure
*/ */
typedef unsigned char castle_t; typedef unsigned char castle_t;

View File

@@ -88,7 +88,7 @@ pos_t *fen2pos(pos_t *pos, char *fen)
SET_COLOR(piece, color); SET_COLOR(piece, color);
board[SQ88(file, rank)].piece = piece; board[SQ88(file, rank)].piece = piece;
board[SQ88(file, rank)].s_piece = board[SQ88(file, rank)].s_piece =
piece_add(pos, piece, SQUARE(file, rank)); piece_add(pos, piece, SQ88(file, rank));
file++; file++;
break; break;
case '/': case '/':
@@ -146,8 +146,10 @@ pos_t *fen2pos(pos_t *pos, char *fen)
SKIP_BLANK(p); SKIP_BLANK(p);
pos->en_passant = 0; pos->en_passant = 0;
if (*p != '-') { if (*p != '-') {
SET_F(pos->en_passant, C2FILE(*p++)); //SET_F(pos->en_passant, C2FILE(*p++));
SET_R(pos->en_passant, C2RANK(*p++)); //SET_R(pos->en_passant, C2RANK(*p++));
pos->en_passant = SQ88(C2FILE(*p), C2RANK(*(p+1)));
pos += 2;
} else { } else {
p++; p++;
} }

View File

@@ -82,8 +82,8 @@ int move_print(move_t *move, move_flags_t flags)
goto end; goto end;
} else { } else {
printf("%s%c%c", P_SYM(move->piece), printf("%s%c%c", P_SYM(move->piece),
FILE2C(GET_F(move->from)), FILE2C(F88(move->from)),
RANK2C(GET_R(move->from))); RANK2C(R88(move->from)));
if (move->taken) { if (move->taken) {
printf("x"); printf("x");
if (flags & M_PR_LONG) if (flags & M_PR_LONG)
@@ -92,8 +92,8 @@ int move_print(move_t *move, move_flags_t flags)
printf("-"); printf("-");
} }
printf("%c%c", printf("%c%c",
FILE2C(GET_F(move->to)), FILE2C(F88(move->to)),
RANK2C(GET_R(move->to))); RANK2C(R88(move->to)));
if (flags & M_PR_LONG && move->flags & M_EN_PASSANT) if (flags & M_PR_LONG && move->flags & M_EN_PASSANT)
printf("e.p."); printf("e.p.");
if (move->promotion) if (move->promotion)
@@ -143,10 +143,10 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(3, "piece_color=%d turn=%d from=%c%c to=%c%c\n", log_i(3, "piece_color=%d turn=%d from=%c%c to=%c%c\n",
COLOR(piece), pos->turn, COLOR(piece), pos->turn,
FILE2C(GET_F(from)), FILE2C(F88(from)),
RANK2C(GET_R(from)), RANK2C(R88(from)),
FILE2C(GET_F(to)), FILE2C(F88(to)),
RANK2C(GET_R(to))); RANK2C(R88(to)));
# endif # endif
if (COLOR(piece) != pos->turn) if (COLOR(piece) != pos->turn)
return NULL; return NULL;
@@ -155,7 +155,7 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
if (board[to].piece & KING) { if (board[to].piece & KING) {
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(2, "invalid position: opponent king [%c%c] is in check.\n", log_i(2, "invalid position: opponent king [%c%c] is in check.\n",
FILE2C(GET_F(to)), RANK2C(GET_R(to))); FILE2C(F88(to)), RANK2C(R88(to)));
# endif # endif
return NULL; return NULL;
} }
@@ -195,8 +195,8 @@ static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(3, "added move from %c%c to %c%c\n", log_i(3, "added move from %c%c to %c%c\n",
FILE2C(GET_F(move->from)), RANK2C(GET_R(move->from)), FILE2C(F88(move->from)), RANK2C(R88(move->from)),
FILE2C(GET_F(move->to)), RANK2C(GET_R(move->to))); FILE2C(F88(move->to)), RANK2C(R88(move->to)));
# endif # endif
return move; return move;
} }
@@ -207,8 +207,8 @@ void move_del(struct list_head *ptr)
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(3, "delete move from %c%c to %c%c\n", log_i(3, "delete move from %c%c to %c%c\n",
FILE2C(GET_F(move->from)), RANK2C(GET_R(move->from)), FILE2C(F88(move->from)), RANK2C(R88(move->from)),
FILE2C(GET_F(move->to)), RANK2C(GET_R(move->to))); FILE2C(F88(move->to)), RANK2C(R88(move->to)));
# endif # endif
/* TODO: remove move->pos if non null */ /* TODO: remove move->pos if non null */
@@ -248,7 +248,7 @@ static move_t *move_pawn_add(pos_t *pos, piece_t piece, square_t from,
if (color != pos->turn) if (color != pos->turn)
return NULL; return NULL;
if (RANK88(from) == rank7) { /* promotion */ if (R88(from) == rank7) { /* promotion */
for (promote = QUEEN; promote > PAWN; promote >>= 1) { for (promote = QUEEN; promote > PAWN; promote >>= 1) {
if ((move = move_add(pos, piece, from, to))) { if ((move = move_add(pos, piece, from, to))) {
move->flags |= M_PROMOTION; move->flags |= M_PROMOTION;
@@ -306,7 +306,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
P_NAME(piece), P_NAME(piece),
dir, dir,
square, square,
FILE2C(GET_F(square)), RANK2C(GET_R(square))); FILE2C(F88(square)), RANK2C(R88(square)));
# endif # endif
/* normal push. We do not test for valid destination square here, /* normal push. We do not test for valid destination square here,
@@ -322,7 +322,7 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
if (doit && (move = move_pawn_add(pos, piece | color, square, new, rank7))) if (doit && (move = move_pawn_add(pos, piece | color, square, new, rank7)))
count++; count++;
/* push 2 squares */ /* push 2 squares */
if (move && RANK88(square) == rank2) { if (move && R88(square) == rank2) {
new += dir * 16; new += dir * 16;
if (SQ88_OK(new) && !board[new].piece) { if (SQ88_OK(new) && !board[new].piece) {
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
@@ -339,16 +339,16 @@ int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
} }
/* en passant - not accounted for mobility. Correct ? */ /* en passant - not accounted for mobility. Correct ? */
if (doit && pos->en_passant && RANK88(square) == rank5) { if (doit && pos->en_passant && R88(square) == rank5) {
unsigned char ep_file = FILE88(pos->en_passant); unsigned char ep_file = F88(pos->en_passant);
unsigned char sq_file = FILE88(square); unsigned char sq_file = F88(square);
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(4, "possible en passant on rank %#x (current = %#0x)\n", ep_file, log_i(4, "possible en passant on rank %#x (current = %#0x)\n", ep_file,
sq_file); sq_file);
# endif # endif
if (sq_file == ep_file - 1 || sq_file == ep_file + 1) { if (sq_file == ep_file - 1 || sq_file == ep_file + 1) {
square_t t_square = SQUARE(ep_file, rank5); /* taken pawn square */ square_t t_square = SQ88(ep_file, rank5); /* taken pawn square */
piece_t taken = board[t_square].piece; piece_t taken = board[t_square].piece;
move = move_pawn_add(pos, piece | color , square, pos->en_passant, rank7); move = move_pawn_add(pos, piece | color , square, pos->en_passant, rank7);
count++; count++;
@@ -425,17 +425,17 @@ int pseudo_moves_castle(pos_t *pos)
printf("Cannot castle K side: controlled\n"); printf("Cannot castle K side: controlled\n");
goto next; goto next;
} }
move = move_add(pos, board[SQUARE(4, rank1)].piece, move = move_add(pos, board[SQ88(4, rank1)].piece,
SQUARE(4, rank1), SQUARE(6, rank1)); SQ88(4, rank1), SQ88(6, rank1));
if (move) { if (move) {
newpos = move->newpos; newpos = move->newpos;
move->flags |= M_CASTLE_K; move->flags |= M_CASTLE_K;
/* move King rook to column F */ /* move King rook to column F */
newpos->board[SQUARE(5, rank1)] = newpos->board[SQUARE(7, rank1)]; newpos->board[SQ88(5, rank1)] = newpos->board[SQ88(7, rank1)];
SET_F(newpos->board[SQUARE(5, rank1)].s_piece->square, 5); SETF88(newpos->board[SQ88(5, rank1)].s_piece->square, 5);
newpos->board[SQUARE(7, rank1)].piece = 0; newpos->board[SQ88(7, rank1)].piece = 0;
newpos->board[SQUARE(7, rank1)].s_piece = NULL; newpos->board[SQ88(7, rank1)].s_piece = NULL;
count++; count++;
} }
@@ -451,16 +451,16 @@ next:
printf("Cannot castle Q side: controlled\n"); printf("Cannot castle Q side: controlled\n");
goto end; goto end;
} }
move = move_add(pos, board[SQUARE(4, rank1)].piece, move = move_add(pos, board[SQ88(4, rank1)].piece,
SQUARE(4, rank1), SQUARE(2, rank1)); SQ88(4, rank1), SQ88(2, rank1));
if (move) { if (move) {
newpos = move->newpos; newpos = move->newpos;
move->flags |= M_CASTLE_Q; move->flags |= M_CASTLE_Q;
/* move King rook to column F */ /* move King rook to column F */
newpos->board[SQUARE(3, rank1)] = newpos->board[SQUARE(0, rank1)]; newpos->board[SQ88(3, rank1)] = newpos->board[SQ88(0, rank1)];
SET_F(newpos->board[SQUARE(3, rank1)].s_piece->square, 3); SETF88(newpos->board[SQ88(3, rank1)].s_piece->square, 3);
newpos->board[SQUARE(0, rank1)].piece = 0; newpos->board[SQ88(0, rank1)].piece = 0;
newpos->board[SQUARE(0, rank1)].s_piece = NULL; newpos->board[SQ88(0, rank1)].s_piece = NULL;
count++; count++;
} }
@@ -492,7 +492,7 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit)
IS_WHITE(color)? "white": "black", IS_WHITE(color)? "white": "black",
P_NAME(piece), P_NAME(piece),
square, square,
FILE2C(GET_F(square)), RANK2C(GET_R(square))); FILE2C(F88(square)), RANK2C(R88(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);
# endif # endif
@@ -509,7 +509,7 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit)
bb_new = SQ88_2_BB(new); bb_new = SQ88_2_BB(new);
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(2, "trying %#x=%c%c bb=%#lx\n", log_i(2, "trying %#x=%c%c bb=%#lx\n",
new, FILE2C(GET_F(new)), RANK2C(GET_R(new)), new, FILE2C(F88(new)), RANK2C(R88(new)),
bb_new); bb_new);
//bitboard_print(new_bitboard); //bitboard_print(new_bitboard);
# endif # endif
@@ -521,7 +521,7 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit)
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(2, "%s king cannot move to %c%c\n", log_i(2, "%s king cannot move to %c%c\n",
IS_WHITE(color)? "white": "black", IS_WHITE(color)? "white": "black",
FILE2C(GET_F(new)), RANK2C(GET_R(new))); FILE2C(F88(new)), RANK2C(R88(new)));
# endif # endif
break; break;
} }
@@ -531,7 +531,7 @@ int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit)
//bitboard_print(pos->occupied[OPPONENT(color)]); //bitboard_print(pos->occupied[OPPONENT(color)]);
# ifdef DEBUG_MOVE # ifdef DEBUG_MOVE
log_i(2, "BB: skipping %#llx [%c%c] (same color piece)\n", log_i(2, "BB: skipping %#llx [%c%c] (same color piece)\n",
new, FILE2C(GET_F(new)), RANK2C(GET_R(new))); new, FILE2C(F88(new)), RANK2C(R88(new)));
# endif # endif
break; break;
} }

View File

@@ -41,8 +41,8 @@ void piece_list_print(struct list_head *list)
piece = list_entry(p_cur, piece_list_t, list); piece = list_entry(p_cur, piece_list_t, list);
printf("%s%c%c ", P_SYM(piece->piece), printf("%s%c%c ", P_SYM(piece->piece),
FILE2C(GET_F(piece->square)), FILE2C(F88(piece->square)),
RANK2C(GET_R(piece->square))); RANK2C(R88(piece->square)));
} }
printf("\n"); printf("\n");
} }
@@ -68,7 +68,7 @@ piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square)
# ifdef DEBUG_PIECE # ifdef DEBUG_PIECE
log_f(3, "piece=%02x square=%02x\n", piece, square); log_f(3, "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",
P_NAME(piece), FILE2C(GET_F(square)), RANK2C(GET_R(square))); P_NAME(piece), FILE2C(F88(square)), RANK2C(R88(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

@@ -85,18 +85,19 @@ void pos_print(pos_t *pos)
printf(" A B C D E F G H\n\n"); printf(" A B C D E F G H\n\n");
printf("Turn: %s.\n", IS_WHITE(pos->turn) ? "white" : "black"); printf("Turn: %s.\n", IS_WHITE(pos->turn) ? "white" : "black");
printf("Kings: W:%c%c B:%c%c\n", printf("Kings: W:%c%c B:%c%c\n",
FILE2C(GET_F(pos->king[WHITE])), FILE2C(F88(pos->king[WHITE])),
RANK2C(GET_R(pos->king[WHITE])), RANK2C(R88(pos->king[WHITE])),
FILE2C(GET_F(pos->king[BLACK])), FILE2C(F88(pos->king[BLACK])),
RANK2C(GET_R(pos->king[BLACK]))); RANK2C(R88(pos->king[BLACK])));
printf("Possible en-passant: [%#x] ", pos->en_passant); printf("Possible en-passant: [%#x] ", pos->en_passant);
if (pos->en_passant == 0) if (pos->en_passant == 0)
printf("None.\n"); printf("None.\n");
else else
printf("%d %d = %c%c\n", GET_F(pos->en_passant), printf("%d %d = %c%c\n",
GET_R(pos->en_passant), F88(pos->en_passant),
FILE2C(GET_F(pos->en_passant)), R88(pos->en_passant),
RANK2C(GET_R(pos->en_passant))); FILE2C(F88(pos->en_passant)),
RANK2C(R88(pos->en_passant)));
printf("castle [%#x] : ", pos->castle); printf("castle [%#x] : ", pos->castle);