Blender
V5.0
intern
cycles
util
murmurhash.cpp
Go to the documentation of this file.
1
/* SPDX-FileCopyrightText: 2018-2022 Blender Foundation
2
*
3
* SPDX-License-Identifier: Apache-2.0 */
4
5
/* This is taken from alShaders/Cryptomatte/MurmurHash3.h:
6
*
7
* MurmurHash3 was written by Austin Appleby, and is placed in the public
8
* domain. The author hereby disclaims copyright to this source code.
9
*/
10
11
#include <cstdlib>
12
#include <cstring>
13
14
#include "
util/math.h
"
15
#include "
util/murmurhash.h
"
16
17
#if defined(_MSC_VER)
18
# define ROTL32(x, y) _rotl(x, y)
19
# define ROTL64(x, y) _rotl64(x, y)
20
# define BIG_CONSTANT(x) (x)
21
#else
22
ccl_device_inline
uint32_t
rotl32
(
const
uint32_t
x
, int8_t r)
23
{
24
return
(
x
<< r) | (
x
>> (32 - r));
25
}
26
# define ROTL32(x, y) rotl32(x, y)
27
# define BIG_CONSTANT(x) (x##LLU)
28
#endif
29
30
CCL_NAMESPACE_BEGIN
31
32
/* Block read - if your platform needs to do endian-swapping or can only
33
* handle aligned reads, do the conversion here. */
34
ccl_device_inline
uint32_t
mm_hash_getblock32
(
const
uint32_t *p,
const
int
i
)
35
{
36
return
p[
i
];
37
}
38
39
/* Finalization mix - force all bits of a hash block to avalanche */
40
ccl_device_inline
uint32_t
mm_hash_fmix32
(uint32_t h)
41
{
42
h ^= h >> 16;
43
h *= 0x85ebca6b;
44
h ^= h >> 13;
45
h *= 0xc2b2ae35;
46
h ^= h >> 16;
47
return
h;
48
}
49
50
uint32_t
util_murmur_hash3
(
const
void
*key,
const
int
len
,
const
uint32_t
seed
)
51
{
52
const
uint8_t *
data
= (
const
uint8_t *)key;
53
const
int
nblocks =
len
/ 4;
54
55
uint32_t h1 =
seed
;
56
57
const
uint32_t c1 = 0xcc9e2d51;
58
const
uint32_t c2 = 0x1b873593;
59
60
const
uint32_t *blocks = (
const
uint32_t *)(
data
+ nblocks * 4);
61
62
for
(
int
i
= -nblocks;
i
;
i
++) {
63
uint32_t k1 =
mm_hash_getblock32
(blocks,
i
);
64
65
k1 *= c1;
66
k1 =
ROTL32
(k1, 15);
67
k1 *= c2;
68
69
h1 ^= k1;
70
h1 =
ROTL32
(h1, 13);
71
h1 = h1 * 5 + 0xe6546b64;
72
}
73
74
const
uint8_t *tail = (
const
uint8_t *)(
data
+ nblocks * 4);
75
76
uint32_t k1 = 0;
77
78
switch
(
len
& 3) {
79
case
3:
80
k1 ^= tail[2] << 16;
81
ATTR_FALLTHROUGH
;
82
case
2:
83
k1 ^= tail[1] << 8;
84
ATTR_FALLTHROUGH
;
85
case
1:
86
k1 ^= tail[0];
87
k1 *= c1;
88
k1 =
ROTL32
(k1, 15);
89
k1 *= c2;
90
h1 ^= k1;
91
ATTR_FALLTHROUGH
;
92
default
:
93
break
;
94
}
95
96
h1 ^=
len
;
97
h1 =
mm_hash_fmix32
(h1);
98
return
h1;
99
}
100
101
/* This is taken from the cryptomatte specification 1.0 */
102
float
util_hash_to_float
(
const
uint32_t
hash
)
103
{
104
const
uint32_t mantissa =
hash
& ((1 << 23) - 1);
105
uint32_t exponent = (
hash
>> 23) & ((1 << 8) - 1);
106
exponent =
max
(exponent, (uint32_t)1);
107
exponent =
min
(exponent, (uint32_t)254);
108
exponent = exponent << 23;
109
uint32_t
sign
= (
hash
>> 31);
110
sign
=
sign
<< 31;
111
uint32_t float_bits =
sign
| exponent | mantissa;
112
float
f;
113
memcpy(&f, &float_bits,
sizeof
(uint32_t));
114
return
f;
115
}
116
117
CCL_NAMESPACE_END
ATTR_FALLTHROUGH
#define ATTR_FALLTHROUGH
Definition
BLI_compiler_attrs.h:76
x
x
Definition
BLI_expr_pylike_eval_test.cc:345
data
BMesh const char void * data
Definition
bmesh_iterators_inline.hh:37
seed
static unsigned long seed
Definition
btSoftBody.h:39
ccl_device_inline
#define ccl_device_inline
CCL_NAMESPACE_END
#define CCL_NAMESPACE_END
Definition
device/cuda/compat.h:10
sign
constexpr T sign(T) RET
rotl32
ccl_device_inline uint32_t rotl32(const uint32_t x, int8_t r)
Definition
murmurhash.cpp:22
util_murmur_hash3
uint32_t util_murmur_hash3(const void *key, const int len, const uint32_t seed)
Definition
murmurhash.cpp:50
mm_hash_getblock32
CCL_NAMESPACE_BEGIN ccl_device_inline uint32_t mm_hash_getblock32(const uint32_t *p, const int i)
Definition
murmurhash.cpp:34
util_hash_to_float
float util_hash_to_float(const uint32_t hash)
Definition
murmurhash.cpp:102
ROTL32
#define ROTL32(x, y)
Definition
murmurhash.cpp:26
mm_hash_fmix32
ccl_device_inline uint32_t mm_hash_fmix32(uint32_t h)
Definition
murmurhash.cpp:40
murmurhash.h
CCL_NAMESPACE_BEGIN
Definition
python.cpp:37
hash
#define hash
Definition
noise_c.cc:154
min
#define min(a, b)
Definition
sort.cc:36
i
i
Definition
text_draw.cc:230
max
max
Definition
text_draw.cc:251
math.h
len
uint len
Definition
uvedit_unwrap_ops.cc:2126
Generated on
for Blender by
doxygen
1.16.1