aboutsummaryrefslogtreecommitdiff
path: root/any_hash.h
blob: 088c444d48dde0c86bd9f5ad4ca1bb8e36705625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#ifndef ANY_HASH_INCLUDE
#define ANY_HASH_INCLUDE

#include <stdint.h>
#include <stddef.h>

#ifndef ANY_HASH_NO_XXH32

typedef uint32_t any_hash32_t;

any_hash32_t any_hash_xxh32(const uint8_t *data, size_t length, any_hash32_t seed);

#endif

#ifndef ANY_HASH_NO_XXH64

typedef uint64_t any_hash64_t;

any_hash64_t any_hash_xxh64(const uint8_t *data, size_t length, any_hash64_t seed);

#endif

#endif

#ifdef ANY_HASH_IMPLEMENT

#include <string.h>

#define ANY_HASH_ALIGNED 0

#define ANY_HASH_ROTL1 1
#define ANY_HASH_ROTL2 7
#define ANY_HASH_ROTL3 12
#define ANY_HASH_ROTL4 18

#ifndef ANY_HASH_LIKELY
#ifdef __has_builtin
#if __has_builtin(__builtin_expect)
#define ANY_HASH_LIKELY(...) __builtin_expect(!!(__VA_ARGS__), 1)
#endif
#endif

#ifndef ANY_HASH_LIKELY
#define ANY_HASH_LIKELY(...)
#endif
#endif

#ifndef ANY_HASH_RUNTIME_ENDIAN
#ifndef ANY_HASH_LE
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
	defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)
#define ANY_HASH_LE 0
#else
#define ANY_HASH_LE 1
#endif
#endif

static bool any_hash_little_endian = ANY_HASH_LE;
#else
static int any_hash_test_endian = 1;
static bool any_hash_little_endian = *((char *)&any_hash_test_endian);
#endif


#ifndef ANY_HASH_NO_XXH32

#define ANY_HASH_BYTES32 sizeof(any_hash32_t)
#define ANY_HASH_DATALEN32 16

#define ANY_HASH_ALIGN32 3
#define ANY_HASH_ROUND32 13

#define ANY_HASH_AV32_1 15
#define ANY_HASH_AV32_2 13
#define ANY_HASH_AV32_3 16

#define ANY_HASH_PROC32_1 17
#define ANY_HASH_PROC32_2 11

#define ANY_HASH_PRIME32_1 2654435761u
#define ANY_HASH_PRIME32_2 2246822519u
#define ANY_HASH_PRIME32_3 3266489917u
#define ANY_HASH_PRIME32_4 668265263u
#define ANY_HASH_PRIME32_5 374761393u

#ifdef __has_builtin
#if __has_builtin(__builtin_bswap32)
#define ANY_HASH_SWAP32 __builtin_bswap32
#endif

#if __has_builtin(__builtin_rotateleft32)
#define ANY_HASH_ROTL32 __builtin_rotateleft32
#endif
#endif

#ifndef ANY_HASH_SWAP32
#define ANY_HASH_SWAP32 any_hash_swap32

static inline any_hash32_t any_hash_swap32(any_hash32_t hash)
{
    return ((hash << 24) & 0xff000000) |
           ((hash <<  8) & 0x00ff0000) |
           ((hash <<  8) & 0x0000ff00) |
           ((hash << 24) & 0x000000ff);
}
#endif

#ifndef ANY_HASH_ROTL32
#define ANY_HASH_ROTL32 any_hash_rotl32

static inline any_hash32_t any_hash_rotl32(any_hash32_t hash, uint32_t bits)
{
    return (hash << bits) | (hash >> (32 - bits));
}
#endif


static inline any_hash32_t any_hash_fetch32(const uint8_t *data, int_fast32_t align)
{
    if (ANY_HASH_LIKELY(align == ANY_HASH_ALIGNED))
        return any_hash_little_endian
            ? *(const any_hash32_t *)data
            : ANY_HASH_SWAP32(*(const any_hash32_t *)data);

    any_hash32_t hash;
    memcpy(&hash, data, 4);

    return any_hash_little_endian
        ? hash
        : ANY_HASH_SWAP32(hash);
}

static inline any_hash32_t any_hash_avalanche32(any_hash32_t hash)
{
    hash ^= hash >> ANY_HASH_AV32_1;
    hash *= ANY_HASH_PRIME32_2;
    hash ^= hash >> ANY_HASH_AV32_2;
    hash *= ANY_HASH_PRIME32_3;
    hash ^= hash >> ANY_HASH_AV32_3;
    return hash;
}

static any_hash32_t any_hash_round32(any_hash32_t hash, any_hash32_t next)
{
    hash += next * ANY_HASH_PRIME32_2;
    hash = ANY_HASH_ROTL32(hash, ANY_HASH_ROUND32);
    hash *= ANY_HASH_PRIME32_1;
    return hash;
}

any_hash32_t any_hash_xxh32(const uint8_t *data, size_t length, any_hash32_t seed)
{
    any_hash32_t hash;
    const int_fast32_t align = (uintptr_t)data & ANY_HASH_ALIGN32;

    if (length >= ANY_HASH_DATALEN32) {
        const uint8_t *const end = data + length - ANY_HASH_DATALEN32;

        any_hash32_t st1 = seed + ANY_HASH_PRIME32_1 + ANY_HASH_PRIME32_2;
        any_hash32_t st2 = seed + ANY_HASH_PRIME32_2;
        any_hash32_t st3 = seed;
        any_hash32_t st4 = seed - ANY_HASH_PRIME32_1;

        do {
            st1 = any_hash_round32(st1, any_hash_fetch32(data, align));
            data += ANY_HASH_BYTES32;
            st2 = any_hash_round32(st2, any_hash_fetch32(data, align));
            data += ANY_HASH_BYTES32;
            st3 = any_hash_round32(st3, any_hash_fetch32(data, align));
            data += ANY_HASH_BYTES32;
            st4 = any_hash_round32(st4, any_hash_fetch32(data, align));
            data += ANY_HASH_BYTES32;
        } while (data <= end);

        hash = ANY_HASH_ROTL32(st1, ANY_HASH_ROTL1) + ANY_HASH_ROTL32(st2, ANY_HASH_ROTL2)
             + ANY_HASH_ROTL32(st3, ANY_HASH_ROTL3) + ANY_HASH_ROTL32(st4, ANY_HASH_ROTL4);
    } else
        hash = seed + ANY_HASH_PRIME32_5;

    hash += length;
    length &= (ANY_HASH_DATALEN32 - 1);

    while (length >= 4) {
        hash += any_hash_fetch32(data, align) * ANY_HASH_PRIME32_3;
        hash = ANY_HASH_ROTL32(hash, ANY_HASH_PROC32_1) * ANY_HASH_PRIME32_4;
        data += ANY_HASH_BYTES32;
        length -= ANY_HASH_BYTES32;
    }

    while (length > 0) {
        hash += (*data++) * ANY_HASH_PRIME32_5;
        hash = ANY_HASH_ROTL32(hash, ANY_HASH_PROC32_2) * ANY_HASH_PRIME32_1;
        --length;
    }

    return any_hash_avalanche32(hash);
}

#endif

#ifndef ANY_HASH_NO_XXH64

#define ANY_HASH_BYTES64 sizeof(any_hash64_t)
#define ANY_HASH_DATALEN64 32

#define ANY_HASH_ALIGN64 7
#define ANY_HASH_ROUND64 31

#define ANY_HASH_AV64_1 33
#define ANY_HASH_AV64_2 29
#define ANY_HASH_AV64_3 32

#define ANY_HASH_PROC64_1 27
#define ANY_HASH_PROC64_2 23
#define ANY_HASH_PROC64_3 11

#define ANY_HASH_PRIME64_1 11400714785074694791ull
#define ANY_HASH_PRIME64_2 14029467366897019727ull
#define ANY_HASH_PRIME64_3 1609587929392839161ull
#define ANY_HASH_PRIME64_4 9650029242287828579ull
#define ANY_HASH_PRIME64_5 2870177450012600261ull

#ifdef __has_builtin
#if __has_builtin(__builtin_bswap64)
#define ANY_HASH_SWAP64 __builtin_bswap64
#endif

#if __has_builtin(__builtin_rotateleft64)
#define ANY_HASH_ROTL64 __builtin_rotateleft64
#endif
#endif

#ifndef ANY_HASH_SWAP64
#define ANY_HASH_SWAP64 any_hash_swap64

static inline any_hash64_t any_hash_swap64(any_hash64_t hash)
{
    return ((hash << 56) & 0xff00000000000000ull) |
           ((hash << 40) & 0x00ff000000000000ull) |
           ((hash << 24) & 0x0000ff0000000000ull) |
           ((hash <<  8) & 0x000000ff00000000ull) |
           ((hash >>  8) & 0x00000000ff000000ull) |
           ((hash >> 24) & 0x0000000000ff0000ull) |
           ((hash >> 40) & 0x000000000000ff00ull) |
           ((hash >> 56) & 0x00000000000000ffull);
}
#endif

#ifndef ANY_HASH_ROTL64
#define ANY_HASH_ROTL64 any_hash_rotl64

static inline any_hash64_t any_hash_rotl64(any_hash64_t hash, uint32_t bits)
{
    return (hash << bits) | (hash >> (64 - bits));
}
#endif

static inline any_hash64_t any_hash_fetch64(const uint8_t *data, int_fast32_t align)
{
    if (ANY_HASH_LIKELY(align == ANY_HASH_ALIGNED))
        return any_hash_little_endian
            ? *(const any_hash64_t *)data
            : ANY_HASH_SWAP64(*(const any_hash64_t *)data);

    any_hash64_t hash;
    memcpy(&hash, data, 8);

    return any_hash_little_endian
        ? hash
        : ANY_HASH_SWAP64(hash);
}

static inline any_hash64_t any_hash_avalanche64(any_hash64_t hash)
{
    hash ^= hash >> ANY_HASH_AV64_1;
    hash *= ANY_HASH_PRIME64_2;
    hash ^= hash >> ANY_HASH_AV64_2;
    hash *= ANY_HASH_PRIME64_3;
    hash ^= hash >> ANY_HASH_AV64_3;
    return hash;
}

static any_hash64_t any_hash_round64(any_hash64_t hash, any_hash64_t next)
{
    hash += next * ANY_HASH_PRIME64_2;
    hash = ANY_HASH_ROTL64(hash, ANY_HASH_ROUND64);
    hash *= ANY_HASH_PRIME64_1;
    return hash;
}

static any_hash64_t any_hash_round64_merge(any_hash64_t hash, any_hash64_t next)
{
    hash ^= any_hash_round64(0, next);
    hash = hash * ANY_HASH_PRIME64_1 + ANY_HASH_PRIME64_4;
    return hash;
}

any_hash64_t any_hash_xxh64(const uint8_t *data, size_t length, any_hash64_t seed)
{
    any_hash64_t hash;
    const int_fast32_t align = (uintptr_t)data & ANY_HASH_ALIGN64;

    if (length >= ANY_HASH_DATALEN64) {
        const uint8_t *const end = data + length - ANY_HASH_DATALEN64;

        any_hash64_t st1 = seed + ANY_HASH_PRIME64_1 + ANY_HASH_PRIME64_2;
        any_hash64_t st2 = seed + ANY_HASH_PRIME64_2;
        any_hash64_t st3 = seed;
        any_hash64_t st4 = seed - ANY_HASH_PRIME64_1;

        do {
            st1 = any_hash_round64(st1, any_hash_fetch64(data, align));
            data += ANY_HASH_BYTES64;
            st2 = any_hash_round64(st2, any_hash_fetch64(data, align));
            data += ANY_HASH_BYTES64;
            st3 = any_hash_round64(st3, any_hash_fetch64(data, align));
            data += ANY_HASH_BYTES64;
            st4 = any_hash_round64(st4, any_hash_fetch64(data, align));
            data += ANY_HASH_BYTES64;
        } while (data <= end);

        hash = ANY_HASH_ROTL64(st1, ANY_HASH_ROTL1) + ANY_HASH_ROTL64(st2, ANY_HASH_ROTL2)
             + ANY_HASH_ROTL64(st3, ANY_HASH_ROTL3) + ANY_HASH_ROTL64(st4, ANY_HASH_ROTL4);

        hash = any_hash_round64_merge(hash, st1);
        hash = any_hash_round64_merge(hash, st2);
        hash = any_hash_round64_merge(hash, st3);
        hash = any_hash_round64_merge(hash, st4);
    } else
        hash = seed + ANY_HASH_PRIME64_5;

    hash += length;
    length &= (ANY_HASH_DATALEN64 - 1);

    while (length >= 8) {
        hash ^= any_hash_round64(0, any_hash_fetch64(data, align));
        hash = ANY_HASH_ROTL64(hash, ANY_HASH_PROC64_1) * ANY_HASH_PRIME64_1 + ANY_HASH_PRIME64_4;
        data += ANY_HASH_BYTES64;
        length -= ANY_HASH_BYTES64;
    }

    while (length >= 4) {
        hash ^= (any_hash64_t)(any_hash_fetch32(data, align)) * ANY_HASH_PRIME64_1;
        hash = ANY_HASH_ROTL64(hash, ANY_HASH_PROC64_2) * ANY_HASH_PRIME64_2 + ANY_HASH_PRIME64_3;
        data += ANY_HASH_BYTES32;
        length -= ANY_HASH_BYTES32;
    }

    while (length > 0) {
        hash ^= (*data++) * ANY_HASH_PRIME64_5;
        hash = ANY_HASH_ROTL64(hash, ANY_HASH_PROC64_3) * ANY_HASH_PRIME64_1;
        --length;
    }

    return any_hash_avalanche64(hash);
}

#endif

#endif