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