Home | History | Annotate | Line # | Download | only in hash
      1  1.13     joerg /*	$NetBSD: hash_func.c,v 1.13 2008/09/10 17:52:35 joerg Exp $	*/
      2   1.5       cgd 
      3   1.1       cgd /*-
      4   1.1       cgd  * Copyright (c) 1990, 1993
      5   1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Margo Seltzer.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18   1.9       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.10       jmc #if HAVE_NBTOOL_CONFIG_H
     36  1.10       jmc #include "nbtool_config.h"
     37  1.10       jmc #endif
     38  1.10       jmc 
     39   1.7  christos #include <sys/cdefs.h>
     40  1.13     joerg __RCSID("$NetBSD: hash_func.c,v 1.13 2008/09/10 17:52:35 joerg Exp $");
     41   1.1       cgd 
     42   1.1       cgd #include <sys/types.h>
     43   1.1       cgd 
     44   1.1       cgd #include <db.h>
     45   1.1       cgd #include "hash.h"
     46   1.1       cgd #include "page.h"
     47   1.1       cgd #include "extern.h"
     48   1.1       cgd 
     49   1.8  christos #if 0
     50  1.12     joerg static uint32_t hash1(const void *, size_t) __attribute__((__unused__));
     51  1.12     joerg static uint32_t hash2(const void *, size_t) __attribute__((__unused__));
     52  1.12     joerg static uint32_t hash3(const void *, size_t) __attribute__((__unused__));
     53   1.8  christos #endif
     54  1.12     joerg static uint32_t hash4(const void *, size_t) __attribute__((__unused__));
     55   1.1       cgd 
     56   1.1       cgd /* Global default hash function */
     57  1.12     joerg uint32_t (*__default_hash)(const void *, size_t) = hash4;
     58   1.8  christos #if 0
     59   1.1       cgd /*
     60   1.4       cgd  * HASH FUNCTIONS
     61   1.4       cgd  *
     62   1.1       cgd  * Assume that we've already split the bucket to which this key hashes,
     63   1.1       cgd  * calculate that bucket, and check that in fact we did already split it.
     64   1.1       cgd  *
     65   1.1       cgd  * This came from ejb's hsearch.
     66   1.1       cgd  */
     67   1.1       cgd 
     68   1.1       cgd #define PRIME1		37
     69   1.1       cgd #define PRIME2		1048583
     70   1.1       cgd 
     71  1.12     joerg static uint32_t
     72  1.11  christos hash1(const void *keyarg, size_t len)
     73   1.1       cgd {
     74  1.12     joerg 	const uint8_t *key;
     75  1.12     joerg 	uint32_t h;
     76   1.1       cgd 
     77   1.1       cgd 	/* Convert string to integer */
     78   1.4       cgd 	for (key = keyarg, h = 0; len--;)
     79   1.1       cgd 		h = h * PRIME1 ^ (*key++ - ' ');
     80   1.1       cgd 	h %= PRIME2;
     81   1.1       cgd 	return (h);
     82   1.1       cgd }
     83   1.1       cgd 
     84   1.1       cgd /*
     85   1.1       cgd  * Phong's linear congruential hash
     86   1.1       cgd  */
     87   1.1       cgd #define dcharhash(h, c)	((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
     88   1.1       cgd 
     89  1.12     joerg static uint32_t
     90  1.11  christos hash2(const void *keyarg, size_t len)
     91   1.1       cgd {
     92  1.12     joerg 	const uint8_t *e, *key;
     93  1.12     joerg 	uint32_t h;
     94  1.12     joerg 	uint8_t c;
     95   1.1       cgd 
     96   1.4       cgd 	key = keyarg;
     97   1.1       cgd 	e = key + len;
     98   1.1       cgd 	for (h = 0; key != e;) {
     99   1.1       cgd 		c = *key++;
    100   1.1       cgd 		if (!c && key > e)
    101   1.1       cgd 			break;
    102   1.1       cgd 		dcharhash(h, c);
    103   1.1       cgd 	}
    104   1.1       cgd 	return (h);
    105   1.1       cgd }
    106   1.1       cgd 
    107   1.1       cgd /*
    108   1.1       cgd  * This is INCREDIBLY ugly, but fast.  We break the string up into 8 byte
    109   1.1       cgd  * units.  On the first time through the loop we get the "leftover bytes"
    110   1.1       cgd  * (strlen % 8).  On every other iteration, we perform 8 HASHC's so we handle
    111   1.1       cgd  * all 8 bytes.  Essentially, this saves us 7 cmp & branch instructions.  If
    112   1.1       cgd  * this routine is heavily used enough, it's worth the ugly coding.
    113   1.1       cgd  *
    114   1.1       cgd  * OZ's original sdbm hash
    115   1.1       cgd  */
    116  1.12     joerg static uint32_t
    117  1.11  christos hash3(const void *keyarg, size_t len)
    118   1.1       cgd {
    119  1.12     joerg 	const uint8_t *key;
    120  1.11  christos 	size_t loop;
    121  1.12     joerg 	uint32_t h;
    122   1.1       cgd 
    123   1.4       cgd #define HASHC   h = *key++ + 65599 * h
    124   1.1       cgd 
    125   1.4       cgd 	h = 0;
    126   1.4       cgd 	key = keyarg;
    127   1.1       cgd 	if (len > 0) {
    128   1.1       cgd 		loop = (len + 8 - 1) >> 3;
    129   1.1       cgd 
    130   1.1       cgd 		switch (len & (8 - 1)) {
    131   1.1       cgd 		case 0:
    132   1.4       cgd 			do {
    133   1.1       cgd 				HASHC;
    134   1.4       cgd 				/* FALLTHROUGH */
    135   1.1       cgd 		case 7:
    136   1.1       cgd 				HASHC;
    137   1.4       cgd 				/* FALLTHROUGH */
    138   1.1       cgd 		case 6:
    139   1.1       cgd 				HASHC;
    140   1.4       cgd 				/* FALLTHROUGH */
    141   1.1       cgd 		case 5:
    142   1.1       cgd 				HASHC;
    143   1.4       cgd 				/* FALLTHROUGH */
    144   1.1       cgd 		case 4:
    145   1.1       cgd 				HASHC;
    146   1.4       cgd 				/* FALLTHROUGH */
    147   1.1       cgd 		case 3:
    148   1.1       cgd 				HASHC;
    149   1.4       cgd 				/* FALLTHROUGH */
    150   1.1       cgd 		case 2:
    151   1.1       cgd 				HASHC;
    152   1.4       cgd 				/* FALLTHROUGH */
    153   1.1       cgd 		case 1:
    154   1.1       cgd 				HASHC;
    155   1.1       cgd 			} while (--loop);
    156   1.1       cgd 		}
    157   1.1       cgd 	}
    158   1.4       cgd 	return (h);
    159   1.1       cgd }
    160   1.8  christos #endif
    161   1.1       cgd 
    162   1.1       cgd /* Hash function from Chris Torek. */
    163  1.12     joerg static uint32_t
    164  1.11  christos hash4(const void *keyarg, size_t len)
    165   1.1       cgd {
    166  1.12     joerg 	const uint8_t *key;
    167  1.11  christos 	size_t loop;
    168  1.12     joerg 	uint32_t h;
    169   1.1       cgd 
    170   1.1       cgd #define HASH4a   h = (h << 5) - h + *key++;
    171   1.1       cgd #define HASH4b   h = (h << 5) + h + *key++;
    172   1.1       cgd #define HASH4 HASH4b
    173   1.1       cgd 
    174   1.1       cgd 	h = 0;
    175   1.4       cgd 	key = keyarg;
    176   1.1       cgd 	if (len > 0) {
    177   1.1       cgd 		loop = (len + 8 - 1) >> 3;
    178   1.1       cgd 
    179   1.1       cgd 		switch (len & (8 - 1)) {
    180   1.1       cgd 		case 0:
    181   1.4       cgd 			do {
    182   1.1       cgd 				HASH4;
    183   1.4       cgd 				/* FALLTHROUGH */
    184   1.1       cgd 		case 7:
    185   1.1       cgd 				HASH4;
    186   1.4       cgd 				/* FALLTHROUGH */
    187   1.1       cgd 		case 6:
    188   1.1       cgd 				HASH4;
    189   1.4       cgd 				/* FALLTHROUGH */
    190   1.1       cgd 		case 5:
    191   1.1       cgd 				HASH4;
    192   1.4       cgd 				/* FALLTHROUGH */
    193   1.1       cgd 		case 4:
    194   1.1       cgd 				HASH4;
    195   1.4       cgd 				/* FALLTHROUGH */
    196   1.1       cgd 		case 3:
    197   1.1       cgd 				HASH4;
    198   1.4       cgd 				/* FALLTHROUGH */
    199   1.1       cgd 		case 2:
    200   1.1       cgd 				HASH4;
    201   1.4       cgd 				/* FALLTHROUGH */
    202   1.1       cgd 		case 1:
    203   1.1       cgd 				HASH4;
    204   1.1       cgd 			} while (--loop);
    205   1.1       cgd 		}
    206   1.1       cgd 	}
    207   1.1       cgd 	return (h);
    208   1.1       cgd }
    209