add sq_line array, bb_sq_aligned3, renamed sq_manh to sq_taxi

This commit is contained in:
2024-03-11 16:04:45 +01:00
parent 87e7695873
commit d81dca6e23
5 changed files with 112 additions and 19 deletions

View File

@@ -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) {

View File

@@ -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);
}
/**

View File

@@ -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);