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