From de8f43c35b435674777eca98790ed3d086682ce4 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 13 Aug 2026 18:31:44 +0200 Subject: adding more memory efficnet helper functions --- src/board/board.cpp | 34 ++++++++++++++++++++++++++++++++++ src/board/board.hpp | 39 ++++++--------------------------------- 2 files changed, 40 insertions(+), 33 deletions(-) (limited to 'src/board') diff --git a/src/board/board.cpp b/src/board/board.cpp index ad5ab12..9d0ef47 100644 --- a/src/board/board.cpp +++ b/src/board/board.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "board.hpp" #include "moves.hpp" @@ -9,6 +10,39 @@ int PositionToIndex(Position i) { return (i.rank * 8) + i.file; } +PieceType getPromotionTypeFromMove(uint16_t move) { + bool firstFlag = (move & (1 << 1)) != 0; + bool secondFlag = (move & (1 << 0)) != 0; + + /* + * 00 for knight + * 10 for queen + * 01 for rook + * 11 for bishop + */ + if (!firstFlag && !secondFlag) { + // 00 + return KNIGHT; + } + if (firstFlag && !secondFlag) { + // 10 + return QUEEN; + } + if (!firstFlag && secondFlag) { + // 01 + return ROOK; + } + if (firstFlag && secondFlag) { + // 11 + return BISHOP; + } + std::cout << "should have never happend"; + assert(false); + return ROOK; +}; +uint8_t getFromValueFromMove(uint16_t move) { return (move >> 2) & 63; }; +uint8_t getToValueFromMove(uint16_t move) { return (move >> 8) & 63; }; + Position FindKing(Game *g, bool color) { return IndexToPosition(__builtin_ctzll(g->PieceBitboards[color][KING])); } diff --git a/src/board/board.hpp b/src/board/board.hpp index 35e2196..8ef3e56 100644 --- a/src/board/board.hpp +++ b/src/board/board.hpp @@ -34,40 +34,13 @@ struct Position { }; /* - * Maybe we can try to be efficent with memory by using a byte (char) or - * something like that let me brainstorm a little - * - * first two bits can be promotion flag because there are 4 posible options: - * NONE BISHOP ROOK QUEEN - * but wait none it not requided because its only applied - * when on last rank - * but that doesnt help becuase we cant have three options - * ok so first 2 bits for promotion flag - * like this ZZYYYYYY with ZZ being the promotion flag - * 00 for none piece - * 01 for queen - * 10 for rook - * 11 for bishop - * these valuesa are abritrari - * so we have other 6 bytes which doesnt work that well because we need to store - * two 64 bit numbers one for "From" and second for "To" - * so wait how much is a 64 for bit - * 2 * 2 = 4 (2 bits) - * 4 * 2 = 8 (3 bits) - * 8 * 2 = 16 (4 bits) - * 16 * 2 = 32 (5 bits) - * 32 * 2 = 64 (6 bits) - * so six bits that means that we can only store one posiiton we need a 2 + 6 * - * 2 (14) bits of value to represent it but wait when promotion flag isnt 0 we - * know its last rank oh but we dont know if from white or black perpective so - * 14 bits is required let me search if there is a way to store that data - * alright search done result seams that best way is to use 16 bit interger - * which look like good solution and we have 2 bits left if we want to add more - * things here is a diagram for it uint16_t: XXFFFFFFTTTTTT XX for promotion - * flag F for from position T for to position so we can try first writing a - * helper function for working with it then replace existing move struct + * things here is a diagram for it uint16_t: XXFFFFFFTTTTTT00, XX for promotion + * flag F for from position T for to position, and 00 for empty ones. */ +PieceType getPromotionTypeFromMove(uint16_t move); +uint8_t getFromValueFromMove(uint16_t move); + struct Move { Position From; Position To; @@ -140,6 +113,6 @@ int PositionToIndex(Position i); UndoMove MakeMove(Move move, Game *g); void UnMakeMove(UndoMove undo, Game *g); -Position FindKing(Game *g, bool white); +Position FindKing(Game *g, bool color); void UpdateHelpers(Game *g); #endif /* SRC_BOARD_H_ */ -- cgit v1.2.3