From cb7f20913e04b051fea6ac9a4055a4e86be452d1 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 26 Dec 2010 12:41:30 +0100 Subject: [PATCH] Retire enum Direction Use SquareDelta instead No functional change. Signed-off-by: Marco Costalba --- src/bitboard.cpp | 14 ++++---------- src/direction.cpp | 43 ++++++++++++++++++------------------------- src/position.cpp | 8 ++++---- src/square.h | 24 ++++++++---------------- 4 files changed, 34 insertions(+), 55 deletions(-) diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 43054cfb..01c9954a 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -455,25 +455,19 @@ namespace { void init_between_bitboards() { - const SquareDelta directionToDelta[] = { - DELTA_E, DELTA_N, DELTA_NE, DELTA_NW, DELTA_W, DELTA_S, DELTA_SW, DELTA_SE - }; - Square s1, s2, s3; - Direction d; + SquareDelta d; 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]); - if (d == DIR_NONE) + if (d == DELTA_NONE) continue; - 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); } } diff --git a/src/direction.cpp b/src/direction.cpp index 77a985b1..ebff65b1 100644 --- a/src/direction.cpp +++ b/src/direction.cpp @@ -24,44 +24,37 @@ #include "square.h" -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 }; - SquareDelta delta = directionToDelta[dest > orig ? d : d + 4]; - Square from = orig; - Square to = from + delta; + Square from, to; - 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; + return DELTA_NONE; } 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)); } diff --git a/src/position.cpp b/src/position.cpp index 2d1e4763..f0301e4e 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -638,9 +638,9 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const { // 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)); } @@ -698,7 +698,7 @@ bool Position::move_is_check(Move m, const CheckInfo& ci) const { { // 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)) return true; } diff --git a/src/square.h b/src/square.h index 731a898e..7a59e078 100644 --- a/src/square.h +++ b/src/square.h @@ -58,26 +58,20 @@ enum Rank { enum SquareDelta { - 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_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(Direction); //// @@ -87,7 +81,7 @@ ENABLE_OPERATORS_ON(Direction); const int FlipMask = 56; const int FlopMask = 7; -extern uint8_t DirectionTable[64][64]; +extern int8_t DirectionTable[64][64]; //// @@ -189,16 +183,14 @@ inline bool square_is_ok(Square 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) { - return DirectionTable[s1][s2] < 2; + return abs(DirectionTable[s1][s2]) == DELTA_N + || abs(DirectionTable[s1][s2]) == DELTA_E; } //// -- 2.39.2