From d81dca6e23b4fcd36c69d570b154386fe93e334b Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Mon, 11 Mar 2024 16:04:45 +0100 Subject: [PATCH] add sq_line array, bb_sq_aligned3, renamed sq_manh to sq_taxi --- Makefile | 8 ++++---- src/bitboard.c | 21 ++++++++++++++++++- src/bitboard.h | 32 +++++++++++++++++++---------- src/board.h | 20 ++++++++++++++++--- test/attack-test.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+), 19 deletions(-) create mode 100644 test/attack-test.c diff --git a/Makefile b/Makefile index 184960d..8eff347 100644 --- a/Makefile +++ b/Makefile @@ -205,7 +205,7 @@ cleanobjdir: cleanobj # "normal" ones, but do not imply to rebuild target. $(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR) $(DEPDIR) @echo compiling brchess module: $< "->" $@. - $(CC) -c $(ALL_CFLAGS) $< -o $@ + @$(CC) -c $(ALL_CFLAGS) $< -o $@ ##################################### brlib libraries .PHONY: cleanbrlib cleanallbrlib brlib @@ -231,7 +231,7 @@ cleanbindir: $(call rmdir,$(BINDIR),binaries) $(TARGET): libs $(OBJ) | $(BINDIR) - @echo generating $@ executable. + @echo generating $@. $(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@ ##################################### pre-processed (.i) and assembler (.s) output @@ -241,11 +241,11 @@ cleanasmcpp: @$(call rmfiles,$(ASMFILES) $(CPPFILES),asm and pre-processed) %.i: %.c - @echo generating $@ + @echo generating $@ (cpp processed). @$(CC) -E $(CPPFLAGS) $(CFLAGS) $< -o $@ %.s: %.c - @echo generating $@ + @echo generating $@ (asm). @$(CC) -S -fverbose-asm $(CPPFLAGS) $(CFLAGS) $< -o $@ ##################################### LSP (ccls) diff --git a/src/bitboard.c b/src/bitboard.c index 4f4aa60..e78e3d6 100644 --- a/src/bitboard.c +++ b/src/bitboard.c @@ -25,6 +25,7 @@ bitboard_t bb_sq[64]; bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64]; bitboard_t bb_between_excl[64][64]; bitboard_t bb_between[64][64]; +bitboard_t bb_line[64][64]; bitboard_t bb_knight[64], bb_king[64]; @@ -59,7 +60,7 @@ bitboard_t bitboard_between_excl(square_t sq1, square_t sq2) btwn_bits = (m1 << sq1) ^ (m1 << sq2); /* includes sq1 and sq2 */ rank_diff = ((sq2 | 7) - sq1) >> 3, /* signed */ - file_diff = (sq2 & 7) - (sq1 & 7); /* signed */ + file_diff = (sq2 & 7) - (sq1 & 7); /* signed */ anti_diff = rank_diff + file_diff; rank_diff = rank_diff & 15; @@ -139,6 +140,24 @@ void bitboard_init(void) bb_sqdiag[sq] = tmpbb[sq][2]; bb_sqanti[sq] = tmpbb[sq][3]; } + for (square_t sq1 = 0; sq1 < 64; ++sq1) { + for (square_t sq2 = 0; sq2 < 64; ++sq2) { + if (sq1 != sq2) { + if (bb_sqfile[sq1] == bb_sqfile[sq2]) + bb_line[sq1][sq2] = bb_sqfile[sq1]; + else if (bb_sqrank[sq1] == bb_sqrank[sq2]) + bb_line[sq1][sq2] = bb_sqrank[sq1]; + else if (bb_sqdiag[sq1] == bb_sqdiag[sq2]) + bb_line[sq1][sq2] = bb_sqdiag[sq1]; + else if (bb_sqanti[sq1] == bb_sqanti[sq2]) + bb_line[sq1][sq2] = bb_sqanti[sq1]; + + //if (bb_line[sq1][sq2]) { + // printf("bb_line[%d][%d] = %16lx\n", sq1, sq2, bb_line[sq1][sq2]); + //} + } + } + } /* 3) knight and king moves */ for (square_t sq = A1; sq <= H8; ++sq) { diff --git a/src/bitboard.h b/src/bitboard.h index 3eb0c27..f17b3e3 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -21,8 +21,6 @@ #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 */ @@ -30,12 +28,17 @@ extern bitboard_t bb_between_excl[64][64]; /* squares between sq1 and sq2, including sq2 */ extern bitboard_t bb_between[64][64]; -/* bb_sqrank[64]: square to rank +/** + * bb_sqrank[64]: square to rank * bb_sqfile[64]: square to file * bb_sqdiag[64]: square to diagonal * bb_sqanti[64]: square to antidiagonal */ extern bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64]; + +/* line (rank, file, diagonal or anti-diagonal) between two squares */ +extern bitboard_t bb_line[64][64]; + /* knight and king moves */ extern bitboard_t bb_knight[64], bb_king[64]; @@ -192,17 +195,24 @@ static __always_inline bitboard_t bb_file(int file) /** * bb_sq_aligned() - check if two squares are aligned (same file or rank). - * @sq1: square 1 - * @sq2: square 2 + * @sq1, @sq2: the two squares. * - * @sq2 is included in return value, to be non zero if the two squares - * are neighbors. - * - * @return: bitboard of squares between @sq1 and @sq2, including @sq2. + * @return: true if @sq1 and @sq2 are on same line, false otherwise. */ -static __always_inline bitboard_t bb_sq_aligned(square_t sq1, square_t sq2) +static __always_inline bool bb_sq_aligned(square_t sq1, square_t sq2) { - return bb_between[sq1][sq2]; + return bb_line[sq1][sq2]; +} + +/** + * bb_sq_aligned3() - check if 3 squares are aligned (same file or rank). + * @sq1, @sq2, @sq3: the three squares. + * + * @return: true if @sq1, @sq2, and @sq3 are aligned, false otherwise. + */ +static __always_inline bool bb_sq_aligned3(square_t sq1, square_t sq2, square_t sq3) +{ + return bb_line[sq1][sq2] & mask(sq3); } /** diff --git a/src/board.h b/src/board.h index 9e5db17..b10a35c 100644 --- a/src/board.h +++ b/src/board.h @@ -59,14 +59,28 @@ static __always_inline rank_t sq_rank(square_t square) #define sq_ok(sq) ((sq) >= A1 && (sq) <= H8) #define sq_coord_ok(c) ((c) >= 0 && (c) < 8) -/* Chebyshev distance: max( |r2 - r1|, |f2 - f1| ) +/** + * sq_dist() - Chebyshev (king) distance between two squares (macro). + * @sq1, @sq2: The two squares. + * * See: https://www.chessprogramming.org/Distance + * Distance is max( |r2 - r1|, |f2 - f1| ) + * + * @Return: the Chebyshev distance. */ #define sq_dist(sq1, sq2) (max(abs(sq_file(sq2) - sq_file(sq1)), \ abs(sq_rank(sq2) - sq_rank(sq1)))) -/* Manhattan distance: |r2 - r1| + |f2 - f1| + +/** + * sq_taxi() - Manhattan (taxi) distance between two squares (macro). + * @sq1, @sq2: The two squares. + * + * See: https://www.chessprogramming.org/Distance + * Distance is |r2 - r1| + |f2 - f1|. + * + * @Return: the Manhattan distance. */ -#define sq_manh(sq1, sq2) (abs(sq_file(sq2) - sq_file(sq1)) + \ +#define sq_taxi(sq1, sq2) (abs(sq_file(sq2) - sq_file(sq1)) + \ abs(sq_rank(sq2) - sq_rank(sq1))) extern const char *sq_to_string(const square_t sq); diff --git a/test/attack-test.c b/test/attack-test.c new file mode 100644 index 0000000..10b8e29 --- /dev/null +++ b/test/attack-test.c @@ -0,0 +1,50 @@ +/* attack-test.c - basic square attack tests. + * + * Copyright (C) 2024 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include +#include +#include + +#include "chessdefs.h" +#include "fen.h" +#include "position.h" +#include "move-gen.h" +#include "attack.h" + +#include "common-test.h" + +int main(int __unused ac, __unused char**av) +{ + int i = 0; + char *fen; + pos_t *pos;//, *fishpos = pos_new(); + + setlinebuf(stdout); /* line-buffered stdout */ + + bitboard_init(); + hyperbola_init(); + + while ((fen = next_fen(ATTACK))) { + //printf(">>>>> %s\n", test[i]); + printf("original fen %d: [%p][%s]\n", i, fen, fen); + if (!(pos = fen2pos(NULL, fen))) { + printf("wrong fen %d: [%s]\n", i, fen); + continue; + } + pos_print(pos); + + pos_del(pos); + i++; + } + return 0; +}