http-bench

http-bench.git
git clone git://git.lenczewski.org/http-bench.git
Log | Files | Refs | README | LICENSE

hash.h (752B)


      1 #ifndef HASH_H
      2 #define HASH_H
      3 
      4 #include <stdint.h>
      5 
      6 static inline uint32_t
      7 fnv32a(void const *buf, size_t len)
      8 {
      9 	// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV_hash_parameters
     10 	uint32_t hash = 0x811C9DC5, prime = 0x01000193;
     11 
     12 	unsigned char const *ptr = buf, *end = ptr + len;
     13 	while (ptr < end)
     14 		hash = (hash ^ *ptr++) * prime;
     15 
     16 	return hash;
     17 }
     18 
     19 static inline uint64_t
     20 fnv64a(void const *buf, size_t len)
     21 {
     22 	// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV_hash_parameters
     23 	uint64_t hash = 0xcbf29ce484222325, prime = 0x00000100000001b3;
     24 
     25 	unsigned char const *ptr = buf, *end = ptr + len;
     26 	while (ptr < end)
     27 		hash = (hash ^ *ptr++) * prime;
     28 
     29 	return hash;
     30 }
     31 
     32 #endif /* HASH_H */