cuSBF
Loading...
Searching...
No Matches
hashutil.cuh
Go to the documentation of this file.
1#pragma once
2
3#include <cuda/std/bit>
4
5#include <cstddef>
6#include <cstdint>
7#include <cstring>
8
48
49constexpr uint64_t PRIME64_1 = 11400714785074694791ULL;
50constexpr uint64_t PRIME64_2 = 14029467366897019727ULL;
51constexpr uint64_t PRIME64_3 = 1609587929392839161ULL;
52constexpr uint64_t PRIME64_4 = 9650029242287828579ULL;
53constexpr uint64_t PRIME64_5 = 2870177450012600261ULL;
54
57 return cuda::std::rotl(x, static_cast<int>(r));
58}
59
61template <typename T>
63 T chunk;
64 memcpy(&chunk, data + index * sizeof(T), sizeof(T));
65 return chunk;
66}
67
70 h ^= h >> 33;
71 h *= PRIME64_2;
72 h ^= h >> 29;
73 h *= PRIME64_3;
74 h ^= h >> 32;
75 return h;
76}
77
86template <typename T>
88 const auto* bytes = reinterpret_cast<const uint8_t*>(&key);
89 uint64_t size = sizeof(T);
90 uint64_t offset = 0;
92
93 // Process 32-byte chunks
94 if (size >= 32) {
95 uint64_t limit = size - 32;
100
101 do {
102 const uint64_t pipeline_offset = offset / 8;
104 v1 = rotl64(v1, 31);
105 v1 *= PRIME64_1;
107 v2 = rotl64(v2, 31);
108 v2 *= PRIME64_1;
110 v3 = rotl64(v3, 31);
111 v3 *= PRIME64_1;
113 v4 = rotl64(v4, 31);
114 v4 *= PRIME64_1;
115 offset += 32;
116 } while (offset <= limit);
117
118 h64 = rotl64(v1, 1) + rotl64(v2, 7) + rotl64(v3, 12) + rotl64(v4, 18);
119
120 v1 *= PRIME64_2;
121 v1 = rotl64(v1, 31);
122 v1 *= PRIME64_1;
123 h64 ^= v1;
125
126 v2 *= PRIME64_2;
127 v2 = rotl64(v2, 31);
128 v2 *= PRIME64_1;
129 h64 ^= v2;
131
132 v3 *= PRIME64_2;
133 v3 = rotl64(v3, 31);
134 v3 *= PRIME64_1;
135 h64 ^= v3;
137
138 v4 *= PRIME64_2;
139 v4 = rotl64(v4, 31);
140 v4 *= PRIME64_1;
141 h64 ^= v4;
143 } else {
144 h64 = seed + PRIME64_5;
145 }
146
147 h64 += size;
148
149 // Process remaining 8-byte chunks
150 if ((size % 32) >= 8) {
151 for (; offset <= size - 8; offset += 8) {
153 k1 = rotl64(k1, 31) * PRIME64_1;
154 h64 ^= k1;
155 h64 = rotl64(h64, 27) * PRIME64_1 + PRIME64_4;
156 }
157 }
158
159 // Process remaining 4-byte chunks
160 if ((size % 8) >= 4) {
161 for (; offset <= size - 4; offset += 4) {
162 h64 ^= (load_chunk<uint32_t>(bytes, offset / 4) & 0xffffffffULL) * PRIME64_1;
163 h64 = rotl64(h64, 23) * PRIME64_2 + PRIME64_3;
164 }
165 }
166
167 // Process remaining bytes
168 if (size % 4) {
169 while (offset < size) {
170 h64 ^= (bytes[offset] & 0xff) * PRIME64_5;
171 h64 = rotl64(h64, 11) * PRIME64_1;
172 ++offset;
173 }
174 }
175
176 return finalize(h64);
177}
178
179} // namespace cusbf::detail::xxhash
180
181namespace cusbf::detail {
182
193 key *= 0x9e3779b97f4a7c15ULL;
194 key ^= key >> 33;
195 return key;
196}
197
207// sufficient for minimizer (shard) selection where only uniformity matters,
208// not full avalanche quality.
210 return key * 0x9E3779B97F4A7C15ULL;
211}
212
213} // namespace cusbf::detail
XXHash_64 implementation from.
Definition hashutil.cuh:47
constexpr uint64_t PRIME64_3
Definition hashutil.cuh:51
constexpr uint64_t PRIME64_1
Definition hashutil.cuh:49
constexpr __host__ __device__ __forceinline__ uint64_t rotl64(uint64_t x, int8_t r)
Rotates x left by r bits.
Definition hashutil.cuh:56
__host__ __device__ __forceinline__ T load_chunk(const uint8_t *data, uint64_t index)
Loads a chunk of type T from data at byte offset index*sizeof(T).
Definition hashutil.cuh:62
constexpr __host__ __device__ __forceinline__ uint64_t finalize(uint64_t h)
Applies the xxHash-64 final mixing (avalanche) step.
Definition hashutil.cuh:69
constexpr uint64_t PRIME64_2
Definition hashutil.cuh:50
constexpr uint64_t PRIME64_4
Definition hashutil.cuh:52
__host__ __device__ uint64_t xxhash64(const T &key, uint64_t seed=0)
Computes the xxHash-64 digest of a value.
Definition hashutil.cuh:87
constexpr uint64_t PRIME64_5
Definition hashutil.cuh:53
constexpr __host__ __device__ __forceinline__ uint64_t minimizerHash64(uint64_t key)
Fast 64-bit hash sufficient for uniform minimizer selection.
Definition hashutil.cuh:209
constexpr __host__ __device__ __forceinline__ uint64_t hash64(uint64_t key)
Fast 64-bit integer hash (non-cryptographic).
Definition hashutil.cuh:192
consteval bool separatorPositionAlwaysEncodesInvalid(char *input, uint64_t separatorPosition, uint64_t index)
Recursively tests whether placing the separator byte at any position in an input of valid bytes alway...
Definition Alphabet.cuh:37