100bf4279Sespie /* An expandable hash tables datatype.
2*150b7e42Smiod Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
3*150b7e42Smiod Free Software Foundation, Inc.
400bf4279Sespie Contributed by Vladimir Makarov (vmakarov@cygnus.com).
500bf4279Sespie
600bf4279Sespie This file is part of the libiberty library.
700bf4279Sespie Libiberty is free software; you can redistribute it and/or
800bf4279Sespie modify it under the terms of the GNU Library General Public
900bf4279Sespie License as published by the Free Software Foundation; either
1000bf4279Sespie version 2 of the License, or (at your option) any later version.
1100bf4279Sespie
1200bf4279Sespie Libiberty is distributed in the hope that it will be useful,
1300bf4279Sespie but WITHOUT ANY WARRANTY; without even the implied warranty of
1400bf4279Sespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1500bf4279Sespie Library General Public License for more details.
1600bf4279Sespie
1700bf4279Sespie You should have received a copy of the GNU Library General Public
1800bf4279Sespie License along with libiberty; see the file COPYING.LIB. If
19*150b7e42Smiod not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20*150b7e42Smiod Boston, MA 02110-1301, USA. */
2100bf4279Sespie
2200bf4279Sespie /* This package implements basic hash table functionality. It is possible
2300bf4279Sespie to search for an entry, create an entry and destroy an entry.
2400bf4279Sespie
2500bf4279Sespie Elements in the table are generic pointers.
2600bf4279Sespie
2700bf4279Sespie The size of the table is not fixed; if the occupancy of the table
2800bf4279Sespie grows too high the hash table will be expanded.
2900bf4279Sespie
3000bf4279Sespie The abstract data implementation is based on generalized Algorithm D
3100bf4279Sespie from Knuth's book "The art of computer programming". Hash table is
3200bf4279Sespie expanded by creation of new hash table and transferring elements from
3300bf4279Sespie the old table to the new table. */
3400bf4279Sespie
3500bf4279Sespie #ifdef HAVE_CONFIG_H
3600bf4279Sespie #include "config.h"
3700bf4279Sespie #endif
3800bf4279Sespie
3937c53322Sespie #include <sys/types.h>
4037c53322Sespie
4100bf4279Sespie #ifdef HAVE_STDLIB_H
4200bf4279Sespie #include <stdlib.h>
4300bf4279Sespie #endif
4437c53322Sespie #ifdef HAVE_STRING_H
4537c53322Sespie #include <string.h>
4637c53322Sespie #endif
47*150b7e42Smiod #ifdef HAVE_MALLOC_H
48*150b7e42Smiod #include <malloc.h>
49*150b7e42Smiod #endif
50*150b7e42Smiod #ifdef HAVE_LIMITS_H
51*150b7e42Smiod #include <limits.h>
52*150b7e42Smiod #endif
53*150b7e42Smiod #ifdef HAVE_STDINT_H
54*150b7e42Smiod #include <stdint.h>
55*150b7e42Smiod #endif
5637c53322Sespie
5737c53322Sespie #include <stdio.h>
5837c53322Sespie
5900bf4279Sespie #include "libiberty.h"
60*150b7e42Smiod #include "ansidecl.h"
6100bf4279Sespie #include "hashtab.h"
6200bf4279Sespie
63*150b7e42Smiod #ifndef CHAR_BIT
64*150b7e42Smiod #define CHAR_BIT 8
65*150b7e42Smiod #endif
6600bf4279Sespie
67*150b7e42Smiod static unsigned int higher_prime_index (unsigned long);
68*150b7e42Smiod static hashval_t htab_mod_1 (hashval_t, hashval_t, hashval_t, int);
69*150b7e42Smiod static hashval_t htab_mod (hashval_t, htab_t);
70*150b7e42Smiod static hashval_t htab_mod_m2 (hashval_t, htab_t);
71*150b7e42Smiod static hashval_t hash_pointer (const void *);
72*150b7e42Smiod static int eq_pointer (const void *, const void *);
73*150b7e42Smiod static int htab_expand (htab_t);
74*150b7e42Smiod static PTR *find_empty_slot_for_expand (htab_t, hashval_t);
7537c53322Sespie
7637c53322Sespie /* At some point, we could make these be NULL, and modify the
7737c53322Sespie hash-table routines to handle NULL specially; that would avoid
7837c53322Sespie function-call overhead for the common case of hashing pointers. */
7937c53322Sespie htab_hash htab_hash_pointer = hash_pointer;
8037c53322Sespie htab_eq htab_eq_pointer = eq_pointer;
8137c53322Sespie
82*150b7e42Smiod /* Table of primes and multiplicative inverses.
8300bf4279Sespie
84*150b7e42Smiod Note that these are not minimally reduced inverses. Unlike when generating
85*150b7e42Smiod code to divide by a constant, we want to be able to use the same algorithm
86*150b7e42Smiod all the time. All of these inverses (are implied to) have bit 32 set.
87*150b7e42Smiod
88*150b7e42Smiod For the record, here's the function that computed the table; it's a
89*150b7e42Smiod vastly simplified version of the function of the same name from gcc. */
90*150b7e42Smiod
91*150b7e42Smiod #if 0
92*150b7e42Smiod unsigned int
93*150b7e42Smiod ceil_log2 (unsigned int x)
9400bf4279Sespie {
95*150b7e42Smiod int i;
96*150b7e42Smiod for (i = 31; i >= 0 ; --i)
97*150b7e42Smiod if (x > (1u << i))
98*150b7e42Smiod return i+1;
99*150b7e42Smiod abort ();
100*150b7e42Smiod }
101*150b7e42Smiod
102*150b7e42Smiod unsigned int
103*150b7e42Smiod choose_multiplier (unsigned int d, unsigned int *mlp, unsigned char *shiftp)
104*150b7e42Smiod {
105*150b7e42Smiod unsigned long long mhigh;
106*150b7e42Smiod double nx;
107*150b7e42Smiod int lgup, post_shift;
108*150b7e42Smiod int pow, pow2;
109*150b7e42Smiod int n = 32, precision = 32;
110*150b7e42Smiod
111*150b7e42Smiod lgup = ceil_log2 (d);
112*150b7e42Smiod pow = n + lgup;
113*150b7e42Smiod pow2 = n + lgup - precision;
114*150b7e42Smiod
115*150b7e42Smiod nx = ldexp (1.0, pow) + ldexp (1.0, pow2);
116*150b7e42Smiod mhigh = nx / d;
117*150b7e42Smiod
118*150b7e42Smiod *shiftp = lgup - 1;
119*150b7e42Smiod *mlp = mhigh;
120*150b7e42Smiod return mhigh >> 32;
121*150b7e42Smiod }
122*150b7e42Smiod #endif
123*150b7e42Smiod
124*150b7e42Smiod struct prime_ent
125*150b7e42Smiod {
126*150b7e42Smiod hashval_t prime;
127*150b7e42Smiod hashval_t inv;
128*150b7e42Smiod hashval_t inv_m2; /* inverse of prime-2 */
129*150b7e42Smiod hashval_t shift;
13037c53322Sespie };
13100bf4279Sespie
132*150b7e42Smiod static struct prime_ent const prime_tab[] = {
133*150b7e42Smiod { 7, 0x24924925, 0x9999999b, 2 },
134*150b7e42Smiod { 13, 0x3b13b13c, 0x745d1747, 3 },
135*150b7e42Smiod { 31, 0x08421085, 0x1a7b9612, 4 },
136*150b7e42Smiod { 61, 0x0c9714fc, 0x15b1e5f8, 5 },
137*150b7e42Smiod { 127, 0x02040811, 0x0624dd30, 6 },
138*150b7e42Smiod { 251, 0x05197f7e, 0x073260a5, 7 },
139*150b7e42Smiod { 509, 0x01824366, 0x02864fc8, 8 },
140*150b7e42Smiod { 1021, 0x00c0906d, 0x014191f7, 9 },
141*150b7e42Smiod { 2039, 0x0121456f, 0x0161e69e, 10 },
142*150b7e42Smiod { 4093, 0x00300902, 0x00501908, 11 },
143*150b7e42Smiod { 8191, 0x00080041, 0x00180241, 12 },
144*150b7e42Smiod { 16381, 0x000c0091, 0x00140191, 13 },
145*150b7e42Smiod { 32749, 0x002605a5, 0x002a06e6, 14 },
146*150b7e42Smiod { 65521, 0x000f00e2, 0x00110122, 15 },
147*150b7e42Smiod { 131071, 0x00008001, 0x00018003, 16 },
148*150b7e42Smiod { 262139, 0x00014002, 0x0001c004, 17 },
149*150b7e42Smiod { 524287, 0x00002001, 0x00006001, 18 },
150*150b7e42Smiod { 1048573, 0x00003001, 0x00005001, 19 },
151*150b7e42Smiod { 2097143, 0x00004801, 0x00005801, 20 },
152*150b7e42Smiod { 4194301, 0x00000c01, 0x00001401, 21 },
153*150b7e42Smiod { 8388593, 0x00001e01, 0x00002201, 22 },
154*150b7e42Smiod { 16777213, 0x00000301, 0x00000501, 23 },
155*150b7e42Smiod { 33554393, 0x00001381, 0x00001481, 24 },
156*150b7e42Smiod { 67108859, 0x00000141, 0x000001c1, 25 },
157*150b7e42Smiod { 134217689, 0x000004e1, 0x00000521, 26 },
158*150b7e42Smiod { 268435399, 0x00000391, 0x000003b1, 27 },
159*150b7e42Smiod { 536870909, 0x00000019, 0x00000029, 28 },
160*150b7e42Smiod { 1073741789, 0x0000008d, 0x00000095, 29 },
161*150b7e42Smiod { 2147483647, 0x00000003, 0x00000007, 30 },
162*150b7e42Smiod /* Avoid "decimal constant so large it is unsigned" for 4294967291. */
163*150b7e42Smiod { 0xfffffffb, 0x00000006, 0x00000008, 31 }
164*150b7e42Smiod };
165*150b7e42Smiod
166*150b7e42Smiod /* The following function returns an index into the above table of the
167*150b7e42Smiod nearest prime number which is greater than N, and near a power of two. */
168*150b7e42Smiod
169*150b7e42Smiod static unsigned int
higher_prime_index(unsigned long n)170*150b7e42Smiod higher_prime_index (unsigned long n)
171*150b7e42Smiod {
172*150b7e42Smiod unsigned int low = 0;
173*150b7e42Smiod unsigned int high = sizeof(prime_tab) / sizeof(prime_tab[0]);
17437c53322Sespie
17537c53322Sespie while (low != high)
17600bf4279Sespie {
177*150b7e42Smiod unsigned int mid = low + (high - low) / 2;
178*150b7e42Smiod if (n > prime_tab[mid].prime)
17937c53322Sespie low = mid + 1;
18037c53322Sespie else
18137c53322Sespie high = mid;
18200bf4279Sespie }
18337c53322Sespie
18437c53322Sespie /* If we've run out of primes, abort. */
185*150b7e42Smiod if (n > prime_tab[low].prime)
18637c53322Sespie {
18737c53322Sespie fprintf (stderr, "Cannot find prime bigger than %lu\n", n);
18837c53322Sespie abort ();
18937c53322Sespie }
19037c53322Sespie
191*150b7e42Smiod return low;
19237c53322Sespie }
19337c53322Sespie
19437c53322Sespie /* Returns a hash code for P. */
19537c53322Sespie
19637c53322Sespie static hashval_t
hash_pointer(const PTR p)197*150b7e42Smiod hash_pointer (const PTR p)
19837c53322Sespie {
19937c53322Sespie return (hashval_t) ((long)p >> 3);
20037c53322Sespie }
20137c53322Sespie
20237c53322Sespie /* Returns non-zero if P1 and P2 are equal. */
20337c53322Sespie
20437c53322Sespie static int
eq_pointer(const PTR p1,const PTR p2)205*150b7e42Smiod eq_pointer (const PTR p1, const PTR p2)
20637c53322Sespie {
20737c53322Sespie return p1 == p2;
20800bf4279Sespie }
20900bf4279Sespie
210*150b7e42Smiod
211*150b7e42Smiod /* The parens around the function names in the next two definitions
212*150b7e42Smiod are essential in order to prevent macro expansions of the name.
213*150b7e42Smiod The bodies, however, are expanded as expected, so they are not
214*150b7e42Smiod recursive definitions. */
215*150b7e42Smiod
216*150b7e42Smiod /* Return the current size of given hash table. */
217*150b7e42Smiod
218*150b7e42Smiod #define htab_size(htab) ((htab)->size)
219*150b7e42Smiod
size_t(htab_size)220*150b7e42Smiod size_t
221*150b7e42Smiod (htab_size) (htab_t htab)
222*150b7e42Smiod {
223*150b7e42Smiod return htab_size (htab);
224*150b7e42Smiod }
225*150b7e42Smiod
226*150b7e42Smiod /* Return the current number of elements in given hash table. */
227*150b7e42Smiod
228*150b7e42Smiod #define htab_elements(htab) ((htab)->n_elements - (htab)->n_deleted)
229*150b7e42Smiod
size_t(htab_elements)230*150b7e42Smiod size_t
231*150b7e42Smiod (htab_elements) (htab_t htab)
232*150b7e42Smiod {
233*150b7e42Smiod return htab_elements (htab);
234*150b7e42Smiod }
235*150b7e42Smiod
236*150b7e42Smiod /* Return X % Y. */
237*150b7e42Smiod
238*150b7e42Smiod static inline hashval_t
htab_mod_1(hashval_t x,hashval_t y,hashval_t inv,int shift)239*150b7e42Smiod htab_mod_1 (hashval_t x, hashval_t y, hashval_t inv, int shift)
240*150b7e42Smiod {
241*150b7e42Smiod /* The multiplicative inverses computed above are for 32-bit types, and
242*150b7e42Smiod requires that we be able to compute a highpart multiply. */
243*150b7e42Smiod #ifdef UNSIGNED_64BIT_TYPE
244*150b7e42Smiod __extension__ typedef UNSIGNED_64BIT_TYPE ull;
245*150b7e42Smiod if (sizeof (hashval_t) * CHAR_BIT <= 32)
246*150b7e42Smiod {
247*150b7e42Smiod hashval_t t1, t2, t3, t4, q, r;
248*150b7e42Smiod
249*150b7e42Smiod t1 = ((ull)x * inv) >> 32;
250*150b7e42Smiod t2 = x - t1;
251*150b7e42Smiod t3 = t2 >> 1;
252*150b7e42Smiod t4 = t1 + t3;
253*150b7e42Smiod q = t4 >> shift;
254*150b7e42Smiod r = x - (q * y);
255*150b7e42Smiod
256*150b7e42Smiod return r;
257*150b7e42Smiod }
258*150b7e42Smiod #endif
259*150b7e42Smiod
260*150b7e42Smiod /* Otherwise just use the native division routines. */
261*150b7e42Smiod return x % y;
262*150b7e42Smiod }
263*150b7e42Smiod
264*150b7e42Smiod /* Compute the primary hash for HASH given HTAB's current size. */
265*150b7e42Smiod
266*150b7e42Smiod static inline hashval_t
htab_mod(hashval_t hash,htab_t htab)267*150b7e42Smiod htab_mod (hashval_t hash, htab_t htab)
268*150b7e42Smiod {
269*150b7e42Smiod const struct prime_ent *p = &prime_tab[htab->size_prime_index];
270*150b7e42Smiod return htab_mod_1 (hash, p->prime, p->inv, p->shift);
271*150b7e42Smiod }
272*150b7e42Smiod
273*150b7e42Smiod /* Compute the secondary hash for HASH given HTAB's current size. */
274*150b7e42Smiod
275*150b7e42Smiod static inline hashval_t
htab_mod_m2(hashval_t hash,htab_t htab)276*150b7e42Smiod htab_mod_m2 (hashval_t hash, htab_t htab)
277*150b7e42Smiod {
278*150b7e42Smiod const struct prime_ent *p = &prime_tab[htab->size_prime_index];
279*150b7e42Smiod return 1 + htab_mod_1 (hash, p->prime - 2, p->inv_m2, p->shift);
280*150b7e42Smiod }
281*150b7e42Smiod
28200bf4279Sespie /* This function creates table with length slightly longer than given
28300bf4279Sespie source length. Created hash table is initiated as empty (all the
284*150b7e42Smiod hash table entries are HTAB_EMPTY_ENTRY). The function returns the
28537c53322Sespie created hash table, or NULL if memory allocation fails. */
28600bf4279Sespie
28737c53322Sespie htab_t
htab_create_alloc(size_t size,htab_hash hash_f,htab_eq eq_f,htab_del del_f,htab_alloc alloc_f,htab_free free_f)288*150b7e42Smiod htab_create_alloc (size_t size, htab_hash hash_f, htab_eq eq_f,
289*150b7e42Smiod htab_del del_f, htab_alloc alloc_f, htab_free free_f)
29000bf4279Sespie {
29137c53322Sespie htab_t result;
292*150b7e42Smiod unsigned int size_prime_index;
29300bf4279Sespie
294*150b7e42Smiod size_prime_index = higher_prime_index (size);
295*150b7e42Smiod size = prime_tab[size_prime_index].prime;
296*150b7e42Smiod
29737c53322Sespie result = (htab_t) (*alloc_f) (1, sizeof (struct htab));
29837c53322Sespie if (result == NULL)
29937c53322Sespie return NULL;
30037c53322Sespie result->entries = (PTR *) (*alloc_f) (size, sizeof (PTR));
30137c53322Sespie if (result->entries == NULL)
30237c53322Sespie {
30337c53322Sespie if (free_f != NULL)
30437c53322Sespie (*free_f) (result);
30537c53322Sespie return NULL;
30637c53322Sespie }
30700bf4279Sespie result->size = size;
308*150b7e42Smiod result->size_prime_index = size_prime_index;
30937c53322Sespie result->hash_f = hash_f;
31037c53322Sespie result->eq_f = eq_f;
31137c53322Sespie result->del_f = del_f;
31237c53322Sespie result->alloc_f = alloc_f;
31337c53322Sespie result->free_f = free_f;
31400bf4279Sespie return result;
31500bf4279Sespie }
31600bf4279Sespie
317707f648cSespie /* As above, but use the variants of alloc_f and free_f which accept
318707f648cSespie an extra argument. */
319707f648cSespie
320707f648cSespie htab_t
htab_create_alloc_ex(size_t size,htab_hash hash_f,htab_eq eq_f,htab_del del_f,void * alloc_arg,htab_alloc_with_arg alloc_f,htab_free_with_arg free_f)321*150b7e42Smiod htab_create_alloc_ex (size_t size, htab_hash hash_f, htab_eq eq_f,
322*150b7e42Smiod htab_del del_f, void *alloc_arg,
323*150b7e42Smiod htab_alloc_with_arg alloc_f,
324*150b7e42Smiod htab_free_with_arg free_f)
325707f648cSespie {
326707f648cSespie htab_t result;
327*150b7e42Smiod unsigned int size_prime_index;
328707f648cSespie
329*150b7e42Smiod size_prime_index = higher_prime_index (size);
330*150b7e42Smiod size = prime_tab[size_prime_index].prime;
331*150b7e42Smiod
332707f648cSespie result = (htab_t) (*alloc_f) (alloc_arg, 1, sizeof (struct htab));
333707f648cSespie if (result == NULL)
334707f648cSespie return NULL;
335707f648cSespie result->entries = (PTR *) (*alloc_f) (alloc_arg, size, sizeof (PTR));
336707f648cSespie if (result->entries == NULL)
337707f648cSespie {
338707f648cSespie if (free_f != NULL)
339707f648cSespie (*free_f) (alloc_arg, result);
340707f648cSespie return NULL;
341707f648cSespie }
342707f648cSespie result->size = size;
343*150b7e42Smiod result->size_prime_index = size_prime_index;
344707f648cSespie result->hash_f = hash_f;
345707f648cSespie result->eq_f = eq_f;
346707f648cSespie result->del_f = del_f;
347707f648cSespie result->alloc_arg = alloc_arg;
348707f648cSespie result->alloc_with_arg_f = alloc_f;
349707f648cSespie result->free_with_arg_f = free_f;
350707f648cSespie return result;
351707f648cSespie }
352707f648cSespie
353707f648cSespie /* Update the function pointers and allocation parameter in the htab_t. */
354707f648cSespie
355707f648cSespie void
htab_set_functions_ex(htab_t htab,htab_hash hash_f,htab_eq eq_f,htab_del del_f,PTR alloc_arg,htab_alloc_with_arg alloc_f,htab_free_with_arg free_f)356*150b7e42Smiod htab_set_functions_ex (htab_t htab, htab_hash hash_f, htab_eq eq_f,
357*150b7e42Smiod htab_del del_f, PTR alloc_arg,
358*150b7e42Smiod htab_alloc_with_arg alloc_f, htab_free_with_arg free_f)
359707f648cSespie {
360707f648cSespie htab->hash_f = hash_f;
361707f648cSespie htab->eq_f = eq_f;
362707f648cSespie htab->del_f = del_f;
363707f648cSespie htab->alloc_arg = alloc_arg;
364707f648cSespie htab->alloc_with_arg_f = alloc_f;
365707f648cSespie htab->free_with_arg_f = free_f;
366707f648cSespie }
367707f648cSespie
36837c53322Sespie /* These functions exist solely for backward compatibility. */
36937c53322Sespie
37037c53322Sespie #undef htab_create
37137c53322Sespie htab_t
htab_create(size_t size,htab_hash hash_f,htab_eq eq_f,htab_del del_f)372*150b7e42Smiod htab_create (size_t size, htab_hash hash_f, htab_eq eq_f, htab_del del_f)
37337c53322Sespie {
37437c53322Sespie return htab_create_alloc (size, hash_f, eq_f, del_f, xcalloc, free);
37537c53322Sespie }
37637c53322Sespie
37737c53322Sespie htab_t
htab_try_create(size_t size,htab_hash hash_f,htab_eq eq_f,htab_del del_f)378*150b7e42Smiod htab_try_create (size_t size, htab_hash hash_f, htab_eq eq_f, htab_del del_f)
37937c53322Sespie {
38037c53322Sespie return htab_create_alloc (size, hash_f, eq_f, del_f, calloc, free);
38137c53322Sespie }
38237c53322Sespie
38300bf4279Sespie /* This function frees all memory allocated for given hash table.
38400bf4279Sespie Naturally the hash table must already exist. */
38500bf4279Sespie
38600bf4279Sespie void
htab_delete(htab_t htab)387*150b7e42Smiod htab_delete (htab_t htab)
38800bf4279Sespie {
389*150b7e42Smiod size_t size = htab_size (htab);
390*150b7e42Smiod PTR *entries = htab->entries;
39137c53322Sespie int i;
39237c53322Sespie
39337c53322Sespie if (htab->del_f)
394*150b7e42Smiod for (i = size - 1; i >= 0; i--)
395*150b7e42Smiod if (entries[i] != HTAB_EMPTY_ENTRY && entries[i] != HTAB_DELETED_ENTRY)
396*150b7e42Smiod (*htab->del_f) (entries[i]);
39737c53322Sespie
39837c53322Sespie if (htab->free_f != NULL)
39937c53322Sespie {
400*150b7e42Smiod (*htab->free_f) (entries);
40137c53322Sespie (*htab->free_f) (htab);
40237c53322Sespie }
403707f648cSespie else if (htab->free_with_arg_f != NULL)
404707f648cSespie {
405*150b7e42Smiod (*htab->free_with_arg_f) (htab->alloc_arg, entries);
406707f648cSespie (*htab->free_with_arg_f) (htab->alloc_arg, htab);
407707f648cSespie }
40800bf4279Sespie }
40900bf4279Sespie
41000bf4279Sespie /* This function clears all entries in the given hash table. */
41100bf4279Sespie
41200bf4279Sespie void
htab_empty(htab_t htab)413*150b7e42Smiod htab_empty (htab_t htab)
41400bf4279Sespie {
415*150b7e42Smiod size_t size = htab_size (htab);
416*150b7e42Smiod PTR *entries = htab->entries;
41737c53322Sespie int i;
41837c53322Sespie
41937c53322Sespie if (htab->del_f)
420*150b7e42Smiod for (i = size - 1; i >= 0; i--)
421*150b7e42Smiod if (entries[i] != HTAB_EMPTY_ENTRY && entries[i] != HTAB_DELETED_ENTRY)
422*150b7e42Smiod (*htab->del_f) (entries[i]);
42337c53322Sespie
424*150b7e42Smiod memset (entries, 0, size * sizeof (PTR));
42537c53322Sespie }
42637c53322Sespie
42737c53322Sespie /* Similar to htab_find_slot, but without several unwanted side effects:
42837c53322Sespie - Does not call htab->eq_f when it finds an existing entry.
42937c53322Sespie - Does not change the count of elements/searches/collisions in the
43037c53322Sespie hash table.
43137c53322Sespie This function also assumes there are no deleted entries in the table.
43237c53322Sespie HASH is the hash value for the element to be inserted. */
43337c53322Sespie
43437c53322Sespie static PTR *
find_empty_slot_for_expand(htab_t htab,hashval_t hash)435*150b7e42Smiod find_empty_slot_for_expand (htab_t htab, hashval_t hash)
43637c53322Sespie {
437*150b7e42Smiod hashval_t index = htab_mod (hash, htab);
438*150b7e42Smiod size_t size = htab_size (htab);
43937c53322Sespie PTR *slot = htab->entries + index;
44037c53322Sespie hashval_t hash2;
44137c53322Sespie
442*150b7e42Smiod if (*slot == HTAB_EMPTY_ENTRY)
44337c53322Sespie return slot;
444*150b7e42Smiod else if (*slot == HTAB_DELETED_ENTRY)
44537c53322Sespie abort ();
44637c53322Sespie
447*150b7e42Smiod hash2 = htab_mod_m2 (hash, htab);
44837c53322Sespie for (;;)
44937c53322Sespie {
45037c53322Sespie index += hash2;
45137c53322Sespie if (index >= size)
45237c53322Sespie index -= size;
45337c53322Sespie
45437c53322Sespie slot = htab->entries + index;
455*150b7e42Smiod if (*slot == HTAB_EMPTY_ENTRY)
45637c53322Sespie return slot;
457*150b7e42Smiod else if (*slot == HTAB_DELETED_ENTRY)
45837c53322Sespie abort ();
45937c53322Sespie }
46000bf4279Sespie }
46100bf4279Sespie
46200bf4279Sespie /* The following function changes size of memory allocated for the
46300bf4279Sespie entries and repeatedly inserts the table elements. The occupancy
46400bf4279Sespie of the table after the call will be about 50%. Naturally the hash
46500bf4279Sespie table must already exist. Remember also that the place of the
46637c53322Sespie table entries is changed. If memory allocation failures are allowed,
46737c53322Sespie this function will return zero, indicating that the table could not be
46837c53322Sespie expanded. If all goes well, it will return a non-zero value. */
46900bf4279Sespie
47037c53322Sespie static int
htab_expand(htab_t htab)471*150b7e42Smiod htab_expand (htab_t htab)
47200bf4279Sespie {
47337c53322Sespie PTR *oentries;
47437c53322Sespie PTR *olimit;
47537c53322Sespie PTR *p;
47637c53322Sespie PTR *nentries;
477*150b7e42Smiod size_t nsize, osize, elts;
478*150b7e42Smiod unsigned int oindex, nindex;
47900bf4279Sespie
48037c53322Sespie oentries = htab->entries;
481*150b7e42Smiod oindex = htab->size_prime_index;
482*150b7e42Smiod osize = htab->size;
483*150b7e42Smiod olimit = oentries + osize;
484*150b7e42Smiod elts = htab_elements (htab);
48537c53322Sespie
486707f648cSespie /* Resize only when table after removal of unused elements is either
487707f648cSespie too full or too empty. */
488*150b7e42Smiod if (elts * 2 > osize || (elts * 8 < osize && osize > 32))
489*150b7e42Smiod {
490*150b7e42Smiod nindex = higher_prime_index (elts * 2);
491*150b7e42Smiod nsize = prime_tab[nindex].prime;
492*150b7e42Smiod }
493707f648cSespie else
494*150b7e42Smiod {
495*150b7e42Smiod nindex = oindex;
496*150b7e42Smiod nsize = osize;
497*150b7e42Smiod }
49837c53322Sespie
499707f648cSespie if (htab->alloc_with_arg_f != NULL)
500707f648cSespie nentries = (PTR *) (*htab->alloc_with_arg_f) (htab->alloc_arg, nsize,
501707f648cSespie sizeof (PTR *));
502707f648cSespie else
503707f648cSespie nentries = (PTR *) (*htab->alloc_f) (nsize, sizeof (PTR *));
50437c53322Sespie if (nentries == NULL)
50537c53322Sespie return 0;
50637c53322Sespie htab->entries = nentries;
50737c53322Sespie htab->size = nsize;
508*150b7e42Smiod htab->size_prime_index = nindex;
50937c53322Sespie htab->n_elements -= htab->n_deleted;
51037c53322Sespie htab->n_deleted = 0;
51137c53322Sespie
51237c53322Sespie p = oentries;
51337c53322Sespie do
5142e0724c7Sespie {
51537c53322Sespie PTR x = *p;
51637c53322Sespie
517*150b7e42Smiod if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
51837c53322Sespie {
51937c53322Sespie PTR *q = find_empty_slot_for_expand (htab, (*htab->hash_f) (x));
52037c53322Sespie
52137c53322Sespie *q = x;
52200bf4279Sespie }
5232e0724c7Sespie
52437c53322Sespie p++;
52501e9b57fSespie }
52637c53322Sespie while (p < olimit);
52737c53322Sespie
52837c53322Sespie if (htab->free_f != NULL)
52937c53322Sespie (*htab->free_f) (oentries);
530707f648cSespie else if (htab->free_with_arg_f != NULL)
531707f648cSespie (*htab->free_with_arg_f) (htab->alloc_arg, oentries);
53237c53322Sespie return 1;
53337c53322Sespie }
53437c53322Sespie
53537c53322Sespie /* This function searches for a hash table entry equal to the given
53637c53322Sespie element. It cannot be used to insert or delete an element. */
53737c53322Sespie
53837c53322Sespie PTR
htab_find_with_hash(htab_t htab,const PTR element,hashval_t hash)539*150b7e42Smiod htab_find_with_hash (htab_t htab, const PTR element, hashval_t hash)
54037c53322Sespie {
541*150b7e42Smiod hashval_t index, hash2;
54237c53322Sespie size_t size;
54337c53322Sespie PTR entry;
54437c53322Sespie
54500bf4279Sespie htab->searches++;
546*150b7e42Smiod size = htab_size (htab);
547*150b7e42Smiod index = htab_mod (hash, htab);
54837c53322Sespie
54937c53322Sespie entry = htab->entries[index];
550*150b7e42Smiod if (entry == HTAB_EMPTY_ENTRY
551*150b7e42Smiod || (entry != HTAB_DELETED_ENTRY && (*htab->eq_f) (entry, element)))
55237c53322Sespie return entry;
55337c53322Sespie
554*150b7e42Smiod hash2 = htab_mod_m2 (hash, htab);
55500bf4279Sespie for (;;)
55600bf4279Sespie {
55737c53322Sespie htab->collisions++;
55837c53322Sespie index += hash2;
55937c53322Sespie if (index >= size)
56037c53322Sespie index -= size;
56137c53322Sespie
56237c53322Sespie entry = htab->entries[index];
563*150b7e42Smiod if (entry == HTAB_EMPTY_ENTRY
564*150b7e42Smiod || (entry != HTAB_DELETED_ENTRY && (*htab->eq_f) (entry, element)))
56537c53322Sespie return entry;
56600bf4279Sespie }
56700bf4279Sespie }
56800bf4279Sespie
56937c53322Sespie /* Like htab_find_slot_with_hash, but compute the hash value from the
57037c53322Sespie element. */
57137c53322Sespie
57237c53322Sespie PTR
htab_find(htab_t htab,const PTR element)573*150b7e42Smiod htab_find (htab_t htab, const PTR element)
57437c53322Sespie {
57537c53322Sespie return htab_find_with_hash (htab, element, (*htab->hash_f) (element));
57637c53322Sespie }
57737c53322Sespie
57837c53322Sespie /* This function searches for a hash table slot containing an entry
57937c53322Sespie equal to the given element. To delete an entry, call this with
580*150b7e42Smiod insert=NO_INSERT, then call htab_clear_slot on the slot returned
581*150b7e42Smiod (possibly after doing some checks). To insert an entry, call this
582*150b7e42Smiod with insert=INSERT, then write the value you want into the returned
583*150b7e42Smiod slot. When inserting an entry, NULL may be returned if memory
584*150b7e42Smiod allocation fails. */
58537c53322Sespie
58637c53322Sespie PTR *
htab_find_slot_with_hash(htab_t htab,const PTR element,hashval_t hash,enum insert_option insert)587*150b7e42Smiod htab_find_slot_with_hash (htab_t htab, const PTR element,
588*150b7e42Smiod hashval_t hash, enum insert_option insert)
58937c53322Sespie {
59037c53322Sespie PTR *first_deleted_slot;
591*150b7e42Smiod hashval_t index, hash2;
59237c53322Sespie size_t size;
59337c53322Sespie PTR entry;
59437c53322Sespie
595*150b7e42Smiod size = htab_size (htab);
596*150b7e42Smiod if (insert == INSERT && size * 3 <= htab->n_elements * 4)
597*150b7e42Smiod {
598*150b7e42Smiod if (htab_expand (htab) == 0)
59937c53322Sespie return NULL;
600*150b7e42Smiod size = htab_size (htab);
601*150b7e42Smiod }
60237c53322Sespie
603*150b7e42Smiod index = htab_mod (hash, htab);
60437c53322Sespie
60537c53322Sespie htab->searches++;
60637c53322Sespie first_deleted_slot = NULL;
60737c53322Sespie
60837c53322Sespie entry = htab->entries[index];
609*150b7e42Smiod if (entry == HTAB_EMPTY_ENTRY)
61037c53322Sespie goto empty_entry;
611*150b7e42Smiod else if (entry == HTAB_DELETED_ENTRY)
61237c53322Sespie first_deleted_slot = &htab->entries[index];
61337c53322Sespie else if ((*htab->eq_f) (entry, element))
61437c53322Sespie return &htab->entries[index];
61537c53322Sespie
616*150b7e42Smiod hash2 = htab_mod_m2 (hash, htab);
61737c53322Sespie for (;;)
61837c53322Sespie {
61937c53322Sespie htab->collisions++;
62037c53322Sespie index += hash2;
62137c53322Sespie if (index >= size)
62237c53322Sespie index -= size;
62337c53322Sespie
62437c53322Sespie entry = htab->entries[index];
625*150b7e42Smiod if (entry == HTAB_EMPTY_ENTRY)
62637c53322Sespie goto empty_entry;
627*150b7e42Smiod else if (entry == HTAB_DELETED_ENTRY)
62837c53322Sespie {
62937c53322Sespie if (!first_deleted_slot)
63037c53322Sespie first_deleted_slot = &htab->entries[index];
63137c53322Sespie }
63237c53322Sespie else if ((*htab->eq_f) (entry, element))
63337c53322Sespie return &htab->entries[index];
63437c53322Sespie }
63537c53322Sespie
63637c53322Sespie empty_entry:
63737c53322Sespie if (insert == NO_INSERT)
63837c53322Sespie return NULL;
63937c53322Sespie
64037c53322Sespie if (first_deleted_slot)
64137c53322Sespie {
642*150b7e42Smiod htab->n_deleted--;
643*150b7e42Smiod *first_deleted_slot = HTAB_EMPTY_ENTRY;
64437c53322Sespie return first_deleted_slot;
64537c53322Sespie }
64637c53322Sespie
647*150b7e42Smiod htab->n_elements++;
64837c53322Sespie return &htab->entries[index];
64937c53322Sespie }
65037c53322Sespie
65137c53322Sespie /* Like htab_find_slot_with_hash, but compute the hash value from the
65237c53322Sespie element. */
65337c53322Sespie
65437c53322Sespie PTR *
htab_find_slot(htab_t htab,const PTR element,enum insert_option insert)655*150b7e42Smiod htab_find_slot (htab_t htab, const PTR element, enum insert_option insert)
65637c53322Sespie {
65737c53322Sespie return htab_find_slot_with_hash (htab, element, (*htab->hash_f) (element),
65837c53322Sespie insert);
65937c53322Sespie }
66037c53322Sespie
66137c53322Sespie /* This function deletes an element with the given value from hash
662*150b7e42Smiod table (the hash is computed from the element). If there is no matching
663*150b7e42Smiod element in the hash table, this function does nothing. */
664*150b7e42Smiod
665*150b7e42Smiod void
htab_remove_elt(htab_t htab,PTR element)666*150b7e42Smiod htab_remove_elt (htab_t htab, PTR element)
667*150b7e42Smiod {
668*150b7e42Smiod htab_remove_elt_with_hash (htab, element, (*htab->hash_f) (element));
669*150b7e42Smiod }
670*150b7e42Smiod
671*150b7e42Smiod
672*150b7e42Smiod /* This function deletes an element with the given value from hash
67337c53322Sespie table. If there is no matching element in the hash table, this
67437c53322Sespie function does nothing. */
67501e9b57fSespie
67601e9b57fSespie void
htab_remove_elt_with_hash(htab_t htab,PTR element,hashval_t hash)677*150b7e42Smiod htab_remove_elt_with_hash (htab_t htab, PTR element, hashval_t hash)
67801e9b57fSespie {
67937c53322Sespie PTR *slot;
68001e9b57fSespie
681*150b7e42Smiod slot = htab_find_slot_with_hash (htab, element, hash, NO_INSERT);
682*150b7e42Smiod if (*slot == HTAB_EMPTY_ENTRY)
68337c53322Sespie return;
68437c53322Sespie
68537c53322Sespie if (htab->del_f)
68637c53322Sespie (*htab->del_f) (*slot);
68737c53322Sespie
688*150b7e42Smiod *slot = HTAB_DELETED_ENTRY;
68937c53322Sespie htab->n_deleted++;
69001e9b57fSespie }
69101e9b57fSespie
69237c53322Sespie /* This function clears a specified slot in a hash table. It is
69337c53322Sespie useful when you've already done the lookup and don't want to do it
69437c53322Sespie again. */
69537c53322Sespie
69637c53322Sespie void
htab_clear_slot(htab_t htab,PTR * slot)697*150b7e42Smiod htab_clear_slot (htab_t htab, PTR *slot)
69837c53322Sespie {
699*150b7e42Smiod if (slot < htab->entries || slot >= htab->entries + htab_size (htab)
700*150b7e42Smiod || *slot == HTAB_EMPTY_ENTRY || *slot == HTAB_DELETED_ENTRY)
70137c53322Sespie abort ();
70237c53322Sespie
70337c53322Sespie if (htab->del_f)
70437c53322Sespie (*htab->del_f) (*slot);
70537c53322Sespie
706*150b7e42Smiod *slot = HTAB_DELETED_ENTRY;
70737c53322Sespie htab->n_deleted++;
70837c53322Sespie }
70937c53322Sespie
71037c53322Sespie /* This function scans over the entire hash table calling
71137c53322Sespie CALLBACK for each live entry. If CALLBACK returns false,
71237c53322Sespie the iteration stops. INFO is passed as CALLBACK's second
71337c53322Sespie argument. */
71437c53322Sespie
71537c53322Sespie void
htab_traverse_noresize(htab_t htab,htab_trav callback,PTR info)716*150b7e42Smiod htab_traverse_noresize (htab_t htab, htab_trav callback, PTR info)
71737c53322Sespie {
718707f648cSespie PTR *slot;
719707f648cSespie PTR *limit;
720707f648cSespie
721707f648cSespie slot = htab->entries;
722*150b7e42Smiod limit = slot + htab_size (htab);
72337c53322Sespie
72437c53322Sespie do
72537c53322Sespie {
72637c53322Sespie PTR x = *slot;
72737c53322Sespie
728*150b7e42Smiod if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
72937c53322Sespie if (!(*callback) (slot, info))
73037c53322Sespie break;
73137c53322Sespie }
73237c53322Sespie while (++slot < limit);
73337c53322Sespie }
73437c53322Sespie
735707f648cSespie /* Like htab_traverse_noresize, but does resize the table when it is
736707f648cSespie too empty to improve effectivity of subsequent calls. */
737707f648cSespie
738707f648cSespie void
htab_traverse(htab_t htab,htab_trav callback,PTR info)739*150b7e42Smiod htab_traverse (htab_t htab, htab_trav callback, PTR info)
740707f648cSespie {
741*150b7e42Smiod if (htab_elements (htab) * 8 < htab_size (htab))
742707f648cSespie htab_expand (htab);
743707f648cSespie
744707f648cSespie htab_traverse_noresize (htab, callback, info);
745707f648cSespie }
746707f648cSespie
74737c53322Sespie /* Return the fraction of fixed collisions during all work with given
74837c53322Sespie hash table. */
74901e9b57fSespie
75037c53322Sespie double
htab_collisions(htab_t htab)751*150b7e42Smiod htab_collisions (htab_t htab)
75201e9b57fSespie {
75337c53322Sespie if (htab->searches == 0)
75437c53322Sespie return 0.0;
75501e9b57fSespie
75637c53322Sespie return (double) htab->collisions / (double) htab->searches;
75701e9b57fSespie }
75801e9b57fSespie
75937c53322Sespie /* Hash P as a null-terminated string.
76001e9b57fSespie
76137c53322Sespie Copied from gcc/hashtable.c. Zack had the following to say with respect
76237c53322Sespie to applicability, though note that unlike hashtable.c, this hash table
76337c53322Sespie implementation re-hashes rather than chain buckets.
76437c53322Sespie
76537c53322Sespie http://gcc.gnu.org/ml/gcc-patches/2001-08/msg01021.html
76637c53322Sespie From: Zack Weinberg <zackw@panix.com>
76737c53322Sespie Date: Fri, 17 Aug 2001 02:15:56 -0400
76837c53322Sespie
76937c53322Sespie I got it by extracting all the identifiers from all the source code
77037c53322Sespie I had lying around in mid-1999, and testing many recurrences of
77137c53322Sespie the form "H_n = H_{n-1} * K + c_n * L + M" where K, L, M were either
77237c53322Sespie prime numbers or the appropriate identity. This was the best one.
77337c53322Sespie I don't remember exactly what constituted "best", except I was
77437c53322Sespie looking at bucket-length distributions mostly.
77537c53322Sespie
77637c53322Sespie So it should be very good at hashing identifiers, but might not be
77737c53322Sespie as good at arbitrary strings.
77837c53322Sespie
77937c53322Sespie I'll add that it thoroughly trounces the hash functions recommended
78037c53322Sespie for this use at http://burtleburtle.net/bob/hash/index.html, both
78137c53322Sespie on speed and bucket distribution. I haven't tried it against the
78237c53322Sespie function they just started using for Perl's hashes. */
78337c53322Sespie
78437c53322Sespie hashval_t
htab_hash_string(const PTR p)785*150b7e42Smiod htab_hash_string (const PTR p)
78601e9b57fSespie {
78737c53322Sespie const unsigned char *str = (const unsigned char *) p;
78837c53322Sespie hashval_t r = 0;
78937c53322Sespie unsigned char c;
79001e9b57fSespie
79137c53322Sespie while ((c = *str++) != 0)
79237c53322Sespie r = r * 67 + c - 113;
79337c53322Sespie
79437c53322Sespie return r;
79501e9b57fSespie }
796*150b7e42Smiod
797*150b7e42Smiod /* DERIVED FROM:
798*150b7e42Smiod --------------------------------------------------------------------
799*150b7e42Smiod lookup2.c, by Bob Jenkins, December 1996, Public Domain.
800*150b7e42Smiod hash(), hash2(), hash3, and mix() are externally useful functions.
801*150b7e42Smiod Routines to test the hash are included if SELF_TEST is defined.
802*150b7e42Smiod You can use this free for any purpose. It has no warranty.
803*150b7e42Smiod --------------------------------------------------------------------
804*150b7e42Smiod */
805*150b7e42Smiod
806*150b7e42Smiod /*
807*150b7e42Smiod --------------------------------------------------------------------
808*150b7e42Smiod mix -- mix 3 32-bit values reversibly.
809*150b7e42Smiod For every delta with one or two bit set, and the deltas of all three
810*150b7e42Smiod high bits or all three low bits, whether the original value of a,b,c
811*150b7e42Smiod is almost all zero or is uniformly distributed,
812*150b7e42Smiod * If mix() is run forward or backward, at least 32 bits in a,b,c
813*150b7e42Smiod have at least 1/4 probability of changing.
814*150b7e42Smiod * If mix() is run forward, every bit of c will change between 1/3 and
815*150b7e42Smiod 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
816*150b7e42Smiod mix() was built out of 36 single-cycle latency instructions in a
817*150b7e42Smiod structure that could supported 2x parallelism, like so:
818*150b7e42Smiod a -= b;
819*150b7e42Smiod a -= c; x = (c>>13);
820*150b7e42Smiod b -= c; a ^= x;
821*150b7e42Smiod b -= a; x = (a<<8);
822*150b7e42Smiod c -= a; b ^= x;
823*150b7e42Smiod c -= b; x = (b>>13);
824*150b7e42Smiod ...
825*150b7e42Smiod Unfortunately, superscalar Pentiums and Sparcs can't take advantage
826*150b7e42Smiod of that parallelism. They've also turned some of those single-cycle
827*150b7e42Smiod latency instructions into multi-cycle latency instructions. Still,
828*150b7e42Smiod this is the fastest good hash I could find. There were about 2^^68
829*150b7e42Smiod to choose from. I only looked at a billion or so.
830*150b7e42Smiod --------------------------------------------------------------------
831*150b7e42Smiod */
832*150b7e42Smiod /* same, but slower, works on systems that might have 8 byte hashval_t's */
833*150b7e42Smiod #define mix(a,b,c) \
834*150b7e42Smiod { \
835*150b7e42Smiod a -= b; a -= c; a ^= (c>>13); \
836*150b7e42Smiod b -= c; b -= a; b ^= (a<< 8); \
837*150b7e42Smiod c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
838*150b7e42Smiod a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
839*150b7e42Smiod b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
840*150b7e42Smiod c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
841*150b7e42Smiod a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
842*150b7e42Smiod b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
843*150b7e42Smiod c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
844*150b7e42Smiod }
845*150b7e42Smiod
846*150b7e42Smiod /*
847*150b7e42Smiod --------------------------------------------------------------------
848*150b7e42Smiod hash() -- hash a variable-length key into a 32-bit value
849*150b7e42Smiod k : the key (the unaligned variable-length array of bytes)
850*150b7e42Smiod len : the length of the key, counting by bytes
851*150b7e42Smiod level : can be any 4-byte value
852*150b7e42Smiod Returns a 32-bit value. Every bit of the key affects every bit of
853*150b7e42Smiod the return value. Every 1-bit and 2-bit delta achieves avalanche.
854*150b7e42Smiod About 36+6len instructions.
855*150b7e42Smiod
856*150b7e42Smiod The best hash table sizes are powers of 2. There is no need to do
857*150b7e42Smiod mod a prime (mod is sooo slow!). If you need less than 32 bits,
858*150b7e42Smiod use a bitmask. For example, if you need only 10 bits, do
859*150b7e42Smiod h = (h & hashmask(10));
860*150b7e42Smiod In which case, the hash table should have hashsize(10) elements.
861*150b7e42Smiod
862*150b7e42Smiod If you are hashing n strings (ub1 **)k, do it like this:
863*150b7e42Smiod for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
864*150b7e42Smiod
865*150b7e42Smiod By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
866*150b7e42Smiod code any way you wish, private, educational, or commercial. It's free.
867*150b7e42Smiod
868*150b7e42Smiod See http://burtleburtle.net/bob/hash/evahash.html
869*150b7e42Smiod Use for hash table lookup, or anything where one collision in 2^32 is
870*150b7e42Smiod acceptable. Do NOT use for cryptographic purposes.
871*150b7e42Smiod --------------------------------------------------------------------
872*150b7e42Smiod */
873*150b7e42Smiod
874*150b7e42Smiod hashval_t
iterative_hash(const PTR k_in,register size_t length,register hashval_t initval)875*150b7e42Smiod iterative_hash (const PTR k_in /* the key */,
876*150b7e42Smiod register size_t length /* the length of the key */,
877*150b7e42Smiod register hashval_t initval /* the previous hash, or
878*150b7e42Smiod an arbitrary value */)
879*150b7e42Smiod {
880*150b7e42Smiod register const unsigned char *k = (const unsigned char *)k_in;
881*150b7e42Smiod register hashval_t a,b,c,len;
882*150b7e42Smiod
883*150b7e42Smiod /* Set up the internal state */
884*150b7e42Smiod len = length;
885*150b7e42Smiod a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
886*150b7e42Smiod c = initval; /* the previous hash value */
887*150b7e42Smiod
888*150b7e42Smiod /*---------------------------------------- handle most of the key */
889*150b7e42Smiod #ifndef WORDS_BIGENDIAN
890*150b7e42Smiod /* On a little-endian machine, if the data is 4-byte aligned we can hash
891*150b7e42Smiod by word for better speed. This gives nondeterministic results on
892*150b7e42Smiod big-endian machines. */
893*150b7e42Smiod if (sizeof (hashval_t) == 4 && (((size_t)k)&3) == 0)
894*150b7e42Smiod while (len >= 12) /* aligned */
895*150b7e42Smiod {
896*150b7e42Smiod a += *(hashval_t *)(k+0);
897*150b7e42Smiod b += *(hashval_t *)(k+4);
898*150b7e42Smiod c += *(hashval_t *)(k+8);
899*150b7e42Smiod mix(a,b,c);
900*150b7e42Smiod k += 12; len -= 12;
901*150b7e42Smiod }
902*150b7e42Smiod else /* unaligned */
903*150b7e42Smiod #endif
904*150b7e42Smiod while (len >= 12)
905*150b7e42Smiod {
906*150b7e42Smiod a += (k[0] +((hashval_t)k[1]<<8) +((hashval_t)k[2]<<16) +((hashval_t)k[3]<<24));
907*150b7e42Smiod b += (k[4] +((hashval_t)k[5]<<8) +((hashval_t)k[6]<<16) +((hashval_t)k[7]<<24));
908*150b7e42Smiod c += (k[8] +((hashval_t)k[9]<<8) +((hashval_t)k[10]<<16)+((hashval_t)k[11]<<24));
909*150b7e42Smiod mix(a,b,c);
910*150b7e42Smiod k += 12; len -= 12;
911*150b7e42Smiod }
912*150b7e42Smiod
913*150b7e42Smiod /*------------------------------------- handle the last 11 bytes */
914*150b7e42Smiod c += length;
915*150b7e42Smiod switch(len) /* all the case statements fall through */
916*150b7e42Smiod {
917*150b7e42Smiod case 11: c+=((hashval_t)k[10]<<24);
918*150b7e42Smiod case 10: c+=((hashval_t)k[9]<<16);
919*150b7e42Smiod case 9 : c+=((hashval_t)k[8]<<8);
920*150b7e42Smiod /* the first byte of c is reserved for the length */
921*150b7e42Smiod case 8 : b+=((hashval_t)k[7]<<24);
922*150b7e42Smiod case 7 : b+=((hashval_t)k[6]<<16);
923*150b7e42Smiod case 6 : b+=((hashval_t)k[5]<<8);
924*150b7e42Smiod case 5 : b+=k[4];
925*150b7e42Smiod case 4 : a+=((hashval_t)k[3]<<24);
926*150b7e42Smiod case 3 : a+=((hashval_t)k[2]<<16);
927*150b7e42Smiod case 2 : a+=((hashval_t)k[1]<<8);
928*150b7e42Smiod case 1 : a+=k[0];
929*150b7e42Smiod /* case 0: nothing left to add */
930*150b7e42Smiod }
931*150b7e42Smiod mix(a,b,c);
932*150b7e42Smiod /*-------------------------------------------- report the result */
933*150b7e42Smiod return c;
934*150b7e42Smiod }
935