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