mirror of
https://github.com/official-stockfish/Stockfish.git
synced 2026-07-22 12:47:08 +00:00
Compare commits
+73
@@ -0,0 +1,73 @@
|
||||
language: cpp
|
||||
sudo: required
|
||||
dist: trusty
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- os: linux
|
||||
compiler: gcc
|
||||
addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test']
|
||||
packages: ['g++-6', 'g++-6-multilib', 'g++-multilib', 'valgrind', 'expect']
|
||||
env:
|
||||
- COMPILER=g++-6
|
||||
- COMP=gcc
|
||||
|
||||
- os: linux
|
||||
compiler: clang
|
||||
addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test']
|
||||
packages: ['clang', 'g++-multilib', 'valgrind', 'expect']
|
||||
env:
|
||||
- COMPILER=clang++
|
||||
- COMP=clang
|
||||
|
||||
- os: osx
|
||||
compiler: gcc
|
||||
env:
|
||||
- COMPILER=g++
|
||||
- COMP=gcc
|
||||
|
||||
- os: osx
|
||||
compiler: clang
|
||||
env:
|
||||
- COMPILER=clang++ V='Apple LLVM 6.0' # Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
|
||||
- COMP=clang
|
||||
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
|
||||
before_script:
|
||||
- cd src
|
||||
|
||||
script:
|
||||
# Obtain bench reference from git log
|
||||
- git log HEAD | grep "\b[Bb]ench[ :]\+[0-9]\{7\}" | head -n 1 | sed "s/[^0-9]*\([0-9][0-9]*\)/\1/g" > git_sig
|
||||
- export benchref=$(cat git_sig)
|
||||
- echo "Reference bench:" $benchref
|
||||
#
|
||||
# Verify bench number against various builds
|
||||
- export CXXFLAGS=-Werror
|
||||
- make clean && make -j2 ARCH=x86-64 optimize=no debug=yes build && ../tests/signature.sh $benchref
|
||||
- make clean && make -j2 ARCH=x86-32 optimize=no debug=yes build && ../tests/signature.sh $benchref
|
||||
- make clean && make -j2 ARCH=x86-32 build && ../tests/signature.sh $benchref
|
||||
- make clean && make -j2 ARCH=x86-64 build && ../tests/signature.sh $benchref
|
||||
#
|
||||
# Check perft and reproducible search
|
||||
- ../tests/perft.sh
|
||||
- ../tests/reprosearch.sh
|
||||
#
|
||||
# Valgrind
|
||||
#
|
||||
- export CXXFLAGS=-O1
|
||||
- if [ -x "$(command -v valgrind )" ]; then make clean && make -j2 ARCH=x86-64 debug=yes optimize=no build > /dev/null && ../tests/instrumented.sh --valgrind; fi
|
||||
- if [ -x "$(command -v valgrind )" ]; then ../tests/instrumented.sh --valgrind-thread; fi
|
||||
#
|
||||
# Sanitizer
|
||||
#
|
||||
# Use g++-6 as a proxy for having sanitizers, might need revision as they become available for more recent versions of clang/gcc
|
||||
- if [[ "$COMPILER" == "g++-6" ]]; then make clean && make -j2 ARCH=x86-64 sanitize=undefined optimize=no debug=yes build > /dev/null && ../tests/instrumented.sh --sanitizer-undefined; fi
|
||||
- if [[ "$COMPILER" == "g++-6" ]]; then make clean && make -j2 ARCH=x86-64 sanitize=thread optimize=no debug=yes build > /dev/null && ../tests/instrumented.sh --sanitizer-thread; fi
|
||||
@@ -1,64 +1,98 @@
|
||||
# Generated with git shortlog -sn | cut -c8-', which sorts by commits (manually moved Tord to top)
|
||||
# Generated with 'git shortlog -sn | cut -c8-', which sorts by commits, manually ordered the first four authors, merged duplicates
|
||||
|
||||
Tord Romstad
|
||||
Marco Costalba
|
||||
Joona Kiiski
|
||||
Gary Linscott
|
||||
Lucas Braesch
|
||||
Reuven Peleg
|
||||
Chris Caino
|
||||
homoSapiensSapiens
|
||||
Jean-Francois Romang
|
||||
jundery
|
||||
Arjun Temurnikar
|
||||
Uri Blass
|
||||
Ralph Stößer
|
||||
Stefan Geschwentner
|
||||
Tom Vijlbrief
|
||||
Daylen Yang
|
||||
Henri Wiechers
|
||||
Leonid Pechenik
|
||||
Marco Costalba (mcostalba)
|
||||
Joona Kiiski (zamar)
|
||||
Gary Linscott (glinscott)
|
||||
Lucas Braesch (lucasart)
|
||||
Bill Henry (VoyagerOne)
|
||||
mstembera
|
||||
Stéphane Nicolet (Stephane Nicolet, snicolet)
|
||||
Stefan Geschwentner
|
||||
Alain SAVARD (Rocky640)
|
||||
Jörg Oster (Joerg Oster, joergoster)
|
||||
Reuven Peleg
|
||||
Chris Caino (Chris Cain, ceebo)
|
||||
Jean-Francois Romang
|
||||
homoSapiensSapiens
|
||||
Leonid Pechenik
|
||||
Stefano Cardanobile (Stefano80)
|
||||
Arjun Temurnikar
|
||||
Uri Blass (uriblass)
|
||||
jundery
|
||||
Ajith (ajithcj)
|
||||
hxim
|
||||
Ralph Stößer (Ralph Stoesser)
|
||||
Guenther Demetz
|
||||
Jonathan Calovski (Mysseno)
|
||||
Tom Vijlbrief
|
||||
mbootsector
|
||||
Daylen Yang
|
||||
ElbertoOne
|
||||
Henri Wiechers
|
||||
loco-loco
|
||||
Joost VandeVondele (Joost Vandevondele)
|
||||
Ronald de Man (syzygy)
|
||||
DU-jdto
|
||||
David Zar
|
||||
Eelco de Groot
|
||||
Jerry Donald
|
||||
Jörg Oster
|
||||
NicklasPersson
|
||||
Ryan Schmitt
|
||||
mcostalba
|
||||
Alexander Kure
|
||||
Dan Schmidt
|
||||
H. Felix Wittmann
|
||||
Jacques
|
||||
Joseph R. Prostko
|
||||
Justin Blanchard
|
||||
Linus Arver
|
||||
Luca Brivio
|
||||
Lyudmil Antonov
|
||||
Rodrigo Exterckötter Tjäder
|
||||
Ron Britvich
|
||||
RyanTaker
|
||||
ceebo
|
||||
Joseph H Ellis
|
||||
Stephane Nicolet
|
||||
Ajith
|
||||
Vince Negri
|
||||
erbsenzaehler
|
||||
Joseph Hellis (jhellis3)
|
||||
shane31
|
||||
Andrew Grant
|
||||
Andy Duplain
|
||||
Auguste Pop
|
||||
Balint Pfliegel
|
||||
Chris Cain
|
||||
Dariusz Orzechowski
|
||||
DiscanX
|
||||
Ernesto Gatti
|
||||
Gregor Cramer
|
||||
Hiraoka Takuya
|
||||
Hiraoka Takuya (HiraokaTakuya)
|
||||
Hongzhi Cheng
|
||||
Jonathan Calovski
|
||||
Joseph R. Prostko
|
||||
IIvec
|
||||
Kelly Wilson
|
||||
Ken T Takusagawa
|
||||
Kojirion
|
||||
Krgp
|
||||
Matt Sullivan
|
||||
Matthew Lai
|
||||
Matthew Sullivan
|
||||
Michel Van den Bergh
|
||||
Mysseno
|
||||
Niklas Fiekas
|
||||
Oskar Werkelin Ahlin
|
||||
Pablo Vazquez
|
||||
Pascal Romaret
|
||||
Raminder Singh
|
||||
Richard Lloyd
|
||||
Ron Britvich
|
||||
Ronald de Man
|
||||
Ryan Takker
|
||||
Thanar2
|
||||
absimaldata
|
||||
atumanian
|
||||
braich
|
||||
fanon
|
||||
gamander
|
||||
gguliash
|
||||
kinderchocolate
|
||||
pellanda
|
||||
ppigazzini
|
||||
renouve
|
||||
shane31
|
||||
sf-x
|
||||
thaspel
|
||||
unknown
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
### Overview
|
||||
|
||||
[](https://travis-ci.org/official-stockfish/Stockfish)
|
||||
[](https://ci.appveyor.com/project/mcostalba/stockfish)
|
||||
|
||||
Stockfish is a free UCI chess engine derived from Glaurung 2.1. It is
|
||||
not a complete chess program and requires some UCI-compatible GUI
|
||||
(e.g. XBoard with PolyGlot, eboard, Arena, Sigma Chess, Shredder, Chess
|
||||
@@ -7,7 +10,7 @@ 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 128 cores. The engine defaults
|
||||
This version of Stockfish supports up to 512 cores. The engine defaults
|
||||
to one search thread, so 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.
|
||||
@@ -54,7 +57,7 @@ for correspondence games (because of tablebase adjudication).
|
||||
|
||||
The "SyzygyProbeLimit" option should normally be left at its default value.
|
||||
|
||||
**What to expect**
|
||||
**What to expect**
|
||||
If the engine is searching a position that is not in the tablebases (e.g.
|
||||
a position with 7 pieces), it will access the tablebases during the search.
|
||||
If the engine reports a very large score (typically 123.xx), this means
|
||||
@@ -93,6 +96,14 @@ compile (for instance with Microsoft MSVC) you need to manually
|
||||
set/unset some switches in the compiler command line; see file *types.h*
|
||||
for a quick reference.
|
||||
|
||||
### Resource For Understanding the Code Base
|
||||
|
||||
* [Chessprogramingwiki](https://chessprogramming.wikispaces.com) has good overall chess engines explanations
|
||||
(techniques used here are well explained like hash maps etc), it was
|
||||
also recommended by the [support at stockfish.](http://support.stockfishchess.org/discussions/questions/1132-how-to-understand-stockfish-sources)
|
||||
|
||||
* [Here](https://chessprogramming.wikispaces.com/Stockfish) you can find a set of features and techniques used by stockfish and each of them is explained at the wiki, however, it's a generic way rather than focusing on stockfish's own implementation, but it will still help you.
|
||||
|
||||
|
||||
### Terms of use
|
||||
|
||||
@@ -109,4 +120,4 @@ to where the source code can be found. If you make any changes to the
|
||||
source code, these changes must also be made available under the GPL.
|
||||
|
||||
For full details, read the copy of the GPL found in the file named
|
||||
*Copying.txt*
|
||||
*Copying.txt*.
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
Contributors with >10,000 CPU hours as of January 23, 2018
|
||||
Thank you!
|
||||
|
||||
Username CPU Hours Games played
|
||||
mibere 518300 41835669
|
||||
crunchy 375564 29121434
|
||||
cw 371664 28748719
|
||||
fastgm 299773 20765374
|
||||
JojoM 220590 15299913
|
||||
glinscott 204517 13932027
|
||||
bking_US 187568 12233168
|
||||
ctoks 169342 13475495
|
||||
spams 149531 10940322
|
||||
Thanar 137015 11714855
|
||||
velislav 127305 10047749
|
||||
vdbergh 121741 9056874
|
||||
malala 117291 8126488
|
||||
vdv 117218 8289983
|
||||
leszek 114825 8331897
|
||||
dsmith 114010 7622414
|
||||
CSU_Dynasty 113516 9582758
|
||||
sqrt2 112407 8782694
|
||||
marrco 111143 8222921
|
||||
drabel 108168 9061580
|
||||
BrunoBanani 104938 7448565
|
||||
Data 94621 8433010
|
||||
CoffeeOne 90394 3964243
|
||||
BRAVONE 80811 5341681
|
||||
psk 77195 6156031
|
||||
brabos 70284 5685893
|
||||
Fisherman 66650 5572406
|
||||
nssy 64587 5369140
|
||||
Pking_cda 64499 5704075
|
||||
sterni1971 63488 5070004
|
||||
mgrabiak 62385 5420812
|
||||
tvijlbrief 58957 4154234
|
||||
jromang 58854 4704502
|
||||
dv8silencer 57421 3961325
|
||||
sunu 56620 4609155
|
||||
tinker 56039 4204914
|
||||
biffhero 55743 4810039
|
||||
teddybaer 52982 4740444
|
||||
bcross 50548 5071599
|
||||
renouve 50318 3544864
|
||||
Freja 50296 3805120
|
||||
robnjr 47504 4131742
|
||||
eva42 46542 4044694
|
||||
davar 46538 4030604
|
||||
finfish 46244 3481661
|
||||
rap 46201 3219490
|
||||
ttruscott 45037 3645430
|
||||
solarlight 44155 4074841
|
||||
TueRens 41372 3891510
|
||||
ElbertoOne 41321 3920894
|
||||
Antihistamine 39218 2792761
|
||||
mhunt 38991 2697512
|
||||
bigpen0r 37820 3149955
|
||||
homyur 35569 3009637
|
||||
VoyagerOne 35137 3302650
|
||||
mhoram 34770 2684128
|
||||
racerschmacer 33022 3231055
|
||||
speedycpu 32043 2531964
|
||||
EthanOConnor 31638 2143255
|
||||
oryx 29574 2767730
|
||||
Pyafue 28885 1986098
|
||||
jkiiski 28014 1923255
|
||||
Garf 27579 2770144
|
||||
slakovv 27017 2031279
|
||||
Bobo1239 27000 2488707
|
||||
pb00067 26817 2306694
|
||||
robal 26337 2316795
|
||||
hyperbolic.tom 26248 2200777
|
||||
rkl 24898 2236013
|
||||
SC 23988 2126825
|
||||
nabildanial 23524 1586321
|
||||
achambord 23495 1942546
|
||||
Sharaf_DG 22975 1790697
|
||||
chriswk 22876 1947731
|
||||
anst 22568 2013953
|
||||
Patrick_G 22435 1682293
|
||||
cuistot 22201 1383031
|
||||
gri 21901 1820968
|
||||
Prcuvu 21182 1890546
|
||||
Zirie 21171 1493227
|
||||
JanErik 20596 1791991
|
||||
Isidor 20560 1730290
|
||||
xor12 20535 1819280
|
||||
team-oh 20364 1653708
|
||||
nesoneg 20264 1493435
|
||||
rstoesser 19802 1335177
|
||||
grandphish2 19402 1834196
|
||||
sg4032 18427 1671742
|
||||
dew 18263 1423326
|
||||
ianh2105 18133 1668562
|
||||
MazeOfGalious 18022 1644593
|
||||
ville 17900 1539130
|
||||
j3corre 17607 975954
|
||||
eudhan 17502 1424648
|
||||
iisiraider 17175 1118788
|
||||
jundery 17172 1115855
|
||||
SFTUser 16635 1363975
|
||||
purplefishies 16621 1106850
|
||||
DragonLord 16599 1252348
|
||||
chris 15274 1575333
|
||||
xoto 14900 1486261
|
||||
dju 14861 901552
|
||||
dex 14647 1228763
|
||||
nordlandia 14551 1369718
|
||||
ronaldjerum 14361 1210607
|
||||
OssumOpossum 14149 1029265
|
||||
IgorLeMasson 13844 1228391
|
||||
enedene 13762 935618
|
||||
ako027ako 13442 1250249
|
||||
AdrianSA 13324 924980
|
||||
bpfliegel 13318 886523
|
||||
ncfish1 13056 932344
|
||||
wei 12863 1369596
|
||||
jpulman 12776 854815
|
||||
horst.prack 12436 1151505
|
||||
joster 12424 986622
|
||||
cisco2015 12265 1205019
|
||||
fatmurphy 12015 901134
|
||||
modolief 11228 926456
|
||||
Dark_wizzie 11214 1017910
|
||||
mschmidt 10973 818594
|
||||
eastorwest 10970 1117836
|
||||
infinity 10762 746397
|
||||
SapphireBrand 10692 1024604
|
||||
Thomas A. 10553 736094
|
||||
pgontarz 10294 878746
|
||||
Andrew Grant 10195 922933
|
||||
stocky 10083 718114
|
||||
@@ -0,0 +1,71 @@
|
||||
version: 1.0.{build}
|
||||
clone_depth: 50
|
||||
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
- appveyor
|
||||
|
||||
# Operating system (build VM template)
|
||||
os: Visual Studio 2015
|
||||
|
||||
# Build platform, i.e. x86, x64, AnyCPU. This setting is optional.
|
||||
platform:
|
||||
- x86
|
||||
- x64
|
||||
|
||||
# build Configuration, i.e. Debug, Release, etc.
|
||||
configuration:
|
||||
- Debug
|
||||
- Release
|
||||
|
||||
matrix:
|
||||
# The build fail immediately once one of the job fails
|
||||
fast_finish: true
|
||||
|
||||
# Scripts that are called at very beginning, before repo cloning
|
||||
init:
|
||||
- cmake --version
|
||||
- msbuild /version
|
||||
|
||||
before_build:
|
||||
- ps: |
|
||||
# Get sources
|
||||
$src = get-childitem -Path *.cpp -Recurse | select -ExpandProperty FullName
|
||||
$src = $src -join ' '
|
||||
$src = $src.Replace("\", "/")
|
||||
|
||||
# Build CMakeLists.txt
|
||||
$t = 'cmake_minimum_required(VERSION 3.8)',
|
||||
'project(Stockfish)',
|
||||
'set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/src)',
|
||||
'set(source_files', $src, ')',
|
||||
'add_executable(stockfish ${source_files})'
|
||||
|
||||
# Write CMakeLists.txt withouth BOM
|
||||
$MyPath = (Get-Item -Path "." -Verbose).FullName + '\CMakeLists.txt'
|
||||
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
|
||||
[System.IO.File]::WriteAllLines($MyPath, $t, $Utf8NoBomEncoding)
|
||||
|
||||
# Obtain bench reference from git log
|
||||
$b = git log HEAD | sls "\b[Bb]ench[ :]+[0-9]{7}" | select -first 1
|
||||
$bench = $b -match '\D+(\d+)' | % { $matches[1] }
|
||||
Write-Host "Reference bench:" $bench
|
||||
$g = "Visual Studio 14 2015"
|
||||
If (${env:PLATFORM} -eq 'x64') { $g = $g + ' Win64' }
|
||||
cmake -G "${g}" .
|
||||
Write-Host "Generated files for: " $g
|
||||
|
||||
build_script:
|
||||
- cmake --build . --config %CONFIGURATION% -- /verbosity:minimal
|
||||
|
||||
before_test:
|
||||
- cd src/%CONFIGURATION%
|
||||
- ps: |
|
||||
# Verify bench number
|
||||
./stockfish bench 2> out.txt 1> null
|
||||
$s = (gc "./out.txt" | out-string)
|
||||
$r = ($s -match 'Nodes searched \D+(\d+)' | % { $matches[1] })
|
||||
Write-Host "Engine bench:" $r
|
||||
Write-Host "Reference bench:" $bench
|
||||
If ($r -ne $bench) { exit 1 }
|
||||
+174
-101
@@ -1,6 +1,7 @@
|
||||
# Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
# Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
# Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
# Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
#
|
||||
# Stockfish is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
@@ -21,7 +22,10 @@
|
||||
### ==========================================================================
|
||||
|
||||
### Establish the operating system name
|
||||
UNAME = $(shell uname)
|
||||
KERNEL = $(shell uname -s)
|
||||
ifeq ($(KERNEL),Linux)
|
||||
OS = $(shell uname -o)
|
||||
endif
|
||||
|
||||
### Executable name
|
||||
EXE = stockfish
|
||||
@@ -31,11 +35,11 @@ PREFIX = /usr/local
|
||||
BINDIR = $(PREFIX)/bin
|
||||
|
||||
### Built-in benchmark for pgo-builds
|
||||
PGOBENCH = ./$(EXE) bench 16 1 1000 default time
|
||||
PGOBENCH = ./$(EXE) bench
|
||||
|
||||
### Object files
|
||||
OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
|
||||
material.o misc.o movegen.o movepick.o pawns.o position.o \
|
||||
material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
|
||||
search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
|
||||
|
||||
### ==========================================================================
|
||||
@@ -46,13 +50,14 @@ OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
|
||||
# ----------------------------------------------------------------------------
|
||||
#
|
||||
# debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
|
||||
# sanitize = undefined/thread/no (-fsanitize )
|
||||
# --- ( undefined ) --- enable undefined behavior checks
|
||||
# --- ( thread ) --- enable threading error checks
|
||||
# optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
|
||||
# arch = (name) --- (-arch) --- Target architecture
|
||||
# bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
|
||||
# prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch x86 asm-instruction
|
||||
# bsfq = yes/no --- -DUSE_BSFQ --- Use bsfq x86_64 asm-instruction (only
|
||||
# with GCC and ICC 64-bit)
|
||||
# popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt x86_64 asm-instruction
|
||||
# prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch asm-instruction
|
||||
# popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt asm-instruction
|
||||
# sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
|
||||
# pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
|
||||
#
|
||||
@@ -63,9 +68,9 @@ OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
|
||||
### 2.1. General and architecture defaults
|
||||
optimize = yes
|
||||
debug = no
|
||||
sanitize = no
|
||||
bits = 32
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
sse = no
|
||||
pext = no
|
||||
@@ -95,7 +100,6 @@ ifeq ($(ARCH),x86-64)
|
||||
arch = x86_64
|
||||
bits = 64
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
sse = yes
|
||||
endif
|
||||
|
||||
@@ -103,7 +107,6 @@ ifeq ($(ARCH),x86-64-modern)
|
||||
arch = x86_64
|
||||
bits = 64
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
popcnt = yes
|
||||
sse = yes
|
||||
endif
|
||||
@@ -112,7 +115,6 @@ ifeq ($(ARCH),x86-64-bmi2)
|
||||
arch = x86_64
|
||||
bits = 64
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
popcnt = yes
|
||||
sse = yes
|
||||
pext = yes
|
||||
@@ -121,7 +123,6 @@ endif
|
||||
ifeq ($(ARCH),armv7)
|
||||
arch = armv7
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),ppc-32)
|
||||
@@ -140,7 +141,8 @@ endif
|
||||
|
||||
### 3.1 Selecting compiler (default = gcc)
|
||||
|
||||
CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti $(EXTRACXXFLAGS)
|
||||
CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++11 $(EXTRACXXFLAGS)
|
||||
DEPENDFLAGS += -std=c++11
|
||||
LDFLAGS += $(EXTRALDFLAGS)
|
||||
|
||||
ifeq ($(COMP),)
|
||||
@@ -150,14 +152,46 @@ endif
|
||||
ifeq ($(COMP),gcc)
|
||||
comp=gcc
|
||||
CXX=g++
|
||||
CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
|
||||
CXXFLAGS += -pedantic -Wextra -Wshadow
|
||||
|
||||
ifeq ($(ARCH),armv7)
|
||||
ifeq ($(OS),Android)
|
||||
CXXFLAGS += -m$(bits)
|
||||
LDFLAGS += -m$(bits)
|
||||
endif
|
||||
else
|
||||
CXXFLAGS += -m$(bits)
|
||||
LDFLAGS += -m$(bits)
|
||||
endif
|
||||
|
||||
ifneq ($(KERNEL),Darwin)
|
||||
LDFLAGS += -Wl,--no-as-needed
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(COMP),mingw)
|
||||
comp=mingw
|
||||
CXX=g++
|
||||
|
||||
ifeq ($(KERNEL),Linux)
|
||||
ifeq ($(bits),64)
|
||||
ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
|
||||
CXX=x86_64-w64-mingw32-c++
|
||||
else
|
||||
CXX=x86_64-w64-mingw32-c++-posix
|
||||
endif
|
||||
else
|
||||
ifeq ($(shell which i686-w64-mingw32-c++-posix),)
|
||||
CXX=i686-w64-mingw32-c++
|
||||
else
|
||||
CXX=i686-w64-mingw32-c++-posix
|
||||
endif
|
||||
endif
|
||||
else
|
||||
CXX=g++
|
||||
endif
|
||||
|
||||
CXXFLAGS += -Wextra -Wshadow
|
||||
LDFLAGS += -static-libstdc++ -static-libgcc
|
||||
LDFLAGS += -static
|
||||
endif
|
||||
|
||||
ifeq ($(COMP),icc)
|
||||
@@ -169,51 +203,84 @@ endif
|
||||
ifeq ($(COMP),clang)
|
||||
comp=clang
|
||||
CXX=clang++
|
||||
CXXFLAGS += -pedantic -Wno-long-long -Wextra -Wshadow
|
||||
CXXFLAGS += -pedantic -Wextra -Wshadow
|
||||
ifneq ($(KERNEL),Darwin)
|
||||
ifneq ($(KERNEL),OpenBSD)
|
||||
LDFLAGS += -latomic
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),armv7)
|
||||
ifeq ($(OS),Android)
|
||||
CXXFLAGS += -m$(bits)
|
||||
LDFLAGS += -m$(bits)
|
||||
endif
|
||||
else
|
||||
CXXFLAGS += -m$(bits)
|
||||
LDFLAGS += -m$(bits)
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(comp),icc)
|
||||
profile_prepare = icc-profile-prepare
|
||||
profile_make = icc-profile-make
|
||||
profile_use = icc-profile-use
|
||||
profile_clean = icc-profile-clean
|
||||
else
|
||||
profile_prepare = gcc-profile-prepare
|
||||
ifeq ($(comp),clang)
|
||||
profile_make = clang-profile-make
|
||||
profile_use = clang-profile-use
|
||||
else
|
||||
profile_make = gcc-profile-make
|
||||
profile_use = gcc-profile-use
|
||||
profile_clean = gcc-profile-clean
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(UNAME),Darwin)
|
||||
CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.6
|
||||
LDFLAGS += -arch $(arch) -mmacosx-version-min=10.6
|
||||
ifeq ($(KERNEL),Darwin)
|
||||
CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
|
||||
LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
|
||||
endif
|
||||
|
||||
### Travis CI script uses COMPILER to overwrite CXX
|
||||
ifdef COMPILER
|
||||
COMPCXX=$(COMPILER)
|
||||
endif
|
||||
|
||||
### Allow overwriting CXX from command line
|
||||
ifdef COMPCXX
|
||||
CXX=$(COMPCXX)
|
||||
endif
|
||||
|
||||
### On mingw use Windows threads, otherwise POSIX
|
||||
ifneq ($(comp),mingw)
|
||||
# On Android Bionic's C library comes with its own pthread implementation bundled in
|
||||
ifneq ($(arch),armv7)
|
||||
ifneq ($(OS),Android)
|
||||
# Haiku has pthreads in its libroot, so only link it in on other platforms
|
||||
ifneq ($(UNAME),Haiku)
|
||||
ifneq ($(KERNEL),Haiku)
|
||||
LDFLAGS += -lpthread
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
### 3.4 Debugging
|
||||
### 3.2.1 Debugging
|
||||
ifeq ($(debug),no)
|
||||
CXXFLAGS += -DNDEBUG
|
||||
else
|
||||
CXXFLAGS += -g
|
||||
endif
|
||||
|
||||
### 3.5 Optimization
|
||||
### 3.2.2 Debugging with undefined behavior sanitizers
|
||||
ifneq ($(sanitize),no)
|
||||
CXXFLAGS += -g3 -fsanitize=$(sanitize) -fuse-ld=gold
|
||||
LDFLAGS += -fsanitize=$(sanitize) -fuse-ld=gold
|
||||
endif
|
||||
|
||||
### 3.3 Optimization
|
||||
ifeq ($(optimize),yes)
|
||||
|
||||
ifeq ($(comp),gcc)
|
||||
CXXFLAGS += -O3
|
||||
CXXFLAGS += -O3
|
||||
|
||||
ifeq ($(UNAME),Darwin)
|
||||
ifeq ($(comp),gcc)
|
||||
|
||||
ifeq ($(KERNEL),Darwin)
|
||||
ifeq ($(arch),i386)
|
||||
CXXFLAGS += -mdynamic-no-pic
|
||||
endif
|
||||
@@ -222,31 +289,21 @@ ifeq ($(optimize),yes)
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(arch),armv7)
|
||||
ifeq ($(OS), Android)
|
||||
CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(comp),mingw)
|
||||
CXXFLAGS += -O3
|
||||
endif
|
||||
|
||||
ifeq ($(comp),icc)
|
||||
ifeq ($(UNAME),Darwin)
|
||||
CXXFLAGS += -fast -mdynamic-no-pic
|
||||
else
|
||||
CXXFLAGS += -fast
|
||||
ifeq ($(KERNEL),Darwin)
|
||||
CXXFLAGS += -mdynamic-no-pic
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(comp),clang)
|
||||
CXXFLAGS += -O3
|
||||
|
||||
ifeq ($(UNAME),Darwin)
|
||||
ifeq ($(pext),no)
|
||||
ifeq ($(KERNEL),Darwin)
|
||||
CXXFLAGS += -flto
|
||||
LDFLAGS += $(CXXFLAGS)
|
||||
endif
|
||||
ifeq ($(arch),i386)
|
||||
CXXFLAGS += -mdynamic-no-pic
|
||||
endif
|
||||
@@ -257,12 +314,12 @@ ifeq ($(optimize),yes)
|
||||
endif
|
||||
endif
|
||||
|
||||
### 3.6. Bits
|
||||
### 3.4 Bits
|
||||
ifeq ($(bits),64)
|
||||
CXXFLAGS += -DIS_64BIT
|
||||
endif
|
||||
|
||||
### 3.7 prefetch
|
||||
### 3.5 prefetch
|
||||
ifeq ($(prefetch),yes)
|
||||
ifeq ($(sse),yes)
|
||||
CXXFLAGS += -msse
|
||||
@@ -272,12 +329,7 @@ else
|
||||
CXXFLAGS += -DNO_PREFETCH
|
||||
endif
|
||||
|
||||
### 3.8 bsfq
|
||||
ifeq ($(bsfq),yes)
|
||||
CXXFLAGS += -DUSE_BSFQ
|
||||
endif
|
||||
|
||||
### 3.9 popcnt
|
||||
### 3.6 popcnt
|
||||
ifeq ($(popcnt),yes)
|
||||
ifeq ($(comp),icc)
|
||||
CXXFLAGS += -msse3 -DUSE_POPCNT
|
||||
@@ -286,33 +338,40 @@ ifeq ($(popcnt),yes)
|
||||
endif
|
||||
endif
|
||||
|
||||
### 3.10 pext
|
||||
### 3.7 pext
|
||||
ifeq ($(pext),yes)
|
||||
CXXFLAGS += -DUSE_PEXT
|
||||
ifeq ($(comp),$(filter $(comp),gcc clang mingw))
|
||||
CXXFLAGS += -mbmi -mbmi2
|
||||
CXXFLAGS += -mbmi2
|
||||
endif
|
||||
endif
|
||||
|
||||
### 3.11 Link Time Optimization, it works since gcc 4.5 but not on mingw.
|
||||
### 3.8 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
|
||||
### This is a mix of compile and link time options because the lto link phase
|
||||
### 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 \)))
|
||||
CXXFLAGS += -flto
|
||||
LDFLAGS += $(CXXFLAGS)
|
||||
endif
|
||||
CXXFLAGS += -flto
|
||||
LDFLAGS += $(CXXFLAGS)
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
### 3.12 Android 5 can only run position independent executables. Note that this
|
||||
ifeq ($(comp),mingw)
|
||||
ifeq ($(KERNEL),Linux)
|
||||
ifeq ($(optimize),yes)
|
||||
ifeq ($(debug),no)
|
||||
CXXFLAGS += -flto
|
||||
LDFLAGS += $(CXXFLAGS)
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
### 3.9 Android 5 can only run position independent executables. Note that this
|
||||
### breaks Android 4.0 and earlier.
|
||||
ifeq ($(arch),armv7)
|
||||
ifeq ($(OS), Android)
|
||||
CXXFLAGS += -fPIE
|
||||
LDFLAGS += -fPIE -pie
|
||||
endif
|
||||
@@ -326,7 +385,7 @@ help:
|
||||
@echo ""
|
||||
@echo "To compile stockfish, type: "
|
||||
@echo ""
|
||||
@echo "make target ARCH=arch [COMP=comp]"
|
||||
@echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
|
||||
@echo ""
|
||||
@echo "Supported targets:"
|
||||
@echo ""
|
||||
@@ -356,36 +415,39 @@ help:
|
||||
@echo "clang > LLVM Clang compiler"
|
||||
@echo "icc > Intel compiler"
|
||||
@echo ""
|
||||
@echo "Examples. If you don't know what to do, you likely want to run: "
|
||||
@echo "Simple examples. If you don't know what to do, you likely want to run: "
|
||||
@echo ""
|
||||
@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 ""
|
||||
@echo "Advanced examples, for experienced users: "
|
||||
@echo ""
|
||||
@echo "make build ARCH=x86-64 COMP=clang"
|
||||
@echo "make profile-build ARCH=x86-64-modern COMP=gcc COMPCXX=g++-4.8"
|
||||
@echo ""
|
||||
|
||||
.PHONY: build profile-build
|
||||
build:
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
|
||||
|
||||
.PHONY: help build profile-build strip install clean objclean profileclean help \
|
||||
config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
|
||||
clang-profile-use clang-profile-make
|
||||
|
||||
build: config-sanity
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
|
||||
|
||||
profile-build:
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
|
||||
profile-build: config-sanity objclean profileclean
|
||||
@echo ""
|
||||
@echo "Step 0/4. Preparing for profile build."
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
|
||||
@echo ""
|
||||
@echo "Step 1/4. Building executable for benchmark ..."
|
||||
@touch *.cpp *.h syzygy/*.cpp syzygy/*.h
|
||||
@echo "Step 1/4. Building instrumented executable ..."
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
|
||||
@echo ""
|
||||
@echo "Step 2/4. Running benchmark for pgo-build ..."
|
||||
@$(PGOBENCH) > /dev/null
|
||||
$(PGOBENCH) > /dev/null
|
||||
@echo ""
|
||||
@echo "Step 3/4. Building final executable ..."
|
||||
@touch *.cpp *.h syzygy/*.cpp syzygy/*.h
|
||||
@echo "Step 3/4. Building optimized executable ..."
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
|
||||
@echo ""
|
||||
@echo "Step 4/4. Deleting profile data ..."
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
|
||||
|
||||
strip:
|
||||
strip $(EXE)
|
||||
@@ -395,8 +457,19 @@ install:
|
||||
-cp $(EXE) $(BINDIR)
|
||||
-strip $(BINDIR)/$(EXE)
|
||||
|
||||
clean:
|
||||
$(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
|
||||
#clean all
|
||||
clean: objclean profileclean
|
||||
@rm -f .depend *~ core
|
||||
|
||||
# clean binaries and objects
|
||||
objclean:
|
||||
@rm -f $(EXE) $(EXE).exe *.o ./syzygy/*.o
|
||||
|
||||
# clean auxiliary profiling files
|
||||
profileclean:
|
||||
@rm -rf profdir
|
||||
@rm -f bench.txt *.gcda ./syzygy/*.gcda *.gcno ./syzygy/*.gcno
|
||||
@rm -f stockfish.profdata *.profraw
|
||||
|
||||
default:
|
||||
help
|
||||
@@ -411,11 +484,13 @@ config-sanity:
|
||||
@echo ""
|
||||
@echo "Config:"
|
||||
@echo "debug: '$(debug)'"
|
||||
@echo "sanitize: '$(sanitize)'"
|
||||
@echo "optimize: '$(optimize)'"
|
||||
@echo "arch: '$(arch)'"
|
||||
@echo "bits: '$(bits)'"
|
||||
@echo "kernel: '$(KERNEL)'"
|
||||
@echo "os: '$(OS)'"
|
||||
@echo "prefetch: '$(prefetch)'"
|
||||
@echo "bsfq: '$(bsfq)'"
|
||||
@echo "popcnt: '$(popcnt)'"
|
||||
@echo "sse: '$(sse)'"
|
||||
@echo "pext: '$(pext)'"
|
||||
@@ -428,12 +503,12 @@ config-sanity:
|
||||
@echo "Testing config sanity. If this fails, try 'make help' ..."
|
||||
@echo ""
|
||||
@test "$(debug)" = "yes" || test "$(debug)" = "no"
|
||||
@test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "no"
|
||||
@test "$(optimize)" = "yes" || test "$(optimize)" = "no"
|
||||
@test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
|
||||
test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
|
||||
@test "$(bits)" = "32" || test "$(bits)" = "64"
|
||||
@test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
|
||||
@test "$(bsfq)" = "yes" || test "$(bsfq)" = "no"
|
||||
@test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
|
||||
@test "$(sse)" = "yes" || test "$(sse)" = "no"
|
||||
@test "$(pext)" = "yes" || test "$(pext)" = "no"
|
||||
@@ -442,32 +517,33 @@ config-sanity:
|
||||
$(EXE): $(OBJS)
|
||||
$(CXX) -o $@ $(OBJS) $(LDFLAGS)
|
||||
|
||||
gcc-profile-prepare:
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
|
||||
clang-profile-make:
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||
EXTRACXXFLAGS='-fprofile-instr-generate ' \
|
||||
EXTRALDFLAGS=' -fprofile-instr-generate' \
|
||||
all
|
||||
|
||||
clang-profile-use:
|
||||
llvm-profdata merge -output=stockfish.profdata *.profraw
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||
EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
|
||||
EXTRALDFLAGS='-fprofile-use ' \
|
||||
all
|
||||
|
||||
gcc-profile-make:
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||
EXTRACXXFLAGS='-fprofile-arcs' \
|
||||
EXTRACXXFLAGS='-fprofile-generate' \
|
||||
EXTRALDFLAGS='-lgcov' \
|
||||
all
|
||||
|
||||
gcc-profile-use:
|
||||
# Deleting corrupt ucioption.gc* profile files is necessary to avoid an
|
||||
# "internal compiler error" for gcc versions 4.7.x
|
||||
@rm -f ucioption.gc*
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||
EXTRACXXFLAGS='-fbranch-probabilities' \
|
||||
EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
|
||||
EXTRALDFLAGS='-lgcov' \
|
||||
all
|
||||
|
||||
gcc-profile-clean:
|
||||
@rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
|
||||
|
||||
icc-profile-prepare:
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
|
||||
@mkdir profdir
|
||||
|
||||
icc-profile-make:
|
||||
@mkdir -p profdir
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||
EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
|
||||
all
|
||||
@@ -477,9 +553,6 @@ icc-profile-use:
|
||||
EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
|
||||
all
|
||||
|
||||
icc-profile-clean:
|
||||
@rm -rf profdir bench.txt
|
||||
|
||||
.depend:
|
||||
-@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null
|
||||
|
||||
|
||||
+44
-63
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,30 +18,25 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
#include "tt.h"
|
||||
#include "uci.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace {
|
||||
|
||||
const char* Defaults[] = {
|
||||
const vector<string> Defaults = {
|
||||
"setoption name UCI_Chess960 value false",
|
||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
||||
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 10",
|
||||
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 11",
|
||||
"4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19",
|
||||
"rq3rk1/ppp2ppp/1bnpb3/3N2B1/3NP3/7P/PPPQ1PP1/2KR3R w - - 7 14",
|
||||
"r1bq1r1k/1pp1n1pp/1p1p4/4p2Q/4Pp2/1BNP4/PPP2PPP/3R1RK1 w - - 2 14",
|
||||
"rq3rk1/ppp2ppp/1bnpb3/3N2B1/3NP3/7P/PPPQ1PP1/2KR3R w - - 7 14 moves d4e6",
|
||||
"r1bq1r1k/1pp1n1pp/1p1p4/4p2Q/4Pp2/1BNP4/PPP2PPP/3R1RK1 w - - 2 14 moves g2g4",
|
||||
"r3r1k1/2p2ppp/p1p1bn2/8/1q2P3/2NPQN2/PPP3PP/R4RK1 b - - 2 15",
|
||||
"r1bbk1nr/pp3p1p/2n5/1N4p1/2Np1B2/8/PPP2PPP/2KR1B1R w kq - 0 13",
|
||||
"r1bq1rk1/ppp1nppp/4n3/3p3Q/3P4/1BP1B3/PP1N2PP/R4RK1 w - - 1 16",
|
||||
@@ -53,7 +49,7 @@ const char* Defaults[] = {
|
||||
"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",
|
||||
"2K5/p7/7P/5pR1/8/5k2/r7/8 w - - 0 1 moves g5g6 f3e3 g6g5 e3f3",
|
||||
"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",
|
||||
@@ -78,23 +74,37 @@ const char* Defaults[] = {
|
||||
|
||||
// 7-man positions
|
||||
"8/R7/2q5/8/6k1/8/1P5p/K6R w - - 0 124", // Draw
|
||||
|
||||
// Mate and stalemate positions
|
||||
"6k1/3b3r/1p1p4/p1n2p2/1PPNpP1q/P3Q1p1/1R1RB1P1/5K2 b - - 0 1",
|
||||
"r2r1n2/pp2bk2/2p1p2p/3q4/3PN1QP/2P3R1/P4PP1/5RK1 w - - 0 1",
|
||||
"8/8/8/8/8/6k1/6p1/6K1 w - -",
|
||||
"7k/7P/6K1/8/3B4/8/8/8 b - -",
|
||||
|
||||
// Chess 960
|
||||
"setoption name UCI_Chess960 value true",
|
||||
"bbqnnrkr/pppppppp/8/8/8/8/PPPPPPPP/BBQNNRKR w KQkq - 0 1 moves g2g3 d7d5 d2d4 c8h3 c1g5 e8d6 g5e7 f7f6",
|
||||
"setoption name UCI_Chess960 value false"
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
/// benchmark() runs a simple benchmark by letting Stockfish analyze a set
|
||||
/// of positions for a given limit each. There are five parameters: the
|
||||
/// transposition table size, the number of search threads that should
|
||||
/// be used, the limit value spent for each position (optional, default is
|
||||
/// depth 13), an optional file name where to look for positions in FEN
|
||||
/// format (defaults are the positions defined above) and the type of the
|
||||
/// limit value: depth (default), time in millisecs or number of nodes.
|
||||
/// setup_bench() builds a list of UCI commands to be run by bench. There
|
||||
/// are five parameters: TT size in MB, number of search threads that
|
||||
/// should be used, the limit value spent for each position, a file name
|
||||
/// where to look for positions in FEN format and the type of the limit:
|
||||
/// depth, perft, nodes and movetime (in millisecs).
|
||||
///
|
||||
/// bench -> search default positions up to depth 13
|
||||
/// bench 64 1 15 -> search default positions up to depth 15 (TT = 64MB)
|
||||
/// bench 64 4 5000 current movetime -> search current position with 4 threads for 5 sec
|
||||
/// bench 64 1 100000 default nodes -> search default positions for 100K nodes each
|
||||
/// bench 16 1 5 default perft -> run a perft 5 on default positions
|
||||
|
||||
void benchmark(const Position& current, istream& is) {
|
||||
vector<string> setup_bench(const Position& current, istream& is) {
|
||||
|
||||
string token;
|
||||
Search::LimitsType limits;
|
||||
vector<string> fens;
|
||||
vector<string> fens, list;
|
||||
string go, token;
|
||||
|
||||
// Assign default values to missing arguments
|
||||
string ttSize = (is >> token) ? token : "16";
|
||||
@@ -103,24 +113,10 @@ void benchmark(const Position& current, istream& is) {
|
||||
string fenFile = (is >> token) ? token : "default";
|
||||
string limitType = (is >> token) ? token : "depth";
|
||||
|
||||
Options["Hash"] = ttSize;
|
||||
Options["Threads"] = threads;
|
||||
TT.clear();
|
||||
|
||||
if (limitType == "time")
|
||||
limits.movetime = atoi(limit.c_str()); // movetime is in ms
|
||||
|
||||
else if (limitType == "nodes")
|
||||
limits.nodes = atoi(limit.c_str());
|
||||
|
||||
else if (limitType == "mate")
|
||||
limits.mate = atoi(limit.c_str());
|
||||
|
||||
else
|
||||
limits.depth = atoi(limit.c_str());
|
||||
go = "go " + limitType + " " + limit;
|
||||
|
||||
if (fenFile == "default")
|
||||
fens.assign(Defaults, Defaults + 37);
|
||||
fens = Defaults;
|
||||
|
||||
else if (fenFile == "current")
|
||||
fens.push_back(current.fen());
|
||||
@@ -128,12 +124,12 @@ void benchmark(const Position& current, istream& is) {
|
||||
else
|
||||
{
|
||||
string fen;
|
||||
ifstream file(fenFile.c_str());
|
||||
ifstream file(fenFile);
|
||||
|
||||
if (!file.is_open())
|
||||
{
|
||||
cerr << "Unable to open file " << fenFile << endl;
|
||||
return;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
while (getline(file, fen))
|
||||
@@ -143,33 +139,18 @@ void benchmark(const Position& current, istream& is) {
|
||||
file.close();
|
||||
}
|
||||
|
||||
uint64_t nodes = 0;
|
||||
Search::StateStackPtr st;
|
||||
Time::point elapsed = Time::now();
|
||||
|
||||
for (size_t i = 0; i < fens.size(); ++i)
|
||||
{
|
||||
Position pos(fens[i], Options["UCI_Chess960"], Threads.main());
|
||||
|
||||
cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
|
||||
|
||||
if (limitType == "perft")
|
||||
nodes += Search::perft<true>(pos, limits.depth * ONE_PLY);
|
||||
list.emplace_back("ucinewgame");
|
||||
list.emplace_back("setoption name Threads value " + threads);
|
||||
list.emplace_back("setoption name Hash value " + ttSize);
|
||||
|
||||
for (const string& fen : fens)
|
||||
if (fen.find("setoption") != string::npos)
|
||||
list.emplace_back(fen);
|
||||
else
|
||||
{
|
||||
Threads.start_thinking(pos, limits, st);
|
||||
Threads.wait_for_think_finished();
|
||||
nodes += Search::RootPos.nodes_searched();
|
||||
list.emplace_back("position fen " + fen);
|
||||
list.emplace_back(go);
|
||||
}
|
||||
}
|
||||
|
||||
elapsed = std::max(Time::now() - elapsed, Time::point(1)); // Avoid a 'divide by zero'
|
||||
|
||||
dbg_print(); // Just before to exit
|
||||
|
||||
cerr << "\n==========================="
|
||||
<< "\nTotal time (ms) : " << elapsed
|
||||
<< "\nNodes searched : " << nodes
|
||||
<< "\nNodes/second : " << 1000 * nodes / elapsed << endl;
|
||||
return list;
|
||||
}
|
||||
|
||||
+51
-46
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +18,9 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
|
||||
#include "bitboard.h"
|
||||
@@ -51,20 +54,19 @@ namespace {
|
||||
WIN = 4
|
||||
};
|
||||
|
||||
inline Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
|
||||
Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
|
||||
|
||||
struct KPKPosition {
|
||||
|
||||
KPKPosition(unsigned idx);
|
||||
KPKPosition() = default;
|
||||
explicit KPKPosition(unsigned idx);
|
||||
operator Result() const { return result; }
|
||||
Result classify(const std::vector<KPKPosition>& db)
|
||||
{ return us == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
|
||||
|
||||
private:
|
||||
template<Color Us> Result classify(const std::vector<KPKPosition>& db);
|
||||
|
||||
Color us;
|
||||
Square bksq, wksq, psq;
|
||||
Square ksq[COLOR_NB], psq;
|
||||
Result result;
|
||||
};
|
||||
|
||||
@@ -82,13 +84,12 @@ bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color us) {
|
||||
|
||||
void Bitbases::init() {
|
||||
|
||||
std::vector<KPKPosition> db(MAX_INDEX);
|
||||
unsigned idx, repeat = 1;
|
||||
std::vector<KPKPosition> db;
|
||||
db.reserve(MAX_INDEX);
|
||||
|
||||
// Initialize db with known win / draw positions
|
||||
for (idx = 0; idx < MAX_INDEX; ++idx)
|
||||
db.push_back(KPKPosition(idx));
|
||||
db[idx] = KPKPosition(idx);
|
||||
|
||||
// Iterate through the positions until none of the unknown positions can be
|
||||
// changed to either wins or draws (15 cycles needed).
|
||||
@@ -107,69 +108,73 @@ namespace {
|
||||
|
||||
KPKPosition::KPKPosition(unsigned idx) {
|
||||
|
||||
wksq = Square((idx >> 0) & 0x3F);
|
||||
bksq = Square((idx >> 6) & 0x3F);
|
||||
us = Color ((idx >> 12) & 0x01);
|
||||
psq = make_square(File((idx >> 13) & 0x3), RANK_7 - Rank((idx >> 15) & 0x7));
|
||||
result = UNKNOWN;
|
||||
ksq[WHITE] = Square((idx >> 0) & 0x3F);
|
||||
ksq[BLACK] = Square((idx >> 6) & 0x3F);
|
||||
us = Color ((idx >> 12) & 0x01);
|
||||
psq = make_square(File((idx >> 13) & 0x3), Rank(RANK_7 - ((idx >> 15) & 0x7)));
|
||||
|
||||
// Check if two pieces are on the same square or if a king can be captured
|
||||
if ( distance(wksq, bksq) <= 1
|
||||
|| wksq == psq
|
||||
|| bksq == psq
|
||||
|| (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
|
||||
if ( distance(ksq[WHITE], ksq[BLACK]) <= 1
|
||||
|| ksq[WHITE] == psq
|
||||
|| ksq[BLACK] == psq
|
||||
|| (us == WHITE && (PawnAttacks[WHITE][psq] & ksq[BLACK])))
|
||||
result = INVALID;
|
||||
|
||||
else if (us == WHITE)
|
||||
{
|
||||
// Immediate win if a pawn can be promoted without getting captured
|
||||
if ( rank_of(psq) == RANK_7
|
||||
&& wksq != psq + DELTA_N
|
||||
&& ( distance(bksq, psq + DELTA_N) > 1
|
||||
||(StepAttacksBB[KING][wksq] & (psq + DELTA_N))))
|
||||
result = WIN;
|
||||
}
|
||||
// Immediate win if a pawn can be promoted without getting captured
|
||||
else if ( us == WHITE
|
||||
&& rank_of(psq) == RANK_7
|
||||
&& ksq[us] != psq + NORTH
|
||||
&& ( distance(ksq[~us], psq + NORTH) > 1
|
||||
|| (PseudoAttacks[KING][ksq[us]] & (psq + NORTH))))
|
||||
result = WIN;
|
||||
|
||||
// Immediate draw if it is a stalemate or a king captures undefended pawn
|
||||
else if ( !(StepAttacksBB[KING][bksq] & ~(StepAttacksBB[KING][wksq] | StepAttacksBB[PAWN][psq]))
|
||||
|| (StepAttacksBB[KING][bksq] & psq & ~StepAttacksBB[KING][wksq]))
|
||||
else if ( us == BLACK
|
||||
&& ( !(PseudoAttacks[KING][ksq[us]] & ~(PseudoAttacks[KING][ksq[~us]] | PawnAttacks[~us][psq]))
|
||||
|| (PseudoAttacks[KING][ksq[us]] & psq & ~PseudoAttacks[KING][ksq[~us]])))
|
||||
result = DRAW;
|
||||
|
||||
// Position will be classified later
|
||||
else
|
||||
result = UNKNOWN;
|
||||
}
|
||||
|
||||
template<Color Us>
|
||||
Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
|
||||
|
||||
// White to Move: If one move leads to a position classified as WIN, the result
|
||||
// White to move: If one move leads to a position classified as WIN, the result
|
||||
// of the current position is WIN. If all moves lead to positions classified
|
||||
// as DRAW, the current position is classified as DRAW, otherwise the current
|
||||
// position is classified as UNKNOWN.
|
||||
//
|
||||
// Black to Move: If one move leads to a position classified as DRAW, the result
|
||||
// Black to move: If one move leads to a position classified as DRAW, the result
|
||||
// of the current position is DRAW. If all moves lead to positions classified
|
||||
// as WIN, the position is classified as WIN, otherwise the current position is
|
||||
// classified as UNKNOWN.
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
const Result Good = (Us == WHITE ? WIN : DRAW);
|
||||
const Result Bad = (Us == WHITE ? DRAW : WIN);
|
||||
|
||||
Result r = INVALID;
|
||||
Bitboard b = StepAttacksBB[KING][Us == WHITE ? wksq : bksq];
|
||||
Bitboard b = PseudoAttacks[KING][ksq[Us]];
|
||||
|
||||
while (b)
|
||||
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(psq) == RANK_2 && s != wksq && s != bksq)
|
||||
r |= db[index(BLACK, bksq, wksq, s + DELTA_N)]; // Double push
|
||||
}
|
||||
r |= Us == WHITE ? db[index(Them, ksq[Them] , pop_lsb(&b), psq)]
|
||||
: db[index(Them, pop_lsb(&b), ksq[Them] , psq)];
|
||||
|
||||
if (Us == WHITE)
|
||||
return result = r & WIN ? WIN : r & UNKNOWN ? UNKNOWN : DRAW;
|
||||
else
|
||||
return result = r & DRAW ? DRAW : r & UNKNOWN ? UNKNOWN : WIN;
|
||||
{
|
||||
if (rank_of(psq) < RANK_7) // Single push
|
||||
r |= db[index(Them, ksq[Them], ksq[Us], psq + NORTH)];
|
||||
|
||||
if ( rank_of(psq) == RANK_2 // Double push
|
||||
&& psq + NORTH != ksq[Us]
|
||||
&& psq + NORTH != ksq[Them])
|
||||
r |= db[index(Them, ksq[Them], ksq[Us], psq + NORTH + NORTH)];
|
||||
}
|
||||
|
||||
return result = r & Good ? Good : r & UNKNOWN ? UNKNOWN : Bad;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
+91
-86
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -18,37 +19,29 @@
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring> // For std::memset
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "bitcount.h"
|
||||
#include "misc.h"
|
||||
|
||||
uint8_t PopCnt16[1 << 16];
|
||||
int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||
|
||||
Bitboard RookMasks [SQUARE_NB];
|
||||
Bitboard RookMagics [SQUARE_NB];
|
||||
Bitboard* RookAttacks[SQUARE_NB];
|
||||
unsigned RookShifts [SQUARE_NB];
|
||||
|
||||
Bitboard BishopMasks [SQUARE_NB];
|
||||
Bitboard BishopMagics [SQUARE_NB];
|
||||
Bitboard* BishopAttacks[SQUARE_NB];
|
||||
unsigned BishopShifts [SQUARE_NB];
|
||||
|
||||
Bitboard SquareBB[SQUARE_NB];
|
||||
Bitboard FileBB[FILE_NB];
|
||||
Bitboard RankBB[RANK_NB];
|
||||
Bitboard AdjacentFilesBB[FILE_NB];
|
||||
Bitboard InFrontBB[COLOR_NB][RANK_NB];
|
||||
Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
|
||||
Bitboard ForwardRanksBB[COLOR_NB][RANK_NB];
|
||||
Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
|
||||
Bitboard LineBB[SQUARE_NB][SQUARE_NB];
|
||||
Bitboard DistanceRingBB[SQUARE_NB][8];
|
||||
Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
|
||||
Bitboard ForwardFileBB[COLOR_NB][SQUARE_NB];
|
||||
Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
|
||||
Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
|
||||
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
||||
Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
|
||||
|
||||
Magic RookMagics[SQUARE_NB];
|
||||
Magic BishopMagics[SQUARE_NB];
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -56,36 +49,45 @@ namespace {
|
||||
const uint64_t DeBruijn64 = 0x3F79D71B4CB0A89ULL;
|
||||
const uint32_t DeBruijn32 = 0x783A9B23;
|
||||
|
||||
int MS1BTable[256]; // To implement software msb()
|
||||
int MSBTable[256]; // To implement software msb()
|
||||
Square BSFTable[SQUARE_NB]; // To implement software bitscan
|
||||
Bitboard RookTable[0x19000]; // To store rook attacks
|
||||
Bitboard BishopTable[0x1480]; // To store bishop attacks
|
||||
|
||||
typedef unsigned (Fn)(Square, Bitboard);
|
||||
|
||||
void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
|
||||
Bitboard masks[], unsigned shifts[], Square deltas[], Fn index);
|
||||
void init_magics(Bitboard table[], Magic magics[], Direction directions[]);
|
||||
|
||||
// bsf_index() returns the index into BSFTable[] to look up the bitscan. Uses
|
||||
// Matt Taylor's folding for 32 bit case, extended to 64 bit by Kim Walisch.
|
||||
|
||||
FORCE_INLINE unsigned bsf_index(Bitboard b) {
|
||||
unsigned bsf_index(Bitboard b) {
|
||||
b ^= b - 1;
|
||||
return Is64Bit ? (b * DeBruijn64) >> 58
|
||||
: ((unsigned(b) ^ unsigned(b >> 32)) * DeBruijn32) >> 26;
|
||||
}
|
||||
|
||||
|
||||
// popcount16() counts the non-zero bits using SWAR-Popcount algorithm
|
||||
|
||||
unsigned popcount16(unsigned u) {
|
||||
u -= (u >> 1) & 0x5555U;
|
||||
u = ((u >> 2) & 0x3333U) + (u & 0x3333U);
|
||||
u = ((u >> 4) + u) & 0x0F0FU;
|
||||
return (u * 0x0101U) >> 8;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef USE_BSFQ
|
||||
#ifdef NO_BSF
|
||||
|
||||
/// Software fall-back of lsb() and msb() for CPU lacking hardware support
|
||||
|
||||
Square lsb(Bitboard b) {
|
||||
assert(b);
|
||||
return BSFTable[bsf_index(b)];
|
||||
}
|
||||
|
||||
Square msb(Bitboard b) {
|
||||
|
||||
assert(b);
|
||||
unsigned b32;
|
||||
int result = 0;
|
||||
|
||||
@@ -109,10 +111,10 @@ Square msb(Bitboard b) {
|
||||
result += 8;
|
||||
}
|
||||
|
||||
return Square(result + MS1BTable[b32]);
|
||||
return Square(result + MSBTable[b32]);
|
||||
}
|
||||
|
||||
#endif // ifndef USE_BSFQ
|
||||
#endif // ifdef NO_BSF
|
||||
|
||||
|
||||
/// Bitboards::pretty() returns an ASCII representation of a bitboard suitable
|
||||
@@ -125,9 +127,9 @@ const std::string Bitboards::pretty(Bitboard b) {
|
||||
for (Rank r = RANK_8; r >= RANK_1; --r)
|
||||
{
|
||||
for (File f = FILE_A; f <= FILE_H; ++f)
|
||||
s.append(b & make_square(f, r) ? "| X " : "| ");
|
||||
s += b & make_square(f, r) ? "| X " : "| ";
|
||||
|
||||
s.append("|\n+---+---+---+---+---+---+---+---+\n");
|
||||
s += "|\n+---+---+---+---+---+---+---+---+\n";
|
||||
}
|
||||
|
||||
return s;
|
||||
@@ -139,14 +141,17 @@ const std::string Bitboards::pretty(Bitboard b) {
|
||||
|
||||
void Bitboards::init() {
|
||||
|
||||
for (unsigned i = 0; i < (1 << 16); ++i)
|
||||
PopCnt16[i] = (uint8_t) popcount16(i);
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||
{
|
||||
SquareBB[s] = 1ULL << s;
|
||||
BSFTable[bsf_index(SquareBB[s])] = s;
|
||||
}
|
||||
|
||||
for (Bitboard b = 1; b < 256; ++b)
|
||||
MS1BTable[b] = more_than_one(b) ? MS1BTable[b - 1] : lsb(b);
|
||||
for (Bitboard b = 2; b < 256; ++b)
|
||||
MSBTable[b] = MSBTable[b - 1] + !more_than_one(b);
|
||||
|
||||
for (File f = FILE_A; f <= FILE_H; ++f)
|
||||
FileBB[f] = f > FILE_A ? FileBB[f - 1] << 1 : FileABB;
|
||||
@@ -158,14 +163,14 @@ void Bitboards::init() {
|
||||
AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0);
|
||||
|
||||
for (Rank r = RANK_1; r < RANK_8; ++r)
|
||||
InFrontBB[WHITE][r] = ~(InFrontBB[BLACK][r + 1] = InFrontBB[BLACK][r] | RankBB[r]);
|
||||
ForwardRanksBB[WHITE][r] = ~(ForwardRanksBB[BLACK][r + 1] = ForwardRanksBB[BLACK][r] | RankBB[r]);
|
||||
|
||||
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)];
|
||||
PawnAttackSpan[c][s] = InFrontBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
|
||||
PassedPawnMask[c][s] = ForwardBB[c][s] | PawnAttackSpan[c][s];
|
||||
ForwardFileBB [c][s] = ForwardRanksBB[c][rank_of(s)] & FileBB[file_of(s)];
|
||||
PawnAttackSpan[c][s] = ForwardRanksBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
|
||||
PassedPawnMask[c][s] = ForwardFileBB [c][s] | PawnAttackSpan[c][s];
|
||||
}
|
||||
|
||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
||||
@@ -176,56 +181,58 @@ void Bitboards::init() {
|
||||
DistanceRingBB[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 } };
|
||||
int steps[][5] = { {}, { 7, 9 }, { 6, 10, 15, 17 }, {}, {}, {}, { 1, 7, 8, 9 } };
|
||||
|
||||
for (Color c = WHITE; c <= BLACK; ++c)
|
||||
for (PieceType pt = PAWN; pt <= KING; ++pt)
|
||||
for (PieceType pt : { PAWN, KNIGHT, KING })
|
||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||
for (int i = 0; steps[pt][i]; ++i)
|
||||
{
|
||||
Square to = s + Square(c == WHITE ? steps[pt][i] : -steps[pt][i]);
|
||||
Square to = s + Direction(c == WHITE ? steps[pt][i] : -steps[pt][i]);
|
||||
|
||||
if (is_ok(to) && distance(s, to) < 3)
|
||||
StepAttacksBB[make_piece(c, pt)][s] |= to;
|
||||
{
|
||||
if (pt == PAWN)
|
||||
PawnAttacks[c][s] |= to;
|
||||
else
|
||||
PseudoAttacks[pt][s] |= to;
|
||||
}
|
||||
}
|
||||
|
||||
Square RookDeltas[] = { DELTA_N, DELTA_E, DELTA_S, DELTA_W };
|
||||
Square BishopDeltas[] = { DELTA_NE, DELTA_SE, DELTA_SW, DELTA_NW };
|
||||
Direction RookDirections[] = { NORTH, EAST, SOUTH, WEST };
|
||||
Direction BishopDirections[] = { NORTH_EAST, SOUTH_EAST, SOUTH_WEST, NORTH_WEST };
|
||||
|
||||
init_magics(RookTable, RookAttacks, RookMagics, RookMasks, RookShifts, RookDeltas, magic_index<ROOK>);
|
||||
init_magics(BishopTable, BishopAttacks, BishopMagics, BishopMasks, BishopShifts, BishopDeltas, magic_index<BISHOP>);
|
||||
init_magics(RookTable, RookMagics, RookDirections);
|
||||
init_magics(BishopTable, BishopMagics, BishopDirections);
|
||||
|
||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
||||
{
|
||||
PseudoAttacks[QUEEN][s1] = PseudoAttacks[BISHOP][s1] = attacks_bb<BISHOP>(s1, 0);
|
||||
PseudoAttacks[QUEEN][s1] |= PseudoAttacks[ ROOK][s1] = attacks_bb< ROOK>(s1, 0);
|
||||
|
||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
|
||||
{
|
||||
Piece pc = (PseudoAttacks[BISHOP][s1] & s2) ? W_BISHOP :
|
||||
(PseudoAttacks[ROOK][s1] & s2) ? W_ROOK : NO_PIECE;
|
||||
for (PieceType pt : { BISHOP, ROOK })
|
||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
|
||||
{
|
||||
if (!(PseudoAttacks[pt][s1] & s2))
|
||||
continue;
|
||||
|
||||
if (pc == NO_PIECE)
|
||||
continue;
|
||||
|
||||
LineBB[s1][s2] = (attacks_bb(pc, s1, 0) & attacks_bb(pc, s2, 0)) | s1 | s2;
|
||||
BetweenBB[s1][s2] = attacks_bb(pc, s1, SquareBB[s2]) & attacks_bb(pc, s2, SquareBB[s1]);
|
||||
}
|
||||
LineBB[s1][s2] = (attacks_bb(pt, s1, 0) & attacks_bb(pt, s2, 0)) | s1 | s2;
|
||||
BetweenBB[s1][s2] = attacks_bb(pt, s1, SquareBB[s2]) & attacks_bb(pt, s2, SquareBB[s1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
Bitboard sliding_attack(Square deltas[], Square sq, Bitboard occupied) {
|
||||
Bitboard sliding_attack(Direction directions[], Square sq, Bitboard occupied) {
|
||||
|
||||
Bitboard attack = 0;
|
||||
|
||||
for (int i = 0; i < 4; ++i)
|
||||
for (Square s = sq + deltas[i];
|
||||
is_ok(s) && distance(s, s - deltas[i]) == 1;
|
||||
s += deltas[i])
|
||||
for (Square s = sq + directions[i];
|
||||
is_ok(s) && distance(s, s - directions[i]) == 1;
|
||||
s += directions[i])
|
||||
{
|
||||
attack |= s;
|
||||
|
||||
@@ -242,17 +249,14 @@ namespace {
|
||||
// chessprogramming.wikispaces.com/Magic+Bitboards. In particular, here we
|
||||
// use the so called "fancy" approach.
|
||||
|
||||
void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
|
||||
Bitboard masks[], unsigned shifts[], Square deltas[], Fn index) {
|
||||
void init_magics(Bitboard table[], Magic magics[], Direction directions[]) {
|
||||
|
||||
// Optimal PRNG seeds to pick the correct magics in the shortest time
|
||||
int seeds[][RANK_NB] = { { 8977, 44560, 54343, 38998, 5731, 95205, 104912, 17020 },
|
||||
{ 728, 10316, 55013, 32803, 12281, 15100, 16645, 255 } };
|
||||
|
||||
Bitboard occupancy[4096], reference[4096], edges, b;
|
||||
int i, size;
|
||||
|
||||
// attacks[s] is a pointer to the beginning of the attacks table for square 's'
|
||||
attacks[SQ_A1] = table;
|
||||
int epoch[4096] = {}, cnt = 0, size = 0;
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||
{
|
||||
@@ -264,28 +268,28 @@ namespace {
|
||||
// all the attacks for each possible subset of the mask and so is 2 power
|
||||
// the number of 1s of the mask. Hence we deduce the size of the shift to
|
||||
// apply to the 64 or 32 bits word to get the index.
|
||||
masks[s] = sliding_attack(deltas, s, 0) & ~edges;
|
||||
shifts[s] = (Is64Bit ? 64 : 32) - popcount<Max15>(masks[s]);
|
||||
Magic& m = magics[s];
|
||||
m.mask = sliding_attack(directions, s, 0) & ~edges;
|
||||
m.shift = (Is64Bit ? 64 : 32) - popcount(m.mask);
|
||||
|
||||
// Set the offset for the attacks table of the square. We have individual
|
||||
// table sizes for each square with "Fancy Magic Bitboards".
|
||||
m.attacks = s == SQ_A1 ? table : magics[s - 1].attacks + size;
|
||||
|
||||
// Use Carry-Rippler trick to enumerate all subsets of masks[s] and
|
||||
// store the corresponding sliding attack bitboard in reference[].
|
||||
b = size = 0;
|
||||
do {
|
||||
occupancy[size] = b;
|
||||
reference[size] = sliding_attack(deltas, s, b);
|
||||
reference[size] = sliding_attack(directions, s, b);
|
||||
|
||||
if (HasPext)
|
||||
attacks[s][pext(b, masks[s])] = reference[size];
|
||||
m.attacks[pext(b, m.mask)] = reference[size];
|
||||
|
||||
size++;
|
||||
b = (b - masks[s]) & masks[s];
|
||||
b = (b - m.mask) & m.mask;
|
||||
} while (b);
|
||||
|
||||
// Set the offset for the table of the next square. We have individual
|
||||
// table sizes for each square with "Fancy Magic Bitboards".
|
||||
if (s < SQ_H8)
|
||||
attacks[s + 1] = attacks[s] + size;
|
||||
|
||||
if (HasPext)
|
||||
continue;
|
||||
|
||||
@@ -293,29 +297,30 @@ namespace {
|
||||
|
||||
// Find a magic for square 's' picking up an (almost) random number
|
||||
// until we find the one that passes the verification test.
|
||||
do {
|
||||
do
|
||||
magics[s] = rng.sparse_rand<Bitboard>();
|
||||
while (popcount<Max15>((magics[s] * masks[s]) >> 56) < 6);
|
||||
|
||||
std::memset(attacks[s], 0, size * sizeof(Bitboard));
|
||||
for (int i = 0; i < size; )
|
||||
{
|
||||
for (m.magic = 0; popcount((m.magic * m.mask) >> 56) < 6; )
|
||||
m.magic = rng.sparse_rand<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)
|
||||
// effect of verifying the magic. Keep track of the attempt count
|
||||
// and save it in epoch[], little speed-up trick to avoid resetting
|
||||
// m.attacks[] after every failed attempt.
|
||||
for (++cnt, i = 0; i < size; ++i)
|
||||
{
|
||||
Bitboard& attack = attacks[s][index(s, occupancy[i])];
|
||||
unsigned idx = m.index(occupancy[i]);
|
||||
|
||||
if (attack && attack != reference[i])
|
||||
if (epoch[idx] < cnt)
|
||||
{
|
||||
epoch[idx] = cnt;
|
||||
m.attacks[idx] = reference[i];
|
||||
}
|
||||
else if (m.attacks[idx] != reference[i])
|
||||
break;
|
||||
|
||||
assert(reference[i]);
|
||||
|
||||
attack = reference[i];
|
||||
}
|
||||
} while (i < size);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+99
-100
@@ -2,13 +2,13 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
@@ -39,6 +39,7 @@ const std::string pretty(Bitboard b);
|
||||
|
||||
}
|
||||
|
||||
const Bitboard AllSquares = ~Bitboard(0);
|
||||
const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
|
||||
|
||||
const Bitboard FileABB = 0x0101010101010101ULL;
|
||||
@@ -61,29 +62,45 @@ const Bitboard Rank8BB = Rank1BB << (8 * 7);
|
||||
|
||||
extern int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||
|
||||
extern Bitboard RookMasks [SQUARE_NB];
|
||||
extern Bitboard RookMagics [SQUARE_NB];
|
||||
extern Bitboard* RookAttacks[SQUARE_NB];
|
||||
extern unsigned RookShifts [SQUARE_NB];
|
||||
|
||||
extern Bitboard BishopMasks [SQUARE_NB];
|
||||
extern Bitboard BishopMagics [SQUARE_NB];
|
||||
extern Bitboard* BishopAttacks[SQUARE_NB];
|
||||
extern unsigned BishopShifts [SQUARE_NB];
|
||||
|
||||
extern Bitboard SquareBB[SQUARE_NB];
|
||||
extern Bitboard FileBB[FILE_NB];
|
||||
extern Bitboard RankBB[RANK_NB];
|
||||
extern Bitboard AdjacentFilesBB[FILE_NB];
|
||||
extern Bitboard InFrontBB[COLOR_NB][RANK_NB];
|
||||
extern Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
|
||||
extern Bitboard ForwardRanksBB[COLOR_NB][RANK_NB];
|
||||
extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
|
||||
extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
|
||||
extern Bitboard DistanceRingBB[SQUARE_NB][8];
|
||||
extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
|
||||
extern Bitboard ForwardFileBB[COLOR_NB][SQUARE_NB];
|
||||
extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
|
||||
extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
|
||||
extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
||||
extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
|
||||
|
||||
|
||||
/// Magic holds all magic bitboards relevant data for a single square
|
||||
struct Magic {
|
||||
Bitboard mask;
|
||||
Bitboard magic;
|
||||
Bitboard* attacks;
|
||||
unsigned shift;
|
||||
|
||||
// Compute the attack's index using the 'magic bitboards' approach
|
||||
unsigned index(Bitboard occupied) const {
|
||||
|
||||
if (HasPext)
|
||||
return unsigned(pext(occupied, mask));
|
||||
|
||||
if (Is64Bit)
|
||||
return unsigned(((occupied & mask) * magic) >> shift);
|
||||
|
||||
unsigned lo = unsigned(occupied) & unsigned(mask);
|
||||
unsigned hi = unsigned(occupied >> 32) & unsigned(mask >> 32);
|
||||
return (lo * unsigned(magic) ^ hi * unsigned(magic >> 32)) >> shift;
|
||||
}
|
||||
};
|
||||
|
||||
extern Magic RookMagics[SQUARE_NB];
|
||||
extern Magic BishopMagics[SQUARE_NB];
|
||||
|
||||
|
||||
/// Overloads of bitwise operators between a Bitboard and a Square for testing
|
||||
@@ -109,11 +126,10 @@ inline Bitboard& operator^=(Bitboard& b, Square s) {
|
||||
return b ^= SquareBB[s];
|
||||
}
|
||||
|
||||
inline bool more_than_one(Bitboard b) {
|
||||
constexpr bool more_than_one(Bitboard b) {
|
||||
return b & (b - 1);
|
||||
}
|
||||
|
||||
|
||||
/// rank_bb() and file_bb() return a bitboard representing all the squares on
|
||||
/// the given file or rank.
|
||||
|
||||
@@ -134,13 +150,13 @@ inline Bitboard file_bb(Square s) {
|
||||
}
|
||||
|
||||
|
||||
/// shift_bb() moves a bitboard one step along direction Delta. Mainly for pawns
|
||||
/// shift() moves a bitboard one step along direction D. 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
|
||||
template<Direction D>
|
||||
constexpr Bitboard shift(Bitboard b) {
|
||||
return D == NORTH ? b << 8 : D == SOUTH ? b >> 8
|
||||
: D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == SOUTH_EAST ? (b & ~FileHBB) >> 7
|
||||
: D == NORTH_WEST ? (b & ~FileABB) << 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
|
||||
: 0;
|
||||
}
|
||||
|
||||
@@ -163,28 +179,28 @@ inline Bitboard between_bb(Square s1, Square s2) {
|
||||
}
|
||||
|
||||
|
||||
/// in_front_bb() returns a bitboard representing all the squares on all the ranks
|
||||
/// forward_ranks_bb() returns a bitboard representing all the squares on all the ranks
|
||||
/// in front of the given one, from the point of view of the given color. For
|
||||
/// instance, in_front_bb(BLACK, RANK_3) will return the squares on ranks 1 and 2.
|
||||
/// instance, forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
|
||||
|
||||
inline Bitboard in_front_bb(Color c, Rank r) {
|
||||
return InFrontBB[c][r];
|
||||
inline Bitboard forward_ranks_bb(Color c, Square s) {
|
||||
return ForwardRanksBB[c][rank_of(s)];
|
||||
}
|
||||
|
||||
|
||||
/// forward_bb() returns a bitboard representing all the squares along the line
|
||||
/// forward_file_bb() returns a bitboard representing all the squares along the line
|
||||
/// in front of the given one, from the point of view of the given color:
|
||||
/// ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s)
|
||||
/// ForwardFileBB[c][s] = forward_ranks_bb(c, s) & file_bb(s)
|
||||
|
||||
inline Bitboard forward_bb(Color c, Square s) {
|
||||
return ForwardBB[c][s];
|
||||
inline Bitboard forward_file_bb(Color c, Square s) {
|
||||
return ForwardFileBB[c][s];
|
||||
}
|
||||
|
||||
|
||||
/// pawn_attack_span() returns a bitboard representing all the squares that can be
|
||||
/// attacked by a pawn of the given color when it moves along its file, starting
|
||||
/// from the given square:
|
||||
/// PawnAttackSpan[c][s] = in_front_bb(c, s) & adjacent_files_bb(s);
|
||||
/// PawnAttackSpan[c][s] = forward_ranks_bb(c, s) & adjacent_files_bb(file_of(s));
|
||||
|
||||
inline Bitboard pawn_attack_span(Color c, Square s) {
|
||||
return PawnAttackSpan[c][s];
|
||||
@@ -193,21 +209,13 @@ inline Bitboard pawn_attack_span(Color c, Square s) {
|
||||
|
||||
/// passed_pawn_mask() returns a bitboard mask which can be used to test if a
|
||||
/// pawn of the given color and on the given square is a passed pawn:
|
||||
/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_bb(c, s)
|
||||
/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_file_bb(c, s)
|
||||
|
||||
inline Bitboard passed_pawn_mask(Color c, Square s) {
|
||||
return PassedPawnMask[c][s];
|
||||
}
|
||||
|
||||
|
||||
/// squares_of_color() returns a bitboard representing all the squares of the
|
||||
/// same color of the given one.
|
||||
|
||||
inline Bitboard squares_of_color(Square s) {
|
||||
return DarkSquares & s ? DarkSquares : ~DarkSquares;
|
||||
}
|
||||
|
||||
|
||||
/// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
|
||||
/// straight or on a diagonal line.
|
||||
|
||||
@@ -228,93 +236,84 @@ template<> inline int distance<Rank>(Square x, Square y) { return distance(rank_
|
||||
|
||||
|
||||
/// attacks_bb() returns a bitboard representing all the squares attacked by a
|
||||
/// piece of type Pt (bishop or rook) placed on 's'. The helper magic_index()
|
||||
/// looks up the index using the 'magic bitboards' approach.
|
||||
template<PieceType Pt>
|
||||
FORCE_INLINE unsigned magic_index(Square s, Bitboard occupied) {
|
||||
|
||||
Bitboard* const Masks = Pt == ROOK ? RookMasks : BishopMasks;
|
||||
Bitboard* const Magics = Pt == ROOK ? RookMagics : BishopMagics;
|
||||
unsigned* const Shifts = Pt == ROOK ? RookShifts : BishopShifts;
|
||||
|
||||
if (HasPext)
|
||||
return unsigned(pext(occupied, Masks[s]));
|
||||
|
||||
if (Is64Bit)
|
||||
return unsigned(((occupied & Masks[s]) * Magics[s]) >> Shifts[s]);
|
||||
|
||||
unsigned lo = unsigned(occupied) & unsigned(Masks[s]);
|
||||
unsigned hi = unsigned(occupied >> 32) & unsigned(Masks[s] >> 32);
|
||||
return (lo * unsigned(Magics[s]) ^ hi * unsigned(Magics[s] >> 32)) >> Shifts[s];
|
||||
}
|
||||
/// piece of type Pt (bishop or rook) placed on 's'.
|
||||
|
||||
template<PieceType Pt>
|
||||
inline Bitboard attacks_bb(Square s, Bitboard occupied) {
|
||||
return (Pt == ROOK ? RookAttacks : BishopAttacks)[s][magic_index<Pt>(s, occupied)];
|
||||
|
||||
const Magic& m = Pt == ROOK ? RookMagics[s] : BishopMagics[s];
|
||||
return m.attacks[m.index(occupied)];
|
||||
}
|
||||
|
||||
inline Bitboard attacks_bb(Piece pc, Square s, Bitboard occupied) {
|
||||
inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
|
||||
|
||||
switch (type_of(pc))
|
||||
assert(pt != PAWN);
|
||||
|
||||
switch (pt)
|
||||
{
|
||||
case BISHOP: return attacks_bb<BISHOP>(s, occupied);
|
||||
case ROOK : return attacks_bb<ROOK>(s, occupied);
|
||||
case ROOK : return attacks_bb< ROOK>(s, occupied);
|
||||
case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
|
||||
default : return StepAttacksBB[pc][s];
|
||||
default : return PseudoAttacks[pt][s];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// popcount() counts the number of non-zero bits in a bitboard
|
||||
|
||||
inline int popcount(Bitboard b) {
|
||||
|
||||
#ifndef USE_POPCNT
|
||||
|
||||
extern uint8_t PopCnt16[1 << 16];
|
||||
union { Bitboard bb; uint16_t u[4]; } v = { b };
|
||||
return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
|
||||
|
||||
#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
|
||||
|
||||
return (int)_mm_popcnt_u64(b);
|
||||
|
||||
#else // Assumed gcc or compatible compiler
|
||||
|
||||
return __builtin_popcountll(b);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/// lsb() and msb() return the least/most significant bit in a non-zero bitboard
|
||||
|
||||
#ifdef USE_BSFQ
|
||||
#if defined(__GNUC__)
|
||||
|
||||
# if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
|
||||
inline Square lsb(Bitboard b) {
|
||||
assert(b);
|
||||
return Square(__builtin_ctzll(b));
|
||||
}
|
||||
|
||||
FORCE_INLINE Square lsb(Bitboard b) {
|
||||
inline Square msb(Bitboard b) {
|
||||
assert(b);
|
||||
return Square(63 ^ __builtin_clzll(b));
|
||||
}
|
||||
|
||||
#elif defined(_WIN64) && defined(_MSC_VER)
|
||||
|
||||
inline Square lsb(Bitboard b) {
|
||||
assert(b);
|
||||
unsigned long idx;
|
||||
_BitScanForward64(&idx, b);
|
||||
return (Square) idx;
|
||||
}
|
||||
|
||||
FORCE_INLINE Square msb(Bitboard b) {
|
||||
inline Square msb(Bitboard b) {
|
||||
assert(b);
|
||||
unsigned long idx;
|
||||
_BitScanReverse64(&idx, b);
|
||||
return (Square) idx;
|
||||
}
|
||||
|
||||
# elif defined(__arm__)
|
||||
#else
|
||||
|
||||
FORCE_INLINE int lsb32(uint32_t v) {
|
||||
__asm__("rbit %0, %1" : "=r"(v) : "r"(v));
|
||||
return __builtin_clz(v);
|
||||
}
|
||||
|
||||
FORCE_INLINE Square msb(Bitboard b) {
|
||||
return (Square) (63 - __builtin_clzll(b));
|
||||
}
|
||||
|
||||
FORCE_INLINE Square lsb(Bitboard b) {
|
||||
return (Square) (uint32_t(b) ? lsb32(uint32_t(b)) : 32 + lsb32(uint32_t(b >> 32)));
|
||||
}
|
||||
|
||||
# else // Assumed gcc or compatible compiler
|
||||
|
||||
FORCE_INLINE Square lsb(Bitboard b) { // Assembly code by Heinz van Saanen
|
||||
Bitboard idx;
|
||||
__asm__("bsfq %1, %0": "=r"(idx): "rm"(b) );
|
||||
return (Square) idx;
|
||||
}
|
||||
|
||||
FORCE_INLINE Square msb(Bitboard b) {
|
||||
Bitboard idx;
|
||||
__asm__("bsrq %1, %0": "=r"(idx): "rm"(b) );
|
||||
return (Square) idx;
|
||||
}
|
||||
|
||||
# endif
|
||||
|
||||
#else // ifdef(USE_BSFQ)
|
||||
#define NO_BSF // Fallback on software implementation for other cases
|
||||
|
||||
Square lsb(Bitboard b);
|
||||
Square msb(Bitboard b);
|
||||
@@ -324,7 +323,7 @@ Square msb(Bitboard b);
|
||||
|
||||
/// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
|
||||
|
||||
FORCE_INLINE Square pop_lsb(Bitboard* b) {
|
||||
inline Square pop_lsb(Bitboard* b) {
|
||||
const Square s = lsb(*b);
|
||||
*b &= *b - 1;
|
||||
return s;
|
||||
|
||||
-105
@@ -1,105 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef BITCOUNT_H_INCLUDED
|
||||
#define BITCOUNT_H_INCLUDED
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
enum BitCountType {
|
||||
CNT_64,
|
||||
CNT_64_MAX15,
|
||||
CNT_32,
|
||||
CNT_32_MAX15,
|
||||
CNT_HW_POPCNT
|
||||
};
|
||||
|
||||
/// Determine at compile time the best popcount<> specialization according to
|
||||
/// whether the platform is 32 or 64 bit, the maximum number of non-zero
|
||||
/// bits to count and if the hardware popcnt instruction is available.
|
||||
const BitCountType Full = HasPopCnt ? CNT_HW_POPCNT : Is64Bit ? CNT_64 : CNT_32;
|
||||
const BitCountType Max15 = HasPopCnt ? CNT_HW_POPCNT : Is64Bit ? CNT_64_MAX15 : CNT_32_MAX15;
|
||||
|
||||
|
||||
/// popcount() counts the number of non-zero bits in a bitboard
|
||||
template<BitCountType> inline int popcount(Bitboard);
|
||||
|
||||
template<>
|
||||
inline int popcount<CNT_64>(Bitboard b) {
|
||||
b -= (b >> 1) & 0x5555555555555555ULL;
|
||||
b = ((b >> 2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
|
||||
b = ((b >> 4) + b) & 0x0F0F0F0F0F0F0F0FULL;
|
||||
return (b * 0x0101010101010101ULL) >> 56;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int popcount<CNT_64_MAX15>(Bitboard b) {
|
||||
b -= (b >> 1) & 0x5555555555555555ULL;
|
||||
b = ((b >> 2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
|
||||
return (b * 0x1111111111111111ULL) >> 60;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int popcount<CNT_32>(Bitboard b) {
|
||||
unsigned w = unsigned(b >> 32), v = unsigned(b);
|
||||
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
|
||||
w -= (w >> 1) & 0x55555555;
|
||||
v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
|
||||
w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
|
||||
v = ((v >> 4) + v + (w >> 4) + w) & 0x0F0F0F0F;
|
||||
return (v * 0x01010101) >> 24;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int popcount<CNT_32_MAX15>(Bitboard b) {
|
||||
unsigned w = unsigned(b >> 32), v = unsigned(b);
|
||||
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
|
||||
w -= (w >> 1) & 0x55555555;
|
||||
v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
|
||||
w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
|
||||
return ((v + w) * 0x11111111) >> 28;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int popcount<CNT_HW_POPCNT>(Bitboard b) {
|
||||
|
||||
#ifndef USE_POPCNT
|
||||
|
||||
assert(false);
|
||||
return b != 0; // Avoid 'b not used' warning
|
||||
|
||||
#elif defined(_MSC_VER) && defined(__INTEL_COMPILER)
|
||||
|
||||
return _mm_popcnt_u64(b);
|
||||
|
||||
#elif defined(_MSC_VER)
|
||||
|
||||
return (int)__popcnt64(b);
|
||||
|
||||
#else // Assumed gcc or compatible compiler
|
||||
|
||||
return __builtin_popcountll(b);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // #ifndef BITCOUNT_H_INCLUDED
|
||||
+96
-137
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -21,7 +22,6 @@
|
||||
#include <cassert>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "bitcount.h"
|
||||
#include "endgame.h"
|
||||
#include "movegen.h"
|
||||
|
||||
@@ -39,7 +39,7 @@ namespace {
|
||||
70, 50, 30, 20, 20, 30, 50, 70,
|
||||
80, 60, 40, 30, 30, 40, 60, 80,
|
||||
90, 70, 60, 50, 50, 60, 70, 90,
|
||||
100, 90, 80, 70, 70, 80, 90, 100,
|
||||
100, 90, 80, 70, 70, 80, 90, 100
|
||||
};
|
||||
|
||||
// Table used to drive the king towards a corner square of the
|
||||
@@ -59,6 +59,9 @@ namespace {
|
||||
const int PushClose[8] = { 0, 0, 100, 80, 60, 40, 20, 10 };
|
||||
const int PushAway [8] = { 0, 5, 20, 40, 60, 80, 90, 100 };
|
||||
|
||||
// Pawn Rank based scaling factors used in KRPPKRP endgame
|
||||
const int KRPPKRPScaleFactors[RANK_NB] = { 0, 9, 10, 14, 21, 44, 0, 0 };
|
||||
|
||||
#ifndef NDEBUG
|
||||
bool verify_material(const Position& pos, Color c, Value npm, int pawnsCnt) {
|
||||
return pos.non_pawn_material(c) == npm && pos.count<PAWN>(c) == pawnsCnt;
|
||||
@@ -71,7 +74,7 @@ namespace {
|
||||
|
||||
assert(pos.count<PAWN>(strongSide) == 1);
|
||||
|
||||
if (file_of(pos.list<PAWN>(strongSide)[0]) >= FILE_E)
|
||||
if (file_of(pos.square<PAWN>(strongSide)) >= FILE_E)
|
||||
sq = Square(sq ^ 7); // Mirror SQ_H1 -> SQ_A1
|
||||
|
||||
if (strongSide == BLACK)
|
||||
@@ -80,28 +83,6 @@ namespace {
|
||||
return sq;
|
||||
}
|
||||
|
||||
// Get the material key of Position out of the given endgame key code
|
||||
// like "KBPKN". The trick here is to first forge an ad-hoc FEN string
|
||||
// and then let a Position object do the work for us.
|
||||
Key key(const string& code, Color c) {
|
||||
|
||||
assert(code.length() > 0 && code.length() < 8);
|
||||
assert(code[0] == 'K');
|
||||
|
||||
string sides[] = { code.substr(code.find('K', 1)), // Weak
|
||||
code.substr(0, code.find('K', 1)) }; // Strong
|
||||
|
||||
std::transform(sides[c].begin(), sides[c].end(), sides[c].begin(), tolower);
|
||||
|
||||
string fen = sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/8/8/"
|
||||
+ sides[1] + char(8 - sides[1].length() + '0') + " w - - 0 10";
|
||||
|
||||
return Position(fen, false, NULL).material_key();
|
||||
}
|
||||
|
||||
template<typename M>
|
||||
void delete_endgame(const typename M::value_type& p) { delete p.second; }
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
@@ -128,19 +109,6 @@ Endgames::Endgames() {
|
||||
add<KRPPKRP>("KRPPKRP");
|
||||
}
|
||||
|
||||
Endgames::~Endgames() {
|
||||
|
||||
for_each(m1.begin(), m1.end(), delete_endgame<M1>);
|
||||
for_each(m2.begin(), m2.end(), delete_endgame<M2>);
|
||||
}
|
||||
|
||||
template<EndgameType E>
|
||||
void Endgames::add(const string& code) {
|
||||
|
||||
map((Endgame<E>*)0)[key(code, WHITE)] = new Endgame<E>(WHITE);
|
||||
map((Endgame<E>*)0)[key(code, BLACK)] = new Endgame<E>(BLACK);
|
||||
}
|
||||
|
||||
|
||||
/// Mate with KX vs K. This function is used to evaluate positions with
|
||||
/// king and plenty of material vs a lone king. It simply gives the
|
||||
@@ -156,8 +124,8 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
|
||||
if (pos.side_to_move() == weakSide && !MoveList<LEGAL>(pos).size())
|
||||
return VALUE_DRAW;
|
||||
|
||||
Square winnerKSq = pos.king_square(strongSide);
|
||||
Square loserKSq = pos.king_square(weakSide);
|
||||
Square winnerKSq = pos.square<KING>(strongSide);
|
||||
Square loserKSq = pos.square<KING>(weakSide);
|
||||
|
||||
Value result = pos.non_pawn_material(strongSide)
|
||||
+ pos.count<PAWN>(strongSide) * PawnValueEg
|
||||
@@ -167,9 +135,9 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
|
||||
if ( pos.count<QUEEN>(strongSide)
|
||||
|| pos.count<ROOK>(strongSide)
|
||||
||(pos.count<BISHOP>(strongSide) && pos.count<KNIGHT>(strongSide))
|
||||
||(pos.count<BISHOP>(strongSide) > 1 && opposite_colors(pos.list<BISHOP>(strongSide)[0],
|
||||
pos.list<BISHOP>(strongSide)[1])))
|
||||
result += VALUE_KNOWN_WIN;
|
||||
|| ( (pos.pieces(strongSide, BISHOP) & ~DarkSquares)
|
||||
&& (pos.pieces(strongSide, BISHOP) & DarkSquares)))
|
||||
result = std::min(result + VALUE_KNOWN_WIN, VALUE_MATE_IN_MAX_PLY - 1);
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
@@ -183,9 +151,9 @@ Value Endgame<KBNK>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, strongSide, KnightValueMg + BishopValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
|
||||
Square winnerKSq = pos.king_square(strongSide);
|
||||
Square loserKSq = pos.king_square(weakSide);
|
||||
Square bishopSq = pos.list<BISHOP>(strongSide)[0];
|
||||
Square winnerKSq = pos.square<KING>(strongSide);
|
||||
Square loserKSq = pos.square<KING>(weakSide);
|
||||
Square bishopSq = pos.square<BISHOP>(strongSide);
|
||||
|
||||
// kbnk_mate_table() tries to drive toward corners A1 or H8. If we have a
|
||||
// bishop that cannot reach the above squares, we flip the kings in order
|
||||
@@ -212,9 +180,9 @@ Value Endgame<KPK>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
|
||||
// Assume strongSide is white and the pawn is on files A-D
|
||||
Square wksq = normalize(pos, strongSide, pos.king_square(strongSide));
|
||||
Square bksq = normalize(pos, strongSide, pos.king_square(weakSide));
|
||||
Square psq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
|
||||
Square wksq = normalize(pos, strongSide, pos.square<KING>(strongSide));
|
||||
Square bksq = normalize(pos, strongSide, pos.square<KING>(weakSide));
|
||||
Square psq = normalize(pos, strongSide, pos.square<PAWN>(strongSide));
|
||||
|
||||
Color us = strongSide == pos.side_to_move() ? WHITE : BLACK;
|
||||
|
||||
@@ -237,10 +205,10 @@ Value Endgame<KRKP>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||
|
||||
Square wksq = relative_square(strongSide, pos.king_square(strongSide));
|
||||
Square bksq = relative_square(strongSide, pos.king_square(weakSide));
|
||||
Square rsq = relative_square(strongSide, pos.list<ROOK>(strongSide)[0]);
|
||||
Square psq = relative_square(strongSide, pos.list<PAWN>(weakSide)[0]);
|
||||
Square wksq = relative_square(strongSide, pos.square<KING>(strongSide));
|
||||
Square bksq = relative_square(strongSide, pos.square<KING>(weakSide));
|
||||
Square rsq = relative_square(strongSide, pos.square<ROOK>(strongSide));
|
||||
Square psq = relative_square(strongSide, pos.square<PAWN>(weakSide));
|
||||
|
||||
Square queeningSq = make_square(file_of(psq), RANK_1);
|
||||
Value result;
|
||||
@@ -264,8 +232,8 @@ Value Endgame<KRKP>::operator()(const Position& pos) const {
|
||||
result = Value(80) - 8 * distance(wksq, psq);
|
||||
|
||||
else
|
||||
result = Value(200) - 8 * ( distance(wksq, psq + DELTA_S)
|
||||
- distance(bksq, psq + DELTA_S)
|
||||
result = Value(200) - 8 * ( distance(wksq, psq + SOUTH)
|
||||
- distance(bksq, psq + SOUTH)
|
||||
- distance(psq, queeningSq));
|
||||
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
@@ -280,7 +248,7 @@ Value Endgame<KRKB>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||
|
||||
Value result = Value(PushToEdges[pos.king_square(weakSide)]);
|
||||
Value result = Value(PushToEdges[pos.square<KING>(weakSide)]);
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
|
||||
@@ -293,8 +261,8 @@ Value Endgame<KRKN>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, KnightValueMg, 0));
|
||||
|
||||
Square bksq = pos.king_square(weakSide);
|
||||
Square bnsq = pos.list<KNIGHT>(weakSide)[0];
|
||||
Square bksq = pos.square<KING>(weakSide);
|
||||
Square bnsq = pos.square<KNIGHT>(weakSide);
|
||||
Value result = Value(PushToEdges[bksq] + PushAway[distance(bksq, bnsq)]);
|
||||
return strongSide == pos.side_to_move() ? result : -result;
|
||||
}
|
||||
@@ -310,9 +278,9 @@ Value Endgame<KQKP>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, strongSide, QueenValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||
|
||||
Square winnerKSq = pos.king_square(strongSide);
|
||||
Square loserKSq = pos.king_square(weakSide);
|
||||
Square pawnSq = pos.list<PAWN>(weakSide)[0];
|
||||
Square winnerKSq = pos.square<KING>(strongSide);
|
||||
Square loserKSq = pos.square<KING>(weakSide);
|
||||
Square pawnSq = pos.square<PAWN>(weakSide);
|
||||
|
||||
Value result = Value(PushClose[distance(winnerKSq, loserKSq)]);
|
||||
|
||||
@@ -335,8 +303,8 @@ Value Endgame<KQKR>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, strongSide, QueenValueMg, 0));
|
||||
assert(verify_material(pos, weakSide, RookValueMg, 0));
|
||||
|
||||
Square winnerKSq = pos.king_square(strongSide);
|
||||
Square loserKSq = pos.king_square(weakSide);
|
||||
Square winnerKSq = pos.square<KING>(strongSide);
|
||||
Square loserKSq = pos.square<KING>(weakSide);
|
||||
|
||||
Value result = QueenValueEg
|
||||
- RookValueEg
|
||||
@@ -365,15 +333,15 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
|
||||
// be detected even when the weaker side has some pawns.
|
||||
|
||||
Bitboard pawns = pos.pieces(strongSide, PAWN);
|
||||
File pawnFile = file_of(pos.list<PAWN>(strongSide)[0]);
|
||||
File pawnsFile = file_of(lsb(pawns));
|
||||
|
||||
// All pawns are on a single rook file ?
|
||||
if ( (pawnFile == FILE_A || pawnFile == FILE_H)
|
||||
&& !(pawns & ~file_bb(pawnFile)))
|
||||
// All pawns are on a single rook file?
|
||||
if ( (pawnsFile == FILE_A || pawnsFile == FILE_H)
|
||||
&& !(pawns & ~file_bb(pawnsFile)))
|
||||
{
|
||||
Square bishopSq = pos.list<BISHOP>(strongSide)[0];
|
||||
Square queeningSq = relative_square(strongSide, make_square(pawnFile, RANK_8));
|
||||
Square kingSq = pos.king_square(weakSide);
|
||||
Square bishopSq = pos.square<BISHOP>(strongSide);
|
||||
Square queeningSq = relative_square(strongSide, make_square(pawnsFile, RANK_8));
|
||||
Square kingSq = pos.square<KING>(weakSide);
|
||||
|
||||
if ( opposite_colors(queeningSq, bishopSq)
|
||||
&& distance(queeningSq, kingSq) <= 1)
|
||||
@@ -381,17 +349,17 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
|
||||
}
|
||||
|
||||
// If all the pawns are on the same B or G file, then it's potentially a draw
|
||||
if ( (pawnFile == FILE_B || pawnFile == FILE_G)
|
||||
&& !(pos.pieces(PAWN) & ~file_bb(pawnFile))
|
||||
if ( (pawnsFile == FILE_B || pawnsFile == FILE_G)
|
||||
&& !(pos.pieces(PAWN) & ~file_bb(pawnsFile))
|
||||
&& pos.non_pawn_material(weakSide) == 0
|
||||
&& pos.count<PAWN>(weakSide) >= 1)
|
||||
{
|
||||
// Get weakSide pawn that is closest to the home rank
|
||||
Square weakPawnSq = backmost_sq(weakSide, pos.pieces(weakSide, PAWN));
|
||||
|
||||
Square strongKingSq = pos.king_square(strongSide);
|
||||
Square weakKingSq = pos.king_square(weakSide);
|
||||
Square bishopSq = pos.list<BISHOP>(strongSide)[0];
|
||||
Square strongKingSq = pos.square<KING>(strongSide);
|
||||
Square weakKingSq = pos.square<KING>(weakSide);
|
||||
Square bishopSq = pos.square<BISHOP>(strongSide);
|
||||
|
||||
// There's potential for a draw if our pawn is blocked on the 7th rank,
|
||||
// the bishop cannot attack it or they only have one pawn left
|
||||
@@ -428,11 +396,11 @@ ScaleFactor Endgame<KQKRPs>::operator()(const Position& pos) const {
|
||||
assert(pos.count<ROOK>(weakSide) == 1);
|
||||
assert(pos.count<PAWN>(weakSide) >= 1);
|
||||
|
||||
Square kingSq = pos.king_square(weakSide);
|
||||
Square rsq = pos.list<ROOK>(weakSide)[0];
|
||||
Square kingSq = pos.square<KING>(weakSide);
|
||||
Square rsq = pos.square<ROOK>(weakSide);
|
||||
|
||||
if ( relative_rank(weakSide, kingSq) <= RANK_2
|
||||
&& relative_rank(weakSide, pos.king_square(strongSide)) >= RANK_4
|
||||
&& relative_rank(weakSide, pos.square<KING>(strongSide)) >= RANK_4
|
||||
&& relative_rank(weakSide, rsq) == RANK_3
|
||||
&& ( pos.pieces(weakSide, PAWN)
|
||||
& pos.attacks_from<KING>(kingSq)
|
||||
@@ -456,11 +424,11 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, weakSide, RookValueMg, 0));
|
||||
|
||||
// Assume strongSide is white and the pawn is on files A-D
|
||||
Square wksq = normalize(pos, strongSide, pos.king_square(strongSide));
|
||||
Square bksq = normalize(pos, strongSide, pos.king_square(weakSide));
|
||||
Square wrsq = normalize(pos, strongSide, pos.list<ROOK>(strongSide)[0]);
|
||||
Square wpsq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
|
||||
Square brsq = normalize(pos, strongSide, pos.list<ROOK>(weakSide)[0]);
|
||||
Square wksq = normalize(pos, strongSide, pos.square<KING>(strongSide));
|
||||
Square bksq = normalize(pos, strongSide, pos.square<KING>(weakSide));
|
||||
Square wrsq = normalize(pos, strongSide, pos.square<ROOK>(strongSide));
|
||||
Square wpsq = normalize(pos, strongSide, pos.square<PAWN>(strongSide));
|
||||
Square brsq = normalize(pos, strongSide, pos.square<ROOK>(weakSide));
|
||||
|
||||
File f = file_of(wpsq);
|
||||
Rank r = rank_of(wpsq);
|
||||
@@ -480,7 +448,7 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
|
||||
if ( r == RANK_6
|
||||
&& distance(bksq, queeningSq) <= 1
|
||||
&& rank_of(wksq) + tempo <= RANK_6
|
||||
&& (rank_of(brsq) == RANK_1 || (!tempo && distance(file_of(brsq), f) >= 3)))
|
||||
&& (rank_of(brsq) == RANK_1 || (!tempo && distance<File>(brsq, wpsq) >= 3)))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
if ( r >= RANK_6
|
||||
@@ -501,7 +469,7 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
|
||||
// If the defending king blocks the pawn and the attacking king is too far
|
||||
// away, it's a draw.
|
||||
if ( r <= RANK_5
|
||||
&& bksq == wpsq + DELTA_N
|
||||
&& bksq == wpsq + NORTH
|
||||
&& distance(wksq, wpsq) - tempo >= 2
|
||||
&& distance(wksq, brsq) - tempo >= 2)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
@@ -522,10 +490,10 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
|
||||
&& file_of(wrsq) == f
|
||||
&& wrsq < wpsq
|
||||
&& (distance(wksq, queeningSq) < distance(bksq, queeningSq) - 2 + tempo)
|
||||
&& (distance(wksq, wpsq + DELTA_N) < distance(bksq, wpsq + DELTA_N) - 2 + tempo)
|
||||
&& (distance(wksq, wpsq + NORTH) < distance(bksq, wpsq + NORTH) - 2 + tempo)
|
||||
&& ( distance(bksq, wrsq) + tempo >= 3
|
||||
|| ( distance(wksq, queeningSq) < distance(bksq, wrsq) + tempo
|
||||
&& (distance(wksq, wpsq + DELTA_N) < distance(bksq, wrsq) + tempo))))
|
||||
&& (distance(wksq, wpsq + NORTH) < distance(bksq, wrsq) + tempo))))
|
||||
return ScaleFactor( SCALE_FACTOR_MAX
|
||||
- 8 * distance(wpsq, queeningSq)
|
||||
- 2 * distance(wksq, queeningSq));
|
||||
@@ -552,11 +520,11 @@ ScaleFactor Endgame<KRPKB>::operator()(const Position& pos) const {
|
||||
// Test for a rook pawn
|
||||
if (pos.pieces(PAWN) & (FileABB | FileHBB))
|
||||
{
|
||||
Square ksq = pos.king_square(weakSide);
|
||||
Square bsq = pos.list<BISHOP>(weakSide)[0];
|
||||
Square psq = pos.list<PAWN>(strongSide)[0];
|
||||
Square ksq = pos.square<KING>(weakSide);
|
||||
Square bsq = pos.square<BISHOP>(weakSide);
|
||||
Square psq = pos.square<PAWN>(strongSide);
|
||||
Rank rk = relative_rank(strongSide, psq);
|
||||
Square push = pawn_push(strongSide);
|
||||
Direction push = pawn_push(strongSide);
|
||||
|
||||
// If the pawn is on the 5th rank and the pawn (currently) is on
|
||||
// the same color square as the bishop then there is a chance of
|
||||
@@ -567,7 +535,7 @@ ScaleFactor Endgame<KRPKB>::operator()(const Position& pos) const {
|
||||
{
|
||||
int d = distance(psq + 3 * push, ksq);
|
||||
|
||||
if (d <= 2 && !(d == 0 && ksq == pos.king_square(strongSide) + 2 * push))
|
||||
if (d <= 2 && !(d == 0 && ksq == pos.square<KING>(strongSide) + 2 * push))
|
||||
return ScaleFactor(24);
|
||||
else
|
||||
return ScaleFactor(48);
|
||||
@@ -595,9 +563,9 @@ ScaleFactor Endgame<KRPPKRP>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, strongSide, RookValueMg, 2));
|
||||
assert(verify_material(pos, weakSide, RookValueMg, 1));
|
||||
|
||||
Square wpsq1 = pos.list<PAWN>(strongSide)[0];
|
||||
Square wpsq2 = pos.list<PAWN>(strongSide)[1];
|
||||
Square bksq = pos.king_square(weakSide);
|
||||
Square wpsq1 = pos.squares<PAWN>(strongSide)[0];
|
||||
Square wpsq2 = pos.squares<PAWN>(strongSide)[1];
|
||||
Square bksq = pos.square<KING>(weakSide);
|
||||
|
||||
// Does the stronger side have a passed pawn?
|
||||
if (pos.pawn_passed(strongSide, wpsq1) || pos.pawn_passed(strongSide, wpsq2))
|
||||
@@ -609,14 +577,8 @@ ScaleFactor Endgame<KRPPKRP>::operator()(const Position& pos) const {
|
||||
&& distance<File>(bksq, wpsq2) <= 1
|
||||
&& relative_rank(strongSide, bksq) > r)
|
||||
{
|
||||
switch (r) {
|
||||
case RANK_2: return ScaleFactor(10);
|
||||
case RANK_3: return ScaleFactor(10);
|
||||
case RANK_4: return ScaleFactor(15);
|
||||
case RANK_5: return ScaleFactor(20);
|
||||
case RANK_6: return ScaleFactor(40);
|
||||
default: assert(false);
|
||||
}
|
||||
assert(r > RANK_1 && r < RANK_7);
|
||||
return ScaleFactor(KRPPKRPScaleFactors[r]);
|
||||
}
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
@@ -631,15 +593,14 @@ ScaleFactor Endgame<KPsK>::operator()(const Position& pos) const {
|
||||
assert(pos.count<PAWN>(strongSide) >= 2);
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
|
||||
Square ksq = pos.king_square(weakSide);
|
||||
Square ksq = pos.square<KING>(weakSide);
|
||||
Bitboard pawns = pos.pieces(strongSide, PAWN);
|
||||
Square psq = pos.list<PAWN>(strongSide)[0];
|
||||
|
||||
// If all pawns are ahead of the king, on a single rook file and
|
||||
// the king is within one file of the pawns, it's a draw.
|
||||
if ( !(pawns & ~in_front_bb(weakSide, rank_of(ksq)))
|
||||
if ( !(pawns & ~forward_ranks_bb(weakSide, ksq))
|
||||
&& !((pawns & ~FileABB) && (pawns & ~FileHBB))
|
||||
&& distance<File>(ksq, psq) <= 1)
|
||||
&& distance<File>(ksq, lsb(pawns)) <= 1)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
@@ -656,10 +617,10 @@ ScaleFactor Endgame<KBPKB>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, strongSide, BishopValueMg, 1));
|
||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||
|
||||
Square pawnSq = pos.list<PAWN>(strongSide)[0];
|
||||
Square strongBishopSq = pos.list<BISHOP>(strongSide)[0];
|
||||
Square weakBishopSq = pos.list<BISHOP>(weakSide)[0];
|
||||
Square weakKingSq = pos.king_square(weakSide);
|
||||
Square pawnSq = pos.square<PAWN>(strongSide);
|
||||
Square strongBishopSq = pos.square<BISHOP>(strongSide);
|
||||
Square weakBishopSq = pos.square<BISHOP>(weakSide);
|
||||
Square weakKingSq = pos.square<KING>(weakSide);
|
||||
|
||||
// Case 1: Defending king blocks the pawn, and cannot be driven away
|
||||
if ( file_of(weakKingSq) == file_of(pawnSq)
|
||||
@@ -683,17 +644,15 @@ ScaleFactor Endgame<KBPKB>::operator()(const Position& pos) const {
|
||||
|
||||
if (relative_rank(strongSide, pawnSq) <= RANK_5)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
else
|
||||
{
|
||||
Bitboard path = forward_bb(strongSide, pawnSq);
|
||||
|
||||
if (path & pos.pieces(weakSide, KING))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
Bitboard path = forward_file_bb(strongSide, pawnSq);
|
||||
|
||||
if ( (pos.attacks_from<BISHOP>(weakBishopSq) & path)
|
||||
&& distance(weakBishopSq, pawnSq) >= 3)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
}
|
||||
if (path & pos.pieces(weakSide, KING))
|
||||
return SCALE_FACTOR_DRAW;
|
||||
|
||||
if ( (pos.attacks_from<BISHOP>(weakBishopSq) & path)
|
||||
&& distance(weakBishopSq, pawnSq) >= 3)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
}
|
||||
return SCALE_FACTOR_NONE;
|
||||
}
|
||||
@@ -706,15 +665,15 @@ ScaleFactor Endgame<KBPPKB>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, strongSide, BishopValueMg, 2));
|
||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||
|
||||
Square wbsq = pos.list<BISHOP>(strongSide)[0];
|
||||
Square bbsq = pos.list<BISHOP>(weakSide)[0];
|
||||
Square wbsq = pos.square<BISHOP>(strongSide);
|
||||
Square bbsq = pos.square<BISHOP>(weakSide);
|
||||
|
||||
if (!opposite_colors(wbsq, bbsq))
|
||||
return SCALE_FACTOR_NONE;
|
||||
|
||||
Square ksq = pos.king_square(weakSide);
|
||||
Square psq1 = pos.list<PAWN>(strongSide)[0];
|
||||
Square psq2 = pos.list<PAWN>(strongSide)[1];
|
||||
Square ksq = pos.square<KING>(weakSide);
|
||||
Square psq1 = pos.squares<PAWN>(strongSide)[0];
|
||||
Square psq2 = pos.squares<PAWN>(strongSide)[1];
|
||||
Rank r1 = rank_of(psq1);
|
||||
Rank r2 = rank_of(psq2);
|
||||
Square blockSq1, blockSq2;
|
||||
@@ -777,9 +736,9 @@ ScaleFactor Endgame<KBPKN>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, strongSide, BishopValueMg, 1));
|
||||
assert(verify_material(pos, weakSide, KnightValueMg, 0));
|
||||
|
||||
Square pawnSq = pos.list<PAWN>(strongSide)[0];
|
||||
Square strongBishopSq = pos.list<BISHOP>(strongSide)[0];
|
||||
Square weakKingSq = pos.king_square(weakSide);
|
||||
Square pawnSq = pos.square<PAWN>(strongSide);
|
||||
Square strongBishopSq = pos.square<BISHOP>(strongSide);
|
||||
Square weakKingSq = pos.square<KING>(weakSide);
|
||||
|
||||
if ( file_of(weakKingSq) == file_of(pawnSq)
|
||||
&& relative_rank(strongSide, pawnSq) < relative_rank(strongSide, weakKingSq)
|
||||
@@ -800,8 +759,8 @@ ScaleFactor Endgame<KNPK>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||
|
||||
// Assume strongSide is white and the pawn is on files A-D
|
||||
Square pawnSq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
|
||||
Square weakKingSq = normalize(pos, strongSide, pos.king_square(weakSide));
|
||||
Square pawnSq = normalize(pos, strongSide, pos.square<PAWN>(strongSide));
|
||||
Square weakKingSq = normalize(pos, strongSide, pos.square<KING>(weakSide));
|
||||
|
||||
if (pawnSq == SQ_A7 && distance(SQ_A8, weakKingSq) <= 1)
|
||||
return SCALE_FACTOR_DRAW;
|
||||
@@ -815,13 +774,13 @@ ScaleFactor Endgame<KNPK>::operator()(const Position& pos) const {
|
||||
template<>
|
||||
ScaleFactor Endgame<KNPKB>::operator()(const Position& pos) const {
|
||||
|
||||
Square pawnSq = pos.list<PAWN>(strongSide)[0];
|
||||
Square bishopSq = pos.list<BISHOP>(weakSide)[0];
|
||||
Square weakKingSq = pos.king_square(weakSide);
|
||||
Square pawnSq = pos.square<PAWN>(strongSide);
|
||||
Square bishopSq = pos.square<BISHOP>(weakSide);
|
||||
Square weakKingSq = pos.square<KING>(weakSide);
|
||||
|
||||
// King needs to get close to promoting pawn to prevent knight from blocking.
|
||||
// Rules for this are very tricky, so just approximate.
|
||||
if (forward_bb(strongSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
|
||||
if (forward_file_bb(strongSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
|
||||
return ScaleFactor(distance(weakKingSq, pawnSq));
|
||||
|
||||
return SCALE_FACTOR_NONE;
|
||||
@@ -840,9 +799,9 @@ ScaleFactor Endgame<KPKP>::operator()(const Position& pos) const {
|
||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||
|
||||
// Assume strongSide is white and the pawn is on files A-D
|
||||
Square wksq = normalize(pos, strongSide, pos.king_square(strongSide));
|
||||
Square bksq = normalize(pos, strongSide, pos.king_square(weakSide));
|
||||
Square psq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
|
||||
Square wksq = normalize(pos, strongSide, pos.square<KING>(strongSide));
|
||||
Square bksq = normalize(pos, strongSide, pos.square<KING>(weakSide));
|
||||
Square psq = normalize(pos, strongSide, pos.square<PAWN>(strongSide));
|
||||
|
||||
Color us = strongSide == pos.side_to_move() ? WHITE : BLACK;
|
||||
|
||||
|
||||
+35
-32
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -21,18 +22,20 @@
|
||||
#define ENDGAME_H_INCLUDED
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "position.h"
|
||||
#include "types.h"
|
||||
|
||||
|
||||
/// EndgameType lists all supported endgames
|
||||
/// EndgameCode lists all supported endgame functions by corresponding codes
|
||||
|
||||
enum EndgameType {
|
||||
|
||||
// Evaluation functions
|
||||
enum EndgameCode {
|
||||
|
||||
EVALUATION_FUNCTIONS,
|
||||
KNNK, // KNN vs K
|
||||
KXK, // Generic "mate lone king" eval
|
||||
KBNK, // KBN vs K
|
||||
@@ -43,10 +46,7 @@ enum EndgameType {
|
||||
KQKP, // KQ vs KP
|
||||
KQKR, // KQ vs KR
|
||||
|
||||
|
||||
// Scaling functions
|
||||
SCALING_FUNCTIONS,
|
||||
|
||||
KBPsK, // KB and pawns vs K
|
||||
KQKRPs, // KQ vs KR and pawns
|
||||
KRPKR, // KRP vs KR
|
||||
@@ -63,33 +63,29 @@ enum EndgameType {
|
||||
|
||||
|
||||
/// Endgame functions can be of two types depending on whether they return a
|
||||
/// Value or a ScaleFactor. Type eg_fun<int>::type returns either ScaleFactor
|
||||
/// or Value depending on whether the template parameter is 0 or 1.
|
||||
|
||||
template<int> struct eg_fun { typedef Value type; };
|
||||
template<> struct eg_fun<1> { typedef ScaleFactor type; };
|
||||
/// Value or a ScaleFactor.
|
||||
template<EndgameCode E> using
|
||||
eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type;
|
||||
|
||||
|
||||
/// Base and derived templates for endgame evaluation and scaling functions
|
||||
/// Base and derived functors for endgame evaluation and scaling functions
|
||||
|
||||
template<typename T>
|
||||
struct EndgameBase {
|
||||
|
||||
virtual ~EndgameBase() {}
|
||||
virtual Color strong_side() const = 0;
|
||||
explicit EndgameBase(Color c) : strongSide(c), weakSide(~c) {}
|
||||
virtual ~EndgameBase() = default;
|
||||
virtual T operator()(const Position&) const = 0;
|
||||
|
||||
const Color strongSide, weakSide;
|
||||
};
|
||||
|
||||
|
||||
template<EndgameType E, typename T = typename eg_fun<(E > SCALING_FUNCTIONS)>::type>
|
||||
template<EndgameCode E, typename T = eg_type<E>>
|
||||
struct Endgame : public EndgameBase<T> {
|
||||
|
||||
explicit Endgame(Color c) : strongSide(c), weakSide(~c) {}
|
||||
Color strong_side() const { return strongSide; }
|
||||
T operator()(const Position&) const;
|
||||
|
||||
private:
|
||||
Color strongSide, weakSide;
|
||||
explicit Endgame(Color c) : EndgameBase<T>(c) {}
|
||||
T operator()(const Position&) const override;
|
||||
};
|
||||
|
||||
|
||||
@@ -99,23 +95,30 @@ private:
|
||||
|
||||
class Endgames {
|
||||
|
||||
typedef std::map<Key, EndgameBase<eg_fun<0>::type>*> M1;
|
||||
typedef std::map<Key, EndgameBase<eg_fun<1>::type>*> M2;
|
||||
template<typename T> using Ptr = std::unique_ptr<EndgameBase<T>>;
|
||||
template<typename T> using Map = std::map<Key, Ptr<T>>;
|
||||
|
||||
M1 m1;
|
||||
M2 m2;
|
||||
template<typename T>
|
||||
Map<T>& map() {
|
||||
return std::get<std::is_same<T, ScaleFactor>::value>(maps);
|
||||
}
|
||||
|
||||
M1& map(M1::mapped_type) { return m1; }
|
||||
M2& map(M2::mapped_type) { return m2; }
|
||||
template<EndgameCode E, typename T = eg_type<E>, typename P = Ptr<T>>
|
||||
void add(const std::string& code) {
|
||||
|
||||
template<EndgameType E> void add(const std::string& code);
|
||||
StateInfo st;
|
||||
map<T>()[Position().set(code, WHITE, &st).material_key()] = P(new Endgame<E>(WHITE));
|
||||
map<T>()[Position().set(code, BLACK, &st).material_key()] = P(new Endgame<E>(BLACK));
|
||||
}
|
||||
|
||||
std::pair<Map<Value>, Map<ScaleFactor>> maps;
|
||||
|
||||
public:
|
||||
Endgames();
|
||||
~Endgames();
|
||||
|
||||
template<typename T> T probe(Key key, T& eg) {
|
||||
return eg = map(eg).count(key) ? map(eg)[key] : NULL;
|
||||
template<typename T>
|
||||
EndgameBase<T>* probe(Key key) {
|
||||
return map<T>().count(key) ? map<T>()[key].get() : nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+633
-586
File diff suppressed because it is too large
Load Diff
+5
-3
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -28,12 +29,13 @@ class Position;
|
||||
|
||||
namespace Eval {
|
||||
|
||||
const Value Tempo = Value(17); // Must be visible to search
|
||||
const Value Tempo = Value(20); // Must be visible to search
|
||||
|
||||
extern Score Contempt;
|
||||
|
||||
void init();
|
||||
Value evaluate(const Position& pos);
|
||||
std::string trace(const Position& pos);
|
||||
|
||||
Value evaluate(const Position& pos);
|
||||
}
|
||||
|
||||
#endif // #ifndef EVALUATE_H_INCLUDED
|
||||
|
||||
+10
-4
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -20,7 +21,6 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "evaluate.h"
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
@@ -28,22 +28,28 @@
|
||||
#include "uci.h"
|
||||
#include "syzygy/tbprobe.h"
|
||||
|
||||
namespace PSQT {
|
||||
void init();
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
std::cout << engine_info() << std::endl;
|
||||
|
||||
UCI::init(Options);
|
||||
PSQT::init();
|
||||
Bitboards::init();
|
||||
Position::init();
|
||||
Bitbases::init();
|
||||
Search::init();
|
||||
Eval::init();
|
||||
Pawns::init();
|
||||
Threads.init();
|
||||
Tablebases::init(Options["SyzygyPath"]);
|
||||
TT.resize(Options["Hash"]);
|
||||
Threads.set(Options["Threads"]);
|
||||
Search::clear(); // After threads are up
|
||||
|
||||
UCI::loop(argc, argv);
|
||||
|
||||
Threads.exit();
|
||||
Threads.set(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
+53
-63
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -30,29 +31,26 @@ namespace {
|
||||
|
||||
// Polynomial material imbalance parameters
|
||||
|
||||
// pair pawn knight bishop rook queen
|
||||
const int Linear[6] = { 1852, -162, -1122, -183, 249, -154 };
|
||||
|
||||
const int QuadraticOurs[][PIECE_TYPE_NB] = {
|
||||
// OUR PIECES
|
||||
// pair pawn knight bishop rook queen
|
||||
{ 0 }, // Bishop pair
|
||||
{ 39, 2 }, // Pawn
|
||||
{ 35, 271, -4 }, // Knight OUR PIECES
|
||||
{ 0, 105, 4, 0 }, // Bishop
|
||||
{ -27, -2, 46, 100, -141 }, // Rook
|
||||
{-177, 25, 129, 142, -137, 0 } // Queen
|
||||
{1667 }, // Bishop pair
|
||||
{ 40, 0 }, // Pawn
|
||||
{ 32, 255, -3 }, // Knight OUR PIECES
|
||||
{ 0, 104, 4, 0 }, // Bishop
|
||||
{ -26, -2, 47, 105, -149 }, // Rook
|
||||
{-189, 24, 117, 133, -134, -10 } // Queen
|
||||
};
|
||||
|
||||
const int QuadraticTheirs[][PIECE_TYPE_NB] = {
|
||||
// THEIR PIECES
|
||||
// pair pawn knight bishop rook queen
|
||||
{ 0 }, // Bishop pair
|
||||
{ 37, 0 }, // Pawn
|
||||
{ 10, 62, 0 }, // Knight OUR PIECES
|
||||
{ 57, 64, 39, 0 }, // Bishop
|
||||
{ 50, 40, 23, -22, 0 }, // Rook
|
||||
{ 98, 105, -39, 141, 274, 0 } // Queen
|
||||
{ 36, 0 }, // Pawn
|
||||
{ 9, 63, 0 }, // Knight OUR PIECES
|
||||
{ 59, 65, 42, 0 }, // Bishop
|
||||
{ 46, 39, 24, -24, 0 }, // Rook
|
||||
{ 97, 100, -42, 137, 268, 0 } // Queen
|
||||
};
|
||||
|
||||
// Endgame evaluation and scaling functions are accessed directly and not through
|
||||
@@ -64,31 +62,28 @@ namespace {
|
||||
Endgame<KPsK> ScaleKPsK[] = { Endgame<KPsK>(WHITE), Endgame<KPsK>(BLACK) };
|
||||
Endgame<KPKP> ScaleKPKP[] = { Endgame<KPKP>(WHITE), Endgame<KPKP>(BLACK) };
|
||||
|
||||
// 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 !more_than_one(pos.pieces(Them))
|
||||
&& pos.non_pawn_material(Us) >= RookValueMg;
|
||||
// Helper used to detect a given material distribution
|
||||
bool is_KXK(const Position& pos, Color us) {
|
||||
return !more_than_one(pos.pieces(~us))
|
||||
&& pos.non_pawn_material(us) >= RookValueMg;
|
||||
}
|
||||
|
||||
template<Color Us> bool is_KBPsKs(const Position& pos) {
|
||||
return pos.non_pawn_material(Us) == BishopValueMg
|
||||
&& pos.count<BISHOP>(Us) == 1
|
||||
&& pos.count<PAWN >(Us) >= 1;
|
||||
bool is_KBPsKs(const Position& pos, Color us) {
|
||||
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.count<PAWN>(Us)
|
||||
&& pos.non_pawn_material(Us) == QueenValueMg
|
||||
&& pos.count<QUEEN>(Us) == 1
|
||||
&& pos.count<ROOK>(Them) == 1
|
||||
&& pos.count<PAWN>(Them) >= 1;
|
||||
bool is_KQKRPs(const Position& pos, Color us) {
|
||||
return !pos.count<PAWN>(us)
|
||||
&& pos.non_pawn_material(us) == QueenValueMg
|
||||
&& pos.count<QUEEN>(us) == 1
|
||||
&& pos.count<ROOK>(~us) == 1
|
||||
&& pos.count<PAWN>(~us) >= 1;
|
||||
}
|
||||
|
||||
/// imbalance() calculates the imbalance by comparing the piece count of each
|
||||
/// piece type for both colors.
|
||||
|
||||
template<Color Us>
|
||||
int imbalance(const int pieceCount[][PIECE_TYPE_NB]) {
|
||||
|
||||
@@ -96,13 +91,13 @@ namespace {
|
||||
|
||||
int bonus = 0;
|
||||
|
||||
// Second-degree polynomial material imbalance by Tord Romstad
|
||||
// Second-degree polynomial material imbalance, by Tord Romstad
|
||||
for (int pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1)
|
||||
{
|
||||
if (!pieceCount[Us][pt1])
|
||||
continue;
|
||||
|
||||
int v = Linear[pt1];
|
||||
int v = 0;
|
||||
|
||||
for (int pt2 = NO_PIECE_TYPE; pt2 <= pt1; ++pt2)
|
||||
v += QuadraticOurs[pt1][pt2] * pieceCount[Us][pt2]
|
||||
@@ -134,53 +129,48 @@ Entry* probe(const Position& pos) {
|
||||
std::memset(e, 0, sizeof(Entry));
|
||||
e->key = key;
|
||||
e->factor[WHITE] = e->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL;
|
||||
e->gamePhase = pos.game_phase();
|
||||
|
||||
Value npm_w = pos.non_pawn_material(WHITE);
|
||||
Value npm_b = pos.non_pawn_material(BLACK);
|
||||
Value npm = std::max(EndgameLimit, std::min(npm_w + npm_b, MidgameLimit));
|
||||
|
||||
// Map total non-pawn material into [PHASE_ENDGAME, PHASE_MIDGAME]
|
||||
e->gamePhase = Phase(((npm - EndgameLimit) * PHASE_MIDGAME) / (MidgameLimit - EndgameLimit));
|
||||
|
||||
// Let's look if we have a specialized evaluation function for this particular
|
||||
// material configuration. Firstly we look for a fixed configuration one, then
|
||||
// for a generic one if the previous search failed.
|
||||
if (pos.this_thread()->endgames.probe(key, e->evaluationFunction))
|
||||
if ((e->evaluationFunction = pos.this_thread()->endgames.probe<Value>(key)) != nullptr)
|
||||
return e;
|
||||
|
||||
if (is_KXK<WHITE>(pos))
|
||||
{
|
||||
e->evaluationFunction = &EvaluateKXK[WHITE];
|
||||
return e;
|
||||
}
|
||||
|
||||
if (is_KXK<BLACK>(pos))
|
||||
{
|
||||
e->evaluationFunction = &EvaluateKXK[BLACK];
|
||||
return e;
|
||||
}
|
||||
for (Color c = WHITE; c <= BLACK; ++c)
|
||||
if (is_KXK(pos, c))
|
||||
{
|
||||
e->evaluationFunction = &EvaluateKXK[c];
|
||||
return e;
|
||||
}
|
||||
|
||||
// OK, we didn't find any special evaluation function for the current material
|
||||
// configuration. Is there a suitable specialized scaling function?
|
||||
EndgameBase<ScaleFactor>* sf;
|
||||
|
||||
if (pos.this_thread()->endgames.probe(key, sf))
|
||||
if ((sf = pos.this_thread()->endgames.probe<ScaleFactor>(key)) != nullptr)
|
||||
{
|
||||
e->scalingFunction[sf->strong_side()] = sf; // Only strong color assigned
|
||||
e->scalingFunction[sf->strongSide] = sf; // Only strong color assigned
|
||||
return e;
|
||||
}
|
||||
|
||||
// We didn't find any specialized scaling function, so fall back on generic
|
||||
// ones that refer to more than one material distribution. Note that in this
|
||||
// case we don't return after setting the function.
|
||||
if (is_KBPsKs<WHITE>(pos))
|
||||
e->scalingFunction[WHITE] = &ScaleKBPsK[WHITE];
|
||||
for (Color c = WHITE; c <= BLACK; ++c)
|
||||
{
|
||||
if (is_KBPsKs(pos, c))
|
||||
e->scalingFunction[c] = &ScaleKBPsK[c];
|
||||
|
||||
if (is_KBPsKs<BLACK>(pos))
|
||||
e->scalingFunction[BLACK] = &ScaleKBPsK[BLACK];
|
||||
|
||||
if (is_KQKRPs<WHITE>(pos))
|
||||
e->scalingFunction[WHITE] = &ScaleKQKRPs[WHITE];
|
||||
|
||||
else if (is_KQKRPs<BLACK>(pos))
|
||||
e->scalingFunction[BLACK] = &ScaleKQKRPs[BLACK];
|
||||
|
||||
Value npm_w = pos.non_pawn_material(WHITE);
|
||||
Value npm_b = pos.non_pawn_material(BLACK);
|
||||
else if (is_KQKRPs(pos, c))
|
||||
e->scalingFunction[c] = &ScaleKQKRPs[c];
|
||||
}
|
||||
|
||||
if (npm_w + npm_b == VALUE_ZERO && pos.pieces(PAWN)) // Only pawns on the board
|
||||
{
|
||||
@@ -210,11 +200,11 @@ Entry* probe(const Position& pos) {
|
||||
// drawish scale factor for cases such as KRKBP and KmmKm (except for KBBKN).
|
||||
if (!pos.count<PAWN>(WHITE) && npm_w - npm_b <= BishopValueMg)
|
||||
e->factor[WHITE] = uint8_t(npm_w < RookValueMg ? SCALE_FACTOR_DRAW :
|
||||
npm_b <= BishopValueMg ? 4 : 12);
|
||||
npm_b <= BishopValueMg ? 4 : 14);
|
||||
|
||||
if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg)
|
||||
e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW :
|
||||
npm_w <= BishopValueMg ? 4 : 12);
|
||||
npm_w <= BishopValueMg ? 4 : 14);
|
||||
|
||||
if (pos.count<PAWN>(WHITE) == 1 && npm_w - npm_b <= BishopValueMg)
|
||||
e->factor[WHITE] = (uint8_t) SCALE_FACTOR_ONEPAWN;
|
||||
|
||||
+7
-6
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -40,7 +41,7 @@ struct Entry {
|
||||
|
||||
Score imbalance() const { return make_score(value, value); }
|
||||
Phase game_phase() const { return gamePhase; }
|
||||
bool specialized_eval_exists() const { return evaluationFunction != NULL; }
|
||||
bool specialized_eval_exists() const { return evaluationFunction != nullptr; }
|
||||
Value evaluate(const Position& pos) const { return (*evaluationFunction)(pos); }
|
||||
|
||||
// scale_factor takes a position and a color as input and returns a scale factor
|
||||
@@ -49,17 +50,17 @@ struct Entry {
|
||||
// the position. For instance, in KBP vs K endgames, the scaling function looks
|
||||
// for rook pawns and wrong-colored bishops.
|
||||
ScaleFactor scale_factor(const Position& pos, Color c) const {
|
||||
return !scalingFunction[c]
|
||||
|| (*scalingFunction[c])(pos) == SCALE_FACTOR_NONE ? ScaleFactor(factor[c])
|
||||
: (*scalingFunction[c])(pos);
|
||||
ScaleFactor sf = scalingFunction[c] ? (*scalingFunction[c])(pos)
|
||||
: SCALE_FACTOR_NONE;
|
||||
return sf != SCALE_FACTOR_NONE ? sf : ScaleFactor(factor[c]);
|
||||
}
|
||||
|
||||
Key key;
|
||||
int16_t value;
|
||||
uint8_t factor[COLOR_NB];
|
||||
EndgameBase<Value>* evaluationFunction;
|
||||
EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB]; // Could be one for each
|
||||
// side (e.g. KPKP, KBPsKs)
|
||||
int16_t value;
|
||||
uint8_t factor[COLOR_NB];
|
||||
Phase gamePhase;
|
||||
};
|
||||
|
||||
|
||||
+156
-47
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,10 +18,29 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifdef _WIN32
|
||||
#if _WIN32_WINNT < 0x0601
|
||||
#undef _WIN32_WINNT
|
||||
#define _WIN32_WINNT 0x0601 // Force to include needed API prototypes
|
||||
#endif
|
||||
#include <windows.h>
|
||||
// The needed Windows API for processor groups could be missed from old Windows
|
||||
// versions, so instead of calling them directly (forcing the linker to resolve
|
||||
// the calls at compile time), try to load them at runtime. To do this we need
|
||||
// first to define the corresponding function pointers.
|
||||
extern "C" {
|
||||
typedef bool(*fun1_t)(LOGICAL_PROCESSOR_RELATIONSHIP,
|
||||
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD);
|
||||
typedef bool(*fun2_t)(USHORT, PGROUP_AFFINITY);
|
||||
typedef bool(*fun3_t)(HANDLE, CONST GROUP_AFFINITY*, PGROUP_AFFINITY);
|
||||
}
|
||||
#endif
|
||||
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include "misc.h"
|
||||
#include "thread.h"
|
||||
@@ -31,60 +51,56 @@ namespace {
|
||||
|
||||
/// Version number. If Version is left empty, then compile date in the format
|
||||
/// DD-MM-YY and show in engine_info.
|
||||
const string Version = "6";
|
||||
|
||||
/// Debug counters
|
||||
int64_t hits[2], means[2];
|
||||
const string Version = "9";
|
||||
|
||||
/// Our fancy logging facility. The trick here is to replace cin.rdbuf() and
|
||||
/// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We
|
||||
/// can toggle the logging of std::cout and std:cin at runtime whilst preserving
|
||||
/// usual i/o functionality, all without changing a single line of code!
|
||||
/// usual I/O functionality, all without changing a single line of code!
|
||||
/// Idea from http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81
|
||||
|
||||
struct Tie: public streambuf { // MSVC requires splitted streambuf for cin and cout
|
||||
struct Tie: public streambuf { // MSVC requires split streambuf for cin and cout
|
||||
|
||||
Tie(streambuf* b, ofstream* f) : buf(b), file(f) {}
|
||||
Tie(streambuf* b, streambuf* l) : buf(b), logBuf(l) {}
|
||||
|
||||
int sync() { return file->rdbuf()->pubsync(), buf->pubsync(); }
|
||||
int overflow(int c) { return log(buf->sputc((char)c), "<< "); }
|
||||
int underflow() { return buf->sgetc(); }
|
||||
int uflow() { return log(buf->sbumpc(), ">> "); }
|
||||
int sync() override { return logBuf->pubsync(), buf->pubsync(); }
|
||||
int overflow(int c) override { return log(buf->sputc((char)c), "<< "); }
|
||||
int underflow() override { return buf->sgetc(); }
|
||||
int uflow() override { return log(buf->sbumpc(), ">> "); }
|
||||
|
||||
streambuf* buf;
|
||||
ofstream* file;
|
||||
streambuf *buf, *logBuf;
|
||||
|
||||
int log(int c, const char* prefix) {
|
||||
|
||||
static int last = '\n';
|
||||
static int last = '\n'; // Single log file
|
||||
|
||||
if (last == '\n')
|
||||
file->rdbuf()->sputn(prefix, 3);
|
||||
logBuf->sputn(prefix, 3);
|
||||
|
||||
return last = file->rdbuf()->sputc((char)c);
|
||||
return last = logBuf->sputc((char)c);
|
||||
}
|
||||
};
|
||||
|
||||
class Logger {
|
||||
|
||||
Logger() : in(cin.rdbuf(), &file), out(cout.rdbuf(), &file) {}
|
||||
~Logger() { start(false); }
|
||||
Logger() : in(cin.rdbuf(), file.rdbuf()), out(cout.rdbuf(), file.rdbuf()) {}
|
||||
~Logger() { start(""); }
|
||||
|
||||
ofstream file;
|
||||
Tie in, out;
|
||||
|
||||
public:
|
||||
static void start(bool b) {
|
||||
static void start(const std::string& fname) {
|
||||
|
||||
static Logger l;
|
||||
|
||||
if (b && !l.file.is_open())
|
||||
if (!fname.empty() && !l.file.is_open())
|
||||
{
|
||||
l.file.open("io_log.txt", ifstream::out | ifstream::app);
|
||||
l.file.open(fname, ifstream::out);
|
||||
cin.rdbuf(&l.in);
|
||||
cout.rdbuf(&l.out);
|
||||
}
|
||||
else if (!b && l.file.is_open())
|
||||
else if (fname.empty() && l.file.is_open())
|
||||
{
|
||||
cout.rdbuf(l.out.buf);
|
||||
cin.rdbuf(l.in.buf);
|
||||
@@ -117,16 +133,17 @@ const string engine_info(bool to_uci) {
|
||||
ss << (Is64Bit ? " 64" : "")
|
||||
<< (HasPext ? " BMI2" : (HasPopCnt ? " POPCNT" : ""))
|
||||
<< (to_uci ? "\nid author ": " by ")
|
||||
<< "Tord Romstad, Marco Costalba and Joona Kiiski";
|
||||
<< "T. Romstad, M. Costalba, J. Kiiski, G. Linscott";
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
||||
/// Debug functions used mainly to collect run-time statistics
|
||||
static int64_t hits[2], means[2];
|
||||
|
||||
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_hit_on(bool c, bool b) { if (c) dbg_hit_on(b); }
|
||||
void dbg_mean_of(int v) { ++means[0]; means[1] += v; }
|
||||
|
||||
void dbg_print() {
|
||||
@@ -159,26 +176,7 @@ std::ostream& operator<<(std::ostream& os, SyncCout sc) {
|
||||
|
||||
|
||||
/// Trampoline helper to avoid moving Logger to misc.h
|
||||
void start_logger(bool b) { Logger::start(b); }
|
||||
|
||||
|
||||
/// timed_wait() waits for msec milliseconds. It is mainly a helper to wrap
|
||||
/// the conversion from milliseconds to struct timespec, as used by pthreads.
|
||||
|
||||
void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) {
|
||||
|
||||
#ifdef _WIN32
|
||||
int tm = msec;
|
||||
#else
|
||||
timespec ts, *tm = &ts;
|
||||
uint64_t ms = Time::now() + msec;
|
||||
|
||||
ts.tv_sec = ms / 1000;
|
||||
ts.tv_nsec = (ms % 1000) * 1000000LL;
|
||||
#endif
|
||||
|
||||
cond_timedwait(sleepCond, sleepLock, tm);
|
||||
}
|
||||
void start_logger(const std::string& fname) { Logger::start(fname); }
|
||||
|
||||
|
||||
/// prefetch() preloads the given address in L1/L2 cache. This is a non-blocking
|
||||
@@ -186,11 +184,11 @@ void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) {
|
||||
/// which can be quite slow.
|
||||
#ifdef NO_PREFETCH
|
||||
|
||||
void prefetch(char*) {}
|
||||
void prefetch(void*) {}
|
||||
|
||||
#else
|
||||
|
||||
void prefetch(char* addr) {
|
||||
void prefetch(void* addr) {
|
||||
|
||||
# if defined(__INTEL_COMPILER)
|
||||
// This hack prevents prefetches from being optimized away by
|
||||
@@ -199,10 +197,121 @@ void prefetch(char* addr) {
|
||||
# endif
|
||||
|
||||
# if defined(__INTEL_COMPILER) || defined(_MSC_VER)
|
||||
_mm_prefetch(addr, _MM_HINT_T0);
|
||||
_mm_prefetch((char*)addr, _MM_HINT_T0);
|
||||
# else
|
||||
__builtin_prefetch(addr);
|
||||
# endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void prefetch2(void* addr) {
|
||||
|
||||
prefetch(addr);
|
||||
prefetch((uint8_t*)addr + 64);
|
||||
}
|
||||
|
||||
namespace WinProcGroup {
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
void bindThisThread(size_t) {}
|
||||
|
||||
#else
|
||||
|
||||
/// get_group() retrieves logical processor information using Windows specific
|
||||
/// API and returns the best group id for the thread with index idx. Original
|
||||
/// code from Texel by Peter Österlund.
|
||||
|
||||
int get_group(size_t idx) {
|
||||
|
||||
int threads = 0;
|
||||
int nodes = 0;
|
||||
int cores = 0;
|
||||
DWORD returnLength = 0;
|
||||
DWORD byteOffset = 0;
|
||||
|
||||
// Early exit if the needed API is not available at runtime
|
||||
HMODULE k32 = GetModuleHandle("Kernel32.dll");
|
||||
auto fun1 = (fun1_t)GetProcAddress(k32, "GetLogicalProcessorInformationEx");
|
||||
if (!fun1)
|
||||
return -1;
|
||||
|
||||
// First call to get returnLength. We expect it to fail due to null buffer
|
||||
if (fun1(RelationAll, nullptr, &returnLength))
|
||||
return -1;
|
||||
|
||||
// Once we know returnLength, allocate the buffer
|
||||
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *buffer, *ptr;
|
||||
ptr = buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)malloc(returnLength);
|
||||
|
||||
// Second call, now we expect to succeed
|
||||
if (!fun1(RelationAll, buffer, &returnLength))
|
||||
{
|
||||
free(buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (ptr->Size > 0 && byteOffset + ptr->Size <= returnLength)
|
||||
{
|
||||
if (ptr->Relationship == RelationNumaNode)
|
||||
nodes++;
|
||||
|
||||
else if (ptr->Relationship == RelationProcessorCore)
|
||||
{
|
||||
cores++;
|
||||
threads += (ptr->Processor.Flags == LTP_PC_SMT) ? 2 : 1;
|
||||
}
|
||||
|
||||
byteOffset += ptr->Size;
|
||||
ptr = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)(((char*)ptr) + ptr->Size);
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
|
||||
std::vector<int> groups;
|
||||
|
||||
// Run as many threads as possible on the same node until core limit is
|
||||
// reached, then move on filling the next node.
|
||||
for (int n = 0; n < nodes; n++)
|
||||
for (int i = 0; i < cores / nodes; i++)
|
||||
groups.push_back(n);
|
||||
|
||||
// In case a core has more than one logical processor (we assume 2) and we
|
||||
// have still threads to allocate, then spread them evenly across available
|
||||
// nodes.
|
||||
for (int t = 0; t < threads - cores; t++)
|
||||
groups.push_back(t % nodes);
|
||||
|
||||
// If we still have more threads than the total number of logical processors
|
||||
// then return -1 and let the OS to decide what to do.
|
||||
return idx < groups.size() ? groups[idx] : -1;
|
||||
}
|
||||
|
||||
|
||||
/// bindThisThread() set the group affinity of the current thread
|
||||
|
||||
void bindThisThread(size_t idx) {
|
||||
|
||||
// Use only local variables to be thread-safe
|
||||
int group = get_group(idx);
|
||||
|
||||
if (group == -1)
|
||||
return;
|
||||
|
||||
// Early exit if the needed API are not available at runtime
|
||||
HMODULE k32 = GetModuleHandle("Kernel32.dll");
|
||||
auto fun2 = (fun2_t)GetProcAddress(k32, "GetNumaNodeProcessorMaskEx");
|
||||
auto fun3 = (fun3_t)GetProcAddress(k32, "SetThreadGroupAffinity");
|
||||
|
||||
if (!fun2 || !fun3)
|
||||
return;
|
||||
|
||||
GROUP_AFFINITY affinity;
|
||||
if (fun2(group, &affinity))
|
||||
fun3(GetCurrentThread(), &affinity, nullptr);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace WinProcGroup
|
||||
|
||||
+22
-10
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -21,6 +22,7 @@
|
||||
#define MISC_H_INCLUDED
|
||||
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -28,29 +30,28 @@
|
||||
#include "types.h"
|
||||
|
||||
const std::string engine_info(bool to_uci = false);
|
||||
void timed_wait(WaitCondition&, Lock&, int);
|
||||
void prefetch(char* addr);
|
||||
void start_logger(bool b);
|
||||
void prefetch(void* addr);
|
||||
void prefetch2(void* addr);
|
||||
void start_logger(const std::string& fname);
|
||||
|
||||
void dbg_hit_on(bool b);
|
||||
void dbg_hit_on_c(bool c, bool b);
|
||||
void dbg_hit_on(bool c, bool b);
|
||||
void dbg_mean_of(int v);
|
||||
void dbg_print();
|
||||
|
||||
typedef std::chrono::milliseconds::rep TimePoint; // A value in milliseconds
|
||||
|
||||
namespace Time {
|
||||
typedef int64_t point;
|
||||
inline point now() { return system_time_to_msec(); }
|
||||
inline TimePoint now() {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>
|
||||
(std::chrono::steady_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
|
||||
template<class Entry, int Size>
|
||||
struct HashTable {
|
||||
HashTable() : table(Size, Entry()) {}
|
||||
Entry* operator[](Key key) { return &table[(uint32_t)key & (Size - 1)]; }
|
||||
|
||||
private:
|
||||
std::vector<Entry> table;
|
||||
std::vector<Entry> table = std::vector<Entry>(Size);
|
||||
};
|
||||
|
||||
|
||||
@@ -97,4 +98,15 @@ public:
|
||||
{ return T(rand64() & rand64() & rand64()); }
|
||||
};
|
||||
|
||||
|
||||
/// Under Windows it is not possible for a process to run on more than one
|
||||
/// logical processor group. This usually means to be limited to use max 64
|
||||
/// cores. To overcome this, some special platform specific API should be
|
||||
/// called to set group affinity for each thread. Original code from Texel by
|
||||
/// Peter Österlund.
|
||||
|
||||
namespace WinProcGroup {
|
||||
void bindThisThread(size_t idx);
|
||||
}
|
||||
|
||||
#endif // #ifndef MISC_H_INCLUDED
|
||||
|
||||
+81
-79
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -25,7 +26,7 @@
|
||||
namespace {
|
||||
|
||||
template<CastlingRight Cr, bool Checks, bool Chess960>
|
||||
ExtMove* generate_castling(const Position& pos, ExtMove* moveList, Color us, const CheckInfo* ci) {
|
||||
ExtMove* generate_castling(const Position& pos, ExtMove* moveList, Color us) {
|
||||
|
||||
static const bool KingSide = (Cr == WHITE_OO || Cr == BLACK_OO);
|
||||
|
||||
@@ -34,15 +35,15 @@ namespace {
|
||||
|
||||
// After castling, the rook and king final positions are the same in Chess960
|
||||
// as they would be in standard chess.
|
||||
Square kfrom = pos.king_square(us);
|
||||
Square kfrom = pos.square<KING>(us);
|
||||
Square rfrom = pos.castling_rook_square(Cr);
|
||||
Square kto = relative_square(us, KingSide ? SQ_G1 : SQ_C1);
|
||||
Bitboard enemies = pos.pieces(~us);
|
||||
|
||||
assert(!pos.checkers());
|
||||
|
||||
const Square K = Chess960 ? kto > kfrom ? DELTA_W : DELTA_E
|
||||
: KingSide ? DELTA_W : DELTA_E;
|
||||
const Direction K = Chess960 ? kto > kfrom ? WEST : EAST
|
||||
: KingSide ? WEST : EAST;
|
||||
|
||||
for (Square s = kto; s != kfrom; s += K)
|
||||
if (pos.attackers_to(s) & enemies)
|
||||
@@ -56,52 +57,50 @@ namespace {
|
||||
|
||||
Move m = make<CASTLING>(kfrom, rfrom);
|
||||
|
||||
if (Checks && !pos.gives_check(m, *ci))
|
||||
if (Checks && !pos.gives_check(m))
|
||||
return moveList;
|
||||
|
||||
(moveList++)->move = m;
|
||||
|
||||
*moveList++ = m;
|
||||
return moveList;
|
||||
}
|
||||
|
||||
|
||||
template<GenType Type, Square Delta>
|
||||
inline ExtMove* make_promotions(ExtMove* moveList, Square to, const CheckInfo* ci) {
|
||||
template<GenType Type, Direction D>
|
||||
ExtMove* make_promotions(ExtMove* moveList, Square to, Square ksq) {
|
||||
|
||||
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
||||
(moveList++)->move = make<PROMOTION>(to - Delta, to, QUEEN);
|
||||
*moveList++ = make<PROMOTION>(to - D, to, QUEEN);
|
||||
|
||||
if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
|
||||
{
|
||||
(moveList++)->move = make<PROMOTION>(to - Delta, to, ROOK);
|
||||
(moveList++)->move = make<PROMOTION>(to - Delta, to, BISHOP);
|
||||
(moveList++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
|
||||
*moveList++ = make<PROMOTION>(to - D, to, ROOK);
|
||||
*moveList++ = make<PROMOTION>(to - D, to, BISHOP);
|
||||
*moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
|
||||
}
|
||||
|
||||
// Knight promotion is the only promotion that can give a direct check
|
||||
// that's not already included in the queen promotion.
|
||||
if (Type == QUIET_CHECKS && (StepAttacksBB[W_KNIGHT][to] & ci->ksq))
|
||||
(moveList++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
|
||||
if (Type == QUIET_CHECKS && (PseudoAttacks[KNIGHT][to] & ksq))
|
||||
*moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
|
||||
else
|
||||
(void)ci; // Silence a warning under MSVC
|
||||
(void)ksq; // Silence a warning under MSVC
|
||||
|
||||
return moveList;
|
||||
}
|
||||
|
||||
|
||||
template<Color Us, GenType Type>
|
||||
ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
|
||||
|
||||
// Compute our parametrized parameters at compile time, named according to
|
||||
// the point of view of white side.
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
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 Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
|
||||
const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
|
||||
const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
|
||||
const Direction Up = (Us == WHITE ? NORTH : SOUTH);
|
||||
const Direction Right = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
|
||||
const Direction Left = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
|
||||
|
||||
Bitboard emptySquares;
|
||||
|
||||
@@ -116,8 +115,8 @@ namespace {
|
||||
{
|
||||
emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
|
||||
|
||||
Bitboard b1 = shift_bb<Up>(pawnsNotOn7) & emptySquares;
|
||||
Bitboard b2 = shift_bb<Up>(b1 & TRank3BB) & emptySquares;
|
||||
Bitboard b1 = shift<Up>(pawnsNotOn7) & emptySquares;
|
||||
Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
|
||||
|
||||
if (Type == EVASIONS) // Consider only blocking squares
|
||||
{
|
||||
@@ -127,17 +126,20 @@ namespace {
|
||||
|
||||
if (Type == QUIET_CHECKS)
|
||||
{
|
||||
b1 &= pos.attacks_from<PAWN>(ci->ksq, Them);
|
||||
b2 &= pos.attacks_from<PAWN>(ci->ksq, Them);
|
||||
Square ksq = pos.square<KING>(Them);
|
||||
|
||||
b1 &= pos.attacks_from<PAWN>(ksq, Them);
|
||||
b2 &= pos.attacks_from<PAWN>(ksq, Them);
|
||||
|
||||
// Add pawn pushes which give discovered check. This is possible only
|
||||
// if the pawn is not on the same file as the enemy king, because we
|
||||
// don't generate captures. Note that a possible discovery check
|
||||
// promotion has been already generated amongst the captures.
|
||||
if (pawnsNotOn7 & ci->dcCandidates)
|
||||
Bitboard dcCandidates = pos.discovered_check_candidates();
|
||||
if (pawnsNotOn7 & dcCandidates)
|
||||
{
|
||||
Bitboard dc1 = shift_bb<Up>(pawnsNotOn7 & ci->dcCandidates) & emptySquares & ~file_bb(ci->ksq);
|
||||
Bitboard dc2 = shift_bb<Up>(dc1 & TRank3BB) & emptySquares;
|
||||
Bitboard dc1 = shift<Up>(pawnsNotOn7 & dcCandidates) & emptySquares & ~file_bb(ksq);
|
||||
Bitboard dc2 = shift<Up>(dc1 & TRank3BB) & emptySquares;
|
||||
|
||||
b1 |= dc1;
|
||||
b2 |= dc2;
|
||||
@@ -147,13 +149,13 @@ namespace {
|
||||
while (b1)
|
||||
{
|
||||
Square to = pop_lsb(&b1);
|
||||
(moveList++)->move = make_move(to - Up, to);
|
||||
*moveList++ = make_move(to - Up, to);
|
||||
}
|
||||
|
||||
while (b2)
|
||||
{
|
||||
Square to = pop_lsb(&b2);
|
||||
(moveList++)->move = make_move(to - Up - Up, to);
|
||||
*moveList++ = make_move(to - Up - Up, to);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,36 +168,38 @@ namespace {
|
||||
if (Type == EVASIONS)
|
||||
emptySquares &= target;
|
||||
|
||||
Bitboard b1 = shift_bb<Right>(pawnsOn7) & enemies;
|
||||
Bitboard b2 = shift_bb<Left >(pawnsOn7) & enemies;
|
||||
Bitboard b3 = shift_bb<Up >(pawnsOn7) & emptySquares;
|
||||
Bitboard b1 = shift<Right>(pawnsOn7) & enemies;
|
||||
Bitboard b2 = shift<Left >(pawnsOn7) & enemies;
|
||||
Bitboard b3 = shift<Up >(pawnsOn7) & emptySquares;
|
||||
|
||||
Square ksq = pos.square<KING>(Them);
|
||||
|
||||
while (b1)
|
||||
moveList = make_promotions<Type, Right>(moveList, pop_lsb(&b1), ci);
|
||||
moveList = make_promotions<Type, Right>(moveList, pop_lsb(&b1), ksq);
|
||||
|
||||
while (b2)
|
||||
moveList = make_promotions<Type, Left >(moveList, pop_lsb(&b2), ci);
|
||||
moveList = make_promotions<Type, Left >(moveList, pop_lsb(&b2), ksq);
|
||||
|
||||
while (b3)
|
||||
moveList = make_promotions<Type, Up >(moveList, pop_lsb(&b3), ci);
|
||||
moveList = make_promotions<Type, Up >(moveList, pop_lsb(&b3), ksq);
|
||||
}
|
||||
|
||||
// Standard and en-passant captures
|
||||
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
||||
{
|
||||
Bitboard b1 = shift_bb<Right>(pawnsNotOn7) & enemies;
|
||||
Bitboard b2 = shift_bb<Left >(pawnsNotOn7) & enemies;
|
||||
Bitboard b1 = shift<Right>(pawnsNotOn7) & enemies;
|
||||
Bitboard b2 = shift<Left >(pawnsNotOn7) & enemies;
|
||||
|
||||
while (b1)
|
||||
{
|
||||
Square to = pop_lsb(&b1);
|
||||
(moveList++)->move = make_move(to - Right, to);
|
||||
*moveList++ = make_move(to - Right, to);
|
||||
}
|
||||
|
||||
while (b2)
|
||||
{
|
||||
Square to = pop_lsb(&b2);
|
||||
(moveList++)->move = make_move(to - Left, to);
|
||||
*moveList++ = make_move(to - Left, to);
|
||||
}
|
||||
|
||||
if (pos.ep_square() != SQ_NONE)
|
||||
@@ -213,7 +217,7 @@ namespace {
|
||||
assert(b1);
|
||||
|
||||
while (b1)
|
||||
(moveList++)->move = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
|
||||
*moveList++ = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,70 +225,69 @@ namespace {
|
||||
}
|
||||
|
||||
|
||||
template<PieceType Pt, bool Checks> FORCE_INLINE
|
||||
template<PieceType Pt, bool Checks>
|
||||
ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Color us,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
Bitboard target) {
|
||||
|
||||
assert(Pt != KING && Pt != PAWN);
|
||||
|
||||
const Square* pl = pos.list<Pt>(us);
|
||||
const Square* pl = pos.squares<Pt>(us);
|
||||
|
||||
for (Square from = *pl; from != SQ_NONE; from = *++pl)
|
||||
{
|
||||
if (Checks)
|
||||
{
|
||||
if ( (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
|
||||
&& !(PseudoAttacks[Pt][from] & target & ci->checkSq[Pt]))
|
||||
&& !(PseudoAttacks[Pt][from] & target & pos.check_squares(Pt)))
|
||||
continue;
|
||||
|
||||
if (ci->dcCandidates && (ci->dcCandidates & from))
|
||||
if (pos.discovered_check_candidates() & from)
|
||||
continue;
|
||||
}
|
||||
|
||||
Bitboard b = pos.attacks_from<Pt>(from) & target;
|
||||
|
||||
if (Checks)
|
||||
b &= ci->checkSq[Pt];
|
||||
b &= pos.check_squares(Pt);
|
||||
|
||||
while (b)
|
||||
(moveList++)->move = make_move(from, pop_lsb(&b));
|
||||
*moveList++ = make_move(from, pop_lsb(&b));
|
||||
}
|
||||
|
||||
return moveList;
|
||||
}
|
||||
|
||||
|
||||
template<Color Us, GenType Type> FORCE_INLINE
|
||||
ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target,
|
||||
const CheckInfo* ci = NULL) {
|
||||
template<Color Us, GenType Type>
|
||||
ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target) {
|
||||
|
||||
const bool Checks = Type == QUIET_CHECKS;
|
||||
|
||||
moveList = generate_pawn_moves<Us, Type>(pos, moveList, target, ci);
|
||||
moveList = generate_moves<KNIGHT, Checks>(pos, moveList, Us, target, ci);
|
||||
moveList = generate_moves<BISHOP, Checks>(pos, moveList, Us, target, ci);
|
||||
moveList = generate_moves< ROOK, Checks>(pos, moveList, Us, target, ci);
|
||||
moveList = generate_moves< QUEEN, Checks>(pos, moveList, Us, target, ci);
|
||||
moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
|
||||
moveList = generate_moves<KNIGHT, Checks>(pos, moveList, Us, target);
|
||||
moveList = generate_moves<BISHOP, Checks>(pos, moveList, Us, target);
|
||||
moveList = generate_moves< ROOK, Checks>(pos, moveList, Us, target);
|
||||
moveList = generate_moves< QUEEN, Checks>(pos, moveList, Us, target);
|
||||
|
||||
if (Type != QUIET_CHECKS && Type != EVASIONS)
|
||||
{
|
||||
Square ksq = pos.king_square(Us);
|
||||
Square ksq = pos.square<KING>(Us);
|
||||
Bitboard b = pos.attacks_from<KING>(ksq) & target;
|
||||
while (b)
|
||||
(moveList++)->move = make_move(ksq, pop_lsb(&b));
|
||||
*moveList++ = make_move(ksq, pop_lsb(&b));
|
||||
}
|
||||
|
||||
if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(Us))
|
||||
{
|
||||
if (pos.is_chess960())
|
||||
{
|
||||
moveList = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, true>(pos, moveList, Us, ci);
|
||||
moveList = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, true>(pos, moveList, Us, ci);
|
||||
moveList = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, true>(pos, moveList, Us);
|
||||
moveList = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, true>(pos, moveList, Us);
|
||||
}
|
||||
else
|
||||
{
|
||||
moveList = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, false>(pos, moveList, Us, ci);
|
||||
moveList = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, false>(pos, moveList, Us, ci);
|
||||
moveList = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, false>(pos, moveList, Us);
|
||||
moveList = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, false>(pos, moveList, Us);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -333,8 +336,7 @@ ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
|
||||
assert(!pos.checkers());
|
||||
|
||||
Color us = pos.side_to_move();
|
||||
CheckInfo ci(pos);
|
||||
Bitboard dc = ci.dcCandidates;
|
||||
Bitboard dc = pos.discovered_check_candidates();
|
||||
|
||||
while (dc)
|
||||
{
|
||||
@@ -344,17 +346,17 @@ ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
|
||||
if (pt == PAWN)
|
||||
continue; // Will be generated together with direct checks
|
||||
|
||||
Bitboard b = pos.attacks_from(Piece(pt), from) & ~pos.pieces();
|
||||
Bitboard b = pos.attacks_from(pt, from) & ~pos.pieces();
|
||||
|
||||
if (pt == KING)
|
||||
b &= ~PseudoAttacks[QUEEN][ci.ksq];
|
||||
b &= ~PseudoAttacks[QUEEN][pos.square<KING>(~us)];
|
||||
|
||||
while (b)
|
||||
(moveList++)->move = make_move(from, pop_lsb(&b));
|
||||
*moveList++ = make_move(from, pop_lsb(&b));
|
||||
}
|
||||
|
||||
return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList, ~pos.pieces(), &ci)
|
||||
: generate_all<BLACK, QUIET_CHECKS>(pos, moveList, ~pos.pieces(), &ci);
|
||||
return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList, ~pos.pieces())
|
||||
: generate_all<BLACK, QUIET_CHECKS>(pos, moveList, ~pos.pieces());
|
||||
}
|
||||
|
||||
|
||||
@@ -366,7 +368,7 @@ ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
|
||||
assert(pos.checkers());
|
||||
|
||||
Color us = pos.side_to_move();
|
||||
Square ksq = pos.king_square(us);
|
||||
Square ksq = pos.square<KING>(us);
|
||||
Bitboard sliderAttacks = 0;
|
||||
Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
|
||||
|
||||
@@ -382,7 +384,7 @@ ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
|
||||
// Generate evasions for king, capture and non capture moves
|
||||
Bitboard b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
|
||||
while (b)
|
||||
(moveList++)->move = make_move(ksq, pop_lsb(&b));
|
||||
*moveList++ = make_move(ksq, pop_lsb(&b));
|
||||
|
||||
if (more_than_one(pos.checkers()))
|
||||
return moveList; // Double check, only a king move can save the day
|
||||
@@ -402,15 +404,15 @@ template<>
|
||||
ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
|
||||
|
||||
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
|
||||
Square ksq = pos.king_square(pos.side_to_move());
|
||||
Square ksq = pos.square<KING>(pos.side_to_move());
|
||||
ExtMove* cur = moveList;
|
||||
|
||||
moveList = pos.checkers() ? generate<EVASIONS >(pos, moveList)
|
||||
: generate<NON_EVASIONS>(pos, moveList);
|
||||
while (cur != moveList)
|
||||
if ( (pinned || from_sq(cur->move) == ksq || type_of(cur->move) == ENPASSANT)
|
||||
&& !pos.legal(cur->move, pinned))
|
||||
cur->move = (--moveList)->move;
|
||||
if ( (pinned || from_sq(*cur) == ksq || type_of(*cur) == ENPASSANT)
|
||||
&& !pos.legal(*cur))
|
||||
*cur = (--moveList)->move;
|
||||
else
|
||||
++cur;
|
||||
|
||||
|
||||
+17
-9
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -20,6 +21,8 @@
|
||||
#ifndef MOVEGEN_H_INCLUDED
|
||||
#define MOVEGEN_H_INCLUDED
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
class Position;
|
||||
@@ -35,7 +38,14 @@ enum GenType {
|
||||
|
||||
struct ExtMove {
|
||||
Move move;
|
||||
Value value;
|
||||
int value;
|
||||
|
||||
operator Move() const { return move; }
|
||||
void operator=(Move m) { move = m; }
|
||||
|
||||
// Inhibit unwanted implicit conversions to Move
|
||||
// with an ambiguity that yields to a compile error.
|
||||
operator float() const = delete;
|
||||
};
|
||||
|
||||
inline bool operator<(const ExtMove& f, const ExtMove& s) {
|
||||
@@ -50,18 +60,16 @@ ExtMove* generate(const Position& pos, ExtMove* moveList);
|
||||
template<GenType T>
|
||||
struct MoveList {
|
||||
|
||||
explicit MoveList(const Position& pos) : cur(moveList), last(generate<T>(pos, moveList)) { last->move = MOVE_NONE; }
|
||||
void operator++() { ++cur; }
|
||||
Move operator*() const { return cur->move; }
|
||||
explicit MoveList(const Position& pos) : last(generate<T>(pos, moveList)) {}
|
||||
const ExtMove* begin() const { return moveList; }
|
||||
const ExtMove* end() const { return last; }
|
||||
size_t size() const { return last - moveList; }
|
||||
bool contains(Move m) const {
|
||||
for (const ExtMove* it(moveList); it != last; ++it) if (it->move == m) return true;
|
||||
return false;
|
||||
bool contains(Move move) const {
|
||||
return std::find(begin(), end(), move) != end();
|
||||
}
|
||||
|
||||
private:
|
||||
ExtMove moveList[MAX_MOVES];
|
||||
ExtMove *cur, *last;
|
||||
ExtMove moveList[MAX_MOVES], *last;
|
||||
};
|
||||
|
||||
#endif // #ifndef MOVEGEN_H_INCLUDED
|
||||
|
||||
+227
-278
@@ -2,13 +2,13 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
@@ -21,78 +21,67 @@
|
||||
#include <cassert>
|
||||
|
||||
#include "movepick.h"
|
||||
#include "thread.h"
|
||||
|
||||
namespace {
|
||||
|
||||
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,
|
||||
QSEARCH_1, CAPTURES_S4,
|
||||
PROBCUT, CAPTURES_S5,
|
||||
RECAPTURE, CAPTURES_S6,
|
||||
STOP
|
||||
MAIN_SEARCH, CAPTURES_INIT, GOOD_CAPTURES, KILLERS, COUNTERMOVE, QUIET_INIT, QUIET, BAD_CAPTURES,
|
||||
EVASION, EVASIONS_INIT, ALL_EVASIONS,
|
||||
PROBCUT, PROBCUT_INIT, PROBCUT_CAPTURES,
|
||||
QSEARCH_WITH_CHECKS, QCAPTURES_1_INIT, QCAPTURES_1, QCHECKS,
|
||||
QSEARCH_NO_CHECKS, QCAPTURES_2_INIT, QCAPTURES_2,
|
||||
QSEARCH_RECAPTURES, QRECAPTURES
|
||||
};
|
||||
|
||||
// Our insertion sort, which is guaranteed (and also needed) to be stable
|
||||
void insertion_sort(ExtMove* begin, ExtMove* end)
|
||||
{
|
||||
ExtMove tmp, *p, *q;
|
||||
// partial_insertion_sort() sorts moves in descending order up to and including
|
||||
// a given limit. The order of moves smaller than the limit is left unspecified.
|
||||
void partial_insertion_sort(ExtMove* begin, ExtMove* end, int limit) {
|
||||
|
||||
for (p = begin + 1; p < end; ++p)
|
||||
{
|
||||
tmp = *p;
|
||||
for (q = p; q != begin && *(q-1) < tmp; --q)
|
||||
*q = *(q-1);
|
||||
*q = tmp;
|
||||
}
|
||||
for (ExtMove *sortedEnd = begin, *p = begin + 1; p < end; ++p)
|
||||
if (p->value >= limit)
|
||||
{
|
||||
ExtMove tmp = *p, *q;
|
||||
*p = *++sortedEnd;
|
||||
for (q = sortedEnd; q != begin && *(q - 1) < tmp; --q)
|
||||
*q = *(q - 1);
|
||||
*q = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
// Unary predicate used by std::partition to split positive values from remaining
|
||||
// ones so as to sort the two sets separately, with the second sort delayed.
|
||||
inline bool has_positive_value(const ExtMove& move) { return move.value > VALUE_ZERO; }
|
||||
// pick_best() finds the best move in the range (begin, end) and moves it to
|
||||
// the front. It's faster than sorting all the moves in advance when there
|
||||
// are few moves, e.g., the possible captures.
|
||||
Move pick_best(ExtMove* begin, ExtMove* end) {
|
||||
|
||||
// Picks the best move in the range (begin, end) and moves it to the front.
|
||||
// It's faster than sorting all the moves in advance when there are few
|
||||
// moves e.g. possible captures.
|
||||
inline ExtMove* pick_best(ExtMove* begin, ExtMove* end)
|
||||
{
|
||||
std::swap(*begin, *std::max_element(begin, end));
|
||||
return begin;
|
||||
std::swap(*begin, *std::max_element(begin, end));
|
||||
return *begin;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
/// Constructors of the MovePicker class. As arguments we pass information
|
||||
/// to help it to return the (presumably) good moves first, to decide which
|
||||
/// moves to return (in the quiescence search, for instance, we only want to
|
||||
/// search captures, promotions and some checks) and how important good move
|
||||
/// search captures, promotions, and some checks) and how important good move
|
||||
/// ordering is at the current node.
|
||||
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
|
||||
Move* cm, Move* fm, Search::Stack* s) : pos(p), history(h), depth(d) {
|
||||
/// MovePicker constructor for the main search
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
|
||||
const CapturePieceToHistory* cph, const PieceToHistory** ch, Move cm, Move* killers_p)
|
||||
: pos(p), mainHistory(mh), captureHistory(cph), contHistory(ch), countermove(cm),
|
||||
killers{killers_p[0], killers_p[1]}, depth(d){
|
||||
|
||||
assert(d > DEPTH_ZERO);
|
||||
|
||||
cur = end = moves;
|
||||
endBadCaptures = moves + MAX_MOVES - 1;
|
||||
countermoves = cm;
|
||||
followupmoves = fm;
|
||||
ss = s;
|
||||
|
||||
if (pos.checkers())
|
||||
stage = EVASION;
|
||||
|
||||
else
|
||||
stage = MAIN_SEARCH;
|
||||
|
||||
ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
end += (ttMove != MOVE_NONE);
|
||||
stage = pos.checkers() ? EVASION : MAIN_SEARCH;
|
||||
ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
|
||||
stage += (ttMove == MOVE_NONE);
|
||||
}
|
||||
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
|
||||
Square s) : pos(p), history(h), cur(moves), end(moves) {
|
||||
/// MovePicker constructor for quiescence search
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh, const CapturePieceToHistory* cph, Square s)
|
||||
: pos(p), mainHistory(mh), captureHistory(cph) {
|
||||
|
||||
assert(d <= DEPTH_ZERO);
|
||||
|
||||
@@ -100,286 +89,246 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats&
|
||||
stage = EVASION;
|
||||
|
||||
else if (d > DEPTH_QS_NO_CHECKS)
|
||||
stage = QSEARCH_0;
|
||||
stage = QSEARCH_WITH_CHECKS;
|
||||
|
||||
else if (d > DEPTH_QS_RECAPTURES)
|
||||
stage = QSEARCH_1;
|
||||
stage = QSEARCH_NO_CHECKS;
|
||||
|
||||
else
|
||||
{
|
||||
stage = RECAPTURE;
|
||||
stage = QSEARCH_RECAPTURES;
|
||||
recaptureSquare = s;
|
||||
ttm = MOVE_NONE;
|
||||
return;
|
||||
}
|
||||
|
||||
ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
end += (ttMove != MOVE_NONE);
|
||||
ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
|
||||
stage += (ttMove == MOVE_NONE);
|
||||
}
|
||||
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, PieceType pt)
|
||||
: pos(p), history(h), cur(moves), end(moves) {
|
||||
/// MovePicker constructor for ProbCut: we generate captures with SEE higher
|
||||
/// than or equal to the given threshold.
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Value th, const CapturePieceToHistory* cph)
|
||||
: pos(p), captureHistory(cph), threshold(th) {
|
||||
|
||||
assert(!pos.checkers());
|
||||
|
||||
stage = PROBCUT;
|
||||
ttMove = ttm
|
||||
&& pos.pseudo_legal(ttm)
|
||||
&& pos.capture(ttm)
|
||||
&& pos.see_ge(ttm, threshold) ? ttm : MOVE_NONE;
|
||||
|
||||
// In ProbCut we generate only captures that are better than the parent's
|
||||
// captured piece.
|
||||
captureThreshold = PieceValue[MG][pt];
|
||||
ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
|
||||
if (ttMove && (!pos.capture(ttMove) || pos.see(ttMove) <= captureThreshold))
|
||||
ttMove = MOVE_NONE;
|
||||
|
||||
end += (ttMove != MOVE_NONE);
|
||||
stage += (ttMove == MOVE_NONE);
|
||||
}
|
||||
|
||||
/// score() assigns a numerical value to each move in a list, used for sorting.
|
||||
/// Captures are ordered by Most Valuable Victim (MVV), preferring captures
|
||||
/// with a good history. Quiets are ordered using the histories.
|
||||
template<GenType Type>
|
||||
void MovePicker::score() {
|
||||
|
||||
/// score() assign a numerical value to each move in a move list. The moves with
|
||||
/// highest values will be picked first.
|
||||
template<>
|
||||
void MovePicker::score<CAPTURES>() {
|
||||
// Winning and equal captures in the main search are ordered by MVV/LVA.
|
||||
// Suprisingly, this appears to perform slightly better than SEE based
|
||||
// move ordering. The reason is probably that in a position with a winning
|
||||
// capture, capturing a more valuable (but sufficiently defended) piece
|
||||
// first usually doesn't hurt. The opponent will have to recapture, and
|
||||
// the hanging piece will still be hanging (except in the unusual cases
|
||||
// where it is possible to recapture with the hanging piece). Exchanging
|
||||
// big pieces before capturing a hanging piece probably helps to reduce
|
||||
// the subtree size.
|
||||
// In main search we want to push captures with negative SEE values to the
|
||||
// badCaptures[] array, but instead of doing it now we delay until the move
|
||||
// has been picked up in pick_move_from_list(). This way we save some SEE
|
||||
// calls in case we get a cutoff.
|
||||
Move m;
|
||||
static_assert(Type == CAPTURES || Type == QUIETS || Type == EVASIONS, "Wrong type");
|
||||
|
||||
for (ExtMove* it = moves; it != end; ++it)
|
||||
{
|
||||
m = it->move;
|
||||
it->value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||
- Value(type_of(pos.moved_piece(m)));
|
||||
for (auto& m : *this)
|
||||
if (Type == CAPTURES)
|
||||
m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||
+ Value((*captureHistory)[pos.moved_piece(m)][to_sq(m)][type_of(pos.piece_on(to_sq(m)))]);
|
||||
|
||||
if (type_of(m) == ENPASSANT)
|
||||
it->value += PieceValue[MG][PAWN];
|
||||
else if (Type == QUIETS)
|
||||
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
|
||||
+ (*contHistory[0])[pos.moved_piece(m)][to_sq(m)]
|
||||
+ (*contHistory[1])[pos.moved_piece(m)][to_sq(m)]
|
||||
+ (*contHistory[3])[pos.moved_piece(m)][to_sq(m)];
|
||||
|
||||
else if (type_of(m) == PROMOTION)
|
||||
it->value += PieceValue[MG][promotion_type(m)] - PieceValue[MG][PAWN];
|
||||
}
|
||||
else // Type == EVASIONS
|
||||
{
|
||||
if (pos.capture(m))
|
||||
m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||
- Value(type_of(pos.moved_piece(m)));
|
||||
else
|
||||
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)] - (1 << 28);
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
void MovePicker::score<QUIETS>() {
|
||||
|
||||
Move m;
|
||||
|
||||
for (ExtMove* it = moves; it != end; ++it)
|
||||
{
|
||||
m = it->move;
|
||||
it->value = history[pos.moved_piece(m)][to_sq(m)];
|
||||
}
|
||||
}
|
||||
|
||||
template<>
|
||||
void MovePicker::score<EVASIONS>() {
|
||||
// Try good captures ordered by MVV/LVA, then non-captures if destination square
|
||||
// is not under attack, ordered by history value, then bad-captures and quiet
|
||||
// moves with a negative SEE. This last group is ordered by the SEE value.
|
||||
Move m;
|
||||
Value see;
|
||||
|
||||
for (ExtMove* it = moves; it != end; ++it)
|
||||
{
|
||||
m = it->move;
|
||||
if ((see = pos.see_sign(m)) < VALUE_ZERO)
|
||||
it->value = see - HistoryStats::Max; // At the bottom
|
||||
|
||||
else if (pos.capture(m))
|
||||
it->value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||
- Value(type_of(pos.moved_piece(m))) + HistoryStats::Max;
|
||||
else
|
||||
it->value = history[pos.moved_piece(m)][to_sq(m)];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// generate_next_stage() generates, scores and sorts the next bunch of moves,
|
||||
/// when there are no more moves to try for the current stage.
|
||||
|
||||
void MovePicker::generate_next_stage() {
|
||||
|
||||
cur = moves;
|
||||
|
||||
switch (++stage) {
|
||||
|
||||
case CAPTURES_S1: case CAPTURES_S3: case CAPTURES_S4: case CAPTURES_S5: case CAPTURES_S6:
|
||||
end = generate<CAPTURES>(pos, moves);
|
||||
score<CAPTURES>();
|
||||
return;
|
||||
|
||||
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;
|
||||
killers[4].move = killers[5].move = MOVE_NONE;
|
||||
|
||||
// Please note that following code is racy and could yield to rare (less
|
||||
// than 1 out of a million) duplicated entries in SMP case. This is harmless.
|
||||
|
||||
// Be sure countermoves are different from killers
|
||||
for (int i = 0; i < 2; ++i)
|
||||
if ( countermoves[i] != (cur+0)->move
|
||||
&& countermoves[i] != (cur+1)->move)
|
||||
(end++)->move = countermoves[i];
|
||||
|
||||
// Be sure followupmoves are different from killers and countermoves
|
||||
for (int i = 0; i < 2; ++i)
|
||||
if ( followupmoves[i] != (cur+0)->move
|
||||
&& followupmoves[i] != (cur+1)->move
|
||||
&& followupmoves[i] != (cur+2)->move
|
||||
&& followupmoves[i] != (cur+3)->move)
|
||||
(end++)->move = followupmoves[i];
|
||||
return;
|
||||
|
||||
case QUIETS_1_S1:
|
||||
endQuiets = end = generate<QUIETS>(pos, moves);
|
||||
score<QUIETS>();
|
||||
end = std::partition(cur, end, has_positive_value);
|
||||
insertion_sort(cur, end);
|
||||
return;
|
||||
|
||||
case QUIETS_2_S1:
|
||||
cur = end;
|
||||
end = endQuiets;
|
||||
if (depth >= 3 * ONE_PLY)
|
||||
insertion_sort(cur, end);
|
||||
return;
|
||||
|
||||
case BAD_CAPTURES_S1:
|
||||
// Just pick them in reverse order to get MVV/LVA ordering
|
||||
cur = moves + MAX_MOVES - 1;
|
||||
end = endBadCaptures;
|
||||
return;
|
||||
|
||||
case EVASIONS_S2:
|
||||
end = generate<EVASIONS>(pos, moves);
|
||||
if (end > moves + 1)
|
||||
score<EVASIONS>();
|
||||
return;
|
||||
|
||||
case QUIET_CHECKS_S3:
|
||||
end = generate<QUIET_CHECKS>(pos, moves);
|
||||
return;
|
||||
|
||||
case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT: case RECAPTURE:
|
||||
stage = STOP;
|
||||
/* Fall through */
|
||||
|
||||
case STOP:
|
||||
end = cur + 1; // Avoid another next_phase() call
|
||||
return;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// next_move() is the most important method of the MovePicker class. It returns
|
||||
/// a new pseudo legal move every time it is called, until there are no more moves
|
||||
/// left. It picks the move with the biggest value from a list of generated moves
|
||||
/// taking care not to return the ttMove if it has already been searched.
|
||||
template<>
|
||||
Move MovePicker::next_move<false>() {
|
||||
|
||||
Move MovePicker::next_move(bool skipQuiets) {
|
||||
|
||||
Move move;
|
||||
|
||||
while (true)
|
||||
{
|
||||
while (cur == end)
|
||||
generate_next_stage();
|
||||
switch (stage) {
|
||||
|
||||
switch (stage) {
|
||||
case MAIN_SEARCH: case EVASION: case QSEARCH_WITH_CHECKS:
|
||||
case QSEARCH_NO_CHECKS: case PROBCUT:
|
||||
++stage;
|
||||
return ttMove;
|
||||
|
||||
case MAIN_SEARCH: case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT:
|
||||
++cur;
|
||||
return ttMove;
|
||||
case CAPTURES_INIT:
|
||||
endBadCaptures = cur = moves;
|
||||
endMoves = generate<CAPTURES>(pos, cur);
|
||||
score<CAPTURES>();
|
||||
++stage;
|
||||
/* fallthrough */
|
||||
|
||||
case CAPTURES_S1:
|
||||
move = pick_best(cur++, end)->move;
|
||||
case GOOD_CAPTURES:
|
||||
while (cur < endMoves)
|
||||
{
|
||||
move = pick_best(cur++, endMoves);
|
||||
if (move != ttMove)
|
||||
{
|
||||
if (pos.see_sign(move) >= VALUE_ZERO)
|
||||
if (pos.see_ge(move, Value(-55 * (cur-1)->value / 1024)))
|
||||
return move;
|
||||
|
||||
// Losing capture, move it to the tail of the array
|
||||
(endBadCaptures--)->move = move;
|
||||
// Losing capture, move it to the beginning of the array
|
||||
*endBadCaptures++ = move;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case KILLERS_S1:
|
||||
move = (cur++)->move;
|
||||
if ( move != MOVE_NONE
|
||||
&& move != ttMove
|
||||
&& pos.pseudo_legal(move)
|
||||
&& !pos.capture(move))
|
||||
return move;
|
||||
break;
|
||||
++stage;
|
||||
move = killers[0]; // First killer move
|
||||
if ( move != MOVE_NONE
|
||||
&& move != ttMove
|
||||
&& pos.pseudo_legal(move)
|
||||
&& !pos.capture(move))
|
||||
return move;
|
||||
/* fallthrough */
|
||||
|
||||
case KILLERS:
|
||||
++stage;
|
||||
move = killers[1]; // Second killer move
|
||||
if ( move != MOVE_NONE
|
||||
&& move != ttMove
|
||||
&& pos.pseudo_legal(move)
|
||||
&& !pos.capture(move))
|
||||
return move;
|
||||
/* fallthrough */
|
||||
|
||||
case COUNTERMOVE:
|
||||
++stage;
|
||||
move = countermove;
|
||||
if ( move != MOVE_NONE
|
||||
&& move != ttMove
|
||||
&& move != killers[0]
|
||||
&& move != killers[1]
|
||||
&& pos.pseudo_legal(move)
|
||||
&& !pos.capture(move))
|
||||
return move;
|
||||
/* fallthrough */
|
||||
|
||||
case QUIET_INIT:
|
||||
cur = endBadCaptures;
|
||||
endMoves = generate<QUIETS>(pos, cur);
|
||||
score<QUIETS>();
|
||||
partial_insertion_sort(cur, endMoves, -4000 * depth / ONE_PLY);
|
||||
++stage;
|
||||
/* fallthrough */
|
||||
|
||||
case QUIET:
|
||||
while ( cur < endMoves
|
||||
&& (!skipQuiets || cur->value >= VALUE_ZERO))
|
||||
{
|
||||
move = *cur++;
|
||||
|
||||
case QUIETS_1_S1: case QUIETS_2_S1:
|
||||
move = (cur++)->move;
|
||||
if ( move != ttMove
|
||||
&& move != killers[0].move
|
||||
&& move != killers[1].move
|
||||
&& move != killers[2].move
|
||||
&& move != killers[3].move
|
||||
&& move != killers[4].move
|
||||
&& move != killers[5].move)
|
||||
&& move != killers[0]
|
||||
&& move != killers[1]
|
||||
&& move != countermove)
|
||||
return move;
|
||||
break;
|
||||
}
|
||||
++stage;
|
||||
cur = moves; // Point to beginning of bad captures
|
||||
/* fallthrough */
|
||||
|
||||
case BAD_CAPTURES_S1:
|
||||
return (cur--)->move;
|
||||
case BAD_CAPTURES:
|
||||
if (cur < endBadCaptures)
|
||||
return *cur++;
|
||||
break;
|
||||
|
||||
case EVASIONS_S2: case CAPTURES_S3: case CAPTURES_S4:
|
||||
move = pick_best(cur++, end)->move;
|
||||
case EVASIONS_INIT:
|
||||
cur = moves;
|
||||
endMoves = generate<EVASIONS>(pos, cur);
|
||||
score<EVASIONS>();
|
||||
++stage;
|
||||
/* fallthrough */
|
||||
|
||||
case ALL_EVASIONS:
|
||||
while (cur < endMoves)
|
||||
{
|
||||
move = pick_best(cur++, endMoves);
|
||||
if (move != ttMove)
|
||||
return move;
|
||||
}
|
||||
break;
|
||||
|
||||
case PROBCUT_INIT:
|
||||
cur = moves;
|
||||
endMoves = generate<CAPTURES>(pos, cur);
|
||||
score<CAPTURES>();
|
||||
++stage;
|
||||
/* fallthrough */
|
||||
|
||||
case PROBCUT_CAPTURES:
|
||||
while (cur < endMoves)
|
||||
{
|
||||
move = pick_best(cur++, endMoves);
|
||||
if ( move != ttMove
|
||||
&& pos.see_ge(move, threshold))
|
||||
return move;
|
||||
}
|
||||
break;
|
||||
|
||||
case QCAPTURES_1_INIT: case QCAPTURES_2_INIT:
|
||||
cur = moves;
|
||||
endMoves = generate<CAPTURES>(pos, cur);
|
||||
score<CAPTURES>();
|
||||
++stage;
|
||||
/* fallthrough */
|
||||
|
||||
case QCAPTURES_1: case QCAPTURES_2:
|
||||
while (cur < endMoves)
|
||||
{
|
||||
move = pick_best(cur++, endMoves);
|
||||
if (move != ttMove)
|
||||
return move;
|
||||
}
|
||||
if (stage == QCAPTURES_2)
|
||||
break;
|
||||
cur = moves;
|
||||
endMoves = generate<QUIET_CHECKS>(pos, cur);
|
||||
++stage;
|
||||
/* fallthrough */
|
||||
|
||||
case CAPTURES_S5:
|
||||
move = pick_best(cur++, end)->move;
|
||||
if (move != ttMove && pos.see(move) > captureThreshold)
|
||||
return move;
|
||||
break;
|
||||
case QCHECKS:
|
||||
while (cur < endMoves)
|
||||
{
|
||||
move = cur++->move;
|
||||
if (move != ttMove)
|
||||
return move;
|
||||
}
|
||||
break;
|
||||
|
||||
case CAPTURES_S6:
|
||||
move = pick_best(cur++, end)->move;
|
||||
case QSEARCH_RECAPTURES:
|
||||
cur = moves;
|
||||
endMoves = generate<CAPTURES>(pos, cur);
|
||||
score<CAPTURES>();
|
||||
++stage;
|
||||
/* fallthrough */
|
||||
|
||||
case QRECAPTURES:
|
||||
while (cur < endMoves)
|
||||
{
|
||||
move = pick_best(cur++, endMoves);
|
||||
if (to_sq(move) == recaptureSquare)
|
||||
return move;
|
||||
break;
|
||||
|
||||
case QUIET_CHECKS_S3:
|
||||
move = (cur++)->move;
|
||||
if (move != ttMove)
|
||||
return move;
|
||||
break;
|
||||
|
||||
case STOP:
|
||||
return MOVE_NONE;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
|
||||
return MOVE_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// Version of next_move() to use at split point nodes where the move is grabbed
|
||||
/// from the split point's shared MovePicker object. This function is not thread
|
||||
/// safe so must be lock protected by the caller.
|
||||
template<>
|
||||
Move MovePicker::next_move<true>() { return ss->splitPoint->movePicker->next_move<false>(); }
|
||||
|
||||
+95
-56
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -20,56 +21,97 @@
|
||||
#ifndef MOVEPICK_H_INCLUDED
|
||||
#define MOVEPICK_H_INCLUDED
|
||||
|
||||
#include <algorithm> // For std::max
|
||||
#include <cstring> // For std::memset
|
||||
#include <array>
|
||||
#include <limits>
|
||||
|
||||
#include "movegen.h"
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
#include "types.h"
|
||||
|
||||
/// StatBoards is a generic 2-dimensional array used to store various statistics
|
||||
template<int Size1, int Size2, typename T = int16_t>
|
||||
struct StatBoards : public std::array<std::array<T, Size2>, Size1> {
|
||||
|
||||
/// The Stats struct stores moves statistics. According to the template parameter
|
||||
/// 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
|
||||
/// using only the 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(250);
|
||||
|
||||
const T* operator[](Piece pc) const { return table[pc]; }
|
||||
void clear() { std::memset(table, 0, sizeof(table)); }
|
||||
|
||||
void update(Piece pc, Square to, Move m) {
|
||||
|
||||
if (m == table[pc][to].first)
|
||||
return;
|
||||
|
||||
table[pc][to].second = table[pc][to].first;
|
||||
table[pc][to].first = m;
|
||||
void fill(const T& v) {
|
||||
T* p = &(*this)[0][0];
|
||||
std::fill(p, p + sizeof(*this) / sizeof(*p), v);
|
||||
}
|
||||
|
||||
void update(Piece pc, Square to, Value v) {
|
||||
void update(T& entry, int bonus, const int D) {
|
||||
|
||||
if (Gain)
|
||||
table[pc][to] = std::max(v, table[pc][to] - 1);
|
||||
assert(abs(bonus) <= D); // Ensure range is [-32 * D, 32 * D]
|
||||
assert(abs(32 * D) < (std::numeric_limits<T>::max)()); // Ensure we don't overflow
|
||||
|
||||
else if (abs(table[pc][to] + v) < Max)
|
||||
table[pc][to] += v;
|
||||
entry += bonus * 32 - entry * abs(bonus) / D;
|
||||
|
||||
assert(abs(entry) <= 32 * D);
|
||||
}
|
||||
|
||||
private:
|
||||
T table[PIECE_NB][SQUARE_NB];
|
||||
};
|
||||
|
||||
typedef Stats< true, Value> GainsStats;
|
||||
typedef Stats<false, Value> HistoryStats;
|
||||
typedef Stats<false, std::pair<Move, Move> > MovesStats;
|
||||
/// StatCubes is a generic 3-dimensional array used to store various statistics
|
||||
template<int Size1, int Size2, int Size3, typename T = int16_t>
|
||||
struct StatCubes : public std::array<std::array<std::array<T, Size3>, Size2>, Size1> {
|
||||
|
||||
void fill(const T& v) {
|
||||
T* p = &(*this)[0][0][0];
|
||||
std::fill(p, p + sizeof(*this) / sizeof(*p), v);
|
||||
}
|
||||
|
||||
void update(T& entry, int bonus, const int D, const int W) {
|
||||
|
||||
assert(abs(bonus) <= D); // Ensure range is [-W * D, W * D]
|
||||
assert(abs(W * D) < (std::numeric_limits<T>::max)()); // Ensure we don't overflow
|
||||
|
||||
entry += bonus * W - entry * abs(bonus) / D;
|
||||
|
||||
assert(abs(entry) <= W * D);
|
||||
}
|
||||
};
|
||||
|
||||
/// ButterflyBoards are 2 tables (one for each color) indexed by the move's from
|
||||
/// and to squares, see chessprogramming.wikispaces.com/Butterfly+Boards
|
||||
typedef StatBoards<COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)> ButterflyBoards;
|
||||
|
||||
/// PieceToBoards are addressed by a move's [piece][to] information
|
||||
typedef StatBoards<PIECE_NB, SQUARE_NB> PieceToBoards;
|
||||
|
||||
/// CapturePieceToBoards are addressed by a move's [piece][to][captured piece type] information
|
||||
typedef StatCubes<PIECE_NB, SQUARE_NB, PIECE_TYPE_NB> CapturePieceToBoards;
|
||||
|
||||
/// ButterflyHistory records how often quiet moves have been successful or
|
||||
/// unsuccessful during the current search, and is used for reduction and move
|
||||
/// ordering decisions. It uses ButterflyBoards as backing store.
|
||||
struct ButterflyHistory : public ButterflyBoards {
|
||||
|
||||
void update(Color c, Move m, int bonus) {
|
||||
StatBoards::update((*this)[c][from_to(m)], bonus, 324);
|
||||
}
|
||||
};
|
||||
|
||||
/// PieceToHistory is like ButterflyHistory, but is based on PieceToBoards
|
||||
struct PieceToHistory : public PieceToBoards {
|
||||
|
||||
void update(Piece pc, Square to, int bonus) {
|
||||
StatBoards::update((*this)[pc][to], bonus, 936);
|
||||
}
|
||||
};
|
||||
|
||||
/// CapturePieceToHistory is like PieceToHistory, but is based on CapturePieceToBoards
|
||||
struct CapturePieceToHistory : public CapturePieceToBoards {
|
||||
|
||||
void update(Piece pc, Square to, PieceType captured, int bonus) {
|
||||
StatCubes::update((*this)[pc][to][captured], bonus, 324, 2);
|
||||
}
|
||||
};
|
||||
|
||||
/// CounterMoveHistory stores counter moves indexed by [piece][to] of the previous
|
||||
/// move, see chessprogramming.wikispaces.com/Countermove+Heuristic
|
||||
typedef StatBoards<PIECE_NB, SQUARE_NB, Move> CounterMoveHistory;
|
||||
|
||||
/// ContinuationHistory is the history of a given pair of moves, usually the
|
||||
/// current one given a previous one. History table is based on PieceToBoards
|
||||
/// instead of ButterflyBoards.
|
||||
typedef StatBoards<PIECE_NB, SQUARE_NB, PieceToHistory> ContinuationHistory;
|
||||
|
||||
|
||||
/// MovePicker class is used to pick one pseudo legal move at a time from the
|
||||
@@ -80,32 +122,29 @@ typedef Stats<false, std::pair<Move, Move> > MovesStats;
|
||||
/// to get a cut-off first.
|
||||
|
||||
class MovePicker {
|
||||
|
||||
MovePicker& operator=(const MovePicker&); // Silence a warning under MSVC
|
||||
|
||||
public:
|
||||
MovePicker(const Position&, Move, Depth, const HistoryStats&, Square);
|
||||
MovePicker(const Position&, Move, const HistoryStats&, PieceType);
|
||||
MovePicker(const Position&, Move, Depth, const HistoryStats&, Move*, Move*, Search::Stack*);
|
||||
|
||||
template<bool SpNode> Move next_move();
|
||||
MovePicker(const MovePicker&) = delete;
|
||||
MovePicker& operator=(const MovePicker&) = delete;
|
||||
MovePicker(const Position&, Move, Value, const CapturePieceToHistory*);
|
||||
MovePicker(const Position&, Move, Depth, const ButterflyHistory*, const CapturePieceToHistory*, Square);
|
||||
MovePicker(const Position&, Move, Depth, const ButterflyHistory*, const CapturePieceToHistory*, const PieceToHistory**, Move, Move*);
|
||||
Move next_move(bool skipQuiets = false);
|
||||
|
||||
private:
|
||||
template<GenType> void score();
|
||||
void generate_next_stage();
|
||||
ExtMove* begin() { return cur; }
|
||||
ExtMove* end() { return endMoves; }
|
||||
|
||||
const Position& pos;
|
||||
const HistoryStats& history;
|
||||
Search::Stack* ss;
|
||||
Move* countermoves;
|
||||
Move* followupmoves;
|
||||
Depth depth;
|
||||
Move ttMove;
|
||||
ExtMove killers[6];
|
||||
Square recaptureSquare;
|
||||
Value captureThreshold;
|
||||
const ButterflyHistory* mainHistory;
|
||||
const CapturePieceToHistory* captureHistory;
|
||||
const PieceToHistory** contHistory;
|
||||
Move ttMove, countermove, killers[2];
|
||||
ExtMove *cur, *endMoves, *endBadCaptures;
|
||||
int stage;
|
||||
ExtMove *cur, *end, *endQuiets, *endBadCaptures;
|
||||
Square recaptureSquare;
|
||||
Value threshold;
|
||||
Depth depth;
|
||||
ExtMove moves[MAX_MOVES];
|
||||
};
|
||||
|
||||
|
||||
+126
-139
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -21,7 +22,6 @@
|
||||
#include <cassert>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "bitcount.h"
|
||||
#include "pawns.h"
|
||||
#include "position.h"
|
||||
#include "thread.h"
|
||||
@@ -31,65 +31,56 @@ namespace {
|
||||
#define V Value
|
||||
#define S(mg, eg) make_score(mg, eg)
|
||||
|
||||
// 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
|
||||
const Score Isolated = S(13, 18);
|
||||
|
||||
// Isolated pawn penalty by opposed flag and file
|
||||
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) } };
|
||||
// Backward pawn penalty
|
||||
const Score Backward = S(24, 12);
|
||||
|
||||
// Backward pawn penalty by opposed flag and file
|
||||
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) } };
|
||||
// Connected pawn bonus by opposed, phalanx, #support and rank
|
||||
Score Connected[2][2][3][RANK_NB];
|
||||
|
||||
// Connected pawn bonus by opposed, phalanx flags and rank
|
||||
Score Connected[2][2][RANK_NB];
|
||||
// Doubled pawn penalty
|
||||
const Score Doubled = S(18, 38);
|
||||
|
||||
// Levers bonus by rank
|
||||
const Score Lever[RANK_NB] = {
|
||||
S( 0, 0), S( 0, 0), S(0, 0), S(0, 0),
|
||||
S(20,20), S(40,40), S(0, 0), S(0, 0) };
|
||||
// Weakness of our pawn shelter in front of the king by [isKingFile][distance from edge][rank].
|
||||
// RANK_1 = 0 is used for files where we have no pawns or our pawn is behind our king.
|
||||
const Value ShelterWeakness[][int(FILE_NB) / 2][RANK_NB] = {
|
||||
{ { V( 97), V(17), V( 9), V(44), V( 84), V( 87), V( 99) }, // Not On King file
|
||||
{ V(106), V( 6), V(33), V(86), V( 87), V(104), V(112) },
|
||||
{ V(101), V( 2), V(65), V(98), V( 58), V( 89), V(115) },
|
||||
{ V( 73), V( 7), V(54), V(73), V( 84), V( 83), V(111) } },
|
||||
{ { V(104), V(20), V( 6), V(27), V( 86), V( 93), V( 82) }, // On King file
|
||||
{ V(123), V( 9), V(34), V(96), V(112), V( 88), V( 75) },
|
||||
{ V(120), V(25), V(65), V(91), V( 66), V( 78), V(117) },
|
||||
{ V( 81), V( 2), V(47), V(63), V( 94), V( 93), V(104) } }
|
||||
};
|
||||
|
||||
// Unsupported pawn penalty
|
||||
const Score UnsupportedPawnPenalty = S(20, 10);
|
||||
|
||||
// Weakness of our pawn shelter in front of the king by [distance from edge][rank]
|
||||
const Value ShelterWeakness[][RANK_NB] = {
|
||||
{ V(100), V(13), V(24), V(64), V(89), V( 93), V(104) },
|
||||
{ V(110), V( 1), V(29), V(75), V(96), V(102), V(107) },
|
||||
{ V(102), V( 0), V(39), V(74), V(88), V(101), V( 98) },
|
||||
{ V( 88), V( 4), V(33), V(67), V(92), V( 94), V(107) } };
|
||||
|
||||
// Danger of enemy pawns moving toward our king by [type][distance from edge][rank]
|
||||
// Danger of enemy pawns moving toward our king by [type][distance from edge][rank].
|
||||
// For the unopposed and unblocked cases, RANK_1 = 0 is used when opponent has
|
||||
// no pawn on the given file, or their pawn is behind our king.
|
||||
const Value StormDanger[][4][RANK_NB] = {
|
||||
{ { V( 0), V( 63), V( 128), V(43), V(27) },
|
||||
{ V( 0), V( 62), V( 131), V(44), V(26) },
|
||||
{ V( 0), V( 59), V( 121), V(50), V(28) },
|
||||
{ V( 0), V( 62), V( 127), V(54), V(28) } },
|
||||
{ { V(24), V( 40), V( 93), V(42), V(22) },
|
||||
{ V(24), V( 28), V( 101), V(38), V(20) },
|
||||
{ V(24), V( 32), V( 95), V(36), V(23) },
|
||||
{ V(27), V( 24), V( 99), V(36), V(24) } },
|
||||
{ { V( 0), V( 0), V( 81), V(16), V( 6) },
|
||||
{ V( 0), V( 0), V( 165), V(29), V( 9) },
|
||||
{ V( 0), V( 0), V( 163), V(23), V(12) },
|
||||
{ V( 0), V( 0), V( 161), V(28), V(13) } },
|
||||
{ { V( 0), V(-296), V(-299), V(55), V(25) },
|
||||
{ V( 0), V( 67), V( 131), V(46), V(21) },
|
||||
{ V( 0), V( 65), V( 135), V(50), V(31) },
|
||||
{ V( 0), V( 62), V( 128), V(51), V(24) } } };
|
||||
{ { V( 0), V(-290), V(-274), V(57), V(41) }, // BlockedByKing
|
||||
{ V( 0), V( 60), V( 144), V(39), V(13) },
|
||||
{ V( 0), V( 65), V( 141), V(41), V(34) },
|
||||
{ V( 0), V( 53), V( 127), V(56), V(14) } },
|
||||
{ { V( 4), V( 73), V( 132), V(46), V(31) }, // Unopposed
|
||||
{ V( 1), V( 64), V( 143), V(26), V(13) },
|
||||
{ V( 1), V( 47), V( 110), V(44), V(24) },
|
||||
{ V( 0), V( 72), V( 127), V(50), V(31) } },
|
||||
{ { V( 0), V( 0), V( 79), V(23), V( 1) }, // BlockedByPawn
|
||||
{ V( 0), V( 0), V( 148), V(27), V( 2) },
|
||||
{ V( 0), V( 0), V( 161), V(16), V( 1) },
|
||||
{ V( 0), V( 0), V( 171), V(22), V(15) } },
|
||||
{ { V(22), V( 45), V( 104), V(62), V( 6) }, // Unblocked
|
||||
{ V(31), V( 30), V( 99), V(39), V(19) },
|
||||
{ V(23), V( 29), V( 96), V(41), V(15) },
|
||||
{ V(21), V( 23), V( 116), V(41), V(15) } }
|
||||
};
|
||||
|
||||
// Max bonus for king safety. Corresponds to start position with all the pawns
|
||||
// in front of the king and no enemy pawn on the horizon.
|
||||
const Value MaxSafetyBonus = V(257);
|
||||
const Value MaxSafetyBonus = V(258);
|
||||
|
||||
#undef S
|
||||
#undef V
|
||||
@@ -97,26 +88,26 @@ namespace {
|
||||
template<Color Us>
|
||||
Score evaluate(const Position& pos, Pawns::Entry* e) {
|
||||
|
||||
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);
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
const Direction Up = (Us == WHITE ? NORTH : SOUTH);
|
||||
const Direction Right = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
|
||||
const Direction Left = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
|
||||
|
||||
Bitboard b, p, doubled, connected;
|
||||
Bitboard b, neighbours, stoppers, doubled, supported, phalanx;
|
||||
Bitboard lever, leverPush;
|
||||
Square s;
|
||||
bool passed, isolated, opposed, phalanx, backward, unsupported, lever;
|
||||
bool opposed, backward;
|
||||
Score score = SCORE_ZERO;
|
||||
const Square* pl = pos.list<PAWN>(Us);
|
||||
const Bitboard* pawnAttacksBB = StepAttacksBB[make_piece(Us, PAWN)];
|
||||
const Square* pl = pos.squares<PAWN>(Us);
|
||||
|
||||
Bitboard ourPawns = pos.pieces(Us , PAWN);
|
||||
Bitboard ourPawns = pos.pieces( Us, PAWN);
|
||||
Bitboard theirPawns = pos.pieces(Them, PAWN);
|
||||
|
||||
e->passedPawns[Us] = 0;
|
||||
e->kingSquares[Us] = SQ_NONE;
|
||||
e->passedPawns[Us] = e->pawnAttacksSpan[Us] = e->weakUnopposed[Us] = 0;
|
||||
e->semiopenFiles[Us] = 0xFF;
|
||||
e->pawnAttacks[Us] = shift_bb<Right>(ourPawns) | shift_bb<Left>(ourPawns);
|
||||
e->pawnsOnSquares[Us][BLACK] = popcount<Max15>(ourPawns & DarkSquares);
|
||||
e->kingSquares[Us] = SQ_NONE;
|
||||
e->pawnAttacks[Us] = shift<Right>(ourPawns) | shift<Left>(ourPawns);
|
||||
e->pawnsOnSquares[Us][BLACK] = popcount(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
|
||||
@@ -126,76 +117,69 @@ namespace {
|
||||
|
||||
File f = file_of(s);
|
||||
|
||||
// This file cannot be semi-open
|
||||
e->semiopenFiles[Us] &= ~(1 << f);
|
||||
e->semiopenFiles[Us] &= ~(1 << f);
|
||||
e->pawnAttacksSpan[Us] |= pawn_attack_span(Us, s);
|
||||
|
||||
// Previous rank
|
||||
p = rank_bb(s - pawn_push(Us));
|
||||
// Flag the pawn
|
||||
opposed = theirPawns & forward_file_bb(Us, s);
|
||||
stoppers = theirPawns & passed_pawn_mask(Us, s);
|
||||
lever = theirPawns & PawnAttacks[Us][s];
|
||||
leverPush = theirPawns & PawnAttacks[Us][s + Up];
|
||||
doubled = ourPawns & (s - Up);
|
||||
neighbours = ourPawns & adjacent_files_bb(f);
|
||||
phalanx = neighbours & rank_bb(s);
|
||||
supported = neighbours & rank_bb(s - Up);
|
||||
|
||||
// Flag the pawn as passed, isolated, doubled,
|
||||
// unsupported or connected (but not the backward one).
|
||||
connected = ourPawns & adjacent_files_bb(f) & (rank_bb(s) | p);
|
||||
phalanx = connected & rank_bb(s);
|
||||
unsupported = !(ourPawns & adjacent_files_bb(f) & p);
|
||||
isolated = !(ourPawns & adjacent_files_bb(f));
|
||||
doubled = ourPawns & forward_bb(Us, s);
|
||||
opposed = theirPawns & forward_bb(Us, s);
|
||||
passed = !(theirPawns & passed_pawn_mask(Us, s));
|
||||
lever = theirPawns & pawnAttacksBB[s];
|
||||
|
||||
// Test for backward pawn.
|
||||
// If the pawn is passed, isolated, or connected it cannot be
|
||||
// backward. If there are friendly pawns behind on adjacent files
|
||||
// or if it can capture an enemy pawn it cannot be backward either.
|
||||
if ( (passed | isolated | connected)
|
||||
|| (ourPawns & pawn_attack_span(Them, s))
|
||||
|| (pos.attacks_from<PAWN>(s, Us) & theirPawns))
|
||||
// A pawn is backward when it is behind all pawns of the same color on the
|
||||
// adjacent files and cannot be safely advanced.
|
||||
if (!neighbours || lever || relative_rank(Us, s) >= RANK_5)
|
||||
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 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));
|
||||
// Find the backmost rank with neighbours or stoppers
|
||||
b = rank_bb(backmost_sq(Us, neighbours | stoppers));
|
||||
|
||||
// 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;
|
||||
// The pawn is backward when it cannot safely progress to that rank:
|
||||
// either there is a stopper in the way on this rank, or there is a
|
||||
// stopper on adjacent file which controls the way to that rank.
|
||||
backward = (b | shift<Up>(b & adjacent_files_bb(f))) & stoppers;
|
||||
|
||||
assert(!(backward && (forward_ranks_bb(Them, s + Up) & neighbours)));
|
||||
}
|
||||
|
||||
assert(opposed | passed | (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
|
||||
// pawn on each file is considered a true passed pawn.
|
||||
if (passed && !doubled)
|
||||
// full attack info to evaluate them. Include also not passed pawns
|
||||
// which could become passed after one or two pawn pushes when are
|
||||
// not attacked more times than defended.
|
||||
if ( !(stoppers ^ lever ^ leverPush)
|
||||
&& !(ourPawns & forward_file_bb(Us, s))
|
||||
&& popcount(supported) >= popcount(lever)
|
||||
&& popcount(phalanx) >= popcount(leverPush))
|
||||
e->passedPawns[Us] |= s;
|
||||
|
||||
else if ( stoppers == SquareBB[s + Up]
|
||||
&& relative_rank(Us, s) >= RANK_5)
|
||||
{
|
||||
b = shift<Up>(supported) & ~theirPawns;
|
||||
while (b)
|
||||
if (!more_than_one(theirPawns & PawnAttacks[Us][pop_lsb(&b)]))
|
||||
e->passedPawns[Us] |= s;
|
||||
}
|
||||
|
||||
// Score this pawn
|
||||
if (isolated)
|
||||
score -= Isolated[opposed][f];
|
||||
if (supported | phalanx)
|
||||
score += Connected[opposed][bool(phalanx)][popcount(supported)][relative_rank(Us, s)];
|
||||
|
||||
if (unsupported && !isolated)
|
||||
score -= UnsupportedPawnPenalty;
|
||||
else if (!neighbours)
|
||||
score -= Isolated, e->weakUnopposed[Us] += !opposed;
|
||||
|
||||
if (doubled)
|
||||
score -= Doubled[f] / distance<Rank>(s, frontmost_sq(Us, doubled));
|
||||
else if (backward)
|
||||
score -= Backward, e->weakUnopposed[Us] += !opposed;
|
||||
|
||||
if (backward)
|
||||
score -= Backward[opposed][f];
|
||||
|
||||
if (connected)
|
||||
score += Connected[opposed][phalanx][relative_rank(Us, s)];
|
||||
|
||||
if (lever)
|
||||
score += Lever[relative_rank(Us, s)];
|
||||
if (doubled && !supported)
|
||||
score -= Doubled;
|
||||
}
|
||||
|
||||
b = e->semiopenFiles[Us] ^ 0xFF;
|
||||
e->pawnSpan[Us] = b ? int(msb(b) - lsb(b)) : 0;
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
@@ -207,17 +191,20 @@ namespace Pawns {
|
||||
/// hard-coded tables, when makes sense, we prefer to calculate them with a formula
|
||||
/// to reduce independent parameters and to allow easier tuning and better insight.
|
||||
|
||||
void init()
|
||||
{
|
||||
static const int Seed[RANK_NB] = { 0, 6, 15, 10, 57, 75, 135, 258 };
|
||||
void init() {
|
||||
|
||||
static const int Seed[RANK_NB] = { 0, 13, 24, 18, 76, 100, 175, 330 };
|
||||
|
||||
for (int opposed = 0; opposed <= 1; ++opposed)
|
||||
for (int phalanx = 0; phalanx <= 1; ++phalanx)
|
||||
for (Rank r = RANK_2; r < RANK_8; ++r)
|
||||
{
|
||||
int bonus = Seed[r] + (phalanx ? (Seed[r + 1] - Seed[r]) / 2 : 0);
|
||||
Connected[opposed][phalanx][r] = make_score(bonus / 2, bonus >> opposed);
|
||||
}
|
||||
for (int support = 0; support <= 2; ++support)
|
||||
for (Rank r = RANK_2; r < RANK_8; ++r)
|
||||
{
|
||||
int v = 17 * support;
|
||||
v += (Seed[r] + (phalanx ? (Seed[r + 1] - Seed[r]) / 2 : 0)) >> opposed;
|
||||
|
||||
Connected[opposed][phalanx][support][r] = make_score(v, v * (r - 2) / 4);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -236,40 +223,43 @@ Entry* probe(const Position& pos) {
|
||||
|
||||
e->key = key;
|
||||
e->score = evaluate<WHITE>(pos, e) - evaluate<BLACK>(pos, e);
|
||||
e->asymmetry = popcount(e->semiopenFiles[WHITE] ^ e->semiopenFiles[BLACK]);
|
||||
e->openFiles = popcount(e->semiopenFiles[WHITE] & e->semiopenFiles[BLACK]);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
/// Entry::shelter_storm() calculates shelter and storm penalties for the file
|
||||
/// the king is on, as well as the two adjacent files.
|
||||
/// the king is on, as well as the two closest files.
|
||||
|
||||
template<Color Us>
|
||||
Value Entry::shelter_storm(const Position& pos, Square ksq) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
enum { NoFriendlyPawn, Unblocked, BlockedByPawn, BlockedByKing };
|
||||
enum { BlockedByKing, Unopposed, BlockedByPawn, Unblocked };
|
||||
|
||||
Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq));
|
||||
Bitboard b = pos.pieces(PAWN) & (forward_ranks_bb(Us, ksq) | rank_bb(ksq));
|
||||
Bitboard ourPawns = b & pos.pieces(Us);
|
||||
Bitboard theirPawns = b & pos.pieces(Them);
|
||||
Value safety = MaxSafetyBonus;
|
||||
File center = std::max(FILE_B, std::min(FILE_G, file_of(ksq)));
|
||||
|
||||
for (File f = center - File(1); f <= center + File(1); ++f)
|
||||
for (File f = File(center - 1); f <= File(center + 1); ++f)
|
||||
{
|
||||
b = ourPawns & file_bb(f);
|
||||
Rank rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1;
|
||||
|
||||
b = theirPawns & file_bb(f);
|
||||
b = theirPawns & file_bb(f);
|
||||
Rank rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1;
|
||||
|
||||
safety -= ShelterWeakness[std::min(f, FILE_H - f)][rkUs]
|
||||
int d = std::min(f, ~f);
|
||||
safety -= ShelterWeakness[f == file_of(ksq)][d][rkUs]
|
||||
+ StormDanger
|
||||
[f == file_of(ksq) && rkThem == relative_rank(Us, ksq) + 1 ? BlockedByKing :
|
||||
rkUs == RANK_1 ? NoFriendlyPawn :
|
||||
rkUs == RANK_1 ? Unopposed :
|
||||
rkThem == rkUs + 1 ? BlockedByPawn : Unblocked]
|
||||
[std::min(f, FILE_H - f)][rkThem];
|
||||
[d][rkThem];
|
||||
}
|
||||
|
||||
return safety;
|
||||
@@ -284,14 +274,11 @@ Score Entry::do_king_safety(const Position& pos, Square ksq) {
|
||||
|
||||
kingSquares[Us] = ksq;
|
||||
castlingRights[Us] = pos.can_castle(Us);
|
||||
minKingPawnDistance[Us] = 0;
|
||||
int minKingPawnDistance = 0;
|
||||
|
||||
Bitboard pawns = pos.pieces(Us, PAWN);
|
||||
if (pawns)
|
||||
while (!(DistanceRingBB[ksq][minKingPawnDistance[Us]++] & pawns)) {}
|
||||
|
||||
if (relative_rank(Us, ksq) > RANK_4)
|
||||
return make_score(0, -16 * minKingPawnDistance[Us]);
|
||||
while (!(DistanceRingBB[ksq][minKingPawnDistance++] & pawns)) {}
|
||||
|
||||
Value bonus = shelter_storm<Us>(pos, ksq);
|
||||
|
||||
@@ -302,7 +289,7 @@ Score Entry::do_king_safety(const Position& pos, Square ksq) {
|
||||
if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::right))
|
||||
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));
|
||||
|
||||
return make_score(bonus, -16 * minKingPawnDistance[Us]);
|
||||
return make_score(bonus, -16 * minKingPawnDistance);
|
||||
}
|
||||
|
||||
// Explicit template instantiation
|
||||
|
||||
+11
-5
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -35,7 +36,10 @@ struct Entry {
|
||||
Score pawns_score() const { return score; }
|
||||
Bitboard pawn_attacks(Color c) const { return pawnAttacks[c]; }
|
||||
Bitboard passed_pawns(Color c) const { return passedPawns[c]; }
|
||||
int pawn_span(Color c) const { return pawnSpan[c]; }
|
||||
Bitboard pawn_attacks_span(Color c) const { return pawnAttacksSpan[c]; }
|
||||
int weak_unopposed(Color c) const { return weakUnopposed[c]; }
|
||||
int pawn_asymmetry() const { return asymmetry; }
|
||||
int open_files() const { return openFiles; }
|
||||
|
||||
int semiopen_file(Color c, File f) const {
|
||||
return semiopenFiles[c] & (1 << f);
|
||||
@@ -46,11 +50,11 @@ struct Entry {
|
||||
}
|
||||
|
||||
int pawns_on_same_color_squares(Color c, Square s) const {
|
||||
return pawnsOnSquares[c][!!(DarkSquares & s)];
|
||||
return pawnsOnSquares[c][bool(DarkSquares & s)];
|
||||
}
|
||||
|
||||
template<Color Us>
|
||||
Score king_safety(const Position& pos, Square ksq) {
|
||||
Score king_safety(const Position& pos, Square ksq) {
|
||||
return kingSquares[Us] == ksq && castlingRights[Us] == pos.can_castle(Us)
|
||||
? kingSafety[Us] : (kingSafety[Us] = do_king_safety<Us>(pos, ksq));
|
||||
}
|
||||
@@ -65,13 +69,15 @@ struct Entry {
|
||||
Score score;
|
||||
Bitboard passedPawns[COLOR_NB];
|
||||
Bitboard pawnAttacks[COLOR_NB];
|
||||
Bitboard pawnAttacksSpan[COLOR_NB];
|
||||
Square kingSquares[COLOR_NB];
|
||||
Score kingSafety[COLOR_NB];
|
||||
int minKingPawnDistance[COLOR_NB];
|
||||
int weakUnopposed[COLOR_NB];
|
||||
int castlingRights[COLOR_NB];
|
||||
int semiopenFiles[COLOR_NB];
|
||||
int pawnSpan[COLOR_NB];
|
||||
int pawnsOnSquares[COLOR_NB][COLOR_NB]; // [color][light/dark squares]
|
||||
int asymmetry;
|
||||
int openFiles;
|
||||
};
|
||||
|
||||
typedef HashTable<Entry, 16384> Table;
|
||||
|
||||
-116
@@ -1,116 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PLATFORM_H_INCLUDED
|
||||
#define PLATFORM_H_INCLUDED
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
||||
// Disable some silly and noisy warnings from MSVC compiler
|
||||
#pragma warning(disable: 4127) // Conditional expression is constant
|
||||
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
|
||||
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
|
||||
#pragma warning(disable: 4996) // Function _ftime() may be unsafe
|
||||
|
||||
// MSVC does not support <inttypes.h>
|
||||
typedef signed __int8 int8_t;
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef signed __int16 int16_t;
|
||||
typedef unsigned __int16 uint16_t;
|
||||
typedef signed __int32 int32_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef signed __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
|
||||
#else
|
||||
# include <inttypes.h>
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32 // Linux - Unix
|
||||
|
||||
# include <sys/time.h>
|
||||
|
||||
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;
|
||||
typedef pthread_cond_t WaitCondition;
|
||||
typedef pthread_t NativeHandle;
|
||||
typedef void*(*pt_start_fn)(void*);
|
||||
|
||||
# define lock_init(x) pthread_mutex_init(&(x), NULL)
|
||||
# define lock_grab(x) pthread_mutex_lock(&(x))
|
||||
# define lock_release(x) pthread_mutex_unlock(&(x))
|
||||
# define lock_destroy(x) pthread_mutex_destroy(&(x))
|
||||
# define cond_destroy(x) pthread_cond_destroy(&(x))
|
||||
# define cond_init(x) pthread_cond_init(&(x), NULL)
|
||||
# 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_join(x) pthread_join(x, NULL)
|
||||
|
||||
#else // Windows and MinGW
|
||||
|
||||
# include <sys/timeb.h>
|
||||
|
||||
inline int64_t system_time_to_msec() {
|
||||
_timeb t;
|
||||
_ftime(&t);
|
||||
return t.time * 1000LL + t.millitm;
|
||||
}
|
||||
|
||||
#ifndef NOMINMAX
|
||||
# define NOMINMAX // disable macros min() and max()
|
||||
#endif
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#undef WIN32_LEAN_AND_MEAN
|
||||
#undef NOMINMAX
|
||||
|
||||
// We use critical sections on Windows to support Windows XP and older versions.
|
||||
// Unfortunately, cond_wait() is racy between lock_release() and WaitForSingleObject()
|
||||
// but apart from this they have the same speed performance of SRW locks.
|
||||
typedef CRITICAL_SECTION Lock;
|
||||
typedef HANDLE WaitCondition;
|
||||
typedef HANDLE NativeHandle;
|
||||
|
||||
// On Windows 95 and 98 parameter lpThreadId may not be null
|
||||
inline DWORD* dwWin9xKludge() { static DWORD dw; return &dw; }
|
||||
|
||||
# define lock_init(x) InitializeCriticalSection(&(x))
|
||||
# define lock_grab(x) EnterCriticalSection(&(x))
|
||||
# define lock_release(x) LeaveCriticalSection(&(x))
|
||||
# define lock_destroy(x) DeleteCriticalSection(&(x))
|
||||
# define cond_init(x) { x = CreateEvent(0, FALSE, FALSE, 0); }
|
||||
# define cond_destroy(x) CloseHandle(x)
|
||||
# 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()))
|
||||
# define thread_join(x) { WaitForSingleObject(x, INFINITE); CloseHandle(x); }
|
||||
|
||||
#endif
|
||||
|
||||
#endif // #ifndef PLATFORM_H_INCLUDED
|
||||
+370
-437
File diff suppressed because it is too large
Load Diff
+107
-120
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -21,28 +22,13 @@
|
||||
#define POSITION_H_INCLUDED
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef> // For offsetof()
|
||||
#include <deque>
|
||||
#include <memory> // For std::unique_ptr
|
||||
#include <string>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "types.h"
|
||||
|
||||
class Position;
|
||||
struct Thread;
|
||||
|
||||
/// CheckInfo struct is initialized at c'tor time and keeps info used to detect
|
||||
/// if a move gives check.
|
||||
|
||||
struct CheckInfo {
|
||||
|
||||
explicit CheckInfo(const Position&);
|
||||
|
||||
Bitboard dcCandidates;
|
||||
Bitboard pinned;
|
||||
Bitboard checkSq[PIECE_TYPE_NB];
|
||||
Square ksq;
|
||||
};
|
||||
|
||||
|
||||
/// 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
|
||||
@@ -60,40 +46,40 @@ struct StateInfo {
|
||||
Score psq;
|
||||
Square epSquare;
|
||||
|
||||
// Not copied when making a move
|
||||
// Not copied when making a move (will be recomputed anyhow)
|
||||
Key key;
|
||||
Bitboard checkersBB;
|
||||
PieceType capturedType;
|
||||
Piece capturedPiece;
|
||||
StateInfo* previous;
|
||||
Bitboard blockersForKing[COLOR_NB];
|
||||
Bitboard pinnersForKing[COLOR_NB];
|
||||
Bitboard checkSquares[PIECE_TYPE_NB];
|
||||
};
|
||||
|
||||
|
||||
/// When making a move the current StateInfo up to 'key' excluded is copied to
|
||||
/// the new one. Here we calculate the quad words (64 bit) needed to be copied.
|
||||
const size_t StateCopySize64 = offsetof(StateInfo, key) / sizeof(uint64_t) + 1;
|
||||
/// A list to keep track of the position states along the setup moves (from the
|
||||
/// start position to the position just before the search starts). Needed by
|
||||
/// 'draw by repetition' detection. Use a std::deque because pointers to
|
||||
/// elements are not invalidated upon list resizing.
|
||||
typedef std::unique_ptr<std::deque<StateInfo>> StateListPtr;
|
||||
|
||||
|
||||
/// Position class stores information regarding the board representation as
|
||||
/// pieces, side to move, hash keys, castling info, etc. Important methods are
|
||||
/// do_move() and undo_move(), used by the search to update node info when
|
||||
/// traversing the search tree.
|
||||
class Thread;
|
||||
|
||||
class Position {
|
||||
|
||||
friend std::ostream& operator<<(std::ostream&, const Position&);
|
||||
|
||||
Position(const Position&); // Disable the default copy constructor
|
||||
|
||||
public:
|
||||
static void init();
|
||||
|
||||
Position() {} // To define the global object RootPos
|
||||
Position(const Position& pos, Thread* th) { *this = pos; thisThread = th; }
|
||||
Position(const std::string& f, bool c960, Thread* th) { set(f, c960, th); }
|
||||
Position& operator=(const Position&); // To assign RootPos from UCI
|
||||
Position() = default;
|
||||
Position(const Position&) = delete;
|
||||
Position& operator=(const Position&) = delete;
|
||||
|
||||
// FEN string input/output
|
||||
void set(const std::string& fenStr, bool isChess960, Thread* th);
|
||||
Position& set(const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th);
|
||||
Position& set(const std::string& code, Color c, StateInfo* si);
|
||||
const std::string fen() const;
|
||||
|
||||
// Position representation
|
||||
@@ -104,11 +90,12 @@ public:
|
||||
Bitboard pieces(Color c, PieceType pt) const;
|
||||
Bitboard pieces(Color c, PieceType pt1, PieceType pt2) const;
|
||||
Piece piece_on(Square s) const;
|
||||
Square king_square(Color c) const;
|
||||
Square ep_square() const;
|
||||
bool empty(Square s) const;
|
||||
template<PieceType Pt> int count(Color c) const;
|
||||
template<PieceType Pt> const Square* list(Color c) const;
|
||||
template<PieceType Pt> int count() const;
|
||||
template<PieceType Pt> const Square* squares(Color c) const;
|
||||
template<PieceType Pt> Square square(Color c) const;
|
||||
|
||||
// Castling
|
||||
int can_castle(Color c) const;
|
||||
@@ -120,90 +107,84 @@ public:
|
||||
Bitboard checkers() const;
|
||||
Bitboard discovered_check_candidates() const;
|
||||
Bitboard pinned_pieces(Color c) const;
|
||||
Bitboard check_squares(PieceType pt) const;
|
||||
|
||||
// Attacks to/from a given square
|
||||
Bitboard attackers_to(Square s) const;
|
||||
Bitboard attackers_to(Square s, Bitboard occupied) const;
|
||||
Bitboard attacks_from(Piece pc, Square s) const;
|
||||
Bitboard attacks_from(PieceType pt, Square s) const;
|
||||
template<PieceType> Bitboard attacks_from(Square s) const;
|
||||
template<PieceType> Bitboard attacks_from(Square s, Color c) const;
|
||||
Bitboard slider_blockers(Bitboard sliders, Square s, Bitboard& pinners) const;
|
||||
|
||||
// Properties of moves
|
||||
bool legal(Move m, Bitboard pinned) const;
|
||||
bool legal(Move m) 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 gives_check(Move m) const;
|
||||
bool advanced_pawn_push(Move m) const;
|
||||
Piece moved_piece(Move m) const;
|
||||
PieceType captured_piece_type() const;
|
||||
Piece captured_piece() const;
|
||||
|
||||
// Piece specific
|
||||
bool pawn_passed(Color c, Square s) const;
|
||||
bool pawn_on_7th(Color c) const;
|
||||
bool opposite_bishops() const;
|
||||
|
||||
// Doing and undoing moves
|
||||
void do_move(Move m, StateInfo& st);
|
||||
void do_move(Move m, StateInfo& st, const CheckInfo& ci, bool moveIsCheck);
|
||||
void do_move(Move m, StateInfo& newSt);
|
||||
void do_move(Move m, StateInfo& newSt, bool givesCheck);
|
||||
void undo_move(Move m);
|
||||
void do_null_move(StateInfo& st);
|
||||
void do_null_move(StateInfo& newSt);
|
||||
void undo_null_move();
|
||||
|
||||
// Static exchange evaluation
|
||||
Value see(Move m) const;
|
||||
Value see_sign(Move m) const;
|
||||
// Static Exchange Evaluation
|
||||
bool see_ge(Move m, Value threshold = VALUE_ZERO) const;
|
||||
|
||||
// Accessing hash keys
|
||||
Key key() const;
|
||||
Key key_after(Move m) const;
|
||||
Key exclusion_key() const;
|
||||
Key material_key() const;
|
||||
Key pawn_key() const;
|
||||
|
||||
// Other properties of the position
|
||||
Color side_to_move() const;
|
||||
Phase game_phase() const;
|
||||
int game_ply() const;
|
||||
bool is_chess960() const;
|
||||
Thread* this_thread() const;
|
||||
uint64_t nodes_searched() const;
|
||||
void set_nodes_searched(uint64_t n);
|
||||
bool is_draw() const;
|
||||
bool is_draw(int ply) const;
|
||||
int rule50_count() const;
|
||||
Score psq_score() const;
|
||||
Value non_pawn_material(Color c) const;
|
||||
Value non_pawn_material() const;
|
||||
|
||||
// Position consistency check, for debugging
|
||||
bool pos_is_ok(int* step = NULL) const;
|
||||
bool pos_is_ok() const;
|
||||
void flip();
|
||||
|
||||
private:
|
||||
// Initialization helpers (used while setting up a position)
|
||||
void clear();
|
||||
void set_castling_right(Color c, Square rfrom);
|
||||
void set_state(StateInfo* si) const;
|
||||
void set_check_info(StateInfo* si) const;
|
||||
|
||||
// Other helpers
|
||||
Bitboard check_blockers(Color c, Color kingColor) 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);
|
||||
void put_piece(Piece pc, Square s);
|
||||
void remove_piece(Piece pc, Square s);
|
||||
void move_piece(Piece pc, Square from, Square to);
|
||||
template<bool Do>
|
||||
void do_castling(Square from, Square& to, Square& rfrom, Square& rto);
|
||||
void do_castling(Color us, Square from, Square& to, Square& rfrom, Square& rto);
|
||||
|
||||
// Data members
|
||||
Piece board[SQUARE_NB];
|
||||
Bitboard byTypeBB[PIECE_TYPE_NB];
|
||||
Bitboard byColorBB[COLOR_NB];
|
||||
int pieceCount[COLOR_NB][PIECE_TYPE_NB];
|
||||
Square pieceList[COLOR_NB][PIECE_TYPE_NB][16];
|
||||
int pieceCount[PIECE_NB];
|
||||
Square pieceList[PIECE_NB][16];
|
||||
int index[SQUARE_NB];
|
||||
int castlingRightsMask[SQUARE_NB];
|
||||
Square castlingRookSquare[CASTLING_RIGHT_NB];
|
||||
Bitboard castlingPath[CASTLING_RIGHT_NB];
|
||||
StateInfo startState;
|
||||
uint64_t nodes;
|
||||
int gamePly;
|
||||
Color sideToMove;
|
||||
Thread* thisThread;
|
||||
@@ -211,6 +192,8 @@ private:
|
||||
bool chess960;
|
||||
};
|
||||
|
||||
extern std::ostream& operator<<(std::ostream& os, const Position& pos);
|
||||
|
||||
inline Color Position::side_to_move() const {
|
||||
return sideToMove;
|
||||
}
|
||||
@@ -252,15 +235,20 @@ inline Bitboard Position::pieces(Color c, PieceType pt1, PieceType pt2) const {
|
||||
}
|
||||
|
||||
template<PieceType Pt> inline int Position::count(Color c) const {
|
||||
return pieceCount[c][Pt];
|
||||
return pieceCount[make_piece(c, Pt)];
|
||||
}
|
||||
|
||||
template<PieceType Pt> inline const Square* Position::list(Color c) const {
|
||||
return pieceList[c][Pt];
|
||||
template<PieceType Pt> inline int Position::count() const {
|
||||
return pieceCount[make_piece(WHITE, Pt)] + pieceCount[make_piece(BLACK, Pt)];
|
||||
}
|
||||
|
||||
inline Square Position::king_square(Color c) const {
|
||||
return pieceList[c][KING][0];
|
||||
template<PieceType Pt> inline const Square* Position::squares(Color c) const {
|
||||
return pieceList[make_piece(c, Pt)];
|
||||
}
|
||||
|
||||
template<PieceType Pt> inline Square Position::square(Color c) const {
|
||||
assert(pieceCount[make_piece(c, Pt)] == 1);
|
||||
return pieceList[make_piece(c, Pt)][0];
|
||||
}
|
||||
|
||||
inline Square Position::ep_square() const {
|
||||
@@ -285,18 +273,19 @@ inline Square Position::castling_rook_square(CastlingRight cr) const {
|
||||
|
||||
template<PieceType Pt>
|
||||
inline Bitboard Position::attacks_from(Square s) const {
|
||||
assert(Pt != PAWN);
|
||||
return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, byTypeBB[ALL_PIECES])
|
||||
: Pt == QUEEN ? attacks_from<ROOK>(s) | attacks_from<BISHOP>(s)
|
||||
: StepAttacksBB[Pt][s];
|
||||
: PseudoAttacks[Pt][s];
|
||||
}
|
||||
|
||||
template<>
|
||||
inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
|
||||
return StepAttacksBB[make_piece(c, PAWN)][s];
|
||||
return PawnAttacks[c][s];
|
||||
}
|
||||
|
||||
inline Bitboard Position::attacks_from(Piece pc, Square s) const {
|
||||
return attacks_bb(pc, s, byTypeBB[ALL_PIECES]);
|
||||
inline Bitboard Position::attacks_from(PieceType pt, Square s) const {
|
||||
return attacks_bb(pt, s, byTypeBB[ALL_PIECES]);
|
||||
}
|
||||
|
||||
inline Bitboard Position::attackers_to(Square s) const {
|
||||
@@ -308,11 +297,15 @@ inline Bitboard Position::checkers() const {
|
||||
}
|
||||
|
||||
inline Bitboard Position::discovered_check_candidates() const {
|
||||
return check_blockers(sideToMove, ~sideToMove);
|
||||
return st->blockersForKing[~sideToMove] & pieces(sideToMove);
|
||||
}
|
||||
|
||||
inline Bitboard Position::pinned_pieces(Color c) const {
|
||||
return check_blockers(c, c);
|
||||
return st->blockersForKing[c] & pieces(c);
|
||||
}
|
||||
|
||||
inline Bitboard Position::check_squares(PieceType pt) const {
|
||||
return st->checkSquares[pt];
|
||||
}
|
||||
|
||||
inline bool Position::pawn_passed(Color c, Square s) const {
|
||||
@@ -344,6 +337,10 @@ inline Value Position::non_pawn_material(Color c) const {
|
||||
return st->nonPawnMaterial[c];
|
||||
}
|
||||
|
||||
inline Value Position::non_pawn_material() const {
|
||||
return st->nonPawnMaterial[WHITE] + st->nonPawnMaterial[BLACK];
|
||||
}
|
||||
|
||||
inline int Position::game_ply() const {
|
||||
return gamePly;
|
||||
}
|
||||
@@ -352,22 +349,10 @@ inline int Position::rule50_count() const {
|
||||
return st->rule50;
|
||||
}
|
||||
|
||||
inline uint64_t Position::nodes_searched() const {
|
||||
return nodes;
|
||||
}
|
||||
|
||||
inline void Position::set_nodes_searched(uint64_t n) {
|
||||
nodes = n;
|
||||
}
|
||||
|
||||
inline bool Position::opposite_bishops() const {
|
||||
return pieceCount[WHITE][BISHOP] == 1
|
||||
&& pieceCount[BLACK][BISHOP] == 1
|
||||
&& opposite_colors(pieceList[WHITE][BISHOP][0], pieceList[BLACK][BISHOP][0]);
|
||||
}
|
||||
|
||||
inline bool Position::pawn_on_7th(Color c) const {
|
||||
return pieces(c, PAWN) & rank_bb(relative_rank(c, RANK_7));
|
||||
return pieceCount[W_BISHOP] == 1
|
||||
&& pieceCount[B_BISHOP] == 1
|
||||
&& opposite_colors(square<BISHOP>(WHITE), square<BISHOP>(BLACK));
|
||||
}
|
||||
|
||||
inline bool Position::is_chess960() const {
|
||||
@@ -375,66 +360,68 @@ inline bool Position::is_chess960() const {
|
||||
}
|
||||
|
||||
inline bool Position::capture_or_promotion(Move m) const {
|
||||
|
||||
assert(is_ok(m));
|
||||
return type_of(m) != NORMAL ? type_of(m) != CASTLING : !empty(to_sq(m));
|
||||
}
|
||||
|
||||
inline bool Position::capture(Move m) const {
|
||||
|
||||
// Castling is encoded as "king captures the rook"
|
||||
assert(is_ok(m));
|
||||
// Castling is encoded as "king captures rook"
|
||||
return (!empty(to_sq(m)) && type_of(m) != CASTLING) || type_of(m) == ENPASSANT;
|
||||
}
|
||||
|
||||
inline PieceType Position::captured_piece_type() const {
|
||||
return st->capturedType;
|
||||
inline Piece Position::captured_piece() const {
|
||||
return st->capturedPiece;
|
||||
}
|
||||
|
||||
inline Thread* Position::this_thread() const {
|
||||
return thisThread;
|
||||
}
|
||||
|
||||
inline void Position::put_piece(Square s, Color c, PieceType pt) {
|
||||
inline void Position::put_piece(Piece pc, Square s) {
|
||||
|
||||
board[s] = make_piece(c, pt);
|
||||
board[s] = pc;
|
||||
byTypeBB[ALL_PIECES] |= s;
|
||||
byTypeBB[pt] |= s;
|
||||
byColorBB[c] |= s;
|
||||
index[s] = pieceCount[c][pt]++;
|
||||
pieceList[c][pt][index[s]] = s;
|
||||
pieceCount[c][ALL_PIECES]++;
|
||||
byTypeBB[type_of(pc)] |= s;
|
||||
byColorBB[color_of(pc)] |= s;
|
||||
index[s] = pieceCount[pc]++;
|
||||
pieceList[pc][index[s]] = s;
|
||||
pieceCount[make_piece(color_of(pc), ALL_PIECES)]++;
|
||||
}
|
||||
|
||||
inline void Position::move_piece(Square from, Square to, Color c, PieceType pt) {
|
||||
inline void Position::remove_piece(Piece pc, Square s) {
|
||||
|
||||
// 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 invariant to a do_move() + undo_move() sequence.
|
||||
byTypeBB[ALL_PIECES] ^= s;
|
||||
byTypeBB[type_of(pc)] ^= s;
|
||||
byColorBB[color_of(pc)] ^= s;
|
||||
/* board[s] = NO_PIECE; Not needed, overwritten by the capturing one */
|
||||
Square lastSquare = pieceList[pc][--pieceCount[pc]];
|
||||
index[lastSquare] = index[s];
|
||||
pieceList[pc][index[lastSquare]] = lastSquare;
|
||||
pieceList[pc][pieceCount[pc]] = SQ_NONE;
|
||||
pieceCount[make_piece(color_of(pc), ALL_PIECES)]--;
|
||||
}
|
||||
|
||||
inline void Position::move_piece(Piece pc, Square from, Square to) {
|
||||
|
||||
// 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;
|
||||
byTypeBB[type_of(pc)] ^= from_to_bb;
|
||||
byColorBB[color_of(pc)] ^= from_to_bb;
|
||||
board[from] = NO_PIECE;
|
||||
board[to] = make_piece(c, pt);
|
||||
board[to] = pc;
|
||||
index[to] = index[from];
|
||||
pieceList[c][pt][index[to]] = to;
|
||||
pieceList[pc][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, overwritten by the capturing one */
|
||||
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;
|
||||
pieceCount[c][ALL_PIECES]--;
|
||||
inline void Position::do_move(Move m, StateInfo& newSt) {
|
||||
do_move(m, newSt, gives_check(m));
|
||||
}
|
||||
|
||||
#endif // #ifndef POSITION_H_INCLUDED
|
||||
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
Value PieceValue[PHASE_NB][PIECE_NB] = {
|
||||
{ VALUE_ZERO, PawnValueMg, KnightValueMg, BishopValueMg, RookValueMg, QueenValueMg },
|
||||
{ VALUE_ZERO, PawnValueEg, KnightValueEg, BishopValueEg, RookValueEg, QueenValueEg }
|
||||
};
|
||||
|
||||
namespace PSQT {
|
||||
|
||||
#define S(mg, eg) make_score(mg, eg)
|
||||
|
||||
// Bonus[PieceType][Square / 2] contains Piece-Square scores. For each piece
|
||||
// type on a given square a (middlegame, endgame) score pair is assigned. Table
|
||||
// is defined for files A..D and white side: it is symmetric for black side and
|
||||
// second half of the files.
|
||||
const Score Bonus[][RANK_NB][int(FILE_NB) / 2] = {
|
||||
{ },
|
||||
{ // Pawn
|
||||
{ S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0) },
|
||||
{ S(-11, 7), S( 6,-4), S( 7, 8), S( 3,-2) },
|
||||
{ S(-18,-4), S( -2,-5), S( 19, 5), S(24, 4) },
|
||||
{ S(-17, 3), S( -9, 3), S( 20,-8), S(35,-3) },
|
||||
{ S( -6, 8), S( 5, 9), S( 3, 7), S(21,-6) },
|
||||
{ S( -6, 8), S( -8,-5), S( -6, 2), S(-2, 4) },
|
||||
{ S( -4, 3), S( 20,-9), S( -8, 1), S(-4,18) }
|
||||
},
|
||||
{ // Knight
|
||||
{ S(-161,-105), S(-96,-82), S(-80,-46), S(-73,-14) },
|
||||
{ S( -83, -69), S(-43,-54), S(-21,-17), S(-10, 9) },
|
||||
{ S( -71, -50), S(-22,-39), S( 0, -7), S( 9, 28) },
|
||||
{ S( -25, -41), S( 18,-25), S( 43, 6), S( 47, 38) },
|
||||
{ S( -26, -46), S( 16,-25), S( 38, 3), S( 50, 40) },
|
||||
{ S( -11, -54), S( 37,-38), S( 56, -7), S( 65, 27) },
|
||||
{ S( -63, -65), S(-19,-50), S( 5,-24), S( 14, 13) },
|
||||
{ S(-195,-109), S(-67,-89), S(-42,-50), S(-29,-13) }
|
||||
},
|
||||
{ // Bishop
|
||||
{ S(-44,-58), S(-13,-31), S(-25,-37), S(-34,-19) },
|
||||
{ S(-20,-34), S( 20, -9), S( 12,-14), S( 1, 4) },
|
||||
{ S( -9,-23), S( 27, 0), S( 21, -3), S( 11, 16) },
|
||||
{ S(-11,-26), S( 28, -3), S( 21, -5), S( 10, 16) },
|
||||
{ S(-11,-26), S( 27, -4), S( 16, -7), S( 9, 14) },
|
||||
{ S(-17,-24), S( 16, -2), S( 12, 0), S( 2, 13) },
|
||||
{ S(-23,-34), S( 17,-10), S( 6,-12), S( -2, 6) },
|
||||
{ S(-35,-55), S(-11,-32), S(-19,-36), S(-29,-17) }
|
||||
},
|
||||
{ // Rook
|
||||
{ S(-25, 0), S(-16, 0), S(-16, 0), S(-9, 0) },
|
||||
{ S(-21, 0), S( -8, 0), S( -3, 0), S( 0, 0) },
|
||||
{ S(-21, 0), S( -9, 0), S( -4, 0), S( 2, 0) },
|
||||
{ S(-22, 0), S( -6, 0), S( -1, 0), S( 2, 0) },
|
||||
{ S(-22, 0), S( -7, 0), S( 0, 0), S( 1, 0) },
|
||||
{ S(-21, 0), S( -7, 0), S( 0, 0), S( 2, 0) },
|
||||
{ S(-12, 0), S( 4, 0), S( 8, 0), S(12, 0) },
|
||||
{ S(-23, 0), S(-15, 0), S(-11, 0), S(-5, 0) }
|
||||
},
|
||||
{ // Queen
|
||||
{ S( 0,-71), S(-4,-56), S(-3,-42), S(-1,-29) },
|
||||
{ S(-4,-56), S( 6,-30), S( 9,-21), S( 8, -5) },
|
||||
{ S(-2,-39), S( 6,-17), S( 9, -8), S( 9, 5) },
|
||||
{ S(-1,-29), S( 8, -5), S(10, 9), S( 7, 19) },
|
||||
{ S(-3,-27), S( 9, -5), S( 8, 10), S( 7, 21) },
|
||||
{ S(-2,-40), S( 6,-16), S( 8,-10), S(10, 3) },
|
||||
{ S(-2,-55), S( 7,-30), S( 7,-21), S( 6, -6) },
|
||||
{ S(-1,-74), S(-4,-55), S(-1,-43), S( 0,-30) }
|
||||
},
|
||||
{ // King
|
||||
{ S(267, 0), S(320, 48), S(270, 75), S(195, 84) },
|
||||
{ S(264, 43), S(304, 92), S(238,143), S(180,132) },
|
||||
{ S(200, 83), S(245,138), S(176,167), S(110,165) },
|
||||
{ S(177,106), S(185,169), S(148,169), S(110,179) },
|
||||
{ S(149,108), S(177,163), S(115,200), S( 66,203) },
|
||||
{ S(118, 95), S(159,155), S( 84,176), S( 41,174) },
|
||||
{ S( 87, 50), S(128, 99), S( 63,122), S( 20,139) },
|
||||
{ S( 63, 9), S( 88, 55), S( 47, 80), S( 0, 90) }
|
||||
}
|
||||
};
|
||||
|
||||
#undef S
|
||||
|
||||
Score psq[PIECE_NB][SQUARE_NB];
|
||||
|
||||
// init() initializes piece-square tables: the white halves of the tables are
|
||||
// copied from Bonus[] adding the piece value, then the black halves of the
|
||||
// tables are initialized by flipping and changing the sign of the white scores.
|
||||
void init() {
|
||||
|
||||
for (Piece pc = W_PAWN; pc <= W_KING; ++pc)
|
||||
{
|
||||
PieceValue[MG][~pc] = PieceValue[MG][pc];
|
||||
PieceValue[EG][~pc] = PieceValue[EG][pc];
|
||||
|
||||
Score v = make_score(PieceValue[MG][pc], PieceValue[EG][pc]);
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||
{
|
||||
File f = std::min(file_of(s), ~file_of(s));
|
||||
psq[ pc][ s] = v + Bonus[pc][rank_of(s)][f];
|
||||
psq[~pc][~s] = -psq[pc][s];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace PSQT
|
||||
@@ -1,98 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PSQTAB_H_INCLUDED
|
||||
#define PSQTAB_H_INCLUDED
|
||||
|
||||
#include "types.h"
|
||||
|
||||
#define S(mg, eg) make_score(mg, eg)
|
||||
|
||||
|
||||
/// PSQT[PieceType][Square] contains Piece-Square scores. For each piece type on
|
||||
/// a given square a (middlegame, endgame) score pair is assigned. PSQT is defined
|
||||
/// for the white side and the tables are symmetric for the black side.
|
||||
|
||||
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(-20, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S(-20, 0),
|
||||
S(-20, 0), S( 0, 0), S(10, 0), S(20, 0), S(20, 0), S(10, 0), S( 0, 0), S(-20, 0),
|
||||
S(-20, 0), S( 0, 0), S(20, 0), S(40, 0), S(40, 0), S(20, 0), S( 0, 0), S(-20, 0),
|
||||
S(-20, 0), S( 0, 0), S(10, 0), S(20, 0), S(20, 0), S(10, 0), S( 0, 0), S(-20, 0),
|
||||
S(-20, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S(-20, 0),
|
||||
S(-20, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S(-20, 0),
|
||||
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
|
||||
S(-144,-98), S(-109,-83), S(-85,-51), S(-73,-16), S(-73,-16), S(-85,-51), S(-109,-83), S(-144,-98),
|
||||
S( -88,-68), S( -43,-53), S(-19,-21), S( -7, 14), S( -7, 14), S(-19,-21), S( -43,-53), S( -88,-68),
|
||||
S( -69,-53), S( -24,-38), S( 0, -6), S( 12, 29), S( 12, 29), S( 0, -6), S( -24,-38), S( -69,-53),
|
||||
S( -28,-42), S( 17,-27), S( 41, 5), S( 53, 40), S( 53, 40), S( 41, 5), S( 17,-27), S( -28,-42),
|
||||
S( -30,-42), S( 15,-27), S( 39, 5), S( 51, 40), S( 51, 40), S( 39, 5), S( 15,-27), S( -30,-42),
|
||||
S( -10,-53), S( 35,-38), S( 59, -6), S( 71, 29), S( 71, 29), S( 59, -6), S( 35,-38), S( -10,-53),
|
||||
S( -64,-68), S( -19,-53), S( 5,-21), S( 17, 14), S( 17, 14), S( 5,-21), S( -19,-53), S( -64,-68),
|
||||
S(-200,-98), S( -65,-83), S(-41,-51), S(-29,-16), S(-29,-16), S(-41,-51), S( -65,-83), S(-200,-98)
|
||||
},
|
||||
{ // Bishop
|
||||
S(-54,-65), S(-27,-42), S(-34,-44), S(-43,-26), S(-43,-26), S(-34,-44), S(-27,-42), S(-54,-65),
|
||||
S(-29,-43), S( 8,-20), S( 1,-22), S( -8, -4), S( -8, -4), S( 1,-22), S( 8,-20), S(-29,-43),
|
||||
S(-20,-33), S( 17,-10), S( 10,-12), S( 1, 6), S( 1, 6), S( 10,-12), S( 17,-10), S(-20,-33),
|
||||
S(-19,-35), S( 18,-12), S( 11,-14), S( 2, 4), S( 2, 4), S( 11,-14), S( 18,-12), S(-19,-35),
|
||||
S(-22,-35), S( 15,-12), S( 8,-14), S( -1, 4), S( -1, 4), S( 8,-14), S( 15,-12), S(-22,-35),
|
||||
S(-28,-33), S( 9,-10), S( 2,-12), S( -7, 6), S( -7, 6), S( 2,-12), S( 9,-10), S(-28,-33),
|
||||
S(-32,-43), S( 5,-20), S( -2,-22), S(-11, -4), S(-11, -4), S( -2,-22), S( 5,-20), S(-32,-43),
|
||||
S(-49,-65), S(-22,-42), S(-29,-44), S(-38,-26), S(-38,-26), S(-29,-44), S(-22,-42), S(-49,-65)
|
||||
},
|
||||
{ // Rook
|
||||
S(-22, 3), S(-17, 3), S(-12, 3), S(-8, 3), S(-8, 3), S(-12, 3), S(-17, 3), S(-22, 3),
|
||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
||||
S(-11, 3), S( 4, 3), S( 9, 3), S(13, 3), S(13, 3), S( 9, 3), S( 4, 3), S(-11, 3),
|
||||
S(-22, 3), S(-17, 3), S(-12, 3), S(-8, 3), S(-8, 3), S(-12, 3), S(-17, 3), S(-22, 3)
|
||||
},
|
||||
{ // Queen
|
||||
S(-2,-80), S(-2,-54), S(-2,-42), S(-2,-30), S(-2,-30), S(-2,-42), S(-2,-54), S(-2,-80),
|
||||
S(-2,-54), S( 8,-30), S( 8,-18), S( 8, -6), S( 8, -6), S( 8,-18), S( 8,-30), S(-2,-54),
|
||||
S(-2,-42), S( 8,-18), S( 8, -6), S( 8, 6), S( 8, 6), S( 8, -6), S( 8,-18), S(-2,-42),
|
||||
S(-2,-30), S( 8, -6), S( 8, 6), S( 8, 18), S( 8, 18), S( 8, 6), S( 8, -6), S(-2,-30),
|
||||
S(-2,-30), S( 8, -6), S( 8, 6), S( 8, 18), S( 8, 18), S( 8, 6), S( 8, -6), S(-2,-30),
|
||||
S(-2,-42), S( 8,-18), S( 8, -6), S( 8, 6), S( 8, 6), S( 8, -6), S( 8,-18), S(-2,-42),
|
||||
S(-2,-54), S( 8,-30), S( 8,-18), S( 8, -6), S( 8, -6), S( 8,-18), S( 8,-30), S(-2,-54),
|
||||
S(-2,-80), S(-2,-54), S(-2,-42), S(-2,-30), S(-2,-30), S(-2,-42), S(-2,-54), S(-2,-80)
|
||||
},
|
||||
{ // King
|
||||
S(298, 27), S(332, 81), S(273,108), S(225,116), S(225,116), S(273,108), S(332, 81), S(298, 27),
|
||||
S(287, 74), S(321,128), S(262,155), S(214,163), S(214,163), S(262,155), S(321,128), S(287, 74),
|
||||
S(224,111), S(258,165), S(199,192), S(151,200), S(151,200), S(199,192), S(258,165), S(224,111),
|
||||
S(196,135), S(230,189), S(171,216), S(123,224), S(123,224), S(171,216), S(230,189), S(196,135),
|
||||
S(173,135), S(207,189), S(148,216), S(100,224), S(100,224), S(148,216), S(207,189), S(173,135),
|
||||
S(146,111), S(180,165), S(121,192), S( 73,200), S( 73,200), S(121,192), S(180,165), S(146,111),
|
||||
S(119, 74), S(153,128), S( 94,155), S( 46,163), S( 46,163), S( 94,155), S(153,128), S(119, 74),
|
||||
S( 98, 27), S(132, 81), S( 73,108), S( 25,116), S( 25,116), S( 73,108), S(132, 81), S( 98, 27)
|
||||
}
|
||||
};
|
||||
|
||||
#undef S
|
||||
|
||||
#endif // #ifndef PSQTAB_H_INCLUDED
|
||||
+922
-970
File diff suppressed because it is too large
Load Diff
+30
-38
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -20,94 +21,85 @@
|
||||
#ifndef SEARCH_H_INCLUDED
|
||||
#define SEARCH_H_INCLUDED
|
||||
|
||||
#include <memory> // For std::auto_ptr
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "movepick.h"
|
||||
#include "types.h"
|
||||
|
||||
struct SplitPoint;
|
||||
class Position;
|
||||
|
||||
namespace Search {
|
||||
|
||||
/// Threshold used for countermoves based pruning
|
||||
const int CounterMovePruneThreshold = 0;
|
||||
|
||||
|
||||
/// Stack struct keeps track of the information we need to remember from nodes
|
||||
/// shallower and deeper in the tree during the search. Each search thread has
|
||||
/// its own array of Stack objects, indexed by the current ply.
|
||||
|
||||
struct Stack {
|
||||
SplitPoint* splitPoint;
|
||||
Move* pv;
|
||||
PieceToHistory* contHistory;
|
||||
int ply;
|
||||
Move currentMove;
|
||||
Move ttMove;
|
||||
Move excludedMove;
|
||||
Move killers[2];
|
||||
Depth reduction;
|
||||
Value staticEval;
|
||||
bool skipEarlyPruning;
|
||||
int statScore;
|
||||
int moveCount;
|
||||
};
|
||||
|
||||
|
||||
/// RootMove struct is used for moves at the root of the tree. For each root move
|
||||
/// we store a score and a PV (really a refutation in the case of moves which
|
||||
/// fail low). Score is normally set at -VALUE_INFINITE for all non-pv moves.
|
||||
|
||||
struct RootMove {
|
||||
|
||||
RootMove(Move m) : score(-VALUE_INFINITE), previousScore(-VALUE_INFINITE), pv(1, m) {}
|
||||
|
||||
bool operator<(const RootMove& m) const { return score > m.score; } // Ascending sort
|
||||
explicit RootMove(Move m) : pv(1, m) {}
|
||||
bool extract_ponder_from_tt(Position& pos);
|
||||
bool operator==(const Move& m) const { return pv[0] == m; }
|
||||
void insert_pv_in_tt(Position& pos);
|
||||
Move extract_ponder_from_tt(Position& pos);
|
||||
bool operator<(const RootMove& m) const { // Sort in descending order
|
||||
return m.score != score ? m.score < score
|
||||
: m.previousScore < previousScore;
|
||||
}
|
||||
|
||||
Value score;
|
||||
Value previousScore;
|
||||
Value score = -VALUE_INFINITE;
|
||||
Value previousScore = -VALUE_INFINITE;
|
||||
int selDepth = 0;
|
||||
std::vector<Move> pv;
|
||||
};
|
||||
|
||||
typedef std::vector<RootMove> RootMoveVector;
|
||||
typedef std::vector<RootMove> RootMoves;
|
||||
|
||||
|
||||
/// LimitsType struct stores information sent by GUI about available time to
|
||||
/// search the current move, maximum depth/time, if we are in analysis mode or
|
||||
/// if we have to ponder while it's our opponent's turn to move.
|
||||
/// search the current move, maximum depth/time, or if we are in analysis mode.
|
||||
|
||||
struct LimitsType {
|
||||
|
||||
LimitsType() { // Init explicitly due to broken value-initialization of non POD in MSVC
|
||||
nodes = time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = movestogo =
|
||||
depth = movetime = mate = infinite = ponder = 0;
|
||||
nodes = time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] =
|
||||
npmsec = movestogo = depth = movetime = mate = perft = infinite = 0;
|
||||
}
|
||||
|
||||
bool use_time_management() const {
|
||||
return !(mate | movetime | depth | nodes | infinite);
|
||||
return !(mate | movetime | depth | nodes | perft | infinite);
|
||||
}
|
||||
|
||||
std::vector<Move> searchmoves;
|
||||
int time[COLOR_NB], inc[COLOR_NB], movestogo, depth, movetime, mate, infinite, ponder;
|
||||
int time[COLOR_NB], inc[COLOR_NB], npmsec, movestogo, depth,
|
||||
movetime, mate, perft, infinite;
|
||||
int64_t nodes;
|
||||
TimePoint startTime;
|
||||
};
|
||||
|
||||
/// The SignalsType struct stores volatile flags updated during the search
|
||||
/// typically in an async fashion e.g. to stop the search by the GUI.
|
||||
|
||||
struct SignalsType {
|
||||
bool stop, stopOnPonderhit, firstRootMove, failedLowAtRoot;
|
||||
};
|
||||
|
||||
typedef std::auto_ptr<std::stack<StateInfo> > StateStackPtr;
|
||||
|
||||
extern volatile SignalsType Signals;
|
||||
extern LimitsType Limits;
|
||||
extern RootMoveVector RootMoves;
|
||||
extern Position RootPos;
|
||||
extern Time::point SearchTime;
|
||||
extern StateStackPtr SetupStates;
|
||||
|
||||
void init();
|
||||
void think();
|
||||
template<bool Root> uint64_t perft(Position& pos, Depth depth);
|
||||
void clear();
|
||||
|
||||
} // namespace Search
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,169 +0,0 @@
|
||||
/*
|
||||
Copyright (c) 2011-2013 Ronald de Man
|
||||
*/
|
||||
|
||||
#ifndef TBCORE_H
|
||||
#define TBCORE_H
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <pthread.h>
|
||||
#define SEP_CHAR ':'
|
||||
#define FD int
|
||||
#define FD_ERR -1
|
||||
#else
|
||||
#include <windows.h>
|
||||
#define SEP_CHAR ';'
|
||||
#define FD HANDLE
|
||||
#define FD_ERR INVALID_HANDLE_VALUE
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
#define LOCK_T pthread_mutex_t
|
||||
#define LOCK_INIT(x) pthread_mutex_init(&(x), NULL)
|
||||
#define LOCK(x) pthread_mutex_lock(&(x))
|
||||
#define UNLOCK(x) pthread_mutex_unlock(&(x))
|
||||
#else
|
||||
#define LOCK_T HANDLE
|
||||
#define LOCK_INIT(x) do { x = CreateMutex(NULL, FALSE, NULL); } while (0)
|
||||
#define LOCK(x) WaitForSingleObject(x, INFINITE)
|
||||
#define UNLOCK(x) ReleaseMutex(x)
|
||||
#endif
|
||||
|
||||
#ifndef _MSC_VER
|
||||
#define BSWAP32(v) __builtin_bswap32(v)
|
||||
#define BSWAP64(v) __builtin_bswap64(v)
|
||||
#else
|
||||
#define BSWAP32(v) _byteswap_ulong(v)
|
||||
#define BSWAP64(v) _byteswap_uint64(v)
|
||||
#endif
|
||||
|
||||
#define WDLSUFFIX ".rtbw"
|
||||
#define DTZSUFFIX ".rtbz"
|
||||
#define WDLDIR "RTBWDIR"
|
||||
#define DTZDIR "RTBZDIR"
|
||||
#define TBPIECES 6
|
||||
|
||||
typedef unsigned long long uint64;
|
||||
typedef unsigned int uint32;
|
||||
typedef unsigned char ubyte;
|
||||
typedef unsigned short ushort;
|
||||
|
||||
const ubyte WDL_MAGIC[4] = { 0x71, 0xe8, 0x23, 0x5d };
|
||||
const ubyte DTZ_MAGIC[4] = { 0xd7, 0x66, 0x0c, 0xa5 };
|
||||
|
||||
#define TBHASHBITS 10
|
||||
|
||||
struct TBHashEntry;
|
||||
|
||||
typedef uint64 base_t;
|
||||
|
||||
struct PairsData {
|
||||
char *indextable;
|
||||
ushort *sizetable;
|
||||
ubyte *data;
|
||||
ushort *offset;
|
||||
ubyte *symlen;
|
||||
ubyte *sympat;
|
||||
int blocksize;
|
||||
int idxbits;
|
||||
int min_len;
|
||||
base_t base[1]; // C++ complains about base[]...
|
||||
};
|
||||
|
||||
struct TBEntry {
|
||||
char *data;
|
||||
uint64 key;
|
||||
uint64 mapping;
|
||||
ubyte ready;
|
||||
ubyte num;
|
||||
ubyte symmetric;
|
||||
ubyte has_pawns;
|
||||
}
|
||||
#ifndef _WIN32
|
||||
__attribute__((__may_alias__))
|
||||
#endif
|
||||
;
|
||||
|
||||
struct TBEntry_piece {
|
||||
char *data;
|
||||
uint64 key;
|
||||
uint64 mapping;
|
||||
ubyte ready;
|
||||
ubyte num;
|
||||
ubyte symmetric;
|
||||
ubyte has_pawns;
|
||||
ubyte enc_type;
|
||||
struct PairsData *precomp[2];
|
||||
int factor[2][TBPIECES];
|
||||
ubyte pieces[2][TBPIECES];
|
||||
ubyte norm[2][TBPIECES];
|
||||
};
|
||||
|
||||
struct TBEntry_pawn {
|
||||
char *data;
|
||||
uint64 key;
|
||||
uint64 mapping;
|
||||
ubyte ready;
|
||||
ubyte num;
|
||||
ubyte symmetric;
|
||||
ubyte has_pawns;
|
||||
ubyte pawns[2];
|
||||
struct {
|
||||
struct PairsData *precomp[2];
|
||||
int factor[2][TBPIECES];
|
||||
ubyte pieces[2][TBPIECES];
|
||||
ubyte norm[2][TBPIECES];
|
||||
} file[4];
|
||||
};
|
||||
|
||||
struct DTZEntry_piece {
|
||||
char *data;
|
||||
uint64 key;
|
||||
uint64 mapping;
|
||||
ubyte ready;
|
||||
ubyte num;
|
||||
ubyte symmetric;
|
||||
ubyte has_pawns;
|
||||
ubyte enc_type;
|
||||
struct PairsData *precomp;
|
||||
int factor[TBPIECES];
|
||||
ubyte pieces[TBPIECES];
|
||||
ubyte norm[TBPIECES];
|
||||
ubyte flags; // accurate, mapped, side
|
||||
ushort map_idx[4];
|
||||
ubyte *map;
|
||||
};
|
||||
|
||||
struct DTZEntry_pawn {
|
||||
char *data;
|
||||
uint64 key;
|
||||
uint64 mapping;
|
||||
ubyte ready;
|
||||
ubyte num;
|
||||
ubyte symmetric;
|
||||
ubyte has_pawns;
|
||||
ubyte pawns[2];
|
||||
struct {
|
||||
struct PairsData *precomp;
|
||||
int factor[TBPIECES];
|
||||
ubyte pieces[TBPIECES];
|
||||
ubyte norm[TBPIECES];
|
||||
} file[4];
|
||||
ubyte flags[4];
|
||||
ushort map_idx[4][4];
|
||||
ubyte *map;
|
||||
};
|
||||
|
||||
struct TBHashEntry {
|
||||
uint64 key;
|
||||
struct TBEntry *ptr;
|
||||
};
|
||||
|
||||
struct DTZTableEntry {
|
||||
uint64 key1;
|
||||
uint64 key2;
|
||||
struct TBEntry *entry;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+1588
-717
File diff suppressed because it is too large
Load Diff
+66
-5
@@ -1,17 +1,78 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (c) 2013 Ronald de Man
|
||||
Copyright (C) 2016-2018 Marco Costalba, Lucas Braesch
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef TBPROBE_H
|
||||
#define TBPROBE_H
|
||||
|
||||
#include <ostream>
|
||||
|
||||
#include "../search.h"
|
||||
|
||||
namespace Tablebases {
|
||||
|
||||
enum WDLScore {
|
||||
WDLLoss = -2, // Loss
|
||||
WDLBlessedLoss = -1, // Loss, but draw under 50-move rule
|
||||
WDLDraw = 0, // Draw
|
||||
WDLCursedWin = 1, // Win, but draw under 50-move rule
|
||||
WDLWin = 2, // Win
|
||||
|
||||
WDLScoreNone = -1000
|
||||
};
|
||||
|
||||
// Possible states after a probing operation
|
||||
enum ProbeState {
|
||||
FAIL = 0, // Probe failed (missing file table)
|
||||
OK = 1, // Probe succesful
|
||||
CHANGE_STM = -1, // DTZ should check the other side
|
||||
ZEROING_BEST_MOVE = 2 // Best move zeroes DTZ (capture or pawn move)
|
||||
};
|
||||
|
||||
extern int MaxCardinality;
|
||||
|
||||
void init(const std::string& path);
|
||||
int probe_wdl(Position& pos, int *success);
|
||||
int probe_dtz(Position& pos, int *success);
|
||||
bool root_probe(Position& pos, Search::RootMoveVector& rootMoves, Value& score);
|
||||
bool root_probe_wdl(Position& pos, Search::RootMoveVector& rootMoves, Value& score);
|
||||
void init(const std::string& paths);
|
||||
WDLScore probe_wdl(Position& pos, ProbeState* result);
|
||||
int probe_dtz(Position& pos, ProbeState* result);
|
||||
bool root_probe(Position& pos, Search::RootMoves& rootMoves, Value& score);
|
||||
bool root_probe_wdl(Position& pos, Search::RootMoves& rootMoves, Value& score);
|
||||
void filter_root_moves(Position& pos, Search::RootMoves& rootMoves);
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const WDLScore v) {
|
||||
|
||||
os << (v == WDLLoss ? "Loss" :
|
||||
v == WDLBlessedLoss ? "Blessed loss" :
|
||||
v == WDLDraw ? "Draw" :
|
||||
v == WDLCursedWin ? "Cursed win" :
|
||||
v == WDLWin ? "Win" : "None");
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const ProbeState v) {
|
||||
|
||||
os << (v == FAIL ? "Failed" :
|
||||
v == OK ? "Success" :
|
||||
v == CHANGE_STM ? "Probed opponent side" :
|
||||
v == ZEROING_BEST_MOVE ? "Best move zeroes DTZ" : "None");
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
+126
-312
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -24,362 +25,175 @@
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
#include "uci.h"
|
||||
|
||||
using namespace Search;
|
||||
#include "syzygy/tbprobe.h"
|
||||
|
||||
ThreadPool Threads; // Global object
|
||||
|
||||
extern void check_time();
|
||||
|
||||
namespace {
|
||||
/// Thread constructor launches the thread and waits until it goes to sleep
|
||||
/// in idle_loop(). Note that 'searching' and 'exit' should be alredy set.
|
||||
|
||||
// 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().
|
||||
|
||||
extern "C" { long start_routine(ThreadBase* th) { th->idle_loop(); return 0; } }
|
||||
|
||||
|
||||
// Helpers to launch a thread after creation and joining before delete. Must be
|
||||
// outside Thread c'tor and d'tor because the object must 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->mutex.lock();
|
||||
th->exit = true; // Search must be already finished
|
||||
th->mutex.unlock();
|
||||
|
||||
th->notify_one();
|
||||
thread_join(th->handle); // Wait for thread termination
|
||||
delete th;
|
||||
}
|
||||
Thread::Thread(size_t n) : idx(n), stdThread(&Thread::idle_loop, this) {
|
||||
|
||||
wait_for_search_finished();
|
||||
}
|
||||
|
||||
|
||||
// ThreadBase::notify_one() wakes up the thread when there is some work to do
|
||||
/// Thread destructor wakes up the thread in idle_loop() and waits
|
||||
/// for its termination. Thread should be already waiting.
|
||||
|
||||
void ThreadBase::notify_one() {
|
||||
Thread::~Thread() {
|
||||
|
||||
mutex.lock();
|
||||
sleepCondition.notify_one();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
// ThreadBase::wait_for() set the thread to sleep until 'condition' turns true
|
||||
|
||||
void ThreadBase::wait_for(volatile const bool& condition) {
|
||||
|
||||
mutex.lock();
|
||||
while (!condition) sleepCondition.wait(mutex);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
// Thread c'tor makes some init but does not launch any execution thread that
|
||||
// will be started only when c'tor returns.
|
||||
|
||||
Thread::Thread() /* : splitPoints() */ { // Initialization of non POD broken in MSVC
|
||||
|
||||
searching = false;
|
||||
maxPly = splitPointsSize = 0;
|
||||
activeSplitPoint = NULL;
|
||||
activePosition = NULL;
|
||||
idx = Threads.size(); // Starts from 0
|
||||
}
|
||||
|
||||
|
||||
// Thread::cutoff_occurred() checks whether a beta cutoff has occurred in the
|
||||
// current active split point, or in some ancestor of the split point.
|
||||
|
||||
bool Thread::cutoff_occurred() const {
|
||||
|
||||
for (SplitPoint* sp = activeSplitPoint; sp; sp = sp->parentSplitPoint)
|
||||
if (sp->cutoff)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// 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 slave's split point
|
||||
// stack (the "helpful master concept" in YBWC terminology).
|
||||
|
||||
bool Thread::available_to(const Thread* master) const {
|
||||
|
||||
if (searching)
|
||||
return false;
|
||||
|
||||
// Make a local copy to be sure it doesn't become zero under our feet while
|
||||
// testing next condition and so leading to an out of bounds access.
|
||||
const int size = splitPointsSize;
|
||||
|
||||
// No split points means that the thread is available as a slave for any
|
||||
// other thread otherwise apply the "helpful master" concept if possible.
|
||||
return !size || splitPoints[size - 1].slavesMask.test(master->idx);
|
||||
}
|
||||
|
||||
|
||||
// Thread::split() does the actual work of distributing the work at a node between
|
||||
// several available threads. If it does not succeed in splitting the node
|
||||
// (because no idle threads are available), the function immediately returns.
|
||||
// If splitting is possible, a SplitPoint object is initialized with all the
|
||||
// data that must be copied to the helper threads and then helper threads are
|
||||
// informed that they have been assigned work. This will cause them to instantly
|
||||
// leave their idle loops and call search(). When all threads have returned from
|
||||
// search() then split() returns.
|
||||
|
||||
void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bestValue,
|
||||
Move* bestMove, Depth depth, int moveCount,
|
||||
MovePicker* movePicker, int nodeType, bool cutNode) {
|
||||
|
||||
assert(searching);
|
||||
assert(-VALUE_INFINITE < *bestValue && *bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE);
|
||||
assert(depth >= Threads.minimumSplitDepth);
|
||||
assert(splitPointsSize < MAX_SPLITPOINTS_PER_THREAD);
|
||||
|
||||
// Pick and init the next available split point
|
||||
SplitPoint& sp = splitPoints[splitPointsSize];
|
||||
|
||||
sp.masterThread = this;
|
||||
sp.parentSplitPoint = activeSplitPoint;
|
||||
sp.slavesMask = 0, sp.slavesMask.set(idx);
|
||||
sp.depth = depth;
|
||||
sp.bestValue = *bestValue;
|
||||
sp.bestMove = *bestMove;
|
||||
sp.alpha = alpha;
|
||||
sp.beta = beta;
|
||||
sp.nodeType = nodeType;
|
||||
sp.cutNode = cutNode;
|
||||
sp.movePicker = movePicker;
|
||||
sp.moveCount = moveCount;
|
||||
sp.pos = &pos;
|
||||
sp.nodes = 0;
|
||||
sp.cutoff = false;
|
||||
sp.ss = ss;
|
||||
|
||||
// Try to allocate available threads and ask them to start searching setting
|
||||
// 'searching' flag. This must be done under lock protection to avoid concurrent
|
||||
// allocation of the same slave by another master.
|
||||
Threads.mutex.lock();
|
||||
sp.mutex.lock();
|
||||
|
||||
sp.allSlavesSearching = true; // Must be set under lock protection
|
||||
++splitPointsSize;
|
||||
activeSplitPoint = &sp;
|
||||
activePosition = NULL;
|
||||
|
||||
Thread* slave;
|
||||
|
||||
while ((slave = Threads.available_slave(this)) != NULL)
|
||||
{
|
||||
sp.slavesMask.set(slave->idx);
|
||||
slave->activeSplitPoint = &sp;
|
||||
slave->searching = true; // Slave leaves idle_loop()
|
||||
slave->notify_one(); // Could be sleeping
|
||||
}
|
||||
|
||||
// Everything is set up. The master thread enters the idle loop, from which
|
||||
// it will instantly launch a search, because its 'searching' flag is set.
|
||||
// The thread will return from the idle loop when all slaves have finished
|
||||
// their work at this split point.
|
||||
sp.mutex.unlock();
|
||||
Threads.mutex.unlock();
|
||||
|
||||
Thread::idle_loop(); // Force a call to base class idle_loop()
|
||||
|
||||
// In the helpful master concept, a master can help only a sub-tree of its
|
||||
// split point and because everything is finished here, it's not possible
|
||||
// for the master to be booked.
|
||||
assert(!searching);
|
||||
assert(!activePosition);
|
||||
|
||||
// We have returned from the idle loop, which means that all threads are
|
||||
// finished. Note that setting 'searching' and decreasing splitPointsSize must
|
||||
// be done under lock protection to avoid a race with Thread::available_to().
|
||||
Threads.mutex.lock();
|
||||
sp.mutex.lock();
|
||||
exit = true;
|
||||
start_searching();
|
||||
stdThread.join();
|
||||
}
|
||||
|
||||
|
||||
/// Thread::clear() reset histories, usually before a new game
|
||||
|
||||
void Thread::clear() {
|
||||
|
||||
counterMoves.fill(MOVE_NONE);
|
||||
mainHistory.fill(0);
|
||||
captureHistory.fill(0);
|
||||
|
||||
for (auto& to : contHistory)
|
||||
for (auto& h : to)
|
||||
h.fill(0);
|
||||
|
||||
contHistory[NO_PIECE][0].fill(Search::CounterMovePruneThreshold - 1);
|
||||
}
|
||||
|
||||
/// Thread::start_searching() wakes up the thread that will start the search
|
||||
|
||||
void Thread::start_searching() {
|
||||
|
||||
std::lock_guard<Mutex> lk(mutex);
|
||||
searching = true;
|
||||
--splitPointsSize;
|
||||
activeSplitPoint = sp.parentSplitPoint;
|
||||
activePosition = &pos;
|
||||
pos.set_nodes_searched(pos.nodes_searched() + sp.nodes);
|
||||
*bestMove = sp.bestMove;
|
||||
*bestValue = sp.bestValue;
|
||||
|
||||
sp.mutex.unlock();
|
||||
Threads.mutex.unlock();
|
||||
cv.notify_one(); // Wake up the thread in idle_loop()
|
||||
}
|
||||
|
||||
|
||||
// TimerThread::idle_loop() is where the timer thread waits Resolution milliseconds
|
||||
// and then calls check_time(). When not searching, thread sleeps until it's woken up.
|
||||
/// Thread::wait_for_search_finished() blocks on the condition variable
|
||||
/// until the thread has finished searching.
|
||||
|
||||
void TimerThread::idle_loop() {
|
||||
void Thread::wait_for_search_finished() {
|
||||
|
||||
while (!exit)
|
||||
std::unique_lock<Mutex> lk(mutex);
|
||||
cv.wait(lk, [&]{ return !searching; });
|
||||
}
|
||||
|
||||
|
||||
/// Thread::idle_loop() is where the thread is parked, blocked on the
|
||||
/// condition variable, when it has no work to do.
|
||||
|
||||
void Thread::idle_loop() {
|
||||
|
||||
// If OS already scheduled us on a different group than 0 then don't overwrite
|
||||
// the choice, eventually we are one of many one-threaded processes running on
|
||||
// some Windows NUMA hardware, for instance in fishtest. To make it simple,
|
||||
// just check if running threads are below a threshold, in this case all this
|
||||
// NUMA machinery is not needed.
|
||||
if (Options["Threads"] >= 8)
|
||||
WinProcGroup::bindThisThread(idx);
|
||||
|
||||
while (true)
|
||||
{
|
||||
mutex.lock();
|
||||
std::unique_lock<Mutex> lk(mutex);
|
||||
searching = false;
|
||||
cv.notify_one(); // Wake up anyone waiting for search finished
|
||||
cv.wait(lk, [&]{ return searching; });
|
||||
|
||||
if (!exit)
|
||||
sleepCondition.wait_for(mutex, run ? Resolution : INT_MAX);
|
||||
if (exit)
|
||||
return;
|
||||
|
||||
mutex.unlock();
|
||||
lk.unlock();
|
||||
|
||||
if (run)
|
||||
check_time();
|
||||
search();
|
||||
}
|
||||
}
|
||||
|
||||
/// ThreadPool::set() creates/destroys threads to match the requested number.
|
||||
/// Created and launced threads wil go immediately to sleep in idle_loop.
|
||||
/// Upon resizing, threads are recreated to allow for binding if necessary.
|
||||
|
||||
// MainThread::idle_loop() is where the main thread is parked waiting to be started
|
||||
// when there is a new search. The main thread will launch all the slave threads.
|
||||
void ThreadPool::set(size_t requested) {
|
||||
|
||||
void MainThread::idle_loop() {
|
||||
if (size() > 0) { // destroy any existing thread(s)
|
||||
main()->wait_for_search_finished();
|
||||
|
||||
while (!exit)
|
||||
{
|
||||
mutex.lock();
|
||||
while (size() > 0)
|
||||
delete back(), pop_back();
|
||||
}
|
||||
|
||||
thinking = false;
|
||||
if (requested > 0) { // create new thread(s)
|
||||
push_back(new MainThread(0));
|
||||
|
||||
while (!thinking && !exit)
|
||||
{
|
||||
Threads.sleepCondition.notify_one(); // Wake up the UI thread if needed
|
||||
sleepCondition.wait(mutex);
|
||||
}
|
||||
|
||||
mutex.unlock();
|
||||
|
||||
if (!exit)
|
||||
{
|
||||
searching = true;
|
||||
|
||||
Search::think();
|
||||
|
||||
assert(searching);
|
||||
|
||||
searching = false;
|
||||
}
|
||||
while (size() < requested)
|
||||
push_back(new Thread(size()));
|
||||
clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// ThreadPool::clear() sets threadPool data to initial values.
|
||||
|
||||
// ThreadPool::init() is called at startup to create and launch requested threads,
|
||||
// that will go immediately to sleep. We cannot use a c'tor because Threads is a
|
||||
// static object and we need a fully initialized engine at this point due to
|
||||
// allocation of Endgames in Thread c'tor.
|
||||
void ThreadPool::clear() {
|
||||
|
||||
void ThreadPool::init() {
|
||||
for (Thread* th : *this)
|
||||
th->clear();
|
||||
|
||||
timer = new_thread<TimerThread>();
|
||||
push_back(new_thread<MainThread>());
|
||||
read_uci_options();
|
||||
main()->callsCnt = 0;
|
||||
main()->previousScore = VALUE_INFINITE;
|
||||
main()->previousTimeReduction = 1;
|
||||
}
|
||||
|
||||
/// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and
|
||||
/// returns immediately. Main thread will wake up other threads and start the search.
|
||||
|
||||
// ThreadPool::exit() terminates the threads before the program exits. Cannot be
|
||||
// done in d'tor because threads must be terminated before freeing us.
|
||||
void ThreadPool::start_thinking(Position& pos, StateListPtr& states,
|
||||
const Search::LimitsType& limits, bool ponderMode) {
|
||||
|
||||
void ThreadPool::exit() {
|
||||
main()->wait_for_search_finished();
|
||||
|
||||
delete_thread(timer); // As first because check_time() accesses threads data
|
||||
stopOnPonderhit = stop = false;
|
||||
ponder = ponderMode;
|
||||
Search::Limits = limits;
|
||||
Search::RootMoves rootMoves;
|
||||
|
||||
for (iterator it = begin(); it != end(); ++it)
|
||||
delete_thread(*it);
|
||||
}
|
||||
|
||||
|
||||
// ThreadPool::read_uci_options() updates internal threads parameters from the
|
||||
// corresponding UCI options and creates/destroys threads to match the requested
|
||||
// number. Thread objects are dynamically allocated to avoid creating all possible
|
||||
// threads in advance (which include pawns and material tables), even if only a
|
||||
// few are to be used.
|
||||
|
||||
void ThreadPool::read_uci_options() {
|
||||
|
||||
minimumSplitDepth = Options["Min Split Depth"] * ONE_PLY;
|
||||
size_t requested = Options["Threads"];
|
||||
|
||||
assert(requested > 0);
|
||||
|
||||
// If zero (default) then set best minimum split depth automatically
|
||||
if (!minimumSplitDepth)
|
||||
minimumSplitDepth = requested < 8 ? 4 * ONE_PLY : 7 * ONE_PLY;
|
||||
|
||||
while (size() < requested)
|
||||
push_back(new_thread<Thread>());
|
||||
|
||||
while (size() > requested)
|
||||
{
|
||||
delete_thread(back());
|
||||
pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ThreadPool::available_slave() tries to find an idle thread which is available
|
||||
// as a slave for the thread 'master'.
|
||||
|
||||
Thread* ThreadPool::available_slave(const Thread* master) const {
|
||||
|
||||
for (const_iterator it = begin(); it != end(); ++it)
|
||||
if ((*it)->available_to(master))
|
||||
return *it;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
// ThreadPool::wait_for_think_finished() waits for main thread to finish the search
|
||||
|
||||
void ThreadPool::wait_for_think_finished() {
|
||||
|
||||
MainThread* th = main();
|
||||
th->mutex.lock();
|
||||
while (th->thinking) sleepCondition.wait(th->mutex);
|
||||
th->mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
// ThreadPool::start_thinking() wakes up the main thread sleeping in
|
||||
// MainThread::idle_loop() and starts a new search, then returns immediately.
|
||||
|
||||
void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
|
||||
StateStackPtr& states) {
|
||||
wait_for_think_finished();
|
||||
|
||||
SearchTime = Time::now(); // As early as possible
|
||||
|
||||
Signals.stopOnPonderhit = Signals.firstRootMove = false;
|
||||
Signals.stop = Signals.failedLowAtRoot = false;
|
||||
|
||||
RootMoves.clear();
|
||||
RootPos = pos;
|
||||
Limits = limits;
|
||||
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> it(pos); *it; ++it)
|
||||
for (const auto& m : MoveList<LEGAL>(pos))
|
||||
if ( limits.searchmoves.empty()
|
||||
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), *it))
|
||||
RootMoves.push_back(RootMove(*it));
|
||||
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
|
||||
rootMoves.emplace_back(m);
|
||||
|
||||
main()->thinking = true;
|
||||
main()->notify_one(); // Starts main thread
|
||||
if (!rootMoves.empty())
|
||||
Tablebases::filter_root_moves(pos, rootMoves);
|
||||
|
||||
// After ownership transfer 'states' becomes empty, so if we stop the search
|
||||
// and call 'go' again without setting a new position states.get() == NULL.
|
||||
assert(states.get() || setupStates.get());
|
||||
|
||||
if (states.get())
|
||||
setupStates = std::move(states); // Ownership transfer, states is now empty
|
||||
|
||||
// We use Position::set() to set root position across threads. But there are
|
||||
// some StateInfo fields (previous, pliesFromNull, capturedPiece) that cannot
|
||||
// be deduced from a fen string, so set() clears them and to not lose the info
|
||||
// we need to backup and later restore setupStates->back(). Note that setupStates
|
||||
// is shared by threads but is accessed in read-only mode.
|
||||
StateInfo tmp = setupStates->back();
|
||||
|
||||
for (Thread* th : *this)
|
||||
{
|
||||
th->nodes = th->tbHits = th->nmp_ply = th->nmp_odd = 0;
|
||||
th->rootDepth = th->completedDepth = DEPTH_ZERO;
|
||||
th->rootMoves = rootMoves;
|
||||
th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th);
|
||||
}
|
||||
|
||||
setupStates->back() = tmp;
|
||||
|
||||
main()->start_searching();
|
||||
}
|
||||
|
||||
+63
-126
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -20,7 +21,10 @@
|
||||
#ifndef THREAD_H_INCLUDED
|
||||
#define THREAD_H_INCLUDED
|
||||
|
||||
#include <bitset>
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "material.h"
|
||||
@@ -28,157 +32,90 @@
|
||||
#include "pawns.h"
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
|
||||
struct Thread;
|
||||
|
||||
const int MAX_THREADS = 128;
|
||||
const int MAX_SPLITPOINTS_PER_THREAD = 8;
|
||||
|
||||
/// Mutex and ConditionVariable struct are wrappers of the low level locking
|
||||
/// machinery and are modeled after the corresponding C++11 classes.
|
||||
|
||||
struct Mutex {
|
||||
Mutex() { lock_init(l); }
|
||||
~Mutex() { lock_destroy(l); }
|
||||
|
||||
void lock() { lock_grab(l); }
|
||||
void unlock() { lock_release(l); }
|
||||
|
||||
private:
|
||||
friend struct ConditionVariable;
|
||||
|
||||
Lock l;
|
||||
};
|
||||
|
||||
struct ConditionVariable {
|
||||
ConditionVariable() { cond_init(c); }
|
||||
~ConditionVariable() { cond_destroy(c); }
|
||||
|
||||
void wait(Mutex& m) { cond_wait(c, m.l); }
|
||||
void wait_for(Mutex& m, int ms) { timed_wait(c, m.l, ms); }
|
||||
void notify_one() { cond_signal(c); }
|
||||
|
||||
private:
|
||||
WaitCondition c;
|
||||
};
|
||||
#include "thread_win32.h"
|
||||
|
||||
|
||||
/// SplitPoint struct stores information shared by the threads searching in
|
||||
/// parallel below the same split point. It is populated at splitting time.
|
||||
/// Thread class keeps together all the thread-related stuff. We 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 SplitPoint {
|
||||
|
||||
// Const data after split point has been setup
|
||||
const Position* pos;
|
||||
Search::Stack* ss;
|
||||
Thread* masterThread;
|
||||
Depth depth;
|
||||
Value beta;
|
||||
int nodeType;
|
||||
bool cutNode;
|
||||
|
||||
// Const pointers to shared data
|
||||
MovePicker* movePicker;
|
||||
SplitPoint* parentSplitPoint;
|
||||
|
||||
// Shared variable data
|
||||
Mutex mutex;
|
||||
std::bitset<MAX_THREADS> slavesMask;
|
||||
volatile bool allSlavesSearching;
|
||||
volatile uint64_t nodes;
|
||||
volatile Value alpha;
|
||||
volatile Value bestValue;
|
||||
volatile Move bestMove;
|
||||
volatile int moveCount;
|
||||
volatile bool cutoff;
|
||||
};
|
||||
|
||||
|
||||
/// ThreadBase struct is the base of the hierarchy from where we derive all the
|
||||
/// specialized thread classes.
|
||||
|
||||
struct ThreadBase {
|
||||
|
||||
ThreadBase() : handle(NativeHandle()), exit(false) {}
|
||||
virtual ~ThreadBase() {}
|
||||
virtual void idle_loop() = 0;
|
||||
void notify_one();
|
||||
void wait_for(volatile const bool& b);
|
||||
class Thread {
|
||||
|
||||
Mutex mutex;
|
||||
ConditionVariable sleepCondition;
|
||||
NativeHandle handle;
|
||||
volatile bool exit;
|
||||
};
|
||||
ConditionVariable cv;
|
||||
size_t idx;
|
||||
bool exit = false, searching = true; // Set before starting std::thread
|
||||
std::thread stdThread;
|
||||
|
||||
public:
|
||||
explicit Thread(size_t);
|
||||
virtual ~Thread();
|
||||
virtual void search();
|
||||
void clear();
|
||||
void idle_loop();
|
||||
void start_searching();
|
||||
void wait_for_search_finished();
|
||||
|
||||
/// 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 : public ThreadBase {
|
||||
|
||||
Thread();
|
||||
virtual void idle_loop();
|
||||
bool cutoff_occurred() const;
|
||||
bool available_to(const Thread* master) const;
|
||||
|
||||
void split(Position& pos, Search::Stack* ss, Value alpha, Value beta, Value* bestValue, Move* bestMove,
|
||||
Depth depth, int moveCount, MovePicker* movePicker, int nodeType, bool cutNode);
|
||||
|
||||
SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD];
|
||||
Pawns::Table pawnsTable;
|
||||
Material::Table materialTable;
|
||||
Endgames endgames;
|
||||
Position* activePosition;
|
||||
size_t idx;
|
||||
int maxPly;
|
||||
SplitPoint* volatile activeSplitPoint;
|
||||
volatile int splitPointsSize;
|
||||
volatile bool searching;
|
||||
size_t PVIdx;
|
||||
int selDepth, nmp_ply, nmp_odd;
|
||||
std::atomic<uint64_t> nodes, tbHits;
|
||||
|
||||
Position rootPos;
|
||||
Search::RootMoves rootMoves;
|
||||
Depth rootDepth, completedDepth;
|
||||
CounterMoveHistory counterMoves;
|
||||
ButterflyHistory mainHistory;
|
||||
CapturePieceToHistory captureHistory;
|
||||
ContinuationHistory contHistory;
|
||||
};
|
||||
|
||||
|
||||
/// MainThread and TimerThread are derived classes used to characterize the two
|
||||
/// special threads: the main one and the recurring timer.
|
||||
/// MainThread is a derived class specific for main thread
|
||||
|
||||
struct MainThread : public Thread {
|
||||
MainThread() : thinking(true) {} // Avoid a race with start_thinking()
|
||||
virtual void idle_loop();
|
||||
volatile bool thinking;
|
||||
};
|
||||
|
||||
struct TimerThread : public ThreadBase {
|
||||
using Thread::Thread;
|
||||
|
||||
static const int Resolution = 5; // Millisec between two check_time() calls
|
||||
void search() override;
|
||||
void check_time();
|
||||
|
||||
TimerThread() : run(false) {}
|
||||
virtual void idle_loop();
|
||||
|
||||
bool run;
|
||||
bool failedLow;
|
||||
double bestMoveChanges, previousTimeReduction;
|
||||
Value previousScore;
|
||||
int callsCnt;
|
||||
};
|
||||
|
||||
|
||||
/// ThreadPool struct handles all the threads related stuff like init, starting,
|
||||
/// parking and, most importantly, launching a slave thread at a split point.
|
||||
/// All the access to shared thread data is done through this class.
|
||||
/// ThreadPool struct handles all the threads-related stuff like init, starting,
|
||||
/// parking and, most importantly, launching a thread. All the access to threads
|
||||
/// is done through this class.
|
||||
|
||||
struct ThreadPool : public std::vector<Thread*> {
|
||||
|
||||
void init(); // No c'tor and d'tor, threads rely on globals that should be
|
||||
void exit(); // initialized and are valid during the whole thread lifetime.
|
||||
void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
|
||||
void clear();
|
||||
void set(size_t);
|
||||
|
||||
MainThread* main() { return static_cast<MainThread*>(at(0)); }
|
||||
void read_uci_options();
|
||||
Thread* available_slave(const Thread* master) const;
|
||||
void wait_for_think_finished();
|
||||
void start_thinking(const Position&, const Search::LimitsType&, Search::StateStackPtr&);
|
||||
MainThread* main() const { return static_cast<MainThread*>(front()); }
|
||||
uint64_t nodes_searched() const { return accumulate(&Thread::nodes); }
|
||||
uint64_t tb_hits() const { return accumulate(&Thread::tbHits); }
|
||||
|
||||
Depth minimumSplitDepth;
|
||||
Mutex mutex;
|
||||
ConditionVariable sleepCondition;
|
||||
TimerThread* timer;
|
||||
std::atomic_bool stop, ponder, stopOnPonderhit;
|
||||
|
||||
private:
|
||||
StateListPtr setupStates;
|
||||
|
||||
uint64_t accumulate(std::atomic<uint64_t> Thread::* member) const {
|
||||
|
||||
uint64_t sum = 0;
|
||||
for (Thread* th : *this)
|
||||
sum += (th->*member).load(std::memory_order_relaxed);
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
|
||||
extern ThreadPool Threads;
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Stockfish is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef THREAD_WIN32_H_INCLUDED
|
||||
#define THREAD_WIN32_H_INCLUDED
|
||||
|
||||
/// STL thread library used by mingw and gcc when cross compiling for Windows
|
||||
/// relies on libwinpthread. Currently libwinpthread implements mutexes directly
|
||||
/// on top of Windows semaphores. Semaphores, being kernel objects, require kernel
|
||||
/// mode transition in order to lock or unlock, which is very slow compared to
|
||||
/// interlocked operations (about 30% slower on bench test). To work around this
|
||||
/// issue, we define our wrappers to the low level Win32 calls. We use critical
|
||||
/// sections to support Windows XP and older versions. Unfortunately, cond_wait()
|
||||
/// is racy between unlock() and WaitForSingleObject() but they have the same
|
||||
/// speed performance as the SRW locks.
|
||||
|
||||
#include <condition_variable>
|
||||
#include <mutex>
|
||||
|
||||
#if defined(_WIN32) && !defined(_MSC_VER)
|
||||
|
||||
#ifndef NOMINMAX
|
||||
# define NOMINMAX // Disable macros min() and max()
|
||||
#endif
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#undef WIN32_LEAN_AND_MEAN
|
||||
#undef NOMINMAX
|
||||
|
||||
/// Mutex and ConditionVariable struct are wrappers of the low level locking
|
||||
/// machinery and are modeled after the corresponding C++11 classes.
|
||||
|
||||
struct Mutex {
|
||||
Mutex() { InitializeCriticalSection(&cs); }
|
||||
~Mutex() { DeleteCriticalSection(&cs); }
|
||||
void lock() { EnterCriticalSection(&cs); }
|
||||
void unlock() { LeaveCriticalSection(&cs); }
|
||||
|
||||
private:
|
||||
CRITICAL_SECTION cs;
|
||||
};
|
||||
|
||||
typedef std::condition_variable_any ConditionVariable;
|
||||
|
||||
#else // Default case: use STL classes
|
||||
|
||||
typedef std::mutex Mutex;
|
||||
typedef std::condition_variable ConditionVariable;
|
||||
|
||||
#endif
|
||||
|
||||
#endif // #ifndef THREAD_WIN32_H_INCLUDED
|
||||
+35
-19
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -25,32 +26,34 @@
|
||||
#include "timeman.h"
|
||||
#include "uci.h"
|
||||
|
||||
TimeManagement Time; // Our global time management object
|
||||
|
||||
namespace {
|
||||
|
||||
enum TimeType { OptimumTime, MaxTime };
|
||||
|
||||
const int MoveHorizon = 50; // Plan time management at most this many moves ahead
|
||||
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
|
||||
const double MaxRatio = 7.09; // When in trouble, we can step over reserved time with this ratio
|
||||
const double StealRatio = 0.35; // However we must not steal time from remaining moves over this ratio
|
||||
|
||||
|
||||
// move_importance() is a skew-logistic function based on naive statistical
|
||||
// analysis of "how many games are still undecided after n half-moves". Game
|
||||
// is considered "undecided" as long as neither side has >275cp advantage.
|
||||
// Data was extracted from CCRL game database with some simple filtering criteria.
|
||||
// Data was extracted from the CCRL game database with some simple filtering criteria.
|
||||
|
||||
double move_importance(int ply) {
|
||||
|
||||
const double XScale = 9.3;
|
||||
const double XShift = 59.8;
|
||||
const double Skew = 0.172;
|
||||
const double XScale = 7.64;
|
||||
const double XShift = 58.4;
|
||||
const double Skew = 0.183;
|
||||
|
||||
return pow((1 + exp((ply - XShift) / XScale)), -Skew) + DBL_MIN; // Ensure non-zero
|
||||
}
|
||||
|
||||
template<TimeType T>
|
||||
int remaining(int myTime, int movesToGo, int ply, int slowMover)
|
||||
{
|
||||
int remaining(int myTime, int movesToGo, int ply, int slowMover) {
|
||||
|
||||
const double TMaxRatio = (T == OptimumTime ? 1 : MaxRatio);
|
||||
const double TStealRatio = (T == OptimumTime ? 0 : StealRatio);
|
||||
|
||||
@@ -63,7 +66,7 @@ namespace {
|
||||
double ratio1 = (TMaxRatio * moveImportance) / (TMaxRatio * moveImportance + otherMovesImportance);
|
||||
double ratio2 = (moveImportance + TStealRatio * otherMovesImportance) / (moveImportance + otherMovesImportance);
|
||||
|
||||
return int(myTime * std::min(ratio1, ratio2)); // Intel C++ asks an explicit cast
|
||||
return int(myTime * std::min(ratio1, ratio2)); // Intel C++ asks for an explicit cast
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -78,15 +81,30 @@ namespace {
|
||||
/// inc > 0 && movestogo == 0 means: x basetime + z increment
|
||||
/// inc > 0 && movestogo != 0 means: x moves in y minutes + z increment
|
||||
|
||||
void TimeManager::init(const Search::LimitsType& limits, Color us, int ply)
|
||||
{
|
||||
void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
|
||||
|
||||
int minThinkingTime = Options["Minimum Thinking Time"];
|
||||
int moveOverhead = Options["Move Overhead"];
|
||||
int slowMover = Options["Slow Mover"];
|
||||
int npmsec = Options["nodestime"];
|
||||
|
||||
// Initialize unstablePvFactor to 1 and search times to maximum values
|
||||
unstablePvFactor = 1;
|
||||
optimumSearchTime = maximumSearchTime = std::max(limits.time[us], minThinkingTime);
|
||||
// If we have to play in 'nodes as time' mode, then convert from time
|
||||
// to nodes, and use resulting values in time management formulas.
|
||||
// WARNING: Given npms (nodes per millisecond) must be much lower then
|
||||
// the real engine speed to avoid time losses.
|
||||
if (npmsec)
|
||||
{
|
||||
if (!availableNodes) // Only once at game start
|
||||
availableNodes = npmsec * limits.time[us]; // Time is in msec
|
||||
|
||||
// Convert from millisecs to nodes
|
||||
limits.time[us] = (int)availableNodes;
|
||||
limits.inc[us] *= npmsec;
|
||||
limits.npmsec = npmsec;
|
||||
}
|
||||
|
||||
startTime = limits.startTime;
|
||||
optimumTime = maximumTime = std::max(limits.time[us], minThinkingTime);
|
||||
|
||||
const int MaxMTG = limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon;
|
||||
|
||||
@@ -105,12 +123,10 @@ void TimeManager::init(const Search::LimitsType& limits, Color us, int ply)
|
||||
int t1 = minThinkingTime + remaining<OptimumTime>(hypMyTime, hypMTG, ply, slowMover);
|
||||
int t2 = minThinkingTime + remaining<MaxTime >(hypMyTime, hypMTG, ply, slowMover);
|
||||
|
||||
optimumSearchTime = std::min(t1, optimumSearchTime);
|
||||
maximumSearchTime = std::min(t2, maximumSearchTime);
|
||||
optimumTime = std::min(t1, optimumTime);
|
||||
maximumTime = std::min(t2, maximumTime);
|
||||
}
|
||||
|
||||
if (Options["Ponder"])
|
||||
optimumSearchTime += optimumSearchTime / 4;
|
||||
|
||||
optimumSearchTime = std::min(optimumSearchTime, maximumSearchTime);
|
||||
optimumTime += optimumTime / 4;
|
||||
}
|
||||
|
||||
+19
-10
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -20,20 +21,28 @@
|
||||
#ifndef TIMEMAN_H_INCLUDED
|
||||
#define TIMEMAN_H_INCLUDED
|
||||
|
||||
/// The TimeManager class computes the optimal time to think depending on the
|
||||
/// maximum available time, the game move number and other parameters.
|
||||
#include "misc.h"
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
|
||||
class TimeManager {
|
||||
/// The TimeManagement class computes the optimal time to think depending on
|
||||
/// the maximum available time, the game move number and other parameters.
|
||||
|
||||
class TimeManagement {
|
||||
public:
|
||||
void init(const Search::LimitsType& limits, Color us, int ply);
|
||||
void pv_instability(double bestMoveChanges) { unstablePvFactor = 1 + bestMoveChanges; }
|
||||
int available_time() const { return int(optimumSearchTime * unstablePvFactor * 0.71); }
|
||||
int maximum_time() const { return maximumSearchTime; }
|
||||
void init(Search::LimitsType& limits, Color us, int ply);
|
||||
int optimum() const { return optimumTime; }
|
||||
int maximum() const { return maximumTime; }
|
||||
int elapsed() const { return int(Search::Limits.npmsec ? Threads.nodes_searched() : now() - startTime); }
|
||||
|
||||
int64_t availableNodes; // When in 'nodes as time' mode
|
||||
|
||||
private:
|
||||
int optimumSearchTime;
|
||||
int maximumSearchTime;
|
||||
double unstablePvFactor;
|
||||
TimePoint startTime;
|
||||
int optimumTime;
|
||||
int maximumTime;
|
||||
};
|
||||
|
||||
extern TimeManagement Time;
|
||||
|
||||
#endif // #ifndef TIMEMAN_H_INCLUDED
|
||||
|
||||
+31
-11
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -32,9 +33,7 @@ TranspositionTable TT; // Our global transposition table
|
||||
|
||||
void TranspositionTable::resize(size_t mbSize) {
|
||||
|
||||
assert(sizeof(Cluster) == CacheLineSize / 2);
|
||||
|
||||
size_t newClusterCount = size_t(1) << msb((mbSize * 1024 * 1024) / sizeof(Cluster));
|
||||
size_t newClusterCount = mbSize * 1024 * 1024 / sizeof(Cluster);
|
||||
|
||||
if (newClusterCount == clusterCount)
|
||||
return;
|
||||
@@ -42,7 +41,7 @@ void TranspositionTable::resize(size_t mbSize) {
|
||||
clusterCount = newClusterCount;
|
||||
|
||||
free(mem);
|
||||
mem = calloc(clusterCount * sizeof(Cluster) + CacheLineSize - 1, 1);
|
||||
mem = malloc(clusterCount * sizeof(Cluster) + CacheLineSize - 1);
|
||||
|
||||
if (!mem)
|
||||
{
|
||||
@@ -52,6 +51,7 @@ void TranspositionTable::resize(size_t mbSize) {
|
||||
}
|
||||
|
||||
table = (Cluster*)((uintptr_t(mem) + CacheLineSize - 1) & ~(CacheLineSize - 1));
|
||||
clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -68,9 +68,9 @@ void TranspositionTable::clear() {
|
||||
/// TranspositionTable::probe() looks up the current position in the transposition
|
||||
/// table. It returns true and a pointer to the TTEntry if the position is found.
|
||||
/// Otherwise, it returns false and a pointer to an empty or least valuable TTEntry
|
||||
/// to be replaced later. A TTEntry t1 is considered to be 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.
|
||||
/// to be replaced later. The replace value of an entry is calculated as its depth
|
||||
/// minus 8 times its relative age. TTEntry t1 is considered more valuable than
|
||||
/// TTEntry t2 if its replace value is greater than that of t2.
|
||||
|
||||
TTEntry* TranspositionTable::probe(const Key key, bool& found) const {
|
||||
|
||||
@@ -80,7 +80,7 @@ TTEntry* TranspositionTable::probe(const Key key, bool& found) const {
|
||||
for (int i = 0; i < ClusterSize; ++i)
|
||||
if (!tte[i].key16 || tte[i].key16 == key16)
|
||||
{
|
||||
if (tte[i].key16)
|
||||
if ((tte[i].genBound8 & 0xFC) != generation8 && tte[i].key16)
|
||||
tte[i].genBound8 = uint8_t(generation8 | tte[i].bound()); // Refresh
|
||||
|
||||
return found = (bool)tte[i].key16, &tte[i];
|
||||
@@ -89,10 +89,30 @@ TTEntry* TranspositionTable::probe(const Key key, bool& found) const {
|
||||
// Find an entry to be replaced according to the replacement strategy
|
||||
TTEntry* replace = tte;
|
||||
for (int i = 1; i < ClusterSize; ++i)
|
||||
if ( (( tte[i].genBound8 & 0xFC) == generation8 || tte[i].bound() == BOUND_EXACT)
|
||||
- ((replace->genBound8 & 0xFC) == generation8)
|
||||
- (tte[i].depth8 < replace->depth8) < 0)
|
||||
// Due to our packed storage format for generation and its cyclic
|
||||
// nature we add 259 (256 is the modulus plus 3 to keep the lowest
|
||||
// two bound bits from affecting the result) to calculate the entry
|
||||
// age correctly even after generation8 overflows into the next cycle.
|
||||
if ( replace->depth8 - ((259 + generation8 - replace->genBound8) & 0xFC) * 2
|
||||
> tte[i].depth8 - ((259 + generation8 - tte[i].genBound8) & 0xFC) * 2)
|
||||
replace = &tte[i];
|
||||
|
||||
return found = false, replace;
|
||||
}
|
||||
|
||||
|
||||
/// TranspositionTable::hashfull() returns an approximation of the hashtable
|
||||
/// occupation during a search. The hash is x permill full, as per UCI protocol.
|
||||
|
||||
int TranspositionTable::hashfull() const {
|
||||
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < 1000 / ClusterSize; i++)
|
||||
{
|
||||
const TTEntry* tte = &table[i].entry[0];
|
||||
for (int j = 0; j < ClusterSize; j++)
|
||||
if ((tte[j].genBound8 & 0xFC) == generation8)
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -38,19 +39,29 @@ struct TTEntry {
|
||||
Move move() const { return (Move )move16; }
|
||||
Value value() const { return (Value)value16; }
|
||||
Value eval() const { return (Value)eval16; }
|
||||
Depth depth() const { return (Depth)depth8; }
|
||||
Depth depth() const { return (Depth)(depth8 * int(ONE_PLY)); }
|
||||
Bound bound() const { return (Bound)(genBound8 & 0x3); }
|
||||
|
||||
void save(Key k, Value v, Bound b, Depth d, Move m, Value ev, uint8_t g) {
|
||||
|
||||
if (m || (k >> 48) != key16) // Preserve any existing move for the same position
|
||||
assert(d / ONE_PLY * ONE_PLY == d);
|
||||
|
||||
// Preserve any existing move for the same position
|
||||
if (m || (k >> 48) != key16)
|
||||
move16 = (uint16_t)m;
|
||||
|
||||
key16 = (uint16_t)(k >> 48);
|
||||
value16 = (int16_t)v;
|
||||
eval16 = (int16_t)ev;
|
||||
genBound8 = (uint8_t)(g | b);
|
||||
depth8 = (int8_t)d;
|
||||
// Don't overwrite more valuable entries
|
||||
if ( (k >> 48) != key16
|
||||
|| d / ONE_PLY > depth8 - 4
|
||||
/* || g != (genBound8 & 0xFC) // Matching non-zero keys are already refreshed by probe() */
|
||||
|| b == BOUND_EXACT)
|
||||
{
|
||||
key16 = (uint16_t)(k >> 48);
|
||||
value16 = (int16_t)v;
|
||||
eval16 = (int16_t)ev;
|
||||
genBound8 = (uint8_t)(g | b);
|
||||
depth8 = (int8_t)(d / ONE_PLY);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -68,8 +79,9 @@ private:
|
||||
/// A TranspositionTable consists of a power of 2 number of clusters and each
|
||||
/// cluster consists of ClusterSize number of TTEntry. Each non-empty entry
|
||||
/// contains information of exactly one position. The size of a cluster should
|
||||
/// not be bigger than a cache line size. In case it is less, it should be padded
|
||||
/// to guarantee always aligned accesses.
|
||||
/// divide the size of a cache line size, to ensure that clusters never cross
|
||||
/// cache lines. This ensures best cache performance, as the cacheline is
|
||||
/// prefetched, as soon as possible.
|
||||
|
||||
class TranspositionTable {
|
||||
|
||||
@@ -78,20 +90,23 @@ class TranspositionTable {
|
||||
|
||||
struct Cluster {
|
||||
TTEntry entry[ClusterSize];
|
||||
char padding[2]; // Align to the cache line size
|
||||
char padding[2]; // Align to a divisor of the cache line size
|
||||
};
|
||||
|
||||
static_assert(CacheLineSize % sizeof(Cluster) == 0, "Cluster size incorrect");
|
||||
|
||||
public:
|
||||
~TranspositionTable() { free(mem); }
|
||||
void new_search() { generation8 += 4; } // Lower 2 bits are used by Bound
|
||||
uint8_t generation() const { return generation8; }
|
||||
TTEntry* probe(const Key key, bool& found) const;
|
||||
int hashfull() const;
|
||||
void resize(size_t mbSize);
|
||||
void clear();
|
||||
|
||||
// The lowest order bits of the key are used to get the index of the cluster
|
||||
// The 32 lowest order bits of the key are used to get the index of the cluster
|
||||
TTEntry* first_entry(const Key key) const {
|
||||
return &table[(size_t)key & (clusterCount - 1)].entry[0];
|
||||
return &table[(uint32_t(key) * uint64_t(clusterCount)) >> 32].entry[0];
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
+154
-124
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -33,13 +34,22 @@
|
||||
///
|
||||
/// -DUSE_POPCNT | Add runtime support for use of popcnt asm-instruction. Works
|
||||
/// | only in 64-bit mode and requires hardware with popcnt support.
|
||||
///
|
||||
/// -DUSE_PEXT | Add runtime support for use of pext asm-instruction. Works
|
||||
/// | only in 64-bit mode and requires hardware with pext support.
|
||||
|
||||
#include <cassert>
|
||||
#include <cctype>
|
||||
#include <climits>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "platform.h"
|
||||
#if defined(_MSC_VER)
|
||||
// Disable some silly and noisy warning from MSVC compiler
|
||||
#pragma warning(disable: 4127) // Conditional expression is constant
|
||||
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
|
||||
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
|
||||
#endif
|
||||
|
||||
/// Predefined macros hell:
|
||||
///
|
||||
@@ -49,14 +59,13 @@
|
||||
/// _WIN32 Building on Windows (any)
|
||||
/// _WIN64 Building on Windows 64 bit
|
||||
|
||||
#if defined(_WIN64) && !defined(IS_64BIT) // Last condition means Makefile is not used
|
||||
# include <intrin.h> // MSVC popcnt and bsfq instrinsics
|
||||
#if defined(_WIN64) && defined(_MSC_VER) // No Makefile used
|
||||
# include <intrin.h> // Microsoft header for _BitScanForward64()
|
||||
# define IS_64BIT
|
||||
# define USE_BSFQ
|
||||
#endif
|
||||
|
||||
#if defined(USE_POPCNT) && defined(__INTEL_COMPILER) && defined(_MSC_VER)
|
||||
# include <nmmintrin.h> // Intel header for _mm_popcnt_u64() intrinsic
|
||||
#if defined(USE_POPCNT) && (defined(__INTEL_COMPILER) || defined(_MSC_VER))
|
||||
# include <nmmintrin.h> // Intel and Microsoft header for _mm_popcnt_u64()
|
||||
#endif
|
||||
|
||||
#if !defined(NO_PREFETCH) && (defined(__INTEL_COMPILER) || defined(_MSC_VER))
|
||||
@@ -67,15 +76,7 @@
|
||||
# include <immintrin.h> // Header for _pext_u64() intrinsic
|
||||
# define pext(b, m) _pext_u64(b, m)
|
||||
#else
|
||||
# define pext(b, m) (0)
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# define FORCE_INLINE __forceinline
|
||||
#elif defined(__GNUC__)
|
||||
# define FORCE_INLINE inline __attribute__((always_inline))
|
||||
#else
|
||||
# define FORCE_INLINE inline
|
||||
# define pext(b, m) 0
|
||||
#endif
|
||||
|
||||
#ifdef USE_POPCNT
|
||||
@@ -114,7 +115,7 @@ const int MAX_PLY = 128;
|
||||
/// any normal move destination square is always different from origin square
|
||||
/// while MOVE_NONE and MOVE_NULL have the same origin and destination square.
|
||||
|
||||
enum Move {
|
||||
enum Move : int {
|
||||
MOVE_NONE,
|
||||
MOVE_NULL = 65
|
||||
};
|
||||
@@ -127,7 +128,7 @@ enum MoveType {
|
||||
};
|
||||
|
||||
enum Color {
|
||||
WHITE, BLACK, NO_COLOR, COLOR_NB = 2
|
||||
WHITE, BLACK, COLOR_NB = 2
|
||||
};
|
||||
|
||||
enum CastlingSide {
|
||||
@@ -145,7 +146,7 @@ enum CastlingRight {
|
||||
};
|
||||
|
||||
template<Color C, CastlingSide S> struct MakeCastling {
|
||||
static const CastlingRight
|
||||
static constexpr CastlingRight
|
||||
right = C == WHITE ? S == QUEEN_SIDE ? WHITE_OOO : WHITE_OO
|
||||
: S == QUEEN_SIDE ? BLACK_OOO : BLACK_OO;
|
||||
};
|
||||
@@ -171,7 +172,7 @@ enum Bound {
|
||||
BOUND_EXACT = BOUND_UPPER | BOUND_LOWER
|
||||
};
|
||||
|
||||
enum Value {
|
||||
enum Value : int {
|
||||
VALUE_ZERO = 0,
|
||||
VALUE_DRAW = 0,
|
||||
VALUE_KNOWN_WIN = 10000,
|
||||
@@ -182,21 +183,19 @@ enum Value {
|
||||
VALUE_MATE_IN_MAX_PLY = VALUE_MATE - 2 * MAX_PLY,
|
||||
VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + 2 * MAX_PLY,
|
||||
|
||||
VALUE_ENSURE_INTEGER_SIZE_P = INT_MAX,
|
||||
VALUE_ENSURE_INTEGER_SIZE_N = INT_MIN,
|
||||
PawnValueMg = 171, PawnValueEg = 240,
|
||||
KnightValueMg = 764, KnightValueEg = 848,
|
||||
BishopValueMg = 826, BishopValueEg = 891,
|
||||
RookValueMg = 1282, RookValueEg = 1373,
|
||||
QueenValueMg = 2526, QueenValueEg = 2646,
|
||||
|
||||
PawnValueMg = 198, PawnValueEg = 258,
|
||||
KnightValueMg = 817, KnightValueEg = 846,
|
||||
BishopValueMg = 836, BishopValueEg = 857,
|
||||
RookValueMg = 1270, RookValueEg = 1278,
|
||||
QueenValueMg = 2521, QueenValueEg = 2558,
|
||||
|
||||
MidgameLimit = 15581, EndgameLimit = 3998
|
||||
MidgameLimit = 15258, EndgameLimit = 3915
|
||||
};
|
||||
|
||||
enum PieceType {
|
||||
NO_PIECE_TYPE, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING,
|
||||
ALL_PIECES = 0,
|
||||
QUEEN_DIAGONAL = 7,
|
||||
PIECE_TYPE_NB = 8
|
||||
};
|
||||
|
||||
@@ -207,20 +206,24 @@ enum Piece {
|
||||
PIECE_NB = 16
|
||||
};
|
||||
|
||||
enum Depth {
|
||||
extern Value PieceValue[PHASE_NB][PIECE_NB];
|
||||
|
||||
enum Depth : int {
|
||||
|
||||
ONE_PLY = 1,
|
||||
|
||||
DEPTH_ZERO = 0,
|
||||
DEPTH_QS_CHECKS = 0,
|
||||
DEPTH_QS_NO_CHECKS = -1,
|
||||
DEPTH_QS_RECAPTURES = -5,
|
||||
DEPTH_ZERO = 0 * ONE_PLY,
|
||||
DEPTH_QS_CHECKS = 0 * ONE_PLY,
|
||||
DEPTH_QS_NO_CHECKS = -1 * ONE_PLY,
|
||||
DEPTH_QS_RECAPTURES = -5 * ONE_PLY,
|
||||
|
||||
DEPTH_NONE = -6,
|
||||
DEPTH_MAX = MAX_PLY
|
||||
DEPTH_NONE = -6 * ONE_PLY,
|
||||
DEPTH_MAX = MAX_PLY * ONE_PLY
|
||||
};
|
||||
|
||||
enum Square {
|
||||
static_assert(!(ONE_PLY & (ONE_PLY - 1)), "ONE_PLY is not a power of 2");
|
||||
|
||||
enum Square : int {
|
||||
SQ_A1, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1,
|
||||
SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2,
|
||||
SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3,
|
||||
@@ -231,138 +234,161 @@ enum Square {
|
||||
SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
|
||||
SQ_NONE,
|
||||
|
||||
SQUARE_NB = 64,
|
||||
|
||||
DELTA_N = 8,
|
||||
DELTA_E = 1,
|
||||
DELTA_S = -8,
|
||||
DELTA_W = -1,
|
||||
|
||||
DELTA_NN = DELTA_N + DELTA_N,
|
||||
DELTA_NE = DELTA_N + DELTA_E,
|
||||
DELTA_SE = DELTA_S + DELTA_E,
|
||||
DELTA_SS = DELTA_S + DELTA_S,
|
||||
DELTA_SW = DELTA_S + DELTA_W,
|
||||
DELTA_NW = DELTA_N + DELTA_W
|
||||
SQUARE_NB = 64
|
||||
};
|
||||
|
||||
enum File {
|
||||
enum Direction : int {
|
||||
NORTH = 8,
|
||||
EAST = 1,
|
||||
SOUTH = -NORTH,
|
||||
WEST = -EAST,
|
||||
|
||||
NORTH_EAST = NORTH + EAST,
|
||||
SOUTH_EAST = SOUTH + EAST,
|
||||
SOUTH_WEST = SOUTH + WEST,
|
||||
NORTH_WEST = NORTH + WEST
|
||||
};
|
||||
|
||||
enum File : int {
|
||||
FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_NB
|
||||
};
|
||||
|
||||
enum Rank {
|
||||
enum Rank : int {
|
||||
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB
|
||||
};
|
||||
|
||||
|
||||
/// Score enum stores a middlegame and an endgame value in a single integer.
|
||||
/// The least significant 16 bits are used to store the endgame value and
|
||||
/// the upper 16 bits are used to store the middlegame value. The compiler
|
||||
/// is free to choose the enum type as long as it can store the data, so we
|
||||
/// ensure that Score is an integer type by assigning some big int values.
|
||||
enum Score {
|
||||
SCORE_ZERO,
|
||||
SCORE_ENSURE_INTEGER_SIZE_P = INT_MAX,
|
||||
SCORE_ENSURE_INTEGER_SIZE_N = INT_MIN
|
||||
};
|
||||
/// Score enum stores a middlegame and an endgame value in a single integer
|
||||
/// (enum). The least significant 16 bits are used to store the endgame value
|
||||
/// and the upper 16 bits are used to store the middlegame value. Take some
|
||||
/// care to avoid left-shifting a signed int to avoid undefined behavior.
|
||||
enum Score : int { SCORE_ZERO };
|
||||
|
||||
inline Score make_score(int mg, int eg) {
|
||||
return Score((mg << 16) + eg);
|
||||
constexpr Score make_score(int mg, int eg) {
|
||||
return Score((int)((unsigned int)eg << 16) + mg);
|
||||
}
|
||||
|
||||
/// Extracting the signed lower and upper 16 bits is 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) {
|
||||
|
||||
union { uint16_t u; int16_t s; } mg = { uint16_t(unsigned(s + 0x8000) >> 16) };
|
||||
return Value(mg.s);
|
||||
}
|
||||
|
||||
inline Value eg_value(Score s) {
|
||||
|
||||
union { uint16_t u; int16_t s; } eg = { uint16_t(unsigned(s)) };
|
||||
union { uint16_t u; int16_t s; } eg = { uint16_t(unsigned(s + 0x8000) >> 16) };
|
||||
return Value(eg.s);
|
||||
}
|
||||
|
||||
#define ENABLE_BASE_OPERATORS_ON(T) \
|
||||
inline T operator+(T d1, T d2) { return T(int(d1) + int(d2)); } \
|
||||
inline T operator-(T d1, T d2) { return T(int(d1) - int(d2)); } \
|
||||
inline T operator*(int i, T d) { return T(i * int(d)); } \
|
||||
inline T operator*(T d, int i) { return T(int(d) * i); } \
|
||||
inline T operator-(T d) { return T(-int(d)); } \
|
||||
inline T& operator+=(T& d1, T d2) { return d1 = d1 + d2; } \
|
||||
inline T& operator-=(T& d1, T d2) { return d1 = d1 - d2; } \
|
||||
inline T& operator*=(T& d, int i) { return d = T(int(d) * i); }
|
||||
inline Value mg_value(Score s) {
|
||||
union { uint16_t u; int16_t s; } mg = { uint16_t(unsigned(s)) };
|
||||
return Value(mg.s);
|
||||
}
|
||||
|
||||
#define ENABLE_FULL_OPERATORS_ON(T) \
|
||||
ENABLE_BASE_OPERATORS_ON(T) \
|
||||
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/(T d, int i) { return T(int(d) / i); } \
|
||||
inline int operator/(T d1, T d2) { return int(d1) / int(d2); } \
|
||||
#define ENABLE_BASE_OPERATORS_ON(T) \
|
||||
constexpr T operator+(T d1, T d2) { return T(int(d1) + int(d2)); } \
|
||||
constexpr T operator-(T d1, T d2) { return T(int(d1) - int(d2)); } \
|
||||
constexpr T operator-(T d) { return T(-int(d)); } \
|
||||
inline T& operator+=(T& d1, T d2) { return d1 = d1 + d2; } \
|
||||
inline T& operator-=(T& d1, T d2) { return d1 = d1 - d2; }
|
||||
|
||||
#define ENABLE_INCR_OPERATORS_ON(T) \
|
||||
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
|
||||
inline T& operator--(T& d) { return d = T(int(d) - 1); }
|
||||
|
||||
#define ENABLE_FULL_OPERATORS_ON(T) \
|
||||
ENABLE_BASE_OPERATORS_ON(T) \
|
||||
ENABLE_INCR_OPERATORS_ON(T) \
|
||||
constexpr T operator*(int i, T d) { return T(i * int(d)); } \
|
||||
constexpr T operator*(T d, int i) { return T(int(d) * i); } \
|
||||
constexpr T operator/(T d, int i) { return T(int(d) / i); } \
|
||||
constexpr int operator/(T d1, T d2) { return int(d1) / int(d2); } \
|
||||
inline T& operator*=(T& d, int i) { return d = T(int(d) * i); } \
|
||||
inline T& operator/=(T& d, int i) { return d = T(int(d) / i); }
|
||||
|
||||
ENABLE_FULL_OPERATORS_ON(Value)
|
||||
ENABLE_FULL_OPERATORS_ON(PieceType)
|
||||
ENABLE_FULL_OPERATORS_ON(Piece)
|
||||
ENABLE_FULL_OPERATORS_ON(Color)
|
||||
ENABLE_FULL_OPERATORS_ON(Depth)
|
||||
ENABLE_FULL_OPERATORS_ON(Square)
|
||||
ENABLE_FULL_OPERATORS_ON(File)
|
||||
ENABLE_FULL_OPERATORS_ON(Rank)
|
||||
ENABLE_FULL_OPERATORS_ON(Direction)
|
||||
|
||||
ENABLE_INCR_OPERATORS_ON(PieceType)
|
||||
ENABLE_INCR_OPERATORS_ON(Piece)
|
||||
ENABLE_INCR_OPERATORS_ON(Color)
|
||||
ENABLE_INCR_OPERATORS_ON(Square)
|
||||
ENABLE_INCR_OPERATORS_ON(File)
|
||||
ENABLE_INCR_OPERATORS_ON(Rank)
|
||||
|
||||
ENABLE_BASE_OPERATORS_ON(Score)
|
||||
|
||||
#undef ENABLE_FULL_OPERATORS_ON
|
||||
#undef ENABLE_INCR_OPERATORS_ON
|
||||
#undef ENABLE_BASE_OPERATORS_ON
|
||||
|
||||
/// Additional operators to add integers to a Value
|
||||
inline Value operator+(Value v, int i) { return Value(int(v) + i); }
|
||||
inline Value operator-(Value v, int i) { return Value(int(v) - i); }
|
||||
constexpr Value operator+(Value v, int i) { return Value(int(v) + i); }
|
||||
constexpr Value operator-(Value v, int i) { return Value(int(v) - i); }
|
||||
inline Value& operator+=(Value& v, int i) { return v = v + i; }
|
||||
inline Value& operator-=(Value& v, int i) { return v = v - i; }
|
||||
|
||||
/// Additional operators to add a Direction to a Square
|
||||
inline Square operator+(Square s, Direction d) { return Square(int(s) + int(d)); }
|
||||
inline Square operator-(Square s, Direction d) { return Square(int(s) - int(d)); }
|
||||
inline Square& operator+=(Square &s, Direction d) { return s = s + d; }
|
||||
inline Square& operator-=(Square &s, Direction d) { return s = s - d; }
|
||||
|
||||
/// Only declared but not defined. We don't want to multiply two scores due to
|
||||
/// a very high risk of overflow. So user should explicitly convert to integer.
|
||||
inline Score operator*(Score s1, Score s2);
|
||||
Score operator*(Score, Score) = delete;
|
||||
|
||||
/// Division of a Score must be handled separately for each term
|
||||
inline Score operator/(Score s, int i) {
|
||||
return make_score(mg_value(s) / i, eg_value(s) / i);
|
||||
}
|
||||
|
||||
extern Value PieceValue[PHASE_NB][PIECE_NB];
|
||||
/// Multiplication of a Score by an integer. We check for overflow in debug mode.
|
||||
inline Score operator*(Score s, int i) {
|
||||
|
||||
inline Color operator~(Color c) {
|
||||
return Color(c ^ BLACK);
|
||||
Score result = Score(int(s) * i);
|
||||
|
||||
assert(eg_value(result) == (i * eg_value(s)));
|
||||
assert(mg_value(result) == (i * mg_value(s)));
|
||||
assert((i == 0) || (result / i) == s );
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Square operator~(Square s) {
|
||||
constexpr Color operator~(Color c) {
|
||||
return Color(c ^ BLACK); // Toggle color
|
||||
}
|
||||
|
||||
constexpr Square operator~(Square s) {
|
||||
return Square(s ^ SQ_A8); // Vertical flip SQ_A1 -> SQ_A8
|
||||
}
|
||||
|
||||
inline CastlingRight operator|(Color c, CastlingSide s) {
|
||||
constexpr File operator~(File f) {
|
||||
return File(f ^ FILE_H); // Horizontal flip FILE_A -> FILE_H
|
||||
}
|
||||
|
||||
constexpr Piece operator~(Piece pc) {
|
||||
return Piece(pc ^ 8); // Swap color of piece B_KNIGHT -> W_KNIGHT
|
||||
}
|
||||
|
||||
constexpr CastlingRight operator|(Color c, CastlingSide s) {
|
||||
return CastlingRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
|
||||
}
|
||||
|
||||
inline Value mate_in(int ply) {
|
||||
constexpr Value mate_in(int ply) {
|
||||
return VALUE_MATE - ply;
|
||||
}
|
||||
|
||||
inline Value mated_in(int ply) {
|
||||
constexpr Value mated_in(int ply) {
|
||||
return -VALUE_MATE + ply;
|
||||
}
|
||||
|
||||
inline Square make_square(File f, Rank r) {
|
||||
return Square((r << 3) | f);
|
||||
constexpr Square make_square(File f, Rank r) {
|
||||
return Square((r << 3) + f);
|
||||
}
|
||||
|
||||
inline Piece make_piece(Color c, PieceType pt) {
|
||||
return Piece((c << 3) | pt);
|
||||
constexpr Piece make_piece(Color c, PieceType pt) {
|
||||
return Piece((c << 3) + pt);
|
||||
}
|
||||
|
||||
inline PieceType type_of(Piece pc) {
|
||||
constexpr PieceType type_of(Piece pc) {
|
||||
return PieceType(pc & 7);
|
||||
}
|
||||
|
||||
@@ -371,27 +397,27 @@ inline Color color_of(Piece pc) {
|
||||
return Color(pc >> 3);
|
||||
}
|
||||
|
||||
inline bool is_ok(Square s) {
|
||||
constexpr bool is_ok(Square s) {
|
||||
return s >= SQ_A1 && s <= SQ_H8;
|
||||
}
|
||||
|
||||
inline File file_of(Square s) {
|
||||
constexpr File file_of(Square s) {
|
||||
return File(s & 7);
|
||||
}
|
||||
|
||||
inline Rank rank_of(Square s) {
|
||||
constexpr Rank rank_of(Square s) {
|
||||
return Rank(s >> 3);
|
||||
}
|
||||
|
||||
inline Square relative_square(Color c, Square s) {
|
||||
constexpr Square relative_square(Color c, Square s) {
|
||||
return Square(s ^ (c * 56));
|
||||
}
|
||||
|
||||
inline Rank relative_rank(Color c, Rank r) {
|
||||
constexpr Rank relative_rank(Color c, Rank r) {
|
||||
return Rank(r ^ (c * 7));
|
||||
}
|
||||
|
||||
inline Rank relative_rank(Color c, Square s) {
|
||||
constexpr Rank relative_rank(Color c, Square s) {
|
||||
return relative_rank(c, rank_of(s));
|
||||
}
|
||||
|
||||
@@ -400,36 +426,40 @@ inline bool opposite_colors(Square s1, Square s2) {
|
||||
return ((s >> 3) ^ s) & 1;
|
||||
}
|
||||
|
||||
inline Square pawn_push(Color c) {
|
||||
return c == WHITE ? DELTA_N : DELTA_S;
|
||||
constexpr Direction pawn_push(Color c) {
|
||||
return c == WHITE ? NORTH : SOUTH;
|
||||
}
|
||||
|
||||
inline Square from_sq(Move m) {
|
||||
constexpr Square from_sq(Move m) {
|
||||
return Square((m >> 6) & 0x3F);
|
||||
}
|
||||
|
||||
inline Square to_sq(Move m) {
|
||||
constexpr Square to_sq(Move m) {
|
||||
return Square(m & 0x3F);
|
||||
}
|
||||
|
||||
inline MoveType type_of(Move m) {
|
||||
constexpr int from_to(Move m) {
|
||||
return m & 0xFFF;
|
||||
}
|
||||
|
||||
constexpr MoveType type_of(Move m) {
|
||||
return MoveType(m & (3 << 14));
|
||||
}
|
||||
|
||||
inline PieceType promotion_type(Move m) {
|
||||
return PieceType(((m >> 12) & 3) + 2);
|
||||
constexpr PieceType promotion_type(Move m) {
|
||||
return PieceType(((m >> 12) & 3) + KNIGHT);
|
||||
}
|
||||
|
||||
inline Move make_move(Square from, Square to) {
|
||||
return Move(to | (from << 6));
|
||||
return Move((from << 6) + to);
|
||||
}
|
||||
|
||||
template<MoveType T>
|
||||
inline Move make(Square from, Square to, PieceType pt = KNIGHT) {
|
||||
return Move(to | (from << 6) | T | ((pt - KNIGHT) << 12));
|
||||
constexpr Move make(Square from, Square to, PieceType pt = KNIGHT) {
|
||||
return Move(T + ((pt - KNIGHT) << 12) + (from << 6) + to);
|
||||
}
|
||||
|
||||
inline bool is_ok(Move m) {
|
||||
constexpr bool is_ok(Move m) {
|
||||
return from_sq(m) != to_sq(m); // Catch MOVE_NULL and MOVE_NONE
|
||||
}
|
||||
|
||||
|
||||
+93
-59
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,6 +18,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
@@ -27,29 +29,26 @@
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
#include "tt.h"
|
||||
#include "timeman.h"
|
||||
#include "uci.h"
|
||||
#include "syzygy/tbprobe.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
extern void benchmark(const Position& pos, istream& is);
|
||||
extern vector<string> setup_bench(const Position&, istream&);
|
||||
|
||||
namespace {
|
||||
|
||||
// FEN string of the initial position, normal chess
|
||||
const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
|
||||
// Stack to keep track of the position states along the setup moves (from the
|
||||
// start position to the position just before the search starts). Needed by
|
||||
// 'draw by repetition' detection.
|
||||
Search::StateStackPtr SetupStates;
|
||||
|
||||
|
||||
// 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 position(Position& pos, istringstream& is) {
|
||||
void position(Position& pos, istringstream& is, StateListPtr& states) {
|
||||
|
||||
Move m;
|
||||
string token, fen;
|
||||
@@ -67,14 +66,14 @@ namespace {
|
||||
else
|
||||
return;
|
||||
|
||||
pos.set(fen, Options["UCI_Chess960"], Threads.main());
|
||||
SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
|
||||
states = StateListPtr(new std::deque<StateInfo>(1)); // Drop old and create a new one
|
||||
pos.set(fen, Options["UCI_Chess960"], &states->back(), Threads.main());
|
||||
|
||||
// Parse move list (if any)
|
||||
while (is >> token && (m = UCI::to_move(pos, token)) != MOVE_NONE)
|
||||
{
|
||||
SetupStates->push(StateInfo());
|
||||
pos.do_move(m, SetupStates->top());
|
||||
states->emplace_back();
|
||||
pos.do_move(m, states->back());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,11 +89,11 @@ namespace {
|
||||
|
||||
// Read option name (can contain spaces)
|
||||
while (is >> token && token != "value")
|
||||
name += string(" ", !name.empty()) + token;
|
||||
name += string(" ", name.empty() ? 0 : 1) + token;
|
||||
|
||||
// Read option value (can contain spaces)
|
||||
while (is >> token)
|
||||
value += string(" ", !value.empty()) + token;
|
||||
value += string(" ", value.empty() ? 0 : 1) + token;
|
||||
|
||||
if (Options.count(name))
|
||||
Options[name] = value;
|
||||
@@ -107,10 +106,13 @@ namespace {
|
||||
// the thinking time and other parameters from the input string, then starts
|
||||
// the search.
|
||||
|
||||
void go(const Position& pos, istringstream& is) {
|
||||
void go(Position& pos, istringstream& is, StateListPtr& states) {
|
||||
|
||||
Search::LimitsType limits;
|
||||
string token;
|
||||
bool ponderMode = false;
|
||||
|
||||
limits.startTime = now(); // As early as possible!
|
||||
|
||||
while (is >> token)
|
||||
if (token == "searchmoves")
|
||||
@@ -126,10 +128,53 @@ namespace {
|
||||
else if (token == "nodes") is >> limits.nodes;
|
||||
else if (token == "movetime") is >> limits.movetime;
|
||||
else if (token == "mate") is >> limits.mate;
|
||||
else if (token == "infinite") limits.infinite = true;
|
||||
else if (token == "ponder") limits.ponder = true;
|
||||
else if (token == "perft") is >> limits.perft;
|
||||
else if (token == "infinite") limits.infinite = 1;
|
||||
else if (token == "ponder") ponderMode = true;
|
||||
|
||||
Threads.start_thinking(pos, limits, SetupStates);
|
||||
Threads.start_thinking(pos, states, limits, ponderMode);
|
||||
}
|
||||
|
||||
|
||||
// bench() is called when engine receives the "bench" command. Firstly
|
||||
// a list of UCI commands is setup according to bench parameters, then
|
||||
// it is run one by one printing a summary at the end.
|
||||
|
||||
void bench(Position& pos, istream& args, StateListPtr& states) {
|
||||
|
||||
string token;
|
||||
uint64_t num, nodes = 0, cnt = 1;
|
||||
|
||||
vector<string> list = setup_bench(pos, args);
|
||||
num = count_if(list.begin(), list.end(), [](string s) { return s.find("go ") == 0; });
|
||||
|
||||
TimePoint elapsed = now();
|
||||
|
||||
for (const auto& cmd : list)
|
||||
{
|
||||
istringstream is(cmd);
|
||||
is >> skipws >> token;
|
||||
|
||||
if (token == "go")
|
||||
{
|
||||
cerr << "\nPosition: " << cnt++ << '/' << num << endl;
|
||||
go(pos, is, states);
|
||||
Threads.main()->wait_for_search_finished();
|
||||
nodes += Threads.nodes_searched();
|
||||
}
|
||||
else if (token == "setoption") setoption(is);
|
||||
else if (token == "position") position(pos, is, states);
|
||||
else if (token == "ucinewgame") Search::clear();
|
||||
}
|
||||
|
||||
elapsed = now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero'
|
||||
|
||||
dbg_print(); // Just before exiting
|
||||
|
||||
cerr << "\n==========================="
|
||||
<< "\nTotal time (ms) : " << elapsed
|
||||
<< "\nNodes searched : " << nodes
|
||||
<< "\nNodes/second : " << 1000 * nodes / elapsed << endl;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -143,8 +188,12 @@ namespace {
|
||||
|
||||
void UCI::loop(int argc, char* argv[]) {
|
||||
|
||||
Position pos(StartFEN, false, Threads.main()); // The root position
|
||||
Position pos;
|
||||
string token, cmd;
|
||||
StateListPtr states(new std::deque<StateInfo>(1));
|
||||
auto uiThread = std::make_shared<Thread>(0);
|
||||
|
||||
pos.set(StartFEN, false, &states->back(), uiThread.get());
|
||||
|
||||
for (int i = 1; i < argc; ++i)
|
||||
cmd += std::string(argv[i]) + " ";
|
||||
@@ -155,57 +204,42 @@ void UCI::loop(int argc, char* argv[]) {
|
||||
|
||||
istringstream is(cmd);
|
||||
|
||||
token.clear(); // getline() could return empty or blank line
|
||||
token.clear(); // Avoid a stale if getline() returns empty or blank line
|
||||
is >> skipws >> token;
|
||||
|
||||
// The GUI sends 'ponderhit' to tell us to ponder on the same move the
|
||||
// opponent has played. In case Signals.stopOnPonderhit is set we are
|
||||
// waiting for 'ponderhit' to stop the search (for instance because we
|
||||
// already ran out of time), otherwise we should continue searching but
|
||||
// switching from pondering to normal search.
|
||||
// The GUI sends 'ponderhit' to tell us the user has played the expected move.
|
||||
// So 'ponderhit' will be sent if we were told to ponder on the same move the
|
||||
// user has played. We should continue searching but switch from pondering to
|
||||
// normal search. In case Threads.stopOnPonderhit is set we are waiting for
|
||||
// 'ponderhit' to stop the search, for instance if max search depth is reached.
|
||||
if ( token == "quit"
|
||||
|| token == "stop"
|
||||
|| (token == "ponderhit" && Search::Signals.stopOnPonderhit))
|
||||
{
|
||||
Search::Signals.stop = true;
|
||||
Threads.main()->notify_one(); // Could be sleeping
|
||||
}
|
||||
|| (token == "ponderhit" && Threads.stopOnPonderhit))
|
||||
Threads.stop = true;
|
||||
|
||||
else if (token == "ponderhit")
|
||||
Search::Limits.ponder = false; // Switch to normal search
|
||||
Threads.ponder = false; // Switch to normal search
|
||||
|
||||
else if (token == "uci")
|
||||
sync_cout << "id name " << engine_info(true)
|
||||
<< "\n" << Options
|
||||
<< "\nuciok" << sync_endl;
|
||||
|
||||
else if (token == "isready") sync_cout << "readyok" << sync_endl;
|
||||
else if (token == "ucinewgame") TT.clear();
|
||||
else if (token == "go") go(pos, is);
|
||||
else if (token == "position") position(pos, is);
|
||||
else if (token == "setoption") setoption(is);
|
||||
else if (token == "go") go(pos, is, states);
|
||||
else if (token == "position") position(pos, is, states);
|
||||
else if (token == "ucinewgame") Search::clear();
|
||||
else if (token == "isready") sync_cout << "readyok" << sync_endl;
|
||||
|
||||
// Additional custom non-UCI commands, useful for debugging
|
||||
else if (token == "flip") pos.flip();
|
||||
else if (token == "bench") benchmark(pos, is);
|
||||
else if (token == "d") sync_cout << pos << sync_endl;
|
||||
else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
|
||||
else if (token == "perft")
|
||||
{
|
||||
int depth;
|
||||
stringstream ss;
|
||||
|
||||
is >> depth;
|
||||
ss << Options["Hash"] << " "
|
||||
<< Options["Threads"] << " " << depth << " current perft";
|
||||
|
||||
benchmark(pos, ss);
|
||||
}
|
||||
// Additional custom non-UCI commands, mainly for debugging
|
||||
else if (token == "flip") pos.flip();
|
||||
else if (token == "bench") bench(pos, is, states);
|
||||
else if (token == "d") sync_cout << pos << sync_endl;
|
||||
else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
|
||||
else
|
||||
sync_cout << "Unknown command: " << cmd << sync_endl;
|
||||
|
||||
} while (token != "quit" && argc == 1); // Passed args have one-shot behaviour
|
||||
|
||||
Threads.wait_for_think_finished(); // Cannot quit whilst the search is running
|
||||
} while (token != "quit" && argc == 1); // Command line args are one-shot
|
||||
}
|
||||
|
||||
|
||||
@@ -218,6 +252,8 @@ void UCI::loop(int argc, char* argv[]) {
|
||||
|
||||
string UCI::value(Value v) {
|
||||
|
||||
assert(-VALUE_INFINITE < v && v < VALUE_INFINITE);
|
||||
|
||||
stringstream ss;
|
||||
|
||||
if (abs(v) < VALUE_MATE - MAX_PLY)
|
||||
@@ -232,9 +268,7 @@ string UCI::value(Value v) {
|
||||
/// UCI::square() converts a Square to a string in algebraic notation (g1, a7, etc.)
|
||||
|
||||
std::string UCI::square(Square s) {
|
||||
|
||||
char sq[] = { char('a' + file_of(s)), char('1' + rank_of(s)), 0 }; // NULL terminated
|
||||
return sq;
|
||||
return std::string{ char('a' + file_of(s)), char('1' + rank_of(s)) };
|
||||
}
|
||||
|
||||
|
||||
@@ -274,9 +308,9 @@ Move UCI::to_move(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> it(pos); *it; ++it)
|
||||
if (str == UCI::move(*it, pos.is_chess960()))
|
||||
return *it;
|
||||
for (const auto& m : MoveList<LEGAL>(pos))
|
||||
if (str == UCI::move(m, pos.is_chess960()))
|
||||
return m;
|
||||
|
||||
return MOVE_NONE;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,8 +18,8 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef UCIOPTION_H_INCLUDED
|
||||
#define UCIOPTION_H_INCLUDED
|
||||
#ifndef UCI_H_INCLUDED
|
||||
#define UCI_H_INCLUDED
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
@@ -45,10 +46,10 @@ class Option {
|
||||
typedef void (*OnChange)(const Option&);
|
||||
|
||||
public:
|
||||
Option(OnChange = NULL);
|
||||
Option(bool v, OnChange = NULL);
|
||||
Option(const char* v, OnChange = NULL);
|
||||
Option(int v, int min, int max, OnChange = NULL);
|
||||
Option(OnChange = nullptr);
|
||||
Option(bool v, OnChange = nullptr);
|
||||
Option(const char* v, OnChange = nullptr);
|
||||
Option(int v, int minv, int maxv, OnChange = nullptr);
|
||||
|
||||
Option& operator=(const std::string&);
|
||||
void operator<<(const Option&);
|
||||
@@ -69,10 +70,11 @@ void loop(int argc, char* argv[]);
|
||||
std::string value(Value v);
|
||||
std::string square(Square s);
|
||||
std::string move(Move m, bool chess960);
|
||||
std::string pv(const Position& pos, Depth depth, Value alpha, Value beta);
|
||||
Move to_move(const Position& pos, std::string& str);
|
||||
|
||||
} // namespace UCI
|
||||
|
||||
extern UCI::OptionsMap Options;
|
||||
|
||||
#endif // #ifndef UCIOPTION_H_INCLUDED
|
||||
#endif // #ifndef UCI_H_INCLUDED
|
||||
|
||||
+24
-22
@@ -2,6 +2,7 @@
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2015-2018 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -19,10 +20,10 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
#include <ostream>
|
||||
|
||||
#include "misc.h"
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
#include "tt.h"
|
||||
#include "uci.h"
|
||||
@@ -35,18 +36,18 @@ UCI::OptionsMap Options; // Global object
|
||||
namespace UCI {
|
||||
|
||||
/// 'On change' actions, triggered by an option's value change
|
||||
void on_clear_hash(const Option&) { TT.clear(); }
|
||||
void on_clear_hash(const Option&) { Search::clear(); }
|
||||
void on_hash_size(const Option& o) { TT.resize(o); }
|
||||
void on_logger(const Option& o) { start_logger(o); }
|
||||
void on_threads(const Option&) { Threads.read_uci_options(); }
|
||||
void on_threads(const Option& o) { Threads.set(o); }
|
||||
void on_tb_path(const Option& o) { Tablebases::init(o); }
|
||||
|
||||
|
||||
/// Our case insensitive less() function as required by UCI protocol
|
||||
bool ci_less(char c1, char c2) { return tolower(c1) < tolower(c2); }
|
||||
|
||||
bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
|
||||
return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), ci_less);
|
||||
|
||||
return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
|
||||
[](char c1, char c2) { return tolower(c1) < tolower(c2); });
|
||||
}
|
||||
|
||||
|
||||
@@ -54,20 +55,21 @@ bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const
|
||||
|
||||
void init(OptionsMap& o) {
|
||||
|
||||
const int MaxHashMB = Is64Bit ? 1024 * 1024 : 2048;
|
||||
// at most 2^32 clusters.
|
||||
const int MaxHashMB = Is64Bit ? 131072 : 2048;
|
||||
|
||||
o["Write Debug Log"] << Option(false, on_logger);
|
||||
o["Contempt"] << Option(0, -100, 100);
|
||||
o["Min Split Depth"] << Option(0, 0, 12, on_threads);
|
||||
o["Threads"] << Option(1, 1, MAX_THREADS, on_threads);
|
||||
o["Debug Log File"] << Option("", on_logger);
|
||||
o["Contempt"] << Option(20, -100, 100);
|
||||
o["Threads"] << Option(1, 1, 512, on_threads);
|
||||
o["Hash"] << Option(16, 1, MaxHashMB, on_hash_size);
|
||||
o["Clear Hash"] << Option(on_clear_hash);
|
||||
o["Ponder"] << Option(true);
|
||||
o["Ponder"] << Option(false);
|
||||
o["MultiPV"] << Option(1, 1, 500);
|
||||
o["Skill Level"] << Option(20, 0, 20);
|
||||
o["Move Overhead"] << Option(30, 0, 5000);
|
||||
o["Minimum Thinking Time"] << Option(20, 0, 5000);
|
||||
o["Slow Mover"] << Option(80, 10, 1000);
|
||||
o["Slow Mover"] << Option(89, 10, 1000);
|
||||
o["nodestime"] << Option(0, 0, 10000);
|
||||
o["UCI_Chess960"] << Option(false);
|
||||
o["SyzygyPath"] << Option("<empty>", on_tb_path);
|
||||
o["SyzygyProbeDepth"] << Option(1, 1, 100);
|
||||
@@ -82,11 +84,11 @@ void init(OptionsMap& o) {
|
||||
std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
|
||||
|
||||
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)
|
||||
for (const auto& it : om)
|
||||
if (it.second.idx == idx)
|
||||
{
|
||||
const Option& o = it->second;
|
||||
os << "\noption name " << it->first << " type " << o.type;
|
||||
const Option& o = it.second;
|
||||
os << "\noption name " << it.first << " type " << o.type;
|
||||
|
||||
if (o.type != "button")
|
||||
os << " default " << o.defaultValue;
|
||||
@@ -96,6 +98,7 @@ std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
@@ -112,12 +115,11 @@ Option::Option(OnChange f) : type("button"), min(0), max(0), on_change(f)
|
||||
{}
|
||||
|
||||
Option::Option(int v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
|
||||
{ std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); }
|
||||
|
||||
{ defaultValue = currentValue = std::to_string(v); }
|
||||
|
||||
Option::operator int() const {
|
||||
assert(type == "check" || type == "spin");
|
||||
return (type == "spin" ? atoi(currentValue.c_str()) : currentValue == "true");
|
||||
return (type == "spin" ? stoi(currentValue) : currentValue == "true");
|
||||
}
|
||||
|
||||
Option::operator std::string() const {
|
||||
@@ -147,7 +149,7 @@ Option& Option::operator=(const string& v) {
|
||||
|
||||
if ( (type != "button" && v.empty())
|
||||
|| (type == "check" && v != "true" && v != "false")
|
||||
|| (type == "spin" && (atoi(v.c_str()) < min || atoi(v.c_str()) > max)))
|
||||
|| (type == "spin" && (stoi(v) < min || stoi(v) > max)))
|
||||
return *this;
|
||||
|
||||
if (type != "button")
|
||||
|
||||
Executable
+123
@@ -0,0 +1,123 @@
|
||||
#!/bin/bash
|
||||
# check for errors under valgrind or sanitizers.
|
||||
|
||||
error()
|
||||
{
|
||||
echo "instrumented testing failed on line $1"
|
||||
exit 1
|
||||
}
|
||||
trap 'error ${LINENO}' ERR
|
||||
|
||||
# define suitable post and prefixes for testing options
|
||||
case $1 in
|
||||
--valgrind)
|
||||
echo "valgrind testing started"
|
||||
prefix=''
|
||||
exeprefix='valgrind --error-exitcode=42'
|
||||
postfix='1>/dev/null'
|
||||
threads="1"
|
||||
;;
|
||||
--valgrind-thread)
|
||||
echo "valgrind-thread testing started"
|
||||
prefix=''
|
||||
exeprefix='valgrind --error-exitcode=42'
|
||||
postfix='1>/dev/null'
|
||||
threads="2"
|
||||
;;
|
||||
--sanitizer-undefined)
|
||||
echo "sanitizer-undefined testing started"
|
||||
prefix='!'
|
||||
exeprefix=''
|
||||
postfix='2>&1 | grep "runtime error:"'
|
||||
threads="1"
|
||||
;;
|
||||
--sanitizer-thread)
|
||||
echo "sanitizer-thread testing started"
|
||||
prefix='!'
|
||||
exeprefix=''
|
||||
postfix='2>&1 | grep "WARNING: ThreadSanitizer:"'
|
||||
threads="2"
|
||||
|
||||
cat << EOF > tsan.supp
|
||||
race:TTEntry::move
|
||||
race:TTEntry::depth
|
||||
race:TTEntry::bound
|
||||
race:TTEntry::save
|
||||
race:TTEntry::value
|
||||
race:TTEntry::eval
|
||||
|
||||
race:TranspositionTable::probe
|
||||
race:TranspositionTable::hashfull
|
||||
|
||||
EOF
|
||||
|
||||
export TSAN_OPTIONS="suppressions=./tsan.supp"
|
||||
|
||||
;;
|
||||
*)
|
||||
echo "unknown testing started"
|
||||
prefix=''
|
||||
exeprefix=''
|
||||
postfix=''
|
||||
threads="1"
|
||||
;;
|
||||
esac
|
||||
|
||||
# simple command line testing
|
||||
for args in "eval" \
|
||||
"go nodes 1000" \
|
||||
"go depth 10" \
|
||||
"go movetime 1000" \
|
||||
"go wtime 8000 btime 8000 winc 500 binc 500" \
|
||||
"bench 128 $threads 10 default depth"
|
||||
do
|
||||
|
||||
echo "$prefix $exeprefix ./stockfish $args $postfix"
|
||||
eval "$prefix $exeprefix ./stockfish $args $postfix"
|
||||
|
||||
done
|
||||
|
||||
# more general testing, following an uci protocol exchange
|
||||
cat << EOF > game.exp
|
||||
set timeout 10
|
||||
spawn $exeprefix ./stockfish
|
||||
|
||||
send "uci\n"
|
||||
expect "uciok"
|
||||
|
||||
send "setoption name Threads value $threads\n"
|
||||
|
||||
send "ucinewgame\n"
|
||||
send "position startpos\n"
|
||||
send "go nodes 1000\n"
|
||||
expect "bestmove"
|
||||
|
||||
send "position startpos moves e2e4 e7e6\n"
|
||||
send "go nodes 1000\n"
|
||||
expect "bestmove"
|
||||
|
||||
send "position fen 5rk1/1K4p1/8/8/3B4/8/8/8 b - - 0 1\n"
|
||||
send "go depth 30\n"
|
||||
expect "bestmove"
|
||||
|
||||
send "quit\n"
|
||||
expect eof
|
||||
|
||||
# return error code of the spawned program, useful for valgrind
|
||||
lassign [wait] pid spawnid os_error_flag value
|
||||
exit \$value
|
||||
EOF
|
||||
|
||||
for exps in game.exp
|
||||
do
|
||||
|
||||
echo "$prefix expect $exps $postfix"
|
||||
eval "$prefix expect $exps $postfix"
|
||||
|
||||
rm $exps
|
||||
|
||||
done
|
||||
|
||||
rm -f tsan.supp
|
||||
|
||||
echo "instrumented testing OK"
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
# verify perft numbers (positions from https://chessprogramming.wikispaces.com/Perft+Results)
|
||||
|
||||
error()
|
||||
{
|
||||
echo "perft testing failed on line $1"
|
||||
exit 1
|
||||
}
|
||||
trap 'error ${LINENO}' ERR
|
||||
|
||||
echo "perft testing started"
|
||||
|
||||
cat << EOF > perft.exp
|
||||
set timeout 10
|
||||
lassign \$argv pos depth result
|
||||
spawn ./stockfish
|
||||
send "position \$pos\\ngo perft \$depth\\n"
|
||||
expect "Nodes searched? \$result" {} timeout {exit 1}
|
||||
send "quit\\n"
|
||||
expect eof
|
||||
EOF
|
||||
|
||||
expect perft.exp startpos 5 4865609 > /dev/null
|
||||
expect perft.exp "fen r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -" 5 193690690 > /dev/null
|
||||
expect perft.exp "fen 8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -" 6 11030083 > /dev/null
|
||||
expect perft.exp "fen r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1" 5 15833292 > /dev/null
|
||||
expect perft.exp "fen rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8" 5 89941194 > /dev/null
|
||||
expect perft.exp "fen r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10" 5 164075551 > /dev/null
|
||||
|
||||
rm perft.exp
|
||||
|
||||
echo "perft testing OK"
|
||||
Executable
+61
@@ -0,0 +1,61 @@
|
||||
#!/bin/bash
|
||||
# verify reproducible search
|
||||
|
||||
error()
|
||||
{
|
||||
echo "reprosearch testing failed on line $1"
|
||||
exit 1
|
||||
}
|
||||
trap 'error ${LINENO}' ERR
|
||||
|
||||
echo "reprosearch testing started"
|
||||
|
||||
# repeat two short games, separated by ucinewgame.
|
||||
# with go nodes $nodes they should result in exactly
|
||||
# the same node count for each iteration.
|
||||
cat << EOF > repeat.exp
|
||||
set timeout 10
|
||||
spawn ./stockfish
|
||||
lassign \$argv nodes
|
||||
|
||||
send "uci\n"
|
||||
expect "uciok"
|
||||
|
||||
send "ucinewgame\n"
|
||||
send "position startpos\n"
|
||||
send "go nodes \$nodes\n"
|
||||
expect "bestmove"
|
||||
|
||||
send "position startpos moves e2e4 e7e6\n"
|
||||
send "go nodes \$nodes\n"
|
||||
expect "bestmove"
|
||||
|
||||
send "ucinewgame\n"
|
||||
send "position startpos\n"
|
||||
send "go nodes \$nodes\n"
|
||||
expect "bestmove"
|
||||
|
||||
send "position startpos moves e2e4 e7e6\n"
|
||||
send "go nodes \$nodes\n"
|
||||
expect "bestmove"
|
||||
|
||||
send "quit\n"
|
||||
expect eof
|
||||
EOF
|
||||
|
||||
# to increase the likelyhood of finding a non-reproducible case,
|
||||
# the allowed number of nodes are varied systematically
|
||||
for i in `seq 1 20`
|
||||
do
|
||||
|
||||
nodes=$((100*3**i/2**i))
|
||||
echo "reprosearch testing with $nodes nodes"
|
||||
|
||||
# each line should appear exactly an even number of times
|
||||
expect repeat.exp $nodes 2>&1 | grep -o "nodes [0-9]*" | sort | uniq -c | awk '{if ($1%2!=0) exit(1)}'
|
||||
|
||||
done
|
||||
|
||||
rm repeat.exp
|
||||
|
||||
echo "reprosearch testing OK"
|
||||
Executable
+27
@@ -0,0 +1,27 @@
|
||||
#!/bin/bash
|
||||
# obtain and optionally verify Bench / signature
|
||||
# if no reference is given, the output is deliberately limited to just the signature
|
||||
|
||||
error()
|
||||
{
|
||||
echo "running bench for signature failed on line $1"
|
||||
exit 1
|
||||
}
|
||||
trap 'error ${LINENO}' ERR
|
||||
|
||||
# obtain
|
||||
|
||||
signature=`./stockfish bench 2>&1 | grep "Nodes searched : " | awk '{print $4}'`
|
||||
|
||||
if [ $# -gt 0 ]; then
|
||||
# compare to given reference
|
||||
if [ "$1" != "$signature" ]; then
|
||||
echo "signature mismatch: reference $1 obtained $signature"
|
||||
exit 1
|
||||
else
|
||||
echo "signature OK: $signature"
|
||||
fi
|
||||
else
|
||||
# just report signature
|
||||
echo $signature
|
||||
fi
|
||||
Reference in New Issue
Block a user