1 | // Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #ifndef RUNTIME_VM_HASH_H_ |
6 | #define RUNTIME_VM_HASH_H_ |
7 | |
8 | #include "platform/globals.h" |
9 | |
10 | namespace dart { |
11 | |
12 | inline uint32_t CombineHashes(uint32_t hash, uint32_t other_hash) { |
13 | hash += other_hash; |
14 | hash += hash << 10; |
15 | hash ^= hash >> 6; // Logical shift, unsigned hash. |
16 | return hash; |
17 | } |
18 | |
19 | inline uint32_t FinalizeHash(uint32_t hash, intptr_t hashbits) { |
20 | hash += hash << 3; |
21 | hash ^= hash >> 11; // Logical shift, unsigned hash. |
22 | hash += hash << 15; |
23 | // FinalizeHash gets called with values for hashbits that are bigger than 31 |
24 | // (like kBitsPerWord - 1). Therefore we are careful to use a type |
25 | // (uintptr_t) big enough to avoid undefined behavior with the left shift. |
26 | hash &= (static_cast<uintptr_t>(1) << hashbits) - 1; |
27 | return (hash == 0) ? 1 : hash; |
28 | } |
29 | |
30 | inline uint32_t HashBytes(const uint8_t* bytes, intptr_t size) { |
31 | uint32_t hash = size; |
32 | while (size > 0) { |
33 | hash = CombineHashes(hash, *bytes); |
34 | bytes++; |
35 | size--; |
36 | } |
37 | return hash; |
38 | } |
39 | |
40 | } // namespace dart |
41 | |
42 | #endif // RUNTIME_VM_HASH_H_ |
43 | |