2 * Copyright 2018 Blender Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* This is taken from alShaders/Cryptomatte/MurmurHash3.h:
19 * MurmurHash3 was written by Austin Appleby, and is placed in the public
20 * domain. The author hereby disclaims copyright to this source code.
24 #include "util/util_algorithm.h"
25 #include "util/util_murmurhash.h"
29 # define ROTL32(x,y) _rotl(x,y)
30 # define ROTL64(x,y) _rotl64(x,y)
31 # define BIG_CONSTANT(x) (x)
33 ccl_device_inline uint32_t rotl32(uint32_t x, int8_t r)
35 return (x << r) | (x >> (32 - r));
37 # define ROTL32(x,y) rotl32(x,y)
38 # define BIG_CONSTANT(x) (x##LLU)
43 /* Block read - if your platform needs to do endian-swapping or can only
44 * handle aligned reads, do the conversion here. */
45 ccl_device_inline uint32_t mm_hash_getblock32(const uint32_t *p, int i)
50 /* Finalization mix - force all bits of a hash block to avalanche */
51 ccl_device_inline uint32_t mm_hash_fmix32 ( uint32_t h )
61 uint32_t util_murmur_hash3(const void *key, int len, uint32_t seed)
63 const uint8_t * data = (const uint8_t*)key;
64 const int nblocks = len / 4;
68 const uint32_t c1 = 0xcc9e2d51;
69 const uint32_t c2 = 0x1b873593;
71 const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);
73 for(int i = -nblocks; i; i++) {
74 uint32_t k1 = mm_hash_getblock32(blocks,i);
82 h1 = h1 * 5 + 0xe6546b64;
85 const uint8_t *tail = (const uint8_t*)(data + nblocks*4);
105 h1 = mm_hash_fmix32(h1);
109 /* This is taken from the cryptomatte specification 1.0 */
110 float util_hash_to_float(uint32_t hash)
112 uint32_t mantissa = hash & (( 1 << 23) - 1);
113 uint32_t exponent = (hash >> 23) & ((1 << 8) - 1);
114 exponent = max(exponent, (uint32_t) 1);
115 exponent = min(exponent, (uint32_t) 254);
116 exponent = exponent << 23;
117 uint32_t sign = (hash >> 31);
119 uint32_t float_bits = sign | exponent | mantissa;
121 memcpy(&f, &float_bits, sizeof(uint32_t));