shithub: moonfish

Download patch

ref: e70fe4956e6b622a15d362e3b05c788057015b8c
parent: 575be7e3a63fadd2aa6a349c5e3e0f9b9e115e0f
author: zamfofex <zamfofex@twdb.moe>
date: Mon Dec 18 21:56:43 EST 2023

improve minification

--- a/chess.c
+++ b/chess.c
@@ -180,10 +180,10 @@
 
 static void moonfish_move_king(struct moonfish_chess *chess, struct moonfish_move **moves, unsigned char from)
 {
-	moonfish_jump(chess, moves, from, 1 + 10);
-	moonfish_jump(chess, moves, from, -1 + 10);
-	moonfish_jump(chess, moves, from, 1 - 10);
-	moonfish_jump(chess, moves, from, -1 - 10);
+	moonfish_jump(chess, moves, from, 11);
+	moonfish_jump(chess, moves, from, 9);
+	moonfish_jump(chess, moves, from, -9);
+	moonfish_jump(chess, moves, from, -11);
 	moonfish_jump(chess, moves, from, 10);
 	moonfish_jump(chess, moves, from, -10);
 	moonfish_jump(chess, moves, from, 1);
--- a/main.c
+++ b/main.c
@@ -15,7 +15,7 @@
 	char *arg;
 	struct moonfish_move move;
 	char name[6];
-	long int time, wtime, btime, *xtime;
+	long int wtime, btime, *xtime;
 	
 	if (argc > 1)
 	{
@@ -78,23 +78,14 @@
 				}
 			}
 			
-			if (wtime < 0 && btime < 0)
-			{
-				wtime = 3600000;
-				btime = 3600000;
-			}
+			if (wtime < 0) wtime = 0;
+			if (btime < 0) btime = 0;
 			
-			if (wtime < 0) wtime = btime;
-			if (btime < 0) btime = wtime;
+			if (ctx->chess.white)
+				moonfish_best_move(ctx, &move, wtime, btime);
+			else
+				moonfish_best_move(ctx, &move, btime, wtime);
 			
-			if (!ctx->chess.white)
-			{
-				time = wtime;
-				wtime = btime;
-				btime = time;
-			}
-			
-			moonfish_best_move(ctx, &move, wtime, btime);
 			moonfish_to_uci(name, &move);
 			printf("bestmove %s\n", name);
 		}
@@ -164,6 +155,7 @@
 		{
 			printf("readyok\n");
 		}
+#ifndef moonfish_mini
 		else if (!strcmp(arg, "debug") || !strcmp(arg, "setoption") || !strcmp(arg, "ucinewgame") || !strcmp(arg, "stop"))
 		{
 		}
@@ -171,6 +163,7 @@
 		{
 			fprintf(stderr, "%s: unknown command '%s'\n", argv[0], arg);
 		}
+#endif
 		
 		fflush(stdout);
 	}
--- a/minify.sh
+++ b/minify.sh
@@ -11,8 +11,8 @@
 # remove the '#' from system '#include'
 sed 's/^#\(include <\)/\1/g' |
 
-# preprocess the file, add '#' back to 'include'
-gcc -E -Dinclude='#include' - |
+# preprocess the file, add '#' back to 'include', remove debug statements
+gcc -E -Dinclude='#include' -D'perror(...)=' -D'fprintf(...)=' -D'free(...)=' -Dmoonfish_mini - |
 
 # remove lines starting with '# '
 sed '/^# /d' |
@@ -35,6 +35,34 @@
 # join all adjacent white space into a single tab
 sed '/^[^"'"'"']/s/[\t ]\+/\t/g' |
 
+# replace long/common words (ugly but effective)
+sed '/^[^"'"'"']/s/\(\b\|_\)moonfish\(\b\|_\)/\1F\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)white\(\b\|_\)/\1W\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)black\(\b\|_\)/\1Y\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)pawn\(\b\|_\)/\1P\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)knight\(\b\|_\)/\1L\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)bishop\(\b\|_\)/\1B\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)rook\(\b\|_\)/\1R\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)queen\(\b\|_\)/\1Q\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)king\(\b\|_\)/\1K\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)chess\(\b\|_\)/\1X\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)score\(\b\|_\)/\1S\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)board\(\b\|_\)/\1D\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)castle\(\b\|_\)/\1O\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)move\(\b\|_\)/\1M\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)moves\(\b\|_\)/\1N\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)from\(\b\|_\)/\1A\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)to\(\b\|_\)/\1Z\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)piece\(\b\|_\)/\1G\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)promotion\(\b\|_\)/\1H\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)color\(\b\|_\)/\1C\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)captured\(\b\|_\)/\1U\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)empty\(\b\|_\)/\1E\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)type\(\b\|_\)/\1T\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)outside\(\b\|_\)/\1J\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)repetition\(\b\|_\)/\1V\2/g' |
+sed '/^[^"'"'"']/s/\(\b\|_\)account\(\b\|_\)/\1I\2/g' |
+
 # remove inserted line breaks
 tr -d '\n' |
 
@@ -55,46 +83,11 @@
 # remove duplicate lines (for '#include')
 awk '!x[$0]++' |
 
-# replace common words
-sed 's/\(\b\|_\)moonfish\(\b\|_\)/_F_/g' |
-sed 's/\(\b\|_\)white\(\b\|_\)/_W_/g' |
-sed 's/\(\b\|_\)black\(\b\|_\)/_Y_/g' |
-sed 's/\(\b\|_\)pawn\(\b\|_\)/_P_/g' |
-sed 's/\(\b\|_\)knight\(\b\|_\)/_L_/g' |
-sed 's/\(\b\|_\)bishop\(\b\|_\)/_B_/g' |
-sed 's/\(\b\|_\)rook\(\b\|_\)/_R_/g' |
-sed 's/\(\b\|_\)queen\(\b\|_\)/_Q_/g' |
-sed 's/\(\b\|_\)king\(\b\|_\)/_K_/g' |
-sed 's/\(\b\|_\)chess\(\b\|_\)/_X_/g' |
-sed 's/\(\b\|_\)score\(\b\|_\)/_S_/g' |
-sed 's/\(\b\|_\)board\(\b\|_\)/_D_/g' |
-sed 's/\(\b\|_\)castle\(\b\|_\)/_O_/g' |
-sed 's/\(\b\|_\)move\(\b\|_\)/_M_/g' |
-sed 's/\(\b\|_\)moves\(\b\|_\)/_N_/g' |
-sed 's/\(\b\|_\)from\(\b\|_\)/_A_/g' |
-sed 's/\(\b\|_\)to\(\b\|_\)/_Z_/g' |
-sed 's/\(\b\|_\)piece\(\b\|_\)/_G_/g' |
-sed 's/\(\b\|_\)promotion\(\b\|_\)/_H_/g' |
-sed 's/\(\b\|_\)color\(\b\|_\)/_C_/g' |
-sed 's/\(\b\|_\)captured\(\b\|_\)/_R_/g' |
-sed 's/\(\b\|_\)empty\(\b\|_\)/_E_/g' |
-sed 's/\(\b\|_\)type\(\b\|_\)/_T_/g' |
-sed 's/\(\b\|_\)outside\(\b\|_\)/_J_/g' |
-
-# collapse underscores
-sed 's/_\+/_/g' |
-sed 's/\b_//g' |
-sed 's/_\b//g' |
-
-# restore significant underscore
-# (hacky, but it works)
-sed 's/\(SC_NPROCESSORS_ONLN\)/_\1/g' |
-
 # store the result into a file
 tee moonfish.c |
 
 # and also compress it
-xz -9 > moonfish.c.xz
+xz -9 -e > moonfish.c.xz
 
 # also make it into a runnable program
 cat - moonfish.c.xz > moonfish.sh << END
--