1 //===- unittests/Support/MathExtrasTest.cpp - math utils tests ------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/MathExtras.h"
10 #include "gtest/gtest.h"
16 TEST(MathExtras
, countTrailingZeros
) {
21 EXPECT_EQ(8u, countTrailingZeros(Z8
));
22 EXPECT_EQ(16u, countTrailingZeros(Z16
));
23 EXPECT_EQ(32u, countTrailingZeros(Z32
));
24 EXPECT_EQ(64u, countTrailingZeros(Z64
));
30 EXPECT_EQ(1u, countTrailingZeros(NZ8
));
31 EXPECT_EQ(1u, countTrailingZeros(NZ16
));
32 EXPECT_EQ(1u, countTrailingZeros(NZ32
));
33 EXPECT_EQ(1u, countTrailingZeros(NZ64
));
36 TEST(MathExtras
, countLeadingZeros
) {
41 EXPECT_EQ(8u, countLeadingZeros(Z8
));
42 EXPECT_EQ(16u, countLeadingZeros(Z16
));
43 EXPECT_EQ(32u, countLeadingZeros(Z32
));
44 EXPECT_EQ(64u, countLeadingZeros(Z64
));
50 EXPECT_EQ(2u, countLeadingZeros(NZ8
));
51 EXPECT_EQ(10u, countLeadingZeros(NZ16
));
52 EXPECT_EQ(26u, countLeadingZeros(NZ32
));
53 EXPECT_EQ(58u, countLeadingZeros(NZ64
));
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
));
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
));
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));
78 EXPECT_EQ(0x00000003U
, maskTrailingOnes
<uint32_t>(2U));
79 EXPECT_EQ(0xC0000000U
, maskLeadingOnes
<uint32_t>(2U));
81 EXPECT_EQ(0x000007FFU
, maskTrailingOnes
<uint32_t>(11U));
82 EXPECT_EQ(0xFFE00000U
, maskLeadingOnes
<uint32_t>(11U));
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));
89 EXPECT_EQ(0x0000FFFFFFFFFFFFULL
, maskTrailingOnes
<uint64_t>(48U));
90 EXPECT_EQ(0xFFFFFFFFFFFF0000ULL
, maskLeadingOnes
<uint64_t>(48U));
93 TEST(MathExtras
, findFirstSet
) {
98 EXPECT_EQ(0xFFULL
, findFirstSet(Z8
));
99 EXPECT_EQ(0xFFFFULL
, findFirstSet(Z16
));
100 EXPECT_EQ(0xFFFFFFFFULL
, findFirstSet(Z32
));
101 EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL
, findFirstSet(Z64
));
107 EXPECT_EQ(1u, findFirstSet(NZ8
));
108 EXPECT_EQ(1u, findFirstSet(NZ16
));
109 EXPECT_EQ(1u, findFirstSet(NZ32
));
110 EXPECT_EQ(1u, findFirstSet(NZ64
));
113 TEST(MathExtras
, findLastSet
) {
118 EXPECT_EQ(0xFFULL
, findLastSet(Z8
));
119 EXPECT_EQ(0xFFFFULL
, findLastSet(Z16
));
120 EXPECT_EQ(0xFFFFFFFFULL
, findLastSet(Z32
));
121 EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL
, findLastSet(Z64
));
127 EXPECT_EQ(5u, findLastSet(NZ8
));
128 EXPECT_EQ(5u, findLastSet(NZ16
));
129 EXPECT_EQ(5u, findLastSet(NZ32
));
130 EXPECT_EQ(5u, findLastSet(NZ64
));
133 TEST(MathExtras
, isIntN
) {
134 EXPECT_TRUE(isIntN(16, 32767));
135 EXPECT_FALSE(isIntN(16, 32768));
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));
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));
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));
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));
167 TEST(MathExtras
, reverseBits
) {
172 EXPECT_EQ(0x54ULL
, reverseBits(NZ8
));
173 EXPECT_EQ(0x5400ULL
, reverseBits(NZ16
));
174 EXPECT_EQ(0x54000000ULL
, reverseBits(NZ32
));
175 EXPECT_EQ(0x5400000000000000ULL
, reverseBits(NZ64
));
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));
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
));
194 TEST(MathExtras
, PowerOf2Ceil
) {
195 EXPECT_EQ(0U, PowerOf2Ceil(0U));
196 EXPECT_EQ(8U, PowerOf2Ceil(8U));
197 EXPECT_EQ(8U, PowerOf2Ceil(7U));
200 TEST(MathExtras
, PowerOf2Floor
) {
201 EXPECT_EQ(0U, PowerOf2Floor(0U));
202 EXPECT_EQ(8U, PowerOf2Floor(8U));
203 EXPECT_EQ(4U, PowerOf2Floor(7U));
206 TEST(MathExtras
, CTLog2
) {
207 EXPECT_EQ(CTLog2
<1ULL << 0>(), 0);
208 EXPECT_EQ(CTLog2
<1ULL << 1>(), 1);
209 EXPECT_EQ(CTLog2
<1ULL << 2>(), 2);
210 EXPECT_EQ(CTLog2
<1ULL << 3>(), 3);
211 EXPECT_EQ(CTLog2
<1ULL << 4>(), 4);
212 EXPECT_EQ(CTLog2
<1ULL << 5>(), 5);
213 EXPECT_EQ(CTLog2
<1ULL << 6>(), 6);
214 EXPECT_EQ(CTLog2
<1ULL << 7>(), 7);
215 EXPECT_EQ(CTLog2
<1ULL << 8>(), 8);
216 EXPECT_EQ(CTLog2
<1ULL << 9>(), 9);
217 EXPECT_EQ(CTLog2
<1ULL << 10>(), 10);
218 EXPECT_EQ(CTLog2
<1ULL << 11>(), 11);
219 EXPECT_EQ(CTLog2
<1ULL << 12>(), 12);
220 EXPECT_EQ(CTLog2
<1ULL << 13>(), 13);
221 EXPECT_EQ(CTLog2
<1ULL << 14>(), 14);
222 EXPECT_EQ(CTLog2
<1ULL << 15>(), 15);
225 TEST(MathExtras
, ByteSwap_32
) {
226 EXPECT_EQ(0x44332211u
, ByteSwap_32(0x11223344));
227 EXPECT_EQ(0xDDCCBBAAu
, ByteSwap_32(0xAABBCCDD));
230 TEST(MathExtras
, ByteSwap_64
) {
231 EXPECT_EQ(0x8877665544332211ULL
, ByteSwap_64(0x1122334455667788LL
));
232 EXPECT_EQ(0x1100FFEEDDCCBBAAULL
, ByteSwap_64(0xAABBCCDDEEFF0011LL
));
235 TEST(MathExtras
, countLeadingOnes
) {
236 for (int i
= 30; i
>= 0; --i
) {
237 // Start with all ones and unset some bit.
238 EXPECT_EQ(31u - i
, countLeadingOnes(0xFFFFFFFF ^ (1 << i
)));
240 for (int i
= 62; i
>= 0; --i
) {
241 // Start with all ones and unset some bit.
242 EXPECT_EQ(63u - i
, countLeadingOnes(0xFFFFFFFFFFFFFFFFULL
^ (1LL << i
)));
244 for (int i
= 30; i
>= 0; --i
) {
245 // Start with all ones and unset some bit.
246 EXPECT_EQ(31u - i
, countLeadingOnes(0xFFFFFFFF ^ (1 << i
)));
250 TEST(MathExtras
, FloatBits
) {
251 static const float kValue
= 5632.34f
;
252 EXPECT_FLOAT_EQ(kValue
, BitsToFloat(FloatToBits(kValue
)));
255 TEST(MathExtras
, DoubleBits
) {
256 static const double kValue
= 87987234.983498;
257 EXPECT_DOUBLE_EQ(kValue
, BitsToDouble(DoubleToBits(kValue
)));
260 TEST(MathExtras
, MinAlign
) {
261 EXPECT_EQ(1u, MinAlign(2, 3));
262 EXPECT_EQ(2u, MinAlign(2, 4));
263 EXPECT_EQ(1u, MinAlign(17, 64));
264 EXPECT_EQ(256u, MinAlign(256, 512));
267 TEST(MathExtras
, NextPowerOf2
) {
268 EXPECT_EQ(4u, NextPowerOf2(3));
269 EXPECT_EQ(16u, NextPowerOf2(15));
270 EXPECT_EQ(256u, NextPowerOf2(128));
273 TEST(MathExtras
, alignTo
) {
274 EXPECT_EQ(8u, alignTo(5, 8));
275 EXPECT_EQ(24u, alignTo(17, 8));
276 EXPECT_EQ(0u, alignTo(~0LL, 8));
278 EXPECT_EQ(7u, alignTo(5, 8, 7));
279 EXPECT_EQ(17u, alignTo(17, 8, 1));
280 EXPECT_EQ(3u, alignTo(~0LL, 8, 3));
281 EXPECT_EQ(552u, alignTo(321, 255, 42));
285 void SaturatingAddTestHelper()
287 const T Max
= std::numeric_limits
<T
>::max();
288 bool ResultOverflowed
;
290 EXPECT_EQ(T(3), SaturatingAdd(T(1), T(2)));
291 EXPECT_EQ(T(3), SaturatingAdd(T(1), T(2), &ResultOverflowed
));
292 EXPECT_FALSE(ResultOverflowed
);
294 EXPECT_EQ(Max
, SaturatingAdd(Max
, T(1)));
295 EXPECT_EQ(Max
, SaturatingAdd(Max
, T(1), &ResultOverflowed
));
296 EXPECT_TRUE(ResultOverflowed
);
298 EXPECT_EQ(Max
, SaturatingAdd(T(1), T(Max
- 1)));
299 EXPECT_EQ(Max
, SaturatingAdd(T(1), T(Max
- 1), &ResultOverflowed
));
300 EXPECT_FALSE(ResultOverflowed
);
302 EXPECT_EQ(Max
, SaturatingAdd(T(1), Max
));
303 EXPECT_EQ(Max
, SaturatingAdd(T(1), Max
, &ResultOverflowed
));
304 EXPECT_TRUE(ResultOverflowed
);
306 EXPECT_EQ(Max
, SaturatingAdd(Max
, Max
));
307 EXPECT_EQ(Max
, SaturatingAdd(Max
, Max
, &ResultOverflowed
));
308 EXPECT_TRUE(ResultOverflowed
);
311 TEST(MathExtras
, SaturatingAdd
) {
312 SaturatingAddTestHelper
<uint8_t>();
313 SaturatingAddTestHelper
<uint16_t>();
314 SaturatingAddTestHelper
<uint32_t>();
315 SaturatingAddTestHelper
<uint64_t>();
319 void SaturatingMultiplyTestHelper()
321 const T Max
= std::numeric_limits
<T
>::max();
322 bool ResultOverflowed
;
324 // Test basic multiplication.
325 EXPECT_EQ(T(6), SaturatingMultiply(T(2), T(3)));
326 EXPECT_EQ(T(6), SaturatingMultiply(T(2), T(3), &ResultOverflowed
));
327 EXPECT_FALSE(ResultOverflowed
);
329 EXPECT_EQ(T(6), SaturatingMultiply(T(3), T(2)));
330 EXPECT_EQ(T(6), SaturatingMultiply(T(3), T(2), &ResultOverflowed
));
331 EXPECT_FALSE(ResultOverflowed
);
333 // Test multiplication by zero.
334 EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(0)));
335 EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(0), &ResultOverflowed
));
336 EXPECT_FALSE(ResultOverflowed
);
338 EXPECT_EQ(T(0), SaturatingMultiply(T(1), T(0)));
339 EXPECT_EQ(T(0), SaturatingMultiply(T(1), T(0), &ResultOverflowed
));
340 EXPECT_FALSE(ResultOverflowed
);
342 EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(1)));
343 EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(1), &ResultOverflowed
));
344 EXPECT_FALSE(ResultOverflowed
);
346 EXPECT_EQ(T(0), SaturatingMultiply(Max
, T(0)));
347 EXPECT_EQ(T(0), SaturatingMultiply(Max
, T(0), &ResultOverflowed
));
348 EXPECT_FALSE(ResultOverflowed
);
350 EXPECT_EQ(T(0), SaturatingMultiply(T(0), Max
));
351 EXPECT_EQ(T(0), SaturatingMultiply(T(0), Max
, &ResultOverflowed
));
352 EXPECT_FALSE(ResultOverflowed
);
354 // Test multiplication by maximum value.
355 EXPECT_EQ(Max
, SaturatingMultiply(Max
, T(2)));
356 EXPECT_EQ(Max
, SaturatingMultiply(Max
, T(2), &ResultOverflowed
));
357 EXPECT_TRUE(ResultOverflowed
);
359 EXPECT_EQ(Max
, SaturatingMultiply(T(2), Max
));
360 EXPECT_EQ(Max
, SaturatingMultiply(T(2), Max
, &ResultOverflowed
));
361 EXPECT_TRUE(ResultOverflowed
);
363 EXPECT_EQ(Max
, SaturatingMultiply(Max
, Max
));
364 EXPECT_EQ(Max
, SaturatingMultiply(Max
, Max
, &ResultOverflowed
));
365 EXPECT_TRUE(ResultOverflowed
);
367 // Test interesting boundary conditions for algorithm -
368 // ((1 << A) - 1) * ((1 << B) + K) for K in [-1, 0, 1]
369 // and A + B == std::numeric_limits<T>::digits.
370 // We expect overflow iff A > B and K = 1.
371 const int Digits
= std::numeric_limits
<T
>::digits
;
372 for (int A
= 1, B
= Digits
- 1; B
>= 1; ++A
, --B
) {
373 for (int K
= -1; K
<= 1; ++K
) {
374 T X
= (T(1) << A
) - T(1);
375 T Y
= (T(1) << B
) + K
;
376 bool OverflowExpected
= A
> B
&& K
== 1;
378 if(OverflowExpected
) {
379 EXPECT_EQ(Max
, SaturatingMultiply(X
, Y
));
380 EXPECT_EQ(Max
, SaturatingMultiply(X
, Y
, &ResultOverflowed
));
381 EXPECT_TRUE(ResultOverflowed
);
383 EXPECT_EQ(X
* Y
, SaturatingMultiply(X
, Y
));
384 EXPECT_EQ(X
* Y
, SaturatingMultiply(X
, Y
, &ResultOverflowed
));
385 EXPECT_FALSE(ResultOverflowed
);
391 TEST(MathExtras
, SaturatingMultiply
) {
392 SaturatingMultiplyTestHelper
<uint8_t>();
393 SaturatingMultiplyTestHelper
<uint16_t>();
394 SaturatingMultiplyTestHelper
<uint32_t>();
395 SaturatingMultiplyTestHelper
<uint64_t>();
399 void SaturatingMultiplyAddTestHelper()
401 const T Max
= std::numeric_limits
<T
>::max();
402 bool ResultOverflowed
;
404 // Test basic multiply-add.
405 EXPECT_EQ(T(16), SaturatingMultiplyAdd(T(2), T(3), T(10)));
406 EXPECT_EQ(T(16), SaturatingMultiplyAdd(T(2), T(3), T(10), &ResultOverflowed
));
407 EXPECT_FALSE(ResultOverflowed
);
409 // Test multiply overflows, add doesn't overflow
410 EXPECT_EQ(Max
, SaturatingMultiplyAdd(Max
, Max
, T(0), &ResultOverflowed
));
411 EXPECT_TRUE(ResultOverflowed
);
413 // Test multiply doesn't overflow, add overflows
414 EXPECT_EQ(Max
, SaturatingMultiplyAdd(T(1), T(1), Max
, &ResultOverflowed
));
415 EXPECT_TRUE(ResultOverflowed
);
417 // Test multiply-add with Max as operand
418 EXPECT_EQ(Max
, SaturatingMultiplyAdd(T(1), T(1), Max
, &ResultOverflowed
));
419 EXPECT_TRUE(ResultOverflowed
);
421 EXPECT_EQ(Max
, SaturatingMultiplyAdd(T(1), Max
, T(1), &ResultOverflowed
));
422 EXPECT_TRUE(ResultOverflowed
);
424 EXPECT_EQ(Max
, SaturatingMultiplyAdd(Max
, Max
, T(1), &ResultOverflowed
));
425 EXPECT_TRUE(ResultOverflowed
);
427 EXPECT_EQ(Max
, SaturatingMultiplyAdd(Max
, Max
, Max
, &ResultOverflowed
));
428 EXPECT_TRUE(ResultOverflowed
);
430 // Test multiply-add with 0 as operand
431 EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(1), T(0), &ResultOverflowed
));
432 EXPECT_FALSE(ResultOverflowed
);
434 EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(0), T(1), &ResultOverflowed
));
435 EXPECT_FALSE(ResultOverflowed
);
437 EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(0), T(0), T(1), &ResultOverflowed
));
438 EXPECT_FALSE(ResultOverflowed
);
440 EXPECT_EQ(T(0), SaturatingMultiplyAdd(T(0), T(0), T(0), &ResultOverflowed
));
441 EXPECT_FALSE(ResultOverflowed
);
445 TEST(MathExtras
, SaturatingMultiplyAdd
) {
446 SaturatingMultiplyAddTestHelper
<uint8_t>();
447 SaturatingMultiplyAddTestHelper
<uint16_t>();
448 SaturatingMultiplyAddTestHelper
<uint32_t>();
449 SaturatingMultiplyAddTestHelper
<uint64_t>();
452 TEST(MathExtras
, IsShiftedUInt
) {
453 EXPECT_TRUE((isShiftedUInt
<1, 0>(0)));
454 EXPECT_TRUE((isShiftedUInt
<1, 0>(1)));
455 EXPECT_FALSE((isShiftedUInt
<1, 0>(2)));
456 EXPECT_FALSE((isShiftedUInt
<1, 0>(3)));
457 EXPECT_FALSE((isShiftedUInt
<1, 0>(0x8000000000000000)));
458 EXPECT_TRUE((isShiftedUInt
<1, 63>(0x8000000000000000)));
459 EXPECT_TRUE((isShiftedUInt
<2, 62>(0xC000000000000000)));
460 EXPECT_FALSE((isShiftedUInt
<2, 62>(0xE000000000000000)));
462 // 0x201 is ten bits long and has a 1 in the MSB and LSB.
463 EXPECT_TRUE((isShiftedUInt
<10, 5>(uint64_t(0x201) << 5)));
464 EXPECT_FALSE((isShiftedUInt
<10, 5>(uint64_t(0x201) << 4)));
465 EXPECT_FALSE((isShiftedUInt
<10, 5>(uint64_t(0x201) << 6)));
468 TEST(MathExtras
, IsShiftedInt
) {
469 EXPECT_TRUE((isShiftedInt
<1, 0>(0)));
470 EXPECT_TRUE((isShiftedInt
<1, 0>(-1)));
471 EXPECT_FALSE((isShiftedInt
<1, 0>(2)));
472 EXPECT_FALSE((isShiftedInt
<1, 0>(3)));
473 EXPECT_FALSE((isShiftedInt
<1, 0>(0x8000000000000000)));
474 EXPECT_TRUE((isShiftedInt
<1, 63>(0x8000000000000000)));
475 EXPECT_TRUE((isShiftedInt
<2, 62>(0xC000000000000000)));
476 EXPECT_FALSE((isShiftedInt
<2, 62>(0xE000000000000000)));
478 // 0x201 is ten bits long and has a 1 in the MSB and LSB.
479 EXPECT_TRUE((isShiftedInt
<11, 5>(int64_t(0x201) << 5)));
480 EXPECT_FALSE((isShiftedInt
<11, 5>(int64_t(0x201) << 3)));
481 EXPECT_FALSE((isShiftedInt
<11, 5>(int64_t(0x201) << 6)));
482 EXPECT_TRUE((isShiftedInt
<11, 5>(-(int64_t(0x201) << 5))));
483 EXPECT_FALSE((isShiftedInt
<11, 5>(-(int64_t(0x201) << 3))));
484 EXPECT_FALSE((isShiftedInt
<11, 5>(-(int64_t(0x201) << 6))));
486 EXPECT_TRUE((isShiftedInt
<6, 10>(-(int64_t(1) << 15))));
487 EXPECT_FALSE((isShiftedInt
<6, 10>(int64_t(1) << 15)));
490 template <typename T
>
491 class OverflowTest
: public ::testing::Test
{ };
493 using OverflowTestTypes
= ::testing::Types
<signed char, short, int, long,
496 TYPED_TEST_CASE(OverflowTest
, OverflowTestTypes
);
498 TYPED_TEST(OverflowTest
, AddNoOverflow
) {
500 EXPECT_FALSE(AddOverflow
<TypeParam
>(1, 2, Result
));
501 EXPECT_EQ(Result
, TypeParam(3));
504 TYPED_TEST(OverflowTest
, AddOverflowToNegative
) {
506 auto MaxValue
= std::numeric_limits
<TypeParam
>::max();
507 EXPECT_TRUE(AddOverflow
<TypeParam
>(MaxValue
, MaxValue
, Result
));
508 EXPECT_EQ(Result
, TypeParam(-2));
511 TYPED_TEST(OverflowTest
, AddOverflowToMin
) {
513 auto MaxValue
= std::numeric_limits
<TypeParam
>::max();
514 EXPECT_TRUE(AddOverflow
<TypeParam
>(MaxValue
, TypeParam(1), Result
));
515 EXPECT_EQ(Result
, std::numeric_limits
<TypeParam
>::min());
518 TYPED_TEST(OverflowTest
, AddOverflowToZero
) {
520 auto MinValue
= std::numeric_limits
<TypeParam
>::min();
521 EXPECT_TRUE(AddOverflow
<TypeParam
>(MinValue
, MinValue
, Result
));
522 EXPECT_EQ(Result
, TypeParam(0));
525 TYPED_TEST(OverflowTest
, AddOverflowToMax
) {
527 auto MinValue
= std::numeric_limits
<TypeParam
>::min();
528 EXPECT_TRUE(AddOverflow
<TypeParam
>(MinValue
, TypeParam(-1), Result
));
529 EXPECT_EQ(Result
, std::numeric_limits
<TypeParam
>::max());
532 TYPED_TEST(OverflowTest
, SubNoOverflow
) {
534 EXPECT_FALSE(SubOverflow
<TypeParam
>(1, 2, Result
));
535 EXPECT_EQ(Result
, TypeParam(-1));
538 TYPED_TEST(OverflowTest
, SubOverflowToMax
) {
540 auto MinValue
= std::numeric_limits
<TypeParam
>::min();
541 EXPECT_TRUE(SubOverflow
<TypeParam
>(0, MinValue
, Result
));
542 EXPECT_EQ(Result
, MinValue
);
545 TYPED_TEST(OverflowTest
, SubOverflowToMin
) {
547 auto MinValue
= std::numeric_limits
<TypeParam
>::min();
548 EXPECT_TRUE(SubOverflow
<TypeParam
>(0, MinValue
, Result
));
549 EXPECT_EQ(Result
, MinValue
);
552 TYPED_TEST(OverflowTest
, SubOverflowToNegative
) {
554 auto MaxValue
= std::numeric_limits
<TypeParam
>::max();
555 auto MinValue
= std::numeric_limits
<TypeParam
>::min();
556 EXPECT_TRUE(SubOverflow
<TypeParam
>(MaxValue
, MinValue
, Result
));
557 EXPECT_EQ(Result
, TypeParam(-1));
560 TYPED_TEST(OverflowTest
, SubOverflowToPositive
) {
562 auto MaxValue
= std::numeric_limits
<TypeParam
>::max();
563 auto MinValue
= std::numeric_limits
<TypeParam
>::min();
564 EXPECT_TRUE(SubOverflow
<TypeParam
>(MinValue
, MaxValue
, Result
));
565 EXPECT_EQ(Result
, TypeParam(1));
568 TYPED_TEST(OverflowTest
, MulNoOverflow
) {
570 EXPECT_FALSE(MulOverflow
<TypeParam
>(1, 2, Result
));
571 EXPECT_EQ(Result
, 2);
572 EXPECT_FALSE(MulOverflow
<TypeParam
>(-1, 3, Result
));
573 EXPECT_EQ(Result
, -3);
574 EXPECT_FALSE(MulOverflow
<TypeParam
>(4, -2, Result
));
575 EXPECT_EQ(Result
, -8);
576 EXPECT_FALSE(MulOverflow
<TypeParam
>(-6, -5, Result
));
577 EXPECT_EQ(Result
, 30);
580 TYPED_TEST(OverflowTest
, MulNoOverflowToMax
) {
582 auto MaxValue
= std::numeric_limits
<TypeParam
>::max();
583 auto MinValue
= std::numeric_limits
<TypeParam
>::min();
584 EXPECT_FALSE(MulOverflow
<TypeParam
>(MinValue
+ 1, -1, Result
));
585 EXPECT_EQ(Result
, MaxValue
);
588 TYPED_TEST(OverflowTest
, MulOverflowToMin
) {
590 auto MinValue
= std::numeric_limits
<TypeParam
>::min();
591 EXPECT_TRUE(MulOverflow
<TypeParam
>(MinValue
, -1, Result
));
592 EXPECT_EQ(Result
, MinValue
);
595 TYPED_TEST(OverflowTest
, MulOverflowMax
) {
597 auto MinValue
= std::numeric_limits
<TypeParam
>::min();
598 auto MaxValue
= std::numeric_limits
<TypeParam
>::max();
599 EXPECT_TRUE(MulOverflow
<TypeParam
>(MinValue
, MinValue
, Result
));
600 EXPECT_EQ(Result
, 0);
601 EXPECT_TRUE(MulOverflow
<TypeParam
>(MaxValue
, MaxValue
, Result
));
602 EXPECT_EQ(Result
, 1);
605 TYPED_TEST(OverflowTest
, MulResultZero
) {
607 EXPECT_FALSE(MulOverflow
<TypeParam
>(4, 0, Result
));
608 EXPECT_EQ(Result
, TypeParam(0));
609 EXPECT_FALSE(MulOverflow
<TypeParam
>(-5, 0, Result
));
610 EXPECT_EQ(Result
, TypeParam(0));
611 EXPECT_FALSE(MulOverflow
<TypeParam
>(0, 5, Result
));
612 EXPECT_EQ(Result
, TypeParam(0));
613 EXPECT_FALSE(MulOverflow
<TypeParam
>(0, -5, Result
));
614 EXPECT_EQ(Result
, TypeParam(0));