- enum Result {
- RESULT_UNKNOWN,
- RESULT_INVALID,
- RESULT_WIN,
- RESULT_LOSS,
- RESULT_DRAW
- };
-
- struct KPKPosition {
- void from_index(int index);
- int to_index() const;
- bool is_legal() const;
- bool is_immediate_draw() const;
- bool is_immediate_win() const;
- Bitboard wk_attacks() const;
- Bitboard bk_attacks() const;
- Bitboard pawn_attacks() const;
-
- Square whiteKingSquare, blackKingSquare, pawnSquare;
- Color sideToMove;
- };
-
-
- Result *Bitbase;
- const int IndexMax = 2*24*64*64;
- int UnknownCount = 0;
-
- void initialize();
- bool next_iteration();
- Result classify_wtm(const KPKPosition &p);
- Result classify_btm(const KPKPosition &p);
- int compute_index(Square wksq, Square bksq, Square psq, Color stm);
- int compress_result(Result r);
-
-}
-
-
-////
-//// Functions
-////
-
-void generate_kpk_bitbase(uint8_t bitbase[]) {
- // Allocate array and initialize:
- Bitbase = new Result[IndexMax];
- initialize();
-
- // Iterate until all positions are classified:
- while(next_iteration());
-
- // Compress bitbase into the supplied parameter:
- int i, j, b;
- for(i = 0; i < 24576; i++) {
- for(b = 0, j = 0; j < 8; b |= (compress_result(Bitbase[8*i+j]) << j), j++);
- bitbase[i] = b;
- }
-
- // Release allocated memory:
- delete [] Bitbase;
-}
-
-
-namespace {
-
- void KPKPosition::from_index(int index) {
- int s;
- sideToMove = Color(index % 2);
- blackKingSquare = Square((index / 2) % 64);
- whiteKingSquare = Square((index / 128) % 64);
- s = (index / 8192) % 24;
- pawnSquare = make_square(File(s % 4), Rank(s / 4 + 1));
- }
-
-
- int KPKPosition::to_index() const {
- return compute_index(whiteKingSquare, blackKingSquare, pawnSquare,
- sideToMove);
- }
-
-
- bool KPKPosition::is_legal() const {
- if(whiteKingSquare == pawnSquare || whiteKingSquare == blackKingSquare ||
- pawnSquare == blackKingSquare)
- return false;
- if(sideToMove == WHITE) {
- if(bit_is_set(this->wk_attacks(), blackKingSquare))
- return false;
- if(bit_is_set(this->pawn_attacks(), blackKingSquare))
- return false;
- }
- else {
- if(bit_is_set(this->bk_attacks(), whiteKingSquare))
- return false;
- }
- return true;
+ // The possible pawns squares are 24, the first 4 files and ranks from 2 to 7
+ const unsigned IndexMax = 2*24*64*64; // stm * psq * wksq * bksq = 196608
+
+ // Each uint32_t stores results of 32 positions, one per bit
+ uint32_t KPKBitbase[IndexMax / 32];
+
+ // A KPK bitbase index is an integer in [0, IndexMax] range
+ //
+ // Information is mapped in a way that minimizes number of iterations:
+ //
+ // bit 0- 5: white king square (from SQ_A1 to SQ_H8)
+ // bit 6-11: black king square (from SQ_A1 to SQ_H8)
+ // bit 12: side to move (WHITE or BLACK)
+ // bit 13-14: white pawn file (from FILE_A to FILE_D)
+ // bit 15-17: white pawn RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2)
+ unsigned index(Color us, Square bksq, Square wksq, Square psq) {
+ return wksq + (bksq << 6) + (us << 12) + (file_of(psq) << 13) + ((RANK_7 - rank_of(psq)) << 15);