From e7ebdc8fb66e04717b15af4cf6719ae23b03aad1 Mon Sep 17 00:00:00 2001
From: Quentin Carbonneaux <quentin@c9x.me>
Date: Mon, 17 Jun 2024 13:17:39 +0200
Subject: [PATCH] qbe has its own magic

---
 simpl.c      | 18 +++++++--------
 tools/log2.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 9 deletions(-)
 create mode 100644 tools/log2.c

diff --git a/simpl.c b/simpl.c
index f22d491..99e09df 100644
--- a/simpl.c
+++ b/simpl.c
@@ -32,20 +32,20 @@ blit(Ref sd[2], int sz, Fn *fn)
 
 static int
 ulog2_tab64[64] = {
-	63,  0, 58,  1, 59, 47, 53,  2,
-	60, 39, 48, 27, 54, 33, 42,  3,
-	61, 51, 37, 40, 49, 18, 28, 20,
-	55, 30, 34, 11, 43, 14, 22,  4,
-	62, 57, 46, 52, 38, 26, 32, 41,
-	50, 36, 17, 19, 29, 10, 13, 21,
-	56, 45, 25, 31, 35, 16,  9, 12,
-	44, 24, 15,  8, 23,  7,  6,  5
+	63,  0,  1, 41, 37,  2, 16, 42,
+	38, 29, 32,  3, 12, 17, 43, 55,
+	39, 35, 30, 53, 33, 21,  4, 23,
+	13,  9, 18,  6, 25, 44, 48, 56,
+	62, 40, 36, 15, 28, 31, 11, 54,
+	34, 52, 20, 22,  8,  5, 24, 47,
+	61, 14, 27, 10, 51, 19,  7, 46,
+	60, 26, 50, 45, 59, 49, 58, 57,
 };
 
 static int
 ulog2(uint64_t pow2)
 {
-	return ulog2_tab64[(pow2 * 0x07EDD5E59A4E28C2) >> 58];
+	return ulog2_tab64[(pow2 * 0x5b31ab928877a7e) >> 58];
 }
 
 static int
diff --git a/tools/log2.c b/tools/log2.c
new file mode 100644
index 0000000..05f97c9
--- /dev/null
+++ b/tools/log2.c
@@ -0,0 +1,64 @@
+#include <assert.h>
+#include <stdio.h>
+
+typedef unsigned long long ullong;
+
+char seen[64];
+ullong rbg = 0x1e0298f7a7e;
+
+int
+bit()
+{
+	int bit;
+
+	bit = rbg & 1;
+	rbg >>= 1;
+	return bit;
+}
+
+int
+search(ullong n, int b, ullong *out)
+{
+	int i, x;
+	ullong y, z;
+
+	if (b == 64) {
+		*out = n;
+		return 1;
+	}
+
+	x = 63 & ((n << (63 - b)) >> 58);
+	assert(!(x & 0) && x <= 62);
+	y = bit();
+
+	for (i=0; i<2; i++) {
+		z = x | (y << 5);
+		if (!seen[z]) {
+			seen[z] = (63-b)+1;
+			if (search(n | (y << b), b+1, out))
+				return 1;
+			seen[z] = 0;
+		}
+		y ^= 1;
+	}
+
+	return 0;
+}
+
+int
+main()
+{
+	ullong out;
+	int i;
+
+	if (search(0, 0, &out)) {
+		printf("0x%llx\n", out);
+		for (i=0; i<64; i++) {
+			printf((i&7) == 0 ? "\t" : " ");
+			printf("%2d,", seen[i]-1);
+			if ((i&7) == 7)
+				printf("\n");
+		}
+	} else
+		puts("not found");
+}