.
diff --git a/src/Stockfish/README.md b/src/Stockfish/README.md
new file mode 100644
index 0000000..1f462d3
--- /dev/null
+++ b/src/Stockfish/README.md
@@ -0,0 +1,164 @@
+
+
+ [![Stockfish][stockfish128-logo]][website-link]
+
+
Stockfish
+
+ A free and strong UCI chess engine.
+
+ [Explore Stockfish docs »][wiki-link]
+
+
+ [Report bug][issue-link]
+ ·
+ [Open a discussion][discussions-link]
+ ·
+ [Discord][discord-link]
+ ·
+ [Blog][website-blog-link]
+
+ [![Build][build-badge]][build-link]
+ [![License][license-badge]][license-link]
+
+ [![Release][release-badge]][release-link]
+ [![Commits][commits-badge]][commits-link]
+
+ [![Website][website-badge]][website-link]
+ [![Fishtest][fishtest-badge]][fishtest-link]
+ [![Discord][discord-badge]][discord-link]
+
+
+
+## Overview
+
+[Stockfish][website-link] is a **free and strong UCI chess engine** derived from
+Glaurung 2.1 that analyzes chess positions and computes the optimal moves.
+
+Stockfish **does not include a graphical user interface** (GUI) that is required
+to display a chessboard and to make it easy to input moves. These GUIs are
+developed independently from Stockfish and are available online. **Read the
+documentation for your GUI** of choice for information about how to use
+Stockfish with it.
+
+See also the Stockfish [documentation][wiki-usage-link] for further usage help.
+
+## Files
+
+This distribution of Stockfish consists of the following files:
+
+ * [README.md][readme-link], the file you are currently reading.
+
+ * [Copying.txt][license-link], a text file containing the GNU General Public
+ License version 3.
+
+ * [AUTHORS][authors-link], a text file with the list of authors for the project.
+
+ * [src][src-link], a subdirectory containing the full source code, including a
+ Makefile that can be used to compile Stockfish on Unix-like systems.
+
+ * a file with the .nnue extension, storing the neural network for the NNUE
+ evaluation. Binary distributions will have this file embedded.
+
+## The UCI protocol
+
+The [Universal Chess Interface][uci-link] (UCI) is a standard text-based protocol
+used to communicate with a chess engine and is the recommended way to do so for
+typical graphical user interfaces (GUI) or chess tools. Stockfish implements the
+majority of its options.
+
+Developers can see the default values for the UCI options available in Stockfish
+by typing `./stockfish uci` in a terminal, but most users should typically use a
+chess GUI to interact with Stockfish.
+
+For more information on UCI or debug commands, see our [documentation][wiki-commands-link].
+
+## Compiling Stockfish
+
+Stockfish has support for 32 or 64-bit CPUs, certain hardware instructions,
+big-endian machines such as Power PC, and other platforms.
+
+On Unix-like systems, it should be easy to compile Stockfish directly from the
+source code with the included Makefile in the folder `src`. In general, it is
+recommended to run `make help` to see a list of make targets with corresponding
+descriptions.
+
+```
+cd src
+make -j build ARCH=x86-64-modern
+```
+
+Detailed compilation instructions for all platforms can be found in our
+[documentation][wiki-compile-link].
+
+## Contributing
+
+### Donating hardware
+
+Improving Stockfish requires a massive amount of testing. You can donate your
+hardware resources by installing the [Fishtest Worker][worker-link] and viewing
+the current tests on [Fishtest][fishtest-link].
+
+### Improving the code
+
+In the [chessprogramming wiki][programming-link], many techniques used in
+Stockfish are explained with a lot of background information.
+The [section on Stockfish][programmingsf-link] describes many features
+and techniques used by Stockfish. However, it is generic rather than
+focused on Stockfish's precise implementation.
+
+The engine testing is done on [Fishtest][fishtest-link].
+If you want to help improve Stockfish, please read this [guideline][guideline-link]
+first, where the basics of Stockfish development are explained.
+
+Discussions about Stockfish take place these days mainly in the Stockfish
+[Discord server][discord-link]. This is also the best place to ask questions
+about the codebase and how to improve it.
+
+## Terms of use
+
+Stockfish is free and distributed under the
+[**GNU General Public License version 3**][license-link] (GPL v3). Essentially,
+this means you are free to do almost exactly what you want with the program,
+including distributing it among your friends, making it available for download
+from your website, selling it (either by itself or as part of some bigger
+software package), or using it as the starting point for a software project of
+your own.
+
+The only real limitation is that whenever you distribute Stockfish in some way,
+you MUST always include the license and the full source code (or a pointer to
+where the source code can be found) to generate the exact binary you are
+distributing. If you make any changes to the source code, these changes must
+also be made available under GPL v3.
+
+
+[authors-link]: https://github.com/official-stockfish/Stockfish/blob/master/AUTHORS
+[build-link]: https://github.com/official-stockfish/Stockfish/actions/workflows/stockfish.yml
+[commits-link]: https://github.com/official-stockfish/Stockfish/commits/master
+[discord-link]: https://discord.gg/GWDRS3kU6R
+[issue-link]: https://github.com/official-stockfish/Stockfish/issues/new?assignees=&labels=&template=BUG-REPORT.yml
+[discussions-link]: https://github.com/official-stockfish/Stockfish/discussions/new
+[fishtest-link]: https://tests.stockfishchess.org/tests
+[guideline-link]: https://github.com/glinscott/fishtest/wiki/Creating-my-first-test
+[license-link]: https://github.com/official-stockfish/Stockfish/blob/master/Copying.txt
+[programming-link]: https://www.chessprogramming.org/Main_Page
+[programmingsf-link]: https://www.chessprogramming.org/Stockfish
+[readme-link]: https://github.com/official-stockfish/Stockfish/blob/master/README.md
+[release-link]: https://github.com/official-stockfish/Stockfish/releases/latest
+[src-link]: https://github.com/official-stockfish/Stockfish/tree/master/src
+[stockfish128-logo]: https://stockfishchess.org/images/logo/icon_128x128.png
+[uci-link]: https://backscattering.de/chess/uci/
+[website-link]: https://stockfishchess.org
+[website-blog-link]: https://stockfishchess.org/blog/
+[wiki-link]: https://github.com/official-stockfish/Stockfish/wiki
+[wiki-usage-link]: https://github.com/official-stockfish/Stockfish/wiki/Download-and-usage
+[wiki-compile-link]: https://github.com/official-stockfish/Stockfish/wiki/Compiling-from-source
+[wiki-commands-link]: https://github.com/official-stockfish/Stockfish/wiki/Commands
+[worker-link]: https://github.com/glinscott/fishtest/wiki/Running-the-worker
+
+[build-badge]: https://img.shields.io/github/actions/workflow/status/official-stockfish/Stockfish/stockfish.yml?branch=master&style=for-the-badge&label=stockfish&logo=github
+[commits-badge]: https://img.shields.io/github/commits-since/official-stockfish/Stockfish/latest?style=for-the-badge
+[discord-badge]: https://img.shields.io/discord/435943710472011776?style=for-the-badge&label=discord&logo=Discord
+[fishtest-badge]: https://img.shields.io/website?style=for-the-badge&down_color=red&down_message=Offline&label=Fishtest&up_color=success&up_message=Online&url=https%3A%2F%2Ftests.stockfishchess.org%2Ftests%2Ffinished
+[license-badge]: https://img.shields.io/github/license/official-stockfish/Stockfish?style=for-the-badge&label=license&color=success
+[release-badge]: https://img.shields.io/github/v/release/official-stockfish/Stockfish?style=for-the-badge&label=official%20release
+[website-badge]: https://img.shields.io/website?style=for-the-badge&down_color=red&down_message=Offline&label=website&up_color=success&up_message=Online&url=https%3A%2F%2Fstockfishchess.org
diff --git a/src/Stockfish/Top CPU Contributors.txt b/src/Stockfish/Top CPU Contributors.txt
new file mode 100644
index 0000000..74c471b
--- /dev/null
+++ b/src/Stockfish/Top CPU Contributors.txt
@@ -0,0 +1,267 @@
+Contributors to Fishtest with >10,000 CPU hours, as of 2023-06-20.
+Thank you!
+
+Username CPU Hours Games played
+------------------------------------------------------------------
+noobpwnftw 37457426 2850540907
+technologov 14135647 742892808
+linrock 4423514 303254809
+mlang 3026000 200065824
+dew 1689162 100033738
+okrout 1578136 148855886
+pemo 1508508 48814305
+grandphish2 1461406 91540343
+TueRens 1194790 70400852
+JojoM 947612 61773190
+tvijlbrief 796125 51897690
+sebastronomy 742434 38218524
+mibere 703840 46867607
+gvreuls 651026 42988582
+oz 543438 39314736
+cw 517858 34869755
+fastgm 503862 30260818
+leszek 467278 33514883
+CSU_Dynasty 464940 31177118
+ctoks 434416 28506889
+crunchy 427035 27344275
+maximmasiutin 424795 26577722
+bcross 415722 29060963
+olafm 395922 32268020
+rpngn 348378 24560289
+velislav 342567 22138992
+Fisherman 327231 21829379
+mgrabiak 300612 20608380
+Dantist 296386 18031762
+nordlandia 246201 16189678
+robal 241300 15656382
+marrco 234581 17714473
+ncfish1 227517 15233777
+glinscott 208125 13277240
+drabel 204167 13930674
+mhoram 202894 12601997
+bking_US 198894 11876016
+Thanar 179852 12365359
+vdv 175544 9904472
+spams 157128 10319326
+sqrt2 147963 9724586
+DesolatedDodo 146350 9536172
+Calis007 143165 9478764
+vdbergh 138650 9064413
+CoffeeOne 137100 5024116
+armo9494 136191 9460264
+malala 136182 8002293
+xoto 133759 9159372
+davar 129023 8376525
+DMBK 122960 8980062
+dsmith 122059 7570238
+amicic 119661 7938029
+Data 113305 8220352
+BrunoBanani 112960 7436849
+CypressChess 108331 7759788
+skiminki 107583 7218170
+jcAEie 105675 8238962
+MaZePallas 102823 6633619
+sterni1971 100532 5880772
+sunu 100167 7040199
+zeryl 99331 6221261
+thirdlife 99124 2242380
+ElbertoOne 99028 7023771
+cuistot 98853 6069816
+bigpen0r 94809 6529203
+brabos 92118 6186135
+Wolfgang 91939 6105872
+psk 89957 5984901
+sschnee 88235 5268000
+racerschmacer 85805 6122790
+Fifis 85722 5709729
+Dubslow 84986 6042456
+Vizvezdenec 83761 5344740
+0x3C33 82614 5271253
+BRAVONE 81239 5054681
+nssy 76497 5259388
+jromang 76106 5236025
+teddybaer 75125 5407666
+tolkki963 74762 5149662
+megaman7de 74351 4940352
+Wencey 74181 4711488
+Pking_cda 73776 5293873
+yurikvelo 73150 5004382
+markkulix 72607 5304642
+Bobo1239 70579 4794999
+solarlight 70517 5028306
+dv8silencer 70287 3883992
+manap 66273 4121774
+tinker 64333 4268790
+qurashee 61208 3429862
+Mineta 59357 4418202
+Spprtr 58723 3911011
+AGI 58147 4325994
+robnjr 57262 4053117
+Freja 56938 3733019
+MaxKlaxxMiner 56879 3423958
+MarcusTullius 56746 3762951
+ttruscott 56010 3680085
+rkl 55132 4164467
+renouve 53811 3501516
+javran 53785 4627608
+finfish 51360 3370515
+eva42 51272 3599691
+eastorwest 51117 3454811
+rap 49985 3219146
+pb00067 49733 3298934
+OuaisBla 48626 3445134
+ronaldjerum 47654 3240695
+biffhero 46564 3111352
+VoyagerOne 45476 3452465
+jmdana 44893 3065205
+maposora 44597 4039578
+oryx 44570 3454238
+speedycpu 43842 3003273
+jbwiebe 43305 2805433
+GPUex 42378 3133332
+Antihistamine 41788 2761312
+mhunt 41735 2691355
+homyur 39893 2850481
+gri 39871 2515779
+Garf 37741 2999686
+SC 37299 2731694
+csnodgrass 36207 2688994
+strelock 34716 2074055
+szupaw 34102 2880346
+EthanOConnor 33370 2090311
+slakovv 32915 2021889
+Gelma 31771 1551204
+gopeto 31671 2060990
+kdave 31157 2198362
+manapbk 30987 1810399
+Prcuvu 30377 2170122
+anst 30301 2190091
+jkiiski 30136 1904470
+spcc 29925 1901692
+hyperbolic.tom 29840 2017394
+chuckstablers 29659 2093438
+Pyafue 29650 1902349
+belzedar94 28846 1811530
+chriswk 26902 1868317
+xwziegtm 26897 2124586
+achambord 26582 1767323
+Patrick_G 26276 1801617
+yorkman 26193 1992080
+Ulysses 25288 1689730
+SFTUser 25182 1675689
+nabildanial 24942 1519409
+Sharaf_DG 24765 1786697
+Maxim 24705 1502062
+rodneyc 24376 1416402
+agg177 23890 1395014
+Goatminola 23763 1956036
+Ente 23639 1671638
+Jopo12321 23467 1483172
+JanErik 23408 1703875
+Isidor 23388 1680691
+Norabor 23371 1603244
+cisco2015 22920 1763301
+jsys14 22824 1591906
+Zirie 22542 1472937
+team-oh 22272 1636708
+Roady 22220 1465606
+MazeOfGalious 21978 1629593
+sg4032 21947 1643353
+ianh2105 21725 1632562
+xor12 21628 1680365
+dex 21612 1467203
+nesoneg 21494 1463031
+user213718 21454 1404128
+sphinx 21211 1384728
+AndreasKrug 21097 1634811
+jjoshua2 21001 1423089
+Zake9298 20938 1565848
+horst.prack 20878 1465656
+0xB00B1ES 20590 1208666
+j3corre 20405 941444
+Adrian.Schmidt123 20316 1281436
+wei 19973 1745989
+notchris 19958 1800128
+Serpensin 19840 1697528
+Gaster319 19712 1677310
+fishtester 19617 1257388
+rstoesser 19569 1293588
+eudhan 19274 1283717
+votoanthuan 19108 1609992
+vulcan 18871 1729392
+Karpovbot 18766 1053178
+qoo_charly_cai 18543 1284937
+jundery 18445 1115855
+ville 17883 1384026
+chris 17698 1487385
+purplefishies 17595 1092533
+dju 17414 981289
+iisiraider 17275 1049015
+DragonLord 17014 1162790
+redstone59 16842 1461780
+Alb11747 16787 1213926
+IgorLeMasson 16064 1147232
+Karby 15982 979610
+scuzzi 15757 968735
+ako027ako 15671 1173203
+Nikolay.IT 15154 1068349
+Andrew Grant 15114 895539
+Naven94 15054 834762
+OssumOpossum 14857 1007129
+ZacHFX 14783 1021842
+enedene 14476 905279
+bpfliegel 14233 882523
+mpx86 14019 759568
+jpulman 13982 870599
+Skiff84 13826 721996
+crocogoat 13803 1117422
+Nesa92 13786 1114691
+joster 13710 946160
+mbeier 13650 1044928
+Hjax 13535 915487
+Nullvalue 13468 1140498
+Dark_wizzie 13422 1007152
+Rudolphous 13244 883140
+pirt 13100 1009897
+Machariel 13010 863104
+infinigon 12991 943216
+mabichito 12903 749391
+thijsk 12886 722107
+AdrianSA 12860 804972
+Flopzee 12698 894821
+korposzczur 12606 838168
+fatmurphy 12547 853210
+SapphireBrand 12416 969604
+Oakwen 12399 844109
+deflectooor 12386 579392
+modolief 12386 896470
+Farseer 12249 694108
+Jackfish 12213 805008
+pgontarz 12151 848794
+dbernier 12103 860824
+getraideBFF 12072 1024966
+stocky 11954 699440
+mschmidt 11941 803401
+MooTheCow 11870 773598
+FormazChar 11766 885707
+whelanh 11557 245188
+3cho 11494 1031076
+infinity 11470 727027
+aga 11412 695127
+torbjo 11395 729145
+Thomas A. Anderson 11372 732094
+savage84 11358 670860
+d64 11263 789184
+ali-al-zhrani 11245 779246
+snicolet 11106 869170
+dapper 11032 771402
+ols 10947 624903
+Karmatron 10828 677458
+basepi 10637 744851
+Cubox 10621 826448
+michaelrpg 10509 739239
+OIVAS7572 10420 995586
+jojo2357 10419 929708
+WoodMan777 10380 873720
+Garruk 10365 706465
+dzjp 10343 732529
diff --git a/src/Stockfish/src/Makefile b/src/Stockfish/src/Makefile
new file mode 100644
index 0000000..8266461
--- /dev/null
+++ b/src/Stockfish/src/Makefile
@@ -0,0 +1,1043 @@
+# Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+# Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
+#
+# Stockfish is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Stockfish is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see .
+
+
+### ==========================================================================
+### Section 1. General Configuration
+### ==========================================================================
+
+### Establish the operating system name
+KERNEL = $(shell uname -s)
+ifeq ($(KERNEL),Linux)
+ OS = $(shell uname -o)
+endif
+
+### Target Windows OS
+ifeq ($(OS),Windows_NT)
+ ifneq ($(COMP),ndk)
+ target_windows = yes
+ endif
+else ifeq ($(COMP),mingw)
+ target_windows = yes
+ ifeq ($(WINE_PATH),)
+ WINE_PATH = $(shell which wine)
+ endif
+endif
+
+### Executable name
+ifeq ($(target_windows),yes)
+ EXE = stockfish.exe
+else
+ EXE = stockfish
+endif
+
+### Installation dir definitions
+PREFIX = /usr/local
+BINDIR = $(PREFIX)/bin
+
+### Built-in benchmark for pgo-builds
+ifeq ($(SDE_PATH),)
+ PGOBENCH = $(WINE_PATH) ./$(EXE) bench
+else
+ PGOBENCH = $(SDE_PATH) -- $(WINE_PATH) ./$(EXE) bench
+endif
+
+### Source and object files
+SRCS = benchmark.cpp bitbase.cpp bitboard.cpp endgame.cpp evaluate.cpp main.cpp \
+ material.cpp misc.cpp movegen.cpp movepick.cpp pawns.cpp position.cpp psqt.cpp \
+ search.cpp thread.cpp timeman.cpp tt.cpp uci.cpp ucioption.cpp tune.cpp syzygy/tbprobe.cpp \
+ nnue/evaluate_nnue.cpp nnue/features/half_ka_v2_hm.cpp
+
+OBJS = $(notdir $(SRCS:.cpp=.o))
+
+VPATH = syzygy:nnue:nnue/features
+
+### ==========================================================================
+### Section 2. High-level Configuration
+### ==========================================================================
+#
+# flag --- Comp switch --- Description
+# ----------------------------------------------------------------------------
+#
+# debug = yes/no --- -DNDEBUG --- Enable/Disable debug mode
+# sanitize = none/ ... (-fsanitize )
+# --- ( undefined ) --- enable undefined behavior checks
+# --- ( thread ) --- enable threading error checks
+# --- ( address ) --- enable memory access checks
+# --- ...etc... --- see compiler documentation for supported sanitizers
+# optimize = yes/no --- (-O3/-fast etc.) --- Enable/Disable optimizations
+# arch = (name) --- (-arch) --- Target architecture
+# bits = 64/32 --- -DIS_64BIT --- 64-/32-bit operating system
+# prefetch = yes/no --- -DUSE_PREFETCH --- Use prefetch asm-instruction
+# popcnt = yes/no --- -DUSE_POPCNT --- Use popcnt asm-instruction
+# pext = yes/no --- -DUSE_PEXT --- Use pext x86_64 asm-instruction
+# sse = yes/no --- -msse --- Use Intel Streaming SIMD Extensions
+# mmx = yes/no --- -mmmx --- Use Intel MMX instructions
+# sse2 = yes/no --- -msse2 --- Use Intel Streaming SIMD Extensions 2
+# ssse3 = yes/no --- -mssse3 --- Use Intel Supplemental Streaming SIMD Extensions 3
+# sse41 = yes/no --- -msse4.1 --- Use Intel Streaming SIMD Extensions 4.1
+# avx2 = yes/no --- -mavx2 --- Use Intel Advanced Vector Extensions 2
+# avxvnni = yes/no --- -mavxvnni --- Use Intel Vector Neural Network Instructions AVX
+# avx512 = yes/no --- -mavx512bw --- Use Intel Advanced Vector Extensions 512
+# vnni256 = yes/no --- -mavx256vnni --- Use Intel Vector Neural Network Instructions 512 with 256bit operands
+# vnni512 = yes/no --- -mavx512vnni --- Use Intel Vector Neural Network Instructions 512
+# neon = yes/no --- -DUSE_NEON --- Use ARM SIMD architecture
+# dotprod = yes/no --- -DUSE_NEON_DOTPROD --- Use ARM advanced SIMD Int8 dot product instructions
+#
+# Note that Makefile is space sensitive, so when adding new architectures
+# or modifying existing flags, you have to make sure there are no extra spaces
+# at the end of the line for flag values.
+#
+# Example of use for these flags:
+# make build ARCH=x86-64-avx512 debug=yes sanitize="address undefined"
+
+
+### 2.1. General and architecture defaults
+
+ifeq ($(ARCH),)
+ ARCH = x86-64-modern
+ help_skip_sanity = yes
+endif
+# explicitly check for the list of supported architectures (as listed with make help),
+# the user can override with `make ARCH=x86-32-vnni256 SUPPORTED_ARCH=true`
+ifeq ($(ARCH), $(filter $(ARCH), \
+ x86-64-vnni512 x86-64-vnni256 x86-64-avx512 x86-64-avxvnni x86-64-bmi2 \
+ x86-64-avx2 x86-64-sse41-popcnt x86-64-modern x86-64-ssse3 x86-64-sse3-popcnt \
+ x86-64 x86-32-sse41-popcnt x86-32-sse2 x86-32 ppc-64 ppc-32 e2k \
+ armv7 armv7-neon armv8 armv8-dotprod apple-silicon general-64 general-32 riscv64))
+ SUPPORTED_ARCH=true
+else
+ SUPPORTED_ARCH=false
+endif
+
+optimize = yes
+debug = no
+sanitize = none
+bits = 64
+prefetch = no
+popcnt = no
+pext = no
+sse = no
+mmx = no
+sse2 = no
+ssse3 = no
+sse41 = no
+avx2 = no
+avxvnni = no
+avx512 = no
+vnni256 = no
+vnni512 = no
+neon = no
+dotprod = no
+arm_version = 0
+STRIP = strip
+
+### 2.2 Architecture specific
+
+ifeq ($(findstring x86,$(ARCH)),x86)
+
+# x86-32/64
+
+ifeq ($(findstring x86-32,$(ARCH)),x86-32)
+ arch = i386
+ bits = 32
+ sse = no
+ mmx = yes
+else
+ arch = x86_64
+ sse = yes
+ sse2 = yes
+endif
+
+ifeq ($(findstring -sse,$(ARCH)),-sse)
+ sse = yes
+endif
+
+ifeq ($(findstring -popcnt,$(ARCH)),-popcnt)
+ popcnt = yes
+endif
+
+ifeq ($(findstring -mmx,$(ARCH)),-mmx)
+ mmx = yes
+endif
+
+ifeq ($(findstring -sse2,$(ARCH)),-sse2)
+ sse = yes
+ sse2 = yes
+endif
+
+ifeq ($(findstring -ssse3,$(ARCH)),-ssse3)
+ sse = yes
+ sse2 = yes
+ ssse3 = yes
+endif
+
+ifeq ($(findstring -sse41,$(ARCH)),-sse41)
+ sse = yes
+ sse2 = yes
+ ssse3 = yes
+ sse41 = yes
+endif
+
+ifeq ($(findstring -modern,$(ARCH)),-modern)
+ popcnt = yes
+ sse = yes
+ sse2 = yes
+ ssse3 = yes
+ sse41 = yes
+endif
+
+ifeq ($(findstring -avx2,$(ARCH)),-avx2)
+ popcnt = yes
+ sse = yes
+ sse2 = yes
+ ssse3 = yes
+ sse41 = yes
+ avx2 = yes
+endif
+
+ifeq ($(findstring -avxvnni,$(ARCH)),-avxvnni)
+ popcnt = yes
+ sse = yes
+ sse2 = yes
+ ssse3 = yes
+ sse41 = yes
+ avx2 = yes
+ avxvnni = yes
+ pext = yes
+endif
+
+ifeq ($(findstring -bmi2,$(ARCH)),-bmi2)
+ popcnt = yes
+ sse = yes
+ sse2 = yes
+ ssse3 = yes
+ sse41 = yes
+ avx2 = yes
+ pext = yes
+endif
+
+ifeq ($(findstring -avx512,$(ARCH)),-avx512)
+ popcnt = yes
+ sse = yes
+ sse2 = yes
+ ssse3 = yes
+ sse41 = yes
+ avx2 = yes
+ pext = yes
+ avx512 = yes
+endif
+
+ifeq ($(findstring -vnni256,$(ARCH)),-vnni256)
+ popcnt = yes
+ sse = yes
+ sse2 = yes
+ ssse3 = yes
+ sse41 = yes
+ avx2 = yes
+ pext = yes
+ vnni256 = yes
+endif
+
+ifeq ($(findstring -vnni512,$(ARCH)),-vnni512)
+ popcnt = yes
+ sse = yes
+ sse2 = yes
+ ssse3 = yes
+ sse41 = yes
+ avx2 = yes
+ pext = yes
+ avx512 = yes
+ vnni512 = yes
+endif
+
+ifeq ($(sse),yes)
+ prefetch = yes
+endif
+
+# 64-bit pext is not available on x86-32
+ifeq ($(bits),32)
+ pext = no
+endif
+
+else
+
+# all other architectures
+
+ifeq ($(ARCH),general-32)
+ arch = any
+ bits = 32
+endif
+
+ifeq ($(ARCH),general-64)
+ arch = any
+endif
+
+ifeq ($(ARCH),armv7)
+ arch = armv7
+ prefetch = yes
+ bits = 32
+ arm_version = 7
+endif
+
+ifeq ($(ARCH),armv7-neon)
+ arch = armv7
+ prefetch = yes
+ popcnt = yes
+ neon = yes
+ bits = 32
+ arm_version = 7
+endif
+
+ifeq ($(ARCH),armv8)
+ arch = armv8
+ prefetch = yes
+ popcnt = yes
+ neon = yes
+ arm_version = 8
+endif
+
+ifeq ($(ARCH),armv8-dotprod)
+ arch = armv8
+ prefetch = yes
+ popcnt = yes
+ neon = yes
+ dotprod = yes
+ arm_version = 8
+endif
+
+ifeq ($(ARCH),apple-silicon)
+ arch = arm64
+ prefetch = yes
+ popcnt = yes
+ neon = yes
+ dotprod = yes
+ arm_version = 8
+endif
+
+ifeq ($(ARCH),ppc-32)
+ arch = ppc
+ bits = 32
+endif
+
+ifeq ($(ARCH),ppc-64)
+ arch = ppc64
+ popcnt = yes
+ prefetch = yes
+endif
+
+ifeq ($(findstring e2k,$(ARCH)),e2k)
+ arch = e2k
+ mmx = yes
+ bits = 64
+ sse = yes
+ sse2 = yes
+ ssse3 = yes
+ sse41 = yes
+ popcnt = yes
+endif
+
+ifeq ($(ARCH),riscv64)
+ arch = riscv64
+endif
+endif
+
+
+### ==========================================================================
+### Section 3. Low-level Configuration
+### ==========================================================================
+
+### 3.1 Selecting compiler (default = gcc)
+ifeq ($(MAKELEVEL),0)
+ export ENV_CXXFLAGS := $(CXXFLAGS)
+ export ENV_DEPENDFLAGS := $(DEPENDFLAGS)
+ export ENV_LDFLAGS := $(LDFLAGS)
+endif
+
+CXXFLAGS = $(ENV_CXXFLAGS) -Wall -Wcast-qual -fno-exceptions -std=c++17 $(EXTRACXXFLAGS)
+DEPENDFLAGS = $(ENV_DEPENDFLAGS) -std=c++17
+LDFLAGS = $(ENV_LDFLAGS) $(EXTRALDFLAGS)
+
+ifeq ($(COMP),)
+ COMP=gcc
+endif
+
+ifeq ($(COMP),gcc)
+ comp=gcc
+ CXX=g++
+ CXXFLAGS += -pedantic -Wextra -Wshadow -Wmissing-declarations
+
+ ifeq ($(arch),$(filter $(arch),armv7 armv8 riscv64))
+ ifeq ($(OS),Android)
+ CXXFLAGS += -m$(bits)
+ LDFLAGS += -m$(bits)
+ endif
+ ifeq ($(ARCH),riscv64)
+ CXXFLAGS += -latomic
+ endif
+ else
+ CXXFLAGS += -m$(bits)
+ LDFLAGS += -m$(bits)
+ endif
+
+ ifeq ($(arch),$(filter $(arch),armv7))
+ LDFLAGS += -latomic
+ endif
+
+ ifneq ($(KERNEL),Darwin)
+ LDFLAGS += -Wl,--no-as-needed
+ endif
+endif
+
+ifeq ($(target_windows),yes)
+ LDFLAGS += -static
+endif
+
+ifeq ($(COMP),mingw)
+ comp=mingw
+
+ ifeq ($(bits),64)
+ ifeq ($(shell which x86_64-w64-mingw32-c++-posix 2> /dev/null),)
+ CXX=x86_64-w64-mingw32-c++
+ else
+ CXX=x86_64-w64-mingw32-c++-posix
+ endif
+ else
+ ifeq ($(shell which i686-w64-mingw32-c++-posix 2> /dev/null),)
+ CXX=i686-w64-mingw32-c++
+ else
+ CXX=i686-w64-mingw32-c++-posix
+ endif
+ endif
+ CXXFLAGS += -pedantic -Wextra -Wshadow -Wmissing-declarations
+endif
+
+ifeq ($(COMP),icx)
+ comp=icx
+ CXX=icpx
+ CXXFLAGS += --intel -pedantic -Wextra -Wshadow -Wmissing-prototypes \
+ -Wconditional-uninitialized -Wabi -Wdeprecated
+endif
+
+ifeq ($(COMP),clang)
+ comp=clang
+ CXX=clang++
+ ifeq ($(target_windows),yes)
+ CXX=x86_64-w64-mingw32-clang++
+ endif
+
+ CXXFLAGS += -pedantic -Wextra -Wshadow -Wmissing-prototypes \
+ -Wconditional-uninitialized
+
+ ifeq ($(filter $(KERNEL),Darwin OpenBSD FreeBSD),)
+ ifeq ($(target_windows),)
+ ifneq ($(RTLIB),compiler-rt)
+ LDFLAGS += -latomic
+ endif
+ endif
+ endif
+
+ ifeq ($(arch),$(filter $(arch),armv7 armv8 riscv64))
+ ifeq ($(OS),Android)
+ CXXFLAGS += -m$(bits)
+ LDFLAGS += -m$(bits)
+ endif
+ ifeq ($(ARCH),riscv64)
+ CXXFLAGS += -latomic
+ endif
+ else
+ CXXFLAGS += -m$(bits)
+ LDFLAGS += -m$(bits)
+ endif
+endif
+
+ifeq ($(KERNEL),Darwin)
+ CXXFLAGS += -mmacosx-version-min=10.14
+ LDFLAGS += -mmacosx-version-min=10.14
+ ifneq ($(arch),any)
+ CXXFLAGS += -arch $(arch)
+ LDFLAGS += -arch $(arch)
+ endif
+ XCRUN = xcrun
+endif
+
+# To cross-compile for Android, NDK version r21 or later is recommended.
+# In earlier NDK versions, you'll need to pass -fno-addrsig if using GNU binutils.
+# Currently we don't know how to make PGO builds with the NDK yet.
+ifeq ($(COMP),ndk)
+ CXXFLAGS += -stdlib=libc++ -fPIE
+ comp=clang
+ ifeq ($(arch),armv7)
+ CXX=armv7a-linux-androideabi16-clang++
+ CXXFLAGS += -mthumb -march=armv7-a -mfloat-abi=softfp -mfpu=neon
+ ifneq ($(shell which arm-linux-androideabi-strip 2>/dev/null),)
+ STRIP=arm-linux-androideabi-strip
+ else
+ STRIP=llvm-strip
+ endif
+ endif
+ ifeq ($(arch),armv8)
+ CXX=aarch64-linux-android21-clang++
+ ifneq ($(shell which aarch64-linux-android-strip 2>/dev/null),)
+ STRIP=aarch64-linux-android-strip
+ else
+ STRIP=llvm-strip
+ endif
+ endif
+ LDFLAGS += -static-libstdc++ -pie -lm -latomic
+endif
+
+ifeq ($(comp),icx)
+ profile_make = icx-profile-make
+ profile_use = icx-profile-use
+else ifeq ($(comp),clang)
+ profile_make = clang-profile-make
+ profile_use = clang-profile-use
+else
+ profile_make = gcc-profile-make
+ profile_use = gcc-profile-use
+ ifeq ($(KERNEL),Darwin)
+ EXTRAPROFILEFLAGS = -fvisibility=hidden
+ endif
+endif
+
+### Travis CI script uses COMPILER to overwrite CXX
+ifdef COMPILER
+ COMPCXX=$(COMPILER)
+endif
+
+### Allow overwriting CXX from command line
+ifdef COMPCXX
+ CXX=$(COMPCXX)
+endif
+
+### Sometimes gcc is really clang
+ifeq ($(COMP),gcc)
+ gccversion = $(shell $(CXX) --version 2>/dev/null)
+ gccisclang = $(findstring clang,$(gccversion))
+ ifneq ($(gccisclang),)
+ profile_make = clang-profile-make
+ profile_use = clang-profile-use
+ endif
+endif
+
+### On mingw use Windows threads, otherwise POSIX
+ifneq ($(comp),mingw)
+ CXXFLAGS += -DUSE_PTHREADS
+ # On Android Bionic's C library comes with its own pthread implementation bundled in
+ ifneq ($(OS),Android)
+ # Haiku has pthreads in its libroot, so only link it in on other platforms
+ ifneq ($(KERNEL),Haiku)
+ ifneq ($(COMP),ndk)
+ LDFLAGS += -lpthread
+ endif
+ endif
+ endif
+endif
+
+### 3.2.1 Debugging
+ifeq ($(debug),no)
+ CXXFLAGS += -DNDEBUG
+else
+ CXXFLAGS += -g
+endif
+
+### 3.2.2 Debugging with undefined behavior sanitizers
+ifneq ($(sanitize),none)
+ CXXFLAGS += -g3 $(addprefix -fsanitize=,$(sanitize))
+ LDFLAGS += $(addprefix -fsanitize=,$(sanitize))
+endif
+
+### 3.3 Optimization
+ifeq ($(optimize),yes)
+
+ CXXFLAGS += -O3
+
+ ifeq ($(comp),gcc)
+ ifeq ($(OS), Android)
+ CXXFLAGS += -fno-gcse -mthumb -march=armv7-a -mfloat-abi=softfp
+ endif
+ endif
+
+ ifeq ($(KERNEL),Darwin)
+ ifeq ($(comp),$(filter $(comp),clang icx))
+ CXXFLAGS += -mdynamic-no-pic
+ endif
+
+ ifeq ($(comp),gcc)
+ ifneq ($(arch),arm64)
+ CXXFLAGS += -mdynamic-no-pic
+ endif
+ endif
+ endif
+
+ ifeq ($(comp),clang)
+ clangmajorversion = $(shell $(CXX) -dumpversion 2>/dev/null | cut -f1 -d.)
+ ifeq ($(shell expr $(clangmajorversion) \< 16),1)
+ CXXFLAGS += -fexperimental-new-pass-manager
+ endif
+ endif
+endif
+
+### 3.4 Bits
+ifeq ($(bits),64)
+ CXXFLAGS += -DIS_64BIT
+endif
+
+### 3.5 prefetch and popcount
+ifeq ($(prefetch),yes)
+ ifeq ($(sse),yes)
+ CXXFLAGS += -msse
+ endif
+else
+ CXXFLAGS += -DNO_PREFETCH
+endif
+
+ifeq ($(popcnt),yes)
+ ifeq ($(arch),$(filter $(arch),ppc64 armv7 armv8 arm64))
+ CXXFLAGS += -DUSE_POPCNT
+ else
+ CXXFLAGS += -msse3 -mpopcnt -DUSE_POPCNT
+ endif
+endif
+
+### 3.6 SIMD architectures
+ifeq ($(avx2),yes)
+ CXXFLAGS += -DUSE_AVX2
+ ifeq ($(comp),$(filter $(comp),gcc clang mingw icx))
+ CXXFLAGS += -mavx2 -mbmi
+ endif
+endif
+
+ifeq ($(avxvnni),yes)
+ CXXFLAGS += -DUSE_VNNI -DUSE_AVXVNNI
+ ifeq ($(comp),$(filter $(comp),gcc clang mingw icx))
+ CXXFLAGS += -mavxvnni
+ endif
+endif
+
+ifeq ($(avx512),yes)
+ CXXFLAGS += -DUSE_AVX512
+ ifeq ($(comp),$(filter $(comp),gcc clang mingw icx))
+ CXXFLAGS += -mavx512f -mavx512bw
+ endif
+endif
+
+ifeq ($(vnni256),yes)
+ CXXFLAGS += -DUSE_VNNI
+ ifeq ($(comp),$(filter $(comp),gcc clang mingw icx))
+ CXXFLAGS += -mavx512f -mavx512bw -mavx512vnni -mavx512dq -mavx512vl -mprefer-vector-width=256
+ endif
+endif
+
+ifeq ($(vnni512),yes)
+ CXXFLAGS += -DUSE_VNNI
+ ifeq ($(comp),$(filter $(comp),gcc clang mingw icx))
+ CXXFLAGS += -mavx512f -mavx512bw -mavx512vnni -mavx512dq -mavx512vl -mprefer-vector-width=512
+ endif
+endif
+
+ifeq ($(sse41),yes)
+ CXXFLAGS += -DUSE_SSE41
+ ifeq ($(comp),$(filter $(comp),gcc clang mingw icx))
+ CXXFLAGS += -msse4.1
+ endif
+endif
+
+ifeq ($(ssse3),yes)
+ CXXFLAGS += -DUSE_SSSE3
+ ifeq ($(comp),$(filter $(comp),gcc clang mingw icx))
+ CXXFLAGS += -mssse3
+ endif
+endif
+
+ifeq ($(sse2),yes)
+ CXXFLAGS += -DUSE_SSE2
+ ifeq ($(comp),$(filter $(comp),gcc clang mingw icx))
+ CXXFLAGS += -msse2
+ endif
+endif
+
+ifeq ($(mmx),yes)
+ CXXFLAGS += -DUSE_MMX
+ ifeq ($(comp),$(filter $(comp),gcc clang mingw icx))
+ CXXFLAGS += -mmmx
+ endif
+endif
+
+ifeq ($(neon),yes)
+ CXXFLAGS += -DUSE_NEON=$(arm_version)
+ ifeq ($(KERNEL),Linux)
+ ifneq ($(COMP),ndk)
+ ifneq ($(arch),armv8)
+ CXXFLAGS += -mfpu=neon
+ endif
+ endif
+ endif
+endif
+
+ifeq ($(dotprod),yes)
+ CXXFLAGS += -march=armv8.2-a+dotprod -DUSE_NEON_DOTPROD
+endif
+
+### 3.7 pext
+ifeq ($(pext),yes)
+ CXXFLAGS += -DUSE_PEXT
+ ifeq ($(comp),$(filter $(comp),gcc clang mingw icx))
+ CXXFLAGS += -mbmi2
+ endif
+endif
+
+### 3.7.1 Try to include git commit sha for versioning
+GIT_SHA = $(shell git rev-parse HEAD 2>/dev/null | cut -c 1-8)
+ifneq ($(GIT_SHA), )
+ CXXFLAGS += -DGIT_SHA=$(GIT_SHA)
+endif
+
+### 3.7.2 Try to include git commit date for versioning
+GIT_DATE = $(shell git show -s --date=format:'%Y%m%d' --format=%cd HEAD 2>/dev/null)
+ifneq ($(GIT_DATE), )
+ CXXFLAGS += -DGIT_DATE=$(GIT_DATE)
+endif
+
+### 3.8 Link Time Optimization
+### This is a mix of compile and link time options because the lto link phase
+### needs access to the optimization flags.
+ifeq ($(optimize),yes)
+ifeq ($(debug), no)
+ ifeq ($(comp),$(filter $(comp),clang icx))
+ CXXFLAGS += -flto=full
+ ifeq ($(comp),icx)
+ CXXFLAGS += -fwhole-program-vtables
+ endif
+ ifeq ($(target_windows),yes)
+ CXXFLAGS += -fuse-ld=lld
+ endif
+ LDFLAGS += $(CXXFLAGS)
+
+# GCC and CLANG use different methods for parallelizing LTO and CLANG pretends to be
+# GCC on some systems.
+ else ifeq ($(comp),gcc)
+ ifeq ($(gccisclang),)
+ CXXFLAGS += -flto -flto-partition=one
+ LDFLAGS += $(CXXFLAGS) -flto=jobserver
+ else
+ CXXFLAGS += -flto=full
+ LDFLAGS += $(CXXFLAGS)
+ endif
+
+# To use LTO and static linking on Windows,
+# the tool chain requires gcc version 10.1 or later.
+ else ifeq ($(comp),mingw)
+ CXXFLAGS += -flto -flto-partition=one
+ LDFLAGS += $(CXXFLAGS) -save-temps
+ endif
+endif
+endif
+
+### 3.9 Android 5 can only run position independent executables. Note that this
+### breaks Android 4.0 and earlier.
+ifeq ($(OS), Android)
+ CXXFLAGS += -fPIE
+ LDFLAGS += -fPIE -pie
+endif
+
+### ==========================================================================
+### Section 4. Public Targets
+### ==========================================================================
+
+
+help:
+ @echo ""
+ @echo "To compile stockfish, type: "
+ @echo ""
+ @echo "make target ARCH=arch [COMP=compiler] [COMPCXX=cxx]"
+ @echo ""
+ @echo "Supported targets:"
+ @echo ""
+ @echo "help > Display architecture details"
+ @echo "profile-build > standard build with profile-guided optimization"
+ @echo "build > skip profile-guided optimization"
+ @echo "net > Download the default nnue net"
+ @echo "strip > Strip executable"
+ @echo "install > Install executable"
+ @echo "clean > Clean up"
+ @echo ""
+ @echo "Supported archs:"
+ @echo ""
+ @echo "x86-64-vnni512 > x86 64-bit with vnni 512bit support"
+ @echo "x86-64-vnni256 > x86 64-bit with vnni 512bit support, limit operands to 256bit wide"
+ @echo "x86-64-avx512 > x86 64-bit with avx512 support"
+ @echo "x86-64-avxvnni > x86 64-bit with vnni 256bit support"
+ @echo "x86-64-bmi2 > x86 64-bit with bmi2 support"
+ @echo "x86-64-avx2 > x86 64-bit with avx2 support"
+ @echo "x86-64-sse41-popcnt > x86 64-bit with sse41 and popcnt support"
+ @echo "x86-64-modern > common modern CPU, currently x86-64-sse41-popcnt"
+ @echo "x86-64-ssse3 > x86 64-bit with ssse3 support"
+ @echo "x86-64-sse3-popcnt > x86 64-bit with sse3 and popcnt support"
+ @echo "x86-64 > x86 64-bit generic (with sse2 support)"
+ @echo "x86-32-sse41-popcnt > x86 32-bit with sse41 and popcnt support"
+ @echo "x86-32-sse2 > x86 32-bit with sse2 support"
+ @echo "x86-32 > x86 32-bit generic (with mmx and sse support)"
+ @echo "ppc-64 > PPC 64-bit"
+ @echo "ppc-32 > PPC 32-bit"
+ @echo "armv7 > ARMv7 32-bit"
+ @echo "armv7-neon > ARMv7 32-bit with popcnt and neon"
+ @echo "armv8 > ARMv8 64-bit with popcnt and neon"
+ @echo "armv8-dotprod > ARMv8 64-bit with popcnt, neon and dot product support"
+ @echo "e2k > Elbrus 2000"
+ @echo "apple-silicon > Apple silicon ARM64"
+ @echo "general-64 > unspecified 64-bit"
+ @echo "general-32 > unspecified 32-bit"
+ @echo "riscv64 > RISC-V 64-bit"
+ @echo ""
+ @echo "Supported compilers:"
+ @echo ""
+ @echo "gcc > Gnu compiler (default)"
+ @echo "mingw > Gnu compiler with MinGW under Windows"
+ @echo "clang > LLVM Clang compiler"
+ @echo "icx > Intel oneAPI DPC++/C++ Compiler"
+ @echo "ndk > Google NDK to cross-compile for Android"
+ @echo ""
+ @echo "Simple examples. If you don't know what to do, you likely want to run one of: "
+ @echo ""
+ @echo "make -j profile-build ARCH=x86-64-avx2 # typically a fast compile for common systems "
+ @echo "make -j profile-build ARCH=x86-64-modern # A more portable compile for 64-bit systems "
+ @echo "make -j profile-build ARCH=x86-64 # A portable compile for 64-bit systems "
+ @echo ""
+ @echo "Advanced examples, for experienced users: "
+ @echo ""
+ @echo "make -j profile-build ARCH=x86-64-bmi2"
+ @echo "make -j profile-build ARCH=x86-64-bmi2 COMP=gcc COMPCXX=g++-9.0"
+ @echo "make -j build ARCH=x86-64-ssse3 COMP=clang"
+ @echo ""
+ @echo "-------------------------------"
+ifeq ($(SUPPORTED_ARCH)$(help_skip_sanity), true)
+ @echo "The selected architecture $(ARCH) will enable the following configuration: "
+ @$(MAKE) ARCH=$(ARCH) COMP=$(COMP) config-sanity
+else
+ @echo "Specify a supported architecture with the ARCH option for more details"
+ @echo ""
+endif
+
+
+.PHONY: help build profile-build strip install clean net objclean profileclean \
+ config-sanity \
+ icx-profile-use icx-profile-make \
+ gcc-profile-use gcc-profile-make \
+ clang-profile-use clang-profile-make FORCE
+
+build: net config-sanity
+ $(MAKE) ARCH=$(ARCH) COMP=$(COMP) all
+
+profile-build: net config-sanity objclean profileclean
+ @echo ""
+ @echo "Step 1/4. Building instrumented executable ..."
+ $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_make)
+ @echo ""
+ @echo "Step 2/4. Running benchmark for pgo-build ..."
+ $(PGOBENCH) 2>&1 | tail -n 4
+ @echo ""
+ @echo "Step 3/4. Building optimized executable ..."
+ $(MAKE) ARCH=$(ARCH) COMP=$(COMP) objclean
+ $(MAKE) ARCH=$(ARCH) COMP=$(COMP) $(profile_use)
+ @echo ""
+ @echo "Step 4/4. Deleting profile data ..."
+ $(MAKE) ARCH=$(ARCH) COMP=$(COMP) profileclean
+
+strip:
+ $(STRIP) $(EXE)
+
+install:
+ -mkdir -p -m 755 $(BINDIR)
+ -cp $(EXE) $(BINDIR)
+ $(STRIP) $(BINDIR)/$(EXE)
+
+# clean all
+clean: objclean profileclean
+ @rm -f .depend *~ core
+
+# evaluation network (nnue)
+net:
+ $(eval nnuenet := $(shell grep EvalFileDefaultName evaluate.h | grep define | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
+ @echo "Default net: $(nnuenet)"
+ $(eval nnuedownloadurl1 := https://tests.stockfishchess.org/api/nn/$(nnuenet))
+ $(eval nnuedownloadurl2 := https://github.com/official-stockfish/networks/raw/master/$(nnuenet))
+ $(eval curl_or_wget := $(shell if hash curl 2>/dev/null; then echo "curl -skL"; elif hash wget 2>/dev/null; then echo "wget -qO-"; fi))
+ @if [ "x$(curl_or_wget)" = "x" ]; then \
+ echo "Neither curl nor wget is installed. Install one of these tools unless the net has been downloaded manually"; \
+ fi
+ $(eval shasum_command := $(shell if hash shasum 2>/dev/null; then echo "shasum -a 256 "; elif hash sha256sum 2>/dev/null; then echo "sha256sum "; fi))
+ @if [ "x$(shasum_command)" = "x" ]; then \
+ echo "shasum / sha256sum not found, skipping net validation"; \
+ fi
+ @for nnuedownloadurl in "$(nnuedownloadurl1)" "$(nnuedownloadurl2)"; do \
+ if test -f "$(nnuenet)"; then \
+ echo "$(nnuenet) available."; \
+ else \
+ if [ "x$(curl_or_wget)" != "x" ]; then \
+ echo "Downloading $${nnuedownloadurl}"; $(curl_or_wget) $${nnuedownloadurl} > $(nnuenet);\
+ else \
+ echo "No net found and download not possible"; exit 1;\
+ fi; \
+ fi; \
+ if [ "x$(shasum_command)" != "x" ]; then \
+ if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \
+ echo "Removing failed download"; rm -f $(nnuenet); \
+ else \
+ echo "Network validated"; break; \
+ fi; \
+ fi; \
+ done
+ @if ! test -f "$(nnuenet)"; then \
+ echo "Failed to download $(nnuenet)."; \
+ fi
+
+# clean binaries and objects
+objclean:
+ @rm -f stockfish stockfish.exe *.o ./syzygy/*.o ./nnue/*.o ./nnue/features/*.o
+
+# clean auxiliary profiling files
+profileclean:
+ @rm -rf profdir
+ @rm -f bench.txt *.gcda *.gcno ./syzygy/*.gcda ./nnue/*.gcda ./nnue/features/*.gcda *.s
+ @rm -f stockfish.profdata *.profraw
+ @rm -f stockfish.*args*
+ @rm -f stockfish.*lt*
+ @rm -f stockfish.res
+ @rm -f ./-lstdc++.res
+
+default:
+ help
+
+### ==========================================================================
+### Section 5. Private Targets
+### ==========================================================================
+
+all: $(EXE) .depend
+
+config-sanity: net
+ @echo ""
+ @echo "Config:"
+ @echo "debug: '$(debug)'"
+ @echo "sanitize: '$(sanitize)'"
+ @echo "optimize: '$(optimize)'"
+ @echo "arch: '$(arch)'"
+ @echo "bits: '$(bits)'"
+ @echo "kernel: '$(KERNEL)'"
+ @echo "os: '$(OS)'"
+ @echo "prefetch: '$(prefetch)'"
+ @echo "popcnt: '$(popcnt)'"
+ @echo "pext: '$(pext)'"
+ @echo "sse: '$(sse)'"
+ @echo "mmx: '$(mmx)'"
+ @echo "sse2: '$(sse2)'"
+ @echo "ssse3: '$(ssse3)'"
+ @echo "sse41: '$(sse41)'"
+ @echo "avx2: '$(avx2)'"
+ @echo "avxvnni: '$(avxvnni)'"
+ @echo "avx512: '$(avx512)'"
+ @echo "vnni256: '$(vnni256)'"
+ @echo "vnni512: '$(vnni512)'"
+ @echo "neon: '$(neon)'"
+ @echo "dotprod: '$(dotprod)'"
+ @echo "arm_version: '$(arm_version)'"
+ @echo "target_windows: '$(target_windows)'"
+ @echo ""
+ @echo "Flags:"
+ @echo "CXX: $(CXX)"
+ @echo "CXXFLAGS: $(CXXFLAGS)"
+ @echo "LDFLAGS: $(LDFLAGS)"
+ @echo ""
+ @echo "Testing config sanity. If this fails, try 'make help' ..."
+ @echo ""
+ @test "$(debug)" = "yes" || test "$(debug)" = "no"
+ @test "$(optimize)" = "yes" || test "$(optimize)" = "no"
+ @test "$(SUPPORTED_ARCH)" = "true"
+ @test "$(arch)" = "any" || test "$(arch)" = "x86_64" || test "$(arch)" = "i386" || \
+ test "$(arch)" = "ppc64" || test "$(arch)" = "ppc" || test "$(arch)" = "e2k" || \
+ test "$(arch)" = "armv7" || test "$(arch)" = "armv8" || test "$(arch)" = "arm64" || test "$(arch)" = "riscv64"
+ @test "$(bits)" = "32" || test "$(bits)" = "64"
+ @test "$(prefetch)" = "yes" || test "$(prefetch)" = "no"
+ @test "$(popcnt)" = "yes" || test "$(popcnt)" = "no"
+ @test "$(pext)" = "yes" || test "$(pext)" = "no"
+ @test "$(sse)" = "yes" || test "$(sse)" = "no"
+ @test "$(mmx)" = "yes" || test "$(mmx)" = "no"
+ @test "$(sse2)" = "yes" || test "$(sse2)" = "no"
+ @test "$(ssse3)" = "yes" || test "$(ssse3)" = "no"
+ @test "$(sse41)" = "yes" || test "$(sse41)" = "no"
+ @test "$(avx2)" = "yes" || test "$(avx2)" = "no"
+ @test "$(avx512)" = "yes" || test "$(avx512)" = "no"
+ @test "$(vnni256)" = "yes" || test "$(vnni256)" = "no"
+ @test "$(vnni512)" = "yes" || test "$(vnni512)" = "no"
+ @test "$(neon)" = "yes" || test "$(neon)" = "no"
+ @test "$(comp)" = "gcc" || test "$(comp)" = "icx" || test "$(comp)" = "mingw" || test "$(comp)" = "clang" \
+ || test "$(comp)" = "armv7a-linux-androideabi16-clang" || test "$(comp)" = "aarch64-linux-android21-clang"
+
+$(EXE): $(OBJS)
+ +$(CXX) -o $@ $(OBJS) $(LDFLAGS)
+
+# Force recompilation to ensure version info is up-to-date
+misc.o: FORCE
+FORCE:
+
+clang-profile-make:
+ $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
+ EXTRACXXFLAGS='-fprofile-instr-generate ' \
+ EXTRALDFLAGS=' -fprofile-instr-generate' \
+ all
+
+clang-profile-use:
+ $(XCRUN) llvm-profdata merge -output=stockfish.profdata *.profraw
+ $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
+ EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
+ EXTRALDFLAGS='-fprofile-use ' \
+ all
+
+gcc-profile-make:
+ @mkdir -p profdir
+ $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
+ EXTRACXXFLAGS='-fprofile-generate=profdir' \
+ EXTRACXXFLAGS+=$(EXTRAPROFILEFLAGS) \
+ EXTRALDFLAGS='-lgcov' \
+ all
+
+gcc-profile-use:
+ $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
+ EXTRACXXFLAGS='-fprofile-use=profdir -fno-peel-loops -fno-tracer' \
+ EXTRACXXFLAGS+=$(EXTRAPROFILEFLAGS) \
+ EXTRALDFLAGS='-lgcov' \
+ all
+
+icx-profile-make:
+ $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
+ EXTRACXXFLAGS='-fprofile-instr-generate ' \
+ EXTRALDFLAGS=' -fprofile-instr-generate' \
+ all
+
+icx-profile-use:
+ $(XCRUN) llvm-profdata merge -output=stockfish.profdata *.profraw
+ $(MAKE) ARCH=$(ARCH) COMP=$(COMP) \
+ EXTRACXXFLAGS='-fprofile-instr-use=stockfish.profdata' \
+ EXTRALDFLAGS='-fprofile-use ' \
+ all
+
+.depend: $(SRCS)
+ -@$(CXX) $(DEPENDFLAGS) -MM $(SRCS) > $@ 2> /dev/null
+
+ifeq (, $(filter $(MAKECMDGOALS), help strip install clean net objclean profileclean config-sanity))
+-include .depend
+endif
diff --git a/src/Stockfish/src/benchmark.cpp b/src/Stockfish/src/benchmark.cpp
new file mode 100644
index 0000000..a1ad055
--- /dev/null
+++ b/src/Stockfish/src/benchmark.cpp
@@ -0,0 +1,177 @@
+/*
+ Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+ Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
+
+ Stockfish is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Stockfish is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+*/
+
+#include "benchmark.h"
+
+#include
+#include
+#include
+#include
+
+#include "position.h"
+
+using namespace std;
+
+namespace {
+
+const vector Defaults = {
+ "setoption name UCI_Chess960 value false",
+ "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
+ "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 10",
+ "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 11",
+ "4rrk1/pp1n3p/3q2pQ/2p1pb2/2PP4/2P3N1/P2B2PP/4RRK1 b - - 7 19",
+ "rq3rk1/ppp2ppp/1bnpb3/3N2B1/3NP3/7P/PPPQ1PP1/2KR3R w - - 7 14 moves d4e6",
+ "r1bq1r1k/1pp1n1pp/1p1p4/4p2Q/4Pp2/1BNP4/PPP2PPP/3R1RK1 w - - 2 14 moves g2g4",
+ "r3r1k1/2p2ppp/p1p1bn2/8/1q2P3/2NPQN2/PPP3PP/R4RK1 b - - 2 15",
+ "r1bbk1nr/pp3p1p/2n5/1N4p1/2Np1B2/8/PPP2PPP/2KR1B1R w kq - 0 13",
+ "r1bq1rk1/ppp1nppp/4n3/3p3Q/3P4/1BP1B3/PP1N2PP/R4RK1 w - - 1 16",
+ "4r1k1/r1q2ppp/ppp2n2/4P3/5Rb1/1N1BQ3/PPP3PP/R5K1 w - - 1 17",
+ "2rqkb1r/ppp2p2/2npb1p1/1N1Nn2p/2P1PP2/8/PP2B1PP/R1BQK2R b KQ - 0 11",
+ "r1bq1r1k/b1p1npp1/p2p3p/1p6/3PP3/1B2NN2/PP3PPP/R2Q1RK1 w - - 1 16",
+ "3r1rk1/p5pp/bpp1pp2/8/q1PP1P2/b3P3/P2NQRPP/1R2B1K1 b - - 6 22",
+ "r1q2rk1/2p1bppp/2Pp4/p6b/Q1PNp3/4B3/PP1R1PPP/2K4R w - - 2 18",
+ "4k2r/1pb2ppp/1p2p3/1R1p4/3P4/2r1PN2/P4PPP/1R4K1 b - - 3 22",
+ "3q2k1/pb3p1p/4pbp1/2r5/PpN2N2/1P2P2P/5PP1/Q2R2K1 b - - 4 26",
+ "6k1/6p1/6Pp/ppp5/3pn2P/1P3K2/1PP2P2/3N4 b - - 0 1",
+ "3b4/5kp1/1p1p1p1p/pP1PpP1P/P1P1P3/3KN3/8/8 w - - 0 1",
+ "2K5/p7/7P/5pR1/8/5k2/r7/8 w - - 0 1 moves g5g6 f3e3 g6g5 e3f3",
+ "8/6pk/1p6/8/PP3p1p/5P2/4KP1q/3Q4 w - - 0 1",
+ "7k/3p2pp/4q3/8/4Q3/5Kp1/P6b/8 w - - 0 1",
+ "8/2p5/8/2kPKp1p/2p4P/2P5/3P4/8 w - - 0 1",
+ "8/1p3pp1/7p/5P1P/2k3P1/8/2K2P2/8 w - - 0 1",
+ "8/pp2r1k1/2p1p3/3pP2p/1P1P1P1P/P5KR/8/8 w - - 0 1",
+ "8/3p4/p1bk3p/Pp6/1Kp1PpPp/2P2P1P/2P5/5B2 b - - 0 1",
+ "5k2/7R/4P2p/5K2/p1r2P1p/8/8/8 b - - 0 1",
+ "6k1/6p1/P6p/r1N5/5p2/7P/1b3PP1/4R1K1 w - - 0 1",
+ "1r3k2/4q3/2Pp3b/3Bp3/2Q2p2/1p1P2P1/1P2KP2/3N4 w - - 0 1",
+ "6k1/4pp1p/3p2p1/P1pPb3/R7/1r2P1PP/3B1P2/6K1 w - - 0 1",
+ "8/3p3B/5p2/5P2/p7/PP5b/k7/6K1 w - - 0 1",
+ "5rk1/q6p/2p3bR/1pPp1rP1/1P1Pp3/P3B1Q1/1K3P2/R7 w - - 93 90",
+ "4rrk1/1p1nq3/p7/2p1P1pp/3P2bp/3Q1Bn1/PPPB4/1K2R1NR w - - 40 21",
+ "r3k2r/3nnpbp/q2pp1p1/p7/Pp1PPPP1/4BNN1/1P5P/R2Q1RK1 w kq - 0 16",
+ "3Qb1k1/1r2ppb1/pN1n2q1/Pp1Pp1Pr/4P2p/4BP2/4B1R1/1R5K b - - 11 40",
+ "4k3/3q1r2/1N2r1b1/3ppN2/2nPP3/1B1R2n1/2R1Q3/3K4 w - - 5 1",
+
+ // 5-man positions
+ "8/8/8/8/5kp1/P7/8/1K1N4 w - - 0 1", // Kc2 - mate
+ "8/8/8/5N2/8/p7/8/2NK3k w - - 0 1", // Na2 - mate
+ "8/3k4/8/8/8/4B3/4KB2/2B5 w - - 0 1", // draw
+
+ // 6-man positions
+ "8/8/1P6/5pr1/8/4R3/7k/2K5 w - - 0 1", // Re5 - mate
+ "8/2p4P/8/kr6/6R1/8/8/1K6 w - - 0 1", // Ka2 - mate
+ "8/8/3P3k/8/1p6/8/1P6/1K3n2 b - - 0 1", // Nd2 - draw
+
+ // 7-man positions
+ "8/R7/2q5/8/6k1/8/1P5p/K6R w - - 0 124", // Draw
+
+ // Mate and stalemate positions
+ "6k1/3b3r/1p1p4/p1n2p2/1PPNpP1q/P3Q1p1/1R1RB1P1/5K2 b - - 0 1",
+ "r2r1n2/pp2bk2/2p1p2p/3q4/3PN1QP/2P3R1/P4PP1/5RK1 w - - 0 1",
+ "8/8/8/8/8/6k1/6p1/6K1 w - -",
+ "7k/7P/6K1/8/3B4/8/8/8 b - -",
+
+ // Chess 960
+ "setoption name UCI_Chess960 value true",
+ "bbqnnrkr/pppppppp/8/8/8/8/PPPPPPPP/BBQNNRKR w HFhf - 0 1 moves g2g3 d7d5 d2d4 c8h3 c1g5 e8d6 g5e7 f7f6",
+ "nqbnrkrb/pppppppp/8/8/8/8/PPPPPPPP/NQBNRKRB w KQkq - 0 1",
+ "setoption name UCI_Chess960 value false"
+};
+
+} // namespace
+
+namespace Stockfish {
+
+/// setup_bench() builds a list of UCI commands to be run by bench. There
+/// are five parameters: TT size in MB, number of search threads that
+/// should be used, the limit value spent for each position, a file name
+/// where to look for positions in FEN format, the type of the limit:
+/// depth, perft, nodes and movetime (in millisecs), and evaluation type
+/// mixed (default), classical, NNUE.
+///
+/// bench -> search default positions up to depth 13
+/// bench 64 1 15 -> search default positions up to depth 15 (TT = 64MB)
+/// bench 64 4 5000 current movetime -> search current position with 4 threads for 5 sec
+/// bench 64 1 100000 default nodes -> search default positions for 100K nodes each
+/// bench 16 1 5 default perft -> run a perft 5 on default positions
+
+vector setup_bench(const Position& current, istream& is) {
+
+ vector fens, list;
+ string go, token;
+
+ // Assign default values to missing arguments
+ string ttSize = (is >> token) ? token : "16";
+ string threads = (is >> token) ? token : "1";
+ string limit = (is >> token) ? token : "13";
+ string fenFile = (is >> token) ? token : "default";
+ string limitType = (is >> token) ? token : "depth";
+ string evalType = (is >> token) ? token : "mixed";
+
+ go = limitType == "eval" ? "eval" : "go " + limitType + " " + limit;
+
+ if (fenFile == "default")
+ fens = Defaults;
+
+ else if (fenFile == "current")
+ fens.push_back(current.fen());
+
+ else
+ {
+ string fen;
+ ifstream file(fenFile);
+
+ if (!file.is_open())
+ {
+ cerr << "Unable to open file " << fenFile << endl;
+ exit(EXIT_FAILURE);
+ }
+
+ while (getline(file, fen))
+ if (!fen.empty())
+ fens.push_back(fen);
+
+ file.close();
+ }
+
+ list.emplace_back("setoption name Threads value " + threads);
+ list.emplace_back("setoption name Hash value " + ttSize);
+ list.emplace_back("ucinewgame");
+
+ size_t posCounter = 0;
+
+ for (const string& fen : fens)
+ if (fen.find("setoption") != string::npos)
+ list.emplace_back(fen);
+ else
+ {
+ if (evalType == "classical" || (evalType == "mixed" && posCounter % 2 == 0))
+ list.emplace_back("setoption name Use NNUE value false");
+ else if (evalType == "NNUE" || (evalType == "mixed" && posCounter % 2 != 0))
+ list.emplace_back("setoption name Use NNUE value true");
+ list.emplace_back("position fen " + fen);
+ list.emplace_back(go);
+ ++posCounter;
+ }
+
+ list.emplace_back("setoption name Use NNUE value true");
+
+ return list;
+}
+
+} // namespace Stockfish
diff --git a/src/Stockfish/src/benchmark.h b/src/Stockfish/src/benchmark.h
new file mode 100644
index 0000000..64acf83
--- /dev/null
+++ b/src/Stockfish/src/benchmark.h
@@ -0,0 +1,34 @@
+/*
+ Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+ Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
+
+ Stockfish is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Stockfish is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+*/
+
+#ifndef BENCHMARK_H_INCLUDED
+#define BENCHMARK_H_INCLUDED
+
+#include
+#include
+#include
+
+namespace Stockfish {
+
+class Position;
+
+std::vector setup_bench(const Position&, std::istream&);
+
+} // namespace Stockfish
+
+#endif // #ifndef BENCHMARK_H_INCLUDED
diff --git a/src/Stockfish/src/bitbase.cpp b/src/Stockfish/src/bitbase.cpp
new file mode 100644
index 0000000..e21d1fe
--- /dev/null
+++ b/src/Stockfish/src/bitbase.cpp
@@ -0,0 +1,172 @@
+/*
+ Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+ Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
+
+ Stockfish is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Stockfish is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+*/
+
+#include
+#include
+#include
+
+#include "bitboard.h"
+#include "types.h"
+
+namespace Stockfish {
+
+namespace {
+
+ // There are 24 possible pawn squares: files A to D and ranks from 2 to 7.
+ // Positions with the pawn on files E to H will be mirrored before probing.
+ constexpr unsigned MAX_INDEX = 2*24*64*64; // stm * psq * wksq * bksq = 196608
+
+ std::bitset KPKBitbase;
+
+ // A KPK bitbase index is an integer in [0, IndexMax] range
+ //
+ // Information is mapped in a way that minimizes the number of iterations:
+ //
+ // bit 0- 5: white king square (from SQ_A1 to SQ_H8)
+ // bit 6-11: black king square (from SQ_A1 to SQ_H8)
+ // bit 12: side to move (WHITE or BLACK)
+ // bit 13-14: white pawn file (from FILE_A to FILE_D)
+ // bit 15-17: white pawn RANK_7 - rank (from RANK_7 - RANK_7 to RANK_7 - RANK_2)
+ unsigned index(Color stm, Square bksq, Square wksq, Square psq) {
+ return int(wksq) | (bksq << 6) | (stm << 12) | (file_of(psq) << 13) | ((RANK_7 - rank_of(psq)) << 15);
+ }
+
+ enum Result {
+ INVALID = 0,
+ UNKNOWN = 1,
+ DRAW = 2,
+ WIN = 4
+ };
+
+ Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
+
+ struct KPKPosition {
+ KPKPosition() = default;
+ explicit KPKPosition(unsigned idx);
+ operator Result() const { return result; }
+ Result classify(const std::vector& db);
+
+ Color stm;
+ Square ksq[COLOR_NB], psq;
+ Result result;
+ };
+
+} // namespace
+
+bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color stm) {
+
+ assert(file_of(wpsq) <= FILE_D);
+
+ return KPKBitbase[index(stm, bksq, wksq, wpsq)];
+}
+
+
+void Bitbases::init() {
+
+ std::vector db(MAX_INDEX);
+ unsigned idx, repeat = 1;
+
+ // Initialize db with known win / draw positions
+ for (idx = 0; idx < MAX_INDEX; ++idx)
+ db[idx] = KPKPosition(idx);
+
+ // Iterate through the positions until none of the unknown positions can be
+ // changed to either wins or draws (15 cycles needed).
+ while (repeat)
+ for (repeat = idx = 0; idx < MAX_INDEX; ++idx)
+ repeat |= (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN);
+
+ // Fill the bitbase with the decisive results
+ for (idx = 0; idx < MAX_INDEX; ++idx)
+ if (db[idx] == WIN)
+ KPKBitbase.set(idx);
+}
+
+namespace {
+
+ KPKPosition::KPKPosition(unsigned idx) {
+
+ ksq[WHITE] = Square((idx >> 0) & 0x3F);
+ ksq[BLACK] = Square((idx >> 6) & 0x3F);
+ stm = Color ((idx >> 12) & 0x01);
+ psq = make_square(File((idx >> 13) & 0x3), Rank(RANK_7 - ((idx >> 15) & 0x7)));
+
+ // Invalid if two pieces are on the same square or if a king can be captured
+ if ( distance(ksq[WHITE], ksq[BLACK]) <= 1
+ || ksq[WHITE] == psq
+ || ksq[BLACK] == psq
+ || (stm == WHITE && (pawn_attacks_bb(WHITE, psq) & ksq[BLACK])))
+ result = INVALID;
+
+ // Win if the pawn can be promoted without getting captured
+ else if ( stm == WHITE
+ && rank_of(psq) == RANK_7
+ && ksq[WHITE] != psq + NORTH
+ && ( distance(ksq[BLACK], psq + NORTH) > 1
+ || (distance(ksq[WHITE], psq + NORTH) == 1)))
+ result = WIN;
+
+ // Draw if it is stalemate or the black king can capture the pawn
+ else if ( stm == BLACK
+ && ( !(attacks_bb(ksq[BLACK]) & ~(attacks_bb(ksq[WHITE]) | pawn_attacks_bb(WHITE, psq)))
+ || (attacks_bb(ksq[BLACK]) & ~attacks_bb(ksq[WHITE]) & psq)))
+ result = DRAW;
+
+ // Position will be classified later
+ else
+ result = UNKNOWN;
+ }
+
+ Result KPKPosition::classify(const std::vector& db) {
+
+ // White to move: If one move leads to a position classified as WIN, the result
+ // of the current position is WIN. If all moves lead to positions classified
+ // as DRAW, the current position is classified as DRAW, otherwise the current
+ // position is classified as UNKNOWN.
+ //
+ // Black to move: If one move leads to a position classified as DRAW, the result
+ // of the current position is DRAW. If all moves lead to positions classified
+ // as WIN, the position is classified as WIN, otherwise the current position is
+ // classified as UNKNOWN.
+ const Result Good = (stm == WHITE ? WIN : DRAW);
+ const Result Bad = (stm == WHITE ? DRAW : WIN);
+
+ Result r = INVALID;
+ Bitboard b = attacks_bb(ksq[stm]);
+
+ while (b)
+ r |= stm == WHITE ? db[index(BLACK, ksq[BLACK], pop_lsb(b), psq)]
+ : db[index(WHITE, pop_lsb(b), ksq[WHITE], psq)];
+
+ if (stm == WHITE)
+ {
+ if (rank_of(psq) < RANK_7) // Single push
+ r |= db[index(BLACK, ksq[BLACK], ksq[WHITE], psq + NORTH)];
+
+ if ( rank_of(psq) == RANK_2 // Double push
+ && psq + NORTH != ksq[WHITE]
+ && psq + NORTH != ksq[BLACK])
+ r |= db[index(BLACK, ksq[BLACK], ksq[WHITE], psq + NORTH + NORTH)];
+ }
+
+ return result = r & Good ? Good : r & UNKNOWN ? UNKNOWN : Bad;
+ }
+
+} // namespace
+
+} // namespace Stockfish
diff --git a/src/Stockfish/src/bitboard.cpp b/src/Stockfish/src/bitboard.cpp
new file mode 100644
index 0000000..fd5c3c2
--- /dev/null
+++ b/src/Stockfish/src/bitboard.cpp
@@ -0,0 +1,218 @@
+/*
+ Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+ Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
+
+ Stockfish is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Stockfish is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+*/
+
+#include
+#include
+
+#include "bitboard.h"
+#include "misc.h"
+
+namespace Stockfish {
+
+uint8_t PopCnt16[1 << 16];
+uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
+
+Bitboard LineBB[SQUARE_NB][SQUARE_NB];
+Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
+Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
+Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
+
+Magic RookMagics[SQUARE_NB];
+Magic BishopMagics[SQUARE_NB];
+
+namespace {
+
+ Bitboard RookTable[0x19000]; // To store rook attacks
+ Bitboard BishopTable[0x1480]; // To store bishop attacks
+
+ void init_magics(PieceType pt, Bitboard table[], Magic magics[]);
+
+}
+
+/// safe_destination() returns the bitboard of target square for the given step
+/// from the given square. If the step is off the board, returns empty bitboard.
+
+inline Bitboard safe_destination(Square s, int step) {
+ Square to = Square(s + step);
+ return is_ok(to) && distance(s, to) <= 2 ? square_bb(to) : Bitboard(0);
+}
+
+
+/// Bitboards::pretty() returns an ASCII representation of a bitboard suitable
+/// to be printed to standard output. Useful for debugging.
+
+std::string Bitboards::pretty(Bitboard b) {
+
+ std::string s = "+---+---+---+---+---+---+---+---+\n";
+
+ for (Rank r = RANK_8; r >= RANK_1; --r)
+ {
+ for (File f = FILE_A; f <= FILE_H; ++f)
+ s += b & make_square(f, r) ? "| X " : "| ";
+
+ s += "| " + std::to_string(1 + r) + "\n+---+---+---+---+---+---+---+---+\n";
+ }
+ s += " a b c d e f g h\n";
+
+ return s;
+}
+
+
+/// Bitboards::init() initializes various bitboard tables. It is called at
+/// startup and relies on global objects to be already zero-initialized.
+
+void Bitboards::init() {
+
+ for (unsigned i = 0; i < (1 << 16); ++i)
+ PopCnt16[i] = uint8_t(std::bitset<16>(i).count());
+
+ for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
+ for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
+ SquareDistance[s1][s2] = std::max(distance(s1, s2), distance(s1, s2));
+
+ init_magics(ROOK, RookTable, RookMagics);
+ init_magics(BISHOP, BishopTable, BishopMagics);
+
+ for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
+ {
+ PawnAttacks[WHITE][s1] = pawn_attacks_bb(square_bb(s1));
+ PawnAttacks[BLACK][s1] = pawn_attacks_bb(square_bb(s1));
+
+ for (int step : {-9, -8, -7, -1, 1, 7, 8, 9} )
+ PseudoAttacks[KING][s1] |= safe_destination(s1, step);
+
+ for (int step : {-17, -15, -10, -6, 6, 10, 15, 17} )
+ PseudoAttacks[KNIGHT][s1] |= safe_destination(s1, step);
+
+ PseudoAttacks[QUEEN][s1] = PseudoAttacks[BISHOP][s1] = attacks_bb(s1, 0);
+ PseudoAttacks[QUEEN][s1] |= PseudoAttacks[ ROOK][s1] = attacks_bb< ROOK>(s1, 0);
+
+ for (PieceType pt : { BISHOP, ROOK })
+ for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
+ {
+ if (PseudoAttacks[pt][s1] & s2)
+ {
+ LineBB[s1][s2] = (attacks_bb(pt, s1, 0) & attacks_bb(pt, s2, 0)) | s1 | s2;
+ BetweenBB[s1][s2] = (attacks_bb(pt, s1, square_bb(s2)) & attacks_bb(pt, s2, square_bb(s1)));
+ }
+ BetweenBB[s1][s2] |= s2;
+ }
+ }
+}
+
+namespace {
+
+ Bitboard sliding_attack(PieceType pt, Square sq, Bitboard occupied) {
+
+ Bitboard attacks = 0;
+ Direction RookDirections[4] = {NORTH, SOUTH, EAST, WEST};
+ Direction BishopDirections[4] = {NORTH_EAST, SOUTH_EAST, SOUTH_WEST, NORTH_WEST};
+
+ for (Direction d : (pt == ROOK ? RookDirections : BishopDirections))
+ {
+ Square s = sq;
+ while (safe_destination(s, d) && !(occupied & s))
+ attacks |= (s += d);
+ }
+
+ return attacks;
+ }
+
+
+ // init_magics() computes all rook and bishop attacks at startup. Magic
+ // bitboards are used to look up attacks of sliding pieces. As a reference see
+ // www.chessprogramming.org/Magic_Bitboards. In particular, here we use the so
+ // called "fancy" approach.
+
+ void init_magics(PieceType pt, Bitboard table[], Magic magics[]) {
+
+ // Optimal PRNG seeds to pick the correct magics in the shortest time
+ int seeds[][RANK_NB] = { { 8977, 44560, 54343, 38998, 5731, 95205, 104912, 17020 },
+ { 728, 10316, 55013, 32803, 12281, 15100, 16645, 255 } };
+
+ Bitboard occupancy[4096], reference[4096], edges, b;
+ int epoch[4096] = {}, cnt = 0, size = 0;
+
+ for (Square s = SQ_A1; s <= SQ_H8; ++s)
+ {
+ // Board edges are not considered in the relevant occupancies
+ edges = ((Rank1BB | Rank8BB) & ~rank_bb(s)) | ((FileABB | FileHBB) & ~file_bb(s));
+
+ // Given a square 's', the mask is the bitboard of sliding attacks from
+ // 's' computed on an empty board. The index must be big enough to contain
+ // all the attacks for each possible subset of the mask and so is 2 power
+ // the number of 1s of the mask. Hence we deduce the size of the shift to
+ // apply to the 64 or 32 bits word to get the index.
+ Magic& m = magics[s];
+ m.mask = sliding_attack(pt, s, 0) & ~edges;
+ m.shift = (Is64Bit ? 64 : 32) - popcount(m.mask);
+
+ // Set the offset for the attacks table of the square. We have individual
+ // table sizes for each square with "Fancy Magic Bitboards".
+ m.attacks = s == SQ_A1 ? table : magics[s - 1].attacks + size;
+
+ // Use Carry-Rippler trick to enumerate all subsets of masks[s] and
+ // store the corresponding sliding attack bitboard in reference[].
+ b = size = 0;
+ do {
+ occupancy[size] = b;
+ reference[size] = sliding_attack(pt, s, b);
+
+ if (HasPext)
+ m.attacks[pext(b, m.mask)] = reference[size];
+
+ size++;
+ b = (b - m.mask) & m.mask;
+ } while (b);
+
+ if (HasPext)
+ continue;
+
+ PRNG rng(seeds[Is64Bit][rank_of(s)]);
+
+ // Find a magic for square 's' picking up an (almost) random number
+ // until we find the one that passes the verification test.
+ for (int i = 0; i < size; )
+ {
+ for (m.magic = 0; popcount((m.magic * m.mask) >> 56) < 6; )
+ m.magic = rng.sparse_rand();
+
+ // A good magic must map every possible occupancy to an index that
+ // looks up the correct sliding attack in the attacks[s] database.
+ // Note that we build up the database for square 's' as a side
+ // effect of verifying the magic. Keep track of the attempt count
+ // and save it in epoch[], little speed-up trick to avoid resetting
+ // m.attacks[] after every failed attempt.
+ for (++cnt, i = 0; i < size; ++i)
+ {
+ unsigned idx = m.index(occupancy[i]);
+
+ if (epoch[idx] < cnt)
+ {
+ epoch[idx] = cnt;
+ m.attacks[idx] = reference[i];
+ }
+ else if (m.attacks[idx] != reference[i])
+ break;
+ }
+ }
+ }
+ }
+}
+
+} // namespace Stockfish
diff --git a/src/Stockfish/src/bitboard.h b/src/Stockfish/src/bitboard.h
new file mode 100644
index 0000000..42fd0e9
--- /dev/null
+++ b/src/Stockfish/src/bitboard.h
@@ -0,0 +1,450 @@
+/*
+ Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+ Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
+
+ Stockfish is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Stockfish is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+*/
+
+#ifndef BITBOARD_H_INCLUDED
+#define BITBOARD_H_INCLUDED
+
+#include
+
+#include "types.h"
+
+namespace Stockfish {
+
+namespace Bitbases {
+
+void init();
+bool probe(Square wksq, Square wpsq, Square bksq, Color us);
+
+} // namespace Stockfish::Bitbases
+
+namespace Bitboards {
+
+void init();
+std::string pretty(Bitboard b);
+
+} // namespace Stockfish::Bitboards
+
+constexpr Bitboard AllSquares = ~Bitboard(0);
+constexpr Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL;
+
+constexpr Bitboard FileABB = 0x0101010101010101ULL;
+constexpr Bitboard FileBBB = FileABB << 1;
+constexpr Bitboard FileCBB = FileABB << 2;
+constexpr Bitboard FileDBB = FileABB << 3;
+constexpr Bitboard FileEBB = FileABB << 4;
+constexpr Bitboard FileFBB = FileABB << 5;
+constexpr Bitboard FileGBB = FileABB << 6;
+constexpr Bitboard FileHBB = FileABB << 7;
+
+constexpr Bitboard Rank1BB = 0xFF;
+constexpr Bitboard Rank2BB = Rank1BB << (8 * 1);
+constexpr Bitboard Rank3BB = Rank1BB << (8 * 2);
+constexpr Bitboard Rank4BB = Rank1BB << (8 * 3);
+constexpr Bitboard Rank5BB = Rank1BB << (8 * 4);
+constexpr Bitboard Rank6BB = Rank1BB << (8 * 5);
+constexpr Bitboard Rank7BB = Rank1BB << (8 * 6);
+constexpr Bitboard Rank8BB = Rank1BB << (8 * 7);
+
+constexpr Bitboard QueenSide = FileABB | FileBBB | FileCBB | FileDBB;
+constexpr Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB;
+constexpr Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB;
+constexpr Bitboard Center = (FileDBB | FileEBB) & (Rank4BB | Rank5BB);
+
+constexpr Bitboard KingFlank[FILE_NB] = {
+ QueenSide ^ FileDBB, QueenSide, QueenSide,
+ CenterFiles, CenterFiles,
+ KingSide, KingSide, KingSide ^ FileEBB
+};
+
+extern uint8_t PopCnt16[1 << 16];
+extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB];
+
+extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
+extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
+extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
+extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB];
+
+
+/// Magic holds all magic bitboards relevant data for a single square
+struct Magic {
+ Bitboard mask;
+ Bitboard magic;
+ Bitboard* attacks;
+ unsigned shift;
+
+ // Compute the attack's index using the 'magic bitboards' approach
+ unsigned index(Bitboard occupied) const {
+
+ if (HasPext)
+ return unsigned(pext(occupied, mask));
+
+ if (Is64Bit)
+ return unsigned(((occupied & mask) * magic) >> shift);
+
+ unsigned lo = unsigned(occupied) & unsigned(mask);
+ unsigned hi = unsigned(occupied >> 32) & unsigned(mask >> 32);
+ return (lo * unsigned(magic) ^ hi * unsigned(magic >> 32)) >> shift;
+ }
+};
+
+extern Magic RookMagics[SQUARE_NB];
+extern Magic BishopMagics[SQUARE_NB];
+
+inline Bitboard square_bb(Square s) {
+ assert(is_ok(s));
+ return (1ULL << s);
+}
+
+
+/// Overloads of bitwise operators between a Bitboard and a Square for testing
+/// whether a given bit is set in a bitboard, and for setting and clearing bits.
+
+inline Bitboard operator&( Bitboard b, Square s) { return b & square_bb(s); }
+inline Bitboard operator|( Bitboard b, Square s) { return b | square_bb(s); }
+inline Bitboard operator^( Bitboard b, Square s) { return b ^ square_bb(s); }
+inline Bitboard& operator|=(Bitboard& b, Square s) { return b |= square_bb(s); }
+inline Bitboard& operator^=(Bitboard& b, Square s) { return b ^= square_bb(s); }
+
+inline Bitboard operator&(Square s, Bitboard b) { return b & s; }
+inline Bitboard operator|(Square s, Bitboard b) { return b | s; }
+inline Bitboard operator^(Square s, Bitboard b) { return b ^ s; }
+
+inline Bitboard operator|(Square s1, Square s2) { return square_bb(s1) | s2; }
+
+constexpr bool more_than_one(Bitboard b) {
+ return b & (b - 1);
+}
+
+
+constexpr bool opposite_colors(Square s1, Square s2) {
+ return (s1 + rank_of(s1) + s2 + rank_of(s2)) & 1;
+}
+
+
+/// rank_bb() and file_bb() return a bitboard representing all the squares on
+/// the given file or rank.
+
+constexpr Bitboard rank_bb(Rank r) {
+ return Rank1BB << (8 * r);
+}
+
+constexpr Bitboard rank_bb(Square s) {
+ return rank_bb(rank_of(s));
+}
+
+constexpr Bitboard file_bb(File f) {
+ return FileABB << f;
+}
+
+constexpr Bitboard file_bb(Square s) {
+ return file_bb(file_of(s));
+}
+
+
+/// shift() moves a bitboard one or two steps as specified by the direction D
+
+template
+constexpr Bitboard shift(Bitboard b) {
+ return D == NORTH ? b << 8 : D == SOUTH ? b >> 8
+ : D == NORTH+NORTH? b <<16 : D == SOUTH+SOUTH? b >>16
+ : D == EAST ? (b & ~FileHBB) << 1 : D == WEST ? (b & ~FileABB) >> 1
+ : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7
+ : D == SOUTH_EAST ? (b & ~FileHBB) >> 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9
+ : 0;
+}
+
+
+/// pawn_attacks_bb() returns the squares attacked by pawns of the given color
+/// from the squares in the given bitboard.
+
+template
+constexpr Bitboard pawn_attacks_bb(Bitboard b) {
+ return C == WHITE ? shift(b) | shift(b)
+ : shift(b) | shift(b);
+}
+
+inline Bitboard pawn_attacks_bb(Color c, Square s) {
+
+ assert(is_ok(s));
+ return PawnAttacks[c][s];
+}
+
+
+/// pawn_double_attacks_bb() returns the squares doubly attacked by pawns of the
+/// given color from the squares in the given bitboard.
+
+template
+constexpr Bitboard pawn_double_attacks_bb(Bitboard b) {
+ return C == WHITE ? shift(b) & shift(b)
+ : shift(b) & shift(b);
+}
+
+
+/// adjacent_files_bb() returns a bitboard representing all the squares on the
+/// adjacent files of a given square.
+
+constexpr Bitboard adjacent_files_bb(Square s) {
+ return shift(file_bb(s)) | shift(file_bb(s));
+}
+
+
+/// line_bb() returns a bitboard representing an entire line (from board edge
+/// to board edge) that intersects the two given squares. If the given squares
+/// are not on a same file/rank/diagonal, the function returns 0. For instance,
+/// line_bb(SQ_C4, SQ_F7) will return a bitboard with the A2-G8 diagonal.
+
+inline Bitboard line_bb(Square s1, Square s2) {
+
+ assert(is_ok(s1) && is_ok(s2));
+
+ return LineBB[s1][s2];
+}
+
+
+/// between_bb(s1, s2) returns a bitboard representing the squares in the semi-open
+/// segment between the squares s1 and s2 (excluding s1 but including s2). If the
+/// given squares are not on a same file/rank/diagonal, it returns s2. For instance,
+/// between_bb(SQ_C4, SQ_F7) will return a bitboard with squares D5, E6 and F7, but
+/// between_bb(SQ_E6, SQ_F8) will return a bitboard with the square F8. This trick
+/// allows to generate non-king evasion moves faster: the defending piece must either
+/// interpose itself to cover the check or capture the checking piece.
+
+inline Bitboard between_bb(Square s1, Square s2) {
+
+ assert(is_ok(s1) && is_ok(s2));
+
+ return BetweenBB[s1][s2];
+}
+
+
+/// forward_ranks_bb() returns a bitboard representing the squares on the ranks in
+/// front of the given one, from the point of view of the given color. For instance,
+/// forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.
+
+constexpr Bitboard forward_ranks_bb(Color c, Square s) {
+ return c == WHITE ? ~Rank1BB << 8 * relative_rank(WHITE, s)
+ : ~Rank8BB >> 8 * relative_rank(BLACK, s);
+}
+
+
+/// forward_file_bb() returns a bitboard representing all the squares along the
+/// line in front of the given one, from the point of view of the given color.
+
+constexpr Bitboard forward_file_bb(Color c, Square s) {
+ return forward_ranks_bb(c, s) & file_bb(s);
+}
+
+
+/// pawn_attack_span() returns a bitboard representing all the squares that can
+/// be attacked by a pawn of the given color when it moves along its file, starting
+/// from the given square.
+
+constexpr Bitboard pawn_attack_span(Color c, Square s) {
+ return forward_ranks_bb(c, s) & adjacent_files_bb(s);
+}
+
+
+/// passed_pawn_span() returns a bitboard which can be used to test if a pawn of
+/// the given color and on the given square is a passed pawn.
+
+constexpr Bitboard passed_pawn_span(Color c, Square s) {
+ return pawn_attack_span(c, s) | forward_file_bb(c, s);
+}
+
+
+/// aligned() returns true if the squares s1, s2 and s3 are aligned either on a
+/// straight or on a diagonal line.
+
+inline bool aligned(Square s1, Square s2, Square s3) {
+ return line_bb(s1, s2) & s3;
+}
+
+
+/// distance() functions return the distance between x and y, defined as the
+/// number of steps for a king in x to reach y.
+
+template inline int distance(Square x, Square y);
+template<> inline int distance(Square x, Square y) { return std::abs(file_of(x) - file_of(y)); }
+template<> inline int distance(Square x, Square y) { return std::abs(rank_of(x) - rank_of(y)); }
+template<> inline int distance(Square x, Square y) { return SquareDistance[x][y]; }
+
+inline int edge_distance(File f) { return std::min(f, File(FILE_H - f)); }
+inline int edge_distance(Rank r) { return std::min(r, Rank(RANK_8 - r)); }
+
+
+/// attacks_bb(Square) returns the pseudo attacks of the give piece type
+/// assuming an empty board.
+
+template
+inline Bitboard attacks_bb(Square s) {
+
+ assert((Pt != PAWN) && (is_ok(s)));
+
+ return PseudoAttacks[Pt][s];
+}
+
+
+/// attacks_bb(Square, Bitboard) returns the attacks by the given piece
+/// assuming the board is occupied according to the passed Bitboard.
+/// Sliding piece attacks do not continue passed an occupied square.
+
+template
+inline Bitboard attacks_bb(Square s, Bitboard occupied) {
+
+ assert((Pt != PAWN) && (is_ok(s)));
+
+ switch (Pt)
+ {
+ case BISHOP: return BishopMagics[s].attacks[BishopMagics[s].index(occupied)];
+ case ROOK : return RookMagics[s].attacks[ RookMagics[s].index(occupied)];
+ case QUEEN : return attacks_bb(s, occupied) | attacks_bb(s, occupied);
+ default : return PseudoAttacks[Pt][s];
+ }
+}
+
+inline Bitboard attacks_bb(PieceType pt, Square s, Bitboard occupied) {
+
+ assert((pt != PAWN) && (is_ok(s)));
+
+ switch (pt)
+ {
+ case BISHOP: return attacks_bb(s, occupied);
+ case ROOK : return attacks_bb< ROOK>(s, occupied);
+ case QUEEN : return attacks_bb(s, occupied) | attacks_bb(s, occupied);
+ default : return PseudoAttacks[pt][s];
+ }
+}
+
+
+/// popcount() counts the number of non-zero bits in a bitboard
+
+inline int popcount(Bitboard b) {
+
+#ifndef USE_POPCNT
+
+ union { Bitboard bb; uint16_t u[4]; } v = { b };
+ return PopCnt16[v.u[0]] + PopCnt16[v.u[1]] + PopCnt16[v.u[2]] + PopCnt16[v.u[3]];
+
+#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
+
+ return (int)_mm_popcnt_u64(b);
+
+#else // Assumed gcc or compatible compiler
+
+ return __builtin_popcountll(b);
+
+#endif
+}
+
+
+/// lsb() and msb() return the least/most significant bit in a non-zero bitboard
+
+#if defined(__GNUC__) // GCC, Clang, ICC
+
+inline Square lsb(Bitboard b) {
+ assert(b);
+ return Square(__builtin_ctzll(b));
+}
+
+inline Square msb(Bitboard b) {
+ assert(b);
+ return Square(63 ^ __builtin_clzll(b));
+}
+
+#elif defined(_MSC_VER) // MSVC
+
+#ifdef _WIN64 // MSVC, WIN64
+
+inline Square lsb(Bitboard b) {
+ assert(b);
+ unsigned long idx;
+ _BitScanForward64(&idx, b);
+ return (Square) idx;
+}
+
+inline Square msb(Bitboard b) {
+ assert(b);
+ unsigned long idx;
+ _BitScanReverse64(&idx, b);
+ return (Square) idx;
+}
+
+#else // MSVC, WIN32
+
+inline Square lsb(Bitboard b) {
+ assert(b);
+ unsigned long idx;
+
+ if (b & 0xffffffff) {
+ _BitScanForward(&idx, int32_t(b));
+ return Square(idx);
+ } else {
+ _BitScanForward(&idx, int32_t(b >> 32));
+ return Square(idx + 32);
+ }
+}
+
+inline Square msb(Bitboard b) {
+ assert(b);
+ unsigned long idx;
+
+ if (b >> 32) {
+ _BitScanReverse(&idx, int32_t(b >> 32));
+ return Square(idx + 32);
+ } else {
+ _BitScanReverse(&idx, int32_t(b));
+ return Square(idx);
+ }
+}
+
+#endif
+
+#else // Compiler is neither GCC nor MSVC compatible
+
+#error "Compiler not supported."
+
+#endif
+
+/// least_significant_square_bb() returns the bitboard of the least significant
+/// square of a non-zero bitboard. It is equivalent to square_bb(lsb(bb)).
+
+inline Bitboard least_significant_square_bb(Bitboard b) {
+ assert(b);
+ return b & -b;
+}
+
+/// pop_lsb() finds and clears the least significant bit in a non-zero bitboard
+
+inline Square pop_lsb(Bitboard& b) {
+ assert(b);
+ const Square s = lsb(b);
+ b &= b - 1;
+ return s;
+}
+
+
+/// frontmost_sq() returns the most advanced square for the given color,
+/// requires a non-zero bitboard.
+inline Square frontmost_sq(Color c, Bitboard b) {
+ assert(b);
+ return c == WHITE ? msb(b) : lsb(b);
+}
+
+} // namespace Stockfish
+
+#endif // #ifndef BITBOARD_H_INCLUDED
diff --git a/src/Stockfish/src/endgame.cpp b/src/Stockfish/src/endgame.cpp
new file mode 100644
index 0000000..9021f24
--- /dev/null
+++ b/src/Stockfish/src/endgame.cpp
@@ -0,0 +1,747 @@
+/*
+ Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+ Copyright (C) 2004-2023 The Stockfish developers (see AUTHORS file)
+
+ Stockfish is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Stockfish is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see .
+*/
+
+#include
+
+#include "bitboard.h"
+#include "endgame.h"
+#include "movegen.h"
+
+namespace Stockfish {
+
+namespace {
+
+ // Used to drive the king towards the edge of the board
+ // in KX vs K and KQ vs KR endgames.
+ // Values range from 27 (center squares) to 90 (in the corners)
+ inline int push_to_edge(Square s) {
+ int rd = edge_distance(rank_of(s)), fd = edge_distance(file_of(s));
+ return 90 - (7 * fd * fd / 2 + 7 * rd * rd / 2);
+ }
+
+ // Used to drive the king towards A1H8 corners in KBN vs K endgames.
+ // Values range from 0 on A8H1 diagonal to 7 in A1H8 corners
+ inline int push_to_corner(Square s) {
+ return abs(7 - rank_of(s) - file_of(s));
+ }
+
+ // Drive a piece close to or away from another piece
+ inline int push_close(Square s1, Square s2) { return 140 - 20 * distance(s1, s2); }
+ inline int push_away(Square s1, Square s2) { return 120 - push_close(s1, s2); }
+
+#ifndef NDEBUG
+ bool verify_material(const Position& pos, Color c, Value npm, int pawnsCnt) {
+ return pos.non_pawn_material(c) == npm && pos.count(c) == pawnsCnt;
+ }
+#endif
+
+ // Map the square as if strongSide is white and strongSide's only pawn
+ // is on the left half of the board.
+ Square normalize(const Position& pos, Color strongSide, Square sq) {
+
+ assert(pos.count(strongSide) == 1);
+
+ if (file_of(pos.square(strongSide)) >= FILE_E)
+ sq = flip_file(sq);
+
+ return strongSide == WHITE ? sq : flip_rank(sq);
+ }
+
+} // namespace
+
+
+namespace Endgames {
+
+ std::pair