blob: 2e6527b0548ec54f6640cc629d06c0a729959fe3 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008#include <stdint.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/base/macros.h"
10#if V8_CC_MSVC
11#include <intrin.h>
12#endif
13#if V8_OS_WIN32
14#include "src/base/win32-headers.h"
15#endif
16
17namespace v8 {
18namespace base {
Ben Murdochc5610432016-08-08 18:44:38 +010019
20namespace internal {
21template <typename T>
22class CheckedNumeric;
23}
24
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025namespace bits {
26
27// CountPopulation32(value) returns the number of bits set in |value|.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040028inline unsigned CountPopulation32(uint32_t value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029#if V8_HAS_BUILTIN_POPCOUNT
30 return __builtin_popcount(value);
31#else
32 value = ((value >> 1) & 0x55555555) + (value & 0x55555555);
33 value = ((value >> 2) & 0x33333333) + (value & 0x33333333);
34 value = ((value >> 4) & 0x0f0f0f0f) + (value & 0x0f0f0f0f);
35 value = ((value >> 8) & 0x00ff00ff) + (value & 0x00ff00ff);
36 value = ((value >> 16) & 0x0000ffff) + (value & 0x0000ffff);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040037 return static_cast<unsigned>(value);
38#endif
39}
40
41
42// CountPopulation64(value) returns the number of bits set in |value|.
43inline unsigned CountPopulation64(uint64_t value) {
44#if V8_HAS_BUILTIN_POPCOUNT
45 return __builtin_popcountll(value);
46#else
47 return CountPopulation32(static_cast<uint32_t>(value)) +
48 CountPopulation32(static_cast<uint32_t>(value >> 32));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049#endif
50}
51
52
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053// Overloaded versions of CountPopulation32/64.
54inline unsigned CountPopulation(uint32_t value) {
55 return CountPopulation32(value);
56}
57
58
59inline unsigned CountPopulation(uint64_t value) {
60 return CountPopulation64(value);
61}
62
63
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064// CountLeadingZeros32(value) returns the number of zero bits following the most
65// significant 1 bit in |value| if |value| is non-zero, otherwise it returns 32.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040066inline unsigned CountLeadingZeros32(uint32_t value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067#if V8_HAS_BUILTIN_CLZ
68 return value ? __builtin_clz(value) : 32;
69#elif V8_CC_MSVC
70 unsigned long result; // NOLINT(runtime/int)
71 if (!_BitScanReverse(&result, value)) return 32;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040072 return static_cast<unsigned>(31 - result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073#else
74 value = value | (value >> 1);
75 value = value | (value >> 2);
76 value = value | (value >> 4);
77 value = value | (value >> 8);
78 value = value | (value >> 16);
79 return CountPopulation32(~value);
80#endif
81}
82
83
Emily Bernierd0a1eb72015-03-24 16:35:39 -040084// CountLeadingZeros64(value) returns the number of zero bits following the most
85// significant 1 bit in |value| if |value| is non-zero, otherwise it returns 64.
86inline unsigned CountLeadingZeros64(uint64_t value) {
87#if V8_HAS_BUILTIN_CLZ
88 return value ? __builtin_clzll(value) : 64;
89#else
90 value = value | (value >> 1);
91 value = value | (value >> 2);
92 value = value | (value >> 4);
93 value = value | (value >> 8);
94 value = value | (value >> 16);
95 value = value | (value >> 32);
96 return CountPopulation64(~value);
97#endif
98}
99
100
Ben Murdoch097c5b22016-05-18 11:27:45 +0100101// ReverseBits(value) returns |value| in reverse bit order.
102template <typename T>
103T ReverseBits(T value) {
104 DCHECK((sizeof(value) == 1) || (sizeof(value) == 2) || (sizeof(value) == 4) ||
105 (sizeof(value) == 8));
106 T result = 0;
107 for (unsigned i = 0; i < (sizeof(value) * 8); i++) {
108 result = (result << 1) | (value & 1);
109 value >>= 1;
110 }
111 return result;
112}
113
114
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115// CountTrailingZeros32(value) returns the number of zero bits preceding the
116// least significant 1 bit in |value| if |value| is non-zero, otherwise it
117// returns 32.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400118inline unsigned CountTrailingZeros32(uint32_t value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119#if V8_HAS_BUILTIN_CTZ
120 return value ? __builtin_ctz(value) : 32;
121#elif V8_CC_MSVC
122 unsigned long result; // NOLINT(runtime/int)
123 if (!_BitScanForward(&result, value)) return 32;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400124 return static_cast<unsigned>(result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000125#else
126 if (value == 0) return 32;
127 unsigned count = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128 for (value ^= value - 1; value >>= 1; ++count) {
129 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130 return count;
131#endif
132}
133
134
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400135// CountTrailingZeros64(value) returns the number of zero bits preceding the
136// least significant 1 bit in |value| if |value| is non-zero, otherwise it
137// returns 64.
138inline unsigned CountTrailingZeros64(uint64_t value) {
139#if V8_HAS_BUILTIN_CTZ
140 return value ? __builtin_ctzll(value) : 64;
141#else
142 if (value == 0) return 64;
143 unsigned count = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000144 for (value ^= value - 1; value >>= 1; ++count) {
145 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400146 return count;
147#endif
148}
149
150
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151// Returns true iff |value| is a power of 2.
152inline bool IsPowerOfTwo32(uint32_t value) {
153 return value && !(value & (value - 1));
154}
155
156
157// Returns true iff |value| is a power of 2.
158inline bool IsPowerOfTwo64(uint64_t value) {
159 return value && !(value & (value - 1));
160}
161
162
163// RoundUpToPowerOfTwo32(value) returns the smallest power of two which is
164// greater than or equal to |value|. If you pass in a |value| that is already a
165// power of two, it is returned as is. |value| must be less than or equal to
166// 0x80000000u. Implementation is from "Hacker's Delight" by Henry S. Warren,
167// Jr., figure 3-3, page 48, where the function is called clp2.
168uint32_t RoundUpToPowerOfTwo32(uint32_t value);
169
170
171// RoundDownToPowerOfTwo32(value) returns the greatest power of two which is
172// less than or equal to |value|. If you pass in a |value| that is already a
173// power of two, it is returned as is.
174inline uint32_t RoundDownToPowerOfTwo32(uint32_t value) {
175 if (value > 0x80000000u) return 0x80000000u;
176 uint32_t result = RoundUpToPowerOfTwo32(value);
177 if (result > value) result >>= 1;
178 return result;
179}
180
181
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000182// Precondition: 0 <= shift < 32
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000183inline uint32_t RotateRight32(uint32_t value, uint32_t shift) {
184 if (shift == 0) return value;
185 return (value >> shift) | (value << (32 - shift));
186}
187
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000188// Precondition: 0 <= shift < 32
189inline uint32_t RotateLeft32(uint32_t value, uint32_t shift) {
190 if (shift == 0) return value;
191 return (value << shift) | (value >> (32 - shift));
192}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000193
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000194// Precondition: 0 <= shift < 64
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000195inline uint64_t RotateRight64(uint64_t value, uint64_t shift) {
196 if (shift == 0) return value;
197 return (value >> shift) | (value << (64 - shift));
198}
199
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000200// Precondition: 0 <= shift < 64
201inline uint64_t RotateLeft64(uint64_t value, uint64_t shift) {
202 if (shift == 0) return value;
203 return (value << shift) | (value >> (64 - shift));
204}
205
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000206
207// SignedAddOverflow32(lhs,rhs,val) performs a signed summation of |lhs| and
208// |rhs| and stores the result into the variable pointed to by |val| and
209// returns true if the signed summation resulted in an overflow.
210inline bool SignedAddOverflow32(int32_t lhs, int32_t rhs, int32_t* val) {
211#if V8_HAS_BUILTIN_SADD_OVERFLOW
212 return __builtin_sadd_overflow(lhs, rhs, val);
213#else
214 uint32_t res = static_cast<uint32_t>(lhs) + static_cast<uint32_t>(rhs);
215 *val = bit_cast<int32_t>(res);
216 return ((res ^ lhs) & (res ^ rhs) & (1U << 31)) != 0;
217#endif
218}
219
220
221// SignedSubOverflow32(lhs,rhs,val) performs a signed subtraction of |lhs| and
222// |rhs| and stores the result into the variable pointed to by |val| and
223// returns true if the signed subtraction resulted in an overflow.
224inline bool SignedSubOverflow32(int32_t lhs, int32_t rhs, int32_t* val) {
225#if V8_HAS_BUILTIN_SSUB_OVERFLOW
226 return __builtin_ssub_overflow(lhs, rhs, val);
227#else
228 uint32_t res = static_cast<uint32_t>(lhs) - static_cast<uint32_t>(rhs);
229 *val = bit_cast<int32_t>(res);
230 return ((res ^ lhs) & (res ^ ~rhs) & (1U << 31)) != 0;
231#endif
232}
233
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400234
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000235// SignedAddOverflow64(lhs,rhs,val) performs a signed summation of |lhs| and
236// |rhs| and stores the result into the variable pointed to by |val| and
237// returns true if the signed summation resulted in an overflow.
238inline bool SignedAddOverflow64(int64_t lhs, int64_t rhs, int64_t* val) {
239 uint64_t res = static_cast<uint64_t>(lhs) + static_cast<uint64_t>(rhs);
240 *val = bit_cast<int64_t>(res);
241 return ((res ^ lhs) & (res ^ rhs) & (1ULL << 63)) != 0;
242}
243
244
245// SignedSubOverflow64(lhs,rhs,val) performs a signed subtraction of |lhs| and
246// |rhs| and stores the result into the variable pointed to by |val| and
247// returns true if the signed subtraction resulted in an overflow.
248inline bool SignedSubOverflow64(int64_t lhs, int64_t rhs, int64_t* val) {
249 uint64_t res = static_cast<uint64_t>(lhs) - static_cast<uint64_t>(rhs);
250 *val = bit_cast<int64_t>(res);
251 return ((res ^ lhs) & (res ^ ~rhs) & (1ULL << 63)) != 0;
252}
253
254
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400255// SignedMulHigh32(lhs, rhs) multiplies two signed 32-bit values |lhs| and
256// |rhs|, extracts the most significant 32 bits of the result, and returns
257// those.
258int32_t SignedMulHigh32(int32_t lhs, int32_t rhs);
259
260
261// SignedMulHighAndAdd32(lhs, rhs, acc) multiplies two signed 32-bit values
262// |lhs| and |rhs|, extracts the most significant 32 bits of the result, and
263// adds the accumulate value |acc|.
264int32_t SignedMulHighAndAdd32(int32_t lhs, int32_t rhs, int32_t acc);
265
266
267// SignedDiv32(lhs, rhs) divides |lhs| by |rhs| and returns the quotient
268// truncated to int32. If |rhs| is zero, then zero is returned. If |lhs|
269// is minint and |rhs| is -1, it returns minint.
270int32_t SignedDiv32(int32_t lhs, int32_t rhs);
271
272
273// SignedMod32(lhs, rhs) divides |lhs| by |rhs| and returns the remainder
274// truncated to int32. If either |rhs| is zero or |lhs| is minint and |rhs|
275// is -1, it returns zero.
276int32_t SignedMod32(int32_t lhs, int32_t rhs);
277
278
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000279// UnsignedAddOverflow32(lhs,rhs,val) performs an unsigned summation of |lhs|
280// and |rhs| and stores the result into the variable pointed to by |val| and
281// returns true if the unsigned summation resulted in an overflow.
282inline bool UnsignedAddOverflow32(uint32_t lhs, uint32_t rhs, uint32_t* val) {
283#if V8_HAS_BUILTIN_SADD_OVERFLOW
284 return __builtin_uadd_overflow(lhs, rhs, val);
285#else
286 *val = lhs + rhs;
287 return *val < (lhs | rhs);
288#endif
289}
290
291
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400292// UnsignedDiv32(lhs, rhs) divides |lhs| by |rhs| and returns the quotient
293// truncated to uint32. If |rhs| is zero, then zero is returned.
294inline uint32_t UnsignedDiv32(uint32_t lhs, uint32_t rhs) {
295 return rhs ? lhs / rhs : 0u;
296}
297
298
299// UnsignedMod32(lhs, rhs) divides |lhs| by |rhs| and returns the remainder
300// truncated to uint32. If |rhs| is zero, then zero is returned.
301inline uint32_t UnsignedMod32(uint32_t lhs, uint32_t rhs) {
302 return rhs ? lhs % rhs : 0u;
303}
304
Ben Murdochc5610432016-08-08 18:44:38 +0100305
306// Clamp |value| on overflow and underflow conditions.
307int64_t FromCheckedNumeric(const internal::CheckedNumeric<int64_t> value);
308
309
310// SignedSaturatedAdd64(lhs, rhs) adds |lhs| and |rhs|,
311// checks and returns the result.
312int64_t SignedSaturatedAdd64(int64_t lhs, int64_t rhs);
313
314
315// SignedSaturatedSub64(lhs, rhs) substracts |lhs| by |rhs|,
316// checks and returns the result.
317int64_t SignedSaturatedSub64(int64_t lhs, int64_t rhs);
318
319
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000320} // namespace bits
321} // namespace base
322} // namespace v8
323
324#endif // V8_BASE_BITS_H_