Last active
February 24, 2021 18:31
-
-
Save Sam-Belliveau/068c4b93406a9bc26fa7ccf07bae9828 to your computer and use it in GitHub Desktop.
Implementation for a home made hashing algorithm that only uses XOR, bitshifts, and multiplication. It is made to be fast, while also usable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright (c) 2021 Sam Belliveau. All rights reserved. | |
| // This work is licensed under the terms of the MIT license. | |
| // For a copy, see <https://opensource.org/licenses/MIT>. | |
| /** | |
| * These hash functions might seem unnessisarly simple, | |
| * but this hash is made to be as fast as theoretically possible, | |
| * whilst still having good properties. | |
| * | |
| * But when testing this hash, it was shown to be significantly | |
| * slower than expected. This is because it processes the data | |
| * 1 byte at a time. This kills its performance. But this hash | |
| * is otherwise a good demonstration of a hash construction. | |
| * | |
| * When testing, the collision rate of this hash always as low | |
| * as statistically probable. It does not have the garuntee of | |
| * a hash like CRC32, where a single bit flip garuntees a new hash, | |
| * but the probability of a new one is very high. | |
| */ | |
| #ifndef SAM_B_HXOR_HASH_FUNCTION | |
| #define SAM_B_HXOR_HASH_FUNCTION | |
| #include <stdint.h> | |
| /* HXOR32 Hash Implementation in C */ | |
| #define HXOR32_INITIAL 0x85ebca6b | |
| #define HXOR32_MULTIPLIER 0xcc9e2d51 | |
| uint32_t hxor32_str_hash(const char *str) | |
| { | |
| char c; | |
| uint32_t state = HXOR32_INITIAL; | |
| while ((c = *str++)) | |
| { | |
| state ^= ((uint32_t)(c)) * HXOR32_MULTIPLIER; | |
| state ^= state << 13; | |
| state ^= state >> 17; | |
| state ^= state << 5; | |
| } | |
| return state; | |
| } | |
| uint32_t hxor32_hash(const char *str, size_t length) | |
| { | |
| uint32_t state = HXOR32_INITIAL; | |
| for (size_t i = 0; i < length; ++i) | |
| { | |
| state ^= ((uint32_t)(str[i])) * HXOR32_MULTIPLIER; | |
| state ^= state << 13; | |
| state ^= state >> 17; | |
| state ^= state << 5; | |
| } | |
| return state; | |
| } | |
| /* HXOR64 Hash Implementation in C */ | |
| #define HXOR64_INITIAL 0xc4ceb9fe1a85ec53L | |
| #define HXOR64_MULTIPLIER 0xff51afd7ed558ccdL | |
| uint64_t hxor64_str_hash(const char *str) | |
| { | |
| char c; | |
| uint64_t state = HXOR64_INITIAL; | |
| while ((c = *str++)) | |
| { | |
| state ^= ((uint64_t)(c)) * HXOR64_MULTIPLIER; | |
| state ^= state << 13; | |
| state ^= state >> 7; | |
| state ^= state << 17; | |
| } | |
| return state; | |
| } | |
| uint64_t hxor64_hash(const char *str, size_t length) | |
| { | |
| uint64_t state = HXOR64_INITIAL; | |
| for (size_t i = 0; i < length; ++i) | |
| { | |
| state ^= ((uint64_t)(str[i])) * HXOR64_MULTIPLIER; | |
| state ^= state << 13; | |
| state ^= state >> 7; | |
| state ^= state << 17; | |
| } | |
| return state; | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment