prepare v1.1.4
[awesomized/libmemcached] / src / libhashkit / fnv_64.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached-awesome - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libhashkit/common.h"
17
18 #if __WORDSIZE == 64 && defined(HAVE_FNV64_HASH)
19
20 /* FNV hash'es lifted from Dustin Sallings work */
21 static uint64_t FNV_64_INIT = 0xcbf29ce484222325;
22 static uint64_t FNV_64_PRIME = 0x100000001b3;
23
24 uint32_t hashkit_fnv1_64(const char *key, size_t key_length, void *) {
25 /* Thanks to pierre@demartines.com for the pointer */
26 uint64_t hash = FNV_64_INIT;
27
28 for (size_t x = 0; x < key_length; x++) {
29 hash *= FNV_64_PRIME;
30 hash ^= (uint64_t) key[x];
31 }
32
33 return (uint32_t) hash;
34 }
35
36 uint32_t hashkit_fnv1a_64(const char *key, size_t key_length, void *) {
37 uint32_t hash = (uint32_t) FNV_64_INIT;
38
39 for (size_t x = 0; x < key_length; x++) {
40 uint32_t val = (uint32_t) key[x];
41 hash ^= val;
42 hash *= (uint32_t) FNV_64_PRIME;
43 }
44
45 return hash;
46 }
47
48 #else
49 uint32_t hashkit_fnv1_64(const char *, size_t, void *) {
50 return 0;
51 }
52
53 uint32_t hashkit_fnv1a_64(const char *, size_t, void *) {
54 return 0;
55 }
56 #endif