blob: ecfbc0a48819d5c3cc970b693a82bbdda04e2ca4 [file] [log] [blame]
Amit Pundir33109f72020-02-07 22:26:08 +05301#include <string.h>
2#include "hash.h"
3
4unsigned 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
17unsigned int hash_string(const char *value)
18{
19 return hash_mem(value, strlen(value));
20}
21
22unsigned int hash_u32(uint32_t value)
23{
24 return value * 2654435761UL;
25}
26
27unsigned int hash_u64(uint64_t value)
28{
29 return hash_u32(value & 0xffffffff) ^ hash_u32(value >> 32);
30}
31
32unsigned 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}