]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: hash: add new function hash_crc32
authorWilly Tarreau <w@1wt.eu>
Tue, 20 Jan 2015 18:17:09 +0000 (19:17 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 20 Jan 2015 18:48:05 +0000 (19:48 +0100)
This function will be used to perform CRC32 computations. This one wa
loosely inspired from crc32b found here, and focuses on size and speed
at the same time :

    http://www.hackersdelight.org/hdcodetxt/crc.c.txt

Much faster table-based versions exist but are pointless for our usage
here, this hash already sustains gigabit speed which is far faster than
what we'd ever need. Better preserve the CPU's cache instead.

include/common/hash.h
src/hash.c

index 7039ba507f5a16836e9d778dfda1d362d8af0f12..16876c29a0daaf48817d15f45327d86033a4c49c 100644 (file)
@@ -25,5 +25,6 @@
 unsigned int hash_djb2(const char *key, int len);
 unsigned int hash_wt6(const char *key, int len);
 unsigned int hash_sdbm(const char *key, int len);
+unsigned int hash_crc32(const char *key, int len);
 
 #endif /* _COMMON_HASH_H_ */
index aa236cbb0054d0f56f4c5950d5bb7591fe068917..43ba11c7caf976569d669b9091ea38875bdad557 100644 (file)
@@ -85,4 +85,23 @@ unsigned int hash_sdbm(const char *key, int len)
        return hash;
 }
 
+/* Small yet efficient CRC32 calculation loosely inspired from crc32b found
+ * here : http://www.hackersdelight.org/hdcodetxt/crc.c.txt
+ * The magic value represents the polynom with one bit per exponent. Much
+ * faster table-based versions exist but are pointless for our usage here,
+ * this hash already sustains gigabit speed which is far faster than what
+ * we'd ever need. Better preserve the CPU's cache instead.
+ */
+unsigned int hash_crc32(const char *key, int len)
+{
+       unsigned int hash;
+       int bit;
 
+       hash = ~0;
+       while (len--) {
+               hash ^= *key++;
+               for (bit = 0; bit < 8; bit++)
+                       hash = (hash >> 1) ^ ((hash & 1) ? 0xedb88320 : 0);
+       }
+       return ~hash;
+}