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. |
11 uint32_t Utils::RoundUpToPowerOfTwo(uint32_t x) { | 11 uintptr_t Utils::RoundUpToPowerOfTwo(uintptr_t x) { |
12 x = x - 1; | 12 x = x - 1; |
13 x = x | (x >> 1); | 13 x = x | (x >> 1); |
14 x = x | (x >> 2); | 14 x = x | (x >> 2); |
15 x = x | (x >> 4); | 15 x = x | (x >> 4); |
16 x = x | (x >> 8); | 16 x = x | (x >> 8); |
17 x = x | (x >> 16); | 17 x = x | (x >> 16); |
| 18 #if defined(ARCH_IS_64_BIT) |
| 19 x = x | (x >> 32); |
| 20 #endif // defined(ARCH_IS_64_BIT) |
18 return x + 1; | 21 return x + 1; |
19 } | 22 } |
20 | 23 |
21 | 24 |
22 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., | 25 // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
23 // figure 5-2, page 66, where the function is called pop. | 26 // figure 5-2, page 66, where the function is called pop. |
24 int Utils::CountOneBits(uint32_t x) { | 27 int Utils::CountOneBits(uint32_t x) { |
25 x = x - ((x >> 1) & 0x55555555); | 28 x = x - ((x >> 1) & 0x55555555); |
26 x = (x & 0x33333333) + ((x >> 2) & 0x33333333); | 29 x = (x & 0x33333333) + ((x >> 2) & 0x33333333); |
27 x = (x + (x >> 4)) & 0x0F0F0F0F; | 30 x = (x + (x >> 4)) & 0x0F0F0F0F; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 a = (a ^ 0xc761c23c) ^ (a >> 19); | 96 a = (a ^ 0xc761c23c) ^ (a >> 19); |
94 a = (a + 0x165667b1) + (a << 5); | 97 a = (a + 0x165667b1) + (a << 5); |
95 a = (a + 0xd3a2646c) ^ (a << 9); | 98 a = (a + 0xd3a2646c) ^ (a << 9); |
96 a = (a + 0xfd7046c5) + (a << 3); | 99 a = (a + 0xfd7046c5) + (a << 3); |
97 a = (a ^ 0xb55a4f09) ^ (a >> 16); | 100 a = (a ^ 0xb55a4f09) ^ (a >> 16); |
98 return static_cast<uint32_t>(a); | 101 return static_cast<uint32_t>(a); |
99 } | 102 } |
100 | 103 |
101 | 104 |
102 } // namespace dart | 105 } // namespace dart |
OLD | NEW |