hgducharme / meatball

A C++17 chess engine written entirely from scratch (WIP)
GNU General Public License v2.0
0 stars 0 forks source link

Make a lot of the precalculated variables constant expressions #19

Open hgducharme opened 1 year ago

hgducharme commented 1 year ago

Figure out how to make a lot of the deterministic, precalculated variables such as magic bitboards, attack tables, etc. constexpr types. Usually when I do this I get redundant symbol definition compiler errors

hgducharme commented 1 year ago

When trying to have magic_bitboards::init() resolve at compile time, I get the following error:

error: constexpr function's return type 'magic_bitboards::(anonymous namespace)::std::array<std::vector<Bitboard>, Square::NUMBER_OF_SQUARES>' is not a literal type
constexpr std::array<std::vector<Bitboard>, Square::NUMBER_OF_SQUARES> calculateBlockerVariations(HashingParameters const * hashingParametersLookup)

Basically, any function that returns std::array<std::vector<Bitboard>, Square::NUMBER_OF_SQUARES> can not be a constexpr because std::array<std::vector<Bitboard>, Square::NUMBER_OF_SQUARES> is not a literal type.

C++ requires that a constexpr function return a literal type. std::vector is not considered a literal type since it involves dynamic memory allocation. To make std::array<std::vector<Bitboard>, Square::NUMBER_OF_SQUARES> a literal type we need to switch to a fixed width container like an array of arrays, where we know the size at compile time. Something like

std::array<std::array<Bitboard, MaxSize>, Square::NUMBER_OF_SQUARES>` where `MaxSize` is known at compiled time.