V8 Project
v8::base::bits Namespace Reference

Functions

 TEST (Bits, CountPopulation32)
 
 TEST (Bits, CountLeadingZeros32)
 
 TEST (Bits, CountTrailingZeros32)
 
 TEST (Bits, IsPowerOfTwo32)
 
 TEST (Bits, IsPowerOfTwo64)
 
 TEST (Bits, RoundUpToPowerOfTwo32)
 
 TEST (BitsDeathTest, DISABLE_IN_RELEASE(RoundUpToPowerOfTwo32))
 
 TEST (Bits, RoundDownToPowerOfTwo32)
 
 TEST (Bits, RotateRight32)
 
 TEST (Bits, RotateRight64)
 
 TEST (Bits, SignedAddOverflow32)
 
 TEST (Bits, SignedSubOverflow32)
 
uint32_t RoundUpToPowerOfTwo32 (uint32_t value)
 
uint32_t CountPopulation32 (uint32_t value)
 
uint32_t CountLeadingZeros32 (uint32_t value)
 
uint32_t CountTrailingZeros32 (uint32_t value)
 
bool IsPowerOfTwo32 (uint32_t value)
 
bool IsPowerOfTwo64 (uint64_t value)
 
uint32_t RoundDownToPowerOfTwo32 (uint32_t value)
 
uint32_t RotateRight32 (uint32_t value, uint32_t shift)
 
uint64_t RotateRight64 (uint64_t value, uint64_t shift)
 
bool SignedAddOverflow32 (int32_t lhs, int32_t rhs, int32_t *val)
 
bool SignedSubOverflow32 (int32_t lhs, int32_t rhs, int32_t *val)
 

Function Documentation

◆ CountLeadingZeros32()

uint32_t v8::base::bits::CountLeadingZeros32 ( uint32_t  value)
inline

Definition at line 38 of file bits.h.

38  {
39 #if V8_HAS_BUILTIN_CLZ
40  return value ? __builtin_clz(value) : 32;
41 #elif V8_CC_MSVC
42  unsigned long result; // NOLINT(runtime/int)
43  if (!_BitScanReverse(&result, value)) return 32;
44  return static_cast<uint32_t>(31 - result);
45 #else
46  value = value | (value >> 1);
47  value = value | (value >> 2);
48  value = value | (value >> 4);
49  value = value | (value >> 8);
50  value = value | (value >> 16);
51  return CountPopulation32(~value);
52 #endif
53 }
uint32_t CountPopulation32(uint32_t value)
Definition: bits.h:22

References CountPopulation32().

Referenced by TEST().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ CountPopulation32()

uint32_t v8::base::bits::CountPopulation32 ( uint32_t  value)
inline

Definition at line 22 of file bits.h.

22  {
23 #if V8_HAS_BUILTIN_POPCOUNT
24  return __builtin_popcount(value);
25 #else
26  value = ((value >> 1) & 0x55555555) + (value & 0x55555555);
27  value = ((value >> 2) & 0x33333333) + (value & 0x33333333);
28  value = ((value >> 4) & 0x0f0f0f0f) + (value & 0x0f0f0f0f);
29  value = ((value >> 8) & 0x00ff00ff) + (value & 0x00ff00ff);
30  value = ((value >> 16) & 0x0000ffff) + (value & 0x0000ffff);
31  return value;
32 #endif
33 }

Referenced by v8::internal::BitVector::Count(), CountLeadingZeros32(), v8::internal::NumRegs(), and TEST().

+ Here is the caller graph for this function:

◆ CountTrailingZeros32()

uint32_t v8::base::bits::CountTrailingZeros32 ( uint32_t  value)
inline

Definition at line 59 of file bits.h.

59  {
60 #if V8_HAS_BUILTIN_CTZ
61  return value ? __builtin_ctz(value) : 32;
62 #elif V8_CC_MSVC
63  unsigned long result; // NOLINT(runtime/int)
64  if (!_BitScanForward(&result, value)) return 32;
65  return static_cast<uint32_t>(result);
66 #else
67  if (value == 0) return 32;
68  unsigned count = 0;
69  for (value ^= value - 1; value >>= 1; ++count)
70  ;
71  return count;
72 #endif
73 }

Referenced by v8::internal::MarkCompactCollector::DiscoverAndEvacuateBlackObjectsOnPage(), v8::internal::DiscoverGreyObjectsOnPage(), and TEST().

+ Here is the caller graph for this function:

◆ IsPowerOfTwo32()

◆ IsPowerOfTwo64()

bool v8::base::bits::IsPowerOfTwo64 ( uint64_t  value)
inline

Definition at line 83 of file bits.h.

83  {
84  return value && !(value & (value - 1));
85 }

Referenced by v8::internal::MacroAssembler::Claim(), v8::internal::MacroAssembler::ClaimBySMI(), v8::internal::MacroAssembler::Drop(), v8::internal::MacroAssembler::DropBySMI(), and TEST().

+ Here is the caller graph for this function:

◆ RotateRight32()

uint32_t v8::base::bits::RotateRight32 ( uint32_t  value,
uint32_t  shift 
)
inline

Definition at line 107 of file bits.h.

107  {
108  if (shift == 0) return value;
109  return (value >> shift) | (value << (32 - shift));
110 }
enable harmony numeric enable harmony object literal extensions Optimize object Array shift

References shift.

Referenced by TEST(), and v8::internal::compiler::TEST_F().

+ Here is the caller graph for this function:

◆ RotateRight64()

uint64_t v8::base::bits::RotateRight64 ( uint64_t  value,
uint64_t  shift 
)
inline

Definition at line 113 of file bits.h.

113  {
114  if (shift == 0) return value;
115  return (value >> shift) | (value << (64 - shift));
116 }

References shift.

Referenced by TEST().

+ Here is the caller graph for this function:

◆ RoundDownToPowerOfTwo32()

uint32_t v8::base::bits::RoundDownToPowerOfTwo32 ( uint32_t  value)
inline

Definition at line 99 of file bits.h.

99  {
100  if (value > 0x80000000u) return 0x80000000u;
101  uint32_t result = RoundUpToPowerOfTwo32(value);
102  if (result > value) result >>= 1;
103  return result;
104 }
uint32_t RoundUpToPowerOfTwo32(uint32_t value)
Definition: bits.cc:12

References RoundUpToPowerOfTwo32().

Referenced by v8::internal::MarkingDeque::Initialize(), and TEST().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RoundUpToPowerOfTwo32()

uint32_t v8::base::bits::RoundUpToPowerOfTwo32 ( uint32_t  value)

Definition at line 12 of file bits.cc.

12  {
13  DCHECK_LE(value, 0x80000000u);
14  value = value - 1;
15  value = value | (value >> 1);
16  value = value | (value >> 2);
17  value = value | (value >> 4);
18  value = value | (value >> 8);
19  value = value | (value >> 16);
20  return value + 1;
21 }
#define DCHECK_LE(v1, v2)
Definition: logging.h:210

References DCHECK_LE.

Referenced by v8::internal::OrderedHashTable< Derived, Iterator, entrysize >::Allocate(), v8::internal::HashTable< Derived, Shape, Key >::ComputeCapacity(), v8::internal::Heap::ConfigureHeap(), v8::base::GetPageSize(), RoundDownToPowerOfTwo32(), and TEST().

+ Here is the caller graph for this function:

◆ SignedAddOverflow32()

bool v8::base::bits::SignedAddOverflow32 ( int32_t  lhs,
int32_t  rhs,
int32_t *  val 
)
inline

Definition at line 122 of file bits.h.

122  {
123 #if V8_HAS_BUILTIN_SADD_OVERFLOW
124  return __builtin_sadd_overflow(lhs, rhs, val);
125 #else
126  uint32_t res = static_cast<uint32_t>(lhs) + static_cast<uint32_t>(rhs);
127  *val = bit_cast<int32_t>(res);
128  return ((res ^ lhs) & (res ^ rhs) & (1U << 31)) != 0;
129 #endif
130 }

Referenced by TEST(), and v8::internal::compiler::TEST_F().

+ Here is the caller graph for this function:

◆ SignedSubOverflow32()

bool v8::base::bits::SignedSubOverflow32 ( int32_t  lhs,
int32_t  rhs,
int32_t *  val 
)
inline

Definition at line 136 of file bits.h.

136  {
137 #if V8_HAS_BUILTIN_SSUB_OVERFLOW
138  return __builtin_ssub_overflow(lhs, rhs, val);
139 #else
140  uint32_t res = static_cast<uint32_t>(lhs) - static_cast<uint32_t>(rhs);
141  *val = bit_cast<int32_t>(res);
142  return ((res ^ lhs) & (res ^ ~rhs) & (1U << 31)) != 0;
143 #endif
144 }

Referenced by TEST(), and v8::internal::compiler::TEST_F().

+ Here is the caller graph for this function:

◆ TEST() [1/12]

v8::base::bits::TEST ( Bits  ,
CountLeadingZeros32   
)

Definition at line 31 of file bits-unittest.cc.

31  {
32  EXPECT_EQ(32u, CountLeadingZeros32(0));
33  EXPECT_EQ(31u, CountLeadingZeros32(1));
34  TRACED_FORRANGE(uint32_t, shift, 0, 31) {
35  EXPECT_EQ(31u - shift, CountLeadingZeros32(1u << shift));
36  }
37  EXPECT_EQ(4u, CountLeadingZeros32(0x0f0f0f0f));
38 }
uint32_t CountLeadingZeros32(uint32_t value)
Definition: bits.h:38

References CountLeadingZeros32(), and shift.

+ Here is the call graph for this function:

◆ TEST() [2/12]

v8::base::bits::TEST ( Bits  ,
CountPopulation32   
)

Definition at line 21 of file bits-unittest.cc.

21  {
22  EXPECT_EQ(0u, CountPopulation32(0));
23  EXPECT_EQ(1u, CountPopulation32(1));
24  EXPECT_EQ(8u, CountPopulation32(0x11111111));
25  EXPECT_EQ(16u, CountPopulation32(0xf0f0f0f0));
26  EXPECT_EQ(24u, CountPopulation32(0xfff0f0ff));
27  EXPECT_EQ(32u, CountPopulation32(0xffffffff));
28 }

References CountPopulation32().

+ Here is the call graph for this function:

◆ TEST() [3/12]

v8::base::bits::TEST ( Bits  ,
CountTrailingZeros32   
)

Definition at line 41 of file bits-unittest.cc.

41  {
42  EXPECT_EQ(32u, CountTrailingZeros32(0));
43  EXPECT_EQ(31u, CountTrailingZeros32(0x80000000));
44  TRACED_FORRANGE(uint32_t, shift, 0, 31) {
45  EXPECT_EQ(shift, CountTrailingZeros32(1u << shift));
46  }
47  EXPECT_EQ(4u, CountTrailingZeros32(0xf0f0f0f0));
48 }
uint32_t CountTrailingZeros32(uint32_t value)
Definition: bits.h:59

References CountTrailingZeros32(), and shift.

+ Here is the call graph for this function:

◆ TEST() [4/12]

v8::base::bits::TEST ( Bits  ,
IsPowerOfTwo32   
)

Definition at line 51 of file bits-unittest.cc.

51  {
52  EXPECT_FALSE(IsPowerOfTwo32(0U));
53  TRACED_FORRANGE(uint32_t, shift, 0, 31) {
54  EXPECT_TRUE(IsPowerOfTwo32(1U << shift));
55  EXPECT_FALSE(IsPowerOfTwo32((1U << shift) + 5U));
56  EXPECT_FALSE(IsPowerOfTwo32(~(1U << shift)));
57  }
58  TRACED_FORRANGE(uint32_t, shift, 2, 31) {
59  EXPECT_FALSE(IsPowerOfTwo32((1U << shift) - 1U));
60  }
61  EXPECT_FALSE(IsPowerOfTwo32(0xffffffff));
62 }
bool IsPowerOfTwo32(uint32_t value)
Definition: bits.h:77
#define U(name)
Definition: runtime.cc:9020

References IsPowerOfTwo32(), shift, and U.

+ Here is the call graph for this function:

◆ TEST() [5/12]

v8::base::bits::TEST ( Bits  ,
IsPowerOfTwo64   
)

Definition at line 65 of file bits-unittest.cc.

65  {
66  EXPECT_FALSE(IsPowerOfTwo64(0U));
67  TRACED_FORRANGE(uint32_t, shift, 0, 63) {
68  EXPECT_TRUE(IsPowerOfTwo64(V8_UINT64_C(1) << shift));
69  EXPECT_FALSE(IsPowerOfTwo64((V8_UINT64_C(1) << shift) + 5U));
70  EXPECT_FALSE(IsPowerOfTwo64(~(V8_UINT64_C(1) << shift)));
71  }
72  TRACED_FORRANGE(uint32_t, shift, 2, 63) {
73  EXPECT_FALSE(IsPowerOfTwo64((V8_UINT64_C(1) << shift) - 1U));
74  }
75  EXPECT_FALSE(IsPowerOfTwo64(V8_UINT64_C(0xffffffffffffffff)));
76 }
#define V8_UINT64_C(x)
Definition: macros.h:357
bool IsPowerOfTwo64(uint64_t value)
Definition: bits.h:83

References IsPowerOfTwo64(), shift, U, and V8_UINT64_C.

+ Here is the call graph for this function:

◆ TEST() [6/12]

v8::base::bits::TEST ( Bits  ,
RotateRight32   
)

Definition at line 105 of file bits-unittest.cc.

105  {
106  TRACED_FORRANGE(uint32_t, shift, 0, 31) {
107  EXPECT_EQ(0u, RotateRight32(0u, shift));
108  }
109  EXPECT_EQ(1u, RotateRight32(1, 0));
110  EXPECT_EQ(1u, RotateRight32(2, 1));
111  EXPECT_EQ(0x80000000u, RotateRight32(1, 1));
112 }
uint32_t RotateRight32(uint32_t value, uint32_t shift)
Definition: bits.h:107

References RotateRight32(), and shift.

+ Here is the call graph for this function:

◆ TEST() [7/12]

v8::base::bits::TEST ( Bits  ,
RotateRight64   
)

Definition at line 115 of file bits-unittest.cc.

115  {
116  TRACED_FORRANGE(uint64_t, shift, 0, 63) {
117  EXPECT_EQ(0u, RotateRight64(0u, shift));
118  }
119  EXPECT_EQ(1u, RotateRight64(1, 0));
120  EXPECT_EQ(1u, RotateRight64(2, 1));
121  EXPECT_EQ(V8_UINT64_C(0x8000000000000000), RotateRight64(1, 1));
122 }
uint64_t RotateRight64(uint64_t value, uint64_t shift)
Definition: bits.h:113

References RotateRight64(), shift, and V8_UINT64_C.

+ Here is the call graph for this function:

◆ TEST() [8/12]

v8::base::bits::TEST ( Bits  ,
RoundDownToPowerOfTwo32   
)

Definition at line 95 of file bits-unittest.cc.

95  {
96  TRACED_FORRANGE(uint32_t, shift, 0, 31) {
97  EXPECT_EQ(1u << shift, RoundDownToPowerOfTwo32(1u << shift));
98  }
99  EXPECT_EQ(0u, RoundDownToPowerOfTwo32(0));
100  EXPECT_EQ(4u, RoundDownToPowerOfTwo32(5));
101  EXPECT_EQ(0x80000000u, RoundDownToPowerOfTwo32(0x80000001u));
102 }
uint32_t RoundDownToPowerOfTwo32(uint32_t value)
Definition: bits.h:99

References RoundDownToPowerOfTwo32(), and shift.

+ Here is the call graph for this function:

◆ TEST() [9/12]

v8::base::bits::TEST ( Bits  ,
RoundUpToPowerOfTwo32   
)

Definition at line 79 of file bits-unittest.cc.

79  {
80  TRACED_FORRANGE(uint32_t, shift, 0, 31) {
81  EXPECT_EQ(1u << shift, RoundUpToPowerOfTwo32(1u << shift));
82  }
83  EXPECT_EQ(0u, RoundUpToPowerOfTwo32(0));
84  EXPECT_EQ(4u, RoundUpToPowerOfTwo32(3));
85  EXPECT_EQ(0x80000000u, RoundUpToPowerOfTwo32(0x7fffffffu));
86 }

References RoundUpToPowerOfTwo32(), and shift.

+ Here is the call graph for this function:

◆ TEST() [10/12]

v8::base::bits::TEST ( Bits  ,
SignedAddOverflow32   
)

Definition at line 125 of file bits-unittest.cc.

125  {
126  int32_t val = 0;
127  EXPECT_FALSE(SignedAddOverflow32(0, 0, &val));
128  EXPECT_EQ(0, val);
129  EXPECT_TRUE(
130  SignedAddOverflow32(std::numeric_limits<int32_t>::max(), 1, &val));
131  EXPECT_EQ(std::numeric_limits<int32_t>::min(), val);
132  EXPECT_TRUE(
134  EXPECT_EQ(std::numeric_limits<int32_t>::max(), val);
135  EXPECT_TRUE(SignedAddOverflow32(std::numeric_limits<int32_t>::max(),
136  std::numeric_limits<int32_t>::max(), &val));
137  EXPECT_EQ(-2, val);
138  TRACED_FORRANGE(int32_t, i, 1, 50) {
139  TRACED_FORRANGE(int32_t, j, 1, i) {
140  EXPECT_FALSE(SignedAddOverflow32(i, j, &val));
141  EXPECT_EQ(i + j, val);
142  }
143  }
144 }
int int32_t
Definition: unicode.cc:24
bool SignedAddOverflow32(int32_t lhs, int32_t rhs, int32_t *val)
Definition: bits.h:122
static int min(int a, int b)
Definition: liveedit.cc:273

References v8::internal::min(), and SignedAddOverflow32().

+ Here is the call graph for this function:

◆ TEST() [11/12]

v8::base::bits::TEST ( Bits  ,
SignedSubOverflow32   
)

Definition at line 147 of file bits-unittest.cc.

147  {
148  int32_t val = 0;
149  EXPECT_FALSE(SignedSubOverflow32(0, 0, &val));
150  EXPECT_EQ(0, val);
151  EXPECT_TRUE(
153  EXPECT_EQ(std::numeric_limits<int32_t>::max(), val);
154  EXPECT_TRUE(
155  SignedSubOverflow32(std::numeric_limits<int32_t>::max(), -1, &val));
156  EXPECT_EQ(std::numeric_limits<int32_t>::min(), val);
157  TRACED_FORRANGE(int32_t, i, 1, 50) {
158  TRACED_FORRANGE(int32_t, j, 1, i) {
159  EXPECT_FALSE(SignedSubOverflow32(i, j, &val));
160  EXPECT_EQ(i - j, val);
161  }
162  }
163 }
bool SignedSubOverflow32(int32_t lhs, int32_t rhs, int32_t *val)
Definition: bits.h:136

References v8::internal::min(), and SignedSubOverflow32().

+ Here is the call graph for this function:

◆ TEST() [12/12]

v8::base::bits::TEST ( BitsDeathTest  ,
DISABLE_IN_RELEASE(RoundUpToPowerOfTwo32  
)

Definition at line 89 of file bits-unittest.cc.

89  {
90  ASSERT_DEATH_IF_SUPPORTED({ RoundUpToPowerOfTwo32(0x80000001u); },
91  "0x80000000");
92 }

References RoundUpToPowerOfTwo32().

+ Here is the call graph for this function: