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