summaryrefslogtreecommitdiffstats
path: root/include/fnv_hash.h
diff options
context:
space:
mode:
authorkcwu <kcwu@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2005-02-25 18:59:22 +0800
committerkcwu <kcwu@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2005-02-25 18:59:22 +0800
commit3944fd34ec9b7b7bf252c7169a7ebc9b84e95fbf (patch)
treee164cedcefe078e766ab4c300fe252611fec69c4 /include/fnv_hash.h
parente14cc0a0a4682b43440aa5db8f72cad16d35abe9 (diff)
downloadpttbbs-3944fd34ec9b7b7bf252c7169a7ebc9b84e95fbf.tar
pttbbs-3944fd34ec9b7b7bf252c7169a7ebc9b84e95fbf.tar.gz
pttbbs-3944fd34ec9b7b7bf252c7169a7ebc9b84e95fbf.tar.bz2
pttbbs-3944fd34ec9b7b7bf252c7169a7ebc9b84e95fbf.tar.lz
pttbbs-3944fd34ec9b7b7bf252c7169a7ebc9b84e95fbf.tar.xz
pttbbs-3944fd34ec9b7b7bf252c7169a7ebc9b84e95fbf.tar.zst
pttbbs-3944fd34ec9b7b7bf252c7169a7ebc9b84e95fbf.zip
import FNV hash function.
rewrite getkeep() for memory saving git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@2544 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'include/fnv_hash.h')
-rw-r--r--include/fnv_hash.h107
1 files changed, 107 insertions, 0 deletions
diff --git a/include/fnv_hash.h b/include/fnv_hash.h
new file mode 100644
index 00000000..9d8851bd
--- /dev/null
+++ b/include/fnv_hash.h
@@ -0,0 +1,107 @@
+/*
+ * Fowler / Noll / Vo Hash (FNV Hash)
+ * http://www.isthe.com/chongo/tech/comp/fnv/
+ *
+ * This is an implementation of the algorithms posted above.
+ * This file is placed in the public domain by Peter Wemm.
+ *
+ * $FreeBSD: src/sys/sys/fnv_hash.h,v 1.2 2001/03/20 02:10:18 peter Exp $
+ */
+
+typedef unsigned int Fnv32_t;
+typedef unsigned long long Fnv64_t;
+
+#define FNV1_32_INIT ((Fnv32_t) 33554467UL)
+#define FNV1_64_INIT ((Fnv64_t) 0xcbf29ce484222325ULL)
+
+#define FNV_32_PRIME ((Fnv32_t) 0x01000193UL)
+#define FNV_64_PRIME ((Fnv64_t) 0x100000001b3ULL)
+
+static __inline Fnv32_t
+fnv_32_buf(const void *buf, size_t len, Fnv32_t hval)
+{
+ const unsigned char *s = (const unsigned char *)buf;
+
+ while (len-- != 0) {
+ hval *= FNV_32_PRIME;
+ hval ^= *s++;
+ }
+ return hval;
+}
+
+static __inline Fnv32_t
+fnv_32_str(const char *str, Fnv32_t hval)
+{
+ const unsigned char *s = (const unsigned char *)str;
+ Fnv32_t c;
+
+ while ((c = *s++) != 0) {
+ hval *= FNV_32_PRIME;
+ hval ^= c;
+ }
+ return hval;
+}
+
+static __inline Fnv32_t
+fnv1a_32_str(const char *str, Fnv32_t hval)
+{
+ const unsigned char *s = (const unsigned char *)str;
+ Fnv32_t c;
+
+ while ((c = *s++) != 0) {
+ hval ^= c;
+ hval *= FNV_32_PRIME;
+ }
+ return hval;
+}
+
+static __inline Fnv32_t
+fnv1a_32_strcase(const char *str, Fnv32_t hval)
+{
+ const unsigned char *s = (const unsigned char *)str;
+ Fnv32_t c;
+
+ while ((c = *s++) != 0) {
+ hval ^= toupper(c);
+ hval *= FNV_32_PRIME;
+ }
+ return hval;
+}
+
+static __inline Fnv64_t
+fnv_64_buf(const void *buf, size_t len, Fnv64_t hval)
+{
+ const unsigned char *s = (const unsigned char *)buf;
+
+ while (len-- != 0) {
+ hval *= FNV_64_PRIME;
+ hval ^= *s++;
+ }
+ return hval;
+}
+
+static __inline Fnv64_t
+fnv_64_str(const char *str, Fnv64_t hval)
+{
+ const unsigned char *s = (const unsigned char *)str;
+ Fnv64_t c;
+
+ while ((c = *s++) != 0) {
+ hval *= FNV_64_PRIME;
+ hval ^= c;
+ }
+ return hval;
+}
+
+static __inline Fnv64_t
+fnv1a_64_strcase(const char *str, Fnv64_t hval)
+{
+ const unsigned char *s = (const unsigned char *)str;
+ Fnv64_t c;
+
+ while ((c = *s++) != 0) {
+ hval ^= toupper(c);
+ hval *= FNV_64_PRIME;
+ }
+ return hval;
+}