Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for handing of popcnt and for compilation on non-x86 architectures #12

Open
wants to merge 73 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
00f0441
Always use TB_CUSTOM_POP_COUNT if defined. If not defined and
jdart1 Jul 14, 2016
358dbc1
Fix issues found by Coverity Scan:
jdart1 Jan 16, 2017
9d26101
Set precomp, data, and mapping fields to NULL after free.
jdart1 Jan 21, 2017
237e1ab
Fix compiler warnings. Fix logic in sanity check in tb_init_impl.
jdart1 Jan 21, 2017
085ff59
Make oppdiag array explicitly signed (fixes compile issue with clang …
jdart1 Apr 17, 2017
6998b4b
Use atomic variable instead of explicit memory barrier if compiling
jdart1 May 15, 2017
64685b5
Use a define to select use of atomic variables. Don't rely on
jdart1 May 15, 2017
980a993
Add check in is_legal for king bitmap empty.
jdart1 May 29, 2017
7f51a4b
Use angle-bracket include for tbconfig.h. This facilitates
jdart1 Aug 20, 2017
0563ea2
Don't make locking code depend on preprocessor variable TB_HAVE_THREADS:
jdart1 Mar 24, 2018
be18005
Don't include lock code if TB_NO_THREADS is defined.
jdart1 Mar 25, 2018
46d711b
Fix asserts in encode_piece (assert was tripping on 6-man tbs: report…
jdart1 Jul 19, 2018
7fc1cde
add std namespace to atomic declarations
jdart1 Nov 25, 2018
f7bbe69
Remove size limitation on file paths.
jdart1 Nov 26, 2018
f333364
update copyrights
jdart1 Nov 26, 2018
d9a12a9
Consistently use TB_USE_ATOMIC define to select usage of atomic varia…
jdart1 Nov 28, 2018
59f96c0
Fix issue 22 in basil00/Fathom/: properly disambiguate moves in
jdart1 Dec 9, 2018
4a4b403
Fix compilation when TB_CUSTOM_LSB is not defined. The previous
jdart1 Jan 21, 2019
0439ca1
Remove use of strdup (not defined in C99). Update copyright.
jdart1 Jan 21, 2019
9f93dfb
remove reference to binaries
jdart1 Jan 26, 2019
c6ff189
first cut at 7man support (from Cfish)
jdart1 Mar 30, 2019
e0bfd48
Fix WDL value when a mate in 1 is possible at the 50-move threshold
jdart1 Apr 17, 2019
4bad501
Bug fixes and code cleanup. Changes include:
jdart1 Apr 21, 2019
f0171d7
remove trailing whitespace
jdart1 Apr 22, 2019
8ab09a8
Fix test for KvK. Fix a signed/unsigned compare warning.
jdart1 Apr 23, 2019
19298c0
optimize by default again
jdart1 Apr 23, 2019
e8b79f7
Fixes for C++ and Windows/MSVC compilation.
jdart1 Apr 23, 2019
965fc71
Fix gcc warnings. Return error if castling status != 0.
jdart1 Apr 23, 2019
2d25890
Fix Windows error reporting: use correct printf specifier, call GetLa…
jdart1 Apr 23, 2019
be1f8f7
Merge branch '7man'
jdart1 Apr 24, 2019
85b96e1
update README
jdart1 May 1, 2019
ec24b3f
small README fix
jdart1 May 1, 2019
d9d8c0a
add some asserts
jdart1 Jul 28, 2019
a330188
Fix mingw64 warnings (reported by Vivien Clauzon).
jdart1 Jul 28, 2019
97fb5ca
minor README changes
jdart1 Aug 12, 2019
2c8c198
Place PieceType and other definitions within an anonymous C++ namespace
jdart1 Jun 25, 2020
6126bcd
fix some warnings
jdart1 Jul 5, 2020
9f011e4
support MSVC compilation in Unicode mode (-D_UNICODE).
jdart1 Jul 5, 2020
401f720
Pass in correct length of Unicode buffer to mbstowcs_s
jdart1 Jul 10, 2020
4c07922
some edits to history/credits section. Add some further advice
jdart1 Aug 22, 2020
da175ac
Fix typo. Add mention that chess code was MIT-licensed.
jdart1 Aug 22, 2020
44234cd
restore original copyright from Ronald de Man from source tbprobe.c
jdart1 Aug 22, 2020
c6cf6e8
Bug fix: ensure TB_LARGEST is set zero if Fathom is re-initialized with
jdart1 Aug 22, 2020
b2e158a
update copyrights in LICENSE file
jdart1 Dec 22, 2020
85b6968
support Emscripten (emcc) compile
jdart1 Mar 24, 2021
8d94ef2
mark unreferenced functions as unused, if using C++17 or above
jdart1 Sep 12, 2021
6a54366
Fix false munmap error
Tearth Aug 23, 2022
b9672ac
Merge pull request #4 from Tearth/munmap
jdart1 Aug 28, 2022
c135b09
Add option to use __builtin_popcount, as suggested in PR#5.
jdart1 Oct 23, 2022
0eb8c27
Fix mapping offset for wide dtz tables.
Matthies Oct 24, 2022
fcbcb50
Murphys law strikes again, I edited the wrong branch. This fixes it.
Matthies Oct 24, 2022
fb9b8fe
Merge pull request #6 from Matthies/master
jdart1 Oct 30, 2022
61327b5
Fix the compilation for C++20
srogatch Nov 26, 2022
c3e6e3f
Merge pull request #7 from srogatch/users/srogatch/fix/C++20
jdart1 Nov 26, 2022
8e9fd4d
Change sprintf to snprintf (fixes clang compiler warnings). Update co…
jdart1 Dec 28, 2022
03882f2
Use madvise to avoid prefetch from tb memory-mapped file region.
jdart1 Dec 29, 2022
a817a9b
fix a shadow warning
jdart1 Jun 10, 2023
294bf60
update copyright dates
jdart1 Aug 13, 2023
aeb9d69
Update README to reference LICENSE file.
jdart1 Aug 13, 2023
da00c95
Fix C++20 warning - don't call init to initialize atomic variables, u…
jdart1 Apr 5, 2024
0c62f4f
use more portable initialization for ready array
jdart1 Apr 10, 2024
9cda72d
call atomic_init for C++ versions below 20
jdart1 Apr 11, 2024
fcd8f26
Add top level Makefile for compiling a shared object
spinojara Apr 25, 2024
cb805af
Update Makefile
spinojara Apr 27, 2024
1b914d6
Add build instructions
spinojara Apr 27, 2024
de5cbdd
Fix typo
spinojara Apr 28, 2024
6063d5a
Merge pull request #8 from Spinojara/master
jdart1 Apr 28, 2024
15d669f
Fix install bug if $(DESTDIR)$(INCDIR) does not exist
spinojara Aug 6, 2024
cad52ba
Merge pull request #9 from spinojara/master
jdart1 Aug 6, 2024
67d7ae7
Correct statement about thread safety for probe_root* functions.
jdart1 Dec 5, 2024
a56c63e
Fix race condition caused by init_table failure
spinojara Dec 5, 2024
6abe461
make probe_table static
jdart1 Dec 8, 2024
b54fe93
Merge pull request #11 from spinojara/master
jdart1 Dec 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
The MIT License (MIT)

Copyright (c) 2013-2018 Ronald de Man
Copyright (c) 2015 basil00
Copyright (c) 2016-2024 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
Expand Down
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
MKDIR_P = mkdir -p
RM = rm
INSTALL = install

CC ?= gcc
CFLAGS = -std=gnu99 -O2 -Wall -Wshadow -Isrc
LDFLAGS = $(CFLAGS)

PREFIX = /usr/local
LIBDIR = $(PREFIX)/lib64
INCDIR = $(PREFIX)/include

libfathom.so: obj/tbprobe.o
$(CC) $(LDFLAGS) -shared $^ -o $@

obj/tbprobe.o: src/tbprobe.c src/tbchess.c src/stdendian.h src/tbprobe.h
@$(MKDIR_P) obj
$(CC) $(CFLAGS) -fPIC -c $< -o $@

clean:
$(RM) -f libfathom.so
$(RM) -rf obj

install: libfathom.so
$(MKDIR_P) $(DESTDIR)$(LIBDIR)
$(MKDIR_P) $(DESTDIR)$(INCDIR)
$(INSTALL) -m 0644 libfathom.so $(DESTDIR)$(LIBDIR)
$(INSTALL) -m 0644 src/{stdendian,tbconfig,tbprobe}.h $(DESTDIR)$(INCDIR)

uninstall:
$(RM) -f $(DESTDIR)$(LIBDIR)/libfathom.so
$(RM) -f $(DESTDIR)$(INCDIR)/{stdendian,tbconfig,tbprobe}.h

.PHONY: clean install uninstall
168 changes: 107 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ Fathom is a stand-alone Syzygy tablebase probing tool. The aims of Fathom
are:

* To make it easy to integrate the Syzygy tablebases into existing chess
engines;
engines
* To make it easy to create stand-alone applications that use the Syzygy
tablebases;
tablebases

Fathom is compilable under either C99 or C++ and supports a variety of
platforms, including at least Windows, Linux, and MacOS.

Building the library
--------------------
It is possible to build and install Fathom as a shared library on Unix-like
systems. Simply run the commands

make
make install

in the top level directory.

Tool
----
Expand All @@ -26,79 +39,112 @@ The tool will print out a PGN representation of the probe result, including:
* WinningMoves: The list of all winning moves
* DrawingMoves: The list of all drawing moves
* LosingMoves: The list of all losing moves
* A pseudo "principle variation" of Syzygy vs. Syzygy for the input position.
* A pseudo "principal variation" of Syzygy vs. Syzygy for the input position.

For more information, run the following command:

fathom --help

Pre-compiled versions of `fathom` (for all platforms) are available from here:

* https://github.com/basil00/Fathom/releases

Programming API
---------------

Fathom provides a simple API. There are three main function calls:
Fathom provides a simple API. Following are the main function calls:

* `tb_init` initializes the tablebase
* `tb_probe_wdl` probes the Win-Draw-Loss (WDL) table for a given position
* `tb_init` initializes the tablebases.
* `tb_free` releases any resources allocated by Fathom.
* `tb_probe_wdl` probes the Win-Draw-Loss (WDL) table for a given position.
* `tb_probe_root` probes the Distance-To-Zero (DTZ) table for the given
position.

All of the API functions use basic integer types, i.e. there is no need to
create and initialize data-structures. Fathom does not require the callee
to provide any additional functionality (e.g. move generation) unlike the
traditional `tbprobe` code. However, chess engines can opt to replace some
of the functionality of Fathom for better performance (see below).

Chess Engines
position. It returns a recommended move, and also a list of unsigned
integers, each one encoding a possible move and its DTZ and WDL values.
* `tb_probe_root_dtz` probes the Distance-To-Zero (DTZ) at the root position.
It returns a score and a rank for each possible move.
* `tb_probe_root_wdl` probes the Win-Draw-Loss (WDL) at the root position.
it returns a score and a rank for each possible move.

Fathom does not require the callee to provide any additional functionality
(e.g. move generation). A simple set of chess-related functions including move
generation is provided in file `tbchess.c`. However, chess engines can opt to
replace some of this functionality for better performance (see below).

Chess engines
-------------

Chess engines can be `tb_probe_wdl` to get the WDL value during search. The
`tb_probe_root` functional can be used to help pick the best move at the root.
Note that `tb_probe_root` is slower and therefore should only be used at the
root.

Chess engines can opt for a tighter integration of Fathom by configuring
`tbconfig.h`. Specifically, the chess engines can define `TB_*_ATTACKS`
macros that replace the default definitions with the engine's own definitions,
avoiding duplication of functionality.

Credits
-------

The Syzygy tablebases were created by Ronald de Man. Much of the probing code
`tbprobe.c` is a modified version of Ronald's `tbprobe.cpp` for Stockfish (all
Stockfish-specific code has been removed). The `tbcore.c` file is virtually
unchanged from Ronald's original version.
Chess engines can use `tb_probe_wdl` to get the WDL value during
search. This function is thread safe (unless TB_NO_THREADS is
set). The various "probe_root" functions are intended for probing only
at the root node, but are also theread-safe.

Chess engines and other clients can modify some features of Fathom and
override some of its internal functions by configuring
`tbconfig.h`. `tbconfig.h` is included in Fathom's code with angle
brackets. This allows a client of Fathom to override tbconfig.h by
placing its own modified copy in its include path before the Fathom
source directory.

One option provided by `tbconfig.h` is to define macros that replace
some aspects of Fathom's functionality, such as calculating piece
attacks, avoiding duplication of functionality. If doing this,
however, be careful with including typedefs or defines from your own
code into `tbconfig.h`, since these may clash with internal definitions
used by Fathom. I recommend instead interfacing to external
functions via a small module, with an interface something like this:

```
#ifndef _TB_ATTACK_INTERFACE
#define _TB_ATTACK_INTERFACE

#ifdef __cplusplus
#include <cstdint>
#else
#include <stdint.h>
#endif

extern tb_knight_attacks(unsigned square);
extern tb_king_attacks(unsigned square);
extern tb_root_attacks(unsigned square, uint64_t occ);
extern tb_bishop_attacks(unsigned square, uint64_t occ);
extern tb_queen_attacks(unsigned square, uint64_t occ);
extern tb_pawn_attacks(unsigned square, uint64_t occ);

#endif
```

You can add if wanted other function definitions such as a popcnt
function based on the chess engine's native popcnt support.

`tbconfig.h` can then reference these functions safety because the
interface depends only on types defined in standard headers. The
implementation, however, can use any types from the chess engine or
other client that are necessary. (A good optimizer with link-time
optimization will inline the implementation code even though it is not
visible in the interface).

History and Credits
-------------------

The Syzygy tablebases were created by Ronald de Man. The original version of Fathom
(https://github.com/basil00/Fathom) combined probing code from Ronald de Man, originally written for
Stockfish, with chess-related functions and other support code from Basil Falcinelli.
That codebase is no longer being maintained. This repository was originaly a fork of
that codebase, with additional modifications by Jon Dart.

However, the current Fathom code in this repository is no longer
derived directly from the probing code written for Stockfish, but
instead derives from tbprobe.c, which is a component of the Cfish
chess engine (https://github.com/syzygy1/Cfish), a Stockfish
derivative. tbprobe.c includes 7-man tablebase support. It was written
by Ronald de Man and released for unrestricted distribution and use.

This fork of Fathom replaces the Cfish board representation and move
generation code used in tbprobe.c with simpler, MIT-licensed code from the original
Fathom source by Basil. The code has been reorganized so that
`tbchess.c` contains all move generation and most chess-related typedefs
and functions, while `tbprobe.c` contains all the tablebase probing
code. The code replacement and reorganization was done by Jon Dart.

License
-------

(C) 2013-2015 Ronald de Man (original code)
(C) 2015 basil (new modifications)

Ronald de Man's original code can be "redistributed and/or modified without
restrictions".

The new modifications are released under the permissive MIT License:

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.
This version of Fathom is released under the MIT License. See the LICENSE file for
details.

4 changes: 2 additions & 2 deletions src/apps/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ endif
ifeq ($(UNAME),Darwin)
TARGET=fathom.macosx
endif
CC=clang
CC?=gcc
STRIP=strip
CFLAGS=-std=gnu99 -O2 -Wall -D TB_NO_THREADS -D TB_NO_HW_POP_COUNT -I..
CFLAGS=-std=gnu99 -O2 -Wall -Wshadow -DTB_NO_THREADS -I..

main: $(TARGET)

Expand Down
Loading