Blender V5.0
BLI_hash.h
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.h"
12
17
18#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
19#define final(a, b, c) \
20 { \
21 c ^= b; \
22 c -= rot(b, 14); \
23 a ^= c; \
24 a -= rot(c, 11); \
25 b ^= a; \
26 b -= rot(a, 25); \
27 c ^= b; \
28 c -= rot(b, 16); \
29 a ^= c; \
30 a -= rot(c, 4); \
31 b ^= a; \
32 b -= rot(a, 14); \
33 c ^= b; \
34 c -= rot(b, 24); \
35 } \
36 ((void)0)
37
38BLI_INLINE unsigned int BLI_hash_int_3d(unsigned int kx, unsigned int ky, unsigned int kz)
39{
40 unsigned int a, b, c;
41 a = b = c = 0xdeadbeef + (3 << 2) + 13;
42
43 c += kz;
44 b += ky;
45 a += kx;
46 final(a, b, c);
47
48 return c;
49}
50
51BLI_INLINE unsigned int BLI_hash_int_2d(unsigned int kx, unsigned int ky)
52{
53 unsigned int a, b, c;
54
55 a = b = c = 0xdeadbeef + (2 << 2) + 13;
56 a += kx;
57 b += ky;
58
59 final(a, b, c);
60
61 return c;
62}
63
64#undef final
65#undef rot
66
67BLI_INLINE unsigned int BLI_hash_string(const char *str)
68{
69 unsigned int i = 0, c;
70
71 while ((c = *str++)) {
72 i = i * 37 + c;
73 }
74 return i;
75}
76
77BLI_INLINE float BLI_hash_int_2d_to_float(uint32_t kx, uint32_t ky)
78{
79 return (float)BLI_hash_int_2d(kx, ky) / (float)0xFFFFFFFFu;
80}
81
82BLI_INLINE float BLI_hash_int_3d_to_float(uint32_t kx, uint32_t ky, uint32_t kz)
83{
84 return (float)BLI_hash_int_3d(kx, ky, kz) / (float)0xFFFFFFFFu;
85}
86
87BLI_INLINE unsigned int BLI_hash_int(unsigned int k)
88{
89 return BLI_hash_int_2d(k, 0);
90}
91
92BLI_INLINE float BLI_hash_int_01(unsigned int k)
93{
94 return (float)BLI_hash_int(k) * (1.0f / (float)0xFFFFFFFF);
95}
96
97BLI_INLINE void BLI_hash_pointer_to_color(const void *ptr, int *r, int *g, int *b)
98{
99 size_t val = (size_t)ptr;
100 const size_t hash_a = BLI_hash_int(val & 0x0000ffff);
101 const size_t hash_b = BLI_hash_int((uint)((val & 0xffff0000) >> 16));
102 const size_t hash = hash_a ^ (hash_b + 0x9e3779b9 + (hash_a << 6) + (hash_a >> 2));
103 *r = (hash & 0xff0000) >> 16;
104 *g = (hash & 0x00ff00) >> 8;
105 *b = hash & 0x0000ff;
106}
#define BLI_INLINE
BLI_INLINE float BLI_hash_int_01(unsigned int k)
Definition BLI_hash.h:92
BLI_INLINE unsigned int BLI_hash_int(unsigned int k)
Definition BLI_hash.h:87
BLI_INLINE void BLI_hash_pointer_to_color(const void *ptr, int *r, int *g, int *b)
Definition BLI_hash.h:97
BLI_INLINE float BLI_hash_int_2d_to_float(uint32_t kx, uint32_t ky)
Definition BLI_hash.h:77
BLI_INLINE unsigned int BLI_hash_string(const char *str)
Definition BLI_hash.h:67
BLI_INLINE float BLI_hash_int_3d_to_float(uint32_t kx, uint32_t ky, uint32_t kz)
Definition BLI_hash.h:82
BLI_INLINE unsigned int BLI_hash_int_2d(unsigned int kx, unsigned int ky)
Definition BLI_hash.h:51
BLI_INLINE unsigned int BLI_hash_int_3d(unsigned int kx, unsigned int ky, unsigned int kz)
Definition BLI_hash.h:38
unsigned int uint
nullptr float
#define str(s)
#define hash
Definition noise_c.cc:154
i
Definition text_draw.cc:230
PointerRNA * ptr
Definition wm_files.cc:4238