summaryrefslogtreecommitdiffstats
path: root/clib/hash.h
blob: 189dacd87b09c45283e829d4a26c6325f20ca6b6 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: clib/hash.h $                                                 */
/*                                                                        */
/* OpenPOWER FFS Project                                                  */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2015                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

/*
 *   File: hash.h
 * Author: Shaun Wetzstein <shaun@us.ibm.com>
 *  Descr: Various int32 hash functions
 *   Note:
 *   Date: 10/03/10
 */

#ifndef __HASH_H__
#define __HASH_H__

#include <stdint.h>

/* ======================================================================= */

static inline int32_t int32_hash1(int32_t);
static inline int32_t int32_hash2(int32_t);
static inline int32_t int32_hash3(int32_t);

typedef uint32_t(*hash_t) (char *, uint32_t);

static inline uint32_t rs_hash(char *, uint32_t);
static inline uint32_t js_hash(char *, uint32_t);
static inline uint32_t pjw_hash(char *, uint32_t);
static inline uint32_t elf_hash(char *, uint32_t);
static inline uint32_t bkdr_hash(char *, uint32_t);
static inline uint32_t sdbm_hash(char *, uint32_t);
static inline uint32_t djb_hash(char *, uint32_t);
static inline uint32_t dek_hash(char *, uint32_t);
static inline uint32_t bp_hash(char *, uint32_t);
static inline uint32_t fnv_hash(char *, uint32_t);
static inline uint32_t ap_hash(char *, uint32_t);

/* ======================================================================= */

static inline int32_t int32_hash1(int32_t key)
{
	key = ~key + (key << 15);
	key = key ^ (key >> 12);
	key = key + (key << 2);
	key = key ^ (key >> 4);
	key = key * 2057;
	key = key ^ (key >> 16);

	return key;
}

static inline int32_t int32_hash2(int32_t key)
{
	key = (key + 0x7ed55d16) + (key << 12);
	key = (key ^ 0xc761c23c) ^ (key >> 19);
	key = (key + 0x165667b1) + (key << 5);
	key = (key + 0xd3a2646c) ^ (key << 9);
	key = (key + 0xfd7046c5) + (key << 3);
	key = (key ^ 0xb55a4f09) ^ (key >> 16);

	return key;
}

static inline int32_t int32_hash3(int32_t key)
{
	int32_t c2 = 0x27d4eb2d;

	key = (key ^ 61) ^ (key >> 16);
	key = key + (key << 3);
	key = key ^ (key >> 4);
	key = key * c2;
	key = key ^ (key >> 15);

	return key;
}

/* ======================================================================= */

static inline uint32_t rs_hash(char *str, uint32_t len)
{
	uint32_t b = 378551;
	uint32_t a = 63689;
	uint32_t hash = 0;
	uint32_t i = 0;

	for (i = 0; i < len; str++, i++) {
		hash = hash * a + (*str);
		a = a * b;
	}

	return hash;
}

static inline uint32_t js_hash(char *str, uint32_t len)
{
	uint32_t hash = 1315423911;
	uint32_t i = 0;

	for (i = 0; i < len; str++, i++) {
		hash ^= ((hash << 5) + (*str) + (hash >> 2));
	}

	return hash;
}

static inline uint32_t pjw_hash(char *str, uint32_t len)
{
	const uint32_t BitsInUnsignedInt = (uint32_t) (sizeof(uint32_t) * 8);
	const uint32_t ThreeQuarters = (uint32_t) ((BitsInUnsignedInt * 3) / 4);
	const uint32_t OneEighth = (uint32_t) (BitsInUnsignedInt / 8);
	const uint32_t HighBits =
	    (uint32_t) (0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
	uint32_t hash = 0;
	uint32_t test = 0;
	uint32_t i = 0;

	for (i = 0; i < len; str++, i++) {
		hash = (hash << OneEighth) + (*str);

		if ((test = hash & HighBits) != 0) {
			hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
		}
	}

	return hash;
}

static inline uint32_t elf_hash(char *str, uint32_t len)
{
	uint32_t hash = 0;
	uint32_t x = 0;
	uint32_t i = 0;

	for (i = 0; i < len; str++, i++) {
		hash = (hash << 4) + (*str);
		if ((x = hash & 0xF0000000L) != 0) {
			hash ^= (x >> 24);
		}
		hash &= ~x;
	}

	return hash;
}

static inline uint32_t bkdr_hash(char *str, uint32_t len)
{
	uint32_t seed = 131;	/* 31 131 1313 13131 131313 etc.. */
	uint32_t hash = 0;
	uint32_t i = 0;

	for (i = 0; i < len; str++, i++) {
		hash = (hash * seed) + (*str);
	}

	return hash;
}

static inline uint32_t sdbm_hash(char *str, uint32_t len)
{
	uint32_t hash = 0;
	uint32_t i = 0;

	for (i = 0; i < len; str++, i++) {
		hash = (*str) + (hash << 6) + (hash << 16) - hash;
	}

	return hash;
}

static inline uint32_t djb_hash(char *str, uint32_t len)
{
	uint32_t hash = 5381;
	uint32_t i = 0;

	for (i = 0; i < len; str++, i++) {
		hash = ((hash << 5) + hash) + (*str);
	}

	return hash;
}

static inline uint32_t dek_hash(char *str, uint32_t len)
{
	uint32_t hash = len;
	uint32_t i = 0;

	for (i = 0; i < len; str++, i++) {
		hash = ((hash << 5) ^ (hash >> 27)) ^ (*str);
	}
	return hash;
}

static inline uint32_t bp_hash(char *str, uint32_t len)
{
	uint32_t hash = 0;
	uint32_t i = 0;

	for (i = 0; i < len; str++, i++) {
		hash = hash << 7 ^ (*str);
	}

	return hash;
}

static inline uint32_t fnv_hash(char *str, uint32_t len)
{
	const uint32_t fnv_prime = 0x811C9DC5;
	uint32_t hash = 0;
	uint32_t i = 0;

	for (i = 0; i < len; str++, i++) {
		hash *= fnv_prime;
		hash ^= (*str);
	}

	return hash;
}

static inline uint32_t ap_hash(char *str, uint32_t len)
{
	uint32_t hash = 0xAAAAAAAA;
	uint32_t i = 0;

	for (i = 0; i < len; str++, i++) {
		hash ^= ((i & 1) == 0) ? ((hash << 7) ^ (*str) * (hash >> 3)) :
		    (~((hash << 11) + ((*str) ^ (hash >> 5))));
	}

	return hash;
}

/* ======================================================================= */

#endif				/* __HASH_H__ */
OpenPOWER on IntegriCloud