2 Commits

Author SHA1 Message Date
4f28b71333 UCI moves && games states list 2024-06-27 10:09:30 +02:00
46aed01079 hash: add hash_short macro, state_s: add prev and move 2024-06-27 08:36:08 +02:00
9 changed files with 120 additions and 40 deletions

View File

@@ -148,6 +148,9 @@ else ifeq ($(BUILD),perf)
CFLAGS += -g # symbols (gdb, perf, etc.) CFLAGS += -g # symbols (gdb, perf, etc.)
CFLAGS += -ginline-points # inlined funcs debug info CFLAGS += -ginline-points # inlined funcs debug info
CFLAGS += -funroll-loops CFLAGS += -funroll-loops
else ifeq ($(BUILD),debug)
CFLAGS += -O0
CFLAGS += -g # symbols (gdb, perf, etc.)
# for gprof # for gprof
#CFLAGS += -pg #CFLAGS += -pg
# Next one may be useful for valgrind (when invalid instructions) # Next one may be useful for valgrind (when invalid instructions)
@@ -207,7 +210,7 @@ $(sort all $(MAKECMDGOALS)):
else else
##################################### General targets ##################################### General targets
.PHONY: all release dev perf compile clean cleanall .PHONY: all release dev perf debug compile clean cleanall
all: testing $(TARGET) all: testing $(TARGET)
@@ -220,6 +223,9 @@ dev:
perf: perf:
$(MAKE) BUILD=perf clean all $(MAKE) BUILD=perf clean all
debug:
$(MAKE) BUILD=debug clean all
compile: brlib objs compile: brlib objs
libs: brlib libs: brlib

View File

@@ -26,6 +26,9 @@
#include "hash.h" #include "hash.h"
#include "fen.h" #include "fen.h"
#include "search.h" #include "search.h"
#include "hist.h"
#include "move-gen.h"
#include "move-do.h"
struct command { struct command {
char *name; /* User printable name */ char *name; /* User printable name */
@@ -252,6 +255,7 @@ int do_ucinewgame(__unused pos_t *pos, __unused char *arg)
{ {
pos_clear(pos); pos_clear(pos);
tt_clear(); tt_clear();
hist_init();
return 1; return 1;
} }
@@ -275,37 +279,74 @@ int do_position(pos_t *pos, char *arg)
{ {
char *saveptr, *token, *fen, *moves; char *saveptr, *token, *fen, *moves;
hist_init();
/* separate "moves" section */ /* separate "moves" section */
saveptr = NULL;
if ((moves = strstr(arg, "moves"))) { if ((moves = strstr(arg, "moves"))) {
*(moves - 1) = 0; *(moves - 1) = 0;
token = strtok_r(moves, " ", &saveptr); }
saveptr = NULL;
token = strtok_r(arg, " ", &saveptr);
if (!strcmp(token, "startpos")) {
startpos(pos);
do_diagram(pos, "");
} else if (!strcmp(token, "fen")) {
fen = strtok_r(NULL, " ", &saveptr);
fen2pos(pos, fen);
} else {
puts("fuck");
}
if (moves) {
saveptr = NULL;
moves = strtok_r(moves, " ", &saveptr);
moves = strtok_r(NULL, "", &saveptr); moves = strtok_r(NULL, "", &saveptr);
printf("moves = %s\n", moves); printf("moves = %s\n", moves);
do_moves(pos, moves); do_moves(pos, moves);
} }
saveptr = NULL;
token = strtok_r(arg, " ", &saveptr);
if (!strcmp(token, "startpos")) {
startpos(pos);
} else if (!strcmp(token, "fen")) {
fen = strtok_r(NULL, "", &saveptr);
fen2pos(pos, fen);
}
return 1; return 1;
} }
static move_t *move_find_move(move_t target, movelist_t *list)
{
move_t *move = list->move, *last = move + list->nmoves;
for (; move < last; ++move) {
if (move_from(target) == move_from(*move) &&
move_to(target) == move_to(*move) &&
move_to(target) == move_to(*move) &&
move_promoted(target) == move_promoted(*move))
return move;
}
return NULL;
}
int do_moves(__unused pos_t *pos, char *arg) int do_moves(__unused pos_t *pos, char *arg)
{ {
char *saveptr = NULL, *move; char *saveptr = NULL, *token, check[8];
move_t move, *foundmove;
state_t state;
movelist_t movelist;
saveptr = NULL; saveptr = NULL;
move = strtok_r(arg, " ", &saveptr); token = strtok_r(arg, " ", &saveptr);
while (move) { while (token) {
printf("move: [%s]\n", move); move = move_from_str(pos, token);
move = strtok_r(NULL, " ", &saveptr); move_to_str(check, move, 0);
printf("move: [%s] %s\n", token, check);
pos_set_checkers_pinners_blockers(pos);
pos_legal(pos, pos_gen_pseudo(pos, &movelist));
foundmove = move_find_move(move, &movelist);
if (!foundmove) {
printf("illegal move");
return 1;
}
move_do(pos, *foundmove, &state);
hist_push(&state, &move);
token = strtok_r(NULL, " ", &saveptr);
} }
hist_static_print();
return 1; return 1;
} }

View File

@@ -97,9 +97,10 @@ typedef u64 bitboard_t;
*/ */
//typedef s32 eval_t; //typedef s32 eval_t;
/* forward enum definition is impossible in C11, to simplify /* forward enum definition is impossible in C11.
* cross-dependancies, all important enum are moved here. * To simplify cross-dependancies, all important enum are moved here.
*/ */
typedef enum { typedef enum {
_SSQUARE_ = -1, /* force signed enum */ _SSQUARE_ = -1, /* force signed enum */
A1 = 0, B1, C1, D1, E1, F1, G1, H1, A1 = 0, B1, C1, D1, E1, F1, G1, H1,

View File

@@ -30,6 +30,11 @@
typedef u64 hkey_t; /* cannot use typedef for key_t */ typedef u64 hkey_t; /* cannot use typedef for key_t */
/**
* hash_short: return the value of a hash first 7 MSB.
*/
#define hash_short(hash) ((hash) >> (64 - 8))
/** /**
* hentry_t: hashtable bucket. * hentry_t: hashtable bucket.
* *

View File

@@ -80,7 +80,6 @@ pos_t *move_do(pos_t *pos, const move_t move, state_t *state)
if (captured != EMPTY) { if (captured != EMPTY) {
pos->clock_50 = 0; pos->clock_50 = 0;
//pos->captured = pos->board[to]; /* save capture info */
bug_on(pos->board[to] == EMPTY || COLOR(pos->captured) != them); bug_on(pos->board[to] == EMPTY || COLOR(pos->captured) != them);
key ^= zobrist_pieces[captured][to]; key ^= zobrist_pieces[captured][to];
pos_clr_sq(pos, to); /* clear square */ pos_clr_sq(pos, to); /* clear square */

View File

@@ -116,6 +116,37 @@ char *move_to_str(char *dst, const move_t move, __unused const int flags)
return dst; return dst;
} }
/**
* move_from_str() - create a move from a position and UCI move string
* @pos: &pos_t
* @str: uci move string
*
* string and corresponding move are considered valid (no check is done).
*
* @return move, or NULL if error.
*/
move_t move_from_str(const pos_t *pos, const char *str)
{
move_t move;
square_t from = sq_from_string(str);
square_t to = sq_from_string(str + 2);
piece_type_t piece = PIECE(pos->board[from]);
piece_type_t promoted = piece_t_from_char(*(str+5));
if (piece == KING && sq_dist(from, to) > 1) { /* castling */
move = move_make_flags(from, to, M_CASTLE);
} else if (piece == PAWN && /* en-passant */
sq_file(from) != sq_file(to) &&
pos->board[to] == EMPTY) {
move = move_make_enpassant(from, to);
} else if (promoted != NO_PIECE_TYPE) { /* promotion */
move = move_make_promote(from, to, promoted);
} else {
move = move_make(from, to);
}
return move;
}
/** /**
* moves_print() - print movelist moves. * moves_print() - print movelist moves.
* @moves: &movelist_t moves list * @moves: &movelist_t moves list

View File

@@ -29,11 +29,7 @@
* ppp 3 12 12-14 piece_type_t 070000 (>>12) &07 promoted * ppp 3 12 12-14 piece_type_t 070000 (>>12) &07 promoted
* FFF 3 15 15-17 move_flags_t 0700000 (>>15) &07 flags * FFF 3 15 15-17 move_flags_t 0700000 (>>15) &07 flags
*/ */
typedef s32 move_t; typedef u32 move_t;
/* special move_t values */
#define MOVE_NONE ((move_t) -1)
#define MOVE_NULL ((move_t) 0) /* hack: from = to = A1 */
enum { enum {
M_OFF_FROM = 0, M_OFF_FROM = 0,
@@ -50,6 +46,11 @@ typedef enum {
// M_CHECK = (3 << M_OFF_FLAGS) /* maybe unknown/useless ? */ // M_CHECK = (3 << M_OFF_FLAGS) /* maybe unknown/useless ? */
} move_flags_t; } move_flags_t;
/* special move_t values */
#define MOVE_NULL 0 /* hack: from = to = A1 */
#define MOVE_NONE 07777 /* hack: from = to = H8 */
#define MOVE_NO_MOVE 01010 /* hack: from = to = A2 */
#define move_set_flags(move, flags) ((move) | (flags)) #define move_set_flags(move, flags) ((move) | (flags))
#define move_flags(move) ((move) & M_FLAGS_MASK) #define move_flags(move) ((move) & M_FLAGS_MASK)
@@ -144,6 +145,7 @@ static inline move_t move_make_promote(square_t from, square_t to,
//int move_print(int movenum, move_t *move, move_flags_t flags); //int move_print(int movenum, move_t *move, move_flags_t flags);
char *move_to_str(char *dst, const move_t move, __unused const int flags); char *move_to_str(char *dst, const move_t move, __unused const int flags);
move_t move_from_str(const pos_t *pos, const char *str);
void moves_print(movelist_t *moves, int flags); void moves_print(movelist_t *moves, int flags);
void move_sort_by_sq(movelist_t *moves); void move_sort_by_sq(movelist_t *moves);

View File

@@ -30,6 +30,7 @@
#include "misc.h" #include "misc.h"
#include "board.h" #include "board.h"
#include "attack.h" #include "attack.h"
#include "hist.h"
/** /**
* pos_new() - allocate a new position * pos_new() - allocate a new position
@@ -123,8 +124,6 @@ pos_t *pos_clear(pos_t *pos)
pos->pinners = 0; pos->pinners = 0;
pos->blockers = 0; pos->blockers = 0;
pos->repeat.moves = 0;
return pos; return pos;
} }
@@ -167,10 +166,12 @@ bool pos_cmp(const pos_t *pos1, const pos_t *pos2)
if (_cmpf(checkers) ||_cmpf(pinners) || _cmpf(blockers)) if (_cmpf(checkers) ||_cmpf(pinners) || _cmpf(blockers))
goto end; goto end;
if (_cmpf(repeat.moves) || /*
memcmp(pos1->repeat.key, pos2->repeat.key, * if (_cmpf(repeat.moves) ||
pos1->repeat.moves * sizeof pos1->repeat.key)) * memcmp(pos1->repeat.key, pos2->repeat.key,
goto end; * pos1->repeat.moves * sizeof pos1->repeat.key))
* goto end;
*/
ret = true; ret = true;
end: end:
@@ -429,7 +430,7 @@ void pos_print(const pos_t *pos)
char str[128]; char str[128];
board_print(pos->board); board_print(pos->board);
printf("key:%lx ", pos->key); printf("key:%lx (#%lx)", pos->key, hash_short(pos->key));
printf("fen: %s\n", pos2fen(pos, str)); printf("fen: %s\n", pos2fen(pos, str));
printf("checkers:%s ", pos_checkers2str(pos, str, sizeof(str))); printf("checkers:%s ", pos_checkers2str(pos, str, sizeof(str)));
printf("pinners: %s ", pos_pinners2str(pos, str, sizeof(str))); printf("pinners: %s ", pos_pinners2str(pos, str, sizeof(str)));

View File

@@ -28,13 +28,6 @@
#include "move.h" #include "move.h"
#include "board.h" #include "board.h"
#define REPEAT_SIZE 1024
typedef struct {
hkey_t key[REPEAT_SIZE];
int moves;
} repeat_t;
typedef struct __pos_s { typedef struct __pos_s {
u64 node_count; /* evaluated nodes */ u64 node_count; /* evaluated nodes */
int turn; /* WHITE or BLACK */ int turn; /* WHITE or BLACK */
@@ -56,6 +49,8 @@ typedef struct __pos_s {
int clock_50; int clock_50;
int plycount; /* plies so far, start from 1 */ int plycount; /* plies so far, start from 1 */
piece_t captured; /* only used in move_undo */ piece_t captured; /* only used in move_undo */
move_t move;
struct state_s *prev;
); );
bitboard_t checkers; /* opponent checkers */ bitboard_t checkers; /* opponent checkers */
bitboard_t pinners; /* opponent pinners */ bitboard_t pinners; /* opponent pinners */
@@ -64,7 +59,6 @@ typedef struct __pos_s {
piece_t board[BOARDSIZE]; piece_t board[BOARDSIZE];
bitboard_t bb[2][PIECE_TYPE_MAX]; /* bb[0][PAWN], bb[1][ALL_PIECES] */ bitboard_t bb[2][PIECE_TYPE_MAX]; /* bb[0][PAWN], bb[1][ALL_PIECES] */
square_t king[2]; /* dup with bb, faster retrieval */ square_t king[2]; /* dup with bb, faster retrieval */
repeat_t repeat; /* for repetition detection */
} pos_t; } pos_t;
typedef struct state_s state_t; typedef struct state_s state_t;