2 * Copyright (c) 2008, 2009, 2010, 2012, 2013, 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 #include "unaligned.h"
21 /* Returns the hash of 'a', 'b', and 'c'. */
23 hash_3words(uint32_t a, uint32_t b, uint32_t c)
25 return hash_finish(hash_add(hash_add(hash_add(a, 0), b), c), 12);
28 /* Returns the hash of the 'n' bytes at 'p', starting from 'basis'. */
30 hash_bytes(const void *p_, size_t n, uint32_t basis)
32 const uint32_t *p = p_;
38 hash = hash_add(hash, get_unaligned_u32(p));
47 hash = hash_add(hash, tmp);
50 return hash_finish(hash, orig_n);
54 hash_double(double x, uint32_t basis)
57 BUILD_ASSERT_DECL(sizeof x == sizeof value);
59 memcpy(value, &x, sizeof value);
60 return hash_3words(value[0], value[1], basis);
64 hash_words__(const uint32_t p[], size_t n_words, uint32_t basis)
66 return hash_words_inline(p, n_words, basis);
70 hash_words64__(const uint64_t p[], size_t n_words, uint32_t basis)
72 return hash_words64_inline(p, n_words, basis);
75 #if !(defined(__x86_64__))
77 hash_bytes128(const void *p_, size_t len, uint32_t basis, ovs_u128 *out)
79 const uint32_t c1 = 0x239b961b;
80 const uint32_t c2 = 0xab0e9789;
81 const uint32_t c3 = 0x38b34ae5;
82 const uint32_t c4 = 0xa1e38b93;
83 const uint8_t *tail, *data = (const uint8_t *)p_;
84 const uint32_t *blocks = (const uint32_t *)p_;
85 const int nblocks = len / 16;
90 uint32_t k1, k2, k3, k4;
93 for (int i = 0; i < nblocks; i++) {
94 uint32_t k1 = get_unaligned_u32(&blocks[i * 4 + 0]);
95 uint32_t k2 = get_unaligned_u32(&blocks[i * 4 + 1]);
96 uint32_t k3 = get_unaligned_u32(&blocks[i * 4 + 2]);
97 uint32_t k4 = get_unaligned_u32(&blocks[i * 4 + 3]);
100 k1 = hash_rot(k1, 15);
104 h1 = hash_rot(h1, 19);
106 h1 = h1 * 5 + 0x561ccd1b;
109 k2 = hash_rot(k2, 16);
113 h2 = hash_rot(h2, 17);
115 h2 = h2 * 5 + 0x0bcaa747;
118 k3 = hash_rot(k3, 17);
122 h3 = hash_rot(h3, 15);
124 h3 = h3 * 5 + 0x96cd1c35;
127 k4 = hash_rot(k4, 18);
131 h4 = hash_rot(h4, 13);
133 h4 = h4 * 5 + 0x32ac3b17;
137 k1 = k2 = k3 = k4 = 0;
138 tail = data + nblocks * 16;
141 k4 ^= tail[14] << 16;
147 k4 = hash_rot(k4, 18);
152 k3 ^= tail[11] << 24;
154 k3 ^= tail[10] << 16;
160 k3 = hash_rot(k3, 17);
173 k2 = hash_rot(k2, 16);
186 k1 = hash_rot(k1, 15);
204 h1 = mhash_finish(h1);
205 h2 = mhash_finish(h2);
206 h3 = mhash_finish(h3);
207 h4 = mhash_finish(h4);
222 #else /* __x86_64__ */
224 static inline uint64_t
225 hash_rot64(uint64_t x, int8_t r)
227 return (x << r) | (x >> (64 - r));
230 static inline uint64_t
234 k *= 0xff51afd7ed558ccdULL;
236 k *= 0xc4ceb9fe1a85ec53ULL;
243 hash_bytes128(const void *p_, size_t len, uint32_t basis, ovs_u128 *out)
245 const uint64_t c1 = 0x87c37b91114253d5ULL;
246 const uint64_t c2 = 0x4cf5ad432745937fULL;
247 const uint8_t *tail, *data = (const uint8_t *)p_;
248 const uint64_t *blocks = (const uint64_t *)p_;
249 const int nblocks = len / 16;
255 for (int i = 0; i < nblocks; i++) {
256 k1 = get_unaligned_u64(&blocks[i * 2 + 0]);
257 k2 = get_unaligned_u64(&blocks[i * 2 + 1]);
260 k1 = hash_rot64(k1, 31);
264 h1 = hash_rot64(h1, 27);
266 h1 = h1 * 5 + 0x52dce729;
269 k2 = hash_rot64(k2, 33);
273 h2 = hash_rot64(h2, 31);
275 h2 = h2 * 5 + 0x38495ab5;
281 tail = data + nblocks * 16;
284 k2 ^= ((uint64_t) tail[14]) << 48;
286 k2 ^= ((uint64_t) tail[13]) << 40;
288 k2 ^= ((uint64_t) tail[12]) << 32;
290 k2 ^= ((uint64_t) tail[11]) << 24;
292 k2 ^= ((uint64_t) tail[10]) << 16;
294 k2 ^= ((uint64_t) tail[9]) << 8;
296 k2 ^= ((uint64_t) tail[8]) << 0;
298 k2 = hash_rot64(k2, 33);
303 k1 ^= ((uint64_t) tail[7]) << 56;
305 k1 ^= ((uint64_t) tail[6]) << 48;
307 k1 ^= ((uint64_t) tail[5]) << 40;
309 k1 ^= ((uint64_t) tail[4]) << 32;
311 k1 ^= ((uint64_t) tail[3]) << 24;
313 k1 ^= ((uint64_t) tail[2]) << 16;
315 k1 ^= ((uint64_t) tail[1]) << 8;
317 k1 ^= ((uint64_t) tail[0]) << 0;
319 k1 = hash_rot64(k1, 31);
337 #endif /* __x86_64__ */