OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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_BASE_BITS_H_ | 5 #ifndef V8_BASE_BITS_H_ |
6 #define V8_BASE_BITS_H_ | 6 #define V8_BASE_BITS_H_ |
7 | 7 |
8 #include "include/v8stdint.h" | 8 #include "include/v8stdint.h" |
9 #if V8_CC_MSVC | 9 #if V8_CC_MSVC |
10 #include <intrin.h> | 10 #include <intrin.h> |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 #else | 65 #else |
66 if (value == 0) return 32; | 66 if (value == 0) return 32; |
67 unsigned count = 0; | 67 unsigned count = 0; |
68 for (value ^= value - 1; value >>= 1; ++count) | 68 for (value ^= value - 1; value >>= 1; ++count) |
69 ; | 69 ; |
70 return count; | 70 return count; |
71 #endif | 71 #endif |
72 } | 72 } |
73 | 73 |
74 | 74 |
| 75 // Returns true iff |value| is a power of 2. |
| 76 inline bool IsPowerOfTwo32(uint32_t value) { |
| 77 return value && !(value & (value - 1)); |
| 78 } |
| 79 |
| 80 |
| 81 // Returns true iff |value| is a power of 2. |
| 82 inline bool IsPowerOfTwo64(uint64_t value) { |
| 83 return value && !(value & (value - 1)); |
| 84 } |
| 85 |
| 86 |
| 87 // RoundUpToPowerOfTwo32(value) returns the smallest power of two which is |
| 88 // greater than or equal to |value|. If you pass in a |value| that is already a |
| 89 // power of two, it is returned as is. |value| must be less than or equal to |
| 90 // 0x80000000u. Implementation is from "Hacker's Delight" by Henry S. Warren, |
| 91 // Jr., figure 3-3, page 48, where the function is called clp2. |
| 92 uint32_t RoundUpToPowerOfTwo32(uint32_t value); |
| 93 |
| 94 |
| 95 // RoundDownToPowerOfTwo32(value) returns the greatest power of two which is |
| 96 // less than or equal to |value|. If you pass in a |value| that is already a |
| 97 // power of two, it is returned as is. |
| 98 inline uint32_t RoundDownToPowerOfTwo32(uint32_t value) { |
| 99 if (value > 0x80000000u) return 0x80000000u; |
| 100 uint32_t result = RoundUpToPowerOfTwo32(value); |
| 101 if (result > value) result >>= 1; |
| 102 return result; |
| 103 } |
| 104 |
| 105 |
75 inline uint32_t RotateRight32(uint32_t value, uint32_t shift) { | 106 inline uint32_t RotateRight32(uint32_t value, uint32_t shift) { |
76 if (shift == 0) return value; | 107 if (shift == 0) return value; |
77 return (value >> shift) | (value << (32 - shift)); | 108 return (value >> shift) | (value << (32 - shift)); |
78 } | 109 } |
79 | 110 |
80 | 111 |
81 inline uint64_t RotateRight64(uint64_t value, uint64_t shift) { | 112 inline uint64_t RotateRight64(uint64_t value, uint64_t shift) { |
82 if (shift == 0) return value; | 113 if (shift == 0) return value; |
83 return (value >> shift) | (value << (64 - shift)); | 114 return (value >> shift) | (value << (64 - shift)); |
84 } | 115 } |
85 | 116 |
86 } // namespace bits | 117 } // namespace bits |
87 } // namespace base | 118 } // namespace base |
88 } // namespace v8 | 119 } // namespace v8 |
89 | 120 |
90 #endif // V8_BASE_BITS_H_ | 121 #endif // V8_BASE_BITS_H_ |
OLD | NEW |