mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
Add a Fathom Syzygy probe NIF (#29)
Vendor the MIT-licensed Fathom C source and compile it into a Rustler NIF (native/fathom_nif). The NIF exposes init/1 (point Fathom at a tablebase directory) and probe/1 (raw WDL/DTZ/move results for a FEN); probes are serialised because Fathom's root probe is not thread safe, and run as dirty NIFs. Add KRvK/KQvK 3-piece WDL+DTZ fixtures and an integration test proving a known KRvK probe returns Win, DTZ 23 and 15 moves.
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2018 Ronald de Man
|
||||
Copyright (c) 2015 basil00
|
||||
Copyright (c) 2016-2025 by Jon Dart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
+286
@@ -0,0 +1,286 @@
|
||||
#ifndef _STDENDIAN_H_
|
||||
#define _STDENDIAN_H_
|
||||
/* from https://gist.github.com/michaeljclark/3b4fd912f6fa8bb598b3 */
|
||||
/* modified to use functions not macros for bswap */
|
||||
/* and added a fix for Cygwin */
|
||||
/*
|
||||
* stdendian.h
|
||||
*
|
||||
* This header defines the following endian macros as defined here:
|
||||
* http://austingroupbugs.net/view.php?id=162
|
||||
*
|
||||
* BYTE_ORDER this macro shall have a value equal to one
|
||||
* of the *_ENDIAN macros in this header.
|
||||
* LITTLE_ENDIAN if BYTE_ORDER == LITTLE_ENDIAN, the host
|
||||
* byte order is from least significant to
|
||||
* most significant.
|
||||
* BIG_ENDIAN if BYTE_ORDER == BIG_ENDIAN, the host byte
|
||||
* order is from most significant to least
|
||||
* significant.
|
||||
*
|
||||
* The following are defined as macros:
|
||||
*
|
||||
* uint16_t bswap16(uint16_t x);
|
||||
* uint32_t bswap32(uint32_t x);
|
||||
* uint64_t bswap64(uint64_t x);
|
||||
|
||||
* uint16_t htobe16(uint16_t x);
|
||||
* uint16_t htole16(uint16_t x);
|
||||
* uint16_t be16toh(uint16_t x);
|
||||
* uint16_t le16toh(uint16_t x);
|
||||
*
|
||||
* uint32_t htobe32(uint32_t x);
|
||||
* uint32_t htole32(uint32_t x);
|
||||
* uint32_t be32toh(uint32_t x);
|
||||
* uint32_t le32toh(uint32_t x);
|
||||
*
|
||||
* uint64_t htobe64(uint64_t x);
|
||||
* uint64_t htole64(uint64_t x);
|
||||
* uint64_t be64toh(uint64_t x);
|
||||
* uint64_t le64toh(uint64_t x);
|
||||
*
|
||||
* The header defines the following macro for OpenCL compatibility
|
||||
* https://www.khronos.org/registry/cl/sdk/2.0/docs/man/xhtml/preprocessorDirectives.html
|
||||
*
|
||||
* __ENDIAN_LITTLE__ if BYTE_ORDER == LITTLE_ENDIAN then this
|
||||
* macro is present for OpenCL compatibility
|
||||
*
|
||||
* The implementation provides a uniform interface to endian macros using only
|
||||
* system headers on recent Linux, Darwin, FreeBSD, Solaris and Windows systems.
|
||||
*
|
||||
* This approach is intended to avoid the need for preflight configure scripts.
|
||||
* An alternative approach would be to test compiler CPU architecture marcros.
|
||||
*
|
||||
* This header has had *limited* testing on recent C11/C++11 compilers and is
|
||||
* based on the austin bug tracker interface, manpages, and headers present in
|
||||
* Linux, FreeBSD, Windows, Solaris and Darwin.
|
||||
*
|
||||
* The header uses __builtin_bswapXX intrinsic with GCC/Clang (__GNUC__) on
|
||||
* platforms that do not provide bswap16, bswap32, bswap64 (Darwin)
|
||||
*
|
||||
* Public Domain.
|
||||
*/
|
||||
|
||||
/* requires C11 or C++11 */
|
||||
#if defined (__cplusplus)
|
||||
#include <cstdint>
|
||||
#elif !defined (__OPENCL_VERSION__)
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
/* Linux / GLIBC */
|
||||
#if defined(__linux__) || defined(__GLIBC__) || defined(__CYGWIN__)
|
||||
#include <endian.h>
|
||||
#include <byteswap.h>
|
||||
#define __ENDIAN_DEFINED 1
|
||||
#define __BSWAP_DEFINED 1
|
||||
#define __HOSTSWAP_DEFINED 1
|
||||
// NDK defines _BYTE_ORDER etc
|
||||
#ifndef _BYTE_ORDER
|
||||
#define _BYTE_ORDER __BYTE_ORDER
|
||||
#define _LITTLE_ENDIAN __LITTLE_ENDIAN
|
||||
#define _BIG_ENDIAN __BIG_ENDIAN
|
||||
#endif
|
||||
#define bswap16(x) bswap_16(x)
|
||||
#define bswap32(x) bswap_32(x)
|
||||
#define bswap64(x) bswap_64(x)
|
||||
#endif /* __linux__ || __GLIBC__ */
|
||||
|
||||
/* BSD */
|
||||
#if defined(__FreeBSD__) || defined(__NetBSD__) || \
|
||||
defined(__DragonFly__) || defined(__OpenBSD__)
|
||||
#include <sys/endian.h>
|
||||
#define __ENDIAN_DEFINED 1
|
||||
#define __BSWAP_DEFINED 1
|
||||
#define __HOSTSWAP_DEFINED 1
|
||||
#endif /* BSD */
|
||||
|
||||
/* Solaris */
|
||||
#if defined (sun)
|
||||
#include <sys/isa_defs.h>
|
||||
/* sun headers don't set a value for _LITTLE_ENDIAN or _BIG_ENDIAN */
|
||||
#if defined(_LITTLE_ENDIAN)
|
||||
#undef _LITTLE_ENDIAN
|
||||
#define _LITTLE_ENDIAN 1234
|
||||
#define _BIG_ENDIAN 4321
|
||||
#define _BYTE_ORDER _LITTLE_ENDIAN
|
||||
#elif defined(_BIG_ENDIAN)
|
||||
#undef _BIG_ENDIAN
|
||||
#define _LITTLE_ENDIAN 1234
|
||||
#define _BIG_ENDIAN 4321
|
||||
#define _BYTE_ORDER _BIG_ENDIAN
|
||||
#endif
|
||||
#define __ENDIAN_DEFINED 1
|
||||
#endif /* sun */
|
||||
|
||||
/* Windows */
|
||||
/* assumes all Microsoft targets are little endian. */
|
||||
/* Emscripten (emcc) also currently assumes little endian. */
|
||||
#if defined(_WIN32) || defined(_MSC_VER) || defined(__EMSCRIPTEN__)
|
||||
#define _LITTLE_ENDIAN 1234
|
||||
#define _BIG_ENDIAN 4321
|
||||
#define _BYTE_ORDER _LITTLE_ENDIAN
|
||||
#define __ENDIAN_DEFINED 1
|
||||
#endif /* _MSC_VER */
|
||||
|
||||
/* OS X */
|
||||
#if defined(__APPLE__)
|
||||
#include <machine/endian.h>
|
||||
#define _BYTE_ORDER BYTE_ORDER
|
||||
#define _LITTLE_ENDIAN LITTLE_ENDIAN
|
||||
#define _BIG_ENDIAN BIG_ENDIAN
|
||||
#define __ENDIAN_DEFINED 1
|
||||
#endif /* __APPLE__ */
|
||||
|
||||
/* OpenCL */
|
||||
#if defined (__OPENCL_VERSION__)
|
||||
#define _LITTLE_ENDIAN 1234
|
||||
#define __BIG_ENDIAN 4321
|
||||
#if defined (__ENDIAN_LITTLE__)
|
||||
#define _BYTE_ORDER _LITTLE_ENDIAN
|
||||
#else
|
||||
#define _BYTE_ORDER _BIG_ENDIAN
|
||||
#endif
|
||||
#define bswap16(x) as_ushort(as_uchar2(ushort(x)).s1s0)
|
||||
#define bswap32(x) as_uint(as_uchar4(uint(x)).s3s2s1s0)
|
||||
#define bswap64(x) as_ulong(as_uchar8(ulong(x)).s7s6s5s4s3s2s1s0)
|
||||
#define __ENDIAN_DEFINED 1
|
||||
#define __BSWAP_DEFINED 1
|
||||
#endif
|
||||
|
||||
/* Unknown */
|
||||
#if !__ENDIAN_DEFINED
|
||||
#error Could not determine CPU byte order
|
||||
#endif
|
||||
|
||||
/* POSIX - http://austingroupbugs.net/view.php?id=162 */
|
||||
#ifndef BYTE_ORDER
|
||||
#define BYTE_ORDER _BYTE_ORDER
|
||||
#endif
|
||||
#ifndef LITTLE_ENDIAN
|
||||
#define LITTLE_ENDIAN _LITTLE_ENDIAN
|
||||
#endif
|
||||
#ifndef BIG_ENDIAN
|
||||
#define BIG_ENDIAN _BIG_ENDIAN
|
||||
#endif
|
||||
|
||||
/* OpenCL compatibility - define __ENDIAN_LITTLE__ on little endian systems */
|
||||
#if _BYTE_ORDER == _LITTLE_ENDIAN
|
||||
#if !defined (__ENDIAN_LITTLE__)
|
||||
#define __ENDIAN_LITTLE__ 1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Byte swap macros */
|
||||
#if !__BSWAP_DEFINED
|
||||
|
||||
#ifndef bswap16
|
||||
/* handle missing __builtin_bswap16 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52624 */
|
||||
#if defined __GNUC__
|
||||
#define bswap16(x) __builtin_bswap16(x)
|
||||
#else
|
||||
inline uint16_t bswap16(uint16_t x) {
|
||||
return (uint16_t)((((uint16_t) (x) & 0xff00) >> 8) | \
|
||||
(((uint16_t) (x) & 0x00ff) << 8));
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef bswap32
|
||||
#if defined __GNUC__
|
||||
#define bswap32(x) __builtin_bswap32(x)
|
||||
#else
|
||||
inline uint32_t bswap32(uint32_t x) {
|
||||
return (( x & 0xff000000) >> 24) | \
|
||||
(( x & 0x00ff0000) >> 8) | \
|
||||
(( x & 0x0000ff00) << 8) | \
|
||||
(( x & 0x000000ff) << 24);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef bswap64
|
||||
#if defined __GNUC__
|
||||
#define bswap64(x) __builtin_bswap64(x)
|
||||
#else
|
||||
inline uint64_t bswap64(uint64_t x) {
|
||||
return (( x & 0xff00000000000000ull) >> 56) | \
|
||||
(( x & 0x00ff000000000000ull) >> 40) | \
|
||||
(( x & 0x0000ff0000000000ull) >> 24) | \
|
||||
(( x & 0x000000ff00000000ull) >> 8) | \
|
||||
(( x & 0x00000000ff000000ull) << 8) | \
|
||||
(( x & 0x0000000000ff0000ull) << 24) | \
|
||||
(( x & 0x000000000000ff00ull) << 40) | \
|
||||
(( x & 0x00000000000000ffull) << 56);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/* Host swap macros */
|
||||
#ifndef __HOSTSWAP_DEFINED
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
#define htobe16(x) bswap16((x))
|
||||
#define htole16(x) ((uint16_t)(x))
|
||||
#define be16toh(x) bswap16((x))
|
||||
#define le16toh(x) ((uint16_t)(x))
|
||||
|
||||
#define htobe32(x) bswap32((x))
|
||||
#define htole32(x) ((uint32_t)(x))
|
||||
#define be32toh(x) bswap32((x))
|
||||
#define le32toh(x) ((uint32_t)(x))
|
||||
|
||||
#define htobe64(x) bswap64((x))
|
||||
#define htole64(x) ((uint64_t)(x))
|
||||
#define be64toh(x) bswap64((x))
|
||||
#define le64toh(x) ((uint64_t)(x))
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
#define htobe16(x) ((uint16_t)(x))
|
||||
#define htole16(x) bswap16((x))
|
||||
#define be16toh(x) ((uint16_t)(x))
|
||||
#define le16toh(x) bswap16((x))
|
||||
|
||||
#define htobe32(x) ((uint32_t)(x))
|
||||
#define htole32(x) bswap32((x))
|
||||
#define be32toh(x) ((uint32_t)(x))
|
||||
#define le64toh(x) bswap64((x))
|
||||
|
||||
#define htobe64(x) ((uint64_t)(x))
|
||||
#define htole64(x) bswap64((x))
|
||||
#define be64toh(x) ((uint64_t)(x))
|
||||
#define le32toh(x) bswap32((x))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdendian.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
printf("little endian\n");
|
||||
#endif
|
||||
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
printf("big endian\n");
|
||||
#endif
|
||||
|
||||
printf("bswap16(%04x) %04x\n", 0xf0e0, bswap16(0xf0e0));
|
||||
printf("htobe16(%04x) %04x\n", 0xf0e0, htobe16(0xf0e0));
|
||||
printf("htole16(%04x) %04x\n", 0xf0e0, htole16(0xf0e0));
|
||||
|
||||
printf("bswap32(%08x) %08x\n", 0xf0e0d0c0, bswap32(0xf0e0d0c0));
|
||||
printf("htobe32(%08x) %08x\n", 0xf0e0d0c0, htobe32(0xf0e0d0c0));
|
||||
printf("htole32(%08x) %08x\n", 0xf0e0d0c0, htole32(0xf0e0d0c0));
|
||||
|
||||
printf("bswap64(%016llx) %016llx\n", 0xf0e0d0c0b0a09080ULL, bswap64(0xf0e0d0c0b0a09080ULL));
|
||||
printf("htobe64(%016llx) %016llx\n", 0xf0e0d0c0b0a09080ULL, htobe64(0xf0e0d0c0b0a09080ULL));
|
||||
printf("htole64(%016llx) %016llx\n", 0xf0e0d0c0b0a09080ULL, htole64(0xf0e0d0c0b0a09080ULL));
|
||||
}
|
||||
|
||||
*/
|
||||
#endif
|
||||
+1050
File diff suppressed because it is too large
Load Diff
+150
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* tbconfig.h
|
||||
* (C) 2015 basil, all rights reserved,
|
||||
* Modifications Copyright 2016-2017 Jon Dart
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef TBCONFIG_H
|
||||
#define TBCONFIG_H
|
||||
|
||||
/****************************************************************************/
|
||||
/* BUILD CONFIG: */
|
||||
/****************************************************************************/
|
||||
|
||||
/*
|
||||
* Define TB_CUSTOM_POP_COUNT to override the internal popcount
|
||||
* implementation. To do this supply a macro or function definition
|
||||
* here:
|
||||
*/
|
||||
/* #define TB_CUSTOM_POP_COUNT(x) <DEFINITION> */
|
||||
|
||||
/*
|
||||
* Define TB_CUSTOM_LSB to override the internal lsb
|
||||
* implementation. To do this supply a macro or function definition
|
||||
* here:
|
||||
*/
|
||||
/* #define TB_CUSTOM_LSB(x) <DEFINITION> */
|
||||
|
||||
/*
|
||||
* Define TB_NO_STDINT if you do not want to use <stdint.h> or it is not
|
||||
* available.
|
||||
*/
|
||||
/* #define TB_NO_STDINT */
|
||||
|
||||
/*
|
||||
* Define TB_NO_STDBOOL if you do not want to use <stdbool.h> or it is not
|
||||
* available or unnecessary (e.g. C++).
|
||||
*/
|
||||
/* #define TB_NO_STDBOOL */
|
||||
|
||||
/*
|
||||
* Define TB_NO_THREADS if your program is not multi-threaded.
|
||||
*/
|
||||
/* #define TB_NO_THREADS */
|
||||
|
||||
/*
|
||||
* Define TB_NO_HELPER_API if you do not need the helper API.
|
||||
*/
|
||||
/* #define TB_NO_HELPER_API */
|
||||
|
||||
/*
|
||||
* Define TB_NO_HW_POP_COUNT if there is no hardware popcount instruction.
|
||||
*
|
||||
* Note: if defined, TB_CUSTOM_POP_COUNT is always used in preference
|
||||
* to any built-in popcount functions.
|
||||
*
|
||||
* If no custom popcount function is defined, and if the following
|
||||
* define is not set, the code will attempt to use an available hardware
|
||||
* popcnt (currently supported on x86_64 architecture only) and otherwise
|
||||
* will fall back to a software implementation.
|
||||
*/
|
||||
/* #define TB_NO_HW_POP_COUNT */
|
||||
|
||||
/***************************************************************************/
|
||||
/* SCORING CONSTANTS */
|
||||
/***************************************************************************/
|
||||
/*
|
||||
* Fathom can produce scores for tablebase moves. These depend on the
|
||||
* value of a pawn, and the magnitude of mate scores. The following
|
||||
* constants are representative values but will likely need
|
||||
* modification to adapt to an engine's own internal score values.
|
||||
*/
|
||||
#define TB_VALUE_PAWN 100 /* value of pawn in endgame */
|
||||
#define TB_VALUE_MATE 32000
|
||||
#define TB_VALUE_INFINITE 32767 /* value above all normal score values */
|
||||
#define TB_VALUE_DRAW 0
|
||||
#define TB_MAX_MATE_PLY 255
|
||||
|
||||
/***************************************************************************/
|
||||
/* ENGINE INTEGRATION CONFIG */
|
||||
/***************************************************************************/
|
||||
|
||||
/*
|
||||
* If you are integrating tbprobe into an engine, you can replace some of
|
||||
* tbprobe's built-in functionality with that already provided by the engine.
|
||||
* This is OPTIONAL. If no definition are provided then tbprobe will use its
|
||||
* own internal defaults. That said, for engines it is generally a good idea
|
||||
* to avoid redundancy.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Define TB_KING_ATTACKS(square) to return the king attacks bitboard for a
|
||||
* king at `square'.
|
||||
*/
|
||||
/* #define TB_KING_ATTACKS(square) <DEFINITION> */
|
||||
|
||||
/*
|
||||
* Define TB_KNIGHT_ATTACKS(square) to return the knight attacks bitboard for
|
||||
* a knight at `square'.
|
||||
*/
|
||||
/* #define TB_KNIGHT_ATTACKS(square) <DEFINITION> */
|
||||
|
||||
/*
|
||||
* Define TB_ROOK_ATTACKS(square, occ) to return the rook attacks bitboard
|
||||
* for a rook at `square' assuming the given `occ' occupancy bitboard.
|
||||
*/
|
||||
/* #define TB_ROOK_ATTACKS(square, occ) <DEFINITION> */
|
||||
|
||||
/*
|
||||
* Define TB_BISHOP_ATTACKS(square, occ) to return the bishop attacks bitboard
|
||||
* for a bishop at `square' assuming the given `occ' occupancy bitboard.
|
||||
*/
|
||||
/* #define TB_BISHOP_ATTACKS(square, occ) <DEFINITION> */
|
||||
|
||||
/*
|
||||
* Define TB_QUEEN_ATTACKS(square, occ) to return the queen attacks bitboard
|
||||
* for a queen at `square' assuming the given `occ' occupancy bitboard.
|
||||
* NOTE: If no definition is provided then tbprobe will use:
|
||||
* TB_ROOK_ATTACKS(square, occ) | TB_BISHOP_ATTACKS(square, occ)
|
||||
*/
|
||||
/* #define TB_QUEEN_ATTACKS(square, occ) <DEFINITION> */
|
||||
|
||||
/*
|
||||
* Define TB_PAWN_ATTACKS(square, color) to return the pawn attacks bitboard
|
||||
* for a `color' pawn at `square'.
|
||||
* NOTE: This definition must work for pawns on ranks 1 and 8. For example,
|
||||
* a white pawn on e1 attacks d2 and f2. A black pawn on e1 attacks
|
||||
* nothing. Etc.
|
||||
* NOTE: This definition must not include en passant captures.
|
||||
*/
|
||||
/* #define TB_PAWN_ATTACKS(square, color) <DEFINITION> */
|
||||
|
||||
#endif
|
||||
+2715
File diff suppressed because it is too large
Load Diff
+399
@@ -0,0 +1,399 @@
|
||||
/*
|
||||
* tbprobe.h
|
||||
* (C) 2015 basil, all rights reserved,
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef TBPROBE_H
|
||||
#define TBPROBE_H
|
||||
|
||||
#include <tbconfig.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#ifndef TB_NO_STDINT
|
||||
#include <stdint.h>
|
||||
#else
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef unsigned uint32_t;
|
||||
typedef long long unsigned uint64_t;
|
||||
typedef char int8_t;
|
||||
typedef short int16_t;
|
||||
typedef int int32_t;
|
||||
typedef long long int64_t;
|
||||
#endif
|
||||
|
||||
#ifndef TB_NO_STDBOOL
|
||||
#include <stdbool.h>
|
||||
#else
|
||||
#ifndef __cplusplus
|
||||
typedef uint8_t bool;
|
||||
#define true 1
|
||||
#define false 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Internal definitions. Do not call these functions directly.
|
||||
*/
|
||||
extern bool tb_init_impl(const char *_path);
|
||||
extern unsigned tb_probe_wdl_impl(
|
||||
uint64_t _white,
|
||||
uint64_t _black,
|
||||
uint64_t _kings,
|
||||
uint64_t _queens,
|
||||
uint64_t _rooks,
|
||||
uint64_t _bishops,
|
||||
uint64_t _knights,
|
||||
uint64_t _pawns,
|
||||
unsigned _ep,
|
||||
bool _turn);
|
||||
extern unsigned tb_probe_root_impl(
|
||||
uint64_t _white,
|
||||
uint64_t _black,
|
||||
uint64_t _kings,
|
||||
uint64_t _queens,
|
||||
uint64_t _rooks,
|
||||
uint64_t _bishops,
|
||||
uint64_t _knights,
|
||||
uint64_t _pawns,
|
||||
unsigned _rule50,
|
||||
unsigned _ep,
|
||||
bool _turn,
|
||||
unsigned *_results);
|
||||
|
||||
/****************************************************************************/
|
||||
/* MAIN API */
|
||||
/****************************************************************************/
|
||||
|
||||
#define TB_MAX_MOVES (192+1)
|
||||
#define TB_MAX_CAPTURES 64
|
||||
#define TB_MAX_PLY 256
|
||||
#define TB_CASTLING_K 0x1 /* White king-side. */
|
||||
#define TB_CASTLING_Q 0x2 /* White queen-side. */
|
||||
#define TB_CASTLING_k 0x4 /* Black king-side. */
|
||||
#define TB_CASTLING_q 0x8 /* Black queen-side. */
|
||||
|
||||
#define TB_LOSS 0 /* LOSS */
|
||||
#define TB_BLESSED_LOSS 1 /* LOSS but 50-move draw */
|
||||
#define TB_DRAW 2 /* DRAW */
|
||||
#define TB_CURSED_WIN 3 /* WIN but 50-move draw */
|
||||
#define TB_WIN 4 /* WIN */
|
||||
|
||||
#define TB_PROMOTES_NONE 0
|
||||
#define TB_PROMOTES_QUEEN 1
|
||||
#define TB_PROMOTES_ROOK 2
|
||||
#define TB_PROMOTES_BISHOP 3
|
||||
#define TB_PROMOTES_KNIGHT 4
|
||||
|
||||
#define TB_RESULT_WDL_MASK 0x0000000F
|
||||
#define TB_RESULT_TO_MASK 0x000003F0
|
||||
#define TB_RESULT_FROM_MASK 0x0000FC00
|
||||
#define TB_RESULT_PROMOTES_MASK 0x00070000
|
||||
#define TB_RESULT_EP_MASK 0x00080000
|
||||
#define TB_RESULT_DTZ_MASK 0xFFF00000
|
||||
#define TB_RESULT_WDL_SHIFT 0
|
||||
#define TB_RESULT_TO_SHIFT 4
|
||||
#define TB_RESULT_FROM_SHIFT 10
|
||||
#define TB_RESULT_PROMOTES_SHIFT 16
|
||||
#define TB_RESULT_EP_SHIFT 19
|
||||
#define TB_RESULT_DTZ_SHIFT 20
|
||||
|
||||
#define TB_GET_WDL(_res) \
|
||||
(((_res) & TB_RESULT_WDL_MASK) >> TB_RESULT_WDL_SHIFT)
|
||||
#define TB_GET_TO(_res) \
|
||||
(((_res) & TB_RESULT_TO_MASK) >> TB_RESULT_TO_SHIFT)
|
||||
#define TB_GET_FROM(_res) \
|
||||
(((_res) & TB_RESULT_FROM_MASK) >> TB_RESULT_FROM_SHIFT)
|
||||
#define TB_GET_PROMOTES(_res) \
|
||||
(((_res) & TB_RESULT_PROMOTES_MASK) >> TB_RESULT_PROMOTES_SHIFT)
|
||||
#define TB_GET_EP(_res) \
|
||||
(((_res) & TB_RESULT_EP_MASK) >> TB_RESULT_EP_SHIFT)
|
||||
#define TB_GET_DTZ(_res) \
|
||||
(((_res) & TB_RESULT_DTZ_MASK) >> TB_RESULT_DTZ_SHIFT)
|
||||
|
||||
#define TB_SET_WDL(_res, _wdl) \
|
||||
(((_res) & ~TB_RESULT_WDL_MASK) | \
|
||||
(((_wdl) << TB_RESULT_WDL_SHIFT) & TB_RESULT_WDL_MASK))
|
||||
#define TB_SET_TO(_res, _to) \
|
||||
(((_res) & ~TB_RESULT_TO_MASK) | \
|
||||
(((_to) << TB_RESULT_TO_SHIFT) & TB_RESULT_TO_MASK))
|
||||
#define TB_SET_FROM(_res, _from) \
|
||||
(((_res) & ~TB_RESULT_FROM_MASK) | \
|
||||
(((_from) << TB_RESULT_FROM_SHIFT) & TB_RESULT_FROM_MASK))
|
||||
#define TB_SET_PROMOTES(_res, _promotes) \
|
||||
(((_res) & ~TB_RESULT_PROMOTES_MASK) | \
|
||||
(((_promotes) << TB_RESULT_PROMOTES_SHIFT) & TB_RESULT_PROMOTES_MASK))
|
||||
#define TB_SET_EP(_res, _ep) \
|
||||
(((_res) & ~TB_RESULT_EP_MASK) | \
|
||||
(((_ep) << TB_RESULT_EP_SHIFT) & TB_RESULT_EP_MASK))
|
||||
#define TB_SET_DTZ(_res, _dtz) \
|
||||
(((_res) & ~TB_RESULT_DTZ_MASK) | \
|
||||
(((_dtz) << TB_RESULT_DTZ_SHIFT) & TB_RESULT_DTZ_MASK))
|
||||
|
||||
#define TB_RESULT_CHECKMATE TB_SET_WDL(0, TB_WIN)
|
||||
#define TB_RESULT_STALEMATE TB_SET_WDL(0, TB_DRAW)
|
||||
#define TB_RESULT_FAILED 0xFFFFFFFF
|
||||
|
||||
/*
|
||||
* The tablebase can be probed for any position where #pieces <= TB_LARGEST.
|
||||
*/
|
||||
extern unsigned TB_LARGEST;
|
||||
|
||||
/*
|
||||
* Initialize the tablebase.
|
||||
*
|
||||
* PARAMETERS:
|
||||
* - path:
|
||||
* The tablebase PATH string.
|
||||
*
|
||||
* RETURN:
|
||||
* - true=success, false=failed. The TB_LARGEST global will also be
|
||||
* initialized. If no tablebase files are found, then `true' is returned
|
||||
* and TB_LARGEST is set to zero.
|
||||
*/
|
||||
bool tb_init(const char *_path);
|
||||
|
||||
/*
|
||||
* Free any resources allocated by tb_init
|
||||
*/
|
||||
void tb_free(void);
|
||||
|
||||
/*
|
||||
* Probe the Win-Draw-Loss (WDL) table.
|
||||
*
|
||||
* PARAMETERS:
|
||||
* - white, black, kings, queens, rooks, bishops, knights, pawns:
|
||||
* The current position (bitboards).
|
||||
* - rule50:
|
||||
* The 50-move half-move clock.
|
||||
* - castling:
|
||||
* Castling rights. Set to zero if no castling is possible.
|
||||
* - ep:
|
||||
* The en passant square (if exists). Set to zero if there is no en passant
|
||||
* square.
|
||||
* - turn:
|
||||
* true=white, false=black
|
||||
*
|
||||
* RETURN:
|
||||
* - One of {TB_LOSS, TB_BLESSED_LOSS, TB_DRAW, TB_CURSED_WIN, TB_WIN}.
|
||||
* Otherwise returns TB_RESULT_FAILED if the probe failed.
|
||||
*
|
||||
* NOTES:
|
||||
* - Engines should use this function during search.
|
||||
* - This function is thread safe assuming TB_NO_THREADS is disabled.
|
||||
*/
|
||||
static inline unsigned tb_probe_wdl(
|
||||
uint64_t _white,
|
||||
uint64_t _black,
|
||||
uint64_t _kings,
|
||||
uint64_t _queens,
|
||||
uint64_t _rooks,
|
||||
uint64_t _bishops,
|
||||
uint64_t _knights,
|
||||
uint64_t _pawns,
|
||||
unsigned _rule50,
|
||||
unsigned _castling,
|
||||
unsigned _ep,
|
||||
bool _turn)
|
||||
{
|
||||
if (_castling != 0)
|
||||
return TB_RESULT_FAILED;
|
||||
if (_rule50 != 0)
|
||||
return TB_RESULT_FAILED;
|
||||
return tb_probe_wdl_impl(_white, _black, _kings, _queens, _rooks,
|
||||
_bishops, _knights, _pawns, _ep, _turn);
|
||||
}
|
||||
|
||||
/*
|
||||
* Probe the Distance-To-Zero (DTZ) table.
|
||||
*
|
||||
* PARAMETERS:
|
||||
* - white, black, kings, queens, rooks, bishops, knights, pawns:
|
||||
* The current position (bitboards).
|
||||
* - rule50:
|
||||
* The 50-move half-move clock.
|
||||
* - castling:
|
||||
* Castling rights. Set to zero if no castling is possible.
|
||||
* - ep:
|
||||
* The en passant square (if exists). Set to zero if there is no en passant
|
||||
* square.
|
||||
* - turn:
|
||||
* true=white, false=black
|
||||
* - results (OPTIONAL):
|
||||
* Alternative results, one for each possible legal move. The passed array
|
||||
* must be TB_MAX_MOVES in size.
|
||||
* If alternative results are not desired then set results=NULL.
|
||||
*
|
||||
* RETURN:
|
||||
* - A TB_RESULT value comprising:
|
||||
* 1) The WDL value (TB_GET_WDL)
|
||||
* 2) The suggested move (TB_GET_FROM, TB_GET_TO, TB_GET_PROMOTES, TB_GET_EP)
|
||||
* 3) The DTZ value (TB_GET_DTZ)
|
||||
* The suggested move is guaranteed to preserved the WDL value.
|
||||
*
|
||||
* Otherwise:
|
||||
* 1) TB_RESULT_STALEMATE is returned if the position is in stalemate.
|
||||
* 2) TB_RESULT_CHECKMATE is returned if the position is in checkmate.
|
||||
* 3) TB_RESULT_FAILED is returned if the probe failed.
|
||||
*
|
||||
* If results!=NULL, then a TB_RESULT for each legal move will be generated
|
||||
* and stored in the results array. The results array will be terminated
|
||||
* by TB_RESULT_FAILED.
|
||||
*
|
||||
* NOTES:
|
||||
* - Engines can use this function to probe at the root. This function should
|
||||
* not be used during search.
|
||||
* - DTZ tablebases can suggest unnatural moves, especially for losing
|
||||
* positions. Engines may prefer to traditional search combined with WDL
|
||||
* move filtering using the alternative results array.
|
||||
* - This function is NOT thread safe. For engines this function should only
|
||||
* be called once at the root per search.
|
||||
*/
|
||||
static inline unsigned tb_probe_root(
|
||||
uint64_t _white,
|
||||
uint64_t _black,
|
||||
uint64_t _kings,
|
||||
uint64_t _queens,
|
||||
uint64_t _rooks,
|
||||
uint64_t _bishops,
|
||||
uint64_t _knights,
|
||||
uint64_t _pawns,
|
||||
unsigned _rule50,
|
||||
unsigned _castling,
|
||||
unsigned _ep,
|
||||
bool _turn,
|
||||
unsigned *_results)
|
||||
{
|
||||
if (_castling != 0)
|
||||
return TB_RESULT_FAILED;
|
||||
return tb_probe_root_impl(_white, _black, _kings, _queens, _rooks,
|
||||
_bishops, _knights, _pawns, _rule50, _ep, _turn, _results);
|
||||
}
|
||||
|
||||
typedef uint16_t TbMove;
|
||||
|
||||
#define TB_MOVE_FROM(move) \
|
||||
(((move) >> 6) & 0x3F)
|
||||
#define TB_MOVE_TO(move) \
|
||||
((move) & 0x3F)
|
||||
#define TB_MOVE_PROMOTES(move) \
|
||||
(((move) >> 12) & 0x7)
|
||||
|
||||
struct TbRootMove {
|
||||
TbMove move;
|
||||
TbMove pv[TB_MAX_PLY];
|
||||
unsigned pvSize;
|
||||
int32_t tbScore, tbRank;
|
||||
};
|
||||
|
||||
struct TbRootMoves {
|
||||
unsigned size;
|
||||
struct TbRootMove moves[TB_MAX_MOVES];
|
||||
};
|
||||
|
||||
/*
|
||||
* Use the DTZ tables to rank and score all root moves.
|
||||
* INPUT: as for tb_probe_root
|
||||
* OUTPUT: TbRootMoves structure is filled in. This contains
|
||||
* an array of TbRootMove structures.
|
||||
* Each structure instance contains a rank, a score, and a
|
||||
* predicted principal variation.
|
||||
* RETURN VALUE:
|
||||
* non-zero if ok, 0 means not all probes were successful
|
||||
*
|
||||
*/
|
||||
int tb_probe_root_dtz(
|
||||
uint64_t _white,
|
||||
uint64_t _black,
|
||||
uint64_t _kings,
|
||||
uint64_t _queens,
|
||||
uint64_t _rooks,
|
||||
uint64_t _bishops,
|
||||
uint64_t _knights,
|
||||
uint64_t _pawns,
|
||||
unsigned _rule50,
|
||||
unsigned _castling,
|
||||
unsigned _ep,
|
||||
bool _turn,
|
||||
bool hasRepeated,
|
||||
bool useRule50,
|
||||
struct TbRootMoves *_results);
|
||||
|
||||
/*
|
||||
// Use the WDL tables to rank and score all root moves.
|
||||
// This is a fallback for the case that some or all DTZ tables are missing.
|
||||
* INPUT: as for tb_probe_root
|
||||
* OUTPUT: TbRootMoves structure is filled in. This contains
|
||||
* an array of TbRootMove structures.
|
||||
* Each structure instance contains a rank, a score, and a
|
||||
* predicted principal variation.
|
||||
* RETURN VALUE:
|
||||
* non-zero if ok, 0 means not all probes were successful
|
||||
*
|
||||
*/
|
||||
int tb_probe_root_wdl(uint64_t _white,
|
||||
uint64_t _black,
|
||||
uint64_t _kings,
|
||||
uint64_t _queens,
|
||||
uint64_t _rooks,
|
||||
uint64_t _bishops,
|
||||
uint64_t _knights,
|
||||
uint64_t _pawns,
|
||||
unsigned _rule50,
|
||||
unsigned _castling,
|
||||
unsigned _ep,
|
||||
bool _turn,
|
||||
bool useRule50,
|
||||
struct TbRootMoves *_results);
|
||||
|
||||
/****************************************************************************/
|
||||
/* HELPER API */
|
||||
/****************************************************************************/
|
||||
|
||||
/*
|
||||
* The HELPER API provides some useful additional functions. It is optional
|
||||
* and can be disabled by defining TB_NO_HELPER_API. Engines should disable
|
||||
* the HELPER API.
|
||||
*/
|
||||
|
||||
#ifndef TB_NO_HELPER_API
|
||||
|
||||
extern unsigned tb_pop_count(uint64_t _bb);
|
||||
extern unsigned tb_lsb(uint64_t _bb);
|
||||
extern uint64_t tb_pop_lsb(uint64_t _bb);
|
||||
extern uint64_t tb_king_attacks(unsigned _square);
|
||||
extern uint64_t tb_queen_attacks(unsigned _square, uint64_t _occ);
|
||||
extern uint64_t tb_rook_attacks(unsigned _square, uint64_t _occ);
|
||||
extern uint64_t tb_bishop_attacks(unsigned _square, uint64_t _occ);
|
||||
extern uint64_t tb_knight_attacks(unsigned _square);
|
||||
extern uint64_t tb_pawn_attacks(unsigned _square, bool _color);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user