Compare commits
341 Commits
b855ba59aa
...
search
| Author | SHA1 | Date | |
|---|---|---|---|
| b7a5d0fe56 | |||
| 87555f5405 | |||
| bb866cb933 | |||
| 7ae6604e10 | |||
| fcd54b44ce | |||
| aef14db3e4 | |||
| f7a6e582ed | |||
| f7fd2cb657 | |||
| 1ca4eb4443 | |||
| 0ff41c408b | |||
| e1debcc3ae | |||
| 01af1f5c49 | |||
| 6ea77c85e1 | |||
| 3fb0c92653 | |||
| a6a21626c7 | |||
| 2cd1289cd6 | |||
| d5920886a3 | |||
| 4733a1d65f | |||
| c93ed5ba01 | |||
| e5a9303d08 | |||
| beaa68cb10 | |||
| 99658316fb | |||
| 448057bd27 | |||
| 4a73de8dd8 | |||
| 3bbc9ea7b8 | |||
| db00014eef | |||
| 98c54553d0 | |||
| a4c413bea3 | |||
| e630a73f26 | |||
| c8fd3b87ac | |||
| 14a2cae072 | |||
| 30ac647fe5 | |||
| ca76b28b00 | |||
| f8bb5c06e5 | |||
| f52454903c | |||
| 5401e83db8 | |||
| 3100504fa2 | |||
| 470109768f | |||
| 96be21231b | |||
| 7ce64c3aee | |||
| 23051cb427 | |||
| 8483ffa101 | |||
| a8f7bd12b8 | |||
| 73d09ec37f | |||
| 2870101896 | |||
| 91abc3e26d | |||
| 902c224aa9 | |||
| 44f604abac | |||
| a2451d79db | |||
| 58c985f52f | |||
| 4d1870ffb3 | |||
| e18a237347 | |||
| 6fc1ebcc9a | |||
| 8c94c6beb1 | |||
| a44483e36c | |||
| 4e6885f26f | |||
| bb13eae0b8 | |||
| b8f0f6a120 | |||
| b4f008b223 | |||
| 46f42ae59b | |||
| cffb0f7b95 | |||
| c5a1936e3b | |||
| 46aed01079 | |||
| ffd5d056cc | |||
| 5cb90f5396 | |||
| da489bad65 | |||
| f4280dfa13 | |||
| d1cc7a8066 | |||
| cfa8b42077 | |||
| 0c2d30c938 | |||
| 3a6c1d11c0 | |||
| a7311a546f | |||
| 84b6c41107 | |||
| 19d10fdfa8 | |||
| 6e38de58cb | |||
| 0c15be28b1 | |||
| 879bda850c | |||
| 0a0c3227b8 | |||
| 242b501404 | |||
| f530a13481 | |||
| 243805f11f | |||
| ae198c891f | |||
| f1657e9806 | |||
| 840202dc0e | |||
| dfecad9ea6 | |||
| e61e1518f4 | |||
| 2fbad1319e | |||
| 148fef20ea | |||
| 8be03c6230 | |||
| ec2d2291d4 | |||
| 49b678e3ce | |||
| ebf01bc7db | |||
| 660d181e41 | |||
| 5a2cdfca56 | |||
| 00fc61020c | |||
| 06904f8a77 | |||
| 431baa43d4 | |||
| cee262e31d | |||
| ec64e2e44d | |||
| 972046351b | |||
| a7495b67d2 | |||
| 8703c892af | |||
| 1670b21dfa | |||
| 07a545adae | |||
| 77695574aa | |||
| 6da0f80d46 | |||
| f657f8498a | |||
| 4a0c734eba | |||
| 239498bc2a | |||
| a012af30fc | |||
| d1cb1f3c2c | |||
| 2069d6073d | |||
| 5db45a760a | |||
| 86f8184c56 | |||
| 022daf0a89 | |||
| 9ef262af48 | |||
| 038f6b5669 | |||
| 7baf66f1b6 | |||
| a0b1d289a7 | |||
| fec1dc68b5 | |||
| 32ade1777f | |||
| c710da4bf9 | |||
| 36e1d987f3 | |||
| a13bdb04f1 | |||
| b884c1d65c | |||
| 2505217c70 | |||
| 8b3202fac0 | |||
| 473cc8683e | |||
| f0acdb6a66 | |||
| a49c712471 | |||
| 2b72fac45e | |||
| 0330696f87 | |||
| a6eedebc19 | |||
| ab31274d17 | |||
| 03da11cc9c | |||
| bfed0f417d | |||
| 926dfa0765 | |||
| 374116b1e7 | |||
| 9b5c2253b1 | |||
| e301e6c726 | |||
| e78eae21e6 | |||
| 711306c92a | |||
| 660722fadc | |||
| e8240c6cab | |||
| eb590f1438 | |||
| f2ce20a504 | |||
| 027aa2c132 | |||
| 30af886594 | |||
| afecbeb955 | |||
| 05748e19ab | |||
| 4816b4a53a | |||
| 96744cea20 | |||
| 24207583d1 | |||
| 92d6909546 | |||
| 85ae4a2230 | |||
| ad8a9609ce | |||
| ad704c216b | |||
| 26b9a5b58a | |||
| 65fe74c9c5 | |||
| 9b2f5ff751 | |||
| edcc87be5a | |||
| 09afd98971 | |||
| 08ba989170 | |||
| 7637bdad10 | |||
| 70d6c23c00 | |||
| aaeab03089 | |||
| ce2e0e8459 | |||
| d0279125ae | |||
| 798047b84d | |||
| 856e3e52da | |||
| 51a348c1e4 | |||
| 51c35e21f4 | |||
| 3a06407d5a | |||
| ae6328ce26 | |||
| 08064dd1a1 | |||
| 49705bc707 | |||
| 8527c3dee1 | |||
| dc916f5c56 | |||
| 748953a767 | |||
| 9c2e76442d | |||
| 49302c7a60 | |||
| db38b507ff | |||
| 9d40a53aea | |||
| 260d8d34bd | |||
| 037d49e4ca | |||
| 4eb620a873 | |||
| 92dcb1e778 | |||
| c8aea61529 | |||
| b3f0dd0534 | |||
| 0f06ccb8db | |||
| 3e477f7442 | |||
| aba0113344 | |||
| cc7d91ebfb | |||
| 17d1dc52f9 | |||
| cc3754ae00 | |||
| 08082faed3 | |||
| 6ee4cd1642 | |||
| b7fdcca66d | |||
| 890cb05296 | |||
| 301ca24783 | |||
| d81dca6e23 | |||
| 87e7695873 | |||
| b351d198b8 | |||
| a499893f32 | |||
| dc6ceb3407 | |||
| beaa7bfcbc | |||
| be32bae86a | |||
| 8f0840fc2f | |||
| 6d412e7fce | |||
| 7859228f89 | |||
| a8e3ec70f8 | |||
| f1a081a7b6 | |||
| ca4e274957 | |||
| e50d9a73e6 | |||
| ccc0dfd2f6 | |||
| fa5c9bb8ab | |||
| d00eab54e7 | |||
| 9c02a02c1e | |||
| 4d8f69e8c9 | |||
| 568b39e366 | |||
| 403e625cbe | |||
| bc28a900be | |||
| c73c448c0b | |||
| 3e828ed29b | |||
| d5906b1fb9 | |||
| 4f25c1416d | |||
| 95cc25a2c2 | |||
| d6f2497bb0 | |||
| 1929d4bb1f | |||
| c57e0463cd | |||
| 33cd40b37f | |||
| 797974f394 | |||
| 32fac4a0ba | |||
| 56c9dde6fb | |||
| 0b6bdd6eb2 | |||
| 10c29ab45d | |||
| c44fc214af | |||
| 13bbf45fc5 | |||
| d2878f70ab | |||
| e2ec09ba65 | |||
| beaeadedd2 | |||
| 3798dbad4b | |||
| c555bc2e72 | |||
| 5b536df4b9 | |||
| 7653ff798f | |||
| 276589364b | |||
| 06f842a5c3 | |||
| c9368a3ada | |||
| b56f5b176b | |||
| e0eccfbafd | |||
| bbb5840e39 | |||
| c4eb134e92 | |||
| 624055eb26 | |||
| 163f4fedca | |||
| 52ec4198c1 | |||
| 5841baf619 | |||
| aff0e5abf4 | |||
| 138b76d055 | |||
| 1b3ac52f52 | |||
| 603aba8043 | |||
| 02c2667858 | |||
| 589755331f | |||
| fbca2fb839 | |||
| 7f972c82a7 | |||
| 0ebecaad41 | |||
| a8565e46cd | |||
| f7fe395a97 | |||
| c7a2d9b330 | |||
| c1aa9a0a0a | |||
| 398f40ecdf | |||
| e709a2c3c6 | |||
| cefbfcbc25 | |||
| da1aff03b3 | |||
| 3e4855ef4d | |||
| bbde461b4b | |||
| 53b612358a | |||
| 04ad280beb | |||
| 160851af18 | |||
| 4c26aa09d9 | |||
| b1027330aa | |||
| 4240646969 | |||
| 49f834573e | |||
| 733c619594 | |||
| aebb798b31 | |||
| 17bbe6379d | |||
| bdd88e8511 | |||
| 18c294a1f1 | |||
| 051dbc3348 | |||
| 3b01841877 | |||
| 5b1d99650d | |||
| ee7701d8ef | |||
| eb362f32bb | |||
| 8140881359 | |||
| be5c4e5c2a | |||
| 917d1852b7 | |||
| f52c50c817 | |||
| 860aa6fc72 | |||
| 7b18c11cd4 | |||
| 83c9444c59 | |||
| 15b004651f | |||
| 1e4760687b | |||
| 636f1f555a | |||
| 8dbae2ceef | |||
| 56543095ac | |||
| 0fc415d900 | |||
| 26c3b04d17 | |||
| 7e870ee269 | |||
| 15ad675838 | |||
| 6acacb0314 | |||
| 033d95ce26 | |||
| e49e9b8f9d | |||
| f0d6f57dd1 | |||
| e2c2903b66 | |||
| 3fdd65acf1 | |||
| b0c3f003ac | |||
| 97f30b4300 | |||
| 9ea42c0604 | |||
| 7930ed11e4 | |||
| 1aa419392e | |||
| 558be57c58 | |||
| 98cb2a8577 | |||
| ab629fdfa0 | |||
| 345834159a | |||
| 09633a4287 | |||
| cb9c81e8f9 | |||
| 072f3ced9b | |||
| 77bde22d00 | |||
| e2d32a7300 | |||
| 01c5765888 | |||
| a0ccad58e5 | |||
| be790056f6 | |||
| e0da38e697 | |||
| 11d3501a35 | |||
| 48b5420830 | |||
| f27b649503 | |||
| 605ef7d201 | |||
| aa1e9fdeda | |||
| d3c78cb0af | |||
| 4ff9df9369 | |||
| b3fde55107 | |||
| f2d4f07069 |
@@ -1,3 +1,3 @@
|
||||
((nil . ((compile-command . (concat "make -C "
|
||||
(vc-root-dir)
|
||||
" -k -j2")))))
|
||||
" -k -j4 build=dev")))))
|
||||
|
||||
29
.gitignore
vendored
29
.gitignore
vendored
@@ -1,22 +1,23 @@
|
||||
core
|
||||
vgcore.*
|
||||
GPATH
|
||||
GRTAGS
|
||||
GTAGS
|
||||
fen
|
||||
pool
|
||||
piece
|
||||
move
|
||||
bits
|
||||
eval
|
||||
debug
|
||||
brchess
|
||||
*.o
|
||||
*.s
|
||||
*.i
|
||||
*.old
|
||||
*.save
|
||||
/test/
|
||||
perf.data
|
||||
/GPATH
|
||||
/GRTAGS
|
||||
/GTAGS
|
||||
/.ccls-root
|
||||
/.ccls-cache/
|
||||
/obj/
|
||||
/lib/
|
||||
/.deps/
|
||||
/libobj/
|
||||
/bin/
|
||||
/dep/
|
||||
/tmp/
|
||||
/notused/
|
||||
valgrind.out
|
||||
gmon.out
|
||||
compile_commands.json
|
||||
.lastbuild
|
||||
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
[submodule "brlib"]
|
||||
path = brlib
|
||||
url = ../brlib.git
|
||||
674
COPYING
Normal file
674
COPYING
Normal file
@@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
38
FENTESTS.txt
38
FENTESTS.txt
@@ -1,38 +0,0 @@
|
||||
4k3/4p3/8/b7/1BR1p2p/1Q3P2/5N2/4K3 w - - 0 1
|
||||
r1bq1rk1/pppp1ppp/2n2n2/4p3/2B1P3/3PPN2/PPP3PP/RN1QK2R b KQ - 1 7
|
||||
6k1/6pp/R2p4/p1p5/8/1P1r3P/6P1/6K1 b - - 3 37
|
||||
|
||||
# both can castle queen only
|
||||
r3k2r/8/3B4/8/8/3b4/8/R3K2R w KQkq - 0 1
|
||||
r3k2r/8/3BB3/8/8/3bb3/8/R3K2R w KQkq - 0 1
|
||||
r2bkb1r/8/8/8/8/3bb3/8/R2BKB1R w KQkq - 0 1
|
||||
|
||||
# 4 castle possible, only K+R
|
||||
r3k2r/8/8/8/8/8/8/R3K2R w KQkq - 0 1
|
||||
|
||||
# illegal positions
|
||||
4k3/8/8/8/7b/8/8/4K3 b - - 0 1
|
||||
2r1k3/3P4/8/8/8/8/8/4K3 w - - 0 1
|
||||
|
||||
# only kings on A1/A8, white to play
|
||||
k7/8/8/8/8/8/8/K7 w - - 0 1
|
||||
|
||||
# only one move possible (Pf2xBg3)
|
||||
k7/8/8/1p1p4/pPpPp3/P1PpPpb1/NBNP1P2/KBB1B3 w - - 0 1
|
||||
|
||||
# only 2 moves possible (Ph5xg6 e.p., Ph5-h6)
|
||||
k7/8/8/1p1p2pP/pPpPp3/P1PpPp2/NBNP1P2/KBB1B3 w - g6 0 1
|
||||
|
||||
# 2 Kings, W/B/ pawns on 7th for promotion
|
||||
k4n2/4P3/8/8/8/8/4p3/K4N2 w - - 0 1
|
||||
|
||||
# position after 1.e4
|
||||
rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1
|
||||
# position after 1.Nh3
|
||||
rnbqkbnr/pppppppp/8/8/8/7N/PPPPPPPP/RNBQKB1R b KQkq - 1 1
|
||||
|
||||
# white castled, and can e.p. on c6 black can castle
|
||||
# white is a pawn down
|
||||
# white has 35 or 36 moves: P=11 + 1 e.p. N=6+3 B=5+5 R=1 Q=3 K=1 + 1 e.p.
|
||||
# black has 32 or 33 moves: P=11 N=2+7 B=5 R=3 Q=3 K=1 + castle
|
||||
rnbqk2r/pp1pbpp1/7p/2pPp3/4n3/3B1N2/PPP2PPP/RNBQ1RK1 w kq c6 0 7
|
||||
23
MEMO.org
Normal file
23
MEMO.org
Normal file
@@ -0,0 +1,23 @@
|
||||
** Some current ideas
|
||||
- Test popbit/square_of with intrinsincs, something like :
|
||||
bitboard_t popbit64(bitboard_t *bb)
|
||||
{
|
||||
bitboard_t first = _blsi_u64(*bb);
|
||||
*bb ^= lsb;
|
||||
return first;
|
||||
}
|
||||
|
||||
square_t square_of(bitboard_t bb) {
|
||||
return _tzcnt_u64(bb);
|
||||
}
|
||||
|
||||
loop:
|
||||
while (bb) {
|
||||
bitboard_t first = popbit(bb);
|
||||
square_t sq = square_of(first);
|
||||
}
|
||||
|
||||
Or maybe faster:
|
||||
for (; bb; bb &= bb - 1) {
|
||||
sq = _tzcnt_u64(bb);
|
||||
}
|
||||
567
Makefile
567
Makefile
@@ -1,6 +1,6 @@
|
||||
# Makefile - GNU make only.
|
||||
# Makefile - brchess Makefile, **GNU make only**
|
||||
#
|
||||
# Copyright (C) 2021-2023 Bruno Raoult ("br")
|
||||
# Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
# Licensed under the GNU General Public License v3.0 or later.
|
||||
# Some rights reserved. See COPYING.
|
||||
#
|
||||
@@ -11,202 +11,491 @@
|
||||
#
|
||||
|
||||
SHELL := /bin/bash
|
||||
CC := gcc
|
||||
|
||||
BEAR := bear
|
||||
TOUCH := touch
|
||||
RM := rm
|
||||
CCLSFILE := compile_commands.json
|
||||
RMDIR := rmdir
|
||||
MAKE := make
|
||||
|
||||
SRCDIR := ./src
|
||||
INCDIR := ./include
|
||||
LIBSRCDIR := ./libsrc
|
||||
BINDIR := ./bin
|
||||
INCDIR := ./src # used by ./test sources
|
||||
OBJDIR := ./obj
|
||||
LIBDIR := ./lib
|
||||
LDFLAGS := -L$(LIBDIR)
|
||||
BINDIR := ./bin
|
||||
DEPDIR := ./dep
|
||||
TSTDIR := ./test
|
||||
|
||||
BRLIB := ./brlib
|
||||
BRINCDIR := $(BRLIB)/include
|
||||
BRLIBDIR := $(BRLIB)/lib
|
||||
|
||||
CCLSROOT := .ccls-root
|
||||
CCLSFILE := compile_commands.json
|
||||
|
||||
SRC := $(wildcard $(SRCDIR)/*.c) # project sources
|
||||
SRC_FN := $(notdir $(SRC)) # source basename
|
||||
OBJ := $(addprefix $(OBJDIR)/,$(SRC_FN:.c=.o))
|
||||
|
||||
LIBSRC := $(wildcard $(LIBSRCDIR)/*.c) # lib sources
|
||||
LIBOBJ := $(patsubst %.c,%.o,$(LIBSRC)) # and objects
|
||||
TSTSRC := $(wildcard $(TSTDIR)/*.c)
|
||||
|
||||
LIB := br_$(shell uname -m) # library name
|
||||
SLIB := $(addsuffix .a, $(LIBDIR)/lib$(LIB)) # static lib
|
||||
DLIB := $(addsuffix .so, $(LIBDIR)/lib$(LIB)) # dynamic lib
|
||||
LIB := $(strip $(LIB))
|
||||
LIBFILE := ${BRLIBDIR}/lib$(strip ${LIB}).a
|
||||
LIBS := $(addprefix -l,$(strip $(LIB)))
|
||||
|
||||
BIN := fen piece move eval brchess
|
||||
DEP_FN := $(SRC_FN)
|
||||
DEP := $(addprefix $(DEPDIR)/,$(DEP_FN:.c=.d))
|
||||
|
||||
LIBS := -l$(LIB) -lreadline -lncurses
|
||||
TARGET_FN := brchess
|
||||
TARGET := $(addprefix $(BINDIR)/,$(TARGET_FN))
|
||||
|
||||
ASMFILES := $(SRC:.c=.s) $(TSTSRC:.c=.s)
|
||||
CPPFILES := $(SRC:.c=.i) $(TSTSRC:.c=.i)
|
||||
|
||||
##################################### Check for compiler and requested build
|
||||
BUILDS := release dev perf debug
|
||||
# last compilation build
|
||||
BUILDFILE := .lastbuild
|
||||
lastbuild := $(file < $(BUILDFILE))
|
||||
|
||||
# default to gcc
|
||||
CC ?= cc
|
||||
ifeq ($(CC),cc)
|
||||
CC = gcc
|
||||
endif
|
||||
|
||||
# if no build specified, use last one
|
||||
ifeq ($(build),)
|
||||
build := $(lastbuild)
|
||||
endif
|
||||
# if build is still undefined, set a default
|
||||
ifeq ($(build),)
|
||||
build := release
|
||||
endif
|
||||
|
||||
# check for valid build
|
||||
ifeq ($(filter $(build),$(BUILDS)),)
|
||||
$(error Error: Unknown build=`$(build)`. Possible builds are: $(BUILDS))
|
||||
endif
|
||||
|
||||
# if new build, rewrite BUILDFILE
|
||||
ifeq ($(build),$(lastbuild))
|
||||
$(info Using last used build:`$(build)`.)
|
||||
else
|
||||
$(info Using new build:`$(build)` (previous:$(lastbuild)))
|
||||
$(file >$(BUILDFILE),$(build))
|
||||
endif
|
||||
|
||||
##################################### set a version string
|
||||
# inspired from:
|
||||
# https://eugene-babichenko.github.io/blog/2019/09/28/nightly-versions-makefiles/
|
||||
|
||||
# last commit and date
|
||||
COMMIT := $(shell git rev-parse --short HEAD)
|
||||
DATE := $(shell git log -1 --format=%cd --date=format:"%Y%m%d")
|
||||
|
||||
# get last commit w/ tag & associated tag, if any
|
||||
TAG_COMM := $(shell git rev-list --abbrev-commit --tags --max-count=1)
|
||||
ifneq ($(TAG_COMMIT),)
|
||||
TAG := $(shell git describe --abbrev=0 --tags ${TG_COMM} 2>/dev/null || true)
|
||||
VERSION := $(TAG:v%=%)
|
||||
endif
|
||||
|
||||
# if no version, use last commit and date.
|
||||
# else, if last commit != last tag commit, add commit and date to version number
|
||||
ifeq ($(VERSION),)
|
||||
VERSION := $(build)-git.$(COMMIT)-$(DATE)
|
||||
else ifneq ($(COMMIT), $(TAG_COMMIT))
|
||||
VERSION := $(VERSION)-next-$(build)-$(COMMIT)-$(DATE)
|
||||
endif
|
||||
# if uncommited changes, add "dirty" indicator
|
||||
ifneq ($(shell git status --porcelain),)
|
||||
VERSION := $(VERSION)-dirty
|
||||
endif
|
||||
|
||||
##################################### pre-processor flags
|
||||
CPPFLAGS := -I$(BRINCDIR) -I$(INCDIR) -DVERSION=\"$(VERSION)\"
|
||||
|
||||
CPPFLAGS += -DDIAGRAM_SYM # UTF8 symbols in diagrams
|
||||
|
||||
ifeq ($(build),release)
|
||||
CPPFLAGS += -DNDEBUG # assert (unused)
|
||||
else # ifeq ($(build),dev)
|
||||
CPPFLAGS += -DBUG_ON # brlib bug.h
|
||||
|
||||
# fen.c
|
||||
#CPPFLAGS += -DDEBUG_FEN # FEN decoding
|
||||
# hash / TT
|
||||
#CPPFLAGS += -DZOBRIST_VERIFY # incr. zobrist check
|
||||
#CPPFLAGS += -DPERFT_MOVE_HISTORY # perft, keep prev moves
|
||||
# attack.c
|
||||
#CPPFLAGS += -DDEBUG_ATTACK_ATTACKERS # sq_attackers
|
||||
#CPPFLAGS += -DDEBUG_ATTACK_PINNERS # sq_pinners details
|
||||
# eval.c eval-simple.c
|
||||
CPPFLAGS += -DDEBUG_EVAL # eval
|
||||
CPPFLAGS += -DPHASE_VERIFY # incr. phase check
|
||||
|
||||
# old unused flags
|
||||
#CPPFLAGS += -DDEBUG_POS # position.c
|
||||
#CPPFLAGS += -DDEBUG_MOVE # move generation
|
||||
endif
|
||||
|
||||
# remove extraneous spaces (due to spaces before comments)
|
||||
CPPFLAGS := $(strip $(CPPFLAGS))
|
||||
|
||||
##################################### compiler / linker flags
|
||||
CFLAGS := -std=gnu11
|
||||
|
||||
#CFLAGS += -O2
|
||||
CFLAGS += -g
|
||||
CFLAGS += -Wall
|
||||
CFLAGS += -Wextra
|
||||
CFLAGS += -Wall -Wextra -Wshadow -Wmissing-declarations
|
||||
CFLAGS += -march=native
|
||||
CFLAGS += -Wmissing-declarations
|
||||
# for gprof
|
||||
# CFLAGS += -pg
|
||||
# Next one may be useful for valgrind (when invalid instructions)
|
||||
# CFLAGS += -mno-tbm
|
||||
|
||||
##################################### DEBUG flags
|
||||
CPPFLAGS := -I$(INCDIR)
|
||||
CPPFLAGS += -DDEBUG # global
|
||||
CPPFLAGS += -DDEBUG_DEBUG # enable log() functions
|
||||
#CPPFLAGS += -DDEBUG_DEBUG_C # enable verbose log() settings
|
||||
CPPFLAGS += -DDEBUG_POOL # memory pools management
|
||||
CPPFLAGS += -DDEBUG_FEN # FEN decoding
|
||||
CPPFLAGS += -DDEBUG_MOVE # move generation
|
||||
CPPFLAGS += -DDEBUG_EVAL # eval functions
|
||||
CPPFLAGS += -DDEBUG_PIECE # piece list management
|
||||
CPPFLAGS += -DDEBUG_SEARCH # move search
|
||||
LDFLAGS := --static
|
||||
LDFLAGS += -L$(BRLIBDIR)
|
||||
|
||||
### dev OR release
|
||||
ifeq ($(build),release)
|
||||
CFLAGS += -O3
|
||||
CFLAGS += -funroll-loops
|
||||
CFLAGS += -flto
|
||||
#CFLAGS += -g
|
||||
#CFLAGS += -ginline-points # inlined funcs debug info
|
||||
LDFLAGS += -flto
|
||||
else ifeq ($(build),dev)
|
||||
CFLAGS += -Og
|
||||
CFLAGS += -g # symbols (gdb, perf, etc.)
|
||||
CFLAGS += -ginline-points # inlined funcs debug info
|
||||
#CFLAGS += -pg # gprof
|
||||
# Next one may be useful for valgrind (when invalid instructions)
|
||||
#CFLAGS += -mno-tbm
|
||||
else ifeq ($(build),perf)
|
||||
CFLAGS += -O3
|
||||
CFLAGS += -g # symbols (gdb, perf, etc.)
|
||||
CFLAGS += -ginline-points # inlined funcs debug info
|
||||
CFLAGS += -funroll-loops
|
||||
else ifeq ($(build),debug)
|
||||
CFLAGS += -Og
|
||||
CFLAGS += -g # symbols (gdb, perf, etc.)
|
||||
CFLAGS += -ginline-points # inlined funcs debug info
|
||||
# for gprof
|
||||
#CFLAGS += -pg
|
||||
# Next one may be useful for valgrind (when invalid instructions)
|
||||
#CFLAGS += -mno-tbm
|
||||
endif
|
||||
|
||||
CFLAGS := $(strip $(CFLAGS))
|
||||
LDFLAGS := $(strip $(LDFLAGS))
|
||||
|
||||
##################################### dependency flags
|
||||
DEPFLAGS = -MMD -MP -MF $(DEPDIR)/$*.d
|
||||
|
||||
##################################### archiver/dependency flags
|
||||
ALL_CFLAGS = $(DEPFLAGS) $(CPPFLAGS) $(CFLAGS)
|
||||
ALL_LDFLAGS = $(LDFLAGS) $(LIBS)
|
||||
|
||||
##################################### Multi-targets
|
||||
# We can have an issue with Make's "-j" option, in some situations,
|
||||
# for example "make -j2 clean testing".
|
||||
# See https://stackoverflow.com/a/8496333/3079831
|
||||
|
||||
# Check if job server supported:
|
||||
ifeq ($(filter jobserver, $(.FEATURES)),)
|
||||
# Job server not supported: sub-makes will only start one job unless
|
||||
# you specify a higher number here. Here we use a MS Windows environment
|
||||
# variable specifying number of processors.
|
||||
JOBSARG := -j $(NUMBER_OF_PROCESSORS)
|
||||
else
|
||||
# Job server is supported; let GNU Make work as normal.
|
||||
JOBSARG :=
|
||||
endif
|
||||
|
||||
# .FEATURES only works in GNU Make 3.81+.
|
||||
# If GNU make is older, assume job server support.
|
||||
ifneq ($(firstword $(sort 3.81 $(MAKE_VERSION))),3.81)
|
||||
# If you are using GNU Make < 3.81 that does not support job servers, you
|
||||
# might want to specify -jN parameter here instead.
|
||||
JOBSARG :=
|
||||
endif
|
||||
|
||||
ifneq ($(words $(MAKECMDGOALS)),1)
|
||||
.NOTPARALLEL:
|
||||
# The "all" target is required in the list,
|
||||
# in case user invokes make with no targets.
|
||||
$(sort all $(MAKECMDGOALS)):
|
||||
@$(MAKE) $(JOBSARG) -f $(firstword $(MAKEFILE_LIST)) $@
|
||||
else
|
||||
|
||||
##################################### General targets
|
||||
.PHONY: compile cflags all clean cleanall
|
||||
.PHONY: all release dev perf debug compile libs clean cleanall
|
||||
|
||||
compile: objects bin
|
||||
all: libs testing $(TARGET)
|
||||
|
||||
cflags:
|
||||
@echo CFLAGS: "$(CFLAGS)"
|
||||
@echo CPPFLAGS: $(CPPFLAGS)
|
||||
@echo DEPFLAGS: $(DEPFLAGS)
|
||||
@echo LDFLAGS: $(LDFLAGS)
|
||||
release:
|
||||
$(MAKE) BUILD=release clean all
|
||||
|
||||
all: clean compile
|
||||
dev:
|
||||
$(MAKE) BUILD=dev clean all
|
||||
|
||||
clean: cleanobj cleanbin
|
||||
perf:
|
||||
$(MAKE) BUILD=perf clean all
|
||||
|
||||
cleanall: clean cleandeps cleanlib
|
||||
debug:
|
||||
$(MAKE) BUILD=debug clean all
|
||||
|
||||
compile: brlib objs
|
||||
|
||||
libs: brlib
|
||||
|
||||
clean: cleandep cleanasmcpp cleanobj cleanbin
|
||||
|
||||
cleanall: clean cleandepdir cleanobjdir cleanallbrlib cleanbindir
|
||||
|
||||
##################################### cleaning functions
|
||||
# rmfiles - delete a list of files if they exist.
|
||||
# $(1): the directory
|
||||
# $(2): The string to include in action output - "cleaning X files."
|
||||
# see: https://stackoverflow.com/questions/6783243/functions-in-makefiles
|
||||
#
|
||||
# Don't use wildcard like "$(DIR)/*.o", so we can control mismatches between
|
||||
# list and actual files in directory.
|
||||
# See rmdir below.
|
||||
define rmfiles
|
||||
@#echo "rmfiles=+$(1)+"
|
||||
$(eval $@_EXIST = $(wildcard $(1)))
|
||||
@#echo "existfile=+${$@_EXIST}+"
|
||||
@if [[ -n "${$@_EXIST}" ]]; then \
|
||||
echo "cleaning $(2) files." ; \
|
||||
$(RM) ${$@_EXIST} ; \
|
||||
fi
|
||||
endef
|
||||
|
||||
# rmdir - delete a directory if it exists.
|
||||
# $(1): the directory
|
||||
# $(2): The string to include in action output - "removing X dir."
|
||||
#
|
||||
# Don't use $(RM) -rf, to control unexpected dep files.
|
||||
# See rmfile above.
|
||||
define rmdir
|
||||
@#echo "rmdir +$(1)+"
|
||||
$(eval $@_EXIST = $(wildcard $(1)))
|
||||
@#echo "existdir=+${$@_EXIST}+"
|
||||
@if [[ -n "${$@_EXIST}" ]]; then \
|
||||
echo "removing $(2) dir." ; \
|
||||
$(RMDIR) ${$@_EXIST} ; \
|
||||
fi
|
||||
endef
|
||||
|
||||
##################################### dirs creation
|
||||
.PHONY: alldirs
|
||||
|
||||
ALLDIRS := $(DEPDIR) $(OBJDIR) $(BINDIR)
|
||||
|
||||
alldirs: $(ALLDIRS)
|
||||
|
||||
# Here, we have something like:
|
||||
# a: a
|
||||
# a will be built if (1) older than a, or (2) does not exist. Here only (2).
|
||||
$(ALLDIRS): $@
|
||||
@echo creating $@ directory.
|
||||
@mkdir -p $@
|
||||
|
||||
##################################### Dependencies files
|
||||
.PHONY: deps cleandeps
|
||||
.PHONY: cleandep cleandepdir
|
||||
|
||||
DEPDIR := ./.deps
|
||||
DEPFILES := $(addprefix $(DEPDIR)/,$(SRC_FN:.c=.d))
|
||||
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.d
|
||||
-include $(wildcard $(DEP))
|
||||
|
||||
$(DEPFILES):
|
||||
# Don't use "rm $(DEPDIR)/*.d", to understand mismatches between dep/ and src/
|
||||
# files.
|
||||
# See second rule below.
|
||||
cleandep:
|
||||
$(call rmfiles,$(DEP),depend)
|
||||
@#echo cleaning dependency files.
|
||||
@#$(RM) -f $(DEP)
|
||||
|
||||
include $(wildcard $(DEPFILES))
|
||||
cleandepdir:
|
||||
$(call rmdir,$(DEPDIR),depend)
|
||||
@#[ -d $(DEPDIR) ] && echo cleaning depend files && $(RM) -f $(DEP) || true
|
||||
|
||||
$(DEPDIR):
|
||||
@echo creating $@ directory.
|
||||
@mkdir -p $@
|
||||
##################################### brchess objects
|
||||
.PHONY: objs cleanobj cleanobjdir
|
||||
|
||||
cleandeps:
|
||||
$(RM) -rf $(DEPDIR)
|
||||
|
||||
##################################### objects
|
||||
.SECONDEXPANSION:
|
||||
OBJ = $(addprefix $(OBJDIR)/,$(SRC_FN:.c=.o))
|
||||
|
||||
.PHONY: cleanobj
|
||||
|
||||
objects: $(OBJ)
|
||||
objs: $(OBJ)
|
||||
|
||||
cleanobj:
|
||||
$(RM) -rf $(OBJDIR)
|
||||
$(call rmfiles,$(OBJ),object)
|
||||
|
||||
$(OBJDIR):
|
||||
@echo creating $@ directory.
|
||||
@mkdir -p $@
|
||||
cleanobjdir: cleanobj
|
||||
$(call rmdir,$(OBJDIR),objects)
|
||||
|
||||
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(DEPDIR)/%.d | $(OBJDIR) $(DEPDIR)
|
||||
@echo compiling $<.
|
||||
@$(CC) -c $(CPPFLAGS) $(CFLAGS) $(DEPFLAGS) -I $(INCDIR) -o $@ $<
|
||||
# The part right of '|' are "order-only prerequisites": They are build as
|
||||
# "normal" ones, but do not imply to rebuild target.
|
||||
$(OBJDIR)/%.o: $(BUILDFILE)
|
||||
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(BUILDFILE) | $(OBJDIR) $(DEPDIR)
|
||||
@echo compiling brchess module: $< "->" $@.
|
||||
$(CC) -c $(ALL_CFLAGS) $< -o $@
|
||||
|
||||
##################################### pre-processed (.i) and assembler (.s) output
|
||||
%.i: %.c
|
||||
@echo generating $@
|
||||
@$(CC) -E $(CFLAGS) -I $(INCDIR) $< -o $@
|
||||
##################################### brlib libraries
|
||||
.PHONY: cleanbrlib cleanallbrlib brlib
|
||||
|
||||
%.s: %.c
|
||||
@echo generating $@
|
||||
@$(CC) -S -fverbose-asm $(CFLAGS) -I $(INCDIR) $< -o $@
|
||||
cleanbrlib:
|
||||
$(MAKE) -C $(BRLIB) clean
|
||||
|
||||
cleanallbrlib:
|
||||
$(MAKE) -C $(BRLIB) cleanall
|
||||
|
||||
##################################### br library
|
||||
.PHONY: cleanlib libs
|
||||
export build
|
||||
brlib:
|
||||
$(info calling with build=$(build))
|
||||
$(MAKE) -e -C $(BRLIB) lib-static
|
||||
unexport build
|
||||
|
||||
ARFLAGS = r
|
||||
##################################### brchess binaries
|
||||
.PHONY: targets cleanbin cleanbindir
|
||||
|
||||
cleanlib:
|
||||
$(RM) -rf $(LIBDIR) $(LIBOBJ)
|
||||
|
||||
libs: $(DLIB) $(SLIB)
|
||||
|
||||
$(LIBDIR):
|
||||
@echo creating $@ directory.
|
||||
@mkdir -p $@
|
||||
|
||||
# remove default rule
|
||||
%.o: %.c
|
||||
|
||||
$(LIBSRCDIR)/%.o: $(LIBSRCDIR)/%.c
|
||||
@echo compiling library $< "->" $@.
|
||||
@$(CC) -c $(CPPFLAGS) $(CFLAGS) -I $(INCDIR) -o $@ $<
|
||||
|
||||
$(SLIB): $(LIBOBJ) | $(LIBDIR)
|
||||
@echo building $@ static library.
|
||||
@$(AR) $(ARFLAGS) -o $@ $^
|
||||
|
||||
$(DLIB): CFLAGS += -fPIC
|
||||
$(DLIB): LDFLAGS += -shared
|
||||
$(DLIB): $(LIBOBJ) | $(LIBDIR)
|
||||
@echo building $@ shared library.
|
||||
@$(CC) $(LDFLAGS) $^ -o $@
|
||||
|
||||
##################################### binaries
|
||||
.PHONY: bin prebin cleanbin
|
||||
|
||||
BINMARK := .bindone
|
||||
|
||||
bin: $(BIN) postbin
|
||||
|
||||
postbin:
|
||||
@[[ ! -f $(BINMARK) ]] || echo done.
|
||||
@$(RM) -f $(BINMARK)
|
||||
targets: $(TARGET)
|
||||
|
||||
cleanbin:
|
||||
$(RM) -f $(BIN) core
|
||||
$(call rmfiles,$(TARGET),binary)
|
||||
|
||||
# TODO: find a better dependancy graph
|
||||
$(BIN): $(SRCDIR)/$$@.c $(DLIB) $$(subst $(OBJDIR)/$$@.o,,$(OBJ))
|
||||
@[[ -f $(BINMARK) ]] || echo -n "generating binaries: "
|
||||
@echo -n "$@... "
|
||||
@$(CC) -DBIN_$@ $(CPPFLAGS) $(CFLAGS) -I $(INCDIR) $(subst libs,,$^) $(LDFLAGS) $(LIBS) -o $@
|
||||
@$(TOUCH) $(BINMARK)
|
||||
cleanbindir:
|
||||
$(call rmdir,$(BINDIR),binaries)
|
||||
|
||||
##################################### ccls
|
||||
$(TARGET): $(LIBFILE) $(OBJ) | $(BINDIR) libs
|
||||
@echo linking $@.
|
||||
$(CC) $(ALL_LDFLAGS) $(OBJ) -o $@
|
||||
|
||||
##################################### pre-processed (.i) and assembler (.s) output
|
||||
.PHONY: cleanasmcpp
|
||||
|
||||
cleanasmcpp:
|
||||
@$(call rmfiles,$(ASMFILES) $(CPPFILES),asm and pre-processed)
|
||||
|
||||
%.i: %.c
|
||||
@echo "generating $@ (cpp processed)."
|
||||
@$(CC) -E $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||
|
||||
%.s: %.c
|
||||
@echo "generating $@ (asm)."
|
||||
$(CC) -S -fverbose-asm $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||
|
||||
##################################### LSP (ccls)
|
||||
.PHONY: ccls
|
||||
|
||||
ccls: $(CCLSFILE)
|
||||
|
||||
# generate compile_commands.json
|
||||
$(CCLSFILE): brchess Makefile
|
||||
$(BEAR) -- make clean bin
|
||||
|
||||
##################################### LSP (ccls)
|
||||
.PHONY: bear
|
||||
ROOTDIR = .ccls-root
|
||||
|
||||
$(CCLSROOT):
|
||||
@echo creating root marker file.
|
||||
@echo creating project root file.
|
||||
@$(TOUCH) $@
|
||||
|
||||
bear: clean $(CCLSROOT)
|
||||
@touch .ccls-root
|
||||
@$(BEAR) -- make compile
|
||||
# generate compile_commands.json.
|
||||
# TODO: add Makefile dependencies.
|
||||
# also, if cclsfile is newer than sources, no need to clean objects file
|
||||
# (and to run bear).
|
||||
# maybe run cleanobj cleanlibobj in commands ?
|
||||
$(CCLSFILE): cleanobj cleanbrlib libs | $(CCLSROOT)
|
||||
@echo "Generating ccls compile commands file ($@)."
|
||||
@$(BEAR) -- $(MAKE)
|
||||
|
||||
##################################### LSP (ccls)
|
||||
##################################### valgrind (mem check)
|
||||
.PHONY: memcheck
|
||||
|
||||
VALGRIND = valgrind
|
||||
VALGRINDFLAGS = --leak-check=full --show-leak-kinds=all
|
||||
VALGRINDFLAGS += --track-origins=yes --sigill-diagnostics=yes
|
||||
VALGRINDFLAGS += --quiet --show-error-list=yes
|
||||
VALGRINDFLAGS += --log-file=valgrind.out
|
||||
# We need to suppress libreadline leaks here. See :
|
||||
# https://stackoverflow.com/questions/72840015
|
||||
VALGRINDFLAGS += --suppressions=etc/libreadline.supp
|
||||
|
||||
memcheck: brchess
|
||||
memcheck: targets
|
||||
@$(VALGRIND) $(VALGRINDFLAGS) $(BINDIR)/brchess
|
||||
|
||||
##################################### test binaries
|
||||
.PHONY: testing
|
||||
|
||||
TEST := piece-test fen-test bitboard-test movegen-test attack-test
|
||||
TEST += movedo-test perft-test tt-test
|
||||
|
||||
PIECE_OBJS := piece.o
|
||||
FEN_OBJS := $(PIECE_OBJS) fen.o position.o bitboard.o board.o \
|
||||
hq.o attack.o hash.o init.o util.o alloc.o move.o \
|
||||
eval.o eval-defs.o eval-simple.o hist.o
|
||||
BB_OBJS := $(FEN_OBJS)
|
||||
MOVEGEN_OBJS := $(BB_OBJS) move-gen.o
|
||||
ATTACK_OBJS := $(MOVEGEN_OBJS)
|
||||
MOVEDO_OBJS := $(ATTACK_OBJS) move-do.o
|
||||
PERFT_OBJS := $(MOVEDO_OBJS) perft.o
|
||||
TT_OBJS := $(MOVEDO_OBJS)
|
||||
|
||||
TEST := $(addprefix $(BINDIR)/,$(TEST))
|
||||
|
||||
PIECE_OBJS := $(addprefix $(OBJDIR)/,$(PIECE_OBJS))
|
||||
FEN_OBJS := $(addprefix $(OBJDIR)/,$(FEN_OBJS))
|
||||
BB_OBJS := $(addprefix $(OBJDIR)/,$(BB_OBJS))
|
||||
MOVEGEN_OBJS := $(addprefix $(OBJDIR)/,$(MOVEGEN_OBJS))
|
||||
ATTACK_OBJS := $(addprefix $(OBJDIR)/,$(ATTACK_OBJS))
|
||||
MOVEDO_OBJS := $(addprefix $(OBJDIR)/,$(MOVEDO_OBJS))
|
||||
PERFT_OBJS := $(addprefix $(OBJDIR)/,$(PERFT_OBJS))
|
||||
TT_OBJS := $(addprefix $(OBJDIR)/,$(TT_OBJS))
|
||||
|
||||
testing: $(TEST)
|
||||
|
||||
bin/piece-test: test/piece-test.c $(FEN_OBJS)
|
||||
@echo linking $@ test executable.
|
||||
@$(CC) $(ALL_CFLAGS) $< $(FEN_OBJS) $(ALL_LDFLAGS) -o $@
|
||||
|
||||
bin/fen-test: test/fen-test.c test/common-test.h $(FEN_OBJS)
|
||||
@echo linking $@ test executable.
|
||||
@$(CC) $(ALL_CFLAGS) $< $(FEN_OBJS) $(ALL_LDFLAGS) -o $@
|
||||
|
||||
bin/bitboard-test: test/bitboard-test.c test/common-test.h $(BB_OBJS)
|
||||
@echo linking $@ test executable.
|
||||
@$(CC) $(ALL_CFLAGS) $< $(BB_OBJS) $(ALL_LDFLAGS) -o $@
|
||||
|
||||
bin/movegen-test: test/movegen-test.c test/common-test.h $(MOVEGEN_OBJS)
|
||||
@echo linking $@ test executable.
|
||||
@$(CC) $(ALL_CFLAGS) $< $(MOVEGEN_OBJS) $(ALL_LDFLAGS) -o $@
|
||||
|
||||
bin/attack-test: test/attack-test.c test/common-test.h $(ATTACK_OBJS)
|
||||
@echo linking $@ test executable.
|
||||
@$(CC) $(ALL_CFLAGS) $< $(ATTACK_OBJS) $(ALL_LDFLAGS) -o $@
|
||||
|
||||
bin/movedo-test: test/movedo-test.c test/common-test.h $(MOVEDO_OBJS)
|
||||
@echo linking $@ test executable.
|
||||
@$(CC) $(ALL_CFLAGS) $< $(MOVEDO_OBJS) $(ALL_LDFLAGS) -o $@
|
||||
|
||||
bin/perft-test: test/perft-test.c test/common-test.h $(PERFT_OBJS)
|
||||
@echo linking $@ test executable.
|
||||
@$(CC) $(ALL_CFLAGS) $< $(PERFT_OBJS) $(ALL_LDFLAGS) -o $@
|
||||
|
||||
bin/tt-test: test/tt-test.c test/common-test.h $(TT_OBJS)
|
||||
@echo linking $@ test executable.
|
||||
@$(CC) $(ALL_CFLAGS) $< $(TT_OBJS) $(ALL_LDFLAGS) -o $@
|
||||
|
||||
##################################### Makefile debug
|
||||
.PHONY: showflags wft
|
||||
|
||||
info:
|
||||
@printf "CFLAGS: +%s+\n" "$(CFLAGS)"
|
||||
@printf "CPPFLAGS: +%s+\n" "$(CPPFLAGS)"
|
||||
@printf "DEPFLAGS: +%s+\n" "$(DEPFLAGS)"
|
||||
@printf "LDFLAGS: +%s+\n" "$(LDFLAGS)"
|
||||
@printf "DEPFLAGS: +%s+\n" "$(DEPFLAGS)"
|
||||
@printf "VERSION: +%s+\n" "$(VERSION)"
|
||||
|
||||
wtf:
|
||||
@printf "BRLIBDIR=%s\n" "$(BRLIBDIR)"
|
||||
@printf "LDFLAGS=%s\n\n" "$(LDFLAGS)"
|
||||
@#printf "LIBOBJ=%s\n\n" "$(LIBOBJ)"
|
||||
@#printf "OBJDIR=%s\n\n" "$(OBJDIR)"
|
||||
@#printf "OBJ=%s\n\n" "$(OBJ)"
|
||||
@#echo LIBOBJ=$(LIBOBJ)
|
||||
@#echo DEP=$(DEP)
|
||||
@#echo LIBSRC=$(LIBSRC)
|
||||
|
||||
zob:
|
||||
echo $(LIBFILE)
|
||||
@#$(CC) $(LDFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(LIBS) src/util.c -o util
|
||||
|
||||
##################################### End of multi-targets
|
||||
endif
|
||||
|
||||
40
README.org
Normal file
40
README.org
Normal file
@@ -0,0 +1,40 @@
|
||||
#+OPTIONS: toc:nil
|
||||
#+OPTIONS: num:2
|
||||
**This is not a working chess program !!**
|
||||
|
||||
/I am only experimenting some chess programming concepts./
|
||||
|
||||
** License
|
||||
~SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>~~
|
||||
|
||||
This work is Copyright (C) 2021-2024 Bruno Raoult ("br"), and licensed under
|
||||
the GNU General Public License v3.0 or later.
|
||||
|
||||
Some rights reserved. See COPYING.
|
||||
|
||||
** Installation (don't do it until version 0.9)
|
||||
*** dependencire
|
||||
- GCC 10 or newer
|
||||
- libreadline
|
||||
*** clone repository
|
||||
*user...**
|
||||
#+BEGIN_EXAMPLE
|
||||
$ git clone https://github.com/braoult/brchess.git
|
||||
or
|
||||
$ git clone https://git.raoult.com/bruno/brchess.git
|
||||
#+END_EXAMPLE
|
||||
|
||||
*OR ...developer*
|
||||
#+BEGIN_EXAMPLE
|
||||
$ git clone git@git.raoult.com:bruno/brchess.git
|
||||
$ cd brchess
|
||||
$ git remote add github git@github.com:braoult/brchess.git
|
||||
$ git fetch --all
|
||||
#+END_EXAMPLE
|
||||
|
||||
*** add "brlib" submodule
|
||||
#+BEGIN_EXAMPLE
|
||||
$ cd brchess
|
||||
$ git submodule init
|
||||
$ git submodule update
|
||||
#+END_EXAMPLE
|
||||
57
STATUS.org
Normal file
57
STATUS.org
Normal file
@@ -0,0 +1,57 @@
|
||||
#+OPTIONS: toc:nil
|
||||
|
||||
* Misc
|
||||
- License: [[https://www.gnu.org/licenses/gpl-3.0-standalone.html][GNU General Public License v3.0 or later]].
|
||||
- Code is in C (gnu11 = C11 + some GNU extensions), tested only on Linux x86-64.
|
||||
- [[https://git.raoult.com/bruno/brchess][Source Code]].
|
||||
|
||||
* Done or partially done
|
||||
*** Text interface
|
||||
- basic commands like "init", "fen xxxx", "depth n", "search", "pvs", etc...
|
||||
*** Dual [[https://en.wikipedia.org/wiki/0x88][0x88]] / [[https://en.wikipedia.org/wiki/Bitboard#Chess_bitboards][bitboard]] representations
|
||||
- I started with a [[https://en.wikipedia.org/wiki/0x88][0x88 board representation]]
|
||||
- But... wanted to switch to [[https://en.wikipedia.org/wiki/Bitboard#Chess_bitboards][bitboard]] lately
|
||||
- Today there is a messy mix of the two representations.
|
||||
*** A [[https://www.chessprogramming.org/Pseudo-Legal_Move][pseudo-legal move]] generator
|
||||
- Does not check for some invalid moves (especially king-pinned pieces moves, which could be very expensive).
|
||||
I believe some programs do not do it too, and prefer to see invalid positions at next ply (TODO).
|
||||
- Still with 0x88 board (needs to be rewritten with bitboards).
|
||||
*** *Very expensive* pieces and moves list
|
||||
- They should be converted into arrays, to allow fast duplication for move_do(), the function which actually makes a move.
|
||||
*** A basic eval function
|
||||
- preferred squares for pieces
|
||||
- mobility
|
||||
*** "Move Search" functions
|
||||
**** Basic [[https://en.wikipedia.org/wiki/Negamax][negamax]] search function
|
||||
- No [[https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning][alpha-beta pruning]]
|
||||
**** [[https://en.wikipedia.org/wiki/Principal_variation_search][Principal Variation Search]] (PVS) function
|
||||
- Alpha-beta pruning
|
||||
- Basic moves [[https://www.chessprogramming.org/Move_Ordering][pre-ordering]]
|
||||
It makes the PVS/alpha-beta pruning quite decent. For example, a 6 ply depth search gives:
|
||||
- negamax: 1,196 secs for 125,799 Knodes
|
||||
- PVS: 14 secs for 1,575 Knodes
|
||||
- Both make search at a fixed depth (still no [[https://www.chessprogramming.org/Quiescence_Search][quiescence search]] at terminal nodes).
|
||||
|
||||
* Missing
|
||||
*** All Chess rules
|
||||
- Mate/Pat detection (!)
|
||||
Not trivial, as using pseudo-valid moves implies mate/pat detection is late
|
||||
- Special rules like 50 moves/position repetition
|
||||
*** Actual game management
|
||||
- Play a move, ask engine to play a move
|
||||
- A standard interface for usual software (like SCID)
|
||||
Probably [[https://www.gnu.org/software/xboard/engine-intf.html][xboard]] first, as it looks simpler than [[https://en.wikipedia.org/wiki/Universal_Chess_Interface][UCI]].
|
||||
*** Search improvement
|
||||
- Book-keeping of moves during search (did not decide the method).
|
||||
- [[https://en.wikipedia.org/wiki/Zobrist_hashing][Positions hashing]] / transposition detection
|
||||
|
||||
* Next steps planned (no specific order)
|
||||
*** Memory cleanup
|
||||
- proper recusive position/moves cleanup in search() and pvs()
|
||||
- use valgrind to find possible other unfreed memory
|
||||
*** Replace the current interface with a basic xboard one
|
||||
1. Check which commands are necessary/mandatory.
|
||||
1. This will allow easy testing with common software
|
||||
*** Mate/Pat detection
|
||||
*** In Search, detect King capture
|
||||
*** In Search, do something when no move available
|
||||
1
brlib
Submodule
1
brlib
Submodule
Submodule brlib added at 180839c960
6
etc/libreadline.supp
Normal file
6
etc/libreadline.supp
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
ignore_libreadline_leaks
|
||||
Memcheck:Leak
|
||||
...
|
||||
obj:*/libreadline.so.*
|
||||
}
|
||||
539
include/bits.h
539
include/bits.h
@@ -1,539 +0,0 @@
|
||||
/* bits.h - bits functions.
|
||||
*
|
||||
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
#ifndef _BITS_H
|
||||
#define _BITS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* next include will define __WORDSIZE: 32 or 64
|
||||
*/
|
||||
#include <bits/wordsize.h>
|
||||
|
||||
#ifndef __has_builtin
|
||||
#define __has_builtin(x) 0
|
||||
#endif
|
||||
|
||||
/* no plan to support 32bits for now...
|
||||
* #if __WORDSIZE != 64
|
||||
* #error "Only 64 bits word size supported."
|
||||
* #endif
|
||||
*/
|
||||
|
||||
/* fixed-size types
|
||||
*/
|
||||
typedef int64_t s64;
|
||||
typedef int32_t s32;
|
||||
typedef int16_t s16;
|
||||
typedef int8_t s8;
|
||||
|
||||
typedef uint64_t u64;
|
||||
typedef uint32_t u32;
|
||||
typedef uint16_t u16;
|
||||
typedef uint8_t u8;
|
||||
|
||||
/* convenience types
|
||||
*/
|
||||
typedef long long int llong;
|
||||
typedef unsigned long long int ullong;
|
||||
typedef unsigned long int ulong;
|
||||
typedef unsigned int uint;
|
||||
typedef unsigned short ushort;
|
||||
typedef unsigned char uchar;
|
||||
/* char is a special case, as it can be signed or unsigned
|
||||
*/
|
||||
typedef signed char schar;
|
||||
|
||||
/* define common types sizes
|
||||
*/
|
||||
#define BITS_PER_CHAR 8
|
||||
|
||||
#ifndef BITS_PER_SHORT
|
||||
#define BITS_PER_SHORT (BITS_PER_CHAR * sizeof (short))
|
||||
#endif
|
||||
#ifndef BITS_PER_INT
|
||||
#define BITS_PER_INT (BITS_PER_CHAR * sizeof (int))
|
||||
#endif
|
||||
#ifndef BITS_PER_LONG
|
||||
#define BITS_PER_LONG (BITS_PER_CHAR * sizeof (long))
|
||||
#endif
|
||||
#ifndef BITS_PER_LLONG
|
||||
#define BITS_PER_LLONG (BITS_PER_CHAR * sizeof (long long))
|
||||
#endif
|
||||
|
||||
/* count set bits: 10101000 -> 3
|
||||
* ^ ^ ^
|
||||
*/
|
||||
static __always_inline int popcount64(u64 n)
|
||||
{
|
||||
# if __has_builtin(__builtin_popcountl)
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "builtin.\n");
|
||||
# endif
|
||||
return __builtin_popcountl(n);
|
||||
|
||||
# else
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "emulated.\n");
|
||||
# endif
|
||||
int count = 0;
|
||||
while (n) {
|
||||
count++;
|
||||
n &= (n - 1);
|
||||
}
|
||||
return count;
|
||||
# endif
|
||||
}
|
||||
|
||||
static __always_inline int popcount32(u32 n)
|
||||
{
|
||||
# if __has_builtin(__builtin_popcount)
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "builtin.\n");
|
||||
# endif
|
||||
return __builtin_popcount(n);
|
||||
|
||||
# else
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "emulated.\n");
|
||||
# endif
|
||||
int count = 0;
|
||||
while (n) {
|
||||
count++;
|
||||
n &= (n - 1);
|
||||
}
|
||||
return count;
|
||||
# endif
|
||||
}
|
||||
|
||||
/* count trailing zeroes : 00101000 -> 3
|
||||
* ^^^
|
||||
*/
|
||||
static __always_inline int ctz64(u64 n)
|
||||
{
|
||||
# if __has_builtin(__builtin_ctzl)
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "builtin ctzl.\n");
|
||||
# endif
|
||||
return __builtin_ctzl(n);
|
||||
|
||||
# elif __has_builtin(__builtin_clzl)
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "builtin clzl.\n");
|
||||
# endif
|
||||
return __WORDSIZE - (__builtin_clzl(n & -n) + 1);
|
||||
|
||||
# else
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "emulated.\n");
|
||||
# endif
|
||||
return popcount64((n & -n) - 1);
|
||||
# endif
|
||||
}
|
||||
|
||||
static __always_inline int ctz32(u32 n)
|
||||
{
|
||||
# if __has_builtin(__builtin_ctz)
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "builtin ctz.\n");
|
||||
# endif
|
||||
return __builtin_ctzl(n);
|
||||
|
||||
# elif __has_builtin(__builtin_clz)
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "builtin clz.\n");
|
||||
# endif
|
||||
return __WORDSIZE - (__builtin_clz(n & -n) + 1);
|
||||
|
||||
# else
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "emulated.\n");
|
||||
# endif
|
||||
return popcount32((n & -n) - 1);
|
||||
# endif
|
||||
}
|
||||
|
||||
/* clz - count leading zeroes : 00101000 -> 2
|
||||
* ^^
|
||||
*/
|
||||
static __always_inline int clz64(u64 n)
|
||||
{
|
||||
# if __has_builtin(__builtin_clzl)
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "builtin.\n");
|
||||
# endif
|
||||
return __builtin_clzl(n);
|
||||
|
||||
# else
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "emulated.\n");
|
||||
# endif
|
||||
u64 r, q;
|
||||
|
||||
r = (n > 0xFFFFFFFF) << 5; n >>= r;
|
||||
q = (n > 0xFFFF) << 4; n >>= q; r |= q;
|
||||
q = (n > 0xFF ) << 3; n >>= q; r |= q;
|
||||
q = (n > 0xF ) << 2; n >>= q; r |= q;
|
||||
q = (n > 0x3 ) << 1; n >>= q; r |= q;
|
||||
r |= (n >> 1);
|
||||
return 64 - r - 1;
|
||||
# endif
|
||||
}
|
||||
|
||||
static __always_inline int clz32(u32 n)
|
||||
{
|
||||
# if __has_builtin(__builtin_clz)
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "builtin.\n");
|
||||
# endif
|
||||
return __builtin_clz(n);
|
||||
|
||||
# else
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "emulated.\n");
|
||||
# endif
|
||||
u32 r, q;
|
||||
|
||||
r = (n > 0xFFFF) << 4; n >>= r;
|
||||
q = (n > 0xFF ) << 3; n >>= q; r |= q;
|
||||
q = (n > 0xF ) << 2; n >>= q; r |= q;
|
||||
q = (n > 0x3 ) << 1; n >>= q; r |= q;
|
||||
r |= (n >> 1);
|
||||
return 32 - r - 1;
|
||||
# endif
|
||||
}
|
||||
|
||||
/* fls - find last set : 00101000 -> 6
|
||||
* ^
|
||||
*/
|
||||
static __always_inline int fls64(u64 n)
|
||||
{
|
||||
if (!n)
|
||||
return 0;
|
||||
return 64 - clz64(n);
|
||||
}
|
||||
|
||||
static __always_inline int fls32(u32 n)
|
||||
{
|
||||
if (!n)
|
||||
return 0;
|
||||
return 32 - clz32(n);
|
||||
}
|
||||
|
||||
/* find first set : 00101000 -> 4
|
||||
* ^
|
||||
*/
|
||||
static __always_inline uint ffs64(u64 n)
|
||||
{
|
||||
# if __has_builtin(__builtin_ffsl)
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "builtin ffsl.\n");
|
||||
# endif
|
||||
return __builtin_ffsl(n);
|
||||
|
||||
# elif __has_builtin(__builtin_ctzl)
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "builtin ctzl.\n");
|
||||
# endif
|
||||
if (n == 0)
|
||||
return (0);
|
||||
return __builtin_ctzl(n) + 1;
|
||||
|
||||
# else
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "emulated.\n");
|
||||
# endif
|
||||
return popcount64(n ^ ~-n);
|
||||
# endif
|
||||
}
|
||||
|
||||
static __always_inline uint ffs32(u32 n)
|
||||
{
|
||||
# if __has_builtin(__builtin_ffs)
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "builtin ffs.\n");
|
||||
# endif
|
||||
return __builtin_ffs(n);
|
||||
|
||||
# elif __has_builtin(__builtin_ctz)
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "builtin ctz.\n");
|
||||
# endif
|
||||
if (n == 0)
|
||||
return (0);
|
||||
return __builtin_ctz(n) + 1;
|
||||
|
||||
# else
|
||||
# ifdef DEBUG_BITS
|
||||
log_f(1, "emulated.\n");
|
||||
# endif
|
||||
return popcount32(n ^ ~-n);
|
||||
# endif
|
||||
}
|
||||
|
||||
/* rolXX are taken from kernel's <linux/bitops.h> are are:
|
||||
* SPDX-License-Identifier: GPL-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* rol64 - rotate a 64-bit value left
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline u64 rol64(u64 word, unsigned int shift)
|
||||
{
|
||||
return (word << (shift & 63)) | (word >> ((-shift) & 63));
|
||||
}
|
||||
|
||||
/**
|
||||
* ror64 - rotate a 64-bit value right
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline u64 ror64(u64 word, unsigned int shift)
|
||||
{
|
||||
return (word >> (shift & 63)) | (word << ((-shift) & 63));
|
||||
}
|
||||
|
||||
/**
|
||||
* rol32 - rotate a 32-bit value left
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline u32 rol32(u32 word, unsigned int shift)
|
||||
{
|
||||
return (word << (shift & 31)) | (word >> ((-shift) & 31));
|
||||
}
|
||||
|
||||
/**
|
||||
* ror32 - rotate a 32-bit value right
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline u32 ror32(u32 word, unsigned int shift)
|
||||
{
|
||||
return (word >> (shift & 31)) | (word << ((-shift) & 31));
|
||||
}
|
||||
|
||||
/**
|
||||
* rol16 - rotate a 16-bit value left
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline u16 rol16(u16 word, unsigned int shift)
|
||||
{
|
||||
return (word << (shift & 15)) | (word >> ((-shift) & 15));
|
||||
}
|
||||
|
||||
/**
|
||||
* ror16 - rotate a 16-bit value right
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline u16 ror16(u16 word, unsigned int shift)
|
||||
{
|
||||
return (word >> (shift & 15)) | (word << ((-shift) & 15));
|
||||
}
|
||||
|
||||
/**
|
||||
* rol8 - rotate an 8-bit value left
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline u8 rol8(u8 word, unsigned int shift)
|
||||
{
|
||||
return (word << (shift & 7)) | (word >> ((-shift) & 7));
|
||||
}
|
||||
|
||||
/**
|
||||
* ror8 - rotate an 8-bit value right
|
||||
* @word: value to rotate
|
||||
* @shift: bits to roll
|
||||
*/
|
||||
static inline u8 ror8(u8 word, unsigned int shift)
|
||||
{
|
||||
return (word >> (shift & 7)) | (word << ((-shift) & 7));
|
||||
}
|
||||
|
||||
/**
|
||||
* ilog2 -
|
||||
*/
|
||||
static __always_inline __attribute__((const))
|
||||
int __ilog2_u32(u32 n)
|
||||
{
|
||||
return fls32(n) - 1;
|
||||
}
|
||||
|
||||
static __always_inline __attribute__((const))
|
||||
int __ilog2_u64(u64 n)
|
||||
{
|
||||
return fls64(n) - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* is_power_of_2() - check if a value is a power of two
|
||||
* @n: the value to check
|
||||
*
|
||||
* Determine whether some value is a power of two, where zero is
|
||||
* *not* considered a power of two.
|
||||
* Return: true if @n is a power of 2, otherwise false.
|
||||
*/
|
||||
static inline __attribute__((const))
|
||||
bool is_power_of_2(unsigned long n)
|
||||
{
|
||||
return (n != 0 && ((n & (n - 1)) == 0));
|
||||
}
|
||||
|
||||
/**
|
||||
* ilog2 - log base 2 of 32-bit or a 64-bit unsigned value
|
||||
* @n: parameter
|
||||
*
|
||||
* constant-capable log of base 2 calculation
|
||||
* - this can be used to initialise global variables from constant data, hence
|
||||
* the massive ternary operator construction
|
||||
*
|
||||
* selects the appropriately-sized optimised version depending on sizeof(n)
|
||||
*/
|
||||
#define ilog2(n) \
|
||||
( \
|
||||
__builtin_constant_p(n) ? \
|
||||
((n) < 2 ? 0 : \
|
||||
63 - __builtin_clzll(n)) : \
|
||||
(sizeof(n) <= 4) ? \
|
||||
__ilog2_u32(n) : \
|
||||
__ilog2_u64(n) \
|
||||
)
|
||||
|
||||
/**
|
||||
* roundup_pow_of_two - round the given value up to nearest power of two
|
||||
* @n: parameter
|
||||
*
|
||||
* round the given value up to the nearest power of two
|
||||
* - the result is undefined when n == 0
|
||||
* - this can be used to initialise global variables from constant data
|
||||
*/
|
||||
#define roundup_pow_of_two(n) \
|
||||
( \
|
||||
__builtin_constant_p(n) ? ( \
|
||||
((n) == 1) ? 1 : \
|
||||
(1UL << (ilog2((n) - 1) + 1)) \
|
||||
) : \
|
||||
__roundup_pow_of_two(n) \
|
||||
)
|
||||
|
||||
/**
|
||||
* rounddown_pow_of_two - round the given value down to nearest power of two
|
||||
* @n: parameter
|
||||
*
|
||||
* round the given value down to the nearest power of two
|
||||
* - the result is undefined when n == 0
|
||||
* - this can be used to initialise global variables from constant data
|
||||
*/
|
||||
#define rounddown_pow_of_two(n) \
|
||||
( \
|
||||
__builtin_constant_p(n) ? ( \
|
||||
(1UL << ilog2(n))) : \
|
||||
__rounddown_pow_of_two(n) \
|
||||
)
|
||||
|
||||
static inline __attribute_const__
|
||||
int __order_base_2(unsigned long n)
|
||||
{
|
||||
return n > 1 ? ilog2(n - 1) + 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* order_base_2 - calculate the (rounded up) base 2 order of the argument
|
||||
* @n: parameter
|
||||
*
|
||||
* The first few values calculated by this routine:
|
||||
* ob2(0) = 0
|
||||
* ob2(1) = 0
|
||||
* ob2(2) = 1
|
||||
* ob2(3) = 2
|
||||
* ob2(4) = 2
|
||||
* ob2(5) = 3
|
||||
* ... and so on.
|
||||
*/
|
||||
#define order_base_2(n) \
|
||||
( \
|
||||
__builtin_constant_p(n) ? ( \
|
||||
((n) == 0 || (n) == 1) ? 0 : \
|
||||
ilog2((n) - 1) + 1) : \
|
||||
__order_base_2(n) \
|
||||
)
|
||||
|
||||
static inline __attribute__((const))
|
||||
int __bits_per(unsigned long n)
|
||||
{
|
||||
if (n < 2)
|
||||
return 1;
|
||||
if (is_power_of_2(n))
|
||||
return order_base_2(n) + 1;
|
||||
return order_base_2(n);
|
||||
}
|
||||
|
||||
/**
|
||||
* bits_per - calculate the number of bits required for the argument
|
||||
* @n: parameter
|
||||
*
|
||||
* This is constant-capable and can be used for compile time
|
||||
* initializations, e.g bitfields.
|
||||
*
|
||||
* The first few values calculated by this routine:
|
||||
* bf(0) = 1
|
||||
* bf(1) = 1
|
||||
* bf(2) = 2
|
||||
* bf(3) = 2
|
||||
* bf(4) = 3
|
||||
* ... and so on.
|
||||
*/
|
||||
#define bits_per(n) \
|
||||
( \
|
||||
__builtin_constant_p(n) ? ( \
|
||||
((n) == 0 || (n) == 1) \
|
||||
? 1 : ilog2(n) + 1 \
|
||||
) : \
|
||||
__bits_per(n) \
|
||||
)
|
||||
|
||||
/** bit_for_each - iterate over an u64/u32 bits
|
||||
* @pos: an int used as current bit
|
||||
* @tmp: a temp u64/u32 used as temporary storage
|
||||
* @ul: the u64/u32 to loop over
|
||||
*
|
||||
* Usage:
|
||||
* u64 u=139, _t; // u=b10001011
|
||||
* int cur;
|
||||
* bit_for_each64(cur, _t, u) {
|
||||
* printf("%d\n", cur);
|
||||
* }
|
||||
* This will display the position of each bit set in ul: 1, 2, 4, 8
|
||||
*
|
||||
* I should probably re-think the implementation...
|
||||
*/
|
||||
#define bit_for_each64(pos, tmp, ul) \
|
||||
for (tmp = ul, pos = ffs64(tmp); tmp; tmp &= (tmp - 1), pos = ffs64(tmp))
|
||||
|
||||
#define bit_for_each32(pos, tmp, ul) \
|
||||
for (tmp = ul, pos = ffs32(tmp); tmp; tmp &= (tmp - 1), pos = ffs32(tmp))
|
||||
|
||||
/** or would it be more useful (counting bits from zero instead of 1) ?
|
||||
*/
|
||||
#define bit_for_each64_2(pos, tmp, ul) \
|
||||
for (tmp = ul, pos = ctz64(tmp); tmp; tmp ^= 1UL << pos, pos = ctz64(tmp))
|
||||
|
||||
#define bit_for_each32_2(pos, tmp, ul) \
|
||||
for (tmp = ul, pos = ctz32(tmp); tmp; tmp ^= 1U << pos, pos = ctz32(tmp))
|
||||
|
||||
#endif /* _BITS_H */
|
||||
224
include/br.h
224
include/br.h
@@ -1,224 +0,0 @@
|
||||
/* br.h - misc macros.
|
||||
*
|
||||
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
* Some parts are taken from Linux's kernel <linux/kernel.h> and others, and are :
|
||||
* SPDX-License-Identifier: GPL-2.0
|
||||
*
|
||||
* This header contains generic stuff.
|
||||
*/
|
||||
|
||||
#ifndef _BR_H
|
||||
#define _BR_H
|
||||
|
||||
#include "struct-group.h"
|
||||
|
||||
/* Indirect stringification. Doing two levels allows the parameter to be a
|
||||
* macro itself. For example, compile with -DFOO=bar, __stringify(FOO)
|
||||
* converts to "bar".
|
||||
*/
|
||||
#define __stringify_1(x...) #x
|
||||
#define __stringify(x...) __stringify_1(x)
|
||||
|
||||
/* generate a (maybe) unique id.
|
||||
*/
|
||||
#define ___PASTE(x, y) x##y
|
||||
#define __PASTE(x, y) ___PASTE(x, y)
|
||||
#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
|
||||
|
||||
/* unused/used parameters/functions
|
||||
* https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-unused-function-attribute
|
||||
* https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#index-unused-type-attribute
|
||||
* https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-unused-variable-attribute
|
||||
* https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-unused-label-attribute
|
||||
* https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-used-function-attribute
|
||||
* https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-used-variable-attribute
|
||||
*/
|
||||
#define __unused __attribute__((__unused__))
|
||||
#define __used __attribute__((__used__))
|
||||
|
||||
/* see https://lkml.org/lkml/2018/3/20/845 for explanation of this monster
|
||||
*/
|
||||
#define __is_constexpr(x) \
|
||||
(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
|
||||
|
||||
/*
|
||||
* min()/max()/clamp() macros must accomplish three things:
|
||||
*
|
||||
* - avoid multiple evaluations of the arguments (so side-effects like
|
||||
* "x++" happen only once) when non-constant.
|
||||
* - perform strict type-checking (to generate warnings instead of
|
||||
* nasty runtime surprises). See the "unnecessary" pointer comparison
|
||||
* in __typecheck().
|
||||
* - retain result as a constant expressions when called with only
|
||||
* constant expressions (to avoid tripping VLA warnings in stack
|
||||
* allocation usage).
|
||||
*/
|
||||
#define __typecheck(x, y) \
|
||||
(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
|
||||
|
||||
#define __no_side_effects(x, y) \
|
||||
(__is_constexpr(x) && __is_constexpr(y))
|
||||
|
||||
#define __safe_cmp(x, y) \
|
||||
(__typecheck(x, y) && __no_side_effects(x, y))
|
||||
|
||||
#define __cmp(x, y, op) ((x) op (y) ? (x) : (y))
|
||||
|
||||
#define __cmp_once(x, y, unique_x, unique_y, op) ({ \
|
||||
typeof(x) unique_x = (x); \
|
||||
typeof(y) unique_y = (y); \
|
||||
__cmp(unique_x, unique_y, op); })
|
||||
|
||||
#define __careful_cmp(x, y, op) \
|
||||
__builtin_choose_expr(__safe_cmp(x, y), \
|
||||
__cmp(x, y, op), \
|
||||
__cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
|
||||
|
||||
#define __pure __attribute__((__pure__))
|
||||
|
||||
/**
|
||||
* min - return minimum of two values of the same or compatible types
|
||||
* @x: first value
|
||||
* @y: second value
|
||||
*/
|
||||
#define min(x, y) __careful_cmp(x, y, <)
|
||||
|
||||
/**
|
||||
* max - return maximum of two values of the same or compatible types
|
||||
* @x: first value
|
||||
* @y: second value
|
||||
*/
|
||||
#define max(x, y) __careful_cmp(x, y, >)
|
||||
|
||||
/**
|
||||
* min3 - return minimum of three values
|
||||
* @x: first value
|
||||
* @y: second value
|
||||
* @z: third value
|
||||
*/
|
||||
#define min3(x, y, z) min((typeof(x))min(x, y), z)
|
||||
|
||||
/**
|
||||
* max3 - return maximum of three values
|
||||
* @x: first value
|
||||
* @y: second value
|
||||
* @z: third value
|
||||
*/
|
||||
#define max3(x, y, z) max((typeof(x))max(x, y), z)
|
||||
|
||||
/**
|
||||
* min_not_zero - return the minimum that is _not_ zero, unless both are zero
|
||||
* @x: value1
|
||||
* @y: value2
|
||||
*/
|
||||
#define min_not_zero(x, y) ({ \
|
||||
typeof(x) __x = (x); \
|
||||
typeof(y) __y = (y); \
|
||||
__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
|
||||
|
||||
/**
|
||||
* clamp - return a value clamped to a given range with strict typechecking
|
||||
* @val: current value
|
||||
* @lo: lowest allowable value
|
||||
* @hi: highest allowable value
|
||||
*
|
||||
* This macro does strict typechecking of @lo/@hi to make sure they are of the
|
||||
* same type as @val. See the unnecessary pointer comparisons.
|
||||
*/
|
||||
#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
|
||||
|
||||
/*
|
||||
* ..and if you can't take the strict
|
||||
* types, you can specify one yourself.
|
||||
*
|
||||
* Or not use min/max/clamp at all, of course.
|
||||
*/
|
||||
|
||||
/**
|
||||
* min_t - return minimum of two values, using the specified type
|
||||
* @type: data type to use
|
||||
* @x: first value
|
||||
* @y: second value
|
||||
*/
|
||||
#define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <)
|
||||
|
||||
/**
|
||||
* max_t - return maximum of two values, using the specified type
|
||||
* @type: data type to use
|
||||
* @x: first value
|
||||
* @y: second value
|
||||
*/
|
||||
#define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >)
|
||||
|
||||
/**
|
||||
* clamp_t - return a value clamped to a given range using a given type
|
||||
* @type: the type of variable to use
|
||||
* @val: current value
|
||||
* @lo: minimum allowable value
|
||||
* @hi: maximum allowable value
|
||||
*
|
||||
* This macro does no typechecking and uses temporary variables of type
|
||||
* @type to make all the comparisons.
|
||||
*/
|
||||
#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
|
||||
|
||||
/**
|
||||
* clamp_val - return a value clamped to a given range using val's type
|
||||
* @val: current value
|
||||
* @lo: minimum allowable value
|
||||
* @hi: maximum allowable value
|
||||
*
|
||||
* This macro does no typechecking and uses temporary variables of whatever
|
||||
* type the input argument @val is. This is useful when @val is an unsigned
|
||||
* type and @lo and @hi are literals that will otherwise be assigned a signed
|
||||
* integer type.
|
||||
*/
|
||||
#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
|
||||
|
||||
/**
|
||||
* swap - swap values of @a and @b
|
||||
* @a: first value
|
||||
* @b: second value
|
||||
*/
|
||||
#define swap(a, b) \
|
||||
do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
|
||||
|
||||
/**
|
||||
* ARRAY_SIZE - get the number of elements in array @arr
|
||||
* @arr: array to be sized
|
||||
*/
|
||||
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
|
||||
|
||||
/**
|
||||
* abs - return absolute value of an argument
|
||||
* @x: the value. If it is unsigned type, it is converted to signed type first.
|
||||
* char is treated as if it was signed (regardless of whether it really is)
|
||||
* but the macro's return type is preserved as char.
|
||||
*
|
||||
* Return: an absolute value of x.
|
||||
*/
|
||||
#define abs(x) __abs_choose_expr(x, long long, \
|
||||
__abs_choose_expr(x, long, \
|
||||
__abs_choose_expr(x, int, \
|
||||
__abs_choose_expr(x, short, \
|
||||
__abs_choose_expr(x, char, \
|
||||
__builtin_choose_expr( \
|
||||
__builtin_types_compatible_p(typeof(x), char), \
|
||||
(char)({ signed char __x = (x); __x<0?-__x:__x; }), \
|
||||
((void)0)))))))
|
||||
|
||||
#define __abs_choose_expr(x, type, other) __builtin_choose_expr( \
|
||||
__builtin_types_compatible_p(typeof(x), signed type) || \
|
||||
__builtin_types_compatible_p(typeof(x), unsigned type), \
|
||||
({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
|
||||
|
||||
|
||||
#endif /* _BR_H */
|
||||
@@ -1,71 +0,0 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
#ifndef _BR_BUG_H
|
||||
#define _BR_BUG_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include "likely.h"
|
||||
#include "debug.h"
|
||||
|
||||
/* BUG functions inspired by Linux kernel's <asm/bug.h>
|
||||
*/
|
||||
|
||||
#define panic() exit(0xff)
|
||||
|
||||
/*
|
||||
* Don't use BUG() or BUG_ON() unless there's really no way out; one
|
||||
* example might be detecting data structure corruption in the middle
|
||||
* of an operation that can't be backed out of. If the (sub)system
|
||||
* can somehow continue operating, perhaps with reduced functionality,
|
||||
* it's probably not BUG-worthy.
|
||||
*
|
||||
* If you're tempted to BUG(), think again: is completely giving up
|
||||
* really the *only* solution? There are usually better options, where
|
||||
* users don't need to reboot ASAP and can mostly shut down cleanly.
|
||||
*/
|
||||
#define BUG() do { \
|
||||
fprintf(stderr, "BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
|
||||
panic(); \
|
||||
} while (0)
|
||||
|
||||
#define BUG_ON(condition) do { if (unlikely(condition)) BUG(); } while (0)
|
||||
|
||||
/*
|
||||
* WARN(), WARN_ON(), WARN_ON_ONCE, and so on can be used to report
|
||||
* significant kernel issues that need prompt attention if they should ever
|
||||
* appear at runtime.
|
||||
*
|
||||
* Do not use these macros when checking for invalid external inputs
|
||||
* (e.g. invalid system call arguments, or invalid data coming from
|
||||
* network/devices), and on transient conditions like ENOMEM or EAGAIN.
|
||||
* These macros should be used for recoverable kernel issues only.
|
||||
* For invalid external inputs, transient conditions, etc use
|
||||
* pr_err[_once/_ratelimited]() followed by dump_stack(), if necessary.
|
||||
* Do not include "BUG"/"WARNING" in format strings manually to make these
|
||||
* conditions distinguishable from kernel issues.
|
||||
*
|
||||
* Use the versions with printk format strings to provide better diagnostics.
|
||||
*/
|
||||
#define __WARN() do { \
|
||||
fprintf(stderr, "WARNING: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
|
||||
} while (0)
|
||||
#define __WARN_printf(arg...) do { \
|
||||
vfprintf(stderr, arg); \
|
||||
} while (0)
|
||||
|
||||
#define WARN_ON(condition) ({ \
|
||||
int __ret_warn_on = !!(condition); \
|
||||
if (unlikely(__ret_warn_on)) \
|
||||
__WARN(); \
|
||||
unlikely(__ret_warn_on); \
|
||||
})
|
||||
|
||||
#define WARN(condition, format...) ({ \
|
||||
int __ret_warn_on = !!(condition); \
|
||||
if (unlikely(__ret_warn_on)) \
|
||||
__WARN_printf(format); \
|
||||
unlikely(__ret_warn_on); \
|
||||
})
|
||||
|
||||
#endif /* _BR_BUG_H */
|
||||
@@ -1,30 +0,0 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
|
||||
/* adaptation of Linux kernel's <linux/container_of.h>
|
||||
*/
|
||||
#ifndef _BR_CONTAINER_OF_H
|
||||
#define _BR_CONTAINER_OF_H
|
||||
|
||||
/* Are two types/vars the same type (ignoring qualifiers)? */
|
||||
#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
|
||||
|
||||
/**
|
||||
* typeof_member -
|
||||
*/
|
||||
#define typeof_member(T, m) typeof(((T*)0)->m)
|
||||
|
||||
/**
|
||||
* container_of - cast a member of a structure out to the containing structure
|
||||
* @ptr: the pointer to the member.
|
||||
* @type: the type of the container struct this is embedded in.
|
||||
* @member: the name of the member within the struct.
|
||||
*
|
||||
*/
|
||||
#define container_of(ptr, type, member) ({ \
|
||||
void *__mptr = (void *)(ptr); \
|
||||
_Static_assert(__same_type(*(ptr), ((type *)0)->member) || \
|
||||
__same_type(*(ptr), void), \
|
||||
"pointer type mismatch in container_of()"); \
|
||||
((type *)(__mptr - offsetof(type, member))); })
|
||||
|
||||
#endif /* BR_CONTAINER_OF_H */
|
||||
115
include/debug.h
115
include/debug.h
@@ -1,115 +0,0 @@
|
||||
/* debug.h - debug/log management.
|
||||
*
|
||||
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef DEBUG_H
|
||||
#define DEBUG_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <br.h>
|
||||
#include <bits.h>
|
||||
|
||||
#define NANOSEC 1000000000 /* nano sec in sec */
|
||||
#define MILLISEC 1000000 /* milli sec in sec */
|
||||
|
||||
#define _printf __attribute__ ((format (printf, 6, 7)))
|
||||
|
||||
#ifdef DEBUG_DEBUG
|
||||
|
||||
void debug_init(uint level, FILE *stream, bool flush);
|
||||
void debug_level_set(uint level);
|
||||
uint debug_level_get(void);
|
||||
void debug_stream_set(FILE *stream);
|
||||
long long debug_timer_elapsed(void);
|
||||
void debug_flush_set(bool flush);
|
||||
void _printf debug(uint level, bool timestamp,
|
||||
uint indent, const char *src,
|
||||
uint line, const char *fmt, ...);
|
||||
#else /* DEBUG_DEBUG */
|
||||
|
||||
static inline void debug_init(__unused uint level,
|
||||
__unused FILE *stream,
|
||||
_unused bool flush) {}
|
||||
static inline void debug_level_set(__unused uint level) {}
|
||||
static inline uint debug_level_get(void) {return 0;}
|
||||
static inline void debug_stream_set(__unused FILE *stream) {}
|
||||
static inline long long debug_timer_elapsed(void) {return 0LL};
|
||||
static inline void debug_flush_set(__unused bool level) {}
|
||||
static inline void _printf debug(__unused uint level, __unused bool timestamp,
|
||||
__unused uint indent, __unused const char *src,
|
||||
__unused uint line, __unused const char *fmt, ...) {}
|
||||
|
||||
#endif /* DEBUG_DEBUG */
|
||||
|
||||
#undef _printf
|
||||
|
||||
/**
|
||||
* log - simple log (no function name, no indent, no timestamp)
|
||||
* @level: log level
|
||||
* @fmt: printf format string
|
||||
* @args: subsequent arguments to printf
|
||||
*/
|
||||
#define log(level, fmt, args...) \
|
||||
debug((level), false, 0, NULL, 0, fmt, ##args)
|
||||
|
||||
/**
|
||||
* log_i - log with indent (no function name, no timestamp)
|
||||
* @level: log level
|
||||
* @fmt: printf format string
|
||||
* @args: subsequent arguments to printf
|
||||
*
|
||||
* Output example:
|
||||
* >>>>val=2
|
||||
*/
|
||||
#define log_i(level, fmt, args...) \
|
||||
debug((level), false, (level), NULL, 0, fmt, ##args)
|
||||
|
||||
/**
|
||||
* log_f - log with function name (no indent name, no timestamp)
|
||||
* @level: log level
|
||||
* @fmt: printf format string
|
||||
* @args: subsequent arguments to printf
|
||||
*
|
||||
* Output example:
|
||||
* [function] val=2
|
||||
*/
|
||||
#define log_f(level, fmt, args...) \
|
||||
debug((level), false, 0, __func__, 0, fmt, ##args)
|
||||
|
||||
/**
|
||||
* log_if - log with function name and line number (no indent name, no timestamp)
|
||||
* @level: log level
|
||||
* @fmt: printf format string
|
||||
* @args: subsequent arguments to printf
|
||||
*
|
||||
* Output example:
|
||||
* >>>> [function:15] val=2
|
||||
*/
|
||||
#define log_if(level, fmt, args...) \
|
||||
debug((level), false, (level), __func__, __LINE__, fmt, ##args)
|
||||
|
||||
/**
|
||||
* log_it - log with function name, line number, indent, and timestamp
|
||||
* @level: log level
|
||||
* @fmt: printf format string
|
||||
* @args: subsequent arguments to printf
|
||||
*
|
||||
* Output example:
|
||||
* >>>> [function:15] val=2
|
||||
*/
|
||||
#define log_it(level, fmt, args...) \
|
||||
debug((level), true, (level), __func__, __LINE__, fmt, ##args)
|
||||
|
||||
#endif /* DEBUG_H */
|
||||
172
include/hash.h
172
include/hash.h
@@ -1,172 +0,0 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
|
||||
#ifndef _BR_HASH_H
|
||||
#define _BR_HASH_H
|
||||
/* adaptation of Linux kernel's <linux/hash.h> and <linux/stringhash.h>
|
||||
*/
|
||||
|
||||
/* Fast hashing routine for ints, longs and pointers.
|
||||
(C) 2002 Nadia Yvette Chambers, IBM */
|
||||
|
||||
#include <asm/bitsperlong.h>
|
||||
#include "bits.h"
|
||||
#include "br.h"
|
||||
|
||||
/*
|
||||
* The "GOLDEN_RATIO_PRIME" is used in ifs/btrfs/brtfs_inode.h and
|
||||
* fs/inode.c. It's not actually prime any more (the previous primes
|
||||
* were actively bad for hashing), but the name remains.
|
||||
*/
|
||||
#if __BITS_PER_LONG == 32
|
||||
#define GOLDEN_RATIO_PRIME GOLDEN_RATIO_32
|
||||
#define hash_long(val, bits) hash_32(val, bits)
|
||||
#elif __BITS_PER_LONG == 64
|
||||
#define hash_long(val, bits) hash_64(val, bits)
|
||||
#define GOLDEN_RATIO_PRIME GOLDEN_RATIO_64
|
||||
#else
|
||||
#error Wordsize not 32 or 64
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This hash multiplies the input by a large odd number and takes the
|
||||
* high bits. Since multiplication propagates changes to the most
|
||||
* significant end only, it is essential that the high bits of the
|
||||
* product be used for the hash value.
|
||||
*
|
||||
* Chuck Lever verified the effectiveness of this technique:
|
||||
* http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
|
||||
*
|
||||
* Although a random odd number will do, it turns out that the golden
|
||||
* ratio phi = (sqrt(5)-1)/2, or its negative, has particularly nice
|
||||
* properties. (See Knuth vol 3, section 6.4, exercise 9.)
|
||||
*
|
||||
* These are the negative, (1 - phi) = phi**2 = (3 - sqrt(5))/2,
|
||||
* which is very slightly easier to multiply by and makes no
|
||||
* difference to the hash distribution.
|
||||
*/
|
||||
#define GOLDEN_RATIO_32 0x61C88647
|
||||
#define GOLDEN_RATIO_64 0x61C8864680B583EBull
|
||||
|
||||
/*
|
||||
* The _generic versions exist only so lib/test_hash.c can compare
|
||||
* the arch-optimized versions with the generic.
|
||||
*
|
||||
* Note that if you change these, any <asm/hash.h> that aren't updated
|
||||
* to match need to have their HAVE_ARCH_* define values updated so the
|
||||
* self-test will not false-positive.
|
||||
*/
|
||||
#ifndef HAVE_ARCH__HASH_32
|
||||
#define __hash_32 __hash_32_generic
|
||||
#endif
|
||||
static inline u32 __hash_32_generic(u32 val)
|
||||
{
|
||||
return val * GOLDEN_RATIO_32;
|
||||
}
|
||||
|
||||
static inline u32 hash_32(u32 val, unsigned int bits)
|
||||
{
|
||||
/* High bits are more random, so use them. */
|
||||
return __hash_32(val) >> (32 - bits);
|
||||
}
|
||||
|
||||
#ifndef HAVE_ARCH_HASH_64
|
||||
#define hash_64 hash_64_generic
|
||||
#endif
|
||||
static __always_inline u32 hash_64_generic(u64 val, unsigned int bits)
|
||||
{
|
||||
#if __BITS_PER_LONG == 64
|
||||
/* 64x64-bit multiply is efficient on all 64-bit processors */
|
||||
return val * GOLDEN_RATIO_64 >> (64 - bits);
|
||||
#else
|
||||
/* Hash 64 bits using only 32x32-bit multiply. */
|
||||
return hash_32((u32)val ^ __hash_32(val >> 32), bits);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline u32 hash_ptr(const void *ptr, unsigned int bits)
|
||||
{
|
||||
return hash_long((unsigned long)ptr, bits);
|
||||
}
|
||||
|
||||
/* This really should be called fold32_ptr; it does no hashing to speak of. */
|
||||
static inline u32 hash32_ptr(const void *ptr)
|
||||
{
|
||||
unsigned long val = (unsigned long)ptr;
|
||||
|
||||
#if __BITS_PER_LONG == 64
|
||||
val ^= (val >> 32);
|
||||
#endif
|
||||
return (u32)val;
|
||||
}
|
||||
|
||||
/*
|
||||
* Routines for hashing strings of bytes to a 32-bit hash value.
|
||||
*
|
||||
* These hash functions are NOT GUARANTEED STABLE between kernel
|
||||
* versions, architectures, or even repeated boots of the same kernel.
|
||||
* (E.g. they may depend on boot-time hardware detection or be
|
||||
* deliberately randomized.)
|
||||
*
|
||||
* They are also not intended to be secure against collisions caused by
|
||||
* malicious inputs; much slower hash functions are required for that.
|
||||
*
|
||||
* They are optimized for pathname components, meaning short strings.
|
||||
* Even if a majority of files have longer names, the dynamic profile of
|
||||
* pathname components skews short due to short directory names.
|
||||
* (E.g. /usr/lib/libsesquipedalianism.so.3.141.)
|
||||
*/
|
||||
|
||||
/*
|
||||
* Version 1: one byte at a time. Example of use:
|
||||
*
|
||||
* unsigned long hash = init_name_hash;
|
||||
* while (*p)
|
||||
* hash = partial_name_hash(tolower(*p++), hash);
|
||||
* hash = end_name_hash(hash);
|
||||
*
|
||||
* Although this is designed for bytes, fs/hfsplus/unicode.c
|
||||
* abuses it to hash 16-bit values.
|
||||
*/
|
||||
|
||||
/* Hash courtesy of the R5 hash in reiserfs modulo sign bits */
|
||||
#define init_name_hash(salt) (unsigned long)(salt)
|
||||
|
||||
/* partial hash update function. Assume roughly 4 bits per character */
|
||||
static inline unsigned long
|
||||
partial_name_hash(unsigned long c, unsigned long prevhash)
|
||||
{
|
||||
return (prevhash + (c << 4) + (c >> 4)) * 11;
|
||||
}
|
||||
|
||||
/*
|
||||
* Finally: cut down the number of bits to a int value (and try to avoid
|
||||
* losing bits). This also has the property (wanted by the dcache)
|
||||
* that the msbits make a good hash table index.
|
||||
*/
|
||||
static inline unsigned int end_name_hash(unsigned long hash)
|
||||
{
|
||||
return hash_long(hash, 32);
|
||||
}
|
||||
|
||||
/*
|
||||
* Version 2: One word (32 or 64 bits) at a time.
|
||||
* If CONFIG_DCACHE_WORD_ACCESS is defined (meaning <asm/word-at-a-time.h>
|
||||
* exists, which describes major Linux platforms like x86 and ARM), then
|
||||
* this computes a different hash function much faster.
|
||||
*
|
||||
* If not set, this falls back to a wrapper around the preceding.
|
||||
*/
|
||||
extern unsigned int __pure hash_string(const void *salt, const char *, unsigned int);
|
||||
|
||||
/*
|
||||
* A hash_len is a u64 with the hash of a string in the low
|
||||
* half and the length in the high half.
|
||||
*/
|
||||
#define hashlen_hash(hashlen) ((u32)(hashlen))
|
||||
#define hashlen_len(hashlen) ((u32)((hashlen) >> 32))
|
||||
#define hashlen_create(hash, len) ((u64)(len)<<32 | (u32)(hash))
|
||||
|
||||
/* Return the "hash_len" (hash and length) of a null-terminated string */
|
||||
extern u64 __pure hashlen_string(const void *salt, const char *name);
|
||||
|
||||
#endif /* _BR_HASH_H */
|
||||
@@ -1,202 +0,0 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
/* adaptation of Linux kernel's <linux/hashtable.h>
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Statically sized hash table implementation
|
||||
* (C) 2012 Sasha Levin <levinsasha928@gmail.com>
|
||||
*/
|
||||
|
||||
#ifndef _LINUX_HASHTABLE_H
|
||||
#define _LINUX_HASHTABLE_H
|
||||
|
||||
#include "list.h"
|
||||
#include "hash.h"
|
||||
//#include <linux/rculist.h>
|
||||
|
||||
#define DEFINE_HASHTABLE(name, bits) \
|
||||
struct hlist_head name[1 << (bits)] = \
|
||||
{ [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
|
||||
|
||||
#define DEFINE_READ_MOSTLY_HASHTABLE(name, bits) \
|
||||
struct hlist_head name[1 << (bits)] __read_mostly = \
|
||||
{ [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
|
||||
|
||||
#define DECLARE_HASHTABLE(name, bits) \
|
||||
struct hlist_head name[1 << (bits)]
|
||||
|
||||
#define HASH_SIZE(name) (ARRAY_SIZE(name))
|
||||
#define HASH_BITS(name) ilog2(HASH_SIZE(name))
|
||||
|
||||
/* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
|
||||
#define hash_min(val, bits) \
|
||||
(sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
|
||||
|
||||
static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < sz; i++)
|
||||
INIT_HLIST_HEAD(&ht[i]);
|
||||
}
|
||||
|
||||
/**
|
||||
* hash_init - initialize a hash table
|
||||
* @hashtable: hashtable to be initialized
|
||||
*
|
||||
* Calculates the size of the hashtable from the given parameter, otherwise
|
||||
* same as hash_init_size.
|
||||
*
|
||||
* This has to be a macro since HASH_BITS() will not work on pointers since
|
||||
* it calculates the size during preprocessing.
|
||||
*/
|
||||
#define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
|
||||
|
||||
/**
|
||||
* hash_add - add an object to a hashtable
|
||||
* @hashtable: hashtable to add to
|
||||
* @node: the &struct hlist_node of the object to be added
|
||||
* @key: the key of the object to be added
|
||||
*/
|
||||
#define hash_add(hashtable, node, key) \
|
||||
hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
|
||||
|
||||
/**
|
||||
* hash_add_rcu - add an object to a rcu enabled hashtable
|
||||
* @hashtable: hashtable to add to
|
||||
* @node: the &struct hlist_node of the object to be added
|
||||
* @key: the key of the object to be added
|
||||
*/
|
||||
#define hash_add_rcu(hashtable, node, key) \
|
||||
hlist_add_head_rcu(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
|
||||
|
||||
/**
|
||||
* hash_hashed - check whether an object is in any hashtable
|
||||
* @node: the &struct hlist_node of the object to be checked
|
||||
*/
|
||||
static inline bool hash_hashed(struct hlist_node *node)
|
||||
{
|
||||
return !hlist_unhashed(node);
|
||||
}
|
||||
|
||||
static inline bool __hash_empty(struct hlist_head *ht, unsigned int sz)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < sz; i++)
|
||||
if (!hlist_empty(&ht[i]))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* hash_empty - check whether a hashtable is empty
|
||||
* @hashtable: hashtable to check
|
||||
*
|
||||
* This has to be a macro since HASH_BITS() will not work on pointers since
|
||||
* it calculates the size during preprocessing.
|
||||
*/
|
||||
#define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
|
||||
|
||||
/**
|
||||
* hash_del - remove an object from a hashtable
|
||||
* @node: &struct hlist_node of the object to remove
|
||||
*/
|
||||
static inline void hash_del(struct hlist_node *node)
|
||||
{
|
||||
hlist_del_init(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* hash_for_each - iterate over a hashtable
|
||||
* @name: hashtable to iterate
|
||||
* @bkt: integer to use as bucket loop cursor
|
||||
* @obj: the type * to use as a loop cursor for each entry
|
||||
* @member: the name of the hlist_node within the struct
|
||||
*/
|
||||
#define hash_for_each(name, bkt, obj, member) \
|
||||
for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
|
||||
(bkt)++)\
|
||||
hlist_for_each_entry(obj, &name[bkt], member)
|
||||
|
||||
/**
|
||||
* hash_for_each_rcu - iterate over a rcu enabled hashtable
|
||||
* @name: hashtable to iterate
|
||||
* @bkt: integer to use as bucket loop cursor
|
||||
* @obj: the type * to use as a loop cursor for each entry
|
||||
* @member: the name of the hlist_node within the struct
|
||||
*/
|
||||
#define hash_for_each_rcu(name, bkt, obj, member) \
|
||||
for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
|
||||
(bkt)++)\
|
||||
hlist_for_each_entry_rcu(obj, &name[bkt], member)
|
||||
|
||||
/**
|
||||
* hash_for_each_safe - iterate over a hashtable safe against removal of
|
||||
* hash entry
|
||||
* @name: hashtable to iterate
|
||||
* @bkt: integer to use as bucket loop cursor
|
||||
* @tmp: a &struct hlist_node used for temporary storage
|
||||
* @obj: the type * to use as a loop cursor for each entry
|
||||
* @member: the name of the hlist_node within the struct
|
||||
*/
|
||||
#define hash_for_each_safe(name, bkt, tmp, obj, member) \
|
||||
for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
|
||||
(bkt)++)\
|
||||
hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
|
||||
|
||||
/**
|
||||
* hash_for_each_possible - iterate over all possible objects hashing to the
|
||||
* same bucket
|
||||
* @name: hashtable to iterate
|
||||
* @obj: the type * to use as a loop cursor for each entry
|
||||
* @member: the name of the hlist_node within the struct
|
||||
* @key: the key of the objects to iterate over
|
||||
*/
|
||||
#define hash_for_each_possible(name, obj, member, key) \
|
||||
hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
|
||||
|
||||
/**
|
||||
* hash_for_each_possible_rcu - iterate over all possible objects hashing to the
|
||||
* same bucket in an rcu enabled hashtable
|
||||
* @name: hashtable to iterate
|
||||
* @obj: the type * to use as a loop cursor for each entry
|
||||
* @member: the name of the hlist_node within the struct
|
||||
* @key: the key of the objects to iterate over
|
||||
*/
|
||||
#define hash_for_each_possible_rcu(name, obj, member, key, cond...) \
|
||||
hlist_for_each_entry_rcu(obj, &name[hash_min(key, HASH_BITS(name))],\
|
||||
member, ## cond)
|
||||
|
||||
/**
|
||||
* hash_for_each_possible_rcu_notrace - iterate over all possible objects hashing
|
||||
* to the same bucket in an rcu enabled hashtable in a rcu enabled hashtable
|
||||
* @name: hashtable to iterate
|
||||
* @obj: the type * to use as a loop cursor for each entry
|
||||
* @member: the name of the hlist_node within the struct
|
||||
* @key: the key of the objects to iterate over
|
||||
*
|
||||
* This is the same as hash_for_each_possible_rcu() except that it does
|
||||
* not do any RCU debugging or tracing.
|
||||
*/
|
||||
#define hash_for_each_possible_rcu_notrace(name, obj, member, key) \
|
||||
hlist_for_each_entry_rcu_notrace(obj, \
|
||||
&name[hash_min(key, HASH_BITS(name))], member)
|
||||
|
||||
/**
|
||||
* hash_for_each_possible_safe - iterate over all possible objects hashing to the
|
||||
* same bucket safe against removals
|
||||
* @name: hashtable to iterate
|
||||
* @obj: the type * to use as a loop cursor for each entry
|
||||
* @tmp: a &struct hlist_node used for temporary storage
|
||||
* @member: the name of the hlist_node within the struct
|
||||
* @key: the key of the objects to iterate over
|
||||
*/
|
||||
#define hash_for_each_possible_safe(name, obj, tmp, member, key) \
|
||||
hlist_for_each_entry_safe(obj, tmp,\
|
||||
&name[hash_min(key, HASH_BITS(name))], member)
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,18 +0,0 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
|
||||
/* taken from Kernel's <linux/compiler.h
|
||||
*/
|
||||
#ifndef __LIKELY_H
|
||||
#define __LIKELY_H
|
||||
|
||||
/* See https://kernelnewbies.org/FAQ/LikelyUnlikely
|
||||
*
|
||||
* In 2 words:
|
||||
* "You should use it [likely() and unlikely()] only in cases when the likeliest
|
||||
* branch is very very very likely, or when the unlikeliest branch is very very
|
||||
* very unlikely."
|
||||
*/
|
||||
# define likely(x) __builtin_expect(!!(x), 1)
|
||||
# define unlikely(x) __builtin_expect(!!(x), 0)
|
||||
|
||||
#endif /* __LIKELY_H */
|
||||
992
include/list.h
992
include/list.h
@@ -1,992 +0,0 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
|
||||
/* adaptation of kernel's <linux/list.h>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __BR_LIST_H
|
||||
#define __BR_LIST_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include "rwonce.h"
|
||||
#include "container-of.h"
|
||||
|
||||
/************ originally in <include/linux/types.h> */
|
||||
struct list_head {
|
||||
struct list_head *next, *prev;
|
||||
};
|
||||
struct hlist_head {
|
||||
struct hlist_node *first;
|
||||
};
|
||||
|
||||
struct hlist_node {
|
||||
struct hlist_node *next, **pprev;
|
||||
};
|
||||
|
||||
/************ originally in <include/linux/poison.h> */
|
||||
# define POISON_POINTER_DELTA 0
|
||||
/* These are non-NULL pointers that will result in page faults
|
||||
* under normal circumstances, used to verify that nobody uses
|
||||
* non-initialized list entries.
|
||||
*/
|
||||
#define LIST_POISON1 ((void *) 0x100 + POISON_POINTER_DELTA)
|
||||
#define LIST_POISON2 ((void *) 0x200 + POISON_POINTER_DELTA)
|
||||
|
||||
/*
|
||||
* Circular doubly linked list implementation.
|
||||
*
|
||||
* Some of the internal functions ("__xxx") are useful when
|
||||
* manipulating whole lists rather than single entries, as
|
||||
* sometimes we already know the next/prev entries and we can
|
||||
* generate better code by using them directly rather than
|
||||
* using the generic single-entry routines.
|
||||
*/
|
||||
|
||||
#define LIST_HEAD_INIT(name) { &(name), &(name) }
|
||||
|
||||
#define LIST_HEAD(name) \
|
||||
struct list_head name = LIST_HEAD_INIT(name)
|
||||
|
||||
/**
|
||||
* INIT_LIST_HEAD - Initialize a list_head structure
|
||||
* @list: list_head structure to be initialized.
|
||||
*
|
||||
* Initializes the list_head to point to itself. If it is a list header,
|
||||
* the result is an empty list.
|
||||
*/
|
||||
static inline void INIT_LIST_HEAD(struct list_head *list)
|
||||
{
|
||||
WRITE_ONCE(list->next, list);
|
||||
list->prev = list;
|
||||
}
|
||||
|
||||
/*
|
||||
* Insert a new entry between two known consecutive entries.
|
||||
*
|
||||
* This is only for internal list manipulation where we know
|
||||
* the prev/next entries already!
|
||||
*/
|
||||
static inline void __list_add(struct list_head *new,
|
||||
struct list_head *prev,
|
||||
struct list_head *next)
|
||||
{
|
||||
next->prev = new;
|
||||
new->next = next;
|
||||
new->prev = prev;
|
||||
WRITE_ONCE(prev->next, new);
|
||||
}
|
||||
|
||||
/**
|
||||
* list_add - add a new entry
|
||||
* @new: new entry to be added
|
||||
* @head: list head to add it after
|
||||
*
|
||||
* Insert a new entry after the specified head.
|
||||
* This is good for implementing stacks.
|
||||
*/
|
||||
static inline void list_add(struct list_head *new, struct list_head *head)
|
||||
{
|
||||
__list_add(new, head, head->next);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* list_add_tail - add a new entry
|
||||
* @new: new entry to be added
|
||||
* @head: list head to add it before
|
||||
*
|
||||
* Insert a new entry before the specified head.
|
||||
* This is useful for implementing queues.
|
||||
*/
|
||||
static inline void list_add_tail(struct list_head *new, struct list_head *head)
|
||||
{
|
||||
__list_add(new, head->prev, head);
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete a list entry by making the prev/next entries
|
||||
* point to each other.
|
||||
*
|
||||
* This is only for internal list manipulation where we know
|
||||
* the prev/next entries already!
|
||||
*/
|
||||
static inline void __list_del(struct list_head * prev, struct list_head * next)
|
||||
{
|
||||
next->prev = prev;
|
||||
WRITE_ONCE(prev->next, next);
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete a list entry and clear the 'prev' pointer.
|
||||
*
|
||||
* This is a special-purpose list clearing method used in the networking code
|
||||
* for lists allocated as per-cpu, where we don't want to incur the extra
|
||||
* WRITE_ONCE() overhead of a regular list_del_init(). The code that uses this
|
||||
* needs to check the node 'prev' pointer instead of calling list_empty().
|
||||
*/
|
||||
static inline void __list_del_clearprev(struct list_head *entry)
|
||||
{
|
||||
__list_del(entry->prev, entry->next);
|
||||
entry->prev = NULL;
|
||||
}
|
||||
|
||||
static inline void __list_del_entry(struct list_head *entry)
|
||||
{
|
||||
__list_del(entry->prev, entry->next);
|
||||
}
|
||||
|
||||
/**
|
||||
* list_del - deletes entry from list.
|
||||
* @entry: the element to delete from the list.
|
||||
* Note: list_empty() on entry does not return true after this, the entry is
|
||||
* in an undefined state.
|
||||
*/
|
||||
static inline void list_del(struct list_head *entry)
|
||||
{
|
||||
__list_del_entry(entry);
|
||||
entry->next = LIST_POISON1;
|
||||
entry->prev = LIST_POISON2;
|
||||
}
|
||||
|
||||
/**
|
||||
* list_replace - replace old entry by new one
|
||||
* @old : the element to be replaced
|
||||
* @new : the new element to insert
|
||||
*
|
||||
* If @old was empty, it will be overwritten.
|
||||
*/
|
||||
static inline void list_replace(struct list_head *old,
|
||||
struct list_head *new)
|
||||
{
|
||||
new->next = old->next;
|
||||
new->next->prev = new;
|
||||
new->prev = old->prev;
|
||||
new->prev->next = new;
|
||||
}
|
||||
|
||||
/**
|
||||
* list_replace_init - replace old entry by new one and initialize the old one
|
||||
* @old : the element to be replaced
|
||||
* @new : the new element to insert
|
||||
*
|
||||
* If @old was empty, it will be overwritten.
|
||||
*/
|
||||
static inline void list_replace_init(struct list_head *old,
|
||||
struct list_head *new)
|
||||
{
|
||||
list_replace(old, new);
|
||||
INIT_LIST_HEAD(old);
|
||||
}
|
||||
|
||||
/**
|
||||
* list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position
|
||||
* @entry1: the location to place entry2
|
||||
* @entry2: the location to place entry1
|
||||
*/
|
||||
static inline void list_swap(struct list_head *entry1,
|
||||
struct list_head *entry2)
|
||||
{
|
||||
struct list_head *pos = entry2->prev;
|
||||
|
||||
list_del(entry2);
|
||||
list_replace(entry1, entry2);
|
||||
if (pos == entry1)
|
||||
pos = entry2;
|
||||
list_add(entry1, pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* list_del_init - deletes entry from list and reinitialize it.
|
||||
* @entry: the element to delete from the list.
|
||||
*/
|
||||
static inline void list_del_init(struct list_head *entry)
|
||||
{
|
||||
__list_del_entry(entry);
|
||||
INIT_LIST_HEAD(entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* list_move - delete from one list and add as another's head
|
||||
* @list: the entry to move
|
||||
* @head: the head that will precede our entry
|
||||
*/
|
||||
static inline void list_move(struct list_head *list, struct list_head *head)
|
||||
{
|
||||
__list_del_entry(list);
|
||||
list_add(list, head);
|
||||
}
|
||||
|
||||
/**
|
||||
* list_move_tail - delete from one list and add as another's tail
|
||||
* @list: the entry to move
|
||||
* @head: the head that will follow our entry
|
||||
*/
|
||||
static inline void list_move_tail(struct list_head *list,
|
||||
struct list_head *head)
|
||||
{
|
||||
__list_del_entry(list);
|
||||
list_add_tail(list, head);
|
||||
}
|
||||
|
||||
/**
|
||||
* list_bulk_move_tail - move a subsection of a list to its tail
|
||||
* @head: the head that will follow our entry
|
||||
* @first: first entry to move
|
||||
* @last: last entry to move, can be the same as first
|
||||
*
|
||||
* Move all entries between @first and including @last before @head.
|
||||
* All three entries must belong to the same linked list.
|
||||
*/
|
||||
static inline void list_bulk_move_tail(struct list_head *head,
|
||||
struct list_head *first,
|
||||
struct list_head *last)
|
||||
{
|
||||
first->prev->next = last->next;
|
||||
last->next->prev = first->prev;
|
||||
|
||||
head->prev->next = first;
|
||||
first->prev = head->prev;
|
||||
|
||||
last->next = head;
|
||||
head->prev = last;
|
||||
}
|
||||
|
||||
/**
|
||||
* list_is_first -- tests whether @list is the first entry in list @head
|
||||
* @list: the entry to test
|
||||
* @head: the head of the list
|
||||
*/
|
||||
static inline int list_is_first(const struct list_head *list,
|
||||
const struct list_head *head)
|
||||
{
|
||||
return list->prev == head;
|
||||
}
|
||||
|
||||
/**
|
||||
* list_is_last - tests whether @list is the last entry in list @head
|
||||
* @list: the entry to test
|
||||
* @head: the head of the list
|
||||
*/
|
||||
static inline int list_is_last(const struct list_head *list,
|
||||
const struct list_head *head)
|
||||
{
|
||||
return list->next == head;
|
||||
}
|
||||
|
||||
/**
|
||||
* list_empty - tests whether a list is empty
|
||||
* @head: the list to test.
|
||||
*/
|
||||
static inline int list_empty(const struct list_head *head)
|
||||
{
|
||||
return READ_ONCE(head->next) == head;
|
||||
}
|
||||
|
||||
/**
|
||||
* list_rotate_left - rotate the list to the left
|
||||
* @head: the head of the list
|
||||
*/
|
||||
static inline void list_rotate_left(struct list_head *head)
|
||||
{
|
||||
struct list_head *first;
|
||||
|
||||
if (!list_empty(head)) {
|
||||
first = head->next;
|
||||
list_move_tail(first, head);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* list_rotate_to_front() - Rotate list to specific item.
|
||||
* @list: The desired new front of the list.
|
||||
* @head: The head of the list.
|
||||
*
|
||||
* Rotates list so that @list becomes the new front of the list.
|
||||
*/
|
||||
static inline void list_rotate_to_front(struct list_head *list,
|
||||
struct list_head *head)
|
||||
{
|
||||
/*
|
||||
* Deletes the list head from the list denoted by @head and
|
||||
* places it as the tail of @list, this effectively rotates the
|
||||
* list so that @list is at the front.
|
||||
*/
|
||||
list_move_tail(head, list);
|
||||
}
|
||||
|
||||
/**
|
||||
* list_is_singular - tests whether a list has just one entry.
|
||||
* @head: the list to test.
|
||||
*/
|
||||
static inline int list_is_singular(const struct list_head *head)
|
||||
{
|
||||
return !list_empty(head) && (head->next == head->prev);
|
||||
}
|
||||
|
||||
static inline void __list_cut_position(struct list_head *list,
|
||||
struct list_head *head, struct list_head *entry)
|
||||
{
|
||||
struct list_head *new_first = entry->next;
|
||||
list->next = head->next;
|
||||
list->next->prev = list;
|
||||
list->prev = entry;
|
||||
entry->next = list;
|
||||
head->next = new_first;
|
||||
new_first->prev = head;
|
||||
}
|
||||
|
||||
/**
|
||||
* list_cut_position - cut a list into two
|
||||
* @list: a new list to add all removed entries
|
||||
* @head: a list with entries
|
||||
* @entry: an entry within head, could be the head itself
|
||||
* and if so we won't cut the list
|
||||
*
|
||||
* This helper moves the initial part of @head, up to and
|
||||
* including @entry, from @head to @list. You should
|
||||
* pass on @entry an element you know is on @head. @list
|
||||
* should be an empty list or a list you do not care about
|
||||
* losing its data.
|
||||
*
|
||||
*/
|
||||
static inline void list_cut_position(struct list_head *list,
|
||||
struct list_head *head, struct list_head *entry)
|
||||
{
|
||||
if (list_empty(head))
|
||||
return;
|
||||
if (list_is_singular(head) &&
|
||||
(head->next != entry && head != entry))
|
||||
return;
|
||||
if (entry == head)
|
||||
INIT_LIST_HEAD(list);
|
||||
else
|
||||
__list_cut_position(list, head, entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* list_cut_before - cut a list into two, before given entry
|
||||
* @list: a new list to add all removed entries
|
||||
* @head: a list with entries
|
||||
* @entry: an entry within head, could be the head itself
|
||||
*
|
||||
* This helper moves the initial part of @head, up to but
|
||||
* excluding @entry, from @head to @list. You should pass
|
||||
* in @entry an element you know is on @head. @list should
|
||||
* be an empty list or a list you do not care about losing
|
||||
* its data.
|
||||
* If @entry == @head, all entries on @head are moved to
|
||||
* @list.
|
||||
*/
|
||||
static inline void list_cut_before(struct list_head *list,
|
||||
struct list_head *head,
|
||||
struct list_head *entry)
|
||||
{
|
||||
if (head->next == entry) {
|
||||
INIT_LIST_HEAD(list);
|
||||
return;
|
||||
}
|
||||
list->next = head->next;
|
||||
list->next->prev = list;
|
||||
list->prev = entry->prev;
|
||||
list->prev->next = list;
|
||||
head->next = entry;
|
||||
entry->prev = head;
|
||||
}
|
||||
|
||||
static inline void __list_splice(const struct list_head *list,
|
||||
struct list_head *prev,
|
||||
struct list_head *next)
|
||||
{
|
||||
struct list_head *first = list->next;
|
||||
struct list_head *last = list->prev;
|
||||
|
||||
first->prev = prev;
|
||||
prev->next = first;
|
||||
|
||||
last->next = next;
|
||||
next->prev = last;
|
||||
}
|
||||
|
||||
/**
|
||||
* list_splice - join two lists, this is designed for stacks
|
||||
* @list: the new list to add.
|
||||
* @head: the place to add it in the first list.
|
||||
*/
|
||||
static inline void list_splice(const struct list_head *list,
|
||||
struct list_head *head)
|
||||
{
|
||||
if (!list_empty(list))
|
||||
__list_splice(list, head, head->next);
|
||||
}
|
||||
|
||||
/**
|
||||
* list_splice_tail - join two lists, each list being a queue
|
||||
* @list: the new list to add.
|
||||
* @head: the place to add it in the first list.
|
||||
*/
|
||||
static inline void list_splice_tail(struct list_head *list,
|
||||
struct list_head *head)
|
||||
{
|
||||
if (!list_empty(list))
|
||||
__list_splice(list, head->prev, head);
|
||||
}
|
||||
|
||||
/**
|
||||
* list_splice_init - join two lists and reinitialise the emptied list.
|
||||
* @list: the new list to add.
|
||||
* @head: the place to add it in the first list.
|
||||
*
|
||||
* The list at @list is reinitialised
|
||||
*/
|
||||
static inline void list_splice_init(struct list_head *list,
|
||||
struct list_head *head)
|
||||
{
|
||||
if (!list_empty(list)) {
|
||||
__list_splice(list, head, head->next);
|
||||
INIT_LIST_HEAD(list);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* list_splice_tail_init - join two lists and reinitialise the emptied list
|
||||
* @list: the new list to add.
|
||||
* @head: the place to add it in the first list.
|
||||
*
|
||||
* Each of the lists is a queue.
|
||||
* The list at @list is reinitialised
|
||||
*/
|
||||
static inline void list_splice_tail_init(struct list_head *list,
|
||||
struct list_head *head)
|
||||
{
|
||||
if (!list_empty(list)) {
|
||||
__list_splice(list, head->prev, head);
|
||||
INIT_LIST_HEAD(list);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* list_entry - get the struct for this entry
|
||||
* @ptr: the &struct list_head pointer.
|
||||
* @type: the type of the struct this is embedded in.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*/
|
||||
#define list_entry(ptr, type, member) \
|
||||
container_of(ptr, type, member)
|
||||
|
||||
/**
|
||||
* list_first_entry - get the first element from a list
|
||||
* @ptr: the list head to take the element from.
|
||||
* @type: the type of the struct this is embedded in.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*
|
||||
* Note, that list is expected to be not empty.
|
||||
*/
|
||||
#define list_first_entry(ptr, type, member) \
|
||||
list_entry((ptr)->next, type, member)
|
||||
|
||||
/**
|
||||
* list_last_entry - get the last element from a list
|
||||
* @ptr: the list head to take the element from.
|
||||
* @type: the type of the struct this is embedded in.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*
|
||||
* Note, that list is expected to be not empty.
|
||||
*/
|
||||
#define list_last_entry(ptr, type, member) \
|
||||
list_entry((ptr)->prev, type, member)
|
||||
|
||||
/**
|
||||
* list_first_entry_or_null - get the first element from a list
|
||||
* @ptr: the list head to take the element from.
|
||||
* @type: the type of the struct this is embedded in.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*
|
||||
* Note that if the list is empty, it returns NULL.
|
||||
*/
|
||||
#define list_first_entry_or_null(ptr, type, member) ({ \
|
||||
struct list_head *head__ = (ptr); \
|
||||
struct list_head *pos__ = READ_ONCE(head__->next); \
|
||||
pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
|
||||
})
|
||||
|
||||
/**
|
||||
* list_next_entry - get the next element in list
|
||||
* @pos: the type * to cursor
|
||||
* @member: the name of the list_head within the struct.
|
||||
*/
|
||||
#define list_next_entry(pos, member) \
|
||||
list_entry((pos)->member.next, __typeof__(*(pos)), member)
|
||||
|
||||
/**
|
||||
* list_prev_entry - get the prev element in list
|
||||
* @pos: the type * to cursor
|
||||
* @member: the name of the list_head within the struct.
|
||||
*/
|
||||
#define list_prev_entry(pos, member) \
|
||||
list_entry((pos)->member.prev, __typeof__(*(pos)), member)
|
||||
|
||||
/**
|
||||
* list_for_each - iterate over a list
|
||||
* @pos: the &struct list_head to use as a loop cursor.
|
||||
* @head: the head for your list.
|
||||
*/
|
||||
#define list_for_each(pos, head) \
|
||||
for (pos = (head)->next; pos != (head); pos = pos->next)
|
||||
|
||||
/**
|
||||
* list_for_each_continue - continue iteration over a list
|
||||
* @pos: the &struct list_head to use as a loop cursor.
|
||||
* @head: the head for your list.
|
||||
*
|
||||
* Continue to iterate over a list, continuing after the current position.
|
||||
*/
|
||||
#define list_for_each_continue(pos, head) \
|
||||
for (pos = pos->next; pos != (head); pos = pos->next)
|
||||
|
||||
/**
|
||||
* list_for_each_prev - iterate over a list backwards
|
||||
* @pos: the &struct list_head to use as a loop cursor.
|
||||
* @head: the head for your list.
|
||||
*/
|
||||
#define list_for_each_prev(pos, head) \
|
||||
for (pos = (head)->prev; pos != (head); pos = pos->prev)
|
||||
|
||||
/**
|
||||
* list_for_each_safe - iterate over a list safe against removal of list entry
|
||||
* @pos: the &struct list_head to use as a loop cursor.
|
||||
* @n: another &struct list_head to use as temporary storage
|
||||
* @head: the head for your list.
|
||||
*/
|
||||
#define list_for_each_safe(pos, n, head) \
|
||||
for (pos = (head)->next, n = pos->next; pos != (head); \
|
||||
pos = n, n = pos->next)
|
||||
|
||||
/**
|
||||
* list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
|
||||
* @pos: the &struct list_head to use as a loop cursor.
|
||||
* @n: another &struct list_head to use as temporary storage
|
||||
* @head: the head for your list.
|
||||
*/
|
||||
#define list_for_each_prev_safe(pos, n, head) \
|
||||
for (pos = (head)->prev, n = pos->prev; \
|
||||
pos != (head); \
|
||||
pos = n, n = pos->prev)
|
||||
|
||||
/**
|
||||
* list_entry_is_head - test if the entry points to the head of the list
|
||||
* @pos: the type * to cursor
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*/
|
||||
#define list_entry_is_head(pos, head, member) \
|
||||
(&pos->member == (head))
|
||||
|
||||
/**
|
||||
* list_for_each_entry - iterate over list of given type
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*/
|
||||
#define list_for_each_entry(pos, head, member) \
|
||||
for (pos = list_first_entry(head, __typeof__(*pos), member); \
|
||||
!list_entry_is_head(pos, head, member); \
|
||||
pos = list_next_entry(pos, member))
|
||||
|
||||
/**
|
||||
* list_for_each_entry_reverse - iterate backwards over list of given type.
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*/
|
||||
#define list_for_each_entry_reverse(pos, head, member) \
|
||||
for (pos = list_last_entry(head, __typeof__(*pos), member); \
|
||||
!list_entry_is_head(pos, head, member); \
|
||||
pos = list_prev_entry(pos, member))
|
||||
|
||||
/**
|
||||
* list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
|
||||
* @pos: the type * to use as a start point
|
||||
* @head: the head of the list
|
||||
* @member: the name of the list_head within the struct.
|
||||
*
|
||||
* Prepares a pos entry for use as a start point in list_for_each_entry_continue().
|
||||
*/
|
||||
#define list_prepare_entry(pos, head, member) \
|
||||
((pos) ? : list_entry(head, __typeof__(*pos), member))
|
||||
|
||||
/**
|
||||
* list_for_each_entry_continue - continue iteration over list of given type
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*
|
||||
* Continue to iterate over list of given type, continuing after
|
||||
* the current position.
|
||||
*/
|
||||
#define list_for_each_entry_continue(pos, head, member) \
|
||||
for (pos = list_next_entry(pos, member); \
|
||||
!list_entry_is_head(pos, head, member); \
|
||||
pos = list_next_entry(pos, member))
|
||||
|
||||
/**
|
||||
* list_for_each_entry_continue_reverse - iterate backwards from the given point
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*
|
||||
* Start to iterate over list of given type backwards, continuing after
|
||||
* the current position.
|
||||
*/
|
||||
#define list_for_each_entry_continue_reverse(pos, head, member) \
|
||||
for (pos = list_prev_entry(pos, member); \
|
||||
!list_entry_is_head(pos, head, member); \
|
||||
pos = list_prev_entry(pos, member))
|
||||
|
||||
/**
|
||||
* list_for_each_entry_from - iterate over list of given type from the current point
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*
|
||||
* Iterate over list of given type, continuing from current position.
|
||||
*/
|
||||
#define list_for_each_entry_from(pos, head, member) \
|
||||
for (; !list_entry_is_head(pos, head, member); \
|
||||
pos = list_next_entry(pos, member))
|
||||
|
||||
/**
|
||||
* list_for_each_entry_from_reverse - iterate backwards over list of given type
|
||||
* from the current point
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*
|
||||
* Iterate backwards over list of given type, continuing from current position.
|
||||
*/
|
||||
#define list_for_each_entry_from_reverse(pos, head, member) \
|
||||
for (; !list_entry_is_head(pos, head, member); \
|
||||
pos = list_prev_entry(pos, member))
|
||||
|
||||
/**
|
||||
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @n: another type * to use as temporary storage
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*/
|
||||
#define list_for_each_entry_safe(pos, n, head, member) \
|
||||
for (pos = list_first_entry(head, __typeof__(*pos), member), \
|
||||
n = list_next_entry(pos, member); \
|
||||
!list_entry_is_head(pos, head, member); \
|
||||
pos = n, n = list_next_entry(n, member))
|
||||
|
||||
/**
|
||||
* list_for_each_entry_safe_continue - continue list iteration safe against removal
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @n: another type * to use as temporary storage
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*
|
||||
* Iterate over list of given type, continuing after current point,
|
||||
* safe against removal of list entry.
|
||||
*/
|
||||
#define list_for_each_entry_safe_continue(pos, n, head, member) \
|
||||
for (pos = list_next_entry(pos, member), \
|
||||
n = list_next_entry(pos, member); \
|
||||
!list_entry_is_head(pos, head, member); \
|
||||
pos = n, n = list_next_entry(n, member))
|
||||
|
||||
/**
|
||||
* list_for_each_entry_safe_from - iterate over list from current point safe against removal
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @n: another type * to use as temporary storage
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*
|
||||
* Iterate over list of given type from current point, safe against
|
||||
* removal of list entry.
|
||||
*/
|
||||
#define list_for_each_entry_safe_from(pos, n, head, member) \
|
||||
for (n = list_next_entry(pos, member); \
|
||||
!list_entry_is_head(pos, head, member); \
|
||||
pos = n, n = list_next_entry(n, member))
|
||||
|
||||
/**
|
||||
* list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @n: another type * to use as temporary storage
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the list_head within the struct.
|
||||
*
|
||||
* Iterate backwards over list of given type, safe against removal
|
||||
* of list entry.
|
||||
*/
|
||||
#define list_for_each_entry_safe_reverse(pos, n, head, member) \
|
||||
for (pos = list_last_entry(head, __typeof__(*pos), member), \
|
||||
n = list_prev_entry(pos, member); \
|
||||
!list_entry_is_head(pos, head, member); \
|
||||
pos = n, n = list_prev_entry(n, member))
|
||||
|
||||
/**
|
||||
* list_safe_reset_next - reset a stale list_for_each_entry_safe loop
|
||||
* @pos: the loop cursor used in the list_for_each_entry_safe loop
|
||||
* @n: temporary storage used in list_for_each_entry_safe
|
||||
* @member: the name of the list_head within the struct.
|
||||
*
|
||||
* list_safe_reset_next is not safe to use in general if the list may be
|
||||
* modified concurrently (eg. the lock is dropped in the loop body). An
|
||||
* exception to this is if the cursor element (pos) is pinned in the list,
|
||||
* and list_safe_reset_next is called after re-taking the lock and before
|
||||
* completing the current iteration of the loop body.
|
||||
*/
|
||||
#define list_safe_reset_next(pos, n, member) \
|
||||
n = list_next_entry(pos, member)
|
||||
|
||||
/*
|
||||
* Double linked lists with a single pointer list head.
|
||||
* Mostly useful for hash tables where the two pointer list head is
|
||||
* too wasteful.
|
||||
* You lose the ability to access the tail in O(1).
|
||||
*/
|
||||
|
||||
#define HLIST_HEAD_INIT { .first = NULL }
|
||||
#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
|
||||
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
|
||||
static inline void INIT_HLIST_NODE(struct hlist_node *h)
|
||||
{
|
||||
h->next = NULL;
|
||||
h->pprev = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* hlist_unhashed - Has node been removed from list and reinitialized?
|
||||
* @h: Node to be checked
|
||||
*
|
||||
* Not that not all removal functions will leave a node in unhashed
|
||||
* state. For example, hlist_nulls_del_init_rcu() does leave the
|
||||
* node in unhashed state, but hlist_nulls_del() does not.
|
||||
*/
|
||||
static inline int hlist_unhashed(const struct hlist_node *h)
|
||||
{
|
||||
return !h->pprev;
|
||||
}
|
||||
|
||||
/**
|
||||
* hlist_unhashed_lockless - Version of hlist_unhashed for lockless use
|
||||
* @h: Node to be checked
|
||||
*
|
||||
* This variant of hlist_unhashed() must be used in lockless contexts
|
||||
* to avoid potential load-tearing. The READ_ONCE() is paired with the
|
||||
* various WRITE_ONCE() in hlist helpers that are defined below.
|
||||
*/
|
||||
static inline int hlist_unhashed_lockless(const struct hlist_node *h)
|
||||
{
|
||||
return !READ_ONCE(h->pprev);
|
||||
}
|
||||
|
||||
/**
|
||||
* hlist_empty - Is the specified hlist_head structure an empty hlist?
|
||||
* @h: Structure to check.
|
||||
*/
|
||||
static inline int hlist_empty(const struct hlist_head *h)
|
||||
{
|
||||
return !READ_ONCE(h->first);
|
||||
}
|
||||
|
||||
static inline void __hlist_del(struct hlist_node *n)
|
||||
{
|
||||
struct hlist_node *next = n->next;
|
||||
struct hlist_node **pprev = n->pprev;
|
||||
|
||||
WRITE_ONCE(*pprev, next);
|
||||
if (next)
|
||||
WRITE_ONCE(next->pprev, pprev);
|
||||
}
|
||||
|
||||
/**
|
||||
* hlist_del - Delete the specified hlist_node from its list
|
||||
* @n: Node to delete.
|
||||
*
|
||||
* Note that this function leaves the node in hashed state. Use
|
||||
* hlist_del_init() or similar instead to unhash @n.
|
||||
*/
|
||||
static inline void hlist_del(struct hlist_node *n)
|
||||
{
|
||||
__hlist_del(n);
|
||||
n->next = LIST_POISON1;
|
||||
n->pprev = LIST_POISON2;
|
||||
}
|
||||
|
||||
/**
|
||||
* hlist_del_init - Delete the specified hlist_node from its list and initialize
|
||||
* @n: Node to delete.
|
||||
*
|
||||
* Note that this function leaves the node in unhashed state.
|
||||
*/
|
||||
static inline void hlist_del_init(struct hlist_node *n)
|
||||
{
|
||||
if (!hlist_unhashed(n)) {
|
||||
__hlist_del(n);
|
||||
INIT_HLIST_NODE(n);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* hlist_add_head - add a new entry at the beginning of the hlist
|
||||
* @n: new entry to be added
|
||||
* @h: hlist head to add it after
|
||||
*
|
||||
* Insert a new entry after the specified head.
|
||||
* This is good for implementing stacks.
|
||||
*/
|
||||
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
|
||||
{
|
||||
struct hlist_node *first = h->first;
|
||||
WRITE_ONCE(n->next, first);
|
||||
if (first)
|
||||
WRITE_ONCE(first->pprev, &n->next);
|
||||
WRITE_ONCE(h->first, n);
|
||||
WRITE_ONCE(n->pprev, &h->first);
|
||||
}
|
||||
|
||||
/**
|
||||
* hlist_add_before - add a new entry before the one specified
|
||||
* @n: new entry to be added
|
||||
* @next: hlist node to add it before, which must be non-NULL
|
||||
*/
|
||||
static inline void hlist_add_before(struct hlist_node *n,
|
||||
struct hlist_node *next)
|
||||
{
|
||||
WRITE_ONCE(n->pprev, next->pprev);
|
||||
WRITE_ONCE(n->next, next);
|
||||
WRITE_ONCE(next->pprev, &n->next);
|
||||
WRITE_ONCE(*(n->pprev), n);
|
||||
}
|
||||
|
||||
/**
|
||||
* hlist_add_behind - add a new entry after the one specified
|
||||
* @n: new entry to be added
|
||||
* @prev: hlist node to add it after, which must be non-NULL
|
||||
*/
|
||||
static inline void hlist_add_behind(struct hlist_node *n,
|
||||
struct hlist_node *prev)
|
||||
{
|
||||
WRITE_ONCE(n->next, prev->next);
|
||||
WRITE_ONCE(prev->next, n);
|
||||
WRITE_ONCE(n->pprev, &prev->next);
|
||||
|
||||
if (n->next)
|
||||
WRITE_ONCE(n->next->pprev, &n->next);
|
||||
}
|
||||
|
||||
/**
|
||||
* hlist_add_fake - create a fake hlist consisting of a single headless node
|
||||
* @n: Node to make a fake list out of
|
||||
*
|
||||
* This makes @n appear to be its own predecessor on a headless hlist.
|
||||
* The point of this is to allow things like hlist_del() to work correctly
|
||||
* in cases where there is no list.
|
||||
*/
|
||||
static inline void hlist_add_fake(struct hlist_node *n)
|
||||
{
|
||||
n->pprev = &n->next;
|
||||
}
|
||||
|
||||
/**
|
||||
* hlist_fake: Is this node a fake hlist?
|
||||
* @h: Node to check for being a self-referential fake hlist.
|
||||
*/
|
||||
static inline bool hlist_fake(struct hlist_node *h)
|
||||
{
|
||||
return h->pprev == &h->next;
|
||||
}
|
||||
|
||||
/**
|
||||
* hlist_is_singular_node - is node the only element of the specified hlist?
|
||||
* @n: Node to check for singularity.
|
||||
* @h: Header for potentially singular list.
|
||||
*
|
||||
* Check whether the node is the only node of the head without
|
||||
* accessing head, thus avoiding unnecessary cache misses.
|
||||
*/
|
||||
static inline bool
|
||||
hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
|
||||
{
|
||||
return !n->next && n->pprev == &h->first;
|
||||
}
|
||||
|
||||
/**
|
||||
* hlist_move_list - Move an hlist
|
||||
* @old: hlist_head for old list.
|
||||
* @new: hlist_head for new list.
|
||||
*
|
||||
* Move a list from one list head to another. Fixup the pprev
|
||||
* reference of the first entry if it exists.
|
||||
*/
|
||||
static inline void hlist_move_list(struct hlist_head *old,
|
||||
struct hlist_head *new)
|
||||
{
|
||||
new->first = old->first;
|
||||
if (new->first)
|
||||
new->first->pprev = &new->first;
|
||||
old->first = NULL;
|
||||
}
|
||||
|
||||
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
|
||||
|
||||
#define hlist_for_each(pos, head) \
|
||||
for (pos = (head)->first; pos ; pos = pos->next)
|
||||
|
||||
#define hlist_for_each_safe(pos, n, head) \
|
||||
for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
|
||||
pos = n)
|
||||
|
||||
#define hlist_entry_safe(ptr, type, member) \
|
||||
({ __typeof__(ptr) ____ptr = (ptr); \
|
||||
____ptr ? hlist_entry(____ptr, type, member) : NULL; \
|
||||
})
|
||||
|
||||
/**
|
||||
* hlist_for_each_entry - iterate over list of given type
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the hlist_node within the struct.
|
||||
*/
|
||||
#define hlist_for_each_entry(pos, head, member) \
|
||||
for (pos = hlist_entry_safe((head)->first, __typeof__(*(pos)), member); \
|
||||
pos; \
|
||||
pos = hlist_entry_safe((pos)->member.next, __typeof__(*(pos)), member))
|
||||
|
||||
/**
|
||||
* hlist_for_each_entry_continue - iterate over a hlist continuing after current point
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @member: the name of the hlist_node within the struct.
|
||||
*/
|
||||
#define hlist_for_each_entry_continue(pos, member) \
|
||||
for (pos = hlist_entry_safe((pos)->member.next, __typeof__(*(pos)), member); \
|
||||
pos; \
|
||||
pos = hlist_entry_safe((pos)->member.next, __typeof__(*(pos)), member))
|
||||
|
||||
/**
|
||||
* hlist_for_each_entry_from - iterate over a hlist continuing from current point
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @member: the name of the hlist_node within the struct.
|
||||
*/
|
||||
#define hlist_for_each_entry_from(pos, member) \
|
||||
for (; pos; \
|
||||
pos = hlist_entry_safe((pos)->member.next, __typeof__(*(pos)), member))
|
||||
|
||||
/**
|
||||
* hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
|
||||
* @pos: the type * to use as a loop cursor.
|
||||
* @n: a &struct hlist_node to use as temporary storage
|
||||
* @head: the head for your list.
|
||||
* @member: the name of the hlist_node within the struct.
|
||||
*/
|
||||
#define hlist_for_each_entry_safe(pos, n, head, member) \
|
||||
for (pos = hlist_entry_safe((head)->first, __typeof__(*pos), member); \
|
||||
pos && ({ n = pos->member.next; 1; }); \
|
||||
pos = hlist_entry_safe(n, __typeof__(*pos), member))
|
||||
|
||||
#endif /* __BR_LIST_H */
|
||||
@@ -1,20 +0,0 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
|
||||
/*
|
||||
* Taken from linux kernel: lib/list_sort.c
|
||||
*/
|
||||
|
||||
#ifndef _BR_LIST_SORT_H
|
||||
#define _BR_LIST_SORT_H
|
||||
|
||||
//#include <linux/types.h>
|
||||
|
||||
struct list_head;
|
||||
|
||||
typedef int __attribute__((nonnull(2,3))) (*list_cmp_func_t)(void *,
|
||||
const struct list_head *, const struct list_head *);
|
||||
|
||||
__attribute__((nonnull(2,3)))
|
||||
void list_sort(void *priv, struct list_head *head, list_cmp_func_t cmp);
|
||||
|
||||
#endif /* _BR_LIST_SORT */
|
||||
@@ -1,46 +0,0 @@
|
||||
#ifndef _LINUX_UNALIGNED_PACKED_STRUCT_H
|
||||
#define _LINUX_UNALIGNED_PACKED_STRUCT_H
|
||||
|
||||
#include <linux/types.h>
|
||||
|
||||
struct __una_u16 { u16 x; } __packed;
|
||||
struct __una_u32 { u32 x; } __packed;
|
||||
struct __una_u64 { u64 x; } __packed;
|
||||
|
||||
static inline u16 __get_unaligned_cpu16(const void *p)
|
||||
{
|
||||
const struct __una_u16 *ptr = (const struct __una_u16 *)p;
|
||||
return ptr->x;
|
||||
}
|
||||
|
||||
static inline u32 __get_unaligned_cpu32(const void *p)
|
||||
{
|
||||
const struct __una_u32 *ptr = (const struct __una_u32 *)p;
|
||||
return ptr->x;
|
||||
}
|
||||
|
||||
static inline u64 __get_unaligned_cpu64(const void *p)
|
||||
{
|
||||
const struct __una_u64 *ptr = (const struct __una_u64 *)p;
|
||||
return ptr->x;
|
||||
}
|
||||
|
||||
static inline void __put_unaligned_cpu16(u16 val, void *p)
|
||||
{
|
||||
struct __una_u16 *ptr = (struct __una_u16 *)p;
|
||||
ptr->x = val;
|
||||
}
|
||||
|
||||
static inline void __put_unaligned_cpu32(u32 val, void *p)
|
||||
{
|
||||
struct __una_u32 *ptr = (struct __una_u32 *)p;
|
||||
ptr->x = val;
|
||||
}
|
||||
|
||||
static inline void __put_unaligned_cpu64(u64 val, void *p)
|
||||
{
|
||||
struct __una_u64 *ptr = (struct __una_u64 *)p;
|
||||
ptr->x = val;
|
||||
}
|
||||
|
||||
#endif /* _LINUX_UNALIGNED_PACKED_STRUCT_H */
|
||||
@@ -1,53 +0,0 @@
|
||||
/* pjwhash-inline.h - PJW hash function, inline version.
|
||||
*
|
||||
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _PJWHASH_INLINE_H
|
||||
#define _PJWHASH_INLINE_H
|
||||
|
||||
#include "bits.h"
|
||||
|
||||
#define THREE_QUARTERS ((int) ((BITS_PER_INT * 3) / 4))
|
||||
#define ONE_EIGHTH ((int) (BITS_PER_INT / 8))
|
||||
#define HIGH_BITS ( ~((uint)(~0) >> ONE_EIGHTH ))
|
||||
|
||||
#ifndef _pjw_inline
|
||||
#define _pjw_inline static inline
|
||||
#endif
|
||||
|
||||
/**
|
||||
* unsigned int pjwhash - PJW hash function
|
||||
* @key: the key address.
|
||||
* @length: the length of key.
|
||||
*
|
||||
* This hash was created by Peter Jay Weinberger (AT&T Bell Labs):
|
||||
* https://en.wikipedia.org/wiki/PJW_hash_function
|
||||
*
|
||||
* Return: the PJW hash.
|
||||
*/
|
||||
_pjw_inline uint pjwhash(const void* key, uint length)
|
||||
{
|
||||
uint hash = 0, high;
|
||||
const u8 *k = key;
|
||||
|
||||
for (uint i = 0; i < length; ++k, ++i) {
|
||||
hash = (hash << ONE_EIGHTH) + *k;
|
||||
high = hash & HIGH_BITS;
|
||||
if (high != 0) {
|
||||
hash ^= high >> THREE_QUARTERS;
|
||||
hash &= ~high;
|
||||
}
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
#endif /* _PJWHASH_INLINE_H */
|
||||
@@ -1,30 +0,0 @@
|
||||
/* pjwhash.h - PJW hash function, extern version.
|
||||
*
|
||||
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*/
|
||||
|
||||
#ifndef _PJWHASH_H
|
||||
#define _PJWHASH_H
|
||||
|
||||
#include "bits.h"
|
||||
|
||||
/**
|
||||
* unsigned int pjwhash - PJW hash function
|
||||
* @key: the key address.
|
||||
* @length: the length of key.
|
||||
*
|
||||
* This hash was created by Peter Jay Weinberger (AT&T Bell Labs):
|
||||
* https://en.wikipedia.org/wiki/PJW_hash_function
|
||||
*
|
||||
* Return: the PJW hash.
|
||||
*/
|
||||
extern uint pjwhash (const void* key, uint length);
|
||||
|
||||
#endif /* _PJWHASH_H */
|
||||
345
include/plist.h
345
include/plist.h
@@ -1,345 +0,0 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
|
||||
/* adaptation of kernel's <linux/plist.h>
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Descending-priority-sorted double-linked list
|
||||
*
|
||||
* (C) 2002-2003 Intel Corp
|
||||
* Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>.
|
||||
*
|
||||
* 2001-2005 (c) MontaVista Software, Inc.
|
||||
* Daniel Walker <dwalker@mvista.com>
|
||||
*
|
||||
* (C) 2005 Thomas Gleixner <tglx@linutronix.de>
|
||||
*
|
||||
* Simplifications of the original code by
|
||||
* Oleg Nesterov <oleg@tv-sign.ru>
|
||||
*
|
||||
* Based on simple lists (include/linux/list.h).
|
||||
*
|
||||
* This is a priority-sorted list of nodes; each node has a
|
||||
* priority from INT_MIN (highest) to INT_MAX (lowest).
|
||||
*
|
||||
* Addition is O(K), removal is O(1), change of priority of a node is
|
||||
* O(K) and K is the number of RT priority levels used in the system.
|
||||
* (1 <= K <= 99)
|
||||
*
|
||||
* This list is really a list of lists:
|
||||
*
|
||||
* - The tier 1 list is the prio_list, different priority nodes.
|
||||
*
|
||||
* - The tier 2 list is the node_list, serialized nodes.
|
||||
*
|
||||
* Simple ASCII art explanation:
|
||||
*
|
||||
* pl:prio_list (only for plist_node)
|
||||
* nl:node_list
|
||||
* HEAD| NODE(S)
|
||||
* |
|
||||
* ||------------------------------------|
|
||||
* ||->|pl|<->|pl|<--------------->|pl|<-|
|
||||
* | |10| |21| |21| |21| |40| (prio)
|
||||
* | | | | | | | | | | |
|
||||
* | | | | | | | | | | |
|
||||
* |->|nl|<->|nl|<->|nl|<->|nl|<->|nl|<->|nl|<-|
|
||||
* |-------------------------------------------|
|
||||
*
|
||||
* The nodes on the prio_list list are sorted by priority to simplify
|
||||
* the insertion of new nodes. There are no nodes with duplicate
|
||||
* priorites on the list.
|
||||
*
|
||||
* The nodes on the node_list are ordered by priority and can contain
|
||||
* entries which have the same priority. Those entries are ordered
|
||||
* FIFO
|
||||
*
|
||||
* Addition means: look for the prio_list node in the prio_list
|
||||
* for the priority of the node and insert it before the node_list
|
||||
* entry of the next prio_list node. If it is the first node of
|
||||
* that priority, add it to the prio_list in the right position and
|
||||
* insert it into the serialized node_list list
|
||||
*
|
||||
* Removal means remove it from the node_list and remove it from
|
||||
* the prio_list if the node_list list_head is non empty. In case
|
||||
* of removal from the prio_list it must be checked whether other
|
||||
* entries of the same priority are on the list or not. If there
|
||||
* is another entry of the same priority then this entry has to
|
||||
* replace the removed entry on the prio_list. If the entry which
|
||||
* is removed is the only entry of this priority then a simple
|
||||
* remove from both list is sufficient.
|
||||
*
|
||||
* INT_MIN is the highest priority, 0 is the medium highest, INT_MAX
|
||||
* is lowest priority.
|
||||
*
|
||||
* No locking is done, up to the caller.
|
||||
*/
|
||||
#ifndef _LINUX_PLIST_H_
|
||||
#define _LINUX_PLIST_H_
|
||||
|
||||
#include "container-of.h"
|
||||
#include "list.h"
|
||||
//#include <types.h>
|
||||
|
||||
// #include <asm/bug.h>
|
||||
|
||||
struct plist_head {
|
||||
struct list_head node_list;
|
||||
};
|
||||
|
||||
struct plist_node {
|
||||
int prio;
|
||||
struct list_head prio_list;
|
||||
struct list_head node_list;
|
||||
};
|
||||
|
||||
/**
|
||||
* PLIST_HEAD_INIT - static struct plist_head initializer
|
||||
* @head: struct plist_head variable name
|
||||
*/
|
||||
#define PLIST_HEAD_INIT(head) \
|
||||
{ \
|
||||
.node_list = LIST_HEAD_INIT((head).node_list) \
|
||||
}
|
||||
|
||||
/**
|
||||
* PLIST_HEAD - declare and init plist_head
|
||||
* @head: name for struct plist_head variable
|
||||
*/
|
||||
#define PLIST_HEAD(head) \
|
||||
struct plist_head head = PLIST_HEAD_INIT(head)
|
||||
|
||||
/**
|
||||
* PLIST_NODE_INIT - static struct plist_node initializer
|
||||
* @node: struct plist_node variable name
|
||||
* @__prio: initial node priority
|
||||
*/
|
||||
#define PLIST_NODE_INIT(node, __prio) \
|
||||
{ \
|
||||
.prio = (__prio), \
|
||||
.prio_list = LIST_HEAD_INIT((node).prio_list), \
|
||||
.node_list = LIST_HEAD_INIT((node).node_list), \
|
||||
}
|
||||
|
||||
/**
|
||||
* plist_head_init - dynamic struct plist_head initializer
|
||||
* @head: &struct plist_head pointer
|
||||
*/
|
||||
static inline void
|
||||
plist_head_init(struct plist_head *head)
|
||||
{
|
||||
INIT_LIST_HEAD(&head->node_list);
|
||||
}
|
||||
|
||||
/**
|
||||
* plist_node_init - Dynamic struct plist_node initializer
|
||||
* @node: &struct plist_node pointer
|
||||
* @prio: initial node priority
|
||||
*/
|
||||
static inline void plist_node_init(struct plist_node *node, int prio)
|
||||
{
|
||||
node->prio = prio;
|
||||
INIT_LIST_HEAD(&node->prio_list);
|
||||
INIT_LIST_HEAD(&node->node_list);
|
||||
}
|
||||
|
||||
extern void plist_add(struct plist_node *node, struct plist_head *head);
|
||||
extern void plist_del(struct plist_node *node, struct plist_head *head);
|
||||
|
||||
extern void plist_requeue(struct plist_node *node, struct plist_head *head);
|
||||
|
||||
/**
|
||||
* plist_for_each - iterate over the plist
|
||||
* @pos: the type * to use as a loop counter
|
||||
* @head: the head for your list
|
||||
*/
|
||||
#define plist_for_each(pos, head) \
|
||||
list_for_each_entry(pos, &(head)->node_list, node_list)
|
||||
|
||||
/**
|
||||
* plist_for_each_reverse - iterate backwards over the plist
|
||||
* @pos: the type * to use as a loop counter
|
||||
* @head: the head for your list
|
||||
*/
|
||||
#define plist_for_each_reverse(pos, head) \
|
||||
list_for_each_entry_reverse(pos, &(head)->node_list, node_list)
|
||||
|
||||
/**
|
||||
* plist_for_each_continue - continue iteration over the plist
|
||||
* @pos: the type * to use as a loop cursor
|
||||
* @head: the head for your list
|
||||
*
|
||||
* Continue to iterate over plist, continuing after the current position.
|
||||
*/
|
||||
#define plist_for_each_continue(pos, head) \
|
||||
list_for_each_entry_continue(pos, &(head)->node_list, node_list)
|
||||
|
||||
/**
|
||||
* plist_for_each_continue_reverse - continue iteration over the plist
|
||||
* @pos: the type * to use as a loop cursor
|
||||
* @head: the head for your list
|
||||
*
|
||||
* Continue to iterate backwards over plist, continuing after the current
|
||||
* position.
|
||||
*/
|
||||
#define plist_for_each_continue_reverse(pos, head) \
|
||||
list_for_each_entry_continue_reverse(pos, &(head)->node_list, node_list)
|
||||
|
||||
/**
|
||||
* plist_for_each_safe - iterate safely over a plist of given type
|
||||
* @pos: the type * to use as a loop counter
|
||||
* @n: another type * to use as temporary storage
|
||||
* @head: the head for your list
|
||||
*
|
||||
* Iterate over a plist of given type, safe against removal of list entry.
|
||||
*/
|
||||
#define plist_for_each_safe(pos, n, head) \
|
||||
list_for_each_entry_safe(pos, n, &(head)->node_list, node_list)
|
||||
|
||||
/**
|
||||
* plist_for_each_safe_reverse - iterate backwards safely over a plist of given type
|
||||
* @pos: the type * to use as a loop counter
|
||||
* @n: another type * to use as temporary storage
|
||||
* @head: the head for your list
|
||||
*
|
||||
* Iterate backwards over a plist of given type, safe against removal of list entry.
|
||||
*/
|
||||
#define plist_for_each_safe_reverse(pos, n, head) \
|
||||
list_for_each_entry_safe_reverse(pos, n, &(head)->node_list, node_list)
|
||||
|
||||
/**
|
||||
* plist_for_each_entry - iterate over list of given type
|
||||
* @pos: the type * to use as a loop counter
|
||||
* @head: the head for your list
|
||||
* @mem: the name of the list_head within the struct
|
||||
*/
|
||||
#define plist_for_each_entry(pos, head, mem) \
|
||||
list_for_each_entry(pos, &(head)->node_list, mem.node_list)
|
||||
|
||||
/**
|
||||
* plist_for_each_entry_reverse - iterate backwards over list of given type
|
||||
* @pos: the type * to use as a loop counter
|
||||
* @head: the head for your list
|
||||
* @mem: the name of the list_head within the struct
|
||||
*/
|
||||
#define plist_for_each_entry_reverse(pos, head, mem) \
|
||||
list_for_each_entry_reverse(pos, &(head)->node_list, mem.node_list)
|
||||
|
||||
/**
|
||||
* plist_for_each_entry_continue - continue iteration over list of given type
|
||||
* @pos: the type * to use as a loop cursor
|
||||
* @head: the head for your list
|
||||
* @m: the name of the list_head within the struct
|
||||
*
|
||||
* Continue to iterate over list of given type, continuing after
|
||||
* the current position.
|
||||
*/
|
||||
#define plist_for_each_entry_continue(pos, head, m) \
|
||||
list_for_each_entry_continue(pos, &(head)->node_list, m.node_list)
|
||||
|
||||
/**
|
||||
* plist_for_each_entry_safe - iterate safely over list of given type
|
||||
* @pos: the type * to use as a loop counter
|
||||
* @n: another type * to use as temporary storage
|
||||
* @head: the head for your list
|
||||
* @m: the name of the list_head within the struct
|
||||
*
|
||||
* Iterate over list of given type, safe against removal of list entry.
|
||||
*/
|
||||
#define plist_for_each_entry_safe(pos, n, head, m) \
|
||||
list_for_each_entry_safe(pos, n, &(head)->node_list, m.node_list)
|
||||
|
||||
/**
|
||||
* plist_head_empty - return !0 if a plist_head is empty
|
||||
* @head: &struct plist_head pointer
|
||||
*/
|
||||
static inline int plist_head_empty(const struct plist_head *head)
|
||||
{
|
||||
return list_empty(&head->node_list);
|
||||
}
|
||||
|
||||
/**
|
||||
* plist_node_empty - return !0 if plist_node is not on a list
|
||||
* @node: &struct plist_node pointer
|
||||
*/
|
||||
static inline int plist_node_empty(const struct plist_node *node)
|
||||
{
|
||||
return list_empty(&node->node_list);
|
||||
}
|
||||
|
||||
/* All functions below assume the plist_head is not empty. */
|
||||
|
||||
/**
|
||||
* plist_first_entry - get the struct for the first entry
|
||||
* @head: the &struct plist_head pointer
|
||||
* @type: the type of the struct this is embedded in
|
||||
* @member: the name of the list_head within the struct
|
||||
*/
|
||||
#ifdef CONFIG_DEBUG_PLIST
|
||||
# define plist_first_entry(head, type, member) \
|
||||
({ \
|
||||
WARN_ON(plist_head_empty(head)); \
|
||||
container_of(plist_first(head), type, member); \
|
||||
})
|
||||
#else
|
||||
# define plist_first_entry(head, type, member) \
|
||||
container_of(plist_first(head), type, member)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* plist_last_entry - get the struct for the last entry
|
||||
* @head: the &struct plist_head pointer
|
||||
* @type: the type of the struct this is embedded in
|
||||
* @member: the name of the list_head within the struct
|
||||
*/
|
||||
#ifdef CONFIG_DEBUG_PLIST
|
||||
# define plist_last_entry(head, type, member) \
|
||||
({ \
|
||||
WARN_ON(plist_head_empty(head)); \
|
||||
container_of(plist_last(head), type, member); \
|
||||
})
|
||||
#else
|
||||
# define plist_last_entry(head, type, member) \
|
||||
container_of(plist_last(head), type, member)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* plist_next - get the next entry in list
|
||||
* @pos: the type * to cursor
|
||||
*/
|
||||
#define plist_next(pos) \
|
||||
list_next_entry(pos, node_list)
|
||||
|
||||
/**
|
||||
* plist_prev - get the prev entry in list
|
||||
* @pos: the type * to cursor
|
||||
*/
|
||||
#define plist_prev(pos) \
|
||||
list_prev_entry(pos, node_list)
|
||||
|
||||
/**
|
||||
* plist_first - return the first node (and thus, highest priority)
|
||||
* @head: the &struct plist_head pointer
|
||||
*
|
||||
* Assumes the plist is _not_ empty.
|
||||
*/
|
||||
static inline struct plist_node *plist_first(const struct plist_head *head)
|
||||
{
|
||||
return list_entry(head->node_list.next,
|
||||
struct plist_node, node_list);
|
||||
}
|
||||
|
||||
/**
|
||||
* plist_last - return the last node (and thus, lowest priority)
|
||||
* @head: the &struct plist_head pointer
|
||||
*
|
||||
* Assumes the plist is _not_ empty.
|
||||
*/
|
||||
static inline struct plist_node *plist_last(const struct plist_head *head)
|
||||
{
|
||||
return list_entry(head->node_list.prev,
|
||||
struct plist_node, node_list);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,90 +0,0 @@
|
||||
/* pool.h - A simple memory pool manager.
|
||||
*
|
||||
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef POOL_H
|
||||
#define POOL_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "list.h"
|
||||
#include "bits.h"
|
||||
|
||||
#define POOL_NAME_LENGTH (16) /* max name length including trailing \0 */
|
||||
|
||||
typedef struct {
|
||||
struct list_head list_blocks; /* list of allocated blocks in pool */
|
||||
char data[]; /* objects block */
|
||||
} block_t;
|
||||
|
||||
typedef struct {
|
||||
char name[POOL_NAME_LENGTH]; /* pool name */
|
||||
size_t eltsize; /* object size */
|
||||
u32 available; /* current available elements */
|
||||
u32 allocated; /* total objects allocated */
|
||||
u32 growsize; /* number of objects per block allocated */
|
||||
u32 nblocks; /* number of blocks allocated */
|
||||
struct list_head list_available; /* available nodes */
|
||||
struct list_head list_blocks; /* allocated blocks */
|
||||
} pool_t;
|
||||
|
||||
/**
|
||||
* pool_stats - display some pool statistics
|
||||
* @pool: the pool address.
|
||||
*/
|
||||
void pool_stats(pool_t *pool);
|
||||
|
||||
/**
|
||||
* pool_create - create a new memory pool
|
||||
* @name: the name to give to the pool.
|
||||
* @grow: the number of elements to add when no more available.
|
||||
* @size: the size of an element in pool.
|
||||
*
|
||||
* The name will be truncated to 16 characters (including the final '\0').
|
||||
*
|
||||
* Return: The address of the created pool, or NULL if error.
|
||||
*/
|
||||
pool_t *pool_create(const char *name, u32 grow, size_t size);
|
||||
|
||||
/**
|
||||
* pool_get() - Get an element from a pool.
|
||||
* @pool: The pool address.
|
||||
*
|
||||
* Get an object from the pool.
|
||||
*
|
||||
* Return: The address of the object, or NULL if error.
|
||||
*/
|
||||
void *pool_get(pool_t *pool);
|
||||
|
||||
/**
|
||||
* pool_add() - Add (free) an element to a pool.
|
||||
* @pool: The pool address.
|
||||
* @elt: The address of the object to add to the pool.
|
||||
*
|
||||
* The object will be available for further pool_get().
|
||||
*
|
||||
* Return: The current number of available elements in pool (including
|
||||
* @elt).
|
||||
*/
|
||||
u32 pool_add(pool_t *pool, void *elt);
|
||||
|
||||
/**
|
||||
* pool_destroy() - destroy a pool.
|
||||
* @pool: The pool address.
|
||||
*
|
||||
* Attention: All memory is freed, but no check is done whether all pool
|
||||
* elements have been released. Referencing any pool object after this call
|
||||
* will likely imply some memory corruption.
|
||||
*/
|
||||
void pool_destroy(pool_t *pool);
|
||||
|
||||
#endif
|
||||
128
include/rwonce.h
128
include/rwonce.h
@@ -1,128 +0,0 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
|
||||
/* adaptation of kernel's <asm-generic/rwonce.h>
|
||||
* See https://www.kernel.org/doc/Documentation/memory-barriers.txt
|
||||
*/
|
||||
/*
|
||||
* Prevent the compiler from merging or refetching reads or writes. The
|
||||
* compiler is also forbidden from reordering successive instances of
|
||||
* READ_ONCE and WRITE_ONCE, but only when the compiler is aware of some
|
||||
* particular ordering. One way to make the compiler aware of ordering is to
|
||||
* put the two invocations of READ_ONCE or WRITE_ONCE in different C
|
||||
* statements.
|
||||
*
|
||||
* These two macros will also work on aggregate data types like structs or
|
||||
* unions.
|
||||
*
|
||||
* Their two major use cases are: (1) Mediating communication between
|
||||
* process-level code and irq/NMI handlers, all running on the same CPU,
|
||||
* and (2) Ensuring that the compiler does not fold, spindle, or otherwise
|
||||
* mutilate accesses that either do not require ordering or that interact
|
||||
* with an explicit memory barrier or atomic instruction that provides the
|
||||
* required ordering.
|
||||
*/
|
||||
#ifndef __BR_RWONCE_H
|
||||
#define __BR_RWONCE_H
|
||||
|
||||
/************ originally in <include/linux/compiler_attributes.h> */
|
||||
#if __has_attribute(__error__)
|
||||
# define __compiletime_error(msg) __attribute__((__error__(msg)))
|
||||
#else
|
||||
# define __compiletime_error(msg)
|
||||
#endif
|
||||
|
||||
/************ originally in <include/linux/compiler_types.h> */
|
||||
/*
|
||||
* __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving
|
||||
* non-scalar types unchanged.
|
||||
*/
|
||||
/*
|
||||
* Prefer C11 _Generic for better compile-times and simpler code. Note: 'char'
|
||||
* is not type-compatible with 'signed char', and we define a separate case.
|
||||
*/
|
||||
#define __scalar_type_to_expr_cases(type) \
|
||||
unsigned type: (unsigned type)0, \
|
||||
signed type: (signed type)0
|
||||
|
||||
#define __unqual_scalar_typeof(x) \
|
||||
typeof(_Generic((x), \
|
||||
char: (char)0, \
|
||||
__scalar_type_to_expr_cases(char), \
|
||||
__scalar_type_to_expr_cases(short), \
|
||||
__scalar_type_to_expr_cases(int), \
|
||||
__scalar_type_to_expr_cases(long), \
|
||||
__scalar_type_to_expr_cases(long long), \
|
||||
default: (x)))
|
||||
|
||||
/* Is this type a native word size -- useful for atomic operations */
|
||||
#define __native_word(t) \
|
||||
(sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || \
|
||||
sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
|
||||
|
||||
#ifdef __OPTIMIZE__
|
||||
# define __compiletime_assert(condition, msg, prefix, suffix) \
|
||||
do { \
|
||||
extern void prefix ## suffix(void) __compiletime_error(msg); \
|
||||
if (!(condition)) \
|
||||
prefix ## suffix(); \
|
||||
} while (0)
|
||||
#else
|
||||
# define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
|
||||
#endif
|
||||
|
||||
#define _compiletime_assert(condition, msg, prefix, suffix) \
|
||||
__compiletime_assert(condition, msg, prefix, suffix)
|
||||
|
||||
/**
|
||||
* compiletime_assert - break build and emit msg if condition is false
|
||||
* @condition: a compile-time constant condition to check
|
||||
* @msg: a message to emit if condition is false
|
||||
*
|
||||
* In tradition of POSIX assert, this macro will break the build if the
|
||||
* supplied condition is *false*, emitting the supplied error message if the
|
||||
* compiler has support to do so.
|
||||
*/
|
||||
#define compiletime_assert(condition, msg) \
|
||||
_compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
|
||||
|
||||
#define compiletime_assert_atomic_type(t) \
|
||||
compiletime_assert(__native_word(t), \
|
||||
"Need native word sized stores/loads for atomicity.")
|
||||
|
||||
/************ originally in <asm-generic/rwonce.h> */
|
||||
/*
|
||||
* Yes, this permits 64-bit accesses on 32-bit architectures. These will
|
||||
* actually be atomic in some cases (namely Armv7 + LPAE), but for others we
|
||||
* rely on the access being split into 2x32-bit accesses for a 32-bit quantity
|
||||
* (e.g. a virtual address) and a strong prevailing wind.
|
||||
*/
|
||||
#define compiletime_assert_rwonce_type(t) \
|
||||
compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long), \
|
||||
"Unsupported access size for {READ,WRITE}_ONCE().")
|
||||
|
||||
/*
|
||||
* Use __READ_ONCE() instead of READ_ONCE() if you do not require any
|
||||
* atomicity. Note that this may result in tears!
|
||||
*/
|
||||
#ifndef __READ_ONCE
|
||||
#define __READ_ONCE(x) (*(const volatile __unqual_scalar_typeof(x) *)&(x))
|
||||
#endif
|
||||
|
||||
#define READ_ONCE(x) \
|
||||
({ \
|
||||
compiletime_assert_rwonce_type(x); \
|
||||
__READ_ONCE(x); \
|
||||
})
|
||||
|
||||
#define __WRITE_ONCE(x, val) \
|
||||
do { \
|
||||
*(volatile typeof(x) *)&(x) = (val); \
|
||||
} while (0)
|
||||
|
||||
#define WRITE_ONCE(x, val) \
|
||||
do { \
|
||||
compiletime_assert_rwonce_type(x); \
|
||||
__WRITE_ONCE(x, val); \
|
||||
} while (0)
|
||||
|
||||
#endif /* __BR_RWONCE_H */
|
||||
@@ -1,105 +0,0 @@
|
||||
/* struct-group.h - mirrored structure macros.
|
||||
*
|
||||
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
* Some parts are taken from Linux's kernel <linux/stddef.h> and others, and are :
|
||||
* SPDX-License-Identifier: GPL-2.0
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _STRUCT_GROUP_H
|
||||
#define _STRUCT_GROUP_H
|
||||
|
||||
/**
|
||||
* __struct_group() - Create a mirrored named and anonyomous struct
|
||||
*
|
||||
* @TAG: The tag name for the named sub-struct (usually empty)
|
||||
* @NAME: The identifier name of the mirrored sub-struct
|
||||
* @ATTRS: Any struct attributes (usually empty)
|
||||
* @MEMBERS: The member declarations for the mirrored structs
|
||||
*
|
||||
* Used to create an anonymous union of two structs with identical layout
|
||||
* and size: one anonymous and one named. The former's members can be used
|
||||
* normally without sub-struct naming, and the latter can be used to
|
||||
* reason about the start, end, and size of the group of struct members.
|
||||
* The named struct can also be explicitly tagged for layer reuse, as well
|
||||
* as both having struct attributes appended.
|
||||
*/
|
||||
#define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
|
||||
union { \
|
||||
struct { MEMBERS } ATTRS; \
|
||||
struct TAG { MEMBERS } ATTRS NAME; \
|
||||
}
|
||||
|
||||
/**
|
||||
* DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
|
||||
*
|
||||
* @TYPE: The type of each flexible array element
|
||||
* @NAME: The name of the flexible array member
|
||||
*
|
||||
* In order to have a flexible array member in a union or alone in a
|
||||
* struct, it needs to be wrapped in an anonymous struct with at least 1
|
||||
* named member, but that member can be empty.
|
||||
*/
|
||||
#define DECLARE_FLEX_ARRAY(TYPE, NAME) \
|
||||
struct { \
|
||||
struct { } __empty_ ## NAME; \
|
||||
TYPE NAME[]; \
|
||||
}
|
||||
|
||||
/**
|
||||
* struct_group() - Wrap a set of declarations in a mirrored struct
|
||||
*
|
||||
* @NAME: The identifier name of the mirrored sub-struct
|
||||
* @MEMBERS: The member declarations for the mirrored structs
|
||||
*
|
||||
* Used to create an anonymous union of two structs with identical
|
||||
* layout and size: one anonymous and one named. The former can be
|
||||
* used normally without sub-struct naming, and the latter can be
|
||||
* used to reason about the start, end, and size of the group of
|
||||
* struct members.
|
||||
*/
|
||||
#define struct_group(NAME, MEMBERS...) \
|
||||
__struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS)
|
||||
|
||||
/**
|
||||
* struct_group_attr() - Create a struct_group() with trailing attributes
|
||||
*
|
||||
* @NAME: The identifier name of the mirrored sub-struct
|
||||
* @ATTRS: Any struct attributes to apply
|
||||
* @MEMBERS: The member declarations for the mirrored structs
|
||||
*
|
||||
* Used to create an anonymous union of two structs with identical
|
||||
* layout and size: one anonymous and one named. The former can be
|
||||
* used normally without sub-struct naming, and the latter can be
|
||||
* used to reason about the start, end, and size of the group of
|
||||
* struct members. Includes structure attributes argument.
|
||||
*/
|
||||
#define struct_group_attr(NAME, ATTRS, MEMBERS...) \
|
||||
__struct_group(/* no tag */, NAME, ATTRS, MEMBERS)
|
||||
|
||||
/**
|
||||
* struct_group_tagged() - Create a struct_group with a reusable tag
|
||||
*
|
||||
* @TAG: The tag name for the named sub-struct
|
||||
* @NAME: The identifier name of the mirrored sub-struct
|
||||
* @MEMBERS: The member declarations for the mirrored structs
|
||||
*
|
||||
* Used to create an anonymous union of two structs with identical
|
||||
* layout and size: one anonymous and one named. The former can be
|
||||
* used normally without sub-struct naming, and the latter can be
|
||||
* used to reason about the start, end, and size of the group of
|
||||
* struct members. Includes struct tag argument for the named copy,
|
||||
* so the specified layout can be reused later.
|
||||
*/
|
||||
#define struct_group_tagged(TAG, NAME, MEMBERS...) \
|
||||
__struct_group(TAG, NAME, /* no attrs */, MEMBERS)
|
||||
|
||||
#endif /* _STRUCT_GROUP_H */
|
||||
146
libsrc/debug.c
146
libsrc/debug.c
@@ -1,146 +0,0 @@
|
||||
/* debug.c - debug/log management
|
||||
*
|
||||
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifndef DEBUG_DEBUG
|
||||
#define DEBUG_DEBUG
|
||||
#endif
|
||||
|
||||
#include "bits.h"
|
||||
#include "debug.h"
|
||||
|
||||
static long long timer_start; /* in nanosecond */
|
||||
static uint debug_level = 0;
|
||||
static bool debug_flush = false;
|
||||
static FILE *stream = NULL;
|
||||
|
||||
/**
|
||||
* debug_level_set() - set debug level.
|
||||
* @level: unsigned debug level.
|
||||
*/
|
||||
void debug_level_set(uint level)
|
||||
{
|
||||
debug_level = level;
|
||||
# ifdef DEBUG_DEBUG_C
|
||||
log(0, "debug level set to %u\n", level);
|
||||
# endif
|
||||
}
|
||||
|
||||
/**
|
||||
* debug_level_get() - get debug level.
|
||||
* @return: current level debug (unsigned integer).
|
||||
*/
|
||||
uint debug_level_get(void)
|
||||
{
|
||||
return debug_level;
|
||||
}
|
||||
|
||||
void debug_stream_set(FILE *_stream)
|
||||
{
|
||||
stream = _stream;
|
||||
# ifdef DEBUG_DEBUG_C
|
||||
log(0, "stream set to %d\n", stream? fileno(stream): -1);
|
||||
# endif
|
||||
}
|
||||
|
||||
void debug_flush_set(bool flush)
|
||||
{
|
||||
debug_flush = flush;
|
||||
# ifdef DEBUG_DEBUG_C
|
||||
log(0, "debug flush %s.\n", flush? "set": "unset");
|
||||
# endif
|
||||
}
|
||||
|
||||
void debug_init(uint level, FILE *_stream, bool flush)
|
||||
{
|
||||
struct timespec timer;
|
||||
|
||||
debug_stream_set(_stream);
|
||||
debug_level_set(level);
|
||||
debug_flush_set(flush);
|
||||
if (!clock_gettime(CLOCK_MONOTONIC, &timer)) {
|
||||
timer_start = timer.tv_sec * NANOSEC + timer.tv_nsec;
|
||||
}
|
||||
else {
|
||||
timer_start = 0;
|
||||
}
|
||||
log(0, "timer started.\n");
|
||||
}
|
||||
|
||||
long long debug_timer_elapsed(void)
|
||||
{
|
||||
struct timespec timer;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &timer);
|
||||
return (timer.tv_sec * NANOSEC + timer.tv_nsec) - timer_start;
|
||||
}
|
||||
|
||||
/**
|
||||
* debug() - log function
|
||||
* @level: log level
|
||||
* @timestamp: boolean, print timestamp if true
|
||||
* @indent: indent level (2 spaces each)
|
||||
* @src: source file/func name (or NULL)
|
||||
* @line: line number
|
||||
*/
|
||||
void debug(uint level, bool timestamp, uint indent, const char *src,
|
||||
uint line, const char *fmt, ...)
|
||||
{
|
||||
if (!stream || level > debug_level)
|
||||
return;
|
||||
|
||||
va_list ap;
|
||||
|
||||
if (indent)
|
||||
fprintf(stream, "%*s", 2*(indent-1), "");
|
||||
|
||||
if (timestamp) {
|
||||
long long diff = debug_timer_elapsed();
|
||||
fprintf(stream, "%lld.%03lld ", diff/NANOSEC, (diff/1000000)%1000);
|
||||
fprintf(stream, "%010lld ", diff);
|
||||
}
|
||||
|
||||
if (src) {
|
||||
if (line)
|
||||
fprintf(stream, "[%s:%u] ", src, line);
|
||||
else
|
||||
fprintf(stream, "[%s] ", src);
|
||||
}
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stream, fmt, ap);
|
||||
va_end(ap);
|
||||
if (debug_flush)
|
||||
fflush(stream);
|
||||
}
|
||||
|
||||
#ifdef BIN_debug
|
||||
#include <unistd.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int foo=1;
|
||||
debug_init(5);
|
||||
|
||||
log(0, "log0=%d\n", foo++);
|
||||
log(1, "log1=%d\n", foo++);
|
||||
log(2, "log2=%d\n", foo++);
|
||||
log_i(2, "log_i 2=%d\n", foo++);
|
||||
log_i(5, "log_i 5=%d\n", foo++);
|
||||
log_i(6, "log_i 6=%d\n", foo++);
|
||||
log_it(4, "log_it 4=%d\n", foo++);
|
||||
log_f(1, "log_f 5=%d\n", foo++);
|
||||
}
|
||||
#endif
|
||||
@@ -1,29 +0,0 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
|
||||
/* inspired from kernel's <fs/namei.h>
|
||||
*/
|
||||
#include "hash.h"
|
||||
|
||||
/* Return the hash of a string of known length */
|
||||
unsigned int hash_string(const void *salt, const char *name, unsigned int len)
|
||||
{
|
||||
unsigned long hash = init_name_hash(salt);
|
||||
while (len--)
|
||||
hash = partial_name_hash((unsigned char)*name++, hash);
|
||||
return end_name_hash(hash);
|
||||
}
|
||||
|
||||
/* Return the "hash_len" (hash and length) of a null-terminated string */
|
||||
u64 hashlen_string(const void *salt, const char *name)
|
||||
{
|
||||
unsigned long hash = init_name_hash(salt);
|
||||
unsigned long len = 0, c;
|
||||
|
||||
c = (unsigned char)*name;
|
||||
while (c) {
|
||||
len++;
|
||||
hash = partial_name_hash(c, hash);
|
||||
c = (unsigned char)name[len];
|
||||
}
|
||||
return hashlen_create(end_name_hash(hash), len);
|
||||
}
|
||||
@@ -1,253 +0,0 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
/*
|
||||
* Taken from linux kernel: lib/list_sort.c
|
||||
*/
|
||||
#include "list_sort.h"
|
||||
#include "list.h"
|
||||
#include "bits.h"
|
||||
#include "likely.h"
|
||||
|
||||
/*
|
||||
* Returns a list organized in an intermediate format suited
|
||||
* to chaining of merge() calls: null-terminated, no reserved or
|
||||
* sentinel head node, "prev" links not maintained.
|
||||
*/
|
||||
__attribute__((nonnull(2,3,4)))
|
||||
static struct list_head *merge(void *priv, list_cmp_func_t cmp,
|
||||
struct list_head *a, struct list_head *b)
|
||||
{
|
||||
struct list_head *head, **tail = &head;
|
||||
|
||||
for (;;) {
|
||||
/* if equal, take 'a' -- important for sort stability */
|
||||
if (cmp(priv, a, b) <= 0) {
|
||||
*tail = a;
|
||||
tail = &a->next;
|
||||
a = a->next;
|
||||
if (!a) {
|
||||
*tail = b;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
*tail = b;
|
||||
tail = &b->next;
|
||||
b = b->next;
|
||||
if (!b) {
|
||||
*tail = a;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/*
|
||||
* Combine final list merge with restoration of standard doubly-linked
|
||||
* list structure. This approach duplicates code from merge(), but
|
||||
* runs faster than the tidier alternatives of either a separate final
|
||||
* prev-link restoration pass, or maintaining the prev links
|
||||
* throughout.
|
||||
*/
|
||||
__attribute__((nonnull(2,3,4,5)))
|
||||
static void merge_final(void *priv, list_cmp_func_t cmp, struct list_head *head,
|
||||
struct list_head *a, struct list_head *b)
|
||||
{
|
||||
struct list_head *tail = head;
|
||||
u8 count = 0;
|
||||
|
||||
for (;;) {
|
||||
/* if equal, take 'a' -- important for sort stability */
|
||||
if (cmp(priv, a, b) <= 0) {
|
||||
tail->next = a;
|
||||
a->prev = tail;
|
||||
tail = a;
|
||||
a = a->next;
|
||||
if (!a)
|
||||
break;
|
||||
} else {
|
||||
tail->next = b;
|
||||
b->prev = tail;
|
||||
tail = b;
|
||||
b = b->next;
|
||||
if (!b) {
|
||||
b = a;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Finish linking remainder of list b on to tail */
|
||||
tail->next = b;
|
||||
do {
|
||||
/*
|
||||
* If the merge is highly unbalanced (e.g. the input is
|
||||
* already sorted), this loop may run many iterations.
|
||||
* Continue callbacks to the client even though no
|
||||
* element comparison is needed, so the client's cmp()
|
||||
* routine can invoke cond_resched() periodically.
|
||||
*/
|
||||
if (unlikely(!++count))
|
||||
cmp(priv, b, b);
|
||||
b->prev = tail;
|
||||
tail = b;
|
||||
b = b->next;
|
||||
} while (b);
|
||||
|
||||
/* And the final links to make a circular doubly-linked list */
|
||||
tail->next = head;
|
||||
head->prev = tail;
|
||||
}
|
||||
|
||||
/**
|
||||
* list_sort - sort a list
|
||||
* @priv: private data, opaque to list_sort(), passed to @cmp
|
||||
* @head: the list to sort
|
||||
* @cmp: the elements comparison function
|
||||
*
|
||||
* The comparison function @cmp must return > 0 if @a should sort after
|
||||
* @b ("@a > @b" if you want an ascending sort), and <= 0 if @a should
|
||||
* sort before @b *or* their original order should be preserved. It is
|
||||
* always called with the element that came first in the input in @a,
|
||||
* and list_sort is a stable sort, so it is not necessary to distinguish
|
||||
* the @a < @b and @a == @b cases.
|
||||
*
|
||||
* This is compatible with two styles of @cmp function:
|
||||
* - The traditional style which returns <0 / =0 / >0, or
|
||||
* - Returning a boolean 0/1.
|
||||
* The latter offers a chance to save a few cycles in the comparison
|
||||
* (which is used by e.g. plug_ctx_cmp() in block/blk-mq.c).
|
||||
*
|
||||
* A good way to write a multi-word comparison is::
|
||||
*
|
||||
* if (a->high != b->high)
|
||||
* return a->high > b->high;
|
||||
* if (a->middle != b->middle)
|
||||
* return a->middle > b->middle;
|
||||
* return a->low > b->low;
|
||||
*
|
||||
*
|
||||
* This mergesort is as eager as possible while always performing at least
|
||||
* 2:1 balanced merges. Given two pending sublists of size 2^k, they are
|
||||
* merged to a size-2^(k+1) list as soon as we have 2^k following elements.
|
||||
*
|
||||
* Thus, it will avoid cache thrashing as long as 3*2^k elements can
|
||||
* fit into the cache. Not quite as good as a fully-eager bottom-up
|
||||
* mergesort, but it does use 0.2*n fewer comparisons, so is faster in
|
||||
* the common case that everything fits into L1.
|
||||
*
|
||||
*
|
||||
* The merging is controlled by "count", the number of elements in the
|
||||
* pending lists. This is beautifully simple code, but rather subtle.
|
||||
*
|
||||
* Each time we increment "count", we set one bit (bit k) and clear
|
||||
* bits k-1 .. 0. Each time this happens (except the very first time
|
||||
* for each bit, when count increments to 2^k), we merge two lists of
|
||||
* size 2^k into one list of size 2^(k+1).
|
||||
*
|
||||
* This merge happens exactly when the count reaches an odd multiple of
|
||||
* 2^k, which is when we have 2^k elements pending in smaller lists,
|
||||
* so it's safe to merge away two lists of size 2^k.
|
||||
*
|
||||
* After this happens twice, we have created two lists of size 2^(k+1),
|
||||
* which will be merged into a list of size 2^(k+2) before we create
|
||||
* a third list of size 2^(k+1), so there are never more than two pending.
|
||||
*
|
||||
* The number of pending lists of size 2^k is determined by the
|
||||
* state of bit k of "count" plus two extra pieces of information:
|
||||
*
|
||||
* - The state of bit k-1 (when k == 0, consider bit -1 always set), and
|
||||
* - Whether the higher-order bits are zero or non-zero (i.e.
|
||||
* is count >= 2^(k+1)).
|
||||
*
|
||||
* There are six states we distinguish. "x" represents some arbitrary
|
||||
* bits, and "y" represents some arbitrary non-zero bits:
|
||||
* 0: 00x: 0 pending of size 2^k; x pending of sizes < 2^k
|
||||
* 1: 01x: 0 pending of size 2^k; 2^(k-1) + x pending of sizes < 2^k
|
||||
* 2: x10x: 0 pending of size 2^k; 2^k + x pending of sizes < 2^k
|
||||
* 3: x11x: 1 pending of size 2^k; 2^(k-1) + x pending of sizes < 2^k
|
||||
* 4: y00x: 1 pending of size 2^k; 2^k + x pending of sizes < 2^k
|
||||
* 5: y01x: 2 pending of size 2^k; 2^(k-1) + x pending of sizes < 2^k
|
||||
* (merge and loop back to state 2)
|
||||
*
|
||||
* We gain lists of size 2^k in the 2->3 and 4->5 transitions (because
|
||||
* bit k-1 is set while the more significant bits are non-zero) and
|
||||
* merge them away in the 5->2 transition. Note in particular that just
|
||||
* before the 5->2 transition, all lower-order bits are 11 (state 3),
|
||||
* so there is one list of each smaller size.
|
||||
*
|
||||
* When we reach the end of the input, we merge all the pending
|
||||
* lists, from smallest to largest. If you work through cases 2 to
|
||||
* 5 above, you can see that the number of elements we merge with a list
|
||||
* of size 2^k varies from 2^(k-1) (cases 3 and 5 when x == 0) to
|
||||
* 2^(k+1) - 1 (second merge of case 5 when x == 2^(k-1) - 1).
|
||||
*/
|
||||
__attribute__((nonnull(2,3)))
|
||||
void list_sort(void *priv, struct list_head *head, list_cmp_func_t cmp)
|
||||
{
|
||||
struct list_head *list = head->next, *pending = NULL;
|
||||
size_t count = 0; /* Count of pending */
|
||||
|
||||
if (list == head->prev) /* Zero or one elements */
|
||||
return;
|
||||
|
||||
/* Convert to a null-terminated singly-linked list. */
|
||||
head->prev->next = NULL;
|
||||
|
||||
/*
|
||||
* Data structure invariants:
|
||||
* - All lists are singly linked and null-terminated; prev
|
||||
* pointers are not maintained.
|
||||
* - pending is a prev-linked "list of lists" of sorted
|
||||
* sublists awaiting further merging.
|
||||
* - Each of the sorted sublists is power-of-two in size.
|
||||
* - Sublists are sorted by size and age, smallest & newest at front.
|
||||
* - There are zero to two sublists of each size.
|
||||
* - A pair of pending sublists are merged as soon as the number
|
||||
* of following pending elements equals their size (i.e.
|
||||
* each time count reaches an odd multiple of that size).
|
||||
* That ensures each later final merge will be at worst 2:1.
|
||||
* - Each round consists of:
|
||||
* - Merging the two sublists selected by the highest bit
|
||||
* which flips when count is incremented, and
|
||||
* - Adding an element from the input as a size-1 sublist.
|
||||
*/
|
||||
do {
|
||||
size_t bits;
|
||||
struct list_head **tail = &pending;
|
||||
|
||||
/* Find the least-significant clear bit in count */
|
||||
for (bits = count; bits & 1; bits >>= 1)
|
||||
tail = &(*tail)->prev;
|
||||
/* Do the indicated merge */
|
||||
if (likely(bits)) {
|
||||
struct list_head *a = *tail, *b = a->prev;
|
||||
|
||||
a = merge(priv, cmp, b, a);
|
||||
/* Install the merged result in place of the inputs */
|
||||
a->prev = b->prev;
|
||||
*tail = a;
|
||||
}
|
||||
|
||||
/* Move one element from input list to pending */
|
||||
list->prev = pending;
|
||||
pending = list;
|
||||
list = list->next;
|
||||
pending->next = NULL;
|
||||
count++;
|
||||
} while (list);
|
||||
|
||||
/* End of input; merge together all the pending lists. */
|
||||
list = pending;
|
||||
pending = pending->prev;
|
||||
for (;;) {
|
||||
struct list_head *next = pending->prev;
|
||||
|
||||
if (!next)
|
||||
break;
|
||||
list = merge(priv, cmp, pending, list);
|
||||
pending = next;
|
||||
}
|
||||
/* The final merge, rebuilding prev links */
|
||||
merge_final(priv, cmp, head, pending, list);
|
||||
}
|
||||
173
libsrc/plist.c
173
libsrc/plist.c
@@ -1,173 +0,0 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
/*
|
||||
* adapted from Linux kernel lib/plist.c
|
||||
*
|
||||
* Descending-priority-sorted double-linked list
|
||||
*
|
||||
* (C) 2002-2003 Intel Corp
|
||||
* Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>.
|
||||
*
|
||||
* 2001-2005 (c) MontaVista Software, Inc.
|
||||
* Daniel Walker <dwalker@mvista.com>
|
||||
*
|
||||
* (C) 2005 Thomas Gleixner <tglx@linutronix.de>
|
||||
*
|
||||
* Simplifications of the original code by
|
||||
* Oleg Nesterov <oleg@tv-sign.ru>
|
||||
*
|
||||
* Based on simple lists (include/linux/list.h).
|
||||
*
|
||||
* This file contains the add / del functions which are considered to
|
||||
* be too large to inline. See include/linux/plist.h for further
|
||||
* information.
|
||||
*/
|
||||
|
||||
#include "plist.h"
|
||||
#include "bug.h"
|
||||
|
||||
#ifdef DEBUG_PLIST
|
||||
|
||||
static struct plist_head test_head;
|
||||
|
||||
static void plist_check_prev_next(struct list_head *t, struct list_head *p,
|
||||
struct list_head *n)
|
||||
{
|
||||
WARN(n->prev != p || p->next != n,
|
||||
"top: %p, n: %p, p: %p\n"
|
||||
"prev: %p, n: %p, p: %p\n"
|
||||
"next: %p, n: %p, p: %p\n",
|
||||
t, t->next, t->prev,
|
||||
p, p->next, p->prev,
|
||||
n, n->next, n->prev);
|
||||
}
|
||||
|
||||
static void plist_check_list(struct list_head *top)
|
||||
{
|
||||
struct list_head *prev = top, *next = top->next;
|
||||
|
||||
plist_check_prev_next(top, prev, next);
|
||||
while (next != top) {
|
||||
prev = next;
|
||||
next = prev->next;
|
||||
plist_check_prev_next(top, prev, next);
|
||||
}
|
||||
}
|
||||
|
||||
static void plist_check_head(struct plist_head *head)
|
||||
{
|
||||
if (!plist_head_empty(head))
|
||||
plist_check_list(&plist_first(head)->prio_list);
|
||||
plist_check_list(&head->node_list);
|
||||
}
|
||||
|
||||
#else
|
||||
# define plist_check_head(h) do { } while (0)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* plist_add - add @node to @head
|
||||
*
|
||||
* @node: &struct plist_node pointer
|
||||
* @head: &struct plist_head pointer
|
||||
*/
|
||||
void plist_add(struct plist_node *node, struct plist_head *head)
|
||||
{
|
||||
struct plist_node *first, *iter, *prev = NULL;
|
||||
struct list_head *node_next = &head->node_list;
|
||||
|
||||
plist_check_head(head);
|
||||
WARN_ON(!plist_node_empty(node));
|
||||
WARN_ON(!list_empty(&node->prio_list));
|
||||
|
||||
if (plist_head_empty(head))
|
||||
goto ins_node;
|
||||
|
||||
first = iter = plist_first(head);
|
||||
|
||||
do {
|
||||
if (node->prio < iter->prio) {
|
||||
node_next = &iter->node_list;
|
||||
break;
|
||||
}
|
||||
|
||||
prev = iter;
|
||||
iter = list_entry(iter->prio_list.next,
|
||||
struct plist_node, prio_list);
|
||||
} while (iter != first);
|
||||
|
||||
if (!prev || prev->prio != node->prio)
|
||||
list_add_tail(&node->prio_list, &iter->prio_list);
|
||||
ins_node:
|
||||
list_add_tail(&node->node_list, node_next);
|
||||
|
||||
plist_check_head(head);
|
||||
}
|
||||
|
||||
/**
|
||||
* plist_del - Remove a @node from plist.
|
||||
*
|
||||
* @node: &struct plist_node pointer - entry to be removed
|
||||
* @head: &struct plist_head pointer - list head
|
||||
*/
|
||||
void plist_del(struct plist_node *node, struct plist_head *head)
|
||||
{
|
||||
plist_check_head(head);
|
||||
|
||||
if (!list_empty(&node->prio_list)) {
|
||||
if (node->node_list.next != &head->node_list) {
|
||||
struct plist_node *next;
|
||||
|
||||
next = list_entry(node->node_list.next,
|
||||
struct plist_node, node_list);
|
||||
|
||||
/* add the next plist_node into prio_list */
|
||||
if (list_empty(&next->prio_list))
|
||||
list_add(&next->prio_list, &node->prio_list);
|
||||
}
|
||||
list_del_init(&node->prio_list);
|
||||
}
|
||||
|
||||
list_del_init(&node->node_list);
|
||||
|
||||
plist_check_head(head);
|
||||
}
|
||||
|
||||
/**
|
||||
* plist_requeue - Requeue @node at end of same-prio entries.
|
||||
*
|
||||
* This is essentially an optimized plist_del() followed by
|
||||
* plist_add(). It moves an entry already in the plist to
|
||||
* after any other same-priority entries.
|
||||
*
|
||||
* @node: &struct plist_node pointer - entry to be moved
|
||||
* @head: &struct plist_head pointer - list head
|
||||
*/
|
||||
void plist_requeue(struct plist_node *node, struct plist_head *head)
|
||||
{
|
||||
struct plist_node *iter;
|
||||
struct list_head *node_next = &head->node_list;
|
||||
|
||||
plist_check_head(head);
|
||||
BUG_ON(plist_head_empty(head));
|
||||
BUG_ON(plist_node_empty(node));
|
||||
|
||||
if (node == plist_last(head))
|
||||
return;
|
||||
|
||||
iter = plist_next(node);
|
||||
|
||||
if (node->prio != iter->prio)
|
||||
return;
|
||||
|
||||
plist_del(node, head);
|
||||
|
||||
plist_for_each_continue(iter, head) {
|
||||
if (node->prio != iter->prio) {
|
||||
node_next = &iter->node_list;
|
||||
break;
|
||||
}
|
||||
}
|
||||
list_add_tail(&node->node_list, node_next);
|
||||
|
||||
plist_check_head(head);
|
||||
}
|
||||
219
libsrc/pool.c
219
libsrc/pool.c
@@ -1,219 +0,0 @@
|
||||
/* pool.c - A simple pool manager.
|
||||
*
|
||||
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "list.h"
|
||||
#include "pool.h"
|
||||
#include "debug.h"
|
||||
#include "bits.h"
|
||||
|
||||
void pool_stats(pool_t *pool)
|
||||
{
|
||||
if (pool) {
|
||||
block_t *block;
|
||||
|
||||
log_f(1, "[%s] pool [%p]: blocks:%u avail:%u alloc:%u grow:%u eltsize:%zu\n",
|
||||
pool->name, (void *)pool, pool->nblocks, pool->available,
|
||||
pool->allocated, pool->growsize, pool->eltsize);
|
||||
log(5, "\tblocks: ");
|
||||
list_for_each_entry(block, &pool->list_blocks, list_blocks) {
|
||||
log(5, "%p ", block);
|
||||
}
|
||||
log(5, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
pool_t *pool_create(const char *name, u32 growsize, size_t eltsize)
|
||||
{
|
||||
pool_t *pool;
|
||||
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(1, "name=[%s] growsize=%u eltsize=%zu\n", name, growsize, eltsize);
|
||||
# endif
|
||||
/* we need at least sizeof(struct list_head) space in pool elements
|
||||
*/
|
||||
if (eltsize < sizeof (struct list_head)) {
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(1, "[%s]: structure size too small (%zu < %zu), adjusting to %zu.\n",
|
||||
name, eltsize, sizeof(struct list_head), sizeof(struct list_head));
|
||||
# endif
|
||||
eltsize = sizeof(struct list_head);
|
||||
}
|
||||
if ((pool = malloc(sizeof (*pool)))) {
|
||||
strncpy(pool->name, name, POOL_NAME_LENGTH - 1);
|
||||
pool->name[POOL_NAME_LENGTH - 1] = 0;
|
||||
pool->growsize = growsize;
|
||||
pool->eltsize = eltsize;
|
||||
pool->available = 0;
|
||||
pool->allocated = 0;
|
||||
pool->nblocks = 0;
|
||||
INIT_LIST_HEAD(&pool->list_available);
|
||||
INIT_LIST_HEAD(&pool->list_blocks);
|
||||
} else {
|
||||
errno = ENOMEM;
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
|
||||
static u32 _pool_add(pool_t *pool, struct list_head *elt)
|
||||
{
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(6, "pool=%p &head=%p elt=%p off1=%zu off2=%zu\n",
|
||||
(void *)pool, (void *)&pool->list_available, (void *)elt,
|
||||
(void *)&pool->list_available - (void *)pool,
|
||||
offsetof(pool_t, list_available));
|
||||
# endif
|
||||
|
||||
list_add(elt, &pool->list_available);
|
||||
return ++pool->available;
|
||||
}
|
||||
|
||||
u32 pool_add(pool_t *pool, void *elt)
|
||||
{
|
||||
return _pool_add(pool, elt);
|
||||
}
|
||||
|
||||
static struct list_head *_pool_get(pool_t *pool)
|
||||
{
|
||||
struct list_head *res = pool->list_available.next;
|
||||
pool->available--;
|
||||
list_del(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
void *pool_get(pool_t *pool)
|
||||
{
|
||||
if (!pool)
|
||||
return NULL;
|
||||
if (!pool->available) {
|
||||
block_t *block = malloc(sizeof(block_t) + pool->eltsize * pool->growsize);
|
||||
if (!block) {
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(1, "[%s]: failed block allocation\n", pool->name);
|
||||
# endif
|
||||
errno = ENOMEM;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* maintain list of allocated blocks
|
||||
*/
|
||||
list_add(&block->list_blocks, &pool->list_blocks);
|
||||
pool->nblocks++;
|
||||
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(1, "[%s]: growing pool from %u to %u elements. block=%p nblocks=%u\n",
|
||||
pool->name,
|
||||
pool->allocated,
|
||||
pool->allocated + pool->growsize,
|
||||
block,
|
||||
pool->nblocks);
|
||||
# endif
|
||||
|
||||
pool->allocated += pool->growsize;
|
||||
for (u32 i = 0; i < pool->growsize; ++i) {
|
||||
void *cur = block->data + i * pool->eltsize;
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(7, "alloc=%p cur=%p\n", block, cur);
|
||||
# endif
|
||||
_pool_add(pool, (struct list_head *)cur);
|
||||
}
|
||||
}
|
||||
/* this is the effective address of the object (and also the
|
||||
* pool list_head address)
|
||||
*/
|
||||
return _pool_get(pool);
|
||||
}
|
||||
|
||||
void pool_destroy(pool_t *pool)
|
||||
{
|
||||
block_t *block, *tmp;
|
||||
if (!pool)
|
||||
return;
|
||||
/* release memory blocks */
|
||||
# ifdef DEBUG_POOL
|
||||
log_f(1, "[%s]: releasing %d blocks and main structure\n", pool->name, pool->nblocks);
|
||||
log(5, "blocks:");
|
||||
# endif
|
||||
list_for_each_entry_safe(block, tmp, &pool->list_blocks, list_blocks) {
|
||||
# ifdef DEBUG_POOL
|
||||
log(5, " %p", block);
|
||||
# endif
|
||||
list_del(&block->list_blocks);
|
||||
free(block);
|
||||
}
|
||||
# ifdef DEBUG_POOL
|
||||
log(5, "\n");
|
||||
# endif
|
||||
free(pool);
|
||||
}
|
||||
|
||||
#ifdef BIN_pool
|
||||
struct d {
|
||||
u16 data1;
|
||||
char c;
|
||||
struct list_head list;
|
||||
};
|
||||
|
||||
static LIST_HEAD (head);
|
||||
|
||||
int main(int ac, char**av)
|
||||
{
|
||||
pool_t *pool;
|
||||
int total;
|
||||
int action=0;
|
||||
u16 icur=0;
|
||||
char ccur='z';
|
||||
struct d *elt;
|
||||
|
||||
debug_init(3);
|
||||
log_f(1, "%s: sizeof(d)=%lu sizeof(*d)=%lu off=%lu\n", *av, sizeof(elt),
|
||||
sizeof(*elt), offsetof(struct d, list));
|
||||
|
||||
if ((pool = pool_create("dummy", 3, sizeof(*elt)))) {
|
||||
pool_stats(pool);
|
||||
for (int cur=1; cur<ac; ++cur) {
|
||||
total = atoi(av[cur]);
|
||||
if (action == 0) { /* add elt to list */
|
||||
log_f(2, "adding %d elements\n", total);
|
||||
for (int i = 0; i < total; ++i) {
|
||||
elt = pool_get(pool);
|
||||
elt->data1 = icur++;
|
||||
elt->c = ccur--;
|
||||
list_add(&elt->list, &head);
|
||||
}
|
||||
pool_stats(pool);
|
||||
action = 1;
|
||||
} else { /* remove one elt from list */
|
||||
log_f(2, "deleting %d elements\n", total);
|
||||
for (int i = 0; i < total; ++i) {
|
||||
if (!list_empty(&head)) {
|
||||
elt = list_last_entry(&head, struct d, list);
|
||||
printf("elt=[%d, %c]\n", elt->data1, elt->c);
|
||||
list_del(&elt->list);
|
||||
pool_add(pool, elt);
|
||||
}
|
||||
}
|
||||
pool_stats(pool);
|
||||
action = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
pool_stats(pool);
|
||||
pool_destroy(pool);
|
||||
}
|
||||
#endif
|
||||
543
resources/engine-interface.txt
Normal file
543
resources/engine-interface.txt
Normal file
@@ -0,0 +1,543 @@
|
||||
|
||||
|
||||
Description of the universal chess interface (UCI) April 2006
|
||||
=================================================================
|
||||
|
||||
* The specification is independent of the operating system. For Windows,
|
||||
the engine is a normal exe file, either a console or "real" windows application.
|
||||
|
||||
* all communication is done via standard input and output with text commands,
|
||||
|
||||
* The engine should boot and wait for input from the GUI,
|
||||
the engine should wait for the "isready" or "setoption" command to set up its internal parameters
|
||||
as the boot process should be as quick as possible.
|
||||
|
||||
* the engine must always be able to process input from stdin, even while thinking.
|
||||
|
||||
* all command strings the engine receives will end with '\n',
|
||||
also all commands the GUI receives should end with '\n',
|
||||
Note: '\n' can be 0x0d or 0x0a0d or any combination depending on your OS.
|
||||
If you use Engine and GUI in the same OS this should be no problem if you communicate in text mode,
|
||||
but be aware of this when for example running a Linux engine in a Windows GUI.
|
||||
|
||||
* arbitrary white space between tokens is allowed
|
||||
Example: "debug on\n" and " debug on \n" and "\t debug \t \t\ton\t \n"
|
||||
all set the debug mode of the engine on.
|
||||
|
||||
* The engine will always be in forced mode which means it should never start calculating
|
||||
or pondering without receiving a "go" command first.
|
||||
|
||||
* Before the engine is asked to search on a position, there will always be a position command
|
||||
to tell the engine about the current position.
|
||||
|
||||
* by default all the opening book handling is done by the GUI,
|
||||
but there is an option for the engine to use its own book ("OwnBook" option, see below)
|
||||
|
||||
* if the engine or the GUI receives an unknown command or token it should just ignore it and try to
|
||||
parse the rest of the string in this line.
|
||||
Examples: "joho debug on\n" should switch the debug mode on given that joho is not defined,
|
||||
"debug joho on\n" will be undefined however.
|
||||
|
||||
* if the engine receives a command which is not supposed to come, for example "stop" when the engine is
|
||||
not calculating, it should also just ignore it.
|
||||
|
||||
|
||||
Move format:
|
||||
------------
|
||||
|
||||
The move format is in long algebraic notation.
|
||||
A nullmove from the Engine to the GUI should be sent as 0000.
|
||||
Examples: e2e4, e7e5, e1g1 (white short castling), e7e8q (for promotion)
|
||||
|
||||
|
||||
|
||||
GUI to engine:
|
||||
--------------
|
||||
|
||||
These are all the command the engine gets from the interface.
|
||||
|
||||
* uci
|
||||
tell engine to use the uci (universal chess interface),
|
||||
this will be sent once as a first command after program boot
|
||||
to tell the engine to switch to uci mode.
|
||||
After receiving the uci command the engine must identify itself with the "id" command
|
||||
and send the "option" commands to tell the GUI which engine settings the engine supports if any.
|
||||
After that the engine should send "uciok" to acknowledge the uci mode.
|
||||
If no uciok is sent within a certain time period, the engine task will be killed by the GUI.
|
||||
|
||||
* debug [ on | off ]
|
||||
switch the debug mode of the engine on and off.
|
||||
In debug mode the engine should send additional infos to the GUI, e.g. with the "info string" command,
|
||||
to help debugging, e.g. the commands that the engine has received etc.
|
||||
This mode should be switched off by default and this command can be sent
|
||||
any time, also when the engine is thinking.
|
||||
|
||||
* isready
|
||||
this is used to synchronize the engine with the GUI. When the GUI has sent a command or
|
||||
multiple commands that can take some time to complete,
|
||||
this command can be used to wait for the engine to be ready again or
|
||||
to ping the engine to find out if it is still alive.
|
||||
E.g. this should be sent after setting the path to the tablebases as this can take some time.
|
||||
This command is also required once before the engine is asked to do any search
|
||||
to wait for the engine to finish initializing.
|
||||
This command must always be answered with "readyok" and can be sent also when the engine is calculating
|
||||
in which case the engine should also immediately answer with "readyok" without stopping the search.
|
||||
|
||||
* setoption name <id> [value <x>]
|
||||
this is sent to the engine when the user wants to change the internal parameters
|
||||
of the engine. For the "button" type no value is needed.
|
||||
One string will be sent for each parameter and this will only be sent when the engine is waiting.
|
||||
The name and value of the option in <id> should not be case sensitive and can inlude spaces.
|
||||
The substrings "value" and "name" should be avoided in <id> and <x> to allow unambiguous parsing,
|
||||
for example do not use <name> = "draw value".
|
||||
Here are some strings for the example below:
|
||||
"setoption name Nullmove value true\n"
|
||||
"setoption name Selectivity value 3\n"
|
||||
"setoption name Style value Risky\n"
|
||||
"setoption name Clear Hash\n"
|
||||
"setoption name NalimovPath value c:\chess\tb\4;c:\chess\tb\5\n"
|
||||
|
||||
* register
|
||||
this is the command to try to register an engine or to tell the engine that registration
|
||||
will be done later. This command should always be sent if the engine has sent "registration error"
|
||||
at program startup.
|
||||
The following tokens are allowed:
|
||||
* later
|
||||
the user doesn't want to register the engine now.
|
||||
* name <x>
|
||||
the engine should be registered with the name <x>
|
||||
* code <y>
|
||||
the engine should be registered with the code <y>
|
||||
Example:
|
||||
"register later"
|
||||
"register name Stefan MK code 4359874324"
|
||||
|
||||
* ucinewgame
|
||||
this is sent to the engine when the next search (started with "position" and "go") will be from
|
||||
a different game. This can be a new game the engine should play or a new game it should analyse but
|
||||
also the next position from a testsuite with positions only.
|
||||
If the GUI hasn't sent a "ucinewgame" before the first "position" command, the engine shouldn't
|
||||
expect any further ucinewgame commands as the GUI is probably not supporting the ucinewgame command.
|
||||
So the engine should not rely on this command even though all new GUIs should support it.
|
||||
As the engine's reaction to "ucinewgame" can take some time the GUI should always send "isready"
|
||||
after "ucinewgame" to wait for the engine to finish its operation.
|
||||
|
||||
* position [fen <fenstring> | startpos ] moves <move1> .... <movei>
|
||||
set up the position described in fenstring on the internal board and
|
||||
play the moves on the internal chess board.
|
||||
if the game was played from the start position the string "startpos" will be sent
|
||||
Note: no "new" command is needed. However, if this position is from a different game than
|
||||
the last position sent to the engine, the GUI should have sent a "ucinewgame" inbetween.
|
||||
|
||||
* go
|
||||
start calculating on the current position set up with the "position" command.
|
||||
There are a number of commands that can follow this command, all will be sent in the same string.
|
||||
If one command is not sent its value should be interpreted as it would not influence the search.
|
||||
* searchmoves <move1> .... <movei>
|
||||
restrict search to this moves only
|
||||
Example: After "position startpos" and "go infinite searchmoves e2e4 d2d4"
|
||||
the engine should only search the two moves e2e4 and d2d4 in the initial position.
|
||||
* ponder
|
||||
start searching in pondering mode.
|
||||
Do not exit the search in ponder mode, even if it's mate!
|
||||
This means that the last move sent in in the position string is the ponder move.
|
||||
The engine can do what it wants to do, but after a "ponderhit" command
|
||||
it should execute the suggested move to ponder on. This means that the ponder move sent by
|
||||
the GUI can be interpreted as a recommendation about which move to ponder. However, if the
|
||||
engine decides to ponder on a different move, it should not display any mainlines as they are
|
||||
likely to be misinterpreted by the GUI because the GUI expects the engine to ponder
|
||||
on the suggested move.
|
||||
* wtime <x>
|
||||
white has x msec left on the clock
|
||||
* btime <x>
|
||||
black has x msec left on the clock
|
||||
* winc <x>
|
||||
white increment per move in mseconds if x > 0
|
||||
* binc <x>
|
||||
black increment per move in mseconds if x > 0
|
||||
* movestogo <x>
|
||||
there are x moves to the next time control,
|
||||
this will only be sent if x > 0,
|
||||
if you don't get this and get the wtime and btime it's sudden death
|
||||
* depth <x>
|
||||
search x plies only.
|
||||
* nodes <x>
|
||||
search x nodes only,
|
||||
* mate <x>
|
||||
search for a mate in x moves
|
||||
* movetime <x>
|
||||
search exactly x mseconds
|
||||
* infinite
|
||||
search until the "stop" command. Do not exit the search without being told so in this mode!
|
||||
|
||||
* stop
|
||||
stop calculating as soon as possible,
|
||||
don't forget the "bestmove" and possibly the "ponder" token when finishing the search
|
||||
|
||||
* ponderhit
|
||||
the user has played the expected move. This will be sent if the engine was told to ponder on the same move
|
||||
the user has played. The engine should continue searching but switch from pondering to normal search.
|
||||
|
||||
* quit
|
||||
quit the program as soon as possible
|
||||
|
||||
|
||||
Engine to GUI:
|
||||
--------------
|
||||
|
||||
* id
|
||||
* name <x>
|
||||
this must be sent after receiving the "uci" command to identify the engine,
|
||||
e.g. "id name Shredder X.Y\n"
|
||||
* author <x>
|
||||
this must be sent after receiving the "uci" command to identify the engine,
|
||||
e.g. "id author Stefan MK\n"
|
||||
|
||||
* uciok
|
||||
Must be sent after the id and optional options to tell the GUI that the engine
|
||||
has sent all infos and is ready in uci mode.
|
||||
|
||||
* readyok
|
||||
This must be sent when the engine has received an "isready" command and has
|
||||
processed all input and is ready to accept new commands now.
|
||||
It is usually sent after a command that can take some time to be able to wait for the engine,
|
||||
but it can be used anytime, even when the engine is searching,
|
||||
and must always be answered with "isready".
|
||||
|
||||
* bestmove <move1> [ ponder <move2> ]
|
||||
the engine has stopped searching and found the move <move> best in this position.
|
||||
the engine can send the move it likes to ponder on. The engine must not start pondering automatically.
|
||||
this command must always be sent if the engine stops searching, also in pondering mode if there is a
|
||||
"stop" command, so for every "go" command a "bestmove" command is needed!
|
||||
Directly before that the engine should send a final "info" command with the final search information,
|
||||
the the GUI has the complete statistics about the last search.
|
||||
|
||||
* copyprotection
|
||||
this is needed for copyprotected engines. After the uciok command the engine can tell the GUI,
|
||||
that it will check the copy protection now. This is done by "copyprotection checking".
|
||||
If the check is ok the engine should send "copyprotection ok", otherwise "copyprotection error".
|
||||
If there is an error the engine should not function properly but should not quit alone.
|
||||
If the engine reports "copyprotection error" the GUI should not use this engine
|
||||
and display an error message instead!
|
||||
The code in the engine can look like this
|
||||
TellGUI("copyprotection checking\n");
|
||||
// ... check the copy protection here ...
|
||||
if(ok)
|
||||
TellGUI("copyprotection ok\n");
|
||||
else
|
||||
TellGUI("copyprotection error\n");
|
||||
|
||||
* registration
|
||||
this is needed for engines that need a username and/or a code to function with all features.
|
||||
Analog to the "copyprotection" command the engine can send "registration checking"
|
||||
after the uciok command followed by either "registration ok" or "registration error".
|
||||
Also after every attempt to register the engine it should answer with "registration checking"
|
||||
and then either "registration ok" or "registration error".
|
||||
In contrast to the "copyprotection" command, the GUI can use the engine after the engine has
|
||||
reported an error, but should inform the user that the engine is not properly registered
|
||||
and might not use all its features.
|
||||
In addition the GUI should offer to open a dialog to
|
||||
enable registration of the engine. To try to register an engine the GUI can send
|
||||
the "register" command.
|
||||
The GUI has to always answer with the "register" command if the engine sends "registration error"
|
||||
at engine startup (this can also be done with "register later")
|
||||
and tell the user somehow that the engine is not registered.
|
||||
This way the engine knows that the GUI can deal with the registration procedure and the user
|
||||
will be informed that the engine is not properly registered.
|
||||
|
||||
* info
|
||||
the engine wants to send information to the GUI. This should be done whenever one of the info has changed.
|
||||
The engine can send only selected infos or multiple infos with one info command,
|
||||
e.g. "info currmove e2e4 currmovenumber 1" or
|
||||
"info depth 12 nodes 123456 nps 100000".
|
||||
Also all infos belonging to the pv should be sent together
|
||||
e.g. "info depth 2 score cp 214 time 1242 nodes 2124 nps 34928 pv e2e4 e7e5 g1f3"
|
||||
I suggest to start sending "currmove", "currmovenumber", "currline" and "refutation" only after one second
|
||||
to avoid too much traffic.
|
||||
Additional info:
|
||||
* depth <x>
|
||||
search depth in plies
|
||||
* seldepth <x>
|
||||
selective search depth in plies,
|
||||
if the engine sends seldepth there must also be a "depth" present in the same string.
|
||||
* time <x>
|
||||
the time searched in ms, this should be sent together with the pv.
|
||||
* nodes <x>
|
||||
x nodes searched, the engine should send this info regularly
|
||||
* pv <move1> ... <movei>
|
||||
the best line found
|
||||
* multipv <num>
|
||||
this for the multi pv mode.
|
||||
for the best move/pv add "multipv 1" in the string when you send the pv.
|
||||
in k-best mode always send all k variants in k strings together.
|
||||
* score
|
||||
* cp <x>
|
||||
the score from the engine's point of view in centipawns.
|
||||
* mate <y>
|
||||
mate in y moves, not plies.
|
||||
If the engine is getting mated use negative values for y.
|
||||
* lowerbound
|
||||
the score is just a lower bound.
|
||||
* upperbound
|
||||
the score is just an upper bound.
|
||||
* currmove <move>
|
||||
currently searching this move
|
||||
* currmovenumber <x>
|
||||
currently searching move number x, for the first move x should be 1 not 0.
|
||||
* hashfull <x>
|
||||
the hash is x permill full, the engine should send this info regularly
|
||||
* nps <x>
|
||||
x nodes per second searched, the engine should send this info regularly
|
||||
* tbhits <x>
|
||||
x positions where found in the endgame table bases
|
||||
* sbhits <x>
|
||||
x positions where found in the shredder endgame databases
|
||||
* cpuload <x>
|
||||
the cpu usage of the engine is x permill.
|
||||
* string <str>
|
||||
any string str which will be displayed be the engine,
|
||||
if there is a string command the rest of the line will be interpreted as <str>.
|
||||
* refutation <move1> <move2> ... <movei>
|
||||
move <move1> is refuted by the line <move2> ... <movei>, i can be any number >= 1.
|
||||
Example: after move d1h5 is searched, the engine can send
|
||||
"info refutation d1h5 g6h5"
|
||||
if g6h5 is the best answer after d1h5 or if g6h5 refutes the move d1h5.
|
||||
if there is no refutation for d1h5 found, the engine should just send
|
||||
"info refutation d1h5"
|
||||
The engine should only send this if the option "UCI_ShowRefutations" is set to true.
|
||||
* currline <cpunr> <move1> ... <movei>
|
||||
this is the current line the engine is calculating. <cpunr> is the number of the cpu if
|
||||
the engine is running on more than one cpu. <cpunr> = 1,2,3....
|
||||
if the engine is just using one cpu, <cpunr> can be omitted.
|
||||
If <cpunr> is greater than 1, always send all k lines in k strings together.
|
||||
The engine should only send this if the option "UCI_ShowCurrLine" is set to true.
|
||||
|
||||
|
||||
* option
|
||||
This command tells the GUI which parameters can be changed in the engine.
|
||||
This should be sent once at engine startup after the "uci" and the "id" commands
|
||||
if any parameter can be changed in the engine.
|
||||
The GUI should parse this and build a dialog for the user to change the settings.
|
||||
Note that not every option needs to appear in this dialog as some options like
|
||||
"Ponder", "UCI_AnalyseMode", etc. are better handled elsewhere or are set automatically.
|
||||
If the user wants to change some settings, the GUI will send a "setoption" command to the engine.
|
||||
Note that the GUI need not send the setoption command when starting the engine for every option if
|
||||
it doesn't want to change the default value.
|
||||
For all allowed combinations see the examples below,
|
||||
as some combinations of this tokens don't make sense.
|
||||
One string will be sent for each parameter.
|
||||
* name <id>
|
||||
The option has the name id.
|
||||
Certain options have a fixed value for <id>, which means that the semantics of this option is fixed.
|
||||
Usually those options should not be displayed in the normal engine options window of the GUI but
|
||||
get a special treatment. "Pondering" for example should be set automatically when pondering is
|
||||
enabled or disabled in the GUI options. The same for "UCI_AnalyseMode" which should also be set
|
||||
automatically by the GUI. All those certain options have the prefix "UCI_" except for the
|
||||
first 6 options below. If the GUI gets an unknown Option with the prefix "UCI_", it should just
|
||||
ignore it and not display it in the engine's options dialog.
|
||||
* <id> = Hash, type is spin
|
||||
the value in MB for memory for hash tables can be changed,
|
||||
this should be answered with the first "setoptions" command at program boot
|
||||
if the engine has sent the appropriate "option name Hash" command,
|
||||
which should be supported by all engines!
|
||||
So the engine should use a very small hash first as default.
|
||||
* <id> = NalimovPath, type string
|
||||
this is the path on the hard disk to the Nalimov compressed format.
|
||||
Multiple directories can be concatenated with ";"
|
||||
* <id> = NalimovCache, type spin
|
||||
this is the size in MB for the cache for the nalimov table bases
|
||||
These last two options should also be present in the initial options exchange dialog
|
||||
when the engine is booted if the engine supports it
|
||||
* <id> = Ponder, type check
|
||||
this means that the engine is able to ponder.
|
||||
The GUI will send this whenever pondering is possible or not.
|
||||
Note: The engine should not start pondering on its own if this is enabled, this option is only
|
||||
needed because the engine might change its time management algorithm when pondering is allowed.
|
||||
* <id> = OwnBook, type check
|
||||
this means that the engine has its own book which is accessed by the engine itself.
|
||||
if this is set, the engine takes care of the opening book and the GUI will never
|
||||
execute a move out of its book for the engine. If this is set to false by the GUI,
|
||||
the engine should not access its own book.
|
||||
* <id> = MultiPV, type spin
|
||||
the engine supports multi best line or k-best mode. the default value is 1
|
||||
* <id> = UCI_ShowCurrLine, type check, should be false by default,
|
||||
the engine can show the current line it is calculating. see "info currline" above.
|
||||
* <id> = UCI_ShowRefutations, type check, should be false by default,
|
||||
the engine can show a move and its refutation in a line. see "info refutations" above.
|
||||
* <id> = UCI_LimitStrength, type check, should be false by default,
|
||||
The engine is able to limit its strength to a specific Elo number,
|
||||
This should always be implemented together with "UCI_Elo".
|
||||
* <id> = UCI_Elo, type spin
|
||||
The engine can limit its strength in Elo within this interval.
|
||||
If UCI_LimitStrength is set to false, this value should be ignored.
|
||||
If UCI_LimitStrength is set to true, the engine should play with this specific strength.
|
||||
This should always be implemented together with "UCI_LimitStrength".
|
||||
* <id> = UCI_AnalyseMode, type check
|
||||
The engine wants to behave differently when analysing or playing a game.
|
||||
For example when playing it can use some kind of learning.
|
||||
This is set to false if the engine is playing a game, otherwise it is true.
|
||||
* <id> = UCI_Opponent, type string
|
||||
With this command the GUI can send the name, title, elo and if the engine is playing a human
|
||||
or computer to the engine.
|
||||
The format of the string has to be [GM|IM|FM|WGM|WIM|none] [<elo>|none] [computer|human] <name>
|
||||
Examples:
|
||||
"setoption name UCI_Opponent value GM 2800 human Gary Kasparov"
|
||||
"setoption name UCI_Opponent value none none computer Shredder"
|
||||
* <id> = UCI_EngineAbout, type string
|
||||
With this command, the engine tells the GUI information about itself, for example a license text,
|
||||
usually it doesn't make sense that the GUI changes this text with the setoption command.
|
||||
Example:
|
||||
"option name UCI_EngineAbout type string default Shredder by Stefan Meyer-Kahlen, see www.shredderchess.com"
|
||||
* <id> = UCI_ShredderbasesPath, type string
|
||||
this is either the path to the folder on the hard disk containing the Shredder endgame databases or
|
||||
the path and filename of one Shredder endgame datbase.
|
||||
* <id> = UCI_SetPositionValue, type string
|
||||
the GUI can send this to the engine to tell the engine to use a certain value in centipawns from white's
|
||||
point of view if evaluating this specifix position.
|
||||
The string can have the formats:
|
||||
<value> + <fen> | clear + <fen> | clearall
|
||||
|
||||
* type <t>
|
||||
The option has type t.
|
||||
There are 5 different types of options the engine can send
|
||||
* check
|
||||
a checkbox that can either be true or false
|
||||
* spin
|
||||
a spin wheel that can be an integer in a certain range
|
||||
* combo
|
||||
a combo box that can have different predefined strings as a value
|
||||
* button
|
||||
a button that can be pressed to send a command to the engine
|
||||
* string
|
||||
a text field that has a string as a value,
|
||||
an empty string has the value "<empty>"
|
||||
* default <x>
|
||||
the default value of this parameter is x
|
||||
* min <x>
|
||||
the minimum value of this parameter is x
|
||||
* max <x>
|
||||
the maximum value of this parameter is x
|
||||
* var <x>
|
||||
a predefined value of this parameter is x
|
||||
Examples:
|
||||
Here are 5 strings for each of the 5 possible types of options
|
||||
"option name Nullmove type check default true\n"
|
||||
"option name Selectivity type spin default 2 min 0 max 4\n"
|
||||
"option name Style type combo default Normal var Solid var Normal var Risky\n"
|
||||
"option name NalimovPath type string default c:\\n"
|
||||
"option name Clear Hash type button\n"
|
||||
|
||||
|
||||
|
||||
Examples:
|
||||
---------
|
||||
|
||||
This is how the communication when the engine boots can look like:
|
||||
|
||||
GUI engine
|
||||
|
||||
// tell the engine to switch to UCI mode
|
||||
uci
|
||||
|
||||
// engine identify
|
||||
id name Shredder
|
||||
id author Stefan MK
|
||||
|
||||
// engine sends the options it can change
|
||||
// the engine can change the hash size from 1 to 128 MB
|
||||
option name Hash type spin default 1 min 1 max 128
|
||||
|
||||
// the engine supports Nalimov endgame tablebases
|
||||
option name NalimovPath type string default <empty>
|
||||
option name NalimovCache type spin default 1 min 1 max 32
|
||||
|
||||
// the engine can switch off Nullmove and set the playing style
|
||||
option name Nullmove type check default true
|
||||
option name Style type combo default Normal var Solid var Normal var Risky
|
||||
|
||||
// the engine has sent all parameters and is ready
|
||||
uciok
|
||||
|
||||
// Note: here the GUI can already send a "quit" command if it just wants to find out
|
||||
// details about the engine, so the engine should not initialize its internal
|
||||
// parameters before here.
|
||||
// now the GUI sets some values in the engine
|
||||
// set hash to 32 MB
|
||||
setoption name Hash value 32
|
||||
|
||||
// init tbs
|
||||
setoption name NalimovCache value 1
|
||||
setoption name NalimovPath value d:\tb;c\tb
|
||||
|
||||
// waiting for the engine to finish initializing
|
||||
// this command and the answer is required here!
|
||||
isready
|
||||
|
||||
// engine has finished setting up the internal values
|
||||
readyok
|
||||
|
||||
// now we are ready to go
|
||||
|
||||
// if the GUI is supporting it, tell the engine that is is
|
||||
// searching on a game that it hasn't searched on before
|
||||
ucinewgame
|
||||
|
||||
// if the engine supports the "UCI_AnalyseMode" option and the next search is supposed to
|
||||
// be an analysis, the GUI should set "UCI_AnalyseMode" to true if it is currently
|
||||
// set to false with this engine
|
||||
setoption name UCI_AnalyseMode value true
|
||||
|
||||
// tell the engine to search infinite from the start position after 1.e4 e5
|
||||
position startpos moves e2e4 e7e5
|
||||
go infinite
|
||||
|
||||
// the engine starts sending infos about the search to the GUI
|
||||
// (only some examples are given)
|
||||
|
||||
|
||||
info depth 1 seldepth 0
|
||||
info score cp 13 depth 1 nodes 13 time 15 pv f1b5
|
||||
info depth 2 seldepth 2
|
||||
info nps 15937
|
||||
info score cp 14 depth 2 nodes 255 time 15 pv f1c4 f8c5
|
||||
info depth 2 seldepth 7 nodes 255
|
||||
info depth 3 seldepth 7
|
||||
info nps 26437
|
||||
info score cp 20 depth 3 nodes 423 time 15 pv f1c4 g8f6 b1c3
|
||||
info nps 41562
|
||||
....
|
||||
|
||||
|
||||
// here the user has seen enough and asks to stop the searching
|
||||
stop
|
||||
|
||||
// the engine has finished searching and is sending the bestmove command
|
||||
// which is needed for every "go" command sent to tell the GUI
|
||||
// that the engine is ready again
|
||||
bestmove g1f3 ponder d8f6
|
||||
|
||||
|
||||
|
||||
Chess960
|
||||
========
|
||||
|
||||
UCI could easily be extended to support Chess960 (also known as Fischer Random Chess).
|
||||
|
||||
The engine has to tell the GUI that it is capable of playing Chess960 and the GUI has to tell
|
||||
the engine that is should play according to the Chess960 rules.
|
||||
This is done by the special engine option UCI_Chess960. If the engine knows about Chess960
|
||||
it should send the command 'option name UCI_Chess960 type check default false'
|
||||
to the GUI at program startup.
|
||||
Whenever a Chess960 game is played, the GUI should set this engine option to 'true'.
|
||||
|
||||
Castling is different in Chess960 and the white king move when castling short is not always e1g1.
|
||||
A king move could both be the castling king move or just a normal king move.
|
||||
This is why castling moves are sent in the form king "takes" his own rook.
|
||||
Example: e1h1 for the white short castle move in the normal chess start position.
|
||||
|
||||
In EPD and FEN position strings specifying the castle rights with w and q is not enough as
|
||||
there could be more than one rook on the right or left side of the king.
|
||||
This is why the castle rights are specified with the letter of the castle rook's line.
|
||||
Upper case letters for white's and lower case letters for black's castling rights.
|
||||
Example: The normal chess position would be:
|
||||
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w AHah -
|
||||
33
scripts/env.sh
Executable file
33
scripts/env.sh
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# env.sh - set environment for brchess developer.
|
||||
#
|
||||
# Copyright (C) 2023 Bruno Raoult ("br")
|
||||
# Licensed under the GNU General Public License v3.0 or later.
|
||||
# Some rights reserved. See COPYING.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with this
|
||||
# program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
#
|
||||
# USAGE: source env.sh [arg]
|
||||
#
|
||||
# This file will actually be sourced if it was never sourced in current bash
|
||||
# environment.
|
||||
|
||||
if [[ ! -v _BRCHESS_ENV_ ]]; then
|
||||
export _BRCHESS_ENV_=1 BRCHESS_ROOT LD_LIBRARY_PATH
|
||||
BRCHESS_ROOT=$(realpath -L "$(dirname "${BASH_SOURCE[0]}")/..")
|
||||
|
||||
BRCHESS_SCRIPTDIR="$BRCHESS_ROOT/scripts"
|
||||
BRCHESS_BINDIR="$BRCHESS_ROOT/bin"
|
||||
PATH="$PATH:$BRCHESS_BINDIR:$BRCHESS_SCRIPTDIR"
|
||||
|
||||
BRCHESS_BRLIBDIR="$BRCHESS_ROOT/brlib"
|
||||
BRCHESS_LIBDIR="$BRCHESS_BRLIBDIR/lib"
|
||||
LD_LIBRARY_PATH="${BRCHESS_LIBDIR}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
|
||||
#printf "R=%s L=%s LD=%s\n" "$BRCHESS_ROOT" "$BRLIB_DIR" "$LD_LIBRARY_PATH"
|
||||
unset BRCHESS_SCRIPTDIR BRCHESS_BINDIR BRCHESS_LIBDIR
|
||||
printf "brchess environment complete.\n"
|
||||
fi
|
||||
47
scripts/fetch-all.sh
Executable file
47
scripts/fetch-all.sh
Executable file
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Fetch all branches from remotes, and track missing ones.
|
||||
#
|
||||
# The value of variable ORIGIN is used for repository origin. If
|
||||
# not set, the default is "origin".
|
||||
|
||||
default_origin=origin
|
||||
|
||||
origin=${ORIGIN:-$default_origin}
|
||||
|
||||
declare -a local_b
|
||||
declare -A alocal_b
|
||||
|
||||
# fetch all remotes
|
||||
git fetch --all --tags
|
||||
|
||||
# get local branches, and build reverse associative array
|
||||
readarray -t local_b < <(git for-each-ref --format='%(refname:short)' refs/heads/)
|
||||
for ref in "${local_b[@]}"; do
|
||||
alocal_b[$ref]=1
|
||||
done
|
||||
|
||||
# get "origin" branches
|
||||
readarray -t orig_b < <(git for-each-ref --format='%(refname:short)' \
|
||||
refs/remotes/"$origin"/)
|
||||
|
||||
# find-out missing local branches and track them.
|
||||
# bugs:
|
||||
# - We only check local branch existence, not tracking information correctness.
|
||||
# - What about sub-branches ? Like remote/a and remote/a/b not being tracked ?
|
||||
#for i in `git branch -a | grep remote | grep -v HEAD | grep -v master`; do
|
||||
# git branch --track ${i#remotes/origin/} $i
|
||||
#done
|
||||
for remote_b in "${orig_b[@]}"; do
|
||||
short=${remote_b#"$origin"/};
|
||||
# OR (??): short=${remote_b##*/}
|
||||
|
||||
if ! [[ -v alocal_b[$short] ]]; then
|
||||
printf "local branch %s set to track %s.\n" "$short" "$remote_b"
|
||||
git branch --track "$short" "$remote_b"
|
||||
else
|
||||
printf "skipping %s.\n" "$remote_b"
|
||||
fi
|
||||
done
|
||||
|
||||
git pull -a
|
||||
102
scripts/git-split.sh
Executable file
102
scripts/git-split.sh
Executable file
@@ -0,0 +1,102 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# git-split.sh - duplicate GIT file, with history (well, sort of)
|
||||
# Sources:
|
||||
# https://stackoverflow.com/a/53849613/3079831
|
||||
# https://stackoverflow.com/a/75942970/3079831
|
||||
#
|
||||
# (C) Bruno Raoult ("br"), 2024
|
||||
# Licensed under the GNU General Public License v3.0 or later.
|
||||
# Some rights reserved. See COPYING.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with this
|
||||
# program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
|
||||
CMDNAME=${0##*/} # script name
|
||||
|
||||
if (( $# == 0 || $# % 2 )); then
|
||||
printf "Usage: %s orig1 copy [orig2 copy2 ...]\n" "$CMDNAME"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
yes_or_no() {
|
||||
while true; do
|
||||
read -rp "$* [y/N]: " yn
|
||||
case $yn in
|
||||
[Yy])
|
||||
return 0
|
||||
;;
|
||||
[Nn]|"")
|
||||
printf "Aborted.\n"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
# not reached
|
||||
}
|
||||
|
||||
# prints out and run a command.
|
||||
echorun () {
|
||||
printf ">>> %s\n" "$*"
|
||||
"$@"
|
||||
return $?
|
||||
}
|
||||
|
||||
declare -A files
|
||||
declare -a from # to keep order
|
||||
|
||||
while (($#)); do
|
||||
if [[ ! -f "$1" ]]; then
|
||||
printf "Missing '%s'. Aborting.\n" "$1"
|
||||
exit 1
|
||||
elif [[ -f "$2" ]]; then
|
||||
printf "'%s' already exists. Aborting.\n" "$2"
|
||||
exit 1
|
||||
fi
|
||||
files["$1"]="$2"
|
||||
from+=("$1")
|
||||
shift 2
|
||||
done
|
||||
|
||||
printf "The following files will be split :\n"
|
||||
|
||||
for file in "${from[@]}"; do
|
||||
printf "%s -> %s\n" "$file" "${files[$file]}"
|
||||
done
|
||||
|
||||
yes_or_no "Proceed (y/N)? " || exit 1
|
||||
|
||||
branch="split-file"
|
||||
|
||||
printf -v msg1 "Splitting '%s'" "${from[0]}"
|
||||
printf -v msg2 "Restore duplicated %s" "${from[0]}"
|
||||
if (( ${#from[@]} > 1 )); then
|
||||
msg1+=" and $(( ${#from[@]} - 1 )) others"
|
||||
msg2+=" and $(( ${#from[@]} - 1 )) others"
|
||||
|
||||
fi
|
||||
printf "%s using branch '$branch'.\n" "$msg1"
|
||||
|
||||
if git show-ref --verify --quiet "refs/heads/$branch"; then
|
||||
echorun git switch "$branch"
|
||||
echorun git merge master
|
||||
else
|
||||
echorun git switch -c "$branch"
|
||||
fi
|
||||
|
||||
for file in "${from[@]}"; do # make the duplicates
|
||||
echorun git mv "$file" "${files[$file]}"
|
||||
done
|
||||
echorun git commit -m "$msg1" # ... and commit
|
||||
|
||||
for file in "${from[@]}"; do # bring back the original
|
||||
echorun git checkout HEAD~ "$file"
|
||||
done
|
||||
echorun git commit -m "$msg2" # ... and commit
|
||||
|
||||
echorun git checkout - # switch back to source branch
|
||||
echorun git merge --no-ff "$branch" -m "Merge $branch" # merge dup into source branch
|
||||
|
||||
exit 0
|
||||
3
scripts/submodules-upd.sh
Executable file
3
scripts/submodules-upd.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
git submodule update --remote --merge
|
||||
174
src/alloc.c
Normal file
174
src/alloc.c
Normal file
@@ -0,0 +1,174 @@
|
||||
/* alloc.c - various memory allocation helpers
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include <brlib.h>
|
||||
#include <bitops.h>
|
||||
#include <bug.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "alloc.h"
|
||||
|
||||
/* values for linux
|
||||
* PAGE_SIZE can be obtained with getpagesize(2), but unfortunately nothing for
|
||||
* HUGE_PAGE_SIZE (except looking in /proc/meminfo or such).
|
||||
*/
|
||||
#define PAGE_SIZE (4 * 1024) /* 4 Kb */
|
||||
#define HUGE_PAGE_SIZE (2 * 1024 * 1024) /* 2 Mb */
|
||||
|
||||
/**
|
||||
* alloc() - allocate memory.
|
||||
* @size: size to allocate
|
||||
*
|
||||
* Allocate memory on the heap.
|
||||
*
|
||||
* @return: memory address if success, NULL otherwise.
|
||||
*/
|
||||
void *alloc(size_t size)
|
||||
{
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* alloc_aligned() - allocate aligned memory.
|
||||
* @align: alignment, in bytes
|
||||
* @size: size to allocate
|
||||
*
|
||||
* Allocate aligned memory on the heap. @align must be a power of 2 and
|
||||
* a multiple of sizeof(void *). See aligned_alloc(3) for details.
|
||||
*
|
||||
* @return: memory address if success, NULL otherwise.
|
||||
*/
|
||||
void *alloc_aligned(size_t align, size_t size)
|
||||
{
|
||||
bug_on(!size || !align ||
|
||||
align & (align - 1) || align % sizeof (void *));
|
||||
return aligned_alloc(align, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* alloc_page_aligned() - allocate page-aligned memory.
|
||||
* @size: size to allocate
|
||||
*
|
||||
* Allocate page-aligned memory on the heap.
|
||||
*
|
||||
* @return: memory address if success, NULL otherwise.
|
||||
*/
|
||||
void *alloc_aligned_page(size_t size)
|
||||
{
|
||||
/* round size (up) to alignment */
|
||||
//size_t rounded = (size + PAGE_SIZE - 1) & -PAGE_SIZE;
|
||||
void *mem = alloc_aligned(PAGE_SIZE, size);
|
||||
return mem;
|
||||
}
|
||||
|
||||
/**
|
||||
* alloc_huge_page_aligned() - allocate huge-page-aligned memory.
|
||||
* @size: size to allocate
|
||||
*
|
||||
* Allocate page-aligned memory on the heap.
|
||||
*
|
||||
* @return: memory address if success, NULL otherwise.
|
||||
*/
|
||||
void *alloc_aligned_hugepage(size_t size)
|
||||
{
|
||||
/* round size (up) to alignment */
|
||||
size_t rounded = (size + PAGE_SIZE - 1) & -PAGE_SIZE;
|
||||
void *mem = alloc_aligned(PAGE_SIZE, rounded);
|
||||
//printf("size=%zu rounded=%zu\n", size, rounded);
|
||||
//void *mem = aligned_alloc(HUGE_PAGE_SIZE, size);
|
||||
if (mem) {
|
||||
if (madvise(mem, rounded, MADV_HUGEPAGE | MADV_RANDOM))
|
||||
perror("madvise");
|
||||
}
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
/**
|
||||
* safe_alloc() - allocate memory or fail.
|
||||
* @size: size to allocate
|
||||
*
|
||||
* Allocate memory on the heap. This function does not return if allocation
|
||||
* fails.
|
||||
*
|
||||
* @return: memory address (if success only).
|
||||
*/
|
||||
void *safe_alloc(size_t size)
|
||||
{
|
||||
void *mem = malloc(size);
|
||||
bug_on_always(mem == NULL);
|
||||
return mem;
|
||||
}
|
||||
|
||||
/**
|
||||
* safe_alloc_aligned() - allocate aligned memory or fail.
|
||||
* @align: alignment.
|
||||
* @size: size to allocate
|
||||
*
|
||||
* Allocate aligned memory on the heap.
|
||||
* This function does not return if allocation fails. See alloc_aligned()
|
||||
* for more details.
|
||||
*
|
||||
* @return: memory address (if success only).
|
||||
*/
|
||||
void *safe_alloc_aligned(size_t align, size_t size)
|
||||
{
|
||||
void *mem = alloc_aligned(align, size);
|
||||
bug_on_always(mem == NULL);
|
||||
return mem;
|
||||
}
|
||||
|
||||
/**
|
||||
* safe_alloc_aligned_page() - allocate page-aligned memory or fail.
|
||||
* @size: size to allocate
|
||||
*
|
||||
* Allocate memory on the heap. This function does not return if allocation
|
||||
* fails. See alloc_aligned() for more details.
|
||||
*
|
||||
* @return: memory address (if success only).
|
||||
*/
|
||||
void *safe_alloc_aligned_page(size_t size)
|
||||
{
|
||||
void *mem = alloc_aligned_page(size);
|
||||
bug_on_always(mem == NULL);
|
||||
return mem;
|
||||
}
|
||||
|
||||
/**
|
||||
* safe_alloc_aligned_hugepage() - allocate huge page aligned memory or fail.
|
||||
* @size: size to allocate
|
||||
*
|
||||
* Allocate memory on the heap. This function does not return if allocation
|
||||
* fails. See alloc_aligned() for more details.
|
||||
*
|
||||
* @return: memory address (if success only).
|
||||
*/
|
||||
void *safe_alloc_aligned_hugepage(size_t size)
|
||||
{
|
||||
/* round size (up) to alignment */
|
||||
//size_t rounded = (size + HUGE_PAGE_SIZE - 1) & -HUGE_PAGE_SIZE;
|
||||
//void *mem = aligned_alloc(rounded, HUGE_PAGE_SIZE);
|
||||
void *mem = alloc_aligned_hugepage(size);
|
||||
bug_on_always(mem == NULL);
|
||||
return mem;
|
||||
}
|
||||
|
||||
void safe_free(void *ptr)
|
||||
{
|
||||
bug_on_always(ptr == NULL);
|
||||
free(ptr);
|
||||
}
|
||||
30
src/alloc.h
Normal file
30
src/alloc.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/* alloc.h - various memory allocation helpers
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _ALLOC_H
|
||||
#define _ALLOC_H
|
||||
|
||||
#include "chessdefs.h"
|
||||
|
||||
void *alloc(size_t size);
|
||||
void *alloc_aligned(size_t align, size_t size);
|
||||
void *alloc_aligned_page(size_t size);
|
||||
void *alloc_aligned_hugepage(size_t size);
|
||||
|
||||
void *safe_alloc(size_t size);
|
||||
void *safe_alloc_aligned(size_t align, size_t size);
|
||||
void *safe_alloc_aligned_page(size_t size);
|
||||
void *safe_alloc_aligned_hugepage(size_t size);
|
||||
void safe_free(void *ptr);
|
||||
|
||||
#endif /* _ALLOC_H */
|
||||
216
src/attack.c
Normal file
216
src/attack.c
Normal file
@@ -0,0 +1,216 @@
|
||||
/* attack.c - attack functions.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "bitboard.h"
|
||||
#include "position.h"
|
||||
#include "hq.h"
|
||||
#include "attack.h"
|
||||
|
||||
|
||||
/**
|
||||
* sq_is_attacked() - find if a square is attacked
|
||||
* @pos: position
|
||||
* @occ: occupation mask used
|
||||
* @sq: square to test
|
||||
* @c: attacker color
|
||||
*
|
||||
* Find if a @c piece attacks @sq.
|
||||
*
|
||||
* Algorithm: We perform a reverse attack, and check if any given
|
||||
* piece type on @sq can attack a @c piece of same type.
|
||||
*
|
||||
* For example, if @c is white, we test for all T in P,N,B,R,Q,K if
|
||||
* T on @sq could attack a white T piece.
|
||||
*
|
||||
* @Return: true if @sq is attacked by a @c piece, false otherwise.
|
||||
*/
|
||||
bool sq_is_attacked(const pos_t *pos, const bitboard_t occ, const square_t sq, const color_t c)
|
||||
{
|
||||
color_t opp = OPPONENT(c);
|
||||
|
||||
/*
|
||||
* return (hyperbola_bishop_moves(occ, sq) & (pos->bb[c][BISHOP] | pos->bb[c][QUEEN])
|
||||
* || hyperbola_rook_moves(occ, sq) & (pos->bb[c][ROOK] | pos->bb[c][QUEEN])
|
||||
* || bb_pawn_attacks[opp][sq] & pos->bb[c][PAWN]
|
||||
* || bb_knight_moves(pos->bb[c][KNIGHT], sq)
|
||||
* || bb_king_moves(pos->bb[c][KING], sq)
|
||||
* )
|
||||
*/
|
||||
|
||||
/* bishop / queen */
|
||||
if (hq_bishop_moves(occ, sq) & (pos->bb[c][BISHOP] | pos->bb[c][QUEEN]))
|
||||
return true;
|
||||
|
||||
/* rook / queen */
|
||||
if (hq_rook_moves(occ, sq) & (pos->bb[c][ROOK] | pos->bb[c][QUEEN]))
|
||||
return true;
|
||||
|
||||
/* knight */
|
||||
if (bb_knight_moves(pos->bb[c][KNIGHT], sq))
|
||||
return true;
|
||||
|
||||
/* pawn */
|
||||
if (bb_pawn_attacks[opp][sq] & pos->bb[c][PAWN])
|
||||
return true;
|
||||
|
||||
/* king */
|
||||
if (bb_king_moves(pos->bb[c][KING], sq))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* is_in_check() - find if a king is in check.
|
||||
* @pos: position
|
||||
* @color: king color
|
||||
*
|
||||
* Find if a @c king is in check. This function is a wrapper over @sq_is_attacked().
|
||||
*
|
||||
* @Return: true if @sq is attacked by a @c piece, false otherwise.
|
||||
*/
|
||||
bool is_in_check(const pos_t *pos, const color_t color)
|
||||
{
|
||||
bitboard_t occ = pos_occ(pos);
|
||||
return sq_is_attacked(pos, occ, pos->king[color], OPPONENT(color));
|
||||
}
|
||||
|
||||
/**
|
||||
* sq_attackers() - find attackers on a square
|
||||
* @pos: position
|
||||
* @occ: occupation mask used
|
||||
* @sq: square to test
|
||||
* @c: attacker color
|
||||
*
|
||||
* Find all @c attacks on @sq. En-passant is not considered.
|
||||
*
|
||||
* Algorithm: We perform a reverse attack, and check if any given
|
||||
* piece type on @sq can attack a @c piece of same type.
|
||||
*
|
||||
* For example, if @c is white, we test for all T in P,N,B,R,Q,K if
|
||||
* T on @sq could attack a white T piece.
|
||||
*
|
||||
* @Return: a bitboard of attackers.
|
||||
*/
|
||||
bitboard_t sq_attackers(const pos_t *pos, const bitboard_t occ, const square_t sq, const color_t c)
|
||||
{
|
||||
bitboard_t attackers = 0, tmp;
|
||||
bitboard_t sqbb = BIT(sq);
|
||||
bitboard_t to;
|
||||
color_t opp = OPPONENT(c);
|
||||
|
||||
/* pawn */
|
||||
to = pos->bb[c][PAWN];
|
||||
tmp = bb_pawns_attacks(sqbb, sq_up(opp)) & to;
|
||||
attackers |= tmp;
|
||||
# ifdef DEBUG_ATTACK_ATTACKERS
|
||||
bb_print("att pawn", tmp);
|
||||
# endif
|
||||
|
||||
/* knight & king */
|
||||
to = pos->bb[c][KNIGHT];
|
||||
tmp = bb_knight_moves(to, sq);
|
||||
attackers |= tmp;
|
||||
# ifdef DEBUG_ATTACK_ATTACKERS
|
||||
bb_print("att knight", tmp);
|
||||
# endif
|
||||
to = pos->bb[c][KING];
|
||||
tmp = bb_king_moves(to, sq);
|
||||
attackers |= tmp;
|
||||
# ifdef DEBUG_ATTACK_ATTACKERS
|
||||
bb_print("att king", tmp);
|
||||
# endif
|
||||
|
||||
/* bishop / queen */
|
||||
to = pos->bb[c][BISHOP] | pos->bb[c][QUEEN];
|
||||
tmp = hq_bishop_moves(occ, sq) & to;
|
||||
attackers |= tmp;
|
||||
# ifdef DEBUG_ATTACK_ATTACKERS
|
||||
bb_print("att bishop/queen", tmp);
|
||||
# endif
|
||||
|
||||
/* rook / queen */
|
||||
to = pos->bb[c][ROOK] | pos->bb[c][QUEEN];
|
||||
tmp = hq_rook_moves(occ, sq) & to;
|
||||
attackers |= tmp;
|
||||
# ifdef DEBUG_ATTACK_ATTACKERS
|
||||
bb_print("att rook/queen", tmp);
|
||||
bb_print("ATTACKERS", attackers);
|
||||
# endif
|
||||
|
||||
return attackers;
|
||||
}
|
||||
|
||||
/**
|
||||
* sq_pinners() - get "pinners" on a square
|
||||
* @pos: position
|
||||
* @sq: square to test
|
||||
* @color: attacker color
|
||||
*
|
||||
* Find all @c pieces which are separated from @sq by only one piece (of
|
||||
* any color).
|
||||
*
|
||||
* @Return: bitboard of pinners.
|
||||
*/
|
||||
bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t color)
|
||||
{
|
||||
bitboard_t attackers, pinners = 0;
|
||||
bitboard_t occ = pos_occ(pos);
|
||||
bitboard_t maybe_pinner, tmp, lines;
|
||||
|
||||
/* bishop type */
|
||||
attackers = pos->bb[color][BISHOP] | pos->bb[color][QUEEN];
|
||||
/* occupancy on sq diag and antidiag */
|
||||
lines = (bb_sqdiag[sq] | bb_sqanti[sq]) & occ;
|
||||
bit_for_each64(maybe_pinner, tmp, attackers) {
|
||||
bitboard_t between = bb_between_excl[maybe_pinner][sq];
|
||||
/* keep only squares between AND on sq diag/anti */
|
||||
if (popcount64(between & lines) == 1)
|
||||
pinners |= BIT(maybe_pinner);
|
||||
}
|
||||
|
||||
/* same for rook type */
|
||||
attackers = pos->bb[color][ROOK] | pos->bb[color][QUEEN];
|
||||
lines = (bb_sqrank[sq] | bb_sqfile[sq]) & occ;
|
||||
bit_for_each64(maybe_pinner, tmp, attackers) {
|
||||
bitboard_t between = bb_between_excl[maybe_pinner][sq];
|
||||
if (popcount64(between & lines) == 1)
|
||||
pinners |= BIT(maybe_pinner);
|
||||
}
|
||||
# ifdef DEBUG_ATTACK_ATTACKERS1
|
||||
char str[32];
|
||||
printf("pinners : %s\n", pos_pinners2str(pos, str, sizeof(str)));
|
||||
printf("pinners : %lx\n", pinners);
|
||||
# endif
|
||||
return pinners;
|
||||
}
|
||||
|
||||
/**
|
||||
* sq_attackers() - find all attackers on a square
|
||||
* @pos: position
|
||||
* @sq: square to test
|
||||
*
|
||||
* Find all attacks on @sq. En-passant is not considered.
|
||||
* Just a wrapper over @sq_attackers().
|
||||
*
|
||||
* @Return: a bitboard of attackers.
|
||||
*/
|
||||
bitboard_t sq_attackers_all(const pos_t *pos, const square_t sq)
|
||||
{
|
||||
bitboard_t occ = pos_occ(pos);
|
||||
return sq_attackers(pos, occ, sq, WHITE) | sq_attackers(pos, occ, sq, BLACK);
|
||||
}
|
||||
26
src/attack.h
Normal file
26
src/attack.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/* attack.h - attack functions.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _ATTACK_H
|
||||
#define _ATTACK_H
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "bitboard.h"
|
||||
|
||||
bool sq_is_attacked(const pos_t *pos, const bitboard_t occ, const square_t sq, const color_t c);
|
||||
bool is_in_check(const pos_t *pos, const color_t color);
|
||||
|
||||
bitboard_t sq_attackers(const pos_t *pos, const bitboard_t occ, const square_t sq, const color_t c);
|
||||
bitboard_t sq_attackers_all(const pos_t *pos, const square_t sq);
|
||||
bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t c);
|
||||
#endif
|
||||
330
src/bitboard.c
Normal file
330
src/bitboard.c
Normal file
@@ -0,0 +1,330 @@
|
||||
/* bitboard.c - bitboard functions.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <brlib.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "piece.h"
|
||||
#include "board.h"
|
||||
#include "bitboard.h"
|
||||
|
||||
bitboard_t bb_sq[64];
|
||||
bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64];
|
||||
bitboard_t bb_between_excl[64][64];
|
||||
bitboard_t bb_between[64][64];
|
||||
bitboard_t bb_line[64][64];
|
||||
|
||||
bitboard_t bb_knight[64], bb_king[64], bb_pawn_attacks[2][64];
|
||||
|
||||
/* vectors are clockwise from N */
|
||||
static int knight_vector[8] = {
|
||||
NORTH_EAST + NORTH, NORTH_EAST + EAST,
|
||||
SOUTH_EAST + EAST, SOUTH_EAST + SOUTH,
|
||||
SOUTH_WEST + SOUTH, SOUTH_WEST + WEST,
|
||||
NORTH_WEST + WEST, NORTH_WEST + NORTH
|
||||
};
|
||||
static int king_vector[8] = {
|
||||
NORTH, NORTH_EAST, EAST, SOUTH_EAST,
|
||||
SOUTH, SOUTH_WEST, WEST, NORTH_WEST
|
||||
};
|
||||
|
||||
/**
|
||||
* bitboard_between_excl() - get bitboard of squares between two squares.
|
||||
* @sq1, @sq2: The two square_t squares
|
||||
*
|
||||
* From: http://www.talkchess.com/forum3/viewtopic.php?f=7&t=12499&start=14
|
||||
* This function may be used instead of bb_XXX arrays if cache pressure is high.
|
||||
*
|
||||
* @Return: bitboard_t, squares between @sq1 and @sq2 (excl. @sq1 and @sq2).
|
||||
*/
|
||||
bitboard_t bitboard_between_excl(square_t sq1, square_t sq2)
|
||||
{
|
||||
const bitboard_t m1 = -1;
|
||||
const bitboard_t a2a7 = U64(0x0001010101010100);
|
||||
const bitboard_t b7h1 = U64(0x0002040810204080);
|
||||
bitboard_t btwn_bits, ray_bits;
|
||||
u32 rank_diff, file_diff, anti_diff, diag_diff;
|
||||
|
||||
btwn_bits = (m1 << sq1) ^ (m1 << sq2); /* includes sq1 and sq2 */
|
||||
rank_diff = ((sq2 | 7) - sq1) >> 3, /* signed */
|
||||
file_diff = (sq2 & 7) - (sq1 & 7); /* signed */
|
||||
|
||||
anti_diff = rank_diff + file_diff;
|
||||
rank_diff = rank_diff & 15;
|
||||
file_diff = file_diff & 15;
|
||||
anti_diff = anti_diff & 15;
|
||||
diag_diff = rank_diff ^ file_diff;
|
||||
ray_bits = 2 * ((rank_diff - 1) >> 26);
|
||||
|
||||
ray_bits |= bswap64((m1 + diag_diff) & b7h1);
|
||||
ray_bits |= (m1 + anti_diff) & b7h1;
|
||||
ray_bits |= (m1 + file_diff) & a2a7;
|
||||
ray_bits *= btwn_bits & -btwn_bits;
|
||||
return ray_bits & btwn_bits;
|
||||
}
|
||||
|
||||
/**
|
||||
* bitboard_init() - initialize general bitboards
|
||||
*
|
||||
* Generate the following bitboards :
|
||||
* bb_sq[64]: square to bitboard
|
||||
* bb_sqrank[64]: square to rank
|
||||
* bb_sqfile[64]: square to file
|
||||
* bb_sqdiag[64]: square to diagonal
|
||||
* bb_sqanti[64]: square to antidiagonal
|
||||
*
|
||||
* bb_between_excl[64][64]: strict squares between two squares
|
||||
* bb_between[64][64]: squares between two squares including second square
|
||||
*
|
||||
* And the following pseudo move masks:
|
||||
* bb_knight[64]: knight moves
|
||||
* bb_king[64]: king moves
|
||||
*
|
||||
*/
|
||||
void bitboard_init(void)
|
||||
{
|
||||
/* for each square, the 4 masks: file, rank, diagonal, antidiagonal */
|
||||
struct { int df, dr; } vecs[4] = {
|
||||
{ 0, 1 }, /* vertical/file */
|
||||
{ 1, 0 }, /* horizontal/rank */
|
||||
{ 1, 1 }, /* diagonal */
|
||||
{ 1, -1 }, /* antidiagonal */
|
||||
} ;
|
||||
bitboard_t tmpbb[64][4] = { 0 };
|
||||
|
||||
/* 1) square to bitboard
|
||||
* in-between, sq2 excluded
|
||||
*/
|
||||
for (square_t sq1 = A1; sq1 <= H8; ++sq1) {
|
||||
bb_sq[sq1] = BIT(sq1);
|
||||
for (square_t sq2 = A1; sq2 <= H8; ++sq2)
|
||||
bb_between_excl[sq1][sq2] = bitboard_between_excl(sq1, sq2);
|
||||
}
|
||||
|
||||
/* 2) sq1-to-sq2 mask, sq2 included
|
||||
* square to file/rank/dia/anti bitmaps
|
||||
*/
|
||||
for (square_t sq = 0; sq < 64; ++sq) {
|
||||
file_t f = sq_file(sq);
|
||||
rank_t r = sq_rank(sq);
|
||||
for (int vec = 0; vec < 4; ++vec) {
|
||||
tmpbb[sq][vec] |= BIT(sq_make(f, r));
|
||||
for (int dir = -1; dir <= 1; dir += 2) {
|
||||
file_t df = dir * vecs[vec].df, f2 = f + df;
|
||||
rank_t dr = dir * vecs[vec].dr, r2 = r + dr;
|
||||
bitboard_t mask_between = 0;
|
||||
while (sq_coord_ok(f2) && sq_coord_ok(r2)) {
|
||||
square_t dest = sq_make(f2, r2);
|
||||
tmpbb[sq][vec] |= BIT(dest);
|
||||
mask_between |= BIT(dest);
|
||||
bb_between[sq][dest] = mask_between;
|
||||
f2 += df, r2 += dr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (square_t sq = 0; sq < 64; ++sq) {
|
||||
bb_sqfile[sq] = tmpbb[sq][0];
|
||||
bb_sqrank[sq] = tmpbb[sq][1];
|
||||
bb_sqdiag[sq] = tmpbb[sq][2];
|
||||
bb_sqanti[sq] = tmpbb[sq][3];
|
||||
}
|
||||
for (square_t sq1 = 0; sq1 < 64; ++sq1) {
|
||||
for (square_t sq2 = 0; sq2 < 64; ++sq2) {
|
||||
if (sq1 != sq2) {
|
||||
if (bb_sqfile[sq1] == bb_sqfile[sq2])
|
||||
bb_line[sq1][sq2] = bb_sqfile[sq1];
|
||||
else if (bb_sqrank[sq1] == bb_sqrank[sq2])
|
||||
bb_line[sq1][sq2] = bb_sqrank[sq1];
|
||||
else if (bb_sqdiag[sq1] == bb_sqdiag[sq2])
|
||||
bb_line[sq1][sq2] = bb_sqdiag[sq1];
|
||||
else if (bb_sqanti[sq1] == bb_sqanti[sq2])
|
||||
bb_line[sq1][sq2] = bb_sqanti[sq1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 3) pawn, knight and king attacks
|
||||
*/
|
||||
for (square_t sq = A1; sq <= H8; ++sq) {
|
||||
if (sq <= H7)
|
||||
bb_pawn_attacks[WHITE][sq] = bb_pawns_attacks(BIT(sq), sq_up(WHITE));
|
||||
if (sq >= A2)
|
||||
bb_pawn_attacks[BLACK][sq] = bb_pawns_attacks(BIT(sq), sq_up(BLACK));
|
||||
|
||||
for (int vec = 0; vec < 8; ++vec) {
|
||||
int dst = sq + knight_vector[vec];
|
||||
if (sq_ok(dst)) {
|
||||
if (sq_dist(dst, sq) == 2) {
|
||||
bb_knight[sq] |= bb_sq[dst];
|
||||
}
|
||||
}
|
||||
dst = sq + king_vector[vec];
|
||||
if (sq_ok(dst)) {
|
||||
if (sq_dist(dst, sq) == 1) {
|
||||
bb_king[sq] |= bb_sq[dst];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* bb_knight_moves() - get bitboard of knight pseudo-moves
|
||||
* @notmine: bitboard_t of squares not occupied by own pieces
|
||||
* @sq: knight square
|
||||
*
|
||||
* @Return: bitboard of available moves.
|
||||
*/
|
||||
bitboard_t bb_knight_moves(bitboard_t notmine, square_t sq)
|
||||
{
|
||||
return bb_knight[sq] & notmine;
|
||||
}
|
||||
|
||||
/**
|
||||
* bb_king_moves() - get bitboard of king pseudo-moves
|
||||
* @notmine: bitboard_t of squares not occupied by own pieces
|
||||
* @sq: king square
|
||||
*
|
||||
* @Return: bitboard of available moves.
|
||||
*/
|
||||
bitboard_t bb_king_moves(bitboard_t notmine, square_t sq)
|
||||
{
|
||||
return bb_king[sq] & notmine;
|
||||
}
|
||||
|
||||
/**
|
||||
* bb_print() - print simple bitboard representation
|
||||
* @title: a string or NULL
|
||||
* @bitboard: the bitboard
|
||||
*/
|
||||
void bb_print(const char *title, const bitboard_t bitboard)
|
||||
{
|
||||
//char c = p? p: 'X';
|
||||
if (title)
|
||||
printf("%s\n", title);
|
||||
for (rank_t r = RANK_8 + 1; r --> RANK_1;) { /* "downto"" op." */
|
||||
printf("%d ", r + 1);
|
||||
for (file_t f = FILE_A; f <= FILE_H; ++f) {
|
||||
printf(" %c", bitboard & bb_sq[sq_make(f, r)] ? 'X': '.');
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf(" a b c d e f g h\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* bb_print_multi() - print multiple bitboards horizontally
|
||||
* @title: a string or NULL
|
||||
* @n: number of bitboards
|
||||
* @bb_ptr...: pointers to bitboards
|
||||
*
|
||||
* @n is the number of bitboards to print. If @n > 10, it is reduced to 10
|
||||
*/
|
||||
void bb_print_multi(const char *title, int n, ...)
|
||||
{
|
||||
bitboard_t bb[8];
|
||||
va_list ap;
|
||||
|
||||
n = min(n, 10);
|
||||
|
||||
va_start(ap, n);
|
||||
for (int i = 0; i < n; ++i) { /* save all bitboards */
|
||||
bb[i] = va_arg(ap, bitboard_t);
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
if (title)
|
||||
printf("%s\n", title);
|
||||
|
||||
for (rank_t r = RANK_8 + 1; r --> RANK_1;) {
|
||||
for (int i = 0; i < n; ++i) {
|
||||
printf("%d ", r + 1);
|
||||
for (file_t f = FILE_A; f <= FILE_H; ++f) {
|
||||
printf(" %c", bb[i] & bb_sq[sq_make(f, r)] ? 'X': '.');
|
||||
}
|
||||
printf(" ");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
printf(" a b c d e f g h");
|
||||
printf(" ");
|
||||
}
|
||||
printf("\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* bb_rank_sprint() - print an u8 rank binary representation
|
||||
* @str: the destination string
|
||||
* @bb8: the uchar to print
|
||||
*
|
||||
* @return: @str, filled with ascii representation
|
||||
*/
|
||||
char *bb_rank_sprint(char *str, const uchar bb8)
|
||||
{
|
||||
file_t f;
|
||||
for (f = FILE_A; f <= FILE_H; ++f) {
|
||||
*(str + f) = bb8 & BIT(f) ? '1': '.';
|
||||
}
|
||||
*(str + f) = 0;
|
||||
//printf(" 0 1 2 3 4 5 6 7\n");
|
||||
//printf("\n");
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* bb_sq2str() - convert bitboard to a string with a list of squares.
|
||||
* @bb: bitboard
|
||||
* @str: destination string
|
||||
* @len: max @str length
|
||||
*
|
||||
* @str will be filled with the list of string representation of @bb, up to @len
|
||||
* characters.
|
||||
* 3 characters are used per square, so, for example, @str len should be :
|
||||
* - For a valid position checkers (two checkers max): 2*3 + 1 = 7.
|
||||
* - For a valid position pinners (8 pinners): 8*3 + 1 = 25.
|
||||
* - for a full bitboard: 64*3 + 1 = 193.
|
||||
*
|
||||
* If @len is not enough to fill all moves, the last fitting move is replaced by "...".
|
||||
*
|
||||
* @return: The string.
|
||||
*/
|
||||
char *bb_sq2str(const bitboard_t bb, char *str, const int len)
|
||||
{
|
||||
bitboard_t tmp, sq;
|
||||
int nocc = popcount64(bb);
|
||||
int needed = 3 * nocc + 1;
|
||||
int willdo = (int) len >= needed ? nocc: (len - 1) / 3 - 1;
|
||||
int current = 0;
|
||||
|
||||
//printf("sq2str bb=%lx len=%d willdo=%d\n", bb, len, willdo);
|
||||
bit_for_each64(sq, tmp, bb) {
|
||||
if (current == willdo) {
|
||||
strcpy(str + current * 3, "...");
|
||||
} else {
|
||||
strcpy(str + current * 3, sq_to_string(sq));
|
||||
str[current * 3 + 2] = ' ';
|
||||
}
|
||||
if (++current > willdo)
|
||||
break;
|
||||
}
|
||||
str[current * 3] = 0;
|
||||
return str;
|
||||
}
|
||||
319
src/bitboard.h
319
src/bitboard.h
@@ -1,6 +1,6 @@
|
||||
/* bitboard.h - bitboard definitions.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -11,60 +11,293 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BITBOARD_H
|
||||
#define BITBOARD_H
|
||||
#ifndef _BITBOARD_H
|
||||
#define _BITBOARD_H
|
||||
|
||||
#include <bits.h>
|
||||
#include <brlib.h>
|
||||
#include <bitops.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "board.h"
|
||||
#include "piece.h"
|
||||
|
||||
enum bb_square {
|
||||
A1 = 1UL << 0, B1 = 1UL << 1, C1 = 1UL << 2, D1 = 1UL << 3,
|
||||
E1 = 1UL << 4, F1 = 1UL << 5, G1 = 1UL << 6, H1 = 1UL << 7,
|
||||
/* mapping square -> bitboard */
|
||||
extern bitboard_t bb_sq[64];
|
||||
/* squares between sq1 and sq2, exclusing both */
|
||||
extern bitboard_t bb_between_excl[64][64];
|
||||
/* squares between sq1 and sq2, including sq2 */
|
||||
extern bitboard_t bb_between[64][64];
|
||||
|
||||
A2 = 1UL << 8, B2 = 1UL << 9, C2 = 1UL << 10, D2 = 1UL << 11,
|
||||
E2 = 1UL << 12, F2 = 1UL << 13, G2 = 1UL << 14, H2 = 1UL << 15,
|
||||
/**
|
||||
* bb_sqrank[64]: square to rank
|
||||
* bb_sqfile[64]: square to file
|
||||
* bb_sqdiag[64]: square to diagonal
|
||||
* bb_sqanti[64]: square to antidiagonal
|
||||
*/
|
||||
extern bitboard_t bb_sqrank[64], bb_sqfile[64], bb_sqdiag[64], bb_sqanti[64];
|
||||
|
||||
A3 = 1UL << 16, B3 = 1UL << 17, C3 = 1UL << 18, D3 = 1UL << 19,
|
||||
E3 = 1UL << 20, F3 = 1UL << 21, G3 = 1UL << 22, H3 = 1UL << 23,
|
||||
/* line (rank, file, diagonal or anti-diagonal) between two squares */
|
||||
extern bitboard_t bb_line[64][64];
|
||||
|
||||
A4 = 1UL << 24, B4 = 1UL << 25, C4 = 1UL << 26, D4 = 1UL << 27,
|
||||
E4 = 1UL << 28, F4 = 1UL << 29, G4 = 1UL << 30, H4 = 1UL << 31,
|
||||
/* pawn, knight and king attacks */
|
||||
extern bitboard_t bb_knight[64], bb_king[64], bb_pawn_attacks[2][64];
|
||||
|
||||
A5 = 1UL << 32, B5 = 1UL << 33, C5 = 1UL << 34, D5 = 1UL << 35,
|
||||
E5 = 1UL << 36, F5 = 1UL << 37, G5 = 1UL << 38, H5 = 1UL << 39,
|
||||
/* TODO (maybe C23?) when we can ensure an enum can be u64
|
||||
*
|
||||
* enum {
|
||||
* A1bb = mask(A1), A2bb = mask(A2), A3bb = mask(A3), A4bb = mask(A4),
|
||||
* A5bb = mask(A5), A6bb = mask(A6), A7bb = mask(A7), A8bb = mask(A8),
|
||||
* B1bb = mask(B1), B2bb = mask(B2), B3bb = mask(B3), B4bb = mask(B4),
|
||||
* B5bb = mask(B5), B6bb = mask(B6), B7bb = mask(B7), B8bb = mask(B8),
|
||||
* C1bb = mask(C1), C2bb = mask(C2), C3bb = mask(C3), C4bb = mask(C4),
|
||||
* C5bb = mask(C5), C6bb = mask(C6), C7bb = mask(C7), C8bb = mask(C8),
|
||||
* D1bb = mask(D1), D2bb = mask(D2), D3bb = mask(D3), D4bb = mask(D4),
|
||||
* D5bb = mask(D5), D6bb = mask(D6), D7bb = mask(D7), D8bb = mask(D8),
|
||||
* E1bb = mask(E1), E2bb = mask(E2), E3bb = mask(E3), E4bb = mask(E4),
|
||||
* E5bb = mask(E5), E6bb = mask(E6), E7bb = mask(E7), E8bb = mask(E8),
|
||||
* F1bb = mask(F1), F2bb = mask(F2), F3bb = mask(F3), F4bb = mask(F4),
|
||||
* F5bb = mask(F5), F6bb = mask(F6), F7bb = mask(F7), F8bb = mask(F8),
|
||||
* G1bb = mask(G1), G2bb = mask(G2), G3bb = mask(G3), G4bb = mask(G4),
|
||||
* G5bb = mask(G5), G6bb = mask(G6), G7bb = mask(G7), G8bb = mask(G8),
|
||||
* H1bb = mask(H1), H2bb = mask(H2), H3bb = mask(H3), H4bb = mask(H4),
|
||||
* H5bb = mask(H5), H6bb = mask(H6), H7bb = mask(H7), H8bb = mask(H8),
|
||||
* };
|
||||
*
|
||||
* enum {
|
||||
* FILE_Abb = 0x0101010101010101ull, FILE_Bbb = 0x0202020202020202ull,
|
||||
* FILE_Cbb = 0x0404040404040404ull, FILE_Dbb = 0x0808080808080808ull,
|
||||
* FILE_Ebb = 0x1010101010101010ull, FILE_Fbb = 0x2020202020202020ull,
|
||||
* FILE_Gbb = 0x4040404040404040ull, FILE_Hbb = 0x8080808080808080ull,
|
||||
*
|
||||
* RANK_1bb = 0x00000000000000ffull, RANK_2bb = 0x000000000000ff00ull,
|
||||
* RANK_3bb = 0x0000000000ff0000ull, RANK_4bb = 0x00000000ff000000ull,
|
||||
* RANK_5bb = 0x000000ff00000000ull, RANK_6bb = 0x0000ff0000000000ull,
|
||||
* RANK_7bb = 0x00ff000000000000ull, RANK_8bb = 0xff00000000000000ull
|
||||
* };
|
||||
*/
|
||||
|
||||
A6 = 1UL << 40, B6 = 1UL << 41, C6 = 1UL << 42, D6 = 1UL << 43,
|
||||
E6 = 1UL << 44, F6 = 1UL << 45, G6 = 1UL << 46, H6 = 1UL << 47,
|
||||
/* generated with bash:
|
||||
* R="ABCDEFGH"
|
||||
* F="12345678"
|
||||
* for i in {0..63}; do
|
||||
* printf "#define %c%cbb %#018llxull\n" ${R:i/8:1} ${F:i%8:1} $((1 << i))
|
||||
* done
|
||||
*/
|
||||
#define A1bb 0x0000000000000001ull
|
||||
#define A2bb 0x0000000000000002ull
|
||||
#define A3bb 0x0000000000000004ull
|
||||
#define A4bb 0x0000000000000008ull
|
||||
#define A5bb 0x0000000000000010ull
|
||||
#define A6bb 0x0000000000000020ull
|
||||
#define A7bb 0x0000000000000040ull
|
||||
#define A8bb 0x0000000000000080ull
|
||||
#define B1bb 0x0000000000000100ull
|
||||
#define B2bb 0x0000000000000200ull
|
||||
#define B3bb 0x0000000000000400ull
|
||||
#define B4bb 0x0000000000000800ull
|
||||
#define B5bb 0x0000000000001000ull
|
||||
#define B6bb 0x0000000000002000ull
|
||||
#define B7bb 0x0000000000004000ull
|
||||
#define B8bb 0x0000000000008000ull
|
||||
#define C1bb 0x0000000000010000ull
|
||||
#define C2bb 0x0000000000020000ull
|
||||
#define C3bb 0x0000000000040000ull
|
||||
#define C4bb 0x0000000000080000ull
|
||||
#define C5bb 0x0000000000100000ull
|
||||
#define C6bb 0x0000000000200000ull
|
||||
#define C7bb 0x0000000000400000ull
|
||||
#define C8bb 0x0000000000800000ull
|
||||
#define D1bb 0x0000000001000000ull
|
||||
#define D2bb 0x0000000002000000ull
|
||||
#define D3bb 0x0000000004000000ull
|
||||
#define D4bb 0x0000000008000000ull
|
||||
#define D5bb 0x0000000010000000ull
|
||||
#define D6bb 0x0000000020000000ull
|
||||
#define D7bb 0x0000000040000000ull
|
||||
#define D8bb 0x0000000080000000ull
|
||||
#define E1bb 0x0000000100000000ull
|
||||
#define E2bb 0x0000000200000000ull
|
||||
#define E3bb 0x0000000400000000ull
|
||||
#define E4bb 0x0000000800000000ull
|
||||
#define E5bb 0x0000001000000000ull
|
||||
#define E6bb 0x0000002000000000ull
|
||||
#define E7bb 0x0000004000000000ull
|
||||
#define E8bb 0x0000008000000000ull
|
||||
#define F1bb 0x0000010000000000ull
|
||||
#define F2bb 0x0000020000000000ull
|
||||
#define F3bb 0x0000040000000000ull
|
||||
#define F4bb 0x0000080000000000ull
|
||||
#define F5bb 0x0000100000000000ull
|
||||
#define F6bb 0x0000200000000000ull
|
||||
#define F7bb 0x0000400000000000ull
|
||||
#define F8bb 0x0000800000000000ull
|
||||
#define G1bb 0x0001000000000000ull
|
||||
#define G2bb 0x0002000000000000ull
|
||||
#define G3bb 0x0004000000000000ull
|
||||
#define G4bb 0x0008000000000000ull
|
||||
#define G5bb 0x0010000000000000ull
|
||||
#define G6bb 0x0020000000000000ull
|
||||
#define G7bb 0x0040000000000000ull
|
||||
#define G8bb 0x0080000000000000ull
|
||||
#define H1bb 0x0100000000000000ull
|
||||
#define H2bb 0x0200000000000000ull
|
||||
#define H3bb 0x0400000000000000ull
|
||||
#define H4bb 0x0800000000000000ull
|
||||
#define H5bb 0x1000000000000000ull
|
||||
#define H6bb 0x2000000000000000ull
|
||||
#define H7bb 0x4000000000000000ull
|
||||
#define H8bb 0x8000000000000000ull
|
||||
|
||||
A7 = 1UL << 48, B7 = 1UL << 49, C7 = 1UL << 50, D7 = 1UL << 51,
|
||||
E7 = 1UL << 52, F7 = 1UL << 53, G7 = 1UL << 54, H7 = 1UL << 55,
|
||||
#define FILE_Abb 0x0101010101010101ull
|
||||
#define FILE_Bbb 0x0202020202020202ull
|
||||
#define FILE_Cbb 0x0404040404040404ull
|
||||
#define FILE_Dbb 0x0808080808080808ull
|
||||
#define FILE_Ebb 0x1010101010101010ull
|
||||
#define FILE_Fbb 0x2020202020202020ull
|
||||
#define FILE_Gbb 0x4040404040404040ull
|
||||
#define FILE_Hbb 0x8080808080808080ull
|
||||
|
||||
A8 = 1UL << 56, B8 = 1UL << 57, C8 = 1UL << 58, D8 = 1UL << 59,
|
||||
E8 = 1UL << 60, F8 = 1UL << 61, G8 = 1UL << 62, H8 = 1UL << 63,
|
||||
};
|
||||
#define RANK_1bb 0x00000000000000ffull
|
||||
#define RANK_2bb 0x000000000000ff00ull
|
||||
#define RANK_3bb 0x0000000000ff0000ull
|
||||
#define RANK_4bb 0x00000000ff000000ull
|
||||
#define RANK_5bb 0x000000ff00000000ull
|
||||
#define RANK_6bb 0x0000ff0000000000ull
|
||||
#define RANK_7bb 0x00ff000000000000ull
|
||||
#define RANK_8bb 0xff00000000000000ull
|
||||
|
||||
enum bb_files {
|
||||
F_1 = A1 | A2 | A3 | A4 | A5 | A6 | A7 | A8,
|
||||
F_2 = B1 | B2 | B3 | B4 | B5 | B6 | B7 | B8,
|
||||
F_3 = C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8,
|
||||
F_4 = D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8,
|
||||
F_5 = E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8,
|
||||
F_6 = F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8,
|
||||
F_7 = G1 | G2 | G3 | G4 | G5 | G6 | G7 | G8,
|
||||
F_8 = H1 | H2 | H3 | H4 | H5 | H6 | H7 | H8,
|
||||
};
|
||||
|
||||
enum bb_ranges {
|
||||
R_1 = A1 | B1 | C1 | D1 | E1 | F1 | G1 | H1,
|
||||
R_2 = A2 | B2 | C2 | D2 | E2 | F2 | G2 | H2,
|
||||
R_3 = A3 | B3 | C3 | D3 | E3 | F3 | G3 | H3,
|
||||
R_4 = A4 | B4 | C4 | D4 | E4 | F4 | G4 | H4,
|
||||
R_5 = A5 | B5 | C5 | D5 | E5 | F5 | G5 | H5,
|
||||
R_6 = A6 | B6 | C6 | D6 | E6 | F6 | G6 | H6,
|
||||
R_7 = A7 | B7 | C7 | D7 | E7 | F7 | G7 | H7,
|
||||
R_8 = A8 | B8 | C8 | D8 | E8 | F8 | G8 | H8,
|
||||
};
|
||||
/*
|
||||
static __always_inline bitboard_t bb_rank(int rank)
|
||||
{
|
||||
return RANK_1bb << (rank * 8);
|
||||
}
|
||||
static __always_inline bitboard_t bb_rel_rank(int rank, color)
|
||||
{
|
||||
return RANK_1bb << (rank * 8);
|
||||
}
|
||||
static __always_inline bitboard_t bb_file(int file)
|
||||
{
|
||||
return FILE_Abb << file;
|
||||
}
|
||||
*/
|
||||
|
||||
#endif /* BOARD_H */
|
||||
/**
|
||||
* bb_first_bb() - return bitboard of first square of a bitboard.
|
||||
* @bb: bitboard
|
||||
*
|
||||
* bb must be non-zero.
|
||||
*
|
||||
* @return: bitboard of first square (lsb) of @bb.
|
||||
*/
|
||||
static __always_inline square_t bb_first_bb(bitboard_t bb)
|
||||
{
|
||||
return bb & -bb;
|
||||
}
|
||||
|
||||
/**
|
||||
* bb_next() - clear and return next (lsb) square of a bitboard.
|
||||
* @bb: &bitboard
|
||||
*
|
||||
* The bitboard addressed by @bb must be non-zero.
|
||||
*
|
||||
* @return: first bit (lsb) of @bb.
|
||||
*/
|
||||
static __always_inline square_t bb_next(bitboard_t *bb)
|
||||
{
|
||||
square_t sq = ctz64(*bb);
|
||||
*bb &= *bb - 1;
|
||||
return sq;
|
||||
}
|
||||
|
||||
/**
|
||||
* bb_multiple() - test if a bitboard has multiple bits.
|
||||
* @bb: bitboard
|
||||
*
|
||||
* @return: true if @bb has more than 1 bit, false otherwise.
|
||||
*/
|
||||
static __always_inline bool bb_multiple(bitboard_t bb)
|
||||
{
|
||||
return !!(bb & (bb - 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* bb_shift() - shift bitboard
|
||||
* @bb: bitboard
|
||||
*
|
||||
* No control is done on off-board shifting (i.e. shifting -1 from A2 gives H3).
|
||||
*
|
||||
* @return: true if @bb has more than 1 bit, false otherwise.
|
||||
*/
|
||||
static __always_inline bitboard_t bb_shift(bitboard_t bb, int shift)
|
||||
{
|
||||
return shift >= 0 ? bb << shift : bb >> -shift;
|
||||
}
|
||||
|
||||
/**
|
||||
* bb_pawns_attacks() - shift up pawns on both diagonals (attacks)
|
||||
* @bb: pawns bitboard
|
||||
* @push: shift value for pawn up
|
||||
*
|
||||
* Get the possible attacks for all @bb pawns.
|
||||
*
|
||||
* @return: squares attacked by @bbpawns
|
||||
*/
|
||||
static __always_inline bitboard_t bb_pawns_attacks(const bitboard_t bb, int push)
|
||||
{
|
||||
return bb_shift(bb & ~FILE_Abb, push - 1) | bb_shift(bb & ~FILE_Hbb, push + 1);
|
||||
}
|
||||
|
||||
#define bb_rank(r) ((u64) RANK_1bb << ((r) * 8))
|
||||
#define bb_file(f) ((u64) FILE_Abb << (f))
|
||||
|
||||
#define bb_rel_rank(r, c) bb_rank(sq_rel_rank(r, c))
|
||||
#define bb_rel_file(f, c) bb_file(sq_rel_rank(f, c)) /* likely useless */
|
||||
|
||||
/**
|
||||
* bb_sq_aligned() - check if two squares are aligned (same file or rank).
|
||||
* @sq1, @sq2: the two squares.
|
||||
*
|
||||
* @return: true if @sq1 and @sq2 are on same line, false otherwise.
|
||||
*/
|
||||
static __always_inline bool bb_sq_aligned(square_t sq1, square_t sq2)
|
||||
{
|
||||
return bb_line[sq1][sq2];
|
||||
}
|
||||
|
||||
/**
|
||||
* bb_sq_aligned3() - check if 3 squares are aligned (same file or rank).
|
||||
* @sq1, @sq2, @sq3: the three squares.
|
||||
*
|
||||
* @return: true if @sq1, @sq2, and @sq3 are aligned, false otherwise.
|
||||
*/
|
||||
static __always_inline bool bb_sq_aligned3(square_t sq1, square_t sq2, square_t sq3)
|
||||
{
|
||||
return bb_line[sq1][sq2] & BIT(sq3);
|
||||
}
|
||||
|
||||
/**
|
||||
* bb_sq_between() - check if a square is between two squares
|
||||
* @sq: the possibly "in-between" square
|
||||
* @sq1: square 1
|
||||
* @sq2: square 2
|
||||
*
|
||||
* @return: bitboard of @sq if between @sq1 and @sq2.
|
||||
*/
|
||||
static __always_inline bitboard_t bb_sq_between(square_t sq, square_t sq1, square_t sq2)
|
||||
{
|
||||
return bb_between_excl[sq1][sq2] & BIT(sq);
|
||||
}
|
||||
|
||||
bitboard_t bitboard_between_excl(square_t sq1, square_t sq2);
|
||||
void bitboard_init(void);
|
||||
|
||||
bitboard_t bb_knight_moves(bitboard_t notmine, square_t sq);
|
||||
bitboard_t bb_king_moves(bitboard_t notmine, square_t sq);
|
||||
|
||||
void bb_print(const char *title, const bitboard_t bitboard);
|
||||
void bb_print_multi(const char *title, const int n, ...);
|
||||
char *bb_rank_sprint(char *str, const uchar bb8);
|
||||
char *bb_sq2str(const bitboard_t bb, char *str, int len);
|
||||
|
||||
#endif /* _BITBOARD_H */
|
||||
|
||||
129
src/board.c
Normal file
129
src/board.c
Normal file
@@ -0,0 +1,129 @@
|
||||
/* board.c - 8x8 functions.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <brlib.h>
|
||||
|
||||
#include "board.h"
|
||||
#include "bitboard.h"
|
||||
|
||||
static const char *sq_strings[] = {
|
||||
"a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1",
|
||||
"a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
|
||||
"a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
|
||||
"a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
|
||||
"a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
|
||||
"a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
|
||||
"a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
|
||||
"a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8",
|
||||
};
|
||||
|
||||
/**
|
||||
* sq_to_string() - return a square string
|
||||
* @square: square (0-64)
|
||||
*
|
||||
* @Return: Pointer to @square string representation ("a1"-"h8").
|
||||
*/
|
||||
const char *sq_to_string(const square_t square)
|
||||
{
|
||||
return sq_strings[square];
|
||||
}
|
||||
|
||||
/**
|
||||
* sq_from_string() - return a square from a string
|
||||
* @sqstr: the square representation (a1-h8)
|
||||
*
|
||||
* @Return: square_t representation of str.
|
||||
*/
|
||||
square_t sq_from_string(const char *sqstr)
|
||||
{
|
||||
file_t f = C2FILE(sqstr[0]);
|
||||
rank_t r = C2RANK(sqstr[1]);
|
||||
return sq_coord_ok(f) && sq_coord_ok(r) ? sq_make(f, r): SQUARE_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* board_print() - Print a board
|
||||
* @board: &board_t to print
|
||||
*/
|
||||
void board_print(const piece_t *board)
|
||||
{
|
||||
printf(" +---+---+---+---+---+---+---+---+\n");
|
||||
for (int rank = 7; rank >= 0; --rank) {
|
||||
printf("%c |", rank + '1');
|
||||
for (int file = 0; file < 8; ++file) {
|
||||
piece_t pc = board[sq_make(file, rank)];
|
||||
# ifdef DIAGRAM_SYM
|
||||
printf(" %s |", pc? piece_to_sym(pc): " ");
|
||||
# else
|
||||
printf(" %s |", pc? piece_to_fen(pc): " ");
|
||||
# endif
|
||||
}
|
||||
printf("\n +---+---+---+---+---+---+---+---+\n");
|
||||
}
|
||||
printf(" A B C D E F G H\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* board_print_mask() - Print a board position with some reversed squares
|
||||
* @board: &board_t to print
|
||||
* @mask: a bitboard indicating reverse color displayed squares
|
||||
*
|
||||
* Squares corresponding to @mask will be displayed in reverse colors.
|
||||
*/
|
||||
void board_print_mask(const piece_t *board, const bitboard_t mask)
|
||||
{
|
||||
// 6: blink
|
||||
# define REVERSE "\e[7m▌"
|
||||
# define RESET "▐\e[0m"
|
||||
printf(" +---+---+---+---+---+---+---+---+\n");
|
||||
for (int rank = 7; rank >= 0; --rank) {
|
||||
printf("%c |", rank + '1');
|
||||
for (int file = 0; file < 8; ++file) {
|
||||
square_t sq = sq_make(file, rank);
|
||||
piece_t pc = board[sq];
|
||||
bitboard_t set = BIT(sq) & mask;
|
||||
printf("%s", set? REVERSE : " ");
|
||||
# ifdef DIAGRAM_SYM
|
||||
printf("%s", pc? piece_to_sym(pc): " ");
|
||||
# else
|
||||
printf("%s", pc? piece_to_char_color(pc): " ");
|
||||
# endif
|
||||
printf("%s|", set? RESET : " ");
|
||||
}
|
||||
printf("\n +---+---+---+---+---+---+---+---+\n");
|
||||
}
|
||||
printf(" A B C D E F G H\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* board_print_raw - print raw (octal or FEN symbol) board
|
||||
* @bb: the bitboard
|
||||
* @type: int, 0 for octal, 1 for fen symbol
|
||||
*/
|
||||
void board_print_raw(const piece_t *board, const int type)
|
||||
{
|
||||
for (rank_t r = RANK_8 + 1; r --> RANK_1;) {
|
||||
for (file_t f = FILE_A; f <= FILE_H; ++f) {
|
||||
piece_t p = board[sq_make(f, r)];
|
||||
if (type) {
|
||||
printf("%s ", p == EMPTY? ".": piece_to_char(p));
|
||||
} else {
|
||||
printf("%02o ", p);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
119
src/board.h
119
src/board.h
@@ -1,6 +1,6 @@
|
||||
/* board.h - board definitions.
|
||||
/* board.h - 8x8 board definitions.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -11,70 +11,83 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BOARD_H
|
||||
#define BOARD_H
|
||||
#ifndef _BOARD_H
|
||||
#define _BOARD_H
|
||||
|
||||
#include <brlib.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include "chessdefs.h"
|
||||
#include "piece.h"
|
||||
#include "bitboard.h"
|
||||
|
||||
typedef struct board_s {
|
||||
piece_t piece;
|
||||
piece_list_t *s_piece;
|
||||
//struct list_head *s_piece;
|
||||
} board_t; /* 0x88 board */
|
||||
#define BOARDSIZE (8*8*2)
|
||||
/* from human to machine */
|
||||
#define C2FILE(c) (tolower(c) - 'a')
|
||||
#define C2RANK(c) (tolower(c) - '1')
|
||||
/* from machine to human */
|
||||
#define FILE2C(f) ((f) + 'a')
|
||||
#define RANK2C(r) ((r) + '1')
|
||||
|
||||
/* definitions for 0x88 representation
|
||||
/* a square is defined as
|
||||
* rrrfff
|
||||
*/
|
||||
#define SQ88(f, r) (((r) << 4) | (f)) /* from rank,file to sq88 */
|
||||
#define F88(s) ((s) & 0x0f) /* from sq88 to file */
|
||||
#define R88(s) ((s) >> 4) /* from sq88 to rank */
|
||||
#define SQ_FILEMASK (007) /* warning, octal */
|
||||
#define SQ_RANKMASK (070)
|
||||
|
||||
#define SETF88(s, r) ((s) &= 0xf0, (s) |= (r))
|
||||
#define SETR88(s, f) ((s) &= 0x0f, (s) |= (f)<<4)
|
||||
|
||||
#define SQ88_NOK(s) ((s) & 0x88) /* invalid square */
|
||||
#define SQ88_OK(s) (!SQ88_NOK(s))
|
||||
|
||||
/* definitions for bitboard representation
|
||||
/* flip a 0-63 square:
|
||||
* Vertical: G8 (62) becomes G1 (6)
|
||||
* Horizontal: G8 (62) becomes B8 (57)
|
||||
*/
|
||||
#define BB(f, r) (1ULL << (8 * (r) + (f))) /* from rank,file to bitboard */
|
||||
#define SQ88_2_BB(s) (BB(F88(s), R88(s))) /* from sq88 to bitboard */
|
||||
#define FILEBB(b) ((b) % 8) /* from sq88 to file */
|
||||
#define RANKBB(b) ((b) / 8) /* from sq88 to rank */
|
||||
#define FLIP_V(sq) ((sq) ^ 56)
|
||||
#define FLIP_H(sq) ((sq) ^ 7)
|
||||
#define FLIP_HV(sq) ((sq) ^ 63) /* FLIP_V ^ FLIP_H */
|
||||
|
||||
#define SQ88_NOK(s) ((s) & 0x88) /* invalid square */
|
||||
#define SQ88_OK(s) (!SQ88_NOK(s))
|
||||
/* TODO: revert to macros after bitboard migration */
|
||||
static __always_inline square_t sq_make(file_t file, rank_t rank)
|
||||
{
|
||||
return (rank << 3) + file;
|
||||
}
|
||||
static __always_inline file_t sq_file(square_t square)
|
||||
{
|
||||
return square & SQ_FILEMASK;
|
||||
}
|
||||
static __always_inline rank_t sq_rank(square_t square)
|
||||
{
|
||||
return square >> 3;
|
||||
}
|
||||
|
||||
/* piece human notation
|
||||
#define sq_ok(sq) ((sq) <= H8)
|
||||
#define sq_coord_ok(c) ((c) < 8)
|
||||
|
||||
/**
|
||||
* sq_dist() - Chebyshev (king) distance between two squares (macro).
|
||||
* @sq1, @sq2: The two squares.
|
||||
*
|
||||
* See: https://www.chessprogramming.org/Distance
|
||||
* Distance is max( |r2 - r1|, |f2 - f1| )
|
||||
*
|
||||
* @Return: the Chebyshev distance.
|
||||
*/
|
||||
#define CHAR_EMPTY ' '
|
||||
#define CHAR_PAWN 'P'
|
||||
#define CHAR_KNIGHT 'N'
|
||||
#define CHAR_BISHOP 'B'
|
||||
#define CHAR_ROOK 'R'
|
||||
#define CHAR_QUEEN 'Q'
|
||||
#define CHAR_KING 'K'
|
||||
#define sq_dist(sq1, sq2) (max(abs(sq_file(sq2) - sq_file(sq1)), \
|
||||
abs(sq_rank(sq2) - sq_rank(sq1))))
|
||||
|
||||
/* from human to machine
|
||||
/**
|
||||
* sq_taxi() - Manhattan (taxi) distance between two squares (macro).
|
||||
* @sq1, @sq2: The two squares.
|
||||
*
|
||||
* See: https://www.chessprogramming.org/Distance
|
||||
* Distance is |r2 - r1| + |f2 - f1|.
|
||||
*
|
||||
* @Return: the Manhattan distance.
|
||||
*/
|
||||
#define C2FILE(c) (tolower(c) - 'a')
|
||||
#define C2RANK(c) (tolower(c) - '1')
|
||||
/* from machine to human
|
||||
*/
|
||||
#define FILE2C(f) ((f) + 'a')
|
||||
#define RANK2C(r) ((r) + '1')
|
||||
#define sq_taxi(sq1, sq2) (abs(sq_file(sq2) - sq_file(sq1)) + \
|
||||
abs(sq_rank(sq2) - sq_rank(sq1)))
|
||||
|
||||
enum x88_square {
|
||||
x88_A1=0x00, x88_B1, x88_C1, x88_D1, x88_E1, x88_F1, x88_G1, x88_H1,
|
||||
x88_A2=0x10, x88_B2, x88_C2, x88_D2, x88_E2, x88_F2, x88_G2, x88_H2,
|
||||
x88_A3=0x20, x88_B3, x88_C3, x88_D3, x88_E3, x88_F3, x88_G3, x88_H3,
|
||||
x88_A4=0x30, x88_B4, x88_C4, x88_D4, x88_E4, x88_F4, x88_G4, x88_H4,
|
||||
x88_A5=0x40, x88_B5, x88_C5, x88_D5, x88_E5, x88_F5, x88_G5, x88_H5,
|
||||
x88_A6=0x50, x88_B6, x88_C6, x88_D6, x88_E6, x88_F6, x88_G6, x88_H6,
|
||||
x88_A7=0x60, x88_B7, x88_C7, x88_D7, x88_E7, x88_F7, x88_G7, x88_H7,
|
||||
x88_A8=0x70, x88_B8, x88_C8, x88_D8, x88_E8, x88_F8, x88_G8, x88_H8,
|
||||
};
|
||||
extern const char *sq_to_string(const square_t sq);
|
||||
extern square_t sq_from_string(const char *sq_string);
|
||||
|
||||
#endif /* BOARD_H */
|
||||
extern void board_print(const piece_t *board);
|
||||
extern void board_print_mask(const piece_t *board, const bitboard_t mask);
|
||||
extern void board_print_raw(const piece_t *board, const int type);
|
||||
|
||||
#endif /* _BOARD_H */
|
||||
|
||||
501
src/brchess.c
501
src/brchess.c
@@ -1,6 +1,6 @@
|
||||
/* brchess.c - main loop.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -12,476 +12,18 @@
|
||||
*/
|
||||
|
||||
|
||||
#include <err.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <br.h>
|
||||
#include <list.h>
|
||||
#include <debug.h>
|
||||
#include <brlib.h>
|
||||
|
||||
#include "brchess.h"
|
||||
#include "chessdefs.h"
|
||||
#include "board.h"
|
||||
#include "piece.h"
|
||||
#include "move.h"
|
||||
#include "position.h"
|
||||
#include "fen.h"
|
||||
#include "eval.h"
|
||||
#include "eval-simple.h"
|
||||
#include "search.h"
|
||||
#include "hist.h"
|
||||
#include "uci.h"
|
||||
|
||||
struct command {
|
||||
char *name; /* User printable name */
|
||||
int (*func)(pos_t *, char *); /* function doing the job */
|
||||
char *doc; /* function doc */
|
||||
};
|
||||
|
||||
/* readline example inspired by :
|
||||
* - https://thoughtbot.com/blog/tab-completion-in-gnu-readline
|
||||
* - http://web.mit.edu/gnu/doc/html/rlman_2.html
|
||||
*/
|
||||
char **commands_completion(const char *, int, int);
|
||||
char *commands_generator(const char *, int);
|
||||
char *escape(const char *);
|
||||
int quote_detector(char *, int);
|
||||
int execute_line (pos_t *, char *line);
|
||||
struct command *find_command (char *);
|
||||
char *stripwhite (char *string);
|
||||
|
||||
/* The names of functions that actually do the manipulation. */
|
||||
int do_help(pos_t *, char*);
|
||||
int do_fen(pos_t *, char*);
|
||||
int do_init(pos_t *, char*);
|
||||
int do_pos(pos_t *, char*);
|
||||
int do_genmoves(pos_t *, char*);
|
||||
int do_prmoves(pos_t *, char*);
|
||||
//int do_prmovepos(pos_t *pos, char *arg);
|
||||
int do_prpieces(pos_t *pos, char *arg);
|
||||
int do_memstats(pos_t *, char*);
|
||||
int do_eval(pos_t *, char*);
|
||||
int do_simple_eval(pos_t *, char*);
|
||||
int do_move(pos_t *, char*);
|
||||
int do_quit(pos_t *, char*);
|
||||
int do_debug(pos_t *, char*);
|
||||
int do_depth(pos_t *, char*);
|
||||
int do_search(pos_t *, char*);
|
||||
int do_pvs(pos_t *, char*);
|
||||
|
||||
struct command commands[] = {
|
||||
{ "help", do_help, "Display this text" },
|
||||
{ "?", do_help, "Synonym for 'help'" },
|
||||
{ "fen", do_fen, "Set position to FEN" },
|
||||
{ "init", do_init, "Set position to normal start position" },
|
||||
{ "pos", do_pos, "Print current position" },
|
||||
{ "quit", do_quit, "Quit" },
|
||||
{ "genmove", do_genmoves, "Generate move list for " },
|
||||
{ "prmoves", do_prmoves, "Print position move list" },
|
||||
// { "prmovepos", do_prmovepos, "Print Nth move resulting position" },
|
||||
{ "prpieces", do_prpieces, "Print Pieces (from pieces lists)" },
|
||||
{ "memstats", do_memstats, "Generate next move list" },
|
||||
{ "eval", do_eval, "Eval current position" },
|
||||
{ "simple-eval", do_simple_eval, "Simple eval current position" },
|
||||
{ "do_move", do_move, "execute nth move on current position" },
|
||||
{ "debug", do_debug, "Set log level to LEVEL" },
|
||||
{ "depth", do_depth, "Set search depth to N" },
|
||||
{ "search", do_search, "Search best move (negamax)" },
|
||||
{ "pvs", do_pvs, "Search best move (Principal Variation Search)" },
|
||||
{ NULL, (int(*)()) NULL, NULL }
|
||||
};
|
||||
|
||||
static int done=0;
|
||||
static int depth=1;
|
||||
|
||||
int brchess(pos_t *pos)
|
||||
{
|
||||
char *buffer, *s;
|
||||
|
||||
rl_attempted_completion_function = commands_completion;
|
||||
rl_completer_quote_characters = "'\"";
|
||||
rl_completer_word_break_characters = " ";
|
||||
rl_char_is_quoted_p = "e_detector;
|
||||
|
||||
while (!done) {
|
||||
buffer = readline("chess> ");
|
||||
if (!buffer)
|
||||
break;
|
||||
/* Remove leading and trailing whitespace from the line.
|
||||
* Then, if there is anything left, add it to the history list
|
||||
* and execute it.
|
||||
*/
|
||||
s = stripwhite(buffer);
|
||||
|
||||
if (*s) {
|
||||
add_history(s);
|
||||
execute_line(pos, s);
|
||||
}
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//char **commands_completion(const char *text, int start, int end)
|
||||
char **commands_completion(const char *text, __unused int start, __unused int end)
|
||||
{
|
||||
rl_attempted_completion_over = 1;
|
||||
return rl_completion_matches(text, commands_generator);
|
||||
}
|
||||
|
||||
char *commands_generator(const char *text, int state)
|
||||
{
|
||||
static int list_index, len;
|
||||
char *name;
|
||||
|
||||
if (!state) {
|
||||
list_index = 0;
|
||||
len = strlen(text);
|
||||
}
|
||||
|
||||
while ((name = commands[list_index++].name)) {
|
||||
if (rl_completion_quote_character) {
|
||||
name = strdup(name);
|
||||
} else {
|
||||
name = escape(name);
|
||||
}
|
||||
|
||||
if (strncmp(name, text, len) == 0) {
|
||||
return name;
|
||||
} else {
|
||||
free(name);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *escape(const char *original)
|
||||
{
|
||||
size_t original_len;
|
||||
size_t i, j;
|
||||
char *escaped, *resized_escaped;
|
||||
|
||||
original_len = strlen(original);
|
||||
|
||||
if (original_len > SIZE_MAX / 2) {
|
||||
errx(1, "string too long to escape");
|
||||
}
|
||||
|
||||
if ((escaped = malloc(2 * original_len + 1)) == NULL) {
|
||||
err(1, NULL);
|
||||
}
|
||||
|
||||
for (i = 0, j = 0; i < original_len; ++i, ++j) {
|
||||
if (original[i] == ' ') {
|
||||
escaped[j++] = '\\';
|
||||
}
|
||||
escaped[j] = original[i];
|
||||
}
|
||||
escaped[j] = '\0';
|
||||
|
||||
if ((resized_escaped = realloc(escaped, j)) == NULL) {
|
||||
free(escaped);
|
||||
resized_escaped = NULL;
|
||||
err(1, NULL);
|
||||
}
|
||||
|
||||
return resized_escaped;
|
||||
}
|
||||
|
||||
int quote_detector(char *line, int index)
|
||||
{
|
||||
return index > 0
|
||||
&& line[index - 1] == '\\'
|
||||
&&!quote_detector(line, index - 1);
|
||||
}
|
||||
|
||||
/* Execute a command line. */
|
||||
int execute_line(pos_t *pos, char *line)
|
||||
{
|
||||
register int i;
|
||||
struct command *command;
|
||||
char *word;
|
||||
|
||||
/* Isolate the command word. */
|
||||
i = 0;
|
||||
while (line[i] && whitespace(line[i]))
|
||||
i++;
|
||||
word = line + i;
|
||||
|
||||
while (line[i] && !whitespace(line[i]))
|
||||
i++;
|
||||
|
||||
if (line[i])
|
||||
line[i++] = '\0';
|
||||
|
||||
command = find_command(word);
|
||||
|
||||
if (!command) {
|
||||
fprintf(stderr, "%s: Unknown command.\n", word);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Get argument to command, if any. */
|
||||
while (whitespace(line[i]))
|
||||
i++;
|
||||
|
||||
word = line + i;
|
||||
|
||||
/* return command number */
|
||||
return (*command->func)(pos, word);
|
||||
}
|
||||
|
||||
/* Look up NAME as the name of a command, and return a pointer to that
|
||||
command. Return a NULL pointer if NAME isn't a command name. */
|
||||
struct command *find_command(char *name)
|
||||
{
|
||||
register int i;
|
||||
|
||||
for (i = 0; commands[i].name; i++)
|
||||
if (strcmp(name, commands[i].name) == 0)
|
||||
return &commands[i];
|
||||
|
||||
return (struct command *)NULL;
|
||||
}
|
||||
|
||||
/* Strip whitespace from the start and end of STRING. Return a pointer
|
||||
into STRING. */
|
||||
char *stripwhite(char *string)
|
||||
{
|
||||
register char *s, *t;
|
||||
|
||||
for (s = string; whitespace(*s); s++)
|
||||
;
|
||||
|
||||
if (*s == 0)
|
||||
return s;
|
||||
|
||||
t = s + strlen(s) - 1;
|
||||
while (t > s && whitespace(*t))
|
||||
t--;
|
||||
*++t = '\0';
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int do_eval(__unused pos_t *pos, __unused char *arg)
|
||||
{
|
||||
eval_t material[2], control[2], mobility[2];
|
||||
for (int color = WHITE; color <= BLACK; ++color) {
|
||||
material[color] = eval_material(pos, color);
|
||||
control[color] = eval_square_control(pos, color);
|
||||
mobility[color] = eval_mobility(pos, color);
|
||||
printf("%s: material=%d mobility=%d controlled=%d\n",
|
||||
color? "Black": "White", material[color],
|
||||
mobility[color], control[color]);
|
||||
}
|
||||
eval_t res = eval(pos);
|
||||
printf("eval = %d centipawns\n", res);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_simple_eval(__unused pos_t *pos, __unused char *arg)
|
||||
{
|
||||
eval_t eval = eval_simple(pos);
|
||||
printf("eval = %d centipawns\n", eval);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_fen(pos_t *pos, char *arg)
|
||||
{
|
||||
fen2pos(pos, arg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_init(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
pos_startpos(pos);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_pos(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
pos_print(pos);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_genmoves(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
moves_gen_all(pos);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_prmoves(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
uint debug_level = debug_level_get();
|
||||
debug_level_set(1);
|
||||
moves_print(pos, M_PR_SEPARATE | M_PR_NUM | M_PR_LONG);
|
||||
debug_level_set(debug_level);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* int do_prmovepos(pos_t *pos, char *arg)
|
||||
* {
|
||||
* struct list_head *p_cur, *tmp;
|
||||
* int movenum = atoi(arg), cur = 0; /\* starts with 0 *\/
|
||||
* move_t *move;
|
||||
*
|
||||
* log_f(1, "%s\n", arg);
|
||||
* list_for_each_safe(p_cur, tmp, &pos->moves[pos->turn]) {
|
||||
* move = list_entry(p_cur, move_t, list);
|
||||
* if (cur++ == movenum) {
|
||||
* pos_print(move->newpos);
|
||||
* break;
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* return 1;
|
||||
* }
|
||||
*/
|
||||
|
||||
int do_prpieces(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
log_f(1, "%s\n", arg);
|
||||
pos_pieces_print(pos);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_memstats(__unused pos_t *pos,__unused char *arg)
|
||||
{
|
||||
moves_pool_stats();
|
||||
piece_pool_stats();
|
||||
pos_pool_stats();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_move(__unused pos_t *pos, __unused char *arg)
|
||||
{
|
||||
int i = 1, nmove = atoi(arg);
|
||||
move_t *move;
|
||||
pos_t *newpos;
|
||||
|
||||
if (list_empty(&pos->moves[pos->turn])) {
|
||||
log_f(1, "No moves list.\n");
|
||||
return 0;
|
||||
}
|
||||
list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
if (i == nmove)
|
||||
goto doit;
|
||||
i++;
|
||||
}
|
||||
log_f(1, "Invalid <%d> move, should be <1-%d>.\n", nmove, i);
|
||||
return 0;
|
||||
doit:
|
||||
newpos = move_do(pos, move);
|
||||
pos_print(newpos);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_quit(__unused pos_t *pos, __unused char *arg)
|
||||
{
|
||||
return done = 1;
|
||||
}
|
||||
|
||||
int do_debug(__unused pos_t *pos, __unused char *arg)
|
||||
{
|
||||
debug_level_set(atoi(arg));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Print out help for ARG, or for all of the commands if ARG is
|
||||
not present. */
|
||||
int do_help(__unused pos_t *pos, __unused char *arg)
|
||||
{
|
||||
int i;
|
||||
int printed = 0;
|
||||
|
||||
for (i = 0; commands[i].name; i++) {
|
||||
if (!*arg || (strcmp(arg, commands[i].name) == 0)) {
|
||||
printf("%-11.11s%s.\n", commands[i].name, commands[i].doc);
|
||||
printed++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!printed) {
|
||||
printf("No commands match `%s'. Possibilties are:\n", arg);
|
||||
|
||||
for (i = 0; commands[i].name; i++) {
|
||||
/* Print in six columns. */
|
||||
if (printed == 6) {
|
||||
printed = 0;
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("%s\t", commands[i].name);
|
||||
printed++;
|
||||
}
|
||||
|
||||
if (printed)
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int do_depth(__unused pos_t *pos, char *arg)
|
||||
{
|
||||
depth = atoi(arg);
|
||||
printf("depth = %d\n", depth);
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
int do_search(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
int debug_level = debug_level_get();
|
||||
float timer1, timer2, nodes_sec;
|
||||
|
||||
timer1 = debug_timer_elapsed();
|
||||
negamax(pos, depth, pos->turn == WHITE ? 1 : -1);
|
||||
timer2 = debug_timer_elapsed();
|
||||
nodes_sec = (float) pos->node_count / ((float) (timer2 - timer1) / (float)NANOSEC);
|
||||
log(1, "best=");
|
||||
debug_level_set(1);
|
||||
move_print(0, pos->bestmove, 0);
|
||||
debug_level_set(debug_level);
|
||||
log(1, " negamax=%d\n", pos->bestmove->negamax);
|
||||
printf("Depth:%d Nodes:%luK time:%.02fs (%.0f kn/s)\n", depth,
|
||||
pos->node_count / 1000, (timer2 - timer1)/NANOSEC, nodes_sec/1000);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_pvs(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
int debug_level = debug_level_get();
|
||||
float timer1, timer2, nodes_sec;
|
||||
eval_t _pvs;
|
||||
|
||||
timer1 = debug_timer_elapsed();
|
||||
moves_gen_eval_sort(pos);
|
||||
_pvs = pvs(pos, depth, EVAL_MIN, EVAL_MAX, pos->turn == WHITE ? 1 : -1);
|
||||
timer2 = debug_timer_elapsed();
|
||||
nodes_sec = (float) pos->node_count / ((float) (timer2 - timer1) / (float)NANOSEC);
|
||||
log(1, "best=");
|
||||
if (pos->bestmove) {
|
||||
debug_level_set(1);
|
||||
move_print(0, pos->bestmove, 0);
|
||||
debug_level_set(debug_level);
|
||||
log(1, " pvs=%d stored=%d\n", _pvs, pos->bestmove->negamax);
|
||||
} else {
|
||||
log(1, "<no-best-move>");
|
||||
}
|
||||
printf("Depth:%d Nodes:%luK time:%.02fs (%.0f kn/s)\n", depth,
|
||||
pos->node_count / 1000, (timer2 - timer1)/NANOSEC, nodes_sec/1000);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef BIN_brchess
|
||||
/** main()
|
||||
* options:
|
||||
int brchess(pos_t *pos)
|
||||
/**
|
||||
* usage - brchess usage function.
|
||||
*
|
||||
*/
|
||||
static int usage(char *prg)
|
||||
@@ -490,24 +32,28 @@ static int usage(char *prg)
|
||||
return 1;
|
||||
}
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
pos_t *pos;
|
||||
int opt;
|
||||
|
||||
piece_pool_init();
|
||||
moves_pool_init();
|
||||
pos_pool_init();
|
||||
pos = pos_get();
|
||||
debug_init(1, stderr, true);
|
||||
eval_simple_init();
|
||||
printf("brchess " VERSION "\n");
|
||||
init_all();
|
||||
pos = pos_new();
|
||||
printf("Engine ready.\n");
|
||||
|
||||
// size_t len = 0;
|
||||
// char *str = NULL;
|
||||
//while (getline(&str, &len, stdin) >= 0) {
|
||||
// printf("[%s] -> ", str);
|
||||
// int newlen = string_trim(str);
|
||||
// printf("%d [%s]\n", newlen, str);
|
||||
//}
|
||||
//exit(0);
|
||||
while ((opt = getopt(ac, av, "d:f:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'd':
|
||||
debug_level_set(atoi(optarg));
|
||||
//debug_level_set(atoi(optarg));
|
||||
break;
|
||||
case 'f':
|
||||
fen2pos(pos, optarg);
|
||||
@@ -519,6 +65,5 @@ int main(int ac, char **av)
|
||||
if (optind < ac)
|
||||
return usage(*av);
|
||||
|
||||
return brchess(pos);
|
||||
return uci(pos);
|
||||
}
|
||||
#endif /* BIN_brchess */
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* bodichess.h - main loop.
|
||||
/* brchess.h - main loop.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -14,8 +14,4 @@
|
||||
#ifndef BRCHESS_H
|
||||
#define BRCHESS_H
|
||||
|
||||
#include "position.h"
|
||||
|
||||
int brchess(pos_t *pos);
|
||||
|
||||
#endif /* BRCHESS_H */
|
||||
|
||||
214
src/chessdefs.h
214
src/chessdefs.h
@@ -1,6 +1,6 @@
|
||||
/* chessdefs.h - generic chess definitions.
|
||||
/* chessdefs.h - generic/catchall chess definitions.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -11,119 +11,137 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CHESSDEFS_H
|
||||
#define CHESSDEFS_H
|
||||
#ifndef _CHESSDEFS_H
|
||||
#define _CHESSDEFS_H
|
||||
|
||||
#include <bits.h>
|
||||
#include <brlib.h> /* brlib types */
|
||||
|
||||
/* piece_t bits structure
|
||||
* MSB 8 7 6 5 4 3 2 1 LSB
|
||||
* 1: color (0 for white)
|
||||
* 2-7: bit set for pawn (2), knight, bishop, rook, queen, king (7)
|
||||
#define ONE 1ul
|
||||
#define U64(const_u64) const_u64##UL
|
||||
#define BIT(i) ( (u64) (ONE << (i)) )
|
||||
|
||||
#define BOARDSIZE (8*8)
|
||||
#define GAMESIZE 1024 /* max game size (512 moves) */
|
||||
|
||||
/**
|
||||
* sq_rel - get relative square
|
||||
* @sq: white point of view square
|
||||
* @c: color
|
||||
*
|
||||
* Get relative (mirrored if @c is BLACK) square.
|
||||
* Example: sq_rel(A1, WHITE) = A1, sq_rel(B2, BLACK) = B7
|
||||
*
|
||||
* @return: Relative square.
|
||||
*/
|
||||
typedef u8 piece_t;
|
||||
#define sq_rel(sq, c) ((square_t)((sq) ^ (56 * (c))))
|
||||
|
||||
enum {
|
||||
E_EMPTY = 0,
|
||||
E_COLOR, /* LSB */
|
||||
E_PAWN,
|
||||
E_KNIGHT,
|
||||
E_BISHOP,
|
||||
E_ROOK,
|
||||
E_QUEEN,
|
||||
E_KING,
|
||||
};
|
||||
|
||||
/* pos_t bitboards tables
|
||||
/**
|
||||
* sq_rel_rank - get relative rank
|
||||
* @rank: white point of view rank
|
||||
* @c: color
|
||||
*
|
||||
* Get relative (mirrored if @c is BLACK) rank.
|
||||
* Example: sq_rel(RANK_2, WHITE) = RANK_2, sq_rel(RANK_6, BLACK) = RANK_3
|
||||
*
|
||||
* @return: Relative rank.
|
||||
*/
|
||||
enum {
|
||||
BB_ALL = 0, /* OR of all bitboards */
|
||||
BB_UNUSED, /* future use ? */
|
||||
BB_PAWN = E_PAWN,
|
||||
BB_KNIGHT,
|
||||
BB_BISHOP,
|
||||
BB_ROOK,
|
||||
BB_QUEEN,
|
||||
BB_KING,
|
||||
BB_END
|
||||
};
|
||||
|
||||
/* piece bitmask in piece_t
|
||||
*/
|
||||
enum {
|
||||
EMPTY = 0,
|
||||
PAWN = 1 << (E_PAWN - 1), /* 1<<(2-1) = 0x02 00000010 */
|
||||
KNIGHT = 1 << (E_KNIGHT - 1), /* 0x04 00000100 */
|
||||
BISHOP = 1 << (E_BISHOP - 1), /* 0x08 00001000 */
|
||||
ROOK = 1 << (E_ROOK - 1), /* 0x10 00010000 */
|
||||
QUEEN = 1 << (E_QUEEN - 1), /* 0x20 00100000 */
|
||||
KING = 1 << (E_KING - 1), /* 0x40 01000000 */
|
||||
};
|
||||
|
||||
#define PIECETOBB(p) (ffs64(PIECE(p))) /* from piece_t to bb piece array */
|
||||
|
||||
#define WHITE 0 /* 0x00 00000000 */
|
||||
#define BLACK 1 /* 0x01 00000001 */
|
||||
#define OPPONENT(p) !(p)
|
||||
|
||||
#define MASK_COLOR 0x01 /* 00000001 */
|
||||
#define MASK_PIECE 0x7E /* 01111110 */
|
||||
|
||||
#define COLOR(p) ((p) & MASK_COLOR) /* bitmask */
|
||||
#define PIECE(p) ((p) & MASK_PIECE)
|
||||
#define E_PIECE(p) (ffs64(PIECE(p))) /* convert mask to E_XX */
|
||||
|
||||
#define IS_WHITE(p) (!COLOR(p))
|
||||
#define IS_BLACK(p) (COLOR(p))
|
||||
|
||||
#define SET_WHITE(p) ((p) &= ~MASK_COLOR)
|
||||
#define SET_BLACK(p) ((p) |= MASK_COLOR)
|
||||
#define SET_COLOR(p, c) (!(c)? SET_WHITE(p): SET_BLACK(p))
|
||||
|
||||
/* flip a 0-63 square:
|
||||
* Vertical: G8 (62) becomes G1 (6)
|
||||
* Horizontal: G8 (62) becomes B8 (57)
|
||||
*/
|
||||
#define FLIP_V(sq) ((sq) ^ 56)
|
||||
#define FLIP_H(sq) ((sq) ^ 7)
|
||||
/* square_t bits structure : rrrrffff
|
||||
* ffff: file
|
||||
* rrrr: rank
|
||||
*/
|
||||
typedef unsigned char square_t;
|
||||
#define sq_rel_rank(rank, c) ((rank_t)((7 * (c)) ^ rank))
|
||||
#define sq_rel_file(file, c) ((file_t)((7 * (c)) ^ file))
|
||||
|
||||
/* castle_t bits structure
|
||||
*/
|
||||
typedef unsigned char castle_t;
|
||||
typedef enum {
|
||||
CASTLE_NONE = 0,
|
||||
CASTLE_WK = (1 << 0), /* 0001 */
|
||||
CASTLE_WQ = (1 << 1), /* 0010 */
|
||||
CASTLE_BK = (1 << 2), /* 0100 */
|
||||
CASTLE_BQ = (1 << 3), /* 1000 */
|
||||
|
||||
#define CASTLE_WK (1 << 0) /* 0x01 00000001 */
|
||||
#define CASTLE_WQ (1 << 1) /* 0x02 00000010 */
|
||||
#define CASTLE_BK (1 << 2) /* 0x04 00000100 */
|
||||
#define CASTLE_BQ (1 << 3) /* 0x08 00001000 */
|
||||
CASTLE_W = (CASTLE_WK | CASTLE_WQ), /* 0011 = 3 = W castle mask */
|
||||
CASTLE_B = (CASTLE_BK | CASTLE_BQ), /* 1100 = 12 = B castle mask */
|
||||
CASTLE_ALL = (CASTLE_W | CASTLE_B), /* 1111 = 15 */
|
||||
|
||||
#define CASTLE_W (CASTLE_WK | CASTLE_WQ) /* 00000011 W castle mask */
|
||||
#define CASTLE_B (CASTLE_BK | CASTLE_BQ) /* 00001100 B castle mask */
|
||||
CASTLE_K = (1 << 0), /* generic K/Q, bits 0 and 1 */
|
||||
CASTLE_Q = (1 << 1),
|
||||
CASTLE_KQ = (CASTLE_K |CASTLE_Q),
|
||||
} castle_rights_t;
|
||||
|
||||
/* game phases
|
||||
/* determine is oo or ooo is possible with castle flags f and color c
|
||||
*/
|
||||
#define OPENING 0
|
||||
#define MIDDLEGAME 1
|
||||
#define ENDGAME 2
|
||||
//#define NORM_CASTLE(f, c) ((f) >> (2 * (c))) /* shift flags to bits 0/1 */
|
||||
//#define
|
||||
//(NORM_CASTLE(f, c) & CASTLE_Q)
|
||||
#define can_oo(f, c) ((f) & (CASTLE_K << ((c) * 2)))
|
||||
#define can_ooo(f, c) ((f) & (CASTLE_Q << ((c) * 2)))
|
||||
#define can_castle(f, c) ((f) & (CASTLE_KQ << ((c) * 2)))
|
||||
|
||||
/* bitboard
|
||||
#define clr_oo(f, c) ((f) & ~(CASTLE_K << (2 * (c))))
|
||||
#define clr_ooo(f, c) ((f) & ~(CASTLE_Q << (2 * (c))))
|
||||
#define clr_castle(f, c) ((f) & ~(CASTLE_KQ << (2 * (c)) ))
|
||||
|
||||
/* forward defs */
|
||||
typedef struct __pos_s pos_t;
|
||||
typedef struct __movelist_s movelist_t;
|
||||
|
||||
/* basic types
|
||||
*/
|
||||
typedef u64 bitboard_t;
|
||||
typedef s16 eval_t;
|
||||
typedef s8 phase_t;
|
||||
|
||||
|
||||
/* eval type
|
||||
/* forward enum definition is impossible in C11.
|
||||
* To simplify cross-dependancies, all important enum are moved here.
|
||||
*/
|
||||
typedef s32 eval_t;
|
||||
|
||||
/* forward typedefs
|
||||
enum {
|
||||
//_SSQUARE_ = -1, /* force signed enum */
|
||||
A1 = 0, B1, C1, D1, E1, F1, G1, H1,
|
||||
A2, B2, C2, D2, E2, F2, G2, H2,
|
||||
A3, B3, C3, D3, E3, F3, G3, H3,
|
||||
A4, B4, C4, D4, E4, F4, G4, H4,
|
||||
A5, B5, C5, D5, E5, F5, G5, H5,
|
||||
A6, B6, C6, D6, E6, F6, G6, H6,
|
||||
A7, B7, C7, D7, E7, F7, G7, H7,
|
||||
A8, B8, C8, D8, E8, F8, G8, H8,
|
||||
SQUARE_NB = 64,
|
||||
SQUARE_NONE = 64
|
||||
};
|
||||
typedef u8 square_t;
|
||||
|
||||
enum {
|
||||
//_SFILE_ = -1, /* force signed enum */
|
||||
FILE_A = 0, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H,
|
||||
FILE_NB,
|
||||
};
|
||||
typedef u8 file_t;
|
||||
|
||||
enum {
|
||||
//_SRANK_ = -1, /* force signed enum */
|
||||
RANK_1 = 0, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8,
|
||||
RANK_NB,
|
||||
};
|
||||
typedef u8 rank_t;
|
||||
|
||||
enum {
|
||||
NORTH = 8,
|
||||
EAST = 1,
|
||||
SOUTH = -NORTH,
|
||||
WEST = -EAST,
|
||||
|
||||
NORTH_EAST = (NORTH + EAST),
|
||||
SOUTH_EAST = (SOUTH + EAST),
|
||||
SOUTH_WEST = (SOUTH + WEST),
|
||||
NORTH_WEST = (NORTH + WEST),
|
||||
};
|
||||
typedef u8 dir_t;
|
||||
|
||||
/* define diff for relative squares */
|
||||
#define sq_up(c) ((c) == WHITE ? NORTH: SOUTH)
|
||||
/* Attention here: We mix "up" (color dependent) and W/E, color independant.
|
||||
*/
|
||||
typedef struct piece_list_s piece_list_t;
|
||||
typedef struct board_s board_t;
|
||||
typedef struct pos_s pos_t;
|
||||
typedef struct move_s move_t;
|
||||
#define sq_upwest(up) ((up) - 1)
|
||||
#define sq_upeast(up) ((up) + 1)
|
||||
|
||||
#endif
|
||||
void init_all(void);
|
||||
|
||||
#endif /* _CHESSDEFS_H */
|
||||
|
||||
599
src/eval-defs.c
Normal file
599
src/eval-defs.c
Normal file
@@ -0,0 +1,599 @@
|
||||
/* eval-values.c - eval parameters
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <brlib.h>
|
||||
#include <bitops.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "position.h"
|
||||
#include "piece.h"
|
||||
#include "eval-defs.h"
|
||||
#include "util.h"
|
||||
//#include "eval-simple.h"
|
||||
//#include "eval.h"
|
||||
|
||||
phase_t piece_phase[PIECE_TYPE_NB] = {
|
||||
0, P_PHASE, N_PHASE, B_PHASE, R_PHASE, Q_PHASE, 0
|
||||
};
|
||||
|
||||
/* eval parameters definition. */
|
||||
static const struct ev_params ev_param_def [EV_PARAMS_NB] = {
|
||||
/* type setable def min max name */
|
||||
[WT_MAT] = { PAR_SPN, true, 100, 0, 400, "material weight" },
|
||||
[WT_PST] = { PAR_SPN, true, 100, 0, 400, "pst weight" },
|
||||
[TST_CHK] = { PAR_CHK, true, 1, 0, 0, "test check" },
|
||||
[TST_SPN] = { PAR_BTN, true, 0, 0, 0, "test button" },
|
||||
|
||||
};
|
||||
|
||||
void param_init()
|
||||
{
|
||||
for (int i = 0; i < EV_PARAMS_NB; ++i)
|
||||
parameters[i] = ev_param_def[i].init;
|
||||
}
|
||||
|
||||
int param_find_name(char *name)
|
||||
{
|
||||
for (int i = 0; i < EV_PARAMS_NB; ++i)
|
||||
if (str_eq_case(ev_param_def[i].name, name))
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
char *param_name(const int num)
|
||||
{
|
||||
return ev_param_def[num].name;
|
||||
}
|
||||
|
||||
eval_t param_default(const int num)
|
||||
{
|
||||
return ev_param_def[num].init;
|
||||
}
|
||||
eval_t param_min(const int num)
|
||||
{
|
||||
return ev_param_def[num].min;
|
||||
}
|
||||
eval_t param_max(const int num)
|
||||
{
|
||||
return ev_param_def[num].max;
|
||||
}
|
||||
bool param_setable(const int num)
|
||||
{
|
||||
return ev_param_def[num].setable;
|
||||
}
|
||||
int param_type(const int num)
|
||||
{
|
||||
return ev_param_def[num].type;
|
||||
}
|
||||
|
||||
/* parameters in use */
|
||||
eval_t parameters[EV_PARAMS_NB];
|
||||
void param_set(int num, eval_t val)
|
||||
{
|
||||
if (num >= 0 && num < EV_PARAMS_NB &&
|
||||
val >= param_min(num) && val <= param_max(num)) {
|
||||
parameters[num] = val;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Piece-square tables. For easier reading, they are defined for black side:
|
||||
*
|
||||
* {
|
||||
* A8 .... H8
|
||||
* ..........
|
||||
* ..........
|
||||
* A1 .... H1
|
||||
* }
|
||||
*/
|
||||
static const struct pst {
|
||||
char *name; /* one word only, no spaces */
|
||||
int val[PIECE_TYPE_NB][PHASE_NB][SQUARE_NB]; /* MG then EG */
|
||||
} pst_defs[PST_NB] = {
|
||||
[PST_ROFCHADE] = {
|
||||
/*
|
||||
* rofchade:
|
||||
* https://www.talkchess.com/forum3/viewtopic.php?f=2&t=68311&start=19
|
||||
*/
|
||||
"Rofchade",
|
||||
{
|
||||
/* A8 ..... H8
|
||||
* ...........
|
||||
* A1 ..... H1
|
||||
*/
|
||||
[PAWN] = {
|
||||
{ /* midgame */
|
||||
+ 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
+98, 134, 61, 95, 68, 126, 34, -11,
|
||||
- 6, 7, 26, 31, 65, 56, 25, -20,
|
||||
-14, 13, 6, 21, 23, 12, 17, -23,
|
||||
-27, -2, -5, 12, 17, 6, 10, -25,
|
||||
-26, -4, -4, -10, 3, 3, 33, -12,
|
||||
-35, -1, -20, -23, -15, 24, 38, -22,
|
||||
+ 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
},
|
||||
{ /* endgame */
|
||||
+ 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
178, 173, 158, 134, 147, 132, 165, 187,
|
||||
+94, 100, 85, 67, 56, 53, 82, 84,
|
||||
+32, 24, 13, 5, -2, 4, 17, 17,
|
||||
+13, 9, -3, -7, -7, -8, 3, -1,
|
||||
+ 4, 7, -6, 1, 0, -5, -1, -8,
|
||||
+13, 8, 8, 10, 13, 0, 2, -7,
|
||||
+ 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
}
|
||||
},
|
||||
[KNIGHT] = {
|
||||
{
|
||||
/* midgame */
|
||||
-167, -89, -34, -49, 61, -97, -15, -107,
|
||||
-73, -41, 72, 36, 23, 62, 7, -17,
|
||||
-47, 60, 37, 65, 84, 129, 73, 44,
|
||||
-9, 17, 19, 53, 37, 69, 18, 22,
|
||||
-13, 4, 16, 13, 28, 19, 21, -8,
|
||||
-23, -9, 12, 10, 19, 17, 25, -16,
|
||||
-29, -53, -12, -3, -1, 18, -14, -19,
|
||||
-105, -21, -58, -33, -17, -28, -19, -23,
|
||||
},
|
||||
{ /* endgame */
|
||||
-58, -38, -13, -28, -31, -27, -63, -99,
|
||||
-25, -8, -25, -2, -9, -25, -24, -52,
|
||||
-24, -20, 10, 9, -1, -9, -19, -41,
|
||||
-17, 3, 22, 22, 22, 11, 8, -18,
|
||||
-18, -6, 16, 25, 16, 17, 4, -18,
|
||||
-23, -3, -1, 15, 10, -3, -20, -22,
|
||||
-42, -20, -10, -5, -2, -20, -23, -44,
|
||||
-29, -51, -23, -15, -22, -18, -50, -64,
|
||||
}
|
||||
},
|
||||
[BISHOP] = {
|
||||
{ /* midgame */
|
||||
-29, 4, -82, -37, -25, -42, 7, -8,
|
||||
-26, 16, -18, -13, 30, 59, 18, -47,
|
||||
-16, 37, 43, 40, 35, 50, 37, -2,
|
||||
- 4, 5, 19, 50, 37, 37, 7, -2,
|
||||
- 6, 13, 13, 26, 34, 12, 10, 4,
|
||||
+ 0, 15, 15, 15, 14, 27, 18, 10,
|
||||
+ 4, 15, 16, 0, 7, 21, 33, 1,
|
||||
-33, -3, -14, -21, -13, -12, -39, -21,
|
||||
},
|
||||
{ /* endgame */
|
||||
-14, -21, -11, -8, -7, -9, -17, -24,
|
||||
- 8, -4, 7, -12, -3, -13, -4, -14,
|
||||
+ 2, -8, 0, -1, -2, 6, 0, 4,
|
||||
- 3, 9, 12, 9, 14, 10, 3, 2,
|
||||
- 6, 3, 13, 19, 7, 10, -3, -9,
|
||||
-12, -3, 8, 10, 13, 3, -7, -15,
|
||||
-14, -18, -7, -1, 4, -9, -15, -27,
|
||||
-23, -9, -23, -5, -9, -16, -5, -17,
|
||||
},
|
||||
},
|
||||
[ROOK] = {
|
||||
{ /* midgame */
|
||||
+32, 42, 32, 51, 63, 9, 31, 43,
|
||||
+27, 32, 58, 62, 80, 67, 26, 44,
|
||||
- 5, 19, 26, 36, 17, 45, 61, 16,
|
||||
-24, -11, 7, 26, 24, 35, -8, -20,
|
||||
-36, -26, -12, -1, 9, -7, 6, -23,
|
||||
-45, -25, -16, -17, 3, 0, -5, -33,
|
||||
-44, -16, -20, -9, -1, 11, -6, -71,
|
||||
-19, -13, 1, 17, 16, 7, -37, -26,
|
||||
},
|
||||
{ /* endgame */
|
||||
13, 10, 18, 15, 12, 12, 8, 5,
|
||||
11, 13, 13, 11, -3, 3, 8, 3,
|
||||
+7, 7, 7, 5, 4, -3, -5, -3,
|
||||
+4, 3, 13, 1, 2, 1, -1, 2,
|
||||
+3, 5, 8, 4, -5, -6, -8, -11,
|
||||
-4, 0, -5, -1, -7, -12, -8, -16,
|
||||
-6, -6, 0, 2, -9, -9, -11, -3,
|
||||
-9, 2, 3, -1, -5, -13, 4, -20,
|
||||
},
|
||||
},
|
||||
[QUEEN] = {
|
||||
{ /* midgame */
|
||||
-28, 0, 29, 12, 59, 44, 43, 45,
|
||||
-24, -39, -5, 1, -16, 57, 28, 54,
|
||||
-13, -17, 7, 8, 29, 56, 47, 57,
|
||||
-27, -27, -16, -16, -1, 17, -2, 1,
|
||||
- 9, -26, -9, -10, -2, -4, 3, -3,
|
||||
-14, 2, -11, -2, -5, 2, 14, 5,
|
||||
-35, -8, 11, 2, 8, 15, -3, 1,
|
||||
- 1, -18, -9, 10, -15, -25, -31, -50,
|
||||
},
|
||||
{ /* endgame */
|
||||
- 9, 22, 22, 27, 27, 19, 10, 20,
|
||||
-17, 20, 32, 41, 58, 25, 30, 0,
|
||||
-20, 6, 9, 49, 47, 35, 19, 9,
|
||||
+ 3, 22, 24, 45, 57, 40, 57, 36,
|
||||
-18, 28, 19, 47, 31, 34, 39, 23,
|
||||
-16, -27, 15, 6, 9, 17, 10, 5,
|
||||
-22, -23, -30, -16, -16, -23, -36, -32,
|
||||
-33, -28, -22, -43, -5, -32, -20, -41,
|
||||
},
|
||||
},
|
||||
[KING] = { /* midgame */
|
||||
{
|
||||
-65, 23, 16, -15, -56, -34, 2, 13,
|
||||
+29, -1, -20, -7, -8, -4, -38, -29,
|
||||
- 9, 24, 2, -16, -20, 6, 22, -22,
|
||||
-17, -20, -12, -27, -30, -25, -14, -36,
|
||||
-49, -1, -27, -39, -46, -44, -33, -51,
|
||||
-14, -14, -22, -46, -44, -30, -15, -27,
|
||||
+ 1, 7, -8, -64, -43, -16, 9, 8,
|
||||
-15, 36, 12, -54, 8, -28, 24, 14,
|
||||
},
|
||||
{ /* endgame */
|
||||
-74, -35, -18, -18, -11, 15, 4, -17,
|
||||
-12, 17, 14, 17, 17, 38, 23, 11,
|
||||
+10, 17, 23, 15, 20, 45, 44, 13,
|
||||
- 8, 22, 24, 27, 26, 33, 26, 3,
|
||||
-18, -4, 21, 24, 27, 23, 9, -11,
|
||||
-19, -3, 11, 21, 23, 16, 7, -9,
|
||||
-27, -11, 4, 13, 14, 4, -5, -17,
|
||||
-53, -34, -21, -11, -28, -14, -24, -43
|
||||
}
|
||||
}
|
||||
}
|
||||
}, /* rofchade end */
|
||||
|
||||
[PST_CPW] = {
|
||||
/*
|
||||
* CPW:
|
||||
* https://www.chessprogramming.org/Simplified_Evaluation_Function
|
||||
* Note: ≠ https://github.com/nescitus/cpw-engine
|
||||
*/
|
||||
"CPW",
|
||||
{
|
||||
/* A8 ..... H8
|
||||
* ...........
|
||||
* A1 ..... H1
|
||||
*/
|
||||
[PAWN] = {
|
||||
{ /* H1 H8 */
|
||||
/* midgame */
|
||||
+0, 0, 0, 0, 0, 0, 0, 0,
|
||||
50, 50, 50, 50, 50, 50, 50, 50,
|
||||
10, 10, 20, 30, 30, 20, 10, 10,
|
||||
+5, 5, 10, 25, 25, 10, 5, 5,
|
||||
+0, 0, 0, 20, 20, 0, 0, 0,
|
||||
+5, -5, -10, 0, 0, -10, -5, 5,
|
||||
+5, 10, 10, -20, -20, 10, 10, 5,
|
||||
+0, 0, 0, 0, 0, 0, 0, 0,
|
||||
},
|
||||
{ /* endgame */
|
||||
+0, 0, 0, 0, 0, 0, 0, 0,
|
||||
50, 50, 50, 50, 50, 50, 50, 50,
|
||||
10, 10, 20, 30, 30, 20, 10, 10,
|
||||
+5, 5, 10, 25, 25, 10, 5, 5,
|
||||
+0, 0, 0, 20, 20, 0, 0, 0,
|
||||
+5, -5, -10, 0, 0, -10, -5, 5,
|
||||
+5, 10, 10, -20, -20, 10, 10, 5,
|
||||
+0, 0, 0, 0, 0, 0, 0, 0,
|
||||
},
|
||||
},
|
||||
[KNIGHT] = {
|
||||
{ /* midgame */
|
||||
-50, -40, -30, -30, -30, -30, -40, -50,
|
||||
-40, -20, 0, 0, 0, 0, -20, -40,
|
||||
-30, 0, 10, 15, 15, 10, 0, -30,
|
||||
-30, 5, 15, 20, 20, 15, 5, -30,
|
||||
-30, 0, 15, 20, 20, 15, 0, -30,
|
||||
-30, 5, 10, 15, 15, 10, 5, -30,
|
||||
-40, -20, 0, 5, 5, 0, -20, -40,
|
||||
-50, -40, -30, -30, -30, -30, -40, -50
|
||||
},
|
||||
{ /* endgame */
|
||||
-50, -40, -30, -30, -30, -30, -40, -50,
|
||||
-40, -20, 0, 0, 0, 0, -20, -40,
|
||||
-30, 0, 10, 15, 15, 10, 0, -30,
|
||||
-30, 5, 15, 20, 20, 15, 5, -30,
|
||||
-30, 0, 15, 20, 20, 15, 0, -30,
|
||||
-30, 5, 10, 15, 15, 10, 5, -30,
|
||||
-40, -20, 0, 5, 5, 0, -20, -40,
|
||||
-50, -40, -30, -30, -30, -30, -40, -50
|
||||
},
|
||||
},
|
||||
[BISHOP] = {
|
||||
{ /* midgame */
|
||||
-20, -10, -10, -10, -10, -10, -10, -20,
|
||||
-10, 0, 0, 0, 0, 0, 0, -10,
|
||||
-10, 0, 5, 10, 10, 5, 0, -10,
|
||||
-10, 5, 5, 10, 10, 5, 5, -10,
|
||||
-10, 0, 10, 10, 10, 10, 0, -10,
|
||||
-10, 10, 10, 10, 10, 10, 10, -10,
|
||||
-10, 5, 0, 0, 0, 0, 5, -10,
|
||||
-20, -10, -10, -10, -10, -10, -10, -20,
|
||||
},
|
||||
{ /* endgame */
|
||||
-20, -10, -10, -10, -10, -10, -10, -20,
|
||||
-10, 0, 0, 0, 0, 0, 0, -10,
|
||||
-10, 0, 5, 10, 10, 5, 0, -10,
|
||||
-10, 5, 5, 10, 10, 5, 5, -10,
|
||||
-10, 0, 10, 10, 10, 10, 0, -10,
|
||||
-10, 10, 10, 10, 10, 10, 10, -10,
|
||||
-10, 5, 0, 0, 0, 0, 5, -10,
|
||||
-20, -10, -10, -10, -10, -10, -10, -20,
|
||||
},
|
||||
},
|
||||
[ROOK] = {
|
||||
{ /* midgame */
|
||||
+0, 0, 0, 0, 0, 0, 0, 0,
|
||||
+5, 10, 10, 10, 10, 10, 10, 5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
+0, 0, 0, 5, 5, 0, 0, 0,
|
||||
},
|
||||
{ /* endgame */
|
||||
+0, 0, 0, 0, 0, 0, 0, 0,
|
||||
+5, 10, 10, 10, 10, 10, 10, 5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
+0, 0, 0, 5, 5, 0, 0, 0,
|
||||
},
|
||||
},
|
||||
[QUEEN] = {
|
||||
{ /* midgame */
|
||||
-20, -10, -10, -5, -5, -10, -10, -20,
|
||||
-10, 0, 0, 0, 0, 0, 0, -10,
|
||||
-10, 0, 5, 5, 5, 5, 0, -10,
|
||||
- 5, 0, 5, 5, 5, 5, 0, -5,
|
||||
+ 0, 0, 5, 5, 5, 5, 0, -5,
|
||||
-10, 5, 5, 5, 5, 5, 0, -10,
|
||||
-10, 0, 5, 0, 0, 0, 0, -10,
|
||||
-20, -10, -10, -5, -5, -10, -10, -20,
|
||||
},
|
||||
{ /* endgame */
|
||||
-20, -10, -10, -5, -5, -10, -10, -20,
|
||||
-10, 0, 0, 0, 0, 0, 0, -10,
|
||||
-10, 0, 5, 5, 5, 5, 0, -10,
|
||||
- 5, 0, 5, 5, 5, 5, 0, -5,
|
||||
+ 0, 0, 5, 5, 5, 5, 0, -5,
|
||||
-10, 5, 5, 5, 5, 5, 0, -10,
|
||||
-10, 0, 5, 0, 0, 0, 0, -10,
|
||||
-20, -10, -10, -5, -5, -10, -10, -20,
|
||||
},
|
||||
},
|
||||
[KING] = { /* midgame */
|
||||
{
|
||||
-30, -40, -40, -50, -50, -40, -40, -30,
|
||||
-30, -40, -40, -50, -50, -40, -40, -30,
|
||||
-30, -40, -40, -50, -50, -40, -40, -30,
|
||||
-30, -40, -40, -50, -50, -40, -40, -30,
|
||||
-20, -30, -30, -40, -40, -30, -30, -20,
|
||||
-10, -20, -20, -20, -20, -20, -20, -10,
|
||||
+20, 20, 0, 0, 0, 0, 20, 20,
|
||||
+20, 30, 10, 0, 0, 10, 30, 20,
|
||||
},
|
||||
{ /* endgame */
|
||||
-50, -40, -30, -20, -20, -30, -40, -50,
|
||||
-30, -20, -10, 0, 0, -10, -20, -30,
|
||||
-30, -10, 20, 30, 30, 20, -10, -30,
|
||||
-30, -10, 30, 40, 40, 30, -10, -30,
|
||||
-30, -10, 30, 40, 40, 30, -10, -30,
|
||||
-30, -10, 20, 30, 30, 20, -10, -30,
|
||||
-30, -30, 0, 0, 0, 0, -30, -30,
|
||||
-50, -30, -30, -30, -30, -30, -30, -50,
|
||||
}
|
||||
}
|
||||
}
|
||||
}, /* CPW end */
|
||||
|
||||
[PST_SJENG] = {
|
||||
/*
|
||||
* sjeng: https://github.com/gcp/sjeng
|
||||
* Rook and Queen from CPW.
|
||||
*/
|
||||
"Sjeng",
|
||||
{
|
||||
/* A8 ..... H8
|
||||
* ...........
|
||||
* A1 ..... H1
|
||||
*/
|
||||
[PAWN] = {
|
||||
{ /* midgame */
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
5, 10, 15, 20, 20, 15, 10, 5,
|
||||
4, 8, 12, 16, 16, 12, 8, 4,
|
||||
3, 6, 9, 14, 14, 9, 6, 3,
|
||||
2, 4, 6, 12, 12, 6, 4, 2,
|
||||
1, 2, 3, 10, 10, 3, 2, 1,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
},
|
||||
{ /* endgame */
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
5, 10, 15, 20, 20, 15, 10, 5,
|
||||
4, 8, 12, 16, 16, 12, 8, 4,
|
||||
3, 6, 9, 14, 14, 9, 6, 3,
|
||||
2, 4, 6, 12, 12, 6, 4, 2,
|
||||
1, 2, 3, 10, 10, 3, 2, 1,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
},
|
||||
},
|
||||
[KNIGHT] = {
|
||||
{ /* midgame */
|
||||
-20, -10, -10, -10, -10, -10, -10, -20,
|
||||
-10, 0, 0, 3, 3, 0, 0, -10,
|
||||
-10, 0, 5, 5, 5, 5, 0, -10,
|
||||
-10, 0, 5, 10, 10, 5, 0, -10,
|
||||
-10, 0, 5, 10, 10, 5, 0, -10,
|
||||
-10, 0, 5, 5, 5, 5, 0, -10,
|
||||
-10, 0, 0, 3, 3, 0, 0, -10,
|
||||
-20, -10, -10, -10, -10, -10, -10, -20,
|
||||
},
|
||||
{ /* endgame */
|
||||
-20, -10, -10, -10, -10, -10, -10, -20,
|
||||
-10, 0, 0, 3, 3, 0, 0, -10,
|
||||
-10, 0, 5, 5, 5, 5, 0, -10,
|
||||
-10, 0, 5, 10, 10, 5, 0, -10,
|
||||
-10, 0, 5, 10, 10, 5, 0, -10,
|
||||
-10, 0, 5, 5, 5, 5, 0, -10,
|
||||
-10, 0, 0, 3, 3, 0, 0, -10,
|
||||
-20, -10, -10, -10, -10, -10, -10, -20,
|
||||
},
|
||||
},
|
||||
[BISHOP] = {
|
||||
{ /* midgame */
|
||||
-2, -2, -2, -2, -2, -2, -2, -2,
|
||||
-2, 8, 5, 5, 5, 5, 8, -2,
|
||||
-2, 3, 3, 5, 5, 3, 3, -2,
|
||||
-2, 2, 5, 4, 4, 5, 2, -2,
|
||||
-2, 2, 5, 4, 4, 5, 2, -2,
|
||||
-2, 3, 3, 5, 5, 3, 3, -2,
|
||||
-2, 8, 5, 5, 5, 5, 8, -2,
|
||||
-2, -2, -2, -2, -2, -2, -2, -2,
|
||||
},
|
||||
{ /* endgame */
|
||||
-2, -2, -2, -2, -2, -2, -2, -2,
|
||||
-2, 8, 5, 5, 5, 5, 8, -2,
|
||||
-2, 3, 3, 5, 5, 3, 3, -2,
|
||||
-2, 2, 5, 4, 4, 5, 2, -2,
|
||||
-2, 2, 5, 4, 4, 5, 2, -2,
|
||||
-2, 3, 3, 5, 5, 3, 3, -2,
|
||||
-2, 8, 5, 5, 5, 5, 8, -2,
|
||||
-2, -2, -2, -2, -2, -2, -2, -2,
|
||||
},
|
||||
},
|
||||
[ROOK] = {
|
||||
{ /* midgame */
|
||||
+0, 0, 0, 0, 0, 0, 0, 0,
|
||||
+5, 10, 10, 10, 10, 10, 10, 5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
+0, 0, 0, 5, 5, 0, 0, 0,
|
||||
},
|
||||
{ /* endgame */
|
||||
+0, 0, 0, 0, 0, 0, 0, 0,
|
||||
+5, 10, 10, 10, 10, 10, 10, 5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
+0, 0, 0, 5, 5, 0, 0, 0,
|
||||
},
|
||||
},
|
||||
[QUEEN] = {
|
||||
{ /* midgame */
|
||||
-20, -10, -10, -5, -5, -10, -10, -20,
|
||||
-10, 0, 5, 0, 0, 0, 0, -10,
|
||||
-10, 5, 5, 5, 5, 5, 0, -10,
|
||||
+ 0, 0, 5, 5, 5, 5, 0, -5,
|
||||
-05, 0, 5, 5, 5, 5, 0, -5,
|
||||
-10, 0, 5, 5, 5, 5, 0, -10,
|
||||
-10, 0, 0, 0, 0, 0, 0, -10,
|
||||
-20, -10, -10, -5, -5, -10, -10, -20,
|
||||
},
|
||||
{ /* endgame */
|
||||
-20, -10, -10, -5, -5, -10, -10, -20,
|
||||
-10, 0, 5, 0, 0, 0, 0, -10,
|
||||
-10, 5, 5, 5, 5, 5, 0, -10,
|
||||
+ 0, 0, 5, 5, 5, 5, 0, -5,
|
||||
- 5, 0, 5, 5, 5, 5, 0, -5,
|
||||
-10, 0, 5, 5, 5, 5, 0, -10,
|
||||
-10, 0, 0, 0, 0, 0, 0, -10,
|
||||
-20, -10, -10, -5, -5, -10, -10, -20,
|
||||
},
|
||||
},
|
||||
[KING] = { /* midgame */
|
||||
{
|
||||
-55, -55, -89, -89, -89, -89, -55, -55,
|
||||
-34, -34, -55, -55, -55, -55, -34, -34,
|
||||
-21, -21, -34, -34, -34, -34, -21, -21,
|
||||
-13, -13, -21, -21, -21, -21, -13, -13,
|
||||
- 8, -8, -13, -13, -13, -13, -8, -8,
|
||||
- 5, -5, -8, -8, -8, -8, -5, -5,
|
||||
- 3, -5, -6, -6, -6, -6, -5, -3,
|
||||
+ 2, 14, 0, 0, 0, 9, 14, 2,
|
||||
},
|
||||
{ /* endgame */
|
||||
-5, -3, -1, 0, 0, -1, -3, -5,
|
||||
-3, 10, 10, 10, 10, 10, 10, -3,
|
||||
-1, 10, 25, 25, 25, 25, 10, -1,
|
||||
+0, 10, 25, 30, 30, 25, 10, 0,
|
||||
+0, 10, 25, 30, 30, 25, 10, 0,
|
||||
-1, 10, 25, 25, 25, 25, 10, -1,
|
||||
-3, 10, 10, 10, 10, 10, 10, -3,
|
||||
-5, -3, -1, 0, 0, -1, -3, -5,
|
||||
}
|
||||
}
|
||||
},
|
||||
}, /* sjeng end */
|
||||
};
|
||||
|
||||
int pst_current = PST_DEFAULT;
|
||||
eval_t pst_mg[COLOR_NB][PT_NB][SQUARE_NB];
|
||||
eval_t pst_eg[COLOR_NB][PT_NB][SQUARE_NB];
|
||||
|
||||
void pst_set(char *str)
|
||||
{
|
||||
pst_init(pst_find(str));
|
||||
}
|
||||
|
||||
int pst_find(char *str)
|
||||
{
|
||||
for (int i = 0; i < PST_NB; ++i) {
|
||||
if (str_eq_case(pst_defs[i].name, str))
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void pst_init(int set)
|
||||
{
|
||||
const struct pst *pst;
|
||||
|
||||
if (warn(set < 0 || set >= PST_NB, "wrong PST, defaulting to zero."))
|
||||
set = 0;
|
||||
|
||||
# ifdef DEBUG_EVAL2
|
||||
printf("initializing piece-square tables %d ('%s')\n", set, pst_defs[set].name);
|
||||
# endif
|
||||
|
||||
pst = pst_defs + set;
|
||||
pst_current = set;
|
||||
int wmat = param(WT_MAT);
|
||||
int wpst = param(WT_PST);
|
||||
for (piece_type_t pt = PAWN; pt < PT_NB; ++pt) {
|
||||
eval_t mid_pc = piece_midval(pt);
|
||||
eval_t end_pc = piece_endval(pt);
|
||||
for (square_t sq = 0; sq < SQUARE_NB; ++sq) {
|
||||
eval_t mid_pst = pst->val[MIDGAME][pt][sq];
|
||||
eval_t end_pst = pst->val[ENDGAME][pt][sq];
|
||||
|
||||
pst_mg[BLACK][pt][sq] = (mid_pc * wmat + mid_pst * wpst) / 100;
|
||||
pst_eg[BLACK][pt][sq] = (end_pc * wmat + end_pst * wpst) / 100;
|
||||
|
||||
pst_mg[WHITE][pt][FLIP_V(sq)] = pst_mg[BLACK][pt][sq];
|
||||
pst_eg[WHITE][pt][FLIP_V(sq)] = pst_eg[BLACK][pt][sq];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char *pst_name(int i)
|
||||
{
|
||||
if (i >= 0 && i < PST_NB)
|
||||
return pst_defs[i].name;
|
||||
return "undef";
|
||||
}
|
||||
125
src/eval-defs.h
Normal file
125
src/eval-defs.h
Normal file
@@ -0,0 +1,125 @@
|
||||
/* eval-defs.h - eval parameters
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef EVAL_DEFS_H
|
||||
#define EVAL_DEFS_H
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "piece.h"
|
||||
#include "eval.h"
|
||||
|
||||
/* game phases
|
||||
*/
|
||||
enum {
|
||||
MIDGAME,
|
||||
ENDGAME,
|
||||
PHASE_NB
|
||||
};
|
||||
|
||||
/* pieces weight in phase calculation.
|
||||
*/
|
||||
enum {
|
||||
P_PHASE = 0,
|
||||
N_PHASE = 1,
|
||||
B_PHASE = 1,
|
||||
R_PHASE = 2,
|
||||
Q_PHASE = 4,
|
||||
ALL_PHASE = P_PHASE*16 + N_PHASE*4 + B_PHASE*4 + R_PHASE*4 + Q_PHASE*2
|
||||
};
|
||||
extern phase_t piece_phase[PIECE_TYPE_NB];
|
||||
|
||||
static inline phase_t pt_phase(piece_type_t pt)
|
||||
{
|
||||
return piece_phase[pt];
|
||||
}
|
||||
|
||||
/* max pieces eval is 9*QUEEN_VALUE + 2*ROOK_VALUE + 2*BISHOP_VALUE
|
||||
* + 2*KNIGHT_VALUE which is (for a pawn valued at 100) well less than 15,000.
|
||||
*/
|
||||
#define EVAL_MAX (SHRT_MAX) /* 32767 */
|
||||
#define EVAL_MIN (-EVAL_MAX)
|
||||
#define EVAL_DRAW 0
|
||||
#define EVAL_INV EVAL_MIN
|
||||
|
||||
#define EVAL_MATE 30000
|
||||
|
||||
/* engine parameters */
|
||||
enum {
|
||||
WT_MAT,
|
||||
WT_PST,
|
||||
|
||||
TST_SPN,
|
||||
TST_CHK,
|
||||
|
||||
EV_PARAMS_NB
|
||||
};
|
||||
|
||||
enum {
|
||||
PAR_CHK,
|
||||
PAR_SPN,
|
||||
PAR_BTN,
|
||||
PAR_STR,
|
||||
};
|
||||
|
||||
/**
|
||||
* ev_params - parameters definition
|
||||
* @init: eval_t default value
|
||||
* @min, @max: eval_t min and max values
|
||||
* @type: variable type (PARAM_CHECK, etc...)
|
||||
* @setable: bool setable (proposed in UCI options)
|
||||
* @name: char * human readable name
|
||||
*/
|
||||
struct ev_params {
|
||||
int type;
|
||||
bool setable; /* true: proposed in UCI options */
|
||||
eval_t init, min, max;
|
||||
char *name;
|
||||
};
|
||||
void param_init(void);
|
||||
int param_find_name(char *name); /* find parameter number by name */
|
||||
char *param_name(const int num);
|
||||
|
||||
eval_t param_default(const int num); /* get default param value */
|
||||
eval_t param_min(const int num);
|
||||
eval_t param_max(const int num);
|
||||
bool param_setable(const int num);
|
||||
int param_type(const int num);
|
||||
|
||||
/* parameters in use */
|
||||
extern eval_t parameters[EV_PARAMS_NB];
|
||||
#define param(n) parameters[n]
|
||||
void param_set (int num, eval_t val);
|
||||
|
||||
/* PST data */
|
||||
/**
|
||||
* pst_defs - pre-defined piece-square tables.
|
||||
*/
|
||||
enum {
|
||||
PST_ROFCHADE,
|
||||
PST_CPW,
|
||||
PST_SJENG,
|
||||
PST_NB
|
||||
};
|
||||
|
||||
#define PST_DEFAULT PST_CPW
|
||||
extern int pst_current;
|
||||
extern eval_t pst_mg[COLOR_NB][PT_NB][SQUARE_NB];
|
||||
extern eval_t pst_eg[COLOR_NB][PT_NB][SQUARE_NB];
|
||||
|
||||
void pst_set(char *str);
|
||||
int pst_find(char *str);
|
||||
void pst_init(int pst);
|
||||
|
||||
char *pst_name(int i);
|
||||
|
||||
#endif /* EVAL_DEFS_H */
|
||||
@@ -11,189 +11,80 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <bits.h>
|
||||
#include <debug.h>
|
||||
#include <brlib.h>
|
||||
#include <bitops.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "piece.h"
|
||||
#include "eval-simple.h"
|
||||
#include "position.h"
|
||||
#include "eval-simple.h"
|
||||
#include "eval.h"
|
||||
|
||||
/*
|
||||
* Tables are from https://www.chessprogramming.org/Simplified_Evaluation_Function
|
||||
/**
|
||||
* eval_material() - eval position material
|
||||
* @pos: &position to evaluate
|
||||
*
|
||||
* Attention! Tables are black point of view (to be visually easier to read).
|
||||
* Basic material evaluation. Only midgame value is used.
|
||||
*
|
||||
* @return: the @pos material evaluation in centipawns
|
||||
*/
|
||||
|
||||
static int mg_pawn[] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
50, 50, 50, 50, 50, 50, 50, 50,
|
||||
10, 10, 20, 30, 30, 20, 10, 10,
|
||||
5, 5, 10, 25, 25, 10, 5, 5,
|
||||
0, 0, 0, 20, 20, 0, 0, 0,
|
||||
5, -5, -10, 0, 0, -10, -5, 5,
|
||||
5, 10, 10, -20, -20, 10, 10, 5,
|
||||
0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
static int mg_knight[] = {
|
||||
-50, -40, -30, -30, -30, -30, -40, -50,
|
||||
-40, -20, 0, 0, 0, 0, -20, -40,
|
||||
-30, 0, 10, 15, 15, 10, 0, -30,
|
||||
-30, 5, 15, 20, 20, 15, 5, -30,
|
||||
-30, 0, 15, 20, 20, 15, 0, -30,
|
||||
-30, 5, 10, 15, 15, 10, 5, -30,
|
||||
-40, -20, 0, 5, 5, 0, -20, -40,
|
||||
-50, -40, -30, -30, -30, -30, -40, -50
|
||||
};
|
||||
|
||||
static int mg_bishop[] = {
|
||||
-20, -10, -10, -10, -10, -10, -10, -20,
|
||||
-10, 0, 0, 0, 0, 0, 0, -10,
|
||||
-10, 0, 5, 10, 10, 5, 0, -10,
|
||||
-10, 5, 5, 10, 10, 5, 5, -10,
|
||||
-10, 0, 10, 10, 10, 10, 0, -10,
|
||||
-10, 10, 10, 10, 10, 10, 10, -10,
|
||||
-10, 5, 0, 0, 0, 0, 5, -10,
|
||||
-20, -10, -10, -10, -10, -10, -10, -20
|
||||
};
|
||||
|
||||
static int mg_rook[] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
5, 10, 10, 10, 10, 10, 10, 5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
0, 0, 0, 5, 5, 0, 0, 0
|
||||
};
|
||||
|
||||
static int mg_queen[] = {
|
||||
-20, -10, -10, -5, -5, -10, -10, -20,
|
||||
-10, 0, 0, 0, 0, 0, 0, -10,
|
||||
-10, 0, 5, 5, 5, 5, 0, -10,
|
||||
-5, 0, 5, 5, 5, 5, 0, -5,
|
||||
0, 0, 5, 5, 5, 5, 0, -5,
|
||||
-10, 5, 5, 5, 5, 5, 0, -10,
|
||||
-10, 0, 5, 0, 0, 0, 0, -10,
|
||||
-20, -10, -10, -5, -5, -10, -10, -20
|
||||
};
|
||||
|
||||
static int mg_king[] = {
|
||||
-30, -40, -40, -50, -50, -40, -40, -30,
|
||||
-30, -40, -40, -50, -50, -40, -40, -30,
|
||||
-30, -40, -40, -50, -50, -40, -40, -30,
|
||||
-30, -40, -40, -50, -50, -40, -40, -30,
|
||||
-20, -30, -30, -40, -40, -30, -30, -20,
|
||||
-10, -20, -20, -20, -20, -20, -20, -10,
|
||||
20, 20, 0, 0, 0, 0, 20, 20,
|
||||
20, 30, 10, 0, 0, 10, 30, 20
|
||||
};
|
||||
|
||||
static int eg_king[] = {
|
||||
-50, -40, -30, -20, -20, -30, -40, -50,
|
||||
-30, -20, -10, 0, 0, -10, -20, -30,
|
||||
-30, -10, 20, 30, 30, 20, -10, -30,
|
||||
-30, -10, 30, 40, 40, 30, -10, -30,
|
||||
-30, -10, 30, 40, 40, 30, -10, -30,
|
||||
-30, -10, 20, 30, 30, 20, -10, -30,
|
||||
-30, -30, 0, 0, 0, 0, -30, -30,
|
||||
-50, -30, -30, -30, -30, -30, -30, -50
|
||||
};
|
||||
|
||||
/* as pieces bitboard tables start at position 2; we make these tables
|
||||
* bigger.
|
||||
*/
|
||||
static int *mg_tables[] = {
|
||||
NULL,
|
||||
NULL,
|
||||
mg_pawn,
|
||||
mg_knight,
|
||||
mg_bishop,
|
||||
mg_rook,
|
||||
mg_queen,
|
||||
mg_king
|
||||
};
|
||||
|
||||
static int *eg_tables[] = {
|
||||
NULL,
|
||||
NULL,
|
||||
mg_pawn,
|
||||
mg_knight,
|
||||
mg_bishop,
|
||||
mg_rook,
|
||||
mg_queen,
|
||||
eg_king
|
||||
};
|
||||
|
||||
/* to flip vertically a square, we need to XOR it with 56
|
||||
*/
|
||||
static int mg_table[2][6 + 2][64];
|
||||
static int eg_table[2][6 + 2][64];
|
||||
|
||||
void eval_simple_init(void)
|
||||
eval_t eval_material(pos_t *pos)
|
||||
{
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(1, "initializing piece tables\n");
|
||||
# endif
|
||||
for (int piece = BB_PAWN; piece <= BB_KING; ++piece) {
|
||||
for (int square = 0; square < 64; ++square) {
|
||||
mg_table[WHITE][piece][square] = mg_tables[piece][FLIP_V(square)];
|
||||
eg_table[WHITE][piece][square] = eg_tables[piece][FLIP_V(square)];
|
||||
mg_table[BLACK][piece][square] = mg_tables[piece][square];
|
||||
eg_table[BLACK][piece][square] = eg_tables[piece][square];
|
||||
}
|
||||
eval_t val[COLOR_NB] = { 0 };
|
||||
|
||||
for (piece_type_t pt = PAWN; pt < KING; ++pt) {
|
||||
eval_t pval = piece_midval(pt);
|
||||
val[WHITE] += popcount64(pos->bb[WHITE][pt]) * pval;
|
||||
val[BLACK] += popcount64(pos->bb[BLACK][pt]) * pval;
|
||||
}
|
||||
# ifdef DEBUG_EVAL
|
||||
printf("material: w:%d b:%d\n", val[WHITE], val[BLACK]);
|
||||
# endif
|
||||
|
||||
return val[WHITE] - val[BLACK];
|
||||
}
|
||||
|
||||
/**
|
||||
* eval_simple() - simple and fast position evaluation
|
||||
* @pos: &position to evaluate
|
||||
*
|
||||
* This function is normally used only during initialization,
|
||||
* or when changing phase (middlegame <--> endgame), as the eval
|
||||
* will be done increntally when doing moves.
|
||||
*
|
||||
* @return: the @pos evaluation in centipawns
|
||||
*/
|
||||
eval_t eval_simple(pos_t *pos)
|
||||
{
|
||||
eval_t eval[2] = { 0, 0 };
|
||||
int eg = simple_is_endgame(pos);
|
||||
int (*gg)[6 + 2][64]= eg? eg_table: mg_table;
|
||||
eval_t eval[2] = { 0, 0 }, res;
|
||||
eval_t mg_eval[2], eg_eval[2];
|
||||
|
||||
pos->eval_simple_phase = ENDGAME;
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(5, "phase = %s.\n", eg? "endgame": "midgame");
|
||||
# endif
|
||||
int eg_weight = clamp((int) pos->phase, ALL_PHASE, 0);
|
||||
int mg_weight = ALL_PHASE - pos->phase;
|
||||
|
||||
for (int color = WHITE; color <= BLACK; ++color) {
|
||||
for (uint piece = PAWN; piece <= KING; piece <<= 1) {
|
||||
int bb = PIECETOBB(piece), cur;
|
||||
u64 _t;
|
||||
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(5, "p=%u bb=%d %s %s: count=%d val=%ld ", piece, bb, color? "black": "white",
|
||||
P_SYM(piece), popcount64(pos->bb[color][bb]),
|
||||
popcount64(pos->bb[color][bb]) * P_VALUE(piece));
|
||||
# endif
|
||||
|
||||
eval[color] += popcount64(pos->bb[color][bb]) * P_LETTER(piece);
|
||||
bit_for_each64_2(cur, _t, pos->bb[color][bb]) {
|
||||
# ifdef DEBUG_EVAL
|
||||
log(5, "sq=%d:%d ", cur, gg[color][bb][cur]);
|
||||
# endif
|
||||
eval[color] += gg[color][bb][cur];
|
||||
for (color_t color = WHITE; color < COLOR_NB; ++color) {
|
||||
mg_eval[color] = 0;
|
||||
eg_eval[color] = 0;
|
||||
for (piece_type_t pt = PAWN; pt < KING; pt++) {
|
||||
bitboard_t bb = pos->bb[color][pt];
|
||||
while (bb) {
|
||||
square_t sq = bb_next(&bb);
|
||||
mg_eval[color] += pst_mg[color][pt][sq];
|
||||
eg_eval[color] += pst_eg[color][pt][sq];
|
||||
}
|
||||
|
||||
# ifdef DEBUG_EVAL
|
||||
log(5, "\n");
|
||||
printf("c=%d pt=%d mg=%d eg=%d\n", color, pt,
|
||||
mg_eval[color], eg_eval[color]);
|
||||
# endif
|
||||
|
||||
}
|
||||
}
|
||||
eval[WHITE] = mg_eval[WHITE] * mg_weight + eg_eval[WHITE] * eg_weight;
|
||||
eval[BLACK] = mg_eval[BLACK] * mg_weight + eg_eval[BLACK] * eg_weight;
|
||||
res = (eval[WHITE] - eval[BLACK]) / ALL_PHASE;
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(2, "eval:%d white:%d black:%d\n", eval[WHITE] - eval[BLACK],
|
||||
eval[WHITE], eval[BLACK]);
|
||||
printf("phase:%d mg:%d/%d eg:%d/%d ev:%d/%d RES=%d\n",
|
||||
pos->phase, mg_eval[WHITE], mg_eval[BLACK],
|
||||
eg_eval[WHITE], eg_eval[BLACK],
|
||||
eval[WHITE], eval[BLACK], res
|
||||
);
|
||||
# endif
|
||||
|
||||
return eval[WHITE] - eval[BLACK];
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/* eval-simple.h - simple position evaluation.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -14,21 +14,22 @@
|
||||
#ifndef EVAL_SIMPLE_H
|
||||
#define EVAL_SIMPLE_H
|
||||
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "piece.h"
|
||||
#include "eval.h"
|
||||
|
||||
/* no queen on board */
|
||||
#define simple_no_queen(p, c) \
|
||||
( !(p)->bb[c][BB_QUEEN] )
|
||||
( !(p)->bb[c][QUEEN] )
|
||||
|
||||
#define simple_one_queen(p, c) \
|
||||
( popcount64((p)->bb[c][BB_QUEEN]) == 1 )
|
||||
( popcount64((p)->bb[c][QUEEN]) == 1 )
|
||||
|
||||
#define simple_no_rook(p, c) \
|
||||
(!(p)->bb[c][BB_ROOK])
|
||||
(!(p)->bb[c][ROOK])
|
||||
|
||||
#define simple_one_minor_piece(p, c) \
|
||||
(popcount64((p)->bb[c][BB_KNIGHT] | (p)->bb[c][BB_BISHOP]) == 1)
|
||||
(popcount64((p)->bb[c][KNIGHT] | (p)->bb[c][BISHOP]) == 1)
|
||||
|
||||
#define simple_is_endgame(p) \
|
||||
( (simple_no_queen(p, WHITE) || \
|
||||
@@ -41,7 +42,12 @@
|
||||
simple_no_rook(p, BLACK) && \
|
||||
simple_one_minor_piece(p, BLACK))) )
|
||||
|
||||
void eval_simple_init(void);
|
||||
//s16 calc_phase(pos_t *pos);
|
||||
//int eval_simple_find(char *str);
|
||||
//void eval_simple_set(int set);
|
||||
//void eval_simple_init(char *set);
|
||||
|
||||
eval_t eval_material(pos_t *pos);
|
||||
eval_t eval_simple(pos_t *pos);
|
||||
|
||||
#endif /* EVAL_SIMPLE_H */
|
||||
|
||||
183
src/eval.c
183
src/eval.c
@@ -1,6 +1,6 @@
|
||||
/* eval.c - static position evaluation.
|
||||
*
|
||||
* Copyright (C) 2021-2023 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -13,64 +13,133 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <list.h>
|
||||
#include <debug.h>
|
||||
#include <brlib.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "position.h"
|
||||
#include "eval.h"
|
||||
#include "eval-simple.h"
|
||||
#include "hist.h"
|
||||
|
||||
inline eval_t eval_material(pos_t *pos, bool color)
|
||||
/**
|
||||
* calc_phase - calculate position phase
|
||||
* @pos: &position
|
||||
*
|
||||
* This function should be calculated when a new position is setup, or as
|
||||
* a verification of an incremental one.
|
||||
* Note: phase is *not* clamped, to avoid update errors.
|
||||
*
|
||||
* @return: phase value
|
||||
*/
|
||||
phase_t calc_phase(pos_t *pos)
|
||||
{
|
||||
eval_t res = 0;
|
||||
int phase = ALL_PHASE;
|
||||
for (piece_type_t pt = PAWN; pt < KING; ++pt)
|
||||
phase -= piece_phase[pt] * popcount64(pos->bb[WHITE][pt] | pos->bb[BLACK][pt]);
|
||||
# ifdef DEBUG_PHASE
|
||||
printf("calc phase:%d\n", phase);
|
||||
# endif
|
||||
return phase;
|
||||
}
|
||||
|
||||
/* I need to do something about the king, if it can be potentially taken
|
||||
* if pseudo-moves include a pinned piece on King.
|
||||
*/
|
||||
for (uint piece = PAWN; piece < KING; piece <<= 1) {
|
||||
uint bb = PIECETOBB(piece);
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(2, "color=%u piece=%u bb=%u=%c count=%ul val=%ld\n",
|
||||
color, piece, bb, P_LETTER(piece), popcount64(pos->bb[color][bb]),
|
||||
P_VALUE(piece));
|
||||
# endif
|
||||
/* attention here */
|
||||
res += popcount64(pos->bb[color][bb]) * P_VALUE(piece);
|
||||
/**
|
||||
* phase_verify() - verify position phase.
|
||||
* @pos: &position
|
||||
*
|
||||
* Verify that position phase is correct (a full recalculation is performed).
|
||||
* This function is inactive if PHASE_VERIFY is not set.
|
||||
*
|
||||
* @return: True if phase key is OK, no return otherwise.
|
||||
*/
|
||||
#ifdef PHASE_VERIFY
|
||||
bool phase_verify(pos_t *pos)
|
||||
{
|
||||
phase_t verif = calc_phase(pos);
|
||||
if (pos->phase != verif) {
|
||||
warn(true, "warn phase=%d verif=%d\n", pos->phase, verif);
|
||||
hist_print(pos);
|
||||
bug_on(pos->phase != verif);
|
||||
}
|
||||
return res;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* calc_phase - calculate position phase
|
||||
* @pos: &position
|
||||
*
|
||||
* This function should be calculated when a new position is setup, or as
|
||||
* a verification of an incremental one.
|
||||
* phase is clamped between 0 (opening) and 24 (ending).
|
||||
*
|
||||
* @return:
|
||||
*/
|
||||
__unused static phase_t calc_phase2(pos_t *pos)
|
||||
{
|
||||
int phase = ALL_PHASE;
|
||||
phase -= P_PHASE * popcount64(pos->bb[WHITE][PAWN] | pos->bb[BLACK][PAWN]);
|
||||
phase -= N_PHASE * popcount64(pos->bb[WHITE][KNIGHT] | pos->bb[BLACK][KNIGHT]);
|
||||
phase -= B_PHASE * popcount64(pos->bb[WHITE][BISHOP] | pos->bb[BLACK][BISHOP]);
|
||||
phase -= R_PHASE * popcount64(pos->bb[WHITE][ROOK] | pos->bb[BLACK][ROOK]);
|
||||
phase -= Q_PHASE * popcount64(pos->bb[WHITE][QUEEN] | pos->bb[BLACK][QUEEN]);
|
||||
|
||||
phase = max(phase, 0);
|
||||
# ifdef DEBUG_EVAL
|
||||
printf("calc phase:%d verif=%d\n", phase, calc_phase(pos));
|
||||
# endif
|
||||
return phase;
|
||||
}
|
||||
|
||||
inline eval_t eval_mobility(pos_t *pos, bool color)
|
||||
{
|
||||
return pos->mobility[color];
|
||||
}
|
||||
|
||||
inline eval_t eval_square_control(pos_t *pos, bool color)
|
||||
{
|
||||
return popcount64(pos->controlled[color]);
|
||||
}
|
||||
|
||||
/*
|
||||
* inline eval_t eval_mobility(pos_t *pos, bool color)
|
||||
* {
|
||||
* return pos->mobility[color];
|
||||
* }
|
||||
*
|
||||
*
|
||||
* inline eval_t eval_square_control(pos_t *pos, bool color)
|
||||
* {
|
||||
* return popcount64(pos->controlled[color]);
|
||||
* }
|
||||
*/
|
||||
|
||||
/*
|
||||
* inline eval_t eval_mobility(pos_t *pos, bool color)
|
||||
* {
|
||||
* return pos->mobility[color];
|
||||
* }
|
||||
*
|
||||
*
|
||||
* inline eval_t eval_square_control(pos_t *pos, bool color)
|
||||
* {
|
||||
* return popcount64(pos->controlled[color]);
|
||||
* }
|
||||
*/
|
||||
|
||||
eval_t eval(pos_t *pos)
|
||||
{
|
||||
eval_t simple = 0, control[2] = {0};
|
||||
//eval_t simple = 0, control[2] = {0};
|
||||
eval_t simple = 0;
|
||||
|
||||
if (pos->eval != EVAL_INVALID)
|
||||
return pos->eval;
|
||||
//if (pos->eval != EVAL_INVALID)
|
||||
// return pos->eval;
|
||||
|
||||
/* 1) pieces value */
|
||||
//material[WHITE] = eval_material(pos, WHITE);
|
||||
//material[BLACK] = eval_material(pos, BLACK);
|
||||
simple = eval_simple(pos);
|
||||
|
||||
# ifdef DEBUG_EVAL
|
||||
# ifdef DEBUG_EVAL2
|
||||
log_f(2, "eval_simple=%d\n", simple);
|
||||
# endif
|
||||
|
||||
/* 2) square control: 10 square controls diff = 1 pawn */
|
||||
control[WHITE] = eval_square_control(pos, WHITE);
|
||||
control[BLACK] = eval_square_control(pos, BLACK);
|
||||
//control[WHITE] = eval_square_control(pos, WHITE);
|
||||
//control[BLACK] = eval_square_control(pos, BLACK);
|
||||
|
||||
# ifdef DEBUG_EVAL
|
||||
# ifdef DEBUG_EVAL2
|
||||
log_f(2, "square control: W:%d B:%d diff=%d\n",
|
||||
control[WHITE], control[BLACK],
|
||||
(control[WHITE] - control[BLACK]) * 10);
|
||||
@@ -78,52 +147,18 @@ eval_t eval(pos_t *pos)
|
||||
|
||||
/* 3) mobility: 10 mobility diff = 1 pawn
|
||||
*/
|
||||
# ifdef DEBUG_EVAL
|
||||
# ifdef DEBUG_EVAL2
|
||||
log_f(2, "mobility: W:%u B:%u diff=%d\n",
|
||||
pos->mobility[WHITE], pos->mobility[BLACK],
|
||||
(pos->mobility[WHITE] - pos->mobility[BLACK]) * 10);
|
||||
# endif
|
||||
|
||||
eval_t res = simple +
|
||||
(control[WHITE] - control[BLACK]) * 10 +
|
||||
(pos->mobility[WHITE] - pos->mobility[BLACK]) * 10;
|
||||
# ifdef DEBUG_EVAL
|
||||
//eval_t res = simple +
|
||||
// (control[WHITE] - control[BLACK]) * 10 +
|
||||
// (pos->mobility[WHITE] - pos->mobility[BLACK]) * 10;
|
||||
# ifdef DEBUG_EVAL2
|
||||
log_f(2, "eval: %d\n", res);
|
||||
# endif
|
||||
pos->eval = res;
|
||||
return res;
|
||||
pos->eval = simple;
|
||||
return simple;
|
||||
}
|
||||
|
||||
#ifdef BIN_eval
|
||||
#include "fen.h"
|
||||
#include "move.h"
|
||||
|
||||
int main(int ac, char**av)
|
||||
{
|
||||
pos_t *pos;
|
||||
eval_t res;
|
||||
|
||||
debug_init(5, stderr, true);
|
||||
|
||||
piece_pool_init();
|
||||
moves_pool_init();
|
||||
pos_pool_init();
|
||||
pos = pos_get();
|
||||
|
||||
if (ac == 1) {
|
||||
pos_startpos(pos);
|
||||
} else {
|
||||
fen2pos(pos, av[1]);
|
||||
}
|
||||
|
||||
pos_print(pos);
|
||||
pos_pieces_print(pos);
|
||||
|
||||
moves_gen_all(pos);
|
||||
|
||||
pos_print(pos);
|
||||
moves_print(pos, M_PR_SEPARATE);
|
||||
res = eval(pos);
|
||||
printf("eval=%d centipawns)\n", res);
|
||||
}
|
||||
#endif
|
||||
|
||||
19
src/eval.h
19
src/eval.h
@@ -1,6 +1,6 @@
|
||||
/* eval.h - static position evaluation.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -17,18 +17,15 @@
|
||||
#include <limits.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "piece.h"
|
||||
#include "eval-defs.h"
|
||||
|
||||
/* max pieces eval is KING_VALUE + 9*QUEEN_VALUE + 2*ROOK_VALUE + 2*BISHOP_VALUE
|
||||
* + 2*KNIGHT_VALUE which around 30000.
|
||||
* We are on secure sire with -50000/+50000
|
||||
*/
|
||||
phase_t calc_phase(pos_t *pos);
|
||||
#ifdef PHASE_VERIFY
|
||||
bool phase_verify(pos_t *pos);
|
||||
#else
|
||||
#define phase_verify(p) true
|
||||
#endif
|
||||
|
||||
#define EVAL_MAX (50000)
|
||||
#define EVAL_MIN (-EVAL_MAX)
|
||||
#define EVAL_INVALID INT_MIN
|
||||
|
||||
eval_t eval_material(pos_t *pos, bool color);
|
||||
eval_t eval_mobility(pos_t *pos, bool color);
|
||||
eval_t eval_square_control(pos_t *pos, bool color);
|
||||
|
||||
|
||||
443
src/fen.c
443
src/fen.c
@@ -1,6 +1,6 @@
|
||||
/* fen.c - fen notation.
|
||||
/* fen.c - fen parsing/generation/test.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -18,176 +18,339 @@
|
||||
#include <ctype.h>
|
||||
|
||||
#include <debug.h>
|
||||
#include <bug.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "alloc.h"
|
||||
#include "position.h"
|
||||
#include "board.h"
|
||||
#include "eval-defs.h"
|
||||
#include "fen.h"
|
||||
#include "piece.h"
|
||||
|
||||
/* Starting Position :
|
||||
* rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
|
||||
* After 1.e4 :
|
||||
* rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1
|
||||
* After 1... c5 :
|
||||
* rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2
|
||||
* After 2. Nf3:
|
||||
* rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2
|
||||
*
|
||||
* 1 : White uppercase
|
||||
/* FEN description:
|
||||
* 1 : pieces on board (no space allowed):
|
||||
* - rank 8 first, '/' between ranks
|
||||
* - piece is usual piece notation(PNBRQK), black lowercase.
|
||||
* - empty: number of consecutive empty squares (digit)
|
||||
* 2 : next move (w or b)
|
||||
* 3 : Castling capabilities: "-" if none, KQ/kq if white/black can castle
|
||||
* on K or Q side
|
||||
* 4 : en-passant: if pawn just moved 2 squares, indicate target square (e.g.
|
||||
* for e2-e4 this field is e3)
|
||||
* 4 : en-passant: "-" if none. If pawn just moved 2 squares, indicate target
|
||||
* en-passant square (e.g. for e2-e4 this field is e3)
|
||||
* 5 : half moves since last capture or pawn advance (for 50 moves rule)
|
||||
* 6 : full moves, starts at 1, increments after black move
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* starting position:
|
||||
* rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
|
||||
* after 1.e4 e6 2.e5 d5
|
||||
* rnbqkbnr/ppp2ppp/4p3/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 3
|
||||
* 3.Nc3 Nc6 4.Rb1 Rb8 5.Nf3 h5 6.Be2
|
||||
* 1rbqkbnr/ppp2pp1/2n1p3/3pP2p/8/2N2N2/PPPPBPPP/1RBQK2R b Kk - 1 6
|
||||
* 6...Be7
|
||||
* 1rbqk1nr/ppp1bpp1/2n1p3/3pP2p/8/2N2N2/PPPPBPPP/1RBQK2R w Kk - 2 7
|
||||
* 7.Nxd5 h4 8.g4
|
||||
* 1rbqk1nr/ppp1bpp1/2n1p3/3NP3/6Pp/5N2/PPPPBP1P/1RBQK2R b Kk g3 0 8
|
||||
*/
|
||||
|
||||
// warning, we expect a valid fen input
|
||||
pos_t *fen2pos(pos_t *pos, char *fen)
|
||||
/* chess startup position FEN */
|
||||
const char *startfen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
static const char *castle_str = "KQkq";
|
||||
|
||||
#define SKIP_BLANK(p) for(;isspace(*(p)); (p)++)
|
||||
|
||||
/**
|
||||
* fen_ok() - test (and try to fix) fen-generated position.
|
||||
* @pos: &position
|
||||
* @fixit: action flag
|
||||
*
|
||||
* Test and possibly fix the following:
|
||||
* - inconsistent castle flags (if K & R are not in correct position)
|
||||
* - inconsistent en-passant square (turn, bad pawns positions)
|
||||
*
|
||||
* if @fixit is true, any error above will be fixed, and pos_ok() will
|
||||
is also called, leading to fatal errors if something is wrong.
|
||||
*
|
||||
* @return: 0 if OK, 1 if OK after fix, -1 if fatal issue.
|
||||
*/
|
||||
int fen_ok(pos_t *pos, bool fixit)
|
||||
{
|
||||
char *p = fen;
|
||||
short rank, file, skip, color, bbpiece;
|
||||
piece_t piece;
|
||||
board_t *board = pos->board;
|
||||
# define SKIP_BLANK(p) for(;*(p) == ' '; (p)++)
|
||||
char *colstr[2] = { "white", "black"};
|
||||
int warning = 0;
|
||||
color_t us = pos->turn, them = OPPONENT(us);
|
||||
int up = sq_up(us); /* pawn up */
|
||||
square_t ep = pos->en_passant;
|
||||
|
||||
pos_clear(pos);
|
||||
/* 1) get piece placement information
|
||||
*/
|
||||
for (rank = 7, file = 0; *p && *p != ' '; ++p) {
|
||||
color = isupper(*p)? WHITE: BLACK;
|
||||
char cp = toupper(*p);
|
||||
switch (cp) {
|
||||
case CHAR_PAWN:
|
||||
bbpiece = BB_PAWN;
|
||||
piece = PAWN;
|
||||
goto set_square;
|
||||
case CHAR_KNIGHT:
|
||||
bbpiece = BB_KNIGHT;
|
||||
piece = KNIGHT;
|
||||
goto set_square;
|
||||
case CHAR_BISHOP:
|
||||
bbpiece = BB_BISHOP;
|
||||
piece = BISHOP;
|
||||
goto set_square;
|
||||
case CHAR_ROOK:
|
||||
bbpiece = BB_ROOK;
|
||||
piece = ROOK;
|
||||
goto set_square;
|
||||
case CHAR_QUEEN:
|
||||
bbpiece = BB_QUEEN;
|
||||
piece = QUEEN;
|
||||
goto set_square;
|
||||
case CHAR_KING:
|
||||
bbpiece = BB_KING;
|
||||
piece = KING;
|
||||
//pos->bb[color][BB_KING] = BB(file, rank);
|
||||
//goto set_square;
|
||||
set_square:
|
||||
# ifdef DEBUG_FEN
|
||||
log_i(5, "f=%d r=%d *p=%c piece=%c color=%d\n",
|
||||
file, rank, *p, cp, color);
|
||||
# endif
|
||||
pos->bb[color][bbpiece] |= BB(file, rank);
|
||||
pos->occupied[color] |= BB(file, rank);
|
||||
SET_COLOR(piece, color);
|
||||
board[SQ88(file, rank)].piece = piece;
|
||||
board[SQ88(file, rank)].s_piece =
|
||||
piece_add(pos, piece, SQ88(file, rank));
|
||||
file++;
|
||||
break;
|
||||
case '/':
|
||||
rank--;
|
||||
file = 0;
|
||||
break;
|
||||
default:
|
||||
skip = cp - '0';
|
||||
while (skip--) {
|
||||
board[SQ88(file++, rank)].piece = EMPTY;
|
||||
}
|
||||
}
|
||||
}
|
||||
# ifdef DEBUG_FEN
|
||||
for (rank = 7; rank >= 0; --rank) {
|
||||
for (file = 0; file < 8; ++file) {
|
||||
log(5, "%02x ", board[SQ88(file, rank)].piece);
|
||||
}
|
||||
log(5, "\n");
|
||||
}
|
||||
# endif
|
||||
/* en passant, depends on who plays next */
|
||||
if (ep != SQUARE_NONE) {
|
||||
rank_t eprank = sq_rank(ep);
|
||||
rank_t rank6 = sq_rel_rank(RANK_6, us);
|
||||
piece_t pawn = MAKE_PIECE(PAWN, them);
|
||||
bitboard_t att = bb_pawn_attacks[them][ep] & pos->bb[us][PAWN];
|
||||
|
||||
/* 2) next move color
|
||||
*/
|
||||
SKIP_BLANK(p);
|
||||
SET_COLOR(pos->turn, *p == 'w' ? WHITE : BLACK);
|
||||
p++;
|
||||
if (eprank != rank6 ||
|
||||
pos->board[ep - up] != pawn ||
|
||||
pos->board[ep] != EMPTY ||
|
||||
pos->board[ep + up] != EMPTY ||
|
||||
att == 0ull) {
|
||||
|
||||
/* 3) castle status
|
||||
*/
|
||||
SKIP_BLANK(p);
|
||||
pos->castle = 0;
|
||||
if (*p != '-') {
|
||||
for (; *p && *p != ' '; ++p) {
|
||||
switch (*p) {
|
||||
case 'K':
|
||||
pos->castle |= CASTLE_WK;
|
||||
break;
|
||||
case 'k':
|
||||
pos->castle |= CASTLE_BK;
|
||||
break;
|
||||
case 'Q':
|
||||
pos->castle |= CASTLE_WQ;
|
||||
break;
|
||||
case 'q':
|
||||
pos->castle |= CASTLE_BQ;
|
||||
break;
|
||||
warning++;
|
||||
if (fixit) {
|
||||
warn(true, "pos warn: wrong en-passant settings (fixed).\n");
|
||||
pos->en_passant = SQUARE_NONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
p++;
|
||||
|
||||
for (int color = WHITE; color <= BLACK; ++color) {
|
||||
rank_t rank1 = sq_rel_rank(RANK_1, color);
|
||||
|
||||
/* castling */
|
||||
/* where K and R should be for valid castle flag */
|
||||
bitboard_t k = bb_sq[sq_make(FILE_E, rank1)];
|
||||
bitboard_t r_k = bb_sq[sq_make(FILE_H, rank1)];
|
||||
bitboard_t r_q = bb_sq[sq_make(FILE_A, rank1)];
|
||||
|
||||
/* where they are */
|
||||
bitboard_t kingbb = pos->bb[color][KING];
|
||||
bitboard_t rookbb = pos->bb[color][ROOK];
|
||||
castle_rights_t castle_k = color == WHITE? CASTLE_WK: CASTLE_BK;
|
||||
castle_rights_t castle_q = color == WHITE? CASTLE_WQ: CASTLE_BQ;
|
||||
if (pos->castle & castle_k && !(k & kingbb && r_k & rookbb)) {
|
||||
warning++;
|
||||
if (fixit) {
|
||||
warn(true, "fen warn: wrong %s short castling (fixed)\n",
|
||||
colstr[color]);
|
||||
pos->castle &= ~castle_k;
|
||||
}
|
||||
}
|
||||
if (pos->castle & castle_q && !(k & kingbb && r_q & rookbb)) {
|
||||
warning++;
|
||||
if (fixit) {
|
||||
warn(true, "fen warn: wrong %s long castling (fixed)\n",
|
||||
colstr[color]);
|
||||
pos->castle &= ~castle_q;
|
||||
}
|
||||
}
|
||||
}
|
||||
return warning;
|
||||
}
|
||||
|
||||
/**
|
||||
* startpos - create a game start position
|
||||
* @pos: a position pointer or NULL
|
||||
*
|
||||
* See @fen2pos function.
|
||||
*
|
||||
* @return: the pos position.
|
||||
*/
|
||||
pos_t *startpos(pos_t *pos)
|
||||
{
|
||||
return fen2pos(pos, startfen);
|
||||
}
|
||||
|
||||
/**
|
||||
* fen2pos - make a position from a fen string
|
||||
* @pos: a position pointer or NULL
|
||||
* @fen: a valid fen string
|
||||
*
|
||||
* If @pos is NULL, a position will be allocated with @pos_new(),
|
||||
* that should be freed by caller.
|
||||
*
|
||||
* @return: the pos position, or NULL if error.
|
||||
*/
|
||||
pos_t *fen2pos(pos_t *pos, const char *fen)
|
||||
{
|
||||
const char *cur = fen;
|
||||
char *p;
|
||||
short rank, file, tmp;
|
||||
piece_t piece;
|
||||
int consumed, err_line = 0, err_pos = 0, err_char = 0;
|
||||
pos_t tmppos;
|
||||
|
||||
pos_clear(&tmppos);
|
||||
/* 1) get piece placement information
|
||||
*/
|
||||
for (rank = 7, file = 0; *cur && !isspace(*cur); ++cur) {
|
||||
if (*cur == '/') { /* next rank */
|
||||
rank--;
|
||||
file = 0;
|
||||
continue;
|
||||
}
|
||||
if (isdigit(*cur)) { /* empty square(s) */
|
||||
file += *cur - '0';
|
||||
continue;
|
||||
}
|
||||
if ((piece = piece_from_fen(*cur)) != EMPTY) {
|
||||
# ifdef DEBUG_FEN
|
||||
printf("f=%d r=%d *p=%c piece=%#04x t=%d c=%d\n", file, rank, *cur,
|
||||
piece, PIECE(piece), COLOR(piece));
|
||||
# endif
|
||||
pos_set_sq(&tmppos, sq_make(file, rank), piece);
|
||||
if (PIECE(piece) == KING)
|
||||
tmppos.king[COLOR(piece)] = sq_make(file, rank);
|
||||
file++;
|
||||
} else { /* error */
|
||||
err_line = __LINE__, err_char = *cur, err_pos = cur - fen;
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
SKIP_BLANK(cur);
|
||||
|
||||
/* 2) next turn color
|
||||
*/
|
||||
tmppos.turn = *cur++ == 'w' ? WHITE : BLACK;
|
||||
SKIP_BLANK(cur);
|
||||
|
||||
/* 3) castle rights
|
||||
*/
|
||||
if (*cur == '-') {
|
||||
cur++;
|
||||
} else {
|
||||
for (; *cur && !isspace(*cur); ++cur) {
|
||||
if ((p = strchr(castle_str, *cur))) { /* valid castle letter */
|
||||
tmppos.castle |= 1 << (p - castle_str);
|
||||
} else {
|
||||
err_line = __LINE__, err_char = *cur, err_pos = cur - fen;
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
}
|
||||
SKIP_BLANK(cur);
|
||||
|
||||
/* 4) en passant
|
||||
*/
|
||||
SKIP_BLANK(p);
|
||||
pos->en_passant = 0;
|
||||
if (*p != '-') {
|
||||
//SET_F(pos->en_passant, C2FILE(*p++));
|
||||
//SET_R(pos->en_passant, C2RANK(*p++));
|
||||
pos->en_passant = SQ88(C2FILE(*p), C2RANK(*(p+1)));
|
||||
pos += 2;
|
||||
if (*cur == '-') {
|
||||
cur++;
|
||||
} else {
|
||||
p++;
|
||||
tmppos.en_passant = sq_from_string(cur);
|
||||
cur += 2;
|
||||
}
|
||||
SKIP_BLANK(cur);
|
||||
|
||||
/* 5) half moves since last capture or pawn move (50 moves rule)
|
||||
*/
|
||||
tmppos.ply50 = 0;
|
||||
tmppos.plycount = 1;
|
||||
if (sscanf(cur, "%hd%n", &tmp, &consumed) != 1)
|
||||
goto end; /* early end, ignore w/o err */
|
||||
|
||||
tmppos.ply50 = tmp;
|
||||
cur += consumed;
|
||||
SKIP_BLANK(cur);
|
||||
|
||||
/* 6) current full move number, starting with 1
|
||||
*/
|
||||
if (sscanf(cur, "%hd", &tmp) != 1)
|
||||
goto end;
|
||||
if (tmp <= 0) /* fix faulty numbers*/
|
||||
tmp = 1;
|
||||
tmp = 2 * (tmp - 1) + (tmppos.turn == BLACK); /* plies, +1 if black turn */
|
||||
tmppos.plycount = tmp;
|
||||
|
||||
end:
|
||||
if (warn(err_line, "FEN error line %d: charpos=%d char=%#x(%c)\n",
|
||||
err_line, err_pos, err_char, err_char)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* 5) half moves since last capture or pawn move and
|
||||
* 6) current move number
|
||||
*/
|
||||
SKIP_BLANK(p);
|
||||
//log_i(5, "pos=%d\n", (int)(p-fen));
|
||||
sscanf(p, "%hd %hd", &pos->clock_50, &pos->curmove);
|
||||
fen_ok(&tmppos, true); /* fix e.p & castling flags */
|
||||
if (!pos_ok(&tmppos, false))
|
||||
return NULL; /* invalid position: ignored */
|
||||
|
||||
tmppos.key = zobrist_calc(&tmppos);
|
||||
tmppos.phase = calc_phase(&tmppos);
|
||||
if (!pos)
|
||||
pos = pos_new();
|
||||
pos_copy(&tmppos, pos);
|
||||
# ifdef DEBUG_FEN
|
||||
log_i(5, "50 rule=%d current move=%d\n", pos->clock_50, pos->curmove);
|
||||
pos_print_raw(&tmppos, 1);
|
||||
# endif
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
#ifdef BIN_fen
|
||||
int main(int ac, char**av)
|
||||
/**
|
||||
* pos2fen - make a FEN string from a position.
|
||||
* @pos: a position pointer
|
||||
* @fen: destination FEN char*, or NULL
|
||||
*
|
||||
* If @fen is NULL, a 92 bytes memory will be allocated with malloc(1),
|
||||
* that should be freed by caller.
|
||||
*
|
||||
* Note: If @fen is given, no check is done on its length, but to
|
||||
* be on secure side, it should be at least 90 bytes. See:
|
||||
* https://chess.stackexchange.com/questions/30004/longest-possible-fen
|
||||
* For convenience, use FENSTRLEN.
|
||||
*
|
||||
* @return: the pos position, or NULL if error.
|
||||
*/
|
||||
char *pos2fen(const pos_t *pos, char *fen)
|
||||
{
|
||||
pos_t *pos;
|
||||
int cur = 0;
|
||||
|
||||
debug_init(5, stderr, true);
|
||||
piece_pool_init();
|
||||
pos_pool_init();
|
||||
pos = pos_get();
|
||||
if (ac == 1) {
|
||||
pos_startpos(pos);
|
||||
} else {
|
||||
fen2pos(pos, av[1]);
|
||||
if (!fen)
|
||||
fen = safe_alloc(92);
|
||||
|
||||
/* 1) position
|
||||
*/
|
||||
for (rank_t r = RANK_8 + 1; r --> RANK_1;) {
|
||||
for (file_t f = FILE_A; f <= FILE_H;) {
|
||||
square_t sq = sq_make(f, r);
|
||||
piece_t piece = pos->board[sq];
|
||||
# ifdef DEBUG_FEN
|
||||
printf("r=%d f=%d p=%d pos=%d\n", r, f, piece, cur);
|
||||
# endif
|
||||
if (piece == EMPTY) {
|
||||
int len = 0;
|
||||
for (; f <= FILE_H && pos->board[sq_make(f, r)] == EMPTY; f++)
|
||||
len++;
|
||||
# ifdef DEBUG_FEN
|
||||
printf("empty=%d char=%c\n", len, '0' + len);
|
||||
# endif
|
||||
fen[cur++] = '0' + len;
|
||||
} else {
|
||||
fen[cur++] = *piece_to_fen(piece);
|
||||
# ifdef DEBUG_FEN
|
||||
printf("f1=%d r=%d c=%c t=%d c=%d \n", f, r,
|
||||
*(piece_to_fen(piece)), PIECE(piece), COLOR(piece));
|
||||
# endif
|
||||
f++;
|
||||
}
|
||||
}
|
||||
fen[cur++] = r == RANK_1? ' ': '/';
|
||||
}
|
||||
pos_print(pos);
|
||||
|
||||
/* 2) next turn color
|
||||
*/
|
||||
fen[cur++] = pos->turn == WHITE? 'w': 'b';
|
||||
fen[cur++] = ' ';
|
||||
|
||||
/* 3) castle rights
|
||||
*/
|
||||
if (pos->castle == 0) {
|
||||
fen[cur++] = '-';
|
||||
} else {
|
||||
for (int i = 0; i < 4; ++i)
|
||||
if (pos->castle & BIT(i))
|
||||
fen[cur++] = castle_str[i];
|
||||
}
|
||||
fen[cur++] = ' ';
|
||||
|
||||
/* 4) en passant
|
||||
*/
|
||||
if (pos->en_passant == SQUARE_NONE) {
|
||||
fen[cur++] = '-';
|
||||
} else {
|
||||
strcpy(fen+cur, sq_to_string(pos->en_passant));
|
||||
cur += 2;
|
||||
}
|
||||
fen[cur++] = ' ';
|
||||
|
||||
/* 5) moves since last capture or pawn move (50 moves rule)
|
||||
* 6) current full move number, starting with 1
|
||||
*/
|
||||
sprintf(fen+cur, "%d %d", pos->ply50,
|
||||
1 + (pos->plycount - (pos->turn == BLACK)) / 2);
|
||||
return fen;
|
||||
}
|
||||
#endif
|
||||
|
||||
13
src/fen.h
13
src/fen.h
@@ -1,6 +1,6 @@
|
||||
/* fen.h - fen notation.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -14,8 +14,15 @@
|
||||
#ifndef FEN_H
|
||||
#define FEN_H
|
||||
|
||||
#include "position.h"
|
||||
#include "chessdefs.h"
|
||||
|
||||
pos_t *fen2pos(pos_t *pos, char *fen);
|
||||
#define FENSTRLEN 92 /* secure FEN string size */
|
||||
|
||||
extern const char *startfen; /* startup position */
|
||||
|
||||
extern int fen_ok(pos_t *pos, bool fixit);
|
||||
extern pos_t *startpos(pos_t *pos);
|
||||
extern pos_t *fen2pos(pos_t *pos, const char *fen);
|
||||
extern char *pos2fen(const pos_t *pos, char *fen);
|
||||
|
||||
#endif /* FEN_H */
|
||||
|
||||
412
src/hash.c
Normal file
412
src/hash.c
Normal file
@@ -0,0 +1,412 @@
|
||||
/* hash.c - hash management.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <brlib.h>
|
||||
#include <bitops.h>
|
||||
#include <bug.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "util.h"
|
||||
#include "alloc.h"
|
||||
#include "position.h"
|
||||
#include "piece.h"
|
||||
#include "hash.h"
|
||||
|
||||
u64 zobrist_pieces[16][64];
|
||||
u64 zobrist_castling[4 * 4 + 1];
|
||||
u64 zobrist_turn; /* for black, XOR each ply */
|
||||
u64 zobrist_ep[9]; /* 0-7: ep file, 8: SQUARE_NONE */
|
||||
|
||||
hasht_t hash_tt; /* main transposition table */
|
||||
|
||||
/**
|
||||
* zobrist_init() - initialize zobrist tables.
|
||||
*
|
||||
* Initialize all zobrist random bitmasks. Must be called before any other
|
||||
* zobrist function, and can be called once only (further calls will be ignored).
|
||||
*/
|
||||
void zobrist_init(void)
|
||||
{
|
||||
static bool called = false;
|
||||
if (!called) {
|
||||
called = true;
|
||||
for (color_t c = WHITE; c <= BLACK; ++c) {
|
||||
for (piece_type_t p = PAWN; p <= KING; ++p)
|
||||
for (square_t sq = A1; sq <= H8; ++sq)
|
||||
zobrist_pieces[MAKE_PIECE(p, c)][sq] = rand64();
|
||||
}
|
||||
for (castle_rights_t c = CASTLE_NONE; c <= CASTLE_ALL; ++c)
|
||||
zobrist_castling[c] = rand64();
|
||||
for (file_t f = FILE_A; f <= FILE_H; ++f)
|
||||
zobrist_ep[f] = rand64();
|
||||
zobrist_ep[8] = 0; /* see EP_ZOBRIST_IDX macro */
|
||||
zobrist_turn = rand64();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* zobrist_calc() - calculate a position zobrist hash.
|
||||
* @pos: &position
|
||||
*
|
||||
* Normally, Zobrist keys are incrementally calculated when doing or
|
||||
* undoing a move.
|
||||
* This function should normally only be called:
|
||||
* - When starting a new position
|
||||
* - To verify incremental Zobrist calculation is correct
|
||||
*
|
||||
* @return: @pos Zobrist key
|
||||
*/
|
||||
hkey_t zobrist_calc(pos_t *pos)
|
||||
{
|
||||
hkey_t key = 0;
|
||||
|
||||
if (pos->turn == BLACK)
|
||||
key ^= zobrist_turn;
|
||||
|
||||
for (color_t c = WHITE; c <= BLACK; ++c) {
|
||||
for (piece_type_t pt = PAWN; pt <= KING; ++pt) {
|
||||
piece_t piece = MAKE_PIECE(pt, c);
|
||||
bitboard_t bb = pos->bb[c][pt];
|
||||
while (bb) {
|
||||
square_t sq = bb_next(&bb);
|
||||
key ^= zobrist_pieces[piece][sq];
|
||||
}
|
||||
}
|
||||
}
|
||||
key ^= zobrist_castling[pos->castle];
|
||||
key ^= zobrist_ep[EP_ZOBRIST_IDX(pos->en_passant)];
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* zobrist_verify() - verify current position Zobrist key.
|
||||
* @pos: &position
|
||||
*
|
||||
* Verify that position Zobrist key matches a full Zobrist calculation.
|
||||
* This function is inactive if ZOBRIST_VERIFY is not set.
|
||||
*
|
||||
* @return: True if Zobrist key is OK, no return otherwise.
|
||||
*/
|
||||
#ifdef ZOBRIST_VERIFY
|
||||
bool zobrist_verify(pos_t *pos)
|
||||
{
|
||||
hkey_t diff, key = zobrist_calc(pos);
|
||||
if (pos->key == key)
|
||||
return true;
|
||||
|
||||
printf("key verify: cur=%#lx != %#lx\n", pos->key, key);
|
||||
|
||||
/* try to find-out the key in different zobrist tables */
|
||||
diff = pos->key ^ key;
|
||||
|
||||
for (color_t c = WHITE; c <= BLACK; ++c) {
|
||||
for (piece_type_t p = PAWN; p <= KING; ++p)
|
||||
for (square_t sq = A1; sq <= H8; ++sq)
|
||||
if (diff == zobrist_pieces[MAKE_PIECE(p, c)][sq]) {
|
||||
warn(true, "zobrist difference is piece:[%s][%s]\n",
|
||||
piece_to_fen(MAKE_PIECE(p, c)), sq_to_string(sq));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
for (castle_rights_t c = CASTLE_NONE; c <= CASTLE_ALL; ++c) {
|
||||
if (diff == zobrist_castling[c]) {
|
||||
warn(true, "zobrist difference is castling:[%d]\n", c);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
for (file_t f = FILE_A; f <= FILE_H; ++f) {
|
||||
if (diff == zobrist_ep[f]) {
|
||||
warn(true, "zobrist difference is ep:[%d]\n", f);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (diff == zobrist_turn) {
|
||||
warn(true, "zobrist difference is turn\n");
|
||||
goto end;
|
||||
}
|
||||
warn(true, "zobrist diff %lx is unknown\n", diff);
|
||||
end:
|
||||
bug_on_always(false);
|
||||
/* not reached */
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* tt_create() - create transposition table
|
||||
* @sizemb: s32 size of hash table in Mb
|
||||
*
|
||||
* Create a hash table of max @sizemb (or HASH_SIZE_MBif @sizemb <= 0) Mb size.
|
||||
* This function must be called at startup.
|
||||
*
|
||||
* The number of bucket_t entries fitting in @sizemb is calculated, and rounded
|
||||
* (down) to a power of 2.
|
||||
* This means the actual size could be lower than @sizemb (nearly halved in
|
||||
* worst case).
|
||||
*
|
||||
* If transposition hashtable already exists and new size would not change,
|
||||
* the old one is cleared.
|
||||
* If transposition hashtable already exists and new size is different,
|
||||
* the old one is destroyed first (old data is not preserved).
|
||||
*
|
||||
* TODO:
|
||||
* - Rebuild old hashtable data ?
|
||||
*
|
||||
* @return: hash table size in Mb. If memory allocation fails, the function does
|
||||
* not return.
|
||||
*/
|
||||
int tt_create(s32 sizemb)
|
||||
{
|
||||
size_t bytes, target_nbuckets;
|
||||
u32 nbits;
|
||||
|
||||
static_assert(sizeof(hentry_t) == 16, "fatal: hentry_t size != 16");
|
||||
|
||||
//printf("mb = %'7u ", sizemb);
|
||||
/* adjust tt size */
|
||||
if (sizemb <= 0)
|
||||
sizemb = HASH_SIZE_DEFAULT;
|
||||
sizemb = clamp(sizemb, HASH_SIZE_MIN, HASH_SIZE_MAX);
|
||||
|
||||
bytes = sizemb * 1024ull * 1024ull; /* bytes wanted */
|
||||
target_nbuckets = bytes / sizeof(bucket_t); /* target buckets */
|
||||
|
||||
nbits = msb64(target_nbuckets); /* adjust to power of 2 */
|
||||
|
||||
if (hash_tt.nbits != nbits) {
|
||||
if (hash_tt.keys)
|
||||
tt_delete();
|
||||
|
||||
hash_tt.nbits = nbits;
|
||||
|
||||
hash_tt.nbuckets = BIT(hash_tt.nbits);
|
||||
hash_tt.nkeys = hash_tt.nbuckets * ENTRIES_PER_BUCKET;
|
||||
|
||||
hash_tt.bytes = hash_tt.nbuckets * sizeof(bucket_t);
|
||||
hash_tt.mb = hash_tt.bytes / 1024 / 1024;
|
||||
|
||||
hash_tt.mask = -1ull >> (64 - nbits);
|
||||
|
||||
hash_tt.keys = safe_alloc_aligned_hugepage(hash_tt.bytes);
|
||||
|
||||
//printf("bits=%2d size=%'15lu/%'6d Mb/%'14lu buckets ",
|
||||
// hash_tt.nbits, hash_tt.bytes, hash_tt.mb, hash_tt.nbuckets);
|
||||
//printf("mask=%9x\n", hash_tt.mask);
|
||||
}
|
||||
//else {
|
||||
// printf("unchanged (cleared)\n");
|
||||
//}
|
||||
/* attention - may fail ! */
|
||||
tt_clear();
|
||||
|
||||
return hash_tt.nbits;
|
||||
}
|
||||
|
||||
/**
|
||||
* tt_clear() - clear transposition table
|
||||
*
|
||||
* Reset hashtable entries (if available) and statistic information.
|
||||
*/
|
||||
void tt_clear()
|
||||
{
|
||||
if (hash_tt.keys)
|
||||
memset(hash_tt.keys, 0, hash_tt.bytes);
|
||||
|
||||
hash_tt.used_keys = 0;
|
||||
hash_tt.collisions = 0;
|
||||
hash_tt.hits = 0;
|
||||
hash_tt.misses = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* tt_delete() - delete transposition table
|
||||
*
|
||||
* free hashtable data.
|
||||
*/
|
||||
void tt_delete()
|
||||
{
|
||||
if (hash_tt.keys) {
|
||||
safe_free(hash_tt.keys);
|
||||
hash_tt.keys = NULL;
|
||||
}
|
||||
tt_clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* tt_probe() - probe tt for an entry
|
||||
*
|
||||
*
|
||||
*/
|
||||
hentry_t *tt_probe(hkey_t key)
|
||||
{
|
||||
bucket_t *bucket;
|
||||
hentry_t *entry;
|
||||
int i;
|
||||
|
||||
bug_on(!hash_tt.keys);
|
||||
bucket = hash_tt.keys + (key & hash_tt.mask);
|
||||
|
||||
/* find key in buckets */
|
||||
for (i = 0; i < ENTRIES_PER_BUCKET; ++i) {
|
||||
entry = bucket->entry + i;
|
||||
if (key == entry->key)
|
||||
return entry;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* tt_probe_perft() - probe tt for an entry (perft version)
|
||||
* @key: Zobrist (hkey_t) key
|
||||
* @depth: depth from search root
|
||||
*
|
||||
* Search transposition for @key entry with @depth depth.
|
||||
*
|
||||
* @return: @hentry_t address is found, TT_MISS otherwise.
|
||||
*/
|
||||
hentry_t *tt_probe_perft(const hkey_t key, const u16 depth)
|
||||
{
|
||||
bucket_t *bucket;
|
||||
hentry_t *entry;
|
||||
int i;
|
||||
|
||||
bug_on(!hash_tt.keys);
|
||||
bucket = hash_tt.keys + (key & hash_tt.mask);
|
||||
|
||||
/* find key in buckets */
|
||||
for (i = 0; i < ENTRIES_PER_BUCKET; ++i) {
|
||||
entry = bucket->entry + i;
|
||||
if (key == entry->key && HASH_PERFT_DEPTH(entry->data) == depth) {
|
||||
hash_tt.hits++;
|
||||
/*
|
||||
* printf("tt hit: key=%lx depth=%d bucket=%lu entry=%d!\n",
|
||||
* key, depth, bucket - hash_tt.keys, i);
|
||||
*/
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* printf("tt miss: key=%lx depth=%d ucket=%lu\n",
|
||||
* key, depth, bucket - hash_tt.keys);
|
||||
*/
|
||||
hash_tt.misses++;
|
||||
return TT_MISS;
|
||||
}
|
||||
|
||||
/**
|
||||
* tt_store_perft() - store a transposition table entry (perft version)
|
||||
* @key: Zobrist (hkey_t) key
|
||||
* @depth: depth from search root
|
||||
* @nodes: value to store
|
||||
*
|
||||
*/
|
||||
hentry_t *tt_store_perft(const hkey_t key, const u16 depth, const u64 nodes)
|
||||
{
|
||||
bucket_t *bucket;
|
||||
hentry_t *entry;
|
||||
int replace = -1;
|
||||
uint mindepth = 1024;
|
||||
u64 data = HASH_PERFT(depth, nodes);
|
||||
|
||||
//printf("tt_store: key=%lx data=%lx depth=%d=%d nodes=%lu=%lu\n",
|
||||
// key, data, depth, HASH_PERFT_DEPTH(data), nodes, HASH_PERFT_VAL(data));
|
||||
/*
|
||||
* printf("tt_store: key=%lx depth=%d nodes=%lu ",
|
||||
* key, depth, nodes);
|
||||
*/
|
||||
bug_on(!hash_tt.keys);
|
||||
bucket = hash_tt.keys + (key & hash_tt.mask);
|
||||
|
||||
/* find key in buckets */
|
||||
for (int i = 0; i < ENTRIES_PER_BUCKET; ++i) {
|
||||
entry = bucket->entry + i;
|
||||
//if (!entry->key) {
|
||||
// replace = i;
|
||||
//hash_tt.used_keys++;
|
||||
// break;
|
||||
//}
|
||||
if (key == entry->key) {
|
||||
if (depth == HASH_PERFT_DEPTH(entry->data)) {
|
||||
printf("tt_store: dup key=%lx depth=%d, this should not happen!\n",
|
||||
key, depth);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
/* always keep higher nodes */
|
||||
if (HASH_PERFT_DEPTH(entry->data) < mindepth) {
|
||||
mindepth = HASH_PERFT_DEPTH(entry->data);
|
||||
replace = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (replace >= 0) {
|
||||
entry = bucket->entry + replace;
|
||||
|
||||
hash_tt.used_keys += entry->key == 0;
|
||||
hash_tt.collisions += entry->key && (key != entry->key);
|
||||
/*
|
||||
* if (HASH_PERFT_VAL(entry->data)) {
|
||||
* printf("REPL entry=%lu[%d] key=%lx->%lx val=%lu->%lu\n",
|
||||
* bucket - hash_tt.keys, replace,
|
||||
* entry->key, key,
|
||||
* HASH_PERFT_VAL(entry->data), nodes);
|
||||
* } else {
|
||||
* printf("NEW entry=%lu[%d] key=%lx val=%lu\n",
|
||||
* bucket - hash_tt.keys, replace,
|
||||
* entry->key, nodes);
|
||||
* }
|
||||
*/
|
||||
entry->key = key;
|
||||
entry->data = data;
|
||||
return entry;
|
||||
} else {
|
||||
//printf("TT full, skip\n");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* tt_info() - print hash-table information.
|
||||
*/
|
||||
void tt_info()
|
||||
{
|
||||
if (hash_tt.keys) {
|
||||
printf("TT: Mb:%d buckets:%'lu (bits:%u mask:%#x) entries:%'lu\n",
|
||||
hash_tt.mb, hash_tt.nbuckets, hash_tt.nbits,
|
||||
hash_tt.mask, hash_tt.nkeys);
|
||||
} else {
|
||||
printf("TT: not set.\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* tt_stats() - print hash-table usage.
|
||||
*/
|
||||
void tt_stats()
|
||||
{
|
||||
if (hash_tt.keys) {
|
||||
float percent = 100.0 * hash_tt.used_keys / hash_tt.nkeys;
|
||||
printf("hash: used:%'lu/%'lu (%.2f%%) hit:%'lu miss:%'lu coll:%'lu\n",
|
||||
hash_tt.used_keys, hash_tt.nkeys, percent,
|
||||
hash_tt.hits, hash_tt.misses,
|
||||
hash_tt.collisions);
|
||||
} else {
|
||||
printf("hash: not set.\n");
|
||||
}
|
||||
}
|
||||
147
src/hash.h
Normal file
147
src/hash.h
Normal file
@@ -0,0 +1,147 @@
|
||||
/* hash.h - hash management.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef HASH_H
|
||||
#define HASH_H
|
||||
|
||||
#include <brlib.h>
|
||||
#include <bug.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "move.h"
|
||||
|
||||
#define ENTRIES_PER_BUCKET 4 /* buckets per hash table entry */
|
||||
|
||||
#define HASH_SIZE_DEFAULT 16 /* default: 16Mb */
|
||||
#define HASH_SIZE_MIN 1
|
||||
#define HASH_SIZE_MAX 32768 /* 32Gb */
|
||||
|
||||
#define TT_MISS NULL
|
||||
#define TT_DUP (void *) U64(0x01)
|
||||
#define TT_OK(p) ((p) > (void *)U64(0xF))
|
||||
|
||||
typedef u64 hkey_t; /* cannot use typedef for key_t */
|
||||
|
||||
/**
|
||||
* hash_short: get the most significant 7 nibble of a 64 bits value.
|
||||
*/
|
||||
#define hash_short(hash) ((hash) >> (64 - 4 * 7))
|
||||
|
||||
/**
|
||||
* hentry_t: hashtable bucket.
|
||||
*
|
||||
* Size should be exactly 16 bytes. If impossible to fit necessary data in
|
||||
* 16 bytes in future, it should be updated to be exactly 32 bytes.
|
||||
*/
|
||||
typedef struct {
|
||||
hkey_t key; /* position key */
|
||||
union {
|
||||
u64 data;
|
||||
struct {
|
||||
s16 eval; /* 16: eval */
|
||||
move_t move; /* 16: best move */
|
||||
u8 depth; /* 8: search depth */
|
||||
u8 gen8; /* 8: search generation */
|
||||
|
||||
//flags; /* maybe for locking, etc... */
|
||||
//u8 filler;
|
||||
};
|
||||
};
|
||||
} hentry_t;
|
||||
|
||||
/* hentry perft data:
|
||||
* 0-47: perft value
|
||||
* 48-63: depth
|
||||
*/
|
||||
#define HASH_PERFT_MASK U64(0xffffffffffff)
|
||||
#define HASH_PERFT(depth, val) ((((u64) depth) << 48) | ((val) & HASH_PERFT_MASK))
|
||||
#define HASH_PERFT_VAL(data) ((data) & HASH_PERFT_MASK)
|
||||
#define HASH_PERFT_DEPTH(data) ((u16)((data) >> 48))
|
||||
|
||||
|
||||
typedef struct {
|
||||
hentry_t entry[ENTRIES_PER_BUCKET];
|
||||
} bucket_t;
|
||||
|
||||
typedef struct {
|
||||
bucket_t *keys; /* &hashtable entries */
|
||||
|
||||
/* memory size in bytes/mb */
|
||||
size_t bytes;
|
||||
u32 mb;
|
||||
|
||||
/* size in buckets/keys */
|
||||
size_t nbuckets;
|
||||
size_t nkeys; /* nbuckets * NBUCKETS */
|
||||
|
||||
/* internal representation */
|
||||
u32 nbits; /* #buckets in bits, power of 2 */
|
||||
u32 mask; /* nbuckets - 1, key mask */
|
||||
|
||||
/* stats - unsure about usage */
|
||||
//size_t used_buckets;
|
||||
size_t used_keys;
|
||||
u64 collisions;
|
||||
u64 hits;
|
||||
u64 misses;
|
||||
} hasht_t;
|
||||
|
||||
/* hack:
|
||||
* ep zobrist key index is 0-7 for each en-passant file, 8 for SQUARE_NONE.
|
||||
* To transform :
|
||||
* - ep == 64 (SQUARE_NONE) to id = 8
|
||||
* - ep == 0~63 to idx = sq_file(ep), i.e. (ep & 7)
|
||||
* we use the formula:
|
||||
* idx = ( ( ep & SQUARE_NONE ) >> 3 ) | sq_file(ep);
|
||||
*/
|
||||
#define EP_ZOBRIST_IDX(ep) ( ( ( ep & SQUARE_NONE ) >> 3 ) | sq_file(ep) )
|
||||
|
||||
extern hkey_t zobrist_pieces[16][64];
|
||||
extern hkey_t zobrist_castling[4 * 4 + 1];
|
||||
extern hkey_t zobrist_turn; /* for black, XOR each ply */
|
||||
extern hkey_t zobrist_ep[9]; /* 0-7: ep file, 8: SQUARE_NONE */
|
||||
|
||||
extern hasht_t hash_tt; /* main transposition table */
|
||||
|
||||
void zobrist_init(void);
|
||||
hkey_t zobrist_calc(pos_t *pos);
|
||||
|
||||
#ifdef ZOBRIST_VERIFY
|
||||
bool zobrist_verify(pos_t *pos);
|
||||
#else
|
||||
#define zobrist_verify(p) true
|
||||
#endif
|
||||
|
||||
/**
|
||||
* tt_prefetch() - prefetch hash table entry
|
||||
* @hash: u64 key
|
||||
*
|
||||
* Prefetch memory for @key.
|
||||
*/
|
||||
static inline void tt_prefetch(hkey_t key)
|
||||
{
|
||||
bug_on(!hash_tt.keys);
|
||||
__builtin_prefetch(hash_tt.keys + (key & hash_tt.mask));
|
||||
}
|
||||
|
||||
int tt_create(int Mb);
|
||||
void tt_clear(void);
|
||||
void tt_delete(void);
|
||||
|
||||
hentry_t *tt_probe(hkey_t key);
|
||||
hentry_t *tt_probe_perft(const hkey_t key, const u16 depth);
|
||||
hentry_t *tt_store_perft(const hkey_t key, const u16 depth, const u64 nodes);
|
||||
void tt_info(void);
|
||||
void tt_stats(void);
|
||||
|
||||
#endif /* HASH_H */
|
||||
177
src/hist.c
Normal file
177
src/hist.c
Normal file
@@ -0,0 +1,177 @@
|
||||
/* hist.c - history management.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <brlib.h>
|
||||
#include <bug.h>
|
||||
|
||||
#include "position.h"
|
||||
#include "hash.h"
|
||||
#include "move.h"
|
||||
#include "hist.h"
|
||||
|
||||
hist_t hist = {
|
||||
.nstates = 1,
|
||||
{ { .move = MOVE_NONE, .key = U64(0), .prev = &hist.state[0] } },
|
||||
};
|
||||
|
||||
/**
|
||||
* hist_init() - initialize history states data.
|
||||
*
|
||||
* This should be done every time a new position must be handled.
|
||||
*/
|
||||
void hist_init(void)
|
||||
{
|
||||
hist.nstates = 1;
|
||||
hist.state[0].key = U64(0);
|
||||
hist.state[0].move = MOVE_NONE;
|
||||
hist.state[0].prev = &hist.state[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* hist_next() - get next history slot.
|
||||
*
|
||||
* return: @state_t next history slot.
|
||||
*/
|
||||
state_t *hist_next(void)
|
||||
{
|
||||
return hist.state + hist.nstates++;
|
||||
}
|
||||
|
||||
/**
|
||||
* hist_push() - add a state to hist list
|
||||
* @st: &state_t to add
|
||||
*
|
||||
* Used to add moves when the UCI "position" command includes moves.
|
||||
* These moves, but last one, should be pushed. Last move should be
|
||||
* linked to hist with @hist_link().
|
||||
*/
|
||||
void hist_push(state_t *st) //, move_t *move)
|
||||
{
|
||||
int last = hist.nstates++;
|
||||
|
||||
bug_on(last >= HIST_SIZE);
|
||||
hist.state[last] = *st;
|
||||
hist.state[last].prev = &hist.state[last - 1];
|
||||
// hist.state[last].move = *move;
|
||||
}
|
||||
|
||||
/**
|
||||
* hist_link() - link a position to last hist element.
|
||||
* @pos: &pos_t to link
|
||||
*
|
||||
* Used to add position resulting from last "move" in UCI "position" command.
|
||||
* All other moves in UCI "position" command should be pushed instead, with
|
||||
* hist_push().
|
||||
*/
|
||||
void hist_link(pos_t *pos)
|
||||
{
|
||||
pos->prev = hist_last();
|
||||
}
|
||||
|
||||
/**
|
||||
* hist_pop() - return last state from hist entry, and remove it from list
|
||||
*
|
||||
* Not used, only for debug.
|
||||
*/
|
||||
state_t *hist_pop(void)
|
||||
{
|
||||
if (hist.nstates > 1)
|
||||
hist.nstates--;
|
||||
return hist_last();
|
||||
}
|
||||
|
||||
/**
|
||||
* hist_last() - return last state from hist.
|
||||
*/
|
||||
state_t *hist_last(void)
|
||||
{
|
||||
return hist.state + hist.nstates - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* hist_prev() - return a state's ancestor.
|
||||
* @st: &state_t state
|
||||
*
|
||||
* No test is done on ancestor. Caller should check it is different
|
||||
* from HIST_START.
|
||||
*/
|
||||
state_t *hist_prev(state_t *st)
|
||||
{
|
||||
return st->prev;
|
||||
}
|
||||
|
||||
/**
|
||||
* hist_prev2() - return a state's second ancestor.
|
||||
* @st: &state_t state
|
||||
*
|
||||
* No test is done on ancestors. Caller should check it is different
|
||||
* from HIST_START.
|
||||
*/
|
||||
state_t *hist_prev2(state_t *st)
|
||||
{
|
||||
return st->prev->prev;
|
||||
}
|
||||
|
||||
/**
|
||||
* hist_prev4() - return a state's 4th ancestor.
|
||||
* @st: &state_t state
|
||||
*
|
||||
* No test is done on ancestors. Caller should check it is different
|
||||
* from HIST_START.
|
||||
*/
|
||||
state_t *hist_prev4(state_t *st)
|
||||
{
|
||||
return st->prev->prev->prev->prev;
|
||||
}
|
||||
|
||||
/**
|
||||
* hist_static_print() - print hist entries
|
||||
*/
|
||||
void hist_static_print(void)
|
||||
{
|
||||
char movestr[8];
|
||||
state_t *st = hist_last();
|
||||
|
||||
printf("UCI state history: ");
|
||||
while (true) {
|
||||
printf("%s(#%lx r=%d) ",
|
||||
move_to_str(movestr, st->move, 0),
|
||||
hash_short(st->key),
|
||||
st->repcount);
|
||||
if (st == HIST_START)
|
||||
break;
|
||||
st = hist_prev(st);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* hist_print() - print position history
|
||||
* @pos: &pos to start from
|
||||
*/
|
||||
void hist_print(pos_t *pos)
|
||||
{
|
||||
char movestr[8];
|
||||
state_t *st = &pos->state;
|
||||
printf("position states history: ");
|
||||
while (true) {
|
||||
printf("%s(#%lx r=%d) ",
|
||||
move_to_str(movestr, st->move, 0),
|
||||
hash_short(st->key),
|
||||
st->repcount);
|
||||
if (st == HIST_START)
|
||||
break;
|
||||
st = hist_prev(st);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
62
src/hist.h
Normal file
62
src/hist.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/* hist.h - history management.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef HIST_H
|
||||
#define HIST_H
|
||||
|
||||
#include <brlib.h>
|
||||
#include <bug.h>
|
||||
|
||||
#include "position.h"
|
||||
#include "hash.h"
|
||||
|
||||
#define HIST_SIZE 4096 /* I know, I know... */
|
||||
|
||||
/**
|
||||
* hist - history states data.
|
||||
*
|
||||
* This variable is of type hist_t, which contains:
|
||||
* @state: state_t array, size HIST_SIZE
|
||||
* @nstates: current number of @state
|
||||
*
|
||||
* hist contains moves already played.
|
||||
*
|
||||
* Only HIST_SIZE - 1 hist elements are available, as the first element is
|
||||
* used as a sentinel value (hist[0].state.prev = &hist[0].state).
|
||||
* This first element allows multiple backards searches (p->prev->prev).
|
||||
*
|
||||
* hist is only written by main thread, and read by other threads/processes,
|
||||
* therefore is never duplicated (even after a fork(), due to COW).
|
||||
*/
|
||||
typedef struct {
|
||||
int nstates;
|
||||
state_t state[HIST_SIZE];
|
||||
} hist_t;
|
||||
|
||||
extern hist_t hist;
|
||||
|
||||
#define HIST_START (hist.state)
|
||||
|
||||
void hist_init(void);
|
||||
state_t *hist_next(void);
|
||||
void hist_push(state_t *st); //, move_t *move);
|
||||
void hist_link(pos_t *pos);
|
||||
state_t *hist_pop(void);
|
||||
state_t *hist_last(void);
|
||||
state_t *hist_prev(state_t *st);
|
||||
state_t *hist_prev2(state_t *st);
|
||||
state_t *hist_prev4(state_t *st);
|
||||
void hist_static_print(void);
|
||||
void hist_print(pos_t *pos);
|
||||
|
||||
#endif /* HIST_H */
|
||||
195
src/hq.c
Normal file
195
src/hq.c
Normal file
@@ -0,0 +1,195 @@
|
||||
/* hq.c - hyperbola quintessence functions.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <brlib.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "board.h"
|
||||
#include "bitboard.h"
|
||||
#include "hq.h"
|
||||
|
||||
|
||||
uchar bb_rank_attacks[64 * 8];
|
||||
|
||||
/**
|
||||
* hq_init() - init hyperbola quintessence attack bitboards
|
||||
*
|
||||
* See: https://www.chessprogramming.org/Kindergarten_Bitboards
|
||||
* and https://www.chessprogramming.org/Hyperbola_Quintessence
|
||||
*
|
||||
*
|
||||
* Rank attacks table:
|
||||
* bb_rank_hyperbola[512 = 9 bits], indexed by oooooofff where:
|
||||
* - O = oooooo: occupation of inner 6 bits on rank
|
||||
* - F = fff: file of sliding piece
|
||||
* The index is built as (oooooo << 3 + fff), = ((O << 3) + F), where:
|
||||
* - O = all combinations of 6 bits (loop from 0 to 64)
|
||||
* - F = all files (loop from 0 to 7)
|
||||
* To retrieve the index, given an 8 bits mask M=XooooooX, and a file F=fff,
|
||||
* we get O from M with:
|
||||
* 1) remove bits 'X' (O = M & 01111110)
|
||||
* 2) shift left result 2 more bits, as bit 0 is unused and already cleared:
|
||||
* (O <<= 2)
|
||||
*
|
||||
* TODO ? create masks excluding slider (eg. bb_diag ^ bb_sq[square]),
|
||||
* to save one operation in hq_moves().
|
||||
* TODO ? replace rank attack with this idea, mapping rank to diagonal ?
|
||||
* See http://timcooijmans.blogspot.com/2014/04/
|
||||
*/
|
||||
void hq_init()
|
||||
{
|
||||
/* generate rank attacks, not handled by HQ
|
||||
*/
|
||||
for (int occ = 0; occ < 64; ++occ) {
|
||||
for (int file = 0; file < 8; ++file) {
|
||||
int attacks = 0;
|
||||
|
||||
/* set f left attacks */
|
||||
for (int slide = file - 1; slide >= 0; --slide) {
|
||||
int b = bb_sq[slide]; /* bit to consider */
|
||||
attacks |= b; /* add to attack mask */
|
||||
if ((occ << 1) & b) /* piece on b, we stop */
|
||||
break;
|
||||
}
|
||||
/* set f right attacks */
|
||||
for (int slide = file + 1; slide < 8; ++slide) {
|
||||
int b = bb_sq[slide];
|
||||
attacks |= b;
|
||||
if ((occ << 1) & b) /* piece on b, we stop */
|
||||
break;
|
||||
}
|
||||
bb_rank_attacks[(occ << 3) + file] = attacks;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* hq_rank_moves() - get rank moves for a sliding piece.
|
||||
* @pieces: occupation bitboard
|
||||
* @sq: piece square
|
||||
*
|
||||
* Rank attacks are not handled by HQ, so this function uses a pre-calculated
|
||||
* rank attacks table (@bb_rank_attacks).
|
||||
*
|
||||
* @Return: bitboard of @piece available pseudo-moves.
|
||||
*/
|
||||
bitboard_t hq_rank_moves(bitboard_t occ, square_t sq)
|
||||
{
|
||||
u32 rank = sq & SQ_RANKMASK;
|
||||
u32 file = sq & SQ_FILEMASK;
|
||||
u64 o = (occ >> rank) & 0176; /* 01111110 clear bits 0 & 7 */
|
||||
return ((bitboard_t)bb_rank_attacks[(o << 2) + file]) << rank;
|
||||
}
|
||||
|
||||
/**
|
||||
* hq_moves() - get hyperbola pseudo-moves for a sliding piece
|
||||
* @pieces: occupation bitboard
|
||||
* @sq: piece square
|
||||
* @mask: the appropriate mask (pre-calculated)
|
||||
*
|
||||
* This function can be used for files, diagonal, and anti-diagonal attacks.
|
||||
* @mask is the corresponding pre-calculated table (@bb_sqfile, @bb_sqdiag,
|
||||
* or @bb_sqanti).
|
||||
* See https://www.chessprogramming.org/Hyperbola_Quintessence for details.
|
||||
*
|
||||
* @Return: bitboard of piece available pseudo-moves.
|
||||
*/
|
||||
bitboard_t hq_moves(const bitboard_t pieces, const square_t sq,
|
||||
const bitboard_t mask)
|
||||
{
|
||||
bitboard_t o = pieces & mask;
|
||||
bitboard_t r = bswap64(o);
|
||||
square_t r_sq = FLIP_V(sq);
|
||||
|
||||
return ( (o - 2 * BIT(sq) )
|
||||
^ bswap64(r - 2 * BIT(r_sq)))
|
||||
& mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* hq_file_moves() - get file pseudo-moves for a sliding piece.
|
||||
* @pieces: occupation bitboard
|
||||
* @sq: piece square
|
||||
*
|
||||
* @Return: bitboard of piece available pseudo-moves on its file.
|
||||
*/
|
||||
bitboard_t hq_file_moves(const bitboard_t occ, const square_t sq)
|
||||
{
|
||||
return hq_moves(occ, sq, bb_sqfile[sq]);
|
||||
}
|
||||
|
||||
/**
|
||||
* hq_diag_moves() - get diagonal pseudo-moves for a sliding piece.
|
||||
* @pieces: occupation bitboard
|
||||
* @sq: piece square
|
||||
*
|
||||
* @Return: bitboard of piece available pseudo-moves on its diagonal.
|
||||
*/
|
||||
bitboard_t hq_diag_moves(const bitboard_t occ, const square_t sq)
|
||||
{
|
||||
return hq_moves(occ, sq, bb_sqdiag[sq]);
|
||||
}
|
||||
|
||||
/**
|
||||
* hq_anti_moves() - get anti-diagonal pseudo-moves for a sliding piece.
|
||||
* @pieces: occupation bitboard
|
||||
* @sq: piece square
|
||||
*
|
||||
* @Return: bitboard of piece available pseudo-moves on its anti-diagonal.
|
||||
*/
|
||||
bitboard_t hq_anti_moves(const bitboard_t occ, const square_t sq)
|
||||
{
|
||||
return hq_moves(occ, sq, bb_sqanti[sq]);
|
||||
}
|
||||
|
||||
/**
|
||||
* hq_bishop_moves() - get bitboard of bishop pseudo-moves
|
||||
* @occ: occupation bitboard
|
||||
* @sq: bishop square
|
||||
*
|
||||
* @Return: bitboard of bishop available pseudo-moves.
|
||||
*/
|
||||
bitboard_t hq_bishop_moves(const bitboard_t occ, const square_t sq)
|
||||
{
|
||||
return hq_diag_moves(occ, sq) | hq_anti_moves(occ, sq);
|
||||
}
|
||||
|
||||
/**
|
||||
* hq_rook_moves() - get bitboard of rook pseudo-moves
|
||||
* @occ: occupation bitboard
|
||||
* @sq: rook square
|
||||
*
|
||||
* @Return: bitboard of rook available pseudo-moves.
|
||||
*/
|
||||
bitboard_t hq_rook_moves(const bitboard_t occ, const square_t sq)
|
||||
{
|
||||
return hq_file_moves(occ, sq) | hq_rank_moves(occ, sq);
|
||||
}
|
||||
|
||||
/**
|
||||
* hq_queen_moves() - get bitboard of queen pseudo-moves
|
||||
* @occ: occupation bitboard
|
||||
* @sq: queen square
|
||||
*
|
||||
* This function is a wrapper over @hyperbola_bishop_moves() and
|
||||
* @hyperbola_rook_moves().
|
||||
*
|
||||
* @Return: bitboard of queen available pseudo-moves.
|
||||
*/
|
||||
bitboard_t hq_queen_moves(const bitboard_t occ, const square_t sq)
|
||||
{
|
||||
return hq_bishop_moves(occ, sq) | hq_rook_moves(occ, sq);
|
||||
}
|
||||
33
src/hq.h
Normal file
33
src/hq.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/* hq.h - hyperbola-quintessence definitions.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _HQ_H
|
||||
#define _HQ_H
|
||||
|
||||
#include "board.h"
|
||||
#include "bitboard.h"
|
||||
|
||||
void hq_init(void);
|
||||
|
||||
bitboard_t hq_rank_moves(const bitboard_t occ, const square_t sq);
|
||||
bitboard_t hq_moves(const bitboard_t pieces, const square_t sq,
|
||||
const bitboard_t mask);
|
||||
bitboard_t hq_file_moves(const bitboard_t occ, const square_t sq);
|
||||
bitboard_t hq_diag_moves(const bitboard_t occ, const square_t sq);
|
||||
bitboard_t hq_anti_moves(const bitboard_t occ, const square_t sq);
|
||||
|
||||
bitboard_t hq_bishop_moves(const bitboard_t occ, const square_t sq);
|
||||
bitboard_t hq_rook_moves(const bitboard_t occ, const square_t sq);
|
||||
bitboard_t hq_queen_moves(const bitboard_t occ, const square_t sq);
|
||||
|
||||
#endif /* _HQ_H */
|
||||
75
src/init.c
Normal file
75
src/init.c
Normal file
@@ -0,0 +1,75 @@
|
||||
/* init.c - initialize all.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <locale.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "util.h"
|
||||
#include "bitboard.h"
|
||||
#include "hq.h"
|
||||
#include "eval-defs.h"
|
||||
#include "hash.h"
|
||||
#include "hist.h"
|
||||
|
||||
#define printff(x) ({ printf(x); fflush(stdout); })
|
||||
|
||||
void init_all(void)
|
||||
{
|
||||
printff("initiazing system: ");
|
||||
/* line-buffered stdout */
|
||||
printff("stdout buffering... ");
|
||||
setlinebuf(stdout);
|
||||
|
||||
printff("locale... ");
|
||||
/* for printf() thousands separator */
|
||||
setlocale(LC_NUMERIC, "");
|
||||
setlocale(LC_CTYPE, "C");
|
||||
|
||||
/* pseudo random generator seed */
|
||||
printff("random generator... ");
|
||||
rand_init(RAND_SEED_DEFAULT);
|
||||
|
||||
printf("done.\n");
|
||||
|
||||
printff("initiazing board data: ");
|
||||
/* bitboards & hq */
|
||||
printff("bitboards... ");
|
||||
bitboard_init();
|
||||
|
||||
printff("hq bitboards... ");
|
||||
hq_init();
|
||||
|
||||
printf("done.\n");
|
||||
|
||||
printff("initiazing move data: ");
|
||||
/* zobrist tables & default tt hashtable */
|
||||
printff("zobrist tables... ");
|
||||
zobrist_init();
|
||||
|
||||
printff("transposition tables... ");
|
||||
tt_create(HASH_SIZE_DEFAULT);
|
||||
|
||||
printf("done.\n");
|
||||
|
||||
printff("initiazing eval data: ");
|
||||
printf("parameters... ");
|
||||
param_init();
|
||||
|
||||
/* eval tables */
|
||||
printf("pst tables... ");
|
||||
pst_init(PST_DEFAULT);
|
||||
|
||||
printf("done.\n");
|
||||
}
|
||||
388
src/move-do.c
Normal file
388
src/move-do.c
Normal file
@@ -0,0 +1,388 @@
|
||||
/* move-do.c - move do/undo.
|
||||
*
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <malloc.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <brlib.h>
|
||||
#include <likely.h>
|
||||
#include <bug.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "move.h"
|
||||
#include "position.h"
|
||||
#include "move-do.h"
|
||||
#include "hash.h"
|
||||
#include "hist.h"
|
||||
|
||||
/**
|
||||
* move_do() - do move.
|
||||
* @pos: &pos_t position
|
||||
* @move: move to apply
|
||||
* @state: &state_t address where irreversible changes will be saved
|
||||
*
|
||||
* @move is applied to @pos:
|
||||
* - bitboards and board are updated
|
||||
* - counters are updated:
|
||||
* - move count
|
||||
* - 50-moves rule count
|
||||
* - flags are possibly updated:
|
||||
* - castling
|
||||
* - en-passant
|
||||
* - captured piece (excl. en-passant)
|
||||
* - tt hash values are updated for:
|
||||
* - side-to-move
|
||||
* - en-passant
|
||||
* - castling rights.
|
||||
*
|
||||
* @return: updated pos.
|
||||
*/
|
||||
pos_t *move_do(pos_t *pos, const move_t move, state_t *state)
|
||||
{
|
||||
color_t us = pos->turn, them = OPPONENT(us);
|
||||
square_t from = move_from(move), to = move_to(move);
|
||||
piece_t piece = pos->board[from];
|
||||
piece_t captured = pos->board[to];
|
||||
piece_type_t ptype = PIECE(piece);
|
||||
piece_t new_piece = piece;
|
||||
int up = sq_up(us);
|
||||
hkey_t key = pos->key;
|
||||
|
||||
*state = pos->state; /* save irreversible changes */
|
||||
pos->prev = state;
|
||||
|
||||
/* update key: switch turn, reset castling and ep */
|
||||
key ^= zobrist_turn;
|
||||
key ^= zobrist_castling[pos->castle];
|
||||
key ^= zobrist_ep[EP_ZOBRIST_IDX(pos->en_passant)];
|
||||
|
||||
++pos->ply50;
|
||||
++pos->plycount;
|
||||
++pos->plyroot;
|
||||
pos->en_passant = SQUARE_NONE;
|
||||
pos->turn = them;
|
||||
pos->captured = captured;
|
||||
pos->move = move;
|
||||
|
||||
bug_on(COLOR(piece) != us);
|
||||
|
||||
if (is_promotion(move)) {
|
||||
bug_on(sq_rank(to) != sq_rel_rank(RANK_8, us));
|
||||
piece_t promoted = move_promoted(move);
|
||||
new_piece = MAKE_PIECE(promoted, us);
|
||||
pos->phase += pt_phase(PAWN) - pt_phase(promoted);
|
||||
}
|
||||
|
||||
if (captured != EMPTY) {
|
||||
pos->ply50 = 0;
|
||||
pos->phase += pt_phase(PIECE(captured));
|
||||
|
||||
bug_on(COLOR(captured) != them);
|
||||
key ^= zobrist_pieces[captured][to];
|
||||
pos_clr_sq(pos, to); /* clear square */
|
||||
} else if (is_castle(move)) { /* handle rook move */
|
||||
square_t rookfrom, rookto;
|
||||
|
||||
if (to > from) {
|
||||
rookfrom = sq_rel(H1, us);
|
||||
rookto = sq_rel(F1, us);
|
||||
} else {
|
||||
rookfrom = sq_rel(A1, us);
|
||||
rookto = sq_rel(D1, us);
|
||||
}
|
||||
key ^= zobrist_pieces[pos->board[rookfrom]][rookto] ^
|
||||
zobrist_pieces[pos->board[rookfrom]][rookfrom];
|
||||
pos_set_sq(pos, rookto, pos->board[rookfrom]);
|
||||
pos_clr_sq(pos, rookfrom);
|
||||
pos->castle = clr_castle(pos->castle, us);
|
||||
} else if (ptype == PAWN) { /* pawn non capture or e.p. */
|
||||
pos->ply50 = 0;
|
||||
if (from + up + up == to) { /* if pawn double push, set e.p. */
|
||||
square_t ep = from + up;
|
||||
|
||||
if (bb_pawn_attacks[us][ep] & pos->bb[them][PAWN]) {
|
||||
pos->en_passant = ep;
|
||||
key ^= zobrist_ep[EP_ZOBRIST_IDX(pos->en_passant)];
|
||||
}
|
||||
} else if (is_enpassant(move)) { /* clear grabbed pawn */
|
||||
square_t grabbed = to - up;
|
||||
piece_t pc = pos->board[grabbed];
|
||||
|
||||
pos->phase += pt_phase(PAWN);
|
||||
key ^= zobrist_pieces[pc][grabbed];
|
||||
pos_clr_sq(pos, grabbed);
|
||||
}
|
||||
}
|
||||
|
||||
key ^= zobrist_pieces[piece][from] ^ zobrist_pieces[new_piece][to];
|
||||
pos_clr_sq(pos, from); /* clear "from" and set "to" */
|
||||
pos_set_sq(pos, to, new_piece);
|
||||
|
||||
if (ptype == KING)
|
||||
pos->king[us] = to;
|
||||
|
||||
/* update castling flags
|
||||
* As we always consider flags are valid, we :
|
||||
* - adjust our flags if relative from is "E1", "A1", H1"
|
||||
* - adjust opp flags if relative to if "A8", H8"
|
||||
*/
|
||||
if (can_castle(pos->castle, us)) { /* do we save time with this test ? */
|
||||
square_t rel_e1 = sq_rel(E1, us);
|
||||
square_t rel_a1 = sq_rel(A1, us);
|
||||
square_t rel_h1 = sq_rel(H1, us);
|
||||
if (from == rel_e1)
|
||||
pos->castle = clr_castle(pos->castle, us);
|
||||
else if (from == rel_a1)
|
||||
pos->castle = clr_ooo(pos->castle, us);
|
||||
else if (from == rel_h1)
|
||||
pos->castle = clr_oo(pos->castle, us);
|
||||
}
|
||||
if (can_castle(pos->castle, them)) {
|
||||
square_t rel_a8 = sq_rel(A8, us);
|
||||
square_t rel_h8 = sq_rel(H8, us);
|
||||
if (to == rel_a8)
|
||||
pos->castle = clr_ooo(pos->castle, them);
|
||||
else if (to == rel_h8)
|
||||
pos->castle = clr_oo(pos->castle, them);
|
||||
}
|
||||
|
||||
/* update castling rights key */
|
||||
key ^= zobrist_castling[pos->castle];
|
||||
|
||||
pos->key = key;
|
||||
|
||||
pos->repcount = pos_repcount(pos);
|
||||
|
||||
/* consistency checks.
|
||||
* Checks done only if compiled with '-DZOBRIST_VERIFY' or '-DPHASE_VERIFY'
|
||||
*/
|
||||
zobrist_verify(pos);
|
||||
phase_verify(pos);
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* move_undo() - undo move.
|
||||
* @pos: &pos_t position
|
||||
* @move: move to undo
|
||||
* @state: &state_t address where irreversible changes were saved
|
||||
*
|
||||
* @move is applied to @pos:
|
||||
* - bitboards and board are updated
|
||||
* - previous information is restored:
|
||||
* - castling
|
||||
* - en-passant
|
||||
* - captured piece (excl. en-passant)
|
||||
* - move count
|
||||
* - 50-moves rule count
|
||||
*
|
||||
* @return: pos.
|
||||
*/
|
||||
pos_t *move_undo(pos_t *pos, const move_t move, const state_t *state)
|
||||
{
|
||||
color_t them = pos->turn, us = OPPONENT(them);
|
||||
square_t from = move_from(move), to = move_to(move);
|
||||
piece_t piece = pos->board[to];
|
||||
int up = sq_up(them);
|
||||
|
||||
if (is_promotion(move))
|
||||
piece = MAKE_PIECE(PAWN, us);
|
||||
|
||||
pos_clr_sq(pos, to); /* always clear "to" ... */
|
||||
pos_set_sq(pos, from, piece); /* ... and set "from" */
|
||||
|
||||
if (PIECE(piece) == KING)
|
||||
pos->king[us] = from;
|
||||
|
||||
if (pos->captured != EMPTY) {
|
||||
pos_set_sq(pos, to, pos->captured); /* restore captured piece */
|
||||
} else if (is_castle(move)) { /* make reverse rook move */
|
||||
square_t rookfrom, rookto;
|
||||
if (to > from) {
|
||||
rookfrom = sq_rel(F1, us);
|
||||
rookto = sq_rel(H1, us);
|
||||
} else {
|
||||
rookfrom = sq_rel(D1, us);
|
||||
rookto = sq_rel(A1, us);
|
||||
}
|
||||
pos_set_sq(pos, rookto, pos->board[rookfrom]);
|
||||
pos_clr_sq(pos, rookfrom);
|
||||
} else if (is_enpassant(move)) { /* restore grabbed pawn */
|
||||
square_t grabbed = to + up;
|
||||
pos_set_sq(pos, grabbed, MAKE_PIECE(PAWN, them));
|
||||
}
|
||||
|
||||
pos->state = *state; /* restore irreversible changes */
|
||||
pos->turn = us;
|
||||
--pos->plycount;
|
||||
--pos->plyroot;
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* move_{do,undo}_alt - alternative move_do/move_undo (to experiment)
|
||||
*/
|
||||
pos_t *move_do_alt(pos_t *pos, const move_t move, state_t *state)
|
||||
{
|
||||
color_t us = pos->turn, them = OPPONENT(us);
|
||||
square_t from = move_from(move), to = move_to(move);
|
||||
piece_t piece = pos->board[from];
|
||||
piece_t captured = pos->board[to];
|
||||
piece_type_t ptype = PIECE(piece);
|
||||
piece_t new_piece = piece;
|
||||
int up = sq_up(us);
|
||||
hkey_t key = pos->key;
|
||||
|
||||
*state = pos->state; /* save irreversible changes */
|
||||
pos->prev = state;
|
||||
|
||||
/* update key: switch turn, reset castling and ep */
|
||||
key ^= zobrist_turn;
|
||||
key ^= zobrist_castling[pos->castle];
|
||||
key ^= zobrist_ep[EP_ZOBRIST_IDX(pos->en_passant)];
|
||||
|
||||
++pos->ply50;
|
||||
++pos->plycount;
|
||||
++pos->plyroot;
|
||||
pos->en_passant = SQUARE_NONE;
|
||||
pos->turn = them;
|
||||
pos->captured = captured;
|
||||
pos->move = move;
|
||||
|
||||
bug_on(COLOR(piece) != us);
|
||||
|
||||
if (is_promotion(move)) {
|
||||
bug_on(sq_rank(to) != sq_rel_rank(RANK_8, us));
|
||||
new_piece = MAKE_PIECE(move_promoted(move), us);
|
||||
}
|
||||
|
||||
if (captured != EMPTY) {
|
||||
pos->ply50 = 0;
|
||||
bug_on(pos->board[to] == EMPTY || COLOR(pos->captured) != them);
|
||||
key ^= zobrist_pieces[captured][to];
|
||||
pos_clr_sq(pos, to); /* clear square */
|
||||
} else if (is_castle(move)) { /* handle rook move */
|
||||
square_t rookfrom, rookto;
|
||||
if (to > from) {
|
||||
rookfrom = sq_rel(H1, us);
|
||||
rookto = sq_rel(F1, us);
|
||||
} else {
|
||||
rookfrom = sq_rel(A1, us);
|
||||
rookto = sq_rel(D1, us);
|
||||
}
|
||||
key ^= zobrist_pieces[pos->board[rookfrom]][rookto] ^
|
||||
zobrist_pieces[pos->board[rookfrom]][rookfrom];
|
||||
pos_set_sq(pos, rookto, pos->board[rookfrom]);
|
||||
pos_clr_sq(pos, rookfrom);
|
||||
pos->castle = clr_castle(pos->castle, us);
|
||||
} else if (ptype == PAWN) { /* pawn non capture or e.p. */
|
||||
pos->ply50 = 0;
|
||||
if (from + up + up == to) { /* if pawn double push, set e.p. */
|
||||
square_t ep = from + up;
|
||||
if (bb_pawn_attacks[us][ep] & pos->bb[them][PAWN]) {
|
||||
pos->en_passant = ep;
|
||||
key ^= zobrist_ep[EP_ZOBRIST_IDX(pos->en_passant)];
|
||||
}
|
||||
} else if (is_enpassant(move)) { /* clear grabbed pawn */
|
||||
square_t grabbed = to - up;
|
||||
piece_t pc = pos->board[grabbed];
|
||||
key ^= zobrist_pieces[pc][grabbed];
|
||||
pos_clr_sq(pos, grabbed);
|
||||
}
|
||||
}
|
||||
|
||||
key ^= zobrist_pieces[piece][from] ^ zobrist_pieces[new_piece][to];
|
||||
pos_clr_sq(pos, from); /* clear "from" and set "to" */
|
||||
pos_set_sq(pos, to, new_piece);
|
||||
|
||||
if (ptype == KING)
|
||||
pos->king[us] = to;
|
||||
|
||||
/* update castling flags
|
||||
* As we always consider flags are valid, we :
|
||||
* - adjust our flags if relative from is "E1", "A1", H1"
|
||||
* - adjust opp flags if relative to if "A8", H8"
|
||||
*/
|
||||
if (can_castle(pos->castle, us)) { /* do we save time with this test ? */
|
||||
square_t rel_e1 = sq_rel(E1, us);
|
||||
square_t rel_a1 = sq_rel(A1, us);
|
||||
square_t rel_h1 = sq_rel(H1, us);
|
||||
if (from == rel_e1)
|
||||
pos->castle = clr_castle(pos->castle, us);
|
||||
else if (from == rel_a1)
|
||||
pos->castle = clr_ooo(pos->castle, us);
|
||||
else if (from == rel_h1)
|
||||
pos->castle = clr_oo(pos->castle, us);
|
||||
}
|
||||
if (can_castle(pos->castle, them)) {
|
||||
square_t rel_a8 = sq_rel(A8, us);
|
||||
square_t rel_h8 = sq_rel(H8, us);
|
||||
if (to == rel_a8)
|
||||
pos->castle = clr_ooo(pos->castle, them);
|
||||
else if (to == rel_h8)
|
||||
pos->castle = clr_oo(pos->castle, them);
|
||||
}
|
||||
|
||||
/* update castling rights key */
|
||||
key ^= zobrist_castling[pos->castle];
|
||||
|
||||
pos->key = key;
|
||||
|
||||
pos->repcount = pos_repcount(pos);
|
||||
zobrist_verify(pos);
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
pos_t *move_undo_alt(pos_t *pos, const move_t move, const state_t *state)
|
||||
{
|
||||
color_t them = pos->turn, us = OPPONENT(them);
|
||||
square_t from = move_from(move), to = move_to(move);
|
||||
piece_t piece = pos->board[to];
|
||||
int up = sq_up(them);
|
||||
|
||||
if (is_promotion(move))
|
||||
piece = MAKE_PIECE(PAWN, us);
|
||||
|
||||
pos_clr_sq(pos, to); /* always clear "to" ... */
|
||||
pos_set_sq(pos, from, piece); /* ... and set "from" */
|
||||
|
||||
if (PIECE(piece) == KING)
|
||||
pos->king[us] = from;
|
||||
|
||||
if (pos->captured != EMPTY) {
|
||||
pos_set_sq(pos, to, pos->captured); /* restore captured piece */
|
||||
} else if (is_castle(move)) { /* make reverse rook move */
|
||||
square_t rookfrom, rookto;
|
||||
if (to > from) {
|
||||
rookfrom = sq_rel(F1, us);
|
||||
rookto = sq_rel(H1, us);
|
||||
} else {
|
||||
rookfrom = sq_rel(D1, us);
|
||||
rookto = sq_rel(A1, us);
|
||||
}
|
||||
pos_set_sq(pos, rookto, pos->board[rookfrom]);
|
||||
pos_clr_sq(pos, rookfrom);
|
||||
} else if (is_enpassant(move)) { /* restore grabbed pawn */
|
||||
square_t grabbed = to + up;
|
||||
pos_set_sq(pos, grabbed, MAKE_PIECE(PAWN, them));
|
||||
}
|
||||
|
||||
pos->state = *state; /* restore irreversible changes */
|
||||
pos->turn = us;
|
||||
--pos->plycount;
|
||||
--pos->plyroot;
|
||||
return pos;
|
||||
}
|
||||
26
src/move-do.h
Normal file
26
src/move-do.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/* move-do.h - move do/undo.
|
||||
*
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef MOVE_DO_H
|
||||
#define MOVE_DO_H
|
||||
|
||||
#include "position.h"
|
||||
|
||||
pos_t *move_do(pos_t *pos, const move_t move, state_t *state);
|
||||
pos_t *move_undo(pos_t *pos, const move_t move, const state_t *state);
|
||||
|
||||
/* new version testing */
|
||||
pos_t *move_do_alt(pos_t *pos, const move_t move, state_t *state);
|
||||
pos_t *move_undo_alt(pos_t *pos, const move_t move, const state_t *state);
|
||||
|
||||
#endif /* MOVE_DO_H */
|
||||
497
src/move-gen.c
Normal file
497
src/move-gen.c
Normal file
@@ -0,0 +1,497 @@
|
||||
/* move-gen.c - move generation
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "bitops.h"
|
||||
#include "bug.h"
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "board.h"
|
||||
#include "bitboard.h"
|
||||
#include "piece.h"
|
||||
#include "position.h"
|
||||
#include "move.h"
|
||||
#include "hq.h"
|
||||
#include "attack.h"
|
||||
#include "move-gen.h"
|
||||
|
||||
/**
|
||||
* pseudo_is_legal() - check if a move is legal.
|
||||
* @pos: position
|
||||
* @move: move_t to verify
|
||||
*
|
||||
* @return: true if move is valid, false otherwise.
|
||||
*/
|
||||
bool pseudo_is_legal(const pos_t *pos, const move_t move)
|
||||
{
|
||||
color_t us = pos->turn;
|
||||
color_t them = OPPONENT(us);
|
||||
square_t from = move_from(move);
|
||||
square_t to = move_to(move);
|
||||
square_t kingsq = pos->king[us];
|
||||
square_t ep = pos->en_passant;
|
||||
bitboard_t kingbb = pos->bb[us][KING];
|
||||
bitboard_t occ = pos_occ(pos);
|
||||
u64 pinned = BIT(from) & pos->blockers;
|
||||
u64 checkers = pos->checkers;
|
||||
|
||||
bug_on(pos->board[from] == NO_PIECE || COLOR(pos->board[from]) != us);
|
||||
|
||||
/* (1) - Castling & King
|
||||
* For castling, we need to check intermediate squares attacks only.
|
||||
* Attention: To test if K is in check after moving, we need to exclude
|
||||
* king from occupation bitboard (to catch king moving away from checker
|
||||
* on same line) !
|
||||
*/
|
||||
if (is_castle(move)) {
|
||||
int dir = to > from? 1: -1;
|
||||
if (sq_is_attacked(pos, occ, from + dir, them))
|
||||
return false;
|
||||
}
|
||||
if (from == kingsq) {
|
||||
return !sq_is_attacked(pos, occ ^ kingbb, to, them);
|
||||
}
|
||||
|
||||
/* (2) - King is in check
|
||||
* Double-check is already handled in (1), as only K moves were generated
|
||||
* by pseudo legal move generator.
|
||||
* Special cases (illegal):
|
||||
* - e.p., if the grabbed pawn is *not* giving check
|
||||
* - piece is pinned
|
||||
*/
|
||||
if (checkers) {
|
||||
if (pinned)
|
||||
return false;
|
||||
if (is_enpassant(move)) {
|
||||
return ep + sq_up(them) == ctz64(checkers);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* (3) - pinned pieces
|
||||
* We verify here that pinned piece P stays on line between K & dest square.
|
||||
*/
|
||||
if (pinned) {
|
||||
return bb_line[from][kingsq] & BIT(to); /* is to on pinner line ? */
|
||||
}
|
||||
|
||||
/* (4) - En-passant
|
||||
* pinned piece is already handled in (3).
|
||||
* One case not handled anywhere else: when the two "disappearing" pawns
|
||||
* would discover a R/Q horizontal check.
|
||||
* Note: grabbed pawn *cannot* discover a check (impossible position).
|
||||
*/
|
||||
if (is_enpassant(move)) {
|
||||
bitboard_t rank5 = bb_rel_rank(RANK_5, us);
|
||||
|
||||
if (kingbb & rank5) {
|
||||
bitboard_t exclude = BIT(ep + sq_up(them)) | BIT(from);
|
||||
bitboard_t rooks = (pos->bb[them][ROOK] | pos->bb[them][QUEEN]) & rank5;
|
||||
|
||||
return !(hq_rank_moves(occ ^ exclude, kingsq) & rooks);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_next_legal() - get next legal move in position.
|
||||
* @pos: position
|
||||
* @movelist: &pseudo-legal movelist
|
||||
* @start: &int, starting position in @movelist
|
||||
*
|
||||
* Get next valid move in @pos move list, from move @start, or MOVE_NONE.
|
||||
* @start is set to next non-checked move in pseudo-legal list.
|
||||
* Position pseudo-legal moves must be already calculated before calling this function.
|
||||
*
|
||||
* @return: move, or MOVE_NONE if no move.
|
||||
*/
|
||||
move_t pos_next_legal(const pos_t *pos, movelist_t *movelist, int *start)
|
||||
{
|
||||
const int nmoves = movelist->nmoves;
|
||||
const move_t *moves = movelist->move;
|
||||
move_t move;
|
||||
|
||||
while (*start < nmoves) {
|
||||
if (pseudo_is_legal(pos, (move = moves[(*start)++] )))
|
||||
return move;
|
||||
}
|
||||
return MOVE_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_legal_dup() - get legal moves from pseudo-legal ones in new list.
|
||||
* @pos: position
|
||||
* @pseudo: &movelist_t pseudo-legal moves list
|
||||
* @legal: &movelist_t legal moves
|
||||
*
|
||||
* The pseudo-legal moves must be already calculated before calling this function.
|
||||
* No check is done on @legal limits.
|
||||
* This function is similar to pos_legal(), but creates a new list for legal moves.
|
||||
* It should only be used for debug purpose, when we want to keep a copy of
|
||||
* pseudo-legal moves.
|
||||
*
|
||||
* @return: @legal
|
||||
*/
|
||||
movelist_t *pos_legal_dup(const pos_t *pos, movelist_t *pseudo, movelist_t *legal)
|
||||
{
|
||||
int tmp = legal->nmoves = 0;
|
||||
move_t move;
|
||||
|
||||
while ((move = pos_next_legal(pos, pseudo, &tmp)) != MOVE_NONE)
|
||||
legal->move[legal->nmoves++] = move;
|
||||
return legal;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_legal() - get legal moves from pseudo-legal ones in new list.
|
||||
* @pos: position
|
||||
* @list: &movelist_t pseudo-legal moves list
|
||||
*
|
||||
* The pseudo-legal moves must be already calculated before calling this function.
|
||||
* @list is replaced by legal moves.
|
||||
*
|
||||
* @return: @list
|
||||
*/
|
||||
movelist_t *pos_legal(const pos_t *pos, movelist_t *list)
|
||||
{
|
||||
move_t *cur = list->move, *last = list->move + list->nmoves;
|
||||
|
||||
while (cur < last) {
|
||||
if (pseudo_is_legal(pos, *cur))
|
||||
cur++;
|
||||
else {
|
||||
*cur = *--last;
|
||||
}
|
||||
}
|
||||
list->nmoves = last - list->move;
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* gen_pseudo_king() - generate king pseudo-legal moves.
|
||||
* @pos: position
|
||||
* @movelist: &movelist_t array to store pseudo-moves
|
||||
*
|
||||
*/
|
||||
static inline __unused move_t *gen_pseudo_king(move_t *moves, square_t from,
|
||||
bitboard_t mask)
|
||||
{
|
||||
//color_t us = pos->turn;
|
||||
//square_t from = pos->king[us];
|
||||
//bitboard_t not_my_pieces = ~pos->bb[us][ALL_PIECES];
|
||||
bitboard_t to_bb = bb_king_moves(mask, from);
|
||||
square_t to;
|
||||
while (moves) {
|
||||
to = bb_next(&to_bb);
|
||||
*moves++ = move_make(from, to);
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_gen_check_pseudomoves() - generate position pseudo-legal moves when in check
|
||||
* @pos: position
|
||||
* @movelist: &movelist_t array to store pseudo-moves
|
||||
*
|
||||
* Generate all @pos pseudo moves for player-to-move.
|
||||
* @movelist is filled with the moves.
|
||||
*
|
||||
* Only a few validity checks are done here (i.e. moves are not generated):
|
||||
* - castling, if king is in check
|
||||
* - castling, if king passes an enemy-controlled square (not final square).
|
||||
* When immediately known, a few move flags are also applied in these cases:
|
||||
* - castling: M_CASTLE
|
||||
* - en-passant: M_EN_PASSANT
|
||||
* - promotion: M_PROMOTION
|
||||
*
|
||||
* TODO: move code to specific functions (especially castling, pawn push/capture)
|
||||
*
|
||||
* @Return: The total number of moves.
|
||||
*/
|
||||
|
||||
/**
|
||||
* gen_pseudo_escape() - generate position pseudo-legal moves when in check
|
||||
* @pos: square_t king position
|
||||
* @mask: possible destinations to consider
|
||||
* @moves: &move_t array to store pseudo-moves
|
||||
*
|
||||
* Generate all @pos pseudo moves for player-to-move, when in check.
|
||||
* @movelist is filled with the moves.
|
||||
*
|
||||
* If king is doubles-checked, only King moves will be generated.
|
||||
* Otherwise, only moves where destination in between King or checker, or checker
|
||||
* square.
|
||||
*
|
||||
*/
|
||||
/*
|
||||
* static void __always_inline gen_pseudo_escape(pos_t *pos, movelist_t *movelist)
|
||||
* {
|
||||
* color_t us = pos->turn;
|
||||
* color_t them = OPPONENT(us);
|
||||
* square_t king = pos->king[us];
|
||||
*
|
||||
* gen_pseudo_king(pos, movelist);
|
||||
*
|
||||
* }
|
||||
*/
|
||||
|
||||
/**
|
||||
* moves_gen_flags() - generate all moves from square to bitboard (with flags).
|
||||
* @moves: &move_t array where to store moves
|
||||
* @from: square_t piece position
|
||||
* @to_bb: destination bitboard
|
||||
* @flags: flags to apply
|
||||
*
|
||||
* Generate (at address @moves) moves from square @from to each square in @to_bb,
|
||||
* with flags @flags.
|
||||
*
|
||||
* @Return: New @moves.
|
||||
*/
|
||||
static inline __unused move_t *moves_gen_flags(move_t *moves, square_t from,
|
||||
bitboard_t to_bb,
|
||||
__unused move_flags_t flags)
|
||||
{
|
||||
square_t to;
|
||||
while(to_bb) {
|
||||
to = bb_next(&to_bb);
|
||||
*moves++ = move_make_flags(from, to, flags);
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
/**
|
||||
* move_gen_promotions() - generate all promotions for given pawn and dest.
|
||||
* @moves: &move_t array where to store moves
|
||||
* @from: pawn position
|
||||
* @to: promotion square
|
||||
*
|
||||
* Generate (at address @moves) all promotion (Q/R/B/N) moves on @to for
|
||||
* pawn @from.
|
||||
* Actual promoted piece type is encoded as piece - 2, i.e. N = 0, B = 1,
|
||||
* R = 2, Q = 3.
|
||||
*
|
||||
* @Return: New @moves.
|
||||
*/
|
||||
static inline move_t *move_gen_promotions(move_t *moves, square_t from, square_t to)
|
||||
{
|
||||
/* your attention: "downto operator" */
|
||||
for (piece_type_t pt = QUEEN - 1; pt --> KNIGHT - 2;)
|
||||
*moves++ = move_make_promote(from, to, pt);
|
||||
return moves;
|
||||
}
|
||||
|
||||
/**
|
||||
* moves_gen() - generate all moves from square to bitboard.
|
||||
* @moves: &move_t array where to store moves
|
||||
* @from: square_t piece position
|
||||
* @to_bb: destination bitboard
|
||||
*
|
||||
* Generate (at address @moves) moves from square @from to each square in @to_bb.
|
||||
*
|
||||
* @Return: New @moves.
|
||||
*/
|
||||
static inline move_t *moves_gen(move_t *moves, square_t from, bitboard_t to_bb)
|
||||
{
|
||||
square_t to;
|
||||
// bb_print(sq_to_string(from), to_bb);
|
||||
while(to_bb) {
|
||||
to = bb_next(&to_bb);
|
||||
*moves++ = move_make(from, to);
|
||||
}
|
||||
return moves;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_gen_pseudo() - generate position pseudo-legal moves
|
||||
* @pos: position
|
||||
* @movelist: &movelist_t array to store pseudo-moves
|
||||
*
|
||||
* Generate all @pos pseudo moves for player-to-move.
|
||||
* @movelist is filled with the moves.
|
||||
*
|
||||
* Only a few validity checks are done here (i.e. moves are not generated):
|
||||
* - castling, if king is in check
|
||||
* - castling, if king passes an enemy-controlled square (not final square).
|
||||
* When immediately known, a few move flags are also applied in these cases:
|
||||
* - castling: M_CASTLE_{K,Q}
|
||||
* - capture (excl. en-passant): M_CAPTURE
|
||||
* - en-passant: M_EN_PASSANT
|
||||
* - promotion: M_PROMOTION
|
||||
* - promotion and capture
|
||||
*
|
||||
* TODO: move code to specific functions (especially castling, pawn push/capture)
|
||||
*
|
||||
* @Return: movelist
|
||||
*/
|
||||
movelist_t *pos_gen_pseudo(pos_t *pos, movelist_t *movelist)
|
||||
{
|
||||
color_t us = pos->turn;
|
||||
color_t them = OPPONENT(us);
|
||||
|
||||
bitboard_t my_pieces = pos->bb[us][ALL_PIECES];
|
||||
bitboard_t enemy_pieces = pos->bb[them][ALL_PIECES];
|
||||
bitboard_t dest_squares = ~my_pieces;
|
||||
bitboard_t occ = my_pieces | enemy_pieces;
|
||||
bitboard_t empty = ~occ;
|
||||
move_t *moves = movelist->move;
|
||||
square_t king = pos->king[us];
|
||||
|
||||
bitboard_t from_bb, to_bb;
|
||||
bitboard_t tmp_bb;
|
||||
square_t from, to;
|
||||
|
||||
/* king - MUST BE FIRST */
|
||||
to_bb = bb_king_moves(dest_squares, king);
|
||||
moves = moves_gen(moves, king, to_bb);
|
||||
|
||||
if (bb_multiple(pos->checkers)) /* double check, we stop here */
|
||||
goto finish;
|
||||
|
||||
if (pos->checkers) {
|
||||
/* one checker: we limit destination squares to line between
|
||||
* checker and king + checker square (think: knight).
|
||||
*/
|
||||
square_t checker = ctz64(pos->checkers);
|
||||
dest_squares &= bb_between[king][checker] | pos->checkers;
|
||||
enemy_pieces &= dest_squares;
|
||||
} else {
|
||||
/* no checker: castling moves
|
||||
* Attention ! Castling flags are assumed correct
|
||||
*/
|
||||
bitboard_t rel_rank1 = bb_rel_rank(RANK_1, us);
|
||||
/* For castle, we check the opponent attacks on squares between from and to.
|
||||
* To square attack check will be done in gen_is_legal.
|
||||
*/
|
||||
if (can_oo(pos->castle, us)) {
|
||||
|
||||
/* CHANGE HERE, either with bitmask >> or direct sq check */
|
||||
bitboard_t occmask = rel_rank1 & (FILE_Fbb | FILE_Gbb);
|
||||
if (!(occ & occmask)) {
|
||||
*moves++ = move_make_flags(king, king + 2, M_CASTLE);
|
||||
}
|
||||
}
|
||||
if (can_ooo(pos->castle, us)) {
|
||||
bitboard_t occmask = rel_rank1 & (FILE_Bbb | FILE_Cbb | FILE_Dbb);
|
||||
if (!(occ & occmask)) { // &&
|
||||
*moves++ = move_make_flags(king, king - 2, M_CASTLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* sliding pieces */
|
||||
from_bb = pos->bb[us][BISHOP] | pos->bb[us][QUEEN];
|
||||
while (from_bb) {
|
||||
from = bb_next(&from_bb);
|
||||
to_bb = hq_bishop_moves(occ, from) & dest_squares;
|
||||
moves = moves_gen(moves, from, to_bb);
|
||||
}
|
||||
from_bb = pos->bb[us][ROOK] | pos->bb[us][QUEEN];
|
||||
while (from_bb) {
|
||||
from = bb_next(&from_bb);
|
||||
to_bb = hq_rook_moves(occ, from) & dest_squares;
|
||||
moves = moves_gen(moves, from, to_bb);
|
||||
}
|
||||
|
||||
/* knight */
|
||||
from_bb = pos->bb[us][KNIGHT];
|
||||
while (from_bb) {
|
||||
from = bb_next(&from_bb);
|
||||
to_bb = bb_knight_moves(dest_squares, from);
|
||||
moves = moves_gen(moves, from, to_bb);
|
||||
}
|
||||
|
||||
/* pawn: relative rank and files */
|
||||
bitboard_t rel_rank8 = bb_rel_rank(RANK_8, us);
|
||||
bitboard_t rel_rank3 = bb_rel_rank(RANK_3, us);
|
||||
|
||||
/* pawn: push */
|
||||
int shift = sq_up(us);
|
||||
tmp_bb = bb_shift(pos->bb[us][PAWN], shift) & empty;
|
||||
|
||||
to_bb = tmp_bb & ~rel_rank8 & dest_squares; /* non promotion */
|
||||
while(to_bb) {
|
||||
to = bb_next(&to_bb);
|
||||
from = to - shift;
|
||||
*moves++ = move_make(from, to);
|
||||
}
|
||||
to_bb = tmp_bb & rel_rank8 & dest_squares; /* promotions */
|
||||
while(to_bb) {
|
||||
to = bb_next(&to_bb);
|
||||
from = to - shift;
|
||||
moves = move_gen_promotions(moves, from, to);
|
||||
}
|
||||
|
||||
/* possible second push */
|
||||
to_bb = bb_shift(tmp_bb & rel_rank3, shift) & empty & dest_squares;
|
||||
while(to_bb) {
|
||||
to = bb_next(&to_bb);
|
||||
from = to - shift - shift;
|
||||
*moves++ = move_make(from, to);
|
||||
}
|
||||
|
||||
/* pawn: captures */
|
||||
tmp_bb = bb_pawns_attacks(pos->bb[us][PAWN], shift) & enemy_pieces;
|
||||
to_bb = tmp_bb & ~rel_rank8;
|
||||
while (to_bb) {
|
||||
to = bb_next(&to_bb);
|
||||
from_bb = bb_pawn_attacks[them][to] & pos->bb[us][PAWN];
|
||||
while (from_bb) {
|
||||
from = bb_next(&from_bb);
|
||||
*moves++ = move_make(from, to);
|
||||
}
|
||||
}
|
||||
to_bb = tmp_bb & rel_rank8;
|
||||
while (to_bb) {
|
||||
to = bb_next(&to_bb);
|
||||
from_bb = bb_pawn_attacks[them][to] & pos->bb[us][PAWN];
|
||||
while (from_bb) {
|
||||
from = bb_next(&from_bb);
|
||||
moves = move_gen_promotions(moves, from, to);
|
||||
}
|
||||
}
|
||||
|
||||
/* pawn: en-passant
|
||||
* TODO: special case when in-check here ?
|
||||
*/
|
||||
if ((to = pos->en_passant) != SQUARE_NONE) {
|
||||
from_bb = bb_pawn_attacks[them][to] & pos->bb[us][PAWN];
|
||||
while (from_bb) {
|
||||
from = bb_next(&from_bb);
|
||||
*moves++ = move_make_enpassant(from, to);
|
||||
}
|
||||
}
|
||||
|
||||
/* TODO: add function per piece, and type, for easier debug
|
||||
*/
|
||||
finish:
|
||||
movelist->nmoves = moves - movelist->move;
|
||||
return movelist;
|
||||
//return movelist->nmoves = moves - movelist->move;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_gen_legal() - generate position legal moves
|
||||
* @pos: position
|
||||
* @movelist: &movelist_t array to store moves
|
||||
*
|
||||
* Generate all @pos legal moves for player-to-move.
|
||||
* @movelist is filled with the moves.
|
||||
*
|
||||
* @Return: movelist
|
||||
*/
|
||||
movelist_t *pos_gen_legal(pos_t *pos, movelist_t *movelist)
|
||||
{
|
||||
return pos_legal(pos, pos_gen_pseudo(pos, movelist));
|
||||
}
|
||||
32
src/move-gen.h
Normal file
32
src/move-gen.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/* move-gen.h - move generation
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef MOVEGEN_H
|
||||
#define MOVEGEN_H
|
||||
|
||||
#include "bitops.h"
|
||||
|
||||
#include "bitboard.h"
|
||||
#include "hq.h"
|
||||
#include "piece.h"
|
||||
#include "move.h"
|
||||
|
||||
bool pseudo_is_legal(const pos_t *pos, const move_t move);
|
||||
move_t pos_next_legal(const pos_t *pos, movelist_t *movelist, int *start);
|
||||
movelist_t *pos_legal_dup(const pos_t *pos, movelist_t *pseudo, movelist_t *legal);
|
||||
movelist_t *pos_legal(const pos_t *pos, movelist_t *list);
|
||||
|
||||
movelist_t *pos_gen_pseudo(pos_t *pos, movelist_t *movelist);
|
||||
movelist_t *pos_gen_legal(pos_t *pos, movelist_t *movelist);
|
||||
|
||||
#endif /* MOVEGEN_H */
|
||||
1006
src/move.c
1006
src/move.c
File diff suppressed because it is too large
Load Diff
172
src/move.h
172
src/move.h
@@ -1,6 +1,6 @@
|
||||
/* move.h - move management.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -15,20 +15,127 @@
|
||||
#define MOVE_H
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "position.h"
|
||||
#include "pool.h"
|
||||
#include "piece.h"
|
||||
#include "board.h"
|
||||
|
||||
/* move flags
|
||||
/* move structure:
|
||||
* 11 11 1
|
||||
* 54 32 1 6 5 0
|
||||
* FF pp tttttt ffffff
|
||||
*
|
||||
* bits len off range type mask get desc
|
||||
* ffffff 6 0 0-5 square_t 077 &077 from
|
||||
* tttttt 6 6 6-11 square_t 07700 (>>6) &077 to
|
||||
* pp 2 12 12-13 piece_type_t 030000 (>>12) &03 promoted
|
||||
* FF 2 14 14-15 move_flags_t 0140000 (>>14) &03 flags
|
||||
*/
|
||||
typedef u16 move_t;
|
||||
|
||||
enum {
|
||||
M_OFF_FROM = 0,
|
||||
M_OFF_TO = 6,
|
||||
M_OFF_PROMOTED = 12,
|
||||
M_OFF_FLAGS = 14
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
M_PROMOTED_MASK = 0030000,
|
||||
M_FLAGS_MASK = 0140000,
|
||||
|
||||
M_ENPASSANT = 040000, /* 1 << M_OFF_FLAGS */
|
||||
M_CASTLE = 0100000, /* 2 << M_OFF_FLAGS */
|
||||
M_PROMOTION = 0140000, /* 3 << M_OFF_FLAGS */
|
||||
} move_flags_t;
|
||||
|
||||
/* special move_t values */
|
||||
#define MOVE_NULL 0 /* hack: from = to = A1 */
|
||||
#define MOVE_NONE 07777 /* hack: from = to = H8 */
|
||||
|
||||
#define move_set_flags(move, flags) ((move) | (flags))
|
||||
#define move_flags(move) ((move) & M_FLAGS_MASK)
|
||||
|
||||
#define is_promotion(m) (move_flags(m) == M_PROMOTION)
|
||||
#define is_enpassant(m) (move_flags(m) == M_ENPASSANT)
|
||||
#define is_castle(m) (move_flags(m) == M_CASTLE)
|
||||
// #define is_check(m) (move_flags(m) == M_CHECK)
|
||||
|
||||
#define MOVES_MAX 256
|
||||
|
||||
typedef struct __movelist_s {
|
||||
move_t move[MOVES_MAX];
|
||||
int nmoves; /* total moves (fill) */
|
||||
} movelist_t;
|
||||
|
||||
static inline square_t move_from(move_t move)
|
||||
{
|
||||
return move & 077;
|
||||
}
|
||||
|
||||
static inline square_t move_to(move_t move)
|
||||
{
|
||||
return (move >> M_OFF_TO) & 077;
|
||||
}
|
||||
|
||||
static inline move_t move_fromto(move_t move)
|
||||
{
|
||||
return move & 07777;
|
||||
}
|
||||
|
||||
static inline piece_type_t move_promoted(move_t move)
|
||||
{
|
||||
return ((move >> M_OFF_PROMOTED) & 03) + KNIGHT;
|
||||
}
|
||||
|
||||
/*
|
||||
* static inline piece_type_t move_captured(move_t move)
|
||||
* {
|
||||
* return (move >> M_OFF_CAPTURED) & 07;
|
||||
* }
|
||||
*/
|
||||
|
||||
static inline move_t move_make(square_t from, square_t to)
|
||||
{
|
||||
return (to << M_OFF_TO) | from;
|
||||
}
|
||||
|
||||
static inline move_t move_make_flags(square_t from, square_t to, move_flags_t flags)
|
||||
{
|
||||
return (to << M_OFF_TO) | from | flags;
|
||||
//move_set_flags(move_make(from, to), flags);
|
||||
}
|
||||
|
||||
/*
|
||||
* static inline move_t move_make_capture(square_t from, square_t to)
|
||||
* {
|
||||
* return move_make_flags(from, to, M_CAPTURE);
|
||||
* }
|
||||
*/
|
||||
|
||||
static inline move_t move_make_enpassant(square_t from, square_t to)
|
||||
{
|
||||
return move_make_flags(from, to, M_ENPASSANT);
|
||||
}
|
||||
|
||||
static inline move_t move_make_promote(square_t from, square_t to,
|
||||
piece_type_t promoted)
|
||||
{
|
||||
return move_make_flags(from, to, M_PROMOTION) | (promoted << M_OFF_PROMOTED);
|
||||
}
|
||||
|
||||
/*
|
||||
* static inline move_t move_make_promote_capture(square_t from, square_t to,
|
||||
* piece_type_t promoted)
|
||||
* {
|
||||
* return move_make_promote(from, to, promoted) | M_CAPTURE;
|
||||
* }
|
||||
*/
|
||||
|
||||
/*
|
||||
* static inline move_t move_set_captured(move_t move, piece_type_t captured)
|
||||
* {
|
||||
* return move | (captured << M_OFF_CAPTURED);
|
||||
* }
|
||||
*/
|
||||
typedef unsigned char move_flags_t;
|
||||
#define M_NORMAL 0x00
|
||||
#define M_CHECK 0x01 /* unsure if we know */
|
||||
#define M_CAPTURE 0x02
|
||||
#define M_EN_PASSANT 0x04
|
||||
#define M_PROMOTION 0x08
|
||||
#define M_CASTLE_K 0x10
|
||||
#define M_CASTLE_Q 0x20
|
||||
|
||||
/* moves_print flags
|
||||
*/
|
||||
@@ -36,44 +143,15 @@ typedef unsigned char move_flags_t;
|
||||
#define M_PR_NCAPT 0x02
|
||||
#define M_PR_NUM 0x04
|
||||
#define M_PR_NL 0x08
|
||||
#define M_UCI 0x10 /* UCI style - e7e8q */
|
||||
#define M_PR_EVAL 0x20 /* separate captures */
|
||||
#define M_PR_SEPARATE 0x40 /* separate captures */
|
||||
#define M_PR_LONG 0x80
|
||||
|
||||
typedef struct move_s {
|
||||
piece_t piece;
|
||||
square_t from, to;
|
||||
piece_t capture; /* captured piece */
|
||||
piece_t promotion; /* promoted piece */
|
||||
move_flags_t flags;
|
||||
eval_t negamax;
|
||||
eval_t eval;
|
||||
eval_t eval_simple;
|
||||
pos_t *pos;
|
||||
struct list_head list; /* next move */
|
||||
} move_t;
|
||||
|
||||
pool_t *moves_pool_init();
|
||||
void moves_pool_stats();
|
||||
int move_print(int movenum, move_t *move, move_flags_t flags);
|
||||
void moves_print(pos_t *move, move_flags_t flags);
|
||||
|
||||
void move_del(struct list_head *ptr);
|
||||
int moves_del(pos_t *pos);
|
||||
|
||||
int pseudo_moves_castle(pos_t *pos, bool color, bool doit, bool do_king);
|
||||
int pseudo_moves_gen(pos_t *pos, piece_list_t *piece, bool doit, bool do_king);
|
||||
int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece, bool doit);
|
||||
int moves_gen(pos_t *pos, bool color, bool doit, bool do_king);
|
||||
int moves_gen_king_moves(pos_t *pos, bool color, bool doit);
|
||||
|
||||
void moves_sort(pos_t *pos);
|
||||
void moves_gen_eval_sort(pos_t *pos);
|
||||
|
||||
void moves_gen_all(pos_t *pos);
|
||||
void moves_gen_all_nomoves(pos_t *pos);
|
||||
|
||||
pos_t *move_do(pos_t *pos, move_t *move);
|
||||
void move_undo(pos_t *pos, move_t *move);
|
||||
char *move_to_str(char *dst, const move_t move, __unused const int flags);
|
||||
move_t move_from_str(const char *str);
|
||||
move_t move_find_in_movelist(move_t target, movelist_t *list);
|
||||
void moves_print(movelist_t *moves, int flags);
|
||||
void move_sort_by_sq(movelist_t *moves);
|
||||
|
||||
#endif /* MOVE_H */
|
||||
|
||||
194
src/perft.c
Normal file
194
src/perft.c
Normal file
@@ -0,0 +1,194 @@
|
||||
/* perft.c - perft functions.
|
||||
*
|
||||
* Copyright (C) 2023-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <brlib.h>
|
||||
|
||||
#include "perft.h"
|
||||
#include "move-gen.h"
|
||||
#include "move-do.h"
|
||||
|
||||
/**
|
||||
* do_perft() - perft engine
|
||||
* @pos: &position to search
|
||||
* @depth: Wanted depth.
|
||||
*
|
||||
* Run perft on a position.
|
||||
*
|
||||
* This version uses the algorithm:
|
||||
* gen legal moves
|
||||
* if last depth
|
||||
* return number of legal move
|
||||
* loop for legal move
|
||||
* do-move
|
||||
* if depth == 2
|
||||
*
|
||||
* do_perft (depth -1)
|
||||
* undo-move
|
||||
*
|
||||
* @return: total moves found at @depth level.
|
||||
*/
|
||||
static u64 do_perft(pos_t *pos, int depth)
|
||||
{
|
||||
u64 nodes = 0;
|
||||
movelist_t movelist;
|
||||
move_t *move, *last;
|
||||
state_t state;
|
||||
|
||||
pos_set_checkers_pinners_blockers(pos);
|
||||
pos_legal(pos, pos_gen_pseudo(pos, &movelist));
|
||||
|
||||
//if (depth == 1)
|
||||
// return movelist.nmoves;
|
||||
|
||||
last = movelist.move + movelist.nmoves;
|
||||
switch (depth) {
|
||||
case 1:
|
||||
/* This case could be removed if 'case 2' is handled in perft()
|
||||
*/
|
||||
return movelist.nmoves;
|
||||
break;
|
||||
case 2:
|
||||
/* For depth 2, we directly calculate the possible legal moves
|
||||
* after each possible moves.
|
||||
*/
|
||||
for (move = movelist.move; move < last; ++move) {
|
||||
move_do(pos, *move, &state);
|
||||
movelist_t movelist2;
|
||||
pos_set_checkers_pinners_blockers(pos);
|
||||
nodes += pos_legal(pos, pos_gen_pseudo(pos, &movelist2))->nmoves;
|
||||
move_undo(pos, *move, &state);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
/* Default: Search in TT for same key+depth. Use it if found, create
|
||||
* it otherwise.
|
||||
*/
|
||||
for (move = movelist.move; move < last; ++move) {
|
||||
move_do(pos, *move, &state);
|
||||
hentry_t *entry = tt_probe_perft(pos->key, depth);
|
||||
if (entry != TT_MISS) {
|
||||
nodes += HASH_PERFT_VAL(entry->data);
|
||||
} else {
|
||||
u64 subnodes = do_perft(pos, depth - 1);
|
||||
tt_store_perft(pos->key, depth, subnodes);
|
||||
nodes += subnodes;
|
||||
}
|
||||
move_undo(pos, *move, &state);
|
||||
}
|
||||
//} else {
|
||||
//subnodes = do_perft(pos, depth - 1);
|
||||
}
|
||||
//move_undo(pos, *move, &state);
|
||||
//nodes += subnodes;
|
||||
//}
|
||||
|
||||
return nodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* perft() - Perform perft on position
|
||||
* @pos: &position to search
|
||||
* @depth: Wanted depth.
|
||||
* @ply: current perft depth level (root = 1)
|
||||
* @divide: output total for 1st level moves.
|
||||
*
|
||||
* Run perft on a position. This function displays the available moves at @depth
|
||||
* level for each possible first move, and the total of moves.
|
||||
*
|
||||
* This version uses the algorithm:
|
||||
* if last depth
|
||||
* return 1;
|
||||
* gen legal moves
|
||||
* loop for legal move
|
||||
* do-move
|
||||
* perft (depth -1)
|
||||
* undo-move
|
||||
*
|
||||
* @return: total moves found at @depth level.
|
||||
*/
|
||||
u64 perft(pos_t *pos, int depth, bool divide)
|
||||
{
|
||||
u64 subnodes = 0, nodes = 0;
|
||||
movelist_t movelist;
|
||||
move_t *move, *last;
|
||||
state_t state;
|
||||
|
||||
pos_set_checkers_pinners_blockers(pos);
|
||||
|
||||
pos_legal(pos, pos_gen_pseudo(pos, &movelist));
|
||||
last = movelist.move + movelist.nmoves;
|
||||
for (move = movelist.move; move < last; ++move) {
|
||||
if (depth == 1) {
|
||||
subnodes = 1;
|
||||
} else {
|
||||
move_do(pos, *move, &state);
|
||||
subnodes = do_perft(pos, depth - 1);
|
||||
move_undo(pos, *move, &state);
|
||||
}
|
||||
if (divide) {
|
||||
char movestr[8];
|
||||
printf("%s: %lu\n", move_to_str(movestr, *move, 0), subnodes);
|
||||
}
|
||||
nodes += subnodes;
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* perft_alt() - Perform perft on position, experimental version.
|
||||
* @pos: &position to search
|
||||
* @depth: Wanted depth.
|
||||
* @ply: current perft depth level (root = 1)
|
||||
* @divide: output total for 1st level moves.
|
||||
*
|
||||
* Run perft on a position. This function displays the available moves at @depth
|
||||
* level for each possible first move, and the total of moves.
|
||||
*
|
||||
* @return: total moves found at @depth level.
|
||||
*/
|
||||
u64 perft_alt(pos_t *pos, int depth, int ply, bool divide)
|
||||
{
|
||||
u64 subnodes = 0, nodes = 0;
|
||||
movelist_t movelist;
|
||||
move_t *move, *last;
|
||||
state_t state;
|
||||
|
||||
pos_set_checkers_pinners_blockers(pos);
|
||||
|
||||
pos_legal(pos, pos_gen_pseudo(pos, &movelist));
|
||||
last = movelist.move + movelist.nmoves;
|
||||
for (move = movelist.move; move < last; ++move) {
|
||||
if (depth == 1) {
|
||||
subnodes = 1;
|
||||
} else {
|
||||
move_do_alt(pos, *move, &state);
|
||||
if (depth == 2) {
|
||||
movelist_t movelist2;
|
||||
pos_set_checkers_pinners_blockers(pos);
|
||||
subnodes = pos_legal(pos, pos_gen_pseudo(pos, &movelist2))->nmoves;
|
||||
} else {
|
||||
subnodes = perft_alt(pos, depth - 1, ply + 1, divide);
|
||||
}
|
||||
move_undo_alt(pos, *move, &state);
|
||||
}
|
||||
nodes += subnodes;
|
||||
if (ply == 1 && divide) {
|
||||
char movestr[8];
|
||||
printf("%s: %lu\n", move_to_str(movestr, *move, 0), subnodes);
|
||||
}
|
||||
}
|
||||
|
||||
return nodes;
|
||||
}
|
||||
22
src/perft.h
Normal file
22
src/perft.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/* perft.h.h - perft.
|
||||
*
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef PERFT_H
|
||||
#define PERFT_H
|
||||
|
||||
#include "position.h"
|
||||
|
||||
u64 perft(pos_t *pos, int depth,/* int ply,*/ bool divide);
|
||||
u64 perft_alt(pos_t *pos, int depth, int ply, bool output);
|
||||
|
||||
#endif /* PERFT_H */
|
||||
210
src/piece.c
210
src/piece.c
@@ -1,6 +1,6 @@
|
||||
/* piece.c - piece list management.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -13,157 +13,87 @@
|
||||
|
||||
#include <malloc.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <debug.h>
|
||||
#include <pool.h>
|
||||
#include <list.h>
|
||||
#include "bug.h"
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "piece.h"
|
||||
#include "board.h"
|
||||
#include "bitboard.h"
|
||||
#include "position.h"
|
||||
|
||||
static pool_t *pieces_pool;
|
||||
|
||||
struct piece_details piece_details[] = {
|
||||
[E_EMPTY] = { ' ', ' ', " ", " ", "", 0 },
|
||||
[E_PAWN] = { 'P', 'p', "♙", "♟", "Pawn", PAWN_VALUE },
|
||||
[E_KNIGHT] = { 'N', 'n', "♘", "♞", "Knight", KNIGHT_VALUE },
|
||||
[E_BISHOP] = { 'B', 'b', "♗", "♝", "Bishop", BISHOP_VALUE },
|
||||
[E_ROOK] = { 'R', 'r', "♖", "♜", "Rook", ROOK_VALUE },
|
||||
[E_QUEEN] = { 'Q', 'q', "♕", "♛", "Queen", QUEEN_VALUE },
|
||||
[E_KING] = { 'K', 'k', "♔", "♚", "King", KING_VALUE }
|
||||
/**
|
||||
* piece_details
|
||||
*/
|
||||
const struct piece_details piece_details[PIECE_NB] = {
|
||||
/* cap low fen sym name midgame val endgame val */
|
||||
[EMPTY] = { "", "", "", "", "", 0, 0 },
|
||||
[W_PAWN] = { "", "", "P", "♙", "Pawn", P_VAL_MID, P_VAL_END },
|
||||
[W_KNIGHT] = { "N", "n", "N", "♘", "Knight", N_VAL_MID, N_VAL_END },
|
||||
[W_BISHOP] = { "B", "b", "B", "♗", "Bishop", B_VAL_MID, B_VAL_END },
|
||||
[W_ROOK] = { "R", "r", "R", "♖", "Rook", R_VAL_MID, R_VAL_END },
|
||||
[W_QUEEN] = { "Q", "q", "Q", "♕", "Queen", Q_VAL_MID, Q_VAL_END },
|
||||
[W_KING] = { "K", "k", "K", "♔", "King", K_VAL_MID, K_VAL_END },
|
||||
[7] = { "", "", "", "", "", 0, 0 },
|
||||
[8] = { "", "", "", "", "", 0, 0 },
|
||||
[B_PAWN] = { "", "", "p", "♟", "Pawn", P_VAL_MID, P_VAL_END },
|
||||
[B_KNIGHT] = { "N", "n", "n", "♞", "Knight", N_VAL_MID, N_VAL_END },
|
||||
[B_BISHOP] = { "B", "b", "b", "♝", "Bishop", B_VAL_MID, B_VAL_END },
|
||||
[B_ROOK] = { "R", "r", "r", "♜", "Rook", R_VAL_MID, R_VAL_END },
|
||||
[B_QUEEN] = { "Q", "q", "q", "♛", "Queen", Q_VAL_MID, Q_VAL_END },
|
||||
[B_KING] = { "K", "k", "k", "♚", "King", K_VAL_MID, K_VAL_END },
|
||||
};
|
||||
|
||||
void piece_list_print(struct list_head *list)
|
||||
const char pieces_str[6+6+1] = "PNBRQKpnbrqk";
|
||||
|
||||
bool piece_ok(piece_t p)
|
||||
{
|
||||
struct list_head *p_cur, *tmp;
|
||||
piece_list_t *piece;
|
||||
piece_type_t pt = PIECE(p);
|
||||
return !(p & ~(MASK_COLOR | MASK_PIECE)) && pt && (pt <= KING);
|
||||
}
|
||||
|
||||
list_for_each_safe(p_cur, tmp, list) {
|
||||
piece = list_entry(p_cur, piece_list_t, list);
|
||||
char *piece_to_cap(piece_t p)
|
||||
{
|
||||
return piece_details[p].cap;
|
||||
}
|
||||
|
||||
printf("%s%c%c ", P_SYM(piece->piece),
|
||||
FILE2C(F88(piece->square)),
|
||||
RANK2C(R88(piece->square)));
|
||||
char *piece_to_char(piece_t p)
|
||||
{
|
||||
return piece_details[p].fen;
|
||||
}
|
||||
|
||||
char *piece_to_low(piece_t p)
|
||||
{
|
||||
return piece_details[p].low;
|
||||
}
|
||||
|
||||
char *piece_to_sym(piece_t p)
|
||||
{
|
||||
return piece_details[p].sym;
|
||||
}
|
||||
|
||||
char *piece_to_name(piece_t p)
|
||||
{
|
||||
return piece_details[p].name;
|
||||
}
|
||||
|
||||
piece_type_t piece_t_from_char(char c)
|
||||
{
|
||||
char *p;
|
||||
piece_type_t pt = NO_PIECE_TYPE;
|
||||
if (c && (p = strchr(pieces_str, c))) {
|
||||
pt = (p - pieces_str) % 6 + 1;
|
||||
}
|
||||
printf("\n");
|
||||
return pt;
|
||||
}
|
||||
|
||||
pool_t *piece_pool_init()
|
||||
//piece_type_t piece_from_promotion(char c, color_t color)
|
||||
//{
|
||||
// piece_type_t piece = piece_t_from_char(c);
|
||||
// return piece? SET_COLOR()p? (p - pieces_str) % 6 + 1: NO_PIECE_TYPE;
|
||||
//}
|
||||
|
||||
piece_t piece_from_char(char c)
|
||||
{
|
||||
if (!pieces_pool)
|
||||
pieces_pool = pool_create("pieces", 128, sizeof(piece_list_t));
|
||||
return pieces_pool;
|
||||
piece_type_t piece = piece_t_from_char(c);
|
||||
return isupper(c)? SET_WHITE(piece): SET_BLACK(piece);
|
||||
}
|
||||
|
||||
void piece_pool_stats()
|
||||
{
|
||||
if (pieces_pool)
|
||||
pool_stats(pieces_pool);
|
||||
}
|
||||
|
||||
piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square)
|
||||
{
|
||||
piece_list_t *new;
|
||||
short color = COLOR(piece);
|
||||
|
||||
# ifdef DEBUG_PIECE
|
||||
log_f(3, "piece=%02x square=%02x\n", piece, square);
|
||||
log_f(5, "Adding %s %s on %c%c\n", color? "Black": "White",
|
||||
P_NAME(piece), FILE2C(F88(square)), RANK2C(R88(square)));
|
||||
# endif
|
||||
if ((new = pool_get(pieces_pool))) {
|
||||
/* first piece is always king */
|
||||
if (PIECE(piece) == KING)
|
||||
list_add(&new->list, &pos->pieces[color]);
|
||||
else
|
||||
list_add_tail(&new->list, &pos->pieces[color]);
|
||||
new->piece = piece;
|
||||
new->square = square;
|
||||
new->castle = 0;
|
||||
new-> value = piece_details[PIECE(piece)].value;
|
||||
}
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
void piece_del(struct list_head *ptr)
|
||||
{
|
||||
piece_list_t *piece = list_entry(ptr, piece_list_t, list);
|
||||
# ifdef DEBUG_PIECE
|
||||
log_f(3, "piece=%02x square=%02x\n", piece->piece, piece->square);
|
||||
# endif
|
||||
list_del(ptr);
|
||||
pool_add(pieces_pool, piece);
|
||||
return;
|
||||
}
|
||||
|
||||
int pieces_del(pos_t *pos, short color)
|
||||
{
|
||||
struct list_head *p_cur, *tmp, *head;
|
||||
int count = 0;
|
||||
|
||||
head = &pos->pieces[color];
|
||||
|
||||
list_for_each_safe(p_cur, tmp, head) {
|
||||
piece_del(p_cur);
|
||||
count++;
|
||||
}
|
||||
# ifdef DEBUG_PIECE
|
||||
log_f(3, "color=%d removed=%d\n", color, count);
|
||||
# endif
|
||||
return count;
|
||||
}
|
||||
|
||||
#ifdef BIN_piece
|
||||
#include "fen.h"
|
||||
int main(int ac, char**av)
|
||||
{
|
||||
pos_t *pos;
|
||||
printf("zobi\n");fflush(stdout);
|
||||
debug_init(6, stderr, true);
|
||||
log_f(5, "kfsjdhg\n");
|
||||
pos_pool_init();
|
||||
pos = pos_get();
|
||||
piece_pool_init();
|
||||
|
||||
if (ac == 1) {
|
||||
printf("zoba\n");fflush(stdout);
|
||||
pos_startpos(pos);
|
||||
} else {
|
||||
fen2pos(pos, av[1]);
|
||||
}
|
||||
pos_print(pos);
|
||||
pos_pieces_print(pos);
|
||||
|
||||
printf("0x1c = 11100 = C1-E1:\n");
|
||||
bitboard_print(0x1c);
|
||||
|
||||
printf("0x70 = 111 = A1-C1\n");
|
||||
bitboard_print(0x70);
|
||||
|
||||
printf("0x0e = 1110 = B1-D1\n");
|
||||
bitboard_print(0x0e);
|
||||
|
||||
printf("0x60 = 1100000 = F1-G1\n");
|
||||
bitboard_print(0x60);
|
||||
|
||||
printf("A1:\n");
|
||||
bitboard_print(A1);
|
||||
|
||||
printf("1:\n");
|
||||
bitboard_print(1L);
|
||||
printf("H1:\n");
|
||||
bitboard_print(H1);
|
||||
printf("C1:\n");
|
||||
bitboard_print(C1);
|
||||
printf("D1:\n");
|
||||
bitboard_print(D1);
|
||||
printf("C1|D1:\n");
|
||||
bitboard_print(C1|D1);
|
||||
printf("H8:\n");
|
||||
bitboard_print(H8);
|
||||
}
|
||||
#endif
|
||||
|
||||
156
src/piece.h
156
src/piece.h
@@ -1,6 +1,6 @@
|
||||
/* piece.h - piece definitions.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -14,58 +14,126 @@
|
||||
#ifndef PIECE_H
|
||||
#define PIECE_H
|
||||
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "list.h"
|
||||
#include "pool.h"
|
||||
|
||||
#define PIECE_DEFAULT_VALUE 0
|
||||
/* piece_t bits structure
|
||||
* piece is on bits 1-3, color on bit 4:
|
||||
* .... CPPP
|
||||
* C: 0 for white, 1: black
|
||||
* PPP: pawn (1), knight, bishop, rook, queen, king (6)
|
||||
*/
|
||||
enum {
|
||||
WHITE, BLACK,
|
||||
COLOR_NB
|
||||
};
|
||||
typedef u8 color_t;
|
||||
|
||||
/* initial default values */
|
||||
#define PAWN_VALUE 100
|
||||
#define KNIGHT_VALUE 300
|
||||
#define BISHOP_VALUE 300
|
||||
#define ROOK_VALUE 500
|
||||
#define QUEEN_VALUE 900
|
||||
#define KING_VALUE 20000
|
||||
enum {
|
||||
ALL_PIECES = 0, /* 'all pieces' bitboard */
|
||||
NO_PIECE_TYPE = 0,
|
||||
PAWN = 1, KNIGHT, BISHOP, ROOK, QUEEN, KING,
|
||||
PIECE_TYPE_NB,
|
||||
PT_NB = PIECE_TYPE_NB
|
||||
};
|
||||
typedef u8 piece_type_t;
|
||||
|
||||
typedef struct piece_list_s {
|
||||
piece_t piece;
|
||||
square_t square;
|
||||
short castle;
|
||||
s64 value;
|
||||
struct list_head list;
|
||||
} piece_list_t;
|
||||
enum __piece_e {
|
||||
EMPTY = 0,
|
||||
NO_PIECE = 0,
|
||||
W_PAWN = PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
|
||||
B_PAWN = PAWN | 8, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING,
|
||||
PIECE_NB
|
||||
};
|
||||
typedef u8 piece_t;
|
||||
|
||||
/* default values for midgame, endgame
|
||||
*/
|
||||
#define E_VAL_MID 0
|
||||
#define P_VAL_MID 100
|
||||
#define N_VAL_MID 300
|
||||
#define B_VAL_MID 300
|
||||
#define R_VAL_MID 500
|
||||
#define Q_VAL_MID 900
|
||||
#define K_VAL_MID 10000
|
||||
|
||||
#define E_VAL_END 0
|
||||
#define P_VAL_END 100
|
||||
#define N_VAL_END 300
|
||||
#define B_VAL_END 300
|
||||
#define R_VAL_END 500
|
||||
#define Q_VAL_END 900
|
||||
#define K_VAL_END 10000
|
||||
|
||||
/* some default values for pieces
|
||||
* @abbr: char, piece capital letter (used for game notation)
|
||||
* @abbr_c: char, capital for white, lowercase for black
|
||||
* char *sym;
|
||||
* char *sym_c;
|
||||
* char *name;
|
||||
* s64 opn_value;
|
||||
* s64 mid_value;
|
||||
* s64 end_value;
|
||||
*/
|
||||
extern struct piece_details {
|
||||
char abbrev_w; /* used for game notation */
|
||||
char abbrev_b;
|
||||
char *symbol_w;
|
||||
char *symbol_b; /* used for game notation */
|
||||
char *name;
|
||||
s64 value;
|
||||
} piece_details[];
|
||||
extern const struct piece_details {
|
||||
char *cap; /* used for game notation */
|
||||
char *low; /* used also for UCI promotion */
|
||||
char *fen; /* cap=white, low=black */
|
||||
char *sym; /* UTF-8 symbol */
|
||||
char *name; /* piece name */
|
||||
s16 mid_value; /* value midgame */
|
||||
s16 end_value; /* value endgame */
|
||||
} piece_details[PIECE_NB];
|
||||
|
||||
#define P_NAME(p) piece_details[E_PIECE(p)].name
|
||||
#define P_LETTER(p) piece_details[E_PIECE(p)].abbrev_w
|
||||
#define P_SYM(p) piece_details[E_PIECE(p)].symbol_b
|
||||
#define P_CSHORT(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].abbrev_w: \
|
||||
piece_details[E_PIECE(p)].abbrev_b)
|
||||
#define P_CSYM(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].symbol_w: \
|
||||
piece_details[E_PIECE(p)].symbol_b)
|
||||
#define P_VALUE(p) (piece_details[E_PIECE(p)].value)
|
||||
/* use short name or symbol - no effect
|
||||
*/
|
||||
#define P_USE_UTF 1
|
||||
extern const char pieces_str[6+6+1]; /* to search from fen/user input */
|
||||
|
||||
void piece_list_print(struct list_head *list);
|
||||
pool_t *piece_pool_init();
|
||||
void piece_pool_stats();
|
||||
piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square);
|
||||
void piece_del(struct list_head *ptr);
|
||||
int pieces_del(pos_t *pos, short color);
|
||||
#define OPPONENT(color) !(color)
|
||||
|
||||
#define MASK_PIECE 0x07 /* 00000111 */
|
||||
#define MASK_COLOR 0x08 /* 00001000 */
|
||||
|
||||
#define COLOR(p) ((p) >> 3) /* bitmask */
|
||||
#define PIECE(p) ((p) & MASK_PIECE)
|
||||
#define MAKE_PIECE(p, c) ((p) | (c) << 3)
|
||||
|
||||
#define IS_BLACK(p) ((p) & MASK_COLOR)
|
||||
#define IS_WHITE(p) (! IS_BLACK(p))
|
||||
|
||||
#define SET_WHITE(p) (piece_t)((p) &= ~MASK_COLOR)
|
||||
#define SET_BLACK(p) (piece_t)((p) |= MASK_COLOR)
|
||||
#define SET_COLOR(p, c) (piece_t)(!(c)? SET_WHITE(p): SET_BLACK(p))
|
||||
|
||||
static __inline eval_t piece_midval(piece_type_t pt)
|
||||
{
|
||||
return piece_details[pt].mid_value;
|
||||
}
|
||||
static __inline eval_t piece_endval(piece_type_t pt)
|
||||
{
|
||||
return piece_details[pt].end_value;
|
||||
}
|
||||
|
||||
bool piece_ok(piece_t p);
|
||||
|
||||
char *piece_to_cap(piece_t p);
|
||||
char *piece_to_low(piece_t p);
|
||||
char *piece_to_fen(piece_t p);
|
||||
char *piece_to_sym(piece_t p);
|
||||
char *piece_to_name(piece_t p);
|
||||
|
||||
#define piece_to_char(c) piece_to_fen(c)
|
||||
#define piece_to_uci(p) piece_to_low(p)
|
||||
|
||||
piece_type_t piece_t_from_char(char c);
|
||||
piece_t piece_from_fen(char c);
|
||||
|
||||
#define piece_from_char(c) piece_from_fen(c)
|
||||
|
||||
//void piece_list_print(struct list_head *list);
|
||||
//pool_t *piece_pool_init();
|
||||
//void piece_pool_stats();
|
||||
//piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square);
|
||||
//void piece_del(struct list_head *ptr);
|
||||
//int pieces_del(pos_t *pos, short color);
|
||||
|
||||
#endif
|
||||
|
||||
726
src/position.c
726
src/position.c
@@ -1,6 +1,6 @@
|
||||
/* position.c - position management.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -16,306 +16,482 @@
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <brlib.h>
|
||||
#include <bitops.h>
|
||||
#include <bug.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "position.h"
|
||||
#include "move.h"
|
||||
#include "bitboard.h"
|
||||
#include "hq.h"
|
||||
#include "fen.h"
|
||||
#include "piece.h"
|
||||
#include "eval.h"
|
||||
|
||||
static pool_t *pos_pool;
|
||||
|
||||
#define BYTE_PRINT "%c%c%c%c%c%c%c%c"
|
||||
#define BYTE2BIN(b) ((b) & 0x01 ? '1' : '0'), \
|
||||
((b) & 0x02 ? '1' : '0'), \
|
||||
((b) & 0x04 ? '1' : '0'), \
|
||||
((b) & 0x08 ? '1' : '0'), \
|
||||
((b) & 0x10 ? '1' : '0'), \
|
||||
((b) & 0x20 ? '1' : '0'), \
|
||||
((b) & 0x40 ? '1' : '0'), \
|
||||
((b) & 0x80 ? '1' : '0')
|
||||
|
||||
inline void bitboard_print(bitboard_t bb)
|
||||
{
|
||||
int i;
|
||||
printf("%#018lx\n", bb);
|
||||
for (i=56; i>=0; i-=8)
|
||||
printf("\t"BYTE_PRINT"\n",
|
||||
BYTE2BIN(bb>>i));
|
||||
}
|
||||
|
||||
inline void bitboard_print2(bitboard_t bb1, bitboard_t bb2)
|
||||
{
|
||||
int i;
|
||||
printf("\tW: %#018lx\tB: %#018lx\n", bb1, bb2);
|
||||
for (i=56; i>=0; i-=8)
|
||||
printf("\t"BYTE_PRINT"\t\t"BYTE_PRINT"\n",
|
||||
BYTE2BIN(bb1>>i),
|
||||
BYTE2BIN(bb2>>i));
|
||||
}
|
||||
#include "alloc.h"
|
||||
#include "board.h"
|
||||
#include "attack.h"
|
||||
#include "hist.h"
|
||||
|
||||
/**
|
||||
* pos_pieces_print() - Print position pieces
|
||||
* @pos: &position
|
||||
* pos_new() - allocate a new position
|
||||
*
|
||||
* Position is not initialized.
|
||||
* If BUG_ON compilation is defined, the program will fail.
|
||||
*
|
||||
* @Return: The new position or NULL.
|
||||
*/
|
||||
void pos_pieces_print(pos_t *pos)
|
||||
pos_t *pos_new(void)
|
||||
{
|
||||
printf("White pieces (%d): \t", popcount64(pos->occupied[WHITE]));
|
||||
piece_list_print(&pos->pieces[WHITE]);
|
||||
printf("Black pieces (%d): \t", popcount64(pos->occupied[BLACK]));
|
||||
piece_list_print(&pos->pieces[BLACK]);
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_bitboards_print() - Print position bitboards
|
||||
* @pos: &position
|
||||
*/
|
||||
void pos_bitboards_print(pos_t *pos)
|
||||
{
|
||||
printf("Bitboards occupied :\n");
|
||||
bitboard_print2(pos->occupied[WHITE], pos->occupied[BLACK]);
|
||||
printf("Bitboards controlled :\n");
|
||||
bitboard_print2(pos->controlled[WHITE], pos->controlled[BLACK]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_print() - Print position on stdout.
|
||||
* @pos: &position
|
||||
*/
|
||||
void pos_print(pos_t *pos)
|
||||
{
|
||||
int rank, file;
|
||||
piece_t piece;
|
||||
board_t *board = pos->board;
|
||||
piece_list_t *wk = list_first_entry(&pos->pieces[WHITE], piece_list_t, list),
|
||||
*bk = list_first_entry(&pos->pieces[BLACK], piece_list_t, list);
|
||||
|
||||
|
||||
printf(" +---+---+---+---+---+---+---+---+\n");
|
||||
for (rank = 7; rank >= 0; --rank) {
|
||||
printf("%c |", rank + '1');
|
||||
for (file = 0; file < 8; ++file) {
|
||||
piece = board[SQ88(file, rank)].piece;
|
||||
printf(" %s |", P_CSYM(piece));
|
||||
}
|
||||
printf("\n +---+---+---+---+---+---+---+---+\n");
|
||||
}
|
||||
printf(" A B C D E F G H\n\n");
|
||||
printf("Turn: %s.\n", IS_WHITE(pos->turn) ? "white" : "black");
|
||||
printf("Kings: W:%c%c B:%c%c\n",
|
||||
FILE2C(F88(wk->square)),
|
||||
RANK2C(R88(wk->square)),
|
||||
FILE2C(F88(bk->square)),
|
||||
RANK2C(R88(bk->square)));
|
||||
printf("Possible en-passant: [%#x] ", pos->en_passant);
|
||||
if (pos->en_passant == 0)
|
||||
printf("None.\n");
|
||||
else
|
||||
printf("%d %d = %c%c\n",
|
||||
F88(pos->en_passant),
|
||||
R88(pos->en_passant),
|
||||
FILE2C(F88(pos->en_passant)),
|
||||
RANK2C(R88(pos->en_passant)));
|
||||
|
||||
printf("castle [%#x] : ", pos->castle);
|
||||
|
||||
if (pos->castle & CASTLE_WK)
|
||||
printf("K");
|
||||
if (pos->castle & CASTLE_WQ)
|
||||
printf("Q");
|
||||
if (pos->castle & CASTLE_BK)
|
||||
printf("k");
|
||||
if (pos->castle & CASTLE_BQ)
|
||||
printf("q");
|
||||
|
||||
printf("\n50 half-moves-rule = %d\n", pos->clock_50);
|
||||
printf("Current move = %d\n", pos->curmove);
|
||||
printf("Squares controlled: W:%d B:%d\n", popcount64(pos->controlled[WHITE]),
|
||||
popcount64(pos->controlled[BLACK]));
|
||||
printf("Mobility: W:%u B:%u\n", pos->mobility[WHITE],
|
||||
pos->mobility[BLACK]);
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_check() - extensive position consistenci check.
|
||||
* @pos: &position
|
||||
*/
|
||||
void pos_check(pos_t *pos)
|
||||
{
|
||||
int rank, file;
|
||||
piece_t piece;
|
||||
board_t *board = pos->board;
|
||||
|
||||
/* check that board and bitboard reflect same information */
|
||||
for (rank = 7; rank >= 0; --rank) {
|
||||
for (file = 0; file < 8; ++file) {
|
||||
piece_list_t *ppiece;
|
||||
printf("checking %c%c ", file+'a', rank+'1');
|
||||
|
||||
piece = board[SQ88(file, rank)].piece;
|
||||
ppiece= board[SQ88(file, rank)].s_piece;
|
||||
printf("piece=%s ", P_CSYM(piece));
|
||||
if (ppiece)
|
||||
printf("ppiece=%s/sq=%#x ", P_CSYM(ppiece->piece), ppiece->square);
|
||||
switch(PIECE(piece)) {
|
||||
case PAWN:
|
||||
printf("pawn" );
|
||||
break;
|
||||
case KNIGHT:
|
||||
printf("knight ");
|
||||
break;
|
||||
case BISHOP:
|
||||
printf("bishop ");
|
||||
break;
|
||||
case ROOK:
|
||||
printf("rook ");
|
||||
break;
|
||||
case QUEEN:
|
||||
printf("queen ");
|
||||
break;
|
||||
case KING:
|
||||
printf("king ");
|
||||
break;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pos_t *pos_clear(pos_t *pos)
|
||||
{
|
||||
int file, rank;
|
||||
board_t *board = pos->board;
|
||||
|
||||
for (file = 0; file < 8; ++file) {
|
||||
for (rank = 0; rank < 8; ++rank) {
|
||||
/*printf("file = %d rank = %d SQ88 = %#2x = %d addr=%p\n", file, rank,
|
||||
SQ88(file, rank), SQ88(file, rank),
|
||||
&board[SQ88(file, rank)].piece);
|
||||
*/
|
||||
board[SQ88(file, rank)].piece = EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
SET_WHITE(pos->turn);
|
||||
pos->node_count = 0;
|
||||
pos->castle = 0;
|
||||
pos->clock_50 = 0;
|
||||
pos->curmove = 0;
|
||||
pos->eval = 0;
|
||||
pos->en_passant = 0;
|
||||
pos->occupied[WHITE] = 0;
|
||||
pos->occupied[BLACK] = 0;
|
||||
for (int color=0; color<2; ++color)
|
||||
for (int piece = BB_ALL; piece < BB_END; ++piece)
|
||||
pos->bb[color][piece] = 0;
|
||||
pos->controlled[WHITE] = 0;
|
||||
pos->controlled[BLACK] = 0;
|
||||
pos->mobility[WHITE] = 0;
|
||||
pos->mobility[BLACK] = 0;
|
||||
pos->moves_generated = false;
|
||||
pos->moves_counted = false;
|
||||
/* remove pieces / moves */
|
||||
pieces_del(pos, WHITE);
|
||||
pieces_del(pos, BLACK);
|
||||
moves_del(pos);
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_del() - delete a position.
|
||||
* @pos: &position.
|
||||
*/
|
||||
void pos_del(pos_t *pos)
|
||||
{
|
||||
pieces_del(pos, WHITE);
|
||||
pieces_del(pos, BLACK);
|
||||
moves_del(pos);
|
||||
pool_add(pos_pool, pos);
|
||||
}
|
||||
|
||||
pos_t *pos_startpos(pos_t *pos)
|
||||
{
|
||||
static char *startfen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
|
||||
return fen2pos(pos, startfen);
|
||||
}
|
||||
|
||||
pos_t *pos_get()
|
||||
{
|
||||
pos_t *pos = pool_get(pos_pool);
|
||||
if (pos) {
|
||||
INIT_LIST_HEAD(&pos->pieces[WHITE]);
|
||||
INIT_LIST_HEAD(&pos->pieces[BLACK]);
|
||||
|
||||
INIT_LIST_HEAD(&pos->moves[WHITE]);
|
||||
INIT_LIST_HEAD(&pos->moves[BLACK]);
|
||||
pos_clear(pos);
|
||||
} else {
|
||||
fprintf(stderr, "zobaaa\n");
|
||||
}
|
||||
return pos;
|
||||
return safe_alloc(sizeof(pos_t));
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_dup() - duplicate a position.
|
||||
* @pos: &position to duplicate.
|
||||
*
|
||||
* New position is the same as source one (with duplicated pieces list),
|
||||
* except:
|
||||
* - moves list is empty
|
||||
* - bestmove is NULL
|
||||
* - nodecount is set to zero
|
||||
* - eval is set to EVAL_INVALID
|
||||
* - moves_generated ans moves_counted are unset
|
||||
* Return a copy, allocated with malloc(1), of @pos.
|
||||
*
|
||||
* @return: The new position.
|
||||
* @Return: The new position.
|
||||
*
|
||||
* TODO: merge with pos_get - NULL for init, non null for duplicate
|
||||
* TODO: merge with pos_new - NULL for init, non null for duplicate
|
||||
*/
|
||||
pos_t *pos_dup(pos_t *pos)
|
||||
pos_t *pos_dup(const pos_t *pos)
|
||||
{
|
||||
struct list_head *p_cur, *piece_list;
|
||||
piece_list_t *oldpiece;
|
||||
board_t *board;
|
||||
pos_t *new = pool_get(pos_pool);
|
||||
pos_t *newpos = safe_alloc(sizeof(pos_t));
|
||||
|
||||
if (new) {
|
||||
board = new->board;
|
||||
*new = *pos;
|
||||
for (int color = 0; color < 2; ++color) {
|
||||
INIT_LIST_HEAD(&new->pieces[color]);
|
||||
INIT_LIST_HEAD(&new->moves[color]);
|
||||
/* duplicate piece list */
|
||||
piece_list = &pos->pieces[color]; /* white/black piece list */
|
||||
*newpos = *pos;
|
||||
return newpos;
|
||||
}
|
||||
|
||||
list_for_each(p_cur, piece_list) {
|
||||
oldpiece = list_entry(p_cur, piece_list_t, list);
|
||||
board[oldpiece->square].s_piece =
|
||||
piece_add(new, oldpiece->piece, oldpiece->square);
|
||||
}
|
||||
}
|
||||
new->bestmove = NULL;
|
||||
new->node_count = 0;
|
||||
new->eval = EVAL_INVALID;
|
||||
new->moves_generated = false;
|
||||
new->moves_counted = false;
|
||||
/**
|
||||
* pos_copy() - copy a position into another one.
|
||||
* @from: &position to duplicate.
|
||||
* @to: &destination position.
|
||||
*
|
||||
* Return a copy of @from into @to.
|
||||
*
|
||||
* @Return: @to.
|
||||
*/
|
||||
pos_t *pos_copy(const pos_t *from, pos_t *to)
|
||||
{
|
||||
*to = *from;
|
||||
return to;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_del() - delete a position.
|
||||
* @pos: &position.
|
||||
*
|
||||
*/
|
||||
void pos_del(pos_t *pos)
|
||||
{
|
||||
safe_free(pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_clear() - clear a position.
|
||||
* @pos: &position.
|
||||
*
|
||||
* @return: @pos.
|
||||
*/
|
||||
pos_t *pos_clear(pos_t *pos)
|
||||
{
|
||||
# ifdef DEBUG_POS
|
||||
printf("size(pos_board=%lu elt=%lu\n", sizeof(pos->board), sizeof(int));
|
||||
# endif
|
||||
memset(pos, 0, sizeof (pos_t));
|
||||
|
||||
/* non-zero values */
|
||||
pos->en_passant = SQUARE_NONE;
|
||||
pos->move = MOVE_NONE;
|
||||
|
||||
pos->king[WHITE] = SQUARE_NONE;
|
||||
pos->king[BLACK] = SQUARE_NONE;
|
||||
pos->prev = HIST_START;
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_cmp() - compare two positions..
|
||||
* @pos1, @pos2: The two &position.
|
||||
*
|
||||
* Used only for move_{do,undo} test/debug.
|
||||
*
|
||||
* @return: true if equal, false otherwise.
|
||||
*/
|
||||
bool pos_cmp(const pos_t *pos1, const pos_t *pos2)
|
||||
{
|
||||
#define _cmpf(a) (pos1->a != pos2->a)
|
||||
bool ret = false;
|
||||
|
||||
if (_cmpf(node_count) || _cmpf(turn))
|
||||
goto end;
|
||||
|
||||
/* move_do/undo position state */
|
||||
if (_cmpf(key) || _cmpf(en_passant) || _cmpf(castle) ||
|
||||
_cmpf(ply50) || _cmpf(plycount) || _cmpf(captured))
|
||||
goto end;
|
||||
|
||||
if (_cmpf(checkers) || _cmpf(pinners) || _cmpf(blockers))
|
||||
goto end;
|
||||
|
||||
for (square_t sq = A1; sq <= H8; ++sq)
|
||||
if (_cmpf(board[sq]))
|
||||
goto end;
|
||||
|
||||
for (color_t color = WHITE; color <= BLACK; ++color) {
|
||||
for (piece_type_t piece = ALL_PIECES; piece <= KING; ++piece)
|
||||
if (_cmpf(bb[color][piece]))
|
||||
goto end;
|
||||
if (_cmpf(king[color]))
|
||||
goto end;
|
||||
}
|
||||
return new;
|
||||
|
||||
if (_cmpf(checkers) ||_cmpf(pinners) || _cmpf(blockers))
|
||||
goto end;
|
||||
|
||||
/*
|
||||
* if (_cmpf(repeat.moves) ||
|
||||
* memcmp(pos1->repeat.key, pos2->repeat.key,
|
||||
* pos1->repeat.moves * sizeof pos1->repeat.key))
|
||||
* goto end;
|
||||
*/
|
||||
|
||||
ret = true;
|
||||
end:
|
||||
return ret;
|
||||
#undef _cmpf
|
||||
}
|
||||
|
||||
pool_t *pos_pool_init()
|
||||
/**
|
||||
* pos_checkers() - find all checkers on a king.
|
||||
* @pos: &position
|
||||
* @color: king color
|
||||
*
|
||||
* Get a bitboard of all checkers on @color king.
|
||||
* Just a wrapper over @sq_attackers().
|
||||
*
|
||||
* @return: a bitboard of checkers.
|
||||
*/
|
||||
bitboard_t pos_checkers(const pos_t *pos, const color_t color)
|
||||
{
|
||||
if (!pos_pool)
|
||||
pos_pool = pool_create("positions", 128, sizeof(pos_t));
|
||||
return pos_pool;
|
||||
bitboard_t occ = pos_occ(pos);
|
||||
return sq_attackers(pos, occ, pos->king[color], OPPONENT(color));
|
||||
}
|
||||
|
||||
void pos_pool_stats()
|
||||
/**
|
||||
* pos_repcount() - return position repetition count.
|
||||
* @pos: &position to search
|
||||
*
|
||||
* Attention: positions before (and including) root position repcount are
|
||||
* already decreased by one. See do_moves() in uci.c.
|
||||
*
|
||||
* @return: The number of repetitions in history, zero otherwise.
|
||||
*/
|
||||
u8 pos_repcount(pos_t *pos)
|
||||
{
|
||||
if (pos_pool)
|
||||
pool_stats(pos_pool);
|
||||
int c50 = pos->ply50;
|
||||
state_t *st = &pos->state;
|
||||
hkey_t key = pos->key;
|
||||
|
||||
st = hist_prev4(st);
|
||||
//printf("is rep: START=%p ", HIST_START);
|
||||
//printf("state=%p diff=%zd c50=%d k1=%#lx k2=%#lx\n",
|
||||
// st, st - HIST_START, c50, hash_short(pos->key), hash_short(st->key));
|
||||
|
||||
/* attention, what about root position ? Isn't it dangerous to compare with
|
||||
* its key ? like: st->prev != HIST_START
|
||||
*/
|
||||
for (; c50 >= 0 && st != HIST_START; st = hist_prev2(st), c50 -= 2) {
|
||||
if (key == st->key)
|
||||
return st->repcount + 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_set_checkers_pinners_blockers() - calculate checkers, pinners and blockers.
|
||||
* @pos: &position
|
||||
*
|
||||
* Set position checkers, pinners and blockers on player-to-play king.
|
||||
* It should be slightly faster than @pos_checkers + @pos_set_pinners_blockers, as
|
||||
* some calculation will be done once.
|
||||
*/
|
||||
void pos_set_checkers_pinners_blockers(pos_t *pos)
|
||||
{
|
||||
int us = pos->turn, them = OPPONENT(us);
|
||||
bitboard_t occ = pos_occ(pos);
|
||||
bitboard_t attackers;
|
||||
bitboard_t checkers = 0, blockers = 0, pinners = 0;
|
||||
bitboard_t targets, tmpcheckers, maybeblockers, tmppinners;
|
||||
square_t king = pos->king[us];
|
||||
int pinner;
|
||||
|
||||
/* bishop type - we attack with a bishop from king position */
|
||||
attackers = pos->bb[them][BISHOP] | pos->bb[them][QUEEN];
|
||||
|
||||
/* targets is all "target" pieces if K was a bishop */
|
||||
targets = hq_bishop_moves(occ, king) & occ;
|
||||
|
||||
/* checkers = only opponent B/Q */
|
||||
tmpcheckers = targets & attackers;
|
||||
checkers |= tmpcheckers;
|
||||
|
||||
/* maybe blockers = we remove checkers, to look "behind" */
|
||||
maybeblockers = targets & ~tmpcheckers;
|
||||
|
||||
/* we find second targets, by removing first ones (excl. checkers) */
|
||||
if (maybeblockers) {
|
||||
targets = hq_bishop_moves(occ ^ maybeblockers, king) ^ tmpcheckers;
|
||||
|
||||
/* pinners = only B/Q */
|
||||
tmppinners = targets & attackers;
|
||||
|
||||
/* blockers = we find occupied squares between pinner and king */
|
||||
while (tmppinners) {
|
||||
pinner = bb_next(&tmppinners);
|
||||
pinners |= BIT(pinner);
|
||||
blockers |= bb_between[pinner][king] & maybeblockers;
|
||||
}
|
||||
}
|
||||
|
||||
/* same for rook type */
|
||||
attackers = pos->bb[them][ROOK] | pos->bb[them][QUEEN];
|
||||
targets = hq_rook_moves(occ, king) & occ;
|
||||
|
||||
tmpcheckers = targets & attackers;
|
||||
checkers |= tmpcheckers;
|
||||
|
||||
maybeblockers = targets & ~tmpcheckers;
|
||||
if (maybeblockers) {
|
||||
targets = hq_rook_moves(occ ^ maybeblockers, king) ^ tmpcheckers;
|
||||
tmppinners = targets & attackers;
|
||||
while (tmppinners) {
|
||||
pinner = bb_next(&tmppinners);
|
||||
pinners |= BIT(pinner);
|
||||
blockers |= bb_between[pinner][king] & maybeblockers;
|
||||
}
|
||||
}
|
||||
|
||||
/* pawns & knights */
|
||||
checkers |= bb_pawn_attacks[us][king] & pos->bb[them][PAWN];
|
||||
checkers |= bb_knight[king] & pos->bb[them][KNIGHT];
|
||||
|
||||
pos->checkers = checkers;
|
||||
pos->pinners = pinners;
|
||||
pos->blockers = blockers;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_set_pinners_blockers() - set position pinners and blockers.
|
||||
* @pos: &position
|
||||
*
|
||||
* set position pinners and blockers on player-to-play king.
|
||||
*/
|
||||
void pos_set_pinners_blockers(pos_t *pos)
|
||||
{
|
||||
color_t color = pos->turn;
|
||||
square_t king = pos->king[color];
|
||||
bitboard_t tmp, occ = pos_occ(pos), blockers = 0;
|
||||
int pinner;
|
||||
|
||||
pos->pinners = sq_pinners(pos, king, OPPONENT(pos->turn));
|
||||
bit_for_each64(pinner, tmp, pos->pinners) {
|
||||
//bitboard_t blocker =
|
||||
// warn_on(popcount64(blocker) != 1);
|
||||
blockers |= bb_between_excl[pinner][king] & occ;
|
||||
}
|
||||
pos->blockers = blockers;
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_king_pinners() - get the "pinners" on a king "pinners".
|
||||
* @pos: &position
|
||||
* @color: king color.
|
||||
*
|
||||
* get position "pinners" on @color king. Here, pinner means a piece separated from king
|
||||
* by one piece (any color) only.
|
||||
* This is just a wrapper over @sq_pinners().
|
||||
*
|
||||
* @return: pinners bitboard.
|
||||
*/
|
||||
bitboard_t pos_king_pinners(const pos_t *pos, const color_t color)
|
||||
{
|
||||
return sq_pinners(pos, pos->king[color], OPPONENT(pos->turn));
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_king_blockers() - get the pin blockers on a king.
|
||||
* @pos: &position
|
||||
* @color: king color.
|
||||
* @pinners: pinners bitboard.
|
||||
*
|
||||
* get @pinners blockers pieces on @color king.
|
||||
*
|
||||
* @return: blockers bitboard.
|
||||
*/
|
||||
bitboard_t pos_king_blockers(const pos_t *pos, const color_t color, const bitboard_t pinners)
|
||||
{
|
||||
bitboard_t tmp, blockers = 0, occ = pos_occ(pos);
|
||||
square_t pinner, king = pos->king[color];
|
||||
|
||||
bit_for_each64(pinner, tmp, pinners) {
|
||||
//warn_on(popcount64(blocker) != 1);
|
||||
//if (popcount64(blocker) != 1) {
|
||||
// printf("n blockers = %d\n", popcount64(blocker));
|
||||
// bb_print("blockers", blocker);
|
||||
//}
|
||||
blockers |= bb_between_excl[pinner][king] & occ;
|
||||
}
|
||||
return blockers;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_ok() - extensive position consistency check.
|
||||
* @pos: &position
|
||||
* @strict: if true, call bug_on() on any error.
|
||||
*
|
||||
* Perform some validity check on position @pos:
|
||||
* - pawns on 1st or 8th rank
|
||||
* - number of pawns > 8 (per color)
|
||||
* - total number of pieces > 16 or zero (per color)
|
||||
* - number of kings != 1 (per color)
|
||||
* - discrepancy between board and king (per color)
|
||||
* - discrepancy between bitboards and board (per color)
|
||||
* - side-to-move already checking opponent king
|
||||
* - side-to-move in check more than twice
|
||||
* - kings distance is 1
|
||||
* - TODO: - castling / e.p. flags
|
||||
*
|
||||
* In case of errors, and @strict is true, @bug_on() is called, and program will
|
||||
* be terminated.
|
||||
* This function should be called with @strict == false during initialization phase
|
||||
* (eg after fen parsing), and with @strict == true otherwise (as we have some data
|
||||
* corruption).
|
||||
*
|
||||
* @return: (if @strict is false) return true if check is ok, false otherwise.
|
||||
*/
|
||||
bool pos_ok(pos_t *pos, const bool strict)
|
||||
{
|
||||
int n, count = 0, bbcount = 0, error = 0;
|
||||
color_t us = pos->turn, __unused them = OPPONENT(us);
|
||||
|
||||
/* pawns on 1st ot 8th rank */
|
||||
error += warn_on_or_eval((pos->bb[WHITE][PAWN] | pos->bb[BLACK][PAWN]) &
|
||||
(RANK_1bb | RANK_8bb));
|
||||
|
||||
for (color_t color = WHITE; color <= BLACK; ++color) {
|
||||
/* pawn count */
|
||||
n = popcount64(pos->bb[color][PAWN]);
|
||||
error += warn_on_or_eval(n > 8);
|
||||
/* king count */
|
||||
n = popcount64(pos->bb[color][KING]);
|
||||
error += warn_on_or_eval(n != 1);
|
||||
/* king mismatch with board */
|
||||
error += warn_on_or_eval(PIECE(pos->board[pos->king[color]]) != KING);
|
||||
/* pieces count */
|
||||
n = popcount64(pos->bb[color][ALL_PIECES]);
|
||||
error += warn_on_or_eval(n == 0 || n > 16);
|
||||
bbcount += n;
|
||||
}
|
||||
for (square_t sq = 0; sq < 64; ++sq) {
|
||||
piece_t piece = pos->board[sq];
|
||||
__unused bitboard_t match;
|
||||
if (piece == EMPTY)
|
||||
continue;
|
||||
color_t c = COLOR(piece);
|
||||
piece_type_t p = PIECE(piece);
|
||||
match = pos->bb[c][p] & BIT(sq);
|
||||
error += warn_on_or_eval(!match);
|
||||
count++;
|
||||
}
|
||||
/* occupied board is different from bitboards */
|
||||
error += warn_on_or_eval(count != bbcount);
|
||||
/* is opponent already in check ? */
|
||||
error += warn_on_or_eval(pos_checkers(pos, them));
|
||||
/* is color to play in check more than twice ? */
|
||||
error += warn_on_or_eval(popcount64(pos_checkers(pos, us)) > 2);
|
||||
/* kings distance is less than 2 */
|
||||
error += warn_on_or_eval(sq_dist(pos->king[WHITE], pos->king[BLACK]) < 2);
|
||||
/* e.p. and castling rights check */
|
||||
error += fen_ok(pos, false);
|
||||
|
||||
if (strict) {
|
||||
bug_on_always(error);
|
||||
}
|
||||
return error? false: true;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_print() - Print position and fen on stdout.
|
||||
* @pos: &position
|
||||
*/
|
||||
void pos_print(const pos_t *pos)
|
||||
{
|
||||
char str[128];
|
||||
|
||||
board_print(pos->board);
|
||||
printf("fen: %s\n", pos2fen(pos, str));
|
||||
printf("last move:%s ", move_to_str(str, pos->move, 0));
|
||||
printf("key:%lx\n", pos->key);
|
||||
printf("checkers:%s ", pos_checkers2str(pos, str, sizeof(str)));
|
||||
printf("pinners: %s ", pos_pinners2str(pos, str, sizeof(str)));
|
||||
printf("blockers: %s\n", pos_blockers2str(pos, str, sizeof(str)));
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_print_mask() - Print position and fen on stdout, with highlighted squares.
|
||||
* @pos: &position
|
||||
* @mask: mask of highlighted squares.
|
||||
*/
|
||||
void pos_print_mask(const pos_t *pos, const bitboard_t mask)
|
||||
{
|
||||
char fen[92];
|
||||
|
||||
board_print_mask(pos->board, mask);
|
||||
printf("fen %s\n", pos2fen(pos, fen));
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_print_raw - print simple position board (octal/FEN symbol values)
|
||||
* @bb: the bitboard
|
||||
* @type: int, 0 for octal, 1 for fen symbol
|
||||
*/
|
||||
void pos_print_raw(const pos_t *pos, const int type)
|
||||
{
|
||||
board_print_raw(pos->board, type);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_print_pieces() - Print position pieces
|
||||
* @pos: &position
|
||||
*/
|
||||
void pos_print_pieces(const pos_t *pos)
|
||||
{
|
||||
int sq, count, cur;
|
||||
char *pname;
|
||||
u64 tmp;
|
||||
bitboard_t p;
|
||||
for (int color = WHITE; color <= BLACK; ++color) {
|
||||
for (int piece = KING; piece >= PAWN; --piece) {
|
||||
p = pos->bb[color][piece];
|
||||
count = popcount64(p);
|
||||
cur = 0;
|
||||
pname = piece_to_char(p);
|
||||
printf("%s(%d)%s", pname, count, count? ":": "");
|
||||
if (count) {
|
||||
bit_for_each64(sq, tmp, p) {
|
||||
// char cf = sq_file(bit), cr = sq_rank(bit);
|
||||
printf("%s%s", cur? ",": "", sq_to_string(sq));
|
||||
cur++;
|
||||
}
|
||||
}
|
||||
printf(" ");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
217
src/position.h
217
src/position.h
@@ -1,6 +1,6 @@
|
||||
/* position.h - position management definitions.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -15,50 +15,191 @@
|
||||
#define POSITION_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <pool.h>
|
||||
#include <list.h>
|
||||
#include <bits.h>
|
||||
|
||||
#include "board.h"
|
||||
#include <brlib.h>
|
||||
#include <bitops.h>
|
||||
#include <struct-group.h>
|
||||
#include <bug.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "hash.h"
|
||||
#include "bitboard.h"
|
||||
#include "piece.h"
|
||||
#include "move.h"
|
||||
#include "board.h"
|
||||
#include "eval-defs.h"
|
||||
|
||||
typedef struct pos_s {
|
||||
typedef struct __pos_s {
|
||||
u64 node_count; /* evaluated nodes */
|
||||
piece_t turn; /* we use only color bit */
|
||||
castle_t castle;
|
||||
u16 clock_50;
|
||||
u16 curmove;
|
||||
int turn; /* WHITE or BLACK */
|
||||
|
||||
/* data which cannot be recovered by move_undo (like castle_rights, ...).
|
||||
*
|
||||
* Attention: checkers/pinners/blockers are not included here, and
|
||||
* are not available in move_undo or any following legality check.
|
||||
*
|
||||
* Following data can be accessed either directly, either via "state"
|
||||
* structure name.
|
||||
* For example, pos->en_passant and pos->state.en_passant are the same.
|
||||
* This allows a memcpy on this data (to save/restore position state).
|
||||
*/
|
||||
struct_group_tagged(state_s, state,
|
||||
|
||||
/* 64 bits */
|
||||
struct state_s *prev;
|
||||
hkey_t key;
|
||||
|
||||
/* 16 bits */
|
||||
move_t move;
|
||||
|
||||
/* 8 bits */
|
||||
square_t en_passant;
|
||||
castle_rights_t castle;
|
||||
piece_t captured; /* only used in move_undo */
|
||||
phase_t phase;
|
||||
u8 ply50;
|
||||
u8 repcount; /* repetition count */
|
||||
);
|
||||
eval_t eval;
|
||||
int eval_simple_phase;
|
||||
eval_t eval_simple;
|
||||
move_t *bestmove;
|
||||
bool moves_generated;
|
||||
bool moves_counted;
|
||||
board_t board[BOARDSIZE];
|
||||
|
||||
square_t en_passant;
|
||||
|
||||
bitboard_t bb[2][BB_END]; /* use: pieces[BLACK][BB_PAWN] */
|
||||
bitboard_t occupied[2]; /* OR of bb[COLOR][x] */
|
||||
bitboard_t controlled[2];
|
||||
u16 mobility[2];
|
||||
struct list_head pieces[2]; /* pieces list, King is first */
|
||||
struct list_head moves[2];
|
||||
bitboard_t checkers; /* opponent checkers */
|
||||
bitboard_t pinners; /* opponent pinners */
|
||||
bitboard_t blockers; /* pieces blocking pin */
|
||||
piece_t board[BOARDSIZE];
|
||||
bitboard_t bb[2][PT_NB]; /* bb[0][PAWN], bb[1][ALL_PIECES] */
|
||||
square_t king[2]; /* dup with bb, faster retrieval */
|
||||
u16 plycount; /* plies in game, start from 1 */
|
||||
u8 plyroot; /* plies since search root. root=0 */
|
||||
} pos_t;
|
||||
|
||||
void bitboard_print(bitboard_t bb);
|
||||
void bitboard_print2(bitboard_t bb1, bitboard_t bb2);
|
||||
void pos_pieces_print(pos_t *pos);
|
||||
void pos_bitboards_print(pos_t *pos);
|
||||
void pos_print(pos_t *pos);
|
||||
pos_t *pos_clear(pos_t *pos);
|
||||
typedef struct state_s state_t;
|
||||
|
||||
#define pos_pinned(p) (p->blockers & p->bb[p->turn][ALL_PIECES])
|
||||
|
||||
/**
|
||||
* pos_set_sq - unconditionally set a piece on a square
|
||||
* @pos: position
|
||||
* @square: square_t to set
|
||||
* @piece: piece_t to add
|
||||
*
|
||||
* Both position board and bitboards are modified.
|
||||
*/
|
||||
static __always_inline void pos_set_sq(pos_t *pos, square_t square, piece_t piece)
|
||||
{
|
||||
color_t color = COLOR(piece);
|
||||
piece_type_t type = PIECE(piece);
|
||||
|
||||
bug_on(pos->board[square] != EMPTY);
|
||||
|
||||
pos->board[square] = piece;
|
||||
pos->bb[color][type] |= BIT(square);
|
||||
pos->bb[color][ALL_PIECES] |= BIT(square);
|
||||
//if (type == KING)
|
||||
// pos->king[color] = square;
|
||||
|
||||
//pos->key ^= zobrist_pieces[piece][square];
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_clr_sq - unconditionally remove a piece from square
|
||||
* @pos: position
|
||||
* @square: square_t to clear
|
||||
*
|
||||
* Both position board and bitboards are modified.
|
||||
*/
|
||||
static __always_inline void pos_clr_sq(pos_t *pos, square_t square)
|
||||
{
|
||||
piece_t piece = pos->board[square];
|
||||
piece_type_t type = PIECE(piece);
|
||||
color_t color = COLOR(piece);
|
||||
|
||||
bug_on(pos->board[square] == EMPTY);
|
||||
|
||||
//pos->key ^= zobrist_pieces[piece][square];
|
||||
|
||||
pos->board[square] = EMPTY;
|
||||
pos->bb[color][type] &= ~BIT(square);
|
||||
pos->bb[color][ALL_PIECES] &= ~BIT(square);
|
||||
//if (type == KING)
|
||||
// pos->king[color] = SQUARE_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_occ() - get position occupation (all pieces)
|
||||
* @pos: position
|
||||
*
|
||||
* @return: occupation bitboard.
|
||||
*/
|
||||
static __always_inline bitboard_t pos_occ(const pos_t *pos)
|
||||
{
|
||||
return pos->bb[WHITE][ALL_PIECES] | pos->bb[BLACK][ALL_PIECES];
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_between_occ() - find occupation between two squares.
|
||||
* @pos: position
|
||||
* @sq1: square 1
|
||||
* @sq2: square 2
|
||||
*
|
||||
* @return: bitboard of @betw if between @sq1 and @sq2.
|
||||
*/
|
||||
static __always_inline bitboard_t pos_between_occ(const pos_t *pos,
|
||||
const square_t sq1, const square_t sq2)
|
||||
{
|
||||
return bb_between_excl[sq1][sq2] & pos_occ(pos);
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_between_count() - count occupied squares between two squares.
|
||||
* @pos: position
|
||||
* @sq1: square 1
|
||||
* @sq2: square 2
|
||||
*
|
||||
* @return: bitboard of @betw if between @sq1 and @sq2.
|
||||
*/
|
||||
static __always_inline int pos_between_count(const pos_t *pos,
|
||||
const square_t sq1, const square_t sq2)
|
||||
{
|
||||
return popcount64(pos_between_occ(pos, sq1, sq2));
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_checkers2str() - get of string of checkers.
|
||||
* @p: position
|
||||
* @str: destination string
|
||||
* @len: max @str len.
|
||||
*
|
||||
* A wrapper over @bb_sq2str() for checkers bitmap.
|
||||
*
|
||||
* @return: @str.
|
||||
*/
|
||||
#define pos_checkers2str(p, str, len) bb_sq2str(p->checkers, str, len)
|
||||
#define pos_pinners2str(p, str, len) bb_sq2str(p->pinners, str, len)
|
||||
#define pos_blockers2str(p, str, len) bb_sq2str(p->blockers, str, len)
|
||||
|
||||
//void bitboard_print(bitboard_t bb, char *title);
|
||||
//void bitboard_print2(bitboard_t bb1, bitboard_t bb2, char *title);
|
||||
|
||||
pos_t *pos_new();
|
||||
pos_t *pos_dup(const pos_t *pos);
|
||||
pos_t *pos_copy(const pos_t *from, pos_t *to);
|
||||
void pos_del(pos_t *pos);
|
||||
pos_t *pos_startpos(pos_t *pos);
|
||||
pos_t *pos_create();
|
||||
pool_t *pos_pool_init();
|
||||
void pos_pool_stats();
|
||||
pos_t *pos_get();
|
||||
pos_t *pos_dup(pos_t *pos);
|
||||
void pos_check(pos_t *pos);
|
||||
pos_t *pos_clear(pos_t *pos);
|
||||
bool pos_cmp(const pos_t *pos1, const pos_t *pos2);
|
||||
|
||||
u8 pos_repcount(pos_t *pos);
|
||||
void pos_set_checkers_pinners_blockers(pos_t *pos);
|
||||
void pos_set_pinners_blockers(pos_t *pos);
|
||||
bitboard_t pos_checkers(const pos_t *pos, const color_t color);
|
||||
bitboard_t pos_king_pinners(const pos_t *pos, const color_t color);
|
||||
bitboard_t pos_king_blockers(const pos_t *pos, const color_t color, const bitboard_t );
|
||||
|
||||
bool pos_ok(pos_t *pos, const bool strict);
|
||||
|
||||
void pos_print(const pos_t *pos);
|
||||
void pos_print_mask(const pos_t *pos, const bitboard_t mask);
|
||||
void pos_print_raw(const pos_t *pos, const int type);
|
||||
|
||||
void pos_print_pieces(const pos_t *pos);
|
||||
|
||||
|
||||
#endif /* POSITION_H */
|
||||
|
||||
53
src/pv.h
Normal file
53
src/pv.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/* pv.c - Principal variation.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <brlib.h>
|
||||
|
||||
#include "move.h"
|
||||
|
||||
/*
|
||||
* For PV, we use a triangular structure. The following table is adapted from
|
||||
* https://www.chessprogramming.org/Triangular_PV-Table.
|
||||
*
|
||||
* ply Search maxLengthPV
|
||||
* Depth
|
||||
* +--------------------------------------------+
|
||||
* 0=root N |N |
|
||||
* +------------------------------------------+-+
|
||||
* 1 N-1 |N-1 |
|
||||
* +----------------------------------------+-+
|
||||
* 2 N-2 |N-2 |
|
||||
* +----------------------------------------+-+
|
||||
* ... /
|
||||
* +-----+-+
|
||||
* N-3 3 |3 |
|
||||
* +---+-+
|
||||
* N-2 2 |2 |
|
||||
* +-+-+
|
||||
* N-1 1 |1|
|
||||
* +-+
|
||||
*
|
||||
* The offsets will be:
|
||||
* Depth (=size): 1 2 3 4 5 6 7 ...
|
||||
* Offset: 0 1 3 6 10 15 21 ...
|
||||
*
|
||||
* Off(n) = n * (n - 1) / 2 (direct calc)
|
||||
* Off(n + 1) = Off(n) + n (calc from previous)
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
move[];
|
||||
} foo;
|
||||
256
src/search.c
256
src/search.c
@@ -1,6 +1,6 @@
|
||||
/* search.c - search good moves.
|
||||
/* search.c - search for perfect move.
|
||||
*
|
||||
* Copyright (C) 2023 Bruno Raoult ("br")
|
||||
* Copyright (C) 2023-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -11,14 +11,34 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <br.h>
|
||||
#include <list.h>
|
||||
#include <debug.h>
|
||||
#include <brlib.h>
|
||||
|
||||
#include "move.h"
|
||||
#include "eval.h"
|
||||
#include "chessdefs.h"
|
||||
|
||||
#include "position.h"
|
||||
#include "move-gen.h"
|
||||
#include "move-do.h"
|
||||
#include "search.h"
|
||||
#include "attack.h"
|
||||
#include "hist.h"
|
||||
|
||||
search_uci_t search_uci;
|
||||
|
||||
/**
|
||||
* is_draw() - check if position is draw by 50 or repetition rule.
|
||||
* @pos: &position to search
|
||||
*
|
||||
* Note that states before (and including) root position state have
|
||||
* their repcount decreased by one.
|
||||
*
|
||||
* @return: The @pos negamax evaluation.
|
||||
*/
|
||||
bool is_draw(pos_t *pos)
|
||||
{
|
||||
return (pos->ply50 > 100 || pos->repcount);
|
||||
}
|
||||
|
||||
/**
|
||||
* negamax() - search position negamax.
|
||||
@@ -33,31 +53,55 @@
|
||||
*/
|
||||
eval_t negamax(pos_t *pos, int depth, int color)
|
||||
{
|
||||
move_t *move;
|
||||
pos_t *newpos;
|
||||
move_t *move, *last;
|
||||
state_t state;
|
||||
eval_t best = EVAL_MIN, score;
|
||||
movelist_t movelist;
|
||||
|
||||
pos->node_count++;
|
||||
|
||||
if (pos_repcount(pos)) /* repetition */
|
||||
return EVAL_DRAW * color;
|
||||
|
||||
pos_set_checkers_pinners_blockers(pos);
|
||||
pos_gen_legal(pos, &movelist);
|
||||
|
||||
if (!movelist.nmoves) /* no move: mate ot draw */
|
||||
return (pos->checkers? EVAL_MATE: EVAL_DRAW) * color;
|
||||
|
||||
if (depth == 0) {
|
||||
moves_gen_all_nomoves(pos);
|
||||
score = eval(pos) * color;
|
||||
return score;
|
||||
}
|
||||
moves_gen_all(pos);
|
||||
list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
newpos = move_do(pos, move);
|
||||
score = -negamax(newpos, depth - 1, -color);
|
||||
pos->node_count += newpos->node_count;
|
||||
move->negamax = score;
|
||||
|
||||
last = movelist.move + movelist.nmoves;
|
||||
for (move = movelist.move; move < last; ++move) {
|
||||
move_do(pos, *move, &state);
|
||||
score = -negamax(pos, depth - 1, -color);
|
||||
if (score > best) {
|
||||
best = score;
|
||||
pos->bestmove = move;
|
||||
pos->eval = best;
|
||||
}
|
||||
move_undo(newpos, move);
|
||||
move_undo(pos, *move, &state);
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/**
|
||||
* search() - do (iterative) search.
|
||||
* @pos: &position to search
|
||||
* @depth: wanted depth.
|
||||
* @alpha: alpha.
|
||||
* @beta: beta.
|
||||
* @color: 1 for white, -1 for black.
|
||||
*
|
||||
*
|
||||
* @return: The @pos PVS evaluation.
|
||||
*/
|
||||
void search(__unused pos_t *pos)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* pvs() - Principal Variation Search.
|
||||
@@ -74,74 +118,76 @@ eval_t negamax(pos_t *pos, int depth, int color)
|
||||
*
|
||||
* @return: The @pos PVS evaluation.
|
||||
*/
|
||||
eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color)
|
||||
{
|
||||
move_t *move;
|
||||
pos_t *newpos;
|
||||
eval_t score = EVAL_INVALID;
|
||||
bool firstchild = true;
|
||||
|
||||
pos->node_count++;
|
||||
|
||||
if (depth == 0) {
|
||||
//return quiesce(p, alpha, beta); /* leaf node */
|
||||
moves_gen_all_nomoves(pos);
|
||||
score = eval(pos) * color;
|
||||
log_f(2, "Terminal: depth=%d ", depth);
|
||||
log_f(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);
|
||||
return score;
|
||||
}
|
||||
|
||||
moves_gen_all(pos);
|
||||
//moves_print(pos, M_PR_EVAL);
|
||||
/* do the full search for first child */
|
||||
//move = list_first_entry_or_null(&pos->moves[pos->turn], move_t, list);
|
||||
|
||||
list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
newpos = move_do(pos, move);
|
||||
log(2, "%.*s", 5 - depth, " ");
|
||||
if (firstchild) { /* first child */
|
||||
score = -pvs(newpos, depth - 1, -beta, -alpha, -color);
|
||||
log_f(2, "First child depth=%d move=", depth);
|
||||
//move_print(0, move, 0);
|
||||
log(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);
|
||||
pos->bestmove = move;
|
||||
} else {
|
||||
/* search with a null window */
|
||||
score = -pvs(newpos, depth - 1, -alpha - 1, -alpha, -color);
|
||||
log_f(2, "Other child depth=%d move=", depth);
|
||||
//move_print(0, move, 0);
|
||||
log_f(2, "score=%d alpha=%d beta=%d ", score, alpha, beta);
|
||||
/* for fail-soft: if (score > alpha && score < beta) */
|
||||
if (score > alpha) {
|
||||
/* if failed high, do a full re-search */
|
||||
log_f(2, "doing full search.");
|
||||
score = -pvs(newpos, depth - 1, -beta, -alpha, -color);
|
||||
}
|
||||
log(2, "\n");
|
||||
}
|
||||
pos->node_count += newpos->node_count;
|
||||
move_undo(newpos, move);
|
||||
if (score >= beta) { /* fail-hard hard beta cut-off */
|
||||
log(2, "%.*s", 5 - depth, " ");
|
||||
log_f(2, "depth=%d score=%d alpha=%d beta=%d beta cut-off.\n",
|
||||
depth, score, alpha, beta);
|
||||
return beta;
|
||||
}
|
||||
if (score > alpha) {
|
||||
log(2, "%.*s", 5 - depth, " ");
|
||||
log_f(2, "depth=%d setting new alpha from %d to %d\n",
|
||||
depth, alpha, score);
|
||||
alpha = score;
|
||||
pos->bestmove = move;
|
||||
}
|
||||
move->pos = NULL;
|
||||
move->negamax = score;
|
||||
firstchild = false;
|
||||
}
|
||||
|
||||
return alpha;
|
||||
}
|
||||
/*
|
||||
* eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color)
|
||||
* {
|
||||
* move_t *move;
|
||||
* pos_t *newpos;
|
||||
* eval_t score = EVAL_INVALID;
|
||||
* bool firstchild = true;
|
||||
*
|
||||
* pos->node_count++;
|
||||
*
|
||||
* if (depth == 0) {
|
||||
* //return quiesce(p, alpha, beta); /\* leaf node *\/
|
||||
* moves_gen_all_nomoves(pos);
|
||||
* score = eval(pos) * color;
|
||||
* log_f(2, "Terminal: depth=%d ", depth);
|
||||
* log_f(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);
|
||||
* return score;
|
||||
* }
|
||||
*
|
||||
* moves_gen_all(pos);
|
||||
* //moves_print(pos, M_PR_EVAL);
|
||||
* /\* do the full search for first child *\/
|
||||
* //move = list_first_entry_or_null(&pos->moves[pos->turn], move_t, list);
|
||||
*
|
||||
* list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
* newpos = move_do(pos, move);
|
||||
* log(2, "%.*s", 5 - depth, " ");
|
||||
* if (firstchild) { /\* first child *\/
|
||||
* score = -pvs(newpos, depth - 1, -beta, -alpha, -color);
|
||||
* log_f(2, "First child depth=%d move=", depth);
|
||||
* //move_print(0, move, 0);
|
||||
* log(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);
|
||||
* pos->bestmove = move;
|
||||
* } else {
|
||||
* /\* search with a null window *\/
|
||||
* score = -pvs(newpos, depth - 1, -alpha - 1, -alpha, -color);
|
||||
* log_f(2, "Other child depth=%d move=", depth);
|
||||
* //move_print(0, move, 0);
|
||||
* log_f(2, "score=%d alpha=%d beta=%d ", score, alpha, beta);
|
||||
* /\* for fail-soft: if (score > alpha && score < beta) *\/
|
||||
* if (score > alpha) {
|
||||
* /\* if failed high, do a full re-search *\/
|
||||
* log_f(2, "doing full search.");
|
||||
* score = -pvs(newpos, depth - 1, -beta, -alpha, -color);
|
||||
* }
|
||||
* log(2, "\n");
|
||||
* }
|
||||
* pos->node_count += newpos->node_count;
|
||||
* move_undo(newpos, move);
|
||||
* if (score >= beta) { /\* fail-hard hard beta cut-off *\/
|
||||
* log(2, "%.*s", 5 - depth, " ");
|
||||
* log_f(2, "depth=%d score=%d alpha=%d beta=%d beta cut-off.\n",
|
||||
* depth, score, alpha, beta);
|
||||
* return beta;
|
||||
* }
|
||||
* if (score > alpha) {
|
||||
* log(2, "%.*s", 5 - depth, " ");
|
||||
* log_f(2, "depth=%d setting new alpha from %d to %d\n",
|
||||
* depth, alpha, score);
|
||||
* alpha = score;
|
||||
* pos->bestmove = move;
|
||||
* }
|
||||
* move->pos = NULL;
|
||||
* move->negamax = score;
|
||||
* firstchild = false;
|
||||
* }
|
||||
*
|
||||
* return alpha;
|
||||
* }
|
||||
*/
|
||||
|
||||
/*
|
||||
* int negascout (pos_t *pos, int depth, int alpha, int beta )
|
||||
@@ -214,24 +260,24 @@ eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color)
|
||||
* @return: The @pos negamax evaluation.
|
||||
*/
|
||||
/*int ab_negamax(pos_t *pos, int alpha, int beta, int depth)
|
||||
{
|
||||
move_t *move;
|
||||
pos_t *newpos;
|
||||
eval_t best = EVAL_MIN, score;
|
||||
{
|
||||
move_t *move;
|
||||
pos_t *newpos;
|
||||
eval_t best = EVAL_MIN, score;
|
||||
|
||||
if(depth == 0) {
|
||||
//return quiesce( alpha, beta );
|
||||
moves_gen_all_nomoves(pos);
|
||||
score = eval(pos) * color;
|
||||
return score;
|
||||
}
|
||||
for ( all moves) {
|
||||
score = -alphaBeta( -beta, -alpha, depthleft - 1 );
|
||||
if( score >= beta )
|
||||
return beta; // fail hard beta-cutoff
|
||||
if( score > alpha )
|
||||
alpha = score; // alpha acts like max in MiniMax
|
||||
}
|
||||
return alpha;
|
||||
}
|
||||
if(depth == 0) {
|
||||
//return quiesce( alpha, beta );
|
||||
moves_gen_all_nomoves(pos);
|
||||
score = eval(pos) * color;
|
||||
return score;
|
||||
}
|
||||
for ( all moves) {
|
||||
score = -alphaBeta( -beta, -alpha, depthleft - 1 );
|
||||
if( score >= beta )
|
||||
return beta; // fail hard beta-cutoff
|
||||
if( score > alpha )
|
||||
alpha = score; // alpha acts like max in MiniMax
|
||||
}
|
||||
return alpha;
|
||||
}
|
||||
*/
|
||||
|
||||
18
src/search.h
18
src/search.h
@@ -1,6 +1,6 @@
|
||||
/* search.h - search for perfect move.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -16,7 +16,21 @@
|
||||
|
||||
#include "position.h"
|
||||
|
||||
/**
|
||||
* search_values_t - search values given by "go" command
|
||||
* @depth: max depth "go depth X", MAX_DEPTH for no limit
|
||||
*
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
u16 depth; /* go depth X */
|
||||
} search_uci_t;
|
||||
|
||||
extern search_uci_t search_uci;
|
||||
|
||||
bool is_draw(pos_t *pos);
|
||||
eval_t negamax(pos_t *pos, int depth, int color);
|
||||
eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color);
|
||||
void search(pos_t *pos);
|
||||
//eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color);
|
||||
|
||||
#endif /* SEARCH_H */
|
||||
|
||||
42
src/temp-migration-bitboard/bitboard.c
Normal file
42
src/temp-migration-bitboard/bitboard.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/* bitboard.c - bitboard functions.
|
||||
*
|
||||
* Copyright (C) 2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include "br.h"
|
||||
#include "bitboard.h"
|
||||
|
||||
/* maps are clockwise from N */
|
||||
static char knight_vector[8] = {
|
||||
NORTH*2 + EAST, NORTH + EAST*2, SOUTH + EAST*2, SOUTH*2 + EAST,
|
||||
SOUTH*2 + WEST, SOUTH + WEST*2, NORTH + WEST*2, NORTH*2 + WEST
|
||||
};
|
||||
static char king_vector[8] = {
|
||||
NORTH, NORTH_EAST, EAST, SOUTH_EAST,
|
||||
SOUTH, SOUTH_WEST, WEST, NORTH_WEST
|
||||
};
|
||||
|
||||
static u64 knight_attacks[64], king_attacks[64];
|
||||
static int zob=0;
|
||||
|
||||
void bitboard_init(void)
|
||||
{
|
||||
for (int sq = SQ_0; sq < SQ_N; ++sq) {
|
||||
/* knight/king */
|
||||
for (int j = 0; j < 8; ++j) {
|
||||
zob += *knight_attacks + *king_attacks + *knight_vector +
|
||||
*king_vector;
|
||||
//int dest_knight = sq + knight_vector[j];
|
||||
//if ()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
105
src/temp-migration-bitboard/bitboard.h
Normal file
105
src/temp-migration-bitboard/bitboard.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/* bitboard.h - bitboard definitions.
|
||||
*
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BITBOARD_H
|
||||
#define BITBOARD_H
|
||||
|
||||
#include "br.h"
|
||||
#include "chessdefs.h"
|
||||
#include "piece.h"
|
||||
#include "bitops.h"
|
||||
|
||||
#define mask(s) ( 1ULL << (s) )
|
||||
#define C64(const_u64) const_u64##ULL
|
||||
|
||||
typedef enum square {
|
||||
A1, B1, C1, D1, E1, F1, G1, H1,
|
||||
A2, B2, C2, D2, E2, F2, G2, H2,
|
||||
A3, B3, C3, D3, E3, F3, G3, H3,
|
||||
A4, B4, C4, D4, E4, F4, G4, H4,
|
||||
A5, B5, C5, D5, E5, F5, G5, H5,
|
||||
A6, B6, C6, D6, E6, F6, G6, H6,
|
||||
A7, B7, C7, D7, E7, F7, G7, H7,
|
||||
A8, B8, C8, D8, E8, F8, G8, H8,
|
||||
SQ_N,
|
||||
SQ_0 = 0
|
||||
} square;
|
||||
|
||||
typedef enum file {
|
||||
FILE_A, FILE_B, FILE_C, FILE_D, FILE_E, FILE_F, FILE_G, FILE_H,
|
||||
FILE_N,
|
||||
FILE_0 = 0
|
||||
} file;
|
||||
|
||||
typedef enum rank {
|
||||
RANK_1, RANK_2, RANK_3, RANK_4, RANK_5, RANK_6, RANK_7, RANK_8,
|
||||
RANK_N,
|
||||
RANK_0 = 0
|
||||
} rank;
|
||||
|
||||
typedef enum sq_bb {
|
||||
//A1 = 0x01ULL, B1 = 0x02ULL, C1 = 1UL << 2, D1 = 1UL << 3,
|
||||
//E1 = 1UL << 4, F1 = 1UL << 5, G1 = 1UL << 6, H1 = 1UL <<
|
||||
A1bb = mask(A1), A2bb = mask(A2), A3bb = mask(A3), A4bb = mask(A4),
|
||||
A5bb = mask(A5), A6bb = mask(A6), A7bb = mask(A7), A8bb = mask(A8),
|
||||
B1bb = mask(B1), B2bb = mask(B2), B3bb = mask(B3), B4bb = mask(B4),
|
||||
B5bb = mask(B5), B6bb = mask(B6), B7bb = mask(B7), B8bb = mask(B8),
|
||||
C1bb = mask(C1), C2bb = mask(C2), C3bb = mask(C3), C4bb = mask(C4),
|
||||
C5bb = mask(C5), C6bb = mask(C6), C7bb = mask(C7), C8bb = mask(C8),
|
||||
D1bb = mask(D1), D2bb = mask(D2), D3bb = mask(D3), D4bb = mask(D4),
|
||||
D5bb = mask(D5), D6bb = mask(D6), D7bb = mask(D7), D8bb = mask(D8),
|
||||
E1bb = mask(E1), E2bb = mask(E2), E3bb = mask(E3), E4bb = mask(E4),
|
||||
E5bb = mask(E5), E6bb = mask(E6), E7bb = mask(E7), E8bb = mask(E8),
|
||||
F1bb = mask(F1), F2bb = mask(F2), F3bb = mask(F3), F4bb = mask(F4),
|
||||
F5bb = mask(F5), F6bb = mask(F6), F7bb = mask(F7), F8bb = mask(F8),
|
||||
G1bb = mask(G1), G2bb = mask(G2), G3bb = mask(G3), G4bb = mask(G4),
|
||||
G5bb = mask(G5), G6bb = mask(G6), G7bb = mask(G7), G8bb = mask(G8),
|
||||
H1bb = mask(H1), H2bb = mask(H2), H3bb = mask(H3), H4bb = mask(H4),
|
||||
H5bb = mask(H5), H6bb = mask(H6), H7bb = mask(H7), H8bb = mask(H8),
|
||||
} sq_bb;
|
||||
|
||||
typedef enum file_bb {
|
||||
FILE_Abb = 0x0101010101010101ULL,
|
||||
FILE_Bbb = 0x0202020202020202ULL,
|
||||
FILE_Cbb = 0x0404040404040404ULL,
|
||||
FILE_Dbb = 0x0808080808080808ULL,
|
||||
FILE_Ebb = 0x1010101010101010ULL,
|
||||
FILE_Fbb = 0x2020202020202020ULL,
|
||||
FILE_Gbb = 0x4040404040404040ULL,
|
||||
FILE_Hbb = 0x8080808080808080ULL,
|
||||
} file_bb;
|
||||
|
||||
typedef enum rank_bb {
|
||||
RANK_1bb = 0x00000000000000ffULL,
|
||||
RANK_2bb = 0x000000000000ff00ULL,
|
||||
RANK_3bb = 0x0000000000ff0000ULL,
|
||||
RANK_4bb = 0x00000000ff000000ULL,
|
||||
RANK_5bb = 0x000000ff00000000ULL,
|
||||
RANK_6bb = 0x0000ff0000000000ULL,
|
||||
RANK_7bb = 0x00ff000000000000ULL,
|
||||
RANK_8bb = 0xff00000000000000ULL
|
||||
} rank_bb;
|
||||
|
||||
#define NORTH 8
|
||||
#define EAST 1
|
||||
#define SOUTH -NORTH
|
||||
#define WEST -EAST
|
||||
|
||||
#define NORTH_EAST (NORTH + EAST)
|
||||
#define SOUTH_EAST (SOUTH + EAST)
|
||||
#define SOUTH_WEST (SOUTH + WEST)
|
||||
#define NORTH_WEST (NORTH + WEST)
|
||||
|
||||
void bitboard_init(void);
|
||||
|
||||
#endif /* BITBOARD_H */
|
||||
80
src/temp-migration-bitboard/board.h
Normal file
80
src/temp-migration-bitboard/board.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/* board.h - board definitions.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BOARD_H
|
||||
#define BOARD_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "chessdefs.h"
|
||||
#include "piece.h"
|
||||
|
||||
typedef struct board_s {
|
||||
piece_t piece;
|
||||
piece_list_t *s_piece;
|
||||
//struct list_head *s_piece;
|
||||
} board_t; /* 0x88 board */
|
||||
#define BOARDSIZE (8*8*2)
|
||||
|
||||
/* definitions for 0x88 representation
|
||||
*/
|
||||
#define SQ88(f, r) (((r) << 4) | (f)) /* from rank,file to sq88 */
|
||||
#define F88(s) ((s) & 0x0f) /* from sq88 to file */
|
||||
#define R88(s) ((s) >> 4) /* from sq88 to rank */
|
||||
|
||||
#define SETF88(s, r) ((s) &= 0xf0, (s) |= (r))
|
||||
#define SETR88(s, f) ((s) &= 0x0f, (s) |= (f)<<4)
|
||||
|
||||
#define SQ88_NOK(s) ((s) & 0x88) /* invalid square */
|
||||
#define SQ88_OK(s) (!SQ88_NOK(s))
|
||||
|
||||
/* definitions for bitboard representation
|
||||
*/
|
||||
#define BB(f, r) (1ULL << (8 * (r) + (f))) /* from rank,file to bitboard */
|
||||
#define SQ88_2_BB(s) (BB(F88(s), R88(s))) /* from sq88 to bitboard */
|
||||
#define FILEBB(b) ((b) % 8) /* from sq88 to file */
|
||||
#define RANKBB(b) ((b) / 8) /* from sq88 to rank */
|
||||
|
||||
#define SQ88_NOK(s) ((s) & 0x88) /* invalid square */
|
||||
#define SQ88_OK(s) (!SQ88_NOK(s))
|
||||
|
||||
/* piece human notation
|
||||
*/
|
||||
#define CHAR_EMPTY ' '
|
||||
#define CHAR_PAWN 'P'
|
||||
#define CHAR_KNIGHT 'N'
|
||||
#define CHAR_BISHOP 'B'
|
||||
#define CHAR_ROOK 'R'
|
||||
#define CHAR_QUEEN 'Q'
|
||||
#define CHAR_KING 'K'
|
||||
|
||||
/* from human to machine
|
||||
*/
|
||||
#define C2FILE(c) (tolower(c) - 'a')
|
||||
#define C2RANK(c) (tolower(c) - '1')
|
||||
/* from machine to human
|
||||
*/
|
||||
#define FILE2C(f) ((f) + 'a')
|
||||
#define RANK2C(r) ((r) + '1')
|
||||
|
||||
enum x88_square {
|
||||
x88_A1=0x00, x88_B1, x88_C1, x88_D1, x88_E1, x88_F1, x88_G1, x88_H1,
|
||||
x88_A2=0x10, x88_B2, x88_C2, x88_D2, x88_E2, x88_F2, x88_G2, x88_H2,
|
||||
x88_A3=0x20, x88_B3, x88_C3, x88_D3, x88_E3, x88_F3, x88_G3, x88_H3,
|
||||
x88_A4=0x30, x88_B4, x88_C4, x88_D4, x88_E4, x88_F4, x88_G4, x88_H4,
|
||||
x88_A5=0x40, x88_B5, x88_C5, x88_D5, x88_E5, x88_F5, x88_G5, x88_H5,
|
||||
x88_A6=0x50, x88_B6, x88_C6, x88_D6, x88_E6, x88_F6, x88_G6, x88_H6,
|
||||
x88_A7=0x60, x88_B7, x88_C7, x88_D7, x88_E7, x88_F7, x88_G7, x88_H7,
|
||||
x88_A8=0x70, x88_B8, x88_C8, x88_D8, x88_E8, x88_F8, x88_G8, x88_H8,
|
||||
};
|
||||
|
||||
#endif /* BOARD_H */
|
||||
522
src/temp-migration-bitboard/brchess.c
Normal file
522
src/temp-migration-bitboard/brchess.c
Normal file
@@ -0,0 +1,522 @@
|
||||
/* brchess.c - main loop.
|
||||
*
|
||||
* Copyright (C) 2021-2023 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <err.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
|
||||
#include <br.h>
|
||||
#include <list.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include "brchess.h"
|
||||
#include "chessdefs.h"
|
||||
#include "board.h"
|
||||
#include "piece.h"
|
||||
#include "move.h"
|
||||
#include "fen.h"
|
||||
#include "eval.h"
|
||||
#include "eval-simple.h"
|
||||
#include "search.h"
|
||||
|
||||
struct command {
|
||||
char *name; /* User printable name */
|
||||
int (*func)(pos_t *, char *); /* function doing the job */
|
||||
char *doc; /* function doc */
|
||||
};
|
||||
|
||||
/* readline example inspired by :
|
||||
* - https://thoughtbot.com/blog/tab-completion-in-gnu-readline
|
||||
* - http://web.mit.edu/gnu/doc/html/rlman_2.html
|
||||
*/
|
||||
char **commands_completion(const char *, int, int);
|
||||
char *commands_generator(const char *, int);
|
||||
char *escape(const char *);
|
||||
int quote_detector(char *, int);
|
||||
int execute_line (pos_t *, char *line);
|
||||
struct command *find_command (char *);
|
||||
char *stripwhite (char *string);
|
||||
|
||||
/* The names of functions that actually do the manipulation. */
|
||||
int do_help(pos_t *, char*);
|
||||
int do_fen(pos_t *, char*);
|
||||
int do_init(pos_t *, char*);
|
||||
int do_pos(pos_t *, char*);
|
||||
int do_genmoves(pos_t *, char*);
|
||||
int do_prmoves(pos_t *, char*);
|
||||
//int do_prmovepos(pos_t *pos, char *arg);
|
||||
int do_prpieces(pos_t *pos, char *arg);
|
||||
int do_memstats(pos_t *, char*);
|
||||
int do_eval(pos_t *, char*);
|
||||
int do_simple_eval(pos_t *, char*);
|
||||
int do_move(pos_t *, char*);
|
||||
int do_quit(pos_t *, char*);
|
||||
int do_debug(pos_t *, char*);
|
||||
int do_depth(pos_t *, char*);
|
||||
int do_search(pos_t *, char*);
|
||||
int do_pvs(pos_t *, char*);
|
||||
|
||||
struct command commands[] = {
|
||||
{ "help", do_help, "Display this text" },
|
||||
{ "?", do_help, "Synonym for 'help'" },
|
||||
{ "fen", do_fen, "Set position to FEN" },
|
||||
{ "init", do_init, "Set position to normal start position" },
|
||||
{ "pos", do_pos, "Print current position" },
|
||||
{ "quit", do_quit, "Quit" },
|
||||
{ "genmove", do_genmoves, "Generate move list for " },
|
||||
{ "prmoves", do_prmoves, "Print position move list" },
|
||||
// { "prmovepos", do_prmovepos, "Print Nth move resulting position" },
|
||||
{ "prpieces", do_prpieces, "Print Pieces (from pieces lists)" },
|
||||
{ "memstats", do_memstats, "Generate next move list" },
|
||||
{ "eval", do_eval, "Eval current position" },
|
||||
{ "simple-eval", do_simple_eval, "Simple eval current position" },
|
||||
{ "do_move", do_move, "execute nth move on current position" },
|
||||
{ "debug", do_debug, "Set log level to LEVEL" },
|
||||
{ "depth", do_depth, "Set search depth to N" },
|
||||
{ "search", do_search, "Search best move (negamax)" },
|
||||
{ "pvs", do_pvs, "Search best move (Principal Variation Search)" },
|
||||
{ NULL, (int(*)()) NULL, NULL }
|
||||
};
|
||||
|
||||
static int done=0;
|
||||
static int depth=1;
|
||||
|
||||
int brchess(pos_t *pos)
|
||||
{
|
||||
char *buffer, *s;
|
||||
|
||||
rl_attempted_completion_function = commands_completion;
|
||||
rl_completer_quote_characters = "'\"";
|
||||
rl_completer_word_break_characters = " ";
|
||||
rl_char_is_quoted_p = "e_detector;
|
||||
|
||||
while (!done) {
|
||||
buffer = readline("chess> ");
|
||||
if (!buffer)
|
||||
break;
|
||||
/* Remove leading and trailing whitespace from the line.
|
||||
* Then, if there is anything left, add it to the history list
|
||||
* and execute it.
|
||||
*/
|
||||
s = stripwhite(buffer);
|
||||
|
||||
if (*s) {
|
||||
add_history(s);
|
||||
execute_line(pos, s);
|
||||
}
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//char **commands_completion(const char *text, int start, int end)
|
||||
char **commands_completion(const char *text, __unused int start, __unused int end)
|
||||
{
|
||||
rl_attempted_completion_over = 1;
|
||||
return rl_completion_matches(text, commands_generator);
|
||||
}
|
||||
|
||||
char *commands_generator(const char *text, int state)
|
||||
{
|
||||
static int list_index, len;
|
||||
char *name;
|
||||
|
||||
if (!state) {
|
||||
list_index = 0;
|
||||
len = strlen(text);
|
||||
}
|
||||
|
||||
while ((name = commands[list_index++].name)) {
|
||||
if (rl_completion_quote_character) {
|
||||
name = strdup(name);
|
||||
} else {
|
||||
name = escape(name);
|
||||
}
|
||||
|
||||
if (strncmp(name, text, len) == 0) {
|
||||
return name;
|
||||
} else {
|
||||
free(name);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *escape(const char *original)
|
||||
{
|
||||
size_t original_len;
|
||||
size_t i, j;
|
||||
char *escaped, *resized_escaped;
|
||||
|
||||
original_len = strlen(original);
|
||||
|
||||
if (original_len > SIZE_MAX / 2) {
|
||||
errx(1, "string too long to escape");
|
||||
}
|
||||
|
||||
if ((escaped = malloc(2 * original_len + 1)) == NULL) {
|
||||
err(1, NULL);
|
||||
}
|
||||
|
||||
for (i = 0, j = 0; i < original_len; ++i, ++j) {
|
||||
if (original[i] == ' ') {
|
||||
escaped[j++] = '\\';
|
||||
}
|
||||
escaped[j] = original[i];
|
||||
}
|
||||
escaped[j] = '\0';
|
||||
|
||||
if ((resized_escaped = realloc(escaped, j)) == NULL) {
|
||||
free(escaped);
|
||||
resized_escaped = NULL;
|
||||
err(1, NULL);
|
||||
}
|
||||
|
||||
return resized_escaped;
|
||||
}
|
||||
|
||||
int quote_detector(char *line, int index)
|
||||
{
|
||||
return index > 0
|
||||
&& line[index - 1] == '\\'
|
||||
&&!quote_detector(line, index - 1);
|
||||
}
|
||||
|
||||
/* Execute a command line. */
|
||||
int execute_line(pos_t *pos, char *line)
|
||||
{
|
||||
register int i;
|
||||
struct command *command;
|
||||
char *word;
|
||||
|
||||
/* Isolate the command word. */
|
||||
i = 0;
|
||||
while (line[i] && whitespace(line[i]))
|
||||
i++;
|
||||
word = line + i;
|
||||
|
||||
while (line[i] && !whitespace(line[i]))
|
||||
i++;
|
||||
|
||||
if (line[i])
|
||||
line[i++] = '\0';
|
||||
|
||||
command = find_command(word);
|
||||
|
||||
if (!command) {
|
||||
fprintf(stderr, "%s: Unknown command.\n", word);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Get argument to command, if any. */
|
||||
while (whitespace(line[i]))
|
||||
i++;
|
||||
|
||||
word = line + i;
|
||||
|
||||
/* return command number */
|
||||
return (*command->func)(pos, word);
|
||||
}
|
||||
|
||||
/* Look up NAME as the name of a command, and return a pointer to that
|
||||
command. Return a NULL pointer if NAME isn't a command name. */
|
||||
struct command *find_command(char *name)
|
||||
{
|
||||
register int i;
|
||||
|
||||
for (i = 0; commands[i].name; i++)
|
||||
if (strcmp(name, commands[i].name) == 0)
|
||||
return &commands[i];
|
||||
|
||||
return (struct command *)NULL;
|
||||
}
|
||||
|
||||
/* Strip whitespace from the start and end of STRING. Return a pointer
|
||||
into STRING. */
|
||||
char *stripwhite(char *string)
|
||||
{
|
||||
register char *s, *t;
|
||||
|
||||
for (s = string; whitespace(*s); s++)
|
||||
;
|
||||
|
||||
if (*s == 0)
|
||||
return s;
|
||||
|
||||
t = s + strlen(s) - 1;
|
||||
while (t > s && whitespace(*t))
|
||||
t--;
|
||||
*++t = '\0';
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
int do_eval(__unused pos_t *pos, __unused char *arg)
|
||||
{
|
||||
eval_t material[2], control[2], mobility[2];
|
||||
for (int color = WHITE; color <= BLACK; ++color) {
|
||||
material[color] = eval_material(pos, color);
|
||||
control[color] = eval_square_control(pos, color);
|
||||
mobility[color] = eval_mobility(pos, color);
|
||||
printf("%s: material=%d mobility=%d controlled=%d\n",
|
||||
color? "Black": "White", material[color],
|
||||
mobility[color], control[color]);
|
||||
}
|
||||
eval_t res = eval(pos);
|
||||
printf("eval = %d centipawns\n", res);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_simple_eval(__unused pos_t *pos, __unused char *arg)
|
||||
{
|
||||
eval_t eval = eval_simple(pos);
|
||||
printf("eval = %d centipawns\n", eval);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_fen(pos_t *pos, char *arg)
|
||||
{
|
||||
fen2pos(pos, arg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_init(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
pos_startpos(pos);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_pos(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
pos_print(pos);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_genmoves(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
moves_gen_all(pos);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_prmoves(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
uint debug_level = debug_level_get();
|
||||
debug_level_set(1);
|
||||
moves_print(pos, M_PR_SEPARATE | M_PR_NUM | M_PR_LONG);
|
||||
debug_level_set(debug_level);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* int do_prmovepos(pos_t *pos, char *arg)
|
||||
* {
|
||||
* struct list_head *p_cur, *tmp;
|
||||
* int movenum = atoi(arg), cur = 0; /\* starts with 0 *\/
|
||||
* move_t *move;
|
||||
*
|
||||
* log_f(1, "%s\n", arg);
|
||||
* list_for_each_safe(p_cur, tmp, &pos->moves[pos->turn]) {
|
||||
* move = list_entry(p_cur, move_t, list);
|
||||
* if (cur++ == movenum) {
|
||||
* pos_print(move->newpos);
|
||||
* break;
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* return 1;
|
||||
* }
|
||||
*/
|
||||
|
||||
int do_prpieces(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
log_f(1, "%s\n", arg);
|
||||
pos_pieces_print(pos);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_memstats(__unused pos_t *pos,__unused char *arg)
|
||||
{
|
||||
moves_pool_stats();
|
||||
piece_pool_stats();
|
||||
pos_pool_stats();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_move(__unused pos_t *pos, __unused char *arg)
|
||||
{
|
||||
int i = 1, nmove = atoi(arg);
|
||||
move_t *move;
|
||||
pos_t *newpos;
|
||||
|
||||
if (list_empty(&pos->moves[pos->turn])) {
|
||||
log_f(1, "No moves list.\n");
|
||||
return 0;
|
||||
}
|
||||
list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
if (i == nmove)
|
||||
goto doit;
|
||||
i++;
|
||||
}
|
||||
log_f(1, "Invalid <%d> move, should be <1-%d>.\n", nmove, i);
|
||||
return 0;
|
||||
doit:
|
||||
newpos = move_do(pos, move);
|
||||
pos_print(newpos);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_quit(__unused pos_t *pos, __unused char *arg)
|
||||
{
|
||||
return done = 1;
|
||||
}
|
||||
|
||||
int do_debug(__unused pos_t *pos, __unused char *arg)
|
||||
{
|
||||
debug_level_set(atoi(arg));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Print out help for ARG, or for all of the commands if ARG is
|
||||
not present. */
|
||||
int do_help(__unused pos_t *pos, __unused char *arg)
|
||||
{
|
||||
int i;
|
||||
int printed = 0;
|
||||
|
||||
for (i = 0; commands[i].name; i++) {
|
||||
if (!*arg || (strcmp(arg, commands[i].name) == 0)) {
|
||||
printf("%-11.11s%s.\n", commands[i].name, commands[i].doc);
|
||||
printed++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!printed) {
|
||||
printf("No commands match `%s'. Possibilties are:\n", arg);
|
||||
|
||||
for (i = 0; commands[i].name; i++) {
|
||||
/* Print in six columns. */
|
||||
if (printed == 6) {
|
||||
printed = 0;
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("%s\t", commands[i].name);
|
||||
printed++;
|
||||
}
|
||||
|
||||
if (printed)
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int do_depth(__unused pos_t *pos, char *arg)
|
||||
{
|
||||
depth = atoi(arg);
|
||||
printf("depth = %d\n", depth);
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
int do_search(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
int debug_level = debug_level_get();
|
||||
float timer1, timer2, nodes_sec;
|
||||
|
||||
timer1 = debug_timer_elapsed();
|
||||
negamax(pos, depth, pos->turn == WHITE ? 1 : -1);
|
||||
timer2 = debug_timer_elapsed();
|
||||
nodes_sec = (float) pos->node_count / ((float) (timer2 - timer1) / (float)NANOSEC);
|
||||
log(1, "best=");
|
||||
debug_level_set(1);
|
||||
move_print(0, pos->bestmove, 0);
|
||||
debug_level_set(debug_level);
|
||||
log(1, " negamax=%d\n", pos->bestmove->negamax);
|
||||
printf("Depth:%d Nodes:%luK time:%.02fs (%.0f kn/s)\n", depth,
|
||||
pos->node_count / 1000, (timer2 - timer1)/NANOSEC, nodes_sec/1000);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int do_pvs(pos_t *pos, __unused char *arg)
|
||||
{
|
||||
int debug_level = debug_level_get();
|
||||
float timer1, timer2, nodes_sec;
|
||||
eval_t _pvs;
|
||||
|
||||
timer1 = debug_timer_elapsed();
|
||||
moves_gen_eval_sort(pos);
|
||||
_pvs = pvs(pos, depth, EVAL_MIN, EVAL_MAX, pos->turn == WHITE ? 1 : -1);
|
||||
timer2 = debug_timer_elapsed();
|
||||
nodes_sec = (float) pos->node_count / ((float) (timer2 - timer1) / (float)NANOSEC);
|
||||
log(1, "best=");
|
||||
if (pos->bestmove) {
|
||||
debug_level_set(1);
|
||||
move_print(0, pos->bestmove, 0);
|
||||
debug_level_set(debug_level);
|
||||
log(1, " pvs=%d stored=%d\n", _pvs, pos->bestmove->negamax);
|
||||
} else {
|
||||
log(1, "<no-best-move>");
|
||||
}
|
||||
printf("Depth:%d Nodes:%luK time:%.02fs (%.0f kn/s)\n", depth,
|
||||
pos->node_count / 1000, (timer2 - timer1)/NANOSEC, nodes_sec/1000);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/** main()
|
||||
* options:
|
||||
int brchess(pos_t *pos)
|
||||
*
|
||||
*/
|
||||
static int usage(char *prg)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s [-ilw] [file...]\n", prg);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
pos_t *pos;
|
||||
int opt;
|
||||
|
||||
piece_pool_init();
|
||||
moves_pool_init();
|
||||
pos_pool_init();
|
||||
pos = pos_get();
|
||||
debug_init(1, stderr, true);
|
||||
eval_simple_init();
|
||||
|
||||
while ((opt = getopt(ac, av, "d:f:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'd':
|
||||
debug_level_set(atoi(optarg));
|
||||
break;
|
||||
case 'f':
|
||||
fen2pos(pos, optarg);
|
||||
break;
|
||||
default:
|
||||
return usage(*av);
|
||||
}
|
||||
}
|
||||
if (optind < ac)
|
||||
return usage(*av);
|
||||
|
||||
return brchess(pos);
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/* pjwhash.c - PJW hash function.
|
||||
/* brchess.h - main loop.
|
||||
*
|
||||
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||
* Copyright (C) 2021-2024 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
@@ -11,10 +11,11 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#define _pjw_inline extern
|
||||
#ifndef BRCHESS_H
|
||||
#define BRCHESS_H
|
||||
|
||||
//#include "bits.h"
|
||||
//extern unsigned int pjwhash (const void* key, uint length);
|
||||
#include "position.h"
|
||||
|
||||
#include "pjwhash.h"
|
||||
#include "pjwhash-inline.h"
|
||||
int brchess(pos_t *pos);
|
||||
|
||||
#endif /* BRCHESS_H */
|
||||
129
src/temp-migration-bitboard/chessdefs.h
Normal file
129
src/temp-migration-bitboard/chessdefs.h
Normal file
@@ -0,0 +1,129 @@
|
||||
/* chessdefs.h - generic chess definitions.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CHESSDEFS_H
|
||||
#define CHESSDEFS_H
|
||||
|
||||
#include "br.h"
|
||||
|
||||
/* piece_t bits structure
|
||||
* MSB 8 7 6 5 4 3 2 1 LSB
|
||||
* 1: color (0 for white)
|
||||
* 2-7: bit set for pawn (2), knight, bishop, rook, queen, king (7)
|
||||
*/
|
||||
typedef u8 piece_t;
|
||||
|
||||
enum {
|
||||
E_EMPTY = 0,
|
||||
E_COLOR, /* LSB */
|
||||
E_PAWN,
|
||||
E_KNIGHT,
|
||||
E_BISHOP,
|
||||
E_ROOK,
|
||||
E_QUEEN,
|
||||
E_KING,
|
||||
};
|
||||
|
||||
/* pos_t bitboards tables
|
||||
*/
|
||||
enum {
|
||||
BB_ALL = 0, /* OR of all bitboards */
|
||||
BB_UNUSED, /* future use ? */
|
||||
BB_PAWN = E_PAWN,
|
||||
BB_KNIGHT,
|
||||
BB_BISHOP,
|
||||
BB_ROOK,
|
||||
BB_QUEEN,
|
||||
BB_KING,
|
||||
BB_END
|
||||
};
|
||||
|
||||
/* piece bitmask in piece_t
|
||||
*/
|
||||
enum {
|
||||
EMPTY = 0,
|
||||
PAWN = 1 << (E_PAWN - 1), /* 1<<(2-1) = 0x02 00000010 */
|
||||
KNIGHT = 1 << (E_KNIGHT - 1), /* 0x04 00000100 */
|
||||
BISHOP = 1 << (E_BISHOP - 1), /* 0x08 00001000 */
|
||||
ROOK = 1 << (E_ROOK - 1), /* 0x10 00010000 */
|
||||
QUEEN = 1 << (E_QUEEN - 1), /* 0x20 00100000 */
|
||||
KING = 1 << (E_KING - 1), /* 0x40 01000000 */
|
||||
};
|
||||
|
||||
#define PIECETOBB(p) (ffs64(PIECE(p))) /* from piece_t to bb piece array */
|
||||
|
||||
#define WHITE 0 /* 0x00 00000000 */
|
||||
#define BLACK 1 /* 0x01 00000001 */
|
||||
#define OPPONENT(p) !(p)
|
||||
|
||||
#define MASK_COLOR 0x01 /* 00000001 */
|
||||
#define MASK_PIECE 0x7E /* 01111110 */
|
||||
|
||||
#define COLOR(p) ((p) & MASK_COLOR) /* bitmask */
|
||||
#define PIECE(p) ((p) & MASK_PIECE)
|
||||
#define E_PIECE(p) (ffs64(PIECE(p))) /* convert mask to E_XX */
|
||||
|
||||
#define IS_WHITE(p) (!COLOR(p))
|
||||
#define IS_BLACK(p) (COLOR(p))
|
||||
|
||||
#define SET_WHITE(p) ((p) &= ~MASK_COLOR)
|
||||
#define SET_BLACK(p) ((p) |= MASK_COLOR)
|
||||
#define SET_COLOR(p, c) (!(c)? SET_WHITE(p): SET_BLACK(p))
|
||||
|
||||
/* flip a 0-63 square:
|
||||
* Vertical: G8 (62) becomes G1 (6)
|
||||
* Horizontal: G8 (62) becomes B8 (57)
|
||||
*/
|
||||
#define FLIP_V(sq) ((sq) ^ 56)
|
||||
#define FLIP_H(sq) ((sq) ^ 7)
|
||||
/* square_t bits structure : rrrrffff
|
||||
* ffff: file
|
||||
* rrrr: rank
|
||||
*/
|
||||
typedef uchar square_t;
|
||||
|
||||
/* castle_t bits structure
|
||||
*/
|
||||
typedef unsigned char castle_t;
|
||||
|
||||
#define CASTLE_WK (1 << 0) /* 0x01 00000001 */
|
||||
#define CASTLE_WQ (1 << 1) /* 0x02 00000010 */
|
||||
#define CASTLE_BK (1 << 2) /* 0x04 00000100 */
|
||||
#define CASTLE_BQ (1 << 3) /* 0x08 00001000 */
|
||||
|
||||
#define CASTLE_W (CASTLE_WK | CASTLE_WQ) /* 00000011 W castle mask */
|
||||
#define CASTLE_B (CASTLE_BK | CASTLE_BQ) /* 00001100 B castle mask */
|
||||
|
||||
/* game phases
|
||||
*/
|
||||
#define OPENING 0
|
||||
#define MIDDLEGAME 1
|
||||
#define ENDGAME 2
|
||||
|
||||
/* bitboard
|
||||
*/
|
||||
typedef u64 bitboard_t;
|
||||
|
||||
|
||||
/* eval type
|
||||
*/
|
||||
typedef s32 eval_t;
|
||||
|
||||
/* forward typedefs
|
||||
*/
|
||||
typedef struct piece_list_s piece_list_t;
|
||||
typedef struct board_s board_t;
|
||||
typedef struct pos_s pos_t;
|
||||
typedef struct move_s move_t;
|
||||
|
||||
#endif
|
||||
199
src/temp-migration-bitboard/eval-simple.c
Normal file
199
src/temp-migration-bitboard/eval-simple.c
Normal file
@@ -0,0 +1,199 @@
|
||||
/* eval-simple.c - simple position evaluation.
|
||||
*
|
||||
* Copyright (C) 2023 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include "br.h"
|
||||
#include "debug.h"
|
||||
|
||||
#include "piece.h"
|
||||
#include "eval-simple.h"
|
||||
#include "position.h"
|
||||
|
||||
/*
|
||||
* Tables are from https://www.chessprogramming.org/Simplified_Evaluation_Function
|
||||
*
|
||||
* Attention! Tables are black point of view (to be visually easier to read).
|
||||
*/
|
||||
|
||||
static int mg_pawn[] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
50, 50, 50, 50, 50, 50, 50, 50,
|
||||
10, 10, 20, 30, 30, 20, 10, 10,
|
||||
5, 5, 10, 25, 25, 10, 5, 5,
|
||||
0, 0, 0, 20, 20, 0, 0, 0,
|
||||
5, -5, -10, 0, 0, -10, -5, 5,
|
||||
5, 10, 10, -20, -20, 10, 10, 5,
|
||||
0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
static int mg_knight[] = {
|
||||
-50, -40, -30, -30, -30, -30, -40, -50,
|
||||
-40, -20, 0, 0, 0, 0, -20, -40,
|
||||
-30, 0, 10, 15, 15, 10, 0, -30,
|
||||
-30, 5, 15, 20, 20, 15, 5, -30,
|
||||
-30, 0, 15, 20, 20, 15, 0, -30,
|
||||
-30, 5, 10, 15, 15, 10, 5, -30,
|
||||
-40, -20, 0, 5, 5, 0, -20, -40,
|
||||
-50, -40, -30, -30, -30, -30, -40, -50
|
||||
};
|
||||
|
||||
static int mg_bishop[] = {
|
||||
-20, -10, -10, -10, -10, -10, -10, -20,
|
||||
-10, 0, 0, 0, 0, 0, 0, -10,
|
||||
-10, 0, 5, 10, 10, 5, 0, -10,
|
||||
-10, 5, 5, 10, 10, 5, 5, -10,
|
||||
-10, 0, 10, 10, 10, 10, 0, -10,
|
||||
-10, 10, 10, 10, 10, 10, 10, -10,
|
||||
-10, 5, 0, 0, 0, 0, 5, -10,
|
||||
-20, -10, -10, -10, -10, -10, -10, -20
|
||||
};
|
||||
|
||||
static int mg_rook[] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
5, 10, 10, 10, 10, 10, 10, 5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
-5, 0, 0, 0, 0, 0, 0, -5,
|
||||
0, 0, 0, 5, 5, 0, 0, 0
|
||||
};
|
||||
|
||||
static int mg_queen[] = {
|
||||
-20, -10, -10, -5, -5, -10, -10, -20,
|
||||
-10, 0, 0, 0, 0, 0, 0, -10,
|
||||
-10, 0, 5, 5, 5, 5, 0, -10,
|
||||
-5, 0, 5, 5, 5, 5, 0, -5,
|
||||
0, 0, 5, 5, 5, 5, 0, -5,
|
||||
-10, 5, 5, 5, 5, 5, 0, -10,
|
||||
-10, 0, 5, 0, 0, 0, 0, -10,
|
||||
-20, -10, -10, -5, -5, -10, -10, -20
|
||||
};
|
||||
|
||||
static int mg_king[] = {
|
||||
-30, -40, -40, -50, -50, -40, -40, -30,
|
||||
-30, -40, -40, -50, -50, -40, -40, -30,
|
||||
-30, -40, -40, -50, -50, -40, -40, -30,
|
||||
-30, -40, -40, -50, -50, -40, -40, -30,
|
||||
-20, -30, -30, -40, -40, -30, -30, -20,
|
||||
-10, -20, -20, -20, -20, -20, -20, -10,
|
||||
20, 20, 0, 0, 0, 0, 20, 20,
|
||||
20, 30, 10, 0, 0, 10, 30, 20
|
||||
};
|
||||
|
||||
static int eg_king[] = {
|
||||
-50, -40, -30, -20, -20, -30, -40, -50,
|
||||
-30, -20, -10, 0, 0, -10, -20, -30,
|
||||
-30, -10, 20, 30, 30, 20, -10, -30,
|
||||
-30, -10, 30, 40, 40, 30, -10, -30,
|
||||
-30, -10, 30, 40, 40, 30, -10, -30,
|
||||
-30, -10, 20, 30, 30, 20, -10, -30,
|
||||
-30, -30, 0, 0, 0, 0, -30, -30,
|
||||
-50, -30, -30, -30, -30, -30, -30, -50
|
||||
};
|
||||
|
||||
/* as pieces bitboard tables start at position 2; we make these tables
|
||||
* bigger.
|
||||
*/
|
||||
static int *mg_tables[] = {
|
||||
NULL,
|
||||
NULL,
|
||||
mg_pawn,
|
||||
mg_knight,
|
||||
mg_bishop,
|
||||
mg_rook,
|
||||
mg_queen,
|
||||
mg_king
|
||||
};
|
||||
|
||||
static int *eg_tables[] = {
|
||||
NULL,
|
||||
NULL,
|
||||
mg_pawn,
|
||||
mg_knight,
|
||||
mg_bishop,
|
||||
mg_rook,
|
||||
mg_queen,
|
||||
eg_king
|
||||
};
|
||||
|
||||
/* to flip vertically a square, we need to XOR it with 56
|
||||
*/
|
||||
static int mg_table[2][6 + 2][64];
|
||||
static int eg_table[2][6 + 2][64];
|
||||
|
||||
void eval_simple_init(void)
|
||||
{
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(1, "initializing piece tables\n");
|
||||
# endif
|
||||
for (int piece = BB_PAWN; piece <= BB_KING; ++piece) {
|
||||
for (int square = 0; square < 64; ++square) {
|
||||
mg_table[WHITE][piece][square] = mg_tables[piece][FLIP_V(square)];
|
||||
eg_table[WHITE][piece][square] = eg_tables[piece][FLIP_V(square)];
|
||||
mg_table[BLACK][piece][square] = mg_tables[piece][square];
|
||||
eg_table[BLACK][piece][square] = eg_tables[piece][square];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* eval_simple() - simple and fast position evaluation
|
||||
* @pos: &position to evaluate
|
||||
*
|
||||
* This function is normally used only during initialization,
|
||||
* or when changing phase (middlegame <--> endgame), as the eval
|
||||
* will be done increntally when doing moves.
|
||||
*
|
||||
* @return: the @pos evaluation in centipawns
|
||||
*/
|
||||
eval_t eval_simple(pos_t *pos)
|
||||
{
|
||||
eval_t eval[2] = { 0, 0 };
|
||||
int eg = simple_is_endgame(pos);
|
||||
int (*gg)[6 + 2][64]= eg? eg_table: mg_table;
|
||||
|
||||
pos->eval_simple_phase = ENDGAME;
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(5, "phase = %s.\n", eg? "endgame": "midgame");
|
||||
# endif
|
||||
|
||||
for (int color = WHITE; color <= BLACK; ++color) {
|
||||
for (uint piece = PAWN; piece <= KING; piece <<= 1) {
|
||||
int bb = PIECETOBB(piece), cur;
|
||||
u64 _t;
|
||||
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(5, "p=%u bb=%d %s %s: count=%d val=%ld ", piece, bb, color? "black": "white",
|
||||
P_SYM(piece), popcount64(pos->bb[color][bb]),
|
||||
popcount64(pos->bb[color][bb]) * P_VALUE(piece));
|
||||
# endif
|
||||
|
||||
eval[color] += popcount64(pos->bb[color][bb]) * P_LETTER(piece);
|
||||
bit_for_each64(cur, _t, pos->bb[color][bb]) {
|
||||
# ifdef DEBUG_EVAL
|
||||
log(5, "sq=%d:%d ", cur, gg[color][bb][cur]);
|
||||
# endif
|
||||
eval[color] += gg[color][bb][cur];
|
||||
}
|
||||
# ifdef DEBUG_EVAL
|
||||
log(5, "\n");
|
||||
# endif
|
||||
}
|
||||
}
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(2, "eval:%d white:%d black:%d\n", eval[WHITE] - eval[BLACK],
|
||||
eval[WHITE], eval[BLACK]);
|
||||
# endif
|
||||
|
||||
return eval[WHITE] - eval[BLACK];
|
||||
}
|
||||
47
src/temp-migration-bitboard/eval-simple.h
Normal file
47
src/temp-migration-bitboard/eval-simple.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/* eval-simple.h - simple position evaluation.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef EVAL_SIMPLE_H
|
||||
#define EVAL_SIMPLE_H
|
||||
|
||||
|
||||
#include "chessdefs.h"
|
||||
|
||||
/* no queen on board */
|
||||
#define simple_no_queen(p, c) \
|
||||
( !(p)->bb[c][BB_QUEEN] )
|
||||
|
||||
#define simple_one_queen(p, c) \
|
||||
( popcount64((p)->bb[c][BB_QUEEN]) == 1 )
|
||||
|
||||
#define simple_no_rook(p, c) \
|
||||
(!(p)->bb[c][BB_ROOK])
|
||||
|
||||
#define simple_one_minor_piece(p, c) \
|
||||
(popcount64((p)->bb[c][BB_KNIGHT] | (p)->bb[c][BB_BISHOP]) == 1)
|
||||
|
||||
#define simple_is_endgame(p) \
|
||||
( (simple_no_queen(p, WHITE) || \
|
||||
(simple_one_queen(p, WHITE) && \
|
||||
simple_no_rook(p, WHITE) && \
|
||||
simple_one_minor_piece(p, WHITE))) \
|
||||
&& \
|
||||
(simple_no_queen(p, BLACK) || \
|
||||
(simple_one_queen(p, BLACK) && \
|
||||
simple_no_rook(p, BLACK) && \
|
||||
simple_one_minor_piece(p, BLACK))) )
|
||||
|
||||
void eval_simple_init(void);
|
||||
eval_t eval_simple(pos_t *pos);
|
||||
|
||||
#endif /* EVAL_SIMPLE_H */
|
||||
95
src/temp-migration-bitboard/eval.c
Normal file
95
src/temp-migration-bitboard/eval.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/* eval.c - static position evaluation.
|
||||
*
|
||||
* Copyright (C) 2021-2023 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <list.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include "position.h"
|
||||
#include "eval.h"
|
||||
#include "eval-simple.h"
|
||||
|
||||
inline eval_t eval_material(pos_t *pos, bool color)
|
||||
{
|
||||
eval_t res = 0;
|
||||
|
||||
/* I need to do something about the king, if it can be potentially taken
|
||||
* if pseudo-moves include a pinned piece on King.
|
||||
*/
|
||||
for (uint piece = PAWN; piece < KING; piece <<= 1) {
|
||||
uint bb = PIECETOBB(piece);
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(2, "color=%u piece=%u bb=%u=%c count=%ul val=%ld\n",
|
||||
color, piece, bb, P_LETTER(piece), popcount64(pos->bb[color][bb]),
|
||||
P_VALUE(piece));
|
||||
# endif
|
||||
/* attention here */
|
||||
res += popcount64(pos->bb[color][bb]) * P_VALUE(piece);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
inline eval_t eval_mobility(pos_t *pos, bool color)
|
||||
{
|
||||
return pos->mobility[color];
|
||||
}
|
||||
|
||||
inline eval_t eval_square_control(pos_t *pos, bool color)
|
||||
{
|
||||
return popcount64(pos->controlled[color]);
|
||||
}
|
||||
|
||||
eval_t eval(pos_t *pos)
|
||||
{
|
||||
eval_t simple = 0, control[2] = {0};
|
||||
|
||||
if (pos->eval != EVAL_INVALID)
|
||||
return pos->eval;
|
||||
|
||||
/* 1) pieces value */
|
||||
//material[WHITE] = eval_material(pos, WHITE);
|
||||
//material[BLACK] = eval_material(pos, BLACK);
|
||||
simple = eval_simple(pos);
|
||||
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(2, "eval_simple=%d\n", simple);
|
||||
# endif
|
||||
|
||||
/* 2) square control: 10 square controls diff = 1 pawn */
|
||||
control[WHITE] = eval_square_control(pos, WHITE);
|
||||
control[BLACK] = eval_square_control(pos, BLACK);
|
||||
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(2, "square control: W:%d B:%d diff=%d\n",
|
||||
control[WHITE], control[BLACK],
|
||||
(control[WHITE] - control[BLACK]) * 10);
|
||||
# endif
|
||||
|
||||
/* 3) mobility: 10 mobility diff = 1 pawn
|
||||
*/
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(2, "mobility: W:%u B:%u diff=%d\n",
|
||||
pos->mobility[WHITE], pos->mobility[BLACK],
|
||||
(pos->mobility[WHITE] - pos->mobility[BLACK]) * 10);
|
||||
# endif
|
||||
|
||||
eval_t res = simple +
|
||||
(control[WHITE] - control[BLACK]) * 10 +
|
||||
(pos->mobility[WHITE] - pos->mobility[BLACK]) * 10;
|
||||
# ifdef DEBUG_EVAL
|
||||
log_f(2, "eval: %d\n", res);
|
||||
# endif
|
||||
pos->eval = res;
|
||||
return res;
|
||||
}
|
||||
37
src/temp-migration-bitboard/eval.h
Normal file
37
src/temp-migration-bitboard/eval.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/* eval.h - static position evaluation.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef EVAL_H
|
||||
#define EVAL_H
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "piece.h"
|
||||
|
||||
/* max pieces eval is KING_VALUE + 9*QUEEN_VALUE + 2*ROOK_VALUE + 2*BISHOP_VALUE
|
||||
* + 2*KNIGHT_VALUE which around 30000.
|
||||
* We are on secure side with -50000/+50000
|
||||
*/
|
||||
#define EVAL_MAX (50000)
|
||||
#define EVAL_MIN (-EVAL_MAX)
|
||||
#define EVAL_INVALID INT_MIN
|
||||
#define EVAL_MATE EVAL_MAX
|
||||
|
||||
eval_t eval_material(pos_t *pos, bool color);
|
||||
eval_t eval_mobility(pos_t *pos, bool color);
|
||||
eval_t eval_square_control(pos_t *pos, bool color);
|
||||
|
||||
eval_t eval(pos_t *pos);
|
||||
|
||||
#endif /* EVAL_H */
|
||||
175
src/temp-migration-bitboard/fen.c
Normal file
175
src/temp-migration-bitboard/fen.c
Normal file
@@ -0,0 +1,175 @@
|
||||
/* fen.c - fen notation.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <debug.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "position.h"
|
||||
#include "board.h"
|
||||
#include "fen.h"
|
||||
#include "piece.h"
|
||||
|
||||
/* Starting Position :
|
||||
* rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
|
||||
* After 1.e4 :
|
||||
* rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1
|
||||
* After 1... c5 :
|
||||
* rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2
|
||||
* After 2. Nf3:
|
||||
* rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2
|
||||
*
|
||||
* 1 : White uppercase
|
||||
* 2 : next move (w or b)
|
||||
* 3 : Castling capabilities: "-" if none, KQ/kq if white/black can castle
|
||||
* on K or Q side
|
||||
* 4 : en-passant: if pawn just moved 2 squares, indicate target square (e.g.
|
||||
* for e2-e4 this field is e3)
|
||||
* 5 : half moves since last capture or pawn advance (for 50 moves rule)
|
||||
* 6 : full moves, starts at 1, increments after black move
|
||||
*
|
||||
*/
|
||||
|
||||
// warning, we expect a valid fen input
|
||||
pos_t *fen2pos(pos_t *pos, char *fen)
|
||||
{
|
||||
char *p = fen;
|
||||
short rank, file, skip, color, bbpiece;
|
||||
piece_t piece;
|
||||
board_t *board = pos->board;
|
||||
# define SKIP_BLANK(p) for(;*(p) == ' '; (p)++)
|
||||
|
||||
pos_clear(pos);
|
||||
/* 1) get piece placement information
|
||||
*/
|
||||
for (rank = 7, file = 0; *p && *p != ' '; ++p) {
|
||||
color = isupper(*p)? WHITE: BLACK;
|
||||
char cp = toupper(*p);
|
||||
switch (cp) {
|
||||
case CHAR_PAWN:
|
||||
bbpiece = BB_PAWN;
|
||||
piece = PAWN;
|
||||
goto set_square;
|
||||
case CHAR_KNIGHT:
|
||||
bbpiece = BB_KNIGHT;
|
||||
piece = KNIGHT;
|
||||
goto set_square;
|
||||
case CHAR_BISHOP:
|
||||
bbpiece = BB_BISHOP;
|
||||
piece = BISHOP;
|
||||
goto set_square;
|
||||
case CHAR_ROOK:
|
||||
bbpiece = BB_ROOK;
|
||||
piece = ROOK;
|
||||
goto set_square;
|
||||
case CHAR_QUEEN:
|
||||
bbpiece = BB_QUEEN;
|
||||
piece = QUEEN;
|
||||
goto set_square;
|
||||
case CHAR_KING:
|
||||
bbpiece = BB_KING;
|
||||
piece = KING;
|
||||
//pos->bb[color][BB_KING] = BB(file, rank);
|
||||
//goto set_square;
|
||||
set_square:
|
||||
# ifdef DEBUG_FEN
|
||||
log_i(5, "f=%d r=%d *p=%c piece=%c color=%d\n",
|
||||
file, rank, *p, cp, color);
|
||||
# endif
|
||||
pos->bb[color][bbpiece] |= BB(file, rank);
|
||||
pos->occupied[color] |= BB(file, rank);
|
||||
SET_COLOR(piece, color);
|
||||
board[SQ88(file, rank)].piece = piece;
|
||||
board[SQ88(file, rank)].s_piece =
|
||||
piece_add(pos, piece, SQ88(file, rank));
|
||||
file++;
|
||||
break;
|
||||
case '/':
|
||||
rank--;
|
||||
file = 0;
|
||||
break;
|
||||
default:
|
||||
skip = cp - '0';
|
||||
while (skip--) {
|
||||
board[SQ88(file++, rank)].piece = EMPTY;
|
||||
}
|
||||
}
|
||||
}
|
||||
# ifdef DEBUG_FEN
|
||||
for (rank = 7; rank >= 0; --rank) {
|
||||
for (file = 0; file < 8; ++file) {
|
||||
log(5, "%02x ", board[SQ88(file, rank)].piece);
|
||||
}
|
||||
log(5, "\n");
|
||||
}
|
||||
# endif
|
||||
|
||||
/* 2) next move color
|
||||
*/
|
||||
SKIP_BLANK(p);
|
||||
SET_COLOR(pos->turn, *p == 'w' ? WHITE : BLACK);
|
||||
p++;
|
||||
|
||||
/* 3) castle status
|
||||
*/
|
||||
SKIP_BLANK(p);
|
||||
pos->castle = 0;
|
||||
if (*p != '-') {
|
||||
for (; *p && *p != ' '; ++p) {
|
||||
switch (*p) {
|
||||
case 'K':
|
||||
pos->castle |= CASTLE_WK;
|
||||
break;
|
||||
case 'k':
|
||||
pos->castle |= CASTLE_BK;
|
||||
break;
|
||||
case 'Q':
|
||||
pos->castle |= CASTLE_WQ;
|
||||
break;
|
||||
case 'q':
|
||||
pos->castle |= CASTLE_BQ;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
p++;
|
||||
|
||||
/* 4) en passant
|
||||
*/
|
||||
SKIP_BLANK(p);
|
||||
pos->en_passant = 0;
|
||||
if (*p != '-') {
|
||||
//SET_F(pos->en_passant, C2FILE(*p++));
|
||||
//SET_R(pos->en_passant, C2RANK(*p++));
|
||||
pos->en_passant = SQ88(C2FILE(*p), C2RANK(*(p+1)));
|
||||
pos += 2;
|
||||
} else {
|
||||
p++;
|
||||
}
|
||||
|
||||
/* 5) half moves since last capture or pawn move and
|
||||
* 6) current move number
|
||||
*/
|
||||
SKIP_BLANK(p);
|
||||
//log_i(5, "pos=%d\n", (int)(p-fen));
|
||||
sscanf(p, "%hd %hd", &pos->clock_50, &pos->curmove);
|
||||
# ifdef DEBUG_FEN
|
||||
log_i(5, "50 rule=%d current move=%d\n", pos->clock_50, pos->curmove);
|
||||
# endif
|
||||
return pos;
|
||||
}
|
||||
21
src/temp-migration-bitboard/fen.h
Normal file
21
src/temp-migration-bitboard/fen.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/* fen.h - fen notation.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef FEN_H
|
||||
#define FEN_H
|
||||
|
||||
#include "position.h"
|
||||
|
||||
pos_t *fen2pos(pos_t *pos, char *fen);
|
||||
|
||||
#endif /* FEN_H */
|
||||
890
src/temp-migration-bitboard/move.c
Normal file
890
src/temp-migration-bitboard/move.c
Normal file
@@ -0,0 +1,890 @@
|
||||
/* move.c - move management.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <malloc.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <br.h>
|
||||
#include <list.h>
|
||||
#include <list_sort.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "board.h"
|
||||
#include "bitboard.h"
|
||||
#include "piece.h"
|
||||
#include "move.h"
|
||||
#include "eval.h"
|
||||
#include "eval-simple.h"
|
||||
|
||||
static pool_t *moves_pool;
|
||||
|
||||
static struct vector {
|
||||
unsigned char ndirs;
|
||||
char slide;
|
||||
char dir[8];
|
||||
} vectors[] = {
|
||||
[KNIGHT] = { 8, 0, { -33, -31, -18, -14, 14, 18, 31, 33 }},
|
||||
[BISHOP] = { 4, 1, { -15, -17, 15, 17 }},
|
||||
[ROOK] = { 4, 1, { -1, -16, 1, 16 }},
|
||||
[QUEEN] = { 8, 1, { -1, -16, 1, 16, -15, -17, 15, 17 }},
|
||||
[KING] = { 8, 0, { -1, -16, 1, 16, -15, -17, 15, 17 }}
|
||||
};
|
||||
|
||||
|
||||
/* squares needed to be empty & not controlled by opponent for castle.
|
||||
* For black castle, same values 7 rows higher (>> 7*8)
|
||||
*/
|
||||
static struct can_castle {
|
||||
bitboard_t controlled[2];
|
||||
bitboard_t occupied[2];
|
||||
} castle_squares[2] = {
|
||||
/* Queen side King side Queen side King side */
|
||||
{ { C1bb|D1bb|E1bb, E1bb|F1bb|G1bb }, { B1bb|C1bb|D1bb, F1bb|G1bb } },
|
||||
{ { C8bb|D8bb|E8bb, E8bb|F8bb|G8bb }, { B8bb|C8bb|D8bb, F8bb|G8bb } }
|
||||
};
|
||||
|
||||
pool_t *moves_pool_init()
|
||||
{
|
||||
if (!moves_pool)
|
||||
moves_pool = pool_create("moves", 128, sizeof(move_t));
|
||||
return moves_pool;
|
||||
}
|
||||
|
||||
void moves_pool_stats()
|
||||
{
|
||||
if (moves_pool)
|
||||
pool_stats(moves_pool);
|
||||
}
|
||||
|
||||
/**
|
||||
* move_print() - print a move
|
||||
* @movenum: move number
|
||||
* @move: &move to display
|
||||
* @flags: options to display
|
||||
*
|
||||
* Possible flags are:
|
||||
* M_PR_CAPT: print move if capture
|
||||
* M_PR_NCAPT: print move if non capture
|
||||
* M_PR_NUM: print also move number
|
||||
* M_PR_LONG: print long notation
|
||||
* M_PR_NL: print a newline after move
|
||||
* M_PR_EVAL: print move eval
|
||||
*
|
||||
* @return: 0 if nothing printed, 1 otherwise
|
||||
*/
|
||||
int move_print(int movenum, move_t *move, move_flags_t flags)
|
||||
{
|
||||
if ((flags & M_PR_CAPT) && !(move->flags & M_CAPTURE)) {
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(9, "skipping capture & %#04x\n", move->flags);
|
||||
# endif
|
||||
return 0;
|
||||
}
|
||||
if ((flags & M_PR_NCAPT) && (move->flags & M_CAPTURE)) {
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(9, "skipping !capture & %#04x\n", move->flags);
|
||||
# endif
|
||||
return 0;
|
||||
}
|
||||
if (flags & M_PR_NUM)
|
||||
log(1, "%d:", movenum);
|
||||
if (move->flags & M_CASTLE_K) {
|
||||
log(1, "O-O");
|
||||
goto end;
|
||||
} else if (move->flags & M_CASTLE_Q) {
|
||||
log(1, "O-O-O");
|
||||
goto end;
|
||||
} else {
|
||||
log(1, "%s%c%c", P_SYM(move->piece),
|
||||
FILE2C(F88(move->from)),
|
||||
RANK2C(R88(move->from)));
|
||||
if (move->flags & M_CAPTURE) {
|
||||
log(1, "x");
|
||||
if (flags & M_PR_LONG)
|
||||
log(1, "%s", P_SYM(move->capture));
|
||||
} else {
|
||||
log(1, "-");
|
||||
}
|
||||
log(1, "%c%c",
|
||||
FILE2C(F88(move->to)),
|
||||
RANK2C(R88(move->to)));
|
||||
if (flags & M_PR_LONG && move->flags & M_EN_PASSANT)
|
||||
log(1, "e.p.");
|
||||
if (move->promotion)
|
||||
log(1, "=%s", P_SYM(move->promotion));
|
||||
if (flags & M_PR_EVAL)
|
||||
log(1, "[ev:%d] ", move->eval);
|
||||
end:
|
||||
log(1, " ");
|
||||
}
|
||||
if (flags & M_PR_NL)
|
||||
log(1, "\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
void moves_print(pos_t *pos, move_flags_t flags)
|
||||
{
|
||||
struct list_head *p_cur, *tmp;
|
||||
move_t *move;
|
||||
int movenum;
|
||||
|
||||
for (int color=WHITE; color <= BLACK; ++color) {
|
||||
int verif = 0;
|
||||
log(1, "%s pseudo-moves:\n\t", color == WHITE? "White": "Black");
|
||||
if (! (flags & M_PR_SEPARATE)) {
|
||||
movenum = 1;
|
||||
list_for_each_safe(p_cur, tmp, &pos->moves[color]) {
|
||||
move = list_entry(p_cur, move_t, list);
|
||||
verif += move_print(movenum++, move, flags);
|
||||
}
|
||||
} else {
|
||||
log(1, "captures: ");
|
||||
movenum = 1;
|
||||
list_for_each_safe(p_cur, tmp, &pos->moves[color]) {
|
||||
move = list_entry(p_cur, move_t, list);
|
||||
verif += move_print(movenum++, move, flags | M_PR_CAPT);
|
||||
}
|
||||
movenum = 1;
|
||||
log(1, "\n\tothers : ");
|
||||
list_for_each_safe(p_cur, tmp, &pos->moves[color]) {
|
||||
move = list_entry(p_cur, move_t, list);
|
||||
verif += move_print(movenum++, move, flags | M_PR_NCAPT);
|
||||
}
|
||||
}
|
||||
log(1, "\n\tTotal moves = %d mobility=%u\n", verif, pos->mobility[color]);
|
||||
}
|
||||
}
|
||||
|
||||
static move_t *move_add(pos_t *pos, piece_t piece, square_t from,
|
||||
square_t to)
|
||||
{
|
||||
board_t *board = pos->board;
|
||||
move_t *move;
|
||||
int color = COLOR(pos->board[from].piece);
|
||||
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(3, "piece_color=%d turn=%d from=%c%c to=%c%c\n",
|
||||
color, pos->turn,
|
||||
FILE2C(F88(from)),
|
||||
RANK2C(R88(from)),
|
||||
FILE2C(F88(to)),
|
||||
RANK2C(R88(to)));
|
||||
# endif
|
||||
/* invalid position if opponent king is attacked
|
||||
*/
|
||||
if (board[to].piece & KING) {
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(2, "invalid position: opponent king [%c%c] is in check.\n",
|
||||
FILE2C(F88(to)), RANK2C(R88(to)));
|
||||
# endif
|
||||
return NULL;
|
||||
}
|
||||
if (!(move = pool_get(moves_pool)))
|
||||
return NULL;
|
||||
list_add(&move->list, &pos->moves[color]);
|
||||
|
||||
move->piece = piece | color;
|
||||
move->from = from;
|
||||
move->to = to;
|
||||
move->capture = board[to].piece;
|
||||
if (PIECE(move->capture) == KING)
|
||||
pos->check[color]++;
|
||||
|
||||
move->flags = M_NORMAL;
|
||||
if (move->capture)
|
||||
move->flags |= M_CAPTURE;
|
||||
move->pos = NULL;
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(3, "added %s %s move from %c%c to %c%c\n",
|
||||
COLOR(move->piece)? "black": "white",
|
||||
P_NAME(PIECE(move->piece)),
|
||||
FILE2C(F88(move->from)), RANK2C(R88(move->from)),
|
||||
FILE2C(F88(move->to)), RANK2C(R88(move->to)));
|
||||
# endif
|
||||
return move;
|
||||
}
|
||||
|
||||
/**
|
||||
* move_del() - delete a move from list.
|
||||
* @ptr: move &list_head
|
||||
*
|
||||
* Remove the move whose 'list' element address is @ptr.
|
||||
*/
|
||||
void move_del(struct list_head *ptr)
|
||||
{
|
||||
move_t *move = list_entry(ptr, move_t, list);
|
||||
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(3, "delete move from %c%c to %c%c\n",
|
||||
FILE2C(F88(move->from)), RANK2C(R88(move->from)),
|
||||
FILE2C(F88(move->to)), RANK2C(R88(move->to)));
|
||||
# endif
|
||||
|
||||
if (move->pos)
|
||||
pos_del(move->pos);
|
||||
list_del(ptr);
|
||||
pool_add(moves_pool, move);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* move_del() - delete all position moves.
|
||||
* @ppos: &position.
|
||||
*
|
||||
* Remove all generated moves from @pos structure.
|
||||
*/
|
||||
int moves_del(pos_t *pos)
|
||||
{
|
||||
struct list_head *p_cur, *tmp, *head;
|
||||
int count = 0;
|
||||
|
||||
for (int color = WHITE; color <= BLACK; ++color) {
|
||||
head = &pos->moves[color];
|
||||
|
||||
list_for_each_safe(p_cur, tmp, head) {
|
||||
move_del(p_cur);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
# ifdef DEBUG_PIECE
|
||||
log_f(3, "%d moves removed\n", count);
|
||||
# endif
|
||||
return count;
|
||||
}
|
||||
|
||||
/* TODO: return nmoves */
|
||||
static move_t *move_pawn_add(pos_t *pos, piece_t piece, square_t from,
|
||||
square_t to, unsigned char rank7)
|
||||
{
|
||||
move_t *move;
|
||||
piece_t promote;
|
||||
unsigned char color = COLOR(piece);
|
||||
|
||||
if (R88(from) == rank7) { /* promotion */
|
||||
for (promote = QUEEN; promote > PAWN; promote >>= 1) {
|
||||
if ((move = move_add(pos, piece, from, to))) {
|
||||
move->flags |= M_PROMOTION;
|
||||
move->promotion = promote | color;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
move = move_add(pos, piece, from, to);
|
||||
}
|
||||
return move;
|
||||
}
|
||||
|
||||
/**
|
||||
* pseudo_moves_pawn() - generate moves for pawn.
|
||||
* @pos: &position
|
||||
* @ppiece: &piece_list pawn structure pointer
|
||||
* @doit: add move to moves list
|
||||
*
|
||||
* Calculate all possible moves for @ppiece pawn.
|
||||
* If @doit is true, add moves to @pos' moves list.
|
||||
*/
|
||||
int pseudo_moves_pawn(pos_t *pos, piece_list_t *ppiece, bool doit)
|
||||
{
|
||||
piece_t piece = PIECE(ppiece->piece);
|
||||
unsigned char color = COLOR(ppiece->piece);
|
||||
square_t square = ppiece->square, new;
|
||||
board_t *board = pos->board;
|
||||
unsigned char rank2, rank7, rank5;
|
||||
move_t *move = NULL;
|
||||
char dir;
|
||||
int count = 0;
|
||||
|
||||
/* setup direction */
|
||||
if (IS_WHITE(color)) {
|
||||
dir = 1;
|
||||
rank2 = 1;
|
||||
rank7 = 6;
|
||||
rank5 = 4;
|
||||
} else {
|
||||
dir = -1;
|
||||
rank2 = 6;
|
||||
rank7 = 1;
|
||||
rank5 = 3;
|
||||
}
|
||||
|
||||
# ifdef DEBUG_MOVE
|
||||
log_f(2, "pos:%p turn:%s piece:%d [%s %s] dir:%d at %#04x[%c%c]\n",
|
||||
pos,
|
||||
IS_WHITE(pos->turn)? "white": "black",
|
||||
piece,
|
||||
IS_WHITE(color)? "white": "black",
|
||||
P_NAME(piece),
|
||||
dir,
|
||||
square,
|
||||
FILE2C(F88(square)), RANK2C(R88(square)));
|
||||
# endif
|
||||
|
||||
/* normal push. We do not test for valid destination square here,
|
||||
* assuming position is valid. Is that correct ?
|
||||
*/
|
||||
new = square + dir * 16;
|
||||
if (!board[new].piece) {
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(4, "pushing pawn %#04x\n", square);
|
||||
# endif
|
||||
pos->mobility[color]++;
|
||||
count++;
|
||||
if (doit)
|
||||
move_pawn_add(pos, piece | color, square, new, rank7);
|
||||
/* push 2 squares */
|
||||
log(4, "R88(%#x)=%d R2=%d \n", square, R88(square), rank2);
|
||||
if (R88(square) == rank2) {
|
||||
new += dir * 16;
|
||||
if (SQ88_OK(new) && !board[new].piece) {
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(4, "pushing pawn %#04x 2 squares\n", square);
|
||||
# endif
|
||||
//log_f(2, "pawn move2 mobility\n");
|
||||
pos->mobility[color]++;
|
||||
count++;
|
||||
if (doit)
|
||||
move_pawn_add(pos, piece | color, square, new, rank7);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* en passant - not accounted for mobility. Correct ? */
|
||||
if (pos->en_passant && R88(square) == rank5) {
|
||||
unsigned char ep_file = F88(pos->en_passant);
|
||||
unsigned char sq_file = F88(square);
|
||||
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(4, "possible en passant on rank %#x (current = %#0x)\n", ep_file,
|
||||
sq_file);
|
||||
# endif
|
||||
if (sq_file == ep_file - 1 || sq_file == ep_file + 1) {
|
||||
square_t t_square = SQ88(ep_file, rank5); /* taken pawn square */
|
||||
piece_t captured = board[t_square].piece;
|
||||
move = move_pawn_add(pos, piece | color , square, pos->en_passant, rank7);
|
||||
move->flags |= M_EN_PASSANT | M_CAPTURE;
|
||||
move->capture = captured;
|
||||
pos->mobility[color]++;
|
||||
count++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* capture */
|
||||
int two=0;
|
||||
for (new = square + dir * 15; two < 2; new = square + dir * 17, two++) {
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(4, "pawn capture %#04x %#04x\n", square, new);
|
||||
# endif
|
||||
if (SQ88_NOK(new))
|
||||
continue;
|
||||
pos->controlled[color] |= SQ88_2_BB(new);
|
||||
if (board[new].piece && COLOR(board[new].piece) != color) {
|
||||
if (PIECE(board[new].piece) == KING)
|
||||
pos->check[color]++;
|
||||
//log_f(2, "pawn capture mobility\n");
|
||||
pos->mobility[color]++;
|
||||
count++;
|
||||
if (doit)
|
||||
move_pawn_add(pos, piece | color, square, new, rank7);
|
||||
}
|
||||
}
|
||||
# ifdef DEBUG_MOVE
|
||||
log_f(2, "pos:%p turn:%s piece:%d [%s %s] dir:%d at %#04x[%c%c] count=%d\n",
|
||||
pos,
|
||||
IS_WHITE(pos->turn)? "white": "black",
|
||||
piece,
|
||||
IS_WHITE(color)? "white": "black",
|
||||
P_NAME(piece),
|
||||
dir,
|
||||
square,
|
||||
FILE2C(F88(square)), RANK2C(R88(square)), count);
|
||||
# endif
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* pseudo_moves_castle() - generate castle moves.
|
||||
* @pos: &position
|
||||
* @color: side for which to generate moves
|
||||
* @doit: add move to moves list
|
||||
* @do_king: count king moves in mobility
|
||||
*
|
||||
* Calculate the possible castle moves for @color side.
|
||||
* If @doit is true, add moves to @pos' moves list.
|
||||
* If @do_king is true, account king moves (incl. castle) to mobility.
|
||||
*
|
||||
* @return: The number of possible king moves.
|
||||
*/
|
||||
int pseudo_moves_castle(pos_t *pos, bool color, bool doit, bool do_king)
|
||||
{
|
||||
board_t *board = pos->board;
|
||||
unsigned char rank1, castle_K, castle_Q;
|
||||
move_t *move = NULL;
|
||||
unsigned short count=0;
|
||||
struct can_castle *can_castle;
|
||||
bitboard_t controlled;
|
||||
bitboard_t occupied = pos->occupied[WHITE] | pos->occupied[BLACK];
|
||||
//pos_t *newpos;
|
||||
|
||||
# ifdef DEBUG_MOVE
|
||||
log_f(2, "pos:%p turn:%s color:%s\n",
|
||||
pos,
|
||||
IS_WHITE(pos->turn)? "white": "black",
|
||||
IS_WHITE(color)? "white": "black");
|
||||
# endif
|
||||
|
||||
if (IS_WHITE(color)) {
|
||||
rank1 = 0;
|
||||
castle_K = pos->castle & CASTLE_WK;
|
||||
castle_Q = pos->castle & CASTLE_WQ;
|
||||
can_castle = castle_squares+WHITE;
|
||||
controlled = pos->controlled[BLACK];
|
||||
} else {
|
||||
rank1 = 7;
|
||||
castle_K = pos->castle & CASTLE_BK;
|
||||
castle_Q = pos->castle & CASTLE_BQ;
|
||||
can_castle = castle_squares+BLACK;
|
||||
controlled = pos->controlled[WHITE];
|
||||
}
|
||||
if (castle_K) {
|
||||
if (occupied & can_castle->occupied[1]) {
|
||||
# ifdef DEBUG_MOVE
|
||||
log(5, "Cannot castle K side: occupied\n");
|
||||
# endif
|
||||
goto next;
|
||||
}
|
||||
if (controlled & can_castle->controlled[1]) {
|
||||
# ifdef DEBUG_MOVE
|
||||
log(5, "Cannot castle K side: controlled\n");
|
||||
# endif
|
||||
goto next;
|
||||
}
|
||||
if (do_king) {
|
||||
pos->mobility[color]++;
|
||||
count++;
|
||||
}
|
||||
if (doit) {
|
||||
move = move_add(pos, board[SQ88(4, rank1)].piece,
|
||||
SQ88(4, rank1), SQ88(6, rank1));
|
||||
if (move)
|
||||
move->flags |= M_CASTLE_K;
|
||||
}
|
||||
}
|
||||
|
||||
next:
|
||||
if (castle_Q) {
|
||||
if (occupied & can_castle->occupied[0]) {
|
||||
# ifdef DEBUG_MOVE
|
||||
log(5, "Cannot castle Q side: occupied\n");
|
||||
# endif
|
||||
goto end;
|
||||
}
|
||||
if (controlled & can_castle->controlled[0]) {
|
||||
# ifdef DEBUG_MOVE
|
||||
log(5, "Cannot castle Q side: controlled\n");
|
||||
# endif
|
||||
goto end;
|
||||
}
|
||||
if (do_king) {
|
||||
pos->mobility[color]++;
|
||||
count++;
|
||||
}
|
||||
if (doit) {
|
||||
move = move_add(pos, board[SQ88(4, rank1)].piece,
|
||||
SQ88(4, rank1), SQ88(2, rank1));
|
||||
if (move)
|
||||
move->flags |= M_CASTLE_Q;
|
||||
}
|
||||
}
|
||||
end:
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* pseudo_moves_gen() - general move generation for non pawn pieces
|
||||
* @pos: &position
|
||||
* @ppiece: &piece_list structure pointer
|
||||
* @doit: add move to moves list
|
||||
* @do_king: count king moves
|
||||
*
|
||||
* Calculate all possible moves for @ppiece.
|
||||
* If @doit is true, add moves to @pos' moves list.
|
||||
* If @do_king is true, account king moves (incl. castle) to mobility.
|
||||
*/
|
||||
int pseudo_moves_gen(pos_t *pos, piece_list_t *ppiece, bool doit, bool do_king)
|
||||
{
|
||||
piece_t piece = PIECE(ppiece->piece);
|
||||
unsigned char color = COLOR(ppiece->piece);
|
||||
struct vector *vector = vectors+piece;
|
||||
square_t square = ppiece->square;
|
||||
unsigned char ndirs = vector->ndirs;
|
||||
char slide = vector->slide;
|
||||
board_t *board = pos->board;
|
||||
//move_t *move;
|
||||
int count = 0;
|
||||
u64 bb_new;
|
||||
|
||||
# ifdef DEBUG_MOVE
|
||||
log_f(2, "pos:%p turn:%s piece:%d [%s %s] at %#04x[%c%c]\n",
|
||||
pos,
|
||||
IS_WHITE(pos->turn)? "white": "black",
|
||||
piece,
|
||||
IS_WHITE(color)? "white": "black",
|
||||
P_NAME(piece),
|
||||
square,
|
||||
FILE2C(F88(square)), RANK2C(R88(square)));
|
||||
log_i(5, "vector=%ld ndirs=%d slide=%d\n", vector-vectors, ndirs, slide);
|
||||
# endif
|
||||
|
||||
for (int curdir = 0; curdir < ndirs; ++curdir) {
|
||||
char dir = vector->dir[curdir];
|
||||
for (square_t new = square + dir; ; new = new + dir) {
|
||||
/* outside board */
|
||||
if (SQ88_NOK(new)) {
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(4,"skipping %04x (invalid square)\n", new);
|
||||
# endif
|
||||
break;
|
||||
}
|
||||
bb_new = SQ88_2_BB(new);
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(2, "trying %#x=%c%c bb=%#lx\n",
|
||||
new, FILE2C(F88(new)), RANK2C(R88(new)),
|
||||
bb_new);
|
||||
//bitboard_print(new_bitboard);
|
||||
# endif
|
||||
|
||||
pos->controlled[color] |= bb_new;;
|
||||
|
||||
/* king: do not move to opponent controlled square */
|
||||
if (piece == KING && pos->controlled[OPPONENT(color)] & bb_new) {
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(2, "%s king cannot move to %c%c\n",
|
||||
IS_WHITE(color)? "white": "black",
|
||||
FILE2C(F88(new)), RANK2C(R88(new)));
|
||||
# endif
|
||||
break;
|
||||
}
|
||||
|
||||
if (bb_new & pos->occupied[color]) {
|
||||
//bitboard_print(pos->occupied[color]);
|
||||
//bitboard_print(pos->occupied[OPPONENT(color)]);
|
||||
# ifdef DEBUG_MOVE
|
||||
log_i(2, "BB: skipping %#lx [%c%c] (same color piece)\n",
|
||||
bb_new, FILE2C(F88(new)), RANK2C(R88(new)));
|
||||
# endif
|
||||
break;
|
||||
}
|
||||
|
||||
/* we are sure the move is valid : we create move */
|
||||
if (piece != KING || do_king) {
|
||||
pos->mobility[color]++;
|
||||
count++;
|
||||
}
|
||||
if (doit)
|
||||
move_add(pos, ppiece->piece, square, new);
|
||||
if (board[new].piece) { /* stopper move */
|
||||
break;
|
||||
}
|
||||
if (!slide)
|
||||
break;
|
||||
}
|
||||
}
|
||||
# ifdef DEBUG_MOVE
|
||||
log_f(2, "pos:%p turn:%s piece:%d [%s %s] at %#04x[%c%c] count=%d\n",
|
||||
pos,
|
||||
IS_WHITE(pos->turn)? "white": "black",
|
||||
piece,
|
||||
IS_WHITE(color)? "white": "black",
|
||||
P_NAME(piece),
|
||||
square,
|
||||
FILE2C(F88(square)), RANK2C(R88(square)), count);
|
||||
# endif
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* moves_gen() - move generation for one color
|
||||
* @pos: &position
|
||||
* @color: side
|
||||
* @doit: add move to moves list
|
||||
* @do_king: count king moves
|
||||
*
|
||||
* Calculate all possible moves for @color.
|
||||
* If @doit is true, add moves to @pos' moves list.
|
||||
* If @do_king is true, account king moves (incl. castle) to mobility.
|
||||
*/
|
||||
int moves_gen(pos_t *pos, bool color, bool doit, bool do_king)
|
||||
{
|
||||
struct list_head *p_cur, *tmp, *piece_list;
|
||||
piece_list_t *piece;
|
||||
int count = 0;
|
||||
|
||||
# ifdef DEBUG_MOVE
|
||||
log_f(2, "color:%s doit:%d\n", color? "Black": "White", doit);
|
||||
# endif
|
||||
|
||||
/* do not generate moves if already done for color */
|
||||
if (!list_empty(&pos->moves[color]))
|
||||
doit = false;
|
||||
|
||||
piece_list = &pos->pieces[color];
|
||||
pos->mobility[color] = 0;
|
||||
pos->controlled[color] = 0;
|
||||
count += pseudo_moves_castle(pos, color, doit, do_king);
|
||||
list_for_each_safe(p_cur, tmp, piece_list) {
|
||||
piece = list_entry(p_cur, piece_list_t, list);
|
||||
if (PIECE(piece->piece) != PAWN)
|
||||
count += pseudo_moves_gen(pos, piece, doit, do_king);
|
||||
else
|
||||
count += pseudo_moves_pawn(pos, piece, doit);
|
||||
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* moves_gen_king_moves() - adjust king mobility
|
||||
* @pos: &position
|
||||
* @color: king color
|
||||
* @doit: add move to moves list
|
||||
*
|
||||
* Compute the number of king moves (incl. castle), after opponent controlled
|
||||
* are known.
|
||||
* If @doit is true, add moves to @pos' moves list.
|
||||
*
|
||||
* @return: The number of possible king moves.
|
||||
*/
|
||||
int moves_gen_king_moves(pos_t *pos, bool color, bool doit)
|
||||
{
|
||||
int count = 0;
|
||||
piece_list_t *king = list_first_entry(&pos->pieces[color], piece_list_t, list);
|
||||
count = pseudo_moves_castle(pos, king, doit, true);
|
||||
count += pseudo_moves_gen(pos, king, doit, true);
|
||||
return count;
|
||||
}
|
||||
|
||||
static int moves_cmp_eval(__unused void *data, const struct list_head *h1, const struct list_head *h2)
|
||||
{
|
||||
move_t *m1 = list_entry(h1, move_t, list);
|
||||
move_t *m2 = list_entry(h2, move_t, list);
|
||||
return m2->eval_simple - m1->eval_simple;
|
||||
}
|
||||
|
||||
/**
|
||||
* moves_sort() sort - sort moves list, best eval first.
|
||||
* @pos: &position.
|
||||
*/
|
||||
void moves_sort(pos_t *pos)
|
||||
{
|
||||
list_sort(NULL, &pos->moves[pos->turn], moves_cmp_eval);
|
||||
}
|
||||
|
||||
/**
|
||||
* moves_gen_all_eval_sort() - calculate/generate/sort moves for side to play.
|
||||
* @pos: &position
|
||||
*
|
||||
* Generate positions for each move for player to move.
|
||||
* For each of them generate opponents moves, calculate eval, and sort the moves list.
|
||||
*/
|
||||
void moves_gen_eval_sort(pos_t *pos)
|
||||
{
|
||||
move_t *move;
|
||||
pos_t *newpos;
|
||||
|
||||
moves_gen_all(pos);
|
||||
|
||||
list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
newpos = move_do(pos, move);
|
||||
move->pos = newpos;
|
||||
//move_print(0, move, 0);
|
||||
move->eval_simple = eval_simple(newpos);
|
||||
newpos->eval_simple = move->eval_simple;
|
||||
}
|
||||
moves_sort(pos);
|
||||
//moves_print(pos, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* moves_gen_all() - calculate all moves, and generate moves for side to play.
|
||||
* @pos: &position
|
||||
*
|
||||
* Compute pseudo moves for both sides, and generate moves for player to move.
|
||||
*/
|
||||
void moves_gen_all(pos_t *pos)
|
||||
{
|
||||
//log_f(1, "turn=%d opponent=%d\n", pos->turn, OPPONENT(pos->turn));
|
||||
if (!pos->moves_generated) {
|
||||
if (!pos->moves_counted) {}
|
||||
moves_gen(pos, OPPONENT(pos->turn), false, false);
|
||||
moves_gen(pos, pos->turn, true, true);
|
||||
if (!pos->moves_counted)
|
||||
moves_gen_king_moves(pos, OPPONENT(pos->turn), false);
|
||||
pos->moves_counted = true;
|
||||
pos->moves_generated = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* moves_gen_all_nomoves() - calculate number of moves for each player.
|
||||
* @pos: &position
|
||||
*/
|
||||
void moves_gen_all_nomoves(pos_t *pos)
|
||||
{
|
||||
//log_f(1, "turn=%d opponent=%d\n", pos->turn, OPPONENT(pos->turn));
|
||||
if (!pos->moves_counted) {
|
||||
moves_gen(pos, OPPONENT(pos->turn), false, false);
|
||||
moves_gen(pos, pos->turn, false, true);
|
||||
moves_gen_king_moves(pos, OPPONENT(pos->turn), false);
|
||||
pos->moves_counted = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* move_do() - execute move in a duplicated position.
|
||||
* @pos: &pos_t struct on which move will be applied
|
||||
* @move: &move_t struct to apply
|
||||
*
|
||||
* @return: &new position
|
||||
*/
|
||||
pos_t *move_do(pos_t *pos, move_t *move)
|
||||
{
|
||||
# ifdef DEBUG_MOVE
|
||||
//log(1, "new move: ");
|
||||
//move_print(0, move, M_PR_NL | M_PR_LONG);
|
||||
# endif
|
||||
|
||||
pos_t *new = pos_dup(pos);
|
||||
piece_t piece = PIECE(move->piece), newpiece = piece, captured = move->capture;
|
||||
int color = COLOR(move->piece);
|
||||
square_t from = move->from, to = move->to;
|
||||
u64 bb_from = SQ88_2_BB(from), bb_to = SQ88_2_BB(to);
|
||||
|
||||
if (move->capture || piece == PAWN) /* 50 moves */
|
||||
new->clock_50 = 0;
|
||||
else
|
||||
new->clock_50++;
|
||||
|
||||
if (move->flags & M_CAPTURE) { /* capture */
|
||||
if (move->flags & M_EN_PASSANT) {
|
||||
uchar ep_file = F88(pos->en_passant);
|
||||
square_t ep_grab = color == WHITE ? SQ88(ep_file, 4): SQ88(ep_file, 3);
|
||||
u64 bb_ep_grab = SQ88_2_BB(ep_grab);
|
||||
|
||||
log_f(5, "en-passant=%d,%d\n", ep_file, color == WHITE ? 4 : 3);
|
||||
piece_del(&new->board[ep_grab].s_piece->list);
|
||||
new->board[ep_grab].piece = 0;
|
||||
new->occupied[OPPONENT(color)] &= ~bb_ep_grab;
|
||||
new->bb[OPPONENT(color)][BB_PAWN] &= ~bb_ep_grab;
|
||||
|
||||
} else {
|
||||
piece_del(&new->board[to].s_piece->list);
|
||||
new->board[to].piece = 0;
|
||||
new->occupied[OPPONENT(color)] &= ~bb_to;
|
||||
new->bb[OPPONENT(color)][PIECETOBB(captured)] &= ~bb_to;
|
||||
}
|
||||
|
||||
} else if (move->flags & M_CASTLE_Q) {
|
||||
uchar row = R88(from);
|
||||
square_t rook_from = SQ88(0, row);
|
||||
square_t rook_to = SQ88(3, row);
|
||||
u64 bb_rook_from = SQ88_2_BB(rook_from);
|
||||
u64 bb_rook_to = SQ88_2_BB(rook_to);
|
||||
|
||||
new->board[rook_to] = new->board[rook_from];
|
||||
new->board[rook_to].s_piece->square = rook_to;
|
||||
new->occupied[color] &= ~bb_rook_from;
|
||||
new->occupied[color] |= bb_rook_to;
|
||||
new->bb[color][PIECETOBB(BB_ROOK)] &= ~bb_rook_from;
|
||||
new->bb[color][PIECETOBB(BB_ROOK)] |= bb_rook_to;
|
||||
new->board[rook_from].piece = 0;
|
||||
new->board[rook_from].s_piece = NULL;
|
||||
//new->castle &= color == WHITE? ~CASTLE_W: ~CASTLE_B;
|
||||
|
||||
} else if (move->flags & M_CASTLE_K) {
|
||||
uchar row = R88(from);
|
||||
square_t rook_from = SQ88(7, row);
|
||||
square_t rook_to = SQ88(5, row);
|
||||
u64 bb_rook_from = SQ88_2_BB(rook_from);
|
||||
u64 bb_rook_to = SQ88_2_BB(rook_to);
|
||||
|
||||
new->board[rook_to] = new->board[rook_from];
|
||||
new->board[rook_to].s_piece->square = rook_to;
|
||||
new->occupied[color] &= ~bb_rook_from;
|
||||
new->occupied[color] |= bb_rook_to;
|
||||
new->bb[color][PIECETOBB(BB_ROOK)] &= ~bb_rook_from;
|
||||
new->bb[color][PIECETOBB(BB_ROOK)] |= bb_rook_to;
|
||||
new->board[rook_from].piece = 0;
|
||||
new->board[rook_from].s_piece = NULL;
|
||||
// new->castle &= color == WHITE? ~CASTLE_W: ~CASTLE_B;
|
||||
}
|
||||
|
||||
new->board[to] = new->board[from];
|
||||
/* fix dest square */
|
||||
new->board[to].s_piece->square = to;
|
||||
if (move->flags & M_PROMOTION) {
|
||||
log_f(5, "promotion to %s\n", P_SYM(move->promotion));
|
||||
log_f(5, "newpiece=%#x p=%#x\n", move->promotion, PIECE(move->promotion));
|
||||
newpiece = PIECE(move->promotion);
|
||||
new->board[to].piece = move->promotion;
|
||||
new->board[to].s_piece->piece = move->promotion;
|
||||
}
|
||||
/* replace old occupied bitboard by new one */
|
||||
new->occupied[color] &= ~bb_from;
|
||||
new->occupied[color] |= bb_to;
|
||||
new->bb[color][PIECETOBB(piece)] &= ~bb_from;
|
||||
new->bb[color][PIECETOBB(newpiece)] |= bb_to;
|
||||
if (move->flags & M_PROMOTION) {
|
||||
log_f(5, "promotion color=%d bbpiece=%d\n", color, PIECETOBB(newpiece));
|
||||
//bitboard_print(new->bb[color][PIECETOBB(newpiece)]);
|
||||
}
|
||||
/* set en_passant */
|
||||
new->en_passant = 0;
|
||||
if (piece == PAWN) {
|
||||
if (R88(from) == 1 && R88(to) == 3)
|
||||
pos->en_passant = SQ88(F88(from), 2);
|
||||
else if (R88(from) == 6 && R88(to) == 4)
|
||||
pos->en_passant = SQ88(F88(from), 5);
|
||||
}
|
||||
|
||||
/* always make "from" square empty */
|
||||
new->board[from].piece = 0;
|
||||
new->board[from].s_piece = NULL;
|
||||
|
||||
//printf("old turn=%d ", color);
|
||||
//printf("new turn=%d\n", new->turn);
|
||||
//fflush(stdout);
|
||||
/* adjust castling flags */
|
||||
if ((bb_from | bb_to) & E1bb)
|
||||
new->castle &= ~(CASTLE_WQ | CASTLE_WK);
|
||||
else if ((bb_from | bb_to) & A1bb)
|
||||
new->castle &= ~CASTLE_WQ;
|
||||
else if ((bb_from | bb_to) & H1bb)
|
||||
new->castle &= ~CASTLE_WK;
|
||||
|
||||
if ((bb_from | bb_to) & E8bb)
|
||||
new->castle &= ~(CASTLE_BQ | CASTLE_BK);
|
||||
else if ((bb_from | bb_to) & A8bb)
|
||||
new->castle &= ~CASTLE_BQ;
|
||||
else if ((bb_from | bb_to) & H8bb)
|
||||
new->castle &= ~CASTLE_BK;
|
||||
|
||||
SET_COLOR(new->turn, OPPONENT(color)); /* pos color */
|
||||
return new;
|
||||
}
|
||||
|
||||
void move_undo(pos_t *pos, __unused move_t *move)
|
||||
{
|
||||
pos_del(pos);
|
||||
}
|
||||
79
src/temp-migration-bitboard/move.h
Normal file
79
src/temp-migration-bitboard/move.h
Normal file
@@ -0,0 +1,79 @@
|
||||
/* move.h - move management.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef MOVE_H
|
||||
#define MOVE_H
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "position.h"
|
||||
#include "pool.h"
|
||||
#include "piece.h"
|
||||
|
||||
/* move flags
|
||||
*/
|
||||
typedef unsigned char move_flags_t;
|
||||
#define M_NORMAL 0x00
|
||||
#define M_CHECK 0x01 /* unsure if we know */
|
||||
#define M_CAPTURE 0x02
|
||||
#define M_EN_PASSANT 0x04
|
||||
#define M_PROMOTION 0x08
|
||||
#define M_CASTLE_K 0x10
|
||||
#define M_CASTLE_Q 0x20
|
||||
|
||||
/* moves_print flags
|
||||
*/
|
||||
#define M_PR_CAPT 0x01
|
||||
#define M_PR_NCAPT 0x02
|
||||
#define M_PR_NUM 0x04
|
||||
#define M_PR_NL 0x08
|
||||
#define M_PR_EVAL 0x20 /* separate captures */
|
||||
#define M_PR_SEPARATE 0x40 /* separate captures */
|
||||
#define M_PR_LONG 0x80
|
||||
|
||||
typedef struct move_s {
|
||||
piece_t piece;
|
||||
square_t from, to;
|
||||
piece_t capture; /* captured piece */
|
||||
piece_t promotion; /* promoted piece */
|
||||
move_flags_t flags;
|
||||
eval_t negamax;
|
||||
eval_t eval;
|
||||
eval_t eval_simple;
|
||||
pos_t *pos;
|
||||
struct list_head list; /* next move */
|
||||
} move_t;
|
||||
|
||||
pool_t *moves_pool_init();
|
||||
void moves_pool_stats();
|
||||
int move_print(int movenum, move_t *move, move_flags_t flags);
|
||||
void moves_print(pos_t *move, move_flags_t flags);
|
||||
|
||||
void move_del(struct list_head *ptr);
|
||||
int moves_del(pos_t *pos);
|
||||
|
||||
int pseudo_moves_castle(pos_t *pos, bool color, bool doit, bool do_king);
|
||||
int pseudo_moves_gen(pos_t *pos, piece_list_t *piece, bool doit, bool do_king);
|
||||
int pseudo_moves_pawn(pos_t *pos, piece_list_t *piece, bool doit);
|
||||
int moves_gen(pos_t *pos, bool color, bool doit, bool do_king);
|
||||
int moves_gen_king_moves(pos_t *pos, bool color, bool doit);
|
||||
|
||||
void moves_sort(pos_t *pos);
|
||||
void moves_gen_eval_sort(pos_t *pos);
|
||||
|
||||
void moves_gen_all(pos_t *pos);
|
||||
void moves_gen_all_nomoves(pos_t *pos);
|
||||
|
||||
pos_t *move_do(pos_t *pos, move_t *move);
|
||||
void move_undo(pos_t *pos, move_t *move);
|
||||
|
||||
#endif /* MOVE_H */
|
||||
118
src/temp-migration-bitboard/piece.c
Normal file
118
src/temp-migration-bitboard/piece.c
Normal file
@@ -0,0 +1,118 @@
|
||||
/* piece.c - piece list management.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <malloc.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <debug.h>
|
||||
#include <pool.h>
|
||||
#include <list.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "piece.h"
|
||||
#include "board.h"
|
||||
#include "bitboard.h"
|
||||
#include "position.h"
|
||||
|
||||
static pool_t *pieces_pool;
|
||||
|
||||
struct piece_details piece_details[] = {
|
||||
[E_EMPTY] = { ' ', ' ', " ", " ", "", 0 },
|
||||
[E_PAWN] = { 'P', 'p', "♙", "♟", "Pawn", PAWN_VALUE },
|
||||
[E_KNIGHT] = { 'N', 'n', "♘", "♞", "Knight", KNIGHT_VALUE },
|
||||
[E_BISHOP] = { 'B', 'b', "♗", "♝", "Bishop", BISHOP_VALUE },
|
||||
[E_ROOK] = { 'R', 'r', "♖", "♜", "Rook", ROOK_VALUE },
|
||||
[E_QUEEN] = { 'Q', 'q', "♕", "♛", "Queen", QUEEN_VALUE },
|
||||
[E_KING] = { 'K', 'k', "♔", "♚", "King", KING_VALUE }
|
||||
};
|
||||
|
||||
void piece_list_print(struct list_head *list)
|
||||
{
|
||||
struct list_head *p_cur, *tmp;
|
||||
piece_list_t *piece;
|
||||
|
||||
list_for_each_safe(p_cur, tmp, list) {
|
||||
piece = list_entry(p_cur, piece_list_t, list);
|
||||
|
||||
printf("%s%c%c ", P_SYM(piece->piece),
|
||||
FILE2C(F88(piece->square)),
|
||||
RANK2C(R88(piece->square)));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
pool_t *piece_pool_init()
|
||||
{
|
||||
if (!pieces_pool)
|
||||
pieces_pool = pool_create("pieces", 128, sizeof(piece_list_t));
|
||||
return pieces_pool;
|
||||
}
|
||||
|
||||
void piece_pool_stats()
|
||||
{
|
||||
if (pieces_pool)
|
||||
pool_stats(pieces_pool);
|
||||
}
|
||||
|
||||
piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square)
|
||||
{
|
||||
piece_list_t *new;
|
||||
short color = COLOR(piece);
|
||||
|
||||
# ifdef DEBUG_PIECE
|
||||
log_f(3, "piece=%02x square=%02x\n", piece, square);
|
||||
log_f(5, "Adding %s %s on %c%c\n", color? "Black": "White",
|
||||
P_NAME(piece), FILE2C(F88(square)), RANK2C(R88(square)));
|
||||
# endif
|
||||
if ((new = pool_get(pieces_pool))) {
|
||||
/* first piece is always king */
|
||||
if (PIECE(piece) == KING)
|
||||
list_add(&new->list, &pos->pieces[color]);
|
||||
else
|
||||
list_add_tail(&new->list, &pos->pieces[color]);
|
||||
new->piece = piece;
|
||||
new->square = square;
|
||||
new->castle = 0;
|
||||
new-> value = piece_details[PIECE(piece)].value;
|
||||
}
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
void piece_del(struct list_head *ptr)
|
||||
{
|
||||
piece_list_t *piece = list_entry(ptr, piece_list_t, list);
|
||||
# ifdef DEBUG_PIECE
|
||||
log_f(3, "piece=%02x square=%02x\n", piece->piece, piece->square);
|
||||
# endif
|
||||
list_del(ptr);
|
||||
pool_add(pieces_pool, piece);
|
||||
return;
|
||||
}
|
||||
|
||||
int pieces_del(pos_t *pos, short color)
|
||||
{
|
||||
struct list_head *p_cur, *tmp, *head;
|
||||
int count = 0;
|
||||
|
||||
head = &pos->pieces[color];
|
||||
|
||||
list_for_each_safe(p_cur, tmp, head) {
|
||||
piece_del(p_cur);
|
||||
count++;
|
||||
}
|
||||
# ifdef DEBUG_PIECE
|
||||
log_f(3, "color=%d removed=%d\n", color, count);
|
||||
# endif
|
||||
return count;
|
||||
}
|
||||
71
src/temp-migration-bitboard/piece.h
Normal file
71
src/temp-migration-bitboard/piece.h
Normal file
@@ -0,0 +1,71 @@
|
||||
/* piece.h - piece definitions.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef PIECE_H
|
||||
#define PIECE_H
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "list.h"
|
||||
#include "pool.h"
|
||||
|
||||
#define PIECE_DEFAULT_VALUE 0
|
||||
|
||||
/* initial default values */
|
||||
#define PAWN_VALUE 100
|
||||
#define KNIGHT_VALUE 300
|
||||
#define BISHOP_VALUE 300
|
||||
#define ROOK_VALUE 500
|
||||
#define QUEEN_VALUE 900
|
||||
#define KING_VALUE 20000
|
||||
|
||||
typedef struct piece_list_s {
|
||||
piece_t piece;
|
||||
square_t square;
|
||||
short castle;
|
||||
s64 value;
|
||||
struct list_head list;
|
||||
} piece_list_t;
|
||||
|
||||
/* some default values for pieces
|
||||
*/
|
||||
extern struct piece_details {
|
||||
char abbrev_w; /* used for game notation */
|
||||
char abbrev_b;
|
||||
char *symbol_w;
|
||||
char *symbol_b; /* used for game notation */
|
||||
char *name;
|
||||
s64 value;
|
||||
} piece_details[];
|
||||
|
||||
#define P_NAME(p) piece_details[E_PIECE(p)].name
|
||||
#define P_LETTER(p) piece_details[E_PIECE(p)].abbrev_w
|
||||
#define P_SYM(p) piece_details[E_PIECE(p)].symbol_b
|
||||
#define P_CSHORT(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].abbrev_w: \
|
||||
piece_details[E_PIECE(p)].abbrev_b)
|
||||
#define P_CSYM(p) (IS_WHITE(p)? piece_details[E_PIECE(p)].symbol_w: \
|
||||
piece_details[E_PIECE(p)].symbol_b)
|
||||
#define P_VALUE(p) (piece_details[E_PIECE(p)].value)
|
||||
/* use short name or symbol - no effect
|
||||
*/
|
||||
#define P_USE_UTF 1
|
||||
|
||||
void piece_list_print(struct list_head *list);
|
||||
pool_t *piece_pool_init();
|
||||
void piece_pool_stats();
|
||||
piece_list_t *piece_add(pos_t *pos, piece_t piece, square_t square);
|
||||
void piece_del(struct list_head *ptr);
|
||||
int pieces_del(pos_t *pos, short color);
|
||||
|
||||
#endif
|
||||
323
src/temp-migration-bitboard/position.c
Normal file
323
src/temp-migration-bitboard/position.c
Normal file
@@ -0,0 +1,323 @@
|
||||
/* position.c - position management.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "chessdefs.h"
|
||||
#include "position.h"
|
||||
#include "move.h"
|
||||
#include "fen.h"
|
||||
#include "piece.h"
|
||||
#include "eval.h"
|
||||
|
||||
static pool_t *pos_pool;
|
||||
|
||||
#define BYTE_PRINT "%c%c%c%c%c%c%c%c"
|
||||
#define BYTE2BIN(b) ((b) & 0x01 ? '1' : '0'), \
|
||||
((b) & 0x02 ? '1' : '0'), \
|
||||
((b) & 0x04 ? '1' : '0'), \
|
||||
((b) & 0x08 ? '1' : '0'), \
|
||||
((b) & 0x10 ? '1' : '0'), \
|
||||
((b) & 0x20 ? '1' : '0'), \
|
||||
((b) & 0x40 ? '1' : '0'), \
|
||||
((b) & 0x80 ? '1' : '0')
|
||||
|
||||
inline void bitboard_print(bitboard_t bb)
|
||||
{
|
||||
int i;
|
||||
printf("%#018lx\n", bb);
|
||||
for (i=56; i>=0; i-=8)
|
||||
printf("\t"BYTE_PRINT"\n",
|
||||
BYTE2BIN(bb>>i));
|
||||
}
|
||||
|
||||
inline void bitboard_print2(bitboard_t bb1, bitboard_t bb2)
|
||||
{
|
||||
int i;
|
||||
printf("\tW: %#018lx\tB: %#018lx\n", bb1, bb2);
|
||||
for (i=56; i>=0; i-=8)
|
||||
printf("\t"BYTE_PRINT"\t\t"BYTE_PRINT"\n",
|
||||
BYTE2BIN(bb1>>i),
|
||||
BYTE2BIN(bb2>>i));
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_pieces_print() - Print position pieces
|
||||
* @pos: &position
|
||||
*/
|
||||
void pos_pieces_print(pos_t *pos)
|
||||
{
|
||||
printf("White pieces (%d): \t", popcount64(pos->occupied[WHITE]));
|
||||
piece_list_print(&pos->pieces[WHITE]);
|
||||
printf("Black pieces (%d): \t", popcount64(pos->occupied[BLACK]));
|
||||
piece_list_print(&pos->pieces[BLACK]);
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_bitboards_print() - Print position bitboards
|
||||
* @pos: &position
|
||||
*/
|
||||
void pos_bitboards_print(pos_t *pos)
|
||||
{
|
||||
printf("Bitboards occupied :\n");
|
||||
bitboard_print2(pos->occupied[WHITE], pos->occupied[BLACK]);
|
||||
printf("Bitboards controlled :\n");
|
||||
bitboard_print2(pos->controlled[WHITE], pos->controlled[BLACK]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_print() - Print position on stdout.
|
||||
* @pos: &position
|
||||
*/
|
||||
void pos_print(pos_t *pos)
|
||||
{
|
||||
int rank, file;
|
||||
piece_t piece;
|
||||
board_t *board = pos->board;
|
||||
piece_list_t *wk = list_first_entry(&pos->pieces[WHITE], piece_list_t, list),
|
||||
*bk = list_first_entry(&pos->pieces[BLACK], piece_list_t, list);
|
||||
|
||||
|
||||
printf(" +---+---+---+---+---+---+---+---+\n");
|
||||
for (rank = 7; rank >= 0; --rank) {
|
||||
printf("%c |", rank + '1');
|
||||
for (file = 0; file < 8; ++file) {
|
||||
piece = board[SQ88(file, rank)].piece;
|
||||
printf(" %s |", P_CSYM(piece));
|
||||
}
|
||||
printf("\n +---+---+---+---+---+---+---+---+\n");
|
||||
}
|
||||
printf(" A B C D E F G H\n\n");
|
||||
printf("Turn: %s.\n", IS_WHITE(pos->turn) ? "white" : "black");
|
||||
printf("Kings: W:%c%c B:%c%c\n",
|
||||
FILE2C(F88(wk->square)),
|
||||
RANK2C(R88(wk->square)),
|
||||
FILE2C(F88(bk->square)),
|
||||
RANK2C(R88(bk->square)));
|
||||
printf("Possible en-passant: [%#x] ", pos->en_passant);
|
||||
if (pos->en_passant == 0)
|
||||
printf("None.\n");
|
||||
else
|
||||
printf("%d %d = %c%c\n",
|
||||
F88(pos->en_passant),
|
||||
R88(pos->en_passant),
|
||||
FILE2C(F88(pos->en_passant)),
|
||||
RANK2C(R88(pos->en_passant)));
|
||||
|
||||
printf("castle [%#x] : ", pos->castle);
|
||||
|
||||
if (pos->castle & CASTLE_WK)
|
||||
printf("K");
|
||||
if (pos->castle & CASTLE_WQ)
|
||||
printf("Q");
|
||||
if (pos->castle & CASTLE_BK)
|
||||
printf("k");
|
||||
if (pos->castle & CASTLE_BQ)
|
||||
printf("q");
|
||||
|
||||
printf("\n50 half-moves-rule = %d\n", pos->clock_50);
|
||||
printf("Current move = %d\n", pos->curmove);
|
||||
printf("Squares controlled: W:%d B:%d\n", popcount64(pos->controlled[WHITE]),
|
||||
popcount64(pos->controlled[BLACK]));
|
||||
printf("Mobility: W:%u B:%u\n", pos->mobility[WHITE],
|
||||
pos->mobility[BLACK]);
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_check() - extensive position consistenci check.
|
||||
* @pos: &position
|
||||
*/
|
||||
void pos_check(pos_t *pos)
|
||||
{
|
||||
int rank, file;
|
||||
piece_t piece;
|
||||
board_t *board = pos->board;
|
||||
|
||||
/* check that board and bitboard reflect same information */
|
||||
for (rank = 7; rank >= 0; --rank) {
|
||||
for (file = 0; file < 8; ++file) {
|
||||
piece_list_t *ppiece;
|
||||
printf("checking %c%c ", file+'a', rank+'1');
|
||||
|
||||
piece = board[SQ88(file, rank)].piece;
|
||||
ppiece= board[SQ88(file, rank)].s_piece;
|
||||
printf("piece=%s ", P_CSYM(piece));
|
||||
if (ppiece)
|
||||
printf("ppiece=%s/sq=%#x ", P_CSYM(ppiece->piece), ppiece->square);
|
||||
switch(PIECE(piece)) {
|
||||
case PAWN:
|
||||
printf("pawn" );
|
||||
break;
|
||||
case KNIGHT:
|
||||
printf("knight ");
|
||||
break;
|
||||
case BISHOP:
|
||||
printf("bishop ");
|
||||
break;
|
||||
case ROOK:
|
||||
printf("rook ");
|
||||
break;
|
||||
case QUEEN:
|
||||
printf("queen ");
|
||||
break;
|
||||
case KING:
|
||||
printf("king ");
|
||||
break;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pos_t *pos_clear(pos_t *pos)
|
||||
{
|
||||
int file, rank;
|
||||
board_t *board = pos->board;
|
||||
|
||||
for (file = 0; file < 8; ++file) {
|
||||
for (rank = 0; rank < 8; ++rank) {
|
||||
/*printf("file = %d rank = %d SQ88 = %#2x = %d addr=%p\n", file, rank,
|
||||
SQ88(file, rank), SQ88(file, rank),
|
||||
&board[SQ88(file, rank)].piece);
|
||||
*/
|
||||
board[SQ88(file, rank)].piece = EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
SET_WHITE(pos->turn);
|
||||
pos->node_count = 0;
|
||||
pos->castle = 0;
|
||||
pos->clock_50 = 0;
|
||||
pos->curmove = 0;
|
||||
pos->eval = 0;
|
||||
pos->en_passant = 0;
|
||||
pos->occupied[WHITE] = 0;
|
||||
pos->occupied[BLACK] = 0;
|
||||
for (int color=0; color<2; ++color)
|
||||
for (int piece = BB_ALL; piece < BB_END; ++piece)
|
||||
pos->bb[color][piece] = 0;
|
||||
pos->controlled[WHITE] = 0;
|
||||
pos->controlled[BLACK] = 0;
|
||||
pos->mobility[WHITE] = 0;
|
||||
pos->mobility[BLACK] = 0;
|
||||
pos->moves_generated = false;
|
||||
pos->moves_counted = false;
|
||||
/* remove pieces / moves */
|
||||
pieces_del(pos, WHITE);
|
||||
pieces_del(pos, BLACK);
|
||||
moves_del(pos);
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_del() - delete a position.
|
||||
* @pos: &position.
|
||||
*/
|
||||
void pos_del(pos_t *pos)
|
||||
{
|
||||
pieces_del(pos, WHITE);
|
||||
pieces_del(pos, BLACK);
|
||||
moves_del(pos);
|
||||
pool_add(pos_pool, pos);
|
||||
}
|
||||
|
||||
pos_t *pos_startpos(pos_t *pos)
|
||||
{
|
||||
static char *startfen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
|
||||
return fen2pos(pos, startfen);
|
||||
}
|
||||
|
||||
pos_t *pos_get()
|
||||
{
|
||||
pos_t *pos = pool_get(pos_pool);
|
||||
if (pos) {
|
||||
INIT_LIST_HEAD(&pos->pieces[WHITE]);
|
||||
INIT_LIST_HEAD(&pos->pieces[BLACK]);
|
||||
|
||||
INIT_LIST_HEAD(&pos->moves[WHITE]);
|
||||
INIT_LIST_HEAD(&pos->moves[BLACK]);
|
||||
pos_clear(pos);
|
||||
} else {
|
||||
fprintf(stderr, "zobaaa\n");
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* pos_dup() - duplicate a position.
|
||||
* @pos: &position to duplicate.
|
||||
*
|
||||
* New position is the same as source one (with duplicated pieces list),
|
||||
* except:
|
||||
* - moves list is empty
|
||||
* - bestmove is NULL
|
||||
* - nodecount is set to zero
|
||||
* - eval is set to EVAL_INVALID
|
||||
* - moves_generated ans moves_counted are unset
|
||||
* - check is set to zero
|
||||
*
|
||||
* @return: The new position.
|
||||
*
|
||||
* TODO: merge with pos_get - NULL for init, non null for duplicate
|
||||
*/
|
||||
pos_t *pos_dup(pos_t *pos)
|
||||
{
|
||||
struct list_head *p_cur, *piece_list;
|
||||
piece_list_t *oldpiece;
|
||||
board_t *board;
|
||||
pos_t *new = pool_get(pos_pool);
|
||||
|
||||
if (new) {
|
||||
board = new->board;
|
||||
*new = *pos;
|
||||
for (int color = 0; color < 2; ++color) {
|
||||
INIT_LIST_HEAD(&new->pieces[color]);
|
||||
INIT_LIST_HEAD(&new->moves[color]);
|
||||
/* duplicate piece list */
|
||||
piece_list = &pos->pieces[color]; /* white/black piece list */
|
||||
|
||||
list_for_each(p_cur, piece_list) {
|
||||
oldpiece = list_entry(p_cur, piece_list_t, list);
|
||||
board[oldpiece->square].s_piece =
|
||||
piece_add(new, oldpiece->piece, oldpiece->square);
|
||||
}
|
||||
}
|
||||
new->bestmove = NULL;
|
||||
new->node_count = 0;
|
||||
new->eval = EVAL_INVALID;
|
||||
new->moves_generated = false;
|
||||
new->moves_counted = false;
|
||||
new->check[WHITE] = new->check[BLACK] = 0;
|
||||
}
|
||||
return new;
|
||||
}
|
||||
|
||||
pool_t *pos_pool_init()
|
||||
{
|
||||
if (!pos_pool)
|
||||
pos_pool = pool_create("positions", 128, sizeof(pos_t));
|
||||
return pos_pool;
|
||||
}
|
||||
|
||||
void pos_pool_stats()
|
||||
{
|
||||
if (pos_pool)
|
||||
pool_stats(pos_pool);
|
||||
}
|
||||
66
src/temp-migration-bitboard/position.h
Normal file
66
src/temp-migration-bitboard/position.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/* position.h - position management definitions.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef POSITION_H
|
||||
#define POSITION_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "pool.h"
|
||||
#include "list.h"
|
||||
#include "bitops.h"
|
||||
|
||||
#include "board.h"
|
||||
#include "chessdefs.h"
|
||||
|
||||
typedef struct pos_s {
|
||||
u64 node_count; /* evaluated nodes */
|
||||
piece_t turn; /* we use only color bit */
|
||||
castle_t castle;
|
||||
u16 clock_50;
|
||||
u16 curmove;
|
||||
eval_t eval;
|
||||
int check[2];
|
||||
int eval_simple_phase;
|
||||
eval_t eval_simple;
|
||||
move_t *bestmove;
|
||||
bool moves_generated;
|
||||
bool moves_counted;
|
||||
board_t board[BOARDSIZE];
|
||||
|
||||
square_t en_passant;
|
||||
|
||||
bitboard_t bb[2][BB_END]; /* use: pieces[BLACK][BB_PAWN] */
|
||||
bitboard_t occupied[2]; /* OR of bb[COLOR][x] */
|
||||
bitboard_t controlled[2];
|
||||
u16 mobility[2];
|
||||
struct list_head pieces[2]; /* pieces list, King is first */
|
||||
struct list_head moves[2];
|
||||
} pos_t;
|
||||
|
||||
void bitboard_print(bitboard_t bb);
|
||||
void bitboard_print2(bitboard_t bb1, bitboard_t bb2);
|
||||
void pos_pieces_print(pos_t *pos);
|
||||
void pos_bitboards_print(pos_t *pos);
|
||||
void pos_print(pos_t *pos);
|
||||
pos_t *pos_clear(pos_t *pos);
|
||||
void pos_del(pos_t *pos);
|
||||
pos_t *pos_startpos(pos_t *pos);
|
||||
pos_t *pos_create();
|
||||
pool_t *pos_pool_init();
|
||||
void pos_pool_stats();
|
||||
pos_t *pos_get();
|
||||
pos_t *pos_dup(pos_t *pos);
|
||||
void pos_check(pos_t *pos);
|
||||
|
||||
#endif /* POSITION_H */
|
||||
237
src/temp-migration-bitboard/search.c
Normal file
237
src/temp-migration-bitboard/search.c
Normal file
@@ -0,0 +1,237 @@
|
||||
/* search.c - search good moves.
|
||||
*
|
||||
* Copyright (C) 2023 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include <br.h>
|
||||
#include <list.h>
|
||||
#include "debug.h"
|
||||
|
||||
#include "move.h"
|
||||
#include "eval.h"
|
||||
#include "search.h"
|
||||
|
||||
/**
|
||||
* negamax() - search position negamax.
|
||||
* @pos: &position to search
|
||||
* @depth: Wanted depth.
|
||||
* @color: 1 for white, -1 for black.
|
||||
*
|
||||
* Calculate the negamax value of @pos. This is an extensive search, with
|
||||
* absolutely no cutoff.
|
||||
*
|
||||
* @return: The @pos negamax evaluation.
|
||||
*/
|
||||
eval_t negamax(pos_t *pos, int depth, int color)
|
||||
{
|
||||
move_t *move;
|
||||
pos_t *newpos;
|
||||
eval_t best = EVAL_MIN, score;
|
||||
|
||||
pos->node_count++;
|
||||
if (depth == 0) {
|
||||
moves_gen_all_nomoves(pos);
|
||||
score = eval(pos) * color;
|
||||
return score;
|
||||
}
|
||||
moves_gen_all(pos);
|
||||
list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
newpos = move_do(pos, move);
|
||||
score = -negamax(newpos, depth - 1, -color);
|
||||
pos->node_count += newpos->node_count;
|
||||
move->negamax = score;
|
||||
if (score > best) {
|
||||
best = score;
|
||||
pos->bestmove = move;
|
||||
}
|
||||
move_undo(newpos, move);
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* pvs() - Principal Variation Search.
|
||||
* @pos: &position to search
|
||||
* @depth: wanted depth.
|
||||
* @alpha: alpha value.
|
||||
* @beta: beta value.
|
||||
* @color: 1 for white, -1 for black.
|
||||
*
|
||||
* Calculate the PVS value of @pos.
|
||||
* See https://en.wikipedia.org/wiki/Principal_variation_search
|
||||
*
|
||||
* Moves list should be first generated and evaluated/sorted.
|
||||
*
|
||||
* @return: The @pos PVS evaluation.
|
||||
*/
|
||||
eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color)
|
||||
{
|
||||
move_t *move;
|
||||
pos_t *newpos;
|
||||
eval_t score = EVAL_INVALID;
|
||||
bool firstchild = true;
|
||||
|
||||
pos->node_count++;
|
||||
|
||||
if (depth == 0) {
|
||||
//return quiesce(p, alpha, beta); /* leaf node */
|
||||
moves_gen_all_nomoves(pos);
|
||||
score = eval(pos) * color;
|
||||
log_f(2, "Terminal: depth=%d ", depth);
|
||||
log_f(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);
|
||||
return score;
|
||||
}
|
||||
|
||||
moves_gen_all(pos);
|
||||
//moves_print(pos, M_PR_EVAL);
|
||||
/* do the full search for first child */
|
||||
//move = list_first_entry_or_null(&pos->moves[pos->turn], move_t, list);
|
||||
|
||||
list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
newpos = move_do(pos, move);
|
||||
log(2, "%.*s", 5 - depth, " ");
|
||||
if (firstchild) { /* first child */
|
||||
score = -pvs(newpos, depth - 1, -beta, -alpha, -color);
|
||||
log_f(2, "First child depth=%d move=", depth);
|
||||
//move_print(0, move, 0);
|
||||
log(2, "score=%d alpha=%d beta=%d\n", score, alpha, beta);
|
||||
pos->bestmove = move;
|
||||
} else {
|
||||
/* search with a null window */
|
||||
score = -pvs(newpos, depth - 1, -alpha - 1, -alpha, -color);
|
||||
log_f(2, "Other child depth=%d move=", depth);
|
||||
//move_print(0, move, 0);
|
||||
log_f(2, "score=%d alpha=%d beta=%d ", score, alpha, beta);
|
||||
/* for fail-soft: if (score > alpha && score < beta) */
|
||||
if (score > alpha) {
|
||||
/* if failed high, do a full re-search */
|
||||
log_f(2, "doing full search.");
|
||||
score = -pvs(newpos, depth - 1, -beta, -alpha, -color);
|
||||
}
|
||||
log(2, "\n");
|
||||
}
|
||||
pos->node_count += newpos->node_count;
|
||||
move_undo(newpos, move);
|
||||
if (score >= beta) { /* fail-hard hard beta cut-off */
|
||||
log(2, "%.*s", 5 - depth, " ");
|
||||
log_f(2, "depth=%d score=%d alpha=%d beta=%d beta cut-off.\n",
|
||||
depth, score, alpha, beta);
|
||||
return beta;
|
||||
}
|
||||
if (score > alpha) {
|
||||
log(2, "%.*s", 5 - depth, " ");
|
||||
log_f(2, "depth=%d setting new alpha from %d to %d\n",
|
||||
depth, alpha, score);
|
||||
alpha = score;
|
||||
pos->bestmove = move;
|
||||
}
|
||||
move->pos = NULL;
|
||||
move->negamax = score;
|
||||
firstchild = false;
|
||||
}
|
||||
|
||||
return alpha;
|
||||
}
|
||||
|
||||
/*
|
||||
* int negascout (pos_t *pos, int depth, int alpha, int beta )
|
||||
* { /\* compute minimax value of position p *\/
|
||||
* move_t *move;
|
||||
* pos_t *newpos;
|
||||
* eval_t best = EVAL_MIN, score;
|
||||
*
|
||||
* int a, b, t, i;
|
||||
*
|
||||
* if (depth == 0) {
|
||||
* //return quiesce(p, alpha, beta); /\* leaf node *\/
|
||||
* moves_gen_all_nomoves(pos);
|
||||
* score = eval(pos) * color;
|
||||
* return score;
|
||||
* }
|
||||
* moves_gen_all(pos);
|
||||
* a = alpha;
|
||||
* b = beta;
|
||||
* list_for_each_entry(move, &pos->moves[pos->turn], list) {
|
||||
* log(1, "%.*s", 5 - depth, " ");
|
||||
* newpos = move_do(pos, move);
|
||||
* // for ( i = 1; i <= w; i++ ) {
|
||||
* t = -negascout (newpos, depth - 1, -b, -alpha);
|
||||
* if ( (t > a) && (t < beta) && (i > 1) )
|
||||
* t = -NegaScout ( p_i, -beta, -alpha ); /\* re-search *\/
|
||||
* alpha = max( alpha, t );
|
||||
* if ( alpha >= beta )
|
||||
* return alpha; /\* cut-off *\/
|
||||
* b = alpha + 1; /\* set new null window *\/
|
||||
* }
|
||||
* return alpha;
|
||||
* }
|
||||
*/
|
||||
|
||||
/*
|
||||
* int quiesce(pos_t *pos, int alpha, int beta)
|
||||
* {
|
||||
* int stand_pat = eval(pos);
|
||||
*
|
||||
* if( stand_pat >= beta )
|
||||
* return beta;
|
||||
* if( alpha < stand_pat )
|
||||
* alpha = stand_pat;
|
||||
*
|
||||
* /\*
|
||||
* * until( every_capture_has_been_examined ) {
|
||||
* * MakeCapture();
|
||||
* * score = -Quiesce( -beta, -alpha );
|
||||
* * TakeBackMove();
|
||||
* *
|
||||
* * if( score >= beta )
|
||||
* * return beta;
|
||||
* * if( score > alpha )
|
||||
* * alpha = score;
|
||||
* * }
|
||||
* *\/
|
||||
* return alpha;
|
||||
* }
|
||||
*/
|
||||
|
||||
/**
|
||||
* ab_negamax() - search position negamax with alpha-beta cutoff.
|
||||
* @pos: &position to search
|
||||
* @depth: Wanted depth.
|
||||
* @color: 1 for white, -1 for black.
|
||||
*
|
||||
* Calculate the negamax value of @pos, with alpha-beta pruning.
|
||||
*
|
||||
* @return: The @pos negamax evaluation.
|
||||
*/
|
||||
/*int ab_negamax(pos_t *pos, int alpha, int beta, int depth)
|
||||
{
|
||||
move_t *move;
|
||||
pos_t *newpos;
|
||||
eval_t best = EVAL_MIN, score;
|
||||
|
||||
if(depth == 0) {
|
||||
//return quiesce( alpha, beta );
|
||||
moves_gen_all_nomoves(pos);
|
||||
score = eval(pos) * color;
|
||||
return score;
|
||||
}
|
||||
for ( all moves) {
|
||||
score = -alphaBeta( -beta, -alpha, depthleft - 1 );
|
||||
if( score >= beta )
|
||||
return beta; // fail hard beta-cutoff
|
||||
if( score > alpha )
|
||||
alpha = score; // alpha acts like max in MiniMax
|
||||
}
|
||||
return alpha;
|
||||
}
|
||||
*/
|
||||
22
src/temp-migration-bitboard/search.h
Normal file
22
src/temp-migration-bitboard/search.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/* search.h - search for perfect move.
|
||||
*
|
||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
||||
* Licensed under the GNU General Public License v3.0 or later.
|
||||
* Some rights reserved. See COPYING.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this
|
||||
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SEARCH_H
|
||||
#define SEARCH_H
|
||||
|
||||
#include "position.h"
|
||||
|
||||
eval_t negamax(pos_t *pos, int depth, int color);
|
||||
eval_t pvs(pos_t *pos, int depth, int alpha, int beta, int color);
|
||||
|
||||
#endif /* SEARCH_H */
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user