| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/utils.h" | 5 #include "platform/utils.h" |
| 6 | 6 |
| 7 namespace dart { | 7 namespace dart { |
| 8 | 8 |
| 9 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., | 9 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
| 10 // figure 3-3, page 48, where the function is called clp2. | 10 // figure 3-3, page 48, where the function is called clp2. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 int Utils::CountOneBits(uint32_t x) { | 24 int Utils::CountOneBits(uint32_t x) { |
| 25 x = x - ((x >> 1) & 0x55555555); | 25 x = x - ((x >> 1) & 0x55555555); |
| 26 x = (x & 0x33333333) + ((x >> 2) & 0x33333333); | 26 x = (x & 0x33333333) + ((x >> 2) & 0x33333333); |
| 27 x = (x + (x >> 4)) & 0x0F0F0F0F; | 27 x = (x + (x >> 4)) & 0x0F0F0F0F; |
| 28 x = x + (x >> 8); | 28 x = x + (x >> 8); |
| 29 x = x + (x >> 16); | 29 x = x + (x >> 16); |
| 30 return static_cast<int>(x & 0x0000003F); | 30 return static_cast<int>(x & 0x0000003F); |
| 31 } | 31 } |
| 32 | 32 |
| 33 | 33 |
| 34 int Utils::HighestBit(int64_t v) { |
| 35 uint64_t t = static_cast<uint64_t>((v > 0) ? v : -v); |
| 36 int count = 0; |
| 37 while ((t >>= 1) != 0) { |
| 38 count++; |
| 39 } |
| 40 return count; |
| 41 } |
| 42 |
| 43 |
| 34 uint32_t Utils::StringHash(const char* data, int length) { | 44 uint32_t Utils::StringHash(const char* data, int length) { |
| 35 // This implementation is based on the public domain MurmurHash | 45 // This implementation is based on the public domain MurmurHash |
| 36 // version 2.0. It assumes that the underlying CPU can read from | 46 // version 2.0. It assumes that the underlying CPU can read from |
| 37 // unaligned addresses. The constants M and R have been determined | 47 // unaligned addresses. The constants M and R have been determined |
| 38 // to work well experimentally. | 48 // to work well experimentally. |
| 39 // TODO(3158902): need to account for unaligned address access on ARM. | 49 // TODO(3158902): need to account for unaligned address access on ARM. |
| 40 const uint32_t M = 0x5bd1e995; | 50 const uint32_t M = 0x5bd1e995; |
| 41 const int R = 24; | 51 const int R = 24; |
| 42 int size = length; | 52 int size = length; |
| 43 uint32_t hash = size; | 53 uint32_t hash = size; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 uword a = static_cast<uword>(key); | 91 uword a = static_cast<uword>(key); |
| 82 a = (a + 0x7ed55d16) + (a << 12); | 92 a = (a + 0x7ed55d16) + (a << 12); |
| 83 a = (a ^ 0xc761c23c) ^ (a >> 19); | 93 a = (a ^ 0xc761c23c) ^ (a >> 19); |
| 84 a = (a + 0x165667b1) + (a << 5); | 94 a = (a + 0x165667b1) + (a << 5); |
| 85 a = (a + 0xd3a2646c) ^ (a << 9); | 95 a = (a + 0xd3a2646c) ^ (a << 9); |
| 86 a = (a + 0xfd7046c5) + (a << 3); | 96 a = (a + 0xfd7046c5) + (a << 3); |
| 87 a = (a ^ 0xb55a4f09) ^ (a >> 16); | 97 a = (a ^ 0xb55a4f09) ^ (a >> 16); |
| 88 return static_cast<uint32_t>(a); | 98 return static_cast<uint32_t>(a); |
| 89 } | 99 } |
| 90 | 100 |
| 101 |
| 91 } // namespace dart | 102 } // namespace dart |
| OLD | NEW |