Line data Source code
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
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_BASE_BITS_H_
6 : #define V8_BASE_BITS_H_
7 :
8 : #include <stdint.h>
9 : #include <type_traits>
10 :
11 : #include "src/base/base-export.h"
12 : #include "src/base/macros.h"
13 : #if V8_CC_MSVC
14 : #include <intrin.h>
15 : #endif
16 : #if V8_OS_WIN32
17 : #include "src/base/win32-headers.h"
18 : #endif
19 :
20 : namespace v8 {
21 : namespace base {
22 : namespace bits {
23 :
24 : // CountPopulation(value) returns the number of bits set in |value|.
25 : template <typename T>
26 : constexpr inline
27 : typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) <= 8,
28 : unsigned>::type
29 : CountPopulation(T value) {
30 : #if V8_HAS_BUILTIN_POPCOUNT
31 359689 : return sizeof(T) == 8 ? __builtin_popcountll(static_cast<uint64_t>(value))
32 4631699 : : __builtin_popcount(static_cast<uint32_t>(value));
33 : #else
34 : constexpr uint64_t mask[] = {0x5555555555555555, 0x3333333333333333,
35 : 0x0f0f0f0f0f0f0f0f, 0x00ff00ff00ff00ff,
36 : 0x0000ffff0000ffff, 0x00000000ffffffff};
37 : value = ((value >> 1) & mask[0]) + (value & mask[0]);
38 : value = ((value >> 2) & mask[1]) + (value & mask[1]);
39 : value = ((value >> 4) & mask[2]) + (value & mask[2]);
40 : if (sizeof(T) > 1)
41 : value = ((value >> (sizeof(T) > 1 ? 8 : 0)) & mask[3]) + (value & mask[3]);
42 : if (sizeof(T) > 2)
43 : value = ((value >> (sizeof(T) > 2 ? 16 : 0)) & mask[4]) + (value & mask[4]);
44 : if (sizeof(T) > 4)
45 : value = ((value >> (sizeof(T) > 4 ? 32 : 0)) & mask[5]) + (value & mask[5]);
46 : return static_cast<unsigned>(value);
47 : #endif
48 : }
49 :
50 : // ReverseBits(value) returns |value| in reverse bit order.
51 : template <typename T>
52 : T ReverseBits(T value) {
53 : DCHECK((sizeof(value) == 1) || (sizeof(value) == 2) || (sizeof(value) == 4) ||
54 : (sizeof(value) == 8));
55 : T result = 0;
56 : for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
57 : result = (result << 1) | (value & 1);
58 : value >>= 1;
59 : }
60 : return result;
61 : }
62 :
63 : // CountLeadingZeros(value) returns the number of zero bits following the most
64 : // significant 1 bit in |value| if |value| is non-zero, otherwise it returns
65 : // {sizeof(T) * 8}.
66 : template <typename T, unsigned bits = sizeof(T) * 8>
67 : inline constexpr
68 : typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) <= 8,
69 : unsigned>::type
70 : CountLeadingZeros(T value) {
71 : static_assert(bits > 0, "invalid instantiation");
72 : #if V8_HAS_BUILTIN_CLZ
73 : return value == 0
74 : ? bits
75 : : bits == 64
76 1042948 : ? __builtin_clzll(static_cast<uint64_t>(value))
77 12715150 : : __builtin_clz(static_cast<uint32_t>(value)) - (32 - bits);
78 : #else
79 : // Binary search algorithm taken from "Hacker's Delight" (by Henry S. Warren,
80 : // Jr.), figures 5-11 and 5-12.
81 : if (bits == 1) return static_cast<unsigned>(value) ^ 1;
82 : T upper_half = value >> (bits / 2);
83 : T next_value = upper_half != 0 ? upper_half : value;
84 : unsigned add = upper_half != 0 ? 0 : bits / 2;
85 : constexpr unsigned next_bits = bits == 1 ? 1 : bits / 2;
86 : return CountLeadingZeros<T, next_bits>(next_value) + add;
87 : #endif
88 : }
89 :
90 : inline constexpr unsigned CountLeadingZeros32(uint32_t value) {
91 : return CountLeadingZeros(value);
92 : }
93 : inline constexpr unsigned CountLeadingZeros64(uint64_t value) {
94 : return CountLeadingZeros(value);
95 : }
96 :
97 : // CountTrailingZeros(value) returns the number of zero bits preceding the
98 : // least significant 1 bit in |value| if |value| is non-zero, otherwise it
99 : // returns {sizeof(T) * 8}.
100 : template <typename T, unsigned bits = sizeof(T) * 8>
101 : inline constexpr
102 : typename std::enable_if<std::is_integral<T>::value && sizeof(T) <= 8,
103 : unsigned>::type
104 : CountTrailingZeros(T value) {
105 : #if V8_HAS_BUILTIN_CTZ
106 : return value == 0 ? bits
107 359915 : : bits == 64 ? __builtin_ctzll(static_cast<uint64_t>(value))
108 696457185 : : __builtin_ctz(static_cast<uint32_t>(value));
109 : #else
110 : // Fall back to popcount (see "Hacker's Delight" by Henry S. Warren, Jr.),
111 : // chapter 5-4. On x64, since is faster than counting in a loop and faster
112 : // than doing binary search.
113 : using U = typename std::make_unsigned<T>::type;
114 : U u = value;
115 : return CountPopulation(static_cast<U>(~u & (u - 1u)));
116 : #endif
117 : }
118 :
119 : inline constexpr unsigned CountTrailingZeros32(uint32_t value) {
120 : return CountTrailingZeros(value);
121 : }
122 : inline constexpr unsigned CountTrailingZeros64(uint64_t value) {
123 : return CountTrailingZeros(value);
124 : }
125 :
126 : // Returns true iff |value| is a power of 2.
127 : template <typename T,
128 : typename = typename std::enable_if<std::is_integral<T>::value ||
129 : std::is_enum<T>::value>::type>
130 : constexpr inline bool IsPowerOfTwo(T value) {
131 63470755 : return value > 0 && (value & (value - 1)) == 0;
132 : }
133 :
134 : // RoundUpToPowerOfTwo32(value) returns the smallest power of two which is
135 : // greater than or equal to |value|. If you pass in a |value| that is already a
136 : // power of two, it is returned as is. |value| must be less than or equal to
137 : // 0x80000000u. Uses computation based on leading zeros if we have compiler
138 : // support for that. Falls back to the implementation from "Hacker's Delight" by
139 : // Henry S. Warren, Jr., figure 3-3, page 48, where the function is called clp2.
140 : V8_BASE_EXPORT uint32_t RoundUpToPowerOfTwo32(uint32_t value);
141 : // Same for 64 bit integers. |value| must be <= 2^63
142 : V8_BASE_EXPORT uint64_t RoundUpToPowerOfTwo64(uint64_t value);
143 : // Same for size_t integers.
144 : inline size_t RoundUpToPowerOfTwo(size_t value) {
145 : if (sizeof(size_t) == sizeof(uint64_t)) {
146 13146 : return RoundUpToPowerOfTwo64(value);
147 : } else {
148 : return RoundUpToPowerOfTwo32(value);
149 : }
150 : }
151 :
152 : // RoundDownToPowerOfTwo32(value) returns the greatest power of two which is
153 : // less than or equal to |value|. If you pass in a |value| that is already a
154 : // power of two, it is returned as is.
155 : inline uint32_t RoundDownToPowerOfTwo32(uint32_t value) {
156 32 : if (value > 0x80000000u) return 0x80000000u;
157 34 : uint32_t result = RoundUpToPowerOfTwo32(value);
158 34 : if (result > value) result >>= 1;
159 : return result;
160 : }
161 :
162 :
163 : // Precondition: 0 <= shift < 32
164 : inline uint32_t RotateRight32(uint32_t value, uint32_t shift) {
165 47575 : if (shift == 0) return value;
166 46128 : return (value >> shift) | (value << (32 - shift));
167 : }
168 :
169 : // Precondition: 0 <= shift < 32
170 : inline uint32_t RotateLeft32(uint32_t value, uint32_t shift) {
171 816 : if (shift == 0) return value;
172 780 : return (value << shift) | (value >> (32 - shift));
173 : }
174 :
175 : // Precondition: 0 <= shift < 64
176 : inline uint64_t RotateRight64(uint64_t value, uint64_t shift) {
177 78732 : if (shift == 0) return value;
178 59292 : return (value >> shift) | (value << (64 - shift));
179 : }
180 :
181 : // Precondition: 0 <= shift < 64
182 : inline uint64_t RotateLeft64(uint64_t value, uint64_t shift) {
183 79752 : if (shift == 0) return value;
184 60264 : return (value << shift) | (value >> (64 - shift));
185 : }
186 :
187 :
188 : // SignedAddOverflow32(lhs,rhs,val) performs a signed summation of |lhs| and
189 : // |rhs| and stores the result into the variable pointed to by |val| and
190 : // returns true if the signed summation resulted in an overflow.
191 : inline bool SignedAddOverflow32(int32_t lhs, int32_t rhs, int32_t* val) {
192 : #if V8_HAS_BUILTIN_SADD_OVERFLOW
193 : return __builtin_sadd_overflow(lhs, rhs, val);
194 : #else
195 40786669 : uint32_t res = static_cast<uint32_t>(lhs) + static_cast<uint32_t>(rhs);
196 1278 : *val = bit_cast<int32_t>(res);
197 40786669 : return ((res ^ lhs) & (res ^ rhs) & (1U << 31)) != 0;
198 : #endif
199 : }
200 :
201 :
202 : // SignedSubOverflow32(lhs,rhs,val) performs a signed subtraction of |lhs| and
203 : // |rhs| and stores the result into the variable pointed to by |val| and
204 : // returns true if the signed subtraction resulted in an overflow.
205 : inline bool SignedSubOverflow32(int32_t lhs, int32_t rhs, int32_t* val) {
206 : #if V8_HAS_BUILTIN_SSUB_OVERFLOW
207 : return __builtin_ssub_overflow(lhs, rhs, val);
208 : #else
209 98611 : uint32_t res = static_cast<uint32_t>(lhs) - static_cast<uint32_t>(rhs);
210 1277 : *val = bit_cast<int32_t>(res);
211 98611 : return ((res ^ lhs) & (res ^ ~rhs) & (1U << 31)) != 0;
212 : #endif
213 : }
214 :
215 : // SignedMulOverflow32(lhs,rhs,val) performs a signed multiplication of |lhs|
216 : // and |rhs| and stores the result into the variable pointed to by |val| and
217 : // returns true if the signed multiplication resulted in an overflow.
218 : V8_BASE_EXPORT bool SignedMulOverflow32(int32_t lhs, int32_t rhs, int32_t* val);
219 :
220 : // SignedAddOverflow64(lhs,rhs,val) performs a signed summation of |lhs| and
221 : // |rhs| and stores the result into the variable pointed to by |val| and
222 : // returns true if the signed summation resulted in an overflow.
223 : inline bool SignedAddOverflow64(int64_t lhs, int64_t rhs, int64_t* val) {
224 131220 : uint64_t res = static_cast<uint64_t>(lhs) + static_cast<uint64_t>(rhs);
225 : *val = bit_cast<int64_t>(res);
226 131220 : return ((res ^ lhs) & (res ^ rhs) & (1ULL << 63)) != 0;
227 : }
228 :
229 :
230 : // SignedSubOverflow64(lhs,rhs,val) performs a signed subtraction of |lhs| and
231 : // |rhs| and stores the result into the variable pointed to by |val| and
232 : // returns true if the signed subtraction resulted in an overflow.
233 : inline bool SignedSubOverflow64(int64_t lhs, int64_t rhs, int64_t* val) {
234 131220 : uint64_t res = static_cast<uint64_t>(lhs) - static_cast<uint64_t>(rhs);
235 : *val = bit_cast<int64_t>(res);
236 131220 : return ((res ^ lhs) & (res ^ ~rhs) & (1ULL << 63)) != 0;
237 : }
238 :
239 : // SignedMulHigh32(lhs, rhs) multiplies two signed 32-bit values |lhs| and
240 : // |rhs|, extracts the most significant 32 bits of the result, and returns
241 : // those.
242 : V8_BASE_EXPORT int32_t SignedMulHigh32(int32_t lhs, int32_t rhs);
243 :
244 : // SignedMulHighAndAdd32(lhs, rhs, acc) multiplies two signed 32-bit values
245 : // |lhs| and |rhs|, extracts the most significant 32 bits of the result, and
246 : // adds the accumulate value |acc|.
247 : V8_BASE_EXPORT int32_t SignedMulHighAndAdd32(int32_t lhs, int32_t rhs,
248 : int32_t acc);
249 :
250 : // SignedDiv32(lhs, rhs) divides |lhs| by |rhs| and returns the quotient
251 : // truncated to int32. If |rhs| is zero, then zero is returned. If |lhs|
252 : // is minint and |rhs| is -1, it returns minint.
253 : V8_BASE_EXPORT int32_t SignedDiv32(int32_t lhs, int32_t rhs);
254 :
255 : // SignedMod32(lhs, rhs) divides |lhs| by |rhs| and returns the remainder
256 : // truncated to int32. If either |rhs| is zero or |lhs| is minint and |rhs|
257 : // is -1, it returns zero.
258 : V8_BASE_EXPORT int32_t SignedMod32(int32_t lhs, int32_t rhs);
259 :
260 : // UnsignedAddOverflow32(lhs,rhs,val) performs an unsigned summation of |lhs|
261 : // and |rhs| and stores the result into the variable pointed to by |val| and
262 : // returns true if the unsigned summation resulted in an overflow.
263 : inline bool UnsignedAddOverflow32(uint32_t lhs, uint32_t rhs, uint32_t* val) {
264 : #if V8_HAS_BUILTIN_SADD_OVERFLOW
265 : return __builtin_uadd_overflow(lhs, rhs, val);
266 : #else
267 150953053 : *val = lhs + rhs;
268 150953051 : return *val < (lhs | rhs);
269 : #endif
270 : }
271 :
272 :
273 : // UnsignedDiv32(lhs, rhs) divides |lhs| by |rhs| and returns the quotient
274 : // truncated to uint32. If |rhs| is zero, then zero is returned.
275 : inline uint32_t UnsignedDiv32(uint32_t lhs, uint32_t rhs) {
276 28091 : return rhs ? lhs / rhs : 0u;
277 : }
278 :
279 :
280 : // UnsignedMod32(lhs, rhs) divides |lhs| by |rhs| and returns the remainder
281 : // truncated to uint32. If |rhs| is zero, then zero is returned.
282 : inline uint32_t UnsignedMod32(uint32_t lhs, uint32_t rhs) {
283 28140 : return rhs ? lhs % rhs : 0u;
284 : }
285 :
286 :
287 : // SignedSaturatedAdd64(lhs, rhs) adds |lhs| and |rhs|,
288 : // checks and returns the result.
289 : V8_BASE_EXPORT int64_t SignedSaturatedAdd64(int64_t lhs, int64_t rhs);
290 :
291 : // SignedSaturatedSub64(lhs, rhs) subtracts |lhs| by |rhs|,
292 : // checks and returns the result.
293 : V8_BASE_EXPORT int64_t SignedSaturatedSub64(int64_t lhs, int64_t rhs);
294 :
295 : } // namespace bits
296 : } // namespace base
297 : } // namespace v8
298 :
299 : #endif // V8_BASE_BITS_H_
|