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