mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 16:47:37 +00:00
Compare commits
402 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 758f9c9350 | |||
| d60f5de967 | |||
| c7118fb46d | |||
| 6a6faac04d | |||
| f5a31b7e57 | |||
| 1370127fcd | |||
| 85ae65db1d | |||
| d8f3209fb4 | |||
| 3411631162 | |||
| d756d97a66 | |||
| 41c6a74d37 | |||
| 219fa2f0a7 | |||
| 6c1df553fa | |||
| a413900791 | |||
| e048d11825 | |||
| ad2aa8c06f | |||
| 61a2cb84a6 | |||
| d09653df0d | |||
| f154ed7a2d | |||
| 8333b2a94c | |||
| a5500edc55 | |||
| 4ec8945eaf | |||
| 5604b255e6 | |||
| 804394b939 | |||
| 234d2156fd | |||
| 79c5f3a692 | |||
| 9be2977da7 | |||
| d6b6360ff5 | |||
| f97a86e213 | |||
| 93f71ecfe1 | |||
| e90341f9c9 | |||
| d5271af0ee | |||
| da937e219e | |||
| 8bab09749d | |||
| f436bf77ad | |||
| 232bf19be4 | |||
| 4339a756ac | |||
| 70e51a5bc8 | |||
| 29295ecfd3 | |||
| dc0c441b7c | |||
| 154e7afed0 | |||
| 5a871e174f | |||
| 82bb21dc7a | |||
| 1591e5ac3b | |||
| 9fa258ee1d | |||
| eaf2c8207f | |||
| 5eeb96d0e7 | |||
| a4d18d23a9 | |||
| dddf8fc2b4 | |||
| 97860cb575 | |||
| 02ef1f4496 | |||
| 15ac117ac4 | |||
| 5f290352cd | |||
| 3370f69881 | |||
| 4568f6369b | |||
| 1054a483ca | |||
| 0a01dd044f | |||
| e639c45577 | |||
| 7cc929f437 | |||
| b8f4903fbb | |||
| 675f6a038b | |||
| 582c88ee94 | |||
| 18389e269d | |||
| c4a644922d | |||
| 4b4b7d1209 | |||
| 95d24b77df | |||
| 2e02dd7936 | |||
| aa18b68033 | |||
| c2aaaa65f9 | |||
| 85f8ee6199 | |||
| 442c40b43d | |||
| 5304b561ab | |||
| 4d6a11a04c | |||
| 6edc29d720 | |||
| 2d5dcf3d18 | |||
| d54b85b4bd | |||
| 00297cfef0 | |||
| 809849fa27 | |||
| 90cf8e7d2b | |||
| 7f1333ccf8 | |||
| 653bd0817c | |||
| 8fadbcf1b2 | |||
| 4c7de9e8ab | |||
| 6ede1bed89 | |||
| 1a168201bd | |||
| 48df0754bc | |||
| f7d1491b3d | |||
| cc7bcd5303 | |||
| 22b7909809 | |||
| 5372f81cc8 | |||
| c079acc26f | |||
| 9eb7b607cf | |||
| a32d2086bc | |||
| 285a79eaa0 | |||
| e1f12aa4e6 | |||
| e41f727f0f | |||
| 6e0680efa0 | |||
| c4db7fd1f9 | |||
| e6e324eb28 | |||
| df2f7e7527 | |||
| c25d4c4887 | |||
| c3b67faf98 | |||
| 319af5cf0a | |||
| 19a90b45bc | |||
| 9f6bcb38c0 | |||
| 471d93063a | |||
| 08e0f52b77 | |||
| 910cf8b218 | |||
| e31f97e3ba | |||
| f3a2296e59 | |||
| 004ea2c25e | |||
| 45f2416db4 | |||
| eae0f8dd06 | |||
| 270a0e737f | |||
| 4ac7d726ec | |||
| 174b038bf3 | |||
| 5f781d366e | |||
| 27139dedac | |||
| abef3e86f4 | |||
| 2da1d1bf57 | |||
| 84b1940fca | |||
| 3ec6e1d245 | |||
| cb9c2594fc | |||
| b0b31558a2 | |||
| 08ac4e9db5 | |||
| 4d3950c6eb | |||
| 95d7369e54 | |||
| e178a09c47 | |||
| 50200de5af | |||
| 90d051952f | |||
| 8b4afcf8f7 | |||
| bddd38c45e | |||
| 9083050be6 | |||
| 77cf5704b6 | |||
| 67062637f4 | |||
| 48bf1a386f | |||
| 2b0372319d | |||
| d11101e4c6 | |||
| 7678d63cf2 | |||
| c5d45d3220 | |||
| 44b1ba89a9 | |||
| c5a280c012 | |||
| 9ad0ea7382 | |||
| 2efda17c2a | |||
| ad926d34c0 | |||
| 0b41887527 | |||
| 061f98a9e3 | |||
| 1066119083 | |||
| 93b14a17d1 | |||
| 7d82f0d1f4 | |||
| 0a6168089d | |||
| 88f17a814d | |||
| 22e92d23d2 | |||
| 2c30956a13 | |||
| 74776dbcd5 | |||
| ca51b45649 | |||
| fb7d3ab32e | |||
| 0a318cdddf | |||
| 939b694bfd | |||
| dc5d9bdfee | |||
| 0889210262 | |||
| 3bea736a2a | |||
| c6edf33f53 | |||
| ea1ddb6aef | |||
| d579db34a3 | |||
| 9db6ca8592 | |||
| 8e82345931 | |||
| 9451419912 | |||
| c228f3196a | |||
| 4766dfc395 | |||
| b82d93ece4 | |||
| a3d425cf55 | |||
| 7d44b43b3c | |||
| 18f2b12cd0 | |||
| a6a9d828ab | |||
| 327060232a | |||
| e4b7403f12 | |||
| c9977aa0a8 | |||
| c1f9a359e8 | |||
| 95a2ac1e07 | |||
| 4b86ef8c4f | |||
| 64f21ecdae | |||
| 282644f141 | |||
| ca3c1c5f3a | |||
| e4a0c6c759 | |||
| af050e5eed | |||
| 8bb5a436b2 | |||
| 4bb11e823f | |||
| 9ee58dc7a7 | |||
| 0ac8aca893 | |||
| 092b27a6d0 | |||
| 7218ec4df9 | |||
| a943b1d28d | |||
| a5a89b27c8 | |||
| f5df517145 | |||
| 9048ac00db | |||
| 1a5c21dc56 | |||
| c4a1390f4e | |||
| 7b278aab9f | |||
| a0259d8ab9 | |||
| 45e5e65a28 | |||
| c2b9134c6e | |||
| 11c6cf720d | |||
| 5a223afe4c | |||
| ef4822aa8d | |||
| 0e89d6e754 | |||
| a8330d5c3b | |||
| 717d6c5ed5 | |||
| 7262fd5d14 | |||
| 385deefd80 | |||
| 2c86ae196d | |||
| 8557f35aa5 | |||
| 1163d972a9 | |||
| fc8213c7df | |||
| 927a84d310 | |||
| 2214fcecf7 | |||
| 644f6d4790 | |||
| 8a8640a761 | |||
| 42a895d9c9 | |||
| 4af1ae82c6 | |||
| b37054c310 | |||
| 67d0616483 | |||
| f7494961de | |||
| 8a74c08928 | |||
| 6847be2c75 | |||
| 4231d99ab4 | |||
| 580698e5e5 | |||
| 0bddd942b4 | |||
| 673841301b | |||
| c8459b18ba | |||
| f21a66f70d | |||
| 54a989930e | |||
| 329bdbd9cf | |||
| 371b522e9e | |||
| 135caee606 | |||
| 21ad356c09 | |||
| 919da65d70 | |||
| 00e34a758f | |||
| ff3fa0c664 | |||
| 73018a0337 | |||
| e8788d1b32 | |||
| 5b47b4e6c0 | |||
| 723f48dec0 | |||
| fd5e77950e | |||
| 30fdbf4328 | |||
| b7b6b4ba18 | |||
| c31fc8d163 | |||
| be63ce1bb5 | |||
| e404a7d97c | |||
| 2807dcfab6 | |||
| ad357e147a | |||
| 69eede7d08 | |||
| f30f231cbf | |||
| af0d82792e | |||
| d754ea50a8 | |||
| 590447d7a1 | |||
| 939ffe454d | |||
| e57d2d9d47 | |||
| 18dcf1f097 | |||
| ccf0239bc4 | |||
| 1946a67567 | |||
| f10ebc2bdf | |||
| d61d38586e | |||
| dabaf2220f | |||
| a1a83f3869 | |||
| 73ef5b8c4a | |||
| 5cd42f6b0b | |||
| 31ebd918ea | |||
| a0fca67da4 | |||
| 26edf9534a | |||
| e973eee919 | |||
| 237ed1ef8f | |||
| 910d26b5c3 | |||
| b939c80513 | |||
| a85928e7ec | |||
| 760b7462bc | |||
| d957179df7 | |||
| bc654257e7 | |||
| 36f8d3806b | |||
| dbd7f602d3 | |||
| f4986f4596 | |||
| 09b6d28391 | |||
| 8fc297c506 | |||
| 516ad1c9bf | |||
| ec8dfe7315 | |||
| d297d1d8a7 | |||
| b51b094419 | |||
| 7cfc1f9b15 | |||
| 773dff0209 | |||
| 2275923d3c | |||
| 49283d3a66 | |||
| b94a651878 | |||
| dc4983327d | |||
| e47b74457e | |||
| 0470bcef0e | |||
| 9b82414b67 | |||
| 2e2865d34b | |||
| ed436a36ba | |||
| 70ac5ecbb6 | |||
| ba01f4b954 | |||
| 2e745956c0 | |||
| 0171b506ec | |||
| adfb23c029 | |||
| 07e6ceacd6 | |||
| 86afb6a7cf | |||
| 14b673d90f | |||
| 07c8448034 | |||
| 55e69dc88d | |||
| 68bf362ea2 | |||
| 8ec9e10866 | |||
| 4c4e104cad | |||
| 900f249f59 | |||
| f8c779dbe5 | |||
| ce4c523ad3 | |||
| e1f181ee64 | |||
| 7819412002 | |||
| b84fa04db6 | |||
| 559942d64d | |||
| 785b708097 | |||
| 999e142c54 | |||
| 3802cdf9b6 | |||
| 98cbaa6c6b | |||
| 58307562b6 | |||
| 8f081c86f7 | |||
| 4445965f97 | |||
| 0b7cc8bd2f | |||
| 9353e72103 | |||
| d53071eff4 | |||
| 4ada291429 | |||
| 95f73ff393 | |||
| 9fd5b44d60 | |||
| e8418bb1b9 | |||
| 5448cad29e | |||
| 4c02998325 | |||
| 6174a37a37 | |||
| f193778446 | |||
| 1b325bf86d | |||
| 83e0af288a | |||
| 9d53129075 | |||
| e044068b43 | |||
| a2f01c07eb | |||
| ff4c22238a | |||
| 49c79aa15c | |||
| fb2d175f97 | |||
| f233ca1af4 | |||
| 754fc8a8b5 | |||
| 2c3f7619f9 | |||
| 6b9a70ace8 | |||
| 038487f954 | |||
| 0faf81d1f6 | |||
| d37de3cb1d | |||
| e8d64af123 | |||
| f90274d8ce | |||
| 61e1c66b7c | |||
| c82f6f56a6 | |||
| 24b8b3098b | |||
| bd756ee45c | |||
| 594e2ac999 | |||
| b62af7ac1e | |||
| a0e2debe3f | |||
| 602687801b | |||
| 58054fd0fa | |||
| d777ea79ff | |||
| ca250e969c | |||
| b1c8840f10 | |||
| 33fadb5118 | |||
| 6ad4f485d3 | |||
| 84b42b3ab3 | |||
| 33a858eaa1 | |||
| c0ff241464 | |||
| b748b46714 | |||
| 32d781769d | |||
| fbbd4adc3c | |||
| a7ab92ec25 | |||
| 255514fb29 | |||
| 14d162d9f4 | |||
| 4889cf22bb | |||
| 79bb28281c | |||
| 3dfda1b28e | |||
| f40913f7f6 | |||
| b862c8d4be | |||
| c489df6f5b | |||
| 62a0b65ff8 | |||
| f28303d214 | |||
| 83eac08e75 | |||
| ec42154ef2 | |||
| ace9632c67 | |||
| 5089061659 | |||
| d58e83695f | |||
| 830f597134 | |||
| 4b509559fb | |||
| 939395729c | |||
| f3b296c2e2 | |||
| b74274628c | |||
| 5346f1c6c7 | |||
| d4b864ff12 | |||
| 7ffae17f85 | |||
| 9b1274aba3 | |||
| 0f3f5d85fb | |||
| 7c30091a92 | |||
| 6294db7514 | |||
| a31007c9e7 |
@@ -0,0 +1,65 @@
|
|||||||
|
name: Report issue
|
||||||
|
description: Create a report to help us fix issues with the engine
|
||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Describe the issue
|
||||||
|
description: A clear and concise description of what you're experiencing.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Expected behavior
|
||||||
|
description: A clear and concise description of what you expected to happen.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Steps to reproduce
|
||||||
|
description: |
|
||||||
|
Steps to reproduce the behavior.
|
||||||
|
You can also use this section to paste the command line output.
|
||||||
|
placeholder: |
|
||||||
|
```
|
||||||
|
position startpos moves g2g4 e7e5 f2f3
|
||||||
|
go mate 1
|
||||||
|
info string NNUE evaluation using nn-6877cd24400e.nnue enabled
|
||||||
|
info depth 1 seldepth 1 multipv 1 score mate 1 nodes 33 nps 11000 tbhits 0 time 3 pv d8h4
|
||||||
|
bestmove d8h4
|
||||||
|
```
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Anything else?
|
||||||
|
description: |
|
||||||
|
Anything that will give us more context about the issue you are encountering.
|
||||||
|
You can also use this section to propose ideas on how to solve the issue.
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
|
||||||
|
- type: dropdown
|
||||||
|
attributes:
|
||||||
|
label: Operating system
|
||||||
|
options:
|
||||||
|
- All
|
||||||
|
- Windows
|
||||||
|
- Linux
|
||||||
|
- MacOS
|
||||||
|
- Android
|
||||||
|
- Other or N/A
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: input
|
||||||
|
attributes:
|
||||||
|
label: Stockfish version
|
||||||
|
description: |
|
||||||
|
This can be found by running the engine.
|
||||||
|
You can also use the commit ID.
|
||||||
|
placeholder: Stockfish 15 / e6e324e
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
blank_issues_enabled: false
|
||||||
|
contact_links:
|
||||||
|
- name: Discord server
|
||||||
|
url: https://discord.gg/GWDRS3kU6R
|
||||||
|
about: Feel free to ask for support or have a chat with us in our Discord server!
|
||||||
|
- name: Discussions, Q&A, ideas, show us something...
|
||||||
|
url: https://github.com/official-stockfish/Stockfish/discussions/new
|
||||||
|
about: Do you have an idea for Stockfish? Do you want to show something that you made? Please open a discussion about it!
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
name: Stockfish
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- tools
|
||||||
|
- github_ci
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- tools
|
||||||
|
jobs:
|
||||||
|
Sanitizers:
|
||||||
|
uses: ./.github/workflows/stockfish_sanitizers.yml
|
||||||
|
Tests:
|
||||||
|
uses: ./.github/workflows/stockfish_test.yml
|
||||||
|
Compiles:
|
||||||
|
uses: ./.github/workflows/stockfish_compile_test.yml
|
||||||
|
Binaries:
|
||||||
|
if: github.ref == 'refs/heads/master'
|
||||||
|
uses: ./.github/workflows/stockfish_binaries.yml
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
name: Stockfish
|
||||||
|
on:
|
||||||
|
workflow_call:
|
||||||
|
jobs:
|
||||||
|
Stockfish:
|
||||||
|
name: ${{ matrix.config.name }} ${{ matrix.binaries }}
|
||||||
|
runs-on: ${{ matrix.config.os }}
|
||||||
|
env:
|
||||||
|
COMPILER: ${{ matrix.config.compiler }}
|
||||||
|
COMP: ${{ matrix.config.comp }}
|
||||||
|
EXT: ${{ matrix.config.ext }}
|
||||||
|
OS: ${{ matrix.config.os }}
|
||||||
|
BINARY: ${{ matrix.binaries }}
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
config:
|
||||||
|
- {
|
||||||
|
name: "Ubuntu 20.04 GCC",
|
||||||
|
os: ubuntu-20.04,
|
||||||
|
compiler: g++,
|
||||||
|
comp: gcc,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "MacOS 12 Apple Clang",
|
||||||
|
os: macos-12,
|
||||||
|
compiler: clang++,
|
||||||
|
comp: clang,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "Windows 2022 Mingw-w64 GCC x86_64",
|
||||||
|
os: windows-2022,
|
||||||
|
compiler: g++,
|
||||||
|
comp: mingw,
|
||||||
|
msys_sys: 'mingw64',
|
||||||
|
msys_env: 'x86_64-gcc',
|
||||||
|
shell: 'msys2 {0}',
|
||||||
|
ext: .exe
|
||||||
|
}
|
||||||
|
binaries:
|
||||||
|
- x86-64
|
||||||
|
- x86-64-modern
|
||||||
|
- x86-64-avx2
|
||||||
|
exclude:
|
||||||
|
- binaries: x86-64-avx2
|
||||||
|
config: {os: macos-12}
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: src
|
||||||
|
shell: ${{ matrix.config.shell }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Download required linux packages
|
||||||
|
if: runner.os == 'Linux'
|
||||||
|
run: |
|
||||||
|
sudo apt update
|
||||||
|
|
||||||
|
- name: Setup msys and install required packages
|
||||||
|
if: runner.os == 'Windows'
|
||||||
|
uses: msys2/setup-msys2@v2
|
||||||
|
with:
|
||||||
|
msystem: ${{matrix.config.msys_sys}}
|
||||||
|
install: mingw-w64-${{matrix.config.msys_env}} make git expect
|
||||||
|
|
||||||
|
- name: Download the used network from the fishtest framework
|
||||||
|
run: |
|
||||||
|
make net
|
||||||
|
|
||||||
|
- name: Check compiler
|
||||||
|
run: |
|
||||||
|
export PATH=$PATH:$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin
|
||||||
|
$COMPILER -v
|
||||||
|
|
||||||
|
- name: Test help target
|
||||||
|
run: |
|
||||||
|
make help
|
||||||
|
|
||||||
|
# Compile profile guided builds
|
||||||
|
|
||||||
|
- name: Compile ${{ matrix.binaries }} build
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 profile-build ARCH=$BINARY COMP=$COMP
|
||||||
|
strip ./stockfish$EXT
|
||||||
|
mv ./stockfish$EXT ../stockfish-$OS-$BINARY$EXT
|
||||||
|
|
||||||
|
- name: Remove non src files
|
||||||
|
run: rm -f *.o .depend *.nnue
|
||||||
|
|
||||||
|
- name: Create tar archive.
|
||||||
|
run: |
|
||||||
|
cd ..
|
||||||
|
mkdir stockfish
|
||||||
|
cp -r src stockfish/
|
||||||
|
cp stockfish-$OS-$BINARY$EXT stockfish/
|
||||||
|
cp "Top CPU Contributors.txt" stockfish/
|
||||||
|
cp Copying.txt stockfish/
|
||||||
|
cp AUTHORS stockfish/
|
||||||
|
tar -cvf stockfish-$OS-$BINARY.tar stockfish
|
||||||
|
|
||||||
|
- name: Upload binaries
|
||||||
|
uses: actions/upload-artifact@v3
|
||||||
|
with:
|
||||||
|
name: stockfish-${{ matrix.config.os }}-${{ matrix.binaries }}
|
||||||
|
path: |
|
||||||
|
stockfish-${{ matrix.config.os }}-${{ matrix.binaries }}.tar
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
name: Stockfish
|
||||||
|
on:
|
||||||
|
workflow_call:
|
||||||
|
jobs:
|
||||||
|
Stockfish:
|
||||||
|
name: ${{ matrix.config.name }}
|
||||||
|
runs-on: ${{ matrix.config.os }}
|
||||||
|
env:
|
||||||
|
COMPILER: ${{ matrix.config.compiler }}
|
||||||
|
COMP: ${{ matrix.config.comp }}
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
config:
|
||||||
|
- {
|
||||||
|
name: "Ubuntu 20.04 GCC",
|
||||||
|
os: ubuntu-20.04,
|
||||||
|
compiler: g++,
|
||||||
|
comp: gcc,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "Ubuntu 20.04 Clang",
|
||||||
|
os: ubuntu-20.04,
|
||||||
|
compiler: clang++,
|
||||||
|
comp: clang,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "MacOS 12 Apple Clang",
|
||||||
|
os: macos-12,
|
||||||
|
compiler: clang++,
|
||||||
|
comp: clang,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "MacOS 12 GCC 11",
|
||||||
|
os: macos-12,
|
||||||
|
compiler: g++-11,
|
||||||
|
comp: gcc,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "Windows 2022 Mingw-w64 GCC x86_64",
|
||||||
|
os: windows-2022,
|
||||||
|
compiler: g++,
|
||||||
|
comp: mingw,
|
||||||
|
msys_sys: 'mingw64',
|
||||||
|
msys_env: 'x86_64-gcc',
|
||||||
|
shell: 'msys2 {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "Windows 2022 Mingw-w64 Clang x86_64",
|
||||||
|
os: windows-2022,
|
||||||
|
compiler: clang++,
|
||||||
|
comp: clang,
|
||||||
|
msys_sys: 'clang64',
|
||||||
|
msys_env: 'clang-x86_64-clang',
|
||||||
|
shell: 'msys2 {0}'
|
||||||
|
}
|
||||||
|
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: src
|
||||||
|
shell: ${{ matrix.config.shell }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Setup msys and install required packages
|
||||||
|
if: runner.os == 'Windows'
|
||||||
|
uses: msys2/setup-msys2@v2
|
||||||
|
with:
|
||||||
|
msystem: ${{matrix.config.msys_sys}}
|
||||||
|
install: mingw-w64-${{matrix.config.msys_env}} make git expect
|
||||||
|
|
||||||
|
- name: Download the used network from the fishtest framework
|
||||||
|
run: |
|
||||||
|
make net
|
||||||
|
|
||||||
|
- name: Check compiler
|
||||||
|
run: |
|
||||||
|
export PATH=$PATH:$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin
|
||||||
|
$COMPILER -v
|
||||||
|
|
||||||
|
- name: Test help target
|
||||||
|
run: |
|
||||||
|
make help
|
||||||
|
|
||||||
|
# x86-64 with newer extensions tests
|
||||||
|
|
||||||
|
- name: Compile x86-64-avx2 build
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-64-avx2 build
|
||||||
|
|
||||||
|
- name: Compile x86-64-bmi2 build
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-64-bmi2 build
|
||||||
|
|
||||||
|
- name: Compile x86-64-avx512 build
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-64-avx512 build
|
||||||
|
|
||||||
|
- name: Compile x86-64-vnni512 build
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-64-vnni512 build
|
||||||
|
|
||||||
|
- name: Compile x86-64-vnni256 build
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-64-vnni256 build
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
name: Stockfish
|
||||||
|
on:
|
||||||
|
workflow_call:
|
||||||
|
jobs:
|
||||||
|
Stockfish:
|
||||||
|
name: ${{ matrix.sanitizers.name }}
|
||||||
|
runs-on: ${{ matrix.config.os }}
|
||||||
|
env:
|
||||||
|
COMPILER: ${{ matrix.config.compiler }}
|
||||||
|
COMP: ${{ matrix.config.comp }}
|
||||||
|
CXXFLAGS: "-Werror"
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
config:
|
||||||
|
- {
|
||||||
|
name: "Ubuntu 20.04 GCC",
|
||||||
|
os: ubuntu-20.04,
|
||||||
|
compiler: g++,
|
||||||
|
comp: gcc,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
sanitizers:
|
||||||
|
- {
|
||||||
|
name: Run with thread sanitizer,
|
||||||
|
make_option: sanitize=thread,
|
||||||
|
instrumented_option: sanitizer-thread
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: Run with UB sanitizer,
|
||||||
|
make_option: sanitize=undefined,
|
||||||
|
instrumented_option: sanitizer-undefined
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: Run under valgrind,
|
||||||
|
make_option: "",
|
||||||
|
instrumented_option: valgrind
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: Run under valgrind-thread,
|
||||||
|
make_option: "",
|
||||||
|
instrumented_option: valgrind-thread
|
||||||
|
}
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: src
|
||||||
|
shell: ${{ matrix.config.shell }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Download required linux packages
|
||||||
|
run: |
|
||||||
|
sudo apt update
|
||||||
|
sudo apt install expect valgrind g++-multilib qemu-user
|
||||||
|
|
||||||
|
- name: Download the used network from the fishtest framework
|
||||||
|
run: |
|
||||||
|
make net
|
||||||
|
|
||||||
|
- name: Check compiler
|
||||||
|
run: |
|
||||||
|
export PATH=$PATH:$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin
|
||||||
|
$COMPILER -v
|
||||||
|
|
||||||
|
- name: Test help target
|
||||||
|
run: |
|
||||||
|
make help
|
||||||
|
|
||||||
|
# Sanitizers
|
||||||
|
|
||||||
|
- name: ${{ matrix.sanitizers.name }}
|
||||||
|
run: |
|
||||||
|
export CXXFLAGS="-O1 -fno-inline"
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-64-modern ${{ matrix.sanitizers.make_option }} debug=yes optimize=no build > /dev/null
|
||||||
|
../tests/instrumented.sh --${{ matrix.sanitizers.instrumented_option }}
|
||||||
@@ -0,0 +1,284 @@
|
|||||||
|
name: Stockfish
|
||||||
|
on:
|
||||||
|
workflow_call:
|
||||||
|
jobs:
|
||||||
|
Stockfish:
|
||||||
|
name: ${{ matrix.config.name }}
|
||||||
|
runs-on: ${{ matrix.config.os }}
|
||||||
|
env:
|
||||||
|
COMPILER: ${{ matrix.config.compiler }}
|
||||||
|
COMP: ${{ matrix.config.comp }}
|
||||||
|
CXXFLAGS: "-Werror"
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
config:
|
||||||
|
- {
|
||||||
|
name: "Ubuntu 20.04 GCC",
|
||||||
|
os: ubuntu-20.04,
|
||||||
|
compiler: g++,
|
||||||
|
comp: gcc,
|
||||||
|
run_32bit_tests: true,
|
||||||
|
run_64bit_tests: true,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "Ubuntu 20.04 Clang",
|
||||||
|
os: ubuntu-20.04,
|
||||||
|
compiler: clang++,
|
||||||
|
comp: clang,
|
||||||
|
run_32bit_tests: true,
|
||||||
|
run_64bit_tests: true,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "Ubuntu 20.04 NDK armv8",
|
||||||
|
os: ubuntu-20.04,
|
||||||
|
compiler: aarch64-linux-android21-clang++,
|
||||||
|
comp: ndk,
|
||||||
|
run_armv8_tests: false,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "Ubuntu 20.04 NDK armv7",
|
||||||
|
os: ubuntu-20.04,
|
||||||
|
compiler: armv7a-linux-androideabi21-clang++,
|
||||||
|
comp: ndk,
|
||||||
|
run_armv7_tests: false,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "MacOS 12 Apple Clang",
|
||||||
|
os: macos-12,
|
||||||
|
compiler: clang++,
|
||||||
|
comp: clang,
|
||||||
|
run_64bit_tests: true,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "MacOS 12 GCC 11",
|
||||||
|
os: macos-12,
|
||||||
|
compiler: g++-11,
|
||||||
|
comp: gcc,
|
||||||
|
run_64bit_tests: true,
|
||||||
|
shell: 'bash {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "Windows 2022 Mingw-w64 GCC x86_64",
|
||||||
|
os: windows-2022,
|
||||||
|
compiler: g++,
|
||||||
|
comp: mingw,
|
||||||
|
run_64bit_tests: true,
|
||||||
|
msys_sys: 'mingw64',
|
||||||
|
msys_env: 'x86_64-gcc',
|
||||||
|
shell: 'msys2 {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "Windows 2022 Mingw-w64 GCC i686",
|
||||||
|
os: windows-2022,
|
||||||
|
compiler: g++,
|
||||||
|
comp: mingw,
|
||||||
|
run_32bit_tests: true,
|
||||||
|
msys_sys: 'mingw32',
|
||||||
|
msys_env: 'i686-gcc',
|
||||||
|
shell: 'msys2 {0}'
|
||||||
|
}
|
||||||
|
- {
|
||||||
|
name: "Windows 2022 Mingw-w64 Clang x86_64",
|
||||||
|
os: windows-2022,
|
||||||
|
compiler: clang++,
|
||||||
|
comp: clang,
|
||||||
|
run_64bit_tests: true,
|
||||||
|
msys_sys: 'clang64',
|
||||||
|
msys_env: 'clang-x86_64-clang',
|
||||||
|
shell: 'msys2 {0}'
|
||||||
|
}
|
||||||
|
exclude:
|
||||||
|
- config:
|
||||||
|
{
|
||||||
|
name: "Ubuntu 20.04 NDK armv7"
|
||||||
|
}
|
||||||
|
- config:
|
||||||
|
{
|
||||||
|
name: "Ubuntu 20.04 NDK armv8"
|
||||||
|
}
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: src
|
||||||
|
shell: ${{ matrix.config.shell }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Download required linux packages
|
||||||
|
if: runner.os == 'Linux'
|
||||||
|
run: |
|
||||||
|
sudo apt update
|
||||||
|
sudo apt install expect valgrind g++-multilib qemu-user
|
||||||
|
|
||||||
|
- name: Setup msys and install required packages
|
||||||
|
if: runner.os == 'Windows'
|
||||||
|
uses: msys2/setup-msys2@v2
|
||||||
|
with:
|
||||||
|
msystem: ${{matrix.config.msys_sys}}
|
||||||
|
install: mingw-w64-${{matrix.config.msys_env}} make git expect
|
||||||
|
|
||||||
|
- name: Download the used network from the fishtest framework
|
||||||
|
run: |
|
||||||
|
make net
|
||||||
|
|
||||||
|
- name: Extract the bench number from the commit history
|
||||||
|
run: |
|
||||||
|
git log HEAD | grep "\b[Bb]ench[ :]\+[0-9]\{7\}" | head -n 1 | sed "s/[^0-9]*\([0-9]*\).*/\1/g" > git_sig
|
||||||
|
[ -s git_sig ] && echo "benchref=$(cat git_sig)" >> $GITHUB_ENV && echo "Reference bench:" $(cat git_sig) || echo "No bench found"
|
||||||
|
|
||||||
|
- name: Check compiler
|
||||||
|
run: |
|
||||||
|
export PATH=$PATH:$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin
|
||||||
|
$COMPILER -v
|
||||||
|
|
||||||
|
- name: Test help target
|
||||||
|
run: |
|
||||||
|
make help
|
||||||
|
|
||||||
|
# x86-32 tests
|
||||||
|
|
||||||
|
- name: Test debug x86-32 build
|
||||||
|
if: ${{ matrix.config.run_32bit_tests }}
|
||||||
|
run: |
|
||||||
|
export CXXFLAGS="-Werror -D_GLIBCXX_DEBUG"
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-32 optimize=no debug=yes build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
- name: Test x86-32 build
|
||||||
|
if: ${{ matrix.config.run_32bit_tests }}
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-32 build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
- name: Test x86-32-sse41-popcnt build
|
||||||
|
if: ${{ matrix.config.run_32bit_tests }}
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-32-sse41-popcnt build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
- name: Test x86-32-sse2 build
|
||||||
|
if: ${{ matrix.config.run_32bit_tests }}
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-32-sse2 build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
- name: Test general-32 build
|
||||||
|
if: ${{ matrix.config.run_32bit_tests }}
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=general-32 build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
# x86-64 tests
|
||||||
|
|
||||||
|
- name: Test debug x86-64-modern build
|
||||||
|
if: ${{ matrix.config.run_64bit_tests }}
|
||||||
|
run: |
|
||||||
|
export CXXFLAGS="-Werror -D_GLIBCXX_DEBUG"
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-64-modern optimize=no debug=yes build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
- name: Test x86-64-modern build
|
||||||
|
if: ${{ matrix.config.run_64bit_tests }}
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-64-modern build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
- name: Test x86-64-ssse3 build
|
||||||
|
if: ${{ matrix.config.run_64bit_tests }}
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-64-ssse3 build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
- name: Test x86-64-sse3-popcnt build
|
||||||
|
if: ${{ matrix.config.run_64bit_tests }}
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-64-sse3-popcnt build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
- name: Test x86-64 build
|
||||||
|
if: ${{ matrix.config.run_64bit_tests }}
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-64 build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
- name: Test general-64 build
|
||||||
|
if: matrix.config.run_64bit_tests
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=general-64 build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
# armv8 tests
|
||||||
|
|
||||||
|
- name: Test armv8 build
|
||||||
|
if: ${{ matrix.config.run_armv8_tests }}
|
||||||
|
run: |
|
||||||
|
ANDROID_ROOT=/usr/local/lib/android
|
||||||
|
ANDROID_SDK_ROOT=${ANDROID_ROOT}/sdk
|
||||||
|
SDKMANAGER=${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin/sdkmanager
|
||||||
|
echo "y" | $SDKMANAGER "ndk;21.4.7075529"
|
||||||
|
ANDROID_NDK_ROOT=${ANDROID_SDK_ROOT}/ndk-bundle
|
||||||
|
ln -sfn $ANDROID_SDK_ROOT/ndk/21.4.7075529 $ANDROID_NDK_ROOT
|
||||||
|
export PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH
|
||||||
|
export LDFLAGS="-static -Wno-unused-command-line-argument"
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=armv8 build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
# armv7 tests
|
||||||
|
|
||||||
|
- name: Test armv7 build
|
||||||
|
if: ${{ matrix.config.run_armv7_tests }}
|
||||||
|
run: |
|
||||||
|
ANDROID_ROOT=/usr/local/lib/android
|
||||||
|
ANDROID_SDK_ROOT=${ANDROID_ROOT}/sdk
|
||||||
|
SDKMANAGER=${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin/sdkmanager
|
||||||
|
echo "y" | $SDKMANAGER "ndk;21.4.7075529"
|
||||||
|
ANDROID_NDK_ROOT=${ANDROID_SDK_ROOT}/ndk-bundle
|
||||||
|
ln -sfn $ANDROID_SDK_ROOT/ndk/21.4.7075529 $ANDROID_NDK_ROOT
|
||||||
|
export PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH
|
||||||
|
export LDFLAGS="-static -Wno-unused-command-line-argument"
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=armv7 build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
- name: Test armv7-neon build
|
||||||
|
if: ${{ matrix.config.run_armv7_tests }}
|
||||||
|
run: |
|
||||||
|
ANDROID_ROOT=/usr/local/lib/android
|
||||||
|
ANDROID_SDK_ROOT=${ANDROID_ROOT}/sdk
|
||||||
|
SDKMANAGER=${ANDROID_SDK_ROOT}/cmdline-tools/latest/bin/sdkmanager
|
||||||
|
echo "y" | $SDKMANAGER "ndk;21.4.7075529"
|
||||||
|
ANDROID_NDK_ROOT=${ANDROID_SDK_ROOT}/ndk-bundle
|
||||||
|
ln -sfn $ANDROID_SDK_ROOT/ndk/21.4.7075529 $ANDROID_NDK_ROOT
|
||||||
|
export PATH=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH
|
||||||
|
export LDFLAGS="-static -Wno-unused-command-line-argument"
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=armv7-neon build
|
||||||
|
../tests/signature.sh $benchref
|
||||||
|
|
||||||
|
# Other tests
|
||||||
|
|
||||||
|
- name: Check perft and search reproducibility
|
||||||
|
if: ${{ matrix.config.run_64bit_tests }}
|
||||||
|
run: |
|
||||||
|
make clean
|
||||||
|
make -j2 ARCH=x86-64-modern build
|
||||||
|
../tests/perft.sh
|
||||||
|
../tests/reprosearch.sh
|
||||||
-101
@@ -1,101 +0,0 @@
|
|||||||
language: cpp
|
|
||||||
dist: bionic
|
|
||||||
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- os: linux
|
|
||||||
compiler: gcc
|
|
||||||
addons:
|
|
||||||
apt:
|
|
||||||
packages: ['g++-8', 'g++-8-multilib', 'g++-multilib', 'valgrind', 'expect', 'curl']
|
|
||||||
env:
|
|
||||||
- COMPILER=g++-8
|
|
||||||
- COMP=gcc
|
|
||||||
|
|
||||||
- os: linux
|
|
||||||
compiler: clang
|
|
||||||
addons:
|
|
||||||
apt:
|
|
||||||
packages: ['clang-10', 'llvm-10-dev', 'g++-multilib', 'valgrind', 'expect', 'curl']
|
|
||||||
env:
|
|
||||||
- COMPILER=clang++-10
|
|
||||||
- COMP=clang
|
|
||||||
|
|
||||||
- os: osx
|
|
||||||
osx_image: xcode12
|
|
||||||
compiler: gcc
|
|
||||||
env:
|
|
||||||
- COMPILER=g++
|
|
||||||
- COMP=gcc
|
|
||||||
|
|
||||||
- os: osx
|
|
||||||
osx_image: xcode12
|
|
||||||
compiler: clang
|
|
||||||
env:
|
|
||||||
- COMPILER=clang++
|
|
||||||
- COMP=clang
|
|
||||||
|
|
||||||
branches:
|
|
||||||
only:
|
|
||||||
- master
|
|
||||||
|
|
||||||
before_script:
|
|
||||||
- cd src
|
|
||||||
|
|
||||||
script:
|
|
||||||
# Download net
|
|
||||||
- make net
|
|
||||||
|
|
||||||
# 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]*\).*/\1/g" > git_sig
|
|
||||||
- export benchref=$(cat git_sig)
|
|
||||||
- echo "Reference bench:" $benchref
|
|
||||||
|
|
||||||
# Compiler version string
|
|
||||||
- $COMPILER -v
|
|
||||||
|
|
||||||
# test help target
|
|
||||||
- make help
|
|
||||||
|
|
||||||
# Verify bench number against various builds
|
|
||||||
- export CXXFLAGS="-Werror -D_GLIBCXX_DEBUG"
|
|
||||||
- make clean && make -j2 ARCH=x86-64-modern optimize=no debug=yes build && ../tests/signature.sh $benchref
|
|
||||||
- export CXXFLAGS="-Werror"
|
|
||||||
- make clean && make -j2 ARCH=x86-64-modern build && ../tests/signature.sh $benchref
|
|
||||||
- make clean && make -j2 ARCH=x86-64-ssse3 build && ../tests/signature.sh $benchref
|
|
||||||
- make clean && make -j2 ARCH=x86-64-sse3-popcnt build && ../tests/signature.sh $benchref
|
|
||||||
- make clean && make -j2 ARCH=x86-64 build && ../tests/signature.sh $benchref
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then make clean && make -j2 ARCH=general-64 build && ../tests/signature.sh $benchref; fi
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then make clean && make -j2 ARCH=x86-32 optimize=no debug=yes build && ../tests/signature.sh $benchref; fi
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then make clean && make -j2 ARCH=x86-32-sse41-popcnt build && ../tests/signature.sh $benchref; fi
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then make clean && make -j2 ARCH=x86-32-sse2 build && ../tests/signature.sh $benchref; fi
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then make clean && make -j2 ARCH=x86-32 build && ../tests/signature.sh $benchref; fi
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then make clean && make -j2 ARCH=general-32 build && ../tests/signature.sh $benchref; fi
|
|
||||||
# workaround: exclude a custom version of llvm+clang, which doesn't find llvm-profdata on ubuntu
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" != "linux" || "$COMP" == "gcc" ]]; then make clean && make -j2 ARCH=x86-64-modern profile-build && ../tests/signature.sh $benchref; fi
|
|
||||||
|
|
||||||
# compile only for some more advanced architectures (might not run in travis)
|
|
||||||
- make clean && make -j2 ARCH=x86-64-avx2 build
|
|
||||||
- make clean && make -j2 ARCH=x86-64-bmi2 build
|
|
||||||
- make clean && make -j2 ARCH=x86-64-avx512 build
|
|
||||||
- make clean && make -j2 ARCH=x86-64-vnni512 build
|
|
||||||
- make clean && make -j2 ARCH=x86-64-vnni256 build
|
|
||||||
|
|
||||||
#
|
|
||||||
# Check perft and reproducible search
|
|
||||||
- make clean && make -j2 ARCH=x86-64-modern build
|
|
||||||
- ../tests/perft.sh
|
|
||||||
- ../tests/reprosearch.sh
|
|
||||||
|
|
||||||
#
|
|
||||||
# Valgrind
|
|
||||||
#
|
|
||||||
- export CXXFLAGS="-O1 -fno-inline"
|
|
||||||
- if [ -x "$(command -v valgrind )" ]; then make clean && make -j2 ARCH=x86-64-modern 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
|
|
||||||
#
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then make clean && make -j2 ARCH=x86-64-modern sanitize=undefined optimize=no debug=yes build > /dev/null && ../tests/instrumented.sh --sanitizer-undefined; fi
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then make clean && make -j2 ARCH=x86-64-modern sanitize=thread optimize=no debug=yes build > /dev/null && ../tests/instrumented.sh --sanitizer-thread; fi
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
# List of authors for Stockfish, as of August 4, 2020
|
# List of authors for Stockfish
|
||||||
|
|
||||||
# Founders of the Stockfish project and fishtest infrastructure
|
# Founders of the Stockfish project and fishtest infrastructure
|
||||||
Tord Romstad (romstad)
|
Tord Romstad (romstad)
|
||||||
@@ -21,35 +21,45 @@ Alexander Kure
|
|||||||
Alexander Pagel (Lolligerhans)
|
Alexander Pagel (Lolligerhans)
|
||||||
Alfredo Menezes (lonfom169)
|
Alfredo Menezes (lonfom169)
|
||||||
Ali AlZhrani (Cooffe)
|
Ali AlZhrani (Cooffe)
|
||||||
|
Andrei Vetrov (proukornew)
|
||||||
Andrew Grant (AndyGrant)
|
Andrew Grant (AndyGrant)
|
||||||
Andrey Neporada (nepal)
|
Andrey Neporada (nepal)
|
||||||
Andy Duplain
|
Andy Duplain
|
||||||
|
Antoine Champion (antoinechampion)
|
||||||
Aram Tumanian (atumanian)
|
Aram Tumanian (atumanian)
|
||||||
Arjun Temurnikar
|
Arjun Temurnikar
|
||||||
|
Artem Solopiy (EntityFX)
|
||||||
Auguste Pop
|
Auguste Pop
|
||||||
Balint Pfliegel
|
Balint Pfliegel
|
||||||
|
Ben Chaney (Chaneybenjamini)
|
||||||
Ben Koshy (BKSpurgeon)
|
Ben Koshy (BKSpurgeon)
|
||||||
Bill Henry (VoyagerOne)
|
Bill Henry (VoyagerOne)
|
||||||
Bojun Guo (noobpwnftw, Nooby)
|
Bojun Guo (noobpwnftw, Nooby)
|
||||||
|
Boštjan Mejak (PedanticHacker)
|
||||||
braich
|
braich
|
||||||
Brian Sheppard (SapphireBrand, briansheppard-toast)
|
Brian Sheppard (SapphireBrand, briansheppard-toast)
|
||||||
Bruno de Melo Costa (BM123499)
|
Bruno de Melo Costa (BM123499)
|
||||||
|
Bruno Pellanda (pellanda)
|
||||||
Bryan Cross (crossbr)
|
Bryan Cross (crossbr)
|
||||||
candirufish
|
candirufish
|
||||||
Chess13234
|
Chess13234
|
||||||
Chris Cain (ceebo)
|
Chris Cain (ceebo)
|
||||||
|
clefrks
|
||||||
Dale Weiler (graphitemaster)
|
Dale Weiler (graphitemaster)
|
||||||
Dan Schmidt (dfannius)
|
Dan Schmidt (dfannius)
|
||||||
Daniel Axtens (daxtens)
|
Daniel Axtens (daxtens)
|
||||||
Daniel Dugovic (ddugovic)
|
Daniel Dugovic (ddugovic)
|
||||||
Dariusz Orzechowski (dorzechowski)
|
Dariusz Orzechowski (dorzechowski)
|
||||||
David Zar
|
David Zar
|
||||||
|
David (dav1312)
|
||||||
Daylen Yang (daylen)
|
Daylen Yang (daylen)
|
||||||
Deshawn Mohan-Smith (GoldenRare)
|
Deshawn Mohan-Smith (GoldenRare)
|
||||||
Dieter Dobbelaere (ddobbelaere)
|
Dieter Dobbelaere (ddobbelaere)
|
||||||
DiscanX
|
DiscanX
|
||||||
Dominik Schlösser (domschl)
|
Dominik Schlösser (domschl)
|
||||||
double-beep
|
double-beep
|
||||||
|
Douglas Matos Gomes (dsmsgms)
|
||||||
|
Dubslow
|
||||||
Eduardo Cáceres (eduherminio)
|
Eduardo Cáceres (eduherminio)
|
||||||
Eelco de Groot (KingDefender)
|
Eelco de Groot (KingDefender)
|
||||||
Elvin Liu (solarlight2)
|
Elvin Liu (solarlight2)
|
||||||
@@ -66,6 +76,7 @@ gamander
|
|||||||
Gary Heckman (gheckman)
|
Gary Heckman (gheckman)
|
||||||
George Sobala (gsobala)
|
George Sobala (gsobala)
|
||||||
gguliash
|
gguliash
|
||||||
|
Giacomo Lorenzetti (G-Lorenz)
|
||||||
Gian-Carlo Pascutto (gcp)
|
Gian-Carlo Pascutto (gcp)
|
||||||
Gontran Lemaire (gonlem)
|
Gontran Lemaire (gonlem)
|
||||||
Goodkov Vasiliy Aleksandrovich (goodkov)
|
Goodkov Vasiliy Aleksandrovich (goodkov)
|
||||||
@@ -80,7 +91,7 @@ Hongzhi Cheng
|
|||||||
Ivan Ivec (IIvec)
|
Ivan Ivec (IIvec)
|
||||||
Jacques B. (Timshel)
|
Jacques B. (Timshel)
|
||||||
Jan Ondruš (hxim)
|
Jan Ondruš (hxim)
|
||||||
Jared Kish (Kurtbusch)
|
Jared Kish (Kurtbusch, kurt22i)
|
||||||
Jarrod Torriero (DU-jdto)
|
Jarrod Torriero (DU-jdto)
|
||||||
Jean Gauthier (OuaisBla)
|
Jean Gauthier (OuaisBla)
|
||||||
Jean-Francois Romang (jromang)
|
Jean-Francois Romang (jromang)
|
||||||
@@ -93,16 +104,19 @@ Joost VandeVondele (vondele)
|
|||||||
Jörg Oster (joergoster)
|
Jörg Oster (joergoster)
|
||||||
Joseph Ellis (jhellis3)
|
Joseph Ellis (jhellis3)
|
||||||
Joseph R. Prostko
|
Joseph R. Prostko
|
||||||
|
Julian Willemer (NightlyKing)
|
||||||
jundery
|
jundery
|
||||||
Justin Blanchard (UncombedCoconut)
|
Justin Blanchard (UncombedCoconut)
|
||||||
Kelly Wilson
|
Kelly Wilson
|
||||||
Ken Takusagawa
|
Ken Takusagawa
|
||||||
|
Kian E (KJE-98)
|
||||||
kinderchocolate
|
kinderchocolate
|
||||||
Kiran Panditrao (Krgp)
|
Kiran Panditrao (Krgp)
|
||||||
Kojirion
|
Kojirion
|
||||||
Krystian Kuzniarek (kuzkry)
|
Krystian Kuzniarek (kuzkry)
|
||||||
Leonardo Ljubičić (ICCF World Champion)
|
Leonardo Ljubičić (ICCF World Champion)
|
||||||
Leonid Pechenik (lp--)
|
Leonid Pechenik (lp--)
|
||||||
|
Liam Keegan (lkeegan)
|
||||||
Linus Arver (listx)
|
Linus Arver (listx)
|
||||||
loco-loco
|
loco-loco
|
||||||
Lub van den Berg (ElbertoOne)
|
Lub van den Berg (ElbertoOne)
|
||||||
@@ -116,6 +130,7 @@ marotear
|
|||||||
Matt Ginsberg (mattginsberg)
|
Matt Ginsberg (mattginsberg)
|
||||||
Matthew Lai (matthewlai)
|
Matthew Lai (matthewlai)
|
||||||
Matthew Sullivan (Matt14916)
|
Matthew Sullivan (Matt14916)
|
||||||
|
Max A. (Disservin)
|
||||||
Maxim Molchanov (Maxim)
|
Maxim Molchanov (Maxim)
|
||||||
Michael An (man)
|
Michael An (man)
|
||||||
Michael Byrne (MichaelB7)
|
Michael Byrne (MichaelB7)
|
||||||
@@ -125,6 +140,7 @@ Michael Whiteley (protonspring)
|
|||||||
Michel Van den Bergh (vdbergh)
|
Michel Van den Bergh (vdbergh)
|
||||||
Miguel Lahoz (miguel-l)
|
Miguel Lahoz (miguel-l)
|
||||||
Mikael Bäckman (mbootsector)
|
Mikael Bäckman (mbootsector)
|
||||||
|
Mike Babigian (Farseer)
|
||||||
Mira
|
Mira
|
||||||
Miroslav Fontán (Hexik)
|
Miroslav Fontán (Hexik)
|
||||||
Moez Jellouli (MJZ1977)
|
Moez Jellouli (MJZ1977)
|
||||||
@@ -137,6 +153,7 @@ Nikolay Kostov (NikolayIT)
|
|||||||
Nguyen Pham (nguyenpham)
|
Nguyen Pham (nguyenpham)
|
||||||
Norman Schmidt (FireFather)
|
Norman Schmidt (FireFather)
|
||||||
notruck
|
notruck
|
||||||
|
Ofek Shochat (OfekShochat, ghostway)
|
||||||
Ondrej Mosnáček (WOnder93)
|
Ondrej Mosnáček (WOnder93)
|
||||||
Oskar Werkelin Ahlin
|
Oskar Werkelin Ahlin
|
||||||
Pablo Vazquez
|
Pablo Vazquez
|
||||||
@@ -144,19 +161,21 @@ Panthee
|
|||||||
Pascal Romaret
|
Pascal Romaret
|
||||||
Pasquale Pigazzini (ppigazzini)
|
Pasquale Pigazzini (ppigazzini)
|
||||||
Patrick Jansen (mibere)
|
Patrick Jansen (mibere)
|
||||||
pellanda
|
Peter Schneider (pschneider1968)
|
||||||
Peter Zsifkovits (CoffeeOne)
|
Peter Zsifkovits (CoffeeOne)
|
||||||
Praveen Kumar Tummala (praveentml)
|
Praveen Kumar Tummala (praveentml)
|
||||||
Rahul Dsilva (silversolver1)
|
Rahul Dsilva (silversolver1)
|
||||||
Ralph Stößer (Ralph Stoesser)
|
Ralph Stößer (Ralph Stoesser)
|
||||||
Raminder Singh
|
Raminder Singh
|
||||||
renouve
|
renouve
|
||||||
Reuven Peleg
|
Reuven Peleg (R-Peleg)
|
||||||
Richard Lloyd
|
Richard Lloyd (Richard-Lloyd)
|
||||||
Rodrigo Exterckötter Tjäder
|
Rodrigo Exterckötter Tjäder
|
||||||
|
Rodrigo Roim (roim)
|
||||||
Ron Britvich (Britvich)
|
Ron Britvich (Britvich)
|
||||||
Ronald de Man (syzygy1, syzygy)
|
Ronald de Man (syzygy1, syzygy)
|
||||||
rqs
|
rqs
|
||||||
|
Rui Coelho (ruicoelhopedro)
|
||||||
Ryan Schmitt
|
Ryan Schmitt
|
||||||
Ryan Takker
|
Ryan Takker
|
||||||
Sami Kiminki (skiminki)
|
Sami Kiminki (skiminki)
|
||||||
@@ -167,10 +186,13 @@ Sergio Vieri (sergiovieri)
|
|||||||
sf-x
|
sf-x
|
||||||
Shane Booth (shane31)
|
Shane Booth (shane31)
|
||||||
Shawn Varghese (xXH4CKST3RXx)
|
Shawn Varghese (xXH4CKST3RXx)
|
||||||
|
Siad Daboul (Topologist)
|
||||||
Stefan Geschwentner (locutus2)
|
Stefan Geschwentner (locutus2)
|
||||||
Stefano Cardanobile (Stefano80)
|
Stefano Cardanobile (Stefano80)
|
||||||
Steinar Gunderson (sesse)
|
Steinar Gunderson (sesse)
|
||||||
Stéphane Nicolet (snicolet)
|
Stéphane Nicolet (snicolet)
|
||||||
|
Syine Mineta (MinetaS)
|
||||||
|
Prokop Randáček (ProkopRandacek)
|
||||||
Thanar2
|
Thanar2
|
||||||
thaspel
|
thaspel
|
||||||
theo77186
|
theo77186
|
||||||
@@ -178,11 +200,13 @@ Tom Truscott
|
|||||||
Tom Vijlbrief (tomtor)
|
Tom Vijlbrief (tomtor)
|
||||||
Tomasz Sobczyk (Sopel97)
|
Tomasz Sobczyk (Sopel97)
|
||||||
Torsten Franz (torfranz, tfranzer)
|
Torsten Franz (torfranz, tfranzer)
|
||||||
|
Torsten Hellwig (Torom)
|
||||||
Tracey Emery (basepr1me)
|
Tracey Emery (basepr1me)
|
||||||
tttak
|
tttak
|
||||||
Unai Corzo (unaiic)
|
Unai Corzo (unaiic)
|
||||||
Uri Blass (uriblass)
|
Uri Blass (uriblass)
|
||||||
Vince Negri (cuddlestmonkey)
|
Vince Negri (cuddlestmonkey)
|
||||||
|
xefoci7612
|
||||||
zz4032
|
zz4032
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,41 +1,62 @@
|
|||||||
|
<div align="center">
|
||||||
|
|
||||||
|
[![Stockfish][stockfish128-logo]][website-link]
|
||||||
|
|
||||||
|
[![Build][build-badge]][build-link]
|
||||||
|
[![License][license-badge]][license-link]
|
||||||
|
<br>
|
||||||
|
[![Release][release-badge]][release-link]
|
||||||
|
[![Commits][commits-badge]][commits-link]
|
||||||
|
<br>
|
||||||
|
[![Website][website-badge]][website-link]
|
||||||
|
[![Fishtest][fishtest-badge]][fishtest-link]
|
||||||
|
[![Discord][discord-badge]][discord-link]
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
## Overview
|
## Overview
|
||||||
|
|
||||||
[](https://travis-ci.org/official-stockfish/Stockfish)
|
[Stockfish][website-link] is a free, powerful UCI chess engine derived from
|
||||||
[](https://ci.appveyor.com/project/mcostalba/stockfish/branch/master)
|
Glaurung 2.1. Stockfish is not a complete chess program and requires a UCI-compatible
|
||||||
|
graphical user interface (GUI) (e.g. XBoard with PolyGlot, Scid, Cute Chess, eboard,
|
||||||
[Stockfish](https://stockfishchess.org) is a free, powerful UCI chess engine
|
Arena, Sigma Chess, Shredder, Chess Partner or Fritz) in order to be used comfortably.
|
||||||
derived from Glaurung 2.1. Stockfish is not a complete chess program and requires a
|
Read the documentation for your GUI of choice for information about how to use
|
||||||
UCI-compatible graphical user interface (GUI) (e.g. XBoard with PolyGlot, Scid,
|
Stockfish with it.
|
||||||
Cute Chess, eboard, Arena, Sigma Chess, Shredder, Chess 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.
|
|
||||||
|
|
||||||
The Stockfish engine features two evaluation functions for chess, the classical
|
|
||||||
evaluation based on handcrafted terms, and the NNUE evaluation based on efficiently
|
|
||||||
updatable neural networks. The classical evaluation runs efficiently on almost all
|
|
||||||
CPU architectures, while the NNUE evaluation benefits from the vector
|
|
||||||
intrinsics available on most CPUs (sse2, avx2, neon, or similar).
|
|
||||||
|
|
||||||
|
The Stockfish engine features two evaluation functions for chess. The efficiently
|
||||||
|
updatable neural network (NNUE) based evaluation is the default and by far the strongest.
|
||||||
|
The classical evaluation based on handcrafted terms remains available. The strongest
|
||||||
|
network is integrated in the binary and downloaded automatically during the build process.
|
||||||
|
The NNUE evaluation benefits from the vector intrinsics available on most CPUs (sse2,
|
||||||
|
avx2, neon, or similar).
|
||||||
|
|
||||||
## Files
|
## Files
|
||||||
|
|
||||||
This distribution of Stockfish consists of the following files:
|
This distribution of Stockfish consists of the following files:
|
||||||
|
|
||||||
* Readme.md, the file you are currently reading.
|
* [README.md][readme-link], the file you are currently reading.
|
||||||
|
|
||||||
* Copying.txt, a text file containing the GNU General Public License version 3.
|
* [Copying.txt][license-link], a text file containing the GNU General Public License
|
||||||
|
version 3.
|
||||||
|
|
||||||
* AUTHORS, a text file with the list of authors for the project
|
* [AUTHORS][authors-link], a text file with the list of authors for the project.
|
||||||
|
|
||||||
* src, a subdirectory containing the full source code, including a Makefile
|
* [src][src-link], a subdirectory containing the full source code, including a Makefile
|
||||||
that can be used to compile Stockfish on Unix-like systems.
|
that can be used to compile Stockfish on Unix-like systems.
|
||||||
|
|
||||||
* a file with the .nnue extension, storing the neural network for the NNUE
|
* a file with the .nnue extension, storing the neural network for the NNUE evaluation.
|
||||||
evaluation. Binary distributions will have this file embedded.
|
Binary distributions will have this file embedded.
|
||||||
|
|
||||||
## UCI options
|
## The UCI protocol and available options
|
||||||
|
|
||||||
Currently, Stockfish has the following UCI options:
|
The Universal Chess Interface (UCI) is a standard protocol used to communicate with
|
||||||
|
a chess engine, and is the recommended way to do so for typical graphical user interfaces
|
||||||
|
(GUI) or chess tools. Stockfish implements the majority of its options as described
|
||||||
|
in [the UCI protocol][uci-link].
|
||||||
|
|
||||||
|
Developers can see the default values for UCI options available in Stockfish by typing
|
||||||
|
`./stockfish uci` in a terminal, but the majority of users will typically see them and
|
||||||
|
change them via a chess GUI. This is a list of available UCI options in Stockfish:
|
||||||
|
|
||||||
* #### Threads
|
* #### Threads
|
||||||
The number of CPU threads used for searching a position. For best performance, set
|
The number of CPU threads used for searching a position. For best performance, set
|
||||||
@@ -61,9 +82,9 @@ Currently, Stockfish has the following UCI options:
|
|||||||
|
|
||||||
* #### EvalFile
|
* #### EvalFile
|
||||||
The name of the file of the NNUE evaluation parameters. Depending on the GUI the
|
The name of the file of the NNUE evaluation parameters. Depending on the GUI the
|
||||||
filename might have to include the full path to the folder/directory that contains the file.
|
filename might have to include the full path to the folder/directory that contains
|
||||||
Other locations, such as the directory that contains the binary and the working directory,
|
the file. Other locations, such as the directory that contains the binary and the
|
||||||
are also searched.
|
working directory, are also searched.
|
||||||
|
|
||||||
* #### UCI_AnalyseMode
|
* #### UCI_AnalyseMode
|
||||||
An option handled by your GUI.
|
An option handled by your GUI.
|
||||||
@@ -96,7 +117,7 @@ Currently, Stockfish has the following UCI options:
|
|||||||
Example: `C:\tablebases\wdl345;C:\tablebases\wdl6;D:\tablebases\dtz345;D:\tablebases\dtz6`
|
Example: `C:\tablebases\wdl345;C:\tablebases\wdl6;D:\tablebases\dtz345;D:\tablebases\dtz6`
|
||||||
|
|
||||||
It is recommended to store .rtbw files on an SSD. There is no loss in storing
|
It is recommended to store .rtbw files on an SSD. There is no loss in storing
|
||||||
the .rtbz files on a regular HD. It is recommended to verify all md5 checksums
|
the .rtbz files on a regular HDD. It is recommended to verify all md5 checksums
|
||||||
of the downloaded tablebase files (`md5sum -c checksum.md5`) as corruption will
|
of the downloaded tablebase files (`md5sum -c checksum.md5`) as corruption will
|
||||||
lead to engine crashes.
|
lead to engine crashes.
|
||||||
|
|
||||||
@@ -113,14 +134,6 @@ Currently, Stockfish has the following UCI options:
|
|||||||
Limit Syzygy tablebase probing to positions with at most this many pieces left
|
Limit Syzygy tablebase probing to positions with at most this many pieces left
|
||||||
(including kings and pawns).
|
(including kings and pawns).
|
||||||
|
|
||||||
* #### Contempt
|
|
||||||
A positive value for contempt favors middle game positions and avoids draws,
|
|
||||||
effective for the classical evaluation only.
|
|
||||||
|
|
||||||
* #### Analysis Contempt
|
|
||||||
By default, contempt is set to prefer the side to move. Set this option to "White"
|
|
||||||
or "Black" to analyse with contempt for that side, or "Off" to disable contempt.
|
|
||||||
|
|
||||||
* #### Move Overhead
|
* #### Move Overhead
|
||||||
Assume a time delay of x ms due to network and GUI overheads. This is useful to
|
Assume a time delay of x ms due to network and GUI overheads. This is useful to
|
||||||
avoid losses on time in those cases.
|
avoid losses on time in those cases.
|
||||||
@@ -136,6 +149,35 @@ Currently, Stockfish has the following UCI options:
|
|||||||
* #### Debug Log File
|
* #### Debug Log File
|
||||||
Write all communication to and from the engine into a text file.
|
Write all communication to and from the engine into a text file.
|
||||||
|
|
||||||
|
For developers the following non-standard commands might be of interest, mainly useful for debugging:
|
||||||
|
|
||||||
|
* #### bench *ttSize threads limit fenFile limitType evalType*
|
||||||
|
Performs a standard benchmark using various options. The signature of a version
|
||||||
|
(standard node count) is obtained using all defaults. `bench` is currently
|
||||||
|
`bench 16 1 13 default depth mixed`.
|
||||||
|
|
||||||
|
* #### compiler
|
||||||
|
Give information about the compiler and environment used for building a binary.
|
||||||
|
|
||||||
|
* #### d
|
||||||
|
Display the current position, with ascii art and fen.
|
||||||
|
|
||||||
|
* #### eval
|
||||||
|
Return the evaluation of the current position.
|
||||||
|
|
||||||
|
* #### export_net [filename]
|
||||||
|
Exports the currently loaded network to a file.
|
||||||
|
If the currently loaded network is the embedded network and the filename
|
||||||
|
is not specified then the network is saved to the file matching the name
|
||||||
|
of the embedded network, as defined in evaluate.h.
|
||||||
|
If the currently loaded network is not the embedded network (some net set
|
||||||
|
through the UCI setoption) then the filename parameter is required and the
|
||||||
|
network is saved into that file.
|
||||||
|
|
||||||
|
* #### flip
|
||||||
|
Flips the side to move.
|
||||||
|
|
||||||
|
|
||||||
## A note on classical evaluation versus NNUE evaluation
|
## A note on classical evaluation versus NNUE evaluation
|
||||||
|
|
||||||
Both approaches assign a value to a position that is used in alpha-beta (PVS) search
|
Both approaches assign a value to a position that is used in alpha-beta (PVS) search
|
||||||
@@ -148,22 +190,25 @@ on the evaluations of millions of positions at moderate search depth.
|
|||||||
The NNUE evaluation was first introduced in shogi, and ported to Stockfish afterward.
|
The NNUE evaluation was first introduced in shogi, and ported to Stockfish afterward.
|
||||||
It can be evaluated efficiently on CPUs, and exploits the fact that only parts
|
It can be evaluated efficiently on CPUs, and exploits the fact that only parts
|
||||||
of the neural network need to be updated after a typical chess move.
|
of the neural network need to be updated after a typical chess move.
|
||||||
[The nodchip repository](https://github.com/nodchip/Stockfish) provides additional
|
[The nodchip repository][nodchip-link] provided the first version of the needed tools
|
||||||
tools to train and develop the NNUE networks. On CPUs supporting modern vector instructions
|
to train and develop the NNUE networks. Today, more advanced training tools are
|
||||||
(avx2 and similar), the NNUE evaluation results in much stronger playing strength, even
|
available in [the nnue-pytorch repository][pytorch-link], while data generation tools
|
||||||
if the nodes per second computed by the engine is somewhat lower (roughly 80% of nps
|
are available in [a dedicated branch][tools-link].
|
||||||
is typical).
|
|
||||||
|
On CPUs supporting modern vector instructions (avx2 and similar), the NNUE evaluation
|
||||||
|
results in much stronger playing strength, even if the nodes per second computed by
|
||||||
|
the engine is somewhat lower (roughly 80% of nps is typical).
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
|
|
||||||
1) the NNUE evaluation depends on the Stockfish binary and the network parameter
|
1) the NNUE evaluation depends on the Stockfish binary and the network parameter file
|
||||||
file (see the EvalFile UCI option). Not every parameter file is compatible with a given
|
(see the EvalFile UCI option). Not every parameter file is compatible with a given
|
||||||
Stockfish binary, but the default value of the EvalFile UCI option is the name of a network
|
Stockfish binary, but the default value of the EvalFile UCI option is the name of a
|
||||||
that is guaranteed to be compatible with that binary.
|
network that is guaranteed to be compatible with that binary.
|
||||||
|
|
||||||
2) to use the NNUE evaluation, the additional data file with neural network parameters
|
2) to use the NNUE evaluation, the additional data file with neural network parameters
|
||||||
needs to be available. Normally, this file is already embedded in the binary or it
|
needs to be available. Normally, this file is already embedded in the binary or it can
|
||||||
can be downloaded. The filename for the default (recommended) net can be found as the default
|
be downloaded. The filename for the default (recommended) net can be found as the default
|
||||||
value of the `EvalFile` UCI option, with the format `nn-[SHA256 first 12 digits].nnue`
|
value of the `EvalFile` UCI option, with the format `nn-[SHA256 first 12 digits].nnue`
|
||||||
(for instance, `nn-c157e0a5755b.nnue`). This file can be downloaded from
|
(for instance, `nn-c157e0a5755b.nnue`). This file can be downloaded from
|
||||||
```
|
```
|
||||||
@@ -214,8 +259,8 @@ are already enabled, and no configuration is needed.
|
|||||||
### Support on Windows
|
### Support on Windows
|
||||||
|
|
||||||
The use of large pages requires "Lock Pages in Memory" privilege. See
|
The use of large pages requires "Lock Pages in Memory" privilege. See
|
||||||
[Enable the Lock Pages in Memory Option (Windows)](https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/enable-the-lock-pages-in-memory-option-windows)
|
[Enable the Lock Pages in Memory Option (Windows)][lockpages-link]
|
||||||
on how to enable this privilege, then run [RAMMap](https://docs.microsoft.com/en-us/sysinternals/downloads/rammap)
|
on how to enable this privilege, then run [RAMMap][rammap-link]
|
||||||
to double-check that large pages are used. We suggest that you reboot
|
to double-check that large pages are used. We suggest that you reboot
|
||||||
your computer after you have enabled large pages, because long Windows
|
your computer after you have enabled large pages, because long Windows
|
||||||
sessions suffer from memory fragmentation, which may prevent Stockfish
|
sessions suffer from memory fragmentation, which may prevent Stockfish
|
||||||
@@ -242,9 +287,9 @@ When not using the Makefile to compile (for instance, with Microsoft MSVC) you
|
|||||||
need to manually set/unset some switches in the compiler command line; see
|
need to manually set/unset some switches in the compiler command line; see
|
||||||
file *types.h* for a quick reference.
|
file *types.h* for a quick reference.
|
||||||
|
|
||||||
When reporting an issue or a bug, please tell us which version and
|
When reporting an issue or a bug, please tell us which Stockfish version
|
||||||
compiler you used to create your executable. These informations can
|
and which compiler you used to create your executable. This information
|
||||||
be found by typing the following commands in a console:
|
can be found by typing the following command in a console:
|
||||||
|
|
||||||
```
|
```
|
||||||
./stockfish compiler
|
./stockfish compiler
|
||||||
@@ -252,32 +297,32 @@ be found by typing the following commands in a console:
|
|||||||
|
|
||||||
## Understanding the code base and participating in the project
|
## Understanding the code base and participating in the project
|
||||||
|
|
||||||
Stockfish's improvement over the last couple of years has been a great
|
Stockfish's improvement over the last decade has been a great community
|
||||||
community effort. There are a few ways to help contribute to its growth.
|
effort. There are a few ways to help contribute to its growth.
|
||||||
|
|
||||||
### Donating hardware
|
### Donating hardware
|
||||||
|
|
||||||
Improving Stockfish requires a massive amount of testing. You can donate
|
Improving Stockfish requires a massive amount of testing. You can donate
|
||||||
your hardware resources by installing the [Fishtest Worker](https://github.com/glinscott/fishtest/wiki/Running-the-worker:-overview)
|
your hardware resources by installing the [Fishtest Worker][worker-link]
|
||||||
and view the current tests on [Fishtest](https://tests.stockfishchess.org/tests).
|
and view the current tests on [Fishtest][fishtest-link].
|
||||||
|
|
||||||
### Improving the code
|
### Improving the code
|
||||||
|
|
||||||
If you want to help improve the code, there are several valuable resources:
|
If you want to help improve the code, there are several valuable resources:
|
||||||
|
|
||||||
* [In this wiki,](https://www.chessprogramming.org) many techniques used in
|
* [In this wiki,][programming-link] many techniques used in
|
||||||
Stockfish are explained with a lot of background information.
|
Stockfish are explained with a lot of background information.
|
||||||
|
|
||||||
* [The section on Stockfish](https://www.chessprogramming.org/Stockfish)
|
* [The section on Stockfish][programmingsf-link]
|
||||||
describes many features and techniques used by Stockfish. However, it is
|
describes many features and techniques used by Stockfish. However, it is
|
||||||
generic rather than being focused on Stockfish's precise implementation.
|
generic rather than being focused on Stockfish's precise implementation.
|
||||||
Nevertheless, a helpful resource.
|
Nevertheless, a helpful resource.
|
||||||
|
|
||||||
* The latest source can always be found on [GitHub](https://github.com/official-stockfish/Stockfish).
|
* The latest source can always be found on [GitHub][github-link].
|
||||||
Discussions about Stockfish take place these days mainly in the [FishCooking](https://groups.google.com/forum/#!forum/fishcooking)
|
Discussions about Stockfish take place these days mainly in the [FishCooking][fishcooking-link]
|
||||||
group and on the [Stockfish Discord channel](https://discord.gg/nv8gDtt).
|
group and on the [Stockfish Discord channel][discord-link].
|
||||||
The engine testing is done on [Fishtest](https://tests.stockfishchess.org/tests).
|
The engine testing is done on [Fishtest][fishtest-link].
|
||||||
If you want to help improve Stockfish, please read this [guideline](https://github.com/glinscott/fishtest/wiki/Creating-my-first-test)
|
If you want to help improve Stockfish, please read this [guideline][guideline-link]
|
||||||
first, where the basics of Stockfish development are explained.
|
first, where the basics of Stockfish development are explained.
|
||||||
|
|
||||||
|
|
||||||
@@ -291,10 +336,43 @@ it (either by itself or as part of some bigger software package), or
|
|||||||
using it as the starting point for a software project of your own.
|
using it as the starting point for a software project of your own.
|
||||||
|
|
||||||
The only real limitation is that whenever you distribute Stockfish in
|
The only real limitation is that whenever you distribute Stockfish in
|
||||||
some way, you MUST always include the full source code, or a pointer
|
some way, you MUST always include the license and the full source code
|
||||||
to where the source code can be found, to generate the exact binary
|
(or a pointer to where the source code can be found) to generate the
|
||||||
you are distributing. If you make any changes to the source code,
|
exact binary you are distributing. If you make any changes to the
|
||||||
these changes must also be made available under the GPL.
|
source code, these changes must also be made available under the GPL v3.
|
||||||
|
|
||||||
For full details, read the copy of the GPL v3 found in the file named
|
For full details, read the copy of the GPL v3 found in the file named
|
||||||
*Copying.txt*.
|
[*Copying.txt*][license-link].
|
||||||
|
|
||||||
|
|
||||||
|
[authors-link]: https://github.com/official-stockfish/Stockfish/blob/master/AUTHORS
|
||||||
|
[build-link]: https://github.com/official-stockfish/Stockfish/actions/workflows/stockfish.yml
|
||||||
|
[commits-link]: https://github.com/official-stockfish/Stockfish/commits/master
|
||||||
|
[discord-link]: https://discord.gg/GWDRS3kU6R
|
||||||
|
[fishcooking-link]: https://groups.google.com/g/fishcooking
|
||||||
|
[fishtest-link]: https://tests.stockfishchess.org/tests
|
||||||
|
[github-link]: https://github.com/official-stockfish/Stockfish
|
||||||
|
[guideline-link]: https://github.com/glinscott/fishtest/wiki/Creating-my-first-test
|
||||||
|
[license-link]: https://github.com/official-stockfish/Stockfish/blob/master/Copying.txt
|
||||||
|
[lockpages-link]: https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/enable-the-lock-pages-in-memory-option-windows
|
||||||
|
[nodchip-link]: https://github.com/nodchip/Stockfish
|
||||||
|
[programming-link]: https://www.chessprogramming.org/Main_Page
|
||||||
|
[programmingsf-link]: https://www.chessprogramming.org/Stockfish
|
||||||
|
[pytorch-link]: https://github.com/glinscott/nnue-pytorch
|
||||||
|
[rammap-link]: https://docs.microsoft.com/en-us/sysinternals/downloads/rammap
|
||||||
|
[readme-link]: https://github.com/official-stockfish/Stockfish/blob/master/README.md
|
||||||
|
[release-link]: https://github.com/official-stockfish/Stockfish/releases/latest
|
||||||
|
[src-link]: https://github.com/official-stockfish/Stockfish/tree/master/src
|
||||||
|
[stockfish128-logo]: https://stockfishchess.org/images/logo/icon_128x128.png
|
||||||
|
[tools-link]: https://github.com/official-stockfish/Stockfish/tree/tools
|
||||||
|
[uci-link]: https://www.shredderchess.com/download/div/uci.zip
|
||||||
|
[website-link]: https://stockfishchess.org
|
||||||
|
[worker-link]: https://github.com/glinscott/fishtest/wiki/Running-the-worker:-overview
|
||||||
|
|
||||||
|
[build-badge]: https://img.shields.io/github/workflow/status/official-stockfish/Stockfish/Stockfish?style=for-the-badge&label=stockfish&logo=github
|
||||||
|
[commits-badge]: https://img.shields.io/github/commits-since/official-stockfish/Stockfish/latest?style=for-the-badge
|
||||||
|
[discord-badge]: https://img.shields.io/discord/435943710472011776?style=for-the-badge&label=discord&logo=Discord
|
||||||
|
[fishtest-badge]: https://img.shields.io/website?style=for-the-badge&down_color=red&down_message=Offline&label=Fishtest&up_color=success&up_message=Online&url=https%3A%2F%2Ftests.stockfishchess.org%2Ftests%2Ffinished
|
||||||
|
[license-badge]: https://img.shields.io/github/license/official-stockfish/Stockfish?style=for-the-badge&label=license&color=success
|
||||||
|
[release-badge]: https://img.shields.io/github/v/release/official-stockfish/Stockfish?style=for-the-badge&label=official%20release
|
||||||
|
[website-badge]: https://img.shields.io/website?style=for-the-badge&down_color=red&down_message=Offline&label=website&up_color=success&up_message=Online&url=https%3A%2F%2Fstockfishchess.org
|
||||||
|
|||||||
+151
-80
@@ -1,189 +1,260 @@
|
|||||||
Contributors to Fishtest with >10,000 CPU hours, as of Feb 15, 2021.
|
Contributors to Fishtest with >10,000 CPU hours, as of 2022-11-19.
|
||||||
Thank you!
|
Thank you!
|
||||||
|
|
||||||
Username CPU Hours Games played
|
Username CPU Hours Games played
|
||||||
----------------------------------------------------
|
------------------------------------------------------------------
|
||||||
noobpwnftw 23930906 1560559941
|
noobpwnftw 36475307 2748033975
|
||||||
dew 1169948 70333008
|
technologov 14570711 760073590
|
||||||
mlang 957168 61657446
|
mlang 3026000 200065824
|
||||||
|
dew 1689222 100034318
|
||||||
|
grandphish2 1442171 86798057
|
||||||
|
okrout 1439985 133471766
|
||||||
|
pemo 1405374 44189811
|
||||||
|
linrock 1299003 28382783
|
||||||
|
TueRens 1163420 71159522
|
||||||
|
JojoM 897158 55177114
|
||||||
|
tvijlbrief 796125 51897690
|
||||||
mibere 703840 46867607
|
mibere 703840 46867607
|
||||||
tvijlbrief 517888 33379462
|
gvreuls 635982 40652394
|
||||||
JojoM 515404 30334272
|
oz 590763 41201352
|
||||||
cw 443276 29385549
|
sebastronomy 581517 23307132
|
||||||
|
cw 517915 34865769
|
||||||
|
fastgm 504266 30264740
|
||||||
|
CSU_Dynasty 479901 31846710
|
||||||
|
ctoks 433503 28180725
|
||||||
crunchy 427035 27344275
|
crunchy 427035 27344275
|
||||||
grandphish2 425794 26347253
|
leszek 416883 27493447
|
||||||
fastgm 414133 24519696
|
bcross 409982 28062127
|
||||||
gvreuls 377843 24708884
|
velislav 345954 22232274
|
||||||
CSU_Dynasty 338718 23030006
|
Fisherman 327231 21829379
|
||||||
Fisherman 326795 21820747
|
Dantist 296386 18031762
|
||||||
TueRens 313730 19490246
|
mgrabiak 288928 18869896
|
||||||
ctoks 298442 20052551
|
rpngn 259965 16281463
|
||||||
velislav 270519 17355456
|
robal 237653 15148350
|
||||||
bcross 241064 17196165
|
ncfish1 231764 15275003
|
||||||
glinscott 217799 13780820
|
nordlandia 226923 14624832
|
||||||
nordlandia 211692 13484886
|
glinscott 208125 13277240
|
||||||
|
drabel 204167 13930674
|
||||||
|
mhoram 202894 12601997
|
||||||
bking_US 198894 11876016
|
bking_US 198894 11876016
|
||||||
drabel 191096 13129722
|
thirdlife 198844 5453268
|
||||||
leszek 189170 11446821
|
|
||||||
mgrabiak 187153 12013300
|
|
||||||
robal 181389 11539242
|
|
||||||
Thanar 179852 12365359
|
Thanar 179852 12365359
|
||||||
vdv 175274 9889046
|
vdv 175544 9904472
|
||||||
|
armo9494 168201 11136452
|
||||||
spams 157128 10319326
|
spams 157128 10319326
|
||||||
marrco 150292 9401741
|
marrco 151599 9551115
|
||||||
sqrt2 147963 9724586
|
sqrt2 147963 9724586
|
||||||
CoffeeOne 137086 5022516
|
vdbergh 137690 8971569
|
||||||
vdbergh 137041 8926915
|
CoffeeOne 137100 5024116
|
||||||
malala 136182 8002293
|
malala 136182 8002293
|
||||||
mhoram 132780 8398229
|
DesolatedDodo 135276 8657464
|
||||||
xoto 124729 8652088
|
xoto 133759 9159372
|
||||||
davar 122092 7960001
|
davar 129023 8376525
|
||||||
dsmith 122059 7570238
|
dsmith 122059 7570238
|
||||||
|
amicic 119661 7938029
|
||||||
Data 113305 8220352
|
Data 113305 8220352
|
||||||
BrunoBanani 112960 7436849
|
BrunoBanani 112960 7436849
|
||||||
pemo 109598 5036441
|
CypressChess 108331 7759788
|
||||||
Dantist 106768 6431396
|
skiminki 106518 7062598
|
||||||
MaZePallas 102741 6630419
|
MaZePallas 102823 6633619
|
||||||
|
sterni1971 100532 5880772
|
||||||
|
sunu 100167 7040199
|
||||||
|
zeryl 99331 6221261
|
||||||
ElbertoOne 99028 7023771
|
ElbertoOne 99028 7023771
|
||||||
|
DMBK 97572 6950312
|
||||||
|
Calis007 96779 5611552
|
||||||
|
cuistot 93111 5536500
|
||||||
brabos 92118 6186135
|
brabos 92118 6186135
|
||||||
linrock 90903 6708639
|
Wolfgang 91769 5720158
|
||||||
psk 89957 5984901
|
psk 89957 5984901
|
||||||
sunu 88614 6020673
|
racerschmacer 85805 6122790
|
||||||
sterni1971 86948 5613788
|
jcAEie 85527 5630616
|
||||||
Vizvezdenec 83761 5344740
|
Vizvezdenec 83761 5344740
|
||||||
|
sschnee 83557 4853690
|
||||||
|
0x3C33 82614 5271253
|
||||||
BRAVONE 81239 5054681
|
BRAVONE 81239 5054681
|
||||||
|
Dubslow 78461 5042980
|
||||||
nssy 76497 5259388
|
nssy 76497 5259388
|
||||||
cuistot 76366 4370584
|
jromang 76106 5236025
|
||||||
racerschmacer 75753 5442626
|
|
||||||
teddybaer 75125 5407666
|
teddybaer 75125 5407666
|
||||||
|
yurikvelo 73933 5031096
|
||||||
|
tolkki963 73885 4721430
|
||||||
Pking_cda 73776 5293873
|
Pking_cda 73776 5293873
|
||||||
0x3C33 73133 4670293
|
Bobo1239 71675 4860987
|
||||||
jromang 72117 5054915
|
|
||||||
solarlight 70517 5028306
|
solarlight 70517 5028306
|
||||||
dv8silencer 70287 3883992
|
dv8silencer 70287 3883992
|
||||||
Bobo1239 68515 4652287
|
Gelma 69304 3980932
|
||||||
manap 66273 4121774
|
manap 66273 4121774
|
||||||
tinker 64321 4268390
|
megaman7de 65419 4120200
|
||||||
|
markkulix 65331 4114860
|
||||||
|
bigpen0r 64932 4683883
|
||||||
|
tinker 64333 4268790
|
||||||
|
qurashee 61208 3429862
|
||||||
|
AGI 58325 4258646
|
||||||
robnjr 57262 4053117
|
robnjr 57262 4053117
|
||||||
Freja 56938 3733019
|
Freja 56938 3733019
|
||||||
|
MaxKlaxxMiner 56879 3423958
|
||||||
ttruscott 56010 3680085
|
ttruscott 56010 3680085
|
||||||
rkl 54986 4150767
|
rkl 55132 4164467
|
||||||
renouve 53811 3501516
|
renouve 53811 3501516
|
||||||
|
Spprtr 52736 3410019
|
||||||
finfish 51360 3370515
|
finfish 51360 3370515
|
||||||
eva42 51272 3599691
|
eva42 51272 3599691
|
||||||
|
eastorwest 51117 3454811
|
||||||
rap 49985 3219146
|
rap 49985 3219146
|
||||||
|
unixwizard 49734 2536230
|
||||||
pb00067 49727 3298270
|
pb00067 49727 3298270
|
||||||
amicic 49691 3042481
|
|
||||||
ronaldjerum 47654 3240695
|
ronaldjerum 47654 3240695
|
||||||
bigpen0r 47278 3291647
|
|
||||||
biffhero 46564 3111352
|
biffhero 46564 3111352
|
||||||
|
GPUex 45861 2926502
|
||||||
|
Fifis 45843 3088497
|
||||||
|
oryx 45578 3493978
|
||||||
VoyagerOne 45476 3452465
|
VoyagerOne 45476 3452465
|
||||||
eastorwest 45033 3071805
|
Wencey 44943 2654490
|
||||||
speedycpu 43842 3003273
|
speedycpu 43842 3003273
|
||||||
jbwiebe 43305 2805433
|
jbwiebe 43305 2805433
|
||||||
Antihistamine 41788 2761312
|
Antihistamine 41788 2761312
|
||||||
mhunt 41735 2691355
|
mhunt 41735 2691355
|
||||||
|
olafm 41277 3284344
|
||||||
homyur 39893 2850481
|
homyur 39893 2850481
|
||||||
gri 39871 2515779
|
gri 39871 2515779
|
||||||
oryx 38282 2944400
|
MarcusTullius 38303 2251097
|
||||||
Spprtr 38157 2470529
|
Garf 37741 2999686
|
||||||
SC 37290 2731014
|
kdave 37424 2557406
|
||||||
|
SC 37299 2731694
|
||||||
csnodgrass 36207 2688994
|
csnodgrass 36207 2688994
|
||||||
jmdana 36157 2210661
|
jmdana 36157 2210661
|
||||||
strelock 34716 2074055
|
strelock 34716 2074055
|
||||||
Garf 33800 2747562
|
|
||||||
skiminki 33515 2055584
|
|
||||||
EthanOConnor 33370 2090311
|
EthanOConnor 33370 2090311
|
||||||
slakovv 32915 2021889
|
slakovv 32915 2021889
|
||||||
yurikvelo 32600 2255966
|
gopeto 31669 2060958
|
||||||
|
manapbk 30987 1810399
|
||||||
Prcuvu 30377 2170122
|
Prcuvu 30377 2170122
|
||||||
manapbk 30326 1770143
|
|
||||||
anst 30301 2190091
|
anst 30301 2190091
|
||||||
jkiiski 30136 1904470
|
jkiiski 30136 1904470
|
||||||
|
spcc 30135 1903728
|
||||||
hyperbolic.tom 29840 2017394
|
hyperbolic.tom 29840 2017394
|
||||||
|
xwziegtm 29763 2347412
|
||||||
|
chuckstablers 29659 2093438
|
||||||
Pyafue 29650 1902349
|
Pyafue 29650 1902349
|
||||||
qurashee 27758 1509620
|
belzedar94 28846 1811530
|
||||||
OuaisBla 27636 1578800
|
OuaisBla 27636 1578800
|
||||||
chriswk 26902 1868317
|
chriswk 26902 1868317
|
||||||
achambord 26582 1767323
|
achambord 26582 1767323
|
||||||
Fifis 26376 1776853
|
|
||||||
Patrick_G 26276 1801617
|
Patrick_G 26276 1801617
|
||||||
yorkman 26193 1992080
|
yorkman 26193 1992080
|
||||||
|
Ulysses 25289 1674274
|
||||||
SFTUser 25182 1675689
|
SFTUser 25182 1675689
|
||||||
nabildanial 24942 1519409
|
nabildanial 24942 1519409
|
||||||
Sharaf_DG 24765 1786697
|
Sharaf_DG 24765 1786697
|
||||||
ncfish1 24411 1520927
|
rodneyc 24376 1416402
|
||||||
agg177 23890 1395014
|
agg177 23890 1395014
|
||||||
|
Ente 23747 1674582
|
||||||
|
Karpovbot 23629 1313186
|
||||||
JanErik 23408 1703875
|
JanErik 23408 1703875
|
||||||
Isidor 23388 1680691
|
Isidor 23388 1680691
|
||||||
Norabor 23164 1591830
|
Norabor 23371 1603244
|
||||||
cisco2015 22895 1762069
|
cisco2015 22934 1763773
|
||||||
Zirie 22542 1472937
|
Zirie 22542 1472937
|
||||||
team-oh 22272 1636708
|
team-oh 22272 1636708
|
||||||
|
Roady 22220 1465606
|
||||||
MazeOfGalious 21978 1629593
|
MazeOfGalious 21978 1629593
|
||||||
sg4032 21945 1643065
|
sg4032 21947 1643353
|
||||||
ianh2105 21725 1632562
|
ianh2105 21725 1632562
|
||||||
xor12 21628 1680365
|
xor12 21628 1680365
|
||||||
dex 21612 1467203
|
dex 21612 1467203
|
||||||
nesoneg 21494 1463031
|
nesoneg 21494 1463031
|
||||||
jjoshua2 20997 1422689
|
user213718 21454 1404128
|
||||||
|
AndreasKrug 21227 1577833
|
||||||
|
sphinx 21211 1384728
|
||||||
|
jjoshua2 21001 1423089
|
||||||
horst.prack 20878 1465656
|
horst.prack 20878 1465656
|
||||||
|
jsys14 20729 1221010
|
||||||
0xB00B1ES 20590 1208666
|
0xB00B1ES 20590 1208666
|
||||||
sphinx 20515 1352368
|
|
||||||
j3corre 20405 941444
|
j3corre 20405 941444
|
||||||
Adrian.Schmidt123 20316 1281436
|
Adrian.Schmidt123 20316 1281436
|
||||||
Ente 20017 1432602
|
bonsi 20022 1300682
|
||||||
wei 19973 1745989
|
wei 19973 1745989
|
||||||
|
dapper 19754 1167758
|
||||||
|
Zake9298 19745 1458416
|
||||||
|
fishtester 19617 1257388
|
||||||
rstoesser 19569 1293588
|
rstoesser 19569 1293588
|
||||||
eudhan 19274 1283717
|
eudhan 19274 1283717
|
||||||
|
vulcan 18871 1729392
|
||||||
|
Jopo12321 18803 1036284
|
||||||
jundery 18445 1115855
|
jundery 18445 1115855
|
||||||
iisiraider 18247 1101015
|
|
||||||
ville 17883 1384026
|
ville 17883 1384026
|
||||||
|
5t0ckf15hTr4in3r 17809 1105858
|
||||||
chris 17698 1487385
|
chris 17698 1487385
|
||||||
|
dju 17697 994333
|
||||||
purplefishies 17595 1092533
|
purplefishies 17595 1092533
|
||||||
DMBK 17357 1279152
|
iisiraider 17275 1049015
|
||||||
DragonLord 17014 1162790
|
DragonLord 17014 1162790
|
||||||
dju 16515 929427
|
Karby 16457 1010138
|
||||||
|
Goatminola 16278 1145026
|
||||||
IgorLeMasson 16064 1147232
|
IgorLeMasson 16064 1147232
|
||||||
|
Gaster319 16056 1109070
|
||||||
|
redstone59 15953 1161664
|
||||||
|
scuzzi 15757 968735
|
||||||
ako027ako 15671 1173203
|
ako027ako 15671 1173203
|
||||||
Nikolay.IT 15154 1068349
|
Nikolay.IT 15154 1068349
|
||||||
Andrew Grant 15114 895539
|
Andrew Grant 15114 895539
|
||||||
|
Naven94 15054 834762
|
||||||
OssumOpossum 14857 1007129
|
OssumOpossum 14857 1007129
|
||||||
|
qoo_charly_cai 14490 847865
|
||||||
enedene 14476 905279
|
enedene 14476 905279
|
||||||
bpfliegel 14298 884523
|
szupaw 14252 929130
|
||||||
|
bpfliegel 14233 882523
|
||||||
|
mpx86 14019 759568
|
||||||
jpulman 13982 870599
|
jpulman 13982 870599
|
||||||
joster 13794 950160
|
crocogoat 13803 1117422
|
||||||
Nesa92 13786 1114691
|
Nesa92 13786 1114691
|
||||||
crocogoat 13753 1114622
|
joster 13710 946160
|
||||||
|
mbeier 13650 1044928
|
||||||
Hjax 13535 915487
|
Hjax 13535 915487
|
||||||
Dark_wizzie 13422 1007152
|
Dark_wizzie 13422 1007152
|
||||||
mpx86 12941 693640
|
Rudolphous 13244 883140
|
||||||
|
Machariel 13010 863104
|
||||||
|
infinigon 12991 943216
|
||||||
|
pirt 12925 985437
|
||||||
|
Skiff84 12923 649994
|
||||||
mabichito 12903 749391
|
mabichito 12903 749391
|
||||||
thijsk 12886 722107
|
thijsk 12886 722107
|
||||||
AdrianSA 12860 804972
|
AdrianSA 12860 804972
|
||||||
Flopzee 12698 894821
|
Flopzee 12698 894821
|
||||||
fatmurphy 12547 853210
|
fatmurphy 12547 853210
|
||||||
scuzzi 12511 845761
|
woutboat 12419 836696
|
||||||
Karby 12429 735880
|
|
||||||
SapphireBrand 12416 969604
|
SapphireBrand 12416 969604
|
||||||
|
Oakwen 12406 840961
|
||||||
|
deflectooor 12386 579392
|
||||||
modolief 12386 896470
|
modolief 12386 896470
|
||||||
|
Farseer 12249 694108
|
||||||
pgontarz 12151 848794
|
pgontarz 12151 848794
|
||||||
stocky 11954 699440
|
stocky 11954 699440
|
||||||
mschmidt 11941 803401
|
mschmidt 11941 803401
|
||||||
|
MooTheCow 11871 773654
|
||||||
|
Jackfish 11867 773550
|
||||||
|
dbernier 11705 821780
|
||||||
|
whelanh 11557 245188
|
||||||
|
Maxim 11543 836024
|
||||||
|
Nullvalue 11534 731410
|
||||||
|
icewulf 11528 650470
|
||||||
|
FormazChar 11523 861599
|
||||||
infinity 11470 727027
|
infinity 11470 727027
|
||||||
|
aga 11412 695127
|
||||||
torbjo 11395 729145
|
torbjo 11395 729145
|
||||||
Thomas A. Anderson 11372 732094
|
Thomas A. Anderson 11372 732094
|
||||||
|
savage84 11358 670860
|
||||||
|
ali-al-zhrani 11272 781310
|
||||||
d64 11263 789184
|
d64 11263 789184
|
||||||
Maxim 11129 804704
|
Bourbaki 11108 709144
|
||||||
snicolet 11106 869170
|
snicolet 11106 869170
|
||||||
MooTheCow 11008 694942
|
Alb11747 10855 696920
|
||||||
savage84 10965 641068
|
|
||||||
Rudolphous 10915 741268
|
|
||||||
Wolfgang 10809 580032
|
|
||||||
rpngn 10712 688203
|
|
||||||
basepi 10637 744851
|
basepi 10637 744851
|
||||||
michaelrpg 10409 735127
|
Cubox 10621 826448
|
||||||
|
Karmatron 10616 674818
|
||||||
|
michaelrpg 10509 739239
|
||||||
|
OIVAS7572 10420 995586
|
||||||
|
Garruk 10348 704905
|
||||||
dzjp 10343 732529
|
dzjp 10343 732529
|
||||||
ali-al-zhrani 10324 726502
|
|
||||||
ols 10259 570669
|
ols 10259 570669
|
||||||
lbraesch 10252 647825
|
|
||||||
|
|||||||
@@ -1,88 +0,0 @@
|
|||||||
version: 1.0.{build}
|
|
||||||
clone_depth: 50
|
|
||||||
|
|
||||||
branches:
|
|
||||||
only:
|
|
||||||
- master
|
|
||||||
|
|
||||||
# Operating system (build VM template)
|
|
||||||
os: Visual Studio 2019
|
|
||||||
|
|
||||||
# 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.17)',
|
|
||||||
'project(Stockfish)',
|
|
||||||
'set(CMAKE_CXX_STANDARD 17)',
|
|
||||||
'set(CMAKE_CXX_STANDARD_REQUIRED ON)',
|
|
||||||
'set (CMAKE_CXX_EXTENSIONS OFF)',
|
|
||||||
'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 16 2019"
|
|
||||||
If (${env:PLATFORM} -eq 'x64') { $a = "x64" }
|
|
||||||
If (${env:PLATFORM} -eq 'x86') { $a = "Win32" }
|
|
||||||
cmake -G "${g}" -A ${a} .
|
|
||||||
Write-Host "Generated files for: " $g $a
|
|
||||||
|
|
||||||
build_script:
|
|
||||||
- cmake --build . --config %CONFIGURATION% -- /verbosity:minimal
|
|
||||||
- ps: |
|
|
||||||
# Download default NNUE net from fishtest
|
|
||||||
$nnuenet = Get-Content -Path src\evaluate.h | Select-String -CaseSensitive -Pattern "EvalFileDefaultName" | Select-String -CaseSensitive -Pattern "nn-[a-z0-9]{12}.nnue"
|
|
||||||
$dummy = $nnuenet -match "(?<nnuenet>nn-[a-z0-9]{12}.nnue)"
|
|
||||||
$nnuenet = $Matches.nnuenet
|
|
||||||
Write-Host "Default net:" $nnuenet
|
|
||||||
$nnuedownloadurl = "https://tests.stockfishchess.org/api/nn/$nnuenet"
|
|
||||||
$nnuefilepath = "src\${env:CONFIGURATION}\$nnuenet"
|
|
||||||
if (Test-Path -Path $nnuefilepath) {
|
|
||||||
Write-Host "Already available."
|
|
||||||
} else {
|
|
||||||
Write-Host "Downloading $nnuedownloadurl to $nnuefilepath"
|
|
||||||
Invoke-WebRequest -Uri $nnuedownloadurl -OutFile $nnuefilepath
|
|
||||||
}
|
|
||||||
|
|
||||||
before_test:
|
|
||||||
- cd src/%CONFIGURATION%
|
|
||||||
- stockfish bench 2> out.txt >NUL
|
|
||||||
- ps: |
|
|
||||||
# Verify bench number
|
|
||||||
$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 }
|
|
||||||
+210
-89
@@ -1,5 +1,5 @@
|
|||||||
# Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
# Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
# Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
# Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
#
|
#
|
||||||
# Stockfish is free software: you can redistribute it and/or modify
|
# Stockfish is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
@@ -19,8 +19,26 @@
|
|||||||
### Section 1. General Configuration
|
### Section 1. General Configuration
|
||||||
### ==========================================================================
|
### ==========================================================================
|
||||||
|
|
||||||
|
### Establish the operating system name
|
||||||
|
KERNEL = $(shell uname -s)
|
||||||
|
ifeq ($(KERNEL),Linux)
|
||||||
|
OS = $(shell uname -o)
|
||||||
|
endif
|
||||||
|
|
||||||
|
### Target Windows OS
|
||||||
|
ifeq ($(OS),Windows_NT)
|
||||||
|
ifneq ($(COMP),ndk)
|
||||||
|
target_windows = yes
|
||||||
|
endif
|
||||||
|
else ifeq ($(COMP),mingw)
|
||||||
|
target_windows = yes
|
||||||
|
ifeq ($(WINE_PATH),)
|
||||||
|
WINE_PATH = $(shell which wine)
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
### Executable name
|
### Executable name
|
||||||
ifeq ($(COMP),mingw)
|
ifeq ($(target_windows),yes)
|
||||||
EXE = stockfish.exe
|
EXE = stockfish.exe
|
||||||
else
|
else
|
||||||
EXE = stockfish
|
EXE = stockfish
|
||||||
@@ -31,24 +49,22 @@ PREFIX = /usr/local
|
|||||||
BINDIR = $(PREFIX)/bin
|
BINDIR = $(PREFIX)/bin
|
||||||
|
|
||||||
### Built-in benchmark for pgo-builds
|
### Built-in benchmark for pgo-builds
|
||||||
PGOBENCH = ./$(EXE) bench
|
ifeq ($(SDE_PATH),)
|
||||||
|
PGOBENCH = $(WINE_PATH) ./$(EXE) bench
|
||||||
|
else
|
||||||
|
PGOBENCH = $(SDE_PATH) -- $(WINE_PATH) ./$(EXE) bench
|
||||||
|
endif
|
||||||
|
|
||||||
### Source and object files
|
### Source and object files
|
||||||
SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp \
|
SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp \
|
||||||
material.cpp misc.cpp movegen.cpp movepick.cpp pawns.cpp position.cpp psqt.cpp \
|
material.cpp misc.cpp movegen.cpp movepick.cpp pawns.cpp position.cpp psqt.cpp \
|
||||||
search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp \
|
search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp \
|
||||||
nnue/evaluate_nnue.cpp nnue/features/half_kp.cpp
|
nnue/evaluate_nnue.cpp nnue/features/half_ka_v2_hm.cpp
|
||||||
|
|
||||||
OBJS = $(notdir $(SRCS:.cpp=.o))
|
OBJS = $(notdir $(SRCS:.cpp=.o))
|
||||||
|
|
||||||
VPATH = syzygy:nnue:nnue/features
|
VPATH = syzygy:nnue:nnue/features
|
||||||
|
|
||||||
### Establish the operating system name
|
|
||||||
KERNEL = $(shell uname -s)
|
|
||||||
ifeq ($(KERNEL),Linux)
|
|
||||||
OS = $(shell uname -o)
|
|
||||||
endif
|
|
||||||
|
|
||||||
### ==========================================================================
|
### ==========================================================================
|
||||||
### Section 2. High-level Configuration
|
### Section 2. High-level Configuration
|
||||||
### ==========================================================================
|
### ==========================================================================
|
||||||
@@ -57,9 +73,11 @@ endif
|
|||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
|
# debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
|
||||||
# sanitize = undefined/thread/no (-fsanitize )
|
# sanitize = none/<sanitizer> ... (-fsanitize )
|
||||||
# --- ( undefined ) --- enable undefined behavior checks
|
# --- ( undefined ) --- enable undefined behavior checks
|
||||||
# --- ( thread ) --- enable threading error checks
|
# --- ( thread ) --- enable threading error checks
|
||||||
|
# --- ( address ) --- enable memory access checks
|
||||||
|
# --- ...etc... --- see compiler documentation for supported sanitizers
|
||||||
# optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
|
# optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
|
||||||
# arch = (name) --- (-arch) --- Target architecture
|
# arch = (name) --- (-arch) --- Target architecture
|
||||||
# bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
|
# bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
|
||||||
@@ -72,6 +90,7 @@ endif
|
|||||||
# ssse3 = yes/no --- -mssse3 --- Use Intel Supplemental Streaming SIMD Extensions 3
|
# ssse3 = yes/no --- -mssse3 --- Use Intel Supplemental Streaming SIMD Extensions 3
|
||||||
# sse41 = yes/no --- -msse4.1 --- Use Intel Streaming SIMD Extensions 4.1
|
# sse41 = yes/no --- -msse4.1 --- Use Intel Streaming SIMD Extensions 4.1
|
||||||
# avx2 = yes/no --- -mavx2 --- Use Intel Advanced Vector Extensions 2
|
# avx2 = yes/no --- -mavx2 --- Use Intel Advanced Vector Extensions 2
|
||||||
|
# avxvnni = yes/no --- -mavxvnni --- Use Intel Vector Neural Network Instructions AVX
|
||||||
# avx512 = yes/no --- -mavx512bw --- Use Intel Advanced Vector Extensions 512
|
# avx512 = yes/no --- -mavx512bw --- Use Intel Advanced Vector Extensions 512
|
||||||
# vnni256 = yes/no --- -mavx512vnni --- Use Intel Vector Neural Network Instructions 256
|
# vnni256 = yes/no --- -mavx512vnni --- Use Intel Vector Neural Network Instructions 256
|
||||||
# vnni512 = yes/no --- -mavx512vnni --- Use Intel Vector Neural Network Instructions 512
|
# vnni512 = yes/no --- -mavx512vnni --- Use Intel Vector Neural Network Instructions 512
|
||||||
@@ -80,6 +99,10 @@ endif
|
|||||||
# Note that Makefile is space sensitive, so when adding new architectures
|
# Note that Makefile is space sensitive, so when adding new architectures
|
||||||
# or modifying existing flags, you have to make sure there are no extra spaces
|
# or modifying existing flags, you have to make sure there are no extra spaces
|
||||||
# at the end of the line for flag values.
|
# at the end of the line for flag values.
|
||||||
|
#
|
||||||
|
# Example of use for these flags:
|
||||||
|
# make build ARCH=x86-64-avx512 debug=yes sanitize="address undefined"
|
||||||
|
|
||||||
|
|
||||||
### 2.1. General and architecture defaults
|
### 2.1. General and architecture defaults
|
||||||
|
|
||||||
@@ -90,10 +113,10 @@ endif
|
|||||||
# explicitly check for the list of supported architectures (as listed with make help),
|
# explicitly check for the list of supported architectures (as listed with make help),
|
||||||
# the user can override with `make ARCH=x86-32-vnni256 SUPPORTED_ARCH=true`
|
# the user can override with `make ARCH=x86-32-vnni256 SUPPORTED_ARCH=true`
|
||||||
ifeq ($(ARCH), $(filter $(ARCH), \
|
ifeq ($(ARCH), $(filter $(ARCH), \
|
||||||
x86-64-vnni512 x86-64-vnni256 x86-64-avx512 x86-64-bmi2 x86-64-avx2 \
|
x86-64-vnni512 x86-64-vnni256 x86-64-avx512 x86-64-avxvnni x86-64-bmi2 \
|
||||||
x86-64-sse41-popcnt x86-64-modern x86-64-ssse3 x86-64-sse3-popcnt \
|
x86-64-avx2 x86-64-sse41-popcnt x86-64-modern x86-64-ssse3 x86-64-sse3-popcnt \
|
||||||
x86-64 x86-32-sse41-popcnt x86-32-sse2 x86-32 ppc-64 ppc-32 \
|
x86-64 x86-32-sse41-popcnt x86-32-sse2 x86-32 ppc-64 ppc-32 e2k \
|
||||||
armv7 armv7-neon armv8 apple-silicon general-64 general-32))
|
armv7 armv7-neon armv8 apple-silicon general-64 general-32 riscv64))
|
||||||
SUPPORTED_ARCH=true
|
SUPPORTED_ARCH=true
|
||||||
else
|
else
|
||||||
SUPPORTED_ARCH=false
|
SUPPORTED_ARCH=false
|
||||||
@@ -101,7 +124,7 @@ endif
|
|||||||
|
|
||||||
optimize = yes
|
optimize = yes
|
||||||
debug = no
|
debug = no
|
||||||
sanitize = no
|
sanitize = none
|
||||||
bits = 64
|
bits = 64
|
||||||
prefetch = no
|
prefetch = no
|
||||||
popcnt = no
|
popcnt = no
|
||||||
@@ -112,10 +135,12 @@ sse2 = no
|
|||||||
ssse3 = no
|
ssse3 = no
|
||||||
sse41 = no
|
sse41 = no
|
||||||
avx2 = no
|
avx2 = no
|
||||||
|
avxvnni = no
|
||||||
avx512 = no
|
avx512 = no
|
||||||
vnni256 = no
|
vnni256 = no
|
||||||
vnni512 = no
|
vnni512 = no
|
||||||
neon = no
|
neon = no
|
||||||
|
arm_version = 0
|
||||||
STRIP = strip
|
STRIP = strip
|
||||||
|
|
||||||
### 2.2 Architecture specific
|
### 2.2 Architecture specific
|
||||||
@@ -127,7 +152,7 @@ ifeq ($(findstring x86,$(ARCH)),x86)
|
|||||||
ifeq ($(findstring x86-32,$(ARCH)),x86-32)
|
ifeq ($(findstring x86-32,$(ARCH)),x86-32)
|
||||||
arch = i386
|
arch = i386
|
||||||
bits = 32
|
bits = 32
|
||||||
sse = yes
|
sse = no
|
||||||
mmx = yes
|
mmx = yes
|
||||||
else
|
else
|
||||||
arch = x86_64
|
arch = x86_64
|
||||||
@@ -182,6 +207,17 @@ ifeq ($(findstring -avx2,$(ARCH)),-avx2)
|
|||||||
avx2 = yes
|
avx2 = yes
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(findstring -avxvnni,$(ARCH)),-avxvnni)
|
||||||
|
popcnt = yes
|
||||||
|
sse = yes
|
||||||
|
sse2 = yes
|
||||||
|
ssse3 = yes
|
||||||
|
sse41 = yes
|
||||||
|
avx2 = yes
|
||||||
|
avxvnni = yes
|
||||||
|
pext = yes
|
||||||
|
endif
|
||||||
|
|
||||||
ifeq ($(findstring -bmi2,$(ARCH)),-bmi2)
|
ifeq ($(findstring -bmi2,$(ARCH)),-bmi2)
|
||||||
popcnt = yes
|
popcnt = yes
|
||||||
sse = yes
|
sse = yes
|
||||||
@@ -252,6 +288,7 @@ ifeq ($(ARCH),armv7)
|
|||||||
arch = armv7
|
arch = armv7
|
||||||
prefetch = yes
|
prefetch = yes
|
||||||
bits = 32
|
bits = 32
|
||||||
|
arm_version = 7
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(ARCH),armv7-neon)
|
ifeq ($(ARCH),armv7-neon)
|
||||||
@@ -260,6 +297,7 @@ ifeq ($(ARCH),armv7-neon)
|
|||||||
popcnt = yes
|
popcnt = yes
|
||||||
neon = yes
|
neon = yes
|
||||||
bits = 32
|
bits = 32
|
||||||
|
arm_version = 7
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(ARCH),armv8)
|
ifeq ($(ARCH),armv8)
|
||||||
@@ -267,6 +305,7 @@ ifeq ($(ARCH),armv8)
|
|||||||
prefetch = yes
|
prefetch = yes
|
||||||
popcnt = yes
|
popcnt = yes
|
||||||
neon = yes
|
neon = yes
|
||||||
|
arm_version = 8
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(ARCH),apple-silicon)
|
ifeq ($(ARCH),apple-silicon)
|
||||||
@@ -274,6 +313,7 @@ ifeq ($(ARCH),apple-silicon)
|
|||||||
prefetch = yes
|
prefetch = yes
|
||||||
popcnt = yes
|
popcnt = yes
|
||||||
neon = yes
|
neon = yes
|
||||||
|
arm_version = 8
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(ARCH),ppc-32)
|
ifeq ($(ARCH),ppc-32)
|
||||||
@@ -287,16 +327,37 @@ ifeq ($(ARCH),ppc-64)
|
|||||||
prefetch = yes
|
prefetch = yes
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(findstring e2k,$(ARCH)),e2k)
|
||||||
|
arch = e2k
|
||||||
|
mmx = yes
|
||||||
|
bits = 64
|
||||||
|
sse = yes
|
||||||
|
sse2 = yes
|
||||||
|
ssse3 = yes
|
||||||
|
sse41 = yes
|
||||||
|
popcnt = yes
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(ARCH),riscv64)
|
||||||
|
arch = riscv64
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
### ==========================================================================
|
### ==========================================================================
|
||||||
### Section 3. Low-level Configuration
|
### Section 3. Low-level Configuration
|
||||||
### ==========================================================================
|
### ==========================================================================
|
||||||
|
|
||||||
### 3.1 Selecting compiler (default = gcc)
|
### 3.1 Selecting compiler (default = gcc)
|
||||||
CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++17 $(EXTRACXXFLAGS)
|
ifeq ($(MAKELEVEL),0)
|
||||||
DEPENDFLAGS += -std=c++17
|
export ENV_CXXFLAGS := $(CXXFLAGS)
|
||||||
LDFLAGS += $(EXTRALDFLAGS)
|
export ENV_DEPENDFLAGS := $(DEPENDFLAGS)
|
||||||
|
export ENV_LDFLAGS := $(LDFLAGS)
|
||||||
|
endif
|
||||||
|
|
||||||
|
CXXFLAGS = $(ENV_CXXFLAGS) -Wall -Wcast-qual -fno-exceptions -std=c++17 $(EXTRACXXFLAGS)
|
||||||
|
DEPENDFLAGS = $(ENV_DEPENDFLAGS) -std=c++17
|
||||||
|
LDFLAGS = $(ENV_LDFLAGS) $(EXTRALDFLAGS)
|
||||||
|
|
||||||
ifeq ($(COMP),)
|
ifeq ($(COMP),)
|
||||||
COMP=gcc
|
COMP=gcc
|
||||||
@@ -307,11 +368,14 @@ ifeq ($(COMP),gcc)
|
|||||||
CXX=g++
|
CXX=g++
|
||||||
CXXFLAGS += -pedantic -Wextra -Wshadow
|
CXXFLAGS += -pedantic -Wextra -Wshadow
|
||||||
|
|
||||||
ifeq ($(arch),$(filter $(arch),armv7 armv8))
|
ifeq ($(arch),$(filter $(arch),armv7 armv8 riscv64))
|
||||||
ifeq ($(OS),Android)
|
ifeq ($(OS),Android)
|
||||||
CXXFLAGS += -m$(bits)
|
CXXFLAGS += -m$(bits)
|
||||||
LDFLAGS += -m$(bits)
|
LDFLAGS += -m$(bits)
|
||||||
endif
|
endif
|
||||||
|
ifeq ($(ARCH),riscv64)
|
||||||
|
CXXFLAGS += -latomic
|
||||||
|
endif
|
||||||
else
|
else
|
||||||
CXXFLAGS += -m$(bits)
|
CXXFLAGS += -m$(bits)
|
||||||
LDFLAGS += -m$(bits)
|
LDFLAGS += -m$(bits)
|
||||||
@@ -326,29 +390,27 @@ ifeq ($(COMP),gcc)
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(target_windows),yes)
|
||||||
|
LDFLAGS += -static
|
||||||
|
endif
|
||||||
|
|
||||||
ifeq ($(COMP),mingw)
|
ifeq ($(COMP),mingw)
|
||||||
comp=mingw
|
comp=mingw
|
||||||
|
|
||||||
ifeq ($(KERNEL),Linux)
|
|
||||||
ifeq ($(bits),64)
|
ifeq ($(bits),64)
|
||||||
ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
|
ifeq ($(shell which x86_64-w64-mingw32-c++-posix 2> /dev/null),)
|
||||||
CXX=x86_64-w64-mingw32-c++
|
CXX=x86_64-w64-mingw32-c++
|
||||||
else
|
else
|
||||||
CXX=x86_64-w64-mingw32-c++-posix
|
CXX=x86_64-w64-mingw32-c++-posix
|
||||||
endif
|
endif
|
||||||
else
|
else
|
||||||
ifeq ($(shell which i686-w64-mingw32-c++-posix),)
|
ifeq ($(shell which i686-w64-mingw32-c++-posix 2> /dev/null),)
|
||||||
CXX=i686-w64-mingw32-c++
|
CXX=i686-w64-mingw32-c++
|
||||||
else
|
else
|
||||||
CXX=i686-w64-mingw32-c++-posix
|
CXX=i686-w64-mingw32-c++-posix
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
else
|
CXXFLAGS += -pedantic -Wextra -Wshadow
|
||||||
CXX=g++
|
|
||||||
endif
|
|
||||||
|
|
||||||
CXXFLAGS += -Wextra -Wshadow
|
|
||||||
LDFLAGS += -static
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(COMP),icc)
|
ifeq ($(COMP),icc)
|
||||||
@@ -360,21 +422,28 @@ endif
|
|||||||
ifeq ($(COMP),clang)
|
ifeq ($(COMP),clang)
|
||||||
comp=clang
|
comp=clang
|
||||||
CXX=clang++
|
CXX=clang++
|
||||||
|
ifeq ($(target_windows),yes)
|
||||||
|
CXX=x86_64-w64-mingw32-clang++
|
||||||
|
endif
|
||||||
|
|
||||||
CXXFLAGS += -pedantic -Wextra -Wshadow
|
CXXFLAGS += -pedantic -Wextra -Wshadow
|
||||||
|
|
||||||
ifneq ($(KERNEL),Darwin)
|
ifeq ($(filter $(KERNEL),Darwin OpenBSD FreeBSD),)
|
||||||
ifneq ($(KERNEL),OpenBSD)
|
ifeq ($(target_windows),)
|
||||||
ifneq ($(KERNEL),FreeBSD)
|
ifneq ($(RTLIB),compiler-rt)
|
||||||
LDFLAGS += -latomic
|
LDFLAGS += -latomic
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(arch),$(filter $(arch),armv7 armv8))
|
ifeq ($(arch),$(filter $(arch),armv7 armv8 riscv64))
|
||||||
ifeq ($(OS),Android)
|
ifeq ($(OS),Android)
|
||||||
CXXFLAGS += -m$(bits)
|
CXXFLAGS += -m$(bits)
|
||||||
LDFLAGS += -m$(bits)
|
LDFLAGS += -m$(bits)
|
||||||
endif
|
endif
|
||||||
|
ifeq ($(ARCH),riscv64)
|
||||||
|
CXXFLAGS += -latomic
|
||||||
|
endif
|
||||||
else
|
else
|
||||||
CXXFLAGS += -m$(bits)
|
CXXFLAGS += -m$(bits)
|
||||||
LDFLAGS += -m$(bits)
|
LDFLAGS += -m$(bits)
|
||||||
@@ -382,8 +451,12 @@ ifeq ($(COMP),clang)
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(KERNEL),Darwin)
|
ifeq ($(KERNEL),Darwin)
|
||||||
CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.14
|
CXXFLAGS += -mmacosx-version-min=10.14
|
||||||
LDFLAGS += -arch $(arch) -mmacosx-version-min=10.14
|
LDFLAGS += -mmacosx-version-min=10.14
|
||||||
|
ifneq ($(arch),any)
|
||||||
|
CXXFLAGS += -arch $(arch)
|
||||||
|
LDFLAGS += -arch $(arch)
|
||||||
|
endif
|
||||||
XCRUN = xcrun
|
XCRUN = xcrun
|
||||||
endif
|
endif
|
||||||
|
|
||||||
@@ -396,11 +469,19 @@ ifeq ($(COMP),ndk)
|
|||||||
ifeq ($(arch),armv7)
|
ifeq ($(arch),armv7)
|
||||||
CXX=armv7a-linux-androideabi16-clang++
|
CXX=armv7a-linux-androideabi16-clang++
|
||||||
CXXFLAGS += -mthumb -march=armv7-a -mfloat-abi=softfp -mfpu=neon
|
CXXFLAGS += -mthumb -march=armv7-a -mfloat-abi=softfp -mfpu=neon
|
||||||
|
ifneq ($(shell which arm-linux-androideabi-strip 2>/dev/null),)
|
||||||
STRIP=arm-linux-androideabi-strip
|
STRIP=arm-linux-androideabi-strip
|
||||||
|
else
|
||||||
|
STRIP=llvm-strip
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
ifeq ($(arch),armv8)
|
ifeq ($(arch),armv8)
|
||||||
CXX=aarch64-linux-android21-clang++
|
CXX=aarch64-linux-android21-clang++
|
||||||
|
ifneq ($(shell which aarch64-linux-android-strip 2>/dev/null),)
|
||||||
STRIP=aarch64-linux-android-strip
|
STRIP=aarch64-linux-android-strip
|
||||||
|
else
|
||||||
|
STRIP=llvm-strip
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
LDFLAGS += -static-libstdc++ -pie -lm -latomic
|
LDFLAGS += -static-libstdc++ -pie -lm -latomic
|
||||||
endif
|
endif
|
||||||
@@ -414,6 +495,9 @@ else ifeq ($(comp),clang)
|
|||||||
else
|
else
|
||||||
profile_make = gcc-profile-make
|
profile_make = gcc-profile-make
|
||||||
profile_use = gcc-profile-use
|
profile_use = gcc-profile-use
|
||||||
|
ifeq ($(KERNEL),Darwin)
|
||||||
|
EXTRAPROFILEFLAGS = -fvisibility=hidden
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### Travis CI script uses COMPILER to overwrite CXX
|
### Travis CI script uses COMPILER to overwrite CXX
|
||||||
@@ -458,9 +542,9 @@ else
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.2.2 Debugging with undefined behavior sanitizers
|
### 3.2.2 Debugging with undefined behavior sanitizers
|
||||||
ifneq ($(sanitize),no)
|
ifneq ($(sanitize),none)
|
||||||
CXXFLAGS += -g3 -fsanitize=$(sanitize)
|
CXXFLAGS += -g3 $(addprefix -fsanitize=,$(sanitize))
|
||||||
LDFLAGS += -fsanitize=$(sanitize)
|
LDFLAGS += $(addprefix -fsanitize=,$(sanitize))
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.3 Optimization
|
### 3.3 Optimization
|
||||||
@@ -474,10 +558,16 @@ ifeq ($(optimize),yes)
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(comp),$(filter $(comp),gcc clang icc))
|
|
||||||
ifeq ($(KERNEL),Darwin)
|
ifeq ($(KERNEL),Darwin)
|
||||||
|
ifeq ($(comp),$(filter $(comp),clang icc))
|
||||||
CXXFLAGS += -mdynamic-no-pic
|
CXXFLAGS += -mdynamic-no-pic
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(comp),gcc)
|
||||||
|
ifneq ($(arch),arm64)
|
||||||
|
CXXFLAGS += -mdynamic-no-pic
|
||||||
|
endif
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(comp),clang)
|
ifeq ($(comp),clang)
|
||||||
@@ -490,7 +580,7 @@ ifeq ($(bits),64)
|
|||||||
CXXFLAGS += -DIS_64BIT
|
CXXFLAGS += -DIS_64BIT
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.5 prefetch
|
### 3.5 prefetch and popcount
|
||||||
ifeq ($(prefetch),yes)
|
ifeq ($(prefetch),yes)
|
||||||
ifeq ($(sse),yes)
|
ifeq ($(sse),yes)
|
||||||
CXXFLAGS += -msse
|
CXXFLAGS += -msse
|
||||||
@@ -499,7 +589,6 @@ else
|
|||||||
CXXFLAGS += -DNO_PREFETCH
|
CXXFLAGS += -DNO_PREFETCH
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.6 popcnt
|
|
||||||
ifeq ($(popcnt),yes)
|
ifeq ($(popcnt),yes)
|
||||||
ifeq ($(arch),$(filter $(arch),ppc64 armv7 armv8 arm64))
|
ifeq ($(arch),$(filter $(arch),ppc64 armv7 armv8 arm64))
|
||||||
CXXFLAGS += -DUSE_POPCNT
|
CXXFLAGS += -DUSE_POPCNT
|
||||||
@@ -510,11 +599,18 @@ ifeq ($(popcnt),yes)
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
### 3.6 SIMD architectures
|
||||||
ifeq ($(avx2),yes)
|
ifeq ($(avx2),yes)
|
||||||
CXXFLAGS += -DUSE_AVX2
|
CXXFLAGS += -DUSE_AVX2
|
||||||
ifeq ($(comp),$(filter $(comp),gcc clang mingw))
|
ifeq ($(comp),$(filter $(comp),gcc clang mingw))
|
||||||
CXXFLAGS += -mavx2
|
CXXFLAGS += -mavx2 -mbmi
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(avxvnni),yes)
|
||||||
|
CXXFLAGS += -DUSE_VNNI -DUSE_AVXVNNI
|
||||||
|
ifeq ($(comp),$(filter $(comp),gcc clang mingw))
|
||||||
|
CXXFLAGS += -mavxvnni
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
@@ -568,7 +664,7 @@ ifeq ($(mmx),yes)
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(neon),yes)
|
ifeq ($(neon),yes)
|
||||||
CXXFLAGS += -DUSE_NEON
|
CXXFLAGS += -DUSE_NEON=$(arm_version)
|
||||||
ifeq ($(KERNEL),Linux)
|
ifeq ($(KERNEL),Linux)
|
||||||
ifneq ($(COMP),ndk)
|
ifneq ($(COMP),ndk)
|
||||||
ifneq ($(arch),armv8)
|
ifneq ($(arch),armv8)
|
||||||
@@ -586,16 +682,26 @@ ifeq ($(pext),yes)
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
### 3.7.1 Try to include git commit sha for versioning
|
||||||
|
GIT_SHA = $(shell git rev-parse --short HEAD 2>/dev/null)
|
||||||
|
ifneq ($(GIT_SHA), )
|
||||||
|
CXXFLAGS += -DGIT_SHA=\"$(GIT_SHA)\"
|
||||||
|
endif
|
||||||
|
|
||||||
|
### 3.7.2 Try to include git commit date for versioning
|
||||||
|
GIT_DATE = $(shell git show -s --date=format:'%Y%m%d' --format=%cd HEAD 2>/dev/null)
|
||||||
|
ifneq ($(GIT_DATE), )
|
||||||
|
CXXFLAGS += -DGIT_DATE=\"$(GIT_DATE)\"
|
||||||
|
endif
|
||||||
|
|
||||||
### 3.8 Link Time Optimization
|
### 3.8 Link Time Optimization
|
||||||
### This is a mix of compile and link time options because the lto link phase
|
### This is a mix of compile and link time options because the lto link phase
|
||||||
### needs access to the optimization flags.
|
### needs access to the optimization flags.
|
||||||
ifeq ($(optimize),yes)
|
ifeq ($(optimize),yes)
|
||||||
ifeq ($(debug), no)
|
ifeq ($(debug), no)
|
||||||
ifeq ($(comp),clang)
|
ifeq ($(comp),clang)
|
||||||
CXXFLAGS += -flto
|
CXXFLAGS += -flto=full
|
||||||
ifneq ($(findstring MINGW,$(KERNEL)),)
|
ifeq ($(target_windows),yes)
|
||||||
CXXFLAGS += -fuse-ld=lld
|
|
||||||
else ifneq ($(findstring MSYS,$(KERNEL)),)
|
|
||||||
CXXFLAGS += -fuse-ld=lld
|
CXXFLAGS += -fuse-ld=lld
|
||||||
endif
|
endif
|
||||||
LDFLAGS += $(CXXFLAGS)
|
LDFLAGS += $(CXXFLAGS)
|
||||||
@@ -604,28 +710,18 @@ ifeq ($(debug), no)
|
|||||||
# GCC on some systems.
|
# GCC on some systems.
|
||||||
else ifeq ($(comp),gcc)
|
else ifeq ($(comp),gcc)
|
||||||
ifeq ($(gccisclang),)
|
ifeq ($(gccisclang),)
|
||||||
CXXFLAGS += -flto
|
CXXFLAGS += -flto -flto-partition=one
|
||||||
LDFLAGS += $(CXXFLAGS) -flto=jobserver
|
LDFLAGS += $(CXXFLAGS) -flto=jobserver
|
||||||
ifneq ($(findstring MINGW,$(KERNEL)),)
|
|
||||||
LDFLAGS += -save-temps
|
|
||||||
else ifneq ($(findstring MSYS,$(KERNEL)),)
|
|
||||||
LDFLAGS += -save-temps
|
|
||||||
endif
|
|
||||||
else
|
else
|
||||||
CXXFLAGS += -flto
|
CXXFLAGS += -flto=full
|
||||||
LDFLAGS += $(CXXFLAGS)
|
LDFLAGS += $(CXXFLAGS)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# To use LTO and static linking on windows, the tool chain requires a recent gcc:
|
# To use LTO and static linking on Windows,
|
||||||
# gcc version 10.1 in msys2 or TDM-GCC version 9.2 are known to work, older might not.
|
# the tool chain requires gcc version 10.1 or later.
|
||||||
# So, only enable it for a cross from Linux by default.
|
|
||||||
else ifeq ($(comp),mingw)
|
else ifeq ($(comp),mingw)
|
||||||
ifeq ($(KERNEL),Linux)
|
CXXFLAGS += -flto -flto-partition=one
|
||||||
ifneq ($(arch),i386)
|
LDFLAGS += $(CXXFLAGS) -save-temps
|
||||||
CXXFLAGS += -flto
|
|
||||||
LDFLAGS += $(CXXFLAGS) -flto=jobserver
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
@@ -663,6 +759,7 @@ help:
|
|||||||
@echo "x86-64-vnni512 > x86 64-bit with vnni support 512bit wide"
|
@echo "x86-64-vnni512 > x86 64-bit with vnni support 512bit wide"
|
||||||
@echo "x86-64-vnni256 > x86 64-bit with vnni support 256bit wide"
|
@echo "x86-64-vnni256 > x86 64-bit with vnni support 256bit wide"
|
||||||
@echo "x86-64-avx512 > x86 64-bit with avx512 support"
|
@echo "x86-64-avx512 > x86 64-bit with avx512 support"
|
||||||
|
@echo "x86-64-avxvnni > x86 64-bit with avxvnni support"
|
||||||
@echo "x86-64-bmi2 > x86 64-bit with bmi2 support"
|
@echo "x86-64-bmi2 > x86 64-bit with bmi2 support"
|
||||||
@echo "x86-64-avx2 > x86 64-bit with avx2 support"
|
@echo "x86-64-avx2 > x86 64-bit with avx2 support"
|
||||||
@echo "x86-64-sse41-popcnt > x86 64-bit with sse41 and popcnt support"
|
@echo "x86-64-sse41-popcnt > x86 64-bit with sse41 and popcnt support"
|
||||||
@@ -678,9 +775,11 @@ help:
|
|||||||
@echo "armv7 > ARMv7 32-bit"
|
@echo "armv7 > ARMv7 32-bit"
|
||||||
@echo "armv7-neon > ARMv7 32-bit with popcnt and neon"
|
@echo "armv7-neon > ARMv7 32-bit with popcnt and neon"
|
||||||
@echo "armv8 > ARMv8 64-bit with popcnt and neon"
|
@echo "armv8 > ARMv8 64-bit with popcnt and neon"
|
||||||
|
@echo "e2k > Elbrus 2000"
|
||||||
@echo "apple-silicon > Apple silicon ARM64"
|
@echo "apple-silicon > Apple silicon ARM64"
|
||||||
@echo "general-64 > unspecified 64-bit"
|
@echo "general-64 > unspecified 64-bit"
|
||||||
@echo "general-32 > unspecified 32-bit"
|
@echo "general-32 > unspecified 32-bit"
|
||||||
|
@echo "riscv64 > RISC-V 64-bit"
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Supported compilers:"
|
@echo "Supported compilers:"
|
||||||
@echo ""
|
@echo ""
|
||||||
@@ -713,7 +812,7 @@ endif
|
|||||||
|
|
||||||
.PHONY: help build profile-build strip install clean net objclean profileclean \
|
.PHONY: help build profile-build strip install clean net objclean profileclean \
|
||||||
config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
|
config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
|
||||||
clang-profile-use clang-profile-make
|
clang-profile-use clang-profile-make FORCE
|
||||||
|
|
||||||
build: net config-sanity
|
build: net config-sanity
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
|
||||||
@@ -724,7 +823,7 @@ profile-build: net config-sanity objclean profileclean
|
|||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Step 2/4. Running benchmark for pgo-build ..."
|
@echo "Step 2/4. Running benchmark for pgo-build ..."
|
||||||
$(PGOBENCH) > /dev/null
|
$(PGOBENCH) 2>&1 | tail -n 4
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Step 3/4. Building optimized executable ..."
|
@echo "Step 3/4. Building optimized executable ..."
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
|
||||||
@@ -739,7 +838,7 @@ strip:
|
|||||||
install:
|
install:
|
||||||
-mkdir -p -m 755 $(BINDIR)
|
-mkdir -p -m 755 $(BINDIR)
|
||||||
-cp $(EXE) $(BINDIR)
|
-cp $(EXE) $(BINDIR)
|
||||||
-strip $(BINDIR)/$(EXE)
|
$(STRIP) $(BINDIR)/$(EXE)
|
||||||
|
|
||||||
# clean all
|
# clean all
|
||||||
clean: objclean profileclean
|
clean: objclean profileclean
|
||||||
@@ -749,35 +848,49 @@ clean: objclean profileclean
|
|||||||
net:
|
net:
|
||||||
$(eval nnuenet := $(shell grep EvalFileDefaultName evaluate.h | grep define | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
|
$(eval nnuenet := $(shell grep EvalFileDefaultName evaluate.h | grep define | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
|
||||||
@echo "Default net: $(nnuenet)"
|
@echo "Default net: $(nnuenet)"
|
||||||
$(eval nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(nnuenet))
|
$(eval nnuedownloadurl1 := https://tests.stockfishchess.org/api/nn/$(nnuenet))
|
||||||
|
$(eval nnuedownloadurl2 := https://github.com/official-stockfish/networks/raw/master/$(nnuenet))
|
||||||
$(eval curl_or_wget := $(shell if hash curl 2>/dev/null; then echo "curl -skL"; elif hash wget 2>/dev/null; then echo "wget -qO-"; fi))
|
$(eval curl_or_wget := $(shell if hash curl 2>/dev/null; then echo "curl -skL"; elif hash wget 2>/dev/null; then echo "wget -qO-"; fi))
|
||||||
@if test -f "$(nnuenet)"; then \
|
@if [ "x$(curl_or_wget)" = "x" ]; then \
|
||||||
echo "Already available."; \
|
|
||||||
else \
|
|
||||||
if [ "x$(curl_or_wget)" = "x" ]; then \
|
|
||||||
echo "Automatic download failed: neither curl nor wget is installed. Install one of these tools or download the net manually"; exit 1; \
|
echo "Automatic download failed: neither curl nor wget is installed. Install one of these tools or download the net manually"; exit 1; \
|
||||||
else \
|
fi
|
||||||
echo "Downloading $(nnuedownloadurl)"; $(curl_or_wget) $(nnuedownloadurl) > $(nnuenet);\
|
|
||||||
fi; \
|
|
||||||
fi;
|
|
||||||
$(eval shasum_command := $(shell if hash shasum 2>/dev/null; then echo "shasum -a 256 "; elif hash sha256sum 2>/dev/null; then echo "sha256sum "; fi))
|
$(eval shasum_command := $(shell if hash shasum 2>/dev/null; then echo "shasum -a 256 "; elif hash sha256sum 2>/dev/null; then echo "sha256sum "; fi))
|
||||||
@if [ "x$(shasum_command)" != "x" ]; then \
|
@if [ "x$(shasum_command)" = "x" ]; then \
|
||||||
if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \
|
|
||||||
echo "Failed download or $(nnuenet) corrupted, please delete!"; exit 1; \
|
|
||||||
fi \
|
|
||||||
else \
|
|
||||||
echo "shasum / sha256sum not found, skipping net validation"; \
|
echo "shasum / sha256sum not found, skipping net validation"; \
|
||||||
fi
|
fi
|
||||||
|
@for nnuedownloadurl in "$(nnuedownloadurl1)" "$(nnuedownloadurl2)"; do \
|
||||||
|
if test -f "$(nnuenet)"; then \
|
||||||
|
echo "$(nnuenet) available."; \
|
||||||
|
else \
|
||||||
|
if [ "x$(curl_or_wget)" != "x" ]; then \
|
||||||
|
echo "Downloading $${nnuedownloadurl}"; $(curl_or_wget) $${nnuedownloadurl} > $(nnuenet);\
|
||||||
|
fi; \
|
||||||
|
fi; \
|
||||||
|
if [ "x$(shasum_command)" != "x" ]; then \
|
||||||
|
if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \
|
||||||
|
echo "Removing failed download"; rm -f $(nnuenet); \
|
||||||
|
else \
|
||||||
|
echo "Network validated"; break; \
|
||||||
|
fi; \
|
||||||
|
fi; \
|
||||||
|
done
|
||||||
|
@if ! test -f "$(nnuenet)"; then \
|
||||||
|
echo "Failed to download $(nnuenet)."; \
|
||||||
|
fi
|
||||||
|
|
||||||
# clean binaries and objects
|
# clean binaries and objects
|
||||||
objclean:
|
objclean:
|
||||||
@rm -f $(EXE) *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o
|
@rm -f stockfish stockfish.exe *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o
|
||||||
|
|
||||||
# clean auxiliary profiling files
|
# clean auxiliary profiling files
|
||||||
profileclean:
|
profileclean:
|
||||||
@rm -rf profdir
|
@rm -rf profdir
|
||||||
@rm -f bench.txt *.gcda *.gcno ./syzygy/*.gcda ./nnue/*.gcda ./nnue/features/*.gcda *.s
|
@rm -f bench.txt *.gcda *.gcno ./syzygy/*.gcda ./nnue/*.gcda ./nnue/features/*.gcda *.s
|
||||||
@rm -f stockfish.profdata *.profraw
|
@rm -f stockfish.profdata *.profraw
|
||||||
|
@rm -f stockfish.*args*
|
||||||
|
@rm -f stockfish.*lt*
|
||||||
|
@rm -f stockfish.res
|
||||||
|
@rm -f ./-lstdc++.res
|
||||||
|
|
||||||
default:
|
default:
|
||||||
help
|
help
|
||||||
@@ -807,10 +920,12 @@ config-sanity: net
|
|||||||
@echo "ssse3: '$(ssse3)'"
|
@echo "ssse3: '$(ssse3)'"
|
||||||
@echo "sse41: '$(sse41)'"
|
@echo "sse41: '$(sse41)'"
|
||||||
@echo "avx2: '$(avx2)'"
|
@echo "avx2: '$(avx2)'"
|
||||||
|
@echo "avxvnni: '$(avxvnni)'"
|
||||||
@echo "avx512: '$(avx512)'"
|
@echo "avx512: '$(avx512)'"
|
||||||
@echo "vnni256: '$(vnni256)'"
|
@echo "vnni256: '$(vnni256)'"
|
||||||
@echo "vnni512: '$(vnni512)'"
|
@echo "vnni512: '$(vnni512)'"
|
||||||
@echo "neon: '$(neon)'"
|
@echo "neon: '$(neon)'"
|
||||||
|
@echo "arm_version: '$(arm_version)'"
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Flags:"
|
@echo "Flags:"
|
||||||
@echo "CXX: $(CXX)"
|
@echo "CXX: $(CXX)"
|
||||||
@@ -820,12 +935,11 @@ config-sanity: net
|
|||||||
@echo "Testing config sanity. If this fails, try 'make help' ..."
|
@echo "Testing config sanity. If this fails, try 'make help' ..."
|
||||||
@echo ""
|
@echo ""
|
||||||
@test "$(debug)" = "yes" || test "$(debug)" = "no"
|
@test "$(debug)" = "yes" || test "$(debug)" = "no"
|
||||||
@test "$(sanitize)" = "undefined" || test "$(sanitize)" = "thread" || test "$(sanitize)" = "address" || test "$(sanitize)" = "no"
|
|
||||||
@test "$(optimize)" = "yes" || test "$(optimize)" = "no"
|
@test "$(optimize)" = "yes" || test "$(optimize)" = "no"
|
||||||
@test "$(SUPPORTED_ARCH)" = "true"
|
@test "$(SUPPORTED_ARCH)" = "true"
|
||||||
@test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
|
@test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
|
||||||
test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || \
|
test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "e2k" || \
|
||||||
test "$(arch)" = "armv7" || test "$(arch)" = "armv8" || test "$(arch)" = "arm64"
|
test "$(arch)" = "armv7" || test "$(arch)" = "armv8" || test "$(arch)" = "arm64" || test "$(arch)" = "riscv64"
|
||||||
@test "$(bits)" = "32" || test "$(bits)" = "64"
|
@test "$(bits)" = "32" || test "$(bits)" = "64"
|
||||||
@test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
|
@test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
|
||||||
@test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
|
@test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
|
||||||
@@ -846,6 +960,10 @@ config-sanity: net
|
|||||||
$(EXE): $(OBJS)
|
$(EXE): $(OBJS)
|
||||||
+$(CXX) -o $@ $(OBJS) $(LDFLAGS)
|
+$(CXX) -o $@ $(OBJS) $(LDFLAGS)
|
||||||
|
|
||||||
|
# Force recompilation to ensure version info is up-to-date
|
||||||
|
misc.o: FORCE
|
||||||
|
FORCE:
|
||||||
|
|
||||||
clang-profile-make:
|
clang-profile-make:
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||||
EXTRACXXFLAGS='-fprofile-instr-generate ' \
|
EXTRACXXFLAGS='-fprofile-instr-generate ' \
|
||||||
@@ -860,14 +978,17 @@ clang-profile-use:
|
|||||||
all
|
all
|
||||||
|
|
||||||
gcc-profile-make:
|
gcc-profile-make:
|
||||||
|
@mkdir -p profdir
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||||
EXTRACXXFLAGS='-fprofile-generate' \
|
EXTRACXXFLAGS='-fprofile-generate=profdir' \
|
||||||
|
EXTRACXXFLAGS+=$(EXTRAPROFILEFLAGS) \
|
||||||
EXTRALDFLAGS='-lgcov' \
|
EXTRALDFLAGS='-lgcov' \
|
||||||
all
|
all
|
||||||
|
|
||||||
gcc-profile-use:
|
gcc-profile-use:
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||||
EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
|
EXTRACXXFLAGS='-fprofile-use=profdir -fno-peel-loops -fno-tracer' \
|
||||||
|
EXTRACXXFLAGS+=$(EXTRAPROFILEFLAGS) \
|
||||||
EXTRALDFLAGS='-lgcov' \
|
EXTRALDFLAGS='-lgcov' \
|
||||||
all
|
all
|
||||||
|
|
||||||
@@ -882,7 +1003,7 @@ icc-profile-use:
|
|||||||
EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
|
EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
|
||||||
all
|
all
|
||||||
|
|
||||||
.depend:
|
.depend: $(SRCS)
|
||||||
-@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null
|
-@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null
|
||||||
|
|
||||||
-include .depend
|
-include .depend
|
||||||
|
|||||||
+6
-1
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -87,11 +87,14 @@ const vector<string> Defaults = {
|
|||||||
// Chess 960
|
// Chess 960
|
||||||
"setoption name UCI_Chess960 value true",
|
"setoption name UCI_Chess960 value true",
|
||||||
"bbqnnrkr/pppppppp/8/8/8/8/PPPPPPPP/BBQNNRKR w HFhf - 0 1 moves g2g3 d7d5 d2d4 c8h3 c1g5 e8d6 g5e7 f7f6",
|
"bbqnnrkr/pppppppp/8/8/8/8/PPPPPPPP/BBQNNRKR w HFhf - 0 1 moves g2g3 d7d5 d2d4 c8h3 c1g5 e8d6 g5e7 f7f6",
|
||||||
|
"nqbnrkrb/pppppppp/8/8/8/8/PPPPPPPP/NQBNRKRB w KQkq - 0 1",
|
||||||
"setoption name UCI_Chess960 value false"
|
"setoption name UCI_Chess960 value false"
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
/// setup_bench() builds a list of UCI commands to be run by bench. There
|
/// 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
|
/// 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
|
/// should be used, the limit value spent for each position, a file name
|
||||||
@@ -168,3 +171,5 @@ vector<string> setup_bench(const Position& current, istream& is) {
|
|||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+7
-5
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -23,6 +23,8 @@
|
|||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// There are 24 possible pawn squares: files A to D and ranks from 2 to 7.
|
// There are 24 possible pawn squares: files A to D and ranks from 2 to 7.
|
||||||
@@ -66,7 +68,6 @@ namespace {
|
|||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color stm) {
|
bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color stm) {
|
||||||
|
|
||||||
assert(file_of(wpsq) <= FILE_D);
|
assert(file_of(wpsq) <= FILE_D);
|
||||||
@@ -96,7 +97,6 @@ void Bitbases::init() {
|
|||||||
KPKBitbase.set(idx);
|
KPKBitbase.set(idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
KPKPosition::KPKPosition(unsigned idx) {
|
KPKPosition::KPKPosition(unsigned idx) {
|
||||||
@@ -150,8 +150,8 @@ namespace {
|
|||||||
Bitboard b = attacks_bb<KING>(ksq[stm]);
|
Bitboard b = attacks_bb<KING>(ksq[stm]);
|
||||||
|
|
||||||
while (b)
|
while (b)
|
||||||
r |= stm == WHITE ? db[index(BLACK, ksq[BLACK] , pop_lsb(&b), psq)]
|
r |= stm == WHITE ? db[index(BLACK, ksq[BLACK], pop_lsb(b), psq)]
|
||||||
: db[index(WHITE, pop_lsb(&b), ksq[WHITE], psq)];
|
: db[index(WHITE, pop_lsb(b), ksq[WHITE], psq)];
|
||||||
|
|
||||||
if (stm == WHITE)
|
if (stm == WHITE)
|
||||||
{
|
{
|
||||||
@@ -168,3 +168,5 @@ namespace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+13
-4
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -22,11 +22,14 @@
|
|||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
uint8_t PopCnt16[1 << 16];
|
uint8_t PopCnt16[1 << 16];
|
||||||
uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
|
uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||||
|
|
||||||
Bitboard SquareBB[SQUARE_NB];
|
Bitboard SquareBB[SQUARE_NB];
|
||||||
Bitboard LineBB[SQUARE_NB][SQUARE_NB];
|
Bitboard LineBB[SQUARE_NB][SQUARE_NB];
|
||||||
|
Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
|
||||||
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
||||||
Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
|
Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
|
||||||
|
|
||||||
@@ -42,7 +45,6 @@ namespace {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// safe_destination() returns the bitboard of target square for the given step
|
/// safe_destination() returns the bitboard of target square for the given step
|
||||||
/// from the given square. If the step is off the board, returns empty bitboard.
|
/// from the given square. If the step is off the board, returns empty bitboard.
|
||||||
|
|
||||||
@@ -55,7 +57,7 @@ inline Bitboard safe_destination(Square s, int step) {
|
|||||||
/// Bitboards::pretty() returns an ASCII representation of a bitboard suitable
|
/// Bitboards::pretty() returns an ASCII representation of a bitboard suitable
|
||||||
/// to be printed to standard output. Useful for debugging.
|
/// to be printed to standard output. Useful for debugging.
|
||||||
|
|
||||||
const std::string Bitboards::pretty(Bitboard b) {
|
std::string Bitboards::pretty(Bitboard b) {
|
||||||
|
|
||||||
std::string s = "+---+---+---+---+---+---+---+---+\n";
|
std::string s = "+---+---+---+---+---+---+---+---+\n";
|
||||||
|
|
||||||
@@ -106,11 +108,16 @@ void Bitboards::init() {
|
|||||||
|
|
||||||
for (PieceType pt : { BISHOP, ROOK })
|
for (PieceType pt : { BISHOP, ROOK })
|
||||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
|
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
|
||||||
|
{
|
||||||
if (PseudoAttacks[pt][s1] & s2)
|
if (PseudoAttacks[pt][s1] & s2)
|
||||||
|
{
|
||||||
LineBB[s1][s2] = (attacks_bb(pt, s1, 0) & attacks_bb(pt, s2, 0)) | s1 | s2;
|
LineBB[s1][s2] = (attacks_bb(pt, s1, 0) & attacks_bb(pt, s2, 0)) | s1 | s2;
|
||||||
|
BetweenBB[s1][s2] = (attacks_bb(pt, s1, square_bb(s2)) & attacks_bb(pt, s2, square_bb(s1)));
|
||||||
|
}
|
||||||
|
BetweenBB[s1][s2] |= s2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@@ -211,3 +218,5 @@ namespace {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+34
-16
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -23,19 +23,21 @@
|
|||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
namespace Bitbases {
|
namespace Bitbases {
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
bool probe(Square wksq, Square wpsq, Square bksq, Color us);
|
bool probe(Square wksq, Square wpsq, Square bksq, Color us);
|
||||||
|
|
||||||
}
|
} // namespace Stockfish::Bitbases
|
||||||
|
|
||||||
namespace Bitboards {
|
namespace Bitboards {
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
const std::string pretty(Bitboard b);
|
std::string pretty(Bitboard b);
|
||||||
|
|
||||||
}
|
} // namespace Stockfish::Bitboards
|
||||||
|
|
||||||
constexpr Bitboard AllSquares = ~Bitboard(0);
|
constexpr Bitboard AllSquares = ~Bitboard(0);
|
||||||
constexpr Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
|
constexpr Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
|
||||||
@@ -73,6 +75,7 @@ extern uint8_t PopCnt16[1 << 16];
|
|||||||
extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
|
extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||||
|
|
||||||
extern Bitboard SquareBB[SQUARE_NB];
|
extern Bitboard SquareBB[SQUARE_NB];
|
||||||
|
extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
|
||||||
extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
|
extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
|
||||||
extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
||||||
extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
|
extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
|
||||||
@@ -209,23 +212,29 @@ constexpr Bitboard adjacent_files_bb(Square s) {
|
|||||||
inline Bitboard line_bb(Square s1, Square s2) {
|
inline Bitboard line_bb(Square s1, Square s2) {
|
||||||
|
|
||||||
assert(is_ok(s1) && is_ok(s2));
|
assert(is_ok(s1) && is_ok(s2));
|
||||||
|
|
||||||
return LineBB[s1][s2];
|
return LineBB[s1][s2];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// between_bb() returns a bitboard representing squares that are linearly
|
/// between_bb(s1, s2) returns a bitboard representing the squares in the semi-open
|
||||||
/// between the two given squares (excluding the given squares). If the given
|
/// segment between the squares s1 and s2 (excluding s1 but including s2). If the
|
||||||
/// squares are not on a same file/rank/diagonal, we return 0. For instance,
|
/// given squares are not on a same file/rank/diagonal, it returns s2. For instance,
|
||||||
/// between_bb(SQ_C4, SQ_F7) will return a bitboard with squares D5 and E6.
|
/// between_bb(SQ_C4, SQ_F7) will return a bitboard with squares D5, E6 and F7, but
|
||||||
|
/// between_bb(SQ_E6, SQ_F8) will return a bitboard with the square F8. This trick
|
||||||
|
/// allows to generate non-king evasion moves faster: the defending piece must either
|
||||||
|
/// interpose itself to cover the check or capture the checking piece.
|
||||||
|
|
||||||
inline Bitboard between_bb(Square s1, Square s2) {
|
inline Bitboard between_bb(Square s1, Square s2) {
|
||||||
Bitboard b = line_bb(s1, s2) & ((AllSquares << s1) ^ (AllSquares << s2));
|
|
||||||
return b & (b - 1); //exclude lsb
|
assert(is_ok(s1) && is_ok(s2));
|
||||||
|
|
||||||
|
return BetweenBB[s1][s2];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// forward_ranks_bb() returns a bitboard representing the squares on the ranks
|
/// forward_ranks_bb() returns a bitboard representing the squares on the ranks in
|
||||||
/// in front of the given one, from the point of view of the given color. For instance,
|
/// front of the given one, from the point of view of the given color. For instance,
|
||||||
/// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
|
/// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
|
||||||
|
|
||||||
constexpr Bitboard forward_ranks_bb(Color c, Square s) {
|
constexpr Bitboard forward_ranks_bb(Color c, Square s) {
|
||||||
@@ -412,13 +421,20 @@ inline Square msb(Bitboard b) {
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/// least_significant_square_bb() returns the bitboard of the least significant
|
||||||
|
/// square of a non-zero bitboard. It is equivalent to square_bb(lsb(bb)).
|
||||||
|
|
||||||
|
inline Bitboard least_significant_square_bb(Bitboard b) {
|
||||||
|
assert(b);
|
||||||
|
return b & -b;
|
||||||
|
}
|
||||||
|
|
||||||
/// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
|
/// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
|
||||||
|
|
||||||
inline Square pop_lsb(Bitboard* b) {
|
inline Square pop_lsb(Bitboard& b) {
|
||||||
assert(*b);
|
assert(b);
|
||||||
const Square s = lsb(*b);
|
const Square s = lsb(b);
|
||||||
*b &= *b - 1;
|
b &= b - 1;
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -430,4 +446,6 @@ inline Square frontmost_sq(Color c, Bitboard b) {
|
|||||||
return c == WHITE ? msb(b) : lsb(b);
|
return c == WHITE ? msb(b) : lsb(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef BITBOARD_H_INCLUDED
|
#endif // #ifndef BITBOARD_H_INCLUDED
|
||||||
|
|||||||
+5
-1
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -22,6 +22,8 @@
|
|||||||
#include "endgame.h"
|
#include "endgame.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// Used to drive the king towards the edge of the board
|
// Used to drive the king towards the edge of the board
|
||||||
@@ -741,3 +743,5 @@ ScaleFactor Endgame<KPKP>::operator()(const Position& pos) const {
|
|||||||
// it's probably at least a draw even with the pawn.
|
// it's probably at least a draw even with the pawn.
|
||||||
return Bitbases::probe(strongKing, strongPawn, weakKing, us) ? SCALE_FACTOR_NONE : SCALE_FACTOR_DRAW;
|
return Bitbases::probe(strongKing, strongPawn, weakKing, us) ? SCALE_FACTOR_NONE : SCALE_FACTOR_DRAW;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+4
-1
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -28,6 +28,7 @@
|
|||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
/// EndgameCode lists all supported endgame functions by corresponding codes
|
/// EndgameCode lists all supported endgame functions by corresponding codes
|
||||||
|
|
||||||
@@ -120,4 +121,6 @@ namespace Endgames {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef ENDGAME_H_INCLUDED
|
#endif // #ifndef ENDGAME_H_INCLUDED
|
||||||
|
|||||||
+126
-103
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -33,6 +33,7 @@
|
|||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "pawns.h"
|
#include "pawns.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
#include "timeman.h"
|
||||||
#include "uci.h"
|
#include "uci.h"
|
||||||
#include "incbin/incbin.h"
|
#include "incbin/incbin.h"
|
||||||
|
|
||||||
@@ -54,12 +55,13 @@
|
|||||||
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace Eval::NNUE;
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
namespace Eval {
|
namespace Eval {
|
||||||
|
|
||||||
bool useNNUE;
|
bool useNNUE;
|
||||||
string eval_file_loaded = "None";
|
string currentEvalFileName = "None";
|
||||||
|
|
||||||
/// NNUE::init() tries to load a NNUE network at startup time, or when the engine
|
/// NNUE::init() tries to load a NNUE network at startup time, or when the engine
|
||||||
/// receives a UCI command "setoption name EvalFile value nn-[a-z0-9]{12}.nnue"
|
/// receives a UCI command "setoption name EvalFile value nn-[a-z0-9]{12}.nnue"
|
||||||
@@ -76,6 +78,8 @@ namespace Eval {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
string eval_file = string(Options["EvalFile"]);
|
string eval_file = string(Options["EvalFile"]);
|
||||||
|
if (eval_file.empty())
|
||||||
|
eval_file = EvalFileDefaultName;
|
||||||
|
|
||||||
#if defined(DEFAULT_NNUE_DIRECTORY)
|
#if defined(DEFAULT_NNUE_DIRECTORY)
|
||||||
#define stringify2(x) #x
|
#define stringify2(x) #x
|
||||||
@@ -86,13 +90,13 @@ namespace Eval {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (string directory : dirs)
|
for (string directory : dirs)
|
||||||
if (eval_file_loaded != eval_file)
|
if (currentEvalFileName != eval_file)
|
||||||
{
|
{
|
||||||
if (directory != "<internal>")
|
if (directory != "<internal>")
|
||||||
{
|
{
|
||||||
ifstream stream(directory + eval_file, ios::binary);
|
ifstream stream(directory + eval_file, ios::binary);
|
||||||
if (load_eval(eval_file, stream))
|
if (load_eval(eval_file, stream))
|
||||||
eval_file_loaded = eval_file;
|
currentEvalFileName = eval_file;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (directory == "<internal>" && eval_file == EvalFileDefaultName)
|
if (directory == "<internal>" && eval_file == EvalFileDefaultName)
|
||||||
@@ -104,10 +108,11 @@ namespace Eval {
|
|||||||
|
|
||||||
MemoryBuffer buffer(const_cast<char*>(reinterpret_cast<const char*>(gEmbeddedNNUEData)),
|
MemoryBuffer buffer(const_cast<char*>(reinterpret_cast<const char*>(gEmbeddedNNUEData)),
|
||||||
size_t(gEmbeddedNNUESize));
|
size_t(gEmbeddedNNUESize));
|
||||||
|
(void) gEmbeddedNNUEEnd; // Silence warning on unused variable
|
||||||
|
|
||||||
istream stream(&buffer);
|
istream stream(&buffer);
|
||||||
if (load_eval(eval_file, stream))
|
if (load_eval(eval_file, stream))
|
||||||
eval_file_loaded = eval_file;
|
currentEvalFileName = eval_file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,16 +121,16 @@ namespace Eval {
|
|||||||
void NNUE::verify() {
|
void NNUE::verify() {
|
||||||
|
|
||||||
string eval_file = string(Options["EvalFile"]);
|
string eval_file = string(Options["EvalFile"]);
|
||||||
|
if (eval_file.empty())
|
||||||
|
eval_file = EvalFileDefaultName;
|
||||||
|
|
||||||
if (useNNUE && eval_file_loaded != eval_file)
|
if (useNNUE && currentEvalFileName != eval_file)
|
||||||
{
|
{
|
||||||
UCI::OptionsMap defaults;
|
|
||||||
UCI::init(defaults);
|
|
||||||
|
|
||||||
string msg1 = "If the UCI option \"Use NNUE\" is set to true, network evaluation parameters compatible with the engine must be available.";
|
string msg1 = "If the UCI option \"Use NNUE\" is set to true, network evaluation parameters compatible with the engine must be available.";
|
||||||
string msg2 = "The option is set to true, but the network file " + eval_file + " was not loaded successfully.";
|
string msg2 = "The option is set to true, but the network file " + eval_file + " was not loaded successfully.";
|
||||||
string msg3 = "The UCI option EvalFile might need to specify the full path, including the directory name, to the network file.";
|
string msg3 = "The UCI option EvalFile might need to specify the full path, including the directory name, to the network file.";
|
||||||
string msg4 = "The default net can be downloaded from: https://tests.stockfishchess.org/api/nn/" + string(defaults["EvalFile"]);
|
string msg4 = "The default net can be downloaded from: https://tests.stockfishchess.org/api/nn/" + std::string(EvalFileDefaultName);
|
||||||
string msg5 = "The engine will be terminated now.";
|
string msg5 = "The engine will be terminated now.";
|
||||||
|
|
||||||
sync_cout << "info string ERROR: " << msg1 << sync_endl;
|
sync_cout << "info string ERROR: " << msg1 << sync_endl;
|
||||||
@@ -154,7 +159,7 @@ namespace Trace {
|
|||||||
|
|
||||||
Score scores[TERM_NB][COLOR_NB];
|
Score scores[TERM_NB][COLOR_NB];
|
||||||
|
|
||||||
double to_cp(Value v) { return double(v) / PawnValueEg; }
|
double to_cp(Value v) { return double(v) / UCI::NormalizeToPawnValue; }
|
||||||
|
|
||||||
void add(int idx, Color c, Score s) {
|
void add(int idx, Color c, Score s) {
|
||||||
scores[idx][c] = s;
|
scores[idx][c] = s;
|
||||||
@@ -178,7 +183,7 @@ namespace Trace {
|
|||||||
else
|
else
|
||||||
os << scores[t][WHITE] << " | " << scores[t][BLACK];
|
os << scores[t][WHITE] << " | " << scores[t][BLACK];
|
||||||
|
|
||||||
os << " | " << scores[t][WHITE] - scores[t][BLACK] << "\n";
|
os << " | " << scores[t][WHITE] - scores[t][BLACK] << " |\n";
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,19 +193,17 @@ using namespace Trace;
|
|||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// Threshold for lazy and space evaluation
|
// Threshold for lazy and space evaluation
|
||||||
constexpr Value LazyThreshold1 = Value(1565);
|
constexpr Value LazyThreshold1 = Value(3631);
|
||||||
constexpr Value LazyThreshold2 = Value(1102);
|
constexpr Value LazyThreshold2 = Value(2084);
|
||||||
constexpr Value SpaceThreshold = Value(11551);
|
constexpr Value SpaceThreshold = Value(11551);
|
||||||
constexpr Value NNUEThreshold1 = Value(682);
|
|
||||||
constexpr Value NNUEThreshold2 = Value(176);
|
|
||||||
|
|
||||||
// KingAttackWeights[PieceType] contains king attack weights by piece type
|
// KingAttackWeights[PieceType] contains king attack weights by piece type
|
||||||
constexpr int KingAttackWeights[PIECE_TYPE_NB] = { 0, 0, 81, 52, 44, 10 };
|
constexpr int KingAttackWeights[PIECE_TYPE_NB] = { 0, 0, 76, 46, 45, 14 };
|
||||||
|
|
||||||
// SafeCheck[PieceType][single/multiple] contains safe check bonus by piece type,
|
// SafeCheck[PieceType][single/multiple] contains safe check bonus by piece type,
|
||||||
// higher if multiple safe checks are possible for that piece type.
|
// higher if multiple safe checks are possible for that piece type.
|
||||||
constexpr int SafeCheck[][2] = {
|
constexpr int SafeCheck[][2] = {
|
||||||
{}, {}, {803, 1292}, {639, 974}, {1087, 1878}, {759, 1132}
|
{}, {}, {805, 1292}, {650, 984}, {1071, 1886}, {730, 1128}
|
||||||
};
|
};
|
||||||
|
|
||||||
#define S(mg, eg) make_score(mg, eg)
|
#define S(mg, eg) make_score(mg, eg)
|
||||||
@@ -226,57 +229,58 @@ namespace {
|
|||||||
// BishopPawns[distance from edge] contains a file-dependent penalty for pawns on
|
// BishopPawns[distance from edge] contains a file-dependent penalty for pawns on
|
||||||
// squares of the same color as our bishop.
|
// squares of the same color as our bishop.
|
||||||
constexpr Score BishopPawns[int(FILE_NB) / 2] = {
|
constexpr Score BishopPawns[int(FILE_NB) / 2] = {
|
||||||
S(3, 8), S(3, 9), S(2, 8), S(3, 8)
|
S(3, 8), S(3, 9), S(2, 7), S(3, 7)
|
||||||
};
|
};
|
||||||
|
|
||||||
// KingProtector[knight/bishop] contains penalty for each distance unit to own king
|
// KingProtector[knight/bishop] contains penalty for each distance unit to own king
|
||||||
constexpr Score KingProtector[] = { S(8, 9), S(6, 9) };
|
constexpr Score KingProtector[] = { S(9, 9), S(7, 9) };
|
||||||
|
|
||||||
// Outpost[knight/bishop] contains bonuses for each knight or bishop occupying a
|
// Outpost[knight/bishop] contains bonuses for each knight or bishop occupying a
|
||||||
// pawn protected square on rank 4 to 6 which is also safe from a pawn attack.
|
// pawn protected square on rank 4 to 6 which is also safe from a pawn attack.
|
||||||
constexpr Score Outpost[] = { S(57, 38), S(31, 24) };
|
constexpr Score Outpost[] = { S(54, 34), S(31, 25) };
|
||||||
|
|
||||||
// PassedRank[Rank] contains a bonus according to the rank of a passed pawn
|
// PassedRank[Rank] contains a bonus according to the rank of a passed pawn
|
||||||
constexpr Score PassedRank[RANK_NB] = {
|
constexpr Score PassedRank[RANK_NB] = {
|
||||||
S(0, 0), S(7, 27), S(16, 32), S(17, 40), S(64, 71), S(170, 174), S(278, 262)
|
S(0, 0), S(2, 38), S(15, 36), S(22, 50), S(64, 81), S(166, 184), S(284, 269)
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr Score RookOnClosedFile = S(10, 5);
|
constexpr Score RookOnClosedFile = S(10, 5);
|
||||||
constexpr Score RookOnOpenFile[] = { S(19, 6), S(47, 26) };
|
constexpr Score RookOnOpenFile[] = { S(18, 8), S(49, 26) };
|
||||||
|
|
||||||
// ThreatByMinor/ByRook[attacked PieceType] contains bonuses according to
|
// ThreatByMinor/ByRook[attacked PieceType] contains bonuses according to
|
||||||
// which piece type attacks which one. Attacks on lesser pieces which are
|
// which piece type attacks which one. Attacks on lesser pieces which are
|
||||||
// pawn-defended are not considered.
|
// pawn-defended are not considered.
|
||||||
constexpr Score ThreatByMinor[PIECE_TYPE_NB] = {
|
constexpr Score ThreatByMinor[PIECE_TYPE_NB] = {
|
||||||
S(0, 0), S(5, 32), S(55, 41), S(77, 56), S(89, 119), S(79, 162)
|
S(0, 0), S(6, 37), S(64, 50), S(82, 57), S(103, 130), S(81, 163)
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr Score ThreatByRook[PIECE_TYPE_NB] = {
|
constexpr Score ThreatByRook[PIECE_TYPE_NB] = {
|
||||||
S(0, 0), S(3, 44), S(37, 68), S(42, 60), S(0, 39), S(58, 43)
|
S(0, 0), S(3, 44), S(36, 71), S(44, 59), S(0, 39), S(60, 39)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
constexpr Value CorneredBishop = Value(50);
|
||||||
|
|
||||||
// Assorted bonuses and penalties
|
// Assorted bonuses and penalties
|
||||||
constexpr Score UncontestedOutpost = S( 1, 10);
|
constexpr Score UncontestedOutpost = S( 0, 10);
|
||||||
constexpr Score BishopOnKingRing = S( 24, 0);
|
constexpr Score BishopOnKingRing = S( 24, 0);
|
||||||
constexpr Score BishopXRayPawns = S( 4, 5);
|
constexpr Score BishopXRayPawns = S( 4, 5);
|
||||||
constexpr Score CorneredBishop = S( 50, 50);
|
|
||||||
constexpr Score FlankAttacks = S( 8, 0);
|
constexpr Score FlankAttacks = S( 8, 0);
|
||||||
constexpr Score Hanging = S( 69, 36);
|
constexpr Score Hanging = S( 72, 40);
|
||||||
constexpr Score KnightOnQueen = S( 16, 11);
|
constexpr Score KnightOnQueen = S( 16, 11);
|
||||||
constexpr Score LongDiagonalBishop = S( 45, 0);
|
constexpr Score LongDiagonalBishop = S( 45, 0);
|
||||||
constexpr Score MinorBehindPawn = S( 18, 3);
|
constexpr Score MinorBehindPawn = S( 18, 3);
|
||||||
constexpr Score PassedFile = S( 11, 8);
|
constexpr Score PassedFile = S( 13, 8);
|
||||||
constexpr Score PawnlessFlank = S( 17, 95);
|
constexpr Score PawnlessFlank = S( 19, 97);
|
||||||
constexpr Score ReachableOutpost = S( 31, 22);
|
constexpr Score ReachableOutpost = S( 33, 19);
|
||||||
constexpr Score RestrictedPiece = S( 7, 7);
|
constexpr Score RestrictedPiece = S( 6, 7);
|
||||||
constexpr Score RookOnKingRing = S( 16, 0);
|
constexpr Score RookOnKingRing = S( 16, 0);
|
||||||
constexpr Score SliderOnQueen = S( 60, 18);
|
constexpr Score SliderOnQueen = S( 62, 21);
|
||||||
constexpr Score ThreatByKing = S( 24, 89);
|
constexpr Score ThreatByKing = S( 24, 87);
|
||||||
constexpr Score ThreatByPawnPush = S( 48, 39);
|
constexpr Score ThreatByPawnPush = S( 48, 39);
|
||||||
constexpr Score ThreatBySafePawn = S(173, 94);
|
constexpr Score ThreatBySafePawn = S(167, 99);
|
||||||
constexpr Score TrappedRook = S( 55, 13);
|
constexpr Score TrappedRook = S( 55, 13);
|
||||||
constexpr Score WeakQueenProtection = S( 14, 0);
|
constexpr Score WeakQueenProtection = S( 14, 0);
|
||||||
constexpr Score WeakQueen = S( 56, 15);
|
constexpr Score WeakQueen = S( 57, 19);
|
||||||
|
|
||||||
|
|
||||||
#undef S
|
#undef S
|
||||||
@@ -394,8 +398,9 @@ namespace {
|
|||||||
|
|
||||||
attackedBy[Us][Pt] = 0;
|
attackedBy[Us][Pt] = 0;
|
||||||
|
|
||||||
while (b1) {
|
while (b1)
|
||||||
Square s = pop_lsb(&b1);
|
{
|
||||||
|
Square s = pop_lsb(b1);
|
||||||
|
|
||||||
// Find attacked squares, including x-ray attacks for bishops and rooks
|
// Find attacked squares, including x-ray attacks for bishops and rooks
|
||||||
b = Pt == BISHOP ? attacks_bb<BISHOP>(s, pos.pieces() ^ pos.pieces(QUEEN))
|
b = Pt == BISHOP ? attacks_bb<BISHOP>(s, pos.pieces() ^ pos.pieces(QUEEN))
|
||||||
@@ -475,9 +480,8 @@ namespace {
|
|||||||
{
|
{
|
||||||
Direction d = pawn_push(Us) + (file_of(s) == FILE_A ? EAST : WEST);
|
Direction d = pawn_push(Us) + (file_of(s) == FILE_A ? EAST : WEST);
|
||||||
if (pos.piece_on(s + d) == make_piece(Us, PAWN))
|
if (pos.piece_on(s + d) == make_piece(Us, PAWN))
|
||||||
score -= !pos.empty(s + d + pawn_push(Us)) ? CorneredBishop * 4
|
score -= !pos.empty(s + d + pawn_push(Us)) ? 4 * make_score(CorneredBishop, CorneredBishop)
|
||||||
: pos.piece_on(s + d + d) == make_piece(Us, PAWN) ? CorneredBishop * 2
|
: 3 * make_score(CorneredBishop, CorneredBishop);
|
||||||
: CorneredBishop;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -656,11 +660,11 @@ namespace {
|
|||||||
{
|
{
|
||||||
b = (defended | weak) & (attackedBy[Us][KNIGHT] | attackedBy[Us][BISHOP]);
|
b = (defended | weak) & (attackedBy[Us][KNIGHT] | attackedBy[Us][BISHOP]);
|
||||||
while (b)
|
while (b)
|
||||||
score += ThreatByMinor[type_of(pos.piece_on(pop_lsb(&b)))];
|
score += ThreatByMinor[type_of(pos.piece_on(pop_lsb(b)))];
|
||||||
|
|
||||||
b = weak & attackedBy[Us][ROOK];
|
b = weak & attackedBy[Us][ROOK];
|
||||||
while (b)
|
while (b)
|
||||||
score += ThreatByRook[type_of(pos.piece_on(pop_lsb(&b)))];
|
score += ThreatByRook[type_of(pos.piece_on(pop_lsb(b)))];
|
||||||
|
|
||||||
if (weak & attackedBy[Us][KING])
|
if (weak & attackedBy[Us][KING])
|
||||||
score += ThreatByKing;
|
score += ThreatByKing;
|
||||||
@@ -758,7 +762,7 @@ namespace {
|
|||||||
|
|
||||||
while (b)
|
while (b)
|
||||||
{
|
{
|
||||||
Square s = pop_lsb(&b);
|
Square s = pop_lsb(b);
|
||||||
|
|
||||||
assert(!(pos.pieces(Them, PAWN) & forward_file_bb(Us, s + Up)));
|
assert(!(pos.pieces(Them, PAWN) & forward_file_bb(Us, s + Up)));
|
||||||
|
|
||||||
@@ -904,7 +908,7 @@ namespace {
|
|||||||
Color strongSide = eg > VALUE_DRAW ? WHITE : BLACK;
|
Color strongSide = eg > VALUE_DRAW ? WHITE : BLACK;
|
||||||
int sf = me->scale_factor(pos, strongSide);
|
int sf = me->scale_factor(pos, strongSide);
|
||||||
|
|
||||||
// If scale factor is not already specific, scale down via general heuristics
|
// If scale factor is not already specific, scale up/down via general heuristics
|
||||||
if (sf == SCALE_FACTOR_NORMAL)
|
if (sf == SCALE_FACTOR_NORMAL)
|
||||||
{
|
{
|
||||||
if (pos.opposite_bishops())
|
if (pos.opposite_bishops())
|
||||||
@@ -977,7 +981,7 @@ namespace {
|
|||||||
// Initialize score by reading the incrementally updated scores included in
|
// Initialize score by reading the incrementally updated scores included in
|
||||||
// the position object (material + piece square tables) and the material
|
// the position object (material + piece square tables) and the material
|
||||||
// imbalance. Score is computed internally from the white point of view.
|
// imbalance. Score is computed internally from the white point of view.
|
||||||
Score score = pos.psq_score() + me->imbalance() + pos.this_thread()->contempt;
|
Score score = pos.psq_score() + me->imbalance();
|
||||||
|
|
||||||
// Probe the pawn hash table
|
// Probe the pawn hash table
|
||||||
pe = Pawns::probe(pos);
|
pe = Pawns::probe(pos);
|
||||||
@@ -985,7 +989,9 @@ namespace {
|
|||||||
|
|
||||||
// Early exit if score is high
|
// Early exit if score is high
|
||||||
auto lazy_skip = [&](Value lazyThreshold) {
|
auto lazy_skip = [&](Value lazyThreshold) {
|
||||||
return abs(mg_value(score) + eg_value(score)) / 2 > lazyThreshold + pos.non_pawn_material() / 64;
|
return abs(mg_value(score) + eg_value(score)) > lazyThreshold
|
||||||
|
+ std::abs(pos.this_thread()->bestValue) * 5 / 4
|
||||||
|
+ pos.non_pawn_material() / 32;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (lazy_skip(LazyThreshold1))
|
if (lazy_skip(LazyThreshold1))
|
||||||
@@ -1031,60 +1037,63 @@ make_v:
|
|||||||
v = (v / 16) * 16;
|
v = (v / 16) * 16;
|
||||||
|
|
||||||
// Side to move point of view
|
// Side to move point of view
|
||||||
v = (pos.side_to_move() == WHITE ? v : -v) + Tempo;
|
v = (pos.side_to_move() == WHITE ? v : -v);
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace Eval
|
||||||
|
|
||||||
|
|
||||||
/// evaluate() is the evaluator for the outer world. It returns a static
|
/// evaluate() is the evaluator for the outer world. It returns a static
|
||||||
/// evaluation of the position from the point of view of the side to move.
|
/// evaluation of the position from the point of view of the side to move.
|
||||||
|
|
||||||
Value Eval::evaluate(const Position& pos) {
|
Value Eval::evaluate(const Position& pos, int* complexity) {
|
||||||
|
|
||||||
Value v;
|
Value v;
|
||||||
|
Value psq = pos.psq_eg_stm();
|
||||||
|
|
||||||
if (!Eval::useNNUE)
|
// We use the much less accurate but faster Classical eval when the NNUE
|
||||||
|
// option is set to false. Otherwise we use the NNUE eval unless the
|
||||||
|
// PSQ advantage is decisive and several pieces remain. (~3 Elo)
|
||||||
|
bool useClassical = !useNNUE || (pos.count<ALL_PIECES>() > 7 && abs(psq) > 1760);
|
||||||
|
|
||||||
|
if (useClassical)
|
||||||
v = Evaluation<NO_TRACE>(pos).value();
|
v = Evaluation<NO_TRACE>(pos).value();
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Scale and shift NNUE for compatibility with search and classical evaluation
|
int nnueComplexity;
|
||||||
auto adjusted_NNUE = [&](){
|
int scale = 1064 + 106 * pos.non_pawn_material() / 5120;
|
||||||
int mat = pos.non_pawn_material() + 2 * PawnValueMg * pos.count<PAWN>();
|
|
||||||
return NNUE::evaluate(pos) * (641 + mat / 32 - 4 * pos.rule50_count()) / 1024 + Tempo;
|
|
||||||
};
|
|
||||||
|
|
||||||
// If there is PSQ imbalance use classical eval, with small probability if it is small
|
Color stm = pos.side_to_move();
|
||||||
Value psq = Value(abs(eg_value(pos.psq_score())));
|
Value optimism = pos.this_thread()->optimism[stm];
|
||||||
int r50 = 16 + pos.rule50_count();
|
|
||||||
bool largePsq = psq * 16 > (NNUEThreshold1 + pos.non_pawn_material() / 64) * r50;
|
|
||||||
bool classical = largePsq || (psq > PawnValueMg / 4 && !(pos.this_thread()->nodes & 0xB));
|
|
||||||
|
|
||||||
// Use classical evaluation for really low piece endgames.
|
Value nnue = NNUE::evaluate(pos, true, &nnueComplexity);
|
||||||
// The most critical case is a bishop + A/H file pawn vs naked king draw.
|
|
||||||
bool strongClassical = pos.non_pawn_material() < 2 * RookValueMg && pos.count<PAWN>() < 2;
|
|
||||||
|
|
||||||
v = classical || strongClassical ? Evaluation<NO_TRACE>(pos).value() : adjusted_NNUE();
|
// Blend nnue complexity with (semi)classical complexity
|
||||||
|
nnueComplexity = ( 416 * nnueComplexity
|
||||||
|
+ 424 * abs(psq - nnue)
|
||||||
|
+ (optimism > 0 ? int(optimism) * int(psq - nnue) : 0)
|
||||||
|
) / 1024;
|
||||||
|
|
||||||
// If the classical eval is small and imbalance large, use NNUE nevertheless.
|
// Return hybrid NNUE complexity to caller
|
||||||
// For the case of opposite colored bishops, switch to NNUE eval with
|
if (complexity)
|
||||||
// small probability if the classical eval is less than the threshold.
|
*complexity = nnueComplexity;
|
||||||
if ( largePsq && !strongClassical
|
|
||||||
&& ( abs(v) * 16 < NNUEThreshold2 * r50
|
optimism = optimism * (269 + nnueComplexity) / 256;
|
||||||
|| ( pos.opposite_bishops()
|
v = (nnue * scale + optimism * (scale - 754)) / 1024;
|
||||||
&& abs(v) * 16 < (NNUEThreshold1 + pos.non_pawn_material() / 64) * r50
|
|
||||||
&& !(pos.this_thread()->nodes & 0xB))))
|
|
||||||
v = adjusted_NNUE();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Damp down the evaluation linearly when shuffling
|
// Damp down the evaluation linearly when shuffling
|
||||||
v = v * (100 - pos.rule50_count()) / 100;
|
v = v * (195 - pos.rule50_count()) / 211;
|
||||||
|
|
||||||
// Guarantee evaluation does not hit the tablebase range
|
// Guarantee evaluation does not hit the tablebase range
|
||||||
v = std::clamp(v, VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);
|
v = std::clamp(v, VALUE_TB_LOSS_IN_MAX_PLY + 1, VALUE_TB_WIN_IN_MAX_PLY - 1);
|
||||||
|
|
||||||
|
// When not using NNUE, return classical complexity to caller
|
||||||
|
if (complexity && (!useNNUE || useClassical))
|
||||||
|
*complexity = abs(v - psq);
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1093,7 +1102,7 @@ Value Eval::evaluate(const Position& pos) {
|
|||||||
/// descriptions and values of each evaluation term. Useful for debugging.
|
/// descriptions and values of each evaluation term. Useful for debugging.
|
||||||
/// Trace scores are from white's point of view
|
/// Trace scores are from white's point of view
|
||||||
|
|
||||||
std::string Eval::trace(const Position& pos) {
|
std::string Eval::trace(Position& pos) {
|
||||||
|
|
||||||
if (pos.checkers())
|
if (pos.checkers())
|
||||||
return "Final evaluation: none (in check)";
|
return "Final evaluation: none (in check)";
|
||||||
@@ -1105,44 +1114,58 @@ std::string Eval::trace(const Position& pos) {
|
|||||||
|
|
||||||
std::memset(scores, 0, sizeof(scores));
|
std::memset(scores, 0, sizeof(scores));
|
||||||
|
|
||||||
pos.this_thread()->contempt = SCORE_ZERO; // Reset any dynamic contempt
|
// Reset any global variable used in eval
|
||||||
|
pos.this_thread()->bestValue = VALUE_ZERO;
|
||||||
|
pos.this_thread()->optimism[WHITE] = VALUE_ZERO;
|
||||||
|
pos.this_thread()->optimism[BLACK] = VALUE_ZERO;
|
||||||
|
|
||||||
v = Evaluation<TRACE>(pos).value();
|
v = Evaluation<TRACE>(pos).value();
|
||||||
|
|
||||||
ss << std::showpoint << std::noshowpos << std::fixed << std::setprecision(2)
|
ss << std::showpoint << std::noshowpos << std::fixed << std::setprecision(2)
|
||||||
<< " Term | White | Black | Total \n"
|
<< " Contributing terms for the classical eval:\n"
|
||||||
<< " | MG EG | MG EG | MG EG \n"
|
<< "+------------+-------------+-------------+-------------+\n"
|
||||||
<< " ------------+-------------+-------------+------------\n"
|
<< "| Term | White | Black | Total |\n"
|
||||||
<< " Material | " << Term(MATERIAL)
|
<< "| | MG EG | MG EG | MG EG |\n"
|
||||||
<< " Imbalance | " << Term(IMBALANCE)
|
<< "+------------+-------------+-------------+-------------+\n"
|
||||||
<< " Pawns | " << Term(PAWN)
|
<< "| Material | " << Term(MATERIAL)
|
||||||
<< " Knights | " << Term(KNIGHT)
|
<< "| Imbalance | " << Term(IMBALANCE)
|
||||||
<< " Bishops | " << Term(BISHOP)
|
<< "| Pawns | " << Term(PAWN)
|
||||||
<< " Rooks | " << Term(ROOK)
|
<< "| Knights | " << Term(KNIGHT)
|
||||||
<< " Queens | " << Term(QUEEN)
|
<< "| Bishops | " << Term(BISHOP)
|
||||||
<< " Mobility | " << Term(MOBILITY)
|
<< "| Rooks | " << Term(ROOK)
|
||||||
<< " King safety | " << Term(KING)
|
<< "| Queens | " << Term(QUEEN)
|
||||||
<< " Threats | " << Term(THREAT)
|
<< "| Mobility | " << Term(MOBILITY)
|
||||||
<< " Passed | " << Term(PASSED)
|
<< "|King safety | " << Term(KING)
|
||||||
<< " Space | " << Term(SPACE)
|
<< "| Threats | " << Term(THREAT)
|
||||||
<< " Winnable | " << Term(WINNABLE)
|
<< "| Passed | " << Term(PASSED)
|
||||||
<< " ------------+-------------+-------------+------------\n"
|
<< "| Space | " << Term(SPACE)
|
||||||
<< " Total | " << Term(TOTAL);
|
<< "| Winnable | " << Term(WINNABLE)
|
||||||
|
<< "+------------+-------------+-------------+-------------+\n"
|
||||||
v = pos.side_to_move() == WHITE ? v : -v;
|
<< "| Total | " << Term(TOTAL)
|
||||||
|
<< "+------------+-------------+-------------+-------------+\n";
|
||||||
ss << "\nClassical evaluation: " << to_cp(v) << " (white side)\n";
|
|
||||||
|
|
||||||
if (Eval::useNNUE)
|
if (Eval::useNNUE)
|
||||||
{
|
ss << '\n' << NNUE::trace(pos) << '\n';
|
||||||
v = NNUE::evaluate(pos);
|
|
||||||
|
ss << std::showpoint << std::showpos << std::fixed << std::setprecision(2) << std::setw(15);
|
||||||
|
|
||||||
v = pos.side_to_move() == WHITE ? v : -v;
|
v = pos.side_to_move() == WHITE ? v : -v;
|
||||||
ss << "\nNNUE evaluation: " << to_cp(v) << " (white side)\n";
|
ss << "\nClassical evaluation " << to_cp(v) << " (white side)\n";
|
||||||
|
if (Eval::useNNUE)
|
||||||
|
{
|
||||||
|
v = NNUE::evaluate(pos, false);
|
||||||
|
v = pos.side_to_move() == WHITE ? v : -v;
|
||||||
|
ss << "NNUE evaluation " << to_cp(v) << " (white side)\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
v = evaluate(pos);
|
v = evaluate(pos);
|
||||||
v = pos.side_to_move() == WHITE ? v : -v;
|
v = pos.side_to_move() == WHITE ? v : -v;
|
||||||
ss << "\nFinal evaluation: " << to_cp(v) << " (white side)\n";
|
ss << "Final evaluation " << to_cp(v) << " (white side)";
|
||||||
|
if (Eval::useNNUE)
|
||||||
|
ss << " [with scaled NNUE, hybrid, ...]";
|
||||||
|
ss << "\n";
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+17
-7
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -20,33 +20,43 @@
|
|||||||
#define EVALUATE_H_INCLUDED
|
#define EVALUATE_H_INCLUDED
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
class Position;
|
class Position;
|
||||||
|
|
||||||
namespace Eval {
|
namespace Eval {
|
||||||
|
|
||||||
std::string trace(const Position& pos);
|
std::string trace(Position& pos);
|
||||||
Value evaluate(const Position& pos);
|
Value evaluate(const Position& pos, int* complexity = nullptr);
|
||||||
|
|
||||||
extern bool useNNUE;
|
extern bool useNNUE;
|
||||||
extern std::string eval_file_loaded;
|
extern std::string currentEvalFileName;
|
||||||
|
|
||||||
// The default net name MUST follow the format nn-[SHA256 first 12 digits].nnue
|
// The default net name MUST follow the format nn-[SHA256 first 12 digits].nnue
|
||||||
// for the build process (profile-build and fishtest) to work. Do not change the
|
// for the build process (profile-build and fishtest) to work. Do not change the
|
||||||
// name of the macro, as it is used in the Makefile.
|
// name of the macro, as it is used in the Makefile.
|
||||||
#define EvalFileDefaultName "nn-62ef826d1a6d.nnue"
|
#define EvalFileDefaultName "nn-ad9b42354671.nnue"
|
||||||
|
|
||||||
namespace NNUE {
|
namespace NNUE {
|
||||||
|
|
||||||
Value evaluate(const Position& pos);
|
std::string trace(Position& pos);
|
||||||
bool load_eval(std::string name, std::istream& stream);
|
Value evaluate(const Position& pos, bool adjusted = false, int* complexity = nullptr);
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
void verify();
|
void verify();
|
||||||
|
|
||||||
|
bool load_eval(std::string name, std::istream& stream);
|
||||||
|
bool save_eval(std::ostream& stream);
|
||||||
|
bool save_eval(const std::optional<std::string>& filename);
|
||||||
|
|
||||||
} // namespace NNUE
|
} // namespace NNUE
|
||||||
|
|
||||||
} // namespace Eval
|
} // namespace Eval
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef EVALUATE_H_INCLUDED
|
#endif // #ifndef EVALUATE_H_INCLUDED
|
||||||
|
|||||||
+3
-1
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -28,6 +28,8 @@
|
|||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
#include "uci.h"
|
#include "uci.h"
|
||||||
|
|
||||||
|
using namespace Stockfish;
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
std::cout << engine_info() << std::endl;
|
std::cout << engine_info() << std::endl;
|
||||||
|
|||||||
+5
-1
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -24,6 +24,8 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
#define S(mg, eg) make_score(mg, eg)
|
#define S(mg, eg) make_score(mg, eg)
|
||||||
|
|
||||||
@@ -223,3 +225,5 @@ Entry* probe(const Position& pos) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Material
|
} // namespace Material
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
namespace Material {
|
namespace Stockfish::Material {
|
||||||
|
|
||||||
/// Material::Entry contains various information about a material configuration.
|
/// Material::Entry contains various information about a material configuration.
|
||||||
/// It contains a material imbalance evaluation, a function pointer to a special
|
/// It contains a material imbalance evaluation, a function pointer to a special
|
||||||
@@ -66,6 +66,6 @@ typedef HashTable<Entry, 8192> Table;
|
|||||||
|
|
||||||
Entry* probe(const Position& pos);
|
Entry* probe(const Position& pos);
|
||||||
|
|
||||||
} // namespace Material
|
} // namespace Stockfish::Material
|
||||||
|
|
||||||
#endif // #ifndef MATERIAL_H_INCLUDED
|
#endif // #ifndef MATERIAL_H_INCLUDED
|
||||||
|
|||||||
+98
-39
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -36,6 +36,8 @@ typedef bool(*fun1_t)(LOGICAL_PROCESSOR_RELATIONSHIP,
|
|||||||
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD);
|
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD);
|
||||||
typedef bool(*fun2_t)(USHORT, PGROUP_AFFINITY);
|
typedef bool(*fun2_t)(USHORT, PGROUP_AFFINITY);
|
||||||
typedef bool(*fun3_t)(HANDLE, CONST GROUP_AFFINITY*, PGROUP_AFFINITY);
|
typedef bool(*fun3_t)(HANDLE, CONST GROUP_AFFINITY*, PGROUP_AFFINITY);
|
||||||
|
typedef bool(*fun4_t)(USHORT, PGROUP_AFFINITY, USHORT, PUSHORT);
|
||||||
|
typedef WORD(*fun5_t)();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -51,7 +53,7 @@ typedef bool(*fun3_t)(HANDLE, CONST GROUP_AFFINITY*, PGROUP_AFFINITY);
|
|||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__APPLE__) || defined(__ANDROID__) || defined(__OpenBSD__) || (defined(__GLIBCXX__) && !defined(_GLIBCXX_HAVE_ALIGNED_ALLOC) && !defined(_WIN32))
|
#if defined(__APPLE__) || defined(__ANDROID__) || defined(__OpenBSD__) || (defined(__GLIBCXX__) && !defined(_GLIBCXX_HAVE_ALIGNED_ALLOC) && !defined(_WIN32)) || defined(__e2k__)
|
||||||
#define POSIXALIGNEDALLOC
|
#define POSIXALIGNEDALLOC
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -61,11 +63,12 @@ typedef bool(*fun3_t)(HANDLE, CONST GROUP_AFFINITY*, PGROUP_AFFINITY);
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
/// Version number. If Version is left empty, then compile date in the format
|
/// Version number or dev.
|
||||||
/// DD-MM-YY and show in engine_info.
|
const string version = "15.1";
|
||||||
const string Version = "13";
|
|
||||||
|
|
||||||
/// Our fancy logging facility. The trick here is to replace cin.rdbuf() and
|
/// 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
|
/// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We
|
||||||
@@ -108,7 +111,14 @@ public:
|
|||||||
|
|
||||||
static Logger l;
|
static Logger l;
|
||||||
|
|
||||||
if (!fname.empty() && !l.file.is_open())
|
if (l.file.is_open())
|
||||||
|
{
|
||||||
|
cout.rdbuf(l.out.buf);
|
||||||
|
cin.rdbuf(l.in.buf);
|
||||||
|
l.file.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fname.empty())
|
||||||
{
|
{
|
||||||
l.file.open(fname, ifstream::out);
|
l.file.open(fname, ifstream::out);
|
||||||
|
|
||||||
@@ -121,35 +131,47 @@ public:
|
|||||||
cin.rdbuf(&l.in);
|
cin.rdbuf(&l.in);
|
||||||
cout.rdbuf(&l.out);
|
cout.rdbuf(&l.out);
|
||||||
}
|
}
|
||||||
else if (fname.empty() && l.file.is_open())
|
|
||||||
{
|
|
||||||
cout.rdbuf(l.out.buf);
|
|
||||||
cin.rdbuf(l.in.buf);
|
|
||||||
l.file.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
/// engine_info() returns the full name of the current Stockfish version. This
|
/// engine_info() returns the full name of the current Stockfish version.
|
||||||
/// will be either "Stockfish <Tag> DD-MM-YY" (where DD-MM-YY is the date when
|
/// For local dev compiles we try to append the commit sha and commit date
|
||||||
/// the program was compiled) or "Stockfish <Version>", depending on whether
|
/// from git if that fails only the local compilation date is set and "nogit" is specified:
|
||||||
/// Version is empty.
|
/// Stockfish dev-YYYYMMDD-SHA
|
||||||
|
/// or
|
||||||
|
/// Stockfish dev-YYYYMMDD-nogit
|
||||||
|
///
|
||||||
|
/// For releases (non dev builds) we only include the version number:
|
||||||
|
/// Stockfish version
|
||||||
|
|
||||||
const string engine_info(bool to_uci) {
|
string engine_info(bool to_uci) {
|
||||||
|
stringstream ss;
|
||||||
|
ss << "Stockfish " << version << setfill('0');
|
||||||
|
|
||||||
|
if (version == "dev")
|
||||||
|
{
|
||||||
|
ss << "-";
|
||||||
|
#ifdef GIT_DATE
|
||||||
|
ss << GIT_DATE;
|
||||||
|
#else
|
||||||
const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
|
const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
|
||||||
string month, day, year;
|
string month, day, year;
|
||||||
stringstream ss, date(__DATE__); // From compiler, format is "Sep 21 2008"
|
stringstream date(__DATE__); // From compiler, format is "Sep 21 2008"
|
||||||
|
|
||||||
ss << "Stockfish " << Version << setfill('0');
|
|
||||||
|
|
||||||
if (Version.empty())
|
|
||||||
{
|
|
||||||
date >> month >> day >> year;
|
date >> month >> day >> year;
|
||||||
ss << setw(2) << day << setw(2) << (1 + months.find(month) / 4) << year.substr(2);
|
ss << year << setw(2) << setfill('0') << (1 + months.find(month) / 4) << setw(2) << setfill('0') << day;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ss << "-";
|
||||||
|
|
||||||
|
#ifdef GIT_SHA
|
||||||
|
ss << GIT_SHA;
|
||||||
|
#else
|
||||||
|
ss << "nogit";
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
ss << (to_uci ? "\nid author ": " by ")
|
ss << (to_uci ? "\nid author ": " by ")
|
||||||
@@ -161,7 +183,7 @@ const string engine_info(bool to_uci) {
|
|||||||
|
|
||||||
/// compiler_info() returns a string trying to describe the compiler we use
|
/// compiler_info() returns a string trying to describe the compiler we use
|
||||||
|
|
||||||
const std::string compiler_info() {
|
std::string compiler_info() {
|
||||||
|
|
||||||
#define stringify2(x) #x
|
#define stringify2(x) #x
|
||||||
#define stringify(x) stringify2(x)
|
#define stringify(x) stringify2(x)
|
||||||
@@ -190,6 +212,18 @@ const std::string compiler_info() {
|
|||||||
compiler += "(version ";
|
compiler += "(version ";
|
||||||
compiler += stringify(_MSC_FULL_VER) "." stringify(_MSC_BUILD);
|
compiler += stringify(_MSC_FULL_VER) "." stringify(_MSC_BUILD);
|
||||||
compiler += ")";
|
compiler += ")";
|
||||||
|
#elif defined(__e2k__) && defined(__LCC__)
|
||||||
|
#define dot_ver2(n) \
|
||||||
|
compiler += (char)'.'; \
|
||||||
|
compiler += (char)('0' + (n) / 10); \
|
||||||
|
compiler += (char)('0' + (n) % 10);
|
||||||
|
|
||||||
|
compiler += "MCST LCC ";
|
||||||
|
compiler += "(version ";
|
||||||
|
compiler += std::to_string(__LCC__ / 100);
|
||||||
|
dot_ver2(__LCC__ % 100)
|
||||||
|
dot_ver2(__LCC_MINOR__)
|
||||||
|
compiler += ")";
|
||||||
#elif __GNUC__
|
#elif __GNUC__
|
||||||
compiler += "g++ (GNUC) ";
|
compiler += "g++ (GNUC) ";
|
||||||
compiler += make_version_string(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
|
compiler += make_version_string(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
|
||||||
@@ -361,7 +395,11 @@ void std_aligned_free(void* ptr) {
|
|||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
|
||||||
static void* aligned_large_pages_alloc_win(size_t allocSize) {
|
static void* aligned_large_pages_alloc_windows([[maybe_unused]] size_t allocSize) {
|
||||||
|
|
||||||
|
#if !defined(_WIN64)
|
||||||
|
return nullptr;
|
||||||
|
#else
|
||||||
|
|
||||||
HANDLE hProcessToken { };
|
HANDLE hProcessToken { };
|
||||||
LUID luid { };
|
LUID luid { };
|
||||||
@@ -404,12 +442,14 @@ static void* aligned_large_pages_alloc_win(size_t allocSize) {
|
|||||||
CloseHandle(hProcessToken);
|
CloseHandle(hProcessToken);
|
||||||
|
|
||||||
return mem;
|
return mem;
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void* aligned_large_pages_alloc(size_t allocSize) {
|
void* aligned_large_pages_alloc(size_t allocSize) {
|
||||||
|
|
||||||
// Try to allocate large pages
|
// Try to allocate large pages
|
||||||
void* mem = aligned_large_pages_alloc_win(allocSize);
|
void* mem = aligned_large_pages_alloc_windows(allocSize);
|
||||||
|
|
||||||
// Fall back to regular, page aligned, allocation if necessary
|
// Fall back to regular, page aligned, allocation if necessary
|
||||||
if (!mem)
|
if (!mem)
|
||||||
@@ -449,8 +489,9 @@ void aligned_large_pages_free(void* mem) {
|
|||||||
if (mem && !VirtualFree(mem, 0, MEM_RELEASE))
|
if (mem && !VirtualFree(mem, 0, MEM_RELEASE))
|
||||||
{
|
{
|
||||||
DWORD err = GetLastError();
|
DWORD err = GetLastError();
|
||||||
std::cerr << "Failed to free transposition table. Error code: 0x" <<
|
std::cerr << "Failed to free large page memory. Error code: 0x"
|
||||||
std::hex << err << std::dec << std::endl;
|
<< std::hex << err
|
||||||
|
<< std::dec << std::endl;
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -472,11 +513,11 @@ void bindThisThread(size_t) {}
|
|||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
/// best_group() retrieves logical processor information using Windows specific
|
/// best_node() retrieves logical processor information using Windows specific
|
||||||
/// API and returns the best group id for the thread with index idx. Original
|
/// API and returns the best node id for the thread with index idx. Original
|
||||||
/// code from Texel by Peter Österlund.
|
/// code from Texel by Peter Österlund.
|
||||||
|
|
||||||
int best_group(size_t idx) {
|
int best_node(size_t idx) {
|
||||||
|
|
||||||
int threads = 0;
|
int threads = 0;
|
||||||
int nodes = 0;
|
int nodes = 0;
|
||||||
@@ -490,7 +531,8 @@ int best_group(size_t idx) {
|
|||||||
if (!fun1)
|
if (!fun1)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
// First call to get returnLength. We expect it to fail due to null buffer
|
// First call to GetLogicalProcessorInformationEx() to get returnLength.
|
||||||
|
// We expect the call to fail due to null buffer.
|
||||||
if (fun1(RelationAll, nullptr, &returnLength))
|
if (fun1(RelationAll, nullptr, &returnLength))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@@ -498,7 +540,7 @@ int best_group(size_t idx) {
|
|||||||
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *buffer, *ptr;
|
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *buffer, *ptr;
|
||||||
ptr = buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)malloc(returnLength);
|
ptr = buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)malloc(returnLength);
|
||||||
|
|
||||||
// Second call, now we expect to succeed
|
// Second call to GetLogicalProcessorInformationEx(), now we expect to succeed
|
||||||
if (!fun1(RelationAll, buffer, &returnLength))
|
if (!fun1(RelationAll, buffer, &returnLength))
|
||||||
{
|
{
|
||||||
free(buffer);
|
free(buffer);
|
||||||
@@ -548,22 +590,38 @@ int best_group(size_t idx) {
|
|||||||
void bindThisThread(size_t idx) {
|
void bindThisThread(size_t idx) {
|
||||||
|
|
||||||
// Use only local variables to be thread-safe
|
// Use only local variables to be thread-safe
|
||||||
int group = best_group(idx);
|
int node = best_node(idx);
|
||||||
|
|
||||||
if (group == -1)
|
if (node == -1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Early exit if the needed API are not available at runtime
|
// Early exit if the needed API are not available at runtime
|
||||||
HMODULE k32 = GetModuleHandle("Kernel32.dll");
|
HMODULE k32 = GetModuleHandle("Kernel32.dll");
|
||||||
auto fun2 = (fun2_t)(void(*)())GetProcAddress(k32, "GetNumaNodeProcessorMaskEx");
|
auto fun2 = (fun2_t)(void(*)())GetProcAddress(k32, "GetNumaNodeProcessorMaskEx");
|
||||||
auto fun3 = (fun3_t)(void(*)())GetProcAddress(k32, "SetThreadGroupAffinity");
|
auto fun3 = (fun3_t)(void(*)())GetProcAddress(k32, "SetThreadGroupAffinity");
|
||||||
|
auto fun4 = (fun4_t)(void(*)())GetProcAddress(k32, "GetNumaNodeProcessorMask2");
|
||||||
|
auto fun5 = (fun5_t)(void(*)())GetProcAddress(k32, "GetMaximumProcessorGroupCount");
|
||||||
|
|
||||||
if (!fun2 || !fun3)
|
if (!fun2 || !fun3)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (!fun4 || !fun5)
|
||||||
|
{
|
||||||
GROUP_AFFINITY affinity;
|
GROUP_AFFINITY affinity;
|
||||||
if (fun2(group, &affinity))
|
if (fun2(node, &affinity)) // GetNumaNodeProcessorMaskEx
|
||||||
fun3(GetCurrentThread(), &affinity, nullptr);
|
fun3(GetCurrentThread(), &affinity, nullptr); // SetThreadGroupAffinity
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// If a numa node has more than one processor group, we assume they are
|
||||||
|
// sized equal and we spread threads evenly across the groups.
|
||||||
|
USHORT elements, returnedElements;
|
||||||
|
elements = fun5(); // GetMaximumProcessorGroupCount
|
||||||
|
GROUP_AFFINITY *affinity = (GROUP_AFFINITY*)malloc(elements * sizeof(GROUP_AFFINITY));
|
||||||
|
if (fun4(node, affinity, elements, &returnedElements)) // GetNumaNodeProcessorMask2
|
||||||
|
fun3(GetCurrentThread(), &affinity[idx % returnedElements], nullptr); // SetThreadGroupAffinity
|
||||||
|
free(affinity);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -584,8 +642,7 @@ string argv0; // path+name of the executable binary, as given by argv
|
|||||||
string binaryDirectory; // path of the executable directory
|
string binaryDirectory; // path of the executable directory
|
||||||
string workingDirectory; // path of the working directory
|
string workingDirectory; // path of the working directory
|
||||||
|
|
||||||
void init(int argc, char* argv[]) {
|
void init([[maybe_unused]] int argc, char* argv[]) {
|
||||||
(void)argc;
|
|
||||||
string pathSeparator;
|
string pathSeparator;
|
||||||
|
|
||||||
// extract the path+name of the executable binary
|
// extract the path+name of the executable binary
|
||||||
@@ -626,3 +683,5 @@ void init(int argc, char* argv[]) {
|
|||||||
|
|
||||||
|
|
||||||
} // namespace CommandLine
|
} // namespace CommandLine
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+58
-6
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -28,8 +28,10 @@
|
|||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
const std::string engine_info(bool to_uci = false);
|
namespace Stockfish {
|
||||||
const std::string compiler_info();
|
|
||||||
|
std::string engine_info(bool to_uci = false);
|
||||||
|
std::string compiler_info();
|
||||||
void prefetch(void* addr);
|
void prefetch(void* addr);
|
||||||
void start_logger(const std::string& fname);
|
void start_logger(const std::string& fname);
|
||||||
void* std_aligned_alloc(size_t alignment, size_t size);
|
void* std_aligned_alloc(size_t alignment, size_t size);
|
||||||
@@ -64,9 +66,10 @@ std::ostream& operator<<(std::ostream&, SyncCout);
|
|||||||
#define sync_cout std::cout << IO_LOCK
|
#define sync_cout std::cout << IO_LOCK
|
||||||
#define sync_endl std::endl << IO_UNLOCK
|
#define sync_endl std::endl << IO_UNLOCK
|
||||||
|
|
||||||
// `ptr` must point to an array of size at least
|
|
||||||
// `sizeof(T) * N + alignment` bytes, where `N` is the
|
// align_ptr_up() : get the first aligned element of an array.
|
||||||
// number of elements in the array.
|
// ptr must point to an array of size at least `sizeof(T) * N + alignment` bytes,
|
||||||
|
// where N is the number of elements in the array.
|
||||||
template <uintptr_t Alignment, typename T>
|
template <uintptr_t Alignment, typename T>
|
||||||
T* align_ptr_up(T* ptr)
|
T* align_ptr_up(T* ptr)
|
||||||
{
|
{
|
||||||
@@ -76,6 +79,53 @@ T* align_ptr_up(T* ptr)
|
|||||||
return reinterpret_cast<T*>(reinterpret_cast<char*>((ptrint + (Alignment - 1)) / Alignment * Alignment));
|
return reinterpret_cast<T*>(reinterpret_cast<char*>((ptrint + (Alignment - 1)) / Alignment * Alignment));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// IsLittleEndian : true if and only if the binary is compiled on a little endian machine
|
||||||
|
static inline const union { uint32_t i; char c[4]; } Le = { 0x01020304 };
|
||||||
|
static inline const bool IsLittleEndian = (Le.c[0] == 4);
|
||||||
|
|
||||||
|
|
||||||
|
// RunningAverage : a class to calculate a running average of a series of values.
|
||||||
|
// For efficiency, all computations are done with integers.
|
||||||
|
class RunningAverage {
|
||||||
|
public:
|
||||||
|
|
||||||
|
// Reset the running average to rational value p / q
|
||||||
|
void set(int64_t p, int64_t q)
|
||||||
|
{ average = p * PERIOD * RESOLUTION / q; }
|
||||||
|
|
||||||
|
// Update average with value v
|
||||||
|
void update(int64_t v)
|
||||||
|
{ average = RESOLUTION * v + (PERIOD - 1) * average / PERIOD; }
|
||||||
|
|
||||||
|
// Test if average is strictly greater than rational a / b
|
||||||
|
bool is_greater(int64_t a, int64_t b) const
|
||||||
|
{ return b * average > a * (PERIOD * RESOLUTION); }
|
||||||
|
|
||||||
|
int64_t value() const
|
||||||
|
{ return average / (PERIOD * RESOLUTION); }
|
||||||
|
|
||||||
|
private :
|
||||||
|
static constexpr int64_t PERIOD = 4096;
|
||||||
|
static constexpr int64_t RESOLUTION = 1024;
|
||||||
|
int64_t average;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, std::size_t MaxSize>
|
||||||
|
class ValueList {
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::size_t size() const { return size_; }
|
||||||
|
void push_back(const T& value) { values_[size_++] = value; }
|
||||||
|
const T* begin() const { return values_; }
|
||||||
|
const T* end() const { return values_ + size_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
T values_[MaxSize];
|
||||||
|
std::size_t size_ = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/// xorshift64star Pseudo-Random Number Generator
|
/// xorshift64star Pseudo-Random Number Generator
|
||||||
/// This class is based on original code written and dedicated
|
/// This class is based on original code written and dedicated
|
||||||
/// to the public domain by Sebastiano Vigna (2014).
|
/// to the public domain by Sebastiano Vigna (2014).
|
||||||
@@ -143,4 +193,6 @@ namespace CommandLine {
|
|||||||
extern std::string workingDirectory; // path of the working directory
|
extern std::string workingDirectory; // path of the working directory
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef MISC_H_INCLUDED
|
#endif // #ifndef MISC_H_INCLUDED
|
||||||
|
|||||||
+68
-156
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -21,23 +21,20 @@
|
|||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
template<GenType Type, Direction D>
|
template<GenType Type, Direction D>
|
||||||
ExtMove* make_promotions(ExtMove* moveList, Square to, Square ksq) {
|
ExtMove* make_promotions(ExtMove* moveList, Square to) {
|
||||||
|
|
||||||
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
||||||
{
|
|
||||||
*moveList++ = make<PROMOTION>(to - D, to, QUEEN);
|
*moveList++ = make<PROMOTION>(to - D, to, QUEEN);
|
||||||
if (attacks_bb<KNIGHT>(to) & ksq)
|
|
||||||
*moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
|
if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
|
||||||
{
|
{
|
||||||
*moveList++ = make<PROMOTION>(to - D, to, ROOK);
|
*moveList++ = make<PROMOTION>(to - D, to, ROOK);
|
||||||
*moveList++ = make<PROMOTION>(to - D, to, BISHOP);
|
*moveList++ = make<PROMOTION>(to - D, to, BISHOP);
|
||||||
if (!(attacks_bb<KNIGHT>(to) & ksq))
|
|
||||||
*moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
|
*moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,20 +52,16 @@ namespace {
|
|||||||
constexpr Direction UpRight = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
|
constexpr Direction UpRight = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
|
||||||
constexpr Direction UpLeft = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
|
constexpr Direction UpLeft = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
|
||||||
|
|
||||||
const Square ksq = pos.square<KING>(Them);
|
const Bitboard emptySquares = ~pos.pieces();
|
||||||
Bitboard emptySquares;
|
const Bitboard enemies = Type == EVASIONS ? pos.checkers()
|
||||||
|
: pos.pieces(Them);
|
||||||
|
|
||||||
Bitboard pawnsOn7 = pos.pieces(Us, PAWN) & TRank7BB;
|
Bitboard pawnsOn7 = pos.pieces(Us, PAWN) & TRank7BB;
|
||||||
Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
|
Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
|
||||||
|
|
||||||
Bitboard enemies = (Type == EVASIONS ? pos.pieces(Them) & target:
|
|
||||||
Type == CAPTURES ? target : pos.pieces(Them));
|
|
||||||
|
|
||||||
// Single and double pawn pushes, no promotions
|
// Single and double pawn pushes, no promotions
|
||||||
if (Type != CAPTURES)
|
if (Type != CAPTURES)
|
||||||
{
|
{
|
||||||
emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
|
|
||||||
|
|
||||||
Bitboard b1 = shift<Up>(pawnsNotOn7) & emptySquares;
|
Bitboard b1 = shift<Up>(pawnsNotOn7) & emptySquares;
|
||||||
Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
|
Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
|
||||||
|
|
||||||
@@ -80,33 +73,24 @@ namespace {
|
|||||||
|
|
||||||
if (Type == QUIET_CHECKS)
|
if (Type == QUIET_CHECKS)
|
||||||
{
|
{
|
||||||
b1 &= pawn_attacks_bb(Them, ksq);
|
// To make a quiet check, you either make a direct check by pushing a pawn
|
||||||
b2 &= pawn_attacks_bb(Them, ksq);
|
// or push a blocker pawn that is not on the same file as the enemy king.
|
||||||
|
// Discovered check promotion has been already generated amongst the captures.
|
||||||
// Add pawn pushes which give discovered check. This is possible only
|
Square ksq = pos.square<KING>(Them);
|
||||||
// if the pawn is not on the same file as the enemy king, because we
|
Bitboard dcCandidatePawns = pos.blockers_for_king(Them) & ~file_bb(ksq);
|
||||||
// don't generate captures. Note that a possible discovered check
|
b1 &= pawn_attacks_bb(Them, ksq) | shift< Up>(dcCandidatePawns);
|
||||||
// promotion has been already generated amongst the captures.
|
b2 &= pawn_attacks_bb(Them, ksq) | shift<Up+Up>(dcCandidatePawns);
|
||||||
Bitboard dcCandidateQuiets = pos.blockers_for_king(Them) & pawnsNotOn7;
|
|
||||||
if (dcCandidateQuiets)
|
|
||||||
{
|
|
||||||
Bitboard dc1 = shift<Up>(dcCandidateQuiets) & emptySquares & ~file_bb(ksq);
|
|
||||||
Bitboard dc2 = shift<Up>(dc1 & TRank3BB) & emptySquares;
|
|
||||||
|
|
||||||
b1 |= dc1;
|
|
||||||
b2 |= dc2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (b1)
|
while (b1)
|
||||||
{
|
{
|
||||||
Square to = pop_lsb(&b1);
|
Square to = pop_lsb(b1);
|
||||||
*moveList++ = make_move(to - Up, to);
|
*moveList++ = make_move(to - Up, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (b2)
|
while (b2)
|
||||||
{
|
{
|
||||||
Square to = pop_lsb(&b2);
|
Square to = pop_lsb(b2);
|
||||||
*moveList++ = make_move(to - Up - Up, to);
|
*moveList++ = make_move(to - Up - Up, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,24 +98,21 @@ namespace {
|
|||||||
// Promotions and underpromotions
|
// Promotions and underpromotions
|
||||||
if (pawnsOn7)
|
if (pawnsOn7)
|
||||||
{
|
{
|
||||||
if (Type == CAPTURES)
|
|
||||||
emptySquares = ~pos.pieces();
|
|
||||||
|
|
||||||
if (Type == EVASIONS)
|
|
||||||
emptySquares &= target;
|
|
||||||
|
|
||||||
Bitboard b1 = shift<UpRight>(pawnsOn7) & enemies;
|
Bitboard b1 = shift<UpRight>(pawnsOn7) & enemies;
|
||||||
Bitboard b2 = shift<UpLeft >(pawnsOn7) & enemies;
|
Bitboard b2 = shift<UpLeft >(pawnsOn7) & enemies;
|
||||||
Bitboard b3 = shift<Up >(pawnsOn7) & emptySquares;
|
Bitboard b3 = shift<Up >(pawnsOn7) & emptySquares;
|
||||||
|
|
||||||
|
if (Type == EVASIONS)
|
||||||
|
b3 &= target;
|
||||||
|
|
||||||
while (b1)
|
while (b1)
|
||||||
moveList = make_promotions<Type, UpRight>(moveList, pop_lsb(&b1), ksq);
|
moveList = make_promotions<Type, UpRight>(moveList, pop_lsb(b1));
|
||||||
|
|
||||||
while (b2)
|
while (b2)
|
||||||
moveList = make_promotions<Type, UpLeft >(moveList, pop_lsb(&b2), ksq);
|
moveList = make_promotions<Type, UpLeft >(moveList, pop_lsb(b2));
|
||||||
|
|
||||||
while (b3)
|
while (b3)
|
||||||
moveList = make_promotions<Type, Up >(moveList, pop_lsb(&b3), ksq);
|
moveList = make_promotions<Type, Up >(moveList, pop_lsb(b3));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Standard and en passant captures
|
// Standard and en passant captures
|
||||||
@@ -142,13 +123,13 @@ namespace {
|
|||||||
|
|
||||||
while (b1)
|
while (b1)
|
||||||
{
|
{
|
||||||
Square to = pop_lsb(&b1);
|
Square to = pop_lsb(b1);
|
||||||
*moveList++ = make_move(to - UpRight, to);
|
*moveList++ = make_move(to - UpRight, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (b2)
|
while (b2)
|
||||||
{
|
{
|
||||||
Square to = pop_lsb(&b2);
|
Square to = pop_lsb(b2);
|
||||||
*moveList++ = make_move(to - UpLeft, to);
|
*moveList++ = make_move(to - UpLeft, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +137,7 @@ namespace {
|
|||||||
{
|
{
|
||||||
assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
|
assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
|
||||||
|
|
||||||
// An en passant capture cannot resolve a discovered check.
|
// An en passant capture cannot resolve a discovered check
|
||||||
if (Type == EVASIONS && (target & (pos.ep_square() + Up)))
|
if (Type == EVASIONS && (target & (pos.ep_square() + Up)))
|
||||||
return moveList;
|
return moveList;
|
||||||
|
|
||||||
@@ -165,7 +146,7 @@ namespace {
|
|||||||
assert(b1);
|
assert(b1);
|
||||||
|
|
||||||
while (b1)
|
while (b1)
|
||||||
*moveList++ = make<EN_PASSANT>(pop_lsb(&b1), pos.ep_square());
|
*moveList++ = make<EN_PASSANT>(pop_lsb(b1), pos.ep_square());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,27 +154,24 @@ namespace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<PieceType Pt, bool Checks>
|
template<Color Us, PieceType Pt, bool Checks>
|
||||||
ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Bitboard piecesToMove, Bitboard target) {
|
ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
|
||||||
|
|
||||||
static_assert(Pt != KING && Pt != PAWN, "Unsupported piece type in generate_moves()");
|
static_assert(Pt != KING && Pt != PAWN, "Unsupported piece type in generate_moves()");
|
||||||
|
|
||||||
Bitboard bb = piecesToMove & pos.pieces(Pt);
|
Bitboard bb = pos.pieces(Us, Pt);
|
||||||
|
|
||||||
if (!bb)
|
|
||||||
return moveList;
|
|
||||||
|
|
||||||
[[maybe_unused]] const Bitboard checkSquares = pos.check_squares(Pt);
|
|
||||||
|
|
||||||
while (bb) {
|
|
||||||
Square from = pop_lsb(&bb);
|
|
||||||
|
|
||||||
|
while (bb)
|
||||||
|
{
|
||||||
|
Square from = pop_lsb(bb);
|
||||||
Bitboard b = attacks_bb<Pt>(from, pos.pieces()) & target;
|
Bitboard b = attacks_bb<Pt>(from, pos.pieces()) & target;
|
||||||
if constexpr (Checks)
|
|
||||||
b &= checkSquares;
|
// To check, you either move freely a blocker or make a direct check.
|
||||||
|
if (Checks && (Pt == QUEEN || !(pos.blockers_for_king(~Us) & from)))
|
||||||
|
b &= pos.check_squares(Pt);
|
||||||
|
|
||||||
while (b)
|
while (b)
|
||||||
*moveList++ = make_move(from, pop_lsb(&b));
|
*moveList++ = make_move(from, pop_lsb(b));
|
||||||
}
|
}
|
||||||
|
|
||||||
return moveList;
|
return moveList;
|
||||||
@@ -206,45 +184,34 @@ namespace {
|
|||||||
static_assert(Type != LEGAL, "Unsupported type in generate_all()");
|
static_assert(Type != LEGAL, "Unsupported type in generate_all()");
|
||||||
|
|
||||||
constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantiations
|
constexpr bool Checks = Type == QUIET_CHECKS; // Reduce template instantiations
|
||||||
Bitboard target, piecesToMove = pos.pieces(Us);
|
const Square ksq = pos.square<KING>(Us);
|
||||||
|
Bitboard target;
|
||||||
|
|
||||||
if(Type == QUIET_CHECKS)
|
// Skip generating non-king moves when in double check
|
||||||
piecesToMove &= ~pos.blockers_for_king(~Us);
|
if (Type != EVASIONS || !more_than_one(pos.checkers()))
|
||||||
|
|
||||||
switch (Type)
|
|
||||||
{
|
{
|
||||||
case CAPTURES:
|
target = Type == EVASIONS ? between_bb(ksq, lsb(pos.checkers()))
|
||||||
target = pos.pieces(~Us);
|
: Type == NON_EVASIONS ? ~pos.pieces( Us)
|
||||||
break;
|
: Type == CAPTURES ? pos.pieces(~Us)
|
||||||
case QUIETS:
|
: ~pos.pieces( ); // QUIETS || QUIET_CHECKS
|
||||||
case QUIET_CHECKS:
|
|
||||||
target = ~pos.pieces();
|
|
||||||
break;
|
|
||||||
case EVASIONS:
|
|
||||||
{
|
|
||||||
Square checksq = lsb(pos.checkers());
|
|
||||||
target = between_bb(pos.square<KING>(Us), checksq) | checksq;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case NON_EVASIONS:
|
|
||||||
target = ~pos.pieces(Us);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
|
moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
|
||||||
moveList = generate_moves<KNIGHT, Checks>(pos, moveList, piecesToMove, target);
|
moveList = generate_moves<Us, KNIGHT, Checks>(pos, moveList, target);
|
||||||
moveList = generate_moves<BISHOP, Checks>(pos, moveList, piecesToMove, target);
|
moveList = generate_moves<Us, BISHOP, Checks>(pos, moveList, target);
|
||||||
moveList = generate_moves< ROOK, Checks>(pos, moveList, piecesToMove, target);
|
moveList = generate_moves<Us, ROOK, Checks>(pos, moveList, target);
|
||||||
moveList = generate_moves< QUEEN, Checks>(pos, moveList, piecesToMove, target);
|
moveList = generate_moves<Us, QUEEN, Checks>(pos, moveList, target);
|
||||||
|
}
|
||||||
|
|
||||||
if (Type != QUIET_CHECKS && Type != EVASIONS)
|
if (!Checks || pos.blockers_for_king(~Us) & ksq)
|
||||||
{
|
{
|
||||||
Square ksq = pos.square<KING>(Us);
|
Bitboard b = attacks_bb<KING>(ksq) & (Type == EVASIONS ? ~pos.pieces(Us) : target);
|
||||||
Bitboard b = attacks_bb<KING>(ksq) & target;
|
if (Checks)
|
||||||
while (b)
|
b &= ~attacks_bb<QUEEN>(pos.square<KING>(~Us));
|
||||||
*moveList++ = make_move(ksq, pop_lsb(&b));
|
|
||||||
|
|
||||||
if ((Type != CAPTURES) && pos.can_castle(Us & ANY_CASTLING))
|
while (b)
|
||||||
|
*moveList++ = make_move(ksq, pop_lsb(b));
|
||||||
|
|
||||||
|
if ((Type == QUIETS || Type == NON_EVASIONS) && pos.can_castle(Us & ANY_CASTLING))
|
||||||
for (CastlingRights cr : { Us & KING_SIDE, Us & QUEEN_SIDE } )
|
for (CastlingRights cr : { Us & KING_SIDE, Us & QUEEN_SIDE } )
|
||||||
if (!pos.castling_impeded(cr) && pos.can_castle(cr))
|
if (!pos.castling_impeded(cr) && pos.can_castle(cr))
|
||||||
*moveList++ = make<CASTLING>(ksq, pos.castling_rook_square(cr));
|
*moveList++ = make<CASTLING>(ksq, pos.castling_rook_square(cr));
|
||||||
@@ -256,8 +223,10 @@ namespace {
|
|||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
/// <CAPTURES> Generates all pseudo-legal captures plus queen and checking knight promotions
|
/// <CAPTURES> Generates all pseudo-legal captures plus queen promotions
|
||||||
/// <QUIETS> Generates all pseudo-legal non-captures and underpromotions (except checking knight)
|
/// <QUIETS> Generates all pseudo-legal non-captures and underpromotions
|
||||||
|
/// <EVASIONS> Generates all pseudo-legal check evasions when the side to move is in check
|
||||||
|
/// <QUIET_CHECKS> Generates all pseudo-legal non-captures giving check, except castling and promotions
|
||||||
/// <NON_EVASIONS> Generates all pseudo-legal captures and non-captures
|
/// <NON_EVASIONS> Generates all pseudo-legal captures and non-captures
|
||||||
///
|
///
|
||||||
/// Returns a pointer to the end of the move list.
|
/// Returns a pointer to the end of the move list.
|
||||||
@@ -265,8 +234,8 @@ namespace {
|
|||||||
template<GenType Type>
|
template<GenType Type>
|
||||||
ExtMove* generate(const Position& pos, ExtMove* moveList) {
|
ExtMove* generate(const Position& pos, ExtMove* moveList) {
|
||||||
|
|
||||||
static_assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS, "Unsupported type in generate()");
|
static_assert(Type != LEGAL, "Unsupported type in generate()");
|
||||||
assert(!pos.checkers());
|
assert((Type == EVASIONS) == (bool)pos.checkers());
|
||||||
|
|
||||||
Color us = pos.side_to_move();
|
Color us = pos.side_to_move();
|
||||||
|
|
||||||
@@ -277,70 +246,11 @@ ExtMove* generate(const Position& pos, ExtMove* moveList) {
|
|||||||
// Explicit template instantiations
|
// Explicit template instantiations
|
||||||
template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
|
template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
|
||||||
template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
|
template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
|
||||||
|
template ExtMove* generate<EVASIONS>(const Position&, ExtMove*);
|
||||||
|
template ExtMove* generate<QUIET_CHECKS>(const Position&, ExtMove*);
|
||||||
template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
|
template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
|
||||||
|
|
||||||
|
|
||||||
/// generate<QUIET_CHECKS> generates all pseudo-legal non-captures giving check,
|
|
||||||
/// except castling. Returns a pointer to the end of the move list.
|
|
||||||
template<>
|
|
||||||
ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
|
|
||||||
|
|
||||||
assert(!pos.checkers());
|
|
||||||
|
|
||||||
Color us = pos.side_to_move();
|
|
||||||
Bitboard dc = pos.blockers_for_king(~us) & pos.pieces(us) & ~pos.pieces(PAWN);
|
|
||||||
|
|
||||||
while (dc)
|
|
||||||
{
|
|
||||||
Square from = pop_lsb(&dc);
|
|
||||||
PieceType pt = type_of(pos.piece_on(from));
|
|
||||||
|
|
||||||
Bitboard b = attacks_bb(pt, from, pos.pieces()) & ~pos.pieces();
|
|
||||||
|
|
||||||
if (pt == KING)
|
|
||||||
b &= ~attacks_bb<QUEEN>(pos.square<KING>(~us));
|
|
||||||
|
|
||||||
while (b)
|
|
||||||
*moveList++ = make_move(from, pop_lsb(&b));
|
|
||||||
}
|
|
||||||
|
|
||||||
return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList)
|
|
||||||
: generate_all<BLACK, QUIET_CHECKS>(pos, moveList);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// generate<EVASIONS> generates all pseudo-legal check evasions when the side
|
|
||||||
/// to move is in check. Returns a pointer to the end of the move list.
|
|
||||||
template<>
|
|
||||||
ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
|
|
||||||
|
|
||||||
assert(pos.checkers());
|
|
||||||
|
|
||||||
Color us = pos.side_to_move();
|
|
||||||
Square ksq = pos.square<KING>(us);
|
|
||||||
Bitboard sliderAttacks = 0;
|
|
||||||
Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
|
|
||||||
|
|
||||||
// Find all the squares attacked by slider checkers. We will remove them from
|
|
||||||
// the king evasions in order to skip known illegal moves, which avoids any
|
|
||||||
// useless legality checks later on.
|
|
||||||
while (sliders)
|
|
||||||
sliderAttacks |= line_bb(ksq, pop_lsb(&sliders)) & ~pos.checkers();
|
|
||||||
|
|
||||||
// Generate evasions for king, capture and non capture moves
|
|
||||||
Bitboard b = attacks_bb<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
|
|
||||||
while (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
|
|
||||||
|
|
||||||
// Generate blocking evasions or captures of the checking piece
|
|
||||||
return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, moveList)
|
|
||||||
: generate_all<BLACK, EVASIONS>(pos, moveList);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// generate<LEGAL> generates all the legal moves in the given position
|
/// generate<LEGAL> generates all the legal moves in the given position
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
@@ -362,3 +272,5 @@ ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
|
|||||||
|
|
||||||
return moveList;
|
return moveList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+5
-1
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -23,6 +23,8 @@
|
|||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
class Position;
|
class Position;
|
||||||
|
|
||||||
enum GenType {
|
enum GenType {
|
||||||
@@ -70,4 +72,6 @@ private:
|
|||||||
ExtMove moveList[MAX_MOVES], *last;
|
ExtMove moveList[MAX_MOVES], *last;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef MOVEGEN_H_INCLUDED
|
#endif // #ifndef MOVEGEN_H_INCLUDED
|
||||||
|
|||||||
+53
-22
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -18,8 +18,11 @@
|
|||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "bitboard.h"
|
||||||
#include "movepick.h"
|
#include "movepick.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
enum Stages {
|
enum Stages {
|
||||||
@@ -54,35 +57,40 @@ namespace {
|
|||||||
/// ordering is at the current node.
|
/// ordering is at the current node.
|
||||||
|
|
||||||
/// MovePicker constructor for the main search
|
/// MovePicker constructor for the main search
|
||||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh, const LowPlyHistory* lp,
|
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
|
||||||
const CapturePieceToHistory* cph, const PieceToHistory** ch, Move cm, const Move* killers, int pl)
|
const CapturePieceToHistory* cph,
|
||||||
: pos(p), mainHistory(mh), lowPlyHistory(lp), captureHistory(cph), continuationHistory(ch),
|
const PieceToHistory** ch,
|
||||||
ttMove(ttm), refutations{{killers[0], 0}, {killers[1], 0}, {cm, 0}}, depth(d), ply(pl) {
|
Move cm,
|
||||||
|
const Move* killers)
|
||||||
|
: pos(p), mainHistory(mh), captureHistory(cph), continuationHistory(ch),
|
||||||
|
ttMove(ttm), refutations{{killers[0], 0}, {killers[1], 0}, {cm, 0}}, depth(d)
|
||||||
|
{
|
||||||
assert(d > 0);
|
assert(d > 0);
|
||||||
|
|
||||||
stage = (pos.checkers() ? EVASION_TT : MAIN_TT) +
|
stage = (pos.checkers() ? EVASION_TT : MAIN_TT) +
|
||||||
!(ttm && pos.pseudo_legal(ttm));
|
!(ttm && pos.pseudo_legal(ttm));
|
||||||
|
threatenedPieces = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// MovePicker constructor for quiescence search
|
/// MovePicker constructor for quiescence search
|
||||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
|
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
|
||||||
const CapturePieceToHistory* cph, const PieceToHistory** ch, Square rs)
|
const CapturePieceToHistory* cph,
|
||||||
: pos(p), mainHistory(mh), captureHistory(cph), continuationHistory(ch), ttMove(ttm), recaptureSquare(rs), depth(d) {
|
const PieceToHistory** ch,
|
||||||
|
Square rs)
|
||||||
|
: pos(p), mainHistory(mh), captureHistory(cph), continuationHistory(ch), ttMove(ttm), recaptureSquare(rs), depth(d)
|
||||||
|
{
|
||||||
assert(d <= 0);
|
assert(d <= 0);
|
||||||
|
|
||||||
stage = (pos.checkers() ? EVASION_TT : QSEARCH_TT) +
|
stage = (pos.checkers() ? EVASION_TT : QSEARCH_TT) +
|
||||||
!( ttm
|
!( ttm
|
||||||
&& (pos.checkers() || depth > DEPTH_QS_RECAPTURES || to_sq(ttm) == recaptureSquare)
|
|
||||||
&& pos.pseudo_legal(ttm));
|
&& pos.pseudo_legal(ttm));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// MovePicker constructor for ProbCut: we generate captures with SEE greater
|
/// MovePicker constructor for ProbCut: we generate captures with SEE greater
|
||||||
/// than or equal to the given threshold.
|
/// than or equal to the given threshold.
|
||||||
MovePicker::MovePicker(const Position& p, Move ttm, Value th, const CapturePieceToHistory* cph)
|
MovePicker::MovePicker(const Position& p, Move ttm, Value th, const CapturePieceToHistory* cph)
|
||||||
: pos(p), captureHistory(cph), ttMove(ttm), threshold(th) {
|
: pos(p), captureHistory(cph), ttMove(ttm), threshold(th)
|
||||||
|
{
|
||||||
assert(!pos.checkers());
|
assert(!pos.checkers());
|
||||||
|
|
||||||
stage = PROBCUT_TT + !(ttm && pos.capture(ttm)
|
stage = PROBCUT_TT + !(ttm && pos.capture(ttm)
|
||||||
@@ -98,28 +106,48 @@ void MovePicker::score() {
|
|||||||
|
|
||||||
static_assert(Type == CAPTURES || Type == QUIETS || Type == EVASIONS, "Wrong type");
|
static_assert(Type == CAPTURES || Type == QUIETS || Type == EVASIONS, "Wrong type");
|
||||||
|
|
||||||
|
[[maybe_unused]] Bitboard threatenedByPawn, threatenedByMinor, threatenedByRook;
|
||||||
|
if constexpr (Type == QUIETS)
|
||||||
|
{
|
||||||
|
Color us = pos.side_to_move();
|
||||||
|
|
||||||
|
threatenedByPawn = pos.attacks_by<PAWN>(~us);
|
||||||
|
threatenedByMinor = pos.attacks_by<KNIGHT>(~us) | pos.attacks_by<BISHOP>(~us) | threatenedByPawn;
|
||||||
|
threatenedByRook = pos.attacks_by<ROOK>(~us) | threatenedByMinor;
|
||||||
|
|
||||||
|
// Pieces threatened by pieces of lesser material value
|
||||||
|
threatenedPieces = (pos.pieces(us, QUEEN) & threatenedByRook)
|
||||||
|
| (pos.pieces(us, ROOK) & threatenedByMinor)
|
||||||
|
| (pos.pieces(us, KNIGHT, BISHOP) & threatenedByPawn);
|
||||||
|
}
|
||||||
|
|
||||||
for (auto& m : *this)
|
for (auto& m : *this)
|
||||||
if constexpr (Type == CAPTURES)
|
if constexpr (Type == CAPTURES)
|
||||||
m.value = int(PieceValue[MG][pos.piece_on(to_sq(m))]) * 6
|
m.value = 6 * int(PieceValue[MG][pos.piece_on(to_sq(m))])
|
||||||
+ (*captureHistory)[pos.moved_piece(m)][to_sq(m)][type_of(pos.piece_on(to_sq(m)))];
|
+ (*captureHistory)[pos.moved_piece(m)][to_sq(m)][type_of(pos.piece_on(to_sq(m)))];
|
||||||
|
|
||||||
else if constexpr (Type == QUIETS)
|
else if constexpr (Type == QUIETS)
|
||||||
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
|
m.value = 2 * (*mainHistory)[pos.side_to_move()][from_to(m)]
|
||||||
+ 2 * (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
|
+ 2 * (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
|
||||||
+ (*continuationHistory[1])[pos.moved_piece(m)][to_sq(m)]
|
+ (*continuationHistory[1])[pos.moved_piece(m)][to_sq(m)]
|
||||||
+ (*continuationHistory[3])[pos.moved_piece(m)][to_sq(m)]
|
+ (*continuationHistory[3])[pos.moved_piece(m)][to_sq(m)]
|
||||||
+ (*continuationHistory[5])[pos.moved_piece(m)][to_sq(m)]
|
+ (*continuationHistory[5])[pos.moved_piece(m)][to_sq(m)]
|
||||||
+ (ply < MAX_LPH ? std::min(4, depth / 3) * (*lowPlyHistory)[ply][from_to(m)] : 0);
|
+ (threatenedPieces & from_sq(m) ?
|
||||||
|
(type_of(pos.moved_piece(m)) == QUEEN && !(to_sq(m) & threatenedByRook) ? 50000
|
||||||
|
: type_of(pos.moved_piece(m)) == ROOK && !(to_sq(m) & threatenedByMinor) ? 25000
|
||||||
|
: !(to_sq(m) & threatenedByPawn) ? 15000
|
||||||
|
: 0)
|
||||||
|
: 0)
|
||||||
|
+ bool(pos.check_squares(type_of(pos.moved_piece(m))) & to_sq(m)) * 16384;
|
||||||
else // Type == EVASIONS
|
else // Type == EVASIONS
|
||||||
{
|
{
|
||||||
if (pos.capture(m))
|
if (pos.capture(m))
|
||||||
m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||||
- Value(type_of(pos.moved_piece(m)));
|
- Value(type_of(pos.moved_piece(m)))
|
||||||
|
+ (1 << 28);
|
||||||
else
|
else
|
||||||
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
|
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
|
||||||
+ 2 * (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
|
+ (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)];
|
||||||
- (1 << 28);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,11 +191,12 @@ top:
|
|||||||
endMoves = generate<CAPTURES>(pos, cur);
|
endMoves = generate<CAPTURES>(pos, cur);
|
||||||
|
|
||||||
score<CAPTURES>();
|
score<CAPTURES>();
|
||||||
|
partial_insertion_sort(cur, endMoves, std::numeric_limits<int>::min());
|
||||||
++stage;
|
++stage;
|
||||||
goto top;
|
goto top;
|
||||||
|
|
||||||
case GOOD_CAPTURE:
|
case GOOD_CAPTURE:
|
||||||
if (select<Best>([&](){
|
if (select<Next>([&](){
|
||||||
return pos.see_ge(*cur, Value(-69 * cur->value / 1024)) ?
|
return pos.see_ge(*cur, Value(-69 * cur->value / 1024)) ?
|
||||||
// Move losing capture to endBadCaptures to be tried later
|
// Move losing capture to endBadCaptures to be tried later
|
||||||
true : (*endBadCaptures++ = *cur, false); }))
|
true : (*endBadCaptures++ = *cur, false); }))
|
||||||
@@ -235,10 +264,10 @@ top:
|
|||||||
return select<Best>([](){ return true; });
|
return select<Best>([](){ return true; });
|
||||||
|
|
||||||
case PROBCUT:
|
case PROBCUT:
|
||||||
return select<Best>([&](){ return pos.see_ge(*cur, threshold); });
|
return select<Next>([&](){ return pos.see_ge(*cur, threshold); });
|
||||||
|
|
||||||
case QCAPTURE:
|
case QCAPTURE:
|
||||||
if (select<Best>([&](){ return depth > DEPTH_QS_RECAPTURES
|
if (select<Next>([&](){ return depth > DEPTH_QS_RECAPTURES
|
||||||
|| to_sq(*cur) == recaptureSquare; }))
|
|| to_sq(*cur) == recaptureSquare; }))
|
||||||
return *(cur - 1);
|
return *(cur - 1);
|
||||||
|
|
||||||
@@ -263,3 +292,5 @@ top:
|
|||||||
assert(false);
|
assert(false);
|
||||||
return MOVE_NONE; // Silence warning
|
return MOVE_NONE; // Silence warning
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+16
-18
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -27,6 +27,8 @@
|
|||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
/// StatsEntry stores the stat table value. It is usually a number but could
|
/// StatsEntry stores the stat table value. It is usually a number but could
|
||||||
/// be a move or even a nested history. We use a class instead of naked value
|
/// be a move or even a nested history. We use a class instead of naked value
|
||||||
/// to directly call history update operator<<() on the entry so to use stats
|
/// to directly call history update operator<<() on the entry so to use stats
|
||||||
@@ -84,13 +86,8 @@ enum StatsType { NoCaptures, Captures };
|
|||||||
/// unsuccessful during the current search, and is used for reduction and move
|
/// unsuccessful during the current search, and is used for reduction and move
|
||||||
/// ordering decisions. It uses 2 tables (one for each color) indexed by
|
/// ordering decisions. It uses 2 tables (one for each color) indexed by
|
||||||
/// the move's from and to squares, see www.chessprogramming.org/Butterfly_Boards
|
/// the move's from and to squares, see www.chessprogramming.org/Butterfly_Boards
|
||||||
typedef Stats<int16_t, 13365, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)> ButterflyHistory;
|
/// (~11 elo)
|
||||||
|
typedef Stats<int16_t, 7183, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)> ButterflyHistory;
|
||||||
/// At higher depths LowPlyHistory records successful quiet moves near the root
|
|
||||||
/// and quiet moves which are/were in the PV (ttPv). It is cleared with each new
|
|
||||||
/// search and filled during iterative deepening.
|
|
||||||
constexpr int MAX_LPH = 4;
|
|
||||||
typedef Stats<int16_t, 10692, MAX_LPH, int(SQUARE_NB) * int(SQUARE_NB)> LowPlyHistory;
|
|
||||||
|
|
||||||
/// CounterMoveHistory stores counter moves indexed by [piece][to] of the previous
|
/// CounterMoveHistory stores counter moves indexed by [piece][to] of the previous
|
||||||
/// move, see www.chessprogramming.org/Countermove_Heuristic
|
/// move, see www.chessprogramming.org/Countermove_Heuristic
|
||||||
@@ -105,6 +102,7 @@ typedef Stats<int16_t, 29952, PIECE_NB, SQUARE_NB> PieceToHistory;
|
|||||||
/// ContinuationHistory is the combined history of a given pair of moves, usually
|
/// ContinuationHistory is the combined history of a given pair of moves, usually
|
||||||
/// the current one given a previous one. The nested history table is based on
|
/// the current one given a previous one. The nested history table is based on
|
||||||
/// PieceToHistory instead of ButterflyBoards.
|
/// PieceToHistory instead of ButterflyBoards.
|
||||||
|
/// (~63 elo)
|
||||||
typedef Stats<PieceToHistory, NOT_USED, PIECE_NB, SQUARE_NB> ContinuationHistory;
|
typedef Stats<PieceToHistory, NOT_USED, PIECE_NB, SQUARE_NB> ContinuationHistory;
|
||||||
|
|
||||||
|
|
||||||
@@ -121,20 +119,20 @@ class MovePicker {
|
|||||||
public:
|
public:
|
||||||
MovePicker(const MovePicker&) = delete;
|
MovePicker(const MovePicker&) = delete;
|
||||||
MovePicker& operator=(const MovePicker&) = delete;
|
MovePicker& operator=(const MovePicker&) = delete;
|
||||||
MovePicker(const Position&, Move, Value, const CapturePieceToHistory*);
|
MovePicker(const Position&, Move, Depth, const ButterflyHistory*,
|
||||||
|
const CapturePieceToHistory*,
|
||||||
|
const PieceToHistory**,
|
||||||
|
Move,
|
||||||
|
const Move*);
|
||||||
MovePicker(const Position&, Move, Depth, const ButterflyHistory*,
|
MovePicker(const Position&, Move, Depth, const ButterflyHistory*,
|
||||||
const CapturePieceToHistory*,
|
const CapturePieceToHistory*,
|
||||||
const PieceToHistory**,
|
const PieceToHistory**,
|
||||||
Square);
|
Square);
|
||||||
MovePicker(const Position&, Move, Depth, const ButterflyHistory*,
|
MovePicker(const Position&, Move, Value, const CapturePieceToHistory*);
|
||||||
const LowPlyHistory*,
|
|
||||||
const CapturePieceToHistory*,
|
|
||||||
const PieceToHistory**,
|
|
||||||
Move,
|
|
||||||
const Move*,
|
|
||||||
int);
|
|
||||||
Move next_move(bool skipQuiets = false);
|
Move next_move(bool skipQuiets = false);
|
||||||
|
|
||||||
|
Bitboard threatenedPieces;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template<PickType T, typename Pred> Move select(Pred);
|
template<PickType T, typename Pred> Move select(Pred);
|
||||||
template<GenType> void score();
|
template<GenType> void score();
|
||||||
@@ -143,7 +141,6 @@ private:
|
|||||||
|
|
||||||
const Position& pos;
|
const Position& pos;
|
||||||
const ButterflyHistory* mainHistory;
|
const ButterflyHistory* mainHistory;
|
||||||
const LowPlyHistory* lowPlyHistory;
|
|
||||||
const CapturePieceToHistory* captureHistory;
|
const CapturePieceToHistory* captureHistory;
|
||||||
const PieceToHistory** continuationHistory;
|
const PieceToHistory** continuationHistory;
|
||||||
Move ttMove;
|
Move ttMove;
|
||||||
@@ -152,8 +149,9 @@ private:
|
|||||||
Square recaptureSquare;
|
Square recaptureSquare;
|
||||||
Value threshold;
|
Value threshold;
|
||||||
Depth depth;
|
Depth depth;
|
||||||
int ply;
|
|
||||||
ExtMove moves[MAX_MOVES];
|
ExtMove moves[MAX_MOVES];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef MOVEPICK_H_INCLUDED
|
#endif // #ifndef MOVEPICK_H_INCLUDED
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
|
||||||
|
|
||||||
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/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Definition of input features and network structure used in NNUE evaluation function
|
|
||||||
|
|
||||||
#ifndef NNUE_HALFKP_256X2_32_32_H_INCLUDED
|
|
||||||
#define NNUE_HALFKP_256X2_32_32_H_INCLUDED
|
|
||||||
|
|
||||||
#include "../features/feature_set.h"
|
|
||||||
#include "../features/half_kp.h"
|
|
||||||
|
|
||||||
#include "../layers/input_slice.h"
|
|
||||||
#include "../layers/affine_transform.h"
|
|
||||||
#include "../layers/clipped_relu.h"
|
|
||||||
|
|
||||||
namespace Eval::NNUE {
|
|
||||||
|
|
||||||
// Input features used in evaluation function
|
|
||||||
using RawFeatures = Features::FeatureSet<
|
|
||||||
Features::HalfKP<Features::Side::kFriend>>;
|
|
||||||
|
|
||||||
// Number of input feature dimensions after conversion
|
|
||||||
constexpr IndexType kTransformedFeatureDimensions = 256;
|
|
||||||
|
|
||||||
namespace Layers {
|
|
||||||
|
|
||||||
// Define network structure
|
|
||||||
using InputLayer = InputSlice<kTransformedFeatureDimensions * 2>;
|
|
||||||
using HiddenLayer1 = ClippedReLU<AffineTransform<InputLayer, 32>>;
|
|
||||||
using HiddenLayer2 = ClippedReLU<AffineTransform<HiddenLayer1, 32>>;
|
|
||||||
using OutputLayer = AffineTransform<HiddenLayer2, 1>;
|
|
||||||
|
|
||||||
} // namespace Layers
|
|
||||||
|
|
||||||
using Network = Layers::OutputLayer;
|
|
||||||
|
|
||||||
} // namespace Eval::NNUE
|
|
||||||
|
|
||||||
#endif // #ifndef NNUE_HALFKP_256X2_32_32_H_INCLUDED
|
|
||||||
+303
-41
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -20,6 +20,9 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include "../evaluate.h"
|
#include "../evaluate.h"
|
||||||
#include "../position.h"
|
#include "../position.h"
|
||||||
@@ -29,29 +32,30 @@
|
|||||||
|
|
||||||
#include "evaluate_nnue.h"
|
#include "evaluate_nnue.h"
|
||||||
|
|
||||||
namespace Eval::NNUE {
|
namespace Stockfish::Eval::NNUE {
|
||||||
|
|
||||||
// Input feature converter
|
// Input feature converter
|
||||||
LargePagePtr<FeatureTransformer> feature_transformer;
|
LargePagePtr<FeatureTransformer> featureTransformer;
|
||||||
|
|
||||||
// Evaluation function
|
// Evaluation function
|
||||||
AlignedPtr<Network> network;
|
AlignedPtr<Network> network[LayerStacks];
|
||||||
|
|
||||||
// Evaluation function file name
|
// Evaluation function file name
|
||||||
std::string fileName;
|
std::string fileName;
|
||||||
|
std::string netDescription;
|
||||||
|
|
||||||
namespace Detail {
|
namespace Detail {
|
||||||
|
|
||||||
// Initialize the evaluation function parameters
|
// Initialize the evaluation function parameters
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void Initialize(AlignedPtr<T>& pointer) {
|
void initialize(AlignedPtr<T>& pointer) {
|
||||||
|
|
||||||
pointer.reset(reinterpret_cast<T*>(std_aligned_alloc(alignof(T), sizeof(T))));
|
pointer.reset(reinterpret_cast<T*>(std_aligned_alloc(alignof(T), sizeof(T))));
|
||||||
std::memset(pointer.get(), 0, sizeof(T));
|
std::memset(pointer.get(), 0, sizeof(T));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void Initialize(LargePagePtr<T>& pointer) {
|
void initialize(LargePagePtr<T>& pointer) {
|
||||||
|
|
||||||
static_assert(alignof(T) <= 4096, "aligned_large_pages_alloc() may fail for such a big alignment requirement of T");
|
static_assert(alignof(T) <= 4096, "aligned_large_pages_alloc() may fail for such a big alignment requirement of T");
|
||||||
pointer.reset(reinterpret_cast<T*>(aligned_large_pages_alloc(sizeof(T))));
|
pointer.reset(reinterpret_cast<T*>(aligned_large_pages_alloc(sizeof(T))));
|
||||||
@@ -60,85 +64,343 @@ namespace Eval::NNUE {
|
|||||||
|
|
||||||
// Read evaluation function parameters
|
// Read evaluation function parameters
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool ReadParameters(std::istream& stream, T& reference) {
|
bool read_parameters(std::istream& stream, T& reference) {
|
||||||
|
|
||||||
std::uint32_t header;
|
std::uint32_t header;
|
||||||
header = read_little_endian<std::uint32_t>(stream);
|
header = read_little_endian<std::uint32_t>(stream);
|
||||||
if (!stream || header != T::GetHashValue()) return false;
|
if (!stream || header != T::get_hash_value()) return false;
|
||||||
return reference.ReadParameters(stream);
|
return reference.read_parameters(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write evaluation function parameters
|
||||||
|
template <typename T>
|
||||||
|
bool write_parameters(std::ostream& stream, const T& reference) {
|
||||||
|
|
||||||
|
write_little_endian<std::uint32_t>(stream, T::get_hash_value());
|
||||||
|
return reference.write_parameters(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Detail
|
} // namespace Detail
|
||||||
|
|
||||||
// Initialize the evaluation function parameters
|
// Initialize the evaluation function parameters
|
||||||
void Initialize() {
|
void initialize() {
|
||||||
|
|
||||||
Detail::Initialize(feature_transformer);
|
Detail::initialize(featureTransformer);
|
||||||
Detail::Initialize(network);
|
for (std::size_t i = 0; i < LayerStacks; ++i)
|
||||||
|
Detail::initialize(network[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read network header
|
// Read network header
|
||||||
bool ReadHeader(std::istream& stream, std::uint32_t* hash_value, std::string* architecture)
|
bool read_header(std::istream& stream, std::uint32_t* hashValue, std::string* desc)
|
||||||
{
|
{
|
||||||
std::uint32_t version, size;
|
std::uint32_t version, size;
|
||||||
|
|
||||||
version = read_little_endian<std::uint32_t>(stream);
|
version = read_little_endian<std::uint32_t>(stream);
|
||||||
*hash_value = read_little_endian<std::uint32_t>(stream);
|
*hashValue = read_little_endian<std::uint32_t>(stream);
|
||||||
size = read_little_endian<std::uint32_t>(stream);
|
size = read_little_endian<std::uint32_t>(stream);
|
||||||
if (!stream || version != kVersion) return false;
|
if (!stream || version != Version) return false;
|
||||||
architecture->resize(size);
|
desc->resize(size);
|
||||||
stream.read(&(*architecture)[0], size);
|
stream.read(&(*desc)[0], size);
|
||||||
|
return !stream.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write network header
|
||||||
|
bool write_header(std::ostream& stream, std::uint32_t hashValue, const std::string& desc)
|
||||||
|
{
|
||||||
|
write_little_endian<std::uint32_t>(stream, Version);
|
||||||
|
write_little_endian<std::uint32_t>(stream, hashValue);
|
||||||
|
write_little_endian<std::uint32_t>(stream, (std::uint32_t)desc.size());
|
||||||
|
stream.write(&desc[0], desc.size());
|
||||||
return !stream.fail();
|
return !stream.fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read network parameters
|
// Read network parameters
|
||||||
bool ReadParameters(std::istream& stream) {
|
bool read_parameters(std::istream& stream) {
|
||||||
|
|
||||||
std::uint32_t hash_value;
|
std::uint32_t hashValue;
|
||||||
std::string architecture;
|
if (!read_header(stream, &hashValue, &netDescription)) return false;
|
||||||
if (!ReadHeader(stream, &hash_value, &architecture)) return false;
|
if (hashValue != HashValue) return false;
|
||||||
if (hash_value != kHashValue) return false;
|
if (!Detail::read_parameters(stream, *featureTransformer)) return false;
|
||||||
if (!Detail::ReadParameters(stream, *feature_transformer)) return false;
|
for (std::size_t i = 0; i < LayerStacks; ++i)
|
||||||
if (!Detail::ReadParameters(stream, *network)) return false;
|
if (!Detail::read_parameters(stream, *(network[i]))) return false;
|
||||||
return stream && stream.peek() == std::ios::traits_type::eof();
|
return stream && stream.peek() == std::ios::traits_type::eof();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write network parameters
|
||||||
|
bool write_parameters(std::ostream& stream) {
|
||||||
|
|
||||||
|
if (!write_header(stream, HashValue, netDescription)) return false;
|
||||||
|
if (!Detail::write_parameters(stream, *featureTransformer)) return false;
|
||||||
|
for (std::size_t i = 0; i < LayerStacks; ++i)
|
||||||
|
if (!Detail::write_parameters(stream, *(network[i]))) return false;
|
||||||
|
return (bool)stream;
|
||||||
|
}
|
||||||
|
|
||||||
// Evaluation function. Perform differential calculation.
|
// Evaluation function. Perform differential calculation.
|
||||||
Value evaluate(const Position& pos) {
|
Value evaluate(const Position& pos, bool adjusted, int* complexity) {
|
||||||
|
|
||||||
// We manually align the arrays on the stack because with gcc < 9.3
|
// We manually align the arrays on the stack because with gcc < 9.3
|
||||||
// overaligning stack variables with alignas() doesn't work correctly.
|
// overaligning stack variables with alignas() doesn't work correctly.
|
||||||
|
|
||||||
constexpr uint64_t alignment = kCacheLineSize;
|
constexpr uint64_t alignment = CacheLineSize;
|
||||||
|
int delta = 24 - pos.non_pawn_material() / 9560;
|
||||||
|
|
||||||
#if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
|
#if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
|
||||||
TransformedFeatureType transformed_features_unaligned[
|
TransformedFeatureType transformedFeaturesUnaligned[
|
||||||
FeatureTransformer::kBufferSize + alignment / sizeof(TransformedFeatureType)];
|
FeatureTransformer::BufferSize + alignment / sizeof(TransformedFeatureType)];
|
||||||
char buffer_unaligned[Network::kBufferSize + alignment];
|
|
||||||
|
|
||||||
auto* transformed_features = align_ptr_up<alignment>(&transformed_features_unaligned[0]);
|
auto* transformedFeatures = align_ptr_up<alignment>(&transformedFeaturesUnaligned[0]);
|
||||||
auto* buffer = align_ptr_up<alignment>(&buffer_unaligned[0]);
|
|
||||||
#else
|
#else
|
||||||
alignas(alignment)
|
alignas(alignment)
|
||||||
TransformedFeatureType transformed_features[FeatureTransformer::kBufferSize];
|
TransformedFeatureType transformedFeatures[FeatureTransformer::BufferSize];
|
||||||
alignas(alignment) char buffer[Network::kBufferSize];
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ASSERT_ALIGNED(transformed_features, alignment);
|
ASSERT_ALIGNED(transformedFeatures, alignment);
|
||||||
ASSERT_ALIGNED(buffer, alignment);
|
|
||||||
|
|
||||||
feature_transformer->Transform(pos, transformed_features);
|
const int bucket = (pos.count<ALL_PIECES>() - 1) / 4;
|
||||||
const auto output = network->Propagate(transformed_features, buffer);
|
const auto psqt = featureTransformer->transform(pos, transformedFeatures, bucket);
|
||||||
|
const auto positional = network[bucket]->propagate(transformedFeatures);
|
||||||
|
|
||||||
return static_cast<Value>(output[0] / FV_SCALE);
|
if (complexity)
|
||||||
|
*complexity = abs(psqt - positional) / OutputScale;
|
||||||
|
|
||||||
|
// Give more value to positional evaluation when adjusted flag is set
|
||||||
|
if (adjusted)
|
||||||
|
return static_cast<Value>(((1024 - delta) * psqt + (1024 + delta) * positional) / (1024 * OutputScale));
|
||||||
|
else
|
||||||
|
return static_cast<Value>((psqt + positional) / OutputScale);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct NnueEvalTrace {
|
||||||
|
static_assert(LayerStacks == PSQTBuckets);
|
||||||
|
|
||||||
|
Value psqt[LayerStacks];
|
||||||
|
Value positional[LayerStacks];
|
||||||
|
std::size_t correctBucket;
|
||||||
|
};
|
||||||
|
|
||||||
|
static NnueEvalTrace trace_evaluate(const Position& pos) {
|
||||||
|
|
||||||
|
// We manually align the arrays on the stack because with gcc < 9.3
|
||||||
|
// overaligning stack variables with alignas() doesn't work correctly.
|
||||||
|
|
||||||
|
constexpr uint64_t alignment = CacheLineSize;
|
||||||
|
|
||||||
|
#if defined(ALIGNAS_ON_STACK_VARIABLES_BROKEN)
|
||||||
|
TransformedFeatureType transformedFeaturesUnaligned[
|
||||||
|
FeatureTransformer::BufferSize + alignment / sizeof(TransformedFeatureType)];
|
||||||
|
|
||||||
|
auto* transformedFeatures = align_ptr_up<alignment>(&transformedFeaturesUnaligned[0]);
|
||||||
|
#else
|
||||||
|
alignas(alignment)
|
||||||
|
TransformedFeatureType transformedFeatures[FeatureTransformer::BufferSize];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ASSERT_ALIGNED(transformedFeatures, alignment);
|
||||||
|
|
||||||
|
NnueEvalTrace t{};
|
||||||
|
t.correctBucket = (pos.count<ALL_PIECES>() - 1) / 4;
|
||||||
|
for (IndexType bucket = 0; bucket < LayerStacks; ++bucket) {
|
||||||
|
const auto materialist = featureTransformer->transform(pos, transformedFeatures, bucket);
|
||||||
|
const auto positional = network[bucket]->propagate(transformedFeatures);
|
||||||
|
|
||||||
|
t.psqt[bucket] = static_cast<Value>( materialist / OutputScale );
|
||||||
|
t.positional[bucket] = static_cast<Value>( positional / OutputScale );
|
||||||
|
}
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const std::string PieceToChar(" PNBRQK pnbrqk");
|
||||||
|
|
||||||
|
|
||||||
|
// format_cp_compact() converts a Value into (centi)pawns and writes it in a buffer.
|
||||||
|
// The buffer must have capacity for at least 5 chars.
|
||||||
|
static void format_cp_compact(Value v, char* buffer) {
|
||||||
|
|
||||||
|
buffer[0] = (v < 0 ? '-' : v > 0 ? '+' : ' ');
|
||||||
|
|
||||||
|
int cp = std::abs(100 * v / UCI::NormalizeToPawnValue);
|
||||||
|
if (cp >= 10000)
|
||||||
|
{
|
||||||
|
buffer[1] = '0' + cp / 10000; cp %= 10000;
|
||||||
|
buffer[2] = '0' + cp / 1000; cp %= 1000;
|
||||||
|
buffer[3] = '0' + cp / 100;
|
||||||
|
buffer[4] = ' ';
|
||||||
|
}
|
||||||
|
else if (cp >= 1000)
|
||||||
|
{
|
||||||
|
buffer[1] = '0' + cp / 1000; cp %= 1000;
|
||||||
|
buffer[2] = '0' + cp / 100; cp %= 100;
|
||||||
|
buffer[3] = '.';
|
||||||
|
buffer[4] = '0' + cp / 10;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
buffer[1] = '0' + cp / 100; cp %= 100;
|
||||||
|
buffer[2] = '.';
|
||||||
|
buffer[3] = '0' + cp / 10; cp %= 10;
|
||||||
|
buffer[4] = '0' + cp / 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// format_cp_aligned_dot() converts a Value into (centi)pawns and writes it in a buffer,
|
||||||
|
// always keeping two decimals. The buffer must have capacity for at least 7 chars.
|
||||||
|
static void format_cp_aligned_dot(Value v, char* buffer) {
|
||||||
|
|
||||||
|
buffer[0] = (v < 0 ? '-' : v > 0 ? '+' : ' ');
|
||||||
|
|
||||||
|
double cp = 1.0 * std::abs(int(v)) / UCI::NormalizeToPawnValue;
|
||||||
|
sprintf(&buffer[1], "%6.2f", cp);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// trace() returns a string with the value of each piece on a board,
|
||||||
|
// and a table for (PSQT, Layers) values bucket by bucket.
|
||||||
|
|
||||||
|
std::string trace(Position& pos) {
|
||||||
|
|
||||||
|
std::stringstream ss;
|
||||||
|
|
||||||
|
char board[3*8+1][8*8+2];
|
||||||
|
std::memset(board, ' ', sizeof(board));
|
||||||
|
for (int row = 0; row < 3*8+1; ++row)
|
||||||
|
board[row][8*8+1] = '\0';
|
||||||
|
|
||||||
|
// A lambda to output one box of the board
|
||||||
|
auto writeSquare = [&board](File file, Rank rank, Piece pc, Value value) {
|
||||||
|
|
||||||
|
const int x = ((int)file) * 8;
|
||||||
|
const int y = (7 - (int)rank) * 3;
|
||||||
|
for (int i = 1; i < 8; ++i)
|
||||||
|
board[y][x+i] = board[y+3][x+i] = '-';
|
||||||
|
for (int i = 1; i < 3; ++i)
|
||||||
|
board[y+i][x] = board[y+i][x+8] = '|';
|
||||||
|
board[y][x] = board[y][x+8] = board[y+3][x+8] = board[y+3][x] = '+';
|
||||||
|
if (pc != NO_PIECE)
|
||||||
|
board[y+1][x+4] = PieceToChar[pc];
|
||||||
|
if (value != VALUE_NONE)
|
||||||
|
format_cp_compact(value, &board[y+2][x+2]);
|
||||||
|
};
|
||||||
|
|
||||||
|
// We estimate the value of each piece by doing a differential evaluation from
|
||||||
|
// the current base eval, simulating the removal of the piece from its square.
|
||||||
|
Value base = evaluate(pos);
|
||||||
|
base = pos.side_to_move() == WHITE ? base : -base;
|
||||||
|
|
||||||
|
for (File f = FILE_A; f <= FILE_H; ++f)
|
||||||
|
for (Rank r = RANK_1; r <= RANK_8; ++r)
|
||||||
|
{
|
||||||
|
Square sq = make_square(f, r);
|
||||||
|
Piece pc = pos.piece_on(sq);
|
||||||
|
Value v = VALUE_NONE;
|
||||||
|
|
||||||
|
if (pc != NO_PIECE && type_of(pc) != KING)
|
||||||
|
{
|
||||||
|
auto st = pos.state();
|
||||||
|
|
||||||
|
pos.remove_piece(sq);
|
||||||
|
st->accumulator.computed[WHITE] = false;
|
||||||
|
st->accumulator.computed[BLACK] = false;
|
||||||
|
|
||||||
|
Value eval = evaluate(pos);
|
||||||
|
eval = pos.side_to_move() == WHITE ? eval : -eval;
|
||||||
|
v = base - eval;
|
||||||
|
|
||||||
|
pos.put_piece(pc, sq);
|
||||||
|
st->accumulator.computed[WHITE] = false;
|
||||||
|
st->accumulator.computed[BLACK] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeSquare(f, r, pc, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
ss << " NNUE derived piece values:\n";
|
||||||
|
for (int row = 0; row < 3*8+1; ++row)
|
||||||
|
ss << board[row] << '\n';
|
||||||
|
ss << '\n';
|
||||||
|
|
||||||
|
auto t = trace_evaluate(pos);
|
||||||
|
|
||||||
|
ss << " NNUE network contributions "
|
||||||
|
<< (pos.side_to_move() == WHITE ? "(White to move)" : "(Black to move)") << std::endl
|
||||||
|
<< "+------------+------------+------------+------------+\n"
|
||||||
|
<< "| Bucket | Material | Positional | Total |\n"
|
||||||
|
<< "| | (PSQT) | (Layers) | |\n"
|
||||||
|
<< "+------------+------------+------------+------------+\n";
|
||||||
|
|
||||||
|
for (std::size_t bucket = 0; bucket < LayerStacks; ++bucket)
|
||||||
|
{
|
||||||
|
char buffer[3][8];
|
||||||
|
std::memset(buffer, '\0', sizeof(buffer));
|
||||||
|
|
||||||
|
format_cp_aligned_dot(t.psqt[bucket], buffer[0]);
|
||||||
|
format_cp_aligned_dot(t.positional[bucket], buffer[1]);
|
||||||
|
format_cp_aligned_dot(t.psqt[bucket] + t.positional[bucket], buffer[2]);
|
||||||
|
|
||||||
|
ss << "| " << bucket << " "
|
||||||
|
<< " | " << buffer[0] << " "
|
||||||
|
<< " | " << buffer[1] << " "
|
||||||
|
<< " | " << buffer[2] << " "
|
||||||
|
<< " |";
|
||||||
|
if (bucket == t.correctBucket)
|
||||||
|
ss << " <-- this bucket is used";
|
||||||
|
ss << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
ss << "+------------+------------+------------+------------+\n";
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Load eval, from a file stream or a memory stream
|
// Load eval, from a file stream or a memory stream
|
||||||
bool load_eval(std::string name, std::istream& stream) {
|
bool load_eval(std::string name, std::istream& stream) {
|
||||||
|
|
||||||
Initialize();
|
initialize();
|
||||||
fileName = name;
|
fileName = name;
|
||||||
return ReadParameters(stream);
|
return read_parameters(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Eval::NNUE
|
// Save eval, to a file stream or a memory stream
|
||||||
|
bool save_eval(std::ostream& stream) {
|
||||||
|
|
||||||
|
if (fileName.empty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return write_parameters(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Save eval, to a file given by its name
|
||||||
|
bool save_eval(const std::optional<std::string>& filename) {
|
||||||
|
|
||||||
|
std::string actualFilename;
|
||||||
|
std::string msg;
|
||||||
|
|
||||||
|
if (filename.has_value())
|
||||||
|
actualFilename = filename.value();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (currentEvalFileName != EvalFileDefaultName)
|
||||||
|
{
|
||||||
|
msg = "Failed to export a net. A non-embedded net can only be saved if the filename is specified";
|
||||||
|
|
||||||
|
sync_cout << msg << sync_endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
actualFilename = EvalFileDefaultName;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ofstream stream(actualFilename, std::ios_base::binary);
|
||||||
|
bool saved = save_eval(stream);
|
||||||
|
|
||||||
|
msg = saved ? "Network saved successfully to " + actualFilename
|
||||||
|
: "Failed to export a net";
|
||||||
|
|
||||||
|
sync_cout << msg << sync_endl;
|
||||||
|
return saved;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace Stockfish::Eval::NNUE
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -25,11 +25,11 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace Eval::NNUE {
|
namespace Stockfish::Eval::NNUE {
|
||||||
|
|
||||||
// Hash value of evaluation function structure
|
// Hash value of evaluation function structure
|
||||||
constexpr std::uint32_t kHashValue =
|
constexpr std::uint32_t HashValue =
|
||||||
FeatureTransformer::GetHashValue() ^ Network::GetHashValue();
|
FeatureTransformer::get_hash_value() ^ Network::get_hash_value();
|
||||||
|
|
||||||
// Deleter for automating release of memory area
|
// Deleter for automating release of memory area
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -54,6 +54,6 @@ namespace Eval::NNUE {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
using LargePagePtr = std::unique_ptr<T, LargePageDeleter<T>>;
|
using LargePagePtr = std::unique_ptr<T, LargePageDeleter<T>>;
|
||||||
|
|
||||||
} // namespace Eval::NNUE
|
} // namespace Stockfish::Eval::NNUE
|
||||||
|
|
||||||
#endif // #ifndef NNUE_EVALUATE_NNUE_H_INCLUDED
|
#endif // #ifndef NNUE_EVALUATE_NNUE_H_INCLUDED
|
||||||
|
|||||||
@@ -1,69 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
|
||||||
|
|
||||||
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/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// A class template that represents the input feature set of the NNUE evaluation function
|
|
||||||
|
|
||||||
#ifndef NNUE_FEATURE_SET_H_INCLUDED
|
|
||||||
#define NNUE_FEATURE_SET_H_INCLUDED
|
|
||||||
|
|
||||||
#include "features_common.h"
|
|
||||||
#include <array>
|
|
||||||
|
|
||||||
namespace Eval::NNUE::Features {
|
|
||||||
|
|
||||||
// Class template that represents a list of values
|
|
||||||
template <typename T, T... Values>
|
|
||||||
struct CompileTimeList;
|
|
||||||
|
|
||||||
template <typename T, T First, T... Remaining>
|
|
||||||
struct CompileTimeList<T, First, Remaining...> {
|
|
||||||
static constexpr bool Contains(T value) {
|
|
||||||
return value == First || CompileTimeList<T, Remaining...>::Contains(value);
|
|
||||||
}
|
|
||||||
static constexpr std::array<T, sizeof...(Remaining) + 1>
|
|
||||||
kValues = {{First, Remaining...}};
|
|
||||||
};
|
|
||||||
|
|
||||||
// Base class of feature set
|
|
||||||
template <typename Derived>
|
|
||||||
class FeatureSetBase {
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
// Class template that represents the feature set
|
|
||||||
template <typename FeatureType>
|
|
||||||
class FeatureSet<FeatureType> : public FeatureSetBase<FeatureSet<FeatureType>> {
|
|
||||||
|
|
||||||
public:
|
|
||||||
// Hash value embedded in the evaluation file
|
|
||||||
static constexpr std::uint32_t kHashValue = FeatureType::kHashValue;
|
|
||||||
// Number of feature dimensions
|
|
||||||
static constexpr IndexType kDimensions = FeatureType::kDimensions;
|
|
||||||
// Maximum number of simultaneously active features
|
|
||||||
static constexpr IndexType kMaxActiveDimensions =
|
|
||||||
FeatureType::kMaxActiveDimensions;
|
|
||||||
// Trigger for full calculation instead of difference calculation
|
|
||||||
using SortedTriggerSet =
|
|
||||||
CompileTimeList<TriggerEvent, FeatureType::kRefreshTrigger>;
|
|
||||||
static constexpr auto kRefreshTriggers = SortedTriggerSet::kValues;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Eval::NNUE::Features
|
|
||||||
|
|
||||||
#endif // #ifndef NNUE_FEATURE_SET_H_INCLUDED
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
|
||||||
|
|
||||||
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/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
//Common header of input features of NNUE evaluation function
|
|
||||||
|
|
||||||
#ifndef NNUE_FEATURES_COMMON_H_INCLUDED
|
|
||||||
#define NNUE_FEATURES_COMMON_H_INCLUDED
|
|
||||||
|
|
||||||
#include "../../evaluate.h"
|
|
||||||
#include "../nnue_common.h"
|
|
||||||
|
|
||||||
namespace Eval::NNUE::Features {
|
|
||||||
|
|
||||||
class IndexList;
|
|
||||||
|
|
||||||
template <typename... FeatureTypes>
|
|
||||||
class FeatureSet;
|
|
||||||
|
|
||||||
// Trigger to perform full calculations instead of difference only
|
|
||||||
enum class TriggerEvent {
|
|
||||||
kFriendKingMoved // calculate full evaluation when own king moves
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class Side {
|
|
||||||
kFriend // side to move
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Eval::NNUE::Features
|
|
||||||
|
|
||||||
#endif // #ifndef NNUE_FEATURES_COMMON_H_INCLUDED
|
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//Definition of input features HalfKAv2_hm of NNUE evaluation function
|
||||||
|
|
||||||
|
#include "half_ka_v2_hm.h"
|
||||||
|
|
||||||
|
#include "../../position.h"
|
||||||
|
|
||||||
|
namespace Stockfish::Eval::NNUE::Features {
|
||||||
|
|
||||||
|
// Index of a feature for a given king position and another piece on some square
|
||||||
|
template<Color Perspective>
|
||||||
|
inline IndexType HalfKAv2_hm::make_index(Square s, Piece pc, Square ksq) {
|
||||||
|
return IndexType((int(s) ^ OrientTBL[Perspective][ksq]) + PieceSquareIndex[Perspective][pc] + KingBuckets[Perspective][ksq]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a list of indices for active features
|
||||||
|
template<Color Perspective>
|
||||||
|
void HalfKAv2_hm::append_active_indices(
|
||||||
|
const Position& pos,
|
||||||
|
IndexList& active
|
||||||
|
) {
|
||||||
|
Square ksq = pos.square<KING>(Perspective);
|
||||||
|
Bitboard bb = pos.pieces();
|
||||||
|
while (bb)
|
||||||
|
{
|
||||||
|
Square s = pop_lsb(bb);
|
||||||
|
active.push_back(make_index<Perspective>(s, pos.piece_on(s), ksq));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explicit template instantiations
|
||||||
|
template void HalfKAv2_hm::append_active_indices<WHITE>(const Position& pos, IndexList& active);
|
||||||
|
template void HalfKAv2_hm::append_active_indices<BLACK>(const Position& pos, IndexList& active);
|
||||||
|
|
||||||
|
// append_changed_indices() : get a list of indices for recently changed features
|
||||||
|
template<Color Perspective>
|
||||||
|
void HalfKAv2_hm::append_changed_indices(
|
||||||
|
Square ksq,
|
||||||
|
const DirtyPiece& dp,
|
||||||
|
IndexList& removed,
|
||||||
|
IndexList& added
|
||||||
|
) {
|
||||||
|
for (int i = 0; i < dp.dirty_num; ++i) {
|
||||||
|
if (dp.from[i] != SQ_NONE)
|
||||||
|
removed.push_back(make_index<Perspective>(dp.from[i], dp.piece[i], ksq));
|
||||||
|
if (dp.to[i] != SQ_NONE)
|
||||||
|
added.push_back(make_index<Perspective>(dp.to[i], dp.piece[i], ksq));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explicit template instantiations
|
||||||
|
template void HalfKAv2_hm::append_changed_indices<WHITE>(Square ksq, const DirtyPiece& dp, IndexList& removed, IndexList& added);
|
||||||
|
template void HalfKAv2_hm::append_changed_indices<BLACK>(Square ksq, const DirtyPiece& dp, IndexList& removed, IndexList& added);
|
||||||
|
|
||||||
|
int HalfKAv2_hm::update_cost(const StateInfo* st) {
|
||||||
|
return st->dirtyPiece.dirty_num;
|
||||||
|
}
|
||||||
|
|
||||||
|
int HalfKAv2_hm::refresh_cost(const Position& pos) {
|
||||||
|
return pos.count<ALL_PIECES>();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HalfKAv2_hm::requires_refresh(const StateInfo* st, Color perspective) {
|
||||||
|
return st->dirtyPiece.piece[0] == make_piece(perspective, KING);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish::Eval::NNUE::Features
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
/*
|
||||||
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//Definition of input features HalfKP of NNUE evaluation function
|
||||||
|
|
||||||
|
#ifndef NNUE_FEATURES_HALF_KA_V2_HM_H_INCLUDED
|
||||||
|
#define NNUE_FEATURES_HALF_KA_V2_HM_H_INCLUDED
|
||||||
|
|
||||||
|
#include "../nnue_common.h"
|
||||||
|
|
||||||
|
#include "../../evaluate.h"
|
||||||
|
#include "../../misc.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
struct StateInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Stockfish::Eval::NNUE::Features {
|
||||||
|
|
||||||
|
// Feature HalfKAv2_hm: Combination of the position of own king
|
||||||
|
// and the position of pieces. Position mirrored such that king always on e..h files.
|
||||||
|
class HalfKAv2_hm {
|
||||||
|
|
||||||
|
// unique number for each piece type on each square
|
||||||
|
enum {
|
||||||
|
PS_NONE = 0,
|
||||||
|
PS_W_PAWN = 0,
|
||||||
|
PS_B_PAWN = 1 * SQUARE_NB,
|
||||||
|
PS_W_KNIGHT = 2 * SQUARE_NB,
|
||||||
|
PS_B_KNIGHT = 3 * SQUARE_NB,
|
||||||
|
PS_W_BISHOP = 4 * SQUARE_NB,
|
||||||
|
PS_B_BISHOP = 5 * SQUARE_NB,
|
||||||
|
PS_W_ROOK = 6 * SQUARE_NB,
|
||||||
|
PS_B_ROOK = 7 * SQUARE_NB,
|
||||||
|
PS_W_QUEEN = 8 * SQUARE_NB,
|
||||||
|
PS_B_QUEEN = 9 * SQUARE_NB,
|
||||||
|
PS_KING = 10 * SQUARE_NB,
|
||||||
|
PS_NB = 11 * SQUARE_NB
|
||||||
|
};
|
||||||
|
|
||||||
|
static constexpr IndexType PieceSquareIndex[COLOR_NB][PIECE_NB] = {
|
||||||
|
// convention: W - us, B - them
|
||||||
|
// viewed from other side, W and B are reversed
|
||||||
|
{ PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_KING, PS_NONE,
|
||||||
|
PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_KING, PS_NONE },
|
||||||
|
{ PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_KING, PS_NONE,
|
||||||
|
PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_KING, PS_NONE }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Index of a feature for a given king position and another piece on some square
|
||||||
|
template<Color Perspective>
|
||||||
|
static IndexType make_index(Square s, Piece pc, Square ksq);
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Feature name
|
||||||
|
static constexpr const char* Name = "HalfKAv2_hm(Friend)";
|
||||||
|
|
||||||
|
// Hash value embedded in the evaluation file
|
||||||
|
static constexpr std::uint32_t HashValue = 0x7f234cb8u;
|
||||||
|
|
||||||
|
// Number of feature dimensions
|
||||||
|
static constexpr IndexType Dimensions =
|
||||||
|
static_cast<IndexType>(SQUARE_NB) * static_cast<IndexType>(PS_NB) / 2;
|
||||||
|
|
||||||
|
#define B(v) (v * PS_NB)
|
||||||
|
static constexpr int KingBuckets[COLOR_NB][SQUARE_NB] = {
|
||||||
|
{ B(28), B(29), B(30), B(31), B(31), B(30), B(29), B(28),
|
||||||
|
B(24), B(25), B(26), B(27), B(27), B(26), B(25), B(24),
|
||||||
|
B(20), B(21), B(22), B(23), B(23), B(22), B(21), B(20),
|
||||||
|
B(16), B(17), B(18), B(19), B(19), B(18), B(17), B(16),
|
||||||
|
B(12), B(13), B(14), B(15), B(15), B(14), B(13), B(12),
|
||||||
|
B( 8), B( 9), B(10), B(11), B(11), B(10), B( 9), B( 8),
|
||||||
|
B( 4), B( 5), B( 6), B( 7), B( 7), B( 6), B( 5), B( 4),
|
||||||
|
B( 0), B( 1), B( 2), B( 3), B( 3), B( 2), B( 1), B( 0) },
|
||||||
|
{ B( 0), B( 1), B( 2), B( 3), B( 3), B( 2), B( 1), B( 0),
|
||||||
|
B( 4), B( 5), B( 6), B( 7), B( 7), B( 6), B( 5), B( 4),
|
||||||
|
B( 8), B( 9), B(10), B(11), B(11), B(10), B( 9), B( 8),
|
||||||
|
B(12), B(13), B(14), B(15), B(15), B(14), B(13), B(12),
|
||||||
|
B(16), B(17), B(18), B(19), B(19), B(18), B(17), B(16),
|
||||||
|
B(20), B(21), B(22), B(23), B(23), B(22), B(21), B(20),
|
||||||
|
B(24), B(25), B(26), B(27), B(27), B(26), B(25), B(24),
|
||||||
|
B(28), B(29), B(30), B(31), B(31), B(30), B(29), B(28) }
|
||||||
|
};
|
||||||
|
#undef B
|
||||||
|
|
||||||
|
// Orient a square according to perspective (rotates by 180 for black)
|
||||||
|
static constexpr int OrientTBL[COLOR_NB][SQUARE_NB] = {
|
||||||
|
{ SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
|
||||||
|
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
|
||||||
|
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
|
||||||
|
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
|
||||||
|
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
|
||||||
|
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
|
||||||
|
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1,
|
||||||
|
SQ_H1, SQ_H1, SQ_H1, SQ_H1, SQ_A1, SQ_A1, SQ_A1, SQ_A1 },
|
||||||
|
{ SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
|
||||||
|
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
|
||||||
|
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
|
||||||
|
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
|
||||||
|
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
|
||||||
|
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
|
||||||
|
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8,
|
||||||
|
SQ_H8, SQ_H8, SQ_H8, SQ_H8, SQ_A8, SQ_A8, SQ_A8, SQ_A8 }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Maximum number of simultaneously active features.
|
||||||
|
static constexpr IndexType MaxActiveDimensions = 32;
|
||||||
|
using IndexList = ValueList<IndexType, MaxActiveDimensions>;
|
||||||
|
|
||||||
|
// Get a list of indices for active features
|
||||||
|
template<Color Perspective>
|
||||||
|
static void append_active_indices(
|
||||||
|
const Position& pos,
|
||||||
|
IndexList& active);
|
||||||
|
|
||||||
|
// Get a list of indices for recently changed features
|
||||||
|
template<Color Perspective>
|
||||||
|
static void append_changed_indices(
|
||||||
|
Square ksq,
|
||||||
|
const DirtyPiece& dp,
|
||||||
|
IndexList& removed,
|
||||||
|
IndexList& added
|
||||||
|
);
|
||||||
|
|
||||||
|
// Returns the cost of updating one perspective, the most costly one.
|
||||||
|
// Assumes no refresh needed.
|
||||||
|
static int update_cost(const StateInfo* st);
|
||||||
|
static int refresh_cost(const Position& pos);
|
||||||
|
|
||||||
|
// Returns whether the change stored in this StateInfo means that
|
||||||
|
// a full accumulator refresh is required.
|
||||||
|
static bool requires_refresh(const StateInfo* st, Color perspective);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Stockfish::Eval::NNUE::Features
|
||||||
|
|
||||||
|
#endif // #ifndef NNUE_FEATURES_HALF_KA_V2_HM_H_INCLUDED
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
|
||||||
|
|
||||||
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/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
//Definition of input features HalfKP of NNUE evaluation function
|
|
||||||
|
|
||||||
#include "half_kp.h"
|
|
||||||
#include "index_list.h"
|
|
||||||
|
|
||||||
namespace Eval::NNUE::Features {
|
|
||||||
|
|
||||||
// Orient a square according to perspective (rotates by 180 for black)
|
|
||||||
inline Square orient(Color perspective, Square s) {
|
|
||||||
return Square(int(s) ^ (bool(perspective) * 63));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Index of a feature for a given king position and another piece on some square
|
|
||||||
inline IndexType make_index(Color perspective, Square s, Piece pc, Square ksq) {
|
|
||||||
return IndexType(orient(perspective, s) + kpp_board_index[perspective][pc] + PS_END * ksq);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a list of indices for active features
|
|
||||||
template <Side AssociatedKing>
|
|
||||||
void HalfKP<AssociatedKing>::AppendActiveIndices(
|
|
||||||
const Position& pos, Color perspective, IndexList* active) {
|
|
||||||
|
|
||||||
Square ksq = orient(perspective, pos.square<KING>(perspective));
|
|
||||||
Bitboard bb = pos.pieces() & ~pos.pieces(KING);
|
|
||||||
while (bb) {
|
|
||||||
Square s = pop_lsb(&bb);
|
|
||||||
active->push_back(make_index(perspective, s, pos.piece_on(s), ksq));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a list of indices for recently changed features
|
|
||||||
template <Side AssociatedKing>
|
|
||||||
void HalfKP<AssociatedKing>::AppendChangedIndices(
|
|
||||||
const Position& pos, const DirtyPiece& dp, Color perspective,
|
|
||||||
IndexList* removed, IndexList* added) {
|
|
||||||
|
|
||||||
Square ksq = orient(perspective, pos.square<KING>(perspective));
|
|
||||||
for (int i = 0; i < dp.dirty_num; ++i) {
|
|
||||||
Piece pc = dp.piece[i];
|
|
||||||
if (type_of(pc) == KING) continue;
|
|
||||||
if (dp.from[i] != SQ_NONE)
|
|
||||||
removed->push_back(make_index(perspective, dp.from[i], pc, ksq));
|
|
||||||
if (dp.to[i] != SQ_NONE)
|
|
||||||
added->push_back(make_index(perspective, dp.to[i], pc, ksq));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template class HalfKP<Side::kFriend>;
|
|
||||||
|
|
||||||
} // namespace Eval::NNUE::Features
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
|
||||||
|
|
||||||
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/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
//Definition of input features HalfKP of NNUE evaluation function
|
|
||||||
|
|
||||||
#ifndef NNUE_FEATURES_HALF_KP_H_INCLUDED
|
|
||||||
#define NNUE_FEATURES_HALF_KP_H_INCLUDED
|
|
||||||
|
|
||||||
#include "../../evaluate.h"
|
|
||||||
#include "features_common.h"
|
|
||||||
|
|
||||||
namespace Eval::NNUE::Features {
|
|
||||||
|
|
||||||
// Feature HalfKP: Combination of the position of own king
|
|
||||||
// and the position of pieces other than kings
|
|
||||||
template <Side AssociatedKing>
|
|
||||||
class HalfKP {
|
|
||||||
|
|
||||||
public:
|
|
||||||
// Feature name
|
|
||||||
static constexpr const char* kName = "HalfKP(Friend)";
|
|
||||||
// Hash value embedded in the evaluation file
|
|
||||||
static constexpr std::uint32_t kHashValue =
|
|
||||||
0x5D69D5B9u ^ (AssociatedKing == Side::kFriend);
|
|
||||||
// Number of feature dimensions
|
|
||||||
static constexpr IndexType kDimensions =
|
|
||||||
static_cast<IndexType>(SQUARE_NB) * static_cast<IndexType>(PS_END);
|
|
||||||
// Maximum number of simultaneously active features
|
|
||||||
static constexpr IndexType kMaxActiveDimensions = 30; // Kings don't count
|
|
||||||
// Trigger for full calculation instead of difference calculation
|
|
||||||
static constexpr TriggerEvent kRefreshTrigger = TriggerEvent::kFriendKingMoved;
|
|
||||||
|
|
||||||
// Get a list of indices for active features
|
|
||||||
static void AppendActiveIndices(const Position& pos, Color perspective,
|
|
||||||
IndexList* active);
|
|
||||||
|
|
||||||
// Get a list of indices for recently changed features
|
|
||||||
static void AppendChangedIndices(const Position& pos, const DirtyPiece& dp, Color perspective,
|
|
||||||
IndexList* removed, IndexList* added);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Eval::NNUE::Features
|
|
||||||
|
|
||||||
#endif // #ifndef NNUE_FEATURES_HALF_KP_H_INCLUDED
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
|
||||||
|
|
||||||
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/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Definition of index list of input features
|
|
||||||
|
|
||||||
#ifndef NNUE_FEATURES_INDEX_LIST_H_INCLUDED
|
|
||||||
#define NNUE_FEATURES_INDEX_LIST_H_INCLUDED
|
|
||||||
|
|
||||||
#include "../../position.h"
|
|
||||||
#include "../nnue_architecture.h"
|
|
||||||
|
|
||||||
namespace Eval::NNUE::Features {
|
|
||||||
|
|
||||||
// Class template used for feature index list
|
|
||||||
template <typename T, std::size_t MaxSize>
|
|
||||||
class ValueList {
|
|
||||||
|
|
||||||
public:
|
|
||||||
std::size_t size() const { return size_; }
|
|
||||||
void resize(std::size_t size) { size_ = size; }
|
|
||||||
void push_back(const T& value) { values_[size_++] = value; }
|
|
||||||
T& operator[](std::size_t index) { return values_[index]; }
|
|
||||||
T* begin() { return values_; }
|
|
||||||
T* end() { return values_ + size_; }
|
|
||||||
const T& operator[](std::size_t index) const { return values_[index]; }
|
|
||||||
const T* begin() const { return values_; }
|
|
||||||
const T* end() const { return values_ + size_; }
|
|
||||||
|
|
||||||
void swap(ValueList& other) {
|
|
||||||
const std::size_t max_size = std::max(size_, other.size_);
|
|
||||||
for (std::size_t i = 0; i < max_size; ++i) {
|
|
||||||
std::swap(values_[i], other.values_[i]);
|
|
||||||
}
|
|
||||||
std::swap(size_, other.size_);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
T values_[MaxSize];
|
|
||||||
std::size_t size_ = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
//Type of feature index list
|
|
||||||
class IndexList
|
|
||||||
: public ValueList<IndexType, RawFeatures::kMaxActiveDimensions> {
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Eval::NNUE::Features
|
|
||||||
|
|
||||||
#endif // NNUE_FEATURES_INDEX_LIST_H_INCLUDED
|
|
||||||
+466
-385
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -22,410 +22,344 @@
|
|||||||
#define NNUE_LAYERS_AFFINE_TRANSFORM_H_INCLUDED
|
#define NNUE_LAYERS_AFFINE_TRANSFORM_H_INCLUDED
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <type_traits>
|
||||||
#include "../nnue_common.h"
|
#include "../nnue_common.h"
|
||||||
|
#include "simd.h"
|
||||||
|
|
||||||
namespace Eval::NNUE::Layers {
|
/*
|
||||||
|
This file contains the definition for a fully connected layer (aka affine transform).
|
||||||
|
Two approaches are employed, depending on the sizes of the transform.
|
||||||
|
|
||||||
// Affine transformation layer
|
Approach 1:
|
||||||
template <typename PreviousLayer, IndexType OutputDimensions>
|
- used when the PaddedInputDimensions >= 128
|
||||||
class AffineTransform {
|
- uses AVX512 if possible
|
||||||
public:
|
- processes inputs in batches of 2*InputSimdWidth
|
||||||
// Input/output type
|
- so in batches of 128 for AVX512
|
||||||
using InputType = typename PreviousLayer::OutputType;
|
- the weight blocks of size InputSimdWidth are transposed such that
|
||||||
using OutputType = std::int32_t;
|
access is sequential
|
||||||
static_assert(std::is_same<InputType, std::uint8_t>::value, "");
|
- N columns of the weight matrix are processed a time, where N
|
||||||
|
depends on the architecture (the amount of registers)
|
||||||
|
- accumulate + hadd is used
|
||||||
|
|
||||||
// Number of input/output dimensions
|
Approach 2:
|
||||||
static constexpr IndexType kInputDimensions =
|
- used when the PaddedInputDimensions < 128
|
||||||
PreviousLayer::kOutputDimensions;
|
- does not use AVX512
|
||||||
static constexpr IndexType kOutputDimensions = OutputDimensions;
|
- expected use-case is for when PaddedInputDimensions == 32 and InputDimensions <= 32.
|
||||||
static constexpr IndexType kPaddedInputDimensions =
|
- that's why AVX512 is hard to implement
|
||||||
CeilToMultiple<IndexType>(kInputDimensions, kMaxSimdWidth);
|
- expected use-case is small layers
|
||||||
#if defined (USE_AVX512)
|
- not optimized as well as the approach 1
|
||||||
static constexpr const IndexType kOutputSimdWidth = kSimdWidth / 2;
|
- inputs are processed in chunks of 4, weights are respectively transposed
|
||||||
#elif defined (USE_SSSE3)
|
- accumulation happens directly to int32s
|
||||||
static constexpr const IndexType kOutputSimdWidth = kSimdWidth / 4;
|
*/
|
||||||
#endif
|
|
||||||
|
|
||||||
// Size of forward propagation buffer used in this layer
|
namespace Stockfish::Eval::NNUE::Layers {
|
||||||
static constexpr std::size_t kSelfBufferSize =
|
|
||||||
CeilToMultiple(kOutputDimensions * sizeof(OutputType), kCacheLineSize);
|
|
||||||
|
|
||||||
// Size of the forward propagation buffer used from the input layer to this layer
|
// Fallback implementation for older/other architectures.
|
||||||
static constexpr std::size_t kBufferSize =
|
// Identical for both approaches. Requires the input to be padded to at least 16 values.
|
||||||
PreviousLayer::kBufferSize + kSelfBufferSize;
|
|
||||||
|
|
||||||
// Hash value embedded in the evaluation file
|
|
||||||
static constexpr std::uint32_t GetHashValue() {
|
|
||||||
std::uint32_t hash_value = 0xCC03DAE4u;
|
|
||||||
hash_value += kOutputDimensions;
|
|
||||||
hash_value ^= PreviousLayer::GetHashValue() >> 1;
|
|
||||||
hash_value ^= PreviousLayer::GetHashValue() << 31;
|
|
||||||
return hash_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read network parameters
|
|
||||||
bool ReadParameters(std::istream& stream) {
|
|
||||||
if (!previous_layer_.ReadParameters(stream)) return false;
|
|
||||||
for (std::size_t i = 0; i < kOutputDimensions; ++i)
|
|
||||||
biases_[i] = read_little_endian<BiasType>(stream);
|
|
||||||
for (std::size_t i = 0; i < kOutputDimensions * kPaddedInputDimensions; ++i)
|
|
||||||
#if !defined(USE_SSSE3)
|
#if !defined(USE_SSSE3)
|
||||||
weights_[i] = read_little_endian<WeightType>(stream);
|
template <IndexType InputDimensions, IndexType PaddedInputDimensions, IndexType OutputDimensions>
|
||||||
#else
|
static void affine_transform_non_ssse3(std::int32_t* output, const std::int8_t* weights, const std::int32_t* biases, const std::uint8_t* input)
|
||||||
weights_[
|
|
||||||
(i / 4) % (kPaddedInputDimensions / 4) * kOutputDimensions * 4 +
|
|
||||||
i / kPaddedInputDimensions * 4 +
|
|
||||||
i % 4
|
|
||||||
] = read_little_endian<WeightType>(stream);
|
|
||||||
|
|
||||||
// Determine if eights of weight and input products can be summed using 16bits
|
|
||||||
// without saturation. We assume worst case combinations of 0 and 127 for all inputs.
|
|
||||||
if (kOutputDimensions > 1 && !stream.fail())
|
|
||||||
{
|
{
|
||||||
canSaturate16.count = 0;
|
|
||||||
#if !defined(USE_VNNI)
|
|
||||||
for (IndexType i = 0; i < kPaddedInputDimensions; i += 16)
|
|
||||||
for (IndexType j = 0; j < kOutputDimensions; ++j)
|
|
||||||
for (int x = 0; x < 2; ++x)
|
|
||||||
{
|
|
||||||
WeightType* w = &weights_[i * kOutputDimensions + j * 4 + x * 2];
|
|
||||||
int sum[2] = {0, 0};
|
|
||||||
for (int k = 0; k < 8; ++k)
|
|
||||||
{
|
|
||||||
IndexType idx = k / 2 * kOutputDimensions * 4 + k % 2;
|
|
||||||
sum[w[idx] < 0] += w[idx];
|
|
||||||
}
|
|
||||||
for (int sign : {-1, 1})
|
|
||||||
while (sign * sum[sign == -1] > 258)
|
|
||||||
{
|
|
||||||
int maxK = 0, maxW = 0;
|
|
||||||
for (int k = 0; k < 8; ++k)
|
|
||||||
{
|
|
||||||
IndexType idx = k / 2 * kOutputDimensions * 4 + k % 2;
|
|
||||||
if (maxW < sign * w[idx])
|
|
||||||
maxK = k, maxW = sign * w[idx];
|
|
||||||
}
|
|
||||||
|
|
||||||
IndexType idx = maxK / 2 * kOutputDimensions * 4 + maxK % 2;
|
|
||||||
sum[sign == -1] -= w[idx];
|
|
||||||
canSaturate16.add(j, i + maxK / 2 * 4 + maxK % 2 + x * 2, w[idx]);
|
|
||||||
w[idx] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Non functional optimization for faster more linear access
|
|
||||||
std::sort(canSaturate16.ids, canSaturate16.ids + canSaturate16.count,
|
|
||||||
[](const typename CanSaturate::Entry& e1, const typename CanSaturate::Entry& e2)
|
|
||||||
{ return e1.in == e2.in ? e1.out < e2.out : e1.in < e2.in; });
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return !stream.fail();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Forward propagation
|
|
||||||
const OutputType* Propagate(
|
|
||||||
const TransformedFeatureType* transformed_features, char* buffer) const {
|
|
||||||
const auto input = previous_layer_.Propagate(
|
|
||||||
transformed_features, buffer + kSelfBufferSize);
|
|
||||||
|
|
||||||
#if defined (USE_AVX512)
|
|
||||||
|
|
||||||
[[maybe_unused]] const __m512i kOnes512 = _mm512_set1_epi16(1);
|
|
||||||
|
|
||||||
[[maybe_unused]] auto m512_hadd = [](__m512i sum, int bias) -> int {
|
|
||||||
return _mm512_reduce_add_epi32(sum) + bias;
|
|
||||||
};
|
|
||||||
|
|
||||||
[[maybe_unused]] auto m512_add_dpbusd_epi32 = [=](__m512i& acc, __m512i a, __m512i b) {
|
|
||||||
#if defined (USE_VNNI)
|
|
||||||
acc = _mm512_dpbusd_epi32(acc, a, b);
|
|
||||||
#else
|
|
||||||
__m512i product0 = _mm512_maddubs_epi16(a, b);
|
|
||||||
product0 = _mm512_madd_epi16(product0, kOnes512);
|
|
||||||
acc = _mm512_add_epi32(acc, product0);
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
[[maybe_unused]] auto m512_add_dpbusd_epi32x4 = [=](__m512i& acc, __m512i a0, __m512i b0, __m512i a1, __m512i b1,
|
|
||||||
__m512i a2, __m512i b2, __m512i a3, __m512i b3) {
|
|
||||||
#if defined (USE_VNNI)
|
|
||||||
acc = _mm512_dpbusd_epi32(acc, a0, b0);
|
|
||||||
acc = _mm512_dpbusd_epi32(acc, a1, b1);
|
|
||||||
acc = _mm512_dpbusd_epi32(acc, a2, b2);
|
|
||||||
acc = _mm512_dpbusd_epi32(acc, a3, b3);
|
|
||||||
#else
|
|
||||||
__m512i product0 = _mm512_maddubs_epi16(a0, b0);
|
|
||||||
__m512i product1 = _mm512_maddubs_epi16(a1, b1);
|
|
||||||
__m512i product2 = _mm512_maddubs_epi16(a2, b2);
|
|
||||||
__m512i product3 = _mm512_maddubs_epi16(a3, b3);
|
|
||||||
product0 = _mm512_add_epi16(product0, product1);
|
|
||||||
product2 = _mm512_add_epi16(product2, product3);
|
|
||||||
product0 = _mm512_add_epi16(product0, product2);
|
|
||||||
product0 = _mm512_madd_epi16(product0, kOnes512);
|
|
||||||
acc = _mm512_add_epi32(acc, product0);
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
#if defined (USE_AVX2)
|
|
||||||
|
|
||||||
[[maybe_unused]] const __m256i kOnes256 = _mm256_set1_epi16(1);
|
|
||||||
|
|
||||||
[[maybe_unused]] auto m256_hadd = [](__m256i sum, int bias) -> int {
|
|
||||||
__m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(sum), _mm256_extracti128_si256(sum, 1));
|
|
||||||
sum128 = _mm_add_epi32(sum128, _mm_shuffle_epi32(sum128, _MM_PERM_BADC));
|
|
||||||
sum128 = _mm_add_epi32(sum128, _mm_shuffle_epi32(sum128, _MM_PERM_CDAB));
|
|
||||||
return _mm_cvtsi128_si32(sum128) + bias;
|
|
||||||
};
|
|
||||||
|
|
||||||
[[maybe_unused]] auto m256_add_dpbusd_epi32 = [=](__m256i& acc, __m256i a, __m256i b) {
|
|
||||||
#if defined (USE_VNNI)
|
|
||||||
acc = _mm256_dpbusd_epi32(acc, a, b);
|
|
||||||
#else
|
|
||||||
__m256i product0 = _mm256_maddubs_epi16(a, b);
|
|
||||||
product0 = _mm256_madd_epi16(product0, kOnes256);
|
|
||||||
acc = _mm256_add_epi32(acc, product0);
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
[[maybe_unused]] auto m256_add_dpbusd_epi32x4 = [=](__m256i& acc, __m256i a0, __m256i b0, __m256i a1, __m256i b1,
|
|
||||||
__m256i a2, __m256i b2, __m256i a3, __m256i b3) {
|
|
||||||
#if defined (USE_VNNI)
|
|
||||||
acc = _mm256_dpbusd_epi32(acc, a0, b0);
|
|
||||||
acc = _mm256_dpbusd_epi32(acc, a1, b1);
|
|
||||||
acc = _mm256_dpbusd_epi32(acc, a2, b2);
|
|
||||||
acc = _mm256_dpbusd_epi32(acc, a3, b3);
|
|
||||||
#else
|
|
||||||
__m256i product0 = _mm256_maddubs_epi16(a0, b0);
|
|
||||||
__m256i product1 = _mm256_maddubs_epi16(a1, b1);
|
|
||||||
__m256i product2 = _mm256_maddubs_epi16(a2, b2);
|
|
||||||
__m256i product3 = _mm256_maddubs_epi16(a3, b3);
|
|
||||||
product0 = _mm256_add_epi16(product0, product1);
|
|
||||||
product2 = _mm256_add_epi16(product2, product3);
|
|
||||||
product0 = _mm256_add_epi16(product0, product2);
|
|
||||||
product0 = _mm256_madd_epi16(product0, kOnes256);
|
|
||||||
acc = _mm256_add_epi32(acc, product0);
|
|
||||||
#endif
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
#if defined (USE_SSSE3)
|
|
||||||
|
|
||||||
[[maybe_unused]] const __m128i kOnes128 = _mm_set1_epi16(1);
|
|
||||||
|
|
||||||
[[maybe_unused]] auto m128_hadd = [](__m128i sum, int bias) -> int {
|
|
||||||
sum = _mm_add_epi32(sum, _mm_shuffle_epi32(sum, 0x4E)); //_MM_PERM_BADC
|
|
||||||
sum = _mm_add_epi32(sum, _mm_shuffle_epi32(sum, 0xB1)); //_MM_PERM_CDAB
|
|
||||||
return _mm_cvtsi128_si32(sum) + bias;
|
|
||||||
};
|
|
||||||
|
|
||||||
[[maybe_unused]] auto m128_add_dpbusd_epi32 = [=](__m128i& acc, __m128i a, __m128i b) {
|
|
||||||
__m128i product0 = _mm_maddubs_epi16(a, b);
|
|
||||||
product0 = _mm_madd_epi16(product0, kOnes128);
|
|
||||||
acc = _mm_add_epi32(acc, product0);
|
|
||||||
};
|
|
||||||
|
|
||||||
[[maybe_unused]] auto m128_add_dpbusd_epi32x4 = [=](__m128i& acc, __m128i a0, __m128i b0, __m128i a1, __m128i b1,
|
|
||||||
__m128i a2, __m128i b2, __m128i a3, __m128i b3) {
|
|
||||||
__m128i product0 = _mm_maddubs_epi16(a0, b0);
|
|
||||||
__m128i product1 = _mm_maddubs_epi16(a1, b1);
|
|
||||||
__m128i product2 = _mm_maddubs_epi16(a2, b2);
|
|
||||||
__m128i product3 = _mm_maddubs_epi16(a3, b3);
|
|
||||||
product0 = _mm_adds_epi16(product0, product1);
|
|
||||||
product2 = _mm_adds_epi16(product2, product3);
|
|
||||||
product0 = _mm_adds_epi16(product0, product2);
|
|
||||||
product0 = _mm_madd_epi16(product0, kOnes128);
|
|
||||||
acc = _mm_add_epi32(acc, product0);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined (USE_AVX512)
|
|
||||||
using vec_t = __m512i;
|
|
||||||
#define vec_setzero _mm512_setzero_si512
|
|
||||||
#define vec_set_32 _mm512_set1_epi32
|
|
||||||
auto& vec_add_dpbusd_32 = m512_add_dpbusd_epi32;
|
|
||||||
auto& vec_add_dpbusd_32x4 = m512_add_dpbusd_epi32x4;
|
|
||||||
auto& vec_hadd = m512_hadd;
|
|
||||||
#elif defined (USE_AVX2)
|
|
||||||
using vec_t = __m256i;
|
|
||||||
#define vec_setzero _mm256_setzero_si256
|
|
||||||
#define vec_set_32 _mm256_set1_epi32
|
|
||||||
auto& vec_add_dpbusd_32 = m256_add_dpbusd_epi32;
|
|
||||||
auto& vec_add_dpbusd_32x4 = m256_add_dpbusd_epi32x4;
|
|
||||||
auto& vec_hadd = m256_hadd;
|
|
||||||
#elif defined (USE_SSSE3)
|
|
||||||
using vec_t = __m128i;
|
|
||||||
#define vec_setzero _mm_setzero_si128
|
|
||||||
#define vec_set_32 _mm_set1_epi32
|
|
||||||
auto& vec_add_dpbusd_32 = m128_add_dpbusd_epi32;
|
|
||||||
auto& vec_add_dpbusd_32x4 = m128_add_dpbusd_epi32x4;
|
|
||||||
auto& vec_hadd = m128_hadd;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined (USE_SSSE3)
|
|
||||||
|
|
||||||
const auto output = reinterpret_cast<OutputType*>(buffer);
|
|
||||||
const auto input_vector = reinterpret_cast<const vec_t*>(input);
|
|
||||||
|
|
||||||
static_assert(kOutputDimensions % kOutputSimdWidth == 0 || kOutputDimensions == 1);
|
|
||||||
|
|
||||||
// kOutputDimensions is either 1 or a multiple of kSimdWidth
|
|
||||||
// because then it is also an input dimension.
|
|
||||||
if constexpr (kOutputDimensions % kOutputSimdWidth == 0)
|
|
||||||
{
|
|
||||||
constexpr IndexType kNumChunks = kPaddedInputDimensions / 4;
|
|
||||||
|
|
||||||
const auto input32 = reinterpret_cast<const std::int32_t*>(input);
|
|
||||||
vec_t* outptr = reinterpret_cast<vec_t*>(output);
|
|
||||||
std::memcpy(output, biases_, kOutputDimensions * sizeof(OutputType));
|
|
||||||
|
|
||||||
for (int i = 0; i < (int)kNumChunks - 3; i += 4)
|
|
||||||
{
|
|
||||||
const vec_t in0 = vec_set_32(input32[i + 0]);
|
|
||||||
const vec_t in1 = vec_set_32(input32[i + 1]);
|
|
||||||
const vec_t in2 = vec_set_32(input32[i + 2]);
|
|
||||||
const vec_t in3 = vec_set_32(input32[i + 3]);
|
|
||||||
const auto col0 = reinterpret_cast<const vec_t*>(&weights_[(i + 0) * kOutputDimensions * 4]);
|
|
||||||
const auto col1 = reinterpret_cast<const vec_t*>(&weights_[(i + 1) * kOutputDimensions * 4]);
|
|
||||||
const auto col2 = reinterpret_cast<const vec_t*>(&weights_[(i + 2) * kOutputDimensions * 4]);
|
|
||||||
const auto col3 = reinterpret_cast<const vec_t*>(&weights_[(i + 3) * kOutputDimensions * 4]);
|
|
||||||
for (int j = 0; j * kOutputSimdWidth < kOutputDimensions; ++j)
|
|
||||||
vec_add_dpbusd_32x4(outptr[j], in0, col0[j], in1, col1[j], in2, col2[j], in3, col3[j]);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < canSaturate16.count; ++i)
|
|
||||||
output[canSaturate16.ids[i].out] += input[canSaturate16.ids[i].in] * canSaturate16.ids[i].w;
|
|
||||||
}
|
|
||||||
else if constexpr (kOutputDimensions == 1)
|
|
||||||
{
|
|
||||||
#if defined (USE_AVX512)
|
|
||||||
if constexpr (kPaddedInputDimensions % (kSimdWidth * 2) != 0)
|
|
||||||
{
|
|
||||||
constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
|
|
||||||
const auto input_vector256 = reinterpret_cast<const __m256i*>(input);
|
|
||||||
|
|
||||||
__m256i sum0 = _mm256_setzero_si256();
|
|
||||||
const auto row0 = reinterpret_cast<const __m256i*>(&weights_[0]);
|
|
||||||
|
|
||||||
for (int j = 0; j < (int)kNumChunks; ++j)
|
|
||||||
{
|
|
||||||
const __m256i in = input_vector256[j];
|
|
||||||
m256_add_dpbusd_epi32(sum0, in, row0[j]);
|
|
||||||
}
|
|
||||||
output[0] = m256_hadd(sum0, biases_[0]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
#if defined (USE_AVX512)
|
|
||||||
constexpr IndexType kNumChunks = kPaddedInputDimensions / (kSimdWidth * 2);
|
|
||||||
#else
|
|
||||||
constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
|
|
||||||
#endif
|
|
||||||
vec_t sum0 = vec_setzero();
|
|
||||||
const auto row0 = reinterpret_cast<const vec_t*>(&weights_[0]);
|
|
||||||
|
|
||||||
for (int j = 0; j < (int)kNumChunks; ++j)
|
|
||||||
{
|
|
||||||
const vec_t in = input_vector[j];
|
|
||||||
vec_add_dpbusd_32(sum0, in, row0[j]);
|
|
||||||
}
|
|
||||||
output[0] = vec_hadd(sum0, biases_[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
// Use old implementation for the other architectures.
|
|
||||||
|
|
||||||
auto output = reinterpret_cast<OutputType*>(buffer);
|
|
||||||
|
|
||||||
# if defined(USE_SSE2)
|
# if defined(USE_SSE2)
|
||||||
constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
|
// At least a multiple of 16, with SSE2.
|
||||||
const __m128i kZeros = _mm_setzero_si128();
|
constexpr IndexType NumChunks = ceil_to_multiple<IndexType>(InputDimensions, 16) / 16;
|
||||||
const auto input_vector = reinterpret_cast<const __m128i*>(input);
|
const __m128i Zeros = _mm_setzero_si128();
|
||||||
|
const auto inputVector = reinterpret_cast<const __m128i*>(input);
|
||||||
|
|
||||||
# elif defined(USE_MMX)
|
# elif defined(USE_MMX)
|
||||||
constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
|
constexpr IndexType NumChunks = ceil_to_multiple<IndexType>(InputDimensions, 8) / 8;
|
||||||
const __m64 kZeros = _mm_setzero_si64();
|
const __m64 Zeros = _mm_setzero_si64();
|
||||||
const auto input_vector = reinterpret_cast<const __m64*>(input);
|
const auto inputVector = reinterpret_cast<const __m64*>(input);
|
||||||
|
|
||||||
# elif defined(USE_NEON)
|
# elif defined(USE_NEON)
|
||||||
constexpr IndexType kNumChunks = kPaddedInputDimensions / kSimdWidth;
|
constexpr IndexType NumChunks = ceil_to_multiple<IndexType>(InputDimensions, 16) / 16;
|
||||||
const auto input_vector = reinterpret_cast<const int8x8_t*>(input);
|
const auto inputVector = reinterpret_cast<const int8x8_t*>(input);
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
for (IndexType i = 0; i < kOutputDimensions; ++i) {
|
for (IndexType i = 0; i < OutputDimensions; ++i) {
|
||||||
const IndexType offset = i * kPaddedInputDimensions;
|
const IndexType offset = i * PaddedInputDimensions;
|
||||||
|
|
||||||
# if defined(USE_SSE2)
|
# if defined(USE_SSE2)
|
||||||
__m128i sum_lo = _mm_cvtsi32_si128(biases_[i]);
|
__m128i sumLo = _mm_cvtsi32_si128(biases[i]);
|
||||||
__m128i sum_hi = kZeros;
|
__m128i sumHi = Zeros;
|
||||||
const auto row = reinterpret_cast<const __m128i*>(&weights_[offset]);
|
const auto row = reinterpret_cast<const __m128i*>(&weights[offset]);
|
||||||
for (IndexType j = 0; j < kNumChunks; ++j) {
|
for (IndexType j = 0; j < NumChunks; ++j) {
|
||||||
__m128i row_j = _mm_load_si128(&row[j]);
|
__m128i row_j = _mm_load_si128(&row[j]);
|
||||||
__m128i input_j = _mm_load_si128(&input_vector[j]);
|
__m128i input_j = _mm_load_si128(&inputVector[j]);
|
||||||
__m128i extended_row_lo = _mm_srai_epi16(_mm_unpacklo_epi8(row_j, row_j), 8);
|
__m128i extendedRowLo = _mm_srai_epi16(_mm_unpacklo_epi8(row_j, row_j), 8);
|
||||||
__m128i extended_row_hi = _mm_srai_epi16(_mm_unpackhi_epi8(row_j, row_j), 8);
|
__m128i extendedRowHi = _mm_srai_epi16(_mm_unpackhi_epi8(row_j, row_j), 8);
|
||||||
__m128i extended_input_lo = _mm_unpacklo_epi8(input_j, kZeros);
|
__m128i extendedInputLo = _mm_unpacklo_epi8(input_j, Zeros);
|
||||||
__m128i extended_input_hi = _mm_unpackhi_epi8(input_j, kZeros);
|
__m128i extendedInputHi = _mm_unpackhi_epi8(input_j, Zeros);
|
||||||
__m128i product_lo = _mm_madd_epi16(extended_row_lo, extended_input_lo);
|
__m128i productLo = _mm_madd_epi16(extendedRowLo, extendedInputLo);
|
||||||
__m128i product_hi = _mm_madd_epi16(extended_row_hi, extended_input_hi);
|
__m128i productHi = _mm_madd_epi16(extendedRowHi, extendedInputHi);
|
||||||
sum_lo = _mm_add_epi32(sum_lo, product_lo);
|
sumLo = _mm_add_epi32(sumLo, productLo);
|
||||||
sum_hi = _mm_add_epi32(sum_hi, product_hi);
|
sumHi = _mm_add_epi32(sumHi, productHi);
|
||||||
}
|
}
|
||||||
__m128i sum = _mm_add_epi32(sum_lo, sum_hi);
|
__m128i sum = _mm_add_epi32(sumLo, sumHi);
|
||||||
__m128i sum_high_64 = _mm_shuffle_epi32(sum, _MM_SHUFFLE(1, 0, 3, 2));
|
__m128i sumHigh_64 = _mm_shuffle_epi32(sum, _MM_SHUFFLE(1, 0, 3, 2));
|
||||||
sum = _mm_add_epi32(sum, sum_high_64);
|
sum = _mm_add_epi32(sum, sumHigh_64);
|
||||||
__m128i sum_second_32 = _mm_shufflelo_epi16(sum, _MM_SHUFFLE(1, 0, 3, 2));
|
__m128i sum_second_32 = _mm_shufflelo_epi16(sum, _MM_SHUFFLE(1, 0, 3, 2));
|
||||||
sum = _mm_add_epi32(sum, sum_second_32);
|
sum = _mm_add_epi32(sum, sum_second_32);
|
||||||
output[i] = _mm_cvtsi128_si32(sum);
|
output[i] = _mm_cvtsi128_si32(sum);
|
||||||
|
|
||||||
# elif defined(USE_MMX)
|
# elif defined(USE_MMX)
|
||||||
__m64 sum_lo = _mm_cvtsi32_si64(biases_[i]);
|
__m64 sumLo = _mm_cvtsi32_si64(biases[i]);
|
||||||
__m64 sum_hi = kZeros;
|
__m64 sumHi = Zeros;
|
||||||
const auto row = reinterpret_cast<const __m64*>(&weights_[offset]);
|
const auto row = reinterpret_cast<const __m64*>(&weights[offset]);
|
||||||
for (IndexType j = 0; j < kNumChunks; ++j) {
|
for (IndexType j = 0; j < NumChunks; ++j) {
|
||||||
__m64 row_j = row[j];
|
__m64 row_j = row[j];
|
||||||
__m64 input_j = input_vector[j];
|
__m64 input_j = inputVector[j];
|
||||||
__m64 extended_row_lo = _mm_srai_pi16(_mm_unpacklo_pi8(row_j, row_j), 8);
|
__m64 extendedRowLo = _mm_srai_pi16(_mm_unpacklo_pi8(row_j, row_j), 8);
|
||||||
__m64 extended_row_hi = _mm_srai_pi16(_mm_unpackhi_pi8(row_j, row_j), 8);
|
__m64 extendedRowHi = _mm_srai_pi16(_mm_unpackhi_pi8(row_j, row_j), 8);
|
||||||
__m64 extended_input_lo = _mm_unpacklo_pi8(input_j, kZeros);
|
__m64 extendedInputLo = _mm_unpacklo_pi8(input_j, Zeros);
|
||||||
__m64 extended_input_hi = _mm_unpackhi_pi8(input_j, kZeros);
|
__m64 extendedInputHi = _mm_unpackhi_pi8(input_j, Zeros);
|
||||||
__m64 product_lo = _mm_madd_pi16(extended_row_lo, extended_input_lo);
|
__m64 productLo = _mm_madd_pi16(extendedRowLo, extendedInputLo);
|
||||||
__m64 product_hi = _mm_madd_pi16(extended_row_hi, extended_input_hi);
|
__m64 productHi = _mm_madd_pi16(extendedRowHi, extendedInputHi);
|
||||||
sum_lo = _mm_add_pi32(sum_lo, product_lo);
|
sumLo = _mm_add_pi32(sumLo, productLo);
|
||||||
sum_hi = _mm_add_pi32(sum_hi, product_hi);
|
sumHi = _mm_add_pi32(sumHi, productHi);
|
||||||
}
|
}
|
||||||
__m64 sum = _mm_add_pi32(sum_lo, sum_hi);
|
__m64 sum = _mm_add_pi32(sumLo, sumHi);
|
||||||
sum = _mm_add_pi32(sum, _mm_unpackhi_pi32(sum, sum));
|
sum = _mm_add_pi32(sum, _mm_unpackhi_pi32(sum, sum));
|
||||||
output[i] = _mm_cvtsi64_si32(sum);
|
output[i] = _mm_cvtsi64_si32(sum);
|
||||||
|
|
||||||
# elif defined(USE_NEON)
|
# elif defined(USE_NEON)
|
||||||
int32x4_t sum = {biases_[i]};
|
int32x4_t sum = {biases[i]};
|
||||||
const auto row = reinterpret_cast<const int8x8_t*>(&weights_[offset]);
|
const auto row = reinterpret_cast<const int8x8_t*>(&weights[offset]);
|
||||||
for (IndexType j = 0; j < kNumChunks; ++j) {
|
for (IndexType j = 0; j < NumChunks; ++j) {
|
||||||
int16x8_t product = vmull_s8(input_vector[j * 2], row[j * 2]);
|
int16x8_t product = vmull_s8(inputVector[j * 2], row[j * 2]);
|
||||||
product = vmlal_s8(product, input_vector[j * 2 + 1], row[j * 2 + 1]);
|
product = vmlal_s8(product, inputVector[j * 2 + 1], row[j * 2 + 1]);
|
||||||
sum = vpadalq_s16(sum, product);
|
sum = vpadalq_s16(sum, product);
|
||||||
}
|
}
|
||||||
output[i] = sum[0] + sum[1] + sum[2] + sum[3];
|
output[i] = sum[0] + sum[1] + sum[2] + sum[3];
|
||||||
|
|
||||||
# else
|
# else
|
||||||
OutputType sum = biases_[i];
|
std::int32_t sum = biases[i];
|
||||||
for (IndexType j = 0; j < kInputDimensions; ++j) {
|
for (IndexType j = 0; j < InputDimensions; ++j) {
|
||||||
sum += weights_[offset + j] * input[j];
|
sum += weights[offset + j] * input[j];
|
||||||
}
|
}
|
||||||
output[i] = sum;
|
output[i] = sum;
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# if defined(USE_MMX)
|
# if defined(USE_MMX)
|
||||||
_mm_empty();
|
_mm_empty();
|
||||||
# endif
|
# endif
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template <IndexType InDims, IndexType OutDims, typename Enabled = void>
|
||||||
|
class AffineTransform;
|
||||||
|
|
||||||
|
#if defined (USE_AVX512)
|
||||||
|
constexpr IndexType LargeInputSize = 2 * 64;
|
||||||
|
#else
|
||||||
|
constexpr IndexType LargeInputSize = std::numeric_limits<IndexType>::max();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// A specialization for large inputs.
|
||||||
|
template <IndexType InDims, IndexType OutDims>
|
||||||
|
class AffineTransform<InDims, OutDims, std::enable_if_t<(ceil_to_multiple<IndexType>(InDims, MaxSimdWidth) >= LargeInputSize)>> {
|
||||||
|
public:
|
||||||
|
// Input/output type
|
||||||
|
using InputType = std::uint8_t;
|
||||||
|
using OutputType = std::int32_t;
|
||||||
|
|
||||||
|
// Number of input/output dimensions
|
||||||
|
static constexpr IndexType InputDimensions = InDims;
|
||||||
|
static constexpr IndexType OutputDimensions = OutDims;
|
||||||
|
|
||||||
|
static constexpr IndexType PaddedInputDimensions =
|
||||||
|
ceil_to_multiple<IndexType>(InputDimensions, MaxSimdWidth);
|
||||||
|
static constexpr IndexType PaddedOutputDimensions =
|
||||||
|
ceil_to_multiple<IndexType>(OutputDimensions, MaxSimdWidth);
|
||||||
|
|
||||||
|
using OutputBuffer = OutputType[PaddedOutputDimensions];
|
||||||
|
|
||||||
|
static_assert(PaddedInputDimensions >= LargeInputSize, "Something went wrong. This specialization should not have been chosen.");
|
||||||
|
|
||||||
|
#if defined (USE_AVX512)
|
||||||
|
static constexpr const IndexType InputSimdWidth = 64;
|
||||||
|
static constexpr const IndexType MaxNumOutputRegs = 16;
|
||||||
|
#elif defined (USE_AVX2)
|
||||||
|
static constexpr const IndexType InputSimdWidth = 32;
|
||||||
|
static constexpr const IndexType MaxNumOutputRegs = 8;
|
||||||
|
#elif defined (USE_SSSE3)
|
||||||
|
static constexpr const IndexType InputSimdWidth = 16;
|
||||||
|
static constexpr const IndexType MaxNumOutputRegs = 8;
|
||||||
|
#elif defined (USE_NEON)
|
||||||
|
static constexpr const IndexType InputSimdWidth = 8;
|
||||||
|
static constexpr const IndexType MaxNumOutputRegs = 8;
|
||||||
|
#else
|
||||||
|
// The fallback implementation will not have permuted weights.
|
||||||
|
// We define these to avoid a lot of ifdefs later.
|
||||||
|
static constexpr const IndexType InputSimdWidth = 1;
|
||||||
|
static constexpr const IndexType MaxNumOutputRegs = 1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// A big block is a region in the weight matrix of the size [PaddedInputDimensions, NumOutputRegs].
|
||||||
|
// A small block is a region of size [InputSimdWidth, 1]
|
||||||
|
|
||||||
|
static constexpr const IndexType NumOutputRegs = std::min(MaxNumOutputRegs, OutputDimensions);
|
||||||
|
static constexpr const IndexType SmallBlockSize = InputSimdWidth;
|
||||||
|
static constexpr const IndexType BigBlockSize = NumOutputRegs * PaddedInputDimensions;
|
||||||
|
static constexpr const IndexType NumSmallBlocksInBigBlock = BigBlockSize / SmallBlockSize;
|
||||||
|
static constexpr const IndexType NumSmallBlocksPerOutput = PaddedInputDimensions / SmallBlockSize;
|
||||||
|
static constexpr const IndexType NumBigBlocks = OutputDimensions / NumOutputRegs;
|
||||||
|
|
||||||
|
static_assert(OutputDimensions % NumOutputRegs == 0);
|
||||||
|
|
||||||
|
// Hash value embedded in the evaluation file
|
||||||
|
static constexpr std::uint32_t get_hash_value(std::uint32_t prevHash) {
|
||||||
|
std::uint32_t hashValue = 0xCC03DAE4u;
|
||||||
|
hashValue += OutputDimensions;
|
||||||
|
hashValue ^= prevHash >> 1;
|
||||||
|
hashValue ^= prevHash << 31;
|
||||||
|
return hashValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Transposes the small blocks within a block.
|
||||||
|
Effectively means that weights can be traversed sequentially during inference.
|
||||||
|
*/
|
||||||
|
static IndexType get_weight_index(IndexType i)
|
||||||
|
{
|
||||||
|
const IndexType smallBlock = (i / SmallBlockSize) % NumSmallBlocksInBigBlock;
|
||||||
|
const IndexType smallBlockCol = smallBlock / NumSmallBlocksPerOutput;
|
||||||
|
const IndexType smallBlockRow = smallBlock % NumSmallBlocksPerOutput;
|
||||||
|
const IndexType bigBlock = i / BigBlockSize;
|
||||||
|
const IndexType rest = i % SmallBlockSize;
|
||||||
|
|
||||||
|
const IndexType idx =
|
||||||
|
bigBlock * BigBlockSize
|
||||||
|
+ smallBlockRow * SmallBlockSize * NumOutputRegs
|
||||||
|
+ smallBlockCol * SmallBlockSize
|
||||||
|
+ rest;
|
||||||
|
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read network parameters
|
||||||
|
bool read_parameters(std::istream& stream) {
|
||||||
|
for (IndexType i = 0; i < OutputDimensions; ++i)
|
||||||
|
biases[i] = read_little_endian<BiasType>(stream);
|
||||||
|
|
||||||
|
for (IndexType i = 0; i < OutputDimensions * PaddedInputDimensions; ++i)
|
||||||
|
weights[get_weight_index(i)] = read_little_endian<WeightType>(stream);
|
||||||
|
|
||||||
|
return !stream.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write network parameters
|
||||||
|
bool write_parameters(std::ostream& stream) const {
|
||||||
|
for (IndexType i = 0; i < OutputDimensions; ++i)
|
||||||
|
write_little_endian<BiasType>(stream, biases[i]);
|
||||||
|
|
||||||
|
for (IndexType i = 0; i < OutputDimensions * PaddedInputDimensions; ++i)
|
||||||
|
write_little_endian<WeightType>(stream, weights[get_weight_index(i)]);
|
||||||
|
|
||||||
|
return !stream.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forward propagation
|
||||||
|
const OutputType* propagate(
|
||||||
|
const InputType* input, OutputType* output) const {
|
||||||
|
|
||||||
|
#if defined (USE_AVX512)
|
||||||
|
using acc_vec_t = __m512i;
|
||||||
|
using bias_vec_t = __m128i;
|
||||||
|
using weight_vec_t = __m512i;
|
||||||
|
using in_vec_t = __m512i;
|
||||||
|
#define vec_zero _mm512_setzero_si512()
|
||||||
|
#define vec_add_dpbusd_32x2 Simd::m512_add_dpbusd_epi32x2
|
||||||
|
#define vec_hadd Simd::m512_hadd
|
||||||
|
#define vec_haddx4 Simd::m512_haddx4
|
||||||
|
#elif defined (USE_AVX2)
|
||||||
|
using acc_vec_t = __m256i;
|
||||||
|
using bias_vec_t = __m128i;
|
||||||
|
using weight_vec_t = __m256i;
|
||||||
|
using in_vec_t = __m256i;
|
||||||
|
#define vec_zero _mm256_setzero_si256()
|
||||||
|
#define vec_add_dpbusd_32x2 Simd::m256_add_dpbusd_epi32x2
|
||||||
|
#define vec_hadd Simd::m256_hadd
|
||||||
|
#define vec_haddx4 Simd::m256_haddx4
|
||||||
|
#elif defined (USE_SSSE3)
|
||||||
|
using acc_vec_t = __m128i;
|
||||||
|
using bias_vec_t = __m128i;
|
||||||
|
using weight_vec_t = __m128i;
|
||||||
|
using in_vec_t = __m128i;
|
||||||
|
#define vec_zero _mm_setzero_si128()
|
||||||
|
#define vec_add_dpbusd_32x2 Simd::m128_add_dpbusd_epi32x2
|
||||||
|
#define vec_hadd Simd::m128_hadd
|
||||||
|
#define vec_haddx4 Simd::m128_haddx4
|
||||||
|
#elif defined (USE_NEON)
|
||||||
|
using acc_vec_t = int32x4_t;
|
||||||
|
using bias_vec_t = int32x4_t;
|
||||||
|
using weight_vec_t = int8x8_t;
|
||||||
|
using in_vec_t = int8x8_t;
|
||||||
|
#define vec_zero {0}
|
||||||
|
#define vec_add_dpbusd_32x2 Simd::neon_m128_add_dpbusd_epi32x2
|
||||||
|
#define vec_hadd Simd::neon_m128_hadd
|
||||||
|
#define vec_haddx4 Simd::neon_m128_haddx4
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined (USE_SSSE3) || defined (USE_NEON)
|
||||||
|
const in_vec_t* invec = reinterpret_cast<const in_vec_t*>(input);
|
||||||
|
|
||||||
|
// Perform accumulation to registers for each big block
|
||||||
|
for (IndexType bigBlock = 0; bigBlock < NumBigBlocks; ++bigBlock)
|
||||||
|
{
|
||||||
|
acc_vec_t acc[NumOutputRegs] = { vec_zero };
|
||||||
|
|
||||||
|
// Each big block has NumOutputRegs small blocks in each "row", one per register.
|
||||||
|
// We process two small blocks at a time to save on one addition without VNNI.
|
||||||
|
for (IndexType smallBlock = 0; smallBlock < NumSmallBlocksPerOutput; smallBlock += 2)
|
||||||
|
{
|
||||||
|
const weight_vec_t* weightvec =
|
||||||
|
reinterpret_cast<const weight_vec_t*>(
|
||||||
|
weights
|
||||||
|
+ bigBlock * BigBlockSize
|
||||||
|
+ smallBlock * SmallBlockSize * NumOutputRegs);
|
||||||
|
|
||||||
|
const in_vec_t in0 = invec[smallBlock + 0];
|
||||||
|
const in_vec_t in1 = invec[smallBlock + 1];
|
||||||
|
|
||||||
|
for (IndexType k = 0; k < NumOutputRegs; ++k)
|
||||||
|
vec_add_dpbusd_32x2(acc[k], in0, weightvec[k], in1, weightvec[k + NumOutputRegs]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Horizontally add all accumulators.
|
||||||
|
if constexpr (NumOutputRegs % 4 == 0)
|
||||||
|
{
|
||||||
|
bias_vec_t* outputvec = reinterpret_cast<bias_vec_t*>(output);
|
||||||
|
const bias_vec_t* biasvec = reinterpret_cast<const bias_vec_t*>(biases);
|
||||||
|
|
||||||
|
for (IndexType k = 0; k < NumOutputRegs; k += 4)
|
||||||
|
{
|
||||||
|
const IndexType idx = (bigBlock * NumOutputRegs + k) / 4;
|
||||||
|
outputvec[idx] = vec_haddx4(acc[k+0], acc[k+1], acc[k+2], acc[k+3], biasvec[idx]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (IndexType k = 0; k < NumOutputRegs; ++k)
|
||||||
|
{
|
||||||
|
const IndexType idx = (bigBlock * NumOutputRegs + k);
|
||||||
|
output[idx] = vec_hadd(acc[k], biases[idx]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# undef vec_zero
|
||||||
|
# undef vec_add_dpbusd_32x2
|
||||||
|
# undef vec_hadd
|
||||||
|
# undef vec_haddx4
|
||||||
|
#else
|
||||||
|
// Use old implementation for the other architectures.
|
||||||
|
affine_transform_non_ssse3<
|
||||||
|
InputDimensions,
|
||||||
|
PaddedInputDimensions,
|
||||||
|
OutputDimensions>(output, weights, biases, input);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -436,29 +370,176 @@ namespace Eval::NNUE::Layers {
|
|||||||
using BiasType = OutputType;
|
using BiasType = OutputType;
|
||||||
using WeightType = std::int8_t;
|
using WeightType = std::int8_t;
|
||||||
|
|
||||||
PreviousLayer previous_layer_;
|
alignas(CacheLineSize) BiasType biases[OutputDimensions];
|
||||||
|
alignas(CacheLineSize) WeightType weights[OutputDimensions * PaddedInputDimensions];
|
||||||
alignas(kCacheLineSize) BiasType biases_[kOutputDimensions];
|
|
||||||
alignas(kCacheLineSize) WeightType weights_[kOutputDimensions * kPaddedInputDimensions];
|
|
||||||
#if defined (USE_SSSE3)
|
|
||||||
struct CanSaturate {
|
|
||||||
int count;
|
|
||||||
struct Entry {
|
|
||||||
uint16_t out;
|
|
||||||
uint16_t in;
|
|
||||||
int8_t w;
|
|
||||||
} ids[kPaddedInputDimensions * kOutputDimensions * 3 / 4];
|
|
||||||
|
|
||||||
void add(int i, int j, int8_t w) {
|
|
||||||
ids[count].out = i;
|
|
||||||
ids[count].in = j;
|
|
||||||
ids[count].w = w;
|
|
||||||
++count;
|
|
||||||
}
|
|
||||||
} canSaturate16;
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Eval::NNUE::Layers
|
template <IndexType InDims, IndexType OutDims>
|
||||||
|
class AffineTransform<InDims, OutDims, std::enable_if_t<(ceil_to_multiple<IndexType>(InDims, MaxSimdWidth) < LargeInputSize)>> {
|
||||||
|
public:
|
||||||
|
// Input/output type
|
||||||
|
// Input/output type
|
||||||
|
using InputType = std::uint8_t;
|
||||||
|
using OutputType = std::int32_t;
|
||||||
|
|
||||||
|
// Number of input/output dimensions
|
||||||
|
static constexpr IndexType InputDimensions = InDims;
|
||||||
|
static constexpr IndexType OutputDimensions = OutDims;
|
||||||
|
|
||||||
|
static constexpr IndexType PaddedInputDimensions =
|
||||||
|
ceil_to_multiple<IndexType>(InputDimensions, MaxSimdWidth);
|
||||||
|
static constexpr IndexType PaddedOutputDimensions =
|
||||||
|
ceil_to_multiple<IndexType>(OutputDimensions, MaxSimdWidth);
|
||||||
|
|
||||||
|
using OutputBuffer = OutputType[PaddedOutputDimensions];
|
||||||
|
|
||||||
|
static_assert(PaddedInputDimensions < LargeInputSize, "Something went wrong. This specialization should not have been chosen.");
|
||||||
|
|
||||||
|
#if defined (USE_SSSE3)
|
||||||
|
static constexpr const IndexType OutputSimdWidth = SimdWidth / 4;
|
||||||
|
static constexpr const IndexType InputSimdWidth = SimdWidth;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Hash value embedded in the evaluation file
|
||||||
|
static constexpr std::uint32_t get_hash_value(std::uint32_t prevHash) {
|
||||||
|
std::uint32_t hashValue = 0xCC03DAE4u;
|
||||||
|
hashValue += OutputDimensions;
|
||||||
|
hashValue ^= prevHash >> 1;
|
||||||
|
hashValue ^= prevHash << 31;
|
||||||
|
return hashValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
static IndexType get_weight_index_scrambled(IndexType i)
|
||||||
|
{
|
||||||
|
return
|
||||||
|
(i / 4) % (PaddedInputDimensions / 4) * OutputDimensions * 4 +
|
||||||
|
i / PaddedInputDimensions * 4 +
|
||||||
|
i % 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
static IndexType get_weight_index(IndexType i)
|
||||||
|
{
|
||||||
|
#if defined (USE_SSSE3)
|
||||||
|
return get_weight_index_scrambled(i);
|
||||||
|
#else
|
||||||
|
return i;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read network parameters
|
||||||
|
bool read_parameters(std::istream& stream) {
|
||||||
|
for (IndexType i = 0; i < OutputDimensions; ++i)
|
||||||
|
biases[i] = read_little_endian<BiasType>(stream);
|
||||||
|
for (IndexType i = 0; i < OutputDimensions * PaddedInputDimensions; ++i)
|
||||||
|
weights[get_weight_index(i)] = read_little_endian<WeightType>(stream);
|
||||||
|
|
||||||
|
return !stream.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write network parameters
|
||||||
|
bool write_parameters(std::ostream& stream) const {
|
||||||
|
for (IndexType i = 0; i < OutputDimensions; ++i)
|
||||||
|
write_little_endian<BiasType>(stream, biases[i]);
|
||||||
|
|
||||||
|
for (IndexType i = 0; i < OutputDimensions * PaddedInputDimensions; ++i)
|
||||||
|
write_little_endian<WeightType>(stream, weights[get_weight_index(i)]);
|
||||||
|
|
||||||
|
return !stream.fail();
|
||||||
|
}
|
||||||
|
// Forward propagation
|
||||||
|
const OutputType* propagate(
|
||||||
|
const InputType* input, OutputType* output) const {
|
||||||
|
|
||||||
|
#if defined (USE_AVX2)
|
||||||
|
using vec_t = __m256i;
|
||||||
|
#define vec_setzero _mm256_setzero_si256
|
||||||
|
#define vec_set_32 _mm256_set1_epi32
|
||||||
|
#define vec_add_dpbusd_32 Simd::m256_add_dpbusd_epi32
|
||||||
|
#define vec_add_dpbusd_32x2 Simd::m256_add_dpbusd_epi32x2
|
||||||
|
#define vec_add_dpbusd_32x4 Simd::m256_add_dpbusd_epi32x4
|
||||||
|
#define vec_hadd Simd::m256_hadd
|
||||||
|
#define vec_haddx4 Simd::m256_haddx4
|
||||||
|
#elif defined (USE_SSSE3)
|
||||||
|
using vec_t = __m128i;
|
||||||
|
#define vec_setzero _mm_setzero_si128
|
||||||
|
#define vec_set_32 _mm_set1_epi32
|
||||||
|
#define vec_add_dpbusd_32 Simd::m128_add_dpbusd_epi32
|
||||||
|
#define vec_add_dpbusd_32x2 Simd::m128_add_dpbusd_epi32x2
|
||||||
|
#define vec_add_dpbusd_32x4 Simd::m128_add_dpbusd_epi32x4
|
||||||
|
#define vec_hadd Simd::m128_hadd
|
||||||
|
#define vec_haddx4 Simd::m128_haddx4
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined (USE_SSSE3)
|
||||||
|
const auto inputVector = reinterpret_cast<const vec_t*>(input);
|
||||||
|
|
||||||
|
static_assert(OutputDimensions % OutputSimdWidth == 0 || OutputDimensions == 1);
|
||||||
|
|
||||||
|
if constexpr (OutputDimensions % OutputSimdWidth == 0)
|
||||||
|
{
|
||||||
|
constexpr IndexType NumChunks = ceil_to_multiple<IndexType>(InputDimensions, 8) / 4;
|
||||||
|
constexpr IndexType NumRegs = OutputDimensions / OutputSimdWidth;
|
||||||
|
|
||||||
|
const auto input32 = reinterpret_cast<const std::int32_t*>(input);
|
||||||
|
const vec_t* biasvec = reinterpret_cast<const vec_t*>(biases);
|
||||||
|
vec_t acc[NumRegs];
|
||||||
|
for (IndexType k = 0; k < NumRegs; ++k)
|
||||||
|
acc[k] = biasvec[k];
|
||||||
|
|
||||||
|
for (IndexType i = 0; i < NumChunks; i += 2)
|
||||||
|
{
|
||||||
|
const vec_t in0 = vec_set_32(input32[i + 0]);
|
||||||
|
const vec_t in1 = vec_set_32(input32[i + 1]);
|
||||||
|
const auto col0 = reinterpret_cast<const vec_t*>(&weights[(i + 0) * OutputDimensions * 4]);
|
||||||
|
const auto col1 = reinterpret_cast<const vec_t*>(&weights[(i + 1) * OutputDimensions * 4]);
|
||||||
|
for (IndexType k = 0; k < NumRegs; ++k)
|
||||||
|
vec_add_dpbusd_32x2(acc[k], in0, col0[k], in1, col1[k]);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec_t* outptr = reinterpret_cast<vec_t*>(output);
|
||||||
|
for (IndexType k = 0; k < NumRegs; ++k)
|
||||||
|
outptr[k] = acc[k];
|
||||||
|
}
|
||||||
|
else if constexpr (OutputDimensions == 1)
|
||||||
|
{
|
||||||
|
constexpr IndexType NumChunks = PaddedInputDimensions / SimdWidth;
|
||||||
|
vec_t sum0 = vec_setzero();
|
||||||
|
const auto row0 = reinterpret_cast<const vec_t*>(&weights[0]);
|
||||||
|
|
||||||
|
for (int j = 0; j < (int)NumChunks; ++j)
|
||||||
|
{
|
||||||
|
const vec_t in = inputVector[j];
|
||||||
|
vec_add_dpbusd_32(sum0, in, row0[j]);
|
||||||
|
}
|
||||||
|
output[0] = vec_hadd(sum0, biases[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
# undef vec_setzero
|
||||||
|
# undef vec_set_32
|
||||||
|
# undef vec_add_dpbusd_32
|
||||||
|
# undef vec_add_dpbusd_32x2
|
||||||
|
# undef vec_add_dpbusd_32x4
|
||||||
|
# undef vec_hadd
|
||||||
|
# undef vec_haddx4
|
||||||
|
#else
|
||||||
|
// Use old implementation for the other architectures.
|
||||||
|
affine_transform_non_ssse3<
|
||||||
|
InputDimensions,
|
||||||
|
PaddedInputDimensions,
|
||||||
|
OutputDimensions>(output, weights, biases, input);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
using BiasType = OutputType;
|
||||||
|
using WeightType = std::int8_t;
|
||||||
|
|
||||||
|
alignas(CacheLineSize) BiasType biases[OutputDimensions];
|
||||||
|
alignas(CacheLineSize) WeightType weights[OutputDimensions * PaddedInputDimensions];
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Stockfish::Eval::NNUE::Layers
|
||||||
|
|
||||||
#endif // #ifndef NNUE_LAYERS_AFFINE_TRANSFORM_H_INCLUDED
|
#endif // #ifndef NNUE_LAYERS_AFFINE_TRANSFORM_H_INCLUDED
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -23,144 +23,158 @@
|
|||||||
|
|
||||||
#include "../nnue_common.h"
|
#include "../nnue_common.h"
|
||||||
|
|
||||||
namespace Eval::NNUE::Layers {
|
namespace Stockfish::Eval::NNUE::Layers {
|
||||||
|
|
||||||
// Clipped ReLU
|
// Clipped ReLU
|
||||||
template <typename PreviousLayer>
|
template <IndexType InDims>
|
||||||
class ClippedReLU {
|
class ClippedReLU {
|
||||||
public:
|
public:
|
||||||
// Input/output type
|
// Input/output type
|
||||||
using InputType = typename PreviousLayer::OutputType;
|
using InputType = std::int32_t;
|
||||||
using OutputType = std::uint8_t;
|
using OutputType = std::uint8_t;
|
||||||
static_assert(std::is_same<InputType, std::int32_t>::value, "");
|
|
||||||
|
|
||||||
// Number of input/output dimensions
|
// Number of input/output dimensions
|
||||||
static constexpr IndexType kInputDimensions =
|
static constexpr IndexType InputDimensions = InDims;
|
||||||
PreviousLayer::kOutputDimensions;
|
static constexpr IndexType OutputDimensions = InputDimensions;
|
||||||
static constexpr IndexType kOutputDimensions = kInputDimensions;
|
static constexpr IndexType PaddedOutputDimensions =
|
||||||
|
ceil_to_multiple<IndexType>(OutputDimensions, 32);
|
||||||
|
|
||||||
// Size of forward propagation buffer used in this layer
|
using OutputBuffer = OutputType[PaddedOutputDimensions];
|
||||||
static constexpr std::size_t kSelfBufferSize =
|
|
||||||
CeilToMultiple(kOutputDimensions * sizeof(OutputType), kCacheLineSize);
|
|
||||||
|
|
||||||
// Size of the forward propagation buffer used from the input layer to this layer
|
|
||||||
static constexpr std::size_t kBufferSize =
|
|
||||||
PreviousLayer::kBufferSize + kSelfBufferSize;
|
|
||||||
|
|
||||||
// Hash value embedded in the evaluation file
|
// Hash value embedded in the evaluation file
|
||||||
static constexpr std::uint32_t GetHashValue() {
|
static constexpr std::uint32_t get_hash_value(std::uint32_t prevHash) {
|
||||||
std::uint32_t hash_value = 0x538D24C7u;
|
std::uint32_t hashValue = 0x538D24C7u;
|
||||||
hash_value += PreviousLayer::GetHashValue();
|
hashValue += prevHash;
|
||||||
return hash_value;
|
return hashValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read network parameters
|
// Read network parameters
|
||||||
bool ReadParameters(std::istream& stream) {
|
bool read_parameters(std::istream&) {
|
||||||
return previous_layer_.ReadParameters(stream);
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write network parameters
|
||||||
|
bool write_parameters(std::ostream&) const {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forward propagation
|
// Forward propagation
|
||||||
const OutputType* Propagate(
|
const OutputType* propagate(
|
||||||
const TransformedFeatureType* transformed_features, char* buffer) const {
|
const InputType* input, OutputType* output) const {
|
||||||
const auto input = previous_layer_.Propagate(
|
|
||||||
transformed_features, buffer + kSelfBufferSize);
|
|
||||||
const auto output = reinterpret_cast<OutputType*>(buffer);
|
|
||||||
|
|
||||||
#if defined(USE_AVX2)
|
#if defined(USE_AVX2)
|
||||||
constexpr IndexType kNumChunks = kInputDimensions / kSimdWidth;
|
if constexpr (InputDimensions % SimdWidth == 0) {
|
||||||
const __m256i kZero = _mm256_setzero_si256();
|
constexpr IndexType NumChunks = InputDimensions / SimdWidth;
|
||||||
const __m256i kOffsets = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
|
const __m256i Zero = _mm256_setzero_si256();
|
||||||
|
const __m256i Offsets = _mm256_set_epi32(7, 3, 6, 2, 5, 1, 4, 0);
|
||||||
const auto in = reinterpret_cast<const __m256i*>(input);
|
const auto in = reinterpret_cast<const __m256i*>(input);
|
||||||
const auto out = reinterpret_cast<__m256i*>(output);
|
const auto out = reinterpret_cast<__m256i*>(output);
|
||||||
for (IndexType i = 0; i < kNumChunks; ++i) {
|
for (IndexType i = 0; i < NumChunks; ++i) {
|
||||||
const __m256i words0 = _mm256_srai_epi16(_mm256_packs_epi32(
|
const __m256i words0 = _mm256_srai_epi16(_mm256_packs_epi32(
|
||||||
_mm256_load_si256(&in[i * 4 + 0]),
|
_mm256_load_si256(&in[i * 4 + 0]),
|
||||||
_mm256_load_si256(&in[i * 4 + 1])), kWeightScaleBits);
|
_mm256_load_si256(&in[i * 4 + 1])), WeightScaleBits);
|
||||||
const __m256i words1 = _mm256_srai_epi16(_mm256_packs_epi32(
|
const __m256i words1 = _mm256_srai_epi16(_mm256_packs_epi32(
|
||||||
_mm256_load_si256(&in[i * 4 + 2]),
|
_mm256_load_si256(&in[i * 4 + 2]),
|
||||||
_mm256_load_si256(&in[i * 4 + 3])), kWeightScaleBits);
|
_mm256_load_si256(&in[i * 4 + 3])), WeightScaleBits);
|
||||||
_mm256_store_si256(&out[i], _mm256_permutevar8x32_epi32(_mm256_max_epi8(
|
_mm256_store_si256(&out[i], _mm256_permutevar8x32_epi32(_mm256_max_epi8(
|
||||||
_mm256_packs_epi16(words0, words1), kZero), kOffsets));
|
_mm256_packs_epi16(words0, words1), Zero), Offsets));
|
||||||
}
|
}
|
||||||
constexpr IndexType kStart = kNumChunks * kSimdWidth;
|
} else {
|
||||||
|
constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);
|
||||||
|
const __m128i Zero = _mm_setzero_si128();
|
||||||
|
const auto in = reinterpret_cast<const __m128i*>(input);
|
||||||
|
const auto out = reinterpret_cast<__m128i*>(output);
|
||||||
|
for (IndexType i = 0; i < NumChunks; ++i) {
|
||||||
|
const __m128i words0 = _mm_srai_epi16(_mm_packs_epi32(
|
||||||
|
_mm_load_si128(&in[i * 4 + 0]),
|
||||||
|
_mm_load_si128(&in[i * 4 + 1])), WeightScaleBits);
|
||||||
|
const __m128i words1 = _mm_srai_epi16(_mm_packs_epi32(
|
||||||
|
_mm_load_si128(&in[i * 4 + 2]),
|
||||||
|
_mm_load_si128(&in[i * 4 + 3])), WeightScaleBits);
|
||||||
|
const __m128i packedbytes = _mm_packs_epi16(words0, words1);
|
||||||
|
_mm_store_si128(&out[i], _mm_max_epi8(packedbytes, Zero));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
constexpr IndexType Start =
|
||||||
|
InputDimensions % SimdWidth == 0
|
||||||
|
? InputDimensions / SimdWidth * SimdWidth
|
||||||
|
: InputDimensions / (SimdWidth / 2) * (SimdWidth / 2);
|
||||||
|
|
||||||
#elif defined(USE_SSE2)
|
#elif defined(USE_SSE2)
|
||||||
constexpr IndexType kNumChunks = kInputDimensions / kSimdWidth;
|
constexpr IndexType NumChunks = InputDimensions / SimdWidth;
|
||||||
|
|
||||||
#ifdef USE_SSE41
|
#ifdef USE_SSE41
|
||||||
const __m128i kZero = _mm_setzero_si128();
|
const __m128i Zero = _mm_setzero_si128();
|
||||||
#else
|
#else
|
||||||
const __m128i k0x80s = _mm_set1_epi8(-128);
|
const __m128i k0x80s = _mm_set1_epi8(-128);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const auto in = reinterpret_cast<const __m128i*>(input);
|
const auto in = reinterpret_cast<const __m128i*>(input);
|
||||||
const auto out = reinterpret_cast<__m128i*>(output);
|
const auto out = reinterpret_cast<__m128i*>(output);
|
||||||
for (IndexType i = 0; i < kNumChunks; ++i) {
|
for (IndexType i = 0; i < NumChunks; ++i) {
|
||||||
const __m128i words0 = _mm_srai_epi16(_mm_packs_epi32(
|
const __m128i words0 = _mm_srai_epi16(_mm_packs_epi32(
|
||||||
_mm_load_si128(&in[i * 4 + 0]),
|
_mm_load_si128(&in[i * 4 + 0]),
|
||||||
_mm_load_si128(&in[i * 4 + 1])), kWeightScaleBits);
|
_mm_load_si128(&in[i * 4 + 1])), WeightScaleBits);
|
||||||
const __m128i words1 = _mm_srai_epi16(_mm_packs_epi32(
|
const __m128i words1 = _mm_srai_epi16(_mm_packs_epi32(
|
||||||
_mm_load_si128(&in[i * 4 + 2]),
|
_mm_load_si128(&in[i * 4 + 2]),
|
||||||
_mm_load_si128(&in[i * 4 + 3])), kWeightScaleBits);
|
_mm_load_si128(&in[i * 4 + 3])), WeightScaleBits);
|
||||||
const __m128i packedbytes = _mm_packs_epi16(words0, words1);
|
const __m128i packedbytes = _mm_packs_epi16(words0, words1);
|
||||||
_mm_store_si128(&out[i],
|
_mm_store_si128(&out[i],
|
||||||
|
|
||||||
#ifdef USE_SSE41
|
#ifdef USE_SSE41
|
||||||
_mm_max_epi8(packedbytes, kZero)
|
_mm_max_epi8(packedbytes, Zero)
|
||||||
#else
|
#else
|
||||||
_mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)
|
_mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
constexpr IndexType kStart = kNumChunks * kSimdWidth;
|
constexpr IndexType Start = NumChunks * SimdWidth;
|
||||||
|
|
||||||
#elif defined(USE_MMX)
|
#elif defined(USE_MMX)
|
||||||
constexpr IndexType kNumChunks = kInputDimensions / kSimdWidth;
|
constexpr IndexType NumChunks = InputDimensions / SimdWidth;
|
||||||
const __m64 k0x80s = _mm_set1_pi8(-128);
|
const __m64 k0x80s = _mm_set1_pi8(-128);
|
||||||
const auto in = reinterpret_cast<const __m64*>(input);
|
const auto in = reinterpret_cast<const __m64*>(input);
|
||||||
const auto out = reinterpret_cast<__m64*>(output);
|
const auto out = reinterpret_cast<__m64*>(output);
|
||||||
for (IndexType i = 0; i < kNumChunks; ++i) {
|
for (IndexType i = 0; i < NumChunks; ++i) {
|
||||||
const __m64 words0 = _mm_srai_pi16(
|
const __m64 words0 = _mm_srai_pi16(
|
||||||
_mm_packs_pi32(in[i * 4 + 0], in[i * 4 + 1]),
|
_mm_packs_pi32(in[i * 4 + 0], in[i * 4 + 1]),
|
||||||
kWeightScaleBits);
|
WeightScaleBits);
|
||||||
const __m64 words1 = _mm_srai_pi16(
|
const __m64 words1 = _mm_srai_pi16(
|
||||||
_mm_packs_pi32(in[i * 4 + 2], in[i * 4 + 3]),
|
_mm_packs_pi32(in[i * 4 + 2], in[i * 4 + 3]),
|
||||||
kWeightScaleBits);
|
WeightScaleBits);
|
||||||
const __m64 packedbytes = _mm_packs_pi16(words0, words1);
|
const __m64 packedbytes = _mm_packs_pi16(words0, words1);
|
||||||
out[i] = _mm_subs_pi8(_mm_adds_pi8(packedbytes, k0x80s), k0x80s);
|
out[i] = _mm_subs_pi8(_mm_adds_pi8(packedbytes, k0x80s), k0x80s);
|
||||||
}
|
}
|
||||||
_mm_empty();
|
_mm_empty();
|
||||||
constexpr IndexType kStart = kNumChunks * kSimdWidth;
|
constexpr IndexType Start = NumChunks * SimdWidth;
|
||||||
|
|
||||||
#elif defined(USE_NEON)
|
#elif defined(USE_NEON)
|
||||||
constexpr IndexType kNumChunks = kInputDimensions / (kSimdWidth / 2);
|
constexpr IndexType NumChunks = InputDimensions / (SimdWidth / 2);
|
||||||
const int8x8_t kZero = {0};
|
const int8x8_t Zero = {0};
|
||||||
const auto in = reinterpret_cast<const int32x4_t*>(input);
|
const auto in = reinterpret_cast<const int32x4_t*>(input);
|
||||||
const auto out = reinterpret_cast<int8x8_t*>(output);
|
const auto out = reinterpret_cast<int8x8_t*>(output);
|
||||||
for (IndexType i = 0; i < kNumChunks; ++i) {
|
for (IndexType i = 0; i < NumChunks; ++i) {
|
||||||
int16x8_t shifted;
|
int16x8_t shifted;
|
||||||
const auto pack = reinterpret_cast<int16x4_t*>(&shifted);
|
const auto pack = reinterpret_cast<int16x4_t*>(&shifted);
|
||||||
pack[0] = vqshrn_n_s32(in[i * 2 + 0], kWeightScaleBits);
|
pack[0] = vqshrn_n_s32(in[i * 2 + 0], WeightScaleBits);
|
||||||
pack[1] = vqshrn_n_s32(in[i * 2 + 1], kWeightScaleBits);
|
pack[1] = vqshrn_n_s32(in[i * 2 + 1], WeightScaleBits);
|
||||||
out[i] = vmax_s8(vqmovn_s16(shifted), kZero);
|
out[i] = vmax_s8(vqmovn_s16(shifted), Zero);
|
||||||
}
|
}
|
||||||
constexpr IndexType kStart = kNumChunks * (kSimdWidth / 2);
|
constexpr IndexType Start = NumChunks * (SimdWidth / 2);
|
||||||
#else
|
#else
|
||||||
constexpr IndexType kStart = 0;
|
constexpr IndexType Start = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (IndexType i = kStart; i < kInputDimensions; ++i) {
|
for (IndexType i = Start; i < InputDimensions; ++i) {
|
||||||
output[i] = static_cast<OutputType>(
|
output[i] = static_cast<OutputType>(
|
||||||
std::max(0, std::min(127, input[i] >> kWeightScaleBits)));
|
std::max(0, std::min(127, input[i] >> WeightScaleBits)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
PreviousLayer previous_layer_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Eval::NNUE::Layers
|
} // namespace Stockfish::Eval::NNUE::Layers
|
||||||
|
|
||||||
#endif // NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED
|
#endif // NNUE_LAYERS_CLIPPED_RELU_H_INCLUDED
|
||||||
|
|||||||
@@ -1,68 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
|
||||||
|
|
||||||
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/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// NNUE evaluation function layer InputSlice definition
|
|
||||||
|
|
||||||
#ifndef NNUE_LAYERS_INPUT_SLICE_H_INCLUDED
|
|
||||||
#define NNUE_LAYERS_INPUT_SLICE_H_INCLUDED
|
|
||||||
|
|
||||||
#include "../nnue_common.h"
|
|
||||||
|
|
||||||
namespace Eval::NNUE::Layers {
|
|
||||||
|
|
||||||
// Input layer
|
|
||||||
template <IndexType OutputDimensions, IndexType Offset = 0>
|
|
||||||
class InputSlice {
|
|
||||||
public:
|
|
||||||
// Need to maintain alignment
|
|
||||||
static_assert(Offset % kMaxSimdWidth == 0, "");
|
|
||||||
|
|
||||||
// Output type
|
|
||||||
using OutputType = TransformedFeatureType;
|
|
||||||
|
|
||||||
// Output dimensionality
|
|
||||||
static constexpr IndexType kOutputDimensions = OutputDimensions;
|
|
||||||
|
|
||||||
// Size of forward propagation buffer used from the input layer to this layer
|
|
||||||
static constexpr std::size_t kBufferSize = 0;
|
|
||||||
|
|
||||||
// Hash value embedded in the evaluation file
|
|
||||||
static constexpr std::uint32_t GetHashValue() {
|
|
||||||
std::uint32_t hash_value = 0xEC42E90Du;
|
|
||||||
hash_value ^= kOutputDimensions ^ (Offset << 10);
|
|
||||||
return hash_value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read network parameters
|
|
||||||
bool ReadParameters(std::istream& /*stream*/) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Forward propagation
|
|
||||||
const OutputType* Propagate(
|
|
||||||
const TransformedFeatureType* transformed_features,
|
|
||||||
char* /*buffer*/) const {
|
|
||||||
return transformed_features + Offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Layers
|
|
||||||
|
|
||||||
#endif // #ifndef NNUE_LAYERS_INPUT_SLICE_H_INCLUDED
|
|
||||||
@@ -0,0 +1,387 @@
|
|||||||
|
/*
|
||||||
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
|
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 STOCKFISH_SIMD_H_INCLUDED
|
||||||
|
#define STOCKFISH_SIMD_H_INCLUDED
|
||||||
|
|
||||||
|
#if defined(USE_AVX2)
|
||||||
|
# include <immintrin.h>
|
||||||
|
|
||||||
|
#elif defined(USE_SSE41)
|
||||||
|
# include <smmintrin.h>
|
||||||
|
|
||||||
|
#elif defined(USE_SSSE3)
|
||||||
|
# include <tmmintrin.h>
|
||||||
|
|
||||||
|
#elif defined(USE_SSE2)
|
||||||
|
# include <emmintrin.h>
|
||||||
|
|
||||||
|
#elif defined(USE_MMX)
|
||||||
|
# include <mmintrin.h>
|
||||||
|
|
||||||
|
#elif defined(USE_NEON)
|
||||||
|
# include <arm_neon.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// The inline asm is only safe for GCC, where it is necessary to get good codegen.
|
||||||
|
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101693
|
||||||
|
// Clang does fine without it.
|
||||||
|
// Play around here: https://godbolt.org/z/7EWqrYq51
|
||||||
|
#if (defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER))
|
||||||
|
#define USE_INLINE_ASM
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Use either the AVX512 or AVX-VNNI version of the VNNI instructions.
|
||||||
|
#if defined(USE_AVXVNNI)
|
||||||
|
#define VNNI_PREFIX "%{vex%} "
|
||||||
|
#else
|
||||||
|
#define VNNI_PREFIX ""
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace Stockfish::Simd {
|
||||||
|
|
||||||
|
#if defined (USE_AVX512)
|
||||||
|
|
||||||
|
[[maybe_unused]] static int m512_hadd(__m512i sum, int bias) {
|
||||||
|
return _mm512_reduce_add_epi32(sum) + bias;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Parameters:
|
||||||
|
sum0 = [zmm0.i128[0], zmm0.i128[1], zmm0.i128[2], zmm0.i128[3]]
|
||||||
|
sum1 = [zmm1.i128[0], zmm1.i128[1], zmm1.i128[2], zmm1.i128[3]]
|
||||||
|
sum2 = [zmm2.i128[0], zmm2.i128[1], zmm2.i128[2], zmm2.i128[3]]
|
||||||
|
sum3 = [zmm3.i128[0], zmm3.i128[1], zmm3.i128[2], zmm3.i128[3]]
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
ret = [
|
||||||
|
reduce_add_epi32(zmm0.i128[0]), reduce_add_epi32(zmm1.i128[0]), reduce_add_epi32(zmm2.i128[0]), reduce_add_epi32(zmm3.i128[0]),
|
||||||
|
reduce_add_epi32(zmm0.i128[1]), reduce_add_epi32(zmm1.i128[1]), reduce_add_epi32(zmm2.i128[1]), reduce_add_epi32(zmm3.i128[1]),
|
||||||
|
reduce_add_epi32(zmm0.i128[2]), reduce_add_epi32(zmm1.i128[2]), reduce_add_epi32(zmm2.i128[2]), reduce_add_epi32(zmm3.i128[2]),
|
||||||
|
reduce_add_epi32(zmm0.i128[3]), reduce_add_epi32(zmm1.i128[3]), reduce_add_epi32(zmm2.i128[3]), reduce_add_epi32(zmm3.i128[3])
|
||||||
|
]
|
||||||
|
*/
|
||||||
|
[[maybe_unused]] static __m512i m512_hadd128x16_interleave(
|
||||||
|
__m512i sum0, __m512i sum1, __m512i sum2, __m512i sum3) {
|
||||||
|
|
||||||
|
__m512i sum01a = _mm512_unpacklo_epi32(sum0, sum1);
|
||||||
|
__m512i sum01b = _mm512_unpackhi_epi32(sum0, sum1);
|
||||||
|
|
||||||
|
__m512i sum23a = _mm512_unpacklo_epi32(sum2, sum3);
|
||||||
|
__m512i sum23b = _mm512_unpackhi_epi32(sum2, sum3);
|
||||||
|
|
||||||
|
__m512i sum01 = _mm512_add_epi32(sum01a, sum01b);
|
||||||
|
__m512i sum23 = _mm512_add_epi32(sum23a, sum23b);
|
||||||
|
|
||||||
|
__m512i sum0123a = _mm512_unpacklo_epi64(sum01, sum23);
|
||||||
|
__m512i sum0123b = _mm512_unpackhi_epi64(sum01, sum23);
|
||||||
|
|
||||||
|
return _mm512_add_epi32(sum0123a, sum0123b);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] static __m128i m512_haddx4(
|
||||||
|
__m512i sum0, __m512i sum1, __m512i sum2, __m512i sum3,
|
||||||
|
__m128i bias) {
|
||||||
|
|
||||||
|
__m512i sum = m512_hadd128x16_interleave(sum0, sum1, sum2, sum3);
|
||||||
|
|
||||||
|
__m256i sum256lo = _mm512_castsi512_si256(sum);
|
||||||
|
__m256i sum256hi = _mm512_extracti64x4_epi64(sum, 1);
|
||||||
|
|
||||||
|
sum256lo = _mm256_add_epi32(sum256lo, sum256hi);
|
||||||
|
|
||||||
|
__m128i sum128lo = _mm256_castsi256_si128(sum256lo);
|
||||||
|
__m128i sum128hi = _mm256_extracti128_si256(sum256lo, 1);
|
||||||
|
|
||||||
|
return _mm_add_epi32(_mm_add_epi32(sum128lo, sum128hi), bias);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] static void m512_add_dpbusd_epi32(
|
||||||
|
__m512i& acc,
|
||||||
|
__m512i a,
|
||||||
|
__m512i b) {
|
||||||
|
|
||||||
|
# if defined (USE_VNNI)
|
||||||
|
# if defined (USE_INLINE_ASM)
|
||||||
|
asm(
|
||||||
|
"vpdpbusd %[b], %[a], %[acc]\n\t"
|
||||||
|
: [acc]"+v"(acc)
|
||||||
|
: [a]"v"(a), [b]"vm"(b)
|
||||||
|
);
|
||||||
|
# else
|
||||||
|
acc = _mm512_dpbusd_epi32(acc, a, b);
|
||||||
|
# endif
|
||||||
|
# else
|
||||||
|
# if defined (USE_INLINE_ASM)
|
||||||
|
__m512i tmp = _mm512_maddubs_epi16(a, b);
|
||||||
|
asm(
|
||||||
|
"vpmaddwd %[tmp], %[ones], %[tmp]\n\t"
|
||||||
|
"vpaddd %[acc], %[tmp], %[acc]\n\t"
|
||||||
|
: [acc]"+v"(acc), [tmp]"+&v"(tmp)
|
||||||
|
: [ones]"v"(_mm512_set1_epi16(1))
|
||||||
|
);
|
||||||
|
# else
|
||||||
|
__m512i product0 = _mm512_maddubs_epi16(a, b);
|
||||||
|
product0 = _mm512_madd_epi16(product0, _mm512_set1_epi16(1));
|
||||||
|
acc = _mm512_add_epi32(acc, product0);
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] static void m512_add_dpbusd_epi32x2(
|
||||||
|
__m512i& acc,
|
||||||
|
__m512i a0, __m512i b0,
|
||||||
|
__m512i a1, __m512i b1) {
|
||||||
|
|
||||||
|
# if defined (USE_VNNI)
|
||||||
|
# if defined (USE_INLINE_ASM)
|
||||||
|
asm(
|
||||||
|
"vpdpbusd %[b0], %[a0], %[acc]\n\t"
|
||||||
|
"vpdpbusd %[b1], %[a1], %[acc]\n\t"
|
||||||
|
: [acc]"+v"(acc)
|
||||||
|
: [a0]"v"(a0), [b0]"vm"(b0), [a1]"v"(a1), [b1]"vm"(b1)
|
||||||
|
);
|
||||||
|
# else
|
||||||
|
acc = _mm512_dpbusd_epi32(acc, a0, b0);
|
||||||
|
acc = _mm512_dpbusd_epi32(acc, a1, b1);
|
||||||
|
# endif
|
||||||
|
# else
|
||||||
|
# if defined (USE_INLINE_ASM)
|
||||||
|
__m512i tmp0 = _mm512_maddubs_epi16(a0, b0);
|
||||||
|
__m512i tmp1 = _mm512_maddubs_epi16(a1, b1);
|
||||||
|
asm(
|
||||||
|
"vpaddsw %[tmp0], %[tmp1], %[tmp0]\n\t"
|
||||||
|
"vpmaddwd %[tmp0], %[ones], %[tmp0]\n\t"
|
||||||
|
"vpaddd %[acc], %[tmp0], %[acc]\n\t"
|
||||||
|
: [acc]"+v"(acc), [tmp0]"+&v"(tmp0)
|
||||||
|
: [tmp1]"v"(tmp1), [ones]"v"(_mm512_set1_epi16(1))
|
||||||
|
);
|
||||||
|
# else
|
||||||
|
__m512i product0 = _mm512_maddubs_epi16(a0, b0);
|
||||||
|
__m512i product1 = _mm512_maddubs_epi16(a1, b1);
|
||||||
|
product0 = _mm512_adds_epi16(product0, product1);
|
||||||
|
product0 = _mm512_madd_epi16(product0, _mm512_set1_epi16(1));
|
||||||
|
acc = _mm512_add_epi32(acc, product0);
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined (USE_AVX2)
|
||||||
|
|
||||||
|
[[maybe_unused]] static int m256_hadd(__m256i sum, int bias) {
|
||||||
|
__m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(sum), _mm256_extracti128_si256(sum, 1));
|
||||||
|
sum128 = _mm_add_epi32(sum128, _mm_shuffle_epi32(sum128, _MM_PERM_BADC));
|
||||||
|
sum128 = _mm_add_epi32(sum128, _mm_shuffle_epi32(sum128, _MM_PERM_CDAB));
|
||||||
|
return _mm_cvtsi128_si32(sum128) + bias;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] static __m128i m256_haddx4(
|
||||||
|
__m256i sum0, __m256i sum1, __m256i sum2, __m256i sum3,
|
||||||
|
__m128i bias) {
|
||||||
|
|
||||||
|
sum0 = _mm256_hadd_epi32(sum0, sum1);
|
||||||
|
sum2 = _mm256_hadd_epi32(sum2, sum3);
|
||||||
|
|
||||||
|
sum0 = _mm256_hadd_epi32(sum0, sum2);
|
||||||
|
|
||||||
|
__m128i sum128lo = _mm256_castsi256_si128(sum0);
|
||||||
|
__m128i sum128hi = _mm256_extracti128_si256(sum0, 1);
|
||||||
|
|
||||||
|
return _mm_add_epi32(_mm_add_epi32(sum128lo, sum128hi), bias);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] static void m256_add_dpbusd_epi32(
|
||||||
|
__m256i& acc,
|
||||||
|
__m256i a,
|
||||||
|
__m256i b) {
|
||||||
|
|
||||||
|
# if defined (USE_VNNI)
|
||||||
|
# if defined (USE_INLINE_ASM)
|
||||||
|
asm(
|
||||||
|
VNNI_PREFIX "vpdpbusd %[b], %[a], %[acc]\n\t"
|
||||||
|
: [acc]"+v"(acc)
|
||||||
|
: [a]"v"(a), [b]"vm"(b)
|
||||||
|
);
|
||||||
|
# else
|
||||||
|
acc = _mm256_dpbusd_epi32(acc, a, b);
|
||||||
|
# endif
|
||||||
|
# else
|
||||||
|
# if defined (USE_INLINE_ASM)
|
||||||
|
__m256i tmp = _mm256_maddubs_epi16(a, b);
|
||||||
|
asm(
|
||||||
|
"vpmaddwd %[tmp], %[ones], %[tmp]\n\t"
|
||||||
|
"vpaddd %[acc], %[tmp], %[acc]\n\t"
|
||||||
|
: [acc]"+v"(acc), [tmp]"+&v"(tmp)
|
||||||
|
: [ones]"v"(_mm256_set1_epi16(1))
|
||||||
|
);
|
||||||
|
# else
|
||||||
|
__m256i product0 = _mm256_maddubs_epi16(a, b);
|
||||||
|
product0 = _mm256_madd_epi16(product0, _mm256_set1_epi16(1));
|
||||||
|
acc = _mm256_add_epi32(acc, product0);
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] static void m256_add_dpbusd_epi32x2(
|
||||||
|
__m256i& acc,
|
||||||
|
__m256i a0, __m256i b0,
|
||||||
|
__m256i a1, __m256i b1) {
|
||||||
|
|
||||||
|
# if defined (USE_VNNI)
|
||||||
|
# if defined (USE_INLINE_ASM)
|
||||||
|
asm(
|
||||||
|
VNNI_PREFIX "vpdpbusd %[b0], %[a0], %[acc]\n\t"
|
||||||
|
VNNI_PREFIX "vpdpbusd %[b1], %[a1], %[acc]\n\t"
|
||||||
|
: [acc]"+v"(acc)
|
||||||
|
: [a0]"v"(a0), [b0]"vm"(b0), [a1]"v"(a1), [b1]"vm"(b1)
|
||||||
|
);
|
||||||
|
# else
|
||||||
|
acc = _mm256_dpbusd_epi32(acc, a0, b0);
|
||||||
|
acc = _mm256_dpbusd_epi32(acc, a1, b1);
|
||||||
|
# endif
|
||||||
|
# else
|
||||||
|
# if defined (USE_INLINE_ASM)
|
||||||
|
__m256i tmp0 = _mm256_maddubs_epi16(a0, b0);
|
||||||
|
__m256i tmp1 = _mm256_maddubs_epi16(a1, b1);
|
||||||
|
asm(
|
||||||
|
"vpaddsw %[tmp0], %[tmp1], %[tmp0]\n\t"
|
||||||
|
"vpmaddwd %[tmp0], %[ones], %[tmp0]\n\t"
|
||||||
|
"vpaddd %[acc], %[tmp0], %[acc]\n\t"
|
||||||
|
: [acc]"+v"(acc), [tmp0]"+&v"(tmp0)
|
||||||
|
: [tmp1]"v"(tmp1), [ones]"v"(_mm256_set1_epi16(1))
|
||||||
|
);
|
||||||
|
# else
|
||||||
|
__m256i product0 = _mm256_maddubs_epi16(a0, b0);
|
||||||
|
__m256i product1 = _mm256_maddubs_epi16(a1, b1);
|
||||||
|
product0 = _mm256_adds_epi16(product0, product1);
|
||||||
|
product0 = _mm256_madd_epi16(product0, _mm256_set1_epi16(1));
|
||||||
|
acc = _mm256_add_epi32(acc, product0);
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined (USE_SSSE3)
|
||||||
|
|
||||||
|
[[maybe_unused]] static int m128_hadd(__m128i sum, int bias) {
|
||||||
|
sum = _mm_add_epi32(sum, _mm_shuffle_epi32(sum, 0x4E)); //_MM_PERM_BADC
|
||||||
|
sum = _mm_add_epi32(sum, _mm_shuffle_epi32(sum, 0xB1)); //_MM_PERM_CDAB
|
||||||
|
return _mm_cvtsi128_si32(sum) + bias;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] static __m128i m128_haddx4(
|
||||||
|
__m128i sum0, __m128i sum1, __m128i sum2, __m128i sum3,
|
||||||
|
__m128i bias) {
|
||||||
|
|
||||||
|
sum0 = _mm_hadd_epi32(sum0, sum1);
|
||||||
|
sum2 = _mm_hadd_epi32(sum2, sum3);
|
||||||
|
sum0 = _mm_hadd_epi32(sum0, sum2);
|
||||||
|
return _mm_add_epi32(sum0, bias);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] static void m128_add_dpbusd_epi32(
|
||||||
|
__m128i& acc,
|
||||||
|
__m128i a,
|
||||||
|
__m128i b) {
|
||||||
|
|
||||||
|
# if defined (USE_INLINE_ASM)
|
||||||
|
__m128i tmp = _mm_maddubs_epi16(a, b);
|
||||||
|
asm(
|
||||||
|
"pmaddwd %[ones], %[tmp]\n\t"
|
||||||
|
"paddd %[tmp], %[acc]\n\t"
|
||||||
|
: [acc]"+v"(acc), [tmp]"+&v"(tmp)
|
||||||
|
: [ones]"v"(_mm_set1_epi16(1))
|
||||||
|
);
|
||||||
|
# else
|
||||||
|
__m128i product0 = _mm_maddubs_epi16(a, b);
|
||||||
|
product0 = _mm_madd_epi16(product0, _mm_set1_epi16(1));
|
||||||
|
acc = _mm_add_epi32(acc, product0);
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] static void m128_add_dpbusd_epi32x2(
|
||||||
|
__m128i& acc,
|
||||||
|
__m128i a0, __m128i b0,
|
||||||
|
__m128i a1, __m128i b1) {
|
||||||
|
|
||||||
|
# if defined (USE_INLINE_ASM)
|
||||||
|
__m128i tmp0 = _mm_maddubs_epi16(a0, b0);
|
||||||
|
__m128i tmp1 = _mm_maddubs_epi16(a1, b1);
|
||||||
|
asm(
|
||||||
|
"paddsw %[tmp1], %[tmp0]\n\t"
|
||||||
|
"pmaddwd %[ones], %[tmp0]\n\t"
|
||||||
|
"paddd %[tmp0], %[acc]\n\t"
|
||||||
|
: [acc]"+v"(acc), [tmp0]"+&v"(tmp0)
|
||||||
|
: [tmp1]"v"(tmp1), [ones]"v"(_mm_set1_epi16(1))
|
||||||
|
);
|
||||||
|
# else
|
||||||
|
__m128i product0 = _mm_maddubs_epi16(a0, b0);
|
||||||
|
__m128i product1 = _mm_maddubs_epi16(a1, b1);
|
||||||
|
product0 = _mm_adds_epi16(product0, product1);
|
||||||
|
product0 = _mm_madd_epi16(product0, _mm_set1_epi16(1));
|
||||||
|
acc = _mm_add_epi32(acc, product0);
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined (USE_NEON)
|
||||||
|
|
||||||
|
[[maybe_unused]] static int neon_m128_reduce_add_epi32(int32x4_t s) {
|
||||||
|
# if USE_NEON >= 8
|
||||||
|
return vaddvq_s32(s);
|
||||||
|
# else
|
||||||
|
return s[0] + s[1] + s[2] + s[3];
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] static int neon_m128_hadd(int32x4_t sum, int bias) {
|
||||||
|
return neon_m128_reduce_add_epi32(sum) + bias;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] static int32x4_t neon_m128_haddx4(
|
||||||
|
int32x4_t sum0, int32x4_t sum1, int32x4_t sum2, int32x4_t sum3,
|
||||||
|
int32x4_t bias) {
|
||||||
|
|
||||||
|
int32x4_t hsums {
|
||||||
|
neon_m128_reduce_add_epi32(sum0),
|
||||||
|
neon_m128_reduce_add_epi32(sum1),
|
||||||
|
neon_m128_reduce_add_epi32(sum2),
|
||||||
|
neon_m128_reduce_add_epi32(sum3)
|
||||||
|
};
|
||||||
|
return vaddq_s32(hsums, bias);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[maybe_unused]] static void neon_m128_add_dpbusd_epi32x2(
|
||||||
|
int32x4_t& acc,
|
||||||
|
int8x8_t a0, int8x8_t b0,
|
||||||
|
int8x8_t a1, int8x8_t b1) {
|
||||||
|
|
||||||
|
int16x8_t product = vmull_s8(a0, b0);
|
||||||
|
product = vmlal_s8(product, a1, b1);
|
||||||
|
acc = vpadalq_s16(acc, product);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // STOCKFISH_SIMD_H_INCLUDED
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
|
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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Definition of layer ClippedReLU of NNUE evaluation function
|
||||||
|
|
||||||
|
#ifndef NNUE_LAYERS_SQR_CLIPPED_RELU_H_INCLUDED
|
||||||
|
#define NNUE_LAYERS_SQR_CLIPPED_RELU_H_INCLUDED
|
||||||
|
|
||||||
|
#include "../nnue_common.h"
|
||||||
|
|
||||||
|
namespace Stockfish::Eval::NNUE::Layers {
|
||||||
|
|
||||||
|
// Clipped ReLU
|
||||||
|
template <IndexType InDims>
|
||||||
|
class SqrClippedReLU {
|
||||||
|
public:
|
||||||
|
// Input/output type
|
||||||
|
using InputType = std::int32_t;
|
||||||
|
using OutputType = std::uint8_t;
|
||||||
|
|
||||||
|
// Number of input/output dimensions
|
||||||
|
static constexpr IndexType InputDimensions = InDims;
|
||||||
|
static constexpr IndexType OutputDimensions = InputDimensions;
|
||||||
|
static constexpr IndexType PaddedOutputDimensions =
|
||||||
|
ceil_to_multiple<IndexType>(OutputDimensions, 32);
|
||||||
|
|
||||||
|
using OutputBuffer = OutputType[PaddedOutputDimensions];
|
||||||
|
|
||||||
|
// Hash value embedded in the evaluation file
|
||||||
|
static constexpr std::uint32_t get_hash_value(std::uint32_t prevHash) {
|
||||||
|
std::uint32_t hashValue = 0x538D24C7u;
|
||||||
|
hashValue += prevHash;
|
||||||
|
return hashValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read network parameters
|
||||||
|
bool read_parameters(std::istream&) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write network parameters
|
||||||
|
bool write_parameters(std::ostream&) const {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forward propagation
|
||||||
|
const OutputType* propagate(
|
||||||
|
const InputType* input, OutputType* output) const {
|
||||||
|
|
||||||
|
#if defined(USE_SSE2)
|
||||||
|
constexpr IndexType NumChunks = InputDimensions / 16;
|
||||||
|
|
||||||
|
#ifdef USE_SSE41
|
||||||
|
const __m128i Zero = _mm_setzero_si128();
|
||||||
|
#else
|
||||||
|
const __m128i k0x80s = _mm_set1_epi8(-128);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static_assert(WeightScaleBits == 6);
|
||||||
|
const auto in = reinterpret_cast<const __m128i*>(input);
|
||||||
|
const auto out = reinterpret_cast<__m128i*>(output);
|
||||||
|
for (IndexType i = 0; i < NumChunks; ++i) {
|
||||||
|
__m128i words0 = _mm_packs_epi32(
|
||||||
|
_mm_load_si128(&in[i * 4 + 0]),
|
||||||
|
_mm_load_si128(&in[i * 4 + 1]));
|
||||||
|
__m128i words1 = _mm_packs_epi32(
|
||||||
|
_mm_load_si128(&in[i * 4 + 2]),
|
||||||
|
_mm_load_si128(&in[i * 4 + 3]));
|
||||||
|
|
||||||
|
// Not sure if
|
||||||
|
words0 = _mm_srli_epi16(_mm_mulhi_epi16(words0, words0), 3);
|
||||||
|
words1 = _mm_srli_epi16(_mm_mulhi_epi16(words1, words1), 3);
|
||||||
|
|
||||||
|
const __m128i packedbytes = _mm_packs_epi16(words0, words1);
|
||||||
|
|
||||||
|
_mm_store_si128(&out[i],
|
||||||
|
|
||||||
|
#ifdef USE_SSE41
|
||||||
|
_mm_max_epi8(packedbytes, Zero)
|
||||||
|
#else
|
||||||
|
_mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
);
|
||||||
|
}
|
||||||
|
constexpr IndexType Start = NumChunks * 16;
|
||||||
|
|
||||||
|
#else
|
||||||
|
constexpr IndexType Start = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (IndexType i = Start; i < InputDimensions; ++i) {
|
||||||
|
output[i] = static_cast<OutputType>(
|
||||||
|
// realy should be /127 but we need to make it fast
|
||||||
|
// needs to be accounted for in the trainer
|
||||||
|
std::max(0ll, std::min(127ll, (((long long)input[i] * input[i]) >> (2 * WeightScaleBits)) / 128)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Stockfish::Eval::NNUE::Layers
|
||||||
|
|
||||||
|
#endif // NNUE_LAYERS_SQR_CLIPPED_RELU_H_INCLUDED
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -23,18 +23,15 @@
|
|||||||
|
|
||||||
#include "nnue_architecture.h"
|
#include "nnue_architecture.h"
|
||||||
|
|
||||||
namespace Eval::NNUE {
|
namespace Stockfish::Eval::NNUE {
|
||||||
|
|
||||||
// The accumulator of a StateInfo without parent is set to the INIT state
|
|
||||||
enum AccumulatorState { EMPTY, COMPUTED, INIT };
|
|
||||||
|
|
||||||
// Class that holds the result of affine transformation of input features
|
// Class that holds the result of affine transformation of input features
|
||||||
struct alignas(kCacheLineSize) Accumulator {
|
struct alignas(CacheLineSize) Accumulator {
|
||||||
std::int16_t
|
std::int16_t accumulation[2][TransformedFeatureDimensions];
|
||||||
accumulation[2][kRefreshTriggers.size()][kTransformedFeatureDimensions];
|
std::int32_t psqtAccumulation[2][PSQTBuckets];
|
||||||
AccumulatorState state[2];
|
bool computed[2];
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Eval::NNUE
|
} // namespace Stockfish::Eval::NNUE
|
||||||
|
|
||||||
#endif // NNUE_ACCUMULATOR_H_INCLUDED
|
#endif // NNUE_ACCUMULATOR_H_INCLUDED
|
||||||
|
|||||||
+110
-10
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -21,18 +21,118 @@
|
|||||||
#ifndef NNUE_ARCHITECTURE_H_INCLUDED
|
#ifndef NNUE_ARCHITECTURE_H_INCLUDED
|
||||||
#define NNUE_ARCHITECTURE_H_INCLUDED
|
#define NNUE_ARCHITECTURE_H_INCLUDED
|
||||||
|
|
||||||
// Defines the network structure
|
#include <memory>
|
||||||
#include "architectures/halfkp_256x2-32-32.h"
|
|
||||||
|
|
||||||
namespace Eval::NNUE {
|
#include "nnue_common.h"
|
||||||
|
|
||||||
static_assert(kTransformedFeatureDimensions % kMaxSimdWidth == 0, "");
|
#include "features/half_ka_v2_hm.h"
|
||||||
static_assert(Network::kOutputDimensions == 1, "");
|
|
||||||
static_assert(std::is_same<Network::OutputType, std::int32_t>::value, "");
|
|
||||||
|
|
||||||
// Trigger for full calculation instead of difference calculation
|
#include "layers/affine_transform.h"
|
||||||
constexpr auto kRefreshTriggers = RawFeatures::kRefreshTriggers;
|
#include "layers/clipped_relu.h"
|
||||||
|
#include "layers/sqr_clipped_relu.h"
|
||||||
|
|
||||||
} // namespace Eval::NNUE
|
#include "../misc.h"
|
||||||
|
|
||||||
|
namespace Stockfish::Eval::NNUE {
|
||||||
|
|
||||||
|
// Input features used in evaluation function
|
||||||
|
using FeatureSet = Features::HalfKAv2_hm;
|
||||||
|
|
||||||
|
// Number of input feature dimensions after conversion
|
||||||
|
constexpr IndexType TransformedFeatureDimensions = 1024;
|
||||||
|
constexpr IndexType PSQTBuckets = 8;
|
||||||
|
constexpr IndexType LayerStacks = 8;
|
||||||
|
|
||||||
|
struct Network
|
||||||
|
{
|
||||||
|
static constexpr int FC_0_OUTPUTS = 15;
|
||||||
|
static constexpr int FC_1_OUTPUTS = 32;
|
||||||
|
|
||||||
|
Layers::AffineTransform<TransformedFeatureDimensions, FC_0_OUTPUTS + 1> fc_0;
|
||||||
|
Layers::SqrClippedReLU<FC_0_OUTPUTS + 1> ac_sqr_0;
|
||||||
|
Layers::ClippedReLU<FC_0_OUTPUTS + 1> ac_0;
|
||||||
|
Layers::AffineTransform<FC_0_OUTPUTS * 2, FC_1_OUTPUTS> fc_1;
|
||||||
|
Layers::ClippedReLU<FC_1_OUTPUTS> ac_1;
|
||||||
|
Layers::AffineTransform<FC_1_OUTPUTS, 1> fc_2;
|
||||||
|
|
||||||
|
// Hash value embedded in the evaluation file
|
||||||
|
static constexpr std::uint32_t get_hash_value() {
|
||||||
|
// input slice hash
|
||||||
|
std::uint32_t hashValue = 0xEC42E90Du;
|
||||||
|
hashValue ^= TransformedFeatureDimensions * 2;
|
||||||
|
|
||||||
|
hashValue = decltype(fc_0)::get_hash_value(hashValue);
|
||||||
|
hashValue = decltype(ac_0)::get_hash_value(hashValue);
|
||||||
|
hashValue = decltype(fc_1)::get_hash_value(hashValue);
|
||||||
|
hashValue = decltype(ac_1)::get_hash_value(hashValue);
|
||||||
|
hashValue = decltype(fc_2)::get_hash_value(hashValue);
|
||||||
|
|
||||||
|
return hashValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read network parameters
|
||||||
|
bool read_parameters(std::istream& stream) {
|
||||||
|
if (!fc_0.read_parameters(stream)) return false;
|
||||||
|
if (!ac_0.read_parameters(stream)) return false;
|
||||||
|
if (!fc_1.read_parameters(stream)) return false;
|
||||||
|
if (!ac_1.read_parameters(stream)) return false;
|
||||||
|
if (!fc_2.read_parameters(stream)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read network parameters
|
||||||
|
bool write_parameters(std::ostream& stream) const {
|
||||||
|
if (!fc_0.write_parameters(stream)) return false;
|
||||||
|
if (!ac_0.write_parameters(stream)) return false;
|
||||||
|
if (!fc_1.write_parameters(stream)) return false;
|
||||||
|
if (!ac_1.write_parameters(stream)) return false;
|
||||||
|
if (!fc_2.write_parameters(stream)) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::int32_t propagate(const TransformedFeatureType* transformedFeatures)
|
||||||
|
{
|
||||||
|
struct alignas(CacheLineSize) Buffer
|
||||||
|
{
|
||||||
|
alignas(CacheLineSize) decltype(fc_0)::OutputBuffer fc_0_out;
|
||||||
|
alignas(CacheLineSize) decltype(ac_sqr_0)::OutputType ac_sqr_0_out[ceil_to_multiple<IndexType>(FC_0_OUTPUTS * 2, 32)];
|
||||||
|
alignas(CacheLineSize) decltype(ac_0)::OutputBuffer ac_0_out;
|
||||||
|
alignas(CacheLineSize) decltype(fc_1)::OutputBuffer fc_1_out;
|
||||||
|
alignas(CacheLineSize) decltype(ac_1)::OutputBuffer ac_1_out;
|
||||||
|
alignas(CacheLineSize) decltype(fc_2)::OutputBuffer fc_2_out;
|
||||||
|
|
||||||
|
Buffer()
|
||||||
|
{
|
||||||
|
std::memset(this, 0, sizeof(*this));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#if defined(__clang__) && (__APPLE__)
|
||||||
|
// workaround for a bug reported with xcode 12
|
||||||
|
static thread_local auto tlsBuffer = std::make_unique<Buffer>();
|
||||||
|
// Access TLS only once, cache result.
|
||||||
|
Buffer& buffer = *tlsBuffer;
|
||||||
|
#else
|
||||||
|
alignas(CacheLineSize) static thread_local Buffer buffer;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
fc_0.propagate(transformedFeatures, buffer.fc_0_out);
|
||||||
|
ac_sqr_0.propagate(buffer.fc_0_out, buffer.ac_sqr_0_out);
|
||||||
|
ac_0.propagate(buffer.fc_0_out, buffer.ac_0_out);
|
||||||
|
std::memcpy(buffer.ac_sqr_0_out + FC_0_OUTPUTS, buffer.ac_0_out, FC_0_OUTPUTS * sizeof(decltype(ac_0)::OutputType));
|
||||||
|
fc_1.propagate(buffer.ac_sqr_0_out, buffer.fc_1_out);
|
||||||
|
ac_1.propagate(buffer.fc_1_out, buffer.ac_1_out);
|
||||||
|
fc_2.propagate(buffer.ac_1_out, buffer.fc_2_out);
|
||||||
|
|
||||||
|
// buffer.fc_0_out[FC_0_OUTPUTS] is such that 1.0 is equal to 127*(1<<WeightScaleBits) in quantized form
|
||||||
|
// but we want 1.0 to be equal to 600*OutputScale
|
||||||
|
std::int32_t fwdOut = int(buffer.fc_0_out[FC_0_OUTPUTS]) * (600*OutputScale) / (127*(1<<WeightScaleBits));
|
||||||
|
std::int32_t outputValue = buffer.fc_2_out[0] + fwdOut;
|
||||||
|
|
||||||
|
return outputValue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Stockfish::Eval::NNUE
|
||||||
|
|
||||||
#endif // #ifndef NNUE_ARCHITECTURE_H_INCLUDED
|
#endif // #ifndef NNUE_ARCHITECTURE_H_INCLUDED
|
||||||
|
|||||||
+74
-42
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -24,6 +24,8 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "../misc.h" // for IsLittleEndian
|
||||||
|
|
||||||
#if defined(USE_AVX2)
|
#if defined(USE_AVX2)
|
||||||
#include <immintrin.h>
|
#include <immintrin.h>
|
||||||
|
|
||||||
@@ -43,61 +45,33 @@
|
|||||||
#include <arm_neon.h>
|
#include <arm_neon.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace Eval::NNUE {
|
namespace Stockfish::Eval::NNUE {
|
||||||
|
|
||||||
// Version of the evaluation file
|
// Version of the evaluation file
|
||||||
constexpr std::uint32_t kVersion = 0x7AF32F16u;
|
constexpr std::uint32_t Version = 0x7AF32F20u;
|
||||||
|
|
||||||
// Constant used in evaluation value calculation
|
// Constant used in evaluation value calculation
|
||||||
constexpr int FV_SCALE = 16;
|
constexpr int OutputScale = 16;
|
||||||
constexpr int kWeightScaleBits = 6;
|
constexpr int WeightScaleBits = 6;
|
||||||
|
|
||||||
// Size of cache line (in bytes)
|
// Size of cache line (in bytes)
|
||||||
constexpr std::size_t kCacheLineSize = 64;
|
constexpr std::size_t CacheLineSize = 64;
|
||||||
|
|
||||||
// SIMD width (in bytes)
|
// SIMD width (in bytes)
|
||||||
#if defined(USE_AVX2)
|
#if defined(USE_AVX2)
|
||||||
constexpr std::size_t kSimdWidth = 32;
|
constexpr std::size_t SimdWidth = 32;
|
||||||
|
|
||||||
#elif defined(USE_SSE2)
|
#elif defined(USE_SSE2)
|
||||||
constexpr std::size_t kSimdWidth = 16;
|
constexpr std::size_t SimdWidth = 16;
|
||||||
|
|
||||||
#elif defined(USE_MMX)
|
#elif defined(USE_MMX)
|
||||||
constexpr std::size_t kSimdWidth = 8;
|
constexpr std::size_t SimdWidth = 8;
|
||||||
|
|
||||||
#elif defined(USE_NEON)
|
#elif defined(USE_NEON)
|
||||||
constexpr std::size_t kSimdWidth = 16;
|
constexpr std::size_t SimdWidth = 16;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
constexpr std::size_t kMaxSimdWidth = 32;
|
constexpr std::size_t MaxSimdWidth = 32;
|
||||||
|
|
||||||
// unique number for each piece type on each square
|
|
||||||
enum {
|
|
||||||
PS_NONE = 0,
|
|
||||||
PS_W_PAWN = 1,
|
|
||||||
PS_B_PAWN = 1 * SQUARE_NB + 1,
|
|
||||||
PS_W_KNIGHT = 2 * SQUARE_NB + 1,
|
|
||||||
PS_B_KNIGHT = 3 * SQUARE_NB + 1,
|
|
||||||
PS_W_BISHOP = 4 * SQUARE_NB + 1,
|
|
||||||
PS_B_BISHOP = 5 * SQUARE_NB + 1,
|
|
||||||
PS_W_ROOK = 6 * SQUARE_NB + 1,
|
|
||||||
PS_B_ROOK = 7 * SQUARE_NB + 1,
|
|
||||||
PS_W_QUEEN = 8 * SQUARE_NB + 1,
|
|
||||||
PS_B_QUEEN = 9 * SQUARE_NB + 1,
|
|
||||||
PS_W_KING = 10 * SQUARE_NB + 1,
|
|
||||||
PS_END = PS_W_KING, // pieces without kings (pawns included)
|
|
||||||
PS_B_KING = 11 * SQUARE_NB + 1,
|
|
||||||
PS_END2 = 12 * SQUARE_NB + 1
|
|
||||||
};
|
|
||||||
|
|
||||||
constexpr uint32_t kpp_board_index[COLOR_NB][PIECE_NB] = {
|
|
||||||
// convention: W - us, B - them
|
|
||||||
// viewed from other side, W and B are reversed
|
|
||||||
{ PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_W_KING, PS_NONE,
|
|
||||||
PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_B_KING, PS_NONE },
|
|
||||||
{ PS_NONE, PS_B_PAWN, PS_B_KNIGHT, PS_B_BISHOP, PS_B_ROOK, PS_B_QUEEN, PS_B_KING, PS_NONE,
|
|
||||||
PS_NONE, PS_W_PAWN, PS_W_KNIGHT, PS_W_BISHOP, PS_W_ROOK, PS_W_QUEEN, PS_W_KING, PS_NONE }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Type of input feature after conversion
|
// Type of input feature after conversion
|
||||||
using TransformedFeatureType = std::uint8_t;
|
using TransformedFeatureType = std::uint8_t;
|
||||||
@@ -105,7 +79,7 @@ namespace Eval::NNUE {
|
|||||||
|
|
||||||
// Round n up to be a multiple of base
|
// Round n up to be a multiple of base
|
||||||
template <typename IntType>
|
template <typename IntType>
|
||||||
constexpr IntType CeilToMultiple(IntType n, IntType base) {
|
constexpr IntType ceil_to_multiple(IntType n, IntType base) {
|
||||||
return (n + base - 1) / base * base;
|
return (n + base - 1) / base * base;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,8 +88,12 @@ namespace Eval::NNUE {
|
|||||||
// necessary to return a result with the byte ordering of the compiling machine.
|
// necessary to return a result with the byte ordering of the compiling machine.
|
||||||
template <typename IntType>
|
template <typename IntType>
|
||||||
inline IntType read_little_endian(std::istream& stream) {
|
inline IntType read_little_endian(std::istream& stream) {
|
||||||
|
|
||||||
IntType result;
|
IntType result;
|
||||||
|
|
||||||
|
if (IsLittleEndian)
|
||||||
|
stream.read(reinterpret_cast<char*>(&result), sizeof(IntType));
|
||||||
|
else
|
||||||
|
{
|
||||||
std::uint8_t u[sizeof(IntType)];
|
std::uint8_t u[sizeof(IntType)];
|
||||||
typename std::make_unsigned<IntType>::type v = 0;
|
typename std::make_unsigned<IntType>::type v = 0;
|
||||||
|
|
||||||
@@ -124,9 +102,63 @@ namespace Eval::NNUE {
|
|||||||
v = (v << 8) | u[sizeof(IntType) - i - 1];
|
v = (v << 8) | u[sizeof(IntType) - i - 1];
|
||||||
|
|
||||||
std::memcpy(&result, &v, sizeof(IntType));
|
std::memcpy(&result, &v, sizeof(IntType));
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Eval::NNUE
|
// write_little_endian() is our utility to write an integer (signed or unsigned, any size)
|
||||||
|
// to a stream in little-endian order. We swap the byte order before the write if
|
||||||
|
// necessary to always write in little endian order, independently of the byte
|
||||||
|
// ordering of the compiling machine.
|
||||||
|
template <typename IntType>
|
||||||
|
inline void write_little_endian(std::ostream& stream, IntType value) {
|
||||||
|
|
||||||
|
if (IsLittleEndian)
|
||||||
|
stream.write(reinterpret_cast<const char*>(&value), sizeof(IntType));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::uint8_t u[sizeof(IntType)];
|
||||||
|
typename std::make_unsigned<IntType>::type v = value;
|
||||||
|
|
||||||
|
std::size_t i = 0;
|
||||||
|
// if constexpr to silence the warning about shift by 8
|
||||||
|
if constexpr (sizeof(IntType) > 1)
|
||||||
|
{
|
||||||
|
for (; i + 1 < sizeof(IntType); ++i)
|
||||||
|
{
|
||||||
|
u[i] = (std::uint8_t)v;
|
||||||
|
v >>= 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
u[i] = (std::uint8_t)v;
|
||||||
|
|
||||||
|
stream.write(reinterpret_cast<char*>(u), sizeof(IntType));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// read_little_endian(s, out, N) : read integers in bulk from a little indian stream.
|
||||||
|
// This reads N integers from stream s and put them in array out.
|
||||||
|
template <typename IntType>
|
||||||
|
inline void read_little_endian(std::istream& stream, IntType* out, std::size_t count) {
|
||||||
|
if (IsLittleEndian)
|
||||||
|
stream.read(reinterpret_cast<char*>(out), sizeof(IntType) * count);
|
||||||
|
else
|
||||||
|
for (std::size_t i = 0; i < count; ++i)
|
||||||
|
out[i] = read_little_endian<IntType>(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
// write_little_endian(s, values, N) : write integers in bulk to a little indian stream.
|
||||||
|
// This takes N integers from array values and writes them on stream s.
|
||||||
|
template <typename IntType>
|
||||||
|
inline void write_little_endian(std::ostream& stream, const IntType* values, std::size_t count) {
|
||||||
|
if (IsLittleEndian)
|
||||||
|
stream.write(reinterpret_cast<const char*>(values), sizeof(IntType) * count);
|
||||||
|
else
|
||||||
|
for (std::size_t i = 0; i < count; ++i)
|
||||||
|
write_little_endian<IntType>(stream, values[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish::Eval::NNUE
|
||||||
|
|
||||||
#endif // #ifndef NNUE_COMMON_H_INCLUDED
|
#endif // #ifndef NNUE_COMMON_H_INCLUDED
|
||||||
|
|||||||
+368
-196
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -23,72 +23,213 @@
|
|||||||
|
|
||||||
#include "nnue_common.h"
|
#include "nnue_common.h"
|
||||||
#include "nnue_architecture.h"
|
#include "nnue_architecture.h"
|
||||||
#include "features/index_list.h"
|
|
||||||
|
|
||||||
#include <cstring> // std::memset()
|
#include <cstring> // std::memset()
|
||||||
|
|
||||||
namespace Eval::NNUE {
|
namespace Stockfish::Eval::NNUE {
|
||||||
|
|
||||||
|
using BiasType = std::int16_t;
|
||||||
|
using WeightType = std::int16_t;
|
||||||
|
using PSQTWeightType = std::int32_t;
|
||||||
|
|
||||||
// If vector instructions are enabled, we update and refresh the
|
// If vector instructions are enabled, we update and refresh the
|
||||||
// accumulator tile by tile such that each tile fits in the CPU's
|
// accumulator tile by tile such that each tile fits in the CPU's
|
||||||
// vector registers.
|
// vector registers.
|
||||||
#define VECTOR
|
#define VECTOR
|
||||||
|
|
||||||
|
static_assert(PSQTBuckets % 8 == 0,
|
||||||
|
"Per feature PSQT values cannot be processed at granularity lower than 8 at a time.");
|
||||||
|
|
||||||
#ifdef USE_AVX512
|
#ifdef USE_AVX512
|
||||||
typedef __m512i vec_t;
|
typedef __m512i vec_t;
|
||||||
|
typedef __m256i psqt_vec_t;
|
||||||
#define vec_load(a) _mm512_load_si512(a)
|
#define vec_load(a) _mm512_load_si512(a)
|
||||||
#define vec_store(a,b) _mm512_store_si512(a,b)
|
#define vec_store(a,b) _mm512_store_si512(a,b)
|
||||||
#define vec_add_16(a,b) _mm512_add_epi16(a,b)
|
#define vec_add_16(a,b) _mm512_add_epi16(a,b)
|
||||||
#define vec_sub_16(a,b) _mm512_sub_epi16(a,b)
|
#define vec_sub_16(a,b) _mm512_sub_epi16(a,b)
|
||||||
static constexpr IndexType kNumRegs = 8; // only 8 are needed
|
#define vec_mul_16(a,b) _mm512_mullo_epi16(a,b)
|
||||||
|
#define vec_zero() _mm512_setzero_epi32()
|
||||||
|
#define vec_set_16(a) _mm512_set1_epi16(a)
|
||||||
|
#define vec_max_16(a,b) _mm512_max_epi16(a,b)
|
||||||
|
#define vec_min_16(a,b) _mm512_min_epi16(a,b)
|
||||||
|
inline vec_t vec_msb_pack_16(vec_t a, vec_t b){
|
||||||
|
vec_t compacted = _mm512_packs_epi16(_mm512_srli_epi16(a,7),_mm512_srli_epi16(b,7));
|
||||||
|
return _mm512_permutexvar_epi64(_mm512_setr_epi64(0, 2, 4, 6, 1, 3, 5, 7), compacted);
|
||||||
|
}
|
||||||
|
#define vec_load_psqt(a) _mm256_load_si256(a)
|
||||||
|
#define vec_store_psqt(a,b) _mm256_store_si256(a,b)
|
||||||
|
#define vec_add_psqt_32(a,b) _mm256_add_epi32(a,b)
|
||||||
|
#define vec_sub_psqt_32(a,b) _mm256_sub_epi32(a,b)
|
||||||
|
#define vec_zero_psqt() _mm256_setzero_si256()
|
||||||
|
#define NumRegistersSIMD 32
|
||||||
|
#define MaxChunkSize 64
|
||||||
|
|
||||||
#elif USE_AVX2
|
#elif USE_AVX2
|
||||||
typedef __m256i vec_t;
|
typedef __m256i vec_t;
|
||||||
|
typedef __m256i psqt_vec_t;
|
||||||
#define vec_load(a) _mm256_load_si256(a)
|
#define vec_load(a) _mm256_load_si256(a)
|
||||||
#define vec_store(a,b) _mm256_store_si256(a,b)
|
#define vec_store(a,b) _mm256_store_si256(a,b)
|
||||||
#define vec_add_16(a,b) _mm256_add_epi16(a,b)
|
#define vec_add_16(a,b) _mm256_add_epi16(a,b)
|
||||||
#define vec_sub_16(a,b) _mm256_sub_epi16(a,b)
|
#define vec_sub_16(a,b) _mm256_sub_epi16(a,b)
|
||||||
static constexpr IndexType kNumRegs = 16;
|
#define vec_mul_16(a,b) _mm256_mullo_epi16(a,b)
|
||||||
|
#define vec_zero() _mm256_setzero_si256()
|
||||||
|
#define vec_set_16(a) _mm256_set1_epi16(a)
|
||||||
|
#define vec_max_16(a,b) _mm256_max_epi16(a,b)
|
||||||
|
#define vec_min_16(a,b) _mm256_min_epi16(a,b)
|
||||||
|
inline vec_t vec_msb_pack_16(vec_t a, vec_t b){
|
||||||
|
vec_t compacted = _mm256_packs_epi16(_mm256_srli_epi16(a,7), _mm256_srli_epi16(b,7));
|
||||||
|
return _mm256_permute4x64_epi64(compacted, 0b11011000);
|
||||||
|
}
|
||||||
|
#define vec_load_psqt(a) _mm256_load_si256(a)
|
||||||
|
#define vec_store_psqt(a,b) _mm256_store_si256(a,b)
|
||||||
|
#define vec_add_psqt_32(a,b) _mm256_add_epi32(a,b)
|
||||||
|
#define vec_sub_psqt_32(a,b) _mm256_sub_epi32(a,b)
|
||||||
|
#define vec_zero_psqt() _mm256_setzero_si256()
|
||||||
|
#define NumRegistersSIMD 16
|
||||||
|
#define MaxChunkSize 32
|
||||||
|
|
||||||
#elif USE_SSE2
|
#elif USE_SSE2
|
||||||
typedef __m128i vec_t;
|
typedef __m128i vec_t;
|
||||||
|
typedef __m128i psqt_vec_t;
|
||||||
#define vec_load(a) (*(a))
|
#define vec_load(a) (*(a))
|
||||||
#define vec_store(a,b) *(a)=(b)
|
#define vec_store(a,b) *(a)=(b)
|
||||||
#define vec_add_16(a,b) _mm_add_epi16(a,b)
|
#define vec_add_16(a,b) _mm_add_epi16(a,b)
|
||||||
#define vec_sub_16(a,b) _mm_sub_epi16(a,b)
|
#define vec_sub_16(a,b) _mm_sub_epi16(a,b)
|
||||||
static constexpr IndexType kNumRegs = Is64Bit ? 16 : 8;
|
#define vec_mul_16(a,b) _mm_mullo_epi16(a,b)
|
||||||
|
#define vec_zero() _mm_setzero_si128()
|
||||||
|
#define vec_set_16(a) _mm_set1_epi16(a)
|
||||||
|
#define vec_max_16(a,b) _mm_max_epi16(a,b)
|
||||||
|
#define vec_min_16(a,b) _mm_min_epi16(a,b)
|
||||||
|
#define vec_msb_pack_16(a,b) _mm_packs_epi16(_mm_srli_epi16(a,7),_mm_srli_epi16(b,7))
|
||||||
|
#define vec_load_psqt(a) (*(a))
|
||||||
|
#define vec_store_psqt(a,b) *(a)=(b)
|
||||||
|
#define vec_add_psqt_32(a,b) _mm_add_epi32(a,b)
|
||||||
|
#define vec_sub_psqt_32(a,b) _mm_sub_epi32(a,b)
|
||||||
|
#define vec_zero_psqt() _mm_setzero_si128()
|
||||||
|
#define NumRegistersSIMD (Is64Bit ? 16 : 8)
|
||||||
|
#define MaxChunkSize 16
|
||||||
|
|
||||||
#elif USE_MMX
|
#elif USE_MMX
|
||||||
typedef __m64 vec_t;
|
typedef __m64 vec_t;
|
||||||
|
typedef __m64 psqt_vec_t;
|
||||||
#define vec_load(a) (*(a))
|
#define vec_load(a) (*(a))
|
||||||
#define vec_store(a,b) *(a)=(b)
|
#define vec_store(a,b) *(a)=(b)
|
||||||
#define vec_add_16(a,b) _mm_add_pi16(a,b)
|
#define vec_add_16(a,b) _mm_add_pi16(a,b)
|
||||||
#define vec_sub_16(a,b) _mm_sub_pi16(a,b)
|
#define vec_sub_16(a,b) _mm_sub_pi16(a,b)
|
||||||
static constexpr IndexType kNumRegs = 8;
|
#define vec_mul_16(a,b) _mm_mullo_pi16(a,b)
|
||||||
|
#define vec_zero() _mm_setzero_si64()
|
||||||
|
#define vec_set_16(a) _mm_set1_pi16(a)
|
||||||
|
inline vec_t vec_max_16(vec_t a,vec_t b){
|
||||||
|
vec_t comparison = _mm_cmpgt_pi16(a,b);
|
||||||
|
return _mm_or_si64(_mm_and_si64(comparison, a), _mm_andnot_si64(comparison, b));
|
||||||
|
}
|
||||||
|
inline vec_t vec_min_16(vec_t a,vec_t b){
|
||||||
|
vec_t comparison = _mm_cmpgt_pi16(a,b);
|
||||||
|
return _mm_or_si64(_mm_and_si64(comparison, b), _mm_andnot_si64(comparison, a));
|
||||||
|
}
|
||||||
|
#define vec_msb_pack_16(a,b) _mm_packs_pi16(_mm_srli_pi16(a,7),_mm_srli_pi16(b,7))
|
||||||
|
#define vec_load_psqt(a) (*(a))
|
||||||
|
#define vec_store_psqt(a,b) *(a)=(b)
|
||||||
|
#define vec_add_psqt_32(a,b) _mm_add_pi32(a,b)
|
||||||
|
#define vec_sub_psqt_32(a,b) _mm_sub_pi32(a,b)
|
||||||
|
#define vec_zero_psqt() _mm_setzero_si64()
|
||||||
|
#define vec_cleanup() _mm_empty()
|
||||||
|
#define NumRegistersSIMD 8
|
||||||
|
#define MaxChunkSize 8
|
||||||
|
|
||||||
#elif USE_NEON
|
#elif USE_NEON
|
||||||
typedef int16x8_t vec_t;
|
typedef int16x8_t vec_t;
|
||||||
|
typedef int32x4_t psqt_vec_t;
|
||||||
#define vec_load(a) (*(a))
|
#define vec_load(a) (*(a))
|
||||||
#define vec_store(a,b) *(a)=(b)
|
#define vec_store(a,b) *(a)=(b)
|
||||||
#define vec_add_16(a,b) vaddq_s16(a,b)
|
#define vec_add_16(a,b) vaddq_s16(a,b)
|
||||||
#define vec_sub_16(a,b) vsubq_s16(a,b)
|
#define vec_sub_16(a,b) vsubq_s16(a,b)
|
||||||
static constexpr IndexType kNumRegs = 16;
|
#define vec_mul_16(a,b) vmulq_s16(a,b)
|
||||||
|
#define vec_zero() vec_t{0}
|
||||||
|
#define vec_set_16(a) vdupq_n_s16(a)
|
||||||
|
#define vec_max_16(a,b) vmaxq_s16(a,b)
|
||||||
|
#define vec_min_16(a,b) vminq_s16(a,b)
|
||||||
|
inline vec_t vec_msb_pack_16(vec_t a, vec_t b){
|
||||||
|
const int8x8_t shifta = vshrn_n_s16(a, 7);
|
||||||
|
const int8x8_t shiftb = vshrn_n_s16(b, 7);
|
||||||
|
const int8x16_t compacted = vcombine_s8(shifta,shiftb);
|
||||||
|
return *reinterpret_cast<const vec_t*> (&compacted);
|
||||||
|
}
|
||||||
|
#define vec_load_psqt(a) (*(a))
|
||||||
|
#define vec_store_psqt(a,b) *(a)=(b)
|
||||||
|
#define vec_add_psqt_32(a,b) vaddq_s32(a,b)
|
||||||
|
#define vec_sub_psqt_32(a,b) vsubq_s32(a,b)
|
||||||
|
#define vec_zero_psqt() psqt_vec_t{0}
|
||||||
|
#define NumRegistersSIMD 16
|
||||||
|
#define MaxChunkSize 16
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#undef VECTOR
|
#undef VECTOR
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef VECTOR
|
||||||
|
|
||||||
|
// Compute optimal SIMD register count for feature transformer accumulation.
|
||||||
|
|
||||||
|
// We use __m* types as template arguments, which causes GCC to emit warnings
|
||||||
|
// about losing some attribute information. This is irrelevant to us as we
|
||||||
|
// only take their size, so the following pragma are harmless.
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Wignored-attributes"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template <typename SIMDRegisterType,
|
||||||
|
typename LaneType,
|
||||||
|
int NumLanes,
|
||||||
|
int MaxRegisters>
|
||||||
|
static constexpr int BestRegisterCount()
|
||||||
|
{
|
||||||
|
#define RegisterSize sizeof(SIMDRegisterType)
|
||||||
|
#define LaneSize sizeof(LaneType)
|
||||||
|
|
||||||
|
static_assert(RegisterSize >= LaneSize);
|
||||||
|
static_assert(MaxRegisters <= NumRegistersSIMD);
|
||||||
|
static_assert(MaxRegisters > 0);
|
||||||
|
static_assert(NumRegistersSIMD > 0);
|
||||||
|
static_assert(RegisterSize % LaneSize == 0);
|
||||||
|
static_assert((NumLanes * LaneSize) % RegisterSize == 0);
|
||||||
|
|
||||||
|
const int ideal = (NumLanes * LaneSize) / RegisterSize;
|
||||||
|
if (ideal <= MaxRegisters)
|
||||||
|
return ideal;
|
||||||
|
|
||||||
|
// Look for the largest divisor of the ideal register count that is smaller than MaxRegisters
|
||||||
|
for (int divisor = MaxRegisters; divisor > 1; --divisor)
|
||||||
|
if (ideal % divisor == 0)
|
||||||
|
return divisor;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr int NumRegs = BestRegisterCount<vec_t, WeightType, TransformedFeatureDimensions, NumRegistersSIMD>();
|
||||||
|
static constexpr int NumPsqtRegs = BestRegisterCount<psqt_vec_t, PSQTWeightType, PSQTBuckets, NumRegistersSIMD>();
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Input feature converter
|
// Input feature converter
|
||||||
class FeatureTransformer {
|
class FeatureTransformer {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Number of output dimensions for one side
|
// Number of output dimensions for one side
|
||||||
static constexpr IndexType kHalfDimensions = kTransformedFeatureDimensions;
|
static constexpr IndexType HalfDimensions = TransformedFeatureDimensions;
|
||||||
|
|
||||||
#ifdef VECTOR
|
#ifdef VECTOR
|
||||||
static constexpr IndexType kTileHeight = kNumRegs * sizeof(vec_t) / 2;
|
static constexpr IndexType TileHeight = NumRegs * sizeof(vec_t) / 2;
|
||||||
static_assert(kHalfDimensions % kTileHeight == 0, "kTileHeight must divide kHalfDimensions");
|
static constexpr IndexType PsqtTileHeight = NumPsqtRegs * sizeof(psqt_vec_t) / 4;
|
||||||
|
static_assert(HalfDimensions % TileHeight == 0, "TileHeight must divide HalfDimensions");
|
||||||
|
static_assert(PSQTBuckets % PsqtTileHeight == 0, "PsqtTileHeight must divide PSQTBuckets");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -96,174 +237,138 @@ namespace Eval::NNUE {
|
|||||||
using OutputType = TransformedFeatureType;
|
using OutputType = TransformedFeatureType;
|
||||||
|
|
||||||
// Number of input/output dimensions
|
// Number of input/output dimensions
|
||||||
static constexpr IndexType kInputDimensions = RawFeatures::kDimensions;
|
static constexpr IndexType InputDimensions = FeatureSet::Dimensions;
|
||||||
static constexpr IndexType kOutputDimensions = kHalfDimensions * 2;
|
static constexpr IndexType OutputDimensions = HalfDimensions;
|
||||||
|
|
||||||
// Size of forward propagation buffer
|
// Size of forward propagation buffer
|
||||||
static constexpr std::size_t kBufferSize =
|
static constexpr std::size_t BufferSize =
|
||||||
kOutputDimensions * sizeof(OutputType);
|
OutputDimensions * sizeof(OutputType);
|
||||||
|
|
||||||
// Hash value embedded in the evaluation file
|
// Hash value embedded in the evaluation file
|
||||||
static constexpr std::uint32_t GetHashValue() {
|
static constexpr std::uint32_t get_hash_value() {
|
||||||
|
return FeatureSet::HashValue ^ (OutputDimensions * 2);
|
||||||
return RawFeatures::kHashValue ^ kOutputDimensions;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read network parameters
|
// Read network parameters
|
||||||
bool ReadParameters(std::istream& stream) {
|
bool read_parameters(std::istream& stream) {
|
||||||
|
|
||||||
|
read_little_endian<BiasType >(stream, biases , HalfDimensions );
|
||||||
|
read_little_endian<WeightType >(stream, weights , HalfDimensions * InputDimensions);
|
||||||
|
read_little_endian<PSQTWeightType>(stream, psqtWeights, PSQTBuckets * InputDimensions);
|
||||||
|
|
||||||
|
return !stream.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write network parameters
|
||||||
|
bool write_parameters(std::ostream& stream) const {
|
||||||
|
|
||||||
|
write_little_endian<BiasType >(stream, biases , HalfDimensions );
|
||||||
|
write_little_endian<WeightType >(stream, weights , HalfDimensions * InputDimensions);
|
||||||
|
write_little_endian<PSQTWeightType>(stream, psqtWeights, PSQTBuckets * InputDimensions);
|
||||||
|
|
||||||
for (std::size_t i = 0; i < kHalfDimensions; ++i)
|
|
||||||
biases_[i] = read_little_endian<BiasType>(stream);
|
|
||||||
for (std::size_t i = 0; i < kHalfDimensions * kInputDimensions; ++i)
|
|
||||||
weights_[i] = read_little_endian<WeightType>(stream);
|
|
||||||
return !stream.fail();
|
return !stream.fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert input features
|
// Convert input features
|
||||||
void Transform(const Position& pos, OutputType* output) const {
|
std::int32_t transform(const Position& pos, OutputType* output, int bucket) const {
|
||||||
|
update_accumulator<WHITE>(pos);
|
||||||
UpdateAccumulator(pos, WHITE);
|
update_accumulator<BLACK>(pos);
|
||||||
UpdateAccumulator(pos, BLACK);
|
|
||||||
|
|
||||||
const auto& accumulation = pos.state()->accumulator.accumulation;
|
|
||||||
|
|
||||||
#if defined(USE_AVX512)
|
|
||||||
constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth * 2);
|
|
||||||
static_assert(kHalfDimensions % (kSimdWidth * 2) == 0);
|
|
||||||
const __m512i kControl = _mm512_setr_epi64(0, 2, 4, 6, 1, 3, 5, 7);
|
|
||||||
const __m512i kZero = _mm512_setzero_si512();
|
|
||||||
|
|
||||||
#elif defined(USE_AVX2)
|
|
||||||
constexpr IndexType kNumChunks = kHalfDimensions / kSimdWidth;
|
|
||||||
constexpr int kControl = 0b11011000;
|
|
||||||
const __m256i kZero = _mm256_setzero_si256();
|
|
||||||
|
|
||||||
#elif defined(USE_SSE2)
|
|
||||||
constexpr IndexType kNumChunks = kHalfDimensions / kSimdWidth;
|
|
||||||
|
|
||||||
#ifdef USE_SSE41
|
|
||||||
const __m128i kZero = _mm_setzero_si128();
|
|
||||||
#else
|
|
||||||
const __m128i k0x80s = _mm_set1_epi8(-128);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#elif defined(USE_MMX)
|
|
||||||
constexpr IndexType kNumChunks = kHalfDimensions / kSimdWidth;
|
|
||||||
const __m64 k0x80s = _mm_set1_pi8(-128);
|
|
||||||
|
|
||||||
#elif defined(USE_NEON)
|
|
||||||
constexpr IndexType kNumChunks = kHalfDimensions / (kSimdWidth / 2);
|
|
||||||
const int8x8_t kZero = {0};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
const Color perspectives[2] = {pos.side_to_move(), ~pos.side_to_move()};
|
const Color perspectives[2] = {pos.side_to_move(), ~pos.side_to_move()};
|
||||||
for (IndexType p = 0; p < 2; ++p) {
|
const auto& accumulation = pos.state()->accumulator.accumulation;
|
||||||
const IndexType offset = kHalfDimensions * p;
|
const auto& psqtAccumulation = pos.state()->accumulator.psqtAccumulation;
|
||||||
|
|
||||||
#if defined(USE_AVX512)
|
const auto psqt = (
|
||||||
auto out = reinterpret_cast<__m512i*>(&output[offset]);
|
psqtAccumulation[perspectives[0]][bucket]
|
||||||
for (IndexType j = 0; j < kNumChunks; ++j) {
|
- psqtAccumulation[perspectives[1]][bucket]
|
||||||
__m512i sum0 = _mm512_load_si512(
|
) / 2;
|
||||||
&reinterpret_cast<const __m512i*>(accumulation[perspectives[p]][0])[j * 2 + 0]);
|
|
||||||
__m512i sum1 = _mm512_load_si512(
|
|
||||||
&reinterpret_cast<const __m512i*>(accumulation[perspectives[p]][0])[j * 2 + 1]);
|
|
||||||
_mm512_store_si512(&out[j], _mm512_permutexvar_epi64(kControl,
|
|
||||||
_mm512_max_epi8(_mm512_packs_epi16(sum0, sum1), kZero)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#elif defined(USE_AVX2)
|
|
||||||
auto out = reinterpret_cast<__m256i*>(&output[offset]);
|
|
||||||
for (IndexType j = 0; j < kNumChunks; ++j) {
|
|
||||||
__m256i sum0 = _mm256_load_si256(
|
|
||||||
&reinterpret_cast<const __m256i*>(accumulation[perspectives[p]][0])[j * 2 + 0]);
|
|
||||||
__m256i sum1 = _mm256_load_si256(
|
|
||||||
&reinterpret_cast<const __m256i*>(accumulation[perspectives[p]][0])[j * 2 + 1]);
|
|
||||||
_mm256_store_si256(&out[j], _mm256_permute4x64_epi64(_mm256_max_epi8(
|
|
||||||
_mm256_packs_epi16(sum0, sum1), kZero), kControl));
|
|
||||||
}
|
|
||||||
|
|
||||||
#elif defined(USE_SSE2)
|
for (IndexType p = 0; p < 2; ++p)
|
||||||
auto out = reinterpret_cast<__m128i*>(&output[offset]);
|
{
|
||||||
for (IndexType j = 0; j < kNumChunks; ++j) {
|
const IndexType offset = (HalfDimensions / 2) * p;
|
||||||
__m128i sum0 = _mm_load_si128(&reinterpret_cast<const __m128i*>(
|
|
||||||
accumulation[perspectives[p]][0])[j * 2 + 0]);
|
|
||||||
__m128i sum1 = _mm_load_si128(&reinterpret_cast<const __m128i*>(
|
|
||||||
accumulation[perspectives[p]][0])[j * 2 + 1]);
|
|
||||||
const __m128i packedbytes = _mm_packs_epi16(sum0, sum1);
|
|
||||||
|
|
||||||
_mm_store_si128(&out[j],
|
#if defined(VECTOR)
|
||||||
|
|
||||||
#ifdef USE_SSE41
|
constexpr IndexType OutputChunkSize = MaxChunkSize;
|
||||||
_mm_max_epi8(packedbytes, kZero)
|
static_assert((HalfDimensions / 2) % OutputChunkSize == 0);
|
||||||
#else
|
constexpr IndexType NumOutputChunks = HalfDimensions / 2 / OutputChunkSize;
|
||||||
_mm_subs_epi8(_mm_adds_epi8(packedbytes, k0x80s), k0x80s)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
);
|
vec_t Zero = vec_zero();
|
||||||
}
|
vec_t One = vec_set_16(127);
|
||||||
|
|
||||||
#elif defined(USE_MMX)
|
const vec_t* in0 = reinterpret_cast<const vec_t*>(&(accumulation[perspectives[p]][0]));
|
||||||
auto out = reinterpret_cast<__m64*>(&output[offset]);
|
const vec_t* in1 = reinterpret_cast<const vec_t*>(&(accumulation[perspectives[p]][HalfDimensions / 2]));
|
||||||
for (IndexType j = 0; j < kNumChunks; ++j) {
|
vec_t* out = reinterpret_cast< vec_t*>(output + offset);
|
||||||
__m64 sum0 = *(&reinterpret_cast<const __m64*>(
|
|
||||||
accumulation[perspectives[p]][0])[j * 2 + 0]);
|
|
||||||
__m64 sum1 = *(&reinterpret_cast<const __m64*>(
|
|
||||||
accumulation[perspectives[p]][0])[j * 2 + 1]);
|
|
||||||
const __m64 packedbytes = _mm_packs_pi16(sum0, sum1);
|
|
||||||
out[j] = _mm_subs_pi8(_mm_adds_pi8(packedbytes, k0x80s), k0x80s);
|
|
||||||
}
|
|
||||||
|
|
||||||
#elif defined(USE_NEON)
|
for (IndexType j = 0; j < NumOutputChunks; j += 1)
|
||||||
const auto out = reinterpret_cast<int8x8_t*>(&output[offset]);
|
{
|
||||||
for (IndexType j = 0; j < kNumChunks; ++j) {
|
const vec_t sum0a = vec_max_16(vec_min_16(in0[j * 2 + 0], One), Zero);
|
||||||
int16x8_t sum = reinterpret_cast<const int16x8_t*>(
|
const vec_t sum0b = vec_max_16(vec_min_16(in0[j * 2 + 1], One), Zero);
|
||||||
accumulation[perspectives[p]][0])[j];
|
const vec_t sum1a = vec_max_16(vec_min_16(in1[j * 2 + 0], One), Zero);
|
||||||
out[j] = vmax_s8(vqmovn_s16(sum), kZero);
|
const vec_t sum1b = vec_max_16(vec_min_16(in1[j * 2 + 1], One), Zero);
|
||||||
|
|
||||||
|
const vec_t pa = vec_mul_16(sum0a, sum1a);
|
||||||
|
const vec_t pb = vec_mul_16(sum0b, sum1b);
|
||||||
|
|
||||||
|
out[j] = vec_msb_pack_16(pa, pb);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
for (IndexType j = 0; j < kHalfDimensions; ++j) {
|
|
||||||
BiasType sum = accumulation[static_cast<int>(perspectives[p])][0][j];
|
for (IndexType j = 0; j < HalfDimensions / 2; ++j) {
|
||||||
output[offset + j] = static_cast<OutputType>(
|
BiasType sum0 = accumulation[static_cast<int>(perspectives[p])][j + 0];
|
||||||
std::max<int>(0, std::min<int>(127, sum)));
|
BiasType sum1 = accumulation[static_cast<int>(perspectives[p])][j + HalfDimensions / 2];
|
||||||
|
sum0 = std::max<int>(0, std::min<int>(127, sum0));
|
||||||
|
sum1 = std::max<int>(0, std::min<int>(127, sum1));
|
||||||
|
output[offset + j] = static_cast<OutputType>(sum0 * sum1 / 128);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(vec_cleanup)
|
||||||
|
vec_cleanup();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
return psqt;
|
||||||
#if defined(USE_MMX)
|
|
||||||
_mm_empty();
|
} // end of function transform()
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void UpdateAccumulator(const Position& pos, const Color c) const {
|
template<Color Perspective>
|
||||||
|
void update_accumulator(const Position& pos) const {
|
||||||
|
|
||||||
|
// The size must be enough to contain the largest possible update.
|
||||||
|
// That might depend on the feature set and generally relies on the
|
||||||
|
// feature set's update cost calculation to be correct and never
|
||||||
|
// allow updates with more added/removed features than MaxActiveDimensions.
|
||||||
|
|
||||||
#ifdef VECTOR
|
#ifdef VECTOR
|
||||||
// Gcc-10.2 unnecessarily spills AVX2 registers if this array
|
// Gcc-10.2 unnecessarily spills AVX2 registers if this array
|
||||||
// is defined in the VECTOR code below, once in each branch
|
// is defined in the VECTOR code below, once in each branch
|
||||||
vec_t acc[kNumRegs];
|
vec_t acc[NumRegs];
|
||||||
|
psqt_vec_t psqt[NumPsqtRegs];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Look for a usable accumulator of an earlier position. We keep track
|
// Look for a usable accumulator of an earlier position. We keep track
|
||||||
// of the estimated gain in terms of features to be added/subtracted.
|
// of the estimated gain in terms of features to be added/subtracted.
|
||||||
StateInfo *st = pos.state(), *next = nullptr;
|
StateInfo *st = pos.state(), *next = nullptr;
|
||||||
int gain = pos.count<ALL_PIECES>() - 2;
|
int gain = FeatureSet::refresh_cost(pos);
|
||||||
while (st->accumulator.state[c] == EMPTY)
|
while (st->previous && !st->accumulator.computed[Perspective])
|
||||||
{
|
{
|
||||||
auto& dp = st->dirtyPiece;
|
// This governs when a full feature refresh is needed and how many
|
||||||
// The first condition tests whether an incremental update is
|
// updates are better than just one full refresh.
|
||||||
// possible at all: if this side's king has moved, it is not possible.
|
if ( FeatureSet::requires_refresh(st, Perspective)
|
||||||
static_assert(std::is_same_v<RawFeatures::SortedTriggerSet,
|
|| (gain -= FeatureSet::update_cost(st) + 1) < 0)
|
||||||
Features::CompileTimeList<Features::TriggerEvent, Features::TriggerEvent::kFriendKingMoved>>,
|
|
||||||
"Current code assumes that only kFriendlyKingMoved refresh trigger is being used.");
|
|
||||||
if ( dp.piece[0] == make_piece(c, KING)
|
|
||||||
|| (gain -= dp.dirty_num + 1) < 0)
|
|
||||||
break;
|
break;
|
||||||
next = st;
|
next = st;
|
||||||
st = st->previous;
|
st = st->previous;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (st->accumulator.state[c] == COMPUTED)
|
if (st->accumulator.computed[Perspective])
|
||||||
{
|
{
|
||||||
if (next == nullptr)
|
if (next == nullptr)
|
||||||
return;
|
return;
|
||||||
@@ -271,85 +376,129 @@ namespace Eval::NNUE {
|
|||||||
// Update incrementally in two steps. First, we update the "next"
|
// Update incrementally in two steps. First, we update the "next"
|
||||||
// accumulator. Then, we update the current accumulator (pos.state()).
|
// accumulator. Then, we update the current accumulator (pos.state()).
|
||||||
|
|
||||||
// Gather all features to be updated. This code assumes HalfKP features
|
// Gather all features to be updated.
|
||||||
// only and doesn't support refresh triggers.
|
const Square ksq = pos.square<KING>(Perspective);
|
||||||
static_assert(std::is_same_v<Features::FeatureSet<Features::HalfKP<Features::Side::kFriend>>,
|
FeatureSet::IndexList removed[2], added[2];
|
||||||
RawFeatures>);
|
FeatureSet::append_changed_indices<Perspective>(
|
||||||
Features::IndexList removed[2], added[2];
|
ksq, next->dirtyPiece, removed[0], added[0]);
|
||||||
Features::HalfKP<Features::Side::kFriend>::AppendChangedIndices(pos,
|
|
||||||
next->dirtyPiece, c, &removed[0], &added[0]);
|
|
||||||
for (StateInfo *st2 = pos.state(); st2 != next; st2 = st2->previous)
|
for (StateInfo *st2 = pos.state(); st2 != next; st2 = st2->previous)
|
||||||
Features::HalfKP<Features::Side::kFriend>::AppendChangedIndices(pos,
|
FeatureSet::append_changed_indices<Perspective>(
|
||||||
st2->dirtyPiece, c, &removed[1], &added[1]);
|
ksq, st2->dirtyPiece, removed[1], added[1]);
|
||||||
|
|
||||||
// Mark the accumulators as computed.
|
// Mark the accumulators as computed.
|
||||||
next->accumulator.state[c] = COMPUTED;
|
next->accumulator.computed[Perspective] = true;
|
||||||
pos.state()->accumulator.state[c] = COMPUTED;
|
pos.state()->accumulator.computed[Perspective] = true;
|
||||||
|
|
||||||
// Now update the accumulators listed in info[], where the last element is a sentinel.
|
// Now update the accumulators listed in states_to_update[], where the last element is a sentinel.
|
||||||
StateInfo *info[3] =
|
StateInfo *states_to_update[3] =
|
||||||
{ next, next == pos.state() ? nullptr : pos.state(), nullptr };
|
{ next, next == pos.state() ? nullptr : pos.state(), nullptr };
|
||||||
#ifdef VECTOR
|
#ifdef VECTOR
|
||||||
for (IndexType j = 0; j < kHalfDimensions / kTileHeight; ++j)
|
for (IndexType j = 0; j < HalfDimensions / TileHeight; ++j)
|
||||||
{
|
{
|
||||||
// Load accumulator
|
// Load accumulator
|
||||||
auto accTile = reinterpret_cast<vec_t*>(
|
auto accTile = reinterpret_cast<vec_t*>(
|
||||||
&st->accumulator.accumulation[c][0][j * kTileHeight]);
|
&st->accumulator.accumulation[Perspective][j * TileHeight]);
|
||||||
for (IndexType k = 0; k < kNumRegs; ++k)
|
for (IndexType k = 0; k < NumRegs; ++k)
|
||||||
acc[k] = vec_load(&accTile[k]);
|
acc[k] = vec_load(&accTile[k]);
|
||||||
|
|
||||||
for (IndexType i = 0; info[i]; ++i)
|
for (IndexType i = 0; states_to_update[i]; ++i)
|
||||||
{
|
{
|
||||||
// Difference calculation for the deactivated features
|
// Difference calculation for the deactivated features
|
||||||
for (const auto index : removed[i])
|
for (const auto index : removed[i])
|
||||||
{
|
{
|
||||||
const IndexType offset = kHalfDimensions * index + j * kTileHeight;
|
const IndexType offset = HalfDimensions * index + j * TileHeight;
|
||||||
auto column = reinterpret_cast<const vec_t*>(&weights_[offset]);
|
auto column = reinterpret_cast<const vec_t*>(&weights[offset]);
|
||||||
for (IndexType k = 0; k < kNumRegs; ++k)
|
for (IndexType k = 0; k < NumRegs; ++k)
|
||||||
acc[k] = vec_sub_16(acc[k], column[k]);
|
acc[k] = vec_sub_16(acc[k], column[k]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Difference calculation for the activated features
|
// Difference calculation for the activated features
|
||||||
for (const auto index : added[i])
|
for (const auto index : added[i])
|
||||||
{
|
{
|
||||||
const IndexType offset = kHalfDimensions * index + j * kTileHeight;
|
const IndexType offset = HalfDimensions * index + j * TileHeight;
|
||||||
auto column = reinterpret_cast<const vec_t*>(&weights_[offset]);
|
auto column = reinterpret_cast<const vec_t*>(&weights[offset]);
|
||||||
for (IndexType k = 0; k < kNumRegs; ++k)
|
for (IndexType k = 0; k < NumRegs; ++k)
|
||||||
acc[k] = vec_add_16(acc[k], column[k]);
|
acc[k] = vec_add_16(acc[k], column[k]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store accumulator
|
// Store accumulator
|
||||||
accTile = reinterpret_cast<vec_t*>(
|
accTile = reinterpret_cast<vec_t*>(
|
||||||
&info[i]->accumulator.accumulation[c][0][j * kTileHeight]);
|
&states_to_update[i]->accumulator.accumulation[Perspective][j * TileHeight]);
|
||||||
for (IndexType k = 0; k < kNumRegs; ++k)
|
for (IndexType k = 0; k < NumRegs; ++k)
|
||||||
vec_store(&accTile[k], acc[k]);
|
vec_store(&accTile[k], acc[k]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
for (IndexType j = 0; j < PSQTBuckets / PsqtTileHeight; ++j)
|
||||||
for (IndexType i = 0; info[i]; ++i)
|
|
||||||
{
|
{
|
||||||
std::memcpy(info[i]->accumulator.accumulation[c][0],
|
// Load accumulator
|
||||||
st->accumulator.accumulation[c][0],
|
auto accTilePsqt = reinterpret_cast<psqt_vec_t*>(
|
||||||
kHalfDimensions * sizeof(BiasType));
|
&st->accumulator.psqtAccumulation[Perspective][j * PsqtTileHeight]);
|
||||||
st = info[i];
|
for (std::size_t k = 0; k < NumPsqtRegs; ++k)
|
||||||
|
psqt[k] = vec_load_psqt(&accTilePsqt[k]);
|
||||||
|
|
||||||
|
for (IndexType i = 0; states_to_update[i]; ++i)
|
||||||
|
{
|
||||||
// Difference calculation for the deactivated features
|
// Difference calculation for the deactivated features
|
||||||
for (const auto index : removed[i])
|
for (const auto index : removed[i])
|
||||||
{
|
{
|
||||||
const IndexType offset = kHalfDimensions * index;
|
const IndexType offset = PSQTBuckets * index + j * PsqtTileHeight;
|
||||||
|
auto columnPsqt = reinterpret_cast<const psqt_vec_t*>(&psqtWeights[offset]);
|
||||||
for (IndexType j = 0; j < kHalfDimensions; ++j)
|
for (std::size_t k = 0; k < NumPsqtRegs; ++k)
|
||||||
st->accumulator.accumulation[c][0][j] -= weights_[offset + j];
|
psqt[k] = vec_sub_psqt_32(psqt[k], columnPsqt[k]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Difference calculation for the activated features
|
// Difference calculation for the activated features
|
||||||
for (const auto index : added[i])
|
for (const auto index : added[i])
|
||||||
{
|
{
|
||||||
const IndexType offset = kHalfDimensions * index;
|
const IndexType offset = PSQTBuckets * index + j * PsqtTileHeight;
|
||||||
|
auto columnPsqt = reinterpret_cast<const psqt_vec_t*>(&psqtWeights[offset]);
|
||||||
|
for (std::size_t k = 0; k < NumPsqtRegs; ++k)
|
||||||
|
psqt[k] = vec_add_psqt_32(psqt[k], columnPsqt[k]);
|
||||||
|
}
|
||||||
|
|
||||||
for (IndexType j = 0; j < kHalfDimensions; ++j)
|
// Store accumulator
|
||||||
st->accumulator.accumulation[c][0][j] += weights_[offset + j];
|
accTilePsqt = reinterpret_cast<psqt_vec_t*>(
|
||||||
|
&states_to_update[i]->accumulator.psqtAccumulation[Perspective][j * PsqtTileHeight]);
|
||||||
|
for (std::size_t k = 0; k < NumPsqtRegs; ++k)
|
||||||
|
vec_store_psqt(&accTilePsqt[k], psqt[k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
for (IndexType i = 0; states_to_update[i]; ++i)
|
||||||
|
{
|
||||||
|
std::memcpy(states_to_update[i]->accumulator.accumulation[Perspective],
|
||||||
|
st->accumulator.accumulation[Perspective],
|
||||||
|
HalfDimensions * sizeof(BiasType));
|
||||||
|
|
||||||
|
for (std::size_t k = 0; k < PSQTBuckets; ++k)
|
||||||
|
states_to_update[i]->accumulator.psqtAccumulation[Perspective][k] = st->accumulator.psqtAccumulation[Perspective][k];
|
||||||
|
|
||||||
|
st = states_to_update[i];
|
||||||
|
|
||||||
|
// Difference calculation for the deactivated features
|
||||||
|
for (const auto index : removed[i])
|
||||||
|
{
|
||||||
|
const IndexType offset = HalfDimensions * index;
|
||||||
|
|
||||||
|
for (IndexType j = 0; j < HalfDimensions; ++j)
|
||||||
|
st->accumulator.accumulation[Perspective][j] -= weights[offset + j];
|
||||||
|
|
||||||
|
for (std::size_t k = 0; k < PSQTBuckets; ++k)
|
||||||
|
st->accumulator.psqtAccumulation[Perspective][k] -= psqtWeights[index * PSQTBuckets + k];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Difference calculation for the activated features
|
||||||
|
for (const auto index : added[i])
|
||||||
|
{
|
||||||
|
const IndexType offset = HalfDimensions * index;
|
||||||
|
|
||||||
|
for (IndexType j = 0; j < HalfDimensions; ++j)
|
||||||
|
st->accumulator.accumulation[Perspective][j] += weights[offset + j];
|
||||||
|
|
||||||
|
for (std::size_t k = 0; k < PSQTBuckets; ++k)
|
||||||
|
st->accumulator.psqtAccumulation[Perspective][k] += psqtWeights[index * PSQTBuckets + k];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -358,43 +507,69 @@ namespace Eval::NNUE {
|
|||||||
{
|
{
|
||||||
// Refresh the accumulator
|
// Refresh the accumulator
|
||||||
auto& accumulator = pos.state()->accumulator;
|
auto& accumulator = pos.state()->accumulator;
|
||||||
accumulator.state[c] = COMPUTED;
|
accumulator.computed[Perspective] = true;
|
||||||
Features::IndexList active;
|
FeatureSet::IndexList active;
|
||||||
Features::HalfKP<Features::Side::kFriend>::AppendActiveIndices(pos, c, &active);
|
FeatureSet::append_active_indices<Perspective>(pos, active);
|
||||||
|
|
||||||
#ifdef VECTOR
|
#ifdef VECTOR
|
||||||
for (IndexType j = 0; j < kHalfDimensions / kTileHeight; ++j)
|
for (IndexType j = 0; j < HalfDimensions / TileHeight; ++j)
|
||||||
{
|
{
|
||||||
auto biasesTile = reinterpret_cast<const vec_t*>(
|
auto biasesTile = reinterpret_cast<const vec_t*>(
|
||||||
&biases_[j * kTileHeight]);
|
&biases[j * TileHeight]);
|
||||||
for (IndexType k = 0; k < kNumRegs; ++k)
|
for (IndexType k = 0; k < NumRegs; ++k)
|
||||||
acc[k] = biasesTile[k];
|
acc[k] = biasesTile[k];
|
||||||
|
|
||||||
for (const auto index : active)
|
for (const auto index : active)
|
||||||
{
|
{
|
||||||
const IndexType offset = kHalfDimensions * index + j * kTileHeight;
|
const IndexType offset = HalfDimensions * index + j * TileHeight;
|
||||||
auto column = reinterpret_cast<const vec_t*>(&weights_[offset]);
|
auto column = reinterpret_cast<const vec_t*>(&weights[offset]);
|
||||||
|
|
||||||
for (unsigned k = 0; k < kNumRegs; ++k)
|
for (unsigned k = 0; k < NumRegs; ++k)
|
||||||
acc[k] = vec_add_16(acc[k], column[k]);
|
acc[k] = vec_add_16(acc[k], column[k]);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto accTile = reinterpret_cast<vec_t*>(
|
auto accTile = reinterpret_cast<vec_t*>(
|
||||||
&accumulator.accumulation[c][0][j * kTileHeight]);
|
&accumulator.accumulation[Perspective][j * TileHeight]);
|
||||||
for (unsigned k = 0; k < kNumRegs; k++)
|
for (unsigned k = 0; k < NumRegs; k++)
|
||||||
vec_store(&accTile[k], acc[k]);
|
vec_store(&accTile[k], acc[k]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
for (IndexType j = 0; j < PSQTBuckets / PsqtTileHeight; ++j)
|
||||||
std::memcpy(accumulator.accumulation[c][0], biases_,
|
{
|
||||||
kHalfDimensions * sizeof(BiasType));
|
for (std::size_t k = 0; k < NumPsqtRegs; ++k)
|
||||||
|
psqt[k] = vec_zero_psqt();
|
||||||
|
|
||||||
for (const auto index : active)
|
for (const auto index : active)
|
||||||
{
|
{
|
||||||
const IndexType offset = kHalfDimensions * index;
|
const IndexType offset = PSQTBuckets * index + j * PsqtTileHeight;
|
||||||
|
auto columnPsqt = reinterpret_cast<const psqt_vec_t*>(&psqtWeights[offset]);
|
||||||
|
|
||||||
for (IndexType j = 0; j < kHalfDimensions; ++j)
|
for (std::size_t k = 0; k < NumPsqtRegs; ++k)
|
||||||
accumulator.accumulation[c][0][j] += weights_[offset + j];
|
psqt[k] = vec_add_psqt_32(psqt[k], columnPsqt[k]);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto accTilePsqt = reinterpret_cast<psqt_vec_t*>(
|
||||||
|
&accumulator.psqtAccumulation[Perspective][j * PsqtTileHeight]);
|
||||||
|
for (std::size_t k = 0; k < NumPsqtRegs; ++k)
|
||||||
|
vec_store_psqt(&accTilePsqt[k], psqt[k]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
std::memcpy(accumulator.accumulation[Perspective], biases,
|
||||||
|
HalfDimensions * sizeof(BiasType));
|
||||||
|
|
||||||
|
for (std::size_t k = 0; k < PSQTBuckets; ++k)
|
||||||
|
accumulator.psqtAccumulation[Perspective][k] = 0;
|
||||||
|
|
||||||
|
for (const auto index : active)
|
||||||
|
{
|
||||||
|
const IndexType offset = HalfDimensions * index;
|
||||||
|
|
||||||
|
for (IndexType j = 0; j < HalfDimensions; ++j)
|
||||||
|
accumulator.accumulation[Perspective][j] += weights[offset + j];
|
||||||
|
|
||||||
|
for (std::size_t k = 0; k < PSQTBuckets; ++k)
|
||||||
|
accumulator.psqtAccumulation[Perspective][k] += psqtWeights[index * PSQTBuckets + k];
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -404,14 +579,11 @@ namespace Eval::NNUE {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
using BiasType = std::int16_t;
|
alignas(CacheLineSize) BiasType biases[HalfDimensions];
|
||||||
using WeightType = std::int16_t;
|
alignas(CacheLineSize) WeightType weights[HalfDimensions * InputDimensions];
|
||||||
|
alignas(CacheLineSize) PSQTWeightType psqtWeights[InputDimensions * PSQTBuckets];
|
||||||
alignas(kCacheLineSize) BiasType biases_[kHalfDimensions];
|
|
||||||
alignas(kCacheLineSize)
|
|
||||||
WeightType weights_[kHalfDimensions * kInputDimensions];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Eval::NNUE
|
} // namespace Stockfish::Eval::NNUE
|
||||||
|
|
||||||
#endif // #ifndef NNUE_FEATURE_TRANSFORMER_H_INCLUDED
|
#endif // #ifndef NNUE_FEATURE_TRANSFORMER_H_INCLUDED
|
||||||
|
|||||||
+28
-23
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -24,36 +24,38 @@
|
|||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
#define V Value
|
#define V Value
|
||||||
#define S(mg, eg) make_score(mg, eg)
|
#define S(mg, eg) make_score(mg, eg)
|
||||||
|
|
||||||
// Pawn penalties
|
// Pawn penalties
|
||||||
constexpr Score Backward = S( 9, 22);
|
constexpr Score Backward = S( 6, 19);
|
||||||
constexpr Score Doubled = S(13, 51);
|
constexpr Score Doubled = S(11, 51);
|
||||||
constexpr Score DoubledEarly = S(20, 7);
|
constexpr Score DoubledEarly = S(17, 7);
|
||||||
constexpr Score Isolated = S( 3, 15);
|
constexpr Score Isolated = S( 1, 20);
|
||||||
constexpr Score WeakLever = S( 4, 58);
|
constexpr Score WeakLever = S( 2, 57);
|
||||||
constexpr Score WeakUnopposed = S(13, 24);
|
constexpr Score WeakUnopposed = S(15, 18);
|
||||||
|
|
||||||
// Bonus for blocked pawns at 5th or 6th rank
|
// Bonus for blocked pawns at 5th or 6th rank
|
||||||
constexpr Score BlockedPawn[2] = { S(-17, -6), S(-9, 2) };
|
constexpr Score BlockedPawn[2] = { S(-19, -8), S(-7, 3) };
|
||||||
|
|
||||||
constexpr Score BlockedStorm[RANK_NB] = {
|
constexpr Score BlockedStorm[RANK_NB] = {
|
||||||
S(0, 0), S(0, 0), S(75, 78), S(-8, 16), S(-6, 10), S(-6, 6), S(0, 2)
|
S(0, 0), S(0, 0), S(64, 75), S(-3, 14), S(-12, 19), S(-7, 4), S(-10, 5)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Connected pawn bonus
|
// Connected pawn bonus
|
||||||
constexpr int Connected[RANK_NB] = { 0, 5, 7, 11, 23, 48, 87 };
|
constexpr int Connected[RANK_NB] = { 0, 3, 7, 7, 15, 54, 86 };
|
||||||
|
|
||||||
// Strength of pawn shelter for our king by [distance from edge][rank].
|
// Strength of pawn shelter for our king by [distance from edge][rank].
|
||||||
// RANK_1 = 0 is used for files where we have no pawn, or pawn is behind our king.
|
// RANK_1 = 0 is used for files where we have no pawn, or pawn is behind our king.
|
||||||
constexpr Value ShelterStrength[int(FILE_NB) / 2][RANK_NB] = {
|
constexpr Value ShelterStrength[int(FILE_NB) / 2][RANK_NB] = {
|
||||||
{ V( -5), V( 82), V( 92), V( 54), V( 36), V( 22), V( 28) },
|
{ V(-2), V(85), V(95), V(53), V(39), V(23), V(25) },
|
||||||
{ V(-44), V( 63), V( 33), V(-50), V(-30), V(-12), V( -62) },
|
{ V(-55), V(64), V(32), V(-55), V(-30), V(-11), V(-61) },
|
||||||
{ V(-11), V( 77), V( 22), V( -6), V( 31), V( 8), V( -45) },
|
{ V(-11), V(75), V(19), V(-6), V(26), V(9), V(-47) },
|
||||||
{ V(-39), V(-12), V(-29), V(-50), V(-43), V(-68), V(-164) }
|
{ V(-41), V(-11), V(-27), V(-58), V(-42), V(-66), V(-163) }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Danger of enemy pawns moving toward our king by [distance from edge][rank].
|
// Danger of enemy pawns moving toward our king by [distance from edge][rank].
|
||||||
@@ -61,17 +63,17 @@ namespace {
|
|||||||
// is behind our king. Note that UnblockedStorm[0][1-2] accommodate opponent pawn
|
// is behind our king. Note that UnblockedStorm[0][1-2] accommodate opponent pawn
|
||||||
// on edge, likely blocked by our king.
|
// on edge, likely blocked by our king.
|
||||||
constexpr Value UnblockedStorm[int(FILE_NB) / 2][RANK_NB] = {
|
constexpr Value UnblockedStorm[int(FILE_NB) / 2][RANK_NB] = {
|
||||||
{ V( 87), V(-288), V(-168), V( 96), V( 47), V( 44), V( 46) },
|
{ V(94), V(-280), V(-170), V(90), V(59), V(47), V(53) },
|
||||||
{ V( 42), V( -25), V( 120), V( 45), V( 34), V( -9), V( 24) },
|
{ V(43), V(-17), V(128), V(39), V(26), V(-17), V(15) },
|
||||||
{ V( -8), V( 51), V( 167), V( 35), V( -4), V(-16), V(-12) },
|
{ V(-9), V(62), V(170), V(34), V(-5), V(-20), V(-11) },
|
||||||
{ V(-17), V( -13), V( 100), V( 4), V( 9), V(-16), V(-31) }
|
{ V(-27), V(-19), V(106), V(10), V(2), V(-13), V(-24) }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// KingOnFile[semi-open Us][semi-open Them] contains bonuses/penalties
|
// KingOnFile[semi-open Us][semi-open Them] contains bonuses/penalties
|
||||||
// for king when the king is on a semi-open or open file.
|
// for king when the king is on a semi-open or open file.
|
||||||
constexpr Score KingOnFile[2][2] = {{ S(-21,10), S(-7, 1) },
|
constexpr Score KingOnFile[2][2] = {{ S(-18,11), S(-6,-3) },
|
||||||
{ S( 0,-3), S( 9,-4) }};
|
{ S( 0, 0), S( 5,-4) }};
|
||||||
|
|
||||||
#undef S
|
#undef S
|
||||||
#undef V
|
#undef V
|
||||||
@@ -107,8 +109,9 @@ namespace {
|
|||||||
e->blockedCount += popcount(shift<Up>(ourPawns) & (theirPawns | doubleAttackThem));
|
e->blockedCount += popcount(shift<Up>(ourPawns) & (theirPawns | doubleAttackThem));
|
||||||
|
|
||||||
// Loop through all pawns of the current color and score each pawn
|
// Loop through all pawns of the current color and score each pawn
|
||||||
while (b) {
|
while (b)
|
||||||
s = pop_lsb(&b);
|
{
|
||||||
|
s = pop_lsb(b);
|
||||||
|
|
||||||
assert(pos.piece_on(s) == make_piece(Us, PAWN));
|
assert(pos.piece_on(s) == make_piece(Us, PAWN));
|
||||||
|
|
||||||
@@ -288,7 +291,7 @@ Score Entry::do_king_safety(const Position& pos) {
|
|||||||
if (pawns & attacks_bb<KING>(ksq))
|
if (pawns & attacks_bb<KING>(ksq))
|
||||||
minPawnDist = 1;
|
minPawnDist = 1;
|
||||||
else while (pawns)
|
else while (pawns)
|
||||||
minPawnDist = std::min(minPawnDist, distance(ksq, pop_lsb(&pawns)));
|
minPawnDist = std::min(minPawnDist, distance(ksq, pop_lsb(pawns)));
|
||||||
|
|
||||||
return shelter - make_score(0, 16 * minPawnDist);
|
return shelter - make_score(0, 16 * minPawnDist);
|
||||||
}
|
}
|
||||||
@@ -298,3 +301,5 @@ template Score Entry::do_king_safety<WHITE>(const Position& pos);
|
|||||||
template Score Entry::do_king_safety<BLACK>(const Position& pos);
|
template Score Entry::do_king_safety<BLACK>(const Position& pos);
|
||||||
|
|
||||||
} // namespace Pawns
|
} // namespace Pawns
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
namespace Pawns {
|
namespace Stockfish::Pawns {
|
||||||
|
|
||||||
/// Pawns::Entry contains various information about a pawn structure. A lookup
|
/// Pawns::Entry contains various information about a pawn structure. A lookup
|
||||||
/// to the pawn hash table (performed by calling the probe function) returns a
|
/// to the pawn hash table (performed by calling the probe function) returns a
|
||||||
@@ -65,6 +65,6 @@ typedef HashTable<Entry, 131072> Table;
|
|||||||
|
|
||||||
Entry* probe(const Position& pos);
|
Entry* probe(const Position& pos);
|
||||||
|
|
||||||
} // namespace Pawns
|
} // namespace Stockfish::Pawns
|
||||||
|
|
||||||
#endif // #ifndef PAWNS_H_INCLUDED
|
#endif // #ifndef PAWNS_H_INCLUDED
|
||||||
|
|||||||
+70
-57
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -34,6 +34,8 @@
|
|||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
namespace Zobrist {
|
namespace Zobrist {
|
||||||
|
|
||||||
Key psq[PIECE_NB][SQUARE_NB];
|
Key psq[PIECE_NB][SQUARE_NB];
|
||||||
@@ -71,13 +73,13 @@ std::ostream& operator<<(std::ostream& os, const Position& pos) {
|
|||||||
<< std::setfill(' ') << std::dec << "\nCheckers: ";
|
<< std::setfill(' ') << std::dec << "\nCheckers: ";
|
||||||
|
|
||||||
for (Bitboard b = pos.checkers(); b; )
|
for (Bitboard b = pos.checkers(); b; )
|
||||||
os << UCI::square(pop_lsb(&b)) << " ";
|
os << UCI::square(pop_lsb(b)) << " ";
|
||||||
|
|
||||||
if ( int(Tablebases::MaxCardinality) >= popcount(pos.pieces())
|
if ( int(Tablebases::MaxCardinality) >= popcount(pos.pieces())
|
||||||
&& !pos.can_castle(ANY_CASTLING))
|
&& !pos.can_castle(ANY_CASTLING))
|
||||||
{
|
{
|
||||||
StateInfo st;
|
StateInfo st;
|
||||||
ASSERT_ALIGNED(&st, Eval::NNUE::kCacheLineSize);
|
ASSERT_ALIGNED(&st, Eval::NNUE::CacheLineSize);
|
||||||
|
|
||||||
Position p;
|
Position p;
|
||||||
p.set(pos.fen(), pos.is_chess960(), &st, pos.this_thread());
|
p.set(pos.fen(), pos.is_chess960(), &st, pos.this_thread());
|
||||||
@@ -127,7 +129,7 @@ void Position::init() {
|
|||||||
// Prepare the cuckoo tables
|
// Prepare the cuckoo tables
|
||||||
std::memset(cuckoo, 0, sizeof(cuckoo));
|
std::memset(cuckoo, 0, sizeof(cuckoo));
|
||||||
std::memset(cuckooMove, 0, sizeof(cuckooMove));
|
std::memset(cuckooMove, 0, sizeof(cuckooMove));
|
||||||
int count = 0;
|
[[maybe_unused]] int count = 0;
|
||||||
for (Piece pc : Pieces)
|
for (Piece pc : Pieces)
|
||||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
||||||
for (Square s2 = Square(s1 + 1); s2 <= SQ_H8; ++s2)
|
for (Square s2 = Square(s1 + 1); s2 <= SQ_H8; ++s2)
|
||||||
@@ -249,8 +251,6 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th
|
|||||||
set_castling_right(c, rsq);
|
set_castling_right(c, rsq);
|
||||||
}
|
}
|
||||||
|
|
||||||
set_state(st);
|
|
||||||
|
|
||||||
// 4. En passant square.
|
// 4. En passant square.
|
||||||
// Ignore if square is invalid or not on side to move relative rank 6.
|
// Ignore if square is invalid or not on side to move relative rank 6.
|
||||||
bool enpassant = false;
|
bool enpassant = false;
|
||||||
@@ -264,24 +264,12 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th
|
|||||||
// a) side to move have a pawn threatening epSquare
|
// a) side to move have a pawn threatening epSquare
|
||||||
// b) there is an enemy pawn in front of epSquare
|
// b) there is an enemy pawn in front of epSquare
|
||||||
// c) there is no piece on epSquare or behind epSquare
|
// c) there is no piece on epSquare or behind epSquare
|
||||||
// d) enemy pawn didn't block a check of its own color by moving forward
|
|
||||||
enpassant = pawn_attacks_bb(~sideToMove, st->epSquare) & pieces(sideToMove, PAWN)
|
enpassant = pawn_attacks_bb(~sideToMove, st->epSquare) & pieces(sideToMove, PAWN)
|
||||||
&& (pieces(~sideToMove, PAWN) & (st->epSquare + pawn_push(~sideToMove)))
|
&& (pieces(~sideToMove, PAWN) & (st->epSquare + pawn_push(~sideToMove)))
|
||||||
&& !(pieces() & (st->epSquare | (st->epSquare + pawn_push(sideToMove))))
|
&& !(pieces() & (st->epSquare | (st->epSquare + pawn_push(sideToMove))));
|
||||||
&& ( file_of(square<KING>(sideToMove)) == file_of(st->epSquare)
|
|
||||||
|| !(blockers_for_king(sideToMove) & (st->epSquare + pawn_push(~sideToMove))));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// It's necessary for st->previous to be intialized in this way because legality check relies on its existence
|
if (!enpassant)
|
||||||
if (enpassant) {
|
|
||||||
st->previous = new StateInfo();
|
|
||||||
remove_piece(st->epSquare - pawn_push(sideToMove));
|
|
||||||
st->previous->checkersBB = attackers_to(square<KING>(~sideToMove)) & pieces(sideToMove);
|
|
||||||
st->previous->blockersForKing[WHITE] = slider_blockers(pieces(BLACK), square<KING>(WHITE), st->previous->pinners[BLACK]);
|
|
||||||
st->previous->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), square<KING>(BLACK), st->previous->pinners[WHITE]);
|
|
||||||
put_piece(make_piece(~sideToMove, PAWN), st->epSquare - pawn_push(sideToMove));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
st->epSquare = SQ_NONE;
|
st->epSquare = SQ_NONE;
|
||||||
|
|
||||||
// 5-6. Halfmove clock and fullmove number
|
// 5-6. Halfmove clock and fullmove number
|
||||||
@@ -293,8 +281,7 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th
|
|||||||
|
|
||||||
chess960 = isChess960;
|
chess960 = isChess960;
|
||||||
thisThread = th;
|
thisThread = th;
|
||||||
st->accumulator.state[WHITE] = Eval::NNUE::INIT;
|
set_state(st);
|
||||||
st->accumulator.state[BLACK] = Eval::NNUE::INIT;
|
|
||||||
|
|
||||||
assert(pos_is_ok());
|
assert(pos_is_ok());
|
||||||
|
|
||||||
@@ -318,7 +305,7 @@ void Position::set_castling_right(Color c, Square rfrom) {
|
|||||||
Square kto = relative_square(c, cr & KING_SIDE ? SQ_G1 : SQ_C1);
|
Square kto = relative_square(c, cr & KING_SIDE ? SQ_G1 : SQ_C1);
|
||||||
Square rto = relative_square(c, cr & KING_SIDE ? SQ_F1 : SQ_D1);
|
Square rto = relative_square(c, cr & KING_SIDE ? SQ_F1 : SQ_D1);
|
||||||
|
|
||||||
castlingPath[cr] = (between_bb(rfrom, rto) | between_bb(kfrom, kto) | rto | kto)
|
castlingPath[cr] = (between_bb(rfrom, rto) | between_bb(kfrom, kto))
|
||||||
& ~(kfrom | rfrom);
|
& ~(kfrom | rfrom);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -357,7 +344,7 @@ void Position::set_state(StateInfo* si) const {
|
|||||||
|
|
||||||
for (Bitboard b = pieces(); b; )
|
for (Bitboard b = pieces(); b; )
|
||||||
{
|
{
|
||||||
Square s = pop_lsb(&b);
|
Square s = pop_lsb(b);
|
||||||
Piece pc = piece_on(s);
|
Piece pc = piece_on(s);
|
||||||
si->key ^= Zobrist::psq[pc][s];
|
si->key ^= Zobrist::psq[pc][s];
|
||||||
|
|
||||||
@@ -408,7 +395,7 @@ Position& Position::set(const string& code, Color c, StateInfo* si) {
|
|||||||
/// Position::fen() returns a FEN representation of the position. In case of
|
/// Position::fen() returns a FEN representation of the position. In case of
|
||||||
/// Chess960 the Shredder-FEN notation is used. This is mainly a debugging function.
|
/// Chess960 the Shredder-FEN notation is used. This is mainly a debugging function.
|
||||||
|
|
||||||
const string Position::fen() const {
|
string Position::fen() const {
|
||||||
|
|
||||||
int emptyCnt;
|
int emptyCnt;
|
||||||
std::ostringstream ss;
|
std::ostringstream ss;
|
||||||
@@ -474,7 +461,7 @@ Bitboard Position::slider_blockers(Bitboard sliders, Square s, Bitboard& pinners
|
|||||||
|
|
||||||
while (snipers)
|
while (snipers)
|
||||||
{
|
{
|
||||||
Square sniperSq = pop_lsb(&snipers);
|
Square sniperSq = pop_lsb(snipers);
|
||||||
Bitboard b = between_bb(s, sniperSq) & occupancy;
|
Bitboard b = between_bb(s, sniperSq) & occupancy;
|
||||||
|
|
||||||
if (b && !more_than_one(b))
|
if (b && !more_than_one(b))
|
||||||
@@ -515,11 +502,23 @@ bool Position::legal(Move m) const {
|
|||||||
assert(color_of(moved_piece(m)) == us);
|
assert(color_of(moved_piece(m)) == us);
|
||||||
assert(piece_on(square<KING>(us)) == make_piece(us, KING));
|
assert(piece_on(square<KING>(us)) == make_piece(us, KING));
|
||||||
|
|
||||||
// st->previous->blockersForKing consider capsq as empty.
|
// En passant captures are a tricky special case. Because they are rather
|
||||||
// If pinned, it has to move along the king ray.
|
// uncommon, we do it simply by testing whether the king is attacked after
|
||||||
|
// the move is made.
|
||||||
if (type_of(m) == EN_PASSANT)
|
if (type_of(m) == EN_PASSANT)
|
||||||
return !(st->previous->blockersForKing[sideToMove] & from)
|
{
|
||||||
|| aligned(from, to, square<KING>(us));
|
Square ksq = square<KING>(us);
|
||||||
|
Square capsq = to - pawn_push(us);
|
||||||
|
Bitboard occupied = (pieces() ^ from ^ capsq) | to;
|
||||||
|
|
||||||
|
assert(to == ep_square());
|
||||||
|
assert(moved_piece(m) == make_piece(us, PAWN));
|
||||||
|
assert(piece_on(capsq) == make_piece(~us, PAWN));
|
||||||
|
assert(piece_on(to) == NO_PIECE);
|
||||||
|
|
||||||
|
return !(attacks_bb< ROOK>(ksq, occupied) & pieces(~us, QUEEN, ROOK))
|
||||||
|
&& !(attacks_bb<BISHOP>(ksq, occupied) & pieces(~us, QUEEN, BISHOP));
|
||||||
|
}
|
||||||
|
|
||||||
// Castling moves generation does not check if the castling path is clear of
|
// Castling moves generation does not check if the castling path is clear of
|
||||||
// enemy attacks, it is delayed at a later time: now!
|
// enemy attacks, it is delayed at a later time: now!
|
||||||
@@ -542,7 +541,7 @@ bool Position::legal(Move m) const {
|
|||||||
// If the moving piece is a king, check whether the destination square is
|
// If the moving piece is a king, check whether the destination square is
|
||||||
// attacked by the opponent.
|
// attacked by the opponent.
|
||||||
if (type_of(piece_on(from)) == KING)
|
if (type_of(piece_on(from)) == KING)
|
||||||
return !(attackers_to(to) & pieces(~us));
|
return !(attackers_to(to, pieces() ^ from) & pieces(~us));
|
||||||
|
|
||||||
// A non-king move is legal if and only if it is not pinned or it
|
// A non-king move is legal if and only if it is not pinned or it
|
||||||
// is moving along the ray towards or away from the king.
|
// is moving along the ray towards or away from the king.
|
||||||
@@ -611,8 +610,8 @@ bool Position::pseudo_legal(const Move m) const {
|
|||||||
if (more_than_one(checkers()))
|
if (more_than_one(checkers()))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Our move must be a blocking evasion or a capture of the checking piece
|
// Our move must be a blocking interposition or a capture of the checking piece
|
||||||
if (!((between_bb(lsb(checkers()), square<KING>(us)) | checkers()) & to))
|
if (!(between_bb(square<KING>(us), lsb(checkers())) & to))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// In case of king moves under check we have to remove king so as to catch
|
// In case of king moves under check we have to remove king so as to catch
|
||||||
@@ -652,15 +651,18 @@ bool Position::gives_check(Move m) const {
|
|||||||
case PROMOTION:
|
case PROMOTION:
|
||||||
return attacks_bb(promotion_type(m), to, pieces() ^ from) & square<KING>(~sideToMove);
|
return attacks_bb(promotion_type(m), to, pieces() ^ from) & square<KING>(~sideToMove);
|
||||||
|
|
||||||
// The double-pushed pawn blocked a check? En Passant will remove the blocker.
|
// En passant capture with check? We have already handled the case
|
||||||
// The only discovery check that wasn't handle is through capsq and fromsq
|
// of direct checks and ordinary discovered check, so the only case we
|
||||||
// So the King must be in the same rank as fromsq to consider this possibility.
|
// need to handle is the unusual case of a discovered check through
|
||||||
// st->previous->blockersForKing consider capsq as empty.
|
// the captured pawn.
|
||||||
case EN_PASSANT:
|
case EN_PASSANT:
|
||||||
return st->previous->checkersBB
|
{
|
||||||
|| ( rank_of(square<KING>(~sideToMove)) == rank_of(from)
|
Square capsq = make_square(file_of(to), rank_of(from));
|
||||||
&& st->previous->blockersForKing[~sideToMove] & from);
|
Bitboard b = (pieces() ^ from ^ capsq) | to;
|
||||||
|
|
||||||
|
return (attacks_bb< ROOK>(square<KING>(~sideToMove), b) & pieces(sideToMove, QUEEN, ROOK))
|
||||||
|
| (attacks_bb<BISHOP>(square<KING>(~sideToMove), b) & pieces(sideToMove, QUEEN, BISHOP));
|
||||||
|
}
|
||||||
default: //CASTLING
|
default: //CASTLING
|
||||||
{
|
{
|
||||||
// Castling is encoded as 'king captures the rook'
|
// Castling is encoded as 'king captures the rook'
|
||||||
@@ -700,8 +702,8 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
|
|||||||
++st->pliesFromNull;
|
++st->pliesFromNull;
|
||||||
|
|
||||||
// Used by NNUE
|
// Used by NNUE
|
||||||
st->accumulator.state[WHITE] = Eval::NNUE::EMPTY;
|
st->accumulator.computed[WHITE] = false;
|
||||||
st->accumulator.state[BLACK] = Eval::NNUE::EMPTY;
|
st->accumulator.computed[BLACK] = false;
|
||||||
auto& dp = st->dirtyPiece;
|
auto& dp = st->dirtyPiece;
|
||||||
dp.dirty_num = 1;
|
dp.dirty_num = 1;
|
||||||
|
|
||||||
@@ -986,7 +988,7 @@ void Position::do_castling(Color us, Square from, Square& to, Square& rfrom, Squ
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position::do(undo)_null_move() is used to do(undo) a "null move": it flips
|
/// Position::do_null_move() is used to do a "null move": it flips
|
||||||
/// the side to move without executing any move on the board.
|
/// the side to move without executing any move on the board.
|
||||||
|
|
||||||
void Position::do_null_move(StateInfo& newSt) {
|
void Position::do_null_move(StateInfo& newSt) {
|
||||||
@@ -1001,8 +1003,8 @@ void Position::do_null_move(StateInfo& newSt) {
|
|||||||
|
|
||||||
st->dirtyPiece.dirty_num = 0;
|
st->dirtyPiece.dirty_num = 0;
|
||||||
st->dirtyPiece.piece[0] = NO_PIECE; // Avoid checks in UpdateAccumulator()
|
st->dirtyPiece.piece[0] = NO_PIECE; // Avoid checks in UpdateAccumulator()
|
||||||
st->accumulator.state[WHITE] = Eval::NNUE::EMPTY;
|
st->accumulator.computed[WHITE] = false;
|
||||||
st->accumulator.state[BLACK] = Eval::NNUE::EMPTY;
|
st->accumulator.computed[BLACK] = false;
|
||||||
|
|
||||||
if (st->epSquare != SQ_NONE)
|
if (st->epSquare != SQ_NONE)
|
||||||
{
|
{
|
||||||
@@ -1011,9 +1013,9 @@ void Position::do_null_move(StateInfo& newSt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
st->key ^= Zobrist::side;
|
st->key ^= Zobrist::side;
|
||||||
|
++st->rule50;
|
||||||
prefetch(TT.first_entry(key()));
|
prefetch(TT.first_entry(key()));
|
||||||
|
|
||||||
++st->rule50;
|
|
||||||
st->pliesFromNull = 0;
|
st->pliesFromNull = 0;
|
||||||
|
|
||||||
sideToMove = ~sideToMove;
|
sideToMove = ~sideToMove;
|
||||||
@@ -1025,6 +1027,9 @@ void Position::do_null_move(StateInfo& newSt) {
|
|||||||
assert(pos_is_ok());
|
assert(pos_is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Position::undo_null_move() must be used to undo a "null move"
|
||||||
|
|
||||||
void Position::undo_null_move() {
|
void Position::undo_null_move() {
|
||||||
|
|
||||||
assert(!checkers());
|
assert(!checkers());
|
||||||
@@ -1049,7 +1054,10 @@ Key Position::key_after(Move m) const {
|
|||||||
if (captured)
|
if (captured)
|
||||||
k ^= Zobrist::psq[captured][to];
|
k ^= Zobrist::psq[captured][to];
|
||||||
|
|
||||||
return k ^ Zobrist::psq[pc][to] ^ Zobrist::psq[pc][from];
|
k ^= Zobrist::psq[pc][to] ^ Zobrist::psq[pc][from];
|
||||||
|
|
||||||
|
return (captured || type_of(pc) == PAWN)
|
||||||
|
? k : adjust_key50<true>(k);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1075,8 +1083,9 @@ bool Position::see_ge(Move m, Value threshold) const {
|
|||||||
if (swap <= 0)
|
if (swap <= 0)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
assert(color_of(piece_on(from)) == sideToMove);
|
||||||
Bitboard occupied = pieces() ^ from ^ to;
|
Bitboard occupied = pieces() ^ from ^ to;
|
||||||
Color stm = color_of(piece_on(from));
|
Color stm = sideToMove;
|
||||||
Bitboard attackers = attackers_to(to, occupied);
|
Bitboard attackers = attackers_to(to, occupied);
|
||||||
Bitboard stmAttackers, bb;
|
Bitboard stmAttackers, bb;
|
||||||
int res = 1;
|
int res = 1;
|
||||||
@@ -1090,13 +1099,15 @@ bool Position::see_ge(Move m, Value threshold) const {
|
|||||||
if (!(stmAttackers = attackers & pieces(stm)))
|
if (!(stmAttackers = attackers & pieces(stm)))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Don't allow pinned pieces to attack (except the king) as long as
|
// Don't allow pinned pieces to attack as long as there are
|
||||||
// there are pinners on their original square.
|
// pinners on their original square.
|
||||||
if (pinners(~stm) & occupied)
|
if (pinners(~stm) & occupied)
|
||||||
|
{
|
||||||
stmAttackers &= ~blockers_for_king(stm);
|
stmAttackers &= ~blockers_for_king(stm);
|
||||||
|
|
||||||
if (!stmAttackers)
|
if (!stmAttackers)
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
res ^= 1;
|
res ^= 1;
|
||||||
|
|
||||||
@@ -1107,7 +1118,7 @@ bool Position::see_ge(Move m, Value threshold) const {
|
|||||||
if ((swap = PawnValueMg - swap) < res)
|
if ((swap = PawnValueMg - swap) < res)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
occupied ^= lsb(bb);
|
occupied ^= least_significant_square_bb(bb);
|
||||||
attackers |= attacks_bb<BISHOP>(to, occupied) & pieces(BISHOP, QUEEN);
|
attackers |= attacks_bb<BISHOP>(to, occupied) & pieces(BISHOP, QUEEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1116,7 +1127,7 @@ bool Position::see_ge(Move m, Value threshold) const {
|
|||||||
if ((swap = KnightValueMg - swap) < res)
|
if ((swap = KnightValueMg - swap) < res)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
occupied ^= lsb(bb);
|
occupied ^= least_significant_square_bb(bb);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if ((bb = stmAttackers & pieces(BISHOP)))
|
else if ((bb = stmAttackers & pieces(BISHOP)))
|
||||||
@@ -1124,7 +1135,7 @@ bool Position::see_ge(Move m, Value threshold) const {
|
|||||||
if ((swap = BishopValueMg - swap) < res)
|
if ((swap = BishopValueMg - swap) < res)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
occupied ^= lsb(bb);
|
occupied ^= least_significant_square_bb(bb);
|
||||||
attackers |= attacks_bb<BISHOP>(to, occupied) & pieces(BISHOP, QUEEN);
|
attackers |= attacks_bb<BISHOP>(to, occupied) & pieces(BISHOP, QUEEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1133,7 +1144,7 @@ bool Position::see_ge(Move m, Value threshold) const {
|
|||||||
if ((swap = RookValueMg - swap) < res)
|
if ((swap = RookValueMg - swap) < res)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
occupied ^= lsb(bb);
|
occupied ^= least_significant_square_bb(bb);
|
||||||
attackers |= attacks_bb<ROOK>(to, occupied) & pieces(ROOK, QUEEN);
|
attackers |= attacks_bb<ROOK>(to, occupied) & pieces(ROOK, QUEEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1142,7 +1153,7 @@ bool Position::see_ge(Move m, Value threshold) const {
|
|||||||
if ((swap = QueenValueMg - swap) < res)
|
if ((swap = QueenValueMg - swap) < res)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
occupied ^= lsb(bb);
|
occupied ^= least_significant_square_bb(bb);
|
||||||
attackers |= (attacks_bb<BISHOP>(to, occupied) & pieces(BISHOP, QUEEN))
|
attackers |= (attacks_bb<BISHOP>(to, occupied) & pieces(BISHOP, QUEEN))
|
||||||
| (attacks_bb<ROOK >(to, occupied) & pieces(ROOK , QUEEN));
|
| (attacks_bb<ROOK >(to, occupied) & pieces(ROOK , QUEEN));
|
||||||
}
|
}
|
||||||
@@ -1216,7 +1227,7 @@ bool Position::has_game_cycle(int ply) const {
|
|||||||
Square s1 = from_sq(move);
|
Square s1 = from_sq(move);
|
||||||
Square s2 = to_sq(move);
|
Square s2 = to_sq(move);
|
||||||
|
|
||||||
if (!(between_bb(s1, s2) & pieces()))
|
if (!((between_bb(s1, s2) ^ s2) & pieces()))
|
||||||
{
|
{
|
||||||
if (ply > i)
|
if (ply > i)
|
||||||
return true;
|
return true;
|
||||||
@@ -1313,7 +1324,7 @@ bool Position::pos_is_ok() const {
|
|||||||
assert(0 && "pos_is_ok: Bitboards");
|
assert(0 && "pos_is_ok: Bitboards");
|
||||||
|
|
||||||
StateInfo si = *st;
|
StateInfo si = *st;
|
||||||
ASSERT_ALIGNED(&si, Eval::NNUE::kCacheLineSize);
|
ASSERT_ALIGNED(&si, Eval::NNUE::CacheLineSize);
|
||||||
|
|
||||||
set_state(&si);
|
set_state(&si);
|
||||||
if (std::memcmp(&si, st, sizeof(StateInfo)))
|
if (std::memcmp(&si, st, sizeof(StateInfo)))
|
||||||
@@ -1338,3 +1349,5 @@ bool Position::pos_is_ok() const {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+44
-27
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -31,6 +31,7 @@
|
|||||||
|
|
||||||
#include "nnue/nnue_accumulator.h"
|
#include "nnue/nnue_accumulator.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
/// StateInfo struct stores information needed to restore a Position object to
|
/// 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
|
/// its previous state when we retract a move. Whenever a move is made on the
|
||||||
@@ -50,11 +51,11 @@ struct StateInfo {
|
|||||||
// Not copied when making a move (will be recomputed anyhow)
|
// Not copied when making a move (will be recomputed anyhow)
|
||||||
Key key;
|
Key key;
|
||||||
Bitboard checkersBB;
|
Bitboard checkersBB;
|
||||||
Piece capturedPiece;
|
|
||||||
StateInfo* previous;
|
StateInfo* previous;
|
||||||
Bitboard blockersForKing[COLOR_NB];
|
Bitboard blockersForKing[COLOR_NB];
|
||||||
Bitboard pinners[COLOR_NB];
|
Bitboard pinners[COLOR_NB];
|
||||||
Bitboard checkSquares[PIECE_TYPE_NB];
|
Bitboard checkSquares[PIECE_TYPE_NB];
|
||||||
|
Piece capturedPiece;
|
||||||
int repetition;
|
int repetition;
|
||||||
|
|
||||||
// Used by NNUE
|
// Used by NNUE
|
||||||
@@ -87,7 +88,7 @@ public:
|
|||||||
// FEN string input/output
|
// FEN string input/output
|
||||||
Position& set(const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th);
|
Position& set(const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th);
|
||||||
Position& set(const std::string& code, Color c, StateInfo* si);
|
Position& set(const std::string& code, Color c, StateInfo* si);
|
||||||
const std::string fen() const;
|
std::string fen() const;
|
||||||
|
|
||||||
// Position representation
|
// Position representation
|
||||||
Bitboard pieces(PieceType pt) const;
|
Bitboard pieces(PieceType pt) const;
|
||||||
@@ -114,20 +115,18 @@ public:
|
|||||||
Bitboard blockers_for_king(Color c) const;
|
Bitboard blockers_for_king(Color c) const;
|
||||||
Bitboard check_squares(PieceType pt) const;
|
Bitboard check_squares(PieceType pt) const;
|
||||||
Bitboard pinners(Color c) const;
|
Bitboard pinners(Color c) const;
|
||||||
bool is_discovered_check_on_king(Color c, Move m) const;
|
|
||||||
|
|
||||||
// Attacks to/from a given square
|
// Attacks to/from a given square
|
||||||
Bitboard attackers_to(Square s) const;
|
Bitboard attackers_to(Square s) const;
|
||||||
Bitboard attackers_to(Square s, Bitboard occupied) const;
|
Bitboard attackers_to(Square s, Bitboard occupied) const;
|
||||||
Bitboard slider_blockers(Bitboard sliders, Square s, Bitboard& pinners) const;
|
Bitboard slider_blockers(Bitboard sliders, Square s, Bitboard& pinners) const;
|
||||||
|
template<PieceType Pt> Bitboard attacks_by(Color c) const;
|
||||||
|
|
||||||
// Properties of moves
|
// Properties of moves
|
||||||
bool legal(Move m) const;
|
bool legal(Move m) const;
|
||||||
bool pseudo_legal(const Move m) const;
|
bool pseudo_legal(const Move m) const;
|
||||||
bool capture(Move m) const;
|
bool capture(Move m) const;
|
||||||
bool capture_or_promotion(Move m) const;
|
|
||||||
bool gives_check(Move m) const;
|
bool gives_check(Move m) const;
|
||||||
bool advanced_pawn_push(Move m) const;
|
|
||||||
Piece moved_piece(Move m) const;
|
Piece moved_piece(Move m) const;
|
||||||
Piece captured_piece() const;
|
Piece captured_piece() const;
|
||||||
|
|
||||||
@@ -162,6 +161,7 @@ public:
|
|||||||
bool has_repeated() const;
|
bool has_repeated() const;
|
||||||
int rule50_count() const;
|
int rule50_count() const;
|
||||||
Score psq_score() const;
|
Score psq_score() const;
|
||||||
|
Value psq_eg_stm() const;
|
||||||
Value non_pawn_material(Color c) const;
|
Value non_pawn_material(Color c) const;
|
||||||
Value non_pawn_material() const;
|
Value non_pawn_material() const;
|
||||||
|
|
||||||
@@ -172,6 +172,9 @@ public:
|
|||||||
// Used by NNUE
|
// Used by NNUE
|
||||||
StateInfo* state() const;
|
StateInfo* state() const;
|
||||||
|
|
||||||
|
void put_piece(Piece pc, Square s);
|
||||||
|
void remove_piece(Square s);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Initialization helpers (used while setting up a position)
|
// Initialization helpers (used while setting up a position)
|
||||||
void set_castling_right(Color c, Square rfrom);
|
void set_castling_right(Color c, Square rfrom);
|
||||||
@@ -179,11 +182,11 @@ private:
|
|||||||
void set_check_info(StateInfo* si) const;
|
void set_check_info(StateInfo* si) const;
|
||||||
|
|
||||||
// Other helpers
|
// Other helpers
|
||||||
void put_piece(Piece pc, Square s);
|
|
||||||
void remove_piece(Square s);
|
|
||||||
void move_piece(Square from, Square to);
|
void move_piece(Square from, Square to);
|
||||||
template<bool Do>
|
template<bool Do>
|
||||||
void do_castling(Color us, Square from, Square& to, Square& rfrom, Square& rto);
|
void do_castling(Color us, Square from, Square& to, Square& rfrom, Square& rto);
|
||||||
|
template<bool AfterMove>
|
||||||
|
Key adjust_key50(Key k) const;
|
||||||
|
|
||||||
// Data members
|
// Data members
|
||||||
Piece board[SQUARE_NB];
|
Piece board[SQUARE_NB];
|
||||||
@@ -193,11 +196,11 @@ private:
|
|||||||
int castlingRightsMask[SQUARE_NB];
|
int castlingRightsMask[SQUARE_NB];
|
||||||
Square castlingRookSquare[CASTLING_RIGHT_NB];
|
Square castlingRookSquare[CASTLING_RIGHT_NB];
|
||||||
Bitboard castlingPath[CASTLING_RIGHT_NB];
|
Bitboard castlingPath[CASTLING_RIGHT_NB];
|
||||||
|
Thread* thisThread;
|
||||||
|
StateInfo* st;
|
||||||
int gamePly;
|
int gamePly;
|
||||||
Color sideToMove;
|
Color sideToMove;
|
||||||
Score psq;
|
Score psq;
|
||||||
Thread* thisThread;
|
|
||||||
StateInfo* st;
|
|
||||||
bool chess960;
|
bool chess960;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -285,6 +288,22 @@ inline Bitboard Position::attackers_to(Square s) const {
|
|||||||
return attackers_to(s, pieces());
|
return attackers_to(s, pieces());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<PieceType Pt>
|
||||||
|
inline Bitboard Position::attacks_by(Color c) const {
|
||||||
|
|
||||||
|
if constexpr (Pt == PAWN)
|
||||||
|
return c == WHITE ? pawn_attacks_bb<WHITE>(pieces(WHITE, PAWN))
|
||||||
|
: pawn_attacks_bb<BLACK>(pieces(BLACK, PAWN));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Bitboard threats = 0;
|
||||||
|
Bitboard attackers = pieces(c, Pt);
|
||||||
|
while (attackers)
|
||||||
|
threats |= attacks_bb<Pt>(pop_lsb(attackers), pieces());
|
||||||
|
return threats;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
inline Bitboard Position::checkers() const {
|
inline Bitboard Position::checkers() const {
|
||||||
return st->checkersBB;
|
return st->checkersBB;
|
||||||
}
|
}
|
||||||
@@ -301,26 +320,23 @@ inline Bitboard Position::check_squares(PieceType pt) const {
|
|||||||
return st->checkSquares[pt];
|
return st->checkSquares[pt];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Position::is_discovered_check_on_king(Color c, Move m) const {
|
|
||||||
return st->blockersForKing[c] & from_sq(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool Position::pawn_passed(Color c, Square s) const {
|
inline bool Position::pawn_passed(Color c, Square s) const {
|
||||||
return !(pieces(~c, PAWN) & passed_pawn_span(c, s));
|
return !(pieces(~c, PAWN) & passed_pawn_span(c, s));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Position::advanced_pawn_push(Move m) const {
|
|
||||||
return type_of(moved_piece(m)) == PAWN
|
|
||||||
&& relative_rank(sideToMove, to_sq(m)) > RANK_5;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline int Position::pawns_on_same_color_squares(Color c, Square s) const {
|
inline int Position::pawns_on_same_color_squares(Color c, Square s) const {
|
||||||
return popcount(pieces(c, PAWN) & ((DarkSquares & s) ? DarkSquares : ~DarkSquares));
|
return popcount(pieces(c, PAWN) & ((DarkSquares & s) ? DarkSquares : ~DarkSquares));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Key Position::key() const {
|
inline Key Position::key() const {
|
||||||
return st->rule50 < 14 ? st->key
|
return adjust_key50<false>(st->key);
|
||||||
: st->key ^ make_key((st->rule50 - 14) / 8);
|
}
|
||||||
|
|
||||||
|
template<bool AfterMove>
|
||||||
|
inline Key Position::adjust_key50(Key k) const
|
||||||
|
{
|
||||||
|
return st->rule50 < 14 - AfterMove
|
||||||
|
? k : k ^ make_key((st->rule50 - (14 - AfterMove)) / 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Key Position::pawn_key() const {
|
inline Key Position::pawn_key() const {
|
||||||
@@ -335,6 +351,10 @@ inline Score Position::psq_score() const {
|
|||||||
return psq;
|
return psq;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Value Position::psq_eg_stm() const {
|
||||||
|
return (sideToMove == WHITE ? 1 : -1) * eg_value(psq);
|
||||||
|
}
|
||||||
|
|
||||||
inline Value Position::non_pawn_material(Color c) const {
|
inline Value Position::non_pawn_material(Color c) const {
|
||||||
return st->nonPawnMaterial[c];
|
return st->nonPawnMaterial[c];
|
||||||
}
|
}
|
||||||
@@ -361,11 +381,6 @@ inline bool Position::is_chess960() const {
|
|||||||
return chess960;
|
return chess960;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
inline bool Position::capture(Move m) const {
|
||||||
assert(is_ok(m));
|
assert(is_ok(m));
|
||||||
// Castling is encoded as "king captures rook"
|
// Castling is encoded as "king captures rook"
|
||||||
@@ -396,7 +411,7 @@ inline void Position::remove_piece(Square s) {
|
|||||||
byTypeBB[ALL_PIECES] ^= s;
|
byTypeBB[ALL_PIECES] ^= s;
|
||||||
byTypeBB[type_of(pc)] ^= s;
|
byTypeBB[type_of(pc)] ^= s;
|
||||||
byColorBB[color_of(pc)] ^= s;
|
byColorBB[color_of(pc)] ^= s;
|
||||||
/* board[s] = NO_PIECE; Not needed, overwritten by the capturing one */
|
board[s] = NO_PIECE;
|
||||||
pieceCount[pc]--;
|
pieceCount[pc]--;
|
||||||
pieceCount[make_piece(color_of(pc), ALL_PIECES)]--;
|
pieceCount[make_piece(color_of(pc), ALL_PIECES)]--;
|
||||||
psq -= PSQT::psq[pc][s];
|
psq -= PSQT::psq[pc][s];
|
||||||
@@ -423,4 +438,6 @@ inline StateInfo* Position::state() const {
|
|||||||
return st;
|
return st;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef POSITION_H_INCLUDED
|
#endif // #ifndef POSITION_H_INCLUDED
|
||||||
|
|||||||
+4
-1
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -24,6 +24,7 @@
|
|||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
@@ -126,3 +127,5 @@ void init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace PSQT
|
} // namespace PSQT
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
|
||||||
namespace PSQT
|
namespace Stockfish::PSQT
|
||||||
{
|
{
|
||||||
|
|
||||||
extern Score psq[PIECE_NB][SQUARE_NB];
|
extern Score psq[PIECE_NB][SQUARE_NB];
|
||||||
@@ -32,7 +32,7 @@ extern Score psq[PIECE_NB][SQUARE_NB];
|
|||||||
// Fill psqt array from a set of internally linked parameters
|
// Fill psqt array from a set of internally linked parameters
|
||||||
extern void init();
|
extern void init();
|
||||||
|
|
||||||
} // namespace PSQT
|
} // namespace Stockfish::PSQT
|
||||||
|
|
||||||
|
|
||||||
#endif // PSQT_H_INCLUDED
|
#endif // PSQT_H_INCLUDED
|
||||||
|
|||||||
+412
-466
File diff suppressed because it is too large
Load Diff
+10
-4
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -25,13 +25,12 @@
|
|||||||
#include "movepick.h"
|
#include "movepick.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
class Position;
|
class Position;
|
||||||
|
|
||||||
namespace Search {
|
namespace Search {
|
||||||
|
|
||||||
/// Threshold used for countermoves based pruning
|
|
||||||
constexpr int CounterMovePruneThreshold = 0;
|
|
||||||
|
|
||||||
|
|
||||||
/// Stack struct keeps track of the information we need to remember from nodes
|
/// 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
|
/// shallower and deeper in the tree during the search. Each search thread has
|
||||||
@@ -50,6 +49,8 @@ struct Stack {
|
|||||||
bool inCheck;
|
bool inCheck;
|
||||||
bool ttPv;
|
bool ttPv;
|
||||||
bool ttHit;
|
bool ttHit;
|
||||||
|
int doubleExtensions;
|
||||||
|
int cutoffCnt;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -69,6 +70,9 @@ struct RootMove {
|
|||||||
|
|
||||||
Value score = -VALUE_INFINITE;
|
Value score = -VALUE_INFINITE;
|
||||||
Value previousScore = -VALUE_INFINITE;
|
Value previousScore = -VALUE_INFINITE;
|
||||||
|
Value averageScore = -VALUE_INFINITE;
|
||||||
|
bool scoreLowerbound = false;
|
||||||
|
bool scoreUpperbound = false;
|
||||||
int selDepth = 0;
|
int selDepth = 0;
|
||||||
int tbRank = 0;
|
int tbRank = 0;
|
||||||
Value tbScore;
|
Value tbScore;
|
||||||
@@ -106,4 +110,6 @@ void clear();
|
|||||||
|
|
||||||
} // namespace Search
|
} // namespace Search
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef SEARCH_H_INCLUDED
|
#endif // #ifndef SEARCH_H_INCLUDED
|
||||||
|
|||||||
+46
-30
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -50,13 +50,16 @@
|
|||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using namespace Tablebases;
|
using namespace Stockfish::Tablebases;
|
||||||
|
|
||||||
int Tablebases::MaxCardinality;
|
int Stockfish::Tablebases::MaxCardinality;
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
constexpr int TBPIECES = 7; // Max number of supported pieces
|
constexpr int TBPIECES = 7; // Max number of supported pieces
|
||||||
|
constexpr int MAX_DTZ = 1 << 18; // Max DTZ supported, large enough to deal with the syzygy TB limit.
|
||||||
|
|
||||||
enum { BigEndian, LittleEndian };
|
enum { BigEndian, LittleEndian };
|
||||||
enum TBType { WDL, DTZ }; // Used as template parameter
|
enum TBType { WDL, DTZ }; // Used as template parameter
|
||||||
@@ -103,9 +106,6 @@ template<> inline void swap_endian<uint8_t>(uint8_t&) {}
|
|||||||
|
|
||||||
template<typename T, int LE> T number(void* addr)
|
template<typename T, int LE> T number(void* addr)
|
||||||
{
|
{
|
||||||
static const union { uint32_t i; char c[4]; } Le = { 0x01020304 };
|
|
||||||
static const bool IsLittleEndian = (Le.c[0] == 4);
|
|
||||||
|
|
||||||
T v;
|
T v;
|
||||||
|
|
||||||
if ((uintptr_t)addr & (alignof(T) - 1)) // Unaligned pointer (very rare)
|
if ((uintptr_t)addr & (alignof(T) - 1)) // Unaligned pointer (very rare)
|
||||||
@@ -190,7 +190,8 @@ public:
|
|||||||
std::stringstream ss(Paths);
|
std::stringstream ss(Paths);
|
||||||
std::string path;
|
std::string path;
|
||||||
|
|
||||||
while (std::getline(ss, path, SepChar)) {
|
while (std::getline(ss, path, SepChar))
|
||||||
|
{
|
||||||
fname = path + "/" + f;
|
fname = path + "/" + f;
|
||||||
std::ifstream::open(fname);
|
std::ifstream::open(fname);
|
||||||
if (is_open())
|
if (is_open())
|
||||||
@@ -565,7 +566,8 @@ int decompress_pairs(PairsData* d, uint64_t idx) {
|
|||||||
int buf64Size = 64;
|
int buf64Size = 64;
|
||||||
Sym sym;
|
Sym sym;
|
||||||
|
|
||||||
while (true) {
|
while (true)
|
||||||
|
{
|
||||||
int len = 0; // This is the symbol length - d->min_sym_len
|
int len = 0; // This is the symbol length - d->min_sym_len
|
||||||
|
|
||||||
// Now get the symbol length. For any symbol s64 of length l right-padded
|
// Now get the symbol length. For any symbol s64 of length l right-padded
|
||||||
@@ -603,8 +605,8 @@ int decompress_pairs(PairsData* d, uint64_t idx) {
|
|||||||
// We binary-search for our value recursively expanding into the left and
|
// We binary-search for our value recursively expanding into the left and
|
||||||
// right child symbols until we reach a leaf node where symlen[sym] + 1 == 1
|
// right child symbols until we reach a leaf node where symlen[sym] + 1 == 1
|
||||||
// that will store the value we need.
|
// that will store the value we need.
|
||||||
while (d->symlen[sym]) {
|
while (d->symlen[sym])
|
||||||
|
{
|
||||||
Sym left = d->btree[sym].get<LR::Left>();
|
Sym left = d->btree[sym].get<LR::Left>();
|
||||||
|
|
||||||
// If a symbol contains 36 sub-symbols (d->symlen[sym] + 1 = 36) and
|
// If a symbol contains 36 sub-symbols (d->symlen[sym] + 1 = 36) and
|
||||||
@@ -709,7 +711,7 @@ Ret do_probe_table(const Position& pos, T* entry, WDLScore wdl, ProbeState* resu
|
|||||||
|
|
||||||
leadPawns = b = pos.pieces(color_of(pc), PAWN);
|
leadPawns = b = pos.pieces(color_of(pc), PAWN);
|
||||||
do
|
do
|
||||||
squares[size++] = pop_lsb(&b) ^ flipSquares;
|
squares[size++] = pop_lsb(b) ^ flipSquares;
|
||||||
while (b);
|
while (b);
|
||||||
|
|
||||||
leadPawnsCnt = size;
|
leadPawnsCnt = size;
|
||||||
@@ -729,7 +731,7 @@ Ret do_probe_table(const Position& pos, T* entry, WDLScore wdl, ProbeState* resu
|
|||||||
// directly map them to the correct color and square.
|
// directly map them to the correct color and square.
|
||||||
b = pos.pieces() ^ leadPawns;
|
b = pos.pieces() ^ leadPawns;
|
||||||
do {
|
do {
|
||||||
Square s = pop_lsb(&b);
|
Square s = pop_lsb(b);
|
||||||
squares[size] = s ^ flipSquares;
|
squares[size] = s ^ flipSquares;
|
||||||
pieces[size++] = Piece(pos.piece_on(s) ^ flipColor);
|
pieces[size++] = Piece(pos.piece_on(s) ^ flipColor);
|
||||||
} while (b);
|
} while (b);
|
||||||
@@ -768,7 +770,7 @@ Ret do_probe_table(const Position& pos, T* entry, WDLScore wdl, ProbeState* resu
|
|||||||
goto encode_remaining; // With pawns we have finished special treatments
|
goto encode_remaining; // With pawns we have finished special treatments
|
||||||
}
|
}
|
||||||
|
|
||||||
// In positions withouth pawns, we further flip the squares to ensure leading
|
// In positions without pawns, we further flip the squares to ensure leading
|
||||||
// piece is below RANK_5.
|
// piece is below RANK_5.
|
||||||
if (rank_of(squares[0]) > RANK_4)
|
if (rank_of(squares[0]) > RANK_4)
|
||||||
for (int i = 0; i < size; ++i)
|
for (int i = 0; i < size; ++i)
|
||||||
@@ -811,7 +813,7 @@ Ret do_probe_table(const Position& pos, T* entry, WDLScore wdl, ProbeState* resu
|
|||||||
// Rs "together" in 62 * 61 / 2 ways (we divide by 2 because rooks can be
|
// Rs "together" in 62 * 61 / 2 ways (we divide by 2 because rooks can be
|
||||||
// swapped and still get the same position.)
|
// swapped and still get the same position.)
|
||||||
//
|
//
|
||||||
// In case we have at least 3 unique pieces (inlcuded kings) we encode them
|
// In case we have at least 3 unique pieces (included kings) we encode them
|
||||||
// together.
|
// together.
|
||||||
if (entry->hasUniquePieces) {
|
if (entry->hasUniquePieces) {
|
||||||
|
|
||||||
@@ -826,7 +828,7 @@ Ret do_probe_table(const Position& pos, T* entry, WDLScore wdl, ProbeState* resu
|
|||||||
+ (squares[1] - adjust1)) * 62
|
+ (squares[1] - adjust1)) * 62
|
||||||
+ squares[2] - adjust2;
|
+ squares[2] - adjust2;
|
||||||
|
|
||||||
// First piece is on a1-h8 diagonal, second below: map this occurence to
|
// First piece is on a1-h8 diagonal, second below: map this occurrence to
|
||||||
// 6 to differentiate from the above case, rank_of() maps a1-d4 diagonal
|
// 6 to differentiate from the above case, rank_of() maps a1-d4 diagonal
|
||||||
// to 0...3 and finally MapB1H1H7[] maps the b1-h1-h7 triangle to 0..27.
|
// to 0...3 and finally MapB1H1H7[] maps the b1-h1-h7 triangle to 0..27.
|
||||||
else if (off_A1H8(squares[1]))
|
else if (off_A1H8(squares[1]))
|
||||||
@@ -856,7 +858,7 @@ encode_remaining:
|
|||||||
idx *= d->groupIdx[0];
|
idx *= d->groupIdx[0];
|
||||||
Square* groupSq = squares + d->groupLen[0];
|
Square* groupSq = squares + d->groupLen[0];
|
||||||
|
|
||||||
// Encode remainig pawns then pieces according to square, in ascending order
|
// Encode remaining pawns then pieces according to square, in ascending order
|
||||||
bool remainingPawns = entry->hasPawns && entry->pawnCount[1];
|
bool remainingPawns = entry->hasPawns && entry->pawnCount[1];
|
||||||
|
|
||||||
while (d->groupLen[++next])
|
while (d->groupLen[++next])
|
||||||
@@ -884,7 +886,7 @@ encode_remaining:
|
|||||||
|
|
||||||
// Group together pieces that will be encoded together. The general rule is that
|
// Group together pieces that will be encoded together. The general rule is that
|
||||||
// a group contains pieces of same type and color. The exception is the leading
|
// a group contains pieces of same type and color. The exception is the leading
|
||||||
// group that, in case of positions withouth pawns, can be formed by 3 different
|
// group that, in case of positions without pawns, can be formed by 3 different
|
||||||
// pieces (default) or by the king pair when there is not a unique piece apart
|
// pieces (default) or by the king pair when there is not a unique piece apart
|
||||||
// from the kings. When there are pawns, pawns are always first in pieces[].
|
// from the kings. When there are pawns, pawns are always first in pieces[].
|
||||||
//
|
//
|
||||||
@@ -916,7 +918,7 @@ void set_groups(T& e, PairsData* d, int order[], File f) {
|
|||||||
//
|
//
|
||||||
// This ensures unique encoding for the whole position. The order of the
|
// This ensures unique encoding for the whole position. The order of the
|
||||||
// groups is a per-table parameter and could not follow the canonical leading
|
// groups is a per-table parameter and could not follow the canonical leading
|
||||||
// pawns/pieces -> remainig pawns -> remaining pieces. In particular the
|
// pawns/pieces -> remaining pawns -> remaining pieces. In particular the
|
||||||
// first group is at order[0] position and the remaining pawns, when present,
|
// first group is at order[0] position and the remaining pawns, when present,
|
||||||
// are at order[1] position.
|
// are at order[1] position.
|
||||||
bool pp = e.hasPawns && e.pawnCount[1]; // Pawns on both sides
|
bool pp = e.hasPawns && e.pawnCount[1]; // Pawns on both sides
|
||||||
@@ -936,7 +938,7 @@ void set_groups(T& e, PairsData* d, int order[], File f) {
|
|||||||
d->groupIdx[1] = idx;
|
d->groupIdx[1] = idx;
|
||||||
idx *= Binomial[d->groupLen[1]][48 - d->groupLen[0]];
|
idx *= Binomial[d->groupLen[1]][48 - d->groupLen[0]];
|
||||||
}
|
}
|
||||||
else // Remainig pieces
|
else // Remaining pieces
|
||||||
{
|
{
|
||||||
d->groupIdx[next] = idx;
|
d->groupIdx[next] = idx;
|
||||||
idx *= Binomial[d->groupLen[next]][freeSquares];
|
idx *= Binomial[d->groupLen[next]][freeSquares];
|
||||||
@@ -946,7 +948,7 @@ void set_groups(T& e, PairsData* d, int order[], File f) {
|
|||||||
d->groupIdx[n] = idx;
|
d->groupIdx[n] = idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
// In Recursive Pairing each symbol represents a pair of childern symbols. So
|
// In Recursive Pairing each symbol represents a pair of children symbols. So
|
||||||
// read d->btree[] symbols data and expand each one in his left and right child
|
// read d->btree[] symbols data and expand each one in his left and right child
|
||||||
// symbol until reaching the leafs that represent the symbol value.
|
// symbol until reaching the leafs that represent the symbol value.
|
||||||
uint8_t set_symlen(PairsData* d, Sym s, std::vector<bool>& visited) {
|
uint8_t set_symlen(PairsData* d, Sym s, std::vector<bool>& visited) {
|
||||||
@@ -1289,7 +1291,7 @@ void Tablebases::init(const std::string& paths) {
|
|||||||
for (auto s : diagonal)
|
for (auto s : diagonal)
|
||||||
MapA1D1D4[s] = code++;
|
MapA1D1D4[s] = code++;
|
||||||
|
|
||||||
// MapKK[] encodes all the 461 possible legal positions of two kings where
|
// MapKK[] encodes all the 462 possible legal positions of two kings where
|
||||||
// the first is in the a1-d1-d4 triangle. If the first king is on the a1-d4
|
// the first is in the a1-d1-d4 triangle. If the first king is on the a1-d4
|
||||||
// diagonal, the other one shall not to be above the a1-h8 diagonal.
|
// diagonal, the other one shall not to be above the a1-h8 diagonal.
|
||||||
std::vector<std::pair<int, Square>> bothOnDiagonal;
|
std::vector<std::pair<int, Square>> bothOnDiagonal;
|
||||||
@@ -1316,7 +1318,7 @@ void Tablebases::init(const std::string& paths) {
|
|||||||
for (auto p : bothOnDiagonal)
|
for (auto p : bothOnDiagonal)
|
||||||
MapKK[p.first][p.second] = code++;
|
MapKK[p.first][p.second] = code++;
|
||||||
|
|
||||||
// Binomial[] stores the Binomial Coefficents using Pascal rule. There
|
// Binomial[] stores the Binomial Coefficients using Pascal rule. There
|
||||||
// are Binomial[k][n] ways to choose k elements from a set of n elements.
|
// are Binomial[k][n] ways to choose k elements from a set of n elements.
|
||||||
Binomial[0][0] = 1;
|
Binomial[0][0] = 1;
|
||||||
|
|
||||||
@@ -1336,7 +1338,7 @@ void Tablebases::init(const std::string& paths) {
|
|||||||
for (int leadPawnsCnt = 1; leadPawnsCnt <= 5; ++leadPawnsCnt)
|
for (int leadPawnsCnt = 1; leadPawnsCnt <= 5; ++leadPawnsCnt)
|
||||||
for (File f = FILE_A; f <= FILE_D; ++f)
|
for (File f = FILE_A; f <= FILE_D; ++f)
|
||||||
{
|
{
|
||||||
// Restart the index at every file because TB table is splitted
|
// Restart the index at every file because TB table is split
|
||||||
// by file, so we can reuse the same index for different files.
|
// by file, so we can reuse the same index for different files.
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
|
|
||||||
@@ -1521,7 +1523,7 @@ bool Tablebases::root_probe(Position& pos, Search::RootMoves& rootMoves) {
|
|||||||
// Check whether a position was repeated since the last zeroing move.
|
// Check whether a position was repeated since the last zeroing move.
|
||||||
bool rep = pos.has_repeated();
|
bool rep = pos.has_repeated();
|
||||||
|
|
||||||
int dtz, bound = Options["Syzygy50MoveRule"] ? 900 : 1;
|
int dtz, bound = Options["Syzygy50MoveRule"] ? (MAX_DTZ - 100) : 1;
|
||||||
|
|
||||||
// Probe and rank each move
|
// Probe and rank each move
|
||||||
for (auto& m : rootMoves)
|
for (auto& m : rootMoves)
|
||||||
@@ -1535,6 +1537,14 @@ bool Tablebases::root_probe(Position& pos, Search::RootMoves& rootMoves) {
|
|||||||
WDLScore wdl = -probe_wdl(pos, &result);
|
WDLScore wdl = -probe_wdl(pos, &result);
|
||||||
dtz = dtz_before_zeroing(wdl);
|
dtz = dtz_before_zeroing(wdl);
|
||||||
}
|
}
|
||||||
|
else if (pos.is_draw(1))
|
||||||
|
{
|
||||||
|
// In case a root move leads to a draw by repetition or
|
||||||
|
// 50-move rule, we set dtz to zero. Note: since we are
|
||||||
|
// only 1 ply from the root, this must be a true 3-fold
|
||||||
|
// repetition inside the game history.
|
||||||
|
dtz = 0;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Otherwise, take dtz for the new position and correct by 1 ply
|
// Otherwise, take dtz for the new position and correct by 1 ply
|
||||||
@@ -1556,8 +1566,8 @@ bool Tablebases::root_probe(Position& pos, Search::RootMoves& rootMoves) {
|
|||||||
|
|
||||||
// Better moves are ranked higher. Certain wins are ranked equally.
|
// Better moves are ranked higher. Certain wins are ranked equally.
|
||||||
// Losing moves are ranked equally unless a 50-move draw is in sight.
|
// Losing moves are ranked equally unless a 50-move draw is in sight.
|
||||||
int r = dtz > 0 ? (dtz + cnt50 <= 99 && !rep ? 1000 : 1000 - (dtz + cnt50))
|
int r = dtz > 0 ? (dtz + cnt50 <= 99 && !rep ? MAX_DTZ : MAX_DTZ - (dtz + cnt50))
|
||||||
: dtz < 0 ? (-dtz * 2 + cnt50 < 100 ? -1000 : -1000 + (-dtz + cnt50))
|
: dtz < 0 ? (-dtz * 2 + cnt50 < 100 ? -MAX_DTZ : -MAX_DTZ + (-dtz + cnt50))
|
||||||
: 0;
|
: 0;
|
||||||
m.tbRank = r;
|
m.tbRank = r;
|
||||||
|
|
||||||
@@ -1565,9 +1575,9 @@ bool Tablebases::root_probe(Position& pos, Search::RootMoves& rootMoves) {
|
|||||||
// 1 cp to cursed wins and let it grow to 49 cp as the positions gets
|
// 1 cp to cursed wins and let it grow to 49 cp as the positions gets
|
||||||
// closer to a real win.
|
// closer to a real win.
|
||||||
m.tbScore = r >= bound ? VALUE_MATE - MAX_PLY - 1
|
m.tbScore = r >= bound ? VALUE_MATE - MAX_PLY - 1
|
||||||
: r > 0 ? Value((std::max( 3, r - 800) * int(PawnValueEg)) / 200)
|
: r > 0 ? Value((std::max( 3, r - (MAX_DTZ - 200)) * int(PawnValueEg)) / 200)
|
||||||
: r == 0 ? VALUE_DRAW
|
: r == 0 ? VALUE_DRAW
|
||||||
: r > -bound ? Value((std::min(-3, r + 800) * int(PawnValueEg)) / 200)
|
: r > -bound ? Value((std::min(-3, r + (MAX_DTZ - 200)) * int(PawnValueEg)) / 200)
|
||||||
: -VALUE_MATE + MAX_PLY + 1;
|
: -VALUE_MATE + MAX_PLY + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1581,10 +1591,11 @@ bool Tablebases::root_probe(Position& pos, Search::RootMoves& rootMoves) {
|
|||||||
// A return value false indicates that not all probes were successful.
|
// A return value false indicates that not all probes were successful.
|
||||||
bool Tablebases::root_probe_wdl(Position& pos, Search::RootMoves& rootMoves) {
|
bool Tablebases::root_probe_wdl(Position& pos, Search::RootMoves& rootMoves) {
|
||||||
|
|
||||||
static const int WDL_to_rank[] = { -1000, -899, 0, 899, 1000 };
|
static const int WDL_to_rank[] = { -MAX_DTZ, -MAX_DTZ + 101, 0, MAX_DTZ - 101, MAX_DTZ };
|
||||||
|
|
||||||
ProbeState result;
|
ProbeState result;
|
||||||
StateInfo st;
|
StateInfo st;
|
||||||
|
WDLScore wdl;
|
||||||
|
|
||||||
bool rule50 = Options["Syzygy50MoveRule"];
|
bool rule50 = Options["Syzygy50MoveRule"];
|
||||||
|
|
||||||
@@ -1593,7 +1604,10 @@ bool Tablebases::root_probe_wdl(Position& pos, Search::RootMoves& rootMoves) {
|
|||||||
{
|
{
|
||||||
pos.do_move(m.pv[0], st);
|
pos.do_move(m.pv[0], st);
|
||||||
|
|
||||||
WDLScore wdl = -probe_wdl(pos, &result);
|
if (pos.is_draw(1))
|
||||||
|
wdl = WDLDraw;
|
||||||
|
else
|
||||||
|
wdl = -probe_wdl(pos, &result);
|
||||||
|
|
||||||
pos.undo_move(m.pv[0]);
|
pos.undo_move(m.pv[0]);
|
||||||
|
|
||||||
@@ -1610,3 +1624,5 @@ bool Tablebases::root_probe_wdl(Position& pos, Search::RootMoves& rootMoves) {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
#include "../search.h"
|
#include "../search.h"
|
||||||
|
|
||||||
namespace Tablebases {
|
namespace Stockfish::Tablebases {
|
||||||
|
|
||||||
enum WDLScore {
|
enum WDLScore {
|
||||||
WDLLoss = -2, // Loss
|
WDLLoss = -2, // Loss
|
||||||
@@ -31,14 +31,12 @@ enum WDLScore {
|
|||||||
WDLDraw = 0, // Draw
|
WDLDraw = 0, // Draw
|
||||||
WDLCursedWin = 1, // Win, but draw under 50-move rule
|
WDLCursedWin = 1, // Win, but draw under 50-move rule
|
||||||
WDLWin = 2, // Win
|
WDLWin = 2, // Win
|
||||||
|
|
||||||
WDLScoreNone = -1000
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Possible states after a probing operation
|
// Possible states after a probing operation
|
||||||
enum ProbeState {
|
enum ProbeState {
|
||||||
FAIL = 0, // Probe failed (missing file table)
|
FAIL = 0, // Probe failed (missing file table)
|
||||||
OK = 1, // Probe succesful
|
OK = 1, // Probe successful
|
||||||
CHANGE_STM = -1, // DTZ should check the other side
|
CHANGE_STM = -1, // DTZ should check the other side
|
||||||
ZEROING_BEST_MOVE = 2 // Best move zeroes DTZ (capture or pawn move)
|
ZEROING_BEST_MOVE = 2 // Best move zeroes DTZ (capture or pawn move)
|
||||||
};
|
};
|
||||||
@@ -73,6 +71,6 @@ inline std::ostream& operator<<(std::ostream& os, const ProbeState v) {
|
|||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace Stockfish::Tablebases
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+22
-14
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -26,6 +26,8 @@
|
|||||||
#include "syzygy/tbprobe.h"
|
#include "syzygy/tbprobe.h"
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
ThreadPool Threads; // Global object
|
ThreadPool Threads; // Global object
|
||||||
|
|
||||||
|
|
||||||
@@ -57,17 +59,14 @@ void Thread::clear() {
|
|||||||
|
|
||||||
counterMoves.fill(MOVE_NONE);
|
counterMoves.fill(MOVE_NONE);
|
||||||
mainHistory.fill(0);
|
mainHistory.fill(0);
|
||||||
lowPlyHistory.fill(0);
|
|
||||||
captureHistory.fill(0);
|
captureHistory.fill(0);
|
||||||
|
previousDepth = 0;
|
||||||
|
|
||||||
for (bool inCheck : { false, true })
|
for (bool inCheck : { false, true })
|
||||||
for (StatsType c : { NoCaptures, Captures })
|
for (StatsType c : { NoCaptures, Captures })
|
||||||
{
|
|
||||||
for (auto& to : continuationHistory[inCheck][c])
|
for (auto& to : continuationHistory[inCheck][c])
|
||||||
for (auto& h : to)
|
for (auto& h : to)
|
||||||
h->fill(0);
|
h->fill(-71);
|
||||||
continuationHistory[inCheck][c][NO_PIECE][0]->fill(Search::CounterMovePruneThreshold - 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -126,14 +125,16 @@ void Thread::idle_loop() {
|
|||||||
|
|
||||||
void ThreadPool::set(size_t requested) {
|
void ThreadPool::set(size_t requested) {
|
||||||
|
|
||||||
if (size() > 0) { // destroy any existing thread(s)
|
if (size() > 0) // destroy any existing thread(s)
|
||||||
|
{
|
||||||
main()->wait_for_search_finished();
|
main()->wait_for_search_finished();
|
||||||
|
|
||||||
while (size() > 0)
|
while (size() > 0)
|
||||||
delete back(), pop_back();
|
delete back(), pop_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (requested > 0) { // create new thread(s)
|
if (requested > 0) // create new thread(s)
|
||||||
|
{
|
||||||
push_back(new MainThread(0));
|
push_back(new MainThread(0));
|
||||||
|
|
||||||
while (size() < requested)
|
while (size() < requested)
|
||||||
@@ -158,6 +159,7 @@ void ThreadPool::clear() {
|
|||||||
|
|
||||||
main()->callsCnt = 0;
|
main()->callsCnt = 0;
|
||||||
main()->bestPreviousScore = VALUE_INFINITE;
|
main()->bestPreviousScore = VALUE_INFINITE;
|
||||||
|
main()->bestPreviousAverageScore = VALUE_INFINITE;
|
||||||
main()->previousTimeReduction = 1.0;
|
main()->previousTimeReduction = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,11 +221,14 @@ Thread* ThreadPool::get_best_thread() const {
|
|||||||
minScore = std::min(minScore, th->rootMoves[0].score);
|
minScore = std::min(minScore, th->rootMoves[0].score);
|
||||||
|
|
||||||
// Vote according to score and depth, and select the best thread
|
// Vote according to score and depth, and select the best thread
|
||||||
for (Thread* th : *this)
|
auto thread_value = [minScore](Thread* th) {
|
||||||
{
|
return (th->rootMoves[0].score - minScore + 14) * int(th->completedDepth);
|
||||||
votes[th->rootMoves[0].pv[0]] +=
|
};
|
||||||
(th->rootMoves[0].score - minScore + 14) * int(th->completedDepth);
|
|
||||||
|
|
||||||
|
for (Thread* th : *this)
|
||||||
|
votes[th->rootMoves[0].pv[0]] += thread_value(th);
|
||||||
|
|
||||||
|
for (Thread* th : *this)
|
||||||
if (abs(bestThread->rootMoves[0].score) >= VALUE_TB_WIN_IN_MAX_PLY)
|
if (abs(bestThread->rootMoves[0].score) >= VALUE_TB_WIN_IN_MAX_PLY)
|
||||||
{
|
{
|
||||||
// Make sure we pick the shortest mate / TB conversion or stave off mate the longest
|
// Make sure we pick the shortest mate / TB conversion or stave off mate the longest
|
||||||
@@ -232,9 +237,10 @@ Thread* ThreadPool::get_best_thread() const {
|
|||||||
}
|
}
|
||||||
else if ( th->rootMoves[0].score >= VALUE_TB_WIN_IN_MAX_PLY
|
else if ( th->rootMoves[0].score >= VALUE_TB_WIN_IN_MAX_PLY
|
||||||
|| ( th->rootMoves[0].score > VALUE_TB_LOSS_IN_MAX_PLY
|
|| ( th->rootMoves[0].score > VALUE_TB_LOSS_IN_MAX_PLY
|
||||||
&& votes[th->rootMoves[0].pv[0]] > votes[bestThread->rootMoves[0].pv[0]]))
|
&& ( votes[th->rootMoves[0].pv[0]] > votes[bestThread->rootMoves[0].pv[0]]
|
||||||
|
|| ( votes[th->rootMoves[0].pv[0]] == votes[bestThread->rootMoves[0].pv[0]]
|
||||||
|
&& thread_value(th) > thread_value(bestThread)))))
|
||||||
bestThread = th;
|
bestThread = th;
|
||||||
}
|
|
||||||
|
|
||||||
return bestThread;
|
return bestThread;
|
||||||
}
|
}
|
||||||
@@ -258,3 +264,5 @@ void ThreadPool::wait_for_search_finished() const {
|
|||||||
if (th != front())
|
if (th != front())
|
||||||
th->wait_for_search_finished();
|
th->wait_for_search_finished();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+11
-7
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -32,6 +32,7 @@
|
|||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "thread_win32_osx.h"
|
#include "thread_win32_osx.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
/// Thread class keeps together all the thread-related stuff. We use
|
/// Thread class keeps together all the thread-related stuff. We use
|
||||||
/// per-thread pawn and material hash tables so that once we get a
|
/// per-thread pawn and material hash tables so that once we get a
|
||||||
@@ -54,26 +55,26 @@ public:
|
|||||||
void idle_loop();
|
void idle_loop();
|
||||||
void start_searching();
|
void start_searching();
|
||||||
void wait_for_search_finished();
|
void wait_for_search_finished();
|
||||||
|
size_t id() const { return idx; }
|
||||||
|
|
||||||
Pawns::Table pawnsTable;
|
Pawns::Table pawnsTable;
|
||||||
Material::Table materialTable;
|
Material::Table materialTable;
|
||||||
size_t pvIdx, pvLast;
|
size_t pvIdx, pvLast;
|
||||||
uint64_t ttHitAverage;
|
RunningAverage complexityAverage;
|
||||||
|
std::atomic<uint64_t> nodes, tbHits, bestMoveChanges;
|
||||||
int selDepth, nmpMinPly;
|
int selDepth, nmpMinPly;
|
||||||
Color nmpColor;
|
Color nmpColor;
|
||||||
std::atomic<uint64_t> nodes, tbHits, bestMoveChanges;
|
Value bestValue, optimism[COLOR_NB];
|
||||||
|
|
||||||
Position rootPos;
|
Position rootPos;
|
||||||
StateInfo rootState;
|
StateInfo rootState;
|
||||||
Search::RootMoves rootMoves;
|
Search::RootMoves rootMoves;
|
||||||
Depth rootDepth, completedDepth;
|
Depth rootDepth, completedDepth, previousDepth;
|
||||||
|
Value rootDelta;
|
||||||
CounterMoveHistory counterMoves;
|
CounterMoveHistory counterMoves;
|
||||||
ButterflyHistory mainHistory;
|
ButterflyHistory mainHistory;
|
||||||
LowPlyHistory lowPlyHistory;
|
|
||||||
CapturePieceToHistory captureHistory;
|
CapturePieceToHistory captureHistory;
|
||||||
ContinuationHistory continuationHistory[2][2];
|
ContinuationHistory continuationHistory[2][2];
|
||||||
Score contempt;
|
|
||||||
int failedHighCnt;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -88,6 +89,7 @@ struct MainThread : public Thread {
|
|||||||
|
|
||||||
double previousTimeReduction;
|
double previousTimeReduction;
|
||||||
Value bestPreviousScore;
|
Value bestPreviousScore;
|
||||||
|
Value bestPreviousAverageScore;
|
||||||
Value iterValue[4];
|
Value iterValue[4];
|
||||||
int callsCnt;
|
int callsCnt;
|
||||||
bool stopOnPonderhit;
|
bool stopOnPonderhit;
|
||||||
@@ -128,4 +130,6 @@ private:
|
|||||||
|
|
||||||
extern ThreadPool Threads;
|
extern ThreadPool Threads;
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef THREAD_H_INCLUDED
|
#endif // #ifndef THREAD_H_INCLUDED
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -31,6 +31,8 @@
|
|||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
static const size_t TH_STACK_SIZE = 8 * 1024 * 1024;
|
static const size_t TH_STACK_SIZE = 8 * 1024 * 1024;
|
||||||
|
|
||||||
template <class T, class P = std::pair<T*, void(T::*)()>>
|
template <class T, class P = std::pair<T*, void(T::*)()>>
|
||||||
@@ -57,10 +59,16 @@ public:
|
|||||||
void join() { pthread_join(thread, NULL); }
|
void join() { pthread_join(thread, NULL); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#else // Default case: use STL classes
|
#else // Default case: use STL classes
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
typedef std::thread NativeThread;
|
typedef std::thread NativeThread;
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // #ifndef THREAD_WIN32_OSX_H_INCLUDED
|
#endif // #ifndef THREAD_WIN32_OSX_H_INCLUDED
|
||||||
|
|||||||
+13
-5
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -24,6 +24,8 @@
|
|||||||
#include "timeman.h"
|
#include "timeman.h"
|
||||||
#include "uci.h"
|
#include "uci.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
TimeManagement Time; // Our global time management object
|
TimeManagement Time; // Our global time management object
|
||||||
|
|
||||||
|
|
||||||
@@ -66,6 +68,9 @@ void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
|
|||||||
TimePoint timeLeft = std::max(TimePoint(1),
|
TimePoint timeLeft = std::max(TimePoint(1),
|
||||||
limits.time[us] + limits.inc[us] * (mtg - 1) - moveOverhead * (2 + mtg));
|
limits.time[us] + limits.inc[us] * (mtg - 1) - moveOverhead * (2 + mtg));
|
||||||
|
|
||||||
|
// Use extra time with larger increments
|
||||||
|
double optExtra = std::clamp(1.0 + 12.0 * limits.inc[us] / limits.time[us], 1.0, 1.12);
|
||||||
|
|
||||||
// A user may scale time usage by setting UCI option "Slow Mover"
|
// A user may scale time usage by setting UCI option "Slow Mover"
|
||||||
// Default is 100 and changing this value will probably lose elo.
|
// Default is 100 and changing this value will probably lose elo.
|
||||||
timeLeft = slowMover * timeLeft / 100;
|
timeLeft = slowMover * timeLeft / 100;
|
||||||
@@ -75,16 +80,17 @@ void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
|
|||||||
// game time for the current move, so also cap to 20% of available game time.
|
// game time for the current move, so also cap to 20% of available game time.
|
||||||
if (limits.movestogo == 0)
|
if (limits.movestogo == 0)
|
||||||
{
|
{
|
||||||
optScale = std::min(0.0084 + std::pow(ply + 3.0, 0.5) * 0.0042,
|
optScale = std::min(0.0120 + std::pow(ply + 3.0, 0.45) * 0.0039,
|
||||||
0.2 * limits.time[us] / double(timeLeft));
|
0.2 * limits.time[us] / double(timeLeft))
|
||||||
|
* optExtra;
|
||||||
maxScale = std::min(7.0, 4.0 + ply / 12.0);
|
maxScale = std::min(7.0, 4.0 + ply / 12.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// x moves in y seconds (+ z increment)
|
// x moves in y seconds (+ z increment)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
optScale = std::min((0.8 + ply / 128.0) / mtg,
|
optScale = std::min((0.88 + ply / 116.4) / mtg,
|
||||||
0.8 * limits.time[us] / double(timeLeft));
|
0.88 * limits.time[us] / double(timeLeft));
|
||||||
maxScale = std::min(6.3, 1.5 + 0.11 * mtg);
|
maxScale = std::min(6.3, 1.5 + 0.11 * mtg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,3 +101,5 @@ void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
|
|||||||
if (Options["Ponder"])
|
if (Options["Ponder"])
|
||||||
optimumTime += optimumTime / 4;
|
optimumTime += optimumTime / 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+5
-1
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -23,6 +23,8 @@
|
|||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
/// The TimeManagement class computes the optimal time to think depending on
|
/// The TimeManagement class computes the optimal time to think depending on
|
||||||
/// the maximum available time, the game move number and other parameters.
|
/// the maximum available time, the game move number and other parameters.
|
||||||
|
|
||||||
@@ -44,4 +46,6 @@ private:
|
|||||||
|
|
||||||
extern TimeManagement Time;
|
extern TimeManagement Time;
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef TIMEMAN_H_INCLUDED
|
#endif // #ifndef TIMEMAN_H_INCLUDED
|
||||||
|
|||||||
+6
-2
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -26,6 +26,8 @@
|
|||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
#include "uci.h"
|
#include "uci.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
TranspositionTable TT; // Our global transposition table
|
TranspositionTable TT; // Our global transposition table
|
||||||
|
|
||||||
/// TTEntry::save() populates the TTEntry with a new node's data, possibly
|
/// TTEntry::save() populates the TTEntry with a new node's data, possibly
|
||||||
@@ -40,7 +42,7 @@ void TTEntry::save(Key k, Value v, bool pv, Bound b, Depth d, Move m, Value ev)
|
|||||||
// Overwrite less valuable entries (cheapest checks first)
|
// Overwrite less valuable entries (cheapest checks first)
|
||||||
if ( b == BOUND_EXACT
|
if ( b == BOUND_EXACT
|
||||||
|| (uint16_t)k != key16
|
|| (uint16_t)k != key16
|
||||||
|| d - DEPTH_OFFSET > depth8 - 4)
|
|| d - DEPTH_OFFSET + 2 * pv > depth8 - 4)
|
||||||
{
|
{
|
||||||
assert(d > DEPTH_OFFSET);
|
assert(d > DEPTH_OFFSET);
|
||||||
assert(d < 256 + DEPTH_OFFSET);
|
assert(d < 256 + DEPTH_OFFSET);
|
||||||
@@ -156,3 +158,5 @@ int TranspositionTable::hashfull() const {
|
|||||||
|
|
||||||
return cnt / ClusterSize;
|
return cnt / ClusterSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -22,6 +22,8 @@
|
|||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
/// TTEntry struct is the 10 bytes transposition table entry, defined as below:
|
/// TTEntry struct is the 10 bytes transposition table entry, defined as below:
|
||||||
///
|
///
|
||||||
/// key 16 bit
|
/// key 16 bit
|
||||||
@@ -100,4 +102,6 @@ private:
|
|||||||
|
|
||||||
extern TranspositionTable TT;
|
extern TranspositionTable TT;
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef TT_H_INCLUDED
|
#endif // #ifndef TT_H_INCLUDED
|
||||||
|
|||||||
+8
-19
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -26,9 +26,10 @@
|
|||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
bool Tune::update_on_last;
|
bool Tune::update_on_last;
|
||||||
const UCI::Option* LastOption = nullptr;
|
const UCI::Option* LastOption = nullptr;
|
||||||
BoolConditions Conditions;
|
|
||||||
static std::map<std::string, int> TuneResults;
|
static std::map<std::string, int> TuneResults;
|
||||||
|
|
||||||
string Tune::next(string& names, bool pop) {
|
string Tune::next(string& names, bool pop) {
|
||||||
@@ -108,23 +109,7 @@ template<> void Tune::Entry<Score>::read_option() {
|
|||||||
template<> void Tune::Entry<Tune::PostUpdate>::init_option() {}
|
template<> void Tune::Entry<Tune::PostUpdate>::init_option() {}
|
||||||
template<> void Tune::Entry<Tune::PostUpdate>::read_option() { value(); }
|
template<> void Tune::Entry<Tune::PostUpdate>::read_option() { value(); }
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
// Set binary conditions according to a probability that depends
|
|
||||||
// on the corresponding parameter value.
|
|
||||||
|
|
||||||
void BoolConditions::set() {
|
|
||||||
|
|
||||||
static PRNG rng(now());
|
|
||||||
static bool startup = true; // To workaround fishtest bench
|
|
||||||
|
|
||||||
for (size_t i = 0; i < binary.size(); i++)
|
|
||||||
binary[i] = !startup && (values[i] + int(rng.rand<unsigned>() % variance) > threshold);
|
|
||||||
|
|
||||||
startup = false;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < binary.size(); i++)
|
|
||||||
sync_cout << binary[i] << sync_endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Init options with tuning session results instead of default values. Useful to
|
// Init options with tuning session results instead of default values. Useful to
|
||||||
@@ -138,7 +123,11 @@ void BoolConditions::set() {
|
|||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
void Tune::read_results() {
|
void Tune::read_results() {
|
||||||
|
|
||||||
/* ...insert your values here... */
|
/* ...insert your values here... */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+5
-35
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -24,6 +24,8 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
typedef std::pair<int, int> Range; // Option's min-max values
|
typedef std::pair<int, int> Range; // Option's min-max values
|
||||||
typedef Range (RangeFun) (int);
|
typedef Range (RangeFun) (int);
|
||||||
|
|
||||||
@@ -44,27 +46,6 @@ struct SetRange {
|
|||||||
#define SetDefaultRange SetRange(default_range)
|
#define SetDefaultRange SetRange(default_range)
|
||||||
|
|
||||||
|
|
||||||
/// BoolConditions struct is used to tune boolean conditions in the
|
|
||||||
/// code by toggling them on/off according to a probability that
|
|
||||||
/// depends on the value of a tuned integer parameter: for high
|
|
||||||
/// values of the parameter condition is always disabled, for low
|
|
||||||
/// values is always enabled, otherwise it is enabled with a given
|
|
||||||
/// probability that depnends on the parameter under tuning.
|
|
||||||
|
|
||||||
struct BoolConditions {
|
|
||||||
void init(size_t size) { values.resize(size, defaultValue), binary.resize(size, 0); }
|
|
||||||
void set();
|
|
||||||
|
|
||||||
std::vector<int> binary, values;
|
|
||||||
int defaultValue = 465, variance = 40, threshold = 500;
|
|
||||||
SetRange range = SetRange(0, 1000);
|
|
||||||
};
|
|
||||||
|
|
||||||
extern BoolConditions Conditions;
|
|
||||||
|
|
||||||
inline void set_conditions() { Conditions.set(); }
|
|
||||||
|
|
||||||
|
|
||||||
/// Tune class implements the 'magic' code that makes the setup of a fishtest
|
/// Tune class implements the 'magic' code that makes the setup of a fishtest
|
||||||
/// tuning session as easy as it can be. Mainly you have just to remove const
|
/// tuning session as easy as it can be. Mainly you have just to remove const
|
||||||
/// qualifiers from the variables you want to tune and flag them for tuning, so
|
/// qualifiers from the variables you want to tune and flag them for tuning, so
|
||||||
@@ -103,7 +84,7 @@ class Tune {
|
|||||||
|
|
||||||
static Tune& instance() { static Tune t; return t; } // Singleton
|
static Tune& instance() { static Tune t; return t; } // Singleton
|
||||||
|
|
||||||
// Use polymorphism to accomodate Entry of different types in the same vector
|
// Use polymorphism to accommodate Entry of different types in the same vector
|
||||||
struct EntryBase {
|
struct EntryBase {
|
||||||
virtual ~EntryBase() = default;
|
virtual ~EntryBase() = default;
|
||||||
virtual void init_option() = 0;
|
virtual void init_option() = 0;
|
||||||
@@ -157,14 +138,6 @@ class Tune {
|
|||||||
return add(value, (next(names), std::move(names)), args...);
|
return add(value, (next(names), std::move(names)), args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Template specialization for BoolConditions
|
|
||||||
template<typename... Args>
|
|
||||||
int add(const SetRange& range, std::string&& names, BoolConditions& cond, Args&&... args) {
|
|
||||||
for (size_t size = cond.values.size(), i = 0; i < size; i++)
|
|
||||||
add(cond.range, next(names, i == size - 1) + "_" + std::to_string(i), cond.values[i]);
|
|
||||||
return add(range, std::move(names), args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::unique_ptr<EntryBase>> list;
|
std::vector<std::unique_ptr<EntryBase>> list;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -185,9 +158,6 @@ public:
|
|||||||
|
|
||||||
#define UPDATE_ON_LAST() bool UNIQUE(p, __LINE__) = Tune::update_on_last = true
|
#define UPDATE_ON_LAST() bool UNIQUE(p, __LINE__) = Tune::update_on_last = true
|
||||||
|
|
||||||
// Some macro to tune toggling of boolean conditions
|
} // namespace Stockfish
|
||||||
#define CONDITION(x) (Conditions.binary[__COUNTER__] || (x))
|
|
||||||
#define TUNE_CONDITIONS() int UNIQUE(c, __LINE__) = (Conditions.init(__COUNTER__), 0); \
|
|
||||||
TUNE(Conditions, set_conditions)
|
|
||||||
|
|
||||||
#endif // #ifndef TUNE_H_INCLUDED
|
#endif // #ifndef TUNE_H_INCLUDED
|
||||||
|
|||||||
+5
-6
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -83,6 +83,8 @@
|
|||||||
# define pext(b, m) 0
|
# define pext(b, m) 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
#ifdef USE_POPCNT
|
#ifdef USE_POPCNT
|
||||||
constexpr bool HasPopCnt = true;
|
constexpr bool HasPopCnt = true;
|
||||||
#else
|
#else
|
||||||
@@ -189,7 +191,6 @@ enum Value : int {
|
|||||||
BishopValueMg = 825, BishopValueEg = 915,
|
BishopValueMg = 825, BishopValueEg = 915,
|
||||||
RookValueMg = 1276, RookValueEg = 1380,
|
RookValueMg = 1276, RookValueEg = 1380,
|
||||||
QueenValueMg = 2538, QueenValueEg = 2682,
|
QueenValueMg = 2538, QueenValueEg = 2682,
|
||||||
Tempo = 28,
|
|
||||||
|
|
||||||
MidgameLimit = 15258, EndgameLimit = 3915
|
MidgameLimit = 15258, EndgameLimit = 3915
|
||||||
};
|
};
|
||||||
@@ -464,10 +465,6 @@ constexpr Move make_move(Square from, Square to) {
|
|||||||
return Move((from << 6) + to);
|
return Move((from << 6) + to);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr Move reverse_move(Move m) {
|
|
||||||
return make_move(to_sq(m), from_sq(m));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<MoveType T>
|
template<MoveType T>
|
||||||
constexpr Move make(Square from, Square to, PieceType pt = KNIGHT) {
|
constexpr Move make(Square from, Square to, PieceType pt = KNIGHT) {
|
||||||
return Move(T + ((pt - KNIGHT) << 12) + (from << 6) + to);
|
return Move(T + ((pt - KNIGHT) << 12) + (from << 6) + to);
|
||||||
@@ -482,6 +479,8 @@ constexpr Key make_key(uint64_t seed) {
|
|||||||
return seed * 6364136223846793005ULL + 1442695040888963407ULL;
|
return seed * 6364136223846793005ULL + 1442695040888963407ULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef TYPES_H_INCLUDED
|
#endif // #ifndef TYPES_H_INCLUDED
|
||||||
|
|
||||||
#include "tune.h" // Global visibility to tuning setup
|
#include "tune.h" // Global visibility to tuning setup
|
||||||
|
|||||||
+87
-65
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -34,18 +34,20 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
extern vector<string> setup_bench(const Position&, istream&);
|
extern vector<string> setup_bench(const Position&, istream&);
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// FEN string of the initial position, normal chess
|
// FEN string for the initial position in standard chess
|
||||||
const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||||
|
|
||||||
|
|
||||||
// position() is called when engine receives the "position" UCI command.
|
// position() is called when the engine receives the "position" UCI command.
|
||||||
// The function sets up the position described in the given FEN string ("fen")
|
// It sets up the position that is described in the given FEN string ("fen") or
|
||||||
// or the starting position ("startpos") and then makes the moves given in the
|
// the initial position ("startpos") and then makes the moves given in the following
|
||||||
// following move list ("moves").
|
// move list ("moves").
|
||||||
|
|
||||||
void position(Position& pos, istringstream& is, StateListPtr& states) {
|
void position(Position& pos, istringstream& is, StateListPtr& states) {
|
||||||
|
|
||||||
@@ -57,7 +59,7 @@ namespace {
|
|||||||
if (token == "startpos")
|
if (token == "startpos")
|
||||||
{
|
{
|
||||||
fen = StartFEN;
|
fen = StartFEN;
|
||||||
is >> token; // Consume "moves" token if any
|
is >> token; // Consume the "moves" token, if any
|
||||||
}
|
}
|
||||||
else if (token == "fen")
|
else if (token == "fen")
|
||||||
while (is >> token && token != "moves")
|
while (is >> token && token != "moves")
|
||||||
@@ -65,10 +67,10 @@ namespace {
|
|||||||
else
|
else
|
||||||
return;
|
return;
|
||||||
|
|
||||||
states = StateListPtr(new std::deque<StateInfo>(1)); // Drop old and create a new one
|
states = StateListPtr(new std::deque<StateInfo>(1)); // Drop the old state and create a new one
|
||||||
pos.set(fen, Options["UCI_Chess960"], &states->back(), Threads.main());
|
pos.set(fen, Options["UCI_Chess960"], &states->back(), Threads.main());
|
||||||
|
|
||||||
// Parse move list (if any)
|
// Parse the move list, if any
|
||||||
while (is >> token && (m = UCI::to_move(pos, token)) != MOVE_NONE)
|
while (is >> token && (m = UCI::to_move(pos, token)) != MOVE_NONE)
|
||||||
{
|
{
|
||||||
states->emplace_back();
|
states->emplace_back();
|
||||||
@@ -76,8 +78,8 @@ namespace {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// trace_eval() prints the evaluation for the current position, consistent with the UCI
|
// trace_eval() prints the evaluation of the current position, consistent with
|
||||||
// options set so far.
|
// the UCI options set so far.
|
||||||
|
|
||||||
void trace_eval(Position& pos) {
|
void trace_eval(Position& pos) {
|
||||||
|
|
||||||
@@ -91,20 +93,20 @@ namespace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// setoption() is called when engine receives the "setoption" UCI command. The
|
// setoption() is called when the engine receives the "setoption" UCI command.
|
||||||
// function updates the UCI option ("name") to the given value ("value").
|
// The function updates the UCI option ("name") to the given value ("value").
|
||||||
|
|
||||||
void setoption(istringstream& is) {
|
void setoption(istringstream& is) {
|
||||||
|
|
||||||
string token, name, value;
|
string token, name, value;
|
||||||
|
|
||||||
is >> token; // Consume "name" token
|
is >> token; // Consume the "name" token
|
||||||
|
|
||||||
// Read option name (can contain spaces)
|
// Read the option name (can contain spaces)
|
||||||
while (is >> token && token != "value")
|
while (is >> token && token != "value")
|
||||||
name += (name.empty() ? "" : " ") + token;
|
name += (name.empty() ? "" : " ") + token;
|
||||||
|
|
||||||
// Read option value (can contain spaces)
|
// Read the option value (can contain spaces)
|
||||||
while (is >> token)
|
while (is >> token)
|
||||||
value += (value.empty() ? "" : " ") + token;
|
value += (value.empty() ? "" : " ") + token;
|
||||||
|
|
||||||
@@ -115,9 +117,9 @@ namespace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// go() is called when engine receives the "go" UCI command. The function sets
|
// go() is called when the engine receives the "go" UCI command. The function
|
||||||
// the thinking time and other parameters from the input string, then starts
|
// sets the thinking time and other parameters from the input string, then starts
|
||||||
// the search.
|
// with a search.
|
||||||
|
|
||||||
void go(Position& pos, istringstream& is, StateListPtr& states) {
|
void go(Position& pos, istringstream& is, StateListPtr& states) {
|
||||||
|
|
||||||
@@ -125,7 +127,7 @@ namespace {
|
|||||||
string token;
|
string token;
|
||||||
bool ponderMode = false;
|
bool ponderMode = false;
|
||||||
|
|
||||||
limits.startTime = now(); // As early as possible!
|
limits.startTime = now(); // The search starts as early as possible
|
||||||
|
|
||||||
while (is >> token)
|
while (is >> token)
|
||||||
if (token == "searchmoves") // Needs to be the last command on the line
|
if (token == "searchmoves") // Needs to be the last command on the line
|
||||||
@@ -149,9 +151,9 @@ namespace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// bench() is called when engine receives the "bench" command. Firstly
|
// bench() is called when the engine receives the "bench" command.
|
||||||
// a list of UCI commands is setup according to bench parameters, then
|
// Firstly, a list of UCI commands is set up according to the bench
|
||||||
// it is run one by one printing a summary at the end.
|
// parameters, then it is run one by one, printing a summary at the end.
|
||||||
|
|
||||||
void bench(Position& pos, istream& args, StateListPtr& states) {
|
void bench(Position& pos, istream& args, StateListPtr& states) {
|
||||||
|
|
||||||
@@ -182,12 +184,12 @@ namespace {
|
|||||||
}
|
}
|
||||||
else if (token == "setoption") setoption(is);
|
else if (token == "setoption") setoption(is);
|
||||||
else if (token == "position") position(pos, is, states);
|
else if (token == "position") position(pos, is, states);
|
||||||
else if (token == "ucinewgame") { Search::clear(); elapsed = now(); } // Search::clear() may take some while
|
else if (token == "ucinewgame") { Search::clear(); elapsed = now(); } // Search::clear() may take a while
|
||||||
}
|
}
|
||||||
|
|
||||||
elapsed = now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero'
|
elapsed = now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero'
|
||||||
|
|
||||||
dbg_print(); // Just before exiting
|
dbg_print();
|
||||||
|
|
||||||
cerr << "\n==========================="
|
cerr << "\n==========================="
|
||||||
<< "\nTotal time (ms) : " << elapsed
|
<< "\nTotal time (ms) : " << elapsed
|
||||||
@@ -195,36 +197,40 @@ namespace {
|
|||||||
<< "\nNodes/second : " << 1000 * nodes / elapsed << endl;
|
<< "\nNodes/second : " << 1000 * nodes / elapsed << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The win rate model returns the probability (per mille) of winning given an eval
|
// The win rate model returns the probability of winning (in per mille units) given an
|
||||||
// and a game-ply. The model fits rather accurately the LTC fishtest statistics.
|
// eval and a game ply. It fits the LTC fishtest statistics rather accurately.
|
||||||
int win_rate_model(Value v, int ply) {
|
int win_rate_model(Value v, int ply) {
|
||||||
|
|
||||||
// The model captures only up to 240 plies, so limit input (and rescale)
|
// The model only captures up to 240 plies, so limit the input and then rescale
|
||||||
double m = std::min(240, ply) / 64.0;
|
double m = std::min(240, ply) / 64.0;
|
||||||
|
|
||||||
// Coefficients of a 3rd order polynomial fit based on fishtest data
|
// The coefficients of a third-order polynomial fit is based on the fishtest data
|
||||||
// for two parameters needed to transform eval to the argument of a
|
// for two parameters that need to transform eval to the argument of a logistic
|
||||||
// logistic function.
|
// function.
|
||||||
double as[] = {-8.24404295, 64.23892342, -95.73056462, 153.86478679};
|
constexpr double as[] = { -0.58270499, 2.68512549, 15.24638015, 344.49745382};
|
||||||
double bs[] = {-3.37154371, 28.44489198, -56.67657741, 72.05858751};
|
constexpr double bs[] = { -2.65734562, 15.96509799, -20.69040836, 73.61029937 };
|
||||||
|
|
||||||
|
// Enforce that NormalizeToPawnValue corresponds to a 50% win rate at ply 64
|
||||||
|
static_assert(UCI::NormalizeToPawnValue == int(as[0] + as[1] + as[2] + as[3]));
|
||||||
|
|
||||||
double a = (((as[0] * m + as[1]) * m + as[2]) * m) + as[3];
|
double a = (((as[0] * m + as[1]) * m + as[2]) * m) + as[3];
|
||||||
double b = (((bs[0] * m + bs[1]) * m + bs[2]) * m) + bs[3];
|
double b = (((bs[0] * m + bs[1]) * m + bs[2]) * m) + bs[3];
|
||||||
|
|
||||||
// Transform eval to centipawns with limited range
|
// Transform the eval to centipawns with limited range
|
||||||
double x = std::clamp(double(100 * v) / PawnValueEg, -1000.0, 1000.0);
|
double x = std::clamp(double(v), -4000.0, 4000.0);
|
||||||
|
|
||||||
// Return win rate in per mille (rounded to nearest)
|
// Return the win rate in per mille units rounded to the nearest value
|
||||||
return int(0.5 + 1000 / (1 + std::exp((a - x) / b)));
|
return int(0.5 + 1000 / (1 + std::exp((a - x) / b)));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
/// UCI::loop() waits for a command from stdin, parses it and calls the appropriate
|
/// UCI::loop() waits for a command from the stdin, parses it and then calls the appropriate
|
||||||
/// function. Also intercepts EOF from stdin to ensure gracefully exiting if the
|
/// function. It also intercepts an end-of-file (EOF) indication from the stdin to ensure a
|
||||||
/// GUI dies unexpectedly. When called with some command line arguments, e.g. to
|
/// graceful exit if the GUI dies unexpectedly. When called with some command-line arguments,
|
||||||
/// run 'bench', once the command is executed the function returns immediately.
|
/// like running 'bench', the function returns immediately after the command is executed.
|
||||||
/// In addition to the UCI ones, also some additional debug commands are supported.
|
/// In addition to the UCI ones, some additional debug commands are also supported.
|
||||||
|
|
||||||
void UCI::loop(int argc, char* argv[]) {
|
void UCI::loop(int argc, char* argv[]) {
|
||||||
|
|
||||||
@@ -238,24 +244,24 @@ void UCI::loop(int argc, char* argv[]) {
|
|||||||
cmd += std::string(argv[i]) + " ";
|
cmd += std::string(argv[i]) + " ";
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (argc == 1 && !getline(cin, cmd)) // Block here waiting for input or EOF
|
if (argc == 1 && !getline(cin, cmd)) // Wait for an input or an end-of-file (EOF) indication
|
||||||
cmd = "quit";
|
cmd = "quit";
|
||||||
|
|
||||||
istringstream is(cmd);
|
istringstream is(cmd);
|
||||||
|
|
||||||
token.clear(); // Avoid a stale if getline() returns empty or blank line
|
token.clear(); // Avoid a stale if getline() returns nothing or a blank line
|
||||||
is >> skipws >> token;
|
is >> skipws >> token;
|
||||||
|
|
||||||
if ( token == "quit"
|
if ( token == "quit"
|
||||||
|| token == "stop")
|
|| token == "stop")
|
||||||
Threads.stop = true;
|
Threads.stop = true;
|
||||||
|
|
||||||
// The GUI sends 'ponderhit' to tell us the user has played the expected move.
|
// The GUI sends 'ponderhit' to tell that the user has played the expected move.
|
||||||
// So 'ponderhit' will be sent if we were told to ponder on the same move the
|
// So, 'ponderhit' is sent if pondering was done on the same move that the user
|
||||||
// user has played. We should continue searching but switch from pondering to
|
// has played. The search should continue, but should also switch from pondering
|
||||||
// normal search.
|
// to the normal search.
|
||||||
else if (token == "ponderhit")
|
else if (token == "ponderhit")
|
||||||
Threads.main()->ponder = false; // Switch to normal search
|
Threads.main()->ponder = false; // Switch to the normal search
|
||||||
|
|
||||||
else if (token == "uci")
|
else if (token == "uci")
|
||||||
sync_cout << "id name " << engine_info(true)
|
sync_cout << "id name " << engine_info(true)
|
||||||
@@ -268,26 +274,40 @@ void UCI::loop(int argc, char* argv[]) {
|
|||||||
else if (token == "ucinewgame") Search::clear();
|
else if (token == "ucinewgame") Search::clear();
|
||||||
else if (token == "isready") sync_cout << "readyok" << sync_endl;
|
else if (token == "isready") sync_cout << "readyok" << sync_endl;
|
||||||
|
|
||||||
// Additional custom non-UCI commands, mainly for debugging.
|
// Add custom non-UCI commands, mainly for debugging purposes.
|
||||||
// Do not use these commands during a search!
|
// These commands must not be used during a search!
|
||||||
else if (token == "flip") pos.flip();
|
else if (token == "flip") pos.flip();
|
||||||
else if (token == "bench") bench(pos, is, states);
|
else if (token == "bench") bench(pos, is, states);
|
||||||
else if (token == "d") sync_cout << pos << sync_endl;
|
else if (token == "d") sync_cout << pos << sync_endl;
|
||||||
else if (token == "eval") trace_eval(pos);
|
else if (token == "eval") trace_eval(pos);
|
||||||
else if (token == "compiler") sync_cout << compiler_info() << sync_endl;
|
else if (token == "compiler") sync_cout << compiler_info() << sync_endl;
|
||||||
else
|
else if (token == "export_net")
|
||||||
sync_cout << "Unknown command: " << cmd << sync_endl;
|
{
|
||||||
|
std::optional<std::string> filename;
|
||||||
|
std::string f;
|
||||||
|
if (is >> skipws >> f)
|
||||||
|
filename = f;
|
||||||
|
Eval::NNUE::save_eval(filename);
|
||||||
|
}
|
||||||
|
else if (token == "--help" || token == "help" || token == "--license" || token == "license")
|
||||||
|
sync_cout << "\nStockfish is a powerful chess engine for playing and analyzing."
|
||||||
|
"\nIt is released as free software licensed under the GNU GPLv3 License."
|
||||||
|
"\nStockfish is normally used with a graphical user interface (GUI) and implements"
|
||||||
|
"\nthe Universal Chess Interface (UCI) protocol to communicate with a GUI, an API, etc."
|
||||||
|
"\nFor any further information, visit https://github.com/official-stockfish/Stockfish#readme"
|
||||||
|
"\nor read the corresponding README.md and Copying.txt files distributed along with this program.\n" << sync_endl;
|
||||||
|
else if (!token.empty() && token[0] != '#')
|
||||||
|
sync_cout << "Unknown command: '" << cmd << "'. Type help for more information." << sync_endl;
|
||||||
|
|
||||||
} while (token != "quit" && argc == 1); // Command line args are one-shot
|
} while (token != "quit" && argc == 1); // The command-line arguments are one-shot
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// UCI::value() converts a Value to a string suitable for use with the UCI
|
/// UCI::value() converts a Value to a string by adhering to the UCI protocol specification:
|
||||||
/// protocol specification:
|
|
||||||
///
|
///
|
||||||
/// cp <x> The score from the engine's point of view in centipawns.
|
/// cp <x> The score from the engine's point of view in centipawns.
|
||||||
/// mate <y> Mate in y moves, not plies. If the engine is getting mated
|
/// mate <y> Mate in 'y' moves (not plies). If the engine is getting mated,
|
||||||
/// use negative values for y.
|
/// uses negative values for 'y'.
|
||||||
|
|
||||||
string UCI::value(Value v) {
|
string UCI::value(Value v) {
|
||||||
|
|
||||||
@@ -296,7 +316,7 @@ string UCI::value(Value v) {
|
|||||||
stringstream ss;
|
stringstream ss;
|
||||||
|
|
||||||
if (abs(v) < VALUE_MATE_IN_MAX_PLY)
|
if (abs(v) < VALUE_MATE_IN_MAX_PLY)
|
||||||
ss << "cp " << v * 100 / PawnValueEg;
|
ss << "cp " << v * 100 / NormalizeToPawnValue;
|
||||||
else
|
else
|
||||||
ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
|
ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
|
||||||
|
|
||||||
@@ -304,8 +324,8 @@ string UCI::value(Value v) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// UCI::wdl() report WDL statistics given an evaluation and a game ply, based on
|
/// UCI::wdl() reports the win-draw-loss (WDL) statistics given an evaluation
|
||||||
/// data gathered for fishtest LTC games.
|
/// and a game ply based on the data gathered for fishtest LTC games.
|
||||||
|
|
||||||
string UCI::wdl(Value v, int ply) {
|
string UCI::wdl(Value v, int ply) {
|
||||||
|
|
||||||
@@ -328,9 +348,9 @@ std::string UCI::square(Square s) {
|
|||||||
|
|
||||||
|
|
||||||
/// UCI::move() converts a Move to a string in coordinate notation (g1f3, a7a8q).
|
/// UCI::move() converts a Move to a string in coordinate notation (g1f3, a7a8q).
|
||||||
/// The only special case is castling, where we print in the e1g1 notation in
|
/// The only special case is castling where the e1g1 notation is printed in
|
||||||
/// normal chess mode, and in e1h1 notation in chess960 mode. Internally all
|
/// standard chess mode and in e1h1 notation it is printed in Chess960 mode.
|
||||||
/// castling moves are always encoded as 'king captures rook'.
|
/// Internally, all castling moves are always encoded as 'king captures rook'.
|
||||||
|
|
||||||
string UCI::move(Move m, bool chess960) {
|
string UCI::move(Move m, bool chess960) {
|
||||||
|
|
||||||
@@ -360,8 +380,8 @@ string UCI::move(Move m, bool chess960) {
|
|||||||
|
|
||||||
Move UCI::to_move(const Position& pos, string& str) {
|
Move UCI::to_move(const Position& pos, string& str) {
|
||||||
|
|
||||||
if (str.length() == 5) // Junior could send promotion piece in uppercase
|
if (str.length() == 5)
|
||||||
str[4] = char(tolower(str[4]));
|
str[4] = char(tolower(str[4])); // The promotion piece character must be lowercased
|
||||||
|
|
||||||
for (const auto& m : MoveList<LEGAL>(pos))
|
for (const auto& m : MoveList<LEGAL>(pos))
|
||||||
if (str == UCI::move(m, pos.is_chess960()))
|
if (str == UCI::move(m, pos.is_chess960()))
|
||||||
@@ -369,3 +389,5 @@ Move UCI::to_move(const Position& pos, string& str) {
|
|||||||
|
|
||||||
return MOVE_NONE;
|
return MOVE_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -24,21 +24,30 @@
|
|||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
class Position;
|
class Position;
|
||||||
|
|
||||||
namespace UCI {
|
namespace UCI {
|
||||||
|
|
||||||
|
// Normalizes the internal value as reported by evaluate or search
|
||||||
|
// to the UCI centipawn result used in output. This value is derived from
|
||||||
|
// the win_rate_model() such that Stockfish outputs an advantage of
|
||||||
|
// "100 centipawns" for a position if the engine has a 50% probability to win
|
||||||
|
// from this position in selfplay at fishtest LTC time control.
|
||||||
|
const int NormalizeToPawnValue = 361;
|
||||||
|
|
||||||
class Option;
|
class Option;
|
||||||
|
|
||||||
/// Custom comparator because UCI options should be case insensitive
|
/// Define a custom comparator, because the UCI options should be case-insensitive
|
||||||
struct CaseInsensitiveLess {
|
struct CaseInsensitiveLess {
|
||||||
bool operator() (const std::string&, const std::string&) const;
|
bool operator() (const std::string&, const std::string&) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Our options container is actually a std::map
|
/// The options container is defined as a std::map
|
||||||
typedef std::map<std::string, Option, CaseInsensitiveLess> OptionsMap;
|
typedef std::map<std::string, Option, CaseInsensitiveLess> OptionsMap;
|
||||||
|
|
||||||
/// Option class implements an option as defined by UCI protocol
|
/// The Option class implements each option as specified by the UCI protocol
|
||||||
class Option {
|
class Option {
|
||||||
|
|
||||||
typedef void (*OnChange)(const Option&);
|
typedef void (*OnChange)(const Option&);
|
||||||
@@ -70,7 +79,7 @@ void loop(int argc, char* argv[]);
|
|||||||
std::string value(Value v);
|
std::string value(Value v);
|
||||||
std::string square(Square s);
|
std::string square(Square s);
|
||||||
std::string move(Move m, bool chess960);
|
std::string move(Move m, bool chess960);
|
||||||
std::string pv(const Position& pos, Depth depth, Value alpha, Value beta);
|
std::string pv(const Position& pos, Depth depth);
|
||||||
std::string wdl(Value v, int ply);
|
std::string wdl(Value v, int ply);
|
||||||
Move to_move(const Position& pos, std::string& str);
|
Move to_move(const Position& pos, std::string& str);
|
||||||
|
|
||||||
@@ -78,4 +87,6 @@ Move to_move(const Position& pos, std::string& str);
|
|||||||
|
|
||||||
extern UCI::OptionsMap Options;
|
extern UCI::OptionsMap Options;
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|
||||||
#endif // #ifndef UCI_H_INCLUDED
|
#endif // #ifndef UCI_H_INCLUDED
|
||||||
|
|||||||
+7
-5
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
|
Copyright (C) 2004-2022 The Stockfish developers (see AUTHORS file)
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -31,6 +31,8 @@
|
|||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
|
namespace Stockfish {
|
||||||
|
|
||||||
UCI::OptionsMap Options; // Global object
|
UCI::OptionsMap Options; // Global object
|
||||||
|
|
||||||
namespace UCI {
|
namespace UCI {
|
||||||
@@ -59,9 +61,7 @@ void init(OptionsMap& o) {
|
|||||||
constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048;
|
constexpr int MaxHashMB = Is64Bit ? 33554432 : 2048;
|
||||||
|
|
||||||
o["Debug Log File"] << Option("", on_logger);
|
o["Debug Log File"] << Option("", on_logger);
|
||||||
o["Contempt"] << Option(24, -100, 100);
|
o["Threads"] << Option(1, 1, 1024, on_threads);
|
||||||
o["Analysis Contempt"] << Option("Both var Off var White var Black var Both", "Both");
|
|
||||||
o["Threads"] << Option(1, 1, 512, on_threads);
|
|
||||||
o["Hash"] << Option(16, 1, MaxHashMB, on_hash_size);
|
o["Hash"] << Option(16, 1, MaxHashMB, on_hash_size);
|
||||||
o["Clear Hash"] << Option(on_clear_hash);
|
o["Clear Hash"] << Option(on_clear_hash);
|
||||||
o["Ponder"] << Option(false);
|
o["Ponder"] << Option(false);
|
||||||
@@ -164,7 +164,7 @@ Option& Option::operator=(const string& v) {
|
|||||||
|
|
||||||
assert(!type.empty());
|
assert(!type.empty());
|
||||||
|
|
||||||
if ( (type != "button" && v.empty())
|
if ( (type != "button" && type != "string" && v.empty())
|
||||||
|| (type == "check" && v != "true" && v != "false")
|
|| (type == "check" && v != "true" && v != "false")
|
||||||
|| (type == "spin" && (stof(v) < min || stof(v) > max)))
|
|| (type == "spin" && (stof(v) < min || stof(v) > max)))
|
||||||
return *this;
|
return *this;
|
||||||
@@ -190,3 +190,5 @@ Option& Option::operator=(const string& v) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace UCI
|
} // namespace UCI
|
||||||
|
|
||||||
|
} // namespace Stockfish
|
||||||
|
|||||||
+11
-11
@@ -13,7 +13,7 @@ case $1 in
|
|||||||
--valgrind)
|
--valgrind)
|
||||||
echo "valgrind testing started"
|
echo "valgrind testing started"
|
||||||
prefix=''
|
prefix=''
|
||||||
exeprefix='valgrind --error-exitcode=42'
|
exeprefix='valgrind --error-exitcode=42 --errors-for-leak-kinds=all --leak-check=full'
|
||||||
postfix='1>/dev/null'
|
postfix='1>/dev/null'
|
||||||
threads="1"
|
threads="1"
|
||||||
;;
|
;;
|
||||||
@@ -39,16 +39,16 @@ case $1 in
|
|||||||
threads="2"
|
threads="2"
|
||||||
|
|
||||||
cat << EOF > tsan.supp
|
cat << EOF > tsan.supp
|
||||||
race:TTEntry::move
|
race:Stockfish::TTEntry::move
|
||||||
race:TTEntry::depth
|
race:Stockfish::TTEntry::depth
|
||||||
race:TTEntry::bound
|
race:Stockfish::TTEntry::bound
|
||||||
race:TTEntry::save
|
race:Stockfish::TTEntry::save
|
||||||
race:TTEntry::value
|
race:Stockfish::TTEntry::value
|
||||||
race:TTEntry::eval
|
race:Stockfish::TTEntry::eval
|
||||||
race:TTEntry::is_pv
|
race:Stockfish::TTEntry::is_pv
|
||||||
|
|
||||||
race:TranspositionTable::probe
|
race:Stockfish::TranspositionTable::probe
|
||||||
race:TranspositionTable::hashfull
|
race:Stockfish::TranspositionTable::hashfull
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ cat << EOF > game.exp
|
|||||||
expect "bestmove"
|
expect "bestmove"
|
||||||
|
|
||||||
send "position fen 5rk1/1K4p1/8/8/3B4/8/8/8 b - - 0 1\n"
|
send "position fen 5rk1/1K4p1/8/8/3B4/8/8/8 b - - 0 1\n"
|
||||||
send "go depth 20\n"
|
send "go depth 10\n"
|
||||||
expect "bestmove"
|
expect "bestmove"
|
||||||
|
|
||||||
send "quit\n"
|
send "quit\n"
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ cat << EOF > repeat.exp
|
|||||||
expect eof
|
expect eof
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# to increase the likelyhood of finding a non-reproducible case,
|
# to increase the likelihood of finding a non-reproducible case,
|
||||||
# the allowed number of nodes are varied systematically
|
# the allowed number of nodes are varied systematically
|
||||||
for i in `seq 1 20`
|
for i in `seq 1 20`
|
||||||
do
|
do
|
||||||
|
|||||||
Reference in New Issue
Block a user