-
Notifications
You must be signed in to change notification settings - Fork 6
/
murmurhash3.c
189 lines (167 loc) · 4.85 KB
/
murmurhash3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (c) 2022, smartmx <smartmx@qq.com>
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-04-03 smartmx the first version
*
*/
/*
* @Note:
* This is murmur3 hash algorithm: https://github.com/aappleby/smhasher.
* But coded with c instead.
* This repository only accomplish 32bit hash algorithm.
*/
#include "murmurhash3.h"
#define ROTL32(x,y) ((x << y) | (x >> (32 - y)))
/**
* calculate hash_code.
*
* @param hash_key the hash_key start address.
* @param len the length of the hash_key.
*
* @return type uint32_t, the result of calculated value.
*/
uint32_t murmurhash3_caculate32(const void *hash_key, uint32_t len)
{
const uint8_t *hash_key_ptr = (const uint8_t *)hash_key;
int nblocks = len / 4;
uint32_t hash = MURMURHASH3_SEED_VALUE;
uint32_t data;
/* body */
while (nblocks > 0)
{
/* get 32bit data */
data = (hash_key_ptr[0] << 24) | (hash_key_ptr[1] << 16) | (hash_key_ptr[2] << 8) | (hash_key_ptr[3]);
data *= MURMURHASH3_C1_VALUE;
data = ROTL32(data, MURMURHASH3_R1_VALUE);
data *= MURMURHASH3_C2_VALUE;
hash ^= data;
hash = ROTL32(hash, MURMURHASH3_R2_VALUE);
hash = hash * MURMURHASH3_M_VALUE + MURMURHASH3_N_VALUE;
hash_key_ptr += 4;
nblocks--;
}
/* tail */
data = 0;
switch (len & 3)
{
case 3:
data ^= (hash_key_ptr[2] << 16);/* @suppress("No break at end of case") */
case 2:
data ^= (hash_key_ptr[1] << 8); /* @suppress("No break at end of case") */
case 1:
data ^= hash_key_ptr[0];
data *= MURMURHASH3_C1_VALUE;
data = ROTL32(data, MURMURHASH3_R1_VALUE);
data *= MURMURHASH3_C2_VALUE;
hash ^= data;
}
/* finalization */
hash ^= len;
/* Finalization mix - force all bits of a hash block to avalanche */
hash ^= hash >> 16;
hash *= 0x85ebca6b;
hash ^= hash >> 13;
hash *= 0xc2b2ae35;
hash ^= hash >> 16;
return hash;
}
/**
* upper all lower letters in hash key and calculate the hash_code.
*
* @param c the char value.
*
* @return type uint8_t, the upper char value.
*/
static uint8_t murmurhash3_lower_char_upper(uint8_t c)
{
if ((c >= 'a') && (c <= 'z'))
return c + ('A' - 'a');
return c;
}
/**
* upper all lower letters in hash key and calculate the hash_code.
*
* @param c the char value.
*
* @return type uint8_t, the upper char value.
*/
uint8_t murmurhash3_lower_char_upper_memcmp(const void *src1, const void *src2, uint32_t len)
{
uint8_t *src1_char = (uint8_t *)src1;
uint8_t *src2_char = (uint8_t *)src2;
uint32_t all_len = len;
while (all_len)
{
if (murmurhash3_lower_char_upper(*src1_char) == murmurhash3_lower_char_upper(*src2_char))
{
src1_char++;
src2_char++;
all_len--;
}
else
{
return (len - all_len);
}
}
return 0;
}
/**
* upper all lower letters in hash key and calculate the hash_code.
*
* @param hash_key the hash_key start address.
* @param len the length of the hash_key.
*
* @return type uint32_t, the result of calculated value.
*/
uint32_t murmurhash3_upper_caculate32(const void *hash_key, uint32_t len)
{
const uint8_t *hash_key_ptr = (const uint8_t *)hash_key;
int nblocks = len / 4;
uint32_t hash = MURMURHASH3_SEED_VALUE;
uint32_t data;
/* body */
while (nblocks > 0)
{
/* get 32bit data */
data = (murmurhash3_lower_char_upper(hash_key_ptr[0]) << 24) | \
(murmurhash3_lower_char_upper(hash_key_ptr[1]) << 16) | \
(murmurhash3_lower_char_upper(hash_key_ptr[2]) << 8) | \
(murmurhash3_lower_char_upper(hash_key_ptr[3]));
data *= MURMURHASH3_C1_VALUE;
data = ROTL32(data, MURMURHASH3_R1_VALUE);
data *= MURMURHASH3_C2_VALUE;
hash ^= data;
hash = ROTL32(hash, MURMURHASH3_R2_VALUE);
hash = hash * MURMURHASH3_M_VALUE + MURMURHASH3_N_VALUE;
hash_key_ptr += 4;
nblocks--;
}
/* tail */
data = 0;
switch (len & 3)
{
case 3:
data ^= (murmurhash3_lower_char_upper(hash_key_ptr[2]) << 16);/* @suppress("No break at end of case") */
case 2:
data ^= (murmurhash3_lower_char_upper(hash_key_ptr[1]) << 8); /* @suppress("No break at end of case") */
case 1:
data ^= murmurhash3_lower_char_upper(hash_key_ptr[0]);
data *= MURMURHASH3_C1_VALUE;
data = ROTL32(data, MURMURHASH3_R1_VALUE);
data *= MURMURHASH3_C2_VALUE;
hash ^= data;
}
/* finalization */
hash ^= len;
/* Finalization mix - force all bits of a hash block to avalanche */
hash ^= hash >> 16;
hash *= 0x85ebca6b;
hash ^= hash >> 13;
hash *= 0xc2b2ae35;
hash ^= hash >> 16;
return hash;
}