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 16 matching lines...) Expand all Loading... |
27 int Utils::CountOneBits(uint32_t x) { | 27 int Utils::CountOneBits(uint32_t x) { |
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 // TODO(koda): Compare to flsll call/intrinsic. |
37 int Utils::HighestBit(int64_t v) { | 38 int Utils::HighestBit(int64_t v) { |
38 uint64_t x = static_cast<uint64_t>((v > 0) ? v : -v); | 39 uint64_t x = static_cast<uint64_t>((v > 0) ? v : -v); |
39 uint64_t t; | 40 uint64_t t; |
40 int r = 0; | 41 int r = 0; |
41 if ((t = x >> 32) != 0) { x = t; r += 32; } | 42 if ((t = x >> 32) != 0) { x = t; r += 32; } |
42 if ((t = x >> 16) != 0) { x = t; r += 16; } | 43 if ((t = x >> 16) != 0) { x = t; r += 16; } |
43 if ((t = x >> 8) != 0) { x = t; r += 8; } | 44 if ((t = x >> 8) != 0) { x = t; r += 8; } |
44 if ((t = x >> 4) != 0) { x = t; r += 4; } | 45 if ((t = x >> 4) != 0) { x = t; r += 4; } |
45 if ((t = x >> 2) != 0) { x = t; r += 2; } | 46 if ((t = x >> 2) != 0) { x = t; r += 2; } |
46 if (x > 1) r += 1; | 47 if (x > 1) r += 1; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 a = (a ^ 0xc761c23c) ^ (a >> 19); | 101 a = (a ^ 0xc761c23c) ^ (a >> 19); |
101 a = (a + 0x165667b1) + (a << 5); | 102 a = (a + 0x165667b1) + (a << 5); |
102 a = (a + 0xd3a2646c) ^ (a << 9); | 103 a = (a + 0xd3a2646c) ^ (a << 9); |
103 a = (a + 0xfd7046c5) + (a << 3); | 104 a = (a + 0xfd7046c5) + (a << 3); |
104 a = (a ^ 0xb55a4f09) ^ (a >> 16); | 105 a = (a ^ 0xb55a4f09) ^ (a >> 16); |
105 return static_cast<uint32_t>(a); | 106 return static_cast<uint32_t>(a); |
106 } | 107 } |
107 | 108 |
108 | 109 |
109 } // namespace dart | 110 } // namespace dart |
OLD | NEW |