mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 12:07:43 +00:00
Compare commits
1268 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d120ee02e | |||
| f45eee318b | |||
| 8c2fd2170a | |||
| 91c2c44fb1 | |||
| 27e9fc1067 | |||
| e005270fb6 | |||
| 4f55ed14d3 | |||
| a6c0ba2100 | |||
| fc316cbca9 | |||
| 11b1a76f35 | |||
| bd8f463b7e | |||
| 4d14f97482 | |||
| 15616ad199 | |||
| 94a3608ab9 | |||
| 5769509d72 | |||
| fff6b9f061 | |||
| 56d2c3844a | |||
| 23b6809f3d | |||
| f31847302d | |||
| b1a4a18d63 | |||
| f7096ea7ce | |||
| a16ba5bbd1 | |||
| 408e6ee9b6 | |||
| 7b4f5c8f72 | |||
| 55948623e7 | |||
| 4d46d29efe | |||
| cc608a7aba | |||
| 1f40cd6d02 | |||
| 28bc8ed462 | |||
| 5ee16a180a | |||
| d30dfc084c | |||
| 6373e88b5b | |||
| 7487eb0dca | |||
| 2067a99c07 | |||
| 002062ae93 | |||
| 4064ee5406 | |||
| b0fd2b6b98 | |||
| d0bc951835 | |||
| 73131e7c78 | |||
| 1cc18d8a7a | |||
| c45c6d308f | |||
| f73bb438aa | |||
| 71dd8a333f | |||
| 9207baed65 | |||
| b191df5ebe | |||
| 3e95800814 | |||
| 2ed56f4d5f | |||
| 7b0b463720 | |||
| a6c5b4c6fb | |||
| 0504a6975d | |||
| a5b5a91512 | |||
| 1a8f63a896 | |||
| ee5514b8fd | |||
| 99e547f4cb | |||
| 110644d918 | |||
| 4b3a0fdab0 | |||
| 05e31c5e5f | |||
| 46fdb14b2f | |||
| cbb1a8ed31 | |||
| a6c5f60caa | |||
| 9518cc3254 | |||
| b0a177bc67 | |||
| 5b7b330616 | |||
| 3f64a2af6a | |||
| 9749f1f14c | |||
| 4ede49cd85 | |||
| 6960f41e03 | |||
| bf90499fc3 | |||
| 404c4122ce | |||
| 2a0bbb9faa | |||
| 128e097d03 | |||
| c1264e46d0 | |||
| 366f6b0dab | |||
| 58aee9a9ea | |||
| 4b703b1429 | |||
| 62d38f0196 | |||
| 7e575512ae | |||
| a55fb76dcc | |||
| 838255ef91 | |||
| 6fbe027da0 | |||
| ddcb572c41 | |||
| 12c0dfc113 | |||
| db53883f06 | |||
| 13ffb08136 | |||
| 2d82db1d14 | |||
| b88bc7b766 | |||
| e074a19f5c | |||
| b8930d0c26 | |||
| 4fc7734547 | |||
| 92dcbfa658 | |||
| b50eb6bea8 | |||
| 6f079ae720 | |||
| 203fdc9ac1 | |||
| 17d41b3861 | |||
| 8cff4862a6 | |||
| 908d98820b | |||
| a4c11b71ac | |||
| b2fadf32aa | |||
| 378bcfe760 | |||
| fe2ed42661 | |||
| e215a88cdd | |||
| e95e69515a | |||
| e05c80a088 | |||
| 7b31e81d77 | |||
| c0964fc70f | |||
| cd782c11ec | |||
| 5ea984ac35 | |||
| 1fd020a8ba | |||
| 02420d4670 | |||
| 2c7ab488a8 | |||
| d54e8a5955 | |||
| 4bebb15e94 | |||
| 3b8f66f8ac | |||
| b6e9d901b0 | |||
| 1b7223a53c | |||
| de1dc4f2de | |||
| a6e0f62a4f | |||
| 81e242a96d | |||
| dd1855eb2f | |||
| db4cd89cb8 | |||
| 7e95495b35 | |||
| 55eb7dd1e9 | |||
| 902c0566a6 | |||
| bc02cc0c8a | |||
| 05c6f7a40b | |||
| 2a98042c21 | |||
| 46409a7852 | |||
| d8b266af8b | |||
| abb40777bf | |||
| 90abcac1c7 | |||
| 7222f47350 | |||
| eafb66e1aa | |||
| d4a02b135d | |||
| d3608c4e79 | |||
| 38cfbeeb50 | |||
| 77547a4ef1 | |||
| 8ceef92266 | |||
| f7c013edd0 | |||
| 148490f04c | |||
| 7c6f346c90 | |||
| 1a34496761 | |||
| 049e5ca191 | |||
| 19dd0de4ff | |||
| 8a61b030a6 | |||
| c7e31d5aa8 | |||
| 818a3537a7 | |||
| bcbc9bfd1f | |||
| 7eda7335fd | |||
| 02606a8c83 | |||
| e7505324f6 | |||
| 653c0527a7 | |||
| 7f4c7cd785 | |||
| 0958e5c6d3 | |||
| 9fc77bc414 | |||
| 3b92159908 | |||
| e00bb13e85 | |||
| 3b41e62666 | |||
| 37c91aa94c | |||
| 43f67eab5f | |||
| 11d30b6298 | |||
| 3edb15d183 | |||
| d44ac0a485 | |||
| 481eda4ca0 | |||
| 7323231786 | |||
| e381951a24 | |||
| aa2368a687 | |||
| 293c44bc09 | |||
| 06b9140e5c | |||
| 156635749b | |||
| 083fe58124 | |||
| e508494a99 | |||
| 2ef53ee368 | |||
| d810441b35 | |||
| 79bcb2ca54 | |||
| 289a767ab3 | |||
| 4381ea23fe | |||
| 817ca1820b | |||
| 8df17204f4 | |||
| f84f04742a | |||
| cc40d1c46a | |||
| 818e0b2d2b | |||
| 3e4dfb49a7 | |||
| 87436e5544 | |||
| fe72c93141 | |||
| 75221fcf5e | |||
| a95cbca568 | |||
| d23454854e | |||
| c2902112e5 | |||
| 9498b2af82 | |||
| 2a5ae34bb2 | |||
| d3fe153fe6 | |||
| 889922041b | |||
| 7bad50773a | |||
| d3c3c4f8e7 | |||
| 2097cd1221 | |||
| 0c1b40c5e2 | |||
| 8c10029df1 | |||
| 6e2371a86b | |||
| 36c82b751c | |||
| 7d42d02ec7 | |||
| c89274d8fb | |||
| f2638816bf | |||
| 0b4ea54da9 | |||
| 09f1fdf52f | |||
| d39b22927e | |||
| 81e9cf043a | |||
| 077e32efc9 | |||
| 52f3e717fa | |||
| 0586b51f9c | |||
| d2eeef89aa | |||
| 70d20326b0 | |||
| ccf4ec6768 | |||
| a24da071f0 | |||
| 3698d9aa55 | |||
| 10429dd616 | |||
| db322e6a63 | |||
| 45dba12c5b | |||
| ccad601389 | |||
| d165d5af91 | |||
| 3ce43c20de | |||
| def50020ad | |||
| 0d68b523a3 | |||
| 0e1ad3ad33 | |||
| 0fc9a01933 | |||
| 68d1bebd8e | |||
| 57b6df4874 | |||
| bc38efd128 | |||
| 335b57b5ef | |||
| dbd28bc7f8 | |||
| ccf21f5595 | |||
| 1d1fcf80a0 | |||
| 373503f4a9 | |||
| 9a1d5f0f1d | |||
| d5e49a3ad4 | |||
| ea4e22be1d | |||
| dc8c6ea2d1 | |||
| c5ec94d0f1 | |||
| d570260a28 | |||
| 76caef8ba1 | |||
| 5a156df719 | |||
| e0dfb0bc34 | |||
| 3922ac2fe4 | |||
| 66a1a77487 | |||
| cd0ecace19 | |||
| 10d29add18 | |||
| f2950ae206 | |||
| d0c2faa5fd | |||
| 671f24ff35 | |||
| f32f899467 | |||
| 17b71fe51d | |||
| 30c2e3828a | |||
| a7cdf7299a | |||
| 733d0099b2 | |||
| 0be7b8c542 | |||
| c698362680 | |||
| fe3352665b | |||
| 3f44be9baa | |||
| 0c06b4509d | |||
| e5bc79fb9c | |||
| 880726c13a | |||
| 50a7200b18 | |||
| c67fb8ef04 | |||
| 14c2c1395b | |||
| 88c3670edf | |||
| f69c185e02 | |||
| bf706c4a4f | |||
| 1a414cd9cb | |||
| 91427c8242 | |||
| b8c5ea869c | |||
| 6560e4cc5b | |||
| 08d615cc95 | |||
| 1c4e6d7ea2 | |||
| 5f58db8c99 | |||
| ddbe6082c4 | |||
| 53051eefc7 | |||
| a72710c660 | |||
| 9f94d22801 | |||
| 0901e12102 | |||
| 58c9fbacc7 | |||
| 483c98a69e | |||
| 2218a5836a | |||
| 76a0d3c05a | |||
| 52cab06fff | |||
| bd87ab9ff5 | |||
| 57797822f8 | |||
| 7062db7cb2 | |||
| 166cc0292c | |||
| 496c7497cb | |||
| e83b9075ff | |||
| 6950d07bf4 | |||
| 054d117d25 | |||
| 62b32a4737 | |||
| 588670e8d2 | |||
| c465f4c4df | |||
| 8737b26a23 | |||
| 78a9531773 | |||
| d1143794a0 | |||
| e70eae2c91 | |||
| 99ae47716a | |||
| dda7de17e7 | |||
| 869c924410 | |||
| ea6c1f7a17 | |||
| 81cd7d787e | |||
| a523cea772 | |||
| a66a7c3870 | |||
| 89a89eb605 | |||
| 3b14b17664 | |||
| 3a836dab59 | |||
| e1191b35e8 | |||
| edce2a8448 | |||
| 9b1cf3cf43 | |||
| bff65a211f | |||
| 2d60995c00 | |||
| 0454bbc54f | |||
| 900e2d4e1e | |||
| 9d1151575d | |||
| 193741218c | |||
| 896420b166 | |||
| 009a0f88e0 | |||
| e1d681458e | |||
| ce063f59cd | |||
| 3cf6471738 | |||
| f78b68b7ff | |||
| e9ab7353de | |||
| 9d1978e217 | |||
| db097921bc | |||
| 894c43a1d6 | |||
| b5b799b5ab | |||
| e82382703c | |||
| 3b49aeb4f2 | |||
| 423c6d8a8a | |||
| 158014b39d | |||
| 231f62baf7 | |||
| 52bbf372bb | |||
| 3ddf91d9d1 | |||
| a2f46446cf | |||
| 9edc7d6958 | |||
| 55db871472 | |||
| da98a45bcb | |||
| feeafb0a50 | |||
| 22c557ca7c | |||
| 6a93488291 | |||
| 6fa83f5188 | |||
| ce248e7920 | |||
| 23bdd06442 | |||
| 98cd8239cc | |||
| 2a585b63b8 | |||
| a5ea3a202e | |||
| 69de670353 | |||
| 4e5d834e8e | |||
| 4502917665 | |||
| 5af8179647 | |||
| 55df3fa2d7 | |||
| 8367cf15da | |||
| 942989939a | |||
| 239d7b3fd1 | |||
| 116234d6c6 | |||
| c45a4e0b48 | |||
| 96d3b1c92b | |||
| 3b87314331 | |||
| c581b7ea36 | |||
| a5b1f4774f | |||
| 37e9802411 | |||
| a87831230d | |||
| dd5b3086f5 | |||
| 47f988f05f | |||
| 07989712af | |||
| d0d69a5358 | |||
| 972de506a0 | |||
| ed1574e46c | |||
| 52f55179a8 | |||
| bbdf9e4737 | |||
| 94ecdef8ac | |||
| e3b0327812 | |||
| c039103b31 | |||
| fe1cbe2638 | |||
| 4e31c39a64 | |||
| 13f90a30ef | |||
| ad1941fd00 | |||
| cee6336515 | |||
| b3d030294b | |||
| cd80762c13 | |||
| 00a853950f | |||
| 4c7a71a44b | |||
| c594b989c0 | |||
| 71f37ac1aa | |||
| 77c91ac1ba | |||
| 5436d98fc5 | |||
| 0587c5b605 | |||
| c1f4000426 | |||
| 22715259a0 | |||
| 4eda653a56 | |||
| e8e5b9f537 | |||
| 9c2b3faec4 | |||
| 70b1b79264 | |||
| 5fc8b27db9 | |||
| dbbbd3880c | |||
| e40b06a050 | |||
| e304db9d1e | |||
| 6b909b2343 | |||
| 55bd27b8f0 | |||
| 1018474853 | |||
| c11edd3b0b | |||
| f0298862f5 | |||
| 739d23f2a3 | |||
| 3aa2d6db18 | |||
| 6a75291ab1 | |||
| eb1a4f11fa | |||
| d023be5a21 | |||
| c2cd75843e | |||
| 189b6fc270 | |||
| 6c8663341e | |||
| aba152ea3a | |||
| 7f9ebf8e86 | |||
| 4e7da9be3d | |||
| 46c01b5083 | |||
| dae843d4d6 | |||
| 78fe0cfb8d | |||
| d77d555c72 | |||
| bbaef048cd | |||
| 7e2d49368d | |||
| 954fc950d9 | |||
| d777c4d789 | |||
| cedbd3332a | |||
| ff9ca3e76e | |||
| b8948e84b8 | |||
| 1ac417edb8 | |||
| d471c49700 | |||
| c9f9262a49 | |||
| bd7a0d4ce4 | |||
| 561eb34aea | |||
| 4c91dbc28e | |||
| 1b6b711c44 | |||
| faaa1f1116 | |||
| ed0fb0b05f | |||
| e5463eb3ae | |||
| d53c928261 | |||
| bc8f5fe0bf | |||
| 9204a60dbb | |||
| 1cb2722c95 | |||
| 3caeabf73b | |||
| fdbe8006e0 | |||
| 09acdac56b | |||
| e4a0482e43 | |||
| 22a5f91aa7 | |||
| 9ce7469846 | |||
| 4b7dbb3922 | |||
| e0035e9ca9 | |||
| 0a18adb02a | |||
| 630b3b2482 | |||
| 6008f6538e | |||
| afcee1e8a4 | |||
| e0bd0f250b | |||
| 1598a3edf8 | |||
| 0dacab65eb | |||
| 834bd9edd7 | |||
| 6e840f8033 | |||
| 2379312028 | |||
| 37db62b2ea | |||
| b50ce5ebfb | |||
| 5900ab76a0 | |||
| e6d8e74152 | |||
| 9cdca7516c | |||
| 6436e75858 | |||
| 831f91b859 | |||
| 1258c7aabe | |||
| 8dcb4bc3cc | |||
| 0a003d3ba1 | |||
| 92e759a676 | |||
| 3df2c01b57 | |||
| 8991a6f005 | |||
| 16f380e5c1 | |||
| cbd7ce468c | |||
| b6883c872d | |||
| fde0b9e701 | |||
| 7a2825053e | |||
| 4e619a13d6 | |||
| 0de9257610 | |||
| b84af67f4c | |||
| 7c8b7222f5 | |||
| ec9038b7b4 | |||
| ab65d3fd0e | |||
| e8b7109eff | |||
| 0c6ed5929c | |||
| 2f4a9a140a | |||
| dba1bc354a | |||
| 4b19430103 | |||
| 7c1f8dbde9 | |||
| 2c1ba2ab0d | |||
| b011818917 | |||
| bc4de9edae | |||
| 9de4ee6d32 | |||
| 90ec4a403a | |||
| b61ec33f22 | |||
| 1f7b5d9a79 | |||
| 5c6ba81cf2 | |||
| 591adf564a | |||
| f0db6a6c0b | |||
| 520e680278 | |||
| abc6a0be2f | |||
| 9dbda6652e | |||
| 5dc0df8435 | |||
| 6e5a334c95 | |||
| 6becc81446 | |||
| 6b5322ce00 | |||
| 67d91dfd50 | |||
| 089e54c7fd | |||
| 0b3ffb54b7 | |||
| 775488340e | |||
| 7d2530873e | |||
| 18505f1fc4 | |||
| dc88cd691f | |||
| ebe8009aff | |||
| 628808a113 | |||
| dc7fd868f4 | |||
| 7b4aa10708 | |||
| 960a689769 | |||
| 5f5d056c8f | |||
| 9c7e2c8f9d | |||
| 4f5680950a | |||
| 9793fa1906 | |||
| 0446fc85de | |||
| 764d3f44b6 | |||
| 0412f4a1ee | |||
| 6828325881 | |||
| 61a054b170 | |||
| 3361ad4242 | |||
| 6c9c6dd989 | |||
| a358dfe934 | |||
| 0ecc920a09 | |||
| f86182e791 | |||
| c19a6ef82d | |||
| ecb84464f9 | |||
| 1b2af05ea6 | |||
| caef319219 | |||
| 2f47844c7c | |||
| b9bc6e823f | |||
| 5e90580088 | |||
| ef0496ff40 | |||
| 96f4ab48d8 | |||
| 39c08c17c5 | |||
| b93693b831 | |||
| cdfe43eb8f | |||
| 456f37b8ab | |||
| e8d89ca5b0 | |||
| 2aac860db3 | |||
| 8b00e50cb7 | |||
| be3b8f3ae9 | |||
| ce159b16b9 | |||
| 5392007a24 | |||
| f59323b56a | |||
| 25a9b601b2 | |||
| c645aca199 | |||
| 44432f67d7 | |||
| d66b765eb6 | |||
| e72b93e44f | |||
| 68885f78f3 | |||
| 4a310baae2 | |||
| d549497144 | |||
| e56342ed00 | |||
| 0049d3f337 | |||
| e0cae4bef8 | |||
| 9546b79e20 | |||
| cc04a745e2 | |||
| ce5b972736 | |||
| f30f384757 | |||
| 676b2c8435 | |||
| c2fc80e5d1 | |||
| b1f57e92ce | |||
| bed4075580 | |||
| 5a2d525048 | |||
| e1919384a2 | |||
| 699f700162 | |||
| 797c960d20 | |||
| 673bc5526f | |||
| 0439a79566 | |||
| 37fa8adc2b | |||
| 2f99de0c6c | |||
| 7a8429d9f1 | |||
| dda0fa1a43 | |||
| 7eb6a488ad | |||
| 72641dcaae | |||
| 6e00aa6bae | |||
| 9bbd27a80f | |||
| adb71b8096 | |||
| 32c504076f | |||
| 304deb5e83 | |||
| d84865eac3 | |||
| cc6c745b54 | |||
| d033d5e06a | |||
| b0b9bb3462 | |||
| 5e18b81e87 | |||
| 10e64e0509 | |||
| 76622342ec | |||
| d6e3a40c81 | |||
| 46a50cbf38 | |||
| cc2b3ece5c | |||
| 22e294e044 | |||
| 4124c94583 | |||
| a56322fde8 | |||
| 3d0d0237c5 | |||
| d11a529904 | |||
| 3aa471f2a9 | |||
| 374c9e6b63 | |||
| 32d3a07c67 | |||
| b978eb05dc | |||
| e4efc8b741 | |||
| 58c2fe391d | |||
| dbe5e28eaa | |||
| c483ffc773 | |||
| 41561c9bb8 | |||
| 553655eb07 | |||
| f01b53c374 | |||
| 8ec421fa14 | |||
| 11b0c7b44a | |||
| f8224fc7d3 | |||
| b356e0fae3 | |||
| 06f33ff1ee | |||
| c47a74ec62 | |||
| e26d13bb31 | |||
| f41a21fefd | |||
| d4c9abb967 | |||
| d1e18fc7dd | |||
| 3c6a4bfbed | |||
| 3b7dbc4f6d | |||
| 17d1940278 | |||
| 258da28e79 | |||
| df80232495 | |||
| eb28a683bd | |||
| 2dfc94e0b6 | |||
| 7bc3688714 | |||
| fc3ea7365a | |||
| 55376219b7 | |||
| 9b26356347 | |||
| 7c8a8e038f | |||
| ee838f56f7 | |||
| 9934b8ec31 | |||
| 3dccdf5b83 | |||
| 4220f191d8 | |||
| 843a5961e1 | |||
| d8e56cbe54 | |||
| 2ef5b4066e | |||
| 482b5b7ece | |||
| 19540c9ee8 | |||
| cf0561d31a | |||
| 161c6b025e | |||
| b966adf103 | |||
| c2a68708ef | |||
| 34178205fc | |||
| 5bb766e826 | |||
| 96eefc4af6 | |||
| 8751b18cf0 | |||
| 2608b9249d | |||
| 43f84efa15 | |||
| ea5616785e | |||
| 6a48325c49 | |||
| 50edb7cd73 | |||
| 4aadd1e401 | |||
| ec5b9994b5 | |||
| 3441e0075d | |||
| 821e1c7233 | |||
| 6088ac2108 | |||
| d8349f9d0f | |||
| fd35d92c1e | |||
| 3b906ffc27 | |||
| 7b4b65d7a9 | |||
| 099b5e45e6 | |||
| b1768c115c | |||
| 86e159997c | |||
| 576f0f6985 | |||
| b46bf2950f | |||
| 0347339970 | |||
| 40e939421f | |||
| b1cf1acb93 | |||
| c94cfebb7e | |||
| 57e942145c | |||
| 51e8efdab5 | |||
| df31398bb9 | |||
| a94fd3bbec | |||
| 1a1742ac4f | |||
| a492a9dd07 | |||
| 875a8079bc | |||
| b76c04c097 | |||
| f7b4983137 | |||
| eeef654cd7 | |||
| 7fb6fd2f55 | |||
| a189a5f0c5 | |||
| 24b25b4827 | |||
| 830ff985db | |||
| 3d937e1e90 | |||
| 04ff9c2548 | |||
| 28bf56e725 | |||
| 662d1859bd | |||
| 97e0b0a01e | |||
| 04ac1bcabe | |||
| ec9b037e5f | |||
| 6f6be95bad | |||
| b96db269a8 | |||
| dcbb05ef39 | |||
| e25de55fac | |||
| 28892666bd | |||
| 6058e7cf60 | |||
| 2a21543c88 | |||
| 007613cb5e | |||
| 1b69ef8e6c | |||
| 972dec454c | |||
| b6b8c62ba5 | |||
| db57b5f8f4 | |||
| 4a4513d126 | |||
| 2b1324eddb | |||
| dfd030b67a | |||
| 20621ed3e3 | |||
| 2aec8fb956 | |||
| 3ec94abcdb | |||
| e1907e349f | |||
| f237e8b8ea | |||
| 103b368ab7 | |||
| a29dd88f75 | |||
| a1076cc68a | |||
| 78908b7aed | |||
| d98150dffc | |||
| c549f71f64 | |||
| c19ea4b000 | |||
| bede30e7a6 | |||
| b05fbb3733 | |||
| 9b43fd7937 | |||
| 87fc9dcaa3 | |||
| 83f3ea7ab4 | |||
| 9c8c4ff46f | |||
| d655147e8c | |||
| 3c675db3d0 | |||
| cf247e7e30 | |||
| 0026c88b3a | |||
| 6482ce2bb2 | |||
| 867a5a8cd2 | |||
| 78e6b361c5 | |||
| aa392c366e | |||
| f80c50bcdd | |||
| d282cf6964 | |||
| b1fcfe4c5d | |||
| 30418a3cfc | |||
| cb1709ef5e | |||
| 67338e6f32 | |||
| 8300ab149c | |||
| c00443b19e | |||
| 9db9e4f7d3 | |||
| 22e40c8c10 | |||
| e9296d694c | |||
| fb4b4f772e | |||
| f4dadee5e2 | |||
| 808a312e1c | |||
| 93e539d909 | |||
| 9d7a36121a | |||
| 8307da0de7 | |||
| ad43ce1436 | |||
| 0a6532a39d | |||
| 20a6f99cdb | |||
| 4554d8b2ac | |||
| 9cb187762a | |||
| 4a8c1b2470 | |||
| 0759d8f430 | |||
| ae65ab25d5 | |||
| 24417a6cd9 | |||
| ad4739a6d4 | |||
| a695ed65a8 | |||
| 750ac9ac50 | |||
| 07f3f0384a | |||
| 87b483f999 | |||
| c2d42ea833 | |||
| b5f6c2241b | |||
| e89a8e0913 | |||
| d543a64cc7 | |||
| 939b621e5c | |||
| a77a3b723f | |||
| 976270916b | |||
| 87f7e99bc4 | |||
| 72d8d27234 | |||
| 1ae6ae9b60 | |||
| af4fadebda | |||
| 0af3af5d25 | |||
| 852d45a600 | |||
| 96213689e4 | |||
| 4e59c5c274 | |||
| 7d97ebfe7f | |||
| 377c406c74 | |||
| 91601d7f95 | |||
| f3728e66f8 | |||
| 3f14ed6602 | |||
| 14df99130f | |||
| 47bcb892af | |||
| 6e05055f06 | |||
| da6e53a436 | |||
| 56774fff20 | |||
| 3527a42063 | |||
| 98352a5e84 | |||
| 11a7980976 | |||
| 5b8ca1eee7 | |||
| 81cd417b45 | |||
| a44c5cf4f7 | |||
| 5c5af4fa65 | |||
| efd2167998 | |||
| 348f824104 | |||
| 0f7cbaca75 | |||
| e870afa5d5 | |||
| 9bacd921fa | |||
| bb3427ca85 | |||
| 6809b57cfc | |||
| ffa75215cc | |||
| c4517c013c | |||
| ed04c010eb | |||
| e9dc2e9e1e | |||
| 9c7d72739c | |||
| 3fc08f8ab6 | |||
| c56a7ee803 | |||
| 35fa4fc71f | |||
| b56a098cfb | |||
| 4cc272cb94 | |||
| c4fc82c6b7 | |||
| a40ded2884 | |||
| 44c78fdb7a | |||
| ecb98a3330 | |||
| 9b6b510ca8 | |||
| c2600a73cf | |||
| 43204d9ac2 | |||
| 369789b426 | |||
| 8fa53a5b92 | |||
| d58176bfea | |||
| 0095f423f2 | |||
| 2617aa415e | |||
| 22b9307aba | |||
| ac7339877b | |||
| 90890844ad | |||
| 8a89b12641 | |||
| 4a3b162c8c | |||
| 5c8af7ccb8 | |||
| 7942e6f3bf | |||
| c6f497f09d | |||
| 29be28e1a2 | |||
| e7939f450f | |||
| 81801d395f | |||
| bc76c62c63 | |||
| fd5d6c5340 | |||
| 08abe8b4a3 | |||
| e896368496 | |||
| 2fe4e10b0b | |||
| f2e78d9f84 | |||
| b5bbc1f713 | |||
| 23943208ec | |||
| c555e1aa96 | |||
| 36cc214703 | |||
| 8f7d36a85d | |||
| e7cfe42d3f | |||
| 500fff920b | |||
| 782c3f36cc | |||
| 8272dcb6cd | |||
| fec623d68d | |||
| a3bf09c5c9 | |||
| 325dedc7da | |||
| 09b5949cd2 | |||
| 3141490374 | |||
| 1a8e3f0b2e | |||
| 80dd90f972 | |||
| c2c185423b | |||
| 25b4d0c127 | |||
| 2225e3bbe7 | |||
| a90a990118 | |||
| 83d8fe2d59 | |||
| 63a04134d0 | |||
| 6bc16f3ff1 | |||
| 10b24af98a | |||
| be9aba2fa0 | |||
| 35018fa307 | |||
| b706165527 | |||
| 6963c3802d | |||
| 3b67636f0e | |||
| 48e39c5c8e | |||
| 13524bea9b | |||
| 500c7f44ab | |||
| d156e7a20b | |||
| c386ce0023 | |||
| b69d9ee3f7 | |||
| 7d5b8fcf77 | |||
| bc6a6e04a0 | |||
| e5ffe9959c | |||
| 86b95f2105 | |||
| 1e92df6b20 | |||
| ba85c59d96 | |||
| dafd5b5864 | |||
| eabba1119f | |||
| 06e0d48794 | |||
| 4a71c86270 | |||
| 68583f4d48 | |||
| 5b35c149e8 | |||
| 9a542d9698 | |||
| cd27ed9a74 | |||
| b9f8cb7837 | |||
| ffa150bec3 | |||
| 13bc6ba2c6 | |||
| 30b3edf328 | |||
| 7902d6089e | |||
| b6b0878da8 | |||
| 3a76163aba | |||
| 1e7c6fc761 | |||
| c50ad85c3c | |||
| bb6a6e159a | |||
| 1036cadcec | |||
| c71ae794df | |||
| 409930e98c | |||
| b88f7df387 | |||
| adcfffceeb | |||
| a3c1d64a5f | |||
| ce24a229df | |||
| ce619b3b6c | |||
| 2e2a4b4ea3 | |||
| 7f519a2463 | |||
| ff1ecb5d6c | |||
| dab1cd8af9 | |||
| 5b0c6b9bc0 | |||
| fc290dc30b | |||
| 5f7eb20090 | |||
| 03ad183384 | |||
| 527a2ec541 | |||
| 3185c36a65 | |||
| 3d8140a541 | |||
| a6fc3d6ee5 | |||
| a1f9bf19d9 | |||
| 4ad6a3496b | |||
| 969ad8001c | |||
| 07e0dd27fb | |||
| b8eb699db7 | |||
| ad1f28bc1c | |||
| 67686b7684 | |||
| 69f4954df1 | |||
| 4894231ff7 | |||
| 36bb57a47e | |||
| a042449f5d | |||
| f5e434c2d9 | |||
| 37d551fb39 | |||
| 9c8c0de538 | |||
| fbdabe2975 | |||
| 0cfb30e5be | |||
| c081a81daf | |||
| 155bed18f5 | |||
| 95d9687d95 | |||
| ff41b8df76 | |||
| 53ccba8457 | |||
| 7ac6e3b850 | |||
| d15217b953 | |||
| bc54a44010 | |||
| 3f806df0ca | |||
| f3799aa750 | |||
| 30ca6935a5 | |||
| f25582d4b8 | |||
| fb2fdb21d3 | |||
| 31a0d2200c | |||
| e0a00c4996 | |||
| e5077dc11e | |||
| 01a191936e | |||
| 8094b2add8 | |||
| ffb638995d | |||
| c2a4856f9e | |||
| c9b24c3358 | |||
| 9305983018 | |||
| ae2f5f25cd | |||
| 923b14afaa | |||
| a9782b94e6 | |||
| 351ef5c85b | |||
| be2925b3c5 | |||
| ace27831ee | |||
| 6a1707889c | |||
| 1c42d15340 | |||
| fe26967ea0 | |||
| e7413417ce | |||
| 018022b866 | |||
| 7ea38e980b | |||
| 8173d92dd2 | |||
| 808a4fe817 | |||
| 72760c05c6 | |||
| 15683034a7 | |||
| 4c9d570e43 | |||
| fc519ca74a | |||
| a24c0a736c | |||
| b3a0b389d2 | |||
| 47959c56fd | |||
| aee75ae105 | |||
| 4b8a7f2793 | |||
| 89ec224cb9 | |||
| b21a5e2f06 | |||
| 735cac5d53 | |||
| bc4f3155ae | |||
| a21a110188 | |||
| b414fc0dfd | |||
| e0215f3222 | |||
| 3dfff5bdae | |||
| d632e77058 | |||
| 6d665b7f78 | |||
| b1b0c64046 | |||
| 670cee44f0 | |||
| 3092f0c646 | |||
| 2f6142cb9b | |||
| 3b2bcee0a8 | |||
| 91407f4f74 | |||
| 025d57855a | |||
| 8647fbd6ed | |||
| 811037c845 | |||
| 181cc3f93f | |||
| 91b919fd1d | |||
| 5ff6289afd | |||
| fca0a2dd88 | |||
| 462a39ec49 | |||
| 70125a3be0 | |||
| cff8877a1a | |||
| 8f51f09de7 | |||
| 013dc43d5d | |||
| 853e2a9495 | |||
| 4c3a000211 | |||
| 9884573561 | |||
| 4b232f5ddc | |||
| 45ce92b89c | |||
| bc86668ba4 | |||
| 0783211950 | |||
| 13fe7ee4df | |||
| 21fc66c246 | |||
| ff9e49bac9 | |||
| 90e83fa879 | |||
| 3ef4fdeaa0 | |||
| 13d8af1852 | |||
| 5b7a141065 | |||
| 89a06f6651 | |||
| a2e924039b | |||
| 85d1f9c5ec | |||
| e444e18d2b | |||
| ad0fdf0da6 | |||
| fbbc7e421c | |||
| 1cc42ac9e4 | |||
| 6624105b5b | |||
| de58594b0f | |||
| d5f0b91c06 | |||
| f78488bbdb | |||
| e3b23eb818 | |||
| d494725400 | |||
| 2535dc1340 | |||
| 339bf9b7e1 | |||
| bb86691a0d | |||
| f7fee4c616 | |||
| 315c58354e | |||
| 5ef2b8c494 | |||
| 9c5a53ca45 | |||
| b1929960f9 | |||
| 8447248705 | |||
| a10487b074 | |||
| db31efb8a6 | |||
| 75acd52415 | |||
| 18c9b5ee86 | |||
| 33bd67e052 | |||
| 4dc7ba1619 | |||
| 92d70fb667 | |||
| 0bf475ec55 | |||
| bb7713c8e9 | |||
| e656ddcf18 | |||
| 1d0159075e | |||
| 321320b081 | |||
| ca9d40c145 | |||
| 03f4d1e8d6 | |||
| 87f2b52ace | |||
| 09d01ee9dc | |||
| 05cfb00f26 | |||
| 339e1b49f6 | |||
| fecefbb99c | |||
| c9d7e99de6 | |||
| ccd5ccbcdb | |||
| 633c83f648 | |||
| fe213d30fa | |||
| 8b2adcf99e | |||
| 1d368bbbdc | |||
| bbfe452f85 | |||
| 8bf9dc8254 | |||
| f349143a6b | |||
| 79e50a2fbf | |||
| 2c317d7b28 | |||
| fdc9f8cbd7 | |||
| 2e6839c9a0 | |||
| 67b0d0b1cc | |||
| 068561f86a | |||
| 076b62310e | |||
| 5ba85ef441 | |||
| c13b53a514 | |||
| a860576493 | |||
| 786564068b | |||
| 6056a43419 | |||
| f8e767388b | |||
| b41b590457 | |||
| 7f367e6019 | |||
| 54f1c383d3 | |||
| fe8f5b3497 | |||
| 6608a16a6a | |||
| b5d5646c84 | |||
| 08c464c690 | |||
| f28ddbb852 | |||
| c88eebc989 | |||
| 7a84b8ca34 | |||
| 6738b65be9 | |||
| 2f1935078d | |||
| 8e71ee7ec6 | |||
| 9bee5f51d8 | |||
| 927f1b0bd3 | |||
| 04108d4541 | |||
| f7ef48b478 | |||
| 2e8998eac9 | |||
| 8402b40341 | |||
| 408fdcc93f | |||
| b27e237b04 | |||
| f5b8db7a1e | |||
| 8d4caebabe | |||
| d173285da5 | |||
| 41fe70d703 | |||
| d372f2e39a | |||
| 4270aec558 | |||
| f427acbd27 | |||
| 87ca13a79a | |||
| d52d91064f | |||
| 920b1abede | |||
| 5ea08e79c4 | |||
| 635be39acf | |||
| c1c0984452 | |||
| d653aeca05 | |||
| 09d217bff7 | |||
| d9113d127b | |||
| c2511243b4 | |||
| d85cf6d9c3 | |||
| a617b03875 | |||
| c980163316 | |||
| 76506bd3d1 | |||
| 9069193125 | |||
| 0c775fae4b | |||
| 1f73a9ed63 | |||
| af236373cb | |||
| ce2845d333 | |||
| 204efb109b | |||
| 91a2766308 | |||
| bd33766da0 | |||
| dd718d92a7 | |||
| 23cbb221b2 | |||
| 0fcda095df | |||
| 95212222c7 | |||
| 2ff2b59727 | |||
| 5b2ac7590c | |||
| 2511386155 | |||
| deb212cb05 | |||
| aa40d0a917 | |||
| b3108547de | |||
| 706b44a966 | |||
| 823a5918e7 | |||
| 932ae761c6 | |||
| 29fa6f3c5f | |||
| 5815718177 | |||
| 6a19f5832a | |||
| 4ead60e2a7 | |||
| 2786aed195 | |||
| 876ceb1feb | |||
| 29076043e0 | |||
| aa84731fb9 | |||
| c006435bb4 | |||
| 141410f177 | |||
| 3abff79df3 | |||
| 62c707e1d5 | |||
| 26e7673c18 | |||
| b366c7dc38 | |||
| 2f6788104f | |||
| 69726f4df3 | |||
| f3e0b32def | |||
| 188700d7f1 | |||
| 9ba7f701ea | |||
| afae86bfb4 | |||
| f352008958 | |||
| 6849f0800e | |||
| 5194b2bb06 | |||
| a8f457d425 | |||
| 79b1a7417f | |||
| b67de36671 | |||
| 59c85346d2 | |||
| e8f885145b | |||
| 324ca87aff | |||
| 4f3fe89fb6 | |||
| 45acec1865 | |||
| a38b14bd33 | |||
| d91d6da3c4 | |||
| 5bad5fc0a7 | |||
| 6119e4ea37 | |||
| 5555b60b38 | |||
| 846087e4fb | |||
| 2e2f1064ba | |||
| 392c7f2ab6 | |||
| 6b8026806c | |||
| 299bda9ea3 | |||
| 998845763a | |||
| 842efefcad | |||
| c17a127c42 | |||
| e06c99cad0 | |||
| 04001f776e | |||
| 877b468e3e | |||
| 13a42284b6 | |||
| 3bb1ab34e4 | |||
| 7faeab0878 | |||
| 2052407090 | |||
| d6fdd4f6d9 | |||
| b85bcc039c | |||
| 62cd133b3a | |||
| 6442981cb6 | |||
| d4b92ae9a0 | |||
| 611a29f767 | |||
| d84ffc0cfa | |||
| caa02b0e43 | |||
| a8741bd59f | |||
| 06c14d0a37 | |||
| 15153a1de7 | |||
| c89762288b | |||
| b4acf83704 | |||
| ee0afea1e5 | |||
| c82906a2c3 | |||
| 6b49d509a1 | |||
| 82d5386435 | |||
| f4b4d4901c | |||
| 8d86e95e37 | |||
| 0f81f97bb6 | |||
| d9b96f0e49 | |||
| 4fa0395eb8 | |||
| b3545737fa | |||
| 44fbbeafc9 | |||
| 57b3ca916f | |||
| 12f4bbc8f2 | |||
| 1e7aaed8bc | |||
| a46b53e1c2 | |||
| dcbc8a7e75 | |||
| 0ddf84870a | |||
| 803c8e0be3 | |||
| fca74b1882 | |||
| b08ba446f6 | |||
| 1a20d72701 | |||
| 916c0cbfbc | |||
| 2e46db4369 | |||
| 5fc8f86a4f | |||
| c14dae1fa2 | |||
| d4ded09e17 | |||
| 631fa6a100 | |||
| dadf6a6fe9 | |||
| 6a5dc14251 | |||
| 55b16593a4 | |||
| 7614501362 | |||
| 22ca7a601f | |||
| 97212bafc9 | |||
| 07fcc8076d | |||
| be5b32bb9c | |||
| 9da1f45b1d | |||
| 096351d1f5 | |||
| 6b96e6f33d | |||
| f200f3ccd2 | |||
| 2bb555025f | |||
| d5f2e32b5c | |||
| 83d8d54216 | |||
| 078354060e | |||
| 5973e09854 | |||
| 22ede4442c | |||
| deee18c758 | |||
| 0693ff178e | |||
| 57c51dd1ef | |||
| f902ddaa89 | |||
| 24485df3df |
@@ -0,0 +1,70 @@
|
||||
### Overview
|
||||
|
||||
Stockfish is a free UCI chess engine derived from Glaurung 2.1. It is
|
||||
not a complete chess program and requires some UCI-compatible GUI
|
||||
(e.g. XBoard with PolyGlot, eboard, Arena, Sigma Chess, Shredder, Chess
|
||||
Partner or Fritz) in order to be used comfortably. Read the
|
||||
documentation for your GUI of choice for information about how to use
|
||||
Stockfish with it.
|
||||
|
||||
This version of Stockfish supports up to 64 CPUs. The engine defaults
|
||||
to one search thread it is therefore recommended to inspect the value of
|
||||
the *Threads* UCI parameter, and to make sure it equals the number of CPU
|
||||
cores on your computer.
|
||||
|
||||
|
||||
### Files
|
||||
|
||||
This distribution of Stockfish consists of the following files:
|
||||
|
||||
* Readme.md, the file you are currently reading.
|
||||
|
||||
* Copying.txt, a text file containing the GNU General Public License.
|
||||
|
||||
* src, a subdirectory containing the full source code, including a Makefile
|
||||
that can be used to compile Stockfish on Unix-like systems. For further
|
||||
information about how to compile Stockfish yourself read section below.
|
||||
|
||||
* polyglot.ini, for using Stockfish with Fabien Letouzey's PolyGlot
|
||||
adapter.
|
||||
|
||||
|
||||
### Opening books
|
||||
|
||||
This version of Stockfish has support for PolyGlot opening books. For
|
||||
information about how to create such books, consult the PolyGlot
|
||||
documentation. The book file can be selected by setting the *Book File*
|
||||
UCI parameter.
|
||||
|
||||
|
||||
### Compiling it yourself
|
||||
|
||||
On Unix-like systems, it should be possible to compile Stockfish
|
||||
directly from the source code with the included Makefile.
|
||||
|
||||
Stockfish has support for 32 or 64-bit CPUs, the hardware POPCNT
|
||||
instruction, big-endian machines such as Power PC, and other platforms.
|
||||
|
||||
In general it is recommended to run `make help` to see a list of make
|
||||
targets with corresponding descriptions. When not using Makefile to
|
||||
compile (for instance with Microsoft MSVC) you need to manually
|
||||
set/unset some switches in the compiler command line; see file *types.h*
|
||||
for a quick reference.
|
||||
|
||||
|
||||
### Terms of use
|
||||
|
||||
Stockfish is free, and distributed under the **GNU General Public License**
|
||||
(GPL). Essentially, this means that you are free to do almost exactly
|
||||
what you want with the program, including distributing it among your
|
||||
friends, making it available for download from your web site, selling
|
||||
it (either by itself or as part of some bigger software package), or
|
||||
using it as the starting point for a software project of your own.
|
||||
|
||||
The only real limitation is that whenever you distribute Stockfish in
|
||||
some way, you must always include the full source code, or a pointer
|
||||
to where the source code can be found. If you make any changes to the
|
||||
source code, these changes must also be made available under the GPL.
|
||||
|
||||
For full details, read the copy of the GPL found in the file named
|
||||
*Copying.txt*
|
||||
-85
@@ -1,85 +0,0 @@
|
||||
1. Introduction
|
||||
---------------
|
||||
|
||||
Stockfish is a free UCI chess engine derived from Glaurung 2.1. It is
|
||||
not a complete chess program, but requires some UCI compatible GUI
|
||||
(like XBoard with PolyGlot, eboard, Josè, Arena, Sigma Chess, Shredder,
|
||||
Chess Partner, or Fritz) in order to be used comfortably. Read the
|
||||
documentation for your GUI of choice for information about how to use
|
||||
Stockfish with your GUI.
|
||||
|
||||
This version of Stockfish supports up to 8 CPUs, but has not been
|
||||
tested thoroughly with more than 2. The program tries to detect the
|
||||
number of CPUs on your computer and set the number of search threads
|
||||
accordingly, but please be aware that the detection is not always
|
||||
correct. It is therefore recommended to inspect the value of the
|
||||
"Threads" UCI parameter, and to make sure it equals the number of CPU
|
||||
cores on your computer. If you are using more than four threads, it
|
||||
is recommended to raise the value of "Minimum Split Depth" UCI parameter
|
||||
to 6.
|
||||
|
||||
|
||||
2. Files
|
||||
--------
|
||||
|
||||
This distribution of Stockfish consists of the following files:
|
||||
|
||||
* Readme.txt, the file you are currently reading.
|
||||
|
||||
* Copying.txt, a text file containing the GNU General Public
|
||||
License.
|
||||
|
||||
* src/, a subdirectory containing the full source code, including a
|
||||
Makefile that can be used to compile Stockfish on Unix-like
|
||||
systems. For further information about how to compile Stockfish
|
||||
yourself, read section 4 below.
|
||||
|
||||
* polyglot.ini, for using Stockfish with Fabien Letouzey's PolyGlot
|
||||
adapter.
|
||||
|
||||
|
||||
3. Opening books
|
||||
----------------
|
||||
|
||||
This version of Stockfish has experimental support for PolyGlot opening
|
||||
books. For information about how to create such books, consult the
|
||||
PolyGlot documentation. The book file can be selected by setting the
|
||||
UCI parameter "Book File".
|
||||
|
||||
|
||||
4. Compiling it yourself
|
||||
------------------------
|
||||
|
||||
On Unix-like systems, it should usually be possible to compile
|
||||
Stockfish directly from the source code with the included Makefile.
|
||||
|
||||
For big-endian machines like Power PC you need to enable the proper
|
||||
flag changing from -DNBIGENDIAN to -DBIGENDIAN in the Makefile.
|
||||
|
||||
Stockfish has POPCNT instruction runtime detection and support. This can
|
||||
give an extra speed on Core i7 or similar systems. To enable this feature
|
||||
compile with 'make icc-profile-popcnt'
|
||||
|
||||
On 64 bit Unix-like systems the 'bsfq' assembly instruction will be used
|
||||
for bit counting. Detection is automatic at compile time, but in case you
|
||||
experience compile problems you can comment out #define USE_BSFQ line in types.h
|
||||
|
||||
|
||||
5. Terms of use
|
||||
---------------
|
||||
|
||||
Stockfish is free, and distributed under the GNU General Public License
|
||||
(GPL). Essentially, this means that you are free to do almost exactly
|
||||
what you want with the program, including distributing it among your
|
||||
friends, making it available for download from your web site, selling
|
||||
it (either by itself or as part of some bigger software package), or
|
||||
using it as the starting point for a software project of your own.
|
||||
|
||||
The only real limitation is that whenever you distribute Stockfish in
|
||||
some way, you must always include the full source code, or a pointer
|
||||
to where the source code can be found. If you make any changes to the
|
||||
source code, these changes must also be made available under the GPL.
|
||||
|
||||
For full details, read the copy of the GPL found in the file named
|
||||
Copying.txt.
|
||||
|
||||
+23
-24
@@ -1,4 +1,3 @@
|
||||
|
||||
[PolyGlot]
|
||||
|
||||
EngineDir = .
|
||||
@@ -15,34 +14,34 @@ ResignScore = 600
|
||||
|
||||
[Engine]
|
||||
|
||||
Hash = 128
|
||||
Threads = 1
|
||||
OwnBook = false
|
||||
Write Debug Log = false
|
||||
Write Search Log = false
|
||||
Search Log Filename = SearchLog.txt
|
||||
Book File = book.bin
|
||||
Best Book Move = false
|
||||
Use Search Log = false
|
||||
Search Log Filename = SearchLog.txt
|
||||
Mobility (Middle Game) = 100
|
||||
Contempt Factor = 0
|
||||
Mobility (Midgame) = 100
|
||||
Mobility (Endgame) = 100
|
||||
Pawn Structure (Middle Game) = 100
|
||||
Pawn Structure (Midgame) = 100
|
||||
Pawn Structure (Endgame) = 100
|
||||
Passed Pawns (Middle Game) = 100
|
||||
Passed Pawns (Midgame) = 100
|
||||
Passed Pawns (Endgame) = 100
|
||||
Space = 100
|
||||
Aggressiveness = 100
|
||||
Cowardice = 100
|
||||
Check Extension (PV nodes) = 2
|
||||
Check Extension (non-PV nodes) = 1
|
||||
Single Reply Extension (PV nodes) = 2
|
||||
Single Reply Extension (non-PV nodes) = 2
|
||||
Mate Threat Extension (PV nodes) = 0
|
||||
Mate Threat Extension (non-PV nodes) = 0
|
||||
Pawn Push to 7th Extension (PV nodes) = 1
|
||||
Pawn Push to 7th Extension (non-PV nodes) = 1
|
||||
Passed Pawn Extension (PV nodes) = 1
|
||||
Passed Pawn Extension (non-PV nodes) = 0
|
||||
Pawn Endgame Extension (PV nodes) = 2
|
||||
Pawn Endgame Extension (non-PV nodes) = 2
|
||||
Randomness = 0
|
||||
Minimum Split Depth = 4
|
||||
Maximum Number of Threads per Split Point = 5
|
||||
Min Split Depth = 0
|
||||
Max Threads per Split Point = 5
|
||||
Threads = 1
|
||||
Idle Threads Sleep = false
|
||||
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
|
||||
UCI_AnalyseMode = false
|
||||
|
||||
-674
@@ -1,674 +0,0 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program 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.
|
||||
|
||||
This program 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/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<http://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
||||
+167
-130
@@ -1,8 +1,6 @@
|
||||
# Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
# Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
# Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
#
|
||||
# This file is part of Stockfish.
|
||||
# Copyright (C) 2008-2013 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
|
||||
@@ -22,21 +20,28 @@
|
||||
### Section 1. General Configuration
|
||||
### ==========================================================================
|
||||
|
||||
### Establish the operating system name
|
||||
UNAME = $(shell uname)
|
||||
|
||||
### Executable name
|
||||
EXE = stockfish
|
||||
|
||||
### Installation dir definitions
|
||||
PREFIX = /usr/local
|
||||
# Haiku has a non-standard filesystem layout
|
||||
ifeq ($(UNAME),Haiku)
|
||||
PREFIX=/boot/common
|
||||
endif
|
||||
BINDIR = $(PREFIX)/bin
|
||||
|
||||
### Built-in benchmark for pgo-builds
|
||||
### Built-in benchmark for pgo-builds and signature
|
||||
PGOBENCH = ./$(EXE) bench 32 1 10 default depth
|
||||
SIGNBENCH = ./$(EXE) bench
|
||||
|
||||
### Object files
|
||||
OBJS = bitboard.o pawns.o material.o endgame.o evaluate.o main.o \
|
||||
misc.o move.o movegen.o history.o movepick.o search.o position.o \
|
||||
tt.o uci.o ucioption.o book.o bitbase.o san.o benchmark.o timeman.o
|
||||
|
||||
OBJS = benchmark.o bitbase.o bitboard.o book.o endgame.o evaluate.o main.o \
|
||||
material.o misc.o movegen.o movepick.o notation.o pawns.o position.o \
|
||||
search.o thread.o timeman.o tt.o uci.o ucioption.o
|
||||
|
||||
### ==========================================================================
|
||||
### Section 2. High-level Configuration
|
||||
@@ -45,16 +50,16 @@ OBJS = bitboard.o pawns.o material.o endgame.o evaluate.o main.o \
|
||||
# flag --- Comp switch --- Description
|
||||
# ----------------------------------------------------------------------------
|
||||
#
|
||||
# debug = no/yes --- -DNDEBUG --- Enable/Disable debug mode
|
||||
# debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
|
||||
# optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
|
||||
# arch = (name) --- (-arch) --- Target architecture
|
||||
# os = (name) --- --- Target operating system
|
||||
# bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
|
||||
# bigendian = no/yes --- -DBIGENDIAN --- big/little-endian byte order
|
||||
# prefetch = no/yes --- -DUSE_PREFETCH --- Use prefetch x86 asm-instruction
|
||||
# bsfq = no/yes --- -DUSE_BSFQ --- Use bsfq x86_64 asm-instruction
|
||||
# --- (Works only with GCC and ICC 64-bit)
|
||||
# popcnt = no/yes --- -DUSE_POPCNT --- Use popcnt x86_64 asm-instruction
|
||||
# arch = (name) --- (-arch) --- Target architecture
|
||||
# os = (name) --- --- Target operating system
|
||||
# bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
|
||||
# prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch x86 asm-instruction
|
||||
# bsfq = yes/no --- -DUSE_BSFQ --- Use bsfq x86_64 asm-instruction (only
|
||||
# with GCC and ICC 64-bit)
|
||||
# popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt x86_64 asm-instruction
|
||||
# sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
|
||||
#
|
||||
# Note that Makefile is space sensitive, so when adding new architectures
|
||||
# or modifying existing flags, you have to make sure there are no extra spaces
|
||||
@@ -71,40 +76,20 @@ ifeq ($(ARCH),general-64)
|
||||
arch = any
|
||||
os = any
|
||||
bits = 64
|
||||
bigendian = no
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
sse = no
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),general-32)
|
||||
arch = any
|
||||
os = any
|
||||
bits = 32
|
||||
bigendian = no
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),bigendian-64)
|
||||
arch = any
|
||||
os = any
|
||||
bits = 64
|
||||
bigendian = yes
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),bigendian-32)
|
||||
arch = any
|
||||
os = any
|
||||
bits = 32
|
||||
bigendian = yes
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
sse = no
|
||||
endif
|
||||
|
||||
# x86-section
|
||||
@@ -112,40 +97,51 @@ ifeq ($(ARCH),x86-64)
|
||||
arch = x86_64
|
||||
os = any
|
||||
bits = 64
|
||||
bigendian = no
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
popcnt = no
|
||||
sse = yes
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),x86-64-modern)
|
||||
arch = x86_64
|
||||
os = any
|
||||
bits = 64
|
||||
bigendian = no
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
popcnt = yes
|
||||
sse = yes
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),x86-32)
|
||||
arch = i386
|
||||
os = any
|
||||
bits = 32
|
||||
bigendian = no
|
||||
prefetch = yes
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
sse = yes
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),x86-32-old)
|
||||
arch = i386
|
||||
os = any
|
||||
bits = 32
|
||||
bigendian = no
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
sse = no
|
||||
endif
|
||||
|
||||
#arm section
|
||||
ifeq ($(ARCH),armv7)
|
||||
arch = armv7
|
||||
os = any
|
||||
bits = 32
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
popcnt = no
|
||||
sse = no
|
||||
endif
|
||||
|
||||
# osx-section
|
||||
@@ -153,40 +149,40 @@ ifeq ($(ARCH),osx-ppc-64)
|
||||
arch = ppc64
|
||||
os = osx
|
||||
bits = 64
|
||||
bigendian = yes
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
sse = no
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),osx-ppc-32)
|
||||
arch = ppc
|
||||
os = osx
|
||||
bits = 32
|
||||
bigendian = yes
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
sse = no
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),osx-x86-64)
|
||||
arch = x86_64
|
||||
os = osx
|
||||
bits = 64
|
||||
bigendian = no
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
popcnt = no
|
||||
sse = yes
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),osx-x86-32)
|
||||
arch = i386
|
||||
os = osx
|
||||
bits = 32
|
||||
bigendian = no
|
||||
prefetch = yes
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
sse = yes
|
||||
endif
|
||||
|
||||
|
||||
@@ -226,35 +222,62 @@ ifeq ($(COMP),icc)
|
||||
profile_clean = icc-profile-clean
|
||||
endif
|
||||
|
||||
ifeq ($(COMP),clang)
|
||||
comp=clang
|
||||
CXX=clang++
|
||||
profile_prepare = gcc-profile-prepare
|
||||
profile_make = gcc-profile-make
|
||||
profile_use = gcc-profile-use
|
||||
profile_clean = gcc-profile-clean
|
||||
endif
|
||||
|
||||
### 3.2 General compiler settings
|
||||
CXXFLAGS = -g -Wall -Wcast-qual -fno-exceptions -fno-rtti $(EXTRACXXFLAGS)
|
||||
CXXFLAGS = -Wall -Wcast-qual -fno-exceptions -fno-rtti $(EXTRACXXFLAGS)
|
||||
|
||||
ifeq ($(comp),gcc)
|
||||
CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra
|
||||
CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
|
||||
endif
|
||||
|
||||
ifeq ($(comp),mingw)
|
||||
CXXFLAGS += -Wno-long-long -Wextra
|
||||
CXXFLAGS += -Wextra -Wshadow
|
||||
endif
|
||||
|
||||
ifeq ($(comp),icc)
|
||||
CXXFLAGS += -wd383,981,1418,1419,10187,10188,11505,11503 -Wcheck -Wabi -Wdeprecated -strict-ansi
|
||||
CXXFLAGS += -wd383,981,1418,1419,1476,10187,10188,11505,11503 -Wcheck -Wabi -Wdeprecated -strict-ansi
|
||||
endif
|
||||
|
||||
ifeq ($(comp),clang)
|
||||
CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
|
||||
endif
|
||||
|
||||
ifeq ($(os),osx)
|
||||
CXXFLAGS += -arch $(arch)
|
||||
CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.0
|
||||
endif
|
||||
|
||||
### 3.3 General linker settings
|
||||
LDFLAGS = -lpthread $(EXTRALDFLAGS)
|
||||
LDFLAGS = $(EXTRALDFLAGS)
|
||||
|
||||
ifeq ($(comp),mingw)
|
||||
LDFLAGS += -static-libstdc++ -static-libgcc
|
||||
endif
|
||||
|
||||
### On mingw use Windows threads, otherwise POSIX
|
||||
ifneq ($(comp),mingw)
|
||||
# Haiku has pthreads in its libroot, so only link it in on other platforms
|
||||
ifneq ($(UNAME),Haiku)
|
||||
LDFLAGS += -lpthread
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(os),osx)
|
||||
LDFLAGS += -arch $(arch)
|
||||
LDFLAGS += -arch $(arch) -mmacosx-version-min=10.0
|
||||
endif
|
||||
|
||||
### 3.4 Debugging
|
||||
ifeq ($(debug),no)
|
||||
CXXFLAGS += -DNDEBUG
|
||||
else
|
||||
CXXFLAGS += -g
|
||||
endif
|
||||
|
||||
### 3.5 Optimization
|
||||
@@ -271,6 +294,10 @@ ifeq ($(optimize),yes)
|
||||
CXXFLAGS += -mdynamic-no-pic
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(arch),armv7)
|
||||
CXXFLAGS += -fno-gcse
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(comp),mingw)
|
||||
@@ -278,10 +305,24 @@ ifeq ($(optimize),yes)
|
||||
endif
|
||||
|
||||
ifeq ($(comp),icc)
|
||||
CXXFLAGS += -fast
|
||||
ifeq ($(os),osx)
|
||||
CXXFLAGS += -fast -mdynamic-no-pic
|
||||
else
|
||||
CXXFLAGS += -O3
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(comp),clang)
|
||||
### -O4 requires a linker that supports LLVM's LTO
|
||||
CXXFLAGS += -O3
|
||||
|
||||
ifeq ($(os),osx)
|
||||
CXXFLAGS += -mdynamic-no-pic
|
||||
ifeq ($(arch),i386)
|
||||
CXXFLAGS += -mdynamic-no-pic
|
||||
endif
|
||||
ifeq ($(arch),x86_64)
|
||||
CXXFLAGS += -mdynamic-no-pic
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
@@ -291,36 +332,46 @@ ifeq ($(bits),64)
|
||||
CXXFLAGS += -DIS_64BIT
|
||||
endif
|
||||
|
||||
### 3.7 Endianess
|
||||
ifeq ($(bigendian),yes)
|
||||
CXXFLAGS += -DBIGENDIAN
|
||||
endif
|
||||
|
||||
### 3.8 prefetch
|
||||
### 3.7 prefetch
|
||||
ifeq ($(prefetch),yes)
|
||||
CXXFLAGS += -msse
|
||||
DEPENDFLAGS += -msse
|
||||
ifeq ($(sse),yes)
|
||||
CXXFLAGS += -msse
|
||||
DEPENDFLAGS += -msse
|
||||
endif
|
||||
else
|
||||
CXXFLAGS += -DNO_PREFETCH
|
||||
endif
|
||||
|
||||
### 3.9 bsfq
|
||||
### 3.8 bsfq
|
||||
ifeq ($(bsfq),yes)
|
||||
CXXFLAGS += -DUSE_BSFQ
|
||||
endif
|
||||
|
||||
### 3.10 popcnt
|
||||
### 3.9 popcnt
|
||||
ifeq ($(popcnt),yes)
|
||||
CXXFLAGS += -DUSE_POPCNT
|
||||
CXXFLAGS += -msse3 -DUSE_POPCNT
|
||||
endif
|
||||
|
||||
### 3.10 Link Time Optimization, it works since gcc 4.5 but not on mingw.
|
||||
### This is a mix of compile and link time options because the lto link phase
|
||||
### needs access to the optimization flags.
|
||||
ifeq ($(comp),gcc)
|
||||
ifeq ($(optimize),yes)
|
||||
ifeq ($(debug),no)
|
||||
GCC_MAJOR := `$(CXX) -dumpversion | cut -f1 -d.`
|
||||
GCC_MINOR := `$(CXX) -dumpversion | cut -f2 -d.`
|
||||
ifeq (1,$(shell expr \( $(GCC_MAJOR) \> 4 \) \| \( $(GCC_MAJOR) \= 4 \& $(GCC_MINOR) \>= 5 \)))
|
||||
CXXFLAGS += -flto
|
||||
LDFLAGS += $(CXXFLAGS)
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
### ==========================================================================
|
||||
### Section 4. Public targets
|
||||
### ==========================================================================
|
||||
|
||||
default:
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) build
|
||||
|
||||
help:
|
||||
@echo ""
|
||||
@echo "To compile stockfish, type: "
|
||||
@@ -329,45 +380,46 @@ help:
|
||||
@echo ""
|
||||
@echo "Supported targets:"
|
||||
@echo ""
|
||||
@echo "build > Build unoptimized version"
|
||||
@echo "profile-build > Build PGO-optimized version"
|
||||
@echo "popcnt-profile-build > Build PGO-optimized version with optional popcnt-support"
|
||||
@echo "strip > Strip executable"
|
||||
@echo "install > Install executable"
|
||||
@echo "clean > Clean up"
|
||||
@echo "testrun > Make sample run"
|
||||
@echo "build > Standard build"
|
||||
@echo "signature-build > Standard build with embedded signature"
|
||||
@echo "profile-build > PGO build"
|
||||
@echo "signature-profile-build > PGO build with embedded signature"
|
||||
@echo "strip > Strip executable"
|
||||
@echo "install > Install executable"
|
||||
@echo "clean > Clean up"
|
||||
@echo ""
|
||||
@echo "Supported archs:"
|
||||
@echo ""
|
||||
@echo "x86-64 > x86 64-bit"
|
||||
@echo "x86-64-modern > x86 64-bit with runtime support for popcnt-instruction"
|
||||
@echo "x86-32 > x86 32-bit excluding very old hardware without SSE-support"
|
||||
@echo "x86-32-old > x86 32-bit including also very old hardware"
|
||||
@echo "osx-ppc-64 > PPC-Mac OS X 64 bit"
|
||||
@echo "osx-ppc-32 > PPC-Mac OS X 32 bit"
|
||||
@echo "osx-x86-64 > x86-Mac OS X 64 bit"
|
||||
@echo "osx-x86-32 > x86-Mac OS X 32 bit"
|
||||
@echo "general-64 > unspecified 64-bit"
|
||||
@echo "general-32 > unspecified 32-bit"
|
||||
@echo "bigendian-64 > unspecified 64-bit with bigendian byte order"
|
||||
@echo "bigendian-32 > unspecified 32-bit with bigendian byte order"
|
||||
@echo "x86-64 > x86 64-bit"
|
||||
@echo "x86-64-modern > x86 64-bit with popcnt support"
|
||||
@echo "x86-32 > x86 32-bit with SSE support"
|
||||
@echo "x86-32-old > x86 32-bit fall back for old hardware"
|
||||
@echo "osx-ppc-64 > PPC-Mac OS X 64 bit"
|
||||
@echo "osx-ppc-32 > PPC-Mac OS X 32 bit"
|
||||
@echo "osx-x86-64 > x86-Mac OS X 64 bit"
|
||||
@echo "osx-x86-32 > x86-Mac OS X 32 bit"
|
||||
@echo "armv7 > ARMv7 32 bit"
|
||||
@echo "general-64 > unspecified 64-bit"
|
||||
@echo "general-32 > unspecified 32-bit"
|
||||
@echo ""
|
||||
@echo "Supported comps:"
|
||||
@echo "Supported compilers:"
|
||||
@echo ""
|
||||
@echo "gcc > Gnu compiler (default)"
|
||||
@echo "icc > Intel compiler"
|
||||
@echo "mingw > Gnu compiler with MinGW under Windows"
|
||||
@echo "gcc > Gnu compiler (default)"
|
||||
@echo "mingw > Gnu compiler with MinGW under Windows"
|
||||
@echo "clang > LLVM Clang compiler"
|
||||
@echo "icc > Intel compiler"
|
||||
@echo ""
|
||||
@echo "Non-standard targets:"
|
||||
@echo ""
|
||||
@echo "make hpux > Compile for HP-UX. Compiler = aCC"
|
||||
@echo "make hpux > Compile for HP-UX. Compiler = aCC"
|
||||
@echo ""
|
||||
@echo "Examples. If you don't know what to do, you likely want to run: "
|
||||
@echo ""
|
||||
@echo "make profile-build ARCH=x86-64 (This is for 64-bit systems)"
|
||||
@echo "make profile-build ARCH=x86-32 (This is for 32-bit systems)"
|
||||
@echo "make build ARCH=x86-64 (This is for 64-bit systems)"
|
||||
@echo "make build ARCH=x86-32 (This is for 32-bit systems)"
|
||||
@echo ""
|
||||
|
||||
.PHONY: build profile-build embed-signature
|
||||
build:
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
|
||||
@@ -392,33 +444,17 @@ profile-build:
|
||||
@echo "Step 4/4. Deleting profile data ..."
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
|
||||
|
||||
popcnt-profile-build:
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
|
||||
@echo ""
|
||||
@echo "Step 0/6. Preparing for profile build."
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_prepare)
|
||||
@echo ""
|
||||
@echo "Step 1/6. Building executable for benchmark (popcnt disabled)..."
|
||||
@touch *.cpp *.h
|
||||
$(MAKE) ARCH=x86-64 COMP=$(COMP) $(profile_make)
|
||||
@echo ""
|
||||
@echo "Step 2/6. Running benchmark for pgo-build (popcnt disabled)..."
|
||||
@$(PGOBENCH) > /dev/null
|
||||
@echo ""
|
||||
@echo "Step 3/6. Building executable for benchmark (popcnt enabled)..."
|
||||
@touch *.cpp *.h
|
||||
$(MAKE) ARCH=x86-64-modern COMP=$(COMP) $(profile_make)
|
||||
@echo ""
|
||||
@echo "Step 4/6. Running benchmark for pgo-build (popcnt enabled)..."
|
||||
@$(PGOBENCH) > /dev/null
|
||||
@echo ""
|
||||
@echo "Step 5/6. Building final executable ..."
|
||||
@touch *.cpp *.h
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
|
||||
@echo ""
|
||||
@echo "Step 6/6. Deleting profile data ..."
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_clean)
|
||||
@echo ""
|
||||
embed-signature:
|
||||
@echo "Running benchmark for getting the signature ..."
|
||||
@$(SIGNBENCH) 2>&1 | sed -n 's/Nodes searched : \(.*\)/\/string Version\/s\/"\\(.*\\)"\/"sig-\1"\//p' > sign.txt
|
||||
@sed -f sign.txt misc.cpp > misc2.cpp
|
||||
@mv misc2.cpp misc.cpp
|
||||
@rm sign.txt
|
||||
|
||||
signature-build: build embed-signature
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
|
||||
|
||||
signature-profile-build: build embed-signature profile-build
|
||||
|
||||
strip:
|
||||
strip $(EXE)
|
||||
@@ -431,8 +467,8 @@ install:
|
||||
clean:
|
||||
$(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda
|
||||
|
||||
testrun:
|
||||
@$(PGOBENCH)
|
||||
default:
|
||||
help
|
||||
|
||||
### ==========================================================================
|
||||
### Section 5. Private targets
|
||||
@@ -448,10 +484,10 @@ config-sanity:
|
||||
@echo "arch: '$(arch)'"
|
||||
@echo "os: '$(os)'"
|
||||
@echo "bits: '$(bits)'"
|
||||
@echo "bigendian: '$(bigendian)'"
|
||||
@echo "prefetch: '$(prefetch)'"
|
||||
@echo "bsfq: '$(bsfq)'"
|
||||
@echo "popcnt: '$(popcnt)'"
|
||||
@echo "sse: '$(sse)'"
|
||||
@echo ""
|
||||
@echo "Flags:"
|
||||
@echo "CXX: $(CXX)"
|
||||
@@ -463,14 +499,14 @@ config-sanity:
|
||||
@test "$(debug)" = "yes" || test "$(debug)" = "no"
|
||||
@test "$(optimize)" = "yes" || test "$(optimize)" = "no"
|
||||
@test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
|
||||
test "$(arch)" = "ppc64" || test "$(arch)" = "ppc"
|
||||
test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
|
||||
@test "$(os)" = "any" || test "$(os)" = "osx"
|
||||
@test "$(bits)" = "32" || test "$(bits)" = "64"
|
||||
@test "$(bigendian)" = "yes" || test "$(bigendian)" = "no"
|
||||
@test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
|
||||
@test "$(bsfq)" = "yes" || test "$(bsfq)" = "no"
|
||||
@test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
|
||||
@test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw"
|
||||
@test "$(sse)" = "yes" || test "$(sse)" = "no"
|
||||
@test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
|
||||
|
||||
$(EXE): $(OBJS)
|
||||
$(CXX) -o $@ $(OBJS) $(LDFLAGS)
|
||||
@@ -487,6 +523,7 @@ gcc-profile-make:
|
||||
gcc-profile-use:
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
|
||||
EXTRACXXFLAGS='-fprofile-use' \
|
||||
EXTRALDFLAGS='-lgcov' \
|
||||
all
|
||||
|
||||
gcc-profile-clean:
|
||||
@@ -521,7 +558,7 @@ icc-profile-clean:
|
||||
|
||||
hpux:
|
||||
$(MAKE) \
|
||||
CXX='/opt/aCC/bin/aCC -AA +hpxstd98 -DBIGENDIAN -mt +O3 -DNDEBUG -DNO_PREFETCH' \
|
||||
CXX='/opt/aCC/bin/aCC -AA +hpxstd98 -mt +O3 -DNDEBUG -DNO_PREFETCH' \
|
||||
CXXFLAGS="" \
|
||||
LDFLAGS="" \
|
||||
all
|
||||
|
||||
+82
-91
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,27 +17,24 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
#include "tt.h"
|
||||
#include "ucioption.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
////
|
||||
//// Variables
|
||||
////
|
||||
|
||||
static const string BenchmarkPositions[] = {
|
||||
static const char* Defaults[] = {
|
||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
||||
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -",
|
||||
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -",
|
||||
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 10",
|
||||
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 11",
|
||||
"4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19",
|
||||
"rq3rk1/ppp2ppp/1bnpb3/3N2B1/3NP3/7P/PPPQ1PP1/2KR3R w - - 7 14",
|
||||
"r1bq1r1k/1pp1n1pp/1p1p4/4p2Q/4Pp2/1BNP4/PPP2PPP/3R1RK1 w - - 2 14",
|
||||
@@ -50,105 +47,99 @@ static const string BenchmarkPositions[] = {
|
||||
"3r1rk1/p5pp/bpp1pp2/8/q1PP1P2/b3P3/P2NQRPP/1R2B1K1 b - - 6 22",
|
||||
"r1q2rk1/2p1bppp/2Pp4/p6b/Q1PNp3/4B3/PP1R1PPP/2K4R w - - 2 18",
|
||||
"4k2r/1pb2ppp/1p2p3/1R1p4/3P4/2r1PN2/P4PPP/1R4K1 b - - 3 22",
|
||||
"3q2k1/pb3p1p/4pbp1/2r5/PpN2N2/1P2P2P/5PP1/Q2R2K1 b - - 4 26",
|
||||
""
|
||||
"3q2k1/pb3p1p/4pbp1/2r5/PpN2N2/1P2P2P/5PP1/Q2R2K1 b - - 4 26"
|
||||
};
|
||||
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
/// 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
|
||||
/// transposition table size, the number of search threads that should
|
||||
/// be used, the limit value spent for each position (optional, default
|
||||
/// is ply 12), an optional file name where to look for positions in fen
|
||||
/// format (default are the BenchmarkPositions defined above) and the type
|
||||
/// of the limit value: depth (default), time in secs or number of nodes.
|
||||
/// The analysis is written to a file named bench.txt.
|
||||
/// be used, the limit value spent for each position (optional, default is
|
||||
/// depth 12), an optional file name where to look for positions in fen
|
||||
/// format (defaults are the positions defined above) and the type of the
|
||||
/// limit value: depth (default), time in secs or number of nodes.
|
||||
|
||||
void benchmark(int argc, char* argv[]) {
|
||||
void benchmark(const Position& current, istream& is) {
|
||||
|
||||
vector<string> positions;
|
||||
string ttSize, threads, valStr, posFile, valType;
|
||||
int val, secsPerPos, maxDepth, maxNodes;
|
||||
string token;
|
||||
Search::LimitsType limits;
|
||||
vector<string> fens;
|
||||
|
||||
ttSize = argc > 2 ? argv[2] : "128";
|
||||
threads = argc > 3 ? argv[3] : "1";
|
||||
valStr = argc > 4 ? argv[4] : "12";
|
||||
posFile = argc > 5 ? argv[5] : "default";
|
||||
valType = argc > 6 ? argv[6] : "depth";
|
||||
// Assign default values to missing arguments
|
||||
string ttSize = (is >> token) ? token : "32";
|
||||
string threads = (is >> token) ? token : "1";
|
||||
string limit = (is >> token) ? token : "12";
|
||||
string fenFile = (is >> token) ? token : "default";
|
||||
string limitType = (is >> token) ? token : "depth";
|
||||
|
||||
Options["Hash"].set_value(ttSize);
|
||||
Options["Threads"].set_value(threads);
|
||||
Options["OwnBook"].set_value("false");
|
||||
Options["Use Search Log"].set_value("true");
|
||||
Options["Search Log Filename"].set_value("bench.txt");
|
||||
Options["Hash"] = ttSize;
|
||||
Options["Threads"] = threads;
|
||||
TT.clear();
|
||||
|
||||
secsPerPos = maxDepth = maxNodes = 0;
|
||||
val = atoi(valStr.c_str());
|
||||
if (limitType == "time")
|
||||
limits.movetime = 1000 * atoi(limit.c_str()); // movetime is in ms
|
||||
|
||||
else if (limitType == "nodes")
|
||||
limits.nodes = atoi(limit.c_str());
|
||||
|
||||
else if (limitType == "mate")
|
||||
limits.mate = atoi(limit.c_str());
|
||||
|
||||
if (valType == "depth" || valType == "perft")
|
||||
maxDepth = val;
|
||||
else if (valType == "time")
|
||||
secsPerPos = val * 1000;
|
||||
else
|
||||
maxNodes = val;
|
||||
limits.depth = atoi(limit.c_str());
|
||||
|
||||
if (posFile != "default")
|
||||
if (fenFile == "default")
|
||||
fens.assign(Defaults, Defaults + 16);
|
||||
|
||||
else if (fenFile == "current")
|
||||
fens.push_back(current.fen());
|
||||
|
||||
else
|
||||
{
|
||||
ifstream fenFile(posFile.c_str());
|
||||
if (!fenFile.is_open())
|
||||
{
|
||||
cerr << "Unable to open positions file " << posFile << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
string pos;
|
||||
while (fenFile.good())
|
||||
{
|
||||
getline(fenFile, pos);
|
||||
if (!pos.empty())
|
||||
positions.push_back(pos);
|
||||
}
|
||||
fenFile.close();
|
||||
} else
|
||||
for (int i = 0; !BenchmarkPositions[i].empty(); i++)
|
||||
positions.push_back(BenchmarkPositions[i]);
|
||||
string fen;
|
||||
ifstream file(fenFile.c_str());
|
||||
|
||||
vector<string>::iterator it;
|
||||
int cnt = 1;
|
||||
int64_t totalNodes = 0;
|
||||
int startTime = get_system_time();
|
||||
if (!file.is_open())
|
||||
{
|
||||
cerr << "Unable to open file " << fenFile << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
for (it = positions.begin(); it != positions.end(); ++it, ++cnt)
|
||||
while (getline(file, fen))
|
||||
if (!fen.empty())
|
||||
fens.push_back(fen);
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
int64_t nodes = 0;
|
||||
Search::StateStackPtr st;
|
||||
Time::point elapsed = Time::now();
|
||||
|
||||
for (size_t i = 0; i < fens.size(); i++)
|
||||
{
|
||||
Move moves[1] = { MOVE_NONE };
|
||||
int dummy[2] = { 0, 0 };
|
||||
Position pos(*it, 0);
|
||||
cerr << "\nBench position: " << cnt << '/' << positions.size() << endl << endl;
|
||||
if (valType == "perft")
|
||||
Position pos(fens[i], Options["UCI_Chess960"], Threads.main());
|
||||
|
||||
cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
|
||||
|
||||
if (limitType == "perft")
|
||||
{
|
||||
int64_t perftCnt = perft(pos, maxDepth * ONE_PLY);
|
||||
cerr << "\nPerft " << maxDepth << " result (nodes searched): " << perftCnt << endl << endl;
|
||||
totalNodes += perftCnt;
|
||||
} else {
|
||||
if (!think(pos, false, false, dummy, dummy, 0, maxDepth, maxNodes, secsPerPos, moves))
|
||||
break;
|
||||
totalNodes += pos.nodes_searched();
|
||||
size_t cnt = Search::perft(pos, limits.depth * ONE_PLY);
|
||||
cerr << "\nPerft " << limits.depth << " leaf nodes: " << cnt << endl;
|
||||
nodes += cnt;
|
||||
}
|
||||
else
|
||||
{
|
||||
Threads.start_thinking(pos, limits, vector<Move>(), st);
|
||||
Threads.wait_for_think_finished();
|
||||
nodes += Search::RootPos.nodes_searched();
|
||||
}
|
||||
}
|
||||
|
||||
cnt = get_system_time() - startTime;
|
||||
cerr << "==============================="
|
||||
<< "\nTotal time (ms) : " << cnt
|
||||
<< "\nNodes searched : " << totalNodes
|
||||
<< "\nNodes/second : " << (int)(totalNodes/(cnt/1000.0)) << endl << endl;
|
||||
elapsed = Time::now() - elapsed + 1; // Assure positive to avoid a 'divide by zero'
|
||||
|
||||
// Under MS Visual C++ debug window always unconditionally closes
|
||||
// when program exits, this is bad because we want to read results before.
|
||||
#if (defined(WINDOWS) || defined(WIN32) || defined(WIN64))
|
||||
cerr << "Press any key to exit" << endl;
|
||||
cin >> ttSize;
|
||||
#endif
|
||||
cerr << "\n==========================="
|
||||
<< "\nTotal time (ms) : " << elapsed
|
||||
<< "\nNodes searched : " << nodes
|
||||
<< "\nNodes/second : " << 1000 * nodes / elapsed << endl;
|
||||
}
|
||||
|
||||
+116
-260
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,303 +17,159 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "square.h"
|
||||
|
||||
|
||||
////
|
||||
//// Local definitions
|
||||
////
|
||||
#include "types.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// The possible pawns squares are 24, the first 4 files and ranks from 2 to 7
|
||||
const unsigned IndexMax = 2*24*64*64; // stm * psq * wksq * bksq = 196608
|
||||
|
||||
// Each uint32_t stores results of 32 positions, one per bit
|
||||
uint32_t KPKBitbase[IndexMax / 32];
|
||||
|
||||
// A KPK bitbase index is an integer in [0, IndexMax] range
|
||||
//
|
||||
// Information is mapped in a way that minimizes number of iterations:
|
||||
//
|
||||
// bit 0- 5: white king square (from SQ_A1 to SQ_H8)
|
||||
// bit 6-11: black king square (from SQ_A1 to SQ_H8)
|
||||
// bit 12: side to move (WHITE or BLACK)
|
||||
// bit 13-14: white pawn file (from FILE_A to FILE_D)
|
||||
// bit 15-17: white pawn RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2)
|
||||
unsigned index(Color us, Square bksq, Square wksq, Square psq) {
|
||||
return wksq + (bksq << 6) + (us << 12) + (file_of(psq) << 13) + ((RANK_7 - rank_of(psq)) << 15);
|
||||
}
|
||||
|
||||
enum Result {
|
||||
RESULT_UNKNOWN,
|
||||
RESULT_INVALID,
|
||||
RESULT_WIN,
|
||||
RESULT_LOSS,
|
||||
RESULT_DRAW
|
||||
INVALID = 0,
|
||||
UNKNOWN = 1,
|
||||
DRAW = 2,
|
||||
WIN = 4
|
||||
};
|
||||
|
||||
inline Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
|
||||
|
||||
struct KPKPosition {
|
||||
void from_index(int index);
|
||||
bool is_legal() const;
|
||||
bool is_immediate_draw() const;
|
||||
bool is_immediate_win() const;
|
||||
Bitboard wk_attacks() const { return StepAttackBB[WK][whiteKingSquare]; }
|
||||
Bitboard bk_attacks() const { return StepAttackBB[BK][blackKingSquare]; }
|
||||
Bitboard pawn_attacks() const { return StepAttackBB[WP][pawnSquare]; }
|
||||
|
||||
Square whiteKingSquare, blackKingSquare, pawnSquare;
|
||||
Color sideToMove;
|
||||
operator Result() const { return res; }
|
||||
Result classify_leaf(unsigned idx);
|
||||
Result classify(const std::vector<KPKPosition>& db)
|
||||
{ return us == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
|
||||
|
||||
private:
|
||||
template<Color Us> Result classify(const std::vector<KPKPosition>& db);
|
||||
|
||||
Color us;
|
||||
Square bksq, wksq, psq;
|
||||
Result res;
|
||||
};
|
||||
|
||||
const int IndexMax = 2 * 24 * 64 * 64;
|
||||
} // namespace
|
||||
|
||||
Result classify_wtm(const KPKPosition& pos, const Result bb[]);
|
||||
Result classify_btm(const KPKPosition& pos, const Result bb[]);
|
||||
int compute_index(Square wksq, Square bksq, Square psq, Color stm);
|
||||
|
||||
bool Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color us) {
|
||||
|
||||
assert(file_of(wpsq) <= FILE_D);
|
||||
|
||||
unsigned idx = index(us, bksq, wksq, wpsq);
|
||||
return KPKBitbase[idx / 32] & (1 << (idx & 0x1F));
|
||||
}
|
||||
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
void Bitbases::init_kpk() {
|
||||
|
||||
void generate_kpk_bitbase(uint8_t bitbase[]) {
|
||||
unsigned idx, repeat = 1;
|
||||
std::vector<KPKPosition> db(IndexMax);
|
||||
|
||||
bool repeat;
|
||||
int i, j, b;
|
||||
KPKPosition pos;
|
||||
Result bb[IndexMax];
|
||||
// Initialize db with known win / draw positions
|
||||
for (idx = 0; idx < IndexMax; idx++)
|
||||
db[idx].classify_leaf(idx);
|
||||
|
||||
// Initialize table
|
||||
for (i = 0; i < IndexMax; i++)
|
||||
{
|
||||
pos.from_index(i);
|
||||
bb[i] = !pos.is_legal() ? RESULT_INVALID
|
||||
: pos.is_immediate_draw() ? RESULT_DRAW
|
||||
: pos.is_immediate_win() ? RESULT_WIN : RESULT_UNKNOWN;
|
||||
}
|
||||
// Iterate through the positions until no more of the unknown positions can be
|
||||
// changed to either wins or draws (15 cycles needed).
|
||||
while (repeat)
|
||||
for (repeat = idx = 0; idx < IndexMax; idx++)
|
||||
if (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN)
|
||||
repeat = 1;
|
||||
|
||||
// Iterate until all positions are classified (30 cycles needed)
|
||||
do {
|
||||
repeat = false;
|
||||
|
||||
for (i = 0; i < IndexMax; i++)
|
||||
if (bb[i] == RESULT_UNKNOWN)
|
||||
{
|
||||
pos.from_index(i);
|
||||
|
||||
bb[i] = (pos.sideToMove == WHITE) ? classify_wtm(pos, bb)
|
||||
: classify_btm(pos, bb);
|
||||
if (bb[i] != RESULT_UNKNOWN)
|
||||
repeat = true;
|
||||
}
|
||||
|
||||
} while (repeat);
|
||||
|
||||
// Compress result and map into supplied bitbase parameter
|
||||
for (i = 0; i < 24576; i++)
|
||||
{
|
||||
b = 0;
|
||||
for (j = 0; j < 8; j++)
|
||||
if (bb[8*i+j] == RESULT_WIN || bb[8*i+j] == RESULT_LOSS)
|
||||
b |= (1 << j);
|
||||
|
||||
bitbase[i] = (uint8_t)b;
|
||||
}
|
||||
// Map 32 results into one KPKBitbase[] entry
|
||||
for (idx = 0; idx < IndexMax; idx++)
|
||||
if (db[idx] == WIN)
|
||||
KPKBitbase[idx / 32] |= 1 << (idx & 0x1F);
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
int compute_index(Square wksq, Square bksq, Square psq, Color stm) {
|
||||
Result KPKPosition::classify_leaf(unsigned idx) {
|
||||
|
||||
int p = int(square_file(psq)) + (int(square_rank(psq)) - 1) * 4;
|
||||
int r = int(stm) + 2 * int(bksq) + 128 * int(wksq) + 8192 * p;
|
||||
wksq = Square((idx >> 0) & 0x3F);
|
||||
bksq = Square((idx >> 6) & 0x3F);
|
||||
us = Color ((idx >> 12) & 0x01);
|
||||
psq = File ((idx >> 13) & 0x03) | Rank(RANK_7 - (idx >> 15));
|
||||
|
||||
assert(r >= 0 && r < IndexMax);
|
||||
// Check if two pieces are on the same square or if a king can be captured
|
||||
if ( wksq == psq || wksq == bksq || bksq == psq
|
||||
|| (StepAttacksBB[KING][wksq] & bksq)
|
||||
|| (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
|
||||
return res = INVALID;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
void KPKPosition::from_index(int index) {
|
||||
|
||||
int s = (index / 8192) % 24;
|
||||
|
||||
sideToMove = Color(index % 2);
|
||||
blackKingSquare = Square((index / 2) % 64);
|
||||
whiteKingSquare = Square((index / 128) % 64);
|
||||
pawnSquare = make_square(File(s % 4), Rank(s / 4 + 1));
|
||||
}
|
||||
|
||||
bool KPKPosition::is_legal() const {
|
||||
|
||||
if ( whiteKingSquare == pawnSquare
|
||||
|| whiteKingSquare == blackKingSquare
|
||||
|| pawnSquare == blackKingSquare)
|
||||
return false;
|
||||
|
||||
if (sideToMove == WHITE)
|
||||
if (us == WHITE)
|
||||
{
|
||||
if ( bit_is_set(wk_attacks(), blackKingSquare)
|
||||
|| bit_is_set(pawn_attacks(), blackKingSquare))
|
||||
return false;
|
||||
// Immediate win if pawn can be promoted without getting captured
|
||||
if ( rank_of(psq) == RANK_7
|
||||
&& wksq != psq + DELTA_N
|
||||
&& ( square_distance(bksq, psq + DELTA_N) > 1
|
||||
||(StepAttacksBB[KING][wksq] & (psq + DELTA_N))))
|
||||
return res = WIN;
|
||||
}
|
||||
else if (bit_is_set(bk_attacks(), whiteKingSquare))
|
||||
return false;
|
||||
// Immediate draw if is stalemate or king captures undefended pawn
|
||||
else if ( !(StepAttacksBB[KING][bksq] & ~(StepAttacksBB[KING][wksq] | StepAttacksBB[PAWN][psq]))
|
||||
|| (StepAttacksBB[KING][bksq] & psq & ~StepAttacksBB[KING][wksq]))
|
||||
return res = DRAW;
|
||||
|
||||
return true;
|
||||
return res = UNKNOWN;
|
||||
}
|
||||
|
||||
bool KPKPosition::is_immediate_draw() const {
|
||||
template<Color Us>
|
||||
Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
|
||||
|
||||
if (sideToMove == BLACK)
|
||||
// White to Move: If one move leads to a position classified as WIN, the result
|
||||
// of the current position is WIN. If all moves lead to positions classified
|
||||
// as DRAW, the current position is classified DRAW otherwise the current
|
||||
// position is classified as UNKNOWN.
|
||||
//
|
||||
// Black to Move: If one move leads to a position classified as DRAW, the result
|
||||
// of the current position is DRAW. If all moves lead to positions classified
|
||||
// as WIN, the position is classified WIN otherwise the current position is
|
||||
// classified UNKNOWN.
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
Result r = INVALID;
|
||||
Bitboard b = StepAttacksBB[KING][Us == WHITE ? wksq : bksq];
|
||||
|
||||
while (b)
|
||||
r |= Us == WHITE ? db[index(Them, bksq, pop_lsb(&b), psq)]
|
||||
: db[index(Them, pop_lsb(&b), wksq, psq)];
|
||||
|
||||
if (Us == WHITE && rank_of(psq) < RANK_7)
|
||||
{
|
||||
Bitboard wka = wk_attacks();
|
||||
Bitboard bka = bk_attacks();
|
||||
Square s = psq + DELTA_N;
|
||||
r |= db[index(BLACK, bksq, wksq, s)]; // Single push
|
||||
|
||||
// Case 1: Stalemate
|
||||
if ((bka & ~(wka | pawn_attacks())) == EmptyBoardBB)
|
||||
return true;
|
||||
|
||||
// Case 2: King can capture pawn
|
||||
if (bit_is_set(bka, pawnSquare) && !bit_is_set(wka, pawnSquare))
|
||||
return true;
|
||||
if (rank_of(s) == RANK_3 && s != wksq && s != bksq)
|
||||
r |= db[index(BLACK, bksq, wksq, s + DELTA_N)]; // Double push
|
||||
}
|
||||
|
||||
if (Us == WHITE)
|
||||
return res = r & WIN ? WIN : r & UNKNOWN ? UNKNOWN : DRAW;
|
||||
else
|
||||
{
|
||||
// Case 1: Stalemate
|
||||
if ( whiteKingSquare == SQ_A8
|
||||
&& pawnSquare == SQ_A7
|
||||
&& (blackKingSquare == SQ_C7 || blackKingSquare == SQ_C8))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return res = r & DRAW ? DRAW : r & UNKNOWN ? UNKNOWN : WIN;
|
||||
}
|
||||
|
||||
bool KPKPosition::is_immediate_win() const {
|
||||
|
||||
// The position is an immediate win if it is white to move and the
|
||||
// white pawn can be promoted without getting captured.
|
||||
return sideToMove == WHITE
|
||||
&& square_rank(pawnSquare) == RANK_7
|
||||
&& ( square_distance(blackKingSquare, pawnSquare + DELTA_N) > 1
|
||||
|| bit_is_set(wk_attacks(), pawnSquare + DELTA_N));
|
||||
}
|
||||
|
||||
Result classify_wtm(const KPKPosition& pos, const Result bb[]) {
|
||||
|
||||
// If one move leads to a position classified as RESULT_LOSS, the result
|
||||
// of the current position is RESULT_WIN. If all moves lead to positions
|
||||
// classified as RESULT_DRAW, the current position is classified RESULT_DRAW
|
||||
// otherwise the current position is classified as RESULT_UNKNOWN.
|
||||
|
||||
bool unknownFound = false;
|
||||
Bitboard b;
|
||||
Square s;
|
||||
int idx;
|
||||
|
||||
// King moves
|
||||
b = pos.wk_attacks();
|
||||
while (b)
|
||||
{
|
||||
s = pop_1st_bit(&b);
|
||||
idx = compute_index(s, pos.blackKingSquare, pos.pawnSquare, BLACK);
|
||||
|
||||
switch (bb[idx]) {
|
||||
|
||||
case RESULT_LOSS:
|
||||
return RESULT_WIN;
|
||||
|
||||
case RESULT_UNKNOWN:
|
||||
unknownFound = true;
|
||||
|
||||
case RESULT_DRAW:
|
||||
case RESULT_INVALID:
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
// Pawn moves
|
||||
if (square_rank(pos.pawnSquare) < RANK_7)
|
||||
{
|
||||
s = pos.pawnSquare + DELTA_N;
|
||||
idx = compute_index(pos.whiteKingSquare, pos.blackKingSquare, s, BLACK);
|
||||
|
||||
switch (bb[idx]) {
|
||||
|
||||
case RESULT_LOSS:
|
||||
return RESULT_WIN;
|
||||
|
||||
case RESULT_UNKNOWN:
|
||||
unknownFound = true;
|
||||
|
||||
case RESULT_DRAW:
|
||||
case RESULT_INVALID:
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
|
||||
// Double pawn push
|
||||
if ( square_rank(s) == RANK_3
|
||||
&& s != pos.whiteKingSquare
|
||||
&& s != pos.blackKingSquare)
|
||||
{
|
||||
s += DELTA_N;
|
||||
idx = compute_index(pos.whiteKingSquare, pos.blackKingSquare, s, BLACK);
|
||||
|
||||
switch (bb[idx]) {
|
||||
|
||||
case RESULT_LOSS:
|
||||
return RESULT_WIN;
|
||||
|
||||
case RESULT_UNKNOWN:
|
||||
unknownFound = true;
|
||||
|
||||
case RESULT_DRAW:
|
||||
case RESULT_INVALID:
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
return unknownFound ? RESULT_UNKNOWN : RESULT_DRAW;
|
||||
}
|
||||
|
||||
|
||||
Result classify_btm(const KPKPosition& pos, const Result bb[]) {
|
||||
|
||||
// If one move leads to a position classified as RESULT_DRAW, the result
|
||||
// of the current position is RESULT_DRAW. If all moves lead to positions
|
||||
// classified as RESULT_WIN, the current position is classified as
|
||||
// RESULT_LOSS. Otherwise, the current position is classified as
|
||||
// RESULT_UNKNOWN.
|
||||
|
||||
bool unknownFound = false;
|
||||
Bitboard b;
|
||||
Square s;
|
||||
int idx;
|
||||
|
||||
// King moves
|
||||
b = pos.bk_attacks();
|
||||
while (b)
|
||||
{
|
||||
s = pop_1st_bit(&b);
|
||||
idx = compute_index(pos.whiteKingSquare, s, pos.pawnSquare, WHITE);
|
||||
|
||||
switch (bb[idx]) {
|
||||
|
||||
case RESULT_DRAW:
|
||||
return RESULT_DRAW;
|
||||
|
||||
case RESULT_UNKNOWN:
|
||||
unknownFound = true;
|
||||
|
||||
case RESULT_WIN:
|
||||
case RESULT_INVALID:
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
return unknownFound ? RESULT_UNKNOWN : RESULT_LOSS;
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace
|
||||
|
||||
+267
-475
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,531 +17,323 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "bitcount.h"
|
||||
#include "misc.h"
|
||||
#include "rkiss.h"
|
||||
|
||||
CACHE_LINE_ALIGNMENT
|
||||
|
||||
#if defined(IS_64BIT)
|
||||
Bitboard RMasks[SQUARE_NB];
|
||||
Bitboard RMagics[SQUARE_NB];
|
||||
Bitboard* RAttacks[SQUARE_NB];
|
||||
unsigned RShifts[SQUARE_NB];
|
||||
|
||||
const uint64_t BMult[64] = {
|
||||
0x440049104032280ULL, 0x1021023c82008040ULL, 0x404040082000048ULL,
|
||||
0x48c4440084048090ULL, 0x2801104026490000ULL, 0x4100880442040800ULL,
|
||||
0x181011002e06040ULL, 0x9101004104200e00ULL, 0x1240848848310401ULL,
|
||||
0x2000142828050024ULL, 0x1004024d5000ULL, 0x102044400800200ULL,
|
||||
0x8108108820112000ULL, 0xa880818210c00046ULL, 0x4008008801082000ULL,
|
||||
0x60882404049400ULL, 0x104402004240810ULL, 0xa002084250200ULL,
|
||||
0x100b0880801100ULL, 0x4080201220101ULL, 0x44008080a00000ULL,
|
||||
0x202200842000ULL, 0x5006004882d00808ULL, 0x200045080802ULL,
|
||||
0x86100020200601ULL, 0xa802080a20112c02ULL, 0x80411218080900ULL,
|
||||
0x200a0880080a0ULL, 0x9a01010000104000ULL, 0x28008003100080ULL,
|
||||
0x211021004480417ULL, 0x401004188220806ULL, 0x825051400c2006ULL,
|
||||
0x140c0210943000ULL, 0x242800300080ULL, 0xc2208120080200ULL,
|
||||
0x2430008200002200ULL, 0x1010100112008040ULL, 0x8141050100020842ULL,
|
||||
0x822081014405ULL, 0x800c049e40400804ULL, 0x4a0404028a000820ULL,
|
||||
0x22060201041200ULL, 0x360904200840801ULL, 0x881a08208800400ULL,
|
||||
0x60202c00400420ULL, 0x1204440086061400ULL, 0x8184042804040ULL,
|
||||
0x64040315300400ULL, 0xc01008801090a00ULL, 0x808010401140c00ULL,
|
||||
0x4004830c2020040ULL, 0x80005002020054ULL, 0x40000c14481a0490ULL,
|
||||
0x10500101042048ULL, 0x1010100200424000ULL, 0x640901901040ULL,
|
||||
0xa0201014840ULL, 0x840082aa011002ULL, 0x10010840084240aULL,
|
||||
0x420400810420608ULL, 0x8d40230408102100ULL, 0x4a00200612222409ULL,
|
||||
0xa08520292120600ULL
|
||||
};
|
||||
Bitboard BMasks[SQUARE_NB];
|
||||
Bitboard BMagics[SQUARE_NB];
|
||||
Bitboard* BAttacks[SQUARE_NB];
|
||||
unsigned BShifts[SQUARE_NB];
|
||||
|
||||
const uint64_t RMult[64] = {
|
||||
0xa8002c000108020ULL, 0x4440200140003000ULL, 0x8080200010011880ULL,
|
||||
0x380180080141000ULL, 0x1a00060008211044ULL, 0x410001000a0c0008ULL,
|
||||
0x9500060004008100ULL, 0x100024284a20700ULL, 0x802140008000ULL,
|
||||
0x80c01002a00840ULL, 0x402004282011020ULL, 0x9862000820420050ULL,
|
||||
0x1001448011100ULL, 0x6432800200800400ULL, 0x40100010002000cULL,
|
||||
0x2800d0010c080ULL, 0x90c0008000803042ULL, 0x4010004000200041ULL,
|
||||
0x3010010200040ULL, 0xa40828028001000ULL, 0x123010008000430ULL,
|
||||
0x24008004020080ULL, 0x60040001104802ULL, 0x582200028400d1ULL,
|
||||
0x4000802080044000ULL, 0x408208200420308ULL, 0x610038080102000ULL,
|
||||
0x3601000900100020ULL, 0x80080040180ULL, 0xc2020080040080ULL,
|
||||
0x80084400100102ULL, 0x4022408200014401ULL, 0x40052040800082ULL,
|
||||
0xb08200280804000ULL, 0x8a80a008801000ULL, 0x4000480080801000ULL,
|
||||
0x911808800801401ULL, 0x822a003002001894ULL, 0x401068091400108aULL,
|
||||
0x4a10a00004cULL, 0x2000800640008024ULL, 0x1486408102020020ULL,
|
||||
0x100a000d50041ULL, 0x810050020b0020ULL, 0x204000800808004ULL,
|
||||
0x20048100a000cULL, 0x112000831020004ULL, 0x9000040810002ULL,
|
||||
0x440490200208200ULL, 0x8910401000200040ULL, 0x6404200050008480ULL,
|
||||
0x4b824a2010010100ULL, 0x4080801810c0080ULL, 0x400802a0080ULL,
|
||||
0x8224080110026400ULL, 0x40002c4104088200ULL, 0x1002100104a0282ULL,
|
||||
0x1208400811048021ULL, 0x3201014a40d02001ULL, 0x5100019200501ULL,
|
||||
0x101000208001005ULL, 0x2008450080702ULL, 0x1002080301d00cULL,
|
||||
0x410201ce5c030092ULL
|
||||
};
|
||||
Bitboard SquareBB[SQUARE_NB];
|
||||
Bitboard FileBB[FILE_NB];
|
||||
Bitboard RankBB[RANK_NB];
|
||||
Bitboard AdjacentFilesBB[FILE_NB];
|
||||
Bitboard InFrontBB[COLOR_NB][RANK_NB];
|
||||
Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
|
||||
Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
|
||||
Bitboard DistanceRingsBB[SQUARE_NB][8];
|
||||
Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
|
||||
Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
|
||||
Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
|
||||
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
||||
|
||||
const int BShift[64] = {
|
||||
58, 59, 59, 59, 59, 59, 59, 58, 59, 59, 59, 59, 59, 59, 59, 59,
|
||||
59, 59, 57, 57, 57, 57, 59, 59, 59, 59, 57, 55, 55, 57, 59, 59,
|
||||
59, 59, 57, 55, 55, 57, 59, 59, 59, 59, 57, 57, 57, 57, 59, 59,
|
||||
59, 59, 59, 59, 59, 59, 59, 59, 58, 59, 59, 59, 59, 59, 59, 58
|
||||
};
|
||||
|
||||
const int RShift[64] = {
|
||||
52, 53, 53, 53, 53, 53, 53, 52, 53, 54, 54, 54, 54, 54, 54, 53,
|
||||
53, 54, 54, 54, 54, 54, 54, 53, 53, 54, 54, 54, 54, 54, 54, 53,
|
||||
53, 54, 54, 54, 54, 54, 54, 53, 53, 54, 54, 54, 54, 54, 54, 53,
|
||||
53, 54, 54, 54, 54, 54, 54, 53, 52, 53, 53, 53, 53, 53, 53, 52
|
||||
};
|
||||
|
||||
#else // if !defined(IS_64BIT)
|
||||
|
||||
const uint64_t BMult[64] = {
|
||||
0x54142844c6a22981ULL, 0x710358a6ea25c19eULL, 0x704f746d63a4a8dcULL,
|
||||
0xbfed1a0b80f838c5ULL, 0x90561d5631e62110ULL, 0x2804260376e60944ULL,
|
||||
0x84a656409aa76871ULL, 0xf0267f64c28b6197ULL, 0x70764ebb762f0585ULL,
|
||||
0x92aa09e0cfe161deULL, 0x41ee1f6bb266f60eULL, 0xddcbf04f6039c444ULL,
|
||||
0x5a3fab7bac0d988aULL, 0xd3727877fa4eaa03ULL, 0xd988402d868ddaaeULL,
|
||||
0x812b291afa075c7cULL, 0x94faf987b685a932ULL, 0x3ed867d8470d08dbULL,
|
||||
0x92517660b8901de8ULL, 0x2d97e43e058814b4ULL, 0x880a10c220b25582ULL,
|
||||
0xc7c6520d1f1a0477ULL, 0xdbfc7fbcd7656aa6ULL, 0x78b1b9bfb1a2b84fULL,
|
||||
0x2f20037f112a0bc1ULL, 0x657171ea2269a916ULL, 0xc08302b07142210eULL,
|
||||
0x880a4403064080bULL, 0x3602420842208c00ULL, 0x852800dc7e0b6602ULL,
|
||||
0x595a3fbbaa0f03b2ULL, 0x9f01411558159d5eULL, 0x2b4a4a5f88b394f2ULL,
|
||||
0x4afcbffc292dd03aULL, 0x4a4094a3b3f10522ULL, 0xb06f00b491f30048ULL,
|
||||
0xd5b3820280d77004ULL, 0x8b2e01e7c8e57a75ULL, 0x2d342794e886c2e6ULL,
|
||||
0xc302c410cde21461ULL, 0x111f426f1379c274ULL, 0xe0569220abb31588ULL,
|
||||
0x5026d3064d453324ULL, 0xe2076040c343cd8aULL, 0x93efd1e1738021eeULL,
|
||||
0xb680804bed143132ULL, 0x44e361b21986944cULL, 0x44c60170ef5c598cULL,
|
||||
0xf4da475c195c9c94ULL, 0xa3afbb5f72060b1dULL, 0xbc75f410e41c4ffcULL,
|
||||
0xb51c099390520922ULL, 0x902c011f8f8ec368ULL, 0x950b56b3d6f5490aULL,
|
||||
0x3909e0635bf202d0ULL, 0x5744f90206ec10ccULL, 0xdc59fd76317abbc1ULL,
|
||||
0x881c7c67fcbfc4f6ULL, 0x47ca41e7e440d423ULL, 0xeb0c88112048d004ULL,
|
||||
0x51c60e04359aef1aULL, 0x1aa1fe0e957a5554ULL, 0xdd9448db4f5e3104ULL,
|
||||
0xdc01f6dca4bebbdcULL,
|
||||
};
|
||||
|
||||
const uint64_t RMult[64] = {
|
||||
0xd7445cdec88002c0ULL, 0xd0a505c1f2001722ULL, 0xe065d1c896002182ULL,
|
||||
0x9a8c41e75a000892ULL, 0x8900b10c89002aa8ULL, 0x9b28d1c1d60005a2ULL,
|
||||
0x15d6c88de002d9aULL, 0xb1dbfc802e8016a9ULL, 0x149a1042d9d60029ULL,
|
||||
0xb9c08050599e002fULL, 0x132208c3af300403ULL, 0xc1000ce2e9c50070ULL,
|
||||
0x9d9aa13c99020012ULL, 0xb6b078daf71e0046ULL, 0x9d880182fb6e002eULL,
|
||||
0x52889f467e850037ULL, 0xda6dc008d19a8480ULL, 0x468286034f902420ULL,
|
||||
0x7140ac09dc54c020ULL, 0xd76ffffa39548808ULL, 0xea901c4141500808ULL,
|
||||
0xc91004093f953a02ULL, 0x2882afa8f6bb402ULL, 0xaebe335692442c01ULL,
|
||||
0xe904a22079fb91eULL, 0x13a514851055f606ULL, 0x76c782018c8fe632ULL,
|
||||
0x1dc012a9d116da06ULL, 0x3c9e0037264fffa6ULL, 0x2036002853c6e4a2ULL,
|
||||
0xe3fe08500afb47d4ULL, 0xf38af25c86b025c2ULL, 0xc0800e2182cf9a40ULL,
|
||||
0x72002480d1f60673ULL, 0x2500200bae6e9b53ULL, 0xc60018c1eefca252ULL,
|
||||
0x600590473e3608aULL, 0x46002c4ab3fe51b2ULL, 0xa200011486bcc8d2ULL,
|
||||
0xb680078095784c63ULL, 0x2742002639bf11aeULL, 0xc7d60021a5bdb142ULL,
|
||||
0xc8c04016bb83d820ULL, 0xbd520028123b4842ULL, 0x9d1600344ac2a832ULL,
|
||||
0x6a808005631c8a05ULL, 0x604600a148d5389aULL, 0xe2e40103d40dea65ULL,
|
||||
0x945b5a0087c62a81ULL, 0x12dc200cd82d28eULL, 0x2431c600b5f9ef76ULL,
|
||||
0xfb142a006a9b314aULL, 0x6870e00a1c97d62ULL, 0x2a9db2004a2689a2ULL,
|
||||
0xd3594600caf5d1a2ULL, 0xee0e4900439344a7ULL, 0x89c4d266ca25007aULL,
|
||||
0x3e0013a2743f97e3ULL, 0x180e31a0431378aULL, 0x3a9e465a4d42a512ULL,
|
||||
0x98d0a11a0c0d9cc2ULL, 0x8e711c1aba19b01eULL, 0x8dcdc836dd201142ULL,
|
||||
0x5ac08a4735370479ULL,
|
||||
};
|
||||
|
||||
const int BShift[64] = {
|
||||
26, 27, 27, 27, 27, 27, 27, 26, 27, 27, 27, 27, 27, 27, 27, 27,
|
||||
27, 27, 25, 25, 25, 25, 27, 27, 27, 27, 25, 23, 23, 25, 27, 27,
|
||||
27, 27, 25, 23, 23, 25, 27, 27, 27, 27, 25, 25, 25, 25, 27, 27,
|
||||
27, 27, 27, 27, 27, 27, 27, 27, 26, 27, 27, 27, 27, 27, 27, 26
|
||||
};
|
||||
|
||||
const int RShift[64] = {
|
||||
20, 21, 21, 21, 21, 21, 21, 20, 21, 22, 22, 22, 22, 22, 22, 21,
|
||||
21, 22, 22, 22, 22, 22, 22, 21, 21, 22, 22, 22, 22, 22, 22, 21,
|
||||
21, 22, 22, 22, 22, 22, 22, 21, 21, 22, 22, 22, 22, 22, 22, 21,
|
||||
21, 22, 22, 22, 22, 22, 22, 21, 20, 21, 21, 21, 21, 21, 21, 20
|
||||
};
|
||||
|
||||
#endif // defined(IS_64BIT)
|
||||
|
||||
const Bitboard LightSquaresBB = 0x55AA55AA55AA55AAULL;
|
||||
const Bitboard DarkSquaresBB = 0xAA55AA55AA55AA55ULL;
|
||||
|
||||
const Bitboard SquaresByColorBB[2] = { DarkSquaresBB, LightSquaresBB };
|
||||
|
||||
const Bitboard FileBB[8] = {
|
||||
FileABB, FileBBB, FileCBB, FileDBB, FileEBB, FileFBB, FileGBB, FileHBB
|
||||
};
|
||||
|
||||
const Bitboard NeighboringFilesBB[8] = {
|
||||
FileBBB, FileABB|FileCBB, FileBBB|FileDBB, FileCBB|FileEBB,
|
||||
FileDBB|FileFBB, FileEBB|FileGBB, FileFBB|FileHBB, FileGBB
|
||||
};
|
||||
|
||||
const Bitboard ThisAndNeighboringFilesBB[8] = {
|
||||
FileABB|FileBBB, FileABB|FileBBB|FileCBB,
|
||||
FileBBB|FileCBB|FileDBB, FileCBB|FileDBB|FileEBB,
|
||||
FileDBB|FileEBB|FileFBB, FileEBB|FileFBB|FileGBB,
|
||||
FileFBB|FileGBB|FileHBB, FileGBB|FileHBB
|
||||
};
|
||||
|
||||
const Bitboard RankBB[8] = {
|
||||
Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB
|
||||
};
|
||||
|
||||
const Bitboard RelativeRankBB[2][8] = {
|
||||
{ Rank1BB, Rank2BB, Rank3BB, Rank4BB, Rank5BB, Rank6BB, Rank7BB, Rank8BB },
|
||||
{ Rank8BB, Rank7BB, Rank6BB, Rank5BB, Rank4BB, Rank3BB, Rank2BB, Rank1BB }
|
||||
};
|
||||
|
||||
const Bitboard InFrontBB[2][8] = {
|
||||
{ Rank2BB | Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank3BB | Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank4BB | Rank5BB | Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank5BB | Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank6BB | Rank7BB | Rank8BB,
|
||||
Rank7BB | Rank8BB,
|
||||
Rank8BB,
|
||||
EmptyBoardBB
|
||||
},
|
||||
{ EmptyBoardBB,
|
||||
Rank1BB,
|
||||
Rank2BB | Rank1BB,
|
||||
Rank3BB | Rank2BB | Rank1BB,
|
||||
Rank4BB | Rank3BB | Rank2BB | Rank1BB,
|
||||
Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
|
||||
Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB,
|
||||
Rank7BB | Rank6BB | Rank5BB | Rank4BB | Rank3BB | Rank2BB | Rank1BB
|
||||
}
|
||||
};
|
||||
|
||||
Bitboard RMask[64];
|
||||
int RAttackIndex[64];
|
||||
Bitboard RAttacks[0x19000];
|
||||
|
||||
Bitboard BMask[64];
|
||||
int BAttackIndex[64];
|
||||
Bitboard BAttacks[0x1480];
|
||||
|
||||
Bitboard SetMaskBB[65];
|
||||
Bitboard ClearMaskBB[65];
|
||||
|
||||
Bitboard StepAttackBB[16][64];
|
||||
Bitboard BetweenBB[64][64];
|
||||
|
||||
Bitboard SquaresInFrontMask[2][64];
|
||||
Bitboard PassedPawnMask[2][64];
|
||||
Bitboard AttackSpanMask[2][64];
|
||||
|
||||
Bitboard BishopPseudoAttacks[64];
|
||||
Bitboard RookPseudoAttacks[64];
|
||||
Bitboard QueenPseudoAttacks[64];
|
||||
|
||||
uint8_t BitCount8Bit[256];
|
||||
|
||||
|
||||
////
|
||||
//// Local definitions
|
||||
////
|
||||
int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||
|
||||
namespace {
|
||||
|
||||
void init_masks();
|
||||
void init_attacks();
|
||||
void init_between_bitboards();
|
||||
void init_pseudo_attacks();
|
||||
SquareDelta squares_delta(Square orig, Square dest);
|
||||
Bitboard index_to_bitboard(int index, Bitboard mask);
|
||||
Bitboard sliding_attacks(int sq, Bitboard block, int dirs, int deltas[][2],
|
||||
int fmin, int fmax, int rmin, int rmax);
|
||||
void init_sliding_attacks(Bitboard attacks[], int attackIndex[], Bitboard mask[],
|
||||
const int shift[], const Bitboard mult[], int deltas[][2]);
|
||||
}
|
||||
// De Bruijn sequences. See chessprogramming.wikispaces.com/BitScan
|
||||
const uint64_t DeBruijn_64 = 0x3F79D71B4CB0A89ULL;
|
||||
const uint32_t DeBruijn_32 = 0x783A9B23;
|
||||
|
||||
CACHE_LINE_ALIGNMENT
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
int MS1BTable[256];
|
||||
Square BSFTable[SQUARE_NB];
|
||||
Bitboard RTable[0x19000]; // Storage space for rook attacks
|
||||
Bitboard BTable[0x1480]; // Storage space for bishop attacks
|
||||
|
||||
/// print_bitboard() prints a bitboard in an easily readable format to the
|
||||
/// standard output. This is sometimes useful for debugging.
|
||||
typedef unsigned (Fn)(Square, Bitboard);
|
||||
|
||||
void print_bitboard(Bitboard b) {
|
||||
void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
|
||||
Bitboard masks[], unsigned shifts[], Square deltas[], Fn index);
|
||||
|
||||
for (Rank r = RANK_8; r >= RANK_1; r--)
|
||||
{
|
||||
std::cout << "+---+---+---+---+---+---+---+---+" << std::endl;
|
||||
for (File f = FILE_A; f <= FILE_H; f++)
|
||||
std::cout << "| " << (bit_is_set(b, make_square(f, r))? 'X' : ' ') << ' ';
|
||||
FORCE_INLINE unsigned bsf_index(Bitboard b) {
|
||||
|
||||
std::cout << "|" << std::endl;
|
||||
// Matt Taylor's folding for 32 bit systems, extended to 64 bits by Kim Walisch
|
||||
b ^= (b - 1);
|
||||
return Is64Bit ? (b * DeBruijn_64) >> 58
|
||||
: ((unsigned(b) ^ unsigned(b >> 32)) * DeBruijn_32) >> 26;
|
||||
}
|
||||
std::cout << "+---+---+---+---+---+---+---+---+" << std::endl;
|
||||
}
|
||||
|
||||
/// lsb()/msb() finds the least/most significant bit in a nonzero bitboard.
|
||||
/// pop_lsb() finds and clears the least significant bit in a nonzero bitboard.
|
||||
|
||||
#ifndef USE_BSFQ
|
||||
|
||||
Square lsb(Bitboard b) { return BSFTable[bsf_index(b)]; }
|
||||
|
||||
Square pop_lsb(Bitboard* b) {
|
||||
|
||||
Bitboard bb = *b;
|
||||
*b = bb & (bb - 1);
|
||||
return BSFTable[bsf_index(bb)];
|
||||
}
|
||||
|
||||
Square msb(Bitboard b) {
|
||||
|
||||
unsigned b32;
|
||||
int result = 0;
|
||||
|
||||
if (b > 0xFFFFFFFF)
|
||||
{
|
||||
b >>= 32;
|
||||
result = 32;
|
||||
}
|
||||
|
||||
b32 = unsigned(b);
|
||||
|
||||
if (b32 > 0xFFFF)
|
||||
{
|
||||
b32 >>= 16;
|
||||
result += 16;
|
||||
}
|
||||
|
||||
if (b32 > 0xFF)
|
||||
{
|
||||
b32 >>= 8;
|
||||
result += 8;
|
||||
}
|
||||
|
||||
return (Square)(result + MS1BTable[b32]);
|
||||
}
|
||||
|
||||
#endif // ifndef USE_BSFQ
|
||||
|
||||
|
||||
/// Bitboards::print() prints a bitboard in an easily readable format to the
|
||||
/// standard output. This is sometimes useful for debugging.
|
||||
|
||||
void Bitboards::print(Bitboard b) {
|
||||
|
||||
sync_cout;
|
||||
|
||||
for (Rank rank = RANK_8; rank >= RANK_1; rank--)
|
||||
{
|
||||
std::cout << "+---+---+---+---+---+---+---+---+" << '\n';
|
||||
|
||||
for (File file = FILE_A; file <= FILE_H; file++)
|
||||
std::cout << "| " << (b & (file | rank) ? "X " : " ");
|
||||
|
||||
std::cout << "|\n";
|
||||
}
|
||||
std::cout << "+---+---+---+---+---+---+---+---+" << sync_endl;
|
||||
}
|
||||
|
||||
|
||||
/// init_bitboards() initializes various bitboard arrays. It is called during
|
||||
/// Bitboards::init() initializes various bitboard arrays. It is called during
|
||||
/// program initialization.
|
||||
|
||||
void init_bitboards() {
|
||||
void Bitboards::init() {
|
||||
|
||||
int rookDeltas[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
|
||||
int bishopDeltas[4][2] = {{1,1},{-1,1},{1,-1},{-1,-1}};
|
||||
for (int k = 0, i = 0; i < 8; i++)
|
||||
while (k < (2 << i))
|
||||
MS1BTable[k++] = i;
|
||||
|
||||
init_masks();
|
||||
init_attacks();
|
||||
init_between_bitboards();
|
||||
init_sliding_attacks(RAttacks, RAttackIndex, RMask, RShift, RMult, rookDeltas);
|
||||
init_sliding_attacks(BAttacks, BAttackIndex, BMask, BShift, BMult, bishopDeltas);
|
||||
init_pseudo_attacks();
|
||||
for (int i = 0; i < 64; i++)
|
||||
BSFTable[bsf_index(1ULL << i)] = Square(i);
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
SquareBB[s] = 1ULL << s;
|
||||
|
||||
FileBB[FILE_A] = FileABB;
|
||||
RankBB[RANK_1] = Rank1BB;
|
||||
|
||||
for (int i = 1; i < 8; i++)
|
||||
{
|
||||
FileBB[i] = FileBB[i - 1] << 1;
|
||||
RankBB[i] = RankBB[i - 1] << 8;
|
||||
}
|
||||
|
||||
for (File f = FILE_A; f <= FILE_H; f++)
|
||||
AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0);
|
||||
|
||||
for (Rank r = RANK_1; r < RANK_8; r++)
|
||||
InFrontBB[WHITE][r] = ~(InFrontBB[BLACK][r + 1] = InFrontBB[BLACK][r] | RankBB[r]);
|
||||
|
||||
for (Color c = WHITE; c <= BLACK; c++)
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
{
|
||||
ForwardBB[c][s] = InFrontBB[c][rank_of(s)] & FileBB[file_of(s)];
|
||||
PawnAttackSpan[c][s] = InFrontBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
|
||||
PassedPawnMask[c][s] = ForwardBB[c][s] | PawnAttackSpan[c][s];
|
||||
}
|
||||
|
||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
|
||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++)
|
||||
{
|
||||
SquareDistance[s1][s2] = std::max(file_distance(s1, s2), rank_distance(s1, s2));
|
||||
if (s1 != s2)
|
||||
DistanceRingsBB[s1][SquareDistance[s1][s2] - 1] |= s2;
|
||||
}
|
||||
|
||||
int steps[][9] = { {}, { 7, 9 }, { 17, 15, 10, 6, -6, -10, -15, -17 },
|
||||
{}, {}, {}, { 9, 7, -7, -9, 8, 1, -1, -8 } };
|
||||
|
||||
for (Color c = WHITE; c <= BLACK; c++)
|
||||
for (PieceType pt = PAWN; pt <= KING; pt++)
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
for (int k = 0; steps[pt][k]; k++)
|
||||
{
|
||||
Square to = s + Square(c == WHITE ? steps[pt][k] : -steps[pt][k]);
|
||||
|
||||
if (is_ok(to) && square_distance(s, to) < 3)
|
||||
StepAttacksBB[make_piece(c, pt)][s] |= to;
|
||||
}
|
||||
|
||||
Square RDeltas[] = { DELTA_N, DELTA_E, DELTA_S, DELTA_W };
|
||||
Square BDeltas[] = { DELTA_NE, DELTA_SE, DELTA_SW, DELTA_NW };
|
||||
|
||||
init_magics(RTable, RAttacks, RMagics, RMasks, RShifts, RDeltas, magic_index<ROOK>);
|
||||
init_magics(BTable, BAttacks, BMagics, BMasks, BShifts, BDeltas, magic_index<BISHOP>);
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
{
|
||||
PseudoAttacks[QUEEN][s] = PseudoAttacks[BISHOP][s] = attacks_bb<BISHOP>(s, 0);
|
||||
PseudoAttacks[QUEEN][s] |= PseudoAttacks[ ROOK][s] = attacks_bb< ROOK>(s, 0);
|
||||
}
|
||||
|
||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
|
||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++)
|
||||
if (PseudoAttacks[QUEEN][s1] & s2)
|
||||
{
|
||||
Square delta = (s2 - s1) / square_distance(s1, s2);
|
||||
|
||||
for (Square s = s1 + delta; s != s2; s += delta)
|
||||
BetweenBB[s1][s2] |= s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// first_1() finds the least significant nonzero bit in a nonzero bitboard.
|
||||
/// pop_1st_bit() finds and clears the least significant nonzero bit in a
|
||||
/// nonzero bitboard.
|
||||
|
||||
#if defined(IS_64BIT) && !defined(USE_BSFQ)
|
||||
|
||||
static CACHE_LINE_ALIGNMENT
|
||||
const int BitTable[64] = {
|
||||
0, 1, 2, 7, 3, 13, 8, 19, 4, 25, 14, 28, 9, 34, 20, 40, 5, 17, 26, 38, 15,
|
||||
46, 29, 48, 10, 31, 35, 54, 21, 50, 41, 57, 63, 6, 12, 18, 24, 27, 33, 39,
|
||||
16, 37, 45, 47, 30, 53, 49, 56, 62, 11, 23, 32, 36, 44, 52, 55, 61, 22, 43,
|
||||
51, 60, 42, 59, 58
|
||||
};
|
||||
|
||||
Square first_1(Bitboard b) {
|
||||
return Square(BitTable[((b & -b) * 0x218a392cd3d5dbfULL) >> 58]);
|
||||
}
|
||||
|
||||
Square pop_1st_bit(Bitboard* b) {
|
||||
Bitboard bb = *b;
|
||||
*b &= (*b - 1);
|
||||
return Square(BitTable[((bb & -bb) * 0x218a392cd3d5dbfULL) >> 58]);
|
||||
}
|
||||
|
||||
#elif !defined(USE_BSFQ)
|
||||
|
||||
static CACHE_LINE_ALIGNMENT
|
||||
const int BitTable[64] = {
|
||||
63, 30, 3, 32, 25, 41, 22, 33, 15, 50, 42, 13, 11, 53, 19, 34, 61, 29, 2,
|
||||
51, 21, 43, 45, 10, 18, 47, 1, 54, 9, 57, 0, 35, 62, 31, 40, 4, 49, 5, 52,
|
||||
26, 60, 6, 23, 44, 46, 27, 56, 16, 7, 39, 48, 24, 59, 14, 12, 55, 38, 28,
|
||||
58, 20, 37, 17, 36, 8
|
||||
};
|
||||
|
||||
Square first_1(Bitboard b) {
|
||||
|
||||
b ^= (b - 1);
|
||||
uint32_t fold = int(b) ^ int(b >> 32);
|
||||
return Square(BitTable[(fold * 0x783a9b23) >> 26]);
|
||||
}
|
||||
|
||||
// Use type-punning
|
||||
union b_union {
|
||||
|
||||
Bitboard b;
|
||||
struct {
|
||||
#if defined (BIGENDIAN)
|
||||
uint32_t h;
|
||||
uint32_t l;
|
||||
#else
|
||||
uint32_t l;
|
||||
uint32_t h;
|
||||
#endif
|
||||
} dw;
|
||||
};
|
||||
|
||||
Square pop_1st_bit(Bitboard* bb) {
|
||||
|
||||
b_union u;
|
||||
Square ret;
|
||||
|
||||
u.b = *bb;
|
||||
|
||||
if (u.dw.l)
|
||||
{
|
||||
ret = Square(BitTable[((u.dw.l ^ (u.dw.l - 1)) * 0x783a9b23) >> 26]);
|
||||
u.dw.l &= (u.dw.l - 1);
|
||||
*bb = u.b;
|
||||
return ret;
|
||||
}
|
||||
ret = Square(BitTable[((~(u.dw.h ^ (u.dw.h - 1))) * 0x783a9b23) >> 26]);
|
||||
u.dw.h &= (u.dw.h - 1);
|
||||
*bb = u.b;
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
// All functions below are used to precompute various bitboards during
|
||||
// program initialization. Some of the functions may be difficult to
|
||||
// understand, but they all seem to work correctly, and it should never
|
||||
// be necessary to touch any of them.
|
||||
Bitboard sliding_attack(Square deltas[], Square sq, Bitboard occupied) {
|
||||
|
||||
void init_masks() {
|
||||
Bitboard attack = 0;
|
||||
|
||||
SetMaskBB[SQ_NONE] = 0ULL;
|
||||
ClearMaskBB[SQ_NONE] = ~SetMaskBB[SQ_NONE];
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
{
|
||||
SetMaskBB[s] = (1ULL << s);
|
||||
ClearMaskBB[s] = ~SetMaskBB[s];
|
||||
}
|
||||
|
||||
for (Color c = WHITE; c <= BLACK; c++)
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
for (int i = 0; i < 4; i++)
|
||||
for (Square s = sq + deltas[i];
|
||||
is_ok(s) && square_distance(s, s - deltas[i]) == 1;
|
||||
s += deltas[i])
|
||||
{
|
||||
SquaresInFrontMask[c][s] = in_front_bb(c, s) & file_bb(s);
|
||||
PassedPawnMask[c][s] = in_front_bb(c, s) & this_and_neighboring_files_bb(s);
|
||||
AttackSpanMask[c][s] = in_front_bb(c, s) & neighboring_files_bb(s);
|
||||
}
|
||||
attack |= s;
|
||||
|
||||
for (Bitboard b = 0; b < 256; b++)
|
||||
BitCount8Bit[b] = (uint8_t)count_1s<CNT32>(b);
|
||||
}
|
||||
|
||||
void init_attacks() {
|
||||
|
||||
const int step[16][8] = {
|
||||
{0},
|
||||
{7,9,0}, {17,15,10,6,-6,-10,-15,-17}, {9,7,-7,-9,0}, {8,1,-1,-8,0},
|
||||
{9,7,-7,-9,8,1,-1,-8}, {9,7,-7,-9,8,1,-1,-8}, {0}, {0},
|
||||
{-7,-9,0}, {17,15,10,6,-6,-10,-15,-17}, {9,7,-7,-9,0}, {8,1,-1,-8,0},
|
||||
{9,7,-7,-9,8,1,-1,-8}, {9,7,-7,-9,8,1,-1,-8}
|
||||
};
|
||||
|
||||
for (int i = 0; i < 64; i++)
|
||||
for (int j = 0; j <= int(BK); j++)
|
||||
{
|
||||
StepAttackBB[j][i] = EmptyBoardBB;
|
||||
for (int k = 0; k < 8 && step[j][k] != 0; k++)
|
||||
{
|
||||
int l = i + step[j][k];
|
||||
if (l >= 0 && l < 64 && abs((i & 7) - (l & 7)) < 3)
|
||||
StepAttackBB[j][i] |= (1ULL << l);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Bitboard sliding_attacks(int sq, Bitboard block, int dirs, int deltas[][2],
|
||||
int fmin=0, int fmax=7, int rmin=0, int rmax=7) {
|
||||
Bitboard result = 0ULL;
|
||||
int rk = sq / 8;
|
||||
int fl = sq % 8;
|
||||
|
||||
for (int i = 0; i < dirs; i++)
|
||||
{
|
||||
int dx = deltas[i][0];
|
||||
int dy = deltas[i][1];
|
||||
int f = fl + dx;
|
||||
int r = rk + dy;
|
||||
|
||||
while ( (dx == 0 || (f >= fmin && f <= fmax))
|
||||
&& (dy == 0 || (r >= rmin && r <= rmax)))
|
||||
{
|
||||
result |= (1ULL << (f + r*8));
|
||||
if (block & (1ULL << (f + r*8)))
|
||||
if (occupied & s)
|
||||
break;
|
||||
|
||||
f += dx;
|
||||
r += dy;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
return attack;
|
||||
}
|
||||
|
||||
SquareDelta squares_delta(Square orig, Square dest) {
|
||||
|
||||
const SquareDelta deltas[] = { DELTA_N, DELTA_NE, DELTA_E, DELTA_SE,
|
||||
DELTA_S, DELTA_SW, DELTA_W, DELTA_NW };
|
||||
Bitboard pick_random(RKISS& rk, int booster) {
|
||||
|
||||
for (int idx = 0; idx < 8; idx++)
|
||||
{
|
||||
Square s = orig + deltas[idx];
|
||||
// Values s1 and s2 are used to rotate the candidate magic of a
|
||||
// quantity known to be the optimal to quickly find the magics.
|
||||
int s1 = booster & 63, s2 = (booster >> 6) & 63;
|
||||
|
||||
while (square_is_ok(s) && square_distance(s, s - deltas[idx]) == 1)
|
||||
{
|
||||
if (s == dest)
|
||||
return deltas[idx];
|
||||
|
||||
s += deltas[idx];
|
||||
}
|
||||
}
|
||||
return DELTA_NONE;
|
||||
Bitboard m = rk.rand<Bitboard>();
|
||||
m = (m >> s1) | (m << (64 - s1));
|
||||
m &= rk.rand<Bitboard>();
|
||||
m = (m >> s2) | (m << (64 - s2));
|
||||
return m & rk.rand<Bitboard>();
|
||||
}
|
||||
|
||||
void init_between_bitboards() {
|
||||
|
||||
Square s1, s2, s3;
|
||||
SquareDelta d;
|
||||
// init_magics() computes all rook and bishop attacks at startup. Magic
|
||||
// bitboards are used to look up attacks of sliding pieces. As a reference see
|
||||
// chessprogramming.wikispaces.com/Magic+Bitboards. In particular, here we
|
||||
// use the so called "fancy" approach.
|
||||
|
||||
for (s1 = SQ_A1; s1 <= SQ_H8; s1++)
|
||||
for (s2 = SQ_A1; s2 <= SQ_H8; s2++)
|
||||
{
|
||||
BetweenBB[s1][s2] = EmptyBoardBB;
|
||||
d = squares_delta(s1, s2);
|
||||
void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
|
||||
Bitboard masks[], unsigned shifts[], Square deltas[], Fn index) {
|
||||
|
||||
if (d != DELTA_NONE)
|
||||
for (s3 = s1 + d; s3 != s2; s3 += d)
|
||||
set_bit(&(BetweenBB[s1][s2]), s3);
|
||||
}
|
||||
}
|
||||
int MagicBoosters[][8] = { { 3191, 2184, 1310, 3618, 2091, 1308, 2452, 3996 },
|
||||
{ 1059, 3608, 605, 3234, 3326, 38, 2029, 3043 } };
|
||||
RKISS rk;
|
||||
Bitboard occupancy[4096], reference[4096], edges, b;
|
||||
int i, size, booster;
|
||||
|
||||
Bitboard index_to_bitboard(int index, Bitboard mask) {
|
||||
|
||||
Bitboard result = 0ULL;
|
||||
int bits = count_1s<CNT32>(mask);
|
||||
|
||||
for (int i = 0; i < bits; i++)
|
||||
{
|
||||
int j = pop_1st_bit(&mask);
|
||||
if (index & (1 << i))
|
||||
result |= (1ULL << j);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void init_sliding_attacks(Bitboard attacks[], int attackIndex[], Bitboard mask[],
|
||||
const int shift[], const Bitboard mult[], int deltas[][2]) {
|
||||
|
||||
for (int i = 0, index = 0; i < 64; i++)
|
||||
{
|
||||
attackIndex[i] = index;
|
||||
mask[i] = sliding_attacks(i, 0, 4, deltas, 1, 6, 1, 6);
|
||||
|
||||
#if defined(IS_64BIT)
|
||||
int j = (1 << (64 - shift[i]));
|
||||
#else
|
||||
int j = (1 << (32 - shift[i]));
|
||||
#endif
|
||||
|
||||
for (int k = 0; k < j; k++)
|
||||
{
|
||||
#if defined(IS_64BIT)
|
||||
Bitboard b = index_to_bitboard(k, mask[i]);
|
||||
attacks[index + ((b * mult[i]) >> shift[i])] = sliding_attacks(i, b, 4, deltas);
|
||||
#else
|
||||
Bitboard b = index_to_bitboard(k, mask[i]);
|
||||
unsigned v = int(b) * int(mult[i]) ^ int(b >> 32) * int(mult[i] >> 32);
|
||||
attacks[index + (v >> shift[i])] = sliding_attacks(i, b, 4, deltas);
|
||||
#endif
|
||||
}
|
||||
index += j;
|
||||
}
|
||||
}
|
||||
|
||||
void init_pseudo_attacks() {
|
||||
// attacks[s] is a pointer to the beginning of the attacks table for square 's'
|
||||
attacks[SQ_A1] = table;
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
{
|
||||
BishopPseudoAttacks[s] = bishop_attacks_bb(s, EmptyBoardBB);
|
||||
RookPseudoAttacks[s] = rook_attacks_bb(s, EmptyBoardBB);
|
||||
QueenPseudoAttacks[s] = queen_attacks_bb(s, EmptyBoardBB);
|
||||
// Board edges are not considered in the relevant occupancies
|
||||
edges = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));
|
||||
|
||||
// Given a square 's', the mask is the bitboard of sliding attacks from
|
||||
// 's' computed on an empty board. The index must be big enough to contain
|
||||
// all the attacks for each possible subset of the mask and so is 2 power
|
||||
// the number of 1s of the mask. Hence we deduce the size of the shift to
|
||||
// apply to the 64 or 32 bits word to get the index.
|
||||
masks[s] = sliding_attack(deltas, s, 0) & ~edges;
|
||||
shifts[s] = (Is64Bit ? 64 : 32) - popcount<Max15>(masks[s]);
|
||||
|
||||
// Use Carry-Rippler trick to enumerate all subsets of masks[s] and
|
||||
// store the corresponding sliding attack bitboard in reference[].
|
||||
b = size = 0;
|
||||
do {
|
||||
occupancy[size] = b;
|
||||
reference[size++] = sliding_attack(deltas, s, b);
|
||||
b = (b - masks[s]) & masks[s];
|
||||
} while (b);
|
||||
|
||||
// Set the offset for the table of the next square. We have individual
|
||||
// table sizes for each square with "Fancy Magic Bitboards".
|
||||
if (s < SQ_H8)
|
||||
attacks[s + 1] = attacks[s] + size;
|
||||
|
||||
booster = MagicBoosters[Is64Bit][rank_of(s)];
|
||||
|
||||
// Find a magic for square 's' picking up an (almost) random number
|
||||
// until we find the one that passes the verification test.
|
||||
do {
|
||||
do magics[s] = pick_random(rk, booster);
|
||||
while (popcount<Max15>((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
|
||||
// looks up the correct sliding attack in the attacks[s] database.
|
||||
// Note that we build up the database for square 's' as a side
|
||||
// effect of verifying the magic.
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
Bitboard& attack = attacks[s][index(s, occupancy[i])];
|
||||
|
||||
if (attack && attack != reference[i])
|
||||
break;
|
||||
|
||||
assert(reference[i] != 0);
|
||||
|
||||
attack = reference[i];
|
||||
}
|
||||
} while (i != size);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+190
-211
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -18,24 +18,24 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(BITBOARD_H_INCLUDED)
|
||||
#ifndef BITBOARD_H_INCLUDED
|
||||
#define BITBOARD_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include "piece.h"
|
||||
#include "square.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace Bitboards {
|
||||
|
||||
////
|
||||
//// Constants and variables
|
||||
////
|
||||
void init();
|
||||
void print(Bitboard b);
|
||||
|
||||
const Bitboard EmptyBoardBB = 0;
|
||||
}
|
||||
|
||||
namespace Bitbases {
|
||||
|
||||
void init_kpk();
|
||||
bool probe_kpk(Square wksq, Square wpsq, Square bksq, Color us);
|
||||
|
||||
}
|
||||
|
||||
const Bitboard FileABB = 0x0101010101010101ULL;
|
||||
const Bitboard FileBBB = FileABB << 1;
|
||||
@@ -55,75 +55,88 @@ const Bitboard Rank6BB = Rank1BB << (8 * 5);
|
||||
const Bitboard Rank7BB = Rank1BB << (8 * 6);
|
||||
const Bitboard Rank8BB = Rank1BB << (8 * 7);
|
||||
|
||||
extern const Bitboard SquaresByColorBB[2];
|
||||
extern const Bitboard FileBB[8];
|
||||
extern const Bitboard NeighboringFilesBB[8];
|
||||
extern const Bitboard ThisAndNeighboringFilesBB[8];
|
||||
extern const Bitboard RankBB[8];
|
||||
extern const Bitboard RelativeRankBB[2][8];
|
||||
extern const Bitboard InFrontBB[2][8];
|
||||
CACHE_LINE_ALIGNMENT
|
||||
|
||||
extern Bitboard SetMaskBB[65];
|
||||
extern Bitboard ClearMaskBB[65];
|
||||
extern Bitboard RMasks[SQUARE_NB];
|
||||
extern Bitboard RMagics[SQUARE_NB];
|
||||
extern Bitboard* RAttacks[SQUARE_NB];
|
||||
extern unsigned RShifts[SQUARE_NB];
|
||||
|
||||
extern Bitboard StepAttackBB[16][64];
|
||||
extern Bitboard BetweenBB[64][64];
|
||||
extern Bitboard BMasks[SQUARE_NB];
|
||||
extern Bitboard BMagics[SQUARE_NB];
|
||||
extern Bitboard* BAttacks[SQUARE_NB];
|
||||
extern unsigned BShifts[SQUARE_NB];
|
||||
|
||||
extern Bitboard SquaresInFrontMask[2][64];
|
||||
extern Bitboard PassedPawnMask[2][64];
|
||||
extern Bitboard AttackSpanMask[2][64];
|
||||
extern Bitboard SquareBB[SQUARE_NB];
|
||||
extern Bitboard FileBB[FILE_NB];
|
||||
extern Bitboard RankBB[RANK_NB];
|
||||
extern Bitboard AdjacentFilesBB[FILE_NB];
|
||||
extern Bitboard InFrontBB[COLOR_NB][RANK_NB];
|
||||
extern Bitboard StepAttacksBB[PIECE_NB][SQUARE_NB];
|
||||
extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
|
||||
extern Bitboard DistanceRingsBB[SQUARE_NB][8];
|
||||
extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
|
||||
extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
|
||||
extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
|
||||
extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
|
||||
|
||||
extern const uint64_t RMult[64];
|
||||
extern const int RShift[64];
|
||||
extern Bitboard RMask[64];
|
||||
extern int RAttackIndex[64];
|
||||
extern Bitboard RAttacks[0x19000];
|
||||
extern int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||
|
||||
extern const uint64_t BMult[64];
|
||||
extern const int BShift[64];
|
||||
extern Bitboard BMask[64];
|
||||
extern int BAttackIndex[64];
|
||||
extern Bitboard BAttacks[0x1480];
|
||||
const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
|
||||
|
||||
extern Bitboard BishopPseudoAttacks[64];
|
||||
extern Bitboard RookPseudoAttacks[64];
|
||||
extern Bitboard QueenPseudoAttacks[64];
|
||||
/// 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.
|
||||
|
||||
extern uint8_t BitCount8Bit[256];
|
||||
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
/// Functions for testing whether a given bit is set in a bitboard, and for
|
||||
/// setting and clearing bits.
|
||||
|
||||
inline Bitboard bit_is_set(Bitboard b, Square s) {
|
||||
return b & SetMaskBB[s];
|
||||
inline Bitboard operator&(Bitboard b, Square s) {
|
||||
return b & SquareBB[s];
|
||||
}
|
||||
|
||||
inline void set_bit(Bitboard *b, Square s) {
|
||||
*b |= SetMaskBB[s];
|
||||
inline Bitboard& operator|=(Bitboard& b, Square s) {
|
||||
return b |= SquareBB[s];
|
||||
}
|
||||
|
||||
inline void clear_bit(Bitboard *b, Square s) {
|
||||
*b &= ClearMaskBB[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) {
|
||||
return b ^ SquareBB[s];
|
||||
}
|
||||
|
||||
inline bool more_than_one(Bitboard b) {
|
||||
return b & (b - 1);
|
||||
}
|
||||
|
||||
inline int square_distance(Square s1, Square s2) {
|
||||
return SquareDistance[s1][s2];
|
||||
}
|
||||
|
||||
inline int file_distance(Square s1, Square s2) {
|
||||
return abs(file_of(s1) - file_of(s2));
|
||||
}
|
||||
|
||||
inline int rank_distance(Square s1, Square s2) {
|
||||
return abs(rank_of(s1) - rank_of(s2));
|
||||
}
|
||||
|
||||
|
||||
/// Functions used to update a bitboard after a move. This is faster
|
||||
/// then calling a sequence of clear_bit() + set_bit()
|
||||
/// shift_bb() moves bitboard one step along direction Delta. Mainly for pawns.
|
||||
|
||||
inline Bitboard make_move_bb(Square from, Square to) {
|
||||
return SetMaskBB[from] | SetMaskBB[to];
|
||||
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;
|
||||
}
|
||||
|
||||
inline void do_move_bb(Bitboard *b, Bitboard move_bb) {
|
||||
*b ^= move_bb;
|
||||
}
|
||||
|
||||
/// rank_bb() and file_bb() take a file or a square as input, and return
|
||||
/// 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) {
|
||||
@@ -131,7 +144,7 @@ inline Bitboard rank_bb(Rank r) {
|
||||
}
|
||||
|
||||
inline Bitboard rank_bb(Square s) {
|
||||
return rank_bb(square_rank(s));
|
||||
return RankBB[rank_of(s)];
|
||||
}
|
||||
|
||||
inline Bitboard file_bb(File f) {
|
||||
@@ -139,205 +152,171 @@ inline Bitboard file_bb(File f) {
|
||||
}
|
||||
|
||||
inline Bitboard file_bb(Square s) {
|
||||
return file_bb(square_file(s));
|
||||
return FileBB[file_of(s)];
|
||||
}
|
||||
|
||||
|
||||
/// neighboring_files_bb takes a file or a square as input, and returns a
|
||||
/// bitboard representing all squares on the neighboring files.
|
||||
/// adjacent_files_bb() takes a file as input and returns a bitboard representing
|
||||
/// all squares on the adjacent files.
|
||||
|
||||
inline Bitboard neighboring_files_bb(File f) {
|
||||
return NeighboringFilesBB[f];
|
||||
}
|
||||
|
||||
inline Bitboard neighboring_files_bb(Square s) {
|
||||
return NeighboringFilesBB[square_file(s)];
|
||||
inline Bitboard adjacent_files_bb(File f) {
|
||||
return AdjacentFilesBB[f];
|
||||
}
|
||||
|
||||
|
||||
/// this_and_neighboring_files_bb takes a file or a square as input, and
|
||||
/// returns a bitboard representing all squares on the given and neighboring
|
||||
/// files.
|
||||
|
||||
inline Bitboard this_and_neighboring_files_bb(File f) {
|
||||
return ThisAndNeighboringFilesBB[f];
|
||||
}
|
||||
|
||||
inline Bitboard this_and_neighboring_files_bb(Square s) {
|
||||
return ThisAndNeighboringFilesBB[square_file(s)];
|
||||
}
|
||||
|
||||
|
||||
/// relative_rank_bb() takes a color and a rank as input, and returns a bitboard
|
||||
/// representing all squares on the given rank from the given color's point of
|
||||
/// view. For instance, relative_rank_bb(WHITE, 7) gives all squares on the
|
||||
/// 7th rank, while relative_rank_bb(BLACK, 7) gives all squares on the 2nd
|
||||
/// rank.
|
||||
|
||||
inline Bitboard relative_rank_bb(Color c, Rank r) {
|
||||
return RelativeRankBB[c][r];
|
||||
}
|
||||
|
||||
|
||||
/// in_front_bb() takes a color and a rank or square as input, and returns a
|
||||
/// bitboard representing all the squares on all ranks in front of the rank
|
||||
/// (or square), from the given color's point of view. For instance,
|
||||
/// in_front_bb(WHITE, RANK_5) will give all squares on ranks 6, 7 and 8, while
|
||||
/// in_front_bb(BLACK, SQ_D3) will give all squares on ranks 1 and 2.
|
||||
/// in_front_bb() takes a color and a rank as input, and returns a bitboard
|
||||
/// representing all the squares on all ranks in front of the rank, from the
|
||||
/// given color's point of view. For instance, in_front_bb(BLACK, RANK_3) will
|
||||
/// give all squares on ranks 1 and 2.
|
||||
|
||||
inline Bitboard in_front_bb(Color c, Rank r) {
|
||||
return InFrontBB[c][r];
|
||||
}
|
||||
|
||||
inline Bitboard in_front_bb(Color c, Square s) {
|
||||
return InFrontBB[c][square_rank(s)];
|
||||
}
|
||||
|
||||
/// between_bb() returns a bitboard representing all squares between two squares.
|
||||
/// For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with the bits for
|
||||
/// square d5 and e6 set. If s1 and s2 are not on the same line, file or diagonal,
|
||||
/// 0 is returned.
|
||||
|
||||
/// behind_bb() takes a color and a rank or square as input, and returns a
|
||||
/// bitboard representing all the squares on all ranks behind of the rank
|
||||
/// (or square), from the given color's point of view.
|
||||
|
||||
inline Bitboard behind_bb(Color c, Rank r) {
|
||||
return InFrontBB[opposite_color(c)][r];
|
||||
}
|
||||
|
||||
inline Bitboard behind_bb(Color c, Square s) {
|
||||
return InFrontBB[opposite_color(c)][square_rank(s)];
|
||||
}
|
||||
|
||||
|
||||
/// Functions for computing sliding attack bitboards. rook_attacks_bb(),
|
||||
/// bishop_attacks_bb() and queen_attacks_bb() all take a square and a
|
||||
/// bitboard of occupied squares as input, and return a bitboard representing
|
||||
/// all squares attacked by a rook, bishop or queen on the given square.
|
||||
|
||||
#if defined(IS_64BIT)
|
||||
|
||||
inline Bitboard rook_attacks_bb(Square s, Bitboard blockers) {
|
||||
Bitboard b = blockers & RMask[s];
|
||||
return RAttacks[RAttackIndex[s] + ((b * RMult[s]) >> RShift[s])];
|
||||
}
|
||||
|
||||
inline Bitboard bishop_attacks_bb(Square s, Bitboard blockers) {
|
||||
Bitboard b = blockers & BMask[s];
|
||||
return BAttacks[BAttackIndex[s] + ((b * BMult[s]) >> BShift[s])];
|
||||
}
|
||||
|
||||
#else // if !defined(IS_64BIT)
|
||||
|
||||
inline Bitboard rook_attacks_bb(Square s, Bitboard blockers) {
|
||||
Bitboard b = blockers & RMask[s];
|
||||
return RAttacks[RAttackIndex[s] +
|
||||
(unsigned(int(b) * int(RMult[s]) ^
|
||||
int(b >> 32) * int(RMult[s] >> 32))
|
||||
>> RShift[s])];
|
||||
}
|
||||
|
||||
inline Bitboard bishop_attacks_bb(Square s, Bitboard blockers) {
|
||||
Bitboard b = blockers & BMask[s];
|
||||
return BAttacks[BAttackIndex[s] +
|
||||
(unsigned(int(b) * int(BMult[s]) ^
|
||||
int(b >> 32) * int(BMult[s] >> 32))
|
||||
>> BShift[s])];
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
inline Bitboard queen_attacks_bb(Square s, Bitboard blockers) {
|
||||
return rook_attacks_bb(s, blockers) | bishop_attacks_bb(s, blockers);
|
||||
}
|
||||
|
||||
|
||||
/// squares_between returns a bitboard representing all squares between
|
||||
/// two squares. For instance, squares_between(SQ_C4, SQ_F7) returns a
|
||||
/// bitboard with the bits for square d5 and e6 set. If s1 and s2 are not
|
||||
/// on the same line, file or diagonal, EmptyBoardBB is returned.
|
||||
|
||||
inline Bitboard squares_between(Square s1, Square s2) {
|
||||
inline Bitboard between_bb(Square s1, Square s2) {
|
||||
return BetweenBB[s1][s2];
|
||||
}
|
||||
|
||||
|
||||
/// squares_in_front_of takes a color and a square as input, and returns a
|
||||
/// bitboard representing all squares along the line in front of the square,
|
||||
/// from the point of view of the given color. Definition of the table is:
|
||||
/// SquaresInFrontOf[c][s] = in_front_bb(c, s) & file_bb(s)
|
||||
/// forward_bb() takes a color and a square as input, and returns a bitboard
|
||||
/// representing all squares along the line in front of the square, from the
|
||||
/// point of view of the given color. Definition of the table is:
|
||||
/// ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s)
|
||||
|
||||
inline Bitboard squares_in_front_of(Color c, Square s) {
|
||||
return SquaresInFrontMask[c][s];
|
||||
inline Bitboard forward_bb(Color c, Square s) {
|
||||
return ForwardBB[c][s];
|
||||
}
|
||||
|
||||
|
||||
/// squares_behind is similar to squares_in_front, but returns the squares
|
||||
/// behind the square instead of in front of the square.
|
||||
/// pawn_attack_span() takes a color and a square as input, and returns a bitboard
|
||||
/// representing all squares that can be attacked by a pawn of the given color
|
||||
/// when it moves along its file starting from the given square. Definition is:
|
||||
/// PawnAttackSpan[c][s] = in_front_bb(c, s) & adjacent_files_bb(s);
|
||||
|
||||
inline Bitboard squares_behind(Color c, Square s) {
|
||||
return SquaresInFrontMask[opposite_color(c)][s];
|
||||
inline Bitboard pawn_attack_span(Color c, Square s) {
|
||||
return PawnAttackSpan[c][s];
|
||||
}
|
||||
|
||||
|
||||
/// passed_pawn_mask takes a color and a square as input, and returns a
|
||||
/// passed_pawn_mask() takes a color and a square as input, and returns a
|
||||
/// bitboard mask which can be used to test if a pawn of the given color on
|
||||
/// the given square is a passed pawn. Definition of the table is:
|
||||
/// PassedPawnMask[c][s] = in_front_bb(c, s) & this_and_neighboring_files_bb(s)
|
||||
/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_bb(c, s)
|
||||
|
||||
inline Bitboard passed_pawn_mask(Color c, Square s) {
|
||||
return PassedPawnMask[c][s];
|
||||
}
|
||||
|
||||
|
||||
/// attack_span_mask takes a color and a square as input, and returns a bitboard
|
||||
/// representing all squares that can be attacked by a pawn of the given color
|
||||
/// when it moves along its file starting from the given square. Definition is:
|
||||
/// AttackSpanMask[c][s] = in_front_bb(c, s) & neighboring_files_bb(s);
|
||||
/// squares_of_color() returns a bitboard representing all squares with the same
|
||||
/// color of the given square.
|
||||
|
||||
inline Bitboard attack_span_mask(Color c, Square s) {
|
||||
return AttackSpanMask[c][s];
|
||||
inline Bitboard squares_of_color(Square s) {
|
||||
return DarkSquares & s ? DarkSquares : ~DarkSquares;
|
||||
}
|
||||
|
||||
|
||||
/// squares_aligned returns true if the squares s1, s2 and s3 are aligned
|
||||
/// squares_aligned() returns true if the squares s1, s2 and s3 are aligned
|
||||
/// either on a straight or on a diagonal line.
|
||||
|
||||
inline bool squares_aligned(Square s1, Square s2, Square s3) {
|
||||
return (BetweenBB[s1][s2] | BetweenBB[s1][s3] | BetweenBB[s2][s3])
|
||||
& ((1ULL << s1) | (1ULL << s2) | (1ULL << s3));
|
||||
& ( SquareBB[s1] | SquareBB[s2] | SquareBB[s3]);
|
||||
}
|
||||
|
||||
|
||||
/// first_1() finds the least significant nonzero bit in a nonzero bitboard.
|
||||
/// pop_1st_bit() finds and clears the least significant nonzero bit in a
|
||||
/// nonzero bitboard.
|
||||
/// Functions for computing sliding attack bitboards. Function attacks_bb() takes
|
||||
/// a square and a bitboard of occupied squares as input, and returns a bitboard
|
||||
/// 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) {
|
||||
|
||||
#if defined(USE_BSFQ) // Assembly code by Heinz van Saanen
|
||||
Bitboard* const Masks = Pt == ROOK ? RMasks : BMasks;
|
||||
Bitboard* const Magics = Pt == ROOK ? RMagics : BMagics;
|
||||
unsigned* const Shifts = Pt == ROOK ? RShifts : BShifts;
|
||||
|
||||
inline Square first_1(Bitboard b) {
|
||||
Bitboard dummy;
|
||||
__asm__("bsfq %1, %0": "=r"(dummy): "rm"(b) );
|
||||
return (Square)(dummy);
|
||||
if (Is64Bit)
|
||||
return unsigned(((occ & Masks[s]) * Magics[s]) >> Shifts[s]);
|
||||
|
||||
unsigned lo = unsigned(occ) & unsigned(Masks[s]);
|
||||
unsigned hi = unsigned(occ >> 32) & unsigned(Masks[s] >> 32);
|
||||
return (lo * unsigned(Magics[s]) ^ hi * unsigned(Magics[s] >> 32)) >> Shifts[s];
|
||||
}
|
||||
|
||||
inline Square pop_1st_bit(Bitboard* b) {
|
||||
const Square s = first_1(*b);
|
||||
*b &= ~(1ULL<<s);
|
||||
template<PieceType Pt>
|
||||
inline Bitboard attacks_bb(Square s, Bitboard occ) {
|
||||
return (Pt == ROOK ? RAttacks : BAttacks)[s][magic_index<Pt>(s, occ)];
|
||||
}
|
||||
|
||||
|
||||
/// lsb()/msb() finds the least/most significant bit in a nonzero bitboard.
|
||||
/// pop_lsb() finds and clears the least significant bit in a nonzero bitboard.
|
||||
|
||||
#ifdef USE_BSFQ
|
||||
|
||||
# if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
|
||||
|
||||
FORCE_INLINE Square lsb(Bitboard b) {
|
||||
unsigned long index;
|
||||
_BitScanForward64(&index, b);
|
||||
return (Square) index;
|
||||
}
|
||||
|
||||
FORCE_INLINE Square msb(Bitboard b) {
|
||||
unsigned long index;
|
||||
_BitScanReverse64(&index, b);
|
||||
return (Square) index;
|
||||
}
|
||||
|
||||
# elif defined(__arm__)
|
||||
|
||||
FORCE_INLINE int lsb32(uint32_t v) {
|
||||
__asm__("rbit %0, %1" : "=r"(v) : "r"(v));
|
||||
return __builtin_clz(v);
|
||||
}
|
||||
|
||||
FORCE_INLINE Square msb(Bitboard b) {
|
||||
return (Square) (63 - __builtin_clzll(b));
|
||||
}
|
||||
|
||||
FORCE_INLINE Square lsb(Bitboard b) {
|
||||
return (Square) (uint32_t(b) ? lsb32(uint32_t(b)) : 32 + lsb32(uint32_t(b >> 32)));
|
||||
}
|
||||
|
||||
# else
|
||||
|
||||
FORCE_INLINE Square lsb(Bitboard b) { // Assembly code by Heinz van Saanen
|
||||
Bitboard index;
|
||||
__asm__("bsfq %1, %0": "=r"(index): "rm"(b) );
|
||||
return (Square) index;
|
||||
}
|
||||
|
||||
FORCE_INLINE Square msb(Bitboard b) {
|
||||
Bitboard index;
|
||||
__asm__("bsrq %1, %0": "=r"(index): "rm"(b) );
|
||||
return (Square) index;
|
||||
}
|
||||
|
||||
# endif
|
||||
|
||||
FORCE_INLINE Square pop_lsb(Bitboard* b) {
|
||||
const Square s = lsb(*b);
|
||||
*b &= *b - 1;
|
||||
return s;
|
||||
}
|
||||
|
||||
#else // if !defined(USE_BSFQ)
|
||||
#else // if defined(USE_BSFQ)
|
||||
|
||||
extern Square first_1(Bitboard b);
|
||||
extern Square pop_1st_bit(Bitboard* b);
|
||||
extern Square msb(Bitboard b);
|
||||
extern Square lsb(Bitboard b);
|
||||
extern Square pop_lsb(Bitboard* b);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
////
|
||||
//// Prototypes
|
||||
////
|
||||
|
||||
extern void print_bitboard(Bitboard b);
|
||||
extern void init_bitboards();
|
||||
|
||||
|
||||
#endif // !defined(BITBOARD_H_INCLUDED)
|
||||
#endif // #ifndef BITBOARD_H_INCLUDED
|
||||
|
||||
+59
-79
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -18,108 +18,88 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(BITCOUNT_H_INCLUDED)
|
||||
#ifndef BITCOUNT_H_INCLUDED
|
||||
#define BITCOUNT_H_INCLUDED
|
||||
|
||||
#include <cassert>
|
||||
#include "types.h"
|
||||
|
||||
enum BitCountType {
|
||||
CNT64,
|
||||
CNT64_MAX15,
|
||||
CNT32,
|
||||
CNT32_MAX15,
|
||||
CNT_POPCNT
|
||||
CNT_64,
|
||||
CNT_64_MAX15,
|
||||
CNT_32,
|
||||
CNT_32_MAX15,
|
||||
CNT_HW_POPCNT
|
||||
};
|
||||
|
||||
/// count_1s() counts the number of nonzero bits in a bitboard.
|
||||
/// We have different optimized versions according if platform
|
||||
/// is 32 or 64 bits, and to the maximum number of nonzero bits.
|
||||
/// We also support hardware popcnt instruction. See Readme.txt
|
||||
/// on how to pgo compile with popcnt support.
|
||||
template<BitCountType> inline int count_1s(Bitboard);
|
||||
/// Determine at compile time the best popcount<> specialization according if
|
||||
/// platform is 32 or 64 bits, to the maximum number of nonzero bits to count
|
||||
/// and if 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 nonzero bits in a bitboard
|
||||
template<BitCountType> inline int popcount(Bitboard);
|
||||
|
||||
template<>
|
||||
inline int count_1s<CNT64>(Bitboard b) {
|
||||
b -= ((b>>1) & 0x5555555555555555ULL);
|
||||
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
|
||||
b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL;
|
||||
b *= 0x0101010101010101ULL;
|
||||
return int(b >> 56);
|
||||
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 count_1s<CNT64_MAX15>(Bitboard b) {
|
||||
b -= (b>>1) & 0x5555555555555555ULL;
|
||||
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
|
||||
b *= 0x1111111111111111ULL;
|
||||
return int(b >> 60);
|
||||
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 count_1s<CNT32>(Bitboard b) {
|
||||
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) & 0x0F0F0F0F; // 0-8 in 8 bits
|
||||
v += (((w >> 4) + w) & 0x0F0F0F0F); // 0-16 in 8 bits
|
||||
v *= 0x01010101; // mul is fast on amd procs
|
||||
return int(v >> 24);
|
||||
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 count_1s<CNT32_MAX15>(Bitboard b) {
|
||||
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);
|
||||
v += w; // 0-8 in 4 bits
|
||||
v *= 0x11111111;
|
||||
return int(v >> 28);
|
||||
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 count_1s<CNT_POPCNT>(Bitboard b) {
|
||||
#if !defined(USE_POPCNT)
|
||||
return int(b != 0); // Avoid 'b not used' warning
|
||||
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 __popcnt64(b);
|
||||
#elif defined(__GNUC__)
|
||||
unsigned long ret;
|
||||
__asm__("popcnt %1, %0" : "=r" (ret) : "r" (b));
|
||||
return ret;
|
||||
|
||||
return (int)__popcnt64(b);
|
||||
|
||||
#else
|
||||
|
||||
__asm__("popcnt %1, %0" : "=r" (b) : "r" (b));
|
||||
return b;
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/// cpu_has_popcnt() detects support for popcnt instruction at runtime
|
||||
inline bool cpu_has_popcnt() {
|
||||
|
||||
int CPUInfo[4] = {-1};
|
||||
__cpuid(CPUInfo, 0x00000001);
|
||||
return (CPUInfo[2] >> 23) & 1;
|
||||
}
|
||||
|
||||
|
||||
/// CpuHasPOPCNT is a global constant initialized at startup that
|
||||
/// is set to true if CPU on which application runs supports popcnt
|
||||
/// hardware instruction. Unless USE_POPCNT is not defined.
|
||||
#if defined(USE_POPCNT)
|
||||
const bool CpuHasPOPCNT = cpu_has_popcnt();
|
||||
#else
|
||||
const bool CpuHasPOPCNT = false;
|
||||
#endif
|
||||
|
||||
|
||||
/// CpuIs64Bit is a global constant initialized at compile time that
|
||||
/// is set to true if CPU on which application runs is a 64 bits.
|
||||
#if defined(IS_64BIT)
|
||||
const bool CpuIs64Bit = true;
|
||||
#else
|
||||
const bool CpuIs64Bit = false;
|
||||
#endif
|
||||
|
||||
#endif // !defined(BITCOUNT_H_INCLUDED)
|
||||
#endif // #ifndef BITCOUNT_H_INCLUDED
|
||||
|
||||
+135
-244
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,36 +17,44 @@
|
||||
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
|
||||
by Fabien Letouzey. PolyGlot is available under the GNU General
|
||||
Public License, and can be downloaded from http://wbec-ridderkerk.nl
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
#include "book.h"
|
||||
#include "misc.h"
|
||||
#include "movegen.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
////
|
||||
//// Local definitions
|
||||
////
|
||||
|
||||
namespace {
|
||||
|
||||
// Book entry size in bytes
|
||||
const int EntrySize = 16;
|
||||
// A Polyglot book is a series of "entries" of 16 bytes. All integers are
|
||||
// stored in big-endian format, with 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 uint64_t Random64[781] = {
|
||||
const union {
|
||||
Key PolyGlotRandoms[781];
|
||||
struct {
|
||||
Key psq[12][64]; // [piece][square]
|
||||
Key castle[4]; // [castle right]
|
||||
Key enpassant[8]; // [file]
|
||||
Key turn;
|
||||
} Zobrist;
|
||||
} PG = {{
|
||||
0x9D39247E33776D41ULL, 0x2AF7398005AAA5C7ULL, 0x44DB015024623547ULL,
|
||||
0x9C15F73E62A76AE2ULL, 0x75834465489C0C89ULL, 0x3290AC3A203001BFULL,
|
||||
0x0FBBAD1F61042279ULL, 0xE83A908FF2FB60CAULL, 0x0D7E765D58755C10ULL,
|
||||
@@ -308,280 +316,163 @@ namespace {
|
||||
0x003A93D8B2806962ULL, 0x1C99DED33CB890A1ULL, 0xCF3145DE0ADD4289ULL,
|
||||
0xD0E4427A5514FB72ULL, 0x77C621CC9FB3A483ULL, 0x67A34DAC4356550BULL,
|
||||
0xF8D626AAAF278509ULL
|
||||
};
|
||||
}};
|
||||
|
||||
// Indices to the Random64[] array
|
||||
const int PieceIdx = 0;
|
||||
const int CastleIdx = 768;
|
||||
const int EnPassantIdx = 772;
|
||||
const int TurnIdx = 780;
|
||||
// polyglot_key() returns the PolyGlot hash key of the given position
|
||||
Key polyglot_key(const Position& pos) {
|
||||
|
||||
// Local functions
|
||||
uint64_t book_key(const Position& pos);
|
||||
uint64_t book_piece_key(Piece p, Square s);
|
||||
uint64_t book_castle_key(const Position& pos);
|
||||
uint64_t book_ep_key(const Position& pos);
|
||||
uint64_t book_color_key(const Position& pos);
|
||||
}
|
||||
Key key = 0;
|
||||
Bitboard b = pos.pieces();
|
||||
|
||||
while (b)
|
||||
{
|
||||
Square s = pop_lsb(&b);
|
||||
Piece p = pos.piece_on(s);
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
// PolyGlot pieces are: BP = 0, WP = 1, BN = 2, ... BK = 10, WK = 11
|
||||
key ^= PG.Zobrist.psq[2 * (type_of(p) - 1) + (color_of(p) == WHITE)][s];
|
||||
}
|
||||
|
||||
// C'tor. Make random number generation less deterministic, for book moves
|
||||
Book::Book() {
|
||||
b = pos.can_castle(ALL_CASTLES);
|
||||
|
||||
for (int i = abs(get_system_time() % 10000); i > 0; i--)
|
||||
RKiss.rand<unsigned>();
|
||||
}
|
||||
while (b)
|
||||
key ^= PG.Zobrist.castle[pop_lsb(&b)];
|
||||
|
||||
if (pos.ep_square() != SQ_NONE)
|
||||
key ^= PG.Zobrist.enpassant[file_of(pos.ep_square())];
|
||||
|
||||
/// Destructor. Be sure file is closed before we leave.
|
||||
if (pos.side_to_move() == WHITE)
|
||||
key ^= PG.Zobrist.turn;
|
||||
|
||||
Book::~Book() {
|
||||
|
||||
close();
|
||||
}
|
||||
|
||||
|
||||
/// Book::close() closes the file only if it is open, otherwise
|
||||
/// we can end up in a little mess due to how std::ifstream works.
|
||||
|
||||
void Book::close() {
|
||||
|
||||
if (is_open())
|
||||
ifstream::close();
|
||||
}
|
||||
|
||||
|
||||
/// Book::open() opens a book file with a given file name
|
||||
|
||||
void Book::open(const string& fName) {
|
||||
|
||||
// Close old file before opening the new
|
||||
close();
|
||||
|
||||
fileName = fName;
|
||||
ifstream::open(fileName.c_str(), ifstream::in | ifstream::binary);
|
||||
|
||||
// Silently return when asked to open a non-exsistent file
|
||||
if (!is_open())
|
||||
return;
|
||||
|
||||
// Get the book size in number of entries
|
||||
seekg(0, ios::end);
|
||||
bookSize = long(tellg()) / EntrySize;
|
||||
seekg(0, ios::beg);
|
||||
|
||||
if (!good())
|
||||
{
|
||||
cerr << "Failed to open book file " << fileName << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
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 in 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;
|
||||
}
|
||||
|
||||
|
||||
/// Book::file_name() returns the file name of the currently active book,
|
||||
/// or the empty string if no book is open.
|
||||
/// open() tries to open a book file with the given name after closing any
|
||||
/// exsisting one.
|
||||
|
||||
const string Book::file_name() { // Not const to compile on HP-UX 11.X
|
||||
bool PolyglotBook::open(const char* fName) {
|
||||
|
||||
return is_open() ? fileName : "";
|
||||
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 retry ifstream::open()
|
||||
return !fileName.empty();
|
||||
}
|
||||
|
||||
|
||||
/// Book::get_move() gets a book move for a given position. Returns
|
||||
/// MOVE_NONE if no book move is found.
|
||||
/// probe() tries to find a book move for the given position. If no move is
|
||||
/// found returns MOVE_NONE. If pickBest is true returns always the highest
|
||||
/// rated move, otherwise randomly chooses one, based on the move score.
|
||||
|
||||
Move Book::get_move(const Position& pos, bool findBestMove) {
|
||||
Move PolyglotBook::probe(const Position& pos, const string& fName, bool pickBest) {
|
||||
|
||||
if (!is_open() || bookSize == 0)
|
||||
if (fileName != fName && !open(fName.c_str()))
|
||||
return MOVE_NONE;
|
||||
|
||||
BookEntry entry;
|
||||
int bookMove = MOVE_NONE;
|
||||
unsigned scoresSum = 0, bestScore = 0;
|
||||
uint64_t key = book_key(pos);
|
||||
Entry e;
|
||||
uint16_t best = 0;
|
||||
unsigned sum = 0;
|
||||
Move move = MOVE_NONE;
|
||||
Key key = polyglot_key(pos);
|
||||
|
||||
// Choose a book move among the possible moves for the given position
|
||||
for (int idx = find_key(key); idx < bookSize; idx++)
|
||||
seekg(find_first(key) * sizeof(Entry), ios_base::beg);
|
||||
|
||||
while (*this >> e, e.key == key && good())
|
||||
{
|
||||
read_entry(entry, idx);
|
||||
if (entry.key != key)
|
||||
break;
|
||||
|
||||
unsigned score = entry.count;
|
||||
|
||||
// If findBestMove is true choose highest rated book move
|
||||
if (findBestMove)
|
||||
{
|
||||
if (score > bestScore)
|
||||
{
|
||||
bestScore = score;
|
||||
bookMove = entry.move;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
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 more probability to be choosen then a one with
|
||||
// lower score. Note that first entry is always chosen.
|
||||
scoresSum += score;
|
||||
if (RKiss.rand<unsigned>() % scoresSum < score)
|
||||
bookMove = entry.move;
|
||||
// high score it has higher probability to be choosen than a move
|
||||
// with lower score. Note that first entry is always chosen.
|
||||
if ( (sum && rkiss.rand<unsigned>() % sum < e.count)
|
||||
|| (pickBest && e.count == best))
|
||||
move = Move(e.move);
|
||||
}
|
||||
if (!bookMove)
|
||||
|
||||
if (!move)
|
||||
return MOVE_NONE;
|
||||
|
||||
// Verify the book move is legal
|
||||
MoveStack mlist[MOVES_MAX];
|
||||
MoveStack* last = generate_moves(pos, mlist);
|
||||
for (MoveStack* cur = mlist; cur != last; cur++)
|
||||
if ((int(cur->move) & 07777) == bookMove)
|
||||
return cur->move;
|
||||
// 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 "king captures rook" representation. So in case book
|
||||
// move is a promotion we have to convert to our representation, in all the
|
||||
// other cases we can directly compare with a Move after having masked out
|
||||
// the special Move's 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;
|
||||
}
|
||||
|
||||
|
||||
/// Book::find_key() takes a book key as input, and does a binary search
|
||||
/// through the book file for the given key. The index to the first book
|
||||
/// entry with the same key as the input is returned. When the key is not
|
||||
/// found in the book file, bookSize is returned.
|
||||
/// 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.
|
||||
|
||||
int Book::find_key(uint64_t key) {
|
||||
size_t PolyglotBook::find_first(Key key) {
|
||||
|
||||
int left, right, mid;
|
||||
BookEntry entry;
|
||||
seekg(0, ios::end); // Move pointer to end, so tellg() gets file's size
|
||||
|
||||
// Binary search (finds the leftmost entry)
|
||||
left = 0;
|
||||
right = bookSize - 1;
|
||||
size_t low = 0, mid, high = (size_t)tellg() / sizeof(Entry) - 1;
|
||||
Entry e;
|
||||
|
||||
assert(left <= right);
|
||||
assert(low <= high);
|
||||
|
||||
while (left < right)
|
||||
while (low < high && good())
|
||||
{
|
||||
mid = (left + right) / 2;
|
||||
mid = (low + high) / 2;
|
||||
|
||||
assert(mid >= left && mid < right);
|
||||
assert(mid >= low && mid < high);
|
||||
|
||||
read_entry(entry, mid);
|
||||
seekg(mid * sizeof(Entry), ios_base::beg);
|
||||
*this >> e;
|
||||
|
||||
if (key <= entry.key)
|
||||
right = mid;
|
||||
if (key <= e.key)
|
||||
high = mid;
|
||||
else
|
||||
left = mid + 1;
|
||||
low = mid + 1;
|
||||
}
|
||||
|
||||
assert(left == right);
|
||||
assert(low == high);
|
||||
|
||||
read_entry(entry, left);
|
||||
return entry.key == key ? left : bookSize;
|
||||
}
|
||||
|
||||
|
||||
/// Book::read_entry() takes a BookEntry reference and an integer index as
|
||||
/// input, and looks up the opening book entry at the given index in the book
|
||||
/// file. The book entry is copied to the first input parameter.
|
||||
|
||||
void Book::read_entry(BookEntry& entry, int idx) {
|
||||
|
||||
assert(idx >= 0 && idx < bookSize);
|
||||
assert(is_open());
|
||||
|
||||
seekg(idx * EntrySize, ios_base::beg);
|
||||
|
||||
*this >> entry;
|
||||
|
||||
if (!good())
|
||||
{
|
||||
cerr << "Failed to read book entry at index " << idx << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Book::read_integer() reads size chars from the file stream
|
||||
/// and converts them in an integer number.
|
||||
|
||||
uint64_t Book::read_integer(int size) {
|
||||
|
||||
char buf[8];
|
||||
uint64_t n = 0;
|
||||
|
||||
read(buf, size);
|
||||
|
||||
// Numbers are stored on disk as a binary byte stream
|
||||
for (int i = 0; i < size; i++)
|
||||
n = (n << 8) + (unsigned char)buf[i];
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
////
|
||||
//// Local definitions
|
||||
////
|
||||
|
||||
namespace {
|
||||
|
||||
uint64_t book_key(const Position& pos) {
|
||||
|
||||
uint64_t result = 0;
|
||||
Bitboard b = pos.occupied_squares();
|
||||
|
||||
while (b)
|
||||
{
|
||||
Square s = pop_1st_bit(&b);
|
||||
result ^= book_piece_key(pos.piece_on(s), s);
|
||||
}
|
||||
|
||||
result ^= book_castle_key(pos);
|
||||
result ^= book_ep_key(pos);
|
||||
result ^= book_color_key(pos);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
uint64_t book_piece_key(Piece p, Square s) {
|
||||
|
||||
// Convert pieces to the range 0..11
|
||||
static const int PieceTo12[] = { 0, 0, 2, 4, 6, 8, 10, 0, 0, 1, 3, 5, 7, 9, 11 };
|
||||
|
||||
return Random64[PieceIdx + (PieceTo12[int(p)]^1) * 64 + int(s)];
|
||||
}
|
||||
|
||||
|
||||
uint64_t book_castle_key(const Position& pos) {
|
||||
|
||||
uint64_t result = 0;
|
||||
|
||||
if (pos.can_castle_kingside(WHITE))
|
||||
result ^= Random64[CastleIdx + 0];
|
||||
|
||||
if (pos.can_castle_queenside(WHITE))
|
||||
result ^= Random64[CastleIdx + 1];
|
||||
|
||||
if (pos.can_castle_kingside(BLACK))
|
||||
result ^= Random64[CastleIdx + 2];
|
||||
|
||||
if (pos.can_castle_queenside(BLACK))
|
||||
result ^= Random64[CastleIdx + 3];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
uint64_t book_ep_key(const Position& pos) {
|
||||
|
||||
return pos.ep_square() == SQ_NONE ? 0 : Random64[EnPassantIdx + square_file(pos.ep_square())];
|
||||
}
|
||||
|
||||
|
||||
uint64_t book_color_key(const Position& pos) {
|
||||
|
||||
return pos.side_to_move() == WHITE ? Random64[TurnIdx] : 0;
|
||||
}
|
||||
return low;
|
||||
}
|
||||
|
||||
+11
-47
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,65 +17,29 @@
|
||||
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
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(BOOK_H_INCLUDED)
|
||||
#ifndef BOOK_H_INCLUDED
|
||||
#define BOOK_H_INCLUDED
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
#include "move.h"
|
||||
#include "position.h"
|
||||
#include "rkiss.h"
|
||||
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
struct BookEntry {
|
||||
uint64_t key;
|
||||
uint16_t move;
|
||||
uint16_t count;
|
||||
uint16_t n;
|
||||
uint16_t sum;
|
||||
};
|
||||
|
||||
class Book : private std::ifstream {
|
||||
Book(const Book&); // just decleared..
|
||||
Book& operator=(const Book&); // ..to avoid a warning
|
||||
class PolyglotBook : private std::ifstream {
|
||||
public:
|
||||
Book();
|
||||
~Book();
|
||||
void open(const std::string& fName);
|
||||
void close();
|
||||
const std::string file_name();
|
||||
Move get_move(const Position& pos, bool findBestMove);
|
||||
PolyglotBook();
|
||||
~PolyglotBook();
|
||||
Move probe(const Position& pos, const std::string& fName, bool pickBest);
|
||||
|
||||
private:
|
||||
Book& operator>>(uint64_t& n) { n = read_integer(8); return *this; }
|
||||
Book& operator>>(uint16_t& n) { n = (uint16_t)read_integer(2); return *this; }
|
||||
void operator>>(BookEntry& e) { *this >> e.key >> e.move >> e.count >> e.n >> e.sum; }
|
||||
template<typename T> PolyglotBook& operator>>(T& n);
|
||||
|
||||
uint64_t read_integer(int size);
|
||||
void read_entry(BookEntry& e, int n);
|
||||
int find_key(uint64_t key);
|
||||
bool open(const char* fName);
|
||||
size_t find_first(Key key);
|
||||
|
||||
RKISS rkiss;
|
||||
std::string fileName;
|
||||
int bookSize;
|
||||
RKISS RKiss;
|
||||
};
|
||||
|
||||
#endif // !defined(BOOK_H_INCLUDED)
|
||||
#endif // #ifndef BOOK_H_INCLUDED
|
||||
|
||||
-43
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(DEPTH_H_INCLUDED)
|
||||
#define DEPTH_H_INCLUDED
|
||||
|
||||
#include "types.h"
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
enum Depth {
|
||||
|
||||
ONE_PLY = 2,
|
||||
|
||||
DEPTH_ZERO = 0 * ONE_PLY,
|
||||
DEPTH_QS_CHECKS = -1 * ONE_PLY,
|
||||
DEPTH_QS_NO_CHECKS = -2 * ONE_PLY,
|
||||
|
||||
DEPTH_NONE = -127 * ONE_PLY
|
||||
};
|
||||
|
||||
ENABLE_OPERATORS_ON(Depth);
|
||||
|
||||
#endif // !defined(DEPTH_H_INCLUDED)
|
||||
+433
-379
File diff suppressed because it is too large
Load Diff
+84
-63
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,88 +17,109 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(ENDGAME_H_INCLUDED)
|
||||
#ifndef ENDGAME_H_INCLUDED
|
||||
#define ENDGAME_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "position.h"
|
||||
#include "value.h"
|
||||
#include "types.h"
|
||||
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
/// EndgameType lists all supported endgames
|
||||
|
||||
enum EndgameType {
|
||||
|
||||
// Evaluation functions
|
||||
KXK, // Generic "mate lone king" eval
|
||||
KBNK, // KBN vs K
|
||||
KPK, // KP vs K
|
||||
KRKP, // KR vs KP
|
||||
KRKB, // KR vs KB
|
||||
KRKN, // KR vs KN
|
||||
KQKR, // KQ vs KR
|
||||
KBBKN, // KBB vs KN
|
||||
KNNK, // KNN vs K
|
||||
KmmKm, // K and two minors vs K and one or two minors
|
||||
// Evaluation functions
|
||||
|
||||
// Scaling functions
|
||||
KBPsK, // KB+pawns vs K
|
||||
KQKRPs, // KQ vs KR+pawns
|
||||
KRPKR, // KRP vs KR
|
||||
KRPPKRP, // KRPP vs KRP
|
||||
KPsK, // King and pawns vs king
|
||||
KBPKB, // KBP vs KB
|
||||
KBPPKB, // KBPP vs KB
|
||||
KBPKN, // KBP vs KN
|
||||
KNPK, // KNP vs K
|
||||
KPKP // KP vs KP
|
||||
KK, // K vs K
|
||||
KBK, // KB vs K
|
||||
KNK, // KN vs K
|
||||
KNNK, // KNN vs K
|
||||
KXK, // Generic "mate lone king" eval
|
||||
KBNK, // KBN vs K
|
||||
KPK, // KP vs K
|
||||
KRKP, // KR vs KP
|
||||
KRKB, // KR vs KB
|
||||
KRKN, // KR vs KN
|
||||
KQKP, // KQ vs KP
|
||||
KQKR, // KQ vs KR
|
||||
KBBKN, // KBB vs KN
|
||||
KmmKm, // K and two minors vs K and one or two minors
|
||||
|
||||
|
||||
// Scaling functions
|
||||
SCALE_FUNS,
|
||||
|
||||
KBPsK, // KB+pawns vs K
|
||||
KQKRPs, // KQ vs KR+pawns
|
||||
KRPKR, // KRP vs KR
|
||||
KRPPKRP, // KRPP vs KRP
|
||||
KPsK, // King and pawns vs king
|
||||
KBPKB, // KBP vs KB
|
||||
KBPPKB, // KBPP vs KB
|
||||
KBPKN, // KBP vs KN
|
||||
KNPK, // KNP vs K
|
||||
KNPKB, // KNP vs KB
|
||||
KPKP // KP vs KP
|
||||
};
|
||||
|
||||
/// Template abstract base class for all special endgame functions
|
||||
|
||||
/// Endgame functions can be of two types according if return a Value or a
|
||||
/// ScaleFactor. Type eg_fun<int>::type equals to either ScaleFactor or Value
|
||||
/// depending if the template parameter is 0 or 1.
|
||||
|
||||
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
|
||||
|
||||
template<typename T>
|
||||
class EndgameFunctionBase {
|
||||
public:
|
||||
EndgameFunctionBase(Color c) : strongerSide(c), weakerSide(opposite_color(c)) {}
|
||||
virtual ~EndgameFunctionBase() {}
|
||||
virtual T apply(const Position&) const = 0;
|
||||
Color color() const { return strongerSide; }
|
||||
struct EndgameBase {
|
||||
|
||||
protected:
|
||||
virtual ~EndgameBase() {}
|
||||
virtual Color color() const = 0;
|
||||
virtual T operator()(const Position&) const = 0;
|
||||
};
|
||||
|
||||
|
||||
template<EndgameType E, typename T = typename eg_fun<(E > SCALE_FUNS)>::type>
|
||||
struct Endgame : public EndgameBase<T> {
|
||||
|
||||
explicit Endgame(Color c) : strongerSide(c), weakerSide(~c) {}
|
||||
Color color() const { return strongerSide; }
|
||||
T operator()(const Position&) const;
|
||||
|
||||
private:
|
||||
Color strongerSide, weakerSide;
|
||||
};
|
||||
|
||||
typedef EndgameFunctionBase<Value> EndgameEvaluationFunctionBase;
|
||||
typedef EndgameFunctionBase<ScaleFactor> EndgameScalingFunctionBase;
|
||||
|
||||
/// Endgames class stores in two std::map the pointers to endgame evaluation
|
||||
/// and scaling base objects. Then we use polymorphism to invoke the actual
|
||||
/// endgame function calling its operator() that is virtual.
|
||||
|
||||
/// Templates subclass for various concrete endgames
|
||||
class Endgames {
|
||||
|
||||
template<EndgameType>
|
||||
struct EvaluationFunction : public EndgameEvaluationFunctionBase {
|
||||
typedef EndgameEvaluationFunctionBase Base;
|
||||
explicit EvaluationFunction(Color c): EndgameEvaluationFunctionBase(c) {}
|
||||
Value apply(const Position&) const;
|
||||
typedef std::map<Key, EndgameBase<eg_fun<0>::type>*> M1;
|
||||
typedef std::map<Key, EndgameBase<eg_fun<1>::type>*> M2;
|
||||
|
||||
M1 m1;
|
||||
M2 m2;
|
||||
|
||||
M1& map(M1::mapped_type) { return m1; }
|
||||
M2& map(M2::mapped_type) { return m2; }
|
||||
|
||||
template<EndgameType E> void add(const std::string& code);
|
||||
|
||||
public:
|
||||
Endgames();
|
||||
~Endgames();
|
||||
|
||||
template<typename T> T probe(Key key, T& eg)
|
||||
{ return eg = map(eg).count(key) ? map(eg)[key] : NULL; }
|
||||
};
|
||||
|
||||
template<EndgameType>
|
||||
struct ScalingFunction : public EndgameScalingFunctionBase {
|
||||
typedef EndgameScalingFunctionBase Base;
|
||||
explicit ScalingFunction(Color c) : EndgameScalingFunctionBase(c) {}
|
||||
ScaleFactor apply(const Position&) const;
|
||||
};
|
||||
|
||||
|
||||
////
|
||||
//// Prototypes
|
||||
////
|
||||
|
||||
extern void init_bitbases();
|
||||
|
||||
#endif // !defined(ENDGAME_H_INCLUDED)
|
||||
#endif // #ifndef ENDGAME_H_INCLUDED
|
||||
|
||||
+602
-541
File diff suppressed because it is too large
Load Diff
+11
-10
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,18 +17,19 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(EVALUATE_H_INCLUDED)
|
||||
#ifndef EVALUATE_H_INCLUDED
|
||||
#define EVALUATE_H_INCLUDED
|
||||
|
||||
#include "color.h"
|
||||
#include "value.h"
|
||||
#include "types.h"
|
||||
|
||||
class Position;
|
||||
|
||||
extern Value evaluate(const Position& pos, Value& margin);
|
||||
extern void init_eval(int threads);
|
||||
extern void quit_eval();
|
||||
extern void read_evaluation_uci_options(Color sideToMove);
|
||||
namespace Eval {
|
||||
|
||||
#endif // !defined(EVALUATE_H_INCLUDED)
|
||||
extern void init();
|
||||
extern Value evaluate(const Position& pos, Value& margin);
|
||||
extern std::string trace(const Position& pos);
|
||||
|
||||
}
|
||||
|
||||
#endif // #ifndef EVALUATE_H_INCLUDED
|
||||
|
||||
@@ -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-2010 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/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
#include "history.h"
|
||||
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
|
||||
/// Constructor
|
||||
|
||||
History::History() { clear(); }
|
||||
|
||||
|
||||
/// History::clear() clears the history tables
|
||||
|
||||
void History::clear() {
|
||||
memset(history, 0, 16 * 64 * sizeof(int));
|
||||
memset(maxStaticValueDelta, 0, 16 * 64 * sizeof(int));
|
||||
}
|
||||
|
||||
|
||||
/// History::success() registers a move as being successful. This is done
|
||||
/// whenever a non-capturing move causes a beta cutoff in the main search.
|
||||
/// The three parameters are the moving piece, the destination square, and
|
||||
/// the search depth.
|
||||
|
||||
void History::success(Piece p, Square to, Depth d) {
|
||||
|
||||
assert(piece_is_ok(p));
|
||||
assert(square_is_ok(to));
|
||||
|
||||
history[p][to] += int(d) * int(d);
|
||||
|
||||
// Prevent history overflow
|
||||
if (history[p][to] >= HistoryMax)
|
||||
for (int i = 0; i < 16; i++)
|
||||
for (int j = 0; j < 64; j++)
|
||||
history[i][j] /= 2;
|
||||
}
|
||||
|
||||
|
||||
/// History::failure() registers a move as being unsuccessful. The function is
|
||||
/// called for each non-capturing move which failed to produce a beta cutoff
|
||||
/// at a node where a beta cutoff was finally found.
|
||||
|
||||
void History::failure(Piece p, Square to, Depth d) {
|
||||
|
||||
assert(piece_is_ok(p));
|
||||
assert(square_is_ok(to));
|
||||
|
||||
history[p][to] -= int(d) * int(d);
|
||||
|
||||
// Prevent history underflow
|
||||
if (history[p][to] <= -HistoryMax)
|
||||
for (int i = 0; i < 16; i++)
|
||||
for (int j = 0; j < 64; j++)
|
||||
history[i][j] /= 2;
|
||||
}
|
||||
|
||||
|
||||
/// History::set_gain() and History::gain() store and retrieve the
|
||||
/// gain of a move given the delta of the static position evaluations
|
||||
/// before and after the move.
|
||||
|
||||
void History::set_gain(Piece p, Square to, Value delta) {
|
||||
|
||||
if (delta >= maxStaticValueDelta[p][to])
|
||||
maxStaticValueDelta[p][to] = delta;
|
||||
else
|
||||
maxStaticValueDelta[p][to]--;
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(HISTORY_H_INCLUDED)
|
||||
#define HISTORY_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include "depth.h"
|
||||
#include "move.h"
|
||||
#include "piece.h"
|
||||
#include "value.h"
|
||||
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
/// The History class stores statistics about how often different moves
|
||||
/// have been successful or unsuccessful during the current search. These
|
||||
/// statistics are used for reduction and move ordering decisions. History
|
||||
/// entries are stored according only to moving piece and destination square,
|
||||
/// in particular two moves with different origin but same destination and
|
||||
/// same piece will be considered identical.
|
||||
|
||||
class History {
|
||||
|
||||
public:
|
||||
History();
|
||||
void clear();
|
||||
void success(Piece p, Square to, Depth d);
|
||||
void failure(Piece p, Square to, Depth d);
|
||||
int value(Piece p, Square to) const;
|
||||
void set_gain(Piece p, Square to, Value delta);
|
||||
Value gain(Piece p, Square to) const;
|
||||
|
||||
private:
|
||||
int history[16][64]; // [piece][square]
|
||||
int maxStaticValueDelta[16][64]; // [piece][from_square][to_square]
|
||||
};
|
||||
|
||||
|
||||
////
|
||||
//// Constants and variables
|
||||
////
|
||||
|
||||
/// HistoryMax controls how often the history counters will be scaled down:
|
||||
/// When the history score for a move gets bigger than HistoryMax, all
|
||||
/// entries in the table are divided by 2. It is difficult to guess what
|
||||
/// the ideal value of this constant is. Scaling down the scores often has
|
||||
/// the effect that parts of the search tree which have been searched
|
||||
/// recently have a bigger importance for move ordering than the moves which
|
||||
/// have been searched a long time ago.
|
||||
|
||||
const int HistoryMax = 50000 * ONE_PLY;
|
||||
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
inline int History::value(Piece p, Square to) const {
|
||||
return history[p][to];
|
||||
}
|
||||
|
||||
inline Value History::gain(Piece p, Square to) const {
|
||||
return Value(maxStaticValueDelta[p][to]);
|
||||
}
|
||||
|
||||
#endif // !defined(HISTORY_H_INCLUDED)
|
||||
-60
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(LOCK_H_INCLUDED)
|
||||
#define LOCK_H_INCLUDED
|
||||
|
||||
|
||||
#if !defined(_MSC_VER)
|
||||
|
||||
# include <pthread.h>
|
||||
|
||||
typedef pthread_mutex_t Lock;
|
||||
typedef pthread_cond_t WaitCondition;
|
||||
|
||||
# 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)
|
||||
|
||||
#else
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#undef WIN32_LEAN_AND_MEAN
|
||||
|
||||
typedef CRITICAL_SECTION Lock;
|
||||
typedef HANDLE WaitCondition;
|
||||
|
||||
# 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); }
|
||||
#endif
|
||||
|
||||
#endif // !defined(LOCK_H_INCLUDED)
|
||||
+16
-74
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,94 +17,36 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// To profile with callgrind uncomment following line
|
||||
//#define USE_CALLGRIND
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "bitcount.h"
|
||||
#include "endgame.h"
|
||||
#include "evaluate.h"
|
||||
#include "material.h"
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
#include "tt.h"
|
||||
#include "ucioption.h"
|
||||
|
||||
#ifdef USE_CALLGRIND
|
||||
#include <valgrind/callgrind.h>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
extern bool execute_uci_command(const string& cmd);
|
||||
extern void benchmark(int argc, char* argv[]);
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
// Disable IO buffering
|
||||
cout.rdbuf()->pubsetbuf(NULL, 0);
|
||||
cin.rdbuf()->pubsetbuf(NULL, 0);
|
||||
std::cout << engine_info() << std::endl;
|
||||
|
||||
// Startup initializations
|
||||
init_bitboards();
|
||||
init_uci_options();
|
||||
Position::init_zobrist();
|
||||
Position::init_piece_square_tables();
|
||||
init_eval(1);
|
||||
init_bitbases();
|
||||
init_search();
|
||||
init_threads();
|
||||
UCI::init(Options);
|
||||
Bitboards::init();
|
||||
Position::init();
|
||||
Bitbases::init_kpk();
|
||||
Search::init();
|
||||
Eval::init();
|
||||
Threads.init();
|
||||
TT.set_size(Options["Hash"]);
|
||||
|
||||
#ifdef USE_CALLGRIND
|
||||
CALLGRIND_START_INSTRUMENTATION;
|
||||
#endif
|
||||
std::string args;
|
||||
|
||||
if (argc <= 1)
|
||||
{
|
||||
// Print copyright notice
|
||||
cout << engine_name()
|
||||
<< " by Tord Romstad, Marco Costalba, Joona Kiiski" << endl;
|
||||
for (int i = 1; i < argc; i++)
|
||||
args += std::string(argv[i]) + " ";
|
||||
|
||||
if (CpuHasPOPCNT)
|
||||
cout << "Good! CPU has hardware POPCNT." << endl;
|
||||
UCI::loop(args);
|
||||
|
||||
// Wait for a command from the user, and passes this command to
|
||||
// execute_uci_command() and also intercepts EOF from stdin, by
|
||||
// translating EOF to the "quit" command. This ensures that we
|
||||
// exit gracefully if the GUI dies unexpectedly.
|
||||
string cmd;
|
||||
|
||||
do {
|
||||
// Wait for a command from stdin
|
||||
if (!getline(cin, cmd))
|
||||
cmd = "quit";
|
||||
|
||||
} while (execute_uci_command(cmd));
|
||||
}
|
||||
else // Process command line arguments
|
||||
{
|
||||
if (string(argv[1]) != "bench" || argc > 7)
|
||||
cout << "Usage: stockfish bench [hash size = 128] [threads = 1] "
|
||||
<< "[limit = 12] [fen positions file = default] "
|
||||
<< "[depth, time, perft or node limited = depth]" << endl;
|
||||
else
|
||||
benchmark(argc, argv);
|
||||
}
|
||||
|
||||
exit_threads();
|
||||
quit_eval();
|
||||
return 0;
|
||||
Threads.exit();
|
||||
}
|
||||
|
||||
+165
-308
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,213 +17,174 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <algorithm> // For std::min
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
|
||||
#include "material.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
////
|
||||
//// Local definitions
|
||||
////
|
||||
|
||||
namespace {
|
||||
|
||||
// Values modified by Joona Kiiski
|
||||
const Value MidgameLimit = Value(15581);
|
||||
const Value EndgameLimit = Value(3998);
|
||||
|
||||
// Scale factors used when one side has no more pawns
|
||||
const int NoPawnsSF[4] = { 6, 12, 32 };
|
||||
|
||||
// Polynomial material balance parameters
|
||||
const Value RedundantQueenPenalty = Value(320);
|
||||
const Value RedundantRookPenalty = Value(554);
|
||||
const Value RedundantQueen = Value(320);
|
||||
const Value RedundantRook = Value(554);
|
||||
|
||||
const int LinearCoefficients[6] = { 1617, -162, -1172, -190, 105, 26 };
|
||||
// pair pawn knight bishop rook queen
|
||||
const int LinearCoefficients[6] = { 1617, -162, -1172, -190, 105, 26 };
|
||||
|
||||
const int QuadraticCoefficientsSameColor[][8] = {
|
||||
{ 7, 7, 7, 7, 7, 7 }, { 39, 2, 7, 7, 7, 7 }, { 35, 271, -4, 7, 7, 7 },
|
||||
{ 7, 25, 4, 7, 7, 7 }, { -27, -2, 46, 100, 56, 7 }, { 58, 29, 83, 148, -3, -25 } };
|
||||
const int QuadraticCoefficientsSameColor[][PIECE_TYPE_NB] = {
|
||||
// pair pawn knight bishop rook queen
|
||||
{ 7 }, // Bishop pair
|
||||
{ 39, 2 }, // Pawn
|
||||
{ 35, 271, -4 }, // Knight
|
||||
{ 7, 105, 4, 7 }, // Bishop
|
||||
{ -27, -2, 46, 100, 56 }, // Rook
|
||||
{ 58, 29, 83, 148, -3, -25 } // Queen
|
||||
};
|
||||
|
||||
const int QuadraticCoefficientsOppositeColor[][8] = {
|
||||
{ 41, 41, 41, 41, 41, 41 }, { 37, 41, 41, 41, 41, 41 }, { 10, 62, 41, 41, 41, 41 },
|
||||
{ 57, 64, 39, 41, 41, 41 }, { 50, 40, 23, -22, 41, 41 }, { 106, 101, 3, 151, 171, 41 } };
|
||||
|
||||
typedef EndgameEvaluationFunctionBase EF;
|
||||
typedef EndgameScalingFunctionBase SF;
|
||||
typedef map<Key, EF*> EFMap;
|
||||
typedef map<Key, SF*> SFMap;
|
||||
const int QuadraticCoefficientsOppositeColor[][PIECE_TYPE_NB] = {
|
||||
// THEIR PIECES
|
||||
// pair pawn knight bishop rook queen
|
||||
{ 41 }, // Bishop pair
|
||||
{ 37, 41 }, // Pawn
|
||||
{ 10, 62, 41 }, // Knight OUR PIECES
|
||||
{ 57, 64, 39, 41 }, // Bishop
|
||||
{ 50, 40, 23, -22, 41 }, // Rook
|
||||
{ 106, 101, 3, 151, 171, 41 } // Queen
|
||||
};
|
||||
|
||||
// Endgame evaluation and scaling functions accessed direcly and not through
|
||||
// the function maps because correspond to more then one material hash key.
|
||||
EvaluationFunction<KmmKm> EvaluateKmmKm[] = { EvaluationFunction<KmmKm>(WHITE), EvaluationFunction<KmmKm>(BLACK) };
|
||||
EvaluationFunction<KXK> EvaluateKXK[] = { EvaluationFunction<KXK>(WHITE), EvaluationFunction<KXK>(BLACK) };
|
||||
ScalingFunction<KBPsK> ScaleKBPsK[] = { ScalingFunction<KBPsK>(WHITE), ScalingFunction<KBPsK>(BLACK) };
|
||||
ScalingFunction<KQKRPs> ScaleKQKRPs[] = { ScalingFunction<KQKRPs>(WHITE), ScalingFunction<KQKRPs>(BLACK) };
|
||||
ScalingFunction<KPsK> ScaleKPsK[] = { ScalingFunction<KPsK>(WHITE), ScalingFunction<KPsK>(BLACK) };
|
||||
ScalingFunction<KPKP> ScaleKPKP[] = { ScalingFunction<KPKP>(WHITE), ScalingFunction<KPKP>(BLACK) };
|
||||
Endgame<KmmKm> EvaluateKmmKm[] = { Endgame<KmmKm>(WHITE), Endgame<KmmKm>(BLACK) };
|
||||
Endgame<KXK> EvaluateKXK[] = { Endgame<KXK>(WHITE), Endgame<KXK>(BLACK) };
|
||||
|
||||
Endgame<KBPsK> ScaleKBPsK[] = { Endgame<KBPsK>(WHITE), Endgame<KBPsK>(BLACK) };
|
||||
Endgame<KQKRPs> ScaleKQKRPs[] = { Endgame<KQKRPs>(WHITE), Endgame<KQKRPs>(BLACK) };
|
||||
Endgame<KPsK> ScaleKPsK[] = { Endgame<KPsK>(WHITE), Endgame<KPsK>(BLACK) };
|
||||
Endgame<KPKP> ScaleKPKP[] = { Endgame<KPKP>(WHITE), Endgame<KPKP>(BLACK) };
|
||||
|
||||
// Helper templates used to detect a given material distribution
|
||||
template<Color Us> bool is_KXK(const Position& pos) {
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
return pos.non_pawn_material(Them) == VALUE_ZERO
|
||||
&& pos.piece_count(Them, PAWN) == 0
|
||||
&& pos.non_pawn_material(Us) >= RookValueMidgame;
|
||||
return !pos.count<PAWN>(Them)
|
||||
&& pos.non_pawn_material(Them) == VALUE_ZERO
|
||||
&& pos.non_pawn_material(Us) >= RookValueMg;
|
||||
}
|
||||
|
||||
template<Color Us> bool is_KBPsK(const Position& pos) {
|
||||
return pos.non_pawn_material(Us) == BishopValueMidgame
|
||||
&& pos.piece_count(Us, BISHOP) == 1
|
||||
&& pos.piece_count(Us, PAWN) >= 1;
|
||||
template<Color Us> bool is_KBPsKs(const Position& pos) {
|
||||
return pos.non_pawn_material(Us) == BishopValueMg
|
||||
&& pos.count<BISHOP>(Us) == 1
|
||||
&& pos.count<PAWN >(Us) >= 1;
|
||||
}
|
||||
|
||||
template<Color Us> bool is_KQKRPs(const Position& pos) {
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
return pos.piece_count(Us, PAWN) == 0
|
||||
&& pos.non_pawn_material(Us) == QueenValueMidgame
|
||||
&& pos.piece_count(Us, QUEEN) == 1
|
||||
&& pos.piece_count(Them, ROOK) == 1
|
||||
&& pos.piece_count(Them, PAWN) >= 1;
|
||||
return !pos.count<PAWN>(Us)
|
||||
&& pos.non_pawn_material(Us) == QueenValueMg
|
||||
&& pos.count<QUEEN>(Us) == 1
|
||||
&& pos.count<ROOK>(Them) == 1
|
||||
&& pos.count<PAWN>(Them) >= 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// imbalance() calculates imbalance comparing piece count of each
|
||||
/// piece type for both colors.
|
||||
|
||||
////
|
||||
//// Classes
|
||||
////
|
||||
template<Color Us>
|
||||
int imbalance(const int pieceCount[][PIECE_TYPE_NB]) {
|
||||
|
||||
/// EndgameFunctions class stores endgame evaluation and scaling functions
|
||||
/// in two std::map. Because STL library is not guaranteed to be thread
|
||||
/// safe even for read access, the maps, although with identical content,
|
||||
/// are replicated for each thread. This is faster then using locks.
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
class EndgameFunctions {
|
||||
public:
|
||||
EndgameFunctions();
|
||||
~EndgameFunctions();
|
||||
template<class T> T* get(Key key) const;
|
||||
int pt1, pt2, pc, v;
|
||||
int value = 0;
|
||||
|
||||
private:
|
||||
template<class T> void add(const string& keyCode);
|
||||
// Redundancy of major pieces, formula based on Kaufman's paper
|
||||
// "The Evaluation of Material Imbalances in Chess"
|
||||
if (pieceCount[Us][ROOK] > 0)
|
||||
value -= RedundantRook * (pieceCount[Us][ROOK] - 1)
|
||||
+ RedundantQueen * pieceCount[Us][QUEEN];
|
||||
|
||||
static Key buildKey(const string& keyCode);
|
||||
static const string swapColors(const string& keyCode);
|
||||
// Second-degree polynomial material imbalance by Tord Romstad
|
||||
for (pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; pt1++)
|
||||
{
|
||||
pc = pieceCount[Us][pt1];
|
||||
if (!pc)
|
||||
continue;
|
||||
|
||||
// Here we store two maps, for evaluate and scaling functions...
|
||||
pair<EFMap, SFMap> maps;
|
||||
v = LinearCoefficients[pt1];
|
||||
|
||||
// ...and here is the accessing template function
|
||||
template<typename T> const map<Key, T*>& get() const;
|
||||
};
|
||||
for (pt2 = NO_PIECE_TYPE; pt2 <= pt1; pt2++)
|
||||
v += QuadraticCoefficientsSameColor[pt1][pt2] * pieceCount[Us][pt2]
|
||||
+ QuadraticCoefficientsOppositeColor[pt1][pt2] * pieceCount[Them][pt2];
|
||||
|
||||
// Explicit specializations of a member function shall be declared in
|
||||
// the namespace of which the class template is a member.
|
||||
template<> const EFMap& EndgameFunctions::get<EF>() const { return maps.first; }
|
||||
template<> const SFMap& EndgameFunctions::get<SF>() const { return maps.second; }
|
||||
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
/// MaterialInfoTable c'tor and d'tor, called once by each thread
|
||||
|
||||
MaterialInfoTable::MaterialInfoTable() {
|
||||
|
||||
entries = new MaterialInfo[MaterialTableSize];
|
||||
funcs = new EndgameFunctions();
|
||||
|
||||
if (!entries || !funcs)
|
||||
{
|
||||
cerr << "Failed to allocate " << MaterialTableSize * sizeof(MaterialInfo)
|
||||
<< " bytes for material hash table." << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
value += pc * v;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
memset(entries, 0, MaterialTableSize * sizeof(MaterialInfo));
|
||||
}
|
||||
|
||||
MaterialInfoTable::~MaterialInfoTable() {
|
||||
} // namespace
|
||||
|
||||
delete funcs;
|
||||
delete [] entries;
|
||||
}
|
||||
namespace Material {
|
||||
|
||||
/// Material::probe() takes a position object as input, looks up a MaterialEntry
|
||||
/// object, and returns a pointer to it. If the material configuration is not
|
||||
/// already present in the table, it is computed and stored there, so we don't
|
||||
/// have to recompute everything when the same material configuration occurs again.
|
||||
|
||||
/// MaterialInfoTable::game_phase() calculates the phase given the current
|
||||
/// position. Because the phase is strictly a function of the material, it
|
||||
/// is stored in MaterialInfo.
|
||||
Entry* probe(const Position& pos, Table& entries, Endgames& endgames) {
|
||||
|
||||
Phase MaterialInfoTable::game_phase(const Position& pos) {
|
||||
Key key = pos.material_key();
|
||||
Entry* e = entries[key];
|
||||
|
||||
Value npm = pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK);
|
||||
|
||||
if (npm >= MidgameLimit)
|
||||
return PHASE_MIDGAME;
|
||||
|
||||
if (npm <= EndgameLimit)
|
||||
return PHASE_ENDGAME;
|
||||
|
||||
return Phase(((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit));
|
||||
}
|
||||
|
||||
/// MaterialInfoTable::get_material_info() takes a position object as input,
|
||||
/// computes or looks up a MaterialInfo object, and returns a pointer to it.
|
||||
/// If the material configuration is not already present in the table, it
|
||||
/// is stored there, so we don't have to recompute everything when the
|
||||
/// same material configuration occurs again.
|
||||
|
||||
MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
|
||||
|
||||
Key key = pos.get_material_key();
|
||||
unsigned index = unsigned(key & (MaterialTableSize - 1));
|
||||
MaterialInfo* mi = entries + index;
|
||||
|
||||
// If mi->key matches the position's material hash key, it means that we
|
||||
// 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 (mi->key == key)
|
||||
return mi;
|
||||
if (e->key == key)
|
||||
return e;
|
||||
|
||||
// Clear the MaterialInfo object, and set its key
|
||||
memset(mi, 0, sizeof(MaterialInfo));
|
||||
mi->factor[WHITE] = mi->factor[BLACK] = uint8_t(SCALE_FACTOR_NORMAL);
|
||||
mi->key = key;
|
||||
|
||||
// Store game phase
|
||||
mi->gamePhase = MaterialInfoTable::game_phase(pos);
|
||||
std::memset(e, 0, sizeof(Entry));
|
||||
e->key = key;
|
||||
e->factor[WHITE] = e->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL;
|
||||
e->gamePhase = game_phase(pos);
|
||||
|
||||
// Let's look if we have a specialized evaluation function for this
|
||||
// particular material configuration. First we look for a fixed
|
||||
// configuration one, then a generic one if previous search failed.
|
||||
if ((mi->evaluationFunction = funcs->get<EF>(key)) != NULL)
|
||||
return mi;
|
||||
if (endgames.probe(key, e->evaluationFunction))
|
||||
return e;
|
||||
|
||||
if (is_KXK<WHITE>(pos) || is_KXK<BLACK>(pos))
|
||||
if (is_KXK<WHITE>(pos))
|
||||
{
|
||||
mi->evaluationFunction = is_KXK<WHITE>(pos) ? &EvaluateKXK[WHITE] : &EvaluateKXK[BLACK];
|
||||
return mi;
|
||||
e->evaluationFunction = &EvaluateKXK[WHITE];
|
||||
return e;
|
||||
}
|
||||
|
||||
if ( pos.pieces(PAWN) == EmptyBoardBB
|
||||
&& pos.pieces(ROOK) == EmptyBoardBB
|
||||
&& pos.pieces(QUEEN) == EmptyBoardBB)
|
||||
if (is_KXK<BLACK>(pos))
|
||||
{
|
||||
e->evaluationFunction = &EvaluateKXK[BLACK];
|
||||
return e;
|
||||
}
|
||||
|
||||
if (!pos.pieces(PAWN) && !pos.pieces(ROOK) && !pos.pieces(QUEEN))
|
||||
{
|
||||
// Minor piece endgame with at least one minor piece per side and
|
||||
// no pawns. Note that the case KmmK is already handled by KXK.
|
||||
assert((pos.pieces(KNIGHT, WHITE) | pos.pieces(BISHOP, WHITE)));
|
||||
assert((pos.pieces(KNIGHT, BLACK) | pos.pieces(BISHOP, BLACK)));
|
||||
assert((pos.pieces(WHITE, KNIGHT) | pos.pieces(WHITE, BISHOP)));
|
||||
assert((pos.pieces(BLACK, KNIGHT) | pos.pieces(BLACK, BISHOP)));
|
||||
|
||||
if ( pos.piece_count(WHITE, BISHOP) + pos.piece_count(WHITE, KNIGHT) <= 2
|
||||
&& pos.piece_count(BLACK, BISHOP) + pos.piece_count(BLACK, KNIGHT) <= 2)
|
||||
if ( pos.count<BISHOP>(WHITE) + pos.count<KNIGHT>(WHITE) <= 2
|
||||
&& pos.count<BISHOP>(BLACK) + pos.count<KNIGHT>(BLACK) <= 2)
|
||||
{
|
||||
mi->evaluationFunction = &EvaluateKmmKm[WHITE];
|
||||
return mi;
|
||||
e->evaluationFunction = &EvaluateKmmKm[pos.side_to_move()];
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,204 +193,100 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
|
||||
//
|
||||
// We face problems when there are several conflicting applicable
|
||||
// scaling functions and we need to decide which one to use.
|
||||
SF* sf;
|
||||
EndgameBase<ScaleFactor>* sf;
|
||||
|
||||
if ((sf = funcs->get<SF>(key)) != NULL)
|
||||
if (endgames.probe(key, sf))
|
||||
{
|
||||
mi->scalingFunction[sf->color()] = sf;
|
||||
return mi;
|
||||
e->scalingFunction[sf->color()] = sf;
|
||||
return e;
|
||||
}
|
||||
|
||||
// Generic scaling functions that refer to more then one material
|
||||
// distribution. Should be probed after the specialized ones.
|
||||
// Note that these ones don't return after setting the function.
|
||||
if (is_KBPsK<WHITE>(pos))
|
||||
mi->scalingFunction[WHITE] = &ScaleKBPsK[WHITE];
|
||||
if (is_KBPsKs<WHITE>(pos))
|
||||
e->scalingFunction[WHITE] = &ScaleKBPsK[WHITE];
|
||||
|
||||
if (is_KBPsK<BLACK>(pos))
|
||||
mi->scalingFunction[BLACK] = &ScaleKBPsK[BLACK];
|
||||
if (is_KBPsKs<BLACK>(pos))
|
||||
e->scalingFunction[BLACK] = &ScaleKBPsK[BLACK];
|
||||
|
||||
if (is_KQKRPs<WHITE>(pos))
|
||||
mi->scalingFunction[WHITE] = &ScaleKQKRPs[WHITE];
|
||||
e->scalingFunction[WHITE] = &ScaleKQKRPs[WHITE];
|
||||
|
||||
else if (is_KQKRPs<BLACK>(pos))
|
||||
mi->scalingFunction[BLACK] = &ScaleKQKRPs[BLACK];
|
||||
e->scalingFunction[BLACK] = &ScaleKQKRPs[BLACK];
|
||||
|
||||
if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) == VALUE_ZERO)
|
||||
Value npm_w = pos.non_pawn_material(WHITE);
|
||||
Value npm_b = pos.non_pawn_material(BLACK);
|
||||
|
||||
if (npm_w + npm_b == VALUE_ZERO)
|
||||
{
|
||||
if (pos.piece_count(BLACK, PAWN) == 0)
|
||||
if (!pos.count<PAWN>(BLACK))
|
||||
{
|
||||
assert(pos.piece_count(WHITE, PAWN) >= 2);
|
||||
mi->scalingFunction[WHITE] = &ScaleKPsK[WHITE];
|
||||
assert(pos.count<PAWN>(WHITE) >= 2);
|
||||
e->scalingFunction[WHITE] = &ScaleKPsK[WHITE];
|
||||
}
|
||||
else if (pos.piece_count(WHITE, PAWN) == 0)
|
||||
else if (!pos.count<PAWN>(WHITE))
|
||||
{
|
||||
assert(pos.piece_count(BLACK, PAWN) >= 2);
|
||||
mi->scalingFunction[BLACK] = &ScaleKPsK[BLACK];
|
||||
assert(pos.count<PAWN>(BLACK) >= 2);
|
||||
e->scalingFunction[BLACK] = &ScaleKPsK[BLACK];
|
||||
}
|
||||
else if (pos.piece_count(WHITE, PAWN) == 1 && pos.piece_count(BLACK, PAWN) == 1)
|
||||
else if (pos.count<PAWN>(WHITE) == 1 && pos.count<PAWN>(BLACK) == 1)
|
||||
{
|
||||
// This is a special case because we set scaling functions
|
||||
// for both colors instead of only one.
|
||||
mi->scalingFunction[WHITE] = &ScaleKPKP[WHITE];
|
||||
mi->scalingFunction[BLACK] = &ScaleKPKP[BLACK];
|
||||
e->scalingFunction[WHITE] = &ScaleKPKP[WHITE];
|
||||
e->scalingFunction[BLACK] = &ScaleKPKP[BLACK];
|
||||
}
|
||||
}
|
||||
|
||||
// No pawns makes it difficult to win, even with a material advantage
|
||||
if (!pos.count<PAWN>(WHITE) && npm_w - npm_b <= BishopValueMg)
|
||||
{
|
||||
e->factor[WHITE] = (uint8_t)
|
||||
(npm_w == npm_b || npm_w < RookValueMg ? 0 : NoPawnsSF[std::min(pos.count<BISHOP>(WHITE), 2)]);
|
||||
}
|
||||
|
||||
if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg)
|
||||
{
|
||||
e->factor[BLACK] = (uint8_t)
|
||||
(npm_w == npm_b || npm_b < RookValueMg ? 0 : NoPawnsSF[std::min(pos.count<BISHOP>(BLACK), 2)]);
|
||||
}
|
||||
|
||||
// Compute the space weight
|
||||
if (pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK) >=
|
||||
2*QueenValueMidgame + 4*RookValueMidgame + 2*KnightValueMidgame)
|
||||
if (npm_w + npm_b >= 2 * QueenValueMg + 4 * RookValueMg + 2 * KnightValueMg)
|
||||
{
|
||||
int minorPieceCount = pos.piece_count(WHITE, KNIGHT)
|
||||
+ pos.piece_count(BLACK, KNIGHT)
|
||||
+ pos.piece_count(WHITE, BISHOP)
|
||||
+ pos.piece_count(BLACK, BISHOP);
|
||||
int minorPieceCount = pos.count<KNIGHT>(WHITE) + pos.count<BISHOP>(WHITE)
|
||||
+ pos.count<KNIGHT>(BLACK) + pos.count<BISHOP>(BLACK);
|
||||
|
||||
mi->spaceWeight = minorPieceCount * minorPieceCount;
|
||||
e->spaceWeight = make_score(minorPieceCount * minorPieceCount, 0);
|
||||
}
|
||||
|
||||
// Evaluate the material balance
|
||||
const int pieceCount[2][8] = {
|
||||
{ pos.piece_count(WHITE, BISHOP) > 1, pos.piece_count(WHITE, PAWN), pos.piece_count(WHITE, KNIGHT),
|
||||
pos.piece_count(WHITE, BISHOP), pos.piece_count(WHITE, ROOK), pos.piece_count(WHITE, QUEEN) },
|
||||
{ pos.piece_count(BLACK, BISHOP) > 1, pos.piece_count(BLACK, PAWN), pos.piece_count(BLACK, KNIGHT),
|
||||
pos.piece_count(BLACK, BISHOP), pos.piece_count(BLACK, ROOK), pos.piece_count(BLACK, QUEEN) } };
|
||||
// Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder
|
||||
// for the bishop pair "extended piece", this allow us to be more flexible
|
||||
// in defining bishop pair bonuses.
|
||||
const int pieceCount[COLOR_NB][PIECE_TYPE_NB] = {
|
||||
{ pos.count<BISHOP>(WHITE) > 1, pos.count<PAWN>(WHITE), pos.count<KNIGHT>(WHITE),
|
||||
pos.count<BISHOP>(WHITE) , pos.count<ROOK>(WHITE), pos.count<QUEEN >(WHITE) },
|
||||
{ pos.count<BISHOP>(BLACK) > 1, pos.count<PAWN>(BLACK), pos.count<KNIGHT>(BLACK),
|
||||
pos.count<BISHOP>(BLACK) , pos.count<ROOK>(BLACK), pos.count<QUEEN >(BLACK) } };
|
||||
|
||||
Color c, them;
|
||||
int sign, pt1, pt2, pc;
|
||||
int v, vv, matValue = 0;
|
||||
|
||||
for (c = WHITE, sign = 1; c <= BLACK; c++, sign = -sign)
|
||||
{
|
||||
// No pawns makes it difficult to win, even with a material advantage
|
||||
if ( pos.piece_count(c, PAWN) == 0
|
||||
&& pos.non_pawn_material(c) - pos.non_pawn_material(opposite_color(c)) <= BishopValueMidgame)
|
||||
{
|
||||
if ( pos.non_pawn_material(c) == pos.non_pawn_material(opposite_color(c))
|
||||
|| pos.non_pawn_material(c) < RookValueMidgame)
|
||||
mi->factor[c] = 0;
|
||||
else
|
||||
{
|
||||
switch (pos.piece_count(c, BISHOP)) {
|
||||
case 2:
|
||||
mi->factor[c] = 32;
|
||||
break;
|
||||
case 1:
|
||||
mi->factor[c] = 12;
|
||||
break;
|
||||
case 0:
|
||||
mi->factor[c] = 6;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Redundancy of major pieces, formula based on Kaufman's paper
|
||||
// "The Evaluation of Material Imbalances in Chess"
|
||||
// http://mywebpages.comcast.net/danheisman/Articles/evaluation_of_material_imbalance.htm
|
||||
if (pieceCount[c][ROOK] >= 1)
|
||||
matValue -= sign * ((pieceCount[c][ROOK] - 1) * RedundantRookPenalty + pieceCount[c][QUEEN] * RedundantQueenPenalty);
|
||||
|
||||
them = opposite_color(c);
|
||||
v = 0;
|
||||
|
||||
// Second-degree polynomial material imbalance by Tord Romstad
|
||||
//
|
||||
// We use PIECE_TYPE_NONE as a place holder for the bishop pair "extended piece",
|
||||
// this allow us to be more flexible in defining bishop pair bonuses.
|
||||
for (pt1 = PIECE_TYPE_NONE; pt1 <= QUEEN; pt1++)
|
||||
{
|
||||
pc = pieceCount[c][pt1];
|
||||
if (!pc)
|
||||
continue;
|
||||
|
||||
vv = LinearCoefficients[pt1];
|
||||
|
||||
for (pt2 = PIECE_TYPE_NONE; pt2 <= pt1; pt2++)
|
||||
vv += pieceCount[c][pt2] * QuadraticCoefficientsSameColor[pt1][pt2]
|
||||
+ pieceCount[them][pt2] * QuadraticCoefficientsOppositeColor[pt1][pt2];
|
||||
|
||||
v += pc * vv;
|
||||
}
|
||||
matValue += sign * v;
|
||||
}
|
||||
mi->value = int16_t(matValue / 16);
|
||||
return mi;
|
||||
e->value = (int16_t)((imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
/// EndgameFunctions member definitions
|
||||
/// 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.
|
||||
|
||||
EndgameFunctions::EndgameFunctions() {
|
||||
Phase game_phase(const Position& pos) {
|
||||
|
||||
add<EvaluationFunction<KNNK> >("KNNK");
|
||||
add<EvaluationFunction<KPK> >("KPK");
|
||||
add<EvaluationFunction<KBNK> >("KBNK");
|
||||
add<EvaluationFunction<KRKP> >("KRKP");
|
||||
add<EvaluationFunction<KRKB> >("KRKB");
|
||||
add<EvaluationFunction<KRKN> >("KRKN");
|
||||
add<EvaluationFunction<KQKR> >("KQKR");
|
||||
add<EvaluationFunction<KBBKN> >("KBBKN");
|
||||
Value npm = pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK);
|
||||
|
||||
add<ScalingFunction<KNPK> >("KNPK");
|
||||
add<ScalingFunction<KRPKR> >("KRPKR");
|
||||
add<ScalingFunction<KBPKB> >("KBPKB");
|
||||
add<ScalingFunction<KBPPKB> >("KBPPKB");
|
||||
add<ScalingFunction<KBPKN> >("KBPKN");
|
||||
add<ScalingFunction<KRPPKRP> >("KRPPKRP");
|
||||
return npm >= MidgameLimit ? PHASE_MIDGAME
|
||||
: npm <= EndgameLimit ? PHASE_ENDGAME
|
||||
: Phase(((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit));
|
||||
}
|
||||
|
||||
EndgameFunctions::~EndgameFunctions() {
|
||||
|
||||
for (EFMap::const_iterator it = maps.first.begin(); it != maps.first.end(); ++it)
|
||||
delete it->second;
|
||||
|
||||
for (SFMap::const_iterator it = maps.second.begin(); it != maps.second.end(); ++it)
|
||||
delete it->second;
|
||||
}
|
||||
|
||||
Key EndgameFunctions::buildKey(const string& keyCode) {
|
||||
|
||||
assert(keyCode.length() > 0 && keyCode.length() < 8);
|
||||
assert(keyCode[0] == 'K');
|
||||
|
||||
string fen;
|
||||
bool upcase = false;
|
||||
|
||||
// Build up a fen string with the given pieces, note that
|
||||
// the fen string could be of an illegal position.
|
||||
for (size_t i = 0; i < keyCode.length(); i++)
|
||||
{
|
||||
if (keyCode[i] == 'K')
|
||||
upcase = !upcase;
|
||||
|
||||
fen += char(upcase ? toupper(keyCode[i]) : tolower(keyCode[i]));
|
||||
}
|
||||
fen += char(8 - keyCode.length() + '0');
|
||||
fen += "/8/8/8/8/8/8/8 w - -";
|
||||
return Position(fen, 0).get_material_key();
|
||||
}
|
||||
|
||||
const string EndgameFunctions::swapColors(const string& keyCode) {
|
||||
|
||||
// Build corresponding key for the opposite color: "KBPKN" -> "KNKBP"
|
||||
size_t idx = keyCode.find('K', 1);
|
||||
return keyCode.substr(idx) + keyCode.substr(0, idx);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void EndgameFunctions::add(const string& keyCode) {
|
||||
|
||||
typedef typename T::Base F;
|
||||
typedef map<Key, F*> M;
|
||||
|
||||
const_cast<M&>(get<F>()).insert(pair<Key, F*>(buildKey(keyCode), new T(WHITE)));
|
||||
const_cast<M&>(get<F>()).insert(pair<Key, F*>(buildKey(swapColors(keyCode)), new T(BLACK)));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T* EndgameFunctions::get(Key key) const {
|
||||
|
||||
typename map<Key, T*>::const_iterator it = get<T>().find(key);
|
||||
return it != get<T>().end() ? it->second : NULL;
|
||||
}
|
||||
} // namespace Material
|
||||
|
||||
+27
-112
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,146 +17,61 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(MATERIAL_H_INCLUDED)
|
||||
#ifndef MATERIAL_H_INCLUDED
|
||||
#define MATERIAL_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include "endgame.h"
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace Material {
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
const int MaterialTableSize = 1024;
|
||||
|
||||
/// MaterialInfo is a class which contains various information about a
|
||||
/// material configuration. It contains a material balance evaluation,
|
||||
/// a function pointer to a special endgame evaluation function (which in
|
||||
/// most cases is NULL, meaning that the standard evaluation function will
|
||||
/// be used), and "scale factors" for black and white.
|
||||
/// Material::Entry contains various information about a material configuration.
|
||||
/// It contains a material balance evaluation, a function pointer to a special
|
||||
/// endgame evaluation function (which in most cases is NULL, meaning that the
|
||||
/// standard evaluation function will be used), and "scale factors".
|
||||
///
|
||||
/// The scale factors are used to scale the evaluation score up or down.
|
||||
/// For instance, in KRB vs KR endgames, the score is scaled down by a factor
|
||||
/// of 4, which will result in scores of absolute value less than one pawn.
|
||||
|
||||
class MaterialInfo {
|
||||
struct Entry {
|
||||
|
||||
friend class MaterialInfoTable;
|
||||
|
||||
public:
|
||||
Score material_value() const;
|
||||
Score material_value() const { return make_score(value, value); }
|
||||
Score space_weight() const { return spaceWeight; }
|
||||
Phase game_phase() const { return gamePhase; }
|
||||
bool specialized_eval_exists() const { return evaluationFunction != NULL; }
|
||||
Value evaluate(const Position& p) const { return (*evaluationFunction)(p); }
|
||||
ScaleFactor scale_factor(const Position& pos, Color c) const;
|
||||
int space_weight() const;
|
||||
Phase game_phase() const;
|
||||
bool specialized_eval_exists() const;
|
||||
Value evaluate(const Position& pos) const;
|
||||
|
||||
private:
|
||||
Key key;
|
||||
int16_t value;
|
||||
uint8_t factor[2];
|
||||
EndgameEvaluationFunctionBase* evaluationFunction;
|
||||
EndgameScalingFunctionBase* scalingFunction[2];
|
||||
int spaceWeight;
|
||||
uint8_t factor[COLOR_NB];
|
||||
EndgameBase<Value>* evaluationFunction;
|
||||
EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB];
|
||||
Score spaceWeight;
|
||||
Phase gamePhase;
|
||||
};
|
||||
|
||||
/// The MaterialInfoTable class represents a pawn hash table. It is basically
|
||||
/// just an array of MaterialInfo objects and a few methods for accessing these
|
||||
/// objects. The most important method is get_material_info, which looks up a
|
||||
/// position in the table and returns a pointer to a MaterialInfo object.
|
||||
class EndgameFunctions;
|
||||
typedef HashTable<Entry, 8192> Table;
|
||||
|
||||
class MaterialInfoTable {
|
||||
Entry* probe(const Position& pos, Table& entries, Endgames& endgames);
|
||||
Phase game_phase(const Position& pos);
|
||||
|
||||
MaterialInfoTable(const MaterialInfoTable&);
|
||||
MaterialInfoTable& operator=(const MaterialInfoTable&);
|
||||
|
||||
public:
|
||||
MaterialInfoTable();
|
||||
~MaterialInfoTable();
|
||||
MaterialInfo* get_material_info(const Position& pos);
|
||||
|
||||
static Phase game_phase(const Position& pos);
|
||||
|
||||
private:
|
||||
MaterialInfo* entries;
|
||||
EndgameFunctions* funcs;
|
||||
};
|
||||
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
|
||||
/// MaterialInfo::material_value simply returns the material balance
|
||||
/// evaluation that is independent from game phase.
|
||||
|
||||
inline Score MaterialInfo::material_value() const {
|
||||
|
||||
return make_score(value, value);
|
||||
}
|
||||
|
||||
/// MaterialInfo::scale_factor takes a position and a color as input, and
|
||||
/// Material::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, because the scale factor need not
|
||||
/// to be a constant: It can also be a function which should be applied to
|
||||
/// the position. For instance, in KBP vs K endgames, a scaling function
|
||||
/// which checks for draws with rook pawns and wrong-colored bishops.
|
||||
|
||||
inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) const {
|
||||
inline ScaleFactor Entry::scale_factor(const Position& pos, Color c) const {
|
||||
|
||||
if (scalingFunction[c] != NULL)
|
||||
{
|
||||
ScaleFactor sf = scalingFunction[c]->apply(pos);
|
||||
if (sf != SCALE_FACTOR_NONE)
|
||||
return sf;
|
||||
}
|
||||
return ScaleFactor(factor[c]);
|
||||
return !scalingFunction[c] || (*scalingFunction[c])(pos) == SCALE_FACTOR_NONE
|
||||
? ScaleFactor(factor[c]) : (*scalingFunction[c])(pos);
|
||||
}
|
||||
|
||||
|
||||
/// MaterialInfo::space_weight() simply returns the weight for the space
|
||||
/// evaluation for this material configuration.
|
||||
|
||||
inline int MaterialInfo::space_weight() const {
|
||||
|
||||
return spaceWeight;
|
||||
}
|
||||
|
||||
/// MaterialInfo::game_phase() returns the game phase according
|
||||
/// to this material configuration.
|
||||
|
||||
inline Phase MaterialInfo::game_phase() const {
|
||||
|
||||
return gamePhase;
|
||||
}
|
||||
|
||||
|
||||
/// MaterialInfo::specialized_eval_exists decides whether there is a
|
||||
/// specialized evaluation function for the current material configuration,
|
||||
/// or if the normal evaluation function should be used.
|
||||
|
||||
inline bool MaterialInfo::specialized_eval_exists() const {
|
||||
|
||||
return evaluationFunction != NULL;
|
||||
}
|
||||
|
||||
|
||||
/// MaterialInfo::evaluate applies a specialized evaluation function
|
||||
/// to a given position object. It should only be called when
|
||||
/// specialized_eval_exists() returns 'true'.
|
||||
|
||||
inline Value MaterialInfo::evaluate(const Position& pos) const {
|
||||
|
||||
return evaluationFunction->apply(pos);
|
||||
}
|
||||
|
||||
#endif // !defined(MATERIAL_H_INCLUDED)
|
||||
#endif // #ifndef MATERIAL_H_INCLUDED
|
||||
|
||||
+119
-205
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,274 +17,188 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#if !defined(_MSC_VER)
|
||||
|
||||
# include <sys/time.h>
|
||||
# include <sys/types.h>
|
||||
# include <unistd.h>
|
||||
# if defined(__hpux)
|
||||
# include <sys/pstat.h>
|
||||
# endif
|
||||
|
||||
#else
|
||||
|
||||
#define _CRT_SECURE_NO_DEPRECATE
|
||||
#include <windows.h>
|
||||
#include <sys/timeb.h>
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined(NO_PREFETCH)
|
||||
# include <xmmintrin.h>
|
||||
#endif
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include "bitcount.h"
|
||||
#include "misc.h"
|
||||
#include "thread.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Version number. If this is left empty, the current date (in the format
|
||||
/// YYMMDD) is used as a version number.
|
||||
|
||||
static const string EngineVersion = "2.0";
|
||||
static const string AppName = "Stockfish";
|
||||
static const string AppTag = "";
|
||||
/// Version number. If Version is left empty, then compile date, in the
|
||||
/// format DD-MM-YY, is shown in engine_info.
|
||||
static const string Version = "4";
|
||||
|
||||
|
||||
////
|
||||
//// Variables
|
||||
////
|
||||
/// 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
|
||||
/// the program was compiled) or "Stockfish <Version>", depending on whether
|
||||
/// Version is empty.
|
||||
|
||||
static uint64_t dbg_cnt0 = 0;
|
||||
static uint64_t dbg_cnt1 = 0;
|
||||
|
||||
bool dbg_show_mean = false;
|
||||
bool dbg_show_hit_rate = false;
|
||||
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
void dbg_hit_on(bool b) {
|
||||
|
||||
assert(!dbg_show_mean);
|
||||
dbg_show_hit_rate = true;
|
||||
dbg_cnt0++;
|
||||
if (b)
|
||||
dbg_cnt1++;
|
||||
}
|
||||
|
||||
void dbg_hit_on_c(bool c, bool b) {
|
||||
|
||||
if (c)
|
||||
dbg_hit_on(b);
|
||||
}
|
||||
|
||||
void dbg_before() {
|
||||
|
||||
assert(!dbg_show_mean);
|
||||
dbg_show_hit_rate = true;
|
||||
dbg_cnt0++;
|
||||
}
|
||||
|
||||
void dbg_after() {
|
||||
|
||||
assert(!dbg_show_mean);
|
||||
dbg_show_hit_rate = true;
|
||||
dbg_cnt1++;
|
||||
}
|
||||
|
||||
void dbg_mean_of(int v) {
|
||||
|
||||
assert(!dbg_show_hit_rate);
|
||||
dbg_show_mean = true;
|
||||
dbg_cnt0++;
|
||||
dbg_cnt1 += v;
|
||||
}
|
||||
|
||||
void dbg_print_hit_rate() {
|
||||
|
||||
cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
|
||||
<< " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
|
||||
}
|
||||
|
||||
void dbg_print_mean() {
|
||||
|
||||
cout << "Total " << dbg_cnt0 << " Mean "
|
||||
<< (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
|
||||
}
|
||||
|
||||
|
||||
/// engine_name() returns the full name of the current Stockfish version.
|
||||
/// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when
|
||||
/// the program was compiled) or "Stockfish <version number>", depending
|
||||
/// on whether the constant EngineVersion (defined in misc.h) is empty.
|
||||
|
||||
const string engine_name() {
|
||||
const string engine_info(bool to_uci) {
|
||||
|
||||
const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
|
||||
const string cpu64(CpuIs64Bit ? " 64bit" : "");
|
||||
|
||||
if (!EngineVersion.empty())
|
||||
return AppName + " " + EngineVersion + cpu64;
|
||||
|
||||
stringstream s, date(__DATE__); // From compiler, format is "Sep 21 2008"
|
||||
string month, day, year;
|
||||
stringstream s, date(__DATE__); // From compiler, format is "Sep 21 2008"
|
||||
|
||||
date >> month >> day >> year;
|
||||
s << "Stockfish " << Version << setfill('0');
|
||||
|
||||
s << setfill('0') << AppName + " " + AppTag + " "
|
||||
<< year.substr(2, 2) << setw(2)
|
||||
<< (1 + months.find(month) / 4) << setw(2)
|
||||
<< day << cpu64;
|
||||
if (Version.empty())
|
||||
{
|
||||
date >> month >> day >> year;
|
||||
s << setw(2) << day << setw(2) << (1 + months.find(month) / 4) << year.substr(2);
|
||||
}
|
||||
|
||||
s << (Is64Bit ? " 64" : "")
|
||||
<< (HasPopCnt ? " SSE4.2" : "")
|
||||
<< (to_uci ? "\nid author ": " by ")
|
||||
<< "Tord Romstad, Marco Costalba and Joona Kiiski";
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
|
||||
/// get_system_time() returns the current system time, measured in
|
||||
/// milliseconds.
|
||||
/// Debug functions used mainly to collect run-time statistics
|
||||
|
||||
int get_system_time() {
|
||||
static uint64_t hits[2], means[2];
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
struct _timeb t;
|
||||
_ftime(&t);
|
||||
return int(t.time*1000 + t.millitm);
|
||||
#else
|
||||
struct timeval t;
|
||||
gettimeofday(&t, NULL);
|
||||
return t.tv_sec*1000 + t.tv_usec/1000;
|
||||
#endif
|
||||
void dbg_hit_on(bool b) { hits[0]++; if (b) hits[1]++; }
|
||||
void dbg_hit_on_c(bool c, bool b) { if (c) dbg_hit_on(b); }
|
||||
void dbg_mean_of(int v) { means[0]++; means[1] += v; }
|
||||
|
||||
void dbg_print() {
|
||||
|
||||
if (hits[0])
|
||||
cerr << "Total " << hits[0] << " Hits " << hits[1]
|
||||
<< " hit rate (%) " << 100 * hits[1] / hits[0] << endl;
|
||||
|
||||
if (means[0])
|
||||
cerr << "Total " << means[0] << " Mean "
|
||||
<< (float)means[1] / means[0] << endl;
|
||||
}
|
||||
|
||||
|
||||
/// cpu_count() tries to detect the number of CPU cores.
|
||||
/// 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 while preserving
|
||||
/// usual i/o functionality and without changing a single line of code!
|
||||
/// Idea from http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81
|
||||
|
||||
#if !defined(_MSC_VER)
|
||||
struct Tie: public streambuf { // MSVC requires splitted streambuf for cin and cout
|
||||
|
||||
# if defined(_SC_NPROCESSORS_ONLN)
|
||||
int cpu_count() {
|
||||
return Min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREADS);
|
||||
}
|
||||
# elif defined(__hpux)
|
||||
int cpu_count() {
|
||||
struct pst_dynamic psd;
|
||||
if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1)
|
||||
return 1;
|
||||
Tie(streambuf* b, ofstream* f) : buf(b), file(f) {}
|
||||
|
||||
return Min(psd.psd_proc_cnt, MAX_THREADS);
|
||||
}
|
||||
# else
|
||||
int cpu_count() {
|
||||
return 1;
|
||||
}
|
||||
# endif
|
||||
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(), ">> "); }
|
||||
|
||||
#else
|
||||
streambuf* buf;
|
||||
ofstream* file;
|
||||
|
||||
int cpu_count() {
|
||||
SYSTEM_INFO s;
|
||||
GetSystemInfo(&s);
|
||||
return Min(s.dwNumberOfProcessors, MAX_THREADS);
|
||||
}
|
||||
int log(int c, const char* prefix) {
|
||||
|
||||
#endif
|
||||
static int last = '\n';
|
||||
|
||||
if (last == '\n')
|
||||
file->rdbuf()->sputn(prefix, 3);
|
||||
|
||||
/// Check for console input. Original code from Beowulf and Olithink
|
||||
return last = file->rdbuf()->sputc((char)c);
|
||||
}
|
||||
};
|
||||
|
||||
#ifndef _WIN32
|
||||
class Logger {
|
||||
|
||||
int data_available()
|
||||
{
|
||||
fd_set readfds;
|
||||
struct timeval timeout;
|
||||
Logger() : in(cin.rdbuf(), &file), out(cout.rdbuf(), &file) {}
|
||||
~Logger() { start(false); }
|
||||
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(fileno(stdin), &readfds);
|
||||
timeout.tv_sec = 0; // Set to timeout immediately
|
||||
timeout.tv_usec = 0;
|
||||
select(16, &readfds, 0, 0, &timeout);
|
||||
ofstream file;
|
||||
Tie in, out;
|
||||
|
||||
return (FD_ISSET(fileno(stdin), &readfds));
|
||||
}
|
||||
public:
|
||||
static void start(bool b) {
|
||||
|
||||
#else
|
||||
static Logger l;
|
||||
|
||||
int data_available()
|
||||
{
|
||||
static HANDLE inh = NULL;
|
||||
static bool usePipe;
|
||||
INPUT_RECORD rec[256];
|
||||
DWORD dw, recCnt;
|
||||
|
||||
if (!inh)
|
||||
if (b && !l.file.is_open())
|
||||
{
|
||||
inh = GetStdHandle(STD_INPUT_HANDLE);
|
||||
usePipe = !GetConsoleMode(inh, &dw);
|
||||
if (!usePipe)
|
||||
{
|
||||
SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
|
||||
FlushConsoleInputBuffer(inh);
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// If we're running under XBoard then we can't use PeekConsoleInput() as
|
||||
// the input commands are sent to us directly over the internal pipe.
|
||||
if (usePipe)
|
||||
return PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL) ? dw : 1;
|
||||
|
||||
// Count the number of unread input records, including keyboard,
|
||||
// mouse, and window-resizing input records.
|
||||
GetNumberOfConsoleInputEvents(inh, &dw);
|
||||
/// Used to serialize access to std::cout to avoid multiple threads to write at
|
||||
/// the same time.
|
||||
|
||||
// Read data from console without removing it from the buffer
|
||||
if (dw <= 0 || !PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
|
||||
return 0;
|
||||
std::ostream& operator<<(std::ostream& os, SyncCout sc) {
|
||||
|
||||
// Search for at least one keyboard event
|
||||
for (DWORD i = 0; i < recCnt; i++)
|
||||
if (rec[i].EventType == KEY_EVENT)
|
||||
return 1;
|
||||
static Mutex m;
|
||||
|
||||
return 0;
|
||||
if (sc == io_lock)
|
||||
m.lock();
|
||||
|
||||
if (sc == io_unlock)
|
||||
m.unlock();
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
/// Trampoline helper to avoid moving Logger to misc.h
|
||||
void start_logger(bool b) { Logger::start(b); }
|
||||
|
||||
|
||||
/// timed_wait() waits for msec milliseconds. It is mainly an helper to wrap
|
||||
/// 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 function and do not stalls the CPU waiting for data to be
|
||||
/// loaded from RAM, that can be very slow.
|
||||
#if defined(NO_PREFETCH)
|
||||
/// loaded from memory, that can be quite slow.
|
||||
#ifdef NO_PREFETCH
|
||||
|
||||
void prefetch(char*) {}
|
||||
|
||||
#else
|
||||
|
||||
void prefetch(char* addr) {
|
||||
|
||||
#if defined(__INTEL_COMPILER) || defined(__ICL)
|
||||
# if defined(__INTEL_COMPILER)
|
||||
// This hack prevents prefetches to be optimized away by
|
||||
// Intel compiler. Both MSVC and gcc seems not affected.
|
||||
__asm__ ("");
|
||||
#endif
|
||||
# endif
|
||||
|
||||
_mm_prefetch(addr, _MM_HINT_T2);
|
||||
_mm_prefetch(addr+64, _MM_HINT_T2); // 64 bytes ahead
|
||||
# if defined(__INTEL_COMPILER) || defined(_MSC_VER)
|
||||
_mm_prefetch(addr, _MM_HINT_T0);
|
||||
# else
|
||||
__builtin_prefetch(addr);
|
||||
# endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
+37
-38
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,53 +17,52 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(MISC_H_INCLUDED)
|
||||
#ifndef MISC_H_INCLUDED
|
||||
#define MISC_H_INCLUDED
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
////
|
||||
//// Macros
|
||||
////
|
||||
|
||||
#define Min(x, y) (((x) < (y))? (x) : (y))
|
||||
#define Max(x, y) (((x) < (y))? (y) : (x))
|
||||
|
||||
|
||||
////
|
||||
//// Prototypes
|
||||
////
|
||||
|
||||
extern const std::string engine_name();
|
||||
extern int get_system_time();
|
||||
extern int cpu_count();
|
||||
extern int data_available();
|
||||
extern const std::string engine_info(bool to_uci = false);
|
||||
extern void timed_wait(WaitCondition&, Lock&, int);
|
||||
extern void prefetch(char* addr);
|
||||
extern void prefetchPawn(Key, int);
|
||||
|
||||
|
||||
////
|
||||
//// Debug
|
||||
////
|
||||
|
||||
extern bool dbg_show_mean;
|
||||
extern bool dbg_show_hit_rate;
|
||||
extern void start_logger(bool b);
|
||||
|
||||
extern void dbg_hit_on(bool b);
|
||||
extern void dbg_hit_on_c(bool c, bool b);
|
||||
extern void dbg_before();
|
||||
extern void dbg_after();
|
||||
extern void dbg_mean_of(int v);
|
||||
extern void dbg_print_hit_rate();
|
||||
extern void dbg_print_mean();
|
||||
extern void dbg_print();
|
||||
|
||||
#endif // !defined(MISC_H_INCLUDED)
|
||||
|
||||
struct Log : public std::ofstream {
|
||||
Log(const std::string& f = "log.txt") : std::ofstream(f.c_str(), std::ios::out | std::ios::app) {}
|
||||
~Log() { if (is_open()) close(); }
|
||||
};
|
||||
|
||||
|
||||
namespace Time {
|
||||
typedef int64_t point;
|
||||
inline point now() { return system_time_to_msec(); }
|
||||
}
|
||||
|
||||
|
||||
template<class Entry, int Size>
|
||||
struct HashTable {
|
||||
HashTable() : e(Size, Entry()) {}
|
||||
Entry* operator[](Key k) { return &e[(uint32_t)k & (Size - 1)]; }
|
||||
|
||||
private:
|
||||
std::vector<Entry> e;
|
||||
};
|
||||
|
||||
|
||||
enum SyncCout { io_lock, io_unlock };
|
||||
std::ostream& operator<<(std::ostream&, SyncCout);
|
||||
|
||||
#define sync_cout std::cout << io_lock
|
||||
#define sync_endl std::endl << io_unlock
|
||||
|
||||
#endif // #ifndef MISC_H_INCLUDED
|
||||
|
||||
-152
@@ -1,152 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 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/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cassert>
|
||||
#include <cctype>
|
||||
|
||||
#include "move.h"
|
||||
#include "piece.h"
|
||||
#include "position.h"
|
||||
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
/// move_from_uci() takes a position and a string as input, and attempts to
|
||||
/// convert the string to a move, using simple coordinate notation (g1f3,
|
||||
/// a7a8q, etc.). In order to correctly parse en passant captures and castling
|
||||
/// moves, we need the position. This function is not robust, and expects that
|
||||
/// the input move is legal and correctly formatted.
|
||||
|
||||
Move move_from_uci(const Position& pos, const std::string& str) {
|
||||
|
||||
Square from, to;
|
||||
Piece piece;
|
||||
Color us = pos.side_to_move();
|
||||
|
||||
if (str.length() < 4)
|
||||
return MOVE_NONE;
|
||||
|
||||
// Read the from and to squares
|
||||
from = make_square(file_from_char(str[0]), rank_from_char(str[1]));
|
||||
to = make_square(file_from_char(str[2]), rank_from_char(str[3]));
|
||||
|
||||
// Find the moving piece
|
||||
piece = pos.piece_on(from);
|
||||
|
||||
// If the string has more than 4 characters, try to interpret the 5th
|
||||
// character as a promotion.
|
||||
if (str.length() > 4 && piece == piece_of_color_and_type(us, PAWN))
|
||||
{
|
||||
switch (tolower(str[4])) {
|
||||
case 'n':
|
||||
return make_promotion_move(from, to, KNIGHT);
|
||||
case 'b':
|
||||
return make_promotion_move(from, to, BISHOP);
|
||||
case 'r':
|
||||
return make_promotion_move(from, to, ROOK);
|
||||
case 'q':
|
||||
return make_promotion_move(from, to, QUEEN);
|
||||
}
|
||||
}
|
||||
|
||||
// En passant move? We assume that a pawn move is an en passant move
|
||||
// if the destination square is epSquare.
|
||||
if (to == pos.ep_square() && piece == piece_of_color_and_type(us, PAWN))
|
||||
return make_ep_move(from, to);
|
||||
|
||||
// Is this a castling move? A king move is assumed to be a castling move
|
||||
// if the destination square is occupied by a friendly rook, or if the
|
||||
// distance between the source and destination squares is more than 1.
|
||||
if (piece == piece_of_color_and_type(us, KING))
|
||||
{
|
||||
if (pos.piece_on(to) == piece_of_color_and_type(us, ROOK))
|
||||
return make_castle_move(from, to);
|
||||
|
||||
if (square_distance(from, to) > 1)
|
||||
{
|
||||
// This is a castling move, but we have to translate it to the
|
||||
// internal "king captures rook" representation.
|
||||
SquareDelta delta = (to > from ? DELTA_E : DELTA_W);
|
||||
Square s = from;
|
||||
|
||||
do s += delta;
|
||||
while ( pos.piece_on(s) != piece_of_color_and_type(us, ROOK)
|
||||
&& relative_rank(us, s) == RANK_1);
|
||||
|
||||
return relative_rank(us, s) == RANK_1 ? make_castle_move(from, s) : MOVE_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
return make_move(from, to);
|
||||
}
|
||||
|
||||
|
||||
/// 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.
|
||||
|
||||
const std::string move_to_uci(Move move, bool chess960) {
|
||||
|
||||
std::string str;
|
||||
Square from = move_from(move);
|
||||
Square to = move_to(move);
|
||||
|
||||
if (move == MOVE_NONE)
|
||||
str = "(none)";
|
||||
else if (move == MOVE_NULL)
|
||||
str = "0000";
|
||||
else
|
||||
{
|
||||
if (move_is_short_castle(move) && !chess960)
|
||||
return (from == SQ_E1 ? "e1g1" : "e8g8");
|
||||
|
||||
if (move_is_long_castle(move) && !chess960)
|
||||
return (from == SQ_E1 ? "e1c1" : "e8c8");
|
||||
|
||||
str = square_to_string(from) + square_to_string(to);
|
||||
if (move_is_promotion(move))
|
||||
str += char(tolower(piece_type_to_char(move_promotion_piece(move))));
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
/// Overload the << operator, to make it easier to print moves.
|
||||
|
||||
std::ostream& operator << (std::ostream& os, Move m) {
|
||||
|
||||
bool chess960 = (os.iword(0) != 0); // See set960()
|
||||
return os << move_to_uci(m, chess960);
|
||||
}
|
||||
|
||||
|
||||
/// move_is_ok(), for debugging.
|
||||
|
||||
bool move_is_ok(Move m) {
|
||||
|
||||
return square_is_ok(move_from(m)) && square_is_ok(move_to(m));
|
||||
}
|
||||
-211
@@ -1,211 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(MOVE_H_INCLUDED)
|
||||
#define MOVE_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "misc.h"
|
||||
#include "piece.h"
|
||||
#include "square.h"
|
||||
|
||||
// Maximum number of allowed moves per position
|
||||
const int MOVES_MAX = 256;
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
class Position;
|
||||
|
||||
/// A move needs 17 bits to be stored
|
||||
///
|
||||
/// bit 0- 5: destination square (from 0 to 63)
|
||||
/// bit 6-11: origin square (from 0 to 63)
|
||||
/// bit 12-14: promotion piece type
|
||||
/// bit 15: en passant flag
|
||||
/// bit 16: castle flag
|
||||
///
|
||||
/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in
|
||||
/// because in any normal move destination square is always different
|
||||
/// from origin square while MOVE_NONE and MOVE_NULL have the same
|
||||
/// origin and destination square, 0 and 1 respectively.
|
||||
|
||||
enum Move {
|
||||
MOVE_NONE = 0,
|
||||
MOVE_NULL = 65
|
||||
};
|
||||
|
||||
|
||||
struct MoveStack {
|
||||
Move move;
|
||||
int score;
|
||||
};
|
||||
|
||||
inline bool operator<(const MoveStack& f, const MoveStack& s) { return f.score < s.score; }
|
||||
|
||||
// An helper insertion sort implementation, works with pointers and iterators
|
||||
template<typename T, typename K>
|
||||
inline void insertion_sort(K firstMove, K lastMove)
|
||||
{
|
||||
T value;
|
||||
K cur, p, d;
|
||||
|
||||
if (firstMove != lastMove)
|
||||
for (cur = firstMove + 1; cur != lastMove; cur++)
|
||||
{
|
||||
p = d = cur;
|
||||
value = *p--;
|
||||
if (*p < value)
|
||||
{
|
||||
do *d = *p;
|
||||
while (--d != firstMove && *--p < value);
|
||||
*d = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Our dedicated sort in range [firstMove, lastMove), first splits
|
||||
// positive scores from ramining then order seaprately the two sets.
|
||||
template<typename T>
|
||||
inline void sort_moves(T* firstMove, T* lastMove, T** lastPositive)
|
||||
{
|
||||
T tmp;
|
||||
T *p, *d;
|
||||
|
||||
d = lastMove;
|
||||
p = firstMove - 1;
|
||||
|
||||
d->score = -1; // right guard
|
||||
|
||||
// Split positives vs non-positives
|
||||
do {
|
||||
while ((++p)->score > 0) {}
|
||||
|
||||
if (p != d)
|
||||
{
|
||||
while (--d != p && d->score <= 0) {}
|
||||
|
||||
tmp = *p;
|
||||
*p = *d;
|
||||
*d = tmp;
|
||||
}
|
||||
|
||||
} while (p != d);
|
||||
|
||||
// Sort just positive scored moves, remaining only when we get there
|
||||
insertion_sort<T, T*>(firstMove, p);
|
||||
*lastPositive = p;
|
||||
}
|
||||
|
||||
// Picks up the best move in range [curMove, lastMove), one per cycle.
|
||||
// It is faster then sorting all the moves in advance when moves are few,
|
||||
// as normally are the possible captures. Note that is not a stable alghoritm.
|
||||
template<typename T>
|
||||
inline T pick_best(T* curMove, T* lastMove)
|
||||
{
|
||||
T bestMove, tmp;
|
||||
|
||||
bestMove = *curMove;
|
||||
while (++curMove != lastMove)
|
||||
{
|
||||
if (bestMove < *curMove)
|
||||
{
|
||||
tmp = *curMove;
|
||||
*curMove = bestMove;
|
||||
bestMove = tmp;
|
||||
}
|
||||
}
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
inline Square move_from(Move m) {
|
||||
return Square((int(m) >> 6) & 0x3F);
|
||||
}
|
||||
|
||||
inline Square move_to(Move m) {
|
||||
return Square(m & 0x3F);
|
||||
}
|
||||
|
||||
inline PieceType move_promotion_piece(Move m) {
|
||||
return PieceType((int(m) >> 12) & 7);
|
||||
}
|
||||
|
||||
inline int move_is_special(Move m) {
|
||||
return m & (0x1F << 12);
|
||||
}
|
||||
|
||||
inline int move_is_promotion(Move m) {
|
||||
return m & (7 << 12);
|
||||
}
|
||||
|
||||
inline int move_is_ep(Move m) {
|
||||
return m & (1 << 15);
|
||||
}
|
||||
|
||||
inline int move_is_castle(Move m) {
|
||||
return m & (1 << 16);
|
||||
}
|
||||
|
||||
inline bool move_is_short_castle(Move m) {
|
||||
return move_is_castle(m) && (move_to(m) > move_from(m));
|
||||
}
|
||||
|
||||
inline bool move_is_long_castle(Move m) {
|
||||
return move_is_castle(m) && (move_to(m) < move_from(m));
|
||||
}
|
||||
|
||||
inline Move make_promotion_move(Square from, Square to, PieceType promotion) {
|
||||
return Move(int(to) | (int(from) << 6) | (int(promotion) << 12));
|
||||
}
|
||||
|
||||
inline Move make_move(Square from, Square to) {
|
||||
return Move(int(to) | (int(from) << 6));
|
||||
}
|
||||
|
||||
inline Move make_castle_move(Square from, Square to) {
|
||||
return Move(int(to) | (int(from) << 6) | (1 << 16));
|
||||
}
|
||||
|
||||
inline Move make_ep_move(Square from, Square to) {
|
||||
return Move(int(to) | (int(from) << 6) | (1 << 15));
|
||||
}
|
||||
|
||||
|
||||
////
|
||||
//// Prototypes
|
||||
////
|
||||
|
||||
extern std::ostream& operator<<(std::ostream& os, Move m);
|
||||
extern Move move_from_uci(const Position& pos, const std::string &str);
|
||||
extern const std::string move_to_uci(Move m, bool chess960);
|
||||
extern bool move_is_ok(Move m);
|
||||
|
||||
|
||||
#endif // !defined(MOVE_H_INCLUDED)
|
||||
+323
-599
File diff suppressed because it is too large
Load Diff
+32
-19
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,29 +17,42 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(MOVEGEN_H_INCLUDED)
|
||||
#ifndef MOVEGEN_H_INCLUDED
|
||||
#define MOVEGEN_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
#include "types.h"
|
||||
|
||||
#include "position.h"
|
||||
enum GenType {
|
||||
CAPTURES,
|
||||
QUIETS,
|
||||
QUIET_CHECKS,
|
||||
EVASIONS,
|
||||
NON_EVASIONS,
|
||||
LEGAL
|
||||
};
|
||||
|
||||
class Position;
|
||||
|
||||
////
|
||||
//// Prototypes
|
||||
////
|
||||
template<GenType>
|
||||
ExtMove* generate(const Position& pos, ExtMove* mlist);
|
||||
|
||||
extern MoveStack* generate_captures(const Position& pos, MoveStack* mlist);
|
||||
extern MoveStack* generate_noncaptures(const Position& pos, MoveStack* mlist);
|
||||
extern MoveStack* generate_non_capture_checks(const Position& pos, MoveStack* mlist);
|
||||
extern MoveStack* generate_evasions(const Position& pos, MoveStack* mlist);
|
||||
extern MoveStack* generate_non_evasions(const Position& pos, MoveStack* mlist);
|
||||
extern MoveStack* generate_moves(const Position& pos, MoveStack* mlist, bool pseudoLegal = false);
|
||||
extern bool move_is_legal(const Position& pos, const Move m, Bitboard pinned);
|
||||
extern bool move_is_legal(const Position& pos, const Move m);
|
||||
/// The MoveList struct is a simple wrapper around generate(), sometimes comes
|
||||
/// handy to use this class instead of the low level generate() function.
|
||||
template<GenType T>
|
||||
struct MoveList {
|
||||
|
||||
explicit MoveList(const Position& pos) : cur(mlist), last(generate<T>(pos, mlist)) { last->move = MOVE_NONE; }
|
||||
void operator++() { cur++; }
|
||||
Move operator*() const { return cur->move; }
|
||||
size_t size() const { return last - mlist; }
|
||||
bool contains(Move m) const {
|
||||
for (const ExtMove* it(mlist); it != last; ++it) if (it->move == m) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif // !defined(MOVEGEN_H_INCLUDED)
|
||||
private:
|
||||
ExtMove mlist[MAX_MOVES];
|
||||
ExtMove *cur, *last;
|
||||
};
|
||||
|
||||
#endif // #ifndef MOVEGEN_H_INCLUDED
|
||||
|
||||
+282
-262
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -18,174 +18,132 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "history.h"
|
||||
#include "movegen.h"
|
||||
#include "movepick.h"
|
||||
#include "search.h"
|
||||
#include "value.h"
|
||||
|
||||
|
||||
////
|
||||
//// Local definitions
|
||||
////
|
||||
#include "thread.h"
|
||||
|
||||
namespace {
|
||||
|
||||
enum MovegenPhase {
|
||||
PH_TT_MOVES, // Transposition table move and mate killer
|
||||
PH_GOOD_CAPTURES, // Queen promotions and captures with SEE values >= 0
|
||||
PH_KILLERS, // Killer moves from the current ply
|
||||
PH_NONCAPTURES, // Non-captures and underpromotions
|
||||
PH_BAD_CAPTURES, // Queen promotions and captures with SEE values < 0
|
||||
PH_EVASIONS, // Check evasions
|
||||
PH_QCAPTURES, // Captures in quiescence search
|
||||
PH_QCHECKS, // Non-capture checks in quiescence search
|
||||
PH_STOP
|
||||
enum Stages {
|
||||
MAIN_SEARCH, CAPTURES_S1, KILLERS_S1, QUIETS_1_S1, QUIETS_2_S1, BAD_CAPTURES_S1,
|
||||
EVASION, EVASIONS_S2,
|
||||
QSEARCH_0, CAPTURES_S3, QUIET_CHECKS_S3,
|
||||
QSEARCH_1, CAPTURES_S4,
|
||||
PROBCUT, CAPTURES_S5,
|
||||
RECAPTURE, CAPTURES_S6,
|
||||
STOP
|
||||
};
|
||||
|
||||
CACHE_LINE_ALIGNMENT
|
||||
const uint8_t MainSearchPhaseTable[] = { PH_TT_MOVES, PH_GOOD_CAPTURES, PH_KILLERS, PH_NONCAPTURES, PH_BAD_CAPTURES, PH_STOP};
|
||||
const uint8_t EvasionsPhaseTable[] = { PH_TT_MOVES, PH_EVASIONS, PH_STOP};
|
||||
const uint8_t QsearchWithChecksPhaseTable[] = { PH_TT_MOVES, PH_QCAPTURES, PH_QCHECKS, PH_STOP};
|
||||
const uint8_t QsearchWithoutChecksPhaseTable[] = { PH_TT_MOVES, PH_QCAPTURES, PH_STOP};
|
||||
// Our insertion sort, guaranteed to be stable, as is needed
|
||||
void insertion_sort(ExtMove* begin, ExtMove* end)
|
||||
{
|
||||
ExtMove tmp, *p, *q;
|
||||
|
||||
for (p = begin + 1; p < end; ++p)
|
||||
{
|
||||
tmp = *p;
|
||||
for (q = p; q != begin && *(q-1) < tmp; --q)
|
||||
*q = *(q-1);
|
||||
*q = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
// Unary predicate used by std::partition to split positive scores from remaining
|
||||
// ones so to sort separately the two sets, and with the second sort delayed.
|
||||
inline bool has_positive_score(const ExtMove& ms) { return ms.score > 0; }
|
||||
|
||||
// Picks and moves to the front the best move in the range [begin, end),
|
||||
// it is faster than sorting all the moves in advance when moves are few, as
|
||||
// normally are the possible captures.
|
||||
inline ExtMove* pick_best(ExtMove* begin, ExtMove* end)
|
||||
{
|
||||
std::swap(*begin, *std::max_element(begin, end));
|
||||
return begin;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
|
||||
/// Constructor for the MovePicker class. Apart from the position for which
|
||||
/// it is asked to pick legal moves, MovePicker also wants some information
|
||||
/// Constructors of the MovePicker class. As arguments we pass information
|
||||
/// to help it to return the presumably good moves first, to decide which
|
||||
/// moves to return (in the quiescence search, for instance, we only want to
|
||||
/// search captures, promotions and some checks) and about how important good
|
||||
/// move ordering is at the current node.
|
||||
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
|
||||
SearchStack* ss, Value beta) : pos(p), H(h) {
|
||||
int searchTT = ttm;
|
||||
ttMoves[0].move = ttm;
|
||||
badCaptureThreshold = 0;
|
||||
badCaptures = moves + MOVES_MAX;
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
|
||||
Move* cm, Search::Stack* s) : pos(p), history(h), depth(d) {
|
||||
|
||||
pinned = p.pinned_pieces(pos.side_to_move());
|
||||
assert(d > DEPTH_ZERO);
|
||||
|
||||
if (ss && !p.is_check())
|
||||
cur = end = moves;
|
||||
endBadCaptures = moves + MAX_MOVES - 1;
|
||||
countermoves = cm;
|
||||
ss = s;
|
||||
|
||||
if (p.checkers())
|
||||
stage = EVASION;
|
||||
|
||||
else
|
||||
stage = MAIN_SEARCH;
|
||||
|
||||
ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
end += (ttMove != MOVE_NONE);
|
||||
}
|
||||
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
|
||||
Square sq) : pos(p), history(h), cur(moves), end(moves) {
|
||||
|
||||
assert(d <= DEPTH_ZERO);
|
||||
|
||||
if (p.checkers())
|
||||
stage = EVASION;
|
||||
|
||||
else if (d > DEPTH_QS_NO_CHECKS)
|
||||
stage = QSEARCH_0;
|
||||
|
||||
else if (d > DEPTH_QS_RECAPTURES)
|
||||
{
|
||||
ttMoves[1].move = (ss->mateKiller == ttm) ? MOVE_NONE : ss->mateKiller;
|
||||
searchTT |= ttMoves[1].move;
|
||||
killers[0].move = ss->killers[0];
|
||||
killers[1].move = ss->killers[1];
|
||||
} else
|
||||
ttMoves[1].move = killers[0].move = killers[1].move = MOVE_NONE;
|
||||
stage = QSEARCH_1;
|
||||
|
||||
if (p.is_check())
|
||||
phasePtr = EvasionsPhaseTable;
|
||||
else if (d > DEPTH_ZERO)
|
||||
{
|
||||
// Consider sligtly negative captures as good if at low
|
||||
// depth and far from beta.
|
||||
if (ss && ss->eval < beta - PawnValueMidgame && d < 3 * ONE_PLY)
|
||||
badCaptureThreshold = -PawnValueMidgame;
|
||||
|
||||
phasePtr = MainSearchPhaseTable;
|
||||
// Skip TT move if is not a capture or a promotion, this avoids qsearch
|
||||
// tree explosion due to a possible perpetual check or similar rare cases
|
||||
// when TT table is full.
|
||||
if (ttm && !pos.is_capture_or_promotion(ttm))
|
||||
ttm = MOVE_NONE;
|
||||
}
|
||||
else if (d >= DEPTH_QS_CHECKS)
|
||||
phasePtr = QsearchWithChecksPhaseTable;
|
||||
else
|
||||
{
|
||||
phasePtr = QsearchWithoutChecksPhaseTable;
|
||||
|
||||
// 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 != MOVE_NONE && !pos.move_is_capture_or_promotion(ttm))
|
||||
searchTT = ttMoves[0].move = MOVE_NONE;
|
||||
stage = RECAPTURE;
|
||||
recaptureSquare = sq;
|
||||
ttm = MOVE_NONE;
|
||||
}
|
||||
|
||||
phasePtr += int(!searchTT) - 1;
|
||||
go_next_phase();
|
||||
ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
end += (ttMove != MOVE_NONE);
|
||||
}
|
||||
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, PieceType pt)
|
||||
: pos(p), history(h), cur(moves), end(moves) {
|
||||
|
||||
assert(!pos.checkers());
|
||||
|
||||
stage = PROBCUT;
|
||||
|
||||
// In ProbCut we generate only captures better than parent's captured piece
|
||||
captureThreshold = PieceValue[MG][pt];
|
||||
ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
|
||||
if (ttMove && (!pos.is_capture(ttMove) || pos.see(ttMove) <= captureThreshold))
|
||||
ttMove = MOVE_NONE;
|
||||
|
||||
end += (ttMove != MOVE_NONE);
|
||||
}
|
||||
|
||||
|
||||
/// MovePicker::go_next_phase() generates, scores and sorts the next bunch
|
||||
/// of moves when there are no more moves to try for the current phase.
|
||||
|
||||
void MovePicker::go_next_phase() {
|
||||
|
||||
curMove = moves;
|
||||
phase = *(++phasePtr);
|
||||
switch (phase) {
|
||||
|
||||
case PH_TT_MOVES:
|
||||
curMove = ttMoves;
|
||||
lastMove = curMove + 2;
|
||||
return;
|
||||
|
||||
case PH_GOOD_CAPTURES:
|
||||
lastMove = generate_captures(pos, moves);
|
||||
score_captures();
|
||||
return;
|
||||
|
||||
case PH_KILLERS:
|
||||
curMove = killers;
|
||||
lastMove = curMove + 2;
|
||||
return;
|
||||
|
||||
case PH_NONCAPTURES:
|
||||
lastMove = generate_noncaptures(pos, moves);
|
||||
score_noncaptures();
|
||||
sort_moves(moves, lastMove, &lastGoodNonCapture);
|
||||
return;
|
||||
|
||||
case PH_BAD_CAPTURES:
|
||||
// Bad captures SEE value is already calculated so just pick
|
||||
// them in order to get SEE move ordering.
|
||||
curMove = badCaptures;
|
||||
lastMove = moves + MOVES_MAX;
|
||||
return;
|
||||
|
||||
case PH_EVASIONS:
|
||||
assert(pos.is_check());
|
||||
lastMove = generate_evasions(pos, moves);
|
||||
score_evasions();
|
||||
return;
|
||||
|
||||
case PH_QCAPTURES:
|
||||
lastMove = generate_captures(pos, moves);
|
||||
score_captures();
|
||||
return;
|
||||
|
||||
case PH_QCHECKS:
|
||||
lastMove = generate_non_capture_checks(pos, moves);
|
||||
return;
|
||||
|
||||
case PH_STOP:
|
||||
lastMove = curMove + 1; // Avoids another go_next_phase() call
|
||||
return;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// MovePicker::score_captures(), MovePicker::score_noncaptures() and
|
||||
/// MovePicker::score_evasions() assign a numerical move ordering score
|
||||
/// to each move in a move list. The moves with highest scores will be
|
||||
/// picked first by get_next_move().
|
||||
|
||||
void MovePicker::score_captures() {
|
||||
/// score() assign a numerical move ordering score to each move in a move list.
|
||||
/// The moves with highest scores will be picked first.
|
||||
template<>
|
||||
void MovePicker::score<CAPTURES>() {
|
||||
// Winning and equal captures in the main search are ordered by MVV/LVA.
|
||||
// Suprisingly, this appears to perform slightly better than SEE based
|
||||
// move ordering. The reason is probably that in a position with a winning
|
||||
@@ -201,157 +159,219 @@ void MovePicker::score_captures() {
|
||||
// some SEE calls in case we get a cutoff (idea from Pablo Vazquez).
|
||||
Move m;
|
||||
|
||||
// Use MVV/LVA ordering
|
||||
for (MoveStack* cur = moves; cur != lastMove; cur++)
|
||||
for (ExtMove* it = moves; it != end; ++it)
|
||||
{
|
||||
m = cur->move;
|
||||
if (move_is_promotion(m))
|
||||
cur->score = QueenValueMidgame;
|
||||
else
|
||||
cur->score = pos.midgame_value_of_piece_on(move_to(m))
|
||||
- pos.type_of_piece_on(move_from(m));
|
||||
m = it->move;
|
||||
it->score = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||
- type_of(pos.piece_moved(m));
|
||||
|
||||
if (type_of(m) == PROMOTION)
|
||||
it->score += PieceValue[MG][promotion_type(m)] - PieceValue[MG][PAWN];
|
||||
|
||||
else if (type_of(m) == ENPASSANT)
|
||||
it->score += PieceValue[MG][PAWN];
|
||||
}
|
||||
}
|
||||
|
||||
void MovePicker::score_noncaptures() {
|
||||
// First score by history, when no history is available then use
|
||||
// piece/square tables values. This seems to be better then a
|
||||
// random choice when we don't have an history for any move.
|
||||
template<>
|
||||
void MovePicker::score<QUIETS>() {
|
||||
|
||||
Move m;
|
||||
Piece piece;
|
||||
Square from, to;
|
||||
|
||||
for (MoveStack* cur = moves; cur != lastMove; cur++)
|
||||
for (ExtMove* it = moves; it != end; ++it)
|
||||
{
|
||||
m = cur->move;
|
||||
from = move_from(m);
|
||||
to = move_to(m);
|
||||
piece = pos.piece_on(from);
|
||||
cur->score = H.value(piece, to) + H.gain(piece, to);
|
||||
m = it->move;
|
||||
it->score = history[pos.piece_moved(m)][to_sq(m)];
|
||||
}
|
||||
}
|
||||
|
||||
void MovePicker::score_evasions() {
|
||||
// Try good captures ordered by MVV/LVA, then non-captures if
|
||||
// destination square is not under attack, ordered by history
|
||||
// value, and at the end bad-captures and non-captures with a
|
||||
// negative SEE. This last group is ordered by the SEE score.
|
||||
template<>
|
||||
void MovePicker::score<EVASIONS>() {
|
||||
// Try good captures ordered by MVV/LVA, then non-captures if destination square
|
||||
// is not under attack, ordered by history value, then bad-captures and quiet
|
||||
// moves with a negative SEE. This last group is ordered by the SEE score.
|
||||
Move m;
|
||||
int seeScore;
|
||||
|
||||
// Skip if we don't have at least two moves to order
|
||||
if (lastMove < moves + 2)
|
||||
return;
|
||||
|
||||
for (MoveStack* cur = moves; cur != lastMove; cur++)
|
||||
for (ExtMove* it = moves; it != end; ++it)
|
||||
{
|
||||
m = cur->move;
|
||||
m = it->move;
|
||||
if ((seeScore = pos.see_sign(m)) < 0)
|
||||
cur->score = seeScore - HistoryMax; // Be sure are at the bottom
|
||||
else if (pos.move_is_capture(m))
|
||||
cur->score = pos.midgame_value_of_piece_on(move_to(m))
|
||||
- pos.type_of_piece_on(move_from(m)) + HistoryMax;
|
||||
it->score = seeScore - HistoryStats::Max; // At the bottom
|
||||
|
||||
else if (pos.is_capture(m))
|
||||
it->score = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||
- type_of(pos.piece_moved(m)) + HistoryStats::Max;
|
||||
else
|
||||
cur->score = H.value(pos.piece_on(move_from(m)), move_to(m));
|
||||
it->score = history[pos.piece_moved(m)][to_sq(m)];
|
||||
}
|
||||
}
|
||||
|
||||
/// MovePicker::get_next_move() is the most important method of the MovePicker
|
||||
/// class. It returns a new legal move every time it is called, until there
|
||||
/// are no more moves left.
|
||||
/// It picks the move with the biggest score from a list of generated moves taking
|
||||
/// care not to return the tt move if has already been searched previously.
|
||||
/// Note that this function is not thread safe so should be lock protected by
|
||||
/// caller when accessed through a shared MovePicker object.
|
||||
|
||||
Move MovePicker::get_next_move() {
|
||||
/// generate_next() generates, scores and sorts the next bunch of moves, when
|
||||
/// there are no more moves to try for the current phase.
|
||||
|
||||
void MovePicker::generate_next() {
|
||||
|
||||
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;
|
||||
|
||||
// Be sure countermoves are different from killers
|
||||
for (int i = 0; i < 2; i++)
|
||||
if (countermoves[i] != cur->move && countermoves[i] != (cur+1)->move)
|
||||
(end++)->move = countermoves[i];
|
||||
|
||||
if (countermoves[1] && countermoves[1] == countermoves[0]) // Due to SMP races
|
||||
killers[3].move = MOVE_NONE;
|
||||
|
||||
return;
|
||||
|
||||
case QUIETS_1_S1:
|
||||
endQuiets = end = generate<QUIETS>(pos, moves);
|
||||
score<QUIETS>();
|
||||
end = std::partition(cur, end, has_positive_score);
|
||||
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;
|
||||
case STOP:
|
||||
end = cur + 1; // Avoid another next_phase() call
|
||||
return;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// next_move() is the most important method of the MovePicker class. It returns
|
||||
/// a new pseudo legal move every time is called, until there are no more moves
|
||||
/// left. It picks the move with the biggest score from a list of generated moves
|
||||
/// taking care not returning the ttMove if has already been searched previously.
|
||||
template<>
|
||||
Move MovePicker::next_move<false>() {
|
||||
|
||||
Move move;
|
||||
|
||||
while (true)
|
||||
{
|
||||
while (curMove != lastMove)
|
||||
{
|
||||
switch (phase) {
|
||||
while (cur == end)
|
||||
generate_next();
|
||||
|
||||
case PH_TT_MOVES:
|
||||
move = (curMove++)->move;
|
||||
if ( move != MOVE_NONE
|
||||
&& move_is_legal(pos, move, pinned))
|
||||
switch (stage) {
|
||||
|
||||
case MAIN_SEARCH: case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT:
|
||||
cur++;
|
||||
return ttMove;
|
||||
|
||||
case CAPTURES_S1:
|
||||
move = pick_best(cur++, end)->move;
|
||||
if (move != ttMove)
|
||||
{
|
||||
if (pos.see_sign(move) >= 0)
|
||||
return move;
|
||||
break;
|
||||
|
||||
case PH_GOOD_CAPTURES:
|
||||
move = pick_best(curMove++, lastMove).move;
|
||||
if ( move != ttMoves[0].move
|
||||
&& move != ttMoves[1].move
|
||||
&& pos.pl_move_is_legal(move, pinned))
|
||||
{
|
||||
// Check for a non negative SEE now
|
||||
int seeValue = pos.see_sign(move);
|
||||
if (seeValue >= badCaptureThreshold)
|
||||
return move;
|
||||
|
||||
// Losing capture, move it to the tail of the array, note
|
||||
// that move has now been already checked for legality.
|
||||
(--badCaptures)->move = move;
|
||||
badCaptures->score = seeValue;
|
||||
}
|
||||
break;
|
||||
|
||||
case PH_KILLERS:
|
||||
move = (curMove++)->move;
|
||||
if ( move != MOVE_NONE
|
||||
&& move_is_legal(pos, move, pinned)
|
||||
&& move != ttMoves[0].move
|
||||
&& move != ttMoves[1].move
|
||||
&& !pos.move_is_capture(move))
|
||||
return move;
|
||||
break;
|
||||
|
||||
case PH_NONCAPTURES:
|
||||
|
||||
// Sort negative scored moves only when we get there
|
||||
if (curMove == lastGoodNonCapture)
|
||||
insertion_sort<MoveStack>(lastGoodNonCapture, lastMove);
|
||||
|
||||
move = (curMove++)->move;
|
||||
if ( move != ttMoves[0].move
|
||||
&& move != ttMoves[1].move
|
||||
&& move != killers[0].move
|
||||
&& move != killers[1].move
|
||||
&& pos.pl_move_is_legal(move, pinned))
|
||||
return move;
|
||||
break;
|
||||
|
||||
case PH_BAD_CAPTURES:
|
||||
move = pick_best(curMove++, lastMove).move;
|
||||
return move;
|
||||
|
||||
case PH_EVASIONS:
|
||||
case PH_QCAPTURES:
|
||||
move = pick_best(curMove++, lastMove).move;
|
||||
if ( move != ttMoves[0].move
|
||||
&& pos.pl_move_is_legal(move, pinned))
|
||||
return move;
|
||||
break;
|
||||
|
||||
case PH_QCHECKS:
|
||||
move = (curMove++)->move;
|
||||
if ( move != ttMoves[0].move
|
||||
&& pos.pl_move_is_legal(move, pinned))
|
||||
return move;
|
||||
break;
|
||||
|
||||
case PH_STOP:
|
||||
return MOVE_NONE;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
// Losing capture, move it to the tail of the array
|
||||
(endBadCaptures--)->move = move;
|
||||
}
|
||||
break;
|
||||
|
||||
case KILLERS_S1:
|
||||
move = (cur++)->move;
|
||||
if ( move != MOVE_NONE
|
||||
&& pos.is_pseudo_legal(move)
|
||||
&& move != ttMove
|
||||
&& !pos.is_capture(move))
|
||||
return move;
|
||||
break;
|
||||
|
||||
case QUIETS_1_S1: case QUIETS_2_S1:
|
||||
move = (cur++)->move;
|
||||
if ( move != ttMove
|
||||
&& move != killers[0].move
|
||||
&& move != killers[1].move
|
||||
&& move != killers[2].move
|
||||
&& move != killers[3].move)
|
||||
return move;
|
||||
break;
|
||||
|
||||
case BAD_CAPTURES_S1:
|
||||
return (cur--)->move;
|
||||
|
||||
case EVASIONS_S2: case CAPTURES_S3: case CAPTURES_S4:
|
||||
move = pick_best(cur++, end)->move;
|
||||
if (move != ttMove)
|
||||
return move;
|
||||
break;
|
||||
|
||||
case CAPTURES_S5:
|
||||
move = pick_best(cur++, end)->move;
|
||||
if (move != ttMove && pos.see(move) > captureThreshold)
|
||||
return move;
|
||||
break;
|
||||
|
||||
case CAPTURES_S6:
|
||||
move = pick_best(cur++, end)->move;
|
||||
if (to_sq(move) == recaptureSquare)
|
||||
return move;
|
||||
break;
|
||||
|
||||
case QUIET_CHECKS_S3:
|
||||
move = (cur++)->move;
|
||||
if (move != ttMove)
|
||||
return move;
|
||||
break;
|
||||
|
||||
case STOP:
|
||||
return MOVE_NONE;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
go_next_phase();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// 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>(); }
|
||||
|
||||
+73
-50
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,71 +17,94 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined MOVEPICK_H_INCLUDED
|
||||
#ifndef MOVEPICK_H_INCLUDED
|
||||
#define MOVEPICK_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
#include <algorithm> // For std::max
|
||||
#include <cstring> // For std::memset
|
||||
|
||||
#include "depth.h"
|
||||
#include "history.h"
|
||||
#include "movegen.h"
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
#include "types.h"
|
||||
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
/// The Stats struct stores moves statistics. According to the template parameter
|
||||
/// the class can store History, Gains and Countermoves. History records how often
|
||||
/// different moves have been successful or unsuccessful during the current search
|
||||
/// and is used for reduction and move ordering decisions. Gains records the move's
|
||||
/// best evaluation gain from one ply to the next and is used for pruning decisions.
|
||||
/// Countermoves store the move that refute a previous one. Entries are stored
|
||||
/// according only to moving piece and destination square, hence two moves with
|
||||
/// different origin but same destination and piece will be considered identical.
|
||||
template<bool Gain, typename T>
|
||||
struct Stats {
|
||||
|
||||
struct SearchStack;
|
||||
static const Value Max = Value(2000);
|
||||
|
||||
/// MovePicker is a class which is used to pick one legal move at a time from
|
||||
/// the current position. It is initialized with a Position object and a few
|
||||
/// moves we have reason to believe are good. The most important method is
|
||||
/// MovePicker::pick_next_move(), which returns a new legal move each time it
|
||||
/// is called, until there are no legal moves left, 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 to be strongest first.
|
||||
const T* operator[](Piece p) const { return table[p]; }
|
||||
void clear() { std::memset(table, 0, sizeof(table)); }
|
||||
|
||||
void update(Piece p, Square to, Move m) {
|
||||
|
||||
if (m == table[p][to].first)
|
||||
return;
|
||||
|
||||
table[p][to].second = table[p][to].first;
|
||||
table[p][to].first = m;
|
||||
}
|
||||
|
||||
void update(Piece p, Square to, Value v) {
|
||||
|
||||
if (Gain)
|
||||
table[p][to] = std::max(v, table[p][to] - 1);
|
||||
|
||||
else if (abs(table[p][to] + v) < Max)
|
||||
table[p][to] += v;
|
||||
}
|
||||
|
||||
private:
|
||||
T table[PIECE_NB][SQUARE_NB];
|
||||
};
|
||||
|
||||
typedef Stats< true, Value> GainsStats;
|
||||
typedef Stats<false, Value> HistoryStats;
|
||||
typedef Stats<false, std::pair<Move, Move> > CountermovesStats;
|
||||
|
||||
|
||||
/// MovePicker class is used to pick one pseudo legal move at a time from the
|
||||
/// current position. The most important method is next_move(), which returns a
|
||||
/// new pseudo legal move each time it is called, until there are no moves left,
|
||||
/// 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
|
||||
/// to get a cut-off first.
|
||||
|
||||
class MovePicker {
|
||||
|
||||
MovePicker& operator=(const MovePicker&); // silence a warning under MSVC
|
||||
MovePicker& operator=(const MovePicker&); // Silence a warning under MSVC
|
||||
|
||||
public:
|
||||
MovePicker(const Position& p, Move ttm, Depth d, const History& h, SearchStack* ss = NULL, Value beta = -VALUE_INFINITE);
|
||||
Move get_next_move();
|
||||
int number_of_evasions() const;
|
||||
MovePicker(const Position&, Move, Depth, const HistoryStats&, Square);
|
||||
MovePicker(const Position&, Move, const HistoryStats&, PieceType);
|
||||
MovePicker(const Position&, Move, Depth, const HistoryStats&, Move*, Search::Stack*);
|
||||
|
||||
template<bool SpNode> Move next_move();
|
||||
|
||||
private:
|
||||
void score_captures();
|
||||
void score_noncaptures();
|
||||
void score_evasions();
|
||||
void go_next_phase();
|
||||
template<GenType> void score();
|
||||
void generate_next();
|
||||
|
||||
const Position& pos;
|
||||
const History& H;
|
||||
Bitboard pinned;
|
||||
MoveStack ttMoves[2], killers[2];
|
||||
int badCaptureThreshold, phase;
|
||||
const uint8_t* phasePtr;
|
||||
MoveStack *curMove, *lastMove, *lastGoodNonCapture, *badCaptures;
|
||||
MoveStack moves[MOVES_MAX];
|
||||
const HistoryStats& history;
|
||||
Search::Stack* ss;
|
||||
Move* countermoves;
|
||||
Depth depth;
|
||||
Move ttMove;
|
||||
ExtMove killers[4];
|
||||
Square recaptureSquare;
|
||||
int captureThreshold, stage;
|
||||
ExtMove *cur, *end, *endQuiets, *endBadCaptures;
|
||||
ExtMove moves[MAX_MOVES];
|
||||
};
|
||||
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
/// MovePicker::number_of_evasions() simply returns the number of moves in
|
||||
/// evasions phase. It is intended to be used in positions where the side to
|
||||
/// move is in check, for detecting checkmates or situations where there is
|
||||
/// only a single reply to check.
|
||||
/// WARNING: It works as long as PH_EVASIONS is the _only_ phase for evasions.
|
||||
|
||||
inline int MovePicker::number_of_evasions() const {
|
||||
return int(lastMove - moves);
|
||||
}
|
||||
|
||||
#endif // !defined(MOVEPICK_H_INCLUDED)
|
||||
#endif // #ifndef MOVEPICK_H_INCLUDED
|
||||
|
||||
@@ -0,0 +1,263 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2013 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 s;
|
||||
|
||||
if (abs(v) < VALUE_MATE_IN_MAX_PLY)
|
||||
s << "cp " << v * 100 / int(PawnValueMg);
|
||||
else
|
||||
s << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
|
||||
|
||||
s << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
|
||||
|
||||
return s.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 castle moves are always coded 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) == CASTLE && !chess960)
|
||||
to = (to > from ? FILE_G : FILE_C) | rank_of(from);
|
||||
|
||||
string move = square_to_string(from) + square_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) == CASTLE)
|
||||
san = to > from ? "O-O" : "O-O-O";
|
||||
else
|
||||
{
|
||||
if (pt != PAWN)
|
||||
{
|
||||
san = PieceToChar[WHITE][pt]; // Upper case
|
||||
|
||||
// Disambiguation 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)
|
||||
{
|
||||
Move move = make_move(pop_lsb(&b), to);
|
||||
if (!pos.pl_move_is_legal(move, pos.pinned_pieces()))
|
||||
others ^= from_sq(move);
|
||||
}
|
||||
|
||||
if (others)
|
||||
{
|
||||
if (!(others & file_bb(from)))
|
||||
san += file_to_char(file_of(from));
|
||||
|
||||
else if (!(others & rank_bb(from)))
|
||||
san += rank_to_char(rank_of(from));
|
||||
|
||||
else
|
||||
san += square_to_string(from);
|
||||
}
|
||||
}
|
||||
else if (pos.is_capture(m))
|
||||
san = file_to_char(file_of(from));
|
||||
|
||||
if (pos.is_capture(m))
|
||||
san += 'x';
|
||||
|
||||
san += square_to_string(to);
|
||||
|
||||
if (type_of(m) == PROMOTION)
|
||||
san += string("=") + PieceToChar[WHITE][promotion_type(m)];
|
||||
}
|
||||
|
||||
if (pos.move_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 time and score respectively.
|
||||
|
||||
static string time_to_string(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 s;
|
||||
|
||||
if (hours)
|
||||
s << hours << ':';
|
||||
|
||||
s << setfill('0') << setw(2) << minutes << ':' << setw(2) << seconds;
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
static string score_to_string(Value v) {
|
||||
|
||||
stringstream s;
|
||||
|
||||
if (v >= VALUE_MATE_IN_MAX_PLY)
|
||||
s << "#" << (VALUE_MATE - v + 1) / 2;
|
||||
|
||||
else if (v <= VALUE_MATED_IN_MAX_PLY)
|
||||
s << "-#" << (VALUE_MATE + v) / 2;
|
||||
|
||||
else
|
||||
s << setprecision(2) << fixed << showpos << float(v) / PawnValueMg;
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
string pretty_pv(Position& pos, int depth, Value value, int64_t msecs, Move pv[]) {
|
||||
|
||||
const int64_t K = 1000;
|
||||
const int64_t M = 1000000;
|
||||
|
||||
std::stack<StateInfo> st;
|
||||
Move* m = pv;
|
||||
string san, padding;
|
||||
size_t length;
|
||||
stringstream s;
|
||||
|
||||
s << setw(2) << depth
|
||||
<< setw(8) << score_to_string(value)
|
||||
<< setw(8) << time_to_string(msecs);
|
||||
|
||||
if (pos.nodes_searched() < M)
|
||||
s << setw(8) << pos.nodes_searched() / 1 << " ";
|
||||
|
||||
else if (pos.nodes_searched() < K * M)
|
||||
s << setw(7) << pos.nodes_searched() / K << "K ";
|
||||
|
||||
else
|
||||
s << setw(7) << pos.nodes_searched() / M << "M ";
|
||||
|
||||
padding = string(s.str().length(), ' ');
|
||||
length = padding.length();
|
||||
|
||||
while (*m != MOVE_NONE)
|
||||
{
|
||||
san = move_to_san(pos, *m);
|
||||
|
||||
if (length + san.length() > 80)
|
||||
{
|
||||
s << "\n" + padding;
|
||||
length = padding.length();
|
||||
}
|
||||
|
||||
s << san << ' ';
|
||||
length += san.length() + 1;
|
||||
|
||||
st.push(StateInfo());
|
||||
pos.do_move(*m++, st.top());
|
||||
}
|
||||
|
||||
while (m != pv)
|
||||
pos.undo_move(*--m);
|
||||
|
||||
return s.str();
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,40 +17,19 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef NOTATION_H_INCLUDED
|
||||
#define NOTATION_H_INCLUDED
|
||||
|
||||
#if !defined(COLOR_H_INCLUDED)
|
||||
#define COLOR_H_INCLUDED
|
||||
#include <string>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
class Position;
|
||||
|
||||
enum Color {
|
||||
WHITE,
|
||||
BLACK,
|
||||
COLOR_NONE
|
||||
};
|
||||
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[]);
|
||||
|
||||
enum SquareColor {
|
||||
DARK,
|
||||
LIGHT
|
||||
};
|
||||
|
||||
ENABLE_OPERATORS_ON(Color);
|
||||
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
inline Color opposite_color(Color c) {
|
||||
return Color(int(c) ^ 1);
|
||||
}
|
||||
|
||||
inline bool color_is_ok(Color c) {
|
||||
return c == WHITE || c == BLACK;
|
||||
}
|
||||
|
||||
#endif // !defined(COLOR_H_INCLUDED)
|
||||
#endif // #ifndef NOTATION_H_INCLUDED
|
||||
+207
-164
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,222 +17,265 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "bitcount.h"
|
||||
#include "pawns.h"
|
||||
#include "position.h"
|
||||
|
||||
|
||||
////
|
||||
//// Local definitions
|
||||
////
|
||||
|
||||
namespace {
|
||||
|
||||
/// Constants and variables
|
||||
|
||||
#define V Value
|
||||
#define S(mg, eg) make_score(mg, eg)
|
||||
|
||||
// Doubled pawn penalty by opposed flag and file
|
||||
const Score DoubledPawnPenalty[2][8] = {
|
||||
const Score Doubled[2][FILE_NB] = {
|
||||
{ S(13, 43), S(20, 48), S(23, 48), S(23, 48),
|
||||
S(23, 48), S(23, 48), S(20, 48), S(13, 43) },
|
||||
{ S(13, 43), S(20, 48), S(23, 48), S(23, 48),
|
||||
S(23, 48), S(23, 48), S(20, 48), S(13, 43) }};
|
||||
|
||||
// Isolated pawn penalty by opposed flag and file
|
||||
const Score IsolatedPawnPenalty[2][8] = {
|
||||
const Score Isolated[2][FILE_NB] = {
|
||||
{ S(37, 45), S(54, 52), S(60, 52), S(60, 52),
|
||||
S(60, 52), S(60, 52), S(54, 52), S(37, 45) },
|
||||
{ S(25, 30), S(36, 35), S(40, 35), S(40, 35),
|
||||
S(40, 35), S(40, 35), S(36, 35), S(25, 30) }};
|
||||
|
||||
// Backward pawn penalty by opposed flag and file
|
||||
const Score BackwardPawnPenalty[2][8] = {
|
||||
const Score Backward[2][FILE_NB] = {
|
||||
{ S(30, 42), S(43, 46), S(49, 46), S(49, 46),
|
||||
S(49, 46), S(49, 46), S(43, 46), S(30, 42) },
|
||||
{ S(20, 28), S(29, 31), S(33, 31), S(33, 31),
|
||||
S(33, 31), S(33, 31), S(29, 31), S(20, 28) }};
|
||||
|
||||
// Pawn chain membership bonus by file
|
||||
const Score ChainBonus[8] = {
|
||||
const Score ChainMember[FILE_NB] = {
|
||||
S(11,-1), S(13,-1), S(13,-1), S(14,-1),
|
||||
S(14,-1), S(13,-1), S(13,-1), S(11,-1)
|
||||
};
|
||||
|
||||
// Candidate passed pawn bonus by rank
|
||||
const Score CandidateBonus[8] = {
|
||||
const Score CandidatePassed[RANK_NB] = {
|
||||
S( 0, 0), S( 6, 13), S(6,13), S(14,29),
|
||||
S(34,68), S(83,166), S(0, 0), S( 0, 0)
|
||||
};
|
||||
|
||||
// Weakness of our pawn shelter in front of the king indexed by [rank]
|
||||
const Value ShelterWeakness[RANK_NB] =
|
||||
{ V(100), V(0), V(27), V(73), V(92), V(101), V(101) };
|
||||
|
||||
// Danger of enemy pawns moving toward our king indexed by
|
||||
// [no friendly pawn | pawn unblocked | pawn blocked][rank of enemy pawn]
|
||||
const Value StormDanger[3][RANK_NB] = {
|
||||
{ V( 0), V(64), V(128), V(51), V(26) },
|
||||
{ V(26), V(32), V( 96), V(38), V(20) },
|
||||
{ V( 0), V( 0), V( 64), V(25), V(13) }};
|
||||
|
||||
// Max bonus for king safety. Corresponds to start position with all the pawns
|
||||
// in front of the king and no enemy pawn on the horizont.
|
||||
const Value MaxSafetyBonus = V(263);
|
||||
|
||||
#undef S
|
||||
}
|
||||
#undef V
|
||||
|
||||
template<Color Us>
|
||||
Score evaluate(const Position& pos, Pawns::Entry* e) {
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
const Square Up = (Us == WHITE ? DELTA_N : DELTA_S);
|
||||
const Square Right = (Us == WHITE ? DELTA_NE : DELTA_SW);
|
||||
const Square Left = (Us == WHITE ? DELTA_NW : DELTA_SE);
|
||||
|
||||
/// PawnInfoTable c'tor and d'tor instantiated one each thread
|
||||
Bitboard b;
|
||||
Square s;
|
||||
File f;
|
||||
Rank r;
|
||||
bool passed, isolated, doubled, opposed, chain, backward, candidate;
|
||||
Score value = SCORE_ZERO;
|
||||
const Square* pl = pos.list<PAWN>(Us);
|
||||
|
||||
PawnInfoTable::PawnInfoTable() {
|
||||
Bitboard ourPawns = pos.pieces(Us, PAWN);
|
||||
Bitboard theirPawns = pos.pieces(Them, PAWN);
|
||||
|
||||
entries = new PawnInfo[PawnTableSize];
|
||||
e->passedPawns[Us] = 0;
|
||||
e->kingSquares[Us] = SQ_NONE;
|
||||
e->semiopenFiles[Us] = 0xFF;
|
||||
e->pawnAttacks[Us] = shift_bb<Right>(ourPawns) | shift_bb<Left>(ourPawns);
|
||||
e->pawnsOnSquares[Us][BLACK] = popcount<Max15>(ourPawns & DarkSquares);
|
||||
e->pawnsOnSquares[Us][WHITE] = pos.count<PAWN>(Us) - e->pawnsOnSquares[Us][BLACK];
|
||||
|
||||
if (!entries)
|
||||
{
|
||||
std::cerr << "Failed to allocate " << (PawnTableSize * sizeof(PawnInfo))
|
||||
<< " bytes for pawn hash table." << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
// Loop through all pawns of the current color and score each pawn
|
||||
while ((s = *pl++) != SQ_NONE)
|
||||
{
|
||||
assert(pos.piece_on(s) == make_piece(Us, PAWN));
|
||||
|
||||
f = file_of(s);
|
||||
r = rank_of(s);
|
||||
|
||||
// This file cannot be semi-open
|
||||
e->semiopenFiles[Us] &= ~(1 << f);
|
||||
|
||||
// Our rank plus previous one. Used for chain detection
|
||||
b = rank_bb(r) | rank_bb(Us == WHITE ? r - Rank(1) : r + Rank(1));
|
||||
|
||||
// Flag the pawn as passed, isolated, doubled or member of a pawn
|
||||
// chain (but not the backward one).
|
||||
chain = ourPawns & adjacent_files_bb(f) & b;
|
||||
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
|
||||
backward = false;
|
||||
|
||||
// If the pawn is passed, isolated, or member of a pawn chain it cannot
|
||||
// be backward. If there are friendly pawns behind on adjacent files
|
||||
// or if can capture an enemy pawn it cannot be backward either.
|
||||
if ( !(passed | isolated | chain)
|
||||
&& !(ourPawns & pawn_attack_span(Them, s))
|
||||
&& !(pos.attacks_from<PAWN>(s, Us) & theirPawns))
|
||||
{
|
||||
// We now know that there are no friendly pawns beside or behind this
|
||||
// pawn on adjacent files. We now check whether the pawn is
|
||||
// backward by looking in the forward direction on the adjacent
|
||||
// files, and seeing whether we meet a friendly or an enemy pawn first.
|
||||
b = pos.attacks_from<PAWN>(s, Us);
|
||||
|
||||
// Note that we are sure to find something because pawn is not passed
|
||||
// nor isolated, so loop is potentially infinite, but it isn't.
|
||||
while (!(b & (ourPawns | theirPawns)))
|
||||
b = shift_bb<Up>(b);
|
||||
|
||||
// The friendly pawn needs to be at least two ranks closer than the
|
||||
// enemy pawn in order to help the potentially backward pawn advance.
|
||||
backward = (b | shift_bb<Up>(b)) & theirPawns;
|
||||
}
|
||||
|
||||
assert(opposed | passed | (pawn_attack_span(Us, s) & theirPawns));
|
||||
|
||||
// A not passed pawn is a candidate to become passed if it is free to
|
||||
// advance and if the number of friendly pawns beside or behind this
|
||||
// pawn on adjacent files is higher or equal than the number of
|
||||
// enemy pawns in the forward direction on the adjacent files.
|
||||
candidate = !(opposed | passed | backward | isolated)
|
||||
&& (b = pawn_attack_span(Them, s + pawn_push(Us)) & ourPawns) != 0
|
||||
&& popcount<Max15>(b) >= popcount<Max15>(pawn_attack_span(Us, s) & theirPawns);
|
||||
|
||||
// Passed pawns will be properly scored in evaluation because we need
|
||||
// full attack info to evaluate passed pawns. Only the frontmost passed
|
||||
// pawn on each file is considered a true passed pawn.
|
||||
if (passed && !doubled)
|
||||
e->passedPawns[Us] |= s;
|
||||
|
||||
// Score this pawn
|
||||
if (isolated)
|
||||
value -= Isolated[opposed][f];
|
||||
|
||||
if (doubled)
|
||||
value -= Doubled[opposed][f];
|
||||
|
||||
if (backward)
|
||||
value -= Backward[opposed][f];
|
||||
|
||||
if (chain)
|
||||
value += ChainMember[f];
|
||||
|
||||
if (candidate)
|
||||
value += CandidatePassed[relative_rank(Us, s)];
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
memset(entries, 0, PawnTableSize * sizeof(PawnInfo));
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace Pawns {
|
||||
|
||||
/// probe() takes a position object as input, computes a Entry object, and returns
|
||||
/// a pointer to it. The result is also stored in a hash table, so we don't have
|
||||
/// to recompute everything when the same pawn structure occurs again.
|
||||
|
||||
Entry* probe(const Position& pos, Table& entries) {
|
||||
|
||||
Key key = pos.pawn_key();
|
||||
Entry* e = entries[key];
|
||||
|
||||
if (e->key == key)
|
||||
return e;
|
||||
|
||||
e->key = key;
|
||||
e->value = evaluate<WHITE>(pos, e) - evaluate<BLACK>(pos, e);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
PawnInfoTable::~PawnInfoTable() {
|
||||
|
||||
delete [] entries;
|
||||
}
|
||||
|
||||
|
||||
/// PawnInfoTable::get_pawn_info() takes a position object as input, computes
|
||||
/// a PawnInfo object, and returns a pointer to it. The result is also stored
|
||||
/// in a hash table, so we don't have to recompute everything when the same
|
||||
/// pawn structure occurs again.
|
||||
|
||||
PawnInfo* PawnInfoTable::get_pawn_info(const Position& pos) const {
|
||||
|
||||
assert(pos.is_ok());
|
||||
|
||||
Key key = pos.get_pawn_key();
|
||||
unsigned index = unsigned(key & (PawnTableSize - 1));
|
||||
PawnInfo* pi = entries + index;
|
||||
|
||||
// If pi->key matches the position's pawn hash key, it means that we
|
||||
// have analysed this pawn structure before, and we can simply return
|
||||
// the information we found the last time instead of recomputing it.
|
||||
if (pi->key == key)
|
||||
return pi;
|
||||
|
||||
// Clear the PawnInfo object, and set the key
|
||||
memset(pi, 0, sizeof(PawnInfo));
|
||||
pi->halfOpenFiles[WHITE] = pi->halfOpenFiles[BLACK] = 0xFF;
|
||||
pi->kingSquares[WHITE] = pi->kingSquares[BLACK] = SQ_NONE;
|
||||
pi->key = key;
|
||||
|
||||
// Calculate pawn attacks
|
||||
Bitboard wPawns = pos.pieces(PAWN, WHITE);
|
||||
Bitboard bPawns = pos.pieces(PAWN, BLACK);
|
||||
pi->pawnAttacks[WHITE] = ((wPawns << 9) & ~FileABB) | ((wPawns << 7) & ~FileHBB);
|
||||
pi->pawnAttacks[BLACK] = ((bPawns >> 7) & ~FileABB) | ((bPawns >> 9) & ~FileHBB);
|
||||
|
||||
// Evaluate pawns for both colors
|
||||
pi->value = evaluate_pawns<WHITE>(pos, wPawns, bPawns, pi)
|
||||
- evaluate_pawns<BLACK>(pos, bPawns, wPawns, pi);
|
||||
return pi;
|
||||
}
|
||||
|
||||
|
||||
/// PawnInfoTable::evaluate_pawns() evaluates each pawn of the given color
|
||||
/// Entry::shelter_storm() calculates shelter and storm penalties for the file
|
||||
/// the king is on, as well as the two adjacent files.
|
||||
|
||||
template<Color Us>
|
||||
Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
|
||||
Bitboard theirPawns, PawnInfo* pi) const {
|
||||
Bitboard b;
|
||||
Square s;
|
||||
File f;
|
||||
Rank r;
|
||||
bool passed, isolated, doubled, opposed, chain, backward, candidate;
|
||||
Score value = SCORE_ZERO;
|
||||
const BitCountType Max15 = CpuIs64Bit ? CNT64_MAX15 : CNT32_MAX15;
|
||||
const Square* ptr = pos.piece_list_begin(Us, PAWN);
|
||||
Value Entry::shelter_storm(const Position& pos, Square ksq) {
|
||||
|
||||
// Loop through all pawns of the current color and score each pawn
|
||||
while ((s = *ptr++) != SQ_NONE)
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
Value safety = MaxSafetyBonus;
|
||||
Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq));
|
||||
Bitboard ourPawns = b & pos.pieces(Us);
|
||||
Bitboard theirPawns = b & pos.pieces(Them);
|
||||
Rank rkUs, rkThem;
|
||||
File kf = file_of(ksq);
|
||||
|
||||
kf = (kf == FILE_A) ? FILE_B : (kf == FILE_H) ? FILE_G : kf;
|
||||
|
||||
for (int f = kf - 1; f <= kf + 1; f++)
|
||||
{
|
||||
assert(pos.piece_on(s) == piece_of_color_and_type(Us, PAWN));
|
||||
b = ourPawns & FileBB[f];
|
||||
rkUs = b ? relative_rank(Us, Us == WHITE ? lsb(b) : msb(b)) : RANK_1;
|
||||
safety -= ShelterWeakness[rkUs];
|
||||
|
||||
f = square_file(s);
|
||||
r = square_rank(s);
|
||||
|
||||
// This file cannot be half open
|
||||
pi->halfOpenFiles[Us] &= ~(1 << f);
|
||||
|
||||
// Our rank plus previous one. Used for chain detection.
|
||||
b = rank_bb(r) | rank_bb(Us == WHITE ? r - Rank(1) : r + Rank(1));
|
||||
|
||||
// Passed, isolated, doubled or member of a pawn
|
||||
// chain (but not the backward one) ?
|
||||
passed = !(theirPawns & passed_pawn_mask(Us, s));
|
||||
doubled = ourPawns & squares_in_front_of(Us, s);
|
||||
opposed = theirPawns & squares_in_front_of(Us, s);
|
||||
isolated = !(ourPawns & neighboring_files_bb(f));
|
||||
chain = ourPawns & neighboring_files_bb(f) & b;
|
||||
|
||||
// Test for backward pawn
|
||||
//
|
||||
backward = false;
|
||||
|
||||
// If the pawn is passed, isolated, or member of a pawn chain
|
||||
// it cannot be backward. If can capture an enemy pawn or if
|
||||
// there are friendly pawns behind on neighboring files it cannot
|
||||
// be backward either.
|
||||
if ( !(passed | isolated | chain)
|
||||
&& !(ourPawns & attack_span_mask(opposite_color(Us), s))
|
||||
&& !(pos.attacks_from<PAWN>(s, Us) & theirPawns))
|
||||
{
|
||||
// We now know that there are no friendly pawns beside or behind this
|
||||
// pawn on neighboring files. We now check whether the pawn is
|
||||
// backward by looking in the forward direction on the neighboring
|
||||
// files, and seeing whether we meet a friendly or an enemy pawn first.
|
||||
b = pos.attacks_from<PAWN>(s, Us);
|
||||
|
||||
// Note that we are sure to find something because pawn is not passed
|
||||
// nor isolated, so loop is potentially infinite, but it isn't.
|
||||
while (!(b & (ourPawns | theirPawns)))
|
||||
Us == WHITE ? b <<= 8 : b >>= 8;
|
||||
|
||||
// The friendly pawn needs to be at least two ranks closer than the enemy
|
||||
// pawn in order to help the potentially backward pawn advance.
|
||||
backward = (b | (Us == WHITE ? b << 8 : b >> 8)) & theirPawns;
|
||||
}
|
||||
|
||||
assert(passed | opposed | (attack_span_mask(Us, s) & theirPawns));
|
||||
|
||||
// Test for candidate passed pawn
|
||||
candidate = !(opposed | passed)
|
||||
&& (b = attack_span_mask(opposite_color(Us), s + pawn_push(Us)) & ourPawns) != EmptyBoardBB
|
||||
&& count_1s<Max15>(b) >= count_1s<Max15>(attack_span_mask(Us, s) & theirPawns);
|
||||
|
||||
// Mark the pawn as passed. Pawn will be properly scored in evaluation
|
||||
// because we need full attack info to evaluate passed pawns. Only the
|
||||
// frontmost passed pawn on each file is considered a true passed pawn.
|
||||
if (passed && !doubled)
|
||||
set_bit(&(pi->passedPawns[Us]), s);
|
||||
|
||||
// Score this pawn
|
||||
if (isolated)
|
||||
value -= IsolatedPawnPenalty[opposed][f];
|
||||
|
||||
if (doubled)
|
||||
value -= DoubledPawnPenalty[opposed][f];
|
||||
|
||||
if (backward)
|
||||
value -= BackwardPawnPenalty[opposed][f];
|
||||
|
||||
if (chain)
|
||||
value += ChainBonus[f];
|
||||
|
||||
if (candidate)
|
||||
value += CandidateBonus[relative_rank(Us, s)];
|
||||
b = theirPawns & FileBB[f];
|
||||
rkThem = b ? relative_rank(Us, Us == WHITE ? lsb(b) : msb(b)) : RANK_1;
|
||||
safety -= StormDanger[rkUs == RANK_1 ? 0 : rkThem == rkUs + 1 ? 2 : 1][rkThem];
|
||||
}
|
||||
return value;
|
||||
|
||||
return safety;
|
||||
}
|
||||
|
||||
|
||||
/// Entry::update_safety() calculates and caches a bonus for king safety. It is
|
||||
/// called only when king square changes, about 20% of total king_safety() calls.
|
||||
|
||||
template<Color Us>
|
||||
Score Entry::update_safety(const Position& pos, Square ksq) {
|
||||
|
||||
kingSquares[Us] = ksq;
|
||||
castleRights[Us] = pos.can_castle(Us);
|
||||
minKPdistance[Us] = 0;
|
||||
|
||||
Bitboard pawns = pos.pieces(Us, PAWN);
|
||||
if (pawns)
|
||||
while (!(DistanceRingsBB[ksq][minKPdistance[Us]++] & pawns)) {}
|
||||
|
||||
if (relative_rank(Us, ksq) > RANK_4)
|
||||
return kingSafety[Us] = make_score(0, -16 * minKPdistance[Us]);
|
||||
|
||||
Value bonus = shelter_storm<Us>(pos, ksq);
|
||||
|
||||
// If we can castle use the bonus after the castle if is bigger
|
||||
if (pos.can_castle(make_castle_right(Us, KING_SIDE)))
|
||||
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_G1)));
|
||||
|
||||
if (pos.can_castle(make_castle_right(Us, QUEEN_SIDE)))
|
||||
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));
|
||||
|
||||
return kingSafety[Us] = make_score(bonus, -16 * minKPdistance[Us]);
|
||||
}
|
||||
|
||||
// Explicit template instantiation
|
||||
template Score Entry::update_safety<WHITE>(const Position& pos, Square ksq);
|
||||
template Score Entry::update_safety<BLACK>(const Position& pos, Square ksq);
|
||||
|
||||
} // namespace Pawns
|
||||
|
||||
+39
-124
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,147 +17,62 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(PAWNS_H_INCLUDED)
|
||||
#ifndef PAWNS_H_INCLUDED
|
||||
#define PAWNS_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "value.h"
|
||||
#include "types.h"
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
namespace Pawns {
|
||||
|
||||
const int PawnTableSize = 16384;
|
||||
/// Pawns::Entry contains various information about a pawn structure. Currently,
|
||||
/// it only includes a middle game and end game pawn structure evaluation, and a
|
||||
/// bitboard of passed pawns. We may want to add further information in the future.
|
||||
/// A lookup to the pawn hash table (performed by calling the probe function)
|
||||
/// returns a pointer to an Entry object.
|
||||
|
||||
/// PawnInfo is a class which contains various information about a pawn
|
||||
/// structure. Currently, it only includes a middle game and an end game
|
||||
/// pawn structure evaluation, and a bitboard of passed pawns. We may want
|
||||
/// to add further information in the future. A lookup to the pawn hash table
|
||||
/// (performed by calling the get_pawn_info method in a PawnInfoTable object)
|
||||
/// returns a pointer to a PawnInfo object.
|
||||
class PawnInfo {
|
||||
struct Entry {
|
||||
|
||||
friend class PawnInfoTable;
|
||||
Score pawns_value() const { return value; }
|
||||
Bitboard pawn_attacks(Color c) const { return pawnAttacks[c]; }
|
||||
Bitboard passed_pawns(Color c) const { return passedPawns[c]; }
|
||||
int pawns_on_same_color_squares(Color c, Square s) const { return pawnsOnSquares[c][!!(DarkSquares & s)]; }
|
||||
int semiopen(Color c, File f) const { return semiopenFiles[c] & (1 << int(f)); }
|
||||
int semiopen_on_side(Color c, File f, bool left) const {
|
||||
|
||||
public:
|
||||
Score pawns_value() const;
|
||||
Bitboard pawn_attacks(Color c) const;
|
||||
Bitboard passed_pawns(Color c) const;
|
||||
int file_is_half_open(Color c, File f) const;
|
||||
int has_open_file_to_left(Color c, File f) const;
|
||||
int has_open_file_to_right(Color c, File f) const;
|
||||
return semiopenFiles[c] & (left ? ((1 << int(f)) - 1) : ~((1 << int(f+1)) - 1));
|
||||
}
|
||||
|
||||
template<Color Us>
|
||||
Score king_shelter(const Position& pos, Square ksq);
|
||||
Score king_safety(const Position& pos, Square ksq) {
|
||||
|
||||
return kingSquares[Us] == ksq && castleRights[Us] == pos.can_castle(Us)
|
||||
? kingSafety[Us] : update_safety<Us>(pos, ksq);
|
||||
}
|
||||
|
||||
private:
|
||||
template<Color Us>
|
||||
Score updateShelter(const Position& pos, Square ksq);
|
||||
Score update_safety(const Position& pos, Square ksq);
|
||||
|
||||
template<Color Us>
|
||||
Value shelter_storm(const Position& pos, Square ksq);
|
||||
|
||||
Key key;
|
||||
Bitboard passedPawns[2];
|
||||
Bitboard pawnAttacks[2];
|
||||
Square kingSquares[2];
|
||||
Bitboard passedPawns[COLOR_NB];
|
||||
Bitboard pawnAttacks[COLOR_NB];
|
||||
Square kingSquares[COLOR_NB];
|
||||
int minKPdistance[COLOR_NB];
|
||||
int castleRights[COLOR_NB];
|
||||
Score value;
|
||||
int halfOpenFiles[2];
|
||||
Score kingShelters[2];
|
||||
int semiopenFiles[COLOR_NB];
|
||||
Score kingSafety[COLOR_NB];
|
||||
int pawnsOnSquares[COLOR_NB][COLOR_NB];
|
||||
};
|
||||
|
||||
/// The PawnInfoTable class represents a pawn hash table. It is basically
|
||||
/// just an array of PawnInfo objects and a few methods for accessing these
|
||||
/// objects. The most important method is get_pawn_info, which looks up a
|
||||
/// position in the table and returns a pointer to a PawnInfo object.
|
||||
typedef HashTable<Entry, 16384> Table;
|
||||
|
||||
class PawnInfoTable {
|
||||
Entry* probe(const Position& pos, Table& entries);
|
||||
|
||||
enum SideType { KingSide, QueenSide };
|
||||
|
||||
PawnInfoTable(const PawnInfoTable&);
|
||||
PawnInfoTable& operator=(const PawnInfoTable&);
|
||||
|
||||
public:
|
||||
PawnInfoTable();
|
||||
~PawnInfoTable();
|
||||
PawnInfo* get_pawn_info(const Position& pos) const;
|
||||
void prefetch(Key key) const;
|
||||
|
||||
private:
|
||||
template<Color Us>
|
||||
Score evaluate_pawns(const Position& pos, Bitboard ourPawns, Bitboard theirPawns, PawnInfo* pi) const;
|
||||
|
||||
PawnInfo* entries;
|
||||
};
|
||||
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
inline void PawnInfoTable::prefetch(Key key) const {
|
||||
|
||||
unsigned index = unsigned(key & (PawnTableSize - 1));
|
||||
PawnInfo* pi = entries + index;
|
||||
::prefetch((char*) pi);
|
||||
}
|
||||
|
||||
inline Score PawnInfo::pawns_value() const {
|
||||
return value;
|
||||
}
|
||||
|
||||
inline Bitboard PawnInfo::pawn_attacks(Color c) const {
|
||||
return pawnAttacks[c];
|
||||
}
|
||||
|
||||
inline Bitboard PawnInfo::passed_pawns(Color c) const {
|
||||
return passedPawns[c];
|
||||
}
|
||||
|
||||
inline int PawnInfo::file_is_half_open(Color c, File f) const {
|
||||
return (halfOpenFiles[c] & (1 << int(f)));
|
||||
}
|
||||
|
||||
inline int PawnInfo::has_open_file_to_left(Color c, File f) const {
|
||||
return halfOpenFiles[c] & ((1 << int(f)) - 1);
|
||||
}
|
||||
|
||||
inline int PawnInfo::has_open_file_to_right(Color c, File f) const {
|
||||
return halfOpenFiles[c] & ~((1 << int(f+1)) - 1);
|
||||
}
|
||||
|
||||
/// PawnInfo::updateShelter() calculates and caches king shelter. It is called
|
||||
/// only when king square changes, about 20% of total king_shelter() calls.
|
||||
template<Color Us>
|
||||
Score PawnInfo::updateShelter(const Position& pos, Square ksq) {
|
||||
|
||||
const int Shift = (Us == WHITE ? 8 : -8);
|
||||
|
||||
Bitboard pawns;
|
||||
int r, shelter = 0;
|
||||
|
||||
if (relative_rank(Us, ksq) <= RANK_4)
|
||||
{
|
||||
pawns = pos.pieces(PAWN, Us) & this_and_neighboring_files_bb(ksq);
|
||||
r = square_rank(ksq) * 8;
|
||||
for (int i = 1; i < 4; i++)
|
||||
{
|
||||
r += Shift;
|
||||
shelter += BitCount8Bit[(pawns >> r) & 0xFF] * (128 >> i);
|
||||
}
|
||||
}
|
||||
kingSquares[Us] = ksq;
|
||||
kingShelters[Us] = make_score(shelter, 0);
|
||||
return kingShelters[Us];
|
||||
}
|
||||
|
||||
template<Color Us>
|
||||
inline Score PawnInfo::king_shelter(const Position& pos, Square ksq) {
|
||||
return kingSquares[Us] == ksq ? kingShelters[Us] : updateShelter<Us>(pos, ksq);
|
||||
}
|
||||
|
||||
#endif // !defined(PAWNS_H_INCLUDED)
|
||||
#endif // #ifndef PAWNS_H_INCLUDED
|
||||
|
||||
-111
@@ -1,111 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(PIECE_H_INCLUDED)
|
||||
#define PIECE_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "color.h"
|
||||
#include "square.h"
|
||||
#include "value.h"
|
||||
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
enum PieceType {
|
||||
PIECE_TYPE_NONE = 0,
|
||||
PAWN = 1, KNIGHT = 2, BISHOP = 3, ROOK = 4, QUEEN = 5, KING = 6
|
||||
};
|
||||
|
||||
enum Piece {
|
||||
PIECE_NONE_DARK_SQ = 0, WP = 1, WN = 2, WB = 3, WR = 4, WQ = 5, WK = 6,
|
||||
BP = 9, BN = 10, BB = 11, BR = 12, BQ = 13, BK = 14, PIECE_NONE = 16
|
||||
};
|
||||
|
||||
ENABLE_OPERATORS_ON(PieceType);
|
||||
ENABLE_OPERATORS_ON(Piece);
|
||||
|
||||
|
||||
////
|
||||
//// Constants
|
||||
////
|
||||
|
||||
/// Important: If the material values are changed, one must also
|
||||
/// adjust the piece square tables, and the method game_phase() in the
|
||||
/// Position class!
|
||||
///
|
||||
/// Values modified by Joona Kiiski
|
||||
|
||||
const Value PawnValueMidgame = Value(0x0C6);
|
||||
const Value PawnValueEndgame = Value(0x102);
|
||||
const Value KnightValueMidgame = Value(0x331);
|
||||
const Value KnightValueEndgame = Value(0x34E);
|
||||
const Value BishopValueMidgame = Value(0x344);
|
||||
const Value BishopValueEndgame = Value(0x359);
|
||||
const Value RookValueMidgame = Value(0x4F6);
|
||||
const Value RookValueEndgame = Value(0x4FE);
|
||||
const Value QueenValueMidgame = Value(0x9D9);
|
||||
const Value QueenValueEndgame = Value(0x9FE);
|
||||
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
inline PieceType type_of_piece(Piece p) {
|
||||
return PieceType(int(p) & 7);
|
||||
}
|
||||
|
||||
inline Color color_of_piece(Piece p) {
|
||||
return Color(int(p) >> 3);
|
||||
}
|
||||
|
||||
inline Piece piece_of_color_and_type(Color c, PieceType pt) {
|
||||
return Piece((int(c) << 3) | int(pt));
|
||||
}
|
||||
|
||||
inline SquareDelta pawn_push(Color c) {
|
||||
return (c == WHITE ? DELTA_N : DELTA_S);
|
||||
}
|
||||
|
||||
inline bool piece_type_is_ok(PieceType pt) {
|
||||
return pt >= PAWN && pt <= KING;
|
||||
}
|
||||
|
||||
inline bool piece_is_ok(Piece p) {
|
||||
return piece_type_is_ok(type_of_piece(p)) && color_is_ok(color_of_piece(p));
|
||||
}
|
||||
|
||||
inline char piece_type_to_char(PieceType pt) {
|
||||
return std::string(" PNBRQK")[pt];
|
||||
}
|
||||
|
||||
inline PieceType piece_type_from_char(char c) {
|
||||
return PieceType(std::string(" PNBRQK").find(c));
|
||||
}
|
||||
|
||||
#endif // !defined(PIECE_H_INCLUDED)
|
||||
+116
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2013 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 warning from MSVC compiler
|
||||
#pragma warning(disable: 4127) // Conditional expression is constant
|
||||
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
|
||||
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
|
||||
#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,
|
||||
// unfortunatly 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 my 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
|
||||
+841
-1418
File diff suppressed because it is too large
Load Diff
+209
-338
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,85 +17,56 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(POSITION_H_INCLUDED)
|
||||
#ifndef POSITION_H_INCLUDED
|
||||
#define POSITION_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "color.h"
|
||||
#include "move.h"
|
||||
#include "piece.h"
|
||||
#include "square.h"
|
||||
#include "value.h"
|
||||
#include "types.h"
|
||||
|
||||
|
||||
////
|
||||
//// Constants
|
||||
////
|
||||
|
||||
/// Maximum number of plies per game (220 should be enough, because the
|
||||
/// maximum search depth is 100, and during position setup we reset the
|
||||
/// move counter for every non-reversible move).
|
||||
const int MaxGameLength = 220;
|
||||
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
/// struct checkInfo is initialized at c'tor time and keeps
|
||||
/// info used to detect if a move gives check.
|
||||
/// The checkInfo struct is initialized at c'tor time and keeps info used
|
||||
/// to detect if a move gives check.
|
||||
class Position;
|
||||
struct Thread;
|
||||
|
||||
struct CheckInfo {
|
||||
|
||||
explicit CheckInfo(const Position&);
|
||||
explicit CheckInfo(const Position&);
|
||||
|
||||
Bitboard dcCandidates;
|
||||
Bitboard checkSq[8];
|
||||
Square ksq;
|
||||
};
|
||||
|
||||
/// Castle rights, encoded as bit fields
|
||||
|
||||
enum CastleRights {
|
||||
CASTLES_NONE = 0,
|
||||
WHITE_OO = 1,
|
||||
BLACK_OO = 2,
|
||||
WHITE_OOO = 4,
|
||||
BLACK_OOO = 8,
|
||||
ALL_CASTLES = 15
|
||||
};
|
||||
|
||||
/// Game phase
|
||||
enum Phase {
|
||||
PHASE_ENDGAME = 0,
|
||||
PHASE_MIDGAME = 128
|
||||
Bitboard dcCandidates;
|
||||
Bitboard pinned;
|
||||
Bitboard checkSq[PIECE_TYPE_NB];
|
||||
Square ksq;
|
||||
};
|
||||
|
||||
|
||||
/// The StateInfo struct stores information we need 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), an StateInfo object
|
||||
/// is made on the board (by calling Position::do_move), a StateInfo object
|
||||
/// must be passed as a parameter.
|
||||
|
||||
struct StateInfo {
|
||||
Key pawnKey, materialKey;
|
||||
int castleRights, rule50, gamePly, pliesFromNull;
|
||||
Value npMaterial[COLOR_NB];
|
||||
int castleRights, rule50, pliesFromNull;
|
||||
Score psq;
|
||||
Square epSquare;
|
||||
Score value;
|
||||
Value npMaterial[2];
|
||||
|
||||
PieceType capturedType;
|
||||
Key key;
|
||||
Bitboard checkersBB;
|
||||
PieceType capturedType;
|
||||
StateInfo* previous;
|
||||
};
|
||||
|
||||
|
||||
/// When making a move the current StateInfo up to 'key' excluded is copied to
|
||||
/// 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 data structure. A position consists of the following data:
|
||||
///
|
||||
/// * For each piece type, a bitboard representing the squares occupied
|
||||
@@ -119,108 +90,68 @@ struct StateInfo {
|
||||
/// * A counter for detecting 50 move rule draws.
|
||||
|
||||
class Position {
|
||||
|
||||
friend class MaterialInfo;
|
||||
friend class EndgameFunctions;
|
||||
|
||||
Position(); // No default or copy c'tor allowed
|
||||
Position(const Position& pos);
|
||||
|
||||
public:
|
||||
enum GamePhase {
|
||||
MidGame,
|
||||
EndGame
|
||||
};
|
||||
|
||||
// Constructors
|
||||
Position(const Position& pos, int threadID);
|
||||
Position(const std::string& fen, int threadID);
|
||||
Position() {}
|
||||
Position(const Position& p, Thread* t) { *this = p; thisThread = t; }
|
||||
Position(const std::string& f, bool c960, Thread* t) { set(f, c960, t); }
|
||||
Position& operator=(const Position&);
|
||||
static void init();
|
||||
|
||||
// Text input/output
|
||||
void from_fen(const std::string& fen);
|
||||
const std::string to_fen() const;
|
||||
void print(Move m = MOVE_NONE) const;
|
||||
void set(const std::string& fen, bool isChess960, Thread* th);
|
||||
const std::string fen() const;
|
||||
const std::string pretty(Move m = MOVE_NONE) const;
|
||||
|
||||
// Copying
|
||||
void flipped_copy(const Position& pos);
|
||||
|
||||
// The piece on a given square
|
||||
Piece piece_on(Square s) const;
|
||||
PieceType type_of_piece_on(Square s) const;
|
||||
Color color_of_piece_on(Square s) const;
|
||||
bool square_is_empty(Square s) const;
|
||||
bool square_is_occupied(Square s) const;
|
||||
Value midgame_value_of_piece_on(Square s) const;
|
||||
Value endgame_value_of_piece_on(Square s) const;
|
||||
|
||||
// Side to move
|
||||
Color side_to_move() const;
|
||||
|
||||
// Bitboard representation of the position
|
||||
Bitboard empty_squares() const;
|
||||
Bitboard occupied_squares() const;
|
||||
Bitboard pieces_of_color(Color c) const;
|
||||
// Position representation
|
||||
Bitboard pieces() const;
|
||||
Bitboard pieces(PieceType pt) const;
|
||||
Bitboard pieces(PieceType pt, Color c) const;
|
||||
Bitboard pieces(PieceType pt1, PieceType pt2) const;
|
||||
Bitboard pieces(PieceType pt1, PieceType pt2, Color c) const;
|
||||
|
||||
// Number of pieces of each color and type
|
||||
int piece_count(Color c, PieceType pt) const;
|
||||
|
||||
// The en passant square
|
||||
Square ep_square() const;
|
||||
|
||||
// Current king position for each color
|
||||
Bitboard pieces(Color c) const;
|
||||
Bitboard pieces(Color c, PieceType pt) const;
|
||||
Bitboard pieces(Color c, PieceType pt1, PieceType pt2) const;
|
||||
Piece piece_on(Square s) const;
|
||||
Square king_square(Color c) const;
|
||||
Square ep_square() const;
|
||||
bool is_empty(Square s) const;
|
||||
template<PieceType Pt> int count(Color c) const;
|
||||
template<PieceType Pt> const Square* list(Color c) const;
|
||||
|
||||
// Castling rights
|
||||
bool can_castle_kingside(Color c) const;
|
||||
bool can_castle_queenside(Color c) const;
|
||||
bool can_castle(Color c) const;
|
||||
Square initial_kr_square(Color c) const;
|
||||
Square initial_qr_square(Color c) const;
|
||||
// Castling
|
||||
int can_castle(CastleRight f) const;
|
||||
int can_castle(Color c) const;
|
||||
bool castle_impeded(Color c, CastlingSide s) const;
|
||||
Square castle_rook_square(Color c, CastlingSide s) const;
|
||||
|
||||
// Bitboards for pinned pieces and discovered check candidates
|
||||
Bitboard discovered_check_candidates(Color c) const;
|
||||
Bitboard pinned_pieces(Color c) const;
|
||||
|
||||
// Checking pieces and under check information
|
||||
// Checking
|
||||
Bitboard checkers() const;
|
||||
bool is_check() const;
|
||||
Bitboard discovered_check_candidates() const;
|
||||
Bitboard pinned_pieces() const;
|
||||
|
||||
// Piece lists
|
||||
Square piece_list(Color c, PieceType pt, int index) const;
|
||||
const Square* piece_list_begin(Color c, PieceType pt) const;
|
||||
|
||||
// Information about attacks to or from a given square
|
||||
// Attacks to/from a given square
|
||||
Bitboard attackers_to(Square s) const;
|
||||
Bitboard attackers_to(Square s, Bitboard occ) const;
|
||||
Bitboard attacks_from(Piece p, Square s) const;
|
||||
static Bitboard attacks_from(Piece p, Square s, Bitboard occ);
|
||||
template<PieceType> Bitboard attacks_from(Square s) const;
|
||||
template<PieceType> Bitboard attacks_from(Square s, Color c) const;
|
||||
|
||||
// Properties of moves
|
||||
bool move_gives_check(Move m, const CheckInfo& ci) const;
|
||||
bool pl_move_is_legal(Move m, Bitboard pinned) const;
|
||||
bool pl_move_is_evasion(Move m, Bitboard pinned) const;
|
||||
bool move_is_check(Move m) const;
|
||||
bool move_is_check(Move m, const CheckInfo& ci) const;
|
||||
bool move_is_capture(Move m) const;
|
||||
bool move_is_capture_or_promotion(Move m) const;
|
||||
bool move_is_passed_pawn_push(Move m) const;
|
||||
bool move_attacks_square(Move m, Square s) const;
|
||||
|
||||
// Piece captured with previous moves
|
||||
bool is_pseudo_legal(const Move m) const;
|
||||
bool is_capture(Move m) const;
|
||||
bool is_capture_or_promotion(Move m) const;
|
||||
bool is_passed_pawn_push(Move m) const;
|
||||
Piece piece_moved(Move m) const;
|
||||
PieceType captured_piece_type() const;
|
||||
|
||||
// Information about pawns
|
||||
// Piece specific
|
||||
bool pawn_is_passed(Color c, Square s) const;
|
||||
|
||||
// Weak squares
|
||||
bool square_is_weak(Square s, Color c) const;
|
||||
bool pawn_on_7th(Color c) const;
|
||||
bool opposite_bishops() const;
|
||||
bool bishop_pair(Color c) const;
|
||||
|
||||
// Doing and undoing moves
|
||||
void detach();
|
||||
void do_move(Move m, StateInfo& st);
|
||||
void do_move(Move m, StateInfo& st, const CheckInfo& ci, bool moveIsCheck);
|
||||
void undo_move(Move m);
|
||||
@@ -228,71 +159,43 @@ public:
|
||||
void undo_null_move();
|
||||
|
||||
// Static exchange evaluation
|
||||
int see(Square from, Square to) const;
|
||||
int see(Move m) const;
|
||||
int see(Move m, int asymmThreshold = 0) const;
|
||||
int see_sign(Move m) const;
|
||||
|
||||
// Accessing hash keys
|
||||
Key get_key() const;
|
||||
Key get_exclusion_key() const;
|
||||
Key get_pawn_key() const;
|
||||
Key get_material_key() const;
|
||||
Key key() const;
|
||||
Key exclusion_key() const;
|
||||
Key pawn_key() const;
|
||||
Key material_key() const;
|
||||
|
||||
// Incremental evaluation
|
||||
Score value() const;
|
||||
// Incremental piece-square evaluation
|
||||
Score psq_score() const;
|
||||
Value non_pawn_material(Color c) const;
|
||||
static Score pst_delta(Piece piece, Square from, Square to);
|
||||
|
||||
// Game termination checks
|
||||
bool is_mate() const;
|
||||
bool is_draw() const;
|
||||
|
||||
// Check if side to move could be mated in one
|
||||
bool has_mate_threat();
|
||||
|
||||
// Number of plies since the last non-reversible move
|
||||
int rule_50_counter() const;
|
||||
|
||||
int startpos_ply_counter() const;
|
||||
|
||||
// Other properties of the position
|
||||
bool opposite_colored_bishops() const;
|
||||
bool has_pawn_on_7th(Color c) const;
|
||||
Color side_to_move() const;
|
||||
int game_ply() const;
|
||||
bool is_chess960() const;
|
||||
|
||||
// Current thread ID searching on the position
|
||||
int thread() const;
|
||||
|
||||
// Reset the gamePly variable to 0
|
||||
void reset_game_ply();
|
||||
void inc_startpos_ply_counter();
|
||||
Thread* this_thread() const;
|
||||
int64_t nodes_searched() const;
|
||||
void set_nodes_searched(int64_t n);
|
||||
bool is_draw() const;
|
||||
|
||||
// Position consistency check, for debugging
|
||||
bool is_ok(int* failedStep = NULL) const;
|
||||
|
||||
// Static member functions
|
||||
static void init_zobrist();
|
||||
static void init_piece_square_tables();
|
||||
bool pos_is_ok(int* failedStep = NULL) const;
|
||||
void flip();
|
||||
|
||||
private:
|
||||
|
||||
// Initialization helper functions (used while setting up a position)
|
||||
// Initialization helpers (used while setting up a position)
|
||||
void clear();
|
||||
void put_piece(Piece p, Square s);
|
||||
void do_allow_oo(Color c);
|
||||
void do_allow_ooo(Color c);
|
||||
bool set_castling_rights(char token);
|
||||
void set_castle_right(Color c, Square rfrom);
|
||||
|
||||
// Helper functions for doing and undoing moves
|
||||
void do_capture_move(Key& key, PieceType capture, Color them, Square to, bool ep);
|
||||
void do_castle_move(Move m);
|
||||
void undo_castle_move(Move m);
|
||||
void find_checkers();
|
||||
|
||||
template<bool FindPinned>
|
||||
Bitboard hidden_checkers(Color c) const;
|
||||
// Helper functions
|
||||
void do_castle(Square kfrom, Square kto, Square rfrom, Square rto);
|
||||
Bitboard hidden_checkers(Square ksq, Color c) const;
|
||||
void put_piece(Square s, Color c, PieceType pt);
|
||||
void remove_piece(Square s, Color c, PieceType pt);
|
||||
void move_piece(Square from, Square to, Color c, PieceType pt);
|
||||
|
||||
// Computing hash keys from scratch (for initialization and debugging)
|
||||
Key compute_key() const;
|
||||
@@ -300,52 +203,30 @@ private:
|
||||
Key compute_material_key() const;
|
||||
|
||||
// Computing incremental evaluation scores and material counts
|
||||
static Score pst(Color c, PieceType pt, Square s);
|
||||
Score compute_value() const;
|
||||
Score compute_psq_score() const;
|
||||
Value compute_non_pawn_material(Color c) const;
|
||||
|
||||
// Board
|
||||
Piece board[64];
|
||||
|
||||
// Bitboards
|
||||
Bitboard byTypeBB[8], byColorBB[2];
|
||||
|
||||
// Piece counts
|
||||
int pieceCount[2][8]; // [color][pieceType]
|
||||
|
||||
// Piece lists
|
||||
Square pieceList[2][8][16]; // [color][pieceType][index]
|
||||
int index[64]; // [square]
|
||||
// Board and pieces
|
||||
Piece board[SQUARE_NB];
|
||||
Bitboard byTypeBB[PIECE_TYPE_NB];
|
||||
Bitboard byColorBB[COLOR_NB];
|
||||
int pieceCount[COLOR_NB][PIECE_TYPE_NB];
|
||||
Square pieceList[COLOR_NB][PIECE_TYPE_NB][16];
|
||||
int index[SQUARE_NB];
|
||||
|
||||
// Other info
|
||||
Color sideToMove;
|
||||
Key history[MaxGameLength];
|
||||
int castleRightsMask[64];
|
||||
int castleRightsMask[SQUARE_NB];
|
||||
Square castleRookSquare[COLOR_NB][CASTLING_SIDE_NB];
|
||||
Bitboard castlePath[COLOR_NB][CASTLING_SIDE_NB];
|
||||
StateInfo startState;
|
||||
File initialKFile, initialKRFile, initialQRFile;
|
||||
bool isChess960;
|
||||
int startPosPlyCounter;
|
||||
int threadID;
|
||||
int64_t nodes;
|
||||
int gamePly;
|
||||
Color sideToMove;
|
||||
Thread* thisThread;
|
||||
StateInfo* st;
|
||||
|
||||
// Static variables
|
||||
static Key zobrist[2][8][64];
|
||||
static Key zobEp[64];
|
||||
static Key zobCastle[16];
|
||||
static Key zobSideToMove;
|
||||
static Score PieceSquareTable[16][64];
|
||||
static Key zobExclusion;
|
||||
static const Value seeValues[8];
|
||||
static const Value PieceValueMidgame[17];
|
||||
static const Value PieceValueEndgame[17];
|
||||
int chess960;
|
||||
};
|
||||
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
inline int64_t Position::nodes_searched() const {
|
||||
return nodes;
|
||||
}
|
||||
@@ -358,72 +239,48 @@ inline Piece Position::piece_on(Square s) const {
|
||||
return board[s];
|
||||
}
|
||||
|
||||
inline Color Position::color_of_piece_on(Square s) const {
|
||||
return color_of_piece(piece_on(s));
|
||||
inline Piece Position::piece_moved(Move m) const {
|
||||
return board[from_sq(m)];
|
||||
}
|
||||
|
||||
inline PieceType Position::type_of_piece_on(Square s) const {
|
||||
return type_of_piece(piece_on(s));
|
||||
}
|
||||
|
||||
inline bool Position::square_is_empty(Square s) const {
|
||||
return piece_on(s) == PIECE_NONE;
|
||||
}
|
||||
|
||||
inline bool Position::square_is_occupied(Square s) const {
|
||||
return !square_is_empty(s);
|
||||
}
|
||||
|
||||
inline Value Position::midgame_value_of_piece_on(Square s) const {
|
||||
return PieceValueMidgame[piece_on(s)];
|
||||
}
|
||||
|
||||
inline Value Position::endgame_value_of_piece_on(Square s) const {
|
||||
return PieceValueEndgame[piece_on(s)];
|
||||
inline bool Position::is_empty(Square s) const {
|
||||
return board[s] == NO_PIECE;
|
||||
}
|
||||
|
||||
inline Color Position::side_to_move() const {
|
||||
return sideToMove;
|
||||
}
|
||||
|
||||
inline Bitboard Position::occupied_squares() const {
|
||||
return byTypeBB[0];
|
||||
}
|
||||
|
||||
inline Bitboard Position::empty_squares() const {
|
||||
return ~occupied_squares();
|
||||
}
|
||||
|
||||
inline Bitboard Position::pieces_of_color(Color c) const {
|
||||
return byColorBB[c];
|
||||
inline Bitboard Position::pieces() const {
|
||||
return byTypeBB[ALL_PIECES];
|
||||
}
|
||||
|
||||
inline Bitboard Position::pieces(PieceType pt) const {
|
||||
return byTypeBB[pt];
|
||||
}
|
||||
|
||||
inline Bitboard Position::pieces(PieceType pt, Color c) const {
|
||||
return byTypeBB[pt] & byColorBB[c];
|
||||
}
|
||||
|
||||
inline Bitboard Position::pieces(PieceType pt1, PieceType pt2) const {
|
||||
return byTypeBB[pt1] | byTypeBB[pt2];
|
||||
}
|
||||
|
||||
inline Bitboard Position::pieces(PieceType pt1, PieceType pt2, Color c) const {
|
||||
return (byTypeBB[pt1] | byTypeBB[pt2]) & byColorBB[c];
|
||||
inline Bitboard Position::pieces(Color c) const {
|
||||
return byColorBB[c];
|
||||
}
|
||||
|
||||
inline int Position::piece_count(Color c, PieceType pt) const {
|
||||
return pieceCount[c][pt];
|
||||
inline Bitboard Position::pieces(Color c, PieceType pt) const {
|
||||
return byColorBB[c] & byTypeBB[pt];
|
||||
}
|
||||
|
||||
inline Square Position::piece_list(Color c, PieceType pt, int idx) const {
|
||||
return pieceList[c][pt][idx];
|
||||
inline Bitboard Position::pieces(Color c, PieceType pt1, PieceType pt2) const {
|
||||
return byColorBB[c] & (byTypeBB[pt1] | byTypeBB[pt2]);
|
||||
}
|
||||
|
||||
inline const Square* Position::piece_list_begin(Color c, PieceType pt) const {
|
||||
return pieceList[c][pt];
|
||||
template<PieceType Pt> inline int Position::count(Color c) const {
|
||||
return pieceCount[c][Pt];
|
||||
}
|
||||
|
||||
template<PieceType Pt> inline const Square* Position::list(Color c) const {
|
||||
return pieceList[c][Pt];
|
||||
}
|
||||
|
||||
inline Square Position::ep_square() const {
|
||||
@@ -434,155 +291,169 @@ inline Square Position::king_square(Color c) const {
|
||||
return pieceList[c][KING][0];
|
||||
}
|
||||
|
||||
inline bool Position::can_castle_kingside(Color side) const {
|
||||
return st->castleRights & (1+int(side));
|
||||
inline int Position::can_castle(CastleRight f) const {
|
||||
return st->castleRights & f;
|
||||
}
|
||||
|
||||
inline bool Position::can_castle_queenside(Color side) const {
|
||||
return st->castleRights & (4+4*int(side));
|
||||
inline int Position::can_castle(Color c) const {
|
||||
return st->castleRights & ((WHITE_OO | WHITE_OOO) << (2 * c));
|
||||
}
|
||||
|
||||
inline bool Position::can_castle(Color side) const {
|
||||
return can_castle_kingside(side) || can_castle_queenside(side);
|
||||
inline bool Position::castle_impeded(Color c, CastlingSide s) const {
|
||||
return byTypeBB[ALL_PIECES] & castlePath[c][s];
|
||||
}
|
||||
|
||||
inline Square Position::initial_kr_square(Color c) const {
|
||||
return relative_square(c, make_square(initialKRFile, RANK_1));
|
||||
inline Square Position::castle_rook_square(Color c, CastlingSide s) const {
|
||||
return castleRookSquare[c][s];
|
||||
}
|
||||
|
||||
inline Square Position::initial_qr_square(Color c) const {
|
||||
return relative_square(c, make_square(initialQRFile, RANK_1));
|
||||
template<PieceType Pt>
|
||||
inline Bitboard Position::attacks_from(Square s) const {
|
||||
|
||||
return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, pieces())
|
||||
: Pt == QUEEN ? attacks_from<ROOK>(s) | attacks_from<BISHOP>(s)
|
||||
: StepAttacksBB[Pt][s];
|
||||
}
|
||||
|
||||
template<>
|
||||
inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
|
||||
return StepAttackBB[piece_of_color_and_type(c, PAWN)][s];
|
||||
return StepAttacksBB[make_piece(c, PAWN)][s];
|
||||
}
|
||||
|
||||
template<PieceType Piece> // Knight and King and white pawns
|
||||
inline Bitboard Position::attacks_from(Square s) const {
|
||||
return StepAttackBB[Piece][s];
|
||||
inline Bitboard Position::attacks_from(Piece p, Square s) const {
|
||||
return attacks_from(p, s, byTypeBB[ALL_PIECES]);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline Bitboard Position::attacks_from<BISHOP>(Square s) const {
|
||||
return bishop_attacks_bb(s, occupied_squares());
|
||||
}
|
||||
|
||||
template<>
|
||||
inline Bitboard Position::attacks_from<ROOK>(Square s) const {
|
||||
return rook_attacks_bb(s, occupied_squares());
|
||||
}
|
||||
|
||||
template<>
|
||||
inline Bitboard Position::attacks_from<QUEEN>(Square s) const {
|
||||
return attacks_from<ROOK>(s) | attacks_from<BISHOP>(s);
|
||||
inline Bitboard Position::attackers_to(Square s) const {
|
||||
return attackers_to(s, byTypeBB[ALL_PIECES]);
|
||||
}
|
||||
|
||||
inline Bitboard Position::checkers() const {
|
||||
return st->checkersBB;
|
||||
}
|
||||
|
||||
inline bool Position::is_check() const {
|
||||
return st->checkersBB != EmptyBoardBB;
|
||||
inline Bitboard Position::discovered_check_candidates() const {
|
||||
return hidden_checkers(king_square(~sideToMove), sideToMove);
|
||||
}
|
||||
|
||||
inline Bitboard Position::pinned_pieces() const {
|
||||
return hidden_checkers(king_square(sideToMove), ~sideToMove);
|
||||
}
|
||||
|
||||
inline bool Position::pawn_is_passed(Color c, Square s) const {
|
||||
return !(pieces(PAWN, opposite_color(c)) & passed_pawn_mask(c, s));
|
||||
return !(pieces(~c, PAWN) & passed_pawn_mask(c, s));
|
||||
}
|
||||
|
||||
inline bool Position::square_is_weak(Square s, Color c) const {
|
||||
return !(pieces(PAWN, opposite_color(c)) & attack_span_mask(c, s));
|
||||
}
|
||||
|
||||
inline Key Position::get_key() const {
|
||||
inline Key Position::key() const {
|
||||
return st->key;
|
||||
}
|
||||
|
||||
inline Key Position::get_exclusion_key() const {
|
||||
return st->key ^ zobExclusion;
|
||||
}
|
||||
|
||||
inline Key Position::get_pawn_key() const {
|
||||
inline Key Position::pawn_key() const {
|
||||
return st->pawnKey;
|
||||
}
|
||||
|
||||
inline Key Position::get_material_key() const {
|
||||
inline Key Position::material_key() const {
|
||||
return st->materialKey;
|
||||
}
|
||||
|
||||
inline Score Position::pst(Color c, PieceType pt, Square s) {
|
||||
return PieceSquareTable[piece_of_color_and_type(c, pt)][s];
|
||||
}
|
||||
|
||||
inline Score Position::pst_delta(Piece piece, Square from, Square to) {
|
||||
return PieceSquareTable[piece][to] - PieceSquareTable[piece][from];
|
||||
}
|
||||
|
||||
inline Score Position::value() const {
|
||||
return st->value;
|
||||
inline Score Position::psq_score() const {
|
||||
return st->psq;
|
||||
}
|
||||
|
||||
inline Value Position::non_pawn_material(Color c) const {
|
||||
return st->npMaterial[c];
|
||||
}
|
||||
|
||||
inline bool Position::move_is_passed_pawn_push(Move m) const {
|
||||
inline bool Position::is_passed_pawn_push(Move m) const {
|
||||
|
||||
Color c = side_to_move();
|
||||
return piece_on(move_from(m)) == piece_of_color_and_type(c, PAWN)
|
||||
&& pawn_is_passed(c, move_to(m));
|
||||
return type_of(piece_moved(m)) == PAWN
|
||||
&& pawn_is_passed(sideToMove, to_sq(m));
|
||||
}
|
||||
|
||||
inline int Position::rule_50_counter() const {
|
||||
return st->rule50;
|
||||
inline int Position::game_ply() const {
|
||||
return gamePly;
|
||||
}
|
||||
|
||||
inline int Position::startpos_ply_counter() const {
|
||||
return startPosPlyCounter;
|
||||
inline bool Position::opposite_bishops() const {
|
||||
|
||||
return pieceCount[WHITE][BISHOP] == 1
|
||||
&& pieceCount[BLACK][BISHOP] == 1
|
||||
&& opposite_colors(pieceList[WHITE][BISHOP][0], pieceList[BLACK][BISHOP][0]);
|
||||
}
|
||||
|
||||
inline bool Position::opposite_colored_bishops() const {
|
||||
inline bool Position::bishop_pair(Color c) const {
|
||||
|
||||
return piece_count(WHITE, BISHOP) == 1
|
||||
&& piece_count(BLACK, BISHOP) == 1
|
||||
&& !same_color_squares(piece_list(WHITE, BISHOP, 0), piece_list(BLACK, BISHOP, 0));
|
||||
return pieceCount[c][BISHOP] >= 2
|
||||
&& opposite_colors(pieceList[c][BISHOP][0], pieceList[c][BISHOP][1]);
|
||||
}
|
||||
|
||||
inline bool Position::has_pawn_on_7th(Color c) const {
|
||||
return pieces(PAWN, c) & relative_rank_bb(c, RANK_7);
|
||||
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 {
|
||||
return isChess960;
|
||||
return chess960;
|
||||
}
|
||||
|
||||
inline bool Position::move_is_capture(Move m) const {
|
||||
inline bool Position::is_capture_or_promotion(Move m) const {
|
||||
|
||||
// Move must not be MOVE_NONE !
|
||||
return (m & (3 << 15)) ? !move_is_castle(m) : !square_is_empty(move_to(m));
|
||||
assert(is_ok(m));
|
||||
return type_of(m) ? type_of(m) != CASTLE : !is_empty(to_sq(m));
|
||||
}
|
||||
|
||||
inline bool Position::move_is_capture_or_promotion(Move m) const {
|
||||
inline bool Position::is_capture(Move m) const {
|
||||
|
||||
// Move must not be MOVE_NONE !
|
||||
return (m & (0x1F << 12)) ? !move_is_castle(m) : !square_is_empty(move_to(m));
|
||||
// Note that castle is coded as "king captures the rook"
|
||||
assert(is_ok(m));
|
||||
return (!is_empty(to_sq(m)) && type_of(m) != CASTLE) || type_of(m) == ENPASSANT;
|
||||
}
|
||||
|
||||
inline PieceType Position::captured_piece_type() const {
|
||||
return st->capturedType;
|
||||
}
|
||||
|
||||
inline int Position::thread() const {
|
||||
return threadID;
|
||||
inline Thread* Position::this_thread() const {
|
||||
return thisThread;
|
||||
}
|
||||
|
||||
inline void Position::do_allow_oo(Color c) {
|
||||
st->castleRights |= (1 + int(c));
|
||||
inline void Position::put_piece(Square s, Color c, PieceType pt) {
|
||||
|
||||
board[s] = make_piece(c, pt);
|
||||
byTypeBB[ALL_PIECES] |= s;
|
||||
byTypeBB[pt] |= s;
|
||||
byColorBB[c] |= s;
|
||||
index[s] = pieceCount[c][pt]++;
|
||||
pieceList[c][pt][index[s]] = s;
|
||||
}
|
||||
|
||||
inline void Position::do_allow_ooo(Color c) {
|
||||
st->castleRights |= (4 + 4*int(c));
|
||||
inline void Position::move_piece(Square from, Square to, Color c, PieceType pt) {
|
||||
|
||||
// index[from] is not updated and becomes stale. This works as long
|
||||
// as index[] is accessed just by known occupied squares.
|
||||
Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
|
||||
byTypeBB[ALL_PIECES] ^= from_to_bb;
|
||||
byTypeBB[pt] ^= from_to_bb;
|
||||
byColorBB[c] ^= from_to_bb;
|
||||
board[from] = NO_PIECE;
|
||||
board[to] = make_piece(c, pt);
|
||||
index[to] = index[from];
|
||||
pieceList[c][pt][index[to]] = to;
|
||||
}
|
||||
|
||||
#endif // !defined(POSITION_H_INCLUDED)
|
||||
inline void Position::remove_piece(Square s, Color c, PieceType pt) {
|
||||
|
||||
// WARNING: This is not a reversible operation. If we remove a piece in
|
||||
// do_move() and then replace it in undo_move() we will put it at the end of
|
||||
// the list and not in its original place, it means index[] and pieceList[]
|
||||
// are not guaranteed to be invariant to a do_move() + undo_move() sequence.
|
||||
byTypeBB[ALL_PIECES] ^= s;
|
||||
byTypeBB[pt] ^= s;
|
||||
byColorBB[c] ^= s;
|
||||
/* board[s] = NO_PIECE; */ // Not needed, will be overwritten by capturing
|
||||
Square lastSquare = pieceList[c][pt][--pieceCount[c][pt]];
|
||||
index[lastSquare] = index[s];
|
||||
pieceList[c][pt][index[lastSquare]] = lastSquare;
|
||||
pieceList[c][pt][pieceCount[c][pt]] = SQ_NONE;
|
||||
}
|
||||
|
||||
#endif // #ifndef POSITION_H_INCLUDED
|
||||
|
||||
+64
-156
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,174 +17,82 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(PSQTAB_H_INCLUDED)
|
||||
#ifndef PSQTAB_H_INCLUDED
|
||||
#define PSQTAB_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
#include "types.h"
|
||||
|
||||
#include "value.h"
|
||||
#define S(mg, eg) make_score(mg, eg)
|
||||
|
||||
namespace {
|
||||
|
||||
////
|
||||
//// Constants modified by Joona Kiiski
|
||||
////
|
||||
/// PSQT[PieceType][Square] contains Piece-Square scores. For each piece type on
|
||||
/// a given square a (midgame, endgame) score pair is assigned. PSQT is defined
|
||||
/// for white side, for black side the tables are symmetric.
|
||||
|
||||
const Value MP = PawnValueMidgame;
|
||||
const Value MK = KnightValueMidgame;
|
||||
const Value MB = BishopValueMidgame;
|
||||
const Value MR = RookValueMidgame;
|
||||
const Value MQ = QueenValueMidgame;
|
||||
|
||||
const Value EP = PawnValueEndgame;
|
||||
const Value EK = KnightValueEndgame;
|
||||
const Value EB = BishopValueEndgame;
|
||||
const Value ER = RookValueEndgame;
|
||||
const Value EQ = QueenValueEndgame;
|
||||
|
||||
const int MgPST[][64] = {
|
||||
static const Score PSQT[][SQUARE_NB] = {
|
||||
{ },
|
||||
{// Pawn
|
||||
// A B C D E F G H
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
MP-28, MP-6, MP+ 4, MP+14, MP+14, MP+ 4, MP-6, MP-28,
|
||||
MP-28, MP-6, MP+ 9, MP+36, MP+36, MP+ 9, MP-6, MP-28,
|
||||
MP-28, MP-6, MP+17, MP+58, MP+58, MP+17, MP-6, MP-28,
|
||||
MP-28, MP-6, MP+17, MP+36, MP+36, MP+17, MP-6, MP-28,
|
||||
MP-28, MP-6, MP+ 9, MP+14, MP+14, MP+ 9, MP-6, MP-28,
|
||||
MP-28, MP-6, MP+ 4, MP+14, MP+14, MP+ 4, MP-6, MP-28,
|
||||
0, 0, 0, 0, 0, 0, 0, 0
|
||||
{ // 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,-8), S(-6,-8), S( 4,-8), S(14,-8), S(14,-8), S( 4,-8), S(-6,-8), S(-20,-8),
|
||||
S(-20,-8), S(-6,-8), S( 9,-8), S(34,-8), S(34,-8), S( 9,-8), S(-6,-8), S(-20,-8),
|
||||
S(-20,-8), S(-6,-8), S(17,-8), S(54,-8), S(54,-8), S(17,-8), S(-6,-8), S(-20,-8),
|
||||
S(-20,-8), S(-6,-8), S(17,-8), S(34,-8), S(34,-8), S(17,-8), S(-6,-8), S(-20,-8),
|
||||
S(-20,-8), S(-6,-8), S( 9,-8), S(14,-8), S(14,-8), S( 9,-8), S(-6,-8), S(-20,-8),
|
||||
S(-20,-8), S(-6,-8), S( 4,-8), S(14,-8), S(14,-8), S( 4,-8), S(-6,-8), S(-20,-8),
|
||||
S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S( 0, 0)
|
||||
},
|
||||
{// Knight
|
||||
// A B C D E F G H
|
||||
MK-135, MK-107, MK-80, MK-67, MK-67, MK-80, MK-107, MK-135,
|
||||
MK- 93, MK- 67, MK-39, MK-25, MK-25, MK-39, MK- 67, MK- 93,
|
||||
MK- 53, MK- 25, MK+ 1, MK+13, MK+13, MK+ 1, MK- 25, MK- 53,
|
||||
MK- 25, MK+ 1, MK+27, MK+41, MK+41, MK+27, MK+ 1, MK- 25,
|
||||
MK- 11, MK+ 13, MK+41, MK+55, MK+55, MK+41, MK+ 13, MK- 11,
|
||||
MK- 11, MK+ 13, MK+41, MK+55, MK+55, MK+41, MK+ 13, MK- 11,
|
||||
MK- 53, MK- 25, MK+ 1, MK+13, MK+13, MK+ 1, MK- 25, MK- 53,
|
||||
MK-193, MK- 67, MK-39, MK-25, MK-25, MK-39, MK- 67, MK-193
|
||||
{ // Knight
|
||||
S(-135,-104), S(-107,-79), S(-80,-55), S(-67,-42), S(-67,-42), S(-80,-55), S(-107,-79), S(-135,-104),
|
||||
S( -93, -79), S( -67,-55), S(-39,-30), S(-25,-17), S(-25,-17), S(-39,-30), S( -67,-55), S( -93, -79),
|
||||
S( -53, -55), S( -25,-30), S( 1, -6), S( 13, 5), S( 13, 5), S( 1, -6), S( -25,-30), S( -53, -55),
|
||||
S( -25, -42), S( 1,-17), S( 27, 5), S( 41, 18), S( 41, 18), S( 27, 5), S( 1,-17), S( -25, -42),
|
||||
S( -11, -42), S( 13,-17), S( 41, 5), S( 55, 18), S( 55, 18), S( 41, 5), S( 13,-17), S( -11, -42),
|
||||
S( -11, -55), S( 13,-30), S( 41, -6), S( 55, 5), S( 55, 5), S( 41, -6), S( 13,-30), S( -11, -55),
|
||||
S( -53, -79), S( -25,-55), S( 1,-30), S( 13,-17), S( 13,-17), S( 1,-30), S( -25,-55), S( -53, -79),
|
||||
S(-193,-104), S( -67,-79), S(-39,-55), S(-25,-42), S(-25,-42), S(-39,-55), S( -67,-79), S(-193,-104)
|
||||
},
|
||||
{// Bishop
|
||||
// A B C D E F G H
|
||||
MB-40, MB-40, MB-35, MB-30, MB-30, MB-35, MB-40, MB-40,
|
||||
MB-17, MB+ 0, MB- 4, MB+ 0, MB+ 0, MB- 4, MB+ 0, MB-17,
|
||||
MB-13, MB- 4, MB+ 8, MB+ 4, MB+ 4, MB+ 8, MB- 4, MB-13,
|
||||
MB- 8, MB+ 0, MB+ 4, MB+17, MB+17, MB+ 4, MB+ 0, MB- 8,
|
||||
MB- 8, MB+ 0, MB+ 4, MB+17, MB+17, MB+ 4, MB+ 0, MB- 8,
|
||||
MB-13, MB- 4, MB+ 8, MB+ 4, MB+ 4, MB+ 8, MB- 4, MB-13,
|
||||
MB-17, MB+ 0, MB- 4, MB+ 0, MB+ 0, MB- 4, MB+ 0, MB-17,
|
||||
MB-17, MB-17, MB-13, MB- 8, MB- 8, MB-13, MB-17, MB-17
|
||||
{ // Bishop
|
||||
S(-40,-59), S(-40,-42), S(-35,-35), S(-30,-26), S(-30,-26), S(-35,-35), S(-40,-42), S(-40,-59),
|
||||
S(-17,-42), S( 0,-26), S( -4,-18), S( 0,-11), S( 0,-11), S( -4,-18), S( 0,-26), S(-17,-42),
|
||||
S(-13,-35), S( -4,-18), S( 8,-11), S( 4, -4), S( 4, -4), S( 8,-11), S( -4,-18), S(-13,-35),
|
||||
S( -8,-26), S( 0,-11), S( 4, -4), S( 17, 4), S( 17, 4), S( 4, -4), S( 0,-11), S( -8,-26),
|
||||
S( -8,-26), S( 0,-11), S( 4, -4), S( 17, 4), S( 17, 4), S( 4, -4), S( 0,-11), S( -8,-26),
|
||||
S(-13,-35), S( -4,-18), S( 8,-11), S( 4, -4), S( 4, -4), S( 8,-11), S( -4,-18), S(-13,-35),
|
||||
S(-17,-42), S( 0,-26), S( -4,-18), S( 0,-11), S( 0,-11), S( -4,-18), S( 0,-26), S(-17,-42),
|
||||
S(-17,-59), S(-17,-42), S(-13,-35), S( -8,-26), S( -8,-26), S(-13,-35), S(-17,-42), S(-17,-59)
|
||||
},
|
||||
{// Rook
|
||||
// A B C D E F G H
|
||||
MR-12, MR-7, MR-2, MR+2, MR+2, MR-2, MR-7, MR-12,
|
||||
MR-12, MR-7, MR-2, MR+2, MR+2, MR-2, MR-7, MR-12,
|
||||
MR-12, MR-7, MR-2, MR+2, MR+2, MR-2, MR-7, MR-12,
|
||||
MR-12, MR-7, MR-2, MR+2, MR+2, MR-2, MR-7, MR-12,
|
||||
MR-12, MR-7, MR-2, MR+2, MR+2, MR-2, MR-7, MR-12,
|
||||
MR-12, MR-7, MR-2, MR+2, MR+2, MR-2, MR-7, MR-12,
|
||||
MR-12, MR-7, MR-2, MR+2, MR+2, MR-2, MR-7, MR-12,
|
||||
MR-12, MR-7, MR-2, MR+2, MR+2, MR-2, MR-7, MR-12
|
||||
{ // Rook
|
||||
S(-12, 3), S(-7, 3), S(-2, 3), S(2, 3), S(2, 3), S(-2, 3), S(-7, 3), S(-12, 3),
|
||||
S(-12, 3), S(-7, 3), S(-2, 3), S(2, 3), S(2, 3), S(-2, 3), S(-7, 3), S(-12, 3),
|
||||
S(-12, 3), S(-7, 3), S(-2, 3), S(2, 3), S(2, 3), S(-2, 3), S(-7, 3), S(-12, 3),
|
||||
S(-12, 3), S(-7, 3), S(-2, 3), S(2, 3), S(2, 3), S(-2, 3), S(-7, 3), S(-12, 3),
|
||||
S(-12, 3), S(-7, 3), S(-2, 3), S(2, 3), S(2, 3), S(-2, 3), S(-7, 3), S(-12, 3),
|
||||
S(-12, 3), S(-7, 3), S(-2, 3), S(2, 3), S(2, 3), S(-2, 3), S(-7, 3), S(-12, 3),
|
||||
S(-12, 3), S(-7, 3), S(-2, 3), S(2, 3), S(2, 3), S(-2, 3), S(-7, 3), S(-12, 3),
|
||||
S(-12, 3), S(-7, 3), S(-2, 3), S(2, 3), S(2, 3), S(-2, 3), S(-7, 3), S(-12, 3)
|
||||
},
|
||||
{// Queen
|
||||
// A B C D E F G H
|
||||
MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8,
|
||||
MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8,
|
||||
MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8,
|
||||
MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8,
|
||||
MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8,
|
||||
MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8,
|
||||
MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8,
|
||||
MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8, MQ+8
|
||||
{ // Queen
|
||||
S(8,-80), S(8,-54), S(8,-42), S(8,-30), S(8,-30), S(8,-42), S(8,-54), S(8,-80),
|
||||
S(8,-54), S(8,-30), S(8,-18), S(8, -6), S(8, -6), S(8,-18), S(8,-30), S(8,-54),
|
||||
S(8,-42), S(8,-18), S(8, -6), S(8, 6), S(8, 6), S(8, -6), S(8,-18), S(8,-42),
|
||||
S(8,-30), S(8, -6), S(8, 6), S(8, 18), S(8, 18), S(8, 6), S(8, -6), S(8,-30),
|
||||
S(8,-30), S(8, -6), S(8, 6), S(8, 18), S(8, 18), S(8, 6), S(8, -6), S(8,-30),
|
||||
S(8,-42), S(8,-18), S(8, -6), S(8, 6), S(8, 6), S(8, -6), S(8,-18), S(8,-42),
|
||||
S(8,-54), S(8,-30), S(8,-18), S(8, -6), S(8, -6), S(8,-18), S(8,-30), S(8,-54),
|
||||
S(8,-80), S(8,-54), S(8,-42), S(8,-30), S(8,-30), S(8,-42), S(8,-54), S(8,-80)
|
||||
},
|
||||
{// King
|
||||
//A B C D E F G H
|
||||
287, 311, 262, 214, 214, 262, 311, 287,
|
||||
262, 287, 238, 190, 190, 238, 287, 262,
|
||||
214, 238, 190, 142, 142, 190, 238, 214,
|
||||
190, 214, 167, 119, 119, 167, 214, 190,
|
||||
167, 190, 142, 94, 94, 142, 190, 167,
|
||||
142, 167, 119, 69, 69, 119, 167, 142,
|
||||
119, 142, 94, 46, 46, 94, 142, 119,
|
||||
94, 119, 69, 21, 21, 69, 119, 94
|
||||
{ // King
|
||||
S(287, 18), S(311, 77), S(262,105), S(214,135), S(214,135), S(262,105), S(311, 77), S(287, 18),
|
||||
S(262, 77), S(287,135), S(238,165), S(190,193), S(190,193), S(238,165), S(287,135), S(262, 77),
|
||||
S(214,105), S(238,165), S(190,193), S(142,222), S(142,222), S(190,193), S(238,165), S(214,105),
|
||||
S(190,135), S(214,193), S(167,222), S(119,251), S(119,251), S(167,222), S(214,193), S(190,135),
|
||||
S(167,135), S(190,193), S(142,222), S( 94,251), S( 94,251), S(142,222), S(190,193), S(167,135),
|
||||
S(142,105), S(167,165), S(119,193), S( 69,222), S( 69,222), S(119,193), S(167,165), S(142,105),
|
||||
S(119, 77), S(142,135), S( 94,165), S( 46,193), S( 46,193), S( 94,165), S(142,135), S(119, 77),
|
||||
S(94, 18), S(119, 77), S( 69,105), S( 21,135), S( 21,135), S( 69,105), S(119, 77), S( 94, 18)
|
||||
}
|
||||
};
|
||||
|
||||
const int EgPST[][64] = {
|
||||
{ },
|
||||
{// Pawn
|
||||
// A B C D E F G H
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
EP-8, EP-8, EP-8, EP-8, EP-8, EP-8, EP-8, EP-8,
|
||||
EP-8, EP-8, EP-8, EP-8, EP-8, EP-8, EP-8, EP-8,
|
||||
EP-8, EP-8, EP-8, EP-8, EP-8, EP-8, EP-8, EP-8,
|
||||
EP-8, EP-8, EP-8, EP-8, EP-8, EP-8, EP-8, EP-8,
|
||||
EP-8, EP-8, EP-8, EP-8, EP-8, EP-8, EP-8, EP-8,
|
||||
EP-8, EP-8, EP-8, EP-8, EP-8, EP-8, EP-8, EP-8,
|
||||
0, 0, 0, 0, 0, 0, 0, 0
|
||||
},
|
||||
{// Knight
|
||||
// A B C D E F G H
|
||||
EK-104, EK-79, EK-55, EK-42, EK-42, EK-55, EK-79, EK-104,
|
||||
EK- 79, EK-55, EK-30, EK-17, EK-17, EK-30, EK-55, EK- 79,
|
||||
EK- 55, EK-30, EK- 6, EK+ 5, EK+ 5, EK- 6, EK-30, EK- 55,
|
||||
EK- 42, EK-17, EK+ 5, EK+18, EK+18, EK+ 5, EK-17, EK- 42,
|
||||
EK- 42, EK-17, EK+ 5, EK+18, EK+18, EK+ 5, EK-17, EK- 42,
|
||||
EK- 55, EK-30, EK- 6, EK+ 5, EK+ 5, EK- 6, EK-30, EK- 55,
|
||||
EK- 79, EK-55, EK-30, EK-17, EK-17, EK-30, EK-55, EK- 79,
|
||||
EK-104, EK-79, EK-55, EK-42, EK-42, EK-55, EK-79, EK-104
|
||||
},
|
||||
{// Bishop
|
||||
// A B C D E F G H
|
||||
EB-59, EB-42, EB-35, EB-26, EB-26, EB-35, EB-42, EB-59,
|
||||
EB-42, EB-26, EB-18, EB-11, EB-11, EB-18, EB-26, EB-42,
|
||||
EB-35, EB-18, EB-11, EB- 4, EB- 4, EB-11, EB-18, EB-35,
|
||||
EB-26, EB-11, EB- 4, EB+ 4, EB+ 4, EB- 4, EB-11, EB-26,
|
||||
EB-26, EB-11, EB- 4, EB+ 4, EB+ 4, EB- 4, EB-11, EB-26,
|
||||
EB-35, EB-18, EB-11, EB- 4, EB- 4, EB-11, EB-18, EB-35,
|
||||
EB-42, EB-26, EB-18, EB-11, EB-11, EB-18, EB-26, EB-42,
|
||||
EB-59, EB-42, EB-35, EB-26, EB-26, EB-35, EB-42, EB-59
|
||||
},
|
||||
{// Rook
|
||||
// A B C D E F G H
|
||||
ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3,
|
||||
ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3,
|
||||
ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3,
|
||||
ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3,
|
||||
ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3,
|
||||
ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3,
|
||||
ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3,
|
||||
ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3, ER+3
|
||||
},
|
||||
{// Queen
|
||||
// A B C D E F G H
|
||||
EQ-80, EQ-54, EQ-42, EQ-30, EQ-30, EQ-42, EQ-54, EQ-80,
|
||||
EQ-54, EQ-30, EQ-18, EQ- 6, EQ- 6, EQ-18, EQ-30, EQ-54,
|
||||
EQ-42, EQ-18, EQ- 6, EQ+ 6, EQ+ 6, EQ- 6, EQ-18, EQ-42,
|
||||
EQ-30, EQ- 6, EQ+ 6, EQ+18, EQ+18, EQ+ 6, EQ- 6, EQ-30,
|
||||
EQ-30, EQ- 6, EQ+ 6, EQ+18, EQ+18, EQ+ 6, EQ- 6, EQ-30,
|
||||
EQ-42, EQ-18, EQ- 6, EQ+ 6, EQ+ 6, EQ- 6, EQ-18, EQ-42,
|
||||
EQ-54, EQ-30, EQ-18, EQ- 6, EQ- 6, EQ-18, EQ-30, EQ-54,
|
||||
EQ-80, EQ-54, EQ-42, EQ-30, EQ-30, EQ-42, EQ-54, EQ-80
|
||||
},
|
||||
{// King
|
||||
//A B C D E F G H
|
||||
18, 77, 105, 135, 135, 105, 77, 18,
|
||||
77, 135, 165, 193, 193, 165, 135, 77,
|
||||
105, 165, 193, 222, 222, 193, 165, 105,
|
||||
135, 193, 222, 251, 251, 222, 193, 135,
|
||||
135, 193, 222, 251, 251, 222, 193, 135,
|
||||
105, 165, 193, 222, 222, 193, 165, 105,
|
||||
77, 135, 165, 193, 193, 165, 135, 77,
|
||||
18, 77, 105, 135, 135, 105, 77, 18
|
||||
}
|
||||
};
|
||||
#undef S
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // !defined(PSQTAB_H_INCLUDED)
|
||||
#endif // #ifndef PSQTAB_H_INCLUDED
|
||||
|
||||
+34
-44
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -20,65 +20,55 @@
|
||||
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.
|
||||
|
||||
** A small "keep it simple and stupid" RNG with some fancy merits:
|
||||
**
|
||||
** 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
|
||||
**
|
||||
** (c) Heinz van Saanen
|
||||
|
||||
*/
|
||||
|
||||
#if !defined(RKISS_H_INCLUDED)
|
||||
#ifndef RKISS_H_INCLUDED
|
||||
#define RKISS_H_INCLUDED
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include "types.h"
|
||||
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
/// 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 {
|
||||
|
||||
// Keep variables always together
|
||||
struct S { uint64_t a, b, c, d; } s;
|
||||
struct S { uint64_t a, b, c, d; } s; // Keep variables always together
|
||||
|
||||
// Return 64 bit unsigned integer in between [0,2^64-1]
|
||||
uint64_t rand64() {
|
||||
|
||||
const uint64_t
|
||||
e = s.a - ((s.b << 7) | (s.b >> 57));
|
||||
s.a = s.b ^ ((s.c << 13) | (s.c >> 51));
|
||||
s.b = s.c + ((s.d << 37) | (s.d >> 27));
|
||||
s.c = s.d + e;
|
||||
return s.d = e + s.a;
|
||||
uint64_t rotate(uint64_t x, uint64_t k) const {
|
||||
return (x << k) | (x >> (64 - k));
|
||||
}
|
||||
|
||||
// Init seed and scramble a few rounds
|
||||
void raninit() {
|
||||
uint64_t rand64() {
|
||||
|
||||
s.a = 0xf1ea5eed;
|
||||
s.b = s.c = s.d = 0xd4e12c77;
|
||||
for (int i = 0; i < 73; i++)
|
||||
rand64();
|
||||
const uint64_t
|
||||
e = s.a - rotate(s.b, 7);
|
||||
s.a = s.b ^ rotate(s.c, 13);
|
||||
s.b = s.c + rotate(s.d, 37);
|
||||
s.c = s.d + e;
|
||||
return s.d = e + s.a;
|
||||
}
|
||||
|
||||
public:
|
||||
RKISS() { raninit(); }
|
||||
RKISS(int seed = 73) {
|
||||
|
||||
s.a = 0xf1ea5eed;
|
||||
s.b = s.c = s.d = 0xd4e12c77;
|
||||
for (int i = 0; i < seed; i++) // Scramble a few rounds
|
||||
rand64();
|
||||
}
|
||||
|
||||
template<typename T> T rand() { return T(rand64()); }
|
||||
};
|
||||
|
||||
#endif // !defined(RKISS_H_INCLUDED)
|
||||
#endif // #ifndef RKISS_H_INCLUDED
|
||||
|
||||
-439
@@ -1,439 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 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/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
|
||||
#include "movegen.h"
|
||||
#include "san.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
////
|
||||
//// Local definitions
|
||||
////
|
||||
|
||||
namespace {
|
||||
|
||||
enum Ambiguity {
|
||||
AMBIGUITY_NONE, AMBIGUITY_FILE, AMBIGUITY_RANK, AMBIGUITY_BOTH
|
||||
};
|
||||
|
||||
Ambiguity move_ambiguity(const Position& pos, Move m);
|
||||
const string time_string(int milliseconds);
|
||||
const string score_string(Value v);
|
||||
}
|
||||
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
/// move_to_san() takes a position and a move as input, where it is assumed
|
||||
/// that the move is a legal move from the position. The return value is
|
||||
/// a string containing the move in short algebraic notation.
|
||||
|
||||
const string move_to_san(Position& pos, Move m) {
|
||||
|
||||
assert(pos.is_ok());
|
||||
assert(move_is_ok(m));
|
||||
|
||||
string san;
|
||||
Square from = move_from(m);
|
||||
Square to = move_to(m);
|
||||
PieceType pt = type_of_piece(pos.piece_on(from));
|
||||
|
||||
if (m == MOVE_NONE)
|
||||
return "(none)";
|
||||
|
||||
if (m == MOVE_NULL)
|
||||
return "(null)";
|
||||
|
||||
if (move_is_long_castle(m))
|
||||
san = "O-O-O";
|
||||
else if (move_is_short_castle(m))
|
||||
san = "O-O";
|
||||
else
|
||||
{
|
||||
if (pt != PAWN)
|
||||
{
|
||||
san += piece_type_to_char(pt);
|
||||
|
||||
switch (move_ambiguity(pos, m)) {
|
||||
case AMBIGUITY_NONE:
|
||||
break;
|
||||
case AMBIGUITY_FILE:
|
||||
san += file_to_char(square_file(from));
|
||||
break;
|
||||
case AMBIGUITY_RANK:
|
||||
san += rank_to_char(square_rank(from));
|
||||
break;
|
||||
case AMBIGUITY_BOTH:
|
||||
san += square_to_string(from);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (pos.move_is_capture(m))
|
||||
{
|
||||
if (pt == PAWN)
|
||||
san += file_to_char(square_file(from));
|
||||
|
||||
san += 'x';
|
||||
}
|
||||
san += square_to_string(to);
|
||||
|
||||
if (move_is_promotion(m))
|
||||
{
|
||||
san += '=';
|
||||
san += piece_type_to_char(move_promotion_piece(m));
|
||||
}
|
||||
}
|
||||
|
||||
// The move gives check ? We don't use pos.move_is_check() here
|
||||
// because we need to test for mate after the move is done.
|
||||
StateInfo st;
|
||||
pos.do_move(m, st);
|
||||
if (pos.is_check())
|
||||
san += pos.is_mate() ? "#" : "+";
|
||||
pos.undo_move(m);
|
||||
|
||||
return san;
|
||||
}
|
||||
|
||||
|
||||
/// move_from_san() takes a position and a string as input, and tries to
|
||||
/// interpret the string as a move in short algebraic notation. On success,
|
||||
/// the move is returned. On failure (i.e. if the string is unparsable, or
|
||||
/// if the move is illegal or ambiguous), MOVE_NONE is returned.
|
||||
|
||||
Move move_from_san(const Position& pos, const string& movestr) {
|
||||
|
||||
assert(pos.is_ok());
|
||||
|
||||
enum { START, TO_FILE, TO_RANK, PROMOTION_OR_CHECK, PROMOTION, CHECK, END };
|
||||
static const string pieceLetters = "KQRBN";
|
||||
|
||||
MoveStack mlist[MOVES_MAX], *last;
|
||||
PieceType pt = PIECE_TYPE_NONE, promotion = PIECE_TYPE_NONE;
|
||||
File fromFile = FILE_NONE, toFile = FILE_NONE;
|
||||
Rank fromRank = RANK_NONE, toRank = RANK_NONE;
|
||||
Move move = MOVE_NONE;
|
||||
Square from, to;
|
||||
int matches, state = START;
|
||||
|
||||
// Generate all legal moves for the given position
|
||||
last = generate_moves(pos, mlist);
|
||||
|
||||
// Castling moves
|
||||
if (movestr == "O-O-O" || movestr == "O-O-O+")
|
||||
{
|
||||
for (MoveStack* cur = mlist; cur != last; cur++)
|
||||
if (move_is_long_castle(cur->move))
|
||||
return cur->move;
|
||||
|
||||
return MOVE_NONE;
|
||||
}
|
||||
else if (movestr == "O-O" || movestr == "O-O+")
|
||||
{
|
||||
for (MoveStack* cur = mlist; cur != last; cur++)
|
||||
if (move_is_short_castle(cur->move))
|
||||
return cur->move;
|
||||
|
||||
return MOVE_NONE;
|
||||
}
|
||||
|
||||
// Normal moves. We use a simple FSM to parse the san string
|
||||
for (size_t i = 0; i < movestr.length(); i++)
|
||||
{
|
||||
char type, c = movestr[i];
|
||||
|
||||
if (pieceLetters.find(c) != string::npos)
|
||||
type = 'P';
|
||||
else if (c >= 'a' && c <= 'h')
|
||||
type = 'F';
|
||||
else if (c >= '1' && c <= '8')
|
||||
type = 'R';
|
||||
else
|
||||
type = c;
|
||||
|
||||
switch (type) {
|
||||
case 'P':
|
||||
if (state == START)
|
||||
{
|
||||
pt = piece_type_from_char(c);
|
||||
state = TO_FILE;
|
||||
}
|
||||
else if (state == PROMOTION)
|
||||
{
|
||||
promotion = piece_type_from_char(c);
|
||||
state = (i < movestr.length() - 1 ? CHECK : END);
|
||||
}
|
||||
else
|
||||
return MOVE_NONE;
|
||||
break;
|
||||
case 'F':
|
||||
if (state == START)
|
||||
{
|
||||
pt = PAWN;
|
||||
fromFile = toFile = file_from_char(c);
|
||||
state = TO_RANK;
|
||||
}
|
||||
else if (state == TO_FILE)
|
||||
{
|
||||
toFile = file_from_char(c);
|
||||
state = TO_RANK;
|
||||
}
|
||||
else if (state == TO_RANK && toFile != FILE_NONE)
|
||||
{
|
||||
// Previous file was for disambiguation
|
||||
fromFile = toFile;
|
||||
toFile = file_from_char(c);
|
||||
}
|
||||
else
|
||||
return MOVE_NONE;
|
||||
break;
|
||||
case 'R':
|
||||
if (state == TO_RANK)
|
||||
{
|
||||
toRank = rank_from_char(c);
|
||||
state = (i < movestr.length() - 1) ? PROMOTION_OR_CHECK : END;
|
||||
}
|
||||
else if (state == TO_FILE && fromRank == RANK_NONE)
|
||||
{
|
||||
// It's a disambiguation rank instead of a file
|
||||
fromRank = rank_from_char(c);
|
||||
}
|
||||
else
|
||||
return MOVE_NONE;
|
||||
break;
|
||||
case 'x':
|
||||
case 'X':
|
||||
if (state == TO_RANK)
|
||||
{
|
||||
// Previous file was for disambiguation, or it's a pawn capture
|
||||
fromFile = toFile;
|
||||
state = TO_FILE;
|
||||
}
|
||||
else if (state != TO_FILE)
|
||||
return MOVE_NONE;
|
||||
break;
|
||||
case '=':
|
||||
if (state == PROMOTION_OR_CHECK)
|
||||
state = PROMOTION;
|
||||
else
|
||||
return MOVE_NONE;
|
||||
break;
|
||||
case '+':
|
||||
case '#':
|
||||
if (state == PROMOTION_OR_CHECK || state == CHECK)
|
||||
state = END;
|
||||
else
|
||||
return MOVE_NONE;
|
||||
break;
|
||||
default:
|
||||
return MOVE_NONE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (state != END)
|
||||
return MOVE_NONE;
|
||||
|
||||
// Look for an unambiguous matching move
|
||||
to = make_square(toFile, toRank);
|
||||
matches = 0;
|
||||
|
||||
for (MoveStack* cur = mlist; cur != last; cur++)
|
||||
{
|
||||
from = move_from(cur->move);
|
||||
|
||||
if ( pos.type_of_piece_on(from) == pt
|
||||
&& move_to(cur->move) == to
|
||||
&& move_promotion_piece(cur->move) == promotion
|
||||
&& (fromFile == FILE_NONE || fromFile == square_file(from))
|
||||
&& (fromRank == RANK_NONE || fromRank == square_rank(from)))
|
||||
{
|
||||
move = cur->move;
|
||||
matches++;
|
||||
}
|
||||
}
|
||||
return matches == 1 ? move : MOVE_NONE;
|
||||
}
|
||||
|
||||
|
||||
/// line_to_san() takes a position and a line (an array of moves representing
|
||||
/// a sequence of legal moves from the position) as input, and returns a
|
||||
/// string containing the line in short algebraic notation. If the boolean
|
||||
/// parameter 'breakLines' is true, line breaks are inserted, with a line
|
||||
/// length of 80 characters. After a line break, 'startColumn' spaces are
|
||||
/// inserted at the beginning of the new line.
|
||||
|
||||
const string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines) {
|
||||
|
||||
StateInfo st;
|
||||
std::stringstream s;
|
||||
string moveStr;
|
||||
size_t length = 0;
|
||||
size_t maxLength = 80 - startColumn;
|
||||
Position p(pos, pos.thread());
|
||||
|
||||
for (Move* m = line; *m != MOVE_NONE; m++)
|
||||
{
|
||||
moveStr = move_to_san(p, *m);
|
||||
length += moveStr.length() + 1;
|
||||
if (breakLines && length > maxLength)
|
||||
{
|
||||
s << "\n" << std::setw(startColumn) << " ";
|
||||
length = moveStr.length() + 1;
|
||||
}
|
||||
s << moveStr << ' ';
|
||||
|
||||
if (*m == MOVE_NULL)
|
||||
p.do_null_move(st);
|
||||
else
|
||||
p.do_move(*m, st);
|
||||
}
|
||||
return s.str();
|
||||
}
|
||||
|
||||
|
||||
/// pretty_pv() creates a human-readable string from a position and a PV.
|
||||
/// It is used to write search information to the log file (which is created
|
||||
/// when the UCI parameter "Use Search Log" is "true").
|
||||
|
||||
const string pretty_pv(const Position& pos, int time, int depth,
|
||||
Value score, ValueType type, Move pv[]) {
|
||||
|
||||
const int64_t K = 1000;
|
||||
const int64_t M = 1000000;
|
||||
|
||||
std::stringstream s;
|
||||
|
||||
// Depth
|
||||
s << std::setw(2) << depth << " ";
|
||||
|
||||
// Score
|
||||
s << (type == VALUE_TYPE_LOWER ? ">" : type == VALUE_TYPE_UPPER ? "<" : " ")
|
||||
<< std::setw(7) << score_string(score);
|
||||
|
||||
// Time
|
||||
s << std::setw(8) << time_string(time) << " ";
|
||||
|
||||
// Nodes
|
||||
if (pos.nodes_searched() < M)
|
||||
s << std::setw(8) << pos.nodes_searched() / 1 << " ";
|
||||
else if (pos.nodes_searched() < K * M)
|
||||
s << std::setw(7) << pos.nodes_searched() / K << "K ";
|
||||
else
|
||||
s << std::setw(7) << pos.nodes_searched() / M << "M ";
|
||||
|
||||
// PV
|
||||
s << line_to_san(pos, pv, 30, true);
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
Ambiguity move_ambiguity(const Position& pos, Move m) {
|
||||
|
||||
MoveStack mlist[MOVES_MAX], *last;
|
||||
Move candidates[8];
|
||||
Square from = move_from(m);
|
||||
Square to = move_to(m);
|
||||
Piece pc = pos.piece_on(from);
|
||||
int matches = 0, f = 0, r = 0;
|
||||
|
||||
// If there is only one piece 'pc' then move cannot be ambiguous
|
||||
if (pos.piece_count(pos.side_to_move(), type_of_piece(pc)) == 1)
|
||||
return AMBIGUITY_NONE;
|
||||
|
||||
// Collect all legal moves of piece 'pc' with destination 'to'
|
||||
last = generate_moves(pos, mlist);
|
||||
for (MoveStack* cur = mlist; cur != last; cur++)
|
||||
if (move_to(cur->move) == to && pos.piece_on(move_from(cur->move)) == pc)
|
||||
candidates[matches++] = cur->move;
|
||||
|
||||
if (matches == 1)
|
||||
return AMBIGUITY_NONE;
|
||||
|
||||
for (int i = 0; i < matches; i++)
|
||||
{
|
||||
if (square_file(move_from(candidates[i])) == square_file(from))
|
||||
f++;
|
||||
|
||||
if (square_rank(move_from(candidates[i])) == square_rank(from))
|
||||
r++;
|
||||
}
|
||||
|
||||
return f == 1 ? AMBIGUITY_FILE : r == 1 ? AMBIGUITY_RANK : AMBIGUITY_BOTH;
|
||||
}
|
||||
|
||||
|
||||
const string time_string(int millisecs) {
|
||||
|
||||
const int MSecMinute = 1000 * 60;
|
||||
const int MSecHour = 1000 * 60 * 60;
|
||||
|
||||
std::stringstream s;
|
||||
s << std::setfill('0');
|
||||
|
||||
int hours = millisecs / MSecHour;
|
||||
int minutes = (millisecs - hours * MSecHour) / MSecMinute;
|
||||
int seconds = (millisecs - hours * MSecHour - minutes * MSecMinute) / 1000;
|
||||
|
||||
if (hours)
|
||||
s << hours << ':';
|
||||
|
||||
s << std::setw(2) << minutes << ':' << std::setw(2) << seconds;
|
||||
return s.str();
|
||||
}
|
||||
|
||||
|
||||
const string score_string(Value v) {
|
||||
|
||||
std::stringstream s;
|
||||
|
||||
if (v >= VALUE_MATE - 200)
|
||||
s << "#" << (VALUE_MATE - v + 1) / 2;
|
||||
else if (v <= -VALUE_MATE + 200)
|
||||
s << "-#" << (VALUE_MATE + v) / 2;
|
||||
else
|
||||
{
|
||||
float floatScore = float(v) / float(PawnValueMidgame);
|
||||
if (v >= 0)
|
||||
s << '+';
|
||||
|
||||
s << std::setprecision(2) << std::fixed << floatScore;
|
||||
}
|
||||
return s.str();
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(SAN_H_INCLUDED)
|
||||
#define SAN_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "move.h"
|
||||
#include "position.h"
|
||||
#include "value.h"
|
||||
|
||||
|
||||
////
|
||||
//// Prototypes
|
||||
////
|
||||
|
||||
extern const std::string move_to_san(Position& pos, Move m);
|
||||
extern Move move_from_san(const Position& pos, const std::string& str);
|
||||
extern const std::string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines);
|
||||
extern const std::string pretty_pv(const Position& pos, int time, int depth, Value score, ValueType type, Move pv[]);
|
||||
|
||||
#endif // !defined(SAN_H_INCLUDED)
|
||||
+1310
-2200
File diff suppressed because it is too large
Load Diff
+78
-43
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,61 +17,96 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(SEARCH_H_INCLUDED)
|
||||
#ifndef SEARCH_H_INCLUDED
|
||||
#define SEARCH_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
|
||||
#include "depth.h"
|
||||
#include "move.h"
|
||||
#include "value.h"
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "types.h"
|
||||
|
||||
|
||||
////
|
||||
//// Constants
|
||||
////
|
||||
|
||||
const int PLY_MAX = 100;
|
||||
const int PLY_MAX_PLUS_2 = PLY_MAX + 2;
|
||||
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
/// The SearchStack struct keeps track of the information we need to remember
|
||||
/// from nodes shallower and deeper in the tree during the search. Each
|
||||
/// search thread has its own array of SearchStack objects, indexed by the
|
||||
/// current ply.
|
||||
struct EvalInfo;
|
||||
struct SplitPoint;
|
||||
|
||||
struct SearchStack {
|
||||
namespace Search {
|
||||
|
||||
/// The Stack struct keeps track of the information we need to remember from
|
||||
/// nodes shallower and deeper in the tree during the search. Each search thread
|
||||
/// has its own array of Stack objects, indexed by the current ply.
|
||||
|
||||
struct Stack {
|
||||
SplitPoint* splitPoint;
|
||||
int ply;
|
||||
Move currentMove;
|
||||
Move mateKiller;
|
||||
Move excludedMove;
|
||||
Move bestMove;
|
||||
Move killers[2];
|
||||
Depth reduction;
|
||||
Value eval;
|
||||
Value staticEval;
|
||||
Value evalMargin;
|
||||
bool skipNullMove;
|
||||
SplitPoint* sp;
|
||||
int skipNullMove;
|
||||
int futilityMoveCount;
|
||||
};
|
||||
|
||||
|
||||
////
|
||||
//// Prototypes
|
||||
////
|
||||
/// RootMove struct is used for moves at the root of the tree. For each root
|
||||
/// move we store a score, a node count, and a PV (really a refutation in the
|
||||
/// case of moves which fail low). Score is normally set at -VALUE_INFINITE for
|
||||
/// all non-pv moves.
|
||||
struct RootMove {
|
||||
|
||||
extern void init_search();
|
||||
extern void init_threads();
|
||||
extern void exit_threads();
|
||||
extern int perft(Position& pos, Depth depth);
|
||||
extern bool think(Position& pos, bool infinite, bool ponder, int time[], int increment[],
|
||||
int movesToGo, int maxDepth, int maxNodes, int maxTime, Move searchMoves[]);
|
||||
RootMove(Move m) : score(-VALUE_INFINITE), prevScore(-VALUE_INFINITE) {
|
||||
pv.push_back(m); pv.push_back(MOVE_NONE);
|
||||
}
|
||||
|
||||
#endif // !defined(SEARCH_H_INCLUDED)
|
||||
bool operator<(const RootMove& m) const { return score > m.score; } // Ascending sort
|
||||
bool operator==(const Move& m) const { return pv[0] == m; }
|
||||
|
||||
void extract_pv_from_tt(Position& pos);
|
||||
void insert_pv_in_tt(Position& pos);
|
||||
|
||||
Value score;
|
||||
Value prevScore;
|
||||
std::vector<Move> pv;
|
||||
};
|
||||
|
||||
|
||||
/// 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
|
||||
/// or if we have to ponder while is our opponent's side to move.
|
||||
|
||||
struct LimitsType {
|
||||
|
||||
LimitsType() { std::memset(this, 0, sizeof(LimitsType)); }
|
||||
bool use_time_management() const { return !(mate | movetime | depth | nodes | infinite); }
|
||||
|
||||
int time[COLOR_NB], inc[COLOR_NB], movestogo, depth, nodes, movetime, mate, infinite, ponder;
|
||||
};
|
||||
|
||||
|
||||
/// The SignalsType struct stores volatile flags updated during the search
|
||||
/// typically in an async fashion, for instance to stop the search by the GUI.
|
||||
|
||||
struct SignalsType {
|
||||
bool stopOnPonderhit, firstRootMove, stop, failedLowAtRoot;
|
||||
};
|
||||
|
||||
typedef std::auto_ptr<std::stack<StateInfo> > StateStackPtr;
|
||||
|
||||
extern volatile SignalsType Signals;
|
||||
extern LimitsType Limits;
|
||||
extern std::vector<RootMove> RootMoves;
|
||||
extern Position RootPos;
|
||||
extern Color RootColor;
|
||||
extern Time::point SearchTime;
|
||||
extern StateStackPtr SetupStates;
|
||||
|
||||
extern void init();
|
||||
extern size_t perft(Position& pos, Depth depth);
|
||||
extern void think();
|
||||
|
||||
} // namespace Search
|
||||
|
||||
#endif // #ifndef SEARCH_H_INCLUDED
|
||||
|
||||
-183
@@ -1,183 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(SQUARE_H_INCLUDED)
|
||||
#define SQUARE_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cstdlib> // for abs()
|
||||
#include <string>
|
||||
|
||||
#include "color.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
enum Square {
|
||||
SQ_A1, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1,
|
||||
SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2,
|
||||
SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3,
|
||||
SQ_A4, SQ_B4, SQ_C4, SQ_D4, SQ_E4, SQ_F4, SQ_G4, SQ_H4,
|
||||
SQ_A5, SQ_B5, SQ_C5, SQ_D5, SQ_E5, SQ_F5, SQ_G5, SQ_H5,
|
||||
SQ_A6, SQ_B6, SQ_C6, SQ_D6, SQ_E6, SQ_F6, SQ_G6, SQ_H6,
|
||||
SQ_A7, SQ_B7, SQ_C7, SQ_D7, SQ_E7, SQ_F7, SQ_G7, SQ_H7,
|
||||
SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
|
||||
SQ_NONE
|
||||
};
|
||||
|
||||
enum File {
|
||||
FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_NONE
|
||||
};
|
||||
|
||||
enum Rank {
|
||||
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NONE
|
||||
};
|
||||
|
||||
enum SquareDelta {
|
||||
|
||||
DELTA_N = 8, DELTA_E = 1, DELTA_S = -8, DELTA_W = -1, DELTA_NONE = 0,
|
||||
|
||||
DELTA_NN = DELTA_N + DELTA_N,
|
||||
DELTA_NE = DELTA_N + DELTA_E,
|
||||
DELTA_SE = DELTA_S + DELTA_E,
|
||||
DELTA_SS = DELTA_S + DELTA_S,
|
||||
DELTA_SW = DELTA_S + DELTA_W,
|
||||
DELTA_NW = DELTA_N + DELTA_W
|
||||
};
|
||||
|
||||
ENABLE_OPERATORS_ON(Square);
|
||||
ENABLE_OPERATORS_ON(File);
|
||||
ENABLE_OPERATORS_ON(Rank);
|
||||
ENABLE_OPERATORS_ON(SquareDelta);
|
||||
|
||||
|
||||
////
|
||||
//// Constants
|
||||
////
|
||||
|
||||
const int FlipMask = 56;
|
||||
const int FlopMask = 7;
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
inline Square operator+ (Square x, SquareDelta i) { return x + Square(i); }
|
||||
inline void operator+= (Square& x, SquareDelta i) { x = x + Square(i); }
|
||||
inline Square operator- (Square x, SquareDelta i) { return x - Square(i); }
|
||||
inline void operator-= (Square& x, SquareDelta i) { x = x - Square(i); }
|
||||
|
||||
inline Square make_square(File f, Rank r) {
|
||||
return Square(int(f) | (int(r) << 3));
|
||||
}
|
||||
|
||||
inline File square_file(Square s) {
|
||||
return File(int(s) & 7);
|
||||
}
|
||||
|
||||
inline Rank square_rank(Square s) {
|
||||
return Rank(int(s) >> 3);
|
||||
}
|
||||
|
||||
inline Square flip_square(Square s) {
|
||||
return Square(int(s) ^ FlipMask);
|
||||
}
|
||||
|
||||
inline Square flop_square(Square s) {
|
||||
return Square(int(s) ^ FlopMask);
|
||||
}
|
||||
|
||||
inline Square relative_square(Color c, Square s) {
|
||||
return Square(int(s) ^ (int(c) * FlipMask));
|
||||
}
|
||||
|
||||
inline Rank relative_rank(Color c, Square s) {
|
||||
return square_rank(relative_square(c, s));
|
||||
}
|
||||
|
||||
inline SquareColor square_color(Square s) {
|
||||
return SquareColor((int(square_file(s)) + int(square_rank(s))) & 1);
|
||||
}
|
||||
|
||||
inline bool same_color_squares(Square s1, Square s2) {
|
||||
int s = int(s1) ^ int(s2);
|
||||
return (((s >> 3) ^ s) & 1) == 0;
|
||||
}
|
||||
|
||||
inline int file_distance(File f1, File f2) {
|
||||
return abs(int(f1) - int(f2));
|
||||
}
|
||||
|
||||
inline int file_distance(Square s1, Square s2) {
|
||||
return file_distance(square_file(s1), square_file(s2));
|
||||
}
|
||||
|
||||
inline int rank_distance(Rank r1, Rank r2) {
|
||||
return abs(int(r1) - int(r2));
|
||||
}
|
||||
|
||||
inline int rank_distance(Square s1, Square s2) {
|
||||
return rank_distance(square_rank(s1), square_rank(s2));
|
||||
}
|
||||
|
||||
inline int square_distance(Square s1, Square s2) {
|
||||
return Max(file_distance(s1, s2), rank_distance(s1, s2));
|
||||
}
|
||||
|
||||
inline File file_from_char(char c) {
|
||||
return File(c - 'a') + FILE_A;
|
||||
}
|
||||
|
||||
inline char file_to_char(File f) {
|
||||
return char(f - FILE_A + int('a'));
|
||||
}
|
||||
|
||||
inline Rank rank_from_char(char c) {
|
||||
return Rank(c - '1') + RANK_1;
|
||||
}
|
||||
|
||||
inline char rank_to_char(Rank r) {
|
||||
return char(r - RANK_1 + int('1'));
|
||||
}
|
||||
|
||||
inline const std::string square_to_string(Square s) {
|
||||
return std::string(1, file_to_char(square_file(s)))
|
||||
+ std::string(1, rank_to_char(square_rank(s)));
|
||||
}
|
||||
|
||||
inline bool file_is_ok(File f) {
|
||||
return f >= FILE_A && f <= FILE_H;
|
||||
}
|
||||
|
||||
inline bool rank_is_ok(Rank r) {
|
||||
return r >= RANK_1 && r <= RANK_8;
|
||||
}
|
||||
|
||||
inline bool square_is_ok(Square s) {
|
||||
return file_is_ok(square_file(s)) && rank_is_ok(square_rank(s));
|
||||
}
|
||||
|
||||
#endif // !defined(SQUARE_H_INCLUDED)
|
||||
+394
@@ -0,0 +1,394 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2013 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 <algorithm> // For std::count
|
||||
#include <cassert>
|
||||
|
||||
#include "movegen.h"
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
#include "ucioption.h"
|
||||
|
||||
using namespace Search;
|
||||
|
||||
ThreadPool Threads; // Global object
|
||||
|
||||
namespace {
|
||||
|
||||
// start_routine() is the C function which is called when a new thread
|
||||
// is launched. It is a wrapper to the virtual function idle_loop().
|
||||
|
||||
extern "C" { long start_routine(ThreadBase* th) { th->idle_loop(); return 0; } }
|
||||
|
||||
|
||||
// Helpers to launch a thread after creation and joining before delete. Must be
|
||||
// outside Thread c'tor and d'tor because object shall be fully initialized
|
||||
// when start_routine (and hence virtual idle_loop) is called and when joining.
|
||||
|
||||
template<typename T> T* new_thread() {
|
||||
T* th = new T();
|
||||
thread_create(th->handle, start_routine, th); // Will go to sleep
|
||||
return th;
|
||||
}
|
||||
|
||||
void delete_thread(ThreadBase* th) {
|
||||
th->exit = true; // Search must be already finished
|
||||
th->notify_one();
|
||||
thread_join(th->handle); // Wait for thread termination
|
||||
delete th;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// ThreadBase::notify_one() wakes up the thread when there is some work to do
|
||||
|
||||
void ThreadBase::notify_one() {
|
||||
|
||||
mutex.lock();
|
||||
sleepCondition.notify_one();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
// ThreadBase::wait_for() set the thread to sleep until condition 'b' turns true
|
||||
|
||||
void ThreadBase::wait_for(volatile const bool& b) {
|
||||
|
||||
mutex.lock();
|
||||
while (!b) sleepCondition.wait(mutex);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
// Thread c'tor just inits data but does not launch any thread of execution that
|
||||
// instead will be started only upon c'tor returns.
|
||||
|
||||
Thread::Thread() /* : splitPoints() */ { // Value-initialization bug in MSVC
|
||||
|
||||
searching = false;
|
||||
maxPly = splitPointsSize = 0;
|
||||
activeSplitPoint = NULL;
|
||||
activePosition = NULL;
|
||||
idx = Threads.size();
|
||||
}
|
||||
|
||||
|
||||
// TimerThread::idle_loop() is where the timer thread waits msec milliseconds
|
||||
// and then calls check_time(). If msec is 0 thread sleeps until is woken up.
|
||||
extern void check_time();
|
||||
|
||||
void TimerThread::idle_loop() {
|
||||
|
||||
while (!exit)
|
||||
{
|
||||
mutex.lock();
|
||||
|
||||
if (!exit)
|
||||
sleepCondition.wait_for(mutex, msec ? msec : INT_MAX);
|
||||
|
||||
mutex.unlock();
|
||||
|
||||
if (msec)
|
||||
check_time();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MainThread::idle_loop() is where the main thread is parked waiting to be started
|
||||
// when there is a new search. 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 UI thread if needed
|
||||
sleepCondition.wait(mutex);
|
||||
}
|
||||
|
||||
mutex.unlock();
|
||||
|
||||
if (exit)
|
||||
return;
|
||||
|
||||
searching = true;
|
||||
|
||||
Search::think();
|
||||
|
||||
assert(searching);
|
||||
|
||||
searching = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Thread::cutoff_occurred() checks whether a beta cutoff has occurred in the
|
||||
// current active split point, or in some ancestor of the split point.
|
||||
|
||||
bool Thread::cutoff_occurred() const {
|
||||
|
||||
for (SplitPoint* sp = activeSplitPoint; sp; sp = sp->parentSplitPoint)
|
||||
if (sp->cutoff)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Thread::is_available_to() checks whether the thread is available to help the
|
||||
// thread 'master' at a split point. An obvious requirement is that thread must
|
||||
// be idle. With more than two threads, this is not sufficient: If the thread is
|
||||
// the master of some split point, it is only available as a slave to the slaves
|
||||
// which are busy searching the split point at the top of slaves split point
|
||||
// stack (the "helpful master concept" in YBWC terminology).
|
||||
|
||||
bool Thread::is_available_to(const Thread* master) const {
|
||||
|
||||
if (searching)
|
||||
return false;
|
||||
|
||||
// Make a local copy to be sure doesn't become zero under our feet while
|
||||
// testing next condition and so leading to an out of bound 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 & (1ULL << master->idx));
|
||||
}
|
||||
|
||||
|
||||
// init() is called at startup to create and launch requested threads, that will
|
||||
// go immediately to sleep due to 'sleepWhileIdle' set to true. We cannot use
|
||||
// a c'tor becuase Threads is a static object and we need a fully initialized
|
||||
// engine at this point due to allocation of Endgames in Thread c'tor.
|
||||
|
||||
void ThreadPool::init() {
|
||||
|
||||
sleepWhileIdle = true;
|
||||
timer = new_thread<TimerThread>();
|
||||
push_back(new_thread<MainThread>());
|
||||
read_uci_options();
|
||||
}
|
||||
|
||||
|
||||
// exit() cleanly terminates the threads before the program exits
|
||||
|
||||
void ThreadPool::exit() {
|
||||
|
||||
delete_thread(timer); // As first because check_time() accesses threads data
|
||||
|
||||
for (iterator it = begin(); it != end(); ++it)
|
||||
delete_thread(*it);
|
||||
}
|
||||
|
||||
|
||||
// read_uci_options() updates internal threads parameters from the corresponding
|
||||
// UCI options and creates/destroys threads to match the requested number. Thread
|
||||
// objects are dynamically allocated to avoid creating in advance all possible
|
||||
// threads, with included pawns and material tables, if only few are used.
|
||||
|
||||
void ThreadPool::read_uci_options() {
|
||||
|
||||
maxThreadsPerSplitPoint = Options["Max Threads per Split Point"];
|
||||
minimumSplitDepth = Options["Min Split Depth"] * ONE_PLY;
|
||||
size_t requested = Options["Threads"];
|
||||
|
||||
assert(requested > 0);
|
||||
|
||||
// Value 0 has a special meaning: We determine the optimal minimum split depth
|
||||
// automatically. Anyhow the minimumSplitDepth should never be under 4 plies.
|
||||
if (!minimumSplitDepth)
|
||||
minimumSplitDepth = (requested < 8 ? 4 : 7) * ONE_PLY;
|
||||
else
|
||||
minimumSplitDepth = std::max(4 * ONE_PLY, minimumSplitDepth);
|
||||
|
||||
while (size() < requested)
|
||||
push_back(new_thread<Thread>());
|
||||
|
||||
while (size() > requested)
|
||||
{
|
||||
delete_thread(back());
|
||||
pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// slave_available() tries to find an idle thread which is available as a slave
|
||||
// for the thread 'master'.
|
||||
|
||||
Thread* ThreadPool::available_slave(const Thread* master) const {
|
||||
|
||||
for (const_iterator it = begin(); it != end(); ++it)
|
||||
if ((*it)->is_available_to(master))
|
||||
return *it;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
// split() does the actual work of distributing the work at a node between
|
||||
// several available threads. If it does not succeed in splitting the node
|
||||
// (because no idle threads are available), the function immediately returns.
|
||||
// If splitting is possible, a SplitPoint object is initialized with all the
|
||||
// data that must be copied to the helper threads and then helper threads are
|
||||
// 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>
|
||||
void Thread::split(Position& pos, const Stack* ss, Value alpha, Value beta, Value* bestValue,
|
||||
Move* bestMove, Depth depth, Move threatMove, int moveCount,
|
||||
MovePicker* movePicker, int nodeType, bool cutNode) {
|
||||
|
||||
assert(pos.pos_is_ok());
|
||||
assert(*bestValue <= alpha && alpha < beta && beta <= VALUE_INFINITE);
|
||||
assert(*bestValue > -VALUE_INFINITE);
|
||||
assert(depth >= Threads.minimumSplitDepth);
|
||||
assert(searching);
|
||||
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 = 1ULL << idx;
|
||||
sp.depth = depth;
|
||||
sp.bestValue = *bestValue;
|
||||
sp.bestMove = *bestMove;
|
||||
sp.threatMove = threatMove;
|
||||
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();
|
||||
|
||||
splitPointsSize++;
|
||||
activeSplitPoint = &sp;
|
||||
activePosition = NULL;
|
||||
|
||||
size_t slavesCnt = 1; // This thread is always included
|
||||
Thread* slave;
|
||||
|
||||
while ( (slave = Threads.available_slave(this)) != NULL
|
||||
&& ++slavesCnt <= Threads.maxThreadsPerSplitPoint && !Fake)
|
||||
{
|
||||
sp.slavesMask |= 1ULL << 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.
|
||||
if (slavesCnt > 1 || Fake)
|
||||
{
|
||||
sp.mutex.unlock();
|
||||
Threads.mutex.unlock();
|
||||
|
||||
Thread::idle_loop(); // Force a call to base class idle_loop()
|
||||
|
||||
// In helpful master concept a master can help only a sub-tree of its split
|
||||
// point, and because here is all finished is not possible master is 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::is_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, Move, int, MovePicker*, int, bool);
|
||||
template void Thread::split< true>(Position&, const Stack*, Value, Value, Value*, Move*, Depth, Move, int, MovePicker*, int, bool);
|
||||
|
||||
|
||||
// wait_for_think_finished() waits for main thread to go to sleep then returns
|
||||
|
||||
void ThreadPool::wait_for_think_finished() {
|
||||
|
||||
MainThread* t = main();
|
||||
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()
|
||||
// so to start a new search, then returns immediately.
|
||||
|
||||
void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits,
|
||||
const std::vector<Move>& searchMoves, StateStackPtr& states) {
|
||||
wait_for_think_finished();
|
||||
|
||||
SearchTime = Time::now(); // As early as possible
|
||||
|
||||
Signals.stopOnPonderhit = Signals.firstRootMove = false;
|
||||
Signals.stop = Signals.failedLowAtRoot = false;
|
||||
|
||||
RootMoves.clear();
|
||||
RootPos = pos;
|
||||
Limits = limits;
|
||||
if (states.get()) // If we don't set a new position, preserve current state
|
||||
{
|
||||
SetupStates = states; // Ownership transfer here
|
||||
assert(!states.get());
|
||||
}
|
||||
|
||||
for (MoveList<LEGAL> it(pos); *it; ++it)
|
||||
if ( searchMoves.empty()
|
||||
|| std::count(searchMoves.begin(), searchMoves.end(), *it))
|
||||
RootMoves.push_back(RootMove(*it));
|
||||
|
||||
main()->thinking = true;
|
||||
main()->notify_one(); // Starts main thread
|
||||
}
|
||||
+131
-49
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,80 +17,162 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(THREAD_H_INCLUDED)
|
||||
#ifndef THREAD_H_INCLUDED
|
||||
#define THREAD_H_INCLUDED
|
||||
|
||||
#include <vector>
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "lock.h"
|
||||
#include "material.h"
|
||||
#include "movepick.h"
|
||||
#include "pawns.h"
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
|
||||
const int MAX_THREADS = 64; // Because SplitPoint::slavesMask is a uint64_t
|
||||
const int MAX_SPLITPOINTS_PER_THREAD = 8;
|
||||
|
||||
////
|
||||
//// Constants and variables
|
||||
////
|
||||
struct Mutex {
|
||||
Mutex() { lock_init(l); }
|
||||
~Mutex() { lock_destroy(l); }
|
||||
|
||||
const int MAX_THREADS = 16;
|
||||
const int MAX_ACTIVE_SPLIT_POINTS = 8;
|
||||
void lock() { lock_grab(l); }
|
||||
void unlock() { lock_release(l); }
|
||||
|
||||
private:
|
||||
friend struct ConditionVariable;
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
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 splitPoint has been setup
|
||||
SplitPoint* parent;
|
||||
// Const data after split point has been setup
|
||||
const Position* pos;
|
||||
const Search::Stack* ss;
|
||||
Thread* masterThread;
|
||||
Depth depth;
|
||||
bool pvNode, mateThreat;
|
||||
Value beta;
|
||||
int ply;
|
||||
int master;
|
||||
int nodeType;
|
||||
Move threatMove;
|
||||
SearchStack sstack[MAX_THREADS][PLY_MAX_PLUS_2];
|
||||
bool cutNode;
|
||||
|
||||
// Const pointers to shared data
|
||||
MovePicker* mp;
|
||||
SearchStack* parentSstack;
|
||||
MovePicker* movePicker;
|
||||
SplitPoint* parentSplitPoint;
|
||||
|
||||
// Shared data
|
||||
Lock lock;
|
||||
Mutex mutex;
|
||||
volatile uint64_t slavesMask;
|
||||
volatile int64_t nodes;
|
||||
volatile Value alpha;
|
||||
volatile Value bestValue;
|
||||
volatile Move bestMove;
|
||||
volatile int moveCount;
|
||||
volatile bool betaCutoff;
|
||||
volatile int slaves[MAX_THREADS];
|
||||
};
|
||||
|
||||
// ThreadState type is used to represent thread's current state
|
||||
|
||||
enum ThreadState
|
||||
{
|
||||
THREAD_INITIALIZING, // thread is initializing itself
|
||||
THREAD_SEARCHING, // thread is performing work
|
||||
THREAD_AVAILABLE, // thread is waiting for work
|
||||
THREAD_BOOKED, // other thread (master) has booked us as a slave
|
||||
THREAD_WORKISWAITING, // master has ordered us to start
|
||||
THREAD_TERMINATED // we are quitting and thread is terminated
|
||||
};
|
||||
|
||||
struct Thread {
|
||||
volatile ThreadState state;
|
||||
SplitPoint* volatile splitPoint;
|
||||
volatile int activeSplitPoints;
|
||||
SplitPoint splitPoints[MAX_ACTIVE_SPLIT_POINTS];
|
||||
volatile bool cutoff;
|
||||
};
|
||||
|
||||
|
||||
#endif // !defined(THREAD_H_INCLUDED)
|
||||
/// ThreadBase struct is the base of the hierarchy from where we derive all the
|
||||
/// specialized thread classes.
|
||||
|
||||
struct ThreadBase {
|
||||
|
||||
ThreadBase() : exit(false) {}
|
||||
virtual ~ThreadBase() {}
|
||||
virtual void idle_loop() = 0;
|
||||
void notify_one();
|
||||
void wait_for(volatile const bool& b);
|
||||
|
||||
Mutex mutex;
|
||||
ConditionVariable sleepCondition;
|
||||
NativeHandle handle;
|
||||
volatile bool exit;
|
||||
};
|
||||
|
||||
|
||||
/// Thread struct keeps together all the thread related stuff like locks, state
|
||||
/// and especially split points. We also use per-thread pawn and material hash
|
||||
/// tables so that once we get a pointer to an entry its life time is unlimited
|
||||
/// and we don't have to care about someone changing the entry under our feet.
|
||||
|
||||
struct Thread : public ThreadBase {
|
||||
|
||||
Thread();
|
||||
virtual void idle_loop();
|
||||
bool cutoff_occurred() const;
|
||||
bool is_available_to(const Thread* master) const;
|
||||
|
||||
template <bool Fake>
|
||||
void split(Position& pos, const Search::Stack* ss, Value alpha, Value beta, Value* bestValue, Move* bestMove,
|
||||
Depth depth, Move threatMove, int moveCount, MovePicker* movePicker, int nodeType, bool cutNode);
|
||||
|
||||
SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD];
|
||||
Material::Table materialTable;
|
||||
Endgames endgames;
|
||||
Pawns::Table pawnsTable;
|
||||
Position* activePosition;
|
||||
size_t idx;
|
||||
int maxPly;
|
||||
SplitPoint* volatile activeSplitPoint;
|
||||
volatile int splitPointsSize;
|
||||
volatile bool searching;
|
||||
};
|
||||
|
||||
|
||||
/// MainThread and TimerThread are derived classes used to characterize the two
|
||||
/// special threads: the main one and the recurring timer.
|
||||
|
||||
struct MainThread : public Thread {
|
||||
MainThread() : thinking(true) {} // Avoid a race with start_thinking()
|
||||
virtual void idle_loop();
|
||||
volatile bool thinking;
|
||||
};
|
||||
|
||||
struct TimerThread : public ThreadBase {
|
||||
TimerThread() : msec(0) {}
|
||||
virtual void idle_loop();
|
||||
int msec;
|
||||
};
|
||||
|
||||
|
||||
/// ThreadPool struct handles all the threads related stuff like init, starting,
|
||||
/// parking and, the most important, launching a slave thread at a split point.
|
||||
/// All the access to shared thread data is done through this class.
|
||||
|
||||
struct ThreadPool : public std::vector<Thread*> {
|
||||
|
||||
void init(); // No c'tor and d'tor, threads rely on globals that should
|
||||
void exit(); // be initialized and valid during the whole thread lifetime.
|
||||
|
||||
MainThread* main() { return static_cast<MainThread*>((*this)[0]); }
|
||||
void read_uci_options();
|
||||
Thread* available_slave(const Thread* master) const;
|
||||
void wait_for_think_finished();
|
||||
void start_thinking(const Position&, const Search::LimitsType&,
|
||||
const std::vector<Move>&, Search::StateStackPtr&);
|
||||
|
||||
bool sleepWhileIdle;
|
||||
Depth minimumSplitDepth;
|
||||
size_t maxThreadsPerSplitPoint;
|
||||
Mutex mutex;
|
||||
ConditionVariable sleepCondition;
|
||||
TimerThread* timer;
|
||||
};
|
||||
|
||||
extern ThreadPool Threads;
|
||||
|
||||
#endif // #ifndef THREAD_H_INCLUDED
|
||||
|
||||
+40
-48
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,27 +17,19 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
#include "misc.h"
|
||||
#include "search.h"
|
||||
#include "timeman.h"
|
||||
#include "ucioption.h"
|
||||
|
||||
////
|
||||
//// Local definitions
|
||||
////
|
||||
|
||||
namespace {
|
||||
|
||||
/// Constants
|
||||
|
||||
const int MoveHorizon = 50; // Plan time management at most this many moves ahead
|
||||
const float MaxRatio = 3.0f; // When in trouble, we can step over reserved time with this ratio
|
||||
const float MaxRatio = 7.0f; // When in trouble, we can step over reserved time with this ratio
|
||||
const float StealRatio = 0.33f; // However we must not steal time from remaining moves over this ratio
|
||||
|
||||
|
||||
@@ -72,7 +64,7 @@ namespace {
|
||||
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 2, 2, 2, 2,
|
||||
2, 1, 1, 1, 1, 1, 1, 1 };
|
||||
|
||||
int move_importance(int ply) { return MoveImportance[Min(ply, 511)]; }
|
||||
int move_importance(int ply) { return MoveImportance[std::min(ply, 511)]; }
|
||||
|
||||
|
||||
/// Function Prototypes
|
||||
@@ -80,83 +72,83 @@ namespace {
|
||||
enum TimeType { OptimumTime, MaxTime };
|
||||
|
||||
template<TimeType>
|
||||
int remaining(int myTime, int movesToGo, int currentPly);
|
||||
int remaining(int myTime, int movesToGo, int fullMoveNumber, int slowMover);
|
||||
}
|
||||
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
void TimeManager::pv_instability(int curChanges, int prevChanges) {
|
||||
|
||||
unstablePVExtraTime = curChanges * (optimumSearchTime / 2)
|
||||
+ prevChanges * (optimumSearchTime / 3);
|
||||
unstablePVExtraTime = curChanges * (optimumSearchTime / 2)
|
||||
+ prevChanges * (optimumSearchTime / 3);
|
||||
}
|
||||
|
||||
void TimeManager::init(int myTime, int myInc, int movesToGo, int currentPly)
|
||||
|
||||
void TimeManager::init(const Search::LimitsType& limits, int currentPly, Color us)
|
||||
{
|
||||
/* We support four different kind of time controls:
|
||||
|
||||
Inc == 0 && movesToGo == 0 means: x basetime [sudden death!]
|
||||
Inc == 0 && movesToGo != 0 means: (x moves) / (y minutes)
|
||||
Inc > 0 && movesToGo == 0 means: x basetime + z inc.
|
||||
Inc > 0 && movesToGo != 0 means: (x moves) / (y minutes) + z inc
|
||||
increment == 0 && movesToGo == 0 means: x basetime [sudden death!]
|
||||
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:
|
||||
|
||||
emergencyMoveHorizon :Be prepared to always play at least this many moves
|
||||
emergencyBaseTime :Always attempt to keep at least this much time (in ms) at clock
|
||||
emergencyMoveTime :Plus attempt to keep at least this much time for each remaining emergency move
|
||||
minThinkingTime :No matter what, use at least this much thinking before doing the move
|
||||
emergencyMoveHorizon: Be prepared to always play at least this many moves
|
||||
emergencyBaseTime : Always attempt to keep at least this much time (in ms) at clock
|
||||
emergencyMoveTime : Plus attempt to keep at least this much time for each remaining emergency move
|
||||
minThinkingTime : No matter what, use at least this much thinking before doing the move
|
||||
*/
|
||||
|
||||
int hypMTG, hypMyTime, t1, t2;
|
||||
|
||||
// Read uci parameters
|
||||
int emergencyMoveHorizon = Options["Emergency Move Horizon"].value<int>();
|
||||
int emergencyBaseTime = Options["Emergency Base Time"].value<int>();
|
||||
int emergencyMoveTime = Options["Emergency Move Time"].value<int>();
|
||||
int minThinkingTime = Options["Minimum Thinking Time"].value<int>();
|
||||
int emergencyMoveHorizon = Options["Emergency Move Horizon"];
|
||||
int emergencyBaseTime = Options["Emergency Base Time"];
|
||||
int emergencyMoveTime = Options["Emergency Move Time"];
|
||||
int minThinkingTime = Options["Minimum Thinking Time"];
|
||||
int slowMover = Options["Slow Mover"];
|
||||
|
||||
// Initialize to maximum values but unstablePVExtraTime that is reset
|
||||
unstablePVExtraTime = 0;
|
||||
optimumSearchTime = maximumSearchTime = myTime;
|
||||
optimumSearchTime = maximumSearchTime = limits.time[us];
|
||||
|
||||
// We calculate optimum time usage for different hypothetic "moves to go"-values and choose the
|
||||
// minimum of calculated search time values. Usually the greatest hypMTG gives the minimum values.
|
||||
for (hypMTG = 1; hypMTG <= (movesToGo ? Min(movesToGo, MoveHorizon) : MoveHorizon); hypMTG++)
|
||||
for (hypMTG = 1; hypMTG <= (limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon); hypMTG++)
|
||||
{
|
||||
// Calculate thinking time for hypothetic "moves to go"-value
|
||||
hypMyTime = Max(myTime + (hypMTG - 1) * myInc - emergencyBaseTime - Min(hypMTG, emergencyMoveHorizon) * emergencyMoveTime, 0);
|
||||
hypMyTime = limits.time[us]
|
||||
+ limits.inc[us] * (hypMTG - 1)
|
||||
- emergencyBaseTime
|
||||
- emergencyMoveTime * std::min(hypMTG, emergencyMoveHorizon);
|
||||
|
||||
t1 = minThinkingTime + remaining<OptimumTime>(hypMyTime, hypMTG, currentPly);
|
||||
t2 = minThinkingTime + remaining<MaxTime>(hypMyTime, hypMTG, currentPly);
|
||||
hypMyTime = std::max(hypMyTime, 0);
|
||||
|
||||
optimumSearchTime = Min(optimumSearchTime, t1);
|
||||
maximumSearchTime = Min(maximumSearchTime, t2);
|
||||
t1 = minThinkingTime + remaining<OptimumTime>(hypMyTime, hypMTG, currentPly, slowMover);
|
||||
t2 = minThinkingTime + remaining<MaxTime>(hypMyTime, hypMTG, currentPly, slowMover);
|
||||
|
||||
optimumSearchTime = std::min(optimumSearchTime, t1);
|
||||
maximumSearchTime = std::min(maximumSearchTime, t2);
|
||||
}
|
||||
|
||||
if (Options["Ponder"].value<bool>())
|
||||
if (Options["Ponder"])
|
||||
optimumSearchTime += optimumSearchTime / 4;
|
||||
|
||||
// Make sure that maxSearchTime is not over absoluteMaxSearchTime
|
||||
optimumSearchTime = Min(optimumSearchTime, maximumSearchTime);
|
||||
optimumSearchTime = std::min(optimumSearchTime, maximumSearchTime);
|
||||
}
|
||||
|
||||
////
|
||||
//// Local functions
|
||||
////
|
||||
|
||||
namespace {
|
||||
|
||||
template<TimeType T>
|
||||
int remaining(int myTime, int movesToGo, int currentPly)
|
||||
int remaining(int myTime, int movesToGo, int currentPly, int slowMover)
|
||||
{
|
||||
const float TMaxRatio = (T == OptimumTime ? 1 : MaxRatio);
|
||||
const float TStealRatio = (T == OptimumTime ? 0 : StealRatio);
|
||||
|
||||
int thisMoveImportance = move_importance(currentPly);
|
||||
int thisMoveImportance = move_importance(currentPly) * slowMover / 100;
|
||||
int otherMovesImportance = 0;
|
||||
|
||||
for (int i = 1; i < movesToGo; i++)
|
||||
@@ -165,6 +157,6 @@ namespace {
|
||||
float ratio1 = (TMaxRatio * thisMoveImportance) / float(TMaxRatio * thisMoveImportance + otherMovesImportance);
|
||||
float ratio2 = (thisMoveImportance + TStealRatio * otherMovesImportance) / float(thisMoveImportance + otherMovesImportance);
|
||||
|
||||
return int(floor(myTime * Min(ratio1, ratio2)));
|
||||
return int(floor(myTime * std::min(ratio1, ratio2)));
|
||||
}
|
||||
}
|
||||
|
||||
+6
-9
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,18 +17,15 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(TIMEMAN_H_INCLUDED)
|
||||
#ifndef TIMEMAN_H_INCLUDED
|
||||
#define TIMEMAN_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Prototypes
|
||||
////
|
||||
/// The TimeManager class computes the optimal time to think depending on the
|
||||
/// maximum available time, the move game number and other parameters.
|
||||
|
||||
class TimeManager {
|
||||
public:
|
||||
|
||||
void init(int myTime, int myInc, int movesToGo, int currentPly);
|
||||
void init(const Search::LimitsType& limits, int currentPly, Color us);
|
||||
void pv_instability(int curChanges, int prevChanges);
|
||||
int available_time() const { return optimumSearchTime + unstablePVExtraTime; }
|
||||
int maximum_time() const { return maximumSearchTime; }
|
||||
@@ -39,4 +36,4 @@ private:
|
||||
int unstablePVExtraTime;
|
||||
};
|
||||
|
||||
#endif // !defined(TIMEMAN_H_INCLUDED)
|
||||
#endif // #ifndef TIMEMAN_H_INCLUDED
|
||||
|
||||
+68
-105
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,142 +17,105 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "tt.h"
|
||||
|
||||
// The main transposition table
|
||||
TranspositionTable TT;
|
||||
|
||||
////
|
||||
//// Functions
|
||||
////
|
||||
|
||||
TranspositionTable::TranspositionTable() {
|
||||
|
||||
size = 0;
|
||||
entries = 0;
|
||||
generation = 0;
|
||||
}
|
||||
|
||||
TranspositionTable::~TranspositionTable() {
|
||||
|
||||
delete [] entries;
|
||||
}
|
||||
TranspositionTable TT; // Our global transposition table
|
||||
|
||||
|
||||
/// TranspositionTable::set_size sets the size of the transposition table,
|
||||
/// measured in megabytes.
|
||||
/// TranspositionTable::set_size() sets the size of the transposition table,
|
||||
/// measured in megabytes. Transposition table consists of a power of 2 number
|
||||
/// of clusters and each cluster consists of ClusterSize number of TTEntry.
|
||||
|
||||
void TranspositionTable::set_size(size_t mbSize) {
|
||||
|
||||
size_t newSize = 1024;
|
||||
assert(msb((mbSize << 20) / sizeof(TTEntry)) < 32);
|
||||
|
||||
// Transposition table consists of clusters and
|
||||
// each cluster consists of ClusterSize number of TTEntries.
|
||||
// Each non-empty entry contains information of exactly one position.
|
||||
// newSize is the number of clusters we are going to allocate.
|
||||
while ((2 * newSize) * sizeof(TTCluster) <= (mbSize << 20))
|
||||
newSize *= 2;
|
||||
uint32_t size = ClusterSize << msb((mbSize << 20) / sizeof(TTEntry[ClusterSize]));
|
||||
|
||||
if (newSize != size)
|
||||
if (hashMask == size - ClusterSize)
|
||||
return;
|
||||
|
||||
hashMask = size - ClusterSize;
|
||||
free(mem);
|
||||
mem = calloc(size * sizeof(TTEntry) + CACHE_LINE_SIZE - 1, 1);
|
||||
|
||||
if (!mem)
|
||||
{
|
||||
size = newSize;
|
||||
delete [] entries;
|
||||
entries = new TTCluster[size];
|
||||
if (!entries)
|
||||
{
|
||||
std::cerr << "Failed to allocate " << mbSize
|
||||
<< " MB for transposition table." << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
clear();
|
||||
std::cerr << "Failed to allocate " << mbSize
|
||||
<< "MB for transposition table." << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
table = (TTEntry*)((uintptr_t(mem) + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 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
|
||||
/// user asks the program to clear the table (from the UCI interface).
|
||||
/// Perhaps we should also clear it when the "ucinewgame" command is received?
|
||||
|
||||
void TranspositionTable::clear() {
|
||||
|
||||
memset(entries, 0, size * sizeof(TTCluster));
|
||||
std::memset(table, 0, (hashMask + ClusterSize) * sizeof(TTEntry));
|
||||
}
|
||||
|
||||
|
||||
/// TranspositionTable::store writes a new entry containing position key and
|
||||
/// valuable information of current position.
|
||||
/// The Lowest order bits of position key are used to decide on which cluster
|
||||
/// the position will be placed.
|
||||
/// When a new entry is written and there are no empty entries available in cluster,
|
||||
/// it replaces the least valuable of 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.
|
||||
/// TranspositionTable::probe() looks up the current position in the
|
||||
/// transposition table. Returns a pointer to the TTEntry or NULL if
|
||||
/// position is not found.
|
||||
|
||||
void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m, Value statV, Value kingD) {
|
||||
const TTEntry* TranspositionTable::probe(const Key key) const {
|
||||
|
||||
int c1, c2, c3;
|
||||
TTEntry *tte, *replace;
|
||||
uint32_t posKey32 = posKey >> 32; // Use the high 32 bits as key
|
||||
const TTEntry* tte = first_entry(key);
|
||||
uint32_t key32 = key >> 32;
|
||||
|
||||
tte = replace = first_entry(posKey);
|
||||
for (int i = 0; i < ClusterSize; i++, tte++)
|
||||
{
|
||||
if (!tte->key() || tte->key() == posKey32) // empty or overwrite old
|
||||
{
|
||||
// Preserve any existing ttMove
|
||||
if (m == MOVE_NONE)
|
||||
m = tte->move();
|
||||
|
||||
tte->save(posKey32, v, t, d, m, generation, statV, kingD);
|
||||
return;
|
||||
}
|
||||
|
||||
if (i == 0) // Replacing first entry is default and already set before entering for-loop
|
||||
continue;
|
||||
|
||||
c1 = (replace->generation() == generation ? 2 : 0);
|
||||
c2 = (tte->generation() == generation ? -2 : 0);
|
||||
c3 = (tte->depth() < replace->depth() ? 1 : 0);
|
||||
|
||||
if (c1 + c2 + c3 > 0)
|
||||
replace = tte;
|
||||
}
|
||||
replace->save(posKey32, v, t, d, m, generation, statV, kingD);
|
||||
}
|
||||
|
||||
|
||||
/// TranspositionTable::retrieve looks up the current position in the
|
||||
/// transposition table. Returns a pointer to the TTEntry or NULL
|
||||
/// if position is not found.
|
||||
|
||||
TTEntry* TranspositionTable::retrieve(const Key posKey) const {
|
||||
|
||||
uint32_t posKey32 = posKey >> 32;
|
||||
TTEntry* tte = first_entry(posKey);
|
||||
|
||||
for (int i = 0; i < ClusterSize; i++, tte++)
|
||||
if (tte->key() == posKey32)
|
||||
for (unsigned i = 0; i < ClusterSize; i++, tte++)
|
||||
if (tte->key() == key32)
|
||||
return tte;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/// TranspositionTable::new_search() is called at the beginning of every new
|
||||
/// search. It increments the "generation" variable, which is used to
|
||||
/// distinguish transposition table entries from previous searches from
|
||||
/// entries from the current search.
|
||||
/// TranspositionTable::store() writes a new entry containing position key and
|
||||
/// valuable information of current position. The lowest order bits of position
|
||||
/// key are used to decide on which cluster the position will be placed.
|
||||
/// When a new entry is written and there are no empty entries available in cluster,
|
||||
/// it replaces the least valuable of 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::new_search() {
|
||||
generation++;
|
||||
void TranspositionTable::store(const Key key, Value v, Bound b, Depth d, Move m, Value statV, Value evalM) {
|
||||
|
||||
int c1, c2, c3;
|
||||
TTEntry *tte, *replace;
|
||||
uint32_t key32 = key >> 32; // Use the high 32 bits as key inside the cluster
|
||||
|
||||
tte = replace = first_entry(key);
|
||||
|
||||
for (unsigned i = 0; i < ClusterSize; i++, tte++)
|
||||
{
|
||||
if (!tte->key() || tte->key() == key32) // Empty or overwrite old
|
||||
{
|
||||
if (!m)
|
||||
m = tte->move(); // Preserve any existing ttMove
|
||||
|
||||
replace = tte;
|
||||
break;
|
||||
}
|
||||
|
||||
// Implement replace strategy
|
||||
c1 = (replace->generation() == generation ? 2 : 0);
|
||||
c2 = (tte->generation() == generation || tte->bound() == BOUND_EXACT ? -2 : 0);
|
||||
c3 = (tte->depth() < replace->depth() ? 1 : 0);
|
||||
|
||||
if (c1 + c2 + c3 > 0)
|
||||
replace = tte;
|
||||
}
|
||||
|
||||
replace->save(key32, v, b, d, m, generation, statV, evalM);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,131 +17,102 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(TT_H_INCLUDED)
|
||||
#ifndef TT_H_INCLUDED
|
||||
#define TT_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
#include "misc.h"
|
||||
#include "types.h"
|
||||
|
||||
#include "depth.h"
|
||||
#include "move.h"
|
||||
#include "value.h"
|
||||
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
/// The TTEntry class is the class of transposition table entries
|
||||
/// The TTEntry is the 128 bit transposition table entry, defined as below:
|
||||
///
|
||||
/// A TTEntry needs 128 bits to be stored
|
||||
///
|
||||
/// bit 0-31: key
|
||||
/// bit 32-63: data
|
||||
/// bit 64-79: value
|
||||
/// bit 80-95: depth
|
||||
/// bit 96-111: static value
|
||||
/// bit 112-127: margin of static value
|
||||
///
|
||||
/// the 32 bits of the data field are so defined
|
||||
///
|
||||
/// bit 0-16: move
|
||||
/// bit 17-20: not used
|
||||
/// bit 21-22: value type
|
||||
/// bit 23-31: generation
|
||||
/// key: 32 bit
|
||||
/// move: 16 bit
|
||||
/// bound type: 8 bit
|
||||
/// generation: 8 bit
|
||||
/// value: 16 bit
|
||||
/// depth: 16 bit
|
||||
/// static value: 16 bit
|
||||
/// static margin: 16 bit
|
||||
|
||||
class TTEntry {
|
||||
struct TTEntry {
|
||||
|
||||
public:
|
||||
void save(uint32_t k, Value v, ValueType t, Depth d, Move m, int g, Value statV, Value kd) {
|
||||
void save(uint32_t k, Value v, Bound b, Depth d, Move m, int g, Value ev, Value em) {
|
||||
|
||||
key32 = k;
|
||||
data = (m & 0x1FFFF) | (t << 21) | (g << 23);
|
||||
value16 = int16_t(v);
|
||||
depth16 = int16_t(d);
|
||||
staticValue = int16_t(statV);
|
||||
staticValueMargin = int16_t(kd);
|
||||
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;
|
||||
evalMargin = (int16_t)em;
|
||||
}
|
||||
void set_generation(int g) { data = move() | (type() << 21) | (g << 23); }
|
||||
void set_generation(uint8_t g) { generation8 = g; }
|
||||
|
||||
uint32_t key() const { return key32; }
|
||||
Depth depth() const { return Depth(depth16); }
|
||||
Move move() const { return Move(data & 0x1FFFF); }
|
||||
Value value() const { return Value(value16); }
|
||||
ValueType type() const { return ValueType((data >> 21) & 3); }
|
||||
int generation() const { return data >> 23; }
|
||||
Value static_value() const { return Value(staticValue); }
|
||||
Value static_value_margin() const { return Value(staticValueMargin); }
|
||||
uint32_t key() const { return key32; }
|
||||
Depth depth() const { return (Depth)depth16; }
|
||||
Move move() const { return (Move)move16; }
|
||||
Value value() const { return (Value)value16; }
|
||||
Bound bound() const { return (Bound)bound8; }
|
||||
int generation() const { return (int)generation8; }
|
||||
Value eval_value() const { return (Value)evalValue; }
|
||||
Value eval_margin() const { return (Value)evalMargin; }
|
||||
|
||||
private:
|
||||
uint32_t key32;
|
||||
uint32_t data;
|
||||
int16_t value16;
|
||||
int16_t depth16;
|
||||
int16_t staticValue;
|
||||
int16_t staticValueMargin;
|
||||
uint16_t move16;
|
||||
uint8_t bound8, generation8;
|
||||
int16_t value16, depth16, evalValue, evalMargin;
|
||||
};
|
||||
|
||||
|
||||
/// This is the number of TTEntry slots for each cluster
|
||||
const int ClusterSize = 4;
|
||||
|
||||
/// TTCluster consists of ClusterSize number of TTEntries.
|
||||
/// Size of TTCluster must not be bigger than a cache line size.
|
||||
/// In case it is less, it should be padded to guarantee always aligned accesses.
|
||||
|
||||
struct TTCluster {
|
||||
TTEntry data[ClusterSize];
|
||||
};
|
||||
|
||||
|
||||
/// The transposition table class. This is basically just a huge array
|
||||
/// containing TTCluster objects, and a few methods for writing new entries
|
||||
/// and reading new ones.
|
||||
/// A TranspositionTable consists of a power of 2 number of clusters and each
|
||||
/// cluster consists of ClusterSize number of TTEntry. Each non-empty entry
|
||||
/// contains information of exactly one position. Size of a cluster shall not be
|
||||
/// bigger than a cache line size. In case it is less, it should be padded to
|
||||
/// guarantee always aligned accesses.
|
||||
|
||||
class TranspositionTable {
|
||||
|
||||
TranspositionTable(const TranspositionTable&);
|
||||
TranspositionTable& operator=(const TranspositionTable&);
|
||||
static const unsigned ClusterSize = 4; // A cluster is 64 Bytes
|
||||
|
||||
public:
|
||||
TranspositionTable();
|
||||
~TranspositionTable();
|
||||
~TranspositionTable() { free(mem); }
|
||||
void new_search() { generation++; }
|
||||
|
||||
const TTEntry* probe(const Key key) const;
|
||||
TTEntry* first_entry(const Key key) const;
|
||||
void refresh(const TTEntry* tte) const;
|
||||
void set_size(size_t mbSize);
|
||||
void clear();
|
||||
void store(const Key posKey, Value v, ValueType type, Depth d, Move m, Value statV, Value kingD);
|
||||
TTEntry* retrieve(const Key posKey) const;
|
||||
void new_search();
|
||||
TTEntry* first_entry(const Key posKey) const;
|
||||
void refresh(const TTEntry* tte) const;
|
||||
void store(const Key key, Value v, Bound type, Depth d, Move m, Value statV, Value kingD);
|
||||
|
||||
private:
|
||||
size_t size;
|
||||
TTCluster* entries;
|
||||
uint8_t generation; // To properly compare, size must be smaller then TT stored value
|
||||
uint32_t hashMask;
|
||||
TTEntry* table;
|
||||
void* mem;
|
||||
uint8_t generation; // Size must be not bigger than TTEntry::generation8
|
||||
};
|
||||
|
||||
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.
|
||||
/// 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 posKey) const {
|
||||
inline TTEntry* TranspositionTable::first_entry(const Key key) const {
|
||||
|
||||
return entries[uint32_t(posKey) & (size - 1)].data;
|
||||
return table + ((uint32_t)key & hashMask);
|
||||
}
|
||||
|
||||
|
||||
/// TranspositionTable::refresh updates the 'generation' value of the TTEntry
|
||||
/// to avoid aging. Normally called after a TT hit, before to return.
|
||||
/// TranspositionTable::refresh() updates the 'generation' value of the TTEntry
|
||||
/// to avoid aging. Normally called after a TT hit.
|
||||
|
||||
inline void TranspositionTable::refresh(const TTEntry* tte) const {
|
||||
|
||||
const_cast<TTEntry*>(tte)->set_generation(generation);
|
||||
}
|
||||
|
||||
#endif // !defined(TT_H_INCLUDED)
|
||||
#endif // #ifndef TT_H_INCLUDED
|
||||
|
||||
+390
-123
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,160 +17,427 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(TYPES_H_INCLUDED)
|
||||
#ifndef TYPES_H_INCLUDED
|
||||
#define TYPES_H_INCLUDED
|
||||
|
||||
#if !defined(_MSC_VER)
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
#else
|
||||
|
||||
// Disable some silly and noisy warning from MSVC compiler
|
||||
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
|
||||
#pragma warning(disable: 4127) // Conditional expression is constant
|
||||
|
||||
typedef __int8 int8_t;
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef __int16 int16;
|
||||
typedef unsigned __int16 uint16_t;
|
||||
typedef __int32 int32_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
|
||||
typedef __int16 int16_t;
|
||||
typedef __int64 int64_t;
|
||||
|
||||
#endif // !defined(_MSC_VER)
|
||||
|
||||
// Hash keys
|
||||
typedef uint64_t Key;
|
||||
|
||||
// Bitboard type
|
||||
typedef uint64_t Bitboard;
|
||||
/// For Linux and OSX configuration is done automatically using Makefile. To get
|
||||
/// started type 'make help'.
|
||||
///
|
||||
/// For Windows, part of the configuration is detected automatically, but some
|
||||
/// switches need to be set manually:
|
||||
///
|
||||
/// -DNDEBUG | Disable debugging mode. Use always.
|
||||
///
|
||||
/// -DNO_PREFETCH | Disable use of prefetch asm-instruction. A must if you want
|
||||
/// | the executable to run on some very old machines.
|
||||
///
|
||||
/// -DUSE_POPCNT | Add runtime support for use of popcnt asm-instruction. Works
|
||||
/// | only in 64-bit mode. For compiling requires hardware with
|
||||
/// | popcnt support.
|
||||
|
||||
#include <cassert>
|
||||
#include <cctype>
|
||||
#include <climits>
|
||||
#include <cstdlib>
|
||||
|
||||
////
|
||||
//// Configuration
|
||||
////
|
||||
#include "platform.h"
|
||||
|
||||
//// For Linux and OSX configuration is done automatically using Makefile.
|
||||
//// To get started type "make help".
|
||||
////
|
||||
//// For windows part of the configuration is detected automatically, but
|
||||
//// some switches need to be set manually:
|
||||
////
|
||||
//// -DNDEBUG | Disable debugging mode. Use always.
|
||||
////
|
||||
//// -DNO_PREFETCH | Disable use of prefetch asm-instruction. A must if you want the
|
||||
//// | executable to run on some very old machines.
|
||||
////
|
||||
//// -DUSE_POPCNT | Add runtime support for use of popcnt asm-instruction.
|
||||
//// | Works only in 64-bit mode. For compiling requires hardware
|
||||
//// | with popcnt support. Around 4% speed-up.
|
||||
#define unlikely(x) (x) // For code annotation purposes
|
||||
|
||||
// Automatic detection for 64-bit under Windows
|
||||
#if defined(_WIN64)
|
||||
#define IS_64BIT
|
||||
#if defined(_WIN64) && !defined(IS_64BIT)
|
||||
# include <intrin.h> // MSVC popcnt and bsfq instrinsics
|
||||
# define IS_64BIT
|
||||
# define USE_BSFQ
|
||||
#endif
|
||||
|
||||
// Automatic detection for use of bsfq asm-instruction under Windows.
|
||||
// Works only in 64-bit mode. Does not work with MSVC.
|
||||
#if defined(_WIN64) && defined(__INTEL_COMPILER)
|
||||
#define USE_BSFQ
|
||||
#if defined(USE_POPCNT) && defined(_MSC_VER) && defined(__INTEL_COMPILER)
|
||||
# include <nmmintrin.h> // Intel header for _mm_popcnt_u64() intrinsic
|
||||
#endif
|
||||
|
||||
// Cache line alignment specification
|
||||
# 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(64))
|
||||
# define CACHE_LINE_ALIGNMENT __declspec(align(CACHE_LINE_SIZE))
|
||||
#else
|
||||
#define CACHE_LINE_ALIGNMENT __attribute__ ((aligned(64)))
|
||||
# define CACHE_LINE_ALIGNMENT __attribute__ ((aligned(CACHE_LINE_SIZE)))
|
||||
#endif
|
||||
|
||||
// Define a __cpuid() function for gcc compilers, for Intel and MSVC
|
||||
// is already available as an intrinsic.
|
||||
#if defined(_MSC_VER)
|
||||
#include <intrin.h>
|
||||
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
|
||||
inline void __cpuid(int CPUInfo[4], int InfoType)
|
||||
{
|
||||
int* eax = CPUInfo + 0;
|
||||
int* ebx = CPUInfo + 1;
|
||||
int* ecx = CPUInfo + 2;
|
||||
int* edx = CPUInfo + 3;
|
||||
|
||||
*eax = InfoType;
|
||||
*ecx = 0;
|
||||
__asm__("cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
|
||||
: "0" (*eax), "2" (*ecx));
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
# define FORCE_INLINE __forceinline
|
||||
#elif defined(__GNUC__)
|
||||
# define FORCE_INLINE inline __attribute__((always_inline))
|
||||
#else
|
||||
inline void __cpuid(int CPUInfo[4], int)
|
||||
{
|
||||
CPUInfo[0] = CPUInfo[1] = CPUInfo[2] = CPUInfo[3] = 0;
|
||||
}
|
||||
# define FORCE_INLINE inline
|
||||
#endif
|
||||
|
||||
#ifdef USE_POPCNT
|
||||
const bool HasPopCnt = true;
|
||||
#else
|
||||
const bool HasPopCnt = false;
|
||||
#endif
|
||||
|
||||
// Templetized operators used by enum types like Depth, Piece, Square and so on.
|
||||
// We don't want to write the same inline for each different enum. Note that we
|
||||
// pass by value to silence scaring warnings when using volatiles.
|
||||
// Because these templates override common operators and are included in all the
|
||||
// files, there is a possibility that the compiler silently performs some unwanted
|
||||
// overrides. To avoid possible very nasty bugs the templates are disabled by default
|
||||
// and must be enabled for each type on a case by case base. The enabling trick
|
||||
// uses template specialization, namely we just declare following struct.
|
||||
template<typename T> struct TempletizedOperator;
|
||||
#ifdef IS_64BIT
|
||||
const bool Is64Bit = true;
|
||||
#else
|
||||
const bool Is64Bit = false;
|
||||
#endif
|
||||
|
||||
// Then to enable the enum type we use following macro that defines a specialization
|
||||
// of TempletizedOperator for the given enum T. Here is defined typedef Not_Enabled.
|
||||
// Name of typedef is chosen to produce somewhat informative compile error messages.
|
||||
#define ENABLE_OPERATORS_ON(T) \
|
||||
template<> struct TempletizedOperator<T> { typedef T Not_Enabled; }
|
||||
typedef uint64_t Key;
|
||||
typedef uint64_t Bitboard;
|
||||
|
||||
// Finally we use macro OK(T) to check if type T is enabled. The macro simply
|
||||
// tries to use Not_Enabled, if was not previously defined a compile error occurs.
|
||||
// The check is done fully at compile time and there is zero overhead at runtime.
|
||||
#define OK(T) typedef typename TempletizedOperator<T>::Not_Enabled Type
|
||||
const int MAX_MOVES = 192;
|
||||
const int MAX_PLY = 100;
|
||||
const int MAX_PLY_PLUS_6 = MAX_PLY + 6;
|
||||
|
||||
template<typename T>
|
||||
inline T operator+ (const T d1, const T d2) { OK(T); return T(int(d1) + int(d2)); }
|
||||
/// A move needs 16 bits to be stored
|
||||
///
|
||||
/// bit 0- 5: destination square (from 0 to 63)
|
||||
/// bit 6-11: origin square (from 0 to 63)
|
||||
/// bit 12-13: promotion piece type - 2 (from KNIGHT-2 to QUEEN-2)
|
||||
/// bit 14-15: special move flag: promotion (1), en passant (2), castle (3)
|
||||
///
|
||||
/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in because in
|
||||
/// any normal move destination square is always different from origin square
|
||||
/// while MOVE_NONE and MOVE_NULL have the same origin and destination square.
|
||||
|
||||
template<typename T>
|
||||
inline T operator- (const T d1, const T d2) { OK(T); return T(int(d1) - int(d2)); }
|
||||
enum Move {
|
||||
MOVE_NONE,
|
||||
MOVE_NULL = 65
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline T operator* (int i, const T d) { OK(T); return T(i * int(d)); }
|
||||
enum MoveType {
|
||||
NORMAL,
|
||||
PROMOTION = 1 << 14,
|
||||
ENPASSANT = 2 << 14,
|
||||
CASTLE = 3 << 14
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline T operator* (const T d, int i) { OK(T); return T(int(d) * i); }
|
||||
enum CastleRight { // Defined as in PolyGlot book hash key
|
||||
CASTLES_NONE,
|
||||
WHITE_OO,
|
||||
WHITE_OOO = WHITE_OO << 1,
|
||||
BLACK_OO = WHITE_OO << 2,
|
||||
BLACK_OOO = WHITE_OO << 3,
|
||||
ALL_CASTLES = WHITE_OO | WHITE_OOO | BLACK_OO | BLACK_OOO,
|
||||
CASTLE_RIGHT_NB = 16
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline T operator/ (const T d, int i) { OK(T); return T(int(d) / i); }
|
||||
enum CastlingSide {
|
||||
KING_SIDE,
|
||||
QUEEN_SIDE,
|
||||
CASTLING_SIDE_NB = 2
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline T operator- (const T d) { OK(T); return T(-int(d)); }
|
||||
enum Phase {
|
||||
PHASE_ENDGAME,
|
||||
PHASE_MIDGAME = 128,
|
||||
MG = 0, EG = 1, PHASE_NB = 2
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline T operator++ (T& d, int) { OK(T); d = T(int(d) + 1); return d; }
|
||||
enum ScaleFactor {
|
||||
SCALE_FACTOR_DRAW = 0,
|
||||
SCALE_FACTOR_NORMAL = 64,
|
||||
SCALE_FACTOR_MAX = 128,
|
||||
SCALE_FACTOR_NONE = 255
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline T operator-- (T& d, int) { OK(T); d = T(int(d) - 1); return d; }
|
||||
enum Bound {
|
||||
BOUND_NONE,
|
||||
BOUND_UPPER,
|
||||
BOUND_LOWER,
|
||||
BOUND_EXACT = BOUND_UPPER | BOUND_LOWER
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline void operator+= (T& d1, const T d2) { OK(T); d1 = d1 + d2; }
|
||||
enum Value {
|
||||
VALUE_ZERO = 0,
|
||||
VALUE_DRAW = 0,
|
||||
VALUE_KNOWN_WIN = 15000,
|
||||
VALUE_MATE = 30000,
|
||||
VALUE_INFINITE = 30001,
|
||||
VALUE_NONE = 30002,
|
||||
|
||||
template<typename T>
|
||||
inline void operator-= (T& d1, const T d2) { OK(T); d1 = d1 - d2; }
|
||||
VALUE_MATE_IN_MAX_PLY = VALUE_MATE - MAX_PLY,
|
||||
VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + MAX_PLY,
|
||||
|
||||
template<typename T>
|
||||
inline void operator*= (T& d, int i) { OK(T); d = T(int(d) * i); }
|
||||
VALUE_ENSURE_INTEGER_SIZE_P = INT_MAX,
|
||||
VALUE_ENSURE_INTEGER_SIZE_N = INT_MIN,
|
||||
|
||||
template<typename T>
|
||||
inline void operator/= (T& d, int i) { OK(T); d = T(int(d) / i); }
|
||||
PawnValueMg = 198, PawnValueEg = 258,
|
||||
KnightValueMg = 817, KnightValueEg = 846,
|
||||
BishopValueMg = 836, BishopValueEg = 857,
|
||||
RookValueMg = 1270, RookValueEg = 1278,
|
||||
QueenValueMg = 2521, QueenValueEg = 2558
|
||||
};
|
||||
|
||||
#undef OK
|
||||
enum PieceType {
|
||||
NO_PIECE_TYPE, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING,
|
||||
ALL_PIECES = 0,
|
||||
PIECE_TYPE_NB = 8
|
||||
};
|
||||
|
||||
#endif // !defined(TYPES_H_INCLUDED)
|
||||
enum Piece {
|
||||
NO_PIECE,
|
||||
W_PAWN = 1, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
|
||||
B_PAWN = 9, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING,
|
||||
PIECE_NB = 16
|
||||
};
|
||||
|
||||
enum Color {
|
||||
WHITE, BLACK, NO_COLOR, COLOR_NB = 2
|
||||
};
|
||||
|
||||
enum Depth {
|
||||
|
||||
ONE_PLY = 2,
|
||||
|
||||
DEPTH_ZERO = 0 * ONE_PLY,
|
||||
DEPTH_QS_CHECKS = -1 * ONE_PLY,
|
||||
DEPTH_QS_NO_CHECKS = -2 * ONE_PLY,
|
||||
DEPTH_QS_RECAPTURES = -7 * ONE_PLY,
|
||||
|
||||
DEPTH_NONE = -127 * ONE_PLY
|
||||
};
|
||||
|
||||
enum Square {
|
||||
SQ_A1, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1,
|
||||
SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2,
|
||||
SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3,
|
||||
SQ_A4, SQ_B4, SQ_C4, SQ_D4, SQ_E4, SQ_F4, SQ_G4, SQ_H4,
|
||||
SQ_A5, SQ_B5, SQ_C5, SQ_D5, SQ_E5, SQ_F5, SQ_G5, SQ_H5,
|
||||
SQ_A6, SQ_B6, SQ_C6, SQ_D6, SQ_E6, SQ_F6, SQ_G6, SQ_H6,
|
||||
SQ_A7, SQ_B7, SQ_C7, SQ_D7, SQ_E7, SQ_F7, SQ_G7, SQ_H7,
|
||||
SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
|
||||
SQ_NONE,
|
||||
|
||||
SQUARE_NB = 64,
|
||||
|
||||
DELTA_N = 8,
|
||||
DELTA_E = 1,
|
||||
DELTA_S = -8,
|
||||
DELTA_W = -1,
|
||||
|
||||
DELTA_NN = DELTA_N + DELTA_N,
|
||||
DELTA_NE = DELTA_N + DELTA_E,
|
||||
DELTA_SE = DELTA_S + DELTA_E,
|
||||
DELTA_SS = DELTA_S + DELTA_S,
|
||||
DELTA_SW = DELTA_S + DELTA_W,
|
||||
DELTA_NW = DELTA_N + DELTA_W
|
||||
};
|
||||
|
||||
enum File {
|
||||
FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H, FILE_NB
|
||||
};
|
||||
|
||||
enum Rank {
|
||||
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8, RANK_NB
|
||||
};
|
||||
|
||||
|
||||
/// Score enum keeps a midgame and an endgame value in a single integer (enum),
|
||||
/// first LSB 16 bits are used to store endgame value, while upper bits are used
|
||||
/// for midgame value. Compiler is free to choose the enum type as long as can
|
||||
/// keep its data, so ensure Score to be an integer type.
|
||||
enum Score {
|
||||
SCORE_ZERO,
|
||||
SCORE_ENSURE_INTEGER_SIZE_P = INT_MAX,
|
||||
SCORE_ENSURE_INTEGER_SIZE_N = INT_MIN
|
||||
};
|
||||
|
||||
inline Score make_score(int mg, int eg) { return Score((mg << 16) + eg); }
|
||||
|
||||
/// Extracting the signed lower and upper 16 bits it not so trivial because
|
||||
/// according to the standard a simple cast to short is implementation defined
|
||||
/// and so is a right shift of a signed integer.
|
||||
inline Value mg_value(Score s) { return Value(((s + 0x8000) & ~0xffff) / 0x10000); }
|
||||
|
||||
/// On Intel 64 bit we have a small speed regression with the standard conforming
|
||||
/// version, so use a faster code in this case that, although not 100% standard
|
||||
/// compliant it seems to work for Intel and MSVC.
|
||||
#if defined(IS_64BIT) && (!defined(__GNUC__) || defined(__INTEL_COMPILER))
|
||||
|
||||
inline Value eg_value(Score s) { return Value(int16_t(s & 0xffff)); }
|
||||
|
||||
#else
|
||||
|
||||
inline Value eg_value(Score s) {
|
||||
return Value((int)(unsigned(s) & 0x7fffu) - (int)(unsigned(s) & 0x8000u));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#define ENABLE_SAFE_OPERATORS_ON(T) \
|
||||
inline T operator+(const T d1, const T d2) { return T(int(d1) + int(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) { d1 = d1 + d2; return d1; } \
|
||||
inline T& operator-=(T& d1, const T d2) { d1 = d1 - d2; return d1; } \
|
||||
inline T& operator*=(T& d, int i) { d = T(int(d) * i); return d; }
|
||||
|
||||
#define ENABLE_OPERATORS_ON(T) ENABLE_SAFE_OPERATORS_ON(T) \
|
||||
inline T operator++(T& d, int) { d = T(int(d) + 1); return d; } \
|
||||
inline T operator--(T& d, int) { d = T(int(d) - 1); return d; } \
|
||||
inline T operator/(const T d, int i) { return T(int(d) / i); } \
|
||||
inline T& operator/=(T& d, int i) { d = T(int(d) / i); return d; }
|
||||
|
||||
ENABLE_OPERATORS_ON(Value)
|
||||
ENABLE_OPERATORS_ON(PieceType)
|
||||
ENABLE_OPERATORS_ON(Piece)
|
||||
ENABLE_OPERATORS_ON(Color)
|
||||
ENABLE_OPERATORS_ON(Depth)
|
||||
ENABLE_OPERATORS_ON(Square)
|
||||
ENABLE_OPERATORS_ON(File)
|
||||
ENABLE_OPERATORS_ON(Rank)
|
||||
|
||||
/// Added operators for adding integers to a Value
|
||||
inline Value operator+(Value v, int i) { return Value(int(v) + i); }
|
||||
inline Value operator-(Value v, int i) { return Value(int(v) - i); }
|
||||
|
||||
ENABLE_SAFE_OPERATORS_ON(Score)
|
||||
|
||||
/// Only declared but not defined. We don't want to multiply two scores due to
|
||||
/// a very high risk of overflow. So user should explicitly convert to integer.
|
||||
inline Score operator*(Score s1, Score s2);
|
||||
|
||||
/// Division of a Score must be handled separately for each term
|
||||
inline Score operator/(Score s, int i) {
|
||||
return make_score(mg_value(s) / i, eg_value(s) / i);
|
||||
}
|
||||
|
||||
#undef ENABLE_OPERATORS_ON
|
||||
#undef ENABLE_SAFE_OPERATORS_ON
|
||||
|
||||
extern Value PieceValue[PHASE_NB][PIECE_NB];
|
||||
|
||||
struct ExtMove {
|
||||
Move move;
|
||||
int score;
|
||||
};
|
||||
|
||||
inline bool operator<(const ExtMove& f, const ExtMove& s) {
|
||||
return f.score < s.score;
|
||||
}
|
||||
|
||||
inline Color operator~(Color c) {
|
||||
return Color(c ^ 1);
|
||||
}
|
||||
|
||||
inline Square operator~(Square s) {
|
||||
return Square(s ^ 56); // Vertical flip SQ_A1 -> SQ_A8
|
||||
}
|
||||
|
||||
inline Square operator|(File f, Rank r) {
|
||||
return Square((r << 3) | f);
|
||||
}
|
||||
|
||||
inline Value mate_in(int ply) {
|
||||
return VALUE_MATE - ply;
|
||||
}
|
||||
|
||||
inline Value mated_in(int ply) {
|
||||
return -VALUE_MATE + ply;
|
||||
}
|
||||
|
||||
inline Piece make_piece(Color c, PieceType pt) {
|
||||
return Piece((c << 3) | pt);
|
||||
}
|
||||
|
||||
inline CastleRight make_castle_right(Color c, CastlingSide s) {
|
||||
return CastleRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
|
||||
}
|
||||
|
||||
inline PieceType type_of(Piece p) {
|
||||
return PieceType(p & 7);
|
||||
}
|
||||
|
||||
inline Color color_of(Piece p) {
|
||||
assert(p != NO_PIECE);
|
||||
return Color(p >> 3);
|
||||
}
|
||||
|
||||
inline bool is_ok(Square s) {
|
||||
return s >= SQ_A1 && s <= SQ_H8;
|
||||
}
|
||||
|
||||
inline File file_of(Square s) {
|
||||
return File(s & 7);
|
||||
}
|
||||
|
||||
inline Rank rank_of(Square s) {
|
||||
return Rank(s >> 3);
|
||||
}
|
||||
|
||||
inline Square mirror(Square s) {
|
||||
return Square(s ^ 7); // Horizontal flip SQ_A1 -> SQ_H1
|
||||
}
|
||||
|
||||
inline Square relative_square(Color c, Square s) {
|
||||
return Square(s ^ (c * 56));
|
||||
}
|
||||
|
||||
inline Rank relative_rank(Color c, Rank r) {
|
||||
return Rank(r ^ (c * 7));
|
||||
}
|
||||
|
||||
inline Rank relative_rank(Color c, Square s) {
|
||||
return relative_rank(c, rank_of(s));
|
||||
}
|
||||
|
||||
inline bool opposite_colors(Square s1, Square s2) {
|
||||
int s = int(s1) ^ int(s2);
|
||||
return ((s >> 3) ^ s) & 1;
|
||||
}
|
||||
|
||||
inline char file_to_char(File f, bool tolower = true) {
|
||||
return char(f - FILE_A + (tolower ? 'a' : 'A'));
|
||||
}
|
||||
|
||||
inline char rank_to_char(Rank r) {
|
||||
return char(r - RANK_1 + '1');
|
||||
}
|
||||
|
||||
inline Square pawn_push(Color c) {
|
||||
return c == WHITE ? DELTA_N : DELTA_S;
|
||||
}
|
||||
|
||||
inline Square from_sq(Move m) {
|
||||
return Square((m >> 6) & 0x3F);
|
||||
}
|
||||
|
||||
inline Square to_sq(Move m) {
|
||||
return Square(m & 0x3F);
|
||||
}
|
||||
|
||||
inline MoveType type_of(Move m) {
|
||||
return MoveType(m & (3 << 14));
|
||||
}
|
||||
|
||||
inline PieceType promotion_type(Move m) {
|
||||
return PieceType(((m >> 12) & 3) + 2);
|
||||
}
|
||||
|
||||
inline Move make_move(Square from, Square to) {
|
||||
return Move(to | (from << 6));
|
||||
}
|
||||
|
||||
template<MoveType T>
|
||||
inline Move make(Square from, Square to, PieceType pt = KNIGHT) {
|
||||
return Move(to | (from << 6) | T | ((pt - KNIGHT) << 12));
|
||||
}
|
||||
|
||||
inline bool is_ok(Move m) {
|
||||
return from_sq(m) != to_sq(m); // Catches also MOVE_NULL and MOVE_NONE
|
||||
}
|
||||
|
||||
#include <string>
|
||||
|
||||
inline const std::string square_to_string(Square s) {
|
||||
char ch[] = { file_to_char(file_of(s)), rank_to_char(rank_of(s)), 0 };
|
||||
return ch;
|
||||
}
|
||||
|
||||
#endif // #ifndef TYPES_H_INCLUDED
|
||||
|
||||
+131
-216
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,288 +17,203 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
////
|
||||
//// Includes
|
||||
////
|
||||
|
||||
#include <cassert>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "evaluate.h"
|
||||
#include "misc.h"
|
||||
#include "move.h"
|
||||
#include "movegen.h"
|
||||
#include "notation.h"
|
||||
#include "position.h"
|
||||
#include "san.h"
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
#include "ucioption.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
extern void benchmark(const Position& pos, istream& is);
|
||||
|
||||
namespace {
|
||||
|
||||
// FEN string for the initial position
|
||||
const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
// FEN string of the initial position, normal chess
|
||||
const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
|
||||
// UCIParser is a class for parsing UCI input. The class
|
||||
// is actually a string stream built on a given input string.
|
||||
typedef istringstream UCIParser;
|
||||
// Keep track of position keys along the setup moves (from start position to the
|
||||
// position just before to start searching). Needed by repetition draw detection.
|
||||
Search::StateStackPtr SetupStates;
|
||||
|
||||
// Local functions
|
||||
void set_option(UCIParser& uip);
|
||||
void set_position(Position& pos, UCIParser& uip);
|
||||
bool go(Position& pos, UCIParser& uip);
|
||||
void perft(Position& pos, UCIParser& uip);
|
||||
void setoption(istringstream& up);
|
||||
void position(Position& pos, istringstream& up);
|
||||
void go(const Position& pos, istringstream& up);
|
||||
}
|
||||
|
||||
|
||||
/// execute_uci_command() takes a string as input, uses a UCIParser
|
||||
/// object to parse this text string as a UCI command, and calls
|
||||
/// the appropriate functions. In addition to the UCI commands,
|
||||
/// the function also supports a few debug commands.
|
||||
/// Wait for a command from the user, parse this text string as an UCI command,
|
||||
/// and call the appropriate functions. Also intercepts EOF from stdin to ensure
|
||||
/// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
|
||||
/// commands, the function also supports a few debug commands.
|
||||
|
||||
bool execute_uci_command(const string& cmd) {
|
||||
void UCI::loop(const string& args) {
|
||||
|
||||
static Position pos(StartPositionFEN, 0); // The root position
|
||||
UCIParser up(cmd);
|
||||
string token;
|
||||
Position pos(StartFEN, false, Threads.main()); // The root position
|
||||
string token, cmd = args;
|
||||
|
||||
if (!(up >> token)) // operator>>() skips any whitespace
|
||||
return true;
|
||||
do {
|
||||
if (args.empty() && !getline(cin, cmd)) // Block here waiting for input
|
||||
cmd = "quit";
|
||||
|
||||
if (token == "quit")
|
||||
return false;
|
||||
istringstream is(cmd);
|
||||
|
||||
if (token == "go")
|
||||
return go(pos, up);
|
||||
is >> skipws >> token;
|
||||
|
||||
if (token == "uci")
|
||||
{
|
||||
cout << "id name " << engine_name()
|
||||
<< "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
|
||||
print_uci_options();
|
||||
cout << "uciok" << endl;
|
||||
}
|
||||
else if (token == "ucinewgame")
|
||||
pos.from_fen(StartPositionFEN);
|
||||
if (token == "quit" || token == "stop" || token == "ponderhit")
|
||||
{
|
||||
// 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 != "ponderhit" || Search::Signals.stopOnPonderhit)
|
||||
{
|
||||
Search::Signals.stop = true;
|
||||
Threads.main()->notify_one(); // Could be sleeping
|
||||
}
|
||||
else
|
||||
Search::Limits.ponder = false;
|
||||
}
|
||||
else if (token == "perft" && (is >> token)) // Read perft depth
|
||||
{
|
||||
stringstream ss;
|
||||
|
||||
else if (token == "isready")
|
||||
cout << "readyok" << endl;
|
||||
ss << Options["Hash"] << " "
|
||||
<< Options["Threads"] << " " << token << " current perft";
|
||||
|
||||
else if (token == "position")
|
||||
set_position(pos, up);
|
||||
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 << sync_endl;
|
||||
|
||||
else if (token == "setoption")
|
||||
set_option(up);
|
||||
else if (token == "uci")
|
||||
sync_cout << "id name " << engine_info(true)
|
||||
<< "\n" << Options
|
||||
<< "\nuciok" << sync_endl;
|
||||
|
||||
// The remaining commands are for debugging purposes only
|
||||
else if (token == "d")
|
||||
pos.print();
|
||||
else if (token == "eval")
|
||||
{
|
||||
Search::RootColor = pos.side_to_move(); // Ensure it is set
|
||||
sync_cout << Eval::trace(pos) << sync_endl;
|
||||
}
|
||||
else if (token == "ucinewgame") { /* Avoid returning "Unknown command" */ }
|
||||
else if (token == "go") go(pos, is);
|
||||
else if (token == "position") position(pos, is);
|
||||
else if (token == "setoption") setoption(is);
|
||||
else if (token == "flip") pos.flip();
|
||||
else if (token == "bench") benchmark(pos, is);
|
||||
else if (token == "d") sync_cout << pos.pretty() << sync_endl;
|
||||
else if (token == "isready") sync_cout << "readyok" << sync_endl;
|
||||
else
|
||||
sync_cout << "Unknown command: " << cmd << sync_endl;
|
||||
|
||||
else if (token == "flip")
|
||||
{
|
||||
Position p(pos, pos.thread());
|
||||
pos.flipped_copy(p);
|
||||
}
|
||||
else if (token == "eval")
|
||||
{
|
||||
Value evalMargin;
|
||||
cout << "Incremental mg: " << mg_value(pos.value())
|
||||
<< "\nIncremental eg: " << eg_value(pos.value())
|
||||
<< "\nFull eval: " << evaluate(pos, evalMargin) << endl;
|
||||
}
|
||||
else if (token == "key")
|
||||
cout << "key: " << hex << pos.get_key()
|
||||
<< "\nmaterial key: " << pos.get_material_key()
|
||||
<< "\npawn key: " << pos.get_pawn_key() << endl;
|
||||
} while (token != "quit" && args.empty()); // Args have one-shot behaviour
|
||||
|
||||
else if (token == "perft")
|
||||
perft(pos, up);
|
||||
|
||||
else
|
||||
cout << "Unknown command: " << cmd << endl;
|
||||
|
||||
return true;
|
||||
Threads.wait_for_think_finished(); // Cannot quit while search is running
|
||||
}
|
||||
|
||||
|
||||
////
|
||||
//// Local functions
|
||||
////
|
||||
|
||||
namespace {
|
||||
|
||||
// set_position() is called when Stockfish receives the "position" UCI
|
||||
// command. The input parameter is a UCIParser. It is assumed
|
||||
// that this parser has consumed the first token of the UCI command
|
||||
// ("position"), and is ready to read the second token ("startpos"
|
||||
// or "fen", if the input is well-formed).
|
||||
// position() is called when engine receives the "position" UCI command.
|
||||
// The function sets up the position described in the given fen string ("fen")
|
||||
// or the starting position ("startpos") and then makes the moves given in the
|
||||
// following move list ("moves").
|
||||
|
||||
void set_position(Position& pos, UCIParser& up) {
|
||||
void position(Position& pos, istringstream& is) {
|
||||
|
||||
string token;
|
||||
Move m;
|
||||
string token, fen;
|
||||
|
||||
if (!(up >> token) || (token != "startpos" && token != "fen"))
|
||||
return;
|
||||
is >> token;
|
||||
|
||||
if (token == "startpos")
|
||||
{
|
||||
pos.from_fen(StartPositionFEN);
|
||||
if (!(up >> token))
|
||||
return;
|
||||
fen = StartFEN;
|
||||
is >> token; // Consume "moves" token if any
|
||||
}
|
||||
else // fen
|
||||
{
|
||||
string fen;
|
||||
while (up >> token && token != "moves")
|
||||
{
|
||||
fen += token;
|
||||
fen += ' ';
|
||||
}
|
||||
pos.from_fen(fen);
|
||||
}
|
||||
|
||||
if (token != "moves")
|
||||
else if (token == "fen")
|
||||
while (is >> token && token != "moves")
|
||||
fen += token + " ";
|
||||
else
|
||||
return;
|
||||
|
||||
// Parse optional move list
|
||||
Move move;
|
||||
StateInfo st;
|
||||
while (up >> token)
|
||||
{
|
||||
move = move_from_uci(pos, token);
|
||||
pos.do_move(move, st);
|
||||
if (pos.rule_50_counter() == 0)
|
||||
pos.reset_game_ply();
|
||||
pos.set(fen, Options["UCI_Chess960"], Threads.main());
|
||||
SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
|
||||
|
||||
pos.inc_startpos_ply_counter(); //FIXME: make from_fen to support this and rule50
|
||||
// Parse move list (if any)
|
||||
while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
|
||||
{
|
||||
SetupStates->push(StateInfo());
|
||||
pos.do_move(m, SetupStates->top());
|
||||
}
|
||||
// Our StateInfo st is about going out of scope so copy
|
||||
// its content inside pos before it disappears.
|
||||
pos.detach();
|
||||
}
|
||||
|
||||
|
||||
// set_option() is called when Stockfish receives the "setoption" UCI
|
||||
// command. The input parameter is a UCIParser. It is assumed
|
||||
// that this parser has consumed the first token of the UCI command
|
||||
// ("setoption"), and is ready to read the second token ("name", if
|
||||
// the input is well-formed).
|
||||
// setoption() is called when engine receives the "setoption" UCI command. The
|
||||
// function updates the UCI option ("name") to the given value ("value").
|
||||
|
||||
void set_option(UCIParser& up) {
|
||||
void setoption(istringstream& is) {
|
||||
|
||||
string token, name, value;
|
||||
|
||||
if (!(up >> token) || token != "name") // operator>>() skips any whitespace
|
||||
return;
|
||||
is >> token; // Consume "name" token
|
||||
|
||||
if (!(up >> name))
|
||||
return;
|
||||
// Read option name (can contain spaces)
|
||||
while (is >> token && token != "value")
|
||||
name += string(" ", !name.empty()) + token;
|
||||
|
||||
// Handle names with included spaces
|
||||
while (up >> token && token != "value")
|
||||
name += (" " + token);
|
||||
// Read option value (can contain spaces)
|
||||
while (is >> token)
|
||||
value += string(" ", !value.empty()) + token;
|
||||
|
||||
if (Options.find(name) == Options.end())
|
||||
{
|
||||
cout << "No such option: " << name << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Is a button ?
|
||||
if (token != "value")
|
||||
{
|
||||
Options[name].set_value("true");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(up >> value))
|
||||
return;
|
||||
|
||||
// Handle values with included spaces
|
||||
while (up >> token)
|
||||
value += (" " + token);
|
||||
|
||||
Options[name].set_value(value);
|
||||
if (Options.count(name))
|
||||
Options[name] = value;
|
||||
else
|
||||
sync_cout << "No such option: " << name << sync_endl;
|
||||
}
|
||||
|
||||
|
||||
// go() is called when Stockfish receives the "go" UCI command. The
|
||||
// input parameter is a UCIParser. It is assumed that this
|
||||
// parser has consumed the first token of the UCI command ("go"),
|
||||
// and is ready to read the second token. The function sets the
|
||||
// thinking time and other parameters from the input string, and
|
||||
// calls think() (defined in search.cpp) with the appropriate
|
||||
// parameters. Returns false if a quit command is received while
|
||||
// thinking, returns true otherwise.
|
||||
// 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 search.
|
||||
|
||||
bool go(Position& pos, UCIParser& up) {
|
||||
void go(const Position& pos, istringstream& is) {
|
||||
|
||||
Search::LimitsType limits;
|
||||
vector<Move> searchMoves;
|
||||
string token;
|
||||
|
||||
int time[2] = {0, 0}, inc[2] = {0, 0};
|
||||
int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
|
||||
bool infinite = false, ponder = false;
|
||||
Move searchMoves[MOVES_MAX];
|
||||
|
||||
searchMoves[0] = MOVE_NONE;
|
||||
|
||||
while (up >> token)
|
||||
while (is >> token)
|
||||
{
|
||||
if (token == "infinite")
|
||||
infinite = true;
|
||||
else if (token == "ponder")
|
||||
ponder = true;
|
||||
else if (token == "wtime")
|
||||
up >> time[0];
|
||||
else if (token == "btime")
|
||||
up >> time[1];
|
||||
else if (token == "winc")
|
||||
up >> inc[0];
|
||||
else if (token == "binc")
|
||||
up >> inc[1];
|
||||
else if (token == "movestogo")
|
||||
up >> movesToGo;
|
||||
else if (token == "depth")
|
||||
up >> depth;
|
||||
else if (token == "nodes")
|
||||
up >> nodes;
|
||||
else if (token == "movetime")
|
||||
up >> moveTime;
|
||||
else if (token == "searchmoves")
|
||||
{
|
||||
int numOfMoves = 0;
|
||||
while (up >> token)
|
||||
searchMoves[numOfMoves++] = move_from_uci(pos, token);
|
||||
if (token == "searchmoves")
|
||||
while (is >> token)
|
||||
searchMoves.push_back(move_from_uci(pos, token));
|
||||
|
||||
searchMoves[numOfMoves] = MOVE_NONE;
|
||||
}
|
||||
else if (token == "wtime") is >> limits.time[WHITE];
|
||||
else if (token == "btime") is >> limits.time[BLACK];
|
||||
else if (token == "winc") is >> limits.inc[WHITE];
|
||||
else if (token == "binc") is >> limits.inc[BLACK];
|
||||
else if (token == "movestogo") is >> limits.movestogo;
|
||||
else if (token == "depth") is >> limits.depth;
|
||||
else if (token == "nodes") is >> limits.nodes;
|
||||
else if (token == "movetime") is >> limits.movetime;
|
||||
else if (token == "mate") is >> limits.mate;
|
||||
else if (token == "infinite") limits.infinite = true;
|
||||
else if (token == "ponder") limits.ponder = true;
|
||||
}
|
||||
|
||||
assert(pos.is_ok());
|
||||
|
||||
return think(pos, infinite, ponder, time, inc, movesToGo,
|
||||
depth, nodes, moveTime, searchMoves);
|
||||
}
|
||||
|
||||
void perft(Position& pos, UCIParser& up) {
|
||||
|
||||
int depth, tm, n;
|
||||
|
||||
if (!(up >> depth))
|
||||
return;
|
||||
|
||||
tm = get_system_time();
|
||||
|
||||
n = perft(pos, depth * ONE_PLY);
|
||||
|
||||
tm = get_system_time() - tm;
|
||||
std::cout << "\nNodes " << n
|
||||
<< "\nTime (ms) " << tm
|
||||
<< "\nNodes/second " << int(n / (tm / 1000.0)) << std::endl;
|
||||
Threads.start_thinking(pos, limits, searchMoves, SetupStates);
|
||||
}
|
||||
}
|
||||
|
||||
+102
-116
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,162 +17,148 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
|
||||
#include "evaluate.h"
|
||||
#include "misc.h"
|
||||
#include "thread.h"
|
||||
#include "tt.h"
|
||||
#include "ucioption.h"
|
||||
|
||||
using std::string;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
OptionsMap Options; // Global object
|
||||
UCI::OptionsMap Options; // Global object
|
||||
|
||||
namespace UCI {
|
||||
|
||||
/// 'On change' actions, triggered by an option's value change
|
||||
void on_logger(const Option& o) { start_logger(o); }
|
||||
void on_eval(const Option&) { Eval::init(); }
|
||||
void on_threads(const Option&) { Threads.read_uci_options(); }
|
||||
void on_hash_size(const Option& o) { TT.set_size(o); }
|
||||
void on_clear_hash(const Option&) { TT.clear(); }
|
||||
|
||||
|
||||
// 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 {
|
||||
|
||||
int c1, c2;
|
||||
size_t i = 0;
|
||||
|
||||
while (i < s1.size() && i < s2.size())
|
||||
{
|
||||
c1 = tolower(s1[i]);
|
||||
c2 = tolower(s2[i++]);
|
||||
|
||||
if (c1 != c2)
|
||||
return c1 < c2;
|
||||
}
|
||||
return s1.size() < s2.size();
|
||||
return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), ci_less);
|
||||
}
|
||||
|
||||
|
||||
// stringify() converts a numeric value of type T to a std::string
|
||||
template<typename T>
|
||||
static string stringify(const T& v) {
|
||||
/// init() initializes the UCI options to their hard coded default values
|
||||
|
||||
std::ostringstream ss;
|
||||
ss << v;
|
||||
return ss.str();
|
||||
void init(OptionsMap& o) {
|
||||
|
||||
o["Write Debug Log"] = Option(false, on_logger);
|
||||
o["Write Search Log"] = Option(false);
|
||||
o["Search Log Filename"] = Option("SearchLog.txt");
|
||||
o["Book File"] = Option("book.bin");
|
||||
o["Best Book Move"] = Option(false);
|
||||
o["Contempt Factor"] = Option(0, -50, 50);
|
||||
o["Mobility (Midgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Mobility (Endgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Pawn Structure (Midgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Pawn Structure (Endgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Passed Pawns (Midgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Passed Pawns (Endgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Space"] = Option(100, 0, 200, on_eval);
|
||||
o["Aggressiveness"] = Option(100, 0, 200, on_eval);
|
||||
o["Cowardice"] = Option(100, 0, 200, on_eval);
|
||||
o["Min Split Depth"] = Option(0, 0, 12, on_threads);
|
||||
o["Max Threads per Split Point"] = Option(5, 4, 8, on_threads);
|
||||
o["Threads"] = Option(1, 1, MAX_THREADS, on_threads);
|
||||
o["Idle Threads Sleep"] = Option(false);
|
||||
o["Hash"] = Option(32, 1, 8192, on_hash_size);
|
||||
o["Clear Hash"] = Option(on_clear_hash);
|
||||
o["Ponder"] = Option(true);
|
||||
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(200, 0, 30000);
|
||||
o["Emergency Move Time"] = Option(70, 0, 5000);
|
||||
o["Minimum Thinking Time"] = Option(20, 0, 5000);
|
||||
o["Slow Mover"] = Option(100, 10, 1000);
|
||||
o["UCI_Chess960"] = Option(false);
|
||||
o["UCI_AnalyseMode"] = Option(false, on_eval);
|
||||
}
|
||||
|
||||
|
||||
/// init_uci_options() initializes the UCI options to their hard coded default
|
||||
/// values and initializes the default value of "Threads" and "Minimum Split Depth"
|
||||
/// parameters according to the number of CPU cores.
|
||||
/// operator<<() is used to print all the options default values in chronological
|
||||
/// insertion order (the idx field) and in the format defined by the UCI protocol.
|
||||
|
||||
void init_uci_options() {
|
||||
std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
|
||||
|
||||
Options["Use Search Log"] = Option(false);
|
||||
Options["Search Log Filename"] = Option("SearchLog.txt");
|
||||
Options["Book File"] = Option("book.bin");
|
||||
Options["Best Book Move"] = Option(false);
|
||||
Options["Mobility (Middle Game)"] = Option(100, 0, 200);
|
||||
Options["Mobility (Endgame)"] = Option(100, 0, 200);
|
||||
Options["Pawn Structure (Middle Game)"] = Option(100, 0, 200);
|
||||
Options["Pawn Structure (Endgame)"] = Option(100, 0, 200);
|
||||
Options["Passed Pawns (Middle Game)"] = Option(100, 0, 200);
|
||||
Options["Passed Pawns (Endgame)"] = Option(100, 0, 200);
|
||||
Options["Space"] = Option(100, 0, 200);
|
||||
Options["Aggressiveness"] = Option(100, 0, 200);
|
||||
Options["Cowardice"] = Option(100, 0, 200);
|
||||
Options["Check Extension (PV nodes)"] = Option(2, 0, 2);
|
||||
Options["Check Extension (non-PV nodes)"] = Option(1, 0, 2);
|
||||
Options["Single Evasion Extension (PV nodes)"] = Option(2, 0, 2);
|
||||
Options["Single Evasion Extension (non-PV nodes)"] = Option(2, 0, 2);
|
||||
Options["Mate Threat Extension (PV nodes)"] = Option(2, 0, 2);
|
||||
Options["Mate Threat Extension (non-PV nodes)"] = Option(2, 0, 2);
|
||||
Options["Pawn Push to 7th Extension (PV nodes)"] = Option(1, 0, 2);
|
||||
Options["Pawn Push to 7th Extension (non-PV nodes)"] = Option(1, 0, 2);
|
||||
Options["Passed Pawn Extension (PV nodes)"] = Option(1, 0, 2);
|
||||
Options["Passed Pawn Extension (non-PV nodes)"] = Option(0, 0, 2);
|
||||
Options["Pawn Endgame Extension (PV nodes)"] = Option(2, 0, 2);
|
||||
Options["Pawn Endgame Extension (non-PV nodes)"] = Option(2, 0, 2);
|
||||
Options["Minimum Split Depth"] = Option(4, 4, 7);
|
||||
Options["Maximum Number of Threads per Split Point"] = Option(5, 4, 8);
|
||||
Options["Threads"] = Option(1, 1, MAX_THREADS);
|
||||
Options["Use Sleeping Threads"] = Option(false);
|
||||
Options["Hash"] = Option(32, 4, 8192);
|
||||
Options["Clear Hash"] = Option(false, "button");
|
||||
Options["Ponder"] = Option(true);
|
||||
Options["OwnBook"] = Option(true);
|
||||
Options["MultiPV"] = Option(1, 1, 500);
|
||||
Options["Emergency Move Horizon"] = Option(40, 0, 50);
|
||||
Options["Emergency Base Time"] = Option(200, 0, 60000);
|
||||
Options["Emergency Move Time"] = Option(70, 0, 5000);
|
||||
Options["Minimum Thinking Time"] = Option(20, 0, 5000);
|
||||
Options["UCI_Chess960"] = Option(false); // Just a dummy but needed by GUIs
|
||||
Options["UCI_AnalyseMode"] = Option(false);
|
||||
|
||||
// Set some SMP parameters accordingly to the detected CPU count
|
||||
Option& thr = Options["Threads"];
|
||||
Option& msd = Options["Minimum Split Depth"];
|
||||
|
||||
thr.defaultValue = thr.currentValue = stringify(cpu_count());
|
||||
|
||||
if (cpu_count() >= 8)
|
||||
msd.defaultValue = msd.currentValue = stringify(7);
|
||||
}
|
||||
|
||||
|
||||
/// print_uci_options() prints all the UCI options to the standard output,
|
||||
/// in chronological insertion order (the idx field) and in the format
|
||||
/// defined by the UCI protocol.
|
||||
|
||||
void print_uci_options() {
|
||||
|
||||
for (size_t i = 0; i <= Options.size(); i++)
|
||||
for (OptionsMap::const_iterator it = Options.begin(); it != Options.end(); ++it)
|
||||
if (it->second.idx == i)
|
||||
for (size_t idx = 0; idx < om.size(); idx++)
|
||||
for (OptionsMap::const_iterator it = om.begin(); it != om.end(); ++it)
|
||||
if (it->second.idx == idx)
|
||||
{
|
||||
const Option& o = it->second;
|
||||
cout << "\noption name " << it->first << " type " << o.type;
|
||||
os << "\noption name " << it->first << " type " << o.type;
|
||||
|
||||
if (o.type != "button")
|
||||
cout << " default " << o.defaultValue;
|
||||
os << " default " << o.defaultValue;
|
||||
|
||||
if (o.type == "spin")
|
||||
cout << " min " << o.minValue << " max " << o.maxValue;
|
||||
os << " min " << o.min << " max " << o.max;
|
||||
|
||||
break;
|
||||
}
|
||||
cout << endl;
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
/// Option class c'tors
|
||||
/// Option c'tors and conversion operators
|
||||
|
||||
Option::Option(const char* def) : type("string"), idx(Options.size()), minValue(0), maxValue(0)
|
||||
{ defaultValue = currentValue = def; }
|
||||
Option::Option(const char* v, Fn* f) : type("string"), min(0), max(0), idx(Options.size()), on_change(f)
|
||||
{ defaultValue = currentValue = v; }
|
||||
|
||||
Option::Option(bool def, string t) : type(t), idx(Options.size()), minValue(0), maxValue(0)
|
||||
{ defaultValue = currentValue = (def ? "true" : "false"); }
|
||||
Option::Option(bool v, Fn* f) : type("check"), min(0), max(0), idx(Options.size()), on_change(f)
|
||||
{ defaultValue = currentValue = (v ? "true" : "false"); }
|
||||
|
||||
Option::Option(int def, int minv, int maxv) : type("spin"), idx(Options.size()), minValue(minv), maxValue(maxv)
|
||||
{ defaultValue = currentValue = stringify(def); }
|
||||
Option::Option(Fn* f) : type("button"), min(0), max(0), idx(Options.size()), on_change(f)
|
||||
{}
|
||||
|
||||
Option::Option(int v, int minv, int maxv, Fn* f) : type("spin"), min(minv), max(maxv), idx(Options.size()), on_change(f)
|
||||
{ std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); }
|
||||
|
||||
|
||||
/// set_value() updates currentValue of the Option object. Normally it's up to
|
||||
/// the GUI to check for option's limits, but we could receive the new value
|
||||
/// directly from the user by teminal window. So let's check the bounds anyway.
|
||||
Option::operator int() const {
|
||||
assert(type == "check" || type == "spin");
|
||||
return (type == "spin" ? atoi(currentValue.c_str()) : currentValue == "true");
|
||||
}
|
||||
|
||||
void Option::set_value(const string& value) {
|
||||
Option::operator std::string() const {
|
||||
assert(type == "string");
|
||||
return currentValue;
|
||||
}
|
||||
|
||||
|
||||
/// operator=() updates currentValue and triggers on_change() action. It's up to
|
||||
/// the GUI to check for option's limits, but we could receive the new value from
|
||||
/// the user by console window, so let's check the bounds anyway.
|
||||
|
||||
Option& Option::operator=(const string& v) {
|
||||
|
||||
assert(!type.empty());
|
||||
|
||||
if ( (type == "check" || type == "button")
|
||||
&& !(value == "true" || value == "false"))
|
||||
return;
|
||||
if ( (type != "button" && v.empty())
|
||||
|| (type == "check" && v != "true" && v != "false")
|
||||
|| (type == "spin" && (atoi(v.c_str()) < min || atoi(v.c_str()) > max)))
|
||||
return *this;
|
||||
|
||||
if (type == "spin")
|
||||
{
|
||||
int v = atoi(value.c_str());
|
||||
if (v < minValue || v > maxValue)
|
||||
return;
|
||||
}
|
||||
if (type != "button")
|
||||
currentValue = v;
|
||||
|
||||
currentValue = value;
|
||||
if (on_change)
|
||||
(*on_change)(*this);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace UCI
|
||||
|
||||
+37
-48
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2013 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
|
||||
@@ -17,64 +17,53 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(UCIOPTION_H_INCLUDED)
|
||||
#ifndef UCIOPTION_H_INCLUDED
|
||||
#define UCIOPTION_H_INCLUDED
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class Option {
|
||||
public:
|
||||
Option() {} // To allow insertion in a std::map
|
||||
Option(const char* defaultValue);
|
||||
Option(bool defaultValue, std::string type = "check");
|
||||
Option(int defaultValue, int minValue, int maxValue);
|
||||
namespace UCI {
|
||||
|
||||
void set_value(const std::string& value);
|
||||
template<typename T> T value() const;
|
||||
class Option;
|
||||
|
||||
private:
|
||||
friend void init_uci_options();
|
||||
friend void print_uci_options();
|
||||
|
||||
std::string defaultValue, currentValue, type;
|
||||
size_t idx;
|
||||
int minValue, maxValue;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline T Option::value() const {
|
||||
|
||||
assert(type == "spin");
|
||||
return T(atoi(currentValue.c_str()));
|
||||
}
|
||||
|
||||
template<>
|
||||
inline std::string Option::value<std::string>() const {
|
||||
|
||||
assert(type == "string");
|
||||
return currentValue;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline bool Option::value<bool>() const {
|
||||
|
||||
assert(type == "check" || type == "button");
|
||||
return currentValue == "true";
|
||||
}
|
||||
|
||||
|
||||
// Custom comparator because UCI options should not be case sensitive
|
||||
/// Custom comparator because UCI options should be case insensitive
|
||||
struct CaseInsensitiveLess {
|
||||
bool operator() (const std::string&, const std::string&) const;
|
||||
};
|
||||
|
||||
/// Our options container is actually a std::map
|
||||
typedef std::map<std::string, Option, CaseInsensitiveLess> OptionsMap;
|
||||
|
||||
extern OptionsMap Options;
|
||||
extern void init_uci_options();
|
||||
extern void print_uci_options();
|
||||
/// Option class implements an option as defined by UCI protocol
|
||||
class Option {
|
||||
|
||||
#endif // !defined(UCIOPTION_H_INCLUDED)
|
||||
typedef void (Fn)(const Option&);
|
||||
|
||||
public:
|
||||
Option(Fn* = NULL);
|
||||
Option(bool v, Fn* = NULL);
|
||||
Option(const char* v, Fn* = NULL);
|
||||
Option(int v, int min, int max, Fn* = NULL);
|
||||
|
||||
Option& operator=(const std::string& v);
|
||||
operator int() const;
|
||||
operator std::string() const;
|
||||
|
||||
private:
|
||||
friend std::ostream& operator<<(std::ostream&, const OptionsMap&);
|
||||
|
||||
std::string defaultValue, currentValue, type;
|
||||
int min, max;
|
||||
size_t idx;
|
||||
Fn* on_change;
|
||||
};
|
||||
|
||||
void init(OptionsMap&);
|
||||
void loop(const std::string&);
|
||||
|
||||
} // namespace UCI
|
||||
|
||||
extern UCI::OptionsMap Options;
|
||||
|
||||
#endif // #ifndef UCIOPTION_H_INCLUDED
|
||||
|
||||
-111
@@ -1,111 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2010 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/>.
|
||||
*/
|
||||
|
||||
|
||||
#if !defined(VALUE_H_INCLUDED)
|
||||
#define VALUE_H_INCLUDED
|
||||
|
||||
////
|
||||
//// Types
|
||||
////
|
||||
|
||||
enum ValueType {
|
||||
VALUE_TYPE_NONE = 0,
|
||||
VALUE_TYPE_UPPER = 1, // Upper bound
|
||||
VALUE_TYPE_LOWER = 2, // Lower bound
|
||||
VALUE_TYPE_EXACT = VALUE_TYPE_UPPER | VALUE_TYPE_LOWER
|
||||
};
|
||||
|
||||
|
||||
enum Value {
|
||||
VALUE_ZERO = 0,
|
||||
VALUE_DRAW = 0,
|
||||
VALUE_KNOWN_WIN = 15000,
|
||||
VALUE_MATE = 30000,
|
||||
VALUE_INFINITE = 30001,
|
||||
VALUE_NONE = 30002,
|
||||
VALUE_ENSURE_SIGNED = -1
|
||||
};
|
||||
|
||||
ENABLE_OPERATORS_ON(Value);
|
||||
|
||||
|
||||
enum ScaleFactor {
|
||||
SCALE_FACTOR_ZERO = 0,
|
||||
SCALE_FACTOR_NORMAL = 64,
|
||||
SCALE_FACTOR_MAX = 128,
|
||||
SCALE_FACTOR_NONE = 255
|
||||
};
|
||||
|
||||
|
||||
/// Score enum keeps a midgame and an endgame value in a single
|
||||
/// integer (enum), first LSB 16 bits are used to store endgame
|
||||
/// value, while upper bits are used for midgame value.
|
||||
|
||||
// Compiler is free to choose the enum type as long as can keep
|
||||
// its data, so ensure Score to be an integer type.
|
||||
enum Score {
|
||||
SCORE_ZERO = 0,
|
||||
SCORE_ENSURE_32_BITS_SIZE_P = (1 << 16),
|
||||
SCORE_ENSURE_32_BITS_SIZE_N = -(1 << 16)
|
||||
};
|
||||
|
||||
ENABLE_OPERATORS_ON(Score);
|
||||
|
||||
|
||||
// Extracting the _signed_ lower and upper 16 bits it not so trivial
|
||||
// because according to the standard a simple cast to short is
|
||||
// implementation defined and so is a right shift of a signed integer.
|
||||
inline Value mg_value(Score s) { return Value(((int(s) + 32768) & ~0xffff) / 0x10000); }
|
||||
|
||||
// Unfortunatly on Intel 64 bit we have a small speed regression, so use a faster code in
|
||||
// this case, although not 100% standard compliant it seems to work for Intel and MSVC.
|
||||
#if defined(IS_64BIT) && (!defined(__GNUC__) || defined(__INTEL_COMPILER))
|
||||
inline Value eg_value(Score s) { return Value(int16_t(s & 0xffff)); }
|
||||
#else
|
||||
inline Value eg_value(Score s) { return Value((int)(unsigned(s) & 0x7fffu) - (int)(unsigned(s) & 0x8000u)); }
|
||||
#endif
|
||||
|
||||
inline Score make_score(int mg, int eg) { return Score((mg << 16) + eg); }
|
||||
|
||||
// Division must be handled separately for each term
|
||||
inline Score operator/(Score s, int i) { return make_score(mg_value(s) / i, eg_value(s) / i); }
|
||||
|
||||
// Only declared but not defined. We don't want to multiply two scores due to
|
||||
// a very high risk of overflow. So user should explicitly convert to integer.
|
||||
inline Score operator*(Score s1, Score s2);
|
||||
|
||||
|
||||
////
|
||||
//// Inline functions
|
||||
////
|
||||
|
||||
inline Value operator+ (Value v, int i) { return Value(int(v) + i); }
|
||||
inline Value operator- (Value v, int i) { return Value(int(v) - i); }
|
||||
|
||||
|
||||
inline Value value_mate_in(int ply) {
|
||||
return VALUE_MATE - ply;
|
||||
}
|
||||
|
||||
inline Value value_mated_in(int ply) {
|
||||
return -VALUE_MATE + ply;
|
||||
}
|
||||
|
||||
#endif // !defined(VALUE_H_INCLUDED)
|
||||
Reference in New Issue
Block a user