mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 07:27:46 +00:00
Compare commits
289 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c5bb9b9da9 | |||
| 67e5581e37 | |||
| 3ede7daab3 | |||
| dbd6156fce | |||
| 691ed425ba | |||
| c376ffce0f | |||
| 917944e9c5 | |||
| d9c7cad630 | |||
| 9763c69fa5 | |||
| 555d9a8711 | |||
| b9768b8bc5 | |||
| 4ef6b2c32a | |||
| 1d18647e73 | |||
| a518d5d3ad | |||
| 091aff0445 | |||
| 3ed86ed3f9 | |||
| e4d34e1815 | |||
| 42caebfaa5 | |||
| eed508b444 | |||
| 343544f3f7 | |||
| 13d1f0ae43 | |||
| ecd07e51d0 | |||
| 52ae0efccf | |||
| 53c04c0429 | |||
| 1064288b38 | |||
| d34bb889b1 | |||
| a3a0df92a3 | |||
| e8f9447b11 | |||
| 3cc47edf62 | |||
| d454cd4216 | |||
| 48f38f3092 | |||
| 281472e50e | |||
| f011a5af11 | |||
| 67b0da83da | |||
| 3674f18b97 | |||
| 72f7282ad4 | |||
| fbfce2132a | |||
| 2c825294ec | |||
| f86d2aee29 | |||
| 35ea39bed2 | |||
| 97015afce8 | |||
| f22a63ce67 | |||
| c08e7419a0 | |||
| 67f91bc5ea | |||
| 4bc2374450 | |||
| f5e872a0e3 | |||
| 25cb851f8a | |||
| 3cddb0c076 | |||
| 2bf18bfc63 | |||
| 5aeb907fa1 | |||
| 0c68971c13 | |||
| d9be00342c | |||
| 027d85e82a | |||
| 3bc3c069f1 | |||
| e9366fa155 | |||
| 549b5c478f | |||
| b15e148b5e | |||
| bb83a417cb | |||
| 984ee9d05b | |||
| a0cc15ccbc | |||
| 7f142d6817 | |||
| bd1c3ed7e3 | |||
| cca34e234c | |||
| ed95ad1c0e | |||
| c65d67feb5 | |||
| bc6faf633e | |||
| 8d1c1074d5 | |||
| e654209211 | |||
| b742a3f29a | |||
| 800410eef1 | |||
| 7b2cda95d9 | |||
| c2cefa6de0 | |||
| d7f5f15d69 | |||
| 84906b83ec | |||
| 274079990a | |||
| 10cb19d534 | |||
| d50b33cacd | |||
| 77b5ee0117 | |||
| af750bd2ef | |||
| 21cbfafc03 | |||
| d3947b2f3e | |||
| a71209868b | |||
| 7a1ff6d8ff | |||
| 82f6779c2e | |||
| 3abccdc82d | |||
| 8d6d0223bf | |||
| fc17d0de77 | |||
| 27f2ce8f6e | |||
| 45b0aea875 | |||
| 4803d5772c | |||
| 738c5595ad | |||
| bebd6e16f6 | |||
| 49e110c52b | |||
| 4d90aeb0ab | |||
| 03cd049c68 | |||
| 6ab8b9b6c6 | |||
| 490f67a3f8 | |||
| 0515ad0fb0 | |||
| bf51db2526 | |||
| 59702aca0d | |||
| c86eee3918 | |||
| 10b53e1c5e | |||
| a30d3571ca | |||
| 457ac26de5 | |||
| 679c2ea227 | |||
| 9ff594c3a9 | |||
| 849b089a63 | |||
| aee404f532 | |||
| 0915f85895 | |||
| 5e8bc6ac2a | |||
| 3e4dcaa06e | |||
| 14f47c8ac6 | |||
| 5d90c149b5 | |||
| 4b9e338541 | |||
| a0cf424cfc | |||
| aecdbfc4a0 | |||
| e6482b7d97 | |||
| 6e6c5b6103 | |||
| 04fd7efdfa | |||
| 5e331f9618 | |||
| b9f5d1c6ff | |||
| c4533e0d94 | |||
| f39cf1b008 | |||
| c6baefb79d | |||
| 4d120ee02e | |||
| f45eee318b | |||
| 8c2fd2170a | |||
| 91c2c44fb1 | |||
| 27e9fc1067 | |||
| e005270fb6 | |||
| 4f55ed14d3 | |||
| a6c0ba2100 | |||
| fc316cbca9 | |||
| 11b1a76f35 | |||
| bd8f463b7e | |||
| 4d14f97482 | |||
| 15616ad199 | |||
| 94a3608ab9 | |||
| 5769509d72 | |||
| fff6b9f061 | |||
| 56d2c3844a | |||
| 23b6809f3d | |||
| f31847302d | |||
| b1a4a18d63 | |||
| f7096ea7ce | |||
| a16ba5bbd1 | |||
| 408e6ee9b6 | |||
| 7b4f5c8f72 | |||
| 55948623e7 | |||
| 4d46d29efe | |||
| cc608a7aba | |||
| 1f40cd6d02 | |||
| 28bc8ed462 | |||
| 5ee16a180a | |||
| d30dfc084c | |||
| 6373e88b5b | |||
| 7487eb0dca | |||
| 2067a99c07 | |||
| 002062ae93 | |||
| 4064ee5406 | |||
| b0fd2b6b98 | |||
| d0bc951835 | |||
| 73131e7c78 | |||
| 1cc18d8a7a | |||
| c45c6d308f | |||
| f73bb438aa | |||
| 71dd8a333f | |||
| 9207baed65 | |||
| b191df5ebe | |||
| 3e95800814 | |||
| 2ed56f4d5f | |||
| 7b0b463720 | |||
| a6c5b4c6fb | |||
| 0504a6975d | |||
| a5b5a91512 | |||
| 1a8f63a896 | |||
| ee5514b8fd | |||
| 99e547f4cb | |||
| 110644d918 | |||
| 4b3a0fdab0 | |||
| 05e31c5e5f | |||
| 46fdb14b2f | |||
| cbb1a8ed31 | |||
| a6c5f60caa | |||
| 9518cc3254 | |||
| b0a177bc67 | |||
| 5b7b330616 | |||
| 3f64a2af6a | |||
| 9749f1f14c | |||
| 4ede49cd85 | |||
| 6960f41e03 | |||
| bf90499fc3 | |||
| 404c4122ce | |||
| 2a0bbb9faa | |||
| 128e097d03 | |||
| c1264e46d0 | |||
| 366f6b0dab | |||
| 58aee9a9ea | |||
| 4b703b1429 | |||
| 62d38f0196 | |||
| 7e575512ae | |||
| a55fb76dcc | |||
| 838255ef91 | |||
| 6fbe027da0 | |||
| ddcb572c41 | |||
| 12c0dfc113 | |||
| db53883f06 | |||
| 13ffb08136 | |||
| 2d82db1d14 | |||
| b88bc7b766 | |||
| e074a19f5c | |||
| b8930d0c26 | |||
| 4fc7734547 | |||
| 92dcbfa658 | |||
| b50eb6bea8 | |||
| 6f079ae720 | |||
| 203fdc9ac1 | |||
| 17d41b3861 | |||
| 8cff4862a6 | |||
| 908d98820b | |||
| a4c11b71ac | |||
| b2fadf32aa | |||
| 378bcfe760 | |||
| fe2ed42661 | |||
| e215a88cdd | |||
| e95e69515a | |||
| e05c80a088 | |||
| 7b31e81d77 | |||
| c0964fc70f | |||
| cd782c11ec | |||
| 5ea984ac35 | |||
| 1fd020a8ba | |||
| 02420d4670 | |||
| 2c7ab488a8 | |||
| d54e8a5955 | |||
| 4bebb15e94 | |||
| 3b8f66f8ac | |||
| b6e9d901b0 | |||
| 1b7223a53c | |||
| de1dc4f2de | |||
| a6e0f62a4f | |||
| 81e242a96d | |||
| dd1855eb2f | |||
| db4cd89cb8 | |||
| 7e95495b35 | |||
| 55eb7dd1e9 | |||
| 902c0566a6 | |||
| bc02cc0c8a | |||
| 05c6f7a40b | |||
| 2a98042c21 | |||
| 46409a7852 | |||
| d8b266af8b | |||
| abb40777bf | |||
| 90abcac1c7 | |||
| 7222f47350 | |||
| eafb66e1aa | |||
| d4a02b135d | |||
| d3608c4e79 | |||
| 38cfbeeb50 | |||
| 77547a4ef1 | |||
| 8ceef92266 | |||
| f7c013edd0 | |||
| 148490f04c | |||
| 7c6f346c90 | |||
| 1a34496761 | |||
| 049e5ca191 | |||
| 19dd0de4ff | |||
| 8a61b030a6 | |||
| c7e31d5aa8 | |||
| 818a3537a7 | |||
| bcbc9bfd1f | |||
| 7eda7335fd | |||
| 02606a8c83 | |||
| e7505324f6 | |||
| 653c0527a7 | |||
| 7f4c7cd785 | |||
| 0958e5c6d3 | |||
| 9fc77bc414 | |||
| 3b92159908 | |||
| e00bb13e85 | |||
| 3b41e62666 | |||
| 37c91aa94c | |||
| 43f67eab5f | |||
| 11d30b6298 | |||
| 3edb15d183 | |||
| d44ac0a485 | |||
| 481eda4ca0 | |||
| 7323231786 | |||
| e381951a24 |
@@ -7,14 +7,10 @@ Partner or Fritz) in order to be used comfortably. Read the
|
||||
documentation for your GUI of choice for information about how to use
|
||||
Stockfish with it.
|
||||
|
||||
This version of Stockfish supports up to 64 CPUs, but has not been
|
||||
tested thoroughly with more than 4. The program tries to detect the
|
||||
number of CPUs on your computer and sets the number of search threads
|
||||
accordingly, but please be aware that the detection is not always
|
||||
correct. It is therefore recommended to inspect the value of the
|
||||
*Threads* UCI parameter, and to make sure it equals the number of CPU
|
||||
cores on your computer. If you are using more than eight threads, it is
|
||||
recommended to raise the value of the *Min Split Depth* UCI parameter to 7.
|
||||
This version of Stockfish supports up to 64 CPUs. The engine defaults
|
||||
to one search thread it is therefore recommended to inspect the value of
|
||||
the *Threads* UCI parameter, and to make sure it equals the number of CPU
|
||||
cores on your computer.
|
||||
|
||||
|
||||
### Files
|
||||
@@ -25,7 +21,7 @@ This distribution of Stockfish consists of the following files:
|
||||
|
||||
* Copying.txt, a text file containing the GNU General Public License.
|
||||
|
||||
* src/, a subdirectory containing the full source code, including a Makefile
|
||||
* src, a subdirectory containing the full source code, including a Makefile
|
||||
that can be used to compile Stockfish on Unix-like systems. For further
|
||||
information about how to compile Stockfish yourself read section below.
|
||||
|
||||
|
||||
+9
-5
@@ -14,22 +14,25 @@ ResignScore = 600
|
||||
|
||||
[Engine]
|
||||
|
||||
Use Search Log = false
|
||||
Write Debug Log = false
|
||||
Write Search Log = false
|
||||
Search Log Filename = SearchLog.txt
|
||||
Book File = book.bin
|
||||
Best Book Move = false
|
||||
Contempt Factor = 0
|
||||
Mobility (Middle Game) = 100
|
||||
Mobility (Midgame) = 100
|
||||
Mobility (Endgame) = 100
|
||||
Passed Pawns (Middle Game) = 100
|
||||
Pawn Structure (Midgame) = 100
|
||||
Pawn Structure (Endgame) = 100
|
||||
Passed Pawns (Midgame) = 100
|
||||
Passed Pawns (Endgame) = 100
|
||||
Space = 100
|
||||
Aggressiveness = 100
|
||||
Cowardice = 100
|
||||
Min Split Depth = 4
|
||||
Min Split Depth = 0
|
||||
Max Threads per Split Point = 5
|
||||
Threads = 1
|
||||
Use Sleeping Threads = false
|
||||
Idle Threads Sleep = false
|
||||
Hash = 128
|
||||
Ponder = true
|
||||
OwnBook = false
|
||||
@@ -39,5 +42,6 @@ Emergency Move Horizon = 40
|
||||
Emergency Base Time = 200
|
||||
Emergency Move Time = 70
|
||||
Minimum Thinking Time = 20
|
||||
Slow Mover = 100
|
||||
UCI_Chess960 = false
|
||||
UCI_AnalyseMode = false
|
||||
|
||||
+66
-36
@@ -34,8 +34,9 @@ ifeq ($(UNAME),Haiku)
|
||||
endif
|
||||
BINDIR = $(PREFIX)/bin
|
||||
|
||||
### Built-in benchmark for pgo-builds
|
||||
### Built-in benchmark for pgo-builds and signature
|
||||
PGOBENCH = ./$(EXE) bench 32 1 10 default depth
|
||||
SIGNBENCH = ./$(EXE) bench
|
||||
|
||||
### Object files
|
||||
OBJS = benchmark.o bitbase.o bitboard.o book.o endgame.o evaluate.o main.o \
|
||||
@@ -164,6 +165,16 @@ ifeq ($(ARCH),osx-ppc-32)
|
||||
sse = no
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),linux-ppc-64)
|
||||
arch = ppc64
|
||||
os = any
|
||||
bits = 64
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
sse = no
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),osx-x86-64)
|
||||
arch = x86_64
|
||||
os = osx
|
||||
@@ -231,7 +242,7 @@ ifeq ($(COMP),clang)
|
||||
endif
|
||||
|
||||
### 3.2 General compiler settings
|
||||
CXXFLAGS = -g -Wall -Wcast-qual -fno-exceptions -fno-rtti $(EXTRACXXFLAGS)
|
||||
CXXFLAGS = -Wall -Wcast-qual -fno-exceptions -fno-rtti $(EXTRACXXFLAGS)
|
||||
|
||||
ifeq ($(comp),gcc)
|
||||
CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
|
||||
@@ -242,7 +253,7 @@ ifeq ($(comp),mingw)
|
||||
endif
|
||||
|
||||
ifeq ($(comp),icc)
|
||||
CXXFLAGS += -wd383,981,1418,1419,1476,10187,10188,11505,11503 -Wcheck -Wabi -Wdeprecated -strict-ansi
|
||||
CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
|
||||
endif
|
||||
|
||||
ifeq ($(comp),clang)
|
||||
@@ -262,9 +273,12 @@ endif
|
||||
|
||||
### On mingw use Windows threads, otherwise POSIX
|
||||
ifneq ($(comp),mingw)
|
||||
# Haiku has pthreads in its libroot, so only link it in on other platforms
|
||||
ifneq ($(UNAME),Haiku)
|
||||
LDFLAGS += -lpthread
|
||||
# On Android Bionic's C library comes with its own pthread implementation bundled in
|
||||
ifneq ($(arch),armv7)
|
||||
# Haiku has pthreads in its libroot, so only link it in on other platforms
|
||||
ifneq ($(UNAME),Haiku)
|
||||
LDFLAGS += -lpthread
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
@@ -275,6 +289,8 @@ endif
|
||||
### 3.4 Debugging
|
||||
ifeq ($(debug),no)
|
||||
CXXFLAGS += -DNDEBUG
|
||||
else
|
||||
CXXFLAGS += -g
|
||||
endif
|
||||
|
||||
### 3.5 Optimization
|
||||
@@ -293,7 +309,7 @@ ifeq ($(optimize),yes)
|
||||
endif
|
||||
|
||||
ifeq ($(arch),armv7)
|
||||
CXXFLAGS += -fno-gcse
|
||||
CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
|
||||
endif
|
||||
endif
|
||||
|
||||
@@ -305,7 +321,7 @@ ifeq ($(optimize),yes)
|
||||
ifeq ($(os),osx)
|
||||
CXXFLAGS += -fast -mdynamic-no-pic
|
||||
else
|
||||
CXXFLAGS += -O3
|
||||
CXXFLAGS += -fast
|
||||
endif
|
||||
endif
|
||||
|
||||
@@ -354,6 +370,7 @@ endif
|
||||
### needs access to the optimization flags.
|
||||
ifeq ($(comp),gcc)
|
||||
ifeq ($(optimize),yes)
|
||||
ifeq ($(debug),no)
|
||||
GCC_MAJOR := `$(CXX) -dumpversion | cut -f1 -d.`
|
||||
GCC_MINOR := `$(CXX) -dumpversion | cut -f2 -d.`
|
||||
ifeq (1,$(shell expr \( $(GCC_MAJOR) \> 4 \) \| \( $(GCC_MAJOR) \= 4 \& $(GCC_MINOR) \>= 5 \)))
|
||||
@@ -361,6 +378,7 @@ ifeq ($(comp),gcc)
|
||||
LDFLAGS += $(CXXFLAGS)
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
### ==========================================================================
|
||||
@@ -375,44 +393,47 @@ help:
|
||||
@echo ""
|
||||
@echo "Supported targets:"
|
||||
@echo ""
|
||||
@echo "build > Build unoptimized version"
|
||||
@echo "profile-build > Build PGO-optimized version"
|
||||
@echo "strip > Strip executable"
|
||||
@echo "install > Install executable"
|
||||
@echo "clean > Clean up"
|
||||
@echo "testrun > Make sample run"
|
||||
@echo "build > Standard build"
|
||||
@echo "signature-build > Standard build with embedded signature"
|
||||
@echo "profile-build > PGO build"
|
||||
@echo "signature-profile-build > PGO build with embedded signature"
|
||||
@echo "strip > Strip executable"
|
||||
@echo "install > Install executable"
|
||||
@echo "clean > Clean up"
|
||||
@echo ""
|
||||
@echo "Supported archs:"
|
||||
@echo ""
|
||||
@echo "x86-64 > x86 64-bit"
|
||||
@echo "x86-64-modern > x86 64-bit with runtime support for popcnt instruction"
|
||||
@echo "x86-32 > x86 32-bit excluding old hardware without SSE-support"
|
||||
@echo "x86-32-old > x86 32-bit including also very old hardware"
|
||||
@echo "osx-ppc-64 > PPC-Mac OS X 64 bit"
|
||||
@echo "osx-ppc-32 > PPC-Mac OS X 32 bit"
|
||||
@echo "osx-x86-64 > x86-Mac OS X 64 bit"
|
||||
@echo "osx-x86-32 > x86-Mac OS X 32 bit"
|
||||
@echo "armv7 > ARMv7 32 bit"
|
||||
@echo "general-64 > unspecified 64-bit"
|
||||
@echo "general-32 > unspecified 32-bit"
|
||||
@echo "x86-64 > x86 64-bit"
|
||||
@echo "x86-64-modern > x86 64-bit with popcnt support"
|
||||
@echo "x86-32 > x86 32-bit with SSE support"
|
||||
@echo "x86-32-old > x86 32-bit fall back for old hardware"
|
||||
@echo "linux-ppc-64 > PPC-Linux 64 bit"
|
||||
@echo "osx-ppc-64 > PPC-Mac OS X 64 bit"
|
||||
@echo "osx-ppc-32 > PPC-Mac OS X 32 bit"
|
||||
@echo "osx-x86-64 > x86-Mac OS X 64 bit"
|
||||
@echo "osx-x86-32 > x86-Mac OS X 32 bit"
|
||||
@echo "armv7 > ARMv7 32 bit"
|
||||
@echo "general-64 > unspecified 64-bit"
|
||||
@echo "general-32 > unspecified 32-bit"
|
||||
@echo ""
|
||||
@echo "Supported comps:"
|
||||
@echo "Supported compilers:"
|
||||
@echo ""
|
||||
@echo "gcc > Gnu compiler (default)"
|
||||
@echo "icc > Intel compiler"
|
||||
@echo "mingw > Gnu compiler with MinGW under Windows"
|
||||
@echo "clang > LLVM Clang compiler"
|
||||
@echo "gcc > Gnu compiler (default)"
|
||||
@echo "mingw > Gnu compiler with MinGW under Windows"
|
||||
@echo "clang > LLVM Clang compiler"
|
||||
@echo "icc > Intel compiler"
|
||||
@echo ""
|
||||
@echo "Non-standard targets:"
|
||||
@echo ""
|
||||
@echo "make hpux > Compile for HP-UX. Compiler = aCC"
|
||||
@echo "make hpux > Compile for HP-UX. Compiler = aCC"
|
||||
@echo ""
|
||||
@echo "Examples. If you don't know what to do, you likely want to run: "
|
||||
@echo ""
|
||||
@echo "make profile-build ARCH=x86-64 (This is for 64-bit systems)"
|
||||
@echo "make profile-build ARCH=x86-32 (This is for 32-bit systems)"
|
||||
@echo "make build ARCH=x86-64 (This is for 64-bit systems)"
|
||||
@echo "make build ARCH=x86-32 (This is for 32-bit systems)"
|
||||
@echo ""
|
||||
|
||||
.PHONY: build profile-build embed-signature
|
||||
build:
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
|
||||
@@ -437,6 +458,18 @@ profile-build:
|
||||
@echo "Step 4/4. Deleting profile data ..."
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
|
||||
|
||||
embed-signature:
|
||||
@echo "Running benchmark for getting the signature ..."
|
||||
@$(SIGNBENCH) 2>&1 | sed -n 's/Nodes searched : \(.*\)/\/string Version\/s\/"\\(.*\\)"\/"sig-\1"\//p' > sign.txt
|
||||
@sed -f sign.txt misc.cpp > misc2.cpp
|
||||
@mv misc2.cpp misc.cpp
|
||||
@rm sign.txt
|
||||
|
||||
signature-build: build embed-signature
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
|
||||
|
||||
signature-profile-build: build embed-signature profile-build
|
||||
|
||||
strip:
|
||||
strip $(EXE)
|
||||
|
||||
@@ -448,9 +481,6 @@ install:
|
||||
clean:
|
||||
$(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda
|
||||
|
||||
testrun:
|
||||
@$(PGOBENCH)
|
||||
|
||||
default:
|
||||
help
|
||||
|
||||
|
||||
+19
-5
@@ -47,7 +47,21 @@ static const char* Defaults[] = {
|
||||
"3r1rk1/p5pp/bpp1pp2/8/q1PP1P2/b3P3/P2NQRPP/1R2B1K1 b - - 6 22",
|
||||
"r1q2rk1/2p1bppp/2Pp4/p6b/Q1PNp3/4B3/PP1R1PPP/2K4R w - - 2 18",
|
||||
"4k2r/1pb2ppp/1p2p3/1R1p4/3P4/2r1PN2/P4PPP/1R4K1 b - - 3 22",
|
||||
"3q2k1/pb3p1p/4pbp1/2r5/PpN2N2/1P2P2P/5PP1/Q2R2K1 b - - 4 26"
|
||||
"3q2k1/pb3p1p/4pbp1/2r5/PpN2N2/1P2P2P/5PP1/Q2R2K1 b - - 4 26",
|
||||
"6k1/6p1/6Pp/ppp5/3pn2P/1P3K2/1PP2P2/3N4 b - - 0 1",
|
||||
"3b4/5kp1/1p1p1p1p/pP1PpP1P/P1P1P3/3KN3/8/8 w - - 0 1",
|
||||
"2K5/p7/7P/5pR1/8/5k2/r7/8 w - - 0 1",
|
||||
"8/6pk/1p6/8/PP3p1p/5P2/4KP1q/3Q4 w - - 0 1",
|
||||
"7k/3p2pp/4q3/8/4Q3/5Kp1/P6b/8 w - - 0 1",
|
||||
"8/2p5/8/2kPKp1p/2p4P/2P5/3P4/8 w - - 0 1",
|
||||
"8/1p3pp1/7p/5P1P/2k3P1/8/2K2P2/8 w - - 0 1",
|
||||
"8/pp2r1k1/2p1p3/3pP2p/1P1P1P1P/P5KR/8/8 w - - 0 1",
|
||||
"8/3p4/p1bk3p/Pp6/1Kp1PpPp/2P2P1P/2P5/5B2 b - - 0 1",
|
||||
"5k2/7R/4P2p/5K2/p1r2P1p/8/8/8 b - - 0 1",
|
||||
"6k1/6p1/P6p/r1N5/5p2/7P/1b3PP1/4R1K1 w - - 0 1",
|
||||
"1r3k2/4q3/2Pp3b/3Bp3/2Q2p2/1p1P2P1/1P2KP2/3N4 w - - 0 1",
|
||||
"6k1/4pp1p/3p2p1/P1pPb3/R7/1r2P1PP/3B1P2/6K1 w - - 0 1",
|
||||
"8/3p3B/5p2/5P2/p7/PP5b/k7/6K1 w - - 0 1"
|
||||
};
|
||||
|
||||
|
||||
@@ -68,7 +82,7 @@ void benchmark(const Position& current, istream& is) {
|
||||
// Assign default values to missing arguments
|
||||
string ttSize = (is >> token) ? token : "32";
|
||||
string threads = (is >> token) ? token : "1";
|
||||
string limit = (is >> token) ? token : "12";
|
||||
string limit = (is >> token) ? token : "13";
|
||||
string fenFile = (is >> token) ? token : "default";
|
||||
string limitType = (is >> token) ? token : "depth";
|
||||
|
||||
@@ -89,7 +103,7 @@ void benchmark(const Position& current, istream& is) {
|
||||
limits.depth = atoi(limit.c_str());
|
||||
|
||||
if (fenFile == "default")
|
||||
fens.assign(Defaults, Defaults + 16);
|
||||
fens.assign(Defaults, Defaults + 30);
|
||||
|
||||
else if (fenFile == "current")
|
||||
fens.push_back(current.fen());
|
||||
@@ -116,9 +130,9 @@ void benchmark(const Position& current, istream& is) {
|
||||
Search::StateStackPtr st;
|
||||
Time::point elapsed = Time::now();
|
||||
|
||||
for (size_t i = 0; i < fens.size(); i++)
|
||||
for (size_t i = 0; i < fens.size(); ++i)
|
||||
{
|
||||
Position pos(fens[i], Options["UCI_Chess960"], Threads.main_thread());
|
||||
Position pos(fens[i], Options["UCI_Chess960"], Threads.main());
|
||||
|
||||
cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
|
||||
|
||||
|
||||
+30
-30
@@ -39,9 +39,9 @@ namespace {
|
||||
// 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 6 - rank (from 6 - RANK_7 to 6 - RANK_2)
|
||||
// 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) + ((6 - rank_of(psq)) << 15);
|
||||
return wksq + (bksq << 6) + (us << 12) + (file_of(psq) << 13) + ((RANK_7 - rank_of(psq)) << 15);
|
||||
}
|
||||
|
||||
enum Result {
|
||||
@@ -55,8 +55,8 @@ namespace {
|
||||
|
||||
struct KPKPosition {
|
||||
|
||||
operator Result() const { return res; }
|
||||
Result classify_leaf(unsigned idx);
|
||||
KPKPosition(unsigned idx);
|
||||
operator Result() const { return result; }
|
||||
Result classify(const std::vector<KPKPosition>& db)
|
||||
{ return us == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
|
||||
|
||||
@@ -65,7 +65,7 @@ namespace {
|
||||
|
||||
Color us;
|
||||
Square bksq, wksq, psq;
|
||||
Result res;
|
||||
Result result;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@@ -83,21 +83,21 @@ bool Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color us) {
|
||||
void Bitbases::init_kpk() {
|
||||
|
||||
unsigned idx, repeat = 1;
|
||||
std::vector<KPKPosition> db(IndexMax);
|
||||
std::vector<KPKPosition> db;
|
||||
db.reserve(IndexMax);
|
||||
|
||||
// Initialize db with known win / draw positions
|
||||
for (idx = 0; idx < IndexMax; idx++)
|
||||
db[idx].classify_leaf(idx);
|
||||
for (idx = 0; idx < IndexMax; ++idx)
|
||||
db.push_back(KPKPosition(idx));
|
||||
|
||||
// Iterate through the positions until no more of the unknown positions can be
|
||||
// changed to either wins or draws (15 cycles needed).
|
||||
while (repeat)
|
||||
for (repeat = idx = 0; idx < IndexMax; idx++)
|
||||
if (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN)
|
||||
repeat = 1;
|
||||
for (repeat = idx = 0; idx < IndexMax; ++idx)
|
||||
repeat |= (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN);
|
||||
|
||||
// Map 32 results into one KPKBitbase[] entry
|
||||
for (idx = 0; idx < IndexMax; idx++)
|
||||
for (idx = 0; idx < IndexMax; ++idx)
|
||||
if (db[idx] == WIN)
|
||||
KPKBitbase[idx / 32] |= 1 << (idx & 0x1F);
|
||||
}
|
||||
@@ -105,34 +105,32 @@ void Bitbases::init_kpk() {
|
||||
|
||||
namespace {
|
||||
|
||||
Result KPKPosition::classify_leaf(unsigned idx) {
|
||||
KPKPosition::KPKPosition(unsigned idx) {
|
||||
|
||||
wksq = Square((idx >> 0) & 0x3F);
|
||||
bksq = Square((idx >> 6) & 0x3F);
|
||||
us = Color((idx >> 12) & 0x01);
|
||||
psq = File((idx >> 13) & 3) | Rank(6 - (idx >> 15));
|
||||
wksq = Square((idx >> 0) & 0x3F);
|
||||
bksq = Square((idx >> 6) & 0x3F);
|
||||
us = Color ((idx >> 12) & 0x01);
|
||||
psq = File ((idx >> 13) & 0x03) | Rank(RANK_7 - (idx >> 15));
|
||||
result = UNKNOWN;
|
||||
|
||||
// Check if two pieces are on the same square or if a king can be captured
|
||||
if ( wksq == psq || wksq == bksq || bksq == psq
|
||||
|| (StepAttacksBB[KING][wksq] & bksq)
|
||||
if ( square_distance(wksq, bksq) <= 1 || wksq == psq || bksq == psq
|
||||
|| (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
|
||||
return res = INVALID;
|
||||
result = INVALID;
|
||||
|
||||
if (us == WHITE)
|
||||
else if (us == WHITE)
|
||||
{
|
||||
// Immediate win if pawn can be promoted without getting captured
|
||||
if ( rank_of(psq) == RANK_7
|
||||
&& wksq != psq + DELTA_N
|
||||
&& ( square_distance(bksq, psq + DELTA_N) > 1
|
||||
||(StepAttacksBB[KING][wksq] & (psq + DELTA_N))))
|
||||
return res = WIN;
|
||||
result = WIN;
|
||||
}
|
||||
// Immediate draw if is stalemate or king captures undefended pawn
|
||||
else if ( !(StepAttacksBB[KING][bksq] & ~(StepAttacksBB[KING][wksq] | StepAttacksBB[PAWN][psq]))
|
||||
|| (StepAttacksBB[KING][bksq] & psq & ~StepAttacksBB[KING][wksq]))
|
||||
return res = DRAW;
|
||||
|
||||
return res = UNKNOWN;
|
||||
result = DRAW;
|
||||
}
|
||||
|
||||
template<Color Us>
|
||||
@@ -148,26 +146,28 @@ namespace {
|
||||
// as WIN, the position is classified WIN otherwise the current position is
|
||||
// classified UNKNOWN.
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
Result r = INVALID;
|
||||
Bitboard b = StepAttacksBB[KING][Us == WHITE ? wksq : bksq];
|
||||
|
||||
while (b)
|
||||
r |= Us == WHITE ? db[index(~Us, bksq, pop_lsb(&b), psq)]
|
||||
: db[index(~Us, pop_lsb(&b), wksq, psq)];
|
||||
r |= Us == WHITE ? db[index(Them, bksq, pop_lsb(&b), psq)]
|
||||
: db[index(Them, pop_lsb(&b), wksq, psq)];
|
||||
|
||||
if (Us == WHITE && rank_of(psq) < RANK_7)
|
||||
{
|
||||
Square s = psq + DELTA_N;
|
||||
r |= db[index(BLACK, bksq, wksq, s)]; // Single push
|
||||
|
||||
if (rank_of(s) == RANK_3 && s != wksq && s != bksq)
|
||||
if (rank_of(psq) == RANK_2 && s != wksq && s != bksq)
|
||||
r |= db[index(BLACK, bksq, wksq, s + DELTA_N)]; // Double push
|
||||
}
|
||||
|
||||
if (Us == WHITE)
|
||||
return res = r & WIN ? WIN : r & UNKNOWN ? UNKNOWN : DRAW;
|
||||
return result = r & WIN ? WIN : r & UNKNOWN ? UNKNOWN : DRAW;
|
||||
else
|
||||
return res = r & DRAW ? DRAW : r & UNKNOWN ? UNKNOWN : WIN;
|
||||
return result = r & DRAW ? DRAW : r & UNKNOWN ? UNKNOWN : WIN;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
+36
-38
@@ -42,14 +42,14 @@ Bitboard SquareBB[SQUARE_NB];
|
||||
Bitboard FileBB[FILE_NB];
|
||||
Bitboard RankBB[RANK_NB];
|
||||
Bitboard AdjacentFilesBB[FILE_NB];
|
||||
Bitboard ThisAndAdjacentFilesBB[FILE_NB];
|
||||
Bitboard InFrontBB[COLOR_NB][RANK_NB];
|
||||
Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
|
||||
Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
|
||||
Bitboard LineBB[SQUARE_NB][SQUARE_NB];
|
||||
Bitboard DistanceRingsBB[SQUARE_NB][8];
|
||||
Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
|
||||
Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
|
||||
Bitboard AttackSpanMask[COLOR_NB][SQUARE_NB];
|
||||
Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
|
||||
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
||||
|
||||
int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||
@@ -84,7 +84,7 @@ namespace {
|
||||
/// lsb()/msb() finds the least/most significant bit in a nonzero bitboard.
|
||||
/// pop_lsb() finds and clears the least significant bit in a nonzero bitboard.
|
||||
|
||||
#if !defined(USE_BSFQ)
|
||||
#ifndef USE_BSFQ
|
||||
|
||||
Square lsb(Bitboard b) { return BSFTable[bsf_index(b)]; }
|
||||
|
||||
@@ -123,7 +123,7 @@ Square msb(Bitboard b) {
|
||||
return (Square)(result + MS1BTable[b32]);
|
||||
}
|
||||
|
||||
#endif // !defined(USE_BSFQ)
|
||||
#endif // ifndef USE_BSFQ
|
||||
|
||||
|
||||
/// Bitboards::print() prints a bitboard in an easily readable format to the
|
||||
@@ -133,11 +133,11 @@ void Bitboards::print(Bitboard b) {
|
||||
|
||||
sync_cout;
|
||||
|
||||
for (Rank rank = RANK_8; rank >= RANK_1; rank--)
|
||||
for (Rank rank = RANK_8; rank >= RANK_1; --rank)
|
||||
{
|
||||
std::cout << "+---+---+---+---+---+---+---+---+" << '\n';
|
||||
|
||||
for (File file = FILE_A; file <= FILE_H; file++)
|
||||
for (File file = FILE_A; file <= FILE_H; ++file)
|
||||
std::cout << "| " << (b & (file | rank) ? "X " : " ");
|
||||
|
||||
std::cout << "|\n";
|
||||
@@ -151,59 +151,54 @@ void Bitboards::print(Bitboard b) {
|
||||
|
||||
void Bitboards::init() {
|
||||
|
||||
for (int k = 0, i = 0; i < 8; i++)
|
||||
for (int k = 0, i = 0; i < 8; ++i)
|
||||
while (k < (2 << i))
|
||||
MS1BTable[k++] = i;
|
||||
|
||||
for (int i = 0; i < 64; i++)
|
||||
for (int i = 0; i < 64; ++i)
|
||||
BSFTable[bsf_index(1ULL << i)] = Square(i);
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||
SquareBB[s] = 1ULL << s;
|
||||
|
||||
FileBB[FILE_A] = FileABB;
|
||||
RankBB[RANK_1] = Rank1BB;
|
||||
|
||||
for (int i = 1; i < 8; i++)
|
||||
for (int i = 1; i < 8; ++i)
|
||||
{
|
||||
FileBB[i] = FileBB[i - 1] << 1;
|
||||
RankBB[i] = RankBB[i - 1] << 8;
|
||||
}
|
||||
|
||||
for (File f = FILE_A; f <= FILE_H; f++)
|
||||
{
|
||||
for (File f = FILE_A; f <= FILE_H; ++f)
|
||||
AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0);
|
||||
ThisAndAdjacentFilesBB[f] = FileBB[f] | AdjacentFilesBB[f];
|
||||
}
|
||||
|
||||
for (Rank r = RANK_1; r < RANK_8; r++)
|
||||
for (Rank r = RANK_1; r < RANK_8; ++r)
|
||||
InFrontBB[WHITE][r] = ~(InFrontBB[BLACK][r + 1] = InFrontBB[BLACK][r] | RankBB[r]);
|
||||
|
||||
for (Color c = WHITE; c <= BLACK; c++)
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
for (Color c = WHITE; c <= BLACK; ++c)
|
||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||
{
|
||||
ForwardBB[c][s] = InFrontBB[c][rank_of(s)] & FileBB[file_of(s)];
|
||||
PassedPawnMask[c][s] = InFrontBB[c][rank_of(s)] & ThisAndAdjacentFilesBB[file_of(s)];
|
||||
AttackSpanMask[c][s] = InFrontBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
|
||||
PawnAttackSpan[c][s] = InFrontBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
|
||||
PassedPawnMask[c][s] = ForwardBB[c][s] | PawnAttackSpan[c][s];
|
||||
}
|
||||
|
||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
|
||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++)
|
||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
|
||||
{
|
||||
SquareDistance[s1][s2] = std::max(file_distance(s1, s2), rank_distance(s1, s2));
|
||||
|
||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
|
||||
for (int d = 1; d < 8; d++)
|
||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++)
|
||||
if (SquareDistance[s1][s2] == d)
|
||||
DistanceRingsBB[s1][d - 1] |= s2;
|
||||
if (s1 != s2)
|
||||
DistanceRingsBB[s1][SquareDistance[s1][s2] - 1] |= s2;
|
||||
}
|
||||
|
||||
int steps[][9] = { {}, { 7, 9 }, { 17, 15, 10, 6, -6, -10, -15, -17 },
|
||||
{}, {}, {}, { 9, 7, -7, -9, 8, 1, -1, -8 } };
|
||||
|
||||
for (Color c = WHITE; c <= BLACK; c++)
|
||||
for (PieceType pt = PAWN; pt <= KING; pt++)
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
for (int k = 0; steps[pt][k]; k++)
|
||||
for (Color c = WHITE; c <= BLACK; ++c)
|
||||
for (PieceType pt = PAWN; pt <= KING; ++pt)
|
||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||
for (int k = 0; steps[pt][k]; ++k)
|
||||
{
|
||||
Square to = s + Square(c == WHITE ? steps[pt][k] : -steps[pt][k]);
|
||||
|
||||
@@ -217,20 +212,23 @@ void Bitboards::init() {
|
||||
init_magics(RTable, RAttacks, RMagics, RMasks, RShifts, RDeltas, magic_index<ROOK>);
|
||||
init_magics(BTable, BAttacks, BMagics, BMasks, BShifts, BDeltas, magic_index<BISHOP>);
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||
{
|
||||
PseudoAttacks[QUEEN][s] = PseudoAttacks[BISHOP][s] = attacks_bb<BISHOP>(s, 0);
|
||||
PseudoAttacks[QUEEN][s] |= PseudoAttacks[ ROOK][s] = attacks_bb< ROOK>(s, 0);
|
||||
}
|
||||
|
||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
|
||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++)
|
||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
|
||||
if (PseudoAttacks[QUEEN][s1] & s2)
|
||||
{
|
||||
Square delta = (s2 - s1) / square_distance(s1, s2);
|
||||
|
||||
for (Square s = s1 + delta; s != s2; s += delta)
|
||||
BetweenBB[s1][s2] |= s;
|
||||
|
||||
PieceType pt = (PseudoAttacks[BISHOP][s1] & s2) ? BISHOP : ROOK;
|
||||
LineBB[s1][s2] = (PseudoAttacks[pt][s1] & PseudoAttacks[pt][s2]) | s1 | s2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,7 +239,7 @@ namespace {
|
||||
|
||||
Bitboard attack = 0;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (Square s = sq + deltas[i];
|
||||
is_ok(s) && square_distance(s, s - deltas[i]) == 1;
|
||||
s += deltas[i])
|
||||
@@ -287,7 +285,7 @@ namespace {
|
||||
// attacks[s] is a pointer to the beginning of the attacks table for square 's'
|
||||
attacks[SQ_A1] = table;
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||
{
|
||||
// Board edges are not considered in the relevant occupancies
|
||||
edges = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));
|
||||
@@ -322,13 +320,13 @@ namespace {
|
||||
do magics[s] = pick_random(rk, booster);
|
||||
while (popcount<Max15>((magics[s] * masks[s]) >> 56) < 6);
|
||||
|
||||
memset(attacks[s], 0, size * sizeof(Bitboard));
|
||||
std::memset(attacks[s], 0, size * sizeof(Bitboard));
|
||||
|
||||
// A good magic must map every possible occupancy to an index that
|
||||
// looks up the correct sliding attack in the attacks[s] database.
|
||||
// Note that we build up the database for square 's' as a side
|
||||
// effect of verifying the magic.
|
||||
for (i = 0; i < size; i++)
|
||||
for (i = 0; i < size; ++i)
|
||||
{
|
||||
Bitboard& attack = attacks[s][index(s, occupancy[i])];
|
||||
|
||||
|
||||
+83
-50
@@ -18,7 +18,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(BITBOARD_H_INCLUDED)
|
||||
#ifndef BITBOARD_H_INCLUDED
|
||||
#define BITBOARD_H_INCLUDED
|
||||
|
||||
#include "types.h"
|
||||
@@ -37,6 +37,24 @@ bool probe_kpk(Square wksq, Square wpsq, Square bksq, Color us);
|
||||
|
||||
}
|
||||
|
||||
const Bitboard FileABB = 0x0101010101010101ULL;
|
||||
const Bitboard FileBBB = FileABB << 1;
|
||||
const Bitboard FileCBB = FileABB << 2;
|
||||
const Bitboard FileDBB = FileABB << 3;
|
||||
const Bitboard FileEBB = FileABB << 4;
|
||||
const Bitboard FileFBB = FileABB << 5;
|
||||
const Bitboard FileGBB = FileABB << 6;
|
||||
const Bitboard FileHBB = FileABB << 7;
|
||||
|
||||
const Bitboard Rank1BB = 0xFF;
|
||||
const Bitboard Rank2BB = Rank1BB << (8 * 1);
|
||||
const Bitboard Rank3BB = Rank1BB << (8 * 2);
|
||||
const Bitboard Rank4BB = Rank1BB << (8 * 3);
|
||||
const Bitboard Rank5BB = Rank1BB << (8 * 4);
|
||||
const Bitboard Rank6BB = Rank1BB << (8 * 5);
|
||||
const Bitboard Rank7BB = Rank1BB << (8 * 6);
|
||||
const Bitboard Rank8BB = Rank1BB << (8 * 7);
|
||||
|
||||
CACHE_LINE_ALIGNMENT
|
||||
|
||||
extern Bitboard RMasks[SQUARE_NB];
|
||||
@@ -53,17 +71,19 @@ extern Bitboard SquareBB[SQUARE_NB];
|
||||
extern Bitboard FileBB[FILE_NB];
|
||||
extern Bitboard RankBB[RANK_NB];
|
||||
extern Bitboard AdjacentFilesBB[FILE_NB];
|
||||
extern Bitboard ThisAndAdjacentFilesBB[FILE_NB];
|
||||
extern Bitboard InFrontBB[COLOR_NB][RANK_NB];
|
||||
extern Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
|
||||
extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
|
||||
extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
|
||||
extern Bitboard DistanceRingsBB[SQUARE_NB][8];
|
||||
extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
|
||||
extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
|
||||
extern Bitboard AttackSpanMask[COLOR_NB][SQUARE_NB];
|
||||
extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
|
||||
extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
||||
|
||||
const Bitboard BlackSquares = 0xAA55AA55AA55AA55ULL;
|
||||
extern int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||
|
||||
const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
|
||||
|
||||
/// Overloads of bitwise operators between a Bitboard and a Square for testing
|
||||
/// whether a given bit is set in a bitboard, and for setting and clearing bits.
|
||||
@@ -88,13 +108,34 @@ inline Bitboard operator^(Bitboard b, Square s) {
|
||||
return b ^ SquareBB[s];
|
||||
}
|
||||
|
||||
|
||||
/// more_than_one() returns true if in 'b' there is more than one bit set
|
||||
|
||||
inline bool more_than_one(Bitboard b) {
|
||||
return b & (b - 1);
|
||||
}
|
||||
|
||||
inline int square_distance(Square s1, Square s2) {
|
||||
return SquareDistance[s1][s2];
|
||||
}
|
||||
|
||||
inline int file_distance(Square s1, Square s2) {
|
||||
return abs(file_of(s1) - file_of(s2));
|
||||
}
|
||||
|
||||
inline int rank_distance(Square s1, Square s2) {
|
||||
return abs(rank_of(s1) - rank_of(s2));
|
||||
}
|
||||
|
||||
|
||||
/// shift_bb() moves bitboard one step along direction Delta. Mainly for pawns.
|
||||
|
||||
template<Square Delta>
|
||||
inline Bitboard shift_bb(Bitboard b) {
|
||||
|
||||
return Delta == DELTA_N ? b << 8 : Delta == DELTA_S ? b >> 8
|
||||
: Delta == DELTA_NE ? (b & ~FileHBB) << 9 : Delta == DELTA_SE ? (b & ~FileHBB) >> 7
|
||||
: Delta == DELTA_NW ? (b & ~FileABB) << 7 : Delta == DELTA_SW ? (b & ~FileABB) >> 9
|
||||
: 0;
|
||||
}
|
||||
|
||||
|
||||
/// rank_bb() and file_bb() take a file or a square as input and return
|
||||
/// a bitboard representing all squares on the given file or rank.
|
||||
@@ -116,7 +157,7 @@ inline Bitboard file_bb(Square s) {
|
||||
}
|
||||
|
||||
|
||||
/// adjacent_files_bb takes a file as input and returns a bitboard representing
|
||||
/// adjacent_files_bb() takes a file as input and returns a bitboard representing
|
||||
/// all squares on the adjacent files.
|
||||
|
||||
inline Bitboard adjacent_files_bb(File f) {
|
||||
@@ -124,30 +165,17 @@ inline Bitboard adjacent_files_bb(File f) {
|
||||
}
|
||||
|
||||
|
||||
/// this_and_adjacent_files_bb takes a file as input and returns a bitboard
|
||||
/// representing all squares on the given and adjacent files.
|
||||
|
||||
inline Bitboard this_and_adjacent_files_bb(File f) {
|
||||
return ThisAndAdjacentFilesBB[f];
|
||||
}
|
||||
|
||||
|
||||
/// in_front_bb() takes a color and a rank or square as input, and returns a
|
||||
/// bitboard representing all the squares on all ranks in front of the rank
|
||||
/// (or square), from the given color's point of view. For instance,
|
||||
/// in_front_bb(WHITE, RANK_5) will give all squares on ranks 6, 7 and 8, while
|
||||
/// in_front_bb(BLACK, SQ_D3) will give all squares on ranks 1 and 2.
|
||||
/// in_front_bb() takes a color and a rank as input, and returns a bitboard
|
||||
/// representing all the squares on all ranks in front of the rank, from the
|
||||
/// given color's point of view. For instance, in_front_bb(BLACK, RANK_3) will
|
||||
/// give all squares on ranks 1 and 2.
|
||||
|
||||
inline Bitboard in_front_bb(Color c, Rank r) {
|
||||
return InFrontBB[c][r];
|
||||
}
|
||||
|
||||
inline Bitboard in_front_bb(Color c, Square s) {
|
||||
return InFrontBB[c][rank_of(s)];
|
||||
}
|
||||
|
||||
|
||||
/// between_bb returns a bitboard representing all squares between two squares.
|
||||
/// between_bb() returns a bitboard representing all squares between two squares.
|
||||
/// For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with the bits for
|
||||
/// square d5 and e6 set. If s1 and s2 are not on the same line, file or diagonal,
|
||||
/// 0 is returned.
|
||||
@@ -157,7 +185,7 @@ inline Bitboard between_bb(Square s1, Square s2) {
|
||||
}
|
||||
|
||||
|
||||
/// forward_bb takes a color and a square as input, and returns a bitboard
|
||||
/// forward_bb() takes a color and a square as input, and returns a bitboard
|
||||
/// representing all squares along the line in front of the square, from the
|
||||
/// point of view of the given color. Definition of the table is:
|
||||
/// ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s)
|
||||
@@ -167,40 +195,39 @@ inline Bitboard forward_bb(Color c, Square s) {
|
||||
}
|
||||
|
||||
|
||||
/// passed_pawn_mask takes a color and a square as input, and returns a
|
||||
/// pawn_attack_span() takes a color and a square as input, and returns a bitboard
|
||||
/// representing all squares that can be attacked by a pawn of the given color
|
||||
/// when it moves along its file starting from the given square. Definition is:
|
||||
/// PawnAttackSpan[c][s] = in_front_bb(c, s) & adjacent_files_bb(s);
|
||||
|
||||
inline Bitboard pawn_attack_span(Color c, Square s) {
|
||||
return PawnAttackSpan[c][s];
|
||||
}
|
||||
|
||||
|
||||
/// passed_pawn_mask() takes a color and a square as input, and returns a
|
||||
/// bitboard mask which can be used to test if a pawn of the given color on
|
||||
/// the given square is a passed pawn. Definition of the table is:
|
||||
/// PassedPawnMask[c][s] = in_front_bb(c, s) & this_and_adjacent_files_bb(s)
|
||||
/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_bb(c, s)
|
||||
|
||||
inline Bitboard passed_pawn_mask(Color c, Square s) {
|
||||
return PassedPawnMask[c][s];
|
||||
}
|
||||
|
||||
|
||||
/// attack_span_mask takes a color and a square as input, and returns a bitboard
|
||||
/// representing all squares that can be attacked by a pawn of the given color
|
||||
/// when it moves along its file starting from the given square. Definition is:
|
||||
/// AttackSpanMask[c][s] = in_front_bb(c, s) & adjacent_files_bb(s);
|
||||
/// squares_of_color() returns a bitboard representing all squares with the same
|
||||
/// color of the given square.
|
||||
|
||||
inline Bitboard attack_span_mask(Color c, Square s) {
|
||||
return AttackSpanMask[c][s];
|
||||
inline Bitboard squares_of_color(Square s) {
|
||||
return DarkSquares & s ? DarkSquares : ~DarkSquares;
|
||||
}
|
||||
|
||||
|
||||
/// squares_aligned returns true if the squares s1, s2 and s3 are aligned
|
||||
/// aligned() returns true if the squares s1, s2 and s3 are aligned
|
||||
/// either on a straight or on a diagonal line.
|
||||
|
||||
inline bool squares_aligned(Square s1, Square s2, Square s3) {
|
||||
return (BetweenBB[s1][s2] | BetweenBB[s1][s3] | BetweenBB[s2][s3])
|
||||
& ( SquareBB[s1] | SquareBB[s2] | SquareBB[s3]);
|
||||
}
|
||||
|
||||
|
||||
/// same_color_squares() returns a bitboard representing all squares with
|
||||
/// the same color of the given square.
|
||||
|
||||
inline Bitboard same_color_squares(Square s) {
|
||||
return BlackSquares & s ? BlackSquares : ~BlackSquares;
|
||||
inline bool aligned(Square s1, Square s2, Square s3) {
|
||||
return LineBB[s1][s2] & s3;
|
||||
}
|
||||
|
||||
|
||||
@@ -231,7 +258,7 @@ inline Bitboard attacks_bb(Square s, Bitboard occ) {
|
||||
/// lsb()/msb() finds the least/most significant bit in a nonzero bitboard.
|
||||
/// pop_lsb() finds and clears the least significant bit in a nonzero bitboard.
|
||||
|
||||
#if defined(USE_BSFQ)
|
||||
#ifdef USE_BSFQ
|
||||
|
||||
# if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
|
||||
|
||||
@@ -284,7 +311,7 @@ FORCE_INLINE Square pop_lsb(Bitboard* b) {
|
||||
return s;
|
||||
}
|
||||
|
||||
#else // if !defined(USE_BSFQ)
|
||||
#else // if defined(USE_BSFQ)
|
||||
|
||||
extern Square msb(Bitboard b);
|
||||
extern Square lsb(Bitboard b);
|
||||
@@ -292,4 +319,10 @@ extern Square pop_lsb(Bitboard* b);
|
||||
|
||||
#endif
|
||||
|
||||
#endif // !defined(BITBOARD_H_INCLUDED)
|
||||
/// frontmost_sq() and backmost_sq() find the square corresponding to the
|
||||
/// most/least advanced bit relative to the given color.
|
||||
|
||||
inline Square frontmost_sq(Color c, Bitboard b) { return c == WHITE ? msb(b) : lsb(b); }
|
||||
inline Square backmost_sq(Color c, Bitboard b) { return c == WHITE ? lsb(b) : msb(b); }
|
||||
|
||||
#endif // #ifndef BITBOARD_H_INCLUDED
|
||||
|
||||
+3
-3
@@ -18,7 +18,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(BITCOUNT_H_INCLUDED)
|
||||
#ifndef BITCOUNT_H_INCLUDED
|
||||
#define BITCOUNT_H_INCLUDED
|
||||
|
||||
#include <cassert>
|
||||
@@ -81,7 +81,7 @@ inline int popcount<CNT_32_MAX15>(Bitboard b) {
|
||||
template<>
|
||||
inline int popcount<CNT_HW_POPCNT>(Bitboard b) {
|
||||
|
||||
#if !defined(USE_POPCNT)
|
||||
#ifndef USE_POPCNT
|
||||
|
||||
assert(false);
|
||||
return b != 0; // Avoid 'b not used' warning
|
||||
@@ -102,4 +102,4 @@ inline int popcount<CNT_HW_POPCNT>(Bitboard b) {
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // !defined(BITCOUNT_H_INCLUDED)
|
||||
#endif // #ifndef BITCOUNT_H_INCLUDED
|
||||
|
||||
+5
-5
@@ -361,7 +361,7 @@ PolyglotBook::~PolyglotBook() { if (is_open()) close(); }
|
||||
template<typename T> PolyglotBook& PolyglotBook::operator>>(T& n) {
|
||||
|
||||
n = 0;
|
||||
for (size_t i = 0; i < sizeof(T); i++)
|
||||
for (size_t i = 0; i < sizeof(T); ++i)
|
||||
n = T((n << 8) + ifstream::get());
|
||||
|
||||
return *this;
|
||||
@@ -413,7 +413,7 @@ Move PolyglotBook::probe(const Position& pos, const string& fName, bool pickBest
|
||||
// Choose book move according to its score. If a move has a very
|
||||
// high score it has higher probability to be choosen than a move
|
||||
// with lower score. Note that first entry is always chosen.
|
||||
if ( (sum && rkiss.rand<unsigned>() % sum < e.count)
|
||||
if ( (!pickBest && sum && rkiss.rand<unsigned>() % sum < e.count)
|
||||
|| (pickBest && e.count == best))
|
||||
move = Move(e.move);
|
||||
}
|
||||
@@ -436,9 +436,9 @@ Move PolyglotBook::probe(const Position& pos, const string& fName, bool pickBest
|
||||
move = make<PROMOTION>(from_sq(move), to_sq(move), PieceType(pt + 1));
|
||||
|
||||
// Add 'special move' flags and verify it is legal
|
||||
for (MoveList<LEGAL> ml(pos); !ml.end(); ++ml)
|
||||
if (move == (ml.move() ^ type_of(ml.move())))
|
||||
return ml.move();
|
||||
for (MoveList<LEGAL> it(pos); *it; ++it)
|
||||
if (move == (*it ^ type_of(*it)))
|
||||
return *it;
|
||||
|
||||
return MOVE_NONE;
|
||||
}
|
||||
|
||||
+2
-2
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(BOOK_H_INCLUDED)
|
||||
#ifndef BOOK_H_INCLUDED
|
||||
#define BOOK_H_INCLUDED
|
||||
|
||||
#include <fstream>
|
||||
@@ -42,4 +42,4 @@ private:
|
||||
std::string fileName;
|
||||
};
|
||||
|
||||
#endif // !defined(BOOK_H_INCLUDED)
|
||||
#endif // #ifndef BOOK_H_INCLUDED
|
||||
|
||||
+314
-397
File diff suppressed because it is too large
Load Diff
+7
-6
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(ENDGAME_H_INCLUDED)
|
||||
#ifndef ENDGAME_H_INCLUDED
|
||||
#define ENDGAME_H_INCLUDED
|
||||
|
||||
#include <map>
|
||||
@@ -33,6 +33,7 @@ enum EndgameType {
|
||||
|
||||
// Evaluation functions
|
||||
|
||||
KNNK, // KNN vs K
|
||||
KXK, // Generic "mate lone king" eval
|
||||
KBNK, // KBN vs K
|
||||
KPK, // KP vs K
|
||||
@@ -42,7 +43,6 @@ enum EndgameType {
|
||||
KQKP, // KQ vs KP
|
||||
KQKR, // KQ vs KR
|
||||
KBBKN, // KBB vs KN
|
||||
KNNK, // KNN vs K
|
||||
KmmKm, // K and two minors vs K and one or two minors
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@ enum EndgameType {
|
||||
KBPsK, // KB+pawns vs K
|
||||
KQKRPs, // KQ vs KR+pawns
|
||||
KRPKR, // KRP vs KR
|
||||
KRPKB, // KRP vs KB
|
||||
KRPPKRP, // KRPP vs KRP
|
||||
KPsK, // King and pawns vs king
|
||||
KBPKB, // KBP vs KB
|
||||
@@ -85,12 +86,12 @@ struct EndgameBase {
|
||||
template<EndgameType E, typename T = typename eg_fun<(E > SCALE_FUNS)>::type>
|
||||
struct Endgame : public EndgameBase<T> {
|
||||
|
||||
explicit Endgame(Color c) : strongerSide(c), weakerSide(~c) {}
|
||||
Color color() const { return strongerSide; }
|
||||
explicit Endgame(Color c) : strongSide(c), weakSide(~c) {}
|
||||
Color color() const { return strongSide; }
|
||||
T operator()(const Position&) const;
|
||||
|
||||
private:
|
||||
Color strongerSide, weakerSide;
|
||||
const Color strongSide, weakSide;
|
||||
};
|
||||
|
||||
|
||||
@@ -119,4 +120,4 @@ public:
|
||||
{ return eg = map(eg).count(key) ? map(eg)[key] : NULL; }
|
||||
};
|
||||
|
||||
#endif // !defined(ENDGAME_H_INCLUDED)
|
||||
#endif // #ifndef ENDGAME_H_INCLUDED
|
||||
|
||||
+398
-575
File diff suppressed because it is too large
Load Diff
+3
-3
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(EVALUATE_H_INCLUDED)
|
||||
#ifndef EVALUATE_H_INCLUDED
|
||||
#define EVALUATE_H_INCLUDED
|
||||
|
||||
#include "types.h"
|
||||
@@ -27,9 +27,9 @@ class Position;
|
||||
namespace Eval {
|
||||
|
||||
extern void init();
|
||||
extern Value evaluate(const Position& pos, Value& margin);
|
||||
extern Value evaluate(const Position& pos);
|
||||
extern std::string trace(const Position& pos);
|
||||
|
||||
}
|
||||
|
||||
#endif // !defined(EVALUATE_H_INCLUDED)
|
||||
#endif // #ifndef EVALUATE_H_INCLUDED
|
||||
|
||||
+3
-2
@@ -34,16 +34,17 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
UCI::init(Options);
|
||||
Bitboards::init();
|
||||
Zobrist::init();
|
||||
Position::init();
|
||||
Bitbases::init_kpk();
|
||||
Search::init();
|
||||
Pawns::init();
|
||||
Eval::init();
|
||||
Threads.init();
|
||||
TT.set_size(Options["Hash"]);
|
||||
|
||||
std::string args;
|
||||
|
||||
for (int i = 1; i < argc; i++)
|
||||
for (int i = 1; i < argc; ++i)
|
||||
args += std::string(argv[i]) + " ";
|
||||
|
||||
UCI::loop(args);
|
||||
|
||||
+45
-52
@@ -35,31 +35,29 @@ namespace {
|
||||
const int NoPawnsSF[4] = { 6, 12, 32 };
|
||||
|
||||
// Polynomial material balance parameters
|
||||
const Value RedundantQueenPenalty = Value(320);
|
||||
const Value RedundantRookPenalty = Value(554);
|
||||
|
||||
// pair pawn knight bishop rook queen
|
||||
const int LinearCoefficients[6] = { 1617, -162, -1172, -190, 105, 26 };
|
||||
const int LinearCoefficients[6] = { 1852, -162, -1122, -183, 249, -52 };
|
||||
|
||||
const int QuadraticCoefficientsSameColor[][PIECE_TYPE_NB] = {
|
||||
// pair pawn knight bishop rook queen
|
||||
{ 7 }, // Bishop pair
|
||||
{ 0 }, // Bishop pair
|
||||
{ 39, 2 }, // Pawn
|
||||
{ 35, 271, -4 }, // Knight
|
||||
{ 7, 105, 4, 7 }, // Bishop
|
||||
{ -27, -2, 46, 100, 56 }, // Rook
|
||||
{ 58, 29, 83, 148, -3, -25 } // Queen
|
||||
{ 0, 105, 4, 0 }, // Bishop
|
||||
{ -27, -2, 46, 100, -141 }, // Rook
|
||||
{ 58, 29, 83, 148, -163, 0 } // Queen
|
||||
};
|
||||
|
||||
const int QuadraticCoefficientsOppositeColor[][PIECE_TYPE_NB] = {
|
||||
// THEIR PIECES
|
||||
// pair pawn knight bishop rook queen
|
||||
{ 41 }, // Bishop pair
|
||||
{ 37, 41 }, // Pawn
|
||||
{ 10, 62, 41 }, // Knight OUR PIECES
|
||||
{ 57, 64, 39, 41 }, // Bishop
|
||||
{ 50, 40, 23, -22, 41 }, // Rook
|
||||
{ 106, 101, 3, 151, 171, 41 } // Queen
|
||||
{ 0 }, // Bishop pair
|
||||
{ 37, 0 }, // Pawn
|
||||
{ 10, 62, 0 }, // Knight OUR PIECES
|
||||
{ 57, 64, 39, 0 }, // Bishop
|
||||
{ 50, 40, 23, -22, 0 }, // Rook
|
||||
{ 106, 101, 3, 151, 171, 0 } // Queen
|
||||
};
|
||||
|
||||
// Endgame evaluation and scaling functions accessed direcly and not through
|
||||
@@ -75,24 +73,24 @@ namespace {
|
||||
// Helper templates used to detect a given material distribution
|
||||
template<Color Us> bool is_KXK(const Position& pos) {
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
return pos.non_pawn_material(Them) == VALUE_ZERO
|
||||
&& pos.piece_count(Them, PAWN) == 0
|
||||
&& pos.non_pawn_material(Us) >= RookValueMg;
|
||||
return !pos.count<PAWN>(Them)
|
||||
&& pos.non_pawn_material(Them) == VALUE_ZERO
|
||||
&& pos.non_pawn_material(Us) >= RookValueMg;
|
||||
}
|
||||
|
||||
template<Color Us> bool is_KBPsKs(const Position& pos) {
|
||||
return pos.non_pawn_material(Us) == BishopValueMg
|
||||
&& pos.piece_count(Us, BISHOP) == 1
|
||||
&& pos.piece_count(Us, PAWN) >= 1;
|
||||
return pos.non_pawn_material(Us) == BishopValueMg
|
||||
&& pos.count<BISHOP>(Us) == 1
|
||||
&& pos.count<PAWN >(Us) >= 1;
|
||||
}
|
||||
|
||||
template<Color Us> bool is_KQKRPs(const Position& pos) {
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
return pos.piece_count(Us, PAWN) == 0
|
||||
&& pos.non_pawn_material(Us) == QueenValueMg
|
||||
&& pos.piece_count(Us, QUEEN) == 1
|
||||
&& pos.piece_count(Them, ROOK) == 1
|
||||
&& pos.piece_count(Them, PAWN) >= 1;
|
||||
return !pos.count<PAWN>(Us)
|
||||
&& pos.non_pawn_material(Us) == QueenValueMg
|
||||
&& pos.count<QUEEN>(Us) == 1
|
||||
&& pos.count<ROOK>(Them) == 1
|
||||
&& pos.count<PAWN>(Them) >= 1;
|
||||
}
|
||||
|
||||
/// imbalance() calculates imbalance comparing piece count of each
|
||||
@@ -106,14 +104,8 @@ namespace {
|
||||
int pt1, pt2, pc, v;
|
||||
int value = 0;
|
||||
|
||||
// Redundancy of major pieces, formula based on Kaufman's paper
|
||||
// "The Evaluation of Material Imbalances in Chess"
|
||||
if (pieceCount[Us][ROOK] > 0)
|
||||
value -= RedundantRookPenalty * (pieceCount[Us][ROOK] - 1)
|
||||
+ RedundantQueenPenalty * pieceCount[Us][QUEEN];
|
||||
|
||||
// Second-degree polynomial material imbalance by Tord Romstad
|
||||
for (pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; pt1++)
|
||||
for (pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1)
|
||||
{
|
||||
pc = pieceCount[Us][pt1];
|
||||
if (!pc)
|
||||
@@ -121,7 +113,7 @@ namespace {
|
||||
|
||||
v = LinearCoefficients[pt1];
|
||||
|
||||
for (pt2 = NO_PIECE_TYPE; pt2 <= pt1; pt2++)
|
||||
for (pt2 = NO_PIECE_TYPE; pt2 <= pt1; ++pt2)
|
||||
v += QuadraticCoefficientsSameColor[pt1][pt2] * pieceCount[Us][pt2]
|
||||
+ QuadraticCoefficientsOppositeColor[pt1][pt2] * pieceCount[Them][pt2];
|
||||
|
||||
@@ -150,7 +142,7 @@ Entry* probe(const Position& pos, Table& entries, Endgames& endgames) {
|
||||
if (e->key == key)
|
||||
return e;
|
||||
|
||||
memset(e, 0, sizeof(Entry));
|
||||
std::memset(e, 0, sizeof(Entry));
|
||||
e->key = key;
|
||||
e->factor[WHITE] = e->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL;
|
||||
e->gamePhase = game_phase(pos);
|
||||
@@ -180,8 +172,8 @@ Entry* probe(const Position& pos, Table& entries, Endgames& endgames) {
|
||||
assert((pos.pieces(WHITE, KNIGHT) | pos.pieces(WHITE, BISHOP)));
|
||||
assert((pos.pieces(BLACK, KNIGHT) | pos.pieces(BLACK, BISHOP)));
|
||||
|
||||
if ( pos.piece_count(WHITE, BISHOP) + pos.piece_count(WHITE, KNIGHT) <= 2
|
||||
&& pos.piece_count(BLACK, BISHOP) + pos.piece_count(BLACK, KNIGHT) <= 2)
|
||||
if ( pos.count<BISHOP>(WHITE) + pos.count<KNIGHT>(WHITE) <= 2
|
||||
&& pos.count<BISHOP>(BLACK) + pos.count<KNIGHT>(BLACK) <= 2)
|
||||
{
|
||||
e->evaluationFunction = &EvaluateKmmKm[pos.side_to_move()];
|
||||
return e;
|
||||
@@ -221,17 +213,17 @@ Entry* probe(const Position& pos, Table& entries, Endgames& endgames) {
|
||||
|
||||
if (npm_w + npm_b == VALUE_ZERO)
|
||||
{
|
||||
if (pos.piece_count(BLACK, PAWN) == 0)
|
||||
if (!pos.count<PAWN>(BLACK))
|
||||
{
|
||||
assert(pos.piece_count(WHITE, PAWN) >= 2);
|
||||
assert(pos.count<PAWN>(WHITE) >= 2);
|
||||
e->scalingFunction[WHITE] = &ScaleKPsK[WHITE];
|
||||
}
|
||||
else if (pos.piece_count(WHITE, PAWN) == 0)
|
||||
else if (!pos.count<PAWN>(WHITE))
|
||||
{
|
||||
assert(pos.piece_count(BLACK, PAWN) >= 2);
|
||||
assert(pos.count<PAWN>(BLACK) >= 2);
|
||||
e->scalingFunction[BLACK] = &ScaleKPsK[BLACK];
|
||||
}
|
||||
else if (pos.piece_count(WHITE, PAWN) == 1 && pos.piece_count(BLACK, PAWN) == 1)
|
||||
else if (pos.count<PAWN>(WHITE) == 1 && pos.count<PAWN>(BLACK) == 1)
|
||||
{
|
||||
// This is a special case because we set scaling functions
|
||||
// for both colors instead of only one.
|
||||
@@ -240,36 +232,37 @@ Entry* probe(const Position& pos, Table& entries, Endgames& endgames) {
|
||||
}
|
||||
}
|
||||
|
||||
// No pawns makes it difficult to win, even with a material advantage
|
||||
if (pos.piece_count(WHITE, PAWN) == 0 && npm_w - npm_b <= BishopValueMg)
|
||||
// No pawns makes it difficult to win, even with a material advantage. This
|
||||
// catches some trivial draws like KK, KBK and KNK
|
||||
if (!pos.count<PAWN>(WHITE) && npm_w - npm_b <= BishopValueMg)
|
||||
{
|
||||
e->factor[WHITE] = (uint8_t)
|
||||
(npm_w == npm_b || npm_w < RookValueMg ? 0 : NoPawnsSF[std::min(pos.piece_count(WHITE, BISHOP), 2)]);
|
||||
(npm_w == npm_b || npm_w < RookValueMg ? 0 : NoPawnsSF[std::min(pos.count<BISHOP>(WHITE), 2)]);
|
||||
}
|
||||
|
||||
if (pos.piece_count(BLACK, PAWN) == 0 && npm_b - npm_w <= BishopValueMg)
|
||||
if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg)
|
||||
{
|
||||
e->factor[BLACK] = (uint8_t)
|
||||
(npm_w == npm_b || npm_b < RookValueMg ? 0 : NoPawnsSF[std::min(pos.piece_count(BLACK, BISHOP), 2)]);
|
||||
(npm_w == npm_b || npm_b < RookValueMg ? 0 : NoPawnsSF[std::min(pos.count<BISHOP>(BLACK), 2)]);
|
||||
}
|
||||
|
||||
// Compute the space weight
|
||||
if (npm_w + npm_b >= 2 * QueenValueMg + 4 * RookValueMg + 2 * KnightValueMg)
|
||||
{
|
||||
int minorPieceCount = pos.piece_count(WHITE, KNIGHT) + pos.piece_count(WHITE, BISHOP)
|
||||
+ pos.piece_count(BLACK, KNIGHT) + pos.piece_count(BLACK, BISHOP);
|
||||
int minorPieceCount = pos.count<KNIGHT>(WHITE) + pos.count<BISHOP>(WHITE)
|
||||
+ pos.count<KNIGHT>(BLACK) + pos.count<BISHOP>(BLACK);
|
||||
|
||||
e->spaceWeight = minorPieceCount * minorPieceCount;
|
||||
e->spaceWeight = make_score(minorPieceCount * minorPieceCount, 0);
|
||||
}
|
||||
|
||||
// Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder
|
||||
// for the bishop pair "extended piece", this allow us to be more flexible
|
||||
// in defining bishop pair bonuses.
|
||||
const int pieceCount[COLOR_NB][PIECE_TYPE_NB] = {
|
||||
{ pos.piece_count(WHITE, BISHOP) > 1, pos.piece_count(WHITE, PAWN), pos.piece_count(WHITE, KNIGHT),
|
||||
pos.piece_count(WHITE, BISHOP) , pos.piece_count(WHITE, ROOK), pos.piece_count(WHITE, QUEEN) },
|
||||
{ pos.piece_count(BLACK, BISHOP) > 1, pos.piece_count(BLACK, PAWN), pos.piece_count(BLACK, KNIGHT),
|
||||
pos.piece_count(BLACK, BISHOP) , pos.piece_count(BLACK, ROOK), pos.piece_count(BLACK, QUEEN) } };
|
||||
{ pos.count<BISHOP>(WHITE) > 1, pos.count<PAWN>(WHITE), pos.count<KNIGHT>(WHITE),
|
||||
pos.count<BISHOP>(WHITE) , pos.count<ROOK>(WHITE), pos.count<QUEEN >(WHITE) },
|
||||
{ pos.count<BISHOP>(BLACK) > 1, pos.count<PAWN>(BLACK), pos.count<KNIGHT>(BLACK),
|
||||
pos.count<BISHOP>(BLACK) , pos.count<ROOK>(BLACK), pos.count<QUEEN >(BLACK) } };
|
||||
|
||||
e->value = (int16_t)((imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16);
|
||||
return e;
|
||||
|
||||
+4
-4
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(MATERIAL_H_INCLUDED)
|
||||
#ifndef MATERIAL_H_INCLUDED
|
||||
#define MATERIAL_H_INCLUDED
|
||||
|
||||
#include "endgame.h"
|
||||
@@ -39,7 +39,7 @@ namespace Material {
|
||||
struct Entry {
|
||||
|
||||
Score material_value() const { return make_score(value, value); }
|
||||
int space_weight() const { return spaceWeight; }
|
||||
Score space_weight() const { return spaceWeight; }
|
||||
Phase game_phase() const { return gamePhase; }
|
||||
bool specialized_eval_exists() const { return evaluationFunction != NULL; }
|
||||
Value evaluate(const Position& p) const { return (*evaluationFunction)(p); }
|
||||
@@ -50,7 +50,7 @@ struct Entry {
|
||||
uint8_t factor[COLOR_NB];
|
||||
EndgameBase<Value>* evaluationFunction;
|
||||
EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB];
|
||||
int spaceWeight;
|
||||
Score spaceWeight;
|
||||
Phase gamePhase;
|
||||
};
|
||||
|
||||
@@ -74,4 +74,4 @@ inline ScaleFactor Entry::scale_factor(const Position& pos, Color c) const {
|
||||
|
||||
}
|
||||
|
||||
#endif // !defined(MATERIAL_H_INCLUDED)
|
||||
#endif // #ifndef MATERIAL_H_INCLUDED
|
||||
|
||||
+13
-56
@@ -24,17 +24,11 @@
|
||||
#include "misc.h"
|
||||
#include "thread.h"
|
||||
|
||||
#if defined(__hpux)
|
||||
# include <sys/pstat.h>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Version number. If Version is left empty, then Tag plus current
|
||||
/// date, in the format DD-MM-YY, are used as a version number.
|
||||
|
||||
static const string Version = "3";
|
||||
static const string Tag = "";
|
||||
/// Version number. If Version is left empty, then compile date, in the
|
||||
/// format DD-MM-YY, is shown in engine_info.
|
||||
static const string Version = "DD";
|
||||
|
||||
|
||||
/// engine_info() returns the full name of the current Stockfish version. This
|
||||
@@ -45,43 +39,33 @@ static const string Tag = "";
|
||||
const string engine_info(bool to_uci) {
|
||||
|
||||
const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
|
||||
const string cpu64(Is64Bit ? " 64bit" : "");
|
||||
const string popcnt(HasPopCnt ? " SSE4.2" : "");
|
||||
|
||||
string month, day, year;
|
||||
stringstream s, date(__DATE__); // From compiler, format is "Sep 21 2008"
|
||||
|
||||
s << "Stockfish " << Version;
|
||||
s << "Stockfish " << Version << setfill('0');
|
||||
|
||||
if (Version.empty())
|
||||
{
|
||||
date >> month >> day >> year;
|
||||
|
||||
s << Tag << string(Tag.empty() ? "" : " ") << setfill('0') << setw(2) << day
|
||||
<< "-" << setw(2) << (1 + months.find(month) / 4) << "-" << year.substr(2);
|
||||
s << setw(2) << day << setw(2) << (1 + months.find(month) / 4) << year.substr(2);
|
||||
}
|
||||
|
||||
s << cpu64 << popcnt << (to_uci ? "\nid author ": " by ")
|
||||
s << (Is64Bit ? " 64" : "")
|
||||
<< (HasPopCnt ? " SSE4.2" : "")
|
||||
<< (to_uci ? "\nid author ": " by ")
|
||||
<< "Tord Romstad, Marco Costalba and Joona Kiiski";
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
|
||||
/// Convert system time to milliseconds. That's all we need.
|
||||
|
||||
Time::point Time::now() {
|
||||
sys_time_t t; system_time(&t); return time_to_msec(t);
|
||||
}
|
||||
|
||||
|
||||
/// Debug functions used mainly to collect run-time statistics
|
||||
|
||||
static uint64_t hits[2], means[2];
|
||||
|
||||
void dbg_hit_on(bool b) { hits[0]++; if (b) hits[1]++; }
|
||||
void dbg_hit_on(bool b) { ++hits[0]; if (b) ++hits[1]; }
|
||||
void dbg_hit_on_c(bool c, bool b) { if (c) dbg_hit_on(b); }
|
||||
void dbg_mean_of(int v) { means[0]++; means[1] += v; }
|
||||
void dbg_mean_of(int v) { ++means[0]; means[1] += v; }
|
||||
|
||||
void dbg_print() {
|
||||
|
||||
@@ -91,7 +75,7 @@ void dbg_print() {
|
||||
|
||||
if (means[0])
|
||||
cerr << "Total " << means[0] << " Mean "
|
||||
<< (float)means[1] / means[0] << endl;
|
||||
<< (double)means[1] / means[0] << endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -174,37 +158,12 @@ std::ostream& operator<<(std::ostream& os, SyncCout sc) {
|
||||
void start_logger(bool b) { Logger::start(b); }
|
||||
|
||||
|
||||
/// cpu_count() tries to detect the number of CPU cores
|
||||
|
||||
int cpu_count() {
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
SYSTEM_INFO s;
|
||||
GetSystemInfo(&s);
|
||||
return s.dwNumberOfProcessors;
|
||||
#else
|
||||
|
||||
# if defined(_SC_NPROCESSORS_ONLN)
|
||||
return sysconf(_SC_NPROCESSORS_ONLN);
|
||||
# elif defined(__hpux)
|
||||
struct pst_dynamic psd;
|
||||
if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1)
|
||||
return 1;
|
||||
return psd.psd_proc_cnt;
|
||||
# else
|
||||
return 1;
|
||||
# endif
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/// timed_wait() waits for msec milliseconds. It is mainly an helper to wrap
|
||||
/// conversion from milliseconds to struct timespec, as used by pthreads.
|
||||
|
||||
void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) {
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#ifdef _WIN32
|
||||
int tm = msec;
|
||||
#else
|
||||
timespec ts, *tm = &ts;
|
||||
@@ -221,7 +180,7 @@ void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) {
|
||||
/// prefetch() preloads the given address in L1/L2 cache. This is a non
|
||||
/// blocking function and do not stalls the CPU waiting for data to be
|
||||
/// loaded from memory, that can be quite slow.
|
||||
#if defined(NO_PREFETCH)
|
||||
#ifdef NO_PREFETCH
|
||||
|
||||
void prefetch(char*) {}
|
||||
|
||||
@@ -237,10 +196,8 @@ void prefetch(char* addr) {
|
||||
|
||||
# if defined(__INTEL_COMPILER) || defined(_MSC_VER)
|
||||
_mm_prefetch(addr, _MM_HINT_T0);
|
||||
_mm_prefetch(addr+64, _MM_HINT_T0); // 64 bytes ahead
|
||||
# else
|
||||
__builtin_prefetch(addr);
|
||||
__builtin_prefetch(addr+64);
|
||||
# endif
|
||||
}
|
||||
|
||||
|
||||
+3
-4
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(MISC_H_INCLUDED)
|
||||
#ifndef MISC_H_INCLUDED
|
||||
#define MISC_H_INCLUDED
|
||||
|
||||
#include <fstream>
|
||||
@@ -27,7 +27,6 @@
|
||||
#include "types.h"
|
||||
|
||||
extern const std::string engine_info(bool to_uci = false);
|
||||
extern int cpu_count();
|
||||
extern void timed_wait(WaitCondition&, Lock&, int);
|
||||
extern void prefetch(char* addr);
|
||||
extern void start_logger(bool b);
|
||||
@@ -46,7 +45,7 @@ struct Log : public std::ofstream {
|
||||
|
||||
namespace Time {
|
||||
typedef int64_t point;
|
||||
point now();
|
||||
inline point now() { return system_time_to_msec(); }
|
||||
}
|
||||
|
||||
|
||||
@@ -66,4 +65,4 @@ std::ostream& operator<<(std::ostream&, SyncCout);
|
||||
#define sync_cout std::cout << io_lock
|
||||
#define sync_endl std::endl << io_unlock
|
||||
|
||||
#endif // !defined(MISC_H_INCLUDED)
|
||||
#endif // #ifndef MISC_H_INCLUDED
|
||||
|
||||
+76
-105
@@ -24,15 +24,15 @@
|
||||
|
||||
/// Simple macro to wrap a very common while loop, no facny, no flexibility,
|
||||
/// hardcoded names 'mlist' and 'from'.
|
||||
#define SERIALIZE(b) while (b) (*mlist++).move = make_move(from, pop_lsb(&b))
|
||||
#define SERIALIZE(b) while (b) (mlist++)->move = make_move(from, pop_lsb(&b))
|
||||
|
||||
/// Version used for pawns, where the 'from' square is given as a delta from the 'to' square
|
||||
#define SERIALIZE_PAWNS(b, d) while (b) { Square to = pop_lsb(&b); \
|
||||
(*mlist++).move = make_move(to - (d), to); }
|
||||
(mlist++)->move = make_move(to - (d), to); }
|
||||
namespace {
|
||||
|
||||
template<CastlingSide Side, bool Checks, bool Chess960>
|
||||
MoveStack* generate_castle(const Position& pos, MoveStack* mlist, Color us) {
|
||||
ExtMove* generate_castle(const Position& pos, ExtMove* mlist, Color us) {
|
||||
|
||||
if (pos.castle_impeded(us, Side) || !pos.can_castle(make_castle_right(us, Side)))
|
||||
return mlist;
|
||||
@@ -56,54 +56,42 @@ namespace {
|
||||
// Because we generate only legal castling moves we need to verify that
|
||||
// when moving the castling rook we do not discover some hidden checker.
|
||||
// For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
|
||||
if (Chess960 && (pos.attackers_to(kto, pos.pieces() ^ rfrom) & enemies))
|
||||
if (Chess960 && (attacks_bb<ROOK>(kto, pos.pieces() ^ rfrom) & pos.pieces(~us, ROOK, QUEEN)))
|
||||
return mlist;
|
||||
|
||||
(*mlist++).move = make<CASTLE>(kfrom, rfrom);
|
||||
(mlist++)->move = make<CASTLE>(kfrom, rfrom);
|
||||
|
||||
if (Checks && !pos.move_gives_check((mlist - 1)->move, CheckInfo(pos)))
|
||||
mlist--;
|
||||
if (Checks && !pos.gives_check((mlist - 1)->move, CheckInfo(pos)))
|
||||
--mlist;
|
||||
|
||||
return mlist;
|
||||
}
|
||||
|
||||
|
||||
template<Square Delta>
|
||||
inline Bitboard move_pawns(Bitboard p) {
|
||||
|
||||
return Delta == DELTA_N ? p << 8
|
||||
: Delta == DELTA_S ? p >> 8
|
||||
: Delta == DELTA_NE ? (p & ~FileHBB) << 9
|
||||
: Delta == DELTA_SE ? (p & ~FileHBB) >> 7
|
||||
: Delta == DELTA_NW ? (p & ~FileABB) << 7
|
||||
: Delta == DELTA_SW ? (p & ~FileABB) >> 9 : 0;
|
||||
}
|
||||
|
||||
|
||||
template<GenType Type, Square Delta>
|
||||
inline MoveStack* generate_promotions(MoveStack* mlist, Bitboard pawnsOn7,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
inline ExtMove* generate_promotions(ExtMove* mlist, Bitboard pawnsOn7,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
|
||||
Bitboard b = move_pawns<Delta>(pawnsOn7) & target;
|
||||
Bitboard b = shift_bb<Delta>(pawnsOn7) & target;
|
||||
|
||||
while (b)
|
||||
{
|
||||
Square to = pop_lsb(&b);
|
||||
|
||||
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
||||
(*mlist++).move = make<PROMOTION>(to - Delta, to, QUEEN);
|
||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, QUEEN);
|
||||
|
||||
if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
|
||||
{
|
||||
(*mlist++).move = make<PROMOTION>(to - Delta, to, ROOK);
|
||||
(*mlist++).move = make<PROMOTION>(to - Delta, to, BISHOP);
|
||||
(*mlist++).move = make<PROMOTION>(to - Delta, to, KNIGHT);
|
||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, ROOK);
|
||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, BISHOP);
|
||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
|
||||
}
|
||||
|
||||
// Knight-promotion is the only one that can give a direct check not
|
||||
// already included in the queen-promotion.
|
||||
if (Type == QUIET_CHECKS && (StepAttacksBB[W_KNIGHT][to] & ci->ksq))
|
||||
(*mlist++).move = make<PROMOTION>(to - Delta, to, KNIGHT);
|
||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
|
||||
else
|
||||
(void)ci; // Silence a warning under MSVC
|
||||
}
|
||||
@@ -113,8 +101,8 @@ namespace {
|
||||
|
||||
|
||||
template<Color Us, GenType Type>
|
||||
MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
ExtMove* generate_pawn_moves(const Position& pos, ExtMove* mlist,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
|
||||
// Compute our parametrized parameters at compile time, named according to
|
||||
// the point of view of white side.
|
||||
@@ -122,9 +110,9 @@ namespace {
|
||||
const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
|
||||
const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
|
||||
const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
|
||||
const Square UP = (Us == WHITE ? DELTA_N : DELTA_S);
|
||||
const Square RIGHT = (Us == WHITE ? DELTA_NE : DELTA_SW);
|
||||
const Square LEFT = (Us == WHITE ? DELTA_NW : DELTA_SE);
|
||||
const Square Up = (Us == WHITE ? DELTA_N : DELTA_S);
|
||||
const Square Right = (Us == WHITE ? DELTA_NE : DELTA_SW);
|
||||
const Square Left = (Us == WHITE ? DELTA_NW : DELTA_SE);
|
||||
|
||||
Bitboard b1, b2, dc1, dc2, emptySquares;
|
||||
|
||||
@@ -139,8 +127,8 @@ namespace {
|
||||
{
|
||||
emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
|
||||
|
||||
b1 = move_pawns<UP>(pawnsNotOn7) & emptySquares;
|
||||
b2 = move_pawns<UP>(b1 & TRank3BB) & emptySquares;
|
||||
b1 = shift_bb<Up>(pawnsNotOn7) & emptySquares;
|
||||
b2 = shift_bb<Up>(b1 & TRank3BB) & emptySquares;
|
||||
|
||||
if (Type == EVASIONS) // Consider only blocking squares
|
||||
{
|
||||
@@ -159,16 +147,16 @@ namespace {
|
||||
// promotion has been already generated among captures.
|
||||
if (pawnsNotOn7 & ci->dcCandidates)
|
||||
{
|
||||
dc1 = move_pawns<UP>(pawnsNotOn7 & ci->dcCandidates) & emptySquares & ~file_bb(ci->ksq);
|
||||
dc2 = move_pawns<UP>(dc1 & TRank3BB) & emptySquares;
|
||||
dc1 = shift_bb<Up>(pawnsNotOn7 & ci->dcCandidates) & emptySquares & ~file_bb(ci->ksq);
|
||||
dc2 = shift_bb<Up>(dc1 & TRank3BB) & emptySquares;
|
||||
|
||||
b1 |= dc1;
|
||||
b2 |= dc2;
|
||||
}
|
||||
}
|
||||
|
||||
SERIALIZE_PAWNS(b1, UP);
|
||||
SERIALIZE_PAWNS(b2, UP + UP);
|
||||
SERIALIZE_PAWNS(b1, Up);
|
||||
SERIALIZE_PAWNS(b2, Up + Up);
|
||||
}
|
||||
|
||||
// Promotions and underpromotions
|
||||
@@ -180,19 +168,19 @@ namespace {
|
||||
if (Type == EVASIONS)
|
||||
emptySquares &= target;
|
||||
|
||||
mlist = generate_promotions<Type, RIGHT>(mlist, pawnsOn7, enemies, ci);
|
||||
mlist = generate_promotions<Type, LEFT>(mlist, pawnsOn7, enemies, ci);
|
||||
mlist = generate_promotions<Type, UP>(mlist, pawnsOn7, emptySquares, ci);
|
||||
mlist = generate_promotions<Type, Right>(mlist, pawnsOn7, enemies, ci);
|
||||
mlist = generate_promotions<Type, Left >(mlist, pawnsOn7, enemies, ci);
|
||||
mlist = generate_promotions<Type, Up>(mlist, pawnsOn7, emptySquares, ci);
|
||||
}
|
||||
|
||||
// Standard and en-passant captures
|
||||
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
||||
{
|
||||
b1 = move_pawns<RIGHT>(pawnsNotOn7) & enemies;
|
||||
b2 = move_pawns<LEFT >(pawnsNotOn7) & enemies;
|
||||
b1 = shift_bb<Right>(pawnsNotOn7) & enemies;
|
||||
b2 = shift_bb<Left >(pawnsNotOn7) & enemies;
|
||||
|
||||
SERIALIZE_PAWNS(b1, RIGHT);
|
||||
SERIALIZE_PAWNS(b2, LEFT);
|
||||
SERIALIZE_PAWNS(b1, Right);
|
||||
SERIALIZE_PAWNS(b2, Left);
|
||||
|
||||
if (pos.ep_square() != SQ_NONE)
|
||||
{
|
||||
@@ -201,7 +189,7 @@ namespace {
|
||||
// An en passant capture can be an evasion only if the checking piece
|
||||
// is the double pushed pawn and so is in the target. Otherwise this
|
||||
// is a discovery check and we are forced to do otherwise.
|
||||
if (Type == EVASIONS && !(target & (pos.ep_square() - UP)))
|
||||
if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
|
||||
return mlist;
|
||||
|
||||
b1 = pawnsNotOn7 & pos.attacks_from<PAWN>(pos.ep_square(), Them);
|
||||
@@ -209,7 +197,7 @@ namespace {
|
||||
assert(b1);
|
||||
|
||||
while (b1)
|
||||
(*mlist++).move = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
|
||||
(mlist++)->move = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,12 +206,12 @@ namespace {
|
||||
|
||||
|
||||
template<PieceType Pt, bool Checks> FORCE_INLINE
|
||||
MoveStack* generate_moves(const Position& pos, MoveStack* mlist, Color us,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
ExtMove* generate_moves(const Position& pos, ExtMove* mlist, Color us,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
|
||||
assert(Pt != KING && Pt != PAWN);
|
||||
|
||||
const Square* pl = pos.piece_list(us, Pt);
|
||||
const Square* pl = pos.list<Pt>(us);
|
||||
|
||||
for (Square from = *pl; from != SQ_NONE; from = *++pl)
|
||||
{
|
||||
@@ -233,7 +221,7 @@ namespace {
|
||||
&& !(PseudoAttacks[Pt][from] & target & ci->checkSq[Pt]))
|
||||
continue;
|
||||
|
||||
if (ci->dcCandidates && (ci->dcCandidates & from))
|
||||
if (unlikely(ci->dcCandidates) && (ci->dcCandidates & from))
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -249,38 +237,36 @@ namespace {
|
||||
}
|
||||
|
||||
|
||||
template<GenType Type> FORCE_INLINE
|
||||
MoveStack* generate_all(const Position& pos, MoveStack* mlist, Color us,
|
||||
Bitboard target, const CheckInfo* ci = NULL) {
|
||||
template<Color Us, GenType Type> FORCE_INLINE
|
||||
ExtMove* generate_all(const Position& pos, ExtMove* mlist, Bitboard target,
|
||||
const CheckInfo* ci = NULL) {
|
||||
|
||||
const bool Checks = Type == QUIET_CHECKS;
|
||||
|
||||
mlist = (us == WHITE ? generate_pawn_moves<WHITE, Type>(pos, mlist, target, ci)
|
||||
: generate_pawn_moves<BLACK, Type>(pos, mlist, target, ci));
|
||||
|
||||
mlist = generate_moves<KNIGHT, Checks>(pos, mlist, us, target, ci);
|
||||
mlist = generate_moves<BISHOP, Checks>(pos, mlist, us, target, ci);
|
||||
mlist = generate_moves<ROOK, Checks>(pos, mlist, us, target, ci);
|
||||
mlist = generate_moves<QUEEN, Checks>(pos, mlist, us, target, ci);
|
||||
mlist = generate_pawn_moves<Us, Type>(pos, mlist, target, ci);
|
||||
mlist = generate_moves<KNIGHT, Checks>(pos, mlist, Us, target, ci);
|
||||
mlist = generate_moves<BISHOP, Checks>(pos, mlist, Us, target, ci);
|
||||
mlist = generate_moves< ROOK, Checks>(pos, mlist, Us, target, ci);
|
||||
mlist = generate_moves< QUEEN, Checks>(pos, mlist, Us, target, ci);
|
||||
|
||||
if (Type != QUIET_CHECKS && Type != EVASIONS)
|
||||
{
|
||||
Square from = pos.king_square(us);
|
||||
Square from = pos.king_square(Us);
|
||||
Bitboard b = pos.attacks_from<KING>(from) & target;
|
||||
SERIALIZE(b);
|
||||
}
|
||||
|
||||
if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(us))
|
||||
if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(Us))
|
||||
{
|
||||
if (pos.is_chess960())
|
||||
{
|
||||
mlist = generate_castle<KING_SIDE, Checks, true>(pos, mlist, us);
|
||||
mlist = generate_castle<QUEEN_SIDE, Checks, true>(pos, mlist, us);
|
||||
mlist = generate_castle< KING_SIDE, Checks, true>(pos, mlist, Us);
|
||||
mlist = generate_castle<QUEEN_SIDE, Checks, true>(pos, mlist, Us);
|
||||
}
|
||||
else
|
||||
{
|
||||
mlist = generate_castle<KING_SIDE, Checks, false>(pos, mlist, us);
|
||||
mlist = generate_castle<QUEEN_SIDE, Checks, false>(pos, mlist, us);
|
||||
mlist = generate_castle< KING_SIDE, Checks, false>(pos, mlist, Us);
|
||||
mlist = generate_castle<QUEEN_SIDE, Checks, false>(pos, mlist, Us);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,7 +287,7 @@ namespace {
|
||||
/// non-captures. Returns a pointer to the end of the move list.
|
||||
|
||||
template<GenType Type>
|
||||
MoveStack* generate(const Position& pos, MoveStack* mlist) {
|
||||
ExtMove* generate(const Position& pos, ExtMove* mlist) {
|
||||
|
||||
assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS);
|
||||
assert(!pos.checkers());
|
||||
@@ -312,22 +298,24 @@ MoveStack* generate(const Position& pos, MoveStack* mlist) {
|
||||
: Type == QUIETS ? ~pos.pieces()
|
||||
: Type == NON_EVASIONS ? ~pos.pieces(us) : 0;
|
||||
|
||||
return generate_all<Type>(pos, mlist, us, target);
|
||||
return us == WHITE ? generate_all<WHITE, Type>(pos, mlist, target)
|
||||
: generate_all<BLACK, Type>(pos, mlist, target);
|
||||
}
|
||||
|
||||
// Explicit template instantiations
|
||||
template MoveStack* generate<CAPTURES>(const Position&, MoveStack*);
|
||||
template MoveStack* generate<QUIETS>(const Position&, MoveStack*);
|
||||
template MoveStack* generate<NON_EVASIONS>(const Position&, MoveStack*);
|
||||
template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
|
||||
template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
|
||||
template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
|
||||
|
||||
|
||||
/// generate<QUIET_CHECKS> generates all pseudo-legal non-captures and knight
|
||||
/// underpromotions that give check. Returns a pointer to the end of the move list.
|
||||
template<>
|
||||
MoveStack* generate<QUIET_CHECKS>(const Position& pos, MoveStack* mlist) {
|
||||
ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* mlist) {
|
||||
|
||||
assert(!pos.checkers());
|
||||
|
||||
Color us = pos.side_to_move();
|
||||
CheckInfo ci(pos);
|
||||
Bitboard dc = ci.dcCandidates;
|
||||
|
||||
@@ -347,21 +335,21 @@ MoveStack* generate<QUIET_CHECKS>(const Position& pos, MoveStack* mlist) {
|
||||
SERIALIZE(b);
|
||||
}
|
||||
|
||||
return generate_all<QUIET_CHECKS>(pos, mlist, pos.side_to_move(), ~pos.pieces(), &ci);
|
||||
return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, mlist, ~pos.pieces(), &ci)
|
||||
: generate_all<BLACK, QUIET_CHECKS>(pos, mlist, ~pos.pieces(), &ci);
|
||||
}
|
||||
|
||||
|
||||
/// generate<EVASIONS> generates all pseudo-legal check evasions when the side
|
||||
/// to move is in check. Returns a pointer to the end of the move list.
|
||||
template<>
|
||||
MoveStack* generate<EVASIONS>(const Position& pos, MoveStack* mlist) {
|
||||
ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* mlist) {
|
||||
|
||||
assert(pos.checkers());
|
||||
|
||||
Square from, checksq;
|
||||
int checkersCnt = 0;
|
||||
Color us = pos.side_to_move();
|
||||
Square ksq = pos.king_square(us);
|
||||
Square ksq = pos.king_square(us), from = ksq /* For SERIALIZE */, checksq;
|
||||
Bitboard sliderAttacks = 0;
|
||||
Bitboard b = pos.checkers();
|
||||
|
||||
@@ -371,65 +359,48 @@ MoveStack* generate<EVASIONS>(const Position& pos, MoveStack* mlist) {
|
||||
// evasions so to skip known illegal moves avoiding useless legality check later.
|
||||
do
|
||||
{
|
||||
checkersCnt++;
|
||||
++checkersCnt;
|
||||
checksq = pop_lsb(&b);
|
||||
|
||||
assert(color_of(pos.piece_on(checksq)) == ~us);
|
||||
|
||||
switch (type_of(pos.piece_on(checksq)))
|
||||
{
|
||||
case BISHOP: sliderAttacks |= PseudoAttacks[BISHOP][checksq]; break;
|
||||
case ROOK: sliderAttacks |= PseudoAttacks[ROOK][checksq]; break;
|
||||
case QUEEN:
|
||||
// If queen and king are far or not on a diagonal line we can safely
|
||||
// remove all the squares attacked in the other direction becuase are
|
||||
// not reachable by the king anyway.
|
||||
if (between_bb(ksq, checksq) || !(PseudoAttacks[BISHOP][checksq] & ksq))
|
||||
sliderAttacks |= PseudoAttacks[QUEEN][checksq];
|
||||
if (type_of(pos.piece_on(checksq)) > KNIGHT) // A slider
|
||||
sliderAttacks |= LineBB[checksq][ksq] ^ checksq;
|
||||
|
||||
// Otherwise we need to use real rook attacks to check if king is safe
|
||||
// to move in the other direction. For example: king in B2, queen in A1
|
||||
// a knight in B1, and we can safely move to C1.
|
||||
else
|
||||
sliderAttacks |= PseudoAttacks[BISHOP][checksq] | pos.attacks_from<ROOK>(checksq);
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} while (b);
|
||||
|
||||
// Generate evasions for king, capture and non capture moves
|
||||
b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
|
||||
from = ksq;
|
||||
SERIALIZE(b);
|
||||
|
||||
if (checkersCnt > 1)
|
||||
return mlist; // Double check, only a king move can save the day
|
||||
|
||||
// Generate blocking evasions or captures of the checking piece
|
||||
Bitboard target = between_bb(checksq, ksq) | pos.checkers();
|
||||
Bitboard target = between_bb(checksq, ksq) | checksq;
|
||||
|
||||
return generate_all<EVASIONS>(pos, mlist, us, target);
|
||||
return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, mlist, target)
|
||||
: generate_all<BLACK, EVASIONS>(pos, mlist, target);
|
||||
}
|
||||
|
||||
|
||||
/// generate<LEGAL> generates all the legal moves in the given position
|
||||
|
||||
template<>
|
||||
MoveStack* generate<LEGAL>(const Position& pos, MoveStack* mlist) {
|
||||
ExtMove* generate<LEGAL>(const Position& pos, ExtMove* mlist) {
|
||||
|
||||
MoveStack *end, *cur = mlist;
|
||||
Bitboard pinned = pos.pinned_pieces();
|
||||
ExtMove *end, *cur = mlist;
|
||||
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
|
||||
Square ksq = pos.king_square(pos.side_to_move());
|
||||
|
||||
end = pos.checkers() ? generate<EVASIONS>(pos, mlist)
|
||||
: generate<NON_EVASIONS>(pos, mlist);
|
||||
while (cur != end)
|
||||
if ( (pinned || from_sq(cur->move) == ksq || type_of(cur->move) == ENPASSANT)
|
||||
&& !pos.pl_move_is_legal(cur->move, pinned))
|
||||
&& !pos.legal(cur->move, pinned))
|
||||
cur->move = (--end)->move;
|
||||
else
|
||||
cur++;
|
||||
++cur;
|
||||
|
||||
return end;
|
||||
}
|
||||
|
||||
+9
-10
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(MOVEGEN_H_INCLUDED)
|
||||
#ifndef MOVEGEN_H_INCLUDED
|
||||
#define MOVEGEN_H_INCLUDED
|
||||
|
||||
#include "types.h"
|
||||
@@ -34,26 +34,25 @@ enum GenType {
|
||||
class Position;
|
||||
|
||||
template<GenType>
|
||||
MoveStack* generate(const Position& pos, MoveStack* mlist);
|
||||
ExtMove* generate(const Position& pos, ExtMove* mlist);
|
||||
|
||||
/// The MoveList struct is a simple wrapper around generate(), sometimes comes
|
||||
/// handy to use this class instead of the low level generate() function.
|
||||
template<GenType T>
|
||||
struct MoveList {
|
||||
|
||||
explicit MoveList(const Position& pos) : cur(mlist), last(generate<T>(pos, mlist)) {}
|
||||
void operator++() { cur++; }
|
||||
bool end() const { return cur == last; }
|
||||
Move move() const { return cur->move; }
|
||||
explicit MoveList(const Position& pos) : cur(mlist), last(generate<T>(pos, mlist)) { last->move = MOVE_NONE; }
|
||||
void operator++() { ++cur; }
|
||||
Move operator*() const { return cur->move; }
|
||||
size_t size() const { return last - mlist; }
|
||||
bool contains(Move m) const {
|
||||
for (const MoveStack* it(mlist); it != last; ++it) if (it->move == m) return true;
|
||||
for (const ExtMove* it(mlist); it != last; ++it) if (it->move == m) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
MoveStack mlist[MAX_MOVES];
|
||||
MoveStack *cur, *last;
|
||||
ExtMove mlist[MAX_MOVES];
|
||||
ExtMove *cur, *last;
|
||||
};
|
||||
|
||||
#endif // !defined(MOVEGEN_H_INCLUDED)
|
||||
#endif // #ifndef MOVEGEN_H_INCLUDED
|
||||
|
||||
+56
-56
@@ -25,7 +25,7 @@
|
||||
|
||||
namespace {
|
||||
|
||||
enum Sequencer {
|
||||
enum Stages {
|
||||
MAIN_SEARCH, CAPTURES_S1, KILLERS_S1, QUIETS_1_S1, QUIETS_2_S1, BAD_CAPTURES_S1,
|
||||
EVASION, EVASIONS_S2,
|
||||
QSEARCH_0, CAPTURES_S3, QUIET_CHECKS_S3,
|
||||
@@ -36,9 +36,9 @@ namespace {
|
||||
};
|
||||
|
||||
// Our insertion sort, guaranteed to be stable, as is needed
|
||||
void insertion_sort(MoveStack* begin, MoveStack* end)
|
||||
void insertion_sort(ExtMove* begin, ExtMove* end)
|
||||
{
|
||||
MoveStack tmp, *p, *q;
|
||||
ExtMove tmp, *p, *q;
|
||||
|
||||
for (p = begin + 1; p < end; ++p)
|
||||
{
|
||||
@@ -51,12 +51,12 @@ namespace {
|
||||
|
||||
// Unary predicate used by std::partition to split positive scores from remaining
|
||||
// ones so to sort separately the two sets, and with the second sort delayed.
|
||||
inline bool has_positive_score(const MoveStack& ms) { return ms.score > 0; }
|
||||
inline bool has_positive_score(const ExtMove& ms) { return ms.score > 0; }
|
||||
|
||||
// Picks and moves to the front the best move in the range [begin, end),
|
||||
// it is faster than sorting all the moves in advance when moves are few, as
|
||||
// normally are the possible captures.
|
||||
inline MoveStack* pick_best(MoveStack* begin, MoveStack* end)
|
||||
inline ExtMove* pick_best(ExtMove* begin, ExtMove* end)
|
||||
{
|
||||
std::swap(*begin, *std::max_element(begin, end));
|
||||
return begin;
|
||||
@@ -70,83 +70,70 @@ namespace {
|
||||
/// search captures, promotions and some checks) and about how important good
|
||||
/// move ordering is at the current node.
|
||||
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
|
||||
Search::Stack* s, Value beta) : pos(p), Hist(h), depth(d) {
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
|
||||
Move* cm, Search::Stack* s) : pos(p), history(h), depth(d) {
|
||||
|
||||
assert(d > DEPTH_ZERO);
|
||||
|
||||
captureThreshold = 0;
|
||||
cur = end = moves;
|
||||
endBadCaptures = moves + MAX_MOVES - 1;
|
||||
countermoves = cm;
|
||||
ss = s;
|
||||
|
||||
if (p.checkers())
|
||||
phase = EVASION;
|
||||
stage = EVASION;
|
||||
|
||||
else
|
||||
{
|
||||
phase = MAIN_SEARCH;
|
||||
stage = MAIN_SEARCH;
|
||||
|
||||
killers[0].move = ss->killers[0];
|
||||
killers[1].move = ss->killers[1];
|
||||
|
||||
// Consider sligtly negative captures as good if at low depth and far from beta
|
||||
if (ss && ss->staticEval < beta - PawnValueMg && d < 3 * ONE_PLY)
|
||||
captureThreshold = -PawnValueMg;
|
||||
|
||||
// Consider negative captures as good if still enough to reach beta
|
||||
else if (ss && ss->staticEval > beta)
|
||||
captureThreshold = beta - ss->staticEval;
|
||||
}
|
||||
|
||||
ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
end += (ttMove != MOVE_NONE);
|
||||
}
|
||||
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
|
||||
Square sq) : pos(p), Hist(h), cur(moves), end(moves) {
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
|
||||
Square sq) : pos(p), history(h), cur(moves), end(moves) {
|
||||
|
||||
assert(d <= DEPTH_ZERO);
|
||||
|
||||
if (p.checkers())
|
||||
phase = EVASION;
|
||||
stage = EVASION;
|
||||
|
||||
else if (d > DEPTH_QS_NO_CHECKS)
|
||||
phase = QSEARCH_0;
|
||||
stage = QSEARCH_0;
|
||||
|
||||
else if (d > DEPTH_QS_RECAPTURES)
|
||||
{
|
||||
phase = QSEARCH_1;
|
||||
stage = QSEARCH_1;
|
||||
|
||||
// Skip TT move if is not a capture or a promotion, this avoids qsearch
|
||||
// tree explosion due to a possible perpetual check or similar rare cases
|
||||
// when TT table is full.
|
||||
if (ttm && !pos.is_capture_or_promotion(ttm))
|
||||
if (ttm && !pos.capture_or_promotion(ttm))
|
||||
ttm = MOVE_NONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
phase = RECAPTURE;
|
||||
stage = RECAPTURE;
|
||||
recaptureSquare = sq;
|
||||
ttm = MOVE_NONE;
|
||||
}
|
||||
|
||||
ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
end += (ttMove != MOVE_NONE);
|
||||
}
|
||||
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, const History& h, PieceType pt)
|
||||
: pos(p), Hist(h), cur(moves), end(moves) {
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, PieceType pt)
|
||||
: pos(p), history(h), cur(moves), end(moves) {
|
||||
|
||||
assert(!pos.checkers());
|
||||
|
||||
phase = PROBCUT;
|
||||
stage = PROBCUT;
|
||||
|
||||
// In ProbCut we generate only captures better than parent's captured piece
|
||||
captureThreshold = PieceValue[MG][pt];
|
||||
ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
|
||||
if (ttMove && (!pos.is_capture(ttMove) || pos.see(ttMove) <= captureThreshold))
|
||||
if (ttMove && (!pos.capture(ttMove) || pos.see(ttMove) <= captureThreshold))
|
||||
ttMove = MOVE_NONE;
|
||||
|
||||
end += (ttMove != MOVE_NONE);
|
||||
@@ -172,11 +159,11 @@ void MovePicker::score<CAPTURES>() {
|
||||
// some SEE calls in case we get a cutoff (idea from Pablo Vazquez).
|
||||
Move m;
|
||||
|
||||
for (MoveStack* it = moves; it != end; ++it)
|
||||
for (ExtMove* it = moves; it != end; ++it)
|
||||
{
|
||||
m = it->move;
|
||||
it->score = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||
- type_of(pos.piece_moved(m));
|
||||
- type_of(pos.moved_piece(m));
|
||||
|
||||
if (type_of(m) == PROMOTION)
|
||||
it->score += PieceValue[MG][promotion_type(m)] - PieceValue[MG][PAWN];
|
||||
@@ -191,10 +178,10 @@ void MovePicker::score<QUIETS>() {
|
||||
|
||||
Move m;
|
||||
|
||||
for (MoveStack* it = moves; it != end; ++it)
|
||||
for (ExtMove* it = moves; it != end; ++it)
|
||||
{
|
||||
m = it->move;
|
||||
it->score = Hist[pos.piece_moved(m)][to_sq(m)];
|
||||
it->score = history[pos.moved_piece(m)][to_sq(m)];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,17 +193,17 @@ void MovePicker::score<EVASIONS>() {
|
||||
Move m;
|
||||
int seeScore;
|
||||
|
||||
for (MoveStack* it = moves; it != end; ++it)
|
||||
for (ExtMove* it = moves; it != end; ++it)
|
||||
{
|
||||
m = it->move;
|
||||
if ((seeScore = pos.see_sign(m)) < 0)
|
||||
it->score = seeScore - History::Max; // At the bottom
|
||||
it->score = seeScore - HistoryStats::Max; // At the bottom
|
||||
|
||||
else if (pos.is_capture(m))
|
||||
else if (pos.capture(m))
|
||||
it->score = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||
- type_of(pos.piece_moved(m)) + History::Max;
|
||||
- type_of(pos.moved_piece(m)) + HistoryStats::Max;
|
||||
else
|
||||
it->score = Hist[pos.piece_moved(m)][to_sq(m)];
|
||||
it->score = history[pos.moved_piece(m)][to_sq(m)];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,7 +215,7 @@ void MovePicker::generate_next() {
|
||||
|
||||
cur = moves;
|
||||
|
||||
switch (++phase) {
|
||||
switch (++stage) {
|
||||
|
||||
case CAPTURES_S1: case CAPTURES_S3: case CAPTURES_S4: case CAPTURES_S5: case CAPTURES_S6:
|
||||
end = generate<CAPTURES>(pos, moves);
|
||||
@@ -238,6 +225,19 @@ void MovePicker::generate_next() {
|
||||
case KILLERS_S1:
|
||||
cur = killers;
|
||||
end = cur + 2;
|
||||
|
||||
killers[0].move = ss->killers[0];
|
||||
killers[1].move = ss->killers[1];
|
||||
killers[2].move = killers[3].move = MOVE_NONE;
|
||||
|
||||
// Be sure countermoves are different from killers
|
||||
for (int i = 0; i < 2; ++i)
|
||||
if (countermoves[i] != cur->move && countermoves[i] != (cur+1)->move)
|
||||
(end++)->move = countermoves[i];
|
||||
|
||||
if (countermoves[1] && countermoves[1] == countermoves[0]) // Due to SMP races
|
||||
killers[3].move = MOVE_NONE;
|
||||
|
||||
return;
|
||||
|
||||
case QUIETS_1_S1:
|
||||
@@ -271,7 +271,7 @@ void MovePicker::generate_next() {
|
||||
return;
|
||||
|
||||
case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT: case RECAPTURE:
|
||||
phase = STOP;
|
||||
stage = STOP;
|
||||
case STOP:
|
||||
end = cur + 1; // Avoid another next_phase() call
|
||||
return;
|
||||
@@ -296,19 +296,17 @@ Move MovePicker::next_move<false>() {
|
||||
while (cur == end)
|
||||
generate_next();
|
||||
|
||||
switch (phase) {
|
||||
switch (stage) {
|
||||
|
||||
case MAIN_SEARCH: case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT:
|
||||
cur++;
|
||||
++cur;
|
||||
return ttMove;
|
||||
|
||||
case CAPTURES_S1:
|
||||
move = pick_best(cur++, end)->move;
|
||||
if (move != ttMove)
|
||||
{
|
||||
assert(captureThreshold <= 0); // Otherwise we cannot use see_sign()
|
||||
|
||||
if (pos.see_sign(move) >= captureThreshold)
|
||||
if (pos.see_sign(move) >= 0)
|
||||
return move;
|
||||
|
||||
// Losing capture, move it to the tail of the array
|
||||
@@ -319,9 +317,9 @@ Move MovePicker::next_move<false>() {
|
||||
case KILLERS_S1:
|
||||
move = (cur++)->move;
|
||||
if ( move != MOVE_NONE
|
||||
&& pos.is_pseudo_legal(move)
|
||||
&& pos.pseudo_legal(move)
|
||||
&& move != ttMove
|
||||
&& !pos.is_capture(move))
|
||||
&& !pos.capture(move))
|
||||
return move;
|
||||
break;
|
||||
|
||||
@@ -329,7 +327,9 @@ Move MovePicker::next_move<false>() {
|
||||
move = (cur++)->move;
|
||||
if ( move != ttMove
|
||||
&& move != killers[0].move
|
||||
&& move != killers[1].move)
|
||||
&& move != killers[1].move
|
||||
&& move != killers[2].move
|
||||
&& move != killers[3].move)
|
||||
return move;
|
||||
break;
|
||||
|
||||
|
||||
+36
-24
@@ -17,11 +17,11 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined MOVEPICK_H_INCLUDED
|
||||
#ifndef MOVEPICK_H_INCLUDED
|
||||
#define MOVEPICK_H_INCLUDED
|
||||
|
||||
#include <algorithm> // For std::max
|
||||
#include <cstring> // For memset
|
||||
#include <cstring> // For std::memset
|
||||
|
||||
#include "movegen.h"
|
||||
#include "position.h"
|
||||
@@ -30,20 +30,29 @@
|
||||
|
||||
|
||||
/// The Stats struct stores moves statistics. According to the template parameter
|
||||
/// the class can store both History and Gains type statistics. History records
|
||||
/// how often different moves have been successful or unsuccessful during the
|
||||
/// current search and is used for reduction and move ordering decisions. Gains
|
||||
/// records the move's best evaluation gain from one ply to the next and is used
|
||||
/// for pruning decisions. Entries are stored according only to moving piece and
|
||||
/// destination square, in particular two moves with different origin but same
|
||||
/// destination and same piece will be considered identical.
|
||||
template<bool Gain>
|
||||
/// the class can store History, Gains and Countermoves. History records how often
|
||||
/// different moves have been successful or unsuccessful during the current search
|
||||
/// and is used for reduction and move ordering decisions. Gains records the move's
|
||||
/// best evaluation gain from one ply to the next and is used for pruning decisions.
|
||||
/// Countermoves store the move that refute a previous one. Entries are stored
|
||||
/// according only to moving piece and destination square, hence two moves with
|
||||
/// different origin but same destination and piece will be considered identical.
|
||||
template<bool Gain, typename T>
|
||||
struct Stats {
|
||||
|
||||
static const Value Max = Value(2000);
|
||||
|
||||
const Value* operator[](Piece p) const { return &table[p][0]; }
|
||||
void clear() { memset(table, 0, sizeof(table)); }
|
||||
const T* operator[](Piece p) const { return table[p]; }
|
||||
void clear() { std::memset(table, 0, sizeof(table)); }
|
||||
|
||||
void update(Piece p, Square to, Move m) {
|
||||
|
||||
if (m == table[p][to].first)
|
||||
return;
|
||||
|
||||
table[p][to].second = table[p][to].first;
|
||||
table[p][to].first = m;
|
||||
}
|
||||
|
||||
void update(Piece p, Square to, Value v) {
|
||||
|
||||
@@ -55,11 +64,12 @@ struct Stats {
|
||||
}
|
||||
|
||||
private:
|
||||
Value table[PIECE_NB][SQUARE_NB];
|
||||
T table[PIECE_NB][SQUARE_NB];
|
||||
};
|
||||
|
||||
typedef Stats<false> History;
|
||||
typedef Stats<true> Gains;
|
||||
typedef Stats< true, Value> GainsStats;
|
||||
typedef Stats<false, Value> HistoryStats;
|
||||
typedef Stats<false, std::pair<Move, Move> > CountermovesStats;
|
||||
|
||||
|
||||
/// MovePicker class is used to pick one pseudo legal move at a time from the
|
||||
@@ -74,9 +84,10 @@ class MovePicker {
|
||||
MovePicker& operator=(const MovePicker&); // Silence a warning under MSVC
|
||||
|
||||
public:
|
||||
MovePicker(const Position&, Move, Depth, const History&, Search::Stack*, Value);
|
||||
MovePicker(const Position&, Move, Depth, const History&, Square);
|
||||
MovePicker(const Position&, Move, const History&, PieceType);
|
||||
MovePicker(const Position&, Move, Depth, const HistoryStats&, Square);
|
||||
MovePicker(const Position&, Move, const HistoryStats&, PieceType);
|
||||
MovePicker(const Position&, Move, Depth, const HistoryStats&, Move*, Search::Stack*);
|
||||
|
||||
template<bool SpNode> Move next_move();
|
||||
|
||||
private:
|
||||
@@ -84,15 +95,16 @@ private:
|
||||
void generate_next();
|
||||
|
||||
const Position& pos;
|
||||
const History& Hist;
|
||||
const HistoryStats& history;
|
||||
Search::Stack* ss;
|
||||
Move* countermoves;
|
||||
Depth depth;
|
||||
Move ttMove;
|
||||
MoveStack killers[2];
|
||||
ExtMove killers[4];
|
||||
Square recaptureSquare;
|
||||
int captureThreshold, phase;
|
||||
MoveStack *cur, *end, *endQuiets, *endBadCaptures;
|
||||
MoveStack moves[MAX_MOVES];
|
||||
int captureThreshold, stage;
|
||||
ExtMove *cur, *end, *endQuiets, *endBadCaptures;
|
||||
ExtMove moves[MAX_MOVES];
|
||||
};
|
||||
|
||||
#endif // !defined(MOVEPICK_H_INCLUDED)
|
||||
#endif // #ifndef MOVEPICK_H_INCLUDED
|
||||
|
||||
+8
-8
@@ -89,9 +89,9 @@ Move move_from_uci(const Position& pos, string& str) {
|
||||
if (str.length() == 5) // Junior could send promotion piece in uppercase
|
||||
str[4] = char(tolower(str[4]));
|
||||
|
||||
for (MoveList<LEGAL> ml(pos); !ml.end(); ++ml)
|
||||
if (str == move_to_uci(ml.move(), pos.is_chess960()))
|
||||
return ml.move();
|
||||
for (MoveList<LEGAL> it(pos); *it; ++it)
|
||||
if (str == move_to_uci(*it, pos.is_chess960()))
|
||||
return *it;
|
||||
|
||||
return MOVE_NONE;
|
||||
}
|
||||
@@ -133,7 +133,7 @@ const string move_to_san(Position& pos, Move m) {
|
||||
while (b)
|
||||
{
|
||||
Move move = make_move(pop_lsb(&b), to);
|
||||
if (!pos.pl_move_is_legal(move, pos.pinned_pieces()))
|
||||
if (!pos.legal(move, pos.pinned_pieces(pos.side_to_move())))
|
||||
others ^= from_sq(move);
|
||||
}
|
||||
|
||||
@@ -149,10 +149,10 @@ const string move_to_san(Position& pos, Move m) {
|
||||
san += square_to_string(from);
|
||||
}
|
||||
}
|
||||
else if (pos.is_capture(m))
|
||||
else if (pos.capture(m))
|
||||
san = file_to_char(file_of(from));
|
||||
|
||||
if (pos.is_capture(m))
|
||||
if (pos.capture(m))
|
||||
san += 'x';
|
||||
|
||||
san += square_to_string(to);
|
||||
@@ -161,7 +161,7 @@ const string move_to_san(Position& pos, Move m) {
|
||||
san += string("=") + PieceToChar[WHITE][promotion_type(m)];
|
||||
}
|
||||
|
||||
if (pos.move_gives_check(m, CheckInfo(pos)))
|
||||
if (pos.gives_check(m, CheckInfo(pos)))
|
||||
{
|
||||
StateInfo st;
|
||||
pos.do_move(m, st);
|
||||
@@ -207,7 +207,7 @@ static string score_to_string(Value v) {
|
||||
s << "-#" << (VALUE_MATE + v) / 2;
|
||||
|
||||
else
|
||||
s << setprecision(2) << fixed << showpos << float(v) / PawnValueMg;
|
||||
s << setprecision(2) << fixed << showpos << double(v) / PawnValueMg;
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
+2
-2
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(NOTATION_H_INCLUDED)
|
||||
#ifndef NOTATION_H_INCLUDED
|
||||
#define NOTATION_H_INCLUDED
|
||||
|
||||
#include <string>
|
||||
@@ -32,4 +32,4 @@ const std::string move_to_uci(Move m, bool chess960);
|
||||
const std::string move_to_san(Position& pos, Move m);
|
||||
std::string pretty_pv(Position& pos, int depth, Value score, int64_t msecs, Move pv[]);
|
||||
|
||||
#endif // !defined(NOTATION_H_INCLUDED)
|
||||
#endif // #ifndef NOTATION_H_INCLUDED
|
||||
|
||||
+98
-102
@@ -17,6 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
#include "bitboard.h"
|
||||
@@ -29,71 +30,75 @@ namespace {
|
||||
#define V Value
|
||||
#define S(mg, eg) make_score(mg, eg)
|
||||
|
||||
// Doubled pawn penalty by opposed flag and file
|
||||
const Score DoubledPawnPenalty[2][FILE_NB] = {
|
||||
{ S(13, 43), S(20, 48), S(23, 48), S(23, 48),
|
||||
S(23, 48), S(23, 48), S(20, 48), S(13, 43) },
|
||||
{ S(13, 43), S(20, 48), S(23, 48), S(23, 48),
|
||||
S(23, 48), S(23, 48), S(20, 48), S(13, 43) }};
|
||||
// Doubled pawn penalty by file
|
||||
const Score Doubled[FILE_NB] = {
|
||||
S(13, 43), S(20, 48), S(23, 48), S(23, 48),
|
||||
S(23, 48), S(23, 48), S(20, 48), S(13, 43) };
|
||||
|
||||
// Isolated pawn penalty by opposed flag and file
|
||||
const Score IsolatedPawnPenalty[2][FILE_NB] = {
|
||||
const Score Isolated[2][FILE_NB] = {
|
||||
{ S(37, 45), S(54, 52), S(60, 52), S(60, 52),
|
||||
S(60, 52), S(60, 52), S(54, 52), S(37, 45) },
|
||||
{ S(25, 30), S(36, 35), S(40, 35), S(40, 35),
|
||||
S(40, 35), S(40, 35), S(36, 35), S(25, 30) }};
|
||||
S(40, 35), S(40, 35), S(36, 35), S(25, 30) } };
|
||||
|
||||
// Backward pawn penalty by opposed flag and file
|
||||
const Score BackwardPawnPenalty[2][FILE_NB] = {
|
||||
const Score Backward[2][FILE_NB] = {
|
||||
{ S(30, 42), S(43, 46), S(49, 46), S(49, 46),
|
||||
S(49, 46), S(49, 46), S(43, 46), S(30, 42) },
|
||||
{ S(20, 28), S(29, 31), S(33, 31), S(33, 31),
|
||||
S(33, 31), S(33, 31), S(29, 31), S(20, 28) }};
|
||||
S(33, 31), S(33, 31), S(29, 31), S(20, 28) } };
|
||||
|
||||
// Pawn chain membership bonus by file
|
||||
const Score ChainBonus[FILE_NB] = {
|
||||
S(11,-1), S(13,-1), S(13,-1), S(14,-1),
|
||||
S(14,-1), S(13,-1), S(13,-1), S(11,-1)
|
||||
};
|
||||
// Pawn chain membership bonus by file and rank (initialized by formula)
|
||||
Score ChainMember[FILE_NB][RANK_NB];
|
||||
|
||||
// Candidate passed pawn bonus by rank
|
||||
const Score CandidateBonus[RANK_NB] = {
|
||||
const Score CandidatePassed[RANK_NB] = {
|
||||
S( 0, 0), S( 6, 13), S(6,13), S(14,29),
|
||||
S(34,68), S(83,166), S(0, 0), S( 0, 0)
|
||||
};
|
||||
S(34,68), S(83,166), S(0, 0), S( 0, 0) };
|
||||
|
||||
const Score PawnStructureWeight = S(233, 201);
|
||||
// Weakness of our pawn shelter in front of the king indexed by [rank]
|
||||
const Value ShelterWeakness[RANK_NB] =
|
||||
{ V(100), V(0), V(27), V(73), V(92), V(101), V(101) };
|
||||
|
||||
// Weakness of our pawn shelter in front of the king indexed by [king pawn][rank]
|
||||
const Value ShelterWeakness[2][RANK_NB] =
|
||||
{ { V(141), V(0), V(38), V(102), V(128), V(141), V(141) },
|
||||
{ V( 61), V(0), V(16), V( 44), V( 56), V( 61), V( 61) } };
|
||||
|
||||
// Danger of enemy pawns moving toward our king indexed by [pawn blocked][rank]
|
||||
const Value StormDanger[2][RANK_NB] =
|
||||
{ { V(26), V(0), V(128), V(51), V(26) },
|
||||
{ V(13), V(0), V( 64), V(25), V(13) } };
|
||||
// Danger of enemy pawns moving toward our king indexed by
|
||||
// [no friendly pawn | pawn unblocked | pawn blocked][rank of enemy pawn]
|
||||
const Value StormDanger[3][RANK_NB] = {
|
||||
{ V( 0), V(64), V(128), V(51), V(26) },
|
||||
{ V(26), V(32), V( 96), V(38), V(20) },
|
||||
{ V( 0), V( 0), V( 64), V(25), V(13) } };
|
||||
|
||||
// Max bonus for king safety. Corresponds to start position with all the pawns
|
||||
// in front of the king and no enemy pawn on the horizont.
|
||||
// in front of the king and no enemy pawn on the horizon.
|
||||
const Value MaxSafetyBonus = V(263);
|
||||
|
||||
#undef S
|
||||
#undef V
|
||||
|
||||
template<Color Us>
|
||||
Score evaluate_pawns(const Position& pos, Bitboard ourPawns,
|
||||
Bitboard theirPawns, Pawns::Entry* e) {
|
||||
Score evaluate(const Position& pos, Pawns::Entry* e) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
const Square Up = (Us == WHITE ? DELTA_N : DELTA_S);
|
||||
const Square Right = (Us == WHITE ? DELTA_NE : DELTA_SW);
|
||||
const Square Left = (Us == WHITE ? DELTA_NW : DELTA_SE);
|
||||
|
||||
Bitboard b;
|
||||
Square s;
|
||||
File f;
|
||||
Rank r;
|
||||
bool passed, isolated, doubled, opposed, chain, backward, candidate;
|
||||
Score value = SCORE_ZERO;
|
||||
const Square* pl = pos.piece_list(Us, PAWN);
|
||||
const Square* pl = pos.list<PAWN>(Us);
|
||||
|
||||
Bitboard ourPawns = pos.pieces(Us, PAWN);
|
||||
Bitboard theirPawns = pos.pieces(Them, PAWN);
|
||||
|
||||
e->passedPawns[Us] = e->candidatePawns[Us] = 0;
|
||||
e->kingSquares[Us] = SQ_NONE;
|
||||
e->semiopenFiles[Us] = 0xFF;
|
||||
e->pawnAttacks[Us] = shift_bb<Right>(ourPawns) | shift_bb<Left>(ourPawns);
|
||||
e->pawnsOnSquares[Us][BLACK] = popcount<Max15>(ourPawns & DarkSquares);
|
||||
e->pawnsOnSquares[Us][WHITE] = pos.count<PAWN>(Us) - e->pawnsOnSquares[Us][BLACK];
|
||||
|
||||
// Loop through all pawns of the current color and score each pawn
|
||||
while ((s = *pl++) != SQ_NONE)
|
||||
@@ -101,13 +106,12 @@ namespace {
|
||||
assert(pos.piece_on(s) == make_piece(Us, PAWN));
|
||||
|
||||
f = file_of(s);
|
||||
r = rank_of(s);
|
||||
|
||||
// This file cannot be half open
|
||||
e->halfOpenFiles[Us] &= ~(1 << f);
|
||||
// This file cannot be semi-open
|
||||
e->semiopenFiles[Us] &= ~(1 << f);
|
||||
|
||||
// Our rank plus previous one. Used for chain detection
|
||||
b = rank_bb(r) | rank_bb(Us == WHITE ? r - Rank(1) : r + Rank(1));
|
||||
b = rank_bb(s) | rank_bb(s - pawn_push(Us));
|
||||
|
||||
// Flag the pawn as passed, isolated, doubled or member of a pawn
|
||||
// chain (but not the backward one).
|
||||
@@ -117,41 +121,37 @@ namespace {
|
||||
opposed = theirPawns & forward_bb(Us, s);
|
||||
passed = !(theirPawns & passed_pawn_mask(Us, s));
|
||||
|
||||
// Test for backward pawn
|
||||
backward = false;
|
||||
|
||||
// Test for backward pawn.
|
||||
// If the pawn is passed, isolated, or member of a pawn chain it cannot
|
||||
// be backward. If there are friendly pawns behind on adjacent files
|
||||
// or if can capture an enemy pawn it cannot be backward either.
|
||||
if ( !(passed | isolated | chain)
|
||||
&& !(ourPawns & attack_span_mask(Them, s))
|
||||
&& !(pos.attacks_from<PAWN>(s, Us) & theirPawns))
|
||||
if ( (passed | isolated | chain)
|
||||
|| (ourPawns & pawn_attack_span(Them, s))
|
||||
|| (pos.attacks_from<PAWN>(s, Us) & theirPawns))
|
||||
backward = false;
|
||||
else
|
||||
{
|
||||
// We now know that there are no friendly pawns beside or behind this
|
||||
// pawn on adjacent files. We now check whether the pawn is
|
||||
// backward by looking in the forward direction on the adjacent
|
||||
// files, and seeing whether we meet a friendly or an enemy pawn first.
|
||||
b = pos.attacks_from<PAWN>(s, Us);
|
||||
// files, and picking the closest pawn there.
|
||||
b = pawn_attack_span(Us, s) & (ourPawns | theirPawns);
|
||||
b = pawn_attack_span(Us, s) & rank_bb(backmost_sq(Us, b));
|
||||
|
||||
// Note that we are sure to find something because pawn is not passed
|
||||
// nor isolated, so loop is potentially infinite, but it isn't.
|
||||
while (!(b & (ourPawns | theirPawns)))
|
||||
Us == WHITE ? b <<= 8 : b >>= 8;
|
||||
|
||||
// The friendly pawn needs to be at least two ranks closer than the
|
||||
// enemy pawn in order to help the potentially backward pawn advance.
|
||||
backward = (b | (Us == WHITE ? b << 8 : b >> 8)) & theirPawns;
|
||||
// If we have an enemy pawn in the same or next rank, the pawn is
|
||||
// backward because it cannot advance without being captured.
|
||||
backward = (b | shift_bb<Up>(b)) & theirPawns;
|
||||
}
|
||||
|
||||
assert(opposed | passed | (attack_span_mask(Us, s) & theirPawns));
|
||||
assert(opposed | passed | (pawn_attack_span(Us, s) & theirPawns));
|
||||
|
||||
// A not passed pawn is a candidate to become passed if it is free to
|
||||
// A not passed pawn is a candidate to become passed, if it is free to
|
||||
// advance and if the number of friendly pawns beside or behind this
|
||||
// pawn on adjacent files is higher or equal than the number of
|
||||
// enemy pawns in the forward direction on the adjacent files.
|
||||
candidate = !(opposed | passed | backward | isolated)
|
||||
&& (b = attack_span_mask(Them, s + pawn_push(Us)) & ourPawns) != 0
|
||||
&& popcount<Max15>(b) >= popcount<Max15>(attack_span_mask(Us, s) & theirPawns);
|
||||
&& (b = pawn_attack_span(Them, s + pawn_push(Us)) & ourPawns) != 0
|
||||
&& popcount<Max15>(b) >= popcount<Max15>(pawn_attack_span(Us, s) & theirPawns);
|
||||
|
||||
// Passed pawns will be properly scored in evaluation because we need
|
||||
// full attack info to evaluate passed pawns. Only the frontmost passed
|
||||
@@ -161,33 +161,49 @@ namespace {
|
||||
|
||||
// Score this pawn
|
||||
if (isolated)
|
||||
value -= IsolatedPawnPenalty[opposed][f];
|
||||
value -= Isolated[opposed][f];
|
||||
|
||||
if (doubled)
|
||||
value -= DoubledPawnPenalty[opposed][f];
|
||||
value -= Doubled[f];
|
||||
|
||||
if (backward)
|
||||
value -= BackwardPawnPenalty[opposed][f];
|
||||
value -= Backward[opposed][f];
|
||||
|
||||
if (chain)
|
||||
value += ChainBonus[f];
|
||||
value += ChainMember[f][relative_rank(Us, s)];
|
||||
|
||||
if (candidate)
|
||||
value += CandidateBonus[relative_rank(Us, s)];
|
||||
{
|
||||
value += CandidatePassed[relative_rank(Us, s)];
|
||||
|
||||
if (!doubled)
|
||||
e->candidatePawns[Us] |= s;
|
||||
}
|
||||
}
|
||||
|
||||
e->pawnsOnSquares[Us][BLACK] = popcount<Max15>(ourPawns & BlackSquares);
|
||||
e->pawnsOnSquares[Us][WHITE] = pos.piece_count(Us, PAWN) - e->pawnsOnSquares[Us][BLACK];
|
||||
|
||||
e->pawnsOnSquares[Them][BLACK] = popcount<Max15>(theirPawns & BlackSquares);
|
||||
e->pawnsOnSquares[Them][WHITE] = pos.piece_count(Them, PAWN) - e->pawnsOnSquares[Them][BLACK];
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace Pawns {
|
||||
|
||||
/// init() initializes some tables by formula instead of hard-code their values
|
||||
|
||||
void init() {
|
||||
|
||||
const int chainByFile[8] = { 1, 3, 3, 4, 4, 3, 3, 1 };
|
||||
int bonus;
|
||||
|
||||
for (Rank r = RANK_1; r < RANK_8; ++r)
|
||||
for (File f = FILE_A; f <= FILE_H; ++f)
|
||||
{
|
||||
bonus = r * (r-1) * (r-2) + chainByFile[f] * (r/2 + 1);
|
||||
ChainMember[f][r] = make_score(bonus, bonus);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// probe() takes a position object as input, computes a Entry object, and returns
|
||||
/// a pointer to it. The result is also stored in a hash table, so we don't have
|
||||
/// to recompute everything when the same pawn structure occurs again.
|
||||
@@ -197,27 +213,11 @@ Entry* probe(const Position& pos, Table& entries) {
|
||||
Key key = pos.pawn_key();
|
||||
Entry* e = entries[key];
|
||||
|
||||
// If e->key matches the position's pawn hash key, it means that we
|
||||
// have analysed this pawn structure before, and we can simply return
|
||||
// the information we found the last time instead of recomputing it.
|
||||
if (e->key == key)
|
||||
return e;
|
||||
|
||||
e->key = key;
|
||||
e->passedPawns[WHITE] = e->passedPawns[BLACK] = 0;
|
||||
e->kingSquares[WHITE] = e->kingSquares[BLACK] = SQ_NONE;
|
||||
e->halfOpenFiles[WHITE] = e->halfOpenFiles[BLACK] = 0xFF;
|
||||
|
||||
Bitboard wPawns = pos.pieces(WHITE, PAWN);
|
||||
Bitboard bPawns = pos.pieces(BLACK, PAWN);
|
||||
e->pawnAttacks[WHITE] = ((wPawns & ~FileHBB) << 9) | ((wPawns & ~FileABB) << 7);
|
||||
e->pawnAttacks[BLACK] = ((bPawns & ~FileHBB) >> 7) | ((bPawns & ~FileABB) >> 9);
|
||||
|
||||
e->value = evaluate_pawns<WHITE>(pos, wPawns, bPawns, e)
|
||||
- evaluate_pawns<BLACK>(pos, bPawns, wPawns, e);
|
||||
|
||||
e->value = apply_weight(e->value, PawnStructureWeight);
|
||||
|
||||
e->value = evaluate<WHITE>(pos, e) - evaluate<BLACK>(pos, e);
|
||||
return e;
|
||||
}
|
||||
|
||||
@@ -231,25 +231,21 @@ Value Entry::shelter_storm(const Position& pos, Square ksq) {
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
Value safety = MaxSafetyBonus;
|
||||
Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, ksq) | rank_bb(ksq));
|
||||
Bitboard ourPawns = b & pos.pieces(Us) & ~rank_bb(ksq);
|
||||
Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq));
|
||||
Bitboard ourPawns = b & pos.pieces(Us);
|
||||
Bitboard theirPawns = b & pos.pieces(Them);
|
||||
Rank rkUs, rkThem;
|
||||
File kf = file_of(ksq);
|
||||
File kf = std::max(FILE_B, std::min(FILE_G, file_of(ksq)));
|
||||
|
||||
kf = (kf == FILE_A) ? FILE_B : (kf == FILE_H) ? FILE_G : kf;
|
||||
|
||||
for (int f = kf - 1; f <= kf + 1; f++)
|
||||
for (File f = kf - File(1); f <= kf + File(1); ++f)
|
||||
{
|
||||
// Shelter penalty is higher for the pawn in front of the king
|
||||
b = ourPawns & FileBB[f];
|
||||
rkUs = b ? rank_of(Us == WHITE ? lsb(b) : ~msb(b)) : RANK_1;
|
||||
safety -= ShelterWeakness[f != kf][rkUs];
|
||||
b = ourPawns & file_bb(f);
|
||||
rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1;
|
||||
safety -= ShelterWeakness[rkUs];
|
||||
|
||||
// Storm danger is smaller if enemy pawn is blocked
|
||||
b = theirPawns & FileBB[f];
|
||||
rkThem = b ? rank_of(Us == WHITE ? lsb(b) : ~msb(b)) : RANK_1;
|
||||
safety -= StormDanger[rkThem == rkUs + 1][rkThem];
|
||||
b = theirPawns & file_bb(f);
|
||||
rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1;
|
||||
safety -= StormDanger[rkUs == RANK_1 ? 0 : rkThem == rkUs + 1 ? 2 : 1][rkThem];
|
||||
}
|
||||
|
||||
return safety;
|
||||
@@ -275,7 +271,7 @@ Score Entry::update_safety(const Position& pos, Square ksq) {
|
||||
|
||||
Value bonus = shelter_storm<Us>(pos, ksq);
|
||||
|
||||
// If we can castle use the bonus after the castle if is bigger
|
||||
// If we can castle use the bonus after the castle if it is bigger
|
||||
if (pos.can_castle(make_castle_right(Us, KING_SIDE)))
|
||||
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_G1)));
|
||||
|
||||
|
||||
+12
-7
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(PAWNS_H_INCLUDED)
|
||||
#ifndef PAWNS_H_INCLUDED
|
||||
#define PAWNS_H_INCLUDED
|
||||
|
||||
#include "misc.h"
|
||||
@@ -37,10 +37,13 @@ struct Entry {
|
||||
Score pawns_value() const { return value; }
|
||||
Bitboard pawn_attacks(Color c) const { return pawnAttacks[c]; }
|
||||
Bitboard passed_pawns(Color c) const { return passedPawns[c]; }
|
||||
int file_is_half_open(Color c, File f) const { return halfOpenFiles[c] & (1 << int(f)); }
|
||||
int has_open_file_to_left(Color c, File f) const { return halfOpenFiles[c] & ((1 << int(f)) - 1); }
|
||||
int has_open_file_to_right(Color c, File f) const { return halfOpenFiles[c] & ~((1 << int(f+1)) - 1); }
|
||||
int pawns_on_same_color_squares(Color c, Square s) const { return pawnsOnSquares[c][!!(BlackSquares & s)]; }
|
||||
Bitboard candidate_pawns(Color c) const { return candidatePawns[c]; }
|
||||
int pawns_on_same_color_squares(Color c, Square s) const { return pawnsOnSquares[c][!!(DarkSquares & s)]; }
|
||||
int semiopen(Color c, File f) const { return semiopenFiles[c] & (1 << int(f)); }
|
||||
int semiopen_on_side(Color c, File f, bool left) const {
|
||||
|
||||
return semiopenFiles[c] & (left ? ((1 << int(f)) - 1) : ~((1 << int(f+1)) - 1));
|
||||
}
|
||||
|
||||
template<Color Us>
|
||||
Score king_safety(const Position& pos, Square ksq) {
|
||||
@@ -57,20 +60,22 @@ struct Entry {
|
||||
|
||||
Key key;
|
||||
Bitboard passedPawns[COLOR_NB];
|
||||
Bitboard candidatePawns[COLOR_NB];
|
||||
Bitboard pawnAttacks[COLOR_NB];
|
||||
Square kingSquares[COLOR_NB];
|
||||
int minKPdistance[COLOR_NB];
|
||||
int castleRights[COLOR_NB];
|
||||
Score value;
|
||||
int halfOpenFiles[COLOR_NB];
|
||||
int semiopenFiles[COLOR_NB];
|
||||
Score kingSafety[COLOR_NB];
|
||||
int pawnsOnSquares[COLOR_NB][COLOR_NB];
|
||||
};
|
||||
|
||||
typedef HashTable<Entry, 16384> Table;
|
||||
|
||||
void init();
|
||||
Entry* probe(const Position& pos, Table& entries);
|
||||
|
||||
}
|
||||
|
||||
#endif // !defined(PAWNS_H_INCLUDED)
|
||||
#endif // #ifndef PAWNS_H_INCLUDED
|
||||
|
||||
+17
-14
@@ -17,10 +17,10 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(PLATFORM_H_INCLUDED)
|
||||
#ifndef PLATFORM_H_INCLUDED
|
||||
#define PLATFORM_H_INCLUDED
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#ifdef _MSC_VER
|
||||
|
||||
// Disable some silly and noisy warning from MSVC compiler
|
||||
#pragma warning(disable: 4127) // Conditional expression is constant
|
||||
@@ -40,16 +40,17 @@ typedef unsigned __int64 uint64_t;
|
||||
|
||||
#else
|
||||
# include <inttypes.h>
|
||||
# include <unistd.h> // Used by sysconf(_SC_NPROCESSORS_ONLN)
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32) && !defined(_WIN64) // Linux - Unix
|
||||
#ifndef _WIN32 // Linux - Unix
|
||||
|
||||
# include <sys/time.h>
|
||||
typedef timeval sys_time_t;
|
||||
|
||||
inline void system_time(sys_time_t* t) { gettimeofday(t, NULL); }
|
||||
inline int64_t time_to_msec(const sys_time_t& t) { return t.tv_sec * 1000LL + t.tv_usec / 1000; }
|
||||
inline int64_t system_time_to_msec() {
|
||||
timeval t;
|
||||
gettimeofday(&t, NULL);
|
||||
return t.tv_sec * 1000LL + t.tv_usec / 1000;
|
||||
}
|
||||
|
||||
# include <pthread.h>
|
||||
typedef pthread_mutex_t Lock;
|
||||
@@ -66,18 +67,20 @@ typedef void*(*pt_start_fn)(void*);
|
||||
# define cond_signal(x) pthread_cond_signal(&(x))
|
||||
# define cond_wait(x,y) pthread_cond_wait(&(x),&(y))
|
||||
# define cond_timedwait(x,y,z) pthread_cond_timedwait(&(x),&(y),z)
|
||||
# define thread_create(x,f,t) !pthread_create(&(x),NULL,(pt_start_fn)f,t)
|
||||
# define thread_create(x,f,t) pthread_create(&(x),NULL,(pt_start_fn)f,t)
|
||||
# define thread_join(x) pthread_join(x, NULL)
|
||||
|
||||
#else // Windows and MinGW
|
||||
|
||||
# include <sys/timeb.h>
|
||||
typedef _timeb sys_time_t;
|
||||
|
||||
inline void system_time(sys_time_t* t) { _ftime(t); }
|
||||
inline int64_t time_to_msec(const sys_time_t& t) { return t.time * 1000LL + t.millitm; }
|
||||
inline int64_t system_time_to_msec() {
|
||||
_timeb t;
|
||||
_ftime(&t);
|
||||
return t.time * 1000LL + t.millitm;
|
||||
}
|
||||
|
||||
#if !defined(NOMINMAX)
|
||||
#ifndef NOMINMAX
|
||||
# define NOMINMAX // disable macros min() and max()
|
||||
#endif
|
||||
|
||||
@@ -105,9 +108,9 @@ inline DWORD* dwWin9xKludge() { static DWORD dw; return &dw; }
|
||||
# define cond_signal(x) SetEvent(x)
|
||||
# define cond_wait(x,y) { lock_release(y); WaitForSingleObject(x, INFINITE); lock_grab(y); }
|
||||
# define cond_timedwait(x,y,z) { lock_release(y); WaitForSingleObject(x,z); lock_grab(y); }
|
||||
# define thread_create(x,f,t) (x = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)f,t,0,dwWin9xKludge()), x != NULL)
|
||||
# define thread_create(x,f,t) (x = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)f,t,0,dwWin9xKludge()))
|
||||
# define thread_join(x) { WaitForSingleObject(x, INFINITE); CloseHandle(x); }
|
||||
|
||||
#endif
|
||||
|
||||
#endif // !defined(PLATFORM_H_INCLUDED)
|
||||
#endif // #ifndef PLATFORM_H_INCLUDED
|
||||
|
||||
+244
-377
File diff suppressed because it is too large
Load Diff
+91
-72
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(POSITION_H_INCLUDED)
|
||||
#ifndef POSITION_H_INCLUDED
|
||||
#define POSITION_H_INCLUDED
|
||||
|
||||
#include <cassert>
|
||||
@@ -43,16 +43,16 @@ struct CheckInfo {
|
||||
};
|
||||
|
||||
|
||||
/// The StateInfo struct stores information we need to restore a Position
|
||||
/// The StateInfo struct stores information needed to restore a Position
|
||||
/// object to its previous state when we retract a move. Whenever a move
|
||||
/// is made on the board (by calling Position::do_move), a StateInfo object
|
||||
/// must be passed as a parameter.
|
||||
/// is made on the board (by calling Position::do_move), a StateInfo
|
||||
/// object must be passed as a parameter.
|
||||
|
||||
struct StateInfo {
|
||||
Key pawnKey, materialKey;
|
||||
Value npMaterial[COLOR_NB];
|
||||
int castleRights, rule50, pliesFromNull;
|
||||
Score psqScore;
|
||||
Score psq;
|
||||
Square epSquare;
|
||||
|
||||
Key key;
|
||||
@@ -67,27 +67,10 @@ struct StateInfo {
|
||||
const size_t StateCopySize64 = offsetof(StateInfo, key) / sizeof(uint64_t) + 1;
|
||||
|
||||
|
||||
/// The position data structure. A position consists of the following data:
|
||||
///
|
||||
/// * For each piece type, a bitboard representing the squares occupied
|
||||
/// by pieces of that type.
|
||||
/// * For each color, a bitboard representing the squares occupied by
|
||||
/// pieces of that color.
|
||||
/// * A bitboard of all occupied squares.
|
||||
/// * A bitboard of all checking pieces.
|
||||
/// * A 64-entry array of pieces, indexed by the squares of the board.
|
||||
/// * The current side to move.
|
||||
/// * Information about the castling rights for both sides.
|
||||
/// * The initial files of the kings and both pairs of rooks. This is
|
||||
/// used to implement the Chess960 castling rules.
|
||||
/// * The en passant square (which is SQ_NONE if no en passant capture is
|
||||
/// possible).
|
||||
/// * The squares of the kings for both sides.
|
||||
/// * Hash keys for the position itself, the current pawn structure, and
|
||||
/// the current material situation.
|
||||
/// * Hash keys for all previous positions in the game for detecting
|
||||
/// repetition draws.
|
||||
/// * A counter for detecting 50 move rule draws.
|
||||
/// The Position class stores the information regarding the board representation
|
||||
/// like pieces, side to move, hash keys, castling info, etc. The most important
|
||||
/// methods are do_move() and undo_move(), used by the search to update node info
|
||||
/// when traversing the search tree.
|
||||
|
||||
class Position {
|
||||
public:
|
||||
@@ -95,6 +78,7 @@ public:
|
||||
Position(const Position& p, Thread* t) { *this = p; thisThread = t; }
|
||||
Position(const std::string& f, bool c960, Thread* t) { set(f, c960, t); }
|
||||
Position& operator=(const Position&);
|
||||
static void init();
|
||||
|
||||
// Text input/output
|
||||
void set(const std::string& fen, bool isChess960, Thread* th);
|
||||
@@ -111,9 +95,9 @@ public:
|
||||
Piece piece_on(Square s) const;
|
||||
Square king_square(Color c) const;
|
||||
Square ep_square() const;
|
||||
bool is_empty(Square s) const;
|
||||
const Square* piece_list(Color c, PieceType pt) const;
|
||||
int piece_count(Color c, PieceType pt) const;
|
||||
bool empty(Square s) const;
|
||||
template<PieceType Pt> int count(Color c) const;
|
||||
template<PieceType Pt> const Square* list(Color c) const;
|
||||
|
||||
// Castling
|
||||
int can_castle(CastleRight f) const;
|
||||
@@ -124,7 +108,7 @@ public:
|
||||
// Checking
|
||||
Bitboard checkers() const;
|
||||
Bitboard discovered_check_candidates() const;
|
||||
Bitboard pinned_pieces() const;
|
||||
Bitboard pinned_pieces(Color toMove) const;
|
||||
|
||||
// Attacks to/from a given square
|
||||
Bitboard attackers_to(Square s) const;
|
||||
@@ -135,20 +119,20 @@ public:
|
||||
template<PieceType> Bitboard attacks_from(Square s, Color c) const;
|
||||
|
||||
// Properties of moves
|
||||
bool move_gives_check(Move m, const CheckInfo& ci) const;
|
||||
bool pl_move_is_legal(Move m, Bitboard pinned) const;
|
||||
bool is_pseudo_legal(const Move m) const;
|
||||
bool is_capture(Move m) const;
|
||||
bool is_capture_or_promotion(Move m) const;
|
||||
bool is_passed_pawn_push(Move m) const;
|
||||
Piece piece_moved(Move m) const;
|
||||
bool legal(Move m, Bitboard pinned) const;
|
||||
bool pseudo_legal(const Move m) const;
|
||||
bool capture(Move m) const;
|
||||
bool capture_or_promotion(Move m) const;
|
||||
bool gives_check(Move m, const CheckInfo& ci) const;
|
||||
bool passed_pawn_push(Move m) const;
|
||||
Piece moved_piece(Move m) const;
|
||||
PieceType captured_piece_type() const;
|
||||
|
||||
// Piece specific
|
||||
bool pawn_is_passed(Color c, Square s) const;
|
||||
bool pawn_passed(Color c, Square s) const;
|
||||
bool pawn_on_7th(Color c) const;
|
||||
bool opposite_bishops() const;
|
||||
bool bishop_pair(Color c) const;
|
||||
bool opposite_bishops() const;
|
||||
|
||||
// Doing and undoing moves
|
||||
void do_move(Move m, StateInfo& st);
|
||||
@@ -169,7 +153,6 @@ public:
|
||||
|
||||
// Incremental piece-square evaluation
|
||||
Score psq_score() const;
|
||||
Score psq_delta(Piece p, Square from, Square to) const;
|
||||
Value non_pawn_material(Color c) const;
|
||||
|
||||
// Other properties of the position
|
||||
@@ -188,12 +171,14 @@ public:
|
||||
private:
|
||||
// Initialization helpers (used while setting up a position)
|
||||
void clear();
|
||||
void put_piece(Piece p, Square s);
|
||||
void set_castle_right(Color c, Square rfrom);
|
||||
|
||||
// Helper functions
|
||||
void do_castle(Square kfrom, Square kto, Square rfrom, Square rto);
|
||||
template<bool FindPinned> Bitboard hidden_checkers() const;
|
||||
Bitboard hidden_checkers(Square ksq, Color c, Color toMove) const;
|
||||
void put_piece(Square s, Color c, PieceType pt);
|
||||
void remove_piece(Square s, Color c, PieceType pt);
|
||||
void move_piece(Square from, Square to, Color c, PieceType pt);
|
||||
|
||||
// Computing hash keys from scratch (for initialization and debugging)
|
||||
Key compute_key() const;
|
||||
@@ -237,11 +222,11 @@ inline Piece Position::piece_on(Square s) const {
|
||||
return board[s];
|
||||
}
|
||||
|
||||
inline Piece Position::piece_moved(Move m) const {
|
||||
inline Piece Position::moved_piece(Move m) const {
|
||||
return board[from_sq(m)];
|
||||
}
|
||||
|
||||
inline bool Position::is_empty(Square s) const {
|
||||
inline bool Position::empty(Square s) const {
|
||||
return board[s] == NO_PIECE;
|
||||
}
|
||||
|
||||
@@ -273,12 +258,12 @@ inline Bitboard Position::pieces(Color c, PieceType pt1, PieceType pt2) const {
|
||||
return byColorBB[c] & (byTypeBB[pt1] | byTypeBB[pt2]);
|
||||
}
|
||||
|
||||
inline int Position::piece_count(Color c, PieceType pt) const {
|
||||
return pieceCount[c][pt];
|
||||
template<PieceType Pt> inline int Position::count(Color c) const {
|
||||
return pieceCount[c][Pt];
|
||||
}
|
||||
|
||||
inline const Square* Position::piece_list(Color c, PieceType pt) const {
|
||||
return pieceList[c][pt];
|
||||
template<PieceType Pt> inline const Square* Position::list(Color c) const {
|
||||
return pieceList[c][Pt];
|
||||
}
|
||||
|
||||
inline Square Position::ep_square() const {
|
||||
@@ -331,25 +316,27 @@ inline Bitboard Position::checkers() const {
|
||||
}
|
||||
|
||||
inline Bitboard Position::discovered_check_candidates() const {
|
||||
return hidden_checkers<false>();
|
||||
return hidden_checkers(king_square(~sideToMove), sideToMove, sideToMove);
|
||||
}
|
||||
|
||||
inline Bitboard Position::pinned_pieces() const {
|
||||
return hidden_checkers<true>();
|
||||
inline Bitboard Position::pinned_pieces(Color toMove) const {
|
||||
return hidden_checkers(king_square(toMove), ~toMove, toMove);
|
||||
}
|
||||
|
||||
inline bool Position::pawn_is_passed(Color c, Square s) const {
|
||||
inline bool Position::pawn_passed(Color c, Square s) const {
|
||||
return !(pieces(~c, PAWN) & passed_pawn_mask(c, s));
|
||||
}
|
||||
|
||||
inline bool Position::passed_pawn_push(Move m) const {
|
||||
|
||||
return type_of(moved_piece(m)) == PAWN
|
||||
&& pawn_passed(sideToMove, to_sq(m));
|
||||
}
|
||||
|
||||
inline Key Position::key() const {
|
||||
return st->key;
|
||||
}
|
||||
|
||||
inline Key Position::exclusion_key() const {
|
||||
return st->key ^ Zobrist::exclusion;
|
||||
}
|
||||
|
||||
inline Key Position::pawn_key() const {
|
||||
return st->pawnKey;
|
||||
}
|
||||
@@ -358,24 +345,14 @@ inline Key Position::material_key() const {
|
||||
return st->materialKey;
|
||||
}
|
||||
|
||||
inline Score Position::psq_delta(Piece p, Square from, Square to) const {
|
||||
return pieceSquareTable[p][to] - pieceSquareTable[p][from];
|
||||
}
|
||||
|
||||
inline Score Position::psq_score() const {
|
||||
return st->psqScore;
|
||||
return st->psq;
|
||||
}
|
||||
|
||||
inline Value Position::non_pawn_material(Color c) const {
|
||||
return st->npMaterial[c];
|
||||
}
|
||||
|
||||
inline bool Position::is_passed_pawn_push(Move m) const {
|
||||
|
||||
return type_of(piece_moved(m)) == PAWN
|
||||
&& pawn_is_passed(sideToMove, to_sq(m));
|
||||
}
|
||||
|
||||
inline int Position::game_ply() const {
|
||||
return gamePly;
|
||||
}
|
||||
@@ -401,17 +378,17 @@ inline bool Position::is_chess960() const {
|
||||
return chess960;
|
||||
}
|
||||
|
||||
inline bool Position::is_capture_or_promotion(Move m) const {
|
||||
inline bool Position::capture_or_promotion(Move m) const {
|
||||
|
||||
assert(is_ok(m));
|
||||
return type_of(m) ? type_of(m) != CASTLE : !is_empty(to_sq(m));
|
||||
return type_of(m) ? type_of(m) != CASTLE : !empty(to_sq(m));
|
||||
}
|
||||
|
||||
inline bool Position::is_capture(Move m) const {
|
||||
inline bool Position::capture(Move m) const {
|
||||
|
||||
// Note that castle is coded as "king captures the rook"
|
||||
assert(is_ok(m));
|
||||
return (!is_empty(to_sq(m)) && type_of(m) != CASTLE) || type_of(m) == ENPASSANT;
|
||||
return (!empty(to_sq(m)) && type_of(m) != CASTLE) || type_of(m) == ENPASSANT;
|
||||
}
|
||||
|
||||
inline PieceType Position::captured_piece_type() const {
|
||||
@@ -422,4 +399,46 @@ inline Thread* Position::this_thread() const {
|
||||
return thisThread;
|
||||
}
|
||||
|
||||
#endif // !defined(POSITION_H_INCLUDED)
|
||||
inline void Position::put_piece(Square s, Color c, PieceType pt) {
|
||||
|
||||
board[s] = make_piece(c, pt);
|
||||
byTypeBB[ALL_PIECES] |= s;
|
||||
byTypeBB[pt] |= s;
|
||||
byColorBB[c] |= s;
|
||||
pieceCount[c][ALL_PIECES]++;
|
||||
index[s] = pieceCount[c][pt]++;
|
||||
pieceList[c][pt][index[s]] = s;
|
||||
}
|
||||
|
||||
inline void Position::move_piece(Square from, Square to, Color c, PieceType pt) {
|
||||
|
||||
// index[from] is not updated and becomes stale. This works as long
|
||||
// as index[] is accessed just by known occupied squares.
|
||||
Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
|
||||
byTypeBB[ALL_PIECES] ^= from_to_bb;
|
||||
byTypeBB[pt] ^= from_to_bb;
|
||||
byColorBB[c] ^= from_to_bb;
|
||||
board[from] = NO_PIECE;
|
||||
board[to] = make_piece(c, pt);
|
||||
index[to] = index[from];
|
||||
pieceList[c][pt][index[to]] = to;
|
||||
}
|
||||
|
||||
inline void Position::remove_piece(Square s, Color c, PieceType pt) {
|
||||
|
||||
// WARNING: This is not a reversible operation. If we remove a piece in
|
||||
// do_move() and then replace it in undo_move() we will put it at the end of
|
||||
// the list and not in its original place, it means index[] and pieceList[]
|
||||
// are not guaranteed to be invariant to a do_move() + undo_move() sequence.
|
||||
byTypeBB[ALL_PIECES] ^= s;
|
||||
byTypeBB[pt] ^= s;
|
||||
byColorBB[c] ^= s;
|
||||
/* board[s] = NO_PIECE; */ // Not needed, will be overwritten by capturing
|
||||
pieceCount[c][ALL_PIECES]--;
|
||||
Square lastSquare = pieceList[c][pt][--pieceCount[c][pt]];
|
||||
index[lastSquare] = index[s];
|
||||
pieceList[c][pt][index[lastSquare]] = lastSquare;
|
||||
pieceList[c][pt][pieceCount[c][pt]] = SQ_NONE;
|
||||
}
|
||||
|
||||
#endif // #ifndef POSITION_H_INCLUDED
|
||||
|
||||
+8
-8
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(PSQTAB_H_INCLUDED)
|
||||
#ifndef PSQTAB_H_INCLUDED
|
||||
#define PSQTAB_H_INCLUDED
|
||||
|
||||
#include "types.h"
|
||||
@@ -33,12 +33,12 @@ static const Score PSQT[][SQUARE_NB] = {
|
||||
{ },
|
||||
{ // Pawn
|
||||
S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S( 0, 0),
|
||||
S(-28,-8), S(-6,-8), S( 4,-8), S(14,-8), S(14,-8), S( 4,-8), S(-6,-8), S(-28,-8),
|
||||
S(-28,-8), S(-6,-8), S( 9,-8), S(36,-8), S(36,-8), S( 9,-8), S(-6,-8), S(-28,-8),
|
||||
S(-28,-8), S(-6,-8), S(17,-8), S(58,-8), S(58,-8), S(17,-8), S(-6,-8), S(-28,-8),
|
||||
S(-28,-8), S(-6,-8), S(17,-8), S(36,-8), S(36,-8), S(17,-8), S(-6,-8), S(-28,-8),
|
||||
S(-28,-8), S(-6,-8), S( 9,-8), S(14,-8), S(14,-8), S( 9,-8), S(-6,-8), S(-28,-8),
|
||||
S(-28,-8), S(-6,-8), S( 4,-8), S(14,-8), S(14,-8), S( 4,-8), S(-6,-8), S(-28,-8),
|
||||
S(-20,-8), S(-6,-8), S( 4,-8), S(14,-8), S(14,-8), S( 4,-8), S(-6,-8), S(-20,-8),
|
||||
S(-20,-8), S(-6,-8), S( 9,-8), S(34,-8), S(34,-8), S( 9,-8), S(-6,-8), S(-20,-8),
|
||||
S(-20,-8), S(-6,-8), S(17,-8), S(54,-8), S(54,-8), S(17,-8), S(-6,-8), S(-20,-8),
|
||||
S(-20,-8), S(-6,-8), S(17,-8), S(34,-8), S(34,-8), S(17,-8), S(-6,-8), S(-20,-8),
|
||||
S(-20,-8), S(-6,-8), S( 9,-8), S(14,-8), S(14,-8), S( 9,-8), S(-6,-8), S(-20,-8),
|
||||
S(-20,-8), S(-6,-8), S( 4,-8), S(14,-8), S(14,-8), S( 4,-8), S(-6,-8), S(-20,-8),
|
||||
S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S( 0, 0)
|
||||
},
|
||||
{ // Knight
|
||||
@@ -95,4 +95,4 @@ static const Score PSQT[][SQUARE_NB] = {
|
||||
|
||||
#undef S
|
||||
|
||||
#endif // !defined(PSQTAB_H_INCLUDED)
|
||||
#endif // #ifndef PSQTAB_H_INCLUDED
|
||||
|
||||
+11
-14
@@ -22,7 +22,7 @@
|
||||
(at your option) any later version.
|
||||
*/
|
||||
|
||||
#if !defined(RKISS_H_INCLUDED)
|
||||
#ifndef RKISS_H_INCLUDED
|
||||
#define RKISS_H_INCLUDED
|
||||
|
||||
#include "types.h"
|
||||
@@ -43,34 +43,31 @@
|
||||
|
||||
class RKISS {
|
||||
|
||||
// Keep variables always together
|
||||
struct S { uint64_t a, b, c, d; } s;
|
||||
uint64_t a, b, c, d;
|
||||
|
||||
uint64_t rotate(uint64_t x, uint64_t k) const {
|
||||
return (x << k) | (x >> (64 - k));
|
||||
}
|
||||
|
||||
// Return 64 bit unsigned integer in between [0, 2^64 - 1]
|
||||
uint64_t rand64() {
|
||||
|
||||
const uint64_t
|
||||
e = s.a - rotate(s.b, 7);
|
||||
s.a = s.b ^ rotate(s.c, 13);
|
||||
s.b = s.c + rotate(s.d, 37);
|
||||
s.c = s.d + e;
|
||||
return s.d = e + s.a;
|
||||
const uint64_t e = a - rotate(b, 7);
|
||||
a = b ^ rotate(c, 13);
|
||||
b = c + rotate(d, 37);
|
||||
c = d + e;
|
||||
return d = e + a;
|
||||
}
|
||||
|
||||
public:
|
||||
RKISS(int seed = 73) {
|
||||
|
||||
s.a = 0xf1ea5eed;
|
||||
s.b = s.c = s.d = 0xd4e12c77;
|
||||
for (int i = 0; i < seed; i++) // Scramble a few rounds
|
||||
a = 0xF1EA5EED, b = c = d = 0xD4E12C77;
|
||||
|
||||
for (int i = 0; i < seed; ++i) // Scramble a few rounds
|
||||
rand64();
|
||||
}
|
||||
|
||||
template<typename T> T rand() { return T(rand64()); }
|
||||
};
|
||||
|
||||
#endif // !defined(RKISS_H_INCLUDED)
|
||||
#endif // #ifndef RKISS_H_INCLUDED
|
||||
|
||||
+292
-348
File diff suppressed because it is too large
Load Diff
+3
-5
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(SEARCH_H_INCLUDED)
|
||||
#ifndef SEARCH_H_INCLUDED
|
||||
#define SEARCH_H_INCLUDED
|
||||
|
||||
#include <cstring>
|
||||
@@ -45,9 +45,7 @@ struct Stack {
|
||||
Move killers[2];
|
||||
Depth reduction;
|
||||
Value staticEval;
|
||||
Value evalMargin;
|
||||
int skipNullMove;
|
||||
int futilityMoveCount;
|
||||
};
|
||||
|
||||
|
||||
@@ -79,7 +77,7 @@ struct RootMove {
|
||||
|
||||
struct LimitsType {
|
||||
|
||||
LimitsType() { memset(this, 0, sizeof(LimitsType)); }
|
||||
LimitsType() { std::memset(this, 0, sizeof(LimitsType)); }
|
||||
bool use_time_management() const { return !(mate | movetime | depth | nodes | infinite); }
|
||||
|
||||
int time[COLOR_NB], inc[COLOR_NB], movestogo, depth, nodes, movetime, mate, infinite, ponder;
|
||||
@@ -109,4 +107,4 @@ extern void think();
|
||||
|
||||
} // namespace Search
|
||||
|
||||
#endif // !defined(SEARCH_H_INCLUDED)
|
||||
#endif // #ifndef SEARCH_H_INCLUDED
|
||||
|
||||
+84
-71
@@ -19,7 +19,6 @@
|
||||
|
||||
#include <algorithm> // For std::count
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
#include "movegen.h"
|
||||
#include "search.h"
|
||||
@@ -30,42 +29,64 @@ using namespace Search;
|
||||
|
||||
ThreadPool Threads; // Global object
|
||||
|
||||
namespace { extern "C" {
|
||||
namespace {
|
||||
|
||||
// start_routine() is the C function which is called when a new thread
|
||||
// is launched. It is a wrapper to the virtual function idle_loop().
|
||||
|
||||
long start_routine(Thread* th) { th->idle_loop(); return 0; }
|
||||
|
||||
} }
|
||||
extern "C" { long start_routine(ThreadBase* th) { th->idle_loop(); return 0; } }
|
||||
|
||||
|
||||
// Thread c'tor starts a newly-created thread of execution that will call
|
||||
// the the virtual function idle_loop(), going immediately to sleep.
|
||||
// Helpers to launch a thread after creation and joining before delete. Must be
|
||||
// outside Thread c'tor and d'tor because object shall be fully initialized
|
||||
// when start_routine (and hence virtual idle_loop) is called and when joining.
|
||||
|
||||
template<typename T> T* new_thread() {
|
||||
T* th = new T();
|
||||
thread_create(th->handle, start_routine, th); // Will go to sleep
|
||||
return th;
|
||||
}
|
||||
|
||||
void delete_thread(ThreadBase* th) {
|
||||
th->exit = true; // Search must be already finished
|
||||
th->notify_one();
|
||||
thread_join(th->handle); // Wait for thread termination
|
||||
delete th;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// ThreadBase::notify_one() wakes up the thread when there is some work to do
|
||||
|
||||
void ThreadBase::notify_one() {
|
||||
|
||||
mutex.lock();
|
||||
sleepCondition.notify_one();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
// ThreadBase::wait_for() set the thread to sleep until condition 'b' turns true
|
||||
|
||||
void ThreadBase::wait_for(volatile const bool& b) {
|
||||
|
||||
mutex.lock();
|
||||
while (!b) sleepCondition.wait(mutex);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
// Thread c'tor just inits data but does not launch any thread of execution that
|
||||
// instead will be started only upon c'tor returns.
|
||||
|
||||
Thread::Thread() /* : splitPoints() */ { // Value-initialization bug in MSVC
|
||||
|
||||
searching = exit = false;
|
||||
searching = false;
|
||||
maxPly = splitPointsSize = 0;
|
||||
activeSplitPoint = NULL;
|
||||
activePosition = NULL;
|
||||
idx = Threads.size();
|
||||
|
||||
if (!thread_create(handle, start_routine, this))
|
||||
{
|
||||
std::cerr << "Failed to create thread number " << idx << std::endl;
|
||||
::exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Thread d'tor waits for thread termination before to return
|
||||
|
||||
Thread::~Thread() {
|
||||
|
||||
exit = true; // Search must be already finished
|
||||
notify_one();
|
||||
thread_join(handle); // Wait for thread termination
|
||||
}
|
||||
|
||||
|
||||
@@ -80,11 +101,11 @@ void TimerThread::idle_loop() {
|
||||
mutex.lock();
|
||||
|
||||
if (!exit)
|
||||
sleepCondition.wait_for(mutex, msec ? msec : INT_MAX);
|
||||
sleepCondition.wait_for(mutex, run ? Resolution : INT_MAX);
|
||||
|
||||
mutex.unlock();
|
||||
|
||||
if (msec)
|
||||
if (run)
|
||||
check_time();
|
||||
}
|
||||
}
|
||||
@@ -123,26 +144,6 @@ void MainThread::idle_loop() {
|
||||
}
|
||||
|
||||
|
||||
// Thread::notify_one() wakes up the thread when there is some search to do
|
||||
|
||||
void Thread::notify_one() {
|
||||
|
||||
mutex.lock();
|
||||
sleepCondition.notify_one();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
// Thread::wait_for() set the thread to sleep until condition 'b' turns true
|
||||
|
||||
void Thread::wait_for(volatile const bool& b) {
|
||||
|
||||
mutex.lock();
|
||||
while (!b) sleepCondition.wait(mutex);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
// Thread::cutoff_occurred() checks whether a beta cutoff has occurred in the
|
||||
// current active split point, or in some ancestor of the split point.
|
||||
|
||||
@@ -156,14 +157,14 @@ bool Thread::cutoff_occurred() const {
|
||||
}
|
||||
|
||||
|
||||
// Thread::is_available_to() checks whether the thread is available to help the
|
||||
// Thread::available_to() checks whether the thread is available to help the
|
||||
// thread 'master' at a split point. An obvious requirement is that thread must
|
||||
// be idle. With more than two threads, this is not sufficient: If the thread is
|
||||
// the master of some split point, it is only available as a slave to the slaves
|
||||
// which are busy searching the split point at the top of slaves split point
|
||||
// stack (the "helpful master concept" in YBWC terminology).
|
||||
|
||||
bool Thread::is_available_to(Thread* master) const {
|
||||
bool Thread::available_to(const Thread* master) const {
|
||||
|
||||
if (searching)
|
||||
return false;
|
||||
@@ -186,8 +187,8 @@ bool Thread::is_available_to(Thread* master) const {
|
||||
void ThreadPool::init() {
|
||||
|
||||
sleepWhileIdle = true;
|
||||
timer = new TimerThread();
|
||||
push_back(new MainThread());
|
||||
timer = new_thread<TimerThread>();
|
||||
push_back(new_thread<MainThread>());
|
||||
read_uci_options();
|
||||
}
|
||||
|
||||
@@ -196,10 +197,10 @@ void ThreadPool::init() {
|
||||
|
||||
void ThreadPool::exit() {
|
||||
|
||||
delete timer; // As first because check_time() accesses threads data
|
||||
delete_thread(timer); // As first because check_time() accesses threads data
|
||||
|
||||
for (iterator it = begin(); it != end(); ++it)
|
||||
delete *it;
|
||||
delete_thread(*it);
|
||||
}
|
||||
|
||||
|
||||
@@ -216,12 +217,19 @@ void ThreadPool::read_uci_options() {
|
||||
|
||||
assert(requested > 0);
|
||||
|
||||
// Value 0 has a special meaning: We determine the optimal minimum split depth
|
||||
// automatically. Anyhow the minimumSplitDepth should never be under 4 plies.
|
||||
if (!minimumSplitDepth)
|
||||
minimumSplitDepth = (requested < 8 ? 4 : 7) * ONE_PLY;
|
||||
else
|
||||
minimumSplitDepth = std::max(4 * ONE_PLY, minimumSplitDepth);
|
||||
|
||||
while (size() < requested)
|
||||
push_back(new Thread());
|
||||
push_back(new_thread<Thread>());
|
||||
|
||||
while (size() > requested)
|
||||
{
|
||||
delete back();
|
||||
delete_thread(back());
|
||||
pop_back();
|
||||
}
|
||||
}
|
||||
@@ -230,10 +238,10 @@ void ThreadPool::read_uci_options() {
|
||||
// slave_available() tries to find an idle thread which is available as a slave
|
||||
// for the thread 'master'.
|
||||
|
||||
Thread* ThreadPool::available_slave(Thread* master) const {
|
||||
Thread* ThreadPool::available_slave(const Thread* master) const {
|
||||
|
||||
for (const_iterator it = begin(); it != end(); ++it)
|
||||
if ((*it)->is_available_to(master))
|
||||
if ((*it)->available_to(master))
|
||||
return *it;
|
||||
|
||||
return NULL;
|
||||
@@ -250,9 +258,9 @@ Thread* ThreadPool::available_slave(Thread* master) const {
|
||||
// search() then split() returns.
|
||||
|
||||
template <bool Fake>
|
||||
void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bestValue,
|
||||
void Thread::split(Position& pos, const Stack* ss, Value alpha, Value beta, Value* bestValue,
|
||||
Move* bestMove, Depth depth, Move threatMove, int moveCount,
|
||||
MovePicker* movePicker, int nodeType) {
|
||||
MovePicker* movePicker, int nodeType, bool cutNode) {
|
||||
|
||||
assert(pos.pos_is_ok());
|
||||
assert(*bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE);
|
||||
@@ -274,6 +282,7 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes
|
||||
sp.alpha = alpha;
|
||||
sp.beta = beta;
|
||||
sp.nodeType = nodeType;
|
||||
sp.cutNode = cutNode;
|
||||
sp.movePicker = movePicker;
|
||||
sp.moveCount = moveCount;
|
||||
sp.pos = &pos;
|
||||
@@ -287,7 +296,7 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes
|
||||
Threads.mutex.lock();
|
||||
sp.mutex.lock();
|
||||
|
||||
splitPointsSize++;
|
||||
++splitPointsSize;
|
||||
activeSplitPoint = &sp;
|
||||
activePosition = NULL;
|
||||
|
||||
@@ -321,13 +330,13 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes
|
||||
|
||||
// We have returned from the idle loop, which means that all threads are
|
||||
// finished. Note that setting 'searching' and decreasing splitPointsSize is
|
||||
// done under lock protection to avoid a race with Thread::is_available_to().
|
||||
// done under lock protection to avoid a race with Thread::available_to().
|
||||
Threads.mutex.lock();
|
||||
sp.mutex.lock();
|
||||
}
|
||||
|
||||
searching = true;
|
||||
splitPointsSize--;
|
||||
--splitPointsSize;
|
||||
activeSplitPoint = sp.parentSplitPoint;
|
||||
activePosition = &pos;
|
||||
pos.set_nodes_searched(pos.nodes_searched() + sp.nodes);
|
||||
@@ -339,15 +348,15 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes
|
||||
}
|
||||
|
||||
// Explicit template instantiations
|
||||
template void Thread::split<false>(Position&, Stack*, Value, Value, Value*, Move*, Depth, Move, int, MovePicker*, int);
|
||||
template void Thread::split< true>(Position&, Stack*, Value, Value, Value*, Move*, Depth, Move, int, MovePicker*, int);
|
||||
template void Thread::split<false>(Position&, const Stack*, Value, Value, Value*, Move*, Depth, Move, int, MovePicker*, int, bool);
|
||||
template void Thread::split< true>(Position&, const Stack*, Value, Value, Value*, Move*, Depth, Move, int, MovePicker*, int, bool);
|
||||
|
||||
|
||||
// wait_for_think_finished() waits for main thread to go to sleep then returns
|
||||
|
||||
void ThreadPool::wait_for_think_finished() {
|
||||
|
||||
MainThread* t = main_thread();
|
||||
MainThread* t = main();
|
||||
t->mutex.lock();
|
||||
while (t->thinking) sleepCondition.wait(t->mutex);
|
||||
t->mutex.unlock();
|
||||
@@ -366,16 +375,20 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
|
||||
Signals.stopOnPonderhit = Signals.firstRootMove = false;
|
||||
Signals.stop = Signals.failedLowAtRoot = false;
|
||||
|
||||
RootMoves.clear();
|
||||
RootPos = pos;
|
||||
Limits = limits;
|
||||
SetupStates = states; // Ownership transfer here
|
||||
RootMoves.clear();
|
||||
if (states.get()) // If we don't set a new position, preserve current state
|
||||
{
|
||||
SetupStates = states; // Ownership transfer here
|
||||
assert(!states.get());
|
||||
}
|
||||
|
||||
for (MoveList<LEGAL> ml(pos); !ml.end(); ++ml)
|
||||
for (MoveList<LEGAL> it(pos); *it; ++it)
|
||||
if ( searchMoves.empty()
|
||||
|| std::count(searchMoves.begin(), searchMoves.end(), ml.move()))
|
||||
RootMoves.push_back(RootMove(ml.move()));
|
||||
|| std::count(searchMoves.begin(), searchMoves.end(), *it))
|
||||
RootMoves.push_back(RootMove(*it));
|
||||
|
||||
main_thread()->thinking = true;
|
||||
main_thread()->notify_one(); // Starts main thread
|
||||
main()->thinking = true;
|
||||
main()->notify_one(); // Starts main thread
|
||||
}
|
||||
|
||||
+32
-20
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(THREAD_H_INCLUDED)
|
||||
#ifndef THREAD_H_INCLUDED
|
||||
#define THREAD_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
@@ -68,6 +68,7 @@ struct SplitPoint {
|
||||
Value beta;
|
||||
int nodeType;
|
||||
Move threatMove;
|
||||
bool cutNode;
|
||||
|
||||
// Const pointers to shared data
|
||||
MovePicker* movePicker;
|
||||
@@ -85,25 +86,39 @@ struct SplitPoint {
|
||||
};
|
||||
|
||||
|
||||
/// ThreadBase struct is the base of the hierarchy from where we derive all the
|
||||
/// specialized thread classes.
|
||||
|
||||
struct ThreadBase {
|
||||
|
||||
ThreadBase() : exit(false) {}
|
||||
virtual ~ThreadBase() {}
|
||||
virtual void idle_loop() = 0;
|
||||
void notify_one();
|
||||
void wait_for(volatile const bool& b);
|
||||
|
||||
Mutex mutex;
|
||||
ConditionVariable sleepCondition;
|
||||
NativeHandle handle;
|
||||
volatile bool exit;
|
||||
};
|
||||
|
||||
|
||||
/// Thread struct keeps together all the thread related stuff like locks, state
|
||||
/// and especially split points. We also use per-thread pawn and material hash
|
||||
/// tables so that once we get a pointer to an entry its life time is unlimited
|
||||
/// and we don't have to care about someone changing the entry under our feet.
|
||||
|
||||
struct Thread {
|
||||
struct Thread : public ThreadBase {
|
||||
|
||||
Thread();
|
||||
virtual ~Thread();
|
||||
|
||||
virtual void idle_loop();
|
||||
void notify_one();
|
||||
bool cutoff_occurred() const;
|
||||
bool is_available_to(Thread* master) const;
|
||||
void wait_for(volatile const bool& b);
|
||||
bool available_to(const Thread* master) const;
|
||||
|
||||
template <bool Fake>
|
||||
void split(Position& pos, Search::Stack* ss, Value alpha, Value beta, Value* bestValue, Move* bestMove,
|
||||
Depth depth, Move threatMove, int moveCount, MovePicker* movePicker, int nodeType);
|
||||
void split(Position& pos, const Search::Stack* ss, Value alpha, Value beta, Value* bestValue, Move* bestMove,
|
||||
Depth depth, Move threatMove, int moveCount, MovePicker* movePicker, int nodeType, bool cutNode);
|
||||
|
||||
SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD];
|
||||
Material::Table materialTable;
|
||||
@@ -112,17 +127,13 @@ struct Thread {
|
||||
Position* activePosition;
|
||||
size_t idx;
|
||||
int maxPly;
|
||||
Mutex mutex;
|
||||
ConditionVariable sleepCondition;
|
||||
NativeHandle handle;
|
||||
SplitPoint* volatile activeSplitPoint;
|
||||
volatile int splitPointsSize;
|
||||
volatile bool searching;
|
||||
volatile bool exit;
|
||||
};
|
||||
|
||||
|
||||
/// MainThread and TimerThread are sublassed from Thread to characterize the two
|
||||
/// MainThread and TimerThread are derived classes used to characterize the two
|
||||
/// special threads: the main one and the recurring timer.
|
||||
|
||||
struct MainThread : public Thread {
|
||||
@@ -131,10 +142,11 @@ struct MainThread : public Thread {
|
||||
volatile bool thinking;
|
||||
};
|
||||
|
||||
struct TimerThread : public Thread {
|
||||
TimerThread() : msec(0) {}
|
||||
struct TimerThread : public ThreadBase {
|
||||
TimerThread() : run(false) {}
|
||||
virtual void idle_loop();
|
||||
int msec;
|
||||
bool run;
|
||||
static const int Resolution = 5; // msec between two check_time() calls
|
||||
};
|
||||
|
||||
|
||||
@@ -147,9 +159,9 @@ struct ThreadPool : public std::vector<Thread*> {
|
||||
void init(); // No c'tor and d'tor, threads rely on globals that should
|
||||
void exit(); // be initialized and valid during the whole thread lifetime.
|
||||
|
||||
MainThread* main_thread() { return static_cast<MainThread*>((*this)[0]); }
|
||||
MainThread* main() { return static_cast<MainThread*>((*this)[0]); }
|
||||
void read_uci_options();
|
||||
Thread* available_slave(Thread* master) const;
|
||||
Thread* available_slave(const Thread* master) const;
|
||||
void wait_for_think_finished();
|
||||
void start_thinking(const Position&, const Search::LimitsType&,
|
||||
const std::vector<Move>&, Search::StateStackPtr&);
|
||||
@@ -164,4 +176,4 @@ struct ThreadPool : public std::vector<Thread*> {
|
||||
|
||||
extern ThreadPool Threads;
|
||||
|
||||
#endif // !defined(THREAD_H_INCLUDED)
|
||||
#endif // #ifndef THREAD_H_INCLUDED
|
||||
|
||||
+12
-13
@@ -17,8 +17,8 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "search.h"
|
||||
#include "timeman.h"
|
||||
@@ -29,8 +29,8 @@ namespace {
|
||||
/// Constants
|
||||
|
||||
const int MoveHorizon = 50; // Plan time management at most this many moves ahead
|
||||
const float MaxRatio = 7.0f; // When in trouble, we can step over reserved time with this ratio
|
||||
const float StealRatio = 0.33f; // However we must not steal time from remaining moves over this ratio
|
||||
const double MaxRatio = 7.0; // When in trouble, we can step over reserved time with this ratio
|
||||
const double StealRatio = 0.33; // However we must not steal time from remaining moves over this ratio
|
||||
|
||||
|
||||
// MoveImportance[] is based on naive statistical analysis of "how many games are still undecided
|
||||
@@ -76,10 +76,9 @@ namespace {
|
||||
}
|
||||
|
||||
|
||||
void TimeManager::pv_instability(int curChanges, int prevChanges) {
|
||||
void TimeManager::pv_instability(double bestMoveChanges) {
|
||||
|
||||
unstablePVExtraTime = curChanges * (optimumSearchTime / 2)
|
||||
+ prevChanges * (optimumSearchTime / 3);
|
||||
unstablePVExtraTime = int(bestMoveChanges * optimumSearchTime / 1.4);
|
||||
}
|
||||
|
||||
|
||||
@@ -115,7 +114,7 @@ void TimeManager::init(const Search::LimitsType& limits, int currentPly, Color u
|
||||
|
||||
// We calculate optimum time usage for different hypothetic "moves to go"-values and choose the
|
||||
// minimum of calculated search time values. Usually the greatest hypMTG gives the minimum values.
|
||||
for (hypMTG = 1; hypMTG <= (limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon); hypMTG++)
|
||||
for (hypMTG = 1; hypMTG <= (limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon); ++hypMTG)
|
||||
{
|
||||
// Calculate thinking time for hypothetic "moves to go"-value
|
||||
hypMyTime = limits.time[us]
|
||||
@@ -145,17 +144,17 @@ namespace {
|
||||
template<TimeType T>
|
||||
int remaining(int myTime, int movesToGo, int currentPly, int slowMover)
|
||||
{
|
||||
const float TMaxRatio = (T == OptimumTime ? 1 : MaxRatio);
|
||||
const float TStealRatio = (T == OptimumTime ? 0 : StealRatio);
|
||||
const double TMaxRatio = (T == OptimumTime ? 1 : MaxRatio);
|
||||
const double TStealRatio = (T == OptimumTime ? 0 : StealRatio);
|
||||
|
||||
int thisMoveImportance = move_importance(currentPly) * slowMover / 100;
|
||||
double thisMoveImportance = double(move_importance(currentPly) * slowMover) / 100;
|
||||
int otherMovesImportance = 0;
|
||||
|
||||
for (int i = 1; i < movesToGo; i++)
|
||||
for (int i = 1; i < movesToGo; ++i)
|
||||
otherMovesImportance += move_importance(currentPly + 2 * i);
|
||||
|
||||
float ratio1 = (TMaxRatio * thisMoveImportance) / float(TMaxRatio * thisMoveImportance + otherMovesImportance);
|
||||
float ratio2 = (thisMoveImportance + TStealRatio * otherMovesImportance) / float(thisMoveImportance + otherMovesImportance);
|
||||
double ratio1 = (TMaxRatio * thisMoveImportance) / (TMaxRatio * thisMoveImportance + otherMovesImportance);
|
||||
double ratio2 = (thisMoveImportance + TStealRatio * otherMovesImportance) / (thisMoveImportance + otherMovesImportance);
|
||||
|
||||
return int(floor(myTime * std::min(ratio1, ratio2)));
|
||||
}
|
||||
|
||||
+3
-3
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(TIMEMAN_H_INCLUDED)
|
||||
#ifndef TIMEMAN_H_INCLUDED
|
||||
#define TIMEMAN_H_INCLUDED
|
||||
|
||||
/// The TimeManager class computes the optimal time to think depending on the
|
||||
@@ -26,7 +26,7 @@
|
||||
class TimeManager {
|
||||
public:
|
||||
void init(const Search::LimitsType& limits, int currentPly, Color us);
|
||||
void pv_instability(int curChanges, int prevChanges);
|
||||
void pv_instability(double bestMoveChanges);
|
||||
int available_time() const { return optimumSearchTime + unstablePVExtraTime; }
|
||||
int maximum_time() const { return maximumSearchTime; }
|
||||
|
||||
@@ -36,4 +36,4 @@ private:
|
||||
int unstablePVExtraTime;
|
||||
};
|
||||
|
||||
#endif // !defined(TIMEMAN_H_INCLUDED)
|
||||
#endif // #ifndef TIMEMAN_H_INCLUDED
|
||||
|
||||
+31
-31
@@ -40,17 +40,17 @@ void TranspositionTable::set_size(size_t mbSize) {
|
||||
return;
|
||||
|
||||
hashMask = size - ClusterSize;
|
||||
delete [] table;
|
||||
table = new (std::nothrow) TTEntry[size];
|
||||
free(mem);
|
||||
mem = calloc(size * sizeof(TTEntry) + CACHE_LINE_SIZE - 1, 1);
|
||||
|
||||
if (!table)
|
||||
if (!mem)
|
||||
{
|
||||
std::cerr << "Failed to allocate " << mbSize
|
||||
<< "MB for transposition table." << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
clear(); // Operator new is not guaranteed to initialize memory to zero
|
||||
table = (TTEntry*)((uintptr_t(mem) + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1));
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +60,24 @@ void TranspositionTable::set_size(size_t mbSize) {
|
||||
|
||||
void TranspositionTable::clear() {
|
||||
|
||||
memset(table, 0, (hashMask + ClusterSize) * sizeof(TTEntry));
|
||||
std::memset(table, 0, (hashMask + ClusterSize) * sizeof(TTEntry));
|
||||
}
|
||||
|
||||
|
||||
/// TranspositionTable::probe() looks up the current position in the
|
||||
/// transposition table. Returns a pointer to the TTEntry or NULL if
|
||||
/// position is not found.
|
||||
|
||||
const TTEntry* TranspositionTable::probe(const Key key) const {
|
||||
|
||||
const TTEntry* tte = first_entry(key);
|
||||
uint32_t key32 = key >> 32;
|
||||
|
||||
for (unsigned i = 0; i < ClusterSize; ++i, ++tte)
|
||||
if (tte->key() == key32)
|
||||
return tte;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +89,7 @@ void TranspositionTable::clear() {
|
||||
/// more valuable than a TTEntry t2 if t1 is from the current search and t2 is from
|
||||
/// a previous search, or if the depth of t1 is bigger than the depth of t2.
|
||||
|
||||
void TranspositionTable::store(const Key key, Value v, Bound t, Depth d, Move m, Value statV, Value kingD) {
|
||||
void TranspositionTable::store(const Key key, Value v, Bound b, Depth d, Move m, Value statV) {
|
||||
|
||||
int c1, c2, c3;
|
||||
TTEntry *tte, *replace;
|
||||
@@ -80,42 +97,25 @@ void TranspositionTable::store(const Key key, Value v, Bound t, Depth d, Move m,
|
||||
|
||||
tte = replace = first_entry(key);
|
||||
|
||||
for (unsigned i = 0; i < ClusterSize; i++, tte++)
|
||||
for (unsigned i = 0; i < ClusterSize; ++i, ++tte)
|
||||
{
|
||||
if (!tte->key() || tte->key() == key32) // Empty or overwrite old
|
||||
{
|
||||
// Preserve any existing ttMove
|
||||
if (m == MOVE_NONE)
|
||||
m = tte->move();
|
||||
if (!m)
|
||||
m = tte->move(); // Preserve any existing ttMove
|
||||
|
||||
tte->save(key32, v, t, d, m, generation, statV, kingD);
|
||||
return;
|
||||
replace = tte;
|
||||
break;
|
||||
}
|
||||
|
||||
// Implement replace strategy
|
||||
c1 = (replace->generation() == generation ? 2 : 0);
|
||||
c2 = (tte->generation() == generation || tte->type() == BOUND_EXACT ? -2 : 0);
|
||||
c2 = (tte->generation() == generation || tte->bound() == BOUND_EXACT ? -2 : 0);
|
||||
c3 = (tte->depth() < replace->depth() ? 1 : 0);
|
||||
|
||||
if (c1 + c2 + c3 > 0)
|
||||
replace = tte;
|
||||
}
|
||||
replace->save(key32, v, t, d, m, generation, statV, kingD);
|
||||
}
|
||||
|
||||
|
||||
/// TranspositionTable::probe() looks up the current position in the
|
||||
/// transposition table. Returns a pointer to the TTEntry or NULL if
|
||||
/// position is not found.
|
||||
|
||||
TTEntry* TranspositionTable::probe(const Key key) const {
|
||||
|
||||
TTEntry* tte = first_entry(key);
|
||||
uint32_t key32 = key >> 32;
|
||||
|
||||
for (unsigned i = 0; i < ClusterSize; i++, tte++)
|
||||
if (tte->key() == key32)
|
||||
return tte;
|
||||
|
||||
return NULL;
|
||||
|
||||
replace->save(key32, v, b, d, m, generation, statV);
|
||||
}
|
||||
|
||||
@@ -17,60 +17,50 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(TT_H_INCLUDED)
|
||||
#ifndef TT_H_INCLUDED
|
||||
#define TT_H_INCLUDED
|
||||
|
||||
#include "misc.h"
|
||||
#include "types.h"
|
||||
|
||||
/// The TTEntry is the class of transposition table entries
|
||||
/// The TTEntry is the 128 bit transposition table entry, defined as below:
|
||||
///
|
||||
/// A TTEntry needs 128 bits to be stored
|
||||
///
|
||||
/// bit 0-31: key
|
||||
/// bit 32-63: data
|
||||
/// bit 64-79: value
|
||||
/// bit 80-95: depth
|
||||
/// bit 96-111: static value
|
||||
/// bit 112-127: margin of static value
|
||||
///
|
||||
/// the 32 bits of the data field are so defined
|
||||
///
|
||||
/// bit 0-15: move
|
||||
/// bit 16-20: not used
|
||||
/// bit 21-22: value type
|
||||
/// bit 23-31: generation
|
||||
/// key: 32 bit
|
||||
/// move: 16 bit
|
||||
/// bound type: 8 bit
|
||||
/// generation: 8 bit
|
||||
/// value: 16 bit
|
||||
/// depth: 16 bit
|
||||
/// static value: 16 bit
|
||||
/// static margin: 16 bit
|
||||
|
||||
class TTEntry {
|
||||
struct TTEntry {
|
||||
|
||||
public:
|
||||
void save(uint32_t k, Value v, Bound b, Depth d, Move m, int g, Value ev, Value em) {
|
||||
void save(uint32_t k, Value v, Bound b, Depth d, Move m, int g, Value ev) {
|
||||
|
||||
key32 = (uint32_t)k;
|
||||
move16 = (uint16_t)m;
|
||||
bound = (uint8_t)b;
|
||||
bound8 = (uint8_t)b;
|
||||
generation8 = (uint8_t)g;
|
||||
value16 = (int16_t)v;
|
||||
depth16 = (int16_t)d;
|
||||
evalValue = (int16_t)ev;
|
||||
evalMargin = (int16_t)em;
|
||||
}
|
||||
void set_generation(int g) { generation8 = (uint8_t)g; }
|
||||
void set_generation(uint8_t g) { generation8 = g; }
|
||||
|
||||
uint32_t key() const { return key32; }
|
||||
Depth depth() const { return (Depth)depth16; }
|
||||
Move move() const { return (Move)move16; }
|
||||
Value value() const { return (Value)value16; }
|
||||
Bound type() const { return (Bound)bound; }
|
||||
Bound bound() const { return (Bound)bound8; }
|
||||
int generation() const { return (int)generation8; }
|
||||
Value eval_value() const { return (Value)evalValue; }
|
||||
Value eval_margin() const { return (Value)evalMargin; }
|
||||
|
||||
private:
|
||||
uint32_t key32;
|
||||
uint16_t move16;
|
||||
uint8_t bound, generation8;
|
||||
int16_t value16, depth16, evalValue, evalMargin;
|
||||
uint8_t bound8, generation8;
|
||||
int16_t value16, depth16, evalValue;
|
||||
};
|
||||
|
||||
|
||||
@@ -85,20 +75,21 @@ class TranspositionTable {
|
||||
static const unsigned ClusterSize = 4; // A cluster is 64 Bytes
|
||||
|
||||
public:
|
||||
~TranspositionTable() { delete [] table; }
|
||||
void new_search() { generation++; }
|
||||
~TranspositionTable() { free(mem); }
|
||||
void new_search() { ++generation; }
|
||||
|
||||
TTEntry* probe(const Key key) const;
|
||||
const TTEntry* probe(const Key key) const;
|
||||
TTEntry* first_entry(const Key key) const;
|
||||
void refresh(const TTEntry* tte) const;
|
||||
void set_size(size_t mbSize);
|
||||
void clear();
|
||||
void store(const Key key, Value v, Bound type, Depth d, Move m, Value statV, Value kingD);
|
||||
void store(const Key key, Value v, Bound type, Depth d, Move m, Value statV);
|
||||
|
||||
private:
|
||||
uint32_t hashMask;
|
||||
TTEntry* table;
|
||||
uint8_t generation; // Size must be not bigger then TTEntry::generation8
|
||||
void* mem;
|
||||
uint8_t generation; // Size must be not bigger than TTEntry::generation8
|
||||
};
|
||||
|
||||
extern TranspositionTable TT;
|
||||
@@ -122,4 +113,4 @@ inline void TranspositionTable::refresh(const TTEntry* tte) const {
|
||||
const_cast<TTEntry*>(tte)->set_generation(generation);
|
||||
}
|
||||
|
||||
#endif // !defined(TT_H_INCLUDED)
|
||||
#endif // #ifndef TT_H_INCLUDED
|
||||
|
||||
+45
-98
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(TYPES_H_INCLUDED)
|
||||
#ifndef TYPES_H_INCLUDED
|
||||
#define TYPES_H_INCLUDED
|
||||
|
||||
/// For Linux and OSX configuration is done automatically using Makefile. To get
|
||||
@@ -42,6 +42,8 @@
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
#define unlikely(x) (x) // For code annotation purposes
|
||||
|
||||
#if defined(_WIN64) && !defined(IS_64BIT)
|
||||
# include <intrin.h> // MSVC popcnt and bsfq instrinsics
|
||||
# define IS_64BIT
|
||||
@@ -56,13 +58,14 @@
|
||||
# include <xmmintrin.h> // Intel and Microsoft header for _mm_prefetch()
|
||||
# endif
|
||||
|
||||
#define CACHE_LINE_SIZE 64
|
||||
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
|
||||
# define CACHE_LINE_ALIGNMENT __declspec(align(64))
|
||||
# define CACHE_LINE_ALIGNMENT __declspec(align(CACHE_LINE_SIZE))
|
||||
#else
|
||||
# define CACHE_LINE_ALIGNMENT __attribute__ ((aligned(64)))
|
||||
# define CACHE_LINE_ALIGNMENT __attribute__ ((aligned(CACHE_LINE_SIZE)))
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#ifdef _MSC_VER
|
||||
# define FORCE_INLINE __forceinline
|
||||
#elif defined(__GNUC__)
|
||||
# define FORCE_INLINE inline __attribute__((always_inline))
|
||||
@@ -70,13 +73,13 @@
|
||||
# define FORCE_INLINE inline
|
||||
#endif
|
||||
|
||||
#if defined(USE_POPCNT)
|
||||
#ifdef USE_POPCNT
|
||||
const bool HasPopCnt = true;
|
||||
#else
|
||||
const bool HasPopCnt = false;
|
||||
#endif
|
||||
|
||||
#if defined(IS_64BIT)
|
||||
#ifdef IS_64BIT
|
||||
const bool Is64Bit = true;
|
||||
#else
|
||||
const bool Is64Bit = false;
|
||||
@@ -85,28 +88,9 @@ const bool Is64Bit = false;
|
||||
typedef uint64_t Key;
|
||||
typedef uint64_t Bitboard;
|
||||
|
||||
const int MAX_MOVES = 192;
|
||||
const int MAX_MOVES = 256;
|
||||
const int MAX_PLY = 100;
|
||||
const int MAX_PLY_PLUS_2 = MAX_PLY + 2;
|
||||
|
||||
const Bitboard FileABB = 0x0101010101010101ULL;
|
||||
const Bitboard FileBBB = FileABB << 1;
|
||||
const Bitboard FileCBB = FileABB << 2;
|
||||
const Bitboard FileDBB = FileABB << 3;
|
||||
const Bitboard FileEBB = FileABB << 4;
|
||||
const Bitboard FileFBB = FileABB << 5;
|
||||
const Bitboard FileGBB = FileABB << 6;
|
||||
const Bitboard FileHBB = FileABB << 7;
|
||||
|
||||
const Bitboard Rank1BB = 0xFF;
|
||||
const Bitboard Rank2BB = Rank1BB << (8 * 1);
|
||||
const Bitboard Rank3BB = Rank1BB << (8 * 2);
|
||||
const Bitboard Rank4BB = Rank1BB << (8 * 3);
|
||||
const Bitboard Rank5BB = Rank1BB << (8 * 4);
|
||||
const Bitboard Rank6BB = Rank1BB << (8 * 5);
|
||||
const Bitboard Rank7BB = Rank1BB << (8 * 6);
|
||||
const Bitboard Rank8BB = Rank1BB << (8 * 7);
|
||||
|
||||
const int MAX_PLY_PLUS_6 = MAX_PLY + 6;
|
||||
|
||||
/// A move needs 16 bits to be stored
|
||||
///
|
||||
@@ -120,24 +104,24 @@ const Bitboard Rank8BB = Rank1BB << (8 * 7);
|
||||
/// while MOVE_NONE and MOVE_NULL have the same origin and destination square.
|
||||
|
||||
enum Move {
|
||||
MOVE_NONE = 0,
|
||||
MOVE_NONE,
|
||||
MOVE_NULL = 65
|
||||
};
|
||||
|
||||
enum MoveType {
|
||||
NORMAL = 0,
|
||||
NORMAL,
|
||||
PROMOTION = 1 << 14,
|
||||
ENPASSANT = 2 << 14,
|
||||
CASTLE = 3 << 14
|
||||
};
|
||||
|
||||
enum CastleRight { // Defined as in PolyGlot book hash key
|
||||
CASTLES_NONE = 0,
|
||||
WHITE_OO = 1,
|
||||
WHITE_OOO = 2,
|
||||
BLACK_OO = 4,
|
||||
BLACK_OOO = 8,
|
||||
ALL_CASTLES = 15,
|
||||
CASTLES_NONE,
|
||||
WHITE_OO,
|
||||
WHITE_OOO = WHITE_OO << 1,
|
||||
BLACK_OO = WHITE_OO << 2,
|
||||
BLACK_OOO = WHITE_OO << 3,
|
||||
ALL_CASTLES = WHITE_OO | WHITE_OOO | BLACK_OO | BLACK_OOO,
|
||||
CASTLE_RIGHT_NB = 16
|
||||
};
|
||||
|
||||
@@ -148,7 +132,7 @@ enum CastlingSide {
|
||||
};
|
||||
|
||||
enum Phase {
|
||||
PHASE_ENDGAME = 0,
|
||||
PHASE_ENDGAME,
|
||||
PHASE_MIDGAME = 128,
|
||||
MG = 0, EG = 1, PHASE_NB = 2
|
||||
};
|
||||
@@ -161,9 +145,9 @@ enum ScaleFactor {
|
||||
};
|
||||
|
||||
enum Bound {
|
||||
BOUND_NONE = 0,
|
||||
BOUND_UPPER = 1,
|
||||
BOUND_LOWER = 2,
|
||||
BOUND_NONE,
|
||||
BOUND_UPPER,
|
||||
BOUND_LOWER,
|
||||
BOUND_EXACT = BOUND_UPPER | BOUND_LOWER
|
||||
};
|
||||
|
||||
@@ -189,15 +173,15 @@ enum Value {
|
||||
};
|
||||
|
||||
enum PieceType {
|
||||
NO_PIECE_TYPE = 0, ALL_PIECES = 0,
|
||||
PAWN = 1, KNIGHT = 2, BISHOP = 3, ROOK = 4, QUEEN = 5, KING = 6,
|
||||
NO_PIECE_TYPE, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING,
|
||||
ALL_PIECES = 0,
|
||||
PIECE_TYPE_NB = 8
|
||||
};
|
||||
|
||||
enum Piece {
|
||||
NO_PIECE = 0,
|
||||
W_PAWN = 1, W_KNIGHT = 2, W_BISHOP = 3, W_ROOK = 4, W_QUEEN = 5, W_KING = 6,
|
||||
B_PAWN = 9, B_KNIGHT = 10, B_BISHOP = 11, B_ROOK = 12, B_QUEEN = 13, B_KING = 14,
|
||||
NO_PIECE,
|
||||
W_PAWN = 1, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
|
||||
B_PAWN = 9, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING,
|
||||
PIECE_NB = 16
|
||||
};
|
||||
|
||||
@@ -210,8 +194,8 @@ enum Depth {
|
||||
ONE_PLY = 2,
|
||||
|
||||
DEPTH_ZERO = 0 * ONE_PLY,
|
||||
DEPTH_QS_CHECKS = -1 * ONE_PLY,
|
||||
DEPTH_QS_NO_CHECKS = -2 * ONE_PLY,
|
||||
DEPTH_QS_CHECKS = 0 * ONE_PLY,
|
||||
DEPTH_QS_NO_CHECKS = -1 * ONE_PLY,
|
||||
DEPTH_QS_RECAPTURES = -5 * ONE_PLY,
|
||||
|
||||
DEPTH_NONE = -127 * ONE_PLY
|
||||
@@ -244,11 +228,11 @@ enum Square {
|
||||
};
|
||||
|
||||
enum File {
|
||||
FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_NB = 8
|
||||
FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_NB
|
||||
};
|
||||
|
||||
enum Rank {
|
||||
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB = 8
|
||||
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB
|
||||
};
|
||||
|
||||
|
||||
@@ -257,7 +241,7 @@ enum Rank {
|
||||
/// for midgame value. Compiler is free to choose the enum type as long as can
|
||||
/// keep its data, so ensure Score to be an integer type.
|
||||
enum Score {
|
||||
SCORE_ZERO = 0,
|
||||
SCORE_ZERO,
|
||||
SCORE_ENSURE_INTEGER_SIZE_P = INT_MAX,
|
||||
SCORE_ENSURE_INTEGER_SIZE_N = INT_MIN
|
||||
};
|
||||
@@ -267,7 +251,7 @@ inline Score make_score(int mg, int eg) { return Score((mg << 16) + eg); }
|
||||
/// Extracting the signed lower and upper 16 bits it not so trivial because
|
||||
/// according to the standard a simple cast to short is implementation defined
|
||||
/// and so is a right shift of a signed integer.
|
||||
inline Value mg_value(Score s) { return Value(((s + 32768) & ~0xffff) / 0x10000); }
|
||||
inline Value mg_value(Score s) { return Value(((s + 0x8000) & ~0xffff) / 0x10000); }
|
||||
|
||||
/// On Intel 64 bit we have a small speed regression with the standard conforming
|
||||
/// version, so use a faster code in this case that, although not 100% standard
|
||||
@@ -290,15 +274,15 @@ inline T operator-(const T d1, const T d2) { return T(int(d1) - int(d2)); } \
|
||||
inline T operator*(int i, const T d) { return T(i * int(d)); } \
|
||||
inline T operator*(const T d, int i) { return T(int(d) * i); } \
|
||||
inline T operator-(const T d) { return T(-int(d)); } \
|
||||
inline T& operator+=(T& d1, const T d2) { d1 = d1 + d2; return d1; } \
|
||||
inline T& operator-=(T& d1, const T d2) { d1 = d1 - d2; return d1; } \
|
||||
inline T& operator*=(T& d, int i) { d = T(int(d) * i); return d; }
|
||||
inline T& operator+=(T& d1, const T d2) { return d1 = d1 + d2; } \
|
||||
inline T& operator-=(T& d1, const T d2) { return d1 = d1 - d2; } \
|
||||
inline T& operator*=(T& d, int i) { return d = T(int(d) * i); }
|
||||
|
||||
#define ENABLE_OPERATORS_ON(T) ENABLE_SAFE_OPERATORS_ON(T) \
|
||||
inline T operator++(T& d, int) { d = T(int(d) + 1); return d; } \
|
||||
inline T operator--(T& d, int) { d = T(int(d) - 1); return d; } \
|
||||
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
|
||||
inline T& operator--(T& d) { return d = T(int(d) - 1); } \
|
||||
inline T operator/(const T d, int i) { return T(int(d) / i); } \
|
||||
inline T& operator/=(T& d, int i) { d = T(int(d) / i); return d; }
|
||||
inline T& operator/=(T& d, int i) { return d = T(int(d) / i); }
|
||||
|
||||
ENABLE_OPERATORS_ON(Value)
|
||||
ENABLE_OPERATORS_ON(PieceType)
|
||||
@@ -324,47 +308,26 @@ inline Score operator/(Score s, int i) {
|
||||
return make_score(mg_value(s) / i, eg_value(s) / i);
|
||||
}
|
||||
|
||||
/// Weight score v by score w trying to prevent overflow
|
||||
inline Score apply_weight(Score v, Score w) {
|
||||
return make_score((int(mg_value(v)) * mg_value(w)) / 0x100,
|
||||
(int(eg_value(v)) * eg_value(w)) / 0x100);
|
||||
}
|
||||
|
||||
#undef ENABLE_OPERATORS_ON
|
||||
#undef ENABLE_SAFE_OPERATORS_ON
|
||||
|
||||
namespace Zobrist {
|
||||
|
||||
extern Key psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
|
||||
extern Key enpassant[FILE_NB];
|
||||
extern Key castle[CASTLE_RIGHT_NB];
|
||||
extern Key side;
|
||||
extern Key exclusion;
|
||||
|
||||
void init();
|
||||
}
|
||||
|
||||
CACHE_LINE_ALIGNMENT
|
||||
|
||||
extern Score pieceSquareTable[PIECE_NB][SQUARE_NB];
|
||||
extern Value PieceValue[PHASE_NB][PIECE_NB];
|
||||
extern int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||
|
||||
struct MoveStack {
|
||||
struct ExtMove {
|
||||
Move move;
|
||||
int score;
|
||||
};
|
||||
|
||||
inline bool operator<(const MoveStack& f, const MoveStack& s) {
|
||||
inline bool operator<(const ExtMove& f, const ExtMove& s) {
|
||||
return f.score < s.score;
|
||||
}
|
||||
|
||||
inline Color operator~(Color c) {
|
||||
return Color(c ^ 1);
|
||||
return Color(c ^ BLACK);
|
||||
}
|
||||
|
||||
inline Square operator~(Square s) {
|
||||
return Square(s ^ 56); // Vertical flip SQ_A1 -> SQ_A8
|
||||
return Square(s ^ SQ_A8); // Vertical flip SQ_A1 -> SQ_A8
|
||||
}
|
||||
|
||||
inline Square operator|(File f, Rank r) {
|
||||
@@ -408,10 +371,6 @@ inline Rank rank_of(Square s) {
|
||||
return Rank(s >> 3);
|
||||
}
|
||||
|
||||
inline Square mirror(Square s) {
|
||||
return Square(s ^ 7); // Horizontal flip SQ_A1 -> SQ_H1
|
||||
}
|
||||
|
||||
inline Square relative_square(Color c, Square s) {
|
||||
return Square(s ^ (c * 56));
|
||||
}
|
||||
@@ -429,18 +388,6 @@ inline bool opposite_colors(Square s1, Square s2) {
|
||||
return ((s >> 3) ^ s) & 1;
|
||||
}
|
||||
|
||||
inline int file_distance(Square s1, Square s2) {
|
||||
return abs(file_of(s1) - file_of(s2));
|
||||
}
|
||||
|
||||
inline int rank_distance(Square s1, Square s2) {
|
||||
return abs(rank_of(s1) - rank_of(s2));
|
||||
}
|
||||
|
||||
inline int square_distance(Square s1, Square s2) {
|
||||
return SquareDistance[s1][s2];
|
||||
}
|
||||
|
||||
inline char file_to_char(File f, bool tolower = true) {
|
||||
return char(f - FILE_A + (tolower ? 'a' : 'A'));
|
||||
}
|
||||
@@ -489,4 +436,4 @@ inline const std::string square_to_string(Square s) {
|
||||
return ch;
|
||||
}
|
||||
|
||||
#endif // !defined(TYPES_H_INCLUDED)
|
||||
#endif // #ifndef TYPES_H_INCLUDED
|
||||
|
||||
+16
-12
@@ -42,8 +42,8 @@ namespace {
|
||||
// position just before to start searching). Needed by repetition draw detection.
|
||||
Search::StateStackPtr SetupStates;
|
||||
|
||||
void set_option(istringstream& up);
|
||||
void set_position(Position& pos, istringstream& up);
|
||||
void setoption(istringstream& up);
|
||||
void position(Position& pos, istringstream& up);
|
||||
void go(const Position& pos, istringstream& up);
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ namespace {
|
||||
|
||||
void UCI::loop(const string& args) {
|
||||
|
||||
Position pos(StartFEN, false, Threads.main_thread()); // The root position
|
||||
Position pos(StartFEN, false, Threads.main()); // The root position
|
||||
string token, cmd = args;
|
||||
|
||||
do {
|
||||
@@ -76,7 +76,7 @@ void UCI::loop(const string& args) {
|
||||
if (token != "ponderhit" || Search::Signals.stopOnPonderhit)
|
||||
{
|
||||
Search::Signals.stop = true;
|
||||
Threads.main_thread()->notify_one(); // Could be sleeping
|
||||
Threads.main()->notify_one(); // Could be sleeping
|
||||
}
|
||||
else
|
||||
Search::Limits.ponder = false;
|
||||
@@ -102,15 +102,19 @@ void UCI::loop(const string& args) {
|
||||
<< "\n" << Options
|
||||
<< "\nuciok" << sync_endl;
|
||||
|
||||
else if (token == "eval")
|
||||
{
|
||||
Search::RootColor = pos.side_to_move(); // Ensure it is set
|
||||
sync_cout << Eval::trace(pos) << sync_endl;
|
||||
}
|
||||
else if (token == "ucinewgame") { /* Avoid returning "Unknown command" */ }
|
||||
else if (token == "go") go(pos, is);
|
||||
else if (token == "position") set_position(pos, is);
|
||||
else if (token == "setoption") set_option(is);
|
||||
else if (token == "position") position(pos, is);
|
||||
else if (token == "setoption") setoption(is);
|
||||
else if (token == "flip") pos.flip();
|
||||
else if (token == "bench") benchmark(pos, is);
|
||||
else if (token == "d") sync_cout << pos.pretty() << sync_endl;
|
||||
else if (token == "isready") sync_cout << "readyok" << sync_endl;
|
||||
else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
|
||||
else
|
||||
sync_cout << "Unknown command: " << cmd << sync_endl;
|
||||
|
||||
@@ -122,12 +126,12 @@ void UCI::loop(const string& args) {
|
||||
|
||||
namespace {
|
||||
|
||||
// set_position() is called when engine receives the "position" UCI command.
|
||||
// position() is called when engine receives the "position" UCI command.
|
||||
// The function sets up the position described in the given fen string ("fen")
|
||||
// or the starting position ("startpos") and then makes the moves given in the
|
||||
// following move list ("moves").
|
||||
|
||||
void set_position(Position& pos, istringstream& is) {
|
||||
void position(Position& pos, istringstream& is) {
|
||||
|
||||
Move m;
|
||||
string token, fen;
|
||||
@@ -145,7 +149,7 @@ namespace {
|
||||
else
|
||||
return;
|
||||
|
||||
pos.set(fen, Options["UCI_Chess960"], Threads.main_thread());
|
||||
pos.set(fen, Options["UCI_Chess960"], Threads.main());
|
||||
SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
|
||||
|
||||
// Parse move list (if any)
|
||||
@@ -157,10 +161,10 @@ namespace {
|
||||
}
|
||||
|
||||
|
||||
// set_option() is called when engine receives the "setoption" UCI command. The
|
||||
// setoption() is called when engine receives the "setoption" UCI command. The
|
||||
// function updates the UCI option ("name") to the given value ("value").
|
||||
|
||||
void set_option(istringstream& is) {
|
||||
void setoption(istringstream& is) {
|
||||
|
||||
string token, name, value;
|
||||
|
||||
|
||||
+14
-17
@@ -51,31 +51,28 @@ bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const
|
||||
|
||||
|
||||
/// init() initializes the UCI options to their hard coded default values
|
||||
/// and initializes the default value of "Threads" and "Min Split Depth"
|
||||
/// parameters according to the number of CPU cores detected.
|
||||
|
||||
void init(OptionsMap& o) {
|
||||
|
||||
int cpus = std::min(cpu_count(), MAX_THREADS);
|
||||
int msd = cpus < 8 ? 4 : 7;
|
||||
|
||||
o["Use Debug Log"] = Option(false, on_logger);
|
||||
o["Use Search Log"] = Option(false);
|
||||
o["Write Debug Log"] = Option(false, on_logger);
|
||||
o["Write Search Log"] = Option(false);
|
||||
o["Search Log Filename"] = Option("SearchLog.txt");
|
||||
o["Book File"] = Option("book.bin");
|
||||
o["Best Book Move"] = Option(false);
|
||||
o["Contempt Factor"] = Option(0, -50, 50);
|
||||
o["Mobility (Middle Game)"] = Option(100, 0, 200, on_eval);
|
||||
o["Mobility (Midgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Mobility (Endgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Passed Pawns (Middle Game)"] = Option(100, 0, 200, on_eval);
|
||||
o["Pawn Structure (Midgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Pawn Structure (Endgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Passed Pawns (Midgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Passed Pawns (Endgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Space"] = Option(100, 0, 200, on_eval);
|
||||
o["Aggressiveness"] = Option(100, 0, 200, on_eval);
|
||||
o["Cowardice"] = Option(100, 0, 200, on_eval);
|
||||
o["Min Split Depth"] = Option(msd, 4, 12, on_threads);
|
||||
o["Max Threads per Split Point"] = Option(5, 4, 8, on_threads);
|
||||
o["Threads"] = Option(cpus, 1, MAX_THREADS, on_threads);
|
||||
o["Use Sleeping Threads"] = Option(false);
|
||||
o["Min Split Depth"] = Option(0, 0, 12, on_threads);
|
||||
o["Max Threads per Split Point"] = Option(5, 4, 8, on_threads);
|
||||
o["Threads"] = Option(1, 1, MAX_THREADS, on_threads);
|
||||
o["Idle Threads Sleep"] = Option(false);
|
||||
o["Hash"] = Option(32, 1, 8192, on_hash_size);
|
||||
o["Clear Hash"] = Option(on_clear_hash);
|
||||
o["Ponder"] = Option(true);
|
||||
@@ -83,10 +80,10 @@ void init(OptionsMap& o) {
|
||||
o["MultiPV"] = Option(1, 1, 500);
|
||||
o["Skill Level"] = Option(20, 0, 20);
|
||||
o["Emergency Move Horizon"] = Option(40, 0, 50);
|
||||
o["Emergency Base Time"] = Option(200, 0, 30000);
|
||||
o["Emergency Move Time"] = Option(70, 0, 5000);
|
||||
o["Emergency Base Time"] = Option(60, 0, 30000);
|
||||
o["Emergency Move Time"] = Option(30, 0, 5000);
|
||||
o["Minimum Thinking Time"] = Option(20, 0, 5000);
|
||||
o["Slow Mover"] = Option(100, 10, 1000);
|
||||
o["Slow Mover"] = Option(70, 10, 1000);
|
||||
o["UCI_Chess960"] = Option(false);
|
||||
o["UCI_AnalyseMode"] = Option(false, on_eval);
|
||||
}
|
||||
@@ -97,7 +94,7 @@ void init(OptionsMap& o) {
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
|
||||
|
||||
for (size_t idx = 0; idx < om.size(); idx++)
|
||||
for (size_t idx = 0; idx < om.size(); ++idx)
|
||||
for (OptionsMap::const_iterator it = om.begin(); it != om.end(); ++it)
|
||||
if (it->second.idx == idx)
|
||||
{
|
||||
|
||||
+2
-2
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(UCIOPTION_H_INCLUDED)
|
||||
#ifndef UCIOPTION_H_INCLUDED
|
||||
#define UCIOPTION_H_INCLUDED
|
||||
|
||||
#include <map>
|
||||
@@ -66,4 +66,4 @@ void loop(const std::string&);
|
||||
|
||||
extern UCI::OptionsMap Options;
|
||||
|
||||
#endif // !defined(UCIOPTION_H_INCLUDED)
|
||||
#endif // #ifndef UCIOPTION_H_INCLUDED
|
||||
|
||||
Reference in New Issue
Block a user