Branch data Line data Source code
1 : : // Copyright 2012-2025 David Robillard <d@drobilla.net> 2 : : // SPDX-License-Identifier: ISC 3 : : 4 : : #include <zix/attributes.h> 5 : : #include <zix/digest.h> 6 : : 7 : : #include "qualifiers.h" 8 : : 9 : : #include <assert.h> 10 : : #include <stdint.h> 11 : : #include <string.h> 12 : : 13 : : #if defined(__clang__) && __clang_major__ >= 12 14 : : # define FALLTHROUGH() __attribute__((fallthrough)) 15 : : #elif defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 7 16 : : # define FALLTHROUGH() __attribute__((fallthrough)) 17 : : #else 18 : : # define FALLTHROUGH() 19 : : #endif 20 : : 21 : : /* 22 : : 64-bit hash: Essentially fasthash64, implemented here in an aligned/padded 23 : : and a general UB-free variant. 24 : : */ 25 : : 26 : : static inline uint64_t 27 : 1932408 : mix64(uint64_t h) 28 : : { 29 : 1932408 : h ^= h >> 23U; 30 : 1932408 : h *= 0x2127599BF4325C37ULL; 31 : 1932408 : h ^= h >> 47U; 32 : 1932408 : return h; 33 : : } 34 : : 35 : : ZIX_NONBLOCKING uint64_t 36 : 574378 : zix_digest64(const uint64_t seed, const void* const buf, const size_t len) 37 : : { 38 : 574378 : ZIX_CONSTEXPR uint64_t m = 0x880355F21E6D1965ULL; 39 : : 40 : : // Process as many 64-bit blocks as possible 41 : 574378 : const size_t n_blocks = len / sizeof(uint64_t); 42 : 574378 : const uint8_t* data = (const uint8_t*)buf; 43 : 574378 : const uint8_t* const blocks_end = data + (n_blocks * sizeof(uint64_t)); 44 : 574378 : uint64_t h = seed ^ (len * m); 45 [ + + ]: 1148781 : for (; data != blocks_end; data += sizeof(uint64_t)) { 46 : 574403 : uint64_t k = 0U; 47 : 574403 : memcpy(&k, data, sizeof(uint64_t)); 48 : : 49 : 574403 : h ^= mix64(k); 50 : 574403 : h *= m; 51 : : } 52 : : 53 : : // Process any trailing bytes 54 : 574378 : const uint8_t* const tail = blocks_end; 55 : 574378 : uint64_t v = 0U; 56 [ + + + + : 574378 : switch (len & 7U) { + + - + ] 57 : 574263 : case 7: 58 : 574263 : v |= (uint64_t)tail[6] << 48U; 59 : : FALLTHROUGH(); 60 : 574269 : case 6: 61 : 574269 : v |= (uint64_t)tail[5] << 40U; 62 : : FALLTHROUGH(); 63 : 574274 : case 5: 64 : 574274 : v |= (uint64_t)tail[4] << 32U; 65 : : FALLTHROUGH(); 66 : 574278 : case 4: 67 : 574278 : v |= (uint64_t)tail[3] << 24U; 68 : : FALLTHROUGH(); 69 : 574281 : case 3: 70 : 574281 : v |= (uint64_t)tail[2] << 16U; 71 : : FALLTHROUGH(); 72 : 574283 : case 2: 73 : 574283 : v |= (uint64_t)tail[1] << 8U; 74 : : FALLTHROUGH(); 75 : 574283 : case 1: 76 : 574283 : v |= (uint64_t)tail[0]; 77 : : 78 : 574283 : h ^= mix64(v); 79 : 574283 : h *= m; 80 : : } 81 : : 82 : 574378 : return mix64(h); 83 : : } 84 : : 85 : : ZIX_NONBLOCKING uint64_t 86 : 69774 : zix_digest64_aligned(const uint64_t seed, const void* const buf, size_t len) 87 : : { 88 : 69774 : ZIX_CONSTEXPR uint64_t m = 0x880355F21E6D1965ULL; 89 : : 90 [ - + ]: 69774 : assert((uintptr_t)buf % sizeof(uint64_t) == 0U); 91 [ - + ]: 69774 : assert(len % sizeof(uint64_t) == 0U); 92 : : 93 : 69774 : const uint64_t* const blocks = (const uint64_t*)buf; 94 : 69774 : const size_t n_blocks = len / sizeof(uint64_t); 95 : 69774 : uint64_t h = seed ^ (len * m); 96 : : 97 [ + + ]: 209344 : for (size_t i = 0U; i < n_blocks; ++i) { 98 : 139570 : h ^= mix64(blocks[i]); 99 : 139570 : h *= m; 100 : : } 101 : : 102 : 69774 : return mix64(h); 103 : : } 104 : : 105 : : /* 106 : : 32-bit hash: Essentially murmur3, reimplemented here in an aligned/padded and 107 : : a general UB-free variant. 108 : : */ 109 : : 110 : : /** 111 : : Rotate left by some count of bits. 112 : : 113 : : This is recognized by any halfway decent compiler and compiled to a single 114 : : instruction on architectures that have one. 115 : : */ 116 : : static inline uint32_t 117 : 523090 : rotl32(const uint32_t val, const uint32_t bits) 118 : : { 119 : 523090 : return ((val << bits) | (val >> (32U - bits))); 120 : : } 121 : : 122 : : static inline uint32_t 123 : 69774 : mix32(uint32_t h) 124 : : { 125 : 69774 : h ^= h >> 16U; 126 : 69774 : h *= 0x85EBCA6BU; 127 : 69774 : h ^= h >> 13U; 128 : 69774 : h *= 0xC2B2AE35U; 129 : 69774 : h ^= h >> 16U; 130 : 69774 : return h; 131 : : } 132 : : 133 : : ZIX_NONBLOCKING uint32_t 134 : 35062 : zix_digest32(const uint32_t seed, const void* const buf, const size_t len) 135 : : { 136 : 35062 : ZIX_CONSTEXPR uint32_t c1 = 0xCC9E2D51U; 137 : 35062 : ZIX_CONSTEXPR uint32_t c2 = 0x1B873593U; 138 : : 139 : : // Process as many 32-bit blocks as possible 140 : 35062 : const size_t n_blocks = len / sizeof(uint32_t); 141 : 35062 : const uint8_t* data = (const uint8_t*)buf; 142 : 35062 : const uint8_t* const blocks_end = data + (n_blocks * sizeof(uint32_t)); 143 : 35062 : uint32_t h = seed; 144 [ + + ]: 140249 : for (; data != blocks_end; data += sizeof(uint32_t)) { 145 : 105187 : uint32_t k = 0U; 146 : 105187 : memcpy(&k, data, sizeof(uint32_t)); 147 : : 148 : 105187 : k *= c1; 149 : 105187 : k = rotl32(k, 15); 150 : 105187 : k *= c2; 151 : : 152 : 105187 : h ^= k; 153 : 105187 : h = rotl32(h, 13); 154 : 105187 : h = (h * 5U) + 0xE6546B64U; 155 : : } 156 : : 157 : : // Process any trailing bytes 158 : 35062 : uint32_t k = 0U; 159 [ + - - + ]: 35062 : switch (len & 3U) { 160 : 35034 : case 3U: 161 : 35034 : k ^= (uint32_t)data[2U] << 16U; 162 : : FALLTHROUGH(); 163 : 35034 : case 2U: 164 : 35034 : k ^= (uint32_t)data[1U] << 8U; 165 : : FALLTHROUGH(); 166 : 35034 : case 1U: 167 : 35034 : k ^= (uint32_t)data[0U]; 168 : : 169 : 35034 : k *= c1; 170 : 35034 : k = rotl32(k, 15U); 171 : 35034 : k *= c2; 172 : 35034 : h ^= k; 173 : : } 174 : : 175 : 35062 : return mix32(h ^ (uint32_t)len); 176 : : } 177 : : 178 : : ZIX_NONBLOCKING uint32_t 179 : 34712 : zix_digest32_aligned(const uint32_t seed, 180 : : const void* const buf, 181 : : const size_t len) 182 : : { 183 : 34712 : ZIX_CONSTEXPR uint32_t c1 = 0xCC9E2D51U; 184 : 34712 : ZIX_CONSTEXPR uint32_t c2 = 0x1B873593U; 185 : : 186 [ - + ]: 34712 : assert((uintptr_t)buf % sizeof(uint32_t) == 0U); 187 [ - + ]: 34712 : assert(len % sizeof(uint32_t) == 0U); 188 : : 189 : 34712 : const uint32_t* const blocks = (const uint32_t*)buf; 190 : 34712 : const size_t n_blocks = len / sizeof(uint32_t); 191 : 34712 : uint32_t h = seed; 192 [ + + ]: 173553 : for (size_t i = 0U; i < n_blocks; ++i) { 193 : 138841 : uint32_t k = blocks[i]; 194 : : 195 : 138841 : k *= c1; 196 : 138841 : k = rotl32(k, 15); 197 : 138841 : k *= c2; 198 : : 199 : 138841 : h ^= k; 200 : 138841 : h = rotl32(h, 13); 201 : 138841 : h = (h * 5U) + 0xE6546B64U; 202 : : } 203 : : 204 : 34712 : return mix32(h ^ (uint32_t)len); 205 : : } 206 : : 207 : : // Native word size wrapper 208 : : 209 : : ZIX_NONBLOCKING size_t 210 : 539465 : zix_digest(const size_t seed, const void* const buf, const size_t len) 211 : : { 212 : : #if UINTPTR_MAX >= UINT64_MAX 213 : 539465 : return zix_digest64(seed, buf, len); 214 : : #else 215 : : return zix_digest32(seed, buf, len); 216 : : #endif 217 : : } 218 : : 219 : : ZIX_NONBLOCKING size_t 220 : 35237 : zix_digest_aligned(const size_t seed, const void* const buf, const size_t len) 221 : : { 222 : : #if UINTPTR_MAX >= UINT64_MAX 223 : 35237 : return zix_digest64_aligned(seed, buf, len); 224 : : #else 225 : : return zix_digest32_aligned(seed, buf, len); 226 : : #endif 227 : : }