Lumiera  0.pre.03
»edit your freedom«
hash-fnv.c
Go to the documentation of this file.
1 /*
2  HashFNV - FNV hash functions
3 
4  adapted by Lumiera.org
5  2010, 2011 Christian Thaeter <ct@pipapo.org>
6 
7  original by chongo <Landon Curt Noll> /\oo/\
8  http://www.isthe.com/chongo/
9 
10  Please do not copyright this code. This code is in the public domain.
11 
12  LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
13  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
14  EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
15  CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
16  USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17  OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18  PERFORMANCE OF THIS SOFTWARE.
19 
20  Share and Enjoy! :-)
21 
22 * *****************************************************/
23 
24 
31 #include "lib/hash-fnv.h"
32 
33 #include <nobug.h>
34 
35 
36 
37 uint64_t
38 hash_fnv64a_buf (const void *buf, size_t len, uint64_t hval)
39 {
40  const unsigned char *bp = (const unsigned char *)buf;
41  const unsigned char *be = bp + len;
42  while (bp < be)
43  {
44  hval ^= (uint64_t)*bp++;
45  hval *= HASH_FNV64_PRIME;
46  }
47 
48  return hval;
49 }
50 
51 
52 
53 uint32_t
54 hash_fnv32a_buf (const void *buf, size_t len, uint32_t hval)
55 {
56  const unsigned char *bp = (const unsigned char *)buf;
57  const unsigned char *be = bp + len;
58  while (bp < be)
59  {
60  hval ^= (uint32_t)*bp++;
61  hval *= HASH_FNV32_PRIME;
62  }
63 
64  return hval;
65 }
66 
67 
68 uint64_t
69 hash_fnv64a_strn (const char* str, size_t len, uint64_t hval)
70 {
71  const unsigned char *bp = (const unsigned char *)str;
72  if (bp)
73  while (*bp && len--)
74  {
75  hval ^= (uint64_t)*bp++;
76  hval *= HASH_FNV64_PRIME;
77  }
78 
79  return hval;
80 }
81 
82 
83 
84 uint32_t
85 hash_fnv32a_strn (const char* str, size_t len, uint32_t hval)
86 {
87  const unsigned char *bp = (const unsigned char *)str;
88  if (bp)
89  while (*bp && len--)
90  {
91  hval ^= (uint32_t)*bp++;
92  hval *= HASH_FNV32_PRIME;
93  }
94 
95  return hval;
96 }
97 
98 
99 
100 uint64_t
101 hash_fnv64_xorfold (uint64_t hash, int bits)
102 {
103  REQUIRE(bits <= 64);
104 
105  bits = 64-bits;
106 
107  uint64_t mask = ~0ULL>>bits;
108  for (int todo = 32; bits && todo; todo >>= 1)
109  if (bits >= todo)
110  {
111  hash = hash ^ hash>>todo;
112  bits-=todo;
113  }
114 
115  return hash & mask;
116 }
117 
118 
119 uint32_t
120 hash_fnv32_xorfold (uint32_t hash, int bits)
121 {
122  REQUIRE (bits <= 32);
123 
124  bits = 32-bits;
125 
126  uint32_t mask = ~0ULL>>bits;
127  for (int todo = 16; bits && todo; todo >>= 1)
128  if (bits >= todo)
129  {
130  hash = hash ^ hash>>todo;
131  bits-=todo;
132  }
133 
134  return hash & mask;
135 }
136 
137 
138 uint64_t
139 hash_fnv64_retry (uint64_t hash, uint64_t limit)
140 {
141  uint64_t retry_level= (UINT64_MAX / limit) * limit;
142 
143  while (hash >= retry_level)
144  hash = (hash * HASH_FNV64_PRIME) + HASH_FNV64_BASE;
145 
146  return hash % limit;
147 }
148 
149 
150 uint32_t
151 hash_fnv32_retry (uint64_t hash, uint32_t limit)
152 {
153  uint32_t retry_level= (UINT32_MAX / limit) * limit;
154 
155  while (hash >= retry_level)
156  hash = (hash * HASH_FNV32_PRIME) + HASH_FNV32_BASE;
157 
158  return hash % limit;
159 }
160 
161 /*
162 // Local Variables:
163 // mode: C
164 // c-file-style: "gnu"
165 // indent-tabs-mode: nil
166 // End:
167 */
uint32_t hash_fnv32a_buf(const void *buf, size_t len, uint32_t hval)
FNV-1a 32 bit hash over a buffer.
Definition: hash-fnv.c:54
uint32_t hash_fnv32a_strn(const char *str, size_t len, uint32_t hval)
FNV-1a 32 bit hash over a zero terminated string.
Definition: hash-fnv.c:85
uint32_t hash_fnv32_retry(uint64_t hash, uint32_t limit)
reduce hash to be within 0 to limit-1.
Definition: hash-fnv.c:151
Fowler-Noll-Vo Hashes.
uint64_t hash_fnv64a_strn(const char *str, size_t len, uint64_t hval)
FNV-1a 64 bit hash over a zero terminated string.
Definition: hash-fnv.c:69
uint64_t hash_fnv64_retry(uint64_t hash, uint64_t limit)
reduce hash to be within 0 to limit-1.
Definition: hash-fnv.c:139
uint64_t hash_fnv64a_buf(const void *buf, size_t len, uint64_t hval)
FNV-1a 64 bit hash over a buffer.
Definition: hash-fnv.c:38
uint32_t hash_fnv32_xorfold(uint32_t hash, int bits)
reduce a hash value to n bits.
Definition: hash-fnv.c:120
uint64_t hash_fnv64_xorfold(uint64_t hash, int bits)
reduce a hash value to n bits.
Definition: hash-fnv.c:101