2 Commits

12 changed files with 348 additions and 247 deletions

4
.gitignore vendored
View File

@@ -15,9 +15,9 @@ brchess
*.s
*.old
*.save
/test/
# /test/
/obj/
/lib/
/.deps/
/deps/
/notused/
valgrind.out

314
Makefile
View File

@@ -12,48 +12,50 @@
SHELL := /bin/bash
CC := gcc
LD := ld
BEAR := bear
TOUCH := touch
RM := rm
RMDIR := rmdir
CCLSROOT := .ccls-root
CCLSFILE := compile_commands.json
SRCDIR := ./src
INCDIR := ./include
LIBSRCDIR := ./libsrc
BINDIR := ./bin
OBJDIR := ./obj
LIBSRCDIR := ./libsrc
LIBOBJDIR := ./libobj
BINDIR := ./bin
LIBDIR := ./lib
LDFLAGS := -L$(LIBDIR)
DEPDIR := ./deps
SRC := $(wildcard $(SRCDIR)/*.c) # project sources
SRC_FN := $(notdir $(SRC)) # source basename
OBJ := $(addprefix $(OBJDIR)/,$(SRC_FN:.c=.o))
LIBSRC := $(wildcard $(LIBSRCDIR)/*.c) # lib sources
LIBOBJ := $(patsubst %.c,%.o,$(LIBSRC)) # and objects
LIBSRC_FN := $(notdir $(LIBSRC)) # lib sources basename
LIBOBJ := $(addprefix $(LIBOBJDIR)/,$(LIBSRC_FN:.c=.o)) # and lib obj ones
#LIBOBJ := $(patsubst %.c,%.o,$(LIBSRC))
LIB := br_$(shell uname -m) # library name
SLIB := $(addsuffix .a, $(LIBDIR)/lib$(LIB)) # static lib
DLIB := $(addsuffix .so, $(LIBDIR)/lib$(LIB)) # dynamic lib
BIN := fen piece move eval brchess
DEP_FN := $(SRC_FN) $(LIBSRC_FN)
DEP := $(addprefix $(DEPDIR)/,$(DEP_FN:.c=.d))
LIBSEXT := -lreadline -lncurses
LIBS := -l$(LIB) $(LIBSEXT)
#TARGETS := fen piece move eval brchess
TARGET_FN := brchess
TARGET := $(addprefix $(BINDIR)/,$(TARGET_FN))
CFLAGS := -std=gnu11
LDFLAGS := -L$(LIBDIR)
LIBS := -l$(LIB) -lreadline -lncurses
#CFLAGS += -O2
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -march=native
CFLAGS += -Wmissing-declarations
# for gprof
# CFLAGS += -pg
# Next one may be useful for valgrind (when invalid instructions)
# CFLAGS += -mno-tbm
##################################### DEBUG flags
##################################### pre-processor flags
CPPFLAGS := -I$(INCDIR)
CPPFLAGS += -DDEBUG # global
CPPFLAGS += -DDEBUG_DEBUG # enable log() functions
@@ -65,143 +67,211 @@ CPPFLAGS += -DDEBUG_EVAL # eval functions
CPPFLAGS += -DDEBUG_PIECE # piece list management
CPPFLAGS += -DDEBUG_SEARCH # move search
CPPFLAGS := $(strip $(CPPFLAGS))
##################################### compiler flags
CFLAGS := -std=gnu11
#CFLAGS += -O2
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -march=native
CFLAGS += -Wmissing-declarations
# for gprof
# CFLAGS += -pg
# Next one may be useful for valgrind (when invalid instructions)
# CFLAGS += -mno-tbm
CFLAGS := $(strip $(CFLAGS))
##################################### archiver/linker/dependency flags
ARFLAGS := rcs
LDFLAGS := -L$(LIBDIR)
DEPFLAGS := -MMD -MP -MF $(DEPDIR)/$*.d
##################################### General targets
.PHONY: compile cflags all clean cleanall
.PHONY: all clean cleanall
compile: objects bin
all: $(TARGET)
cflags:
@echo CFLAGS: "$(CFLAGS)"
@echo CPPFLAGS: $(CPPFLAGS)
@echo DEPFLAGS: $(DEPFLAGS)
@echo LDFLAGS: $(LDFLAGS)
compile: libobjs objs
all: clean compile
clean: cleandep cleanobj cleanlib cleanlibobj cleanbin
clean: cleanobj cleanbin
cleanall: clean cleandepdir cleanobjdir cleanlibdir cleanlibobjdir cleanbindir
cleanall: clean cleandeps cleanlib
##################################### cleaning functions
# rmfiles - deletes a list of files in a directory if they exist.
# $(1): the directory
# $(2): the list of files to delete
# $(3): 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
# list and actual files in directory.
# See rmdir below.
define rmfiles
@#echo "rmfiles=+$(1)+"
$(eval $@_EXIST = $(wildcard $(1)))
@#echo "existfile=+${$@_EXIST}+"
@if [[ -n "${$@_EXIST}" ]]; then \
echo "cleaning $(2) files." ; \
$(RM) ${$@_EXIST} ; \
fi
endef
# rmdir - deletes a directory if it exists.
# $(1): the directory
# $(2): The string to include in action output - "removing X dir."
#
# Don't use $(RM) -rf, to control unexpected dep files.
# See rmfile above.
define rmdir
@#echo "rmdir +$(1)+"
$(eval $@_EXIST = $(wildcard $(1)))
@#echo "existdir=+${$@_EXIST}+"
@if [[ -n "${$@_EXIST}" ]]; then \
echo "removing $(2) dir." ; \
$(RMDIR) ${$@_EXIST} ; \
fi
endef
##################################### dirs creation
.PHONY: alldirs
ALLDIRS := $(DEPDIR) $(OBJDIR) $(LIBDIR) $(LIBOBJDIR) $(BINDIR)
alldirs: $(ALLDIRS)
# Here, we have something like:
# a: a
# a will be built if older than a, or does not exist.
$(ALLDIRS): $@
@echo creating $@ directory.
@mkdir -p $@
##################################### Dependencies files
.PHONY: deps cleandeps
.PHONY: cleandeps cleandepsdir
DEPDIR := ./.deps
DEPFILES := $(addprefix $(DEPDIR)/,$(SRC_FN:.c=.d))
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.d
-include $(wildcard $(DEP))
$(DEPFILES):
# Don't use $(DEPDIR)/*.d, to control mismatches between dep and src files.
# See second rule below.
cleandep:
$(call rmfiles,$(DEP),depend)
@#echo cleaning dependency files.
@#$(RM) -f $(DEP)
include $(wildcard $(DEPFILES))
cleandepdir:
$(call rmdir,$(DEPDIR),depend)
@#[ -d $(DEPDIR) ] && echo cleaning depend files && $(RM) -f $(DEP) || true
$(DEPDIR):
@echo creating $@ directory.
@mkdir -p $@
##################################### brchess objects
.PHONY: objs cleanobj cleanobjdir
cleandeps:
$(RM) -rf $(DEPDIR)
##################################### objects
.SECONDEXPANSION:
OBJ = $(addprefix $(OBJDIR)/,$(SRC_FN:.c=.o))
.PHONY: cleanobj
objects: $(OBJ)
objs: $(OBJ)
cleanobj:
$(RM) -rf $(OBJDIR)
$(call rmfiles,$(OBJ),object)
$(OBJDIR):
@echo creating $@ directory.
@mkdir -p $@
cleanobjdir: cleanobj
$(call rmdir,$(OBJDIR),objects)
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(DEPDIR)/%.d | $(OBJDIR) $(DEPDIR)
# The part right of '|' are "order-only prerequisites": They are build as
# "normal" ones, but do not imply to rebuild target.
$(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) $(DEPDIR)
@echo compiling brchess $< "->" $@.
@$(CC) -c $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< -o $@
##################################### brlib objects
.PHONY: libobjs cleanlibobj cleanlibobjdir
libobjs: $(LIBOBJ)
cleanlibobj:
$(call rmfiles,$(LIBOBJ),brlib object)
cleanlibobjdir:
$(call rmdir,$(LIBOBJDIR),brlib objects)
$(LIBOBJDIR)/%.o: $(LIBSRCDIR)/%.c | $(LIBOBJDIR) $(DEPDIR)
@echo compiling $<.
@$(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEPFLAGS) -I $(INCDIR) -o $@ $<
@$(CC) -c $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< -o $@
##################################### pre-processed (.i) and assembler (.s) output
%.i: %.c
@echo generating $@
@$(CC) -E $(CFLAGS) -I $(INCDIR) $< -o $@
%.s: %.c
@echo generating $@
@$(CC) -S -fverbose-asm $(CFLAGS) -I $(INCDIR) $< -o $@
##################################### br library
.PHONY: cleanlib libs
ARFLAGS = r
##################################### libraries
.PHONY: libs cleanlib cleanlibdir
cleanlib:
$(RM) -rf $(LIBDIR) $(LIBOBJ)
$(call rmfiles,$(DLIB) $(SLIB),library)
cleanlibdir:
$(call rmdir,$(LIBDIR),libraries)
libs: $(DLIB) $(SLIB)
$(LIBDIR):
@echo creating $@ directory.
@mkdir -p $@
# remove default rule
%.o: %.c
$(LIBSRCDIR)/%.o: $(LIBSRCDIR)/%.c
$(LIBOBJDIR)/%.o: $(LIBSRCDIR)/%.c | $(LIBOBJDIR) $(DEPDIR)
@echo compiling library $< "->" $@.
@$(CC) -c $(CPPFLAGS) $(CFLAGS) -I $(INCDIR) -o $@ $<
$(SLIB): $(LIBOBJ) | $(LIBDIR)
@echo building $@ static library.
@$(AR) $(ARFLAGS) -o $@ $^
@$(CC) -c $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS) $< -o $@
$(DLIB): CFLAGS += -fPIC
$(DLIB): LDFLAGS += -shared
$(DLIB): $(LIBOBJ) | $(LIBDIR)
@echo building $@ shared library.
@$(CC) $(LDFLAGS) $^ -o $@
@$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
##################################### binaries
.PHONY: bin prebin cleanbin
$(SLIB): $(LIBOBJ) | $(LIBDIR)
@echo building $@ static library.
$(AR) $(ARFLAGS) $@ $^
BINMARK := .bindone
##################################### brchess binaries
.PHONY: targets cleanbin cleanbindir
bin: $(BIN) postbin
postbin:
@[[ ! -f $(BINMARK) ]] || echo done.
@$(RM) -f $(BINMARK)
targets: $(TARGET)
cleanbin:
$(RM) -f $(BIN) core
$(call rmfiles,$(TARGET),binary)
# TODO: find a better dependancy graph
$(BIN): $(SRCDIR)/$$@.c libs $$(subst $(OBJDIR)/$$@.o,,$(OBJ))
@[[ -f $(BINMARK) ]] || echo -n "generating binaries: "
@echo -n "$@... "
@$(CC) -DBIN_$@ $(CPPFLAGS) $(CFLAGS) $(subst libs,,$^) $(LDFLAGS) $(LIBS) -o $@
@$(TOUCH) $(BINMARK)
cleanbindir:
$(call rmdir,$(BINDIR),binaries)
##################################### ccls
$(TARGET): $(DLIB) $(OBJ) | $(BINDIR)
@echo generating $@ executable.
@$(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@
##################################### pre-processed (.i) and assembler (.s) output
%.i: %.c
@echo generating $@
@$(CC) -E $(CPPFLAGS) $(CFLAGS) $< -o $@
%.s: %.c
@echo generating $@
@$(CC) -S -fverbose-asm $(CPPFLAGS) $(CFLAGS) $< -o $@
##################################### LSP (ccls)
.PHONY: ccls
ccls: $(CCLSFILE)
# generate compile_commands.json
$(CCLSFILE): brchess Makefile
$(BEAR) -- make clean bin
##################################### LSP (ccls)
.PHONY: bear
ROOTDIR = .ccls-root
$(CCLSROOT):
@echo creating root marker file.
@echo creating project root file.
@$(TOUCH) $@
bear: clean $(CCLSROOT)
@touch .ccls-root
# generate compile_commands.json.
# Need to add includes and 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 cleanlibobj $(SRC) $(LIBSRC) | $(CCLSROOT)
@echo "Generating ccls compile commands file ($@)."
@$(BEAR) -- make compile
#.PHONY: bear
#bear: cleanobj cleanlibobj Makefile | $(CCLSROOT)
# @$(BEAR) -- make compile
##################################### valgrind (mem check)
.PHONY: memcheck
@@ -214,5 +284,29 @@ VALGRINDFLAGS += --log-file=valgrind.out
# https://stackoverflow.com/questions/72840015
VALGRINDFLAGS += --suppressions=etc/libreadline.supp
memcheck: bin
@$(VALGRIND) $(VALGRINDFLAGS) ./brchess
memcheck: targets
@$(VALGRIND) $(VALGRINDFLAGS) $(BINDIR)/brchess
##################################### Makefile debug
.PHONY: showflags wft
showflags:
@echo CFLAGS: "$(CFLAGS)"
@echo CPPFLAGS: $(CPPFLAGS)
@echo DEPFLAGS: $(DEPFLAGS)
@echo LDFLAGS: $(LDFLAGS)
@echo DEPFLAGS: $(DEPFLAGS)
wtf:
@printf "LIBOBJDIR=%s\n\n" "$(LIBOBJDIR)"
@printf "LIBOBJ=%s\n\n" "$(LIBOBJ)"
@printf "OBJDIR=%s\n\n" "$(OBJDIR)"
@printf "OBJ=%s\n\n" "$(OBJ)"
@#echo LIBOBJ=$(LIBOBJ)
@#echo DEPS=$(DEPS)
@#echo LIBSRC=$(LIBSRC)
@#echo LIBOBJ=$(LIBOBJ)
@#echo DEPS=$(DEPS)
@#echo LIBSRC=$(LIBSRC)

View File

@@ -478,7 +478,6 @@ int do_pvs(pos_t *pos, __unused char *arg)
return 1;
}
#ifdef BIN_brchess
/** main()
* options:
int brchess(pos_t *pos)
@@ -521,4 +520,3 @@ int main(int ac, char **av)
return brchess(pos);
}
#endif /* BIN_brchess */

View File

@@ -93,37 +93,3 @@ eval_t eval(pos_t *pos)
pos->eval = res;
return res;
}
#ifdef BIN_eval
#include "fen.h"
#include "move.h"
int main(int ac, char**av)
{
pos_t *pos;
eval_t res;
debug_init(5, stderr, true);
piece_pool_init();
moves_pool_init();
pos_pool_init();
pos = pos_get();
if (ac == 1) {
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
pos_print(pos);
pos_pieces_print(pos);
moves_gen_all(pos);
pos_print(pos);
moves_print(pos, M_PR_SEPARATE);
res = eval(pos);
printf("eval=%d centipawns)\n", res);
}
#endif

View File

@@ -173,21 +173,3 @@ pos_t *fen2pos(pos_t *pos, char *fen)
# endif
return pos;
}
#ifdef BIN_fen
int main(int ac, char**av)
{
pos_t *pos;
debug_init(5, stderr, true);
piece_pool_init();
pos_pool_init();
pos = pos_get();
if (ac == 1) {
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
pos_print(pos);
}
#endif

View File

@@ -882,32 +882,3 @@ void move_undo(pos_t *pos, __unused move_t *move)
{
pos_del(pos);
}
#ifdef BIN_move
#include "fen.h"
int main(int ac, char**av)
{
pos_t *pos;
debug_init(5, stderr, true);
piece_pool_init();
moves_pool_init();
pos_pool_init();
pos = pos_get();
if (ac == 1) {
fen2pos(pos, "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2");
//pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
//printf("turn = %d opponent = %d\n", pos->turn, OPPONENT(pos->turn));
moves_gen_all(pos);
pos_print(pos);
pos_pieces_print(pos);
moves_print(pos, M_PR_SEPARATE);
//bitboard_print2(castle_squares[0].controlled, castle_squares[1].controlled);
//bitboard_print2(castle_squares[0].occupied, castle_squares[1].occupied);
}
#endif /* BIN_move */

View File

@@ -116,54 +116,3 @@ int pieces_del(pos_t *pos, short color)
# endif
return count;
}
#ifdef BIN_piece
#include "fen.h"
int main(int ac, char**av)
{
pos_t *pos;
printf("zobi\n");fflush(stdout);
debug_init(6, stderr, true);
log_f(5, "kfsjdhg\n");
pos_pool_init();
pos = pos_get();
piece_pool_init();
if (ac == 1) {
printf("zoba\n");fflush(stdout);
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
pos_print(pos);
pos_pieces_print(pos);
printf("0x1c = 11100 = C1-E1:\n");
bitboard_print(0x1c);
printf("0x70 = 111 = A1-C1\n");
bitboard_print(0x70);
printf("0x0e = 1110 = B1-D1\n");
bitboard_print(0x0e);
printf("0x60 = 1100000 = F1-G1\n");
bitboard_print(0x60);
printf("A1:\n");
bitboard_print(A1);
printf("1:\n");
bitboard_print(1L);
printf("H1:\n");
bitboard_print(H1);
printf("C1:\n");
bitboard_print(C1);
printf("D1:\n");
bitboard_print(D1);
printf("C1|D1:\n");
bitboard_print(C1|D1);
printf("H8:\n");
bitboard_print(H8);
}
#endif

View File

@@ -14,7 +14,7 @@
#include <br.h>
#include <list.h>
#include <debug.h>
#include "debug.h"
#include "move.h"
#include "eval.h"

35
test/eval.c Normal file
View File

@@ -0,0 +1,35 @@
#include "debug.h"
#include "../src/position.h"
#include "../src/eval.h"
#include "../src/fen.h"
#include "../src/move.h"
int main(int ac, char**av)
{
pos_t *pos;
eval_t res;
debug_init(5, stderr, true);
piece_pool_init();
moves_pool_init();
pos_pool_init();
pos = pos_get();
if (ac == 1) {
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
pos_print(pos);
pos_pieces_print(pos);
moves_gen_all(pos);
pos_print(pos);
moves_print(pos, M_PR_SEPARATE);
res = eval(pos);
printf("eval=%d centipawns)\n", res);
}

20
test/fen.c Normal file
View File

@@ -0,0 +1,20 @@
#include "debug.h"
#include "pool.h"
#include "../src/position.h"
#include "../src/fen.h"
int main(int ac, char**av)
{
pos_t *pos;
debug_init(5, stderr, true);
piece_pool_init();
pos_pool_init();
pos = pos_get();
if (ac == 1) {
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
pos_print(pos);
}

31
test/move.c Normal file
View File

@@ -0,0 +1,31 @@
#include <stdio.h>
#include "debug.h"
#include "../src/fen.h"
#include "../src/move.h"
int main(int ac, char**av)
{
pos_t *pos;
debug_init(5, stderr, true);
piece_pool_init();
moves_pool_init();
pos_pool_init();
pos = pos_get();
if (ac == 1) {
fen2pos(pos, "rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2");
//pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
//printf("turn = %d opponent = %d\n", pos->turn, OPPONENT(pos->turn));
moves_gen_all(pos);
pos_print(pos);
pos_pieces_print(pos);
moves_print(pos, M_PR_SEPARATE);
//bitboard_print2(castle_squares[0].controlled, castle_squares[1].controlled);
//bitboard_print2(castle_squares[0].occupied, castle_squares[1].occupied);
}

55
test/piece.c Normal file
View File

@@ -0,0 +1,55 @@
#include <stdio.h>
#include "debug.h"
#include "../src/fen.h"
#include "../src/position.h"
#include "../src/bitboard.h"
int main(int ac, char**av)
{
pos_t *pos;
printf("zobi\n");fflush(stdout);
debug_init(6, stderr, true);
log_f(5, "kfsjdhg\n");
pos_pool_init();
pos = pos_get();
piece_pool_init();
if (ac == 1) {
printf("zoba\n");fflush(stdout);
pos_startpos(pos);
} else {
fen2pos(pos, av[1]);
}
pos_print(pos);
pos_pieces_print(pos);
printf("0x1c = 11100 = C1-E1:\n");
bitboard_print(0x1c);
printf("0x70 = 111 = A1-C1\n");
bitboard_print(0x70);
printf("0x0e = 1110 = B1-D1\n");
bitboard_print(0x0e);
printf("0x60 = 1100000 = F1-G1\n");
bitboard_print(0x60);
printf("A1:\n");
bitboard_print(A1);
printf("1:\n");
bitboard_print(1L);
printf("H1:\n");
bitboard_print(H1);
printf("C1:\n");
bitboard_print(C1);
printf("D1:\n");
bitboard_print(D1);
printf("C1|D1:\n");
bitboard_print(C1|D1);
printf("H8:\n");
bitboard_print(H8);
}