Answers.Body: <p>We are going to need the source code : <code>git clone https://github.com/official-stockfish/Stockfish.git</code> (see the right address at Stockfish's github as this address could change) somewhere on your PC. (You need the command line utilities of Tortois Git for instance.) Let's call <code>StockFishDIr</code> the full path to the StockFish folder (that contains <code>src</code> as subfolder) on the PC.</p>
  <p>We also need a cross-compiler to ... <em>cross-compile</em> (!) the source-code on the PC into an executable targeting the Android OS, meaning : being executable on the ANdroid OS, i.e. on the phone. We are going to use <a href="https://developer.android.com/ndk" rel="nofollow noreferrer">Android-NDK</a>, which contains toolchains and cross-compilers. We download the &quot;Latest Stable Version&quot; r22 (as of the moment I am writing these lines) and unzip it to a folder on the PC ; I call &quot;AndroidNdkDir&quot; the full path to this directory (containing <code>build</code>, <code>meta</code>, <code>prebuilt</code>, <code>toolchains</code> etc folders) on the PC.</p>
  <p>Finally, as in Stockfish's source code there is only a Makefile, we are going to need <code>make</code> under windows. Yes. Just get it from <a href="http://gnuwin32.sourceforge.net/packages/make.htm" rel="nofollow noreferrer">GnuWin32</a> and note <code>MakeDir</code> the installation directory on you PC, the one containg the <code>bin</code> folder containing <code>make.exe</code>. Analyzing the <code>MakeFile</code> linearly show that the cross-compilation boils down to only one command line provided some file is downloaded, and to another &quot;optimizing&quot; command line.</p>
  <p>Now we look at the <code>MakeFile</code> from <code>StockFishDIr\src</code> ; we will modify it slightly.</p>
  <p>First we change the executable name by changine the line</p>
  <pre><code>EXE = stockfish
  </code></pre>
  <p>into</p>
  <pre><code>EXE = stockfish_13_armv8_xcomp_win10x64
  </code></pre>
  <p>to give it a meaningful name.</p>
  <p>Second, we locate the occurence of <code>armv8</code> (it is the <code>ARCH</code> of the Samsung Galaxy S21 Ultra ; if you have another Android phone, maybe you should look for 'armv7' and adapt consequently what follows) concerning the compiler : namely, we replace the bit</p>
  <pre><code>    ifeq ($(arch),armv8)
          CXX=aarch64-linux-android21-clang++
          STRIP=aarch64-linux-android-strip
      endif
  </code></pre>
  <p>with</p>
  <pre><code>    ifeq ($(arch),armv8)
          CXX=AndroidNdkDir\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android30-clang++.cmd
          STRIP=AndroidNdkDir\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android-strip.exe
  endif
      endif
  </code></pre>
  <p>(Note that there was the initial <code>android21</code> in the CXX line that I replaced with <code>android30</code>. It is because the Samung Galaxy s21 Ultra runs Android 11 whose real version number is android30. If you have an android phone with another android version, adapt the lines consequently.)</p>
  <p>Now, before compiling, we need one last piece : the <em>net</em> file (technical, net = networks = neural network = the weights file of the neural network Stockfish operates with). &quot;Normally&quot; (ah ah), under a linux setup (all what I do here works almost <em>mutatis mutandis</em> on Linux), we would just do a <code>&quot;MakeDir\bin\make.exe&quot; profile-build ARCH=x86-64</code> which would download the net file where needed, and we would not even bother we it. Sadly, running here <code>&quot;MakeDir\bin\make.exe&quot; profile-build ARCH=x86-64</code> doesn't work for some reason, and we need to download the net file ourselves manually. Hence we have to look in the Makefile to find how the net file is downloaded. The bit that matters in the Makefile is :</p>
  <pre><code># evaluation network (nnue)
  net:
      $(eval nnuenet := $(shell grep EvalFileDefaultName evaluate.h | grep define | sed 's/.*\(nn-[a-z0-9]\{12\}.nnue\).*/\1/'))
      @echo &quot;Default net: $(nnuenet)&quot;
      $(eval nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(nnuenet))
      $(eval curl_or_wget := $(shell if hash curl 2&gt;/dev/null; then echo &quot;curl -skL&quot;; elif hash wget 2&gt;/dev/null; then echo &quot;wget -qO-&quot;; fi))
      @if test -f &quot;$(nnuenet)&quot;; then \
              echo &quot;Already available.&quot;; \
          else \
              if [ &quot;x$(curl_or_wget)&quot; = &quot;x&quot; ]; then \
              echo &quot;Automatic download failed: neither curl nor wget is installed. Install one of these tools or download the net manually&quot;; exit 1; \
              else \
              echo &quot;Downloading $(nnuedownloadurl)&quot;; $(curl_or_wget) $(nnuedownloadurl) &gt; $(nnuenet);\
              fi; \
          fi;
      $(eval shasum_command := $(shell if hash shasum 2&gt;/dev/null; then echo &quot;shasum -a 256 &quot;; elif hash sha256sum 2&gt;/dev/null; then echo &quot;sha256sum &quot;; fi))
      @if [ &quot;x$(shasum_command)&quot; != &quot;x&quot; ]; then \
          if [ &quot;$(nnuenet)&quot; != &quot;nn-&quot;`$(shasum_command) $(nnuenet) | cut -c1-12`&quot;.nnue&quot; ]; then \
                  echo &quot;Failed download or $(nnuenet) corrupted, please delete!&quot;; exit 1; \
              fi \
          else \
              echo &quot;shasum / sha256sum not found, skipping net validation&quot;; \
          fi
  </code></pre>
  <p>We see that the address of the file is <code>https://tests.stockfishchess.org/api/nn/$(nnuenet))</code> and that the variable <code>nnuenet</code> is defined by <code>grep</code>ing the string <code>EvalFileDefaultName</code> inside the header file <code>evaluate.h</code> (which is inside <code>StockFishDIr\src</code>), the line of interest being :</p>
  <pre><code>#define EvalFileDefaultName   &quot;nn-62ef826d1a6d.nnue&quot;
  </code></pre>
  <p>Hence the variable <code>nnuenet</code> is equal to the string &quot;nn-62ef826d1a6d.nnue&quot; and the address we are going to download the nets from manually is <a href="https://tests.stockfishchess.org/api/nn/nn-62ef826d1a6d.nnue" rel="nofollow noreferrer">https://tests.stockfishchess.org/api/nn/nn-62ef826d1a6d.nnue</a>.</p>
  <p>Of course, this address may change in the future, but you see the idea. Now we place the net file inside <code>StockFishDIr\src</code></p>
  <p>Now we are ready to cross-compile, with the following command to execute inside <code>StockFishDIr\src</code> :</p>
  <pre><code>&quot;MakeDir\bin\make.exe&quot; stockfish_13_armv8_xcomp_win10x64 COMP=ndk COMPCC=&quot;AndroidNdkDir\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android30-clang --target=aarch64-linux-android30&quot; ARCH=armv8
  </code></pre>
  <p>(Adapt here also the <code>aarch64-linux-android30</code> bits according to your Android phone.)</p>
  <p>When the preceeding command executes properly it produces the <code>stockfish_13_armv8_xcomp_win10x64</code> binary inside the <code>StockFishDIr\src</code> folder. This binary is already usuable on you phone (by putting it inside the <code>DroidFish\uci</code> folder of your DroidFish app, for instance) but is not optimal as the size is big and the execution is a little bit slow. Hence we have to &quot;strip&quot; the binary with the following command line, still inside the <code>StockFishDIr\src</code> folder :</p>
  <p>&quot;AndroidNdkDir\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android-strip&quot; stockfish_13_armv8_xcomp_win10x64</p>
  <p>(There's no <code>android30</code> anymore but there is still a <code>aarch64</code> : adapt here also the <code>aarch64-linux-android30</code> bits according to your Android phone.</p>
  <p>Now, your binary file <code>stockfish_13_armv8_xcomp_win10x64</code> is perfectly usable. You can play chess against StockFish 13 on your phone, and suffer ! ;)</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-21T18:14:37.713Z
Answers.LastActivityDate: 2021-02-21T18:14:37.713Z
Answers.UserDisplayName: Olórin
Answers.Body: <p><a href="https://forum.armbian.com/topic/9862-info-how-to-compile-the-actual-64bit-version-of-stockfish-chess-engine/" rel="nofollow noreferrer">https://forum.armbian.com/topic/9862-info-how-to-compile-the-actual-64bit-version-of-stockfish-chess-engine/</a></p>
  
  <p>Seems to show that you have to hack it to get it to compile at all.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-18T11:41:22.140Z
Answers.Text: Yeah for sure the `makefile` will have to be tweaked but in the link that you provide he uses `arch = any` still which shows that the build won't be optimized at all for `arm64-v8a`.
Answers.UserDisplayName: Olórin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-18T15:37:33.930Z
Answers.Text: agree - it doesn't seem that it can be optimized in this release.  I don't see how anyway.
Answers.UserDisplayName: Michael West
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-18T00:40:28.023Z
Answers.LastActivityDate: 2020-01-18T00:40:28.023Z
Answers.UserDisplayName: Michael West
Body: <p>I cloned StockFish from GitHub and did a <code>make</code> inside the <code>src</code> folder and found that supported archs are:</p>
  <ul>
  <li>x86-64-bmi2             &gt; x86 64-bit with pext support (also enables SSE4)</li>
  <li>x86-64-modern           &gt; x86 64-bit with popcnt support (also enables SSE3)</li>
  <li>x86-64                  &gt; x86 64-bit generic</li>
  <li>x86-32                  &gt; x86 32-bit (also enables SSE)</li>
  <li>x86-32-old              &gt; x86 32-bit fall back for old hardware</li>
  <li>ppc-64                  &gt; PPC 64-bit</li>
  <li>ppc-32                  &gt; PPC 32-bit</li>
  <li>armv7                   &gt; ARMv7 32-bit</li>
  <li>general-64              &gt; unspecified 64-bit</li>
  <li>general-32              &gt; unspecified 32-bit</li>
  </ul>
  <p>I want to build as optimized as possible for my samsung galaxy s10+ running having an arm64-v8a exynos9820 processor.</p>
  <p>Which archs should I choose? Will they be optimized as they could be when I build  for x86-64-bmi2 for my 64bits ubuntu? If not, how could I optimize them?</p>
  <p>(The rather simple idea is that I want to build it to have it as optimized as possible for my mobile phone.)</p>
  
Tags: <stockfish><programming><android>
Title: Compile StockFish optimized for arm64-v8a
ThreadText: # Compile StockFish optimized for arm64-v8a
  
  (Asked by: username_0 on 2020-01-17)
  
  I cloned StockFish from GitHub and did a `make` inside the `src` folder and found that supported archs are:
  
  * x86-64-bmi2 > x86 64-bit with pext support (also enables SSE4)
  * x86-64-modern > x86 64-bit with popcnt support (also enables SSE3)
  * x86-64 > x86 64-bit generic
  * x86-32 > x86 32-bit (also enables SSE)
  * x86-32-old > x86 32-bit fall back for old hardware
  * ppc-64 > PPC 64-bit
  * ppc-32 > PPC 32-bit
  * armv7 > ARMv7 32-bit
  * general-64 > unspecified 64-bit
  * general-32 > unspecified 32-bit
  
  I want to build as optimized as possible for my samsung galaxy s10+ running having an arm64-v8a exynos9820 processor.
  
  Which archs should I choose? Will they be optimized as they could be when I build for x86-64-bmi2 for my 64bits ubuntu? If not, how could I optimize them?
  
  (The rather simple idea is that I want to build it to have it as optimized as possible for my mobile phone.)
  
  **Tags:** stockfish, programming, android
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2021-02-21. Score: 2 | Accepted answer
  
  We are going to need the source code : `git clone https://github.com/official-stockfish/Stockfish.git` (see the right address at Stockfish's github as this address could change) somewhere on your PC. (You need the command line utilities of Tortois Git for instance.) Let's call `StockFishDIr` the full path to the StockFish folder (that contains `src` as subfolder) on the PC.
  
  We also need a cross-compiler to ... *cross-compile* (!) the source-code on the PC into an executable targeting the Android OS, meaning : being executable on the ANdroid OS, i.e. on the phone. We are going to use [Android-NDK](https://developer.android.com/ndk), which contains toolchains and cross-compilers. We download the "Latest Stable Version" r22 (as of the moment I am writing these lines) and unzip it to a folder on the PC ; I call "AndroidNdkDir" the full path to this directory (containing `build`, `meta`, `prebuilt`, `toolchains` etc folders) on the PC.
  
  Finally, as in Stockfish's source code there is only a Makefile, we are going to need `make` under windows. Yes. Just get it from [GnuWin32](http://gnuwin32.sourceforge.net/packages/make.htm) and note `MakeDir` the installation directory on you PC, the one containg the `bin` folder containing `make.exe`. Analyzing the `MakeFile` linearly show that the cross-compilation boils down to only one command line provided some file is downloaded, and to another "optimizing" command line.
  
  Now we look at the `MakeFile` from `StockFishDIr\src` ; we will modify it slightly.
  
  First we change the executable name by changine the line
  
  ```
  EXE = stockfish
  
  ```
  
  into
  
  ```
  EXE = stockfish_13_armv8_xcomp_win10x64
  
  ```
  
  to give it a meaningful name.
  
  Second, we locate the occurence of `armv8` (it is the `ARCH` of the Samsung Galaxy S21 Ultra ; if you have another Android phone, maybe you should look for 'armv7' and adapt consequently what follows) concerning the compiler : namely, we replace the bit
  
  ```
      ifeq ($(arch),armv8)
          CXX=aarch64-linux-android21-clang++
          STRIP=aarch64-linux-android-strip
      endif
  
  ```
  
  with
  
  ```
      ifeq ($(arch),armv8)
          CXX=AndroidNdkDir\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android30-clang++.cmd
          STRIP=AndroidNdkDir\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android-strip.exe
  endif
      endif
  
  ```
  
  (Note that there was the initial `android21` in the CXX line that I replaced with `android30`. It is because the Samung Galaxy s21 Ultra runs Android 11 whose real version number is android30. If you have an android phone with another android version, adapt the lines consequently.)
  
  Now, before compiling, we need one last piece : the *net* file (technical, net = networks = neural network = the weights file of the neural network Stockfish operates with). "Normally" (ah ah), under a linux setup (all what I do here works almost *mutatis mutandis* on Linux), we would just do a `"MakeDir\bin\make.exe" profile-build ARCH=x86-64` which would download the net file where needed, and we would not even bother we it. Sadly, running here `"MakeDir\bin\make.exe" profile-build ARCH=x86-64` doesn't work for some reason, and we need to download the net file ourselves manually. Hence we have to look in the Makefile to find how the net file is downloaded. The bit that matters in the Makefile is :
  
  ```
  # 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 nnuedownloadurl := https://tests.stockfishchess.org/api/nn/$(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 test -f "$(nnuenet)"; then \
              echo "Already available."; \
          else \
              if [ "x$(curl_or_wget)" = "x" ]; then \
              echo "Automatic download failed: neither curl nor wget is installed. Install one of these tools or download the net manually"; exit 1; \
              else \
              echo "Downloading $(nnuedownloadurl)"; $(curl_or_wget) $(nnuedownloadurl) > $(nnuenet);\
              fi; \
          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 \
          if [ "$(nnuenet)" != "nn-"`$(shasum_command) $(nnuenet) | cut -c1-12`".nnue" ]; then \
                  echo "Failed download or $(nnuenet) corrupted, please delete!"; exit 1; \
              fi \
          else \
              echo "shasum / sha256sum not found, skipping net validation"; \
          fi
  
  ```
  
  We see that the address of the file is `https://tests.stockfishchess.org/api/nn/$(nnuenet))` and that the variable `nnuenet` is defined by `grep`ing the string `EvalFileDefaultName` inside the header file `evaluate.h` (which is inside `StockFishDIr\src`), the line of interest being :
  
  ```
  #define EvalFileDefaultName   "nn-62ef826d1a6d.nnue"
  
  ```
  
  Hence the variable `nnuenet` is equal to the string "nn-62ef826d1a6d.nnue" and the address we are going to download the nets from manually is <https://tests.stockfishchess.org/api/nn/nn-62ef826d1a6d.nnue>.
  
  Of course, this address may change in the future, but you see the idea. Now we place the net file inside `StockFishDIr\src`
  
  Now we are ready to cross-compile, with the following command to execute inside `StockFishDIr\src` :
  
  ```
  "MakeDir\bin\make.exe" stockfish_13_armv8_xcomp_win10x64 COMP=ndk COMPCC="AndroidNdkDir\toolchains\llvm\prebuilt\windows-x86_64\bin\aarch64-linux-android30-clang --target=aarch64-linux-android30" ARCH=armv8
  
  ```
  
  (Adapt here also the `aarch64-linux-android30` bits according to your Android phone.)
  
  When the preceeding command executes properly it produces the `stockfish_13_armv8_xcomp_win10x64` binary inside the `StockFishDIr\src` folder. This binary is already usuable on you phone (by putting it inside the `DroidFish\uci` folder of your DroidFish app, for instance) but is not optimal as the size is big and the execution is a little bit slow. Hence we have to "strip" the binary with the following command line, still inside the `StockFishDIr\src` folder :
  
  "AndroidNdkDir\toolchains\llvm\prebuilt\windows-x86\_64\bin\aarch64-linux-android-strip" stockfish\_13\_armv8\_xcomp\_win10x64
  
  (There's no `android30` anymore but there is still a `aarch64` : adapt here also the `aarch64-linux-android30` bits according to your Android phone.
  
  Now, your binary file `stockfish_13_armv8_xcomp_win10x64` is perfectly usable. You can play chess against StockFish 13 on your phone, and suffer ! ;)
  
  
  ---
  
  ### Answer by username_1 on 2020-01-18. Score: 1
  
  <https://forum.armbian.com/topic/9862-info-how-to-compile-the-actual-64bit-version-of-stockfish-chess-engine/>
  
  Seems to show that you have to hack it to get it to compile at all.
  
  > **Comments:**
  > **username_0**: Yeah for sure the `makefile` will have to be tweaked but in the link that you provide he uses `arch = any` still which shows that the build won't be optimized at all for `arm64-v8a`.  
  > **username_1**: agree - it doesn't seem that it can be optimized in this release.  I don't see how anyway.  
  
  ---
  
  