Amit Pundir | 33109f7 | 2020-02-07 22:26:08 +0530 | [diff] [blame] | 1 | #include <string.h> |
| 2 | #include "hash.h" |
| 3 | |
| 4 | unsigned int hash_mem(const void *data, unsigned int len) |
| 5 | { |
| 6 | unsigned int h; |
| 7 | unsigned int i; |
| 8 | |
| 9 | h = len; |
| 10 | |
| 11 | for (i = 0; i < len; ++i) |
| 12 | h = ((h >> 27) ^ (h << 5)) ^ ((const unsigned char *)data)[i]; |
| 13 | |
| 14 | return h; |
| 15 | } |
| 16 | |
| 17 | unsigned int hash_string(const char *value) |
| 18 | { |
| 19 | return hash_mem(value, strlen(value)); |
| 20 | } |
| 21 | |
| 22 | unsigned int hash_u32(uint32_t value) |
| 23 | { |
| 24 | return value * 2654435761UL; |
| 25 | } |
| 26 | |
| 27 | unsigned int hash_u64(uint64_t value) |
| 28 | { |
| 29 | return hash_u32(value & 0xffffffff) ^ hash_u32(value >> 32); |
| 30 | } |
| 31 | |
| 32 | unsigned int hash_pointer(void *value) |
| 33 | { |
| 34 | if (sizeof(value) == sizeof(uint64_t)) |
| 35 | return hash_u64((long)value); |
| 36 | return hash_u32((long)value); |
| 37 | } |