├── .gitattributes
├── khash64-fn-1dcedff1a8b17e89.png
├── khash32-fn1-6bb75f13-fn2-f9e5a345.png
├── LICENSE
├── README.md
└── khash.h
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/khash64-fn-1dcedff1a8b17e89.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Keith-Cancel/k-hash/HEAD/khash64-fn-1dcedff1a8b17e89.png
--------------------------------------------------------------------------------
/khash32-fn1-6bb75f13-fn2-f9e5a345.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Keith-Cancel/k-hash/HEAD/khash32-fn1-6bb75f13-fn2-f9e5a345.png
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Keith-Cancel
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # K-HASH
2 | A simple single header 64 bit hash function using only add, sub, ror, and xor. This a just general-purpose hash function for like making hash maps and similar data-structures. It's is not a cryptographic hash function!
3 |
4 | It is quite easy to choose a new function at runtime by just passing a random 64 bit value to the func parameter such as:
5 | ```C
6 | #include "khash.h"
7 |
8 | void foo() {
9 | /*
10 | code ....
11 | */
12 | uint64_t hash = khash64_fn(value, your_random_number);
13 | /*
14 | code ....
15 | */
16 | }
17 |
18 | ```
19 | ## K-HASH 64 Output
20 | Here is the output of the 64 bit hash of the integers \[0, 259199\] using 0x1dcedff1a8b17e89 as the function.
21 |
22 |
23 |
24 | ## K-HASH 32 Output
25 |
26 | Here is the output of the 32 bit hash of the integers \[0, 518399\] using 0x6bb75f13 and 0xf9e5a345 as the function;
27 |
28 |
29 |
30 | The output of the above images was generated by basically doing the following for a hash.
31 |
32 | ```C
33 | for(int i = 0; i < sizeof(hash_bytes); i++) {
34 | pixel[img_offset + i].r = hash_bytes[i];
35 | pixel[img_offset + i].g = hash_bytes[i];
36 | pixel[img_offset + i].b = hash_bytes[i];
37 | pixel[img_offset + i].a = 255;
38 | }
39 | ```
40 |
--------------------------------------------------------------------------------
/khash.h:
--------------------------------------------------------------------------------
1 | /*
2 | MIT License
3 |
4 | Copyright (c) 2021 Keith-Cancel
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the “Software”), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 |
25 | #ifndef KEITHS_HASH_H
26 | #define KEITHS_HASH_H
27 |
28 | #include
29 |
30 | #define ROTR32(x,n) (((x) >> (n)) | ((x) << (32 - (n))))
31 | #define ROTR64(x,n) (((x) >> (n)) | ((x) << (64 - (n))))
32 |
33 | // Just initialize with the fractional part of sqrt(2)
34 | #define khash64(input) khash64_fn(input, 0x6a09e667f3bcc908)
35 | #define khash32(input) khash32_fn(input, 0x6a09e667, 0xf3bcc908)
36 |
37 | static inline uint64_t khash64_fn(uint64_t input, uint64_t func) {
38 | uint64_t h = func;
39 | h ^= input - 7;
40 | h ^= ROTR64(h, 31);
41 | h -= ROTR64(h, 11);
42 | h -= ROTR64(h, 17);
43 |
44 | h ^= input - 13;
45 | h ^= ROTR64(h, 23);
46 | h += ROTR64(h, 31);
47 | h -= ROTR64(h, 13);
48 |
49 | h ^= input - 2;
50 | h -= ROTR64(h, 19);
51 | h += ROTR64(h, 5);
52 | h -= ROTR64(h, 31);
53 | return h;
54 | }
55 |
56 | static inline uint32_t khash32_fn(uint32_t input, uint32_t func1, uint32_t func2) {
57 | uint32_t h = input;
58 | h = ROTR32(h, 16);
59 | h ^= func2;
60 | h -= 5;
61 | h = ROTR32(h, 17);
62 | h += func1;
63 | h = ROTR32(h, 1);
64 |
65 | h += ROTR32(h, 27);
66 | h ^= ROTR32(h, 3);
67 | h -= ROTR32(h, 17);
68 | h -= ROTR32(h, 27);
69 |
70 | h ^= input - 107;
71 | h -= ROTR32(h, 11);
72 | h ^= ROTR32(h, 7);
73 | h -= ROTR32(h, 5);
74 | return h;
75 | }
76 |
77 | #endif
--------------------------------------------------------------------------------