• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- unittests/Support/MathExtrasTest.cpp - math utils tests ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Support/MathExtras.h"
11 #include "gtest/gtest.h"
12 
13 using namespace llvm;
14 
15 namespace {
16 
TEST(MathExtras,countTrailingZeros)17 TEST(MathExtras, countTrailingZeros) {
18   uint8_t Z8 = 0;
19   uint16_t Z16 = 0;
20   uint32_t Z32 = 0;
21   uint64_t Z64 = 0;
22   EXPECT_EQ(8u, countTrailingZeros(Z8));
23   EXPECT_EQ(16u, countTrailingZeros(Z16));
24   EXPECT_EQ(32u, countTrailingZeros(Z32));
25   EXPECT_EQ(64u, countTrailingZeros(Z64));
26 
27   uint8_t NZ8 = 42;
28   uint16_t NZ16 = 42;
29   uint32_t NZ32 = 42;
30   uint64_t NZ64 = 42;
31   EXPECT_EQ(1u, countTrailingZeros(NZ8));
32   EXPECT_EQ(1u, countTrailingZeros(NZ16));
33   EXPECT_EQ(1u, countTrailingZeros(NZ32));
34   EXPECT_EQ(1u, countTrailingZeros(NZ64));
35 }
36 
TEST(MathExtras,countLeadingZeros)37 TEST(MathExtras, countLeadingZeros) {
38   uint8_t Z8 = 0;
39   uint16_t Z16 = 0;
40   uint32_t Z32 = 0;
41   uint64_t Z64 = 0;
42   EXPECT_EQ(8u, countLeadingZeros(Z8));
43   EXPECT_EQ(16u, countLeadingZeros(Z16));
44   EXPECT_EQ(32u, countLeadingZeros(Z32));
45   EXPECT_EQ(64u, countLeadingZeros(Z64));
46 
47   uint8_t NZ8 = 42;
48   uint16_t NZ16 = 42;
49   uint32_t NZ32 = 42;
50   uint64_t NZ64 = 42;
51   EXPECT_EQ(2u, countLeadingZeros(NZ8));
52   EXPECT_EQ(10u, countLeadingZeros(NZ16));
53   EXPECT_EQ(26u, countLeadingZeros(NZ32));
54   EXPECT_EQ(58u, countLeadingZeros(NZ64));
55 
56   EXPECT_EQ(8u, countLeadingZeros(0x00F000FFu));
57   EXPECT_EQ(8u, countLeadingZeros(0x00F12345u));
58   for (unsigned i = 0; i <= 30; ++i) {
59     EXPECT_EQ(31 - i, countLeadingZeros(1u << i));
60   }
61 
62   EXPECT_EQ(8u, countLeadingZeros(0x00F1234500F12345ULL));
63   EXPECT_EQ(1u, countLeadingZeros(1ULL << 62));
64   for (unsigned i = 0; i <= 62; ++i) {
65     EXPECT_EQ(63 - i, countLeadingZeros(1ULL << i));
66   }
67 }
68 
TEST(MathExtras,onesMask)69 TEST(MathExtras, onesMask) {
70   EXPECT_EQ(0U, maskLeadingOnes<uint8_t>(0));
71   EXPECT_EQ(0U, maskTrailingOnes<uint8_t>(0));
72   EXPECT_EQ(0U, maskLeadingOnes<uint16_t>(0));
73   EXPECT_EQ(0U, maskTrailingOnes<uint16_t>(0));
74   EXPECT_EQ(0U, maskLeadingOnes<uint32_t>(0));
75   EXPECT_EQ(0U, maskTrailingOnes<uint32_t>(0));
76   EXPECT_EQ(0U, maskLeadingOnes<uint64_t>(0));
77   EXPECT_EQ(0U, maskTrailingOnes<uint64_t>(0));
78 
79   EXPECT_EQ(0x00000003U, maskTrailingOnes<uint32_t>(2U));
80   EXPECT_EQ(0xC0000000U, maskLeadingOnes<uint32_t>(2U));
81 
82   EXPECT_EQ(0x000007FFU, maskTrailingOnes<uint32_t>(11U));
83   EXPECT_EQ(0xFFE00000U, maskLeadingOnes<uint32_t>(11U));
84 
85   EXPECT_EQ(0xFFFFFFFFU, maskTrailingOnes<uint32_t>(32U));
86   EXPECT_EQ(0xFFFFFFFFU, maskLeadingOnes<uint32_t>(32U));
87   EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, maskTrailingOnes<uint64_t>(64U));
88   EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, maskLeadingOnes<uint64_t>(64U));
89 
90   EXPECT_EQ(0x0000FFFFFFFFFFFFULL, maskTrailingOnes<uint64_t>(48U));
91   EXPECT_EQ(0xFFFFFFFFFFFF0000ULL, maskLeadingOnes<uint64_t>(48U));
92 }
93 
TEST(MathExtras,findFirstSet)94 TEST(MathExtras, findFirstSet) {
95   uint8_t Z8 = 0;
96   uint16_t Z16 = 0;
97   uint32_t Z32 = 0;
98   uint64_t Z64 = 0;
99   EXPECT_EQ(0xFFULL, findFirstSet(Z8));
100   EXPECT_EQ(0xFFFFULL, findFirstSet(Z16));
101   EXPECT_EQ(0xFFFFFFFFULL, findFirstSet(Z32));
102   EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, findFirstSet(Z64));
103 
104   uint8_t NZ8 = 42;
105   uint16_t NZ16 = 42;
106   uint32_t NZ32 = 42;
107   uint64_t NZ64 = 42;
108   EXPECT_EQ(1u, findFirstSet(NZ8));
109   EXPECT_EQ(1u, findFirstSet(NZ16));
110   EXPECT_EQ(1u, findFirstSet(NZ32));
111   EXPECT_EQ(1u, findFirstSet(NZ64));
112 }
113 
TEST(MathExtras,findLastSet)114 TEST(MathExtras, findLastSet) {
115   uint8_t Z8 = 0;
116   uint16_t Z16 = 0;
117   uint32_t Z32 = 0;
118   uint64_t Z64 = 0;
119   EXPECT_EQ(0xFFULL, findLastSet(Z8));
120   EXPECT_EQ(0xFFFFULL, findLastSet(Z16));
121   EXPECT_EQ(0xFFFFFFFFULL, findLastSet(Z32));
122   EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, findLastSet(Z64));
123 
124   uint8_t NZ8 = 42;
125   uint16_t NZ16 = 42;
126   uint32_t NZ32 = 42;
127   uint64_t NZ64 = 42;
128   EXPECT_EQ(5u, findLastSet(NZ8));
129   EXPECT_EQ(5u, findLastSet(NZ16));
130   EXPECT_EQ(5u, findLastSet(NZ32));
131   EXPECT_EQ(5u, findLastSet(NZ64));
132 }
133 
TEST(MathExtras,isIntN)134 TEST(MathExtras, isIntN) {
135   EXPECT_TRUE(isIntN(16, 32767));
136   EXPECT_FALSE(isIntN(16, 32768));
137 }
138 
TEST(MathExtras,isUIntN)139 TEST(MathExtras, isUIntN) {
140   EXPECT_TRUE(isUIntN(16, 65535));
141   EXPECT_FALSE(isUIntN(16, 65536));
142   EXPECT_TRUE(isUIntN(1, 0));
143   EXPECT_TRUE(isUIntN(6, 63));
144 }
145 
TEST(MathExtras,maxIntN)146 TEST(MathExtras, maxIntN) {
147   EXPECT_EQ(32767, maxIntN(16));
148   EXPECT_EQ(2147483647, maxIntN(32));
149   EXPECT_EQ(std::numeric_limits<int32_t>::max(), maxIntN(32));
150   EXPECT_EQ(std::numeric_limits<int64_t>::max(), maxIntN(64));
151 }
152 
TEST(MathExtras,minIntN)153 TEST(MathExtras, minIntN) {
154   EXPECT_EQ(-32768LL, minIntN(16));
155   EXPECT_EQ(-64LL, minIntN(7));
156   EXPECT_EQ(std::numeric_limits<int32_t>::min(), minIntN(32));
157   EXPECT_EQ(std::numeric_limits<int64_t>::min(), minIntN(64));
158 }
159 
TEST(MathExtras,maxUIntN)160 TEST(MathExtras, maxUIntN) {
161   EXPECT_EQ(0xffffULL, maxUIntN(16));
162   EXPECT_EQ(0xffffffffULL, maxUIntN(32));
163   EXPECT_EQ(0xffffffffffffffffULL, maxUIntN(64));
164   EXPECT_EQ(1ULL, maxUIntN(1));
165   EXPECT_EQ(0x0fULL, maxUIntN(4));
166 }
167 
TEST(MathExtras,reverseBits)168 TEST(MathExtras, reverseBits) {
169   uint8_t NZ8 = 42;
170   uint16_t NZ16 = 42;
171   uint32_t NZ32 = 42;
172   uint64_t NZ64 = 42;
173   EXPECT_EQ(0x54ULL, reverseBits(NZ8));
174   EXPECT_EQ(0x5400ULL, reverseBits(NZ16));
175   EXPECT_EQ(0x54000000ULL, reverseBits(NZ32));
176   EXPECT_EQ(0x5400000000000000ULL, reverseBits(NZ64));
177 }
178 
TEST(MathExtras,isPowerOf2_32)179 TEST(MathExtras, isPowerOf2_32) {
180   EXPECT_FALSE(isPowerOf2_32(0));
181   EXPECT_TRUE(isPowerOf2_32(1 << 6));
182   EXPECT_TRUE(isPowerOf2_32(1 << 12));
183   EXPECT_FALSE(isPowerOf2_32((1 << 19) + 3));
184   EXPECT_FALSE(isPowerOf2_32(0xABCDEF0));
185 }
186 
TEST(MathExtras,isPowerOf2_64)187 TEST(MathExtras, isPowerOf2_64) {
188   EXPECT_FALSE(isPowerOf2_64(0));
189   EXPECT_TRUE(isPowerOf2_64(1LL << 46));
190   EXPECT_TRUE(isPowerOf2_64(1LL << 12));
191   EXPECT_FALSE(isPowerOf2_64((1LL << 53) + 3));
192   EXPECT_FALSE(isPowerOf2_64(0xABCDEF0ABCDEF0LL));
193 }
194 
TEST(MathExtras,PowerOf2Ceil)195 TEST(MathExtras, PowerOf2Ceil) {
196   EXPECT_EQ(0U, PowerOf2Ceil(0U));
197   EXPECT_EQ(8U, PowerOf2Ceil(8U));
198   EXPECT_EQ(8U, PowerOf2Ceil(7U));
199 }
200 
TEST(MathExtras,PowerOf2Floor)201 TEST(MathExtras, PowerOf2Floor) {
202   EXPECT_EQ(0U, PowerOf2Floor(0U));
203   EXPECT_EQ(8U, PowerOf2Floor(8U));
204   EXPECT_EQ(4U, PowerOf2Floor(7U));
205 }
206 
TEST(MathExtras,ByteSwap_32)207 TEST(MathExtras, ByteSwap_32) {
208   EXPECT_EQ(0x44332211u, ByteSwap_32(0x11223344));
209   EXPECT_EQ(0xDDCCBBAAu, ByteSwap_32(0xAABBCCDD));
210 }
211 
TEST(MathExtras,ByteSwap_64)212 TEST(MathExtras, ByteSwap_64) {
213   EXPECT_EQ(0x8877665544332211ULL, ByteSwap_64(0x1122334455667788LL));
214   EXPECT_EQ(0x1100FFEEDDCCBBAAULL, ByteSwap_64(0xAABBCCDDEEFF0011LL));
215 }
216 
TEST(MathExtras,countLeadingOnes)217 TEST(MathExtras, countLeadingOnes) {
218   for (int i = 30; i >= 0; --i) {
219     // Start with all ones and unset some bit.
220     EXPECT_EQ(31u - i, countLeadingOnes(0xFFFFFFFF ^ (1 << i)));
221   }
222   for (int i = 62; i >= 0; --i) {
223     // Start with all ones and unset some bit.
224     EXPECT_EQ(63u - i, countLeadingOnes(0xFFFFFFFFFFFFFFFFULL ^ (1LL << i)));
225   }
226   for (int i = 30; i >= 0; --i) {
227     // Start with all ones and unset some bit.
228     EXPECT_EQ(31u - i, countLeadingOnes(0xFFFFFFFF ^ (1 << i)));
229   }
230 }
231 
TEST(MathExtras,FloatBits)232 TEST(MathExtras, FloatBits) {
233   static const float kValue = 5632.34f;
234   EXPECT_FLOAT_EQ(kValue, BitsToFloat(FloatToBits(kValue)));
235 }
236 
TEST(MathExtras,DoubleBits)237 TEST(MathExtras, DoubleBits) {
238   static const double kValue = 87987234.983498;
239   EXPECT_DOUBLE_EQ(kValue, BitsToDouble(DoubleToBits(kValue)));
240 }
241 
TEST(MathExtras,MinAlign)242 TEST(MathExtras, MinAlign) {
243   EXPECT_EQ(1u, MinAlign(2, 3));
244   EXPECT_EQ(2u, MinAlign(2, 4));
245   EXPECT_EQ(1u, MinAlign(17, 64));
246   EXPECT_EQ(256u, MinAlign(256, 512));
247 }
248 
TEST(MathExtras,NextPowerOf2)249 TEST(MathExtras, NextPowerOf2) {
250   EXPECT_EQ(4u, NextPowerOf2(3));
251   EXPECT_EQ(16u, NextPowerOf2(15));
252   EXPECT_EQ(256u, NextPowerOf2(128));
253 }
254 
TEST(MathExtras,alignTo)255 TEST(MathExtras, alignTo) {
256   EXPECT_EQ(8u, alignTo(5, 8));
257   EXPECT_EQ(24u, alignTo(17, 8));
258   EXPECT_EQ(0u, alignTo(~0LL, 8));
259 
260   EXPECT_EQ(7u, alignTo(5, 8, 7));
261   EXPECT_EQ(17u, alignTo(17, 8, 1));
262   EXPECT_EQ(3u, alignTo(~0LL, 8, 3));
263   EXPECT_EQ(552u, alignTo(321, 255, 42));
264 }
265 
266 template<typename T>
SaturatingAddTestHelper()267 void SaturatingAddTestHelper()
268 {
269   const T Max = std::numeric_limits<T>::max();
270   bool ResultOverflowed;
271 
272   EXPECT_EQ(T(3), SaturatingAdd(T(1), T(2)));
273   EXPECT_EQ(T(3), SaturatingAdd(T(1), T(2), &ResultOverflowed));
274   EXPECT_FALSE(ResultOverflowed);
275 
276   EXPECT_EQ(Max, SaturatingAdd(Max, T(1)));
277   EXPECT_EQ(Max, SaturatingAdd(Max, T(1), &ResultOverflowed));
278   EXPECT_TRUE(ResultOverflowed);
279 
280   EXPECT_EQ(Max, SaturatingAdd(T(1), T(Max - 1)));
281   EXPECT_EQ(Max, SaturatingAdd(T(1), T(Max - 1), &ResultOverflowed));
282   EXPECT_FALSE(ResultOverflowed);
283 
284   EXPECT_EQ(Max, SaturatingAdd(T(1), Max));
285   EXPECT_EQ(Max, SaturatingAdd(T(1), Max, &ResultOverflowed));
286   EXPECT_TRUE(ResultOverflowed);
287 
288   EXPECT_EQ(Max, SaturatingAdd(Max, Max));
289   EXPECT_EQ(Max, SaturatingAdd(Max, Max, &ResultOverflowed));
290   EXPECT_TRUE(ResultOverflowed);
291 }
292 
TEST(MathExtras,SaturatingAdd)293 TEST(MathExtras, SaturatingAdd) {
294   SaturatingAddTestHelper<uint8_t>();
295   SaturatingAddTestHelper<uint16_t>();
296   SaturatingAddTestHelper<uint32_t>();
297   SaturatingAddTestHelper<uint64_t>();
298 }
299 
300 template<typename T>
SaturatingMultiplyTestHelper()301 void SaturatingMultiplyTestHelper()
302 {
303   const T Max = std::numeric_limits<T>::max();
304   bool ResultOverflowed;
305 
306   // Test basic multiplication.
307   EXPECT_EQ(T(6), SaturatingMultiply(T(2), T(3)));
308   EXPECT_EQ(T(6), SaturatingMultiply(T(2), T(3), &ResultOverflowed));
309   EXPECT_FALSE(ResultOverflowed);
310 
311   EXPECT_EQ(T(6), SaturatingMultiply(T(3), T(2)));
312   EXPECT_EQ(T(6), SaturatingMultiply(T(3), T(2), &ResultOverflowed));
313   EXPECT_FALSE(ResultOverflowed);
314 
315   // Test multiplication by zero.
316   EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(0)));
317   EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(0), &ResultOverflowed));
318   EXPECT_FALSE(ResultOverflowed);
319 
320   EXPECT_EQ(T(0), SaturatingMultiply(T(1), T(0)));
321   EXPECT_EQ(T(0), SaturatingMultiply(T(1), T(0), &ResultOverflowed));
322   EXPECT_FALSE(ResultOverflowed);
323 
324   EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(1)));
325   EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(1), &ResultOverflowed));
326   EXPECT_FALSE(ResultOverflowed);
327 
328   EXPECT_EQ(T(0), SaturatingMultiply(Max, T(0)));
329   EXPECT_EQ(T(0), SaturatingMultiply(Max, T(0), &ResultOverflowed));
330   EXPECT_FALSE(ResultOverflowed);
331 
332   EXPECT_EQ(T(0), SaturatingMultiply(T(0), Max));
333   EXPECT_EQ(T(0), SaturatingMultiply(T(0), Max, &ResultOverflowed));
334   EXPECT_FALSE(ResultOverflowed);
335 
336   // Test multiplication by maximum value.
337   EXPECT_EQ(Max, SaturatingMultiply(Max, T(2)));
338   EXPECT_EQ(Max, SaturatingMultiply(Max, T(2), &ResultOverflowed));
339   EXPECT_TRUE(ResultOverflowed);
340 
341   EXPECT_EQ(Max, SaturatingMultiply(T(2), Max));
342   EXPECT_EQ(Max, SaturatingMultiply(T(2), Max, &ResultOverflowed));
343   EXPECT_TRUE(ResultOverflowed);
344 
345   EXPECT_EQ(Max, SaturatingMultiply(Max, Max));
346   EXPECT_EQ(Max, SaturatingMultiply(Max, Max, &ResultOverflowed));
347   EXPECT_TRUE(ResultOverflowed);
348 
349   // Test interesting boundary conditions for algorithm -
350   // ((1 << A) - 1) * ((1 << B) + K) for K in [-1, 0, 1]
351   // and A + B == std::numeric_limits<T>::digits.
352   // We expect overflow iff A > B and K = 1.
353   const int Digits = std::numeric_limits<T>::digits;
354   for (int A = 1, B = Digits - 1; B >= 1; ++A, --B) {
355     for (int K = -1; K <= 1; ++K) {
356       T X = (T(1) << A) - T(1);
357       T Y = (T(1) << B) + K;
358       bool OverflowExpected = A > B && K == 1;
359 
360       if(OverflowExpected) {
361         EXPECT_EQ(Max, SaturatingMultiply(X, Y));
362         EXPECT_EQ(Max, SaturatingMultiply(X, Y, &ResultOverflowed));
363         EXPECT_TRUE(ResultOverflowed);
364       } else {
365         EXPECT_EQ(X * Y, SaturatingMultiply(X, Y));
366         EXPECT_EQ(X * Y, SaturatingMultiply(X, Y, &ResultOverflowed));
367         EXPECT_FALSE(ResultOverflowed);
368       }
369     }
370   }
371 }
372 
TEST(MathExtras,SaturatingMultiply)373 TEST(MathExtras, SaturatingMultiply) {
374   SaturatingMultiplyTestHelper<uint8_t>();
375   SaturatingMultiplyTestHelper<uint16_t>();
376   SaturatingMultiplyTestHelper<uint32_t>();
377   SaturatingMultiplyTestHelper<uint64_t>();
378 }
379 
380 template<typename T>
SaturatingMultiplyAddTestHelper()381 void SaturatingMultiplyAddTestHelper()
382 {
383   const T Max = std::numeric_limits<T>::max();
384   bool ResultOverflowed;
385 
386   // Test basic multiply-add.
387   EXPECT_EQ(T(16), SaturatingMultiplyAdd(T(2), T(3), T(10)));
388   EXPECT_EQ(T(16), SaturatingMultiplyAdd(T(2), T(3), T(10), &ResultOverflowed));
389   EXPECT_FALSE(ResultOverflowed);
390 
391   // Test multiply overflows, add doesn't overflow
392   EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, T(0), &ResultOverflowed));
393   EXPECT_TRUE(ResultOverflowed);
394 
395   // Test multiply doesn't overflow, add overflows
396   EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), T(1), Max, &ResultOverflowed));
397   EXPECT_TRUE(ResultOverflowed);
398 
399   // Test multiply-add with Max as operand
400   EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), T(1), Max, &ResultOverflowed));
401   EXPECT_TRUE(ResultOverflowed);
402 
403   EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), Max, T(1), &ResultOverflowed));
404   EXPECT_TRUE(ResultOverflowed);
405 
406   EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, T(1), &ResultOverflowed));
407   EXPECT_TRUE(ResultOverflowed);
408 
409   EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, Max, &ResultOverflowed));
410   EXPECT_TRUE(ResultOverflowed);
411 
412   // Test multiply-add with 0 as operand
413   EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(1), T(0), &ResultOverflowed));
414   EXPECT_FALSE(ResultOverflowed);
415 
416   EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(0), T(1), &ResultOverflowed));
417   EXPECT_FALSE(ResultOverflowed);
418 
419   EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(0), T(0), T(1), &ResultOverflowed));
420   EXPECT_FALSE(ResultOverflowed);
421 
422   EXPECT_EQ(T(0), SaturatingMultiplyAdd(T(0), T(0), T(0), &ResultOverflowed));
423   EXPECT_FALSE(ResultOverflowed);
424 
425 }
426 
TEST(MathExtras,SaturatingMultiplyAdd)427 TEST(MathExtras, SaturatingMultiplyAdd) {
428   SaturatingMultiplyAddTestHelper<uint8_t>();
429   SaturatingMultiplyAddTestHelper<uint16_t>();
430   SaturatingMultiplyAddTestHelper<uint32_t>();
431   SaturatingMultiplyAddTestHelper<uint64_t>();
432 }
433 
TEST(MathExtras,IsShiftedUInt)434 TEST(MathExtras, IsShiftedUInt) {
435   EXPECT_TRUE((isShiftedUInt<1, 0>(0)));
436   EXPECT_TRUE((isShiftedUInt<1, 0>(1)));
437   EXPECT_FALSE((isShiftedUInt<1, 0>(2)));
438   EXPECT_FALSE((isShiftedUInt<1, 0>(3)));
439   EXPECT_FALSE((isShiftedUInt<1, 0>(0x8000000000000000)));
440   EXPECT_TRUE((isShiftedUInt<1, 63>(0x8000000000000000)));
441   EXPECT_TRUE((isShiftedUInt<2, 62>(0xC000000000000000)));
442   EXPECT_FALSE((isShiftedUInt<2, 62>(0xE000000000000000)));
443 
444   // 0x201 is ten bits long and has a 1 in the MSB and LSB.
445   EXPECT_TRUE((isShiftedUInt<10, 5>(uint64_t(0x201) << 5)));
446   EXPECT_FALSE((isShiftedUInt<10, 5>(uint64_t(0x201) << 4)));
447   EXPECT_FALSE((isShiftedUInt<10, 5>(uint64_t(0x201) << 6)));
448 }
449 
TEST(MathExtras,IsShiftedInt)450 TEST(MathExtras, IsShiftedInt) {
451   EXPECT_TRUE((isShiftedInt<1, 0>(0)));
452   EXPECT_TRUE((isShiftedInt<1, 0>(-1)));
453   EXPECT_FALSE((isShiftedInt<1, 0>(2)));
454   EXPECT_FALSE((isShiftedInt<1, 0>(3)));
455   EXPECT_FALSE((isShiftedInt<1, 0>(0x8000000000000000)));
456   EXPECT_TRUE((isShiftedInt<1, 63>(0x8000000000000000)));
457   EXPECT_TRUE((isShiftedInt<2, 62>(0xC000000000000000)));
458   EXPECT_FALSE((isShiftedInt<2, 62>(0xE000000000000000)));
459 
460   // 0x201 is ten bits long and has a 1 in the MSB and LSB.
461   EXPECT_TRUE((isShiftedInt<11, 5>(int64_t(0x201) << 5)));
462   EXPECT_FALSE((isShiftedInt<11, 5>(int64_t(0x201) << 3)));
463   EXPECT_FALSE((isShiftedInt<11, 5>(int64_t(0x201) << 6)));
464   EXPECT_TRUE((isShiftedInt<11, 5>(-(int64_t(0x201) << 5))));
465   EXPECT_FALSE((isShiftedInt<11, 5>(-(int64_t(0x201) << 3))));
466   EXPECT_FALSE((isShiftedInt<11, 5>(-(int64_t(0x201) << 6))));
467 
468   EXPECT_TRUE((isShiftedInt<6, 10>(-(int64_t(1) << 15))));
469   EXPECT_FALSE((isShiftedInt<6, 10>(int64_t(1) << 15)));
470 }
471 
472 } // namespace
473