Merge in code for C++ compiling of libmemcached.
[m6w6/libmemcached] / libhashkit / fnv.cc
1 /* HashKit
2 * Copyright (C) 2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 */
8
9 #include <libhashkit/common.h>
10
11 /* FNV hash'es lifted from Dustin Sallings work */
12 static uint64_t FNV_64_INIT= UINT64_C(0xcbf29ce484222325);
13 static uint64_t FNV_64_PRIME= UINT64_C(0x100000001b3);
14 static uint32_t FNV_32_INIT= 2166136261UL;
15 static uint32_t FNV_32_PRIME= 16777619;
16
17 uint32_t hashkit_fnv1_64(const char *key, size_t key_length, void *context)
18 {
19 /* Thanks to pierre@demartines.com for the pointer */
20 uint64_t hash= FNV_64_INIT;
21 (void)context;
22
23 for (size_t x= 0; x < key_length; x++)
24 {
25 hash *= FNV_64_PRIME;
26 hash ^= (uint64_t)key[x];
27 }
28
29 return (uint32_t)hash;
30 }
31
32 uint32_t hashkit_fnv1a_64(const char *key, size_t key_length, void *context)
33 {
34 uint32_t hash= (uint32_t) FNV_64_INIT;
35 (void)context;
36
37 for (size_t x= 0; x < key_length; x++)
38 {
39 uint32_t val= (uint32_t)key[x];
40 hash ^= val;
41 hash *= (uint32_t) FNV_64_PRIME;
42 }
43
44 return hash;
45 }
46
47 uint32_t hashkit_fnv1_32(const char *key, size_t key_length, void *context)
48 {
49 uint32_t hash= FNV_32_INIT;
50 (void)context;
51
52 for (size_t x= 0; x < key_length; x++)
53 {
54 uint32_t val= (uint32_t)key[x];
55 hash *= FNV_32_PRIME;
56 hash ^= val;
57 }
58
59 return hash;
60 }
61
62 uint32_t hashkit_fnv1a_32(const char *key, size_t key_length, void *context)
63 {
64 uint32_t hash= FNV_32_INIT;
65 (void)context;
66
67 for (size_t x= 0; x < key_length; x++)
68 {
69 uint32_t val= (uint32_t)key[x];
70 hash ^= val;
71 hash *= FNV_32_PRIME;
72 }
73
74 return hash;
75 }