]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
c49d1dd1 | 2 | * Copyright (c) 2008, 2009, 2010, 2012, 2013 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
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. | |
064af421 BP |
15 | */ |
16 | #include <config.h> | |
17 | #include "hash.h" | |
18 | #include <string.h> | |
7376da64 | 19 | #include "unaligned.h" |
064af421 | 20 | |
1f26e796 | 21 | /* Returns the hash of 'a', 'b', and 'c'. */ |
cce1d8bd | 22 | uint32_t |
1f26e796 | 23 | hash_3words(uint32_t a, uint32_t b, uint32_t c) |
cce1d8bd | 24 | { |
33c6a1b9 | 25 | return hash_finish(hash_add(hash_add(hash_add(a, 0), b), c), 12); |
1f26e796 BP |
26 | } |
27 | ||
064af421 BP |
28 | /* Returns the hash of the 'n' bytes at 'p', starting from 'basis'. */ |
29 | uint32_t | |
30 | hash_bytes(const void *p_, size_t n, uint32_t basis) | |
31 | { | |
db5a1019 | 32 | const uint32_t *p = p_; |
c49d1dd1 BP |
33 | size_t orig_n = n; |
34 | uint32_t hash; | |
064af421 | 35 | |
c49d1dd1 BP |
36 | hash = basis; |
37 | while (n >= 4) { | |
33c6a1b9 | 38 | hash = hash_add(hash, get_unaligned_u32(p)); |
c49d1dd1 | 39 | n -= 4; |
db5a1019 | 40 | p += 1; |
064af421 BP |
41 | } |
42 | ||
43 | if (n) { | |
c49d1dd1 | 44 | uint32_t tmp = 0; |
7376da64 | 45 | |
c49d1dd1 | 46 | memcpy(&tmp, p, n); |
33c6a1b9 | 47 | hash = hash_add(hash, tmp); |
064af421 BP |
48 | } |
49 | ||
33c6a1b9 | 50 | return hash_finish(hash, orig_n); |
064af421 | 51 | } |
9879b94f | 52 | |
c49d1dd1 BP |
53 | uint32_t |
54 | hash_double(double x, uint32_t basis) | |
55 | { | |
56 | uint32_t value[2]; | |
57 | BUILD_ASSERT_DECL(sizeof x == sizeof value); | |
58 | ||
59 | memcpy(value, &x, sizeof value); | |
60 | return hash_3words(value[0], value[1], basis); | |
61 | } | |
ff8eeabd JR |
62 | |
63 | uint32_t | |
64 | hash_words__(const uint32_t p[], size_t n_words, uint32_t basis) | |
65 | { | |
66 | return hash_words_inline(p, n_words, basis); | |
67 | } | |
68 | ||
69 | uint32_t | |
70 | hash_words64__(const uint64_t p[], size_t n_words, uint64_t basis) | |
71 | { | |
72 | return hash_words64_inline(p, n_words, basis); | |
73 | } |