- uint32_t KPKBitbase[IndexMax / 32];
-
- Result classify_wtm(const KPKPosition& pos, const Result bb[]);
- Result classify_btm(const KPKPosition& pos, const Result bb[]);
- int compute_index(Square wksq, Square bksq, Square wpsq, Color stm);
-}
-
-
-uint32_t probe_kpk_bitbase(Square wksq, Square wpsq, Square bksq, Color stm) {
-
- int index = compute_index(wksq, bksq, wpsq, stm);
-
- return KPKBitbase[index / 32] & (1 << (index & 31));
-}
-
-
-void init_kpk_bitbase() {
-
- Result bb[IndexMax];
- KPKPosition pos;
- bool repeat;
-
- // Initialize table
- for (int i = 0; i < IndexMax; i++)
- {
- pos.from_index(i);
- bb[i] = !pos.is_legal() ? RESULT_INVALID
- : pos.is_immediate_draw() ? RESULT_DRAW
- : pos.is_immediate_win() ? RESULT_WIN : RESULT_UNKNOWN;
+ uint32_t KPKBitbase[MAX_INDEX / 32];
+
+ // A KPK bitbase index is an integer in [0, IndexMax] range
+ //
+ // Information is mapped in a way that minimizes the 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);