3 Commits

Author SHA1 Message Date
9c2e76442d add a struct_group for move_do() irreversible changes 2024-03-17 17:34:21 +01:00
49302c7a60 lowercase move flags macros 2024-03-17 17:33:48 +01:00
db38b507ff add gmon.out 2024-03-17 17:30:51 +01:00
7 changed files with 53 additions and 36 deletions

1
.gitignore vendored
View File

@@ -17,4 +17,5 @@ vgcore.*
/tmp/
/notused/
valgrind.out
gmon.out
compile_commands.json

View File

@@ -79,7 +79,7 @@ bool pseudo_is_legal(const pos_t *pos, const move_t move)
* 5th relative rank. To do so, we create an occupation bb without
* the 2 pawns.
*/
if (IS_CAPTURE(move) && PIECE(pos->board[to]) == EMPTY) {
if (is_capture(move) && PIECE(pos->board[to]) == EMPTY) {
/* from rank bitboard */
bitboard_t rank5 = bb_sqrank[from];
/* enemy rooks/queens on from rank */

View File

@@ -109,7 +109,7 @@ char *move_str(char *dst, const move_t move, __unused const int flags)
int len;
sprintf(dst, "%s%s%n", sq_to_string(from), sq_to_string(to), &len);
if (IS_PROMOTION(move)) {
if (is_promotion(move)) {
piece_t promoted = move_promoted(move);
sprintf(dst + len, "%s", piece_to_low(promoted));
}

View File

@@ -23,14 +23,14 @@
* 1 0 5 3 8 7 5 4 2 1 6 5 0
* S UUUUUUU FFFFFF ccc ppp tttttt ffffff
*
* bits len off range type mask get desc
* ffffff 6 0 0-5 square_t 077 &63 from
* tttttt 6 6 6-11 square_t 07700 (>>6)&63 to
* ppp 3 12 12-14 piece_type_t 070000 (>>12)&7 promoted
* ccc 3 15 15-17 piece_type_t 0700000 (>>15)&7 captured
* FFFFFF 6 18 18-23 move_flags_t 077000000 (>>18)&63 N/A flags
* UUUUUUU 7 24 24-30 unused 017700000000 future usage
* S 1 31 31-31 - 020000000000 sign
* bits len off range type mask get desc
* ffffff 6 0 0-5 square_t 077 &077 from
* tttttt 6 6 6-11 square_t 07700 (>>6) &077 to
* ppp 3 12 12-14 piece_type_t 070000 (>>12) &07 promoted
* ccc 3 15 15-17 piece_type_t 0700000 (>>15) &07 captured
* FFFFFF 6 18 18-23 move_flags_t 077000000 (>>18) &077 N/A flags
* UUUUUUU 7 24 24-30 unused 017700000000 future usage
* S 1 31 31-31 - 020000000000 sign
*/
typedef s32 move_t;
@@ -47,8 +47,6 @@ enum {
};
typedef enum {
//M_INVALID = -1,
//M_START = 18,
M_CAPTURE = mask(0),
M_ENPASSANT = mask(1),
M_PROMOTION = mask(2),
@@ -57,21 +55,12 @@ typedef enum {
M_CHECK = mask(6) /* maybe unknown/useless ? */
} move_flags_t;
#define MOVE_FLAGS(m) ((m) >> M_OFF_FLAGS)
#define IS_CAPTURE(m) (MOVE_FLAGS(m) & M_CAPTURE)
#define IS_ENPASSANT(m) (MOVE_FLAGS(m) & M_ENPASSANT)
#define IS_PROMOTION(m) (MOVE_FLAGS(m) & M_PROMOTION)
#define IS_CASTLE(m) (MOVE_FLAGS(m) & (M_CASTLE_K | M_CASTLE_Q))
#define IS_CASTLE_K(m) (MOVE_FLAGS(m) & M_CASTLE_K)
#define IS_CASTLE_Q(m) (MOVE_FLAGS(m) & M_CASTLE_Q)
#define IS_CHECK(m) (MOVE_FLAGS(m) & M_CHECK)
#define MOVES_MAX 256
typedef struct __movelist_s {
move_t move[MOVES_MAX];
int nmoves; /* total moves (fill) */
int curmove; /* current move (use) */
//int curmove; /* current move (use) */
} movelist_t;
static inline square_t move_from(move_t move)
@@ -94,6 +83,19 @@ static inline piece_type_t move_captured(move_t move)
return (move >> M_OFF_CAPT) & 07;
}
static inline move_flags_t move_flags(move_t move)
{
return (move >> M_OFF_FLAGS) & 077;
}
#define is_capture(m) (move_flags(m) & M_CAPTURE)
#define is_enpassant(m) (move_flags(m) & M_ENPASSANT)
#define is_promotion(m) (move_flags(m) & M_PROMOTION)
#define is_castle(m) (move_flags(m) & (M_CASTLE_K | M_CASTLE_Q))
#define is_castle_K(m) (move_flags(m) & M_CASTLE_K)
#define is_castle_Q(m) (move_flags(m) & M_CASTLE_Q)
#define is_check(m) (move_flags(m) & M_CHECK)
static inline move_t move_make(square_t from, square_t to)
{
return (to << M_OFF_TO) | from;

View File

@@ -95,10 +95,14 @@ pos_t *pos_clear(pos_t *pos)
# endif
pos->node_count = 0;
pos->turn = WHITE;
pos->clock_50 = 0;
pos->plycount = 0;
pos->en_passant = SQUARE_NONE;
pos->castle = 0;
pos->clock_50 = 0;
pos->plycount = 0;
for (square_t sq = A1; sq <= H8; ++sq)
pos->board[sq] = EMPTY;
for (color_t color = WHITE; color <= BLACK; ++color) {
for (piece_type_t piece = 0; piece <= KING; ++piece)
@@ -107,12 +111,12 @@ pos_t *pos_clear(pos_t *pos)
pos->king[color] = SQUARE_NONE;
}
for (square_t sq = A1; sq <= H8; ++sq)
pos->board[sq] = EMPTY;
pos->moves.curmove = 0;
pos->moves.nmoves = 0;
pos->checkers = 0;
pos->pinners = 0;
pos->blockers = 0;
//pos->moves.curmove = 0;
pos->moves.nmoves = 0;
return pos;
}

View File

@@ -18,6 +18,7 @@
#include "brlib.h"
#include "bitops.h"
#include "struct-group.h"
#include "chessdefs.h"
#include "bitboard.h"
@@ -28,19 +29,28 @@
typedef struct __pos_s {
u64 node_count; /* evaluated nodes */
int turn; /* WHITE or BLACK */
u16 clock_50;
u16 plycount; /* plies so far, start is 0 */
square_t king[2]; /* dup with bb, faster retrieval */
square_t en_passant;
castle_rights_t castle;
/* data which cannot be recovered by move_undo
* following data can be accessed either directly, either via "movesave"
* structure name.
* For example, pos->en_passant and pos->movesave.en_passant are the same.
* This allows a memcpy on this data (to save/restore position state).
*/
struct_group_tagged(movesave, movesave,
square_t en_passant;
castle_rights_t castle;
u16 clock_50;
u16 plycount; /* plies so far, start is 0 */
);
piece_t board[BOARDSIZE];
bitboard_t bb[2][PIECE_TYPE_MAX]; /* bb[0][PAWN], bb[1][ALL_PIECES] */
bitboard_t controlled[2]; /* unsure */
square_t king[2]; /* dup with bb, faster retrieval */
bitboard_t checkers; /* opponent checkers */
bitboard_t pinners; /* opponent pinners */
bitboard_t blockers; /* pieces blocking pin */
piece_t board[BOARDSIZE];
movelist_t moves;
} pos_t;

View File

@@ -263,8 +263,8 @@ int main(int __unused ac, __unused char**av)
printf("M: ");
moves_print(&legal, 0);
} else {
printf("[%s]\n\tMoves (OK): ", fen);
moves_print(&fishpos->moves, 0);
printf("[%s]: OK (%d Moves)\n", fen, legal.nmoves);
//moves_print(&fishpos->moves, 0);
}
//compare_moves(&fishpos->moves, &legal);
//} else {