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