aboutsummaryrefslogtreecommitdiff
path: root/any_hash.h
blob: f5aad975c0838d1b780699b64af81402830b9292 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// any_hash
//
// A single-file library that provides a simple implementation of the
// xxHash xxh32 and xxh64 hashing algorithms.
//
// To use this library you should choose a suitable file to put the
// implementation and define ANY_HASH_IMPLEMENT. For example
//
//    #define ANY_HASH_IMPLEMENT
//    #include "any_hash.h"
//
// Additionally, you can customize the library behavior by defining certain
// macros in the file where you put the implementation. You can see which are
// supported by reading the code guarded by ANY_HASH_IMPLEMENT.
//
// This library is licensed under the terms of the MIT license.
// A copy of the license is included at the end of this file.
//

// Rationale
//
// Why use this library when there is the official xxHash library?
//
// The answer is simplicity: the xxHash library is a single header with
// more than 7000 lines of code. This becomes a burden both for shipping
// and compiling the header file in a (especially small) project.
//
// Meanwhile this any_hash is a few hundreds lines at most.
// This comes at a cost thought: less architecture specific optimizations,
// less hashing algorithms, slightly less performance overall.
//
// Note however that the library is still very fast and the perfomance
// difference will not be noticeable in small projects.
//
// How does this library perform?
//
// As said before, it does far less optimizations compared to xxHash.
// It really makes a difference if you are using an architecture with
// modern instruction sets like AVX2.
//
// However any_hash is still quite fast and performant compared to most
// handcoded hashing solutions and comes in a convenient small package.
//

// Perfomance comparison
//
//   hash function  | large inputs |   small inputs
//                  |              |
//    xxHash XXH3   |  17.07 GB/s  |  129813995 hash/s
//    xxHash XXH32  |   6.02 GB/s  |  87749462 hash/s
//    xxHash XXH64  |  11.72 GB/s  |  81670020 hash/s
//    xxHash XXH128 |  16.71 GB/s  | 112659406 hash/s
//     any_xxh32    |   6.10 GB/s  |  83403749 hash/s
//     any_xxh64    |  11.89 GB/s  |  72925776 hash/s
//
// The speed was measured with the 'benchHash' program provided by
// the xxHash library and without AVX2 enabled.
//

#ifndef ANY_HASH_INCLUDE
#define ANY_HASH_INCLUDE

#include <stdint.h>
#include <stddef.h>
#include <stdbool.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_LITTLE_ENDIAN
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ || \
    defined(__BIG_ENDIAN__) || defined(__BIG_ENDIAN) || defined(_BIG_ENDIAN)
#define ANY_HASH_LITTLE_ENDIAN 0
#else
#define ANY_HASH_LITTLE_ENDIAN 1
#endif
#endif
#else
#define ANY_HASH_LITTLE_ENDIAN any_hash_little_endian()

static inline int any_hash_little_endian(void)
{
    const union {
        uint32_t uint;
        char bytes[4];
    } t = { 1 };
    return t.bytes[0];
}
#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

// MIT License
//
// Copyright (c) 2024 Federico Angelilli
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//