Created
March 16, 2018 20:16
-
-
Save TheServer201/c1fb7a119b9f8b6ee0138e88884a48da to your computer and use it in GitHub Desktop.
Implementation of xxHash for small input
This file contains 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
#include <stdlib.h> | |
#include <intrin.h> | |
#define PRIME32_1 0x9E3779B1 | |
#define PRIME32_2 0x85EBCA77 | |
#define PRIME32_3 0xC2B2AE3D | |
#define PRIME32_4 0x27D4EB2F | |
#define PRIME32_5 0x165667B1 | |
uint32_t xxHash(const char *input, size_t size, uint32_t seed) { | |
size_t index; | |
uint32_t hash = seed + size + PRIME32_5; | |
for (index = 0; size - index >= 4; index += 4) { | |
uint32_t value; | |
memcpy(&value, input + index, sizeof(value)); | |
hash += value * PRIME32_3; | |
hash = _rotl(hash, 17) * PRIME32_4; | |
} | |
for (; index < size; index++) { | |
uint8_t value = input[index]; | |
hash += value * PRIME32_5; | |
hash = _rotl(hash, 11) * PRIME32_1; | |
} | |
hash ^= hash >> 15; | |
hash *= PRIME32_2; | |
hash ^= hash >> 13; | |
hash *= PRIME32_3; | |
return hash ^ hash >> 16; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment