Blender
V4.3
source
blender
blenlib
intern
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
45
static
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
62
void
BLI_hash_mm2a_init
(
BLI_HashMurmur2A
*mm2,
uint32_t
seed
)
63
{
64
mm2->
hash
=
seed
;
65
mm2->
tail
= 0;
66
mm2->
count
= 0;
67
mm2->
size
= 0;
68
}
69
70
void
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
85
void
BLI_hash_mm2a_add_int
(
BLI_HashMurmur2A
*mm2,
int
data)
86
{
87
BLI_hash_mm2a_add
(mm2, (
const
uchar
*)&data,
sizeof
(data));
88
}
89
90
uint32_t
BLI_hash_mm2a_end
(
BLI_HashMurmur2A
*mm2)
91
{
92
MM2A_MIX
(mm2->
hash
, mm2->
tail
);
93
MM2A_MIX
(mm2->
hash
, mm2->
size
);
94
95
MM2A_MIX_FINALIZE
(mm2->
hash
);
96
97
return
mm2->
hash
;
98
}
99
100
uint32_t
BLI_hash_mm2
(
const
uchar
*data,
size_t
len
,
uint32_t
seed
)
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;
116
ATTR_FALLTHROUGH
;
117
case
2:
118
h ^= data[1] << 8;
119
ATTR_FALLTHROUGH
;
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. */
126
MM2A_MIX_FINALIZE
(h);
127
128
return
h;
129
}
BLI_compiler_attrs.h
ATTR_FALLTHROUGH
#define ATTR_FALLTHROUGH
Definition
BLI_compiler_attrs.h:76
BLI_hash_mm2a.hh
uchar
unsigned char uchar
Definition
BLI_sys_types.h:71
seed
static unsigned long seed
Definition
btSoftBody.h:39
len
int len
Definition
draw_manager_c.cc:115
BLI_hash_mm2a_init
void BLI_hash_mm2a_init(BLI_HashMurmur2A *mm2, uint32_t seed)
Definition
hash_mm2a.cc:62
mm2a_mix_tail
static void mm2a_mix_tail(BLI_HashMurmur2A *mm2, const uchar **data, size_t *len)
Definition
hash_mm2a.cc:45
BLI_hash_mm2a_add_int
void BLI_hash_mm2a_add_int(BLI_HashMurmur2A *mm2, int data)
Definition
hash_mm2a.cc:85
BLI_hash_mm2
uint32_t BLI_hash_mm2(const uchar *data, size_t len, uint32_t seed)
Definition
hash_mm2a.cc:100
BLI_hash_mm2a_end
uint32_t BLI_hash_mm2a_end(BLI_HashMurmur2A *mm2)
Definition
hash_mm2a.cc:90
BLI_hash_mm2a_add
void BLI_hash_mm2a_add(BLI_HashMurmur2A *mm2, const uchar *data, size_t len)
Definition
hash_mm2a.cc:70
MM2A_MIX_FINALIZE
#define MM2A_MIX_FINALIZE(h)
Definition
hash_mm2a.cc:37
MM2A_M
#define MM2A_M
Definition
hash_mm2a.cc:26
MM2A_MIX
#define MM2A_MIX(h, k)
Definition
hash_mm2a.cc:28
uint32_t
unsigned int uint32_t
Definition
stdint.h:80
BLI_HashMurmur2A
Definition
BLI_hash_mm2a.hh:13
BLI_HashMurmur2A::hash
uint32_t hash
Definition
BLI_hash_mm2a.hh:14
BLI_HashMurmur2A::tail
uint32_t tail
Definition
BLI_hash_mm2a.hh:15
BLI_HashMurmur2A::size
uint32_t size
Definition
BLI_hash_mm2a.hh:17
BLI_HashMurmur2A::count
uint32_t count
Definition
BLI_hash_mm2a.hh:16
Generated on Thu Feb 6 2025 07:36:39 for Blender by
doxygen
1.11.0