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