OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_UTILS_H_ | 5 #ifndef V8_UTILS_H_ |
6 #define V8_UTILS_H_ | 6 #define V8_UTILS_H_ |
7 | 7 |
8 #include <limits.h> | 8 #include <limits.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 case 8: bits++; // Fall through. | 60 case 8: bits++; // Fall through. |
61 case 4: bits++; // Fall through. | 61 case 4: bits++; // Fall through. |
62 case 2: bits++; // Fall through. | 62 case 2: bits++; // Fall through. |
63 case 1: break; | 63 case 1: break; |
64 } | 64 } |
65 DCHECK_EQ(1 << bits, original_x); | 65 DCHECK_EQ(1 << bits, original_x); |
66 return bits; | 66 return bits; |
67 } | 67 } |
68 | 68 |
69 | 69 |
| 70 // X must be a power of 2. Returns the number of trailing zeros. |
| 71 inline int WhichPowerOf2_64(uint64_t x) { |
| 72 DCHECK(base::bits::IsPowerOfTwo64(x)); |
| 73 int bits = 0; |
| 74 #ifdef DEBUG |
| 75 uint64_t original_x = x; |
| 76 #endif |
| 77 if (x >= 0x100000000L) { |
| 78 bits += 32; |
| 79 x >>= 32; |
| 80 } |
| 81 if (x >= 0x10000) { |
| 82 bits += 16; |
| 83 x >>= 16; |
| 84 } |
| 85 if (x >= 0x100) { |
| 86 bits += 8; |
| 87 x >>= 8; |
| 88 } |
| 89 if (x >= 0x10) { |
| 90 bits += 4; |
| 91 x >>= 4; |
| 92 } |
| 93 switch (x) { |
| 94 default: UNREACHABLE(); |
| 95 case 8: bits++; // Fall through. |
| 96 case 4: bits++; // Fall through. |
| 97 case 2: bits++; // Fall through. |
| 98 case 1: break; |
| 99 } |
| 100 DCHECK_EQ(1L << bits, original_x); |
| 101 return bits; |
| 102 } |
| 103 |
| 104 |
70 inline int MostSignificantBit(uint32_t x) { | 105 inline int MostSignificantBit(uint32_t x) { |
71 static const int msb4[] = {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4}; | 106 static const int msb4[] = {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4}; |
72 int nibble = 0; | 107 int nibble = 0; |
73 if (x & 0xffff0000) { | 108 if (x & 0xffff0000) { |
74 nibble += 16; | 109 nibble += 16; |
75 x >>= 16; | 110 x >>= 16; |
76 } | 111 } |
77 if (x & 0xff00) { | 112 if (x & 0xff00) { |
78 nibble += 8; | 113 nibble += 8; |
79 x >>= 8; | 114 x >>= 8; |
(...skipping 1595 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1675 // Takes the address of the limit variable in order to find out where | 1710 // Takes the address of the limit variable in order to find out where |
1676 // the top of stack is right now. | 1711 // the top of stack is right now. |
1677 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit); | 1712 uintptr_t limit = reinterpret_cast<uintptr_t>(&limit); |
1678 return limit; | 1713 return limit; |
1679 } | 1714 } |
1680 | 1715 |
1681 } // namespace internal | 1716 } // namespace internal |
1682 } // namespace v8 | 1717 } // namespace v8 |
1683 | 1718 |
1684 #endif // V8_UTILS_H_ | 1719 #endif // V8_UTILS_H_ |
OLD | NEW |