mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 15:37:47 +00:00
Compare commits
568 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b4c239b625 | |||
| 622360ad66 | |||
| 340e9ea509 | |||
| 7b6fa353a3 | |||
| de7182f4ee | |||
| 6ab92d2e1c | |||
| bb58bc215c | |||
| 6e66e7aae2 | |||
| 4b88bea4fc | |||
| 3925750945 | |||
| cf5d683408 | |||
| 3f2ec5b3d5 | |||
| b92206305f | |||
| d2274e609c | |||
| 4111f36f45 | |||
| 0e508f30bb | |||
| 4350a66ffa | |||
| 8a9c298dee | |||
| 2a7213f720 | |||
| b9f1c9bf3f | |||
| 3cbb05b1b8 | |||
| 68209c9121 | |||
| 30a905c95d | |||
| 05aa34e00e | |||
| cd732c080b | |||
| df50ea5dc6 | |||
| 9315ba60e6 | |||
| a6fe035977 | |||
| 8bb7a73708 | |||
| 3f1eb85a1c | |||
| 4a0db9ea3c | |||
| 7a61368971 | |||
| 081af90805 | |||
| bc3b148d57 | |||
| 9fff272209 | |||
| 542a2b39ed | |||
| 738a6dfd4c | |||
| 97d2cc9a9c | |||
| cb0111d3db | |||
| 0370077c37 | |||
| d615f15fce | |||
| 489357d7b2 | |||
| 8141bdd179 | |||
| bbf9daa175 | |||
| 13d06edb84 | |||
| 49b1591505 | |||
| 33b2f6398c | |||
| 0fa957cf66 | |||
| 4bef7aa5cd | |||
| 767c4ad1fc | |||
| 2bfaf45455 | |||
| f923dc0fe5 | |||
| e846a9306d | |||
| 10bb2e6cdb | |||
| 166bf90e41 | |||
| 4aa091cf44 | |||
| 8a4821923a | |||
| 6307fd08e6 | |||
| 3ac3b68540 | |||
| 28543cddc6 | |||
| f3b8a69919 | |||
| 96c3a1f2ec | |||
| d0f09de2d2 | |||
| 881cab2525 | |||
| 4d22d3e52d | |||
| 41cc4eb953 | |||
| b5581b7779 | |||
| 348cd5ed74 | |||
| f1088c9822 | |||
| 198418ee67 | |||
| bd4d2b0576 | |||
| c569cf263d | |||
| d96c1c32a2 | |||
| 571f54b176 | |||
| fae57273b2 | |||
| 9afa03b80e | |||
| ba2a2c34bb | |||
| c08e05b494 | |||
| 6184d2b2ac | |||
| 9ca014df49 | |||
| e12fc10b5c | |||
| 313f403733 | |||
| c9f80660a6 | |||
| d44701be4b | |||
| 2660a9145e | |||
| a4eda3056e | |||
| ae98927885 | |||
| c4c2e08f0d | |||
| 6e36860554 | |||
| bb56779cb6 | |||
| 38471697b7 | |||
| 50287a55d3 | |||
| 0d5fe2f156 | |||
| af1ddfd83b | |||
| 4bd24da161 | |||
| 53c07c34bb | |||
| 0365b08601 | |||
| 41bc0d5660 | |||
| 12e79be910 | |||
| 3913726d1c | |||
| 2ac35027d5 | |||
| a05793517f | |||
| ee0f5cd303 | |||
| e0f317afaa | |||
| a6fa6a9e92 | |||
| d2d4e85f25 | |||
| d2752fdc15 | |||
| 0f48095759 | |||
| 3279655f12 | |||
| e7cfa5d020 | |||
| 8c4f0ffa1d | |||
| a781535168 | |||
| af6072c8b7 | |||
| 1e9397a2df | |||
| f0a7bed6fb | |||
| efd4ca27c4 | |||
| 34321fcc2d | |||
| 062eb074c8 | |||
| 06a853eb9c | |||
| 745160572f | |||
| 9d219c07e4 | |||
| a834bfe833 | |||
| 6c36e65193 | |||
| fc3af7c4fb | |||
| 88de112b84 | |||
| 86e467520f | |||
| 2af2c67650 | |||
| 528507b303 | |||
| 231103d2cf | |||
| e4f8a4fa7f | |||
| 9597ad8cab | |||
| b939788f9d | |||
| 6b5d671cdc | |||
| f7bae2de82 | |||
| 8ef9bc5a95 | |||
| c5d6ae8c96 | |||
| a0486ecb40 | |||
| 6cc5614124 | |||
| 31b8243272 | |||
| 626d43a329 | |||
| 3d6995eae8 | |||
| d5d23bfd2d | |||
| beaadd6547 | |||
| d702203113 | |||
| 04407e35d6 | |||
| 335dc2d021 | |||
| fd4585ef07 | |||
| e8005ebe56 | |||
| 91a76331ca | |||
| aacee91a5a | |||
| bf01bd0755 | |||
| c163c2fcb0 | |||
| 255df4ffae | |||
| 76d4a80084 | |||
| fc0a1f37cf | |||
| 4d647428d8 | |||
| de0889612f | |||
| 6634f41ac9 | |||
| 8d1625d6df | |||
| 155d5417d9 | |||
| cb4bda0f49 | |||
| d4cb80b210 | |||
| 06e0134cbc | |||
| 12ef8f71a2 | |||
| 5a7cdadfb3 | |||
| 213166ba22 | |||
| d6252ef202 | |||
| 3df8cabb84 | |||
| 11967e89cd | |||
| 45072612d4 | |||
| c794c8c801 | |||
| aef7076c34 | |||
| a64d524d02 | |||
| 82f7d507ea | |||
| f7cc0026e3 | |||
| 73e8daa150 | |||
| 66af80972a | |||
| 0143c6f0c2 | |||
| 108f0da4d7 | |||
| e9aeaad052 | |||
| ae0bb6dc2b | |||
| 6413d9b1f9 | |||
| 62619fa228 | |||
| b88374b14a | |||
| 759d129e47 | |||
| 04a228f9c8 | |||
| 06a8fd2154 | |||
| 0cfb653eec | |||
| e408fd7b10 | |||
| 76f9cd4df1 | |||
| d9cac9a414 | |||
| f2681232e5 | |||
| d5e3e7d207 | |||
| c8ef80f466 | |||
| c959871a59 | |||
| 9953bfff62 | |||
| 5ce630584c | |||
| 29070bd01a | |||
| 367304e930 | |||
| 8ff2fcf299 | |||
| 280022baf7 | |||
| a03e98dcd3 | |||
| f0f6da2d30 | |||
| 62937d1007 | |||
| d40e7ee209 | |||
| ed26d71354 | |||
| bd59560480 | |||
| 1940485030 | |||
| 759b3c79cf | |||
| 350dff4464 | |||
| 96362fe3df | |||
| 8ab12c9012 | |||
| 8db75dd9ec | |||
| edf4c07d25 | |||
| b605103a34 | |||
| c5f6bd517c | |||
| b2961aded6 | |||
| efe702e9f5 | |||
| 840605c14e | |||
| 6e9337b107 | |||
| c3af52c43b | |||
| 5dc381a566 | |||
| 2c5dfb3122 | |||
| 1093047e7d | |||
| 82697f1193 | |||
| 65c3bb8586 | |||
| 6d8f583af2 | |||
| 56a104e2e0 | |||
| d42e6338ea | |||
| 43682d08f7 | |||
| 3192b09fe0 | |||
| 450f04969c | |||
| cad300cfab | |||
| b87308692a | |||
| f35e52f030 | |||
| 94b3cdd908 | |||
| 94abc2a0cf | |||
| 59d10374ca | |||
| ad5d86c771 | |||
| de642f16db | |||
| cccbecb6f8 | |||
| 1463881153 | |||
| 29bc128384 | |||
| ad2a0e356e | |||
| d438720a1c | |||
| 71cc01c2ef | |||
| 7a03450bd9 | |||
| ebb3e7df65 | |||
| 2ec36f8ae8 | |||
| 2e21aba8d9 | |||
| 8dd6875240 | |||
| 16b31bb249 | |||
| 5d57bb467a | |||
| 9246e4a6f9 | |||
| a09eee5798 | |||
| 52f92d05a9 | |||
| 820c5c25b6 | |||
| 6c898a10be | |||
| 67f5f54a29 | |||
| b2b0013d11 | |||
| 80ea80e451 | |||
| 860223c5e6 | |||
| 4c57cf0ead | |||
| baab8be324 | |||
| cb1324312d | |||
| d71adc5bd9 | |||
| 211ebc5c7a | |||
| 917fe69f84 | |||
| 69067e1988 | |||
| 312a248fa9 | |||
| ef61886332 | |||
| 414a3e6ee3 | |||
| e316e432d0 | |||
| d93baae220 | |||
| 44a7db0f9a | |||
| 83c828f31e | |||
| 0f6f42cd83 | |||
| 378c8bdbb8 | |||
| 2e11388581 | |||
| 0a5b03af3f | |||
| fd4d800c98 | |||
| a5a98d822e | |||
| 254d995e18 | |||
| 5451687efb | |||
| b61759e907 | |||
| b508f9561c | |||
| 1b6459195c | |||
| aa88261a8f | |||
| 33682bfb98 | |||
| 66ce8ad5fd | |||
| 9afa1d7330 | |||
| 2ba47416cb | |||
| 7d4d3a2c3a | |||
| 1c50d8cbf5 | |||
| 2198cd0524 | |||
| b53239d641 | |||
| 83e829c9dc | |||
| f6981b1321 | |||
| be6fafd079 | |||
| 020dd69a35 | |||
| 70262f2027 | |||
| 196ae7c38b | |||
| be382bb0cf | |||
| d193482213 | |||
| 54f6ce70fd | |||
| 822695d4d3 | |||
| 2acda1fde3 | |||
| 28b6a457c2 | |||
| ccd6bad512 | |||
| 8a5a64eac5 | |||
| a87a1005ad | |||
| 7dd1f4a7c0 | |||
| d64ffd9621 | |||
| c769d4df84 | |||
| 03a9b3bd8d | |||
| 53239d7d3d | |||
| 3f44f5303b | |||
| 87452f3a8c | |||
| 8cfcca12d1 | |||
| 0a74c16ffe | |||
| 652199d840 | |||
| 4bc11984fc | |||
| 486c8175c4 | |||
| e0d2fdc843 | |||
| e50af36a00 | |||
| 287e2e2f74 | |||
| 86ac50403d | |||
| 0dc3b0978d | |||
| 9d79138682 | |||
| 43c186c645 | |||
| b36489742b | |||
| 452e5154cf | |||
| 07b5a28a68 | |||
| c33af32dad | |||
| 0e949ac2c9 | |||
| ba4e215493 | |||
| 043a469f83 | |||
| 21926ce2d8 | |||
| ed8286eb1b | |||
| 3ac47c84d3 | |||
| 741523eda8 | |||
| 04eb87fd08 | |||
| 323925b91c | |||
| 5ba4373522 | |||
| 3fd7e37868 | |||
| 9d95d43c57 | |||
| e385f194e9 | |||
| 7b4c9852e1 | |||
| bf485f4aff | |||
| a2b8f91cfa | |||
| d5f883ab29 | |||
| 92c39522b1 | |||
| 5ef94eb970 | |||
| 002bf4d8db | |||
| 5ea327d924 | |||
| fe60caba94 | |||
| 7aa7dfd4df | |||
| fa5b0936ee | |||
| 45e254a0a0 | |||
| 444d99b6d2 | |||
| e10255339f | |||
| daf0fe1f57 | |||
| 4d511512d2 | |||
| 9c35b9365d | |||
| 01d97521fd | |||
| d482e3a890 | |||
| 9001f55147 | |||
| ae6a4ebf1f | |||
| 232c50fed0 | |||
| c3e964f35e | |||
| df6cb446ea | |||
| bdeda52efd | |||
| 5837228aa0 | |||
| 2783203428 | |||
| 66c5eaebd8 | |||
| 44236f4ed9 | |||
| 5410424e3d | |||
| 750dfa0521 | |||
| dbc984d9f8 | |||
| f731bcadb7 | |||
| b40e45c1cc | |||
| 53c2d9df5e | |||
| b24bd762b2 | |||
| 722e1e0da6 | |||
| 973ede008a | |||
| a6ae2d3a31 | |||
| 272e4d1ac7 | |||
| e551afbab7 | |||
| d31f068312 | |||
| 377d77dbe9 | |||
| 36a93d90f7 | |||
| 0371a8f8c4 | |||
| c8e5384c3a | |||
| 802fca6fdd | |||
| 01b6cdb76b | |||
| c0cb713a00 | |||
| 6d24ef8585 | |||
| 69eb391cd7 | |||
| 7e897a64f2 | |||
| 05513a6641 | |||
| fa1e3427bd | |||
| 77342126d8 | |||
| 612d93234b | |||
| 0149a4c3d6 | |||
| 336901fdb0 | |||
| 3cb0200459 | |||
| 2c237da546 | |||
| b73016bb41 | |||
| 27ba611a3d | |||
| f907d5b7d9 | |||
| ebc563059c | |||
| 659990b43f | |||
| 6d89d0b64a | |||
| 1781439fc2 | |||
| ecd3218b6b | |||
| 24df0f72c0 | |||
| 1d31065e1d | |||
| c216dcbe7b | |||
| cf893bcded | |||
| 732aa34e3d | |||
| 862934d7ae | |||
| 7edd1f7ccd | |||
| 0c1f119069 | |||
| 99d914985f | |||
| 6b4959e3e0 | |||
| ae97941628 | |||
| 69ec09bd4b | |||
| 25296547d0 | |||
| 321a27fbe3 | |||
| 8b15961349 | |||
| e9f26cccdd | |||
| a18c2c2c3f | |||
| b948b037a5 | |||
| e06a117d5e | |||
| b1b19343cd | |||
| 49a9d4cf99 | |||
| d06a8d0c18 | |||
| b48439e906 | |||
| 6b9a22b40d | |||
| 0868de705d | |||
| 9da3b44ddc | |||
| ced29248c9 | |||
| 2d96e8fbe9 | |||
| d4b9ee0f1d | |||
| 57a3334322 | |||
| 9d3ed9ed2e | |||
| 06175c6055 | |||
| 1454831220 | |||
| b258b4fee7 | |||
| 72a501c6fe | |||
| 35b77b120e | |||
| ebe021f6a5 | |||
| 06eba14dc9 | |||
| c5de4080db | |||
| 3b7c1a17e4 | |||
| afe75571d8 | |||
| 30c583204f | |||
| 352bd6f5aa | |||
| c80d52c845 | |||
| c076216a32 | |||
| a6d6a2c2fa | |||
| d01b66ae8f | |||
| c3d2e6aba9 | |||
| d490bb9973 | |||
| cc76524c2e | |||
| 3627348e2b | |||
| 728ce2195c | |||
| 1810c4d758 | |||
| 8f7e032b8c | |||
| f1e3dfea74 | |||
| 9f48e1ec15 | |||
| eefbe967c2 | |||
| c243cd5f4a | |||
| 05cf45f2d1 | |||
| faedcf08a8 | |||
| 83fb4547f6 | |||
| 1e814e0ca0 | |||
| e0d91f4c44 | |||
| a753e20bd4 | |||
| 5205d44f87 | |||
| 0553b46829 | |||
| ddecdc97d7 | |||
| fa24cc25a4 | |||
| 5254a6040c | |||
| 471f7a1b5c | |||
| 58c181de9a | |||
| cf4a38e0cb | |||
| 9eed183489 | |||
| 243a9f5484 | |||
| 9f8f093fd6 | |||
| de02768af7 | |||
| 99cd513264 | |||
| d2971f3fca | |||
| d40351243b | |||
| d8f683760c | |||
| 332b5013b5 | |||
| 34e47ca87d | |||
| e0504ab876 | |||
| d9dd520896 | |||
| 394e9cd892 | |||
| ba15781be8 | |||
| d39ffbeea6 | |||
| 3ab3e55bb5 | |||
| 1c316c41bb | |||
| 90b052462c | |||
| 8b2c81d3ea | |||
| fe99de20ff | |||
| 6b16ebc825 | |||
| e3a8b8bcff | |||
| 1052ce74f6 | |||
| e258c5a779 | |||
| b7b9d7c9c7 | |||
| 881a9dfb0a | |||
| 43f6b33e50 | |||
| ab4f498bbc | |||
| 8765f9ce16 | |||
| 3728e833aa | |||
| af7412e58a | |||
| 1ceaea701b | |||
| f72b7dc99a | |||
| ee22b61f5e | |||
| 8c61bbda54 | |||
| 847bc0e80f | |||
| 1b62d413c1 | |||
| 589049a0e5 | |||
| a47bbca0ea | |||
| 0d33466bcd | |||
| 46d066b041 | |||
| bf8b45fe63 | |||
| e70da0d2eb | |||
| 1e76ba7cec | |||
| 535435b7fc | |||
| 7a3844e6ef | |||
| fbb2ffacfd | |||
| 8f30d233f8 | |||
| ec83e8a72c | |||
| e7289465b9 | |||
| 8ceb1ff53b | |||
| 2ec626ddae | |||
| 9eccba7761 | |||
| ca464fc89e | |||
| 0d9a9f5e98 | |||
| 6036303bb6 | |||
| b5d10d17c2 | |||
| 797602938d | |||
| 76d113f5f0 | |||
| 18df1698f4 | |||
| 7f4de0196b | |||
| cddc8d4546 | |||
| a90fc4c877 | |||
| e6c2899020 | |||
| de269ee18e | |||
| dc4655e1f9 | |||
| 0fa80c9ba3 | |||
| 61c727fdcb | |||
| 876f07cbee | |||
| f5d3f0ded6 | |||
| e4659693de | |||
| 52cb348023 | |||
| 0d669be76c | |||
| 23f384cac3 | |||
| b915fdc889 | |||
| 67d19447f4 | |||
| d4abf54247 | |||
| 445aade39c | |||
| f3cd7002aa | |||
| c43146edb7 | |||
| c0bb041539 |
+33
-14
@@ -9,20 +9,21 @@ matrix:
|
|||||||
addons:
|
addons:
|
||||||
apt:
|
apt:
|
||||||
sources: ['ubuntu-toolchain-r-test']
|
sources: ['ubuntu-toolchain-r-test']
|
||||||
packages: ['g++-6', 'g++-6-multilib', 'g++-multilib', 'valgrind']
|
packages: ['g++-7', 'g++-7-multilib', 'g++-multilib', 'valgrind', 'expect', 'curl']
|
||||||
env:
|
env:
|
||||||
- COMPILER=g++-6
|
- COMPILER=g++-7
|
||||||
- COMP=gcc
|
- COMP=gcc
|
||||||
|
|
||||||
- os: linux
|
- os: linux
|
||||||
compiler: clang
|
compiler: clang
|
||||||
addons:
|
addons:
|
||||||
apt:
|
apt:
|
||||||
sources: ['ubuntu-toolchain-r-test']
|
sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-5.0']
|
||||||
packages: ['clang', 'g++-multilib', 'valgrind']
|
packages: ['clang-5.0', 'llvm-5.0-dev', 'g++-multilib', 'valgrind', 'expect', 'curl']
|
||||||
env:
|
env:
|
||||||
- COMPILER=clang++
|
- COMPILER=clang++-5.0
|
||||||
- COMP=clang
|
- COMP=clang
|
||||||
|
- LDFLAGS=-fuse-ld=gold
|
||||||
|
|
||||||
- os: osx
|
- os: osx
|
||||||
compiler: gcc
|
compiler: gcc
|
||||||
@@ -44,12 +45,30 @@ before_script:
|
|||||||
- cd src
|
- cd src
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- make clean && make build ARCH=x86-64 && ./stockfish bench 2>&1 >/dev/null | grep 'Nodes searched' | tee bench1
|
# Obtain bench reference from git log
|
||||||
- make clean && make build ARCH=x86-32 && ./stockfish bench 2>&1 >/dev/null | grep 'Nodes searched' | tee bench2
|
- git log HEAD | grep "\b[Bb]ench[ :]\+[0-9]\{7\}" | head -n 1 | sed "s/[^0-9]*\([0-9]*\).*/\1/g" > git_sig
|
||||||
- echo "Checking for same bench numbers..."
|
- export benchref=$(cat git_sig)
|
||||||
- diff bench1 bench2 > result
|
- echo "Reference bench:" $benchref
|
||||||
- test ! -s result
|
#
|
||||||
# if valgrind is available check the build is without error, reduce depth to speedup testing, but not too shallow to catch more cases.
|
# Verify bench number against various builds
|
||||||
- if [ -x "$(command -v valgrind )" ] ; then make clean && make ARCH=x86-64 debug=yes build && valgrind --error-exitcode=42 ./stockfish bench 128 1 10 default depth 1>/dev/null ; fi
|
- export CXXFLAGS=-Werror
|
||||||
# use g++-6 as a proxy for having sanitizers ... might need revision as they become available for more recent versions of clang/gcc than trusty provides
|
- make clean && make -j2 ARCH=x86-64 optimize=no debug=yes build && ../tests/signature.sh $benchref
|
||||||
- if [[ "$COMPILER" == "g++-6" ]]; then make clean && make ARCH=x86-64 sanitize=yes build && ! ./stockfish bench 2>&1 | grep "runtime error:" ; fi
|
- make clean && make -j2 ARCH=x86-32 optimize=no debug=yes build && ../tests/signature.sh $benchref
|
||||||
|
- make clean && make -j2 ARCH=x86-32 build && ../tests/signature.sh $benchref
|
||||||
|
- make clean && make -j2 ARCH=x86-64 build && ../tests/signature.sh $benchref
|
||||||
|
#
|
||||||
|
# Check perft and reproducible search
|
||||||
|
- ../tests/perft.sh
|
||||||
|
- ../tests/reprosearch.sh
|
||||||
|
#
|
||||||
|
# Valgrind
|
||||||
|
#
|
||||||
|
- export CXXFLAGS=-O1
|
||||||
|
- if [ -x "$(command -v valgrind )" ]; then make clean && make -j2 ARCH=x86-64 debug=yes optimize=no build > /dev/null && ../tests/instrumented.sh --valgrind; fi
|
||||||
|
- if [ -x "$(command -v valgrind )" ]; then ../tests/instrumented.sh --valgrind-thread; fi
|
||||||
|
#
|
||||||
|
# Sanitizer
|
||||||
|
#
|
||||||
|
# Use g++-7 as a proxy for having sanitizers, might need revision as they become available for more recent versions of clang/gcc
|
||||||
|
- if [[ "$COMPILER" == "g++-7" ]]; then make clean && make -j2 ARCH=x86-64 sanitize=undefined optimize=no debug=yes build > /dev/null && ../tests/instrumented.sh --sanitizer-undefined; fi
|
||||||
|
- if [[ "$COMPILER" == "g++-7" ]]; then make clean && make -j2 ARCH=x86-64 sanitize=thread optimize=no debug=yes build > /dev/null && ../tests/instrumented.sh --sanitizer-thread; fi
|
||||||
|
|||||||
@@ -1,98 +1,129 @@
|
|||||||
# Generated with git shortlog -sn | cut -c8-', which sorts by commits (manually ordered the first four authors)
|
# List of authors for Stockfish, updated for version 10
|
||||||
|
|
||||||
Tord Romstad
|
Tord Romstad (romstad)
|
||||||
Marco Costalba
|
Marco Costalba (mcostalba)
|
||||||
Joona Kiiski
|
Joona Kiiski (zamar)
|
||||||
Gary Linscott
|
Gary Linscott (glinscott)
|
||||||
lucasart
|
|
||||||
mstembera
|
Aditya (absimaldata)
|
||||||
Lucas Braesch
|
Ajith Chandy Jose (ajithcj)
|
||||||
Stefan Geschwentner
|
Alain Savard (Rocky640)
|
||||||
Reuven Peleg
|
|
||||||
Chris Caino
|
|
||||||
joergoster
|
|
||||||
VoyagerOne
|
|
||||||
Jean-Francois Romang
|
|
||||||
homoSapiensSapiens
|
|
||||||
Alain SAVARD
|
|
||||||
Arjun Temurnikar
|
|
||||||
Stéphane Nicolet
|
|
||||||
Uri Blass
|
|
||||||
jundery
|
|
||||||
Ralph Stößer
|
|
||||||
Ajith
|
|
||||||
Leonid Pechenik
|
|
||||||
Stefano80
|
|
||||||
Tom Vijlbrief
|
|
||||||
hxim
|
|
||||||
snicolet
|
|
||||||
Daylen Yang
|
|
||||||
Henri Wiechers
|
|
||||||
Jonathan Calovski
|
|
||||||
mbootsector
|
|
||||||
David Zar
|
|
||||||
Eelco de Groot
|
|
||||||
Jerry Donald
|
|
||||||
Joerg Oster
|
|
||||||
Jörg Oster
|
|
||||||
Ryan Schmitt
|
|
||||||
mcostalba
|
|
||||||
Alexander Kure
|
Alexander Kure
|
||||||
Dan Schmidt
|
Ali AlZhrani (Cooffe)
|
||||||
H. Felix Wittmann
|
Andrew Grant (AndyGrant)
|
||||||
Joseph R. Prostko
|
Andrey Neporada (nepal)
|
||||||
Justin Blanchard
|
|
||||||
Linus Arver
|
|
||||||
NicklasPersson
|
|
||||||
Rodrigo Exterckötter Tjäder
|
|
||||||
Ron Britvich
|
|
||||||
Ronald de Man
|
|
||||||
RyanTaker
|
|
||||||
Vince Negri
|
|
||||||
ceebo
|
|
||||||
jhellis3
|
|
||||||
ppigazzini
|
|
||||||
shane31
|
|
||||||
Andy Duplain
|
Andy Duplain
|
||||||
|
Aram Tumanian (atumanian)
|
||||||
|
Arjun Temurnikar
|
||||||
Auguste Pop
|
Auguste Pop
|
||||||
Balint Pfliegel
|
Balint Pfliegel
|
||||||
Chris Cain
|
Ben Koshy (BKSpurgeon)
|
||||||
DU-jdto
|
Bill Henry (VoyagerOne)
|
||||||
|
braich
|
||||||
|
Brian Sheppard (SapphireBrand)
|
||||||
|
Bryan Cross (crossbr)
|
||||||
|
Bujun Guo (noobpwnftw)
|
||||||
|
Chris Cain (ceebo)
|
||||||
|
Dan Schmidt
|
||||||
|
Daniel Dugovic (ddugovic)
|
||||||
Dariusz Orzechowski
|
Dariusz Orzechowski
|
||||||
|
David Zar
|
||||||
|
Daylen Yang (daylen)
|
||||||
DiscanX
|
DiscanX
|
||||||
|
Eelco de Groot
|
||||||
|
ElbertoOne
|
||||||
|
erbsenzaehler
|
||||||
Ernesto Gatti
|
Ernesto Gatti
|
||||||
|
Fabian Beuke (madnight)
|
||||||
|
Fabian Fichter (ianfab)
|
||||||
|
fanon
|
||||||
|
Fauzi Akram Dabat (FauziAkram)
|
||||||
|
Felix Wittmann
|
||||||
|
gamander
|
||||||
|
gguliash
|
||||||
|
Gian-Carlo Pascutto (gcp)
|
||||||
|
Gontran Lemaire (gonlem)
|
||||||
|
Goodkov Vasiliy Aleksandrovich (goodkov)
|
||||||
Gregor Cramer
|
Gregor Cramer
|
||||||
Guenther Demetz
|
GuardianRM
|
||||||
Hiraoka Takuya
|
Günther Demetz (pb00067, pb00068)
|
||||||
|
Guy Vreuls (gvreuls)
|
||||||
|
Henri Wiechers
|
||||||
|
Hiraoka Takuya (HiraokaTakuya)
|
||||||
|
homoSapiensSapiens
|
||||||
Hongzhi Cheng
|
Hongzhi Cheng
|
||||||
Joseph Hellis
|
Ivan Ivec (IIvec)
|
||||||
|
Jacques B. (Timshel)
|
||||||
|
Jan Ondruš (hxim)
|
||||||
|
Jared Kish (Kurtbusch)
|
||||||
|
Jarrod Torriero (DU-jdto)
|
||||||
|
Jean-Francois Romang (jromang)
|
||||||
|
Jerry Donald Watson (jerrydonaldwatson)
|
||||||
|
Jonathan Calovski (Mysseno)
|
||||||
|
Jonathan D. (SFisGOD)
|
||||||
|
Joost VandeVondele (vondele)
|
||||||
|
Jörg Oster (joergoster)
|
||||||
|
Joseph Ellis (jhellis3)
|
||||||
|
Joseph R. Prostko
|
||||||
|
jundery
|
||||||
|
Justin Blanchard
|
||||||
Kelly Wilson
|
Kelly Wilson
|
||||||
Ken T Takusagawa
|
Ken Takusagawa
|
||||||
|
kinderchocolate
|
||||||
|
Kiran Panditrao (Krgp)
|
||||||
Kojirion
|
Kojirion
|
||||||
Luca Brivio
|
Leonardo Ljubičić (ICCF World Champion)
|
||||||
Matt Sullivan
|
Leonid Pechenik (lp--)
|
||||||
Matthew Lai
|
Linus Arver
|
||||||
|
loco-loco
|
||||||
|
Luca Brivio (lucabrivio)
|
||||||
|
Lucas Braesch (lucasart)
|
||||||
|
Lyudmil Antonov (lantonov)
|
||||||
|
Matthew Lai (matthewlai)
|
||||||
Matthew Sullivan
|
Matthew Sullivan
|
||||||
Michel Van den Bergh
|
Mark Tenzer (31m059)
|
||||||
Mysseno
|
Michael Byrne (MichaelB7)
|
||||||
|
Michael Stembera (mstembera)
|
||||||
|
Michael Chaly (Vizvezdenec)
|
||||||
|
Michel Van den Bergh (vdbergh)
|
||||||
|
Miguel Lahoz (miguel-l)
|
||||||
|
Mikael Bäckman (mbootsector)
|
||||||
|
Mike Whiteley (protonspring)
|
||||||
|
Miroslav Fontán (Hexik)
|
||||||
|
Moez Jellouli (MJZ1977)
|
||||||
|
Mohammed Li (tthsqe12)
|
||||||
|
Nathan Rugg (nmrugg)
|
||||||
|
Nicklas Persson (NicklasPersson)
|
||||||
|
Niklas Fiekas (niklasf)
|
||||||
|
Ondrej Mosnáček (WOnder93)
|
||||||
Oskar Werkelin Ahlin
|
Oskar Werkelin Ahlin
|
||||||
Pablo Vazquez
|
Pablo Vazquez
|
||||||
Pascal Romaret
|
Pascal Romaret
|
||||||
Ralph Stoesser
|
Pasquale Pigazzini (ppigazzini)
|
||||||
Ralph Stößer
|
Patrick Jansen (mibere)
|
||||||
Raminder Singh
|
|
||||||
Richard Lloyd
|
|
||||||
Ryan Takker
|
|
||||||
Stephane Nicolet
|
|
||||||
Thanar2
|
|
||||||
absimaldata
|
|
||||||
braich
|
|
||||||
gguliash
|
|
||||||
kinderchocolate
|
|
||||||
loco-loco
|
|
||||||
pellanda
|
pellanda
|
||||||
|
Peter Zsifkovits (CoffeeOne)
|
||||||
|
Ralph Stößer (Ralph Stoesser)
|
||||||
|
Raminder Singh
|
||||||
renouve
|
renouve
|
||||||
|
Reuven Peleg
|
||||||
|
Richard Lloyd
|
||||||
|
Rodrigo Exterckötter Tjäder
|
||||||
|
Ron Britvich (Britvich)
|
||||||
|
Ronald de Man (syzygy1)
|
||||||
|
Ryan Schmitt
|
||||||
|
Ryan Takker
|
||||||
|
Sebastian Buchwald (UniQP)
|
||||||
|
Sergei Antonov (saproj)
|
||||||
sf-x
|
sf-x
|
||||||
|
shane31
|
||||||
|
Steinar Gunderson (sesse)
|
||||||
|
Stefan Geschwentner (locutus2)
|
||||||
|
Stefano Cardanobile (Stefano80)
|
||||||
|
Stéphane Nicolet (snicolet)
|
||||||
|
Thanar2
|
||||||
thaspel
|
thaspel
|
||||||
unknown
|
Tom Vijlbrief (tomtor)
|
||||||
uriblass
|
Torsten Franz (torfranz)
|
||||||
|
Uri Blass (uriblass)
|
||||||
|
Vince Negri
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ Partner or Fritz) in order to be used comfortably. Read the
|
|||||||
documentation for your GUI of choice for information about how to use
|
documentation for your GUI of choice for information about how to use
|
||||||
Stockfish with it.
|
Stockfish with it.
|
||||||
|
|
||||||
This version of Stockfish supports up to 128 cores. The engine defaults
|
This version of Stockfish supports up to 512 cores. The engine defaults
|
||||||
to one search thread, so it is therefore recommended to inspect the value of
|
to one search thread, so it is therefore recommended to inspect the value of
|
||||||
the *Threads* UCI parameter, and to make sure it equals the number of CPU
|
the *Threads* UCI parameter, and to make sure it equals the number of CPU
|
||||||
cores on your computer.
|
cores on your computer.
|
||||||
@@ -59,7 +59,7 @@ The "SyzygyProbeLimit" option should normally be left at its default value.
|
|||||||
|
|
||||||
**What to expect**
|
**What to expect**
|
||||||
If the engine is searching a position that is not in the tablebases (e.g.
|
If the engine is searching a position that is not in the tablebases (e.g.
|
||||||
a position with 7 pieces), it will access the tablebases during the search.
|
a position with 8 pieces), it will access the tablebases during the search.
|
||||||
If the engine reports a very large score (typically 123.xx), this means
|
If the engine reports a very large score (typically 123.xx), this means
|
||||||
that it has found a winning line into a tablebase position.
|
that it has found a winning line into a tablebase position.
|
||||||
|
|
||||||
@@ -96,6 +96,18 @@ compile (for instance with Microsoft MSVC) you need to manually
|
|||||||
set/unset some switches in the compiler command line; see file *types.h*
|
set/unset some switches in the compiler command line; see file *types.h*
|
||||||
for a quick reference.
|
for a quick reference.
|
||||||
|
|
||||||
|
### Resource For Understanding the Code Base
|
||||||
|
|
||||||
|
* [Chess Programming Wiki](https://www.chessprogramming.org/Main_Page)
|
||||||
|
has good overall chess engines explanations
|
||||||
|
(techniques used here are well explained like hash maps etc), it was
|
||||||
|
also recommended by the [support team at stockfish.](http://support.stockfishchess.org/discussions/questions/1132-how-to-understand-stockfish-sources)
|
||||||
|
|
||||||
|
* [Here](https://www.chessprogramming.org/Stockfish) you can find a set
|
||||||
|
of features and techniques used by Stockfish and each of them is explained
|
||||||
|
at the wiki, however, it's a generic way rather than focusing on Stockfish's
|
||||||
|
own implementation, but it will still help you.
|
||||||
|
|
||||||
|
|
||||||
### Terms of use
|
### Terms of use
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,146 @@
|
|||||||
|
Contributors with >10,000 CPU hours as of November 4, 2018
|
||||||
|
Thank you!
|
||||||
|
|
||||||
|
Username CPU Hours Games played
|
||||||
|
noobpwnftw 3730975 292309380
|
||||||
|
mibere 535242 43333774
|
||||||
|
crunchy 375564 29121434
|
||||||
|
cw 371664 28748719
|
||||||
|
fastgm 318178 22283584
|
||||||
|
JojoM 295354 20958931
|
||||||
|
dew 215476 17079219
|
||||||
|
ctoks 214031 17312035
|
||||||
|
glinscott 204517 13932027
|
||||||
|
bking_US 187568 12233168
|
||||||
|
velislav 168404 13336219
|
||||||
|
CSU_Dynasty 168069 14417712
|
||||||
|
Thanar 162373 13842179
|
||||||
|
spams 149531 10940322
|
||||||
|
Fisherman 141137 12099359
|
||||||
|
drabel 134441 11180178
|
||||||
|
leszek 133658 9812120
|
||||||
|
marrco 133566 10115202
|
||||||
|
sqrt2 128420 10022279
|
||||||
|
vdbergh 123230 9200516
|
||||||
|
tvijlbrief 123007 9498831
|
||||||
|
vdv 120381 8555423
|
||||||
|
malala 117291 8126488
|
||||||
|
dsmith 114010 7622414
|
||||||
|
BrunoBanani 104938 7448565
|
||||||
|
CoffeeOne 100042 4593596
|
||||||
|
Data 94621 8433010
|
||||||
|
mgrabiak 92248 7787406
|
||||||
|
bcross 89440 8506568
|
||||||
|
brabos 81868 6647613
|
||||||
|
BRAVONE 80811 5341681
|
||||||
|
psk 77195 6156031
|
||||||
|
nordlandia 74833 6231930
|
||||||
|
robal 72818 5969856
|
||||||
|
TueRens 72523 6383294
|
||||||
|
sterni1971 71049 5647590
|
||||||
|
sunu 65855 5360884
|
||||||
|
mhoram 65034 5192880
|
||||||
|
davar 64794 5457564
|
||||||
|
nssy 64607 5371952
|
||||||
|
Pking_cda 64499 5704075
|
||||||
|
biffhero 63557 5480444
|
||||||
|
teddybaer 62147 5585620
|
||||||
|
solarlight 61278 5402642
|
||||||
|
ElbertoOne 60156 5504304
|
||||||
|
jromang 58854 4704502
|
||||||
|
dv8silencer 57421 3961325
|
||||||
|
tinker 56039 4204914
|
||||||
|
Freja 50331 3808121
|
||||||
|
renouve 50318 3544864
|
||||||
|
robnjr 47504 4131742
|
||||||
|
grandphish2 47377 4110003
|
||||||
|
eva42 46857 4075716
|
||||||
|
ttruscott 46802 3811534
|
||||||
|
finfish 46244 3481661
|
||||||
|
rap 46201 3219490
|
||||||
|
ronaldjerum 45641 3964331
|
||||||
|
xoto 44998 4170431
|
||||||
|
gvreuls 44359 3902234
|
||||||
|
bigpen0r 41780 3448224
|
||||||
|
Bobo1239 40767 3657490
|
||||||
|
Antihistamine 39218 2792761
|
||||||
|
mhunt 38991 2697512
|
||||||
|
racerschmacer 38929 3756111
|
||||||
|
VoyagerOne 35896 3378887
|
||||||
|
homyur 35561 3012398
|
||||||
|
rkl 33217 2978536
|
||||||
|
pb00067 33034 2803485
|
||||||
|
speedycpu 32043 2531964
|
||||||
|
SC 31954 2848432
|
||||||
|
EthanOConnor 31638 2143255
|
||||||
|
oryx 30962 2899534
|
||||||
|
gri 30108 2429137
|
||||||
|
csnodgrass 29396 2808611
|
||||||
|
Garf 28887 2873564
|
||||||
|
Pyafue 28885 1986098
|
||||||
|
jkiiski 28014 1923255
|
||||||
|
slakovv 27017 2031279
|
||||||
|
Prcuvu 26300 2307154
|
||||||
|
hyperbolic.tom 26248 2200777
|
||||||
|
jbwiebe 25663 2129063
|
||||||
|
anst 25525 2279159
|
||||||
|
Patrick_G 24222 1835674
|
||||||
|
nabildanial 23524 1586321
|
||||||
|
achambord 23495 1942546
|
||||||
|
Sharaf_DG 22975 1790697
|
||||||
|
chriswk 22876 1947731
|
||||||
|
ncfish1 22689 1830009
|
||||||
|
cuistot 22201 1383031
|
||||||
|
Zirie 21171 1493227
|
||||||
|
Isidor 20634 1736219
|
||||||
|
JanErik 20596 1791991
|
||||||
|
xor12 20535 1819280
|
||||||
|
team-oh 20364 1653708
|
||||||
|
nesoneg 20264 1493435
|
||||||
|
dex 20110 1682756
|
||||||
|
rstoesser 19802 1335177
|
||||||
|
Vizvezdenec 19750 1695579
|
||||||
|
eastorwest 19531 1841839
|
||||||
|
sg4032 18913 1720157
|
||||||
|
horst.prack 18425 1708197
|
||||||
|
cisco2015 18408 1793774
|
||||||
|
ianh2105 18133 1668562
|
||||||
|
MazeOfGalious 18022 1644593
|
||||||
|
ville 17900 1539130
|
||||||
|
j3corre 17607 975954
|
||||||
|
eudhan 17502 1424648
|
||||||
|
jmdana 17351 1287546
|
||||||
|
iisiraider 17175 1118788
|
||||||
|
jundery 17172 1115855
|
||||||
|
wei 16852 1822582
|
||||||
|
SFTUser 16635 1363975
|
||||||
|
purplefishies 16621 1106850
|
||||||
|
DragonLord 16599 1252348
|
||||||
|
chris 15274 1575333
|
||||||
|
IgorLeMasson 15201 1364148
|
||||||
|
dju 15074 914278
|
||||||
|
Flopzee 14700 1331632
|
||||||
|
OssumOpossum 14149 1029265
|
||||||
|
enedene 13762 935618
|
||||||
|
ako027ako 13442 1250249
|
||||||
|
AdrianSA 13324 924980
|
||||||
|
bpfliegel 13318 886523
|
||||||
|
Nikolay.IT 13260 1155612
|
||||||
|
jpulman 12776 854815
|
||||||
|
joster 12438 988413
|
||||||
|
fatmurphy 12015 901134
|
||||||
|
Nesa92 11711 1132245
|
||||||
|
Adrian.Schmidt123 11542 898699
|
||||||
|
modolief 11228 926456
|
||||||
|
Dark_wizzie 11214 1017910
|
||||||
|
mschmidt 10973 818594
|
||||||
|
Andrew Grant 10780 947859
|
||||||
|
infinity 10762 746397
|
||||||
|
SapphireBrand 10692 1024604
|
||||||
|
Thomas A. Anderson 10553 736094
|
||||||
|
basepi 10434 935168
|
||||||
|
lantonov 10325 972610
|
||||||
|
pgontarz 10294 878746
|
||||||
|
Spprtr 10189 823246
|
||||||
|
crocogoat 10115 1017325
|
||||||
|
stocky 10083 718114
|
||||||
+40
-14
@@ -1,5 +1,5 @@
|
|||||||
version: 1.0.{build}
|
version: 1.0.{build}
|
||||||
clone_depth: 5
|
clone_depth: 50
|
||||||
|
|
||||||
branches:
|
branches:
|
||||||
only:
|
only:
|
||||||
@@ -13,10 +13,11 @@ os: Visual Studio 2015
|
|||||||
platform:
|
platform:
|
||||||
- x86
|
- x86
|
||||||
- x64
|
- x64
|
||||||
- Any CPU
|
|
||||||
|
|
||||||
# build Configuration, i.e. Debug, Release, etc.
|
# build Configuration, i.e. Debug, Release, etc.
|
||||||
configuration: Debug
|
configuration:
|
||||||
|
- Debug
|
||||||
|
- Release
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
# The build fail immediately once one of the job fails
|
# The build fail immediately once one of the job fails
|
||||||
@@ -28,18 +29,43 @@ init:
|
|||||||
- msbuild /version
|
- msbuild /version
|
||||||
|
|
||||||
before_build:
|
before_build:
|
||||||
- cd src
|
- ps: |
|
||||||
- echo project (Stockfish) >> CMakeLists.txt
|
# Get sources
|
||||||
- echo add_executable(stockfish benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp >> CMakeLists.txt
|
$src = get-childitem -Path *.cpp -Recurse | select -ExpandProperty FullName
|
||||||
- echo main.cpp material.cpp misc.cpp movegen.cpp movepick.cpp pawns.cpp position.cpp psqt.cpp >> CMakeLists.txt
|
$src = $src -join ' '
|
||||||
- echo search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp syzygy/tbprobe.cpp) >> CMakeLists.txt
|
$src = $src.Replace("\", "/")
|
||||||
- echo set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/src) >> CMakeLists.txt
|
|
||||||
# - echo target_compile_options(stockfish PUBLIC "/Ox") >> CMakeLists.txt
|
# Build CMakeLists.txt
|
||||||
|
$t = 'cmake_minimum_required(VERSION 3.8)',
|
||||||
|
'project(Stockfish)',
|
||||||
|
'set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/src)',
|
||||||
|
'set(source_files', $src, ')',
|
||||||
|
'add_executable(stockfish ${source_files})'
|
||||||
|
|
||||||
|
# Write CMakeLists.txt withouth BOM
|
||||||
|
$MyPath = (Get-Item -Path "." -Verbose).FullName + '\CMakeLists.txt'
|
||||||
|
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
|
||||||
|
[System.IO.File]::WriteAllLines($MyPath, $t, $Utf8NoBomEncoding)
|
||||||
|
|
||||||
|
# Obtain bench reference from git log
|
||||||
|
$b = git log HEAD | sls "\b[Bb]ench[ :]+[0-9]{7}" | select -first 1
|
||||||
|
$bench = $b -match '\D+(\d+)' | % { $matches[1] }
|
||||||
|
Write-Host "Reference bench:" $bench
|
||||||
|
$g = "Visual Studio 14 2015"
|
||||||
|
If (${env:PLATFORM} -eq 'x64') { $g = $g + ' Win64' }
|
||||||
|
cmake -G "${g}" .
|
||||||
|
Write-Host "Generated files for: " $g
|
||||||
|
|
||||||
build_script:
|
build_script:
|
||||||
- cmake -G "Visual Studio 14 2015 Win64" .
|
- cmake --build . --config %CONFIGURATION% -- /verbosity:minimal
|
||||||
- cmake --build .
|
|
||||||
|
|
||||||
before_test:
|
before_test:
|
||||||
- cd Debug
|
- cd src/%CONFIGURATION%
|
||||||
- stockfish.exe bench > null
|
- 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 }
|
||||||
|
|||||||
+80
-87
@@ -1,7 +1,7 @@
|
|||||||
# 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-2008 Tord Romstad (Glaurung author)
|
# Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
# Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
# Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
# Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
# Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
#
|
#
|
||||||
# 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,14 +21,12 @@
|
|||||||
### 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
|
|
||||||
|
|
||||||
### Executable name
|
### Executable name
|
||||||
|
ifeq ($(COMP),mingw)
|
||||||
|
EXE = stockfish.exe
|
||||||
|
else
|
||||||
EXE = stockfish
|
EXE = stockfish
|
||||||
|
endif
|
||||||
|
|
||||||
### Installation dir definitions
|
### Installation dir definitions
|
||||||
PREFIX = /usr/local
|
PREFIX = /usr/local
|
||||||
@@ -42,6 +40,12 @@ OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
|
|||||||
material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
|
material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
|
||||||
search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
|
search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
|
||||||
|
|
||||||
|
### 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
|
||||||
### ==========================================================================
|
### ==========================================================================
|
||||||
@@ -50,7 +54,9 @@ OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
|
|||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
|
# debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
|
||||||
# sanitize = yes/no --- (-fsanitize ) --- enable undefined behavior checks
|
# sanitize = undefined/thread/no (-fsanitize )
|
||||||
|
# --- ( undefined ) --- enable undefined behavior checks
|
||||||
|
# --- ( thread ) --- enable threading error checks
|
||||||
# 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
|
||||||
@@ -139,7 +145,7 @@ endif
|
|||||||
|
|
||||||
### 3.1 Selecting compiler (default = gcc)
|
### 3.1 Selecting compiler (default = gcc)
|
||||||
|
|
||||||
CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti -std=c++11 $(EXTRACXXFLAGS)
|
CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -std=c++11 $(EXTRACXXFLAGS)
|
||||||
DEPENDFLAGS += -std=c++11
|
DEPENDFLAGS += -std=c++11
|
||||||
LDFLAGS += $(EXTRALDFLAGS)
|
LDFLAGS += $(EXTRALDFLAGS)
|
||||||
|
|
||||||
@@ -155,9 +161,11 @@ ifeq ($(COMP),gcc)
|
|||||||
ifeq ($(ARCH),armv7)
|
ifeq ($(ARCH),armv7)
|
||||||
ifeq ($(OS),Android)
|
ifeq ($(OS),Android)
|
||||||
CXXFLAGS += -m$(bits)
|
CXXFLAGS += -m$(bits)
|
||||||
|
LDFLAGS += -m$(bits)
|
||||||
endif
|
endif
|
||||||
else
|
else
|
||||||
CXXFLAGS += -m$(bits)
|
CXXFLAGS += -m$(bits)
|
||||||
|
LDFLAGS += -m$(bits)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifneq ($(KERNEL),Darwin)
|
ifneq ($(KERNEL),Darwin)
|
||||||
@@ -201,6 +209,12 @@ ifeq ($(COMP),clang)
|
|||||||
CXX=clang++
|
CXX=clang++
|
||||||
CXXFLAGS += -pedantic -Wextra -Wshadow
|
CXXFLAGS += -pedantic -Wextra -Wshadow
|
||||||
|
|
||||||
|
ifneq ($(KERNEL),Darwin)
|
||||||
|
ifneq ($(KERNEL),OpenBSD)
|
||||||
|
LDFLAGS += -latomic
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
ifeq ($(ARCH),armv7)
|
ifeq ($(ARCH),armv7)
|
||||||
ifeq ($(OS),Android)
|
ifeq ($(OS),Android)
|
||||||
CXXFLAGS += -m$(bits)
|
CXXFLAGS += -m$(bits)
|
||||||
@@ -210,23 +224,19 @@ ifeq ($(COMP),clang)
|
|||||||
CXXFLAGS += -m$(bits)
|
CXXFLAGS += -m$(bits)
|
||||||
LDFLAGS += -m$(bits)
|
LDFLAGS += -m$(bits)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(KERNEL),Darwin)
|
|
||||||
CXXFLAGS += -stdlib=libc++
|
|
||||||
DEPENDFLAGS += -stdlib=libc++
|
|
||||||
endif
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(comp),icc)
|
ifeq ($(comp),icc)
|
||||||
profile_prepare = icc-profile-prepare
|
|
||||||
profile_make = icc-profile-make
|
profile_make = icc-profile-make
|
||||||
profile_use = icc-profile-use
|
profile_use = icc-profile-use
|
||||||
profile_clean = icc-profile-clean
|
|
||||||
else
|
else
|
||||||
profile_prepare = gcc-profile-prepare
|
ifeq ($(comp),clang)
|
||||||
|
profile_make = clang-profile-make
|
||||||
|
profile_use = clang-profile-use
|
||||||
|
else
|
||||||
profile_make = gcc-profile-make
|
profile_make = gcc-profile-make
|
||||||
profile_use = gcc-profile-use
|
profile_use = gcc-profile-use
|
||||||
profile_clean = gcc-profile-clean
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(KERNEL),Darwin)
|
ifeq ($(KERNEL),Darwin)
|
||||||
@@ -263,8 +273,9 @@ else
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.2.2 Debugging with undefined behavior sanitizers
|
### 3.2.2 Debugging with undefined behavior sanitizers
|
||||||
ifeq ($(sanitize),yes)
|
ifneq ($(sanitize),no)
|
||||||
CXXFLAGS += -g3 -fsanitize=undefined
|
CXXFLAGS += -g3 -fsanitize=$(sanitize) -fuse-ld=gold
|
||||||
|
LDFLAGS += -fsanitize=$(sanitize) -fuse-ld=gold
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.3 Optimization
|
### 3.3 Optimization
|
||||||
@@ -273,41 +284,16 @@ ifeq ($(optimize),yes)
|
|||||||
CXXFLAGS += -O3
|
CXXFLAGS += -O3
|
||||||
|
|
||||||
ifeq ($(comp),gcc)
|
ifeq ($(comp),gcc)
|
||||||
|
|
||||||
ifeq ($(KERNEL),Darwin)
|
|
||||||
ifeq ($(arch),i386)
|
|
||||||
CXXFLAGS += -mdynamic-no-pic
|
|
||||||
endif
|
|
||||||
ifeq ($(arch),x86_64)
|
|
||||||
CXXFLAGS += -mdynamic-no-pic
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
|
|
||||||
ifeq ($(OS), Android)
|
ifeq ($(OS), Android)
|
||||||
CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
|
CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(comp),icc)
|
ifeq ($(comp),$(filter $(comp),gcc clang icc))
|
||||||
ifeq ($(KERNEL),Darwin)
|
ifeq ($(KERNEL),Darwin)
|
||||||
CXXFLAGS += -mdynamic-no-pic
|
CXXFLAGS += -mdynamic-no-pic
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(comp),clang)
|
|
||||||
ifeq ($(KERNEL),Darwin)
|
|
||||||
ifeq ($(pext),no)
|
|
||||||
CXXFLAGS += -flto
|
|
||||||
LDFLAGS += $(CXXFLAGS)
|
|
||||||
endif
|
|
||||||
ifeq ($(arch),i386)
|
|
||||||
CXXFLAGS += -mdynamic-no-pic
|
|
||||||
endif
|
|
||||||
ifeq ($(arch),x86_64)
|
|
||||||
CXXFLAGS += -mdynamic-no-pic
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.4 Bits
|
### 3.4 Bits
|
||||||
@@ -345,24 +331,20 @@ endif
|
|||||||
### 3.8 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
|
### 3.8 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
|
||||||
### This is a mix of compile and link time options because the lto link phase
|
### 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 ($(comp),gcc)
|
ifeq ($(optimize),yes)
|
||||||
ifeq ($(optimize),yes)
|
ifeq ($(debug), no)
|
||||||
ifeq ($(debug),no)
|
ifeq ($(comp),$(filter $(comp),gcc clang))
|
||||||
|
CXXFLAGS += -flto
|
||||||
|
LDFLAGS += $(CXXFLAGS)
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(comp),mingw)
|
||||||
|
ifeq ($(KERNEL),Linux)
|
||||||
CXXFLAGS += -flto
|
CXXFLAGS += -flto
|
||||||
LDFLAGS += $(CXXFLAGS)
|
LDFLAGS += $(CXXFLAGS)
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(comp),mingw)
|
|
||||||
ifeq ($(KERNEL),Linux)
|
|
||||||
ifeq ($(optimize),yes)
|
|
||||||
ifeq ($(debug),no)
|
|
||||||
CXXFLAGS += -flto
|
|
||||||
LDFLAGS += $(CXXFLAGS)
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.9 Android 5 can only run position independent executables. Note that this
|
### 3.9 Android 5 can only run position independent executables. Note that this
|
||||||
@@ -423,30 +405,27 @@ help:
|
|||||||
@echo ""
|
@echo ""
|
||||||
|
|
||||||
|
|
||||||
.PHONY: build profile-build
|
.PHONY: help build profile-build strip install clean objclean profileclean help \
|
||||||
build:
|
config-sanity icc-profile-use icc-profile-make gcc-profile-use gcc-profile-make \
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
|
clang-profile-use clang-profile-make
|
||||||
|
|
||||||
|
build: config-sanity
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
|
||||||
|
|
||||||
profile-build:
|
profile-build: config-sanity objclean profileclean
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
|
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Step 0/4. Preparing for profile build."
|
@echo "Step 1/4. Building instrumented executable ..."
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
|
|
||||||
@echo ""
|
|
||||||
@echo "Step 1/4. Building executable for benchmark ..."
|
|
||||||
@touch *.cpp *.h syzygy/*.cpp syzygy/*.h
|
|
||||||
$(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) > /dev/null
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Step 3/4. Building final executable ..."
|
@echo "Step 3/4. Building optimized executable ..."
|
||||||
@touch *.cpp *.h syzygy/*.cpp syzygy/*.h
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Step 4/4. Deleting profile data ..."
|
@echo "Step 4/4. Deleting profile data ..."
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
|
||||||
|
|
||||||
strip:
|
strip:
|
||||||
strip $(EXE)
|
strip $(EXE)
|
||||||
@@ -456,8 +435,19 @@ install:
|
|||||||
-cp $(EXE) $(BINDIR)
|
-cp $(EXE) $(BINDIR)
|
||||||
-strip $(BINDIR)/$(EXE)
|
-strip $(BINDIR)/$(EXE)
|
||||||
|
|
||||||
clean:
|
#clean all
|
||||||
$(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
|
clean: objclean profileclean
|
||||||
|
@rm -f .depend *~ core
|
||||||
|
|
||||||
|
# clean binaries and objects
|
||||||
|
objclean:
|
||||||
|
@rm -f $(EXE) *.o ./syzygy/*.o
|
||||||
|
|
||||||
|
# clean auxiliary profiling files
|
||||||
|
profileclean:
|
||||||
|
@rm -rf profdir
|
||||||
|
@rm -f bench.txt *.gcda ./syzygy/*.gcda *.gcno ./syzygy/*.gcno
|
||||||
|
@rm -f stockfish.profdata *.profraw
|
||||||
|
|
||||||
default:
|
default:
|
||||||
help
|
help
|
||||||
@@ -472,6 +462,7 @@ config-sanity:
|
|||||||
@echo ""
|
@echo ""
|
||||||
@echo "Config:"
|
@echo "Config:"
|
||||||
@echo "debug: '$(debug)'"
|
@echo "debug: '$(debug)'"
|
||||||
|
@echo "sanitize: '$(sanitize)'"
|
||||||
@echo "optimize: '$(optimize)'"
|
@echo "optimize: '$(optimize)'"
|
||||||
@echo "arch: '$(arch)'"
|
@echo "arch: '$(arch)'"
|
||||||
@echo "bits: '$(bits)'"
|
@echo "bits: '$(bits)'"
|
||||||
@@ -490,6 +481,7 @@ config-sanity:
|
|||||||
@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)" = "no"
|
||||||
@test "$(optimize)" = "yes" || test "$(optimize)" = "no"
|
@test "$(optimize)" = "yes" || test "$(optimize)" = "no"
|
||||||
@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)" = "armv7"
|
test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
|
||||||
@@ -503,8 +495,18 @@ config-sanity:
|
|||||||
$(EXE): $(OBJS)
|
$(EXE): $(OBJS)
|
||||||
$(CXX) -o $@ $(OBJS) $(LDFLAGS)
|
$(CXX) -o $@ $(OBJS) $(LDFLAGS)
|
||||||
|
|
||||||
gcc-profile-prepare:
|
clang-profile-make:
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) gcc-profile-clean
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||||
|
EXTRACXXFLAGS='-fprofile-instr-generate ' \
|
||||||
|
EXTRALDFLAGS=' -fprofile-instr-generate' \
|
||||||
|
all
|
||||||
|
|
||||||
|
clang-profile-use:
|
||||||
|
llvm-profdata merge -output=stockfish.profdata *.profraw
|
||||||
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||||
|
EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
|
||||||
|
EXTRALDFLAGS='-fprofile-use ' \
|
||||||
|
all
|
||||||
|
|
||||||
gcc-profile-make:
|
gcc-profile-make:
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||||
@@ -518,14 +520,8 @@ gcc-profile-use:
|
|||||||
EXTRALDFLAGS='-lgcov' \
|
EXTRALDFLAGS='-lgcov' \
|
||||||
all
|
all
|
||||||
|
|
||||||
gcc-profile-clean:
|
|
||||||
@rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
|
|
||||||
|
|
||||||
icc-profile-prepare:
|
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
|
|
||||||
@mkdir profdir
|
|
||||||
|
|
||||||
icc-profile-make:
|
icc-profile-make:
|
||||||
|
@mkdir -p profdir
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||||
EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
|
EXTRACXXFLAGS='-prof-gen=srcpos -prof_dir ./profdir' \
|
||||||
all
|
all
|
||||||
@@ -535,9 +531,6 @@ icc-profile-use:
|
|||||||
EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
|
EXTRACXXFLAGS='-prof_use -prof_dir ./profdir' \
|
||||||
all
|
all
|
||||||
|
|
||||||
icc-profile-clean:
|
|
||||||
@rm -rf profdir bench.txt
|
|
||||||
|
|
||||||
.depend:
|
.depend:
|
||||||
-@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null
|
-@$(CXX) $(DEPENDFLAGS) -MM $(OBJS:.o=.cpp) > $@ 2> /dev/null
|
||||||
|
|
||||||
|
|||||||
+38
-65
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,23 +23,20 @@
|
|||||||
#include <istream>
|
#include <istream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "misc.h"
|
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "search.h"
|
|
||||||
#include "thread.h"
|
|
||||||
#include "uci.h"
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
const vector<string> Defaults = {
|
const vector<string> Defaults = {
|
||||||
|
"setoption name UCI_Chess960 value false",
|
||||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
||||||
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 10",
|
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 10",
|
||||||
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 11",
|
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 11",
|
||||||
"4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19",
|
"4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19",
|
||||||
"rq3rk1/ppp2ppp/1bnpb3/3N2B1/3NP3/7P/PPPQ1PP1/2KR3R w - - 7 14",
|
"rq3rk1/ppp2ppp/1bnpb3/3N2B1/3NP3/7P/PPPQ1PP1/2KR3R w - - 7 14 moves d4e6",
|
||||||
"r1bq1r1k/1pp1n1pp/1p1p4/4p2Q/4Pp2/1BNP4/PPP2PPP/3R1RK1 w - - 2 14",
|
"r1bq1r1k/1pp1n1pp/1p1p4/4p2Q/4Pp2/1BNP4/PPP2PPP/3R1RK1 w - - 2 14 moves g2g4",
|
||||||
"r3r1k1/2p2ppp/p1p1bn2/8/1q2P3/2NPQN2/PPP3PP/R4RK1 b - - 2 15",
|
"r3r1k1/2p2ppp/p1p1bn2/8/1q2P3/2NPQN2/PPP3PP/R4RK1 b - - 2 15",
|
||||||
"r1bbk1nr/pp3p1p/2n5/1N4p1/2Np1B2/8/PPP2PPP/2KR1B1R w kq - 0 13",
|
"r1bbk1nr/pp3p1p/2n5/1N4p1/2Np1B2/8/PPP2PPP/2KR1B1R w kq - 0 13",
|
||||||
"r1bq1rk1/ppp1nppp/4n3/3p3Q/3P4/1BP1B3/PP1N2PP/R4RK1 w - - 1 16",
|
"r1bq1rk1/ppp1nppp/4n3/3p3Q/3P4/1BP1B3/PP1N2PP/R4RK1 w - - 1 16",
|
||||||
@@ -52,7 +49,7 @@ const vector<string> Defaults = {
|
|||||||
"3q2k1/pb3p1p/4pbp1/2r5/PpN2N2/1P2P2P/5PP1/Q2R2K1 b - - 4 26",
|
"3q2k1/pb3p1p/4pbp1/2r5/PpN2N2/1P2P2P/5PP1/Q2R2K1 b - - 4 26",
|
||||||
"6k1/6p1/6Pp/ppp5/3pn2P/1P3K2/1PP2P2/3N4 b - - 0 1",
|
"6k1/6p1/6Pp/ppp5/3pn2P/1P3K2/1PP2P2/3N4 b - - 0 1",
|
||||||
"3b4/5kp1/1p1p1p1p/pP1PpP1P/P1P1P3/3KN3/8/8 w - - 0 1",
|
"3b4/5kp1/1p1p1p1p/pP1PpP1P/P1P1P3/3KN3/8/8 w - - 0 1",
|
||||||
"2K5/p7/7P/5pR1/8/5k2/r7/8 w - - 0 1",
|
"2K5/p7/7P/5pR1/8/5k2/r7/8 w - - 0 1 moves g5g6 f3e3 g6g5 e3f3",
|
||||||
"8/6pk/1p6/8/PP3p1p/5P2/4KP1q/3Q4 w - - 0 1",
|
"8/6pk/1p6/8/PP3p1p/5P2/4KP1q/3Q4 w - - 0 1",
|
||||||
"7k/3p2pp/4q3/8/4Q3/5Kp1/P6b/8 w - - 0 1",
|
"7k/3p2pp/4q3/8/4Q3/5Kp1/P6b/8 w - - 0 1",
|
||||||
"8/2p5/8/2kPKp1p/2p4P/2P5/3P4/8 w - - 0 1",
|
"8/2p5/8/2kPKp1p/2p4P/2P5/3P4/8 w - - 0 1",
|
||||||
@@ -79,28 +76,35 @@ const vector<string> Defaults = {
|
|||||||
"8/R7/2q5/8/6k1/8/1P5p/K6R w - - 0 124", // Draw
|
"8/R7/2q5/8/6k1/8/1P5p/K6R w - - 0 124", // Draw
|
||||||
|
|
||||||
// Mate and stalemate positions
|
// Mate and stalemate positions
|
||||||
|
"6k1/3b3r/1p1p4/p1n2p2/1PPNpP1q/P3Q1p1/1R1RB1P1/5K2 b - - 0 1",
|
||||||
|
"r2r1n2/pp2bk2/2p1p2p/3q4/3PN1QP/2P3R1/P4PP1/5RK1 w - - 0 1",
|
||||||
"8/8/8/8/8/6k1/6p1/6K1 w - -",
|
"8/8/8/8/8/6k1/6p1/6K1 w - -",
|
||||||
"5k2/5P2/5K2/8/8/8/8/8 b - -",
|
"7k/7P/6K1/8/3B4/8/8/8 b - -",
|
||||||
"8/8/8/8/8/4k3/4p3/4K3 w - -",
|
|
||||||
"8/8/8/8/8/5K2/8/3Q1k2 b - -",
|
// Chess 960
|
||||||
"7k/7P/6K1/8/3B4/8/8/8 b - -"
|
"setoption name UCI_Chess960 value true",
|
||||||
|
"bbqnnrkr/pppppppp/8/8/8/8/PPPPPPPP/BBQNNRKR w KQkq - 0 1 moves g2g3 d7d5 d2d4 c8h3 c1g5 e8d6 g5e7 f7f6",
|
||||||
|
"setoption name UCI_Chess960 value false"
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
/// benchmark() runs a simple benchmark by letting Stockfish analyze a set
|
/// setup_bench() builds a list of UCI commands to be run by bench. There
|
||||||
/// of positions for a given limit each. There are five parameters: the
|
/// are five parameters: TT size in MB, number of search threads that
|
||||||
/// transposition table size, the number of search threads that should
|
/// should be used, the limit value spent for each position, a file name
|
||||||
/// be used, the limit value spent for each position (optional, default is
|
/// where to look for positions in FEN format and the type of the limit:
|
||||||
/// depth 13), an optional file name where to look for positions in FEN
|
/// depth, perft, nodes and movetime (in millisecs).
|
||||||
/// format (defaults are the positions defined above) and the type of the
|
///
|
||||||
/// limit value: depth (default), time in millisecs or number of nodes.
|
/// bench -> search default positions up to depth 13
|
||||||
|
/// bench 64 1 15 -> search default positions up to depth 15 (TT = 64MB)
|
||||||
|
/// bench 64 4 5000 current movetime -> search current position with 4 threads for 5 sec
|
||||||
|
/// bench 64 1 100000 default nodes -> search default positions for 100K nodes each
|
||||||
|
/// bench 16 1 5 default perft -> run a perft 5 on default positions
|
||||||
|
|
||||||
void benchmark(const Position& current, istream& is) {
|
vector<string> setup_bench(const Position& current, istream& is) {
|
||||||
|
|
||||||
string token;
|
vector<string> fens, list;
|
||||||
vector<string> fens;
|
string go, token;
|
||||||
Search::LimitsType limits;
|
|
||||||
|
|
||||||
// Assign default values to missing arguments
|
// Assign default values to missing arguments
|
||||||
string ttSize = (is >> token) ? token : "16";
|
string ttSize = (is >> token) ? token : "16";
|
||||||
@@ -109,21 +113,7 @@ void benchmark(const Position& current, istream& is) {
|
|||||||
string fenFile = (is >> token) ? token : "default";
|
string fenFile = (is >> token) ? token : "default";
|
||||||
string limitType = (is >> token) ? token : "depth";
|
string limitType = (is >> token) ? token : "depth";
|
||||||
|
|
||||||
Options["Hash"] = ttSize;
|
go = "go " + limitType + " " + limit;
|
||||||
Options["Threads"] = threads;
|
|
||||||
Search::clear();
|
|
||||||
|
|
||||||
if (limitType == "time")
|
|
||||||
limits.movetime = stoi(limit); // movetime is in millisecs
|
|
||||||
|
|
||||||
else if (limitType == "nodes")
|
|
||||||
limits.nodes = stoi(limit);
|
|
||||||
|
|
||||||
else if (limitType == "mate")
|
|
||||||
limits.mate = stoi(limit);
|
|
||||||
|
|
||||||
else
|
|
||||||
limits.depth = stoi(limit);
|
|
||||||
|
|
||||||
if (fenFile == "default")
|
if (fenFile == "default")
|
||||||
fens = Defaults;
|
fens = Defaults;
|
||||||
@@ -139,7 +129,7 @@ void benchmark(const Position& current, istream& is) {
|
|||||||
if (!file.is_open())
|
if (!file.is_open())
|
||||||
{
|
{
|
||||||
cerr << "Unable to open file " << fenFile << endl;
|
cerr << "Unable to open file " << fenFile << endl;
|
||||||
return;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (getline(file, fen))
|
while (getline(file, fen))
|
||||||
@@ -149,35 +139,18 @@ void benchmark(const Position& current, istream& is) {
|
|||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t nodes = 0;
|
list.emplace_back("ucinewgame");
|
||||||
TimePoint elapsed = now();
|
list.emplace_back("setoption name Threads value " + threads);
|
||||||
Position pos;
|
list.emplace_back("setoption name Hash value " + ttSize);
|
||||||
|
|
||||||
for (size_t i = 0; i < fens.size(); ++i)
|
|
||||||
{
|
|
||||||
StateListPtr states(new std::deque<StateInfo>(1));
|
|
||||||
pos.set(fens[i], Options["UCI_Chess960"], &states->back(), Threads.main());
|
|
||||||
|
|
||||||
cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
|
|
||||||
|
|
||||||
if (limitType == "perft")
|
|
||||||
nodes += Search::perft(pos, limits.depth * ONE_PLY);
|
|
||||||
|
|
||||||
|
for (const string& fen : fens)
|
||||||
|
if (fen.find("setoption") != string::npos)
|
||||||
|
list.emplace_back(fen);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
limits.startTime = now();
|
list.emplace_back("position fen " + fen);
|
||||||
Threads.start_thinking(pos, states, limits);
|
list.emplace_back(go);
|
||||||
Threads.main()->wait_for_search_finished();
|
|
||||||
nodes += Threads.nodes_searched();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
elapsed = now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero'
|
return list;
|
||||||
|
|
||||||
dbg_print(); // Just before exiting
|
|
||||||
|
|
||||||
cerr << "\n==========================="
|
|
||||||
<< "\nTotal time (ms) : " << elapsed
|
|
||||||
<< "\nNodes searched : " << nodes
|
|
||||||
<< "\nNodes/second : " << 1000 * nodes / elapsed << endl;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-11
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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
|
||||||
@@ -29,7 +29,7 @@
|
|||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// There are 24 possible pawn squares: the first 4 files and ranks from 2 to 7
|
// There are 24 possible pawn squares: the first 4 files and ranks from 2 to 7
|
||||||
const unsigned MAX_INDEX = 2*24*64*64; // stm * psq * wksq * bksq = 196608
|
constexpr unsigned MAX_INDEX = 2*24*64*64; // stm * psq * wksq * bksq = 196608
|
||||||
|
|
||||||
// Each uint32_t stores results of 32 positions, one per bit
|
// Each uint32_t stores results of 32 positions, one per bit
|
||||||
uint32_t KPKBitbase[MAX_INDEX / 32];
|
uint32_t KPKBitbase[MAX_INDEX / 32];
|
||||||
@@ -111,13 +111,13 @@ namespace {
|
|||||||
ksq[WHITE] = Square((idx >> 0) & 0x3F);
|
ksq[WHITE] = Square((idx >> 0) & 0x3F);
|
||||||
ksq[BLACK] = Square((idx >> 6) & 0x3F);
|
ksq[BLACK] = Square((idx >> 6) & 0x3F);
|
||||||
us = Color ((idx >> 12) & 0x01);
|
us = Color ((idx >> 12) & 0x01);
|
||||||
psq = make_square(File((idx >> 13) & 0x3), RANK_7 - Rank((idx >> 15) & 0x7));
|
psq = make_square(File((idx >> 13) & 0x3), Rank(RANK_7 - ((idx >> 15) & 0x7)));
|
||||||
|
|
||||||
// Check if two pieces are on the same square or if a king can be captured
|
// Check if two pieces are on the same square or if a king can be captured
|
||||||
if ( distance(ksq[WHITE], ksq[BLACK]) <= 1
|
if ( distance(ksq[WHITE], ksq[BLACK]) <= 1
|
||||||
|| ksq[WHITE] == psq
|
|| ksq[WHITE] == psq
|
||||||
|| ksq[BLACK] == psq
|
|| ksq[BLACK] == psq
|
||||||
|| (us == WHITE && (StepAttacksBB[PAWN][psq] & ksq[BLACK])))
|
|| (us == WHITE && (PawnAttacks[WHITE][psq] & ksq[BLACK])))
|
||||||
result = INVALID;
|
result = INVALID;
|
||||||
|
|
||||||
// Immediate win if a pawn can be promoted without getting captured
|
// Immediate win if a pawn can be promoted without getting captured
|
||||||
@@ -125,13 +125,13 @@ namespace {
|
|||||||
&& rank_of(psq) == RANK_7
|
&& rank_of(psq) == RANK_7
|
||||||
&& ksq[us] != psq + NORTH
|
&& ksq[us] != psq + NORTH
|
||||||
&& ( distance(ksq[~us], psq + NORTH) > 1
|
&& ( distance(ksq[~us], psq + NORTH) > 1
|
||||||
|| (StepAttacksBB[KING][ksq[us]] & (psq + NORTH))))
|
|| (PseudoAttacks[KING][ksq[us]] & (psq + NORTH))))
|
||||||
result = WIN;
|
result = WIN;
|
||||||
|
|
||||||
// Immediate draw if it is a stalemate or a king captures undefended pawn
|
// Immediate draw if it is a stalemate or a king captures undefended pawn
|
||||||
else if ( us == BLACK
|
else if ( us == BLACK
|
||||||
&& ( !(StepAttacksBB[KING][ksq[us]] & ~(StepAttacksBB[KING][ksq[~us]] | StepAttacksBB[PAWN][psq]))
|
&& ( !(PseudoAttacks[KING][ksq[us]] & ~(PseudoAttacks[KING][ksq[~us]] | PawnAttacks[~us][psq]))
|
||||||
|| (StepAttacksBB[KING][ksq[us]] & psq & ~StepAttacksBB[KING][ksq[~us]])))
|
|| (PseudoAttacks[KING][ksq[us]] & psq & ~PseudoAttacks[KING][ksq[~us]])))
|
||||||
result = DRAW;
|
result = DRAW;
|
||||||
|
|
||||||
// Position will be classified later
|
// Position will be classified later
|
||||||
@@ -152,12 +152,12 @@ namespace {
|
|||||||
// as WIN, the position is classified as WIN, otherwise the current position is
|
// as WIN, the position is classified as WIN, otherwise the current position is
|
||||||
// classified as UNKNOWN.
|
// classified as UNKNOWN.
|
||||||
|
|
||||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||||
const Result Good = (Us == WHITE ? WIN : DRAW);
|
constexpr Result Good = (Us == WHITE ? WIN : DRAW);
|
||||||
const Result Bad = (Us == WHITE ? DRAW : WIN);
|
constexpr Result Bad = (Us == WHITE ? DRAW : WIN);
|
||||||
|
|
||||||
Result r = INVALID;
|
Result r = INVALID;
|
||||||
Bitboard b = StepAttacksBB[KING][ksq[Us]];
|
Bitboard b = PseudoAttacks[KING][ksq[Us]];
|
||||||
|
|
||||||
while (b)
|
while (b)
|
||||||
r |= Us == WHITE ? db[index(Them, ksq[Them] , pop_lsb(&b), psq)]
|
r |= Us == WHITE ? db[index(Them, ksq[Them] , pop_lsb(&b), psq)]
|
||||||
|
|||||||
+62
-131
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,55 +26,29 @@
|
|||||||
uint8_t PopCnt16[1 << 16];
|
uint8_t PopCnt16[1 << 16];
|
||||||
int SquareDistance[SQUARE_NB][SQUARE_NB];
|
int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||||
|
|
||||||
Bitboard RookMasks [SQUARE_NB];
|
|
||||||
Bitboard RookMagics [SQUARE_NB];
|
|
||||||
Bitboard* RookAttacks[SQUARE_NB];
|
|
||||||
unsigned RookShifts [SQUARE_NB];
|
|
||||||
|
|
||||||
Bitboard BishopMasks [SQUARE_NB];
|
|
||||||
Bitboard BishopMagics [SQUARE_NB];
|
|
||||||
Bitboard* BishopAttacks[SQUARE_NB];
|
|
||||||
unsigned BishopShifts [SQUARE_NB];
|
|
||||||
|
|
||||||
Bitboard SquareBB[SQUARE_NB];
|
Bitboard SquareBB[SQUARE_NB];
|
||||||
Bitboard FileBB[FILE_NB];
|
Bitboard FileBB[FILE_NB];
|
||||||
Bitboard RankBB[RANK_NB];
|
Bitboard RankBB[RANK_NB];
|
||||||
Bitboard AdjacentFilesBB[FILE_NB];
|
Bitboard AdjacentFilesBB[FILE_NB];
|
||||||
Bitboard InFrontBB[COLOR_NB][RANK_NB];
|
Bitboard ForwardRanksBB[COLOR_NB][RANK_NB];
|
||||||
Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
|
|
||||||
Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
|
Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
|
||||||
Bitboard LineBB[SQUARE_NB][SQUARE_NB];
|
Bitboard LineBB[SQUARE_NB][SQUARE_NB];
|
||||||
Bitboard DistanceRingBB[SQUARE_NB][8];
|
Bitboard DistanceRingBB[SQUARE_NB][8];
|
||||||
Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
|
Bitboard ForwardFileBB[COLOR_NB][SQUARE_NB];
|
||||||
Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
|
Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
|
||||||
Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
|
Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
|
||||||
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
||||||
|
Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
|
||||||
|
|
||||||
|
Magic RookMagics[SQUARE_NB];
|
||||||
|
Magic BishopMagics[SQUARE_NB];
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// De Bruijn sequences. See chessprogramming.wikispaces.com/BitScan
|
|
||||||
const uint64_t DeBruijn64 = 0x3F79D71B4CB0A89ULL;
|
|
||||||
const uint32_t DeBruijn32 = 0x783A9B23;
|
|
||||||
|
|
||||||
int MSBTable[256]; // To implement software msb()
|
|
||||||
Square BSFTable[SQUARE_NB]; // To implement software bitscan
|
|
||||||
Bitboard RookTable[0x19000]; // To store rook attacks
|
Bitboard RookTable[0x19000]; // To store rook attacks
|
||||||
Bitboard BishopTable[0x1480]; // To store bishop attacks
|
Bitboard BishopTable[0x1480]; // To store bishop attacks
|
||||||
|
|
||||||
typedef unsigned (Fn)(Square, Bitboard);
|
void init_magics(Bitboard table[], Magic magics[], Direction directions[]);
|
||||||
|
|
||||||
void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
|
|
||||||
Bitboard masks[], unsigned shifts[], Square deltas[], Fn index);
|
|
||||||
|
|
||||||
// bsf_index() returns the index into BSFTable[] to look up the bitscan. Uses
|
|
||||||
// Matt Taylor's folding for 32 bit case, extended to 64 bit by Kim Walisch.
|
|
||||||
|
|
||||||
unsigned bsf_index(Bitboard b) {
|
|
||||||
b ^= b - 1;
|
|
||||||
return Is64Bit ? (b * DeBruijn64) >> 58
|
|
||||||
: ((unsigned(b) ^ unsigned(b >> 32)) * DeBruijn32) >> 26;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// popcount16() counts the non-zero bits using SWAR-Popcount algorithm
|
// popcount16() counts the non-zero bits using SWAR-Popcount algorithm
|
||||||
|
|
||||||
@@ -86,46 +60,6 @@ namespace {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef NO_BSF
|
|
||||||
|
|
||||||
/// Software fall-back of lsb() and msb() for CPU lacking hardware support
|
|
||||||
|
|
||||||
Square lsb(Bitboard b) {
|
|
||||||
assert(b);
|
|
||||||
return BSFTable[bsf_index(b)];
|
|
||||||
}
|
|
||||||
|
|
||||||
Square msb(Bitboard b) {
|
|
||||||
|
|
||||||
assert(b);
|
|
||||||
unsigned b32;
|
|
||||||
int result = 0;
|
|
||||||
|
|
||||||
if (b > 0xFFFFFFFF)
|
|
||||||
{
|
|
||||||
b >>= 32;
|
|
||||||
result = 32;
|
|
||||||
}
|
|
||||||
|
|
||||||
b32 = unsigned(b);
|
|
||||||
|
|
||||||
if (b32 > 0xFFFF)
|
|
||||||
{
|
|
||||||
b32 >>= 16;
|
|
||||||
result += 16;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (b32 > 0xFF)
|
|
||||||
{
|
|
||||||
b32 >>= 8;
|
|
||||||
result += 8;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Square(result + MSBTable[b32]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // ifdef NO_BSF
|
|
||||||
|
|
||||||
|
|
||||||
/// 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.
|
||||||
@@ -155,13 +89,7 @@ void Bitboards::init() {
|
|||||||
PopCnt16[i] = (uint8_t) popcount16(i);
|
PopCnt16[i] = (uint8_t) popcount16(i);
|
||||||
|
|
||||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||||
{
|
SquareBB[s] = (1ULL << s);
|
||||||
SquareBB[s] = 1ULL << s;
|
|
||||||
BSFTable[bsf_index(SquareBB[s])] = s;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Bitboard b = 2; b < 256; ++b)
|
|
||||||
MSBTable[b] = MSBTable[b - 1] + !more_than_one(b);
|
|
||||||
|
|
||||||
for (File f = FILE_A; f <= FILE_H; ++f)
|
for (File f = FILE_A; f <= FILE_H; ++f)
|
||||||
FileBB[f] = f > FILE_A ? FileBB[f - 1] << 1 : FileABB;
|
FileBB[f] = f > FILE_A ? FileBB[f - 1] << 1 : FileABB;
|
||||||
@@ -173,14 +101,14 @@ void Bitboards::init() {
|
|||||||
AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0);
|
AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0);
|
||||||
|
|
||||||
for (Rank r = RANK_1; r < RANK_8; ++r)
|
for (Rank r = RANK_1; r < RANK_8; ++r)
|
||||||
InFrontBB[WHITE][r] = ~(InFrontBB[BLACK][r + 1] = InFrontBB[BLACK][r] | RankBB[r]);
|
ForwardRanksBB[WHITE][r] = ~(ForwardRanksBB[BLACK][r + 1] = ForwardRanksBB[BLACK][r] | RankBB[r]);
|
||||||
|
|
||||||
for (Color c = WHITE; c <= BLACK; ++c)
|
for (Color c = WHITE; c <= BLACK; ++c)
|
||||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||||
{
|
{
|
||||||
ForwardBB[c][s] = InFrontBB[c][rank_of(s)] & FileBB[file_of(s)];
|
ForwardFileBB [c][s] = ForwardRanksBB[c][rank_of(s)] & FileBB[file_of(s)];
|
||||||
PawnAttackSpan[c][s] = InFrontBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
|
PawnAttackSpan[c][s] = ForwardRanksBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
|
||||||
PassedPawnMask[c][s] = ForwardBB[c][s] | PawnAttackSpan[c][s];
|
PassedPawnMask[c][s] = ForwardFileBB [c][s] | PawnAttackSpan[c][s];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
||||||
@@ -188,42 +116,46 @@ void Bitboards::init() {
|
|||||||
if (s1 != s2)
|
if (s1 != s2)
|
||||||
{
|
{
|
||||||
SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));
|
SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));
|
||||||
DistanceRingBB[s1][SquareDistance[s1][s2] - 1] |= s2;
|
DistanceRingBB[s1][SquareDistance[s1][s2]] |= s2;
|
||||||
}
|
}
|
||||||
|
|
||||||
int steps[][9] = { {}, { 7, 9 }, { 17, 15, 10, 6, -6, -10, -15, -17 },
|
int steps[][5] = { {}, { 7, 9 }, { 6, 10, 15, 17 }, {}, {}, {}, { 1, 7, 8, 9 } };
|
||||||
{}, {}, {}, { 9, 7, -7, -9, 8, 1, -1, -8 } };
|
|
||||||
|
|
||||||
for (Color c = WHITE; c <= BLACK; ++c)
|
for (Color c = WHITE; c <= BLACK; ++c)
|
||||||
for (PieceType pt = PAWN; pt <= KING; ++pt)
|
for (PieceType pt : { PAWN, KNIGHT, KING })
|
||||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||||
for (int i = 0; steps[pt][i]; ++i)
|
for (int i = 0; steps[pt][i]; ++i)
|
||||||
{
|
{
|
||||||
Square to = s + Square(c == WHITE ? steps[pt][i] : -steps[pt][i]);
|
Square to = s + Direction(c == WHITE ? steps[pt][i] : -steps[pt][i]);
|
||||||
|
|
||||||
if (is_ok(to) && distance(s, to) < 3)
|
if (is_ok(to) && distance(s, to) < 3)
|
||||||
StepAttacksBB[make_piece(c, pt)][s] |= to;
|
{
|
||||||
|
if (pt == PAWN)
|
||||||
|
PawnAttacks[c][s] |= to;
|
||||||
|
else
|
||||||
|
PseudoAttacks[pt][s] |= to;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Square RookDeltas[] = { NORTH, EAST, SOUTH, WEST };
|
Direction RookDirections[] = { NORTH, EAST, SOUTH, WEST };
|
||||||
Square BishopDeltas[] = { NORTH_EAST, SOUTH_EAST, SOUTH_WEST, NORTH_WEST };
|
Direction BishopDirections[] = { NORTH_EAST, SOUTH_EAST, SOUTH_WEST, NORTH_WEST };
|
||||||
|
|
||||||
init_magics(RookTable, RookAttacks, RookMagics, RookMasks, RookShifts, RookDeltas, magic_index<ROOK>);
|
init_magics(RookTable, RookMagics, RookDirections);
|
||||||
init_magics(BishopTable, BishopAttacks, BishopMagics, BishopMasks, BishopShifts, BishopDeltas, magic_index<BISHOP>);
|
init_magics(BishopTable, BishopMagics, BishopDirections);
|
||||||
|
|
||||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
||||||
{
|
{
|
||||||
PseudoAttacks[QUEEN][s1] = PseudoAttacks[BISHOP][s1] = attacks_bb<BISHOP>(s1, 0);
|
PseudoAttacks[QUEEN][s1] = PseudoAttacks[BISHOP][s1] = attacks_bb<BISHOP>(s1, 0);
|
||||||
PseudoAttacks[QUEEN][s1] |= PseudoAttacks[ ROOK][s1] = attacks_bb< ROOK>(s1, 0);
|
PseudoAttacks[QUEEN][s1] |= PseudoAttacks[ ROOK][s1] = attacks_bb< ROOK>(s1, 0);
|
||||||
|
|
||||||
for (Piece pc = W_BISHOP; pc <= W_ROOK; ++pc)
|
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[pc][s1] & s2))
|
if (!(PseudoAttacks[pt][s1] & s2))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
LineBB[s1][s2] = (attacks_bb(pc, s1, 0) & attacks_bb(pc, s2, 0)) | s1 | s2;
|
LineBB[s1][s2] = (attacks_bb(pt, s1, 0) & attacks_bb(pt, s2, 0)) | s1 | s2;
|
||||||
BetweenBB[s1][s2] = attacks_bb(pc, s1, SquareBB[s2]) & attacks_bb(pc, s2, SquareBB[s1]);
|
BetweenBB[s1][s2] = attacks_bb(pt, s1, SquareBB[s2]) & attacks_bb(pt, s2, SquareBB[s1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -231,14 +163,14 @@ void Bitboards::init() {
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
Bitboard sliding_attack(Square deltas[], Square sq, Bitboard occupied) {
|
Bitboard sliding_attack(Direction directions[], Square sq, Bitboard occupied) {
|
||||||
|
|
||||||
Bitboard attack = 0;
|
Bitboard attack = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 4; ++i)
|
for (int i = 0; i < 4; ++i)
|
||||||
for (Square s = sq + deltas[i];
|
for (Square s = sq + directions[i];
|
||||||
is_ok(s) && distance(s, s - deltas[i]) == 1;
|
is_ok(s) && distance(s, s - directions[i]) == 1;
|
||||||
s += deltas[i])
|
s += directions[i])
|
||||||
{
|
{
|
||||||
attack |= s;
|
attack |= s;
|
||||||
|
|
||||||
@@ -255,17 +187,14 @@ namespace {
|
|||||||
// chessprogramming.wikispaces.com/Magic+Bitboards. In particular, here we
|
// chessprogramming.wikispaces.com/Magic+Bitboards. In particular, here we
|
||||||
// use the so called "fancy" approach.
|
// use the so called "fancy" approach.
|
||||||
|
|
||||||
void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
|
void init_magics(Bitboard table[], Magic magics[], Direction directions[]) {
|
||||||
Bitboard masks[], unsigned shifts[], Square deltas[], Fn index) {
|
|
||||||
|
|
||||||
|
// Optimal PRNG seeds to pick the correct magics in the shortest time
|
||||||
int seeds[][RANK_NB] = { { 8977, 44560, 54343, 38998, 5731, 95205, 104912, 17020 },
|
int seeds[][RANK_NB] = { { 8977, 44560, 54343, 38998, 5731, 95205, 104912, 17020 },
|
||||||
{ 728, 10316, 55013, 32803, 12281, 15100, 16645, 255 } };
|
{ 728, 10316, 55013, 32803, 12281, 15100, 16645, 255 } };
|
||||||
|
|
||||||
Bitboard occupancy[4096], reference[4096], edges, b;
|
Bitboard occupancy[4096], reference[4096], edges, b;
|
||||||
int age[4096] = {0}, current = 0, i, size;
|
int epoch[4096] = {}, cnt = 0, size = 0;
|
||||||
|
|
||||||
// attacks[s] is a pointer to the beginning of the attacks table for square 's'
|
|
||||||
attacks[SQ_A1] = table;
|
|
||||||
|
|
||||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||||
{
|
{
|
||||||
@@ -277,28 +206,28 @@ namespace {
|
|||||||
// all the attacks for each possible subset of the mask and so is 2 power
|
// all the attacks for each possible subset of the mask and so is 2 power
|
||||||
// the number of 1s of the mask. Hence we deduce the size of the shift to
|
// the number of 1s of the mask. Hence we deduce the size of the shift to
|
||||||
// apply to the 64 or 32 bits word to get the index.
|
// apply to the 64 or 32 bits word to get the index.
|
||||||
masks[s] = sliding_attack(deltas, s, 0) & ~edges;
|
Magic& m = magics[s];
|
||||||
shifts[s] = (Is64Bit ? 64 : 32) - popcount(masks[s]);
|
m.mask = sliding_attack(directions, s, 0) & ~edges;
|
||||||
|
m.shift = (Is64Bit ? 64 : 32) - popcount(m.mask);
|
||||||
|
|
||||||
|
// Set the offset for the attacks table of the square. We have individual
|
||||||
|
// table sizes for each square with "Fancy Magic Bitboards".
|
||||||
|
m.attacks = s == SQ_A1 ? table : magics[s - 1].attacks + size;
|
||||||
|
|
||||||
// Use Carry-Rippler trick to enumerate all subsets of masks[s] and
|
// Use Carry-Rippler trick to enumerate all subsets of masks[s] and
|
||||||
// store the corresponding sliding attack bitboard in reference[].
|
// store the corresponding sliding attack bitboard in reference[].
|
||||||
b = size = 0;
|
b = size = 0;
|
||||||
do {
|
do {
|
||||||
occupancy[size] = b;
|
occupancy[size] = b;
|
||||||
reference[size] = sliding_attack(deltas, s, b);
|
reference[size] = sliding_attack(directions, s, b);
|
||||||
|
|
||||||
if (HasPext)
|
if (HasPext)
|
||||||
attacks[s][pext(b, masks[s])] = reference[size];
|
m.attacks[pext(b, m.mask)] = reference[size];
|
||||||
|
|
||||||
size++;
|
size++;
|
||||||
b = (b - masks[s]) & masks[s];
|
b = (b - m.mask) & m.mask;
|
||||||
} while (b);
|
} while (b);
|
||||||
|
|
||||||
// Set the offset for the table of the next square. We have individual
|
|
||||||
// table sizes for each square with "Fancy Magic Bitboards".
|
|
||||||
if (s < SQ_H8)
|
|
||||||
attacks[s + 1] = attacks[s] + size;
|
|
||||||
|
|
||||||
if (HasPext)
|
if (HasPext)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@@ -306,28 +235,30 @@ namespace {
|
|||||||
|
|
||||||
// Find a magic for square 's' picking up an (almost) random number
|
// Find a magic for square 's' picking up an (almost) random number
|
||||||
// until we find the one that passes the verification test.
|
// until we find the one that passes the verification test.
|
||||||
do {
|
for (int i = 0; i < size; )
|
||||||
do
|
{
|
||||||
magics[s] = rng.sparse_rand<Bitboard>();
|
for (m.magic = 0; popcount((m.magic * m.mask) >> 56) < 6; )
|
||||||
while (popcount((magics[s] * masks[s]) >> 56) < 6);
|
m.magic = rng.sparse_rand<Bitboard>();
|
||||||
|
|
||||||
// A good magic must map every possible occupancy to an index that
|
// A good magic must map every possible occupancy to an index that
|
||||||
// looks up the correct sliding attack in the attacks[s] database.
|
// looks up the correct sliding attack in the attacks[s] database.
|
||||||
// Note that we build up the database for square 's' as a side
|
// Note that we build up the database for square 's' as a side
|
||||||
// effect of verifying the magic.
|
// effect of verifying the magic. Keep track of the attempt count
|
||||||
for (++current, i = 0; i < size; ++i)
|
// and save it in epoch[], little speed-up trick to avoid resetting
|
||||||
|
// m.attacks[] after every failed attempt.
|
||||||
|
for (++cnt, i = 0; i < size; ++i)
|
||||||
{
|
{
|
||||||
unsigned idx = index(s, occupancy[i]);
|
unsigned idx = m.index(occupancy[i]);
|
||||||
|
|
||||||
if (age[idx] < current)
|
if (epoch[idx] < cnt)
|
||||||
{
|
{
|
||||||
age[idx] = current;
|
epoch[idx] = cnt;
|
||||||
attacks[s][idx] = reference[i];
|
m.attacks[idx] = reference[i];
|
||||||
}
|
}
|
||||||
else if (attacks[s][idx] != reference[i])
|
else if (m.attacks[idx] != reference[i])
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while (i < size);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+126
-80
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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
|
||||||
@@ -39,25 +39,26 @@ const std::string pretty(Bitboard b);
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
|
constexpr Bitboard AllSquares = ~Bitboard(0);
|
||||||
|
constexpr Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
|
||||||
|
|
||||||
const Bitboard FileABB = 0x0101010101010101ULL;
|
constexpr Bitboard FileABB = 0x0101010101010101ULL;
|
||||||
const Bitboard FileBBB = FileABB << 1;
|
constexpr Bitboard FileBBB = FileABB << 1;
|
||||||
const Bitboard FileCBB = FileABB << 2;
|
constexpr Bitboard FileCBB = FileABB << 2;
|
||||||
const Bitboard FileDBB = FileABB << 3;
|
constexpr Bitboard FileDBB = FileABB << 3;
|
||||||
const Bitboard FileEBB = FileABB << 4;
|
constexpr Bitboard FileEBB = FileABB << 4;
|
||||||
const Bitboard FileFBB = FileABB << 5;
|
constexpr Bitboard FileFBB = FileABB << 5;
|
||||||
const Bitboard FileGBB = FileABB << 6;
|
constexpr Bitboard FileGBB = FileABB << 6;
|
||||||
const Bitboard FileHBB = FileABB << 7;
|
constexpr Bitboard FileHBB = FileABB << 7;
|
||||||
|
|
||||||
const Bitboard Rank1BB = 0xFF;
|
constexpr Bitboard Rank1BB = 0xFF;
|
||||||
const Bitboard Rank2BB = Rank1BB << (8 * 1);
|
constexpr Bitboard Rank2BB = Rank1BB << (8 * 1);
|
||||||
const Bitboard Rank3BB = Rank1BB << (8 * 2);
|
constexpr Bitboard Rank3BB = Rank1BB << (8 * 2);
|
||||||
const Bitboard Rank4BB = Rank1BB << (8 * 3);
|
constexpr Bitboard Rank4BB = Rank1BB << (8 * 3);
|
||||||
const Bitboard Rank5BB = Rank1BB << (8 * 4);
|
constexpr Bitboard Rank5BB = Rank1BB << (8 * 4);
|
||||||
const Bitboard Rank6BB = Rank1BB << (8 * 5);
|
constexpr Bitboard Rank6BB = Rank1BB << (8 * 5);
|
||||||
const Bitboard Rank7BB = Rank1BB << (8 * 6);
|
constexpr Bitboard Rank7BB = Rank1BB << (8 * 6);
|
||||||
const Bitboard Rank8BB = Rank1BB << (8 * 7);
|
constexpr Bitboard Rank8BB = Rank1BB << (8 * 7);
|
||||||
|
|
||||||
extern int SquareDistance[SQUARE_NB][SQUARE_NB];
|
extern int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||||
|
|
||||||
@@ -65,45 +66,75 @@ extern Bitboard SquareBB[SQUARE_NB];
|
|||||||
extern Bitboard FileBB[FILE_NB];
|
extern Bitboard FileBB[FILE_NB];
|
||||||
extern Bitboard RankBB[RANK_NB];
|
extern Bitboard RankBB[RANK_NB];
|
||||||
extern Bitboard AdjacentFilesBB[FILE_NB];
|
extern Bitboard AdjacentFilesBB[FILE_NB];
|
||||||
extern Bitboard InFrontBB[COLOR_NB][RANK_NB];
|
extern Bitboard ForwardRanksBB[COLOR_NB][RANK_NB];
|
||||||
extern Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
|
|
||||||
extern Bitboard BetweenBB[SQUARE_NB][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 DistanceRingBB[SQUARE_NB][8];
|
extern Bitboard DistanceRingBB[SQUARE_NB][8];
|
||||||
extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
|
extern Bitboard ForwardFileBB[COLOR_NB][SQUARE_NB];
|
||||||
extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
|
extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
|
||||||
extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
|
extern Bitboard PawnAttackSpan[COLOR_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];
|
||||||
|
|
||||||
|
|
||||||
|
/// Magic holds all magic bitboards relevant data for a single square
|
||||||
|
struct Magic {
|
||||||
|
Bitboard mask;
|
||||||
|
Bitboard magic;
|
||||||
|
Bitboard* attacks;
|
||||||
|
unsigned shift;
|
||||||
|
|
||||||
|
// Compute the attack's index using the 'magic bitboards' approach
|
||||||
|
unsigned index(Bitboard occupied) const {
|
||||||
|
|
||||||
|
if (HasPext)
|
||||||
|
return unsigned(pext(occupied, mask));
|
||||||
|
|
||||||
|
if (Is64Bit)
|
||||||
|
return unsigned(((occupied & mask) * magic) >> shift);
|
||||||
|
|
||||||
|
unsigned lo = unsigned(occupied) & unsigned(mask);
|
||||||
|
unsigned hi = unsigned(occupied >> 32) & unsigned(mask >> 32);
|
||||||
|
return (lo * unsigned(magic) ^ hi * unsigned(magic >> 32)) >> shift;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
extern Magic RookMagics[SQUARE_NB];
|
||||||
|
extern Magic BishopMagics[SQUARE_NB];
|
||||||
|
|
||||||
|
|
||||||
/// Overloads of bitwise operators between a Bitboard and a Square for testing
|
/// Overloads of bitwise operators between a Bitboard and a Square for testing
|
||||||
/// whether a given bit is set in a bitboard, and for setting and clearing bits.
|
/// whether a given bit is set in a bitboard, and for setting and clearing bits.
|
||||||
|
|
||||||
inline Bitboard operator&(Bitboard b, Square s) {
|
inline Bitboard operator&(Bitboard b, Square s) {
|
||||||
|
assert(s >= SQ_A1 && s <= SQ_H8);
|
||||||
return b & SquareBB[s];
|
return b & SquareBB[s];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard operator|(Bitboard b, Square s) {
|
inline Bitboard operator|(Bitboard b, Square s) {
|
||||||
|
assert(s >= SQ_A1 && s <= SQ_H8);
|
||||||
return b | SquareBB[s];
|
return b | SquareBB[s];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard operator^(Bitboard b, Square s) {
|
inline Bitboard operator^(Bitboard b, Square s) {
|
||||||
|
assert(s >= SQ_A1 && s <= SQ_H8);
|
||||||
return b ^ SquareBB[s];
|
return b ^ SquareBB[s];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard& operator|=(Bitboard& b, Square s) {
|
inline Bitboard& operator|=(Bitboard& b, Square s) {
|
||||||
|
assert(s >= SQ_A1 && s <= SQ_H8);
|
||||||
return b |= SquareBB[s];
|
return b |= SquareBB[s];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard& operator^=(Bitboard& b, Square s) {
|
inline Bitboard& operator^=(Bitboard& b, Square s) {
|
||||||
|
assert(s >= SQ_A1 && s <= SQ_H8);
|
||||||
return b ^= SquareBB[s];
|
return b ^= SquareBB[s];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool more_than_one(Bitboard b) {
|
constexpr bool more_than_one(Bitboard b) {
|
||||||
return b & (b - 1);
|
return b & (b - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// rank_bb() and file_bb() return a bitboard representing all the squares on
|
/// rank_bb() and file_bb() return a bitboard representing all the squares on
|
||||||
/// the given file or rank.
|
/// the given file or rank.
|
||||||
|
|
||||||
@@ -124,17 +155,28 @@ inline Bitboard file_bb(Square s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// shift() moves a bitboard one step along direction D. Mainly for pawns
|
/// shift() moves a bitboard one step along direction D (mainly for pawns)
|
||||||
|
|
||||||
template<Square D>
|
template<Direction D>
|
||||||
inline Bitboard shift(Bitboard b) {
|
constexpr Bitboard shift(Bitboard b) {
|
||||||
return D == NORTH ? b << 8 : D == SOUTH ? b >> 8
|
return D == NORTH ? b << 8 : D == SOUTH ? b >> 8
|
||||||
: D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == SOUTH_EAST ? (b & ~FileHBB) >> 7
|
: D == EAST ? (b & ~FileHBB) << 1 : D == WEST ? (b & ~FileABB) >> 1
|
||||||
: D == NORTH_WEST ? (b & ~FileABB) << 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
|
: D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7
|
||||||
|
: D == SOUTH_EAST ? (b & ~FileHBB) >> 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
|
||||||
: 0;
|
: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// pawn_attacks_bb() returns the pawn attacks for the given color from the
|
||||||
|
/// squares in the given bitboard.
|
||||||
|
|
||||||
|
template<Color C>
|
||||||
|
constexpr Bitboard pawn_attacks_bb(Bitboard b) {
|
||||||
|
return C == WHITE ? shift<NORTH_WEST>(b) | shift<NORTH_EAST>(b)
|
||||||
|
: shift<SOUTH_WEST>(b) | shift<SOUTH_EAST>(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// adjacent_files_bb() returns a bitboard representing all the squares on the
|
/// adjacent_files_bb() returns a bitboard representing all the squares on the
|
||||||
/// adjacent files of the given one.
|
/// adjacent files of the given one.
|
||||||
|
|
||||||
@@ -153,28 +195,28 @@ inline Bitboard between_bb(Square s1, Square s2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// in_front_bb() returns a bitboard representing all the squares on all the ranks
|
/// forward_ranks_bb() returns a bitboard representing the squares on all the ranks
|
||||||
/// in front of the given one, from the point of view of the given color. For
|
/// in front of the given one, from the point of view of the given color. For instance,
|
||||||
/// instance, in_front_bb(BLACK, RANK_3) will return the squares on ranks 1 and 2.
|
/// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
|
||||||
|
|
||||||
inline Bitboard in_front_bb(Color c, Rank r) {
|
inline Bitboard forward_ranks_bb(Color c, Square s) {
|
||||||
return InFrontBB[c][r];
|
return ForwardRanksBB[c][rank_of(s)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// forward_bb() returns a bitboard representing all the squares along the line
|
/// forward_file_bb() returns a bitboard representing all the squares along the line
|
||||||
/// in front of the given one, from the point of view of the given color:
|
/// in front of the given one, from the point of view of the given color:
|
||||||
/// ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s)
|
/// ForwardFileBB[c][s] = forward_ranks_bb(c, s) & file_bb(s)
|
||||||
|
|
||||||
inline Bitboard forward_bb(Color c, Square s) {
|
inline Bitboard forward_file_bb(Color c, Square s) {
|
||||||
return ForwardBB[c][s];
|
return ForwardFileBB[c][s];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// pawn_attack_span() returns a bitboard representing all the squares that can be
|
/// pawn_attack_span() returns a bitboard representing all the squares that can be
|
||||||
/// attacked by a pawn of the given color when it moves along its file, starting
|
/// attacked by a pawn of the given color when it moves along its file, starting
|
||||||
/// from the given square:
|
/// from the given square:
|
||||||
/// PawnAttackSpan[c][s] = in_front_bb(c, s) & adjacent_files_bb(s);
|
/// PawnAttackSpan[c][s] = forward_ranks_bb(c, s) & adjacent_files_bb(file_of(s));
|
||||||
|
|
||||||
inline Bitboard pawn_attack_span(Color c, Square s) {
|
inline Bitboard pawn_attack_span(Color c, Square s) {
|
||||||
return PawnAttackSpan[c][s];
|
return PawnAttackSpan[c][s];
|
||||||
@@ -183,7 +225,7 @@ inline Bitboard pawn_attack_span(Color c, Square s) {
|
|||||||
|
|
||||||
/// passed_pawn_mask() returns a bitboard mask which can be used to test if a
|
/// passed_pawn_mask() returns a bitboard mask which can be used to test if a
|
||||||
/// pawn of the given color and on the given square is a passed pawn:
|
/// pawn of the given color and on the given square is a passed pawn:
|
||||||
/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_bb(c, s)
|
/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_file_bb(c, s)
|
||||||
|
|
||||||
inline Bitboard passed_pawn_mask(Color c, Square s) {
|
inline Bitboard passed_pawn_mask(Color c, Square s) {
|
||||||
return PassedPawnMask[c][s];
|
return PassedPawnMask[c][s];
|
||||||
@@ -210,50 +252,25 @@ template<> inline int distance<Rank>(Square x, Square y) { return distance(rank_
|
|||||||
|
|
||||||
|
|
||||||
/// attacks_bb() returns a bitboard representing all the squares attacked by a
|
/// attacks_bb() returns a bitboard representing all the squares attacked by a
|
||||||
/// piece of type Pt (bishop or rook) placed on 's'. The helper magic_index()
|
/// piece of type Pt (bishop or rook) placed on 's'.
|
||||||
/// looks up the index using the 'magic bitboards' approach.
|
|
||||||
template<PieceType Pt>
|
|
||||||
inline unsigned magic_index(Square s, Bitboard occupied) {
|
|
||||||
|
|
||||||
extern Bitboard RookMasks[SQUARE_NB];
|
|
||||||
extern Bitboard RookMagics[SQUARE_NB];
|
|
||||||
extern unsigned RookShifts[SQUARE_NB];
|
|
||||||
extern Bitboard BishopMasks[SQUARE_NB];
|
|
||||||
extern Bitboard BishopMagics[SQUARE_NB];
|
|
||||||
extern unsigned BishopShifts[SQUARE_NB];
|
|
||||||
|
|
||||||
Bitboard* const Masks = Pt == ROOK ? RookMasks : BishopMasks;
|
|
||||||
Bitboard* const Magics = Pt == ROOK ? RookMagics : BishopMagics;
|
|
||||||
unsigned* const Shifts = Pt == ROOK ? RookShifts : BishopShifts;
|
|
||||||
|
|
||||||
if (HasPext)
|
|
||||||
return unsigned(pext(occupied, Masks[s]));
|
|
||||||
|
|
||||||
if (Is64Bit)
|
|
||||||
return unsigned(((occupied & Masks[s]) * Magics[s]) >> Shifts[s]);
|
|
||||||
|
|
||||||
unsigned lo = unsigned(occupied) & unsigned(Masks[s]);
|
|
||||||
unsigned hi = unsigned(occupied >> 32) & unsigned(Masks[s] >> 32);
|
|
||||||
return (lo * unsigned(Magics[s]) ^ hi * unsigned(Magics[s] >> 32)) >> Shifts[s];
|
|
||||||
}
|
|
||||||
|
|
||||||
template<PieceType Pt>
|
template<PieceType Pt>
|
||||||
inline Bitboard attacks_bb(Square s, Bitboard occupied) {
|
inline Bitboard attacks_bb(Square s, Bitboard occupied) {
|
||||||
|
|
||||||
extern Bitboard* RookAttacks[SQUARE_NB];
|
const Magic& m = Pt == ROOK ? RookMagics[s] : BishopMagics[s];
|
||||||
extern Bitboard* BishopAttacks[SQUARE_NB];
|
return m.attacks[m.index(occupied)];
|
||||||
|
|
||||||
return (Pt == ROOK ? RookAttacks : BishopAttacks)[s][magic_index<Pt>(s, occupied)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard attacks_bb(Piece pc, Square s, Bitboard occupied) {
|
inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
|
||||||
|
|
||||||
switch (type_of(pc))
|
assert(pt != PAWN);
|
||||||
|
|
||||||
|
switch (pt)
|
||||||
{
|
{
|
||||||
case BISHOP: return attacks_bb<BISHOP>(s, occupied);
|
case BISHOP: return attacks_bb<BISHOP>(s, occupied);
|
||||||
case ROOK : return attacks_bb<ROOK>(s, occupied);
|
case ROOK : return attacks_bb< ROOK>(s, occupied);
|
||||||
case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
|
case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
|
||||||
default : return StepAttacksBB[pc][s];
|
default : return PseudoAttacks[pt][s];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,7 +299,7 @@ inline int popcount(Bitboard b) {
|
|||||||
|
|
||||||
/// lsb() and msb() return the least/most significant bit in a non-zero bitboard
|
/// lsb() and msb() return the least/most significant bit in a non-zero bitboard
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__) // GCC, Clang, ICC
|
||||||
|
|
||||||
inline Square lsb(Bitboard b) {
|
inline Square lsb(Bitboard b) {
|
||||||
assert(b);
|
assert(b);
|
||||||
@@ -291,10 +308,12 @@ inline Square lsb(Bitboard b) {
|
|||||||
|
|
||||||
inline Square msb(Bitboard b) {
|
inline Square msb(Bitboard b) {
|
||||||
assert(b);
|
assert(b);
|
||||||
return Square(63 - __builtin_clzll(b));
|
return Square(63 ^ __builtin_clzll(b));
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined(_WIN64) && defined(_MSC_VER)
|
#elif defined(_MSC_VER) // MSVC
|
||||||
|
|
||||||
|
#ifdef _WIN64 // MSVC, WIN64
|
||||||
|
|
||||||
inline Square lsb(Bitboard b) {
|
inline Square lsb(Bitboard b) {
|
||||||
assert(b);
|
assert(b);
|
||||||
@@ -310,12 +329,39 @@ inline Square msb(Bitboard b) {
|
|||||||
return (Square) idx;
|
return (Square) idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else // MSVC, WIN32
|
||||||
|
|
||||||
#define NO_BSF // Fallback on software implementation for other cases
|
inline Square lsb(Bitboard b) {
|
||||||
|
assert(b);
|
||||||
|
unsigned long idx;
|
||||||
|
|
||||||
Square lsb(Bitboard b);
|
if (b & 0xffffffff) {
|
||||||
Square msb(Bitboard b);
|
_BitScanForward(&idx, int32_t(b));
|
||||||
|
return Square(idx);
|
||||||
|
} else {
|
||||||
|
_BitScanForward(&idx, int32_t(b >> 32));
|
||||||
|
return Square(idx + 32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Square msb(Bitboard b) {
|
||||||
|
assert(b);
|
||||||
|
unsigned long idx;
|
||||||
|
|
||||||
|
if (b >> 32) {
|
||||||
|
_BitScanReverse(&idx, int32_t(b >> 32));
|
||||||
|
return Square(idx + 32);
|
||||||
|
} else {
|
||||||
|
_BitScanReverse(&idx, int32_t(b));
|
||||||
|
return Square(idx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#else // Compiler is neither GCC nor MSVC compatible
|
||||||
|
|
||||||
|
#error "Compiler not supported."
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
+14
-88
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,7 +31,7 @@ namespace {
|
|||||||
|
|
||||||
// Table used to drive the king towards the edge of the board
|
// Table used to drive the king towards the edge of the board
|
||||||
// in KX vs K and KQ vs KR endgames.
|
// in KX vs K and KQ vs KR endgames.
|
||||||
const int PushToEdges[SQUARE_NB] = {
|
constexpr int PushToEdges[SQUARE_NB] = {
|
||||||
100, 90, 80, 70, 70, 80, 90, 100,
|
100, 90, 80, 70, 70, 80, 90, 100,
|
||||||
90, 70, 60, 50, 50, 60, 70, 90,
|
90, 70, 60, 50, 50, 60, 70, 90,
|
||||||
80, 60, 40, 30, 30, 40, 60, 80,
|
80, 60, 40, 30, 30, 40, 60, 80,
|
||||||
@@ -44,7 +44,7 @@ namespace {
|
|||||||
|
|
||||||
// Table used to drive the king towards a corner square of the
|
// Table used to drive the king towards a corner square of the
|
||||||
// right color in KBN vs K endgames.
|
// right color in KBN vs K endgames.
|
||||||
const int PushToCorners[SQUARE_NB] = {
|
constexpr int PushToCorners[SQUARE_NB] = {
|
||||||
200, 190, 180, 170, 160, 150, 140, 130,
|
200, 190, 180, 170, 160, 150, 140, 130,
|
||||||
190, 180, 170, 160, 150, 140, 130, 140,
|
190, 180, 170, 160, 150, 140, 130, 140,
|
||||||
180, 170, 155, 140, 140, 125, 140, 150,
|
180, 170, 155, 140, 140, 125, 140, 150,
|
||||||
@@ -56,11 +56,11 @@ namespace {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Tables used to drive a piece towards or away from another piece
|
// Tables used to drive a piece towards or away from another piece
|
||||||
const int PushClose[8] = { 0, 0, 100, 80, 60, 40, 20, 10 };
|
constexpr int PushClose[8] = { 0, 0, 100, 80, 60, 40, 20, 10 };
|
||||||
const int PushAway [8] = { 0, 5, 20, 40, 60, 80, 90, 100 };
|
constexpr int PushAway [8] = { 0, 5, 20, 40, 60, 80, 90, 100 };
|
||||||
|
|
||||||
// Pawn Rank based scaling factors used in KRPPKRP endgame
|
// Pawn Rank based scaling factors used in KRPPKRP endgame
|
||||||
const int KRPPKRPScaleFactors[RANK_NB] = { 0, 9, 10, 14, 21, 44, 0, 0 };
|
constexpr int KRPPKRPScaleFactors[RANK_NB] = { 0, 9, 10, 14, 21, 44, 0, 0 };
|
||||||
|
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
bool verify_material(const Position& pos, Color c, Value npm, int pawnsCnt) {
|
bool verify_material(const Position& pos, Color c, Value npm, int pawnsCnt) {
|
||||||
@@ -83,60 +83,9 @@ namespace {
|
|||||||
return sq;
|
return sq;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the material key of Position out of the given endgame key code
|
|
||||||
// like "KBPKN". The trick here is to first forge an ad-hoc FEN string
|
|
||||||
// and then let a Position object do the work for us.
|
|
||||||
Key key(const string& code, Color c) {
|
|
||||||
|
|
||||||
assert(code.length() > 0 && code.length() < 8);
|
|
||||||
assert(code[0] == 'K');
|
|
||||||
|
|
||||||
string sides[] = { code.substr(code.find('K', 1)), // Weak
|
|
||||||
code.substr(0, code.find('K', 1)) }; // Strong
|
|
||||||
|
|
||||||
std::transform(sides[c].begin(), sides[c].end(), sides[c].begin(), tolower);
|
|
||||||
|
|
||||||
string fen = sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/8/8/"
|
|
||||||
+ sides[1] + char(8 - sides[1].length() + '0') + " w - - 0 10";
|
|
||||||
|
|
||||||
StateInfo st;
|
|
||||||
return Position().set(fen, false, &st, nullptr).material_key();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
/// Endgames members definitions
|
|
||||||
|
|
||||||
Endgames::Endgames() {
|
|
||||||
|
|
||||||
add<KPK>("KPK");
|
|
||||||
add<KNNK>("KNNK");
|
|
||||||
add<KBNK>("KBNK");
|
|
||||||
add<KRKP>("KRKP");
|
|
||||||
add<KRKB>("KRKB");
|
|
||||||
add<KRKN>("KRKN");
|
|
||||||
add<KQKP>("KQKP");
|
|
||||||
add<KQKR>("KQKR");
|
|
||||||
|
|
||||||
add<KNPK>("KNPK");
|
|
||||||
add<KNPKB>("KNPKB");
|
|
||||||
add<KRPKR>("KRPKR");
|
|
||||||
add<KRPKB>("KRPKB");
|
|
||||||
add<KBPKB>("KBPKB");
|
|
||||||
add<KBPKN>("KBPKN");
|
|
||||||
add<KBPPKB>("KBPPKB");
|
|
||||||
add<KRPPKRP>("KRPPKRP");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<EndgameType E, typename T>
|
|
||||||
void Endgames::add(const string& code) {
|
|
||||||
map<T>()[key(code, WHITE)] = std::unique_ptr<EndgameBase<T>>(new Endgame<E>(WHITE));
|
|
||||||
map<T>()[key(code, BLACK)] = std::unique_ptr<EndgameBase<T>>(new Endgame<E>(BLACK));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Mate with KX vs K. This function is used to evaluate positions with
|
/// Mate with KX vs K. This function is used to evaluate positions with
|
||||||
/// king and plenty of material vs a lone king. It simply gives the
|
/// king and plenty of material vs a lone king. It simply gives the
|
||||||
/// attacking side a bonus for driving the defending king towards the edge
|
/// attacking side a bonus for driving the defending king towards the edge
|
||||||
@@ -162,8 +111,8 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
|
|||||||
if ( pos.count<QUEEN>(strongSide)
|
if ( pos.count<QUEEN>(strongSide)
|
||||||
|| pos.count<ROOK>(strongSide)
|
|| pos.count<ROOK>(strongSide)
|
||||||
||(pos.count<BISHOP>(strongSide) && pos.count<KNIGHT>(strongSide))
|
||(pos.count<BISHOP>(strongSide) && pos.count<KNIGHT>(strongSide))
|
||||||
||(pos.count<BISHOP>(strongSide) > 1 && opposite_colors(pos.squares<BISHOP>(strongSide)[0],
|
|| ( (pos.pieces(strongSide, BISHOP) & ~DarkSquares)
|
||||||
pos.squares<BISHOP>(strongSide)[1])))
|
&& (pos.pieces(strongSide, BISHOP) & DarkSquares)))
|
||||||
result = std::min(result + VALUE_KNOWN_WIN, VALUE_MATE_IN_MAX_PLY - 1);
|
result = std::min(result + VALUE_KNOWN_WIN, VALUE_MATE_IN_MAX_PLY - 1);
|
||||||
|
|
||||||
return strongSide == pos.side_to_move() ? result : -result;
|
return strongSide == pos.side_to_move() ? result : -result;
|
||||||
@@ -241,7 +190,7 @@ Value Endgame<KRKP>::operator()(const Position& pos) const {
|
|||||||
Value result;
|
Value result;
|
||||||
|
|
||||||
// If the stronger side's king is in front of the pawn, it's a win
|
// If the stronger side's king is in front of the pawn, it's a win
|
||||||
if (wksq < psq && file_of(wksq) == file_of(psq))
|
if (forward_file_bb(WHITE, wksq) & psq)
|
||||||
result = RookValueEg - distance(wksq, psq);
|
result = RookValueEg - distance(wksq, psq);
|
||||||
|
|
||||||
// If the weaker side's king is too far from the pawn and the rook,
|
// If the weaker side's king is too far from the pawn and the rook,
|
||||||
@@ -267,7 +216,7 @@ Value Endgame<KRKP>::operator()(const Position& pos) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// KR vs KB. This is very simple, and always returns drawish scores. The
|
/// KR vs KB. This is very simple, and always returns drawish scores. The
|
||||||
/// score is slightly bigger when the defending king is close to the edge.
|
/// score is slightly bigger when the defending king is close to the edge.
|
||||||
template<>
|
template<>
|
||||||
Value Endgame<KRKB>::operator()(const Position& pos) const {
|
Value Endgame<KRKB>::operator()(const Position& pos) const {
|
||||||
@@ -551,7 +500,7 @@ ScaleFactor Endgame<KRPKB>::operator()(const Position& pos) const {
|
|||||||
Square bsq = pos.square<BISHOP>(weakSide);
|
Square bsq = pos.square<BISHOP>(weakSide);
|
||||||
Square psq = pos.square<PAWN>(strongSide);
|
Square psq = pos.square<PAWN>(strongSide);
|
||||||
Rank rk = relative_rank(strongSide, psq);
|
Rank rk = relative_rank(strongSide, psq);
|
||||||
Square push = pawn_push(strongSide);
|
Direction push = pawn_push(strongSide);
|
||||||
|
|
||||||
// If the pawn is on the 5th rank and the pawn (currently) is on
|
// If the pawn is on the 5th rank and the pawn (currently) is on
|
||||||
// the same color square as the bishop then there is a chance of
|
// the same color square as the bishop then there is a chance of
|
||||||
@@ -625,7 +574,7 @@ ScaleFactor Endgame<KPsK>::operator()(const Position& pos) const {
|
|||||||
|
|
||||||
// If all pawns are ahead of the king, on a single rook file and
|
// If all pawns are ahead of the king, on a single rook file and
|
||||||
// the king is within one file of the pawns, it's a draw.
|
// the king is within one file of the pawns, it's a draw.
|
||||||
if ( !(pawns & ~in_front_bb(weakSide, rank_of(ksq)))
|
if ( !(pawns & ~forward_ranks_bb(weakSide, ksq))
|
||||||
&& !((pawns & ~FileABB) && (pawns & ~FileHBB))
|
&& !((pawns & ~FileABB) && (pawns & ~FileHBB))
|
||||||
&& distance<File>(ksq, lsb(pawns)) <= 1)
|
&& distance<File>(ksq, lsb(pawns)) <= 1)
|
||||||
return SCALE_FACTOR_DRAW;
|
return SCALE_FACTOR_DRAW;
|
||||||
@@ -658,31 +607,8 @@ ScaleFactor Endgame<KBPKB>::operator()(const Position& pos) const {
|
|||||||
|
|
||||||
// Case 2: Opposite colored bishops
|
// Case 2: Opposite colored bishops
|
||||||
if (opposite_colors(strongBishopSq, weakBishopSq))
|
if (opposite_colors(strongBishopSq, weakBishopSq))
|
||||||
{
|
return SCALE_FACTOR_DRAW;
|
||||||
// We assume that the position is drawn in the following three situations:
|
|
||||||
//
|
|
||||||
// a. The pawn is on rank 5 or further back.
|
|
||||||
// b. The defending king is somewhere in the pawn's path.
|
|
||||||
// c. The defending bishop attacks some square along the pawn's path,
|
|
||||||
// and is at least three squares away from the pawn.
|
|
||||||
//
|
|
||||||
// These rules are probably not perfect, but in practice they work
|
|
||||||
// reasonably well.
|
|
||||||
|
|
||||||
if (relative_rank(strongSide, pawnSq) <= RANK_5)
|
|
||||||
return SCALE_FACTOR_DRAW;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Bitboard path = forward_bb(strongSide, pawnSq);
|
|
||||||
|
|
||||||
if (path & pos.pieces(weakSide, KING))
|
|
||||||
return SCALE_FACTOR_DRAW;
|
|
||||||
|
|
||||||
if ( (pos.attacks_from<BISHOP>(weakBishopSq) & path)
|
|
||||||
&& distance(weakBishopSq, pawnSq) >= 3)
|
|
||||||
return SCALE_FACTOR_DRAW;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return SCALE_FACTOR_NONE;
|
return SCALE_FACTOR_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -809,7 +735,7 @@ ScaleFactor Endgame<KNPKB>::operator()(const Position& pos) const {
|
|||||||
|
|
||||||
// King needs to get close to promoting pawn to prevent knight from blocking.
|
// King needs to get close to promoting pawn to prevent knight from blocking.
|
||||||
// Rules for this are very tricky, so just approximate.
|
// Rules for this are very tricky, so just approximate.
|
||||||
if (forward_bb(strongSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
|
if (forward_file_bb(strongSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
|
||||||
return ScaleFactor(distance(weakKingSq, pawnSq));
|
return ScaleFactor(distance(weakKingSq, pawnSq));
|
||||||
|
|
||||||
return SCALE_FACTOR_NONE;
|
return SCALE_FACTOR_NONE;
|
||||||
|
|||||||
+44
-24
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,12 +31,11 @@
|
|||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
|
||||||
/// EndgameType lists all supported endgames
|
/// EndgameCode lists all supported endgame functions by corresponding codes
|
||||||
|
|
||||||
enum EndgameType {
|
enum EndgameCode {
|
||||||
|
|
||||||
// Evaluation functions
|
|
||||||
|
|
||||||
|
EVALUATION_FUNCTIONS,
|
||||||
KNNK, // KNN vs K
|
KNNK, // KNN vs K
|
||||||
KXK, // Generic "mate lone king" eval
|
KXK, // Generic "mate lone king" eval
|
||||||
KBNK, // KBN vs K
|
KBNK, // KBN vs K
|
||||||
@@ -47,10 +46,7 @@ enum EndgameType {
|
|||||||
KQKP, // KQ vs KP
|
KQKP, // KQ vs KP
|
||||||
KQKR, // KQ vs KR
|
KQKR, // KQ vs KR
|
||||||
|
|
||||||
|
|
||||||
// Scaling functions
|
|
||||||
SCALING_FUNCTIONS,
|
SCALING_FUNCTIONS,
|
||||||
|
|
||||||
KBPsK, // KB and pawns vs K
|
KBPsK, // KB and pawns vs K
|
||||||
KQKRPs, // KQ vs KR and pawns
|
KQKRPs, // KQ vs KR and pawns
|
||||||
KRPKR, // KRP vs KR
|
KRPKR, // KRP vs KR
|
||||||
@@ -68,30 +64,29 @@ enum EndgameType {
|
|||||||
|
|
||||||
/// Endgame functions can be of two types depending on whether they return a
|
/// Endgame functions can be of two types depending on whether they return a
|
||||||
/// Value or a ScaleFactor.
|
/// Value or a ScaleFactor.
|
||||||
template<EndgameType E> using
|
|
||||||
|
template<EndgameCode E> using
|
||||||
eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type;
|
eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type;
|
||||||
|
|
||||||
|
|
||||||
/// Base and derived templates for endgame evaluation and scaling functions
|
/// Base and derived functors for endgame evaluation and scaling functions
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct EndgameBase {
|
struct EndgameBase {
|
||||||
|
|
||||||
|
explicit EndgameBase(Color c) : strongSide(c), weakSide(~c) {}
|
||||||
virtual ~EndgameBase() = default;
|
virtual ~EndgameBase() = default;
|
||||||
virtual Color strong_side() const = 0;
|
|
||||||
virtual T operator()(const Position&) const = 0;
|
virtual T operator()(const Position&) const = 0;
|
||||||
|
|
||||||
|
const Color strongSide, weakSide;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<EndgameType E, typename T = eg_type<E>>
|
template<EndgameCode E, typename T = eg_type<E>>
|
||||||
struct Endgame : public EndgameBase<T> {
|
struct Endgame : public EndgameBase<T> {
|
||||||
|
|
||||||
explicit Endgame(Color c) : strongSide(c), weakSide(~c) {}
|
explicit Endgame(Color c) : EndgameBase<T>(c) {}
|
||||||
Color strong_side() const { return strongSide; }
|
T operator()(const Position&) const override;
|
||||||
T operator()(const Position&) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
Color strongSide, weakSide;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -101,23 +96,48 @@ private:
|
|||||||
|
|
||||||
class Endgames {
|
class Endgames {
|
||||||
|
|
||||||
template<typename T> using Map = std::map<Key, std::unique_ptr<EndgameBase<T>>>;
|
template<typename T> using Ptr = std::unique_ptr<EndgameBase<T>>;
|
||||||
|
template<typename T> using Map = std::map<Key, Ptr<T>>;
|
||||||
template<EndgameType E, typename T = eg_type<E>>
|
|
||||||
void add(const std::string& code);
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Map<T>& map() {
|
Map<T>& map() {
|
||||||
return std::get<std::is_same<T, ScaleFactor>::value>(maps);
|
return std::get<std::is_same<T, ScaleFactor>::value>(maps);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<EndgameCode E, typename T = eg_type<E>>
|
||||||
|
void add(const std::string& code) {
|
||||||
|
|
||||||
|
StateInfo st;
|
||||||
|
map<T>()[Position().set(code, WHITE, &st).material_key()] = Ptr<T>(new Endgame<E>(WHITE));
|
||||||
|
map<T>()[Position().set(code, BLACK, &st).material_key()] = Ptr<T>(new Endgame<E>(BLACK));
|
||||||
|
}
|
||||||
|
|
||||||
std::pair<Map<Value>, Map<ScaleFactor>> maps;
|
std::pair<Map<Value>, Map<ScaleFactor>> maps;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Endgames();
|
Endgames() {
|
||||||
|
|
||||||
|
add<KPK>("KPK");
|
||||||
|
add<KNNK>("KNNK");
|
||||||
|
add<KBNK>("KBNK");
|
||||||
|
add<KRKP>("KRKP");
|
||||||
|
add<KRKB>("KRKB");
|
||||||
|
add<KRKN>("KRKN");
|
||||||
|
add<KQKP>("KQKP");
|
||||||
|
add<KQKR>("KQKR");
|
||||||
|
|
||||||
|
add<KNPK>("KNPK");
|
||||||
|
add<KNPKB>("KNPKB");
|
||||||
|
add<KRPKR>("KRPKR");
|
||||||
|
add<KRPKB>("KRPKB");
|
||||||
|
add<KBPKB>("KBPKB");
|
||||||
|
add<KBPKN>("KBPKN");
|
||||||
|
add<KBPPKB>("KBPPKB");
|
||||||
|
add<KRPPKRP>("KRPPKRP");
|
||||||
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
EndgameBase<T>* probe(Key key) {
|
const EndgameBase<T>* probe(Key key) {
|
||||||
return map<T>().count(key) ? map<T>()[key].get() : nullptr;
|
return map<T>().count(key) ? map<T>()[key].get() : nullptr;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+606
-614
File diff suppressed because it is too large
Load Diff
+2
-3
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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
|
||||||
@@ -29,11 +29,10 @@ class Position;
|
|||||||
|
|
||||||
namespace Eval {
|
namespace Eval {
|
||||||
|
|
||||||
const Value Tempo = Value(20); // Must be visible to search
|
constexpr Value Tempo = Value(20); // Must be visible to search
|
||||||
|
|
||||||
std::string trace(const Position& pos);
|
std::string trace(const Position& pos);
|
||||||
|
|
||||||
template<bool DoTrace = false>
|
|
||||||
Value evaluate(const Position& pos);
|
Value evaluate(const Position& pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-5
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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
|
||||||
@@ -43,12 +43,11 @@ int main(int argc, char* argv[]) {
|
|||||||
Bitbases::init();
|
Bitbases::init();
|
||||||
Search::init();
|
Search::init();
|
||||||
Pawns::init();
|
Pawns::init();
|
||||||
Threads.init();
|
Threads.set(Options["Threads"]);
|
||||||
Tablebases::init(Options["SyzygyPath"]);
|
Search::clear(); // After threads are up
|
||||||
TT.resize(Options["Hash"]);
|
|
||||||
|
|
||||||
UCI::loop(argc, argv);
|
UCI::loop(argc, argv);
|
||||||
|
|
||||||
Threads.exit();
|
Threads.set(0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-28
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,18 +31,18 @@ namespace {
|
|||||||
|
|
||||||
// Polynomial material imbalance parameters
|
// Polynomial material imbalance parameters
|
||||||
|
|
||||||
const int QuadraticOurs[][PIECE_TYPE_NB] = {
|
constexpr int QuadraticOurs[][PIECE_TYPE_NB] = {
|
||||||
// OUR PIECES
|
// OUR PIECES
|
||||||
// pair pawn knight bishop rook queen
|
// pair pawn knight bishop rook queen
|
||||||
{1667 }, // Bishop pair
|
{1438 }, // Bishop pair
|
||||||
{ 40, 2 }, // Pawn
|
{ 40, 38 }, // Pawn
|
||||||
{ 32, 255, -3 }, // Knight OUR PIECES
|
{ 32, 255, -62 }, // Knight OUR PIECES
|
||||||
{ 0, 104, 4, 0 }, // Bishop
|
{ 0, 104, 4, 0 }, // Bishop
|
||||||
{ -26, -2, 47, 105, -149 }, // Rook
|
{ -26, -2, 47, 105, -208 }, // Rook
|
||||||
{-185, 24, 122, 137, -134, 0 } // Queen
|
{-189, 24, 117, 133, -134, -6 } // Queen
|
||||||
};
|
};
|
||||||
|
|
||||||
const int QuadraticTheirs[][PIECE_TYPE_NB] = {
|
constexpr int QuadraticTheirs[][PIECE_TYPE_NB] = {
|
||||||
// THEIR PIECES
|
// THEIR PIECES
|
||||||
// pair pawn knight bishop rook queen
|
// pair pawn knight bishop rook queen
|
||||||
{ 0 }, // Bishop pair
|
{ 0 }, // Bishop pair
|
||||||
@@ -50,7 +50,7 @@ namespace {
|
|||||||
{ 9, 63, 0 }, // Knight OUR PIECES
|
{ 9, 63, 0 }, // Knight OUR PIECES
|
||||||
{ 59, 65, 42, 0 }, // Bishop
|
{ 59, 65, 42, 0 }, // Bishop
|
||||||
{ 46, 39, 24, -24, 0 }, // Rook
|
{ 46, 39, 24, -24, 0 }, // Rook
|
||||||
{ 101, 100, -37, 141, 268, 0 } // Queen
|
{ 97, 100, -42, 137, 268, 0 } // Queen
|
||||||
};
|
};
|
||||||
|
|
||||||
// Endgame evaluation and scaling functions are accessed directly and not through
|
// Endgame evaluation and scaling functions are accessed directly and not through
|
||||||
@@ -68,7 +68,7 @@ namespace {
|
|||||||
&& pos.non_pawn_material(us) >= RookValueMg;
|
&& pos.non_pawn_material(us) >= RookValueMg;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_KBPsKs(const Position& pos, Color us) {
|
bool is_KBPsK(const Position& pos, Color us) {
|
||||||
return pos.non_pawn_material(us) == BishopValueMg
|
return pos.non_pawn_material(us) == BishopValueMg
|
||||||
&& pos.count<BISHOP>(us) == 1
|
&& pos.count<BISHOP>(us) == 1
|
||||||
&& pos.count<PAWN >(us) >= 1;
|
&& pos.count<PAWN >(us) >= 1;
|
||||||
@@ -77,7 +77,7 @@ namespace {
|
|||||||
bool is_KQKRPs(const Position& pos, Color us) {
|
bool is_KQKRPs(const Position& pos, Color us) {
|
||||||
return !pos.count<PAWN>(us)
|
return !pos.count<PAWN>(us)
|
||||||
&& pos.non_pawn_material(us) == QueenValueMg
|
&& pos.non_pawn_material(us) == QueenValueMg
|
||||||
&& pos.count<QUEEN>(us) == 1
|
&& pos.count<QUEEN>(us) == 1
|
||||||
&& pos.count<ROOK>(~us) == 1
|
&& pos.count<ROOK>(~us) == 1
|
||||||
&& pos.count<PAWN>(~us) >= 1;
|
&& pos.count<PAWN>(~us) >= 1;
|
||||||
}
|
}
|
||||||
@@ -87,11 +87,11 @@ namespace {
|
|||||||
template<Color Us>
|
template<Color Us>
|
||||||
int imbalance(const int pieceCount[][PIECE_TYPE_NB]) {
|
int imbalance(const int pieceCount[][PIECE_TYPE_NB]) {
|
||||||
|
|
||||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||||
|
|
||||||
int bonus = 0;
|
int bonus = 0;
|
||||||
|
|
||||||
// Second-degree polynomial material imbalance by Tord Romstad
|
// Second-degree polynomial material imbalance, by Tord Romstad
|
||||||
for (int pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1)
|
for (int pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1)
|
||||||
{
|
{
|
||||||
if (!pieceCount[Us][pt1])
|
if (!pieceCount[Us][pt1])
|
||||||
@@ -129,7 +129,13 @@ Entry* probe(const Position& pos) {
|
|||||||
std::memset(e, 0, sizeof(Entry));
|
std::memset(e, 0, sizeof(Entry));
|
||||||
e->key = key;
|
e->key = key;
|
||||||
e->factor[WHITE] = e->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL;
|
e->factor[WHITE] = e->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL;
|
||||||
e->gamePhase = pos.game_phase();
|
|
||||||
|
Value npm_w = pos.non_pawn_material(WHITE);
|
||||||
|
Value npm_b = pos.non_pawn_material(BLACK);
|
||||||
|
Value npm = std::max(EndgameLimit, std::min(npm_w + npm_b, MidgameLimit));
|
||||||
|
|
||||||
|
// Map total non-pawn material into [PHASE_ENDGAME, PHASE_MIDGAME]
|
||||||
|
e->gamePhase = Phase(((npm - EndgameLimit) * PHASE_MIDGAME) / (MidgameLimit - EndgameLimit));
|
||||||
|
|
||||||
// Let's look if we have a specialized evaluation function for this particular
|
// Let's look if we have a specialized evaluation function for this particular
|
||||||
// material configuration. Firstly we look for a fixed configuration one, then
|
// material configuration. Firstly we look for a fixed configuration one, then
|
||||||
@@ -146,11 +152,11 @@ Entry* probe(const Position& pos) {
|
|||||||
|
|
||||||
// OK, we didn't find any special evaluation function for the current material
|
// OK, we didn't find any special evaluation function for the current material
|
||||||
// configuration. Is there a suitable specialized scaling function?
|
// configuration. Is there a suitable specialized scaling function?
|
||||||
EndgameBase<ScaleFactor>* sf;
|
const EndgameBase<ScaleFactor>* sf;
|
||||||
|
|
||||||
if ((sf = pos.this_thread()->endgames.probe<ScaleFactor>(key)) != nullptr)
|
if ((sf = pos.this_thread()->endgames.probe<ScaleFactor>(key)) != nullptr)
|
||||||
{
|
{
|
||||||
e->scalingFunction[sf->strong_side()] = sf; // Only strong color assigned
|
e->scalingFunction[sf->strongSide] = sf; // Only strong color assigned
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,16 +165,13 @@ Entry* probe(const Position& pos) {
|
|||||||
// case we don't return after setting the function.
|
// case we don't return after setting the function.
|
||||||
for (Color c = WHITE; c <= BLACK; ++c)
|
for (Color c = WHITE; c <= BLACK; ++c)
|
||||||
{
|
{
|
||||||
if (is_KBPsKs(pos, c))
|
if (is_KBPsK(pos, c))
|
||||||
e->scalingFunction[c] = &ScaleKBPsK[c];
|
e->scalingFunction[c] = &ScaleKBPsK[c];
|
||||||
|
|
||||||
else if (is_KQKRPs(pos, c))
|
else if (is_KQKRPs(pos, c))
|
||||||
e->scalingFunction[c] = &ScaleKQKRPs[c];
|
e->scalingFunction[c] = &ScaleKQKRPs[c];
|
||||||
}
|
}
|
||||||
|
|
||||||
Value npm_w = pos.non_pawn_material(WHITE);
|
|
||||||
Value npm_b = pos.non_pawn_material(BLACK);
|
|
||||||
|
|
||||||
if (npm_w + npm_b == VALUE_ZERO && pos.pieces(PAWN)) // Only pawns on the board
|
if (npm_w + npm_b == VALUE_ZERO && pos.pieces(PAWN)) // Only pawns on the board
|
||||||
{
|
{
|
||||||
if (!pos.count<PAWN>(BLACK))
|
if (!pos.count<PAWN>(BLACK))
|
||||||
@@ -203,22 +206,16 @@ Entry* probe(const Position& pos) {
|
|||||||
e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW :
|
e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW :
|
||||||
npm_w <= BishopValueMg ? 4 : 14);
|
npm_w <= BishopValueMg ? 4 : 14);
|
||||||
|
|
||||||
if (pos.count<PAWN>(WHITE) == 1 && npm_w - npm_b <= BishopValueMg)
|
|
||||||
e->factor[WHITE] = (uint8_t) SCALE_FACTOR_ONEPAWN;
|
|
||||||
|
|
||||||
if (pos.count<PAWN>(BLACK) == 1 && npm_b - npm_w <= BishopValueMg)
|
|
||||||
e->factor[BLACK] = (uint8_t) SCALE_FACTOR_ONEPAWN;
|
|
||||||
|
|
||||||
// Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder
|
// Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder
|
||||||
// for the bishop pair "extended piece", which allows us to be more flexible
|
// for the bishop pair "extended piece", which allows us to be more flexible
|
||||||
// in defining bishop pair bonuses.
|
// in defining bishop pair bonuses.
|
||||||
const int PieceCount[COLOR_NB][PIECE_TYPE_NB] = {
|
const int pieceCount[COLOR_NB][PIECE_TYPE_NB] = {
|
||||||
{ pos.count<BISHOP>(WHITE) > 1, pos.count<PAWN>(WHITE), pos.count<KNIGHT>(WHITE),
|
{ pos.count<BISHOP>(WHITE) > 1, pos.count<PAWN>(WHITE), pos.count<KNIGHT>(WHITE),
|
||||||
pos.count<BISHOP>(WHITE) , pos.count<ROOK>(WHITE), pos.count<QUEEN >(WHITE) },
|
pos.count<BISHOP>(WHITE) , pos.count<ROOK>(WHITE), pos.count<QUEEN >(WHITE) },
|
||||||
{ pos.count<BISHOP>(BLACK) > 1, pos.count<PAWN>(BLACK), pos.count<KNIGHT>(BLACK),
|
{ pos.count<BISHOP>(BLACK) > 1, pos.count<PAWN>(BLACK), pos.count<KNIGHT>(BLACK),
|
||||||
pos.count<BISHOP>(BLACK) , pos.count<ROOK>(BLACK), pos.count<QUEEN >(BLACK) } };
|
pos.count<BISHOP>(BLACK) , pos.count<ROOK>(BLACK), pos.count<QUEEN >(BLACK) } };
|
||||||
|
|
||||||
e->value = int16_t((imbalance<WHITE>(PieceCount) - imbalance<BLACK>(PieceCount)) / 16);
|
e->value = int16_t((imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16);
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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
|
||||||
@@ -56,11 +56,11 @@ struct Entry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Key key;
|
Key key;
|
||||||
|
const EndgameBase<Value>* evaluationFunction;
|
||||||
|
const EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB]; // Could be one for each
|
||||||
|
// side (e.g. KPKP, KBPsKs)
|
||||||
int16_t value;
|
int16_t value;
|
||||||
uint8_t factor[COLOR_NB];
|
uint8_t factor[COLOR_NB];
|
||||||
EndgameBase<Value>* evaluationFunction;
|
|
||||||
EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB]; // Could be one for each
|
|
||||||
// side (e.g. KPKP, KBPsKs)
|
|
||||||
Phase gamePhase;
|
Phase gamePhase;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+141
-6
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,10 +18,34 @@
|
|||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#if _WIN32_WINNT < 0x0601
|
||||||
|
#undef _WIN32_WINNT
|
||||||
|
#define _WIN32_WINNT 0x0601 // Force to include needed API prototypes
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef NOMINMAX
|
||||||
|
#define NOMINMAX
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
// The needed Windows API for processor groups could be missed from old Windows
|
||||||
|
// versions, so instead of calling them directly (forcing the linker to resolve
|
||||||
|
// the calls at compile time), try to load them at runtime. To do this we need
|
||||||
|
// first to define the corresponding function pointers.
|
||||||
|
extern "C" {
|
||||||
|
typedef bool(*fun1_t)(LOGICAL_PROCESSOR_RELATIONSHIP,
|
||||||
|
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD);
|
||||||
|
typedef bool(*fun2_t)(USHORT, PGROUP_AFFINITY);
|
||||||
|
typedef bool(*fun3_t)(HANDLE, CONST GROUP_AFFINITY*, PGROUP_AFFINITY);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
@@ -32,7 +56,7 @@ namespace {
|
|||||||
|
|
||||||
/// Version number. If Version is left empty, then compile date in the format
|
/// Version number. If Version is left empty, then compile date in the format
|
||||||
/// DD-MM-YY and show in engine_info.
|
/// DD-MM-YY and show in engine_info.
|
||||||
const string Version = "8";
|
const string Version = "10";
|
||||||
|
|
||||||
/// 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
|
||||||
@@ -44,10 +68,10 @@ struct Tie: public streambuf { // MSVC requires split streambuf for cin and cout
|
|||||||
|
|
||||||
Tie(streambuf* b, streambuf* l) : buf(b), logBuf(l) {}
|
Tie(streambuf* b, streambuf* l) : buf(b), logBuf(l) {}
|
||||||
|
|
||||||
int sync() { return logBuf->pubsync(), buf->pubsync(); }
|
int sync() override { return logBuf->pubsync(), buf->pubsync(); }
|
||||||
int overflow(int c) { return log(buf->sputc((char)c), "<< "); }
|
int overflow(int c) override { return log(buf->sputc((char)c), "<< "); }
|
||||||
int underflow() { return buf->sgetc(); }
|
int underflow() override { return buf->sgetc(); }
|
||||||
int uflow() { return log(buf->sbumpc(), ">> "); }
|
int uflow() override { return log(buf->sbumpc(), ">> "); }
|
||||||
|
|
||||||
streambuf *buf, *logBuf;
|
streambuf *buf, *logBuf;
|
||||||
|
|
||||||
@@ -185,3 +209,114 @@ void prefetch(void* addr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void prefetch2(void* addr) {
|
||||||
|
|
||||||
|
prefetch(addr);
|
||||||
|
prefetch((uint8_t*)addr + 64);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace WinProcGroup {
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
|
||||||
|
void bindThisThread(size_t) {}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
/// best_group() retrieves logical processor information using Windows specific
|
||||||
|
/// API and returns the best group id for the thread with index idx. Original
|
||||||
|
/// code from Texel by Peter Österlund.
|
||||||
|
|
||||||
|
int best_group(size_t idx) {
|
||||||
|
|
||||||
|
int threads = 0;
|
||||||
|
int nodes = 0;
|
||||||
|
int cores = 0;
|
||||||
|
DWORD returnLength = 0;
|
||||||
|
DWORD byteOffset = 0;
|
||||||
|
|
||||||
|
// Early exit if the needed API is not available at runtime
|
||||||
|
HMODULE k32 = GetModuleHandle("Kernel32.dll");
|
||||||
|
auto fun1 = (fun1_t)(void(*)())GetProcAddress(k32, "GetLogicalProcessorInformationEx");
|
||||||
|
if (!fun1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// First call to get returnLength. We expect it to fail due to null buffer
|
||||||
|
if (fun1(RelationAll, nullptr, &returnLength))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// Once we know returnLength, allocate the buffer
|
||||||
|
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *buffer, *ptr;
|
||||||
|
ptr = buffer = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)malloc(returnLength);
|
||||||
|
|
||||||
|
// Second call, now we expect to succeed
|
||||||
|
if (!fun1(RelationAll, buffer, &returnLength))
|
||||||
|
{
|
||||||
|
free(buffer);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (ptr->Size > 0 && byteOffset + ptr->Size <= returnLength)
|
||||||
|
{
|
||||||
|
if (ptr->Relationship == RelationNumaNode)
|
||||||
|
nodes++;
|
||||||
|
|
||||||
|
else if (ptr->Relationship == RelationProcessorCore)
|
||||||
|
{
|
||||||
|
cores++;
|
||||||
|
threads += (ptr->Processor.Flags == LTP_PC_SMT) ? 2 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
byteOffset += ptr->Size;
|
||||||
|
ptr = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)(((char*)ptr) + ptr->Size);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(buffer);
|
||||||
|
|
||||||
|
std::vector<int> groups;
|
||||||
|
|
||||||
|
// Run as many threads as possible on the same node until core limit is
|
||||||
|
// reached, then move on filling the next node.
|
||||||
|
for (int n = 0; n < nodes; n++)
|
||||||
|
for (int i = 0; i < cores / nodes; i++)
|
||||||
|
groups.push_back(n);
|
||||||
|
|
||||||
|
// In case a core has more than one logical processor (we assume 2) and we
|
||||||
|
// have still threads to allocate, then spread them evenly across available
|
||||||
|
// nodes.
|
||||||
|
for (int t = 0; t < threads - cores; t++)
|
||||||
|
groups.push_back(t % nodes);
|
||||||
|
|
||||||
|
// If we still have more threads than the total number of logical processors
|
||||||
|
// then return -1 and let the OS to decide what to do.
|
||||||
|
return idx < groups.size() ? groups[idx] : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// bindThisThread() set the group affinity of the current thread
|
||||||
|
|
||||||
|
void bindThisThread(size_t idx) {
|
||||||
|
|
||||||
|
// Use only local variables to be thread-safe
|
||||||
|
int group = best_group(idx);
|
||||||
|
|
||||||
|
if (group == -1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Early exit if the needed API are not available at runtime
|
||||||
|
HMODULE k32 = GetModuleHandle("Kernel32.dll");
|
||||||
|
auto fun2 = (fun2_t)(void(*)())GetProcAddress(k32, "GetNumaNodeProcessorMaskEx");
|
||||||
|
auto fun3 = (fun3_t)(void(*)())GetProcAddress(k32, "SetThreadGroupAffinity");
|
||||||
|
|
||||||
|
if (!fun2 || !fun3)
|
||||||
|
return;
|
||||||
|
|
||||||
|
GROUP_AFFINITY affinity;
|
||||||
|
if (fun2(group, &affinity))
|
||||||
|
fun3(GetCurrentThread(), &affinity, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} // namespace WinProcGroup
|
||||||
|
|||||||
+15
-1
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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 @@
|
|||||||
|
|
||||||
const std::string engine_info(bool to_uci = false);
|
const std::string engine_info(bool to_uci = false);
|
||||||
void prefetch(void* addr);
|
void prefetch(void* addr);
|
||||||
|
void prefetch2(void* addr);
|
||||||
void start_logger(const std::string& fname);
|
void start_logger(const std::string& fname);
|
||||||
|
|
||||||
void dbg_hit_on(bool b);
|
void dbg_hit_on(bool b);
|
||||||
@@ -40,6 +41,8 @@ void dbg_print();
|
|||||||
|
|
||||||
typedef std::chrono::milliseconds::rep TimePoint; // A value in milliseconds
|
typedef std::chrono::milliseconds::rep TimePoint; // A value in milliseconds
|
||||||
|
|
||||||
|
static_assert(sizeof(TimePoint) == sizeof(int64_t), "TimePoint should be 64 bits");
|
||||||
|
|
||||||
inline TimePoint now() {
|
inline TimePoint now() {
|
||||||
return std::chrono::duration_cast<std::chrono::milliseconds>
|
return std::chrono::duration_cast<std::chrono::milliseconds>
|
||||||
(std::chrono::steady_clock::now().time_since_epoch()).count();
|
(std::chrono::steady_clock::now().time_since_epoch()).count();
|
||||||
@@ -97,4 +100,15 @@ public:
|
|||||||
{ return T(rand64() & rand64() & rand64()); }
|
{ return T(rand64() & rand64() & rand64()); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/// Under Windows it is not possible for a process to run on more than one
|
||||||
|
/// logical processor group. This usually means to be limited to use max 64
|
||||||
|
/// cores. To overcome this, some special platform specific API should be
|
||||||
|
/// called to set group affinity for each thread. Original code from Texel by
|
||||||
|
/// Peter Österlund.
|
||||||
|
|
||||||
|
namespace WinProcGroup {
|
||||||
|
void bindThisThread(size_t idx);
|
||||||
|
}
|
||||||
|
|
||||||
#endif // #ifndef MISC_H_INCLUDED
|
#endif // #ifndef MISC_H_INCLUDED
|
||||||
|
|||||||
+43
-41
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,34 +25,35 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
template<CastlingRight Cr, bool Checks, bool Chess960>
|
template<Color Us, CastlingSide Cs, bool Checks, bool Chess960>
|
||||||
ExtMove* generate_castling(const Position& pos, ExtMove* moveList, Color us) {
|
ExtMove* generate_castling(const Position& pos, ExtMove* moveList) {
|
||||||
|
|
||||||
static const bool KingSide = (Cr == WHITE_OO || Cr == BLACK_OO);
|
constexpr CastlingRight Cr = Us | Cs;
|
||||||
|
constexpr bool KingSide = (Cr == WHITE_OO || Cr == BLACK_OO);
|
||||||
|
|
||||||
if (pos.castling_impeded(Cr) || !pos.can_castle(Cr))
|
if (pos.castling_impeded(Cr) || !pos.can_castle(Cr))
|
||||||
return moveList;
|
return moveList;
|
||||||
|
|
||||||
// After castling, the rook and king final positions are the same in Chess960
|
// After castling, the rook and king final positions are the same in Chess960
|
||||||
// as they would be in standard chess.
|
// as they would be in standard chess.
|
||||||
Square kfrom = pos.square<KING>(us);
|
Square kfrom = pos.square<KING>(Us);
|
||||||
Square rfrom = pos.castling_rook_square(Cr);
|
Square rfrom = pos.castling_rook_square(Cr);
|
||||||
Square kto = relative_square(us, KingSide ? SQ_G1 : SQ_C1);
|
Square kto = relative_square(Us, KingSide ? SQ_G1 : SQ_C1);
|
||||||
Bitboard enemies = pos.pieces(~us);
|
Bitboard enemies = pos.pieces(~Us);
|
||||||
|
|
||||||
assert(!pos.checkers());
|
assert(!pos.checkers());
|
||||||
|
|
||||||
const Square K = Chess960 ? kto > kfrom ? WEST : EAST
|
const Direction step = Chess960 ? kto > kfrom ? WEST : EAST
|
||||||
: KingSide ? WEST : EAST;
|
: KingSide ? WEST : EAST;
|
||||||
|
|
||||||
for (Square s = kto; s != kfrom; s += K)
|
for (Square s = kto; s != kfrom; s += step)
|
||||||
if (pos.attackers_to(s) & enemies)
|
if (pos.attackers_to(s) & enemies)
|
||||||
return moveList;
|
return moveList;
|
||||||
|
|
||||||
// Because we generate only legal castling moves we need to verify that
|
// Because we generate only legal castling moves we need to verify that
|
||||||
// when moving the castling rook we do not discover some hidden checker.
|
// when moving the castling rook we do not discover some hidden checker.
|
||||||
// For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
|
// For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
|
||||||
if (Chess960 && (attacks_bb<ROOK>(kto, pos.pieces() ^ rfrom) & pos.pieces(~us, ROOK, QUEEN)))
|
if (Chess960 && (attacks_bb<ROOK>(kto, pos.pieces() ^ rfrom) & pos.pieces(~Us, ROOK, QUEEN)))
|
||||||
return moveList;
|
return moveList;
|
||||||
|
|
||||||
Move m = make<CASTLING>(kfrom, rfrom);
|
Move m = make<CASTLING>(kfrom, rfrom);
|
||||||
@@ -65,7 +66,7 @@ namespace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<GenType Type, Square D>
|
template<GenType Type, Direction D>
|
||||||
ExtMove* make_promotions(ExtMove* moveList, Square to, Square ksq) {
|
ExtMove* make_promotions(ExtMove* moveList, Square to, Square ksq) {
|
||||||
|
|
||||||
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
||||||
@@ -80,7 +81,7 @@ namespace {
|
|||||||
|
|
||||||
// Knight promotion is the only promotion that can give a direct check
|
// Knight promotion is the only promotion that can give a direct check
|
||||||
// that's not already included in the queen promotion.
|
// that's not already included in the queen promotion.
|
||||||
if (Type == QUIET_CHECKS && (StepAttacksBB[W_KNIGHT][to] & ksq))
|
if (Type == QUIET_CHECKS && (PseudoAttacks[KNIGHT][to] & ksq))
|
||||||
*moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
|
*moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
|
||||||
else
|
else
|
||||||
(void)ksq; // Silence a warning under MSVC
|
(void)ksq; // Silence a warning under MSVC
|
||||||
@@ -94,13 +95,13 @@ namespace {
|
|||||||
|
|
||||||
// Compute our parametrized parameters at compile time, named according to
|
// Compute our parametrized parameters at compile time, named according to
|
||||||
// the point of view of white side.
|
// the point of view of white side.
|
||||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||||
const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
|
constexpr Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
|
||||||
const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
|
constexpr Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
|
||||||
const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
|
constexpr Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
|
||||||
const Square Up = (Us == WHITE ? NORTH : SOUTH);
|
constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH);
|
||||||
const Square Right = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
|
constexpr Direction UpRight = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
|
||||||
const Square Left = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
|
constexpr Direction UpLeft = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
|
||||||
|
|
||||||
Bitboard emptySquares;
|
Bitboard emptySquares;
|
||||||
|
|
||||||
@@ -135,7 +136,7 @@ namespace {
|
|||||||
// if the pawn is not on the same file as the enemy king, because we
|
// if the pawn is not on the same file as the enemy king, because we
|
||||||
// don't generate captures. Note that a possible discovery check
|
// don't generate captures. Note that a possible discovery check
|
||||||
// promotion has been already generated amongst the captures.
|
// promotion has been already generated amongst the captures.
|
||||||
Bitboard dcCandidates = pos.discovered_check_candidates();
|
Bitboard dcCandidates = pos.blockers_for_king(Them);
|
||||||
if (pawnsNotOn7 & dcCandidates)
|
if (pawnsNotOn7 & dcCandidates)
|
||||||
{
|
{
|
||||||
Bitboard dc1 = shift<Up>(pawnsNotOn7 & dcCandidates) & emptySquares & ~file_bb(ksq);
|
Bitboard dc1 = shift<Up>(pawnsNotOn7 & dcCandidates) & emptySquares & ~file_bb(ksq);
|
||||||
@@ -168,38 +169,38 @@ namespace {
|
|||||||
if (Type == EVASIONS)
|
if (Type == EVASIONS)
|
||||||
emptySquares &= target;
|
emptySquares &= target;
|
||||||
|
|
||||||
Bitboard b1 = shift<Right>(pawnsOn7) & enemies;
|
Bitboard b1 = shift<UpRight>(pawnsOn7) & enemies;
|
||||||
Bitboard b2 = shift<Left >(pawnsOn7) & enemies;
|
Bitboard b2 = shift<UpLeft >(pawnsOn7) & enemies;
|
||||||
Bitboard b3 = shift<Up >(pawnsOn7) & emptySquares;
|
Bitboard b3 = shift<Up >(pawnsOn7) & emptySquares;
|
||||||
|
|
||||||
Square ksq = pos.square<KING>(Them);
|
Square ksq = pos.square<KING>(Them);
|
||||||
|
|
||||||
while (b1)
|
while (b1)
|
||||||
moveList = make_promotions<Type, Right>(moveList, pop_lsb(&b1), ksq);
|
moveList = make_promotions<Type, UpRight>(moveList, pop_lsb(&b1), ksq);
|
||||||
|
|
||||||
while (b2)
|
while (b2)
|
||||||
moveList = make_promotions<Type, Left >(moveList, pop_lsb(&b2), ksq);
|
moveList = make_promotions<Type, UpLeft >(moveList, pop_lsb(&b2), ksq);
|
||||||
|
|
||||||
while (b3)
|
while (b3)
|
||||||
moveList = make_promotions<Type, Up >(moveList, pop_lsb(&b3), ksq);
|
moveList = make_promotions<Type, Up >(moveList, pop_lsb(&b3), ksq);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Standard and en-passant captures
|
// Standard and en-passant captures
|
||||||
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
||||||
{
|
{
|
||||||
Bitboard b1 = shift<Right>(pawnsNotOn7) & enemies;
|
Bitboard b1 = shift<UpRight>(pawnsNotOn7) & enemies;
|
||||||
Bitboard b2 = shift<Left >(pawnsNotOn7) & enemies;
|
Bitboard b2 = shift<UpLeft >(pawnsNotOn7) & enemies;
|
||||||
|
|
||||||
while (b1)
|
while (b1)
|
||||||
{
|
{
|
||||||
Square to = pop_lsb(&b1);
|
Square to = pop_lsb(&b1);
|
||||||
*moveList++ = make_move(to - Right, 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 - Left, to);
|
*moveList++ = make_move(to - UpLeft, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pos.ep_square() != SQ_NONE)
|
if (pos.ep_square() != SQ_NONE)
|
||||||
@@ -241,7 +242,7 @@ namespace {
|
|||||||
&& !(PseudoAttacks[Pt][from] & target & pos.check_squares(Pt)))
|
&& !(PseudoAttacks[Pt][from] & target & pos.check_squares(Pt)))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (pos.discovered_check_candidates() & from)
|
if (pos.blockers_for_king(~us) & from)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,7 +262,7 @@ namespace {
|
|||||||
template<Color Us, GenType Type>
|
template<Color Us, GenType Type>
|
||||||
ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target) {
|
ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target) {
|
||||||
|
|
||||||
const bool Checks = Type == QUIET_CHECKS;
|
constexpr bool Checks = Type == QUIET_CHECKS;
|
||||||
|
|
||||||
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, Us, target);
|
moveList = generate_moves<KNIGHT, Checks>(pos, moveList, Us, target);
|
||||||
@@ -281,13 +282,13 @@ namespace {
|
|||||||
{
|
{
|
||||||
if (pos.is_chess960())
|
if (pos.is_chess960())
|
||||||
{
|
{
|
||||||
moveList = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, true>(pos, moveList, Us);
|
moveList = generate_castling<Us, KING_SIDE, Checks, true>(pos, moveList);
|
||||||
moveList = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, true>(pos, moveList, Us);
|
moveList = generate_castling<Us, QUEEN_SIDE, Checks, true>(pos, moveList);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
moveList = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, false>(pos, moveList, Us);
|
moveList = generate_castling<Us, KING_SIDE, Checks, false>(pos, moveList);
|
||||||
moveList = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, false>(pos, moveList, Us);
|
moveList = generate_castling<Us, QUEEN_SIDE, Checks, false>(pos, moveList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -336,7 +337,7 @@ ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
|
|||||||
assert(!pos.checkers());
|
assert(!pos.checkers());
|
||||||
|
|
||||||
Color us = pos.side_to_move();
|
Color us = pos.side_to_move();
|
||||||
Bitboard dc = pos.discovered_check_candidates();
|
Bitboard dc = pos.blockers_for_king(~us) & pos.pieces(us);
|
||||||
|
|
||||||
while (dc)
|
while (dc)
|
||||||
{
|
{
|
||||||
@@ -346,7 +347,7 @@ ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* moveList) {
|
|||||||
if (pt == PAWN)
|
if (pt == PAWN)
|
||||||
continue; // Will be generated together with direct checks
|
continue; // Will be generated together with direct checks
|
||||||
|
|
||||||
Bitboard b = pos.attacks_from(Piece(pt), from) & ~pos.pieces();
|
Bitboard b = pos.attacks_from(pt, from) & ~pos.pieces();
|
||||||
|
|
||||||
if (pt == KING)
|
if (pt == KING)
|
||||||
b &= ~PseudoAttacks[QUEEN][pos.square<KING>(~us)];
|
b &= ~PseudoAttacks[QUEEN][pos.square<KING>(~us)];
|
||||||
@@ -403,8 +404,9 @@ ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
|
|||||||
template<>
|
template<>
|
||||||
ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
|
ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
|
||||||
|
|
||||||
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
|
Color us = pos.side_to_move();
|
||||||
Square ksq = pos.square<KING>(pos.side_to_move());
|
Bitboard pinned = pos.blockers_for_king(us) & pos.pieces(us);
|
||||||
|
Square ksq = pos.square<KING>(us);
|
||||||
ExtMove* cur = moveList;
|
ExtMove* cur = moveList;
|
||||||
|
|
||||||
moveList = pos.checkers() ? generate<EVASIONS >(pos, moveList)
|
moveList = pos.checkers() ? generate<EVASIONS >(pos, moveList)
|
||||||
|
|||||||
+9
-4
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,6 +21,8 @@
|
|||||||
#ifndef MOVEGEN_H_INCLUDED
|
#ifndef MOVEGEN_H_INCLUDED
|
||||||
#define MOVEGEN_H_INCLUDED
|
#define MOVEGEN_H_INCLUDED
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
class Position;
|
class Position;
|
||||||
@@ -36,10 +38,14 @@ enum GenType {
|
|||||||
|
|
||||||
struct ExtMove {
|
struct ExtMove {
|
||||||
Move move;
|
Move move;
|
||||||
Value value;
|
int value;
|
||||||
|
|
||||||
operator Move() const { return move; }
|
operator Move() const { return move; }
|
||||||
void operator=(Move m) { move = m; }
|
void operator=(Move m) { move = m; }
|
||||||
|
|
||||||
|
// Inhibit unwanted implicit conversions to Move
|
||||||
|
// with an ambiguity that yields to a compile error.
|
||||||
|
operator float() const = delete;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool operator<(const ExtMove& f, const ExtMove& s) {
|
inline bool operator<(const ExtMove& f, const ExtMove& s) {
|
||||||
@@ -59,8 +65,7 @@ struct MoveList {
|
|||||||
const ExtMove* end() const { return last; }
|
const ExtMove* end() const { return last; }
|
||||||
size_t size() const { return last - moveList; }
|
size_t size() const { return last - moveList; }
|
||||||
bool contains(Move move) const {
|
bool contains(Move move) const {
|
||||||
for (const auto& m : *this) if (m == move) return true;
|
return std::find(begin(), end(), move) != end();
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
+156
-237
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,40 +21,32 @@
|
|||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
#include "movepick.h"
|
#include "movepick.h"
|
||||||
#include "thread.h"
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
enum Stages {
|
enum Stages {
|
||||||
MAIN_SEARCH, CAPTURES_INIT, GOOD_CAPTURES, KILLERS, COUNTERMOVE, QUIET_INIT, QUIET, BAD_CAPTURES,
|
MAIN_TT, CAPTURE_INIT, GOOD_CAPTURE, REFUTATION, QUIET_INIT, QUIET, BAD_CAPTURE,
|
||||||
EVASION, EVASIONS_INIT, ALL_EVASIONS,
|
EVASION_TT, EVASION_INIT, EVASION,
|
||||||
PROBCUT, PROBCUT_INIT, PROBCUT_CAPTURES,
|
PROBCUT_TT, PROBCUT_INIT, PROBCUT,
|
||||||
QSEARCH_WITH_CHECKS, QCAPTURES_1_INIT, QCAPTURES_1, QCHECKS,
|
QSEARCH_TT, QCAPTURE_INIT, QCAPTURE, QCHECK_INIT, QCHECK
|
||||||
QSEARCH_NO_CHECKS, QCAPTURES_2_INIT, QCAPTURES_2,
|
|
||||||
QSEARCH_RECAPTURES, QRECAPTURES
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Our insertion sort, which is guaranteed to be stable, as it should be
|
// Helper filter used with select()
|
||||||
void insertion_sort(ExtMove* begin, ExtMove* end)
|
const auto Any = [](){ return true; };
|
||||||
{
|
|
||||||
ExtMove tmp, *p, *q;
|
|
||||||
|
|
||||||
for (p = begin + 1; p < end; ++p)
|
// partial_insertion_sort() sorts moves in descending order up to and including
|
||||||
{
|
// a given limit. The order of moves smaller than the limit is left unspecified.
|
||||||
tmp = *p;
|
void partial_insertion_sort(ExtMove* begin, ExtMove* end, int limit) {
|
||||||
for (q = p; q != begin && *(q-1) < tmp; --q)
|
|
||||||
*q = *(q-1);
|
|
||||||
*q = tmp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// pick_best() finds the best move in the range (begin, end) and moves it to
|
for (ExtMove *sortedEnd = begin, *p = begin + 1; p < end; ++p)
|
||||||
// the front. It's faster than sorting all the moves in advance when there
|
if (p->value >= limit)
|
||||||
// are few moves, e.g., the possible captures.
|
{
|
||||||
Move pick_best(ExtMove* begin, ExtMove* end)
|
ExtMove tmp = *p, *q;
|
||||||
{
|
*p = *++sortedEnd;
|
||||||
std::swap(*begin, *std::max_element(begin, end));
|
for (q = sortedEnd; q != begin && *(q - 1) < tmp; --q)
|
||||||
return *begin;
|
*q = *(q - 1);
|
||||||
|
*q = tmp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -66,285 +58,212 @@ namespace {
|
|||||||
/// search captures, promotions, and some checks) and how important good move
|
/// search captures, promotions, and some checks) and how important good move
|
||||||
/// ordering is at the current node.
|
/// ordering is at the current node.
|
||||||
|
|
||||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, Search::Stack* s)
|
/// MovePicker constructor for the main search
|
||||||
: pos(p), ss(s), depth(d) {
|
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
|
||||||
|
const CapturePieceToHistory* cph, const PieceToHistory** ch, Move cm, Move* killers)
|
||||||
|
: pos(p), mainHistory(mh), captureHistory(cph), continuationHistory(ch),
|
||||||
|
refutations{{killers[0], 0}, {killers[1], 0}, {cm, 0}}, depth(d) {
|
||||||
|
|
||||||
assert(d > DEPTH_ZERO);
|
assert(d > DEPTH_ZERO);
|
||||||
|
|
||||||
Square prevSq = to_sq((ss-1)->currentMove);
|
stage = pos.checkers() ? EVASION_TT : MAIN_TT;
|
||||||
countermove = pos.this_thread()->counterMoves[pos.piece_on(prevSq)][prevSq];
|
|
||||||
|
|
||||||
stage = pos.checkers() ? EVASION : MAIN_SEARCH;
|
|
||||||
ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
|
ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
|
||||||
stage += (ttMove == MOVE_NONE);
|
stage += (ttMove == MOVE_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, Square s)
|
/// MovePicker constructor for quiescence search
|
||||||
: pos(p) {
|
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh,
|
||||||
|
const CapturePieceToHistory* cph, const PieceToHistory** ch, Square rs)
|
||||||
|
: pos(p), mainHistory(mh), captureHistory(cph), continuationHistory(ch), recaptureSquare(rs), depth(d) {
|
||||||
|
|
||||||
assert(d <= DEPTH_ZERO);
|
assert(d <= DEPTH_ZERO);
|
||||||
|
|
||||||
if (pos.checkers())
|
stage = pos.checkers() ? EVASION_TT : QSEARCH_TT;
|
||||||
stage = EVASION;
|
ttMove = ttm
|
||||||
|
&& pos.pseudo_legal(ttm)
|
||||||
else if (d > DEPTH_QS_NO_CHECKS)
|
&& (depth > DEPTH_QS_RECAPTURES || to_sq(ttm) == recaptureSquare) ? ttm : MOVE_NONE;
|
||||||
stage = QSEARCH_WITH_CHECKS;
|
|
||||||
|
|
||||||
else if (d > DEPTH_QS_RECAPTURES)
|
|
||||||
stage = QSEARCH_NO_CHECKS;
|
|
||||||
|
|
||||||
else
|
|
||||||
{
|
|
||||||
stage = QSEARCH_RECAPTURES;
|
|
||||||
recaptureSquare = s;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
|
|
||||||
stage += (ttMove == MOVE_NONE);
|
stage += (ttMove == MOVE_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
MovePicker::MovePicker(const Position& p, Move ttm, Value th)
|
/// MovePicker constructor for ProbCut: we generate captures with SEE greater
|
||||||
: pos(p), threshold(th) {
|
/// than or equal to the given threshold.
|
||||||
|
MovePicker::MovePicker(const Position& p, Move ttm, Value th, const CapturePieceToHistory* cph)
|
||||||
|
: pos(p), captureHistory(cph), threshold(th) {
|
||||||
|
|
||||||
assert(!pos.checkers());
|
assert(!pos.checkers());
|
||||||
|
|
||||||
stage = PROBCUT;
|
stage = PROBCUT_TT;
|
||||||
|
|
||||||
// In ProbCut we generate captures with SEE higher than the given threshold
|
|
||||||
ttMove = ttm
|
ttMove = ttm
|
||||||
&& pos.pseudo_legal(ttm)
|
&& pos.pseudo_legal(ttm)
|
||||||
&& pos.capture(ttm)
|
&& pos.capture(ttm)
|
||||||
&& pos.see_ge(ttm, threshold + 1)? ttm : MOVE_NONE;
|
&& pos.see_ge(ttm, threshold) ? ttm : MOVE_NONE;
|
||||||
|
|
||||||
stage += (ttMove == MOVE_NONE);
|
stage += (ttMove == MOVE_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// MovePicker::score() assigns a numerical value to each move in a list, used
|
||||||
|
/// for sorting. Captures are ordered by Most Valuable Victim (MVV), preferring
|
||||||
|
/// captures with a good history. Quiets moves are ordered using the histories.
|
||||||
|
template<GenType Type>
|
||||||
|
void MovePicker::score() {
|
||||||
|
|
||||||
/// score() assigns a numerical value to each move in a move list. The moves with
|
static_assert(Type == CAPTURES || Type == QUIETS || Type == EVASIONS, "Wrong type");
|
||||||
/// highest values will be picked first.
|
|
||||||
template<>
|
|
||||||
void MovePicker::score<CAPTURES>() {
|
|
||||||
// Winning and equal captures in the main search are ordered by MVV, preferring
|
|
||||||
// captures near our home rank. Surprisingly, this appears to perform slightly
|
|
||||||
// better than SEE-based move ordering: exchanging big pieces before capturing
|
|
||||||
// a hanging piece probably helps to reduce the subtree size.
|
|
||||||
// In the main search we want to push captures with negative SEE values to the
|
|
||||||
// badCaptures[] array, but instead of doing it now we delay until the move
|
|
||||||
// has been picked up, saving some SEE calls in case we get a cutoff.
|
|
||||||
for (auto& m : *this)
|
|
||||||
m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
|
||||||
- Value(200 * relative_rank(pos.side_to_move(), to_sq(m)));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<>
|
|
||||||
void MovePicker::score<QUIETS>() {
|
|
||||||
|
|
||||||
const HistoryStats& history = pos.this_thread()->history;
|
|
||||||
const FromToStats& fromTo = pos.this_thread()->fromTo;
|
|
||||||
|
|
||||||
const CounterMoveStats* cm = (ss-1)->counterMoves;
|
|
||||||
const CounterMoveStats* fm = (ss-2)->counterMoves;
|
|
||||||
const CounterMoveStats* f2 = (ss-4)->counterMoves;
|
|
||||||
|
|
||||||
Color c = pos.side_to_move();
|
|
||||||
|
|
||||||
for (auto& m : *this)
|
for (auto& m : *this)
|
||||||
m.value = history[pos.moved_piece(m)][to_sq(m)]
|
if (Type == CAPTURES)
|
||||||
+ (cm ? (*cm)[pos.moved_piece(m)][to_sq(m)] : VALUE_ZERO)
|
|
||||||
+ (fm ? (*fm)[pos.moved_piece(m)][to_sq(m)] : VALUE_ZERO)
|
|
||||||
+ (f2 ? (*f2)[pos.moved_piece(m)][to_sq(m)] : VALUE_ZERO)
|
|
||||||
+ fromTo.get(c, m);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<>
|
|
||||||
void MovePicker::score<EVASIONS>() {
|
|
||||||
// Try captures ordered by MVV/LVA, then non-captures ordered by history value
|
|
||||||
const HistoryStats& history = pos.this_thread()->history;
|
|
||||||
const FromToStats& fromTo = pos.this_thread()->fromTo;
|
|
||||||
Color c = pos.side_to_move();
|
|
||||||
|
|
||||||
for (auto& m : *this)
|
|
||||||
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))) + HistoryStats::Max;
|
+ (*captureHistory)[pos.moved_piece(m)][to_sq(m)][type_of(pos.piece_on(to_sq(m)))] / 8;
|
||||||
else
|
|
||||||
m.value = history[pos.moved_piece(m)][to_sq(m)] + fromTo.get(c, m);
|
else if (Type == QUIETS)
|
||||||
|
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
|
||||||
|
+ (*continuationHistory[0])[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)];
|
||||||
|
|
||||||
|
else // Type == EVASIONS
|
||||||
|
{
|
||||||
|
if (pos.capture(m))
|
||||||
|
m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||||
|
- Value(type_of(pos.moved_piece(m)));
|
||||||
|
else
|
||||||
|
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
|
||||||
|
+ (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
|
||||||
|
- (1 << 28);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// MovePicker::select() returns the next move satisfying a predicate function.
|
||||||
|
/// It never returns the TT move.
|
||||||
|
template<MovePicker::PickType T, typename Pred>
|
||||||
|
Move MovePicker::select(Pred filter) {
|
||||||
|
|
||||||
/// next_move() is the most important method of the MovePicker class. It returns
|
while (cur < endMoves)
|
||||||
/// a new pseudo legal move every time it is called, until there are no more moves
|
{
|
||||||
/// left. It picks the move with the biggest value from a list of generated moves
|
if (T == Best)
|
||||||
/// taking care not to return the ttMove if it has already been searched.
|
std::swap(*cur, *std::max_element(cur, endMoves));
|
||||||
|
|
||||||
Move MovePicker::next_move() {
|
move = *cur++;
|
||||||
|
|
||||||
Move move;
|
if (move != ttMove && filter())
|
||||||
|
return move;
|
||||||
|
}
|
||||||
|
return move = MOVE_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// MovePicker::next_move() is the most important method of the MovePicker class. It
|
||||||
|
/// returns a new pseudo legal move every time it is called until there are no more
|
||||||
|
/// moves left, picking the move with the highest score from a list of generated moves.
|
||||||
|
Move MovePicker::next_move(bool skipQuiets) {
|
||||||
|
|
||||||
|
top:
|
||||||
switch (stage) {
|
switch (stage) {
|
||||||
|
|
||||||
case MAIN_SEARCH: case EVASION: case QSEARCH_WITH_CHECKS:
|
case MAIN_TT:
|
||||||
case QSEARCH_NO_CHECKS: case PROBCUT:
|
case EVASION_TT:
|
||||||
|
case QSEARCH_TT:
|
||||||
|
case PROBCUT_TT:
|
||||||
++stage;
|
++stage;
|
||||||
return ttMove;
|
return ttMove;
|
||||||
|
|
||||||
case CAPTURES_INIT:
|
case CAPTURE_INIT:
|
||||||
endBadCaptures = cur = moves;
|
case PROBCUT_INIT:
|
||||||
|
case QCAPTURE_INIT:
|
||||||
|
cur = endBadCaptures = moves;
|
||||||
endMoves = generate<CAPTURES>(pos, cur);
|
endMoves = generate<CAPTURES>(pos, cur);
|
||||||
|
|
||||||
score<CAPTURES>();
|
score<CAPTURES>();
|
||||||
++stage;
|
++stage;
|
||||||
|
goto top;
|
||||||
|
|
||||||
case GOOD_CAPTURES:
|
case GOOD_CAPTURE:
|
||||||
while (cur < endMoves)
|
if (select<Best>([&](){
|
||||||
{
|
return pos.see_ge(move, Value(-55 * (cur-1)->value / 1024)) ?
|
||||||
move = pick_best(cur++, endMoves);
|
// Move losing capture to endBadCaptures to be tried later
|
||||||
if (move != ttMove)
|
true : (*endBadCaptures++ = move, false); }))
|
||||||
{
|
|
||||||
if (pos.see_ge(move, VALUE_ZERO))
|
|
||||||
return move;
|
|
||||||
|
|
||||||
// Losing capture, move it to the beginning of the array
|
|
||||||
*endBadCaptures++ = move;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
++stage;
|
|
||||||
move = ss->killers[0]; // First killer move
|
|
||||||
if ( move != MOVE_NONE
|
|
||||||
&& move != ttMove
|
|
||||||
&& pos.pseudo_legal(move)
|
|
||||||
&& !pos.capture(move))
|
|
||||||
return move;
|
return move;
|
||||||
|
|
||||||
case KILLERS:
|
// Prepare the pointers to loop over the refutations array
|
||||||
++stage;
|
cur = std::begin(refutations);
|
||||||
move = ss->killers[1]; // Second killer move
|
endMoves = std::end(refutations);
|
||||||
if ( move != MOVE_NONE
|
|
||||||
&& move != ttMove
|
// If the countermove is the same as a killer, skip it
|
||||||
&& pos.pseudo_legal(move)
|
if ( refutations[0].move == refutations[2].move
|
||||||
&& !pos.capture(move))
|
|| refutations[1].move == refutations[2].move)
|
||||||
return move;
|
--endMoves;
|
||||||
|
|
||||||
case COUNTERMOVE:
|
|
||||||
++stage;
|
++stage;
|
||||||
move = countermove;
|
/* fallthrough */
|
||||||
if ( move != MOVE_NONE
|
|
||||||
&& move != ttMove
|
case REFUTATION:
|
||||||
&& move != ss->killers[0]
|
if (select<Next>([&](){ return move != MOVE_NONE
|
||||||
&& move != ss->killers[1]
|
&& !pos.capture(move)
|
||||||
&& pos.pseudo_legal(move)
|
&& pos.pseudo_legal(move); }))
|
||||||
&& !pos.capture(move))
|
|
||||||
return move;
|
return move;
|
||||||
|
++stage;
|
||||||
|
/* fallthrough */
|
||||||
|
|
||||||
case QUIET_INIT:
|
case QUIET_INIT:
|
||||||
cur = endBadCaptures;
|
cur = endBadCaptures;
|
||||||
endMoves = generate<QUIETS>(pos, cur);
|
endMoves = generate<QUIETS>(pos, cur);
|
||||||
|
|
||||||
score<QUIETS>();
|
score<QUIETS>();
|
||||||
if (depth < 3 * ONE_PLY)
|
partial_insertion_sort(cur, endMoves, -4000 * depth / ONE_PLY);
|
||||||
{
|
|
||||||
ExtMove* goodQuiet = std::partition(cur, endMoves, [](const ExtMove& m)
|
|
||||||
{ return m.value > VALUE_ZERO; });
|
|
||||||
insertion_sort(cur, goodQuiet);
|
|
||||||
} else
|
|
||||||
insertion_sort(cur, endMoves);
|
|
||||||
++stage;
|
++stage;
|
||||||
|
/* fallthrough */
|
||||||
|
|
||||||
case QUIET:
|
case QUIET:
|
||||||
while (cur < endMoves)
|
if ( !skipQuiets
|
||||||
{
|
&& select<Next>([&](){return move != refutations[0]
|
||||||
move = *cur++;
|
&& move != refutations[1]
|
||||||
if ( move != ttMove
|
&& move != refutations[2];}))
|
||||||
&& move != ss->killers[0]
|
return move;
|
||||||
&& move != ss->killers[1]
|
|
||||||
&& move != countermove)
|
// Prepare the pointers to loop over the bad captures
|
||||||
return move;
|
cur = moves;
|
||||||
}
|
endMoves = endBadCaptures;
|
||||||
|
|
||||||
++stage;
|
++stage;
|
||||||
cur = moves; // Point to beginning of bad captures
|
/* fallthrough */
|
||||||
|
|
||||||
case BAD_CAPTURES:
|
case BAD_CAPTURE:
|
||||||
if (cur < endBadCaptures)
|
return select<Next>(Any);
|
||||||
return *cur++;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case EVASIONS_INIT:
|
case EVASION_INIT:
|
||||||
cur = moves;
|
cur = moves;
|
||||||
endMoves = generate<EVASIONS>(pos, cur);
|
endMoves = generate<EVASIONS>(pos, cur);
|
||||||
|
|
||||||
score<EVASIONS>();
|
score<EVASIONS>();
|
||||||
++stage;
|
++stage;
|
||||||
|
/* fallthrough */
|
||||||
|
|
||||||
case ALL_EVASIONS:
|
case EVASION:
|
||||||
while (cur < endMoves)
|
return select<Best>(Any);
|
||||||
{
|
|
||||||
move = pick_best(cur++, endMoves);
|
case PROBCUT:
|
||||||
if (move != ttMove)
|
return select<Best>([&](){ return pos.see_ge(move, threshold); });
|
||||||
return move;
|
|
||||||
}
|
case QCAPTURE:
|
||||||
break;
|
if (select<Best>([&](){ return depth > DEPTH_QS_RECAPTURES
|
||||||
|
|| to_sq(move) == recaptureSquare; }))
|
||||||
|
return move;
|
||||||
|
|
||||||
|
// If we did not find any move and we do not try checks, we have finished
|
||||||
|
if (depth != DEPTH_QS_CHECKS)
|
||||||
|
return MOVE_NONE;
|
||||||
|
|
||||||
case PROBCUT_INIT:
|
|
||||||
cur = moves;
|
|
||||||
endMoves = generate<CAPTURES>(pos, cur);
|
|
||||||
score<CAPTURES>();
|
|
||||||
++stage;
|
++stage;
|
||||||
|
/* fallthrough */
|
||||||
|
|
||||||
case PROBCUT_CAPTURES:
|
case QCHECK_INIT:
|
||||||
while (cur < endMoves)
|
|
||||||
{
|
|
||||||
move = pick_best(cur++, endMoves);
|
|
||||||
if ( move != ttMove
|
|
||||||
&& pos.see_ge(move, threshold + 1))
|
|
||||||
return move;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case QCAPTURES_1_INIT: case QCAPTURES_2_INIT:
|
|
||||||
cur = moves;
|
|
||||||
endMoves = generate<CAPTURES>(pos, cur);
|
|
||||||
score<CAPTURES>();
|
|
||||||
++stage;
|
|
||||||
|
|
||||||
case QCAPTURES_1: case QCAPTURES_2:
|
|
||||||
while (cur < endMoves)
|
|
||||||
{
|
|
||||||
move = pick_best(cur++, endMoves);
|
|
||||||
if (move != ttMove)
|
|
||||||
return move;
|
|
||||||
}
|
|
||||||
if (stage == QCAPTURES_2)
|
|
||||||
break;
|
|
||||||
cur = moves;
|
cur = moves;
|
||||||
endMoves = generate<QUIET_CHECKS>(pos, cur);
|
endMoves = generate<QUIET_CHECKS>(pos, cur);
|
||||||
|
|
||||||
++stage;
|
++stage;
|
||||||
|
/* fallthrough */
|
||||||
|
|
||||||
case QCHECKS:
|
case QCHECK:
|
||||||
while (cur < endMoves)
|
return select<Next>(Any);
|
||||||
{
|
|
||||||
move = cur++->move;
|
|
||||||
if (move != ttMove)
|
|
||||||
return move;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case QSEARCH_RECAPTURES:
|
|
||||||
cur = moves;
|
|
||||||
endMoves = generate<CAPTURES>(pos, cur);
|
|
||||||
score<CAPTURES>();
|
|
||||||
++stage;
|
|
||||||
|
|
||||||
case QRECAPTURES:
|
|
||||||
while (cur < endMoves)
|
|
||||||
{
|
|
||||||
move = pick_best(cur++, endMoves);
|
|
||||||
if (to_sq(move) == recaptureSquare)
|
|
||||||
return move;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
assert(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return MOVE_NONE;
|
assert(false);
|
||||||
|
return MOVE_NONE; // Silence warning
|
||||||
}
|
}
|
||||||
|
|||||||
+86
-57
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,68 +21,88 @@
|
|||||||
#ifndef MOVEPICK_H_INCLUDED
|
#ifndef MOVEPICK_H_INCLUDED
|
||||||
#define MOVEPICK_H_INCLUDED
|
#define MOVEPICK_H_INCLUDED
|
||||||
|
|
||||||
#include <algorithm> // For std::max
|
#include <array>
|
||||||
#include <cstring> // For std::memset
|
#include <limits>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
/// to directly call history update operator<<() on the entry so to use stats
|
||||||
|
/// tables at caller sites as simple multi-dim arrays.
|
||||||
|
template<typename T, int D>
|
||||||
|
class StatsEntry {
|
||||||
|
|
||||||
/// The Stats struct stores moves statistics. According to the template parameter
|
T entry;
|
||||||
/// the class can store History and Countermoves. History records how often
|
|
||||||
/// different moves have been successful or unsuccessful during the current search
|
|
||||||
/// and is used for reduction and move ordering decisions.
|
|
||||||
/// Countermoves store the move that refute a previous one. Entries are stored
|
|
||||||
/// using only the moving piece and destination square, hence two moves with
|
|
||||||
/// different origin but same destination and piece will be considered identical.
|
|
||||||
template<typename T, bool CM = false>
|
|
||||||
struct Stats {
|
|
||||||
|
|
||||||
static const Value Max = Value(1 << 28);
|
public:
|
||||||
|
void operator=(const T& v) { entry = v; }
|
||||||
|
T* operator&() { return &entry; }
|
||||||
|
T* operator->() { return &entry; }
|
||||||
|
operator const T&() const { return entry; }
|
||||||
|
|
||||||
const T* operator[](Piece pc) const { return table[pc]; }
|
void operator<<(int bonus) {
|
||||||
T* operator[](Piece pc) { return table[pc]; }
|
assert(abs(bonus) <= D); // Ensure range is [-D, D]
|
||||||
void clear() { std::memset(table, 0, sizeof(table)); }
|
static_assert(D <= std::numeric_limits<T>::max(), "D overflows T");
|
||||||
void update(Piece pc, Square to, Move m) { table[pc][to] = m; }
|
|
||||||
void update(Piece pc, Square to, Value v) {
|
|
||||||
|
|
||||||
if (abs(int(v)) >= 324)
|
entry += bonus - entry * abs(bonus) / D;
|
||||||
return;
|
|
||||||
|
|
||||||
table[pc][to] -= table[pc][to] * abs(int(v)) / (CM ? 936 : 324);
|
assert(abs(entry) <= D);
|
||||||
table[pc][to] += int(v) * 32;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
T table[PIECE_NB][SQUARE_NB];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef Stats<Move> MoveStats;
|
/// Stats is a generic N-dimensional array used to store various statistics.
|
||||||
typedef Stats<Value, false> HistoryStats;
|
/// The first template parameter T is the base type of the array, the second
|
||||||
typedef Stats<Value, true> CounterMoveStats;
|
/// template parameter D limits the range of updates in [-D, D] when we update
|
||||||
typedef Stats<CounterMoveStats> CounterMoveHistoryStats;
|
/// values with the << operator, while the last parameters (Size and Sizes)
|
||||||
|
/// encode the dimensions of the array.
|
||||||
|
template <typename T, int D, int Size, int... Sizes>
|
||||||
|
struct Stats : public std::array<Stats<T, D, Sizes...>, Size>
|
||||||
|
{
|
||||||
|
typedef Stats<T, D, Size, Sizes...> stats;
|
||||||
|
|
||||||
struct FromToStats {
|
void fill(const T& v) {
|
||||||
|
|
||||||
Value get(Color c, Move m) const { return table[c][from_sq(m)][to_sq(m)]; }
|
// For standard-layout 'this' points to first struct member
|
||||||
void clear() { std::memset(table, 0, sizeof(table)); }
|
assert(std::is_standard_layout<stats>::value);
|
||||||
void update(Color c, Move m, Value v) {
|
|
||||||
|
|
||||||
if (abs(int(v)) >= 324)
|
typedef StatsEntry<T, D> entry;
|
||||||
return;
|
entry* p = reinterpret_cast<entry*>(this);
|
||||||
|
std::fill(p, p + sizeof(*this) / sizeof(entry), v);
|
||||||
Square from = from_sq(m);
|
|
||||||
Square to = to_sq(m);
|
|
||||||
|
|
||||||
table[c][from][to] -= table[c][from][to] * abs(int(v)) / 324;
|
|
||||||
table[c][from][to] += int(v) * 32;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
Value table[COLOR_NB][SQUARE_NB][SQUARE_NB];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T, int D, int Size>
|
||||||
|
struct Stats<T, D, Size> : public std::array<StatsEntry<T, D>, Size> {};
|
||||||
|
|
||||||
|
/// In stats table, D=0 means that the template parameter is not used
|
||||||
|
enum StatsParams { NOT_USED = 0 };
|
||||||
|
|
||||||
|
|
||||||
|
/// ButterflyHistory records how often quiet moves have been successful or
|
||||||
|
/// unsuccessful during the current search, and is used for reduction and move
|
||||||
|
/// ordering decisions. It uses 2 tables (one for each color) indexed by
|
||||||
|
/// the move's from and to squares, see chessprogramming.wikispaces.com/Butterfly+Boards
|
||||||
|
typedef Stats<int16_t, 10692, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)> ButterflyHistory;
|
||||||
|
|
||||||
|
/// CounterMoveHistory stores counter moves indexed by [piece][to] of the previous
|
||||||
|
/// move, see chessprogramming.wikispaces.com/Countermove+Heuristic
|
||||||
|
typedef Stats<Move, NOT_USED, PIECE_NB, SQUARE_NB> CounterMoveHistory;
|
||||||
|
|
||||||
|
/// CapturePieceToHistory is addressed by a move's [piece][to][captured piece type]
|
||||||
|
typedef Stats<int16_t, 10692, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB> CapturePieceToHistory;
|
||||||
|
|
||||||
|
/// PieceToHistory is like ButterflyHistory but is addressed by a move's [piece][to]
|
||||||
|
typedef Stats<int16_t, 29952, PIECE_NB, SQUARE_NB> PieceToHistory;
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
/// PieceToHistory instead of ButterflyBoards.
|
||||||
|
typedef Stats<PieceToHistory, NOT_USED, PIECE_NB, SQUARE_NB> ContinuationHistory;
|
||||||
|
|
||||||
|
|
||||||
/// MovePicker class is used to pick one pseudo legal move at a time from the
|
/// MovePicker class is used to pick one pseudo legal move at a time from the
|
||||||
/// current position. The most important method is next_move(), which returns a
|
/// current position. The most important method is next_move(), which returns a
|
||||||
@@ -90,33 +110,42 @@ private:
|
|||||||
/// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
|
/// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
|
||||||
/// beta algorithm, MovePicker attempts to return the moves which are most likely
|
/// beta algorithm, MovePicker attempts to return the moves which are most likely
|
||||||
/// to get a cut-off first.
|
/// to get a cut-off first.
|
||||||
namespace Search { struct Stack; }
|
|
||||||
|
|
||||||
class MovePicker {
|
class MovePicker {
|
||||||
|
|
||||||
|
enum PickType { Next, Best };
|
||||||
|
|
||||||
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, Value);
|
MovePicker(const Position&, Move, Depth, const ButterflyHistory*,
|
||||||
MovePicker(const Position&, Move, Depth, Square);
|
const CapturePieceToHistory*,
|
||||||
MovePicker(const Position&, Move, Depth, Search::Stack*);
|
const PieceToHistory**,
|
||||||
|
Square);
|
||||||
Move next_move();
|
MovePicker(const Position&, Move, Depth, const ButterflyHistory*,
|
||||||
|
const CapturePieceToHistory*,
|
||||||
|
const PieceToHistory**,
|
||||||
|
Move,
|
||||||
|
Move*);
|
||||||
|
Move next_move(bool skipQuiets = false);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
template<PickType T, typename Pred> Move select(Pred);
|
||||||
template<GenType> void score();
|
template<GenType> void score();
|
||||||
ExtMove* begin() { return cur; }
|
ExtMove* begin() { return cur; }
|
||||||
ExtMove* end() { return endMoves; }
|
ExtMove* end() { return endMoves; }
|
||||||
|
|
||||||
const Position& pos;
|
const Position& pos;
|
||||||
const Search::Stack* ss;
|
const ButterflyHistory* mainHistory;
|
||||||
Move countermove;
|
const CapturePieceToHistory* captureHistory;
|
||||||
Depth depth;
|
const PieceToHistory** continuationHistory;
|
||||||
Move ttMove;
|
Move ttMove;
|
||||||
|
ExtMove refutations[3], *cur, *endMoves, *endBadCaptures;
|
||||||
|
int stage;
|
||||||
|
Move move;
|
||||||
Square recaptureSquare;
|
Square recaptureSquare;
|
||||||
Value threshold;
|
Value threshold;
|
||||||
int stage;
|
Depth depth;
|
||||||
ExtMove *cur, *endMoves, *endBadCaptures;
|
|
||||||
ExtMove moves[MAX_MOVES];
|
ExtMove moves[MAX_MOVES];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+99
-127
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,84 +31,56 @@ namespace {
|
|||||||
#define V Value
|
#define V Value
|
||||||
#define S(mg, eg) make_score(mg, eg)
|
#define S(mg, eg) make_score(mg, eg)
|
||||||
|
|
||||||
// Isolated pawn penalty by opposed flag
|
// Pawn penalties
|
||||||
const Score Isolated[2] = { S(45, 40), S(30, 27) };
|
constexpr Score Backward = S( 9, 24);
|
||||||
|
constexpr Score Doubled = S(11, 56);
|
||||||
|
constexpr Score Isolated = S( 5, 15);
|
||||||
|
|
||||||
// Backward pawn penalty by opposed flag
|
// Connected pawn bonus by opposed, phalanx, #support and rank
|
||||||
const Score Backward[2] = { S(56, 33), S(41, 19) };
|
Score Connected[2][2][3][RANK_NB];
|
||||||
|
|
||||||
// Unsupported pawn penalty for pawns which are neither isolated or backward
|
// Strength of pawn shelter for our king by [distance from edge][rank].
|
||||||
const Score Unsupported = S(17, 8);
|
// 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] = {
|
||||||
// Connected pawn bonus by opposed, phalanx, twice supported and rank
|
{ V( -6), V( 81), V( 93), V( 58), V( 39), V( 18), V( 25) },
|
||||||
Score Connected[2][2][2][RANK_NB];
|
{ V(-43), V( 61), V( 35), V(-49), V(-29), V(-11), V( -63) },
|
||||||
|
{ V(-10), V( 75), V( 23), V( -2), V( 32), V( 3), V( -45) },
|
||||||
// Doubled pawn penalty
|
{ V(-39), V(-13), V(-29), V(-52), V(-48), V(-67), V(-166) }
|
||||||
const Score Doubled = S(18,38);
|
|
||||||
|
|
||||||
// Lever bonus by rank
|
|
||||||
const Score Lever[RANK_NB] = {
|
|
||||||
S( 0, 0), S( 0, 0), S(0, 0), S(0, 0),
|
|
||||||
S(17, 16), S(33, 32), S(0, 0), S(0, 0)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Weakness of our pawn shelter in front of the king by [distance from edge][rank]
|
// Danger of enemy pawns moving toward our king by [distance from edge][rank].
|
||||||
const Value ShelterWeakness[][RANK_NB] = {
|
// RANK_1 = 0 is used for files where the enemy has no pawn, or their pawn
|
||||||
{ V( 97), V(21), V(26), V(51), V(87), V( 89), V( 99) },
|
// is behind our king.
|
||||||
{ V(120), V( 0), V(28), V(76), V(88), V(103), V(104) },
|
constexpr Value UnblockedStorm[int(FILE_NB) / 2][RANK_NB] = {
|
||||||
{ V(101), V( 7), V(54), V(78), V(77), V( 92), V(101) },
|
{ V( 89), V(107), V(123), V(93), V(57), V( 45), V( 51) },
|
||||||
{ V( 80), V(11), V(44), V(68), V(87), V( 90), V(119) }
|
{ V( 44), V(-18), V(123), V(46), V(39), V( -7), V( 23) },
|
||||||
|
{ V( 4), V( 52), V(162), V(37), V( 7), V(-14), V( -2) },
|
||||||
|
{ V(-10), V(-14), V( 90), V(15), V( 2), V( -7), V(-16) }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Danger of enemy pawns moving toward our king by [type][distance from edge][rank]
|
|
||||||
const Value StormDanger[][4][RANK_NB] = {
|
|
||||||
{ { V( 0), V( 67), V( 134), V(38), V(32) },
|
|
||||||
{ V( 0), V( 57), V( 139), V(37), V(22) },
|
|
||||||
{ V( 0), V( 43), V( 115), V(43), V(27) },
|
|
||||||
{ V( 0), V( 68), V( 124), V(57), V(32) } },
|
|
||||||
{ { V(20), V( 43), V( 100), V(56), V(20) },
|
|
||||||
{ V(23), V( 20), V( 98), V(40), V(15) },
|
|
||||||
{ V(23), V( 39), V( 103), V(36), V(18) },
|
|
||||||
{ V(28), V( 19), V( 108), V(42), V(26) } },
|
|
||||||
{ { V( 0), V( 0), V( 75), V(14), V( 2) },
|
|
||||||
{ V( 0), V( 0), V( 150), V(30), V( 4) },
|
|
||||||
{ V( 0), V( 0), V( 160), V(22), V( 5) },
|
|
||||||
{ V( 0), V( 0), V( 166), V(24), V(13) } },
|
|
||||||
{ { V( 0), V(-283), V(-281), V(57), V(31) },
|
|
||||||
{ V( 0), V( 58), V( 141), V(39), V(18) },
|
|
||||||
{ V( 0), V( 65), V( 142), V(48), V(32) },
|
|
||||||
{ V( 0), V( 60), V( 126), V(51), V(19) } }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Max bonus for king safety. Corresponds to start position with all the pawns
|
|
||||||
// in front of the king and no enemy pawn on the horizon.
|
|
||||||
const Value MaxSafetyBonus = V(258);
|
|
||||||
|
|
||||||
#undef S
|
#undef S
|
||||||
#undef V
|
#undef V
|
||||||
|
|
||||||
template<Color Us>
|
template<Color Us>
|
||||||
Score evaluate(const Position& pos, Pawns::Entry* e) {
|
Score evaluate(const Position& pos, Pawns::Entry* e) {
|
||||||
|
|
||||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||||
const Square Up = (Us == WHITE ? NORTH : SOUTH);
|
constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH);
|
||||||
const Square Right = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
|
|
||||||
const Square Left = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
|
|
||||||
|
|
||||||
Bitboard b, neighbours, stoppers, doubled, supported, phalanx;
|
Bitboard b, neighbours, stoppers, doubled, supported, phalanx;
|
||||||
|
Bitboard lever, leverPush;
|
||||||
Square s;
|
Square s;
|
||||||
bool opposed, lever, connected, backward;
|
bool opposed, backward;
|
||||||
Score score = SCORE_ZERO;
|
Score score = SCORE_ZERO;
|
||||||
const Square* pl = pos.squares<PAWN>(Us);
|
const Square* pl = pos.squares<PAWN>(Us);
|
||||||
const Bitboard* pawnAttacksBB = StepAttacksBB[make_piece(Us, PAWN)];
|
|
||||||
|
|
||||||
Bitboard ourPawns = pos.pieces(Us , PAWN);
|
Bitboard ourPawns = pos.pieces( Us, PAWN);
|
||||||
Bitboard theirPawns = pos.pieces(Them, PAWN);
|
Bitboard theirPawns = pos.pieces(Them, PAWN);
|
||||||
|
|
||||||
e->passedPawns[Us] = e->pawnAttacksSpan[Us] = 0;
|
e->passedPawns[Us] = e->pawnAttacksSpan[Us] = e->weakUnopposed[Us] = 0;
|
||||||
e->semiopenFiles[Us] = 0xFF;
|
e->semiopenFiles[Us] = 0xFF;
|
||||||
e->kingSquares[Us] = SQ_NONE;
|
e->kingSquares[Us] = SQ_NONE;
|
||||||
e->pawnAttacks[Us] = shift<Right>(ourPawns) | shift<Left>(ourPawns);
|
e->pawnAttacks[Us] = pawn_attacks_bb<Us>(ourPawns);
|
||||||
e->pawnsOnSquares[Us][BLACK] = popcount(ourPawns & DarkSquares);
|
e->pawnsOnSquares[Us][BLACK] = popcount(ourPawns & DarkSquares);
|
||||||
e->pawnsOnSquares[Us][WHITE] = pos.count<PAWN>(Us) - e->pawnsOnSquares[Us][BLACK];
|
e->pawnsOnSquares[Us][WHITE] = pos.count<PAWN>(Us) - e->pawnsOnSquares[Us][BLACK];
|
||||||
|
|
||||||
@@ -123,55 +95,50 @@ namespace {
|
|||||||
e->pawnAttacksSpan[Us] |= pawn_attack_span(Us, s);
|
e->pawnAttacksSpan[Us] |= pawn_attack_span(Us, s);
|
||||||
|
|
||||||
// Flag the pawn
|
// Flag the pawn
|
||||||
opposed = theirPawns & forward_bb(Us, s);
|
opposed = theirPawns & forward_file_bb(Us, s);
|
||||||
stoppers = theirPawns & passed_pawn_mask(Us, s);
|
stoppers = theirPawns & passed_pawn_mask(Us, s);
|
||||||
lever = theirPawns & pawnAttacksBB[s];
|
lever = theirPawns & PawnAttacks[Us][s];
|
||||||
doubled = ourPawns & (s + Up);
|
leverPush = theirPawns & PawnAttacks[Us][s + Up];
|
||||||
|
doubled = ourPawns & (s - Up);
|
||||||
neighbours = ourPawns & adjacent_files_bb(f);
|
neighbours = ourPawns & adjacent_files_bb(f);
|
||||||
phalanx = neighbours & rank_bb(s);
|
phalanx = neighbours & rank_bb(s);
|
||||||
supported = neighbours & rank_bb(s - Up);
|
supported = neighbours & rank_bb(s - Up);
|
||||||
connected = supported | phalanx;
|
|
||||||
|
|
||||||
// A pawn is backward when it is behind all pawns of the same color on the
|
// A pawn is backward when it is behind all pawns of the same color
|
||||||
// adjacent files and cannot be safely advanced.
|
// on the adjacent files and cannot be safely advanced.
|
||||||
if (!neighbours || lever || relative_rank(Us, s) >= RANK_5)
|
backward = !(ourPawns & pawn_attack_span(Them, s + Up))
|
||||||
backward = false;
|
&& (stoppers & (leverPush | (s + Up)));
|
||||||
else
|
|
||||||
{
|
|
||||||
// Find the backmost rank with neighbours or stoppers
|
|
||||||
b = rank_bb(backmost_sq(Us, neighbours | stoppers));
|
|
||||||
|
|
||||||
// The pawn is backward when it cannot safely progress to that rank:
|
|
||||||
// either there is a stopper in the way on this rank, or there is a
|
|
||||||
// stopper on adjacent file which controls the way to that rank.
|
|
||||||
backward = (b | shift<Up>(b & adjacent_files_bb(f))) & stoppers;
|
|
||||||
|
|
||||||
assert(!backward || !(pawn_attack_span(Them, s + Up) & neighbours));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Passed pawns will be properly scored in evaluation because we need
|
// Passed pawns will be properly scored in evaluation because we need
|
||||||
// full attack info to evaluate them.
|
// full attack info to evaluate them. Include also not passed pawns
|
||||||
if (!stoppers && !(ourPawns & forward_bb(Us, s)))
|
// which could become passed after one or two pawn pushes when are
|
||||||
|
// not attacked more times than defended.
|
||||||
|
if ( !(stoppers ^ lever ^ leverPush)
|
||||||
|
&& popcount(supported) >= popcount(lever) - 1
|
||||||
|
&& popcount(phalanx) >= popcount(leverPush))
|
||||||
e->passedPawns[Us] |= s;
|
e->passedPawns[Us] |= s;
|
||||||
|
|
||||||
|
else if ( stoppers == SquareBB[s + Up]
|
||||||
|
&& relative_rank(Us, s) >= RANK_5)
|
||||||
|
{
|
||||||
|
b = shift<Up>(supported) & ~theirPawns;
|
||||||
|
while (b)
|
||||||
|
if (!more_than_one(theirPawns & PawnAttacks[Us][pop_lsb(&b)]))
|
||||||
|
e->passedPawns[Us] |= s;
|
||||||
|
}
|
||||||
|
|
||||||
// Score this pawn
|
// Score this pawn
|
||||||
if (!neighbours)
|
if (supported | phalanx)
|
||||||
score -= Isolated[opposed];
|
score += Connected[opposed][bool(phalanx)][popcount(supported)][relative_rank(Us, s)];
|
||||||
|
|
||||||
|
else if (!neighbours)
|
||||||
|
score -= Isolated, e->weakUnopposed[Us] += !opposed;
|
||||||
|
|
||||||
else if (backward)
|
else if (backward)
|
||||||
score -= Backward[opposed];
|
score -= Backward, e->weakUnopposed[Us] += !opposed;
|
||||||
|
|
||||||
else if (!supported)
|
if (doubled && !supported)
|
||||||
score -= Unsupported;
|
|
||||||
|
|
||||||
if (connected)
|
|
||||||
score += Connected[opposed][!!phalanx][more_than_one(supported)][relative_rank(Us, s)];
|
|
||||||
|
|
||||||
if (doubled)
|
|
||||||
score -= Doubled;
|
score -= Doubled;
|
||||||
|
|
||||||
if (lever)
|
|
||||||
score += Lever[relative_rank(Us, s)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return score;
|
return score;
|
||||||
@@ -187,16 +154,17 @@ namespace Pawns {
|
|||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
|
|
||||||
static const int Seed[RANK_NB] = { 0, 8, 19, 13, 71, 94, 169, 324 };
|
static constexpr int Seed[RANK_NB] = { 0, 13, 24, 18, 65, 100, 175, 330 };
|
||||||
|
|
||||||
for (int opposed = 0; opposed <= 1; ++opposed)
|
for (int opposed = 0; opposed <= 1; ++opposed)
|
||||||
for (int phalanx = 0; phalanx <= 1; ++phalanx)
|
for (int phalanx = 0; phalanx <= 1; ++phalanx)
|
||||||
for (int apex = 0; apex <= 1; ++apex)
|
for (int support = 0; support <= 2; ++support)
|
||||||
for (Rank r = RANK_2; r < RANK_8; ++r)
|
for (Rank r = RANK_2; r < RANK_8; ++r)
|
||||||
{
|
{
|
||||||
int v = (Seed[r] + (phalanx ? (Seed[r + 1] - Seed[r]) / 2 : 0)) >> opposed;
|
int v = 17 * support;
|
||||||
v += (apex ? v / 2 : 0);
|
v += (Seed[r] + (phalanx ? (Seed[r + 1] - Seed[r]) / 2 : 0)) >> opposed;
|
||||||
Connected[opposed][phalanx][apex][r] = make_score(v, v * 5 / 8);
|
|
||||||
|
Connected[opposed][phalanx][support][r] = make_score(v, v * (r - 2) / 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,43 +183,46 @@ Entry* probe(const Position& pos) {
|
|||||||
return e;
|
return e;
|
||||||
|
|
||||||
e->key = key;
|
e->key = key;
|
||||||
e->score = evaluate<WHITE>(pos, e) - evaluate<BLACK>(pos, e);
|
e->scores[WHITE] = evaluate<WHITE>(pos, e);
|
||||||
e->asymmetry = popcount(e->semiopenFiles[WHITE] ^ e->semiopenFiles[BLACK]);
|
e->scores[BLACK] = evaluate<BLACK>(pos, e);
|
||||||
e->openFiles = popcount(e->semiopenFiles[WHITE] & e->semiopenFiles[BLACK]);
|
e->openFiles = popcount(e->semiopenFiles[WHITE] & e->semiopenFiles[BLACK]);
|
||||||
|
e->asymmetry = popcount( (e->passedPawns[WHITE] | e->passedPawns[BLACK])
|
||||||
|
| (e->semiopenFiles[WHITE] ^ e->semiopenFiles[BLACK]));
|
||||||
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Entry::shelter_storm() calculates shelter and storm penalties for the file
|
/// Entry::evaluate_shelter() calculates the shelter bonus and the storm
|
||||||
/// the king is on, as well as the two adjacent files.
|
/// penalty for a king, looking at the king file and the two closest files.
|
||||||
|
|
||||||
template<Color Us>
|
template<Color Us>
|
||||||
Value Entry::shelter_storm(const Position& pos, Square ksq) {
|
Value Entry::evaluate_shelter(const Position& pos, Square ksq) {
|
||||||
|
|
||||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
constexpr Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||||
|
constexpr Direction Down = (Us == WHITE ? SOUTH : NORTH);
|
||||||
|
constexpr Bitboard BlockRanks = (Us == WHITE ? Rank1BB | Rank2BB : Rank8BB | Rank7BB);
|
||||||
|
|
||||||
enum { NoFriendlyPawn, Unblocked, BlockedByPawn, BlockedByKing };
|
Bitboard b = pos.pieces(PAWN) & ~forward_ranks_bb(Them, ksq);
|
||||||
|
|
||||||
Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq));
|
|
||||||
Bitboard ourPawns = b & pos.pieces(Us);
|
Bitboard ourPawns = b & pos.pieces(Us);
|
||||||
Bitboard theirPawns = b & pos.pieces(Them);
|
Bitboard theirPawns = b & pos.pieces(Them);
|
||||||
Value safety = MaxSafetyBonus;
|
|
||||||
File center = std::max(FILE_B, std::min(FILE_G, file_of(ksq)));
|
|
||||||
|
|
||||||
for (File f = center - File(1); f <= center + File(1); ++f)
|
Value safety = (shift<Down>(theirPawns) & (FileABB | FileHBB) & BlockRanks & ksq) ?
|
||||||
|
Value(374) : Value(5);
|
||||||
|
|
||||||
|
File center = std::max(FILE_B, std::min(FILE_G, file_of(ksq)));
|
||||||
|
for (File f = File(center - 1); f <= File(center + 1); ++f)
|
||||||
{
|
{
|
||||||
b = ourPawns & file_bb(f);
|
b = ourPawns & file_bb(f);
|
||||||
Rank rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1;
|
int ourRank = b ? relative_rank(Us, backmost_sq(Us, b)) : 0;
|
||||||
|
|
||||||
b = theirPawns & file_bb(f);
|
b = theirPawns & file_bb(f);
|
||||||
Rank rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1;
|
int theirRank = b ? relative_rank(Us, frontmost_sq(Them, b)) : 0;
|
||||||
|
|
||||||
safety -= ShelterWeakness[std::min(f, FILE_H - f)][rkUs]
|
int d = std::min(f, ~f);
|
||||||
+ StormDanger
|
safety += ShelterStrength[d][ourRank];
|
||||||
[f == file_of(ksq) && rkThem == relative_rank(Us, ksq) + 1 ? BlockedByKing :
|
safety -= (ourRank && (ourRank == theirRank - 1)) ? 66 * (theirRank == RANK_3)
|
||||||
rkUs == RANK_1 ? NoFriendlyPawn :
|
: UnblockedStorm[d][theirRank];
|
||||||
rkThem == rkUs + 1 ? BlockedByPawn : Unblocked]
|
|
||||||
[std::min(f, FILE_H - f)][rkThem];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return safety;
|
return safety;
|
||||||
@@ -262,30 +233,31 @@ Value Entry::shelter_storm(const Position& pos, Square ksq) {
|
|||||||
/// when king square changes, which is about 20% of total king_safety() calls.
|
/// when king square changes, which is about 20% of total king_safety() calls.
|
||||||
|
|
||||||
template<Color Us>
|
template<Color Us>
|
||||||
Score Entry::do_king_safety(const Position& pos, Square ksq) {
|
Score Entry::do_king_safety(const Position& pos) {
|
||||||
|
|
||||||
|
Square ksq = pos.square<KING>(Us);
|
||||||
kingSquares[Us] = ksq;
|
kingSquares[Us] = ksq;
|
||||||
castlingRights[Us] = pos.can_castle(Us);
|
castlingRights[Us] = pos.can_castle(Us);
|
||||||
int minKingPawnDistance = 0;
|
int minKingPawnDistance = 0;
|
||||||
|
|
||||||
Bitboard pawns = pos.pieces(Us, PAWN);
|
Bitboard pawns = pos.pieces(Us, PAWN);
|
||||||
if (pawns)
|
if (pawns)
|
||||||
while (!(DistanceRingBB[ksq][minKingPawnDistance++] & pawns)) {}
|
while (!(DistanceRingBB[ksq][++minKingPawnDistance] & pawns)) {}
|
||||||
|
|
||||||
Value bonus = shelter_storm<Us>(pos, ksq);
|
Value bonus = evaluate_shelter<Us>(pos, ksq);
|
||||||
|
|
||||||
// If we can castle use the bonus after the castling if it is bigger
|
// If we can castle use the bonus after the castling if it is bigger
|
||||||
if (pos.can_castle(MakeCastling<Us, KING_SIDE>::right))
|
if (pos.can_castle(Us | KING_SIDE))
|
||||||
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_G1)));
|
bonus = std::max(bonus, evaluate_shelter<Us>(pos, relative_square(Us, SQ_G1)));
|
||||||
|
|
||||||
if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::right))
|
if (pos.can_castle(Us | QUEEN_SIDE))
|
||||||
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));
|
bonus = std::max(bonus, evaluate_shelter<Us>(pos, relative_square(Us, SQ_C1)));
|
||||||
|
|
||||||
return make_score(bonus, -16 * minKingPawnDistance);
|
return make_score(bonus, -16 * minKingPawnDistance);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Explicit template instantiation
|
// Explicit template instantiation
|
||||||
template Score Entry::do_king_safety<WHITE>(const Position& pos, Square ksq);
|
template Score Entry::do_king_safety<WHITE>(const Position& pos);
|
||||||
template Score Entry::do_king_safety<BLACK>(const Position& pos, Square ksq);
|
template Score Entry::do_king_safety<BLACK>(const Position& pos);
|
||||||
|
|
||||||
} // namespace Pawns
|
} // namespace Pawns
|
||||||
|
|||||||
+11
-13
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,10 +33,11 @@ namespace Pawns {
|
|||||||
|
|
||||||
struct Entry {
|
struct Entry {
|
||||||
|
|
||||||
Score pawns_score() const { return score; }
|
Score pawn_score(Color c) const { return scores[c]; }
|
||||||
Bitboard pawn_attacks(Color c) const { return pawnAttacks[c]; }
|
Bitboard pawn_attacks(Color c) const { return pawnAttacks[c]; }
|
||||||
Bitboard passed_pawns(Color c) const { return passedPawns[c]; }
|
Bitboard passed_pawns(Color c) const { return passedPawns[c]; }
|
||||||
Bitboard pawn_attacks_span(Color c) const { return pawnAttacksSpan[c]; }
|
Bitboard pawn_attacks_span(Color c) const { return pawnAttacksSpan[c]; }
|
||||||
|
int weak_unopposed(Color c) const { return weakUnopposed[c]; }
|
||||||
int pawn_asymmetry() const { return asymmetry; }
|
int pawn_asymmetry() const { return asymmetry; }
|
||||||
int open_files() const { return openFiles; }
|
int open_files() const { return openFiles; }
|
||||||
|
|
||||||
@@ -44,33 +45,30 @@ struct Entry {
|
|||||||
return semiopenFiles[c] & (1 << f);
|
return semiopenFiles[c] & (1 << f);
|
||||||
}
|
}
|
||||||
|
|
||||||
int semiopen_side(Color c, File f, bool leftSide) const {
|
|
||||||
return semiopenFiles[c] & (leftSide ? (1 << f) - 1 : ~((1 << (f + 1)) - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
int pawns_on_same_color_squares(Color c, Square s) const {
|
int pawns_on_same_color_squares(Color c, Square s) const {
|
||||||
return pawnsOnSquares[c][!!(DarkSquares & s)];
|
return pawnsOnSquares[c][bool(DarkSquares & s)];
|
||||||
}
|
}
|
||||||
|
|
||||||
template<Color Us>
|
template<Color Us>
|
||||||
Score king_safety(const Position& pos, Square ksq) {
|
Score king_safety(const Position& pos) {
|
||||||
return kingSquares[Us] == ksq && castlingRights[Us] == pos.can_castle(Us)
|
return kingSquares[Us] == pos.square<KING>(Us) && castlingRights[Us] == pos.can_castle(Us)
|
||||||
? kingSafety[Us] : (kingSafety[Us] = do_king_safety<Us>(pos, ksq));
|
? kingSafety[Us] : (kingSafety[Us] = do_king_safety<Us>(pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<Color Us>
|
template<Color Us>
|
||||||
Score do_king_safety(const Position& pos, Square ksq);
|
Score do_king_safety(const Position& pos);
|
||||||
|
|
||||||
template<Color Us>
|
template<Color Us>
|
||||||
Value shelter_storm(const Position& pos, Square ksq);
|
Value evaluate_shelter(const Position& pos, Square ksq);
|
||||||
|
|
||||||
Key key;
|
Key key;
|
||||||
Score score;
|
Score scores[COLOR_NB];
|
||||||
Bitboard passedPawns[COLOR_NB];
|
Bitboard passedPawns[COLOR_NB];
|
||||||
Bitboard pawnAttacks[COLOR_NB];
|
Bitboard pawnAttacks[COLOR_NB];
|
||||||
Bitboard pawnAttacksSpan[COLOR_NB];
|
Bitboard pawnAttacksSpan[COLOR_NB];
|
||||||
Square kingSquares[COLOR_NB];
|
Square kingSquares[COLOR_NB];
|
||||||
Score kingSafety[COLOR_NB];
|
Score kingSafety[COLOR_NB];
|
||||||
|
int weakUnopposed[COLOR_NB];
|
||||||
int castlingRights[COLOR_NB];
|
int castlingRights[COLOR_NB];
|
||||||
int semiopenFiles[COLOR_NB];
|
int semiopenFiles[COLOR_NB];
|
||||||
int pawnsOnSquares[COLOR_NB][COLOR_NB]; // [color][light/dark squares]
|
int pawnsOnSquares[COLOR_NB][COLOR_NB]; // [color][light/dark squares]
|
||||||
|
|||||||
+320
-175
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,46 +32,51 @@
|
|||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
#include "uci.h"
|
#include "uci.h"
|
||||||
|
#include "syzygy/tbprobe.h"
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
namespace PSQT {
|
|
||||||
extern Score psq[PIECE_NB][SQUARE_NB];
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace Zobrist {
|
namespace Zobrist {
|
||||||
|
|
||||||
Key psq[PIECE_NB][SQUARE_NB];
|
Key psq[PIECE_NB][SQUARE_NB];
|
||||||
Key enpassant[FILE_NB];
|
Key enpassant[FILE_NB];
|
||||||
Key castling[CASTLING_RIGHT_NB];
|
Key castling[CASTLING_RIGHT_NB];
|
||||||
Key side;
|
Key side, noPawns;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
const string PieceToChar(" PNBRQK pnbrqk");
|
const string PieceToChar(" PNBRQK pnbrqk");
|
||||||
|
|
||||||
// min_attacker() is a helper function used by see() to locate the least
|
constexpr Piece Pieces[] = { W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
|
||||||
|
B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING };
|
||||||
|
|
||||||
|
// min_attacker() is a helper function used by see_ge() to locate the least
|
||||||
// valuable attacker for the side to move, remove the attacker we just found
|
// valuable attacker for the side to move, remove the attacker we just found
|
||||||
// from the bitboards and scan for new X-ray attacks behind it.
|
// from the bitboards and scan for new X-ray attacks behind it.
|
||||||
|
|
||||||
template<int Pt>
|
template<int Pt>
|
||||||
PieceType min_attacker(const Bitboard* bb, Square to, Bitboard stmAttackers,
|
PieceType min_attacker(const Bitboard* byTypeBB, Square to, Bitboard stmAttackers,
|
||||||
Bitboard& occupied, Bitboard& attackers) {
|
Bitboard& occupied, Bitboard& attackers) {
|
||||||
|
|
||||||
Bitboard b = stmAttackers & bb[Pt];
|
Bitboard b = stmAttackers & byTypeBB[Pt];
|
||||||
if (!b)
|
if (!b)
|
||||||
return min_attacker<Pt+1>(bb, to, stmAttackers, occupied, attackers);
|
return min_attacker<Pt + 1>(byTypeBB, to, stmAttackers, occupied, attackers);
|
||||||
|
|
||||||
occupied ^= b & ~(b - 1);
|
occupied ^= lsb(b); // Remove the attacker from occupied
|
||||||
|
|
||||||
|
// Add any X-ray attack behind the just removed piece. For instance with
|
||||||
|
// rooks in a8 and a7 attacking a1, after removing a7 we add rook in a8.
|
||||||
|
// Note that new added attackers can be of any color.
|
||||||
if (Pt == PAWN || Pt == BISHOP || Pt == QUEEN)
|
if (Pt == PAWN || Pt == BISHOP || Pt == QUEEN)
|
||||||
attackers |= attacks_bb<BISHOP>(to, occupied) & (bb[BISHOP] | bb[QUEEN]);
|
attackers |= attacks_bb<BISHOP>(to, occupied) & (byTypeBB[BISHOP] | byTypeBB[QUEEN]);
|
||||||
|
|
||||||
if (Pt == ROOK || Pt == QUEEN)
|
if (Pt == ROOK || Pt == QUEEN)
|
||||||
attackers |= attacks_bb<ROOK>(to, occupied) & (bb[ROOK] | bb[QUEEN]);
|
attackers |= attacks_bb<ROOK>(to, occupied) & (byTypeBB[ROOK] | byTypeBB[QUEEN]);
|
||||||
|
|
||||||
attackers &= occupied; // After X-ray that may add already processed pieces
|
// X-ray may add already processed pieces because byTypeBB[] is constant: in
|
||||||
|
// the rook example, now attackers contains _again_ rook in a7, so remove it.
|
||||||
|
attackers &= occupied;
|
||||||
return (PieceType)Pt;
|
return (PieceType)Pt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,15 +103,42 @@ std::ostream& operator<<(std::ostream& os, const Position& pos) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
os << "\nFen: " << pos.fen() << "\nKey: " << std::hex << std::uppercase
|
os << "\nFen: " << pos.fen() << "\nKey: " << std::hex << std::uppercase
|
||||||
<< std::setfill('0') << std::setw(16) << pos.key() << std::dec << "\nCheckers: ";
|
<< std::setfill('0') << std::setw(16) << pos.key()
|
||||||
|
<< 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())
|
||||||
|
&& !pos.can_castle(ANY_CASTLING))
|
||||||
|
{
|
||||||
|
StateInfo st;
|
||||||
|
Position p;
|
||||||
|
p.set(pos.fen(), pos.is_chess960(), &st, pos.this_thread());
|
||||||
|
Tablebases::ProbeState s1, s2;
|
||||||
|
Tablebases::WDLScore wdl = Tablebases::probe_wdl(p, &s1);
|
||||||
|
int dtz = Tablebases::probe_dtz(p, &s2);
|
||||||
|
os << "\nTablebases WDL: " << std::setw(4) << wdl << " (" << s1 << ")"
|
||||||
|
<< "\nTablebases DTZ: " << std::setw(4) << dtz << " (" << s2 << ")";
|
||||||
|
}
|
||||||
|
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Marcel van Kervinck's cuckoo algorithm for fast detection of "upcoming repetition"
|
||||||
|
// situations. Description of the algorithm in the following paper:
|
||||||
|
// https://marcelk.net/2013-04-06/paper/upcoming-rep-v2.pdf
|
||||||
|
|
||||||
|
// First and second hash functions for indexing the cuckoo tables
|
||||||
|
inline int H1(Key h) { return h & 0x1fff; }
|
||||||
|
inline int H2(Key h) { return (h >> 16) & 0x1fff; }
|
||||||
|
|
||||||
|
// Cuckoo tables with Zobrist hashes of valid reversible moves, and the moves themselves
|
||||||
|
Key cuckoo[8192];
|
||||||
|
Move cuckooMove[8192];
|
||||||
|
|
||||||
|
|
||||||
/// Position::init() initializes at startup the various arrays used to compute
|
/// Position::init() initializes at startup the various arrays used to compute
|
||||||
/// hash keys.
|
/// hash keys.
|
||||||
|
|
||||||
@@ -133,6 +165,31 @@ void Position::init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Zobrist::side = rng.rand<Key>();
|
Zobrist::side = rng.rand<Key>();
|
||||||
|
Zobrist::noPawns = rng.rand<Key>();
|
||||||
|
|
||||||
|
// Prepare the cuckoo tables
|
||||||
|
std::memset(cuckoo, 0, sizeof(cuckoo));
|
||||||
|
std::memset(cuckooMove, 0, sizeof(cuckooMove));
|
||||||
|
int count = 0;
|
||||||
|
for (Piece pc : Pieces)
|
||||||
|
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
||||||
|
for (Square s2 = Square(s1 + 1); s2 <= SQ_H8; ++s2)
|
||||||
|
if (PseudoAttacks[type_of(pc)][s1] & s2)
|
||||||
|
{
|
||||||
|
Move move = make_move(s1, s2);
|
||||||
|
Key key = Zobrist::psq[pc][s1] ^ Zobrist::psq[pc][s2] ^ Zobrist::side;
|
||||||
|
int i = H1(key);
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
std::swap(cuckoo[i], key);
|
||||||
|
std::swap(cuckooMove[i], move);
|
||||||
|
if (move == 0) // Arrived at empty slot ?
|
||||||
|
break;
|
||||||
|
i = (i == H1(key)) ? H2(key) : H1(key); // Push victim to alternative slot
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
assert(count == 3668);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -164,8 +221,9 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th
|
|||||||
|
|
||||||
4) En passant target square (in algebraic notation). If there's no en passant
|
4) En passant target square (in algebraic notation). If there's no en passant
|
||||||
target square, this is "-". If a pawn has just made a 2-square move, this
|
target square, this is "-". If a pawn has just made a 2-square move, this
|
||||||
is the position "behind" the pawn. This is recorded regardless of whether
|
is the position "behind" the pawn. This is recorded only if there is a pawn
|
||||||
there is a pawn in position to make an en passant capture.
|
in position to make an en passant capture, and if there really is a pawn
|
||||||
|
that might have advanced two squares.
|
||||||
|
|
||||||
5) Halfmove clock. This is the number of halfmoves since the last pawn advance
|
5) Halfmove clock. This is the number of halfmoves since the last pawn advance
|
||||||
or capture. This is used to determine if a draw can be claimed under the
|
or capture. This is used to determine if a draw can be claimed under the
|
||||||
@@ -191,10 +249,10 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th
|
|||||||
while ((ss >> token) && !isspace(token))
|
while ((ss >> token) && !isspace(token))
|
||||||
{
|
{
|
||||||
if (isdigit(token))
|
if (isdigit(token))
|
||||||
sq += Square(token - '0'); // Advance the given number of files
|
sq += (token - '0') * EAST; // Advance the given number of files
|
||||||
|
|
||||||
else if (token == '/')
|
else if (token == '/')
|
||||||
sq -= Square(16);
|
sq += 2 * SOUTH;
|
||||||
|
|
||||||
else if ((idx = PieceToChar.find(token)) != string::npos)
|
else if ((idx = PieceToChar.find(token)) != string::npos)
|
||||||
{
|
{
|
||||||
@@ -242,7 +300,8 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th
|
|||||||
{
|
{
|
||||||
st->epSquare = make_square(File(col - 'a'), Rank(row - '1'));
|
st->epSquare = make_square(File(col - 'a'), Rank(row - '1'));
|
||||||
|
|
||||||
if (!(attackers_to(st->epSquare) & pieces(sideToMove, PAWN)))
|
if ( !(attackers_to(st->epSquare) & pieces(sideToMove, PAWN))
|
||||||
|
|| !(pieces(~sideToMove, PAWN) & (st->epSquare + pawn_push(~sideToMove))))
|
||||||
st->epSquare = SQ_NONE;
|
st->epSquare = SQ_NONE;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -251,7 +310,7 @@ Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Th
|
|||||||
// 5-6. Halfmove clock and fullmove number
|
// 5-6. Halfmove clock and fullmove number
|
||||||
ss >> std::skipws >> st->rule50 >> gamePly;
|
ss >> std::skipws >> st->rule50 >> gamePly;
|
||||||
|
|
||||||
// Convert from fullmove starting from 1 to ply starting from 0,
|
// Convert from fullmove starting from 1 to gamePly starting from 0,
|
||||||
// handle also common incorrect FEN with fullmove = 0.
|
// handle also common incorrect FEN with fullmove = 0.
|
||||||
gamePly = std::max(2 * (gamePly - 1), 0) + (sideToMove == BLACK);
|
gamePly = std::max(2 * (gamePly - 1), 0) + (sideToMove == BLACK);
|
||||||
|
|
||||||
@@ -296,8 +355,8 @@ void Position::set_castling_right(Color c, Square rfrom) {
|
|||||||
|
|
||||||
void Position::set_check_info(StateInfo* si) const {
|
void Position::set_check_info(StateInfo* si) const {
|
||||||
|
|
||||||
si->blockersForKing[WHITE] = slider_blockers(pieces(BLACK), square<KING>(WHITE), si->pinnersForKing[WHITE]);
|
si->blockersForKing[WHITE] = slider_blockers(pieces(BLACK), square<KING>(WHITE), si->pinners[BLACK]);
|
||||||
si->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), square<KING>(BLACK), si->pinnersForKing[BLACK]);
|
si->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), square<KING>(BLACK), si->pinners[WHITE]);
|
||||||
|
|
||||||
Square ksq = square<KING>(~sideToMove);
|
Square ksq = square<KING>(~sideToMove);
|
||||||
|
|
||||||
@@ -317,9 +376,9 @@ void Position::set_check_info(StateInfo* si) const {
|
|||||||
|
|
||||||
void Position::set_state(StateInfo* si) const {
|
void Position::set_state(StateInfo* si) const {
|
||||||
|
|
||||||
si->key = si->pawnKey = si->materialKey = 0;
|
si->key = si->materialKey = 0;
|
||||||
|
si->pawnKey = Zobrist::noPawns;
|
||||||
si->nonPawnMaterial[WHITE] = si->nonPawnMaterial[BLACK] = VALUE_ZERO;
|
si->nonPawnMaterial[WHITE] = si->nonPawnMaterial[BLACK] = VALUE_ZERO;
|
||||||
si->psq = SCORE_ZERO;
|
|
||||||
si->checkersBB = attackers_to(square<KING>(sideToMove)) & pieces(~sideToMove);
|
si->checkersBB = attackers_to(square<KING>(sideToMove)) & pieces(~sideToMove);
|
||||||
|
|
||||||
set_check_info(si);
|
set_check_info(si);
|
||||||
@@ -329,7 +388,6 @@ void Position::set_state(StateInfo* si) const {
|
|||||||
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];
|
||||||
si->psq += PSQT::psq[pc][s];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (si->epSquare != SQ_NONE)
|
if (si->epSquare != SQ_NONE)
|
||||||
@@ -357,6 +415,27 @@ void Position::set_state(StateInfo* si) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Position::set() is an overload to initialize the position object with
|
||||||
|
/// the given endgame code string like "KBPKN". It is mainly a helper to
|
||||||
|
/// get the material key out of an endgame code.
|
||||||
|
|
||||||
|
Position& Position::set(const string& code, Color c, StateInfo* si) {
|
||||||
|
|
||||||
|
assert(code.length() > 0 && code.length() < 8);
|
||||||
|
assert(code[0] == 'K');
|
||||||
|
|
||||||
|
string sides[] = { code.substr(code.find('K', 1)), // Weak
|
||||||
|
code.substr(0, code.find('K', 1)) }; // Strong
|
||||||
|
|
||||||
|
std::transform(sides[c].begin(), sides[c].end(), sides[c].begin(), tolower);
|
||||||
|
|
||||||
|
string fenStr = "8/" + sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/"
|
||||||
|
+ sides[1] + char(8 - sides[1].length() + '0') + "/8 w - - 0 10";
|
||||||
|
|
||||||
|
return set(fenStr, false, si, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// 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.
|
||||||
|
|
||||||
@@ -407,19 +486,6 @@ const string Position::fen() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position::game_phase() calculates the game phase interpolating total non-pawn
|
|
||||||
/// material between endgame and midgame limits.
|
|
||||||
|
|
||||||
Phase Position::game_phase() const {
|
|
||||||
|
|
||||||
Value npm = st->nonPawnMaterial[WHITE] + st->nonPawnMaterial[BLACK];
|
|
||||||
|
|
||||||
npm = std::max(EndgameLimit, std::min(npm, MidgameLimit));
|
|
||||||
|
|
||||||
return Phase(((npm - EndgameLimit) * PHASE_MIDGAME) / (MidgameLimit - EndgameLimit));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Position::slider_blockers() returns a bitboard of all the pieces (both colors)
|
/// Position::slider_blockers() returns a bitboard of all the pieces (both colors)
|
||||||
/// that are blocking attacks on the square 's' from 'sliders'. A piece blocks a
|
/// that are blocking attacks on the square 's' from 'sliders'. A piece blocks a
|
||||||
/// slider if removing that piece from the board would result in a position where
|
/// slider if removing that piece from the board would result in a position where
|
||||||
@@ -429,11 +495,11 @@ Phase Position::game_phase() const {
|
|||||||
|
|
||||||
Bitboard Position::slider_blockers(Bitboard sliders, Square s, Bitboard& pinners) const {
|
Bitboard Position::slider_blockers(Bitboard sliders, Square s, Bitboard& pinners) const {
|
||||||
|
|
||||||
Bitboard result = 0;
|
Bitboard blockers = 0;
|
||||||
pinners = 0;
|
pinners = 0;
|
||||||
|
|
||||||
// Snipers are sliders that attack 's' when a piece is removed
|
// Snipers are sliders that attack 's' when a piece is removed
|
||||||
Bitboard snipers = ( (PseudoAttacks[ROOK ][s] & pieces(QUEEN, ROOK))
|
Bitboard snipers = ( (PseudoAttacks[ ROOK][s] & pieces(QUEEN, ROOK))
|
||||||
| (PseudoAttacks[BISHOP][s] & pieces(QUEEN, BISHOP))) & sliders;
|
| (PseudoAttacks[BISHOP][s] & pieces(QUEEN, BISHOP))) & sliders;
|
||||||
|
|
||||||
while (snipers)
|
while (snipers)
|
||||||
@@ -441,14 +507,14 @@ Bitboard Position::slider_blockers(Bitboard sliders, Square s, Bitboard& pinners
|
|||||||
Square sniperSq = pop_lsb(&snipers);
|
Square sniperSq = pop_lsb(&snipers);
|
||||||
Bitboard b = between_bb(s, sniperSq) & pieces();
|
Bitboard b = between_bb(s, sniperSq) & pieces();
|
||||||
|
|
||||||
if (!more_than_one(b))
|
if (b && !more_than_one(b))
|
||||||
{
|
{
|
||||||
result |= b;
|
blockers |= b;
|
||||||
if (b & pieces(color_of(piece_on(s))))
|
if (b & pieces(color_of(piece_on(s))))
|
||||||
pinners |= sniperSq;
|
pinners |= sniperSq;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return blockers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -460,7 +526,7 @@ Bitboard Position::attackers_to(Square s, Bitboard occupied) const {
|
|||||||
return (attacks_from<PAWN>(s, BLACK) & pieces(WHITE, PAWN))
|
return (attacks_from<PAWN>(s, BLACK) & pieces(WHITE, PAWN))
|
||||||
| (attacks_from<PAWN>(s, WHITE) & pieces(BLACK, PAWN))
|
| (attacks_from<PAWN>(s, WHITE) & pieces(BLACK, PAWN))
|
||||||
| (attacks_from<KNIGHT>(s) & pieces(KNIGHT))
|
| (attacks_from<KNIGHT>(s) & pieces(KNIGHT))
|
||||||
| (attacks_bb<ROOK >(s, occupied) & pieces(ROOK, QUEEN))
|
| (attacks_bb< ROOK>(s, occupied) & pieces( ROOK, QUEEN))
|
||||||
| (attacks_bb<BISHOP>(s, occupied) & pieces(BISHOP, QUEEN))
|
| (attacks_bb<BISHOP>(s, occupied) & pieces(BISHOP, QUEEN))
|
||||||
| (attacks_from<KING>(s) & pieces(KING));
|
| (attacks_from<KING>(s) & pieces(KING));
|
||||||
}
|
}
|
||||||
@@ -505,7 +571,7 @@ bool Position::legal(Move m) const {
|
|||||||
|
|
||||||
// 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.
|
||||||
return !(pinned_pieces(us) & from)
|
return !(blockers_for_king(us) & from)
|
||||||
|| aligned(from, to_sq(m), square<KING>(us));
|
|| aligned(from, to_sq(m), square<KING>(us));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -554,7 +620,7 @@ bool Position::pseudo_legal(const Move m) const {
|
|||||||
&& empty(to - pawn_push(us))))
|
&& empty(to - pawn_push(us))))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else if (!(attacks_from(pc, from) & to))
|
else if (!(attacks_from(type_of(pc), from) & to))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Evasions generator already takes care to avoid some kind of illegal moves
|
// Evasions generator already takes care to avoid some kind of illegal moves
|
||||||
@@ -597,7 +663,7 @@ bool Position::gives_check(Move m) const {
|
|||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Is there a discovered check?
|
// Is there a discovered check?
|
||||||
if ( (discovered_check_candidates() & from)
|
if ( (st->blockersForKing[~sideToMove] & from)
|
||||||
&& !aligned(from, to, square<KING>(~sideToMove)))
|
&& !aligned(from, to, square<KING>(~sideToMove)))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@@ -607,7 +673,7 @@ bool Position::gives_check(Move m) const {
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
case PROMOTION:
|
case PROMOTION:
|
||||||
return attacks_bb(Piece(promotion_type(m)), to, pieces() ^ from) & square<KING>(~sideToMove);
|
return attacks_bb(promotion_type(m), to, pieces() ^ from) & square<KING>(~sideToMove);
|
||||||
|
|
||||||
// En passant capture with check? We have already handled the case
|
// En passant capture with check? We have already handled the case
|
||||||
// of direct checks and ordinary discovered check, so the only case we
|
// of direct checks and ordinary discovered check, so the only case we
|
||||||
@@ -647,7 +713,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
|
|||||||
assert(is_ok(m));
|
assert(is_ok(m));
|
||||||
assert(&newSt != st);
|
assert(&newSt != st);
|
||||||
|
|
||||||
++nodes;
|
thisThread->nodes.fetch_add(1, std::memory_order_relaxed);
|
||||||
Key k = st->key ^ Zobrist::side;
|
Key k = st->key ^ Zobrist::side;
|
||||||
|
|
||||||
// Copy some fields of the old state to our new StateInfo object except the
|
// Copy some fields of the old state to our new StateInfo object except the
|
||||||
@@ -682,7 +748,6 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
|
|||||||
Square rfrom, rto;
|
Square rfrom, rto;
|
||||||
do_castling<true>(us, from, to, rfrom, rto);
|
do_castling<true>(us, from, to, rfrom, rto);
|
||||||
|
|
||||||
st->psq += PSQT::psq[captured][rto] - PSQT::psq[captured][rfrom];
|
|
||||||
k ^= Zobrist::psq[captured][rfrom] ^ Zobrist::psq[captured][rto];
|
k ^= Zobrist::psq[captured][rfrom] ^ Zobrist::psq[captured][rto];
|
||||||
captured = NO_PIECE;
|
captured = NO_PIECE;
|
||||||
}
|
}
|
||||||
@@ -721,9 +786,6 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
|
|||||||
st->materialKey ^= Zobrist::psq[captured][pieceCount[captured]];
|
st->materialKey ^= Zobrist::psq[captured][pieceCount[captured]];
|
||||||
prefetch(thisThread->materialTable[st->materialKey]);
|
prefetch(thisThread->materialTable[st->materialKey]);
|
||||||
|
|
||||||
// Update incremental scores
|
|
||||||
st->psq -= PSQT::psq[captured][capsq];
|
|
||||||
|
|
||||||
// Reset rule 50 counter
|
// Reset rule 50 counter
|
||||||
st->rule50 = 0;
|
st->rule50 = 0;
|
||||||
}
|
}
|
||||||
@@ -757,7 +819,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
|
|||||||
if ( (int(to) ^ int(from)) == 16
|
if ( (int(to) ^ int(from)) == 16
|
||||||
&& (attacks_from<PAWN>(to - pawn_push(us), us) & pieces(them, PAWN)))
|
&& (attacks_from<PAWN>(to - pawn_push(us), us) & pieces(them, PAWN)))
|
||||||
{
|
{
|
||||||
st->epSquare = (from + to) / 2;
|
st->epSquare = to - pawn_push(us);
|
||||||
k ^= Zobrist::enpassant[file_of(st->epSquare)];
|
k ^= Zobrist::enpassant[file_of(st->epSquare)];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -777,24 +839,18 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
|
|||||||
st->materialKey ^= Zobrist::psq[promotion][pieceCount[promotion]-1]
|
st->materialKey ^= Zobrist::psq[promotion][pieceCount[promotion]-1]
|
||||||
^ Zobrist::psq[pc][pieceCount[pc]];
|
^ Zobrist::psq[pc][pieceCount[pc]];
|
||||||
|
|
||||||
// Update incremental score
|
|
||||||
st->psq += PSQT::psq[promotion][to] - PSQT::psq[pc][to];
|
|
||||||
|
|
||||||
// Update material
|
// Update material
|
||||||
st->nonPawnMaterial[us] += PieceValue[MG][promotion];
|
st->nonPawnMaterial[us] += PieceValue[MG][promotion];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update pawn hash key and prefetch access to pawnsTable
|
// Update pawn hash key and prefetch access to pawnsTable
|
||||||
st->pawnKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];
|
st->pawnKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];
|
||||||
prefetch(thisThread->pawnsTable[st->pawnKey]);
|
prefetch2(thisThread->pawnsTable[st->pawnKey]);
|
||||||
|
|
||||||
// Reset rule 50 draw counter
|
// Reset rule 50 draw counter
|
||||||
st->rule50 = 0;
|
st->rule50 = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update incremental scores
|
|
||||||
st->psq += PSQT::psq[pc][to] - PSQT::psq[pc][from];
|
|
||||||
|
|
||||||
// Set capture piece
|
// Set capture piece
|
||||||
st->capturedPiece = captured;
|
st->capturedPiece = captured;
|
||||||
|
|
||||||
@@ -956,106 +1012,201 @@ Key Position::key_after(Move m) const {
|
|||||||
|
|
||||||
|
|
||||||
/// Position::see_ge (Static Exchange Evaluation Greater or Equal) tests if the
|
/// Position::see_ge (Static Exchange Evaluation Greater or Equal) tests if the
|
||||||
/// SEE value of move is greater or equal to the given value. We'll use an
|
/// SEE value of move is greater or equal to the given threshold. We'll use an
|
||||||
/// algorithm similar to alpha-beta pruning with a null window.
|
/// algorithm similar to alpha-beta pruning with a null window.
|
||||||
|
|
||||||
bool Position::see_ge(Move m, Value v) const {
|
bool Position::see_ge(Move m, Value threshold) const {
|
||||||
|
|
||||||
assert(is_ok(m));
|
assert(is_ok(m));
|
||||||
|
|
||||||
// Castling moves are implemented as king capturing the rook so cannot be
|
// Only deal with normal moves, assume others pass a simple see
|
||||||
// handled correctly. Simply assume the SEE value is VALUE_ZERO that is always
|
if (type_of(m) != NORMAL)
|
||||||
// correct unless in the rare case the rook ends up under attack.
|
return VALUE_ZERO >= threshold;
|
||||||
if (type_of(m) == CASTLING)
|
|
||||||
return VALUE_ZERO >= v;
|
|
||||||
|
|
||||||
|
Bitboard stmAttackers;
|
||||||
Square from = from_sq(m), to = to_sq(m);
|
Square from = from_sq(m), to = to_sq(m);
|
||||||
PieceType nextVictim = type_of(piece_on(from));
|
PieceType nextVictim = type_of(piece_on(from));
|
||||||
Color stm = ~color_of(piece_on(from)); // First consider opponent's move
|
Color us = color_of(piece_on(from));
|
||||||
Value balance; // Values of the pieces taken by us minus opponent's ones
|
Color stm = ~us; // First consider opponent's move
|
||||||
Bitboard occupied, stmAttackers;
|
Value balance; // Values of the pieces taken by us minus opponent's ones
|
||||||
|
|
||||||
if (type_of(m) == ENPASSANT)
|
// The opponent may be able to recapture so this is the best result
|
||||||
{
|
// we can hope for.
|
||||||
occupied = SquareBB[to - pawn_push(~stm)]; // Remove the captured pawn
|
balance = PieceValue[MG][piece_on(to)] - threshold;
|
||||||
balance = PieceValue[MG][PAWN];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
balance = PieceValue[MG][piece_on(to)];
|
|
||||||
occupied = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (balance < v)
|
if (balance < VALUE_ZERO)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (nextVictim == KING)
|
// Now assume the worst possible result: that the opponent can
|
||||||
return true;
|
// capture our piece for free.
|
||||||
|
|
||||||
balance -= PieceValue[MG][nextVictim];
|
balance -= PieceValue[MG][nextVictim];
|
||||||
|
|
||||||
if (balance >= v)
|
// If it is enough (like in PxQ) then return immediately. Note that
|
||||||
|
// in case nextVictim == KING we always return here, this is ok
|
||||||
|
// if the given move is legal.
|
||||||
|
if (balance >= VALUE_ZERO)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
bool relativeStm = true; // True if the opponent is to move
|
// Find all attackers to the destination square, with the moving piece
|
||||||
occupied ^= pieces() ^ from ^ to;
|
// removed, but possibly an X-ray attacker added behind it.
|
||||||
|
Bitboard occupied = pieces() ^ from ^ to;
|
||||||
// Find all attackers to the destination square, with the moving piece removed,
|
|
||||||
// but possibly an X-ray attacker added behind it.
|
|
||||||
Bitboard attackers = attackers_to(to, occupied) & occupied;
|
Bitboard attackers = attackers_to(to, occupied) & occupied;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
stmAttackers = attackers & pieces(stm);
|
stmAttackers = attackers & pieces(stm);
|
||||||
|
|
||||||
// Don't allow pinned pieces to attack pieces except the king as long all
|
// Don't allow pinned pieces to attack (except the king) as long as
|
||||||
// pinners are on their original square.
|
// all pinners are on their original square.
|
||||||
if (!(st->pinnersForKing[stm] & ~occupied))
|
if (!(st->pinners[~stm] & ~occupied))
|
||||||
stmAttackers &= ~st->blockersForKing[stm];
|
stmAttackers &= ~st->blockersForKing[stm];
|
||||||
|
|
||||||
|
// If stm has no more attackers then give up: stm loses
|
||||||
if (!stmAttackers)
|
if (!stmAttackers)
|
||||||
return relativeStm;
|
break;
|
||||||
|
|
||||||
// Locate and remove the next least valuable attacker
|
// Locate and remove the next least valuable attacker, and add to
|
||||||
|
// the bitboard 'attackers' the possibly X-ray attackers behind it.
|
||||||
nextVictim = min_attacker<PAWN>(byTypeBB, to, stmAttackers, occupied, attackers);
|
nextVictim = min_attacker<PAWN>(byTypeBB, to, stmAttackers, occupied, attackers);
|
||||||
|
|
||||||
if (nextVictim == KING)
|
stm = ~stm; // Switch side to move
|
||||||
return relativeStm == bool(attackers & pieces(~stm));
|
|
||||||
|
|
||||||
balance += relativeStm ? PieceValue[MG][nextVictim]
|
// Negamax the balance with alpha = balance, beta = balance+1 and
|
||||||
: -PieceValue[MG][nextVictim];
|
// add nextVictim's value.
|
||||||
|
//
|
||||||
|
// (balance, balance+1) -> (-balance-1, -balance)
|
||||||
|
//
|
||||||
|
assert(balance < VALUE_ZERO);
|
||||||
|
|
||||||
relativeStm = !relativeStm;
|
balance = -balance - 1 - PieceValue[MG][nextVictim];
|
||||||
|
|
||||||
if (relativeStm == (balance >= v))
|
// If balance is still non-negative after giving away nextVictim then we
|
||||||
return relativeStm;
|
// win. The only thing to be careful about it is that we should revert
|
||||||
|
// stm if we captured with the king when the opponent still has attackers.
|
||||||
stm = ~stm;
|
if (balance >= VALUE_ZERO)
|
||||||
|
{
|
||||||
|
if (nextVictim == KING && (attackers & pieces(stm)))
|
||||||
|
stm = ~stm;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
assert(nextVictim != KING);
|
||||||
}
|
}
|
||||||
|
return us != stm; // We break the above loop when stm loses
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position::is_draw() tests whether the position is drawn by 50-move rule
|
/// Position::is_draw() tests whether the position is drawn by 50-move rule
|
||||||
/// or by repetition. It does not detect stalemates.
|
/// or by repetition. It does not detect stalemates.
|
||||||
|
|
||||||
bool Position::is_draw() const {
|
bool Position::is_draw(int ply) const {
|
||||||
|
|
||||||
if (st->rule50 > 99 && (!checkers() || MoveList<LEGAL>(*this).size()))
|
if (st->rule50 > 99 && (!checkers() || MoveList<LEGAL>(*this).size()))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
StateInfo* stp = st;
|
int end = std::min(st->rule50, st->pliesFromNull);
|
||||||
for (int i = 2, e = std::min(st->rule50, st->pliesFromNull); i <= e; i += 2)
|
|
||||||
|
if (end < 4)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
StateInfo* stp = st->previous->previous;
|
||||||
|
int cnt = 0;
|
||||||
|
|
||||||
|
for (int i = 4; i <= end; i += 2)
|
||||||
{
|
{
|
||||||
stp = stp->previous->previous;
|
stp = stp->previous->previous;
|
||||||
|
|
||||||
if (stp->key == st->key)
|
// Return a draw score if a position repeats once earlier but strictly
|
||||||
return true; // Draw at first repetition
|
// after the root, or repeats twice before or at the root.
|
||||||
|
if ( stp->key == st->key
|
||||||
|
&& ++cnt + (ply > i) == 2)
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Position::has_repeated() tests whether there has been at least one repetition
|
||||||
|
// of positions since the last capture or pawn move.
|
||||||
|
|
||||||
|
bool Position::has_repeated() const {
|
||||||
|
|
||||||
|
StateInfo* stc = st;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
int i = 4, end = std::min(stc->rule50, stc->pliesFromNull);
|
||||||
|
|
||||||
|
if (end < i)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
StateInfo* stp = stc->previous->previous;
|
||||||
|
|
||||||
|
do {
|
||||||
|
stp = stp->previous->previous;
|
||||||
|
|
||||||
|
if (stp->key == stc->key)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
i += 2;
|
||||||
|
} while (i <= end);
|
||||||
|
|
||||||
|
stc = stc->previous;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Position::has_game_cycle() tests if the position has a move which draws by repetition,
|
||||||
|
/// or an earlier position has a move that directly reaches the current position.
|
||||||
|
|
||||||
|
bool Position::has_game_cycle(int ply) const {
|
||||||
|
|
||||||
|
int j;
|
||||||
|
|
||||||
|
int end = std::min(st->rule50, st->pliesFromNull);
|
||||||
|
|
||||||
|
if (end < 3)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
Key originalKey = st->key;
|
||||||
|
StateInfo* stp = st->previous;
|
||||||
|
|
||||||
|
for (int i = 3; i <= end; i += 2)
|
||||||
|
{
|
||||||
|
stp = stp->previous->previous;
|
||||||
|
|
||||||
|
Key moveKey = originalKey ^ stp->key;
|
||||||
|
if ( (j = H1(moveKey), cuckoo[j] == moveKey)
|
||||||
|
|| (j = H2(moveKey), cuckoo[j] == moveKey))
|
||||||
|
{
|
||||||
|
Move move = cuckooMove[j];
|
||||||
|
Square s1 = from_sq(move);
|
||||||
|
Square s2 = to_sq(move);
|
||||||
|
|
||||||
|
if (!(between_bb(s1, s2) & pieces()))
|
||||||
|
{
|
||||||
|
// In the cuckoo table, both moves Rc1c5 and Rc5c1 are stored in the same
|
||||||
|
// location. We select the legal one by reversing the move variable if necessary.
|
||||||
|
if (empty(s1))
|
||||||
|
move = make_move(s2, s1);
|
||||||
|
|
||||||
|
if (ply > i)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// For repetitions before or at the root, require one more
|
||||||
|
StateInfo* next_stp = stp;
|
||||||
|
for (int k = i + 2; k <= end; k += 2)
|
||||||
|
{
|
||||||
|
next_stp = next_stp->previous->previous;
|
||||||
|
if (next_stp->key == stp->key)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position::flip() flips position with the white and black sides reversed. This
|
/// Position::flip() flips position with the white and black sides reversed. This
|
||||||
/// is only useful for debugging e.g. for finding evaluation symmetry bugs.
|
/// is only useful for debugging e.g. for finding evaluation symmetry bugs.
|
||||||
|
|
||||||
@@ -1091,78 +1242,72 @@ void Position::flip() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position::pos_is_ok() performs some consistency checks for the position object.
|
/// Position::pos_is_ok() performs some consistency checks for the
|
||||||
|
/// position object and raises an asserts if something wrong is detected.
|
||||||
/// This is meant to be helpful when debugging.
|
/// This is meant to be helpful when debugging.
|
||||||
|
|
||||||
bool Position::pos_is_ok(int* failedStep) const {
|
bool Position::pos_is_ok() const {
|
||||||
|
|
||||||
const bool Fast = true; // Quick (default) or full check?
|
constexpr bool Fast = true; // Quick (default) or full check?
|
||||||
|
|
||||||
enum { Default, King, Bitboards, State, Lists, Castling };
|
if ( (sideToMove != WHITE && sideToMove != BLACK)
|
||||||
|
|| piece_on(square<KING>(WHITE)) != W_KING
|
||||||
|
|| piece_on(square<KING>(BLACK)) != B_KING
|
||||||
|
|| ( ep_square() != SQ_NONE
|
||||||
|
&& relative_rank(sideToMove, ep_square()) != RANK_6))
|
||||||
|
assert(0 && "pos_is_ok: Default");
|
||||||
|
|
||||||
for (int step = Default; step <= (Fast ? Default : Castling); step++)
|
if (Fast)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if ( pieceCount[W_KING] != 1
|
||||||
|
|| pieceCount[B_KING] != 1
|
||||||
|
|| attackers_to(square<KING>(~sideToMove)) & pieces(sideToMove))
|
||||||
|
assert(0 && "pos_is_ok: Kings");
|
||||||
|
|
||||||
|
if ( (pieces(PAWN) & (Rank1BB | Rank8BB))
|
||||||
|
|| pieceCount[W_PAWN] > 8
|
||||||
|
|| pieceCount[B_PAWN] > 8)
|
||||||
|
assert(0 && "pos_is_ok: Pawns");
|
||||||
|
|
||||||
|
if ( (pieces(WHITE) & pieces(BLACK))
|
||||||
|
|| (pieces(WHITE) | pieces(BLACK)) != pieces()
|
||||||
|
|| popcount(pieces(WHITE)) > 16
|
||||||
|
|| popcount(pieces(BLACK)) > 16)
|
||||||
|
assert(0 && "pos_is_ok: Bitboards");
|
||||||
|
|
||||||
|
for (PieceType p1 = PAWN; p1 <= KING; ++p1)
|
||||||
|
for (PieceType p2 = PAWN; p2 <= KING; ++p2)
|
||||||
|
if (p1 != p2 && (pieces(p1) & pieces(p2)))
|
||||||
|
assert(0 && "pos_is_ok: Bitboards");
|
||||||
|
|
||||||
|
StateInfo si = *st;
|
||||||
|
set_state(&si);
|
||||||
|
if (std::memcmp(&si, st, sizeof(StateInfo)))
|
||||||
|
assert(0 && "pos_is_ok: State");
|
||||||
|
|
||||||
|
for (Piece pc : Pieces)
|
||||||
{
|
{
|
||||||
if (failedStep)
|
if ( pieceCount[pc] != popcount(pieces(color_of(pc), type_of(pc)))
|
||||||
*failedStep = step;
|
|| pieceCount[pc] != std::count(board, board + SQUARE_NB, pc))
|
||||||
|
assert(0 && "pos_is_ok: Pieces");
|
||||||
|
|
||||||
if (step == Default)
|
for (int i = 0; i < pieceCount[pc]; ++i)
|
||||||
if ( (sideToMove != WHITE && sideToMove != BLACK)
|
if (board[pieceList[pc][i]] != pc || index[pieceList[pc][i]] != i)
|
||||||
|| piece_on(square<KING>(WHITE)) != W_KING
|
assert(0 && "pos_is_ok: Index");
|
||||||
|| piece_on(square<KING>(BLACK)) != B_KING
|
|
||||||
|| ( ep_square() != SQ_NONE
|
|
||||||
&& relative_rank(sideToMove, ep_square()) != RANK_6))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (step == King)
|
|
||||||
if ( std::count(board, board + SQUARE_NB, W_KING) != 1
|
|
||||||
|| std::count(board, board + SQUARE_NB, B_KING) != 1
|
|
||||||
|| attackers_to(square<KING>(~sideToMove)) & pieces(sideToMove))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (step == Bitboards)
|
|
||||||
{
|
|
||||||
if ( (pieces(WHITE) & pieces(BLACK))
|
|
||||||
||(pieces(WHITE) | pieces(BLACK)) != pieces())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
for (PieceType p1 = PAWN; p1 <= KING; ++p1)
|
|
||||||
for (PieceType p2 = PAWN; p2 <= KING; ++p2)
|
|
||||||
if (p1 != p2 && (pieces(p1) & pieces(p2)))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (step == State)
|
|
||||||
{
|
|
||||||
StateInfo si = *st;
|
|
||||||
set_state(&si);
|
|
||||||
if (std::memcmp(&si, st, sizeof(StateInfo)))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (step == Lists)
|
|
||||||
for (Piece pc : Pieces)
|
|
||||||
{
|
|
||||||
if (pieceCount[pc] != popcount(pieces(color_of(pc), type_of(pc))))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
for (int i = 0; i < pieceCount[pc]; ++i)
|
|
||||||
if (board[pieceList[pc][i]] != pc || index[pieceList[pc][i]] != i)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (step == Castling)
|
|
||||||
for (Color c = WHITE; c <= BLACK; ++c)
|
|
||||||
for (CastlingSide s = KING_SIDE; s <= QUEEN_SIDE; s = CastlingSide(s + 1))
|
|
||||||
{
|
|
||||||
if (!can_castle(c | s))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if ( piece_on(castlingRookSquare[c | s]) != make_piece(c, ROOK)
|
|
||||||
|| castlingRightsMask[castlingRookSquare[c | s]] != (c | s)
|
|
||||||
||(castlingRightsMask[square<KING>(c)] & (c | s)) != (c | s))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (Color c = WHITE; c <= BLACK; ++c)
|
||||||
|
for (CastlingSide s = KING_SIDE; s <= QUEEN_SIDE; s = CastlingSide(s + 1))
|
||||||
|
{
|
||||||
|
if (!can_castle(c | s))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if ( piece_on(castlingRookSquare[c | s]) != make_piece(c, ROOK)
|
||||||
|
|| castlingRightsMask[castlingRookSquare[c | s]] != (c | s)
|
||||||
|
|| (castlingRightsMask[square<KING>(c)] & (c | s)) != (c | s))
|
||||||
|
assert(0 && "pos_is_ok: Castling");
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
+51
-34
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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
|
||||||
@@ -43,7 +43,6 @@ struct StateInfo {
|
|||||||
int castlingRights;
|
int castlingRights;
|
||||||
int rule50;
|
int rule50;
|
||||||
int pliesFromNull;
|
int pliesFromNull;
|
||||||
Score psq;
|
|
||||||
Square epSquare;
|
Square epSquare;
|
||||||
|
|
||||||
// Not copied when making a move (will be recomputed anyhow)
|
// Not copied when making a move (will be recomputed anyhow)
|
||||||
@@ -52,11 +51,14 @@ struct StateInfo {
|
|||||||
Piece capturedPiece;
|
Piece capturedPiece;
|
||||||
StateInfo* previous;
|
StateInfo* previous;
|
||||||
Bitboard blockersForKing[COLOR_NB];
|
Bitboard blockersForKing[COLOR_NB];
|
||||||
Bitboard pinnersForKing[COLOR_NB];
|
Bitboard pinners[COLOR_NB];
|
||||||
Bitboard checkSquares[PIECE_TYPE_NB];
|
Bitboard checkSquares[PIECE_TYPE_NB];
|
||||||
};
|
};
|
||||||
|
|
||||||
// In a std::deque references to elements are unaffected upon resizing
|
/// A list to keep track of the position states along the setup moves (from the
|
||||||
|
/// start position to the position just before the search starts). Needed by
|
||||||
|
/// 'draw by repetition' detection. Use a std::deque because pointers to
|
||||||
|
/// elements are not invalidated upon list resizing.
|
||||||
typedef std::unique_ptr<std::deque<StateInfo>> StateListPtr;
|
typedef std::unique_ptr<std::deque<StateInfo>> StateListPtr;
|
||||||
|
|
||||||
|
|
||||||
@@ -76,6 +78,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);
|
||||||
const std::string fen() const;
|
const std::string fen() const;
|
||||||
|
|
||||||
// Position representation
|
// Position representation
|
||||||
@@ -89,6 +92,7 @@ public:
|
|||||||
Square ep_square() const;
|
Square ep_square() const;
|
||||||
bool empty(Square s) const;
|
bool empty(Square s) const;
|
||||||
template<PieceType Pt> int count(Color c) const;
|
template<PieceType Pt> int count(Color c) const;
|
||||||
|
template<PieceType Pt> int count() const;
|
||||||
template<PieceType Pt> const Square* squares(Color c) const;
|
template<PieceType Pt> const Square* squares(Color c) const;
|
||||||
template<PieceType Pt> Square square(Color c) const;
|
template<PieceType Pt> Square square(Color c) const;
|
||||||
|
|
||||||
@@ -100,14 +104,13 @@ public:
|
|||||||
|
|
||||||
// Checking
|
// Checking
|
||||||
Bitboard checkers() const;
|
Bitboard checkers() const;
|
||||||
Bitboard discovered_check_candidates() const;
|
Bitboard blockers_for_king(Color c) const;
|
||||||
Bitboard pinned_pieces(Color c) const;
|
|
||||||
Bitboard check_squares(PieceType pt) const;
|
Bitboard check_squares(PieceType pt) 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 attacks_from(Piece pc, Square s) const;
|
Bitboard attacks_from(PieceType pt, Square s) const;
|
||||||
template<PieceType> Bitboard attacks_from(Square s) const;
|
template<PieceType> Bitboard attacks_from(Square s) const;
|
||||||
template<PieceType> Bitboard attacks_from(Square s, Color c) const;
|
template<PieceType> Bitboard attacks_from(Square s, Color c) const;
|
||||||
Bitboard slider_blockers(Bitboard sliders, Square s, Bitboard& pinners) const;
|
Bitboard slider_blockers(Bitboard sliders, Square s, Bitboard& pinners) const;
|
||||||
@@ -127,13 +130,14 @@ public:
|
|||||||
bool opposite_bishops() const;
|
bool opposite_bishops() const;
|
||||||
|
|
||||||
// Doing and undoing moves
|
// Doing and undoing moves
|
||||||
void do_move(Move m, StateInfo& st, bool givesCheck);
|
void do_move(Move m, StateInfo& newSt);
|
||||||
|
void do_move(Move m, StateInfo& newSt, bool givesCheck);
|
||||||
void undo_move(Move m);
|
void undo_move(Move m);
|
||||||
void do_null_move(StateInfo& st);
|
void do_null_move(StateInfo& newSt);
|
||||||
void undo_null_move();
|
void undo_null_move();
|
||||||
|
|
||||||
// Static Exchange Evaluation
|
// Static Exchange Evaluation
|
||||||
bool see_ge(Move m, Value value) const;
|
bool see_ge(Move m, Value threshold = VALUE_ZERO) const;
|
||||||
|
|
||||||
// Accessing hash keys
|
// Accessing hash keys
|
||||||
Key key() const;
|
Key key() const;
|
||||||
@@ -143,18 +147,19 @@ public:
|
|||||||
|
|
||||||
// Other properties of the position
|
// Other properties of the position
|
||||||
Color side_to_move() const;
|
Color side_to_move() const;
|
||||||
Phase game_phase() const;
|
|
||||||
int game_ply() const;
|
int game_ply() const;
|
||||||
bool is_chess960() const;
|
bool is_chess960() const;
|
||||||
Thread* this_thread() const;
|
Thread* this_thread() const;
|
||||||
uint64_t nodes_searched() const;
|
bool is_draw(int ply) const;
|
||||||
bool is_draw() const;
|
bool has_game_cycle(int ply) const;
|
||||||
|
bool has_repeated() const;
|
||||||
int rule50_count() const;
|
int rule50_count() const;
|
||||||
Score psq_score() const;
|
Score psq_score() const;
|
||||||
Value non_pawn_material(Color c) const;
|
Value non_pawn_material(Color c) const;
|
||||||
|
Value non_pawn_material() const;
|
||||||
|
|
||||||
// Position consistency check, for debugging
|
// Position consistency check, for debugging
|
||||||
bool pos_is_ok(int* failedStep = nullptr) const;
|
bool pos_is_ok() const;
|
||||||
void flip();
|
void flip();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -180,14 +185,18 @@ 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];
|
||||||
uint64_t nodes;
|
|
||||||
int gamePly;
|
int gamePly;
|
||||||
Color sideToMove;
|
Color sideToMove;
|
||||||
|
Score psq;
|
||||||
Thread* thisThread;
|
Thread* thisThread;
|
||||||
StateInfo* st;
|
StateInfo* st;
|
||||||
bool chess960;
|
bool chess960;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
namespace PSQT {
|
||||||
|
extern Score psq[PIECE_NB][SQUARE_NB];
|
||||||
|
}
|
||||||
|
|
||||||
extern std::ostream& operator<<(std::ostream& os, const Position& pos);
|
extern std::ostream& operator<<(std::ostream& os, const Position& pos);
|
||||||
|
|
||||||
inline Color Position::side_to_move() const {
|
inline Color Position::side_to_move() const {
|
||||||
@@ -234,6 +243,10 @@ template<PieceType Pt> inline int Position::count(Color c) const {
|
|||||||
return pieceCount[make_piece(c, Pt)];
|
return pieceCount[make_piece(c, Pt)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<PieceType Pt> inline int Position::count() const {
|
||||||
|
return pieceCount[make_piece(WHITE, Pt)] + pieceCount[make_piece(BLACK, Pt)];
|
||||||
|
}
|
||||||
|
|
||||||
template<PieceType Pt> inline const Square* Position::squares(Color c) const {
|
template<PieceType Pt> inline const Square* Position::squares(Color c) const {
|
||||||
return pieceList[make_piece(c, Pt)];
|
return pieceList[make_piece(c, Pt)];
|
||||||
}
|
}
|
||||||
@@ -265,18 +278,19 @@ inline Square Position::castling_rook_square(CastlingRight cr) const {
|
|||||||
|
|
||||||
template<PieceType Pt>
|
template<PieceType Pt>
|
||||||
inline Bitboard Position::attacks_from(Square s) const {
|
inline Bitboard Position::attacks_from(Square s) const {
|
||||||
|
assert(Pt != PAWN);
|
||||||
return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, byTypeBB[ALL_PIECES])
|
return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, byTypeBB[ALL_PIECES])
|
||||||
: Pt == QUEEN ? attacks_from<ROOK>(s) | attacks_from<BISHOP>(s)
|
: Pt == QUEEN ? attacks_from<ROOK>(s) | attacks_from<BISHOP>(s)
|
||||||
: StepAttacksBB[Pt][s];
|
: PseudoAttacks[Pt][s];
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
|
inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
|
||||||
return StepAttacksBB[make_piece(c, PAWN)][s];
|
return PawnAttacks[c][s];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard Position::attacks_from(Piece pc, Square s) const {
|
inline Bitboard Position::attacks_from(PieceType pt, Square s) const {
|
||||||
return attacks_bb(pc, s, byTypeBB[ALL_PIECES]);
|
return attacks_bb(pt, s, byTypeBB[ALL_PIECES]);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard Position::attackers_to(Square s) const {
|
inline Bitboard Position::attackers_to(Square s) const {
|
||||||
@@ -287,12 +301,8 @@ inline Bitboard Position::checkers() const {
|
|||||||
return st->checkersBB;
|
return st->checkersBB;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard Position::discovered_check_candidates() const {
|
inline Bitboard Position::blockers_for_king(Color c) const {
|
||||||
return st->blockersForKing[~sideToMove] & pieces(sideToMove);
|
return st->blockersForKing[c];
|
||||||
}
|
|
||||||
|
|
||||||
inline Bitboard Position::pinned_pieces(Color c) const {
|
|
||||||
return st->blockersForKing[c] & pieces(c);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard Position::check_squares(PieceType pt) const {
|
inline Bitboard Position::check_squares(PieceType pt) const {
|
||||||
@@ -321,13 +331,17 @@ inline Key Position::material_key() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline Score Position::psq_score() const {
|
inline Score Position::psq_score() const {
|
||||||
return st->psq;
|
return 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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Value Position::non_pawn_material() const {
|
||||||
|
return st->nonPawnMaterial[WHITE] + st->nonPawnMaterial[BLACK];
|
||||||
|
}
|
||||||
|
|
||||||
inline int Position::game_ply() const {
|
inline int Position::game_ply() const {
|
||||||
return gamePly;
|
return gamePly;
|
||||||
}
|
}
|
||||||
@@ -336,10 +350,6 @@ inline int Position::rule50_count() const {
|
|||||||
return st->rule50;
|
return st->rule50;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline uint64_t Position::nodes_searched() const {
|
|
||||||
return nodes;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool Position::opposite_bishops() const {
|
inline bool Position::opposite_bishops() const {
|
||||||
return pieceCount[W_BISHOP] == 1
|
return pieceCount[W_BISHOP] == 1
|
||||||
&& pieceCount[B_BISHOP] == 1
|
&& pieceCount[B_BISHOP] == 1
|
||||||
@@ -378,6 +388,7 @@ inline void Position::put_piece(Piece pc, Square s) {
|
|||||||
index[s] = pieceCount[pc]++;
|
index[s] = pieceCount[pc]++;
|
||||||
pieceList[pc][index[s]] = s;
|
pieceList[pc][index[s]] = s;
|
||||||
pieceCount[make_piece(color_of(pc), ALL_PIECES)]++;
|
pieceCount[make_piece(color_of(pc), ALL_PIECES)]++;
|
||||||
|
psq += PSQT::psq[pc][s];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Position::remove_piece(Piece pc, Square s) {
|
inline void Position::remove_piece(Piece pc, Square s) {
|
||||||
@@ -395,20 +406,26 @@ inline void Position::remove_piece(Piece pc, Square s) {
|
|||||||
pieceList[pc][index[lastSquare]] = lastSquare;
|
pieceList[pc][index[lastSquare]] = lastSquare;
|
||||||
pieceList[pc][pieceCount[pc]] = SQ_NONE;
|
pieceList[pc][pieceCount[pc]] = SQ_NONE;
|
||||||
pieceCount[make_piece(color_of(pc), ALL_PIECES)]--;
|
pieceCount[make_piece(color_of(pc), ALL_PIECES)]--;
|
||||||
|
psq -= PSQT::psq[pc][s];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Position::move_piece(Piece pc, Square from, Square to) {
|
inline void Position::move_piece(Piece pc, Square from, Square to) {
|
||||||
|
|
||||||
// index[from] is not updated and becomes stale. This works as long as index[]
|
// index[from] is not updated and becomes stale. This works as long as index[]
|
||||||
// is accessed just by known occupied squares.
|
// is accessed just by known occupied squares.
|
||||||
Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
|
Bitboard fromTo = SquareBB[from] ^ SquareBB[to];
|
||||||
byTypeBB[ALL_PIECES] ^= from_to_bb;
|
byTypeBB[ALL_PIECES] ^= fromTo;
|
||||||
byTypeBB[type_of(pc)] ^= from_to_bb;
|
byTypeBB[type_of(pc)] ^= fromTo;
|
||||||
byColorBB[color_of(pc)] ^= from_to_bb;
|
byColorBB[color_of(pc)] ^= fromTo;
|
||||||
board[from] = NO_PIECE;
|
board[from] = NO_PIECE;
|
||||||
board[to] = pc;
|
board[to] = pc;
|
||||||
index[to] = index[from];
|
index[to] = index[from];
|
||||||
pieceList[pc][index[to]] = to;
|
pieceList[pc][index[to]] = to;
|
||||||
|
psq += PSQT::psq[pc][to] - PSQT::psq[pc][from];
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void Position::do_move(Move m, StateInfo& newSt) {
|
||||||
|
do_move(m, newSt, gives_check(m));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // #ifndef POSITION_H_INCLUDED
|
#endif // #ifndef POSITION_H_INCLUDED
|
||||||
|
|||||||
+52
-52
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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
|
||||||
@@ -35,66 +35,66 @@ namespace PSQT {
|
|||||||
// type on a given square a (middlegame, endgame) score pair is assigned. Table
|
// type on a given square a (middlegame, endgame) score pair is assigned. Table
|
||||||
// is defined for files A..D and white side: it is symmetric for black side and
|
// is defined for files A..D and white side: it is symmetric for black side and
|
||||||
// second half of the files.
|
// second half of the files.
|
||||||
const Score Bonus[][RANK_NB][int(FILE_NB) / 2] = {
|
constexpr Score Bonus[][RANK_NB][int(FILE_NB) / 2] = {
|
||||||
{ },
|
{ },
|
||||||
{ // Pawn
|
{ // Pawn
|
||||||
{ S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0) },
|
{ S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0) },
|
||||||
{ S(-16, 7), S( 1,-4), S( 7, 8), S( 3,-2) },
|
{ S(-11,-3), S( 7, -1), S( 7, 7), S(17, 2) },
|
||||||
{ S(-23,-4), S( -7,-5), S( 19, 5), S(24, 4) },
|
{ S(-16,-2), S( -3, 2), S( 23, 6), S(23,-1) },
|
||||||
{ S(-22, 3), S(-14, 3), S( 20,-8), S(35,-3) },
|
{ S(-14, 7), S( -7, -4), S( 20,-8), S(24, 2) },
|
||||||
{ S(-11, 8), S( 0, 9), S( 3, 7), S(21,-6) },
|
{ S( -5,13), S( -2, 10), S( -1,-1), S(12,-8) },
|
||||||
{ S(-11, 8), S(-13,-5), S( -6, 2), S(-2, 4) },
|
{ S(-11,16), S(-12, 6), S( -2, 1), S( 4,16) },
|
||||||
{ S( -9, 3), S( 15,-9), S( -8, 1), S(-4,18) }
|
{ S( -2, 1), S( 20,-12), S(-10, 6), S(-2,25) }
|
||||||
},
|
},
|
||||||
{ // Knight
|
{ // Knight
|
||||||
{ S(-143, -97), S(-96,-82), S(-80,-46), S(-73,-14) },
|
{ S(-169,-105), S(-96,-74), S(-80,-46), S(-79,-18) },
|
||||||
{ S( -83, -69), S(-43,-55), S(-21,-17), S(-10, 9) },
|
{ S( -79, -70), S(-39,-56), S(-24,-15), S( -9, 6) },
|
||||||
{ S( -71, -50), S(-22,-39), S( 0, -8), S( 9, 28) },
|
{ S( -64, -38), S(-20,-33), S( 4, -5), S( 19, 27) },
|
||||||
{ S( -25, -41), S( 18,-25), S( 43, 7), S( 47, 38) },
|
{ S( -28, -36), S( 5, 0), S( 41, 13), S( 47, 34) },
|
||||||
{ S( -26, -46), S( 16,-25), S( 38, 2), S( 50, 41) },
|
{ S( -29, -41), S( 13,-20), S( 42, 4), S( 52, 35) },
|
||||||
{ S( -11, -55), S( 37,-38), S( 56, -8), S( 71, 27) },
|
{ S( -11, -51), S( 28,-38), S( 63,-17), S( 55, 19) },
|
||||||
{ S( -62, -64), S(-17,-50), S( 5,-24), S( 14, 13) },
|
{ S( -67, -64), S(-21,-45), S( 6,-37), S( 37, 16) },
|
||||||
{ S(-195,-110), S(-66,-90), S(-42,-50), S(-29,-13) }
|
{ S(-200, -98), S(-80,-89), S(-53,-53), S(-32,-16) }
|
||||||
},
|
},
|
||||||
{ // Bishop
|
{ // Bishop
|
||||||
{ S(-54,-68), S(-23,-40), S(-35,-46), S(-44,-28) },
|
{ S(-49,-58), S(- 7,-31), S(-10,-37), S(-34,-19) },
|
||||||
{ S(-30,-43), S( 10,-17), S( 2,-23), S( -9, -5) },
|
{ S(-24,-34), S( 9, -9), S( 15,-14), S( 1, 4) },
|
||||||
{ S(-19,-32), S( 17, -9), S( 11,-13), S( 1, 8) },
|
{ S( -9,-23), S( 22, 0), S( -3, -3), S( 12, 16) },
|
||||||
{ S(-21,-36), S( 18,-13), S( 11,-15), S( 0, 7) },
|
{ S( 4,-26), S( 9, -3), S( 18, -5), S( 40, 16) },
|
||||||
{ S(-21,-36), S( 14,-14), S( 6,-17), S( -1, 3) },
|
{ S( -8,-26), S( 27, -4), S( 13, -7), S( 30, 14) },
|
||||||
{ S(-27,-35), S( 6,-13), S( 2,-10), S( -8, 1) },
|
{ S(-17,-24), S( 14, -2), S( -6, 0), S( 6, 13) },
|
||||||
{ S(-33,-44), S( 7,-21), S( -4,-22), S(-12, -4) },
|
{ S(-19,-34), S(-13,-10), S( 7,-12), S(-11, 6) },
|
||||||
{ S(-45,-65), S(-21,-42), S(-29,-46), S(-39,-27) }
|
{ S(-47,-55), S( -7,-32), S(-17,-36), S(-29,-17) }
|
||||||
},
|
},
|
||||||
{ // Rook
|
{ // Rook
|
||||||
{ S(-25, 0), S(-16, 0), S(-16, 0), S(-9, 0) },
|
{ S(-24, 0), S(-15, 3), S( -8, 0), S( 0, 3) },
|
||||||
{ S(-21, 0), S( -8, 0), S( -3, 0), S( 0, 0) },
|
{ S(-18,-7), S( -5,-5), S( -1,-5), S( 1,-1) },
|
||||||
{ S(-21, 0), S( -9, 0), S( -4, 0), S( 2, 0) },
|
{ S(-19, 6), S(-10,-7), S( 1, 3), S( 0, 3) },
|
||||||
{ S(-22, 0), S( -6, 0), S( -1, 0), S( 2, 0) },
|
{ S(-21, 0), S( -7, 4), S( -4,-2), S(-4, 1) },
|
||||||
{ S(-22, 0), S( -7, 0), S( 0, 0), S( 1, 0) },
|
{ S(-21,-7), S(-12, 5), S( -1,-5), S( 4,-7) },
|
||||||
{ S(-21, 0), S( -7, 0), S( 0, 0), S( 2, 0) },
|
{ S(-23, 3), S(-10, 2), S( 1,-1), S( 6, 3) },
|
||||||
{ S(-12, 0), S( 4, 0), S( 8, 0), S(12, 0) },
|
{ S(-11,-1), S( 8, 7), S( 9,11), S(12,-1) },
|
||||||
{ S(-23, 0), S(-15, 0), S(-11, 0), S(-5, 0) }
|
{ S(-25, 6), S(-18, 4), S(-11, 6), S( 2, 2) }
|
||||||
},
|
},
|
||||||
{ // Queen
|
{ // Queen
|
||||||
{ S( 0,-70), S(-3,-57), S(-4,-41), S(-1,-29) },
|
{ S( 3,-69), S(-5,-57), S(-5,-47), S( 4,-26) },
|
||||||
{ S(-4,-58), S( 6,-30), S( 9,-21), S( 8, -4) },
|
{ S( -3,-55), S( 5,-31), S( 8,-22), S(12, -4) },
|
||||||
{ S(-2,-39), S( 6,-17), S( 9, -7), S( 9, 5) },
|
{ S( -3,-39), S( 6,-18), S(13, -9), S( 7, 3) },
|
||||||
{ S(-1,-29), S( 8, -5), S(10, 9), S( 7, 17) },
|
{ S( 4,-23), S( 5, -3), S( 9, 13), S( 8, 24) },
|
||||||
{ S(-3,-27), S( 9, -5), S( 8, 10), S( 7, 23) },
|
{ S( 0,-29), S(14, -6), S(12, 9), S( 5, 21) },
|
||||||
{ S(-2,-40), S( 6,-16), S( 8,-11), S(10, 3) },
|
{ S( -4,-38), S(10,-18), S( 6,-12), S( 8, 1) },
|
||||||
{ S(-2,-54), S( 7,-30), S( 7,-21), S( 6, -7) },
|
{ S( -5,-50), S( 6,-27), S(10,-24), S( 8, -8) },
|
||||||
{ S(-1,-75), S(-4,-54), S(-1,-44), S( 0,-30) }
|
{ S( -2,-75), S(-2,-52), S( 1,-43), S(-2,-36) }
|
||||||
},
|
},
|
||||||
{ // King
|
{ // King
|
||||||
{ S(291, 28), S(344, 76), S(294,103), S(219,112) },
|
{ S(272, 0), S(325, 41), S(273, 80), S(190, 93) },
|
||||||
{ S(289, 70), S(329,119), S(263,170), S(205,159) },
|
{ S(277, 57), S(305, 98), S(241,138), S(183,131) },
|
||||||
{ S(226,109), S(271,164), S(202,195), S(136,191) },
|
{ S(198, 86), S(253,138), S(168,165), S(120,173) },
|
||||||
{ S(204,131), S(212,194), S(175,194), S(137,204) },
|
{ S(169,103), S(191,152), S(136,168), S(108,169) },
|
||||||
{ S(177,132), S(205,187), S(143,224), S( 94,227) },
|
{ S(145, 98), S(176,166), S(112,197), S(69, 194) },
|
||||||
{ S(147,118), S(188,178), S(113,199), S( 70,197) },
|
{ S(122, 87), S(159,164), S(85, 174), S(36, 189) },
|
||||||
{ S(116, 72), S(158,121), S( 93,142), S( 48,161) },
|
{ S(87, 40), S(120, 99), S(64, 128), S(25, 141) },
|
||||||
{ S( 94, 30), S(120, 76), S( 78,101), S( 31,111) }
|
{ S(64, 5), S(87, 60), S(49, 75), S(0, 75) }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -112,12 +112,12 @@ void init() {
|
|||||||
PieceValue[MG][~pc] = PieceValue[MG][pc];
|
PieceValue[MG][~pc] = PieceValue[MG][pc];
|
||||||
PieceValue[EG][~pc] = PieceValue[EG][pc];
|
PieceValue[EG][~pc] = PieceValue[EG][pc];
|
||||||
|
|
||||||
Score v = make_score(PieceValue[MG][pc], PieceValue[EG][pc]);
|
Score score = make_score(PieceValue[MG][pc], PieceValue[EG][pc]);
|
||||||
|
|
||||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||||
{
|
{
|
||||||
File f = std::min(file_of(s), FILE_H - file_of(s));
|
File f = std::min(file_of(s), ~file_of(s));
|
||||||
psq[ pc][ s] = v + Bonus[pc][rank_of(s)][f];
|
psq[ pc][ s] = score + Bonus[pc][rank_of(s)][f];
|
||||||
psq[~pc][~s] = -psq[pc][s];
|
psq[~pc][~s] = -psq[pc][s];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+695
-628
File diff suppressed because it is too large
Load Diff
+22
-25
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,7 +21,6 @@
|
|||||||
#ifndef SEARCH_H_INCLUDED
|
#ifndef SEARCH_H_INCLUDED
|
||||||
#define SEARCH_H_INCLUDED
|
#define SEARCH_H_INCLUDED
|
||||||
|
|
||||||
#include <atomic>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
@@ -32,21 +31,24 @@ 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
|
||||||
/// its own array of Stack objects, indexed by the current ply.
|
/// its own array of Stack objects, indexed by the current ply.
|
||||||
|
|
||||||
struct Stack {
|
struct Stack {
|
||||||
Move* pv;
|
Move* pv;
|
||||||
|
PieceToHistory* continuationHistory;
|
||||||
int ply;
|
int ply;
|
||||||
Move currentMove;
|
Move currentMove;
|
||||||
Move excludedMove;
|
Move excludedMove;
|
||||||
Move killers[2];
|
Move killers[2];
|
||||||
Value staticEval;
|
Value staticEval;
|
||||||
Value history;
|
int statScore;
|
||||||
bool skipEarlyPruning;
|
|
||||||
int moveCount;
|
int moveCount;
|
||||||
CounterMoveStats* counterMoves;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -57,13 +59,18 @@ struct Stack {
|
|||||||
struct RootMove {
|
struct RootMove {
|
||||||
|
|
||||||
explicit RootMove(Move m) : pv(1, m) {}
|
explicit RootMove(Move m) : pv(1, m) {}
|
||||||
|
|
||||||
bool operator<(const RootMove& m) const { return m.score < score; } // Descending sort
|
|
||||||
bool operator==(const Move& m) const { return pv[0] == m; }
|
|
||||||
bool extract_ponder_from_tt(Position& pos);
|
bool extract_ponder_from_tt(Position& pos);
|
||||||
|
bool operator==(const Move& m) const { return pv[0] == m; }
|
||||||
|
bool operator<(const RootMove& m) const { // Sort in descending order
|
||||||
|
return m.score != score ? m.score < score
|
||||||
|
: m.previousScore < previousScore;
|
||||||
|
}
|
||||||
|
|
||||||
Value score = -VALUE_INFINITE;
|
Value score = -VALUE_INFINITE;
|
||||||
Value previousScore = -VALUE_INFINITE;
|
Value previousScore = -VALUE_INFINITE;
|
||||||
|
int selDepth = 0;
|
||||||
|
int tbRank;
|
||||||
|
Value tbScore;
|
||||||
std::vector<Move> pv;
|
std::vector<Move> pv;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -71,40 +78,30 @@ typedef std::vector<RootMove> RootMoves;
|
|||||||
|
|
||||||
|
|
||||||
/// LimitsType struct stores information sent by GUI about available time to
|
/// LimitsType struct stores information sent by GUI about available time to
|
||||||
/// search the current move, maximum depth/time, if we are in analysis mode or
|
/// search the current move, maximum depth/time, or if we are in analysis mode.
|
||||||
/// if we have to ponder while it's our opponent's turn to move.
|
|
||||||
|
|
||||||
struct LimitsType {
|
struct LimitsType {
|
||||||
|
|
||||||
LimitsType() { // Init explicitly due to broken value-initialization of non POD in MSVC
|
LimitsType() { // Init explicitly due to broken value-initialization of non POD in MSVC
|
||||||
nodes = time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] =
|
time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = npmsec = movetime = TimePoint(0);
|
||||||
npmsec = movestogo = depth = movetime = mate = infinite = ponder = 0;
|
movestogo = depth = mate = perft = infinite = 0;
|
||||||
|
nodes = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool use_time_management() const {
|
bool use_time_management() const {
|
||||||
return !(mate | movetime | depth | nodes | infinite);
|
return !(mate | movetime | depth | nodes | perft | infinite);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Move> searchmoves;
|
std::vector<Move> searchmoves;
|
||||||
int time[COLOR_NB], inc[COLOR_NB], npmsec, movestogo, depth, movetime, mate, infinite, ponder;
|
TimePoint time[COLOR_NB], inc[COLOR_NB], npmsec, movetime, startTime;
|
||||||
|
int movestogo, depth, mate, perft, infinite;
|
||||||
int64_t nodes;
|
int64_t nodes;
|
||||||
TimePoint startTime;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// SignalsType struct stores atomic flags updated during the search, typically
|
|
||||||
/// in an async fashion e.g. to stop the search by the GUI.
|
|
||||||
|
|
||||||
struct SignalsType {
|
|
||||||
std::atomic_bool stop, stopOnPonderhit;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern SignalsType Signals;
|
|
||||||
extern LimitsType Limits;
|
extern LimitsType Limits;
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
void clear();
|
void clear();
|
||||||
template<bool Root = true> uint64_t perft(Position& pos, Depth depth);
|
|
||||||
|
|
||||||
} // namespace Search
|
} // namespace Search
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,169 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright (c) 2011-2013 Ronald de Man
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef TBCORE_H
|
|
||||||
#define TBCORE_H
|
|
||||||
|
|
||||||
#ifndef _WIN32
|
|
||||||
#include <pthread.h>
|
|
||||||
#define SEP_CHAR ':'
|
|
||||||
#define FD int
|
|
||||||
#define FD_ERR -1
|
|
||||||
#else
|
|
||||||
#include <windows.h>
|
|
||||||
#define SEP_CHAR ';'
|
|
||||||
#define FD HANDLE
|
|
||||||
#define FD_ERR INVALID_HANDLE_VALUE
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef _WIN32
|
|
||||||
#define LOCK_T pthread_mutex_t
|
|
||||||
#define LOCK_INIT(x) pthread_mutex_init(&(x), NULL)
|
|
||||||
#define LOCK(x) pthread_mutex_lock(&(x))
|
|
||||||
#define UNLOCK(x) pthread_mutex_unlock(&(x))
|
|
||||||
#else
|
|
||||||
#define LOCK_T HANDLE
|
|
||||||
#define LOCK_INIT(x) do { x = CreateMutex(NULL, FALSE, NULL); } while (0)
|
|
||||||
#define LOCK(x) WaitForSingleObject(x, INFINITE)
|
|
||||||
#define UNLOCK(x) ReleaseMutex(x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef _MSC_VER
|
|
||||||
#define BSWAP32(v) __builtin_bswap32(v)
|
|
||||||
#define BSWAP64(v) __builtin_bswap64(v)
|
|
||||||
#else
|
|
||||||
#define BSWAP32(v) _byteswap_ulong(v)
|
|
||||||
#define BSWAP64(v) _byteswap_uint64(v)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define WDLSUFFIX ".rtbw"
|
|
||||||
#define DTZSUFFIX ".rtbz"
|
|
||||||
#define WDLDIR "RTBWDIR"
|
|
||||||
#define DTZDIR "RTBZDIR"
|
|
||||||
#define TBPIECES 6
|
|
||||||
|
|
||||||
typedef unsigned long long uint64;
|
|
||||||
typedef unsigned int uint32;
|
|
||||||
typedef unsigned char ubyte;
|
|
||||||
typedef unsigned short ushort;
|
|
||||||
|
|
||||||
const ubyte WDL_MAGIC[4] = { 0x71, 0xe8, 0x23, 0x5d };
|
|
||||||
const ubyte DTZ_MAGIC[4] = { 0xd7, 0x66, 0x0c, 0xa5 };
|
|
||||||
|
|
||||||
#define TBHASHBITS 10
|
|
||||||
|
|
||||||
struct TBHashEntry;
|
|
||||||
|
|
||||||
typedef uint64 base_t;
|
|
||||||
|
|
||||||
struct PairsData {
|
|
||||||
char *indextable;
|
|
||||||
ushort *sizetable;
|
|
||||||
ubyte *data;
|
|
||||||
ushort *offset;
|
|
||||||
ubyte *symlen;
|
|
||||||
ubyte *sympat;
|
|
||||||
int blocksize;
|
|
||||||
int idxbits;
|
|
||||||
int min_len;
|
|
||||||
base_t base[1]; // C++ complains about base[]...
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TBEntry {
|
|
||||||
char *data;
|
|
||||||
uint64 key;
|
|
||||||
uint64 mapping;
|
|
||||||
ubyte ready;
|
|
||||||
ubyte num;
|
|
||||||
ubyte symmetric;
|
|
||||||
ubyte has_pawns;
|
|
||||||
}
|
|
||||||
#ifndef _WIN32
|
|
||||||
__attribute__((__may_alias__))
|
|
||||||
#endif
|
|
||||||
;
|
|
||||||
|
|
||||||
struct TBEntry_piece {
|
|
||||||
char *data;
|
|
||||||
uint64 key;
|
|
||||||
uint64 mapping;
|
|
||||||
ubyte ready;
|
|
||||||
ubyte num;
|
|
||||||
ubyte symmetric;
|
|
||||||
ubyte has_pawns;
|
|
||||||
ubyte enc_type;
|
|
||||||
struct PairsData *precomp[2];
|
|
||||||
int factor[2][TBPIECES];
|
|
||||||
ubyte pieces[2][TBPIECES];
|
|
||||||
ubyte norm[2][TBPIECES];
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TBEntry_pawn {
|
|
||||||
char *data;
|
|
||||||
uint64 key;
|
|
||||||
uint64 mapping;
|
|
||||||
ubyte ready;
|
|
||||||
ubyte num;
|
|
||||||
ubyte symmetric;
|
|
||||||
ubyte has_pawns;
|
|
||||||
ubyte pawns[2];
|
|
||||||
struct {
|
|
||||||
struct PairsData *precomp[2];
|
|
||||||
int factor[2][TBPIECES];
|
|
||||||
ubyte pieces[2][TBPIECES];
|
|
||||||
ubyte norm[2][TBPIECES];
|
|
||||||
} file[4];
|
|
||||||
};
|
|
||||||
|
|
||||||
struct DTZEntry_piece {
|
|
||||||
char *data;
|
|
||||||
uint64 key;
|
|
||||||
uint64 mapping;
|
|
||||||
ubyte ready;
|
|
||||||
ubyte num;
|
|
||||||
ubyte symmetric;
|
|
||||||
ubyte has_pawns;
|
|
||||||
ubyte enc_type;
|
|
||||||
struct PairsData *precomp;
|
|
||||||
int factor[TBPIECES];
|
|
||||||
ubyte pieces[TBPIECES];
|
|
||||||
ubyte norm[TBPIECES];
|
|
||||||
ubyte flags; // accurate, mapped, side
|
|
||||||
ushort map_idx[4];
|
|
||||||
ubyte *map;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct DTZEntry_pawn {
|
|
||||||
char *data;
|
|
||||||
uint64 key;
|
|
||||||
uint64 mapping;
|
|
||||||
ubyte ready;
|
|
||||||
ubyte num;
|
|
||||||
ubyte symmetric;
|
|
||||||
ubyte has_pawns;
|
|
||||||
ubyte pawns[2];
|
|
||||||
struct {
|
|
||||||
struct PairsData *precomp;
|
|
||||||
int factor[TBPIECES];
|
|
||||||
ubyte pieces[TBPIECES];
|
|
||||||
ubyte norm[TBPIECES];
|
|
||||||
} file[4];
|
|
||||||
ubyte flags[4];
|
|
||||||
ushort map_idx[4][4];
|
|
||||||
ubyte *map;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TBHashEntry {
|
|
||||||
uint64 key;
|
|
||||||
struct TBEntry *ptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct DTZTableEntry {
|
|
||||||
uint64 key1;
|
|
||||||
uint64 key2;
|
|
||||||
struct TBEntry *entry;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
+1500
-741
File diff suppressed because it is too large
Load Diff
+66
-6
@@ -1,18 +1,78 @@
|
|||||||
|
/*
|
||||||
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
|
Copyright (c) 2013 Ronald de Man
|
||||||
|
Copyright (C) 2016-2018 Marco Costalba, Lucas Braesch
|
||||||
|
|
||||||
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Stockfish is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef TBPROBE_H
|
#ifndef TBPROBE_H
|
||||||
#define TBPROBE_H
|
#define TBPROBE_H
|
||||||
|
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
#include "../search.h"
|
#include "../search.h"
|
||||||
|
|
||||||
namespace Tablebases {
|
namespace Tablebases {
|
||||||
|
|
||||||
|
enum WDLScore {
|
||||||
|
WDLLoss = -2, // Loss
|
||||||
|
WDLBlessedLoss = -1, // Loss, but draw under 50-move rule
|
||||||
|
WDLDraw = 0, // Draw
|
||||||
|
WDLCursedWin = 1, // Win, but draw under 50-move rule
|
||||||
|
WDLWin = 2, // Win
|
||||||
|
|
||||||
|
WDLScoreNone = -1000
|
||||||
|
};
|
||||||
|
|
||||||
|
// Possible states after a probing operation
|
||||||
|
enum ProbeState {
|
||||||
|
FAIL = 0, // Probe failed (missing file table)
|
||||||
|
OK = 1, // Probe succesful
|
||||||
|
CHANGE_STM = -1, // DTZ should check the other side
|
||||||
|
ZEROING_BEST_MOVE = 2 // Best move zeroes DTZ (capture or pawn move)
|
||||||
|
};
|
||||||
|
|
||||||
extern int MaxCardinality;
|
extern int MaxCardinality;
|
||||||
|
|
||||||
void init(const std::string& path);
|
void init(const std::string& paths);
|
||||||
int probe_wdl(Position& pos, int *success);
|
WDLScore probe_wdl(Position& pos, ProbeState* result);
|
||||||
int probe_dtz(Position& pos, int *success);
|
int probe_dtz(Position& pos, ProbeState* result);
|
||||||
bool root_probe(Position& pos, Search::RootMoves& rootMoves, Value& score);
|
bool root_probe(Position& pos, Search::RootMoves& rootMoves);
|
||||||
bool root_probe_wdl(Position& pos, Search::RootMoves& rootMoves, Value& score);
|
bool root_probe_wdl(Position& pos, Search::RootMoves& rootMoves);
|
||||||
void filter_root_moves(Position& pos, Search::RootMoves& rootMoves);
|
void rank_root_moves(Position& pos, Search::RootMoves& rootMoves);
|
||||||
|
|
||||||
|
inline std::ostream& operator<<(std::ostream& os, const WDLScore v) {
|
||||||
|
|
||||||
|
os << (v == WDLLoss ? "Loss" :
|
||||||
|
v == WDLBlessedLoss ? "Blessed loss" :
|
||||||
|
v == WDLDraw ? "Draw" :
|
||||||
|
v == WDLCursedWin ? "Cursed win" :
|
||||||
|
v == WDLWin ? "Win" : "None");
|
||||||
|
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::ostream& operator<<(std::ostream& os, const ProbeState v) {
|
||||||
|
|
||||||
|
os << (v == FAIL ? "Failed" :
|
||||||
|
v == OK ? "Success" :
|
||||||
|
v == CHANGE_STM ? "Probed opponent side" :
|
||||||
|
v == ZEROING_BEST_MOVE ? "Best move zeroes DTZ" : "None");
|
||||||
|
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+99
-119
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,178 +26,154 @@
|
|||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "uci.h"
|
#include "uci.h"
|
||||||
#include "syzygy/tbprobe.h"
|
#include "syzygy/tbprobe.h"
|
||||||
|
#include "tt.h"
|
||||||
|
|
||||||
ThreadPool Threads; // Global object
|
ThreadPool Threads; // Global object
|
||||||
|
|
||||||
/// Thread constructor launches the thread and then waits until it goes to sleep
|
|
||||||
/// in idle_loop().
|
|
||||||
|
|
||||||
Thread::Thread() {
|
/// Thread constructor launches the thread and waits until it goes to sleep
|
||||||
|
/// in idle_loop(). Note that 'searching' and 'exit' should be alredy set.
|
||||||
|
|
||||||
resetCalls = exit = false;
|
Thread::Thread(size_t n) : idx(n), stdThread(&Thread::idle_loop, this) {
|
||||||
maxPly = callsCnt = 0;
|
|
||||||
tbHits = 0;
|
|
||||||
history.clear();
|
|
||||||
counterMoves.clear();
|
|
||||||
idx = Threads.size(); // Start from 0
|
|
||||||
|
|
||||||
std::unique_lock<Mutex> lk(mutex);
|
wait_for_search_finished();
|
||||||
searching = true;
|
|
||||||
nativeThread = std::thread(&Thread::idle_loop, this);
|
|
||||||
sleepCondition.wait(lk, [&]{ return !searching; });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Thread destructor waits for thread termination before returning
|
/// Thread destructor wakes up the thread in idle_loop() and waits
|
||||||
|
/// for its termination. Thread should be already waiting.
|
||||||
|
|
||||||
Thread::~Thread() {
|
Thread::~Thread() {
|
||||||
|
|
||||||
mutex.lock();
|
assert(!searching);
|
||||||
|
|
||||||
exit = true;
|
exit = true;
|
||||||
sleepCondition.notify_one();
|
start_searching();
|
||||||
mutex.unlock();
|
stdThread.join();
|
||||||
nativeThread.join();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Thread::wait_for_search_finished() waits on sleep condition
|
/// Thread::clear() reset histories, usually before a new game
|
||||||
/// until not searching
|
|
||||||
|
void Thread::clear() {
|
||||||
|
|
||||||
|
counterMoves.fill(MOVE_NONE);
|
||||||
|
mainHistory.fill(0);
|
||||||
|
captureHistory.fill(0);
|
||||||
|
|
||||||
|
for (auto& to : continuationHistory)
|
||||||
|
for (auto& h : to)
|
||||||
|
h->fill(0);
|
||||||
|
|
||||||
|
continuationHistory[NO_PIECE][0]->fill(Search::CounterMovePruneThreshold - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Thread::start_searching() wakes up the thread that will start the search
|
||||||
|
|
||||||
|
void Thread::start_searching() {
|
||||||
|
|
||||||
|
std::lock_guard<Mutex> lk(mutex);
|
||||||
|
searching = true;
|
||||||
|
cv.notify_one(); // Wake up the thread in idle_loop()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Thread::wait_for_search_finished() blocks on the condition variable
|
||||||
|
/// until the thread has finished searching.
|
||||||
|
|
||||||
void Thread::wait_for_search_finished() {
|
void Thread::wait_for_search_finished() {
|
||||||
|
|
||||||
std::unique_lock<Mutex> lk(mutex);
|
std::unique_lock<Mutex> lk(mutex);
|
||||||
sleepCondition.wait(lk, [&]{ return !searching; });
|
cv.wait(lk, [&]{ return !searching; });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Thread::wait() waits on sleep condition until condition is true
|
/// Thread::idle_loop() is where the thread is parked, blocked on the
|
||||||
|
/// condition variable, when it has no work to do.
|
||||||
void Thread::wait(std::atomic_bool& condition) {
|
|
||||||
|
|
||||||
std::unique_lock<Mutex> lk(mutex);
|
|
||||||
sleepCondition.wait(lk, [&]{ return bool(condition); });
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Thread::start_searching() wakes up the thread that will start the search
|
|
||||||
|
|
||||||
void Thread::start_searching(bool resume) {
|
|
||||||
|
|
||||||
std::unique_lock<Mutex> lk(mutex);
|
|
||||||
|
|
||||||
if (!resume)
|
|
||||||
searching = true;
|
|
||||||
|
|
||||||
sleepCondition.notify_one();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// Thread::idle_loop() is where the thread is parked when it has no work to do
|
|
||||||
|
|
||||||
void Thread::idle_loop() {
|
void Thread::idle_loop() {
|
||||||
|
|
||||||
while (!exit)
|
// If OS already scheduled us on a different group than 0 then don't overwrite
|
||||||
|
// the choice, eventually we are one of many one-threaded processes running on
|
||||||
|
// some Windows NUMA hardware, for instance in fishtest. To make it simple,
|
||||||
|
// just check if running threads are below a threshold, in this case all this
|
||||||
|
// NUMA machinery is not needed.
|
||||||
|
if (Options["Threads"] > 8)
|
||||||
|
WinProcGroup::bindThisThread(idx);
|
||||||
|
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
std::unique_lock<Mutex> lk(mutex);
|
std::unique_lock<Mutex> lk(mutex);
|
||||||
|
|
||||||
searching = false;
|
searching = false;
|
||||||
|
cv.notify_one(); // Wake up anyone waiting for search finished
|
||||||
|
cv.wait(lk, [&]{ return searching; });
|
||||||
|
|
||||||
while (!searching && !exit)
|
if (exit)
|
||||||
{
|
return;
|
||||||
sleepCondition.notify_one(); // Wake up any waiting thread
|
|
||||||
sleepCondition.wait(lk);
|
|
||||||
}
|
|
||||||
|
|
||||||
lk.unlock();
|
lk.unlock();
|
||||||
|
|
||||||
if (!exit)
|
search();
|
||||||
search();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// ThreadPool::set() creates/destroys threads to match the requested number.
|
||||||
|
/// Created and launched threads will go immediately to sleep in idle_loop.
|
||||||
|
/// Upon resizing, threads are recreated to allow for binding if necessary.
|
||||||
|
|
||||||
/// ThreadPool::init() creates and launches requested threads that will go
|
void ThreadPool::set(size_t requested) {
|
||||||
/// immediately to sleep. We cannot use a constructor because Threads is a
|
|
||||||
/// static object and we need a fully initialized engine at this point due to
|
|
||||||
/// allocation of Endgames in the Thread constructor.
|
|
||||||
|
|
||||||
void ThreadPool::init() {
|
if (size() > 0) { // destroy any existing thread(s)
|
||||||
|
main()->wait_for_search_finished();
|
||||||
|
|
||||||
push_back(new MainThread);
|
while (size() > 0)
|
||||||
read_uci_options();
|
delete back(), pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (requested > 0) { // create new thread(s)
|
||||||
|
push_back(new MainThread(0));
|
||||||
|
|
||||||
|
while (size() < requested)
|
||||||
|
push_back(new Thread(size()));
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reallocate the hash with the new threadpool size
|
||||||
|
TT.resize(Options["Hash"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// ThreadPool::clear() sets threadPool data to initial values.
|
||||||
|
|
||||||
/// ThreadPool::exit() terminates threads before the program exits. Cannot be
|
void ThreadPool::clear() {
|
||||||
/// done in destructor because threads must be terminated before deleting any
|
|
||||||
/// static objects while still in main().
|
|
||||||
|
|
||||||
void ThreadPool::exit() {
|
|
||||||
|
|
||||||
while (size())
|
|
||||||
delete back(), pop_back();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// ThreadPool::read_uci_options() updates internal threads parameters from the
|
|
||||||
/// corresponding UCI options and creates/destroys threads to match requested
|
|
||||||
/// number. Thread objects are dynamically allocated.
|
|
||||||
|
|
||||||
void ThreadPool::read_uci_options() {
|
|
||||||
|
|
||||||
size_t requested = Options["Threads"];
|
|
||||||
|
|
||||||
assert(requested > 0);
|
|
||||||
|
|
||||||
while (size() < requested)
|
|
||||||
push_back(new Thread);
|
|
||||||
|
|
||||||
while (size() > requested)
|
|
||||||
delete back(), pop_back();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// ThreadPool::nodes_searched() returns the number of nodes searched
|
|
||||||
|
|
||||||
uint64_t ThreadPool::nodes_searched() const {
|
|
||||||
|
|
||||||
uint64_t nodes = 0;
|
|
||||||
for (Thread* th : *this)
|
for (Thread* th : *this)
|
||||||
nodes += th->rootPos.nodes_searched();
|
th->clear();
|
||||||
return nodes;
|
|
||||||
|
main()->callsCnt = 0;
|
||||||
|
main()->previousScore = VALUE_INFINITE;
|
||||||
|
main()->previousTimeReduction = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and
|
||||||
/// ThreadPool::tb_hits() returns the number of TB hits
|
/// returns immediately. Main thread will wake up other threads and start the search.
|
||||||
|
|
||||||
uint64_t ThreadPool::tb_hits() const {
|
|
||||||
|
|
||||||
uint64_t hits = 0;
|
|
||||||
for (Thread* th : *this)
|
|
||||||
hits += th->tbHits;
|
|
||||||
return hits;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// ThreadPool::start_thinking() wakes up the main thread sleeping in idle_loop()
|
|
||||||
/// and starts a new search, then returns immediately.
|
|
||||||
|
|
||||||
void ThreadPool::start_thinking(Position& pos, StateListPtr& states,
|
void ThreadPool::start_thinking(Position& pos, StateListPtr& states,
|
||||||
const Search::LimitsType& limits) {
|
const Search::LimitsType& limits, bool ponderMode) {
|
||||||
|
|
||||||
main()->wait_for_search_finished();
|
main()->wait_for_search_finished();
|
||||||
|
|
||||||
Search::Signals.stopOnPonderhit = Search::Signals.stop = false;
|
stopOnPonderhit = stop = false;
|
||||||
|
ponder = ponderMode;
|
||||||
Search::Limits = limits;
|
Search::Limits = limits;
|
||||||
Search::RootMoves rootMoves;
|
Search::RootMoves rootMoves;
|
||||||
|
|
||||||
for (const auto& m : MoveList<LEGAL>(pos))
|
for (const auto& m : MoveList<LEGAL>(pos))
|
||||||
if ( limits.searchmoves.empty()
|
if ( limits.searchmoves.empty()
|
||||||
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
|
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
|
||||||
rootMoves.push_back(Search::RootMove(m));
|
rootMoves.emplace_back(m);
|
||||||
|
|
||||||
if (!rootMoves.empty())
|
if (!rootMoves.empty())
|
||||||
Tablebases::filter_root_moves(pos, rootMoves);
|
Tablebases::rank_root_moves(pos, rootMoves);
|
||||||
|
|
||||||
// After ownership transfer 'states' becomes empty, so if we stop the search
|
// After ownership transfer 'states' becomes empty, so if we stop the search
|
||||||
// and call 'go' again without setting a new position states.get() == NULL.
|
// and call 'go' again without setting a new position states.get() == NULL.
|
||||||
@@ -206,18 +182,22 @@ void ThreadPool::start_thinking(Position& pos, StateListPtr& states,
|
|||||||
if (states.get())
|
if (states.get())
|
||||||
setupStates = std::move(states); // Ownership transfer, states is now empty
|
setupStates = std::move(states); // Ownership transfer, states is now empty
|
||||||
|
|
||||||
|
// We use Position::set() to set root position across threads. But there are
|
||||||
|
// some StateInfo fields (previous, pliesFromNull, capturedPiece) that cannot
|
||||||
|
// be deduced from a fen string, so set() clears them and to not lose the info
|
||||||
|
// we need to backup and later restore setupStates->back(). Note that setupStates
|
||||||
|
// is shared by threads but is accessed in read-only mode.
|
||||||
StateInfo tmp = setupStates->back();
|
StateInfo tmp = setupStates->back();
|
||||||
|
|
||||||
for (Thread* th : Threads)
|
for (Thread* th : *this)
|
||||||
{
|
{
|
||||||
th->maxPly = 0;
|
th->nodes = th->tbHits = th->nmpMinPly = 0;
|
||||||
th->tbHits = 0;
|
th->rootDepth = th->completedDepth = DEPTH_ZERO;
|
||||||
th->rootDepth = DEPTH_ZERO;
|
|
||||||
th->rootMoves = rootMoves;
|
th->rootMoves = rootMoves;
|
||||||
th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th);
|
th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th);
|
||||||
}
|
}
|
||||||
|
|
||||||
setupStates->back() = tmp; // Restore st->previous, cleared by Position::set()
|
setupStates->back() = tmp;
|
||||||
|
|
||||||
main()->start_searching();
|
main()->start_searching();
|
||||||
}
|
}
|
||||||
|
|||||||
+47
-34
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,7 +22,6 @@
|
|||||||
#define THREAD_H_INCLUDED
|
#define THREAD_H_INCLUDED
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <bitset>
|
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@@ -36,74 +35,88 @@
|
|||||||
#include "thread_win32.h"
|
#include "thread_win32.h"
|
||||||
|
|
||||||
|
|
||||||
/// Thread struct keeps together all the thread-related stuff. We also 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 pointer to an
|
/// per-thread pawn and material hash tables so that once we get a
|
||||||
/// entry its life time is unlimited and we don't have to care about someone
|
/// pointer to an entry its life time is unlimited and we don't have
|
||||||
/// changing the entry under our feet.
|
/// to care about someone changing the entry under our feet.
|
||||||
|
|
||||||
class Thread {
|
class Thread {
|
||||||
|
|
||||||
std::thread nativeThread;
|
|
||||||
Mutex mutex;
|
Mutex mutex;
|
||||||
ConditionVariable sleepCondition;
|
ConditionVariable cv;
|
||||||
bool exit, searching;
|
size_t idx;
|
||||||
|
bool exit = false, searching = true; // Set before starting std::thread
|
||||||
|
std::thread stdThread;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Thread();
|
explicit Thread(size_t);
|
||||||
virtual ~Thread();
|
virtual ~Thread();
|
||||||
virtual void search();
|
virtual void search();
|
||||||
|
void clear();
|
||||||
void idle_loop();
|
void idle_loop();
|
||||||
void start_searching(bool resume = false);
|
void start_searching();
|
||||||
void wait_for_search_finished();
|
void wait_for_search_finished();
|
||||||
void wait(std::atomic_bool& b);
|
|
||||||
|
|
||||||
Pawns::Table pawnsTable;
|
Pawns::Table pawnsTable;
|
||||||
Material::Table materialTable;
|
Material::Table materialTable;
|
||||||
Endgames endgames;
|
Endgames endgames;
|
||||||
size_t idx, PVIdx;
|
size_t pvIdx, pvLast;
|
||||||
int maxPly, callsCnt;
|
int selDepth, nmpMinPly;
|
||||||
uint64_t tbHits;
|
Color nmpColor;
|
||||||
|
std::atomic<uint64_t> nodes, tbHits;
|
||||||
|
|
||||||
Position rootPos;
|
Position rootPos;
|
||||||
Search::RootMoves rootMoves;
|
Search::RootMoves rootMoves;
|
||||||
Depth rootDepth;
|
Depth rootDepth, completedDepth;
|
||||||
Depth completedDepth;
|
CounterMoveHistory counterMoves;
|
||||||
std::atomic_bool resetCalls;
|
ButterflyHistory mainHistory;
|
||||||
HistoryStats history;
|
CapturePieceToHistory captureHistory;
|
||||||
MoveStats counterMoves;
|
ContinuationHistory continuationHistory;
|
||||||
FromToStats fromTo;
|
Score contempt;
|
||||||
CounterMoveHistoryStats counterMoveHistory;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// MainThread is a derived class with a specific overload for the main thread
|
/// MainThread is a derived class specific for main thread
|
||||||
|
|
||||||
struct MainThread : public Thread {
|
struct MainThread : public Thread {
|
||||||
virtual void search();
|
|
||||||
|
|
||||||
bool easyMovePlayed, failedLow;
|
using Thread::Thread;
|
||||||
double bestMoveChanges;
|
|
||||||
|
void search() override;
|
||||||
|
void check_time();
|
||||||
|
|
||||||
|
double bestMoveChanges, previousTimeReduction;
|
||||||
Value previousScore;
|
Value previousScore;
|
||||||
|
int callsCnt;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// ThreadPool struct handles all the threads-related stuff like init, starting,
|
/// ThreadPool struct handles all the threads-related stuff like init, starting,
|
||||||
/// parking and, most importantly, launching a thread. All the access to threads
|
/// parking and, most importantly, launching a thread. All the access to threads
|
||||||
/// data is done through this class.
|
/// is done through this class.
|
||||||
|
|
||||||
struct ThreadPool : public std::vector<Thread*> {
|
struct ThreadPool : public std::vector<Thread*> {
|
||||||
|
|
||||||
void init(); // No constructor and destructor, threads rely on globals that should
|
void start_thinking(Position&, StateListPtr&, const Search::LimitsType&, bool = false);
|
||||||
void exit(); // be initialized and valid during the whole thread lifetime.
|
void clear();
|
||||||
|
void set(size_t);
|
||||||
|
|
||||||
MainThread* main() { return static_cast<MainThread*>(at(0)); }
|
MainThread* main() const { return static_cast<MainThread*>(front()); }
|
||||||
void start_thinking(Position&, StateListPtr&, const Search::LimitsType&);
|
uint64_t nodes_searched() const { return accumulate(&Thread::nodes); }
|
||||||
void read_uci_options();
|
uint64_t tb_hits() const { return accumulate(&Thread::tbHits); }
|
||||||
uint64_t nodes_searched() const;
|
|
||||||
uint64_t tb_hits() const;
|
std::atomic_bool stop, ponder, stopOnPonderhit;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
StateListPtr setupStates;
|
StateListPtr setupStates;
|
||||||
|
|
||||||
|
uint64_t accumulate(std::atomic<uint64_t> Thread::* member) const {
|
||||||
|
|
||||||
|
uint64_t sum = 0;
|
||||||
|
for (Thread* th : *this)
|
||||||
|
sum += (th->*member).load(std::memory_order_relaxed);
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
extern ThreadPool Threads;
|
extern ThreadPool Threads;
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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
-30
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,9 +32,9 @@ namespace {
|
|||||||
|
|
||||||
enum TimeType { OptimumTime, MaxTime };
|
enum TimeType { OptimumTime, MaxTime };
|
||||||
|
|
||||||
const int MoveHorizon = 50; // Plan time management at most this many moves ahead
|
constexpr int MoveHorizon = 50; // Plan time management at most this many moves ahead
|
||||||
const double MaxRatio = 7.09; // When in trouble, we can step over reserved time with this ratio
|
constexpr double MaxRatio = 7.3; // When in trouble, we can step over reserved time with this ratio
|
||||||
const double StealRatio = 0.35; // However we must not steal time from remaining moves over this ratio
|
constexpr double StealRatio = 0.34; // However we must not steal time from remaining moves over this ratio
|
||||||
|
|
||||||
|
|
||||||
// move_importance() is a skew-logistic function based on naive statistical
|
// move_importance() is a skew-logistic function based on naive statistical
|
||||||
@@ -44,21 +44,21 @@ namespace {
|
|||||||
|
|
||||||
double move_importance(int ply) {
|
double move_importance(int ply) {
|
||||||
|
|
||||||
const double XScale = 7.64;
|
constexpr double XScale = 6.85;
|
||||||
const double XShift = 58.4;
|
constexpr double XShift = 64.5;
|
||||||
const double Skew = 0.183;
|
constexpr double Skew = 0.171;
|
||||||
|
|
||||||
return pow((1 + exp((ply - XShift) / XScale)), -Skew) + DBL_MIN; // Ensure non-zero
|
return pow((1 + exp((ply - XShift) / XScale)), -Skew) + DBL_MIN; // Ensure non-zero
|
||||||
}
|
}
|
||||||
|
|
||||||
template<TimeType T>
|
template<TimeType T>
|
||||||
int remaining(int myTime, int movesToGo, int ply, int slowMover) {
|
TimePoint remaining(TimePoint myTime, int movesToGo, int ply, TimePoint slowMover) {
|
||||||
|
|
||||||
const double TMaxRatio = (T == OptimumTime ? 1 : MaxRatio);
|
constexpr double TMaxRatio = (T == OptimumTime ? 1.0 : MaxRatio);
|
||||||
const double TStealRatio = (T == OptimumTime ? 0 : StealRatio);
|
constexpr double TStealRatio = (T == OptimumTime ? 0.0 : StealRatio);
|
||||||
|
|
||||||
double moveImportance = (move_importance(ply) * slowMover) / 100;
|
double moveImportance = (move_importance(ply) * slowMover) / 100.0;
|
||||||
double otherMovesImportance = 0;
|
double otherMovesImportance = 0.0;
|
||||||
|
|
||||||
for (int i = 1; i < movesToGo; ++i)
|
for (int i = 1; i < movesToGo; ++i)
|
||||||
otherMovesImportance += move_importance(ply + 2 * i);
|
otherMovesImportance += move_importance(ply + 2 * i);
|
||||||
@@ -66,7 +66,7 @@ namespace {
|
|||||||
double ratio1 = (TMaxRatio * moveImportance) / (TMaxRatio * moveImportance + otherMovesImportance);
|
double ratio1 = (TMaxRatio * moveImportance) / (TMaxRatio * moveImportance + otherMovesImportance);
|
||||||
double ratio2 = (moveImportance + TStealRatio * otherMovesImportance) / (moveImportance + otherMovesImportance);
|
double ratio2 = (moveImportance + TStealRatio * otherMovesImportance) / (moveImportance + otherMovesImportance);
|
||||||
|
|
||||||
return int(myTime * std::min(ratio1, ratio2)); // Intel C++ asks for an explicit cast
|
return TimePoint(myTime * std::min(ratio1, ratio2)); // Intel C++ asks for an explicit cast
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -83,22 +83,23 @@ namespace {
|
|||||||
|
|
||||||
void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
|
void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
|
||||||
|
|
||||||
int minThinkingTime = Options["Minimum Thinking Time"];
|
TimePoint minThinkingTime = Options["Minimum Thinking Time"];
|
||||||
int moveOverhead = Options["Move Overhead"];
|
TimePoint moveOverhead = Options["Move Overhead"];
|
||||||
int slowMover = Options["Slow Mover"];
|
TimePoint slowMover = Options["Slow Mover"];
|
||||||
int npmsec = Options["nodestime"];
|
TimePoint npmsec = Options["nodestime"];
|
||||||
|
TimePoint hypMyTime;
|
||||||
|
|
||||||
// If we have to play in 'nodes as time' mode, then convert from time
|
// If we have to play in 'nodes as time' mode, then convert from time
|
||||||
// to nodes, and use resulting values in time management formulas.
|
// to nodes, and use resulting values in time management formulas.
|
||||||
// WARNING: Given npms (nodes per millisecond) must be much lower then
|
// WARNING: to avoid time losses, the given npmsec (nodes per millisecond)
|
||||||
// the real engine speed to avoid time losses.
|
// must be much lower than the real engine speed.
|
||||||
if (npmsec)
|
if (npmsec)
|
||||||
{
|
{
|
||||||
if (!availableNodes) // Only once at game start
|
if (!availableNodes) // Only once at game start
|
||||||
availableNodes = npmsec * limits.time[us]; // Time is in msec
|
availableNodes = npmsec * limits.time[us]; // Time is in msec
|
||||||
|
|
||||||
// Convert from millisecs to nodes
|
// Convert from milliseconds to nodes
|
||||||
limits.time[us] = (int)availableNodes;
|
limits.time[us] = TimePoint(availableNodes);
|
||||||
limits.inc[us] *= npmsec;
|
limits.inc[us] *= npmsec;
|
||||||
limits.npmsec = npmsec;
|
limits.npmsec = npmsec;
|
||||||
}
|
}
|
||||||
@@ -106,22 +107,22 @@ void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
|
|||||||
startTime = limits.startTime;
|
startTime = limits.startTime;
|
||||||
optimumTime = maximumTime = std::max(limits.time[us], minThinkingTime);
|
optimumTime = maximumTime = std::max(limits.time[us], minThinkingTime);
|
||||||
|
|
||||||
const int MaxMTG = limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon;
|
const int maxMTG = limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon;
|
||||||
|
|
||||||
// We calculate optimum time usage for different hypothetical "moves to go"-values
|
// We calculate optimum time usage for different hypothetical "moves to go" values
|
||||||
// and choose the minimum of calculated search time values. Usually the greatest
|
// and choose the minimum of calculated search time values. Usually the greatest
|
||||||
// hypMTG gives the minimum values.
|
// hypMTG gives the minimum values.
|
||||||
for (int hypMTG = 1; hypMTG <= MaxMTG; ++hypMTG)
|
for (int hypMTG = 1; hypMTG <= maxMTG; ++hypMTG)
|
||||||
{
|
{
|
||||||
// Calculate thinking time for hypothetical "moves to go"-value
|
// Calculate thinking time for hypothetical "moves to go"-value
|
||||||
int hypMyTime = limits.time[us]
|
hypMyTime = limits.time[us]
|
||||||
+ limits.inc[us] * (hypMTG - 1)
|
+ limits.inc[us] * (hypMTG - 1)
|
||||||
- moveOverhead * (2 + std::min(hypMTG, 40));
|
- moveOverhead * (2 + std::min(hypMTG, 40));
|
||||||
|
|
||||||
hypMyTime = std::max(hypMyTime, 0);
|
hypMyTime = std::max(hypMyTime, TimePoint(0));
|
||||||
|
|
||||||
int t1 = minThinkingTime + remaining<OptimumTime>(hypMyTime, hypMTG, ply, slowMover);
|
TimePoint t1 = minThinkingTime + remaining<OptimumTime>(hypMyTime, hypMTG, ply, slowMover);
|
||||||
int t2 = minThinkingTime + remaining<MaxTime >(hypMyTime, hypMTG, ply, slowMover);
|
TimePoint t2 = minThinkingTime + remaining<MaxTime >(hypMyTime, hypMTG, ply, slowMover);
|
||||||
|
|
||||||
optimumTime = std::min(t1, optimumTime);
|
optimumTime = std::min(t1, optimumTime);
|
||||||
maximumTime = std::min(t2, maximumTime);
|
maximumTime = std::min(t2, maximumTime);
|
||||||
|
|||||||
+7
-6
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,16 +31,17 @@
|
|||||||
class TimeManagement {
|
class TimeManagement {
|
||||||
public:
|
public:
|
||||||
void init(Search::LimitsType& limits, Color us, int ply);
|
void init(Search::LimitsType& limits, Color us, int ply);
|
||||||
int optimum() const { return optimumTime; }
|
TimePoint optimum() const { return optimumTime; }
|
||||||
int maximum() const { return maximumTime; }
|
TimePoint maximum() const { return maximumTime; }
|
||||||
int elapsed() const { return int(Search::Limits.npmsec ? Threads.nodes_searched() : now() - startTime); }
|
TimePoint elapsed() const { return Search::Limits.npmsec ?
|
||||||
|
TimePoint(Threads.nodes_searched()) : now() - startTime; }
|
||||||
|
|
||||||
int64_t availableNodes; // When in 'nodes as time' mode
|
int64_t availableNodes; // When in 'nodes as time' mode
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TimePoint startTime;
|
TimePoint startTime;
|
||||||
int optimumTime;
|
TimePoint optimumTime;
|
||||||
int maximumTime;
|
TimePoint maximumTime;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern TimeManagement Time;
|
extern TimeManagement Time;
|
||||||
|
|||||||
+54
-15
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,12 +20,37 @@
|
|||||||
|
|
||||||
#include <cstring> // For std::memset
|
#include <cstring> // For std::memset
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
|
#include "misc.h"
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
|
#include "uci.h"
|
||||||
|
|
||||||
TranspositionTable TT; // Our global transposition table
|
TranspositionTable TT; // Our global transposition table
|
||||||
|
|
||||||
|
/// TTEntry::save saves a TTEntry
|
||||||
|
void TTEntry::save(Key k, Value v, Bound b, Depth d, Move m, Value ev) {
|
||||||
|
|
||||||
|
assert(d / ONE_PLY * ONE_PLY == d);
|
||||||
|
|
||||||
|
// Preserve any existing move for the same position
|
||||||
|
if (m || (k >> 48) != key16)
|
||||||
|
move16 = (uint16_t)m;
|
||||||
|
|
||||||
|
// Overwrite less valuable entries
|
||||||
|
if ( (k >> 48) != key16
|
||||||
|
|| d / ONE_PLY > depth8 - 4
|
||||||
|
|| b == BOUND_EXACT)
|
||||||
|
{
|
||||||
|
key16 = (uint16_t)(k >> 48);
|
||||||
|
value16 = (int16_t)v;
|
||||||
|
eval16 = (int16_t)ev;
|
||||||
|
genBound8 = (uint8_t)(TT.generation8 | b);
|
||||||
|
depth8 = (int8_t)(d / ONE_PLY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// TranspositionTable::resize() sets the size of the transposition table,
|
/// TranspositionTable::resize() sets the size of the transposition table,
|
||||||
/// measured in megabytes. Transposition table consists of a power of 2 number
|
/// measured in megabytes. Transposition table consists of a power of 2 number
|
||||||
@@ -33,15 +58,10 @@ TranspositionTable TT; // Our global transposition table
|
|||||||
|
|
||||||
void TranspositionTable::resize(size_t mbSize) {
|
void TranspositionTable::resize(size_t mbSize) {
|
||||||
|
|
||||||
size_t newClusterCount = size_t(1) << msb((mbSize * 1024 * 1024) / sizeof(Cluster));
|
clusterCount = mbSize * 1024 * 1024 / sizeof(Cluster);
|
||||||
|
|
||||||
if (newClusterCount == clusterCount)
|
|
||||||
return;
|
|
||||||
|
|
||||||
clusterCount = newClusterCount;
|
|
||||||
|
|
||||||
free(mem);
|
free(mem);
|
||||||
mem = calloc(clusterCount * sizeof(Cluster) + CacheLineSize - 1, 1);
|
mem = malloc(clusterCount * sizeof(Cluster) + CacheLineSize - 1);
|
||||||
|
|
||||||
if (!mem)
|
if (!mem)
|
||||||
{
|
{
|
||||||
@@ -51,18 +71,38 @@ void TranspositionTable::resize(size_t mbSize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
table = (Cluster*)((uintptr_t(mem) + CacheLineSize - 1) & ~(CacheLineSize - 1));
|
table = (Cluster*)((uintptr_t(mem) + CacheLineSize - 1) & ~(CacheLineSize - 1));
|
||||||
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// TranspositionTable::clear() overwrites the entire transposition table
|
/// TranspositionTable::clear() initializes the entire transposition table to zero,
|
||||||
/// with zeros. It is called whenever the table is resized, or when the
|
// in a multi-threaded way.
|
||||||
/// user asks the program to clear the table (from the UCI interface).
|
|
||||||
|
|
||||||
void TranspositionTable::clear() {
|
void TranspositionTable::clear() {
|
||||||
|
|
||||||
std::memset(table, 0, clusterCount * sizeof(Cluster));
|
std::vector<std::thread> threads;
|
||||||
}
|
|
||||||
|
|
||||||
|
for (size_t idx = 0; idx < Options["Threads"]; idx++)
|
||||||
|
{
|
||||||
|
threads.emplace_back([this, idx]() {
|
||||||
|
|
||||||
|
// Thread binding gives faster search on systems with a first-touch policy
|
||||||
|
if (Options["Threads"] > 8)
|
||||||
|
WinProcGroup::bindThisThread(idx);
|
||||||
|
|
||||||
|
// Each thread will zero its part of the hash table
|
||||||
|
const size_t stride = clusterCount / Options["Threads"],
|
||||||
|
start = stride * idx,
|
||||||
|
len = idx != Options["Threads"] - 1 ?
|
||||||
|
stride : clusterCount - start;
|
||||||
|
|
||||||
|
std::memset(&table[start], 0, len * sizeof(Cluster));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (std::thread& th: threads)
|
||||||
|
th.join();
|
||||||
|
}
|
||||||
|
|
||||||
/// TranspositionTable::probe() looks up the current position in the transposition
|
/// TranspositionTable::probe() looks up the current position in the transposition
|
||||||
/// table. It returns true and a pointer to the TTEntry if the position is found.
|
/// table. It returns true and a pointer to the TTEntry if the position is found.
|
||||||
@@ -79,8 +119,7 @@ TTEntry* TranspositionTable::probe(const Key key, bool& found) const {
|
|||||||
for (int i = 0; i < ClusterSize; ++i)
|
for (int i = 0; i < ClusterSize; ++i)
|
||||||
if (!tte[i].key16 || tte[i].key16 == key16)
|
if (!tte[i].key16 || tte[i].key16 == key16)
|
||||||
{
|
{
|
||||||
if ((tte[i].genBound8 & 0xFC) != generation8 && tte[i].key16)
|
tte[i].genBound8 = uint8_t(generation8 | tte[i].bound()); // Refresh
|
||||||
tte[i].genBound8 = uint8_t(generation8 | tte[i].bound()); // Refresh
|
|
||||||
|
|
||||||
return found = (bool)tte[i].key16, &tte[i];
|
return found = (bool)tte[i].key16, &tte[i];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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
|
||||||
@@ -41,28 +41,7 @@ struct TTEntry {
|
|||||||
Value eval() const { return (Value)eval16; }
|
Value eval() const { return (Value)eval16; }
|
||||||
Depth depth() const { return (Depth)(depth8 * int(ONE_PLY)); }
|
Depth depth() const { return (Depth)(depth8 * int(ONE_PLY)); }
|
||||||
Bound bound() const { return (Bound)(genBound8 & 0x3); }
|
Bound bound() const { return (Bound)(genBound8 & 0x3); }
|
||||||
|
void save(Key k, Value v, Bound b, Depth d, Move m, Value ev);
|
||||||
void save(Key k, Value v, Bound b, Depth d, Move m, Value ev, uint8_t g) {
|
|
||||||
|
|
||||||
assert(d / ONE_PLY * ONE_PLY == d);
|
|
||||||
|
|
||||||
// Preserve any existing move for the same position
|
|
||||||
if (m || (k >> 48) != key16)
|
|
||||||
move16 = (uint16_t)m;
|
|
||||||
|
|
||||||
// Don't overwrite more valuable entries
|
|
||||||
if ( (k >> 48) != key16
|
|
||||||
|| d / ONE_PLY > depth8 - 4
|
|
||||||
/* || g != (genBound8 & 0xFC) // Matching non-zero keys are already refreshed by probe() */
|
|
||||||
|| b == BOUND_EXACT)
|
|
||||||
{
|
|
||||||
key16 = (uint16_t)(k >> 48);
|
|
||||||
value16 = (int16_t)v;
|
|
||||||
eval16 = (int16_t)ev;
|
|
||||||
genBound8 = (uint8_t)(g | b);
|
|
||||||
depth8 = (int8_t)(d / ONE_PLY);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class TranspositionTable;
|
friend class TranspositionTable;
|
||||||
@@ -85,8 +64,8 @@ private:
|
|||||||
|
|
||||||
class TranspositionTable {
|
class TranspositionTable {
|
||||||
|
|
||||||
static const int CacheLineSize = 64;
|
static constexpr int CacheLineSize = 64;
|
||||||
static const int ClusterSize = 3;
|
static constexpr int ClusterSize = 3;
|
||||||
|
|
||||||
struct Cluster {
|
struct Cluster {
|
||||||
TTEntry entry[ClusterSize];
|
TTEntry entry[ClusterSize];
|
||||||
@@ -98,18 +77,19 @@ class TranspositionTable {
|
|||||||
public:
|
public:
|
||||||
~TranspositionTable() { free(mem); }
|
~TranspositionTable() { free(mem); }
|
||||||
void new_search() { generation8 += 4; } // Lower 2 bits are used by Bound
|
void new_search() { generation8 += 4; } // Lower 2 bits are used by Bound
|
||||||
uint8_t generation() const { return generation8; }
|
|
||||||
TTEntry* probe(const Key key, bool& found) const;
|
TTEntry* probe(const Key key, bool& found) const;
|
||||||
int hashfull() const;
|
int hashfull() const;
|
||||||
void resize(size_t mbSize);
|
void resize(size_t mbSize);
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
// The lowest order bits of the key are used to get the index of the cluster
|
// The 32 lowest order bits of the key are used to get the index of the cluster
|
||||||
TTEntry* first_entry(const Key key) const {
|
TTEntry* first_entry(const Key key) const {
|
||||||
return &table[(size_t)key & (clusterCount - 1)].entry[0];
|
return &table[(uint32_t(key) * uint64_t(clusterCount)) >> 32].entry[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend struct TTEntry;
|
||||||
|
|
||||||
size_t clusterCount;
|
size_t clusterCount;
|
||||||
Cluster* table;
|
Cluster* table;
|
||||||
void* mem;
|
void* mem;
|
||||||
|
|||||||
+107
-84
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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
|
||||||
@@ -76,32 +76,32 @@
|
|||||||
# include <immintrin.h> // Header for _pext_u64() intrinsic
|
# include <immintrin.h> // Header for _pext_u64() intrinsic
|
||||||
# define pext(b, m) _pext_u64(b, m)
|
# define pext(b, m) _pext_u64(b, m)
|
||||||
#else
|
#else
|
||||||
# define pext(b, m) (0)
|
# define pext(b, m) 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_POPCNT
|
#ifdef USE_POPCNT
|
||||||
const bool HasPopCnt = true;
|
constexpr bool HasPopCnt = true;
|
||||||
#else
|
#else
|
||||||
const bool HasPopCnt = false;
|
constexpr bool HasPopCnt = false;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_PEXT
|
#ifdef USE_PEXT
|
||||||
const bool HasPext = true;
|
constexpr bool HasPext = true;
|
||||||
#else
|
#else
|
||||||
const bool HasPext = false;
|
constexpr bool HasPext = false;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef IS_64BIT
|
#ifdef IS_64BIT
|
||||||
const bool Is64Bit = true;
|
constexpr bool Is64Bit = true;
|
||||||
#else
|
#else
|
||||||
const bool Is64Bit = false;
|
constexpr bool Is64Bit = false;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef uint64_t Key;
|
typedef uint64_t Key;
|
||||||
typedef uint64_t Bitboard;
|
typedef uint64_t Bitboard;
|
||||||
|
|
||||||
const int MAX_MOVES = 256;
|
constexpr int MAX_MOVES = 256;
|
||||||
const int MAX_PLY = 128;
|
constexpr int MAX_PLY = 128;
|
||||||
|
|
||||||
/// A move needs 16 bits to be stored
|
/// A move needs 16 bits to be stored
|
||||||
///
|
///
|
||||||
@@ -128,7 +128,7 @@ enum MoveType {
|
|||||||
};
|
};
|
||||||
|
|
||||||
enum Color {
|
enum Color {
|
||||||
WHITE, BLACK, NO_COLOR, COLOR_NB = 2
|
WHITE, BLACK, COLOR_NB = 2
|
||||||
};
|
};
|
||||||
|
|
||||||
enum CastlingSide {
|
enum CastlingSide {
|
||||||
@@ -145,12 +145,6 @@ enum CastlingRight {
|
|||||||
CASTLING_RIGHT_NB = 16
|
CASTLING_RIGHT_NB = 16
|
||||||
};
|
};
|
||||||
|
|
||||||
template<Color C, CastlingSide S> struct MakeCastling {
|
|
||||||
static const CastlingRight
|
|
||||||
right = C == WHITE ? S == QUEEN_SIDE ? WHITE_OOO : WHITE_OO
|
|
||||||
: S == QUEEN_SIDE ? BLACK_OOO : BLACK_OO;
|
|
||||||
};
|
|
||||||
|
|
||||||
enum Phase {
|
enum Phase {
|
||||||
PHASE_ENDGAME,
|
PHASE_ENDGAME,
|
||||||
PHASE_MIDGAME = 128,
|
PHASE_MIDGAME = 128,
|
||||||
@@ -159,7 +153,6 @@ enum Phase {
|
|||||||
|
|
||||||
enum ScaleFactor {
|
enum ScaleFactor {
|
||||||
SCALE_FACTOR_DRAW = 0,
|
SCALE_FACTOR_DRAW = 0,
|
||||||
SCALE_FACTOR_ONEPAWN = 48,
|
|
||||||
SCALE_FACTOR_NORMAL = 64,
|
SCALE_FACTOR_NORMAL = 64,
|
||||||
SCALE_FACTOR_MAX = 128,
|
SCALE_FACTOR_MAX = 128,
|
||||||
SCALE_FACTOR_NONE = 255
|
SCALE_FACTOR_NONE = 255
|
||||||
@@ -183,11 +176,11 @@ enum Value : int {
|
|||||||
VALUE_MATE_IN_MAX_PLY = VALUE_MATE - 2 * MAX_PLY,
|
VALUE_MATE_IN_MAX_PLY = VALUE_MATE - 2 * MAX_PLY,
|
||||||
VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + 2 * MAX_PLY,
|
VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + 2 * MAX_PLY,
|
||||||
|
|
||||||
PawnValueMg = 188, PawnValueEg = 248,
|
PawnValueMg = 136, PawnValueEg = 208,
|
||||||
KnightValueMg = 753, KnightValueEg = 832,
|
KnightValueMg = 782, KnightValueEg = 865,
|
||||||
BishopValueMg = 826, BishopValueEg = 897,
|
BishopValueMg = 830, BishopValueEg = 918,
|
||||||
RookValueMg = 1285, RookValueEg = 1371,
|
RookValueMg = 1289, RookValueEg = 1378,
|
||||||
QueenValueMg = 2513, QueenValueEg = 2650,
|
QueenValueMg = 2529, QueenValueEg = 2687,
|
||||||
|
|
||||||
MidgameLimit = 15258, EndgameLimit = 3915
|
MidgameLimit = 15258, EndgameLimit = 3915
|
||||||
};
|
};
|
||||||
@@ -205,11 +198,9 @@ enum Piece {
|
|||||||
PIECE_NB = 16
|
PIECE_NB = 16
|
||||||
};
|
};
|
||||||
|
|
||||||
const Piece Pieces[] = { W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
|
|
||||||
B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING };
|
|
||||||
extern Value PieceValue[PHASE_NB][PIECE_NB];
|
extern Value PieceValue[PHASE_NB][PIECE_NB];
|
||||||
|
|
||||||
enum Depth {
|
enum Depth : int {
|
||||||
|
|
||||||
ONE_PLY = 1,
|
ONE_PLY = 1,
|
||||||
|
|
||||||
@@ -224,7 +215,7 @@ enum Depth {
|
|||||||
|
|
||||||
static_assert(!(ONE_PLY & (ONE_PLY - 1)), "ONE_PLY is not a power of 2");
|
static_assert(!(ONE_PLY & (ONE_PLY - 1)), "ONE_PLY is not a power of 2");
|
||||||
|
|
||||||
enum Square {
|
enum Square : int {
|
||||||
SQ_A1, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1,
|
SQ_A1, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1,
|
||||||
SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2,
|
SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2,
|
||||||
SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3,
|
SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3,
|
||||||
@@ -235,12 +226,14 @@ enum Square {
|
|||||||
SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
|
SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
|
||||||
SQ_NONE,
|
SQ_NONE,
|
||||||
|
|
||||||
SQUARE_NB = 64,
|
SQUARE_NB = 64
|
||||||
|
};
|
||||||
|
|
||||||
|
enum Direction : int {
|
||||||
NORTH = 8,
|
NORTH = 8,
|
||||||
EAST = 1,
|
EAST = 1,
|
||||||
SOUTH = -8,
|
SOUTH = -NORTH,
|
||||||
WEST = -1,
|
WEST = -EAST,
|
||||||
|
|
||||||
NORTH_EAST = NORTH + EAST,
|
NORTH_EAST = NORTH + EAST,
|
||||||
SOUTH_EAST = SOUTH + EAST,
|
SOUTH_EAST = SOUTH + EAST,
|
||||||
@@ -257,13 +250,13 @@ enum Rank : int {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// Score enum stores a middlegame and an endgame value in a single integer
|
/// Score enum stores a middlegame and an endgame value in a single integer (enum).
|
||||||
/// (enum). The least significant 16 bits are used to store the endgame value
|
/// The least significant 16 bits are used to store the middlegame value and the
|
||||||
/// and the upper 16 bits are used to store the middlegame value. Take some
|
/// upper 16 bits are used to store the endgame value. We have to take care to
|
||||||
/// care to avoid left-shifting a signed int to avoid undefined behavior.
|
/// avoid left-shifting a signed int to avoid undefined behavior.
|
||||||
enum Score : int { SCORE_ZERO };
|
enum Score : int { SCORE_ZERO };
|
||||||
|
|
||||||
inline Score make_score(int mg, int eg) {
|
constexpr Score make_score(int mg, int eg) {
|
||||||
return Score((int)((unsigned int)eg << 16) + mg);
|
return Score((int)((unsigned int)eg << 16) + mg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,97 +264,123 @@ inline Score make_score(int mg, int eg) {
|
|||||||
/// according to the standard a simple cast to short is implementation defined
|
/// according to the standard a simple cast to short is implementation defined
|
||||||
/// and so is a right shift of a signed integer.
|
/// and so is a right shift of a signed integer.
|
||||||
inline Value eg_value(Score s) {
|
inline Value eg_value(Score s) {
|
||||||
|
|
||||||
union { uint16_t u; int16_t s; } eg = { uint16_t(unsigned(s + 0x8000) >> 16) };
|
union { uint16_t u; int16_t s; } eg = { uint16_t(unsigned(s + 0x8000) >> 16) };
|
||||||
return Value(eg.s);
|
return Value(eg.s);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Value mg_value(Score s) {
|
inline Value mg_value(Score s) {
|
||||||
|
|
||||||
union { uint16_t u; int16_t s; } mg = { uint16_t(unsigned(s)) };
|
union { uint16_t u; int16_t s; } mg = { uint16_t(unsigned(s)) };
|
||||||
return Value(mg.s);
|
return Value(mg.s);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define ENABLE_BASE_OPERATORS_ON(T) \
|
#define ENABLE_BASE_OPERATORS_ON(T) \
|
||||||
inline T operator+(T d1, T d2) { return T(int(d1) + int(d2)); } \
|
constexpr T operator+(T d1, T d2) { return T(int(d1) + int(d2)); } \
|
||||||
inline T operator-(T d1, T d2) { return T(int(d1) - int(d2)); } \
|
constexpr T operator-(T d1, T d2) { return T(int(d1) - int(d2)); } \
|
||||||
inline T operator*(int i, T d) { return T(i * int(d)); } \
|
constexpr T operator-(T d) { return T(-int(d)); } \
|
||||||
inline T operator*(T d, int i) { return T(int(d) * i); } \
|
inline T& operator+=(T& d1, T d2) { return d1 = d1 + d2; } \
|
||||||
inline T operator-(T d) { return T(-int(d)); } \
|
inline T& operator-=(T& d1, T d2) { return d1 = d1 - d2; }
|
||||||
inline T& operator+=(T& d1, T d2) { return d1 = d1 + d2; } \
|
|
||||||
inline T& operator-=(T& d1, T d2) { return d1 = d1 - d2; } \
|
|
||||||
inline T& operator*=(T& d, int i) { return d = T(int(d) * i); }
|
|
||||||
|
|
||||||
#define ENABLE_FULL_OPERATORS_ON(T) \
|
#define ENABLE_INCR_OPERATORS_ON(T) \
|
||||||
ENABLE_BASE_OPERATORS_ON(T) \
|
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
|
||||||
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
|
inline T& operator--(T& d) { return d = T(int(d) - 1); }
|
||||||
inline T& operator--(T& d) { return d = T(int(d) - 1); } \
|
|
||||||
inline T operator/(T d, int i) { return T(int(d) / i); } \
|
#define ENABLE_FULL_OPERATORS_ON(T) \
|
||||||
inline int operator/(T d1, T d2) { return int(d1) / int(d2); } \
|
ENABLE_BASE_OPERATORS_ON(T) \
|
||||||
|
ENABLE_INCR_OPERATORS_ON(T) \
|
||||||
|
constexpr T operator*(int i, T d) { return T(i * int(d)); } \
|
||||||
|
constexpr T operator*(T d, int i) { return T(int(d) * i); } \
|
||||||
|
constexpr T operator/(T d, int i) { return T(int(d) / i); } \
|
||||||
|
constexpr int operator/(T d1, T d2) { return int(d1) / int(d2); } \
|
||||||
|
inline T& operator*=(T& d, int i) { return d = T(int(d) * i); } \
|
||||||
inline T& operator/=(T& d, int i) { return d = T(int(d) / i); }
|
inline T& operator/=(T& d, int i) { return d = T(int(d) / i); }
|
||||||
|
|
||||||
ENABLE_FULL_OPERATORS_ON(Value)
|
ENABLE_FULL_OPERATORS_ON(Value)
|
||||||
ENABLE_FULL_OPERATORS_ON(PieceType)
|
|
||||||
ENABLE_FULL_OPERATORS_ON(Piece)
|
|
||||||
ENABLE_FULL_OPERATORS_ON(Color)
|
|
||||||
ENABLE_FULL_OPERATORS_ON(Depth)
|
ENABLE_FULL_OPERATORS_ON(Depth)
|
||||||
ENABLE_FULL_OPERATORS_ON(Square)
|
ENABLE_FULL_OPERATORS_ON(Direction)
|
||||||
ENABLE_FULL_OPERATORS_ON(File)
|
|
||||||
ENABLE_FULL_OPERATORS_ON(Rank)
|
ENABLE_INCR_OPERATORS_ON(PieceType)
|
||||||
|
ENABLE_INCR_OPERATORS_ON(Piece)
|
||||||
|
ENABLE_INCR_OPERATORS_ON(Color)
|
||||||
|
ENABLE_INCR_OPERATORS_ON(Square)
|
||||||
|
ENABLE_INCR_OPERATORS_ON(File)
|
||||||
|
ENABLE_INCR_OPERATORS_ON(Rank)
|
||||||
|
|
||||||
ENABLE_BASE_OPERATORS_ON(Score)
|
ENABLE_BASE_OPERATORS_ON(Score)
|
||||||
|
|
||||||
#undef ENABLE_FULL_OPERATORS_ON
|
#undef ENABLE_FULL_OPERATORS_ON
|
||||||
|
#undef ENABLE_INCR_OPERATORS_ON
|
||||||
#undef ENABLE_BASE_OPERATORS_ON
|
#undef ENABLE_BASE_OPERATORS_ON
|
||||||
|
|
||||||
/// Additional operators to add integers to a Value
|
/// Additional operators to add integers to a Value
|
||||||
inline Value operator+(Value v, int i) { return Value(int(v) + i); }
|
constexpr Value operator+(Value v, int i) { return Value(int(v) + i); }
|
||||||
inline Value operator-(Value v, int i) { return Value(int(v) - i); }
|
constexpr Value operator-(Value v, int i) { return Value(int(v) - i); }
|
||||||
inline Value& operator+=(Value& v, int i) { return v = v + i; }
|
inline Value& operator+=(Value& v, int i) { return v = v + i; }
|
||||||
inline Value& operator-=(Value& v, int i) { return v = v - i; }
|
inline Value& operator-=(Value& v, int i) { return v = v - i; }
|
||||||
|
|
||||||
|
/// Additional operators to add a Direction to a Square
|
||||||
|
constexpr Square operator+(Square s, Direction d) { return Square(int(s) + int(d)); }
|
||||||
|
constexpr Square operator-(Square s, Direction d) { return Square(int(s) - int(d)); }
|
||||||
|
inline Square& operator+=(Square& s, Direction d) { return s = s + d; }
|
||||||
|
inline Square& operator-=(Square& s, Direction d) { return s = s - d; }
|
||||||
|
|
||||||
/// Only declared but not defined. We don't want to multiply two scores due to
|
/// Only declared but not defined. We don't want to multiply two scores due to
|
||||||
/// a very high risk of overflow. So user should explicitly convert to integer.
|
/// a very high risk of overflow. So user should explicitly convert to integer.
|
||||||
inline Score operator*(Score s1, Score s2);
|
Score operator*(Score, Score) = delete;
|
||||||
|
|
||||||
/// Division of a Score must be handled separately for each term
|
/// Division of a Score must be handled separately for each term
|
||||||
inline Score operator/(Score s, int i) {
|
inline Score operator/(Score s, int i) {
|
||||||
return make_score(mg_value(s) / i, eg_value(s) / i);
|
return make_score(mg_value(s) / i, eg_value(s) / i);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Color operator~(Color c) {
|
/// Multiplication of a Score by an integer. We check for overflow in debug mode.
|
||||||
|
inline Score operator*(Score s, int i) {
|
||||||
|
|
||||||
|
Score result = Score(int(s) * i);
|
||||||
|
|
||||||
|
assert(eg_value(result) == (i * eg_value(s)));
|
||||||
|
assert(mg_value(result) == (i * mg_value(s)));
|
||||||
|
assert((i == 0) || (result / i) == s);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr Color operator~(Color c) {
|
||||||
return Color(c ^ BLACK); // Toggle color
|
return Color(c ^ BLACK); // Toggle color
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Square operator~(Square s) {
|
constexpr Square operator~(Square s) {
|
||||||
return Square(s ^ SQ_A8); // Vertical flip SQ_A1 -> SQ_A8
|
return Square(s ^ SQ_A8); // Vertical flip SQ_A1 -> SQ_A8
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Piece operator~(Piece pc) {
|
constexpr File operator~(File f) {
|
||||||
|
return File(f ^ FILE_H); // Horizontal flip FILE_A -> FILE_H
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr Piece operator~(Piece pc) {
|
||||||
return Piece(pc ^ 8); // Swap color of piece B_KNIGHT -> W_KNIGHT
|
return Piece(pc ^ 8); // Swap color of piece B_KNIGHT -> W_KNIGHT
|
||||||
}
|
}
|
||||||
|
|
||||||
inline CastlingRight operator|(Color c, CastlingSide s) {
|
constexpr CastlingRight operator|(Color c, CastlingSide s) {
|
||||||
return CastlingRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
|
return CastlingRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Value mate_in(int ply) {
|
constexpr Value mate_in(int ply) {
|
||||||
return VALUE_MATE - ply;
|
return VALUE_MATE - ply;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Value mated_in(int ply) {
|
constexpr Value mated_in(int ply) {
|
||||||
return -VALUE_MATE + ply;
|
return -VALUE_MATE + ply;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Square make_square(File f, Rank r) {
|
constexpr Square make_square(File f, Rank r) {
|
||||||
return Square((r << 3) + f);
|
return Square((r << 3) + f);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Piece make_piece(Color c, PieceType pt) {
|
constexpr Piece make_piece(Color c, PieceType pt) {
|
||||||
return Piece((c << 3) + pt);
|
return Piece((c << 3) + pt);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline PieceType type_of(Piece pc) {
|
constexpr PieceType type_of(Piece pc) {
|
||||||
return PieceType(pc & 7);
|
return PieceType(pc & 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -370,27 +389,27 @@ inline Color color_of(Piece pc) {
|
|||||||
return Color(pc >> 3);
|
return Color(pc >> 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool is_ok(Square s) {
|
constexpr bool is_ok(Square s) {
|
||||||
return s >= SQ_A1 && s <= SQ_H8;
|
return s >= SQ_A1 && s <= SQ_H8;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline File file_of(Square s) {
|
constexpr File file_of(Square s) {
|
||||||
return File(s & 7);
|
return File(s & 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Rank rank_of(Square s) {
|
constexpr Rank rank_of(Square s) {
|
||||||
return Rank(s >> 3);
|
return Rank(s >> 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Square relative_square(Color c, Square s) {
|
constexpr Square relative_square(Color c, Square s) {
|
||||||
return Square(s ^ (c * 56));
|
return Square(s ^ (c * 56));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Rank relative_rank(Color c, Rank r) {
|
constexpr Rank relative_rank(Color c, Rank r) {
|
||||||
return Rank(r ^ (c * 7));
|
return Rank(r ^ (c * 7));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Rank relative_rank(Color c, Square s) {
|
constexpr Rank relative_rank(Color c, Square s) {
|
||||||
return relative_rank(c, rank_of(s));
|
return relative_rank(c, rank_of(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -399,36 +418,40 @@ inline bool opposite_colors(Square s1, Square s2) {
|
|||||||
return ((s >> 3) ^ s) & 1;
|
return ((s >> 3) ^ s) & 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Square pawn_push(Color c) {
|
constexpr Direction pawn_push(Color c) {
|
||||||
return c == WHITE ? NORTH : SOUTH;
|
return c == WHITE ? NORTH : SOUTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Square from_sq(Move m) {
|
constexpr Square from_sq(Move m) {
|
||||||
return Square((m >> 6) & 0x3F);
|
return Square((m >> 6) & 0x3F);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Square to_sq(Move m) {
|
constexpr Square to_sq(Move m) {
|
||||||
return Square(m & 0x3F);
|
return Square(m & 0x3F);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline MoveType type_of(Move m) {
|
constexpr int from_to(Move m) {
|
||||||
|
return m & 0xFFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr MoveType type_of(Move m) {
|
||||||
return MoveType(m & (3 << 14));
|
return MoveType(m & (3 << 14));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline PieceType promotion_type(Move m) {
|
constexpr PieceType promotion_type(Move m) {
|
||||||
return PieceType(((m >> 12) & 3) + KNIGHT);
|
return PieceType(((m >> 12) & 3) + KNIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Move make_move(Square from, Square to) {
|
constexpr Move make_move(Square from, Square to) {
|
||||||
return Move((from << 6) + to);
|
return Move((from << 6) + to);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<MoveType T>
|
template<MoveType T>
|
||||||
inline 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool is_ok(Move m) {
|
constexpr bool is_ok(Move m) {
|
||||||
return from_sq(m) != to_sq(m); // Catch MOVE_NULL and MOVE_NONE
|
return from_sq(m) != to_sq(m); // Catch MOVE_NULL and MOVE_NONE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+84
-57
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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,6 +18,7 @@
|
|||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -28,29 +29,26 @@
|
|||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "timeman.h"
|
#include "timeman.h"
|
||||||
|
#include "tt.h"
|
||||||
#include "uci.h"
|
#include "uci.h"
|
||||||
|
#include "syzygy/tbprobe.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
extern void benchmark(const Position& pos, istream& is);
|
extern vector<string> setup_bench(const Position&, istream&);
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// FEN string of the initial position, normal chess
|
// FEN string of the initial position, normal 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";
|
||||||
|
|
||||||
// A list to keep track of the position states along the setup moves (from the
|
|
||||||
// start position to the position just before the search starts). Needed by
|
|
||||||
// 'draw by repetition' detection.
|
|
||||||
StateListPtr States(new std::deque<StateInfo>(1));
|
|
||||||
|
|
||||||
|
|
||||||
// position() is called when engine receives the "position" UCI command.
|
// position() is called when engine receives the "position" UCI command.
|
||||||
// The function sets up the position described in the given FEN string ("fen")
|
// The function sets up the position described in the given FEN string ("fen")
|
||||||
// or the starting position ("startpos") and then makes the moves given in the
|
// or the starting position ("startpos") and then makes the moves given in the
|
||||||
// following move list ("moves").
|
// following move list ("moves").
|
||||||
|
|
||||||
void position(Position& pos, istringstream& is) {
|
void position(Position& pos, istringstream& is, StateListPtr& states) {
|
||||||
|
|
||||||
Move m;
|
Move m;
|
||||||
string token, fen;
|
string token, fen;
|
||||||
@@ -68,14 +66,14 @@ namespace {
|
|||||||
else
|
else
|
||||||
return;
|
return;
|
||||||
|
|
||||||
States = StateListPtr(new std::deque<StateInfo>(1));
|
states = StateListPtr(new std::deque<StateInfo>(1)); // Drop old 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 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->push_back(StateInfo());
|
states->emplace_back();
|
||||||
pos.do_move(m, States->back(), pos.gives_check(m));
|
pos.do_move(m, states->back());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,11 +89,11 @@ namespace {
|
|||||||
|
|
||||||
// Read option name (can contain spaces)
|
// Read option name (can contain spaces)
|
||||||
while (is >> token && token != "value")
|
while (is >> token && token != "value")
|
||||||
name += string(" ", name.empty() ? 0 : 1) + token;
|
name += (name.empty() ? "" : " ") + token;
|
||||||
|
|
||||||
// Read option value (can contain spaces)
|
// Read option value (can contain spaces)
|
||||||
while (is >> token)
|
while (is >> token)
|
||||||
value += string(" ", value.empty() ? 0 : 1) + token;
|
value += (value.empty() ? "" : " ") + token;
|
||||||
|
|
||||||
if (Options.count(name))
|
if (Options.count(name))
|
||||||
Options[name] = value;
|
Options[name] = value;
|
||||||
@@ -108,10 +106,11 @@ namespace {
|
|||||||
// the thinking time and other parameters from the input string, then starts
|
// the thinking time and other parameters from the input string, then starts
|
||||||
// the search.
|
// the search.
|
||||||
|
|
||||||
void go(Position& pos, istringstream& is) {
|
void go(Position& pos, istringstream& is, StateListPtr& states) {
|
||||||
|
|
||||||
Search::LimitsType limits;
|
Search::LimitsType limits;
|
||||||
string token;
|
string token;
|
||||||
|
bool ponderMode = false;
|
||||||
|
|
||||||
limits.startTime = now(); // As early as possible!
|
limits.startTime = now(); // As early as possible!
|
||||||
|
|
||||||
@@ -129,10 +128,53 @@ namespace {
|
|||||||
else if (token == "nodes") is >> limits.nodes;
|
else if (token == "nodes") is >> limits.nodes;
|
||||||
else if (token == "movetime") is >> limits.movetime;
|
else if (token == "movetime") is >> limits.movetime;
|
||||||
else if (token == "mate") is >> limits.mate;
|
else if (token == "mate") is >> limits.mate;
|
||||||
|
else if (token == "perft") is >> limits.perft;
|
||||||
else if (token == "infinite") limits.infinite = 1;
|
else if (token == "infinite") limits.infinite = 1;
|
||||||
else if (token == "ponder") limits.ponder = 1;
|
else if (token == "ponder") ponderMode = true;
|
||||||
|
|
||||||
Threads.start_thinking(pos, States, limits);
|
Threads.start_thinking(pos, states, limits, ponderMode);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// bench() is called when engine receives the "bench" command. Firstly
|
||||||
|
// a list of UCI commands is setup according to bench parameters, then
|
||||||
|
// it is run one by one printing a summary at the end.
|
||||||
|
|
||||||
|
void bench(Position& pos, istream& args, StateListPtr& states) {
|
||||||
|
|
||||||
|
string token;
|
||||||
|
uint64_t num, nodes = 0, cnt = 1;
|
||||||
|
|
||||||
|
vector<string> list = setup_bench(pos, args);
|
||||||
|
num = count_if(list.begin(), list.end(), [](string s) { return s.find("go ") == 0; });
|
||||||
|
|
||||||
|
TimePoint elapsed = now();
|
||||||
|
|
||||||
|
for (const auto& cmd : list)
|
||||||
|
{
|
||||||
|
istringstream is(cmd);
|
||||||
|
is >> skipws >> token;
|
||||||
|
|
||||||
|
if (token == "go")
|
||||||
|
{
|
||||||
|
cerr << "\nPosition: " << cnt++ << '/' << num << endl;
|
||||||
|
go(pos, is, states);
|
||||||
|
Threads.main()->wait_for_search_finished();
|
||||||
|
nodes += Threads.nodes_searched();
|
||||||
|
}
|
||||||
|
else if (token == "setoption") setoption(is);
|
||||||
|
else if (token == "position") position(pos, is, states);
|
||||||
|
else if (token == "ucinewgame") Search::clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
elapsed = now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero'
|
||||||
|
|
||||||
|
dbg_print(); // Just before exiting
|
||||||
|
|
||||||
|
cerr << "\n==========================="
|
||||||
|
<< "\nTotal time (ms) : " << elapsed
|
||||||
|
<< "\nNodes searched : " << nodes
|
||||||
|
<< "\nNodes/second : " << 1000 * nodes / elapsed << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -148,8 +190,10 @@ void UCI::loop(int argc, char* argv[]) {
|
|||||||
|
|
||||||
Position pos;
|
Position pos;
|
||||||
string token, cmd;
|
string token, cmd;
|
||||||
|
StateListPtr states(new std::deque<StateInfo>(1));
|
||||||
|
auto uiThread = std::make_shared<Thread>(0);
|
||||||
|
|
||||||
pos.set(StartFEN, false, &States->back(), Threads.main());
|
pos.set(StartFEN, false, &states->back(), uiThread.get());
|
||||||
|
|
||||||
for (int i = 1; i < argc; ++i)
|
for (int i = 1; i < argc; ++i)
|
||||||
cmd += std::string(argv[i]) + " ";
|
cmd += std::string(argv[i]) + " ";
|
||||||
@@ -160,61 +204,42 @@ void UCI::loop(int argc, char* argv[]) {
|
|||||||
|
|
||||||
istringstream is(cmd);
|
istringstream is(cmd);
|
||||||
|
|
||||||
token.clear(); // getline() could return empty or blank line
|
token.clear(); // Avoid a stale if getline() returns empty or blank line
|
||||||
is >> skipws >> token;
|
is >> skipws >> token;
|
||||||
|
|
||||||
// The GUI sends 'ponderhit' to tell us to ponder on the same move the
|
// The GUI sends 'ponderhit' to tell us the user has played the expected move.
|
||||||
// opponent has played. In case Signals.stopOnPonderhit is set we are
|
// So 'ponderhit' will be sent if we were told to ponder on the same move the
|
||||||
// waiting for 'ponderhit' to stop the search (for instance because we
|
// user has played. We should continue searching but switch from pondering to
|
||||||
// already ran out of time), otherwise we should continue searching but
|
// normal search. In case Threads.stopOnPonderhit is set we are waiting for
|
||||||
// switching from pondering to normal search.
|
// 'ponderhit' to stop the search, for instance if max search depth is reached.
|
||||||
if ( token == "quit"
|
if ( token == "quit"
|
||||||
|| token == "stop"
|
|| token == "stop"
|
||||||
|| (token == "ponderhit" && Search::Signals.stopOnPonderhit))
|
|| (token == "ponderhit" && Threads.stopOnPonderhit))
|
||||||
{
|
Threads.stop = true;
|
||||||
Search::Signals.stop = true;
|
|
||||||
Threads.main()->start_searching(true); // Could be sleeping
|
|
||||||
}
|
|
||||||
else if (token == "ponderhit")
|
else if (token == "ponderhit")
|
||||||
Search::Limits.ponder = 0; // Switch to normal search
|
Threads.ponder = false; // Switch to normal search
|
||||||
|
|
||||||
else if (token == "uci")
|
else if (token == "uci")
|
||||||
sync_cout << "id name " << engine_info(true)
|
sync_cout << "id name " << engine_info(true)
|
||||||
<< "\n" << Options
|
<< "\n" << Options
|
||||||
<< "\nuciok" << sync_endl;
|
<< "\nuciok" << sync_endl;
|
||||||
|
|
||||||
else if (token == "ucinewgame")
|
|
||||||
{
|
|
||||||
Search::clear();
|
|
||||||
Time.availableNodes = 0;
|
|
||||||
}
|
|
||||||
else if (token == "isready") sync_cout << "readyok" << sync_endl;
|
|
||||||
else if (token == "go") go(pos, is);
|
|
||||||
else if (token == "position") position(pos, is);
|
|
||||||
else if (token == "setoption") setoption(is);
|
else if (token == "setoption") setoption(is);
|
||||||
|
else if (token == "go") go(pos, is, states);
|
||||||
|
else if (token == "position") position(pos, is, states);
|
||||||
|
else if (token == "ucinewgame") Search::clear();
|
||||||
|
else if (token == "isready") sync_cout << "readyok" << sync_endl;
|
||||||
|
|
||||||
// Additional custom non-UCI commands, useful for debugging
|
// Additional custom non-UCI commands, mainly for debugging
|
||||||
else if (token == "flip") pos.flip();
|
else if (token == "flip") pos.flip();
|
||||||
else if (token == "bench") benchmark(pos, is);
|
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") sync_cout << Eval::trace(pos) << sync_endl;
|
else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
|
||||||
else if (token == "perft")
|
|
||||||
{
|
|
||||||
int depth;
|
|
||||||
stringstream ss;
|
|
||||||
|
|
||||||
is >> depth;
|
|
||||||
ss << Options["Hash"] << " "
|
|
||||||
<< Options["Threads"] << " " << depth << " current perft";
|
|
||||||
|
|
||||||
benchmark(pos, ss);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
sync_cout << "Unknown command: " << cmd << sync_endl;
|
sync_cout << "Unknown command: " << cmd << sync_endl;
|
||||||
|
|
||||||
} while (token != "quit" && argc == 1); // Passed args have one-shot behaviour
|
} while (token != "quit" && argc == 1); // Command line args are one-shot
|
||||||
|
|
||||||
Threads.main()->wait_for_search_finished();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -227,6 +252,8 @@ void UCI::loop(int argc, char* argv[]) {
|
|||||||
|
|
||||||
string UCI::value(Value v) {
|
string UCI::value(Value v) {
|
||||||
|
|
||||||
|
assert(-VALUE_INFINITE < v && v < VALUE_INFINITE);
|
||||||
|
|
||||||
stringstream ss;
|
stringstream ss;
|
||||||
|
|
||||||
if (abs(v) < VALUE_MATE - MAX_PLY)
|
if (abs(v) < VALUE_MATE - MAX_PLY)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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
|
||||||
@@ -49,12 +49,14 @@ public:
|
|||||||
Option(OnChange = nullptr);
|
Option(OnChange = nullptr);
|
||||||
Option(bool v, OnChange = nullptr);
|
Option(bool v, OnChange = nullptr);
|
||||||
Option(const char* v, OnChange = nullptr);
|
Option(const char* v, OnChange = nullptr);
|
||||||
Option(int v, int min, int max, OnChange = nullptr);
|
Option(double v, int minv, int maxv, OnChange = nullptr);
|
||||||
|
Option(const char* v, const char* cur, OnChange = nullptr);
|
||||||
|
|
||||||
Option& operator=(const std::string&);
|
Option& operator=(const std::string&);
|
||||||
void operator<<(const Option&);
|
void operator<<(const Option&);
|
||||||
operator int() const;
|
operator double() const;
|
||||||
operator std::string() const;
|
operator std::string() const;
|
||||||
|
bool operator==(const char*) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend std::ostream& operator<<(std::ostream&, const OptionsMap&);
|
friend std::ostream& operator<<(std::ostream&, const OptionsMap&);
|
||||||
|
|||||||
+27
-13
@@ -2,7 +2,7 @@
|
|||||||
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-2008 Tord Romstad (Glaurung author)
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
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
|
||||||
@@ -39,7 +39,7 @@ namespace UCI {
|
|||||||
void on_clear_hash(const Option&) { Search::clear(); }
|
void on_clear_hash(const Option&) { Search::clear(); }
|
||||||
void on_hash_size(const Option& o) { TT.resize(o); }
|
void on_hash_size(const Option& o) { TT.resize(o); }
|
||||||
void on_logger(const Option& o) { start_logger(o); }
|
void on_logger(const Option& o) { start_logger(o); }
|
||||||
void on_threads(const Option&) { Threads.read_uci_options(); }
|
void on_threads(const Option& o) { Threads.set(o); }
|
||||||
void on_tb_path(const Option& o) { Tablebases::init(o); }
|
void on_tb_path(const Option& o) { Tablebases::init(o); }
|
||||||
|
|
||||||
|
|
||||||
@@ -55,11 +55,13 @@ bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const
|
|||||||
|
|
||||||
void init(OptionsMap& o) {
|
void init(OptionsMap& o) {
|
||||||
|
|
||||||
const int MaxHashMB = Is64Bit ? 1024 * 1024 : 2048;
|
// at most 2^32 clusters.
|
||||||
|
constexpr int MaxHashMB = Is64Bit ? 131072 : 2048;
|
||||||
|
|
||||||
o["Debug Log File"] << Option("", on_logger);
|
o["Debug Log File"] << Option("", on_logger);
|
||||||
o["Contempt"] << Option(0, -100, 100);
|
o["Contempt"] << Option(24, -100, 100);
|
||||||
o["Threads"] << Option(1, 1, 128, 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);
|
||||||
@@ -67,13 +69,14 @@ void init(OptionsMap& o) {
|
|||||||
o["Skill Level"] << Option(20, 0, 20);
|
o["Skill Level"] << Option(20, 0, 20);
|
||||||
o["Move Overhead"] << Option(30, 0, 5000);
|
o["Move Overhead"] << Option(30, 0, 5000);
|
||||||
o["Minimum Thinking Time"] << Option(20, 0, 5000);
|
o["Minimum Thinking Time"] << Option(20, 0, 5000);
|
||||||
o["Slow Mover"] << Option(89, 10, 1000);
|
o["Slow Mover"] << Option(84, 10, 1000);
|
||||||
o["nodestime"] << Option(0, 0, 10000);
|
o["nodestime"] << Option(0, 0, 10000);
|
||||||
o["UCI_Chess960"] << Option(false);
|
o["UCI_Chess960"] << Option(false);
|
||||||
|
o["UCI_AnalyseMode"] << Option(false);
|
||||||
o["SyzygyPath"] << Option("<empty>", on_tb_path);
|
o["SyzygyPath"] << Option("<empty>", on_tb_path);
|
||||||
o["SyzygyProbeDepth"] << Option(1, 1, 100);
|
o["SyzygyProbeDepth"] << Option(1, 1, 100);
|
||||||
o["Syzygy50MoveRule"] << Option(true);
|
o["Syzygy50MoveRule"] << Option(true);
|
||||||
o["SyzygyProbeLimit"] << Option(6, 0, 6);
|
o["SyzygyProbeLimit"] << Option(7, 0, 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -89,11 +92,13 @@ std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
|
|||||||
const Option& o = it.second;
|
const Option& o = it.second;
|
||||||
os << "\noption name " << it.first << " type " << o.type;
|
os << "\noption name " << it.first << " type " << o.type;
|
||||||
|
|
||||||
if (o.type != "button")
|
if (o.type == "string" || o.type == "check" || o.type == "combo")
|
||||||
os << " default " << o.defaultValue;
|
os << " default " << o.defaultValue;
|
||||||
|
|
||||||
if (o.type == "spin")
|
if (o.type == "spin")
|
||||||
os << " min " << o.min << " max " << o.max;
|
os << " default " << int(stof(o.defaultValue))
|
||||||
|
<< " min " << o.min
|
||||||
|
<< " max " << o.max;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -113,12 +118,15 @@ Option::Option(bool v, OnChange f) : type("check"), min(0), max(0), on_change(f)
|
|||||||
Option::Option(OnChange f) : type("button"), min(0), max(0), on_change(f)
|
Option::Option(OnChange f) : type("button"), min(0), max(0), on_change(f)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Option::Option(int v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
|
Option::Option(double v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
|
||||||
{ defaultValue = currentValue = std::to_string(v); }
|
{ defaultValue = currentValue = std::to_string(v); }
|
||||||
|
|
||||||
Option::operator int() const {
|
Option::Option(const char* v, const char* cur, OnChange f) : type("combo"), min(0), max(0), on_change(f)
|
||||||
|
{ defaultValue = v; currentValue = cur; }
|
||||||
|
|
||||||
|
Option::operator double() const {
|
||||||
assert(type == "check" || type == "spin");
|
assert(type == "check" || type == "spin");
|
||||||
return (type == "spin" ? stoi(currentValue) : currentValue == "true");
|
return (type == "spin" ? stof(currentValue) : currentValue == "true");
|
||||||
}
|
}
|
||||||
|
|
||||||
Option::operator std::string() const {
|
Option::operator std::string() const {
|
||||||
@@ -126,6 +134,12 @@ Option::operator std::string() const {
|
|||||||
return currentValue;
|
return currentValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Option::operator==(const char* s) const {
|
||||||
|
assert(type == "combo");
|
||||||
|
return !CaseInsensitiveLess()(currentValue, s)
|
||||||
|
&& !CaseInsensitiveLess()(s, currentValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// operator<<() inits options and assigns idx in the correct printing order
|
/// operator<<() inits options and assigns idx in the correct printing order
|
||||||
|
|
||||||
@@ -148,7 +162,7 @@ Option& Option::operator=(const string& v) {
|
|||||||
|
|
||||||
if ( (type != "button" && v.empty())
|
if ( (type != "button" && v.empty())
|
||||||
|| (type == "check" && v != "true" && v != "false")
|
|| (type == "check" && v != "true" && v != "false")
|
||||||
|| (type == "spin" && (stoi(v) < min || stoi(v) > max)))
|
|| (type == "spin" && (stof(v) < min || stof(v) > max)))
|
||||||
return *this;
|
return *this;
|
||||||
|
|
||||||
if (type != "button")
|
if (type != "button")
|
||||||
|
|||||||
Executable
+144
@@ -0,0 +1,144 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# check for errors under valgrind or sanitizers.
|
||||||
|
|
||||||
|
error()
|
||||||
|
{
|
||||||
|
echo "instrumented testing failed on line $1"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
trap 'error ${LINENO}' ERR
|
||||||
|
|
||||||
|
# define suitable post and prefixes for testing options
|
||||||
|
case $1 in
|
||||||
|
--valgrind)
|
||||||
|
echo "valgrind testing started"
|
||||||
|
prefix=''
|
||||||
|
exeprefix='valgrind --error-exitcode=42'
|
||||||
|
postfix='1>/dev/null'
|
||||||
|
threads="1"
|
||||||
|
;;
|
||||||
|
--valgrind-thread)
|
||||||
|
echo "valgrind-thread testing started"
|
||||||
|
prefix=''
|
||||||
|
exeprefix='valgrind --error-exitcode=42'
|
||||||
|
postfix='1>/dev/null'
|
||||||
|
threads="2"
|
||||||
|
;;
|
||||||
|
--sanitizer-undefined)
|
||||||
|
echo "sanitizer-undefined testing started"
|
||||||
|
prefix='!'
|
||||||
|
exeprefix=''
|
||||||
|
postfix='2>&1 | grep "runtime error:"'
|
||||||
|
threads="1"
|
||||||
|
;;
|
||||||
|
--sanitizer-thread)
|
||||||
|
echo "sanitizer-thread testing started"
|
||||||
|
prefix='!'
|
||||||
|
exeprefix=''
|
||||||
|
postfix='2>&1 | grep "WARNING: ThreadSanitizer:"'
|
||||||
|
threads="2"
|
||||||
|
|
||||||
|
cat << EOF > tsan.supp
|
||||||
|
race:TTEntry::move
|
||||||
|
race:TTEntry::depth
|
||||||
|
race:TTEntry::bound
|
||||||
|
race:TTEntry::save
|
||||||
|
race:TTEntry::value
|
||||||
|
race:TTEntry::eval
|
||||||
|
|
||||||
|
race:TranspositionTable::probe
|
||||||
|
race:TranspositionTable::hashfull
|
||||||
|
|
||||||
|
EOF
|
||||||
|
|
||||||
|
export TSAN_OPTIONS="suppressions=./tsan.supp"
|
||||||
|
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "unknown testing started"
|
||||||
|
prefix=''
|
||||||
|
exeprefix=''
|
||||||
|
postfix=''
|
||||||
|
threads="1"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# simple command line testing
|
||||||
|
for args in "eval" \
|
||||||
|
"go nodes 1000" \
|
||||||
|
"go depth 10" \
|
||||||
|
"go movetime 1000" \
|
||||||
|
"go wtime 8000 btime 8000 winc 500 binc 500" \
|
||||||
|
"bench 128 $threads 10 default depth"
|
||||||
|
do
|
||||||
|
|
||||||
|
echo "$prefix $exeprefix ./stockfish $args $postfix"
|
||||||
|
eval "$prefix $exeprefix ./stockfish $args $postfix"
|
||||||
|
|
||||||
|
done
|
||||||
|
|
||||||
|
# more general testing, following an uci protocol exchange
|
||||||
|
cat << EOF > game.exp
|
||||||
|
set timeout 10
|
||||||
|
spawn $exeprefix ./stockfish
|
||||||
|
|
||||||
|
send "uci\n"
|
||||||
|
expect "uciok"
|
||||||
|
|
||||||
|
send "setoption name Threads value $threads\n"
|
||||||
|
|
||||||
|
send "ucinewgame\n"
|
||||||
|
send "position startpos\n"
|
||||||
|
send "go nodes 1000\n"
|
||||||
|
expect "bestmove"
|
||||||
|
|
||||||
|
send "position startpos moves e2e4 e7e6\n"
|
||||||
|
send "go nodes 1000\n"
|
||||||
|
expect "bestmove"
|
||||||
|
|
||||||
|
send "position fen 5rk1/1K4p1/8/8/3B4/8/8/8 b - - 0 1\n"
|
||||||
|
send "go depth 30\n"
|
||||||
|
expect "bestmove"
|
||||||
|
|
||||||
|
send "quit\n"
|
||||||
|
expect eof
|
||||||
|
|
||||||
|
# return error code of the spawned program, useful for valgrind
|
||||||
|
lassign [wait] pid spawnid os_error_flag value
|
||||||
|
exit \$value
|
||||||
|
EOF
|
||||||
|
|
||||||
|
#download TB as needed
|
||||||
|
if [ ! -d ../tests/syzygy ]; then
|
||||||
|
curl -sL https://api.github.com/repos/niklasf/python-chess/tarball/9b9aa13f9f36d08aadfabff872882f4ab1494e95 | tar -xzf -
|
||||||
|
mv niklasf-python-chess-9b9aa13 ../tests/syzygy
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat << EOF > syzygy.exp
|
||||||
|
set timeout 240
|
||||||
|
spawn $exeprefix ./stockfish
|
||||||
|
send "uci\n"
|
||||||
|
send "setoption name SyzygyPath value ../tests/syzygy/\n"
|
||||||
|
expect "info string Found 35 tablebases" {} timeout {exit 1}
|
||||||
|
send "bench 128 1 10 default depth\n"
|
||||||
|
send "quit\n"
|
||||||
|
expect eof
|
||||||
|
|
||||||
|
# return error code of the spawned program, useful for valgrind
|
||||||
|
lassign [wait] pid spawnid os_error_flag value
|
||||||
|
exit \$value
|
||||||
|
EOF
|
||||||
|
|
||||||
|
for exp in game.exp syzygy.exp
|
||||||
|
do
|
||||||
|
|
||||||
|
echo "$prefix expect $exp $postfix"
|
||||||
|
eval "$prefix expect $exp $postfix"
|
||||||
|
|
||||||
|
rm $exp
|
||||||
|
|
||||||
|
done
|
||||||
|
|
||||||
|
rm -f tsan.supp
|
||||||
|
|
||||||
|
echo "instrumented testing OK"
|
||||||
Executable
+32
@@ -0,0 +1,32 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# verify perft numbers (positions from https://chessprogramming.wikispaces.com/Perft+Results)
|
||||||
|
|
||||||
|
error()
|
||||||
|
{
|
||||||
|
echo "perft testing failed on line $1"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
trap 'error ${LINENO}' ERR
|
||||||
|
|
||||||
|
echo "perft testing started"
|
||||||
|
|
||||||
|
cat << EOF > perft.exp
|
||||||
|
set timeout 10
|
||||||
|
lassign \$argv pos depth result
|
||||||
|
spawn ./stockfish
|
||||||
|
send "position \$pos\\ngo perft \$depth\\n"
|
||||||
|
expect "Nodes searched? \$result" {} timeout {exit 1}
|
||||||
|
send "quit\\n"
|
||||||
|
expect eof
|
||||||
|
EOF
|
||||||
|
|
||||||
|
expect perft.exp startpos 5 4865609 > /dev/null
|
||||||
|
expect perft.exp "fen r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -" 5 193690690 > /dev/null
|
||||||
|
expect perft.exp "fen 8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -" 6 11030083 > /dev/null
|
||||||
|
expect perft.exp "fen r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1" 5 15833292 > /dev/null
|
||||||
|
expect perft.exp "fen rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8" 5 89941194 > /dev/null
|
||||||
|
expect perft.exp "fen r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10" 5 164075551 > /dev/null
|
||||||
|
|
||||||
|
rm perft.exp
|
||||||
|
|
||||||
|
echo "perft testing OK"
|
||||||
Executable
+61
@@ -0,0 +1,61 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# verify reproducible search
|
||||||
|
|
||||||
|
error()
|
||||||
|
{
|
||||||
|
echo "reprosearch testing failed on line $1"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
trap 'error ${LINENO}' ERR
|
||||||
|
|
||||||
|
echo "reprosearch testing started"
|
||||||
|
|
||||||
|
# repeat two short games, separated by ucinewgame.
|
||||||
|
# with go nodes $nodes they should result in exactly
|
||||||
|
# the same node count for each iteration.
|
||||||
|
cat << EOF > repeat.exp
|
||||||
|
set timeout 10
|
||||||
|
spawn ./stockfish
|
||||||
|
lassign \$argv nodes
|
||||||
|
|
||||||
|
send "uci\n"
|
||||||
|
expect "uciok"
|
||||||
|
|
||||||
|
send "ucinewgame\n"
|
||||||
|
send "position startpos\n"
|
||||||
|
send "go nodes \$nodes\n"
|
||||||
|
expect "bestmove"
|
||||||
|
|
||||||
|
send "position startpos moves e2e4 e7e6\n"
|
||||||
|
send "go nodes \$nodes\n"
|
||||||
|
expect "bestmove"
|
||||||
|
|
||||||
|
send "ucinewgame\n"
|
||||||
|
send "position startpos\n"
|
||||||
|
send "go nodes \$nodes\n"
|
||||||
|
expect "bestmove"
|
||||||
|
|
||||||
|
send "position startpos moves e2e4 e7e6\n"
|
||||||
|
send "go nodes \$nodes\n"
|
||||||
|
expect "bestmove"
|
||||||
|
|
||||||
|
send "quit\n"
|
||||||
|
expect eof
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# to increase the likelyhood of finding a non-reproducible case,
|
||||||
|
# the allowed number of nodes are varied systematically
|
||||||
|
for i in `seq 1 20`
|
||||||
|
do
|
||||||
|
|
||||||
|
nodes=$((100*3**i/2**i))
|
||||||
|
echo "reprosearch testing with $nodes nodes"
|
||||||
|
|
||||||
|
# each line should appear exactly an even number of times
|
||||||
|
expect repeat.exp $nodes 2>&1 | grep -o "nodes [0-9]*" | sort | uniq -c | awk '{if ($1%2!=0) exit(1)}'
|
||||||
|
|
||||||
|
done
|
||||||
|
|
||||||
|
rm repeat.exp
|
||||||
|
|
||||||
|
echo "reprosearch testing OK"
|
||||||
Executable
+31
@@ -0,0 +1,31 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# obtain and optionally verify Bench / signature
|
||||||
|
# if no reference is given, the output is deliberately limited to just the signature
|
||||||
|
|
||||||
|
error()
|
||||||
|
{
|
||||||
|
echo "running bench for signature failed on line $1"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
trap 'error ${LINENO}' ERR
|
||||||
|
|
||||||
|
# obtain
|
||||||
|
|
||||||
|
signature=`./stockfish bench 2>&1 | grep "Nodes searched : " | awk '{print $4}'`
|
||||||
|
|
||||||
|
if [ $# -gt 0 ]; then
|
||||||
|
# compare to given reference
|
||||||
|
if [ "$1" != "$signature" ]; then
|
||||||
|
if [ -z "$signature" ]; then
|
||||||
|
echo "No signature obtained from bench. Code crashed or assert triggered ?"
|
||||||
|
else
|
||||||
|
echo "signature mismatch: reference $1 obtained: $signature ."
|
||||||
|
fi
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
echo "signature OK: $signature"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
# just report signature
|
||||||
|
echo $signature
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user