More Cleanup
[awesomized/libmemcached] / libhashkit / fnv.c
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 "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 __attribute__((unused)))
18 {
19 /* Thanks to pierre@demartines.com for the pointer */
20 uint64_t hash= FNV_64_INIT;
21
22 for (size_t x= 0; x < key_length; x++)
23 {
24 hash *= FNV_64_PRIME;
25 hash ^= (uint64_t)key[x];
26 }
27
28 return (uint32_t)hash;
29 }
30
31 uint32_t hashkit_fnv1a_64(const char *key, size_t key_length, void *context __attribute__((unused)))
32 {
33 uint32_t hash= (uint32_t) FNV_64_INIT;
34
35 for (size_t x= 0; x < key_length; x++)
36 {
37 uint32_t val= (uint32_t)key[x];
38 hash ^= val;
39 hash *= (uint32_t) FNV_64_PRIME;
40 }
41
42 return hash;
43 }
44
45 uint32_t hashkit_fnv1_32(const char *key, size_t key_length, void *context __attribute__((unused)))
46 {
47 uint32_t hash= FNV_32_INIT;
48
49 for (size_t x= 0; x < key_length; x++)
50 {
51 uint32_t val= (uint32_t)key[x];
52 hash *= FNV_32_PRIME;
53 hash ^= val;
54 }
55
56 return hash;
57 }
58
59 uint32_t hashkit_fnv1a_32(const char *key, size_t key_length, void *context __attribute__((unused)))
60 {
61 uint32_t hash= FNV_32_INIT;
62
63 for (size_t x= 0; x < key_length; x++)
64 {
65 uint32_t val= (uint32_t)key[x];
66 hash ^= val;
67 hash *= FNV_32_PRIME;
68 }
69
70 return hash;
71 }