mirror of
https://github.com/opelly27/Stockfish.git
synced 2026-05-20 09:47:46 +00:00
Compare commits
800 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 54f8a9cb13 | |||
| ad937d0b2d | |||
| 495a0fa699 | |||
| 585655b16e | |||
| e49eb67119 | |||
| 88b5100e29 | |||
| 40f5abba10 | |||
| 5e03734eac | |||
| e46a72dd1d | |||
| 5ec63eb6b6 | |||
| a3c8c4b70d | |||
| 696d6cedb9 | |||
| 9f843adf89 | |||
| bfd8704a7d | |||
| f89a8f0769 | |||
| 6ba1d3ead6 | |||
| 7e3dba4f4c | |||
| f6e98a924a | |||
| 8f6a494ad7 | |||
| b8e6f83cfb | |||
| 5413fda739 | |||
| 145d293142 | |||
| f1240483fb | |||
| c0d3010438 | |||
| a9e93fa6a5 | |||
| 9e8bf82350 | |||
| dc87ec7258 | |||
| 43973f43c6 | |||
| bee4f1cf09 | |||
| da91a34c09 | |||
| 626dc8a03b | |||
| cf50e265fa | |||
| d3ffd0ffca | |||
| db229504e2 | |||
| 918c29f83a | |||
| a1f39c1ef9 | |||
| 9f2a64abd2 | |||
| 93e3b06fe2 | |||
| 86c20416c8 | |||
| c9e396b542 | |||
| fe23f27086 | |||
| de6fe2cb4e | |||
| 057c3d60cd | |||
| cb4ac4221e | |||
| 55604f156b | |||
| 7ddbcf7e87 | |||
| 3705559fdb | |||
| 6579a65bbb | |||
| f70cef3b79 | |||
| a89b26bedd | |||
| 8bfb53efe2 | |||
| a66e6e5ad9 | |||
| 56273fca1e | |||
| ef43e6b05d | |||
| 223ebe7b40 | |||
| 0e8ad40ef0 | |||
| eced15fe36 | |||
| a4d058bca2 | |||
| 619d66b7ab | |||
| 9073866491 | |||
| 7bce8831d3 | |||
| 81a8c1118b | |||
| b2c0634d48 | |||
| ada55c5d0a | |||
| 17cb7e7fa3 | |||
| 800ba28e83 | |||
| 0b2794ae12 | |||
| 226bbc1e63 | |||
| da2f8880b9 | |||
| c556fe1d71 | |||
| 2bfe61c33b | |||
| 2f92e3b525 | |||
| 0510112f91 | |||
| 4597324572 | |||
| ce6b7a1b85 | |||
| e3b54235ad | |||
| 8863afeb84 | |||
| 5c75455c8e | |||
| 0d8a4c7565 | |||
| 64d29a6330 | |||
| fcf2a34080 | |||
| 698b645e10 | |||
| d28ea7b518 | |||
| be641e881f | |||
| 3b19ea6ae5 | |||
| ccf59b0228 | |||
| 299afcd886 | |||
| c15b132f03 | |||
| 0ba814b3ca | |||
| d2caba1f66 | |||
| 1efc19ade0 | |||
| 678425f274 | |||
| 422c9c2acd | |||
| 9350d0dce5 | |||
| 4833887842 | |||
| f811a5693e | |||
| af0c13ba6a | |||
| c7cf45241c | |||
| 865b71309c | |||
| e7362dae78 | |||
| 9e72e35942 | |||
| bc183b0c04 | |||
| ffdf63ff7c | |||
| f12449d492 | |||
| 441a34a1c6 | |||
| 5f12069cbf | |||
| 4eee603433 | |||
| c714b90594 | |||
| aab5863dd4 | |||
| fa3f6dcbea | |||
| a091ae4cc8 | |||
| a1a7bc84da | |||
| 6e4b4c42ed | |||
| 142874b058 | |||
| 36c381154b | |||
| d0587f2c7f | |||
| 1f4798a173 | |||
| 61e4443342 | |||
| 6571acffaa | |||
| 6f3d787692 | |||
| 1e032ece92 | |||
| cc3002ff04 | |||
| c40d4e0133 | |||
| ca0804dfe4 | |||
| d2a8ba3299 | |||
| 20ff12e1be | |||
| 5cf9e0b254 | |||
| 1d1b7df7c6 | |||
| 13b9e1e098 | |||
| 553ead429d | |||
| bbd69c0260 | |||
| 708cb311a0 | |||
| 3e5470d88f | |||
| 3d8c0f16c2 | |||
| de2ba70830 | |||
| 9f0485e343 | |||
| b917cd275e | |||
| 0949f06a60 | |||
| 012f20d66e | |||
| b8cfc255d4 | |||
| 9fcefb2760 | |||
| 64a71c3c02 | |||
| 7b0a2f2a90 | |||
| 17ffc22279 | |||
| 52ebf87238 | |||
| d91079d4b0 | |||
| 62c0dc5dea | |||
| 7bdb8c9c5c | |||
| 3336963185 | |||
| 4808d15a85 | |||
| 67f88f5e3e | |||
| b3470d7ab1 | |||
| e4695f15bc | |||
| ffd6685f79 | |||
| 72e8640f4d | |||
| e6523e56b8 | |||
| b534176d4a | |||
| ddeb01612b | |||
| 41641e3b1e | |||
| 2f5aaf7de6 | |||
| d48a304262 | |||
| 0a1092f64e | |||
| 399968f1d0 | |||
| e5c3effdb1 | |||
| e88ef801af | |||
| d8796fe7e9 | |||
| 77341f67f3 | |||
| 93f95cc936 | |||
| f434cea287 | |||
| 216972186e | |||
| 074c7a3c30 | |||
| cf95a55d76 | |||
| a08a21d5a0 | |||
| cabd512916 | |||
| 40c863d41a | |||
| be61edbd19 | |||
| df201175c6 | |||
| 53ab32ef0b | |||
| 402a7ae151 | |||
| 3d58092ec1 | |||
| db05b1f9f5 | |||
| 262c380c4b | |||
| 6a6fd0b5f5 | |||
| f14cd1bb89 | |||
| a5869d8d25 | |||
| 45dbd9cd03 | |||
| a646f74e6a | |||
| 1fee23a598 | |||
| 1574428f64 | |||
| 0118623495 | |||
| 8454d871ec | |||
| 153309e287 | |||
| c9dcda6ac4 | |||
| 29e0d8caa7 | |||
| a795187c50 | |||
| 92faa74dfa | |||
| c5d478b923 | |||
| 9b1d594456 | |||
| 71440cf77b | |||
| 082fbf4264 | |||
| f7742669cb | |||
| 59a9bc9351 | |||
| 8ca80cb0f1 | |||
| 14aebe2b7c | |||
| cf0a2a26a9 | |||
| 899a2c033e | |||
| 8aa4f3fde4 | |||
| 26689d8c2a | |||
| f196c1bad8 | |||
| e460ab74ad | |||
| f5727deee3 | |||
| b96079f86b | |||
| 86347100a5 | |||
| 8e9d4081ee | |||
| 4630ab5743 | |||
| 1ff5ce8863 | |||
| b71418defa | |||
| 47b89f2e37 | |||
| 431c3ac485 | |||
| 190aea4cdc | |||
| f026517e5e | |||
| b71cedb2b0 | |||
| 69a14554ee | |||
| 2408243cf4 | |||
| 342fd6385b | |||
| e6c9ce6358 | |||
| c7e7d9217b | |||
| a8af78c833 | |||
| 13a73f67c0 | |||
| 500b9b0eb3 | |||
| f99cb3dc27 | |||
| dd4e5db2be | |||
| 5f2bf91ad1 | |||
| 034a2b04f2 | |||
| 6ea5dc294c | |||
| 8f5deaea61 | |||
| c5bb9b9da9 | |||
| 67e5581e37 | |||
| 3ede7daab3 | |||
| dbd6156fce | |||
| 691ed425ba | |||
| c376ffce0f | |||
| 917944e9c5 | |||
| d9c7cad630 | |||
| 9763c69fa5 | |||
| 555d9a8711 | |||
| b9768b8bc5 | |||
| 4ef6b2c32a | |||
| 1d18647e73 | |||
| a518d5d3ad | |||
| 091aff0445 | |||
| 3ed86ed3f9 | |||
| e4d34e1815 | |||
| 42caebfaa5 | |||
| eed508b444 | |||
| 343544f3f7 | |||
| 13d1f0ae43 | |||
| ecd07e51d0 | |||
| 52ae0efccf | |||
| 53c04c0429 | |||
| 1064288b38 | |||
| d34bb889b1 | |||
| a3a0df92a3 | |||
| e8f9447b11 | |||
| 3cc47edf62 | |||
| d454cd4216 | |||
| 48f38f3092 | |||
| 281472e50e | |||
| f011a5af11 | |||
| 67b0da83da | |||
| 3674f18b97 | |||
| 72f7282ad4 | |||
| fbfce2132a | |||
| 2c825294ec | |||
| f86d2aee29 | |||
| 35ea39bed2 | |||
| 97015afce8 | |||
| f22a63ce67 | |||
| c08e7419a0 | |||
| 67f91bc5ea | |||
| 4bc2374450 | |||
| f5e872a0e3 | |||
| 25cb851f8a | |||
| 3cddb0c076 | |||
| 2bf18bfc63 | |||
| 5aeb907fa1 | |||
| 0c68971c13 | |||
| d9be00342c | |||
| 027d85e82a | |||
| 3bc3c069f1 | |||
| e9366fa155 | |||
| 549b5c478f | |||
| b15e148b5e | |||
| bb83a417cb | |||
| 984ee9d05b | |||
| a0cc15ccbc | |||
| 7f142d6817 | |||
| bd1c3ed7e3 | |||
| cca34e234c | |||
| ed95ad1c0e | |||
| c65d67feb5 | |||
| bc6faf633e | |||
| 8d1c1074d5 | |||
| e654209211 | |||
| b742a3f29a | |||
| 800410eef1 | |||
| 7b2cda95d9 | |||
| c2cefa6de0 | |||
| d7f5f15d69 | |||
| 84906b83ec | |||
| 274079990a | |||
| 10cb19d534 | |||
| d50b33cacd | |||
| 77b5ee0117 | |||
| af750bd2ef | |||
| 21cbfafc03 | |||
| d3947b2f3e | |||
| a71209868b | |||
| 7a1ff6d8ff | |||
| 82f6779c2e | |||
| 3abccdc82d | |||
| 8d6d0223bf | |||
| fc17d0de77 | |||
| 27f2ce8f6e | |||
| 45b0aea875 | |||
| 4803d5772c | |||
| 738c5595ad | |||
| bebd6e16f6 | |||
| 49e110c52b | |||
| 4d90aeb0ab | |||
| 03cd049c68 | |||
| 6ab8b9b6c6 | |||
| 490f67a3f8 | |||
| 0515ad0fb0 | |||
| bf51db2526 | |||
| 59702aca0d | |||
| c86eee3918 | |||
| 10b53e1c5e | |||
| a30d3571ca | |||
| 457ac26de5 | |||
| 679c2ea227 | |||
| 9ff594c3a9 | |||
| 849b089a63 | |||
| aee404f532 | |||
| 0915f85895 | |||
| 5e8bc6ac2a | |||
| 3e4dcaa06e | |||
| 14f47c8ac6 | |||
| 5d90c149b5 | |||
| 4b9e338541 | |||
| a0cf424cfc | |||
| aecdbfc4a0 | |||
| e6482b7d97 | |||
| 6e6c5b6103 | |||
| 04fd7efdfa | |||
| 5e331f9618 | |||
| b9f5d1c6ff | |||
| c4533e0d94 | |||
| f39cf1b008 | |||
| c6baefb79d | |||
| 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 |
+19
-30
@@ -1,5 +1,4 @@
|
||||
1. Introduction
|
||||
---------------
|
||||
### 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
|
||||
@@ -8,46 +7,37 @@ 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 32 CPUs, but has not been
|
||||
tested thoroughly with more than 4. The program tries to detect the
|
||||
number of CPUs on your computer and sets 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 eight threads, it is
|
||||
recommended to raise the value of the "Min Split Depth" UCI parameter to
|
||||
7.
|
||||
This version of Stockfish supports up to 128 CPUs. The engine defaults
|
||||
to one search thread, so it is therefore recommended to inspect the value of
|
||||
the *Threads* UCI parameter, and to make sure it equals the number of CPU
|
||||
cores on your computer.
|
||||
|
||||
|
||||
2. Files
|
||||
--------
|
||||
### Files
|
||||
|
||||
This distribution of Stockfish consists of the following files:
|
||||
|
||||
* Readme.txt, the file you are currently reading.
|
||||
* 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 4 below.
|
||||
* 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.
|
||||
|
||||
|
||||
3. Opening books
|
||||
----------------
|
||||
### 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"
|
||||
documentation. The book file can be selected by setting the *Book File*
|
||||
UCI parameter.
|
||||
|
||||
|
||||
4. Compiling it yourself
|
||||
------------------------
|
||||
### Compiling it yourself
|
||||
|
||||
On Unix-like systems, it should be possible to compile Stockfish
|
||||
directly from the source code with the included Makefile.
|
||||
@@ -55,17 +45,16 @@ 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
|
||||
In general it is recommended to run `make help` to see a list of make
|
||||
targets with corresponding descriptions. When not using the Makefile to
|
||||
compile (for instance with Microsoft MSVC) you need to manually
|
||||
set/unset some switches in the compiler command line; see file "types.h"
|
||||
set/unset some switches in the compiler command line; see file *types.h*
|
||||
for a quick reference.
|
||||
|
||||
|
||||
5. Terms of use
|
||||
---------------
|
||||
### Terms of use
|
||||
|
||||
Stockfish is free, and distributed under the GNU General Public License
|
||||
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
|
||||
@@ -78,4 +67,4 @@ to where the source code can be found. If you make any changes to the
|
||||
source code, these changes must also be made available under the GPL.
|
||||
|
||||
For full details, read the copy of the GPL found in the file named
|
||||
Copying.txt.
|
||||
*Copying.txt*
|
||||
+10
-10
@@ -1,4 +1,3 @@
|
||||
|
||||
[PolyGlot]
|
||||
|
||||
EngineDir = .
|
||||
@@ -15,21 +14,22 @@ ResignScore = 600
|
||||
|
||||
[Engine]
|
||||
|
||||
Use Search Log = false
|
||||
Write Debug Log = false
|
||||
Write Search Log = false
|
||||
Search Log Filename = SearchLog.txt
|
||||
Book File = book.bin
|
||||
Best Book Move = false
|
||||
Mobility (Middle Game) = 100
|
||||
Contempt Factor = 0
|
||||
Mobility (Midgame) = 100
|
||||
Mobility (Endgame) = 100
|
||||
Passed Pawns (Middle Game) = 100
|
||||
Pawn Structure (Midgame) = 100
|
||||
Pawn Structure (Endgame) = 100
|
||||
Passed Pawns (Midgame) = 100
|
||||
Passed Pawns (Endgame) = 100
|
||||
Space = 100
|
||||
Aggressiveness = 100
|
||||
Cowardice = 100
|
||||
Min Split Depth = 4
|
||||
Max Threads per Split Point = 5
|
||||
King Safety = 100
|
||||
Min Split Depth = 0
|
||||
Threads = 1
|
||||
Use Sleeping Threads = false
|
||||
Hash = 128
|
||||
Ponder = true
|
||||
OwnBook = false
|
||||
@@ -39,5 +39,5 @@ 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
|
||||
|
||||
+144
-162
@@ -1,6 +1,6 @@
|
||||
# Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
# Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
# Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
# Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
#
|
||||
# Stockfish is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
@@ -35,7 +35,7 @@ endif
|
||||
BINDIR = $(PREFIX)/bin
|
||||
|
||||
### Built-in benchmark for pgo-builds
|
||||
PGOBENCH = ./$(EXE) bench 32 1 10 default depth
|
||||
PGOBENCH = ./$(EXE) bench 32 1 1 default time
|
||||
|
||||
### Object files
|
||||
OBJS = benchmark.o bitbase.o bitboard.o book.o endgame.o evaluate.o main.o \
|
||||
@@ -58,108 +58,85 @@ OBJS = benchmark.o bitbase.o bitboard.o book.o endgame.o evaluate.o main.o \
|
||||
# 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
|
||||
# pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
|
||||
#
|
||||
# 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
|
||||
# at the end of the line for flag values.
|
||||
|
||||
### 2.1. General
|
||||
debug = no
|
||||
### 2.1. General and architecture defaults
|
||||
optimize = yes
|
||||
debug = no
|
||||
os = any
|
||||
bits = 32
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
sse = no
|
||||
pext = no
|
||||
|
||||
### 2.2 Architecture specific
|
||||
|
||||
# General-section
|
||||
ifeq ($(ARCH),general-64)
|
||||
arch = any
|
||||
os = any
|
||||
bits = 64
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),general-32)
|
||||
arch = any
|
||||
os = any
|
||||
bits = 32
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
endif
|
||||
|
||||
# x86-section
|
||||
ifeq ($(ARCH),x86-64)
|
||||
arch = x86_64
|
||||
os = any
|
||||
bits = 64
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
popcnt = no
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),x86-64-modern)
|
||||
arch = x86_64
|
||||
os = any
|
||||
bits = 64
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
popcnt = yes
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),x86-32)
|
||||
arch = i386
|
||||
os = any
|
||||
bits = 32
|
||||
prefetch = yes
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),x86-32-old)
|
||||
arch = i386
|
||||
os = any
|
||||
bits = 32
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
endif
|
||||
|
||||
# osx-section
|
||||
ifeq ($(ARCH),osx-ppc-64)
|
||||
arch = ppc64
|
||||
os = osx
|
||||
ifeq ($(ARCH),x86-32)
|
||||
arch = i386
|
||||
prefetch = yes
|
||||
sse = yes
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),general-64)
|
||||
arch = any
|
||||
bits = 64
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),osx-ppc-32)
|
||||
arch = ppc
|
||||
os = osx
|
||||
bits = 32
|
||||
prefetch = no
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),osx-x86-64)
|
||||
ifeq ($(ARCH),x86-64)
|
||||
arch = x86_64
|
||||
os = osx
|
||||
bits = 64
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
popcnt = no
|
||||
sse = yes
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),osx-x86-32)
|
||||
arch = i386
|
||||
os = osx
|
||||
bits = 32
|
||||
ifeq ($(ARCH),x86-64-modern)
|
||||
arch = x86_64
|
||||
bits = 64
|
||||
prefetch = yes
|
||||
bsfq = no
|
||||
popcnt = no
|
||||
bsfq = yes
|
||||
popcnt = yes
|
||||
sse = yes
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),x86-64-bmi2)
|
||||
arch = x86_64
|
||||
bits = 64
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
popcnt = yes
|
||||
sse = yes
|
||||
pext = yes
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),armv7)
|
||||
arch = armv7
|
||||
prefetch = yes
|
||||
bsfq = yes
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),ppc-32)
|
||||
arch = ppc
|
||||
endif
|
||||
|
||||
ifeq ($(ARCH),ppc-64)
|
||||
arch = ppc64
|
||||
bits = 64
|
||||
endif
|
||||
|
||||
|
||||
@@ -168,87 +145,72 @@ endif
|
||||
### ==========================================================================
|
||||
|
||||
### 3.1 Selecting compiler (default = gcc)
|
||||
|
||||
CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti $(EXTRACXXFLAGS)
|
||||
LDFLAGS += $(EXTRALDFLAGS)
|
||||
|
||||
ifeq ($(COMP),)
|
||||
COMP=gcc
|
||||
endif
|
||||
|
||||
ifeq ($(COMP),mingw)
|
||||
comp=mingw
|
||||
CXX=g++
|
||||
profile_prepare = gcc-profile-prepare
|
||||
profile_make = gcc-profile-make
|
||||
profile_use = gcc-profile-use
|
||||
profile_clean = gcc-profile-clean
|
||||
endif
|
||||
|
||||
ifeq ($(COMP),gcc)
|
||||
comp=gcc
|
||||
CXX=g++
|
||||
profile_prepare = gcc-profile-prepare
|
||||
profile_make = gcc-profile-make
|
||||
profile_use = gcc-profile-use
|
||||
profile_clean = gcc-profile-clean
|
||||
CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
|
||||
endif
|
||||
|
||||
ifeq ($(COMP),mingw)
|
||||
comp=mingw
|
||||
CXX=g++
|
||||
CXXFLAGS += -Wextra -Wshadow
|
||||
LDFLAGS += -static-libstdc++ -static-libgcc
|
||||
endif
|
||||
|
||||
ifeq ($(COMP),icc)
|
||||
comp=icc
|
||||
CXX=icpc
|
||||
profile_prepare = icc-profile-prepare
|
||||
profile_make = icc-profile-make
|
||||
profile_use = icc-profile-use
|
||||
profile_clean = icc-profile-clean
|
||||
CXXFLAGS += -diag-disable 1476,10120 -Wcheck -Wabi -Wdeprecated -strict-ansi
|
||||
endif
|
||||
|
||||
ifeq ($(COMP),clang)
|
||||
comp=clang
|
||||
CXX=clang++
|
||||
CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
|
||||
endif
|
||||
|
||||
ifeq ($(comp),icc)
|
||||
profile_prepare = icc-profile-prepare
|
||||
profile_make = icc-profile-make
|
||||
profile_use = icc-profile-use
|
||||
profile_clean = icc-profile-clean
|
||||
else
|
||||
profile_prepare = gcc-profile-prepare
|
||||
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)
|
||||
|
||||
ifeq ($(comp),gcc)
|
||||
CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow
|
||||
ifeq ($(UNAME),Darwin)
|
||||
CXXFLAGS += -arch $(arch) -mmacosx-version-min=10.6
|
||||
LDFLAGS += -arch $(arch) -mmacosx-version-min=10.6
|
||||
endif
|
||||
|
||||
ifeq ($(comp),mingw)
|
||||
CXXFLAGS += -Wextra -Wshadow
|
||||
endif
|
||||
|
||||
ifeq ($(comp),icc)
|
||||
CXXFLAGS += -wd383,981,1418,1419,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)
|
||||
endif
|
||||
|
||||
### 3.3 General linker settings
|
||||
LDFLAGS = $(EXTRALDFLAGS)
|
||||
|
||||
### 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
|
||||
# On Android Bionic's C library comes with its own pthread implementation bundled in
|
||||
ifneq ($(arch),armv7)
|
||||
# Haiku has pthreads in its libroot, so only link it in on other platforms
|
||||
ifneq ($(UNAME),Haiku)
|
||||
LDFLAGS += -lpthread
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(os),osx)
|
||||
LDFLAGS += -arch $(arch)
|
||||
endif
|
||||
|
||||
### 3.4 Debugging
|
||||
ifeq ($(debug),no)
|
||||
CXXFLAGS += -DNDEBUG
|
||||
else
|
||||
CXXFLAGS += -g
|
||||
endif
|
||||
|
||||
### 3.5 Optimization
|
||||
@@ -257,7 +219,7 @@ ifeq ($(optimize),yes)
|
||||
ifeq ($(comp),gcc)
|
||||
CXXFLAGS += -O3
|
||||
|
||||
ifeq ($(os),osx)
|
||||
ifeq ($(UNAME),Darwin)
|
||||
ifeq ($(arch),i386)
|
||||
CXXFLAGS += -mdynamic-no-pic
|
||||
endif
|
||||
@@ -265,6 +227,10 @@ ifeq ($(optimize),yes)
|
||||
CXXFLAGS += -mdynamic-no-pic
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(arch),armv7)
|
||||
CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(comp),mingw)
|
||||
@@ -272,18 +238,21 @@ ifeq ($(optimize),yes)
|
||||
endif
|
||||
|
||||
ifeq ($(comp),icc)
|
||||
ifeq ($(os),osx)
|
||||
ifeq ($(UNAME),Darwin)
|
||||
CXXFLAGS += -fast -mdynamic-no-pic
|
||||
else
|
||||
CXXFLAGS += -O3
|
||||
CXXFLAGS += -fast
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(comp),clang)
|
||||
### -O4 requires a linker that supports LLVM's LTO
|
||||
CXXFLAGS += -O3
|
||||
|
||||
ifeq ($(os),osx)
|
||||
ifeq ($(UNAME),Darwin)
|
||||
ifeq ($(pext),no)
|
||||
CXXFLAGS += -flto
|
||||
LDFLAGS += $(CXXFLAGS)
|
||||
endif
|
||||
ifeq ($(arch),i386)
|
||||
CXXFLAGS += -mdynamic-no-pic
|
||||
endif
|
||||
@@ -301,8 +270,10 @@ endif
|
||||
|
||||
### 3.7 prefetch
|
||||
ifeq ($(prefetch),yes)
|
||||
CXXFLAGS += -msse
|
||||
DEPENDFLAGS += -msse
|
||||
ifeq ($(sse),yes)
|
||||
CXXFLAGS += -msse
|
||||
DEPENDFLAGS += -msse
|
||||
endif
|
||||
else
|
||||
CXXFLAGS += -DNO_PREFETCH
|
||||
endif
|
||||
@@ -317,11 +288,20 @@ ifeq ($(popcnt),yes)
|
||||
CXXFLAGS += -msse3 -DUSE_POPCNT
|
||||
endif
|
||||
|
||||
### 3.10 Link Time Optimization, it works since gcc 4.5 but not on mingw.
|
||||
### 3.10 pext
|
||||
ifeq ($(pext),yes)
|
||||
CXXFLAGS += -DUSE_PEXT
|
||||
ifeq ($(comp),$(filter $(comp),gcc clang mingw))
|
||||
CXXFLAGS += -mbmi -mbmi2
|
||||
endif
|
||||
endif
|
||||
|
||||
### 3.11 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 \)))
|
||||
@@ -329,6 +309,7 @@ ifeq ($(comp),gcc)
|
||||
LDFLAGS += $(CXXFLAGS)
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
### ==========================================================================
|
||||
@@ -343,43 +324,43 @@ help:
|
||||
@echo ""
|
||||
@echo "Supported targets:"
|
||||
@echo ""
|
||||
@echo "build > Build unoptimized version"
|
||||
@echo "profile-build > Build PGO-optimized version"
|
||||
@echo "strip > Strip executable"
|
||||
@echo "install > Install executable"
|
||||
@echo "clean > Clean up"
|
||||
@echo "testrun > Make sample run"
|
||||
@echo "build > Standard build"
|
||||
@echo "profile-build > PGO build"
|
||||
@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 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 "x86-64 > x86 64-bit"
|
||||
@echo "x86-64-modern > x86 64-bit with popcnt support"
|
||||
@echo "x86-64-bmi2 > x86 64-bit with pext support"
|
||||
@echo "x86-32 > x86 32-bit with SSE support"
|
||||
@echo "x86-32-old > x86 32-bit fall back for old hardware"
|
||||
@echo "ppc-64 > PPC 64-bit"
|
||||
@echo "ppc-32 > PPC 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 "clang > LLVM Clang compiler"
|
||||
@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
|
||||
build:
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
|
||||
$(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
|
||||
@@ -415,9 +396,6 @@ install:
|
||||
clean:
|
||||
$(RM) $(EXE) $(EXE).exe *.o .depend *~ core bench.txt *.gcda
|
||||
|
||||
testrun:
|
||||
@$(PGOBENCH)
|
||||
|
||||
default:
|
||||
help
|
||||
|
||||
@@ -438,6 +416,8 @@ config-sanity:
|
||||
@echo "prefetch: '$(prefetch)'"
|
||||
@echo "bsfq: '$(bsfq)'"
|
||||
@echo "popcnt: '$(popcnt)'"
|
||||
@echo "sse: '$(sse)'"
|
||||
@echo "pext: '$(pext)'"
|
||||
@echo ""
|
||||
@echo "Flags:"
|
||||
@echo "CXX: $(CXX)"
|
||||
@@ -449,12 +429,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 "$(os)" = "any" || test "$(os)" = "osx"
|
||||
test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "armv7"
|
||||
@test "$(os)" = "any"
|
||||
@test "$(bits)" = "32" || test "$(bits)" = "64"
|
||||
@test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
|
||||
@test "$(bsfq)" = "yes" || test "$(bsfq)" = "no"
|
||||
@test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
|
||||
@test "$(sse)" = "yes" || test "$(sse)" = "no"
|
||||
@test "$(pext)" = "yes" || test "$(pext)" = "no"
|
||||
@test "$(comp)" = "gcc" || test "$(comp)" = "icc" || test "$(comp)" = "mingw" || test "$(comp)" = "clang"
|
||||
|
||||
$(EXE): $(OBJS)
|
||||
|
||||
+48
-18
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "misc.h"
|
||||
#include "notation.h"
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
@@ -47,15 +48,29 @@ static const char* Defaults[] = {
|
||||
"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",
|
||||
"6k1/6p1/6Pp/ppp5/3pn2P/1P3K2/1PP2P2/3N4 b - - 0 1",
|
||||
"3b4/5kp1/1p1p1p1p/pP1PpP1P/P1P1P3/3KN3/8/8 w - - 0 1",
|
||||
"2K5/p7/7P/5pR1/8/5k2/r7/8 w - - 0 1",
|
||||
"8/6pk/1p6/8/PP3p1p/5P2/4KP1q/3Q4 w - - 0 1",
|
||||
"7k/3p2pp/4q3/8/4Q3/5Kp1/P6b/8 w - - 0 1",
|
||||
"8/2p5/8/2kPKp1p/2p4P/2P5/3P4/8 w - - 0 1",
|
||||
"8/1p3pp1/7p/5P1P/2k3P1/8/2K2P2/8 w - - 0 1",
|
||||
"8/pp2r1k1/2p1p3/3pP2p/1P1P1P1P/P5KR/8/8 w - - 0 1",
|
||||
"8/3p4/p1bk3p/Pp6/1Kp1PpPp/2P2P1P/2P5/5B2 b - - 0 1",
|
||||
"5k2/7R/4P2p/5K2/p1r2P1p/8/8/8 b - - 0 1",
|
||||
"6k1/6p1/P6p/r1N5/5p2/7P/1b3PP1/4R1K1 w - - 0 1",
|
||||
"1r3k2/4q3/2Pp3b/3Bp3/2Q2p2/1p1P2P1/1P2KP2/3N4 w - - 0 1",
|
||||
"6k1/4pp1p/3p2p1/P1pPb3/R7/1r2P1PP/3B1P2/6K1 w - - 0 1",
|
||||
"8/3p3B/5p2/5P2/p7/PP5b/k7/6K1 w - - 0 1"
|
||||
};
|
||||
|
||||
|
||||
/// 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
|
||||
/// depth 12), an optional file name where to look for positions in fen
|
||||
/// depth 13), an optional file name where to look for positions in FEN
|
||||
/// format (defaults are the positions defined above) and the type of the
|
||||
/// limit value: depth (default), time in secs or number of nodes.
|
||||
|
||||
@@ -66,9 +81,9 @@ void benchmark(const Position& current, istream& is) {
|
||||
vector<string> fens;
|
||||
|
||||
// Assign default values to missing arguments
|
||||
string ttSize = (is >> token) ? token : "128";
|
||||
string ttSize = (is >> token) ? token : "32";
|
||||
string threads = (is >> token) ? token : "1";
|
||||
string limit = (is >> token) ? token : "12";
|
||||
string limit = (is >> token) ? token : "13";
|
||||
string fenFile = (is >> token) ? token : "default";
|
||||
string limitType = (is >> token) ? token : "depth";
|
||||
|
||||
@@ -82,14 +97,17 @@ void benchmark(const Position& current, istream& is) {
|
||||
else if (limitType == "nodes")
|
||||
limits.nodes = atoi(limit.c_str());
|
||||
|
||||
else if (limitType == "mate")
|
||||
limits.mate = atoi(limit.c_str());
|
||||
|
||||
else
|
||||
limits.depth = atoi(limit.c_str());
|
||||
|
||||
if (fenFile == "default")
|
||||
fens.assign(Defaults, Defaults + 16);
|
||||
fens.assign(Defaults, Defaults + 30);
|
||||
|
||||
else if (fenFile == "current")
|
||||
fens.push_back(current.to_fen());
|
||||
fens.push_back(current.fen());
|
||||
|
||||
else
|
||||
{
|
||||
@@ -99,7 +117,7 @@ void benchmark(const Position& current, istream& is) {
|
||||
if (!file.is_open())
|
||||
{
|
||||
cerr << "Unable to open file " << fenFile << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
return;
|
||||
}
|
||||
|
||||
while (getline(file, fen))
|
||||
@@ -109,31 +127,43 @@ void benchmark(const Position& current, istream& is) {
|
||||
file.close();
|
||||
}
|
||||
|
||||
int64_t nodes = 0;
|
||||
uint64_t nodes = 0;
|
||||
Search::StateStackPtr st;
|
||||
Time::point elapsed = Time::now();
|
||||
|
||||
for (size_t i = 0; i < fens.size(); i++)
|
||||
for (size_t i = 0; i < fens.size(); ++i)
|
||||
{
|
||||
Position pos(fens[i], Options["UCI_Chess960"], Threads.main_thread());
|
||||
Position pos(fens[i], Options["UCI_Chess960"], Threads.main());
|
||||
|
||||
cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
|
||||
|
||||
if (limitType == "perft")
|
||||
if (limitType == "divide")
|
||||
for (MoveList<LEGAL> it(pos); *it; ++it)
|
||||
{
|
||||
StateInfo si;
|
||||
pos.do_move(*it, si);
|
||||
uint64_t cnt = limits.depth > 1 ? Search::perft(pos, (limits.depth - 1) * ONE_PLY) : 1;
|
||||
pos.undo_move(*it);
|
||||
cerr << move_to_uci(*it, pos.is_chess960()) << ": " << cnt << endl;
|
||||
nodes += cnt;
|
||||
}
|
||||
else if (limitType == "perft")
|
||||
{
|
||||
size_t cnt = Search::perft(pos, limits.depth * ONE_PLY);
|
||||
uint64_t cnt = Search::perft(pos, limits.depth * ONE_PLY);
|
||||
cerr << "\nPerft " << limits.depth << " leaf nodes: " << cnt << endl;
|
||||
nodes += cnt;
|
||||
}
|
||||
else
|
||||
{
|
||||
Threads.start_searching(pos, limits, vector<Move>(), st);
|
||||
Threads.wait_for_search_finished();
|
||||
nodes += Search::RootPosition.nodes_searched();
|
||||
Threads.start_thinking(pos, limits, st);
|
||||
Threads.wait_for_think_finished();
|
||||
nodes += Search::RootPos.nodes_searched();
|
||||
}
|
||||
}
|
||||
|
||||
elapsed = Time::now() - elapsed + 1; // Assure positive to avoid a 'divide by zero'
|
||||
elapsed = Time::now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero'
|
||||
|
||||
dbg_print(); // Just before to exit
|
||||
|
||||
cerr << "\n==========================="
|
||||
<< "\nTotal time (ms) : " << elapsed
|
||||
|
||||
+95
-150
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -18,12 +18,32 @@
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "types.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// There are 24 possible pawn squares: the first 4 files and ranks from 2 to 7
|
||||
const unsigned MAX_INDEX = 2*24*64*64; // stm * psq * wksq * bksq = 196608
|
||||
|
||||
// Each uint32_t stores results of 32 positions, one per bit
|
||||
uint32_t KPKBitbase[MAX_INDEX / 32];
|
||||
|
||||
// A KPK bitbase index is an integer in [0, IndexMax] range
|
||||
//
|
||||
// Information is mapped in a way that minimizes the 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 {
|
||||
INVALID = 0,
|
||||
UNKNOWN = 1,
|
||||
@@ -35,196 +55,121 @@ namespace {
|
||||
|
||||
struct KPKPosition {
|
||||
|
||||
Result classify_leaf(int idx);
|
||||
Result classify(int idx, Result db[]);
|
||||
KPKPosition(unsigned idx);
|
||||
operator Result() const { return result; }
|
||||
Result classify(const std::vector<KPKPosition>& db)
|
||||
{ return us == WHITE ? classify<WHITE>(db) : classify<BLACK>(db); }
|
||||
|
||||
private:
|
||||
template<Color Us> Result classify(const Result db[]) const;
|
||||
template<Color Us> Result classify(const std::vector<KPKPosition>& db);
|
||||
|
||||
template<Color Us> Bitboard k_attacks() const {
|
||||
return Us == WHITE ? StepAttacksBB[W_KING][wksq] : StepAttacksBB[B_KING][bksq];
|
||||
}
|
||||
|
||||
Bitboard p_attacks() const { return StepAttacksBB[W_PAWN][psq]; }
|
||||
void decode_index(int idx);
|
||||
|
||||
Square wksq, bksq, psq;
|
||||
Color stm;
|
||||
Color us;
|
||||
Square bksq, wksq, psq;
|
||||
Result result;
|
||||
};
|
||||
|
||||
// The possible pawns squares are 24, the first 4 files and ranks from 2 to 7
|
||||
const int IndexMax = 2 * 24 * 64 * 64; // stm * wp_sq * wk_sq * bk_sq = 196608
|
||||
|
||||
// Each uint32_t stores results of 32 positions, one per bit
|
||||
uint32_t KPKBitbase[IndexMax / 32];
|
||||
|
||||
int index(Square wksq, Square bksq, Square psq, Color stm);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
||||
uint32_t Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color stm) {
|
||||
bool Bitbases::probe_kpk(Square wksq, Square wpsq, Square bksq, Color us) {
|
||||
|
||||
int idx = index(wksq, bksq, wpsq, stm);
|
||||
return KPKBitbase[idx / 32] & (1 << (idx & 31));
|
||||
assert(file_of(wpsq) <= FILE_D);
|
||||
|
||||
unsigned idx = index(us, bksq, wksq, wpsq);
|
||||
return KPKBitbase[idx / 32] & (1 << (idx & 0x1F));
|
||||
}
|
||||
|
||||
|
||||
void Bitbases::init_kpk() {
|
||||
|
||||
Result db[IndexMax];
|
||||
KPKPosition pos;
|
||||
int idx, bit, repeat = 1;
|
||||
unsigned idx, repeat = 1;
|
||||
std::vector<KPKPosition> db;
|
||||
db.reserve(MAX_INDEX);
|
||||
|
||||
// Initialize table with known win / draw positions
|
||||
for (idx = 0; idx < IndexMax; idx++)
|
||||
db[idx] = pos.classify_leaf(idx);
|
||||
// Initialize db with known win / draw positions
|
||||
for (idx = 0; idx < MAX_INDEX; ++idx)
|
||||
db.push_back(KPKPosition(idx));
|
||||
|
||||
// Iterate until all positions are classified (30 cycles needed)
|
||||
// Iterate through the positions until none 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] = pos.classify(idx, db)) != UNKNOWN)
|
||||
repeat = 1;
|
||||
for (repeat = idx = 0; idx < MAX_INDEX; ++idx)
|
||||
repeat |= (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN);
|
||||
|
||||
// Map 32 position results into one KPKBitbase[] entry
|
||||
for (idx = 0; idx < IndexMax / 32; idx++)
|
||||
for (bit = 0; bit < 32; bit++)
|
||||
if (db[32 * idx + bit] == WIN)
|
||||
KPKBitbase[idx] |= 1 << bit;
|
||||
// Map 32 results into one KPKBitbase[] entry
|
||||
for (idx = 0; idx < MAX_INDEX; ++idx)
|
||||
if (db[idx] == WIN)
|
||||
KPKBitbase[idx / 32] |= 1 << (idx & 0x1F);
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
// A KPK bitbase index is an integer in [0, IndexMax] range
|
||||
//
|
||||
// Information is mapped in this way
|
||||
//
|
||||
// bit 0: side to move (WHITE or BLACK)
|
||||
// bit 1- 6: black king square (from SQ_A1 to SQ_H8)
|
||||
// bit 7-12: white king square (from SQ_A1 to SQ_H8)
|
||||
// bit 13-14: white pawn file (from FILE_A to FILE_D)
|
||||
// bit 15-17: white pawn rank - 1 (from RANK_2 - 1 to RANK_7 - 1)
|
||||
KPKPosition::KPKPosition(unsigned idx) {
|
||||
|
||||
int index(Square w, Square b, Square p, Color c) {
|
||||
|
||||
assert(file_of(p) <= FILE_D);
|
||||
|
||||
return c + (b << 1) + (w << 7) + (file_of(p) << 13) + ((rank_of(p) - 1) << 15);
|
||||
}
|
||||
|
||||
void KPKPosition::decode_index(int idx) {
|
||||
|
||||
stm = Color(idx & 1);
|
||||
bksq = Square((idx >> 1) & 63);
|
||||
wksq = Square((idx >> 7) & 63);
|
||||
psq = File((idx >> 13) & 3) | Rank((idx >> 15) + 1);
|
||||
}
|
||||
|
||||
Result KPKPosition::classify_leaf(int idx) {
|
||||
|
||||
decode_index(idx);
|
||||
wksq = Square((idx >> 0) & 0x3F);
|
||||
bksq = Square((idx >> 6) & 0x3F);
|
||||
us = Color ((idx >> 12) & 0x01);
|
||||
psq = make_square(File((idx >> 13) & 0x03), Rank(RANK_7 - (idx >> 15)));
|
||||
result = UNKNOWN;
|
||||
|
||||
// Check if two pieces are on the same square or if a king can be captured
|
||||
if ( wksq == psq || wksq == bksq || bksq == psq
|
||||
|| (k_attacks<WHITE>() & bksq)
|
||||
|| (stm == WHITE && (p_attacks() & bksq)))
|
||||
return INVALID;
|
||||
if ( square_distance(wksq, bksq) <= 1
|
||||
|| wksq == psq
|
||||
|| bksq == psq
|
||||
|| (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq)))
|
||||
result = INVALID;
|
||||
|
||||
// The position is an immediate win if it is white to move and the white
|
||||
// pawn can be promoted without getting captured.
|
||||
if ( rank_of(psq) == RANK_7
|
||||
&& stm == WHITE
|
||||
&& wksq != psq + DELTA_N
|
||||
&& ( square_distance(bksq, psq + DELTA_N) > 1
|
||||
||(k_attacks<WHITE>() & (psq + DELTA_N))))
|
||||
return WIN;
|
||||
|
||||
// Check for known draw positions
|
||||
//
|
||||
// Case 1: Stalemate
|
||||
if ( stm == BLACK
|
||||
&& !(k_attacks<BLACK>() & ~(k_attacks<WHITE>() | p_attacks())))
|
||||
return DRAW;
|
||||
|
||||
// Case 2: King can capture undefended pawn
|
||||
if ( stm == BLACK
|
||||
&& (k_attacks<BLACK>() & psq & ~k_attacks<WHITE>()))
|
||||
return DRAW;
|
||||
|
||||
// Case 3: Black king in front of white pawn
|
||||
if ( bksq == psq + DELTA_N
|
||||
&& rank_of(psq) < RANK_7)
|
||||
return DRAW;
|
||||
|
||||
// Case 4: White king in front of pawn and black has opposition
|
||||
if ( stm == WHITE
|
||||
&& wksq == psq + DELTA_N
|
||||
&& bksq == wksq + DELTA_N + DELTA_N
|
||||
&& rank_of(psq) < RANK_5)
|
||||
return DRAW;
|
||||
|
||||
// Case 5: Stalemate with rook pawn
|
||||
if ( bksq == SQ_A8
|
||||
&& file_of(psq) == FILE_A)
|
||||
return DRAW;
|
||||
|
||||
// Case 6: White king trapped on the rook file
|
||||
if ( file_of(wksq) == FILE_A
|
||||
&& file_of(psq) == FILE_A
|
||||
&& rank_of(wksq) > rank_of(psq)
|
||||
&& bksq == wksq + 2)
|
||||
return DRAW;
|
||||
|
||||
return UNKNOWN;
|
||||
else if (us == WHITE)
|
||||
{
|
||||
// Immediate win if a pawn can be promoted without getting captured
|
||||
if ( rank_of(psq) == RANK_7
|
||||
&& wksq != psq + DELTA_N
|
||||
&& ( square_distance(bksq, psq + DELTA_N) > 1
|
||||
||(StepAttacksBB[KING][wksq] & (psq + DELTA_N))))
|
||||
result = WIN;
|
||||
}
|
||||
// Immediate draw if it is a stalemate or a king captures undefended pawn
|
||||
else if ( !(StepAttacksBB[KING][bksq] & ~(StepAttacksBB[KING][wksq] | StepAttacksBB[PAWN][psq]))
|
||||
|| (StepAttacksBB[KING][bksq] & psq & ~StepAttacksBB[KING][wksq]))
|
||||
result = DRAW;
|
||||
}
|
||||
|
||||
template<Color Us>
|
||||
Result KPKPosition::classify(const Result db[]) const {
|
||||
Result KPKPosition::classify(const std::vector<KPKPosition>& db) {
|
||||
|
||||
// White to Move: If one move leads to a position classified as RESULT_WIN,
|
||||
// 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.
|
||||
// White to Move: If one move leads to a position classified as WIN, the result
|
||||
// of the current position is WIN. If all moves lead to positions classified
|
||||
// as DRAW, the current position is classified as DRAW, otherwise the current
|
||||
// position is classified as UNKNOWN.
|
||||
//
|
||||
// Black to Move: If one move leads to a position classified as RESULT_DRAW,
|
||||
// the result of the current position is RESULT_DRAW. If all moves lead to
|
||||
// positions classified as RESULT_WIN, the position is classified RESULT_WIN.
|
||||
// Otherwise, the current position is classified as RESULT_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 as WIN, otherwise the current position is
|
||||
// classified as UNKNOWN.
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
Result r = INVALID;
|
||||
Bitboard b = k_attacks<Us>();
|
||||
Bitboard b = StepAttacksBB[KING][Us == WHITE ? wksq : bksq];
|
||||
|
||||
while (b)
|
||||
{
|
||||
r |= Us == WHITE ? db[index(pop_lsb(&b), bksq, psq, BLACK)]
|
||||
: db[index(wksq, pop_lsb(&b), psq, WHITE)];
|
||||
|
||||
if (Us == WHITE && (r & WIN))
|
||||
return WIN;
|
||||
|
||||
if (Us == BLACK && (r & DRAW))
|
||||
return DRAW;
|
||||
}
|
||||
r |= Us == WHITE ? db[index(Them, bksq, pop_lsb(&b), psq)]
|
||||
: db[index(Them, pop_lsb(&b), wksq, psq)];
|
||||
|
||||
if (Us == WHITE && rank_of(psq) < RANK_7)
|
||||
{
|
||||
Square s = psq + DELTA_N;
|
||||
r |= db[index(wksq, bksq, s, BLACK)]; // Single push
|
||||
r |= db[index(BLACK, bksq, wksq, s)]; // Single push
|
||||
|
||||
if (rank_of(s) == RANK_3 && s != wksq && s != bksq)
|
||||
r |= db[index(wksq, bksq, s + DELTA_N, BLACK)]; // Double push
|
||||
|
||||
if (r & WIN)
|
||||
return WIN;
|
||||
if (rank_of(psq) == RANK_2 && s != wksq && s != bksq)
|
||||
r |= db[index(BLACK, bksq, wksq, s + DELTA_N)]; // Double push
|
||||
}
|
||||
|
||||
return r & UNKNOWN ? UNKNOWN : Us == WHITE ? DRAW : WIN;
|
||||
if (Us == WHITE)
|
||||
return result = r & WIN ? WIN : r & UNKNOWN ? UNKNOWN : DRAW;
|
||||
else
|
||||
return result = r & DRAW ? DRAW : r & UNKNOWN ? UNKNOWN : WIN;
|
||||
}
|
||||
|
||||
Result KPKPosition::classify(int idx, Result db[]) {
|
||||
|
||||
decode_index(idx);
|
||||
return stm == WHITE ? classify<WHITE>(db) : classify<BLACK>(db);
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace
|
||||
|
||||
+107
-131
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -18,55 +18,52 @@
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <cstring> // For memset
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "bitcount.h"
|
||||
#include "misc.h"
|
||||
#include "rkiss.h"
|
||||
|
||||
CACHE_LINE_ALIGNMENT
|
||||
|
||||
Bitboard RMasks[64];
|
||||
Bitboard RMagics[64];
|
||||
Bitboard* RAttacks[64];
|
||||
unsigned RShifts[64];
|
||||
Bitboard RMasks[SQUARE_NB];
|
||||
Bitboard RMagics[SQUARE_NB];
|
||||
Bitboard* RAttacks[SQUARE_NB];
|
||||
unsigned RShifts[SQUARE_NB];
|
||||
|
||||
Bitboard BMasks[64];
|
||||
Bitboard BMagics[64];
|
||||
Bitboard* BAttacks[64];
|
||||
unsigned BShifts[64];
|
||||
Bitboard BMasks[SQUARE_NB];
|
||||
Bitboard BMagics[SQUARE_NB];
|
||||
Bitboard* BAttacks[SQUARE_NB];
|
||||
unsigned BShifts[SQUARE_NB];
|
||||
|
||||
Bitboard SquareBB[64];
|
||||
Bitboard FileBB[8];
|
||||
Bitboard RankBB[8];
|
||||
Bitboard AdjacentFilesBB[8];
|
||||
Bitboard ThisAndAdjacentFilesBB[8];
|
||||
Bitboard InFrontBB[2][8];
|
||||
Bitboard StepAttacksBB[16][64];
|
||||
Bitboard BetweenBB[64][64];
|
||||
Bitboard DistanceRingsBB[64][8];
|
||||
Bitboard ForwardBB[2][64];
|
||||
Bitboard PassedPawnMask[2][64];
|
||||
Bitboard AttackSpanMask[2][64];
|
||||
Bitboard PseudoAttacks[6][64];
|
||||
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 LineBB[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];
|
||||
|
||||
int SquareDistance[64][64];
|
||||
int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||
|
||||
namespace {
|
||||
|
||||
// De Bruijn sequences. See chessprogramming.wikispaces.com/BitScan
|
||||
const uint64_t DeBruijn_64 = 0x218A392CD3D5DBFULL;
|
||||
const uint64_t DeBruijn_64 = 0x3F79D71B4CB0A89ULL;
|
||||
const uint32_t DeBruijn_32 = 0x783A9B23;
|
||||
|
||||
CACHE_LINE_ALIGNMENT
|
||||
|
||||
int MS1BTable[256];
|
||||
Square BSFTable[64];
|
||||
Square BSFTable[SQUARE_NB];
|
||||
Bitboard RTable[0x19000]; // Storage space for rook attacks
|
||||
Bitboard BTable[0x1480]; // Storage space for bishop attacks
|
||||
uint8_t BitCount8Bit[256];
|
||||
|
||||
typedef unsigned (Fn)(Square, Bitboard);
|
||||
|
||||
@@ -75,19 +72,17 @@ namespace {
|
||||
|
||||
FORCE_INLINE unsigned bsf_index(Bitboard b) {
|
||||
|
||||
if (Is64Bit)
|
||||
return ((b & -b) * DeBruijn_64) >> 58;
|
||||
|
||||
// Use Matt Taylor's folding trick for 32 bit systems
|
||||
// Matt Taylor's folding for 32 bit systems, extended to 64 bits by Kim Walisch
|
||||
b ^= (b - 1);
|
||||
return ((unsigned(b) ^ unsigned(b >> 32)) * DeBruijn_32) >> 26;
|
||||
return Is64Bit ? (b * DeBruijn_64) >> 58
|
||||
: ((unsigned(b) ^ unsigned(b >> 32)) * DeBruijn_32) >> 26;
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
/// lsb()/msb() finds the least/most significant bit in a non-zero bitboard.
|
||||
/// pop_lsb() finds and clears the least significant bit in a non-zero bitboard.
|
||||
|
||||
#if !defined(USE_BSFQ)
|
||||
#ifndef USE_BSFQ
|
||||
|
||||
Square lsb(Bitboard b) { return BSFTable[bsf_index(b)]; }
|
||||
|
||||
@@ -123,95 +118,79 @@ Square msb(Bitboard b) {
|
||||
result += 8;
|
||||
}
|
||||
|
||||
return (Square)(result + MS1BTable[b32]);
|
||||
return Square(result + MS1BTable[b32]);
|
||||
}
|
||||
|
||||
#endif // !defined(USE_BSFQ)
|
||||
#endif // ifndef USE_BSFQ
|
||||
|
||||
|
||||
/// Bitboards::print() prints a bitboard in an easily readable format to the
|
||||
/// standard output. This is sometimes useful for debugging.
|
||||
/// Bitboards::pretty() returns an ASCII representation of a bitboard to be
|
||||
/// printed to standard output. This is sometimes useful for debugging.
|
||||
|
||||
void Bitboards::print(Bitboard b) {
|
||||
const std::string Bitboards::pretty(Bitboard b) {
|
||||
|
||||
sync_cout;
|
||||
std::string s = "+---+---+---+---+---+---+---+---+\n";
|
||||
|
||||
for (Rank rank = RANK_8; rank >= RANK_1; rank--)
|
||||
for (Rank r = RANK_8; r >= RANK_1; --r)
|
||||
{
|
||||
std::cout << "+---+---+---+---+---+---+---+---+" << '\n';
|
||||
for (File f = FILE_A; f <= FILE_H; ++f)
|
||||
s.append(b & make_square(f, r) ? "| X " : "| ");
|
||||
|
||||
for (File file = FILE_A; file <= FILE_H; file++)
|
||||
std::cout << "| " << (b & (file | rank) ? "X " : " ");
|
||||
|
||||
std::cout << "|\n";
|
||||
s.append("|\n+---+---+---+---+---+---+---+---+\n");
|
||||
}
|
||||
std::cout << "+---+---+---+---+---+---+---+---+" << sync_endl;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
/// Bitboards::init() initializes various bitboard arrays. It is called during
|
||||
/// program initialization.
|
||||
/// Bitboards::init() initializes various bitboard tables. It is called at
|
||||
/// startup and relies on global objects to be already zero-initialized.
|
||||
|
||||
void Bitboards::init() {
|
||||
|
||||
for (int k = 0, i = 0; i < 8; i++)
|
||||
while (k < (2 << i))
|
||||
MS1BTable[k++] = i;
|
||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||
BSFTable[bsf_index(SquareBB[s] = 1ULL << s)] = s;
|
||||
|
||||
for (int i = 0; i < 64; i++)
|
||||
BSFTable[bsf_index(1ULL << i)] = Square(i);
|
||||
for (Bitboard b = 1; b < 256; ++b)
|
||||
MS1BTable[b] = more_than_one(b) ? MS1BTable[b - 1] : lsb(b);
|
||||
|
||||
for (Bitboard b = 0; b < 256; b++)
|
||||
BitCount8Bit[b] = (uint8_t)popcount<Max15>(b);
|
||||
for (File f = FILE_A; f <= FILE_H; ++f)
|
||||
FileBB[f] = f > FILE_A ? FileBB[f - 1] << 1 : FileABB;
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
SquareBB[s] = 1ULL << s;
|
||||
for (Rank r = RANK_1; r <= RANK_8; ++r)
|
||||
RankBB[r] = r > RANK_1 ? RankBB[r - 1] << 8 : Rank1BB;
|
||||
|
||||
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++)
|
||||
{
|
||||
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);
|
||||
ThisAndAdjacentFilesBB[f] = FileBB[f] | AdjacentFilesBB[f];
|
||||
}
|
||||
|
||||
for (Rank r = RANK_1; r < RANK_8; r++)
|
||||
for (Rank r = RANK_1; r < RANK_8; ++r)
|
||||
InFrontBB[WHITE][r] = ~(InFrontBB[BLACK][r + 1] = InFrontBB[BLACK][r] | RankBB[r]);
|
||||
|
||||
for (Color c = WHITE; c <= BLACK; c++)
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
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)];
|
||||
PassedPawnMask[c][s] = InFrontBB[c][rank_of(s)] & ThisAndAdjacentFilesBB[file_of(s)];
|
||||
AttackSpanMask[c][s] = InFrontBB[c][rank_of(s)] & AdjacentFilesBB[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));
|
||||
|
||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
|
||||
for (int d = 1; d < 8; d++)
|
||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++)
|
||||
if (SquareDistance[s1][s2] == d)
|
||||
DistanceRingsBB[s1][d - 1] |= s2;
|
||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
|
||||
if (s1 != s2)
|
||||
{
|
||||
SquareDistance[s1][s2] = std::max(file_distance(s1, s2), rank_distance(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++)
|
||||
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 i = 0; steps[pt][i]; ++i)
|
||||
{
|
||||
Square to = s + Square(c == WHITE ? steps[pt][k] : -steps[pt][k]);
|
||||
Square to = s + Square(c == WHITE ? steps[pt][i] : -steps[pt][i]);
|
||||
|
||||
if (is_ok(to) && square_distance(s, to) < 3)
|
||||
StepAttacksBB[make_piece(c, pt)][s] |= to;
|
||||
@@ -223,21 +202,23 @@ void Bitboards::init() {
|
||||
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++)
|
||||
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
|
||||
{
|
||||
PseudoAttacks[QUEEN][s] = PseudoAttacks[BISHOP][s] = attacks_bb<BISHOP>(s, 0);
|
||||
PseudoAttacks[QUEEN][s] |= PseudoAttacks[ ROOK][s] = attacks_bb< ROOK>(s, 0);
|
||||
PseudoAttacks[QUEEN][s1] = PseudoAttacks[BISHOP][s1] = attacks_bb<BISHOP>(s1, 0);
|
||||
PseudoAttacks[QUEEN][s1] |= PseudoAttacks[ ROOK][s1] = attacks_bb< ROOK>(s1, 0);
|
||||
|
||||
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
|
||||
{
|
||||
Piece pc = (PseudoAttacks[BISHOP][s1] & s2) ? W_BISHOP :
|
||||
(PseudoAttacks[ROOK][s1] & s2) ? W_ROOK : NO_PIECE;
|
||||
|
||||
if (pc == NO_PIECE)
|
||||
continue;
|
||||
|
||||
LineBB[s1][s2] = (attacks_bb(pc, s1, 0) & attacks_bb(pc, s2, 0)) | s1 | s2;
|
||||
BetweenBB[s1][s2] = attacks_bb(pc, s1, SquareBB[s2]) & attacks_bb(pc, s2, SquareBB[s1]);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -247,7 +228,7 @@ namespace {
|
||||
|
||||
Bitboard attack = 0;
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
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])
|
||||
@@ -262,20 +243,6 @@ namespace {
|
||||
}
|
||||
|
||||
|
||||
Bitboard pick_random(RKISS& rk, int booster) {
|
||||
|
||||
// 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;
|
||||
|
||||
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>();
|
||||
}
|
||||
|
||||
|
||||
// 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
|
||||
@@ -284,8 +251,9 @@ namespace {
|
||||
void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
|
||||
Bitboard masks[], unsigned shifts[], Square deltas[], Fn index) {
|
||||
|
||||
int MagicBoosters[][8] = { { 3191, 2184, 1310, 3618, 2091, 1308, 2452, 3996 },
|
||||
{ 1059, 3608, 605, 3234, 3326, 38, 2029, 3043 } };
|
||||
int MagicBoosters[][8] = { { 969, 1976, 2850, 542, 2069, 2852, 1708, 164 },
|
||||
{ 3101, 552, 3555, 926, 834, 26, 2131, 1117 } };
|
||||
|
||||
RKISS rk;
|
||||
Bitboard occupancy[4096], reference[4096], edges, b;
|
||||
int i, size, booster;
|
||||
@@ -293,7 +261,7 @@ namespace {
|
||||
// attacks[s] is a pointer to the beginning of the attacks table for square 's'
|
||||
attacks[SQ_A1] = table;
|
||||
|
||||
for (Square s = SQ_A1; s <= SQ_H8; s++)
|
||||
for (Square s = SQ_A1; s <= SQ_H8; ++s)
|
||||
{
|
||||
// Board edges are not considered in the relevant occupancies
|
||||
edges = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));
|
||||
@@ -311,7 +279,12 @@ namespace {
|
||||
b = size = 0;
|
||||
do {
|
||||
occupancy[size] = b;
|
||||
reference[size++] = sliding_attack(deltas, s, b);
|
||||
reference[size] = sliding_attack(deltas, s, b);
|
||||
|
||||
if (HasPext)
|
||||
attacks[s][_pext_u64(b, masks[s])] = reference[size];
|
||||
|
||||
size++;
|
||||
b = (b - masks[s]) & masks[s];
|
||||
} while (b);
|
||||
|
||||
@@ -320,32 +293,35 @@ namespace {
|
||||
if (s < SQ_H8)
|
||||
attacks[s + 1] = attacks[s] + size;
|
||||
|
||||
if (HasPext)
|
||||
continue;
|
||||
|
||||
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 (BitCount8Bit[(magics[s] * masks[s]) >> 56] < 6);
|
||||
do magics[s] = rk.magic_rand<Bitboard>(booster);
|
||||
while (popcount<Max15>((magics[s] * masks[s]) >> 56) < 6);
|
||||
|
||||
memset(attacks[s], 0, size * sizeof(Bitboard));
|
||||
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++)
|
||||
for (i = 0; i < size; ++i)
|
||||
{
|
||||
Bitboard& attack = attacks[s][index(s, occupancy[i])];
|
||||
|
||||
if (attack && attack != reference[i])
|
||||
break;
|
||||
|
||||
assert(reference[i] != 0);
|
||||
assert(reference[i]);
|
||||
|
||||
attack = reference[i];
|
||||
}
|
||||
} while (i != size);
|
||||
} while (i < size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+149
-88
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -18,7 +18,7 @@
|
||||
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
|
||||
|
||||
#include "types.h"
|
||||
@@ -26,43 +26,64 @@
|
||||
namespace Bitboards {
|
||||
|
||||
void init();
|
||||
void print(Bitboard b);
|
||||
const std::string pretty(Bitboard b);
|
||||
|
||||
}
|
||||
|
||||
namespace Bitbases {
|
||||
|
||||
void init_kpk();
|
||||
uint32_t probe_kpk(Square wksq, Square wpsq, Square bksq, Color stm);
|
||||
bool probe_kpk(Square wksq, Square wpsq, Square bksq, Color us);
|
||||
|
||||
}
|
||||
|
||||
const Bitboard FileABB = 0x0101010101010101ULL;
|
||||
const Bitboard FileBBB = FileABB << 1;
|
||||
const Bitboard FileCBB = FileABB << 2;
|
||||
const Bitboard FileDBB = FileABB << 3;
|
||||
const Bitboard FileEBB = FileABB << 4;
|
||||
const Bitboard FileFBB = FileABB << 5;
|
||||
const Bitboard FileGBB = FileABB << 6;
|
||||
const Bitboard FileHBB = FileABB << 7;
|
||||
|
||||
const Bitboard Rank1BB = 0xFF;
|
||||
const Bitboard Rank2BB = Rank1BB << (8 * 1);
|
||||
const Bitboard Rank3BB = Rank1BB << (8 * 2);
|
||||
const Bitboard Rank4BB = Rank1BB << (8 * 3);
|
||||
const Bitboard Rank5BB = Rank1BB << (8 * 4);
|
||||
const Bitboard Rank6BB = Rank1BB << (8 * 5);
|
||||
const Bitboard Rank7BB = Rank1BB << (8 * 6);
|
||||
const Bitboard Rank8BB = Rank1BB << (8 * 7);
|
||||
|
||||
CACHE_LINE_ALIGNMENT
|
||||
|
||||
extern Bitboard RMasks[64];
|
||||
extern Bitboard RMagics[64];
|
||||
extern Bitboard* RAttacks[64];
|
||||
extern unsigned RShifts[64];
|
||||
extern Bitboard RMasks[SQUARE_NB];
|
||||
extern Bitboard RMagics[SQUARE_NB];
|
||||
extern Bitboard* RAttacks[SQUARE_NB];
|
||||
extern unsigned RShifts[SQUARE_NB];
|
||||
|
||||
extern Bitboard BMasks[64];
|
||||
extern Bitboard BMagics[64];
|
||||
extern Bitboard* BAttacks[64];
|
||||
extern unsigned BShifts[64];
|
||||
extern Bitboard BMasks[SQUARE_NB];
|
||||
extern Bitboard BMagics[SQUARE_NB];
|
||||
extern Bitboard* BAttacks[SQUARE_NB];
|
||||
extern unsigned BShifts[SQUARE_NB];
|
||||
|
||||
extern Bitboard SquareBB[64];
|
||||
extern Bitboard FileBB[8];
|
||||
extern Bitboard RankBB[8];
|
||||
extern Bitboard AdjacentFilesBB[8];
|
||||
extern Bitboard ThisAndAdjacentFilesBB[8];
|
||||
extern Bitboard InFrontBB[2][8];
|
||||
extern Bitboard StepAttacksBB[16][64];
|
||||
extern Bitboard BetweenBB[64][64];
|
||||
extern Bitboard DistanceRingsBB[64][8];
|
||||
extern Bitboard ForwardBB[2][64];
|
||||
extern Bitboard PassedPawnMask[2][64];
|
||||
extern Bitboard AttackSpanMask[2][64];
|
||||
extern Bitboard PseudoAttacks[6][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 LineBB[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 int SquareDistance[SQUARE_NB][SQUARE_NB];
|
||||
|
||||
const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
|
||||
|
||||
/// 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.
|
||||
@@ -87,13 +108,34 @@ inline Bitboard operator^(Bitboard b, Square s) {
|
||||
return b ^ SquareBB[s];
|
||||
}
|
||||
|
||||
|
||||
/// more_than_one() returns true if in 'b' there is more than one bit set
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
/// shift_bb() moves bitboard one step along direction Delta. Mainly for pawns.
|
||||
|
||||
template<Square Delta>
|
||||
inline Bitboard shift_bb(Bitboard b) {
|
||||
|
||||
return Delta == DELTA_N ? b << 8 : Delta == DELTA_S ? b >> 8
|
||||
: Delta == DELTA_NE ? (b & ~FileHBB) << 9 : Delta == DELTA_SE ? (b & ~FileHBB) >> 7
|
||||
: Delta == DELTA_NW ? (b & ~FileABB) << 7 : Delta == DELTA_SW ? (b & ~FileABB) >> 9
|
||||
: 0;
|
||||
}
|
||||
|
||||
|
||||
/// rank_bb() and file_bb() take a file or a square as input and return
|
||||
/// a bitboard representing all squares on the given file or rank.
|
||||
@@ -115,7 +157,7 @@ inline Bitboard file_bb(Square s) {
|
||||
}
|
||||
|
||||
|
||||
/// adjacent_files_bb takes a file as input and returns a bitboard representing
|
||||
/// adjacent_files_bb() takes a file as input and returns a bitboard representing
|
||||
/// all squares on the adjacent files.
|
||||
|
||||
inline Bitboard adjacent_files_bb(File f) {
|
||||
@@ -123,32 +165,19 @@ inline Bitboard adjacent_files_bb(File f) {
|
||||
}
|
||||
|
||||
|
||||
/// this_and_adjacent_files_bb takes a file as input and returns a bitboard
|
||||
/// representing all squares on the given and adjacent files.
|
||||
|
||||
inline Bitboard this_and_adjacent_files_bb(File f) {
|
||||
return ThisAndAdjacentFilesBB[f];
|
||||
}
|
||||
|
||||
|
||||
/// 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][rank_of(s)];
|
||||
}
|
||||
|
||||
|
||||
/// between_bb returns a bitboard representing all squares between two squares.
|
||||
/// 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,
|
||||
/// square d5 and e6 set. If s1 and s2 are not on the same rank, file or diagonal,
|
||||
/// 0 is returned.
|
||||
|
||||
inline Bitboard between_bb(Square s1, Square s2) {
|
||||
@@ -156,7 +185,7 @@ inline Bitboard between_bb(Square s1, Square s2) {
|
||||
}
|
||||
|
||||
|
||||
/// forward_bb takes a color and a square as input, and returns a bitboard
|
||||
/// 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)
|
||||
@@ -166,41 +195,39 @@ inline Bitboard forward_bb(Color c, Square s) {
|
||||
}
|
||||
|
||||
|
||||
/// passed_pawn_mask takes a color and a square as input, and returns a
|
||||
/// 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 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
|
||||
/// 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_adjacent_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) & adjacent_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
|
||||
/// 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])
|
||||
& ( SquareBB[s1] | SquareBB[s2] | SquareBB[s3]);
|
||||
}
|
||||
|
||||
|
||||
/// same_color_squares() returns a bitboard representing all squares with
|
||||
/// the same color of the given square.
|
||||
|
||||
inline Bitboard same_color_squares(Square s) {
|
||||
return Bitboard(0xAA55AA55AA55AA55ULL) & s ? 0xAA55AA55AA55AA55ULL
|
||||
: ~0xAA55AA55AA55AA55ULL;
|
||||
inline bool aligned(Square s1, Square s2, Square s3) {
|
||||
return LineBB[s1][s2] & s3;
|
||||
}
|
||||
|
||||
|
||||
@@ -214,6 +241,9 @@ FORCE_INLINE unsigned magic_index(Square s, Bitboard occ) {
|
||||
Bitboard* const Magics = Pt == ROOK ? RMagics : BMagics;
|
||||
unsigned* const Shifts = Pt == ROOK ? RShifts : BShifts;
|
||||
|
||||
if (HasPext)
|
||||
return unsigned(_pext_u64(occ, Masks[s]));
|
||||
|
||||
if (Is64Bit)
|
||||
return unsigned(((occ & Masks[s]) * Magics[s]) >> Shifts[s]);
|
||||
|
||||
@@ -227,49 +257,74 @@ inline Bitboard attacks_bb(Square s, Bitboard occ) {
|
||||
return (Pt == ROOK ? RAttacks : BAttacks)[s][magic_index<Pt>(s, occ)];
|
||||
}
|
||||
|
||||
inline Bitboard attacks_bb(Piece pc, Square s, Bitboard 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.
|
||||
switch (type_of(pc))
|
||||
{
|
||||
case BISHOP: return attacks_bb<BISHOP>(s, occ);
|
||||
case ROOK : return attacks_bb<ROOK>(s, occ);
|
||||
case QUEEN : return attacks_bb<BISHOP>(s, occ) | attacks_bb<ROOK>(s, occ);
|
||||
default : return StepAttacksBB[pc][s];
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(USE_BSFQ)
|
||||
/// lsb()/msb() finds the least/most significant bit in a non-zero bitboard.
|
||||
/// pop_lsb() finds and clears the least significant bit in a non-zero bitboard.
|
||||
|
||||
#ifdef USE_BSFQ
|
||||
|
||||
# if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
|
||||
|
||||
FORCE_INLINE Square lsb(Bitboard b) {
|
||||
unsigned long index;
|
||||
_BitScanForward64(&index, b);
|
||||
return (Square) index;
|
||||
unsigned long idx;
|
||||
_BitScanForward64(&idx, b);
|
||||
return (Square) idx;
|
||||
}
|
||||
|
||||
FORCE_INLINE Square msb(Bitboard b) {
|
||||
unsigned long index;
|
||||
_BitScanReverse64(&index, b);
|
||||
return (Square) index;
|
||||
unsigned long idx;
|
||||
_BitScanReverse64(&idx, b);
|
||||
return (Square) idx;
|
||||
}
|
||||
|
||||
# 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;
|
||||
Bitboard idx;
|
||||
__asm__("bsfq %1, %0": "=r"(idx): "rm"(b) );
|
||||
return (Square) idx;
|
||||
}
|
||||
|
||||
FORCE_INLINE Square msb(Bitboard b) {
|
||||
Bitboard index;
|
||||
__asm__("bsrq %1, %0": "=r"(index): "rm"(b) );
|
||||
return (Square) index;
|
||||
Bitboard idx;
|
||||
__asm__("bsrq %1, %0": "=r"(idx): "rm"(b) );
|
||||
return (Square) idx;
|
||||
}
|
||||
|
||||
# endif
|
||||
|
||||
FORCE_INLINE Square pop_lsb(Bitboard* b) {
|
||||
const Square s = lsb(*b);
|
||||
*b &= ~(1ULL << s);
|
||||
*b &= *b - 1;
|
||||
return s;
|
||||
}
|
||||
|
||||
#else // if !defined(USE_BSFQ)
|
||||
#else // if defined(USE_BSFQ)
|
||||
|
||||
extern Square msb(Bitboard b);
|
||||
extern Square lsb(Bitboard b);
|
||||
@@ -277,4 +332,10 @@ extern Square pop_lsb(Bitboard* b);
|
||||
|
||||
#endif
|
||||
|
||||
#endif // !defined(BITBOARD_H_INCLUDED)
|
||||
/// frontmost_sq() and backmost_sq() find the square corresponding to the
|
||||
/// most/least advanced bit relative to the given color.
|
||||
|
||||
inline Square frontmost_sq(Color c, Bitboard b) { return c == WHITE ? msb(b) : lsb(b); }
|
||||
inline Square backmost_sq(Color c, Bitboard b) { return c == WHITE ? lsb(b) : msb(b); }
|
||||
|
||||
#endif // #ifndef BITBOARD_H_INCLUDED
|
||||
|
||||
+28
-35
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -18,7 +18,7 @@
|
||||
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>
|
||||
@@ -32,62 +32,56 @@ enum BitCountType {
|
||||
CNT_HW_POPCNT
|
||||
};
|
||||
|
||||
/// 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 or
|
||||
/// use hardware popcnt instruction when available.
|
||||
/// Determine at compile time the best popcount<> specialization according to
|
||||
/// whether the platform is 32 or 64 bit, the maximum number of non-zero
|
||||
/// bits to count and if the hardware popcnt instruction is available.
|
||||
const BitCountType Full = HasPopCnt ? CNT_HW_POPCNT : Is64Bit ? CNT_64 : CNT_32;
|
||||
const BitCountType Max15 = HasPopCnt ? CNT_HW_POPCNT : Is64Bit ? CNT_64_MAX15 : CNT_32_MAX15;
|
||||
|
||||
|
||||
/// popcount() counts the number of nonzero bits in a bitboard
|
||||
/// popcount() counts the number of non-zero bits in a bitboard
|
||||
template<BitCountType> inline int popcount(Bitboard);
|
||||
|
||||
template<>
|
||||
inline int popcount<CNT_64>(Bitboard b) {
|
||||
b -= ((b>>1) & 0x5555555555555555ULL);
|
||||
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
|
||||
b = ((b>>4) + b) & 0x0F0F0F0F0F0F0F0FULL;
|
||||
b *= 0x0101010101010101ULL;
|
||||
return int(b >> 56);
|
||||
b -= (b >> 1) & 0x5555555555555555ULL;
|
||||
b = ((b >> 2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
|
||||
b = ((b >> 4) + b) & 0x0F0F0F0F0F0F0F0FULL;
|
||||
return (b * 0x0101010101010101ULL) >> 56;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int popcount<CNT_64_MAX15>(Bitboard b) {
|
||||
b -= (b>>1) & 0x5555555555555555ULL;
|
||||
b = ((b>>2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
|
||||
b *= 0x1111111111111111ULL;
|
||||
return int(b >> 60);
|
||||
b -= (b >> 1) & 0x5555555555555555ULL;
|
||||
b = ((b >> 2) & 0x3333333333333333ULL) + (b & 0x3333333333333333ULL);
|
||||
return (b * 0x1111111111111111ULL) >> 60;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int popcount<CNT_32>(Bitboard b) {
|
||||
unsigned w = unsigned(b >> 32), v = unsigned(b);
|
||||
v -= (v >> 1) & 0x55555555; // 0-2 in 2 bits
|
||||
w -= (w >> 1) & 0x55555555;
|
||||
v = ((v >> 2) & 0x33333333) + (v & 0x33333333); // 0-4 in 4 bits
|
||||
w = ((w >> 2) & 0x33333333) + (w & 0x33333333);
|
||||
v = ((v >> 4) + v) & 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 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 popcount<CNT_HW_POPCNT>(Bitboard b) {
|
||||
|
||||
#if !defined(USE_POPCNT)
|
||||
#ifndef USE_POPCNT
|
||||
|
||||
assert(false);
|
||||
return b != 0; // Avoid 'b not used' warning
|
||||
@@ -102,11 +96,10 @@ inline int popcount<CNT_HW_POPCNT>(Bitboard b) {
|
||||
|
||||
#else
|
||||
|
||||
unsigned long ret;
|
||||
__asm__("popcnt %1, %0" : "=r" (ret) : "r" (b));
|
||||
return ret;
|
||||
__asm__("popcnt %1, %0" : "=r" (b) : "r" (b));
|
||||
return b;
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // !defined(BITCOUNT_H_INCLUDED)
|
||||
#endif // #ifndef BITCOUNT_H_INCLUDED
|
||||
|
||||
+52
-55
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -25,7 +25,6 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
#include "book.h"
|
||||
#include "misc.h"
|
||||
@@ -36,9 +35,9 @@ using namespace std;
|
||||
namespace {
|
||||
|
||||
// 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 BookEntry {
|
||||
// stored in big-endian format, with the highest byte first (regardless of
|
||||
// size). The entries are ordered according to the key in ascending order.
|
||||
struct Entry {
|
||||
uint64_t key;
|
||||
uint16_t move;
|
||||
uint16_t count;
|
||||
@@ -46,7 +45,15 @@ namespace {
|
||||
};
|
||||
|
||||
// Random numbers from PolyGlot, used to compute book hash keys
|
||||
const Key PolyGlotRandoms[781] = {
|
||||
const union {
|
||||
Key PolyGlotRandoms[781];
|
||||
struct {
|
||||
Key psq[12][64]; // [piece][square]
|
||||
Key castling[4]; // [castling flag]
|
||||
Key enpassant[8]; // [file]
|
||||
Key turn;
|
||||
} Zobrist;
|
||||
} PG = {{
|
||||
0x9D39247E33776D41ULL, 0x2AF7398005AAA5C7ULL, 0x44DB015024623547ULL,
|
||||
0x9C15F73E62A76AE2ULL, 0x75834465489C0C89ULL, 0x3290AC3A203001BFULL,
|
||||
0x0FBBAD1F61042279ULL, 0xE83A908FF2FB60CAULL, 0x0D7E765D58755C10ULL,
|
||||
@@ -308,75 +315,64 @@ namespace {
|
||||
0x003A93D8B2806962ULL, 0x1C99DED33CB890A1ULL, 0xCF3145DE0ADD4289ULL,
|
||||
0xD0E4427A5514FB72ULL, 0x77C621CC9FB3A483ULL, 0x67A34DAC4356550BULL,
|
||||
0xF8D626AAAF278509ULL
|
||||
};
|
||||
}};
|
||||
|
||||
// Offsets to the PolyGlotRandoms[] array of zobrist keys
|
||||
const Key* ZobPiece = PolyGlotRandoms;
|
||||
const Key* ZobCastle = ZobPiece + 12 * 64; // Pieces * squares
|
||||
const Key* ZobEnPassant = ZobCastle + 4; // Castle flags
|
||||
const Key* ZobTurn = ZobEnPassant + 8; // Number of files
|
||||
// polyglot_key() returns the PolyGlot hash key of the given position
|
||||
Key polyglot_key(const Position& pos) {
|
||||
|
||||
// book_key() returns the PolyGlot hash key of the given position
|
||||
uint64_t book_key(const Position& pos) {
|
||||
|
||||
uint64_t key = 0;
|
||||
Key key = 0;
|
||||
Bitboard b = pos.pieces();
|
||||
|
||||
while (b)
|
||||
{
|
||||
// In PolyGlotRandoms[] pieces are stored in the following sequence:
|
||||
// BP = 0, WP = 1, BN = 2, WN = 3, ... BK = 10, WK = 11
|
||||
Square s = pop_lsb(&b);
|
||||
Piece p = pos.piece_on(s);
|
||||
int pieceOfs = 2 * (type_of(p) - 1) + (color_of(p) == WHITE);
|
||||
key ^= ZobPiece[64 * pieceOfs + s];
|
||||
Piece pc = pos.piece_on(s);
|
||||
|
||||
// PolyGlot pieces are: BP = 0, WP = 1, BN = 2, ... BK = 10, WK = 11
|
||||
key ^= PG.Zobrist.psq[2 * (type_of(pc) - 1) + (color_of(pc) == WHITE)][s];
|
||||
}
|
||||
|
||||
b = pos.can_castle(ALL_CASTLES);
|
||||
b = pos.can_castle(ANY_CASTLING);
|
||||
|
||||
while (b)
|
||||
key ^= ZobCastle[pop_lsb(&b)];
|
||||
key ^= PG.Zobrist.castling[pop_lsb(&b)];
|
||||
|
||||
if (pos.ep_square() != SQ_NONE)
|
||||
key ^= ZobEnPassant[file_of(pos.ep_square())];
|
||||
key ^= PG.Zobrist.enpassant[file_of(pos.ep_square())];
|
||||
|
||||
if (pos.side_to_move() == WHITE)
|
||||
key ^= ZobTurn[0];
|
||||
key ^= PG.Zobrist.turn;
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
PolyglotBook::PolyglotBook() {
|
||||
|
||||
for (int i = Time::now() % 10000; i > 0; i--)
|
||||
RKiss.rand<unsigned>(); // Make random number generation less deterministic
|
||||
}
|
||||
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
|
||||
/// converts them into a number of type T. A Polyglot book stores numbers in
|
||||
/// big-endian format.
|
||||
|
||||
template<typename T> PolyglotBook& PolyglotBook::operator>>(T& n) {
|
||||
|
||||
n = 0;
|
||||
for (size_t i = 0; i < sizeof(T); i++)
|
||||
for (size_t i = 0; i < sizeof(T); ++i)
|
||||
n = T((n << 8) + ifstream::get());
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<> PolyglotBook& PolyglotBook::operator>>(BookEntry& e) {
|
||||
template<> PolyglotBook& PolyglotBook::operator>>(Entry& e) {
|
||||
return *this >> e.key >> e.move >> e.count >> e.learn;
|
||||
}
|
||||
|
||||
|
||||
/// open() tries to open a book file with the given name after closing any
|
||||
/// exsisting one.
|
||||
/// existing one.
|
||||
|
||||
bool PolyglotBook::open(const char* fName) {
|
||||
|
||||
@@ -386,37 +382,38 @@ bool PolyglotBook::open(const char* fName) {
|
||||
ifstream::open(fName, ifstream::in | ifstream::binary);
|
||||
|
||||
fileName = is_open() ? fName : "";
|
||||
ifstream::clear(); // Reset any error flag to allow retry ifstream::open()
|
||||
ifstream::clear(); // Reset any error flag to allow a retry ifstream::open()
|
||||
return !fileName.empty();
|
||||
}
|
||||
|
||||
|
||||
/// probe() tries to find a book move for the given position. If no move is
|
||||
/// found returns MOVE_NONE. If pickBest is true returns always the highest
|
||||
/// rated move, otherwise randomly chooses one, based on the move score.
|
||||
/// found, it returns MOVE_NONE. If pickBest is true, then it always returns
|
||||
/// the highest-rated move, otherwise it randomly chooses one based on the
|
||||
/// move score.
|
||||
|
||||
Move PolyglotBook::probe(const Position& pos, const string& fName, bool pickBest) {
|
||||
|
||||
if (fileName != fName && !open(fName.c_str()))
|
||||
return MOVE_NONE;
|
||||
|
||||
BookEntry e;
|
||||
Entry e;
|
||||
uint16_t best = 0;
|
||||
unsigned sum = 0;
|
||||
Move move = MOVE_NONE;
|
||||
uint64_t key = book_key(pos);
|
||||
Key key = polyglot_key(pos);
|
||||
|
||||
seekg(find_first(key) * sizeof(BookEntry), ios_base::beg);
|
||||
seekg(find_first(key) * sizeof(Entry), ios_base::beg);
|
||||
|
||||
while (*this >> e, e.key == key && good())
|
||||
{
|
||||
best = max(best, e.count);
|
||||
sum += e.count;
|
||||
|
||||
// Choose book move according to its score. If a move has a very
|
||||
// high score it has 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)
|
||||
// Choose book move according to its score. If a move has a very high
|
||||
// score it has a higher probability of being choosen than a move with
|
||||
// a lower score. Note that first entry is always chosen.
|
||||
if ( (!pickBest && sum && rkiss.rand<unsigned>() % sum < e.count)
|
||||
|| (pickBest && e.count == best))
|
||||
move = Move(e.move);
|
||||
}
|
||||
@@ -430,18 +427,18 @@ Move PolyglotBook::probe(const Position& pos, const string& fName, bool pickBest
|
||||
// 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.
|
||||
// Castling moves follow the "king captures rook" representation. If a book
|
||||
// move is a promotion, we have to convert it to our representation and in
|
||||
// all other cases, we can directly compare with a Move after having masked
|
||||
// out the special Move flags (bit 14-15) that are not supported by PolyGlot.
|
||||
int pt = (move >> 12) & 7;
|
||||
if (pt)
|
||||
move = make<PROMOTION>(from_sq(move), to_sq(move), PieceType(pt + 1));
|
||||
|
||||
// Add 'special move' flags and verify it is legal
|
||||
for (MoveList<LEGAL> ml(pos); !ml.end(); ++ml)
|
||||
if (move == (ml.move() & 0x3FFF))
|
||||
return ml.move();
|
||||
for (MoveList<LEGAL> it(pos); *it; ++it)
|
||||
if (move == (*it ^ type_of(*it)))
|
||||
return *it;
|
||||
|
||||
return MOVE_NONE;
|
||||
}
|
||||
@@ -451,12 +448,12 @@ Move PolyglotBook::probe(const Position& pos, const string& fName, bool pickBest
|
||||
/// the book file for the given key. Returns the index of the leftmost book
|
||||
/// entry with the same key as the input.
|
||||
|
||||
size_t PolyglotBook::find_first(uint64_t key) {
|
||||
size_t PolyglotBook::find_first(Key key) {
|
||||
|
||||
seekg(0, ios::end); // Move pointer to end, so tellg() gets file's size
|
||||
|
||||
size_t low = 0, mid, high = (size_t)tellg() / sizeof(BookEntry) - 1;
|
||||
BookEntry e;
|
||||
size_t low = 0, mid, high = (size_t)tellg() / sizeof(Entry) - 1;
|
||||
Entry e;
|
||||
|
||||
assert(low <= high);
|
||||
|
||||
@@ -466,7 +463,7 @@ size_t PolyglotBook::find_first(uint64_t key) {
|
||||
|
||||
assert(mid >= low && mid < high);
|
||||
|
||||
seekg(mid * sizeof(BookEntry), ios_base::beg);
|
||||
seekg(mid * sizeof(Entry), ios_base::beg);
|
||||
*this >> e;
|
||||
|
||||
if (key <= e.key)
|
||||
|
||||
+5
-5
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(BOOK_H_INCLUDED)
|
||||
#ifndef BOOK_H_INCLUDED
|
||||
#define BOOK_H_INCLUDED
|
||||
|
||||
#include <fstream>
|
||||
@@ -36,10 +36,10 @@ private:
|
||||
template<typename T> PolyglotBook& operator>>(T& n);
|
||||
|
||||
bool open(const char* fName);
|
||||
size_t find_first(uint64_t key);
|
||||
size_t find_first(Key key);
|
||||
|
||||
RKISS RKiss;
|
||||
RKISS rkiss;
|
||||
std::string fileName;
|
||||
};
|
||||
|
||||
#endif // !defined(BOOK_H_INCLUDED)
|
||||
#endif // #ifndef BOOK_H_INCLUDED
|
||||
|
||||
+379
-417
File diff suppressed because it is too large
Load Diff
+20
-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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +17,7 @@
|
||||
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
|
||||
|
||||
#include <map>
|
||||
@@ -33,37 +33,38 @@ enum EndgameType {
|
||||
|
||||
// Evaluation functions
|
||||
|
||||
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
|
||||
KNNK, // KNN vs K
|
||||
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
|
||||
KBPsK, // KB and pawns vs K
|
||||
KQKRPs, // KQ vs KR and pawns
|
||||
KRPKR, // KRP vs KR
|
||||
KRPKB, // KRP vs KB
|
||||
KRPPKRP, // KRPP vs KRP
|
||||
KPsK, // King and pawns vs king
|
||||
KPsK, // K and pawns vs K
|
||||
KBPKB, // KBP vs KB
|
||||
KBPPKB, // KBPP vs KB
|
||||
KBPKN, // KBP vs KN
|
||||
KNPK, // KNP vs K
|
||||
KNPKB, // KNP vs KB
|
||||
KPKP // KP vs KP
|
||||
};
|
||||
|
||||
|
||||
/// 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.
|
||||
/// Endgame functions can be of two types depending on whether they return a
|
||||
/// Value or a ScaleFactor. Type eg_fun<int>::type returns either ScaleFactor
|
||||
/// or Value depending on whether the template parameter is 0 or 1.
|
||||
|
||||
template<int> struct eg_fun { typedef Value type; };
|
||||
template<> struct eg_fun<1> { typedef ScaleFactor type; };
|
||||
@@ -83,18 +84,18 @@ struct EndgameBase {
|
||||
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; }
|
||||
explicit Endgame(Color c) : strongSide(c), weakSide(~c) {}
|
||||
Color color() const { return strongSide; }
|
||||
T operator()(const Position&) const;
|
||||
|
||||
private:
|
||||
Color strongerSide, weakerSide;
|
||||
const Color strongSide, weakSide;
|
||||
};
|
||||
|
||||
|
||||
/// 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.
|
||||
/// The Endgames class stores the pointers to endgame evaluation and scaling
|
||||
/// base objects in two std::map typedefs. We then use polymorphism to invoke
|
||||
/// the actual endgame function by calling its virtual operator().
|
||||
|
||||
class Endgames {
|
||||
|
||||
@@ -111,10 +112,10 @@ class Endgames {
|
||||
|
||||
public:
|
||||
Endgames();
|
||||
~Endgames();
|
||||
~Endgames();
|
||||
|
||||
template<typename T> T probe(Key key, T& eg)
|
||||
{ return eg = map(eg).count(key) ? map(eg)[key] : NULL; }
|
||||
};
|
||||
|
||||
#endif // !defined(ENDGAME_H_INCLUDED)
|
||||
#endif // #ifndef ENDGAME_H_INCLUDED
|
||||
|
||||
+539
-838
File diff suppressed because it is too large
Load Diff
+4
-6
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +17,7 @@
|
||||
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 "types.h"
|
||||
@@ -26,12 +26,10 @@ class Position;
|
||||
|
||||
namespace Eval {
|
||||
|
||||
extern Color RootColor;
|
||||
|
||||
extern void init();
|
||||
extern Value evaluate(const Position& pos, Value& margin);
|
||||
extern Value evaluate(const Position& pos);
|
||||
extern std::string trace(const Position& pos);
|
||||
|
||||
}
|
||||
|
||||
#endif // !defined(EVALUATE_H_INCLUDED)
|
||||
#endif // #ifndef EVALUATE_H_INCLUDED
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
||||
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
||||
Copyright (C) 2008-2012 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
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
/// 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:
|
||||
void clear();
|
||||
Value value(Piece p, Square to) const;
|
||||
void add(Piece p, Square to, Value bonus);
|
||||
Value gain(Piece p, Square to) const;
|
||||
void update_gain(Piece p, Square to, Value g);
|
||||
|
||||
static const Value MaxValue = Value(2000);
|
||||
|
||||
private:
|
||||
Value history[16][64]; // [piece][to_square]
|
||||
Value maxGains[16][64]; // [piece][to_square]
|
||||
};
|
||||
|
||||
inline void History::clear() {
|
||||
memset(history, 0, 16 * 64 * sizeof(Value));
|
||||
memset(maxGains, 0, 16 * 64 * sizeof(Value));
|
||||
}
|
||||
|
||||
inline Value History::value(Piece p, Square to) const {
|
||||
return history[p][to];
|
||||
}
|
||||
|
||||
inline void History::add(Piece p, Square to, Value bonus) {
|
||||
if (abs(history[p][to] + bonus) < MaxValue) history[p][to] += bonus;
|
||||
}
|
||||
|
||||
inline Value History::gain(Piece p, Square to) const {
|
||||
return maxGains[p][to];
|
||||
}
|
||||
|
||||
inline void History::update_gain(Piece p, Square to, Value g) {
|
||||
maxGains[p][to] = std::max(g, maxGains[p][to] - 1);
|
||||
}
|
||||
|
||||
#endif // !defined(HISTORY_H_INCLUDED)
|
||||
+5
-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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -18,7 +18,6 @@
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "evaluate.h"
|
||||
@@ -34,19 +33,15 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
UCI::init(Options);
|
||||
Bitboards::init();
|
||||
Zobrist::init();
|
||||
Position::init();
|
||||
Bitbases::init_kpk();
|
||||
Search::init();
|
||||
Pawns::init();
|
||||
Eval::init();
|
||||
Threads.init();
|
||||
TT.set_size(Options["Hash"]);
|
||||
TT.resize(Options["Hash"]);
|
||||
|
||||
std::string args;
|
||||
|
||||
for (int i = 1; i < argc; i++)
|
||||
args += std::string(argv[i]) + " ";
|
||||
|
||||
UCI::loop(args);
|
||||
UCI::loop(argc, argv);
|
||||
|
||||
Threads.exit();
|
||||
}
|
||||
|
||||
+110
-121
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <algorithm> // For std::min
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
|
||||
@@ -27,30 +27,35 @@ using namespace std;
|
||||
|
||||
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 int LinearCoefficients[6] = { 1617, -162, -1172, -190, 105, 26 };
|
||||
// pair pawn knight bishop rook queen
|
||||
const int LinearCoefficients[6] = { 1852, -162, -1122, -183, 249, -154 };
|
||||
|
||||
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 QuadraticCoefficientsSameSide[][PIECE_TYPE_NB] = {
|
||||
// OUR PIECES
|
||||
// pair pawn knight bishop rook queen
|
||||
{ 0 }, // Bishop pair
|
||||
{ 39, 2 }, // Pawn
|
||||
{ 35, 271, -4 }, // knight OUR PIECES
|
||||
{ 0, 105, 4, 0 }, // Bishop
|
||||
{ -27, -2, 46, 100, -141 }, // Rook
|
||||
{-177, 25, 129, 142, -137, 0 } // Queen
|
||||
};
|
||||
|
||||
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 } };
|
||||
const int QuadraticCoefficientsOppositeSide[][PIECE_TYPE_NB] = {
|
||||
// THEIR PIECES
|
||||
// pair pawn knight bishop rook queen
|
||||
{ 0 }, // Bishop pair
|
||||
{ 37, 0 }, // Pawn
|
||||
{ 10, 62, 0 }, // Knight OUR PIECES
|
||||
{ 57, 64, 39, 0 }, // Bishop
|
||||
{ 50, 40, 23, -22, 0 }, // Rook
|
||||
{ 98, 105, -39, 141, 274, 0 } // Queen
|
||||
};
|
||||
|
||||
// Endgame evaluation and scaling functions accessed direcly and not through
|
||||
// the function maps because correspond to more then one material hash key.
|
||||
Endgame<KmmKm> EvaluateKmmKm[] = { Endgame<KmmKm>(WHITE), Endgame<KmmKm>(BLACK) };
|
||||
// Endgame evaluation and scaling functions are accessed directly and not through
|
||||
// the function maps because they correspond to more than one material hash key.
|
||||
Endgame<KXK> EvaluateKXK[] = { Endgame<KXK>(WHITE), Endgame<KXK>(BLACK) };
|
||||
|
||||
Endgame<KBPsK> ScaleKBPsK[] = { Endgame<KBPsK>(WHITE), Endgame<KBPsK>(BLACK) };
|
||||
@@ -61,38 +66,69 @@ namespace {
|
||||
// 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) >= RookValueMg;
|
||||
return !pos.count<PAWN>(Them)
|
||||
&& pos.non_pawn_material(Them) == VALUE_ZERO
|
||||
&& pos.non_pawn_material(Us) >= RookValueMg;
|
||||
}
|
||||
|
||||
template<Color Us> bool is_KBPsKs(const Position& pos) {
|
||||
return pos.non_pawn_material(Us) == BishopValueMg
|
||||
&& pos.piece_count(Us, BISHOP) == 1
|
||||
&& pos.piece_count(Us, PAWN) >= 1;
|
||||
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) == QueenValueMg
|
||||
&& 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 the imbalance by comparing the piece count of each
|
||||
/// piece type for both colors.
|
||||
|
||||
template<Color Us>
|
||||
int imbalance(const int pieceCount[][PIECE_TYPE_NB]) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
int pt1, pt2, pc, v;
|
||||
int value = 0;
|
||||
|
||||
// Second-degree polynomial material imbalance by Tord Romstad
|
||||
for (pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; ++pt1)
|
||||
{
|
||||
pc = pieceCount[Us][pt1];
|
||||
if (!pc)
|
||||
continue;
|
||||
|
||||
v = LinearCoefficients[pt1];
|
||||
|
||||
for (pt2 = NO_PIECE_TYPE; pt2 <= pt1; ++pt2)
|
||||
v += QuadraticCoefficientsSameSide[pt1][pt2] * pieceCount[Us][pt2]
|
||||
+ QuadraticCoefficientsOppositeSide[pt1][pt2] * pieceCount[Them][pt2];
|
||||
|
||||
value += pc * v;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace Material {
|
||||
|
||||
/// MaterialTable::probe() takes a position object as input, looks up a MaterialEntry
|
||||
/// 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.
|
||||
|
||||
MaterialEntry* MaterialTable::probe(const Position& pos) {
|
||||
Entry* probe(const Position& pos, Table& entries, Endgames& endgames) {
|
||||
|
||||
Key key = pos.material_key();
|
||||
MaterialEntry* e = entries[key];
|
||||
Entry* e = entries[key];
|
||||
|
||||
// If e->key matches the position's material hash key, it means that we
|
||||
// have analysed this material configuration before, and we can simply
|
||||
@@ -100,14 +136,14 @@ MaterialEntry* MaterialTable::probe(const Position& pos) {
|
||||
if (e->key == key)
|
||||
return e;
|
||||
|
||||
memset(e, 0, sizeof(MaterialEntry));
|
||||
std::memset(e, 0, sizeof(Entry));
|
||||
e->key = key;
|
||||
e->factor[WHITE] = e->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL;
|
||||
e->gamePhase = MaterialTable::game_phase(pos);
|
||||
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.
|
||||
// Let's look if we have a specialized evaluation function for this particular
|
||||
// material configuration. Firstly we look for a fixed configuration one, then
|
||||
// for a generic one if the previous search failed.
|
||||
if (endgames.probe(key, e->evaluationFunction))
|
||||
return e;
|
||||
|
||||
@@ -123,21 +159,6 @@ MaterialEntry* MaterialTable::probe(const Position& pos) {
|
||||
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(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)
|
||||
{
|
||||
e->evaluationFunction = &EvaluateKmmKm[pos.side_to_move()];
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
// OK, we didn't find any special evaluation function for the current
|
||||
// material configuration. Is there a suitable scaling function?
|
||||
//
|
||||
@@ -151,8 +172,8 @@ MaterialEntry* MaterialTable::probe(const Position& pos) {
|
||||
return e;
|
||||
}
|
||||
|
||||
// Generic scaling functions that refer to more then one material
|
||||
// distribution. Should be probed after the specialized ones.
|
||||
// Generic scaling functions that refer to more than one material
|
||||
// distribution. They should be probed after the specialized ones.
|
||||
// Note that these ones don't return after setting the function.
|
||||
if (is_KBPsKs<WHITE>(pos))
|
||||
e->scalingFunction[WHITE] = &ScaleKBPsK[WHITE];
|
||||
@@ -169,19 +190,19 @@ MaterialEntry* MaterialTable::probe(const Position& pos) {
|
||||
Value npm_w = pos.non_pawn_material(WHITE);
|
||||
Value npm_b = pos.non_pawn_material(BLACK);
|
||||
|
||||
if (npm_w + npm_b == VALUE_ZERO)
|
||||
if (npm_w + npm_b == VALUE_ZERO && pos.pieces(PAWN))
|
||||
{
|
||||
if (pos.piece_count(BLACK, PAWN) == 0)
|
||||
if (!pos.count<PAWN>(BLACK))
|
||||
{
|
||||
assert(pos.piece_count(WHITE, PAWN) >= 2);
|
||||
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);
|
||||
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.
|
||||
@@ -190,83 +211,49 @@ MaterialEntry* MaterialTable::probe(const Position& pos) {
|
||||
}
|
||||
}
|
||||
|
||||
// No pawns makes it difficult to win, even with a material advantage
|
||||
if (pos.piece_count(WHITE, PAWN) == 0 && npm_w - npm_b <= BishopValueMg)
|
||||
{
|
||||
e->factor[WHITE] = (uint8_t)
|
||||
(npm_w == npm_b || npm_w < RookValueMg ? 0 : NoPawnsSF[std::min(pos.piece_count(WHITE, BISHOP), 2)]);
|
||||
}
|
||||
// No pawns makes it difficult to win, even with a material advantage. This
|
||||
// catches some trivial draws like KK, KBK and KNK and gives a very drawish
|
||||
// scale factor for cases such as KRKBP and KmmKm (except for KBBKN).
|
||||
if (!pos.count<PAWN>(WHITE) && npm_w - npm_b <= BishopValueMg)
|
||||
e->factor[WHITE] = uint8_t(npm_w < RookValueMg ? SCALE_FACTOR_DRAW : npm_b <= BishopValueMg ? 4 : 12);
|
||||
|
||||
if (pos.piece_count(BLACK, PAWN) == 0 && npm_b - npm_w <= BishopValueMg)
|
||||
{
|
||||
e->factor[BLACK] = (uint8_t)
|
||||
(npm_w == npm_b || npm_b < RookValueMg ? 0 : NoPawnsSF[std::min(pos.piece_count(BLACK, BISHOP), 2)]);
|
||||
}
|
||||
if (!pos.count<PAWN>(BLACK) && npm_b - npm_w <= BishopValueMg)
|
||||
e->factor[BLACK] = uint8_t(npm_b < RookValueMg ? SCALE_FACTOR_DRAW : npm_w <= BishopValueMg ? 4 : 12);
|
||||
|
||||
if (pos.count<PAWN>(WHITE) == 1 && npm_w - npm_b <= BishopValueMg)
|
||||
e->factor[WHITE] = (uint8_t) SCALE_FACTOR_ONEPAWN;
|
||||
|
||||
if (pos.count<PAWN>(BLACK) == 1 && npm_b - npm_w <= BishopValueMg)
|
||||
e->factor[BLACK] = (uint8_t) SCALE_FACTOR_ONEPAWN;
|
||||
|
||||
// Compute the space weight
|
||||
if (npm_w + npm_b >= 2 * QueenValueMg + 4 * RookValueMg + 2 * KnightValueMg)
|
||||
{
|
||||
int minorPieceCount = pos.piece_count(WHITE, KNIGHT) + pos.piece_count(WHITE, BISHOP)
|
||||
+ pos.piece_count(BLACK, KNIGHT) + pos.piece_count(BLACK, BISHOP);
|
||||
int minorPieceCount = pos.count<KNIGHT>(WHITE) + pos.count<BISHOP>(WHITE)
|
||||
+ pos.count<KNIGHT>(BLACK) + pos.count<BISHOP>(BLACK);
|
||||
|
||||
e->spaceWeight = minorPieceCount * minorPieceCount;
|
||||
e->spaceWeight = make_score(minorPieceCount * minorPieceCount, 0);
|
||||
}
|
||||
|
||||
// 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
|
||||
// for the bishop pair "extended piece", which allows us to be more flexible
|
||||
// in defining bishop pair bonuses.
|
||||
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) } };
|
||||
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) } };
|
||||
|
||||
e->value = (int16_t)((imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
/// MaterialTable::imbalance() calculates imbalance comparing piece count of each
|
||||
/// piece type for both colors.
|
||||
|
||||
template<Color Us>
|
||||
int MaterialTable::imbalance(const int pieceCount[][8]) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
int pt1, pt2, pc, v;
|
||||
int value = 0;
|
||||
|
||||
// Redundancy of major pieces, formula based on Kaufman's paper
|
||||
// "The Evaluation of Material Imbalances in Chess"
|
||||
if (pieceCount[Us][ROOK] > 0)
|
||||
value -= RedundantRookPenalty * (pieceCount[Us][ROOK] - 1)
|
||||
+ RedundantQueenPenalty * pieceCount[Us][QUEEN];
|
||||
|
||||
// Second-degree polynomial material imbalance by Tord Romstad
|
||||
for (pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; pt1++)
|
||||
{
|
||||
pc = pieceCount[Us][pt1];
|
||||
if (!pc)
|
||||
continue;
|
||||
|
||||
v = LinearCoefficients[pt1];
|
||||
|
||||
for (pt2 = NO_PIECE_TYPE; pt2 <= pt1; pt2++)
|
||||
v += QuadraticCoefficientsSameColor[pt1][pt2] * pieceCount[Us][pt2]
|
||||
+ QuadraticCoefficientsOppositeColor[pt1][pt2] * pieceCount[Them][pt2];
|
||||
|
||||
value += pc * v;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/// MaterialTable::game_phase() calculates the phase given the current
|
||||
/// Material::game_phase() calculates the phase given the current
|
||||
/// position. Because the phase is strictly a function of the material, it
|
||||
/// is stored in MaterialEntry.
|
||||
|
||||
Phase MaterialTable::game_phase(const Position& pos) {
|
||||
Phase game_phase(const Position& pos) {
|
||||
|
||||
Value npm = pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK);
|
||||
|
||||
@@ -274,3 +261,5 @@ Phase MaterialTable::game_phase(const Position& pos) {
|
||||
: npm <= EndgameLimit ? PHASE_ENDGAME
|
||||
: Phase(((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit));
|
||||
}
|
||||
|
||||
} // namespace Material
|
||||
|
||||
+32
-77
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +17,7 @@
|
||||
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
|
||||
|
||||
#include "endgame.h"
|
||||
@@ -25,96 +25,51 @@
|
||||
#include "position.h"
|
||||
#include "types.h"
|
||||
|
||||
const int MaterialTableSize = 8192;
|
||||
namespace Material {
|
||||
|
||||
/// Game phase
|
||||
enum Phase {
|
||||
PHASE_ENDGAME = 0,
|
||||
PHASE_MIDGAME = 128
|
||||
};
|
||||
|
||||
|
||||
/// MaterialEntry 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 MaterialEntry {
|
||||
struct Entry {
|
||||
|
||||
friend struct MaterialTable;
|
||||
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& pos) const { return (*evaluationFunction)(pos); }
|
||||
|
||||
public:
|
||||
Score material_value() const;
|
||||
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;
|
||||
// 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 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 for draws with rook pawns and wrong-colored bishops.
|
||||
|
||||
ScaleFactor scale_factor(const Position& pos, Color c) const {
|
||||
|
||||
return !scalingFunction[c] || (*scalingFunction[c])(pos) == SCALE_FACTOR_NONE
|
||||
? ScaleFactor(factor[c]) : (*scalingFunction[c])(pos);
|
||||
}
|
||||
|
||||
private:
|
||||
Key key;
|
||||
int16_t value;
|
||||
uint8_t factor[2];
|
||||
uint8_t factor[COLOR_NB];
|
||||
EndgameBase<Value>* evaluationFunction;
|
||||
EndgameBase<ScaleFactor>* scalingFunction[2];
|
||||
int spaceWeight;
|
||||
EndgameBase<ScaleFactor>* scalingFunction[COLOR_NB];
|
||||
Score spaceWeight;
|
||||
Phase gamePhase;
|
||||
};
|
||||
|
||||
typedef HashTable<Entry, 8192> Table;
|
||||
|
||||
/// The MaterialTable class represents a material hash table. The most important
|
||||
/// method is probe(), which returns a pointer to a MaterialEntry object.
|
||||
Entry* probe(const Position& pos, Table& entries, Endgames& endgames);
|
||||
Phase game_phase(const Position& pos);
|
||||
|
||||
struct MaterialTable {
|
||||
} // namespace Material
|
||||
|
||||
MaterialEntry* probe(const Position& pos);
|
||||
static Phase game_phase(const Position& pos);
|
||||
template<Color Us> static int imbalance(const int pieceCount[][8]);
|
||||
|
||||
HashTable<MaterialEntry, MaterialTableSize> entries;
|
||||
Endgames endgames;
|
||||
};
|
||||
|
||||
|
||||
/// MaterialEntry::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 MaterialEntry::scale_factor(const Position& pos, Color c) const {
|
||||
|
||||
if (!scalingFunction[c])
|
||||
return ScaleFactor(factor[c]);
|
||||
|
||||
ScaleFactor sf = (*scalingFunction[c])(pos);
|
||||
return sf == SCALE_FACTOR_NONE ? ScaleFactor(factor[c]) : sf;
|
||||
}
|
||||
|
||||
inline Value MaterialEntry::evaluate(const Position& pos) const {
|
||||
return (*evaluationFunction)(pos);
|
||||
}
|
||||
|
||||
inline Score MaterialEntry::material_value() const {
|
||||
return make_score(value, value);
|
||||
}
|
||||
|
||||
inline int MaterialEntry::space_weight() const {
|
||||
return spaceWeight;
|
||||
}
|
||||
|
||||
inline Phase MaterialEntry::game_phase() const {
|
||||
return gamePhase;
|
||||
}
|
||||
|
||||
inline bool MaterialEntry::specialized_eval_exists() const {
|
||||
return evaluationFunction != NULL;
|
||||
}
|
||||
|
||||
#endif // !defined(MATERIAL_H_INCLUDED)
|
||||
#endif // #ifndef MATERIAL_H_INCLUDED
|
||||
|
||||
+40
-80
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -24,64 +24,48 @@
|
||||
#include "misc.h"
|
||||
#include "thread.h"
|
||||
|
||||
#if defined(__hpux)
|
||||
# include <sys/pstat.h>
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
/// Version number. If Version is left empty, then Tag plus current
|
||||
/// date (in the format YYMMDD) is used as a version number.
|
||||
|
||||
static const string Version = "2.3.1";
|
||||
static const string Tag = "";
|
||||
/// Version number. If Version is left empty, then compile date in the format
|
||||
/// DD-MM-YY and show in engine_info.
|
||||
static const string Version = "5";
|
||||
|
||||
|
||||
/// engine_info() 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 Version is empty.
|
||||
/// 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.
|
||||
|
||||
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(Is64Bit ? " 64bit" : "");
|
||||
const string popcnt(HasPopCnt ? " SSE4.2" : "");
|
||||
|
||||
string month, day, year;
|
||||
stringstream s, date(__DATE__); // From compiler, format is "Sep 21 2008"
|
||||
stringstream ss, date(__DATE__); // From compiler, format is "Sep 21 2008"
|
||||
|
||||
s << "Stockfish " << Version;
|
||||
ss << "Stockfish " << Version << setfill('0');
|
||||
|
||||
if (Version.empty())
|
||||
{
|
||||
date >> month >> day >> year;
|
||||
|
||||
s << Tag << setfill('0') << " " << year.substr(2)
|
||||
<< setw(2) << (1 + months.find(month) / 4) << setw(2) << day;
|
||||
ss << setw(2) << day << setw(2) << (1 + months.find(month) / 4) << year.substr(2);
|
||||
}
|
||||
|
||||
s << cpu64 << popcnt << (to_uci ? "\nid author ": " by ")
|
||||
<< "Tord Romstad, Marco Costalba and Joona Kiiski";
|
||||
ss << (Is64Bit ? " 64" : "")
|
||||
<< (HasPext ? " BMI2" : (HasPopCnt ? " SSE4.2" : ""))
|
||||
<< (to_uci ? "\nid author ": " by ")
|
||||
<< "Tord Romstad, Marco Costalba and Joona Kiiski";
|
||||
|
||||
return s.str();
|
||||
}
|
||||
|
||||
|
||||
/// Convert system time to milliseconds. That's all we need.
|
||||
|
||||
Time::point Time::now() {
|
||||
sys_time_t t; system_time(&t); return time_to_msec(t);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
||||
/// Debug functions used mainly to collect run-time statistics
|
||||
|
||||
static uint64_t hits[2], means[2];
|
||||
static int64_t hits[2], means[2];
|
||||
|
||||
void dbg_hit_on(bool b) { hits[0]++; if (b) hits[1]++; }
|
||||
void dbg_hit_on(bool b) { ++hits[0]; if (b) ++hits[1]; }
|
||||
void dbg_hit_on_c(bool c, bool b) { if (c) dbg_hit_on(b); }
|
||||
void dbg_mean_of(int v) { means[0]++; means[1] += v; }
|
||||
void dbg_mean_of(int v) { ++means[0]; means[1] += v; }
|
||||
|
||||
void dbg_print() {
|
||||
|
||||
@@ -91,14 +75,14 @@ void dbg_print() {
|
||||
|
||||
if (means[0])
|
||||
cerr << "Total " << means[0] << " Mean "
|
||||
<< (float)means[1] / means[0] << endl;
|
||||
<< (double)means[1] / means[0] << endl;
|
||||
}
|
||||
|
||||
|
||||
/// 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!
|
||||
/// can toggle the logging of std::cout and std:cin at runtime whilst preserving
|
||||
/// usual i/o functionality, all without changing a single line of code!
|
||||
/// Idea from http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81
|
||||
|
||||
struct Tie: public streambuf { // MSVC requires splitted streambuf for cin and cout
|
||||
@@ -153,17 +137,17 @@ public:
|
||||
};
|
||||
|
||||
|
||||
/// Used to serialize access to std::cout to avoid multiple threads to write at
|
||||
/// Used to serialize access to std::cout to avoid multiple threads writing at
|
||||
/// the same time.
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, SyncCout sc) {
|
||||
|
||||
static Mutex m;
|
||||
|
||||
if (sc == io_lock)
|
||||
if (sc == IO_LOCK)
|
||||
m.lock();
|
||||
|
||||
if (sc == io_unlock)
|
||||
if (sc == IO_UNLOCK)
|
||||
m.unlock();
|
||||
|
||||
return os;
|
||||
@@ -174,37 +158,12 @@ std::ostream& operator<<(std::ostream& os, SyncCout sc) {
|
||||
void start_logger(bool b) { Logger::start(b); }
|
||||
|
||||
|
||||
/// cpu_count() tries to detect the number of CPU cores
|
||||
|
||||
int cpu_count() {
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
SYSTEM_INFO s;
|
||||
GetSystemInfo(&s);
|
||||
return s.dwNumberOfProcessors;
|
||||
#else
|
||||
|
||||
# if defined(_SC_NPROCESSORS_ONLN)
|
||||
return sysconf(_SC_NPROCESSORS_ONLN);
|
||||
# elif defined(__hpux)
|
||||
struct pst_dynamic psd;
|
||||
if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1)
|
||||
return 1;
|
||||
return psd.psd_proc_cnt;
|
||||
# else
|
||||
return 1;
|
||||
# endif
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/// timed_wait() waits for msec milliseconds. It is mainly an helper to wrap
|
||||
/// conversion from milliseconds to struct timespec, as used by pthreads.
|
||||
/// timed_wait() waits for msec milliseconds. It is mainly a helper to wrap
|
||||
/// the conversion from milliseconds to struct timespec, as used by pthreads.
|
||||
|
||||
void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) {
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#ifdef _WIN32
|
||||
int tm = msec;
|
||||
#else
|
||||
timespec ts, *tm = &ts;
|
||||
@@ -218,27 +177,28 @@ void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) {
|
||||
}
|
||||
|
||||
|
||||
/// 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 memory, that can be quite slow.
|
||||
#if defined(NO_PREFETCH)
|
||||
/// prefetch() preloads the given address in L1/L2 cache. This is a non-blocking
|
||||
/// function that doesn't stall the CPU waiting for data to be loaded from memory,
|
||||
/// which can be quite slow.
|
||||
#ifdef NO_PREFETCH
|
||||
|
||||
void prefetch(char*) {}
|
||||
|
||||
#else
|
||||
|
||||
# include <xmmintrin.h>
|
||||
|
||||
void prefetch(char* addr) {
|
||||
|
||||
# if defined(__INTEL_COMPILER) || defined(__ICL)
|
||||
// This hack prevents prefetches to be optimized away by
|
||||
// Intel compiler. Both MSVC and gcc seems not affected.
|
||||
# if defined(__INTEL_COMPILER)
|
||||
// This hack prevents prefetches from being optimized away by
|
||||
// Intel compiler. Both MSVC and gcc seem not be affected by this.
|
||||
__asm__ ("");
|
||||
# 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
|
||||
|
||||
+10
-11
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +17,7 @@
|
||||
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
|
||||
|
||||
#include <fstream>
|
||||
@@ -27,7 +27,6 @@
|
||||
#include "types.h"
|
||||
|
||||
extern const std::string engine_info(bool to_uci = false);
|
||||
extern int cpu_count();
|
||||
extern void timed_wait(WaitCondition&, Lock&, int);
|
||||
extern void prefetch(char* addr);
|
||||
extern void start_logger(bool b);
|
||||
@@ -46,24 +45,24 @@ struct Log : public std::ofstream {
|
||||
|
||||
namespace Time {
|
||||
typedef int64_t point;
|
||||
point now();
|
||||
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)]; }
|
||||
HashTable() : table(Size, Entry()) {}
|
||||
Entry* operator[](Key k) { return &table[(uint32_t)k & (Size - 1)]; }
|
||||
|
||||
private:
|
||||
std::vector<Entry> e;
|
||||
std::vector<Entry> table;
|
||||
};
|
||||
|
||||
|
||||
enum SyncCout { io_lock, io_unlock };
|
||||
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
|
||||
#define sync_cout std::cout << IO_LOCK
|
||||
#define sync_endl std::endl << IO_UNLOCK
|
||||
|
||||
#endif // !defined(MISC_H_INCLUDED)
|
||||
#endif // #ifndef MISC_H_INCLUDED
|
||||
|
||||
+137
-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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -22,88 +22,73 @@
|
||||
#include "movegen.h"
|
||||
#include "position.h"
|
||||
|
||||
/// Simple macro to wrap a very common while loop, no facny, no flexibility,
|
||||
/// hardcoded names 'mlist' and 'from'.
|
||||
#define SERIALIZE(b) while (b) (*mlist++).move = make_move(from, pop_lsb(&b))
|
||||
|
||||
/// Version used for pawns, where the 'from' square is given as a delta from the 'to' square
|
||||
#define SERIALIZE_PAWNS(b, d) while (b) { Square to = pop_lsb(&b); \
|
||||
(*mlist++).move = make_move(to - (d), to); }
|
||||
namespace {
|
||||
|
||||
template<CastlingSide Side, bool Checks, bool Chess960>
|
||||
MoveStack* generate_castle(const Position& pos, MoveStack* mlist, Color us) {
|
||||
template<CastlingRight Cr, bool Checks, bool Chess960>
|
||||
ExtMove* generate_castling(const Position& pos, ExtMove* mlist, Color us, const CheckInfo* ci) {
|
||||
|
||||
if (pos.castle_impeded(us, Side) || !pos.can_castle(make_castle_right(us, Side)))
|
||||
static const bool KingSide = (Cr == WHITE_OO || Cr == BLACK_OO);
|
||||
|
||||
if (pos.castling_impeded(Cr) || !pos.can_castle(Cr))
|
||||
return mlist;
|
||||
|
||||
// After castling, the rook and king final positions are the same in Chess960
|
||||
// as they would be in standard chess.
|
||||
Square kfrom = pos.king_square(us);
|
||||
Square rfrom = pos.castle_rook_square(us, Side);
|
||||
Square kto = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
|
||||
Square rfrom = pos.castling_rook_square(Cr);
|
||||
Square kto = relative_square(us, KingSide ? SQ_G1 : SQ_C1);
|
||||
Bitboard enemies = pos.pieces(~us);
|
||||
|
||||
assert(!pos.in_check());
|
||||
assert(!pos.checkers());
|
||||
|
||||
const int K = Chess960 ? kto > kfrom ? -1 : 1
|
||||
: Side == KING_SIDE ? -1 : 1;
|
||||
const Square K = Chess960 ? kto > kfrom ? DELTA_W : DELTA_E
|
||||
: KingSide ? DELTA_W : DELTA_E;
|
||||
|
||||
for (Square s = kto; s != kfrom; s += (Square)K)
|
||||
for (Square s = kto; s != kfrom; s += K)
|
||||
if (pos.attackers_to(s) & enemies)
|
||||
return mlist;
|
||||
|
||||
// Because we generate only legal castling moves we need to verify that
|
||||
// when moving the castling rook we do not discover some hidden checker.
|
||||
// For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
|
||||
if (Chess960 && (pos.attackers_to(kto, pos.pieces() ^ rfrom) & enemies))
|
||||
if (Chess960 && (attacks_bb<ROOK>(kto, pos.pieces() ^ rfrom) & pos.pieces(~us, ROOK, QUEEN)))
|
||||
return mlist;
|
||||
|
||||
(*mlist++).move = make<CASTLE>(kfrom, rfrom);
|
||||
Move m = make<CASTLING>(kfrom, rfrom);
|
||||
|
||||
if (Checks && !pos.move_gives_check((mlist - 1)->move, CheckInfo(pos)))
|
||||
mlist--;
|
||||
if (Checks && !pos.gives_check(m, *ci))
|
||||
return mlist;
|
||||
|
||||
(mlist++)->move = m;
|
||||
|
||||
return mlist;
|
||||
}
|
||||
|
||||
|
||||
template<Square Delta>
|
||||
inline Bitboard move_pawns(Bitboard p) {
|
||||
|
||||
return Delta == DELTA_N ? p << 8
|
||||
: Delta == DELTA_S ? p >> 8
|
||||
: Delta == DELTA_NE ? (p & ~FileHBB) << 9
|
||||
: Delta == DELTA_SE ? (p & ~FileHBB) >> 7
|
||||
: Delta == DELTA_NW ? (p & ~FileABB) << 7
|
||||
: Delta == DELTA_SW ? (p & ~FileABB) >> 9 : 0;
|
||||
}
|
||||
|
||||
|
||||
template<GenType Type, Square Delta>
|
||||
inline MoveStack* generate_promotions(MoveStack* mlist, Bitboard pawnsOn7,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
inline ExtMove* generate_promotions(ExtMove* mlist, Bitboard pawnsOn7,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
|
||||
Bitboard b = move_pawns<Delta>(pawnsOn7) & target;
|
||||
Bitboard b = shift_bb<Delta>(pawnsOn7) & target;
|
||||
|
||||
while (b)
|
||||
{
|
||||
Square to = pop_lsb(&b);
|
||||
|
||||
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
||||
(*mlist++).move = make<PROMOTION>(to - Delta, to, QUEEN);
|
||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, QUEEN);
|
||||
|
||||
if (Type == QUIETS || Type == EVASIONS || Type == NON_EVASIONS)
|
||||
{
|
||||
(*mlist++).move = make<PROMOTION>(to - Delta, to, ROOK);
|
||||
(*mlist++).move = make<PROMOTION>(to - Delta, to, BISHOP);
|
||||
(*mlist++).move = make<PROMOTION>(to - Delta, to, KNIGHT);
|
||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, ROOK);
|
||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, BISHOP);
|
||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
|
||||
}
|
||||
|
||||
// Knight-promotion is the only one that can give a direct check not
|
||||
// already included in the queen-promotion.
|
||||
// Knight promotion is the only promotion that can give a direct check
|
||||
// that's not already included in the queen promotion.
|
||||
if (Type == QUIET_CHECKS && (StepAttacksBB[W_KNIGHT][to] & ci->ksq))
|
||||
(*mlist++).move = make<PROMOTION>(to - Delta, to, KNIGHT);
|
||||
(mlist++)->move = make<PROMOTION>(to - Delta, to, KNIGHT);
|
||||
else
|
||||
(void)ci; // Silence a warning under MSVC
|
||||
}
|
||||
@@ -113,8 +98,8 @@ namespace {
|
||||
|
||||
|
||||
template<Color Us, GenType Type>
|
||||
MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
ExtMove* generate_pawn_moves(const Position& pos, ExtMove* mlist,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
|
||||
// Compute our parametrized parameters at compile time, named according to
|
||||
// the point of view of white side.
|
||||
@@ -122,9 +107,9 @@ namespace {
|
||||
const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
|
||||
const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
|
||||
const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
|
||||
const Square UP = (Us == WHITE ? DELTA_N : DELTA_S);
|
||||
const Square RIGHT = (Us == WHITE ? DELTA_NE : DELTA_SW);
|
||||
const Square LEFT = (Us == WHITE ? DELTA_NW : DELTA_SE);
|
||||
const 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);
|
||||
|
||||
Bitboard b1, b2, dc1, dc2, emptySquares;
|
||||
|
||||
@@ -139,8 +124,8 @@ namespace {
|
||||
{
|
||||
emptySquares = (Type == QUIETS || Type == QUIET_CHECKS ? target : ~pos.pieces());
|
||||
|
||||
b1 = move_pawns<UP>(pawnsNotOn7) & emptySquares;
|
||||
b2 = move_pawns<UP>(b1 & TRank3BB) & emptySquares;
|
||||
b1 = shift_bb<Up>(pawnsNotOn7) & emptySquares;
|
||||
b2 = shift_bb<Up>(b1 & TRank3BB) & emptySquares;
|
||||
|
||||
if (Type == EVASIONS) // Consider only blocking squares
|
||||
{
|
||||
@@ -156,19 +141,28 @@ namespace {
|
||||
// Add pawn pushes which give discovered check. This is possible only
|
||||
// if the pawn is not on the same file as the enemy king, because we
|
||||
// don't generate captures. Note that a possible discovery check
|
||||
// promotion has been already generated among captures.
|
||||
// promotion has been already generated amongst the captures.
|
||||
if (pawnsNotOn7 & ci->dcCandidates)
|
||||
{
|
||||
dc1 = move_pawns<UP>(pawnsNotOn7 & ci->dcCandidates) & emptySquares & ~file_bb(ci->ksq);
|
||||
dc2 = move_pawns<UP>(dc1 & TRank3BB) & emptySquares;
|
||||
dc1 = shift_bb<Up>(pawnsNotOn7 & ci->dcCandidates) & emptySquares & ~file_bb(ci->ksq);
|
||||
dc2 = shift_bb<Up>(dc1 & TRank3BB) & emptySquares;
|
||||
|
||||
b1 |= dc1;
|
||||
b2 |= dc2;
|
||||
}
|
||||
}
|
||||
|
||||
SERIALIZE_PAWNS(b1, UP);
|
||||
SERIALIZE_PAWNS(b2, UP + UP);
|
||||
while (b1)
|
||||
{
|
||||
Square to = pop_lsb(&b1);
|
||||
(mlist++)->move = make_move(to - Up, to);
|
||||
}
|
||||
|
||||
while (b2)
|
||||
{
|
||||
Square to = pop_lsb(&b2);
|
||||
(mlist++)->move = make_move(to - Up - Up, to);
|
||||
}
|
||||
}
|
||||
|
||||
// Promotions and underpromotions
|
||||
@@ -180,19 +174,28 @@ namespace {
|
||||
if (Type == EVASIONS)
|
||||
emptySquares &= target;
|
||||
|
||||
mlist = generate_promotions<Type, RIGHT>(mlist, pawnsOn7, enemies, ci);
|
||||
mlist = generate_promotions<Type, LEFT>(mlist, pawnsOn7, enemies, ci);
|
||||
mlist = generate_promotions<Type, UP>(mlist, pawnsOn7, emptySquares, ci);
|
||||
mlist = generate_promotions<Type, Right>(mlist, pawnsOn7, enemies, ci);
|
||||
mlist = generate_promotions<Type, Left >(mlist, pawnsOn7, enemies, ci);
|
||||
mlist = generate_promotions<Type, Up>(mlist, pawnsOn7, emptySquares, ci);
|
||||
}
|
||||
|
||||
// Standard and en-passant captures
|
||||
if (Type == CAPTURES || Type == EVASIONS || Type == NON_EVASIONS)
|
||||
{
|
||||
b1 = move_pawns<RIGHT>(pawnsNotOn7) & enemies;
|
||||
b2 = move_pawns<LEFT >(pawnsNotOn7) & enemies;
|
||||
b1 = shift_bb<Right>(pawnsNotOn7) & enemies;
|
||||
b2 = shift_bb<Left >(pawnsNotOn7) & enemies;
|
||||
|
||||
SERIALIZE_PAWNS(b1, RIGHT);
|
||||
SERIALIZE_PAWNS(b2, LEFT);
|
||||
while (b1)
|
||||
{
|
||||
Square to = pop_lsb(&b1);
|
||||
(mlist++)->move = make_move(to - Right, to);
|
||||
}
|
||||
|
||||
while (b2)
|
||||
{
|
||||
Square to = pop_lsb(&b2);
|
||||
(mlist++)->move = make_move(to - Left, to);
|
||||
}
|
||||
|
||||
if (pos.ep_square() != SQ_NONE)
|
||||
{
|
||||
@@ -201,7 +204,7 @@ namespace {
|
||||
// An en passant capture can be an evasion only if the checking piece
|
||||
// is the double pushed pawn and so is in the target. Otherwise this
|
||||
// is a discovery check and we are forced to do otherwise.
|
||||
if (Type == EVASIONS && !(target & (pos.ep_square() - UP)))
|
||||
if (Type == EVASIONS && !(target & (pos.ep_square() - Up)))
|
||||
return mlist;
|
||||
|
||||
b1 = pawnsNotOn7 & pos.attacks_from<PAWN>(pos.ep_square(), Them);
|
||||
@@ -209,7 +212,7 @@ namespace {
|
||||
assert(b1);
|
||||
|
||||
while (b1)
|
||||
(*mlist++).move = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
|
||||
(mlist++)->move = make<ENPASSANT>(pop_lsb(&b1), pos.ep_square());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,12 +221,12 @@ namespace {
|
||||
|
||||
|
||||
template<PieceType Pt, bool Checks> FORCE_INLINE
|
||||
MoveStack* generate_moves(const Position& pos, MoveStack* mlist, Color us,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
ExtMove* generate_moves(const Position& pos, ExtMove* mlist, Color us,
|
||||
Bitboard target, const CheckInfo* ci) {
|
||||
|
||||
assert(Pt != KING && Pt != PAWN);
|
||||
|
||||
const Square* pl = pos.piece_list(us, Pt);
|
||||
const Square* pl = pos.list<Pt>(us);
|
||||
|
||||
for (Square from = *pl; from != SQ_NONE; from = *++pl)
|
||||
{
|
||||
@@ -233,7 +236,7 @@ namespace {
|
||||
&& !(PseudoAttacks[Pt][from] & target & ci->checkSq[Pt]))
|
||||
continue;
|
||||
|
||||
if (ci->dcCandidates && (ci->dcCandidates & from))
|
||||
if (unlikely(ci->dcCandidates) && (ci->dcCandidates & from))
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -242,48 +245,45 @@ namespace {
|
||||
if (Checks)
|
||||
b &= ci->checkSq[Pt];
|
||||
|
||||
SERIALIZE(b);
|
||||
while (b)
|
||||
(mlist++)->move = make_move(from, pop_lsb(&b));
|
||||
}
|
||||
|
||||
return mlist;
|
||||
}
|
||||
|
||||
|
||||
FORCE_INLINE MoveStack* generate_king_moves(const Position& pos, MoveStack* mlist,
|
||||
Color us, Bitboard target) {
|
||||
Square from = pos.king_square(us);
|
||||
Bitboard b = pos.attacks_from<KING>(from) & target;
|
||||
SERIALIZE(b);
|
||||
return mlist;
|
||||
}
|
||||
template<Color Us, GenType Type> FORCE_INLINE
|
||||
ExtMove* generate_all(const Position& pos, ExtMove* mlist, Bitboard target,
|
||||
const CheckInfo* ci = NULL) {
|
||||
|
||||
const bool Checks = Type == QUIET_CHECKS;
|
||||
|
||||
template<GenType Type> FORCE_INLINE
|
||||
MoveStack* generate_all_moves(const Position& pos, MoveStack* mlist, Color us,
|
||||
Bitboard target, const CheckInfo* ci = NULL) {
|
||||
|
||||
mlist = (us == WHITE ? generate_pawn_moves<WHITE, Type>(pos, mlist, target, ci)
|
||||
: generate_pawn_moves<BLACK, Type>(pos, mlist, target, ci));
|
||||
|
||||
mlist = generate_moves<KNIGHT, Type == QUIET_CHECKS>(pos, mlist, us, target, ci);
|
||||
mlist = generate_moves<BISHOP, Type == QUIET_CHECKS>(pos, mlist, us, target, ci);
|
||||
mlist = generate_moves<ROOK, Type == QUIET_CHECKS>(pos, mlist, us, target, ci);
|
||||
mlist = generate_moves<QUEEN, Type == QUIET_CHECKS>(pos, mlist, us, target, ci);
|
||||
mlist = generate_pawn_moves<Us, Type>(pos, mlist, target, ci);
|
||||
mlist = generate_moves<KNIGHT, Checks>(pos, mlist, Us, target, ci);
|
||||
mlist = generate_moves<BISHOP, Checks>(pos, mlist, Us, target, ci);
|
||||
mlist = generate_moves< ROOK, Checks>(pos, mlist, Us, target, ci);
|
||||
mlist = generate_moves< QUEEN, Checks>(pos, mlist, Us, target, ci);
|
||||
|
||||
if (Type != QUIET_CHECKS && Type != EVASIONS)
|
||||
mlist = generate_king_moves(pos, mlist, us, target);
|
||||
{
|
||||
Square ksq = pos.king_square(Us);
|
||||
Bitboard b = pos.attacks_from<KING>(ksq) & target;
|
||||
while (b)
|
||||
(mlist++)->move = make_move(ksq, pop_lsb(&b));
|
||||
}
|
||||
|
||||
if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(us))
|
||||
if (Type != CAPTURES && Type != EVASIONS && pos.can_castle(Us))
|
||||
{
|
||||
if (pos.is_chess960())
|
||||
{
|
||||
mlist = generate_castle<KING_SIDE, Type == QUIET_CHECKS, true>(pos, mlist, us);
|
||||
mlist = generate_castle<QUEEN_SIDE, Type == QUIET_CHECKS, true>(pos, mlist, us);
|
||||
mlist = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, true>(pos, mlist, Us, ci);
|
||||
mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, true>(pos, mlist, Us, ci);
|
||||
}
|
||||
else
|
||||
{
|
||||
mlist = generate_castle<KING_SIDE, Type == QUIET_CHECKS, false>(pos, mlist, us);
|
||||
mlist = generate_castle<QUEEN_SIDE, Type == QUIET_CHECKS, false>(pos, mlist, us);
|
||||
mlist = generate_castling<MakeCastling<Us, KING_SIDE>::right, Checks, false>(pos, mlist, Us, ci);
|
||||
mlist = generate_castling<MakeCastling<Us, QUEEN_SIDE>::right, Checks, false>(pos, mlist, Us, ci);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -304,38 +304,33 @@ namespace {
|
||||
/// non-captures. Returns a pointer to the end of the move list.
|
||||
|
||||
template<GenType Type>
|
||||
MoveStack* generate(const Position& pos, MoveStack* mlist) {
|
||||
ExtMove* generate(const Position& pos, ExtMove* mlist) {
|
||||
|
||||
assert(Type == CAPTURES || Type == QUIETS || Type == NON_EVASIONS);
|
||||
assert(!pos.in_check());
|
||||
assert(!pos.checkers());
|
||||
|
||||
Color us = pos.side_to_move();
|
||||
Bitboard target;
|
||||
|
||||
if (Type == CAPTURES)
|
||||
target = pos.pieces(~us);
|
||||
Bitboard target = Type == CAPTURES ? pos.pieces(~us)
|
||||
: Type == QUIETS ? ~pos.pieces()
|
||||
: Type == NON_EVASIONS ? ~pos.pieces(us) : 0;
|
||||
|
||||
else if (Type == QUIETS)
|
||||
target = ~pos.pieces();
|
||||
|
||||
else if (Type == NON_EVASIONS)
|
||||
target = ~pos.pieces(us);
|
||||
|
||||
return generate_all_moves<Type>(pos, mlist, us, target);
|
||||
return us == WHITE ? generate_all<WHITE, Type>(pos, mlist, target)
|
||||
: generate_all<BLACK, Type>(pos, mlist, target);
|
||||
}
|
||||
|
||||
// Explicit template instantiations
|
||||
template MoveStack* generate<CAPTURES>(const Position&, MoveStack*);
|
||||
template MoveStack* generate<QUIETS>(const Position&, MoveStack*);
|
||||
template MoveStack* generate<NON_EVASIONS>(const Position&, MoveStack*);
|
||||
template ExtMove* generate<CAPTURES>(const Position&, ExtMove*);
|
||||
template ExtMove* generate<QUIETS>(const Position&, ExtMove*);
|
||||
template ExtMove* generate<NON_EVASIONS>(const Position&, ExtMove*);
|
||||
|
||||
|
||||
/// generate<QUIET_CHECKS> generates all pseudo-legal non-captures and knight
|
||||
/// underpromotions that give check. Returns a pointer to the end of the move list.
|
||||
template<>
|
||||
MoveStack* generate<QUIET_CHECKS>(const Position& pos, MoveStack* mlist) {
|
||||
ExtMove* generate<QUIET_CHECKS>(const Position& pos, ExtMove* mlist) {
|
||||
|
||||
assert(!pos.in_check());
|
||||
assert(!pos.checkers());
|
||||
|
||||
Color us = pos.side_to_move();
|
||||
CheckInfo ci(pos);
|
||||
@@ -347,99 +342,77 @@ MoveStack* generate<QUIET_CHECKS>(const Position& pos, MoveStack* mlist) {
|
||||
PieceType pt = type_of(pos.piece_on(from));
|
||||
|
||||
if (pt == PAWN)
|
||||
continue; // Will be generated togheter with direct checks
|
||||
continue; // Will be generated together with direct checks
|
||||
|
||||
Bitboard b = pos.attacks_from(Piece(pt), from) & ~pos.pieces();
|
||||
|
||||
if (pt == KING)
|
||||
b &= ~PseudoAttacks[QUEEN][ci.ksq];
|
||||
|
||||
SERIALIZE(b);
|
||||
while (b)
|
||||
(mlist++)->move = make_move(from, pop_lsb(&b));
|
||||
}
|
||||
|
||||
return generate_all_moves<QUIET_CHECKS>(pos, mlist, us, ~pos.pieces(), &ci);
|
||||
return us == WHITE ? generate_all<WHITE, QUIET_CHECKS>(pos, mlist, ~pos.pieces(), &ci)
|
||||
: generate_all<BLACK, QUIET_CHECKS>(pos, mlist, ~pos.pieces(), &ci);
|
||||
}
|
||||
|
||||
|
||||
/// generate<EVASIONS> generates all pseudo-legal check evasions when the side
|
||||
/// to move is in check. Returns a pointer to the end of the move list.
|
||||
template<>
|
||||
MoveStack* generate<EVASIONS>(const Position& pos, MoveStack* mlist) {
|
||||
|
||||
assert(pos.in_check());
|
||||
|
||||
Square from, checksq;
|
||||
int checkersCnt = 0;
|
||||
Color us = pos.side_to_move();
|
||||
Square ksq = pos.king_square(us);
|
||||
Bitboard sliderAttacks = 0;
|
||||
Bitboard b = pos.checkers();
|
||||
ExtMove* generate<EVASIONS>(const Position& pos, ExtMove* mlist) {
|
||||
|
||||
assert(pos.checkers());
|
||||
|
||||
// Find squares attacked by slider checkers, we will remove them from the king
|
||||
// evasions so to skip known illegal moves avoiding useless legality check later.
|
||||
do
|
||||
Color us = pos.side_to_move();
|
||||
Square ksq = pos.king_square(us);
|
||||
Bitboard sliderAttacks = 0;
|
||||
Bitboard sliders = pos.checkers() & ~pos.pieces(KNIGHT, PAWN);
|
||||
|
||||
// Find all the squares attacked by slider checkers. We will remove them from
|
||||
// the king evasions in order to skip known illegal moves, which avoids any
|
||||
// useless legality checks later on.
|
||||
while (sliders)
|
||||
{
|
||||
checkersCnt++;
|
||||
checksq = pop_lsb(&b);
|
||||
|
||||
assert(color_of(pos.piece_on(checksq)) == ~us);
|
||||
|
||||
switch (type_of(pos.piece_on(checksq)))
|
||||
{
|
||||
case BISHOP: sliderAttacks |= PseudoAttacks[BISHOP][checksq]; break;
|
||||
case ROOK: sliderAttacks |= PseudoAttacks[ROOK][checksq]; break;
|
||||
case QUEEN:
|
||||
// If queen and king are far or not on a diagonal line we can safely
|
||||
// remove all the squares attacked in the other direction becuase are
|
||||
// not reachable by the king anyway.
|
||||
if (between_bb(ksq, checksq) || !(PseudoAttacks[BISHOP][checksq] & ksq))
|
||||
sliderAttacks |= PseudoAttacks[QUEEN][checksq];
|
||||
|
||||
// Otherwise we need to use real rook attacks to check if king is safe
|
||||
// to move in the other direction. For example: king in B2, queen in A1
|
||||
// a knight in B1, and we can safely move to C1.
|
||||
else
|
||||
sliderAttacks |= PseudoAttacks[BISHOP][checksq] | pos.attacks_from<ROOK>(checksq);
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} while (b);
|
||||
Square checksq = pop_lsb(&sliders);
|
||||
sliderAttacks |= LineBB[checksq][ksq] ^ checksq;
|
||||
}
|
||||
|
||||
// Generate evasions for king, capture and non capture moves
|
||||
b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
|
||||
from = ksq;
|
||||
SERIALIZE(b);
|
||||
Bitboard b = pos.attacks_from<KING>(ksq) & ~pos.pieces(us) & ~sliderAttacks;
|
||||
while (b)
|
||||
(mlist++)->move = make_move(ksq, pop_lsb(&b));
|
||||
|
||||
if (checkersCnt > 1)
|
||||
if (more_than_one(pos.checkers()))
|
||||
return mlist; // Double check, only a king move can save the day
|
||||
|
||||
// Generate blocking evasions or captures of the checking piece
|
||||
Bitboard target = between_bb(checksq, ksq) | pos.checkers();
|
||||
Square checksq = lsb(pos.checkers());
|
||||
Bitboard target = between_bb(checksq, ksq) | checksq;
|
||||
|
||||
return generate_all_moves<EVASIONS>(pos, mlist, us, target);
|
||||
return us == WHITE ? generate_all<WHITE, EVASIONS>(pos, mlist, target)
|
||||
: generate_all<BLACK, EVASIONS>(pos, mlist, target);
|
||||
}
|
||||
|
||||
|
||||
/// generate<LEGAL> generates all the legal moves in the given position
|
||||
|
||||
template<>
|
||||
MoveStack* generate<LEGAL>(const Position& pos, MoveStack* mlist) {
|
||||
ExtMove* generate<LEGAL>(const Position& pos, ExtMove* mlist) {
|
||||
|
||||
MoveStack *end, *cur = mlist;
|
||||
Bitboard pinned = pos.pinned_pieces();
|
||||
ExtMove *end, *cur = mlist;
|
||||
Bitboard pinned = pos.pinned_pieces(pos.side_to_move());
|
||||
Square ksq = pos.king_square(pos.side_to_move());
|
||||
|
||||
end = pos.in_check() ? generate<EVASIONS>(pos, mlist)
|
||||
end = pos.checkers() ? generate<EVASIONS>(pos, mlist)
|
||||
: generate<NON_EVASIONS>(pos, mlist);
|
||||
while (cur != end)
|
||||
if ( (pinned || from_sq(cur->move) == ksq || type_of(cur->move) == ENPASSANT)
|
||||
&& !pos.pl_move_is_legal(cur->move, pinned))
|
||||
&& !pos.legal(cur->move, pinned))
|
||||
cur->move = (--end)->move;
|
||||
else
|
||||
cur++;
|
||||
++cur;
|
||||
|
||||
return end;
|
||||
}
|
||||
|
||||
+15
-12
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +17,7 @@
|
||||
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
|
||||
|
||||
#include "types.h"
|
||||
@@ -34,22 +34,25 @@ enum GenType {
|
||||
class Position;
|
||||
|
||||
template<GenType>
|
||||
MoveStack* generate(const Position& pos, MoveStack* mlist);
|
||||
ExtMove* generate(const Position& pos, ExtMove* mlist);
|
||||
|
||||
/// The MoveList struct is a simple wrapper around generate(), sometimes comes
|
||||
/// handy to use this class instead of the low level generate() function.
|
||||
/// The MoveList struct is a simple wrapper around generate(). It sometimes comes
|
||||
/// in handy to use this class instead of the low level generate() function.
|
||||
template<GenType T>
|
||||
struct MoveList {
|
||||
|
||||
explicit MoveList(const Position& pos) : cur(mlist), last(generate<T>(pos, mlist)) {}
|
||||
void operator++() { cur++; }
|
||||
bool end() const { return cur == last; }
|
||||
Move move() const { return cur->move; }
|
||||
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;
|
||||
}
|
||||
|
||||
private:
|
||||
MoveStack mlist[MAX_MOVES];
|
||||
MoveStack *cur, *last;
|
||||
ExtMove mlist[MAX_MOVES];
|
||||
ExtMove *cur, *last;
|
||||
};
|
||||
|
||||
#endif // !defined(MOVEGEN_H_INCLUDED)
|
||||
#endif // #ifndef MOVEGEN_H_INCLUDED
|
||||
|
||||
+139
-111
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -18,16 +18,14 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
#include "movegen.h"
|
||||
#include "movepick.h"
|
||||
#include "thread.h"
|
||||
|
||||
namespace {
|
||||
|
||||
enum Sequencer {
|
||||
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,
|
||||
@@ -37,14 +35,28 @@ namespace {
|
||||
STOP
|
||||
};
|
||||
|
||||
// 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 MoveStack& ms) { return ms.score > 0; }
|
||||
// Our insertion sort, which is guaranteed (and also needed) to be stable
|
||||
void insertion_sort(ExtMove* begin, ExtMove* end)
|
||||
{
|
||||
ExtMove tmp, *p, *q;
|
||||
|
||||
// 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 MoveStack* pick_best(MoveStack* begin, MoveStack* end)
|
||||
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 values from remaining
|
||||
// ones so as to sort the two sets separately, with the second sort delayed.
|
||||
inline bool has_positive_value(const ExtMove& ms) { return ms.value > 0; }
|
||||
|
||||
// Picks the best move in the range (begin, end) and moves it to the front.
|
||||
// It's faster than sorting all the moves in advance when there are few
|
||||
// moves e.g. possible captures.
|
||||
inline ExtMove* pick_best(ExtMove* begin, ExtMove* end)
|
||||
{
|
||||
std::swap(*begin, *std::max_element(begin, end));
|
||||
return begin;
|
||||
@@ -53,100 +65,87 @@ namespace {
|
||||
|
||||
|
||||
/// Constructors of the MovePicker class. As arguments we pass information
|
||||
/// to help it to return the presumably good moves first, to decide which
|
||||
/// to help it to return the (presumably) good moves first, to decide which
|
||||
/// moves to return (in the quiescence search, for instance, we only want to
|
||||
/// search captures, promotions and some checks) and about how important good
|
||||
/// move ordering is at the current node.
|
||||
/// search captures, promotions and some checks) and how important good move
|
||||
/// ordering is at the current node.
|
||||
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
|
||||
Search::Stack* s, Value beta) : pos(p), H(h), depth(d) {
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
|
||||
Move* cm, Move* fm, Search::Stack* s) : pos(p), history(h), depth(d) {
|
||||
|
||||
assert(d > DEPTH_ZERO);
|
||||
|
||||
captureThreshold = 0;
|
||||
cur = end = moves;
|
||||
endBadCaptures = moves + MAX_MOVES - 1;
|
||||
countermoves = cm;
|
||||
followupmoves = fm;
|
||||
ss = s;
|
||||
|
||||
if (p.in_check())
|
||||
phase = EVASION;
|
||||
if (pos.checkers())
|
||||
stage = EVASION;
|
||||
|
||||
else
|
||||
{
|
||||
phase = MAIN_SEARCH;
|
||||
stage = MAIN_SEARCH;
|
||||
|
||||
killers[0].move = ss->killers[0];
|
||||
killers[1].move = ss->killers[1];
|
||||
|
||||
// Consider sligtly negative captures as good if at low depth and far from beta
|
||||
if (ss && ss->eval < beta - PawnValueMg && d < 3 * ONE_PLY)
|
||||
captureThreshold = -PawnValueMg;
|
||||
|
||||
// Consider negative captures as good if still enough to reach beta
|
||||
else if (ss && ss->eval > beta)
|
||||
captureThreshold = beta - ss->eval;
|
||||
}
|
||||
|
||||
ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
end += (ttMove != MOVE_NONE);
|
||||
}
|
||||
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
|
||||
Square sq) : pos(p), H(h), cur(moves), end(moves) {
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
|
||||
Square s) : pos(p), history(h), cur(moves), end(moves) {
|
||||
|
||||
assert(d <= DEPTH_ZERO);
|
||||
|
||||
if (p.in_check())
|
||||
phase = EVASION;
|
||||
if (pos.checkers())
|
||||
stage = EVASION;
|
||||
|
||||
else if (d > DEPTH_QS_NO_CHECKS)
|
||||
phase = QSEARCH_0;
|
||||
stage = QSEARCH_0;
|
||||
|
||||
else if (d > DEPTH_QS_RECAPTURES)
|
||||
{
|
||||
phase = QSEARCH_1;
|
||||
stage = QSEARCH_1;
|
||||
|
||||
// Skip TT move if is not a capture or a promotion, this avoids qsearch
|
||||
// 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))
|
||||
if (ttm && !pos.capture_or_promotion(ttm))
|
||||
ttm = MOVE_NONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
phase = RECAPTURE;
|
||||
recaptureSquare = sq;
|
||||
stage = RECAPTURE;
|
||||
recaptureSquare = s;
|
||||
ttm = MOVE_NONE;
|
||||
}
|
||||
|
||||
ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
end += (ttMove != MOVE_NONE);
|
||||
}
|
||||
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, const History& h, PieceType pt)
|
||||
: pos(p), H(h), cur(moves), end(moves) {
|
||||
MovePicker::MovePicker(const Position& p, Move ttm, const HistoryStats& h, PieceType pt)
|
||||
: pos(p), history(h), cur(moves), end(moves) {
|
||||
|
||||
assert(!pos.in_check());
|
||||
assert(!pos.checkers());
|
||||
|
||||
phase = PROBCUT;
|
||||
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);
|
||||
// In ProbCut we generate only captures that are better than the parent's
|
||||
// captured piece.
|
||||
captureThreshold = PieceValue[MG][pt];
|
||||
ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
|
||||
|
||||
if (ttMove && (!pos.is_capture(ttMove) || pos.see(ttMove) <= captureThreshold))
|
||||
if (ttMove && (!pos.capture(ttMove) || pos.see(ttMove) <= captureThreshold))
|
||||
ttMove = MOVE_NONE;
|
||||
|
||||
end += (ttMove != MOVE_NONE);
|
||||
}
|
||||
|
||||
|
||||
/// 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 next_move().
|
||||
|
||||
void MovePicker::score_captures() {
|
||||
/// score() assign a numerical value to each move in a move list. The moves with
|
||||
/// highest values will be picked first.
|
||||
template<>
|
||||
void MovePicker::score<CAPTURES>() {
|
||||
// Winning and equal captures in the main search are ordered by MVV/LVA.
|
||||
// Suprisingly, this appears to perform slightly better than SEE based
|
||||
// move ordering. The reason is probably that in a position with a winning
|
||||
@@ -156,89 +155,114 @@ void MovePicker::score_captures() {
|
||||
// where it is possible to recapture with the hanging piece). Exchanging
|
||||
// big pieces before capturing a hanging piece probably helps to reduce
|
||||
// the subtree size.
|
||||
// In main search we want to push captures with negative SEE values to
|
||||
// badCaptures[] array, but instead of doing it now we delay till when
|
||||
// the move has been picked up in pick_move_from_list(), this way we save
|
||||
// some SEE calls in case we get a cutoff (idea from Pablo Vazquez).
|
||||
// In main search we want to push captures with negative SEE values to the
|
||||
// badCaptures[] array, but instead of doing it now we delay until the move
|
||||
// has been picked up in pick_move_from_list(). This way we save some SEE
|
||||
// calls in case we get a cutoff.
|
||||
Move m;
|
||||
|
||||
for (MoveStack* it = moves; it != end; ++it)
|
||||
for (ExtMove* it = moves; it != end; ++it)
|
||||
{
|
||||
m = it->move;
|
||||
it->score = PieceValue[Mg][pos.piece_on(to_sq(m))]
|
||||
- type_of(pos.piece_moved(m));
|
||||
it->value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||
- Value(type_of(pos.moved_piece(m)));
|
||||
|
||||
if (type_of(m) == PROMOTION)
|
||||
it->score += PieceValue[Mg][promotion_type(m)];
|
||||
if (type_of(m) == ENPASSANT)
|
||||
it->value += PieceValue[MG][PAWN];
|
||||
|
||||
else if (type_of(m) == PROMOTION)
|
||||
it->value += PieceValue[MG][promotion_type(m)] - PieceValue[MG][PAWN];
|
||||
}
|
||||
}
|
||||
|
||||
void MovePicker::score_noncaptures() {
|
||||
template<>
|
||||
void MovePicker::score<QUIETS>() {
|
||||
|
||||
Move m;
|
||||
|
||||
for (MoveStack* it = moves; it != end; ++it)
|
||||
for (ExtMove* it = moves; it != end; ++it)
|
||||
{
|
||||
m = it->move;
|
||||
it->score = H.value(pos.piece_moved(m), to_sq(m));
|
||||
it->value = history[pos.moved_piece(m)][to_sq(m)];
|
||||
}
|
||||
}
|
||||
|
||||
void MovePicker::score_evasions() {
|
||||
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.
|
||||
// moves with a negative SEE. This last group is ordered by the SEE value.
|
||||
Move m;
|
||||
int seeScore;
|
||||
Value see;
|
||||
|
||||
if (end < moves + 2)
|
||||
return;
|
||||
|
||||
for (MoveStack* it = moves; it != end; ++it)
|
||||
for (ExtMove* it = moves; it != end; ++it)
|
||||
{
|
||||
m = it->move;
|
||||
if ((seeScore = pos.see_sign(m)) < 0)
|
||||
it->score = seeScore - History::MaxValue; // Be sure we are 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)) + History::MaxValue;
|
||||
if ((see = pos.see_sign(m)) < VALUE_ZERO)
|
||||
it->value = see - HistoryStats::Max; // At the bottom
|
||||
|
||||
else if (pos.capture(m))
|
||||
it->value = PieceValue[MG][pos.piece_on(to_sq(m))]
|
||||
- Value(type_of(pos.moved_piece(m))) + HistoryStats::Max;
|
||||
else
|
||||
it->score = H.value(pos.piece_moved(m), to_sq(m));
|
||||
it->value = history[pos.moved_piece(m)][to_sq(m)];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// MovePicker::generate_next() generates, scores and sorts the next bunch of moves,
|
||||
/// when there are no more moves to try for the current phase.
|
||||
/// generate_next_stage() generates, scores and sorts the next bunch of moves,
|
||||
/// when there are no more moves to try for the current stage.
|
||||
|
||||
void MovePicker::generate_next() {
|
||||
void MovePicker::generate_next_stage() {
|
||||
|
||||
cur = moves;
|
||||
|
||||
switch (++phase) {
|
||||
switch (++stage) {
|
||||
|
||||
case CAPTURES_S1: case CAPTURES_S3: case CAPTURES_S4: case CAPTURES_S5: case CAPTURES_S6:
|
||||
end = generate<CAPTURES>(pos, moves);
|
||||
score_captures();
|
||||
score<CAPTURES>();
|
||||
return;
|
||||
|
||||
case KILLERS_S1:
|
||||
cur = killers;
|
||||
end = cur + 2;
|
||||
|
||||
killers[0].move = ss->killers[0];
|
||||
killers[1].move = ss->killers[1];
|
||||
killers[2].move = killers[3].move = MOVE_NONE;
|
||||
killers[4].move = killers[5].move = MOVE_NONE;
|
||||
|
||||
// Please note that following code is racy and could yield to rare (less
|
||||
// than 1 out of a million) duplicated entries in SMP case. This is harmless.
|
||||
|
||||
// Be sure countermoves are different from killers
|
||||
for (int i = 0; i < 2; ++i)
|
||||
if ( countermoves[i] != (cur+0)->move
|
||||
&& countermoves[i] != (cur+1)->move)
|
||||
(end++)->move = countermoves[i];
|
||||
|
||||
// Be sure followupmoves are different from killers and countermoves
|
||||
for (int i = 0; i < 2; ++i)
|
||||
if ( followupmoves[i] != (cur+0)->move
|
||||
&& followupmoves[i] != (cur+1)->move
|
||||
&& followupmoves[i] != (cur+2)->move
|
||||
&& followupmoves[i] != (cur+3)->move)
|
||||
(end++)->move = followupmoves[i];
|
||||
return;
|
||||
|
||||
case QUIETS_1_S1:
|
||||
endQuiets = end = generate<QUIETS>(pos, moves);
|
||||
score_noncaptures();
|
||||
end = std::partition(cur, end, has_positive_score);
|
||||
sort<MoveStack>(cur, end);
|
||||
score<QUIETS>();
|
||||
end = std::partition(cur, end, has_positive_value);
|
||||
insertion_sort(cur, end);
|
||||
return;
|
||||
|
||||
case QUIETS_2_S1:
|
||||
cur = end;
|
||||
end = endQuiets;
|
||||
if (depth >= 3 * ONE_PLY)
|
||||
sort<MoveStack>(cur, end);
|
||||
insertion_sort(cur, end);
|
||||
return;
|
||||
|
||||
case BAD_CAPTURES_S1:
|
||||
@@ -249,7 +273,8 @@ void MovePicker::generate_next() {
|
||||
|
||||
case EVASIONS_S2:
|
||||
end = generate<EVASIONS>(pos, moves);
|
||||
score_evasions();
|
||||
if (end > moves + 1)
|
||||
score<EVASIONS>();
|
||||
return;
|
||||
|
||||
case QUIET_CHECKS_S3:
|
||||
@@ -257,7 +282,9 @@ void MovePicker::generate_next() {
|
||||
return;
|
||||
|
||||
case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT: case RECAPTURE:
|
||||
phase = STOP;
|
||||
stage = STOP;
|
||||
/* Fall through */
|
||||
|
||||
case STOP:
|
||||
end = cur + 1; // Avoid another next_phase() call
|
||||
return;
|
||||
@@ -268,11 +295,10 @@ void MovePicker::generate_next() {
|
||||
}
|
||||
|
||||
|
||||
/// MovePicker::next_move() is the most important method of the MovePicker class.
|
||||
/// It returns a new pseudo legal move every time it is called, until there
|
||||
/// are no more moves left. 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.
|
||||
/// next_move() is the most important method of the MovePicker class. It returns
|
||||
/// a new pseudo legal move every time it is called, until there are no more moves
|
||||
/// left. It picks the move with the biggest value from a list of generated moves
|
||||
/// taking care not to return the ttMove if it has already been searched.
|
||||
template<>
|
||||
Move MovePicker::next_move<false>() {
|
||||
|
||||
@@ -281,21 +307,19 @@ Move MovePicker::next_move<false>() {
|
||||
while (true)
|
||||
{
|
||||
while (cur == end)
|
||||
generate_next();
|
||||
generate_next_stage();
|
||||
|
||||
switch (phase) {
|
||||
switch (stage) {
|
||||
|
||||
case MAIN_SEARCH: case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT:
|
||||
cur++;
|
||||
++cur;
|
||||
return ttMove;
|
||||
|
||||
case CAPTURES_S1:
|
||||
move = pick_best(cur++, end)->move;
|
||||
if (move != ttMove)
|
||||
{
|
||||
assert(captureThreshold <= 0); // Otherwise we cannot use see_sign()
|
||||
|
||||
if (pos.see_sign(move) >= captureThreshold)
|
||||
if (pos.see_sign(move) >= VALUE_ZERO)
|
||||
return move;
|
||||
|
||||
// Losing capture, move it to the tail of the array
|
||||
@@ -306,9 +330,9 @@ Move MovePicker::next_move<false>() {
|
||||
case KILLERS_S1:
|
||||
move = (cur++)->move;
|
||||
if ( move != MOVE_NONE
|
||||
&& pos.is_pseudo_legal(move)
|
||||
&& move != ttMove
|
||||
&& !pos.is_capture(move))
|
||||
&& pos.pseudo_legal(move)
|
||||
&& !pos.capture(move))
|
||||
return move;
|
||||
break;
|
||||
|
||||
@@ -316,7 +340,11 @@ Move MovePicker::next_move<false>() {
|
||||
move = (cur++)->move;
|
||||
if ( move != ttMove
|
||||
&& move != killers[0].move
|
||||
&& move != killers[1].move)
|
||||
&& move != killers[1].move
|
||||
&& move != killers[2].move
|
||||
&& move != killers[3].move
|
||||
&& move != killers[4].move
|
||||
&& move != killers[5].move)
|
||||
return move;
|
||||
break;
|
||||
|
||||
@@ -359,6 +387,6 @@ Move MovePicker::next_move<false>() {
|
||||
|
||||
/// 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 should be lock protected by the caller.
|
||||
/// safe so must be lock protected by the caller.
|
||||
template<>
|
||||
Move MovePicker::next_move<true>() { return ss->sp->mp->next_move<false>(); }
|
||||
Move MovePicker::next_move<true>() { return ss->splitPoint->movePicker->next_move<false>(); }
|
||||
|
||||
+64
-16
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,15 +17,61 @@
|
||||
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
|
||||
|
||||
#include "history.h"
|
||||
#include <algorithm> // For std::max
|
||||
#include <cstring> // For std::memset
|
||||
|
||||
#include "movegen.h"
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
#include "types.h"
|
||||
|
||||
|
||||
/// The Stats struct stores moves statistics. According to the template parameter
|
||||
/// the class can store History, Gains and Countermoves. History records how often
|
||||
/// different moves have been successful or unsuccessful during the current search
|
||||
/// and is used for reduction and move ordering decisions. Gains records the move's
|
||||
/// best evaluation gain from one ply to the next and is used for pruning decisions.
|
||||
/// Countermoves store the move that refute a previous one. Entries are stored
|
||||
/// using only the moving piece and destination square, hence two moves with
|
||||
/// different origin but same destination and piece will be considered identical.
|
||||
template<bool Gain, typename T>
|
||||
struct Stats {
|
||||
|
||||
static const Value Max = Value(2000);
|
||||
|
||||
const T* operator[](Piece pc) const { return table[pc]; }
|
||||
void clear() { std::memset(table, 0, sizeof(table)); }
|
||||
|
||||
void update(Piece pc, Square to, Move m) {
|
||||
|
||||
if (m == table[pc][to].first)
|
||||
return;
|
||||
|
||||
table[pc][to].second = table[pc][to].first;
|
||||
table[pc][to].first = m;
|
||||
}
|
||||
|
||||
void update(Piece pc, Square to, Value v) {
|
||||
|
||||
if (Gain)
|
||||
table[pc][to] = std::max(v, table[pc][to] - 1);
|
||||
|
||||
else if (abs(table[pc][to] + v) < Max)
|
||||
table[pc][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> > MovesStats;
|
||||
|
||||
|
||||
/// 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,
|
||||
@@ -38,27 +84,29 @@ class MovePicker {
|
||||
MovePicker& operator=(const MovePicker&); // Silence a warning under MSVC
|
||||
|
||||
public:
|
||||
MovePicker(const Position&, Move, Depth, const History&, Search::Stack*, Value);
|
||||
MovePicker(const Position&, Move, Depth, const History&, Square);
|
||||
MovePicker(const Position&, Move, const History&, PieceType);
|
||||
MovePicker(const Position&, Move, Depth, const HistoryStats&, Square);
|
||||
MovePicker(const Position&, Move, const HistoryStats&, PieceType);
|
||||
MovePicker(const Position&, Move, Depth, const HistoryStats&, Move*, Move*, Search::Stack*);
|
||||
|
||||
template<bool SpNode> Move next_move();
|
||||
|
||||
private:
|
||||
void score_captures();
|
||||
void score_noncaptures();
|
||||
void score_evasions();
|
||||
void generate_next();
|
||||
template<GenType> void score();
|
||||
void generate_next_stage();
|
||||
|
||||
const Position& pos;
|
||||
const History& H;
|
||||
const HistoryStats& history;
|
||||
Search::Stack* ss;
|
||||
Move* countermoves;
|
||||
Move* followupmoves;
|
||||
Depth depth;
|
||||
Move ttMove;
|
||||
MoveStack killers[2];
|
||||
ExtMove killers[6];
|
||||
Square recaptureSquare;
|
||||
int captureThreshold, phase;
|
||||
MoveStack *cur, *end, *endQuiets, *endBadCaptures;
|
||||
MoveStack moves[MAX_MOVES];
|
||||
Value captureThreshold;
|
||||
int stage;
|
||||
ExtMove *cur, *end, *endQuiets, *endBadCaptures;
|
||||
ExtMove moves[MAX_MOVES];
|
||||
};
|
||||
|
||||
#endif // !defined(MOVEPICK_H_INCLUDED)
|
||||
#endif // #ifndef MOVEPICK_H_INCLUDED
|
||||
|
||||
+72
-87
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -20,7 +20,7 @@
|
||||
#include <cassert>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <stack>
|
||||
|
||||
#include "movegen.h"
|
||||
#include "notation.h"
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
static const char* PieceToChar = " PNBRQK pnbrqk";
|
||||
static const char* PieceToChar[COLOR_NB] = { " PNBRQK", " pnbrqk" };
|
||||
|
||||
|
||||
/// score_to_uci() converts a value to a string suitable for use with the UCI
|
||||
@@ -40,23 +40,23 @@ static const char* PieceToChar = " PNBRQK pnbrqk";
|
||||
|
||||
string score_to_uci(Value v, Value alpha, Value beta) {
|
||||
|
||||
stringstream s;
|
||||
stringstream ss;
|
||||
|
||||
if (abs(v) < VALUE_MATE_IN_MAX_PLY)
|
||||
s << "cp " << v * 100 / int(PawnValueMg);
|
||||
ss << "cp " << v * 100 / PawnValueEg;
|
||||
else
|
||||
s << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
|
||||
ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
|
||||
|
||||
s << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
|
||||
ss << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
|
||||
|
||||
return s.str();
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
||||
/// move_to_uci() converts a move to a string in coordinate notation
|
||||
/// (g1f3, a7a8q, etc.). The only special case is castling moves, where we print
|
||||
/// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960
|
||||
/// mode. Internally castle moves are always coded as "king captures rook".
|
||||
/// mode. Internally castling moves are always encoded as "king captures rook".
|
||||
|
||||
const string move_to_uci(Move m, bool chess960) {
|
||||
|
||||
@@ -69,13 +69,13 @@ const string move_to_uci(Move m, bool chess960) {
|
||||
if (m == MOVE_NULL)
|
||||
return "0000";
|
||||
|
||||
if (type_of(m) == CASTLE && !chess960)
|
||||
to = (to > from ? FILE_G : FILE_C) | rank_of(from);
|
||||
if (type_of(m) == CASTLING && !chess960)
|
||||
to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
|
||||
|
||||
string move = square_to_string(from) + square_to_string(to);
|
||||
string move = to_string(from) + to_string(to);
|
||||
|
||||
if (type_of(m) == PROMOTION)
|
||||
move += PieceToChar[make_piece(BLACK, promotion_type(m))]; // Lower case
|
||||
move += PieceToChar[BLACK][promotion_type(m)]; // Lower case
|
||||
|
||||
return move;
|
||||
}
|
||||
@@ -89,9 +89,9 @@ 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> ml(pos); !ml.end(); ++ml)
|
||||
if (str == move_to_uci(ml.move(), pos.is_chess960()))
|
||||
return ml.move();
|
||||
for (MoveList<LEGAL> it(pos); *it; ++it)
|
||||
if (str == move_to_uci(*it, pos.is_chess960()))
|
||||
return *it;
|
||||
|
||||
return MOVE_NONE;
|
||||
}
|
||||
@@ -108,10 +108,9 @@ const string move_to_san(Position& pos, Move m) {
|
||||
if (m == MOVE_NULL)
|
||||
return "(null)";
|
||||
|
||||
assert(pos.move_is_legal(m));
|
||||
assert(MoveList<LEGAL>(pos).contains(m));
|
||||
|
||||
Bitboard attackers;
|
||||
bool ambiguousMove, ambiguousFile, ambiguousRank;
|
||||
Bitboard others, b;
|
||||
string san;
|
||||
Color us = pos.side_to_move();
|
||||
Square from = from_sq(m);
|
||||
@@ -119,58 +118,50 @@ const string move_to_san(Position& pos, Move m) {
|
||||
Piece pc = pos.piece_on(from);
|
||||
PieceType pt = type_of(pc);
|
||||
|
||||
if (type_of(m) == CASTLE)
|
||||
if (type_of(m) == CASTLING)
|
||||
san = to > from ? "O-O" : "O-O-O";
|
||||
else
|
||||
{
|
||||
if (pt != PAWN)
|
||||
{
|
||||
san = PieceToChar[pt]; // Upper case
|
||||
san = PieceToChar[WHITE][pt]; // Upper case
|
||||
|
||||
// Disambiguation if we have more then one piece with destination 'to'
|
||||
// note that for pawns is not needed because starting file is explicit.
|
||||
ambiguousMove = ambiguousFile = ambiguousRank = false;
|
||||
// A disambiguation occurs if we have more then one piece of type 'pt'
|
||||
// that can reach 'to' with a legal move.
|
||||
others = b = (pos.attacks_from(pc, to) & pos.pieces(us, pt)) ^ from;
|
||||
|
||||
attackers = (pos.attacks_from(pc, to) & pos.pieces(us, pt)) ^ from;
|
||||
|
||||
while (attackers)
|
||||
while (b)
|
||||
{
|
||||
Square sq = pop_lsb(&attackers);
|
||||
|
||||
// Pinned pieces are not included in the possible sub-set
|
||||
if (!pos.pl_move_is_legal(make_move(sq, to), pos.pinned_pieces()))
|
||||
continue;
|
||||
|
||||
ambiguousFile |= file_of(sq) == file_of(from);
|
||||
ambiguousRank |= rank_of(sq) == rank_of(from);
|
||||
ambiguousMove = true;
|
||||
Square s = pop_lsb(&b);
|
||||
if (!pos.legal(make_move(s, to), pos.pinned_pieces(us)))
|
||||
others ^= s;
|
||||
}
|
||||
|
||||
if (ambiguousMove)
|
||||
{
|
||||
if (!ambiguousFile)
|
||||
san += file_to_char(file_of(from));
|
||||
if (!others)
|
||||
{ /* Disambiguation is not needed */ }
|
||||
|
||||
else if (!ambiguousRank)
|
||||
san += rank_to_char(rank_of(from));
|
||||
else if (!(others & file_bb(from)))
|
||||
san += to_char(file_of(from));
|
||||
|
||||
else
|
||||
san += square_to_string(from);
|
||||
}
|
||||
else if (!(others & rank_bb(from)))
|
||||
san += to_char(rank_of(from));
|
||||
|
||||
else
|
||||
san += to_string(from);
|
||||
}
|
||||
else if (pos.is_capture(m))
|
||||
san = file_to_char(file_of(from));
|
||||
else if (pos.capture(m))
|
||||
san = to_char(file_of(from));
|
||||
|
||||
if (pos.is_capture(m))
|
||||
if (pos.capture(m))
|
||||
san += 'x';
|
||||
|
||||
san += square_to_string(to);
|
||||
san += to_string(to);
|
||||
|
||||
if (type_of(m) == PROMOTION)
|
||||
san += string("=") + PieceToChar[promotion_type(m)];
|
||||
san += string("=") + PieceToChar[WHITE][promotion_type(m)];
|
||||
}
|
||||
|
||||
if (pos.move_gives_check(m, CheckInfo(pos)))
|
||||
if (pos.gives_check(m, CheckInfo(pos)))
|
||||
{
|
||||
StateInfo st;
|
||||
pos.do_move(m, st);
|
||||
@@ -184,9 +175,9 @@ const string move_to_san(Position& pos, Move m) {
|
||||
|
||||
/// 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.
|
||||
/// format the time and score respectively.
|
||||
|
||||
static string time_to_string(int64_t msecs) {
|
||||
static string format(int64_t msecs) {
|
||||
|
||||
const int MSecMinute = 1000 * 60;
|
||||
const int MSecHour = 1000 * 60 * 60;
|
||||
@@ -195,77 +186,71 @@ static string time_to_string(int64_t msecs) {
|
||||
int64_t minutes = (msecs % MSecHour) / MSecMinute;
|
||||
int64_t seconds = ((msecs % MSecHour) % MSecMinute) / 1000;
|
||||
|
||||
stringstream s;
|
||||
stringstream ss;
|
||||
|
||||
if (hours)
|
||||
s << hours << ':';
|
||||
ss << hours << ':';
|
||||
|
||||
s << setfill('0') << setw(2) << minutes << ':' << setw(2) << seconds;
|
||||
ss << setfill('0') << setw(2) << minutes << ':' << setw(2) << seconds;
|
||||
|
||||
return s.str();
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
static string score_to_string(Value v) {
|
||||
static string format(Value v) {
|
||||
|
||||
stringstream s;
|
||||
stringstream ss;
|
||||
|
||||
if (v >= VALUE_MATE_IN_MAX_PLY)
|
||||
s << "#" << (VALUE_MATE - v + 1) / 2;
|
||||
ss << "#" << (VALUE_MATE - v + 1) / 2;
|
||||
|
||||
else if (v <= VALUE_MATED_IN_MAX_PLY)
|
||||
s << "-#" << (VALUE_MATE + v) / 2;
|
||||
ss << "-#" << (VALUE_MATE + v) / 2;
|
||||
|
||||
else
|
||||
s << setprecision(2) << fixed << showpos << float(v) / PawnValueMg;
|
||||
ss << setprecision(2) << fixed << showpos << double(v) / PawnValueEg;
|
||||
|
||||
return s.str();
|
||||
return ss.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;
|
||||
const uint64_t K = 1000;
|
||||
const uint64_t M = 1000000;
|
||||
|
||||
StateInfo state[MAX_PLY_PLUS_2], *st = state;
|
||||
std::stack<StateInfo> st;
|
||||
Move* m = pv;
|
||||
string san, padding;
|
||||
size_t length;
|
||||
stringstream s;
|
||||
string san, str, padding;
|
||||
stringstream ss;
|
||||
|
||||
s << setw(2) << depth
|
||||
<< setw(8) << score_to_string(value)
|
||||
<< setw(8) << time_to_string(msecs);
|
||||
ss << setw(2) << depth << setw(8) << format(value) << setw(8) << format(msecs);
|
||||
|
||||
if (pos.nodes_searched() < M)
|
||||
s << setw(8) << pos.nodes_searched() / 1 << " ";
|
||||
ss << setw(8) << pos.nodes_searched() / 1 << " ";
|
||||
|
||||
else if (pos.nodes_searched() < K * M)
|
||||
s << setw(7) << pos.nodes_searched() / K << "K ";
|
||||
ss << setw(7) << pos.nodes_searched() / K << "K ";
|
||||
|
||||
else
|
||||
s << setw(7) << pos.nodes_searched() / M << "M ";
|
||||
ss << setw(7) << pos.nodes_searched() / M << "M ";
|
||||
|
||||
padding = string(s.str().length(), ' ');
|
||||
length = padding.length();
|
||||
str = ss.str();
|
||||
padding = string(str.length(), ' ');
|
||||
|
||||
while (*m != MOVE_NONE)
|
||||
{
|
||||
san = move_to_san(pos, *m);
|
||||
san = move_to_san(pos, *m) + ' ';
|
||||
|
||||
if (length + san.length() > 80)
|
||||
{
|
||||
s << "\n" + padding;
|
||||
length = padding.length();
|
||||
}
|
||||
if ((str.length() + san.length()) % 80 <= san.length()) // Exceed 80 cols
|
||||
str += "\n" + padding;
|
||||
|
||||
s << san << ' ';
|
||||
length += san.length() + 1;
|
||||
str += san;
|
||||
|
||||
pos.do_move(*m++, *st++);
|
||||
st.push(StateInfo());
|
||||
pos.do_move(*m++, st.top());
|
||||
}
|
||||
|
||||
while (m != pv)
|
||||
pos.undo_move(*--m);
|
||||
|
||||
return s.str();
|
||||
return str;
|
||||
}
|
||||
|
||||
+3
-3
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(NOTATION_H_INCLUDED)
|
||||
#ifndef NOTATION_H_INCLUDED
|
||||
#define NOTATION_H_INCLUDED
|
||||
|
||||
#include <string>
|
||||
@@ -32,4 +32,4 @@ const std::string move_to_uci(Move m, bool chess960);
|
||||
const std::string move_to_san(Position& pos, Move m);
|
||||
std::string pretty_pv(Position& pos, int depth, Value score, int64_t msecs, Move pv[]);
|
||||
|
||||
#endif // !defined(NOTATION_H_INCLUDED)
|
||||
#endif // #ifndef NOTATION_H_INCLUDED
|
||||
|
||||
+209
-180
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,6 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
#include "bitboard.h"
|
||||
@@ -29,238 +30,264 @@ namespace {
|
||||
#define V Value
|
||||
#define S(mg, eg) make_score(mg, eg)
|
||||
|
||||
// Doubled pawn penalty by opposed flag and file
|
||||
const Score DoubledPawnPenalty[2][8] = {
|
||||
{ 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) }};
|
||||
// Doubled pawn penalty by file
|
||||
const Score Doubled[FILE_NB] = {
|
||||
S(13, 43), S(20, 48), S(23, 48), S(23, 48),
|
||||
S(23, 48), S(23, 48), S(20, 48), S(13, 43) };
|
||||
|
||||
// Isolated pawn penalty 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) }};
|
||||
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) }};
|
||||
S(33, 31), S(33, 31), S(29, 31), S(20, 28) } };
|
||||
|
||||
// Pawn chain membership bonus by file
|
||||
const Score ChainBonus[8] = {
|
||||
S(11,-1), S(13,-1), S(13,-1), S(14,-1),
|
||||
S(14,-1), S(13,-1), S(13,-1), S(11,-1)
|
||||
};
|
||||
// Connected pawn bonus by file and rank (initialized by formula)
|
||||
Score Connected[FILE_NB][RANK_NB];
|
||||
|
||||
// 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)
|
||||
};
|
||||
S(34,68), S(83,166), S(0, 0), S( 0, 0) };
|
||||
|
||||
const Score PawnStructureWeight = S(233, 201);
|
||||
// Bonus for file distance of the two outermost pawns
|
||||
const Score PawnsFileSpan = S(0, 15);
|
||||
|
||||
// Weakness of our pawn shelter in front of the king indexed by [king pawn][rank]
|
||||
const Value ShelterWeakness[2][8] =
|
||||
{ { V(141), V(0), V(38), V(102), V(128), V(141), V(141) },
|
||||
{ V( 61), V(0), V(16), V( 44), V( 56), V( 61), V( 61) } };
|
||||
// Unsupported pawn penalty
|
||||
const Score UnsupportedPawnPenalty = S(20, 10);
|
||||
|
||||
// Danger of enemy pawns moving toward our king indexed by [pawn blocked][rank]
|
||||
const Value StormDanger[2][8] =
|
||||
{ { V(26), V(0), V(128), V(51), V(26) },
|
||||
{ V(13), V(0), V( 64), V(25), V(13) } };
|
||||
// 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(160), 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.
|
||||
// in front of the king and no enemy pawn on the horizon.
|
||||
const Value MaxSafetyBonus = V(263);
|
||||
|
||||
#undef S
|
||||
#undef V
|
||||
|
||||
template<Color Us>
|
||||
Score evaluate(const Position& pos, Pawns::Entry* e) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
const Square Up = (Us == WHITE ? DELTA_N : DELTA_S);
|
||||
const Square Right = (Us == WHITE ? DELTA_NE : DELTA_SW);
|
||||
const Square Left = (Us == WHITE ? DELTA_NW : DELTA_SE);
|
||||
|
||||
Bitboard b, p, doubled;
|
||||
Square s;
|
||||
File f;
|
||||
bool passed, isolated, opposed, connected, backward, candidate, unsupported;
|
||||
Score value = SCORE_ZERO;
|
||||
const Square* pl = pos.list<PAWN>(Us);
|
||||
|
||||
Bitboard ourPawns = pos.pieces(Us, PAWN);
|
||||
Bitboard theirPawns = pos.pieces(Them, PAWN);
|
||||
|
||||
e->passedPawns[Us] = e->candidatePawns[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];
|
||||
|
||||
// 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);
|
||||
|
||||
// This file cannot be semi-open
|
||||
e->semiopenFiles[Us] &= ~(1 << f);
|
||||
|
||||
// Previous rank
|
||||
p = rank_bb(s - pawn_push(Us));
|
||||
|
||||
// Our rank plus previous one
|
||||
b = rank_bb(s) | p;
|
||||
|
||||
// Flag the pawn as passed, isolated, doubled,
|
||||
// unsupported or connected (but not the backward one).
|
||||
connected = ourPawns & adjacent_files_bb(f) & b;
|
||||
unsupported = !(ourPawns & adjacent_files_bb(f) & p);
|
||||
isolated = !(ourPawns & adjacent_files_bb(f));
|
||||
doubled = ourPawns & forward_bb(Us, s);
|
||||
opposed = theirPawns & forward_bb(Us, s);
|
||||
passed = !(theirPawns & passed_pawn_mask(Us, s));
|
||||
|
||||
// Test for backward pawn.
|
||||
// If the pawn is passed, isolated, or connected it cannot be
|
||||
// backward. If there are friendly pawns behind on adjacent files
|
||||
// or if it can capture an enemy pawn it cannot be backward either.
|
||||
if ( (passed | isolated | connected)
|
||||
|| (ourPawns & pawn_attack_span(Them, s))
|
||||
|| (pos.attacks_from<PAWN>(s, Us) & theirPawns))
|
||||
backward = false;
|
||||
else
|
||||
{
|
||||
// We now know that there are no friendly pawns beside or behind this
|
||||
// pawn on adjacent files. We now check whether the pawn is
|
||||
// backward by looking in the forward direction on the adjacent
|
||||
// files, and picking the closest pawn there.
|
||||
b = pawn_attack_span(Us, s) & (ourPawns | theirPawns);
|
||||
b = pawn_attack_span(Us, s) & rank_bb(backmost_sq(Us, b));
|
||||
|
||||
// If we have an enemy pawn in the same or next rank, the pawn is
|
||||
// backward because it cannot advance without being captured.
|
||||
backward = (b | shift_bb<Up>(b)) & theirPawns;
|
||||
}
|
||||
|
||||
assert(opposed | passed | (pawn_attack_span(Us, s) & theirPawns));
|
||||
|
||||
// A not-passed pawn is a candidate to become passed, if it is free to
|
||||
// advance and if the number of friendly pawns beside or behind this
|
||||
// pawn on adjacent files is higher than or equal to the number of
|
||||
// enemy pawns in the forward direction on the adjacent files.
|
||||
candidate = !(opposed | passed | backward | isolated)
|
||||
&& (b = pawn_attack_span(Them, s + pawn_push(Us)) & ourPawns) != 0
|
||||
&& popcount<Max15>(b) >= popcount<Max15>(pawn_attack_span(Us, s) & theirPawns);
|
||||
|
||||
// Passed pawns will be properly scored in evaluation because we need
|
||||
// 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 (unsupported && !isolated)
|
||||
value -= UnsupportedPawnPenalty;
|
||||
|
||||
if (doubled)
|
||||
value -= Doubled[f] / rank_distance(s, lsb(doubled));
|
||||
|
||||
if (backward)
|
||||
value -= Backward[opposed][f];
|
||||
|
||||
if (connected)
|
||||
value += Connected[f][relative_rank(Us, s)];
|
||||
|
||||
if (candidate)
|
||||
{
|
||||
value += CandidatePassed[relative_rank(Us, s)];
|
||||
|
||||
if (!doubled)
|
||||
e->candidatePawns[Us] |= s;
|
||||
}
|
||||
}
|
||||
|
||||
// In endgame it's better to have pawns on both wings. So give a bonus according
|
||||
// to file distance between left and right outermost pawns.
|
||||
if (pos.count<PAWN>(Us) > 1)
|
||||
{
|
||||
b = e->semiopenFiles[Us] ^ 0xFF;
|
||||
value += PawnsFileSpan * int(msb(b) - lsb(b));
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace Pawns {
|
||||
|
||||
/// init() initializes some tables by formula instead of hard-coding their values
|
||||
|
||||
void init() {
|
||||
|
||||
const int bonusesByFile[8] = { 1, 3, 3, 4, 4, 3, 3, 1 };
|
||||
int bonus;
|
||||
|
||||
for (Rank r = RANK_1; r < RANK_8; ++r)
|
||||
for (File f = FILE_A; f <= FILE_H; ++f)
|
||||
{
|
||||
bonus = r * (r-1) * (r-2) + bonusesByFile[f] * (r/2 + 1);
|
||||
Connected[f][r] = make_score(bonus, bonus);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// PawnTable::probe() takes a position object as input, computes a PawnEntry
|
||||
/// 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.
|
||||
/// 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.
|
||||
|
||||
PawnEntry* PawnTable::probe(const Position& pos) {
|
||||
Entry* probe(const Position& pos, Table& entries) {
|
||||
|
||||
Key key = pos.pawn_key();
|
||||
PawnEntry* e = entries[key];
|
||||
Entry* e = entries[key];
|
||||
|
||||
// If e->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 (e->key == key)
|
||||
return e;
|
||||
|
||||
e->key = key;
|
||||
e->passedPawns[WHITE] = e->passedPawns[BLACK] = 0;
|
||||
e->kingSquares[WHITE] = e->kingSquares[BLACK] = SQ_NONE;
|
||||
e->halfOpenFiles[WHITE] = e->halfOpenFiles[BLACK] = 0xFF;
|
||||
|
||||
Bitboard wPawns = pos.pieces(WHITE, PAWN);
|
||||
Bitboard bPawns = pos.pieces(BLACK, PAWN);
|
||||
e->pawnAttacks[WHITE] = ((wPawns & ~FileHBB) << 9) | ((wPawns & ~FileABB) << 7);
|
||||
e->pawnAttacks[BLACK] = ((bPawns & ~FileHBB) >> 7) | ((bPawns & ~FileABB) >> 9);
|
||||
|
||||
e->value = evaluate_pawns<WHITE>(pos, wPawns, bPawns, e)
|
||||
- evaluate_pawns<BLACK>(pos, bPawns, wPawns, e);
|
||||
|
||||
e->value = apply_weight(e->value, PawnStructureWeight);
|
||||
|
||||
e->value = evaluate<WHITE>(pos, e) - evaluate<BLACK>(pos, e);
|
||||
return e;
|
||||
}
|
||||
|
||||
|
||||
/// PawnTable::evaluate_pawns() evaluates each pawn of the given color
|
||||
|
||||
template<Color Us>
|
||||
Score PawnTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
|
||||
Bitboard theirPawns, PawnEntry* e) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
|
||||
Bitboard b;
|
||||
Square s;
|
||||
File f;
|
||||
Rank r;
|
||||
bool passed, isolated, doubled, opposed, chain, backward, candidate;
|
||||
Score value = SCORE_ZERO;
|
||||
const Square* pl = pos.piece_list(Us, PAWN);
|
||||
|
||||
// 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 half open
|
||||
e->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));
|
||||
|
||||
// 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 & attack_span_mask(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)))
|
||||
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(opposed | passed | (attack_span_mask(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 = attack_span_mask(Them, s + pawn_push(Us)) & ourPawns) != 0
|
||||
&& popcount<Max15>(b) >= popcount<Max15>(attack_span_mask(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 -= 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)];
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/// PawnEntry::shelter_storm() calculates shelter and storm penalties for the file
|
||||
/// 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>
|
||||
Value PawnEntry::shelter_storm(const Position& pos, Square ksq) {
|
||||
Value Entry::shelter_storm(const Position& pos, Square ksq) {
|
||||
|
||||
const Color Them = (Us == WHITE ? BLACK : WHITE);
|
||||
static const Bitboard MiddleEdges = (FileABB | FileHBB) & (Rank2BB | Rank3BB);
|
||||
|
||||
Value safety = MaxSafetyBonus;
|
||||
Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, ksq) | rank_bb(ksq));
|
||||
Bitboard ourPawns = b & pos.pieces(Us) & ~rank_bb(ksq);
|
||||
Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq));
|
||||
Bitboard ourPawns = b & pos.pieces(Us);
|
||||
Bitboard theirPawns = b & pos.pieces(Them);
|
||||
Rank rkUs, rkThem;
|
||||
File kf = file_of(ksq);
|
||||
File kf = std::max(FILE_B, std::min(FILE_G, file_of(ksq)));
|
||||
|
||||
kf = (kf == FILE_A) ? kf++ : (kf == FILE_H) ? kf-- : kf;
|
||||
|
||||
for (int f = kf - 1; f <= kf + 1; f++)
|
||||
for (File f = kf - File(1); f <= kf + File(1); ++f)
|
||||
{
|
||||
// Shelter penalty is higher for the pawn in front of the king
|
||||
b = ourPawns & FileBB[f];
|
||||
rkUs = b ? rank_of(Us == WHITE ? lsb(b) : ~msb(b)) : RANK_1;
|
||||
safety -= ShelterWeakness[f != kf][rkUs];
|
||||
b = ourPawns & file_bb(f);
|
||||
rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1;
|
||||
|
||||
// Storm danger is smaller if enemy pawn is blocked
|
||||
b = theirPawns & FileBB[f];
|
||||
rkThem = b ? rank_of(Us == WHITE ? lsb(b) : ~msb(b)) : RANK_1;
|
||||
safety -= StormDanger[rkThem == rkUs + 1][rkThem];
|
||||
b = theirPawns & file_bb(f);
|
||||
rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1;
|
||||
|
||||
if ( (MiddleEdges & make_square(f, rkThem))
|
||||
&& file_of(ksq) == f
|
||||
&& relative_rank(Us, ksq) == rkThem - 1)
|
||||
safety += 200;
|
||||
else
|
||||
safety -= ShelterWeakness[rkUs]
|
||||
+ StormDanger[rkUs == RANK_1 ? 0 : rkThem == rkUs + 1 ? 2 : 1][rkThem];
|
||||
}
|
||||
|
||||
return safety;
|
||||
}
|
||||
|
||||
|
||||
/// PawnEntry::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.
|
||||
/// Entry::do_king_safety() calculates a bonus for king safety. It is called only
|
||||
/// when king square changes, which is about 20% of total king_safety() calls.
|
||||
|
||||
template<Color Us>
|
||||
Score PawnEntry::update_safety(const Position& pos, Square ksq) {
|
||||
Score Entry::do_king_safety(const Position& pos, Square ksq) {
|
||||
|
||||
kingSquares[Us] = ksq;
|
||||
castleRights[Us] = pos.can_castle(Us);
|
||||
castlingRights[Us] = pos.can_castle(Us);
|
||||
minKPdistance[Us] = 0;
|
||||
|
||||
Bitboard pawns = pos.pieces(Us, PAWN);
|
||||
@@ -268,20 +295,22 @@ Score PawnEntry::update_safety(const Position& pos, Square ksq) {
|
||||
while (!(DistanceRingsBB[ksq][minKPdistance[Us]++] & pawns)) {}
|
||||
|
||||
if (relative_rank(Us, ksq) > RANK_4)
|
||||
return kingSafety[Us] = make_score(0, -16 * minKPdistance[Us]);
|
||||
return 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)))
|
||||
// If we can castle use the bonus after the castling if it is bigger
|
||||
if (pos.can_castle(MakeCastling<Us, KING_SIDE>::right))
|
||||
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_G1)));
|
||||
|
||||
if (pos.can_castle(make_castle_right(Us, QUEEN_SIDE)))
|
||||
if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::right))
|
||||
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));
|
||||
|
||||
return kingSafety[Us] = make_score(bonus, -16 * minKPdistance[Us]);
|
||||
return make_score(bonus, -16 * minKPdistance[Us]);
|
||||
}
|
||||
|
||||
// Explicit template instantiation
|
||||
template Score PawnEntry::update_safety<WHITE>(const Position& pos, Square ksq);
|
||||
template Score PawnEntry::update_safety<BLACK>(const Position& pos, Square ksq);
|
||||
template Score Entry::do_king_safety<WHITE>(const Position& pos, Square ksq);
|
||||
template Score Entry::do_king_safety<BLACK>(const Position& pos, Square ksq);
|
||||
|
||||
} // namespace Pawns
|
||||
|
||||
+41
-72
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,99 +17,68 @@
|
||||
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
|
||||
|
||||
#include "misc.h"
|
||||
#include "position.h"
|
||||
#include "types.h"
|
||||
|
||||
const int PawnTableSize = 16384;
|
||||
namespace Pawns {
|
||||
|
||||
/// PawnEntry 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 probe method in a PawnTable object)
|
||||
/// returns a pointer to a PawnEntry object.
|
||||
/// Pawns::Entry contains various information about a pawn structure. A lookup
|
||||
/// to the pawn hash table (performed by calling the probe function) returns a
|
||||
/// pointer to an Entry object.
|
||||
|
||||
class PawnEntry {
|
||||
struct Entry {
|
||||
|
||||
friend struct PawnTable;
|
||||
Score pawns_value() const { return value; }
|
||||
Bitboard pawn_attacks(Color c) const { return pawnAttacks[c]; }
|
||||
Bitboard passed_pawns(Color c) const { return passedPawns[c]; }
|
||||
Bitboard candidate_pawns(Color c) const { return candidatePawns[c]; }
|
||||
|
||||
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;
|
||||
int semiopen_file(Color c, File f) const {
|
||||
return semiopenFiles[c] & (1 << f);
|
||||
}
|
||||
|
||||
int semiopen_side(Color c, File f, bool leftSide) const {
|
||||
return semiopenFiles[c] & (leftSide ? (1 << f) - 1 : ~((1 << (f + 1)) - 1));
|
||||
}
|
||||
|
||||
int pawns_on_same_color_squares(Color c, Square s) const {
|
||||
return pawnsOnSquares[c][!!(DarkSquares & s)];
|
||||
}
|
||||
|
||||
template<Color Us>
|
||||
Score king_safety(const Position& pos, Square ksq);
|
||||
Score king_safety(const Position& pos, Square ksq) {
|
||||
return kingSquares[Us] == ksq && castlingRights[Us] == pos.can_castle(Us)
|
||||
? kingSafety[Us] : (kingSafety[Us] = do_king_safety<Us>(pos, ksq));
|
||||
}
|
||||
|
||||
private:
|
||||
template<Color Us>
|
||||
Score update_safety(const Position& pos, Square ksq);
|
||||
Score do_king_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];
|
||||
int minKPdistance[2];
|
||||
int castleRights[2];
|
||||
Score value;
|
||||
int halfOpenFiles[2];
|
||||
Score kingSafety[2];
|
||||
Bitboard passedPawns[COLOR_NB];
|
||||
Bitboard candidatePawns[COLOR_NB];
|
||||
Bitboard pawnAttacks[COLOR_NB];
|
||||
Square kingSquares[COLOR_NB];
|
||||
Score kingSafety[COLOR_NB];
|
||||
int minKPdistance[COLOR_NB];
|
||||
int castlingRights[COLOR_NB];
|
||||
int semiopenFiles[COLOR_NB];
|
||||
int pawnsOnSquares[COLOR_NB][COLOR_NB]; // [color][light/dark squares]
|
||||
};
|
||||
|
||||
typedef HashTable<Entry, 16384> Table;
|
||||
|
||||
/// The PawnTable class represents a pawn hash table. The most important
|
||||
/// method is probe, which returns a pointer to a PawnEntry object.
|
||||
void init();
|
||||
Entry* probe(const Position& pos, Table& entries);
|
||||
|
||||
struct PawnTable {
|
||||
} // namespace Pawns
|
||||
|
||||
PawnEntry* probe(const Position& pos);
|
||||
|
||||
template<Color Us>
|
||||
static Score evaluate_pawns(const Position& pos, Bitboard ourPawns,
|
||||
Bitboard theirPawns, PawnEntry* e);
|
||||
|
||||
HashTable<PawnEntry, PawnTableSize> entries;
|
||||
};
|
||||
|
||||
|
||||
inline Score PawnEntry::pawns_value() const {
|
||||
return value;
|
||||
}
|
||||
|
||||
inline Bitboard PawnEntry::pawn_attacks(Color c) const {
|
||||
return pawnAttacks[c];
|
||||
}
|
||||
|
||||
inline Bitboard PawnEntry::passed_pawns(Color c) const {
|
||||
return passedPawns[c];
|
||||
}
|
||||
|
||||
inline int PawnEntry::file_is_half_open(Color c, File f) const {
|
||||
return halfOpenFiles[c] & (1 << int(f));
|
||||
}
|
||||
|
||||
inline int PawnEntry::has_open_file_to_left(Color c, File f) const {
|
||||
return halfOpenFiles[c] & ((1 << int(f)) - 1);
|
||||
}
|
||||
|
||||
inline int PawnEntry::has_open_file_to_right(Color c, File f) const {
|
||||
return halfOpenFiles[c] & ~((1 << int(f+1)) - 1);
|
||||
}
|
||||
|
||||
template<Color Us>
|
||||
inline Score PawnEntry::king_safety(const Position& pos, Square ksq) {
|
||||
return kingSquares[Us] == ksq && castleRights[Us] == pos.can_castle(Us)
|
||||
? kingSafety[Us] : update_safety<Us>(pos, ksq);
|
||||
}
|
||||
|
||||
#endif // !defined(PAWNS_H_INCLUDED)
|
||||
#endif // #ifndef PAWNS_H_INCLUDED
|
||||
|
||||
+24
-17
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,12 +17,12 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#if !defined(PLATFORM_H_INCLUDED)
|
||||
#ifndef PLATFORM_H_INCLUDED
|
||||
#define PLATFORM_H_INCLUDED
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#ifdef _MSC_VER
|
||||
|
||||
// Disable some silly and noisy warning from MSVC compiler
|
||||
// Disable some silly and noisy warnings from MSVC compiler
|
||||
#pragma warning(disable: 4127) // Conditional expression is constant
|
||||
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
|
||||
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
|
||||
@@ -42,13 +42,15 @@ typedef unsigned __int64 uint64_t;
|
||||
# include <inttypes.h>
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32) && !defined(_WIN64) // Linux - Unix
|
||||
#ifndef _WIN32 // Linux - Unix
|
||||
|
||||
# include <sys/time.h>
|
||||
typedef timeval sys_time_t;
|
||||
|
||||
inline void system_time(sys_time_t* t) { gettimeofday(t, NULL); }
|
||||
inline int64_t time_to_msec(const sys_time_t& t) { return t.tv_sec * 1000LL + t.tv_usec / 1000; }
|
||||
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;
|
||||
@@ -65,18 +67,20 @@ typedef void*(*pt_start_fn)(void*);
|
||||
# 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_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>
|
||||
typedef _timeb sys_time_t;
|
||||
|
||||
inline void system_time(sys_time_t* t) { _ftime(t); }
|
||||
inline int64_t time_to_msec(const sys_time_t& t) { return t.time * 1000LL + t.millitm; }
|
||||
inline int64_t system_time_to_msec() {
|
||||
_timeb t;
|
||||
_ftime(&t);
|
||||
return t.time * 1000LL + t.millitm;
|
||||
}
|
||||
|
||||
#if !defined(NOMINMAX)
|
||||
#ifndef NOMINMAX
|
||||
# define NOMINMAX // disable macros min() and max()
|
||||
#endif
|
||||
|
||||
@@ -85,13 +89,16 @@ inline int64_t time_to_msec(const sys_time_t& t) { return t.time * 1000LL + t.mi
|
||||
#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()
|
||||
// We use critical sections on Windows to support Windows XP and older versions.
|
||||
// Unfortunately, cond_wait() is racy between lock_release() and WaitForSingleObject()
|
||||
// but apart from this they have the same speed performance of SRW locks.
|
||||
typedef CRITICAL_SECTION Lock;
|
||||
typedef HANDLE WaitCondition;
|
||||
typedef HANDLE NativeHandle;
|
||||
|
||||
// On Windows 95 and 98 parameter lpThreadId may not be null
|
||||
inline DWORD* dwWin9xKludge() { static DWORD dw; return &dw; }
|
||||
|
||||
# define lock_init(x) InitializeCriticalSection(&(x))
|
||||
# define lock_grab(x) EnterCriticalSection(&(x))
|
||||
# define lock_release(x) LeaveCriticalSection(&(x))
|
||||
@@ -101,9 +108,9 @@ typedef HANDLE NativeHandle;
|
||||
# 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,NULL), x != NULL)
|
||||
# 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 // !defined(PLATFORM_H_INCLUDED)
|
||||
#endif // #ifndef PLATFORM_H_INCLUDED
|
||||
|
||||
+553
-974
File diff suppressed because it is too large
Load Diff
+148
-148
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,10 +17,11 @@
|
||||
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
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "types.h"
|
||||
@@ -29,7 +30,7 @@
|
||||
/// The checkInfo struct is initialized at c'tor time and keeps info used
|
||||
/// to detect if a move gives check.
|
||||
class Position;
|
||||
class Thread;
|
||||
struct Thread;
|
||||
|
||||
struct CheckInfo {
|
||||
|
||||
@@ -37,21 +38,21 @@ struct CheckInfo {
|
||||
|
||||
Bitboard dcCandidates;
|
||||
Bitboard pinned;
|
||||
Bitboard checkSq[8];
|
||||
Bitboard checkSq[PIECE_TYPE_NB];
|
||||
Square ksq;
|
||||
};
|
||||
|
||||
|
||||
/// The StateInfo struct stores information we need to restore a Position
|
||||
/// The StateInfo struct stores information needed to restore a Position
|
||||
/// object to its previous state when we retract a move. Whenever a move
|
||||
/// is made on the board (by calling Position::do_move), an StateInfo object
|
||||
/// must be passed as a parameter.
|
||||
/// is made on the board (by calling Position::do_move), a StateInfo
|
||||
/// object must be passed as a parameter.
|
||||
|
||||
struct StateInfo {
|
||||
Key pawnKey, materialKey;
|
||||
Value npMaterial[2];
|
||||
int castleRights, rule50, pliesFromNull;
|
||||
Score psqScore;
|
||||
Value npMaterial[COLOR_NB];
|
||||
int castlingRights, rule50, pliesFromNull;
|
||||
Score psq;
|
||||
Square epSquare;
|
||||
|
||||
Key key;
|
||||
@@ -60,48 +61,29 @@ struct StateInfo {
|
||||
StateInfo* previous;
|
||||
};
|
||||
|
||||
struct ReducedStateInfo {
|
||||
Key pawnKey, materialKey;
|
||||
Value npMaterial[2];
|
||||
int castleRights, rule50, pliesFromNull;
|
||||
Score psqScore;
|
||||
Square epSquare;
|
||||
};
|
||||
|
||||
/// 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
|
||||
/// by pieces of that type.
|
||||
/// * For each color, a bitboard representing the squares occupied by
|
||||
/// pieces of that color.
|
||||
/// * A bitboard of all occupied squares.
|
||||
/// * A bitboard of all checking pieces.
|
||||
/// * A 64-entry array of pieces, indexed by the squares of the board.
|
||||
/// * The current side to move.
|
||||
/// * Information about the castling rights for both sides.
|
||||
/// * The initial files of the kings and both pairs of rooks. This is
|
||||
/// used to implement the Chess960 castling rules.
|
||||
/// * The en passant square (which is SQ_NONE if no en passant capture is
|
||||
/// possible).
|
||||
/// * The squares of the kings for both sides.
|
||||
/// * Hash keys for the position itself, the current pawn structure, and
|
||||
/// the current material situation.
|
||||
/// * Hash keys for all previous positions in the game for detecting
|
||||
/// repetition draws.
|
||||
/// * A counter for detecting 50 move rule draws.
|
||||
/// The Position class stores the information regarding the board representation
|
||||
/// like pieces, side to move, hash keys, castling info, etc. The most important
|
||||
/// methods are do_move() and undo_move(), used by the search to update node info
|
||||
/// when traversing the search tree.
|
||||
|
||||
class Position {
|
||||
public:
|
||||
Position() {}
|
||||
Position(const Position& p, Thread* t) { *this = p; thisThread = t; }
|
||||
Position(const std::string& f, bool c960, Thread* t) { from_fen(f, c960, t); }
|
||||
Position(const Position& pos, Thread* t) { *this = pos; thisThread = t; }
|
||||
Position(const std::string& f, bool c960, Thread* t) { set(f, c960, t); }
|
||||
Position& operator=(const Position&);
|
||||
static void init();
|
||||
|
||||
// Text input/output
|
||||
void from_fen(const std::string& fen, bool isChess960, Thread* th);
|
||||
const std::string to_fen() const;
|
||||
void print(Move m = MOVE_NONE) const;
|
||||
void set(const std::string& fenStr, bool isChess960, Thread* th);
|
||||
const std::string fen() const;
|
||||
const std::string pretty(Move m = MOVE_NONE) const;
|
||||
|
||||
// Position representation
|
||||
Bitboard pieces() const;
|
||||
@@ -113,57 +95,54 @@ public:
|
||||
Piece piece_on(Square s) const;
|
||||
Square king_square(Color c) const;
|
||||
Square ep_square() const;
|
||||
bool is_empty(Square s) const;
|
||||
const Square* piece_list(Color c, PieceType pt) const;
|
||||
int piece_count(Color c, PieceType pt) const;
|
||||
bool empty(Square s) const;
|
||||
template<PieceType Pt> int count(Color c) const;
|
||||
template<PieceType Pt> const Square* list(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;
|
||||
int can_castle(CastlingRight cr) const;
|
||||
bool castling_impeded(CastlingRight cr) const;
|
||||
Square castling_rook_square(CastlingRight cr) const;
|
||||
|
||||
// Checking
|
||||
bool in_check() const;
|
||||
Bitboard checkers() const;
|
||||
Bitboard discovered_check_candidates() const;
|
||||
Bitboard pinned_pieces() const;
|
||||
Bitboard pinned_pieces(Color c) const;
|
||||
|
||||
// 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);
|
||||
Bitboard attacks_from(Piece pc, Square s) const;
|
||||
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 move_attacks_square(Move m, Square s) const;
|
||||
bool move_is_legal(const Move m) const;
|
||||
bool pl_move_is_legal(Move m, Bitboard pinned) const;
|
||||
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;
|
||||
bool legal(Move m, Bitboard pinned) const;
|
||||
bool pseudo_legal(const Move m) const;
|
||||
bool capture(Move m) const;
|
||||
bool capture_or_promotion(Move m) const;
|
||||
bool gives_check(Move m, const CheckInfo& ci) const;
|
||||
bool advanced_pawn_push(Move m) const;
|
||||
Piece moved_piece(Move m) const;
|
||||
PieceType captured_piece_type() const;
|
||||
|
||||
// Piece specific
|
||||
bool pawn_is_passed(Color c, Square s) const;
|
||||
bool pawn_passed(Color c, Square s) const;
|
||||
bool pawn_on_7th(Color c) const;
|
||||
bool opposite_bishops() const;
|
||||
bool bishop_pair(Color c) const;
|
||||
bool opposite_bishops() const;
|
||||
|
||||
// Doing and undoing moves
|
||||
void do_move(Move m, StateInfo& st);
|
||||
void do_move(Move m, StateInfo& st, const CheckInfo& ci, bool moveIsCheck);
|
||||
void undo_move(Move m);
|
||||
template<bool Do> void do_null_move(StateInfo& st);
|
||||
void do_null_move(StateInfo& st);
|
||||
void undo_null_move();
|
||||
|
||||
// Static exchange evaluation
|
||||
int see(Move m) const;
|
||||
int see_sign(Move m) const;
|
||||
Value see(Move m) const;
|
||||
Value see_sign(Move m) const;
|
||||
|
||||
// Accessing hash keys
|
||||
Key key() const;
|
||||
@@ -173,67 +152,61 @@ public:
|
||||
|
||||
// Incremental piece-square evaluation
|
||||
Score psq_score() const;
|
||||
Score psq_delta(Piece p, Square from, Square to) const;
|
||||
Value non_pawn_material(Color c) const;
|
||||
|
||||
// Other properties of the position
|
||||
Color side_to_move() const;
|
||||
int startpos_ply_counter() const;
|
||||
int game_ply() const;
|
||||
bool is_chess960() const;
|
||||
Thread* this_thread() const;
|
||||
int64_t nodes_searched() const;
|
||||
void set_nodes_searched(int64_t n);
|
||||
template<bool SkipRepetition> bool is_draw() const;
|
||||
uint64_t nodes_searched() const;
|
||||
void set_nodes_searched(uint64_t n);
|
||||
bool is_draw() const;
|
||||
|
||||
// Position consistency check, for debugging
|
||||
bool pos_is_ok(int* failedStep = NULL) const;
|
||||
bool pos_is_ok(int* step = NULL) const;
|
||||
void flip();
|
||||
|
||||
private:
|
||||
// Initialization helpers (used while setting up a position)
|
||||
void clear();
|
||||
void put_piece(Piece p, Square s);
|
||||
void set_castle_right(Color c, Square rfrom);
|
||||
void set_castling_right(Color c, Square rfrom);
|
||||
void set_state(StateInfo* si) const;
|
||||
|
||||
// Helper template functions
|
||||
template<bool Do> void do_castle_move(Move m);
|
||||
template<bool FindPinned> Bitboard hidden_checkers() const;
|
||||
|
||||
// Computing hash keys from scratch (for initialization and debugging)
|
||||
Key compute_key() const;
|
||||
Key compute_pawn_key() const;
|
||||
Key compute_material_key() const;
|
||||
|
||||
// Computing incremental evaluation scores and material counts
|
||||
Score compute_psq_score() const;
|
||||
Value compute_non_pawn_material(Color c) const;
|
||||
// Helper functions
|
||||
Bitboard check_blockers(Color c, Color kingColor) const;
|
||||
void put_piece(Square s, Color c, PieceType pt);
|
||||
void remove_piece(Square s, Color c, PieceType pt);
|
||||
void move_piece(Square from, Square to, Color c, PieceType pt);
|
||||
template<bool Do>
|
||||
void do_castling(Square from, Square& to, Square& rfrom, Square& rto);
|
||||
|
||||
// Board and pieces
|
||||
Piece board[64]; // [square]
|
||||
Bitboard byTypeBB[8]; // [pieceType]
|
||||
Bitboard byColorBB[2]; // [color]
|
||||
int pieceCount[2][8]; // [color][pieceType]
|
||||
Square pieceList[2][8][16]; // [color][pieceType][index]
|
||||
int index[64]; // [square]
|
||||
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
|
||||
int castleRightsMask[64]; // [square]
|
||||
Square castleRookSquare[2][2]; // [color][side]
|
||||
Bitboard castlePath[2][2]; // [color][side]
|
||||
int castlingRightsMask[SQUARE_NB];
|
||||
Square castlingRookSquare[CASTLING_RIGHT_NB];
|
||||
Bitboard castlingPath[CASTLING_RIGHT_NB];
|
||||
StateInfo startState;
|
||||
int64_t nodes;
|
||||
int startPosPly;
|
||||
uint64_t nodes;
|
||||
int gamePly;
|
||||
Color sideToMove;
|
||||
Thread* thisThread;
|
||||
StateInfo* st;
|
||||
int chess960;
|
||||
bool chess960;
|
||||
};
|
||||
|
||||
inline int64_t Position::nodes_searched() const {
|
||||
inline uint64_t Position::nodes_searched() const {
|
||||
return nodes;
|
||||
}
|
||||
|
||||
inline void Position::set_nodes_searched(int64_t n) {
|
||||
inline void Position::set_nodes_searched(uint64_t n) {
|
||||
nodes = n;
|
||||
}
|
||||
|
||||
@@ -241,11 +214,11 @@ inline Piece Position::piece_on(Square s) const {
|
||||
return board[s];
|
||||
}
|
||||
|
||||
inline Piece Position::piece_moved(Move m) const {
|
||||
inline Piece Position::moved_piece(Move m) const {
|
||||
return board[from_sq(m)];
|
||||
}
|
||||
|
||||
inline bool Position::is_empty(Square s) const {
|
||||
inline bool Position::empty(Square s) const {
|
||||
return board[s] == NO_PIECE;
|
||||
}
|
||||
|
||||
@@ -277,12 +250,12 @@ inline Bitboard Position::pieces(Color c, PieceType pt1, PieceType pt2) const {
|
||||
return byColorBB[c] & (byTypeBB[pt1] | byTypeBB[pt2]);
|
||||
}
|
||||
|
||||
inline int Position::piece_count(Color c, PieceType pt) const {
|
||||
return pieceCount[c][pt];
|
||||
template<PieceType Pt> inline int Position::count(Color c) const {
|
||||
return pieceCount[c][Pt];
|
||||
}
|
||||
|
||||
inline const Square* Position::piece_list(Color c, PieceType pt) const {
|
||||
return pieceList[c][pt];
|
||||
template<PieceType Pt> inline const Square* Position::list(Color c) const {
|
||||
return pieceList[c][Pt];
|
||||
}
|
||||
|
||||
inline Square Position::ep_square() const {
|
||||
@@ -293,26 +266,26 @@ inline Square Position::king_square(Color c) const {
|
||||
return pieceList[c][KING][0];
|
||||
}
|
||||
|
||||
inline int Position::can_castle(CastleRight f) const {
|
||||
return st->castleRights & f;
|
||||
inline int Position::can_castle(CastlingRight cr) const {
|
||||
return st->castlingRights & cr;
|
||||
}
|
||||
|
||||
inline int Position::can_castle(Color c) const {
|
||||
return st->castleRights & ((WHITE_OO | WHITE_OOO) << (2 * c));
|
||||
return st->castlingRights & ((WHITE_OO | WHITE_OOO) << (2 * c));
|
||||
}
|
||||
|
||||
inline bool Position::castle_impeded(Color c, CastlingSide s) const {
|
||||
return byTypeBB[ALL_PIECES] & castlePath[c][s];
|
||||
inline bool Position::castling_impeded(CastlingRight cr) const {
|
||||
return byTypeBB[ALL_PIECES] & castlingPath[cr];
|
||||
}
|
||||
|
||||
inline Square Position::castle_rook_square(Color c, CastlingSide s) const {
|
||||
return castleRookSquare[c][s];
|
||||
inline Square Position::castling_rook_square(CastlingRight cr) const {
|
||||
return castlingRookSquare[cr];
|
||||
}
|
||||
|
||||
template<PieceType Pt>
|
||||
inline Bitboard Position::attacks_from(Square s) const {
|
||||
|
||||
return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, pieces())
|
||||
return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, byTypeBB[ALL_PIECES])
|
||||
: Pt == QUEEN ? attacks_from<ROOK>(s) | attacks_from<BISHOP>(s)
|
||||
: StepAttacksBB[Pt][s];
|
||||
}
|
||||
@@ -322,8 +295,8 @@ inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
|
||||
return StepAttacksBB[make_piece(c, PAWN)][s];
|
||||
}
|
||||
|
||||
inline Bitboard Position::attacks_from(Piece p, Square s) const {
|
||||
return attacks_from(p, s, byTypeBB[ALL_PIECES]);
|
||||
inline Bitboard Position::attacks_from(Piece pc, Square s) const {
|
||||
return attacks_bb(pc, s, byTypeBB[ALL_PIECES]);
|
||||
}
|
||||
|
||||
inline Bitboard Position::attackers_to(Square s) const {
|
||||
@@ -334,30 +307,27 @@ inline Bitboard Position::checkers() const {
|
||||
return st->checkersBB;
|
||||
}
|
||||
|
||||
inline bool Position::in_check() const {
|
||||
return st->checkersBB != 0;
|
||||
}
|
||||
|
||||
inline Bitboard Position::discovered_check_candidates() const {
|
||||
return hidden_checkers<false>();
|
||||
return check_blockers(sideToMove, ~sideToMove);
|
||||
}
|
||||
|
||||
inline Bitboard Position::pinned_pieces() const {
|
||||
return hidden_checkers<true>();
|
||||
inline Bitboard Position::pinned_pieces(Color c) const {
|
||||
return check_blockers(c, c);
|
||||
}
|
||||
|
||||
inline bool Position::pawn_is_passed(Color c, Square s) const {
|
||||
inline bool Position::pawn_passed(Color c, Square s) const {
|
||||
return !(pieces(~c, PAWN) & passed_pawn_mask(c, s));
|
||||
}
|
||||
|
||||
inline bool Position::advanced_pawn_push(Move m) const {
|
||||
return type_of(moved_piece(m)) == PAWN
|
||||
&& relative_rank(sideToMove, from_sq(m)) > RANK_4;
|
||||
}
|
||||
|
||||
inline Key Position::key() const {
|
||||
return st->key;
|
||||
}
|
||||
|
||||
inline Key Position::exclusion_key() const {
|
||||
return st->key ^ Zobrist::exclusion;
|
||||
}
|
||||
|
||||
inline Key Position::pawn_key() const {
|
||||
return st->pawnKey;
|
||||
}
|
||||
@@ -366,26 +336,16 @@ inline Key Position::material_key() const {
|
||||
return st->materialKey;
|
||||
}
|
||||
|
||||
inline Score Position::psq_delta(Piece p, Square from, Square to) const {
|
||||
return pieceSquareTable[p][to] - pieceSquareTable[p][from];
|
||||
}
|
||||
|
||||
inline Score Position::psq_score() const {
|
||||
return st->psqScore;
|
||||
return st->psq;
|
||||
}
|
||||
|
||||
inline Value Position::non_pawn_material(Color c) const {
|
||||
return st->npMaterial[c];
|
||||
}
|
||||
|
||||
inline bool Position::is_passed_pawn_push(Move m) const {
|
||||
|
||||
return type_of(piece_moved(m)) == PAWN
|
||||
&& pawn_is_passed(sideToMove, to_sq(m));
|
||||
}
|
||||
|
||||
inline int Position::startpos_ply_counter() const {
|
||||
return startPosPly + st->pliesFromNull; // HACK
|
||||
inline int Position::game_ply() const {
|
||||
return gamePly;
|
||||
}
|
||||
|
||||
inline bool Position::opposite_bishops() const {
|
||||
@@ -409,17 +369,17 @@ inline bool Position::is_chess960() const {
|
||||
return chess960;
|
||||
}
|
||||
|
||||
inline bool Position::is_capture_or_promotion(Move m) const {
|
||||
inline bool Position::capture_or_promotion(Move m) const {
|
||||
|
||||
assert(is_ok(m));
|
||||
return type_of(m) ? type_of(m) != CASTLE : !is_empty(to_sq(m));
|
||||
return type_of(m) != NORMAL ? type_of(m) != CASTLING : !empty(to_sq(m));
|
||||
}
|
||||
|
||||
inline bool Position::is_capture(Move m) const {
|
||||
inline bool Position::capture(Move m) const {
|
||||
|
||||
// Note that castle is coded as "king captures the rook"
|
||||
// Note that castling is encoded as "king captures the rook"
|
||||
assert(is_ok(m));
|
||||
return (!is_empty(to_sq(m)) && type_of(m) != CASTLE) || type_of(m) == ENPASSANT;
|
||||
return (!empty(to_sq(m)) && type_of(m) != CASTLING) || type_of(m) == ENPASSANT;
|
||||
}
|
||||
|
||||
inline PieceType Position::captured_piece_type() const {
|
||||
@@ -430,4 +390,44 @@ inline Thread* Position::this_thread() const {
|
||||
return thisThread;
|
||||
}
|
||||
|
||||
#endif // !defined(POSITION_H_INCLUDED)
|
||||
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::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;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
+52
-52
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +17,7 @@
|
||||
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
|
||||
|
||||
#include "types.h"
|
||||
@@ -26,73 +26,73 @@
|
||||
|
||||
|
||||
/// 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.
|
||||
/// a given square a (middlegame, endgame) score pair is assigned. PSQT is defined
|
||||
/// for the white side and the tables are symmetric for the black side.
|
||||
|
||||
static const Score PSQT[][64] = {
|
||||
static const Score PSQT[][SQUARE_NB] = {
|
||||
{ },
|
||||
{ // Pawn
|
||||
S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S( 0, 0),
|
||||
S(-28,-8), S(-6,-8), S( 4,-8), S(14,-8), S(14,-8), S( 4,-8), S(-6,-8), S(-28,-8),
|
||||
S(-28,-8), S(-6,-8), S( 9,-8), S(36,-8), S(36,-8), S( 9,-8), S(-6,-8), S(-28,-8),
|
||||
S(-28,-8), S(-6,-8), S(17,-8), S(58,-8), S(58,-8), S(17,-8), S(-6,-8), S(-28,-8),
|
||||
S(-28,-8), S(-6,-8), S(17,-8), S(36,-8), S(36,-8), S(17,-8), S(-6,-8), S(-28,-8),
|
||||
S(-28,-8), S(-6,-8), S( 9,-8), S(14,-8), S(14,-8), S( 9,-8), S(-6,-8), S(-28,-8),
|
||||
S(-28,-8), S(-6,-8), S( 4,-8), S(14,-8), S(14,-8), S( 4,-8), S(-6,-8), S(-28,-8),
|
||||
S(-20, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S(-20, 0),
|
||||
S(-20, 0), S( 0, 0), S(10, 0), S(20, 0), S(20, 0), S(10, 0), S( 0, 0), S(-20, 0),
|
||||
S(-20, 0), S( 0, 0), S(20, 0), S(40, 0), S(40, 0), S(20, 0), S( 0, 0), S(-20, 0),
|
||||
S(-20, 0), S( 0, 0), S(10, 0), S(20, 0), S(20, 0), S(10, 0), S( 0, 0), S(-20, 0),
|
||||
S(-20, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S(-20, 0),
|
||||
S(-20, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S(-20, 0),
|
||||
S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0), S(0, 0), S( 0, 0), S( 0, 0), S( 0, 0)
|
||||
},
|
||||
{ // Knight
|
||||
S(-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)
|
||||
S(-144,-98), S(-109,-83), S(-85,-51), S(-73,-16), S(-73,-16), S(-85,-51), S(-109,-83), S(-144,-98),
|
||||
S( -88,-68), S( -43,-53), S(-19,-21), S( -7, 14), S( -7, 14), S(-19,-21), S( -43,-53), S( -88,-68),
|
||||
S( -69,-53), S( -24,-38), S( 0, -6), S( 12, 29), S( 12, 29), S( 0, -6), S( -24,-38), S( -69,-53),
|
||||
S( -28,-42), S( 17,-27), S( 41, 5), S( 53, 40), S( 53, 40), S( 41, 5), S( 17,-27), S( -28,-42),
|
||||
S( -30,-42), S( 15,-27), S( 39, 5), S( 51, 40), S( 51, 40), S( 39, 5), S( 15,-27), S( -30,-42),
|
||||
S( -10,-53), S( 35,-38), S( 59, -6), S( 71, 29), S( 71, 29), S( 59, -6), S( 35,-38), S( -10,-53),
|
||||
S( -64,-68), S( -19,-53), S( 5,-21), S( 17, 14), S( 17, 14), S( 5,-21), S( -19,-53), S( -64,-68),
|
||||
S(-200,-98), S( -65,-83), S(-41,-51), S(-29,-16), S(-29,-16), S(-41,-51), S( -65,-83), S(-200,-98)
|
||||
},
|
||||
{ // Bishop
|
||||
S(-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)
|
||||
S(-54,-65), S(-27,-42), S(-34,-44), S(-43,-26), S(-43,-26), S(-34,-44), S(-27,-42), S(-54,-65),
|
||||
S(-29,-43), S( 8,-20), S( 1,-22), S( -8, -4), S( -8, -4), S( 1,-22), S( 8,-20), S(-29,-43),
|
||||
S(-20,-33), S( 17,-10), S( 10,-12), S( 1, 6), S( 1, 6), S( 10,-12), S( 17,-10), S(-20,-33),
|
||||
S(-19,-35), S( 18,-12), S( 11,-14), S( 2, 4), S( 2, 4), S( 11,-14), S( 18,-12), S(-19,-35),
|
||||
S(-22,-35), S( 15,-12), S( 8,-14), S( -1, 4), S( -1, 4), S( 8,-14), S( 15,-12), S(-22,-35),
|
||||
S(-28,-33), S( 9,-10), S( 2,-12), S( -7, 6), S( -7, 6), S( 2,-12), S( 9,-10), S(-28,-33),
|
||||
S(-32,-43), S( 5,-20), S( -2,-22), S(-11, -4), S(-11, -4), S( -2,-22), S( 5,-20), S(-32,-43),
|
||||
S(-49,-65), S(-22,-42), S(-29,-44), S(-38,-26), S(-38,-26), S(-29,-44), S(-22,-42), S(-49,-65)
|
||||
},
|
||||
{ // Rook
|
||||
S(-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)
|
||||
S(-22, 3), S(-17, 3), S(-12, 3), S(-8, 3), S(-8, 3), S(-12, 3), S(-17, 3), S(-22, 3),
|
||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
||||
S(-22, 3), S( -7, 3), S( -2, 3), S( 2, 3), S( 2, 3), S( -2, 3), S( -7, 3), S(-22, 3),
|
||||
S(-11, 3), S( 4, 3), S( 9, 3), S(13, 3), S(13, 3), S( 9, 3), S( 4, 3), S(-11, 3),
|
||||
S(-22, 3), S(-17, 3), S(-12, 3), S(-8, 3), S(-8, 3), S(-12, 3), S(-17, 3), S(-22, 3)
|
||||
},
|
||||
{ // Queen
|
||||
S(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)
|
||||
S(-2,-80), S(-2,-54), S(-2,-42), S(-2,-30), S(-2,-30), S(-2,-42), S(-2,-54), S(-2,-80),
|
||||
S(-2,-54), S( 8,-30), S( 8,-18), S( 8, -6), S( 8, -6), S( 8,-18), S( 8,-30), S(-2,-54),
|
||||
S(-2,-42), S( 8,-18), S( 8, -6), S( 8, 6), S( 8, 6), S( 8, -6), S( 8,-18), S(-2,-42),
|
||||
S(-2,-30), S( 8, -6), S( 8, 6), S( 8, 18), S( 8, 18), S( 8, 6), S( 8, -6), S(-2,-30),
|
||||
S(-2,-30), S( 8, -6), S( 8, 6), S( 8, 18), S( 8, 18), S( 8, 6), S( 8, -6), S(-2,-30),
|
||||
S(-2,-42), S( 8,-18), S( 8, -6), S( 8, 6), S( 8, 6), S( 8, -6), S( 8,-18), S(-2,-42),
|
||||
S(-2,-54), S( 8,-30), S( 8,-18), S( 8, -6), S( 8, -6), S( 8,-18), S( 8,-30), S(-2,-54),
|
||||
S(-2,-80), S(-2,-54), S(-2,-42), S(-2,-30), S(-2,-30), S(-2,-42), S(-2,-54), S(-2,-80)
|
||||
},
|
||||
{ // King
|
||||
S(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)
|
||||
S(298, 27), S(332, 81), S(273,108), S(225,116), S(225,116), S(273,108), S(332, 81), S(298, 27),
|
||||
S(287, 74), S(321,128), S(262,155), S(214,163), S(214,163), S(262,155), S(321,128), S(287, 74),
|
||||
S(224,111), S(258,165), S(199,192), S(151,200), S(151,200), S(199,192), S(258,165), S(224,111),
|
||||
S(196,135), S(230,189), S(171,216), S(123,224), S(123,224), S(171,216), S(230,189), S(196,135),
|
||||
S(173,135), S(207,189), S(148,216), S(100,224), S(100,224), S(148,216), S(207,189), S(173,135),
|
||||
S(146,111), S(180,165), S(121,192), S( 73,200), S( 73,200), S(121,192), S(180,165), S(146,111),
|
||||
S(119, 74), S(153,128), S( 94,155), S( 46,163), S( 46,163), S( 94,155), S(153,128), S(119, 74),
|
||||
S( 98, 27), S(132, 81), S( 73,108), S( 25,116), S( 25,116), S( 73,108), S(132, 81), S( 98, 27)
|
||||
}
|
||||
};
|
||||
|
||||
#undef S
|
||||
|
||||
#endif // !defined(PSQTAB_H_INCLUDED)
|
||||
#endif // #ifndef PSQTAB_H_INCLUDED
|
||||
|
||||
+26
-23
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -22,7 +22,7 @@
|
||||
(at your option) any later version.
|
||||
*/
|
||||
|
||||
#if !defined(RKISS_H_INCLUDED)
|
||||
#ifndef RKISS_H_INCLUDED
|
||||
#define RKISS_H_INCLUDED
|
||||
|
||||
#include "types.h"
|
||||
@@ -43,36 +43,39 @@
|
||||
|
||||
class RKISS {
|
||||
|
||||
// Keep variables always together
|
||||
struct S { uint64_t a, b, c, d; } s;
|
||||
uint64_t a, b, c, d;
|
||||
|
||||
uint64_t rotate(uint64_t x, uint64_t k) const {
|
||||
uint64_t rotate_L(uint64_t x, unsigned k) const {
|
||||
return (x << k) | (x >> (64 - k));
|
||||
}
|
||||
|
||||
// Return 64 bit unsigned integer in between [0, 2^64 - 1]
|
||||
uint64_t 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;
|
||||
}
|
||||
|
||||
// Init seed and scramble a few rounds
|
||||
void raninit() {
|
||||
|
||||
s.a = 0xf1ea5eed;
|
||||
s.b = s.c = s.d = 0xd4e12c77;
|
||||
for (int i = 0; i < 73; i++)
|
||||
rand64();
|
||||
const uint64_t e = a - rotate_L(b, 7);
|
||||
a = b ^ rotate_L(c, 13);
|
||||
b = c + rotate_L(d, 37);
|
||||
c = d + e;
|
||||
return d = e + a;
|
||||
}
|
||||
|
||||
public:
|
||||
RKISS() { raninit(); }
|
||||
RKISS(int seed = 73) {
|
||||
|
||||
a = 0xF1EA5EED, b = c = d = 0xD4E12C77;
|
||||
|
||||
for (int i = 0; i < seed; ++i) // Scramble a few rounds
|
||||
rand64();
|
||||
}
|
||||
|
||||
template<typename T> T rand() { return T(rand64()); }
|
||||
|
||||
/// Special generator used to fast init magic numbers. Here the
|
||||
/// trick is to rotate the randoms of a given quantity 's' known
|
||||
/// to be optimal to quickly find a good magic candidate.
|
||||
template<typename T> T magic_rand(int s) {
|
||||
return rotate_L(rotate_L(rand<T>(), (s >> 0) & 0x3F) & rand<T>()
|
||||
, (s >> 6) & 0x3F) & rand<T>();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(RKISS_H_INCLUDED)
|
||||
#endif // #ifndef RKISS_H_INCLUDED
|
||||
|
||||
+843
-964
File diff suppressed because it is too large
Load Diff
+20
-17
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,10 +17,9 @@
|
||||
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
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <stack>
|
||||
#include <vector>
|
||||
@@ -38,14 +37,14 @@ namespace Search {
|
||||
/// has its own array of Stack objects, indexed by the current ply.
|
||||
|
||||
struct Stack {
|
||||
SplitPoint* sp;
|
||||
SplitPoint* splitPoint;
|
||||
int ply;
|
||||
Move currentMove;
|
||||
Move ttMove;
|
||||
Move excludedMove;
|
||||
Move killers[2];
|
||||
Depth reduction;
|
||||
Value eval;
|
||||
Value evalMargin;
|
||||
Value staticEval;
|
||||
int skipNullMove;
|
||||
};
|
||||
|
||||
@@ -56,12 +55,11 @@ struct Stack {
|
||||
/// all non-pv moves.
|
||||
struct RootMove {
|
||||
|
||||
RootMove(){} // Needed by sort()
|
||||
RootMove(Move m) : score(-VALUE_INFINITE), prevScore(-VALUE_INFINITE) {
|
||||
pv.push_back(m); pv.push_back(MOVE_NONE);
|
||||
}
|
||||
|
||||
bool operator<(const RootMove& m) const { return score < m.score; }
|
||||
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);
|
||||
@@ -75,22 +73,26 @@ struct RootMove {
|
||||
|
||||
/// 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.
|
||||
/// or if we have to ponder while it's our opponent's turn to move.
|
||||
|
||||
struct LimitsType {
|
||||
|
||||
LimitsType() { memset(this, 0, sizeof(LimitsType)); }
|
||||
bool use_time_management() const { return !(movetime | depth | nodes | infinite); }
|
||||
LimitsType() { // Using memset on a std::vector is undefined behavior
|
||||
time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = movestogo =
|
||||
depth = nodes = movetime = mate = infinite = ponder = 0;
|
||||
}
|
||||
bool use_time_management() const { return !(mate | movetime | depth | nodes | infinite); }
|
||||
|
||||
int time[2], inc[2], movestogo, depth, nodes, movetime, infinite, ponder;
|
||||
std::vector<Move> searchmoves;
|
||||
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.
|
||||
/// typically in an async fashion e.g. to stop the search by the GUI.
|
||||
|
||||
struct SignalsType {
|
||||
bool stopOnPonderhit, firstRootMove, stop, failedLowAtRoot;
|
||||
bool stop, stopOnPonderhit, firstRootMove, failedLowAtRoot;
|
||||
};
|
||||
|
||||
typedef std::auto_ptr<std::stack<StateInfo> > StateStackPtr;
|
||||
@@ -98,14 +100,15 @@ typedef std::auto_ptr<std::stack<StateInfo> > StateStackPtr;
|
||||
extern volatile SignalsType Signals;
|
||||
extern LimitsType Limits;
|
||||
extern std::vector<RootMove> RootMoves;
|
||||
extern Position RootPosition;
|
||||
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 uint64_t perft(Position& pos, Depth depth);
|
||||
extern void think();
|
||||
|
||||
} // namespace Search
|
||||
|
||||
#endif // !defined(SEARCH_H_INCLUDED)
|
||||
#endif // #ifndef SEARCH_H_INCLUDED
|
||||
|
||||
+220
-270
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,8 +17,8 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <algorithm> // For std::count
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
#include "movegen.h"
|
||||
#include "search.h"
|
||||
@@ -29,101 +29,39 @@ using namespace Search;
|
||||
|
||||
ThreadPool Threads; // Global object
|
||||
|
||||
namespace { extern "C" {
|
||||
|
||||
// start_routine() is the C function which is called when a new thread
|
||||
// is launched. It is a wrapper to member function pointed by start_fn.
|
||||
|
||||
long start_routine(Thread* th) { (th->*(th->start_fn))(); return 0; }
|
||||
|
||||
} }
|
||||
|
||||
|
||||
// Thread c'tor starts a newly-created thread of execution that will call
|
||||
// the idle loop function pointed by start_fn going immediately to sleep.
|
||||
|
||||
Thread::Thread(Fn fn) {
|
||||
|
||||
is_searching = do_exit = false;
|
||||
maxPly = splitPointsCnt = 0;
|
||||
curSplitPoint = NULL;
|
||||
start_fn = fn;
|
||||
idx = Threads.size();
|
||||
|
||||
do_sleep = (fn != &Thread::main_loop); // Avoid a race with start_searching()
|
||||
|
||||
if (!thread_create(handle, start_routine, this))
|
||||
{
|
||||
std::cerr << "Failed to create thread number " << idx << std::endl;
|
||||
::exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Thread d'tor waits for thread termination before to return.
|
||||
|
||||
Thread::~Thread() {
|
||||
|
||||
assert(do_sleep);
|
||||
|
||||
do_exit = true; // Search must be already finished
|
||||
wake_up();
|
||||
thread_join(handle); // Wait for thread termination
|
||||
}
|
||||
|
||||
|
||||
// Thread::timer_loop() is where the timer thread waits maxPly milliseconds and
|
||||
// then calls check_time(). If maxPly is 0 thread sleeps until is woken up.
|
||||
extern void check_time();
|
||||
|
||||
void Thread::timer_loop() {
|
||||
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 the object will be fully initialized
|
||||
// when start_routine (and hence virtual idle_loop) is called and when joining.
|
||||
|
||||
template<typename T> T* new_thread() {
|
||||
T* th = new T();
|
||||
thread_create(th->handle, start_routine, th); // Will go to sleep
|
||||
return th;
|
||||
}
|
||||
|
||||
void delete_thread(ThreadBase* th) {
|
||||
th->exit = true; // Search must be already finished
|
||||
th->notify_one();
|
||||
thread_join(th->handle); // Wait for thread termination
|
||||
delete th;
|
||||
}
|
||||
|
||||
while (!do_exit)
|
||||
{
|
||||
mutex.lock();
|
||||
sleepCondition.wait_for(mutex, maxPly ? maxPly : INT_MAX);
|
||||
mutex.unlock();
|
||||
check_time();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Thread::main_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.
|
||||
// notify_one() wakes up the thread when there is some work to do
|
||||
|
||||
void Thread::main_loop() {
|
||||
|
||||
while (true)
|
||||
{
|
||||
mutex.lock();
|
||||
|
||||
do_sleep = true; // Always return to sleep after a search
|
||||
is_searching = false;
|
||||
|
||||
while (do_sleep && !do_exit)
|
||||
{
|
||||
Threads.sleepCondition.notify_one(); // Wake up UI thread if needed
|
||||
sleepCondition.wait(mutex);
|
||||
}
|
||||
|
||||
mutex.unlock();
|
||||
|
||||
if (do_exit)
|
||||
return;
|
||||
|
||||
is_searching = true;
|
||||
|
||||
Search::think();
|
||||
|
||||
assert(is_searching);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Thread::wake_up() wakes up the thread, normally at the beginning of the search
|
||||
// or, if "sleeping threads" is used at split time.
|
||||
|
||||
void Thread::wake_up() {
|
||||
void ThreadBase::notify_one() {
|
||||
|
||||
mutex.lock();
|
||||
sleepCondition.notify_one();
|
||||
@@ -131,29 +69,35 @@ void Thread::wake_up() {
|
||||
}
|
||||
|
||||
|
||||
// Thread::wait_for_stop_or_ponderhit() is called when the maximum depth is
|
||||
// reached while the program is pondering. The point is to work around a wrinkle
|
||||
// in the UCI protocol: When pondering, the engine is not allowed to give a
|
||||
// "bestmove" before the GUI sends it a "stop" or "ponderhit" command. We simply
|
||||
// wait here until one of these commands (that raise StopRequest) is sent and
|
||||
// then return, after which the bestmove and pondermove will be printed.
|
||||
// wait_for() set the thread to sleep until condition 'b' turns true
|
||||
|
||||
void Thread::wait_for_stop_or_ponderhit() {
|
||||
|
||||
Signals.stopOnPonderhit = true;
|
||||
void ThreadBase::wait_for(volatile const bool& b) {
|
||||
|
||||
mutex.lock();
|
||||
while (!Signals.stop) sleepCondition.wait(mutex);;
|
||||
while (!b) sleepCondition.wait(mutex);
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
// Thread::cutoff_occurred() checks whether a beta cutoff has occurred in the
|
||||
// Thread c'tor just inits data and does not launch any execution thread.
|
||||
// Such a thread will only be started when c'tor returns.
|
||||
|
||||
Thread::Thread() /* : splitPoints() */ { // Value-initialization bug in MSVC
|
||||
|
||||
searching = false;
|
||||
maxPly = splitPointsSize = 0;
|
||||
activeSplitPoint = NULL;
|
||||
activePosition = NULL;
|
||||
idx = Threads.size(); // Starts from 0
|
||||
}
|
||||
|
||||
|
||||
// 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 = curSplitPoint; sp; sp = sp->parent)
|
||||
for (SplitPoint* sp = activeSplitPoint; sp; sp = sp->parentSplitPoint)
|
||||
if (sp->cutoff)
|
||||
return true;
|
||||
|
||||
@@ -161,276 +105,282 @@ bool Thread::cutoff_occurred() const {
|
||||
}
|
||||
|
||||
|
||||
// Thread::is_available_to() checks whether the thread is available to help the
|
||||
// Thread::available_to() checks whether the thread is available to help the
|
||||
// thread 'master' at a split point. An obvious requirement is that thread must
|
||||
// be idle. With more than two threads, this is not sufficient: If the thread is
|
||||
// the master of some active 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).
|
||||
// the master of some split point, it is only available as a slave to the slaves
|
||||
// which are busy searching the split point at the top of slave's split point
|
||||
// stack (the "helpful master concept" in YBWC terminology).
|
||||
|
||||
bool Thread::is_available_to(Thread* master) const {
|
||||
bool Thread::available_to(const Thread* master) const {
|
||||
|
||||
if (is_searching)
|
||||
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 spCnt = splitPointsCnt;
|
||||
// Make a local copy to be sure it doesn't become zero under our feet while
|
||||
// testing next condition and so leading to an out of bounds access.
|
||||
int size = splitPointsSize;
|
||||
|
||||
// No active split points means that the thread is available as a slave for any
|
||||
// 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 !spCnt || (splitPoints[spCnt - 1].slavesMask & (1ULL << master->idx));
|
||||
return !size || splitPoints[size - 1].slavesMask.test(master->idx);
|
||||
}
|
||||
|
||||
|
||||
// init() is called at startup. Initializes lock and condition variable and
|
||||
// launches requested threads sending them immediately to sleep. 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.
|
||||
// TimerThread::idle_loop() is where the timer thread waits msec milliseconds
|
||||
// and then calls check_time(). If msec is 0 thread sleeps until it's woken up.
|
||||
|
||||
void TimerThread::idle_loop() {
|
||||
|
||||
while (!exit)
|
||||
{
|
||||
mutex.lock();
|
||||
|
||||
if (!exit)
|
||||
sleepCondition.wait_for(mutex, run ? Resolution : INT_MAX);
|
||||
|
||||
mutex.unlock();
|
||||
|
||||
if (run)
|
||||
check_time();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MainThread::idle_loop() is where the main thread is parked waiting to be started
|
||||
// when there is a new search. The main thread will launch all the slave threads.
|
||||
|
||||
void MainThread::idle_loop() {
|
||||
|
||||
while (true)
|
||||
{
|
||||
mutex.lock();
|
||||
|
||||
thinking = false;
|
||||
|
||||
while (!thinking && !exit)
|
||||
{
|
||||
Threads.sleepCondition.notify_one(); // Wake up the UI thread if needed
|
||||
sleepCondition.wait(mutex);
|
||||
}
|
||||
|
||||
mutex.unlock();
|
||||
|
||||
if (exit)
|
||||
return;
|
||||
|
||||
searching = true;
|
||||
|
||||
Search::think();
|
||||
|
||||
assert(searching);
|
||||
|
||||
searching = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// init() is called at startup to create and launch requested threads, that will
|
||||
// go immediately to sleep. We cannot use a c'tor because Threads is a static
|
||||
// object and we need a fully initialized engine at this point due to allocation
|
||||
// of Endgames in Thread c'tor.
|
||||
|
||||
void ThreadPool::init() {
|
||||
|
||||
timer = new Thread(&Thread::timer_loop);
|
||||
threads.push_back(new Thread(&Thread::main_loop));
|
||||
timer = new_thread<TimerThread>();
|
||||
push_back(new_thread<MainThread>());
|
||||
read_uci_options();
|
||||
}
|
||||
|
||||
|
||||
// exit() cleanly terminates the threads before the program exits.
|
||||
// exit() cleanly terminates the threads before the program exits. Cannot be done in
|
||||
// d'tor because we have to terminate the threads before to free ThreadPool object.
|
||||
|
||||
void ThreadPool::exit() {
|
||||
|
||||
for (size_t i = 0; i < threads.size(); i++)
|
||||
delete threads[i];
|
||||
delete_thread(timer); // As first because check_time() accesses threads data
|
||||
|
||||
delete timer;
|
||||
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.
|
||||
// objects are dynamically allocated to avoid creating all possible threads
|
||||
// in advance (which include pawns and material tables), even if only a few
|
||||
// are to be used.
|
||||
|
||||
void ThreadPool::read_uci_options() {
|
||||
|
||||
maxThreadsPerSplitPoint = Options["Max Threads per Split Point"];
|
||||
minimumSplitDepth = Options["Min Split Depth"] * ONE_PLY;
|
||||
useSleepingThreads = Options["Use Sleeping Threads"];
|
||||
size_t requested = Options["Threads"];
|
||||
minimumSplitDepth = Options["Min Split Depth"] * ONE_PLY;
|
||||
size_t requested = Options["Threads"];
|
||||
|
||||
assert(requested > 0);
|
||||
|
||||
while (threads.size() < requested)
|
||||
threads.push_back(new Thread(&Thread::idle_loop));
|
||||
// If zero (default) then set best minimum split depth automatically
|
||||
if (!minimumSplitDepth)
|
||||
minimumSplitDepth = requested < 8 ? 4 * ONE_PLY : 7 * ONE_PLY;
|
||||
|
||||
while (threads.size() > requested)
|
||||
while (size() < requested)
|
||||
push_back(new_thread<Thread>());
|
||||
|
||||
while (size() > requested)
|
||||
{
|
||||
delete threads.back();
|
||||
threads.pop_back();
|
||||
delete_thread(back());
|
||||
pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// wake_up() is called before a new search to start the threads that are waiting
|
||||
// on the sleep condition and to reset maxPly. When useSleepingThreads is set
|
||||
// threads will be woken up at split time.
|
||||
// available_slave() tries to find an idle thread which is available as a slave
|
||||
// for the thread 'master'.
|
||||
|
||||
void ThreadPool::wake_up() const {
|
||||
Thread* ThreadPool::available_slave(const Thread* master) const {
|
||||
|
||||
for (size_t i = 0; i < threads.size(); i++)
|
||||
{
|
||||
threads[i]->maxPly = 0;
|
||||
threads[i]->do_sleep = false;
|
||||
for (const_iterator it = begin(); it != end(); ++it)
|
||||
if ((*it)->available_to(master))
|
||||
return *it;
|
||||
|
||||
if (!useSleepingThreads)
|
||||
threads[i]->wake_up();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// sleep() is called after the search finishes to ask all the threads but the
|
||||
// main one to go waiting on a sleep condition.
|
||||
|
||||
void ThreadPool::sleep() const {
|
||||
|
||||
// Main thread will go to sleep by itself to avoid a race with start_searching()
|
||||
for (size_t i = 1; i < threads.size(); i++)
|
||||
threads[i]->do_sleep = true;
|
||||
}
|
||||
|
||||
|
||||
// available_slave_exists() tries to find an idle thread which is available as
|
||||
// a slave for the thread 'master'.
|
||||
|
||||
bool ThreadPool::available_slave_exists(Thread* master) const {
|
||||
|
||||
for (size_t i = 0; i < threads.size(); i++)
|
||||
if (threads[i]->is_available_to(master))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
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, or because we have no unused split
|
||||
// point objects), 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.
|
||||
// (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>
|
||||
Value ThreadPool::split(Position& pos, Stack* ss, Value alpha, Value beta,
|
||||
Value bestValue, Move* bestMove, Depth depth,
|
||||
Move threatMove, int moveCount, MovePicker* mp, int nodeType) {
|
||||
void Thread::split(Position& pos, const Stack* ss, Value alpha, Value beta, Value* bestValue,
|
||||
Move* bestMove, Depth depth, int moveCount,
|
||||
MovePicker* movePicker, int nodeType, bool cutNode) {
|
||||
|
||||
assert(pos.pos_is_ok());
|
||||
assert(bestValue > -VALUE_INFINITE);
|
||||
assert(bestValue <= alpha);
|
||||
assert(alpha < beta);
|
||||
assert(beta <= VALUE_INFINITE);
|
||||
assert(depth > DEPTH_ZERO);
|
||||
|
||||
Thread* master = pos.this_thread();
|
||||
|
||||
if (master->splitPointsCnt >= MAX_SPLITPOINTS_PER_THREAD)
|
||||
return bestValue;
|
||||
assert(-VALUE_INFINITE < *bestValue && *bestValue <= alpha && alpha < beta && beta <= 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 = master->splitPoints[master->splitPointsCnt];
|
||||
SplitPoint& sp = splitPoints[splitPointsSize];
|
||||
|
||||
sp.parent = master->curSplitPoint;
|
||||
sp.master = master;
|
||||
sp.cutoff = false;
|
||||
sp.slavesMask = 1ULL << master->idx;
|
||||
sp.masterThread = this;
|
||||
sp.parentSplitPoint = activeSplitPoint;
|
||||
sp.slavesMask = 0, sp.slavesMask.set(idx);
|
||||
sp.depth = depth;
|
||||
sp.bestValue = *bestValue;
|
||||
sp.bestMove = *bestMove;
|
||||
sp.threatMove = threatMove;
|
||||
sp.alpha = alpha;
|
||||
sp.beta = beta;
|
||||
sp.nodeType = nodeType;
|
||||
sp.bestValue = bestValue;
|
||||
sp.mp = mp;
|
||||
sp.cutNode = cutNode;
|
||||
sp.movePicker = movePicker;
|
||||
sp.moveCount = moveCount;
|
||||
sp.pos = &pos;
|
||||
sp.nodes = 0;
|
||||
sp.cutoff = false;
|
||||
sp.ss = ss;
|
||||
|
||||
assert(master->is_searching);
|
||||
|
||||
master->curSplitPoint = &sp;
|
||||
int slavesCnt = 0;
|
||||
|
||||
// Try to allocate available threads and ask them to start searching setting
|
||||
// is_searching flag. This must be done under lock protection to avoid concurrent
|
||||
// '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();
|
||||
mutex.lock();
|
||||
|
||||
for (size_t i = 0; i < threads.size() && !Fake; ++i)
|
||||
if (threads[i]->is_available_to(master))
|
||||
sp.allSlavesSearching = true; // Must be set under lock protection
|
||||
++splitPointsSize;
|
||||
activeSplitPoint = &sp;
|
||||
activePosition = NULL;
|
||||
|
||||
if (!Fake)
|
||||
for (Thread* slave; (slave = Threads.available_slave(this)) != NULL; )
|
||||
{
|
||||
sp.slavesMask |= 1ULL << i;
|
||||
threads[i]->curSplitPoint = &sp;
|
||||
threads[i]->is_searching = true; // Slave leaves idle_loop()
|
||||
|
||||
if (useSleepingThreads)
|
||||
threads[i]->wake_up();
|
||||
|
||||
if (++slavesCnt + 1 >= maxThreadsPerSplitPoint) // Master is always included
|
||||
break;
|
||||
sp.slavesMask.set(slave->idx);
|
||||
slave->activeSplitPoint = &sp;
|
||||
slave->searching = true; // Slave leaves idle_loop()
|
||||
slave->notify_one(); // Could be sleeping
|
||||
}
|
||||
|
||||
master->splitPointsCnt++;
|
||||
|
||||
mutex.unlock();
|
||||
sp.mutex.unlock();
|
||||
|
||||
// Everything is set up. The master thread enters the idle loop, from which
|
||||
// it will instantly launch a search, because its is_searching flag is set.
|
||||
// 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 || Fake)
|
||||
{
|
||||
master->idle_loop();
|
||||
sp.mutex.unlock();
|
||||
Threads.mutex.unlock();
|
||||
|
||||
// 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(!master->is_searching);
|
||||
}
|
||||
Thread::idle_loop(); // Force a call to base class idle_loop()
|
||||
|
||||
// In the helpful master concept, a master can help only a sub-tree of its
|
||||
// split point and because everything is finished here, it's not possible
|
||||
// for the master to be booked.
|
||||
assert(!searching);
|
||||
assert(!activePosition);
|
||||
|
||||
// We have returned from the idle loop, which means that all threads are
|
||||
// finished. Note that setting is_searching and decreasing splitPointsCnt is
|
||||
// done under lock protection to avoid a race with Thread::is_available_to().
|
||||
sp.mutex.lock(); // To protect sp.nodes
|
||||
mutex.lock();
|
||||
// finished. Note that setting 'searching' and decreasing splitPointsSize is
|
||||
// done under lock protection to avoid a race with Thread::available_to().
|
||||
Threads.mutex.lock();
|
||||
sp.mutex.lock();
|
||||
|
||||
master->is_searching = true;
|
||||
master->splitPointsCnt--;
|
||||
master->curSplitPoint = sp.parent;
|
||||
searching = true;
|
||||
--splitPointsSize;
|
||||
activeSplitPoint = sp.parentSplitPoint;
|
||||
activePosition = &pos;
|
||||
pos.set_nodes_searched(pos.nodes_searched() + sp.nodes);
|
||||
*bestMove = sp.bestMove;
|
||||
*bestValue = sp.bestValue;
|
||||
|
||||
mutex.unlock();
|
||||
sp.mutex.unlock();
|
||||
|
||||
return sp.bestValue;
|
||||
Threads.mutex.unlock();
|
||||
}
|
||||
|
||||
// Explicit template instantiations
|
||||
template Value ThreadPool::split<false>(Position&, Stack*, Value, Value, Value, Move*, Depth, Move, int, MovePicker*, int);
|
||||
template Value ThreadPool::split<true>(Position&, Stack*, Value, Value, Value, Move*, Depth, Move, int, MovePicker*, int);
|
||||
template void Thread::split<false>(Position&, const Stack*, Value, Value, Value*, Move*, Depth, int, MovePicker*, int, bool);
|
||||
template void Thread::split< true>(Position&, const Stack*, Value, Value, Value*, Move*, Depth, int, MovePicker*, int, bool);
|
||||
|
||||
|
||||
// set_timer() is used to set the timer to trigger after msec milliseconds.
|
||||
// If msec is 0 then timer is stopped.
|
||||
// wait_for_think_finished() waits for main thread to go to sleep then returns
|
||||
|
||||
void ThreadPool::set_timer(int msec) {
|
||||
void ThreadPool::wait_for_think_finished() {
|
||||
|
||||
timer->mutex.lock();
|
||||
timer->maxPly = msec;
|
||||
timer->sleepCondition.notify_one(); // Wake up and restart the timer
|
||||
timer->mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
// wait_for_search_finished() waits for main thread to go to sleep, this means
|
||||
// search is finished. Then returns.
|
||||
|
||||
void ThreadPool::wait_for_search_finished() {
|
||||
|
||||
Thread* t = main_thread();
|
||||
MainThread* t = main();
|
||||
t->mutex.lock();
|
||||
t->sleepCondition.notify_one(); // In case is waiting for stop or ponderhit
|
||||
while (!t->do_sleep) sleepCondition.wait(t->mutex);
|
||||
while (t->thinking) sleepCondition.wait(t->mutex);
|
||||
t->mutex.unlock();
|
||||
}
|
||||
|
||||
|
||||
// start_searching() wakes up the main thread sleeping in main_loop() so to start
|
||||
// a new search, then returns immediately.
|
||||
// start_thinking() wakes up the main thread sleeping in MainThread::idle_loop()
|
||||
// so to start a new search, then returns immediately.
|
||||
|
||||
void ThreadPool::start_searching(const Position& pos, const LimitsType& limits,
|
||||
const std::vector<Move>& searchMoves, StateStackPtr& states) {
|
||||
wait_for_search_finished();
|
||||
void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits, StateStackPtr& states) {
|
||||
|
||||
wait_for_think_finished();
|
||||
|
||||
SearchTime = Time::now(); // As early as possible
|
||||
|
||||
Signals.stopOnPonderhit = Signals.firstRootMove = false;
|
||||
Signals.stop = Signals.failedLowAtRoot = false;
|
||||
|
||||
RootPosition = pos;
|
||||
Limits = limits;
|
||||
SetupStates = states; // Ownership transfer here
|
||||
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> ml(pos); !ml.end(); ++ml)
|
||||
if (searchMoves.empty() || count(searchMoves.begin(), searchMoves.end(), ml.move()))
|
||||
RootMoves.push_back(RootMove(ml.move()));
|
||||
for (MoveList<LEGAL> it(pos); *it; ++it)
|
||||
if ( limits.searchmoves.empty()
|
||||
|| std::count(limits.searchmoves.begin(), limits.searchmoves.end(), *it))
|
||||
RootMoves.push_back(RootMove(*it));
|
||||
|
||||
main_thread()->do_sleep = false;
|
||||
main_thread()->wake_up();
|
||||
main()->thinking = true;
|
||||
main()->notify_one(); // Starts main thread
|
||||
}
|
||||
|
||||
+73
-64
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,9 +17,10 @@
|
||||
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 <bitset>
|
||||
#include <vector>
|
||||
|
||||
#include "material.h"
|
||||
@@ -28,7 +29,7 @@
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
|
||||
const int MAX_THREADS = 32;
|
||||
const int MAX_THREADS = 128;
|
||||
const int MAX_SPLITPOINTS_PER_THREAD = 8;
|
||||
|
||||
struct Mutex {
|
||||
@@ -56,27 +57,28 @@ private:
|
||||
WaitCondition c;
|
||||
};
|
||||
|
||||
class Thread;
|
||||
struct Thread;
|
||||
|
||||
struct SplitPoint {
|
||||
|
||||
// Const data after split point has been setup
|
||||
const Position* pos;
|
||||
const Search::Stack* ss;
|
||||
Thread* masterThread;
|
||||
Depth depth;
|
||||
Value beta;
|
||||
int nodeType;
|
||||
Thread* master;
|
||||
Move threatMove;
|
||||
bool cutNode;
|
||||
|
||||
// Const pointers to shared data
|
||||
MovePicker* mp;
|
||||
SplitPoint* parent;
|
||||
MovePicker* movePicker;
|
||||
SplitPoint* parentSplitPoint;
|
||||
|
||||
// Shared data
|
||||
Mutex mutex;
|
||||
volatile uint64_t slavesMask;
|
||||
volatile int64_t nodes;
|
||||
std::bitset<MAX_THREADS> slavesMask;
|
||||
volatile bool allSlavesSearching;
|
||||
volatile uint64_t nodes;
|
||||
volatile Value alpha;
|
||||
volatile Value bestValue;
|
||||
volatile Move bestMove;
|
||||
@@ -85,84 +87,91 @@ struct SplitPoint {
|
||||
};
|
||||
|
||||
|
||||
/// ThreadBase struct is the base of the hierarchy from where we derive all the
|
||||
/// specialized thread classes.
|
||||
|
||||
struct ThreadBase {
|
||||
|
||||
ThreadBase() : handle(NativeHandle()), exit(false) {}
|
||||
virtual ~ThreadBase() {}
|
||||
virtual void idle_loop() = 0;
|
||||
void notify_one();
|
||||
void wait_for(volatile const bool& b);
|
||||
|
||||
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.
|
||||
|
||||
class Thread {
|
||||
struct Thread : public ThreadBase {
|
||||
|
||||
typedef void (Thread::* Fn) (); // Pointer to member function
|
||||
|
||||
public:
|
||||
Thread(Fn fn);
|
||||
~Thread();
|
||||
|
||||
void wake_up();
|
||||
Thread();
|
||||
virtual void idle_loop();
|
||||
bool cutoff_occurred() const;
|
||||
bool is_available_to(Thread* master) const;
|
||||
void idle_loop();
|
||||
void main_loop();
|
||||
void timer_loop();
|
||||
void wait_for_stop_or_ponderhit();
|
||||
bool 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, int moveCount, MovePicker* movePicker, int nodeType, bool cutNode);
|
||||
|
||||
SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD];
|
||||
MaterialTable materialTable;
|
||||
PawnTable pawnTable;
|
||||
Material::Table materialTable;
|
||||
Endgames endgames;
|
||||
Pawns::Table pawnsTable;
|
||||
Position* activePosition;
|
||||
size_t idx;
|
||||
int maxPly;
|
||||
Mutex mutex;
|
||||
ConditionVariable sleepCondition;
|
||||
NativeHandle handle;
|
||||
Fn start_fn;
|
||||
SplitPoint* volatile curSplitPoint;
|
||||
volatile int splitPointsCnt;
|
||||
volatile bool is_searching;
|
||||
volatile bool do_sleep;
|
||||
volatile bool do_exit;
|
||||
SplitPoint* volatile activeSplitPoint;
|
||||
volatile int splitPointsSize;
|
||||
volatile bool searching;
|
||||
};
|
||||
|
||||
|
||||
/// ThreadPool class handles all the threads related stuff like init, starting,
|
||||
/// parking and, the most important, launching a slave thread at a split point.
|
||||
/// 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() : run(false) {}
|
||||
virtual void idle_loop();
|
||||
bool run;
|
||||
static const int Resolution = 5; // msec between two check_time() calls
|
||||
};
|
||||
|
||||
|
||||
/// ThreadPool struct handles all the threads related stuff like init, starting,
|
||||
/// parking and, most importantly, launching a slave thread at a split point.
|
||||
/// All the access to shared thread data is done through this class.
|
||||
|
||||
class ThreadPool {
|
||||
struct ThreadPool : public std::vector<Thread*> {
|
||||
|
||||
public:
|
||||
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.
|
||||
void exit(); // be initialized and are valid during the whole thread lifetime.
|
||||
|
||||
Thread& operator[](size_t id) { return *threads[id]; }
|
||||
bool use_sleeping_threads() const { return useSleepingThreads; }
|
||||
int min_split_depth() const { return minimumSplitDepth; }
|
||||
size_t size() const { return threads.size(); }
|
||||
Thread* main_thread() { return threads[0]; }
|
||||
|
||||
void wake_up() const;
|
||||
void sleep() const;
|
||||
MainThread* main() { return static_cast<MainThread*>((*this)[0]); }
|
||||
void read_uci_options();
|
||||
bool available_slave_exists(Thread* master) const;
|
||||
void set_timer(int msec);
|
||||
void wait_for_search_finished();
|
||||
void start_searching(const Position&, const Search::LimitsType&,
|
||||
const std::vector<Move>&, Search::StateStackPtr&);
|
||||
Thread* available_slave(const Thread* master) const;
|
||||
void wait_for_think_finished();
|
||||
void start_thinking(const Position&, const Search::LimitsType&, Search::StateStackPtr&);
|
||||
|
||||
template <bool Fake>
|
||||
Value split(Position& pos, Search::Stack* ss, Value alpha, Value beta, Value bestValue, Move* bestMove,
|
||||
Depth depth, Move threatMove, int moveCount, MovePicker* mp, int nodeType);
|
||||
private:
|
||||
friend class Thread;
|
||||
|
||||
std::vector<Thread*> threads;
|
||||
Thread* timer;
|
||||
Depth minimumSplitDepth;
|
||||
Mutex mutex;
|
||||
ConditionVariable sleepCondition;
|
||||
Depth minimumSplitDepth;
|
||||
int maxThreadsPerSplitPoint;
|
||||
bool useSleepingThreads;
|
||||
TimerThread* timer;
|
||||
};
|
||||
|
||||
extern ThreadPool Threads;
|
||||
|
||||
#endif // !defined(THREAD_H_INCLUDED)
|
||||
#endif // #ifndef THREAD_H_INCLUDED
|
||||
|
||||
+45
-81
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,8 +17,9 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <cfloat>
|
||||
#include <cmath>
|
||||
|
||||
#include "search.h"
|
||||
#include "timeman.h"
|
||||
@@ -26,66 +27,51 @@
|
||||
|
||||
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 StealRatio = 0.33f; // However we must not steal time from remaining moves over this ratio
|
||||
|
||||
|
||||
// MoveImportance[] is based on naive statistical analysis of "how many games are still undecided
|
||||
// after n half-moves". Game is considered "undecided" as long as neither side has >275cp advantage.
|
||||
// Data was extracted from CCRL game database with some simple filtering criteria.
|
||||
const int MoveImportance[512] = {
|
||||
7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780, 7780,
|
||||
7780, 7780, 7780, 7780, 7778, 7778, 7776, 7776, 7776, 7773, 7770, 7768, 7766, 7763, 7757, 7751,
|
||||
7743, 7735, 7724, 7713, 7696, 7689, 7670, 7656, 7627, 7605, 7571, 7549, 7522, 7493, 7462, 7425,
|
||||
7385, 7350, 7308, 7272, 7230, 7180, 7139, 7094, 7055, 7010, 6959, 6902, 6841, 6778, 6705, 6651,
|
||||
6569, 6508, 6435, 6378, 6323, 6253, 6152, 6085, 5995, 5931, 5859, 5794, 5717, 5646, 5544, 5462,
|
||||
5364, 5282, 5172, 5078, 4988, 4901, 4831, 4764, 4688, 4609, 4536, 4443, 4365, 4293, 4225, 4155,
|
||||
4085, 4005, 3927, 3844, 3765, 3693, 3634, 3560, 3479, 3404, 3331, 3268, 3207, 3146, 3077, 3011,
|
||||
2947, 2894, 2828, 2776, 2727, 2676, 2626, 2589, 2538, 2490, 2442, 2394, 2345, 2302, 2243, 2192,
|
||||
2156, 2115, 2078, 2043, 2004, 1967, 1922, 1893, 1845, 1809, 1772, 1736, 1702, 1674, 1640, 1605,
|
||||
1566, 1536, 1509, 1479, 1452, 1423, 1388, 1362, 1332, 1304, 1289, 1266, 1250, 1228, 1206, 1180,
|
||||
1160, 1134, 1118, 1100, 1080, 1068, 1051, 1034, 1012, 1001, 980, 960, 945, 934, 916, 900, 888,
|
||||
878, 865, 852, 828, 807, 787, 770, 753, 744, 731, 722, 706, 700, 683, 676, 671, 664, 652, 641,
|
||||
634, 627, 613, 604, 591, 582, 568, 560, 552, 540, 534, 529, 519, 509, 495, 484, 474, 467, 460,
|
||||
450, 438, 427, 419, 410, 406, 399, 394, 387, 382, 377, 372, 366, 359, 353, 348, 343, 337, 333,
|
||||
328, 321, 315, 309, 303, 298, 293, 287, 284, 281, 277, 273, 265, 261, 255, 251, 247, 241, 240,
|
||||
235, 229, 218, 217, 213, 212, 208, 206, 197, 193, 191, 189, 185, 184, 180, 177, 172, 170, 170,
|
||||
170, 166, 163, 159, 158, 156, 155, 151, 146, 141, 138, 136, 132, 130, 128, 125, 123, 122, 118,
|
||||
118, 118, 117, 115, 114, 108, 107, 105, 105, 105, 102, 97, 97, 95, 94, 93, 91, 88, 86, 83, 80,
|
||||
80, 79, 79, 79, 78, 76, 75, 72, 72, 71, 70, 68, 65, 63, 61, 61, 59, 59, 59, 58, 56, 55, 54, 54,
|
||||
52, 49, 48, 48, 48, 48, 45, 45, 45, 44, 43, 41, 41, 41, 41, 40, 40, 38, 37, 36, 34, 34, 34, 33,
|
||||
31, 29, 29, 29, 28, 28, 28, 28, 28, 28, 28, 27, 27, 27, 27, 27, 24, 24, 23, 23, 22, 21, 20, 20,
|
||||
19, 19, 19, 19, 19, 18, 18, 18, 18, 17, 17, 17, 17, 17, 16, 16, 15, 15, 14, 14, 14, 12, 12, 11,
|
||||
9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
8, 8, 8, 8, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 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, 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[std::min(ply, 511)]; }
|
||||
|
||||
|
||||
/// Function Prototypes
|
||||
|
||||
enum TimeType { OptimumTime, MaxTime };
|
||||
|
||||
template<TimeType>
|
||||
int remaining(int myTime, int movesToGo, int fullMoveNumber, int slowMover);
|
||||
}
|
||||
const int MoveHorizon = 50; // Plan time management at most this many moves ahead
|
||||
const double MaxRatio = 7.0; // When in trouble, we can step over reserved time with this ratio
|
||||
const double StealRatio = 0.33; // However we must not steal time from remaining moves over this ratio
|
||||
|
||||
const double xscale = 9.3;
|
||||
const double xshift = 59.8;
|
||||
const double skewfactor = 0.172;
|
||||
|
||||
|
||||
void TimeManager::pv_instability(int curChanges, int prevChanges) {
|
||||
// move_importance() is a skew-logistic function based on naive statistical
|
||||
// analysis of "how many games are still undecided after n half-moves". Game
|
||||
// is considered "undecided" as long as neither side has >275cp advantage.
|
||||
// Data was extracted from CCRL game database with some simple filtering criteria.
|
||||
|
||||
unstablePVExtraTime = curChanges * (optimumSearchTime / 2)
|
||||
+ prevChanges * (optimumSearchTime / 3);
|
||||
}
|
||||
double move_importance(int ply) {
|
||||
|
||||
return pow((1 + exp((ply - xshift) / xscale)), -skewfactor) + DBL_MIN; // Ensure non-zero
|
||||
}
|
||||
|
||||
template<TimeType T>
|
||||
int remaining(int myTime, int movesToGo, int currentPly, int slowMover)
|
||||
{
|
||||
const double TMaxRatio = (T == OptimumTime ? 1 : MaxRatio);
|
||||
const double TStealRatio = (T == OptimumTime ? 0 : StealRatio);
|
||||
|
||||
double thisMoveImportance = (move_importance(currentPly) * slowMover) / 100;
|
||||
double otherMovesImportance = 0;
|
||||
|
||||
for (int i = 1; i < movesToGo; ++i)
|
||||
otherMovesImportance += move_importance(currentPly + 2 * i);
|
||||
|
||||
double ratio1 = (TMaxRatio * thisMoveImportance) / (TMaxRatio * thisMoveImportance + otherMovesImportance);
|
||||
double ratio2 = (thisMoveImportance + TStealRatio * otherMovesImportance) / (thisMoveImportance + otherMovesImportance);
|
||||
|
||||
return int(myTime * std::min(ratio1, ratio2));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
void TimeManager::init(const Search::LimitsType& limits, int currentPly, Color us)
|
||||
{
|
||||
/* We support four different kind of time controls:
|
||||
/* We support four different kinds of time controls:
|
||||
|
||||
increment == 0 && movesToGo == 0 means: x basetime [sudden death!]
|
||||
increment == 0 && movesToGo != 0 means: x moves in y minutes
|
||||
@@ -109,15 +95,15 @@ void TimeManager::init(const Search::LimitsType& limits, int currentPly, Color u
|
||||
int minThinkingTime = Options["Minimum Thinking Time"];
|
||||
int slowMover = Options["Slow Mover"];
|
||||
|
||||
// Initialize to maximum values but unstablePVExtraTime that is reset
|
||||
unstablePVExtraTime = 0;
|
||||
optimumSearchTime = maximumSearchTime = limits.time[us];
|
||||
// Initialize unstablePvFactor to 1 and search times to maximum values
|
||||
unstablePvFactor = 1;
|
||||
optimumSearchTime = maximumSearchTime = std::max(limits.time[us], minThinkingTime);
|
||||
|
||||
// We calculate optimum time usage for different hypothetic "moves to go"-values and choose the
|
||||
// We calculate optimum time usage for different hypothetical "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 <= (limits.movestogo ? std::min(limits.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
|
||||
// Calculate thinking time for hypothetical "moves to go"-value
|
||||
hypMyTime = limits.time[us]
|
||||
+ limits.inc[us] * (hypMTG - 1)
|
||||
- emergencyBaseTime
|
||||
@@ -138,25 +124,3 @@ void TimeManager::init(const Search::LimitsType& limits, int currentPly, Color u
|
||||
// Make sure that maxSearchTime is not over absoluteMaxSearchTime
|
||||
optimumSearchTime = std::min(optimumSearchTime, maximumSearchTime);
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
template<TimeType T>
|
||||
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) * slowMover / 100;
|
||||
int otherMovesImportance = 0;
|
||||
|
||||
for (int i = 1; i < movesToGo; i++)
|
||||
otherMovesImportance += move_importance(currentPly + 2 * i);
|
||||
|
||||
float ratio1 = (TMaxRatio * thisMoveImportance) / float(TMaxRatio * thisMoveImportance + otherMovesImportance);
|
||||
float ratio2 = (thisMoveImportance + TStealRatio * otherMovesImportance) / float(thisMoveImportance + otherMovesImportance);
|
||||
|
||||
return int(floor(myTime * std::min(ratio1, ratio2)));
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,23 +17,23 @@
|
||||
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
|
||||
|
||||
/// The TimeManager class computes the optimal time to think depending on the
|
||||
/// maximum available time, the move game number and other parameters.
|
||||
/// maximum available time, the game move number and other parameters.
|
||||
|
||||
class TimeManager {
|
||||
public:
|
||||
void init(const Search::LimitsType& limits, int currentPly, Color us);
|
||||
void pv_instability(int curChanges, int prevChanges);
|
||||
int available_time() const { return optimumSearchTime + unstablePVExtraTime; }
|
||||
void pv_instability(double bestMoveChanges) { unstablePvFactor = 1 + bestMoveChanges; }
|
||||
int available_time() const { return int(optimumSearchTime * unstablePvFactor * 0.71); }
|
||||
int maximum_time() const { return maximumSearchTime; }
|
||||
|
||||
private:
|
||||
int optimumSearchTime;
|
||||
int maximumSearchTime;
|
||||
int unstablePVExtraTime;
|
||||
double unstablePvFactor;
|
||||
};
|
||||
|
||||
#endif // !defined(TIMEMAN_H_INCLUDED)
|
||||
#endif // #ifndef TIMEMAN_H_INCLUDED
|
||||
|
||||
+55
-75
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -25,42 +25,32 @@
|
||||
|
||||
TranspositionTable TT; // Our global transposition table
|
||||
|
||||
TranspositionTable::TranspositionTable() {
|
||||
|
||||
size = generation = 0;
|
||||
entries = NULL;
|
||||
}
|
||||
/// TranspositionTable::resize() 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.
|
||||
|
||||
TranspositionTable::~TranspositionTable() {
|
||||
void TranspositionTable::resize(uint64_t mbSize) {
|
||||
|
||||
delete [] entries;
|
||||
}
|
||||
assert(msb((mbSize << 20) / sizeof(TTEntry)) < 32);
|
||||
|
||||
uint32_t size = ClusterSize << msb((mbSize << 20) / sizeof(TTEntry[ClusterSize]));
|
||||
|
||||
/// TranspositionTable::set_size() sets the size of the transposition table,
|
||||
/// measured in megabytes. Transposition table consists of a power of 2 number of
|
||||
/// TTCluster and each cluster consists of ClusterSize number of TTEntries. Each
|
||||
/// non-empty entry contains information of exactly one position.
|
||||
|
||||
void TranspositionTable::set_size(size_t mbSize) {
|
||||
|
||||
size_t newSize = 1ULL << msb((mbSize << 20) / sizeof(TTCluster));
|
||||
|
||||
if (newSize == size)
|
||||
if (hashMask == size - ClusterSize)
|
||||
return;
|
||||
|
||||
size = newSize;
|
||||
delete [] entries;
|
||||
entries = new (std::nothrow) TTCluster[size];
|
||||
hashMask = size - ClusterSize;
|
||||
free(mem);
|
||||
mem = calloc(size * sizeof(TTEntry) + CACHE_LINE_SIZE - 1, 1);
|
||||
|
||||
if (!entries)
|
||||
if (!mem)
|
||||
{
|
||||
std::cerr << "Failed to allocate " << mbSize
|
||||
<< "MB for transposition table." << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
clear(); // Operator new is not guaranteed to initialize memory to zero
|
||||
table = (TTEntry*)((uintptr_t(mem) + CACHE_LINE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1));
|
||||
}
|
||||
|
||||
|
||||
@@ -70,47 +60,7 @@ void TranspositionTable::set_size(size_t mbSize) {
|
||||
|
||||
void TranspositionTable::clear() {
|
||||
|
||||
memset(entries, 0, size * sizeof(TTCluster));
|
||||
}
|
||||
|
||||
|
||||
/// 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::store(const Key posKey, Value v, Bound t, Depth d, Move m, Value statV, Value kingD) {
|
||||
|
||||
int c1, c2, c3;
|
||||
TTEntry *tte, *replace;
|
||||
uint32_t posKey32 = posKey >> 32; // Use the high 32 bits as key inside the cluster
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Implement replace strategy
|
||||
c1 = (replace->generation() == generation ? 2 : 0);
|
||||
c2 = (tte->generation() == generation || tte->type() == BOUND_EXACT ? -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);
|
||||
std::memset(table, 0, (hashMask + ClusterSize) * sizeof(TTEntry));
|
||||
}
|
||||
|
||||
|
||||
@@ -118,24 +68,54 @@ void TranspositionTable::store(const Key posKey, Value v, Bound t, Depth d, Move
|
||||
/// transposition table. Returns a pointer to the TTEntry or NULL if
|
||||
/// position is not found.
|
||||
|
||||
TTEntry* TranspositionTable::probe(const Key posKey) const {
|
||||
const TTEntry* TranspositionTable::probe(const Key key) const {
|
||||
|
||||
uint32_t posKey32 = posKey >> 32;
|
||||
TTEntry* tte = first_entry(posKey);
|
||||
TTEntry* tte = first_entry(key);
|
||||
uint32_t key32 = key >> 32;
|
||||
|
||||
for (int i = 0; i < ClusterSize; i++, tte++)
|
||||
if (tte->key() == posKey32)
|
||||
for (unsigned i = 0; i < ClusterSize; ++i, ++tte)
|
||||
if (tte->key32 == key32)
|
||||
{
|
||||
tte->generation8 = generation; // Refresh
|
||||
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 in which cluster the position will be placed.
|
||||
/// When a new entry is written and there are no empty entries available in the
|
||||
/// cluster, it replaces the least valuable of the entries. A TTEntry t1 is considered
|
||||
/// to be more valuable than a TTEntry t2 if t1 is from the current search and t2
|
||||
/// is from a previous search, or if the depth of t1 is bigger than the depth of t2.
|
||||
|
||||
void TranspositionTable::new_search() {
|
||||
generation++;
|
||||
void TranspositionTable::store(const Key key, Value v, Bound b, Depth d, Move m, Value statV) {
|
||||
|
||||
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->key32 || tte->key32 == key32) // Empty or overwrite old
|
||||
{
|
||||
if (!m)
|
||||
m = tte->move(); // Preserve any existing ttMove
|
||||
|
||||
replace = tte;
|
||||
break;
|
||||
}
|
||||
|
||||
// Implement replace strategy
|
||||
if ( ( tte->generation8 == generation || tte->bound() == BOUND_EXACT)
|
||||
- (replace->generation8 == generation)
|
||||
- (tte->depth16 < replace->depth16) < 0)
|
||||
replace = tte;
|
||||
}
|
||||
|
||||
replace->save(key32, v, b, d, m, generation, statV);
|
||||
}
|
||||
|
||||
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,99 +17,76 @@
|
||||
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
|
||||
|
||||
#include "misc.h"
|
||||
#include "types.h"
|
||||
|
||||
/// The TTEntry is the class of transposition table entries
|
||||
/// The TTEntry is the 14 bytes 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-15: move
|
||||
/// bit 16-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
|
||||
/// eval value 16 bit
|
||||
|
||||
class TTEntry {
|
||||
struct TTEntry {
|
||||
|
||||
public:
|
||||
void save(uint32_t k, Value v, Bound b, Depth d, Move m, int g, Value statV, Value statM) {
|
||||
|
||||
key32 = (uint32_t)k;
|
||||
move16 = (uint16_t)m;
|
||||
bound = (uint8_t)b;
|
||||
generation8 = (uint8_t)g;
|
||||
value16 = (int16_t)v;
|
||||
depth16 = (int16_t)d;
|
||||
staticValue = (int16_t)statV;
|
||||
staticMargin = (int16_t)statM;
|
||||
}
|
||||
void set_generation(int g) { generation8 = (uint8_t)g; }
|
||||
|
||||
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 type() const { return (Bound)bound; }
|
||||
int generation() const { return (int)generation8; }
|
||||
Value static_value() const { return (Value)staticValue; }
|
||||
Value static_value_margin() const { return (Value)staticMargin; }
|
||||
Move move() const { return (Move )move16; }
|
||||
Bound bound() const { return (Bound)bound8; }
|
||||
Value value() const { return (Value)value16; }
|
||||
Depth depth() const { return (Depth)depth16; }
|
||||
Value eval_value() const { return (Value)evalValue; }
|
||||
|
||||
private:
|
||||
friend class TranspositionTable;
|
||||
|
||||
void save(uint32_t k, Value v, Bound b, Depth d, Move m, uint8_t g, Value ev) {
|
||||
|
||||
key32 = (uint32_t)k;
|
||||
move16 = (uint16_t)m;
|
||||
bound8 = (uint8_t)b;
|
||||
generation8 = (uint8_t)g;
|
||||
value16 = (int16_t)v;
|
||||
depth16 = (int16_t)d;
|
||||
evalValue = (int16_t)ev;
|
||||
}
|
||||
|
||||
uint32_t key32;
|
||||
uint16_t move16;
|
||||
uint8_t bound, generation8;
|
||||
int16_t value16, depth16, staticValue, staticMargin;
|
||||
uint8_t bound8, generation8;
|
||||
int16_t value16, depth16, evalValue;
|
||||
};
|
||||
|
||||
|
||||
/// 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 and reading entries.
|
||||
/// A TranspositionTable consists of a power of 2 number of clusters and each
|
||||
/// cluster consists of ClusterSize number of TTEntry. Each non-empty entry
|
||||
/// contains information of exactly one position. The size of a cluster should
|
||||
/// not be bigger than a cache line size. In case it is less, it should be padded
|
||||
/// to guarantee always aligned accesses.
|
||||
|
||||
class TranspositionTable {
|
||||
|
||||
TranspositionTable(const TranspositionTable&);
|
||||
TranspositionTable& operator=(const TranspositionTable&);
|
||||
static const unsigned ClusterSize = 4;
|
||||
|
||||
public:
|
||||
TranspositionTable();
|
||||
~TranspositionTable();
|
||||
void set_size(size_t mbSize);
|
||||
~TranspositionTable() { free(mem); }
|
||||
void new_search() { ++generation; }
|
||||
|
||||
const TTEntry* probe(const Key key) const;
|
||||
TTEntry* first_entry(const Key key) const;
|
||||
void resize(uint64_t mbSize);
|
||||
void clear();
|
||||
void store(const Key posKey, Value v, Bound type, Depth d, Move m, Value statV, Value kingD);
|
||||
TTEntry* probe(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);
|
||||
|
||||
private:
|
||||
size_t size;
|
||||
TTCluster* entries;
|
||||
uint8_t generation; // Size must be not bigger then TTEntry::generation8
|
||||
uint32_t hashMask;
|
||||
TTEntry* table;
|
||||
void* mem;
|
||||
uint8_t generation; // Size must be not bigger than TTEntry::generation8
|
||||
};
|
||||
|
||||
extern TranspositionTable TT;
|
||||
@@ -119,18 +96,9 @@ extern TranspositionTable TT;
|
||||
/// 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.
|
||||
|
||||
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
|
||||
|
||||
+168
-196
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +17,7 @@
|
||||
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
|
||||
|
||||
/// For Linux and OSX configuration is done automatically using Makefile. To get
|
||||
@@ -26,7 +26,7 @@
|
||||
/// For Windows, part of the configuration is detected automatically, but some
|
||||
/// switches need to be set manually:
|
||||
///
|
||||
/// -DNDEBUG | Disable debugging mode. Use always.
|
||||
/// -DNDEBUG | Disable debugging mode. Always use this.
|
||||
///
|
||||
/// -DNO_PREFETCH | Disable use of prefetch asm-instruction. A must if you want
|
||||
/// | the executable to run on some very old machines.
|
||||
@@ -35,13 +35,16 @@
|
||||
/// | only in 64-bit mode. For compiling requires hardware with
|
||||
/// | popcnt support.
|
||||
|
||||
#include <cassert>
|
||||
#include <cctype>
|
||||
#include <climits>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "platform.h"
|
||||
|
||||
#if defined(_WIN64)
|
||||
#define unlikely(x) (x) // For code annotation purposes
|
||||
|
||||
#if defined(_WIN64) && !defined(IS_64BIT)
|
||||
# include <intrin.h> // MSVC popcnt and bsfq instrinsics
|
||||
# define IS_64BIT
|
||||
# define USE_BSFQ
|
||||
@@ -51,13 +54,24 @@
|
||||
# include <nmmintrin.h> // Intel header for _mm_popcnt_u64() intrinsic
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
|
||||
# define CACHE_LINE_ALIGNMENT __declspec(align(64))
|
||||
#if defined(USE_PEXT)
|
||||
# include <immintrin.h> // Header for _pext_u64() intrinsic
|
||||
#else
|
||||
# define CACHE_LINE_ALIGNMENT __attribute__ ((aligned(64)))
|
||||
# define _pext_u64(b, m) (0)
|
||||
#endif
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
# if !defined(NO_PREFETCH) && (defined(__INTEL_COMPILER) || defined(_MSC_VER))
|
||||
# include <xmmintrin.h> // Intel and Microsoft header for _mm_prefetch()
|
||||
# endif
|
||||
|
||||
#define CACHE_LINE_SIZE 64
|
||||
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
|
||||
# define CACHE_LINE_ALIGNMENT __declspec(align(CACHE_LINE_SIZE))
|
||||
#else
|
||||
# define CACHE_LINE_ALIGNMENT __attribute__ ((aligned(CACHE_LINE_SIZE)))
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# define FORCE_INLINE __forceinline
|
||||
#elif defined(__GNUC__)
|
||||
# define FORCE_INLINE inline __attribute__((always_inline))
|
||||
@@ -65,13 +79,19 @@
|
||||
# define FORCE_INLINE inline
|
||||
#endif
|
||||
|
||||
#if defined(USE_POPCNT)
|
||||
#ifdef USE_POPCNT
|
||||
const bool HasPopCnt = true;
|
||||
#else
|
||||
const bool HasPopCnt = false;
|
||||
#endif
|
||||
|
||||
#if defined(IS_64BIT)
|
||||
#ifdef USE_PEXT
|
||||
const bool HasPext = true;
|
||||
#else
|
||||
const bool HasPext = false;
|
||||
#endif
|
||||
|
||||
#ifdef IS_64BIT
|
||||
const bool Is64Bit = true;
|
||||
#else
|
||||
const bool Is64Bit = false;
|
||||
@@ -80,87 +100,86 @@ const bool Is64Bit = false;
|
||||
typedef uint64_t Key;
|
||||
typedef uint64_t Bitboard;
|
||||
|
||||
const int MAX_MOVES = 192;
|
||||
const int MAX_PLY = 100;
|
||||
const int MAX_PLY_PLUS_2 = MAX_PLY + 2;
|
||||
|
||||
const Bitboard FileABB = 0x0101010101010101ULL;
|
||||
const Bitboard FileBBB = FileABB << 1;
|
||||
const Bitboard FileCBB = FileABB << 2;
|
||||
const Bitboard FileDBB = FileABB << 3;
|
||||
const Bitboard FileEBB = FileABB << 4;
|
||||
const Bitboard FileFBB = FileABB << 5;
|
||||
const Bitboard FileGBB = FileABB << 6;
|
||||
const Bitboard FileHBB = FileABB << 7;
|
||||
|
||||
const Bitboard Rank1BB = 0xFF;
|
||||
const Bitboard Rank2BB = Rank1BB << (8 * 1);
|
||||
const Bitboard Rank3BB = Rank1BB << (8 * 2);
|
||||
const Bitboard Rank4BB = Rank1BB << (8 * 3);
|
||||
const Bitboard Rank5BB = Rank1BB << (8 * 4);
|
||||
const Bitboard Rank6BB = Rank1BB << (8 * 5);
|
||||
const Bitboard Rank7BB = Rank1BB << (8 * 6);
|
||||
const Bitboard Rank8BB = Rank1BB << (8 * 7);
|
||||
|
||||
const int MAX_MOVES = 256;
|
||||
const int MAX_PLY = 120;
|
||||
const int MAX_PLY_PLUS_6 = MAX_PLY + 6;
|
||||
|
||||
/// 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)
|
||||
/// bit 14-15: special move flag: promotion (1), en passant (2), castling (3)
|
||||
/// NOTE: EN-PASSANT bit is set only when a pawn can be captured
|
||||
///
|
||||
/// 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.
|
||||
|
||||
enum Move {
|
||||
MOVE_NONE = 0,
|
||||
MOVE_NONE,
|
||||
MOVE_NULL = 65
|
||||
};
|
||||
|
||||
enum MoveType {
|
||||
NORMAL = 0,
|
||||
NORMAL,
|
||||
PROMOTION = 1 << 14,
|
||||
ENPASSANT = 2 << 14,
|
||||
CASTLE = 3 << 14
|
||||
CASTLING = 3 << 14
|
||||
};
|
||||
|
||||
enum CastleRight { // Defined as in PolyGlot book hash key
|
||||
CASTLES_NONE = 0,
|
||||
WHITE_OO = 1,
|
||||
WHITE_OOO = 2,
|
||||
BLACK_OO = 4,
|
||||
BLACK_OOO = 8,
|
||||
ALL_CASTLES = 15
|
||||
enum Color {
|
||||
WHITE, BLACK, NO_COLOR, COLOR_NB = 2
|
||||
};
|
||||
|
||||
enum CastlingSide {
|
||||
KING_SIDE,
|
||||
QUEEN_SIDE
|
||||
KING_SIDE, QUEEN_SIDE, CASTLING_SIDE_NB = 2
|
||||
};
|
||||
|
||||
enum CastlingRight { // Defined as in PolyGlot book hash key
|
||||
NO_CASTLING,
|
||||
WHITE_OO,
|
||||
WHITE_OOO = WHITE_OO << 1,
|
||||
BLACK_OO = WHITE_OO << 2,
|
||||
BLACK_OOO = WHITE_OO << 3,
|
||||
ANY_CASTLING = WHITE_OO | WHITE_OOO | BLACK_OO | BLACK_OOO,
|
||||
CASTLING_RIGHT_NB = 16
|
||||
};
|
||||
|
||||
template<Color C, CastlingSide S> struct MakeCastling {
|
||||
static const CastlingRight
|
||||
right = C == WHITE ? S == QUEEN_SIDE ? WHITE_OOO : WHITE_OO
|
||||
: S == QUEEN_SIDE ? BLACK_OOO : BLACK_OO;
|
||||
};
|
||||
|
||||
enum Phase {
|
||||
PHASE_ENDGAME,
|
||||
PHASE_MIDGAME = 128,
|
||||
MG = 0, EG = 1, PHASE_NB = 2
|
||||
};
|
||||
|
||||
enum ScaleFactor {
|
||||
SCALE_FACTOR_DRAW = 0,
|
||||
SCALE_FACTOR_NORMAL = 64,
|
||||
SCALE_FACTOR_MAX = 128,
|
||||
SCALE_FACTOR_NONE = 255
|
||||
SCALE_FACTOR_DRAW = 0,
|
||||
SCALE_FACTOR_ONEPAWN = 48,
|
||||
SCALE_FACTOR_NORMAL = 64,
|
||||
SCALE_FACTOR_MAX = 128,
|
||||
SCALE_FACTOR_NONE = 255
|
||||
};
|
||||
|
||||
enum Bound {
|
||||
BOUND_NONE = 0,
|
||||
BOUND_UPPER = 1,
|
||||
BOUND_LOWER = 2,
|
||||
BOUND_NONE,
|
||||
BOUND_UPPER,
|
||||
BOUND_LOWER,
|
||||
BOUND_EXACT = BOUND_UPPER | BOUND_LOWER
|
||||
};
|
||||
|
||||
enum Value {
|
||||
VALUE_ZERO = 0,
|
||||
VALUE_DRAW = 0,
|
||||
VALUE_KNOWN_WIN = 15000,
|
||||
VALUE_MATE = 30000,
|
||||
VALUE_INFINITE = 30001,
|
||||
VALUE_NONE = 30002,
|
||||
VALUE_KNOWN_WIN = 10000,
|
||||
VALUE_MATE = 32000,
|
||||
VALUE_INFINITE = 32001,
|
||||
VALUE_NONE = 32002,
|
||||
|
||||
VALUE_MATE_IN_MAX_PLY = VALUE_MATE - MAX_PLY,
|
||||
VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + MAX_PLY,
|
||||
@@ -168,28 +187,26 @@ enum Value {
|
||||
VALUE_ENSURE_INTEGER_SIZE_P = INT_MAX,
|
||||
VALUE_ENSURE_INTEGER_SIZE_N = INT_MIN,
|
||||
|
||||
Mg = 0, Eg = 1,
|
||||
|
||||
PawnValueMg = 198, PawnValueEg = 258,
|
||||
KnightValueMg = 817, KnightValueEg = 846,
|
||||
BishopValueMg = 836, BishopValueEg = 857,
|
||||
RookValueMg = 1270, RookValueEg = 1278,
|
||||
QueenValueMg = 2521, QueenValueEg = 2558
|
||||
QueenValueMg = 2521, QueenValueEg = 2558,
|
||||
|
||||
MidgameLimit = 15581, EndgameLimit = 3998
|
||||
};
|
||||
|
||||
enum PieceType {
|
||||
NO_PIECE_TYPE = 0, ALL_PIECES = 0,
|
||||
PAWN = 1, KNIGHT = 2, BISHOP = 3, ROOK = 4, QUEEN = 5, KING = 6
|
||||
NO_PIECE_TYPE, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING,
|
||||
ALL_PIECES = 0,
|
||||
PIECE_TYPE_NB = 8
|
||||
};
|
||||
|
||||
enum Piece {
|
||||
NO_PIECE = 16, // color_of(NO_PIECE) == NO_COLOR
|
||||
W_PAWN = 1, W_KNIGHT = 2, W_BISHOP = 3, W_ROOK = 4, W_QUEEN = 5, W_KING = 6,
|
||||
B_PAWN = 9, B_KNIGHT = 10, B_BISHOP = 11, B_ROOK = 12, B_QUEEN = 13, B_KING = 14
|
||||
};
|
||||
|
||||
enum Color {
|
||||
WHITE, BLACK, NO_COLOR
|
||||
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 Depth {
|
||||
@@ -197,8 +214,8 @@ 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_CHECKS = 0 * ONE_PLY,
|
||||
DEPTH_QS_NO_CHECKS = -1 * ONE_PLY,
|
||||
DEPTH_QS_RECAPTURES = -5 * ONE_PLY,
|
||||
|
||||
DEPTH_NONE = -127 * ONE_PLY
|
||||
@@ -215,6 +232,8 @@ enum Square {
|
||||
SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
|
||||
SQ_NONE,
|
||||
|
||||
SQUARE_NB = 64,
|
||||
|
||||
DELTA_N = 8,
|
||||
DELTA_E = 1,
|
||||
DELTA_S = -8,
|
||||
@@ -229,76 +248,85 @@ enum Square {
|
||||
};
|
||||
|
||||
enum File {
|
||||
FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H
|
||||
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_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.
|
||||
/// The Score enum stores a middlegame and an endgame value in a single integer
|
||||
/// (enum). The least significant 16 bits are used to store the endgame value
|
||||
/// and the upper 16 bits are used to store the middlegame value. The compiler
|
||||
/// is free to choose the enum type as long as it can store the data, so we
|
||||
/// ensure that Score is an integer type by assigning some big int values.
|
||||
enum Score {
|
||||
SCORE_ZERO = 0,
|
||||
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); }
|
||||
typedef union {
|
||||
uint32_t full;
|
||||
struct { int16_t eg, mg; } half;
|
||||
} ScoreView;
|
||||
|
||||
/// 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 + 32768) & ~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));
|
||||
inline Score make_score(int mg, int eg) {
|
||||
ScoreView v;
|
||||
v.half.mg = (int16_t)(mg - (uint16_t(eg) >> 15));
|
||||
v.half.eg = (int16_t)eg;
|
||||
return Score(v.full);
|
||||
}
|
||||
|
||||
#endif
|
||||
inline Value mg_value(Score s) {
|
||||
ScoreView v;
|
||||
v.full = s;
|
||||
return Value(v.half.mg + (uint16_t(v.half.eg) >> 15));
|
||||
}
|
||||
|
||||
#define ENABLE_SAFE_OPERATORS_ON(T) \
|
||||
inline Value eg_value(Score s) {
|
||||
ScoreView v;
|
||||
v.full = s;
|
||||
return Value(v.half.eg);
|
||||
}
|
||||
|
||||
#define ENABLE_BASE_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; }
|
||||
inline T& operator+=(T& d1, const T d2) { return d1 = d1 + d2; } \
|
||||
inline T& operator-=(T& d1, const T d2) { return d1 = d1 - d2; } \
|
||||
inline T& operator*=(T& d, int i) { return d = T(int(d) * i); }
|
||||
|
||||
#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; } \
|
||||
ENABLE_BASE_OPERATORS_ON(Score)
|
||||
|
||||
#define ENABLE_FULL_OPERATORS_ON(T) \
|
||||
ENABLE_BASE_OPERATORS_ON(T) \
|
||||
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
|
||||
inline T& operator--(T& d) { return d = T(int(d) - 1); } \
|
||||
inline T operator/(const T d, int i) { return T(int(d) / i); } \
|
||||
inline T& operator/=(T& d, int i) { d = T(int(d) / i); return d; }
|
||||
inline T& operator/=(T& d, int i) { return d = T(int(d) / i); }
|
||||
|
||||
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)
|
||||
ENABLE_FULL_OPERATORS_ON(Value)
|
||||
ENABLE_FULL_OPERATORS_ON(PieceType)
|
||||
ENABLE_FULL_OPERATORS_ON(Piece)
|
||||
ENABLE_FULL_OPERATORS_ON(Color)
|
||||
ENABLE_FULL_OPERATORS_ON(Depth)
|
||||
ENABLE_FULL_OPERATORS_ON(Square)
|
||||
ENABLE_FULL_OPERATORS_ON(File)
|
||||
ENABLE_FULL_OPERATORS_ON(Rank)
|
||||
|
||||
/// Added operators for adding integers to a Value
|
||||
#undef ENABLE_FULL_OPERATORS_ON
|
||||
#undef ENABLE_BASE_OPERATORS_ON
|
||||
|
||||
/// Additional operators to add integers to a Value
|
||||
inline Value operator+(Value v, int i) { return Value(int(v) + i); }
|
||||
inline Value operator-(Value v, int i) { return Value(int(v) - i); }
|
||||
|
||||
ENABLE_SAFE_OPERATORS_ON(Score)
|
||||
inline Value& operator+=(Value& v, int i) { return v = v + i; }
|
||||
inline Value& operator-=(Value& v, int i) { return v = v - 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.
|
||||
@@ -309,51 +337,27 @@ inline Score operator/(Score s, int i) {
|
||||
return make_score(mg_value(s) / i, eg_value(s) / i);
|
||||
}
|
||||
|
||||
/// Weight score v by score w trying to prevent overflow
|
||||
inline Score apply_weight(Score v, Score w) {
|
||||
return make_score((int(mg_value(v)) * mg_value(w)) / 0x100,
|
||||
(int(eg_value(v)) * eg_value(w)) / 0x100);
|
||||
}
|
||||
extern Value PieceValue[PHASE_NB][PIECE_NB];
|
||||
|
||||
#undef ENABLE_OPERATORS_ON
|
||||
#undef ENABLE_SAFE_OPERATORS_ON
|
||||
|
||||
namespace Zobrist {
|
||||
|
||||
extern Key psq[2][8][64]; // [color][pieceType][square / piece count]
|
||||
extern Key enpassant[8]; // [file]
|
||||
extern Key castle[16]; // [castleRight]
|
||||
extern Key side;
|
||||
extern Key exclusion;
|
||||
|
||||
void init();
|
||||
}
|
||||
|
||||
CACHE_LINE_ALIGNMENT
|
||||
|
||||
extern Score pieceSquareTable[16][64]; // [piece][square]
|
||||
extern Value PieceValue[2][18]; // [Mg / Eg][piece / pieceType]
|
||||
extern int SquareDistance[64][64]; // [square][square]
|
||||
|
||||
struct MoveStack {
|
||||
struct ExtMove {
|
||||
Move move;
|
||||
int score;
|
||||
Value value;
|
||||
};
|
||||
|
||||
inline bool operator<(const MoveStack& f, const MoveStack& s) {
|
||||
return f.score < s.score;
|
||||
inline bool operator<(const ExtMove& f, const ExtMove& s) {
|
||||
return f.value < s.value;
|
||||
}
|
||||
|
||||
inline Color operator~(Color c) {
|
||||
return Color(c ^ 1);
|
||||
return Color(c ^ BLACK);
|
||||
}
|
||||
|
||||
inline Square operator~(Square s) {
|
||||
return Square(s ^ 56); // Vertical flip SQ_A1 -> SQ_A8
|
||||
return Square(s ^ SQ_A8); // Vertical flip SQ_A1 -> SQ_A8
|
||||
}
|
||||
|
||||
inline Square operator|(File f, Rank r) {
|
||||
return Square((r << 3) | f);
|
||||
inline CastlingRight operator|(Color c, CastlingSide s) {
|
||||
return CastlingRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
|
||||
}
|
||||
|
||||
inline Value mate_in(int ply) {
|
||||
@@ -364,20 +368,21 @@ inline Value mated_in(int ply) {
|
||||
return -VALUE_MATE + ply;
|
||||
}
|
||||
|
||||
inline Square make_square(File f, Rank r) {
|
||||
return Square((r << 3) | f);
|
||||
}
|
||||
|
||||
inline Piece make_piece(Color c, PieceType pt) {
|
||||
return Piece((c << 3) | pt);
|
||||
}
|
||||
|
||||
inline CastleRight make_castle_right(Color c, CastlingSide s) {
|
||||
return CastleRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
|
||||
inline PieceType type_of(Piece pc) {
|
||||
return PieceType(pc & 7);
|
||||
}
|
||||
|
||||
inline PieceType type_of(Piece p) {
|
||||
return PieceType(p & 7);
|
||||
}
|
||||
|
||||
inline Color color_of(Piece p) {
|
||||
return Color(p >> 3);
|
||||
inline Color color_of(Piece pc) {
|
||||
assert(pc != NO_PIECE);
|
||||
return Color(pc >> 3);
|
||||
}
|
||||
|
||||
inline bool is_ok(Square s) {
|
||||
@@ -392,10 +397,6 @@ 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));
|
||||
}
|
||||
@@ -413,24 +414,12 @@ inline bool opposite_colors(Square s1, Square s2) {
|
||||
return ((s >> 3) ^ s) & 1;
|
||||
}
|
||||
|
||||
inline int file_distance(Square s1, Square s2) {
|
||||
return abs(file_of(s1) - file_of(s2));
|
||||
inline char to_char(File f, bool tolower = true) {
|
||||
return char(f - FILE_A + (tolower ? 'a' : 'A'));
|
||||
}
|
||||
|
||||
inline int rank_distance(Square s1, Square s2) {
|
||||
return abs(rank_of(s1) - rank_of(s2));
|
||||
}
|
||||
|
||||
inline int square_distance(Square s1, Square s2) {
|
||||
return SquareDistance[s1][s2];
|
||||
}
|
||||
|
||||
inline char file_to_char(File f) {
|
||||
return char(f - FILE_A + int('a'));
|
||||
}
|
||||
|
||||
inline char rank_to_char(Rank r) {
|
||||
return char(r - RANK_1 + int('1'));
|
||||
inline char to_char(Rank r) {
|
||||
return char(r - RANK_1 + '1');
|
||||
}
|
||||
|
||||
inline Square pawn_push(Color c) {
|
||||
@@ -459,7 +448,7 @@ inline Move make_move(Square from, Square to) {
|
||||
|
||||
template<MoveType T>
|
||||
inline Move make(Square from, Square to, PieceType pt = KNIGHT) {
|
||||
return Move(to | (from << 6) | T | ((pt - KNIGHT) << 12)) ;
|
||||
return Move(to | (from << 6) | T | ((pt - KNIGHT) << 12));
|
||||
}
|
||||
|
||||
inline bool is_ok(Move m) {
|
||||
@@ -468,26 +457,9 @@ inline bool is_ok(Move m) {
|
||||
|
||||
#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 };
|
||||
inline const std::string to_string(Square s) {
|
||||
char ch[] = { to_char(file_of(s)), to_char(rank_of(s)), 0 };
|
||||
return ch;
|
||||
}
|
||||
|
||||
/// Our insertion sort implementation, works with pointers and iterators and is
|
||||
/// guaranteed to be stable, as is needed.
|
||||
template<typename T, typename K>
|
||||
void sort(K first, K last)
|
||||
{
|
||||
T tmp;
|
||||
K p, q;
|
||||
|
||||
for (p = first + 1; p < last; p++)
|
||||
{
|
||||
tmp = *p;
|
||||
for (q = p; q != first && *(q-1) < tmp; --q)
|
||||
*q = *(q-1);
|
||||
*q = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // !defined(TYPES_H_INCLUDED)
|
||||
#endif // #ifndef TYPES_H_INCLUDED
|
||||
|
||||
+111
-143
@@ -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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,6 +17,7 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
@@ -26,6 +27,7 @@
|
||||
#include "position.h"
|
||||
#include "search.h"
|
||||
#include "thread.h"
|
||||
#include "tt.h"
|
||||
#include "ucioption.h"
|
||||
|
||||
using namespace std;
|
||||
@@ -37,125 +39,18 @@ namespace {
|
||||
// FEN string of the initial position, normal chess
|
||||
const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
|
||||
// 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.
|
||||
// Keep a track of the position keys along the setup moves (from the start position
|
||||
// to the position just before the search starts). This is needed by the repetition
|
||||
// draw detection code.
|
||||
Search::StateStackPtr SetupStates;
|
||||
|
||||
void set_option(istringstream& up);
|
||||
void set_position(Position& pos, istringstream& up);
|
||||
void go(Position& pos, istringstream& up);
|
||||
}
|
||||
|
||||
|
||||
/// 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.
|
||||
|
||||
void UCI::loop(const string& args) {
|
||||
|
||||
Position pos(StartFEN, false, Threads.main_thread()); // The root position
|
||||
string cmd, token;
|
||||
|
||||
while (token != "quit")
|
||||
{
|
||||
if (!args.empty())
|
||||
cmd = args;
|
||||
|
||||
else if (!getline(cin, cmd)) // Block here waiting for input
|
||||
cmd = "quit";
|
||||
|
||||
istringstream is(cmd);
|
||||
|
||||
is >> skipws >> token;
|
||||
|
||||
if (token == "quit" || token == "stop")
|
||||
{
|
||||
Search::Signals.stop = true;
|
||||
Threads.wait_for_search_finished(); // Cannot quit while threads are running
|
||||
}
|
||||
|
||||
else if (token == "ponderhit")
|
||||
{
|
||||
// The opponent has played the expected move. GUI sends "ponderhit" if
|
||||
// we were told to ponder on the same move the opponent has played. We
|
||||
// should continue searching but switching from pondering to normal search.
|
||||
Search::Limits.ponder = false;
|
||||
|
||||
if (Search::Signals.stopOnPonderhit)
|
||||
{
|
||||
Search::Signals.stop = true;
|
||||
Threads.main_thread()->wake_up(); // Could be sleeping
|
||||
}
|
||||
}
|
||||
|
||||
else if (token == "go")
|
||||
go(pos, is);
|
||||
|
||||
else if (token == "ucinewgame")
|
||||
{ /* Avoid returning "Unknown command" */ }
|
||||
|
||||
else if (token == "isready")
|
||||
sync_cout << "readyok" << sync_endl;
|
||||
|
||||
else if (token == "position")
|
||||
set_position(pos, is);
|
||||
|
||||
else if (token == "setoption")
|
||||
set_option(is);
|
||||
|
||||
else if (token == "d")
|
||||
pos.print();
|
||||
|
||||
else if (token == "flip")
|
||||
pos.flip();
|
||||
|
||||
else if (token == "eval")
|
||||
sync_cout << Eval::trace(pos) << sync_endl;
|
||||
|
||||
else if (token == "bench")
|
||||
benchmark(pos, is);
|
||||
|
||||
else if (token == "key")
|
||||
sync_cout << "key: " << hex << pos.key()
|
||||
<< "\nmaterial key: " << pos.material_key()
|
||||
<< "\npawn key: " << pos.pawn_key() << sync_endl;
|
||||
|
||||
else if (token == "uci")
|
||||
sync_cout << "id name " << engine_info(true)
|
||||
<< "\n" << Options
|
||||
<< "\nuciok" << sync_endl;
|
||||
|
||||
else if (token == "perft" && (is >> token)) // Read depth
|
||||
{
|
||||
stringstream ss;
|
||||
|
||||
ss << Options["Hash"] << " "
|
||||
<< Options["Threads"] << " " << token << " current perft";
|
||||
|
||||
benchmark(pos, ss);
|
||||
}
|
||||
|
||||
else
|
||||
sync_cout << "Unknown command: " << cmd << sync_endl;
|
||||
|
||||
if (!args.empty()) // Command line arguments have one-shot behaviour
|
||||
{
|
||||
Threads.wait_for_search_finished();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
// set_position() is called when engine receives the "position" UCI command.
|
||||
// The function sets up the position described in the given fen string ("fen")
|
||||
// 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, istringstream& is) {
|
||||
void position(Position& pos, istringstream& is) {
|
||||
|
||||
Move m;
|
||||
string token, fen;
|
||||
@@ -173,7 +68,7 @@ namespace {
|
||||
else
|
||||
return;
|
||||
|
||||
pos.from_fen(fen, Options["UCI_Chess960"], Threads.main_thread());
|
||||
pos.set(fen, Options["UCI_Chess960"], Threads.main());
|
||||
SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
|
||||
|
||||
// Parse move list (if any)
|
||||
@@ -185,10 +80,10 @@ namespace {
|
||||
}
|
||||
|
||||
|
||||
// set_option() is called when engine receives the "setoption" UCI command. The
|
||||
// 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(istringstream& is) {
|
||||
void setoption(istringstream& is) {
|
||||
|
||||
string token, name, value;
|
||||
|
||||
@@ -210,42 +105,115 @@ namespace {
|
||||
|
||||
|
||||
// go() is called when engine receives the "go" UCI command. The function sets
|
||||
// the thinking time and other parameters from the input string, and then starts
|
||||
// the thinking time and other parameters from the input string, and starts
|
||||
// the search.
|
||||
|
||||
void go(Position& pos, istringstream& is) {
|
||||
void go(const Position& pos, istringstream& is) {
|
||||
|
||||
Search::LimitsType limits;
|
||||
vector<Move> searchMoves;
|
||||
string token;
|
||||
|
||||
while (is >> token)
|
||||
{
|
||||
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 == "infinite")
|
||||
limits.infinite = true;
|
||||
else if (token == "ponder")
|
||||
limits.ponder = true;
|
||||
else if (token == "searchmoves")
|
||||
if (token == "searchmoves")
|
||||
while (is >> token)
|
||||
searchMoves.push_back(move_from_uci(pos, token));
|
||||
limits.searchmoves.push_back(move_from_uci(pos, token));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Threads.start_searching(pos, limits, searchMoves, SetupStates);
|
||||
Threads.start_thinking(pos, limits, SetupStates);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
/// 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.
|
||||
|
||||
void UCI::loop(int argc, char* argv[]) {
|
||||
|
||||
Position pos(StartFEN, false, Threads.main()); // The root position
|
||||
string token, cmd;
|
||||
|
||||
for (int i = 1; i < argc; ++i)
|
||||
cmd += std::string(argv[i]) + " ";
|
||||
|
||||
do {
|
||||
if (argc == 1 && !getline(cin, cmd)) // Block here waiting for input
|
||||
cmd = "quit";
|
||||
|
||||
istringstream is(cmd);
|
||||
|
||||
is >> skipws >> token;
|
||||
|
||||
if (token == "quit" || token == "stop" || token == "ponderhit")
|
||||
{
|
||||
// The GUI sends 'ponderhit' to tell us to ponder on the same move the
|
||||
// opponent has played. In case Signals.stopOnPonderhit is set we are
|
||||
// waiting for 'ponderhit' to stop the search (for instance because we
|
||||
// already ran out of time), otherwise we should continue searching but
|
||||
// switch from pondering to normal search.
|
||||
if (token != "ponderhit" || Search::Signals.stopOnPonderhit)
|
||||
{
|
||||
Search::Signals.stop = true;
|
||||
Threads.main()->notify_one(); // Could be sleeping
|
||||
}
|
||||
else
|
||||
Search::Limits.ponder = false;
|
||||
}
|
||||
else if (token == "perft" || token == "divide")
|
||||
{
|
||||
int depth;
|
||||
stringstream ss;
|
||||
|
||||
is >> depth;
|
||||
ss << Options["Hash"] << " "
|
||||
<< Options["Threads"] << " " << depth << " current " << token;
|
||||
|
||||
benchmark(pos, ss);
|
||||
}
|
||||
else if (token == "key")
|
||||
sync_cout << hex << uppercase << setfill('0')
|
||||
<< "position key: " << setw(16) << pos.key()
|
||||
<< "\nmaterial key: " << setw(16) << pos.material_key()
|
||||
<< "\npawn key: " << setw(16) << pos.pawn_key()
|
||||
<< dec << nouppercase << setfill(' ') << sync_endl;
|
||||
|
||||
else if (token == "uci")
|
||||
sync_cout << "id name " << engine_info(true)
|
||||
<< "\n" << Options
|
||||
<< "\nuciok" << sync_endl;
|
||||
|
||||
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;
|
||||
|
||||
} while (token != "quit" && argc == 1); // Passed args have one-shot behaviour
|
||||
|
||||
Threads.wait_for_think_finished(); // Cannot quit whilst the search is running
|
||||
}
|
||||
|
||||
+50
-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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -38,7 +38,7 @@ namespace UCI {
|
||||
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_hash_size(const Option& o) { TT.resize(o); }
|
||||
void on_clear_hash(const Option&) { TT.clear(); }
|
||||
|
||||
|
||||
@@ -50,44 +50,39 @@ bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const
|
||||
}
|
||||
|
||||
|
||||
/// init() initializes the UCI options to their hard coded default values
|
||||
/// and initializes the default value of "Threads" and "Min Split Depth"
|
||||
/// parameters according to the number of CPU cores detected.
|
||||
/// init() initializes the UCI options to their hard-coded default values
|
||||
|
||||
void init(OptionsMap& o) {
|
||||
|
||||
int cpus = std::min(cpu_count(), MAX_THREADS);
|
||||
int msd = cpus < 8 ? 4 : 7;
|
||||
|
||||
o["Use Debug Log"] = Option(false, on_logger);
|
||||
o["Use Search Log"] = Option(false);
|
||||
o["Search Log Filename"] = Option("SearchLog.txt");
|
||||
o["Book File"] = Option("book.bin");
|
||||
o["Best Book Move"] = Option(false);
|
||||
o["Mobility (Middle Game)"] = Option(100, 0, 200, on_eval);
|
||||
o["Mobility (Endgame)"] = Option(100, 0, 200, on_eval);
|
||||
o["Passed Pawns (Middle Game)"] = 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(msd, 4, 7, on_threads);
|
||||
o["Max Threads per Split Point"] = Option(5, 4, 8, on_threads);
|
||||
o["Threads"] = Option(cpus, 1, MAX_THREADS, on_threads);
|
||||
o["Use Sleeping Threads"] = Option(false, on_threads);
|
||||
o["Hash"] = Option(32, 4, 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);
|
||||
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["Threads"] << Option(1, 1, MAX_THREADS, on_threads);
|
||||
o["Hash"] << Option(32, 1, 16384, 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(60, 0, 30000);
|
||||
o["Emergency Move Time"] << Option(30, 0, 5000);
|
||||
o["Minimum Thinking Time"] << Option(20, 0, 5000);
|
||||
o["Slow Mover"] << Option(80, 10, 1000);
|
||||
o["UCI_Chess960"] << Option(false);
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +91,7 @@ void init(OptionsMap& o) {
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
|
||||
|
||||
for (size_t idx = 0; idx < om.size(); idx++)
|
||||
for (size_t idx = 0; idx < om.size(); ++idx)
|
||||
for (OptionsMap::const_iterator it = om.begin(); it != om.end(); ++it)
|
||||
if (it->second.idx == idx)
|
||||
{
|
||||
@@ -115,18 +110,18 @@ std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
|
||||
}
|
||||
|
||||
|
||||
/// Option c'tors and conversion operators
|
||||
/// Option class constructors and conversion operators
|
||||
|
||||
Option::Option(const char* v, Fn* f) : type("string"), min(0), max(0), idx(Options.size()), on_change(f)
|
||||
Option::Option(const char* v, OnChange f) : type("string"), min(0), max(0), on_change(f)
|
||||
{ defaultValue = currentValue = v; }
|
||||
|
||||
Option::Option(bool v, Fn* f) : type("check"), min(0), max(0), idx(Options.size()), on_change(f)
|
||||
Option::Option(bool v, OnChange f) : type("check"), min(0), max(0), on_change(f)
|
||||
{ defaultValue = currentValue = (v ? "true" : "false"); }
|
||||
|
||||
Option::Option(Fn* f) : type("button"), min(0), max(0), idx(Options.size()), on_change(f)
|
||||
Option::Option(OnChange f) : type("button"), min(0), max(0), 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)
|
||||
Option::Option(int v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f)
|
||||
{ std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); }
|
||||
|
||||
|
||||
@@ -141,6 +136,17 @@ Option::operator std::string() const {
|
||||
}
|
||||
|
||||
|
||||
/// operator<<() inits options and assigns idx in the correct printing order
|
||||
|
||||
void Option::operator<<(const Option& o) {
|
||||
|
||||
static size_t insert_order = 0;
|
||||
|
||||
*this = o;
|
||||
idx = insert_order++;
|
||||
}
|
||||
|
||||
|
||||
/// 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.
|
||||
@@ -158,7 +164,7 @@ Option& Option::operator=(const string& v) {
|
||||
currentValue = v;
|
||||
|
||||
if (on_change)
|
||||
(*on_change)(*this);
|
||||
on_change(*this);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
+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-2012 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
||||
|
||||
Stockfish is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -17,7 +17,7 @@
|
||||
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 <map>
|
||||
@@ -38,15 +38,16 @@ typedef std::map<std::string, Option, CaseInsensitiveLess> OptionsMap;
|
||||
/// Option class implements an option as defined by UCI protocol
|
||||
class Option {
|
||||
|
||||
typedef void (Fn)(const Option&);
|
||||
typedef void (*OnChange)(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(OnChange = NULL);
|
||||
Option(bool v, OnChange = NULL);
|
||||
Option(const char* v, OnChange = NULL);
|
||||
Option(int v, int min, int max, OnChange = NULL);
|
||||
|
||||
Option& operator=(const std::string& v);
|
||||
void operator<<(const Option& o);
|
||||
operator int() const;
|
||||
operator std::string() const;
|
||||
|
||||
@@ -56,14 +57,14 @@ private:
|
||||
std::string defaultValue, currentValue, type;
|
||||
int min, max;
|
||||
size_t idx;
|
||||
Fn* on_change;
|
||||
OnChange on_change;
|
||||
};
|
||||
|
||||
void init(OptionsMap&);
|
||||
void loop(const std::string&);
|
||||
void loop(int argc, char* argv[]);
|
||||
|
||||
} // namespace UCI
|
||||
|
||||
extern UCI::OptionsMap Options;
|
||||
|
||||
#endif // !defined(UCIOPTION_H_INCLUDED)
|
||||
#endif // #ifndef UCIOPTION_H_INCLUDED
|
||||
|
||||
Reference in New Issue
Block a user