Compare commits
3 Commits
ccc0dfd2f6
...
f1a081a7b6
Author | SHA1 | Date | |
---|---|---|---|
f1a081a7b6 | |||
ca4e274957 | |||
e50d9a73e6 |
85
Makefile
85
Makefile
@@ -20,10 +20,11 @@ RMDIR := rmdir
|
||||
MAKE := make
|
||||
|
||||
SRCDIR := ./src
|
||||
INCDIR := ./src
|
||||
INCDIR := ./src # used by ./test sources
|
||||
OBJDIR := ./obj
|
||||
BINDIR := ./bin
|
||||
DEPDIR := ./dep
|
||||
TSTDIR := ./test
|
||||
|
||||
BRLIB := ./brlib
|
||||
BRINCDIR := $(BRLIB)/include
|
||||
@@ -32,11 +33,13 @@ BRLIBDIR := $(BRLIB)/lib
|
||||
CCLSROOT := .ccls-root
|
||||
CCLSFILE := compile_commands.json
|
||||
|
||||
SRC := $(wildcard $(SRCDIR)/*.c) # project sources
|
||||
SRC_FN := $(notdir $(SRC)) # source basename
|
||||
SRC := $(wildcard $(SRCDIR)/*.c) # project sources
|
||||
SRC_FN := $(notdir $(SRC)) # source basename
|
||||
OBJ := $(addprefix $(OBJDIR)/,$(SRC_FN:.c=.o))
|
||||
|
||||
LIB := br_$(shell uname -m) # library name
|
||||
TSTSRC := $(wildcard $(TSTDIR)/*.c)
|
||||
|
||||
LIB := br_$(shell uname -m) # library name
|
||||
|
||||
DEP_FN := $(SRC_FN) $(LIBSRC_FN)
|
||||
DEP := $(addprefix $(DEPDIR)/,$(DEP_FN:.c=.d))
|
||||
@@ -47,21 +50,26 @@ TARGET := $(addprefix $(BINDIR)/,$(TARGET_FN))
|
||||
LDFLAGS := -L$(BRLIBDIR)
|
||||
LIBS := $(strip -l$(LIB) -lreadline)
|
||||
|
||||
ASMFILES := $(SRC:.c=.s) $(TSTSRC:.c=.s)
|
||||
CPPFILES := $(SRC:.c=.i) $(TSTSRC:.c=.i)
|
||||
|
||||
##################################### pre-processor flags
|
||||
CPPFLAGS := -I$(BRINCDIR) -I$(INCDIR)
|
||||
CPPFLAGS += -DBUG_ON
|
||||
CPPFLAGS += -DWARN_ON
|
||||
CPPFLAGS += -DNDEBUG
|
||||
|
||||
#CPPFLAGS += -DDEBUG # global
|
||||
CPPFLAGS += -DDEBUG_DEBUG # enable log() functions
|
||||
#CPPFLAGS += -DDEBUG_DEBUG_C # enable verbose log() settings
|
||||
CPPFLAGS += -DDEBUG_POOL # memory pools management
|
||||
#CPPFLAGS += -DDEBUG_FEN # FEN decoding
|
||||
CPPFLAGS += -DDEBUG_MOVE # move generation
|
||||
CPPFLAGS += -DDEBUG_EVAL # eval functions
|
||||
CPPFLAGS += -DDEBUG_PIECE # piece list management
|
||||
CPPFLAGS += -DDEBUG_SEARCH # move search
|
||||
CPPFLAGS += -DNDEBUG # assert
|
||||
|
||||
CPPFLAGS += -DBUG_ON # bug.h
|
||||
CPPFLAGS += -DWARN_ON # bug.h
|
||||
#CPPFLAGS += -DDEBUG # global - unused
|
||||
|
||||
CPPFLAGS += -DDEBUG_DEBUG # enable log() functions
|
||||
#CPPFLAGS += -DDEBUG_DEBUG_C # enable log() settings
|
||||
CPPFLAGS += -DDEBUG_POOL # memory pools management
|
||||
#CPPFLAGS += -DDEBUG_FEN # FEN decoding
|
||||
CPPFLAGS += -DDEBUG_MOVE # move generation
|
||||
CPPFLAGS += -DDEBUG_EVAL # eval functions
|
||||
CPPFLAGS += -DDEBUG_PIECE # piece list management
|
||||
CPPFLAGS += -DDEBUG_SEARCH # move search
|
||||
|
||||
# remove extraneous spaces (due to spaces before comments)
|
||||
CPPFLAGS := $(strip $(CPPFLAGS))
|
||||
@@ -94,15 +102,14 @@ compile: brlib objs
|
||||
|
||||
libs: brlib
|
||||
|
||||
clean: cleandep cleanobj cleanbin
|
||||
clean: cleandep cleanasmcpp cleanobj cleanbin
|
||||
|
||||
cleanall: clean cleandepdir cleanobjdir cleanallbrlib cleanbindir
|
||||
|
||||
##################################### cleaning functions
|
||||
# rmfiles - deletes a list of files in a directory if they exist.
|
||||
# rmfiles - delete a list of files if they exist.
|
||||
# $(1): the directory
|
||||
# $(2): the list of files to delete
|
||||
# $(3): The string to include in action output - "cleaning X files."
|
||||
# $(2): The string to include in action output - "cleaning X files."
|
||||
# see: https://stackoverflow.com/questions/6783243/functions-in-makefiles
|
||||
#
|
||||
# Don't use wildcard like "$(DIR)/*.o", so we can control mismatches between
|
||||
@@ -118,7 +125,7 @@ define rmfiles
|
||||
fi
|
||||
endef
|
||||
|
||||
# rmdir - deletes a directory if it exists.
|
||||
# rmdir - delete a directory if it exists.
|
||||
# $(1): the directory
|
||||
# $(2): The string to include in action output - "removing X dir."
|
||||
#
|
||||
@@ -182,16 +189,15 @@ $(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) $(DEPDIR)
|
||||
@$(CC) -c $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||
|
||||
##################################### brlib libraries
|
||||
.PHONY: cleanbrlib cleanallbrlib libs brlib
|
||||
.PHONY: cleanbrlib cleanallbrlib brlib
|
||||
|
||||
cleanbrlib:
|
||||
$(MAKE) -C $(BRLIB) clean
|
||||
@#$(call rmfiles,$(DLIB) $(SLIB),library)
|
||||
|
||||
cleanallbrlib:
|
||||
$(MAKE) -C $(BRLIB) cleanall
|
||||
|
||||
brlib: libs
|
||||
brlib:
|
||||
$(MAKE) -C $(BRLIB) libs
|
||||
|
||||
##################################### brchess binaries
|
||||
@@ -205,13 +211,16 @@ cleanbin:
|
||||
cleanbindir:
|
||||
$(call rmdir,$(BINDIR),binaries)
|
||||
|
||||
# We don't use static lib, but we could build it here
|
||||
#$(TARGET): $(DLIB) $(OBJ) | $(BINDIR) $(SLIB)
|
||||
$(TARGET): libs $(OBJ) | $(BINDIR) $(SLIB)
|
||||
$(TARGET): libs $(OBJ) | $(BINDIR)
|
||||
@echo generating $@ executable.
|
||||
$(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@
|
||||
|
||||
##################################### pre-processed (.i) and assembler (.s) output
|
||||
.PHONY: cleanasmcpp
|
||||
|
||||
cleanasmcpp:
|
||||
@$(call rmfiles,$(ASMFILES) $(CPPFILES),asm and pre-processed)
|
||||
|
||||
%.i: %.c
|
||||
@echo generating $@
|
||||
@$(CC) -E $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||
@@ -230,17 +239,13 @@ $(CCLSROOT):
|
||||
@$(TOUCH) $@
|
||||
|
||||
# generate compile_commands.json.
|
||||
# Need to add includes and Makefile dependencies.
|
||||
# TODO: add Makefile dependencies.
|
||||
# also, if cclsfile is newer than sources, no need to clean objects file
|
||||
# (and to run bear).
|
||||
# maybe run cleanobj cleanlibobj in commands ?
|
||||
$(CCLSFILE): cleanobj cleanbrlib $(SRC) $(LIBSRC) | $(CCLSROOT)
|
||||
@echo "Generating ccls compile commands file ($@)."
|
||||
@$(BEAR) -- $(MAKE) compile
|
||||
|
||||
#.PHONY: bear
|
||||
#bear: cleanobj cleanlibobj Makefile | $(CCLSROOT)
|
||||
# @$(BEAR) -- make compile
|
||||
@$(BEAR) -- $(MAKE) compile testing
|
||||
|
||||
##################################### valgrind (mem check)
|
||||
.PHONY: memcheck
|
||||
@@ -258,19 +263,21 @@ memcheck: targets
|
||||
@$(VALGRIND) $(VALGRINDFLAGS) $(BINDIR)/brchess
|
||||
|
||||
##################################### test binaries
|
||||
TEST = bin/fen-test bin/bitboard-test
|
||||
TEST = bin/fen-test bin/bitboard-test
|
||||
|
||||
FENTESTOBJS = obj/fen.o obj/position.o obj/piece.o obj/util.o obj/bitboard.o
|
||||
BITBOARDOBJS = obj/position.o obj/piece.o obj/bitboard.o obj/fen.o \
|
||||
obj/hyperbola-quintessence.o
|
||||
FENTESTOBJS = obj/fen.o obj/position.o obj/piece.o obj/bitboard.o \
|
||||
obj/board.o obj/util.o
|
||||
BITBOARDOBJS = obj/fen.o obj/position.o obj/piece.o obj/bitboard.o \
|
||||
obj/board.o obj/hyperbola-quintessence.o
|
||||
|
||||
testing: $(TEST)
|
||||
|
||||
bin/fen-test: test/fen-test.c $(FENTESTOBJS)
|
||||
$(CC) $(CPPFLAGS) $(CFLAGS) $< $(FENTESTOBJS) $(LDFLAGS) $(LIBS) -o $@
|
||||
$(CC) $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(FENTESTOBJS) $(LDFLAGS) $(LIBS) -o $@
|
||||
|
||||
bin/bitboard-test: test/bitboard-test.c $(BITBOARDOBJS)
|
||||
$(CC) $(CPPFLAGS) $(CFLAGS) $< $(BITBOARDOBJS) $(LDFLAGS) $(LIBS) -o $@
|
||||
echo all=$^
|
||||
$(CC) $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(BITBOARDOBJS) $(LDFLAGS) $(LIBS) -o $@
|
||||
|
||||
|
||||
##################################### Makefile debug
|
||||
|
2
brlib
2
brlib
Submodule brlib updated: f9aec10459...9162db31e3
113
src/bitboard.c
113
src/bitboard.c
@@ -34,17 +34,56 @@ static int king_vector[8] = {
|
||||
SOUTH, SOUTH_WEST, WEST, NORTH_WEST
|
||||
};
|
||||
|
||||
bitboard_t bb_sq[SQUARE_MAX];
|
||||
bitboard_t bb_rank[64], bb_file[64], bb_diagonal[64], bb_antidiagonal[64];
|
||||
bitboard_t bb_sq[64];
|
||||
bitboard_t bb_rank[64], bb_file[64], bb_diag[64], bb_anti[64];
|
||||
bitboard_t bb_between_excl[64][64];
|
||||
bitboard_t bb_between[64][64];
|
||||
|
||||
bitboard_t bb_knight[64], bb_king[64];
|
||||
bitboard_t bb_pawn_push[2][64], bb_bpawn_attack[2][64], bb_pawn_ep[2][64];
|
||||
|
||||
/**
|
||||
* bitboard_between_excl() - get bitboard of squares between two squares.
|
||||
* @sq1, @sq2: The two square_t squares
|
||||
*
|
||||
* From: http://www.talkchess.com/forum3/viewtopic.php?f=7&t=12499&start=14
|
||||
* This function may be used instead of bb_XXX arrays if cache pressure is high.
|
||||
*
|
||||
* @Return: bitboard_t, squares between @sq1 and @sq2 (excl. @sq1 and @sq2).
|
||||
*/
|
||||
bitboard_t bitboard_between_excl(square_t sq1, square_t sq2)
|
||||
{
|
||||
const bitboard_t m1 = -1;
|
||||
const bitboard_t a2a7 = C64(0x0001010101010100);
|
||||
const bitboard_t b7h1 = C64(0x0002040810204080);
|
||||
bitboard_t btwn_bits, ray_bits;
|
||||
u32 rank_diff, file_diff, anti_diff, diag_diff;
|
||||
|
||||
btwn_bits = (m1 << sq1) ^ (m1 << sq2); /* includes sq1 and sq2 */
|
||||
rank_diff = ((sq2 | 7) - sq1) >> 3, /* signed */
|
||||
file_diff = (sq2 & 7) - (sq1 & 7); /* signed */
|
||||
|
||||
anti_diff = rank_diff + file_diff;
|
||||
rank_diff = rank_diff & 15;
|
||||
file_diff = file_diff & 15;
|
||||
anti_diff = anti_diff & 15;
|
||||
diag_diff = rank_diff ^ file_diff;
|
||||
ray_bits = 2 * ((rank_diff - 1) >> 26);
|
||||
|
||||
ray_bits |= bswap64((m1 + diag_diff) & b7h1);
|
||||
ray_bits |= (m1 + anti_diff) & b7h1;
|
||||
ray_bits |= (m1 + file_diff) & a2a7;
|
||||
ray_bits *= btwn_bits & -btwn_bits;
|
||||
return ray_bits & btwn_bits;
|
||||
}
|
||||
|
||||
/**
|
||||
* bitboard_init() - initialize general bitboards
|
||||
*
|
||||
* Generate the following bitboards :
|
||||
* bb_sq[64]: square to bitboard
|
||||
* bb_between_excl[64][64]: strict squares between two squares
|
||||
* bb_between[64][64]: squares between two squares including second square
|
||||
* bb_rank[64]: square to rank
|
||||
* bb_file[64]: square to file
|
||||
* bb_diagonal[64]: square to diagonal
|
||||
@@ -53,13 +92,12 @@ bitboard_t bb_pawn_push[2][64], bb_bpawn_attack[2][64], bb_pawn_ep[2][64];
|
||||
* And the following pseudo move masks:
|
||||
* bb_knight[64]: knight moves
|
||||
* bb_king[64]: king moves
|
||||
* bb_pawn[2][64]: white pawn moves (not attacks)
|
||||
* bb_pawn_att[2][64]: white pawn attacks
|
||||
*
|
||||
*/
|
||||
void bitboard_init(void)
|
||||
{
|
||||
/* for each square, the 4 masks: file, rank, diagonal, antidiagonal */
|
||||
struct { int df, dr; } dirs[4] = {
|
||||
struct { int df, dr; } vecs[4] = {
|
||||
{ 0, 1 }, /* vertical/file */
|
||||
{ 1, 0 }, /* horizontal/rank */
|
||||
{ 1, 1 }, /* diagonal */
|
||||
@@ -67,20 +105,31 @@ void bitboard_init(void)
|
||||
} ;
|
||||
bitboard_t tmpbb[64][4] = { 0 };
|
||||
|
||||
/* 1) square to bitboard */
|
||||
for (square_t sq = A1; sq <= H8; ++sq)
|
||||
bb_sq[sq] = mask(sq);
|
||||
/* 1) square to bitboard, and in-between-sq2-excluded */
|
||||
for (square_t sq1 = A1; sq1 <= H8; ++sq1) {
|
||||
bb_sq[sq1] = mask(sq1);
|
||||
for (square_t sq2 = A1; sq2 <= H8; ++sq2)
|
||||
bb_between_excl[sq1][sq2] = bitboard_between_excl(sq1, sq2);
|
||||
}
|
||||
|
||||
/* 2) square to rank/file/diagonal/antidiagonal */
|
||||
/* 2) sq1-to-sq2 mask, sq2 included
|
||||
* square to file/rank/dia/anti bitmaps
|
||||
*/
|
||||
for (square_t sq = 0; sq < 64; ++sq) {
|
||||
int r = sq_rank(sq), f = sq_file(sq);
|
||||
for (int mult = -7; mult < 8; ++mult) {
|
||||
for (int dir = 0; dir < 4; ++dir) {
|
||||
int dst_f = f + mult * dirs[dir].df;
|
||||
int dst_r = r + mult * dirs[dir].dr;
|
||||
if (sq_coord_ok(dst_f) && sq_coord_ok(dst_r)) {
|
||||
int dst = sq_make(dst_f, dst_r);
|
||||
tmpbb[sq][dir] |= mask(dst);
|
||||
file_t f = sq_file(sq);
|
||||
rank_t r = sq_rank(sq);
|
||||
for (int vec = 0; vec < 4; ++vec) {
|
||||
tmpbb[sq][vec] |= mask(sq_make(f, r));
|
||||
for (int dir = -1; dir <= 1; dir += 2) {
|
||||
file_t df = dir * vecs[vec].df, f2 = f + df;
|
||||
rank_t dr = dir * vecs[vec].dr, r2 = r + dr;
|
||||
bitboard_t mask_between = 0;
|
||||
while (sq_coord_ok(f2) && sq_coord_ok(r2)) {
|
||||
square_t dest = sq_make(f2, r2);
|
||||
tmpbb[sq][vec] |= mask(dest);
|
||||
mask_between |= mask(dest);
|
||||
bb_between[sq][dest] = mask_between;
|
||||
f2 += df, r2 += dr;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,12 +137,34 @@ void bitboard_init(void)
|
||||
for (square_t sq = 0; sq < 64; ++sq) {
|
||||
bb_file[sq] = tmpbb[sq][0];
|
||||
bb_rank[sq] = tmpbb[sq][1];
|
||||
bb_diagonal[sq] = tmpbb[sq][2];
|
||||
bb_antidiagonal[sq] = tmpbb[sq][3];
|
||||
bb_diag[sq] = tmpbb[sq][2];
|
||||
bb_anti[sq] = tmpbb[sq][3];
|
||||
}
|
||||
|
||||
/*
|
||||
* for (int i = 0; i < 64; ++i) {
|
||||
* for (int j = 0; j < 64; ++j) {
|
||||
* if (bb_between[i][j] != bb_between_excl[i][j]) {
|
||||
* bitboard_t diff = bb_between_excl[i][j] ^ bb_between[i][j];
|
||||
* int k = popcount64(bb_between[i][j]) -
|
||||
* popcount64(bb_between_excl[i][j]);
|
||||
* printf("%s-%s diff=%d excl=%s ",
|
||||
* sq_string(i), sq_string(j),
|
||||
* k,
|
||||
* sq_string(ctz64(diff)));
|
||||
* if (k == 1 && ctz64(diff) == j)
|
||||
* printf("OK\n");
|
||||
* else
|
||||
* printf("NOK\n");
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
|
||||
/* 3) knight and king moves */
|
||||
for (square_t sq = A1; sq <= H8; ++sq) {
|
||||
//rank_t r1 = sq_rank(sq);
|
||||
//file_t f1 = sq_file(sq);
|
||||
for (int vec = 0; vec < 8; ++vec) {
|
||||
int dst = sq + knight_vector[vec];
|
||||
if (sq_ok(dst)) {
|
||||
@@ -147,14 +218,14 @@ void bitboard_print(const char *title, const bitboard_t bitboard)
|
||||
* @n: number of bitboards
|
||||
* @bb_ptr...: pointers to bitboards
|
||||
*
|
||||
* @n is the number of bitboards to print. If @n -s > 8, it is reduced to 8;
|
||||
* @n is the number of bitboards to print. If @n > 10, it is reduced to 10
|
||||
*/
|
||||
void bitboard_print_multi(const char *title, int n, ...)
|
||||
{
|
||||
bitboard_t bb[8];
|
||||
va_list ap;
|
||||
|
||||
n = min(n, 8);
|
||||
n = min(n, 10);
|
||||
|
||||
va_start(ap, n);
|
||||
for (int i = 0; i < n; ++i) { /* save all bitboards */
|
||||
|
@@ -19,18 +19,26 @@
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "board.h"
|
||||
#include "piece.h"
|
||||
|
||||
typedef u64 bitboard_t;
|
||||
|
||||
/* mapping square -> bitboard */
|
||||
extern bitboard_t bb_sq[64];
|
||||
/* squares between sq1 and sq2, exclusing both */
|
||||
extern bitboard_t bb_between_excl[64][64];
|
||||
/* squares between sq1 and sq2, including sq2 */
|
||||
extern bitboard_t bb_between[64][64];
|
||||
|
||||
/* mapping square -> rank/file/diagonal/antidiagonal */
|
||||
extern bitboard_t bb_rank[64], bb_file[64], bb_diagonal[64], bb_antidiagonal[64];
|
||||
/* bb_rank[64]: square to rank
|
||||
* bb_file[64]: square to file
|
||||
* bb_diag[64]: square to diagonal
|
||||
* bb_anti[64]: square to antidiagonal
|
||||
*/
|
||||
extern bitboard_t bb_rank[64], bb_file[64], bb_diag[64], bb_anti[64];
|
||||
/* knight and king moves */
|
||||
extern bitboard_t bb_knight[64], bb_king[64];
|
||||
|
||||
#define mask(i) ( 1ULL << (i) )
|
||||
|
||||
enum {
|
||||
FILE_Abb = 0x0101010101010101ULL,
|
||||
FILE_Bbb = 0x0202020202020202ULL,
|
||||
@@ -94,6 +102,12 @@ static __always_inline bitboard_t shift_nw(const bitboard_t bb)
|
||||
return (bb & ~FILE_Abb) << NORTH_WEST;
|
||||
}
|
||||
|
||||
/* pawn moves/attacks */
|
||||
#define pawn_push(bb, c) ((c) == WHITE ? shift_n(bb): shift_s(bb))
|
||||
#define pawn_take_left(bb, c) ((c) == WHITE ? shift_nw(bb): shift_se(bb))
|
||||
#define pawn_take_right(bb, c) ((c) == WHITE ? shift_ne(bb): shift_sw(bb))
|
||||
|
||||
extern bitboard_t bitboard_between_excl(square_t sq1, square_t sq2);
|
||||
extern void bitboard_init(void);
|
||||
|
||||
extern bitboard_t bb_knight_moves(bitboard_t occ, square_t sq);
|
||||
|
12
src/board.c
12
src/board.c
@@ -14,7 +14,7 @@
|
||||
#include "brlib.h"
|
||||
#include "board.h"
|
||||
|
||||
char *sq_strings[] = {
|
||||
static const char *sq_strings[] = {
|
||||
"a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1",
|
||||
"a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
|
||||
"a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
|
||||
@@ -25,7 +25,13 @@ char *sq_strings[] = {
|
||||
"a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8",
|
||||
};
|
||||
|
||||
const char *sq_string(const int sq)
|
||||
/**
|
||||
* sq_string() - return a square string
|
||||
* @square: square (0-64)
|
||||
*
|
||||
* @Return: Pointer to @square string representation ("a1"-"h8").
|
||||
*/
|
||||
const char *sq_string(const square_t square)
|
||||
{
|
||||
return sq_strings[sq];
|
||||
return sq_strings[square];
|
||||
}
|
||||
|
@@ -16,9 +16,9 @@
|
||||
|
||||
#include "brlib.h" /* brlib types */
|
||||
|
||||
#define ONE 1ull
|
||||
#define C64(const_u64) const_u64##ULL
|
||||
#define U64(const_s64) const_s64##LL
|
||||
|
||||
#define mask(i) ( ONE << (i) )
|
||||
//typedef ushort board;
|
||||
|
||||
#define BOARDSIZE (8*8)
|
||||
|
@@ -44,7 +44,7 @@ uchar bb_rank_attacks[64 * 8];
|
||||
* 2) shift left result 2 more bits, as bit 0 is unused and already cleared:
|
||||
* (O <<= 2)
|
||||
*
|
||||
* TODO ? create masks excluding slider (eg. bb_diagonal ^ bb_sq[square]),
|
||||
* TODO ? create masks excluding slider (eg. bb_diag ^ bb_sq[square]),
|
||||
* to save one operation in hyperbola_moves().
|
||||
* TODO ? replace rank attack with this idea, mapping rank to diagonal ?
|
||||
* See http://timcooijmans.blogspot.com/2014/04/
|
||||
@@ -122,19 +122,19 @@ static bitboard_t hyperbola_file_moves(bitboard_t occ, square_t sq)
|
||||
return hyperbola_moves(occ, bb_file[sq], sq);
|
||||
}
|
||||
|
||||
static bitboard_t hyperbola_diagonal_moves(bitboard_t occ, square_t sq)
|
||||
static bitboard_t hyperbola_diag_moves(bitboard_t occ, square_t sq)
|
||||
{
|
||||
return hyperbola_moves(occ, bb_diagonal[sq], sq);
|
||||
return hyperbola_moves(occ, bb_diag[sq], sq);
|
||||
}
|
||||
|
||||
static bitboard_t hyperbola_antidiagonal_moves(bitboard_t occ, square_t sq)
|
||||
static bitboard_t hyperbola_anti_moves(bitboard_t occ, square_t sq)
|
||||
{
|
||||
return hyperbola_moves(occ, bb_antidiagonal[sq], sq);
|
||||
return hyperbola_moves(occ, bb_anti[sq], sq);
|
||||
}
|
||||
|
||||
bitboard_t hyperbola_bishop_moves(bitboard_t occ, square_t sq)
|
||||
{
|
||||
return hyperbola_diagonal_moves(occ, sq) + hyperbola_antidiagonal_moves(occ, sq);
|
||||
return hyperbola_diag_moves(occ, sq) + hyperbola_anti_moves(occ, sq);
|
||||
}
|
||||
|
||||
bitboard_t hyperbola_rook_moves(bitboard_t occ, square_t sq)
|
||||
|
19
src/move.h
19
src/move.h
@@ -36,13 +36,13 @@
|
||||
|
||||
/* move flags */
|
||||
#define M_FLAGS_BEG 18
|
||||
#define M_HAS_FLAGS mask(_M_FLAGS_BEG + 0) /* probably unused */
|
||||
#define M_CAPTURE mask(_M_FLAGS_BEG + 1)
|
||||
#define M_ENPASSANT mask(_M_FLAGS_BEG + 2)
|
||||
#define M_PROMOTION mask(_M_FLAGS_BEG + 3)
|
||||
#define M_CASTLE_K mask(_M_FLAGS_BEG + 4)
|
||||
#define M_CASTLE_Q mask(_M_FLAGS_BEG + 5)
|
||||
#define M_CHECK mask(_M_FLAGS_BEG + 6) /* probably unknown/useless */
|
||||
#define M_HAS_FLAGS mask(M_FLAGS_BEG + 0) /* probably unused */
|
||||
#define M_CAPTURE mask(M_FLAGS_BEG + 1)
|
||||
#define M_ENPASSANT mask(M_FLAGS_BEG + 2)
|
||||
#define M_PROMOTION mask(M_FLAGS_BEG + 3)
|
||||
#define M_CASTLE_K mask(M_FLAGS_BEG + 4)
|
||||
#define M_CASTLE_Q mask(M_FLAGS_BEG + 5)
|
||||
#define M_CHECK mask(M_FLAGS_BEG + 6) /* probably unknown/useless */
|
||||
|
||||
#define M_FLAGS (M_CAPTURE | M_ENPASSANT | M_PROMOTION | \
|
||||
M_CASTLE_K | M_CASTLE_Q | M_CHECK)
|
||||
@@ -55,6 +55,11 @@ static inline move_t move_make(square_t from, square_t to)
|
||||
return (to << 3) | from;
|
||||
}
|
||||
|
||||
static inline move_t move_make_promote(square_t from, square_t to, piece_type_t piece)
|
||||
{
|
||||
return move_make(from, to) | M_ENPASSANT | (piece << 15);
|
||||
}
|
||||
|
||||
static inline move_t move_from(move_t move)
|
||||
{
|
||||
return move & 56;
|
||||
|
113
src/movegen.c
113
src/movegen.c
@@ -18,9 +18,16 @@
|
||||
#include "piece.h"
|
||||
#include "move.h"
|
||||
|
||||
//void add_pseudo_move(move_t *pmove, square_t from, square_t to)
|
||||
//{
|
||||
// pmov
|
||||
/**
|
||||
* gen_pawn_push() - generate pawn push
|
||||
* @pos: position
|
||||
*
|
||||
* Generate all pseudo pawn pushes.
|
||||
*/
|
||||
//int gen_pawn_push(pos_t *pos, bitboard_t occ)
|
||||
//{
|
||||
|
||||
|
||||
//}
|
||||
|
||||
/**
|
||||
@@ -32,29 +39,32 @@
|
||||
*/
|
||||
int gen_all_pseudomoves(pos_t *pos)
|
||||
{
|
||||
int me = pos->turn, other = OPPONENT(me);
|
||||
bitboard_t my_pieces = pos->bb[me][ALL_PIECES], notmine = ~my_pieces,
|
||||
other_pieces = pos->bb[other][ALL_PIECES],
|
||||
occ = my_pieces | other_pieces, movebits;
|
||||
color_t me = pos->turn, enemy = OPPONENT(me);
|
||||
bitboard_t my_pieces = pos->bb[me][ALL_PIECES], not_my_pieces = ~my_pieces,
|
||||
enemy_pieces = pos->bb[enemy][ALL_PIECES];
|
||||
bitboard_t occ = my_pieces | enemy_pieces, empty = ~occ;
|
||||
bitboard_t movebits, from_pawns;
|
||||
|
||||
int tmp1, tmp2, from, to;
|
||||
movelist_t moves = pos->moves;
|
||||
|
||||
/* sliding pieces */
|
||||
bit_for_each64(from, tmp1, pos->bb[me][BISHOP]) {
|
||||
movebits = hyperbola_bishop_moves(occ, from) & notmine;
|
||||
movebits = hyperbola_bishop_moves(occ, from) & not_my_pieces;
|
||||
bit_for_each64(to, tmp2, movebits) {
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
}
|
||||
|
||||
}
|
||||
bit_for_each64(from, tmp1, pos->bb[me][ROOK]) {
|
||||
movebits = hyperbola_rook_moves(occ, from) & notmine;
|
||||
movebits = hyperbola_rook_moves(occ, from) & not_my_pieces;
|
||||
bit_for_each64(to, tmp2, movebits) {
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
}
|
||||
}
|
||||
/* TODO: remove this one */
|
||||
bit_for_each64(from, tmp1, pos->bb[me][QUEEN]) {
|
||||
movebits = hyperbola_queen_moves(occ, from) & notmine;
|
||||
movebits = hyperbola_queen_moves(occ, from) & not_my_pieces;
|
||||
bit_for_each64(to, tmp2, movebits) {
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
}
|
||||
@@ -62,24 +72,93 @@ int gen_all_pseudomoves(pos_t *pos)
|
||||
|
||||
/* knight */
|
||||
bit_for_each64(from, tmp1, pos->bb[me][KNIGHT]) {
|
||||
movebits = bb_knight_moves(occ, from) & notmine;
|
||||
movebits = bb_knight_moves(occ, from) & not_my_pieces;
|
||||
bit_for_each64(to, tmp2, movebits) {
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
/* king */
|
||||
movebits = bb_king_moves(occ, pos->king[me]) & notmine;
|
||||
movebits = bb_king_moves(occ, pos->king[me]) & not_my_pieces;
|
||||
bit_for_each64(to, tmp2, movebits) {
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
}
|
||||
|
||||
/* pawn: relative rank and files */
|
||||
bitboard_t rel_rank7 = (me == WHITE ? RANK_7bb : RANK_2bb);
|
||||
bitboard_t rel_rank3 = (me == WHITE ? RANK_3bb : RANK_6bb);
|
||||
bitboard_t rel_filea = (me == WHITE ? FILE_Abb : FILE_Hbb);
|
||||
bitboard_t rel_fileh = (me == WHITE ? FILE_Hbb : FILE_Abb);
|
||||
int en_passant = pos->en_passant == SQUARE_NONE? 0: pos->en_passant;
|
||||
bitboard_t enemy_avail = bb_sq[en_passant] | enemy_pieces;
|
||||
|
||||
/* pawn: ranks 2-6 push 1 and 2 squares */
|
||||
movebits = pawn_push(pos->bb[me][PAWN] & ~rel_rank7, me) & empty;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push(to, enemy); /* reverse push */
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
}
|
||||
movebits = pawn_push(movebits & rel_rank3, me) & empty;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push(pawn_push(to, enemy), enemy);
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
}
|
||||
/* pawn: rank 7 push */
|
||||
movebits = pawn_push(pos->bb[me][PAWN] & rel_rank7, me) & empty;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_push(to, enemy); /* reverse push */
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, QUEEN);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, ROOK);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, BISHOP);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, KNIGHT);
|
||||
}
|
||||
|
||||
/* pawn: ranks 2-6 captures left, including en-passant */
|
||||
from_pawns = pos->bb[me][PAWN] & ~rel_rank7 & ~rel_filea;
|
||||
movebits = pawn_take_left(from_pawns, me) & enemy_avail;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_take_left(to, enemy); /* reverse capture */
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
}
|
||||
/* pawn: rank 7 captures left */
|
||||
from_pawns = pos->bb[me][PAWN] & rel_rank7 & ~rel_filea;
|
||||
movebits = pawn_take_left(from_pawns, me) & enemy_avail;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_take_left(to, enemy); /* reverse capture */
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, QUEEN);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, ROOK);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, BISHOP);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, KNIGHT);
|
||||
}
|
||||
|
||||
/* pawn: ranks 2-6 captures right, including en-passant */
|
||||
from_pawns = pos->bb[me][PAWN] & ~rel_rank7 & ~rel_fileh;
|
||||
movebits = pawn_take_right(from_pawns, me) & enemy_avail;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_take_right(to, enemy);
|
||||
moves.move[moves.nmoves++] = move_make(from, to);
|
||||
}
|
||||
/* pawn: rank 7 captures right */
|
||||
from_pawns = pos->bb[me][PAWN] & rel_rank7 & ~rel_fileh;
|
||||
movebits = pawn_take_right(from_pawns, me) & enemy_pieces;
|
||||
bit_for_each64(to, tmp1, movebits) {
|
||||
from = pawn_take_right(to, enemy); /* reverse capture */
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, QUEEN);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, ROOK);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, BISHOP);
|
||||
moves.move[moves.nmoves++] = move_make_promote(from, to, KNIGHT);
|
||||
}
|
||||
|
||||
/* TODO
|
||||
* pawn ranks 2-6 advance (1 push, + 2 squares for rank 2)
|
||||
* pawns ranks 2-6 capture
|
||||
* pawns rank 7 advance + promotion
|
||||
* pawns rank 7 capture + promotion
|
||||
* pawns en-passant
|
||||
* DONE. pawn ranks 2-6 advance (1 push, + 2 squares for rank 2)
|
||||
* DONE. pawns rank 7 advance + promotions
|
||||
* DONE. pawns ranks 2-6 captures, left and right
|
||||
* DONE. pawns en-passant (with capture)
|
||||
* DONE. pawns rank 7 capture + promotion
|
||||
* castle
|
||||
*
|
||||
* add function per piece, and type, for easier debug
|
||||
*
|
||||
*/
|
||||
return moves.nmoves;
|
||||
}
|
||||
|
@@ -4,10 +4,11 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../src/position.h"
|
||||
#include "../src/piece.h"
|
||||
#include "../src/bitboard.h"
|
||||
#include "../src/hyperbola-quintessence.h"
|
||||
#include "bug.h"
|
||||
#include "position.h"
|
||||
#include "piece.h"
|
||||
#include "bitboard.h"
|
||||
#include "hyperbola-quintessence.h"
|
||||
|
||||
int main(int __unused ac, __unused char**av)
|
||||
{
|
||||
@@ -21,11 +22,25 @@ int main(int __unused ac, __unused char**av)
|
||||
);
|
||||
bitboard_print_multi(str, 7,
|
||||
bb_file[i] | bb_rank[i] |
|
||||
bb_diagonal[i] | bb_antidiagonal[i],
|
||||
bb_diagonal[i], bb_antidiagonal[i],
|
||||
bb_diag[i] | bb_anti[i],
|
||||
bb_diag[i], bb_anti[i],
|
||||
bb_file[i], bb_rank[i],
|
||||
bb_knight[i], bb_king[i]);
|
||||
}
|
||||
bitboard_print_multi("a1-a8 a1-h8 a1-h1 a2-a7 a2-g7 a2-g2", 6,
|
||||
bb_between[A1][A8], bb_between[A1][H8],
|
||||
bb_between[A1][H1], bb_between[A2][A7],
|
||||
bb_between[A2][G7], bb_between[A2][G2]);
|
||||
bitboard_print_multi("c3-c6 c3-f6 c3-f3 c3-e1 c3-c1 c3-a1 c3-a3 c3-a5", 8,
|
||||
bb_between[C3][C6], bb_between[C3][F6],
|
||||
bb_between[C3][F3], bb_between[C3][E1],
|
||||
bb_between[C3][C1], bb_between[C3][A1],
|
||||
bb_between[C3][A3], bb_between[C3][A5]);
|
||||
bitboard_print_multi("c4-c6 c4-f6 c4-f3 c4-e1 c4-c1 c4-a1 c4-a3 c4-a5", 8,
|
||||
bb_between[C4][C6], bb_between[C4][F6],
|
||||
bb_between[C4][F3], bb_between[C4][E1],
|
||||
bb_between[C4][C1], bb_between[C4][A1],
|
||||
bb_between[C4][A3], bb_between[C4][A5]);
|
||||
/*
|
||||
* for (square_t sq = 0; sq < 64; ++sq) {
|
||||
* sprintf(str, "%2d %#lx %#lx knight", sq, bb_sq[sq], bb_knight[sq]);
|
||||
|
@@ -1,10 +1,11 @@
|
||||
#include "debug.h"
|
||||
#include "pool.h"
|
||||
//#include <stdio.h>
|
||||
|
||||
#include "../src/chessdefs.h"
|
||||
#include "../src/bitboard.h"
|
||||
#include "../src/position.h"
|
||||
#include "../src/fen.h"
|
||||
#include "bug.h"
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "bitboard.h"
|
||||
#include "position.h"
|
||||
#include "fen.h"
|
||||
|
||||
int main(int ac, char**av)
|
||||
{
|
||||
@@ -13,7 +14,6 @@ int main(int ac, char**av)
|
||||
const char *fen;
|
||||
char revfen[128];
|
||||
int comp;
|
||||
|
||||
//debug_init(5, stderr, true);
|
||||
//pos_pool_init();
|
||||
bitboard_init();
|
||||
|
Reference in New Issue
Block a user