Context
I'm developing a chess engine of my own, where if anything can be pre-initialized/pre-calculated, should be. This is because speed is the number-1 priority, and every extra second you spend calculating stuff that could've been calculated before-hand, stacks up and ultimately makes you lose a lot of important time that should have been used for things that cannot be pre-calculated, like evaluation.
The main pre-initialized variables are the various Bitboard masks.
- Diagonals
- Ranks
- Files
- Different square patterns ( center ,edges, light squares, dark squares )
- Attacks for pieces that can be pre-calculated - Knight, King, Pawns
constepxr is present everywhere.
I have a separate header file that manages all of these. They can be initialized once in the start and then used throughout the program several times.
Question
What I am currently doing is using nested namespaces
Mask.h
// Bitboard is an alias for uint64_t
// NB_SQ is the number of squares (64)
namespace Mask
{
void InitMasks();
void InitAttacks();
namespace File
{ // few examples
constexpr Bitboard FileA = 0x0101010101010101;
constexpr Bitboard FileB = FileA << 1;
constexpr Bitboard FileC = FileB << 1;
constexpr Bitboard FileD = FileC << 1;
constexpr Bitboard FileE = FileD << 1;
constexpr Bitboard FileF = FileE << 1;
...
constexpr Bitboard notFileAB = ~(FileA | FileB)
constexpr Bitboard notFileGF = ~(FileG | FileF)
extern uint64_t AllFiles[NB_SQ]
}
namespace Rank{...}
namespace Diagonal {...}
namespace Attack
{
extern Bitboard PawnAttacks[NB_SQ];
extern Bitboard KnightAttacks[NB_SQ];
...
}
}
All extern arrays are initialized once using InitMasks() and InitAttacks()
Examples
I can know how many white pawns are on the 7th rank
WhitePawnsBB & Mask::Rank::Rank7
Or If I want to get the file mask of a specific square
Mask::File::AllFiles[SQ]
If I need to get the pre-calculated set of attacks for a knight on any square
Mask::Attack::KnightAttacks[SQ];
But I am really not happy with this, it gets very messy to know which mask can belong where. Moreover, this isn't the purpose of namespaces, Is there a better way I can structure this?