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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
// Copyright (C) 2011  Davis E. King ([email protected])
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_MURMUR_HAsH_3_Hh_ 
#define DLIB_MURMUR_HAsH_3_Hh_ 

#include "murmur_hash3_abstract.h"
#include "../uintn.h"
#include <utility>
#include <string.h>

namespace dlib
{
    //-----------------------------------------------------------------------------
    // The original MurmurHash3 code was written by Austin Appleby, and is placed 
    // in the public domain. The author hereby disclaims copyright to this source code.
    // The code in this particular file was modified by Davis E. King.  In
    // particular, endian-swapping was added along with some other minor code
    // changes like avoiding strict aliasing violations.


    //-----------------------------------------------------------------------------
    // Platform-specific functions and macros

    // Microsoft Visual Studio


#if ((defined(__GNUC__) && __GNUC__ >= 7) || defined(__clang__))
#define DLIB_FALLTHROUGH [[fallthrough]]
#else
#define DLIB_FALLTHROUGH 
#endif

#if defined(_MSC_VER)

#define DLIB_FORCE_INLINE	__forceinline

#include <stdlib.h>

#define DLIB_ROTL32(x,y)	_rotl(x,y)
#define DLIB_ROTL64(x,y)	_rotl64(x,y)

#define DLIB_BIG_CONSTANT(x) (x)

    // Other compilers

#else	// defined(_MSC_VER)

#define	DLIB_FORCE_INLINE __attribute__((always_inline)) inline 

    inline uint32 murmur_rotl32 ( uint32 x, int8 r )
    {
        return (x << r) | (x >> (32 - r));
    }

    inline uint64 murmur_rotl64 ( uint64 x, int8 r )
    {
        return (x << r) | (x >> (64 - r));
    }

#define	DLIB_ROTL32(x,y)	dlib::murmur_rotl32(x,y)
#define DLIB_ROTL64(x,y)	dlib::murmur_rotl64(x,y)

#define DLIB_BIG_CONSTANT(x) (x##LLU)

#endif // !defined(_MSC_VER)

// ----------------------------------------------------------------------------------------
    // Block read - if your platform needs to do endian-swapping or can only
    // handle aligned reads, do the conversion here

    DLIB_FORCE_INLINE uint32 murmur_getblock ( const uint32 * p, int i )
    {
        // The reason we do a memcpy() here instead of simply returning p[i] is because
        // doing it this way avoids violations of the strict aliasing rule when all these
        // functions are inlined into the user's code.
        uint32 temp;
        memcpy(&temp, p+i, 4);
        return temp;
    }

    DLIB_FORCE_INLINE uint32 murmur_getblock_byte_swap ( const uint32 * p, int i )
    {
        union 
        {
            uint8 bytes[4];
            uint32 val;
        } temp;

        const uint8* pp = reinterpret_cast<const uint8*>(p + i);
        temp.bytes[0] = pp[3];
        temp.bytes[1] = pp[2];
        temp.bytes[2] = pp[1];
        temp.bytes[3] = pp[0];

        return temp.val;
    }

    DLIB_FORCE_INLINE uint64 murmur_getblock ( const uint64 * p, int i )
    {
        // The reason we do a memcpy() here instead of simply returning p[i] is because
        // doing it this way avoids violations of the strict aliasing rule when all these
        // functions are inlined into the user's code.
        uint64 temp;
        memcpy(&temp, p+i, 8);
        return temp;
    }

    DLIB_FORCE_INLINE uint64 murmur_getblock_byte_swap ( const uint64 * p, int i )
    {
        union 
        {
            uint8 bytes[8];
            uint64 val;
        } temp;

        const uint8* pp = reinterpret_cast<const uint8*>(p + i);
        temp.bytes[0] = pp[7];
        temp.bytes[1] = pp[6];
        temp.bytes[2] = pp[5];
        temp.bytes[3] = pp[4];
        temp.bytes[4] = pp[3];
        temp.bytes[5] = pp[2];
        temp.bytes[6] = pp[1];
        temp.bytes[7] = pp[0];

        return temp.val;
    }

// ----------------------------------------------------------------------------------------
    // Finalization mix - force all bits of a hash block to avalanche

    DLIB_FORCE_INLINE uint32 murmur_fmix ( uint32 h )
    {
        h ^= h >> 16;
        h *= 0x85ebca6b;
        h ^= h >> 13;
        h *= 0xc2b2ae35;
        h ^= h >> 16;

        return h;
    }

// ----------------------------------------------------------------------------------------

    DLIB_FORCE_INLINE uint64 murmur_fmix ( uint64 k )
    {
        k ^= k >> 33;
        k *= DLIB_BIG_CONSTANT(0xff51afd7ed558ccd);
        k ^= k >> 33;
        k *= DLIB_BIG_CONSTANT(0xc4ceb9fe1a85ec53);
        k ^= k >> 33;

        return k;
    }

// ----------------------------------------------------------------------------------------

    inline uint32 murmur_hash3 ( 
        const void * key, 
        const int len, 
        const uint32 seed = 0
    )
    {
        const uint8 * data = (const uint8*)key;
        const int nblocks = len / 4;

        uint32 h1 = seed;

        uint32 c1 = 0xcc9e2d51;
        uint32 c2 = 0x1b873593;

        //----------
        // body

        const uint32 * blocks = (const uint32 *)(data + nblocks*4);

        bool is_little_endian = true;
        uint32 endian_test = 1;
        if (*reinterpret_cast<unsigned char*>(&endian_test) != 1)
            is_little_endian = false;


        if (is_little_endian)
        {
            for(int i = -nblocks; i; i++)
            {
                uint32 k1 = murmur_getblock(blocks,i);

                k1 *= c1;
                k1 = DLIB_ROTL32(k1,15);
                k1 *= c2;

                h1 ^= k1;
                h1 = DLIB_ROTL32(h1,13); 
                h1 = h1*5+0xe6546b64;
            }
        }
        else
        {
            for(int i = -nblocks; i; i++)
            {
                uint32 k1 = murmur_getblock_byte_swap(blocks,i);

                k1 *= c1;
                k1 = DLIB_ROTL32(k1,15);
                k1 *= c2;

                h1 ^= k1;
                h1 = DLIB_ROTL32(h1,13); 
                h1 = h1*5+0xe6546b64;
            }
        }

        //----------
        // tail

        const uint8 * tail = (const uint8*)(data + nblocks*4);

        uint32 k1 = 0;

        switch(len & 3)
        {
            case 3: k1 ^= tail[2] << 16;
                    DLIB_FALLTHROUGH; // fall through
            case 2: k1 ^= tail[1] << 8;
                    DLIB_FALLTHROUGH; // fall through
            case 1: k1 ^= tail[0];
                    k1 *= c1; k1 = DLIB_ROTL32(k1,15); k1 *= c2; h1 ^= k1;
        };

        //----------
        // finalization

        h1 ^= len;

        h1 = murmur_fmix(h1);

        return h1;
    } 

// ----------------------------------------------------------------------------------------

    inline uint32 murmur_hash3_2 ( 
        const uint32 v1,
        const uint32 v2 
    )
    {
        uint32 h1 = v2;

        uint32 c1 = 0xcc9e2d51;
        uint32 c2 = 0x1b873593;

        //----------
        // body


        uint32 k1 = v1;

        k1 *= c1;
        k1 = DLIB_ROTL32(k1,15);
        k1 *= c2;

        h1 ^= k1;
        h1 = DLIB_ROTL32(h1,13); 
        h1 = h1*5+0xe6546b64;


        //----------
        // finalization

        h1 ^= 4; // =^ by length in bytes

        h1 = murmur_fmix(h1);

        return h1;
    } 

// ----------------------------------------------------------------------------------------

    inline uint32 murmur_hash3_3 ( 
        const uint32 v1,
        const uint32 v2, 
        const uint32 v3 
    )
    {

        uint32 h1 = v3;

        uint32 c1 = 0xcc9e2d51;
        uint32 c2 = 0x1b873593;

        //----------
        // body


        uint32 k1 = v1;

        k1 *= c1;
        k1 = DLIB_ROTL32(k1,15);
        k1 *= c2;

        h1 ^= k1;
        h1 = DLIB_ROTL32(h1,13); 
        h1 = h1*5+0xe6546b64;

        k1 = v2;
        k1 *= c1;
        k1 = DLIB_ROTL32(k1,15);
        k1 *= c2;

        h1 ^= k1;
        h1 = DLIB_ROTL32(h1,13); 
        h1 = h1*5+0xe6546b64;

        //----------
        // finalization

        h1 ^= 8; // =^ by length in bytes

        h1 = murmur_fmix(h1);

        return h1;
    } 

// ----------------------------------------------------------------------------------------

    inline std::pair<uint64,uint64> murmur_hash3_128bit ( 
        const void* key, 
        const int len,
        const uint64 seed = 0
    )
    {
        const uint8 * data = (const uint8*)key;
        const int nblocks = len / 16;

        uint64 h1 = seed;
        uint64 h2 = seed;

        uint64 c1 = DLIB_BIG_CONSTANT(0x87c37b91114253d5);
        uint64 c2 = DLIB_BIG_CONSTANT(0x4cf5ad432745937f);

        //----------
        // body

        const uint64 * blocks = (const uint64 *)(data);

        bool is_little_endian = true;
        uint32 endian_test = 1;
        if (*reinterpret_cast<unsigned char*>(&endian_test) != 1)
            is_little_endian = false;


        if (is_little_endian)
        {
            for(int i = 0; i < nblocks; i++)
            {
                uint64 k1 = murmur_getblock(blocks,i*2+0);
                uint64 k2 = murmur_getblock(blocks,i*2+1);

                k1 *= c1; k1  = DLIB_ROTL64(k1,31); k1 *= c2; h1 ^= k1;

                h1 = DLIB_ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;

                k2 *= c2; k2  = DLIB_ROTL64(k2,33); k2 *= c1; h2 ^= k2;

                h2 = DLIB_ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
            }
        }
        else
        {
            for(int i = 0; i < nblocks; i++)
            {
                uint64 k1 = murmur_getblock_byte_swap(blocks,i*2+0);
                uint64 k2 = murmur_getblock_byte_swap(blocks,i*2+1);

                k1 *= c1; k1  = DLIB_ROTL64(k1,31); k1 *= c2; h1 ^= k1;

                h1 = DLIB_ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;

                k2 *= c2; k2  = DLIB_ROTL64(k2,33); k2 *= c1; h2 ^= k2;

                h2 = DLIB_ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;
            }
        }

        //----------
        // tail

        const uint8 * tail = (const uint8*)(data + nblocks*16);

        uint64 k1 = 0;
        uint64 k2 = 0;

        switch(len & 15)
        {
            case 15: k2 ^= uint64(tail[14]) << 48; DLIB_FALLTHROUGH; // fall through
            case 14: k2 ^= uint64(tail[13]) << 40; DLIB_FALLTHROUGH; // fall through
            case 13: k2 ^= uint64(tail[12]) << 32; DLIB_FALLTHROUGH; // fall through
            case 12: k2 ^= uint64(tail[11]) << 24; DLIB_FALLTHROUGH; // fall through
            case 11: k2 ^= uint64(tail[10]) << 16; DLIB_FALLTHROUGH; // fall through
            case 10: k2 ^= uint64(tail[ 9]) << 8; DLIB_FALLTHROUGH; // fall through
            case  9: k2 ^= uint64(tail[ 8]) << 0;
                     k2 *= c2; k2  = DLIB_ROTL64(k2,33); k2 *= c1; h2 ^= k2; DLIB_FALLTHROUGH; // fall through

            case  8: k1 ^= uint64(tail[ 7]) << 56; DLIB_FALLTHROUGH; // fall through
            case  7: k1 ^= uint64(tail[ 6]) << 48; DLIB_FALLTHROUGH; // fall through
            case  6: k1 ^= uint64(tail[ 5]) << 40; DLIB_FALLTHROUGH; // fall through
            case  5: k1 ^= uint64(tail[ 4]) << 32; DLIB_FALLTHROUGH; // fall through
            case  4: k1 ^= uint64(tail[ 3]) << 24; DLIB_FALLTHROUGH; // fall through
            case  3: k1 ^= uint64(tail[ 2]) << 16; DLIB_FALLTHROUGH; // fall through
            case  2: k1 ^= uint64(tail[ 1]) << 8; DLIB_FALLTHROUGH; // fall through
            case  1: k1 ^= uint64(tail[ 0]) << 0;
                     k1 *= c1; k1  = DLIB_ROTL64(k1,31); k1 *= c2; h1 ^= k1; 
        };

        //----------
        // finalization

        h1 ^= len; h2 ^= len;

        h1 += h2;
        h2 += h1;

        h1 = murmur_fmix(h1);
        h2 = murmur_fmix(h2);

        h1 += h2;
        h2 += h1;

        return std::make_pair(h1,h2);
    }

// ----------------------------------------------------------------------------------------

    inline std::pair<uint64,uint64> murmur_hash3_128bit ( 
        const uint32& v1, 
        const uint32& v2, 
        const uint32& v3, 
        const uint32& v4 
    )
    {
        uint64 h1 = 0;
        uint64 h2 = 0;

        const uint64 c1 = DLIB_BIG_CONSTANT(0x87c37b91114253d5);
        const uint64 c2 = DLIB_BIG_CONSTANT(0x4cf5ad432745937f);

        //----------
        // body

        uint64 k1 = (static_cast<uint64>(v2)<<32)|v1; 
        uint64 k2 = (static_cast<uint64>(v4)<<32)|v3; 

        k1 *= c1; k1  = DLIB_ROTL64(k1,31); k1 *= c2;

        h1 = DLIB_ROTL64(k1,27); h1 = h1*5+0x52dce729;

        k2 *= c2; k2  = DLIB_ROTL64(k2,33); k2 *= c1; 

        h2 = DLIB_ROTL64(k2,31); h2 += h1; h2 = h2*5+0x38495ab5;

        //----------
        // finalization

        h1 ^= 16; h2 ^= 16;

        h1 += h2;
        h2 += h1;

        h1 = murmur_fmix(h1);
        h2 = murmur_fmix(h2);

        h1 += h2;
        h2 += h1;

        return std::make_pair(h1,h2);
    }

// ----------------------------------------------------------------------------------------

    inline std::pair<uint64,uint64> murmur_hash3_128bit_3 ( 
        uint64 k1, 
        uint64 k2,
        uint64 k3 
    )
    {
        uint64 h1 = k3;
        uint64 h2 = k3;

        const uint64 c1 = DLIB_BIG_CONSTANT(0x87c37b91114253d5);
        const uint64 c2 = DLIB_BIG_CONSTANT(0x4cf5ad432745937f);

        //----------
        // body

        k1 *= c1; k1  = DLIB_ROTL64(k1,31); k1 *= c2; h1 ^= k1;

        h1 = DLIB_ROTL64(h1,27); h1 += h2; h1 = h1*5+0x52dce729;

        k2 *= c2; k2  = DLIB_ROTL64(k2,33); k2 *= c1; h2 ^= k2;

        h2 = DLIB_ROTL64(h2,31); h2 += h1; h2 = h2*5+0x38495ab5;

        //----------
        // finalization

        h1 ^= 16; h2 ^= 16;

        h1 += h2;
        h2 += h1;

        h1 = murmur_fmix(h1);
        h2 = murmur_fmix(h2);

        h1 += h2;
        h2 += h1;

        return std::make_pair(h1,h2);
    }

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_MURMUR_HAsH_3_Hh_