Blender V5.0
hash.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "util/defines.h"
8#include "util/math.h"
9#include "util/types.h"
10
12
13/* [0, uint_max] -> [0.0, 1.0) */
15{
16 /* NOTE: we divide by 4294967808 instead of 2^32 because the latter
17 * leads to a [0.0, 1.0] mapping instead of [0.0, 1.0) due to floating
18 * point rounding error. 4294967808 unfortunately leaves (precisely)
19 * one unused ULP between the max number this outputs and 1.0, but
20 * that's the best you can do with this construction. */
21 return (float)n * (1.0f / 4294967808.0f);
22}
23
24/* [0, uint_max] -> [0.0, 1.0] */
26{
27 return (float)n * (1.0f / (float)0xFFFFFFFFu);
28}
29
30/* PCG 2D, 3D and 4D hash functions,
31 * from "Hash Functions for GPU Rendering" JCGT 2020
32 * https://jcgt.org/published/0009/03/02/
33 *
34 * Slightly modified to only use signed integers,
35 * so that they can also be implemented in OSL.
36 *
37 * Silence UBsan warnings about signed integer overflow. */
38
40{
41 v = v * make_int2(1664525) + make_int2(1013904223);
42 v.x += v.y * 1664525;
43 v.y += v.x * 1664525;
44 v = v ^ (v >> 16);
45 v.x += v.y * 1664525;
46 v.y += v.x * 1664525;
47 return v & make_int2(0x7FFFFFFF);
48}
49
51{
52 v = v * make_int3(1664525) + make_int3(1013904223);
53 v.x += v.y * v.z;
54 v.y += v.z * v.x;
55 v.z += v.x * v.y;
56 v = v ^ (v >> 16);
57 v.x += v.y * v.z;
58 v.y += v.z * v.x;
59 v.z += v.x * v.y;
60 return v & make_int3(0x7FFFFFFF);
61}
62
64{
65 v = v * make_int4(1664525) + make_int4(1013904223);
66 v.x += v.y * v.w;
67 v.y += v.z * v.x;
68 v.z += v.x * v.y;
69 v.w += v.y * v.z;
70 v = v ^ (v >> 16);
71 v.x += v.y * v.w;
72 v.y += v.z * v.x;
73 v.z += v.x * v.y;
74 v.w += v.y * v.z;
75 return v & make_int4(0x7FFFFFFF);
76}
77
78/* ***** Jenkins Lookup3 Hash Functions ***** */
79
80/* Source: http://burtleburtle.net/bob/c/lookup3.c */
81
82#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
83
84#define mix(a, b, c) \
85 { \
86 a -= c; \
87 a ^= rot(c, 4); \
88 c += b; \
89 b -= a; \
90 b ^= rot(a, 6); \
91 a += c; \
92 c -= b; \
93 c ^= rot(b, 8); \
94 b += a; \
95 a -= c; \
96 a ^= rot(c, 16); \
97 c += b; \
98 b -= a; \
99 b ^= rot(a, 19); \
100 a += c; \
101 c -= b; \
102 c ^= rot(b, 4); \
103 b += a; \
104 } \
105 ((void)0)
106
107#define final(a, b, c) \
108 { \
109 c ^= b; \
110 c -= rot(b, 14); \
111 a ^= c; \
112 a -= rot(c, 11); \
113 b ^= a; \
114 b -= rot(a, 25); \
115 c ^= b; \
116 c -= rot(b, 16); \
117 a ^= c; \
118 a -= rot(c, 4); \
119 b ^= a; \
120 b -= rot(a, 14); \
121 c ^= b; \
122 c -= rot(b, 24); \
123 } \
124 ((void)0)
125
127{
128 uint a;
129 uint b;
130 uint c;
131 a = b = c = 0xdeadbeef + (1 << 2) + 13;
132
133 a += kx;
134 final(a, b, c);
135
136 return c;
137}
138
140{
141 uint a;
142 uint b;
143 uint c;
144 a = b = c = 0xdeadbeef + (2 << 2) + 13;
145
146 b += ky;
147 a += kx;
148 final(a, b, c);
149
150 return c;
151}
152
153ccl_device_inline uint hash_uint3(const uint kx, const uint ky, const uint kz)
154{
155 uint a;
156 uint b;
157 uint c;
158 a = b = c = 0xdeadbeef + (3 << 2) + 13;
159
160 c += kz;
161 b += ky;
162 a += kx;
163 final(a, b, c);
164
165 return c;
166}
167
168ccl_device_inline uint hash_uint4(const uint kx, const uint ky, const uint kz, const uint kw)
169{
170 uint a;
171 uint b;
172 uint c;
173 a = b = c = 0xdeadbeef + (4 << 2) + 13;
174
175 a += kx;
176 b += ky;
177 c += kz;
178 mix(a, b, c);
179
180 a += kw;
181 final(a, b, c);
182
183 return c;
184}
185
186#undef rot
187#undef final
188#undef mix
189
190/* Hashing uint or uint[234] into a float in the range [0, 1]. */
191
193{
194 return uint_to_float_incl(hash_uint(kx));
195}
196
198{
199 return uint_to_float_incl(hash_uint2(kx, ky));
200}
201
202ccl_device_inline float hash_uint3_to_float(const uint kx, const uint ky, const uint kz)
203{
204 return uint_to_float_incl(hash_uint3(kx, ky, kz));
205}
206
208 const uint ky,
209 const uint kz,
210 const uint kw)
211{
212 return uint_to_float_incl(hash_uint4(kx, ky, kz, kw));
213}
214
215/* Hashing float or float[234] into a float in the range [0, 1]. */
216
218{
220}
221
226
231
237
238/* Hashing int[234] into float[234] of components in the range [0, 1].
239 * These are based on PCG 2D/3D/4D. */
240
242{
243 int2 h = hash_pcg2d_i(k);
244 float2 f = make_float2((float)h.x, (float)h.y);
245 return f * (1.0f / (float)0x7FFFFFFFu);
246}
247
249{
250 int3 h = hash_pcg3d_i(k);
251 float3 f = make_float3((float)h.x, (float)h.y, (float)h.z);
252 return f * (1.0f / (float)0x7FFFFFFFu);
253}
254
256{
257 int4 h = hash_pcg4d_i(k);
258 float4 f = make_float4(h);
259 return f * (1.0f / (float)0x7FFFFFFFu);
260}
261
266
271
272/* Hashing int[234] / float[234] into float[234] of components in the range [0, 1].
273 *
274 * Note that while using a more modern hash (e.g. PCG) would be faster, the current
275 * behavior has to be kept to match what is possible in OSL (OSL lacks bit casts and unsigned
276 * integers). */
277
282
289
297
298/* Hashing float or float[234] into float3 of components in range [0, 1]. */
299
306
313
320
321/* Hashing float or float[234] into float2 of components in range [0, 1]. */
322
327
333
339
340/* SSE Versions Of Jenkins Lookup3 Hash Functions */
341
342#ifdef __KERNEL_SSE__
343# define rot(x, k) (((x) << (k)) | (srl(x, 32 - (k))))
344
345# define mix(a, b, c) \
346 { \
347 a -= c; \
348 a ^= rot(c, 4); \
349 c += b; \
350 b -= a; \
351 b ^= rot(a, 6); \
352 a += c; \
353 c -= b; \
354 c ^= rot(b, 8); \
355 b += a; \
356 a -= c; \
357 a ^= rot(c, 16); \
358 c += b; \
359 b -= a; \
360 b ^= rot(a, 19); \
361 a += c; \
362 c -= b; \
363 c ^= rot(b, 4); \
364 b += a; \
365 }
366
367# define final(a, b, c) \
368 { \
369 c ^= b; \
370 c -= rot(b, 14); \
371 a ^= c; \
372 a -= rot(c, 11); \
373 b ^= a; \
374 b -= rot(a, 25); \
375 c ^= b; \
376 c -= rot(b, 16); \
377 a ^= c; \
378 a -= rot(c, 4); \
379 b ^= a; \
380 b -= rot(a, 14); \
381 c ^= b; \
382 c -= rot(b, 24); \
383 }
384
385ccl_device_inline int4 hash_int4(const int4 kx)
386{
387 int4 a;
388 int4 b;
389 int4 c;
390 a = b = c = make_int4(0xdeadbeef + (1 << 2) + 13);
391
392 a += kx;
393 final(a, b, c);
394
395 return c;
396}
397
398ccl_device_inline int4 hash_int4_2(const int4 kx, const int4 ky)
399{
400 int4 a;
401 int4 b;
402 int4 c;
403 a = b = c = make_int4(0xdeadbeef + (2 << 2) + 13);
404
405 b += ky;
406 a += kx;
407 final(a, b, c);
408
409 return c;
410}
411
412ccl_device_inline int4 hash_int4_3(const int4 kx, const int4 ky, const int4 kz)
413{
414 int4 a;
415 int4 b;
416 int4 c;
417 a = b = c = make_int4(0xdeadbeef + (3 << 2) + 13);
418
419 c += kz;
420 b += ky;
421 a += kx;
422 final(a, b, c);
423
424 return c;
425}
426
427ccl_device_inline int4 hash_int4_4(const int4 kx, const int4 ky, const int4 kz, const int4 kw)
428{
429 int4 a;
430 int4 b;
431 int4 c;
432 a = b = c = make_int4(0xdeadbeef + (4 << 2) + 13);
433
434 a += kx;
435 b += ky;
436 c += kz;
437 mix(a, b, c);
438
439 a += kw;
440 final(a, b, c);
441
442 return c;
443}
444
445# if defined(__KERNEL_AVX2__)
446ccl_device_inline vint8 hash_int8(vint8 kx)
447{
448 vint8 a, b, c;
449 a = b = c = make_vint8(0xdeadbeef + (1 << 2) + 13);
450
451 a += kx;
452 final(a, b, c);
453
454 return c;
455}
456
457ccl_device_inline vint8 hash_int8_2(vint8 kx, vint8 ky)
458{
459 vint8 a, b, c;
460 a = b = c = make_vint8(0xdeadbeef + (2 << 2) + 13);
461
462 b += ky;
463 a += kx;
464 final(a, b, c);
465
466 return c;
467}
468
469ccl_device_inline vint8 hash_int8_3(vint8 kx, vint8 ky, vint8 kz)
470{
471 vint8 a, b, c;
472 a = b = c = make_vint8(0xdeadbeef + (3 << 2) + 13);
473
474 c += kz;
475 b += ky;
476 a += kx;
477 final(a, b, c);
478
479 return c;
480}
481
482ccl_device_inline vint8 hash_int8_4(vint8 kx, vint8 ky, vint8 kz, vint8 kw)
483{
484 vint8 a, b, c;
485 a = b = c = make_vint8(0xdeadbeef + (4 << 2) + 13);
486
487 a += kx;
488 b += ky;
489 c += kz;
490 mix(a, b, c);
491
492 a += kw;
493 final(a, b, c);
494
495 return c;
496}
497# endif
498
499# undef rot
500# undef final
501# undef mix
502
503#endif
504
505/* ***** Hash Prospector Hash Functions *****
506 *
507 * These are based on the high-quality 32-bit hash/mixing functions from
508 * https://github.com/skeeto/hash-prospector
509 */
510
512{
513 // The actual mixing function from Hash Prospector.
514 i ^= i >> 16;
515 i *= 0x21f0aaad;
516 i ^= i >> 15;
517 i *= 0xd35a2d97;
518 i ^= i >> 15;
519
520 // The xor is just to make input zero not map to output zero.
521 // The number is randomly selected and isn't special.
522 return i ^ 0xe6fe3beb;
523}
524
525/* Seedable version of hash_hp_uint() above. */
527{
528 // Manipulate the seed so it doesn't interact poorly with n when they
529 // are both e.g. incrementing. This isn't fool-proof, but is good
530 // enough for practical use.
531 seed ^= seed << 19;
532
533 return hash_hp_uint(i ^ seed);
534}
535
536/* Outputs [0.0, 1.0). */
541
542/* Outputs [0.0, 1.0). */
547
548/* ***** Modified Wang Hash Functions *****
549 *
550 * These are based on a bespoke modified version of the Wang hash, and
551 * can serve as a faster hash when quality isn't critical.
552 *
553 * The original Wang hash is documented here:
554 * https://www.burtleburtle.net/bob/hash/integer.html
555 */
556
558{
559 i = (i ^ 61) ^ seed;
560 i += i << 3;
561 i ^= i >> 4;
562 i *= 0x27d4eb2d;
563 return i;
564}
565
566/* Outputs [0.0, 1.0). */
571
572/* ***** Index Shuffling Hash Function *****
573 *
574 * This function takes an index, the length of the thing the index points
575 * into, and returns a shuffled index. For example, if you pass indices
576 * 0 through 19 to this function with a length parameter of 20, it will
577 * return the indices in a shuffled order with no repeats. Indices
578 * larger than the length parameter will simply repeat the same shuffled
579 * pattern over and over.
580 *
581 * This is useful for iterating over an array in random shuffled order
582 * without having to shuffle the array itself.
583 *
584 * Passing different seeds results in different random shuffles.
585 *
586 * This function runs in average O(1) time.
587 *
588 * See https://andrew-helmer.github.io/permute/ for details on how this
589 * works.
590 */
592{
593 i = i % length;
594 const uint mask = (1 << (32 - count_leading_zeros(length - 1))) - 1;
595
596 do {
597 i ^= seed;
598 i *= 0xe170893d;
599 i ^= seed >> 16;
600 i ^= (i & mask) >> 4;
601 i ^= seed >> 8;
602 i *= 0x0929eb3f;
603 i ^= seed >> 23;
604 i ^= (i & mask) >> 1;
605 i *= 1 | seed >> 27;
606 i *= 0x6935fa69;
607 i ^= (i & mask) >> 11;
608 i *= 0x74dcb303;
609 i ^= (i & mask) >> 2;
610 i *= 0x9e501cc3;
611 i ^= (i & mask) >> 2;
612 i *= 0xc860a3df;
613 i &= mask;
614 i ^= i >> 5;
615 } while (i >= length);
616
617 return i;
618}
619
626{
627 const uint qx = 1103515245U * ((x >> 1U) ^ (y));
628 const uint qy = 1103515245U * ((y >> 1U) ^ (x));
629 const uint n = 1103515245U * ((qx) ^ (qy >> 3U));
630
631 return n;
632}
633
634/* ********** */
635
636#ifndef __KERNEL_GPU__
637static inline uint hash_string(const char *str)
638{
639 uint i = 0;
640 uint c;
641
642 while ((c = *str++)) {
643 i = i * 37 + c;
644 }
645
646 return i;
647}
648#endif
649
unsigned int uint
ATTR_WARN_UNUSED_RESULT const BMVert * v
static unsigned long seed
Definition btSoftBody.h:39
nullptr float
#define ccl_ignore_integer_overflow
#define ccl_device_forceinline
#define ccl_device_inline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline int3 make_int3(const int x, const int y, const int z)
ccl_device_forceinline int2 make_int2(const int x, const int y)
#define __float_as_uint(x)
ccl_device_forceinline int4 make_int4(const int x, const int y, const int z, const int w)
#define str(s)
float length(VecOp< float, D >) RET
ccl_device_inline uint hash_wang_seeded_uint(uint i, const uint seed)
Definition hash.h:557
static uint hash_string(const char *str)
Definition hash.h:637
ccl_device_inline float2 hash_float3_to_float2(const float3 k)
Definition hash.h:328
ccl_ignore_integer_overflow ccl_device_inline int3 hash_pcg3d_i(int3 v)
Definition hash.h:50
ccl_device_inline float3 hash_float_to_float3(const float k)
Definition hash.h:300
ccl_device_inline float3 hash_int3_to_float3(const int3 k)
Definition hash.h:248
ccl_device_inline uint hash_uint4(const uint kx, const uint ky, const uint kz, const uint kw)
Definition hash.h:168
ccl_device_inline float hash_float_to_float(const float k)
Definition hash.h:217
ccl_device_inline float hash_float3_to_float(const float3 k)
Definition hash.h:227
ccl_device_inline float hash_uint_to_float(const uint kx)
Definition hash.h:192
ccl_device_inline float2 hash_float_to_float2(const float k)
Definition hash.h:323
ccl_device_inline float2 hash_float2_to_float2(const float2 k)
Definition hash.h:278
ccl_device_inline float hash_hp_seeded_float(const uint i, const uint seed)
Definition hash.h:543
ccl_device_inline uint hash_shuffle_uint(uint i, const uint length, const uint seed)
Definition hash.h:591
ccl_device_inline float3 hash_float2_to_float3(const float2 k)
Definition hash.h:307
ccl_device_inline float3 hash_int4_to_float3(const int4 k)
Definition hash.h:267
ccl_device_inline float hash_wang_seeded_float(const uint i, const uint seed)
Definition hash.h:567
ccl_device_inline float2 hash_int2_to_float2(const int2 k)
Definition hash.h:241
ccl_device_inline float hash_float2_to_float(const float2 k)
Definition hash.h:222
ccl_device_inline float3 hash_int2_to_float3(const int2 k)
Definition hash.h:262
ccl_device_inline uint hash_uint2(const uint kx, const uint ky)
Definition hash.h:139
ccl_device_inline uint hash_uint(const uint kx)
Definition hash.h:126
ccl_device_inline float4 hash_float4_to_float4(const float4 k)
Definition hash.h:290
ccl_device_inline uint hash_iqnt2d(const uint x, const uint y)
Definition hash.h:625
ccl_device_inline float4 hash_int4_to_float4(const int4 k)
Definition hash.h:255
ccl_device_forceinline float uint_to_float_incl(const uint n)
Definition hash.h:25
ccl_device_inline float hash_float4_to_float(const float4 k)
Definition hash.h:232
ccl_device_inline float hash_hp_float(const uint i)
Definition hash.h:537
ccl_device_inline float2 hash_float4_to_float2(const float4 k)
Definition hash.h:334
ccl_device_inline float3 hash_float4_to_float3(const float4 k)
Definition hash.h:314
ccl_device_inline uint hash_hp_uint(uint i)
Definition hash.h:511
ccl_device_inline uint hash_uint3(const uint kx, const uint ky, const uint kz)
Definition hash.h:153
ccl_device_inline float hash_uint4_to_float(const uint kx, const uint ky, const uint kz, const uint kw)
Definition hash.h:207
ccl_ignore_integer_overflow ccl_device_inline int4 hash_pcg4d_i(int4 v)
Definition hash.h:63
ccl_device_inline float hash_uint2_to_float(const uint kx, const uint ky)
Definition hash.h:197
ccl_ignore_integer_overflow ccl_device_inline int2 hash_pcg2d_i(int2 v)
Definition hash.h:39
ccl_device_inline uint hash_hp_seeded_uint(const uint i, uint seed)
Definition hash.h:526
ccl_device_inline float3 hash_float3_to_float3(const float3 k)
Definition hash.h:283
ccl_device_inline float hash_uint3_to_float(const uint kx, const uint ky, const uint kz)
Definition hash.h:202
CCL_NAMESPACE_BEGIN ccl_device_forceinline float uint_to_float_excl(const uint n)
Definition hash.h:14
#define mix(a, b, c)
Definition hash.h:84
ccl_device_inline uint count_leading_zeros(const uint x)
Definition math_base.h:707
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
#define make_float2
#define make_float4
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
i
Definition text_draw.cc:230
ccl_device_inline vint8 make_vint8(const vfloat8 f)