]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Add reference tool
authorDale Weiler <killfieldengine@gmail.com>
Sat, 23 Nov 2013 08:10:09 +0000 (03:10 -0500)
committerDale Weiler <killfieldengine@gmail.com>
Sat, 23 Nov 2013 08:10:09 +0000 (03:10 -0500)
util.c

diff --git a/util.c b/util.c
index c73b3beec48e6e0455a193bcd7b1d4518401979a..92e5f755abd9f9720c667aa2ea1bfe73acdc1a85 100644 (file)
--- a/util.c
+++ b/util.c
@@ -455,6 +455,44 @@ uint16_t util_crc16(uint16_t current, const char *k, size_t len) {
 
     return h;
 }
+/*
+ * The table above can be created with the following utility. Which
+ *  just builds the slices and the C array as well.
+ */
+#if 0
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+/* compile with cc -std=c99 */
+int main(void) {
+    uint16_t tab[8][256];
+    for (unsigned i = 0; i < 0x100; ++i) {
+        uint16_t x = i << 8;
+        for (int j = 0; j < 8; ++j)
+            x = (x << 1) ^ ((x & 0x8000) ? 0x1021 : 0);
+        tab[0][i] = x;
+    }
+    for (unsigned i = 0; i < 0x100; ++i) {
+        uint16_t c = tab[0][i];
+        for (unsigned j = 1; j < 8; ++j) {
+            c = tab[0][c >> 8] ^ (c << 8);
+            tab[j][i] = c;
+        }
+    }
+    printf("static const uint16_t util_crc16_table[8][256] = {");
+    for (int i = 0; i < 8; ++i) {
+        printf("{\n");
+        for (int j = 0; j < 0x100; ++j) {
+            printf((j & 7) ? " " : "    ");
+            printf((j != 0x100-1) ? "0x%04X," : "0x%04X", tab[i][j]);
+            if ((j & 7) == 7)
+                printf("\n");
+        }
+        printf((i != 7) ? "}," : "}");
+    }
+    printf("};\n");
+}
+#endif
 
 /*
  * modifier is the match to make and the transposition from it, while add is the upper-value that determines the