xref: /xnu-12377.81.4/bsd/net/flowhash.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2011-2022 Apple Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions  *
15*043036a2SApple OSS Distributions  * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions  *
18*043036a2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions  * limitations under the License.
25*043036a2SApple OSS Distributions  *
26*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions  */
28*043036a2SApple OSS Distributions 
29*043036a2SApple OSS Distributions /*
30*043036a2SApple OSS Distributions  * http://code.google.com/p/smhasher/
31*043036a2SApple OSS Distributions  *
32*043036a2SApple OSS Distributions  * Copyright (c) 2009-2011 Austin Appleby.
33*043036a2SApple OSS Distributions  *
34*043036a2SApple OSS Distributions  * MurmurHash3 was written by Austin Appleby, and is placed in the public
35*043036a2SApple OSS Distributions  * domain. The author hereby disclaims copyright to this source code.
36*043036a2SApple OSS Distributions  */
37*043036a2SApple OSS Distributions 
38*043036a2SApple OSS Distributions /*
39*043036a2SApple OSS Distributions  * http://burtleburtle.net/bob/hash/
40*043036a2SApple OSS Distributions  *
41*043036a2SApple OSS Distributions  * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
42*043036a2SApple OSS Distributions  *
43*043036a2SApple OSS Distributions  * You can use this free for any purpose.  It's in the public domain.
44*043036a2SApple OSS Distributions  * It has no warranty.
45*043036a2SApple OSS Distributions  */
46*043036a2SApple OSS Distributions 
47*043036a2SApple OSS Distributions #include <stdbool.h>
48*043036a2SApple OSS Distributions #include <sys/types.h>
49*043036a2SApple OSS Distributions #include <machine/endian.h>
50*043036a2SApple OSS Distributions #include <machine/trap.h>
51*043036a2SApple OSS Distributions #include <net/flowhash.h>
52*043036a2SApple OSS Distributions #include <os/base.h>
53*043036a2SApple OSS Distributions 
54*043036a2SApple OSS Distributions static inline u_int32_t getblock32(const u_int32_t *__bidi_indexable, int);
55*043036a2SApple OSS Distributions static inline u_int64_t getblock64(const u_int64_t *__bidi_indexable, int);
56*043036a2SApple OSS Distributions static inline u_int32_t mh3_fmix32(u_int32_t);
57*043036a2SApple OSS Distributions static inline u_int64_t mh3_fmix64(u_int64_t);
58*043036a2SApple OSS Distributions 
59*043036a2SApple OSS Distributions #define ALIGNED16(v)    ((((uintptr_t)(v)) & 1) == 0)
60*043036a2SApple OSS Distributions #define ALIGNED32(v)    ((((uintptr_t)(v)) & 3) == 0)
61*043036a2SApple OSS Distributions #define ALIGNED64(v)    ((((uintptr_t)(v)) & 7) == 0)
62*043036a2SApple OSS Distributions 
63*043036a2SApple OSS Distributions #define ROTL32(x, r)    (((x) << (r)) | ((x) >> (32 - (r))))
64*043036a2SApple OSS Distributions #define ROTL64(x, r)    (((x) << (r)) | ((x) >> (64 - (r))))
65*043036a2SApple OSS Distributions 
66*043036a2SApple OSS Distributions /*
67*043036a2SApple OSS Distributions  * The following hash algorithms are selected based on performance:
68*043036a2SApple OSS Distributions  *
69*043036a2SApple OSS Distributions  * 64-bit:	MurmurHash3_x64_128
70*043036a2SApple OSS Distributions  * 32-bit:	JHash
71*043036a2SApple OSS Distributions  */
72*043036a2SApple OSS Distributions #if   defined(__LP64__)
73*043036a2SApple OSS Distributions net_flowhash_fn_t *net_flowhash = net_flowhash_mh3_x64_128;
74*043036a2SApple OSS Distributions #else /* !__LP64__ */
75*043036a2SApple OSS Distributions net_flowhash_fn_t *net_flowhash = net_flowhash_jhash;
76*043036a2SApple OSS Distributions #endif /* !__LP64__ */
77*043036a2SApple OSS Distributions 
78*043036a2SApple OSS Distributions #if defined(__i386__) || defined(__x86_64__) || defined(__arm64__)
79*043036a2SApple OSS Distributions static inline u_int32_t
getblock32(const u_int32_t * __bidi_indexable p,int i)80*043036a2SApple OSS Distributions getblock32(const u_int32_t *__bidi_indexable p, int i)
81*043036a2SApple OSS Distributions {
82*043036a2SApple OSS Distributions 	return p[i];
83*043036a2SApple OSS Distributions }
84*043036a2SApple OSS Distributions 
85*043036a2SApple OSS Distributions static inline u_int64_t
getblock64(const u_int64_t * __bidi_indexable p,int i)86*043036a2SApple OSS Distributions getblock64(const u_int64_t *__bidi_indexable p, int i)
87*043036a2SApple OSS Distributions {
88*043036a2SApple OSS Distributions 	return p[i];
89*043036a2SApple OSS Distributions }
90*043036a2SApple OSS Distributions #else /* !__i386__ && !__x86_64__ && !__arm64__*/
91*043036a2SApple OSS Distributions static inline u_int32_t
getblock32(const u_int32_t * __bidi_indexable p,int i)92*043036a2SApple OSS Distributions getblock32(const u_int32_t *__bidi_indexable p, int i)
93*043036a2SApple OSS Distributions {
94*043036a2SApple OSS Distributions 	const u_int8_t *bytes = (u_int8_t *)(void *)(uintptr_t)(p + i);
95*043036a2SApple OSS Distributions 	u_int32_t value;
96*043036a2SApple OSS Distributions 
97*043036a2SApple OSS Distributions 	if (ALIGNED32(p)) {
98*043036a2SApple OSS Distributions 		value = p[i];
99*043036a2SApple OSS Distributions 	} else {
100*043036a2SApple OSS Distributions #if BYTE_ORDER == BIG_ENDIAN
101*043036a2SApple OSS Distributions 		value =
102*043036a2SApple OSS Distributions 		    (((u_int32_t)bytes[0]) << 24) |
103*043036a2SApple OSS Distributions 		    (((u_int32_t)bytes[1]) << 16) |
104*043036a2SApple OSS Distributions 		    (((u_int32_t)bytes[2]) << 8) |
105*043036a2SApple OSS Distributions 		    ((u_int32_t)bytes[3]);
106*043036a2SApple OSS Distributions #else /* LITTLE_ENDIAN */
107*043036a2SApple OSS Distributions 		value =
108*043036a2SApple OSS Distributions 		    (((u_int32_t)bytes[3]) << 24) |
109*043036a2SApple OSS Distributions 		    (((u_int32_t)bytes[2]) << 16) |
110*043036a2SApple OSS Distributions 		    (((u_int32_t)bytes[1]) << 8) |
111*043036a2SApple OSS Distributions 		    ((u_int32_t)bytes[0]);
112*043036a2SApple OSS Distributions #endif /* LITTLE_ENDIAN */
113*043036a2SApple OSS Distributions 	}
114*043036a2SApple OSS Distributions 	return value;
115*043036a2SApple OSS Distributions }
116*043036a2SApple OSS Distributions 
117*043036a2SApple OSS Distributions static inline u_int64_t
getblock64(const u_int64_t * __bidi_indexable p,int i)118*043036a2SApple OSS Distributions getblock64(const u_int64_t *__bidi_indexable p, int i)
119*043036a2SApple OSS Distributions {
120*043036a2SApple OSS Distributions 	const u_int8_t *bytes = (const u_int8_t *)(void *)(uintptr_t)(p + i);
121*043036a2SApple OSS Distributions 	u_int64_t value;
122*043036a2SApple OSS Distributions 
123*043036a2SApple OSS Distributions 	if (ALIGNED64(p)) {
124*043036a2SApple OSS Distributions 		value = p[i];
125*043036a2SApple OSS Distributions 	} else {
126*043036a2SApple OSS Distributions #if BYTE_ORDER == BIG_ENDIAN
127*043036a2SApple OSS Distributions 		value =
128*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[0]) << 56) |
129*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[1]) << 48) |
130*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[2]) << 40) |
131*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[3]) << 32) |
132*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[4]) << 24) |
133*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[5]) << 16) |
134*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[6]) << 8) |
135*043036a2SApple OSS Distributions 		    ((u_int64_t)bytes[7]);
136*043036a2SApple OSS Distributions #else /* LITTLE_ENDIAN */
137*043036a2SApple OSS Distributions 		value =
138*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[7]) << 56) |
139*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[6]) << 48) |
140*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[5]) << 40) |
141*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[4]) << 32) |
142*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[3]) << 24) |
143*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[2]) << 16) |
144*043036a2SApple OSS Distributions 		    (((u_int64_t)bytes[1]) << 8) |
145*043036a2SApple OSS Distributions 		    ((u_int64_t)bytes[0]);
146*043036a2SApple OSS Distributions #endif /* LITTLE_ENDIAN */
147*043036a2SApple OSS Distributions 	}
148*043036a2SApple OSS Distributions 	return value;
149*043036a2SApple OSS Distributions }
150*043036a2SApple OSS Distributions #endif /* !__i386__ && !__x86_64 && !__arm64__ */
151*043036a2SApple OSS Distributions 
152*043036a2SApple OSS Distributions static inline u_int32_t
mh3_fmix32(u_int32_t h)153*043036a2SApple OSS Distributions mh3_fmix32(u_int32_t h)
154*043036a2SApple OSS Distributions {
155*043036a2SApple OSS Distributions 	h ^= h >> 16;
156*043036a2SApple OSS Distributions 	h *= 0x85ebca6b;
157*043036a2SApple OSS Distributions 	h ^= h >> 13;
158*043036a2SApple OSS Distributions 	h *= 0xc2b2ae35;
159*043036a2SApple OSS Distributions 	h ^= h >> 16;
160*043036a2SApple OSS Distributions 
161*043036a2SApple OSS Distributions 	return h;
162*043036a2SApple OSS Distributions }
163*043036a2SApple OSS Distributions 
164*043036a2SApple OSS Distributions static inline u_int64_t
mh3_fmix64(u_int64_t k)165*043036a2SApple OSS Distributions mh3_fmix64(u_int64_t k)
166*043036a2SApple OSS Distributions {
167*043036a2SApple OSS Distributions 	k ^= k >> 33;
168*043036a2SApple OSS Distributions 	k *= 0xff51afd7ed558ccdLLU;
169*043036a2SApple OSS Distributions 	k ^= k >> 33;
170*043036a2SApple OSS Distributions 	k *= 0xc4ceb9fe1a85ec53LLU;
171*043036a2SApple OSS Distributions 	k ^= k >> 33;
172*043036a2SApple OSS Distributions 
173*043036a2SApple OSS Distributions 	return k;
174*043036a2SApple OSS Distributions }
175*043036a2SApple OSS Distributions 
176*043036a2SApple OSS Distributions /*
177*043036a2SApple OSS Distributions  * MurmurHash3_x86_32
178*043036a2SApple OSS Distributions  */
179*043036a2SApple OSS Distributions #define MH3_X86_32_C1   0xcc9e2d51
180*043036a2SApple OSS Distributions #define MH3_X86_32_C2   0x1b873593
181*043036a2SApple OSS Distributions 
182*043036a2SApple OSS Distributions u_int32_t
net_flowhash_mh3_x86_32(const void * __sized_by (len)key,u_int32_t len,const u_int32_t seed)183*043036a2SApple OSS Distributions net_flowhash_mh3_x86_32(const void *__sized_by(len) key, u_int32_t len, const u_int32_t seed)
184*043036a2SApple OSS Distributions {
185*043036a2SApple OSS Distributions 	const u_int8_t *data = (const u_int8_t *)key;
186*043036a2SApple OSS Distributions 	const u_int32_t nblocks = len / 4;
187*043036a2SApple OSS Distributions 	const u_int32_t *blocks;
188*043036a2SApple OSS Distributions 	const u_int8_t *tail;
189*043036a2SApple OSS Distributions 	u_int32_t h1 = seed, k1;
190*043036a2SApple OSS Distributions 	int i;
191*043036a2SApple OSS Distributions 
192*043036a2SApple OSS Distributions 	/* body */
193*043036a2SApple OSS Distributions 	blocks = (const u_int32_t *)(const void *)(data + nblocks * 4);
194*043036a2SApple OSS Distributions 
195*043036a2SApple OSS Distributions 	for (i = -nblocks; i; i++) {
196*043036a2SApple OSS Distributions 		k1 = getblock32(blocks, i);
197*043036a2SApple OSS Distributions 
198*043036a2SApple OSS Distributions 		k1 *= MH3_X86_32_C1;
199*043036a2SApple OSS Distributions 		k1 = ROTL32(k1, 15);
200*043036a2SApple OSS Distributions 		k1 *= MH3_X86_32_C2;
201*043036a2SApple OSS Distributions 
202*043036a2SApple OSS Distributions 		h1 ^= k1;
203*043036a2SApple OSS Distributions 		h1 = ROTL32(h1, 13);
204*043036a2SApple OSS Distributions 		h1 = h1 * 5 + 0xe6546b64;
205*043036a2SApple OSS Distributions 	}
206*043036a2SApple OSS Distributions 
207*043036a2SApple OSS Distributions 	/* tail */
208*043036a2SApple OSS Distributions 	tail = (const u_int8_t *)(const void *)(data + nblocks * 4);
209*043036a2SApple OSS Distributions 	k1 = 0;
210*043036a2SApple OSS Distributions 
211*043036a2SApple OSS Distributions 	switch (len & 3) {
212*043036a2SApple OSS Distributions 	case 3:
213*043036a2SApple OSS Distributions 		k1 ^= tail[2] << 16;
214*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
215*043036a2SApple OSS Distributions 	case 2:
216*043036a2SApple OSS Distributions 		k1 ^= tail[1] << 8;
217*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
218*043036a2SApple OSS Distributions 	case 1:
219*043036a2SApple OSS Distributions 		k1 ^= tail[0];
220*043036a2SApple OSS Distributions 		k1 *= MH3_X86_32_C1;
221*043036a2SApple OSS Distributions 		k1 = ROTL32(k1, 15);
222*043036a2SApple OSS Distributions 		k1 *= MH3_X86_32_C2;
223*043036a2SApple OSS Distributions 		h1 ^= k1;
224*043036a2SApple OSS Distributions 	}
225*043036a2SApple OSS Distributions 	;
226*043036a2SApple OSS Distributions 
227*043036a2SApple OSS Distributions 	/* finalization */
228*043036a2SApple OSS Distributions 	h1 ^= len;
229*043036a2SApple OSS Distributions 
230*043036a2SApple OSS Distributions 	h1 = mh3_fmix32(h1);
231*043036a2SApple OSS Distributions 
232*043036a2SApple OSS Distributions 	return h1;
233*043036a2SApple OSS Distributions }
234*043036a2SApple OSS Distributions 
235*043036a2SApple OSS Distributions /*
236*043036a2SApple OSS Distributions  * MurmurHash3_x64_128
237*043036a2SApple OSS Distributions  */
238*043036a2SApple OSS Distributions #define MH3_X64_128_C1  0x87c37b91114253d5LLU
239*043036a2SApple OSS Distributions #define MH3_X64_128_C2  0x4cf5ad432745937fLLU
240*043036a2SApple OSS Distributions 
241*043036a2SApple OSS Distributions u_int32_t
net_flowhash_mh3_x64_128(const void * __sized_by (len)key,u_int32_t len,const u_int32_t seed)242*043036a2SApple OSS Distributions net_flowhash_mh3_x64_128(const void *__sized_by(len) key, u_int32_t len, const u_int32_t seed)
243*043036a2SApple OSS Distributions {
244*043036a2SApple OSS Distributions 	const u_int8_t *data = (const u_int8_t *)key;
245*043036a2SApple OSS Distributions 	const u_int32_t nblocks = len / 16;
246*043036a2SApple OSS Distributions 	const u_int64_t *blocks;
247*043036a2SApple OSS Distributions 	const u_int8_t *tail;
248*043036a2SApple OSS Distributions 	u_int64_t h1 = seed, k1;
249*043036a2SApple OSS Distributions 	u_int64_t h2 = seed, k2;
250*043036a2SApple OSS Distributions 	u_int32_t i;
251*043036a2SApple OSS Distributions 
252*043036a2SApple OSS Distributions 	/* body */
253*043036a2SApple OSS Distributions 	blocks = (const u_int64_t *)(const void *)data;
254*043036a2SApple OSS Distributions 
255*043036a2SApple OSS Distributions 	for (i = 0; i < nblocks; i++) {
256*043036a2SApple OSS Distributions 		k1 = getblock64(blocks, i * 2 + 0);
257*043036a2SApple OSS Distributions 		k2 = getblock64(blocks, i * 2 + 1);
258*043036a2SApple OSS Distributions 
259*043036a2SApple OSS Distributions 		k1 *= MH3_X64_128_C1;
260*043036a2SApple OSS Distributions #if defined(__x86_64__)
261*043036a2SApple OSS Distributions 		__asm__ ( "rol   $31, %[k1]\n\t" :[k1] "+r" (k1) : :);
262*043036a2SApple OSS Distributions #elif defined(__arm64__)
263*043036a2SApple OSS Distributions 		__asm__ ( "ror   %[k1], %[k1], #(64-31)\n\t" :[k1] "+r" (k1) : :);
264*043036a2SApple OSS Distributions #else /* !__x86_64__ && !__arm64__ */
265*043036a2SApple OSS Distributions 		k1 = ROTL64(k1, 31);
266*043036a2SApple OSS Distributions #endif /* !__x86_64__ && !__arm64__ */
267*043036a2SApple OSS Distributions 		k1 *= MH3_X64_128_C2;
268*043036a2SApple OSS Distributions 		h1 ^= k1;
269*043036a2SApple OSS Distributions 
270*043036a2SApple OSS Distributions #if defined(__x86_64__)
271*043036a2SApple OSS Distributions 		__asm__ ( "rol   $27, %[h1]\n\t" :[h1] "+r" (h1) : :);
272*043036a2SApple OSS Distributions #elif defined(__arm64__)
273*043036a2SApple OSS Distributions 		__asm__ ( "ror   %[h1], %[h1], #(64-27)\n\t" :[h1] "+r" (h1) : :);
274*043036a2SApple OSS Distributions #else /* !__x86_64__ && !__arm64__ */
275*043036a2SApple OSS Distributions 		h1 = ROTL64(h1, 27);
276*043036a2SApple OSS Distributions #endif /* !__x86_64__ && !__arm64__ */
277*043036a2SApple OSS Distributions 		h1 += h2;
278*043036a2SApple OSS Distributions 		h1 = h1 * 5 + 0x52dce729;
279*043036a2SApple OSS Distributions 
280*043036a2SApple OSS Distributions 		k2 *= MH3_X64_128_C2;
281*043036a2SApple OSS Distributions #if defined(__x86_64__)
282*043036a2SApple OSS Distributions 		__asm__ ( "rol   $33, %[k2]\n\t" :[k2] "+r" (k2) : :);
283*043036a2SApple OSS Distributions #elif defined(__arm64__)
284*043036a2SApple OSS Distributions 		__asm__ ( "ror   %[k2], %[k2], #(64-33)\n\t" :[k2] "+r" (k2) : :);
285*043036a2SApple OSS Distributions #else /* !__x86_64__ && !__arm64__ */
286*043036a2SApple OSS Distributions 		k2 = ROTL64(k2, 33);
287*043036a2SApple OSS Distributions #endif /* !__x86_64__ && !__arm64__ */
288*043036a2SApple OSS Distributions 		k2 *= MH3_X64_128_C1;
289*043036a2SApple OSS Distributions 		h2 ^= k2;
290*043036a2SApple OSS Distributions 
291*043036a2SApple OSS Distributions #if defined(__x86_64__)
292*043036a2SApple OSS Distributions 		__asm__ ( "rol   $31, %[h2]\n\t" :[h2] "+r" (h2) : :);
293*043036a2SApple OSS Distributions #elif defined(__arm64__)
294*043036a2SApple OSS Distributions 		__asm__ ( "ror   %[h2], %[h2], #(64-31)\n\t" :[h2] "+r" (h2) : :);
295*043036a2SApple OSS Distributions #else /* !__x86_64__ && !__arm64__ */
296*043036a2SApple OSS Distributions 		h2 = ROTL64(h2, 31);
297*043036a2SApple OSS Distributions #endif /* !__x86_64__ && !__arm64__ */
298*043036a2SApple OSS Distributions 		h2 += h1;
299*043036a2SApple OSS Distributions 		h2 = h2 * 5 + 0x38495ab5;
300*043036a2SApple OSS Distributions 	}
301*043036a2SApple OSS Distributions 
302*043036a2SApple OSS Distributions 	/* tail */
303*043036a2SApple OSS Distributions 	tail = (const u_int8_t *)(const void *)(data + nblocks * 16);
304*043036a2SApple OSS Distributions 	k1 = 0;
305*043036a2SApple OSS Distributions 	k2 = 0;
306*043036a2SApple OSS Distributions 
307*043036a2SApple OSS Distributions 	switch (len & 15) {
308*043036a2SApple OSS Distributions 	case 15:
309*043036a2SApple OSS Distributions 		k2 ^= ((u_int64_t)tail[14]) << 48;
310*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
311*043036a2SApple OSS Distributions 	case 14:
312*043036a2SApple OSS Distributions 		k2 ^= ((u_int64_t)tail[13]) << 40;
313*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
314*043036a2SApple OSS Distributions 	case 13:
315*043036a2SApple OSS Distributions 		k2 ^= ((u_int64_t)tail[12]) << 32;
316*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
317*043036a2SApple OSS Distributions 	case 12:
318*043036a2SApple OSS Distributions 		k2 ^= ((u_int64_t)tail[11]) << 24;
319*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
320*043036a2SApple OSS Distributions 	case 11:
321*043036a2SApple OSS Distributions 		k2 ^= ((u_int64_t)tail[10]) << 16;
322*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
323*043036a2SApple OSS Distributions 	case 10:
324*043036a2SApple OSS Distributions 		k2 ^= ((u_int64_t)tail[9]) << 8;
325*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
326*043036a2SApple OSS Distributions 	case 9:
327*043036a2SApple OSS Distributions 		k2 ^= ((u_int64_t)tail[8]) << 0;
328*043036a2SApple OSS Distributions 		k2 *= MH3_X64_128_C2;
329*043036a2SApple OSS Distributions #if defined(__x86_64__)
330*043036a2SApple OSS Distributions 		__asm__ ( "rol   $33, %[k2]\n\t" :[k2] "+r" (k2) : :);
331*043036a2SApple OSS Distributions #elif defined(__arm64__)
332*043036a2SApple OSS Distributions 		__asm__ ( "ror   %[k2], %[k2], #(64-33)\n\t" :[k2] "+r" (k2) : :);
333*043036a2SApple OSS Distributions #else /* !__x86_64__ && !__arm64__ */
334*043036a2SApple OSS Distributions 		k2 = ROTL64(k2, 33);
335*043036a2SApple OSS Distributions #endif /* !__x86_64__ && !__arm64__ */
336*043036a2SApple OSS Distributions 		k2 *= MH3_X64_128_C1;
337*043036a2SApple OSS Distributions 		h2 ^= k2;
338*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
339*043036a2SApple OSS Distributions 	case 8:
340*043036a2SApple OSS Distributions 		k1 ^= ((u_int64_t)tail[7]) << 56;
341*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
342*043036a2SApple OSS Distributions 	case 7:
343*043036a2SApple OSS Distributions 		k1 ^= ((u_int64_t)tail[6]) << 48;
344*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
345*043036a2SApple OSS Distributions 	case 6:
346*043036a2SApple OSS Distributions 		k1 ^= ((u_int64_t)tail[5]) << 40;
347*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
348*043036a2SApple OSS Distributions 	case 5:
349*043036a2SApple OSS Distributions 		k1 ^= ((u_int64_t)tail[4]) << 32;
350*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
351*043036a2SApple OSS Distributions 	case 4:
352*043036a2SApple OSS Distributions 		k1 ^= ((u_int64_t)tail[3]) << 24;
353*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
354*043036a2SApple OSS Distributions 	case 3:
355*043036a2SApple OSS Distributions 		k1 ^= ((u_int64_t)tail[2]) << 16;
356*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
357*043036a2SApple OSS Distributions 	case 2:
358*043036a2SApple OSS Distributions 		k1 ^= ((u_int64_t)tail[1]) << 8;
359*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
360*043036a2SApple OSS Distributions 	case 1:
361*043036a2SApple OSS Distributions 		k1 ^= ((u_int64_t)tail[0]) << 0;
362*043036a2SApple OSS Distributions 		k1 *= MH3_X64_128_C1;
363*043036a2SApple OSS Distributions #if defined(__x86_64__)
364*043036a2SApple OSS Distributions 		__asm__ ( "rol   $31, %[k1]\n\t" :[k1] "+r" (k1) : :);
365*043036a2SApple OSS Distributions #elif defined(__arm64__)
366*043036a2SApple OSS Distributions 		__asm__ ( "ror   %[k1], %[k1], #(64-31)\n\t" :[k1] "+r" (k1) : :);
367*043036a2SApple OSS Distributions #else /* !__x86_64__ && !__arm64__ */
368*043036a2SApple OSS Distributions 		k1 = ROTL64(k1, 31);
369*043036a2SApple OSS Distributions #endif /* !__x86_64__ && !__arm64__ */
370*043036a2SApple OSS Distributions 		k1 *= MH3_X64_128_C2;
371*043036a2SApple OSS Distributions 		h1 ^= k1;
372*043036a2SApple OSS Distributions 	}
373*043036a2SApple OSS Distributions 	;
374*043036a2SApple OSS Distributions 
375*043036a2SApple OSS Distributions 	/* finalization */
376*043036a2SApple OSS Distributions 	h1 ^= len;
377*043036a2SApple OSS Distributions 	h2 ^= len;
378*043036a2SApple OSS Distributions 
379*043036a2SApple OSS Distributions 	h1 += h2;
380*043036a2SApple OSS Distributions 	h2 += h1;
381*043036a2SApple OSS Distributions 
382*043036a2SApple OSS Distributions 	h1 = mh3_fmix64(h1);
383*043036a2SApple OSS Distributions 	h2 = mh3_fmix64(h2);
384*043036a2SApple OSS Distributions 
385*043036a2SApple OSS Distributions 	h1 += h2;
386*043036a2SApple OSS Distributions 	h2 += h1;
387*043036a2SApple OSS Distributions 
388*043036a2SApple OSS Distributions 	/* throw all but lowest 32-bit */
389*043036a2SApple OSS Distributions 	return h1 & 0xffffffff;
390*043036a2SApple OSS Distributions }
391*043036a2SApple OSS Distributions 
392*043036a2SApple OSS Distributions #define JHASH_INIT      0xdeadbeef
393*043036a2SApple OSS Distributions 
394*043036a2SApple OSS Distributions #define JHASH_MIX(a, b, c) {                    \
395*043036a2SApple OSS Distributions 	a -= c;  a ^= ROTL32(c, 4);   c += b;   \
396*043036a2SApple OSS Distributions 	b -= a;  b ^= ROTL32(a, 6);   a += c;   \
397*043036a2SApple OSS Distributions 	c -= b;  c ^= ROTL32(b, 8);   b += a;   \
398*043036a2SApple OSS Distributions 	a -= c;  a ^= ROTL32(c, 16);  c += b;   \
399*043036a2SApple OSS Distributions 	b -= a;  b ^= ROTL32(a, 19);  a += c;   \
400*043036a2SApple OSS Distributions 	c -= b;  c ^= ROTL32(b, 4);   b += a;   \
401*043036a2SApple OSS Distributions }
402*043036a2SApple OSS Distributions 
403*043036a2SApple OSS Distributions #define JHASH_FINAL(a, b, c) {                  \
404*043036a2SApple OSS Distributions 	c ^= b;  c -= ROTL32(b, 14);            \
405*043036a2SApple OSS Distributions 	a ^= c;  a -= ROTL32(c, 11);            \
406*043036a2SApple OSS Distributions 	b ^= a;  b -= ROTL32(a, 25);            \
407*043036a2SApple OSS Distributions 	c ^= b;  c -= ROTL32(b, 16);            \
408*043036a2SApple OSS Distributions 	a ^= c;  a -= ROTL32(c, 4);             \
409*043036a2SApple OSS Distributions 	b ^= a;  b -= ROTL32(a, 14);            \
410*043036a2SApple OSS Distributions 	c ^= b;  c -= ROTL32(b, 24);            \
411*043036a2SApple OSS Distributions }
412*043036a2SApple OSS Distributions 
413*043036a2SApple OSS Distributions #if BYTE_ORDER == BIG_ENDIAN
414*043036a2SApple OSS Distributions /*
415*043036a2SApple OSS Distributions  * hashbig()
416*043036a2SApple OSS Distributions  */
417*043036a2SApple OSS Distributions u_int32_t
net_flowhash_jhash(const void * __sized_by (len)key,u_int32_t len,const u_int32_t seed)418*043036a2SApple OSS Distributions net_flowhash_jhash(const void *__sized_by(len) key, u_int32_t len, const u_int32_t seed)
419*043036a2SApple OSS Distributions {
420*043036a2SApple OSS Distributions 	u_int32_t a, b, c;
421*043036a2SApple OSS Distributions 
422*043036a2SApple OSS Distributions 	/* Set up the internal state */
423*043036a2SApple OSS Distributions 	a = b = c = JHASH_INIT + len + seed;
424*043036a2SApple OSS Distributions 
425*043036a2SApple OSS Distributions 	if (ALIGNED32(key)) {
426*043036a2SApple OSS Distributions 		/* read 32-bit chunks */
427*043036a2SApple OSS Distributions 		u_int32_t k_len = len;
428*043036a2SApple OSS Distributions 		const u_int32_t *__sized_by(k_len) k = (const u_int32_t *)key;
429*043036a2SApple OSS Distributions 
430*043036a2SApple OSS Distributions 		/*
431*043036a2SApple OSS Distributions 		 * all but last block:
432*043036a2SApple OSS Distributions 		 * aligned reads and affect 32 bits of (a,b,c)
433*043036a2SApple OSS Distributions 		 */
434*043036a2SApple OSS Distributions 		while (k_len > 12) {
435*043036a2SApple OSS Distributions 			a += k[0];
436*043036a2SApple OSS Distributions 			b += k[1];
437*043036a2SApple OSS Distributions 			c += k[2];
438*043036a2SApple OSS Distributions 			JHASH_MIX(a, b, c);
439*043036a2SApple OSS Distributions 			k_len -= 12;
440*043036a2SApple OSS Distributions 			k += 3;
441*043036a2SApple OSS Distributions 		}
442*043036a2SApple OSS Distributions 
443*043036a2SApple OSS Distributions 		/*
444*043036a2SApple OSS Distributions 		 * handle the last (probably partial) block
445*043036a2SApple OSS Distributions 		 *
446*043036a2SApple OSS Distributions 		 * "k[2] << 8" actually reads beyond the end of the string,
447*043036a2SApple OSS Distributions 		 * but then shifts out the part it's not allowed to read.
448*043036a2SApple OSS Distributions 		 * Because the string is aligned, the illegal read is in
449*043036a2SApple OSS Distributions 		 * the same word as the rest of the string.  The masking
450*043036a2SApple OSS Distributions 		 * trick does make the hash noticably faster for short
451*043036a2SApple OSS Distributions 		 * strings (like English words).
452*043036a2SApple OSS Distributions 		 */
453*043036a2SApple OSS Distributions 		switch (k_len) {
454*043036a2SApple OSS Distributions 		case 12:
455*043036a2SApple OSS Distributions 			c += k[2];
456*043036a2SApple OSS Distributions 			b += k[1];
457*043036a2SApple OSS Distributions 			a += k[0];
458*043036a2SApple OSS Distributions 			break;
459*043036a2SApple OSS Distributions 
460*043036a2SApple OSS Distributions 		case 11:
461*043036a2SApple OSS Distributions 			c += k[2] & 0xffffff00;
462*043036a2SApple OSS Distributions 			b += k[1];
463*043036a2SApple OSS Distributions 			a += k[0];
464*043036a2SApple OSS Distributions 			break;
465*043036a2SApple OSS Distributions 
466*043036a2SApple OSS Distributions 		case 10:
467*043036a2SApple OSS Distributions 			c += k[2] & 0xffff0000;
468*043036a2SApple OSS Distributions 			b += k[1];
469*043036a2SApple OSS Distributions 			a += k[0];
470*043036a2SApple OSS Distributions 			break;
471*043036a2SApple OSS Distributions 
472*043036a2SApple OSS Distributions 		case 9:
473*043036a2SApple OSS Distributions 			c += k[2] & 0xff000000;
474*043036a2SApple OSS Distributions 			b += k[1];
475*043036a2SApple OSS Distributions 			a += k[0];
476*043036a2SApple OSS Distributions 			break;
477*043036a2SApple OSS Distributions 
478*043036a2SApple OSS Distributions 		case 8:
479*043036a2SApple OSS Distributions 			b += k[1];
480*043036a2SApple OSS Distributions 			a += k[0];
481*043036a2SApple OSS Distributions 			break;
482*043036a2SApple OSS Distributions 
483*043036a2SApple OSS Distributions 		case 7:
484*043036a2SApple OSS Distributions 			b += k[1] & 0xffffff00;
485*043036a2SApple OSS Distributions 			a += k[0];
486*043036a2SApple OSS Distributions 			break;
487*043036a2SApple OSS Distributions 
488*043036a2SApple OSS Distributions 		case 6:
489*043036a2SApple OSS Distributions 			b += k[1] & 0xffff0000;
490*043036a2SApple OSS Distributions 			a += k[0];
491*043036a2SApple OSS Distributions 			break;
492*043036a2SApple OSS Distributions 
493*043036a2SApple OSS Distributions 		case 5:
494*043036a2SApple OSS Distributions 			b += k[1] & 0xff000000;
495*043036a2SApple OSS Distributions 			a += k[0];
496*043036a2SApple OSS Distributions 			break;
497*043036a2SApple OSS Distributions 
498*043036a2SApple OSS Distributions 		case 4:
499*043036a2SApple OSS Distributions 			a += k[0];
500*043036a2SApple OSS Distributions 			break;
501*043036a2SApple OSS Distributions 
502*043036a2SApple OSS Distributions 		case 3:
503*043036a2SApple OSS Distributions 			a += k[0] & 0xffffff00;
504*043036a2SApple OSS Distributions 			break;
505*043036a2SApple OSS Distributions 
506*043036a2SApple OSS Distributions 		case 2:
507*043036a2SApple OSS Distributions 			a += k[0] & 0xffff0000;
508*043036a2SApple OSS Distributions 			break;
509*043036a2SApple OSS Distributions 
510*043036a2SApple OSS Distributions 		case 1:
511*043036a2SApple OSS Distributions 			a += k[0] & 0xff000000;
512*043036a2SApple OSS Distributions 			break;
513*043036a2SApple OSS Distributions 
514*043036a2SApple OSS Distributions 		case 0:
515*043036a2SApple OSS Distributions 			/* zero length requires no mixing */
516*043036a2SApple OSS Distributions 			return c;
517*043036a2SApple OSS Distributions 		}
518*043036a2SApple OSS Distributions 
519*043036a2SApple OSS Distributions 		JHASH_FINAL(a, b, c);
520*043036a2SApple OSS Distributions 
521*043036a2SApple OSS Distributions 		return c;
522*043036a2SApple OSS Distributions 	}
523*043036a2SApple OSS Distributions 
524*043036a2SApple OSS Distributions 	/* need to read the key one byte at a time */
525*043036a2SApple OSS Distributions 	u_int32_t k_len = len;
526*043036a2SApple OSS Distributions 	const u_int8_t *__sized_by(k_len) k = (const u_int8_t *)key;
527*043036a2SApple OSS Distributions 
528*043036a2SApple OSS Distributions 	/* all but the last block: affect some 32 bits of (a,b,c) */
529*043036a2SApple OSS Distributions 	while (k_len > 12) {
530*043036a2SApple OSS Distributions 		a += ((u_int32_t)k[0]) << 24;
531*043036a2SApple OSS Distributions 		a += ((u_int32_t)k[1]) << 16;
532*043036a2SApple OSS Distributions 		a += ((u_int32_t)k[2]) << 8;
533*043036a2SApple OSS Distributions 		a += ((u_int32_t)k[3]);
534*043036a2SApple OSS Distributions 		b += ((u_int32_t)k[4]) << 24;
535*043036a2SApple OSS Distributions 		b += ((u_int32_t)k[5]) << 16;
536*043036a2SApple OSS Distributions 		b += ((u_int32_t)k[6]) << 8;
537*043036a2SApple OSS Distributions 		b += ((u_int32_t)k[7]);
538*043036a2SApple OSS Distributions 		c += ((u_int32_t)k[8]) << 24;
539*043036a2SApple OSS Distributions 		c += ((u_int32_t)k[9]) << 16;
540*043036a2SApple OSS Distributions 		c += ((u_int32_t)k[10]) << 8;
541*043036a2SApple OSS Distributions 		c += ((u_int32_t)k[11]);
542*043036a2SApple OSS Distributions 		JHASH_MIX(a, b, c);
543*043036a2SApple OSS Distributions 		k_len -= 12;
544*043036a2SApple OSS Distributions 		k += 12;
545*043036a2SApple OSS Distributions 	}
546*043036a2SApple OSS Distributions 
547*043036a2SApple OSS Distributions 	/* last block: affect all 32 bits of (c) */
548*043036a2SApple OSS Distributions 	switch (k_len) {
549*043036a2SApple OSS Distributions 	case 12:
550*043036a2SApple OSS Distributions 		c += k[11];
551*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
552*043036a2SApple OSS Distributions 	case 11:
553*043036a2SApple OSS Distributions 		c += ((u_int32_t)k[10]) << 8;
554*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
555*043036a2SApple OSS Distributions 	case 10:
556*043036a2SApple OSS Distributions 		c += ((u_int32_t)k[9]) << 16;
557*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
558*043036a2SApple OSS Distributions 	case 9:
559*043036a2SApple OSS Distributions 		c += ((u_int32_t)k[8]) << 24;
560*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
561*043036a2SApple OSS Distributions 	case 8:
562*043036a2SApple OSS Distributions 		b += k[7];
563*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
564*043036a2SApple OSS Distributions 	case 7:
565*043036a2SApple OSS Distributions 		b += ((u_int32_t)k[6]) << 8;
566*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
567*043036a2SApple OSS Distributions 	case 6:
568*043036a2SApple OSS Distributions 		b += ((u_int32_t)k[5]) << 16;
569*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
570*043036a2SApple OSS Distributions 	case 5:
571*043036a2SApple OSS Distributions 		b += ((u_int32_t)k[4]) << 24;
572*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
573*043036a2SApple OSS Distributions 	case 4:
574*043036a2SApple OSS Distributions 		a += k[3];
575*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
576*043036a2SApple OSS Distributions 	case 3:
577*043036a2SApple OSS Distributions 		a += ((u_int32_t)k[2]) << 8;
578*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
579*043036a2SApple OSS Distributions 	case 2:
580*043036a2SApple OSS Distributions 		a += ((u_int32_t)k[1]) << 16;
581*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
582*043036a2SApple OSS Distributions 	case 1:
583*043036a2SApple OSS Distributions 		a += ((u_int32_t)k[0]) << 24;
584*043036a2SApple OSS Distributions 		break;
585*043036a2SApple OSS Distributions 
586*043036a2SApple OSS Distributions 	case 0:
587*043036a2SApple OSS Distributions 		/* zero length requires no mixing */
588*043036a2SApple OSS Distributions 		return c;
589*043036a2SApple OSS Distributions 	}
590*043036a2SApple OSS Distributions 
591*043036a2SApple OSS Distributions 	JHASH_FINAL(a, b, c);
592*043036a2SApple OSS Distributions 
593*043036a2SApple OSS Distributions 	return c;
594*043036a2SApple OSS Distributions }
595*043036a2SApple OSS Distributions #else /* LITTLE_ENDIAN */
596*043036a2SApple OSS Distributions /*
597*043036a2SApple OSS Distributions  * hashlittle()
598*043036a2SApple OSS Distributions  */
599*043036a2SApple OSS Distributions u_int32_t
net_flowhash_jhash(const void * __sized_by (len)key,u_int32_t len,const u_int32_t seed)600*043036a2SApple OSS Distributions net_flowhash_jhash(const void *__sized_by(len) key, u_int32_t len, const u_int32_t seed)
601*043036a2SApple OSS Distributions {
602*043036a2SApple OSS Distributions 	u_int32_t a, b, c;
603*043036a2SApple OSS Distributions 
604*043036a2SApple OSS Distributions 	/* Set up the internal state */
605*043036a2SApple OSS Distributions 	a = b = c = JHASH_INIT + len + seed;
606*043036a2SApple OSS Distributions 
607*043036a2SApple OSS Distributions #if defined(__i386__) || defined(__x86_64__)
608*043036a2SApple OSS Distributions 	/*
609*043036a2SApple OSS Distributions 	 * On i386/x86_64, it is faster to read 32-bit chunks if the key
610*043036a2SApple OSS Distributions 	 * is aligned 32-bit OR not 16-bit, and perform 16-bit reads if it
611*043036a2SApple OSS Distributions 	 * is aligned 16-bit.
612*043036a2SApple OSS Distributions 	 */
613*043036a2SApple OSS Distributions 	if (ALIGNED32(key) || !ALIGNED16(key)) {
614*043036a2SApple OSS Distributions #else /* !defined(__i386__) && !defined(__x86_64__) */
615*043036a2SApple OSS Distributions 	if (ALIGNED32(key)) {
616*043036a2SApple OSS Distributions #endif /* !defined(__i386__) && !defined(__x86_64__) */
617*043036a2SApple OSS Distributions 		/* read 32-bit chunks */
618*043036a2SApple OSS Distributions 		u_int32_t k_len = len;
619*043036a2SApple OSS Distributions 		const u_int32_t *__sized_by(k_len) k = (const u_int32_t *)key;
620*043036a2SApple OSS Distributions 		const u_int16_t *k16 = (const u_int16_t *)key;
621*043036a2SApple OSS Distributions 		const u_int8_t *k8 = (const u_int8_t *)key;
622*043036a2SApple OSS Distributions 
623*043036a2SApple OSS Distributions 		/*
624*043036a2SApple OSS Distributions 		 * all but last block:
625*043036a2SApple OSS Distributions 		 * aligned reads and affect 32 bits of (a,b,c)
626*043036a2SApple OSS Distributions 		 */
627*043036a2SApple OSS Distributions 		while (k_len > 12) {
628*043036a2SApple OSS Distributions 			a += k[0];
629*043036a2SApple OSS Distributions 			b += k[1];
630*043036a2SApple OSS Distributions 			c += k[2];
631*043036a2SApple OSS Distributions 			JHASH_MIX(a, b, c);
632*043036a2SApple OSS Distributions 			k_len -= 12;
633*043036a2SApple OSS Distributions 			k += 3;
634*043036a2SApple OSS Distributions 		}
635*043036a2SApple OSS Distributions 
636*043036a2SApple OSS Distributions 		/* handle the last (probably partial) block */
637*043036a2SApple OSS Distributions 		switch (k_len) {
638*043036a2SApple OSS Distributions 		case 12:
639*043036a2SApple OSS Distributions 			c += k[2];
640*043036a2SApple OSS Distributions 			b += k[1];
641*043036a2SApple OSS Distributions 			a += k[0];
642*043036a2SApple OSS Distributions 			break;
643*043036a2SApple OSS Distributions 
644*043036a2SApple OSS Distributions 		case 11:
645*043036a2SApple OSS Distributions 			c += ((u_int32_t)k8[10]) << 16;
646*043036a2SApple OSS Distributions 			c += k16[4];
647*043036a2SApple OSS Distributions 			b += k[1];
648*043036a2SApple OSS Distributions 			a += k[0];
649*043036a2SApple OSS Distributions 			break;
650*043036a2SApple OSS Distributions 
651*043036a2SApple OSS Distributions 		case 10:
652*043036a2SApple OSS Distributions 			c += k16[4];
653*043036a2SApple OSS Distributions 			b += k[1];
654*043036a2SApple OSS Distributions 			a += k[0];
655*043036a2SApple OSS Distributions 			break;
656*043036a2SApple OSS Distributions 
657*043036a2SApple OSS Distributions 		case 9:
658*043036a2SApple OSS Distributions 			c += k8[8];
659*043036a2SApple OSS Distributions 			b += k[1];
660*043036a2SApple OSS Distributions 			a += k[0];
661*043036a2SApple OSS Distributions 			break;
662*043036a2SApple OSS Distributions 
663*043036a2SApple OSS Distributions 		case 8:
664*043036a2SApple OSS Distributions 			b += k[1];
665*043036a2SApple OSS Distributions 			a += k[0];
666*043036a2SApple OSS Distributions 			break;
667*043036a2SApple OSS Distributions 
668*043036a2SApple OSS Distributions 		case 7:
669*043036a2SApple OSS Distributions 			b += ((u_int32_t)k8[6]) << 16;
670*043036a2SApple OSS Distributions 			b += k16[2];
671*043036a2SApple OSS Distributions 			a += k[0];
672*043036a2SApple OSS Distributions 			break;
673*043036a2SApple OSS Distributions 
674*043036a2SApple OSS Distributions 		case 6:
675*043036a2SApple OSS Distributions 			b += k16[2];
676*043036a2SApple OSS Distributions 			a += k[0];
677*043036a2SApple OSS Distributions 			break;
678*043036a2SApple OSS Distributions 
679*043036a2SApple OSS Distributions 		case 5:
680*043036a2SApple OSS Distributions 			b += k8[4];
681*043036a2SApple OSS Distributions 			a += k[0];
682*043036a2SApple OSS Distributions 			break;
683*043036a2SApple OSS Distributions 
684*043036a2SApple OSS Distributions 		case 4:
685*043036a2SApple OSS Distributions 			a += k[0];
686*043036a2SApple OSS Distributions 			break;
687*043036a2SApple OSS Distributions 
688*043036a2SApple OSS Distributions 		case 3:
689*043036a2SApple OSS Distributions 			a += ((u_int32_t)k8[2]) << 16;
690*043036a2SApple OSS Distributions 			a += k16[0];
691*043036a2SApple OSS Distributions 			break;
692*043036a2SApple OSS Distributions 
693*043036a2SApple OSS Distributions 		case 2:
694*043036a2SApple OSS Distributions 			a += k16[0];
695*043036a2SApple OSS Distributions 			break;
696*043036a2SApple OSS Distributions 
697*043036a2SApple OSS Distributions 		case 1:
698*043036a2SApple OSS Distributions 			a += k8[0];
699*043036a2SApple OSS Distributions 			break;
700*043036a2SApple OSS Distributions 
701*043036a2SApple OSS Distributions 		case 0:
702*043036a2SApple OSS Distributions 			/* zero length requires no mixing */
703*043036a2SApple OSS Distributions 			return c;
704*043036a2SApple OSS Distributions 		}
705*043036a2SApple OSS Distributions 
706*043036a2SApple OSS Distributions 		JHASH_FINAL(a, b, c);
707*043036a2SApple OSS Distributions 
708*043036a2SApple OSS Distributions 		return c;
709*043036a2SApple OSS Distributions 	}
710*043036a2SApple OSS Distributions #if !defined(__i386__) && !defined(__x86_64__)
711*043036a2SApple OSS Distributions 	else if (ALIGNED16(key)) {
712*043036a2SApple OSS Distributions #endif /* !defined(__i386__) && !defined(__x86_64__) */
713*043036a2SApple OSS Distributions 	/* read 16-bit chunks */
714*043036a2SApple OSS Distributions 	u_int32_t k_len = len;
715*043036a2SApple OSS Distributions 	const u_int16_t *__sized_by(k_len) k = (const u_int16_t *)key;
716*043036a2SApple OSS Distributions 	const u_int8_t *k8;
717*043036a2SApple OSS Distributions 
718*043036a2SApple OSS Distributions 	/* all but last block: aligned reads and different mixing */
719*043036a2SApple OSS Distributions 	while (k_len > 12) {
720*043036a2SApple OSS Distributions 		a += k[0] + (((u_int32_t)k[1]) << 16);
721*043036a2SApple OSS Distributions 		b += k[2] + (((u_int32_t)k[3]) << 16);
722*043036a2SApple OSS Distributions 		c += k[4] + (((u_int32_t)k[5]) << 16);
723*043036a2SApple OSS Distributions 		JHASH_MIX(a, b, c);
724*043036a2SApple OSS Distributions 		k_len -= 12;
725*043036a2SApple OSS Distributions 		k += 6;
726*043036a2SApple OSS Distributions 	}
727*043036a2SApple OSS Distributions 
728*043036a2SApple OSS Distributions 	/* handle the last (probably partial) block */
729*043036a2SApple OSS Distributions 	k8 = (const u_int8_t *)k;
730*043036a2SApple OSS Distributions 	switch (k_len) {
731*043036a2SApple OSS Distributions 	case 12:
732*043036a2SApple OSS Distributions 		c += k[4] + (((u_int32_t)k[5]) << 16);
733*043036a2SApple OSS Distributions 		b += k[2] + (((u_int32_t)k[3]) << 16);
734*043036a2SApple OSS Distributions 		a += k[0] + (((u_int32_t)k[1]) << 16);
735*043036a2SApple OSS Distributions 		break;
736*043036a2SApple OSS Distributions 
737*043036a2SApple OSS Distributions 	case 11:
738*043036a2SApple OSS Distributions 		c += ((u_int32_t)k8[10]) << 16;
739*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
740*043036a2SApple OSS Distributions 	case 10:
741*043036a2SApple OSS Distributions 		c += k[4];
742*043036a2SApple OSS Distributions 		b += k[2] + (((u_int32_t)k[3]) << 16);
743*043036a2SApple OSS Distributions 		a += k[0] + (((u_int32_t)k[1]) << 16);
744*043036a2SApple OSS Distributions 		break;
745*043036a2SApple OSS Distributions 
746*043036a2SApple OSS Distributions 	case 9:
747*043036a2SApple OSS Distributions 		c += k8[8];
748*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
749*043036a2SApple OSS Distributions 	case 8:
750*043036a2SApple OSS Distributions 		b += k[2] + (((u_int32_t)k[3]) << 16);
751*043036a2SApple OSS Distributions 		a += k[0] + (((u_int32_t)k[1]) << 16);
752*043036a2SApple OSS Distributions 		break;
753*043036a2SApple OSS Distributions 
754*043036a2SApple OSS Distributions 	case 7:
755*043036a2SApple OSS Distributions 		b += ((u_int32_t)k8[6]) << 16;
756*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
757*043036a2SApple OSS Distributions 	case 6:
758*043036a2SApple OSS Distributions 		b += k[2];
759*043036a2SApple OSS Distributions 		a += k[0] + (((u_int32_t)k[1]) << 16);
760*043036a2SApple OSS Distributions 		break;
761*043036a2SApple OSS Distributions 
762*043036a2SApple OSS Distributions 	case 5:
763*043036a2SApple OSS Distributions 		b += k8[4];
764*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
765*043036a2SApple OSS Distributions 	case 4:
766*043036a2SApple OSS Distributions 		a += k[0] + (((u_int32_t)k[1]) << 16);
767*043036a2SApple OSS Distributions 		break;
768*043036a2SApple OSS Distributions 
769*043036a2SApple OSS Distributions 	case 3:
770*043036a2SApple OSS Distributions 		a += ((u_int32_t)k8[2]) << 16;
771*043036a2SApple OSS Distributions 		OS_FALLTHROUGH;
772*043036a2SApple OSS Distributions 	case 2:
773*043036a2SApple OSS Distributions 		a += k[0];
774*043036a2SApple OSS Distributions 		break;
775*043036a2SApple OSS Distributions 
776*043036a2SApple OSS Distributions 	case 1:
777*043036a2SApple OSS Distributions 		a += k8[0];
778*043036a2SApple OSS Distributions 		break;
779*043036a2SApple OSS Distributions 
780*043036a2SApple OSS Distributions 	case 0:
781*043036a2SApple OSS Distributions 		/* zero length requires no mixing */
782*043036a2SApple OSS Distributions 		return c;
783*043036a2SApple OSS Distributions 	}
784*043036a2SApple OSS Distributions 
785*043036a2SApple OSS Distributions 	JHASH_FINAL(a, b, c);
786*043036a2SApple OSS Distributions 
787*043036a2SApple OSS Distributions 	return c;
788*043036a2SApple OSS Distributions #if !defined(__i386__) && !defined(__x86_64__)
789*043036a2SApple OSS Distributions }
790*043036a2SApple OSS Distributions 
791*043036a2SApple OSS Distributions /* need to read the key one byte at a time */
792*043036a2SApple OSS Distributions u_int32_t k_len = len;
793*043036a2SApple OSS Distributions const u_int8_t *__sized_by(k_len) k = (const u_int8_t *)key;
794*043036a2SApple OSS Distributions 
795*043036a2SApple OSS Distributions /* all but the last block: affect some 32 bits of (a,b,c) */
796*043036a2SApple OSS Distributions while (k_len > 12) {
797*043036a2SApple OSS Distributions 	a += k[0];
798*043036a2SApple OSS Distributions 	a += ((u_int32_t)k[1]) << 8;
799*043036a2SApple OSS Distributions 	a += ((u_int32_t)k[2]) << 16;
800*043036a2SApple OSS Distributions 	a += ((u_int32_t)k[3]) << 24;
801*043036a2SApple OSS Distributions 	b += k[4];
802*043036a2SApple OSS Distributions 	b += ((u_int32_t)k[5]) << 8;
803*043036a2SApple OSS Distributions 	b += ((u_int32_t)k[6]) << 16;
804*043036a2SApple OSS Distributions 	b += ((u_int32_t)k[7]) << 24;
805*043036a2SApple OSS Distributions 	c += k[8];
806*043036a2SApple OSS Distributions 	c += ((u_int32_t)k[9]) << 8;
807*043036a2SApple OSS Distributions 	c += ((u_int32_t)k[10]) << 16;
808*043036a2SApple OSS Distributions 	c += ((u_int32_t)k[11]) << 24;
809*043036a2SApple OSS Distributions 	JHASH_MIX(a, b, c);
810*043036a2SApple OSS Distributions 	k_len -= 12;
811*043036a2SApple OSS Distributions 	k += 12;
812*043036a2SApple OSS Distributions }
813*043036a2SApple OSS Distributions 
814*043036a2SApple OSS Distributions /* last block: affect all 32 bits of (c) */
815*043036a2SApple OSS Distributions switch (k_len) {
816*043036a2SApple OSS Distributions case 12:
817*043036a2SApple OSS Distributions 	c += ((u_int32_t)k[11]) << 24;
818*043036a2SApple OSS Distributions 	OS_FALLTHROUGH;
819*043036a2SApple OSS Distributions case 11:
820*043036a2SApple OSS Distributions 	c += ((u_int32_t)k[10]) << 16;
821*043036a2SApple OSS Distributions 	OS_FALLTHROUGH;
822*043036a2SApple OSS Distributions case 10:
823*043036a2SApple OSS Distributions 	c += ((u_int32_t)k[9]) << 8;
824*043036a2SApple OSS Distributions 	OS_FALLTHROUGH;
825*043036a2SApple OSS Distributions case 9:
826*043036a2SApple OSS Distributions 	c += k[8];
827*043036a2SApple OSS Distributions 	OS_FALLTHROUGH;
828*043036a2SApple OSS Distributions case 8:
829*043036a2SApple OSS Distributions 	b += ((u_int32_t)k[7]) << 24;
830*043036a2SApple OSS Distributions 	OS_FALLTHROUGH;
831*043036a2SApple OSS Distributions case 7:
832*043036a2SApple OSS Distributions 	b += ((u_int32_t)k[6]) << 16;
833*043036a2SApple OSS Distributions 	OS_FALLTHROUGH;
834*043036a2SApple OSS Distributions case 6:
835*043036a2SApple OSS Distributions 	b += ((u_int32_t)k[5]) << 8;
836*043036a2SApple OSS Distributions 	OS_FALLTHROUGH;
837*043036a2SApple OSS Distributions case 5:
838*043036a2SApple OSS Distributions 	b += k[4];
839*043036a2SApple OSS Distributions 	OS_FALLTHROUGH;
840*043036a2SApple OSS Distributions case 4:
841*043036a2SApple OSS Distributions 	a += ((u_int32_t)k[3]) << 24;
842*043036a2SApple OSS Distributions 	OS_FALLTHROUGH;
843*043036a2SApple OSS Distributions case 3:
844*043036a2SApple OSS Distributions 	a += ((u_int32_t)k[2]) << 16;
845*043036a2SApple OSS Distributions 	OS_FALLTHROUGH;
846*043036a2SApple OSS Distributions case 2:
847*043036a2SApple OSS Distributions 	a += ((u_int32_t)k[1]) << 8;
848*043036a2SApple OSS Distributions 	OS_FALLTHROUGH;
849*043036a2SApple OSS Distributions case 1:
850*043036a2SApple OSS Distributions 	a += k[0];
851*043036a2SApple OSS Distributions 	break;
852*043036a2SApple OSS Distributions 
853*043036a2SApple OSS Distributions case 0:
854*043036a2SApple OSS Distributions 	/* zero length requires no mixing */
855*043036a2SApple OSS Distributions 	return c;
856*043036a2SApple OSS Distributions }
857*043036a2SApple OSS Distributions 
858*043036a2SApple OSS Distributions JHASH_FINAL(a, b, c);
859*043036a2SApple OSS Distributions 
860*043036a2SApple OSS Distributions return c;
861*043036a2SApple OSS Distributions #endif /* !defined(__i386__) && !defined(__x86_64__) */
862*043036a2SApple OSS Distributions }
863*043036a2SApple OSS Distributions #endif /* LITTLE_ENDIAN */
864