Blender V4.3
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
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
31
32/* Block read - if your platform needs to do endian-swapping or can only
33 * handle aligned reads, do the conversion here. */
35{
36 return p[i];
37}
38
39/* Finalization mix - force all bits of a hash block to avalanche */
41{
42 h ^= h >> 16;
43 h *= 0x85ebca6b;
44 h ^= h >> 13;
45 h *= 0xc2b2ae35;
46 h ^= h >> 16;
47 return h;
48}
49
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;
82 case 2:
83 k1 ^= tail[1] << 8;
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 */
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
#define ATTR_FALLTHROUGH
static unsigned long seed
Definition btSoftBody.h:39
#define ccl_device_inline
#define CCL_NAMESPACE_END
int len
float util_hash_to_float(uint32_t hash)
uint32_t util_murmur_hash3(const void *key, int len, uint32_t seed)
ccl_device_inline uint32_t rotl32(uint32_t x, int8_t r)
CCL_NAMESPACE_BEGIN ccl_device_inline uint32_t mm_hash_getblock32(const uint32_t *p, int i)
#define ROTL32(x, y)
ccl_device_inline uint32_t mm_hash_fmix32(uint32_t h)
#define hash
Definition noise.c:154
#define min(a, b)
Definition sort.c:32
unsigned int uint32_t
Definition stdint.h:80
unsigned char uint8_t
Definition stdint.h:78
signed char int8_t
Definition stdint.h:75
float max