summaryrefslogtreecommitdiff
path: root/src/chess.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/chess.h')
-rwxr-xr-xsrc/chess.h256
1 files changed, 256 insertions, 0 deletions
diff --git a/src/chess.h b/src/chess.h
index 4bc3464..ed903a5 100755
--- a/src/chess.h
+++ b/src/chess.h
@@ -787,6 +787,30 @@ typedef struct _MOVE_STACK
ULONG uEnd[MAX_PLY_PER_SEARCH];
MOVE mvHash[MAX_PLY_PER_SEARCH];
GENERATOR_FLAGS sGenFlags[MAX_PLY_PER_SEARCH];
+
+ // Scratch, computed once per _GenerateAllMoves call (not per
+ // generator-function call) when any GENERATE_*_BITBOARD toggle is
+ // active -- every bitboard-backed piece-type generator for the
+ // side to move needs "which squares does pos->uToMove already
+ // occupy" (to AND off as illegal destinations), and rebuilding it
+ // from pos->bbPieces/bbPawns freshly inside each generator call
+ // was measured as real, avoidable overhead (board_representation/
+ // MOVEGEN_MIGRATION.md section 5's knight benchmark showed a net
+ // slowdown vs. mailbox until this was hoisted out). Direct callers
+ // of a _Generate*BB function outside of _GenerateAllMoves (e.g.
+ // testgenerate.c's benchmark/correctness harness) must set this
+ // themselves first -- it is not implicitly valid.
+ BITBOARD bbFriendlyOccupied;
+
+ // Same idea as bbFriendlyOccupied, for the slider (rook/bishop/
+ // queen) magic-bitboard generators: full-board occupancy, both
+ // sides, needed to index into g_RookAttackTable/g_BishopAttackTable
+ // (the magic lookup answers "attacks given this exact occupancy,"
+ // not "attacks given only my own pieces"). Computed once per node
+ // by _GenerateAllMoves when any GENERATE_ROOK_BITBOARD/
+ // GENERATE_BISHOP_BITBOARD toggle is active, same amortization
+ // reasoning as bbFriendlyOccupied.
+ BITBOARD bbOccupied;
}
MOVE_STACK;
@@ -1827,6 +1851,42 @@ ExposesCheckEp(POSITION *pos,
COOR cBlock,
COOR cKing);
+// board_representation/MOVEGEN_MIGRATION.md section 6b
+COOR
+FasterExposesCheckBB(POSITION *pos,
+ COOR cRemove,
+ COOR cLocation);
+
+COOR
+ExposesCheckBB(POSITION *pos,
+ COOR cRemove,
+ COOR cLocation);
+
+COOR
+ExposesCheckEpBB(POSITION *pos,
+ COOR cTest,
+ COOR cIgnore,
+ COOR cBlock,
+ COOR cKing);
+
+// Three-way choice matching GetAttacks's own pattern (MIGRATION.md
+// section 6): EXPOSESCHECK_BITBOARD defined -> the BB versions above;
+// else the mailbox versions declared right above this block.
+#if defined(EXPOSESCHECK_BITBOARD)
+#define ExposesCheck ExposesCheckBB
+#define FasterExposesCheck FasterExposesCheckBB
+#define ExposesCheckEp ExposesCheckEpBB
+#endif
+
+// Real mailbox declarations first, unconditionally, so the plain names
+// "IsAttacked"/"InCheck" are always genuinely declared -- the #define
+// below only affects *later* call sites in files that include this
+// header after this point, matching GetAttacks's own three-way-choice
+// pattern (chess.h, `GetAttacks` block) exactly. Declaring these after
+// the #define instead (an earlier mistake here, caught by testsee.c's
+// TestIsAttackedBB needing to call the real mailbox function by name)
+// would macro-substitute this very declaration too, leaving the real
+// name never actually declared anywhere.
FLAG
IsAttacked(COOR cTest, POSITION *pos, ULONG uSide);
@@ -1834,6 +1894,20 @@ FLAG
InCheck(POSITION *pos, ULONG uSide);
FLAG
+IsAttackedBB(COOR cTest, POSITION *pos, ULONG uSide);
+
+FLAG
+InCheckBB(POSITION *pos, ULONG uSide);
+
+// ISATTACKED_BITBOARD defined -> the BB versions above resolve plain
+// "IsAttacked"/"InCheck" call sites from here on; else the mailbox
+// versions declared just above stay live.
+#if defined(ISATTACKED_BITBOARD)
+#define IsAttacked IsAttackedBB
+#define InCheck InCheckBB
+#endif
+
+FLAG
SanityCheckMove(POSITION *pos, MOVE mv);
FLAG
@@ -1947,6 +2021,141 @@ GenerateMoves(SEARCHER_THREAD_CONTEXT *ctx,
MOVE mvOrderFirst,
ULONG uType);
+// board_representation/MOVEGEN_MIGRATION.md section 3 step 1 -- see
+// generate.c for the routine description. Exposed non-static so
+// testgenerate.c's correctness/speed harness can call it directly
+// regardless of whether GENERATE_KNIGHT_BITBOARD is defined for this
+// build.
+BITBOARD
+_BuildFriendlySideBB(POSITION *pos, ULONG uSide);
+
+BITBOARD
+_BuildFullOccupiedBB(POSITION *pos);
+
+// board_representation/MOVEGEN_MIGRATION.md section 6b -- exposed so
+// movesup.c's ExposesCheckBB family can reuse the same magic-lookup
+// arithmetic as the Part A/B generators instead of duplicating it.
+BITBOARD
+_RookAttacksBB(COOR c, BITBOARD bbOccupied);
+
+BITBOARD
+_BishopAttacksBB(COOR c, BITBOARD bbOccupied);
+
+void
+_GenerateRookBB(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cRook);
+
+void
+GenerateRook(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cRook);
+
+void
+_GenerateBishopBB(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cBishop);
+
+void
+GenerateBishop(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cBishop);
+
+void
+_GenerateQueenBB(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cQueen);
+
+// Whole-node dispatch pair -- see _GenerateAllMovesBB's block comment
+// in generate.c. Exposed (not static) so testgenerate.c can benchmark
+// them directly against each other by name.
+void
+_GenerateAllMoves(MOVE_STACK *pStack,
+ POSITION *pos);
+
+void
+_GenerateAllMovesBB(MOVE_STACK *pStack,
+ POSITION *pos);
+
+void
+_GenerateAllPawnMovesBB(MOVE_STACK *pStack,
+ POSITION *pos,
+ ULONG uSide);
+
+// board_representation/MOVEGEN_MIGRATION.md section 6a Phase 2 --
+// exposed (not static) for testgenerate.c's future speed harness,
+// same convention as the Part A _Generate*BB functions.
+void
+_SaveMeKnightBB(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cKnight,
+ COOR cKing,
+ BITBOARD bbTargetMask);
+
+void
+_SaveMeBishopBB(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cBishop,
+ COOR cKing,
+ BITBOARD bbTargetMask);
+
+void
+_SaveMeRookBB(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cRook,
+ COOR cKing,
+ BITBOARD bbTargetMask);
+
+void
+_SaveMeQueenBB(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cQueen,
+ COOR cKing,
+ BITBOARD bbTargetMask);
+
+void
+_SaveMeAllPawnMovesBB(MOVE_STACK *pStack,
+ POSITION *pos,
+ ULONG uSide,
+ COOR cKing,
+ COOR cAttacker,
+ BITBOARD bbTargetMask);
+
+void
+GenerateQueen(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cQueen);
+
+void
+_GenerateKnightBB(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cKnight);
+
+void
+GenerateKnight(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cKnight);
+
+void
+GenerateWhiteKnight(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cKnight);
+
+void
+_GenerateKingBB(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cKing);
+
+void
+GenerateBlackKing(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cKing);
+
+void
+GenerateWhiteKing(MOVE_STACK *pStack,
+ POSITION *pos,
+ COOR cKing);
+
FLAG
WouldGiveCheck(IN SEARCHER_THREAD_CONTEXT *ctx,
IN MOVE mv);
@@ -1967,6 +2176,24 @@ TestMoveGenerator(void);
void
TestLegalMoveGenerator(void);
+void
+TestGenerateKnightSpeed(void);
+
+void
+TestGenerateKingSpeed(void);
+
+void
+TestGenerateRookSpeed(void);
+
+void
+TestGenerateBishopSpeed(void);
+
+void
+TestGenerateQueenSpeed(void);
+
+void
+TestGenerateAllMovesSpeed(void);
+
#endif
//
@@ -2046,7 +2273,16 @@ extern BITBOARD g_BishopRayAll[128];
extern const int g_BishopRayDeltas[4];
extern const FLAG g_BishopRayPositiveDir[4];
extern BITBOARD g_KnightAttacksBB[128];
+extern BITBOARD g_KingAttacksBB[128];
extern BITBOARD g_PawnAttackOriginBB[2][128];
+extern BITBOARD g_RookOccupancyMask[128];
+extern BITBOARD g_BishopOccupancyMask[128];
+extern BITBOARD g_RookMagic[128];
+extern BITBOARD g_BishopMagic[128];
+extern ULONG g_RookMagicShift[128];
+extern ULONG g_BishopMagicShift[128];
+extern BITBOARD *g_RookAttackTable[128];
+extern BITBOARD *g_BishopAttackTable[128];
void
InitializeWhiteSquaresTable(void);
@@ -2070,8 +2306,14 @@ void
InitializeKnightAttackTables(void);
void
+InitializeKingAttackTables(void);
+
+void
InitializePawnAttackOriginTable(void);
+void
+InitMagic(void);
+
#ifdef DEBUG
ULONG CheckVectorWithIndex(int i, ULONG uColor);
#define CHECK_VECTOR_WITH_INDEX(i, color) \
@@ -2603,6 +2845,9 @@ DebugSEE(POSITION *pos,
void
TestGetAttacks(void);
+void
+TestIsAttackedBB(void);
+
//
// hash.c
//
@@ -2905,6 +3150,17 @@ _GetAttacksBB(SEE_LIST *pList,
COOR cSquare,
ULONG uSide);
+// board_representation/MOVEGEN_MIGRATION.md section 6a: exposed (not
+// static) so generate.c's Part B king-flight code can call it with a
+// king-vacated occupancy bitboard. Deliberately excludes pawns -- see
+// its own header comment in see.c; callers needing pawn attacks must
+// check g_PawnAttackOriginBB separately.
+BITBOARD
+_WhoAttacksSquareBB(POSITION *pos,
+ COOR cSquare,
+ ULONG uSide,
+ BITBOARD bbOccupied);
+
// Three-way choice for which GetAttacks implementation is actually
// live -- see MIGRATION.md section 6:
// GETATTACKS_BITBOARD defined -> _GetAttacksBB (bitboard, new)