2 * Copyright (c) 2008, 2009, 2010, 2012, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 static __inline UINT32
20 GetUnalignedU32(const UINT32 *p_)
22 const UINT8 *p = (const UINT8 *)p_;
23 return ntohl((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
26 /* This is the public domain lookup3 hash by Bob Jenkins from
27 * http://burtleburtle.net/bob/c/lookup3.c, modified for style. */
29 static __inline UINT32
30 JhashRot(UINT32 x, INT k)
32 return (x << k) | (x >> (32 - k));
36 JhashMix(UINT32 *a, UINT32 *b, UINT32 *c)
38 *a -= *c; *a ^= JhashRot(*c, 4); *c += *b;
39 *b -= *a; *b ^= JhashRot(*a, 6); *a += *c;
40 *c -= *b; *c ^= JhashRot(*b, 8); *b += *a;
41 *a -= *c; *a ^= JhashRot(*c, 16); *c += *b;
42 *b -= *a; *b ^= JhashRot(*a, 19); *a += *c;
43 *c -= *b; *c ^= JhashRot(*b, 4); *b += *a;
47 JhashFinal(UINT32 *a, UINT32 *b, UINT32 *c)
49 *c ^= *b; *c -= JhashRot(*b, 14);
50 *a ^= *c; *a -= JhashRot(*c, 11);
51 *b ^= *a; *b -= JhashRot(*a, 25);
52 *c ^= *b; *c -= JhashRot(*b, 16);
53 *a ^= *c; *a -= JhashRot(*c, 4);
54 *b ^= *a; *b -= JhashRot(*a, 14);
55 *c ^= *b; *c -= JhashRot(*b, 24);
58 /* Returns the Jenkins hash of the 'n' 32-bit words at 'p', starting from
59 * 'basis'. 'p' must be properly aligned.
61 * Use hash_words() instead, unless you're computing a hash function whose
62 * value is exposed "on the wire" so we don't want to change it. */
64 OvsJhashWords(const UINT32 *p, SIZE_T n, UINT32 basis)
68 a = b = c = 0xdeadbeef + (((UINT32) n) << 2) + basis;
88 JhashFinal(&a, &b, &c);
96 /* Returns the Jenkins hash of the 'n' bytes at 'p', starting from 'basis'.
98 * Use hash_bytes() instead, unless you're computing a hash function whose
99 * value is exposed "on the wire" so we don't want to change it. */
101 OvsJhashBytes(const VOID *p_, SIZE_T n, UINT32 basis)
103 const UINT32 *p = p_;
106 a = b = c = 0xdeadbeef + (UINT32)n + basis;
109 a += GetUnalignedU32(p);
110 b += GetUnalignedU32(p + 1);
111 c += GetUnalignedU32(p + 2);
112 JhashMix(&a, &b, &c);
120 tmp[0] = tmp[1] = tmp[2] = 0;
125 JhashFinal(&a, &b, &c);