Use SquareDelta instead
No functional change.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
void init_between_bitboards() {
void init_between_bitboards() {
- const SquareDelta directionToDelta[] = {
- DELTA_E, DELTA_N, DELTA_NE, DELTA_NW, DELTA_W, DELTA_S, DELTA_SW, DELTA_SE
- };
-
for (s1 = SQ_A1; s1 <= SQ_H8; s1++)
for (s2 = SQ_A1; s2 <= SQ_H8; s2++)
{
BetweenBB[s1][s2] = EmptyBoardBB;
for (s1 = SQ_A1; s1 <= SQ_H8; s1++)
for (s2 = SQ_A1; s2 <= SQ_H8; s2++)
{
BetweenBB[s1][s2] = EmptyBoardBB;
- d = direction_between_squares(s1, s2);
+ d = SquareDelta(DirectionTable[s1][s2]);
- SquareDelta sd = directionToDelta[s2 > s1 ? d : d + 4];
-
- for (s3 = s1 + sd; s3 != s2; s3 += sd)
+ for (s3 = s1 + d; s3 != s2; s3 += d)
set_bit(&(BetweenBB[s1][s2]), s3);
}
}
set_bit(&(BetweenBB[s1][s2]), s3);
}
}
-uint8_t DirectionTable[64][64];
+int8_t DirectionTable[64][64];
-static bool reachable(Square orig, Square dest, Direction d) {
+static SquareDelta direction(Square orig, Square dest) {
- const SquareDelta directionToDelta[] = {
+ const SquareDelta directions[] = {
DELTA_E, DELTA_N, DELTA_NE, DELTA_NW, DELTA_W, DELTA_S, DELTA_SW, DELTA_SE
};
DELTA_E, DELTA_N, DELTA_NE, DELTA_NW, DELTA_W, DELTA_S, DELTA_SW, DELTA_SE
};
- SquareDelta delta = directionToDelta[dest > orig ? d : d + 4];
- Square from = orig;
- Square to = from + delta;
- while (to != dest && square_distance(to, from) == 1 && square_is_ok(to))
+ for (int idx = 0; idx < 8; idx++)
- from = to;
- to += delta;
+ from = orig;
+ to = from + directions[idx];
+
+ while (to != dest && square_distance(to, from) == 1 && square_is_ok(to))
+ {
+ from = to;
+ to += directions[idx];
+ }
+
+ if (to == dest && square_distance(from, to) == 1)
+ return directions[idx];
- return to == dest && square_distance(from, to) == 1;
}
void init_direction_table() {
for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++)
}
void init_direction_table() {
for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++)
- {
- DirectionTable[s1][s2] = uint8_t(DIR_NONE);
-
- if (s1 == s2)
- continue;
-
- for (Direction d = DIR_E; d != DIR_NONE; d++)
- {
- if (reachable(s1, s2, d))
- {
- DirectionTable[s1][s2] = uint8_t(d);
- break;
- }
- }
- }
+ DirectionTable[s1][s2] = uint8_t(direction(s1, s2));
// A non-king move is legal if and only if it is not pinned or it
// is moving along the ray towards or away from the king.
// A non-king move is legal if and only if it is not pinned or it
// is moving along the ray towards or away from the king.
- return ( !pinned
- || !bit_is_set(pinned, from)
- || (direction_between_squares(from, king_square(us)) == direction_between_squares(move_to(m), king_square(us))));
+ return !pinned
+ || !bit_is_set(pinned, from)
+ || squares_aligned(from, move_to(m), king_square(us));
{
// For pawn and king moves we need to verify also direction
if ( (pt != PAWN && pt != KING)
{
// For pawn and king moves we need to verify also direction
if ( (pt != PAWN && pt != KING)
- ||(direction_between_squares(from, ci.ksq) != direction_between_squares(to, ci.ksq)))
+ || !squares_aligned(from, to, ci.ksq))
- DELTA_N = 8, DELTA_E = 1, DELTA_S = -8, DELTA_W = -1,
+ DELTA_N = 8, DELTA_E = 1, DELTA_S = -8, DELTA_W = -1, DELTA_NONE = 0,
DELTA_NN = DELTA_N + DELTA_N,
DELTA_NE = DELTA_N + DELTA_E,
DELTA_SE = DELTA_S + DELTA_E,
DELTA_SS = DELTA_S + DELTA_S,
DELTA_SW = DELTA_S + DELTA_W,
DELTA_NN = DELTA_N + DELTA_N,
DELTA_NE = DELTA_N + DELTA_E,
DELTA_SE = DELTA_S + DELTA_E,
DELTA_SS = DELTA_S + DELTA_S,
DELTA_SW = DELTA_S + DELTA_W,
- DELTA_NW = DELTA_N + DELTA_W,
+ DELTA_NW = DELTA_N + DELTA_W
-enum Direction {
- DIR_E = 0, DIR_N = 1, DIR_NE = 2, DIR_NW = 3, DIR_NONE = 4
-};
-
-
ENABLE_OPERATORS_ON(Square);
ENABLE_OPERATORS_ON(File);
ENABLE_OPERATORS_ON(Rank);
ENABLE_OPERATORS_ON(SquareDelta);
ENABLE_OPERATORS_ON(Square);
ENABLE_OPERATORS_ON(File);
ENABLE_OPERATORS_ON(Rank);
ENABLE_OPERATORS_ON(SquareDelta);
-ENABLE_OPERATORS_ON(Direction);
const int FlipMask = 56;
const int FlopMask = 7;
const int FlipMask = 56;
const int FlopMask = 7;
-extern uint8_t DirectionTable[64][64];
+extern int8_t DirectionTable[64][64];
return file_is_ok(square_file(s)) && rank_is_ok(square_rank(s));
}
return file_is_ok(square_file(s)) && rank_is_ok(square_rank(s));
}
-inline Direction direction_between_squares(Square s1, Square s2) {
- return Direction(DirectionTable[s1][s2]);
-}
-
-inline int direction_is_diagonal(Square s1, Square s2) {
- return DirectionTable[s1][s2] & 2;
+inline bool squares_aligned(Square s1, Square s2, Square s3) {
+ return DirectionTable[s1][s2] != DELTA_NONE
+ && abs(DirectionTable[s1][s2]) == abs(DirectionTable[s2][s3]);
}
inline bool direction_is_straight(Square s1, Square s2) {
}
inline bool direction_is_straight(Square s1, Square s2) {
- return DirectionTable[s1][s2] < 2;
+ return abs(DirectionTable[s1][s2]) == DELTA_N
+ || abs(DirectionTable[s1][s2]) == DELTA_E;