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 17 matching lines...) Expand all Loading... |
28 x = x - ((x >> 1) & 0x55555555); | 28 x = x - ((x >> 1) & 0x55555555); |
29 x = (x & 0x33333333) + ((x >> 2) & 0x33333333); | 29 x = (x & 0x33333333) + ((x >> 2) & 0x33333333); |
30 x = (x + (x >> 4)) & 0x0F0F0F0F; | 30 x = (x + (x >> 4)) & 0x0F0F0F0F; |
31 x = x + (x >> 8); | 31 x = x + (x >> 8); |
32 x = x + (x >> 16); | 32 x = x + (x >> 16); |
33 return static_cast<int>(x & 0x0000003F); | 33 return static_cast<int>(x & 0x0000003F); |
34 } | 34 } |
35 | 35 |
36 | 36 |
37 int Utils::HighestBit(int64_t v) { | 37 int Utils::HighestBit(int64_t v) { |
38 uint64_t t = static_cast<uint64_t>((v > 0) ? v : -v); | 38 uint64_t x = static_cast<uint64_t>((v > 0) ? v : -v); |
39 int count = 0; | 39 uint64_t t; |
40 while ((t >>= 1) != 0) { | 40 int r = 0; |
41 count++; | 41 if ((t = x >> 32) != 0) { x = t; r += 32; } |
42 } | 42 if ((t = x >> 16) != 0) { x = t; r += 16; } |
43 return count; | 43 if ((t = x >> 8) != 0) { x = t; r += 8; } |
| 44 if ((t = x >> 4) != 0) { x = t; r += 4; } |
| 45 if ((t = x >> 2) != 0) { x = t; r += 2; } |
| 46 if (x > 1) r += 1; |
| 47 return r; |
44 } | 48 } |
45 | 49 |
46 | 50 |
47 uint32_t Utils::StringHash(const char* data, int length) { | 51 uint32_t Utils::StringHash(const char* data, int length) { |
48 // This implementation is based on the public domain MurmurHash | 52 // This implementation is based on the public domain MurmurHash |
49 // version 2.0. It assumes that the underlying CPU can read from | 53 // version 2.0. It assumes that the underlying CPU can read from |
50 // unaligned addresses. The constants M and R have been determined | 54 // unaligned addresses. The constants M and R have been determined |
51 // to work well experimentally. | 55 // to work well experimentally. |
52 // TODO(3158902): need to account for unaligned address access on ARM. | 56 // TODO(3158902): need to account for unaligned address access on ARM. |
53 const uint32_t M = 0x5bd1e995; | 57 const uint32_t M = 0x5bd1e995; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 a = (a ^ 0xc761c23c) ^ (a >> 19); | 100 a = (a ^ 0xc761c23c) ^ (a >> 19); |
97 a = (a + 0x165667b1) + (a << 5); | 101 a = (a + 0x165667b1) + (a << 5); |
98 a = (a + 0xd3a2646c) ^ (a << 9); | 102 a = (a + 0xd3a2646c) ^ (a << 9); |
99 a = (a + 0xfd7046c5) + (a << 3); | 103 a = (a + 0xfd7046c5) + (a << 3); |
100 a = (a ^ 0xb55a4f09) ^ (a >> 16); | 104 a = (a ^ 0xb55a4f09) ^ (a >> 16); |
101 return static_cast<uint32_t>(a); | 105 return static_cast<uint32_t>(a); |
102 } | 106 } |
103 | 107 |
104 | 108 |
105 } // namespace dart | 109 } // namespace dart |
OLD | NEW |