• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- unittests/Support/MathExtrasTest.cpp - math utils tests ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Support/MathExtras.h"
10 #include "gtest/gtest.h"
11 
12 using namespace llvm;
13 
14 namespace {
15 
TEST(MathExtras,countTrailingZeros)16 TEST(MathExtras, countTrailingZeros) {
17   uint8_t Z8 = 0;
18   uint16_t Z16 = 0;
19   uint32_t Z32 = 0;
20   uint64_t Z64 = 0;
21   EXPECT_EQ(8u, countTrailingZeros(Z8));
22   EXPECT_EQ(16u, countTrailingZeros(Z16));
23   EXPECT_EQ(32u, countTrailingZeros(Z32));
24   EXPECT_EQ(64u, countTrailingZeros(Z64));
25 
26   uint8_t NZ8 = 42;
27   uint16_t NZ16 = 42;
28   uint32_t NZ32 = 42;
29   uint64_t NZ64 = 42;
30   EXPECT_EQ(1u, countTrailingZeros(NZ8));
31   EXPECT_EQ(1u, countTrailingZeros(NZ16));
32   EXPECT_EQ(1u, countTrailingZeros(NZ32));
33   EXPECT_EQ(1u, countTrailingZeros(NZ64));
34 }
35 
TEST(MathExtras,countLeadingZeros)36 TEST(MathExtras, countLeadingZeros) {
37   uint8_t Z8 = 0;
38   uint16_t Z16 = 0;
39   uint32_t Z32 = 0;
40   uint64_t Z64 = 0;
41   EXPECT_EQ(8u, countLeadingZeros(Z8));
42   EXPECT_EQ(16u, countLeadingZeros(Z16));
43   EXPECT_EQ(32u, countLeadingZeros(Z32));
44   EXPECT_EQ(64u, countLeadingZeros(Z64));
45 
46   uint8_t NZ8 = 42;
47   uint16_t NZ16 = 42;
48   uint32_t NZ32 = 42;
49   uint64_t NZ64 = 42;
50   EXPECT_EQ(2u, countLeadingZeros(NZ8));
51   EXPECT_EQ(10u, countLeadingZeros(NZ16));
52   EXPECT_EQ(26u, countLeadingZeros(NZ32));
53   EXPECT_EQ(58u, countLeadingZeros(NZ64));
54 
55   EXPECT_EQ(8u, countLeadingZeros(0x00F000FFu));
56   EXPECT_EQ(8u, countLeadingZeros(0x00F12345u));
57   for (unsigned i = 0; i <= 30; ++i) {
58     EXPECT_EQ(31 - i, countLeadingZeros(1u << i));
59   }
60 
61   EXPECT_EQ(8u, countLeadingZeros(0x00F1234500F12345ULL));
62   EXPECT_EQ(1u, countLeadingZeros(1ULL << 62));
63   for (unsigned i = 0; i <= 62; ++i) {
64     EXPECT_EQ(63 - i, countLeadingZeros(1ULL << i));
65   }
66 }
67 
TEST(MathExtras,onesMask)68 TEST(MathExtras, onesMask) {
69   EXPECT_EQ(0U, maskLeadingOnes<uint8_t>(0));
70   EXPECT_EQ(0U, maskTrailingOnes<uint8_t>(0));
71   EXPECT_EQ(0U, maskLeadingOnes<uint16_t>(0));
72   EXPECT_EQ(0U, maskTrailingOnes<uint16_t>(0));
73   EXPECT_EQ(0U, maskLeadingOnes<uint32_t>(0));
74   EXPECT_EQ(0U, maskTrailingOnes<uint32_t>(0));
75   EXPECT_EQ(0U, maskLeadingOnes<uint64_t>(0));
76   EXPECT_EQ(0U, maskTrailingOnes<uint64_t>(0));
77 
78   EXPECT_EQ(0x00000003U, maskTrailingOnes<uint32_t>(2U));
79   EXPECT_EQ(0xC0000000U, maskLeadingOnes<uint32_t>(2U));
80 
81   EXPECT_EQ(0x000007FFU, maskTrailingOnes<uint32_t>(11U));
82   EXPECT_EQ(0xFFE00000U, maskLeadingOnes<uint32_t>(11U));
83 
84   EXPECT_EQ(0xFFFFFFFFU, maskTrailingOnes<uint32_t>(32U));
85   EXPECT_EQ(0xFFFFFFFFU, maskLeadingOnes<uint32_t>(32U));
86   EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, maskTrailingOnes<uint64_t>(64U));
87   EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, maskLeadingOnes<uint64_t>(64U));
88 
89   EXPECT_EQ(0x0000FFFFFFFFFFFFULL, maskTrailingOnes<uint64_t>(48U));
90   EXPECT_EQ(0xFFFFFFFFFFFF0000ULL, maskLeadingOnes<uint64_t>(48U));
91 }
92 
TEST(MathExtras,findFirstSet)93 TEST(MathExtras, findFirstSet) {
94   uint8_t Z8 = 0;
95   uint16_t Z16 = 0;
96   uint32_t Z32 = 0;
97   uint64_t Z64 = 0;
98   EXPECT_EQ(0xFFULL, findFirstSet(Z8));
99   EXPECT_EQ(0xFFFFULL, findFirstSet(Z16));
100   EXPECT_EQ(0xFFFFFFFFULL, findFirstSet(Z32));
101   EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, findFirstSet(Z64));
102 
103   uint8_t NZ8 = 42;
104   uint16_t NZ16 = 42;
105   uint32_t NZ32 = 42;
106   uint64_t NZ64 = 42;
107   EXPECT_EQ(1u, findFirstSet(NZ8));
108   EXPECT_EQ(1u, findFirstSet(NZ16));
109   EXPECT_EQ(1u, findFirstSet(NZ32));
110   EXPECT_EQ(1u, findFirstSet(NZ64));
111 }
112 
TEST(MathExtras,findLastSet)113 TEST(MathExtras, findLastSet) {
114   uint8_t Z8 = 0;
115   uint16_t Z16 = 0;
116   uint32_t Z32 = 0;
117   uint64_t Z64 = 0;
118   EXPECT_EQ(0xFFULL, findLastSet(Z8));
119   EXPECT_EQ(0xFFFFULL, findLastSet(Z16));
120   EXPECT_EQ(0xFFFFFFFFULL, findLastSet(Z32));
121   EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, findLastSet(Z64));
122 
123   uint8_t NZ8 = 42;
124   uint16_t NZ16 = 42;
125   uint32_t NZ32 = 42;
126   uint64_t NZ64 = 42;
127   EXPECT_EQ(5u, findLastSet(NZ8));
128   EXPECT_EQ(5u, findLastSet(NZ16));
129   EXPECT_EQ(5u, findLastSet(NZ32));
130   EXPECT_EQ(5u, findLastSet(NZ64));
131 }
132 
TEST(MathExtras,isIntN)133 TEST(MathExtras, isIntN) {
134   EXPECT_TRUE(isIntN(16, 32767));
135   EXPECT_FALSE(isIntN(16, 32768));
136 }
137 
TEST(MathExtras,isUIntN)138 TEST(MathExtras, isUIntN) {
139   EXPECT_TRUE(isUIntN(16, 65535));
140   EXPECT_FALSE(isUIntN(16, 65536));
141   EXPECT_TRUE(isUIntN(1, 0));
142   EXPECT_TRUE(isUIntN(6, 63));
143 }
144 
TEST(MathExtras,maxIntN)145 TEST(MathExtras, maxIntN) {
146   EXPECT_EQ(32767, maxIntN(16));
147   EXPECT_EQ(2147483647, maxIntN(32));
148   EXPECT_EQ(std::numeric_limits<int32_t>::max(), maxIntN(32));
149   EXPECT_EQ(std::numeric_limits<int64_t>::max(), maxIntN(64));
150 }
151 
TEST(MathExtras,minIntN)152 TEST(MathExtras, minIntN) {
153   EXPECT_EQ(-32768LL, minIntN(16));
154   EXPECT_EQ(-64LL, minIntN(7));
155   EXPECT_EQ(std::numeric_limits<int32_t>::min(), minIntN(32));
156   EXPECT_EQ(std::numeric_limits<int64_t>::min(), minIntN(64));
157 }
158 
TEST(MathExtras,maxUIntN)159 TEST(MathExtras, maxUIntN) {
160   EXPECT_EQ(0xffffULL, maxUIntN(16));
161   EXPECT_EQ(0xffffffffULL, maxUIntN(32));
162   EXPECT_EQ(0xffffffffffffffffULL, maxUIntN(64));
163   EXPECT_EQ(1ULL, maxUIntN(1));
164   EXPECT_EQ(0x0fULL, maxUIntN(4));
165 }
166 
TEST(MathExtras,reverseBits)167 TEST(MathExtras, reverseBits) {
168   uint8_t NZ8 = 42;
169   uint16_t NZ16 = 42;
170   uint32_t NZ32 = 42;
171   uint64_t NZ64 = 42;
172   EXPECT_EQ(0x54ULL, reverseBits(NZ8));
173   EXPECT_EQ(0x5400ULL, reverseBits(NZ16));
174   EXPECT_EQ(0x54000000ULL, reverseBits(NZ32));
175   EXPECT_EQ(0x5400000000000000ULL, reverseBits(NZ64));
176 }
177 
TEST(MathExtras,isPowerOf2_32)178 TEST(MathExtras, isPowerOf2_32) {
179   EXPECT_FALSE(isPowerOf2_32(0));
180   EXPECT_TRUE(isPowerOf2_32(1 << 6));
181   EXPECT_TRUE(isPowerOf2_32(1 << 12));
182   EXPECT_FALSE(isPowerOf2_32((1 << 19) + 3));
183   EXPECT_FALSE(isPowerOf2_32(0xABCDEF0));
184 }
185 
TEST(MathExtras,isPowerOf2_64)186 TEST(MathExtras, isPowerOf2_64) {
187   EXPECT_FALSE(isPowerOf2_64(0));
188   EXPECT_TRUE(isPowerOf2_64(1LL << 46));
189   EXPECT_TRUE(isPowerOf2_64(1LL << 12));
190   EXPECT_FALSE(isPowerOf2_64((1LL << 53) + 3));
191   EXPECT_FALSE(isPowerOf2_64(0xABCDEF0ABCDEF0LL));
192 }
193 
TEST(MathExtras,PowerOf2Ceil)194 TEST(MathExtras, PowerOf2Ceil) {
195   EXPECT_EQ(0U, PowerOf2Ceil(0U));
196   EXPECT_EQ(8U, PowerOf2Ceil(8U));
197   EXPECT_EQ(8U, PowerOf2Ceil(7U));
198 }
199 
TEST(MathExtras,PowerOf2Floor)200 TEST(MathExtras, PowerOf2Floor) {
201   EXPECT_EQ(0U, PowerOf2Floor(0U));
202   EXPECT_EQ(8U, PowerOf2Floor(8U));
203   EXPECT_EQ(4U, PowerOf2Floor(7U));
204 }
205 
TEST(MathExtras,CTLog2)206 TEST(MathExtras, CTLog2) {
207   EXPECT_EQ(CTLog2<1ULL << 0>(), 0U);
208   EXPECT_EQ(CTLog2<1ULL << 1>(), 1U);
209   EXPECT_EQ(CTLog2<1ULL << 2>(), 2U);
210   EXPECT_EQ(CTLog2<1ULL << 3>(), 3U);
211   EXPECT_EQ(CTLog2<1ULL << 4>(), 4U);
212   EXPECT_EQ(CTLog2<1ULL << 5>(), 5U);
213   EXPECT_EQ(CTLog2<1ULL << 6>(), 6U);
214   EXPECT_EQ(CTLog2<1ULL << 7>(), 7U);
215   EXPECT_EQ(CTLog2<1ULL << 8>(), 8U);
216   EXPECT_EQ(CTLog2<1ULL << 9>(), 9U);
217   EXPECT_EQ(CTLog2<1ULL << 10>(), 10U);
218   EXPECT_EQ(CTLog2<1ULL << 11>(), 11U);
219   EXPECT_EQ(CTLog2<1ULL << 12>(), 12U);
220   EXPECT_EQ(CTLog2<1ULL << 13>(), 13U);
221   EXPECT_EQ(CTLog2<1ULL << 14>(), 14U);
222   EXPECT_EQ(CTLog2<1ULL << 15>(), 15U);
223 }
224 
TEST(MathExtras,countLeadingOnes)225 TEST(MathExtras, countLeadingOnes) {
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   for (int i = 62; i >= 0; --i) {
231     // Start with all ones and unset some bit.
232     EXPECT_EQ(63u - i, countLeadingOnes(0xFFFFFFFFFFFFFFFFULL ^ (1LL << i)));
233   }
234   for (int i = 30; i >= 0; --i) {
235     // Start with all ones and unset some bit.
236     EXPECT_EQ(31u - i, countLeadingOnes(0xFFFFFFFF ^ (1 << i)));
237   }
238 }
239 
TEST(MathExtras,FloatBits)240 TEST(MathExtras, FloatBits) {
241   static const float kValue = 5632.34f;
242   EXPECT_FLOAT_EQ(kValue, BitsToFloat(FloatToBits(kValue)));
243 }
244 
TEST(MathExtras,DoubleBits)245 TEST(MathExtras, DoubleBits) {
246   static const double kValue = 87987234.983498;
247   EXPECT_DOUBLE_EQ(kValue, BitsToDouble(DoubleToBits(kValue)));
248 }
249 
TEST(MathExtras,MinAlign)250 TEST(MathExtras, MinAlign) {
251   EXPECT_EQ(1u, MinAlign(2, 3));
252   EXPECT_EQ(2u, MinAlign(2, 4));
253   EXPECT_EQ(1u, MinAlign(17, 64));
254   EXPECT_EQ(256u, MinAlign(256, 512));
255 }
256 
TEST(MathExtras,NextPowerOf2)257 TEST(MathExtras, NextPowerOf2) {
258   EXPECT_EQ(4u, NextPowerOf2(3));
259   EXPECT_EQ(16u, NextPowerOf2(15));
260   EXPECT_EQ(256u, NextPowerOf2(128));
261 }
262 
TEST(MathExtras,alignTo)263 TEST(MathExtras, alignTo) {
264   EXPECT_EQ(8u, alignTo(5, 8));
265   EXPECT_EQ(24u, alignTo(17, 8));
266   EXPECT_EQ(0u, alignTo(~0LL, 8));
267 
268   EXPECT_EQ(7u, alignTo(5, 8, 7));
269   EXPECT_EQ(17u, alignTo(17, 8, 1));
270   EXPECT_EQ(3u, alignTo(~0LL, 8, 3));
271   EXPECT_EQ(552u, alignTo(321, 255, 42));
272 }
273 
274 template<typename T>
SaturatingAddTestHelper()275 void SaturatingAddTestHelper()
276 {
277   const T Max = std::numeric_limits<T>::max();
278   bool ResultOverflowed;
279 
280   EXPECT_EQ(T(3), SaturatingAdd(T(1), T(2)));
281   EXPECT_EQ(T(3), SaturatingAdd(T(1), T(2), &ResultOverflowed));
282   EXPECT_FALSE(ResultOverflowed);
283 
284   EXPECT_EQ(Max, SaturatingAdd(Max, T(1)));
285   EXPECT_EQ(Max, SaturatingAdd(Max, T(1), &ResultOverflowed));
286   EXPECT_TRUE(ResultOverflowed);
287 
288   EXPECT_EQ(Max, SaturatingAdd(T(1), T(Max - 1)));
289   EXPECT_EQ(Max, SaturatingAdd(T(1), T(Max - 1), &ResultOverflowed));
290   EXPECT_FALSE(ResultOverflowed);
291 
292   EXPECT_EQ(Max, SaturatingAdd(T(1), Max));
293   EXPECT_EQ(Max, SaturatingAdd(T(1), Max, &ResultOverflowed));
294   EXPECT_TRUE(ResultOverflowed);
295 
296   EXPECT_EQ(Max, SaturatingAdd(Max, Max));
297   EXPECT_EQ(Max, SaturatingAdd(Max, Max, &ResultOverflowed));
298   EXPECT_TRUE(ResultOverflowed);
299 }
300 
TEST(MathExtras,SaturatingAdd)301 TEST(MathExtras, SaturatingAdd) {
302   SaturatingAddTestHelper<uint8_t>();
303   SaturatingAddTestHelper<uint16_t>();
304   SaturatingAddTestHelper<uint32_t>();
305   SaturatingAddTestHelper<uint64_t>();
306 }
307 
308 template<typename T>
SaturatingMultiplyTestHelper()309 void SaturatingMultiplyTestHelper()
310 {
311   const T Max = std::numeric_limits<T>::max();
312   bool ResultOverflowed;
313 
314   // Test basic multiplication.
315   EXPECT_EQ(T(6), SaturatingMultiply(T(2), T(3)));
316   EXPECT_EQ(T(6), SaturatingMultiply(T(2), T(3), &ResultOverflowed));
317   EXPECT_FALSE(ResultOverflowed);
318 
319   EXPECT_EQ(T(6), SaturatingMultiply(T(3), T(2)));
320   EXPECT_EQ(T(6), SaturatingMultiply(T(3), T(2), &ResultOverflowed));
321   EXPECT_FALSE(ResultOverflowed);
322 
323   // Test multiplication by zero.
324   EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(0)));
325   EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(0), &ResultOverflowed));
326   EXPECT_FALSE(ResultOverflowed);
327 
328   EXPECT_EQ(T(0), SaturatingMultiply(T(1), T(0)));
329   EXPECT_EQ(T(0), SaturatingMultiply(T(1), T(0), &ResultOverflowed));
330   EXPECT_FALSE(ResultOverflowed);
331 
332   EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(1)));
333   EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(1), &ResultOverflowed));
334   EXPECT_FALSE(ResultOverflowed);
335 
336   EXPECT_EQ(T(0), SaturatingMultiply(Max, T(0)));
337   EXPECT_EQ(T(0), SaturatingMultiply(Max, T(0), &ResultOverflowed));
338   EXPECT_FALSE(ResultOverflowed);
339 
340   EXPECT_EQ(T(0), SaturatingMultiply(T(0), Max));
341   EXPECT_EQ(T(0), SaturatingMultiply(T(0), Max, &ResultOverflowed));
342   EXPECT_FALSE(ResultOverflowed);
343 
344   // Test multiplication by maximum value.
345   EXPECT_EQ(Max, SaturatingMultiply(Max, T(2)));
346   EXPECT_EQ(Max, SaturatingMultiply(Max, T(2), &ResultOverflowed));
347   EXPECT_TRUE(ResultOverflowed);
348 
349   EXPECT_EQ(Max, SaturatingMultiply(T(2), Max));
350   EXPECT_EQ(Max, SaturatingMultiply(T(2), Max, &ResultOverflowed));
351   EXPECT_TRUE(ResultOverflowed);
352 
353   EXPECT_EQ(Max, SaturatingMultiply(Max, Max));
354   EXPECT_EQ(Max, SaturatingMultiply(Max, Max, &ResultOverflowed));
355   EXPECT_TRUE(ResultOverflowed);
356 
357   // Test interesting boundary conditions for algorithm -
358   // ((1 << A) - 1) * ((1 << B) + K) for K in [-1, 0, 1]
359   // and A + B == std::numeric_limits<T>::digits.
360   // We expect overflow iff A > B and K = 1.
361   const int Digits = std::numeric_limits<T>::digits;
362   for (int A = 1, B = Digits - 1; B >= 1; ++A, --B) {
363     for (int K = -1; K <= 1; ++K) {
364       T X = (T(1) << A) - T(1);
365       T Y = (T(1) << B) + K;
366       bool OverflowExpected = A > B && K == 1;
367 
368       if(OverflowExpected) {
369         EXPECT_EQ(Max, SaturatingMultiply(X, Y));
370         EXPECT_EQ(Max, SaturatingMultiply(X, Y, &ResultOverflowed));
371         EXPECT_TRUE(ResultOverflowed);
372       } else {
373         EXPECT_EQ(X * Y, SaturatingMultiply(X, Y));
374         EXPECT_EQ(X * Y, SaturatingMultiply(X, Y, &ResultOverflowed));
375         EXPECT_FALSE(ResultOverflowed);
376       }
377     }
378   }
379 }
380 
TEST(MathExtras,SaturatingMultiply)381 TEST(MathExtras, SaturatingMultiply) {
382   SaturatingMultiplyTestHelper<uint8_t>();
383   SaturatingMultiplyTestHelper<uint16_t>();
384   SaturatingMultiplyTestHelper<uint32_t>();
385   SaturatingMultiplyTestHelper<uint64_t>();
386 }
387 
388 template<typename T>
SaturatingMultiplyAddTestHelper()389 void SaturatingMultiplyAddTestHelper()
390 {
391   const T Max = std::numeric_limits<T>::max();
392   bool ResultOverflowed;
393 
394   // Test basic multiply-add.
395   EXPECT_EQ(T(16), SaturatingMultiplyAdd(T(2), T(3), T(10)));
396   EXPECT_EQ(T(16), SaturatingMultiplyAdd(T(2), T(3), T(10), &ResultOverflowed));
397   EXPECT_FALSE(ResultOverflowed);
398 
399   // Test multiply overflows, add doesn't overflow
400   EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, T(0), &ResultOverflowed));
401   EXPECT_TRUE(ResultOverflowed);
402 
403   // Test multiply doesn't overflow, add overflows
404   EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), T(1), Max, &ResultOverflowed));
405   EXPECT_TRUE(ResultOverflowed);
406 
407   // Test multiply-add with Max as operand
408   EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), T(1), Max, &ResultOverflowed));
409   EXPECT_TRUE(ResultOverflowed);
410 
411   EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), Max, T(1), &ResultOverflowed));
412   EXPECT_TRUE(ResultOverflowed);
413 
414   EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, T(1), &ResultOverflowed));
415   EXPECT_TRUE(ResultOverflowed);
416 
417   EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, Max, &ResultOverflowed));
418   EXPECT_TRUE(ResultOverflowed);
419 
420   // Test multiply-add with 0 as operand
421   EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(1), T(0), &ResultOverflowed));
422   EXPECT_FALSE(ResultOverflowed);
423 
424   EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(0), T(1), &ResultOverflowed));
425   EXPECT_FALSE(ResultOverflowed);
426 
427   EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(0), T(0), T(1), &ResultOverflowed));
428   EXPECT_FALSE(ResultOverflowed);
429 
430   EXPECT_EQ(T(0), SaturatingMultiplyAdd(T(0), T(0), T(0), &ResultOverflowed));
431   EXPECT_FALSE(ResultOverflowed);
432 
433 }
434 
TEST(MathExtras,SaturatingMultiplyAdd)435 TEST(MathExtras, SaturatingMultiplyAdd) {
436   SaturatingMultiplyAddTestHelper<uint8_t>();
437   SaturatingMultiplyAddTestHelper<uint16_t>();
438   SaturatingMultiplyAddTestHelper<uint32_t>();
439   SaturatingMultiplyAddTestHelper<uint64_t>();
440 }
441 
TEST(MathExtras,IsShiftedUInt)442 TEST(MathExtras, IsShiftedUInt) {
443   EXPECT_TRUE((isShiftedUInt<1, 0>(0)));
444   EXPECT_TRUE((isShiftedUInt<1, 0>(1)));
445   EXPECT_FALSE((isShiftedUInt<1, 0>(2)));
446   EXPECT_FALSE((isShiftedUInt<1, 0>(3)));
447   EXPECT_FALSE((isShiftedUInt<1, 0>(0x8000000000000000)));
448   EXPECT_TRUE((isShiftedUInt<1, 63>(0x8000000000000000)));
449   EXPECT_TRUE((isShiftedUInt<2, 62>(0xC000000000000000)));
450   EXPECT_FALSE((isShiftedUInt<2, 62>(0xE000000000000000)));
451 
452   // 0x201 is ten bits long and has a 1 in the MSB and LSB.
453   EXPECT_TRUE((isShiftedUInt<10, 5>(uint64_t(0x201) << 5)));
454   EXPECT_FALSE((isShiftedUInt<10, 5>(uint64_t(0x201) << 4)));
455   EXPECT_FALSE((isShiftedUInt<10, 5>(uint64_t(0x201) << 6)));
456 }
457 
TEST(MathExtras,IsShiftedInt)458 TEST(MathExtras, IsShiftedInt) {
459   EXPECT_TRUE((isShiftedInt<1, 0>(0)));
460   EXPECT_TRUE((isShiftedInt<1, 0>(-1)));
461   EXPECT_FALSE((isShiftedInt<1, 0>(2)));
462   EXPECT_FALSE((isShiftedInt<1, 0>(3)));
463   EXPECT_FALSE((isShiftedInt<1, 0>(0x8000000000000000)));
464   EXPECT_TRUE((isShiftedInt<1, 63>(0x8000000000000000)));
465   EXPECT_TRUE((isShiftedInt<2, 62>(0xC000000000000000)));
466   EXPECT_FALSE((isShiftedInt<2, 62>(0xE000000000000000)));
467 
468   // 0x201 is ten bits long and has a 1 in the MSB and LSB.
469   EXPECT_TRUE((isShiftedInt<11, 5>(int64_t(0x201) << 5)));
470   EXPECT_FALSE((isShiftedInt<11, 5>(int64_t(0x201) << 3)));
471   EXPECT_FALSE((isShiftedInt<11, 5>(int64_t(0x201) << 6)));
472   EXPECT_TRUE((isShiftedInt<11, 5>(-(int64_t(0x201) << 5))));
473   EXPECT_FALSE((isShiftedInt<11, 5>(-(int64_t(0x201) << 3))));
474   EXPECT_FALSE((isShiftedInt<11, 5>(-(int64_t(0x201) << 6))));
475 
476   EXPECT_TRUE((isShiftedInt<6, 10>(-(int64_t(1) << 15))));
477   EXPECT_FALSE((isShiftedInt<6, 10>(int64_t(1) << 15)));
478 }
479 
480 template <typename T>
481 class OverflowTest : public ::testing::Test { };
482 
483 using OverflowTestTypes = ::testing::Types<signed char, short, int, long,
484                                            long long>;
485 
486 TYPED_TEST_CASE(OverflowTest, OverflowTestTypes);
487 
TYPED_TEST(OverflowTest,AddNoOverflow)488 TYPED_TEST(OverflowTest, AddNoOverflow) {
489   TypeParam Result;
490   EXPECT_FALSE(AddOverflow<TypeParam>(1, 2, Result));
491   EXPECT_EQ(Result, TypeParam(3));
492 }
493 
TYPED_TEST(OverflowTest,AddOverflowToNegative)494 TYPED_TEST(OverflowTest, AddOverflowToNegative) {
495   TypeParam Result;
496   auto MaxValue = std::numeric_limits<TypeParam>::max();
497   EXPECT_TRUE(AddOverflow<TypeParam>(MaxValue, MaxValue, Result));
498   EXPECT_EQ(Result, TypeParam(-2));
499 }
500 
TYPED_TEST(OverflowTest,AddOverflowToMin)501 TYPED_TEST(OverflowTest, AddOverflowToMin) {
502   TypeParam Result;
503   auto MaxValue = std::numeric_limits<TypeParam>::max();
504   EXPECT_TRUE(AddOverflow<TypeParam>(MaxValue, TypeParam(1), Result));
505   EXPECT_EQ(Result, std::numeric_limits<TypeParam>::min());
506 }
507 
TYPED_TEST(OverflowTest,AddOverflowToZero)508 TYPED_TEST(OverflowTest, AddOverflowToZero) {
509   TypeParam Result;
510   auto MinValue = std::numeric_limits<TypeParam>::min();
511   EXPECT_TRUE(AddOverflow<TypeParam>(MinValue, MinValue, Result));
512   EXPECT_EQ(Result, TypeParam(0));
513 }
514 
TYPED_TEST(OverflowTest,AddOverflowToMax)515 TYPED_TEST(OverflowTest, AddOverflowToMax) {
516   TypeParam Result;
517   auto MinValue = std::numeric_limits<TypeParam>::min();
518   EXPECT_TRUE(AddOverflow<TypeParam>(MinValue, TypeParam(-1), Result));
519   EXPECT_EQ(Result, std::numeric_limits<TypeParam>::max());
520 }
521 
TYPED_TEST(OverflowTest,SubNoOverflow)522 TYPED_TEST(OverflowTest, SubNoOverflow) {
523   TypeParam Result;
524   EXPECT_FALSE(SubOverflow<TypeParam>(1, 2, Result));
525   EXPECT_EQ(Result, TypeParam(-1));
526 }
527 
TYPED_TEST(OverflowTest,SubOverflowToMax)528 TYPED_TEST(OverflowTest, SubOverflowToMax) {
529   TypeParam Result;
530   auto MinValue = std::numeric_limits<TypeParam>::min();
531   EXPECT_TRUE(SubOverflow<TypeParam>(0, MinValue, Result));
532   EXPECT_EQ(Result, MinValue);
533 }
534 
TYPED_TEST(OverflowTest,SubOverflowToMin)535 TYPED_TEST(OverflowTest, SubOverflowToMin) {
536   TypeParam Result;
537   auto MinValue = std::numeric_limits<TypeParam>::min();
538   EXPECT_TRUE(SubOverflow<TypeParam>(0, MinValue, Result));
539   EXPECT_EQ(Result, MinValue);
540 }
541 
TYPED_TEST(OverflowTest,SubOverflowToNegative)542 TYPED_TEST(OverflowTest, SubOverflowToNegative) {
543   TypeParam Result;
544   auto MaxValue = std::numeric_limits<TypeParam>::max();
545   auto MinValue = std::numeric_limits<TypeParam>::min();
546   EXPECT_TRUE(SubOverflow<TypeParam>(MaxValue, MinValue, Result));
547   EXPECT_EQ(Result, TypeParam(-1));
548 }
549 
TYPED_TEST(OverflowTest,SubOverflowToPositive)550 TYPED_TEST(OverflowTest, SubOverflowToPositive) {
551   TypeParam Result;
552   auto MaxValue = std::numeric_limits<TypeParam>::max();
553   auto MinValue = std::numeric_limits<TypeParam>::min();
554   EXPECT_TRUE(SubOverflow<TypeParam>(MinValue, MaxValue, Result));
555   EXPECT_EQ(Result, TypeParam(1));
556 }
557 
TYPED_TEST(OverflowTest,MulNoOverflow)558 TYPED_TEST(OverflowTest, MulNoOverflow) {
559   TypeParam Result;
560   EXPECT_FALSE(MulOverflow<TypeParam>(1, 2, Result));
561   EXPECT_EQ(Result, 2);
562   EXPECT_FALSE(MulOverflow<TypeParam>(-1, 3, Result));
563   EXPECT_EQ(Result, -3);
564   EXPECT_FALSE(MulOverflow<TypeParam>(4, -2, Result));
565   EXPECT_EQ(Result, -8);
566   EXPECT_FALSE(MulOverflow<TypeParam>(-6, -5, Result));
567   EXPECT_EQ(Result, 30);
568 }
569 
TYPED_TEST(OverflowTest,MulNoOverflowToMax)570 TYPED_TEST(OverflowTest, MulNoOverflowToMax) {
571   TypeParam Result;
572   auto MaxValue = std::numeric_limits<TypeParam>::max();
573   auto MinValue = std::numeric_limits<TypeParam>::min();
574   EXPECT_FALSE(MulOverflow<TypeParam>(MinValue + 1, -1, Result));
575   EXPECT_EQ(Result, MaxValue);
576 }
577 
TYPED_TEST(OverflowTest,MulOverflowToMin)578 TYPED_TEST(OverflowTest, MulOverflowToMin) {
579   TypeParam Result;
580   auto MinValue = std::numeric_limits<TypeParam>::min();
581   EXPECT_TRUE(MulOverflow<TypeParam>(MinValue, -1, Result));
582   EXPECT_EQ(Result, MinValue);
583 }
584 
TYPED_TEST(OverflowTest,MulOverflowMax)585 TYPED_TEST(OverflowTest, MulOverflowMax) {
586   TypeParam Result;
587   auto MinValue = std::numeric_limits<TypeParam>::min();
588   auto MaxValue = std::numeric_limits<TypeParam>::max();
589   EXPECT_TRUE(MulOverflow<TypeParam>(MinValue, MinValue, Result));
590   EXPECT_EQ(Result, 0);
591   EXPECT_TRUE(MulOverflow<TypeParam>(MaxValue, MaxValue, Result));
592   EXPECT_EQ(Result, 1);
593 }
594 
TYPED_TEST(OverflowTest,MulResultZero)595 TYPED_TEST(OverflowTest, MulResultZero) {
596   TypeParam Result;
597   EXPECT_FALSE(MulOverflow<TypeParam>(4, 0, Result));
598   EXPECT_EQ(Result, TypeParam(0));
599   EXPECT_FALSE(MulOverflow<TypeParam>(-5, 0, Result));
600   EXPECT_EQ(Result, TypeParam(0));
601   EXPECT_FALSE(MulOverflow<TypeParam>(0, 5, Result));
602   EXPECT_EQ(Result, TypeParam(0));
603   EXPECT_FALSE(MulOverflow<TypeParam>(0, -5, Result));
604   EXPECT_EQ(Result, TypeParam(0));
605 }
606 
607 } // namespace
608