]> git.feebdaed.xyz Git - linuxyz.git/commitdiff
hash fnv
authorseantywork <seantywork@gmail.com>
Mon, 25 Aug 2025 03:48:54 +0000 (12:48 +0900)
committerseantywork <seantywork@gmail.com>
Mon, 25 Aug 2025 03:48:54 +0000 (12:48 +0900)
hashmap-concurrent-perf/cmap.c
hashmap-concurrent-perf/cmap.h

index 41841956eef1a12ce66283260862dc4ec4b81957..186a3c2e2c77d0982b7f10b6576a2d906b912d78 100644 (file)
@@ -8,6 +8,7 @@
 
 #define HASHLEN 32
 #define HASHTRUNCLEN 8
+#if HASHSHA
 static inline uint64_t _hashfunc(uint8_t* data, size_t size, uint64_t div){
 
        uint64_t hashtrunc = 0;
@@ -23,6 +24,34 @@ static inline uint64_t _hashfunc(uint8_t* data, size_t size, uint64_t div){
        
        return hashtrunc % div;
 }
+#elif HASHFNV
+#define FNV1_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
+static inline uint64_t _hashfunc(uint8_t* data, size_t size, uint64_t div){
+    unsigned char *bp = (unsigned char *)data; /* start of buffer */
+    unsigned char *be = bp + size;             /* beyond end of buffer */
+       uint64_t hval = FNV1_64_INIT;
+    /*
+     * FNV-1 hash each octet of the buffer
+     */
+    while (bp < be) {
+
+       /* multiply by the 64 bit FNV magic prime mod 2^64 */
+#if defined(NO_FNV_GCC_OPTIMIZATION)
+               hval *= FNV_64_PRIME;
+#else /* NO_FNV_GCC_OPTIMIZATION */
+               hval += (hval << 1) + (hval << 4) + (hval << 5) +
+                       (hval << 7) + (hval << 8) + (hval << 40);
+#endif /* NO_FNV_GCC_OPTIMIZATION */
+
+       /* xor the bottom with the current octet */
+               hval ^= (uint64_t)*bp++;
+    }
+
+    /* return our new hash value */
+    return hval % div;
+}
+#endif
+
 
 
 BOGUS_CMAP* cmap_alloc(int buck_size, int data_size){
index 7b8293a5b27e4a0ab26d5aa03d6c404356f09cbe..4a1ca2d3d7803d927791339a15d92c0fb4ef1f35 100644 (file)
@@ -14,6 +14,9 @@
 #include <openssl/hmac.h>
 #include <openssl/err.h>
 
+#define HASHSHA 0
+#define HASHFNV 1
+
 #define _BOGUS_KEY struct { \
     uint64_t index; \
 }