xref: /netbsd-src/common/lib/libc/stdlib/mi_vector_hash.c (revision aceb213538ec08a74028e213127af18aa17bf1cf)
1*aceb2135Sjoerg /*	$NetBSD: mi_vector_hash.c,v 1.1 2013/12/11 01:24:08 joerg Exp $	*/
2*aceb2135Sjoerg /*-
3*aceb2135Sjoerg  * Copyright (c) 2009 The NetBSD Foundation, Inc.
4*aceb2135Sjoerg  * All rights reserved.
5*aceb2135Sjoerg  *
6*aceb2135Sjoerg  * This code is derived from software contributed to The NetBSD Foundation
7*aceb2135Sjoerg  * by Joerg Sonnenberger.
8*aceb2135Sjoerg  *
9*aceb2135Sjoerg  * Redistribution and use in source and binary forms, with or without
10*aceb2135Sjoerg  * modification, are permitted provided that the following conditions
11*aceb2135Sjoerg  * are met:
12*aceb2135Sjoerg  *
13*aceb2135Sjoerg  * 1. Redistributions of source code must retain the above copyright
14*aceb2135Sjoerg  *    notice, this list of conditions and the following disclaimer.
15*aceb2135Sjoerg  * 2. Redistributions in binary form must reproduce the above copyright
16*aceb2135Sjoerg  *    notice, this list of conditions and the following disclaimer in
17*aceb2135Sjoerg  *    the documentation and/or other materials provided with the
18*aceb2135Sjoerg  *    distribution.
19*aceb2135Sjoerg  *
20*aceb2135Sjoerg  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*aceb2135Sjoerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*aceb2135Sjoerg  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*aceb2135Sjoerg  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24*aceb2135Sjoerg  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*aceb2135Sjoerg  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*aceb2135Sjoerg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27*aceb2135Sjoerg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*aceb2135Sjoerg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*aceb2135Sjoerg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30*aceb2135Sjoerg  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*aceb2135Sjoerg  * SUCH DAMAGE.
32*aceb2135Sjoerg  */
33*aceb2135Sjoerg 
34*aceb2135Sjoerg /*
35*aceb2135Sjoerg  * See http://burtleburtle.net/bob/hash/doobs.html for the full description
36*aceb2135Sjoerg  * and the original version of the code.  This version differs by exposing
37*aceb2135Sjoerg  * the full internal state and avoiding byte operations in the inner loop
38*aceb2135Sjoerg  * if the key is aligned correctly.
39*aceb2135Sjoerg  */
40*aceb2135Sjoerg 
41*aceb2135Sjoerg #if HAVE_NBTOOL_CONFIG_H
42*aceb2135Sjoerg #include "nbtool_config.h"
43*aceb2135Sjoerg #endif
44*aceb2135Sjoerg 
45*aceb2135Sjoerg #include <sys/cdefs.h>
46*aceb2135Sjoerg __RCSID("$NetBSD: mi_vector_hash.c,v 1.1 2013/12/11 01:24:08 joerg Exp $");
47*aceb2135Sjoerg 
48*aceb2135Sjoerg #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
49*aceb2135Sjoerg #include <sys/endian.h>
50*aceb2135Sjoerg #endif
51*aceb2135Sjoerg 
52*aceb2135Sjoerg #if defined(_KERNEL) || defined(_STANDALONE)
53*aceb2135Sjoerg #include <sys/types.h>
54*aceb2135Sjoerg #include <sys/systm.h>
55*aceb2135Sjoerg #include <lib/libkern/libkern.h>
56*aceb2135Sjoerg #else
57*aceb2135Sjoerg #include "namespace.h"
58*aceb2135Sjoerg 
59*aceb2135Sjoerg #include <stdint.h>
60*aceb2135Sjoerg #include <stdlib.h>
61*aceb2135Sjoerg #endif
62*aceb2135Sjoerg 
63*aceb2135Sjoerg #define mix(a, b, c) do {		\
64*aceb2135Sjoerg 	a -= b; a -= c; a ^= (c >> 13);	\
65*aceb2135Sjoerg 	b -= c; b -= a; b ^= (a << 8);	\
66*aceb2135Sjoerg 	c -= a; c -= b; c ^= (b >> 13);	\
67*aceb2135Sjoerg 	a -= b; a -= c; a ^= (c >> 12);	\
68*aceb2135Sjoerg 	b -= c; b -= a; b ^= (a << 16);	\
69*aceb2135Sjoerg 	c -= a; c -= b; c ^= (b >> 5);	\
70*aceb2135Sjoerg 	a -= b; a -= c; a ^= (c >> 3);	\
71*aceb2135Sjoerg 	b -= c; b -= a; b ^= (a << 10);	\
72*aceb2135Sjoerg 	c -= a; c -= b; c ^= (b >> 15);	\
73*aceb2135Sjoerg } while (/* CONSTCOND */0)
74*aceb2135Sjoerg 
75*aceb2135Sjoerg #define FIXED_SEED	0x9e3779b9	/* Golden ratio, arbitrary constant */
76*aceb2135Sjoerg 
77*aceb2135Sjoerg #if !defined(_KERNEL) && !defined(_STANDALONE)
78*aceb2135Sjoerg #ifdef __weak_alias
__weak_alias(mi_vector_hash,_mi_vector_hash)79*aceb2135Sjoerg __weak_alias(mi_vector_hash, _mi_vector_hash)
80*aceb2135Sjoerg #endif
81*aceb2135Sjoerg #endif
82*aceb2135Sjoerg 
83*aceb2135Sjoerg void
84*aceb2135Sjoerg mi_vector_hash(const void * __restrict key, size_t len, uint32_t seed,
85*aceb2135Sjoerg     uint32_t hashes[3])
86*aceb2135Sjoerg {
87*aceb2135Sjoerg 	static const uint32_t mask[4] = {
88*aceb2135Sjoerg 		0x000000ff, 0x0000ffff, 0x00ffffff, 0xffffffff
89*aceb2135Sjoerg 	};
90*aceb2135Sjoerg 	uint32_t orig_len, a, b, c;
91*aceb2135Sjoerg 	const uint8_t *k;
92*aceb2135Sjoerg 
93*aceb2135Sjoerg 	orig_len = (uint32_t)len;
94*aceb2135Sjoerg 
95*aceb2135Sjoerg 	a = b = FIXED_SEED;
96*aceb2135Sjoerg 	c = seed;
97*aceb2135Sjoerg 
98*aceb2135Sjoerg 	if ((uintptr_t)key & 3) {
99*aceb2135Sjoerg 		k = key;
100*aceb2135Sjoerg 		while (len >= 12) {
101*aceb2135Sjoerg 			a += le32dec(k);
102*aceb2135Sjoerg 			b += le32dec(k + 4);
103*aceb2135Sjoerg 			c += le32dec(k + 8);
104*aceb2135Sjoerg 			mix(a, b, c);
105*aceb2135Sjoerg 			k += 12;
106*aceb2135Sjoerg 			len -= 12;
107*aceb2135Sjoerg 		}
108*aceb2135Sjoerg 		c += orig_len;
109*aceb2135Sjoerg 
110*aceb2135Sjoerg 		if (len > 8) {
111*aceb2135Sjoerg 			switch (len) {
112*aceb2135Sjoerg 			case 11:
113*aceb2135Sjoerg 				c += (uint32_t)k[10] << 24;
114*aceb2135Sjoerg 				/* FALLTHROUGH */
115*aceb2135Sjoerg 			case 10:
116*aceb2135Sjoerg 				c += (uint32_t)k[9] << 16;
117*aceb2135Sjoerg 				/* FALLTHROUGH */
118*aceb2135Sjoerg 			case 9:
119*aceb2135Sjoerg 				c += (uint32_t)k[8] << 8;
120*aceb2135Sjoerg 				/* FALLTHROUGH */
121*aceb2135Sjoerg 			}
122*aceb2135Sjoerg 			b += le32dec(k + 4);
123*aceb2135Sjoerg 			a += le32dec(k);
124*aceb2135Sjoerg 		} else if (len > 4) {
125*aceb2135Sjoerg 			switch (len) {
126*aceb2135Sjoerg 			case 8:
127*aceb2135Sjoerg 				b += (uint32_t)k[7] << 24;
128*aceb2135Sjoerg 				/* FALLTHROUGH */
129*aceb2135Sjoerg 			case 7:
130*aceb2135Sjoerg 				b += (uint32_t)k[6] << 16;
131*aceb2135Sjoerg 				/* FALLTHROUGH */
132*aceb2135Sjoerg 			case 6:
133*aceb2135Sjoerg 				b += (uint32_t)k[5] << 8;
134*aceb2135Sjoerg 				/* FALLTHROUGH */
135*aceb2135Sjoerg 			case 5:
136*aceb2135Sjoerg 				b += k[4];
137*aceb2135Sjoerg 				/* FALLTHROUGH */
138*aceb2135Sjoerg 			}
139*aceb2135Sjoerg 			a += le32dec(k);
140*aceb2135Sjoerg 		} else if (len) {
141*aceb2135Sjoerg 			switch (len) {
142*aceb2135Sjoerg 			case 4:
143*aceb2135Sjoerg 				a += (uint32_t)k[3] << 24;
144*aceb2135Sjoerg 				/* FALLTHROUGH */
145*aceb2135Sjoerg 			case 3:
146*aceb2135Sjoerg 				a += (uint32_t)k[2] << 16;
147*aceb2135Sjoerg 				/* FALLTHROUGH */
148*aceb2135Sjoerg 			case 2:
149*aceb2135Sjoerg 				a += (uint32_t)k[1] << 8;
150*aceb2135Sjoerg 				/* FALLTHROUGH */
151*aceb2135Sjoerg 			case 1:
152*aceb2135Sjoerg 				a += k[0];
153*aceb2135Sjoerg 				/* FALLTHROUGH */
154*aceb2135Sjoerg 			}
155*aceb2135Sjoerg 		}
156*aceb2135Sjoerg 	} else {
157*aceb2135Sjoerg 		const uint32_t *key32 = key;
158*aceb2135Sjoerg 		while (len >= 12) {
159*aceb2135Sjoerg 			a += le32toh(key32[0]);
160*aceb2135Sjoerg 			b += le32toh(key32[1]);
161*aceb2135Sjoerg 			c += le32toh(key32[2]);
162*aceb2135Sjoerg 			mix(a, b, c);
163*aceb2135Sjoerg 			key32 += 3;
164*aceb2135Sjoerg 			len -= 12;
165*aceb2135Sjoerg 		}
166*aceb2135Sjoerg 		c += orig_len;
167*aceb2135Sjoerg 
168*aceb2135Sjoerg 		if (len > 8) {
169*aceb2135Sjoerg 			c += (le32toh(key32[2]) & mask[len - 9]) << 8;
170*aceb2135Sjoerg 			b += le32toh(key32[1]);
171*aceb2135Sjoerg 			a += le32toh(key32[0]);
172*aceb2135Sjoerg 		} else if (len > 4) {
173*aceb2135Sjoerg 			b += le32toh(key32[1]) & mask[len - 5];
174*aceb2135Sjoerg 			a += le32toh(key32[0]);
175*aceb2135Sjoerg 		} else if (len)
176*aceb2135Sjoerg 			a += le32toh(key32[0]) & mask[len - 1];
177*aceb2135Sjoerg 	}
178*aceb2135Sjoerg 	mix(a, b, c);
179*aceb2135Sjoerg 	hashes[0] = a;
180*aceb2135Sjoerg 	hashes[1] = b;
181*aceb2135Sjoerg 	hashes[2] = c;
182*aceb2135Sjoerg }
183