Blender
V4.3
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 <stdlib.h>
12
#include <
string.h
>
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
(
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,
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,
int
len
,
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
}
92
93
h1 ^=
len
;
94
h1 =
mm_hash_fmix32
(h1);
95
return
h1;
96
}
97
98
/* This is taken from the cryptomatte specification 1.0 */
99
float
util_hash_to_float
(
uint32_t
hash
)
100
{
101
uint32_t
mantissa =
hash
& ((1 << 23) - 1);
102
uint32_t
exponent = (
hash
>> 23) & ((1 << 8) - 1);
103
exponent =
max
(exponent, (
uint32_t
)1);
104
exponent =
min
(exponent, (
uint32_t
)254);
105
exponent = exponent << 23;
106
uint32_t
sign = (
hash
>> 31);
107
sign = sign << 31;
108
uint32_t
float_bits = sign | exponent | mantissa;
109
float
f;
110
memcpy(&f, &float_bits,
sizeof
(
uint32_t
));
111
return
f;
112
}
113
114
CCL_NAMESPACE_END
ATTR_FALLTHROUGH
#define ATTR_FALLTHROUGH
Definition
BLI_compiler_attrs.h:76
seed
static unsigned long seed
Definition
btSoftBody.h:39
ccl_device_inline
#define ccl_device_inline
Definition
device/cuda/compat.h:36
CCL_NAMESPACE_END
#define CCL_NAMESPACE_END
Definition
device/cuda/compat.h:10
len
int len
Definition
draw_manager_c.cc:115
util_hash_to_float
float util_hash_to_float(uint32_t hash)
Definition
murmurhash.cpp:99
util_murmur_hash3
uint32_t util_murmur_hash3(const void *key, int len, uint32_t seed)
Definition
murmurhash.cpp:50
rotl32
ccl_device_inline uint32_t rotl32(uint32_t x, int8_t r)
Definition
murmurhash.cpp:22
mm_hash_getblock32
CCL_NAMESPACE_BEGIN ccl_device_inline uint32_t mm_hash_getblock32(const uint32_t *p, int i)
Definition
murmurhash.cpp:34
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:44
hash
#define hash
Definition
noise.c:154
min
#define min(a, b)
Definition
sort.c:32
uint32_t
unsigned int uint32_t
Definition
stdint.h:80
uint8_t
unsigned char uint8_t
Definition
stdint.h:78
int8_t
signed char int8_t
Definition
stdint.h:75
string.h
max
float max
Definition
transform_gizmo_3d.cc:93
math.h
Generated on Thu Feb 6 2025 07:36:39 for Blender by
doxygen
1.11.0