mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 16:47:37 +00:00
Compare commits
677 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 369eff437c | |||
| 344616e917 | |||
| 40b1b27178 | |||
| bf51b4796a | |||
| e18e557e77 | |||
| 818b4a126d | |||
| e77f38c431 | |||
| e18321f55a | |||
| ca67752645 | |||
| 3686e719a1 | |||
| 9893e7fd53 | |||
| 99f3ad6858 | |||
| e27d3bb884 | |||
| 16e1881126 | |||
| e1f600f186 | |||
| a3fe80c36a | |||
| fdf3a51c68 | |||
| e61f7b1e6d | |||
| f799610d4b | |||
| 073eed590e | |||
| 1e586288ca | |||
| 0162fb83c2 | |||
| ab26c61971 | |||
| eccccba0ce | |||
| b77bae0529 | |||
| 7ae3c05795 | |||
| 351844061e | |||
| 8662bdfa12 | |||
| 28240d375c | |||
| 943ae89be1 | |||
| 4b0043ae7c | |||
| 92f01aa2bd | |||
| ea41f18e6e | |||
| 057d710fc2 | |||
| 01f2466f6e | |||
| 5c58d1f5cb | |||
| b96dd754ed | |||
| 01ee509a5c | |||
| 95ad2b51b7 | |||
| 438805aee8 | |||
| ace8e951d7 | |||
| 90ce24b11e | |||
| 4c95edddbf | |||
| 602d7fbb07 | |||
| d909d10f33 | |||
| 38428ada54 | |||
| e340ce221c | |||
| ca6c9f85a5 | |||
| c5828c4eba | |||
| d37dfe9ae4 | |||
| 13b4444d9e | |||
| 7f2eb10e93 | |||
| 5cffc032da | |||
| 2731bbaf6b | |||
| 6aa9308f08 | |||
| b4f6728e61 | |||
| 2b57b61cb1 | |||
| 1ee2838214 | |||
| 0b944c7186 | |||
| 805afcbf3d | |||
| 4c5cbb1b14 | |||
| 133808851d | |||
| 9bd856e28e | |||
| 3e739f883f | |||
| 0b4f6e562b | |||
| 5596492f6e | |||
| 8493c8c6b8 | |||
| e3af492142 | |||
| 9585f8ef58 | |||
| 7396c08b79 | |||
| 3d10cfcdd2 | |||
| 8abb98455f | |||
| b3525fa9ea | |||
| 85924db496 | |||
| f2f3a06a1a | |||
| 714329dbdc | |||
| 76971d8acb | |||
| f619f1d38c | |||
| 82d02a3133 | |||
| 1d09ee70f7 | |||
| eb20a87c67 | |||
| ade3bb9a4e | |||
| 716a145a6c | |||
| 190d2ea4bc | |||
| c2c0e6b07d | |||
| c94145b65c | |||
| 09efbf915e | |||
| 6d58bf777c | |||
| 1c0c4db677 | |||
| 8e45e70e55 | |||
| 126036abb0 | |||
| ca14345ba2 | |||
| 7d2a79f037 | |||
| 7c5d724724 | |||
| 6fed8ff22a | |||
| 0c076f1136 | |||
| e48c7547c5 | |||
| 6e2ca97d93 | |||
| 5f096e9bef | |||
| 20023ac9b8 | |||
| 3549d98d07 | |||
| 07b247f943 | |||
| abac509ccb | |||
| ab0f4c0353 | |||
| 71bfbb22fc | |||
| 7cb8cbb403 | |||
| 0784bd542b | |||
| 16c603ce9b | |||
| 969982406c | |||
| 4b9ed6566a | |||
| 5e4cd3fc0d | |||
| 5486911e01 | |||
| 3487eb9f9e | |||
| dc0030de4f | |||
| 2dd24dc4e6 | |||
| 8f934dff9a | |||
| 2694ef23c3 | |||
| e082112cfe | |||
| 4048bae47b | |||
| c737062436 | |||
| 94e41274bb | |||
| ec6aab0136 | |||
| 7eaea3848c | |||
| ee7a68ea5f | |||
| d30994ecd5 | |||
| bd04f9a0f1 | |||
| 1cbba8d6fa | |||
| e9e5f72c74 | |||
| 29b5842da8 | |||
| 8fb45caade | |||
| 900279a06f | |||
| d5b24ad77b | |||
| f256388e08 | |||
| db4b0d8b7d | |||
| 24dac5ccd3 | |||
| 8788612828 | |||
| 5d1644ba69 | |||
| 60590577f2 | |||
| 04be84e0e2 | |||
| 647402ff79 | |||
| a273b6ef8c | |||
| e1a7d135b2 | |||
| c1be0c68c7 | |||
| c13052f344 | |||
| 8de29390f2 | |||
| 45a309d92e | |||
| d5ba8e827d | |||
| 77fa960f89 | |||
| bfe9044ad9 | |||
| 744ed85a4d | |||
| 56dd58e6f9 | |||
| 4f6aa15228 | |||
| aedebe35cf | |||
| 9a10313a9d | |||
| 8c3a5bbc52 | |||
| 28933a580e | |||
| 12eb345ebd | |||
| ebec2fa48e | |||
| 552b3ccb66 | |||
| 356147d99a | |||
| 89723339d9 | |||
| 74e2fa97b7 | |||
| dcd8ce7094 | |||
| 4d1220d672 | |||
| aa31f7f096 | |||
| 1de97337c2 | |||
| 9eceb894ac | |||
| 5972c4a678 | |||
| dd9cf30581 | |||
| d4af15f682 | |||
| 9742fb10fd | |||
| 7cafbab529 | |||
| 6a79d5d10e | |||
| 3e4fed3a91 | |||
| a5c76d69c3 | |||
| 1b5b900a29 | |||
| 90f5937373 | |||
| 1e8836d921 | |||
| e3c85c314d | |||
| da34023cfc | |||
| ed72a1e9ba | |||
| 5bbd944099 | |||
| 38adb487ca | |||
| 2c1797ab81 | |||
| 9fac5774f4 | |||
| 69240a982d | |||
| 7904a7d930 | |||
| 8315620522 | |||
| 52eac1e535 | |||
| 5d077bb482 | |||
| 5a42427cb8 | |||
| 5339ab57d8 | |||
| 388630ae28 | |||
| 79f393084a | |||
| 328098d027 | |||
| 93195555ed | |||
| 07e0741dfb | |||
| 27ca89d479 | |||
| cccf3c815c | |||
| 76ed0ab501 | |||
| ce84ab6e9d | |||
| fca8dbc029 | |||
| e6eeb17aa6 | |||
| 9c9205860c | |||
| 27c5cb5912 | |||
| 86f04dbcc0 | |||
| aa242d2f84 | |||
| 8d858783e1 | |||
| 80d7556af7 | |||
| 00d9e9fd28 | |||
| 3428a28751 | |||
| 307a5a4f63 | |||
| 55758344d3 | |||
| 4eca981e55 | |||
| 5066e17eba | |||
| 328d314f2f | |||
| ecc5ff6693 | |||
| 7ea5659c5f | |||
| 8fd34d7763 | |||
| 55b46ffa90 | |||
| 08d6465d90 | |||
| 5f4d9309c8 | |||
| 436c303731 | |||
| 540b49a152 | |||
| 2e45447957 | |||
| dc3508d157 | |||
| ca38358574 | |||
| 83e19fbed5 | |||
| 9f5b31c21d | |||
| 3c0fe1d9b2 | |||
| 77b4f4c2e7 | |||
| 68fbb1e052 | |||
| 01fab4d432 | |||
| 660c38f781 | |||
| 613dc66c12 | |||
| 3e2591d83c | |||
| 46b5a5f0db | |||
| 84a641b8bb | |||
| 7f300a7698 | |||
| 087b638f6c | |||
| 7ad85fca6d | |||
| bf6bc1b37b | |||
| c052e03426 | |||
| 69a1a808c8 | |||
| 600234f2e2 | |||
| 9dbb3ae8b2 | |||
| f4ace94f91 | |||
| 1aae35b8b7 | |||
| e6310b3469 | |||
| 68d61b80c6 | |||
| a2410227cc | |||
| 4a6d59c6c7 | |||
| 51330414c4 | |||
| 42b77ea7ee | |||
| 4095ff0ee5 | |||
| 1d977aa79e | |||
| d8aeaab79c | |||
| 3054db989d | |||
| b2edac7075 | |||
| aa0dc16b75 | |||
| 112607bf49 | |||
| 7e9f776fca | |||
| 3f1bc9e013 | |||
| e8cc5c9968 | |||
| 00563be5cc | |||
| ad87d707ff | |||
| 7b8ffe0f5d | |||
| c1d3bd2dba | |||
| 5ebdf2f8c8 | |||
| 4502a1934f | |||
| 2795aedbc3 | |||
| 8e95c39bfa | |||
| 80a759d544 | |||
| 2e0ec4ad6f | |||
| e14046517e | |||
| 411e704fdf | |||
| ee0371f86e | |||
| eaeb63f1d0 | |||
| 92dbee7875 | |||
| 2e86d1febc | |||
| 946fa47625 | |||
| 7231b18af1 | |||
| 578b21bbee | |||
| 59f64fda4f | |||
| cc54a91ca5 | |||
| b7063ef65b | |||
| 078625e333 | |||
| 6c040c821a | |||
| 20e92895af | |||
| fb03188fc7 | |||
| a66c73deef | |||
| 27efc5ac99 | |||
| 36f2133df3 | |||
| ef4d89c9bd | |||
| aaf17326e2 | |||
| 2ca142a5b4 | |||
| ffbaa1c5a8 | |||
| 57dfe41ddb | |||
| 926f215061 | |||
| df722521ba | |||
| 5d1b92e8f9 | |||
| 6661a31541 | |||
| 6c42575208 | |||
| 61a6e46664 | |||
| 60beb18efc | |||
| 8463fa479e | |||
| 63500274db | |||
| 1d5eaba573 | |||
| ac8e6ff000 | |||
| 3a6753328c | |||
| 50182f1100 | |||
| e51965aa57 | |||
| a7381d5e81 | |||
| dc3a5f791e | |||
| 35a082064f | |||
| be77406a55 | |||
| 26dabb1e6b | |||
| 35b6079852 | |||
| ebf3735754 | |||
| 966bc477af | |||
| c6f987d1ad | |||
| d477a0d611 | |||
| 54889618c2 | |||
| 9a6cfee73b | |||
| 2e8552db76 | |||
| a4b2eeea75 | |||
| 13d4df95cd | |||
| bae4679de2 | |||
| a3943b2aa7 | |||
| f04f50b368 | |||
| a4b98a052e | |||
| e5da0e4b79 | |||
| d71f707040 | |||
| 558b0c848c | |||
| 062ca91db5 | |||
| 13c11f4048 | |||
| 81c7975dcd | |||
| 4b59347194 | |||
| 6027652773 | |||
| 04372316b3 | |||
| 8725494966 | |||
| be50952533 | |||
| e53774bc49 | |||
| 3017e8c604 | |||
| 8fcaa78a04 | |||
| a590d1d52d | |||
| 856a5f3aaa | |||
| 6fa6da3ee1 | |||
| cb2111f0b6 | |||
| 6645115377 | |||
| 63a5fc2366 | |||
| 0da7295795 | |||
| 1e6d21dbb6 | |||
| 9369f4963d | |||
| a459709fcc | |||
| 2dbb1adf2a | |||
| 8a2c4134a6 | |||
| fce799294c | |||
| 0b36ba74fc | |||
| 153fb216a1 | |||
| d3d26a94b3 | |||
| 38112060dc | |||
| 775f8239d3 | |||
| 098f645d26 | |||
| 29ff925fd8 | |||
| 8d16111ffd | |||
| e2226cbb20 | |||
| 5fd5453e59 | |||
| e74c2df907 | |||
| 41ccc885ec | |||
| 2f46592736 | |||
| 7ff965eebf | |||
| a6f873cd8d | |||
| 40548c9153 | |||
| 667f350737 | |||
| 950c8436ed | |||
| 8d47caa16e | |||
| b9d4e6f7fd | |||
| 193a7ae35b | |||
| 4f906a2589 | |||
| dccaa145d2 | |||
| 6656ed8904 | |||
| d65f75c153 | |||
| f8f5dcbb68 | |||
| 686b45e121 | |||
| dc13004283 | |||
| 901bfb1f55 | |||
| 20a5c07472 | |||
| e38eb2302d | |||
| 0af24a1445 | |||
| a8f9c7a790 | |||
| c0a80afe89 | |||
| 44a571c1c7 | |||
| ce8ac7997c | |||
| e118570038 | |||
| 8f10f6c9cd | |||
| 3184852bdc | |||
| 99c9cae586 | |||
| 152a4dc5cd | |||
| 47a0768102 | |||
| 170bdf40cd | |||
| 8b0fee9998 | |||
| 1277a42823 | |||
| 35aa21c1fe | |||
| 18b0809639 | |||
| f4136c5434 | |||
| ddccb5355c | |||
| 1c7a727795 | |||
| e56e5045dd | |||
| 20d6a8e57f | |||
| 0dc6f16992 | |||
| 21120288a3 | |||
| 519b2fe849 | |||
| f3189bdc9a | |||
| 65f46794af | |||
| 81d6c4a0d6 | |||
| 60c121f3b1 | |||
| 45eac9507c | |||
| bfd0f95f06 | |||
| 1b947aafbf | |||
| a7592e69d7 | |||
| a3b4e9e23c | |||
| f6f1d24223 | |||
| ce0a95c2c0 | |||
| 8aa8608c2a | |||
| 7837fb2aca | |||
| 9f0d5241bf | |||
| 5b555525d2 | |||
| c4518e395e | |||
| d8b3ad2208 | |||
| ec36b8dea9 | |||
| 14f7d9a629 | |||
| 8ebf30d44a | |||
| 5154ac9cff | |||
| 54b5b528d9 | |||
| 44643c2770 | |||
| 69407ae9b8 | |||
| 18375e3bfb | |||
| 6390a3da94 | |||
| 36f8814aa6 | |||
| 96e36a7897 | |||
| 2ca2c3f35b | |||
| f54c44e6be | |||
| ca3622f8e8 | |||
| 97a034ad3e | |||
| 1b62b47b62 | |||
| 7f51610103 | |||
| 712bbb052e | |||
| fe4fda474f | |||
| f7d8ea3866 | |||
| f53aea45e3 | |||
| 3c07603dac | |||
| c73f33f37e | |||
| 05cb58f4fc | |||
| 595fc342cf | |||
| 58fdb84b0d | |||
| ab276357d7 | |||
| 4abe333e1f | |||
| 7b20bb6e1a | |||
| 4eb2d8ce09 | |||
| b97df4c236 | |||
| 17bcdb9212 | |||
| 202a78e8e4 | |||
| 42b48b08e8 | |||
| aea2fde611 | |||
| b73ae56ee1 | |||
| 3fda064a66 | |||
| a6e292034a | |||
| c7332d5610 | |||
| 9d1e2c0e76 | |||
| c150f07291 | |||
| 62f531254e | |||
| 91cc82aa25 | |||
| 19b8249ff4 | |||
| 25b492ab58 | |||
| 4c9b423161 | |||
| 2416242c96 | |||
| 6933f05f4b | |||
| f9571e8d57 | |||
| 2bfacf136c | |||
| 296534f234 | |||
| e5c7b44f7a | |||
| 3231038262 | |||
| 9cae6e66ce | |||
| 46d5fff01f | |||
| b8fd1a78dc | |||
| 413b243809 | |||
| 0edb6348d2 | |||
| 14cf27e6f6 | |||
| 7b4828b68c | |||
| f6d220ab14 | |||
| afafdf7b73 | |||
| b15dcd9774 | |||
| 94dd204c3b | |||
| 5943600a89 | |||
| 589c711449 | |||
| 158864270a | |||
| a87da2c4b3 | |||
| fbb53524ef | |||
| ba1464751d | |||
| eeb6d923fa | |||
| c30eb4c9c9 | |||
| 35c1ccef39 | |||
| 0935dca9a6 | |||
| 314d446518 | |||
| c014444f09 | |||
| 9b4e123fbe | |||
| a43f633c19 | |||
| 66f5cd3f9d | |||
| fe07ae4cb4 | |||
| 2c52147dbf | |||
| 7caa6cd338 | |||
| 4509eb1342 | |||
| 7ad59d9ac9 | |||
| 48127fe5d3 | |||
| 84408e5cd6 | |||
| 79232be02a | |||
| 0a1f54975f | |||
| bffe32f4fe | |||
| 4aca11ae2a | |||
| 1a939cd8c8 | |||
| d65c9a3262 | |||
| 99f2c1a2a6 | |||
| 3b1f552b08 | |||
| 4840643fed | |||
| 4739037f96 | |||
| 234344500f | |||
| b777b17f6f | |||
| db4b8ee000 | |||
| c6d45c60b5 | |||
| d709a5f1c5 | |||
| 1b0df1ae14 | |||
| 57fdfdedcf | |||
| 6fb0a1bc40 | |||
| 8631b08d97 | |||
| 3d2aab11d8 | |||
| 7ebb872409 | |||
| 375797d51c | |||
| 8e98bd616e | |||
| bc83515c9e | |||
| bcbab19376 | |||
| d29a68f585 | |||
| 0608d6aaec | |||
| 2fd075d1ea | |||
| d12378497c | |||
| 8ab9c2511a | |||
| fc0733087a | |||
| 42a20920e5 | |||
| 79fa72f392 | |||
| d3091971b7 | |||
| d9caede324 | |||
| 8a7876d48d | |||
| 2ee1250294 | |||
| 5644e14d0e | |||
| d07a875398 | |||
| 460892382a | |||
| 5605cc7684 | |||
| 7d42752158 | |||
| fefb27bab4 | |||
| 7de40076ac | |||
| f1359845de | |||
| 5ab55827b8 | |||
| 9ba391c5cb | |||
| 2469daebb1 | |||
| aa60c80ade | |||
| 5cbcff55cc | |||
| 428962a2e8 | |||
| 4f6b1bf3be | |||
| 8d62ece945 | |||
| 480682b670 | |||
| 89d9db2979 | |||
| 907f912463 | |||
| c97b702f4d | |||
| 91de6b0f37 | |||
| 069073ea68 | |||
| 15e2191111 | |||
| 8a9b9ec96a | |||
| 74829342ef | |||
| 4b926f227d | |||
| 82d065b011 | |||
| e60cdca9b0 | |||
| a1b62d68ec | |||
| 222f59b9c1 | |||
| 27a1877299 | |||
| ea9c424bba | |||
| 7ed15af371 | |||
| d6613b7589 | |||
| fed3e752ae | |||
| 766fb9c67d | |||
| b652720903 | |||
| 4d0a6c5a6f | |||
| 2eec710318 | |||
| aa10d0aaa6 | |||
| ff480bdb83 | |||
| b66552fc27 | |||
| cd065dd584 | |||
| 95b24083fb | |||
| 877313a413 | |||
| 8b8885ab07 | |||
| a903ed07e0 | |||
| 880e3cd7c8 | |||
| a67c22611a | |||
| 2efeded6e3 | |||
| 6044f25d71 | |||
| 9da015517c | |||
| 888a1d3445 | |||
| 8b88ca9017 | |||
| 94fe366779 | |||
| 535f70088e | |||
| 1b69910865 | |||
| 29451de874 | |||
| 5adc678628 | |||
| f2053ba19f | |||
| 68171ecaca | |||
| 6b82a234d0 | |||
| 4758fd31b1 | |||
| 7f68fc611c | |||
| 896bd917f8 | |||
| 67a5e1ecf9 | |||
| a9b8e8b931 | |||
| 94b1bbb68b | |||
| f133f61e3f | |||
| eb50793cff | |||
| 9b30913996 | |||
| ac7780bd35 | |||
| c192b692cf | |||
| 85b08ce3ad | |||
| b9a88da4ab | |||
| a69f1d7c20 | |||
| 24ba204931 | |||
| 6b354305e1 | |||
| ffedfa3354 | |||
| e4e423bb05 | |||
| ccd823a4ff | |||
| 7ff865b924 | |||
| ec627911f0 | |||
| 22ffb588e5 | |||
| e6a8d03dd8 | |||
| 747e035c7d | |||
| 6c9f4cf36f | |||
| ab580106fd | |||
| aa23eb4440 | |||
| e8baf2b772 | |||
| 58bb23d0c3 | |||
| bbacff5e01 | |||
| 43efd7fad7 | |||
| 3b315c9ada | |||
| f7926ea41e | |||
| 264c8637a3 | |||
| 55a3e0af8d | |||
| 66c93245e0 | |||
| 73ca93f3c0 | |||
| c6fc51c5ea | |||
| 2cb4c7052e | |||
| 3c1201c20c | |||
| 84dabe5982 | |||
| e10276f45d | |||
| 2312c26722 | |||
| b6cd89aeaf | |||
| ce1c260ea9 | |||
| 4d30126e4b | |||
| 7f56d2949d | |||
| d10ae90dea | |||
| 2f75639485 | |||
| ad1167c482 | |||
| 08753771fc | |||
| 69ac45d903 | |||
| 323a006666 | |||
| 16e170d105 | |||
| adeded29fb | |||
| 187a9fe5e7 | |||
| 6f48367094 | |||
| 83a574ff27 | |||
| f4dcec0b94 | |||
| e4fc9d84d7 | |||
| 07a525cdfd | |||
| f5622cd5ec |
+55
@@ -0,0 +1,55 @@
|
|||||||
|
language: cpp
|
||||||
|
sudo: required
|
||||||
|
dist: trusty
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- os: linux
|
||||||
|
compiler: gcc
|
||||||
|
addons:
|
||||||
|
apt:
|
||||||
|
sources: ['ubuntu-toolchain-r-test']
|
||||||
|
packages: ['g++-6', 'g++-6-multilib', 'g++-multilib', 'valgrind']
|
||||||
|
env:
|
||||||
|
- COMPILER=g++-6
|
||||||
|
- COMP=gcc
|
||||||
|
|
||||||
|
- os: linux
|
||||||
|
compiler: clang
|
||||||
|
addons:
|
||||||
|
apt:
|
||||||
|
sources: ['ubuntu-toolchain-r-test']
|
||||||
|
packages: ['clang', 'g++-multilib', 'valgrind']
|
||||||
|
env:
|
||||||
|
- COMPILER=clang++
|
||||||
|
- COMP=clang
|
||||||
|
|
||||||
|
- os: osx
|
||||||
|
compiler: gcc
|
||||||
|
env:
|
||||||
|
- COMPILER=g++
|
||||||
|
- COMP=gcc
|
||||||
|
|
||||||
|
- os: osx
|
||||||
|
compiler: clang
|
||||||
|
env:
|
||||||
|
- COMPILER=clang++ V='Apple LLVM 6.0' # Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
|
||||||
|
- COMP=clang
|
||||||
|
|
||||||
|
branches:
|
||||||
|
only:
|
||||||
|
- master
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- cd src
|
||||||
|
|
||||||
|
script:
|
||||||
|
- make clean && make build ARCH=x86-64 && ./stockfish bench 2>&1 >/dev/null | grep 'Nodes searched' | tee bench1
|
||||||
|
- make clean && make build ARCH=x86-32 && ./stockfish bench 2>&1 >/dev/null | grep 'Nodes searched' | tee bench2
|
||||||
|
- echo "Checking for same bench numbers..."
|
||||||
|
- diff bench1 bench2 > result
|
||||||
|
- 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.
|
||||||
|
- 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
|
||||||
|
# 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
|
||||||
|
- if [[ "$COMPILER" == "g++-6" ]]; then make clean && make ARCH=x86-64 sanitize=yes build && ! ./stockfish bench 2>&1 | grep "runtime error:" ; fi
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
# Generated with git shortlog -sn | cut -c8-', which sorts by commits (manually ordered the first four authors)
|
||||||
|
|
||||||
|
Tord Romstad
|
||||||
|
Marco Costalba
|
||||||
|
Joona Kiiski
|
||||||
|
Gary Linscott
|
||||||
|
lucasart
|
||||||
|
mstembera
|
||||||
|
Lucas Braesch
|
||||||
|
Stefan Geschwentner
|
||||||
|
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
|
||||||
|
Dan Schmidt
|
||||||
|
H. Felix Wittmann
|
||||||
|
Joseph R. Prostko
|
||||||
|
Justin Blanchard
|
||||||
|
Linus Arver
|
||||||
|
NicklasPersson
|
||||||
|
Rodrigo Exterckötter Tjäder
|
||||||
|
Ron Britvich
|
||||||
|
Ronald de Man
|
||||||
|
RyanTaker
|
||||||
|
Vince Negri
|
||||||
|
ceebo
|
||||||
|
jhellis3
|
||||||
|
ppigazzini
|
||||||
|
shane31
|
||||||
|
Andy Duplain
|
||||||
|
Auguste Pop
|
||||||
|
Balint Pfliegel
|
||||||
|
Chris Cain
|
||||||
|
DU-jdto
|
||||||
|
Dariusz Orzechowski
|
||||||
|
DiscanX
|
||||||
|
Ernesto Gatti
|
||||||
|
Gregor Cramer
|
||||||
|
Guenther Demetz
|
||||||
|
Hiraoka Takuya
|
||||||
|
Hongzhi Cheng
|
||||||
|
Joseph Hellis
|
||||||
|
Kelly Wilson
|
||||||
|
Ken T Takusagawa
|
||||||
|
Kojirion
|
||||||
|
Luca Brivio
|
||||||
|
Matt Sullivan
|
||||||
|
Matthew Lai
|
||||||
|
Matthew Sullivan
|
||||||
|
Michel Van den Bergh
|
||||||
|
Mysseno
|
||||||
|
Oskar Werkelin Ahlin
|
||||||
|
Pablo Vazquez
|
||||||
|
Pascal Romaret
|
||||||
|
Ralph Stoesser
|
||||||
|
Ralph Stößer
|
||||||
|
Raminder Singh
|
||||||
|
Richard Lloyd
|
||||||
|
Ryan Takker
|
||||||
|
Stephane Nicolet
|
||||||
|
Thanar2
|
||||||
|
absimaldata
|
||||||
|
braich
|
||||||
|
gguliash
|
||||||
|
kinderchocolate
|
||||||
|
loco-loco
|
||||||
|
pellanda
|
||||||
|
renouve
|
||||||
|
sf-x
|
||||||
|
thaspel
|
||||||
|
unknown
|
||||||
|
uriblass
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
### Overview
|
### Overview
|
||||||
|
|
||||||
|
[](https://travis-ci.org/official-stockfish/Stockfish)
|
||||||
|
[](https://ci.appveyor.com/project/mcostalba/stockfish)
|
||||||
|
|
||||||
Stockfish is a free UCI chess engine derived from Glaurung 2.1. It is
|
Stockfish is a free UCI chess engine derived from Glaurung 2.1. It is
|
||||||
not a complete chess program and requires some UCI-compatible GUI
|
not a complete chess program and requires some UCI-compatible GUI
|
||||||
(e.g. XBoard with PolyGlot, eboard, Arena, Sigma Chess, Shredder, Chess
|
(e.g. XBoard with PolyGlot, eboard, Arena, Sigma Chess, Shredder, Chess
|
||||||
@@ -7,11 +10,13 @@ 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 CPUs. The engine defaults
|
This version of Stockfish supports up to 128 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.
|
||||||
|
|
||||||
|
This version of Stockfish has support for Syzygybases.
|
||||||
|
|
||||||
|
|
||||||
### Files
|
### Files
|
||||||
|
|
||||||
@@ -22,19 +27,59 @@ This distribution of Stockfish consists of the following files:
|
|||||||
* Copying.txt, a text file containing the GNU General Public License.
|
* Copying.txt, a text file containing the GNU General Public License.
|
||||||
|
|
||||||
* src, a subdirectory containing the full source code, including a Makefile
|
* src, a subdirectory containing the full source code, including a Makefile
|
||||||
that can be used to compile Stockfish on Unix-like systems. For further
|
that can be used to compile Stockfish on Unix-like systems.
|
||||||
information about how to compile Stockfish yourself read section below.
|
|
||||||
|
|
||||||
* polyglot.ini, for using Stockfish with Fabien Letouzey's PolyGlot
|
|
||||||
adapter.
|
|
||||||
|
|
||||||
|
|
||||||
### Opening books
|
### Syzygybases
|
||||||
|
|
||||||
This version of Stockfish has support for PolyGlot opening books. For
|
**Configuration**
|
||||||
information about how to create such books, consult the PolyGlot
|
|
||||||
documentation. The book file can be selected by setting the *Book File*
|
Syzygybases are configured using the UCI options "SyzygyPath",
|
||||||
UCI parameter.
|
"SyzygyProbeDepth", "Syzygy50MoveRule" and "SyzygyProbeLimit".
|
||||||
|
|
||||||
|
The option "SyzygyPath" should be set to the directory or directories that
|
||||||
|
contain the .rtbw and .rtbz files. Multiple directories should be
|
||||||
|
separated by ";" on Windows and by ":" on Unix-based operating systems.
|
||||||
|
**Do not use spaces around the ";" or ":".**
|
||||||
|
|
||||||
|
Example: `C:\tablebases\wdl345;C:\tablebases\wdl6;D:\tablebases\dtz345;D:\tablebases\dtz6`
|
||||||
|
|
||||||
|
It is recommended to store .rtbw files on an SSD. There is no loss in
|
||||||
|
storing the .rtbz files on a regular HD.
|
||||||
|
|
||||||
|
Increasing the "SyzygyProbeDepth" option lets the engine probe less
|
||||||
|
aggressively. Set this option to a higher value if you experience too much
|
||||||
|
slowdown (in terms of nps) due to TB probing.
|
||||||
|
|
||||||
|
Set the "Syzygy50MoveRule" option to false if you want tablebase positions
|
||||||
|
that are drawn by the 50-move rule to count as win or loss. This may be useful
|
||||||
|
for correspondence games (because of tablebase adjudication).
|
||||||
|
|
||||||
|
The "SyzygyProbeLimit" option should normally be left at its default value.
|
||||||
|
|
||||||
|
**What to expect**
|
||||||
|
If the engine is searching a position that is not in the tablebases (e.g.
|
||||||
|
a position with 7 pieces), it will access the tablebases during the search.
|
||||||
|
If the engine reports a very large score (typically 123.xx), this means
|
||||||
|
that it has found a winning line into a tablebase position.
|
||||||
|
|
||||||
|
If the engine is given a position to search that is in the tablebases, it
|
||||||
|
will use the tablebases at the beginning of the search to preselect all
|
||||||
|
good moves, i.e. all moves that preserve the win or preserve the draw while
|
||||||
|
taking into account the 50-move rule.
|
||||||
|
It will then perform a search only on those moves. **The engine will not move
|
||||||
|
immediately**, unless there is only a single good move. **The engine likely
|
||||||
|
will not report a mate score even if the position is known to be won.**
|
||||||
|
|
||||||
|
It is therefore clear that behaviour is not identical to what one might
|
||||||
|
be used to with Nalimov tablebases. There are technical reasons for this
|
||||||
|
difference, the main technical reason being that Nalimov tablebases use the
|
||||||
|
DTM metric (distance-to-mate), while Syzygybases use a variation of the
|
||||||
|
DTZ metric (distance-to-zero, zero meaning any move that resets the 50-move
|
||||||
|
counter). This special metric is one of the reasons that Syzygybases are
|
||||||
|
more compact than Nalimov tablebases, while still storing all information
|
||||||
|
needed for optimal play and in addition being able to take into account
|
||||||
|
the 50-move rule.
|
||||||
|
|
||||||
|
|
||||||
### Compiling it yourself
|
### Compiling it yourself
|
||||||
@@ -67,4 +112,4 @@ to where the source code can be found. If you make any changes to the
|
|||||||
source code, these changes must also be made available under the GPL.
|
source code, these changes must also be made available under the GPL.
|
||||||
|
|
||||||
For full details, read the copy of the GPL found in the file named
|
For full details, read the copy of the GPL found in the file named
|
||||||
*Copying.txt*
|
*Copying.txt*.
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
version: 1.0.{build}
|
||||||
|
clone_depth: 5
|
||||||
|
|
||||||
|
branches:
|
||||||
|
only:
|
||||||
|
- master
|
||||||
|
- appveyor
|
||||||
|
|
||||||
|
# Operating system (build VM template)
|
||||||
|
os: Visual Studio 2015
|
||||||
|
|
||||||
|
# Build platform, i.e. x86, x64, AnyCPU. This setting is optional.
|
||||||
|
platform:
|
||||||
|
- x86
|
||||||
|
- x64
|
||||||
|
- Any CPU
|
||||||
|
|
||||||
|
# build Configuration, i.e. Debug, Release, etc.
|
||||||
|
configuration: Debug
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
# The build fail immediately once one of the job fails
|
||||||
|
fast_finish: true
|
||||||
|
|
||||||
|
# Scripts that are called at very beginning, before repo cloning
|
||||||
|
init:
|
||||||
|
- cmake --version
|
||||||
|
- msbuild /version
|
||||||
|
|
||||||
|
before_build:
|
||||||
|
- cd src
|
||||||
|
- echo project (Stockfish) >> CMakeLists.txt
|
||||||
|
- echo add_executable(stockfish benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp >> CMakeLists.txt
|
||||||
|
- echo main.cpp material.cpp misc.cpp movegen.cpp movepick.cpp pawns.cpp position.cpp psqt.cpp >> CMakeLists.txt
|
||||||
|
- echo search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp syzygy/tbprobe.cpp) >> CMakeLists.txt
|
||||||
|
- echo set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/src) >> CMakeLists.txt
|
||||||
|
# - echo target_compile_options(stockfish PUBLIC "/Ox") >> CMakeLists.txt
|
||||||
|
|
||||||
|
build_script:
|
||||||
|
- cmake -G "Visual Studio 14 2015 Win64" .
|
||||||
|
- cmake --build .
|
||||||
|
|
||||||
|
before_test:
|
||||||
|
- cd Debug
|
||||||
|
- stockfish.exe bench > null
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
[PolyGlot]
|
|
||||||
|
|
||||||
EngineDir = .
|
|
||||||
EngineCommand = ./stockfish
|
|
||||||
|
|
||||||
Book = false
|
|
||||||
BookFile = book.bin
|
|
||||||
|
|
||||||
Log = false
|
|
||||||
LogFile = stockfish.log
|
|
||||||
|
|
||||||
Resign = true
|
|
||||||
ResignScore = 600
|
|
||||||
|
|
||||||
[Engine]
|
|
||||||
|
|
||||||
Write Debug Log = false
|
|
||||||
Write Search Log = false
|
|
||||||
Search Log Filename = SearchLog.txt
|
|
||||||
Book File = book.bin
|
|
||||||
Best Book Move = false
|
|
||||||
Contempt Factor = 0
|
|
||||||
Mobility (Midgame) = 100
|
|
||||||
Mobility (Endgame) = 100
|
|
||||||
Pawn Structure (Midgame) = 100
|
|
||||||
Pawn Structure (Endgame) = 100
|
|
||||||
Passed Pawns (Midgame) = 100
|
|
||||||
Passed Pawns (Endgame) = 100
|
|
||||||
Space = 100
|
|
||||||
King Safety = 100
|
|
||||||
Min Split Depth = 0
|
|
||||||
Threads = 1
|
|
||||||
Hash = 128
|
|
||||||
Ponder = true
|
|
||||||
OwnBook = false
|
|
||||||
MultiPV = 1
|
|
||||||
Skill Level = 20
|
|
||||||
Emergency Move Horizon = 40
|
|
||||||
Emergency Base Time = 200
|
|
||||||
Emergency Move Time = 70
|
|
||||||
Minimum Thinking Time = 20
|
|
||||||
Slow Mover = 100
|
|
||||||
UCI_Chess960 = false
|
|
||||||
+148
-99
@@ -1,6 +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-2014 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
|
||||||
#
|
#
|
||||||
# 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,26 +22,25 @@
|
|||||||
### ==========================================================================
|
### ==========================================================================
|
||||||
|
|
||||||
### Establish the operating system name
|
### Establish the operating system name
|
||||||
UNAME = $(shell uname)
|
KERNEL = $(shell uname -s)
|
||||||
|
ifeq ($(KERNEL),Linux)
|
||||||
|
OS = $(shell uname -o)
|
||||||
|
endif
|
||||||
|
|
||||||
### Executable name
|
### Executable name
|
||||||
EXE = stockfish
|
EXE = stockfish
|
||||||
|
|
||||||
### Installation dir definitions
|
### Installation dir definitions
|
||||||
PREFIX = /usr/local
|
PREFIX = /usr/local
|
||||||
# Haiku has a non-standard filesystem layout
|
|
||||||
ifeq ($(UNAME),Haiku)
|
|
||||||
PREFIX=/boot/common
|
|
||||||
endif
|
|
||||||
BINDIR = $(PREFIX)/bin
|
BINDIR = $(PREFIX)/bin
|
||||||
|
|
||||||
### Built-in benchmark for pgo-builds
|
### Built-in benchmark for pgo-builds
|
||||||
PGOBENCH = ./$(EXE) bench 32 1 1 default time
|
PGOBENCH = ./$(EXE) bench
|
||||||
|
|
||||||
### Object files
|
### Object files
|
||||||
OBJS = benchmark.o bitbase.o bitboard.o book.o endgame.o evaluate.o main.o \
|
OBJS = benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o \
|
||||||
material.o misc.o movegen.o movepick.o notation.o pawns.o position.o \
|
material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o \
|
||||||
search.o thread.o timeman.o tt.o uci.o ucioption.o
|
search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o
|
||||||
|
|
||||||
### ==========================================================================
|
### ==========================================================================
|
||||||
### Section 2. High-level Configuration
|
### Section 2. High-level Configuration
|
||||||
@@ -50,14 +50,12 @@ OBJS = benchmark.o bitbase.o bitboard.o book.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
|
||||||
# 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
|
||||||
# os = (name) --- --- Target operating system
|
|
||||||
# bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
|
# bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
|
||||||
# prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch x86 asm-instruction
|
# prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch asm-instruction
|
||||||
# bsfq = yes/no --- -DUSE_BSFQ --- Use bsfq x86_64 asm-instruction (only
|
# popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt asm-instruction
|
||||||
# with GCC and ICC 64-bit)
|
|
||||||
# popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt x86_64 asm-instruction
|
|
||||||
# sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
|
# sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
|
||||||
# pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
|
# pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
|
||||||
#
|
#
|
||||||
@@ -68,10 +66,9 @@ OBJS = benchmark.o bitbase.o bitboard.o book.o endgame.o evaluate.o main.o \
|
|||||||
### 2.1. General and architecture defaults
|
### 2.1. General and architecture defaults
|
||||||
optimize = yes
|
optimize = yes
|
||||||
debug = no
|
debug = no
|
||||||
os = any
|
sanitize = no
|
||||||
bits = 32
|
bits = 32
|
||||||
prefetch = no
|
prefetch = no
|
||||||
bsfq = no
|
|
||||||
popcnt = no
|
popcnt = no
|
||||||
sse = no
|
sse = no
|
||||||
pext = no
|
pext = no
|
||||||
@@ -101,7 +98,6 @@ ifeq ($(ARCH),x86-64)
|
|||||||
arch = x86_64
|
arch = x86_64
|
||||||
bits = 64
|
bits = 64
|
||||||
prefetch = yes
|
prefetch = yes
|
||||||
bsfq = yes
|
|
||||||
sse = yes
|
sse = yes
|
||||||
endif
|
endif
|
||||||
|
|
||||||
@@ -109,7 +105,6 @@ ifeq ($(ARCH),x86-64-modern)
|
|||||||
arch = x86_64
|
arch = x86_64
|
||||||
bits = 64
|
bits = 64
|
||||||
prefetch = yes
|
prefetch = yes
|
||||||
bsfq = yes
|
|
||||||
popcnt = yes
|
popcnt = yes
|
||||||
sse = yes
|
sse = yes
|
||||||
endif
|
endif
|
||||||
@@ -118,7 +113,6 @@ ifeq ($(ARCH),x86-64-bmi2)
|
|||||||
arch = x86_64
|
arch = x86_64
|
||||||
bits = 64
|
bits = 64
|
||||||
prefetch = yes
|
prefetch = yes
|
||||||
bsfq = yes
|
|
||||||
popcnt = yes
|
popcnt = yes
|
||||||
sse = yes
|
sse = yes
|
||||||
pext = yes
|
pext = yes
|
||||||
@@ -127,7 +121,6 @@ endif
|
|||||||
ifeq ($(ARCH),armv7)
|
ifeq ($(ARCH),armv7)
|
||||||
arch = armv7
|
arch = armv7
|
||||||
prefetch = yes
|
prefetch = yes
|
||||||
bsfq = yes
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(ARCH),ppc-32)
|
ifeq ($(ARCH),ppc-32)
|
||||||
@@ -146,7 +139,8 @@ endif
|
|||||||
|
|
||||||
### 3.1 Selecting compiler (default = gcc)
|
### 3.1 Selecting compiler (default = gcc)
|
||||||
|
|
||||||
CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti $(EXTRACXXFLAGS)
|
CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti -std=c++11 $(EXTRACXXFLAGS)
|
||||||
|
DEPENDFLAGS += -std=c++11
|
||||||
LDFLAGS += $(EXTRALDFLAGS)
|
LDFLAGS += $(EXTRALDFLAGS)
|
||||||
|
|
||||||
ifeq ($(COMP),)
|
ifeq ($(COMP),)
|
||||||
@@ -156,14 +150,44 @@ endif
|
|||||||
ifeq ($(COMP),gcc)
|
ifeq ($(COMP),gcc)
|
||||||
comp=gcc
|
comp=gcc
|
||||||
CXX=g++
|
CXX=g++
|
||||||
CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
|
CXXFLAGS += -pedantic -Wextra -Wshadow
|
||||||
|
|
||||||
|
ifeq ($(ARCH),armv7)
|
||||||
|
ifeq ($(OS),Android)
|
||||||
|
CXXFLAGS += -m$(bits)
|
||||||
|
endif
|
||||||
|
else
|
||||||
|
CXXFLAGS += -m$(bits)
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifneq ($(KERNEL),Darwin)
|
||||||
|
LDFLAGS += -Wl,--no-as-needed
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(COMP),mingw)
|
ifeq ($(COMP),mingw)
|
||||||
comp=mingw
|
comp=mingw
|
||||||
CXX=g++
|
|
||||||
|
ifeq ($(KERNEL),Linux)
|
||||||
|
ifeq ($(bits),64)
|
||||||
|
ifeq ($(shell which x86_64-w64-mingw32-c++-posix),)
|
||||||
|
CXX=x86_64-w64-mingw32-c++
|
||||||
|
else
|
||||||
|
CXX=x86_64-w64-mingw32-c++-posix
|
||||||
|
endif
|
||||||
|
else
|
||||||
|
ifeq ($(shell which i686-w64-mingw32-c++-posix),)
|
||||||
|
CXX=i686-w64-mingw32-c++
|
||||||
|
else
|
||||||
|
CXX=i686-w64-mingw32-c++-posix
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
else
|
||||||
|
CXX=g++
|
||||||
|
endif
|
||||||
|
|
||||||
CXXFLAGS += -Wextra -Wshadow
|
CXXFLAGS += -Wextra -Wshadow
|
||||||
LDFLAGS += -static-libstdc++ -static-libgcc
|
LDFLAGS += -static
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(COMP),icc)
|
ifeq ($(COMP),icc)
|
||||||
@@ -175,7 +199,22 @@ endif
|
|||||||
ifeq ($(COMP),clang)
|
ifeq ($(COMP),clang)
|
||||||
comp=clang
|
comp=clang
|
||||||
CXX=clang++
|
CXX=clang++
|
||||||
CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
|
CXXFLAGS += -pedantic -Wextra -Wshadow
|
||||||
|
|
||||||
|
ifeq ($(ARCH),armv7)
|
||||||
|
ifeq ($(OS),Android)
|
||||||
|
CXXFLAGS += -m$(bits)
|
||||||
|
LDFLAGS += -m$(bits)
|
||||||
|
endif
|
||||||
|
else
|
||||||
|
CXXFLAGS += -m$(bits)
|
||||||
|
LDFLAGS += -m$(bits)
|
||||||
|
endif
|
||||||
|
|
||||||
|
ifeq ($(KERNEL),Darwin)
|
||||||
|
CXXFLAGS += -stdlib=libc++
|
||||||
|
DEPENDFLAGS += -stdlib=libc++
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(comp),icc)
|
ifeq ($(comp),icc)
|
||||||
@@ -190,36 +229,52 @@ else
|
|||||||
profile_clean = gcc-profile-clean
|
profile_clean = gcc-profile-clean
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(UNAME),Darwin)
|
ifeq ($(KERNEL),Darwin)
|
||||||
CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.6
|
CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.9
|
||||||
LDFLAGS += -arch $(arch) -mmacosx-version-min=10.6
|
LDFLAGS += -arch $(arch) -mmacosx-version-min=10.9
|
||||||
|
endif
|
||||||
|
|
||||||
|
### Travis CI script uses COMPILER to overwrite CXX
|
||||||
|
ifdef COMPILER
|
||||||
|
COMPCXX=$(COMPILER)
|
||||||
|
endif
|
||||||
|
|
||||||
|
### Allow overwriting CXX from command line
|
||||||
|
ifdef COMPCXX
|
||||||
|
CXX=$(COMPCXX)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### On mingw use Windows threads, otherwise POSIX
|
### On mingw use Windows threads, otherwise POSIX
|
||||||
ifneq ($(comp),mingw)
|
ifneq ($(comp),mingw)
|
||||||
# On Android Bionic's C library comes with its own pthread implementation bundled in
|
# On Android Bionic's C library comes with its own pthread implementation bundled in
|
||||||
ifneq ($(arch),armv7)
|
ifneq ($(OS),Android)
|
||||||
# Haiku has pthreads in its libroot, so only link it in on other platforms
|
# Haiku has pthreads in its libroot, so only link it in on other platforms
|
||||||
ifneq ($(UNAME),Haiku)
|
ifneq ($(KERNEL),Haiku)
|
||||||
LDFLAGS += -lpthread
|
LDFLAGS += -lpthread
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.4 Debugging
|
### 3.2.1 Debugging
|
||||||
ifeq ($(debug),no)
|
ifeq ($(debug),no)
|
||||||
CXXFLAGS += -DNDEBUG
|
CXXFLAGS += -DNDEBUG
|
||||||
else
|
else
|
||||||
CXXFLAGS += -g
|
CXXFLAGS += -g
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.5 Optimization
|
### 3.2.2 Debugging with undefined behavior sanitizers
|
||||||
|
ifeq ($(sanitize),yes)
|
||||||
|
CXXFLAGS += -g3 -fsanitize=undefined
|
||||||
|
endif
|
||||||
|
|
||||||
|
### 3.3 Optimization
|
||||||
ifeq ($(optimize),yes)
|
ifeq ($(optimize),yes)
|
||||||
|
|
||||||
ifeq ($(comp),gcc)
|
CXXFLAGS += -O3
|
||||||
CXXFLAGS += -O3
|
|
||||||
|
|
||||||
ifeq ($(UNAME),Darwin)
|
ifeq ($(comp),gcc)
|
||||||
|
|
||||||
|
ifeq ($(KERNEL),Darwin)
|
||||||
ifeq ($(arch),i386)
|
ifeq ($(arch),i386)
|
||||||
CXXFLAGS += -mdynamic-no-pic
|
CXXFLAGS += -mdynamic-no-pic
|
||||||
endif
|
endif
|
||||||
@@ -228,27 +283,19 @@ ifeq ($(optimize),yes)
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(arch),armv7)
|
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),mingw)
|
|
||||||
CXXFLAGS += -O3
|
|
||||||
endif
|
|
||||||
|
|
||||||
ifeq ($(comp),icc)
|
ifeq ($(comp),icc)
|
||||||
ifeq ($(UNAME),Darwin)
|
ifeq ($(KERNEL),Darwin)
|
||||||
CXXFLAGS += -fast -mdynamic-no-pic
|
CXXFLAGS += -mdynamic-no-pic
|
||||||
else
|
|
||||||
CXXFLAGS += -fast
|
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(comp),clang)
|
ifeq ($(comp),clang)
|
||||||
CXXFLAGS += -O3
|
ifeq ($(KERNEL),Darwin)
|
||||||
|
|
||||||
ifeq ($(UNAME),Darwin)
|
|
||||||
ifeq ($(pext),no)
|
ifeq ($(pext),no)
|
||||||
CXXFLAGS += -flto
|
CXXFLAGS += -flto
|
||||||
LDFLAGS += $(CXXFLAGS)
|
LDFLAGS += $(CXXFLAGS)
|
||||||
@@ -263,12 +310,12 @@ ifeq ($(optimize),yes)
|
|||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.6. Bits
|
### 3.4 Bits
|
||||||
ifeq ($(bits),64)
|
ifeq ($(bits),64)
|
||||||
CXXFLAGS += -DIS_64BIT
|
CXXFLAGS += -DIS_64BIT
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.7 prefetch
|
### 3.5 prefetch
|
||||||
ifeq ($(prefetch),yes)
|
ifeq ($(prefetch),yes)
|
||||||
ifeq ($(sse),yes)
|
ifeq ($(sse),yes)
|
||||||
CXXFLAGS += -msse
|
CXXFLAGS += -msse
|
||||||
@@ -278,40 +325,54 @@ else
|
|||||||
CXXFLAGS += -DNO_PREFETCH
|
CXXFLAGS += -DNO_PREFETCH
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.8 bsfq
|
### 3.6 popcnt
|
||||||
ifeq ($(bsfq),yes)
|
|
||||||
CXXFLAGS += -DUSE_BSFQ
|
|
||||||
endif
|
|
||||||
|
|
||||||
### 3.9 popcnt
|
|
||||||
ifeq ($(popcnt),yes)
|
ifeq ($(popcnt),yes)
|
||||||
CXXFLAGS += -msse3 -DUSE_POPCNT
|
ifeq ($(comp),icc)
|
||||||
endif
|
CXXFLAGS += -msse3 -DUSE_POPCNT
|
||||||
|
else
|
||||||
### 3.10 pext
|
CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
|
||||||
ifeq ($(pext),yes)
|
|
||||||
CXXFLAGS += -DUSE_PEXT
|
|
||||||
ifeq ($(comp),$(filter $(comp),gcc clang mingw))
|
|
||||||
CXXFLAGS += -mbmi -mbmi2
|
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
### 3.11 Link Time Optimization, it works since gcc 4.5 but not on mingw.
|
### 3.7 pext
|
||||||
|
ifeq ($(pext),yes)
|
||||||
|
CXXFLAGS += -DUSE_PEXT
|
||||||
|
ifeq ($(comp),$(filter $(comp),gcc clang mingw))
|
||||||
|
CXXFLAGS += -mbmi2
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
|
### 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 ($(comp),gcc)
|
||||||
ifeq ($(optimize),yes)
|
ifeq ($(optimize),yes)
|
||||||
ifeq ($(debug),no)
|
ifeq ($(debug),no)
|
||||||
GCC_MAJOR := `$(CXX) -dumpversion | cut -f1 -d.`
|
CXXFLAGS += -flto
|
||||||
GCC_MINOR := `$(CXX) -dumpversion | cut -f2 -d.`
|
LDFLAGS += $(CXXFLAGS)
|
||||||
ifeq (1,$(shell expr \( $(GCC_MAJOR) \> 4 \) \| \( $(GCC_MAJOR) \= 4 \& $(GCC_MINOR) \>= 5 \)))
|
|
||||||
CXXFLAGS += -flto
|
|
||||||
LDFLAGS += $(CXXFLAGS)
|
|
||||||
endif
|
|
||||||
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
|
||||||
|
|
||||||
|
### 3.9 Android 5 can only run position independent executables. Note that this
|
||||||
|
### breaks Android 4.0 and earlier.
|
||||||
|
ifeq ($(OS), Android)
|
||||||
|
CXXFLAGS += -fPIE
|
||||||
|
LDFLAGS += -fPIE -pie
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
||||||
### ==========================================================================
|
### ==========================================================================
|
||||||
### Section 4. Public targets
|
### Section 4. Public targets
|
||||||
### ==========================================================================
|
### ==========================================================================
|
||||||
@@ -320,7 +381,7 @@ help:
|
|||||||
@echo ""
|
@echo ""
|
||||||
@echo "To compile stockfish, type: "
|
@echo "To compile stockfish, type: "
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "make target ARCH=arch [COMP=comp]"
|
@echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Supported targets:"
|
@echo "Supported targets:"
|
||||||
@echo ""
|
@echo ""
|
||||||
@@ -350,15 +411,17 @@ help:
|
|||||||
@echo "clang > LLVM Clang compiler"
|
@echo "clang > LLVM Clang compiler"
|
||||||
@echo "icc > Intel compiler"
|
@echo "icc > Intel compiler"
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Non-standard targets:"
|
@echo "Simple examples. If you don't know what to do, you likely want to run: "
|
||||||
@echo ""
|
|
||||||
@echo "make hpux > Compile for HP-UX. Compiler = aCC"
|
|
||||||
@echo ""
|
|
||||||
@echo "Examples. If you don't know what to do, you likely want to run: "
|
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "make build ARCH=x86-64 (This is for 64-bit systems)"
|
@echo "make build ARCH=x86-64 (This is for 64-bit systems)"
|
||||||
@echo "make build ARCH=x86-32 (This is for 32-bit systems)"
|
@echo "make build ARCH=x86-32 (This is for 32-bit systems)"
|
||||||
@echo ""
|
@echo ""
|
||||||
|
@echo "Advanced examples, for experienced users: "
|
||||||
|
@echo ""
|
||||||
|
@echo "make build ARCH=x86-64 COMP=clang"
|
||||||
|
@echo "make profile-build ARCH=x86-64-modern COMP=gcc COMPCXX=g++-4.8"
|
||||||
|
@echo ""
|
||||||
|
|
||||||
|
|
||||||
.PHONY: build profile-build
|
.PHONY: build profile-build
|
||||||
build:
|
build:
|
||||||
@@ -372,14 +435,14 @@ profile-build:
|
|||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
|
||||||
@echo ""
|
@echo ""
|
||||||
@echo "Step 1/4. Building executable for benchmark ..."
|
@echo "Step 1/4. Building executable for benchmark ..."
|
||||||
@touch *.cpp *.h
|
@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 final executable ..."
|
||||||
@touch *.cpp
|
@touch *.cpp *.h syzygy/*.cpp syzygy/*.h
|
||||||
$(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 ..."
|
||||||
@@ -394,7 +457,7 @@ install:
|
|||||||
-strip $(BINDIR)/$(EXE)
|
-strip $(BINDIR)/$(EXE)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda
|
$(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda ./syzygy/*.o ./syzygy/*.gcda
|
||||||
|
|
||||||
default:
|
default:
|
||||||
help
|
help
|
||||||
@@ -411,10 +474,10 @@ config-sanity:
|
|||||||
@echo "debug: '$(debug)'"
|
@echo "debug: '$(debug)'"
|
||||||
@echo "optimize: '$(optimize)'"
|
@echo "optimize: '$(optimize)'"
|
||||||
@echo "arch: '$(arch)'"
|
@echo "arch: '$(arch)'"
|
||||||
@echo "os: '$(os)'"
|
|
||||||
@echo "bits: '$(bits)'"
|
@echo "bits: '$(bits)'"
|
||||||
|
@echo "kernel: '$(KERNEL)'"
|
||||||
|
@echo "os: '$(OS)'"
|
||||||
@echo "prefetch: '$(prefetch)'"
|
@echo "prefetch: '$(prefetch)'"
|
||||||
@echo "bsfq: '$(bsfq)'"
|
|
||||||
@echo "popcnt: '$(popcnt)'"
|
@echo "popcnt: '$(popcnt)'"
|
||||||
@echo "sse: '$(sse)'"
|
@echo "sse: '$(sse)'"
|
||||||
@echo "pext: '$(pext)'"
|
@echo "pext: '$(pext)'"
|
||||||
@@ -430,10 +493,8 @@ config-sanity:
|
|||||||
@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"
|
||||||
@test "$(os)" = "any"
|
|
||||||
@test "$(bits)" = "32" || test "$(bits)" = "64"
|
@test "$(bits)" = "32" || test "$(bits)" = "64"
|
||||||
@test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
|
@test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
|
||||||
@test "$(bsfq)" = "yes" || test "$(bsfq)" = "no"
|
|
||||||
@test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
|
@test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
|
||||||
@test "$(sse)" = "yes" || test "$(sse)" = "no"
|
@test "$(sse)" = "yes" || test "$(sse)" = "no"
|
||||||
@test "$(pext)" = "yes" || test "$(pext)" = "no"
|
@test "$(pext)" = "yes" || test "$(pext)" = "no"
|
||||||
@@ -453,12 +514,12 @@ gcc-profile-make:
|
|||||||
|
|
||||||
gcc-profile-use:
|
gcc-profile-use:
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||||
EXTRACXXFLAGS='-fprofile-use' \
|
EXTRACXXFLAGS='-fprofile-use -fno-peel-loops -fno-tracer' \
|
||||||
EXTRALDFLAGS='-lgcov' \
|
EXTRALDFLAGS='-lgcov' \
|
||||||
all
|
all
|
||||||
|
|
||||||
gcc-profile-clean:
|
gcc-profile-clean:
|
||||||
@rm -rf *.gcda *.gcno bench.txt
|
@rm -rf *.gcda *.gcno syzygy/*.gcda syzygy/*.gcno bench.txt
|
||||||
|
|
||||||
icc-profile-prepare:
|
icc-profile-prepare:
|
||||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
|
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) icc-profile-clean
|
||||||
@@ -482,15 +543,3 @@ icc-profile-clean:
|
|||||||
|
|
||||||
-include .depend
|
-include .depend
|
||||||
|
|
||||||
|
|
||||||
### ==========================================================================
|
|
||||||
### Section 6. Non-standard targets
|
|
||||||
### ==========================================================================
|
|
||||||
|
|
||||||
hpux:
|
|
||||||
$(MAKE) \
|
|
||||||
CXX='/opt/aCC/bin/aCC -AA +hpxstd98 -mt +O3 -DNDEBUG -DNO_PREFETCH' \
|
|
||||||
CXXFLAGS="" \
|
|
||||||
LDFLAGS="" \
|
|
||||||
all
|
|
||||||
|
|
||||||
|
|||||||
+51
-40
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,16 +24,16 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "notation.h"
|
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "tt.h"
|
#include "uci.h"
|
||||||
#include "ucioption.h"
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
static const char* Defaults[] = {
|
namespace {
|
||||||
|
|
||||||
|
const vector<string> Defaults = {
|
||||||
"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",
|
||||||
@@ -62,9 +63,30 @@ static const char* Defaults[] = {
|
|||||||
"6k1/6p1/P6p/r1N5/5p2/7P/1b3PP1/4R1K1 w - - 0 1",
|
"6k1/6p1/P6p/r1N5/5p2/7P/1b3PP1/4R1K1 w - - 0 1",
|
||||||
"1r3k2/4q3/2Pp3b/3Bp3/2Q2p2/1p1P2P1/1P2KP2/3N4 w - - 0 1",
|
"1r3k2/4q3/2Pp3b/3Bp3/2Q2p2/1p1P2P1/1P2KP2/3N4 w - - 0 1",
|
||||||
"6k1/4pp1p/3p2p1/P1pPb3/R7/1r2P1PP/3B1P2/6K1 w - - 0 1",
|
"6k1/4pp1p/3p2p1/P1pPb3/R7/1r2P1PP/3B1P2/6K1 w - - 0 1",
|
||||||
"8/3p3B/5p2/5P2/p7/PP5b/k7/6K1 w - - 0 1"
|
"8/3p3B/5p2/5P2/p7/PP5b/k7/6K1 w - - 0 1",
|
||||||
|
|
||||||
|
// 5-man positions
|
||||||
|
"8/8/8/8/5kp1/P7/8/1K1N4 w - - 0 1", // Kc2 - mate
|
||||||
|
"8/8/8/5N2/8/p7/8/2NK3k w - - 0 1", // Na2 - mate
|
||||||
|
"8/3k4/8/8/8/4B3/4KB2/2B5 w - - 0 1", // draw
|
||||||
|
|
||||||
|
// 6-man positions
|
||||||
|
"8/8/1P6/5pr1/8/4R3/7k/2K5 w - - 0 1", // Re5 - mate
|
||||||
|
"8/2p4P/8/kr6/6R1/8/8/1K6 w - - 0 1", // Ka2 - mate
|
||||||
|
"8/8/3P3k/8/1p6/8/1P6/1K3n2 b - - 0 1", // Nd2 - draw
|
||||||
|
|
||||||
|
// 7-man positions
|
||||||
|
"8/R7/2q5/8/6k1/8/1P5p/K6R w - - 0 124", // Draw
|
||||||
|
|
||||||
|
// Mate and stalemate positions
|
||||||
|
"8/8/8/8/8/6k1/6p1/6K1 w - -",
|
||||||
|
"5k2/5P2/5K2/8/8/8/8/8 b - -",
|
||||||
|
"8/8/8/8/8/4k3/4p3/4K3 w - -",
|
||||||
|
"8/8/8/8/8/5K2/8/3Q1k2 b - -",
|
||||||
|
"7k/7P/6K1/8/3B4/8/8/8 b - -"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
/// benchmark() runs a simple benchmark by letting Stockfish analyze a set
|
/// benchmark() runs a simple benchmark by letting Stockfish analyze a set
|
||||||
/// of positions for a given limit each. There are five parameters: the
|
/// of positions for a given limit each. There are five parameters: the
|
||||||
@@ -72,16 +94,16 @@ static const char* Defaults[] = {
|
|||||||
/// be used, the limit value spent for each position (optional, default is
|
/// be used, the limit value spent for each position (optional, default is
|
||||||
/// depth 13), an optional file name where to look for positions in FEN
|
/// depth 13), an optional file name where to look for positions in FEN
|
||||||
/// format (defaults are the positions defined above) and the type of the
|
/// format (defaults are the positions defined above) and the type of the
|
||||||
/// limit value: depth (default), time in secs or number of nodes.
|
/// limit value: depth (default), time in millisecs or number of nodes.
|
||||||
|
|
||||||
void benchmark(const Position& current, istream& is) {
|
void benchmark(const Position& current, istream& is) {
|
||||||
|
|
||||||
string token;
|
string token;
|
||||||
Search::LimitsType limits;
|
|
||||||
vector<string> fens;
|
vector<string> fens;
|
||||||
|
Search::LimitsType limits;
|
||||||
|
|
||||||
// Assign default values to missing arguments
|
// Assign default values to missing arguments
|
||||||
string ttSize = (is >> token) ? token : "32";
|
string ttSize = (is >> token) ? token : "16";
|
||||||
string threads = (is >> token) ? token : "1";
|
string threads = (is >> token) ? token : "1";
|
||||||
string limit = (is >> token) ? token : "13";
|
string limit = (is >> token) ? token : "13";
|
||||||
string fenFile = (is >> token) ? token : "default";
|
string fenFile = (is >> token) ? token : "default";
|
||||||
@@ -89,22 +111,22 @@ void benchmark(const Position& current, istream& is) {
|
|||||||
|
|
||||||
Options["Hash"] = ttSize;
|
Options["Hash"] = ttSize;
|
||||||
Options["Threads"] = threads;
|
Options["Threads"] = threads;
|
||||||
TT.clear();
|
Search::clear();
|
||||||
|
|
||||||
if (limitType == "time")
|
if (limitType == "time")
|
||||||
limits.movetime = 1000 * atoi(limit.c_str()); // movetime is in ms
|
limits.movetime = stoi(limit); // movetime is in millisecs
|
||||||
|
|
||||||
else if (limitType == "nodes")
|
else if (limitType == "nodes")
|
||||||
limits.nodes = atoi(limit.c_str());
|
limits.nodes = stoi(limit);
|
||||||
|
|
||||||
else if (limitType == "mate")
|
else if (limitType == "mate")
|
||||||
limits.mate = atoi(limit.c_str());
|
limits.mate = stoi(limit);
|
||||||
|
|
||||||
else
|
else
|
||||||
limits.depth = atoi(limit.c_str());
|
limits.depth = stoi(limit);
|
||||||
|
|
||||||
if (fenFile == "default")
|
if (fenFile == "default")
|
||||||
fens.assign(Defaults, Defaults + 30);
|
fens = Defaults;
|
||||||
|
|
||||||
else if (fenFile == "current")
|
else if (fenFile == "current")
|
||||||
fens.push_back(current.fen());
|
fens.push_back(current.fen());
|
||||||
@@ -112,7 +134,7 @@ void benchmark(const Position& current, istream& is) {
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
string fen;
|
string fen;
|
||||||
ifstream file(fenFile.c_str());
|
ifstream file(fenFile);
|
||||||
|
|
||||||
if (!file.is_open())
|
if (!file.is_open())
|
||||||
{
|
{
|
||||||
@@ -128,42 +150,31 @@ void benchmark(const Position& current, istream& is) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint64_t nodes = 0;
|
uint64_t nodes = 0;
|
||||||
Search::StateStackPtr st;
|
TimePoint elapsed = now();
|
||||||
Time::point elapsed = Time::now();
|
Position pos;
|
||||||
|
|
||||||
for (size_t i = 0; i < fens.size(); ++i)
|
for (size_t i = 0; i < fens.size(); ++i)
|
||||||
{
|
{
|
||||||
Position pos(fens[i], Options["UCI_Chess960"], Threads.main());
|
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;
|
cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
|
||||||
|
|
||||||
if (limitType == "divide")
|
if (limitType == "perft")
|
||||||
for (MoveList<LEGAL> it(pos); *it; ++it)
|
nodes += Search::perft(pos, limits.depth * ONE_PLY);
|
||||||
{
|
|
||||||
StateInfo si;
|
|
||||||
pos.do_move(*it, si);
|
|
||||||
uint64_t cnt = limits.depth > 1 ? Search::perft(pos, (limits.depth - 1) * ONE_PLY) : 1;
|
|
||||||
pos.undo_move(*it);
|
|
||||||
cerr << move_to_uci(*it, pos.is_chess960()) << ": " << cnt << endl;
|
|
||||||
nodes += cnt;
|
|
||||||
}
|
|
||||||
else if (limitType == "perft")
|
|
||||||
{
|
|
||||||
uint64_t cnt = Search::perft(pos, limits.depth * ONE_PLY);
|
|
||||||
cerr << "\nPerft " << limits.depth << " leaf nodes: " << cnt << endl;
|
|
||||||
nodes += cnt;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Threads.start_thinking(pos, limits, st);
|
limits.startTime = now();
|
||||||
Threads.wait_for_think_finished();
|
Threads.start_thinking(pos, states, limits);
|
||||||
nodes += Search::RootPos.nodes_searched();
|
Threads.main()->wait_for_search_finished();
|
||||||
|
nodes += Threads.nodes_searched();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
elapsed = Time::now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero'
|
elapsed = now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero'
|
||||||
|
|
||||||
dbg_print(); // Just before to exit
|
dbg_print(); // Just before exiting
|
||||||
|
|
||||||
cerr << "\n==========================="
|
cerr << "\n==========================="
|
||||||
<< "\nTotal time (ms) : " << elapsed
|
<< "\nTotal time (ms) : " << elapsed
|
||||||
|
|||||||
+55
-50
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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
|
||||||
@@ -17,7 +18,9 @@
|
|||||||
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 <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <numeric>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
@@ -41,7 +44,7 @@ namespace {
|
|||||||
// bit 13-14: white pawn file (from FILE_A to FILE_D)
|
// bit 13-14: white pawn file (from FILE_A to FILE_D)
|
||||||
// bit 15-17: white pawn RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2)
|
// bit 15-17: white pawn RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2)
|
||||||
unsigned index(Color us, Square bksq, Square wksq, Square psq) {
|
unsigned index(Color us, Square bksq, Square wksq, Square psq) {
|
||||||
return wksq + (bksq << 6) + (us << 12) + (file_of(psq) << 13) + ((RANK_7 - rank_of(psq)) << 15);
|
return wksq | (bksq << 6) | (us << 12) | (file_of(psq) << 13) | ((RANK_7 - rank_of(psq)) << 15);
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Result {
|
enum Result {
|
||||||
@@ -51,27 +54,26 @@ namespace {
|
|||||||
WIN = 4
|
WIN = 4
|
||||||
};
|
};
|
||||||
|
|
||||||
inline Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
|
Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
|
||||||
|
|
||||||
struct KPKPosition {
|
struct KPKPosition {
|
||||||
|
KPKPosition() = default;
|
||||||
KPKPosition(unsigned idx);
|
explicit KPKPosition(unsigned idx);
|
||||||
operator Result() const { return result; }
|
operator Result() const { return result; }
|
||||||
Result classify(const std::vector<KPKPosition>& db)
|
Result classify(const std::vector<KPKPosition>& db)
|
||||||
{ return us == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
|
{ return us == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
|
||||||
|
|
||||||
private:
|
|
||||||
template<Color Us> Result classify(const std::vector<KPKPosition>& db);
|
template<Color Us> Result classify(const std::vector<KPKPosition>& db);
|
||||||
|
|
||||||
Color us;
|
Color us;
|
||||||
Square bksq, wksq, psq;
|
Square ksq[COLOR_NB], psq;
|
||||||
Result result;
|
Result result;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
bool Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color us) {
|
bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color us) {
|
||||||
|
|
||||||
assert(file_of(wpsq) <= FILE_D);
|
assert(file_of(wpsq) <= FILE_D);
|
||||||
|
|
||||||
@@ -80,15 +82,14 @@ bool Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color us) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Bitbases::init_kpk() {
|
void Bitbases::init() {
|
||||||
|
|
||||||
|
std::vector<KPKPosition> db(MAX_INDEX);
|
||||||
unsigned idx, repeat = 1;
|
unsigned idx, repeat = 1;
|
||||||
std::vector<KPKPosition> db;
|
|
||||||
db.reserve(MAX_INDEX);
|
|
||||||
|
|
||||||
// Initialize db with known win / draw positions
|
// Initialize db with known win / draw positions
|
||||||
for (idx = 0; idx < MAX_INDEX; ++idx)
|
for (idx = 0; idx < MAX_INDEX; ++idx)
|
||||||
db.push_back(KPKPosition(idx));
|
db[idx] = KPKPosition(idx);
|
||||||
|
|
||||||
// Iterate through the positions until none of the unknown positions can be
|
// Iterate through the positions until none of the unknown positions can be
|
||||||
// changed to either wins or draws (15 cycles needed).
|
// changed to either wins or draws (15 cycles needed).
|
||||||
@@ -107,69 +108,73 @@ namespace {
|
|||||||
|
|
||||||
KPKPosition::KPKPosition(unsigned idx) {
|
KPKPosition::KPKPosition(unsigned idx) {
|
||||||
|
|
||||||
wksq = Square((idx >> 0) & 0x3F);
|
ksq[WHITE] = Square((idx >> 0) & 0x3F);
|
||||||
bksq = 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) & 0x03), Rank(RANK_7 - (idx >> 15)));
|
psq = make_square(File((idx >> 13) & 0x3), RANK_7 - Rank((idx >> 15) & 0x7));
|
||||||
result = UNKNOWN;
|
|
||||||
|
|
||||||
// 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 ( square_distance(wksq, bksq) <= 1
|
if ( distance(ksq[WHITE], ksq[BLACK]) <= 1
|
||||||
|| wksq == psq
|
|| ksq[WHITE] == psq
|
||||||
|| bksq == psq
|
|| ksq[BLACK] == psq
|
||||||
|| (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
|
|| (us == WHITE && (StepAttacksBB[PAWN][psq] & ksq[BLACK])))
|
||||||
result = INVALID;
|
result = INVALID;
|
||||||
|
|
||||||
else if (us == WHITE)
|
// Immediate win if a pawn can be promoted without getting captured
|
||||||
{
|
else if ( us == WHITE
|
||||||
// Immediate win if a pawn can be promoted without getting captured
|
&& rank_of(psq) == RANK_7
|
||||||
if ( rank_of(psq) == RANK_7
|
&& ksq[us] != psq + NORTH
|
||||||
&& wksq != psq + DELTA_N
|
&& ( distance(ksq[~us], psq + NORTH) > 1
|
||||||
&& ( square_distance(bksq, psq + DELTA_N) > 1
|
|| (StepAttacksBB[KING][ksq[us]] & (psq + NORTH))))
|
||||||
||(StepAttacksBB[KING][wksq] & (psq + DELTA_N))))
|
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 ( !(StepAttacksBB[KING][bksq] & ~(StepAttacksBB[KING][wksq] | StepAttacksBB[PAWN][psq]))
|
else if ( us == BLACK
|
||||||
|| (StepAttacksBB[KING][bksq] & psq & ~StepAttacksBB[KING][wksq]))
|
&& ( !(StepAttacksBB[KING][ksq[us]] & ~(StepAttacksBB[KING][ksq[~us]] | StepAttacksBB[PAWN][psq]))
|
||||||
|
|| (StepAttacksBB[KING][ksq[us]] & psq & ~StepAttacksBB[KING][ksq[~us]])))
|
||||||
result = DRAW;
|
result = DRAW;
|
||||||
|
|
||||||
|
// Position will be classified later
|
||||||
|
else
|
||||||
|
result = UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<Color Us>
|
template<Color Us>
|
||||||
Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
|
Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
|
||||||
|
|
||||||
// White to Move: If one move leads to a position classified as WIN, the result
|
// White to move: If one move leads to a position classified as WIN, the result
|
||||||
// of the current position is WIN. If all moves lead to positions classified
|
// of the current position is WIN. If all moves lead to positions classified
|
||||||
// as DRAW, the current position is classified as DRAW, otherwise the current
|
// as DRAW, the current position is classified as DRAW, otherwise the current
|
||||||
// position is classified as UNKNOWN.
|
// position is classified as UNKNOWN.
|
||||||
//
|
//
|
||||||
// Black to Move: If one move leads to a position classified as DRAW, the result
|
// Black to move: If one move leads to a position classified as DRAW, the result
|
||||||
// of the current position is DRAW. If all moves lead to positions classified
|
// of the current position is DRAW. If all moves lead to positions classified
|
||||||
// as WIN, the position is classified as WIN, otherwise the current position is
|
// 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);
|
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||||
|
const Result Good = (Us == WHITE ? WIN : DRAW);
|
||||||
|
const Result Bad = (Us == WHITE ? DRAW : WIN);
|
||||||
|
|
||||||
Result r = INVALID;
|
Result r = INVALID;
|
||||||
Bitboard b = StepAttacksBB[KING][Us == WHITE ? wksq : bksq];
|
Bitboard b = StepAttacksBB[KING][ksq[Us]];
|
||||||
|
|
||||||
while (b)
|
while (b)
|
||||||
r |= Us == WHITE ? db[index(Them, bksq, pop_lsb(&b), psq)]
|
r |= Us == WHITE ? db[index(Them, ksq[Them] , pop_lsb(&b), psq)]
|
||||||
: db[index(Them, pop_lsb(&b), wksq, psq)];
|
: db[index(Them, pop_lsb(&b), ksq[Them] , psq)];
|
||||||
|
|
||||||
if (Us == WHITE && rank_of(psq) < RANK_7)
|
|
||||||
{
|
|
||||||
Square s = psq + DELTA_N;
|
|
||||||
r |= db[index(BLACK, bksq, wksq, s)]; // Single push
|
|
||||||
|
|
||||||
if (rank_of(psq) == RANK_2 && s != wksq && s != bksq)
|
|
||||||
r |= db[index(BLACK, bksq, wksq, s + DELTA_N)]; // Double push
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Us == WHITE)
|
if (Us == WHITE)
|
||||||
return result = r & WIN ? WIN : r & UNKNOWN ? UNKNOWN : DRAW;
|
{
|
||||||
else
|
if (rank_of(psq) < RANK_7) // Single push
|
||||||
return result = r & DRAW ? DRAW : r & UNKNOWN ? UNKNOWN : WIN;
|
r |= db[index(Them, ksq[Them], ksq[Us], psq + NORTH)];
|
||||||
|
|
||||||
|
if ( rank_of(psq) == RANK_2 // Double push
|
||||||
|
&& psq + NORTH != ksq[Us]
|
||||||
|
&& psq + NORTH != ksq[Them])
|
||||||
|
r |= db[index(Them, ksq[Them], ksq[Us], psq + NORTH + NORTH)];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result = r & Good ? Good : r & UNKNOWN ? UNKNOWN : Bad;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
+91
-85
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,23 +19,22 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstring> // For memset
|
|
||||||
|
|
||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
#include "bitcount.h"
|
#include "misc.h"
|
||||||
#include "rkiss.h"
|
|
||||||
|
|
||||||
CACHE_LINE_ALIGNMENT
|
uint8_t PopCnt16[1 << 16];
|
||||||
|
int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||||
|
|
||||||
Bitboard RMasks[SQUARE_NB];
|
Bitboard RookMasks [SQUARE_NB];
|
||||||
Bitboard RMagics[SQUARE_NB];
|
Bitboard RookMagics [SQUARE_NB];
|
||||||
Bitboard* RAttacks[SQUARE_NB];
|
Bitboard* RookAttacks[SQUARE_NB];
|
||||||
unsigned RShifts[SQUARE_NB];
|
unsigned RookShifts [SQUARE_NB];
|
||||||
|
|
||||||
Bitboard BMasks[SQUARE_NB];
|
Bitboard BishopMasks [SQUARE_NB];
|
||||||
Bitboard BMagics[SQUARE_NB];
|
Bitboard BishopMagics [SQUARE_NB];
|
||||||
Bitboard* BAttacks[SQUARE_NB];
|
Bitboard* BishopAttacks[SQUARE_NB];
|
||||||
unsigned BShifts[SQUARE_NB];
|
unsigned BishopShifts [SQUARE_NB];
|
||||||
|
|
||||||
Bitboard SquareBB[SQUARE_NB];
|
Bitboard SquareBB[SQUARE_NB];
|
||||||
Bitboard FileBB[FILE_NB];
|
Bitboard FileBB[FILE_NB];
|
||||||
@@ -44,57 +44,60 @@ Bitboard InFrontBB[COLOR_NB][RANK_NB];
|
|||||||
Bitboard StepAttacksBB[PIECE_NB][SQUARE_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 DistanceRingsBB[SQUARE_NB][8];
|
Bitboard DistanceRingBB[SQUARE_NB][8];
|
||||||
Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
|
Bitboard ForwardBB[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];
|
||||||
|
|
||||||
int SquareDistance[SQUARE_NB][SQUARE_NB];
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// De Bruijn sequences. See chessprogramming.wikispaces.com/BitScan
|
// De Bruijn sequences. See chessprogramming.wikispaces.com/BitScan
|
||||||
const uint64_t DeBruijn_64 = 0x3F79D71B4CB0A89ULL;
|
const uint64_t DeBruijn64 = 0x3F79D71B4CB0A89ULL;
|
||||||
const uint32_t DeBruijn_32 = 0x783A9B23;
|
const uint32_t DeBruijn32 = 0x783A9B23;
|
||||||
|
|
||||||
CACHE_LINE_ALIGNMENT
|
int MSBTable[256]; // To implement software msb()
|
||||||
|
Square BSFTable[SQUARE_NB]; // To implement software bitscan
|
||||||
int MS1BTable[256];
|
Bitboard RookTable[0x19000]; // To store rook attacks
|
||||||
Square BSFTable[SQUARE_NB];
|
Bitboard BishopTable[0x1480]; // To store bishop attacks
|
||||||
Bitboard RTable[0x19000]; // Storage space for rook attacks
|
|
||||||
Bitboard BTable[0x1480]; // Storage space for bishop attacks
|
|
||||||
|
|
||||||
typedef unsigned (Fn)(Square, Bitboard);
|
typedef unsigned (Fn)(Square, Bitboard);
|
||||||
|
|
||||||
void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
|
void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
|
||||||
Bitboard masks[], unsigned shifts[], Square deltas[], Fn index);
|
Bitboard masks[], unsigned shifts[], Square deltas[], Fn index);
|
||||||
|
|
||||||
FORCE_INLINE unsigned bsf_index(Bitboard b) {
|
// 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.
|
||||||
|
|
||||||
// Matt Taylor's folding for 32 bit systems, extended to 64 bits by Kim Walisch
|
unsigned bsf_index(Bitboard b) {
|
||||||
b ^= (b - 1);
|
b ^= b - 1;
|
||||||
return Is64Bit ? (b * DeBruijn_64) >> 58
|
return Is64Bit ? (b * DeBruijn64) >> 58
|
||||||
: ((unsigned(b) ^ unsigned(b >> 32)) * DeBruijn_32) >> 26;
|
: ((unsigned(b) ^ unsigned(b >> 32)) * DeBruijn32) >> 26;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// popcount16() counts the non-zero bits using SWAR-Popcount algorithm
|
||||||
|
|
||||||
|
unsigned popcount16(unsigned u) {
|
||||||
|
u -= (u >> 1) & 0x5555U;
|
||||||
|
u = ((u >> 2) & 0x3333U) + (u & 0x3333U);
|
||||||
|
u = ((u >> 4) + u) & 0x0F0FU;
|
||||||
|
return (u * 0x0101U) >> 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// lsb()/msb() finds the least/most significant bit in a non-zero bitboard.
|
#ifdef NO_BSF
|
||||||
/// pop_lsb() finds and clears the least significant bit in a non-zero bitboard.
|
|
||||||
|
|
||||||
#ifndef USE_BSFQ
|
/// Software fall-back of lsb() and msb() for CPU lacking hardware support
|
||||||
|
|
||||||
Square lsb(Bitboard b) { return BSFTable[bsf_index(b)]; }
|
Square lsb(Bitboard b) {
|
||||||
|
assert(b);
|
||||||
Square pop_lsb(Bitboard* b) {
|
return BSFTable[bsf_index(b)];
|
||||||
|
|
||||||
Bitboard bb = *b;
|
|
||||||
*b = bb & (bb - 1);
|
|
||||||
return BSFTable[bsf_index(bb)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Square msb(Bitboard b) {
|
Square msb(Bitboard b) {
|
||||||
|
|
||||||
|
assert(b);
|
||||||
unsigned b32;
|
unsigned b32;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
|
|
||||||
@@ -118,14 +121,14 @@ Square msb(Bitboard b) {
|
|||||||
result += 8;
|
result += 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Square(result + MS1BTable[b32]);
|
return Square(result + MSBTable[b32]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // ifndef USE_BSFQ
|
#endif // ifdef NO_BSF
|
||||||
|
|
||||||
|
|
||||||
/// Bitboards::pretty() returns an ASCII representation of a bitboard to be
|
/// Bitboards::pretty() returns an ASCII representation of a bitboard suitable
|
||||||
/// printed to standard output. This is sometimes useful for debugging.
|
/// to be printed to standard output. Useful for debugging.
|
||||||
|
|
||||||
const std::string Bitboards::pretty(Bitboard b) {
|
const std::string Bitboards::pretty(Bitboard b) {
|
||||||
|
|
||||||
@@ -134,9 +137,9 @@ const std::string Bitboards::pretty(Bitboard b) {
|
|||||||
for (Rank r = RANK_8; r >= RANK_1; --r)
|
for (Rank r = RANK_8; r >= RANK_1; --r)
|
||||||
{
|
{
|
||||||
for (File f = FILE_A; f <= FILE_H; ++f)
|
for (File f = FILE_A; f <= FILE_H; ++f)
|
||||||
s.append(b & make_square(f, r) ? "| X " : "| ");
|
s += b & make_square(f, r) ? "| X " : "| ";
|
||||||
|
|
||||||
s.append("|\n+---+---+---+---+---+---+---+---+\n");
|
s += "|\n+---+---+---+---+---+---+---+---+\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
@@ -148,11 +151,17 @@ const std::string Bitboards::pretty(Bitboard b) {
|
|||||||
|
|
||||||
void Bitboards::init() {
|
void Bitboards::init() {
|
||||||
|
|
||||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
for (unsigned i = 0; i < (1 << 16); ++i)
|
||||||
BSFTable[bsf_index(SquareBB[s] = 1ULL << s)] = s;
|
PopCnt16[i] = (uint8_t) popcount16(i);
|
||||||
|
|
||||||
for (Bitboard b = 1; b < 256; ++b)
|
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||||
MS1BTable[b] = more_than_one(b) ? MS1BTable[b - 1] : lsb(b);
|
{
|
||||||
|
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;
|
||||||
@@ -178,8 +187,8 @@ void Bitboards::init() {
|
|||||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
|
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
|
||||||
if (s1 != s2)
|
if (s1 != s2)
|
||||||
{
|
{
|
||||||
SquareDistance[s1][s2] = std::max(file_distance(s1, s2), rank_distance(s1, s2));
|
SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));
|
||||||
DistanceRingsBB[s1][SquareDistance[s1][s2] - 1] |= s2;
|
DistanceRingBB[s1][SquareDistance[s1][s2] - 1] |= s2;
|
||||||
}
|
}
|
||||||
|
|
||||||
int steps[][9] = { {}, { 7, 9 }, { 17, 15, 10, 6, -6, -10, -15, -17 },
|
int steps[][9] = { {}, { 7, 9 }, { 17, 15, 10, 6, -6, -10, -15, -17 },
|
||||||
@@ -192,32 +201,30 @@ void Bitboards::init() {
|
|||||||
{
|
{
|
||||||
Square to = s + Square(c == WHITE ? steps[pt][i] : -steps[pt][i]);
|
Square to = s + Square(c == WHITE ? steps[pt][i] : -steps[pt][i]);
|
||||||
|
|
||||||
if (is_ok(to) && square_distance(s, to) < 3)
|
if (is_ok(to) && distance(s, to) < 3)
|
||||||
StepAttacksBB[make_piece(c, pt)][s] |= to;
|
StepAttacksBB[make_piece(c, pt)][s] |= to;
|
||||||
}
|
}
|
||||||
|
|
||||||
Square RDeltas[] = { DELTA_N, DELTA_E, DELTA_S, DELTA_W };
|
Square RookDeltas[] = { NORTH, EAST, SOUTH, WEST };
|
||||||
Square BDeltas[] = { DELTA_NE, DELTA_SE, DELTA_SW, DELTA_NW };
|
Square BishopDeltas[] = { NORTH_EAST, SOUTH_EAST, SOUTH_WEST, NORTH_WEST };
|
||||||
|
|
||||||
init_magics(RTable, RAttacks, RMagics, RMasks, RShifts, RDeltas, magic_index<ROOK>);
|
init_magics(RookTable, RookAttacks, RookMagics, RookMasks, RookShifts, RookDeltas, magic_index<ROOK>);
|
||||||
init_magics(BTable, BAttacks, BMagics, BMasks, BShifts, BDeltas, magic_index<BISHOP>);
|
init_magics(BishopTable, BishopAttacks, BishopMagics, BishopMasks, BishopShifts, BishopDeltas, magic_index<BISHOP>);
|
||||||
|
|
||||||
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 (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
|
for (Piece pc = W_BISHOP; pc <= W_ROOK; ++pc)
|
||||||
{
|
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
|
||||||
Piece pc = (PseudoAttacks[BISHOP][s1] & s2) ? W_BISHOP :
|
{
|
||||||
(PseudoAttacks[ROOK][s1] & s2) ? W_ROOK : NO_PIECE;
|
if (!(PseudoAttacks[pc][s1] & s2))
|
||||||
|
continue;
|
||||||
|
|
||||||
if (pc == NO_PIECE)
|
LineBB[s1][s2] = (attacks_bb(pc, s1, 0) & attacks_bb(pc, s2, 0)) | s1 | s2;
|
||||||
continue;
|
BetweenBB[s1][s2] = attacks_bb(pc, s1, SquareBB[s2]) & attacks_bb(pc, s2, SquareBB[s1]);
|
||||||
|
}
|
||||||
LineBB[s1][s2] = (attacks_bb(pc, s1, 0) & attacks_bb(pc, s2, 0)) | s1 | s2;
|
|
||||||
BetweenBB[s1][s2] = attacks_bb(pc, s1, SquareBB[s2]) & attacks_bb(pc, s2, SquareBB[s1]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,7 +237,7 @@ namespace {
|
|||||||
|
|
||||||
for (int i = 0; i < 4; ++i)
|
for (int i = 0; i < 4; ++i)
|
||||||
for (Square s = sq + deltas[i];
|
for (Square s = sq + deltas[i];
|
||||||
is_ok(s) && square_distance(s, s - deltas[i]) == 1;
|
is_ok(s) && distance(s, s - deltas[i]) == 1;
|
||||||
s += deltas[i])
|
s += deltas[i])
|
||||||
{
|
{
|
||||||
attack |= s;
|
attack |= s;
|
||||||
@@ -251,12 +258,11 @@ namespace {
|
|||||||
void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
|
void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
|
||||||
Bitboard masks[], unsigned shifts[], Square deltas[], Fn index) {
|
Bitboard masks[], unsigned shifts[], Square deltas[], Fn index) {
|
||||||
|
|
||||||
int MagicBoosters[][8] = { { 969, 1976, 2850, 542, 2069, 2852, 1708, 164 },
|
int seeds[][RANK_NB] = { { 8977, 44560, 54343, 38998, 5731, 95205, 104912, 17020 },
|
||||||
{ 3101, 552, 3555, 926, 834, 26, 2131, 1117 } };
|
{ 728, 10316, 55013, 32803, 12281, 15100, 16645, 255 } };
|
||||||
|
|
||||||
RKISS rk;
|
|
||||||
Bitboard occupancy[4096], reference[4096], edges, b;
|
Bitboard occupancy[4096], reference[4096], edges, b;
|
||||||
int i, size, booster;
|
int age[4096] = {0}, current = 0, i, size;
|
||||||
|
|
||||||
// attacks[s] is a pointer to the beginning of the attacks table for square 's'
|
// attacks[s] is a pointer to the beginning of the attacks table for square 's'
|
||||||
attacks[SQ_A1] = table;
|
attacks[SQ_A1] = table;
|
||||||
@@ -272,7 +278,7 @@ namespace {
|
|||||||
// 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;
|
masks[s] = sliding_attack(deltas, s, 0) & ~edges;
|
||||||
shifts[s] = (Is64Bit ? 64 : 32) - popcount<Max15>(masks[s]);
|
shifts[s] = (Is64Bit ? 64 : 32) - popcount(masks[s]);
|
||||||
|
|
||||||
// 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[].
|
||||||
@@ -282,7 +288,7 @@ namespace {
|
|||||||
reference[size] = sliding_attack(deltas, s, b);
|
reference[size] = sliding_attack(deltas, s, b);
|
||||||
|
|
||||||
if (HasPext)
|
if (HasPext)
|
||||||
attacks[s][_pext_u64(b, masks[s])] = reference[size];
|
attacks[s][pext(b, masks[s])] = reference[size];
|
||||||
|
|
||||||
size++;
|
size++;
|
||||||
b = (b - masks[s]) & masks[s];
|
b = (b - masks[s]) & masks[s];
|
||||||
@@ -296,30 +302,30 @@ namespace {
|
|||||||
if (HasPext)
|
if (HasPext)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
booster = MagicBoosters[Is64Bit][rank_of(s)];
|
PRNG rng(seeds[Is64Bit][rank_of(s)]);
|
||||||
|
|
||||||
// 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 {
|
do {
|
||||||
do magics[s] = rk.magic_rand<Bitboard>(booster);
|
do
|
||||||
while (popcount<Max15>((magics[s] * masks[s]) >> 56) < 6);
|
magics[s] = rng.sparse_rand<Bitboard>();
|
||||||
|
while (popcount((magics[s] * masks[s]) >> 56) < 6);
|
||||||
std::memset(attacks[s], 0, size * sizeof(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.
|
||||||
for (i = 0; i < size; ++i)
|
for (++current, i = 0; i < size; ++i)
|
||||||
{
|
{
|
||||||
Bitboard& attack = attacks[s][index(s, occupancy[i])];
|
unsigned idx = index(s, occupancy[i]);
|
||||||
|
|
||||||
if (attack && attack != reference[i])
|
if (age[idx] < current)
|
||||||
|
{
|
||||||
|
age[idx] = current;
|
||||||
|
attacks[s][idx] = reference[i];
|
||||||
|
}
|
||||||
|
else if (attacks[s][idx] != reference[i])
|
||||||
break;
|
break;
|
||||||
|
|
||||||
assert(reference[i]);
|
|
||||||
|
|
||||||
attack = reference[i];
|
|
||||||
}
|
}
|
||||||
} while (i < size);
|
} while (i < size);
|
||||||
}
|
}
|
||||||
|
|||||||
+150
-153
@@ -1,14 +1,14 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
(at your option) any later version.
|
(at your option) any later version.
|
||||||
|
|
||||||
|
|
||||||
Stockfish is distributed in the hope that it will be useful,
|
Stockfish is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
@@ -21,8 +21,17 @@
|
|||||||
#ifndef BITBOARD_H_INCLUDED
|
#ifndef BITBOARD_H_INCLUDED
|
||||||
#define BITBOARD_H_INCLUDED
|
#define BITBOARD_H_INCLUDED
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
namespace Bitbases {
|
||||||
|
|
||||||
|
void init();
|
||||||
|
bool probe(Square wksq, Square wpsq, Square bksq, Color us);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
namespace Bitboards {
|
namespace Bitboards {
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
@@ -30,12 +39,7 @@ const std::string pretty(Bitboard b);
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace Bitbases {
|
const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
|
||||||
|
|
||||||
void init_kpk();
|
|
||||||
bool probe_kpk(Square wksq, Square wpsq, Square bksq, Color us);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
const Bitboard FileABB = 0x0101010101010101ULL;
|
const Bitboard FileABB = 0x0101010101010101ULL;
|
||||||
const Bitboard FileBBB = FileABB << 1;
|
const Bitboard FileBBB = FileABB << 1;
|
||||||
@@ -55,17 +59,7 @@ const Bitboard Rank6BB = Rank1BB << (8 * 5);
|
|||||||
const Bitboard Rank7BB = Rank1BB << (8 * 6);
|
const Bitboard Rank7BB = Rank1BB << (8 * 6);
|
||||||
const Bitboard Rank8BB = Rank1BB << (8 * 7);
|
const Bitboard Rank8BB = Rank1BB << (8 * 7);
|
||||||
|
|
||||||
CACHE_LINE_ALIGNMENT
|
extern int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||||
|
|
||||||
extern Bitboard RMasks[SQUARE_NB];
|
|
||||||
extern Bitboard RMagics[SQUARE_NB];
|
|
||||||
extern Bitboard* RAttacks[SQUARE_NB];
|
|
||||||
extern unsigned RShifts[SQUARE_NB];
|
|
||||||
|
|
||||||
extern Bitboard BMasks[SQUARE_NB];
|
|
||||||
extern Bitboard BMagics[SQUARE_NB];
|
|
||||||
extern Bitboard* BAttacks[SQUARE_NB];
|
|
||||||
extern unsigned BShifts[SQUARE_NB];
|
|
||||||
|
|
||||||
extern Bitboard SquareBB[SQUARE_NB];
|
extern Bitboard SquareBB[SQUARE_NB];
|
||||||
extern Bitboard FileBB[FILE_NB];
|
extern Bitboard FileBB[FILE_NB];
|
||||||
@@ -75,15 +69,12 @@ extern Bitboard InFrontBB[COLOR_NB][RANK_NB];
|
|||||||
extern Bitboard StepAttacksBB[PIECE_NB][SQUARE_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 DistanceRingsBB[SQUARE_NB][8];
|
extern Bitboard DistanceRingBB[SQUARE_NB][8];
|
||||||
extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
|
extern Bitboard ForwardBB[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 int SquareDistance[SQUARE_NB][SQUARE_NB];
|
|
||||||
|
|
||||||
const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
|
|
||||||
|
|
||||||
/// 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.
|
||||||
@@ -92,14 +83,6 @@ inline Bitboard operator&(Bitboard b, Square s) {
|
|||||||
return b & SquareBB[s];
|
return b & SquareBB[s];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard& operator|=(Bitboard& b, Square s) {
|
|
||||||
return b |= SquareBB[s];
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Bitboard& operator^=(Bitboard& b, Square s) {
|
|
||||||
return b ^= SquareBB[s];
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Bitboard operator|(Bitboard b, Square s) {
|
inline Bitboard operator|(Bitboard b, Square s) {
|
||||||
return b | SquareBB[s];
|
return b | SquareBB[s];
|
||||||
}
|
}
|
||||||
@@ -108,37 +91,21 @@ inline Bitboard operator^(Bitboard b, Square s) {
|
|||||||
return b ^ SquareBB[s];
|
return b ^ SquareBB[s];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Bitboard& operator|=(Bitboard& b, Square s) {
|
||||||
|
return b |= SquareBB[s];
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Bitboard& operator^=(Bitboard& b, Square s) {
|
||||||
|
return b ^= SquareBB[s];
|
||||||
|
}
|
||||||
|
|
||||||
inline bool more_than_one(Bitboard b) {
|
inline bool more_than_one(Bitboard b) {
|
||||||
return b & (b - 1);
|
return b & (b - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int square_distance(Square s1, Square s2) {
|
|
||||||
return SquareDistance[s1][s2];
|
|
||||||
}
|
|
||||||
|
|
||||||
inline int file_distance(Square s1, Square s2) {
|
/// rank_bb() and file_bb() return a bitboard representing all the squares on
|
||||||
return abs(file_of(s1) - file_of(s2));
|
/// the given file or rank.
|
||||||
}
|
|
||||||
|
|
||||||
inline int rank_distance(Square s1, Square s2) {
|
|
||||||
return abs(rank_of(s1) - rank_of(s2));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// shift_bb() moves bitboard one step along direction Delta. Mainly for pawns.
|
|
||||||
|
|
||||||
template<Square Delta>
|
|
||||||
inline Bitboard shift_bb(Bitboard b) {
|
|
||||||
|
|
||||||
return Delta == DELTA_N ? b << 8 : Delta == DELTA_S ? b >> 8
|
|
||||||
: Delta == DELTA_NE ? (b & ~FileHBB) << 9 : Delta == DELTA_SE ? (b & ~FileHBB) >> 7
|
|
||||||
: Delta == DELTA_NW ? (b & ~FileABB) << 7 : Delta == DELTA_SW ? (b & ~FileABB) >> 9
|
|
||||||
: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// rank_bb() and file_bb() take a file or a square as input and return
|
|
||||||
/// a bitboard representing all squares on the given file or rank.
|
|
||||||
|
|
||||||
inline Bitboard rank_bb(Rank r) {
|
inline Bitboard rank_bb(Rank r) {
|
||||||
return RankBB[r];
|
return RankBB[r];
|
||||||
@@ -157,182 +124,212 @@ inline Bitboard file_bb(Square s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// adjacent_files_bb() takes a file as input and returns a bitboard representing
|
/// shift() moves a bitboard one step along direction D. Mainly for pawns
|
||||||
/// all squares on the adjacent files.
|
|
||||||
|
template<Square D>
|
||||||
|
inline Bitboard shift(Bitboard b) {
|
||||||
|
return D == NORTH ? b << 8 : D == SOUTH ? b >> 8
|
||||||
|
: D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == SOUTH_EAST ? (b & ~FileHBB) >> 7
|
||||||
|
: D == NORTH_WEST ? (b & ~FileABB) << 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
|
||||||
|
: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// adjacent_files_bb() returns a bitboard representing all the squares on the
|
||||||
|
/// adjacent files of the given one.
|
||||||
|
|
||||||
inline Bitboard adjacent_files_bb(File f) {
|
inline Bitboard adjacent_files_bb(File f) {
|
||||||
return AdjacentFilesBB[f];
|
return AdjacentFilesBB[f];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// in_front_bb() takes a color and a rank as input, and returns a bitboard
|
/// between_bb() returns a bitboard representing all the squares between the two
|
||||||
/// representing all the squares on all ranks in front of the rank, from the
|
/// given ones. For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with
|
||||||
/// given color's point of view. For instance, in_front_bb(BLACK, RANK_3) will
|
/// the bits for square d5 and e6 set. If s1 and s2 are not on the same rank, file
|
||||||
/// give all squares on ranks 1 and 2.
|
/// or diagonal, 0 is returned.
|
||||||
|
|
||||||
inline Bitboard in_front_bb(Color c, Rank r) {
|
|
||||||
return InFrontBB[c][r];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// between_bb() returns a bitboard representing all squares between two squares.
|
|
||||||
/// For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with the bits for
|
|
||||||
/// square d5 and e6 set. If s1 and s2 are not on the same rank, file or diagonal,
|
|
||||||
/// 0 is returned.
|
|
||||||
|
|
||||||
inline Bitboard between_bb(Square s1, Square s2) {
|
inline Bitboard between_bb(Square s1, Square s2) {
|
||||||
return BetweenBB[s1][s2];
|
return BetweenBB[s1][s2];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// forward_bb() takes a color and a square as input, and returns a bitboard
|
/// in_front_bb() returns a bitboard representing all the squares on all the ranks
|
||||||
/// representing all squares along the line in front of the square, from the
|
/// in front of the given one, from the point of view of the given color. For
|
||||||
/// point of view of the given color. Definition of the table is:
|
/// instance, in_front_bb(BLACK, RANK_3) will return the squares on ranks 1 and 2.
|
||||||
/// ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s)
|
|
||||||
|
inline Bitboard in_front_bb(Color c, Rank r) {
|
||||||
|
return InFrontBB[c][r];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// forward_bb() returns a bitboard representing all the squares along the line
|
||||||
|
/// in front of the given one, from the point of view of the given color:
|
||||||
|
/// ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s)
|
||||||
|
|
||||||
inline Bitboard forward_bb(Color c, Square s) {
|
inline Bitboard forward_bb(Color c, Square s) {
|
||||||
return ForwardBB[c][s];
|
return ForwardBB[c][s];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// pawn_attack_span() takes a color and a square as input, and returns a bitboard
|
/// pawn_attack_span() returns a bitboard representing all the squares that can be
|
||||||
/// representing all squares that can be attacked by a pawn of the given color
|
/// attacked by a pawn of the given color when it moves along its file, starting
|
||||||
/// when it moves along its file starting from the given square. Definition is:
|
/// from the given square:
|
||||||
/// PawnAttackSpan[c][s] = in_front_bb(c, s) & adjacent_files_bb(s);
|
/// PawnAttackSpan[c][s] = in_front_bb(c, s) & adjacent_files_bb(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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// passed_pawn_mask() takes a color and a square as input, and returns a
|
/// passed_pawn_mask() returns a bitboard mask which can be used to test if a
|
||||||
/// bitboard mask which can be used to test if a pawn of the given color on
|
/// pawn of the given color and on the given square is a passed pawn:
|
||||||
/// the given square is a passed pawn. Definition of the table is:
|
/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_bb(c, s)
|
||||||
/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// squares_of_color() returns a bitboard representing all squares with the same
|
/// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
|
||||||
/// color of the given square.
|
/// straight or on a diagonal line.
|
||||||
|
|
||||||
inline Bitboard squares_of_color(Square s) {
|
|
||||||
return DarkSquares & s ? DarkSquares : ~DarkSquares;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// aligned() returns true if the squares s1, s2 and s3 are aligned
|
|
||||||
/// either on a straight or on a diagonal line.
|
|
||||||
|
|
||||||
inline bool aligned(Square s1, Square s2, Square s3) {
|
inline bool aligned(Square s1, Square s2, Square s3) {
|
||||||
return LineBB[s1][s2] & s3;
|
return LineBB[s1][s2] & s3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Functions for computing sliding attack bitboards. Function attacks_bb() takes
|
/// distance() functions return the distance between x and y, defined as the
|
||||||
/// a square and a bitboard of occupied squares as input, and returns a bitboard
|
/// number of steps for a king in x to reach y. Works with squares, ranks, files.
|
||||||
/// representing all squares attacked by Pt (bishop or rook) on the given square.
|
|
||||||
template<PieceType Pt>
|
|
||||||
FORCE_INLINE unsigned magic_index(Square s, Bitboard occ) {
|
|
||||||
|
|
||||||
Bitboard* const Masks = Pt == ROOK ? RMasks : BMasks;
|
template<typename T> inline int distance(T x, T y) { return x < y ? y - x : x - y; }
|
||||||
Bitboard* const Magics = Pt == ROOK ? RMagics : BMagics;
|
template<> inline int distance<Square>(Square x, Square y) { return SquareDistance[x][y]; }
|
||||||
unsigned* const Shifts = Pt == ROOK ? RShifts : BShifts;
|
|
||||||
|
template<typename T1, typename T2> inline int distance(T2 x, T2 y);
|
||||||
|
template<> inline int distance<File>(Square x, Square y) { return distance(file_of(x), file_of(y)); }
|
||||||
|
template<> inline int distance<Rank>(Square x, Square y) { return distance(rank_of(x), rank_of(y)); }
|
||||||
|
|
||||||
|
|
||||||
|
/// attacks_bb() returns a bitboard representing all the squares attacked by a
|
||||||
|
/// piece of type Pt (bishop or rook) placed on 's'. The helper magic_index()
|
||||||
|
/// looks up the index using the 'magic bitboards' approach.
|
||||||
|
template<PieceType Pt>
|
||||||
|
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)
|
if (HasPext)
|
||||||
return unsigned(_pext_u64(occ, Masks[s]));
|
return unsigned(pext(occupied, Masks[s]));
|
||||||
|
|
||||||
if (Is64Bit)
|
if (Is64Bit)
|
||||||
return unsigned(((occ & Masks[s]) * Magics[s]) >> Shifts[s]);
|
return unsigned(((occupied & Masks[s]) * Magics[s]) >> Shifts[s]);
|
||||||
|
|
||||||
unsigned lo = unsigned(occ) & unsigned(Masks[s]);
|
unsigned lo = unsigned(occupied) & unsigned(Masks[s]);
|
||||||
unsigned hi = unsigned(occ >> 32) & unsigned(Masks[s] >> 32);
|
unsigned hi = unsigned(occupied >> 32) & unsigned(Masks[s] >> 32);
|
||||||
return (lo * unsigned(Magics[s]) ^ hi * unsigned(Magics[s] >> 32)) >> Shifts[s];
|
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 occ) {
|
inline Bitboard attacks_bb(Square s, Bitboard occupied) {
|
||||||
return (Pt == ROOK ? RAttacks : BAttacks)[s][magic_index<Pt>(s, occ)];
|
|
||||||
|
extern Bitboard* RookAttacks[SQUARE_NB];
|
||||||
|
extern Bitboard* BishopAttacks[SQUARE_NB];
|
||||||
|
|
||||||
|
return (Pt == ROOK ? RookAttacks : BishopAttacks)[s][magic_index<Pt>(s, occupied)];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard attacks_bb(Piece pc, Square s, Bitboard occ) {
|
inline Bitboard attacks_bb(Piece pc, Square s, Bitboard occupied) {
|
||||||
|
|
||||||
switch (type_of(pc))
|
switch (type_of(pc))
|
||||||
{
|
{
|
||||||
case BISHOP: return attacks_bb<BISHOP>(s, occ);
|
case BISHOP: return attacks_bb<BISHOP>(s, occupied);
|
||||||
case ROOK : return attacks_bb<ROOK>(s, occ);
|
case ROOK : return attacks_bb<ROOK>(s, occupied);
|
||||||
case QUEEN : return attacks_bb<BISHOP>(s, occ) | attacks_bb<ROOK>(s, occ);
|
case QUEEN : return attacks_bb<BISHOP>(s, occupied) | attacks_bb<ROOK>(s, occupied);
|
||||||
default : return StepAttacksBB[pc][s];
|
default : return StepAttacksBB[pc][s];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// lsb()/msb() finds the least/most significant bit in a non-zero bitboard.
|
|
||||||
/// pop_lsb() finds and clears the least significant bit in a non-zero bitboard.
|
|
||||||
|
|
||||||
#ifdef USE_BSFQ
|
/// popcount() counts the number of non-zero bits in a bitboard
|
||||||
|
|
||||||
# if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
|
inline int popcount(Bitboard b) {
|
||||||
|
|
||||||
FORCE_INLINE Square lsb(Bitboard b) {
|
#ifndef USE_POPCNT
|
||||||
|
|
||||||
|
extern uint8_t PopCnt16[1 << 16];
|
||||||
|
union { Bitboard bb; uint16_t u[4]; } v = { b };
|
||||||
|
return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
|
||||||
|
|
||||||
|
#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
|
||||||
|
|
||||||
|
return (int)_mm_popcnt_u64(b);
|
||||||
|
|
||||||
|
#else // Assumed gcc or compatible compiler
|
||||||
|
|
||||||
|
return __builtin_popcountll(b);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// lsb() and msb() return the least/most significant bit in a non-zero bitboard
|
||||||
|
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
|
||||||
|
inline Square lsb(Bitboard b) {
|
||||||
|
assert(b);
|
||||||
|
return Square(__builtin_ctzll(b));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Square msb(Bitboard b) {
|
||||||
|
assert(b);
|
||||||
|
return Square(63 - __builtin_clzll(b));
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined(_WIN64) && defined(_MSC_VER)
|
||||||
|
|
||||||
|
inline Square lsb(Bitboard b) {
|
||||||
|
assert(b);
|
||||||
unsigned long idx;
|
unsigned long idx;
|
||||||
_BitScanForward64(&idx, b);
|
_BitScanForward64(&idx, b);
|
||||||
return (Square) idx;
|
return (Square) idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
FORCE_INLINE Square msb(Bitboard b) {
|
inline Square msb(Bitboard b) {
|
||||||
|
assert(b);
|
||||||
unsigned long idx;
|
unsigned long idx;
|
||||||
_BitScanReverse64(&idx, b);
|
_BitScanReverse64(&idx, b);
|
||||||
return (Square) idx;
|
return (Square) idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
# elif defined(__arm__)
|
#else
|
||||||
|
|
||||||
FORCE_INLINE int lsb32(uint32_t v) {
|
#define NO_BSF // Fallback on software implementation for other cases
|
||||||
__asm__("rbit %0, %1" : "=r"(v) : "r"(v));
|
|
||||||
return __builtin_clz(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
FORCE_INLINE Square msb(Bitboard b) {
|
Square lsb(Bitboard b);
|
||||||
return (Square) (63 - __builtin_clzll(b));
|
Square msb(Bitboard b);
|
||||||
}
|
|
||||||
|
|
||||||
FORCE_INLINE Square lsb(Bitboard b) {
|
#endif
|
||||||
return (Square) (uint32_t(b) ? lsb32(uint32_t(b)) : 32 + lsb32(uint32_t(b >> 32)));
|
|
||||||
}
|
|
||||||
|
|
||||||
# else
|
|
||||||
|
|
||||||
FORCE_INLINE Square lsb(Bitboard b) { // Assembly code by Heinz van Saanen
|
/// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
|
||||||
Bitboard idx;
|
|
||||||
__asm__("bsfq %1, %0": "=r"(idx): "rm"(b) );
|
|
||||||
return (Square) idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
FORCE_INLINE Square msb(Bitboard b) {
|
inline Square pop_lsb(Bitboard* b) {
|
||||||
Bitboard idx;
|
|
||||||
__asm__("bsrq %1, %0": "=r"(idx): "rm"(b) );
|
|
||||||
return (Square) idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
# endif
|
|
||||||
|
|
||||||
FORCE_INLINE Square pop_lsb(Bitboard* b) {
|
|
||||||
const Square s = lsb(*b);
|
const Square s = lsb(*b);
|
||||||
*b &= *b - 1;
|
*b &= *b - 1;
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else // if defined(USE_BSFQ)
|
|
||||||
|
|
||||||
extern Square msb(Bitboard b);
|
/// frontmost_sq() and backmost_sq() return the square corresponding to the
|
||||||
extern Square lsb(Bitboard b);
|
|
||||||
extern Square pop_lsb(Bitboard* b);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/// frontmost_sq() and backmost_sq() find the square corresponding to the
|
|
||||||
/// most/least advanced bit relative to the given color.
|
/// most/least advanced bit relative to the given color.
|
||||||
|
|
||||||
inline Square frontmost_sq(Color c, Bitboard b) { return c == WHITE ? msb(b) : lsb(b); }
|
inline Square frontmost_sq(Color c, Bitboard b) { return c == WHITE ? msb(b) : lsb(b); }
|
||||||
|
|||||||
-105
@@ -1,105 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
|
||||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
|
|
||||||
Stockfish is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef BITCOUNT_H_INCLUDED
|
|
||||||
#define BITCOUNT_H_INCLUDED
|
|
||||||
|
|
||||||
#include <cassert>
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
enum BitCountType {
|
|
||||||
CNT_64,
|
|
||||||
CNT_64_MAX15,
|
|
||||||
CNT_32,
|
|
||||||
CNT_32_MAX15,
|
|
||||||
CNT_HW_POPCNT
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Determine at compile time the best popcount<> specialization according to
|
|
||||||
/// whether the platform is 32 or 64 bit, the maximum number of non-zero
|
|
||||||
/// bits to count and if the hardware popcnt instruction is available.
|
|
||||||
const BitCountType Full = HasPopCnt ? CNT_HW_POPCNT : Is64Bit ? CNT_64 : CNT_32;
|
|
||||||
const BitCountType Max15 = HasPopCnt ? CNT_HW_POPCNT : Is64Bit ? CNT_64_MAX15 : CNT_32_MAX15;
|
|
||||||
|
|
||||||
|
|
||||||
/// popcount() counts the number of non-zero bits in a bitboard
|
|
||||||
template<BitCountType> inline int popcount(Bitboard);
|
|
||||||
|
|
||||||
template<>
|
|
||||||
inline int popcount<CNT_64>(Bitboard b) {
|
|
||||||
b -= (b >> 1) & 0x5555555555555555ULL;
|
|
||||||
b = ((b >> 2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
|
|
||||||
b = ((b >> 4) + b) & 0x0F0F0F0F0F0F0F0FULL;
|
|
||||||
return (b * 0x0101010101010101ULL) >> 56;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<>
|
|
||||||
inline int popcount<CNT_64_MAX15>(Bitboard b) {
|
|
||||||
b -= (b >> 1) & 0x5555555555555555ULL;
|
|
||||||
b = ((b >> 2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
|
|
||||||
return (b * 0x1111111111111111ULL) >> 60;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<>
|
|
||||||
inline int popcount<CNT_32>(Bitboard b) {
|
|
||||||
unsigned w = unsigned(b >> 32), v = unsigned(b);
|
|
||||||
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
|
|
||||||
w -= (w >> 1) & 0x55555555;
|
|
||||||
v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
|
|
||||||
w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
|
|
||||||
v = ((v >> 4) + v + (w >> 4) + w) & 0x0F0F0F0F;
|
|
||||||
return (v * 0x01010101) >> 24;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<>
|
|
||||||
inline int popcount<CNT_32_MAX15>(Bitboard b) {
|
|
||||||
unsigned w = unsigned(b >> 32), v = unsigned(b);
|
|
||||||
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
|
|
||||||
w -= (w >> 1) & 0x55555555;
|
|
||||||
v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
|
|
||||||
w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
|
|
||||||
return ((v + w) * 0x11111111) >> 28;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<>
|
|
||||||
inline int popcount<CNT_HW_POPCNT>(Bitboard b) {
|
|
||||||
|
|
||||||
#ifndef USE_POPCNT
|
|
||||||
|
|
||||||
assert(false);
|
|
||||||
return b != 0; // Avoid 'b not used' warning
|
|
||||||
|
|
||||||
#elif defined(_MSC_VER) && defined(__INTEL_COMPILER)
|
|
||||||
|
|
||||||
return _mm_popcnt_u64(b);
|
|
||||||
|
|
||||||
#elif defined(_MSC_VER)
|
|
||||||
|
|
||||||
return (int)__popcnt64(b);
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
__asm__("popcnt %1, %0" : "=r" (b) : "r" (b));
|
|
||||||
return b;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // #ifndef BITCOUNT_H_INCLUDED
|
|
||||||
-478
@@ -1,478 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
|
||||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
Stockfish is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
The code in this file is based on the opening book code in PolyGlot
|
|
||||||
by Fabien Letouzey. PolyGlot is available under the GNU General
|
|
||||||
Public License, and can be downloaded from http://wbec-ridderkerk.nl
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <cassert>
|
|
||||||
|
|
||||||
#include "book.h"
|
|
||||||
#include "misc.h"
|
|
||||||
#include "movegen.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
// A Polyglot book is a series of "entries" of 16 bytes. All integers are
|
|
||||||
// stored in big-endian format, with the highest byte first (regardless of
|
|
||||||
// size). The entries are ordered according to the key in ascending order.
|
|
||||||
struct Entry {
|
|
||||||
uint64_t key;
|
|
||||||
uint16_t move;
|
|
||||||
uint16_t count;
|
|
||||||
uint32_t learn;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Random numbers from PolyGlot, used to compute book hash keys
|
|
||||||
const union {
|
|
||||||
Key PolyGlotRandoms[781];
|
|
||||||
struct {
|
|
||||||
Key psq[12][64]; // [piece][square]
|
|
||||||
Key castling[4]; // [castling flag]
|
|
||||||
Key enpassant[8]; // [file]
|
|
||||||
Key turn;
|
|
||||||
} Zobrist;
|
|
||||||
} PG = {{
|
|
||||||
0x9D39247E33776D41ULL, 0x2AF7398005AAA5C7ULL, 0x44DB015024623547ULL,
|
|
||||||
0x9C15F73E62A76AE2ULL, 0x75834465489C0C89ULL, 0x3290AC3A203001BFULL,
|
|
||||||
0x0FBBAD1F61042279ULL, 0xE83A908FF2FB60CAULL, 0x0D7E765D58755C10ULL,
|
|
||||||
0x1A083822CEAFE02DULL, 0x9605D5F0E25EC3B0ULL, 0xD021FF5CD13A2ED5ULL,
|
|
||||||
0x40BDF15D4A672E32ULL, 0x011355146FD56395ULL, 0x5DB4832046F3D9E5ULL,
|
|
||||||
0x239F8B2D7FF719CCULL, 0x05D1A1AE85B49AA1ULL, 0x679F848F6E8FC971ULL,
|
|
||||||
0x7449BBFF801FED0BULL, 0x7D11CDB1C3B7ADF0ULL, 0x82C7709E781EB7CCULL,
|
|
||||||
0xF3218F1C9510786CULL, 0x331478F3AF51BBE6ULL, 0x4BB38DE5E7219443ULL,
|
|
||||||
0xAA649C6EBCFD50FCULL, 0x8DBD98A352AFD40BULL, 0x87D2074B81D79217ULL,
|
|
||||||
0x19F3C751D3E92AE1ULL, 0xB4AB30F062B19ABFULL, 0x7B0500AC42047AC4ULL,
|
|
||||||
0xC9452CA81A09D85DULL, 0x24AA6C514DA27500ULL, 0x4C9F34427501B447ULL,
|
|
||||||
0x14A68FD73C910841ULL, 0xA71B9B83461CBD93ULL, 0x03488B95B0F1850FULL,
|
|
||||||
0x637B2B34FF93C040ULL, 0x09D1BC9A3DD90A94ULL, 0x3575668334A1DD3BULL,
|
|
||||||
0x735E2B97A4C45A23ULL, 0x18727070F1BD400BULL, 0x1FCBACD259BF02E7ULL,
|
|
||||||
0xD310A7C2CE9B6555ULL, 0xBF983FE0FE5D8244ULL, 0x9F74D14F7454A824ULL,
|
|
||||||
0x51EBDC4AB9BA3035ULL, 0x5C82C505DB9AB0FAULL, 0xFCF7FE8A3430B241ULL,
|
|
||||||
0x3253A729B9BA3DDEULL, 0x8C74C368081B3075ULL, 0xB9BC6C87167C33E7ULL,
|
|
||||||
0x7EF48F2B83024E20ULL, 0x11D505D4C351BD7FULL, 0x6568FCA92C76A243ULL,
|
|
||||||
0x4DE0B0F40F32A7B8ULL, 0x96D693460CC37E5DULL, 0x42E240CB63689F2FULL,
|
|
||||||
0x6D2BDCDAE2919661ULL, 0x42880B0236E4D951ULL, 0x5F0F4A5898171BB6ULL,
|
|
||||||
0x39F890F579F92F88ULL, 0x93C5B5F47356388BULL, 0x63DC359D8D231B78ULL,
|
|
||||||
0xEC16CA8AEA98AD76ULL, 0x5355F900C2A82DC7ULL, 0x07FB9F855A997142ULL,
|
|
||||||
0x5093417AA8A7ED5EULL, 0x7BCBC38DA25A7F3CULL, 0x19FC8A768CF4B6D4ULL,
|
|
||||||
0x637A7780DECFC0D9ULL, 0x8249A47AEE0E41F7ULL, 0x79AD695501E7D1E8ULL,
|
|
||||||
0x14ACBAF4777D5776ULL, 0xF145B6BECCDEA195ULL, 0xDABF2AC8201752FCULL,
|
|
||||||
0x24C3C94DF9C8D3F6ULL, 0xBB6E2924F03912EAULL, 0x0CE26C0B95C980D9ULL,
|
|
||||||
0xA49CD132BFBF7CC4ULL, 0xE99D662AF4243939ULL, 0x27E6AD7891165C3FULL,
|
|
||||||
0x8535F040B9744FF1ULL, 0x54B3F4FA5F40D873ULL, 0x72B12C32127FED2BULL,
|
|
||||||
0xEE954D3C7B411F47ULL, 0x9A85AC909A24EAA1ULL, 0x70AC4CD9F04F21F5ULL,
|
|
||||||
0xF9B89D3E99A075C2ULL, 0x87B3E2B2B5C907B1ULL, 0xA366E5B8C54F48B8ULL,
|
|
||||||
0xAE4A9346CC3F7CF2ULL, 0x1920C04D47267BBDULL, 0x87BF02C6B49E2AE9ULL,
|
|
||||||
0x092237AC237F3859ULL, 0xFF07F64EF8ED14D0ULL, 0x8DE8DCA9F03CC54EULL,
|
|
||||||
0x9C1633264DB49C89ULL, 0xB3F22C3D0B0B38EDULL, 0x390E5FB44D01144BULL,
|
|
||||||
0x5BFEA5B4712768E9ULL, 0x1E1032911FA78984ULL, 0x9A74ACB964E78CB3ULL,
|
|
||||||
0x4F80F7A035DAFB04ULL, 0x6304D09A0B3738C4ULL, 0x2171E64683023A08ULL,
|
|
||||||
0x5B9B63EB9CEFF80CULL, 0x506AACF489889342ULL, 0x1881AFC9A3A701D6ULL,
|
|
||||||
0x6503080440750644ULL, 0xDFD395339CDBF4A7ULL, 0xEF927DBCF00C20F2ULL,
|
|
||||||
0x7B32F7D1E03680ECULL, 0xB9FD7620E7316243ULL, 0x05A7E8A57DB91B77ULL,
|
|
||||||
0xB5889C6E15630A75ULL, 0x4A750A09CE9573F7ULL, 0xCF464CEC899A2F8AULL,
|
|
||||||
0xF538639CE705B824ULL, 0x3C79A0FF5580EF7FULL, 0xEDE6C87F8477609DULL,
|
|
||||||
0x799E81F05BC93F31ULL, 0x86536B8CF3428A8CULL, 0x97D7374C60087B73ULL,
|
|
||||||
0xA246637CFF328532ULL, 0x043FCAE60CC0EBA0ULL, 0x920E449535DD359EULL,
|
|
||||||
0x70EB093B15B290CCULL, 0x73A1921916591CBDULL, 0x56436C9FE1A1AA8DULL,
|
|
||||||
0xEFAC4B70633B8F81ULL, 0xBB215798D45DF7AFULL, 0x45F20042F24F1768ULL,
|
|
||||||
0x930F80F4E8EB7462ULL, 0xFF6712FFCFD75EA1ULL, 0xAE623FD67468AA70ULL,
|
|
||||||
0xDD2C5BC84BC8D8FCULL, 0x7EED120D54CF2DD9ULL, 0x22FE545401165F1CULL,
|
|
||||||
0xC91800E98FB99929ULL, 0x808BD68E6AC10365ULL, 0xDEC468145B7605F6ULL,
|
|
||||||
0x1BEDE3A3AEF53302ULL, 0x43539603D6C55602ULL, 0xAA969B5C691CCB7AULL,
|
|
||||||
0xA87832D392EFEE56ULL, 0x65942C7B3C7E11AEULL, 0xDED2D633CAD004F6ULL,
|
|
||||||
0x21F08570F420E565ULL, 0xB415938D7DA94E3CULL, 0x91B859E59ECB6350ULL,
|
|
||||||
0x10CFF333E0ED804AULL, 0x28AED140BE0BB7DDULL, 0xC5CC1D89724FA456ULL,
|
|
||||||
0x5648F680F11A2741ULL, 0x2D255069F0B7DAB3ULL, 0x9BC5A38EF729ABD4ULL,
|
|
||||||
0xEF2F054308F6A2BCULL, 0xAF2042F5CC5C2858ULL, 0x480412BAB7F5BE2AULL,
|
|
||||||
0xAEF3AF4A563DFE43ULL, 0x19AFE59AE451497FULL, 0x52593803DFF1E840ULL,
|
|
||||||
0xF4F076E65F2CE6F0ULL, 0x11379625747D5AF3ULL, 0xBCE5D2248682C115ULL,
|
|
||||||
0x9DA4243DE836994FULL, 0x066F70B33FE09017ULL, 0x4DC4DE189B671A1CULL,
|
|
||||||
0x51039AB7712457C3ULL, 0xC07A3F80C31FB4B4ULL, 0xB46EE9C5E64A6E7CULL,
|
|
||||||
0xB3819A42ABE61C87ULL, 0x21A007933A522A20ULL, 0x2DF16F761598AA4FULL,
|
|
||||||
0x763C4A1371B368FDULL, 0xF793C46702E086A0ULL, 0xD7288E012AEB8D31ULL,
|
|
||||||
0xDE336A2A4BC1C44BULL, 0x0BF692B38D079F23ULL, 0x2C604A7A177326B3ULL,
|
|
||||||
0x4850E73E03EB6064ULL, 0xCFC447F1E53C8E1BULL, 0xB05CA3F564268D99ULL,
|
|
||||||
0x9AE182C8BC9474E8ULL, 0xA4FC4BD4FC5558CAULL, 0xE755178D58FC4E76ULL,
|
|
||||||
0x69B97DB1A4C03DFEULL, 0xF9B5B7C4ACC67C96ULL, 0xFC6A82D64B8655FBULL,
|
|
||||||
0x9C684CB6C4D24417ULL, 0x8EC97D2917456ED0ULL, 0x6703DF9D2924E97EULL,
|
|
||||||
0xC547F57E42A7444EULL, 0x78E37644E7CAD29EULL, 0xFE9A44E9362F05FAULL,
|
|
||||||
0x08BD35CC38336615ULL, 0x9315E5EB3A129ACEULL, 0x94061B871E04DF75ULL,
|
|
||||||
0xDF1D9F9D784BA010ULL, 0x3BBA57B68871B59DULL, 0xD2B7ADEEDED1F73FULL,
|
|
||||||
0xF7A255D83BC373F8ULL, 0xD7F4F2448C0CEB81ULL, 0xD95BE88CD210FFA7ULL,
|
|
||||||
0x336F52F8FF4728E7ULL, 0xA74049DAC312AC71ULL, 0xA2F61BB6E437FDB5ULL,
|
|
||||||
0x4F2A5CB07F6A35B3ULL, 0x87D380BDA5BF7859ULL, 0x16B9F7E06C453A21ULL,
|
|
||||||
0x7BA2484C8A0FD54EULL, 0xF3A678CAD9A2E38CULL, 0x39B0BF7DDE437BA2ULL,
|
|
||||||
0xFCAF55C1BF8A4424ULL, 0x18FCF680573FA594ULL, 0x4C0563B89F495AC3ULL,
|
|
||||||
0x40E087931A00930DULL, 0x8CFFA9412EB642C1ULL, 0x68CA39053261169FULL,
|
|
||||||
0x7A1EE967D27579E2ULL, 0x9D1D60E5076F5B6FULL, 0x3810E399B6F65BA2ULL,
|
|
||||||
0x32095B6D4AB5F9B1ULL, 0x35CAB62109DD038AULL, 0xA90B24499FCFAFB1ULL,
|
|
||||||
0x77A225A07CC2C6BDULL, 0x513E5E634C70E331ULL, 0x4361C0CA3F692F12ULL,
|
|
||||||
0xD941ACA44B20A45BULL, 0x528F7C8602C5807BULL, 0x52AB92BEB9613989ULL,
|
|
||||||
0x9D1DFA2EFC557F73ULL, 0x722FF175F572C348ULL, 0x1D1260A51107FE97ULL,
|
|
||||||
0x7A249A57EC0C9BA2ULL, 0x04208FE9E8F7F2D6ULL, 0x5A110C6058B920A0ULL,
|
|
||||||
0x0CD9A497658A5698ULL, 0x56FD23C8F9715A4CULL, 0x284C847B9D887AAEULL,
|
|
||||||
0x04FEABFBBDB619CBULL, 0x742E1E651C60BA83ULL, 0x9A9632E65904AD3CULL,
|
|
||||||
0x881B82A13B51B9E2ULL, 0x506E6744CD974924ULL, 0xB0183DB56FFC6A79ULL,
|
|
||||||
0x0ED9B915C66ED37EULL, 0x5E11E86D5873D484ULL, 0xF678647E3519AC6EULL,
|
|
||||||
0x1B85D488D0F20CC5ULL, 0xDAB9FE6525D89021ULL, 0x0D151D86ADB73615ULL,
|
|
||||||
0xA865A54EDCC0F019ULL, 0x93C42566AEF98FFBULL, 0x99E7AFEABE000731ULL,
|
|
||||||
0x48CBFF086DDF285AULL, 0x7F9B6AF1EBF78BAFULL, 0x58627E1A149BBA21ULL,
|
|
||||||
0x2CD16E2ABD791E33ULL, 0xD363EFF5F0977996ULL, 0x0CE2A38C344A6EEDULL,
|
|
||||||
0x1A804AADB9CFA741ULL, 0x907F30421D78C5DEULL, 0x501F65EDB3034D07ULL,
|
|
||||||
0x37624AE5A48FA6E9ULL, 0x957BAF61700CFF4EULL, 0x3A6C27934E31188AULL,
|
|
||||||
0xD49503536ABCA345ULL, 0x088E049589C432E0ULL, 0xF943AEE7FEBF21B8ULL,
|
|
||||||
0x6C3B8E3E336139D3ULL, 0x364F6FFA464EE52EULL, 0xD60F6DCEDC314222ULL,
|
|
||||||
0x56963B0DCA418FC0ULL, 0x16F50EDF91E513AFULL, 0xEF1955914B609F93ULL,
|
|
||||||
0x565601C0364E3228ULL, 0xECB53939887E8175ULL, 0xBAC7A9A18531294BULL,
|
|
||||||
0xB344C470397BBA52ULL, 0x65D34954DAF3CEBDULL, 0xB4B81B3FA97511E2ULL,
|
|
||||||
0xB422061193D6F6A7ULL, 0x071582401C38434DULL, 0x7A13F18BBEDC4FF5ULL,
|
|
||||||
0xBC4097B116C524D2ULL, 0x59B97885E2F2EA28ULL, 0x99170A5DC3115544ULL,
|
|
||||||
0x6F423357E7C6A9F9ULL, 0x325928EE6E6F8794ULL, 0xD0E4366228B03343ULL,
|
|
||||||
0x565C31F7DE89EA27ULL, 0x30F5611484119414ULL, 0xD873DB391292ED4FULL,
|
|
||||||
0x7BD94E1D8E17DEBCULL, 0xC7D9F16864A76E94ULL, 0x947AE053EE56E63CULL,
|
|
||||||
0xC8C93882F9475F5FULL, 0x3A9BF55BA91F81CAULL, 0xD9A11FBB3D9808E4ULL,
|
|
||||||
0x0FD22063EDC29FCAULL, 0xB3F256D8ACA0B0B9ULL, 0xB03031A8B4516E84ULL,
|
|
||||||
0x35DD37D5871448AFULL, 0xE9F6082B05542E4EULL, 0xEBFAFA33D7254B59ULL,
|
|
||||||
0x9255ABB50D532280ULL, 0xB9AB4CE57F2D34F3ULL, 0x693501D628297551ULL,
|
|
||||||
0xC62C58F97DD949BFULL, 0xCD454F8F19C5126AULL, 0xBBE83F4ECC2BDECBULL,
|
|
||||||
0xDC842B7E2819E230ULL, 0xBA89142E007503B8ULL, 0xA3BC941D0A5061CBULL,
|
|
||||||
0xE9F6760E32CD8021ULL, 0x09C7E552BC76492FULL, 0x852F54934DA55CC9ULL,
|
|
||||||
0x8107FCCF064FCF56ULL, 0x098954D51FFF6580ULL, 0x23B70EDB1955C4BFULL,
|
|
||||||
0xC330DE426430F69DULL, 0x4715ED43E8A45C0AULL, 0xA8D7E4DAB780A08DULL,
|
|
||||||
0x0572B974F03CE0BBULL, 0xB57D2E985E1419C7ULL, 0xE8D9ECBE2CF3D73FULL,
|
|
||||||
0x2FE4B17170E59750ULL, 0x11317BA87905E790ULL, 0x7FBF21EC8A1F45ECULL,
|
|
||||||
0x1725CABFCB045B00ULL, 0x964E915CD5E2B207ULL, 0x3E2B8BCBF016D66DULL,
|
|
||||||
0xBE7444E39328A0ACULL, 0xF85B2B4FBCDE44B7ULL, 0x49353FEA39BA63B1ULL,
|
|
||||||
0x1DD01AAFCD53486AULL, 0x1FCA8A92FD719F85ULL, 0xFC7C95D827357AFAULL,
|
|
||||||
0x18A6A990C8B35EBDULL, 0xCCCB7005C6B9C28DULL, 0x3BDBB92C43B17F26ULL,
|
|
||||||
0xAA70B5B4F89695A2ULL, 0xE94C39A54A98307FULL, 0xB7A0B174CFF6F36EULL,
|
|
||||||
0xD4DBA84729AF48ADULL, 0x2E18BC1AD9704A68ULL, 0x2DE0966DAF2F8B1CULL,
|
|
||||||
0xB9C11D5B1E43A07EULL, 0x64972D68DEE33360ULL, 0x94628D38D0C20584ULL,
|
|
||||||
0xDBC0D2B6AB90A559ULL, 0xD2733C4335C6A72FULL, 0x7E75D99D94A70F4DULL,
|
|
||||||
0x6CED1983376FA72BULL, 0x97FCAACBF030BC24ULL, 0x7B77497B32503B12ULL,
|
|
||||||
0x8547EDDFB81CCB94ULL, 0x79999CDFF70902CBULL, 0xCFFE1939438E9B24ULL,
|
|
||||||
0x829626E3892D95D7ULL, 0x92FAE24291F2B3F1ULL, 0x63E22C147B9C3403ULL,
|
|
||||||
0xC678B6D860284A1CULL, 0x5873888850659AE7ULL, 0x0981DCD296A8736DULL,
|
|
||||||
0x9F65789A6509A440ULL, 0x9FF38FED72E9052FULL, 0xE479EE5B9930578CULL,
|
|
||||||
0xE7F28ECD2D49EECDULL, 0x56C074A581EA17FEULL, 0x5544F7D774B14AEFULL,
|
|
||||||
0x7B3F0195FC6F290FULL, 0x12153635B2C0CF57ULL, 0x7F5126DBBA5E0CA7ULL,
|
|
||||||
0x7A76956C3EAFB413ULL, 0x3D5774A11D31AB39ULL, 0x8A1B083821F40CB4ULL,
|
|
||||||
0x7B4A38E32537DF62ULL, 0x950113646D1D6E03ULL, 0x4DA8979A0041E8A9ULL,
|
|
||||||
0x3BC36E078F7515D7ULL, 0x5D0A12F27AD310D1ULL, 0x7F9D1A2E1EBE1327ULL,
|
|
||||||
0xDA3A361B1C5157B1ULL, 0xDCDD7D20903D0C25ULL, 0x36833336D068F707ULL,
|
|
||||||
0xCE68341F79893389ULL, 0xAB9090168DD05F34ULL, 0x43954B3252DC25E5ULL,
|
|
||||||
0xB438C2B67F98E5E9ULL, 0x10DCD78E3851A492ULL, 0xDBC27AB5447822BFULL,
|
|
||||||
0x9B3CDB65F82CA382ULL, 0xB67B7896167B4C84ULL, 0xBFCED1B0048EAC50ULL,
|
|
||||||
0xA9119B60369FFEBDULL, 0x1FFF7AC80904BF45ULL, 0xAC12FB171817EEE7ULL,
|
|
||||||
0xAF08DA9177DDA93DULL, 0x1B0CAB936E65C744ULL, 0xB559EB1D04E5E932ULL,
|
|
||||||
0xC37B45B3F8D6F2BAULL, 0xC3A9DC228CAAC9E9ULL, 0xF3B8B6675A6507FFULL,
|
|
||||||
0x9FC477DE4ED681DAULL, 0x67378D8ECCEF96CBULL, 0x6DD856D94D259236ULL,
|
|
||||||
0xA319CE15B0B4DB31ULL, 0x073973751F12DD5EULL, 0x8A8E849EB32781A5ULL,
|
|
||||||
0xE1925C71285279F5ULL, 0x74C04BF1790C0EFEULL, 0x4DDA48153C94938AULL,
|
|
||||||
0x9D266D6A1CC0542CULL, 0x7440FB816508C4FEULL, 0x13328503DF48229FULL,
|
|
||||||
0xD6BF7BAEE43CAC40ULL, 0x4838D65F6EF6748FULL, 0x1E152328F3318DEAULL,
|
|
||||||
0x8F8419A348F296BFULL, 0x72C8834A5957B511ULL, 0xD7A023A73260B45CULL,
|
|
||||||
0x94EBC8ABCFB56DAEULL, 0x9FC10D0F989993E0ULL, 0xDE68A2355B93CAE6ULL,
|
|
||||||
0xA44CFE79AE538BBEULL, 0x9D1D84FCCE371425ULL, 0x51D2B1AB2DDFB636ULL,
|
|
||||||
0x2FD7E4B9E72CD38CULL, 0x65CA5B96B7552210ULL, 0xDD69A0D8AB3B546DULL,
|
|
||||||
0x604D51B25FBF70E2ULL, 0x73AA8A564FB7AC9EULL, 0x1A8C1E992B941148ULL,
|
|
||||||
0xAAC40A2703D9BEA0ULL, 0x764DBEAE7FA4F3A6ULL, 0x1E99B96E70A9BE8BULL,
|
|
||||||
0x2C5E9DEB57EF4743ULL, 0x3A938FEE32D29981ULL, 0x26E6DB8FFDF5ADFEULL,
|
|
||||||
0x469356C504EC9F9DULL, 0xC8763C5B08D1908CULL, 0x3F6C6AF859D80055ULL,
|
|
||||||
0x7F7CC39420A3A545ULL, 0x9BFB227EBDF4C5CEULL, 0x89039D79D6FC5C5CULL,
|
|
||||||
0x8FE88B57305E2AB6ULL, 0xA09E8C8C35AB96DEULL, 0xFA7E393983325753ULL,
|
|
||||||
0xD6B6D0ECC617C699ULL, 0xDFEA21EA9E7557E3ULL, 0xB67C1FA481680AF8ULL,
|
|
||||||
0xCA1E3785A9E724E5ULL, 0x1CFC8BED0D681639ULL, 0xD18D8549D140CAEAULL,
|
|
||||||
0x4ED0FE7E9DC91335ULL, 0xE4DBF0634473F5D2ULL, 0x1761F93A44D5AEFEULL,
|
|
||||||
0x53898E4C3910DA55ULL, 0x734DE8181F6EC39AULL, 0x2680B122BAA28D97ULL,
|
|
||||||
0x298AF231C85BAFABULL, 0x7983EED3740847D5ULL, 0x66C1A2A1A60CD889ULL,
|
|
||||||
0x9E17E49642A3E4C1ULL, 0xEDB454E7BADC0805ULL, 0x50B704CAB602C329ULL,
|
|
||||||
0x4CC317FB9CDDD023ULL, 0x66B4835D9EAFEA22ULL, 0x219B97E26FFC81BDULL,
|
|
||||||
0x261E4E4C0A333A9DULL, 0x1FE2CCA76517DB90ULL, 0xD7504DFA8816EDBBULL,
|
|
||||||
0xB9571FA04DC089C8ULL, 0x1DDC0325259B27DEULL, 0xCF3F4688801EB9AAULL,
|
|
||||||
0xF4F5D05C10CAB243ULL, 0x38B6525C21A42B0EULL, 0x36F60E2BA4FA6800ULL,
|
|
||||||
0xEB3593803173E0CEULL, 0x9C4CD6257C5A3603ULL, 0xAF0C317D32ADAA8AULL,
|
|
||||||
0x258E5A80C7204C4BULL, 0x8B889D624D44885DULL, 0xF4D14597E660F855ULL,
|
|
||||||
0xD4347F66EC8941C3ULL, 0xE699ED85B0DFB40DULL, 0x2472F6207C2D0484ULL,
|
|
||||||
0xC2A1E7B5B459AEB5ULL, 0xAB4F6451CC1D45ECULL, 0x63767572AE3D6174ULL,
|
|
||||||
0xA59E0BD101731A28ULL, 0x116D0016CB948F09ULL, 0x2CF9C8CA052F6E9FULL,
|
|
||||||
0x0B090A7560A968E3ULL, 0xABEEDDB2DDE06FF1ULL, 0x58EFC10B06A2068DULL,
|
|
||||||
0xC6E57A78FBD986E0ULL, 0x2EAB8CA63CE802D7ULL, 0x14A195640116F336ULL,
|
|
||||||
0x7C0828DD624EC390ULL, 0xD74BBE77E6116AC7ULL, 0x804456AF10F5FB53ULL,
|
|
||||||
0xEBE9EA2ADF4321C7ULL, 0x03219A39EE587A30ULL, 0x49787FEF17AF9924ULL,
|
|
||||||
0xA1E9300CD8520548ULL, 0x5B45E522E4B1B4EFULL, 0xB49C3B3995091A36ULL,
|
|
||||||
0xD4490AD526F14431ULL, 0x12A8F216AF9418C2ULL, 0x001F837CC7350524ULL,
|
|
||||||
0x1877B51E57A764D5ULL, 0xA2853B80F17F58EEULL, 0x993E1DE72D36D310ULL,
|
|
||||||
0xB3598080CE64A656ULL, 0x252F59CF0D9F04BBULL, 0xD23C8E176D113600ULL,
|
|
||||||
0x1BDA0492E7E4586EULL, 0x21E0BD5026C619BFULL, 0x3B097ADAF088F94EULL,
|
|
||||||
0x8D14DEDB30BE846EULL, 0xF95CFFA23AF5F6F4ULL, 0x3871700761B3F743ULL,
|
|
||||||
0xCA672B91E9E4FA16ULL, 0x64C8E531BFF53B55ULL, 0x241260ED4AD1E87DULL,
|
|
||||||
0x106C09B972D2E822ULL, 0x7FBA195410E5CA30ULL, 0x7884D9BC6CB569D8ULL,
|
|
||||||
0x0647DFEDCD894A29ULL, 0x63573FF03E224774ULL, 0x4FC8E9560F91B123ULL,
|
|
||||||
0x1DB956E450275779ULL, 0xB8D91274B9E9D4FBULL, 0xA2EBEE47E2FBFCE1ULL,
|
|
||||||
0xD9F1F30CCD97FB09ULL, 0xEFED53D75FD64E6BULL, 0x2E6D02C36017F67FULL,
|
|
||||||
0xA9AA4D20DB084E9BULL, 0xB64BE8D8B25396C1ULL, 0x70CB6AF7C2D5BCF0ULL,
|
|
||||||
0x98F076A4F7A2322EULL, 0xBF84470805E69B5FULL, 0x94C3251F06F90CF3ULL,
|
|
||||||
0x3E003E616A6591E9ULL, 0xB925A6CD0421AFF3ULL, 0x61BDD1307C66E300ULL,
|
|
||||||
0xBF8D5108E27E0D48ULL, 0x240AB57A8B888B20ULL, 0xFC87614BAF287E07ULL,
|
|
||||||
0xEF02CDD06FFDB432ULL, 0xA1082C0466DF6C0AULL, 0x8215E577001332C8ULL,
|
|
||||||
0xD39BB9C3A48DB6CFULL, 0x2738259634305C14ULL, 0x61CF4F94C97DF93DULL,
|
|
||||||
0x1B6BACA2AE4E125BULL, 0x758F450C88572E0BULL, 0x959F587D507A8359ULL,
|
|
||||||
0xB063E962E045F54DULL, 0x60E8ED72C0DFF5D1ULL, 0x7B64978555326F9FULL,
|
|
||||||
0xFD080D236DA814BAULL, 0x8C90FD9B083F4558ULL, 0x106F72FE81E2C590ULL,
|
|
||||||
0x7976033A39F7D952ULL, 0xA4EC0132764CA04BULL, 0x733EA705FAE4FA77ULL,
|
|
||||||
0xB4D8F77BC3E56167ULL, 0x9E21F4F903B33FD9ULL, 0x9D765E419FB69F6DULL,
|
|
||||||
0xD30C088BA61EA5EFULL, 0x5D94337FBFAF7F5BULL, 0x1A4E4822EB4D7A59ULL,
|
|
||||||
0x6FFE73E81B637FB3ULL, 0xDDF957BC36D8B9CAULL, 0x64D0E29EEA8838B3ULL,
|
|
||||||
0x08DD9BDFD96B9F63ULL, 0x087E79E5A57D1D13ULL, 0xE328E230E3E2B3FBULL,
|
|
||||||
0x1C2559E30F0946BEULL, 0x720BF5F26F4D2EAAULL, 0xB0774D261CC609DBULL,
|
|
||||||
0x443F64EC5A371195ULL, 0x4112CF68649A260EULL, 0xD813F2FAB7F5C5CAULL,
|
|
||||||
0x660D3257380841EEULL, 0x59AC2C7873F910A3ULL, 0xE846963877671A17ULL,
|
|
||||||
0x93B633ABFA3469F8ULL, 0xC0C0F5A60EF4CDCFULL, 0xCAF21ECD4377B28CULL,
|
|
||||||
0x57277707199B8175ULL, 0x506C11B9D90E8B1DULL, 0xD83CC2687A19255FULL,
|
|
||||||
0x4A29C6465A314CD1ULL, 0xED2DF21216235097ULL, 0xB5635C95FF7296E2ULL,
|
|
||||||
0x22AF003AB672E811ULL, 0x52E762596BF68235ULL, 0x9AEBA33AC6ECC6B0ULL,
|
|
||||||
0x944F6DE09134DFB6ULL, 0x6C47BEC883A7DE39ULL, 0x6AD047C430A12104ULL,
|
|
||||||
0xA5B1CFDBA0AB4067ULL, 0x7C45D833AFF07862ULL, 0x5092EF950A16DA0BULL,
|
|
||||||
0x9338E69C052B8E7BULL, 0x455A4B4CFE30E3F5ULL, 0x6B02E63195AD0CF8ULL,
|
|
||||||
0x6B17B224BAD6BF27ULL, 0xD1E0CCD25BB9C169ULL, 0xDE0C89A556B9AE70ULL,
|
|
||||||
0x50065E535A213CF6ULL, 0x9C1169FA2777B874ULL, 0x78EDEFD694AF1EEDULL,
|
|
||||||
0x6DC93D9526A50E68ULL, 0xEE97F453F06791EDULL, 0x32AB0EDB696703D3ULL,
|
|
||||||
0x3A6853C7E70757A7ULL, 0x31865CED6120F37DULL, 0x67FEF95D92607890ULL,
|
|
||||||
0x1F2B1D1F15F6DC9CULL, 0xB69E38A8965C6B65ULL, 0xAA9119FF184CCCF4ULL,
|
|
||||||
0xF43C732873F24C13ULL, 0xFB4A3D794A9A80D2ULL, 0x3550C2321FD6109CULL,
|
|
||||||
0x371F77E76BB8417EULL, 0x6BFA9AAE5EC05779ULL, 0xCD04F3FF001A4778ULL,
|
|
||||||
0xE3273522064480CAULL, 0x9F91508BFFCFC14AULL, 0x049A7F41061A9E60ULL,
|
|
||||||
0xFCB6BE43A9F2FE9BULL, 0x08DE8A1C7797DA9BULL, 0x8F9887E6078735A1ULL,
|
|
||||||
0xB5B4071DBFC73A66ULL, 0x230E343DFBA08D33ULL, 0x43ED7F5A0FAE657DULL,
|
|
||||||
0x3A88A0FBBCB05C63ULL, 0x21874B8B4D2DBC4FULL, 0x1BDEA12E35F6A8C9ULL,
|
|
||||||
0x53C065C6C8E63528ULL, 0xE34A1D250E7A8D6BULL, 0xD6B04D3B7651DD7EULL,
|
|
||||||
0x5E90277E7CB39E2DULL, 0x2C046F22062DC67DULL, 0xB10BB459132D0A26ULL,
|
|
||||||
0x3FA9DDFB67E2F199ULL, 0x0E09B88E1914F7AFULL, 0x10E8B35AF3EEAB37ULL,
|
|
||||||
0x9EEDECA8E272B933ULL, 0xD4C718BC4AE8AE5FULL, 0x81536D601170FC20ULL,
|
|
||||||
0x91B534F885818A06ULL, 0xEC8177F83F900978ULL, 0x190E714FADA5156EULL,
|
|
||||||
0xB592BF39B0364963ULL, 0x89C350C893AE7DC1ULL, 0xAC042E70F8B383F2ULL,
|
|
||||||
0xB49B52E587A1EE60ULL, 0xFB152FE3FF26DA89ULL, 0x3E666E6F69AE2C15ULL,
|
|
||||||
0x3B544EBE544C19F9ULL, 0xE805A1E290CF2456ULL, 0x24B33C9D7ED25117ULL,
|
|
||||||
0xE74733427B72F0C1ULL, 0x0A804D18B7097475ULL, 0x57E3306D881EDB4FULL,
|
|
||||||
0x4AE7D6A36EB5DBCBULL, 0x2D8D5432157064C8ULL, 0xD1E649DE1E7F268BULL,
|
|
||||||
0x8A328A1CEDFE552CULL, 0x07A3AEC79624C7DAULL, 0x84547DDC3E203C94ULL,
|
|
||||||
0x990A98FD5071D263ULL, 0x1A4FF12616EEFC89ULL, 0xF6F7FD1431714200ULL,
|
|
||||||
0x30C05B1BA332F41CULL, 0x8D2636B81555A786ULL, 0x46C9FEB55D120902ULL,
|
|
||||||
0xCCEC0A73B49C9921ULL, 0x4E9D2827355FC492ULL, 0x19EBB029435DCB0FULL,
|
|
||||||
0x4659D2B743848A2CULL, 0x963EF2C96B33BE31ULL, 0x74F85198B05A2E7DULL,
|
|
||||||
0x5A0F544DD2B1FB18ULL, 0x03727073C2E134B1ULL, 0xC7F6AA2DE59AEA61ULL,
|
|
||||||
0x352787BAA0D7C22FULL, 0x9853EAB63B5E0B35ULL, 0xABBDCDD7ED5C0860ULL,
|
|
||||||
0xCF05DAF5AC8D77B0ULL, 0x49CAD48CEBF4A71EULL, 0x7A4C10EC2158C4A6ULL,
|
|
||||||
0xD9E92AA246BF719EULL, 0x13AE978D09FE5557ULL, 0x730499AF921549FFULL,
|
|
||||||
0x4E4B705B92903BA4ULL, 0xFF577222C14F0A3AULL, 0x55B6344CF97AAFAEULL,
|
|
||||||
0xB862225B055B6960ULL, 0xCAC09AFBDDD2CDB4ULL, 0xDAF8E9829FE96B5FULL,
|
|
||||||
0xB5FDFC5D3132C498ULL, 0x310CB380DB6F7503ULL, 0xE87FBB46217A360EULL,
|
|
||||||
0x2102AE466EBB1148ULL, 0xF8549E1A3AA5E00DULL, 0x07A69AFDCC42261AULL,
|
|
||||||
0xC4C118BFE78FEAAEULL, 0xF9F4892ED96BD438ULL, 0x1AF3DBE25D8F45DAULL,
|
|
||||||
0xF5B4B0B0D2DEEEB4ULL, 0x962ACEEFA82E1C84ULL, 0x046E3ECAAF453CE9ULL,
|
|
||||||
0xF05D129681949A4CULL, 0x964781CE734B3C84ULL, 0x9C2ED44081CE5FBDULL,
|
|
||||||
0x522E23F3925E319EULL, 0x177E00F9FC32F791ULL, 0x2BC60A63A6F3B3F2ULL,
|
|
||||||
0x222BBFAE61725606ULL, 0x486289DDCC3D6780ULL, 0x7DC7785B8EFDFC80ULL,
|
|
||||||
0x8AF38731C02BA980ULL, 0x1FAB64EA29A2DDF7ULL, 0xE4D9429322CD065AULL,
|
|
||||||
0x9DA058C67844F20CULL, 0x24C0E332B70019B0ULL, 0x233003B5A6CFE6ADULL,
|
|
||||||
0xD586BD01C5C217F6ULL, 0x5E5637885F29BC2BULL, 0x7EBA726D8C94094BULL,
|
|
||||||
0x0A56A5F0BFE39272ULL, 0xD79476A84EE20D06ULL, 0x9E4C1269BAA4BF37ULL,
|
|
||||||
0x17EFEE45B0DEE640ULL, 0x1D95B0A5FCF90BC6ULL, 0x93CBE0B699C2585DULL,
|
|
||||||
0x65FA4F227A2B6D79ULL, 0xD5F9E858292504D5ULL, 0xC2B5A03F71471A6FULL,
|
|
||||||
0x59300222B4561E00ULL, 0xCE2F8642CA0712DCULL, 0x7CA9723FBB2E8988ULL,
|
|
||||||
0x2785338347F2BA08ULL, 0xC61BB3A141E50E8CULL, 0x150F361DAB9DEC26ULL,
|
|
||||||
0x9F6A419D382595F4ULL, 0x64A53DC924FE7AC9ULL, 0x142DE49FFF7A7C3DULL,
|
|
||||||
0x0C335248857FA9E7ULL, 0x0A9C32D5EAE45305ULL, 0xE6C42178C4BBB92EULL,
|
|
||||||
0x71F1CE2490D20B07ULL, 0xF1BCC3D275AFE51AULL, 0xE728E8C83C334074ULL,
|
|
||||||
0x96FBF83A12884624ULL, 0x81A1549FD6573DA5ULL, 0x5FA7867CAF35E149ULL,
|
|
||||||
0x56986E2EF3ED091BULL, 0x917F1DD5F8886C61ULL, 0xD20D8C88C8FFE65FULL,
|
|
||||||
0x31D71DCE64B2C310ULL, 0xF165B587DF898190ULL, 0xA57E6339DD2CF3A0ULL,
|
|
||||||
0x1EF6E6DBB1961EC9ULL, 0x70CC73D90BC26E24ULL, 0xE21A6B35DF0C3AD7ULL,
|
|
||||||
0x003A93D8B2806962ULL, 0x1C99DED33CB890A1ULL, 0xCF3145DE0ADD4289ULL,
|
|
||||||
0xD0E4427A5514FB72ULL, 0x77C621CC9FB3A483ULL, 0x67A34DAC4356550BULL,
|
|
||||||
0xF8D626AAAF278509ULL
|
|
||||||
}};
|
|
||||||
|
|
||||||
// polyglot_key() returns the PolyGlot hash key of the given position
|
|
||||||
Key polyglot_key(const Position& pos) {
|
|
||||||
|
|
||||||
Key key = 0;
|
|
||||||
Bitboard b = pos.pieces();
|
|
||||||
|
|
||||||
while (b)
|
|
||||||
{
|
|
||||||
Square s = pop_lsb(&b);
|
|
||||||
Piece pc = pos.piece_on(s);
|
|
||||||
|
|
||||||
// PolyGlot pieces are: BP = 0, WP = 1, BN = 2, ... BK = 10, WK = 11
|
|
||||||
key ^= PG.Zobrist.psq[2 * (type_of(pc) - 1) + (color_of(pc) == WHITE)][s];
|
|
||||||
}
|
|
||||||
|
|
||||||
b = pos.can_castle(ANY_CASTLING);
|
|
||||||
|
|
||||||
while (b)
|
|
||||||
key ^= PG.Zobrist.castling[pop_lsb(&b)];
|
|
||||||
|
|
||||||
if (pos.ep_square() != SQ_NONE)
|
|
||||||
key ^= PG.Zobrist.enpassant[file_of(pos.ep_square())];
|
|
||||||
|
|
||||||
if (pos.side_to_move() == WHITE)
|
|
||||||
key ^= PG.Zobrist.turn;
|
|
||||||
|
|
||||||
return key;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
PolyglotBook::PolyglotBook() : rkiss(Time::now() % 10000) {}
|
|
||||||
|
|
||||||
PolyglotBook::~PolyglotBook() { if (is_open()) close(); }
|
|
||||||
|
|
||||||
|
|
||||||
/// operator>>() reads sizeof(T) chars from the file's binary byte stream and
|
|
||||||
/// converts them into a number of type T. A Polyglot book stores numbers in
|
|
||||||
/// big-endian format.
|
|
||||||
|
|
||||||
template<typename T> PolyglotBook& PolyglotBook::operator>>(T& n) {
|
|
||||||
|
|
||||||
n = 0;
|
|
||||||
for (size_t i = 0; i < sizeof(T); ++i)
|
|
||||||
n = T((n << 8) + ifstream::get());
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<> PolyglotBook& PolyglotBook::operator>>(Entry& e) {
|
|
||||||
return *this >> e.key >> e.move >> e.count >> e.learn;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// open() tries to open a book file with the given name after closing any
|
|
||||||
/// existing one.
|
|
||||||
|
|
||||||
bool PolyglotBook::open(const char* fName) {
|
|
||||||
|
|
||||||
if (is_open()) // Cannot close an already closed file
|
|
||||||
close();
|
|
||||||
|
|
||||||
ifstream::open(fName, ifstream::in | ifstream::binary);
|
|
||||||
|
|
||||||
fileName = is_open() ? fName : "";
|
|
||||||
ifstream::clear(); // Reset any error flag to allow a retry ifstream::open()
|
|
||||||
return !fileName.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// probe() tries to find a book move for the given position. If no move is
|
|
||||||
/// found, it returns MOVE_NONE. If pickBest is true, then it always returns
|
|
||||||
/// the highest-rated move, otherwise it randomly chooses one based on the
|
|
||||||
/// move score.
|
|
||||||
|
|
||||||
Move PolyglotBook::probe(const Position& pos, const string& fName, bool pickBest) {
|
|
||||||
|
|
||||||
if (fileName != fName && !open(fName.c_str()))
|
|
||||||
return MOVE_NONE;
|
|
||||||
|
|
||||||
Entry e;
|
|
||||||
uint16_t best = 0;
|
|
||||||
unsigned sum = 0;
|
|
||||||
Move move = MOVE_NONE;
|
|
||||||
Key key = polyglot_key(pos);
|
|
||||||
|
|
||||||
seekg(find_first(key) * sizeof(Entry), ios_base::beg);
|
|
||||||
|
|
||||||
while (*this >> e, e.key == key && good())
|
|
||||||
{
|
|
||||||
best = max(best, e.count);
|
|
||||||
sum += e.count;
|
|
||||||
|
|
||||||
// Choose book move according to its score. If a move has a very high
|
|
||||||
// score it has a higher probability of being choosen than a move with
|
|
||||||
// a lower score. Note that first entry is always chosen.
|
|
||||||
if ( (!pickBest && sum && rkiss.rand<unsigned>() % sum < e.count)
|
|
||||||
|| (pickBest && e.count == best))
|
|
||||||
move = Move(e.move);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!move)
|
|
||||||
return MOVE_NONE;
|
|
||||||
|
|
||||||
// A PolyGlot book move is encoded as follows:
|
|
||||||
//
|
|
||||||
// bit 0- 5: destination square (from 0 to 63)
|
|
||||||
// bit 6-11: origin square (from 0 to 63)
|
|
||||||
// bit 12-14: promotion piece (from KNIGHT == 1 to QUEEN == 4)
|
|
||||||
//
|
|
||||||
// Castling moves follow the "king captures rook" representation. If a book
|
|
||||||
// move is a promotion, we have to convert it to our representation and in
|
|
||||||
// all other cases, we can directly compare with a Move after having masked
|
|
||||||
// out the special Move flags (bit 14-15) that are not supported by PolyGlot.
|
|
||||||
int pt = (move >> 12) & 7;
|
|
||||||
if (pt)
|
|
||||||
move = make<PROMOTION>(from_sq(move), to_sq(move), PieceType(pt + 1));
|
|
||||||
|
|
||||||
// Add 'special move' flags and verify it is legal
|
|
||||||
for (MoveList<LEGAL> it(pos); *it; ++it)
|
|
||||||
if (move == (*it ^ type_of(*it)))
|
|
||||||
return *it;
|
|
||||||
|
|
||||||
return MOVE_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// find_first() takes a book key as input, and does a binary search through
|
|
||||||
/// the book file for the given key. Returns the index of the leftmost book
|
|
||||||
/// entry with the same key as the input.
|
|
||||||
|
|
||||||
size_t PolyglotBook::find_first(Key key) {
|
|
||||||
|
|
||||||
seekg(0, ios::end); // Move pointer to end, so tellg() gets file's size
|
|
||||||
|
|
||||||
size_t low = 0, mid, high = (size_t)tellg() / sizeof(Entry) - 1;
|
|
||||||
Entry e;
|
|
||||||
|
|
||||||
assert(low <= high);
|
|
||||||
|
|
||||||
while (low < high && good())
|
|
||||||
{
|
|
||||||
mid = (low + high) / 2;
|
|
||||||
|
|
||||||
assert(mid >= low && mid < high);
|
|
||||||
|
|
||||||
seekg(mid * sizeof(Entry), ios_base::beg);
|
|
||||||
*this >> e;
|
|
||||||
|
|
||||||
if (key <= e.key)
|
|
||||||
high = mid;
|
|
||||||
else
|
|
||||||
low = mid + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(low == high);
|
|
||||||
|
|
||||||
return low;
|
|
||||||
}
|
|
||||||
-45
@@ -1,45 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
|
||||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
Stockfish is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef BOOK_H_INCLUDED
|
|
||||||
#define BOOK_H_INCLUDED
|
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "position.h"
|
|
||||||
#include "rkiss.h"
|
|
||||||
|
|
||||||
class PolyglotBook : private std::ifstream {
|
|
||||||
public:
|
|
||||||
PolyglotBook();
|
|
||||||
~PolyglotBook();
|
|
||||||
Move probe(const Position& pos, const std::string& fName, bool pickBest);
|
|
||||||
|
|
||||||
private:
|
|
||||||
template<typename T> PolyglotBook& operator>>(T& n);
|
|
||||||
|
|
||||||
bool open(const char* fName);
|
|
||||||
size_t find_first(Key key);
|
|
||||||
|
|
||||||
RKISS rkiss;
|
|
||||||
std::string fileName;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // #ifndef BOOK_H_INCLUDED
|
|
||||||
+139
-150
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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 +22,6 @@
|
|||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
#include "bitcount.h"
|
|
||||||
#include "endgame.h"
|
#include "endgame.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ namespace {
|
|||||||
70, 50, 30, 20, 20, 30, 50, 70,
|
70, 50, 30, 20, 20, 30, 50, 70,
|
||||||
80, 60, 40, 30, 30, 40, 60, 80,
|
80, 60, 40, 30, 30, 40, 60, 80,
|
||||||
90, 70, 60, 50, 50, 60, 70, 90,
|
90, 70, 60, 50, 50, 60, 70, 90,
|
||||||
100, 90, 80, 70, 70, 80, 90, 100,
|
100, 90, 80, 70, 70, 80, 90, 100
|
||||||
};
|
};
|
||||||
|
|
||||||
// Table used to drive the king towards a corner square of the
|
// Table used to drive the king towards a corner square of the
|
||||||
@@ -59,9 +59,12 @@ namespace {
|
|||||||
const int PushClose[8] = { 0, 0, 100, 80, 60, 40, 20, 10 };
|
const int PushClose[8] = { 0, 0, 100, 80, 60, 40, 20, 10 };
|
||||||
const int PushAway [8] = { 0, 5, 20, 40, 60, 80, 90, 100 };
|
const int PushAway [8] = { 0, 5, 20, 40, 60, 80, 90, 100 };
|
||||||
|
|
||||||
|
// Pawn Rank based scaling factors used in KRPPKRP endgame
|
||||||
|
const int KRPPKRPScaleFactors[RANK_NB] = { 0, 9, 10, 14, 21, 44, 0, 0 };
|
||||||
|
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
bool verify_material(const Position& pos, Color c, Value npm, int num_pawns) {
|
bool verify_material(const Position& pos, Color c, Value npm, int pawnsCnt) {
|
||||||
return pos.non_pawn_material(c) == npm && pos.count<PAWN>(c) == num_pawns;
|
return pos.non_pawn_material(c) == npm && pos.count<PAWN>(c) == pawnsCnt;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -71,7 +74,7 @@ namespace {
|
|||||||
|
|
||||||
assert(pos.count<PAWN>(strongSide) == 1);
|
assert(pos.count<PAWN>(strongSide) == 1);
|
||||||
|
|
||||||
if (file_of(pos.list<PAWN>(strongSide)[0]) >= FILE_E)
|
if (file_of(pos.square<PAWN>(strongSide)) >= FILE_E)
|
||||||
sq = Square(sq ^ 7); // Mirror SQ_H1 -> SQ_A1
|
sq = Square(sq ^ 7); // Mirror SQ_H1 -> SQ_A1
|
||||||
|
|
||||||
if (strongSide == BLACK)
|
if (strongSide == BLACK)
|
||||||
@@ -96,12 +99,10 @@ namespace {
|
|||||||
string fen = sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/8/8/"
|
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";
|
+ sides[1] + char(8 - sides[1].length() + '0') + " w - - 0 10";
|
||||||
|
|
||||||
return Position(fen, false, NULL).material_key();
|
StateInfo st;
|
||||||
|
return Position().set(fen, false, &st, nullptr).material_key();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename M>
|
|
||||||
void delete_endgame(const typename M::value_type& p) { delete p.second; }
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
@@ -128,17 +129,11 @@ Endgames::Endgames() {
|
|||||||
add<KRPPKRP>("KRPPKRP");
|
add<KRPPKRP>("KRPPKRP");
|
||||||
}
|
}
|
||||||
|
|
||||||
Endgames::~Endgames() {
|
|
||||||
|
|
||||||
for_each(m1.begin(), m1.end(), delete_endgame<M1>);
|
template<EndgameType E, typename T>
|
||||||
for_each(m2.begin(), m2.end(), delete_endgame<M2>);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<EndgameType E>
|
|
||||||
void Endgames::add(const string& code) {
|
void Endgames::add(const string& code) {
|
||||||
|
map<T>()[key(code, WHITE)] = std::unique_ptr<EndgameBase<T>>(new Endgame<E>(WHITE));
|
||||||
map((Endgame<E>*)0)[key(code, WHITE)] = new Endgame<E>(WHITE);
|
map<T>()[key(code, BLACK)] = std::unique_ptr<EndgameBase<T>>(new Endgame<E>(BLACK));
|
||||||
map((Endgame<E>*)0)[key(code, BLACK)] = new Endgame<E>(BLACK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -156,19 +151,20 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
|
|||||||
if (pos.side_to_move() == weakSide && !MoveList<LEGAL>(pos).size())
|
if (pos.side_to_move() == weakSide && !MoveList<LEGAL>(pos).size())
|
||||||
return VALUE_DRAW;
|
return VALUE_DRAW;
|
||||||
|
|
||||||
Square winnerKSq = pos.king_square(strongSide);
|
Square winnerKSq = pos.square<KING>(strongSide);
|
||||||
Square loserKSq = pos.king_square(weakSide);
|
Square loserKSq = pos.square<KING>(weakSide);
|
||||||
|
|
||||||
Value result = pos.non_pawn_material(strongSide)
|
Value result = pos.non_pawn_material(strongSide)
|
||||||
+ pos.count<PAWN>(strongSide) * PawnValueEg
|
+ pos.count<PAWN>(strongSide) * PawnValueEg
|
||||||
+ PushToEdges[loserKSq]
|
+ PushToEdges[loserKSq]
|
||||||
+ PushClose[square_distance(winnerKSq, loserKSq)];
|
+ PushClose[distance(winnerKSq, loserKSq)];
|
||||||
|
|
||||||
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.bishop_pair(strongSide))
|
||(pos.count<BISHOP>(strongSide) > 1 && opposite_colors(pos.squares<BISHOP>(strongSide)[0],
|
||||||
result += VALUE_KNOWN_WIN;
|
pos.squares<BISHOP>(strongSide)[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;
|
||||||
}
|
}
|
||||||
@@ -182,9 +178,9 @@ Value Endgame<KBNK>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, strongSide, KnightValueMg + BishopValueMg, 0));
|
assert(verify_material(pos, strongSide, KnightValueMg + BishopValueMg, 0));
|
||||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||||
|
|
||||||
Square winnerKSq = pos.king_square(strongSide);
|
Square winnerKSq = pos.square<KING>(strongSide);
|
||||||
Square loserKSq = pos.king_square(weakSide);
|
Square loserKSq = pos.square<KING>(weakSide);
|
||||||
Square bishopSq = pos.list<BISHOP>(strongSide)[0];
|
Square bishopSq = pos.square<BISHOP>(strongSide);
|
||||||
|
|
||||||
// kbnk_mate_table() tries to drive toward corners A1 or H8. If we have a
|
// kbnk_mate_table() tries to drive toward corners A1 or H8. If we have a
|
||||||
// bishop that cannot reach the above squares, we flip the kings in order
|
// bishop that cannot reach the above squares, we flip the kings in order
|
||||||
@@ -196,7 +192,7 @@ Value Endgame<KBNK>::operator()(const Position& pos) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Value result = VALUE_KNOWN_WIN
|
Value result = VALUE_KNOWN_WIN
|
||||||
+ PushClose[square_distance(winnerKSq, loserKSq)]
|
+ PushClose[distance(winnerKSq, loserKSq)]
|
||||||
+ PushToCorners[loserKSq];
|
+ PushToCorners[loserKSq];
|
||||||
|
|
||||||
return strongSide == pos.side_to_move() ? result : -result;
|
return strongSide == pos.side_to_move() ? result : -result;
|
||||||
@@ -211,13 +207,13 @@ Value Endgame<KPK>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||||
|
|
||||||
// Assume strongSide is white and the pawn is on files A-D
|
// Assume strongSide is white and the pawn is on files A-D
|
||||||
Square wksq = normalize(pos, strongSide, pos.king_square(strongSide));
|
Square wksq = normalize(pos, strongSide, pos.square<KING>(strongSide));
|
||||||
Square bksq = normalize(pos, strongSide, pos.king_square(weakSide));
|
Square bksq = normalize(pos, strongSide, pos.square<KING>(weakSide));
|
||||||
Square psq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
|
Square psq = normalize(pos, strongSide, pos.square<PAWN>(strongSide));
|
||||||
|
|
||||||
Color us = strongSide == pos.side_to_move() ? WHITE : BLACK;
|
Color us = strongSide == pos.side_to_move() ? WHITE : BLACK;
|
||||||
|
|
||||||
if (!Bitbases::probe_kpk(wksq, psq, bksq, us))
|
if (!Bitbases::probe(wksq, psq, bksq, us))
|
||||||
return VALUE_DRAW;
|
return VALUE_DRAW;
|
||||||
|
|
||||||
Value result = VALUE_KNOWN_WIN + PawnValueEg + Value(rank_of(psq));
|
Value result = VALUE_KNOWN_WIN + PawnValueEg + Value(rank_of(psq));
|
||||||
@@ -236,36 +232,36 @@ Value Endgame<KRKP>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
||||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||||
|
|
||||||
Square wksq = relative_square(strongSide, pos.king_square(strongSide));
|
Square wksq = relative_square(strongSide, pos.square<KING>(strongSide));
|
||||||
Square bksq = relative_square(strongSide, pos.king_square(weakSide));
|
Square bksq = relative_square(strongSide, pos.square<KING>(weakSide));
|
||||||
Square rsq = relative_square(strongSide, pos.list<ROOK>(strongSide)[0]);
|
Square rsq = relative_square(strongSide, pos.square<ROOK>(strongSide));
|
||||||
Square psq = relative_square(strongSide, pos.list<PAWN>(weakSide)[0]);
|
Square psq = relative_square(strongSide, pos.square<PAWN>(weakSide));
|
||||||
|
|
||||||
Square queeningSq = make_square(file_of(psq), RANK_1);
|
Square queeningSq = make_square(file_of(psq), RANK_1);
|
||||||
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 (wksq < psq && file_of(wksq) == file_of(psq))
|
||||||
result = RookValueEg - square_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,
|
||||||
// it's a win.
|
// it's a win.
|
||||||
else if ( square_distance(bksq, psq) >= 3 + (pos.side_to_move() == weakSide)
|
else if ( distance(bksq, psq) >= 3 + (pos.side_to_move() == weakSide)
|
||||||
&& square_distance(bksq, rsq) >= 3)
|
&& distance(bksq, rsq) >= 3)
|
||||||
result = RookValueEg - square_distance(wksq, psq);
|
result = RookValueEg - distance(wksq, psq);
|
||||||
|
|
||||||
// If the pawn is far advanced and supported by the defending king,
|
// If the pawn is far advanced and supported by the defending king,
|
||||||
// the position is drawish
|
// the position is drawish
|
||||||
else if ( rank_of(bksq) <= RANK_3
|
else if ( rank_of(bksq) <= RANK_3
|
||||||
&& square_distance(bksq, psq) == 1
|
&& distance(bksq, psq) == 1
|
||||||
&& rank_of(wksq) >= RANK_4
|
&& rank_of(wksq) >= RANK_4
|
||||||
&& square_distance(wksq, psq) > 2 + (pos.side_to_move() == strongSide))
|
&& distance(wksq, psq) > 2 + (pos.side_to_move() == strongSide))
|
||||||
result = Value(80) - 8 * square_distance(wksq, psq);
|
result = Value(80) - 8 * distance(wksq, psq);
|
||||||
|
|
||||||
else
|
else
|
||||||
result = Value(200) - 8 * ( square_distance(wksq, psq + DELTA_S)
|
result = Value(200) - 8 * ( distance(wksq, psq + SOUTH)
|
||||||
- square_distance(bksq, psq + DELTA_S)
|
- distance(bksq, psq + SOUTH)
|
||||||
- square_distance(psq, queeningSq));
|
- distance(psq, queeningSq));
|
||||||
|
|
||||||
return strongSide == pos.side_to_move() ? result : -result;
|
return strongSide == pos.side_to_move() ? result : -result;
|
||||||
}
|
}
|
||||||
@@ -279,7 +275,7 @@ Value Endgame<KRKB>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
||||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||||
|
|
||||||
Value result = Value(PushToEdges[pos.king_square(weakSide)]);
|
Value result = Value(PushToEdges[pos.square<KING>(weakSide)]);
|
||||||
return strongSide == pos.side_to_move() ? result : -result;
|
return strongSide == pos.side_to_move() ? result : -result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -292,9 +288,9 @@ Value Endgame<KRKN>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
assert(verify_material(pos, strongSide, RookValueMg, 0));
|
||||||
assert(verify_material(pos, weakSide, KnightValueMg, 0));
|
assert(verify_material(pos, weakSide, KnightValueMg, 0));
|
||||||
|
|
||||||
Square bksq = pos.king_square(weakSide);
|
Square bksq = pos.square<KING>(weakSide);
|
||||||
Square bnsq = pos.list<KNIGHT>(weakSide)[0];
|
Square bnsq = pos.square<KNIGHT>(weakSide);
|
||||||
Value result = Value(PushToEdges[bksq] + PushAway[square_distance(bksq, bnsq)]);
|
Value result = Value(PushToEdges[bksq] + PushAway[distance(bksq, bnsq)]);
|
||||||
return strongSide == pos.side_to_move() ? result : -result;
|
return strongSide == pos.side_to_move() ? result : -result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,14 +305,14 @@ Value Endgame<KQKP>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, strongSide, QueenValueMg, 0));
|
assert(verify_material(pos, strongSide, QueenValueMg, 0));
|
||||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||||
|
|
||||||
Square winnerKSq = pos.king_square(strongSide);
|
Square winnerKSq = pos.square<KING>(strongSide);
|
||||||
Square loserKSq = pos.king_square(weakSide);
|
Square loserKSq = pos.square<KING>(weakSide);
|
||||||
Square pawnSq = pos.list<PAWN>(weakSide)[0];
|
Square pawnSq = pos.square<PAWN>(weakSide);
|
||||||
|
|
||||||
Value result = Value(PushClose[square_distance(winnerKSq, loserKSq)]);
|
Value result = Value(PushClose[distance(winnerKSq, loserKSq)]);
|
||||||
|
|
||||||
if ( relative_rank(weakSide, pawnSq) != RANK_7
|
if ( relative_rank(weakSide, pawnSq) != RANK_7
|
||||||
|| square_distance(loserKSq, pawnSq) != 1
|
|| distance(loserKSq, pawnSq) != 1
|
||||||
|| !((FileABB | FileCBB | FileFBB | FileHBB) & pawnSq))
|
|| !((FileABB | FileCBB | FileFBB | FileHBB) & pawnSq))
|
||||||
result += QueenValueEg - PawnValueEg;
|
result += QueenValueEg - PawnValueEg;
|
||||||
|
|
||||||
@@ -334,13 +330,13 @@ Value Endgame<KQKR>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, strongSide, QueenValueMg, 0));
|
assert(verify_material(pos, strongSide, QueenValueMg, 0));
|
||||||
assert(verify_material(pos, weakSide, RookValueMg, 0));
|
assert(verify_material(pos, weakSide, RookValueMg, 0));
|
||||||
|
|
||||||
Square winnerKSq = pos.king_square(strongSide);
|
Square winnerKSq = pos.square<KING>(strongSide);
|
||||||
Square loserKSq = pos.king_square(weakSide);
|
Square loserKSq = pos.square<KING>(weakSide);
|
||||||
|
|
||||||
Value result = QueenValueEg
|
Value result = QueenValueEg
|
||||||
- RookValueEg
|
- RookValueEg
|
||||||
+ PushToEdges[loserKSq]
|
+ PushToEdges[loserKSq]
|
||||||
+ PushClose[square_distance(winnerKSq, loserKSq)];
|
+ PushClose[distance(winnerKSq, loserKSq)];
|
||||||
|
|
||||||
return strongSide == pos.side_to_move() ? result : -result;
|
return strongSide == pos.side_to_move() ? result : -result;
|
||||||
}
|
}
|
||||||
@@ -364,33 +360,33 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
|
|||||||
// be detected even when the weaker side has some pawns.
|
// be detected even when the weaker side has some pawns.
|
||||||
|
|
||||||
Bitboard pawns = pos.pieces(strongSide, PAWN);
|
Bitboard pawns = pos.pieces(strongSide, PAWN);
|
||||||
File pawnFile = file_of(pos.list<PAWN>(strongSide)[0]);
|
File pawnsFile = file_of(lsb(pawns));
|
||||||
|
|
||||||
// All pawns are on a single rook file ?
|
// All pawns are on a single rook file?
|
||||||
if ( (pawnFile == FILE_A || pawnFile == FILE_H)
|
if ( (pawnsFile == FILE_A || pawnsFile == FILE_H)
|
||||||
&& !(pawns & ~file_bb(pawnFile)))
|
&& !(pawns & ~file_bb(pawnsFile)))
|
||||||
{
|
{
|
||||||
Square bishopSq = pos.list<BISHOP>(strongSide)[0];
|
Square bishopSq = pos.square<BISHOP>(strongSide);
|
||||||
Square queeningSq = relative_square(strongSide, make_square(pawnFile, RANK_8));
|
Square queeningSq = relative_square(strongSide, make_square(pawnsFile, RANK_8));
|
||||||
Square kingSq = pos.king_square(weakSide);
|
Square kingSq = pos.square<KING>(weakSide);
|
||||||
|
|
||||||
if ( opposite_colors(queeningSq, bishopSq)
|
if ( opposite_colors(queeningSq, bishopSq)
|
||||||
&& square_distance(queeningSq, kingSq) <= 1)
|
&& distance(queeningSq, kingSq) <= 1)
|
||||||
return SCALE_FACTOR_DRAW;
|
return SCALE_FACTOR_DRAW;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If all the pawns are on the same B or G file, then it's potentially a draw
|
// If all the pawns are on the same B or G file, then it's potentially a draw
|
||||||
if ( (pawnFile == FILE_B || pawnFile == FILE_G)
|
if ( (pawnsFile == FILE_B || pawnsFile == FILE_G)
|
||||||
&& !(pos.pieces(PAWN) & ~file_bb(pawnFile))
|
&& !(pos.pieces(PAWN) & ~file_bb(pawnsFile))
|
||||||
&& pos.non_pawn_material(weakSide) == 0
|
&& pos.non_pawn_material(weakSide) == 0
|
||||||
&& pos.count<PAWN>(weakSide) >= 1)
|
&& pos.count<PAWN>(weakSide) >= 1)
|
||||||
{
|
{
|
||||||
// Get weakSide pawn that is closest to the home rank
|
// Get weakSide pawn that is closest to the home rank
|
||||||
Square weakPawnSq = backmost_sq(weakSide, pos.pieces(weakSide, PAWN));
|
Square weakPawnSq = backmost_sq(weakSide, pos.pieces(weakSide, PAWN));
|
||||||
|
|
||||||
Square strongKingSq = pos.king_square(strongSide);
|
Square strongKingSq = pos.square<KING>(strongSide);
|
||||||
Square weakKingSq = pos.king_square(weakSide);
|
Square weakKingSq = pos.square<KING>(weakSide);
|
||||||
Square bishopSq = pos.list<BISHOP>(strongSide)[0];
|
Square bishopSq = pos.square<BISHOP>(strongSide);
|
||||||
|
|
||||||
// There's potential for a draw if our pawn is blocked on the 7th rank,
|
// There's potential for a draw if our pawn is blocked on the 7th rank,
|
||||||
// the bishop cannot attack it or they only have one pawn left
|
// the bishop cannot attack it or they only have one pawn left
|
||||||
@@ -398,8 +394,8 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
|
|||||||
&& (pos.pieces(strongSide, PAWN) & (weakPawnSq + pawn_push(weakSide)))
|
&& (pos.pieces(strongSide, PAWN) & (weakPawnSq + pawn_push(weakSide)))
|
||||||
&& (opposite_colors(bishopSq, weakPawnSq) || pos.count<PAWN>(strongSide) == 1))
|
&& (opposite_colors(bishopSq, weakPawnSq) || pos.count<PAWN>(strongSide) == 1))
|
||||||
{
|
{
|
||||||
int strongKingDist = square_distance(weakPawnSq, strongKingSq);
|
int strongKingDist = distance(weakPawnSq, strongKingSq);
|
||||||
int weakKingDist = square_distance(weakPawnSq, weakKingSq);
|
int weakKingDist = distance(weakPawnSq, weakKingSq);
|
||||||
|
|
||||||
// It's a draw if the weak king is on its back two ranks, within 2
|
// It's a draw if the weak king is on its back two ranks, within 2
|
||||||
// squares of the blocking pawn and the strong king is not
|
// squares of the blocking pawn and the strong king is not
|
||||||
@@ -427,11 +423,11 @@ ScaleFactor Endgame<KQKRPs>::operator()(const Position& pos) const {
|
|||||||
assert(pos.count<ROOK>(weakSide) == 1);
|
assert(pos.count<ROOK>(weakSide) == 1);
|
||||||
assert(pos.count<PAWN>(weakSide) >= 1);
|
assert(pos.count<PAWN>(weakSide) >= 1);
|
||||||
|
|
||||||
Square kingSq = pos.king_square(weakSide);
|
Square kingSq = pos.square<KING>(weakSide);
|
||||||
Square rsq = pos.list<ROOK>(weakSide)[0];
|
Square rsq = pos.square<ROOK>(weakSide);
|
||||||
|
|
||||||
if ( relative_rank(weakSide, kingSq) <= RANK_2
|
if ( relative_rank(weakSide, kingSq) <= RANK_2
|
||||||
&& relative_rank(weakSide, pos.king_square(strongSide)) >= RANK_4
|
&& relative_rank(weakSide, pos.square<KING>(strongSide)) >= RANK_4
|
||||||
&& relative_rank(weakSide, rsq) == RANK_3
|
&& relative_rank(weakSide, rsq) == RANK_3
|
||||||
&& ( pos.pieces(weakSide, PAWN)
|
&& ( pos.pieces(weakSide, PAWN)
|
||||||
& pos.attacks_from<KING>(kingSq)
|
& pos.attacks_from<KING>(kingSq)
|
||||||
@@ -455,11 +451,11 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, weakSide, RookValueMg, 0));
|
assert(verify_material(pos, weakSide, RookValueMg, 0));
|
||||||
|
|
||||||
// Assume strongSide is white and the pawn is on files A-D
|
// Assume strongSide is white and the pawn is on files A-D
|
||||||
Square wksq = normalize(pos, strongSide, pos.king_square(strongSide));
|
Square wksq = normalize(pos, strongSide, pos.square<KING>(strongSide));
|
||||||
Square bksq = normalize(pos, strongSide, pos.king_square(weakSide));
|
Square bksq = normalize(pos, strongSide, pos.square<KING>(weakSide));
|
||||||
Square wrsq = normalize(pos, strongSide, pos.list<ROOK>(strongSide)[0]);
|
Square wrsq = normalize(pos, strongSide, pos.square<ROOK>(strongSide));
|
||||||
Square wpsq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
|
Square wpsq = normalize(pos, strongSide, pos.square<PAWN>(strongSide));
|
||||||
Square brsq = normalize(pos, strongSide, pos.list<ROOK>(weakSide)[0]);
|
Square brsq = normalize(pos, strongSide, pos.square<ROOK>(weakSide));
|
||||||
|
|
||||||
File f = file_of(wpsq);
|
File f = file_of(wpsq);
|
||||||
Rank r = rank_of(wpsq);
|
Rank r = rank_of(wpsq);
|
||||||
@@ -469,7 +465,7 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
|
|||||||
// If the pawn is not too far advanced and the defending king defends the
|
// If the pawn is not too far advanced and the defending king defends the
|
||||||
// queening square, use the third-rank defence.
|
// queening square, use the third-rank defence.
|
||||||
if ( r <= RANK_5
|
if ( r <= RANK_5
|
||||||
&& square_distance(bksq, queeningSq) <= 1
|
&& distance(bksq, queeningSq) <= 1
|
||||||
&& wksq <= SQ_H5
|
&& wksq <= SQ_H5
|
||||||
&& (rank_of(brsq) == RANK_6 || (r <= RANK_3 && rank_of(wrsq) != RANK_6)))
|
&& (rank_of(brsq) == RANK_6 || (r <= RANK_3 && rank_of(wrsq) != RANK_6)))
|
||||||
return SCALE_FACTOR_DRAW;
|
return SCALE_FACTOR_DRAW;
|
||||||
@@ -477,15 +473,15 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
|
|||||||
// The defending side saves a draw by checking from behind in case the pawn
|
// The defending side saves a draw by checking from behind in case the pawn
|
||||||
// has advanced to the 6th rank with the king behind.
|
// has advanced to the 6th rank with the king behind.
|
||||||
if ( r == RANK_6
|
if ( r == RANK_6
|
||||||
&& square_distance(bksq, queeningSq) <= 1
|
&& distance(bksq, queeningSq) <= 1
|
||||||
&& rank_of(wksq) + tempo <= RANK_6
|
&& rank_of(wksq) + tempo <= RANK_6
|
||||||
&& (rank_of(brsq) == RANK_1 || (!tempo && abs(file_of(brsq) - f) >= 3)))
|
&& (rank_of(brsq) == RANK_1 || (!tempo && distance<File>(brsq, wpsq) >= 3)))
|
||||||
return SCALE_FACTOR_DRAW;
|
return SCALE_FACTOR_DRAW;
|
||||||
|
|
||||||
if ( r >= RANK_6
|
if ( r >= RANK_6
|
||||||
&& bksq == queeningSq
|
&& bksq == queeningSq
|
||||||
&& rank_of(brsq) == RANK_1
|
&& rank_of(brsq) == RANK_1
|
||||||
&& (!tempo || square_distance(wksq, wpsq) >= 2))
|
&& (!tempo || distance(wksq, wpsq) >= 2))
|
||||||
return SCALE_FACTOR_DRAW;
|
return SCALE_FACTOR_DRAW;
|
||||||
|
|
||||||
// White pawn on a7 and rook on a8 is a draw if black's king is on g7 or h7
|
// White pawn on a7 and rook on a8 is a draw if black's king is on g7 or h7
|
||||||
@@ -500,9 +496,9 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
|
|||||||
// If the defending king blocks the pawn and the attacking king is too far
|
// If the defending king blocks the pawn and the attacking king is too far
|
||||||
// away, it's a draw.
|
// away, it's a draw.
|
||||||
if ( r <= RANK_5
|
if ( r <= RANK_5
|
||||||
&& bksq == wpsq + DELTA_N
|
&& bksq == wpsq + NORTH
|
||||||
&& square_distance(wksq, wpsq) - tempo >= 2
|
&& distance(wksq, wpsq) - tempo >= 2
|
||||||
&& square_distance(wksq, brsq) - tempo >= 2)
|
&& distance(wksq, brsq) - tempo >= 2)
|
||||||
return SCALE_FACTOR_DRAW;
|
return SCALE_FACTOR_DRAW;
|
||||||
|
|
||||||
// Pawn on the 7th rank supported by the rook from behind usually wins if the
|
// Pawn on the 7th rank supported by the rook from behind usually wins if the
|
||||||
@@ -512,22 +508,22 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
|
|||||||
&& f != FILE_A
|
&& f != FILE_A
|
||||||
&& file_of(wrsq) == f
|
&& file_of(wrsq) == f
|
||||||
&& wrsq != queeningSq
|
&& wrsq != queeningSq
|
||||||
&& (square_distance(wksq, queeningSq) < square_distance(bksq, queeningSq) - 2 + tempo)
|
&& (distance(wksq, queeningSq) < distance(bksq, queeningSq) - 2 + tempo)
|
||||||
&& (square_distance(wksq, queeningSq) < square_distance(bksq, wrsq) + tempo))
|
&& (distance(wksq, queeningSq) < distance(bksq, wrsq) + tempo))
|
||||||
return ScaleFactor(SCALE_FACTOR_MAX - 2 * square_distance(wksq, queeningSq));
|
return ScaleFactor(SCALE_FACTOR_MAX - 2 * distance(wksq, queeningSq));
|
||||||
|
|
||||||
// Similar to the above, but with the pawn further back
|
// Similar to the above, but with the pawn further back
|
||||||
if ( f != FILE_A
|
if ( f != FILE_A
|
||||||
&& file_of(wrsq) == f
|
&& file_of(wrsq) == f
|
||||||
&& wrsq < wpsq
|
&& wrsq < wpsq
|
||||||
&& (square_distance(wksq, queeningSq) < square_distance(bksq, queeningSq) - 2 + tempo)
|
&& (distance(wksq, queeningSq) < distance(bksq, queeningSq) - 2 + tempo)
|
||||||
&& (square_distance(wksq, wpsq + DELTA_N) < square_distance(bksq, wpsq + DELTA_N) - 2 + tempo)
|
&& (distance(wksq, wpsq + NORTH) < distance(bksq, wpsq + NORTH) - 2 + tempo)
|
||||||
&& ( square_distance(bksq, wrsq) + tempo >= 3
|
&& ( distance(bksq, wrsq) + tempo >= 3
|
||||||
|| ( square_distance(wksq, queeningSq) < square_distance(bksq, wrsq) + tempo
|
|| ( distance(wksq, queeningSq) < distance(bksq, wrsq) + tempo
|
||||||
&& (square_distance(wksq, wpsq + DELTA_N) < square_distance(bksq, wrsq) + tempo))))
|
&& (distance(wksq, wpsq + NORTH) < distance(bksq, wrsq) + tempo))))
|
||||||
return ScaleFactor( SCALE_FACTOR_MAX
|
return ScaleFactor( SCALE_FACTOR_MAX
|
||||||
- 8 * square_distance(wpsq, queeningSq)
|
- 8 * distance(wpsq, queeningSq)
|
||||||
- 2 * square_distance(wksq, queeningSq));
|
- 2 * distance(wksq, queeningSq));
|
||||||
|
|
||||||
// If the pawn is not far advanced and the defending king is somewhere in
|
// If the pawn is not far advanced and the defending king is somewhere in
|
||||||
// the pawn's path, it's probably a draw.
|
// the pawn's path, it's probably a draw.
|
||||||
@@ -535,9 +531,9 @@ ScaleFactor Endgame<KRPKR>::operator()(const Position& pos) const {
|
|||||||
{
|
{
|
||||||
if (file_of(bksq) == file_of(wpsq))
|
if (file_of(bksq) == file_of(wpsq))
|
||||||
return ScaleFactor(10);
|
return ScaleFactor(10);
|
||||||
if ( abs(file_of(bksq) - file_of(wpsq)) == 1
|
if ( distance<File>(bksq, wpsq) == 1
|
||||||
&& square_distance(wksq, bksq) > 2)
|
&& distance(wksq, bksq) > 2)
|
||||||
return ScaleFactor(24 - 2 * square_distance(wksq, bksq));
|
return ScaleFactor(24 - 2 * distance(wksq, bksq));
|
||||||
}
|
}
|
||||||
return SCALE_FACTOR_NONE;
|
return SCALE_FACTOR_NONE;
|
||||||
}
|
}
|
||||||
@@ -551,9 +547,9 @@ ScaleFactor Endgame<KRPKB>::operator()(const Position& pos) const {
|
|||||||
// Test for a rook pawn
|
// Test for a rook pawn
|
||||||
if (pos.pieces(PAWN) & (FileABB | FileHBB))
|
if (pos.pieces(PAWN) & (FileABB | FileHBB))
|
||||||
{
|
{
|
||||||
Square ksq = pos.king_square(weakSide);
|
Square ksq = pos.square<KING>(weakSide);
|
||||||
Square bsq = pos.list<BISHOP>(weakSide)[0];
|
Square bsq = pos.square<BISHOP>(weakSide);
|
||||||
Square psq = pos.list<PAWN>(strongSide)[0];
|
Square psq = pos.square<PAWN>(strongSide);
|
||||||
Rank rk = relative_rank(strongSide, psq);
|
Rank rk = relative_rank(strongSide, psq);
|
||||||
Square push = pawn_push(strongSide);
|
Square push = pawn_push(strongSide);
|
||||||
|
|
||||||
@@ -564,9 +560,9 @@ ScaleFactor Endgame<KRPKB>::operator()(const Position& pos) const {
|
|||||||
// corner but not trapped there.
|
// corner but not trapped there.
|
||||||
if (rk == RANK_5 && !opposite_colors(bsq, psq))
|
if (rk == RANK_5 && !opposite_colors(bsq, psq))
|
||||||
{
|
{
|
||||||
int d = square_distance(psq + 3 * push, ksq);
|
int d = distance(psq + 3 * push, ksq);
|
||||||
|
|
||||||
if (d <= 2 && !(d == 0 && ksq == pos.king_square(strongSide) + 2 * push))
|
if (d <= 2 && !(d == 0 && ksq == pos.square<KING>(strongSide) + 2 * push))
|
||||||
return ScaleFactor(24);
|
return ScaleFactor(24);
|
||||||
else
|
else
|
||||||
return ScaleFactor(48);
|
return ScaleFactor(48);
|
||||||
@@ -577,9 +573,9 @@ ScaleFactor Endgame<KRPKB>::operator()(const Position& pos) const {
|
|||||||
// pawn from a reasonable distance and the defending king is near
|
// pawn from a reasonable distance and the defending king is near
|
||||||
// the corner
|
// the corner
|
||||||
if ( rk == RANK_6
|
if ( rk == RANK_6
|
||||||
&& square_distance(psq + 2 * push, ksq) <= 1
|
&& distance(psq + 2 * push, ksq) <= 1
|
||||||
&& (PseudoAttacks[BISHOP][bsq] & (psq + push))
|
&& (PseudoAttacks[BISHOP][bsq] & (psq + push))
|
||||||
&& file_distance(bsq, psq) >= 2)
|
&& distance<File>(bsq, psq) >= 2)
|
||||||
return ScaleFactor(8);
|
return ScaleFactor(8);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -594,9 +590,9 @@ ScaleFactor Endgame<KRPPKRP>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, strongSide, RookValueMg, 2));
|
assert(verify_material(pos, strongSide, RookValueMg, 2));
|
||||||
assert(verify_material(pos, weakSide, RookValueMg, 1));
|
assert(verify_material(pos, weakSide, RookValueMg, 1));
|
||||||
|
|
||||||
Square wpsq1 = pos.list<PAWN>(strongSide)[0];
|
Square wpsq1 = pos.squares<PAWN>(strongSide)[0];
|
||||||
Square wpsq2 = pos.list<PAWN>(strongSide)[1];
|
Square wpsq2 = pos.squares<PAWN>(strongSide)[1];
|
||||||
Square bksq = pos.king_square(weakSide);
|
Square bksq = pos.square<KING>(weakSide);
|
||||||
|
|
||||||
// Does the stronger side have a passed pawn?
|
// Does the stronger side have a passed pawn?
|
||||||
if (pos.pawn_passed(strongSide, wpsq1) || pos.pawn_passed(strongSide, wpsq2))
|
if (pos.pawn_passed(strongSide, wpsq1) || pos.pawn_passed(strongSide, wpsq2))
|
||||||
@@ -604,18 +600,12 @@ ScaleFactor Endgame<KRPPKRP>::operator()(const Position& pos) const {
|
|||||||
|
|
||||||
Rank r = std::max(relative_rank(strongSide, wpsq1), relative_rank(strongSide, wpsq2));
|
Rank r = std::max(relative_rank(strongSide, wpsq1), relative_rank(strongSide, wpsq2));
|
||||||
|
|
||||||
if ( file_distance(bksq, wpsq1) <= 1
|
if ( distance<File>(bksq, wpsq1) <= 1
|
||||||
&& file_distance(bksq, wpsq2) <= 1
|
&& distance<File>(bksq, wpsq2) <= 1
|
||||||
&& relative_rank(strongSide, bksq) > r)
|
&& relative_rank(strongSide, bksq) > r)
|
||||||
{
|
{
|
||||||
switch (r) {
|
assert(r > RANK_1 && r < RANK_7);
|
||||||
case RANK_2: return ScaleFactor(10);
|
return ScaleFactor(KRPPKRPScaleFactors[r]);
|
||||||
case RANK_3: return ScaleFactor(10);
|
|
||||||
case RANK_4: return ScaleFactor(15);
|
|
||||||
case RANK_5: return ScaleFactor(20);
|
|
||||||
case RANK_6: return ScaleFactor(40);
|
|
||||||
default: assert(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return SCALE_FACTOR_NONE;
|
return SCALE_FACTOR_NONE;
|
||||||
}
|
}
|
||||||
@@ -630,15 +620,14 @@ ScaleFactor Endgame<KPsK>::operator()(const Position& pos) const {
|
|||||||
assert(pos.count<PAWN>(strongSide) >= 2);
|
assert(pos.count<PAWN>(strongSide) >= 2);
|
||||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||||
|
|
||||||
Square ksq = pos.king_square(weakSide);
|
Square ksq = pos.square<KING>(weakSide);
|
||||||
Bitboard pawns = pos.pieces(strongSide, PAWN);
|
Bitboard pawns = pos.pieces(strongSide, PAWN);
|
||||||
Square psq = pos.list<PAWN>(strongSide)[0];
|
|
||||||
|
|
||||||
// If all pawns are ahead of the king, on a single rook file and
|
// 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 & ~in_front_bb(weakSide, rank_of(ksq)))
|
||||||
&& !((pawns & ~FileABB) && (pawns & ~FileHBB))
|
&& !((pawns & ~FileABB) && (pawns & ~FileHBB))
|
||||||
&& file_distance(ksq, psq) <= 1)
|
&& distance<File>(ksq, lsb(pawns)) <= 1)
|
||||||
return SCALE_FACTOR_DRAW;
|
return SCALE_FACTOR_DRAW;
|
||||||
|
|
||||||
return SCALE_FACTOR_NONE;
|
return SCALE_FACTOR_NONE;
|
||||||
@@ -655,10 +644,10 @@ ScaleFactor Endgame<KBPKB>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, strongSide, BishopValueMg, 1));
|
assert(verify_material(pos, strongSide, BishopValueMg, 1));
|
||||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||||
|
|
||||||
Square pawnSq = pos.list<PAWN>(strongSide)[0];
|
Square pawnSq = pos.square<PAWN>(strongSide);
|
||||||
Square strongBishopSq = pos.list<BISHOP>(strongSide)[0];
|
Square strongBishopSq = pos.square<BISHOP>(strongSide);
|
||||||
Square weakBishopSq = pos.list<BISHOP>(weakSide)[0];
|
Square weakBishopSq = pos.square<BISHOP>(weakSide);
|
||||||
Square weakKingSq = pos.king_square(weakSide);
|
Square weakKingSq = pos.square<KING>(weakSide);
|
||||||
|
|
||||||
// Case 1: Defending king blocks the pawn, and cannot be driven away
|
// Case 1: Defending king blocks the pawn, and cannot be driven away
|
||||||
if ( file_of(weakKingSq) == file_of(pawnSq)
|
if ( file_of(weakKingSq) == file_of(pawnSq)
|
||||||
@@ -690,7 +679,7 @@ ScaleFactor Endgame<KBPKB>::operator()(const Position& pos) const {
|
|||||||
return SCALE_FACTOR_DRAW;
|
return SCALE_FACTOR_DRAW;
|
||||||
|
|
||||||
if ( (pos.attacks_from<BISHOP>(weakBishopSq) & path)
|
if ( (pos.attacks_from<BISHOP>(weakBishopSq) & path)
|
||||||
&& square_distance(weakBishopSq, pawnSq) >= 3)
|
&& distance(weakBishopSq, pawnSq) >= 3)
|
||||||
return SCALE_FACTOR_DRAW;
|
return SCALE_FACTOR_DRAW;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -705,15 +694,15 @@ ScaleFactor Endgame<KBPPKB>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, strongSide, BishopValueMg, 2));
|
assert(verify_material(pos, strongSide, BishopValueMg, 2));
|
||||||
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
assert(verify_material(pos, weakSide, BishopValueMg, 0));
|
||||||
|
|
||||||
Square wbsq = pos.list<BISHOP>(strongSide)[0];
|
Square wbsq = pos.square<BISHOP>(strongSide);
|
||||||
Square bbsq = pos.list<BISHOP>(weakSide)[0];
|
Square bbsq = pos.square<BISHOP>(weakSide);
|
||||||
|
|
||||||
if (!opposite_colors(wbsq, bbsq))
|
if (!opposite_colors(wbsq, bbsq))
|
||||||
return SCALE_FACTOR_NONE;
|
return SCALE_FACTOR_NONE;
|
||||||
|
|
||||||
Square ksq = pos.king_square(weakSide);
|
Square ksq = pos.square<KING>(weakSide);
|
||||||
Square psq1 = pos.list<PAWN>(strongSide)[0];
|
Square psq1 = pos.squares<PAWN>(strongSide)[0];
|
||||||
Square psq2 = pos.list<PAWN>(strongSide)[1];
|
Square psq2 = pos.squares<PAWN>(strongSide)[1];
|
||||||
Rank r1 = rank_of(psq1);
|
Rank r1 = rank_of(psq1);
|
||||||
Rank r2 = rank_of(psq2);
|
Rank r2 = rank_of(psq2);
|
||||||
Square blockSq1, blockSq2;
|
Square blockSq1, blockSq2;
|
||||||
@@ -729,7 +718,7 @@ ScaleFactor Endgame<KBPPKB>::operator()(const Position& pos) const {
|
|||||||
blockSq2 = make_square(file_of(psq1), rank_of(psq2));
|
blockSq2 = make_square(file_of(psq1), rank_of(psq2));
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (file_distance(psq1, psq2))
|
switch (distance<File>(psq1, psq2))
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
// Both pawns are on the same file. It's an easy draw if the defender firmly
|
// Both pawns are on the same file. It's an easy draw if the defender firmly
|
||||||
@@ -749,7 +738,7 @@ ScaleFactor Endgame<KBPPKB>::operator()(const Position& pos) const {
|
|||||||
&& opposite_colors(ksq, wbsq)
|
&& opposite_colors(ksq, wbsq)
|
||||||
&& ( bbsq == blockSq2
|
&& ( bbsq == blockSq2
|
||||||
|| (pos.attacks_from<BISHOP>(blockSq2) & pos.pieces(weakSide, BISHOP))
|
|| (pos.attacks_from<BISHOP>(blockSq2) & pos.pieces(weakSide, BISHOP))
|
||||||
|| abs(r1 - r2) >= 2))
|
|| distance(r1, r2) >= 2))
|
||||||
return SCALE_FACTOR_DRAW;
|
return SCALE_FACTOR_DRAW;
|
||||||
|
|
||||||
else if ( ksq == blockSq2
|
else if ( ksq == blockSq2
|
||||||
@@ -776,9 +765,9 @@ ScaleFactor Endgame<KBPKN>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, strongSide, BishopValueMg, 1));
|
assert(verify_material(pos, strongSide, BishopValueMg, 1));
|
||||||
assert(verify_material(pos, weakSide, KnightValueMg, 0));
|
assert(verify_material(pos, weakSide, KnightValueMg, 0));
|
||||||
|
|
||||||
Square pawnSq = pos.list<PAWN>(strongSide)[0];
|
Square pawnSq = pos.square<PAWN>(strongSide);
|
||||||
Square strongBishopSq = pos.list<BISHOP>(strongSide)[0];
|
Square strongBishopSq = pos.square<BISHOP>(strongSide);
|
||||||
Square weakKingSq = pos.king_square(weakSide);
|
Square weakKingSq = pos.square<KING>(weakSide);
|
||||||
|
|
||||||
if ( file_of(weakKingSq) == file_of(pawnSq)
|
if ( file_of(weakKingSq) == file_of(pawnSq)
|
||||||
&& relative_rank(strongSide, pawnSq) < relative_rank(strongSide, weakKingSq)
|
&& relative_rank(strongSide, pawnSq) < relative_rank(strongSide, weakKingSq)
|
||||||
@@ -799,10 +788,10 @@ ScaleFactor Endgame<KNPK>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
assert(verify_material(pos, weakSide, VALUE_ZERO, 0));
|
||||||
|
|
||||||
// Assume strongSide is white and the pawn is on files A-D
|
// Assume strongSide is white and the pawn is on files A-D
|
||||||
Square pawnSq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
|
Square pawnSq = normalize(pos, strongSide, pos.square<PAWN>(strongSide));
|
||||||
Square weakKingSq = normalize(pos, strongSide, pos.king_square(weakSide));
|
Square weakKingSq = normalize(pos, strongSide, pos.square<KING>(weakSide));
|
||||||
|
|
||||||
if (pawnSq == SQ_A7 && square_distance(SQ_A8, weakKingSq) <= 1)
|
if (pawnSq == SQ_A7 && distance(SQ_A8, weakKingSq) <= 1)
|
||||||
return SCALE_FACTOR_DRAW;
|
return SCALE_FACTOR_DRAW;
|
||||||
|
|
||||||
return SCALE_FACTOR_NONE;
|
return SCALE_FACTOR_NONE;
|
||||||
@@ -814,14 +803,14 @@ ScaleFactor Endgame<KNPK>::operator()(const Position& pos) const {
|
|||||||
template<>
|
template<>
|
||||||
ScaleFactor Endgame<KNPKB>::operator()(const Position& pos) const {
|
ScaleFactor Endgame<KNPKB>::operator()(const Position& pos) const {
|
||||||
|
|
||||||
Square pawnSq = pos.list<PAWN>(strongSide)[0];
|
Square pawnSq = pos.square<PAWN>(strongSide);
|
||||||
Square bishopSq = pos.list<BISHOP>(weakSide)[0];
|
Square bishopSq = pos.square<BISHOP>(weakSide);
|
||||||
Square weakKingSq = pos.king_square(weakSide);
|
Square weakKingSq = pos.square<KING>(weakSide);
|
||||||
|
|
||||||
// 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_bb(strongSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
|
||||||
return ScaleFactor(square_distance(weakKingSq, pawnSq));
|
return ScaleFactor(distance(weakKingSq, pawnSq));
|
||||||
|
|
||||||
return SCALE_FACTOR_NONE;
|
return SCALE_FACTOR_NONE;
|
||||||
}
|
}
|
||||||
@@ -839,9 +828,9 @@ ScaleFactor Endgame<KPKP>::operator()(const Position& pos) const {
|
|||||||
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
assert(verify_material(pos, weakSide, VALUE_ZERO, 1));
|
||||||
|
|
||||||
// Assume strongSide is white and the pawn is on files A-D
|
// Assume strongSide is white and the pawn is on files A-D
|
||||||
Square wksq = normalize(pos, strongSide, pos.king_square(strongSide));
|
Square wksq = normalize(pos, strongSide, pos.square<KING>(strongSide));
|
||||||
Square bksq = normalize(pos, strongSide, pos.king_square(weakSide));
|
Square bksq = normalize(pos, strongSide, pos.square<KING>(weakSide));
|
||||||
Square psq = normalize(pos, strongSide, pos.list<PAWN>(strongSide)[0]);
|
Square psq = normalize(pos, strongSide, pos.square<PAWN>(strongSide));
|
||||||
|
|
||||||
Color us = strongSide == pos.side_to_move() ? WHITE : BLACK;
|
Color us = strongSide == pos.side_to_move() ? WHITE : BLACK;
|
||||||
|
|
||||||
@@ -852,5 +841,5 @@ ScaleFactor Endgame<KPKP>::operator()(const Position& pos) const {
|
|||||||
|
|
||||||
// Probe the KPK bitbase with the weakest side's pawn removed. If it's a draw,
|
// Probe the KPK bitbase with the weakest side's pawn removed. If it's a draw,
|
||||||
// it's probably at least a draw even with the pawn.
|
// it's probably at least a draw even with the pawn.
|
||||||
return Bitbases::probe_kpk(wksq, psq, bksq, us) ? SCALE_FACTOR_NONE : SCALE_FACTOR_DRAW;
|
return Bitbases::probe(wksq, psq, bksq, us) ? SCALE_FACTOR_NONE : SCALE_FACTOR_DRAW;
|
||||||
}
|
}
|
||||||
|
|||||||
+28
-24
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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 +22,10 @@
|
|||||||
#define ENDGAME_H_INCLUDED
|
#define ENDGAME_H_INCLUDED
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
@@ -45,7 +49,7 @@ enum EndgameType {
|
|||||||
|
|
||||||
|
|
||||||
// Scaling functions
|
// Scaling functions
|
||||||
SCALE_FUNS,
|
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
|
||||||
@@ -63,11 +67,9 @@ 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. Type eg_fun<int>::type returns either ScaleFactor
|
/// Value or a ScaleFactor.
|
||||||
/// or Value depending on whether the template parameter is 0 or 1.
|
template<EndgameType E> using
|
||||||
|
eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type;
|
||||||
template<int> struct eg_fun { typedef Value type; };
|
|
||||||
template<> struct eg_fun<1> { typedef ScaleFactor type; };
|
|
||||||
|
|
||||||
|
|
||||||
/// Base and derived templates for endgame evaluation and scaling functions
|
/// Base and derived templates for endgame evaluation and scaling functions
|
||||||
@@ -75,47 +77,49 @@ template<> struct eg_fun<1> { typedef ScaleFactor type; };
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
struct EndgameBase {
|
struct EndgameBase {
|
||||||
|
|
||||||
virtual ~EndgameBase() {}
|
virtual ~EndgameBase() = default;
|
||||||
virtual Color color() const = 0;
|
virtual Color strong_side() const = 0;
|
||||||
virtual T operator()(const Position&) const = 0;
|
virtual T operator()(const Position&) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<EndgameType E, typename T = typename eg_fun<(E > SCALE_FUNS)>::type>
|
template<EndgameType 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) : strongSide(c), weakSide(~c) {}
|
||||||
Color color() const { return strongSide; }
|
Color strong_side() const { return strongSide; }
|
||||||
T operator()(const Position&) const;
|
T operator()(const Position&) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const Color strongSide, weakSide;
|
Color strongSide, weakSide;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// The Endgames class stores the pointers to endgame evaluation and scaling
|
/// The Endgames class stores the pointers to endgame evaluation and scaling
|
||||||
/// base objects in two std::map typedefs. We then use polymorphism to invoke
|
/// base objects in two std::map. We use polymorphism to invoke the actual
|
||||||
/// the actual endgame function by calling its virtual operator().
|
/// endgame function by calling its virtual operator().
|
||||||
|
|
||||||
class Endgames {
|
class Endgames {
|
||||||
|
|
||||||
typedef std::map<Key, EndgameBase<eg_fun<0>::type>*> M1;
|
template<typename T> using Map = std::map<Key, std::unique_ptr<EndgameBase<T>>>;
|
||||||
typedef std::map<Key, EndgameBase<eg_fun<1>::type>*> M2;
|
|
||||||
|
|
||||||
M1 m1;
|
template<EndgameType E, typename T = eg_type<E>>
|
||||||
M2 m2;
|
void add(const std::string& code);
|
||||||
|
|
||||||
M1& map(M1::mapped_type) { return m1; }
|
template<typename T>
|
||||||
M2& map(M2::mapped_type) { return m2; }
|
Map<T>& map() {
|
||||||
|
return std::get<std::is_same<T, ScaleFactor>::value>(maps);
|
||||||
|
}
|
||||||
|
|
||||||
template<EndgameType E> void add(const std::string& code);
|
std::pair<Map<Value>, Map<ScaleFactor>> maps;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Endgames();
|
Endgames();
|
||||||
~Endgames();
|
|
||||||
|
|
||||||
template<typename T> T probe(Key key, T& eg)
|
template<typename T>
|
||||||
{ return eg = map(eg).count(key) ? map(eg)[key] : NULL; }
|
EndgameBase<T>* probe(Key key) {
|
||||||
|
return map<T>().count(key) ? map<T>()[key].get() : nullptr;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // #ifndef ENDGAME_H_INCLUDED
|
#endif // #ifndef ENDGAME_H_INCLUDED
|
||||||
|
|||||||
+598
-591
File diff suppressed because it is too large
Load Diff
+9
-4
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,16 +21,20 @@
|
|||||||
#ifndef EVALUATE_H_INCLUDED
|
#ifndef EVALUATE_H_INCLUDED
|
||||||
#define EVALUATE_H_INCLUDED
|
#define EVALUATE_H_INCLUDED
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
class Position;
|
class Position;
|
||||||
|
|
||||||
namespace Eval {
|
namespace Eval {
|
||||||
|
|
||||||
extern void init();
|
const Value Tempo = Value(20); // Must be visible to search
|
||||||
extern Value evaluate(const Position& pos);
|
|
||||||
extern std::string trace(const Position& pos);
|
|
||||||
|
|
||||||
|
std::string trace(const Position& pos);
|
||||||
|
|
||||||
|
template<bool DoTrace = false>
|
||||||
|
Value evaluate(const Position& pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // #ifndef EVALUATE_H_INCLUDED
|
#endif // #ifndef EVALUATE_H_INCLUDED
|
||||||
|
|||||||
+12
-5
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,28 +21,34 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
#include "evaluate.h"
|
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
#include "ucioption.h"
|
#include "uci.h"
|
||||||
|
#include "syzygy/tbprobe.h"
|
||||||
|
|
||||||
|
namespace PSQT {
|
||||||
|
void init();
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
std::cout << engine_info() << std::endl;
|
std::cout << engine_info() << std::endl;
|
||||||
|
|
||||||
UCI::init(Options);
|
UCI::init(Options);
|
||||||
|
PSQT::init();
|
||||||
Bitboards::init();
|
Bitboards::init();
|
||||||
Position::init();
|
Position::init();
|
||||||
Bitbases::init_kpk();
|
Bitbases::init();
|
||||||
Search::init();
|
Search::init();
|
||||||
Pawns::init();
|
Pawns::init();
|
||||||
Eval::init();
|
|
||||||
Threads.init();
|
Threads.init();
|
||||||
|
Tablebases::init(Options["SyzygyPath"]);
|
||||||
TT.resize(Options["Hash"]);
|
TT.resize(Options["Hash"]);
|
||||||
|
|
||||||
UCI::loop(argc, argv);
|
UCI::loop(argc, argv);
|
||||||
|
|
||||||
Threads.exit();
|
Threads.exit();
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+83
-123
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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
|
||||||
@@ -17,189 +18,169 @@
|
|||||||
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 <algorithm> // For std::min
|
#include <algorithm> // For std::min
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstring>
|
#include <cstring> // For std::memset
|
||||||
|
|
||||||
#include "material.h"
|
#include "material.h"
|
||||||
|
#include "thread.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
// Polynomial material balance parameters
|
// Polynomial material imbalance parameters
|
||||||
|
|
||||||
// pair pawn knight bishop rook queen
|
const int QuadraticOurs[][PIECE_TYPE_NB] = {
|
||||||
const int LinearCoefficients[6] = { 1852, -162, -1122, -183, 249, -154 };
|
|
||||||
|
|
||||||
const int QuadraticCoefficientsSameSide[][PIECE_TYPE_NB] = {
|
|
||||||
// OUR PIECES
|
// OUR PIECES
|
||||||
// pair pawn knight bishop rook queen
|
// pair pawn knight bishop rook queen
|
||||||
{ 0 }, // Bishop pair
|
{1667 }, // Bishop pair
|
||||||
{ 39, 2 }, // Pawn
|
{ 40, 2 }, // Pawn
|
||||||
{ 35, 271, -4 }, // knight OUR PIECES
|
{ 32, 255, -3 }, // Knight OUR PIECES
|
||||||
{ 0, 105, 4, 0 }, // Bishop
|
{ 0, 104, 4, 0 }, // Bishop
|
||||||
{ -27, -2, 46, 100, -141 }, // Rook
|
{ -26, -2, 47, 105, -149 }, // Rook
|
||||||
{-177, 25, 129, 142, -137, 0 } // Queen
|
{-185, 24, 122, 137, -134, 0 } // Queen
|
||||||
};
|
};
|
||||||
|
|
||||||
const int QuadraticCoefficientsOppositeSide[][PIECE_TYPE_NB] = {
|
const 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
|
||||||
{ 37, 0 }, // Pawn
|
{ 36, 0 }, // Pawn
|
||||||
{ 10, 62, 0 }, // Knight OUR PIECES
|
{ 9, 63, 0 }, // Knight OUR PIECES
|
||||||
{ 57, 64, 39, 0 }, // Bishop
|
{ 59, 65, 42, 0 }, // Bishop
|
||||||
{ 50, 40, 23, -22, 0 }, // Rook
|
{ 46, 39, 24, -24, 0 }, // Rook
|
||||||
{ 98, 105, -39, 141, 274, 0 } // Queen
|
{ 101, 100, -37, 141, 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
|
||||||
// the function maps because they correspond to more than one material hash key.
|
// the function maps because they correspond to more than one material hash key.
|
||||||
Endgame<KXK> EvaluateKXK[] = { Endgame<KXK>(WHITE), Endgame<KXK>(BLACK) };
|
Endgame<KXK> EvaluateKXK[] = { Endgame<KXK>(WHITE), Endgame<KXK>(BLACK) };
|
||||||
|
|
||||||
Endgame<KBPsK> ScaleKBPsK[] = { Endgame<KBPsK>(WHITE), Endgame<KBPsK>(BLACK) };
|
Endgame<KBPsK> ScaleKBPsK[] = { Endgame<KBPsK>(WHITE), Endgame<KBPsK>(BLACK) };
|
||||||
Endgame<KQKRPs> ScaleKQKRPs[] = { Endgame<KQKRPs>(WHITE), Endgame<KQKRPs>(BLACK) };
|
Endgame<KQKRPs> ScaleKQKRPs[] = { Endgame<KQKRPs>(WHITE), Endgame<KQKRPs>(BLACK) };
|
||||||
Endgame<KPsK> ScaleKPsK[] = { Endgame<KPsK>(WHITE), Endgame<KPsK>(BLACK) };
|
Endgame<KPsK> ScaleKPsK[] = { Endgame<KPsK>(WHITE), Endgame<KPsK>(BLACK) };
|
||||||
Endgame<KPKP> ScaleKPKP[] = { Endgame<KPKP>(WHITE), Endgame<KPKP>(BLACK) };
|
Endgame<KPKP> ScaleKPKP[] = { Endgame<KPKP>(WHITE), Endgame<KPKP>(BLACK) };
|
||||||
|
|
||||||
// Helper templates used to detect a given material distribution
|
// Helper used to detect a given material distribution
|
||||||
template<Color Us> bool is_KXK(const Position& pos) {
|
bool is_KXK(const Position& pos, Color us) {
|
||||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
return !more_than_one(pos.pieces(~us))
|
||||||
return !pos.count<PAWN>(Them)
|
&& pos.non_pawn_material(us) >= RookValueMg;
|
||||||
&& pos.non_pawn_material(Them) == VALUE_ZERO
|
|
||||||
&& pos.non_pawn_material(Us) >= RookValueMg;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<Color Us> bool is_KBPsKs(const Position& pos) {
|
bool is_KBPsKs(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<Color Us> bool is_KQKRPs(const Position& pos) {
|
bool is_KQKRPs(const Position& pos, Color us) {
|
||||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
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>(Them) == 1
|
&& pos.count<PAWN>(~us) >= 1;
|
||||||
&& pos.count<PAWN>(Them) >= 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// imbalance() calculates the imbalance by comparing the piece count of each
|
/// imbalance() calculates the imbalance by comparing the piece count of each
|
||||||
/// piece type for both colors.
|
/// piece type for both colors.
|
||||||
|
|
||||||
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);
|
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||||
|
|
||||||
int pt1, pt2, pc, v;
|
int bonus = 0;
|
||||||
int value = 0;
|
|
||||||
|
|
||||||
// Second-degree polynomial material imbalance by Tord Romstad
|
// Second-degree polynomial material imbalance by Tord Romstad
|
||||||
for (pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1)
|
for (int pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1)
|
||||||
{
|
{
|
||||||
pc = pieceCount[Us][pt1];
|
if (!pieceCount[Us][pt1])
|
||||||
if (!pc)
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
v = LinearCoefficients[pt1];
|
int v = 0;
|
||||||
|
|
||||||
for (pt2 = NO_PIECE_TYPE; pt2 <= pt1; ++pt2)
|
for (int pt2 = NO_PIECE_TYPE; pt2 <= pt1; ++pt2)
|
||||||
v += QuadraticCoefficientsSameSide[pt1][pt2] * pieceCount[Us][pt2]
|
v += QuadraticOurs[pt1][pt2] * pieceCount[Us][pt2]
|
||||||
+ QuadraticCoefficientsOppositeSide[pt1][pt2] * pieceCount[Them][pt2];
|
+ QuadraticTheirs[pt1][pt2] * pieceCount[Them][pt2];
|
||||||
|
|
||||||
value += pc * v;
|
bonus += pieceCount[Us][pt1] * v;
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return bonus;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace Material {
|
namespace Material {
|
||||||
|
|
||||||
/// Material::probe() takes a position object as input, looks up a MaterialEntry
|
/// Material::probe() looks up the current position's material configuration in
|
||||||
/// object, and returns a pointer to it. If the material configuration is not
|
/// the material hash table. It returns a pointer to the Entry if the position
|
||||||
/// already present in the table, it is computed and stored there, so we don't
|
/// is found. Otherwise a new Entry is computed and stored there, so we don't
|
||||||
/// have to recompute everything when the same material configuration occurs again.
|
/// have to recompute all when the same material configuration occurs again.
|
||||||
|
|
||||||
Entry* probe(const Position& pos, Table& entries, Endgames& endgames) {
|
Entry* probe(const Position& pos) {
|
||||||
|
|
||||||
Key key = pos.material_key();
|
Key key = pos.material_key();
|
||||||
Entry* e = entries[key];
|
Entry* e = pos.this_thread()->materialTable[key];
|
||||||
|
|
||||||
// If e->key matches the position's material hash key, it means that we
|
|
||||||
// have analysed this material configuration before, and we can simply
|
|
||||||
// return the information we found the last time instead of recomputing it.
|
|
||||||
if (e->key == key)
|
if (e->key == key)
|
||||||
return e;
|
return e;
|
||||||
|
|
||||||
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 = game_phase(pos);
|
e->gamePhase = pos.game_phase();
|
||||||
|
|
||||||
// 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
|
||||||
// for a generic one if the previous search failed.
|
// for a generic one if the previous search failed.
|
||||||
if (endgames.probe(key, e->evaluationFunction))
|
if ((e->evaluationFunction = pos.this_thread()->endgames.probe<Value>(key)) != nullptr)
|
||||||
return e;
|
return e;
|
||||||
|
|
||||||
if (is_KXK<WHITE>(pos))
|
for (Color c = WHITE; c <= BLACK; ++c)
|
||||||
{
|
if (is_KXK(pos, c))
|
||||||
e->evaluationFunction = &EvaluateKXK[WHITE];
|
{
|
||||||
return e;
|
e->evaluationFunction = &EvaluateKXK[c];
|
||||||
}
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
if (is_KXK<BLACK>(pos))
|
// OK, we didn't find any special evaluation function for the current material
|
||||||
{
|
// configuration. Is there a suitable specialized scaling function?
|
||||||
e->evaluationFunction = &EvaluateKXK[BLACK];
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
|
|
||||||
// OK, we didn't find any special evaluation function for the current
|
|
||||||
// material configuration. Is there a suitable scaling function?
|
|
||||||
//
|
|
||||||
// We face problems when there are several conflicting applicable
|
|
||||||
// scaling functions and we need to decide which one to use.
|
|
||||||
EndgameBase<ScaleFactor>* sf;
|
EndgameBase<ScaleFactor>* sf;
|
||||||
|
|
||||||
if (endgames.probe(key, sf))
|
if ((sf = pos.this_thread()->endgames.probe<ScaleFactor>(key)) != nullptr)
|
||||||
{
|
{
|
||||||
e->scalingFunction[sf->color()] = sf;
|
e->scalingFunction[sf->strong_side()] = sf; // Only strong color assigned
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generic scaling functions that refer to more than one material
|
// We didn't find any specialized scaling function, so fall back on generic
|
||||||
// distribution. They should be probed after the specialized ones.
|
// ones that refer to more than one material distribution. Note that in this
|
||||||
// Note that these ones don't return after setting the function.
|
// case we don't return after setting the function.
|
||||||
if (is_KBPsKs<WHITE>(pos))
|
for (Color c = WHITE; c <= BLACK; ++c)
|
||||||
e->scalingFunction[WHITE] = &ScaleKBPsK[WHITE];
|
{
|
||||||
|
if (is_KBPsKs(pos, c))
|
||||||
|
e->scalingFunction[c] = &ScaleKBPsK[c];
|
||||||
|
|
||||||
if (is_KBPsKs<BLACK>(pos))
|
else if (is_KQKRPs(pos, c))
|
||||||
e->scalingFunction[BLACK] = &ScaleKBPsK[BLACK];
|
e->scalingFunction[c] = &ScaleKQKRPs[c];
|
||||||
|
}
|
||||||
if (is_KQKRPs<WHITE>(pos))
|
|
||||||
e->scalingFunction[WHITE] = &ScaleKQKRPs[WHITE];
|
|
||||||
|
|
||||||
else if (is_KQKRPs<BLACK>(pos))
|
|
||||||
e->scalingFunction[BLACK] = &ScaleKQKRPs[BLACK];
|
|
||||||
|
|
||||||
Value npm_w = pos.non_pawn_material(WHITE);
|
Value npm_w = pos.non_pawn_material(WHITE);
|
||||||
Value npm_b = pos.non_pawn_material(BLACK);
|
Value npm_b = pos.non_pawn_material(BLACK);
|
||||||
|
|
||||||
if (npm_w + npm_b == VALUE_ZERO && pos.pieces(PAWN))
|
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))
|
||||||
{
|
{
|
||||||
assert(pos.count<PAWN>(WHITE) >= 2);
|
assert(pos.count<PAWN>(WHITE) >= 2);
|
||||||
|
|
||||||
e->scalingFunction[WHITE] = &ScaleKPsK[WHITE];
|
e->scalingFunction[WHITE] = &ScaleKPsK[WHITE];
|
||||||
}
|
}
|
||||||
else if (!pos.count<PAWN>(WHITE))
|
else if (!pos.count<PAWN>(WHITE))
|
||||||
{
|
{
|
||||||
assert(pos.count<PAWN>(BLACK) >= 2);
|
assert(pos.count<PAWN>(BLACK) >= 2);
|
||||||
|
|
||||||
e->scalingFunction[BLACK] = &ScaleKPsK[BLACK];
|
e->scalingFunction[BLACK] = &ScaleKPsK[BLACK];
|
||||||
}
|
}
|
||||||
else if (pos.count<PAWN>(WHITE) == 1 && pos.count<PAWN>(BLACK) == 1)
|
else if (pos.count<PAWN>(WHITE) == 1 && pos.count<PAWN>(BLACK) == 1)
|
||||||
@@ -211,14 +192,16 @@ Entry* probe(const Position& pos, Table& entries, Endgames& endgames) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// No pawns makes it difficult to win, even with a material advantage. This
|
// Zero or just one pawn makes it difficult to win, even with a small material
|
||||||
// catches some trivial draws like KK, KBK and KNK and gives a very drawish
|
// advantage. This catches some trivial draws like KK, KBK and KNK and gives a
|
||||||
// scale factor for cases such as KRKBP and KmmKm (except for KBBKN).
|
// drawish scale factor for cases such as KRKBP and KmmKm (except for KBBKN).
|
||||||
if (!pos.count<PAWN>(WHITE) && npm_w - npm_b <= BishopValueMg)
|
if (!pos.count<PAWN>(WHITE) && npm_w - npm_b <= BishopValueMg)
|
||||||
e->factor[WHITE] = uint8_t(npm_w < RookValueMg ? SCALE_FACTOR_DRAW : npm_b <= BishopValueMg ? 4 : 12);
|
e->factor[WHITE] = uint8_t(npm_w < RookValueMg ? SCALE_FACTOR_DRAW :
|
||||||
|
npm_b <= BishopValueMg ? 4 : 14);
|
||||||
|
|
||||||
if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg)
|
if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg)
|
||||||
e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW : npm_w <= BishopValueMg ? 4 : 12);
|
e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW :
|
||||||
|
npm_w <= BishopValueMg ? 4 : 14);
|
||||||
|
|
||||||
if (pos.count<PAWN>(WHITE) == 1 && npm_w - npm_b <= BishopValueMg)
|
if (pos.count<PAWN>(WHITE) == 1 && npm_w - npm_b <= BishopValueMg)
|
||||||
e->factor[WHITE] = (uint8_t) SCALE_FACTOR_ONEPAWN;
|
e->factor[WHITE] = (uint8_t) SCALE_FACTOR_ONEPAWN;
|
||||||
@@ -226,40 +209,17 @@ Entry* probe(const Position& pos, Table& entries, Endgames& endgames) {
|
|||||||
if (pos.count<PAWN>(BLACK) == 1 && npm_b - npm_w <= BishopValueMg)
|
if (pos.count<PAWN>(BLACK) == 1 && npm_b - npm_w <= BishopValueMg)
|
||||||
e->factor[BLACK] = (uint8_t) SCALE_FACTOR_ONEPAWN;
|
e->factor[BLACK] = (uint8_t) SCALE_FACTOR_ONEPAWN;
|
||||||
|
|
||||||
// Compute the space weight
|
|
||||||
if (npm_w + npm_b >= 2 * QueenValueMg + 4 * RookValueMg + 2 * KnightValueMg)
|
|
||||||
{
|
|
||||||
int minorPieceCount = pos.count<KNIGHT>(WHITE) + pos.count<BISHOP>(WHITE)
|
|
||||||
+ pos.count<KNIGHT>(BLACK) + pos.count<BISHOP>(BLACK);
|
|
||||||
|
|
||||||
e->spaceWeight = make_score(minorPieceCount * minorPieceCount, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Material::game_phase() calculates the phase given the current
|
|
||||||
/// position. Because the phase is strictly a function of the material, it
|
|
||||||
/// is stored in MaterialEntry.
|
|
||||||
|
|
||||||
Phase game_phase(const Position& pos) {
|
|
||||||
|
|
||||||
Value npm = pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK);
|
|
||||||
|
|
||||||
return npm >= MidgameLimit ? PHASE_MIDGAME
|
|
||||||
: npm <= EndgameLimit ? PHASE_ENDGAME
|
|
||||||
: Phase(((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Material
|
} // namespace Material
|
||||||
|
|||||||
+20
-22
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -28,47 +29,44 @@
|
|||||||
namespace Material {
|
namespace Material {
|
||||||
|
|
||||||
/// Material::Entry contains various information about a material configuration.
|
/// Material::Entry contains various information about a material configuration.
|
||||||
/// It contains a material balance evaluation, a function pointer to a special
|
/// It contains a material imbalance evaluation, a function pointer to a special
|
||||||
/// endgame evaluation function (which in most cases is NULL, meaning that the
|
/// endgame evaluation function (which in most cases is NULL, meaning that the
|
||||||
/// standard evaluation function will be used), and "scale factors".
|
/// standard evaluation function will be used), and scale factors.
|
||||||
///
|
///
|
||||||
/// The scale factors are used to scale the evaluation score up or down.
|
/// The scale factors are used to scale the evaluation score up or down. For
|
||||||
/// For instance, in KRB vs KR endgames, the score is scaled down by a factor
|
/// instance, in KRB vs KR endgames, the score is scaled down by a factor of 4,
|
||||||
/// of 4, which will result in scores of absolute value less than one pawn.
|
/// which will result in scores of absolute value less than one pawn.
|
||||||
|
|
||||||
struct Entry {
|
struct Entry {
|
||||||
|
|
||||||
Score material_value() const { return make_score(value, value); }
|
Score imbalance() const { return make_score(value, value); }
|
||||||
Score space_weight() const { return spaceWeight; }
|
|
||||||
Phase game_phase() const { return gamePhase; }
|
Phase game_phase() const { return gamePhase; }
|
||||||
bool specialized_eval_exists() const { return evaluationFunction != NULL; }
|
bool specialized_eval_exists() const { return evaluationFunction != nullptr; }
|
||||||
Value evaluate(const Position& pos) const { return (*evaluationFunction)(pos); }
|
Value evaluate(const Position& pos) const { return (*evaluationFunction)(pos); }
|
||||||
|
|
||||||
// scale_factor takes a position and a color as input, and returns a scale factor
|
// scale_factor takes a position and a color as input and returns a scale factor
|
||||||
// for the given color. We have to provide the position in addition to the color,
|
// for the given color. We have to provide the position in addition to the color
|
||||||
// because the scale factor need not be a constant: It can also be a function
|
// because the scale factor may also be a function which should be applied to
|
||||||
// which should be applied to the position. For instance, in KBP vs K endgames,
|
// the position. For instance, in KBP vs K endgames, the scaling function looks
|
||||||
// a scaling function for draws with rook pawns and wrong-colored bishops.
|
// for rook pawns and wrong-colored bishops.
|
||||||
|
|
||||||
ScaleFactor scale_factor(const Position& pos, Color c) const {
|
ScaleFactor scale_factor(const Position& pos, Color c) const {
|
||||||
|
ScaleFactor sf = scalingFunction[c] ? (*scalingFunction[c])(pos)
|
||||||
return !scalingFunction[c] || (*scalingFunction[c])(pos) == SCALE_FACTOR_NONE
|
: SCALE_FACTOR_NONE;
|
||||||
? ScaleFactor(factor[c]) : (*scalingFunction[c])(pos);
|
return sf != SCALE_FACTOR_NONE ? sf : ScaleFactor(factor[c]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Key key;
|
Key key;
|
||||||
int16_t value;
|
int16_t value;
|
||||||
uint8_t factor[COLOR_NB];
|
uint8_t factor[COLOR_NB];
|
||||||
EndgameBase<Value>* evaluationFunction;
|
EndgameBase<Value>* evaluationFunction;
|
||||||
EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB];
|
EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB]; // Could be one for each
|
||||||
Score spaceWeight;
|
// side (e.g. KPKP, KBPsKs)
|
||||||
Phase gamePhase;
|
Phase gamePhase;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef HashTable<Entry, 8192> Table;
|
typedef HashTable<Entry, 8192> Table;
|
||||||
|
|
||||||
Entry* probe(const Position& pos, Table& entries, Endgames& endgames);
|
Entry* probe(const Position& pos);
|
||||||
Phase game_phase(const Position& pos);
|
|
||||||
|
|
||||||
} // namespace Material
|
} // namespace Material
|
||||||
|
|
||||||
|
|||||||
+70
-87
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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
|
||||||
@@ -17,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 <fstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@@ -26,10 +28,69 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
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.
|
||||||
static const string Version = "5";
|
const string Version = "8";
|
||||||
|
|
||||||
|
/// Our fancy logging facility. The trick here is to replace cin.rdbuf() and
|
||||||
|
/// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We
|
||||||
|
/// can toggle the logging of std::cout and std:cin at runtime whilst preserving
|
||||||
|
/// usual I/O functionality, all without changing a single line of code!
|
||||||
|
/// Idea from http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81
|
||||||
|
|
||||||
|
struct Tie: public streambuf { // MSVC requires split streambuf for cin and cout
|
||||||
|
|
||||||
|
Tie(streambuf* b, streambuf* l) : buf(b), logBuf(l) {}
|
||||||
|
|
||||||
|
int sync() { return logBuf->pubsync(), buf->pubsync(); }
|
||||||
|
int overflow(int c) { return log(buf->sputc((char)c), "<< "); }
|
||||||
|
int underflow() { return buf->sgetc(); }
|
||||||
|
int uflow() { return log(buf->sbumpc(), ">> "); }
|
||||||
|
|
||||||
|
streambuf *buf, *logBuf;
|
||||||
|
|
||||||
|
int log(int c, const char* prefix) {
|
||||||
|
|
||||||
|
static int last = '\n'; // Single log file
|
||||||
|
|
||||||
|
if (last == '\n')
|
||||||
|
logBuf->sputn(prefix, 3);
|
||||||
|
|
||||||
|
return last = logBuf->sputc((char)c);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Logger {
|
||||||
|
|
||||||
|
Logger() : in(cin.rdbuf(), file.rdbuf()), out(cout.rdbuf(), file.rdbuf()) {}
|
||||||
|
~Logger() { start(""); }
|
||||||
|
|
||||||
|
ofstream file;
|
||||||
|
Tie in, out;
|
||||||
|
|
||||||
|
public:
|
||||||
|
static void start(const std::string& fname) {
|
||||||
|
|
||||||
|
static Logger l;
|
||||||
|
|
||||||
|
if (!fname.empty() && !l.file.is_open())
|
||||||
|
{
|
||||||
|
l.file.open(fname, ifstream::out);
|
||||||
|
cin.rdbuf(&l.in);
|
||||||
|
cout.rdbuf(&l.out);
|
||||||
|
}
|
||||||
|
else if (fname.empty() && l.file.is_open())
|
||||||
|
{
|
||||||
|
cout.rdbuf(l.out.buf);
|
||||||
|
cin.rdbuf(l.in.buf);
|
||||||
|
l.file.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
/// engine_info() returns the full name of the current Stockfish version. This
|
/// engine_info() returns the full name of the current Stockfish version. This
|
||||||
/// will be either "Stockfish <Tag> DD-MM-YY" (where DD-MM-YY is the date when
|
/// will be either "Stockfish <Tag> DD-MM-YY" (where DD-MM-YY is the date when
|
||||||
@@ -51,20 +112,19 @@ const string engine_info(bool to_uci) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ss << (Is64Bit ? " 64" : "")
|
ss << (Is64Bit ? " 64" : "")
|
||||||
<< (HasPext ? " BMI2" : (HasPopCnt ? " SSE4.2" : ""))
|
<< (HasPext ? " BMI2" : (HasPopCnt ? " POPCNT" : ""))
|
||||||
<< (to_uci ? "\nid author ": " by ")
|
<< (to_uci ? "\nid author ": " by ")
|
||||||
<< "Tord Romstad, Marco Costalba and Joona Kiiski";
|
<< "T. Romstad, M. Costalba, J. Kiiski, G. Linscott";
|
||||||
|
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Debug functions used mainly to collect run-time statistics
|
/// Debug functions used mainly to collect run-time statistics
|
||||||
|
|
||||||
static int64_t hits[2], means[2];
|
static int64_t hits[2], means[2];
|
||||||
|
|
||||||
void dbg_hit_on(bool b) { ++hits[0]; if (b) ++hits[1]; }
|
void dbg_hit_on(bool b) { ++hits[0]; if (b) ++hits[1]; }
|
||||||
void dbg_hit_on_c(bool c, bool b) { if (c) dbg_hit_on(b); }
|
void dbg_hit_on(bool c, bool b) { if (c) dbg_hit_on(b); }
|
||||||
void dbg_mean_of(int v) { ++means[0]; means[1] += v; }
|
void dbg_mean_of(int v) { ++means[0]; means[1] += v; }
|
||||||
|
|
||||||
void dbg_print() {
|
void dbg_print() {
|
||||||
@@ -79,64 +139,6 @@ void dbg_print() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Our fancy logging facility. The trick here is to replace cin.rdbuf() and
|
|
||||||
/// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We
|
|
||||||
/// can toggle the logging of std::cout and std:cin at runtime whilst preserving
|
|
||||||
/// usual i/o functionality, all without changing a single line of code!
|
|
||||||
/// Idea from http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81
|
|
||||||
|
|
||||||
struct Tie: public streambuf { // MSVC requires splitted streambuf for cin and cout
|
|
||||||
|
|
||||||
Tie(streambuf* b, ofstream* f) : buf(b), file(f) {}
|
|
||||||
|
|
||||||
int sync() { return file->rdbuf()->pubsync(), buf->pubsync(); }
|
|
||||||
int overflow(int c) { return log(buf->sputc((char)c), "<< "); }
|
|
||||||
int underflow() { return buf->sgetc(); }
|
|
||||||
int uflow() { return log(buf->sbumpc(), ">> "); }
|
|
||||||
|
|
||||||
streambuf* buf;
|
|
||||||
ofstream* file;
|
|
||||||
|
|
||||||
int log(int c, const char* prefix) {
|
|
||||||
|
|
||||||
static int last = '\n';
|
|
||||||
|
|
||||||
if (last == '\n')
|
|
||||||
file->rdbuf()->sputn(prefix, 3);
|
|
||||||
|
|
||||||
return last = file->rdbuf()->sputc((char)c);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class Logger {
|
|
||||||
|
|
||||||
Logger() : in(cin.rdbuf(), &file), out(cout.rdbuf(), &file) {}
|
|
||||||
~Logger() { start(false); }
|
|
||||||
|
|
||||||
ofstream file;
|
|
||||||
Tie in, out;
|
|
||||||
|
|
||||||
public:
|
|
||||||
static void start(bool b) {
|
|
||||||
|
|
||||||
static Logger l;
|
|
||||||
|
|
||||||
if (b && !l.file.is_open())
|
|
||||||
{
|
|
||||||
l.file.open("io_log.txt", ifstream::out | ifstream::app);
|
|
||||||
cin.rdbuf(&l.in);
|
|
||||||
cout.rdbuf(&l.out);
|
|
||||||
}
|
|
||||||
else if (!b && l.file.is_open())
|
|
||||||
{
|
|
||||||
cout.rdbuf(l.out.buf);
|
|
||||||
cin.rdbuf(l.in.buf);
|
|
||||||
l.file.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/// Used to serialize access to std::cout to avoid multiple threads writing at
|
/// Used to serialize access to std::cout to avoid multiple threads writing at
|
||||||
/// the same time.
|
/// the same time.
|
||||||
|
|
||||||
@@ -155,26 +157,7 @@ std::ostream& operator<<(std::ostream& os, SyncCout sc) {
|
|||||||
|
|
||||||
|
|
||||||
/// Trampoline helper to avoid moving Logger to misc.h
|
/// Trampoline helper to avoid moving Logger to misc.h
|
||||||
void start_logger(bool b) { Logger::start(b); }
|
void start_logger(const std::string& fname) { Logger::start(fname); }
|
||||||
|
|
||||||
|
|
||||||
/// timed_wait() waits for msec milliseconds. It is mainly a helper to wrap
|
|
||||||
/// the conversion from milliseconds to struct timespec, as used by pthreads.
|
|
||||||
|
|
||||||
void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) {
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
int tm = msec;
|
|
||||||
#else
|
|
||||||
timespec ts, *tm = &ts;
|
|
||||||
uint64_t ms = Time::now() + msec;
|
|
||||||
|
|
||||||
ts.tv_sec = ms / 1000;
|
|
||||||
ts.tv_nsec = (ms % 1000) * 1000000LL;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
cond_timedwait(sleepCond, sleepLock, tm);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// prefetch() preloads the given address in L1/L2 cache. This is a non-blocking
|
/// prefetch() preloads the given address in L1/L2 cache. This is a non-blocking
|
||||||
@@ -182,11 +165,11 @@ void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) {
|
|||||||
/// which can be quite slow.
|
/// which can be quite slow.
|
||||||
#ifdef NO_PREFETCH
|
#ifdef NO_PREFETCH
|
||||||
|
|
||||||
void prefetch(char*) {}
|
void prefetch(void*) {}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
void prefetch(char* addr) {
|
void prefetch(void* addr) {
|
||||||
|
|
||||||
# if defined(__INTEL_COMPILER)
|
# if defined(__INTEL_COMPILER)
|
||||||
// This hack prevents prefetches from being optimized away by
|
// This hack prevents prefetches from being optimized away by
|
||||||
@@ -195,7 +178,7 @@ void prefetch(char* addr) {
|
|||||||
# endif
|
# endif
|
||||||
|
|
||||||
# if defined(__INTEL_COMPILER) || defined(_MSC_VER)
|
# if defined(__INTEL_COMPILER) || defined(_MSC_VER)
|
||||||
_mm_prefetch(addr, _MM_HINT_T0);
|
_mm_prefetch((char*)addr, _MM_HINT_T0);
|
||||||
# else
|
# else
|
||||||
__builtin_prefetch(addr);
|
__builtin_prefetch(addr);
|
||||||
# endif
|
# endif
|
||||||
|
|||||||
+55
-23
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,42 +21,36 @@
|
|||||||
#ifndef MISC_H_INCLUDED
|
#ifndef MISC_H_INCLUDED
|
||||||
#define MISC_H_INCLUDED
|
#define MISC_H_INCLUDED
|
||||||
|
|
||||||
#include <fstream>
|
#include <cassert>
|
||||||
|
#include <chrono>
|
||||||
|
#include <ostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
extern const std::string engine_info(bool to_uci = false);
|
const std::string engine_info(bool to_uci = false);
|
||||||
extern void timed_wait(WaitCondition&, Lock&, int);
|
void prefetch(void* addr);
|
||||||
extern void prefetch(char* addr);
|
void start_logger(const std::string& fname);
|
||||||
extern void start_logger(bool b);
|
|
||||||
|
|
||||||
extern void dbg_hit_on(bool b);
|
void dbg_hit_on(bool b);
|
||||||
extern void dbg_hit_on_c(bool c, bool b);
|
void dbg_hit_on(bool c, bool b);
|
||||||
extern void dbg_mean_of(int v);
|
void dbg_mean_of(int v);
|
||||||
extern void dbg_print();
|
void dbg_print();
|
||||||
|
|
||||||
|
typedef std::chrono::milliseconds::rep TimePoint; // A value in milliseconds
|
||||||
|
|
||||||
struct Log : public std::ofstream {
|
inline TimePoint now() {
|
||||||
Log(const std::string& f = "log.txt") : std::ofstream(f.c_str(), std::ios::out | std::ios::app) {}
|
return std::chrono::duration_cast<std::chrono::milliseconds>
|
||||||
~Log() { if (is_open()) close(); }
|
(std::chrono::steady_clock::now().time_since_epoch()).count();
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
namespace Time {
|
|
||||||
typedef int64_t point;
|
|
||||||
inline point now() { return system_time_to_msec(); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Entry, int Size>
|
template<class Entry, int Size>
|
||||||
struct HashTable {
|
struct HashTable {
|
||||||
HashTable() : table(Size, Entry()) {}
|
Entry* operator[](Key key) { return &table[(uint32_t)key & (Size - 1)]; }
|
||||||
Entry* operator[](Key k) { return &table[(uint32_t)k & (Size - 1)]; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<Entry> table;
|
std::vector<Entry> table = std::vector<Entry>(Size);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -65,4 +60,41 @@ std::ostream& operator<<(std::ostream&, SyncCout);
|
|||||||
#define sync_cout std::cout << IO_LOCK
|
#define sync_cout std::cout << IO_LOCK
|
||||||
#define sync_endl std::endl << IO_UNLOCK
|
#define sync_endl std::endl << IO_UNLOCK
|
||||||
|
|
||||||
|
|
||||||
|
/// xorshift64star Pseudo-Random Number Generator
|
||||||
|
/// This class is based on original code written and dedicated
|
||||||
|
/// to the public domain by Sebastiano Vigna (2014).
|
||||||
|
/// It has the following characteristics:
|
||||||
|
///
|
||||||
|
/// - Outputs 64-bit numbers
|
||||||
|
/// - Passes Dieharder and SmallCrush test batteries
|
||||||
|
/// - Does not require warm-up, no zeroland to escape
|
||||||
|
/// - Internal state is a single 64-bit integer
|
||||||
|
/// - Period is 2^64 - 1
|
||||||
|
/// - Speed: 1.60 ns/call (Core i7 @3.40GHz)
|
||||||
|
///
|
||||||
|
/// For further analysis see
|
||||||
|
/// <http://vigna.di.unimi.it/ftp/papers/xorshift.pdf>
|
||||||
|
|
||||||
|
class PRNG {
|
||||||
|
|
||||||
|
uint64_t s;
|
||||||
|
|
||||||
|
uint64_t rand64() {
|
||||||
|
|
||||||
|
s ^= s >> 12, s ^= s << 25, s ^= s >> 27;
|
||||||
|
return s * 2685821657736338717LL;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
PRNG(uint64_t seed) : s(seed) { assert(seed); }
|
||||||
|
|
||||||
|
template<typename T> T rand() { return T(rand64()); }
|
||||||
|
|
||||||
|
/// Special generator used to fast init magic numbers.
|
||||||
|
/// Output values only have 1/8th of their bits set on average.
|
||||||
|
template<typename T> T sparse_rand()
|
||||||
|
{ return T(rand64() & rand64() & rand64()); }
|
||||||
|
};
|
||||||
|
|
||||||
#endif // #ifndef MISC_H_INCLUDED
|
#endif // #ifndef MISC_H_INCLUDED
|
||||||
|
|||||||
+122
-120
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,93 +26,83 @@
|
|||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
template<CastlingRight Cr, bool Checks, bool Chess960>
|
template<CastlingRight Cr, bool Checks, bool Chess960>
|
||||||
ExtMove* generate_castling(const Position& pos, ExtMove* mlist, Color us, const CheckInfo* ci) {
|
ExtMove* generate_castling(const Position& pos, ExtMove* moveList, Color us) {
|
||||||
|
|
||||||
static const bool KingSide = (Cr == WHITE_OO || Cr == BLACK_OO);
|
static const 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 mlist;
|
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.king_square(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 ? DELTA_W : DELTA_E
|
const Square K = Chess960 ? kto > kfrom ? WEST : EAST
|
||||||
: KingSide ? DELTA_W : DELTA_E;
|
: KingSide ? WEST : EAST;
|
||||||
|
|
||||||
for (Square s = kto; s != kfrom; s += K)
|
for (Square s = kto; s != kfrom; s += K)
|
||||||
if (pos.attackers_to(s) & enemies)
|
if (pos.attackers_to(s) & enemies)
|
||||||
return mlist;
|
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 mlist;
|
return moveList;
|
||||||
|
|
||||||
Move m = make<CASTLING>(kfrom, rfrom);
|
Move m = make<CASTLING>(kfrom, rfrom);
|
||||||
|
|
||||||
if (Checks && !pos.gives_check(m, *ci))
|
if (Checks && !pos.gives_check(m))
|
||||||
return mlist;
|
return moveList;
|
||||||
|
|
||||||
(mlist++)->move = m;
|
*moveList++ = m;
|
||||||
|
return moveList;
|
||||||
return mlist;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<GenType Type, Square Delta>
|
template<GenType Type, Square D>
|
||||||
inline ExtMove* generate_promotions(ExtMove* mlist, Bitboard pawnsOn7,
|
ExtMove* make_promotions(ExtMove* moveList, Square to, Square ksq) {
|
||||||
Bitboard target, const CheckInfo* ci) {
|
|
||||||
|
|
||||||
Bitboard b = shift_bb<Delta>(pawnsOn7) & target;
|
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
||||||
|
*moveList++ = make<PROMOTION>(to - D, to, QUEEN);
|
||||||
|
|
||||||
while (b)
|
if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
|
||||||
{
|
{
|
||||||
Square to = pop_lsb(&b);
|
*moveList++ = make<PROMOTION>(to - D, to, ROOK);
|
||||||
|
*moveList++ = make<PROMOTION>(to - D, to, BISHOP);
|
||||||
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
*moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
|
||||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, QUEEN);
|
|
||||||
|
|
||||||
if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
|
|
||||||
{
|
|
||||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, ROOK);
|
|
||||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, BISHOP);
|
|
||||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Knight promotion is the only promotion that can give a direct check
|
|
||||||
// that's not already included in the queen promotion.
|
|
||||||
if (Type == QUIET_CHECKS && (StepAttacksBB[W_KNIGHT][to] & ci->ksq))
|
|
||||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
|
|
||||||
else
|
|
||||||
(void)ci; // Silence a warning under MSVC
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mlist;
|
// Knight promotion is the only promotion that can give a direct check
|
||||||
|
// that's not already included in the queen promotion.
|
||||||
|
if (Type == QUIET_CHECKS && (StepAttacksBB[W_KNIGHT][to] & ksq))
|
||||||
|
*moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
|
||||||
|
else
|
||||||
|
(void)ksq; // Silence a warning under MSVC
|
||||||
|
|
||||||
|
return moveList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<Color Us, GenType Type>
|
template<Color Us, GenType Type>
|
||||||
ExtMove* generate_pawn_moves(const Position& pos, ExtMove* mlist,
|
ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard target) {
|
||||||
Bitboard target, const CheckInfo* ci) {
|
|
||||||
|
|
||||||
// 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);
|
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||||
const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
|
const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
|
||||||
const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
|
const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
|
||||||
const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
|
const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
|
||||||
const Square Up = (Us == WHITE ? DELTA_N : DELTA_S);
|
const Square Up = (Us == WHITE ? NORTH : SOUTH);
|
||||||
const Square Right = (Us == WHITE ? DELTA_NE : DELTA_SW);
|
const Square Right = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
|
||||||
const Square Left = (Us == WHITE ? DELTA_NW : DELTA_SE);
|
const Square Left = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
|
||||||
|
|
||||||
Bitboard b1, b2, dc1, dc2, emptySquares;
|
Bitboard emptySquares;
|
||||||
|
|
||||||
Bitboard pawnsOn7 = pos.pieces(Us, PAWN) & TRank7BB;
|
Bitboard pawnsOn7 = pos.pieces(Us, PAWN) & TRank7BB;
|
||||||
Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
|
Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
|
||||||
@@ -124,8 +115,8 @@ namespace {
|
|||||||
{
|
{
|
||||||
emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
|
emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
|
||||||
|
|
||||||
b1 = shift_bb<Up>(pawnsNotOn7) & emptySquares;
|
Bitboard b1 = shift<Up>(pawnsNotOn7) & emptySquares;
|
||||||
b2 = shift_bb<Up>(b1 & TRank3BB) & emptySquares;
|
Bitboard b2 = shift<Up>(b1 & TRank3BB) & emptySquares;
|
||||||
|
|
||||||
if (Type == EVASIONS) // Consider only blocking squares
|
if (Type == EVASIONS) // Consider only blocking squares
|
||||||
{
|
{
|
||||||
@@ -135,17 +126,20 @@ namespace {
|
|||||||
|
|
||||||
if (Type == QUIET_CHECKS)
|
if (Type == QUIET_CHECKS)
|
||||||
{
|
{
|
||||||
b1 &= pos.attacks_from<PAWN>(ci->ksq, Them);
|
Square ksq = pos.square<KING>(Them);
|
||||||
b2 &= pos.attacks_from<PAWN>(ci->ksq, Them);
|
|
||||||
|
b1 &= pos.attacks_from<PAWN>(ksq, Them);
|
||||||
|
b2 &= pos.attacks_from<PAWN>(ksq, Them);
|
||||||
|
|
||||||
// Add pawn pushes which give discovered check. This is possible only
|
// Add pawn pushes which give discovered check. This is possible only
|
||||||
// if the pawn is not on the same file as the enemy king, because we
|
// 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.
|
||||||
if (pawnsNotOn7 & ci->dcCandidates)
|
Bitboard dcCandidates = pos.discovered_check_candidates();
|
||||||
|
if (pawnsNotOn7 & dcCandidates)
|
||||||
{
|
{
|
||||||
dc1 = shift_bb<Up>(pawnsNotOn7 & ci->dcCandidates) & emptySquares & ~file_bb(ci->ksq);
|
Bitboard dc1 = shift<Up>(pawnsNotOn7 & dcCandidates) & emptySquares & ~file_bb(ksq);
|
||||||
dc2 = shift_bb<Up>(dc1 & TRank3BB) & emptySquares;
|
Bitboard dc2 = shift<Up>(dc1 & TRank3BB) & emptySquares;
|
||||||
|
|
||||||
b1 |= dc1;
|
b1 |= dc1;
|
||||||
b2 |= dc2;
|
b2 |= dc2;
|
||||||
@@ -155,13 +149,13 @@ namespace {
|
|||||||
while (b1)
|
while (b1)
|
||||||
{
|
{
|
||||||
Square to = pop_lsb(&b1);
|
Square to = pop_lsb(&b1);
|
||||||
(mlist++)->move = make_move(to - Up, to);
|
*moveList++ = make_move(to - Up, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (b2)
|
while (b2)
|
||||||
{
|
{
|
||||||
Square to = pop_lsb(&b2);
|
Square to = pop_lsb(&b2);
|
||||||
(mlist++)->move = make_move(to - Up - Up, to);
|
*moveList++ = make_move(to - Up - Up, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,27 +168,38 @@ namespace {
|
|||||||
if (Type == EVASIONS)
|
if (Type == EVASIONS)
|
||||||
emptySquares &= target;
|
emptySquares &= target;
|
||||||
|
|
||||||
mlist = generate_promotions<Type, Right>(mlist, pawnsOn7, enemies, ci);
|
Bitboard b1 = shift<Right>(pawnsOn7) & enemies;
|
||||||
mlist = generate_promotions<Type, Left >(mlist, pawnsOn7, enemies, ci);
|
Bitboard b2 = shift<Left >(pawnsOn7) & enemies;
|
||||||
mlist = generate_promotions<Type, Up>(mlist, pawnsOn7, emptySquares, ci);
|
Bitboard b3 = shift<Up >(pawnsOn7) & emptySquares;
|
||||||
|
|
||||||
|
Square ksq = pos.square<KING>(Them);
|
||||||
|
|
||||||
|
while (b1)
|
||||||
|
moveList = make_promotions<Type, Right>(moveList, pop_lsb(&b1), ksq);
|
||||||
|
|
||||||
|
while (b2)
|
||||||
|
moveList = make_promotions<Type, Left >(moveList, pop_lsb(&b2), ksq);
|
||||||
|
|
||||||
|
while (b3)
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
b1 = shift_bb<Right>(pawnsNotOn7) & enemies;
|
Bitboard b1 = shift<Right>(pawnsNotOn7) & enemies;
|
||||||
b2 = shift_bb<Left >(pawnsNotOn7) & enemies;
|
Bitboard b2 = shift<Left >(pawnsNotOn7) & enemies;
|
||||||
|
|
||||||
while (b1)
|
while (b1)
|
||||||
{
|
{
|
||||||
Square to = pop_lsb(&b1);
|
Square to = pop_lsb(&b1);
|
||||||
(mlist++)->move = make_move(to - Right, to);
|
*moveList++ = make_move(to - Right, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (b2)
|
while (b2)
|
||||||
{
|
{
|
||||||
Square to = pop_lsb(&b2);
|
Square to = pop_lsb(&b2);
|
||||||
(mlist++)->move = make_move(to - Left, to);
|
*moveList++ = make_move(to - Left, to);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pos.ep_square() != SQ_NONE)
|
if (pos.ep_square() != SQ_NONE)
|
||||||
@@ -205,92 +210,90 @@ namespace {
|
|||||||
// is the double pushed pawn and so is in the target. Otherwise this
|
// is the double pushed pawn and so is in the target. Otherwise this
|
||||||
// is a discovery check and we are forced to do otherwise.
|
// is a discovery check and we are forced to do otherwise.
|
||||||
if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
|
if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
|
||||||
return mlist;
|
return moveList;
|
||||||
|
|
||||||
b1 = pawnsNotOn7 & pos.attacks_from<PAWN>(pos.ep_square(), Them);
|
b1 = pawnsNotOn7 & pos.attacks_from<PAWN>(pos.ep_square(), Them);
|
||||||
|
|
||||||
assert(b1);
|
assert(b1);
|
||||||
|
|
||||||
while (b1)
|
while (b1)
|
||||||
(mlist++)->move = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
|
*moveList++ = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mlist;
|
return moveList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<PieceType Pt, bool Checks> FORCE_INLINE
|
template<PieceType Pt, bool Checks>
|
||||||
ExtMove* generate_moves(const Position& pos, ExtMove* mlist, Color us,
|
ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Color us,
|
||||||
Bitboard target, const CheckInfo* ci) {
|
Bitboard target) {
|
||||||
|
|
||||||
assert(Pt != KING && Pt != PAWN);
|
assert(Pt != KING && Pt != PAWN);
|
||||||
|
|
||||||
const Square* pl = pos.list<Pt>(us);
|
const Square* pl = pos.squares<Pt>(us);
|
||||||
|
|
||||||
for (Square from = *pl; from != SQ_NONE; from = *++pl)
|
for (Square from = *pl; from != SQ_NONE; from = *++pl)
|
||||||
{
|
{
|
||||||
if (Checks)
|
if (Checks)
|
||||||
{
|
{
|
||||||
if ( (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
|
if ( (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
|
||||||
&& !(PseudoAttacks[Pt][from] & target & ci->checkSq[Pt]))
|
&& !(PseudoAttacks[Pt][from] & target & pos.check_squares(Pt)))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (unlikely(ci->dcCandidates) && (ci->dcCandidates & from))
|
if (pos.discovered_check_candidates() & from)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bitboard b = pos.attacks_from<Pt>(from) & target;
|
Bitboard b = pos.attacks_from<Pt>(from) & target;
|
||||||
|
|
||||||
if (Checks)
|
if (Checks)
|
||||||
b &= ci->checkSq[Pt];
|
b &= pos.check_squares(Pt);
|
||||||
|
|
||||||
while (b)
|
while (b)
|
||||||
(mlist++)->move = make_move(from, pop_lsb(&b));
|
*moveList++ = make_move(from, pop_lsb(&b));
|
||||||
}
|
}
|
||||||
|
|
||||||
return mlist;
|
return moveList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<Color Us, GenType Type> FORCE_INLINE
|
template<Color Us, GenType Type>
|
||||||
ExtMove* generate_all(const Position& pos, ExtMove* mlist, Bitboard target,
|
ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target) {
|
||||||
const CheckInfo* ci = NULL) {
|
|
||||||
|
|
||||||
const bool Checks = Type == QUIET_CHECKS;
|
const bool Checks = Type == QUIET_CHECKS;
|
||||||
|
|
||||||
mlist = generate_pawn_moves<Us, Type>(pos, mlist, target, ci);
|
moveList = generate_pawn_moves<Us, Type>(pos, moveList, target);
|
||||||
mlist = generate_moves<KNIGHT, Checks>(pos, mlist, Us, target, ci);
|
moveList = generate_moves<KNIGHT, Checks>(pos, moveList, Us, target);
|
||||||
mlist = generate_moves<BISHOP, Checks>(pos, mlist, Us, target, ci);
|
moveList = generate_moves<BISHOP, Checks>(pos, moveList, Us, target);
|
||||||
mlist = generate_moves< ROOK, Checks>(pos, mlist, Us, target, ci);
|
moveList = generate_moves< ROOK, Checks>(pos, moveList, Us, target);
|
||||||
mlist = generate_moves< QUEEN, Checks>(pos, mlist, Us, target, ci);
|
moveList = generate_moves< QUEEN, Checks>(pos, moveList, Us, target);
|
||||||
|
|
||||||
if (Type != QUIET_CHECKS && Type != EVASIONS)
|
if (Type != QUIET_CHECKS && Type != EVASIONS)
|
||||||
{
|
{
|
||||||
Square ksq = pos.king_square(Us);
|
Square ksq = pos.square<KING>(Us);
|
||||||
Bitboard b = pos.attacks_from<KING>(ksq) & target;
|
Bitboard b = pos.attacks_from<KING>(ksq) & target;
|
||||||
while (b)
|
while (b)
|
||||||
(mlist++)->move = make_move(ksq, pop_lsb(&b));
|
*moveList++ = make_move(ksq, pop_lsb(&b));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(Us))
|
if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(Us))
|
||||||
{
|
{
|
||||||
if (pos.is_chess960())
|
if (pos.is_chess960())
|
||||||
{
|
{
|
||||||
mlist = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, true>(pos, mlist, Us, ci);
|
moveList = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, true>(pos, moveList, Us);
|
||||||
mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, true>(pos, mlist, Us, ci);
|
moveList = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, true>(pos, moveList, Us);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mlist = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, false>(pos, mlist, Us, ci);
|
moveList = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, false>(pos, moveList, Us);
|
||||||
mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, false>(pos, mlist, Us, ci);
|
moveList = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, false>(pos, moveList, Us);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mlist;
|
return moveList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
@@ -304,19 +307,19 @@ namespace {
|
|||||||
/// non-captures. Returns a pointer to the end of the move list.
|
/// non-captures. Returns a pointer to the end of the move list.
|
||||||
|
|
||||||
template<GenType Type>
|
template<GenType Type>
|
||||||
ExtMove* generate(const Position& pos, ExtMove* mlist) {
|
ExtMove* generate(const Position& pos, ExtMove* moveList) {
|
||||||
|
|
||||||
assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS);
|
assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS);
|
||||||
assert(!pos.checkers());
|
assert(!pos.checkers());
|
||||||
|
|
||||||
Color us = pos.side_to_move();
|
Color us = pos.side_to_move();
|
||||||
|
|
||||||
Bitboard target = Type == CAPTURES ? pos.pieces(~us)
|
Bitboard target = Type == CAPTURES ? pos.pieces(~us)
|
||||||
: Type == QUIETS ? ~pos.pieces()
|
: Type == QUIETS ? ~pos.pieces()
|
||||||
: Type == NON_EVASIONS ? ~pos.pieces(us) : 0;
|
: Type == NON_EVASIONS ? ~pos.pieces(us) : 0;
|
||||||
|
|
||||||
return us == WHITE ? generate_all<WHITE, Type>(pos, mlist, target)
|
return us == WHITE ? generate_all<WHITE, Type>(pos, moveList, target)
|
||||||
: generate_all<BLACK, Type>(pos, mlist, target);
|
: generate_all<BLACK, Type>(pos, moveList, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Explicit template instantiations
|
// Explicit template instantiations
|
||||||
@@ -328,13 +331,12 @@ template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
|
|||||||
/// generate<QUIET_CHECKS> generates all pseudo-legal non-captures and knight
|
/// generate<QUIET_CHECKS> generates all pseudo-legal non-captures and knight
|
||||||
/// underpromotions that give check. Returns a pointer to the end of the move list.
|
/// underpromotions that give check. Returns a pointer to the end of the move list.
|
||||||
template<>
|
template<>
|
||||||
ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* mlist) {
|
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();
|
||||||
CheckInfo ci(pos);
|
Bitboard dc = pos.discovered_check_candidates();
|
||||||
Bitboard dc = ci.dcCandidates;
|
|
||||||
|
|
||||||
while (dc)
|
while (dc)
|
||||||
{
|
{
|
||||||
@@ -347,26 +349,26 @@ ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* mlist) {
|
|||||||
Bitboard b = pos.attacks_from(Piece(pt), from) & ~pos.pieces();
|
Bitboard b = pos.attacks_from(Piece(pt), from) & ~pos.pieces();
|
||||||
|
|
||||||
if (pt == KING)
|
if (pt == KING)
|
||||||
b &= ~PseudoAttacks[QUEEN][ci.ksq];
|
b &= ~PseudoAttacks[QUEEN][pos.square<KING>(~us)];
|
||||||
|
|
||||||
while (b)
|
while (b)
|
||||||
(mlist++)->move = make_move(from, pop_lsb(&b));
|
*moveList++ = make_move(from, pop_lsb(&b));
|
||||||
}
|
}
|
||||||
|
|
||||||
return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, mlist, ~pos.pieces(), &ci)
|
return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, moveList, ~pos.pieces())
|
||||||
: generate_all<BLACK, QUIET_CHECKS>(pos, mlist, ~pos.pieces(), &ci);
|
: generate_all<BLACK, QUIET_CHECKS>(pos, moveList, ~pos.pieces());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// generate<EVASIONS> generates all pseudo-legal check evasions when the side
|
/// generate<EVASIONS> generates all pseudo-legal check evasions when the side
|
||||||
/// to move is in check. Returns a pointer to the end of the move list.
|
/// to move is in check. Returns a pointer to the end of the move list.
|
||||||
template<>
|
template<>
|
||||||
ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* mlist) {
|
ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* moveList) {
|
||||||
|
|
||||||
assert(pos.checkers());
|
assert(pos.checkers());
|
||||||
|
|
||||||
Color us = pos.side_to_move();
|
Color us = pos.side_to_move();
|
||||||
Square ksq = pos.king_square(us);
|
Square ksq = pos.square<KING>(us);
|
||||||
Bitboard sliderAttacks = 0;
|
Bitboard sliderAttacks = 0;
|
||||||
Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
|
Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
|
||||||
|
|
||||||
@@ -382,37 +384,37 @@ ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* mlist) {
|
|||||||
// Generate evasions for king, capture and non capture moves
|
// Generate evasions for king, capture and non capture moves
|
||||||
Bitboard b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
|
Bitboard b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
|
||||||
while (b)
|
while (b)
|
||||||
(mlist++)->move = make_move(ksq, pop_lsb(&b));
|
*moveList++ = make_move(ksq, pop_lsb(&b));
|
||||||
|
|
||||||
if (more_than_one(pos.checkers()))
|
if (more_than_one(pos.checkers()))
|
||||||
return mlist; // Double check, only a king move can save the day
|
return moveList; // Double check, only a king move can save the day
|
||||||
|
|
||||||
// Generate blocking evasions or captures of the checking piece
|
// Generate blocking evasions or captures of the checking piece
|
||||||
Square checksq = lsb(pos.checkers());
|
Square checksq = lsb(pos.checkers());
|
||||||
Bitboard target = between_bb(checksq, ksq) | checksq;
|
Bitboard target = between_bb(checksq, ksq) | checksq;
|
||||||
|
|
||||||
return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, mlist, target)
|
return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, moveList, target)
|
||||||
: generate_all<BLACK, EVASIONS>(pos, mlist, target);
|
: generate_all<BLACK, EVASIONS>(pos, moveList, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// generate<LEGAL> generates all the legal moves in the given position
|
/// generate<LEGAL> generates all the legal moves in the given position
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
ExtMove* generate<LEGAL>(const Position& pos, ExtMove* mlist) {
|
ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
|
||||||
|
|
||||||
ExtMove *end, *cur = mlist;
|
|
||||||
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
|
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
|
||||||
Square ksq = pos.king_square(pos.side_to_move());
|
Square ksq = pos.square<KING>(pos.side_to_move());
|
||||||
|
ExtMove* cur = moveList;
|
||||||
|
|
||||||
end = pos.checkers() ? generate<EVASIONS>(pos, mlist)
|
moveList = pos.checkers() ? generate<EVASIONS >(pos, moveList)
|
||||||
: generate<NON_EVASIONS>(pos, mlist);
|
: generate<NON_EVASIONS>(pos, moveList);
|
||||||
while (cur != end)
|
while (cur != moveList)
|
||||||
if ( (pinned || from_sq(cur->move) == ksq || type_of(cur->move) == ENPASSANT)
|
if ( (pinned || from_sq(*cur) == ksq || type_of(*cur) == ENPASSANT)
|
||||||
&& !pos.legal(cur->move, pinned))
|
&& !pos.legal(*cur))
|
||||||
cur->move = (--end)->move;
|
*cur = (--moveList)->move;
|
||||||
else
|
else
|
||||||
++cur;
|
++cur;
|
||||||
|
|
||||||
return end;
|
return moveList;
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-11
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -22,6 +23,8 @@
|
|||||||
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
class Position;
|
||||||
|
|
||||||
enum GenType {
|
enum GenType {
|
||||||
CAPTURES,
|
CAPTURES,
|
||||||
QUIETS,
|
QUIETS,
|
||||||
@@ -31,28 +34,37 @@ enum GenType {
|
|||||||
LEGAL
|
LEGAL
|
||||||
};
|
};
|
||||||
|
|
||||||
class Position;
|
struct ExtMove {
|
||||||
|
Move move;
|
||||||
|
Value value;
|
||||||
|
|
||||||
|
operator Move() const { return move; }
|
||||||
|
void operator=(Move m) { move = m; }
|
||||||
|
};
|
||||||
|
|
||||||
|
inline bool operator<(const ExtMove& f, const ExtMove& s) {
|
||||||
|
return f.value < s.value;
|
||||||
|
}
|
||||||
|
|
||||||
template<GenType>
|
template<GenType>
|
||||||
ExtMove* generate(const Position& pos, ExtMove* mlist);
|
ExtMove* generate(const Position& pos, ExtMove* moveList);
|
||||||
|
|
||||||
/// The MoveList struct is a simple wrapper around generate(). It sometimes comes
|
/// The MoveList struct is a simple wrapper around generate(). It sometimes comes
|
||||||
/// in handy to use this class instead of the low level generate() function.
|
/// in handy to use this class instead of the low level generate() function.
|
||||||
template<GenType T>
|
template<GenType T>
|
||||||
struct MoveList {
|
struct MoveList {
|
||||||
|
|
||||||
explicit MoveList(const Position& pos) : cur(mlist), last(generate<T>(pos, mlist)) { last->move = MOVE_NONE; }
|
explicit MoveList(const Position& pos) : last(generate<T>(pos, moveList)) {}
|
||||||
void operator++() { ++cur; }
|
const ExtMove* begin() const { return moveList; }
|
||||||
Move operator*() const { return cur->move; }
|
const ExtMove* end() const { return last; }
|
||||||
size_t size() const { return last - mlist; }
|
size_t size() const { return last - moveList; }
|
||||||
bool contains(Move m) const {
|
bool contains(Move move) const {
|
||||||
for (const ExtMove* it(mlist); it != last; ++it) if (it->move == m) return true;
|
for (const auto& m : *this) if (m == move) return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ExtMove mlist[MAX_MOVES];
|
ExtMove moveList[MAX_MOVES], *last;
|
||||||
ExtMove *cur, *last;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // #ifndef MOVEGEN_H_INCLUDED
|
#endif // #ifndef MOVEGEN_H_INCLUDED
|
||||||
|
|||||||
+217
-259
@@ -1,14 +1,14 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
(at your option) any later version.
|
(at your option) any later version.
|
||||||
|
|
||||||
|
|
||||||
Stockfish is distributed in the hope that it will be useful,
|
Stockfish is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
@@ -26,16 +26,15 @@
|
|||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
enum Stages {
|
enum Stages {
|
||||||
MAIN_SEARCH, CAPTURES_S1, KILLERS_S1, QUIETS_1_S1, QUIETS_2_S1, BAD_CAPTURES_S1,
|
MAIN_SEARCH, CAPTURES_INIT, GOOD_CAPTURES, KILLERS, COUNTERMOVE, QUIET_INIT, QUIET, BAD_CAPTURES,
|
||||||
EVASION, EVASIONS_S2,
|
EVASION, EVASIONS_INIT, ALL_EVASIONS,
|
||||||
QSEARCH_0, CAPTURES_S3, QUIET_CHECKS_S3,
|
PROBCUT, PROBCUT_INIT, PROBCUT_CAPTURES,
|
||||||
QSEARCH_1, CAPTURES_S4,
|
QSEARCH_WITH_CHECKS, QCAPTURES_1_INIT, QCAPTURES_1, QCHECKS,
|
||||||
PROBCUT, CAPTURES_S5,
|
QSEARCH_NO_CHECKS, QCAPTURES_2_INIT, QCAPTURES_2,
|
||||||
RECAPTURE, CAPTURES_S6,
|
QSEARCH_RECAPTURES, QRECAPTURES
|
||||||
STOP
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Our insertion sort, which is guaranteed (and also needed) to be stable
|
// Our insertion sort, which is guaranteed to be stable, as it should be
|
||||||
void insertion_sort(ExtMove* begin, ExtMove* end)
|
void insertion_sort(ExtMove* begin, ExtMove* end)
|
||||||
{
|
{
|
||||||
ExtMove tmp, *p, *q;
|
ExtMove tmp, *p, *q;
|
||||||
@@ -49,50 +48,39 @@ namespace {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unary predicate used by std::partition to split positive values from remaining
|
// pick_best() finds the best move in the range (begin, end) and moves it to
|
||||||
// ones so as to sort the two sets separately, with the second sort delayed.
|
// the front. It's faster than sorting all the moves in advance when there
|
||||||
inline bool has_positive_value(const ExtMove& ms) { return ms.value > 0; }
|
// are few moves, e.g., the possible captures.
|
||||||
|
Move pick_best(ExtMove* begin, ExtMove* end)
|
||||||
// Picks the best move in the range (begin, end) and moves it to the front.
|
|
||||||
// It's faster than sorting all the moves in advance when there are few
|
|
||||||
// moves e.g. possible captures.
|
|
||||||
inline ExtMove* pick_best(ExtMove* begin, ExtMove* end)
|
|
||||||
{
|
{
|
||||||
std::swap(*begin, *std::max_element(begin, end));
|
std::swap(*begin, *std::max_element(begin, end));
|
||||||
return begin;
|
return *begin;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
/// Constructors of the MovePicker class. As arguments we pass information
|
/// Constructors of the MovePicker class. As arguments we pass information
|
||||||
/// to help it to return the (presumably) good moves first, to decide which
|
/// to help it to return the (presumably) good moves first, to decide which
|
||||||
/// moves to return (in the quiescence search, for instance, we only want to
|
/// moves to return (in the quiescence search, for instance, we only want to
|
||||||
/// search captures, promotions and some checks) and how important good move
|
/// search captures, promotions, and some checks) and how important good move
|
||||||
/// ordering is at the current node.
|
/// ordering is at the current node.
|
||||||
|
|
||||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
|
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, Search::Stack* s)
|
||||||
Move* cm, Move* fm, Search::Stack* s) : pos(p), history(h), depth(d) {
|
: pos(p), ss(s), depth(d) {
|
||||||
|
|
||||||
assert(d > DEPTH_ZERO);
|
assert(d > DEPTH_ZERO);
|
||||||
|
|
||||||
cur = end = moves;
|
Square prevSq = to_sq((ss-1)->currentMove);
|
||||||
endBadCaptures = moves + MAX_MOVES - 1;
|
countermove = pos.this_thread()->counterMoves[pos.piece_on(prevSq)][prevSq];
|
||||||
countermoves = cm;
|
|
||||||
followupmoves = fm;
|
|
||||||
ss = s;
|
|
||||||
|
|
||||||
if (pos.checkers())
|
stage = pos.checkers() ? EVASION : MAIN_SEARCH;
|
||||||
stage = EVASION;
|
ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
|
||||||
|
stage += (ttMove == MOVE_NONE);
|
||||||
else
|
|
||||||
stage = MAIN_SEARCH;
|
|
||||||
|
|
||||||
ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
|
||||||
end += (ttMove != MOVE_NONE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
|
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, Square s)
|
||||||
Square s) : pos(p), history(h), cur(moves), end(moves) {
|
: pos(p) {
|
||||||
|
|
||||||
assert(d <= DEPTH_ZERO);
|
assert(d <= DEPTH_ZERO);
|
||||||
|
|
||||||
@@ -100,198 +88,88 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats&
|
|||||||
stage = EVASION;
|
stage = EVASION;
|
||||||
|
|
||||||
else if (d > DEPTH_QS_NO_CHECKS)
|
else if (d > DEPTH_QS_NO_CHECKS)
|
||||||
stage = QSEARCH_0;
|
stage = QSEARCH_WITH_CHECKS;
|
||||||
|
|
||||||
else if (d > DEPTH_QS_RECAPTURES)
|
else if (d > DEPTH_QS_RECAPTURES)
|
||||||
{
|
stage = QSEARCH_NO_CHECKS;
|
||||||
stage = QSEARCH_1;
|
|
||||||
|
|
||||||
// Skip TT move if is not a capture or a promotion. This avoids qsearch
|
|
||||||
// tree explosion due to a possible perpetual check or similar rare cases
|
|
||||||
// when TT table is full.
|
|
||||||
if (ttm && !pos.capture_or_promotion(ttm))
|
|
||||||
ttm = MOVE_NONE;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
stage = RECAPTURE;
|
stage = QSEARCH_RECAPTURES;
|
||||||
recaptureSquare = s;
|
recaptureSquare = s;
|
||||||
ttm = MOVE_NONE;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
ttMove = ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE;
|
||||||
end += (ttMove != MOVE_NONE);
|
stage += (ttMove == MOVE_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, PieceType pt)
|
MovePicker::MovePicker(const Position& p, Move ttm, Value th)
|
||||||
: pos(p), history(h), cur(moves), end(moves) {
|
: pos(p), threshold(th) {
|
||||||
|
|
||||||
assert(!pos.checkers());
|
assert(!pos.checkers());
|
||||||
|
|
||||||
stage = PROBCUT;
|
stage = PROBCUT;
|
||||||
|
|
||||||
// In ProbCut we generate only captures that are better than the parent's
|
// In ProbCut we generate captures with SEE higher than the given threshold
|
||||||
// captured piece.
|
ttMove = ttm
|
||||||
captureThreshold = PieceValue[MG][pt];
|
&& pos.pseudo_legal(ttm)
|
||||||
ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
&& pos.capture(ttm)
|
||||||
|
&& pos.see_ge(ttm, threshold + 1)? ttm : MOVE_NONE;
|
||||||
|
|
||||||
if (ttMove && (!pos.capture(ttMove) || pos.see(ttMove) <= captureThreshold))
|
stage += (ttMove == MOVE_NONE);
|
||||||
ttMove = MOVE_NONE;
|
|
||||||
|
|
||||||
end += (ttMove != MOVE_NONE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// score() assign a numerical value to each move in a move list. The moves with
|
/// score() assigns a numerical value to each move in a move list. The moves with
|
||||||
/// highest values will be picked first.
|
/// highest values will be picked first.
|
||||||
template<>
|
template<>
|
||||||
void MovePicker::score<CAPTURES>() {
|
void MovePicker::score<CAPTURES>() {
|
||||||
// Winning and equal captures in the main search are ordered by MVV/LVA.
|
// Winning and equal captures in the main search are ordered by MVV, preferring
|
||||||
// Suprisingly, this appears to perform slightly better than SEE based
|
// captures near our home rank. Surprisingly, this appears to perform slightly
|
||||||
// move ordering. The reason is probably that in a position with a winning
|
// better than SEE-based move ordering: exchanging big pieces before capturing
|
||||||
// capture, capturing a more valuable (but sufficiently defended) piece
|
// a hanging piece probably helps to reduce the subtree size.
|
||||||
// first usually doesn't hurt. The opponent will have to recapture, and
|
// In the main search we want to push captures with negative SEE values to the
|
||||||
// the hanging piece will still be hanging (except in the unusual cases
|
|
||||||
// where it is possible to recapture with the hanging piece). Exchanging
|
|
||||||
// big pieces before capturing a hanging piece probably helps to reduce
|
|
||||||
// the subtree size.
|
|
||||||
// In main search we want to push captures with negative SEE values to the
|
|
||||||
// badCaptures[] array, but instead of doing it now we delay until the move
|
// badCaptures[] array, but instead of doing it now we delay until the move
|
||||||
// has been picked up in pick_move_from_list(). This way we save some SEE
|
// has been picked up, saving some SEE calls in case we get a cutoff.
|
||||||
// calls in case we get a cutoff.
|
for (auto& m : *this)
|
||||||
Move m;
|
m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||||
|
- Value(200 * relative_rank(pos.side_to_move(), to_sq(m)));
|
||||||
for (ExtMove* it = moves; it != end; ++it)
|
|
||||||
{
|
|
||||||
m = it->move;
|
|
||||||
it->value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
|
||||||
- Value(type_of(pos.moved_piece(m)));
|
|
||||||
|
|
||||||
if (type_of(m) == ENPASSANT)
|
|
||||||
it->value += PieceValue[MG][PAWN];
|
|
||||||
|
|
||||||
else if (type_of(m) == PROMOTION)
|
|
||||||
it->value += PieceValue[MG][promotion_type(m)] - PieceValue[MG][PAWN];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
void MovePicker::score<QUIETS>() {
|
void MovePicker::score<QUIETS>() {
|
||||||
|
|
||||||
Move m;
|
const HistoryStats& history = pos.this_thread()->history;
|
||||||
|
const FromToStats& fromTo = pos.this_thread()->fromTo;
|
||||||
|
|
||||||
for (ExtMove* it = moves; it != end; ++it)
|
const CounterMoveStats* cm = (ss-1)->counterMoves;
|
||||||
{
|
const CounterMoveStats* fm = (ss-2)->counterMoves;
|
||||||
m = it->move;
|
const CounterMoveStats* f2 = (ss-4)->counterMoves;
|
||||||
it->value = history[pos.moved_piece(m)][to_sq(m)];
|
|
||||||
}
|
Color c = pos.side_to_move();
|
||||||
|
|
||||||
|
for (auto& m : *this)
|
||||||
|
m.value = history[pos.moved_piece(m)][to_sq(m)]
|
||||||
|
+ (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<>
|
template<>
|
||||||
void MovePicker::score<EVASIONS>() {
|
void MovePicker::score<EVASIONS>() {
|
||||||
// Try good captures ordered by MVV/LVA, then non-captures if destination square
|
// Try captures ordered by MVV/LVA, then non-captures ordered by history value
|
||||||
// is not under attack, ordered by history value, then bad-captures and quiet
|
const HistoryStats& history = pos.this_thread()->history;
|
||||||
// moves with a negative SEE. This last group is ordered by the SEE value.
|
const FromToStats& fromTo = pos.this_thread()->fromTo;
|
||||||
Move m;
|
Color c = pos.side_to_move();
|
||||||
Value see;
|
|
||||||
|
|
||||||
for (ExtMove* it = moves; it != end; ++it)
|
for (auto& m : *this)
|
||||||
{
|
if (pos.capture(m))
|
||||||
m = it->move;
|
m.value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||||
if ((see = pos.see_sign(m)) < VALUE_ZERO)
|
- Value(type_of(pos.moved_piece(m))) + HistoryStats::Max;
|
||||||
it->value = see - HistoryStats::Max; // At the bottom
|
|
||||||
|
|
||||||
else if (pos.capture(m))
|
|
||||||
it->value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
|
||||||
- Value(type_of(pos.moved_piece(m))) + HistoryStats::Max;
|
|
||||||
else
|
else
|
||||||
it->value = history[pos.moved_piece(m)][to_sq(m)];
|
m.value = history[pos.moved_piece(m)][to_sq(m)] + fromTo.get(c, m);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// generate_next_stage() generates, scores and sorts the next bunch of moves,
|
|
||||||
/// when there are no more moves to try for the current stage.
|
|
||||||
|
|
||||||
void MovePicker::generate_next_stage() {
|
|
||||||
|
|
||||||
cur = moves;
|
|
||||||
|
|
||||||
switch (++stage) {
|
|
||||||
|
|
||||||
case CAPTURES_S1: case CAPTURES_S3: case CAPTURES_S4: case CAPTURES_S5: case CAPTURES_S6:
|
|
||||||
end = generate<CAPTURES>(pos, moves);
|
|
||||||
score<CAPTURES>();
|
|
||||||
return;
|
|
||||||
|
|
||||||
case KILLERS_S1:
|
|
||||||
cur = killers;
|
|
||||||
end = cur + 2;
|
|
||||||
|
|
||||||
killers[0].move = ss->killers[0];
|
|
||||||
killers[1].move = ss->killers[1];
|
|
||||||
killers[2].move = killers[3].move = MOVE_NONE;
|
|
||||||
killers[4].move = killers[5].move = MOVE_NONE;
|
|
||||||
|
|
||||||
// Please note that following code is racy and could yield to rare (less
|
|
||||||
// than 1 out of a million) duplicated entries in SMP case. This is harmless.
|
|
||||||
|
|
||||||
// Be sure countermoves are different from killers
|
|
||||||
for (int i = 0; i < 2; ++i)
|
|
||||||
if ( countermoves[i] != (cur+0)->move
|
|
||||||
&& countermoves[i] != (cur+1)->move)
|
|
||||||
(end++)->move = countermoves[i];
|
|
||||||
|
|
||||||
// Be sure followupmoves are different from killers and countermoves
|
|
||||||
for (int i = 0; i < 2; ++i)
|
|
||||||
if ( followupmoves[i] != (cur+0)->move
|
|
||||||
&& followupmoves[i] != (cur+1)->move
|
|
||||||
&& followupmoves[i] != (cur+2)->move
|
|
||||||
&& followupmoves[i] != (cur+3)->move)
|
|
||||||
(end++)->move = followupmoves[i];
|
|
||||||
return;
|
|
||||||
|
|
||||||
case QUIETS_1_S1:
|
|
||||||
endQuiets = end = generate<QUIETS>(pos, moves);
|
|
||||||
score<QUIETS>();
|
|
||||||
end = std::partition(cur, end, has_positive_value);
|
|
||||||
insertion_sort(cur, end);
|
|
||||||
return;
|
|
||||||
|
|
||||||
case QUIETS_2_S1:
|
|
||||||
cur = end;
|
|
||||||
end = endQuiets;
|
|
||||||
if (depth >= 3 * ONE_PLY)
|
|
||||||
insertion_sort(cur, end);
|
|
||||||
return;
|
|
||||||
|
|
||||||
case BAD_CAPTURES_S1:
|
|
||||||
// Just pick them in reverse order to get MVV/LVA ordering
|
|
||||||
cur = moves + MAX_MOVES - 1;
|
|
||||||
end = endBadCaptures;
|
|
||||||
return;
|
|
||||||
|
|
||||||
case EVASIONS_S2:
|
|
||||||
end = generate<EVASIONS>(pos, moves);
|
|
||||||
if (end > moves + 1)
|
|
||||||
score<EVASIONS>();
|
|
||||||
return;
|
|
||||||
|
|
||||||
case QUIET_CHECKS_S3:
|
|
||||||
end = generate<QUIET_CHECKS>(pos, moves);
|
|
||||||
return;
|
|
||||||
|
|
||||||
case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT: case RECAPTURE:
|
|
||||||
stage = STOP;
|
|
||||||
/* Fall through */
|
|
||||||
|
|
||||||
case STOP:
|
|
||||||
end = cur + 1; // Avoid another next_phase() call
|
|
||||||
return;
|
|
||||||
|
|
||||||
default:
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -299,94 +177,174 @@ void MovePicker::generate_next_stage() {
|
|||||||
/// a new pseudo legal move every time it is called, until there are no more moves
|
/// 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
|
/// left. It picks the move with the biggest value from a list of generated moves
|
||||||
/// taking care not to return the ttMove if it has already been searched.
|
/// taking care not to return the ttMove if it has already been searched.
|
||||||
template<>
|
|
||||||
Move MovePicker::next_move<false>() {
|
Move MovePicker::next_move() {
|
||||||
|
|
||||||
Move move;
|
Move move;
|
||||||
|
|
||||||
while (true)
|
switch (stage) {
|
||||||
{
|
|
||||||
while (cur == end)
|
|
||||||
generate_next_stage();
|
|
||||||
|
|
||||||
switch (stage) {
|
case MAIN_SEARCH: case EVASION: case QSEARCH_WITH_CHECKS:
|
||||||
|
case QSEARCH_NO_CHECKS: case PROBCUT:
|
||||||
|
++stage;
|
||||||
|
return ttMove;
|
||||||
|
|
||||||
case MAIN_SEARCH: case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT:
|
case CAPTURES_INIT:
|
||||||
++cur;
|
endBadCaptures = cur = moves;
|
||||||
return ttMove;
|
endMoves = generate<CAPTURES>(pos, cur);
|
||||||
|
score<CAPTURES>();
|
||||||
|
++stage;
|
||||||
|
|
||||||
case CAPTURES_S1:
|
case GOOD_CAPTURES:
|
||||||
move = pick_best(cur++, end)->move;
|
while (cur < endMoves)
|
||||||
|
{
|
||||||
|
move = pick_best(cur++, endMoves);
|
||||||
if (move != ttMove)
|
if (move != ttMove)
|
||||||
{
|
{
|
||||||
if (pos.see_sign(move) >= VALUE_ZERO)
|
if (pos.see_ge(move, VALUE_ZERO))
|
||||||
return move;
|
return move;
|
||||||
|
|
||||||
// Losing capture, move it to the tail of the array
|
// Losing capture, move it to the beginning of the array
|
||||||
(endBadCaptures--)->move = move;
|
*endBadCaptures++ = move;
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
|
|
||||||
case KILLERS_S1:
|
++stage;
|
||||||
move = (cur++)->move;
|
move = ss->killers[0]; // First killer move
|
||||||
if ( move != MOVE_NONE
|
if ( move != MOVE_NONE
|
||||||
&& move != ttMove
|
&& move != ttMove
|
||||||
&& pos.pseudo_legal(move)
|
&& pos.pseudo_legal(move)
|
||||||
&& !pos.capture(move))
|
&& !pos.capture(move))
|
||||||
return move;
|
return move;
|
||||||
break;
|
|
||||||
|
|
||||||
case QUIETS_1_S1: case QUIETS_2_S1:
|
case KILLERS:
|
||||||
move = (cur++)->move;
|
++stage;
|
||||||
|
move = ss->killers[1]; // Second killer move
|
||||||
|
if ( move != MOVE_NONE
|
||||||
|
&& move != ttMove
|
||||||
|
&& pos.pseudo_legal(move)
|
||||||
|
&& !pos.capture(move))
|
||||||
|
return move;
|
||||||
|
|
||||||
|
case COUNTERMOVE:
|
||||||
|
++stage;
|
||||||
|
move = countermove;
|
||||||
|
if ( move != MOVE_NONE
|
||||||
|
&& move != ttMove
|
||||||
|
&& move != ss->killers[0]
|
||||||
|
&& move != ss->killers[1]
|
||||||
|
&& pos.pseudo_legal(move)
|
||||||
|
&& !pos.capture(move))
|
||||||
|
return move;
|
||||||
|
|
||||||
|
case QUIET_INIT:
|
||||||
|
cur = endBadCaptures;
|
||||||
|
endMoves = generate<QUIETS>(pos, cur);
|
||||||
|
score<QUIETS>();
|
||||||
|
if (depth < 3 * 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;
|
||||||
|
|
||||||
|
case QUIET:
|
||||||
|
while (cur < endMoves)
|
||||||
|
{
|
||||||
|
move = *cur++;
|
||||||
if ( move != ttMove
|
if ( move != ttMove
|
||||||
&& move != killers[0].move
|
&& move != ss->killers[0]
|
||||||
&& move != killers[1].move
|
&& move != ss->killers[1]
|
||||||
&& move != killers[2].move
|
&& move != countermove)
|
||||||
&& move != killers[3].move
|
|
||||||
&& move != killers[4].move
|
|
||||||
&& move != killers[5].move)
|
|
||||||
return move;
|
return move;
|
||||||
break;
|
}
|
||||||
|
++stage;
|
||||||
|
cur = moves; // Point to beginning of bad captures
|
||||||
|
|
||||||
case BAD_CAPTURES_S1:
|
case BAD_CAPTURES:
|
||||||
return (cur--)->move;
|
if (cur < endBadCaptures)
|
||||||
|
return *cur++;
|
||||||
|
break;
|
||||||
|
|
||||||
case EVASIONS_S2: case CAPTURES_S3: case CAPTURES_S4:
|
case EVASIONS_INIT:
|
||||||
move = pick_best(cur++, end)->move;
|
cur = moves;
|
||||||
|
endMoves = generate<EVASIONS>(pos, cur);
|
||||||
|
score<EVASIONS>();
|
||||||
|
++stage;
|
||||||
|
|
||||||
|
case ALL_EVASIONS:
|
||||||
|
while (cur < endMoves)
|
||||||
|
{
|
||||||
|
move = pick_best(cur++, endMoves);
|
||||||
if (move != ttMove)
|
if (move != ttMove)
|
||||||
return move;
|
return move;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PROBCUT_INIT:
|
||||||
|
cur = moves;
|
||||||
|
endMoves = generate<CAPTURES>(pos, cur);
|
||||||
|
score<CAPTURES>();
|
||||||
|
++stage;
|
||||||
|
|
||||||
|
case PROBCUT_CAPTURES:
|
||||||
|
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;
|
break;
|
||||||
|
cur = moves;
|
||||||
|
endMoves = generate<QUIET_CHECKS>(pos, cur);
|
||||||
|
++stage;
|
||||||
|
|
||||||
case CAPTURES_S5:
|
case QCHECKS:
|
||||||
move = pick_best(cur++, end)->move;
|
while (cur < endMoves)
|
||||||
if (move != ttMove && pos.see(move) > captureThreshold)
|
{
|
||||||
return move;
|
move = cur++->move;
|
||||||
break;
|
if (move != ttMove)
|
||||||
|
return move;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case CAPTURES_S6:
|
case QSEARCH_RECAPTURES:
|
||||||
move = pick_best(cur++, end)->move;
|
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)
|
if (to_sq(move) == recaptureSquare)
|
||||||
return move;
|
return move;
|
||||||
break;
|
|
||||||
|
|
||||||
case QUIET_CHECKS_S3:
|
|
||||||
move = (cur++)->move;
|
|
||||||
if (move != ttMove)
|
|
||||||
return move;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case STOP:
|
|
||||||
return MOVE_NONE;
|
|
||||||
|
|
||||||
default:
|
|
||||||
assert(false);
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
assert(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return MOVE_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Version of next_move() to use at split point nodes where the move is grabbed
|
|
||||||
/// from the split point's shared MovePicker object. This function is not thread
|
|
||||||
/// safe so must be lock protected by the caller.
|
|
||||||
template<>
|
|
||||||
Move MovePicker::next_move<true>() { return ss->splitPoint->movePicker->next_move<false>(); }
|
|
||||||
|
|||||||
+50
-39
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,51 +26,62 @@
|
|||||||
|
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "search.h"
|
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
|
||||||
/// The Stats struct stores moves statistics. According to the template parameter
|
/// The Stats struct stores moves statistics. According to the template parameter
|
||||||
/// the class can store History, Gains and Countermoves. History records how often
|
/// the class can store History and Countermoves. History records how often
|
||||||
/// different moves have been successful or unsuccessful during the current search
|
/// different moves have been successful or unsuccessful during the current search
|
||||||
/// and is used for reduction and move ordering decisions. Gains records the move's
|
/// and is used for reduction and move ordering decisions.
|
||||||
/// best evaluation gain from one ply to the next and is used for pruning decisions.
|
|
||||||
/// Countermoves store the move that refute a previous one. Entries are stored
|
/// Countermoves store the move that refute a previous one. Entries are stored
|
||||||
/// using only the moving piece and destination square, hence two moves with
|
/// using only the moving piece and destination square, hence two moves with
|
||||||
/// different origin but same destination and piece will be considered identical.
|
/// different origin but same destination and piece will be considered identical.
|
||||||
template<bool Gain, typename T>
|
template<typename T, bool CM = false>
|
||||||
struct Stats {
|
struct Stats {
|
||||||
|
|
||||||
static const Value Max = Value(2000);
|
static const Value Max = Value(1 << 28);
|
||||||
|
|
||||||
const T* operator[](Piece pc) const { return table[pc]; }
|
const T* operator[](Piece pc) const { return table[pc]; }
|
||||||
|
T* operator[](Piece pc) { return table[pc]; }
|
||||||
void clear() { std::memset(table, 0, sizeof(table)); }
|
void clear() { std::memset(table, 0, sizeof(table)); }
|
||||||
|
void update(Piece pc, Square to, Move m) { table[pc][to] = m; }
|
||||||
void update(Piece pc, Square to, Move m) {
|
|
||||||
|
|
||||||
if (m == table[pc][to].first)
|
|
||||||
return;
|
|
||||||
|
|
||||||
table[pc][to].second = table[pc][to].first;
|
|
||||||
table[pc][to].first = m;
|
|
||||||
}
|
|
||||||
|
|
||||||
void update(Piece pc, Square to, Value v) {
|
void update(Piece pc, Square to, Value v) {
|
||||||
|
|
||||||
if (Gain)
|
if (abs(int(v)) >= 324)
|
||||||
table[pc][to] = std::max(v, table[pc][to] - 1);
|
return;
|
||||||
|
|
||||||
else if (abs(table[pc][to] + v) < Max)
|
table[pc][to] -= table[pc][to] * abs(int(v)) / (CM ? 936 : 324);
|
||||||
table[pc][to] += v;
|
table[pc][to] += int(v) * 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T table[PIECE_NB][SQUARE_NB];
|
T table[PIECE_NB][SQUARE_NB];
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef Stats< true, Value> GainsStats;
|
typedef Stats<Move> MoveStats;
|
||||||
typedef Stats<false, Value> HistoryStats;
|
typedef Stats<Value, false> HistoryStats;
|
||||||
typedef Stats<false, std::pair<Move, Move> > MovesStats;
|
typedef Stats<Value, true> CounterMoveStats;
|
||||||
|
typedef Stats<CounterMoveStats> CounterMoveHistoryStats;
|
||||||
|
|
||||||
|
struct FromToStats {
|
||||||
|
|
||||||
|
Value get(Color c, Move m) const { return table[c][from_sq(m)][to_sq(m)]; }
|
||||||
|
void clear() { std::memset(table, 0, sizeof(table)); }
|
||||||
|
void update(Color c, Move m, Value v) {
|
||||||
|
|
||||||
|
if (abs(int(v)) >= 324)
|
||||||
|
return;
|
||||||
|
|
||||||
|
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];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/// 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
|
||||||
@@ -78,34 +90,33 @@ typedef Stats<false, std::pair<Move, Move> > MovesStats;
|
|||||||
/// 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 {
|
||||||
|
|
||||||
MovePicker& operator=(const MovePicker&); // Silence a warning under MSVC
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MovePicker(const Position&, Move, Depth, const HistoryStats&, Square);
|
MovePicker(const MovePicker&) = delete;
|
||||||
MovePicker(const Position&, Move, const HistoryStats&, PieceType);
|
MovePicker& operator=(const MovePicker&) = delete;
|
||||||
MovePicker(const Position&, Move, Depth, const HistoryStats&, Move*, Move*, Search::Stack*);
|
|
||||||
|
|
||||||
template<bool SpNode> Move next_move();
|
MovePicker(const Position&, Move, Value);
|
||||||
|
MovePicker(const Position&, Move, Depth, Square);
|
||||||
|
MovePicker(const Position&, Move, Depth, Search::Stack*);
|
||||||
|
|
||||||
|
Move next_move();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template<GenType> void score();
|
template<GenType> void score();
|
||||||
void generate_next_stage();
|
ExtMove* begin() { return cur; }
|
||||||
|
ExtMove* end() { return endMoves; }
|
||||||
|
|
||||||
const Position& pos;
|
const Position& pos;
|
||||||
const HistoryStats& history;
|
const Search::Stack* ss;
|
||||||
Search::Stack* ss;
|
Move countermove;
|
||||||
Move* countermoves;
|
|
||||||
Move* followupmoves;
|
|
||||||
Depth depth;
|
Depth depth;
|
||||||
Move ttMove;
|
Move ttMove;
|
||||||
ExtMove killers[6];
|
|
||||||
Square recaptureSquare;
|
Square recaptureSquare;
|
||||||
Value captureThreshold;
|
Value threshold;
|
||||||
int stage;
|
int stage;
|
||||||
ExtMove *cur, *end, *endQuiets, *endBadCaptures;
|
ExtMove *cur, *endMoves, *endBadCaptures;
|
||||||
ExtMove moves[MAX_MOVES];
|
ExtMove moves[MAX_MOVES];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,256 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
|
||||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
Stockfish is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <cassert>
|
|
||||||
#include <iomanip>
|
|
||||||
#include <sstream>
|
|
||||||
#include <stack>
|
|
||||||
|
|
||||||
#include "movegen.h"
|
|
||||||
#include "notation.h"
|
|
||||||
#include "position.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
static const char* PieceToChar[COLOR_NB] = { " PNBRQK", " pnbrqk" };
|
|
||||||
|
|
||||||
|
|
||||||
/// score_to_uci() converts a value to a string suitable for use with the UCI
|
|
||||||
/// protocol specifications:
|
|
||||||
///
|
|
||||||
/// cp <x> The score from the engine's point of view in centipawns.
|
|
||||||
/// mate <y> Mate in y moves, not plies. If the engine is getting mated
|
|
||||||
/// use negative values for y.
|
|
||||||
|
|
||||||
string score_to_uci(Value v, Value alpha, Value beta) {
|
|
||||||
|
|
||||||
stringstream ss;
|
|
||||||
|
|
||||||
if (abs(v) < VALUE_MATE_IN_MAX_PLY)
|
|
||||||
ss << "cp " << v * 100 / PawnValueEg;
|
|
||||||
else
|
|
||||||
ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
|
|
||||||
|
|
||||||
ss << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
|
|
||||||
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// move_to_uci() converts a move to a string in coordinate notation
|
|
||||||
/// (g1f3, a7a8q, etc.). The only special case is castling moves, where we print
|
|
||||||
/// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960
|
|
||||||
/// mode. Internally castling moves are always encoded as "king captures rook".
|
|
||||||
|
|
||||||
const string move_to_uci(Move m, bool chess960) {
|
|
||||||
|
|
||||||
Square from = from_sq(m);
|
|
||||||
Square to = to_sq(m);
|
|
||||||
|
|
||||||
if (m == MOVE_NONE)
|
|
||||||
return "(none)";
|
|
||||||
|
|
||||||
if (m == MOVE_NULL)
|
|
||||||
return "0000";
|
|
||||||
|
|
||||||
if (type_of(m) == CASTLING && !chess960)
|
|
||||||
to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
|
|
||||||
|
|
||||||
string move = to_string(from) + to_string(to);
|
|
||||||
|
|
||||||
if (type_of(m) == PROMOTION)
|
|
||||||
move += PieceToChar[BLACK][promotion_type(m)]; // Lower case
|
|
||||||
|
|
||||||
return move;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// move_from_uci() takes a position and a string representing a move in
|
|
||||||
/// simple coordinate notation and returns an equivalent legal Move if any.
|
|
||||||
|
|
||||||
Move move_from_uci(const Position& pos, string& str) {
|
|
||||||
|
|
||||||
if (str.length() == 5) // Junior could send promotion piece in uppercase
|
|
||||||
str[4] = char(tolower(str[4]));
|
|
||||||
|
|
||||||
for (MoveList<LEGAL> it(pos); *it; ++it)
|
|
||||||
if (str == move_to_uci(*it, pos.is_chess960()))
|
|
||||||
return *it;
|
|
||||||
|
|
||||||
return MOVE_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// move_to_san() takes a position and a legal Move as input and returns its
|
|
||||||
/// short algebraic notation representation.
|
|
||||||
|
|
||||||
const string move_to_san(Position& pos, Move m) {
|
|
||||||
|
|
||||||
if (m == MOVE_NONE)
|
|
||||||
return "(none)";
|
|
||||||
|
|
||||||
if (m == MOVE_NULL)
|
|
||||||
return "(null)";
|
|
||||||
|
|
||||||
assert(MoveList<LEGAL>(pos).contains(m));
|
|
||||||
|
|
||||||
Bitboard others, b;
|
|
||||||
string san;
|
|
||||||
Color us = pos.side_to_move();
|
|
||||||
Square from = from_sq(m);
|
|
||||||
Square to = to_sq(m);
|
|
||||||
Piece pc = pos.piece_on(from);
|
|
||||||
PieceType pt = type_of(pc);
|
|
||||||
|
|
||||||
if (type_of(m) == CASTLING)
|
|
||||||
san = to > from ? "O-O" : "O-O-O";
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (pt != PAWN)
|
|
||||||
{
|
|
||||||
san = PieceToChar[WHITE][pt]; // Upper case
|
|
||||||
|
|
||||||
// A disambiguation occurs if we have more then one piece of type 'pt'
|
|
||||||
// that can reach 'to' with a legal move.
|
|
||||||
others = b = (pos.attacks_from(pc, to) & pos.pieces(us, pt)) ^ from;
|
|
||||||
|
|
||||||
while (b)
|
|
||||||
{
|
|
||||||
Square s = pop_lsb(&b);
|
|
||||||
if (!pos.legal(make_move(s, to), pos.pinned_pieces(us)))
|
|
||||||
others ^= s;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!others)
|
|
||||||
{ /* Disambiguation is not needed */ }
|
|
||||||
|
|
||||||
else if (!(others & file_bb(from)))
|
|
||||||
san += to_char(file_of(from));
|
|
||||||
|
|
||||||
else if (!(others & rank_bb(from)))
|
|
||||||
san += to_char(rank_of(from));
|
|
||||||
|
|
||||||
else
|
|
||||||
san += to_string(from);
|
|
||||||
}
|
|
||||||
else if (pos.capture(m))
|
|
||||||
san = to_char(file_of(from));
|
|
||||||
|
|
||||||
if (pos.capture(m))
|
|
||||||
san += 'x';
|
|
||||||
|
|
||||||
san += to_string(to);
|
|
||||||
|
|
||||||
if (type_of(m) == PROMOTION)
|
|
||||||
san += string("=") + PieceToChar[WHITE][promotion_type(m)];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pos.gives_check(m, CheckInfo(pos)))
|
|
||||||
{
|
|
||||||
StateInfo st;
|
|
||||||
pos.do_move(m, st);
|
|
||||||
san += MoveList<LEGAL>(pos).size() ? "+" : "#";
|
|
||||||
pos.undo_move(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
return san;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// pretty_pv() formats human-readable search information, typically to be
|
|
||||||
/// appended to the search log file. It uses the two helpers below to pretty
|
|
||||||
/// format the time and score respectively.
|
|
||||||
|
|
||||||
static string format(int64_t msecs) {
|
|
||||||
|
|
||||||
const int MSecMinute = 1000 * 60;
|
|
||||||
const int MSecHour = 1000 * 60 * 60;
|
|
||||||
|
|
||||||
int64_t hours = msecs / MSecHour;
|
|
||||||
int64_t minutes = (msecs % MSecHour) / MSecMinute;
|
|
||||||
int64_t seconds = ((msecs % MSecHour) % MSecMinute) / 1000;
|
|
||||||
|
|
||||||
stringstream ss;
|
|
||||||
|
|
||||||
if (hours)
|
|
||||||
ss << hours << ':';
|
|
||||||
|
|
||||||
ss << setfill('0') << setw(2) << minutes << ':' << setw(2) << seconds;
|
|
||||||
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
static string format(Value v) {
|
|
||||||
|
|
||||||
stringstream ss;
|
|
||||||
|
|
||||||
if (v >= VALUE_MATE_IN_MAX_PLY)
|
|
||||||
ss << "#" << (VALUE_MATE - v + 1) / 2;
|
|
||||||
|
|
||||||
else if (v <= VALUE_MATED_IN_MAX_PLY)
|
|
||||||
ss << "-#" << (VALUE_MATE + v) / 2;
|
|
||||||
|
|
||||||
else
|
|
||||||
ss << setprecision(2) << fixed << showpos << double(v) / PawnValueEg;
|
|
||||||
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|
||||||
string pretty_pv(Position& pos, int depth, Value value, int64_t msecs, Move pv[]) {
|
|
||||||
|
|
||||||
const uint64_t K = 1000;
|
|
||||||
const uint64_t M = 1000000;
|
|
||||||
|
|
||||||
std::stack<StateInfo> st;
|
|
||||||
Move* m = pv;
|
|
||||||
string san, str, padding;
|
|
||||||
stringstream ss;
|
|
||||||
|
|
||||||
ss << setw(2) << depth << setw(8) << format(value) << setw(8) << format(msecs);
|
|
||||||
|
|
||||||
if (pos.nodes_searched() < M)
|
|
||||||
ss << setw(8) << pos.nodes_searched() / 1 << " ";
|
|
||||||
|
|
||||||
else if (pos.nodes_searched() < K * M)
|
|
||||||
ss << setw(7) << pos.nodes_searched() / K << "K ";
|
|
||||||
|
|
||||||
else
|
|
||||||
ss << setw(7) << pos.nodes_searched() / M << "M ";
|
|
||||||
|
|
||||||
str = ss.str();
|
|
||||||
padding = string(str.length(), ' ');
|
|
||||||
|
|
||||||
while (*m != MOVE_NONE)
|
|
||||||
{
|
|
||||||
san = move_to_san(pos, *m) + ' ';
|
|
||||||
|
|
||||||
if ((str.length() + san.length()) % 80 <= san.length()) // Exceed 80 cols
|
|
||||||
str += "\n" + padding;
|
|
||||||
|
|
||||||
str += san;
|
|
||||||
|
|
||||||
st.push(StateInfo());
|
|
||||||
pos.do_move(*m++, st.top());
|
|
||||||
}
|
|
||||||
|
|
||||||
while (m != pv)
|
|
||||||
pos.undo_move(*--m);
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
|
||||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
Stockfish is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef NOTATION_H_INCLUDED
|
|
||||||
#define NOTATION_H_INCLUDED
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
class Position;
|
|
||||||
|
|
||||||
std::string score_to_uci(Value v, Value alpha = -VALUE_INFINITE, Value beta = VALUE_INFINITE);
|
|
||||||
Move move_from_uci(const Position& pos, std::string& str);
|
|
||||||
const std::string move_to_uci(Move m, bool chess960);
|
|
||||||
const std::string move_to_san(Position& pos, Move m);
|
|
||||||
std::string pretty_pv(Position& pos, int depth, Value score, int64_t msecs, Move pv[]);
|
|
||||||
|
|
||||||
#endif // #ifndef NOTATION_H_INCLUDED
|
|
||||||
+134
-159
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,62 +22,67 @@
|
|||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
#include "bitcount.h"
|
|
||||||
#include "pawns.h"
|
#include "pawns.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
|
#include "thread.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
#define V Value
|
#define V Value
|
||||||
#define S(mg, eg) make_score(mg, eg)
|
#define S(mg, eg) make_score(mg, eg)
|
||||||
|
|
||||||
// Doubled pawn penalty by file
|
// Isolated pawn penalty by opposed flag
|
||||||
const Score Doubled[FILE_NB] = {
|
const Score Isolated[2] = { S(45, 40), S(30, 27) };
|
||||||
S(13, 43), S(20, 48), S(23, 48), S(23, 48),
|
|
||||||
S(23, 48), S(23, 48), S(20, 48), S(13, 43) };
|
|
||||||
|
|
||||||
// Isolated pawn penalty by opposed flag and file
|
// Backward pawn penalty by opposed flag
|
||||||
const Score Isolated[2][FILE_NB] = {
|
const Score Backward[2] = { S(56, 33), S(41, 19) };
|
||||||
{ S(37, 45), S(54, 52), S(60, 52), S(60, 52),
|
|
||||||
S(60, 52), S(60, 52), S(54, 52), S(37, 45) },
|
|
||||||
{ S(25, 30), S(36, 35), S(40, 35), S(40, 35),
|
|
||||||
S(40, 35), S(40, 35), S(36, 35), S(25, 30) } };
|
|
||||||
|
|
||||||
// Backward pawn penalty by opposed flag and file
|
// Unsupported pawn penalty for pawns which are neither isolated or backward
|
||||||
const Score Backward[2][FILE_NB] = {
|
const Score Unsupported = S(17, 8);
|
||||||
{ S(30, 42), S(43, 46), S(49, 46), S(49, 46),
|
|
||||||
S(49, 46), S(49, 46), S(43, 46), S(30, 42) },
|
|
||||||
{ S(20, 28), S(29, 31), S(33, 31), S(33, 31),
|
|
||||||
S(33, 31), S(33, 31), S(29, 31), S(20, 28) } };
|
|
||||||
|
|
||||||
// Connected pawn bonus by file and rank (initialized by formula)
|
// Connected pawn bonus by opposed, phalanx, twice supported and rank
|
||||||
Score Connected[FILE_NB][RANK_NB];
|
Score Connected[2][2][2][RANK_NB];
|
||||||
|
|
||||||
// Candidate passed pawn bonus by rank
|
// Doubled pawn penalty
|
||||||
const Score CandidatePassed[RANK_NB] = {
|
const Score Doubled = S(18,38);
|
||||||
S( 0, 0), S( 6, 13), S(6,13), S(14,29),
|
|
||||||
S(34,68), S(83,166), S(0, 0), S( 0, 0) };
|
|
||||||
|
|
||||||
// Bonus for file distance of the two outermost pawns
|
// Lever bonus by rank
|
||||||
const Score PawnsFileSpan = S(0, 15);
|
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)
|
||||||
|
};
|
||||||
|
|
||||||
// Unsupported pawn penalty
|
// Weakness of our pawn shelter in front of the king by [distance from edge][rank]
|
||||||
const Score UnsupportedPawnPenalty = S(20, 10);
|
const Value ShelterWeakness[][RANK_NB] = {
|
||||||
|
{ V( 97), V(21), V(26), V(51), V(87), V( 89), V( 99) },
|
||||||
|
{ V(120), V( 0), V(28), V(76), V(88), V(103), V(104) },
|
||||||
|
{ V(101), V( 7), V(54), V(78), V(77), V( 92), V(101) },
|
||||||
|
{ V( 80), V(11), V(44), V(68), V(87), V( 90), V(119) }
|
||||||
|
};
|
||||||
|
|
||||||
// Weakness of our pawn shelter in front of the king indexed by [rank]
|
// Danger of enemy pawns moving toward our king by [type][distance from edge][rank]
|
||||||
const Value ShelterWeakness[RANK_NB] =
|
const Value StormDanger[][4][RANK_NB] = {
|
||||||
{ V(100), V(0), V(27), V(73), V(92), V(101), V(101) };
|
{ { V( 0), V( 67), V( 134), V(38), V(32) },
|
||||||
|
{ V( 0), V( 57), V( 139), V(37), V(22) },
|
||||||
// Danger of enemy pawns moving toward our king indexed by
|
{ V( 0), V( 43), V( 115), V(43), V(27) },
|
||||||
// [no friendly pawn | pawn unblocked | pawn blocked][rank of enemy pawn]
|
{ V( 0), V( 68), V( 124), V(57), V(32) } },
|
||||||
const Value StormDanger[3][RANK_NB] = {
|
{ { V(20), V( 43), V( 100), V(56), V(20) },
|
||||||
{ V( 0), V(64), V(128), V(51), V(26) },
|
{ V(23), V( 20), V( 98), V(40), V(15) },
|
||||||
{ V(26), V(32), V( 96), V(38), V(20) },
|
{ V(23), V( 39), V( 103), V(36), V(18) },
|
||||||
{ V( 0), V( 0), V(160), V(25), V(13) } };
|
{ 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
|
// 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.
|
// in front of the king and no enemy pawn on the horizon.
|
||||||
const Value MaxSafetyBonus = V(263);
|
const Value MaxSafetyBonus = V(258);
|
||||||
|
|
||||||
#undef S
|
#undef S
|
||||||
#undef V
|
#undef V
|
||||||
@@ -84,26 +90,26 @@ namespace {
|
|||||||
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);
|
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||||
const Square Up = (Us == WHITE ? DELTA_N : DELTA_S);
|
const Square Up = (Us == WHITE ? NORTH : SOUTH);
|
||||||
const Square Right = (Us == WHITE ? DELTA_NE : DELTA_SW);
|
const Square Right = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
|
||||||
const Square Left = (Us == WHITE ? DELTA_NW : DELTA_SE);
|
const Square Left = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
|
||||||
|
|
||||||
Bitboard b, p, doubled;
|
Bitboard b, neighbours, stoppers, doubled, supported, phalanx;
|
||||||
Square s;
|
Square s;
|
||||||
File f;
|
bool opposed, lever, connected, backward;
|
||||||
bool passed, isolated, opposed, connected, backward, candidate, unsupported;
|
Score score = SCORE_ZERO;
|
||||||
Score value = SCORE_ZERO;
|
const Square* pl = pos.squares<PAWN>(Us);
|
||||||
const Square* pl = pos.list<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->candidatePawns[Us] = 0;
|
e->passedPawns[Us] = e->pawnAttacksSpan[Us] = 0;
|
||||||
e->kingSquares[Us] = SQ_NONE;
|
|
||||||
e->semiopenFiles[Us] = 0xFF;
|
e->semiopenFiles[Us] = 0xFF;
|
||||||
e->pawnAttacks[Us] = shift_bb<Right>(ourPawns) | shift_bb<Left>(ourPawns);
|
e->kingSquares[Us] = SQ_NONE;
|
||||||
e->pawnsOnSquares[Us][BLACK] = popcount<Max15>(ourPawns & DarkSquares);
|
e->pawnAttacks[Us] = shift<Right>(ourPawns) | shift<Left>(ourPawns);
|
||||||
|
e->pawnsOnSquares[Us][BLACK] = popcount(ourPawns & DarkSquares);
|
||||||
e->pawnsOnSquares[Us][WHITE] = pos.count<PAWN>(Us) - e->pawnsOnSquares[Us][BLACK];
|
e->pawnsOnSquares[Us][WHITE] = pos.count<PAWN>(Us) - e->pawnsOnSquares[Us][BLACK];
|
||||||
|
|
||||||
// Loop through all pawns of the current color and score each pawn
|
// Loop through all pawns of the current color and score each pawn
|
||||||
@@ -111,134 +117,107 @@ namespace {
|
|||||||
{
|
{
|
||||||
assert(pos.piece_on(s) == make_piece(Us, PAWN));
|
assert(pos.piece_on(s) == make_piece(Us, PAWN));
|
||||||
|
|
||||||
f = file_of(s);
|
File f = file_of(s);
|
||||||
|
|
||||||
// This file cannot be semi-open
|
e->semiopenFiles[Us] &= ~(1 << f);
|
||||||
e->semiopenFiles[Us] &= ~(1 << f);
|
e->pawnAttacksSpan[Us] |= pawn_attack_span(Us, s);
|
||||||
|
|
||||||
// Previous rank
|
// Flag the pawn
|
||||||
p = rank_bb(s - pawn_push(Us));
|
opposed = theirPawns & forward_bb(Us, s);
|
||||||
|
stoppers = theirPawns & passed_pawn_mask(Us, s);
|
||||||
|
lever = theirPawns & pawnAttacksBB[s];
|
||||||
|
doubled = ourPawns & (s + Up);
|
||||||
|
neighbours = ourPawns & adjacent_files_bb(f);
|
||||||
|
phalanx = neighbours & rank_bb(s);
|
||||||
|
supported = neighbours & rank_bb(s - Up);
|
||||||
|
connected = supported | phalanx;
|
||||||
|
|
||||||
// Our rank plus previous one
|
// A pawn is backward when it is behind all pawns of the same color on the
|
||||||
b = rank_bb(s) | p;
|
// adjacent files and cannot be safely advanced.
|
||||||
|
if (!neighbours || lever || relative_rank(Us, s) >= RANK_5)
|
||||||
// Flag the pawn as passed, isolated, doubled,
|
|
||||||
// unsupported or connected (but not the backward one).
|
|
||||||
connected = ourPawns & adjacent_files_bb(f) & b;
|
|
||||||
unsupported = !(ourPawns & adjacent_files_bb(f) & p);
|
|
||||||
isolated = !(ourPawns & adjacent_files_bb(f));
|
|
||||||
doubled = ourPawns & forward_bb(Us, s);
|
|
||||||
opposed = theirPawns & forward_bb(Us, s);
|
|
||||||
passed = !(theirPawns & passed_pawn_mask(Us, s));
|
|
||||||
|
|
||||||
// Test for backward pawn.
|
|
||||||
// If the pawn is passed, isolated, or connected it cannot be
|
|
||||||
// backward. If there are friendly pawns behind on adjacent files
|
|
||||||
// or if it can capture an enemy pawn it cannot be backward either.
|
|
||||||
if ( (passed | isolated | connected)
|
|
||||||
|| (ourPawns & pawn_attack_span(Them, s))
|
|
||||||
|| (pos.attacks_from<PAWN>(s, Us) & theirPawns))
|
|
||||||
backward = false;
|
backward = false;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// We now know that there are no friendly pawns beside or behind this
|
// Find the backmost rank with neighbours or stoppers
|
||||||
// pawn on adjacent files. We now check whether the pawn is
|
b = rank_bb(backmost_sq(Us, neighbours | stoppers));
|
||||||
// backward by looking in the forward direction on the adjacent
|
|
||||||
// files, and picking the closest pawn there.
|
|
||||||
b = pawn_attack_span(Us, s) & (ourPawns | theirPawns);
|
|
||||||
b = pawn_attack_span(Us, s) & rank_bb(backmost_sq(Us, b));
|
|
||||||
|
|
||||||
// If we have an enemy pawn in the same or next rank, the pawn is
|
// The pawn is backward when it cannot safely progress to that rank:
|
||||||
// backward because it cannot advance without being captured.
|
// either there is a stopper in the way on this rank, or there is a
|
||||||
backward = (b | shift_bb<Up>(b)) & theirPawns;
|
// 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(opposed | passed | (pawn_attack_span(Us, s) & theirPawns));
|
|
||||||
|
|
||||||
// A not-passed pawn is a candidate to become passed, if it is free to
|
|
||||||
// advance and if the number of friendly pawns beside or behind this
|
|
||||||
// pawn on adjacent files is higher than or equal to the number of
|
|
||||||
// enemy pawns in the forward direction on the adjacent files.
|
|
||||||
candidate = !(opposed | passed | backward | isolated)
|
|
||||||
&& (b = pawn_attack_span(Them, s + pawn_push(Us)) & ourPawns) != 0
|
|
||||||
&& popcount<Max15>(b) >= popcount<Max15>(pawn_attack_span(Us, s) & theirPawns);
|
|
||||||
|
|
||||||
// Passed pawns will be properly scored in evaluation because we need
|
// Passed pawns will be properly scored in evaluation because we need
|
||||||
// full attack info to evaluate passed pawns. Only the frontmost passed
|
// full attack info to evaluate them.
|
||||||
// pawn on each file is considered a true passed pawn.
|
if (!stoppers && !(ourPawns & forward_bb(Us, s)))
|
||||||
if (passed && !doubled)
|
|
||||||
e->passedPawns[Us] |= s;
|
e->passedPawns[Us] |= s;
|
||||||
|
|
||||||
// Score this pawn
|
// Score this pawn
|
||||||
if (isolated)
|
if (!neighbours)
|
||||||
value -= Isolated[opposed][f];
|
score -= Isolated[opposed];
|
||||||
|
|
||||||
if (unsupported && !isolated)
|
else if (backward)
|
||||||
value -= UnsupportedPawnPenalty;
|
score -= Backward[opposed];
|
||||||
|
|
||||||
if (doubled)
|
else if (!supported)
|
||||||
value -= Doubled[f] / rank_distance(s, lsb(doubled));
|
score -= Unsupported;
|
||||||
|
|
||||||
if (backward)
|
|
||||||
value -= Backward[opposed][f];
|
|
||||||
|
|
||||||
if (connected)
|
if (connected)
|
||||||
value += Connected[f][relative_rank(Us, s)];
|
score += Connected[opposed][!!phalanx][more_than_one(supported)][relative_rank(Us, s)];
|
||||||
|
|
||||||
if (candidate)
|
if (doubled)
|
||||||
{
|
score -= Doubled;
|
||||||
value += CandidatePassed[relative_rank(Us, s)];
|
|
||||||
|
|
||||||
if (!doubled)
|
if (lever)
|
||||||
e->candidatePawns[Us] |= s;
|
score += Lever[relative_rank(Us, s)];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// In endgame it's better to have pawns on both wings. So give a bonus according
|
return score;
|
||||||
// to file distance between left and right outermost pawns.
|
|
||||||
if (pos.count<PAWN>(Us) > 1)
|
|
||||||
{
|
|
||||||
b = e->semiopenFiles[Us] ^ 0xFF;
|
|
||||||
value += PawnsFileSpan * int(msb(b) - lsb(b));
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace Pawns {
|
namespace Pawns {
|
||||||
|
|
||||||
/// init() initializes some tables by formula instead of hard-coding their values
|
/// Pawns::init() initializes some tables needed by evaluation. Instead of using
|
||||||
|
/// hard-coded tables, when makes sense, we prefer to calculate them with a formula
|
||||||
|
/// to reduce independent parameters and to allow easier tuning and better insight.
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
|
|
||||||
const int bonusesByFile[8] = { 1, 3, 3, 4, 4, 3, 3, 1 };
|
static const int Seed[RANK_NB] = { 0, 8, 19, 13, 71, 94, 169, 324 };
|
||||||
int bonus;
|
|
||||||
|
|
||||||
for (Rank r = RANK_1; r < RANK_8; ++r)
|
for (int opposed = 0; opposed <= 1; ++opposed)
|
||||||
for (File f = FILE_A; f <= FILE_H; ++f)
|
for (int phalanx = 0; phalanx <= 1; ++phalanx)
|
||||||
{
|
for (int apex = 0; apex <= 1; ++apex)
|
||||||
bonus = r * (r-1) * (r-2) + bonusesByFile[f] * (r/2 + 1);
|
for (Rank r = RANK_2; r < RANK_8; ++r)
|
||||||
Connected[f][r] = make_score(bonus, bonus);
|
{
|
||||||
}
|
int v = (Seed[r] + (phalanx ? (Seed[r + 1] - Seed[r]) / 2 : 0)) >> opposed;
|
||||||
|
v += (apex ? v / 2 : 0);
|
||||||
|
Connected[opposed][phalanx][apex][r] = make_score(v, v * 5 / 8);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// probe() takes a position object as input, computes a Entry object, and returns
|
/// Pawns::probe() looks up the current position's pawns configuration in
|
||||||
/// a pointer to it. The result is also stored in a hash table, so we don't have
|
/// the pawns hash table. It returns a pointer to the Entry if the position
|
||||||
/// to recompute everything when the same pawn structure occurs again.
|
/// is found. Otherwise a new Entry is computed and stored there, so we don't
|
||||||
|
/// have to recompute all when the same pawns configuration occurs again.
|
||||||
|
|
||||||
Entry* probe(const Position& pos, Table& entries) {
|
Entry* probe(const Position& pos) {
|
||||||
|
|
||||||
Key key = pos.pawn_key();
|
Key key = pos.pawn_key();
|
||||||
Entry* e = entries[key];
|
Entry* e = pos.this_thread()->pawnsTable[key];
|
||||||
|
|
||||||
if (e->key == key)
|
if (e->key == key)
|
||||||
return e;
|
return e;
|
||||||
|
|
||||||
e->key = key;
|
e->key = key;
|
||||||
e->value = evaluate<WHITE>(pos, e) - evaluate<BLACK>(pos, e);
|
e->score = evaluate<WHITE>(pos, e) - evaluate<BLACK>(pos, e);
|
||||||
|
e->asymmetry = popcount(e->semiopenFiles[WHITE] ^ e->semiopenFiles[BLACK]);
|
||||||
|
e->openFiles = popcount(e->semiopenFiles[WHITE] & e->semiopenFiles[BLACK]);
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,30 +229,29 @@ template<Color Us>
|
|||||||
Value Entry::shelter_storm(const Position& pos, Square ksq) {
|
Value Entry::shelter_storm(const Position& pos, Square ksq) {
|
||||||
|
|
||||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||||
static const Bitboard MiddleEdges = (FileABB | FileHBB) & (Rank2BB | Rank3BB);
|
|
||||||
|
|
||||||
Value safety = MaxSafetyBonus;
|
enum { NoFriendlyPawn, Unblocked, BlockedByPawn, BlockedByKing };
|
||||||
|
|
||||||
Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq));
|
Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq));
|
||||||
Bitboard ourPawns = b & pos.pieces(Us);
|
Bitboard ourPawns = b & pos.pieces(Us);
|
||||||
Bitboard theirPawns = b & pos.pieces(Them);
|
Bitboard theirPawns = b & pos.pieces(Them);
|
||||||
Rank rkUs, rkThem;
|
Value safety = MaxSafetyBonus;
|
||||||
File kf = std::max(FILE_B, std::min(FILE_G, file_of(ksq)));
|
File center = std::max(FILE_B, std::min(FILE_G, file_of(ksq)));
|
||||||
|
|
||||||
for (File f = kf - File(1); f <= kf + File(1); ++f)
|
for (File f = center - File(1); f <= center + File(1); ++f)
|
||||||
{
|
{
|
||||||
b = ourPawns & file_bb(f);
|
b = ourPawns & file_bb(f);
|
||||||
rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1;
|
Rank rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1;
|
||||||
|
|
||||||
b = theirPawns & file_bb(f);
|
b = theirPawns & file_bb(f);
|
||||||
rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1;
|
Rank rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1;
|
||||||
|
|
||||||
if ( (MiddleEdges & make_square(f, rkThem))
|
safety -= ShelterWeakness[std::min(f, FILE_H - f)][rkUs]
|
||||||
&& file_of(ksq) == f
|
+ StormDanger
|
||||||
&& relative_rank(Us, ksq) == rkThem - 1)
|
[f == file_of(ksq) && rkThem == relative_rank(Us, ksq) + 1 ? BlockedByKing :
|
||||||
safety += 200;
|
rkUs == RANK_1 ? NoFriendlyPawn :
|
||||||
else
|
rkThem == rkUs + 1 ? BlockedByPawn : Unblocked]
|
||||||
safety -= ShelterWeakness[rkUs]
|
[std::min(f, FILE_H - f)][rkThem];
|
||||||
+ StormDanger[rkUs == RANK_1 ? 0 : rkThem == rkUs + 1 ? 2 : 1][rkThem];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return safety;
|
return safety;
|
||||||
@@ -288,14 +266,11 @@ Score Entry::do_king_safety(const Position& pos, Square ksq) {
|
|||||||
|
|
||||||
kingSquares[Us] = ksq;
|
kingSquares[Us] = ksq;
|
||||||
castlingRights[Us] = pos.can_castle(Us);
|
castlingRights[Us] = pos.can_castle(Us);
|
||||||
minKPdistance[Us] = 0;
|
int minKingPawnDistance = 0;
|
||||||
|
|
||||||
Bitboard pawns = pos.pieces(Us, PAWN);
|
Bitboard pawns = pos.pieces(Us, PAWN);
|
||||||
if (pawns)
|
if (pawns)
|
||||||
while (!(DistanceRingsBB[ksq][minKPdistance[Us]++] & pawns)) {}
|
while (!(DistanceRingBB[ksq][minKingPawnDistance++] & pawns)) {}
|
||||||
|
|
||||||
if (relative_rank(Us, ksq) > RANK_4)
|
|
||||||
return make_score(0, -16 * minKPdistance[Us]);
|
|
||||||
|
|
||||||
Value bonus = shelter_storm<Us>(pos, ksq);
|
Value bonus = shelter_storm<Us>(pos, ksq);
|
||||||
|
|
||||||
@@ -306,7 +281,7 @@ Score Entry::do_king_safety(const Position& pos, Square ksq) {
|
|||||||
if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::right))
|
if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::right))
|
||||||
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));
|
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));
|
||||||
|
|
||||||
return make_score(bonus, -16 * minKPdistance[Us]);
|
return make_score(bonus, -16 * minKingPawnDistance);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Explicit template instantiation
|
// Explicit template instantiation
|
||||||
|
|||||||
+12
-8
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,10 +33,12 @@ namespace Pawns {
|
|||||||
|
|
||||||
struct Entry {
|
struct Entry {
|
||||||
|
|
||||||
Score pawns_value() const { return value; }
|
Score pawns_score() const { return score; }
|
||||||
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 candidate_pawns(Color c) const { return candidatePawns[c]; }
|
Bitboard pawn_attacks_span(Color c) const { return pawnAttacksSpan[c]; }
|
||||||
|
int pawn_asymmetry() const { return asymmetry; }
|
||||||
|
int open_files() const { return openFiles; }
|
||||||
|
|
||||||
int semiopen_file(Color c, File f) const {
|
int semiopen_file(Color c, File f) const {
|
||||||
return semiopenFiles[c] & (1 << f);
|
return semiopenFiles[c] & (1 << f);
|
||||||
@@ -50,7 +53,7 @@ struct Entry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<Color Us>
|
template<Color Us>
|
||||||
Score king_safety(const Position& pos, Square ksq) {
|
Score king_safety(const Position& pos, Square ksq) {
|
||||||
return kingSquares[Us] == ksq && castlingRights[Us] == pos.can_castle(Us)
|
return kingSquares[Us] == ksq && 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, ksq));
|
||||||
}
|
}
|
||||||
@@ -62,22 +65,23 @@ struct Entry {
|
|||||||
Value shelter_storm(const Position& pos, Square ksq);
|
Value shelter_storm(const Position& pos, Square ksq);
|
||||||
|
|
||||||
Key key;
|
Key key;
|
||||||
Score value;
|
Score score;
|
||||||
Bitboard passedPawns[COLOR_NB];
|
Bitboard passedPawns[COLOR_NB];
|
||||||
Bitboard candidatePawns[COLOR_NB];
|
|
||||||
Bitboard pawnAttacks[COLOR_NB];
|
Bitboard pawnAttacks[COLOR_NB];
|
||||||
|
Bitboard pawnAttacksSpan[COLOR_NB];
|
||||||
Square kingSquares[COLOR_NB];
|
Square kingSquares[COLOR_NB];
|
||||||
Score kingSafety[COLOR_NB];
|
Score kingSafety[COLOR_NB];
|
||||||
int minKPdistance[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]
|
||||||
|
int asymmetry;
|
||||||
|
int openFiles;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef HashTable<Entry, 16384> Table;
|
typedef HashTable<Entry, 16384> Table;
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
Entry* probe(const Position& pos, Table& entries);
|
Entry* probe(const Position& pos);
|
||||||
|
|
||||||
} // namespace Pawns
|
} // namespace Pawns
|
||||||
|
|
||||||
|
|||||||
-116
@@ -1,116 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
|
||||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
Stockfish is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef PLATFORM_H_INCLUDED
|
|
||||||
#define PLATFORM_H_INCLUDED
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
|
|
||||||
// Disable some silly and noisy warnings from MSVC compiler
|
|
||||||
#pragma warning(disable: 4127) // Conditional expression is constant
|
|
||||||
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
|
|
||||||
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
|
|
||||||
#pragma warning(disable: 4996) // Function _ftime() may be unsafe
|
|
||||||
|
|
||||||
// MSVC does not support <inttypes.h>
|
|
||||||
typedef signed __int8 int8_t;
|
|
||||||
typedef unsigned __int8 uint8_t;
|
|
||||||
typedef signed __int16 int16_t;
|
|
||||||
typedef unsigned __int16 uint16_t;
|
|
||||||
typedef signed __int32 int32_t;
|
|
||||||
typedef unsigned __int32 uint32_t;
|
|
||||||
typedef signed __int64 int64_t;
|
|
||||||
typedef unsigned __int64 uint64_t;
|
|
||||||
|
|
||||||
#else
|
|
||||||
# include <inttypes.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef _WIN32 // Linux - Unix
|
|
||||||
|
|
||||||
# include <sys/time.h>
|
|
||||||
|
|
||||||
inline int64_t system_time_to_msec() {
|
|
||||||
timeval t;
|
|
||||||
gettimeofday(&t, NULL);
|
|
||||||
return t.tv_sec * 1000LL + t.tv_usec / 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
# include <pthread.h>
|
|
||||||
typedef pthread_mutex_t Lock;
|
|
||||||
typedef pthread_cond_t WaitCondition;
|
|
||||||
typedef pthread_t NativeHandle;
|
|
||||||
typedef void*(*pt_start_fn)(void*);
|
|
||||||
|
|
||||||
# define lock_init(x) pthread_mutex_init(&(x), NULL)
|
|
||||||
# define lock_grab(x) pthread_mutex_lock(&(x))
|
|
||||||
# define lock_release(x) pthread_mutex_unlock(&(x))
|
|
||||||
# define lock_destroy(x) pthread_mutex_destroy(&(x))
|
|
||||||
# define cond_destroy(x) pthread_cond_destroy(&(x))
|
|
||||||
# define cond_init(x) pthread_cond_init(&(x), NULL)
|
|
||||||
# define cond_signal(x) pthread_cond_signal(&(x))
|
|
||||||
# define cond_wait(x,y) pthread_cond_wait(&(x),&(y))
|
|
||||||
# define cond_timedwait(x,y,z) pthread_cond_timedwait(&(x),&(y),z)
|
|
||||||
# define thread_create(x,f,t) pthread_create(&(x),NULL,(pt_start_fn)f,t)
|
|
||||||
# define thread_join(x) pthread_join(x, NULL)
|
|
||||||
|
|
||||||
#else // Windows and MinGW
|
|
||||||
|
|
||||||
# include <sys/timeb.h>
|
|
||||||
|
|
||||||
inline int64_t system_time_to_msec() {
|
|
||||||
_timeb t;
|
|
||||||
_ftime(&t);
|
|
||||||
return t.time * 1000LL + t.millitm;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef NOMINMAX
|
|
||||||
# define NOMINMAX // disable macros min() and max()
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#include <windows.h>
|
|
||||||
#undef WIN32_LEAN_AND_MEAN
|
|
||||||
#undef NOMINMAX
|
|
||||||
|
|
||||||
// We use critical sections on Windows to support Windows XP and older versions.
|
|
||||||
// Unfortunately, cond_wait() is racy between lock_release() and WaitForSingleObject()
|
|
||||||
// but apart from this they have the same speed performance of SRW locks.
|
|
||||||
typedef CRITICAL_SECTION Lock;
|
|
||||||
typedef HANDLE WaitCondition;
|
|
||||||
typedef HANDLE NativeHandle;
|
|
||||||
|
|
||||||
// On Windows 95 and 98 parameter lpThreadId may not be null
|
|
||||||
inline DWORD* dwWin9xKludge() { static DWORD dw; return &dw; }
|
|
||||||
|
|
||||||
# define lock_init(x) InitializeCriticalSection(&(x))
|
|
||||||
# define lock_grab(x) EnterCriticalSection(&(x))
|
|
||||||
# define lock_release(x) LeaveCriticalSection(&(x))
|
|
||||||
# define lock_destroy(x) DeleteCriticalSection(&(x))
|
|
||||||
# define cond_init(x) { x = CreateEvent(0, FALSE, FALSE, 0); }
|
|
||||||
# define cond_destroy(x) CloseHandle(x)
|
|
||||||
# define cond_signal(x) SetEvent(x)
|
|
||||||
# define cond_wait(x,y) { lock_release(y); WaitForSingleObject(x, INFINITE); lock_grab(y); }
|
|
||||||
# define cond_timedwait(x,y,z) { lock_release(y); WaitForSingleObject(x,z); lock_grab(y); }
|
|
||||||
# define thread_create(x,f,t) (x = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)f,t,0,dwWin9xKludge()))
|
|
||||||
# define thread_join(x) { WaitForSingleObject(x, INFINITE); CloseHandle(x); }
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif // #ifndef PLATFORM_H_INCLUDED
|
|
||||||
+356
-457
File diff suppressed because it is too large
Load Diff
+129
-148
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,69 +22,61 @@
|
|||||||
#define POSITION_H_INCLUDED
|
#define POSITION_H_INCLUDED
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstddef>
|
#include <deque>
|
||||||
|
#include <memory> // For std::unique_ptr
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
|
|
||||||
/// The checkInfo struct is initialized at c'tor time and keeps info used
|
/// StateInfo struct stores information needed to restore a Position object to
|
||||||
/// to detect if a move gives check.
|
/// its previous state when we retract a move. Whenever a move is made on the
|
||||||
class Position;
|
/// board (by calling Position::do_move), a StateInfo object must be passed.
|
||||||
struct Thread;
|
|
||||||
|
|
||||||
struct CheckInfo {
|
|
||||||
|
|
||||||
explicit CheckInfo(const Position&);
|
|
||||||
|
|
||||||
Bitboard dcCandidates;
|
|
||||||
Bitboard pinned;
|
|
||||||
Bitboard checkSq[PIECE_TYPE_NB];
|
|
||||||
Square ksq;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/// The StateInfo struct stores information needed to restore a Position
|
|
||||||
/// object to its previous state when we retract a move. Whenever a move
|
|
||||||
/// is made on the board (by calling Position::do_move), a StateInfo
|
|
||||||
/// object must be passed as a parameter.
|
|
||||||
|
|
||||||
struct StateInfo {
|
struct StateInfo {
|
||||||
Key pawnKey, materialKey;
|
|
||||||
Value npMaterial[COLOR_NB];
|
// Copied when making a move
|
||||||
int castlingRights, rule50, pliesFromNull;
|
Key pawnKey;
|
||||||
Score psq;
|
Key materialKey;
|
||||||
|
Value nonPawnMaterial[COLOR_NB];
|
||||||
|
int castlingRights;
|
||||||
|
int rule50;
|
||||||
|
int pliesFromNull;
|
||||||
|
Score psq;
|
||||||
Square epSquare;
|
Square epSquare;
|
||||||
|
|
||||||
Key key;
|
// Not copied when making a move (will be recomputed anyhow)
|
||||||
Bitboard checkersBB;
|
Key key;
|
||||||
PieceType capturedType;
|
Bitboard checkersBB;
|
||||||
|
Piece capturedPiece;
|
||||||
StateInfo* previous;
|
StateInfo* previous;
|
||||||
|
Bitboard blockersForKing[COLOR_NB];
|
||||||
|
Bitboard pinnersForKing[COLOR_NB];
|
||||||
|
Bitboard checkSquares[PIECE_TYPE_NB];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// In a std::deque references to elements are unaffected upon resizing
|
||||||
/// When making a move the current StateInfo up to 'key' excluded is copied to
|
typedef std::unique_ptr<std::deque<StateInfo>> StateListPtr;
|
||||||
/// the new one. Here we calculate the quad words (64bits) needed to be copied.
|
|
||||||
const size_t StateCopySize64 = offsetof(StateInfo, key) / sizeof(uint64_t) + 1;
|
|
||||||
|
|
||||||
|
|
||||||
/// The Position class stores the information regarding the board representation
|
/// Position class stores information regarding the board representation as
|
||||||
/// like pieces, side to move, hash keys, castling info, etc. The most important
|
/// pieces, side to move, hash keys, castling info, etc. Important methods are
|
||||||
/// methods are do_move() and undo_move(), used by the search to update node info
|
/// do_move() and undo_move(), used by the search to update node info when
|
||||||
/// when traversing the search tree.
|
/// traversing the search tree.
|
||||||
|
class Thread;
|
||||||
|
|
||||||
class Position {
|
class Position {
|
||||||
public:
|
public:
|
||||||
Position() {}
|
|
||||||
Position(const Position& pos, Thread* t) { *this = pos; thisThread = t; }
|
|
||||||
Position(const std::string& f, bool c960, Thread* t) { set(f, c960, t); }
|
|
||||||
Position& operator=(const Position&);
|
|
||||||
static void init();
|
static void init();
|
||||||
|
|
||||||
// Text input/output
|
Position() = default;
|
||||||
void set(const std::string& fenStr, bool isChess960, Thread* th);
|
Position(const Position&) = delete;
|
||||||
|
Position& operator=(const Position&) = delete;
|
||||||
|
|
||||||
|
// FEN string input/output
|
||||||
|
Position& set(const std::string& fenStr, bool isChess960, StateInfo* si, Thread* th);
|
||||||
const std::string fen() const;
|
const std::string fen() const;
|
||||||
const std::string pretty(Move m = MOVE_NONE) const;
|
|
||||||
|
|
||||||
// Position representation
|
// Position representation
|
||||||
Bitboard pieces() const;
|
Bitboard pieces() const;
|
||||||
@@ -93,11 +86,11 @@ public:
|
|||||||
Bitboard pieces(Color c, PieceType pt) const;
|
Bitboard pieces(Color c, PieceType pt) const;
|
||||||
Bitboard pieces(Color c, PieceType pt1, PieceType pt2) const;
|
Bitboard pieces(Color c, PieceType pt1, PieceType pt2) const;
|
||||||
Piece piece_on(Square s) const;
|
Piece piece_on(Square s) const;
|
||||||
Square king_square(Color c) const;
|
|
||||||
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> const Square* list(Color c) const;
|
template<PieceType Pt> const Square* squares(Color c) const;
|
||||||
|
template<PieceType Pt> Square square(Color c) const;
|
||||||
|
|
||||||
// Castling
|
// Castling
|
||||||
int can_castle(Color c) const;
|
int can_castle(Color c) const;
|
||||||
@@ -109,91 +102,84 @@ public:
|
|||||||
Bitboard checkers() const;
|
Bitboard checkers() const;
|
||||||
Bitboard discovered_check_candidates() const;
|
Bitboard discovered_check_candidates() const;
|
||||||
Bitboard pinned_pieces(Color c) const;
|
Bitboard pinned_pieces(Color c) 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 occ) const;
|
Bitboard attackers_to(Square s, Bitboard occupied) const;
|
||||||
Bitboard attacks_from(Piece pc, Square s) const;
|
Bitboard attacks_from(Piece pc, 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;
|
||||||
|
|
||||||
// Properties of moves
|
// Properties of moves
|
||||||
bool legal(Move m, Bitboard pinned) const;
|
bool legal(Move m) const;
|
||||||
bool pseudo_legal(const Move m) const;
|
bool pseudo_legal(const Move m) const;
|
||||||
bool capture(Move m) const;
|
bool capture(Move m) const;
|
||||||
bool capture_or_promotion(Move m) const;
|
bool capture_or_promotion(Move m) const;
|
||||||
bool gives_check(Move m, const CheckInfo& ci) const;
|
bool gives_check(Move m) const;
|
||||||
bool advanced_pawn_push(Move m) const;
|
bool advanced_pawn_push(Move m) const;
|
||||||
Piece moved_piece(Move m) const;
|
Piece moved_piece(Move m) const;
|
||||||
PieceType captured_piece_type() const;
|
Piece captured_piece() const;
|
||||||
|
|
||||||
// Piece specific
|
// Piece specific
|
||||||
bool pawn_passed(Color c, Square s) const;
|
bool pawn_passed(Color c, Square s) const;
|
||||||
bool pawn_on_7th(Color c) const;
|
|
||||||
bool bishop_pair(Color c) const;
|
|
||||||
bool opposite_bishops() const;
|
bool opposite_bishops() const;
|
||||||
|
|
||||||
// Doing and undoing moves
|
// Doing and undoing moves
|
||||||
void do_move(Move m, StateInfo& st);
|
void do_move(Move m, StateInfo& st, bool givesCheck);
|
||||||
void do_move(Move m, StateInfo& st, const CheckInfo& ci, bool moveIsCheck);
|
|
||||||
void undo_move(Move m);
|
void undo_move(Move m);
|
||||||
void do_null_move(StateInfo& st);
|
void do_null_move(StateInfo& st);
|
||||||
void undo_null_move();
|
void undo_null_move();
|
||||||
|
|
||||||
// Static exchange evaluation
|
// Static Exchange Evaluation
|
||||||
Value see(Move m) const;
|
bool see_ge(Move m, Value value) const;
|
||||||
Value see_sign(Move m) const;
|
|
||||||
|
|
||||||
// Accessing hash keys
|
// Accessing hash keys
|
||||||
Key key() const;
|
Key key() const;
|
||||||
Key exclusion_key() const;
|
Key key_after(Move m) const;
|
||||||
Key pawn_key() const;
|
|
||||||
Key material_key() const;
|
Key material_key() const;
|
||||||
|
Key pawn_key() const;
|
||||||
// Incremental piece-square evaluation
|
|
||||||
Score psq_score() const;
|
|
||||||
Value non_pawn_material(Color c) const;
|
|
||||||
|
|
||||||
// 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;
|
uint64_t nodes_searched() const;
|
||||||
void set_nodes_searched(uint64_t n);
|
|
||||||
bool is_draw() const;
|
bool is_draw() const;
|
||||||
|
int rule50_count() const;
|
||||||
|
Score psq_score() const;
|
||||||
|
Value non_pawn_material(Color c) const;
|
||||||
|
|
||||||
// Position consistency check, for debugging
|
// Position consistency check, for debugging
|
||||||
bool pos_is_ok(int* step = NULL) const;
|
bool pos_is_ok(int* failedStep = nullptr) const;
|
||||||
void flip();
|
void flip();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Initialization helpers (used while setting up a position)
|
// Initialization helpers (used while setting up a position)
|
||||||
void clear();
|
|
||||||
void set_castling_right(Color c, Square rfrom);
|
void set_castling_right(Color c, Square rfrom);
|
||||||
void set_state(StateInfo* si) const;
|
void set_state(StateInfo* si) const;
|
||||||
|
void set_check_info(StateInfo* si) const;
|
||||||
|
|
||||||
// Helper functions
|
// Other helpers
|
||||||
Bitboard check_blockers(Color c, Color kingColor) const;
|
void put_piece(Piece pc, Square s);
|
||||||
void put_piece(Square s, Color c, PieceType pt);
|
void remove_piece(Piece pc, Square s);
|
||||||
void remove_piece(Square s, Color c, PieceType pt);
|
void move_piece(Piece pc, Square from, Square to);
|
||||||
void move_piece(Square from, Square to, Color c, PieceType pt);
|
|
||||||
template<bool Do>
|
template<bool Do>
|
||||||
void do_castling(Square from, Square& to, Square& rfrom, Square& rto);
|
void do_castling(Color us, Square from, Square& to, Square& rfrom, Square& rto);
|
||||||
|
|
||||||
// Board and pieces
|
// Data members
|
||||||
Piece board[SQUARE_NB];
|
Piece board[SQUARE_NB];
|
||||||
Bitboard byTypeBB[PIECE_TYPE_NB];
|
Bitboard byTypeBB[PIECE_TYPE_NB];
|
||||||
Bitboard byColorBB[COLOR_NB];
|
Bitboard byColorBB[COLOR_NB];
|
||||||
int pieceCount[COLOR_NB][PIECE_TYPE_NB];
|
int pieceCount[PIECE_NB];
|
||||||
Square pieceList[COLOR_NB][PIECE_TYPE_NB][16];
|
Square pieceList[PIECE_NB][16];
|
||||||
int index[SQUARE_NB];
|
int index[SQUARE_NB];
|
||||||
|
|
||||||
// Other info
|
|
||||||
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];
|
||||||
StateInfo startState;
|
|
||||||
uint64_t nodes;
|
uint64_t nodes;
|
||||||
int gamePly;
|
int gamePly;
|
||||||
Color sideToMove;
|
Color sideToMove;
|
||||||
@@ -202,12 +188,14 @@ private:
|
|||||||
bool chess960;
|
bool chess960;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline uint64_t Position::nodes_searched() const {
|
extern std::ostream& operator<<(std::ostream& os, const Position& pos);
|
||||||
return nodes;
|
|
||||||
|
inline Color Position::side_to_move() const {
|
||||||
|
return sideToMove;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Position::set_nodes_searched(uint64_t n) {
|
inline bool Position::empty(Square s) const {
|
||||||
nodes = n;
|
return board[s] == NO_PIECE;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Piece Position::piece_on(Square s) const {
|
inline Piece Position::piece_on(Square s) const {
|
||||||
@@ -218,14 +206,6 @@ inline Piece Position::moved_piece(Move m) const {
|
|||||||
return board[from_sq(m)];
|
return board[from_sq(m)];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Position::empty(Square s) const {
|
|
||||||
return board[s] == NO_PIECE;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Color Position::side_to_move() const {
|
|
||||||
return sideToMove;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Bitboard Position::pieces() const {
|
inline Bitboard Position::pieces() const {
|
||||||
return byTypeBB[ALL_PIECES];
|
return byTypeBB[ALL_PIECES];
|
||||||
}
|
}
|
||||||
@@ -251,21 +231,22 @@ inline Bitboard Position::pieces(Color c, PieceType pt1, PieceType pt2) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<PieceType Pt> inline int Position::count(Color c) const {
|
template<PieceType Pt> inline int Position::count(Color c) const {
|
||||||
return pieceCount[c][Pt];
|
return pieceCount[make_piece(c, Pt)];
|
||||||
}
|
}
|
||||||
|
|
||||||
template<PieceType Pt> inline const Square* Position::list(Color c) const {
|
template<PieceType Pt> inline const Square* Position::squares(Color c) const {
|
||||||
return pieceList[c][Pt];
|
return pieceList[make_piece(c, Pt)];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<PieceType Pt> inline Square Position::square(Color c) const {
|
||||||
|
assert(pieceCount[make_piece(c, Pt)] == 1);
|
||||||
|
return pieceList[make_piece(c, Pt)][0];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Square Position::ep_square() const {
|
inline Square Position::ep_square() const {
|
||||||
return st->epSquare;
|
return st->epSquare;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Square Position::king_square(Color c) const {
|
|
||||||
return pieceList[c][KING][0];
|
|
||||||
}
|
|
||||||
|
|
||||||
inline int Position::can_castle(CastlingRight cr) const {
|
inline int Position::can_castle(CastlingRight cr) const {
|
||||||
return st->castlingRights & cr;
|
return st->castlingRights & cr;
|
||||||
}
|
}
|
||||||
@@ -284,7 +265,6 @@ 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 {
|
||||||
|
|
||||||
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];
|
: StepAttacksBB[Pt][s];
|
||||||
@@ -308,11 +288,15 @@ inline Bitboard Position::checkers() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard Position::discovered_check_candidates() const {
|
inline Bitboard Position::discovered_check_candidates() const {
|
||||||
return check_blockers(sideToMove, ~sideToMove);
|
return st->blockersForKing[~sideToMove] & pieces(sideToMove);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Bitboard Position::pinned_pieces(Color c) const {
|
inline Bitboard Position::pinned_pieces(Color c) const {
|
||||||
return check_blockers(c, c);
|
return st->blockersForKing[c] & pieces(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Bitboard Position::check_squares(PieceType pt) const {
|
||||||
|
return st->checkSquares[pt];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Position::pawn_passed(Color c, Square s) const {
|
inline bool Position::pawn_passed(Color c, Square s) const {
|
||||||
@@ -341,28 +325,25 @@ inline Score Position::psq_score() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline Value Position::non_pawn_material(Color c) const {
|
inline Value Position::non_pawn_material(Color c) const {
|
||||||
return st->npMaterial[c];
|
return st->nonPawnMaterial[c];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int Position::game_ply() const {
|
inline int Position::game_ply() const {
|
||||||
return gamePly;
|
return gamePly;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline int Position::rule50_count() const {
|
||||||
|
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[WHITE][BISHOP] == 1
|
&& pieceCount[B_BISHOP] == 1
|
||||||
&& pieceCount[BLACK][BISHOP] == 1
|
&& opposite_colors(square<BISHOP>(WHITE), square<BISHOP>(BLACK));
|
||||||
&& opposite_colors(pieceList[WHITE][BISHOP][0], pieceList[BLACK][BISHOP][0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool Position::bishop_pair(Color c) const {
|
|
||||||
|
|
||||||
return pieceCount[c][BISHOP] >= 2
|
|
||||||
&& opposite_colors(pieceList[c][BISHOP][0], pieceList[c][BISHOP][1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool Position::pawn_on_7th(Color c) const {
|
|
||||||
return pieces(c, PAWN) & rank_bb(relative_rank(c, RANK_7));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Position::is_chess960() const {
|
inline bool Position::is_chess960() const {
|
||||||
@@ -370,64 +351,64 @@ inline bool Position::is_chess960() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline bool Position::capture_or_promotion(Move m) const {
|
inline bool Position::capture_or_promotion(Move m) const {
|
||||||
|
|
||||||
assert(is_ok(m));
|
assert(is_ok(m));
|
||||||
return type_of(m) != NORMAL ? type_of(m) != CASTLING : !empty(to_sq(m));
|
return type_of(m) != NORMAL ? type_of(m) != CASTLING : !empty(to_sq(m));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Position::capture(Move m) const {
|
inline bool Position::capture(Move m) const {
|
||||||
|
|
||||||
// Note that castling is encoded as "king captures the rook"
|
|
||||||
assert(is_ok(m));
|
assert(is_ok(m));
|
||||||
|
// Castling is encoded as "king captures rook"
|
||||||
return (!empty(to_sq(m)) && type_of(m) != CASTLING) || type_of(m) == ENPASSANT;
|
return (!empty(to_sq(m)) && type_of(m) != CASTLING) || type_of(m) == ENPASSANT;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline PieceType Position::captured_piece_type() const {
|
inline Piece Position::captured_piece() const {
|
||||||
return st->capturedType;
|
return st->capturedPiece;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Thread* Position::this_thread() const {
|
inline Thread* Position::this_thread() const {
|
||||||
return thisThread;
|
return thisThread;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Position::put_piece(Square s, Color c, PieceType pt) {
|
inline void Position::put_piece(Piece pc, Square s) {
|
||||||
|
|
||||||
board[s] = make_piece(c, pt);
|
board[s] = pc;
|
||||||
byTypeBB[ALL_PIECES] |= s;
|
byTypeBB[ALL_PIECES] |= s;
|
||||||
byTypeBB[pt] |= s;
|
byTypeBB[type_of(pc)] |= s;
|
||||||
byColorBB[c] |= s;
|
byColorBB[color_of(pc)] |= s;
|
||||||
index[s] = pieceCount[c][pt]++;
|
index[s] = pieceCount[pc]++;
|
||||||
pieceList[c][pt][index[s]] = s;
|
pieceList[pc][index[s]] = s;
|
||||||
|
pieceCount[make_piece(color_of(pc), ALL_PIECES)]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Position::move_piece(Square from, Square to, Color c, PieceType pt) {
|
inline void Position::remove_piece(Piece pc, Square s) {
|
||||||
|
|
||||||
// index[from] is not updated and becomes stale. This works as long
|
|
||||||
// as index[] is accessed just by known occupied squares.
|
|
||||||
Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
|
|
||||||
byTypeBB[ALL_PIECES] ^= from_to_bb;
|
|
||||||
byTypeBB[pt] ^= from_to_bb;
|
|
||||||
byColorBB[c] ^= from_to_bb;
|
|
||||||
board[from] = NO_PIECE;
|
|
||||||
board[to] = make_piece(c, pt);
|
|
||||||
index[to] = index[from];
|
|
||||||
pieceList[c][pt][index[to]] = to;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void Position::remove_piece(Square s, Color c, PieceType pt) {
|
|
||||||
|
|
||||||
// WARNING: This is not a reversible operation. If we remove a piece in
|
// WARNING: This is not a reversible operation. If we remove a piece in
|
||||||
// do_move() and then replace it in undo_move() we will put it at the end of
|
// do_move() and then replace it in undo_move() we will put it at the end of
|
||||||
// the list and not in its original place, it means index[] and pieceList[]
|
// the list and not in its original place, it means index[] and pieceList[]
|
||||||
// are not guaranteed to be invariant to a do_move() + undo_move() sequence.
|
// are not invariant to a do_move() + undo_move() sequence.
|
||||||
byTypeBB[ALL_PIECES] ^= s;
|
byTypeBB[ALL_PIECES] ^= s;
|
||||||
byTypeBB[pt] ^= s;
|
byTypeBB[type_of(pc)] ^= s;
|
||||||
byColorBB[c] ^= s;
|
byColorBB[color_of(pc)] ^= s;
|
||||||
/* board[s] = NO_PIECE; */ // Not needed, will be overwritten by capturing
|
/* board[s] = NO_PIECE; Not needed, overwritten by the capturing one */
|
||||||
Square lastSquare = pieceList[c][pt][--pieceCount[c][pt]];
|
Square lastSquare = pieceList[pc][--pieceCount[pc]];
|
||||||
index[lastSquare] = index[s];
|
index[lastSquare] = index[s];
|
||||||
pieceList[c][pt][index[lastSquare]] = lastSquare;
|
pieceList[pc][index[lastSquare]] = lastSquare;
|
||||||
pieceList[c][pt][pieceCount[c][pt]] = SQ_NONE;
|
pieceList[pc][pieceCount[pc]] = SQ_NONE;
|
||||||
|
pieceCount[make_piece(color_of(pc), ALL_PIECES)]--;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void Position::move_piece(Piece pc, Square from, Square to) {
|
||||||
|
|
||||||
|
// index[from] is not updated and becomes stale. This works as long as index[]
|
||||||
|
// is accessed just by known occupied squares.
|
||||||
|
Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
|
||||||
|
byTypeBB[ALL_PIECES] ^= from_to_bb;
|
||||||
|
byTypeBB[type_of(pc)] ^= from_to_bb;
|
||||||
|
byColorBB[color_of(pc)] ^= from_to_bb;
|
||||||
|
board[from] = NO_PIECE;
|
||||||
|
board[to] = pc;
|
||||||
|
index[to] = index[from];
|
||||||
|
pieceList[pc][index[to]] = to;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // #ifndef POSITION_H_INCLUDED
|
#endif // #ifndef POSITION_H_INCLUDED
|
||||||
|
|||||||
+126
@@ -0,0 +1,126 @@
|
|||||||
|
/*
|
||||||
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
|
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Stockfish is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
Value PieceValue[PHASE_NB][PIECE_NB] = {
|
||||||
|
{ VALUE_ZERO, PawnValueMg, KnightValueMg, BishopValueMg, RookValueMg, QueenValueMg },
|
||||||
|
{ VALUE_ZERO, PawnValueEg, KnightValueEg, BishopValueEg, RookValueEg, QueenValueEg }
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace PSQT {
|
||||||
|
|
||||||
|
#define S(mg, eg) make_score(mg, eg)
|
||||||
|
|
||||||
|
// Bonus[PieceType][Square / 2] contains Piece-Square scores. For each piece
|
||||||
|
// type on a given square a (middlegame, endgame) score pair is assigned. Table
|
||||||
|
// is defined for files A..D and white side: it is symmetric for black side and
|
||||||
|
// second half of the files.
|
||||||
|
const Score Bonus[][RANK_NB][int(FILE_NB) / 2] = {
|
||||||
|
{ },
|
||||||
|
{ // Pawn
|
||||||
|
{ S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0) },
|
||||||
|
{ S(-16, 7), S( 1,-4), S( 7, 8), S( 3,-2) },
|
||||||
|
{ S(-23,-4), S( -7,-5), S( 19, 5), S(24, 4) },
|
||||||
|
{ S(-22, 3), S(-14, 3), S( 20,-8), S(35,-3) },
|
||||||
|
{ S(-11, 8), S( 0, 9), S( 3, 7), S(21,-6) },
|
||||||
|
{ S(-11, 8), S(-13,-5), S( -6, 2), S(-2, 4) },
|
||||||
|
{ S( -9, 3), S( 15,-9), S( -8, 1), S(-4,18) }
|
||||||
|
},
|
||||||
|
{ // Knight
|
||||||
|
{ S(-143, -97), S(-96,-82), S(-80,-46), S(-73,-14) },
|
||||||
|
{ S( -83, -69), S(-43,-55), S(-21,-17), S(-10, 9) },
|
||||||
|
{ S( -71, -50), S(-22,-39), S( 0, -8), S( 9, 28) },
|
||||||
|
{ S( -25, -41), S( 18,-25), S( 43, 7), S( 47, 38) },
|
||||||
|
{ S( -26, -46), S( 16,-25), S( 38, 2), S( 50, 41) },
|
||||||
|
{ S( -11, -55), S( 37,-38), S( 56, -8), S( 71, 27) },
|
||||||
|
{ S( -62, -64), S(-17,-50), S( 5,-24), S( 14, 13) },
|
||||||
|
{ S(-195,-110), S(-66,-90), S(-42,-50), S(-29,-13) }
|
||||||
|
},
|
||||||
|
{ // Bishop
|
||||||
|
{ S(-54,-68), S(-23,-40), S(-35,-46), S(-44,-28) },
|
||||||
|
{ S(-30,-43), S( 10,-17), S( 2,-23), S( -9, -5) },
|
||||||
|
{ S(-19,-32), S( 17, -9), S( 11,-13), S( 1, 8) },
|
||||||
|
{ S(-21,-36), S( 18,-13), S( 11,-15), S( 0, 7) },
|
||||||
|
{ S(-21,-36), S( 14,-14), S( 6,-17), S( -1, 3) },
|
||||||
|
{ S(-27,-35), S( 6,-13), S( 2,-10), S( -8, 1) },
|
||||||
|
{ S(-33,-44), S( 7,-21), S( -4,-22), S(-12, -4) },
|
||||||
|
{ S(-45,-65), S(-21,-42), S(-29,-46), S(-39,-27) }
|
||||||
|
},
|
||||||
|
{ // Rook
|
||||||
|
{ S(-25, 0), S(-16, 0), S(-16, 0), S(-9, 0) },
|
||||||
|
{ S(-21, 0), S( -8, 0), S( -3, 0), S( 0, 0) },
|
||||||
|
{ S(-21, 0), S( -9, 0), S( -4, 0), S( 2, 0) },
|
||||||
|
{ S(-22, 0), S( -6, 0), S( -1, 0), S( 2, 0) },
|
||||||
|
{ S(-22, 0), S( -7, 0), S( 0, 0), S( 1, 0) },
|
||||||
|
{ S(-21, 0), S( -7, 0), S( 0, 0), S( 2, 0) },
|
||||||
|
{ S(-12, 0), S( 4, 0), S( 8, 0), S(12, 0) },
|
||||||
|
{ S(-23, 0), S(-15, 0), S(-11, 0), S(-5, 0) }
|
||||||
|
},
|
||||||
|
{ // Queen
|
||||||
|
{ S( 0,-70), S(-3,-57), S(-4,-41), S(-1,-29) },
|
||||||
|
{ S(-4,-58), S( 6,-30), S( 9,-21), S( 8, -4) },
|
||||||
|
{ S(-2,-39), S( 6,-17), S( 9, -7), S( 9, 5) },
|
||||||
|
{ S(-1,-29), S( 8, -5), S(10, 9), S( 7, 17) },
|
||||||
|
{ S(-3,-27), S( 9, -5), S( 8, 10), S( 7, 23) },
|
||||||
|
{ S(-2,-40), S( 6,-16), S( 8,-11), S(10, 3) },
|
||||||
|
{ S(-2,-54), S( 7,-30), S( 7,-21), S( 6, -7) },
|
||||||
|
{ S(-1,-75), S(-4,-54), S(-1,-44), S( 0,-30) }
|
||||||
|
},
|
||||||
|
{ // King
|
||||||
|
{ S(291, 28), S(344, 76), S(294,103), S(219,112) },
|
||||||
|
{ S(289, 70), S(329,119), S(263,170), S(205,159) },
|
||||||
|
{ S(226,109), S(271,164), S(202,195), S(136,191) },
|
||||||
|
{ S(204,131), S(212,194), S(175,194), S(137,204) },
|
||||||
|
{ S(177,132), S(205,187), S(143,224), S( 94,227) },
|
||||||
|
{ S(147,118), S(188,178), S(113,199), S( 70,197) },
|
||||||
|
{ S(116, 72), S(158,121), S( 93,142), S( 48,161) },
|
||||||
|
{ S( 94, 30), S(120, 76), S( 78,101), S( 31,111) }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#undef S
|
||||||
|
|
||||||
|
Score psq[PIECE_NB][SQUARE_NB];
|
||||||
|
|
||||||
|
// init() initializes piece-square tables: the white halves of the tables are
|
||||||
|
// copied from Bonus[] adding the piece value, then the black halves of the
|
||||||
|
// tables are initialized by flipping and changing the sign of the white scores.
|
||||||
|
void init() {
|
||||||
|
|
||||||
|
for (Piece pc = W_PAWN; pc <= W_KING; ++pc)
|
||||||
|
{
|
||||||
|
PieceValue[MG][~pc] = PieceValue[MG][pc];
|
||||||
|
PieceValue[EG][~pc] = PieceValue[EG][pc];
|
||||||
|
|
||||||
|
Score v = make_score(PieceValue[MG][pc], PieceValue[EG][pc]);
|
||||||
|
|
||||||
|
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||||
|
{
|
||||||
|
File f = std::min(file_of(s), FILE_H - file_of(s));
|
||||||
|
psq[ pc][ s] = v + Bonus[pc][rank_of(s)][f];
|
||||||
|
psq[~pc][~s] = -psq[pc][s];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace PSQT
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
|
||||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
Stockfish is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef PSQTAB_H_INCLUDED
|
|
||||||
#define PSQTAB_H_INCLUDED
|
|
||||||
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
#define S(mg, eg) make_score(mg, eg)
|
|
||||||
|
|
||||||
|
|
||||||
/// PSQT[PieceType][Square] contains Piece-Square scores. For each piece type on
|
|
||||||
/// a given square a (middlegame, endgame) score pair is assigned. PSQT is defined
|
|
||||||
/// for the white side and the tables are symmetric for the black side.
|
|
||||||
|
|
||||||
static const Score PSQT[][SQUARE_NB] = {
|
|
||||||
{ },
|
|
||||||
{ // Pawn
|
|
||||||
S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S( 0, 0),
|
|
||||||
S(-20, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S(-20, 0),
|
|
||||||
S(-20, 0), S( 0, 0), S(10, 0), S(20, 0), S(20, 0), S(10, 0), S( 0, 0), S(-20, 0),
|
|
||||||
S(-20, 0), S( 0, 0), S(20, 0), S(40, 0), S(40, 0), S(20, 0), S( 0, 0), S(-20, 0),
|
|
||||||
S(-20, 0), S( 0, 0), S(10, 0), S(20, 0), S(20, 0), S(10, 0), S( 0, 0), S(-20, 0),
|
|
||||||
S(-20, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S(-20, 0),
|
|
||||||
S(-20, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S(-20, 0),
|
|
||||||
S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S( 0, 0)
|
|
||||||
},
|
|
||||||
{ // Knight
|
|
||||||
S(-144,-98), S(-109,-83), S(-85,-51), S(-73,-16), S(-73,-16), S(-85,-51), S(-109,-83), S(-144,-98),
|
|
||||||
S( -88,-68), S( -43,-53), S(-19,-21), S( -7, 14), S( -7, 14), S(-19,-21), S( -43,-53), S( -88,-68),
|
|
||||||
S( -69,-53), S( -24,-38), S( 0, -6), S( 12, 29), S( 12, 29), S( 0, -6), S( -24,-38), S( -69,-53),
|
|
||||||
S( -28,-42), S( 17,-27), S( 41, 5), S( 53, 40), S( 53, 40), S( 41, 5), S( 17,-27), S( -28,-42),
|
|
||||||
S( -30,-42), S( 15,-27), S( 39, 5), S( 51, 40), S( 51, 40), S( 39, 5), S( 15,-27), S( -30,-42),
|
|
||||||
S( -10,-53), S( 35,-38), S( 59, -6), S( 71, 29), S( 71, 29), S( 59, -6), S( 35,-38), S( -10,-53),
|
|
||||||
S( -64,-68), S( -19,-53), S( 5,-21), S( 17, 14), S( 17, 14), S( 5,-21), S( -19,-53), S( -64,-68),
|
|
||||||
S(-200,-98), S( -65,-83), S(-41,-51), S(-29,-16), S(-29,-16), S(-41,-51), S( -65,-83), S(-200,-98)
|
|
||||||
},
|
|
||||||
{ // Bishop
|
|
||||||
S(-54,-65), S(-27,-42), S(-34,-44), S(-43,-26), S(-43,-26), S(-34,-44), S(-27,-42), S(-54,-65),
|
|
||||||
S(-29,-43), S( 8,-20), S( 1,-22), S( -8, -4), S( -8, -4), S( 1,-22), S( 8,-20), S(-29,-43),
|
|
||||||
S(-20,-33), S( 17,-10), S( 10,-12), S( 1, 6), S( 1, 6), S( 10,-12), S( 17,-10), S(-20,-33),
|
|
||||||
S(-19,-35), S( 18,-12), S( 11,-14), S( 2, 4), S( 2, 4), S( 11,-14), S( 18,-12), S(-19,-35),
|
|
||||||
S(-22,-35), S( 15,-12), S( 8,-14), S( -1, 4), S( -1, 4), S( 8,-14), S( 15,-12), S(-22,-35),
|
|
||||||
S(-28,-33), S( 9,-10), S( 2,-12), S( -7, 6), S( -7, 6), S( 2,-12), S( 9,-10), S(-28,-33),
|
|
||||||
S(-32,-43), S( 5,-20), S( -2,-22), S(-11, -4), S(-11, -4), S( -2,-22), S( 5,-20), S(-32,-43),
|
|
||||||
S(-49,-65), S(-22,-42), S(-29,-44), S(-38,-26), S(-38,-26), S(-29,-44), S(-22,-42), S(-49,-65)
|
|
||||||
},
|
|
||||||
{ // Rook
|
|
||||||
S(-22, 3), S(-17, 3), S(-12, 3), S(-8, 3), S(-8, 3), S(-12, 3), S(-17, 3), S(-22, 3),
|
|
||||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
|
||||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
|
||||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
|
||||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
|
||||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
|
||||||
S(-11, 3), S( 4, 3), S( 9, 3), S(13, 3), S(13, 3), S( 9, 3), S( 4, 3), S(-11, 3),
|
|
||||||
S(-22, 3), S(-17, 3), S(-12, 3), S(-8, 3), S(-8, 3), S(-12, 3), S(-17, 3), S(-22, 3)
|
|
||||||
},
|
|
||||||
{ // Queen
|
|
||||||
S(-2,-80), S(-2,-54), S(-2,-42), S(-2,-30), S(-2,-30), S(-2,-42), S(-2,-54), S(-2,-80),
|
|
||||||
S(-2,-54), S( 8,-30), S( 8,-18), S( 8, -6), S( 8, -6), S( 8,-18), S( 8,-30), S(-2,-54),
|
|
||||||
S(-2,-42), S( 8,-18), S( 8, -6), S( 8, 6), S( 8, 6), S( 8, -6), S( 8,-18), S(-2,-42),
|
|
||||||
S(-2,-30), S( 8, -6), S( 8, 6), S( 8, 18), S( 8, 18), S( 8, 6), S( 8, -6), S(-2,-30),
|
|
||||||
S(-2,-30), S( 8, -6), S( 8, 6), S( 8, 18), S( 8, 18), S( 8, 6), S( 8, -6), S(-2,-30),
|
|
||||||
S(-2,-42), S( 8,-18), S( 8, -6), S( 8, 6), S( 8, 6), S( 8, -6), S( 8,-18), S(-2,-42),
|
|
||||||
S(-2,-54), S( 8,-30), S( 8,-18), S( 8, -6), S( 8, -6), S( 8,-18), S( 8,-30), S(-2,-54),
|
|
||||||
S(-2,-80), S(-2,-54), S(-2,-42), S(-2,-30), S(-2,-30), S(-2,-42), S(-2,-54), S(-2,-80)
|
|
||||||
},
|
|
||||||
{ // King
|
|
||||||
S(298, 27), S(332, 81), S(273,108), S(225,116), S(225,116), S(273,108), S(332, 81), S(298, 27),
|
|
||||||
S(287, 74), S(321,128), S(262,155), S(214,163), S(214,163), S(262,155), S(321,128), S(287, 74),
|
|
||||||
S(224,111), S(258,165), S(199,192), S(151,200), S(151,200), S(199,192), S(258,165), S(224,111),
|
|
||||||
S(196,135), S(230,189), S(171,216), S(123,224), S(123,224), S(171,216), S(230,189), S(196,135),
|
|
||||||
S(173,135), S(207,189), S(148,216), S(100,224), S(100,224), S(148,216), S(207,189), S(173,135),
|
|
||||||
S(146,111), S(180,165), S(121,192), S( 73,200), S( 73,200), S(121,192), S(180,165), S(146,111),
|
|
||||||
S(119, 74), S(153,128), S( 94,155), S( 46,163), S( 46,163), S( 94,155), S(153,128), S(119, 74),
|
|
||||||
S( 98, 27), S(132, 81), S( 73,108), S( 25,116), S( 25,116), S( 73,108), S(132, 81), S( 98, 27)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#undef S
|
|
||||||
|
|
||||||
#endif // #ifndef PSQTAB_H_INCLUDED
|
|
||||||
-81
@@ -1,81 +0,0 @@
|
|||||||
/*
|
|
||||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
||||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
|
||||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
Stockfish is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
This file is based on original code by Heinz van Saanen and is
|
|
||||||
available under 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef RKISS_H_INCLUDED
|
|
||||||
#define RKISS_H_INCLUDED
|
|
||||||
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
/// RKISS is our pseudo random number generator (PRNG) used to compute hash keys.
|
|
||||||
/// George Marsaglia invented the RNG-Kiss-family in the early 90's. This is a
|
|
||||||
/// specific version that Heinz van Saanen derived from some public domain code
|
|
||||||
/// by Bob Jenkins. Following the feature list, as tested by Heinz.
|
|
||||||
///
|
|
||||||
/// - Quite platform independent
|
|
||||||
/// - Passes ALL dieharder tests! Here *nix sys-rand() e.g. fails miserably:-)
|
|
||||||
/// - ~12 times faster than my *nix sys-rand()
|
|
||||||
/// - ~4 times faster than SSE2-version of Mersenne twister
|
|
||||||
/// - Average cycle length: ~2^126
|
|
||||||
/// - 64 bit seed
|
|
||||||
/// - Return doubles with a full 53 bit mantissa
|
|
||||||
/// - Thread safe
|
|
||||||
|
|
||||||
class RKISS {
|
|
||||||
|
|
||||||
uint64_t a, b, c, d;
|
|
||||||
|
|
||||||
uint64_t rotate_L(uint64_t x, unsigned k) const {
|
|
||||||
return (x << k) | (x >> (64 - k));
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t rand64() {
|
|
||||||
|
|
||||||
const uint64_t e = a - rotate_L(b, 7);
|
|
||||||
a = b ^ rotate_L(c, 13);
|
|
||||||
b = c + rotate_L(d, 37);
|
|
||||||
c = d + e;
|
|
||||||
return d = e + a;
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
RKISS(int seed = 73) {
|
|
||||||
|
|
||||||
a = 0xF1EA5EED, b = c = d = 0xD4E12C77;
|
|
||||||
|
|
||||||
for (int i = 0; i < seed; ++i) // Scramble a few rounds
|
|
||||||
rand64();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T> T rand() { return T(rand64()); }
|
|
||||||
|
|
||||||
/// Special generator used to fast init magic numbers. Here the
|
|
||||||
/// trick is to rotate the randoms of a given quantity 's' known
|
|
||||||
/// to be optimal to quickly find a good magic candidate.
|
|
||||||
template<typename T> T magic_rand(int s) {
|
|
||||||
return rotate_L(rotate_L(rand<T>(), (s >> 0) & 0x3F) & rand<T>()
|
|
||||||
, (s >> 6) & 0x3F) & rand<T>();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // #ifndef RKISS_H_INCLUDED
|
|
||||||
+1025
-1029
File diff suppressed because it is too large
Load Diff
+44
-47
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,94 +21,90 @@
|
|||||||
#ifndef SEARCH_H_INCLUDED
|
#ifndef SEARCH_H_INCLUDED
|
||||||
#define SEARCH_H_INCLUDED
|
#define SEARCH_H_INCLUDED
|
||||||
|
|
||||||
#include <memory>
|
#include <atomic>
|
||||||
#include <stack>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "position.h"
|
#include "movepick.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
struct SplitPoint;
|
class Position;
|
||||||
|
|
||||||
namespace Search {
|
namespace Search {
|
||||||
|
|
||||||
/// The Stack struct keeps track of the information we need to remember from
|
/// Stack struct keeps track of the information we need to remember from nodes
|
||||||
/// nodes shallower and deeper in the tree during the search. Each search thread
|
/// shallower and deeper in the tree during the search. Each search thread has
|
||||||
/// 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 {
|
||||||
SplitPoint* splitPoint;
|
Move* pv;
|
||||||
int ply;
|
int ply;
|
||||||
Move currentMove;
|
Move currentMove;
|
||||||
Move ttMove;
|
|
||||||
Move excludedMove;
|
Move excludedMove;
|
||||||
Move killers[2];
|
Move killers[2];
|
||||||
Depth reduction;
|
|
||||||
Value staticEval;
|
Value staticEval;
|
||||||
int skipNullMove;
|
Value history;
|
||||||
|
bool skipEarlyPruning;
|
||||||
|
int moveCount;
|
||||||
|
CounterMoveStats* counterMoves;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// RootMove struct is used for moves at the root of the tree. For each root
|
/// RootMove struct is used for moves at the root of the tree. For each root move
|
||||||
/// move we store a score, a node count, and a PV (really a refutation in the
|
/// we store a score and a PV (really a refutation in the case of moves which
|
||||||
/// case of moves which fail low). Score is normally set at -VALUE_INFINITE for
|
/// fail low). Score is normally set at -VALUE_INFINITE for all non-pv moves.
|
||||||
/// all non-pv moves.
|
|
||||||
struct RootMove {
|
struct RootMove {
|
||||||
|
|
||||||
RootMove(Move m) : score(-VALUE_INFINITE), prevScore(-VALUE_INFINITE) {
|
explicit RootMove(Move m) : pv(1, m) {}
|
||||||
pv.push_back(m); pv.push_back(MOVE_NONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator<(const RootMove& m) const { return score > m.score; } // Ascending sort
|
bool operator<(const RootMove& m) const { return m.score < score; } // Descending sort
|
||||||
bool operator==(const Move& m) const { return pv[0] == m; }
|
bool operator==(const Move& m) const { return pv[0] == m; }
|
||||||
|
bool extract_ponder_from_tt(Position& pos);
|
||||||
|
|
||||||
void extract_pv_from_tt(Position& pos);
|
Value score = -VALUE_INFINITE;
|
||||||
void insert_pv_in_tt(Position& pos);
|
Value previousScore = -VALUE_INFINITE;
|
||||||
|
|
||||||
Value score;
|
|
||||||
Value prevScore;
|
|
||||||
std::vector<Move> pv;
|
std::vector<Move> pv;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef std::vector<RootMove> RootMoves;
|
||||||
|
|
||||||
/// The LimitsType struct stores information sent by GUI about available time
|
|
||||||
/// to search the current move, maximum depth/time, if we are in analysis mode
|
/// LimitsType struct stores information sent by GUI about available time to
|
||||||
/// or if we have to ponder while it's our opponent's turn to move.
|
/// search the current move, maximum depth/time, if we are in analysis mode or
|
||||||
|
/// if we have to ponder while it's our opponent's turn to move.
|
||||||
|
|
||||||
struct LimitsType {
|
struct LimitsType {
|
||||||
|
|
||||||
LimitsType() { // Using memset on a std::vector is undefined behavior
|
LimitsType() { // Init explicitly due to broken value-initialization of non POD in MSVC
|
||||||
time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = movestogo =
|
nodes = time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] =
|
||||||
depth = nodes = movetime = mate = infinite = ponder = 0;
|
npmsec = movestogo = depth = movetime = mate = infinite = ponder = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool use_time_management() const {
|
||||||
|
return !(mate | movetime | depth | nodes | infinite);
|
||||||
}
|
}
|
||||||
bool use_time_management() const { return !(mate | movetime | depth | nodes | infinite); }
|
|
||||||
|
|
||||||
std::vector<Move> searchmoves;
|
std::vector<Move> searchmoves;
|
||||||
int time[COLOR_NB], inc[COLOR_NB], movestogo, depth, nodes, movetime, mate, infinite, ponder;
|
int time[COLOR_NB], inc[COLOR_NB], npmsec, movestogo, depth, movetime, mate, infinite, ponder;
|
||||||
|
int64_t nodes;
|
||||||
|
TimePoint startTime;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// The SignalsType struct stores volatile flags updated during the search
|
/// SignalsType struct stores atomic flags updated during the search, typically
|
||||||
/// typically in an async fashion e.g. to stop the search by the GUI.
|
/// in an async fashion e.g. to stop the search by the GUI.
|
||||||
|
|
||||||
struct SignalsType {
|
struct SignalsType {
|
||||||
bool stop, stopOnPonderhit, firstRootMove, failedLowAtRoot;
|
std::atomic_bool stop, stopOnPonderhit;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::auto_ptr<std::stack<StateInfo> > StateStackPtr;
|
extern SignalsType Signals;
|
||||||
|
|
||||||
extern volatile SignalsType Signals;
|
|
||||||
extern LimitsType Limits;
|
extern LimitsType Limits;
|
||||||
extern std::vector<RootMove> RootMoves;
|
|
||||||
extern Position RootPos;
|
|
||||||
extern Color RootColor;
|
|
||||||
extern Time::point SearchTime;
|
|
||||||
extern StateStackPtr SetupStates;
|
|
||||||
|
|
||||||
extern void init();
|
void init();
|
||||||
extern uint64_t perft(Position& pos, Depth depth);
|
void clear();
|
||||||
extern void think();
|
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
@@ -0,0 +1,169 @@
|
|||||||
|
/*
|
||||||
|
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
|
||||||
|
|
||||||
@@ -0,0 +1,824 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2013 Ronald de Man
|
||||||
|
This file may be redistributed and/or modified without restrictions.
|
||||||
|
|
||||||
|
tbprobe.cpp contains the Stockfish-specific routines of the
|
||||||
|
tablebase probing code. It should be relatively easy to adapt
|
||||||
|
this code to other chess engines.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define NOMINMAX
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include "../position.h"
|
||||||
|
#include "../movegen.h"
|
||||||
|
#include "../bitboard.h"
|
||||||
|
#include "../search.h"
|
||||||
|
|
||||||
|
#include "tbprobe.h"
|
||||||
|
#include "tbcore.h"
|
||||||
|
|
||||||
|
#include "tbcore.cpp"
|
||||||
|
|
||||||
|
namespace Zobrist {
|
||||||
|
extern Key psq[PIECE_NB][SQUARE_NB];
|
||||||
|
}
|
||||||
|
|
||||||
|
int Tablebases::MaxCardinality = 0;
|
||||||
|
|
||||||
|
// Given a position with 6 or fewer pieces, produce a text string
|
||||||
|
// of the form KQPvKRP, where "KQP" represents the white pieces if
|
||||||
|
// mirror == 0 and the black pieces if mirror == 1.
|
||||||
|
static void prt_str(Position& pos, char *str, int mirror)
|
||||||
|
{
|
||||||
|
Color color;
|
||||||
|
PieceType pt;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
color = !mirror ? WHITE : BLACK;
|
||||||
|
for (pt = KING; pt >= PAWN; --pt)
|
||||||
|
for (i = popcount(pos.pieces(color, pt)); i > 0; i--)
|
||||||
|
*str++ = pchr[6 - pt];
|
||||||
|
*str++ = 'v';
|
||||||
|
color = ~color;
|
||||||
|
for (pt = KING; pt >= PAWN; --pt)
|
||||||
|
for (i = popcount(pos.pieces(color, pt)); i > 0; i--)
|
||||||
|
*str++ = pchr[6 - pt];
|
||||||
|
*str++ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Given a position, produce a 64-bit material signature key.
|
||||||
|
// If the engine supports such a key, it should equal the engine's key.
|
||||||
|
static uint64 calc_key(Position& pos, int mirror)
|
||||||
|
{
|
||||||
|
Color color;
|
||||||
|
PieceType pt;
|
||||||
|
int i;
|
||||||
|
uint64 key = 0;
|
||||||
|
|
||||||
|
color = !mirror ? WHITE : BLACK;
|
||||||
|
for (pt = PAWN; pt <= KING; ++pt)
|
||||||
|
for (i = popcount(pos.pieces(color, pt)); i > 0; i--)
|
||||||
|
key ^= Zobrist::psq[make_piece(WHITE, pt)][i - 1];
|
||||||
|
color = ~color;
|
||||||
|
for (pt = PAWN; pt <= KING; ++pt)
|
||||||
|
for (i = popcount(pos.pieces(color, pt)); i > 0; i--)
|
||||||
|
key ^= Zobrist::psq[make_piece(BLACK, pt)][i - 1];
|
||||||
|
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Produce a 64-bit material key corresponding to the material combination
|
||||||
|
// defined by pcs[16], where pcs[1], ..., pcs[6] is the number of white
|
||||||
|
// pawns, ..., kings and pcs[9], ..., pcs[14] is the number of black
|
||||||
|
// pawns, ..., kings.
|
||||||
|
static uint64 calc_key_from_pcs(int *pcs, int mirror)
|
||||||
|
{
|
||||||
|
int color;
|
||||||
|
PieceType pt;
|
||||||
|
int i;
|
||||||
|
uint64 key = 0;
|
||||||
|
|
||||||
|
color = !mirror ? 0 : 8;
|
||||||
|
for (pt = PAWN; pt <= KING; ++pt)
|
||||||
|
for (i = 0; i < pcs[color + pt]; i++)
|
||||||
|
key ^= Zobrist::psq[make_piece(WHITE, pt)][i];
|
||||||
|
color ^= 8;
|
||||||
|
for (pt = PAWN; pt <= KING; ++pt)
|
||||||
|
for (i = 0; i < pcs[color + pt]; i++)
|
||||||
|
key ^= Zobrist::psq[make_piece(BLACK, pt)][i];
|
||||||
|
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_little_endian() {
|
||||||
|
union {
|
||||||
|
int i;
|
||||||
|
char c[sizeof(int)];
|
||||||
|
} x;
|
||||||
|
x.i = 1;
|
||||||
|
return x.c[0] == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ubyte decompress_pairs(struct PairsData *d, uint64 idx)
|
||||||
|
{
|
||||||
|
static const bool isLittleEndian = is_little_endian();
|
||||||
|
return isLittleEndian ? decompress_pairs<true >(d, idx)
|
||||||
|
: decompress_pairs<false>(d, idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
// probe_wdl_table and probe_dtz_table require similar adaptations.
|
||||||
|
static int probe_wdl_table(Position& pos, int *success)
|
||||||
|
{
|
||||||
|
struct TBEntry *ptr;
|
||||||
|
struct TBHashEntry *ptr2;
|
||||||
|
uint64 idx;
|
||||||
|
uint64 key;
|
||||||
|
int i;
|
||||||
|
ubyte res;
|
||||||
|
int p[TBPIECES];
|
||||||
|
|
||||||
|
// Obtain the position's material signature key.
|
||||||
|
key = pos.material_key();
|
||||||
|
|
||||||
|
// Test for KvK.
|
||||||
|
if (key == (Zobrist::psq[W_KING][0] ^ Zobrist::psq[B_KING][0]))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
ptr2 = TB_hash[key >> (64 - TBHASHBITS)];
|
||||||
|
for (i = 0; i < HSHMAX; i++)
|
||||||
|
if (ptr2[i].key == key) break;
|
||||||
|
if (i == HSHMAX) {
|
||||||
|
*success = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr = ptr2[i].ptr;
|
||||||
|
if (!ptr->ready) {
|
||||||
|
LOCK(TB_mutex);
|
||||||
|
if (!ptr->ready) {
|
||||||
|
char str[16];
|
||||||
|
prt_str(pos, str, ptr->key != key);
|
||||||
|
if (!init_table_wdl(ptr, str)) {
|
||||||
|
ptr2[i].key = 0ULL;
|
||||||
|
*success = 0;
|
||||||
|
UNLOCK(TB_mutex);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
// Memory barrier to ensure ptr->ready = 1 is not reordered.
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
_ReadWriteBarrier();
|
||||||
|
#else
|
||||||
|
__asm__ __volatile__ ("" ::: "memory");
|
||||||
|
#endif
|
||||||
|
ptr->ready = 1;
|
||||||
|
}
|
||||||
|
UNLOCK(TB_mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
int bside, mirror, cmirror;
|
||||||
|
if (!ptr->symmetric) {
|
||||||
|
if (key != ptr->key) {
|
||||||
|
cmirror = 8;
|
||||||
|
mirror = 0x38;
|
||||||
|
bside = (pos.side_to_move() == WHITE);
|
||||||
|
} else {
|
||||||
|
cmirror = mirror = 0;
|
||||||
|
bside = !(pos.side_to_move() == WHITE);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cmirror = pos.side_to_move() == WHITE ? 0 : 8;
|
||||||
|
mirror = pos.side_to_move() == WHITE ? 0 : 0x38;
|
||||||
|
bside = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// p[i] is to contain the square 0-63 (A1-H8) for a piece of type
|
||||||
|
// pc[i] ^ cmirror, where 1 = white pawn, ..., 14 = black king.
|
||||||
|
// Pieces of the same type are guaranteed to be consecutive.
|
||||||
|
if (!ptr->has_pawns) {
|
||||||
|
struct TBEntry_piece *entry = (struct TBEntry_piece *)ptr;
|
||||||
|
ubyte *pc = entry->pieces[bside];
|
||||||
|
for (i = 0; i < entry->num;) {
|
||||||
|
Bitboard bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3),
|
||||||
|
(PieceType)(pc[i] & 0x07));
|
||||||
|
do {
|
||||||
|
p[i++] = pop_lsb(&bb);
|
||||||
|
} while (bb);
|
||||||
|
}
|
||||||
|
idx = encode_piece(entry, entry->norm[bside], p, entry->factor[bside]);
|
||||||
|
res = decompress_pairs(entry->precomp[bside], idx);
|
||||||
|
} else {
|
||||||
|
struct TBEntry_pawn *entry = (struct TBEntry_pawn *)ptr;
|
||||||
|
int k = entry->file[0].pieces[0][0] ^ cmirror;
|
||||||
|
Bitboard bb = pos.pieces((Color)(k >> 3), (PieceType)(k & 0x07));
|
||||||
|
i = 0;
|
||||||
|
do {
|
||||||
|
p[i++] = pop_lsb(&bb) ^ mirror;
|
||||||
|
} while (bb);
|
||||||
|
int f = pawn_file(entry, p);
|
||||||
|
ubyte *pc = entry->file[f].pieces[bside];
|
||||||
|
for (; i < entry->num;) {
|
||||||
|
bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3),
|
||||||
|
(PieceType)(pc[i] & 0x07));
|
||||||
|
do {
|
||||||
|
p[i++] = pop_lsb(&bb) ^ mirror;
|
||||||
|
} while (bb);
|
||||||
|
}
|
||||||
|
idx = encode_pawn(entry, entry->file[f].norm[bside], p, entry->file[f].factor[bside]);
|
||||||
|
res = decompress_pairs(entry->file[f].precomp[bside], idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((int)res) - 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int probe_dtz_table(Position& pos, int wdl, int *success)
|
||||||
|
{
|
||||||
|
struct TBEntry *ptr;
|
||||||
|
uint64 idx;
|
||||||
|
int i, res;
|
||||||
|
int p[TBPIECES];
|
||||||
|
|
||||||
|
// Obtain the position's material signature key.
|
||||||
|
uint64 key = pos.material_key();
|
||||||
|
|
||||||
|
if (DTZ_table[0].key1 != key && DTZ_table[0].key2 != key) {
|
||||||
|
for (i = 1; i < DTZ_ENTRIES; i++)
|
||||||
|
if (DTZ_table[i].key1 == key) break;
|
||||||
|
if (i < DTZ_ENTRIES) {
|
||||||
|
struct DTZTableEntry table_entry = DTZ_table[i];
|
||||||
|
for (; i > 0; i--)
|
||||||
|
DTZ_table[i] = DTZ_table[i - 1];
|
||||||
|
DTZ_table[0] = table_entry;
|
||||||
|
} else {
|
||||||
|
struct TBHashEntry *ptr2 = TB_hash[key >> (64 - TBHASHBITS)];
|
||||||
|
for (i = 0; i < HSHMAX; i++)
|
||||||
|
if (ptr2[i].key == key) break;
|
||||||
|
if (i == HSHMAX) {
|
||||||
|
*success = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
ptr = ptr2[i].ptr;
|
||||||
|
char str[16];
|
||||||
|
int mirror = (ptr->key != key);
|
||||||
|
prt_str(pos, str, mirror);
|
||||||
|
if (DTZ_table[DTZ_ENTRIES - 1].entry)
|
||||||
|
free_dtz_entry(DTZ_table[DTZ_ENTRIES-1].entry);
|
||||||
|
for (i = DTZ_ENTRIES - 1; i > 0; i--)
|
||||||
|
DTZ_table[i] = DTZ_table[i - 1];
|
||||||
|
load_dtz_table(str, calc_key(pos, mirror), calc_key(pos, !mirror));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ptr = DTZ_table[0].entry;
|
||||||
|
if (!ptr) {
|
||||||
|
*success = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bside, mirror, cmirror;
|
||||||
|
if (!ptr->symmetric) {
|
||||||
|
if (key != ptr->key) {
|
||||||
|
cmirror = 8;
|
||||||
|
mirror = 0x38;
|
||||||
|
bside = (pos.side_to_move() == WHITE);
|
||||||
|
} else {
|
||||||
|
cmirror = mirror = 0;
|
||||||
|
bside = !(pos.side_to_move() == WHITE);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cmirror = pos.side_to_move() == WHITE ? 0 : 8;
|
||||||
|
mirror = pos.side_to_move() == WHITE ? 0 : 0x38;
|
||||||
|
bside = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ptr->has_pawns) {
|
||||||
|
struct DTZEntry_piece *entry = (struct DTZEntry_piece *)ptr;
|
||||||
|
if ((entry->flags & 1) != bside && !entry->symmetric) {
|
||||||
|
*success = -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
ubyte *pc = entry->pieces;
|
||||||
|
for (i = 0; i < entry->num;) {
|
||||||
|
Bitboard bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3),
|
||||||
|
(PieceType)(pc[i] & 0x07));
|
||||||
|
do {
|
||||||
|
p[i++] = pop_lsb(&bb);
|
||||||
|
} while (bb);
|
||||||
|
}
|
||||||
|
idx = encode_piece((struct TBEntry_piece *)entry, entry->norm, p, entry->factor);
|
||||||
|
res = decompress_pairs(entry->precomp, idx);
|
||||||
|
|
||||||
|
if (entry->flags & 2)
|
||||||
|
res = entry->map[entry->map_idx[wdl_to_map[wdl + 2]] + res];
|
||||||
|
|
||||||
|
if (!(entry->flags & pa_flags[wdl + 2]) || (wdl & 1))
|
||||||
|
res *= 2;
|
||||||
|
} else {
|
||||||
|
struct DTZEntry_pawn *entry = (struct DTZEntry_pawn *)ptr;
|
||||||
|
int k = entry->file[0].pieces[0] ^ cmirror;
|
||||||
|
Bitboard bb = pos.pieces((Color)(k >> 3), (PieceType)(k & 0x07));
|
||||||
|
i = 0;
|
||||||
|
do {
|
||||||
|
p[i++] = pop_lsb(&bb) ^ mirror;
|
||||||
|
} while (bb);
|
||||||
|
int f = pawn_file((struct TBEntry_pawn *)entry, p);
|
||||||
|
if ((entry->flags[f] & 1) != bside) {
|
||||||
|
*success = -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
ubyte *pc = entry->file[f].pieces;
|
||||||
|
for (; i < entry->num;) {
|
||||||
|
bb = pos.pieces((Color)((pc[i] ^ cmirror) >> 3),
|
||||||
|
(PieceType)(pc[i] & 0x07));
|
||||||
|
do {
|
||||||
|
p[i++] = pop_lsb(&bb) ^ mirror;
|
||||||
|
} while (bb);
|
||||||
|
}
|
||||||
|
idx = encode_pawn((struct TBEntry_pawn *)entry, entry->file[f].norm, p, entry->file[f].factor);
|
||||||
|
res = decompress_pairs(entry->file[f].precomp, idx);
|
||||||
|
|
||||||
|
if (entry->flags[f] & 2)
|
||||||
|
res = entry->map[entry->map_idx[f][wdl_to_map[wdl + 2]] + res];
|
||||||
|
|
||||||
|
if (!(entry->flags[f] & pa_flags[wdl + 2]) || (wdl & 1))
|
||||||
|
res *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add underpromotion captures to list of captures.
|
||||||
|
static ExtMove *add_underprom_caps(Position& pos, ExtMove *stack, ExtMove *end)
|
||||||
|
{
|
||||||
|
ExtMove *moves, *extra = end;
|
||||||
|
|
||||||
|
for (moves = stack; moves < end; moves++) {
|
||||||
|
Move move = moves->move;
|
||||||
|
if (type_of(move) == PROMOTION && !pos.empty(to_sq(move))) {
|
||||||
|
(*extra++).move = (Move)(move - (1 << 12));
|
||||||
|
(*extra++).move = (Move)(move - (2 << 12));
|
||||||
|
(*extra++).move = (Move)(move - (3 << 12));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return extra;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int probe_ab(Position& pos, int alpha, int beta, int *success)
|
||||||
|
{
|
||||||
|
int v;
|
||||||
|
ExtMove stack[64];
|
||||||
|
ExtMove *moves, *end;
|
||||||
|
StateInfo st;
|
||||||
|
|
||||||
|
// Generate (at least) all legal non-ep captures including (under)promotions.
|
||||||
|
// It is OK to generate more, as long as they are filtered out below.
|
||||||
|
if (!pos.checkers()) {
|
||||||
|
end = generate<CAPTURES>(pos, stack);
|
||||||
|
// Since underpromotion captures are not included, we need to add them.
|
||||||
|
end = add_underprom_caps(pos, stack, end);
|
||||||
|
} else
|
||||||
|
end = generate<EVASIONS>(pos, stack);
|
||||||
|
|
||||||
|
for (moves = stack; moves < end; moves++) {
|
||||||
|
Move capture = moves->move;
|
||||||
|
if (!pos.capture(capture) || type_of(capture) == ENPASSANT
|
||||||
|
|| !pos.legal(capture))
|
||||||
|
continue;
|
||||||
|
pos.do_move(capture, st, pos.gives_check(capture));
|
||||||
|
v = -probe_ab(pos, -beta, -alpha, success);
|
||||||
|
pos.undo_move(capture);
|
||||||
|
if (*success == 0) return 0;
|
||||||
|
if (v > alpha) {
|
||||||
|
if (v >= beta) {
|
||||||
|
*success = 2;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
alpha = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
v = probe_wdl_table(pos, success);
|
||||||
|
if (*success == 0) return 0;
|
||||||
|
if (alpha >= v) {
|
||||||
|
*success = 1 + (alpha > 0);
|
||||||
|
return alpha;
|
||||||
|
} else {
|
||||||
|
*success = 1;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Probe the WDL table for a particular position.
|
||||||
|
// If *success != 0, the probe was successful.
|
||||||
|
// The return value is from the point of view of the side to move:
|
||||||
|
// -2 : loss
|
||||||
|
// -1 : loss, but draw under 50-move rule
|
||||||
|
// 0 : draw
|
||||||
|
// 1 : win, but draw under 50-move rule
|
||||||
|
// 2 : win
|
||||||
|
int Tablebases::probe_wdl(Position& pos, int *success)
|
||||||
|
{
|
||||||
|
int v;
|
||||||
|
|
||||||
|
*success = 1;
|
||||||
|
v = probe_ab(pos, -2, 2, success);
|
||||||
|
|
||||||
|
// If en passant is not possible, we are done.
|
||||||
|
if (pos.ep_square() == SQ_NONE)
|
||||||
|
return v;
|
||||||
|
if (!(*success)) return 0;
|
||||||
|
|
||||||
|
// Now handle en passant.
|
||||||
|
int v1 = -3;
|
||||||
|
// Generate (at least) all legal en passant captures.
|
||||||
|
ExtMove stack[192];
|
||||||
|
ExtMove *moves, *end;
|
||||||
|
StateInfo st;
|
||||||
|
|
||||||
|
if (!pos.checkers())
|
||||||
|
end = generate<CAPTURES>(pos, stack);
|
||||||
|
else
|
||||||
|
end = generate<EVASIONS>(pos, stack);
|
||||||
|
|
||||||
|
for (moves = stack; moves < end; moves++) {
|
||||||
|
Move capture = moves->move;
|
||||||
|
if (type_of(capture) != ENPASSANT
|
||||||
|
|| !pos.legal(capture))
|
||||||
|
continue;
|
||||||
|
pos.do_move(capture, st, pos.gives_check(capture));
|
||||||
|
int v0 = -probe_ab(pos, -2, 2, success);
|
||||||
|
pos.undo_move(capture);
|
||||||
|
if (*success == 0) return 0;
|
||||||
|
if (v0 > v1) v1 = v0;
|
||||||
|
}
|
||||||
|
if (v1 > -3) {
|
||||||
|
if (v1 >= v) v = v1;
|
||||||
|
else if (v == 0) {
|
||||||
|
// Check whether there is at least one legal non-ep move.
|
||||||
|
for (moves = stack; moves < end; moves++) {
|
||||||
|
Move capture = moves->move;
|
||||||
|
if (type_of(capture) == ENPASSANT) continue;
|
||||||
|
if (pos.legal(capture)) break;
|
||||||
|
}
|
||||||
|
if (moves == end && !pos.checkers()) {
|
||||||
|
end = generate<QUIETS>(pos, end);
|
||||||
|
for (; moves < end; moves++) {
|
||||||
|
Move move = moves->move;
|
||||||
|
if (pos.legal(move))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If not, then we are forced to play the losing ep capture.
|
||||||
|
if (moves == end)
|
||||||
|
v = v1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This routine treats a position with en passant captures as one without.
|
||||||
|
static int probe_dtz_no_ep(Position& pos, int *success)
|
||||||
|
{
|
||||||
|
int wdl, dtz;
|
||||||
|
|
||||||
|
wdl = probe_ab(pos, -2, 2, success);
|
||||||
|
if (*success == 0) return 0;
|
||||||
|
|
||||||
|
if (wdl == 0) return 0;
|
||||||
|
|
||||||
|
if (*success == 2)
|
||||||
|
return wdl == 2 ? 1 : 101;
|
||||||
|
|
||||||
|
ExtMove stack[192];
|
||||||
|
ExtMove *moves, *end = NULL;
|
||||||
|
StateInfo st;
|
||||||
|
|
||||||
|
if (wdl > 0) {
|
||||||
|
// Generate at least all legal non-capturing pawn moves
|
||||||
|
// including non-capturing promotions.
|
||||||
|
if (!pos.checkers())
|
||||||
|
end = generate<NON_EVASIONS>(pos, stack);
|
||||||
|
else
|
||||||
|
end = generate<EVASIONS>(pos, stack);
|
||||||
|
|
||||||
|
for (moves = stack; moves < end; moves++) {
|
||||||
|
Move move = moves->move;
|
||||||
|
if (type_of(pos.moved_piece(move)) != PAWN || pos.capture(move)
|
||||||
|
|| !pos.legal(move))
|
||||||
|
continue;
|
||||||
|
pos.do_move(move, st, pos.gives_check(move));
|
||||||
|
int v = -Tablebases::probe_wdl(pos, success);
|
||||||
|
pos.undo_move(move);
|
||||||
|
if (*success == 0) return 0;
|
||||||
|
if (v == wdl)
|
||||||
|
return v == 2 ? 1 : 101;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dtz = 1 + probe_dtz_table(pos, wdl, success);
|
||||||
|
if (*success >= 0) {
|
||||||
|
if (wdl & 1) dtz += 100;
|
||||||
|
return wdl >= 0 ? dtz : -dtz;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wdl > 0) {
|
||||||
|
int best = 0xffff;
|
||||||
|
for (moves = stack; moves < end; moves++) {
|
||||||
|
Move move = moves->move;
|
||||||
|
if (pos.capture(move) || type_of(pos.moved_piece(move)) == PAWN
|
||||||
|
|| !pos.legal(move))
|
||||||
|
continue;
|
||||||
|
pos.do_move(move, st, pos.gives_check(move));
|
||||||
|
int v = -Tablebases::probe_dtz(pos, success);
|
||||||
|
pos.undo_move(move);
|
||||||
|
if (*success == 0) return 0;
|
||||||
|
if (v > 0 && v + 1 < best)
|
||||||
|
best = v + 1;
|
||||||
|
}
|
||||||
|
return best;
|
||||||
|
} else {
|
||||||
|
int best = -1;
|
||||||
|
if (!pos.checkers())
|
||||||
|
end = generate<NON_EVASIONS>(pos, stack);
|
||||||
|
else
|
||||||
|
end = generate<EVASIONS>(pos, stack);
|
||||||
|
for (moves = stack; moves < end; moves++) {
|
||||||
|
int v;
|
||||||
|
Move move = moves->move;
|
||||||
|
if (!pos.legal(move))
|
||||||
|
continue;
|
||||||
|
pos.do_move(move, st, pos.gives_check(move));
|
||||||
|
if (st.rule50 == 0) {
|
||||||
|
if (wdl == -2) v = -1;
|
||||||
|
else {
|
||||||
|
v = probe_ab(pos, 1, 2, success);
|
||||||
|
v = (v == 2) ? 0 : -101;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
v = -Tablebases::probe_dtz(pos, success) - 1;
|
||||||
|
}
|
||||||
|
pos.undo_move(move);
|
||||||
|
if (*success == 0) return 0;
|
||||||
|
if (v < best)
|
||||||
|
best = v;
|
||||||
|
}
|
||||||
|
return best;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int wdl_to_dtz[] = {
|
||||||
|
-1, -101, 0, 101, 1
|
||||||
|
};
|
||||||
|
|
||||||
|
// Probe the DTZ table for a particular position.
|
||||||
|
// If *success != 0, the probe was successful.
|
||||||
|
// The return value is from the point of view of the side to move:
|
||||||
|
// n < -100 : loss, but draw under 50-move rule
|
||||||
|
// -100 <= n < -1 : loss in n ply (assuming 50-move counter == 0)
|
||||||
|
// 0 : draw
|
||||||
|
// 1 < n <= 100 : win in n ply (assuming 50-move counter == 0)
|
||||||
|
// 100 < n : win, but draw under 50-move rule
|
||||||
|
//
|
||||||
|
// The return value n can be off by 1: a return value -n can mean a loss
|
||||||
|
// in n+1 ply and a return value +n can mean a win in n+1 ply. This
|
||||||
|
// cannot happen for tables with positions exactly on the "edge" of
|
||||||
|
// the 50-move rule.
|
||||||
|
//
|
||||||
|
// This implies that if dtz > 0 is returned, the position is certainly
|
||||||
|
// a win if dtz + 50-move-counter <= 99. Care must be taken that the engine
|
||||||
|
// picks moves that preserve dtz + 50-move-counter <= 99.
|
||||||
|
//
|
||||||
|
// If n = 100 immediately after a capture or pawn move, then the position
|
||||||
|
// is also certainly a win, and during the whole phase until the next
|
||||||
|
// capture or pawn move, the inequality to be preserved is
|
||||||
|
// dtz + 50-movecounter <= 100.
|
||||||
|
//
|
||||||
|
// In short, if a move is available resulting in dtz + 50-move-counter <= 99,
|
||||||
|
// then do not accept moves leading to dtz + 50-move-counter == 100.
|
||||||
|
//
|
||||||
|
int Tablebases::probe_dtz(Position& pos, int *success)
|
||||||
|
{
|
||||||
|
*success = 1;
|
||||||
|
int v = probe_dtz_no_ep(pos, success);
|
||||||
|
|
||||||
|
if (pos.ep_square() == SQ_NONE)
|
||||||
|
return v;
|
||||||
|
if (*success == 0) return 0;
|
||||||
|
|
||||||
|
// Now handle en passant.
|
||||||
|
int v1 = -3;
|
||||||
|
|
||||||
|
ExtMove stack[192];
|
||||||
|
ExtMove *moves, *end;
|
||||||
|
StateInfo st;
|
||||||
|
|
||||||
|
if (!pos.checkers())
|
||||||
|
end = generate<CAPTURES>(pos, stack);
|
||||||
|
else
|
||||||
|
end = generate<EVASIONS>(pos, stack);
|
||||||
|
|
||||||
|
for (moves = stack; moves < end; moves++) {
|
||||||
|
Move capture = moves->move;
|
||||||
|
if (type_of(capture) != ENPASSANT
|
||||||
|
|| !pos.legal(capture))
|
||||||
|
continue;
|
||||||
|
pos.do_move(capture, st, pos.gives_check(capture));
|
||||||
|
int v0 = -probe_ab(pos, -2, 2, success);
|
||||||
|
pos.undo_move(capture);
|
||||||
|
if (*success == 0) return 0;
|
||||||
|
if (v0 > v1) v1 = v0;
|
||||||
|
}
|
||||||
|
if (v1 > -3) {
|
||||||
|
v1 = wdl_to_dtz[v1 + 2];
|
||||||
|
if (v < -100) {
|
||||||
|
if (v1 >= 0)
|
||||||
|
v = v1;
|
||||||
|
} else if (v < 0) {
|
||||||
|
if (v1 >= 0 || v1 < -100)
|
||||||
|
v = v1;
|
||||||
|
} else if (v > 100) {
|
||||||
|
if (v1 > 0)
|
||||||
|
v = v1;
|
||||||
|
} else if (v > 0) {
|
||||||
|
if (v1 == 1)
|
||||||
|
v = v1;
|
||||||
|
} else if (v1 >= 0) {
|
||||||
|
v = v1;
|
||||||
|
} else {
|
||||||
|
for (moves = stack; moves < end; moves++) {
|
||||||
|
Move move = moves->move;
|
||||||
|
if (type_of(move) == ENPASSANT) continue;
|
||||||
|
if (pos.legal(move)) break;
|
||||||
|
}
|
||||||
|
if (moves == end && !pos.checkers()) {
|
||||||
|
end = generate<QUIETS>(pos, end);
|
||||||
|
for (; moves < end; moves++) {
|
||||||
|
Move move = moves->move;
|
||||||
|
if (pos.legal(move))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (moves == end)
|
||||||
|
v = v1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether there has been at least one repetition of positions
|
||||||
|
// since the last capture or pawn move.
|
||||||
|
static int has_repeated(StateInfo *st)
|
||||||
|
{
|
||||||
|
while (1) {
|
||||||
|
int i = 4, e = std::min(st->rule50, st->pliesFromNull);
|
||||||
|
if (e < i)
|
||||||
|
return 0;
|
||||||
|
StateInfo *stp = st->previous->previous;
|
||||||
|
do {
|
||||||
|
stp = stp->previous->previous;
|
||||||
|
if (stp->key == st->key)
|
||||||
|
return 1;
|
||||||
|
i += 2;
|
||||||
|
} while (i <= e);
|
||||||
|
st = st->previous;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static Value wdl_to_Value[5] = {
|
||||||
|
-VALUE_MATE + MAX_PLY + 1,
|
||||||
|
VALUE_DRAW - 2,
|
||||||
|
VALUE_DRAW,
|
||||||
|
VALUE_DRAW + 2,
|
||||||
|
VALUE_MATE - MAX_PLY - 1
|
||||||
|
};
|
||||||
|
|
||||||
|
// Use the DTZ tables to filter out moves that don't preserve the win or draw.
|
||||||
|
// If the position is lost, but DTZ is fairly high, only keep moves that
|
||||||
|
// maximise DTZ.
|
||||||
|
//
|
||||||
|
// A return value false indicates that not all probes were successful and that
|
||||||
|
// no moves were filtered out.
|
||||||
|
bool Tablebases::root_probe(Position& pos, Search::RootMoves& rootMoves, Value& score)
|
||||||
|
{
|
||||||
|
int success;
|
||||||
|
|
||||||
|
int dtz = probe_dtz(pos, &success);
|
||||||
|
if (!success) return false;
|
||||||
|
|
||||||
|
StateInfo st;
|
||||||
|
|
||||||
|
// Probe each move.
|
||||||
|
for (size_t i = 0; i < rootMoves.size(); i++) {
|
||||||
|
Move move = rootMoves[i].pv[0];
|
||||||
|
pos.do_move(move, st, pos.gives_check(move));
|
||||||
|
int v = 0;
|
||||||
|
if (pos.checkers() && dtz > 0) {
|
||||||
|
ExtMove s[192];
|
||||||
|
if (generate<LEGAL>(pos, s) == s)
|
||||||
|
v = 1;
|
||||||
|
}
|
||||||
|
if (!v) {
|
||||||
|
if (st.rule50 != 0) {
|
||||||
|
v = -Tablebases::probe_dtz(pos, &success);
|
||||||
|
if (v > 0) v++;
|
||||||
|
else if (v < 0) v--;
|
||||||
|
} else {
|
||||||
|
v = -Tablebases::probe_wdl(pos, &success);
|
||||||
|
v = wdl_to_dtz[v + 2];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pos.undo_move(move);
|
||||||
|
if (!success) return false;
|
||||||
|
rootMoves[i].score = (Value)v;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtain 50-move counter for the root position.
|
||||||
|
// In Stockfish there seems to be no clean way, so we do it like this:
|
||||||
|
int cnt50 = st.previous->rule50;
|
||||||
|
|
||||||
|
// Use 50-move counter to determine whether the root position is
|
||||||
|
// won, lost or drawn.
|
||||||
|
int wdl = 0;
|
||||||
|
if (dtz > 0)
|
||||||
|
wdl = (dtz + cnt50 <= 100) ? 2 : 1;
|
||||||
|
else if (dtz < 0)
|
||||||
|
wdl = (-dtz + cnt50 <= 100) ? -2 : -1;
|
||||||
|
|
||||||
|
// Determine the score to report to the user.
|
||||||
|
score = wdl_to_Value[wdl + 2];
|
||||||
|
// If the position is winning or losing, but too few moves left, adjust the
|
||||||
|
// score to show how close it is to winning or losing.
|
||||||
|
// NOTE: int(PawnValueEg) is used as scaling factor in score_to_uci().
|
||||||
|
if (wdl == 1 && dtz <= 100)
|
||||||
|
score = (Value)(((200 - dtz - cnt50) * int(PawnValueEg)) / 200);
|
||||||
|
else if (wdl == -1 && dtz >= -100)
|
||||||
|
score = -(Value)(((200 + dtz - cnt50) * int(PawnValueEg)) / 200);
|
||||||
|
|
||||||
|
// Now be a bit smart about filtering out moves.
|
||||||
|
size_t j = 0;
|
||||||
|
if (dtz > 0) { // winning (or 50-move rule draw)
|
||||||
|
int best = 0xffff;
|
||||||
|
for (size_t i = 0; i < rootMoves.size(); i++) {
|
||||||
|
int v = rootMoves[i].score;
|
||||||
|
if (v > 0 && v < best)
|
||||||
|
best = v;
|
||||||
|
}
|
||||||
|
int max = best;
|
||||||
|
// If the current phase has not seen repetitions, then try all moves
|
||||||
|
// that stay safely within the 50-move budget, if there are any.
|
||||||
|
if (!has_repeated(st.previous) && best + cnt50 <= 99)
|
||||||
|
max = 99 - cnt50;
|
||||||
|
for (size_t i = 0; i < rootMoves.size(); i++) {
|
||||||
|
int v = rootMoves[i].score;
|
||||||
|
if (v > 0 && v <= max)
|
||||||
|
rootMoves[j++] = rootMoves[i];
|
||||||
|
}
|
||||||
|
} else if (dtz < 0) { // losing (or 50-move rule draw)
|
||||||
|
int best = 0;
|
||||||
|
for (size_t i = 0; i < rootMoves.size(); i++) {
|
||||||
|
int v = rootMoves[i].score;
|
||||||
|
if (v < best)
|
||||||
|
best = v;
|
||||||
|
}
|
||||||
|
// Try all moves, unless we approach or have a 50-move rule draw.
|
||||||
|
if (-best * 2 + cnt50 < 100)
|
||||||
|
return true;
|
||||||
|
for (size_t i = 0; i < rootMoves.size(); i++) {
|
||||||
|
if (rootMoves[i].score == best)
|
||||||
|
rootMoves[j++] = rootMoves[i];
|
||||||
|
}
|
||||||
|
} else { // drawing
|
||||||
|
// Try all moves that preserve the draw.
|
||||||
|
for (size_t i = 0; i < rootMoves.size(); i++) {
|
||||||
|
if (rootMoves[i].score == 0)
|
||||||
|
rootMoves[j++] = rootMoves[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rootMoves.resize(j, Search::RootMove(MOVE_NONE));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the WDL tables to filter out moves that don't preserve the win or draw.
|
||||||
|
// This is a fallback for the case that some or all DTZ tables are missing.
|
||||||
|
//
|
||||||
|
// A return value false indicates that not all probes were successful and that
|
||||||
|
// no moves were filtered out.
|
||||||
|
bool Tablebases::root_probe_wdl(Position& pos, Search::RootMoves& rootMoves, Value& score)
|
||||||
|
{
|
||||||
|
int success;
|
||||||
|
|
||||||
|
int wdl = Tablebases::probe_wdl(pos, &success);
|
||||||
|
if (!success) return false;
|
||||||
|
score = wdl_to_Value[wdl + 2];
|
||||||
|
|
||||||
|
StateInfo st;
|
||||||
|
|
||||||
|
int best = -2;
|
||||||
|
|
||||||
|
// Probe each move.
|
||||||
|
for (size_t i = 0; i < rootMoves.size(); i++) {
|
||||||
|
Move move = rootMoves[i].pv[0];
|
||||||
|
pos.do_move(move, st, pos.gives_check(move));
|
||||||
|
int v = -Tablebases::probe_wdl(pos, &success);
|
||||||
|
pos.undo_move(move);
|
||||||
|
if (!success) return false;
|
||||||
|
rootMoves[i].score = (Value)v;
|
||||||
|
if (v > best)
|
||||||
|
best = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t j = 0;
|
||||||
|
for (size_t i = 0; i < rootMoves.size(); i++) {
|
||||||
|
if (rootMoves[i].score == best)
|
||||||
|
rootMoves[j++] = rootMoves[i];
|
||||||
|
}
|
||||||
|
rootMoves.resize(j, Search::RootMove(MOVE_NONE));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef TBPROBE_H
|
||||||
|
#define TBPROBE_H
|
||||||
|
|
||||||
|
#include "../search.h"
|
||||||
|
|
||||||
|
namespace Tablebases {
|
||||||
|
|
||||||
|
extern int MaxCardinality;
|
||||||
|
|
||||||
|
void init(const std::string& path);
|
||||||
|
int probe_wdl(Position& pos, int *success);
|
||||||
|
int probe_dtz(Position& pos, int *success);
|
||||||
|
bool root_probe(Position& pos, Search::RootMoves& rootMoves, Value& score);
|
||||||
|
bool root_probe_wdl(Position& pos, Search::RootMoves& rootMoves, Value& score);
|
||||||
|
void filter_root_moves(Position& pos, Search::RootMoves& rootMoves);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
+114
-277
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,364 +24,200 @@
|
|||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "ucioption.h"
|
#include "uci.h"
|
||||||
|
#include "syzygy/tbprobe.h"
|
||||||
using namespace Search;
|
|
||||||
|
|
||||||
ThreadPool Threads; // Global object
|
ThreadPool Threads; // Global object
|
||||||
|
|
||||||
extern void check_time();
|
/// Thread constructor launches the thread and then waits until it goes to sleep
|
||||||
|
/// in idle_loop().
|
||||||
|
|
||||||
namespace {
|
Thread::Thread() {
|
||||||
|
|
||||||
// start_routine() is the C function which is called when a new thread
|
resetCalls = exit = false;
|
||||||
// is launched. It is a wrapper to the virtual function idle_loop().
|
maxPly = callsCnt = 0;
|
||||||
|
tbHits = 0;
|
||||||
extern "C" { long start_routine(ThreadBase* th) { th->idle_loop(); return 0; } }
|
history.clear();
|
||||||
|
counterMoves.clear();
|
||||||
|
idx = Threads.size(); // Start from 0
|
||||||
// Helpers to launch a thread after creation and joining before delete. Must be
|
|
||||||
// outside Thread c'tor and d'tor because the object will be fully initialized
|
|
||||||
// when start_routine (and hence virtual idle_loop) is called and when joining.
|
|
||||||
|
|
||||||
template<typename T> T* new_thread() {
|
|
||||||
T* th = new T();
|
|
||||||
thread_create(th->handle, start_routine, th); // Will go to sleep
|
|
||||||
return th;
|
|
||||||
}
|
|
||||||
|
|
||||||
void delete_thread(ThreadBase* th) {
|
|
||||||
th->exit = true; // Search must be already finished
|
|
||||||
th->notify_one();
|
|
||||||
thread_join(th->handle); // Wait for thread termination
|
|
||||||
delete th;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
std::unique_lock<Mutex> lk(mutex);
|
||||||
|
searching = true;
|
||||||
|
nativeThread = std::thread(&Thread::idle_loop, this);
|
||||||
|
sleepCondition.wait(lk, [&]{ return !searching; });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// notify_one() wakes up the thread when there is some work to do
|
/// Thread destructor waits for thread termination before returning
|
||||||
|
|
||||||
void ThreadBase::notify_one() {
|
Thread::~Thread() {
|
||||||
|
|
||||||
mutex.lock();
|
mutex.lock();
|
||||||
|
exit = true;
|
||||||
sleepCondition.notify_one();
|
sleepCondition.notify_one();
|
||||||
mutex.unlock();
|
mutex.unlock();
|
||||||
|
nativeThread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// wait_for() set the thread to sleep until condition 'b' turns true
|
/// Thread::wait_for_search_finished() waits on sleep condition
|
||||||
|
/// until not searching
|
||||||
|
|
||||||
void ThreadBase::wait_for(volatile const bool& b) {
|
void Thread::wait_for_search_finished() {
|
||||||
|
|
||||||
mutex.lock();
|
std::unique_lock<Mutex> lk(mutex);
|
||||||
while (!b) sleepCondition.wait(mutex);
|
sleepCondition.wait(lk, [&]{ return !searching; });
|
||||||
mutex.unlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Thread c'tor just inits data and does not launch any execution thread.
|
/// Thread::wait() waits on sleep condition until condition is true
|
||||||
// Such a thread will only be started when c'tor returns.
|
|
||||||
|
|
||||||
Thread::Thread() /* : splitPoints() */ { // Value-initialization bug in MSVC
|
void Thread::wait(std::atomic_bool& condition) {
|
||||||
|
|
||||||
searching = false;
|
std::unique_lock<Mutex> lk(mutex);
|
||||||
maxPly = splitPointsSize = 0;
|
sleepCondition.wait(lk, [&]{ return bool(condition); });
|
||||||
activeSplitPoint = NULL;
|
|
||||||
activePosition = NULL;
|
|
||||||
idx = Threads.size(); // Starts from 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// cutoff_occurred() checks whether a beta cutoff has occurred in the
|
/// Thread::start_searching() wakes up the thread that will start the search
|
||||||
// current active split point, or in some ancestor of the split point.
|
|
||||||
|
|
||||||
bool Thread::cutoff_occurred() const {
|
void Thread::start_searching(bool resume) {
|
||||||
|
|
||||||
for (SplitPoint* sp = activeSplitPoint; sp; sp = sp->parentSplitPoint)
|
std::unique_lock<Mutex> lk(mutex);
|
||||||
if (sp->cutoff)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
if (!resume)
|
||||||
|
searching = true;
|
||||||
|
|
||||||
|
sleepCondition.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Thread::available_to() checks whether the thread is available to help the
|
/// Thread::idle_loop() is where the thread is parked when it has no work to do
|
||||||
// thread 'master' at a split point. An obvious requirement is that thread must
|
|
||||||
// be idle. With more than two threads, this is not sufficient: If the thread is
|
|
||||||
// the master of some split point, it is only available as a slave to the slaves
|
|
||||||
// which are busy searching the split point at the top of slave's split point
|
|
||||||
// stack (the "helpful master concept" in YBWC terminology).
|
|
||||||
|
|
||||||
bool Thread::available_to(const Thread* master) const {
|
void Thread::idle_loop() {
|
||||||
|
|
||||||
if (searching)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Make a local copy to be sure it doesn't become zero under our feet while
|
|
||||||
// testing next condition and so leading to an out of bounds access.
|
|
||||||
int size = splitPointsSize;
|
|
||||||
|
|
||||||
// No split points means that the thread is available as a slave for any
|
|
||||||
// other thread otherwise apply the "helpful master" concept if possible.
|
|
||||||
return !size || splitPoints[size - 1].slavesMask.test(master->idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// TimerThread::idle_loop() is where the timer thread waits msec milliseconds
|
|
||||||
// and then calls check_time(). If msec is 0 thread sleeps until it's woken up.
|
|
||||||
|
|
||||||
void TimerThread::idle_loop() {
|
|
||||||
|
|
||||||
while (!exit)
|
while (!exit)
|
||||||
{
|
{
|
||||||
mutex.lock();
|
std::unique_lock<Mutex> lk(mutex);
|
||||||
|
|
||||||
if (!exit)
|
|
||||||
sleepCondition.wait_for(mutex, run ? Resolution : INT_MAX);
|
|
||||||
|
|
||||||
mutex.unlock();
|
|
||||||
|
|
||||||
if (run)
|
|
||||||
check_time();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// MainThread::idle_loop() is where the main thread is parked waiting to be started
|
|
||||||
// when there is a new search. The main thread will launch all the slave threads.
|
|
||||||
|
|
||||||
void MainThread::idle_loop() {
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
mutex.lock();
|
|
||||||
|
|
||||||
thinking = false;
|
|
||||||
|
|
||||||
while (!thinking && !exit)
|
|
||||||
{
|
|
||||||
Threads.sleepCondition.notify_one(); // Wake up the UI thread if needed
|
|
||||||
sleepCondition.wait(mutex);
|
|
||||||
}
|
|
||||||
|
|
||||||
mutex.unlock();
|
|
||||||
|
|
||||||
if (exit)
|
|
||||||
return;
|
|
||||||
|
|
||||||
searching = true;
|
|
||||||
|
|
||||||
Search::think();
|
|
||||||
|
|
||||||
assert(searching);
|
|
||||||
|
|
||||||
searching = false;
|
searching = false;
|
||||||
|
|
||||||
|
while (!searching && !exit)
|
||||||
|
{
|
||||||
|
sleepCondition.notify_one(); // Wake up any waiting thread
|
||||||
|
sleepCondition.wait(lk);
|
||||||
|
}
|
||||||
|
|
||||||
|
lk.unlock();
|
||||||
|
|
||||||
|
if (!exit)
|
||||||
|
search();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// init() is called at startup to create and launch requested threads, that will
|
/// ThreadPool::init() creates and launches requested threads that will go
|
||||||
// go immediately to sleep. We cannot use a c'tor because Threads is a static
|
/// immediately to sleep. We cannot use a constructor because Threads is a
|
||||||
// object and we need a fully initialized engine at this point due to allocation
|
/// static object and we need a fully initialized engine at this point due to
|
||||||
// of Endgames in Thread c'tor.
|
/// allocation of Endgames in the Thread constructor.
|
||||||
|
|
||||||
void ThreadPool::init() {
|
void ThreadPool::init() {
|
||||||
|
|
||||||
timer = new_thread<TimerThread>();
|
push_back(new MainThread);
|
||||||
push_back(new_thread<MainThread>());
|
|
||||||
read_uci_options();
|
read_uci_options();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// exit() cleanly terminates the threads before the program exits. Cannot be done in
|
/// ThreadPool::exit() terminates threads before the program exits. Cannot be
|
||||||
// d'tor because we have to terminate the threads before to free ThreadPool object.
|
/// done in destructor because threads must be terminated before deleting any
|
||||||
|
/// static objects while still in main().
|
||||||
|
|
||||||
void ThreadPool::exit() {
|
void ThreadPool::exit() {
|
||||||
|
|
||||||
delete_thread(timer); // As first because check_time() accesses threads data
|
while (size())
|
||||||
|
delete back(), pop_back();
|
||||||
for (iterator it = begin(); it != end(); ++it)
|
|
||||||
delete_thread(*it);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// read_uci_options() updates internal threads parameters from the corresponding
|
/// ThreadPool::read_uci_options() updates internal threads parameters from the
|
||||||
// UCI options and creates/destroys threads to match the requested number. Thread
|
/// corresponding UCI options and creates/destroys threads to match requested
|
||||||
// objects are dynamically allocated to avoid creating all possible threads
|
/// number. Thread objects are dynamically allocated.
|
||||||
// in advance (which include pawns and material tables), even if only a few
|
|
||||||
// are to be used.
|
|
||||||
|
|
||||||
void ThreadPool::read_uci_options() {
|
void ThreadPool::read_uci_options() {
|
||||||
|
|
||||||
minimumSplitDepth = Options["Min Split Depth"] * ONE_PLY;
|
size_t requested = Options["Threads"];
|
||||||
size_t requested = Options["Threads"];
|
|
||||||
|
|
||||||
assert(requested > 0);
|
assert(requested > 0);
|
||||||
|
|
||||||
// If zero (default) then set best minimum split depth automatically
|
|
||||||
if (!minimumSplitDepth)
|
|
||||||
minimumSplitDepth = requested < 8 ? 4 * ONE_PLY : 7 * ONE_PLY;
|
|
||||||
|
|
||||||
while (size() < requested)
|
while (size() < requested)
|
||||||
push_back(new_thread<Thread>());
|
push_back(new Thread);
|
||||||
|
|
||||||
while (size() > requested)
|
while (size() > requested)
|
||||||
{
|
delete back(), pop_back();
|
||||||
delete_thread(back());
|
|
||||||
pop_back();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// available_slave() tries to find an idle thread which is available as a slave
|
/// ThreadPool::nodes_searched() returns the number of nodes searched
|
||||||
// for the thread 'master'.
|
|
||||||
|
|
||||||
Thread* ThreadPool::available_slave(const Thread* master) const {
|
uint64_t ThreadPool::nodes_searched() const {
|
||||||
|
|
||||||
for (const_iterator it = begin(); it != end(); ++it)
|
uint64_t nodes = 0;
|
||||||
if ((*it)->available_to(master))
|
for (Thread* th : *this)
|
||||||
return *it;
|
nodes += th->rootPos.nodes_searched();
|
||||||
|
return nodes;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// split() does the actual work of distributing the work at a node between
|
/// ThreadPool::tb_hits() returns the number of TB hits
|
||||||
// several available threads. If it does not succeed in splitting the node
|
|
||||||
// (because no idle threads are available), the function immediately returns.
|
|
||||||
// If splitting is possible, a SplitPoint object is initialized with all the
|
|
||||||
// data that must be copied to the helper threads and then helper threads are
|
|
||||||
// told that they have been assigned work. This will cause them to instantly
|
|
||||||
// leave their idle loops and call search(). When all threads have returned from
|
|
||||||
// search() then split() returns.
|
|
||||||
|
|
||||||
template <bool Fake>
|
uint64_t ThreadPool::tb_hits() const {
|
||||||
void Thread::split(Position& pos, const Stack* ss, Value alpha, Value beta, Value* bestValue,
|
|
||||||
Move* bestMove, Depth depth, int moveCount,
|
|
||||||
MovePicker* movePicker, int nodeType, bool cutNode) {
|
|
||||||
|
|
||||||
assert(pos.pos_is_ok());
|
uint64_t hits = 0;
|
||||||
assert(-VALUE_INFINITE < *bestValue && *bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE);
|
for (Thread* th : *this)
|
||||||
assert(depth >= Threads.minimumSplitDepth);
|
hits += th->tbHits;
|
||||||
assert(searching);
|
return hits;
|
||||||
assert(splitPointsSize < MAX_SPLITPOINTS_PER_THREAD);
|
|
||||||
|
|
||||||
// Pick the next available split point from the split point stack
|
|
||||||
SplitPoint& sp = splitPoints[splitPointsSize];
|
|
||||||
|
|
||||||
sp.masterThread = this;
|
|
||||||
sp.parentSplitPoint = activeSplitPoint;
|
|
||||||
sp.slavesMask = 0, sp.slavesMask.set(idx);
|
|
||||||
sp.depth = depth;
|
|
||||||
sp.bestValue = *bestValue;
|
|
||||||
sp.bestMove = *bestMove;
|
|
||||||
sp.alpha = alpha;
|
|
||||||
sp.beta = beta;
|
|
||||||
sp.nodeType = nodeType;
|
|
||||||
sp.cutNode = cutNode;
|
|
||||||
sp.movePicker = movePicker;
|
|
||||||
sp.moveCount = moveCount;
|
|
||||||
sp.pos = &pos;
|
|
||||||
sp.nodes = 0;
|
|
||||||
sp.cutoff = false;
|
|
||||||
sp.ss = ss;
|
|
||||||
|
|
||||||
// Try to allocate available threads and ask them to start searching setting
|
|
||||||
// 'searching' flag. This must be done under lock protection to avoid concurrent
|
|
||||||
// allocation of the same slave by another master.
|
|
||||||
Threads.mutex.lock();
|
|
||||||
sp.mutex.lock();
|
|
||||||
|
|
||||||
sp.allSlavesSearching = true; // Must be set under lock protection
|
|
||||||
++splitPointsSize;
|
|
||||||
activeSplitPoint = &sp;
|
|
||||||
activePosition = NULL;
|
|
||||||
|
|
||||||
if (!Fake)
|
|
||||||
for (Thread* slave; (slave = Threads.available_slave(this)) != NULL; )
|
|
||||||
{
|
|
||||||
sp.slavesMask.set(slave->idx);
|
|
||||||
slave->activeSplitPoint = &sp;
|
|
||||||
slave->searching = true; // Slave leaves idle_loop()
|
|
||||||
slave->notify_one(); // Could be sleeping
|
|
||||||
}
|
|
||||||
|
|
||||||
// Everything is set up. The master thread enters the idle loop, from which
|
|
||||||
// it will instantly launch a search, because its 'searching' flag is set.
|
|
||||||
// The thread will return from the idle loop when all slaves have finished
|
|
||||||
// their work at this split point.
|
|
||||||
sp.mutex.unlock();
|
|
||||||
Threads.mutex.unlock();
|
|
||||||
|
|
||||||
Thread::idle_loop(); // Force a call to base class idle_loop()
|
|
||||||
|
|
||||||
// In the helpful master concept, a master can help only a sub-tree of its
|
|
||||||
// split point and because everything is finished here, it's not possible
|
|
||||||
// for the master to be booked.
|
|
||||||
assert(!searching);
|
|
||||||
assert(!activePosition);
|
|
||||||
|
|
||||||
// We have returned from the idle loop, which means that all threads are
|
|
||||||
// finished. Note that setting 'searching' and decreasing splitPointsSize is
|
|
||||||
// done under lock protection to avoid a race with Thread::available_to().
|
|
||||||
Threads.mutex.lock();
|
|
||||||
sp.mutex.lock();
|
|
||||||
|
|
||||||
searching = true;
|
|
||||||
--splitPointsSize;
|
|
||||||
activeSplitPoint = sp.parentSplitPoint;
|
|
||||||
activePosition = &pos;
|
|
||||||
pos.set_nodes_searched(pos.nodes_searched() + sp.nodes);
|
|
||||||
*bestMove = sp.bestMove;
|
|
||||||
*bestValue = sp.bestValue;
|
|
||||||
|
|
||||||
sp.mutex.unlock();
|
|
||||||
Threads.mutex.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Explicit template instantiations
|
|
||||||
template void Thread::split<false>(Position&, const Stack*, Value, Value, Value*, Move*, Depth, int, MovePicker*, int, bool);
|
|
||||||
template void Thread::split< true>(Position&, const Stack*, Value, Value, Value*, Move*, Depth, int, MovePicker*, int, bool);
|
|
||||||
|
|
||||||
|
|
||||||
// wait_for_think_finished() waits for main thread to go to sleep then returns
|
|
||||||
|
|
||||||
void ThreadPool::wait_for_think_finished() {
|
|
||||||
|
|
||||||
MainThread* t = main();
|
|
||||||
t->mutex.lock();
|
|
||||||
while (t->thinking) sleepCondition.wait(t->mutex);
|
|
||||||
t->mutex.unlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// start_thinking() wakes up the main thread sleeping in MainThread::idle_loop()
|
/// ThreadPool::start_thinking() wakes up the main thread sleeping in idle_loop()
|
||||||
// so to start a new search, then returns immediately.
|
/// and starts a new search, then returns immediately.
|
||||||
|
|
||||||
void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits, StateStackPtr& states) {
|
void ThreadPool::start_thinking(Position& pos, StateListPtr& states,
|
||||||
|
const Search::LimitsType& limits) {
|
||||||
|
|
||||||
wait_for_think_finished();
|
main()->wait_for_search_finished();
|
||||||
|
|
||||||
SearchTime = Time::now(); // As early as possible
|
Search::Signals.stopOnPonderhit = Search::Signals.stop = false;
|
||||||
|
Search::Limits = limits;
|
||||||
|
Search::RootMoves rootMoves;
|
||||||
|
|
||||||
Signals.stopOnPonderhit = Signals.firstRootMove = false;
|
for (const auto& m : MoveList<LEGAL>(pos))
|
||||||
Signals.stop = Signals.failedLowAtRoot = false;
|
|
||||||
|
|
||||||
RootMoves.clear();
|
|
||||||
RootPos = pos;
|
|
||||||
Limits = limits;
|
|
||||||
if (states.get()) // If we don't set a new position, preserve current state
|
|
||||||
{
|
|
||||||
SetupStates = states; // Ownership transfer here
|
|
||||||
assert(!states.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
for (MoveList<LEGAL> it(pos); *it; ++it)
|
|
||||||
if ( limits.searchmoves.empty()
|
if ( limits.searchmoves.empty()
|
||||||
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), *it))
|
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), m))
|
||||||
RootMoves.push_back(RootMove(*it));
|
rootMoves.push_back(Search::RootMove(m));
|
||||||
|
|
||||||
main()->thinking = true;
|
if (!rootMoves.empty())
|
||||||
main()->notify_one(); // Starts main thread
|
Tablebases::filter_root_moves(pos, rootMoves);
|
||||||
|
|
||||||
|
// After ownership transfer 'states' becomes empty, so if we stop the search
|
||||||
|
// and call 'go' again without setting a new position states.get() == NULL.
|
||||||
|
assert(states.get() || setupStates.get());
|
||||||
|
|
||||||
|
if (states.get())
|
||||||
|
setupStates = std::move(states); // Ownership transfer, states is now empty
|
||||||
|
|
||||||
|
StateInfo tmp = setupStates->back();
|
||||||
|
|
||||||
|
for (Thread* th : Threads)
|
||||||
|
{
|
||||||
|
th->maxPly = 0;
|
||||||
|
th->tbHits = 0;
|
||||||
|
th->rootDepth = DEPTH_ZERO;
|
||||||
|
th->rootMoves = rootMoves;
|
||||||
|
th->rootPos.set(pos.fen(), pos.is_chess960(), &setupStates->back(), th);
|
||||||
|
}
|
||||||
|
|
||||||
|
setupStates->back() = tmp; // Restore st->previous, cleared by Position::set()
|
||||||
|
|
||||||
|
main()->start_searching();
|
||||||
}
|
}
|
||||||
|
|||||||
+51
-117
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,7 +21,11 @@
|
|||||||
#ifndef THREAD_H_INCLUDED
|
#ifndef THREAD_H_INCLUDED
|
||||||
#define THREAD_H_INCLUDED
|
#define THREAD_H_INCLUDED
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <mutex>
|
||||||
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "material.h"
|
#include "material.h"
|
||||||
@@ -28,148 +33,77 @@
|
|||||||
#include "pawns.h"
|
#include "pawns.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
|
#include "thread_win32.h"
|
||||||
const int MAX_THREADS = 128;
|
|
||||||
const int MAX_SPLITPOINTS_PER_THREAD = 8;
|
|
||||||
|
|
||||||
struct Mutex {
|
|
||||||
Mutex() { lock_init(l); }
|
|
||||||
~Mutex() { lock_destroy(l); }
|
|
||||||
|
|
||||||
void lock() { lock_grab(l); }
|
|
||||||
void unlock() { lock_release(l); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend struct ConditionVariable;
|
|
||||||
|
|
||||||
Lock l;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ConditionVariable {
|
|
||||||
ConditionVariable() { cond_init(c); }
|
|
||||||
~ConditionVariable() { cond_destroy(c); }
|
|
||||||
|
|
||||||
void wait(Mutex& m) { cond_wait(c, m.l); }
|
|
||||||
void wait_for(Mutex& m, int ms) { timed_wait(c, m.l, ms); }
|
|
||||||
void notify_one() { cond_signal(c); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
WaitCondition c;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Thread;
|
|
||||||
|
|
||||||
struct SplitPoint {
|
|
||||||
|
|
||||||
// Const data after split point has been setup
|
|
||||||
const Position* pos;
|
|
||||||
const Search::Stack* ss;
|
|
||||||
Thread* masterThread;
|
|
||||||
Depth depth;
|
|
||||||
Value beta;
|
|
||||||
int nodeType;
|
|
||||||
bool cutNode;
|
|
||||||
|
|
||||||
// Const pointers to shared data
|
|
||||||
MovePicker* movePicker;
|
|
||||||
SplitPoint* parentSplitPoint;
|
|
||||||
|
|
||||||
// Shared data
|
|
||||||
Mutex mutex;
|
|
||||||
std::bitset<MAX_THREADS> slavesMask;
|
|
||||||
volatile bool allSlavesSearching;
|
|
||||||
volatile uint64_t nodes;
|
|
||||||
volatile Value alpha;
|
|
||||||
volatile Value bestValue;
|
|
||||||
volatile Move bestMove;
|
|
||||||
volatile int moveCount;
|
|
||||||
volatile bool cutoff;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/// ThreadBase struct is the base of the hierarchy from where we derive all the
|
/// Thread struct keeps together all the thread-related stuff. We also use
|
||||||
/// specialized thread classes.
|
/// per-thread pawn and material hash tables so that once we get a pointer to an
|
||||||
|
/// entry its life time is unlimited and we don't have to care about someone
|
||||||
|
/// changing the entry under our feet.
|
||||||
|
|
||||||
struct ThreadBase {
|
class Thread {
|
||||||
|
|
||||||
ThreadBase() : handle(NativeHandle()), exit(false) {}
|
|
||||||
virtual ~ThreadBase() {}
|
|
||||||
virtual void idle_loop() = 0;
|
|
||||||
void notify_one();
|
|
||||||
void wait_for(volatile const bool& b);
|
|
||||||
|
|
||||||
|
std::thread nativeThread;
|
||||||
Mutex mutex;
|
Mutex mutex;
|
||||||
ConditionVariable sleepCondition;
|
ConditionVariable sleepCondition;
|
||||||
NativeHandle handle;
|
bool exit, searching;
|
||||||
volatile bool exit;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/// Thread struct keeps together all the thread related stuff like locks, state
|
|
||||||
/// and especially split points. We also use per-thread pawn and material hash
|
|
||||||
/// tables so that once we get a pointer to an entry its life time is unlimited
|
|
||||||
/// and we don't have to care about someone changing the entry under our feet.
|
|
||||||
|
|
||||||
struct Thread : public ThreadBase {
|
|
||||||
|
|
||||||
|
public:
|
||||||
Thread();
|
Thread();
|
||||||
virtual void idle_loop();
|
virtual ~Thread();
|
||||||
bool cutoff_occurred() const;
|
virtual void search();
|
||||||
bool available_to(const Thread* master) const;
|
void idle_loop();
|
||||||
|
void start_searching(bool resume = false);
|
||||||
|
void wait_for_search_finished();
|
||||||
|
void wait(std::atomic_bool& b);
|
||||||
|
|
||||||
template <bool Fake>
|
Pawns::Table pawnsTable;
|
||||||
void split(Position& pos, const Search::Stack* ss, Value alpha, Value beta, Value* bestValue, Move* bestMove,
|
|
||||||
Depth depth, int moveCount, MovePicker* movePicker, int nodeType, bool cutNode);
|
|
||||||
|
|
||||||
SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD];
|
|
||||||
Material::Table materialTable;
|
Material::Table materialTable;
|
||||||
Endgames endgames;
|
Endgames endgames;
|
||||||
Pawns::Table pawnsTable;
|
size_t idx, PVIdx;
|
||||||
Position* activePosition;
|
int maxPly, callsCnt;
|
||||||
size_t idx;
|
uint64_t tbHits;
|
||||||
int maxPly;
|
|
||||||
SplitPoint* volatile activeSplitPoint;
|
Position rootPos;
|
||||||
volatile int splitPointsSize;
|
Search::RootMoves rootMoves;
|
||||||
volatile bool searching;
|
Depth rootDepth;
|
||||||
|
Depth completedDepth;
|
||||||
|
std::atomic_bool resetCalls;
|
||||||
|
HistoryStats history;
|
||||||
|
MoveStats counterMoves;
|
||||||
|
FromToStats fromTo;
|
||||||
|
CounterMoveHistoryStats counterMoveHistory;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// MainThread and TimerThread are derived classes used to characterize the two
|
/// MainThread is a derived class with a specific overload for the main thread
|
||||||
/// special threads: the main one and the recurring timer.
|
|
||||||
|
|
||||||
struct MainThread : public Thread {
|
struct MainThread : public Thread {
|
||||||
MainThread() : thinking(true) {} // Avoid a race with start_thinking()
|
virtual void search();
|
||||||
virtual void idle_loop();
|
|
||||||
volatile bool thinking;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct TimerThread : public ThreadBase {
|
bool easyMovePlayed, failedLow;
|
||||||
TimerThread() : run(false) {}
|
double bestMoveChanges;
|
||||||
virtual void idle_loop();
|
Value previousScore;
|
||||||
bool run;
|
|
||||||
static const int Resolution = 5; // msec between two check_time() calls
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// 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 slave thread at a split point.
|
/// parking and, most importantly, launching a thread. All the access to threads
|
||||||
/// All the access to shared thread data is done through this class.
|
/// data is done through this class.
|
||||||
|
|
||||||
struct ThreadPool : public std::vector<Thread*> {
|
struct ThreadPool : public std::vector<Thread*> {
|
||||||
|
|
||||||
void init(); // No c'tor and d'tor, threads rely on globals that should
|
void init(); // No constructor and destructor, threads rely on globals that should
|
||||||
void exit(); // be initialized and are valid during the whole thread lifetime.
|
void exit(); // be initialized and valid during the whole thread lifetime.
|
||||||
|
|
||||||
MainThread* main() { return static_cast<MainThread*>((*this)[0]); }
|
MainThread* main() { return static_cast<MainThread*>(at(0)); }
|
||||||
|
void start_thinking(Position&, StateListPtr&, const Search::LimitsType&);
|
||||||
void read_uci_options();
|
void read_uci_options();
|
||||||
Thread* available_slave(const Thread* master) const;
|
uint64_t nodes_searched() const;
|
||||||
void wait_for_think_finished();
|
uint64_t tb_hits() const;
|
||||||
void start_thinking(const Position&, const Search::LimitsType&, Search::StateStackPtr&);
|
|
||||||
|
|
||||||
Depth minimumSplitDepth;
|
private:
|
||||||
Mutex mutex;
|
StateListPtr setupStates;
|
||||||
ConditionVariable sleepCondition;
|
|
||||||
TimerThread* timer;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
extern ThreadPool Threads;
|
extern ThreadPool Threads;
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||||
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||||
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||||
|
Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
||||||
|
|
||||||
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Stockfish is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef THREAD_WIN32_H_INCLUDED
|
||||||
|
#define THREAD_WIN32_H_INCLUDED
|
||||||
|
|
||||||
|
/// STL thread library used by mingw and gcc when cross compiling for Windows
|
||||||
|
/// relies on libwinpthread. Currently libwinpthread implements mutexes directly
|
||||||
|
/// on top of Windows semaphores. Semaphores, being kernel objects, require kernel
|
||||||
|
/// mode transition in order to lock or unlock, which is very slow compared to
|
||||||
|
/// interlocked operations (about 30% slower on bench test). To work around this
|
||||||
|
/// issue, we define our wrappers to the low level Win32 calls. We use critical
|
||||||
|
/// sections to support Windows XP and older versions. Unfortunately, cond_wait()
|
||||||
|
/// is racy between unlock() and WaitForSingleObject() but they have the same
|
||||||
|
/// speed performance as the SRW locks.
|
||||||
|
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
#if defined(_WIN32) && !defined(_MSC_VER)
|
||||||
|
|
||||||
|
#ifndef NOMINMAX
|
||||||
|
# define NOMINMAX // Disable macros min() and max()
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#include <windows.h>
|
||||||
|
#undef WIN32_LEAN_AND_MEAN
|
||||||
|
#undef NOMINMAX
|
||||||
|
|
||||||
|
/// Mutex and ConditionVariable struct are wrappers of the low level locking
|
||||||
|
/// machinery and are modeled after the corresponding C++11 classes.
|
||||||
|
|
||||||
|
struct Mutex {
|
||||||
|
Mutex() { InitializeCriticalSection(&cs); }
|
||||||
|
~Mutex() { DeleteCriticalSection(&cs); }
|
||||||
|
void lock() { EnterCriticalSection(&cs); }
|
||||||
|
void unlock() { LeaveCriticalSection(&cs); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
CRITICAL_SECTION cs;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef std::condition_variable_any ConditionVariable;
|
||||||
|
|
||||||
|
#else // Default case: use STL classes
|
||||||
|
|
||||||
|
typedef std::mutex Mutex;
|
||||||
|
typedef std::condition_variable ConditionVariable;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // #ifndef THREAD_WIN32_H_INCLUDED
|
||||||
+61
-55
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,104 +24,109 @@
|
|||||||
|
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "timeman.h"
|
#include "timeman.h"
|
||||||
#include "ucioption.h"
|
#include "uci.h"
|
||||||
|
|
||||||
|
TimeManagement Time; // Our global time management object
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
enum TimeType { OptimumTime, MaxTime };
|
enum TimeType { OptimumTime, MaxTime };
|
||||||
|
|
||||||
const int MoveHorizon = 50; // Plan time management at most this many moves ahead
|
const int MoveHorizon = 50; // Plan time management at most this many moves ahead
|
||||||
const double MaxRatio = 7.0; // When in trouble, we can step over reserved time with this ratio
|
const double MaxRatio = 7.09; // When in trouble, we can step over reserved time with this ratio
|
||||||
const double StealRatio = 0.33; // However we must not steal time from remaining moves over this ratio
|
const double StealRatio = 0.35; // However we must not steal time from remaining moves over this ratio
|
||||||
|
|
||||||
const double xscale = 9.3;
|
|
||||||
const double xshift = 59.8;
|
|
||||||
const double skewfactor = 0.172;
|
|
||||||
|
|
||||||
|
|
||||||
// move_importance() is a skew-logistic function based on naive statistical
|
// move_importance() is a skew-logistic function based on naive statistical
|
||||||
// analysis of "how many games are still undecided after n half-moves". Game
|
// analysis of "how many games are still undecided after n half-moves". Game
|
||||||
// is considered "undecided" as long as neither side has >275cp advantage.
|
// is considered "undecided" as long as neither side has >275cp advantage.
|
||||||
// Data was extracted from CCRL game database with some simple filtering criteria.
|
// Data was extracted from the CCRL game database with some simple filtering criteria.
|
||||||
|
|
||||||
double move_importance(int ply) {
|
double move_importance(int ply) {
|
||||||
|
|
||||||
return pow((1 + exp((ply - xshift) / xscale)), -skewfactor) + DBL_MIN; // Ensure non-zero
|
const double XScale = 7.64;
|
||||||
|
const double XShift = 58.4;
|
||||||
|
const double Skew = 0.183;
|
||||||
|
|
||||||
|
return pow((1 + exp((ply - XShift) / XScale)), -Skew) + DBL_MIN; // Ensure non-zero
|
||||||
}
|
}
|
||||||
|
|
||||||
template<TimeType T>
|
template<TimeType T>
|
||||||
int remaining(int myTime, int movesToGo, int currentPly, int slowMover)
|
int remaining(int myTime, int movesToGo, int ply, int slowMover) {
|
||||||
{
|
|
||||||
const double TMaxRatio = (T == OptimumTime ? 1 : MaxRatio);
|
const double TMaxRatio = (T == OptimumTime ? 1 : MaxRatio);
|
||||||
const double TStealRatio = (T == OptimumTime ? 0 : StealRatio);
|
const double TStealRatio = (T == OptimumTime ? 0 : StealRatio);
|
||||||
|
|
||||||
double thisMoveImportance = (move_importance(currentPly) * slowMover) / 100;
|
double moveImportance = (move_importance(ply) * slowMover) / 100;
|
||||||
double otherMovesImportance = 0;
|
double otherMovesImportance = 0;
|
||||||
|
|
||||||
for (int i = 1; i < movesToGo; ++i)
|
for (int i = 1; i < movesToGo; ++i)
|
||||||
otherMovesImportance += move_importance(currentPly + 2 * i);
|
otherMovesImportance += move_importance(ply + 2 * i);
|
||||||
|
|
||||||
double ratio1 = (TMaxRatio * thisMoveImportance) / (TMaxRatio * thisMoveImportance + otherMovesImportance);
|
double ratio1 = (TMaxRatio * moveImportance) / (TMaxRatio * moveImportance + otherMovesImportance);
|
||||||
double ratio2 = (thisMoveImportance + TStealRatio * otherMovesImportance) / (thisMoveImportance + otherMovesImportance);
|
double ratio2 = (moveImportance + TStealRatio * otherMovesImportance) / (moveImportance + otherMovesImportance);
|
||||||
|
|
||||||
return int(myTime * std::min(ratio1, ratio2));
|
return int(myTime * std::min(ratio1, ratio2)); // Intel C++ asks for an explicit cast
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
void TimeManager::init(const Search::LimitsType& limits, int currentPly, Color us)
|
/// init() is called at the beginning of the search and calculates the allowed
|
||||||
{
|
/// thinking time out of the time control and current game ply. We support four
|
||||||
/* We support four different kinds of time controls:
|
/// different kinds of time controls, passed in 'limits':
|
||||||
|
///
|
||||||
|
/// inc == 0 && movestogo == 0 means: x basetime [sudden death!]
|
||||||
|
/// inc == 0 && movestogo != 0 means: x moves in y minutes
|
||||||
|
/// inc > 0 && movestogo == 0 means: x basetime + z increment
|
||||||
|
/// inc > 0 && movestogo != 0 means: x moves in y minutes + z increment
|
||||||
|
|
||||||
increment == 0 && movesToGo == 0 means: x basetime [sudden death!]
|
void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
|
||||||
increment == 0 && movesToGo != 0 means: x moves in y minutes
|
|
||||||
increment > 0 && movesToGo == 0 means: x basetime + z increment
|
|
||||||
increment > 0 && movesToGo != 0 means: x moves in y minutes + z increment
|
|
||||||
|
|
||||||
Time management is adjusted by following UCI parameters:
|
int minThinkingTime = Options["Minimum Thinking Time"];
|
||||||
|
int moveOverhead = Options["Move Overhead"];
|
||||||
|
int slowMover = Options["Slow Mover"];
|
||||||
|
int npmsec = Options["nodestime"];
|
||||||
|
|
||||||
emergencyMoveHorizon: Be prepared to always play at least this many moves
|
// If we have to play in 'nodes as time' mode, then convert from time
|
||||||
emergencyBaseTime : Always attempt to keep at least this much time (in ms) at clock
|
// to nodes, and use resulting values in time management formulas.
|
||||||
emergencyMoveTime : Plus attempt to keep at least this much time for each remaining emergency move
|
// WARNING: Given npms (nodes per millisecond) must be much lower then
|
||||||
minThinkingTime : No matter what, use at least this much thinking before doing the move
|
// the real engine speed to avoid time losses.
|
||||||
*/
|
if (npmsec)
|
||||||
|
{
|
||||||
|
if (!availableNodes) // Only once at game start
|
||||||
|
availableNodes = npmsec * limits.time[us]; // Time is in msec
|
||||||
|
|
||||||
int hypMTG, hypMyTime, t1, t2;
|
// Convert from millisecs to nodes
|
||||||
|
limits.time[us] = (int)availableNodes;
|
||||||
|
limits.inc[us] *= npmsec;
|
||||||
|
limits.npmsec = npmsec;
|
||||||
|
}
|
||||||
|
|
||||||
// Read uci parameters
|
startTime = limits.startTime;
|
||||||
int emergencyMoveHorizon = Options["Emergency Move Horizon"];
|
optimumTime = maximumTime = std::max(limits.time[us], minThinkingTime);
|
||||||
int emergencyBaseTime = Options["Emergency Base Time"];
|
|
||||||
int emergencyMoveTime = Options["Emergency Move Time"];
|
|
||||||
int minThinkingTime = Options["Minimum Thinking Time"];
|
|
||||||
int slowMover = Options["Slow Mover"];
|
|
||||||
|
|
||||||
// Initialize unstablePvFactor to 1 and search times to maximum values
|
const int MaxMTG = limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon;
|
||||||
unstablePvFactor = 1;
|
|
||||||
optimumSearchTime = maximumSearchTime = std::max(limits.time[us], minThinkingTime);
|
|
||||||
|
|
||||||
// We calculate optimum time usage for different hypothetical "moves to go"-values and choose the
|
// We calculate optimum time usage for different hypothetical "moves to go"-values
|
||||||
// minimum of calculated search time values. Usually the greatest hypMTG gives the minimum values.
|
// and choose the minimum of calculated search time values. Usually the greatest
|
||||||
for (hypMTG = 1; hypMTG <= (limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon); ++hypMTG)
|
// hypMTG gives the minimum values.
|
||||||
|
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
|
||||||
hypMyTime = limits.time[us]
|
int hypMyTime = limits.time[us]
|
||||||
+ limits.inc[us] * (hypMTG - 1)
|
+ limits.inc[us] * (hypMTG - 1)
|
||||||
- emergencyBaseTime
|
- moveOverhead * (2 + std::min(hypMTG, 40));
|
||||||
- emergencyMoveTime * std::min(hypMTG, emergencyMoveHorizon);
|
|
||||||
|
|
||||||
hypMyTime = std::max(hypMyTime, 0);
|
hypMyTime = std::max(hypMyTime, 0);
|
||||||
|
|
||||||
t1 = minThinkingTime + remaining<OptimumTime>(hypMyTime, hypMTG, currentPly, slowMover);
|
int t1 = minThinkingTime + remaining<OptimumTime>(hypMyTime, hypMTG, ply, slowMover);
|
||||||
t2 = minThinkingTime + remaining<MaxTime>(hypMyTime, hypMTG, currentPly, slowMover);
|
int t2 = minThinkingTime + remaining<MaxTime >(hypMyTime, hypMTG, ply, slowMover);
|
||||||
|
|
||||||
optimumSearchTime = std::min(optimumSearchTime, t1);
|
optimumTime = std::min(t1, optimumTime);
|
||||||
maximumSearchTime = std::min(maximumSearchTime, t2);
|
maximumTime = std::min(t2, maximumTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Options["Ponder"])
|
if (Options["Ponder"])
|
||||||
optimumSearchTime += optimumSearchTime / 4;
|
optimumTime += optimumTime / 4;
|
||||||
|
|
||||||
// Make sure that maxSearchTime is not over absoluteMaxSearchTime
|
|
||||||
optimumSearchTime = std::min(optimumSearchTime, maximumSearchTime);
|
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-11
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,20 +21,28 @@
|
|||||||
#ifndef TIMEMAN_H_INCLUDED
|
#ifndef TIMEMAN_H_INCLUDED
|
||||||
#define TIMEMAN_H_INCLUDED
|
#define TIMEMAN_H_INCLUDED
|
||||||
|
|
||||||
/// The TimeManager class computes the optimal time to think depending on the
|
#include "misc.h"
|
||||||
/// maximum available time, the game move number and other parameters.
|
#include "search.h"
|
||||||
|
#include "thread.h"
|
||||||
|
|
||||||
class TimeManager {
|
/// The TimeManagement class computes the optimal time to think depending on
|
||||||
|
/// the maximum available time, the game move number and other parameters.
|
||||||
|
|
||||||
|
class TimeManagement {
|
||||||
public:
|
public:
|
||||||
void init(const Search::LimitsType& limits, int currentPly, Color us);
|
void init(Search::LimitsType& limits, Color us, int ply);
|
||||||
void pv_instability(double bestMoveChanges) { unstablePvFactor = 1 + bestMoveChanges; }
|
int optimum() const { return optimumTime; }
|
||||||
int available_time() const { return int(optimumSearchTime * unstablePvFactor * 0.71); }
|
int maximum() const { return maximumTime; }
|
||||||
int maximum_time() const { return maximumSearchTime; }
|
int elapsed() const { return int(Search::Limits.npmsec ? Threads.nodes_searched() : now() - startTime); }
|
||||||
|
|
||||||
|
int64_t availableNodes; // When in 'nodes as time' mode
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int optimumSearchTime;
|
TimePoint startTime;
|
||||||
int maximumSearchTime;
|
int optimumTime;
|
||||||
double unstablePvFactor;
|
int maximumTime;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern TimeManagement Time;
|
||||||
|
|
||||||
#endif // #ifndef TIMEMAN_H_INCLUDED
|
#endif // #ifndef TIMEMAN_H_INCLUDED
|
||||||
|
|||||||
+49
-53
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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
|
||||||
@@ -17,7 +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 <cstring>
|
#include <cstring> // For std::memset
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
@@ -30,18 +31,17 @@ TranspositionTable TT; // Our global 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
|
||||||
/// of clusters and each cluster consists of ClusterSize number of TTEntry.
|
/// of clusters and each cluster consists of ClusterSize number of TTEntry.
|
||||||
|
|
||||||
void TranspositionTable::resize(uint64_t mbSize) {
|
void TranspositionTable::resize(size_t mbSize) {
|
||||||
|
|
||||||
assert(msb((mbSize << 20) / sizeof(TTEntry)) < 32);
|
size_t newClusterCount = size_t(1) << msb((mbSize * 1024 * 1024) / sizeof(Cluster));
|
||||||
|
|
||||||
uint32_t size = ClusterSize << msb((mbSize << 20) / sizeof(TTEntry[ClusterSize]));
|
if (newClusterCount == clusterCount)
|
||||||
|
|
||||||
if (hashMask == size - ClusterSize)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
hashMask = size - ClusterSize;
|
clusterCount = newClusterCount;
|
||||||
|
|
||||||
free(mem);
|
free(mem);
|
||||||
mem = calloc(size * sizeof(TTEntry) + CACHE_LINE_SIZE - 1, 1);
|
mem = calloc(clusterCount * sizeof(Cluster) + CacheLineSize - 1, 1);
|
||||||
|
|
||||||
if (!mem)
|
if (!mem)
|
||||||
{
|
{
|
||||||
@@ -50,72 +50,68 @@ void TranspositionTable::resize(uint64_t mbSize) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
table = (TTEntry*)((uintptr_t(mem) + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1));
|
table = (Cluster*)((uintptr_t(mem) + CacheLineSize - 1) & ~(CacheLineSize - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// TranspositionTable::clear() overwrites the entire transposition table
|
/// TranspositionTable::clear() overwrites the entire transposition table
|
||||||
/// with zeroes. It is called whenever the table is resized, or when the
|
/// with zeros. It is called whenever the table is resized, or when the
|
||||||
/// user asks the program to clear the table (from the UCI interface).
|
/// user asks the program to clear the table (from the UCI interface).
|
||||||
|
|
||||||
void TranspositionTable::clear() {
|
void TranspositionTable::clear() {
|
||||||
|
|
||||||
std::memset(table, 0, (hashMask + ClusterSize) * sizeof(TTEntry));
|
std::memset(table, 0, clusterCount * sizeof(Cluster));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// TranspositionTable::probe() looks up the current position in the
|
/// TranspositionTable::probe() looks up the current position in the transposition
|
||||||
/// transposition table. Returns a pointer to the TTEntry or NULL if
|
/// table. It returns true and a pointer to the TTEntry if the position is found.
|
||||||
/// position is not found.
|
/// Otherwise, it returns false and a pointer to an empty or least valuable TTEntry
|
||||||
|
/// to be replaced later. The replace value of an entry is calculated as its depth
|
||||||
|
/// minus 8 times its relative age. TTEntry t1 is considered more valuable than
|
||||||
|
/// TTEntry t2 if its replace value is greater than that of t2.
|
||||||
|
|
||||||
const TTEntry* TranspositionTable::probe(const Key key) const {
|
TTEntry* TranspositionTable::probe(const Key key, bool& found) const {
|
||||||
|
|
||||||
TTEntry* tte = first_entry(key);
|
TTEntry* const tte = first_entry(key);
|
||||||
uint32_t key32 = key >> 32;
|
const uint16_t key16 = key >> 48; // Use the high 16 bits as key inside the cluster
|
||||||
|
|
||||||
for (unsigned i = 0; i < ClusterSize; ++i, ++tte)
|
for (int i = 0; i < ClusterSize; ++i)
|
||||||
if (tte->key32 == key32)
|
if (!tte[i].key16 || tte[i].key16 == key16)
|
||||||
{
|
{
|
||||||
tte->generation8 = generation; // Refresh
|
if ((tte[i].genBound8 & 0xFC) != generation8 && tte[i].key16)
|
||||||
return tte;
|
tte[i].genBound8 = uint8_t(generation8 | tte[i].bound()); // Refresh
|
||||||
|
|
||||||
|
return found = (bool)tte[i].key16, &tte[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
// Find an entry to be replaced according to the replacement strategy
|
||||||
|
TTEntry* replace = tte;
|
||||||
|
for (int i = 1; i < ClusterSize; ++i)
|
||||||
|
// Due to our packed storage format for generation and its cyclic
|
||||||
|
// nature we add 259 (256 is the modulus plus 3 to keep the lowest
|
||||||
|
// two bound bits from affecting the result) to calculate the entry
|
||||||
|
// age correctly even after generation8 overflows into the next cycle.
|
||||||
|
if ( replace->depth8 - ((259 + generation8 - replace->genBound8) & 0xFC) * 2
|
||||||
|
> tte[i].depth8 - ((259 + generation8 - tte[i].genBound8) & 0xFC) * 2)
|
||||||
|
replace = &tte[i];
|
||||||
|
|
||||||
|
return found = false, replace;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// TranspositionTable::store() writes a new entry containing position key and
|
/// TranspositionTable::hashfull() returns an approximation of the hashtable
|
||||||
/// valuable information of current position. The lowest order bits of position
|
/// occupation during a search. The hash is x permill full, as per UCI protocol.
|
||||||
/// key are used to decide in which cluster the position will be placed.
|
|
||||||
/// When a new entry is written and there are no empty entries available in the
|
|
||||||
/// cluster, it replaces the least valuable of the entries. A TTEntry t1 is considered
|
|
||||||
/// to be more valuable than a TTEntry t2 if t1 is from the current search and t2
|
|
||||||
/// is from a previous search, or if the depth of t1 is bigger than the depth of t2.
|
|
||||||
|
|
||||||
void TranspositionTable::store(const Key key, Value v, Bound b, Depth d, Move m, Value statV) {
|
int TranspositionTable::hashfull() const {
|
||||||
|
|
||||||
TTEntry *tte, *replace;
|
int cnt = 0;
|
||||||
uint32_t key32 = key >> 32; // Use the high 32 bits as key inside the cluster
|
for (int i = 0; i < 1000 / ClusterSize; i++)
|
||||||
|
|
||||||
tte = replace = first_entry(key);
|
|
||||||
|
|
||||||
for (unsigned i = 0; i < ClusterSize; ++i, ++tte)
|
|
||||||
{
|
{
|
||||||
if (!tte->key32 || tte->key32 == key32) // Empty or overwrite old
|
const TTEntry* tte = &table[i].entry[0];
|
||||||
{
|
for (int j = 0; j < ClusterSize; j++)
|
||||||
if (!m)
|
if ((tte[j].genBound8 & 0xFC) == generation8)
|
||||||
m = tte->move(); // Preserve any existing ttMove
|
cnt++;
|
||||||
|
|
||||||
replace = tte;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implement replace strategy
|
|
||||||
if ( ( tte->generation8 == generation || tte->bound() == BOUND_EXACT)
|
|
||||||
- (replace->generation8 == generation)
|
|
||||||
- (tte->depth16 < replace->depth16) < 0)
|
|
||||||
replace = tte;
|
|
||||||
}
|
}
|
||||||
|
return cnt;
|
||||||
replace->save(key32, v, b, d, m, generation, statV);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,82 +24,98 @@
|
|||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
/// The TTEntry is the 14 bytes transposition table entry, defined as below:
|
/// TTEntry struct is the 10 bytes transposition table entry, defined as below:
|
||||||
///
|
///
|
||||||
/// key 32 bit
|
/// key 16 bit
|
||||||
/// move 16 bit
|
/// move 16 bit
|
||||||
/// bound type 8 bit
|
|
||||||
/// generation 8 bit
|
|
||||||
/// value 16 bit
|
/// value 16 bit
|
||||||
/// depth 16 bit
|
|
||||||
/// eval value 16 bit
|
/// eval value 16 bit
|
||||||
|
/// generation 6 bit
|
||||||
|
/// bound type 2 bit
|
||||||
|
/// depth 8 bit
|
||||||
|
|
||||||
struct TTEntry {
|
struct TTEntry {
|
||||||
|
|
||||||
Move move() const { return (Move )move16; }
|
Move move() const { return (Move )move16; }
|
||||||
Bound bound() const { return (Bound)bound8; }
|
Value value() const { return (Value)value16; }
|
||||||
Value value() const { return (Value)value16; }
|
Value eval() const { return (Value)eval16; }
|
||||||
Depth depth() const { return (Depth)depth16; }
|
Depth depth() const { return (Depth)(depth8 * int(ONE_PLY)); }
|
||||||
Value eval_value() const { return (Value)evalValue; }
|
Bound bound() const { return (Bound)(genBound8 & 0x3); }
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
void save(uint32_t k, Value v, Bound b, Depth d, Move m, uint8_t g, Value ev) {
|
uint16_t key16;
|
||||||
|
|
||||||
key32 = (uint32_t)k;
|
|
||||||
move16 = (uint16_t)m;
|
|
||||||
bound8 = (uint8_t)b;
|
|
||||||
generation8 = (uint8_t)g;
|
|
||||||
value16 = (int16_t)v;
|
|
||||||
depth16 = (int16_t)d;
|
|
||||||
evalValue = (int16_t)ev;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t key32;
|
|
||||||
uint16_t move16;
|
uint16_t move16;
|
||||||
uint8_t bound8, generation8;
|
int16_t value16;
|
||||||
int16_t value16, depth16, evalValue;
|
int16_t eval16;
|
||||||
|
uint8_t genBound8;
|
||||||
|
int8_t depth8;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// A TranspositionTable consists of a power of 2 number of clusters and each
|
/// A TranspositionTable consists of a power of 2 number of clusters and each
|
||||||
/// cluster consists of ClusterSize number of TTEntry. Each non-empty entry
|
/// cluster consists of ClusterSize number of TTEntry. Each non-empty entry
|
||||||
/// contains information of exactly one position. The size of a cluster should
|
/// contains information of exactly one position. The size of a cluster should
|
||||||
/// not be bigger than a cache line size. In case it is less, it should be padded
|
/// divide the size of a cache line size, to ensure that clusters never cross
|
||||||
/// to guarantee always aligned accesses.
|
/// cache lines. This ensures best cache performance, as the cacheline is
|
||||||
|
/// prefetched, as soon as possible.
|
||||||
|
|
||||||
class TranspositionTable {
|
class TranspositionTable {
|
||||||
|
|
||||||
static const unsigned ClusterSize = 4;
|
static const int CacheLineSize = 64;
|
||||||
|
static const int ClusterSize = 3;
|
||||||
|
|
||||||
|
struct Cluster {
|
||||||
|
TTEntry entry[ClusterSize];
|
||||||
|
char padding[2]; // Align to a divisor of the cache line size
|
||||||
|
};
|
||||||
|
|
||||||
|
static_assert(CacheLineSize % sizeof(Cluster) == 0, "Cluster size incorrect");
|
||||||
|
|
||||||
public:
|
public:
|
||||||
~TranspositionTable() { free(mem); }
|
~TranspositionTable() { free(mem); }
|
||||||
void new_search() { ++generation; }
|
void new_search() { generation8 += 4; } // Lower 2 bits are used by Bound
|
||||||
|
uint8_t generation() const { return generation8; }
|
||||||
const TTEntry* probe(const Key key) const;
|
TTEntry* probe(const Key key, bool& found) const;
|
||||||
TTEntry* first_entry(const Key key) const;
|
int hashfull() const;
|
||||||
void resize(uint64_t mbSize);
|
void resize(size_t mbSize);
|
||||||
void clear();
|
void clear();
|
||||||
void store(const Key key, Value v, Bound type, Depth d, Move m, Value statV);
|
|
||||||
|
// The lowest order bits of the key are used to get the index of the cluster
|
||||||
|
TTEntry* first_entry(const Key key) const {
|
||||||
|
return &table[(size_t)key & (clusterCount - 1)].entry[0];
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint32_t hashMask;
|
size_t clusterCount;
|
||||||
TTEntry* table;
|
Cluster* table;
|
||||||
void* mem;
|
void* mem;
|
||||||
uint8_t generation; // Size must be not bigger than TTEntry::generation8
|
uint8_t generation8; // Size must be not bigger than TTEntry::genBound8
|
||||||
};
|
};
|
||||||
|
|
||||||
extern TranspositionTable TT;
|
extern TranspositionTable TT;
|
||||||
|
|
||||||
|
|
||||||
/// TranspositionTable::first_entry() returns a pointer to the first entry of
|
|
||||||
/// a cluster given a position. The lowest order bits of the key are used to
|
|
||||||
/// get the index of the cluster.
|
|
||||||
|
|
||||||
inline TTEntry* TranspositionTable::first_entry(const Key key) const {
|
|
||||||
|
|
||||||
return table + ((uint32_t)key & hashMask);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // #ifndef TT_H_INCLUDED
|
#endif // #ifndef TT_H_INCLUDED
|
||||||
|
|||||||
+121
-151
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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,63 +21,62 @@
|
|||||||
#ifndef TYPES_H_INCLUDED
|
#ifndef TYPES_H_INCLUDED
|
||||||
#define TYPES_H_INCLUDED
|
#define TYPES_H_INCLUDED
|
||||||
|
|
||||||
/// For Linux and OSX configuration is done automatically using Makefile. To get
|
/// When compiling with provided Makefile (e.g. for Linux and OSX), configuration
|
||||||
/// started type 'make help'.
|
/// is done automatically. To get started type 'make help'.
|
||||||
///
|
///
|
||||||
/// For Windows, part of the configuration is detected automatically, but some
|
/// When Makefile is not used (e.g. with Microsoft Visual Studio) some switches
|
||||||
/// switches need to be set manually:
|
/// need to be set manually:
|
||||||
///
|
///
|
||||||
/// -DNDEBUG | Disable debugging mode. Always use this.
|
/// -DNDEBUG | Disable debugging mode. Always use this for release.
|
||||||
///
|
///
|
||||||
/// -DNO_PREFETCH | Disable use of prefetch asm-instruction. A must if you want
|
/// -DNO_PREFETCH | Disable use of prefetch asm-instruction. You may need this to
|
||||||
/// | the executable to run on some very old machines.
|
/// | run on some very old machines.
|
||||||
///
|
///
|
||||||
/// -DUSE_POPCNT | Add runtime support for use of popcnt asm-instruction. Works
|
/// -DUSE_POPCNT | Add runtime support for use of popcnt asm-instruction. Works
|
||||||
/// | only in 64-bit mode. For compiling requires hardware with
|
/// | only in 64-bit mode and requires hardware with popcnt support.
|
||||||
/// | popcnt support.
|
///
|
||||||
|
/// -DUSE_PEXT | Add runtime support for use of pext asm-instruction. Works
|
||||||
|
/// | only in 64-bit mode and requires hardware with pext support.
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <climits>
|
#include <climits>
|
||||||
|
#include <cstdint>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
#include "platform.h"
|
#if defined(_MSC_VER)
|
||||||
|
// Disable some silly and noisy warning from MSVC compiler
|
||||||
#define unlikely(x) (x) // For code annotation purposes
|
#pragma warning(disable: 4127) // Conditional expression is constant
|
||||||
|
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
|
||||||
#if defined(_WIN64) && !defined(IS_64BIT)
|
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
|
||||||
# include <intrin.h> // MSVC popcnt and bsfq instrinsics
|
|
||||||
# define IS_64BIT
|
|
||||||
# define USE_BSFQ
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(USE_POPCNT) && defined(_MSC_VER) && defined(__INTEL_COMPILER)
|
/// Predefined macros hell:
|
||||||
# include <nmmintrin.h> // Intel header for _mm_popcnt_u64() intrinsic
|
///
|
||||||
|
/// __GNUC__ Compiler is gcc, Clang or Intel on Linux
|
||||||
|
/// __INTEL_COMPILER Compiler is Intel
|
||||||
|
/// _MSC_VER Compiler is MSVC or Intel on Windows
|
||||||
|
/// _WIN32 Building on Windows (any)
|
||||||
|
/// _WIN64 Building on Windows 64 bit
|
||||||
|
|
||||||
|
#if defined(_WIN64) && defined(_MSC_VER) // No Makefile used
|
||||||
|
# include <intrin.h> // Microsoft header for _BitScanForward64()
|
||||||
|
# define IS_64BIT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(USE_POPCNT) && (defined(__INTEL_COMPILER) || defined(_MSC_VER))
|
||||||
|
# include <nmmintrin.h> // Intel and Microsoft header for _mm_popcnt_u64()
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(NO_PREFETCH) && (defined(__INTEL_COMPILER) || defined(_MSC_VER))
|
||||||
|
# include <xmmintrin.h> // Intel and Microsoft header for _mm_prefetch()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(USE_PEXT)
|
#if defined(USE_PEXT)
|
||||||
# include <immintrin.h> // Header for _pext_u64() intrinsic
|
# include <immintrin.h> // Header for _pext_u64() intrinsic
|
||||||
|
# define pext(b, m) _pext_u64(b, m)
|
||||||
#else
|
#else
|
||||||
# define _pext_u64(b, m) (0)
|
# define pext(b, m) (0)
|
||||||
#endif
|
|
||||||
|
|
||||||
# if !defined(NO_PREFETCH) && (defined(__INTEL_COMPILER) || defined(_MSC_VER))
|
|
||||||
# include <xmmintrin.h> // Intel and Microsoft header for _mm_prefetch()
|
|
||||||
# endif
|
|
||||||
|
|
||||||
#define CACHE_LINE_SIZE 64
|
|
||||||
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
|
|
||||||
# define CACHE_LINE_ALIGNMENT __declspec(align(CACHE_LINE_SIZE))
|
|
||||||
#else
|
|
||||||
# define CACHE_LINE_ALIGNMENT __attribute__ ((aligned(CACHE_LINE_SIZE)))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
# define FORCE_INLINE __forceinline
|
|
||||||
#elif defined(__GNUC__)
|
|
||||||
# define FORCE_INLINE inline __attribute__((always_inline))
|
|
||||||
#else
|
|
||||||
# define FORCE_INLINE inline
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_POPCNT
|
#ifdef USE_POPCNT
|
||||||
@@ -100,9 +100,8 @@ const bool Is64Bit = false;
|
|||||||
typedef uint64_t Key;
|
typedef uint64_t Key;
|
||||||
typedef uint64_t Bitboard;
|
typedef uint64_t Bitboard;
|
||||||
|
|
||||||
const int MAX_MOVES = 256;
|
const int MAX_MOVES = 256;
|
||||||
const int MAX_PLY = 120;
|
const int MAX_PLY = 128;
|
||||||
const int MAX_PLY_PLUS_6 = MAX_PLY + 6;
|
|
||||||
|
|
||||||
/// A move needs 16 bits to be stored
|
/// A move needs 16 bits to be stored
|
||||||
///
|
///
|
||||||
@@ -116,7 +115,7 @@ const int MAX_PLY_PLUS_6 = MAX_PLY + 6;
|
|||||||
/// any normal move destination square is always different from origin square
|
/// any normal move destination square is always different from origin square
|
||||||
/// while MOVE_NONE and MOVE_NULL have the same origin and destination square.
|
/// while MOVE_NONE and MOVE_NULL have the same origin and destination square.
|
||||||
|
|
||||||
enum Move {
|
enum Move : int {
|
||||||
MOVE_NONE,
|
MOVE_NONE,
|
||||||
MOVE_NULL = 65
|
MOVE_NULL = 65
|
||||||
};
|
};
|
||||||
@@ -136,12 +135,12 @@ enum CastlingSide {
|
|||||||
KING_SIDE, QUEEN_SIDE, CASTLING_SIDE_NB = 2
|
KING_SIDE, QUEEN_SIDE, CASTLING_SIDE_NB = 2
|
||||||
};
|
};
|
||||||
|
|
||||||
enum CastlingRight { // Defined as in PolyGlot book hash key
|
enum CastlingRight {
|
||||||
NO_CASTLING,
|
NO_CASTLING,
|
||||||
WHITE_OO,
|
WHITE_OO,
|
||||||
WHITE_OOO = WHITE_OO << 1,
|
WHITE_OOO = WHITE_OO << 1,
|
||||||
BLACK_OO = WHITE_OO << 2,
|
BLACK_OO = WHITE_OO << 2,
|
||||||
BLACK_OOO = WHITE_OO << 3,
|
BLACK_OOO = WHITE_OO << 3,
|
||||||
ANY_CASTLING = WHITE_OO | WHITE_OOO | BLACK_OO | BLACK_OOO,
|
ANY_CASTLING = WHITE_OO | WHITE_OOO | BLACK_OO | BLACK_OOO,
|
||||||
CASTLING_RIGHT_NB = 16
|
CASTLING_RIGHT_NB = 16
|
||||||
};
|
};
|
||||||
@@ -173,7 +172,7 @@ enum Bound {
|
|||||||
BOUND_EXACT = BOUND_UPPER | BOUND_LOWER
|
BOUND_EXACT = BOUND_UPPER | BOUND_LOWER
|
||||||
};
|
};
|
||||||
|
|
||||||
enum Value {
|
enum Value : int {
|
||||||
VALUE_ZERO = 0,
|
VALUE_ZERO = 0,
|
||||||
VALUE_DRAW = 0,
|
VALUE_DRAW = 0,
|
||||||
VALUE_KNOWN_WIN = 10000,
|
VALUE_KNOWN_WIN = 10000,
|
||||||
@@ -181,19 +180,16 @@ enum Value {
|
|||||||
VALUE_INFINITE = 32001,
|
VALUE_INFINITE = 32001,
|
||||||
VALUE_NONE = 32002,
|
VALUE_NONE = 32002,
|
||||||
|
|
||||||
VALUE_MATE_IN_MAX_PLY = VALUE_MATE - MAX_PLY,
|
VALUE_MATE_IN_MAX_PLY = VALUE_MATE - 2 * MAX_PLY,
|
||||||
VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + MAX_PLY,
|
VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + 2 * MAX_PLY,
|
||||||
|
|
||||||
VALUE_ENSURE_INTEGER_SIZE_P = INT_MAX,
|
PawnValueMg = 188, PawnValueEg = 248,
|
||||||
VALUE_ENSURE_INTEGER_SIZE_N = INT_MIN,
|
KnightValueMg = 753, KnightValueEg = 832,
|
||||||
|
BishopValueMg = 826, BishopValueEg = 897,
|
||||||
|
RookValueMg = 1285, RookValueEg = 1371,
|
||||||
|
QueenValueMg = 2513, QueenValueEg = 2650,
|
||||||
|
|
||||||
PawnValueMg = 198, PawnValueEg = 258,
|
MidgameLimit = 15258, EndgameLimit = 3915
|
||||||
KnightValueMg = 817, KnightValueEg = 846,
|
|
||||||
BishopValueMg = 836, BishopValueEg = 857,
|
|
||||||
RookValueMg = 1270, RookValueEg = 1278,
|
|
||||||
QueenValueMg = 2521, QueenValueEg = 2558,
|
|
||||||
|
|
||||||
MidgameLimit = 15581, EndgameLimit = 3998
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum PieceType {
|
enum PieceType {
|
||||||
@@ -209,18 +205,25 @@ 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];
|
||||||
|
|
||||||
enum Depth {
|
enum Depth {
|
||||||
|
|
||||||
ONE_PLY = 2,
|
ONE_PLY = 1,
|
||||||
|
|
||||||
DEPTH_ZERO = 0 * ONE_PLY,
|
DEPTH_ZERO = 0 * ONE_PLY,
|
||||||
DEPTH_QS_CHECKS = 0 * ONE_PLY,
|
DEPTH_QS_CHECKS = 0 * ONE_PLY,
|
||||||
DEPTH_QS_NO_CHECKS = -1 * ONE_PLY,
|
DEPTH_QS_NO_CHECKS = -1 * ONE_PLY,
|
||||||
DEPTH_QS_RECAPTURES = -5 * ONE_PLY,
|
DEPTH_QS_RECAPTURES = -5 * ONE_PLY,
|
||||||
|
|
||||||
DEPTH_NONE = -127 * ONE_PLY
|
DEPTH_NONE = -6 * ONE_PLY,
|
||||||
|
DEPTH_MAX = MAX_PLY * ONE_PLY
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static_assert(!(ONE_PLY & (ONE_PLY - 1)), "ONE_PLY is not a power of 2");
|
||||||
|
|
||||||
enum Square {
|
enum Square {
|
||||||
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,
|
||||||
@@ -234,80 +237,67 @@ enum Square {
|
|||||||
|
|
||||||
SQUARE_NB = 64,
|
SQUARE_NB = 64,
|
||||||
|
|
||||||
DELTA_N = 8,
|
NORTH = 8,
|
||||||
DELTA_E = 1,
|
EAST = 1,
|
||||||
DELTA_S = -8,
|
SOUTH = -8,
|
||||||
DELTA_W = -1,
|
WEST = -1,
|
||||||
|
|
||||||
DELTA_NN = DELTA_N + DELTA_N,
|
NORTH_EAST = NORTH + EAST,
|
||||||
DELTA_NE = DELTA_N + DELTA_E,
|
SOUTH_EAST = SOUTH + EAST,
|
||||||
DELTA_SE = DELTA_S + DELTA_E,
|
SOUTH_WEST = SOUTH + WEST,
|
||||||
DELTA_SS = DELTA_S + DELTA_S,
|
NORTH_WEST = NORTH + WEST
|
||||||
DELTA_SW = DELTA_S + DELTA_W,
|
|
||||||
DELTA_NW = DELTA_N + DELTA_W
|
|
||||||
};
|
};
|
||||||
|
|
||||||
enum File {
|
enum File : int {
|
||||||
FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_NB
|
FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_NB
|
||||||
};
|
};
|
||||||
|
|
||||||
enum Rank {
|
enum Rank : int {
|
||||||
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB
|
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/// The 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). The least significant 16 bits are used to store the endgame value
|
/// (enum). The least significant 16 bits are used to store the endgame value
|
||||||
/// and the upper 16 bits are used to store the middlegame value. The compiler
|
/// and the upper 16 bits are used to store the middlegame value. Take some
|
||||||
/// is free to choose the enum type as long as it can store the data, so we
|
/// care to avoid left-shifting a signed int to avoid undefined behavior.
|
||||||
/// ensure that Score is an integer type by assigning some big int values.
|
enum Score : int { SCORE_ZERO };
|
||||||
enum Score {
|
|
||||||
SCORE_ZERO,
|
|
||||||
SCORE_ENSURE_INTEGER_SIZE_P = INT_MAX,
|
|
||||||
SCORE_ENSURE_INTEGER_SIZE_N = INT_MIN
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef union {
|
|
||||||
uint32_t full;
|
|
||||||
struct { int16_t eg, mg; } half;
|
|
||||||
} ScoreView;
|
|
||||||
|
|
||||||
inline Score make_score(int mg, int eg) {
|
inline Score make_score(int mg, int eg) {
|
||||||
ScoreView v;
|
return Score((int)((unsigned int)eg << 16) + mg);
|
||||||
v.half.mg = (int16_t)(mg - (uint16_t(eg) >> 15));
|
}
|
||||||
v.half.eg = (int16_t)eg;
|
|
||||||
return Score(v.full);
|
/// Extracting the signed lower and upper 16 bits is not so trivial because
|
||||||
|
/// according to the standard a simple cast to short is implementation defined
|
||||||
|
/// and so is a right shift of a signed integer.
|
||||||
|
inline Value eg_value(Score s) {
|
||||||
|
|
||||||
|
union { uint16_t u; int16_t s; } eg = { uint16_t(unsigned(s + 0x8000) >> 16) };
|
||||||
|
return Value(eg.s);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Value mg_value(Score s) {
|
inline Value mg_value(Score s) {
|
||||||
ScoreView v;
|
|
||||||
v.full = s;
|
union { uint16_t u; int16_t s; } mg = { uint16_t(unsigned(s)) };
|
||||||
return Value(v.half.mg + (uint16_t(v.half.eg) >> 15));
|
return Value(mg.s);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Value eg_value(Score s) {
|
#define ENABLE_BASE_OPERATORS_ON(T) \
|
||||||
ScoreView v;
|
inline T operator+(T d1, T d2) { return T(int(d1) + int(d2)); } \
|
||||||
v.full = s;
|
inline T operator-(T d1, T d2) { return T(int(d1) - int(d2)); } \
|
||||||
return Value(v.half.eg);
|
inline T operator*(int i, T d) { return T(i * int(d)); } \
|
||||||
}
|
inline T operator*(T d, int i) { return T(int(d) * i); } \
|
||||||
|
inline T operator-(T d) { return T(-int(d)); } \
|
||||||
#define ENABLE_BASE_OPERATORS_ON(T) \
|
inline T& operator+=(T& d1, T d2) { return d1 = d1 + d2; } \
|
||||||
inline T operator+(const T d1, const T d2) { return T(int(d1) + int(d2)); } \
|
inline T& operator-=(T& d1, T d2) { return d1 = d1 - d2; } \
|
||||||
inline T operator-(const T d1, const T d2) { return T(int(d1) - int(d2)); } \
|
|
||||||
inline T operator*(int i, const T d) { return T(i * int(d)); } \
|
|
||||||
inline T operator*(const T d, int i) { return T(int(d) * i); } \
|
|
||||||
inline T operator-(const T d) { return T(-int(d)); } \
|
|
||||||
inline T& operator+=(T& d1, const T d2) { return d1 = d1 + d2; } \
|
|
||||||
inline T& operator-=(T& d1, const T d2) { return d1 = d1 - d2; } \
|
|
||||||
inline T& operator*=(T& d, int i) { return d = T(int(d) * i); }
|
inline T& operator*=(T& d, int i) { return d = T(int(d) * i); }
|
||||||
|
|
||||||
ENABLE_BASE_OPERATORS_ON(Score)
|
#define ENABLE_FULL_OPERATORS_ON(T) \
|
||||||
|
ENABLE_BASE_OPERATORS_ON(T) \
|
||||||
#define ENABLE_FULL_OPERATORS_ON(T) \
|
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
|
||||||
ENABLE_BASE_OPERATORS_ON(T) \
|
inline T& operator--(T& d) { return d = T(int(d) - 1); } \
|
||||||
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
|
inline T operator/(T d, int i) { return T(int(d) / i); } \
|
||||||
inline T& operator--(T& d) { return d = T(int(d) - 1); } \
|
inline int operator/(T d1, T d2) { return int(d1) / int(d2); } \
|
||||||
inline T operator/(const T d, int i) { return 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)
|
||||||
@@ -319,6 +309,8 @@ ENABLE_FULL_OPERATORS_ON(Square)
|
|||||||
ENABLE_FULL_OPERATORS_ON(File)
|
ENABLE_FULL_OPERATORS_ON(File)
|
||||||
ENABLE_FULL_OPERATORS_ON(Rank)
|
ENABLE_FULL_OPERATORS_ON(Rank)
|
||||||
|
|
||||||
|
ENABLE_BASE_OPERATORS_ON(Score)
|
||||||
|
|
||||||
#undef ENABLE_FULL_OPERATORS_ON
|
#undef ENABLE_FULL_OPERATORS_ON
|
||||||
#undef ENABLE_BASE_OPERATORS_ON
|
#undef ENABLE_BASE_OPERATORS_ON
|
||||||
|
|
||||||
@@ -337,25 +329,18 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern Value PieceValue[PHASE_NB][PIECE_NB];
|
|
||||||
|
|
||||||
struct ExtMove {
|
|
||||||
Move move;
|
|
||||||
Value value;
|
|
||||||
};
|
|
||||||
|
|
||||||
inline bool operator<(const ExtMove& f, const ExtMove& s) {
|
|
||||||
return f.value < s.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Color operator~(Color c) {
|
inline Color operator~(Color c) {
|
||||||
return Color(c ^ BLACK);
|
return Color(c ^ BLACK); // Toggle color
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Square operator~(Square s) {
|
inline 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) {
|
||||||
|
return Piece(pc ^ 8); // Swap color of piece B_KNIGHT -> W_KNIGHT
|
||||||
|
}
|
||||||
|
|
||||||
inline CastlingRight operator|(Color c, CastlingSide s) {
|
inline CastlingRight operator|(Color c, CastlingSide s) {
|
||||||
return CastlingRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
|
return CastlingRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
|
||||||
}
|
}
|
||||||
@@ -369,14 +354,14 @@ inline Value mated_in(int ply) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline Square make_square(File f, Rank r) {
|
inline 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) {
|
inline Piece make_piece(Color c, PieceType pt) {
|
||||||
return Piece((c << 3) | pt);
|
return Piece((c << 3) + pt);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline PieceType type_of(Piece pc) {
|
inline PieceType type_of(Piece pc) {
|
||||||
return PieceType(pc & 7);
|
return PieceType(pc & 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -414,16 +399,8 @@ inline bool opposite_colors(Square s1, Square s2) {
|
|||||||
return ((s >> 3) ^ s) & 1;
|
return ((s >> 3) ^ s) & 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline char to_char(File f, bool tolower = true) {
|
|
||||||
return char(f - FILE_A + (tolower ? 'a' : 'A'));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline char to_char(Rank r) {
|
|
||||||
return char(r - RANK_1 + '1');
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Square pawn_push(Color c) {
|
inline Square pawn_push(Color c) {
|
||||||
return c == WHITE ? DELTA_N : DELTA_S;
|
return c == WHITE ? NORTH : SOUTH;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Square from_sq(Move m) {
|
inline Square from_sq(Move m) {
|
||||||
@@ -439,27 +416,20 @@ inline MoveType type_of(Move m) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline PieceType promotion_type(Move m) {
|
inline PieceType promotion_type(Move m) {
|
||||||
return PieceType(((m >> 12) & 3) + 2);
|
return PieceType(((m >> 12) & 3) + KNIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Move make_move(Square from, Square to) {
|
inline Move make_move(Square from, Square to) {
|
||||||
return Move(to | (from << 6));
|
return Move((from << 6) + to);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<MoveType T>
|
template<MoveType T>
|
||||||
inline Move make(Square from, Square to, PieceType pt = KNIGHT) {
|
inline Move make(Square from, Square to, PieceType pt = KNIGHT) {
|
||||||
return Move(to | (from << 6) | T | ((pt - KNIGHT) << 12));
|
return Move(T + ((pt - KNIGHT) << 12) + (from << 6) + to);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool is_ok(Move m) {
|
inline bool is_ok(Move m) {
|
||||||
return from_sq(m) != to_sq(m); // Catches also MOVE_NULL and MOVE_NONE
|
return from_sq(m) != to_sq(m); // Catch MOVE_NULL and MOVE_NONE
|
||||||
}
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
inline const std::string to_string(Square s) {
|
|
||||||
char ch[] = { to_char(file_of(s)), to_char(rank_of(s)), 0 };
|
|
||||||
return ch;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // #ifndef TYPES_H_INCLUDED
|
#endif // #ifndef TYPES_H_INCLUDED
|
||||||
|
|||||||
+137
-67
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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
|
||||||
@@ -17,18 +18,17 @@
|
|||||||
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 <iomanip>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "evaluate.h"
|
#include "evaluate.h"
|
||||||
#include "notation.h"
|
#include "movegen.h"
|
||||||
#include "position.h"
|
#include "position.h"
|
||||||
#include "search.h"
|
#include "search.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "tt.h"
|
#include "timeman.h"
|
||||||
#include "ucioption.h"
|
#include "uci.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -39,10 +39,10 @@ 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";
|
||||||
|
|
||||||
// Keep a track of the position keys along the setup moves (from the start position
|
// A list to keep track of the position states along the setup moves (from the
|
||||||
// to the position just before the search starts). This is needed by the repetition
|
// start position to the position just before the search starts). Needed by
|
||||||
// draw detection code.
|
// 'draw by repetition' detection.
|
||||||
Search::StateStackPtr SetupStates;
|
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.
|
||||||
@@ -68,14 +68,14 @@ namespace {
|
|||||||
else
|
else
|
||||||
return;
|
return;
|
||||||
|
|
||||||
pos.set(fen, Options["UCI_Chess960"], Threads.main());
|
States = StateListPtr(new std::deque<StateInfo>(1));
|
||||||
SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
|
pos.set(fen, Options["UCI_Chess960"], &States->back(), Threads.main());
|
||||||
|
|
||||||
// Parse move list (if any)
|
// Parse move list (if any)
|
||||||
while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
|
while (is >> token && (m = UCI::to_move(pos, token)) != MOVE_NONE)
|
||||||
{
|
{
|
||||||
SetupStates->push(StateInfo());
|
States->push_back(StateInfo());
|
||||||
pos.do_move(m, SetupStates->top());
|
pos.do_move(m, States->back(), pos.gives_check(m));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,11 +91,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()) + token;
|
name += string(" ", name.empty() ? 0 : 1) + token;
|
||||||
|
|
||||||
// Read option value (can contain spaces)
|
// Read option value (can contain spaces)
|
||||||
while (is >> token)
|
while (is >> token)
|
||||||
value += string(" ", !value.empty()) + token;
|
value += string(" ", value.empty() ? 0 : 1) + token;
|
||||||
|
|
||||||
if (Options.count(name))
|
if (Options.count(name))
|
||||||
Options[name] = value;
|
Options[name] = value;
|
||||||
@@ -105,19 +105,20 @@ namespace {
|
|||||||
|
|
||||||
|
|
||||||
// go() is called when engine receives the "go" UCI command. The function sets
|
// go() is called when engine receives the "go" UCI command. The function sets
|
||||||
// the thinking time and other parameters from the input string, and starts
|
// the thinking time and other parameters from the input string, then starts
|
||||||
// the search.
|
// the search.
|
||||||
|
|
||||||
void go(const Position& pos, istringstream& is) {
|
void go(Position& pos, istringstream& is) {
|
||||||
|
|
||||||
Search::LimitsType limits;
|
Search::LimitsType limits;
|
||||||
string token;
|
string token;
|
||||||
|
|
||||||
|
limits.startTime = now(); // As early as possible!
|
||||||
|
|
||||||
while (is >> token)
|
while (is >> token)
|
||||||
{
|
|
||||||
if (token == "searchmoves")
|
if (token == "searchmoves")
|
||||||
while (is >> token)
|
while (is >> token)
|
||||||
limits.searchmoves.push_back(move_from_uci(pos, token));
|
limits.searchmoves.push_back(UCI::to_move(pos, token));
|
||||||
|
|
||||||
else if (token == "wtime") is >> limits.time[WHITE];
|
else if (token == "wtime") is >> limits.time[WHITE];
|
||||||
else if (token == "btime") is >> limits.time[BLACK];
|
else if (token == "btime") is >> limits.time[BLACK];
|
||||||
@@ -128,92 +129,161 @@ 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 == "infinite") limits.infinite = true;
|
else if (token == "infinite") limits.infinite = 1;
|
||||||
else if (token == "ponder") limits.ponder = true;
|
else if (token == "ponder") limits.ponder = 1;
|
||||||
}
|
|
||||||
|
|
||||||
Threads.start_thinking(pos, limits, SetupStates);
|
Threads.start_thinking(pos, States, limits);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
|
||||||
/// Wait for a command from the user, parse this text string as an UCI command,
|
/// UCI::loop() waits for a command from stdin, parses it and calls the appropriate
|
||||||
/// and call the appropriate functions. Also intercepts EOF from stdin to ensure
|
/// function. Also intercepts EOF from stdin to ensure gracefully exiting if the
|
||||||
/// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
|
/// GUI dies unexpectedly. When called with some command line arguments, e.g. to
|
||||||
/// commands, the function also supports a few debug commands.
|
/// run 'bench', once the command is executed the function returns immediately.
|
||||||
|
/// In addition to the UCI ones, also some additional debug commands are supported.
|
||||||
|
|
||||||
void UCI::loop(int argc, char* argv[]) {
|
void UCI::loop(int argc, char* argv[]) {
|
||||||
|
|
||||||
Position pos(StartFEN, false, Threads.main()); // The root position
|
Position pos;
|
||||||
string token, cmd;
|
string token, cmd;
|
||||||
|
|
||||||
|
pos.set(StartFEN, false, &States->back(), Threads.main());
|
||||||
|
|
||||||
for (int i = 1; i < argc; ++i)
|
for (int i = 1; i < argc; ++i)
|
||||||
cmd += std::string(argv[i]) + " ";
|
cmd += std::string(argv[i]) + " ";
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (argc == 1 && !getline(cin, cmd)) // Block here waiting for input
|
if (argc == 1 && !getline(cin, cmd)) // Block here waiting for input or EOF
|
||||||
cmd = "quit";
|
cmd = "quit";
|
||||||
|
|
||||||
istringstream is(cmd);
|
istringstream is(cmd);
|
||||||
|
|
||||||
|
token.clear(); // getline() could return empty or blank line
|
||||||
is >> skipws >> token;
|
is >> skipws >> token;
|
||||||
|
|
||||||
if (token == "quit" || token == "stop" || token == "ponderhit")
|
// The GUI sends 'ponderhit' to tell us to ponder on the same move the
|
||||||
|
// opponent has played. In case Signals.stopOnPonderhit is set we are
|
||||||
|
// waiting for 'ponderhit' to stop the search (for instance because we
|
||||||
|
// already ran out of time), otherwise we should continue searching but
|
||||||
|
// switching from pondering to normal search.
|
||||||
|
if ( token == "quit"
|
||||||
|
|| token == "stop"
|
||||||
|
|| (token == "ponderhit" && Search::Signals.stopOnPonderhit))
|
||||||
{
|
{
|
||||||
// The GUI sends 'ponderhit' to tell us to ponder on the same move the
|
Search::Signals.stop = true;
|
||||||
// opponent has played. In case Signals.stopOnPonderhit is set we are
|
Threads.main()->start_searching(true); // Could be sleeping
|
||||||
// waiting for 'ponderhit' to stop the search (for instance because we
|
|
||||||
// already ran out of time), otherwise we should continue searching but
|
|
||||||
// switch from pondering to normal search.
|
|
||||||
if (token != "ponderhit" || Search::Signals.stopOnPonderhit)
|
|
||||||
{
|
|
||||||
Search::Signals.stop = true;
|
|
||||||
Threads.main()->notify_one(); // Could be sleeping
|
|
||||||
}
|
|
||||||
else
|
|
||||||
Search::Limits.ponder = false;
|
|
||||||
}
|
}
|
||||||
else if (token == "perft" || token == "divide")
|
else if (token == "ponderhit")
|
||||||
{
|
Search::Limits.ponder = 0; // Switch to normal search
|
||||||
int depth;
|
|
||||||
stringstream ss;
|
|
||||||
|
|
||||||
is >> depth;
|
|
||||||
ss << Options["Hash"] << " "
|
|
||||||
<< Options["Threads"] << " " << depth << " current " << token;
|
|
||||||
|
|
||||||
benchmark(pos, ss);
|
|
||||||
}
|
|
||||||
else if (token == "key")
|
|
||||||
sync_cout << hex << uppercase << setfill('0')
|
|
||||||
<< "position key: " << setw(16) << pos.key()
|
|
||||||
<< "\nmaterial key: " << setw(16) << pos.material_key()
|
|
||||||
<< "\npawn key: " << setw(16) << pos.pawn_key()
|
|
||||||
<< dec << nouppercase << setfill(' ') << sync_endl;
|
|
||||||
|
|
||||||
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 == "eval")
|
else if (token == "ucinewgame")
|
||||||
{
|
{
|
||||||
Search::RootColor = pos.side_to_move(); // Ensure it is set
|
Search::clear();
|
||||||
sync_cout << Eval::trace(pos) << sync_endl;
|
Time.availableNodes = 0;
|
||||||
}
|
}
|
||||||
else if (token == "ucinewgame") { /* Avoid returning "Unknown command" */ }
|
else if (token == "isready") sync_cout << "readyok" << sync_endl;
|
||||||
else if (token == "go") go(pos, is);
|
else if (token == "go") go(pos, is);
|
||||||
else if (token == "position") position(pos, is);
|
else if (token == "position") position(pos, is);
|
||||||
else if (token == "setoption") setoption(is);
|
else if (token == "setoption") setoption(is);
|
||||||
|
|
||||||
|
// Additional custom non-UCI commands, useful 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") benchmark(pos, is);
|
||||||
else if (token == "d") sync_cout << pos.pretty() << sync_endl;
|
else if (token == "d") sync_cout << pos << sync_endl;
|
||||||
else if (token == "isready") sync_cout << "readyok" << 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); // Passed args have one-shot behaviour
|
||||||
|
|
||||||
Threads.wait_for_think_finished(); // Cannot quit whilst the search is running
|
Threads.main()->wait_for_search_finished();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// UCI::value() converts a Value to a string suitable for use with the UCI
|
||||||
|
/// protocol specification:
|
||||||
|
///
|
||||||
|
/// cp <x> The score from the engine's point of view in centipawns.
|
||||||
|
/// mate <y> Mate in y moves, not plies. If the engine is getting mated
|
||||||
|
/// use negative values for y.
|
||||||
|
|
||||||
|
string UCI::value(Value v) {
|
||||||
|
|
||||||
|
stringstream ss;
|
||||||
|
|
||||||
|
if (abs(v) < VALUE_MATE - MAX_PLY)
|
||||||
|
ss << "cp " << v * 100 / PawnValueEg;
|
||||||
|
else
|
||||||
|
ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// UCI::square() converts a Square to a string in algebraic notation (g1, a7, etc.)
|
||||||
|
|
||||||
|
std::string UCI::square(Square s) {
|
||||||
|
return std::string{ char('a' + file_of(s)), char('1' + rank_of(s)) };
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// UCI::move() converts a Move to a string in coordinate notation (g1f3, a7a8q).
|
||||||
|
/// The only special case is castling, where we print in the e1g1 notation in
|
||||||
|
/// normal chess mode, and in e1h1 notation in chess960 mode. Internally all
|
||||||
|
/// castling moves are always encoded as 'king captures rook'.
|
||||||
|
|
||||||
|
string UCI::move(Move m, bool chess960) {
|
||||||
|
|
||||||
|
Square from = from_sq(m);
|
||||||
|
Square to = to_sq(m);
|
||||||
|
|
||||||
|
if (m == MOVE_NONE)
|
||||||
|
return "(none)";
|
||||||
|
|
||||||
|
if (m == MOVE_NULL)
|
||||||
|
return "0000";
|
||||||
|
|
||||||
|
if (type_of(m) == CASTLING && !chess960)
|
||||||
|
to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
|
||||||
|
|
||||||
|
string move = UCI::square(from) + UCI::square(to);
|
||||||
|
|
||||||
|
if (type_of(m) == PROMOTION)
|
||||||
|
move += " pnbrqk"[promotion_type(m)];
|
||||||
|
|
||||||
|
return move;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// UCI::to_move() converts a string representing a move in coordinate notation
|
||||||
|
/// (g1f3, a7a8q) to the corresponding legal Move, if any.
|
||||||
|
|
||||||
|
Move UCI::to_move(const Position& pos, string& str) {
|
||||||
|
|
||||||
|
if (str.length() == 5) // Junior could send promotion piece in uppercase
|
||||||
|
str[4] = char(tolower(str[4]));
|
||||||
|
|
||||||
|
for (const auto& m : MoveList<LEGAL>(pos))
|
||||||
|
if (str == UCI::move(m, pos.is_chess960()))
|
||||||
|
return m;
|
||||||
|
|
||||||
|
return MOVE_NONE;
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-10
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
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
|
||||||
@@ -17,12 +18,16 @@
|
|||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef UCIOPTION_H_INCLUDED
|
#ifndef UCI_H_INCLUDED
|
||||||
#define UCIOPTION_H_INCLUDED
|
#define UCI_H_INCLUDED
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
class Position;
|
||||||
|
|
||||||
namespace UCI {
|
namespace UCI {
|
||||||
|
|
||||||
class Option;
|
class Option;
|
||||||
@@ -41,13 +46,13 @@ class Option {
|
|||||||
typedef void (*OnChange)(const Option&);
|
typedef void (*OnChange)(const Option&);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Option(OnChange = NULL);
|
Option(OnChange = nullptr);
|
||||||
Option(bool v, OnChange = NULL);
|
Option(bool v, OnChange = nullptr);
|
||||||
Option(const char* v, OnChange = NULL);
|
Option(const char* v, OnChange = nullptr);
|
||||||
Option(int v, int min, int max, OnChange = NULL);
|
Option(int v, int min, int max, OnChange = nullptr);
|
||||||
|
|
||||||
Option& operator=(const std::string& v);
|
Option& operator=(const std::string&);
|
||||||
void operator<<(const Option& o);
|
void operator<<(const Option&);
|
||||||
operator int() const;
|
operator int() const;
|
||||||
operator std::string() const;
|
operator std::string() const;
|
||||||
|
|
||||||
@@ -62,9 +67,14 @@ private:
|
|||||||
|
|
||||||
void init(OptionsMap&);
|
void init(OptionsMap&);
|
||||||
void loop(int argc, char* argv[]);
|
void loop(int argc, char* argv[]);
|
||||||
|
std::string value(Value v);
|
||||||
|
std::string square(Square s);
|
||||||
|
std::string move(Move m, bool chess960);
|
||||||
|
std::string pv(const Position& pos, Depth depth, Value alpha, Value beta);
|
||||||
|
Move to_move(const Position& pos, std::string& str);
|
||||||
|
|
||||||
} // namespace UCI
|
} // namespace UCI
|
||||||
|
|
||||||
extern UCI::OptionsMap Options;
|
extern UCI::OptionsMap Options;
|
||||||
|
|
||||||
#endif // #ifndef UCIOPTION_H_INCLUDED
|
#endif // #ifndef UCI_H_INCLUDED
|
||||||
+40
-49
@@ -1,7 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
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-2014 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
|
||||||
|
|
||||||
Stockfish is free software: you can redistribute it and/or modify
|
Stockfish is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -19,14 +20,14 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstdlib>
|
#include <ostream>
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
#include "evaluate.h"
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
#include "search.h"
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "tt.h"
|
#include "tt.h"
|
||||||
#include "ucioption.h"
|
#include "uci.h"
|
||||||
|
#include "syzygy/tbprobe.h"
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
@@ -35,18 +36,18 @@ UCI::OptionsMap Options; // Global object
|
|||||||
namespace UCI {
|
namespace UCI {
|
||||||
|
|
||||||
/// 'On change' actions, triggered by an option's value change
|
/// 'On change' actions, triggered by an option's value change
|
||||||
void on_logger(const Option& o) { start_logger(o); }
|
void on_clear_hash(const Option&) { Search::clear(); }
|
||||||
void on_eval(const Option&) { Eval::init(); }
|
|
||||||
void on_threads(const Option&) { Threads.read_uci_options(); }
|
|
||||||
void on_hash_size(const Option& o) { TT.resize(o); }
|
void on_hash_size(const Option& o) { TT.resize(o); }
|
||||||
void on_clear_hash(const Option&) { TT.clear(); }
|
void on_logger(const Option& o) { start_logger(o); }
|
||||||
|
void on_threads(const Option&) { Threads.read_uci_options(); }
|
||||||
|
void on_tb_path(const Option& o) { Tablebases::init(o); }
|
||||||
|
|
||||||
|
|
||||||
/// Our case insensitive less() function as required by UCI protocol
|
/// Our case insensitive less() function as required by UCI protocol
|
||||||
bool ci_less(char c1, char c2) { return tolower(c1) < tolower(c2); }
|
|
||||||
|
|
||||||
bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
|
bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
|
||||||
return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), ci_less);
|
|
||||||
|
return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(),
|
||||||
|
[](char c1, char c2) { return tolower(c1) < tolower(c2); });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -54,35 +55,25 @@ bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const
|
|||||||
|
|
||||||
void init(OptionsMap& o) {
|
void init(OptionsMap& o) {
|
||||||
|
|
||||||
o["Write Debug Log"] << Option(false, on_logger);
|
const int MaxHashMB = Is64Bit ? 1024 * 1024 : 2048;
|
||||||
o["Write Search Log"] << Option(false);
|
|
||||||
o["Search Log Filename"] << Option("SearchLog.txt");
|
o["Debug Log File"] << Option("", on_logger);
|
||||||
o["Book File"] << Option("book.bin");
|
o["Contempt"] << Option(0, -100, 100);
|
||||||
o["Best Book Move"] << Option(false);
|
o["Threads"] << Option(1, 1, 128, on_threads);
|
||||||
o["Contempt Factor"] << Option(0, -50, 50);
|
o["Hash"] << Option(16, 1, MaxHashMB, on_hash_size);
|
||||||
o["Mobility (Midgame)"] << Option(100, 0, 200, on_eval);
|
o["Clear Hash"] << Option(on_clear_hash);
|
||||||
o["Mobility (Endgame)"] << Option(100, 0, 200, on_eval);
|
o["Ponder"] << Option(false);
|
||||||
o["Pawn Structure (Midgame)"] << Option(100, 0, 200, on_eval);
|
o["MultiPV"] << Option(1, 1, 500);
|
||||||
o["Pawn Structure (Endgame)"] << Option(100, 0, 200, on_eval);
|
o["Skill Level"] << Option(20, 0, 20);
|
||||||
o["Passed Pawns (Midgame)"] << Option(100, 0, 200, on_eval);
|
o["Move Overhead"] << Option(30, 0, 5000);
|
||||||
o["Passed Pawns (Endgame)"] << Option(100, 0, 200, on_eval);
|
o["Minimum Thinking Time"] << Option(20, 0, 5000);
|
||||||
o["Space"] << Option(100, 0, 200, on_eval);
|
o["Slow Mover"] << Option(89, 10, 1000);
|
||||||
o["Aggressiveness"] << Option(100, 0, 200, on_eval);
|
o["nodestime"] << Option(0, 0, 10000);
|
||||||
o["Cowardice"] << Option(100, 0, 200, on_eval);
|
o["UCI_Chess960"] << Option(false);
|
||||||
o["Min Split Depth"] << Option(0, 0, 12, on_threads);
|
o["SyzygyPath"] << Option("<empty>", on_tb_path);
|
||||||
o["Threads"] << Option(1, 1, MAX_THREADS, on_threads);
|
o["SyzygyProbeDepth"] << Option(1, 1, 100);
|
||||||
o["Hash"] << Option(32, 1, 16384, on_hash_size);
|
o["Syzygy50MoveRule"] << Option(true);
|
||||||
o["Clear Hash"] << Option(on_clear_hash);
|
o["SyzygyProbeLimit"] << Option(6, 0, 6);
|
||||||
o["Ponder"] << Option(true);
|
|
||||||
o["OwnBook"] << Option(false);
|
|
||||||
o["MultiPV"] << Option(1, 1, 500);
|
|
||||||
o["Skill Level"] << Option(20, 0, 20);
|
|
||||||
o["Emergency Move Horizon"] << Option(40, 0, 50);
|
|
||||||
o["Emergency Base Time"] << Option(60, 0, 30000);
|
|
||||||
o["Emergency Move Time"] << Option(30, 0, 5000);
|
|
||||||
o["Minimum Thinking Time"] << Option(20, 0, 5000);
|
|
||||||
o["Slow Mover"] << Option(80, 10, 1000);
|
|
||||||
o["UCI_Chess960"] << Option(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -92,11 +83,11 @@ void init(OptionsMap& o) {
|
|||||||
std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
|
std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
|
||||||
|
|
||||||
for (size_t idx = 0; idx < om.size(); ++idx)
|
for (size_t idx = 0; idx < om.size(); ++idx)
|
||||||
for (OptionsMap::const_iterator it = om.begin(); it != om.end(); ++it)
|
for (const auto& it : om)
|
||||||
if (it->second.idx == idx)
|
if (it.second.idx == idx)
|
||||||
{
|
{
|
||||||
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 != "button")
|
||||||
os << " default " << o.defaultValue;
|
os << " default " << o.defaultValue;
|
||||||
@@ -106,6 +97,7 @@ std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,12 +114,11 @@ Option::Option(OnChange f) : type("button"), min(0), max(0), on_change(f)
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
Option::Option(int v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
|
Option::Option(int v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
|
||||||
{ std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); }
|
{ defaultValue = currentValue = std::to_string(v); }
|
||||||
|
|
||||||
|
|
||||||
Option::operator int() const {
|
Option::operator int() const {
|
||||||
assert(type == "check" || type == "spin");
|
assert(type == "check" || type == "spin");
|
||||||
return (type == "spin" ? atoi(currentValue.c_str()) : currentValue == "true");
|
return (type == "spin" ? stoi(currentValue) : currentValue == "true");
|
||||||
}
|
}
|
||||||
|
|
||||||
Option::operator std::string() const {
|
Option::operator std::string() const {
|
||||||
@@ -157,7 +148,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" && (atoi(v.c_str()) < min || atoi(v.c_str()) > max)))
|
|| (type == "spin" && (stoi(v) < min || stoi(v) > max)))
|
||||||
return *this;
|
return *this;
|
||||||
|
|
||||||
if (type != "button")
|
if (type != "button")
|
||||||
|
|||||||
Reference in New Issue
Block a user