LCOV - code coverage report
Current view: top level - Fathom/src - tbprobe.h (source / functions) Coverage Total Hit
Test: coverage Lines: 0.0 % 5 0
Test Date: 2026-03-02 16:42:41 Functions: - 0 0

            Line data    Source code
       1              : /*
       2              :  * tbprobe.h
       3              :  * (C) 2015 basil, all rights reserved,
       4              :  *
       5              :  * Permission is hereby granted, free of charge, to any person obtaining a
       6              :  * copy of this software and associated documentation files (the "Software"),
       7              :  * to deal in the Software without restriction, including without limitation
       8              :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
       9              :  * and/or sell copies of the Software, and to permit persons to whom the
      10              :  * Software is furnished to do so, subject to the following conditions:
      11              :  *
      12              :  * The above copyright notice and this permission notice shall be included in
      13              :  * all copies or substantial portions of the Software.
      14              :  *
      15              :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      16              :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      17              :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      18              :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      19              :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      20              :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      21              :  * DEALINGS IN THE SOFTWARE.
      22              :  */
      23              : 
      24              : #ifndef TBPROBE_H
      25              : #define TBPROBE_H
      26              : 
      27              : #include <tbconfig.h>
      28              : 
      29              : #ifdef __cplusplus
      30              : extern "C"
      31              : {
      32              : #endif
      33              : 
      34              : #ifndef TB_NO_STDINT
      35              : #include <stdint.h>
      36              : #else
      37              : typedef unsigned char uint8_t;
      38              : typedef unsigned short uint16_t;
      39              : typedef unsigned uint32_t;
      40              : typedef long long unsigned uint64_t;
      41              : typedef char int8_t;
      42              : typedef short int16_t;
      43              : typedef int int32_t;
      44              : typedef long long int64_t;
      45              : #endif
      46              : 
      47              : #ifndef TB_NO_STDBOOL
      48              : #include <stdbool.h>
      49              : #else
      50              : #ifndef __cplusplus
      51              : typedef uint8_t bool;
      52              : #define true    1
      53              : #define false   0
      54              : #endif
      55              : #endif
      56              : 
      57              : /*
      58              :  * Internal definitions.  Do not call these functions directly.
      59              :  */
      60              : extern bool tb_init_impl(const char *_path);
      61              : extern unsigned tb_probe_wdl_impl(
      62              :     uint64_t _white,
      63              :     uint64_t _black,
      64              :     uint64_t _kings,
      65              :     uint64_t _queens,
      66              :     uint64_t _rooks,
      67              :     uint64_t _bishops,
      68              :     uint64_t _knights,
      69              :     uint64_t _pawns,
      70              :     unsigned _ep,
      71              :     bool     _turn);
      72              : extern unsigned tb_probe_root_impl(
      73              :     uint64_t _white,
      74              :     uint64_t _black,
      75              :     uint64_t _kings,
      76              :     uint64_t _queens,
      77              :     uint64_t _rooks,
      78              :     uint64_t _bishops,
      79              :     uint64_t _knights,
      80              :     uint64_t _pawns,
      81              :     unsigned _rule50,
      82              :     unsigned _ep,
      83              :     bool     _turn,
      84              :     unsigned *_results);
      85              : 
      86              : /****************************************************************************/
      87              : /* MAIN API                                                                 */
      88              : /****************************************************************************/
      89              : 
      90              : #define TB_MAX_MOVES                (192+1)
      91              : #define TB_MAX_CAPTURES             64
      92              : #define TB_MAX_PLY                  256
      93              : #define TB_CASTLING_K               0x1     /* White king-side. */
      94              : #define TB_CASTLING_Q               0x2     /* White queen-side. */
      95              : #define TB_CASTLING_k               0x4     /* Black king-side. */
      96              : #define TB_CASTLING_q               0x8     /* Black queen-side. */
      97              : 
      98              : #define TB_LOSS                     0       /* LOSS */
      99              : #define TB_BLESSED_LOSS             1       /* LOSS but 50-move draw */
     100              : #define TB_DRAW                     2       /* DRAW */
     101              : #define TB_CURSED_WIN               3       /* WIN but 50-move draw  */
     102              : #define TB_WIN                      4       /* WIN  */
     103              : 
     104              : #define TB_PROMOTES_NONE            0
     105              : #define TB_PROMOTES_QUEEN           1
     106              : #define TB_PROMOTES_ROOK            2
     107              : #define TB_PROMOTES_BISHOP          3
     108              : #define TB_PROMOTES_KNIGHT          4
     109              : 
     110              : #define TB_RESULT_WDL_MASK          0x0000000F
     111              : #define TB_RESULT_TO_MASK           0x000003F0
     112              : #define TB_RESULT_FROM_MASK         0x0000FC00
     113              : #define TB_RESULT_PROMOTES_MASK     0x00070000
     114              : #define TB_RESULT_EP_MASK           0x00080000
     115              : #define TB_RESULT_DTZ_MASK          0xFFF00000
     116              : #define TB_RESULT_WDL_SHIFT         0
     117              : #define TB_RESULT_TO_SHIFT          4
     118              : #define TB_RESULT_FROM_SHIFT        10
     119              : #define TB_RESULT_PROMOTES_SHIFT    16
     120              : #define TB_RESULT_EP_SHIFT          19
     121              : #define TB_RESULT_DTZ_SHIFT         20
     122              : 
     123              : #define TB_GET_WDL(_res)                        \
     124              :     (((_res) & TB_RESULT_WDL_MASK) >> TB_RESULT_WDL_SHIFT)
     125              : #define TB_GET_TO(_res)                         \
     126              :     (((_res) & TB_RESULT_TO_MASK) >> TB_RESULT_TO_SHIFT)
     127              : #define TB_GET_FROM(_res)                       \
     128              :     (((_res) & TB_RESULT_FROM_MASK) >> TB_RESULT_FROM_SHIFT)
     129              : #define TB_GET_PROMOTES(_res)                   \
     130              :     (((_res) & TB_RESULT_PROMOTES_MASK) >> TB_RESULT_PROMOTES_SHIFT)
     131              : #define TB_GET_EP(_res)                         \
     132              :     (((_res) & TB_RESULT_EP_MASK) >> TB_RESULT_EP_SHIFT)
     133              : #define TB_GET_DTZ(_res)                        \
     134              :     (((_res) & TB_RESULT_DTZ_MASK) >> TB_RESULT_DTZ_SHIFT)
     135              : 
     136              : #define TB_SET_WDL(_res, _wdl)                  \
     137              :     (((_res) & ~TB_RESULT_WDL_MASK) |           \
     138              :      (((_wdl) << TB_RESULT_WDL_SHIFT) & TB_RESULT_WDL_MASK))
     139              : #define TB_SET_TO(_res, _to)                    \
     140              :     (((_res) & ~TB_RESULT_TO_MASK) |            \
     141              :      (((_to) << TB_RESULT_TO_SHIFT) & TB_RESULT_TO_MASK))
     142              : #define TB_SET_FROM(_res, _from)                \
     143              :     (((_res) & ~TB_RESULT_FROM_MASK) |          \
     144              :      (((_from) << TB_RESULT_FROM_SHIFT) & TB_RESULT_FROM_MASK))
     145              : #define TB_SET_PROMOTES(_res, _promotes)        \
     146              :     (((_res) & ~TB_RESULT_PROMOTES_MASK) |      \
     147              :      (((_promotes) << TB_RESULT_PROMOTES_SHIFT) & TB_RESULT_PROMOTES_MASK))
     148              : #define TB_SET_EP(_res, _ep)                    \
     149              :     (((_res) & ~TB_RESULT_EP_MASK) |            \
     150              :      (((_ep) << TB_RESULT_EP_SHIFT) & TB_RESULT_EP_MASK))
     151              : #define TB_SET_DTZ(_res, _dtz)                  \
     152              :     (((_res) & ~TB_RESULT_DTZ_MASK) |           \
     153              :      (((_dtz) << TB_RESULT_DTZ_SHIFT) & TB_RESULT_DTZ_MASK))
     154              : 
     155              : #define TB_RESULT_CHECKMATE         TB_SET_WDL(0, TB_WIN)
     156              : #define TB_RESULT_STALEMATE         TB_SET_WDL(0, TB_DRAW)
     157              : #define TB_RESULT_FAILED            0xFFFFFFFF
     158              : 
     159              : /*
     160              :  * The tablebase can be probed for any position where #pieces <= TB_LARGEST.
     161              :  */
     162              : extern unsigned TB_LARGEST;
     163              : 
     164              : /*
     165              :  * Initialize the tablebase.
     166              :  *
     167              :  * PARAMETERS:
     168              :  * - path:
     169              :  *   The tablebase PATH string.
     170              :  *
     171              :  * RETURN:
     172              :  * - true=success, false=failed.  The TB_LARGEST global will also be
     173              :  *   initialized.  If no tablebase files are found, then `true' is returned
     174              :  *   and TB_LARGEST is set to zero.
     175              :  */
     176              : bool tb_init(const char *_path);
     177              : 
     178              : /*
     179              :  * Free any resources allocated by tb_init
     180              :  */
     181              : void tb_free(void);
     182              : 
     183              : /*
     184              :  * Probe the Win-Draw-Loss (WDL) table.
     185              :  *
     186              :  * PARAMETERS:
     187              :  * - white, black, kings, queens, rooks, bishops, knights, pawns:
     188              :  *   The current position (bitboards).
     189              :  * - rule50:
     190              :  *   The 50-move half-move clock.
     191              :  * - castling:
     192              :  *   Castling rights.  Set to zero if no castling is possible.
     193              :  * - ep:
     194              :  *   The en passant square (if exists).  Set to zero if there is no en passant
     195              :  *   square.
     196              :  * - turn:
     197              :  *   true=white, false=black
     198              :  *
     199              :  * RETURN:
     200              :  * - One of {TB_LOSS, TB_BLESSED_LOSS, TB_DRAW, TB_CURSED_WIN, TB_WIN}.
     201              :  *   Otherwise returns TB_RESULT_FAILED if the probe failed.
     202              :  *
     203              :  * NOTES:
     204              :  * - Engines should use this function during search.
     205              :  * - This function is thread safe assuming TB_NO_THREADS is disabled.
     206              :  */
     207              : static inline unsigned tb_probe_wdl(
     208              :     uint64_t _white,
     209              :     uint64_t _black,
     210              :     uint64_t _kings,
     211              :     uint64_t _queens,
     212              :     uint64_t _rooks,
     213              :     uint64_t _bishops,
     214              :     uint64_t _knights,
     215              :     uint64_t _pawns,
     216              :     unsigned _rule50,
     217              :     unsigned _castling,
     218              :     unsigned _ep,
     219              :     bool     _turn)
     220              : {
     221            0 :     if (_castling != 0)
     222              :         return TB_RESULT_FAILED;
     223            0 :     if (_rule50 != 0)
     224              :         return TB_RESULT_FAILED;
     225            0 :     return tb_probe_wdl_impl(_white, _black, _kings, _queens, _rooks,
     226              :         _bishops, _knights, _pawns, _ep, _turn);
     227              : }
     228              : 
     229              : /*
     230              :  * Probe the Distance-To-Zero (DTZ) table.
     231              :  *
     232              :  * PARAMETERS:
     233              :  * - white, black, kings, queens, rooks, bishops, knights, pawns:
     234              :  *   The current position (bitboards).
     235              :  * - rule50:
     236              :  *   The 50-move half-move clock.
     237              :  * - castling:
     238              :  *   Castling rights.  Set to zero if no castling is possible.
     239              :  * - ep:
     240              :  *   The en passant square (if exists).  Set to zero if there is no en passant
     241              :  *   square.
     242              :  * - turn:
     243              :  *   true=white, false=black
     244              :  * - results (OPTIONAL):
     245              :  *   Alternative results, one for each possible legal move.  The passed array
     246              :  *   must be TB_MAX_MOVES in size.
     247              :  *   If alternative results are not desired then set results=NULL.
     248              :  *
     249              :  * RETURN:
     250              :  * - A TB_RESULT value comprising:
     251              :  *   1) The WDL value (TB_GET_WDL)
     252              :  *   2) The suggested move (TB_GET_FROM, TB_GET_TO, TB_GET_PROMOTES, TB_GET_EP)
     253              :  *   3) The DTZ value (TB_GET_DTZ)
     254              :  *   The suggested move is guaranteed to preserved the WDL value.
     255              :  *
     256              :  *   Otherwise:
     257              :  *   1) TB_RESULT_STALEMATE is returned if the position is in stalemate.
     258              :  *   2) TB_RESULT_CHECKMATE is returned if the position is in checkmate.
     259              :  *   3) TB_RESULT_FAILED is returned if the probe failed.
     260              :  *
     261              :  *   If results!=NULL, then a TB_RESULT for each legal move will be generated
     262              :  *   and stored in the results array.  The results array will be terminated
     263              :  *   by TB_RESULT_FAILED.
     264              :  *
     265              :  * NOTES:
     266              :  * - Engines can use this function to probe at the root.  This function should
     267              :  *   not be used during search.
     268              :  * - DTZ tablebases can suggest unnatural moves, especially for losing
     269              :  *   positions.  Engines may prefer to traditional search combined with WDL
     270              :  *   move filtering using the alternative results array.
     271              :  * - This function is NOT thread safe.  For engines this function should only
     272              :  *   be called once at the root per search.
     273              :  */
     274              : static inline unsigned tb_probe_root(
     275              :     uint64_t _white,
     276              :     uint64_t _black,
     277              :     uint64_t _kings,
     278              :     uint64_t _queens,
     279              :     uint64_t _rooks,
     280              :     uint64_t _bishops,
     281              :     uint64_t _knights,
     282              :     uint64_t _pawns,
     283              :     unsigned _rule50,
     284              :     unsigned _castling,
     285              :     unsigned _ep,
     286              :     bool     _turn,
     287              :     unsigned *_results)
     288              : {
     289            0 :     if (_castling != 0)
     290              :         return TB_RESULT_FAILED;
     291            0 :     return tb_probe_root_impl(_white, _black, _kings, _queens, _rooks,
     292              :         _bishops, _knights, _pawns, _rule50, _ep, _turn, _results);
     293              : }
     294              : 
     295              : typedef uint16_t TbMove;
     296              : 
     297              : #define TB_MOVE_FROM(move)                                                 \
     298              :     (((move) >> 6) & 0x3F)
     299              : #define TB_MOVE_TO(move)                                                   \
     300              :     ((move) & 0x3F)
     301              : #define TB_MOVE_PROMOTES(move)                                             \
     302              :     (((move) >> 12) & 0x7)
     303              : 
     304              : struct TbRootMove {
     305              :   TbMove move;
     306              :   TbMove pv[TB_MAX_PLY];
     307              :   unsigned pvSize;
     308              :   int32_t tbScore, tbRank;
     309              : };
     310              : 
     311              : struct TbRootMoves {
     312              :   unsigned size;
     313              :   struct TbRootMove moves[TB_MAX_MOVES];
     314              : };
     315              : 
     316              : /*
     317              :  * Use the DTZ tables to rank and score all root moves.
     318              :  * INPUT: as for tb_probe_root
     319              :  * OUTPUT: TbRootMoves structure is filled in. This contains
     320              :  * an array of TbRootMove structures.
     321              :  * Each structure instance contains a rank, a score, and a
     322              :  * predicted principal variation.
     323              :  * RETURN VALUE:
     324              :  *   non-zero if ok, 0 means not all probes were successful
     325              :  *
     326              :  */
     327              : int tb_probe_root_dtz(
     328              :     uint64_t _white,
     329              :     uint64_t _black,
     330              :     uint64_t _kings,
     331              :     uint64_t _queens,
     332              :     uint64_t _rooks,
     333              :     uint64_t _bishops,
     334              :     uint64_t _knights,
     335              :     uint64_t _pawns,
     336              :     unsigned _rule50,
     337              :     unsigned _castling,
     338              :     unsigned _ep,
     339              :     bool     _turn,
     340              :     bool hasRepeated,
     341              :     bool useRule50,
     342              :     struct TbRootMoves *_results);
     343              : 
     344              : /*
     345              : // Use the WDL tables to rank and score all root moves.
     346              : // This is a fallback for the case that some or all DTZ tables are missing.
     347              :  * INPUT: as for tb_probe_root
     348              :  * OUTPUT: TbRootMoves structure is filled in. This contains
     349              :  * an array of TbRootMove structures.
     350              :  * Each structure instance contains a rank, a score, and a
     351              :  * predicted principal variation.
     352              :  * RETURN VALUE:
     353              :  *   non-zero if ok, 0 means not all probes were successful
     354              :  *
     355              :  */
     356              : int tb_probe_root_wdl(uint64_t _white,
     357              :     uint64_t _black,
     358              :     uint64_t _kings,
     359              :     uint64_t _queens,
     360              :     uint64_t _rooks,
     361              :     uint64_t _bishops,
     362              :     uint64_t _knights,
     363              :     uint64_t _pawns,
     364              :     unsigned _rule50,
     365              :     unsigned _castling,
     366              :     unsigned _ep,
     367              :     bool     _turn,
     368              :     bool useRule50,
     369              :     struct TbRootMoves *_results);
     370              : 
     371              : /****************************************************************************/
     372              : /* HELPER API                                                               */
     373              : /****************************************************************************/
     374              : 
     375              : /*
     376              :  * The HELPER API provides some useful additional functions.  It is optional
     377              :  * and can be disabled by defining TB_NO_HELPER_API.  Engines should disable
     378              :  * the HELPER API.
     379              :  */
     380              : 
     381              : #ifndef TB_NO_HELPER_API
     382              : 
     383              : extern unsigned tb_pop_count(uint64_t _bb);
     384              : extern unsigned tb_lsb(uint64_t _bb);
     385              : extern uint64_t tb_pop_lsb(uint64_t _bb);
     386              : extern uint64_t tb_king_attacks(unsigned _square);
     387              : extern uint64_t tb_queen_attacks(unsigned _square, uint64_t _occ);
     388              : extern uint64_t tb_rook_attacks(unsigned _square, uint64_t _occ);
     389              : extern uint64_t tb_bishop_attacks(unsigned _square, uint64_t _occ);
     390              : extern uint64_t tb_knight_attacks(unsigned _square);
     391              : extern uint64_t tb_pawn_attacks(unsigned _square, bool _color);
     392              : 
     393              : #endif
     394              : 
     395              : #ifdef __cplusplus
     396              : }
     397              : #endif
     398              : 
     399              : #endif
        

Generated by: LCOV version 2.0-1