| 1 | /* |
| 2 | * Copyright 2008-2018 Aerospike, Inc. |
| 3 | * |
| 4 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
| 5 | * license agreements. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 | * use this file except in compliance with the License. You may obtain a copy of |
| 9 | * the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations under |
| 15 | * the License. |
| 16 | */ |
| 17 | |
| 18 | #pragma once |
| 19 | |
| 20 | //========================================================== |
| 21 | // Includes. |
| 22 | // |
| 23 | |
| 24 | #include <stddef.h> |
| 25 | |
| 26 | //========================================================== |
| 27 | // Public API. |
| 28 | // |
| 29 | |
| 30 | // 32-bit Fowler-Noll-Vo hash function (FNV-1a). |
| 31 | static inline uint32_t |
| 32 | cf_hash_fnv32(const uint8_t* buf, size_t size) |
| 33 | { |
| 34 | uint32_t hash = 2166136261; |
| 35 | const uint8_t* end = buf + size; |
| 36 | |
| 37 | while (buf < end) { |
| 38 | hash ^= (uint32_t)*buf++; |
| 39 | hash *= 16777619; |
| 40 | } |
| 41 | |
| 42 | return hash; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | // 64-bit Fowler-Noll-Vo hash function (FNV-1a). |
| 47 | static inline uint64_t |
| 48 | cf_hash_fnv64(const uint8_t* buf, size_t size) |
| 49 | { |
| 50 | uint64_t hash = 0xcbf29ce484222325ULL; |
| 51 | const uint8_t* end = buf + size; |
| 52 | |
| 53 | while (buf < end) { |
| 54 | hash ^= (uint64_t)*buf++; |
| 55 | hash *= 0x100000001b3ULL; |
| 56 | } |
| 57 | |
| 58 | return hash; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | // 32-bit Jenkins One-at-a-Time hash function. |
| 63 | static inline uint32_t |
| 64 | cf_hash_jen32(const uint8_t* buf, size_t size) |
| 65 | { |
| 66 | uint32_t hash = 0; |
| 67 | const uint8_t* end = buf + size; |
| 68 | |
| 69 | while (buf < end) { |
| 70 | hash += (uint32_t)*buf++; |
| 71 | hash += hash << 10; |
| 72 | hash ^= hash >> 6; |
| 73 | } |
| 74 | |
| 75 | hash += hash << 3; |
| 76 | hash ^= hash >> 11; |
| 77 | hash += hash << 15; |
| 78 | |
| 79 | return hash; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | // 64-bit Jenkins One-at-a-Time hash function. |
| 84 | static inline uint64_t |
| 85 | cf_hash_jen64(const uint8_t* buf, size_t size) |
| 86 | { |
| 87 | uint64_t hash = 0; |
| 88 | const uint8_t* end = buf + size; |
| 89 | |
| 90 | while (buf < end) { |
| 91 | hash += (uint64_t)*buf++; |
| 92 | hash += hash << 10; |
| 93 | hash ^= hash >> 6; |
| 94 | } |
| 95 | |
| 96 | hash += hash << 3; |
| 97 | hash ^= hash >> 11; |
| 98 | hash += hash << 15; |
| 99 | |
| 100 | return hash; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | // 32-bit pointer hash. |
| 105 | static inline uint32_t |
| 106 | cf_hash_ptr32(const void* p_ptr) |
| 107 | { |
| 108 | return (uint32_t)((*(const uint64_t*)p_ptr * 0xe221f97c30e94e1dULL) >> 32); |
| 109 | } |
| 110 | |