shithub: moonfish

Download patch

ref: 36fb1feedf9263ad373371e9fc85d8b057da122f
parent: e9cdd4a19840832a863180a8e1a83dcd7ea2f92a
author: zamfofex <zamfofex@twdb.moe>
date: Wed Sep 18 23:31:30 EDT 2024

add tests & other small changes

--- a/.build.yml
+++ b/.build.yml
@@ -14,18 +14,18 @@
   - wasi-sdk
   - clang
   - lld
-  - llvm
+  - binaryen
 tasks:
   - build: |
       cd moonfish
       make LDFLAGS=-static
       make LDFLAGS=-static CC=x86_64-w64-mingw32-gcc moonfish.exe
-      make LDFLAGS='-static -Wl,--max-memory=1073741824' CC='clang --target=wasm32-wasi-threads --sysroot=/usr/share/wasi-sysroot' moonfish.wasm
+      make LDFLAGS=-static CC='clang --target=wasm32-wasi' CPPFLAGS='-Dmoonfish_no_threads' moonfish.wasm
       ./minify.sh
   - strip: |
       cd moonfish
       strip --strip-all moonfish play lichess analyse battle ribbon chat book
-      llvm-strip --strip-all moonfish.wasm
+      wasm-opt -O4 -o moonfish.wasm moonfish.wasm
   - deploy: |
       set +x
       if test "x$GIT_REF" = x || ! test -f neocities-token.txt
--- a/.gitignore
+++ b/.gitignore
@@ -7,6 +7,7 @@
 !/README.md
 !/makefile
 !/.build.yml
+!/check.sh
 !/moonfish.vcxproj
 !/minify.sh
 !/rename.sh
--- /dev/null
+++ b/check.sh
@@ -1,0 +1,53 @@
+#!/usr/bin/env bash
+
+# moonfish is licensed under the AGPL (v3 or later)
+# copyright 2024 zamfofex
+
+set -e
+
+positions=("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -" "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -" "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1" "r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 1" "rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8" "r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10")
+
+diff - <(
+	echo 'checking starting position' >&2
+	for n in {1..5}
+	do ./perft -N"$n"
+	done
+	
+	for f in "${positions[@]}"
+	do
+		echo "checking '$f'" >&2
+		for n in {1..4}
+		do ./perft -N"$n" -F"$f"
+		done
+	done
+) <<END
+perft 1: 20
+perft 2: 400
+perft 3: 8902
+perft 4: 197281
+perft 5: 4865609
+perft 1: 48
+perft 2: 2039
+perft 3: 97862
+perft 4: 4085603
+perft 1: 14
+perft 2: 191
+perft 3: 2812
+perft 4: 43238
+perft 1: 6
+perft 2: 264
+perft 3: 9467
+perft 4: 422333
+perft 1: 6
+perft 2: 264
+perft 3: 9467
+perft 4: 422333
+perft 1: 44
+perft 2: 1486
+perft 3: 62379
+perft 4: 2103487
+perft 1: 46
+perft 2: 2079
+perft 3: 89890
+perft 4: 3894594
+END
--- a/makefile
+++ b/makefile
@@ -9,17 +9,21 @@
 
 moonfish_cc := $(cc) -pthread -D_POSIX_C_SOURCE=199309L
 tools_cc := $(cc) -pthread -D_POSIX_C_SOURCE=200809L
+wasm_cc := $(cc) -D_POSIX_C_SOURCE=199309L
 
 tools_src := moonfish.h tools/tools.h tools/utils.c chess.c
 ugi_src := $(tools_src) tools/ugi.h tools/ugi.c tools/ugi-uci.c
 
-.PHONY: all clean install uninstall
+.PHONY: all clean install uninstall check
 
 all: moonfish play lichess analyse battle ribbon chat uci-ugi ugi-uci book
 
-moonfish moonfish.exe moonfish.wasm: moonfish.h chess.c search.c main.c
+moonfish moonfish.exe: moonfish.h chess.c search.c main.c
 	$(moonfish_cc) -o $@ $(filter %.c,$^)
 
+moonfish.wasm: moonfish.h chess.c search.c main.c
+	$(wasm_cc) -o $@ $(filter %.c,$^)
+
 %: $(tools_src) tools/%.c
 	$(tools_cc) -o $@ $(filter %.c,$^)
 
@@ -35,8 +39,11 @@
 uci-ugi: $(ugi_src)
 	$(tools_cc) -o $@ $(filter %.c,$^)
 
-learn: $(tools_src) tools/learn.c
+learn: $(tools_src) search.c tools/learn.c
 	$(tools_cc) -Dmoonfish_learn -o $@ $(filter %.c,$^)
+
+check: perft
+	./check.sh
 
 clean:
 	git clean -fdx
--- a/minify.sh
+++ b/minify.sh
@@ -5,6 +5,9 @@
 
 set -e
 
+sh="${shell:-"${SHELL:-bash}"}"
+cc="${HOST_CC:-gcc}"
+
 # for each C source file
 cat moonfish.h chess.c search.c mini.c |
 
@@ -23,7 +26,7 @@
 sed 's/^int\b//g' |
 sed 's/^void\b//g' |
 
-#remove redundant 'int'
+# remove redundant 'int'
 sed 's/\bstatic int\b/static/g' |
 
 # remove the '#' from system '#include'
@@ -31,7 +34,7 @@
 
 # preprocess the file, add '#' back to 'include'
 # note: this materialises the whole file
-gcc -E -Dinclude='#include' -Dmoonfish_mini - |
+"$cc" -E -Dinclude='#include' -Dmoonfish_mini - |
 
 # remove lines starting with '# '
 sed '/^# /d' |
@@ -54,7 +57,7 @@
 sed '/^[^"'"'"'#]/s/[a-z0-9_]\+/\n\0\n/gi' |
 
 # rename identifiers to be shorter
-./rename.sh |
+"$sh" rename.sh |
 
 # replace all white space with tabs (except inside string literals)
 # note: this makes the next 'sed' materialise the whole file
--