chiark / gitweb /
Add global unihash table; use universal hashing instead of CRC.
[mLib] / crc-mktab.c
index a3ba4301486b7a835a847316cb18cdae0c24d8b4..1833e0e2402fa28cc2bb64e2c2cd841f77300df3 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: crc-mktab.c,v 1.3 2001/01/20 12:06:01 mdw Exp $
+ * $Id: crc-mktab.c,v 1.5 2003/12/15 20:53:47 mdw Exp $
  *
  * Build CRC tables
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: crc-mktab.c,v $
+ * Revision 1.5  2003/12/15 20:53:47  mdw
+ * Add global unihash table; use universal hashing instead of CRC.
+ *
+ * Revision 1.4  2001/03/10 10:59:21  mdw
+ * Fix generating tables where the chunk size is longer than the
+ * polynomial.  Also fix output formatting when there aren't enough entries
+ * to fill a line.
+ *
  * Revision 1.3  2001/01/20 12:06:01  mdw
  * Define flags with macros, to ensure unsignedness.
  *
@@ -94,7 +102,8 @@ static void version(FILE *fp)
 
 static void usage(FILE *fp)
 {
-  pquis(fp, "Usage: $ [-cr] [-g guard] [-o file] [-b bits] [-p poly]\n");
+  pquis(fp, "Usage: $ [-cr] [-o FILE] [-g GUARD] [-s SYM] [-i HEADER]\n\
+       [-t TYPE] [-b BITS] [-B BITS] [-p POLY]\n");
 }
 
 static void help(FILE *fp)
@@ -116,7 +125,8 @@ of options are provided:\n\
 -p, --polynomial=POLY  Use the POLY as the dividing polynomial.\n\
 -r, --reverse          Create a `reversed' CRC table.\n\
 -g, --guard=GUARD      Use GUARD as a multiple-inclusion guard constant.\n\
--s, --symbol=SYM       Name the generated table SYM\n\
+-i, --include=HEADER   Include HEADER at top of C source file.\n\
+-s, --symbol=SYM       Name the generated table SYM.\n\
 -t, --type=TYPE                Give the table entries type TYPE in C source.\n\
 -o, --output=FILE      Write the output to FILE.\n\
 ", stdout);
@@ -256,7 +266,7 @@ int main(int argc, char *argv[])
       bits += 8;
     }
   }
-  nw = bits/4;
+  nw = (bits + 3)/4;
 
   if ((flags & f_ctab) && !type)
     type = bits > 16 ? "unsigned long" : "unsigned short";
@@ -274,7 +284,7 @@ int main(int argc, char *argv[])
       guard = p;
       for (q = file; *q; p++, q++) {
        if (isalnum((unsigned char)*q))
-         *p = *q;
+         *p = toupper((unsigned char)*q);
        else
          *p = '_';
       }
@@ -319,23 +329,36 @@ int main(int argc, char *argv[])
   while (2 + n * (nw + 4) < BSCOL)
     n <<= 1;
   n >>= 1;
+  max = 1 << chunk;
+  if (n > max)
+    n = max;
   t = 0;
   while (((1 + n * (nw + 4)) & ~7) + 8 * t < BSCOL)
     t++;
 
   /* --- Start grinding --- */
 
-  max = 1 << chunk;
   mask = 0xffffffff >> (32 - bits);
   fputc(' ', fp);
   for (i = 0; i < max; i++) {
-    unsigned long x;
+    unsigned long x, y, z;
     unsigned j;
-
-    x = reflect(i, chunk) << (bits - chunk);
-    for (j = 0; j < chunk; j++)
-      x = ((x << 1) ^ (x & (1 << (bits - 1)) ? poly : 0)) & mask;
-    x = reflect(x, bits);
+    unsigned ni, nn;
+
+    x = reflect(i, chunk);
+    y = 0;
+    nn = chunk;
+    while (nn) {
+      ni = bits;
+      if (ni > nn)
+       ni = nn;
+      z = (x >> (nn - ni)) & (0xffffffff >> (32 - ni));
+      y ^= z << (bits - ni);
+      for (j = 0; j < ni; j++)
+       y = ((y << 1) ^ (y & (1 << (bits - 1)) ? poly : 0)) & mask;
+      nn -= ni;
+    }
+    x = reflect(y, bits);
 
     fprintf(fp, " 0x%0*lx", nw, x);
     if (i == max - 1) {