Blender V4.3
hash_mm2a.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
21#include "BLI_compiler_attrs.h"
22
23#include "BLI_hash_mm2a.hh" /* own include */
24
25/* Helpers. */
26#define MM2A_M 0x5bd1e995
27
28#define MM2A_MIX(h, k) \
29 { \
30 (k) *= MM2A_M; \
31 (k) ^= (k) >> 24; \
32 (k) *= MM2A_M; \
33 (h) = ((h) * MM2A_M) ^ (k); \
34 } \
35 (void)0
36
37#define MM2A_MIX_FINALIZE(h) \
38 { \
39 (h) ^= (h) >> 13; \
40 (h) *= MM2A_M; \
41 (h) ^= (h) >> 15; \
42 } \
43 (void)0
44
45static void mm2a_mix_tail(BLI_HashMurmur2A *mm2, const uchar **data, size_t *len)
46{
47 while (*len && ((*len < 4) || mm2->count)) {
48 mm2->tail |= uint32_t(**data) << (mm2->count * 8);
49
50 mm2->count++;
51 (*len)--;
52 (*data)++;
53
54 if (mm2->count == 4) {
55 MM2A_MIX(mm2->hash, mm2->tail);
56 mm2->tail = 0;
57 mm2->count = 0;
58 }
59 }
60}
61
63{
64 mm2->hash = seed;
65 mm2->tail = 0;
66 mm2->count = 0;
67 mm2->size = 0;
68}
69
70void BLI_hash_mm2a_add(BLI_HashMurmur2A *mm2, const uchar *data, size_t len)
71{
72 mm2->size += uint32_t(len);
73
74 mm2a_mix_tail(mm2, &data, &len);
75
76 for (; len >= 4; data += 4, len -= 4) {
77 uint32_t k = *(const uint32_t *)data;
78
79 MM2A_MIX(mm2->hash, k);
80 }
81
82 mm2a_mix_tail(mm2, &data, &len);
83}
84
86{
87 BLI_hash_mm2a_add(mm2, (const uchar *)&data, sizeof(data));
88}
89
91{
92 MM2A_MIX(mm2->hash, mm2->tail);
93 MM2A_MIX(mm2->hash, mm2->size);
94
96
97 return mm2->hash;
98}
99
101{
102 /* Initialize the hash to a 'random' value */
103 uint32_t h = seed ^ len;
104
105 /* Mix 4 bytes at a time into the hash */
106 for (; len >= 4; data += 4, len -= 4) {
107 uint32_t k = *(uint32_t *)data;
108
109 MM2A_MIX(h, k);
110 }
111
112 /* Handle the last few bytes of the input array */
113 switch (len) {
114 case 3:
115 h ^= data[2] << 16;
117 case 2:
118 h ^= data[1] << 8;
120 case 1:
121 h ^= data[0];
122 h *= MM2A_M;
123 }
124
125 /* Do a few final mixes of the hash to ensure the last few bytes are well-incorporated. */
127
128 return h;
129}
#define ATTR_FALLTHROUGH
unsigned char uchar
static unsigned long seed
Definition btSoftBody.h:39
int len
void BLI_hash_mm2a_init(BLI_HashMurmur2A *mm2, uint32_t seed)
Definition hash_mm2a.cc:62
static void mm2a_mix_tail(BLI_HashMurmur2A *mm2, const uchar **data, size_t *len)
Definition hash_mm2a.cc:45
void BLI_hash_mm2a_add_int(BLI_HashMurmur2A *mm2, int data)
Definition hash_mm2a.cc:85
uint32_t BLI_hash_mm2(const uchar *data, size_t len, uint32_t seed)
Definition hash_mm2a.cc:100
uint32_t BLI_hash_mm2a_end(BLI_HashMurmur2A *mm2)
Definition hash_mm2a.cc:90
void BLI_hash_mm2a_add(BLI_HashMurmur2A *mm2, const uchar *data, size_t len)
Definition hash_mm2a.cc:70
#define MM2A_MIX_FINALIZE(h)
Definition hash_mm2a.cc:37
#define MM2A_M
Definition hash_mm2a.cc:26
#define MM2A_MIX(h, k)
Definition hash_mm2a.cc:28
unsigned int uint32_t
Definition stdint.h:80