Blender V5.3
BLI_hash_c.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include "BLI_utildefines.hh"
12
13namespace blender {
14
19
20#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
21#define final(a, b, c) \
22 { \
23 c ^= b; \
24 c -= rot(b, 14); \
25 a ^= c; \
26 a -= rot(c, 11); \
27 b ^= a; \
28 b -= rot(a, 25); \
29 c ^= b; \
30 c -= rot(b, 16); \
31 a ^= c; \
32 a -= rot(c, 4); \
33 b ^= a; \
34 b -= rot(a, 14); \
35 c ^= b; \
36 c -= rot(b, 24); \
37 } \
38 ((void)0)
39
40BLI_INLINE unsigned int BLI_hash_int_3d(unsigned int kx, unsigned int ky, unsigned int kz)
41{
42 unsigned int a, b, c;
43 a = b = c = 0xdeadbeef + (3 << 2) + 13;
44
45 c += kz;
46 b += ky;
47 a += kx;
48 final(a, b, c);
49
50 return c;
51}
52
53BLI_INLINE unsigned int BLI_hash_int_2d(unsigned int kx, unsigned int ky)
54{
55 unsigned int a, b, c;
56
57 a = b = c = 0xdeadbeef + (2 << 2) + 13;
58 a += kx;
59 b += ky;
60
61 final(a, b, c);
62
63 return c;
64}
65
66#undef final
67#undef rot
68
69BLI_INLINE unsigned int BLI_hash_string(const char *str)
70{
71 unsigned int i = 0, c;
72
73 while ((c = *str++)) {
74 i = i * 37 + c;
75 }
76 return i;
77}
78
80{
81 return float(BLI_hash_int_2d(kx, ky)) / float(0xFFFFFFFFu);
82}
83
85{
86 return float(BLI_hash_int_3d(kx, ky, kz)) / float(0xFFFFFFFFu);
87}
88
89BLI_INLINE unsigned int BLI_hash_int(unsigned int k)
90{
91 return BLI_hash_int_2d(k, 0);
92}
93
94BLI_INLINE float BLI_hash_int_01(unsigned int k)
95{
96 return float(BLI_hash_int(k)) * (1.0f / float(0xFFFFFFFF));
97}
98
99BLI_INLINE void BLI_hash_pointer_to_color(const void *ptr, int *r, int *g, int *b)
100{
101 size_t val = reinterpret_cast<size_t>(ptr);
102 const size_t hash_a = BLI_hash_int(val & 0x0000ffff);
103 const size_t hash_b = BLI_hash_int(uint((val & 0xffff0000) >> 16));
104 const size_t hash = hash_a ^ (hash_b + 0x9e3779b9 + (hash_a << 6) + (hash_a >> 2));
105 *r = (hash & 0xff0000) >> 16;
106 *g = (hash & 0x00ff00) >> 8;
107 *b = hash & 0x0000ff;
108}
109
110} // namespace blender
#define BLI_INLINE
#define str(s)
BMesh Iterators.
BLI_INLINE void BLI_hash_pointer_to_color(const void *ptr, int *r, int *g, int *b)
Definition BLI_hash_c.hh:99
unsigned int uint
Definition BKE_text.h:55
BLI_INLINE unsigned int BLI_hash_int_2d(unsigned int kx, unsigned int ky)
Definition BLI_hash_c.hh:53
BLI_INLINE float BLI_hash_int_2d_to_float(uint32_t kx, uint32_t ky)
Definition BLI_hash_c.hh:79
BLI_INLINE float BLI_hash_int_01(unsigned int k)
Definition BLI_hash_c.hh:94
unsigned int uint32_t
Definition BKE_text.h:81
BLI_INLINE unsigned int BLI_hash_int(unsigned int k)
Definition BLI_hash_c.hh:89
BLI_INLINE unsigned int BLI_hash_string(const char *str)
Definition BLI_hash_c.hh:69
BLI_INLINE unsigned int BLI_hash_int_3d(unsigned int kx, unsigned int ky, unsigned int kz)
Definition BLI_hash_c.hh:40
BLI_INLINE float BLI_hash_int_3d_to_float(uint32_t kx, uint32_t ky, uint32_t kz)
Definition BLI_hash_c.hh:84
#define hash
Definition noise_c.cc:156
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float