aboutsummaryrefslogtreecommitdiff
path: root/tools/log2.c
diff options
context:
space:
mode:
authorQuentin Carbonneaux <[email protected]>2024-06-17 13:17:39 +0200
committerQuentin Carbonneaux <[email protected]>2024-06-17 13:17:39 +0200
commite7ebdc8fb66e04717b15af4cf6719ae23b03aad1 (patch)
tree750fa6e3fdb5a978daa43b765276b87990877fa7 /tools/log2.c
parentb5be429091ecc26a5f4f6df777aa99655f868f2a (diff)
qbe has its own magic
Diffstat (limited to 'tools/log2.c')
-rw-r--r--tools/log2.c64
1 files changed, 64 insertions, 0 deletions
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");
+}