1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
19 /** \file
20  * \ingroup bli
21  */
22 
23 #include "BLI_utildefines.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
BLI_hash_int_2d(unsigned int kx,unsigned int ky)29 BLI_INLINE unsigned int BLI_hash_int_2d(unsigned int kx, unsigned int ky)
30 {
31 #define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
32 
33   unsigned int a, b, c;
34 
35   a = b = c = 0xdeadbeef + (2 << 2) + 13;
36   a += kx;
37   b += ky;
38 
39   c ^= b;
40   c -= rot(b, 14);
41   a ^= c;
42   a -= rot(c, 11);
43   b ^= a;
44   b -= rot(a, 25);
45   c ^= b;
46   c -= rot(b, 16);
47   a ^= c;
48   a -= rot(c, 4);
49   b ^= a;
50   b -= rot(a, 14);
51   c ^= b;
52   c -= rot(b, 24);
53 
54   return c;
55 
56 #undef rot
57 }
58 
BLI_hash_string(const char * str)59 BLI_INLINE unsigned int BLI_hash_string(const char *str)
60 {
61   unsigned int i = 0, c;
62 
63   while ((c = *str++)) {
64     i = i * 37 + c;
65   }
66   return i;
67 }
68 
BLI_hash_int(unsigned int k)69 BLI_INLINE unsigned int BLI_hash_int(unsigned int k)
70 {
71   return BLI_hash_int_2d(k, 0);
72 }
73 
BLI_hash_int_01(unsigned int k)74 BLI_INLINE float BLI_hash_int_01(unsigned int k)
75 {
76   return (float)BLI_hash_int(k) * (1.0f / (float)0xFFFFFFFF);
77 }
78 
BLI_hash_pointer_to_color(const void * ptr,int * r,int * g,int * b)79 BLI_INLINE void BLI_hash_pointer_to_color(const void *ptr, int *r, int *g, int *b)
80 {
81   size_t val = (size_t)ptr;
82   const size_t hash_a = BLI_hash_int(val & 0x0000ffff);
83   const size_t hash_b = BLI_hash_int((uint)((val & 0xffff0000) >> 16));
84   const size_t hash = hash_a ^ (hash_b + 0x9e3779b9 + (hash_a << 6) + (hash_a >> 2));
85   *r = (hash & 0xff0000) >> 16;
86   *g = (hash & 0x00ff00) >> 8;
87   *b = hash & 0x0000ff;
88 }
89 
90 #ifdef __cplusplus
91 }
92 #endif
93