Blender
V2.93
source
blender
blenlib
intern
hash_mm2a.c
Go to the documentation of this file.
1
/*
2
* This program is free software; you can redistribute it and/or
3
* modify it under the terms of the GNU General Public License
4
* as published by the Free Software Foundation; either version 2
5
* of the License, or (at your option) any later version.
6
*
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
11
*
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software Foundation,
14
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15
* Copyright (C) 2014 Blender Foundation.
16
*/
17
34
#include "
BLI_compiler_attrs.h
"
35
36
#include "
BLI_hash_mm2a.h
"
/* own include */
37
38
/* Helpers. */
39
#define MM2A_M 0x5bd1e995
40
41
#define MM2A_MIX(h, k) \
42
{ \
43
(k) *= MM2A_M; \
44
(k) ^= (k) >> 24; \
45
(k) *= MM2A_M; \
46
(h) = ((h)*MM2A_M) ^ (k); \
47
} \
48
(void)0
49
50
#define MM2A_MIX_FINALIZE(h) \
51
{ \
52
(h) ^= (h) >> 13; \
53
(h) *= MM2A_M; \
54
(h) ^= (h) >> 15; \
55
} \
56
(void)0
57
58
static
void
mm2a_mix_tail
(
BLI_HashMurmur2A
*mm2,
const
unsigned
char
**
data
,
size_t
*
len
)
59
{
60
while
(*
len
&& ((*
len
< 4) || mm2->
count
)) {
61
mm2->
tail
|= (
uint32_t
)(**
data
) << (mm2->
count
* 8);
62
63
mm2->
count
++;
64
(*len)--;
65
(*data)++;
66
67
if
(mm2->
count
== 4) {
68
MM2A_MIX
(mm2->
hash
, mm2->
tail
);
69
mm2->
tail
= 0;
70
mm2->
count
= 0;
71
}
72
}
73
}
74
75
void
BLI_hash_mm2a_init
(
BLI_HashMurmur2A
*mm2,
uint32_t
seed
)
76
{
77
mm2->
hash
=
seed
;
78
mm2->
tail
= 0;
79
mm2->
count
= 0;
80
mm2->
size
= 0;
81
}
82
83
void
BLI_hash_mm2a_add
(
BLI_HashMurmur2A
*mm2,
const
unsigned
char
*
data
,
size_t
len
)
84
{
85
mm2->
size
+= (
uint32_t
)
len
;
86
87
mm2a_mix_tail
(mm2, &
data
, &
len
);
88
89
for
(;
len
>= 4;
data
+= 4,
len
-= 4) {
90
uint32_t
k = *(
const
uint32_t
*)
data
;
91
92
MM2A_MIX
(mm2->
hash
, k);
93
}
94
95
mm2a_mix_tail
(mm2, &
data
, &
len
);
96
}
97
98
void
BLI_hash_mm2a_add_int
(
BLI_HashMurmur2A
*mm2,
int
data
)
99
{
100
BLI_hash_mm2a_add
(mm2, (
const
unsigned
char
*)&
data
,
sizeof
(
data
));
101
}
102
103
uint32_t
BLI_hash_mm2a_end
(
BLI_HashMurmur2A
*mm2)
104
{
105
MM2A_MIX
(mm2->
hash
, mm2->
tail
);
106
MM2A_MIX
(mm2->
hash
, mm2->
size
);
107
108
MM2A_MIX_FINALIZE
(mm2->
hash
);
109
110
return
mm2->
hash
;
111
}
112
113
/* Non-incremental version, quicker for small keys. */
114
uint32_t
BLI_hash_mm2
(
const
unsigned
char
*
data
,
size_t
len
,
uint32_t
seed
)
115
{
116
/* Initialize the hash to a 'random' value */
117
uint32_t
h =
seed
^
len
;
118
119
/* Mix 4 bytes at a time into the hash */
120
for
(;
len
>= 4;
data
+= 4,
len
-= 4) {
121
uint32_t
k = *(
uint32_t
*)
data
;
122
123
MM2A_MIX
(h, k);
124
}
125
126
/* Handle the last few bytes of the input array */
127
switch
(
len
) {
128
case
3:
129
h ^=
data
[2] << 16;
130
ATTR_FALLTHROUGH
;
131
case
2:
132
h ^=
data
[1] << 8;
133
ATTR_FALLTHROUGH
;
134
case
1:
135
h ^=
data
[0];
136
h *=
MM2A_M
;
137
}
138
139
/* Do a few final mixes of the hash to ensure the last few bytes are well-incorporated. */
140
MM2A_MIX_FINALIZE
(h);
141
142
return
h;
143
}
BLI_compiler_attrs.h
ATTR_FALLTHROUGH
#define ATTR_FALLTHROUGH
Definition:
BLI_compiler_attrs.h:91
BLI_hash_mm2a.h
data
data
Definition:
bmesh_operator_api_inline.h:176
seed
static unsigned long seed
Definition:
btSoftBody.h:39
BLI_hash_mm2a_init
void BLI_hash_mm2a_init(BLI_HashMurmur2A *mm2, uint32_t seed)
Definition:
hash_mm2a.c:75
mm2a_mix_tail
static void mm2a_mix_tail(BLI_HashMurmur2A *mm2, const unsigned char **data, size_t *len)
Definition:
hash_mm2a.c:58
BLI_hash_mm2a_add
void BLI_hash_mm2a_add(BLI_HashMurmur2A *mm2, const unsigned char *data, size_t len)
Definition:
hash_mm2a.c:83
BLI_hash_mm2a_add_int
void BLI_hash_mm2a_add_int(BLI_HashMurmur2A *mm2, int data)
Definition:
hash_mm2a.c:98
BLI_hash_mm2a_end
uint32_t BLI_hash_mm2a_end(BLI_HashMurmur2A *mm2)
Definition:
hash_mm2a.c:103
MM2A_MIX_FINALIZE
#define MM2A_MIX_FINALIZE(h)
Definition:
hash_mm2a.c:50
MM2A_M
#define MM2A_M
Definition:
hash_mm2a.c:39
MM2A_MIX
#define MM2A_MIX(h, k)
Definition:
hash_mm2a.c:41
BLI_hash_mm2
uint32_t BLI_hash_mm2(const unsigned char *data, size_t len, uint32_t seed)
Definition:
hash_mm2a.c:114
uint32_t
unsigned int uint32_t
Definition:
stdint.h:83
BLI_HashMurmur2A
Definition:
BLI_hash_mm2a.h:29
BLI_HashMurmur2A::hash
uint32_t hash
Definition:
BLI_hash_mm2a.h:30
BLI_HashMurmur2A::tail
uint32_t tail
Definition:
BLI_hash_mm2a.h:31
BLI_HashMurmur2A::size
uint32_t size
Definition:
BLI_hash_mm2a.h:33
BLI_HashMurmur2A::count
uint32_t count
Definition:
BLI_hash_mm2a.h:32
len
uint len
Definition:
uvedit_unwrap_ops.c:1146
Generated on Tue Jan 31 2023 14:37:24 for Blender by
doxygen
1.9.1