[docs] Fix build-docs.sh
[llvm-project.git] / llvm / unittests / Support / MathExtrasTest.cpp
blob23034d64028c64107ffef3a25cf7b09a21167644
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/MathExtras.h"
10 #include "gtest/gtest.h"
12 using namespace llvm;
14 namespace {
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));
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));
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));
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));
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) {
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));
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));
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));
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));
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) {
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));
178 TEST(MathExtras, isShiftedMask_32) {
179 EXPECT_FALSE(isShiftedMask_32(0x01010101));
180 EXPECT_TRUE(isShiftedMask_32(0xf0000000));
181 EXPECT_TRUE(isShiftedMask_32(0xffff0000));
182 EXPECT_TRUE(isShiftedMask_32(0xff << 1));
184 unsigned MaskIdx, MaskLen;
185 EXPECT_FALSE(isShiftedMask_32(0x01010101, MaskIdx, MaskLen));
186 EXPECT_TRUE(isShiftedMask_32(0xf0000000, MaskIdx, MaskLen));
187 EXPECT_EQ(28, (int)MaskIdx);
188 EXPECT_EQ(4, (int)MaskLen);
189 EXPECT_TRUE(isShiftedMask_32(0xffff0000, MaskIdx, MaskLen));
190 EXPECT_EQ(16, (int)MaskIdx);
191 EXPECT_EQ(16, (int)MaskLen);
192 EXPECT_TRUE(isShiftedMask_32(0xff << 1, MaskIdx, MaskLen));
193 EXPECT_EQ(1, (int)MaskIdx);
194 EXPECT_EQ(8, (int)MaskLen);
197 TEST(MathExtras, isShiftedMask_64) {
198 EXPECT_FALSE(isShiftedMask_64(0x0101010101010101ull));
199 EXPECT_TRUE(isShiftedMask_64(0xf000000000000000ull));
200 EXPECT_TRUE(isShiftedMask_64(0xffff000000000000ull));
201 EXPECT_TRUE(isShiftedMask_64(0xffull << 55));
203 unsigned MaskIdx, MaskLen;
204 EXPECT_FALSE(isShiftedMask_64(0x0101010101010101ull, MaskIdx, MaskLen));
205 EXPECT_TRUE(isShiftedMask_64(0xf000000000000000ull, MaskIdx, MaskLen));
206 EXPECT_EQ(60, (int)MaskIdx);
207 EXPECT_EQ(4, (int)MaskLen);
208 EXPECT_TRUE(isShiftedMask_64(0xffff000000000000ull, MaskIdx, MaskLen));
209 EXPECT_EQ(48, (int)MaskIdx);
210 EXPECT_EQ(16, (int)MaskLen);
211 EXPECT_TRUE(isShiftedMask_64(0xffull << 55, MaskIdx, MaskLen));
212 EXPECT_EQ(55, (int)MaskIdx);
213 EXPECT_EQ(8, (int)MaskLen);
216 TEST(MathExtras, isPowerOf2_32) {
217 EXPECT_FALSE(isPowerOf2_32(0));
218 EXPECT_TRUE(isPowerOf2_32(1 << 6));
219 EXPECT_TRUE(isPowerOf2_32(1 << 12));
220 EXPECT_FALSE(isPowerOf2_32((1 << 19) + 3));
221 EXPECT_FALSE(isPowerOf2_32(0xABCDEF0));
224 TEST(MathExtras, isPowerOf2_64) {
225 EXPECT_FALSE(isPowerOf2_64(0));
226 EXPECT_TRUE(isPowerOf2_64(1LL << 46));
227 EXPECT_TRUE(isPowerOf2_64(1LL << 12));
228 EXPECT_FALSE(isPowerOf2_64((1LL << 53) + 3));
229 EXPECT_FALSE(isPowerOf2_64(0xABCDEF0ABCDEF0LL));
232 TEST(MathExtras, PowerOf2Ceil) {
233 EXPECT_EQ(0U, PowerOf2Ceil(0U));
234 EXPECT_EQ(8U, PowerOf2Ceil(8U));
235 EXPECT_EQ(8U, PowerOf2Ceil(7U));
238 TEST(MathExtras, PowerOf2Floor) {
239 EXPECT_EQ(0U, PowerOf2Floor(0U));
240 EXPECT_EQ(8U, PowerOf2Floor(8U));
241 EXPECT_EQ(4U, PowerOf2Floor(7U));
244 TEST(MathExtras, CTLog2) {
245 EXPECT_EQ(CTLog2<1ULL << 0>(), 0U);
246 EXPECT_EQ(CTLog2<1ULL << 1>(), 1U);
247 EXPECT_EQ(CTLog2<1ULL << 2>(), 2U);
248 EXPECT_EQ(CTLog2<1ULL << 3>(), 3U);
249 EXPECT_EQ(CTLog2<1ULL << 4>(), 4U);
250 EXPECT_EQ(CTLog2<1ULL << 5>(), 5U);
251 EXPECT_EQ(CTLog2<1ULL << 6>(), 6U);
252 EXPECT_EQ(CTLog2<1ULL << 7>(), 7U);
253 EXPECT_EQ(CTLog2<1ULL << 8>(), 8U);
254 EXPECT_EQ(CTLog2<1ULL << 9>(), 9U);
255 EXPECT_EQ(CTLog2<1ULL << 10>(), 10U);
256 EXPECT_EQ(CTLog2<1ULL << 11>(), 11U);
257 EXPECT_EQ(CTLog2<1ULL << 12>(), 12U);
258 EXPECT_EQ(CTLog2<1ULL << 13>(), 13U);
259 EXPECT_EQ(CTLog2<1ULL << 14>(), 14U);
260 EXPECT_EQ(CTLog2<1ULL << 15>(), 15U);
263 TEST(MathExtras, countLeadingOnes) {
264 for (int i = 30; i >= 0; --i) {
265 // Start with all ones and unset some bit.
266 EXPECT_EQ(31u - i, countLeadingOnes(0xFFFFFFFF ^ (1 << i)));
268 for (int i = 62; i >= 0; --i) {
269 // Start with all ones and unset some bit.
270 EXPECT_EQ(63u - i, countLeadingOnes(0xFFFFFFFFFFFFFFFFULL ^ (1LL << i)));
272 for (int i = 30; i >= 0; --i) {
273 // Start with all ones and unset some bit.
274 EXPECT_EQ(31u - i, countLeadingOnes(0xFFFFFFFF ^ (1 << i)));
278 TEST(MathExtras, FloatBits) {
279 static const float kValue = 5632.34f;
280 EXPECT_FLOAT_EQ(kValue, BitsToFloat(FloatToBits(kValue)));
283 TEST(MathExtras, DoubleBits) {
284 static const double kValue = 87987234.983498;
285 EXPECT_DOUBLE_EQ(kValue, BitsToDouble(DoubleToBits(kValue)));
288 TEST(MathExtras, MinAlign) {
289 EXPECT_EQ(1u, MinAlign(2, 3));
290 EXPECT_EQ(2u, MinAlign(2, 4));
291 EXPECT_EQ(1u, MinAlign(17, 64));
292 EXPECT_EQ(256u, MinAlign(256, 512));
295 TEST(MathExtras, NextPowerOf2) {
296 EXPECT_EQ(4u, NextPowerOf2(3));
297 EXPECT_EQ(16u, NextPowerOf2(15));
298 EXPECT_EQ(256u, NextPowerOf2(128));
301 TEST(MathExtras, alignTo) {
302 EXPECT_EQ(8u, alignTo(5, 8));
303 EXPECT_EQ(24u, alignTo(17, 8));
304 EXPECT_EQ(0u, alignTo(~0LL, 8));
306 EXPECT_EQ(7u, alignTo(5, 8, 7));
307 EXPECT_EQ(17u, alignTo(17, 8, 1));
308 EXPECT_EQ(3u, alignTo(~0LL, 8, 3));
309 EXPECT_EQ(552u, alignTo(321, 255, 42));
312 template<typename T>
313 void SaturatingAddTestHelper()
315 const T Max = std::numeric_limits<T>::max();
316 bool ResultOverflowed;
318 EXPECT_EQ(T(3), SaturatingAdd(T(1), T(2)));
319 EXPECT_EQ(T(3), SaturatingAdd(T(1), T(2), &ResultOverflowed));
320 EXPECT_FALSE(ResultOverflowed);
322 EXPECT_EQ(Max, SaturatingAdd(Max, T(1)));
323 EXPECT_EQ(Max, SaturatingAdd(Max, T(1), &ResultOverflowed));
324 EXPECT_TRUE(ResultOverflowed);
326 EXPECT_EQ(Max, SaturatingAdd(T(1), T(Max - 1)));
327 EXPECT_EQ(Max, SaturatingAdd(T(1), T(Max - 1), &ResultOverflowed));
328 EXPECT_FALSE(ResultOverflowed);
330 EXPECT_EQ(Max, SaturatingAdd(T(1), Max));
331 EXPECT_EQ(Max, SaturatingAdd(T(1), Max, &ResultOverflowed));
332 EXPECT_TRUE(ResultOverflowed);
334 EXPECT_EQ(Max, SaturatingAdd(Max, Max));
335 EXPECT_EQ(Max, SaturatingAdd(Max, Max, &ResultOverflowed));
336 EXPECT_TRUE(ResultOverflowed);
339 TEST(MathExtras, SaturatingAdd) {
340 SaturatingAddTestHelper<uint8_t>();
341 SaturatingAddTestHelper<uint16_t>();
342 SaturatingAddTestHelper<uint32_t>();
343 SaturatingAddTestHelper<uint64_t>();
346 template<typename T>
347 void SaturatingMultiplyTestHelper()
349 const T Max = std::numeric_limits<T>::max();
350 bool ResultOverflowed;
352 // Test basic multiplication.
353 EXPECT_EQ(T(6), SaturatingMultiply(T(2), T(3)));
354 EXPECT_EQ(T(6), SaturatingMultiply(T(2), T(3), &ResultOverflowed));
355 EXPECT_FALSE(ResultOverflowed);
357 EXPECT_EQ(T(6), SaturatingMultiply(T(3), T(2)));
358 EXPECT_EQ(T(6), SaturatingMultiply(T(3), T(2), &ResultOverflowed));
359 EXPECT_FALSE(ResultOverflowed);
361 // Test multiplication by zero.
362 EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(0)));
363 EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(0), &ResultOverflowed));
364 EXPECT_FALSE(ResultOverflowed);
366 EXPECT_EQ(T(0), SaturatingMultiply(T(1), T(0)));
367 EXPECT_EQ(T(0), SaturatingMultiply(T(1), T(0), &ResultOverflowed));
368 EXPECT_FALSE(ResultOverflowed);
370 EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(1)));
371 EXPECT_EQ(T(0), SaturatingMultiply(T(0), T(1), &ResultOverflowed));
372 EXPECT_FALSE(ResultOverflowed);
374 EXPECT_EQ(T(0), SaturatingMultiply(Max, T(0)));
375 EXPECT_EQ(T(0), SaturatingMultiply(Max, T(0), &ResultOverflowed));
376 EXPECT_FALSE(ResultOverflowed);
378 EXPECT_EQ(T(0), SaturatingMultiply(T(0), Max));
379 EXPECT_EQ(T(0), SaturatingMultiply(T(0), Max, &ResultOverflowed));
380 EXPECT_FALSE(ResultOverflowed);
382 // Test multiplication by maximum value.
383 EXPECT_EQ(Max, SaturatingMultiply(Max, T(2)));
384 EXPECT_EQ(Max, SaturatingMultiply(Max, T(2), &ResultOverflowed));
385 EXPECT_TRUE(ResultOverflowed);
387 EXPECT_EQ(Max, SaturatingMultiply(T(2), Max));
388 EXPECT_EQ(Max, SaturatingMultiply(T(2), Max, &ResultOverflowed));
389 EXPECT_TRUE(ResultOverflowed);
391 EXPECT_EQ(Max, SaturatingMultiply(Max, Max));
392 EXPECT_EQ(Max, SaturatingMultiply(Max, Max, &ResultOverflowed));
393 EXPECT_TRUE(ResultOverflowed);
395 // Test interesting boundary conditions for algorithm -
396 // ((1 << A) - 1) * ((1 << B) + K) for K in [-1, 0, 1]
397 // and A + B == std::numeric_limits<T>::digits.
398 // We expect overflow iff A > B and K = 1.
399 const int Digits = std::numeric_limits<T>::digits;
400 for (int A = 1, B = Digits - 1; B >= 1; ++A, --B) {
401 for (int K = -1; K <= 1; ++K) {
402 T X = (T(1) << A) - T(1);
403 T Y = (T(1) << B) + K;
404 bool OverflowExpected = A > B && K == 1;
406 if(OverflowExpected) {
407 EXPECT_EQ(Max, SaturatingMultiply(X, Y));
408 EXPECT_EQ(Max, SaturatingMultiply(X, Y, &ResultOverflowed));
409 EXPECT_TRUE(ResultOverflowed);
410 } else {
411 EXPECT_EQ(X * Y, SaturatingMultiply(X, Y));
412 EXPECT_EQ(X * Y, SaturatingMultiply(X, Y, &ResultOverflowed));
413 EXPECT_FALSE(ResultOverflowed);
419 TEST(MathExtras, SaturatingMultiply) {
420 SaturatingMultiplyTestHelper<uint8_t>();
421 SaturatingMultiplyTestHelper<uint16_t>();
422 SaturatingMultiplyTestHelper<uint32_t>();
423 SaturatingMultiplyTestHelper<uint64_t>();
426 template<typename T>
427 void SaturatingMultiplyAddTestHelper()
429 const T Max = std::numeric_limits<T>::max();
430 bool ResultOverflowed;
432 // Test basic multiply-add.
433 EXPECT_EQ(T(16), SaturatingMultiplyAdd(T(2), T(3), T(10)));
434 EXPECT_EQ(T(16), SaturatingMultiplyAdd(T(2), T(3), T(10), &ResultOverflowed));
435 EXPECT_FALSE(ResultOverflowed);
437 // Test multiply overflows, add doesn't overflow
438 EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, T(0), &ResultOverflowed));
439 EXPECT_TRUE(ResultOverflowed);
441 // Test multiply doesn't overflow, add overflows
442 EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), T(1), Max, &ResultOverflowed));
443 EXPECT_TRUE(ResultOverflowed);
445 // Test multiply-add with Max as operand
446 EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), T(1), Max, &ResultOverflowed));
447 EXPECT_TRUE(ResultOverflowed);
449 EXPECT_EQ(Max, SaturatingMultiplyAdd(T(1), Max, T(1), &ResultOverflowed));
450 EXPECT_TRUE(ResultOverflowed);
452 EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, T(1), &ResultOverflowed));
453 EXPECT_TRUE(ResultOverflowed);
455 EXPECT_EQ(Max, SaturatingMultiplyAdd(Max, Max, Max, &ResultOverflowed));
456 EXPECT_TRUE(ResultOverflowed);
458 // Test multiply-add with 0 as operand
459 EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(1), T(0), &ResultOverflowed));
460 EXPECT_FALSE(ResultOverflowed);
462 EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(1), T(0), T(1), &ResultOverflowed));
463 EXPECT_FALSE(ResultOverflowed);
465 EXPECT_EQ(T(1), SaturatingMultiplyAdd(T(0), T(0), T(1), &ResultOverflowed));
466 EXPECT_FALSE(ResultOverflowed);
468 EXPECT_EQ(T(0), SaturatingMultiplyAdd(T(0), T(0), T(0), &ResultOverflowed));
469 EXPECT_FALSE(ResultOverflowed);
473 TEST(MathExtras, SaturatingMultiplyAdd) {
474 SaturatingMultiplyAddTestHelper<uint8_t>();
475 SaturatingMultiplyAddTestHelper<uint16_t>();
476 SaturatingMultiplyAddTestHelper<uint32_t>();
477 SaturatingMultiplyAddTestHelper<uint64_t>();
480 TEST(MathExtras, IsShiftedUInt) {
481 EXPECT_TRUE((isShiftedUInt<1, 0>(0)));
482 EXPECT_TRUE((isShiftedUInt<1, 0>(1)));
483 EXPECT_FALSE((isShiftedUInt<1, 0>(2)));
484 EXPECT_FALSE((isShiftedUInt<1, 0>(3)));
485 EXPECT_FALSE((isShiftedUInt<1, 0>(0x8000000000000000)));
486 EXPECT_TRUE((isShiftedUInt<1, 63>(0x8000000000000000)));
487 EXPECT_TRUE((isShiftedUInt<2, 62>(0xC000000000000000)));
488 EXPECT_FALSE((isShiftedUInt<2, 62>(0xE000000000000000)));
490 // 0x201 is ten bits long and has a 1 in the MSB and LSB.
491 EXPECT_TRUE((isShiftedUInt<10, 5>(uint64_t(0x201) << 5)));
492 EXPECT_FALSE((isShiftedUInt<10, 5>(uint64_t(0x201) << 4)));
493 EXPECT_FALSE((isShiftedUInt<10, 5>(uint64_t(0x201) << 6)));
496 TEST(MathExtras, IsShiftedInt) {
497 EXPECT_TRUE((isShiftedInt<1, 0>(0)));
498 EXPECT_TRUE((isShiftedInt<1, 0>(-1)));
499 EXPECT_FALSE((isShiftedInt<1, 0>(2)));
500 EXPECT_FALSE((isShiftedInt<1, 0>(3)));
501 EXPECT_FALSE((isShiftedInt<1, 0>(0x8000000000000000)));
502 EXPECT_TRUE((isShiftedInt<1, 63>(0x8000000000000000)));
503 EXPECT_TRUE((isShiftedInt<2, 62>(0xC000000000000000)));
504 EXPECT_FALSE((isShiftedInt<2, 62>(0xE000000000000000)));
506 // 0x201 is ten bits long and has a 1 in the MSB and LSB.
507 EXPECT_TRUE((isShiftedInt<11, 5>(int64_t(0x201) << 5)));
508 EXPECT_FALSE((isShiftedInt<11, 5>(int64_t(0x201) << 3)));
509 EXPECT_FALSE((isShiftedInt<11, 5>(int64_t(0x201) << 6)));
510 EXPECT_TRUE((isShiftedInt<11, 5>(-(int64_t(0x201) << 5))));
511 EXPECT_FALSE((isShiftedInt<11, 5>(-(int64_t(0x201) << 3))));
512 EXPECT_FALSE((isShiftedInt<11, 5>(-(int64_t(0x201) << 6))));
514 EXPECT_TRUE((isShiftedInt<6, 10>(-(int64_t(1) << 15))));
515 EXPECT_FALSE((isShiftedInt<6, 10>(int64_t(1) << 15)));
518 template <typename T>
519 class OverflowTest : public ::testing::Test { };
521 using OverflowTestTypes = ::testing::Types<signed char, short, int, long,
522 long long>;
524 TYPED_TEST_SUITE(OverflowTest, OverflowTestTypes, );
526 TYPED_TEST(OverflowTest, AddNoOverflow) {
527 TypeParam Result;
528 EXPECT_FALSE(AddOverflow<TypeParam>(1, 2, Result));
529 EXPECT_EQ(Result, TypeParam(3));
532 TYPED_TEST(OverflowTest, AddOverflowToNegative) {
533 TypeParam Result;
534 auto MaxValue = std::numeric_limits<TypeParam>::max();
535 EXPECT_TRUE(AddOverflow<TypeParam>(MaxValue, MaxValue, Result));
536 EXPECT_EQ(Result, TypeParam(-2));
539 TYPED_TEST(OverflowTest, AddOverflowToMin) {
540 TypeParam Result;
541 auto MaxValue = std::numeric_limits<TypeParam>::max();
542 EXPECT_TRUE(AddOverflow<TypeParam>(MaxValue, TypeParam(1), Result));
543 EXPECT_EQ(Result, std::numeric_limits<TypeParam>::min());
546 TYPED_TEST(OverflowTest, AddOverflowToZero) {
547 TypeParam Result;
548 auto MinValue = std::numeric_limits<TypeParam>::min();
549 EXPECT_TRUE(AddOverflow<TypeParam>(MinValue, MinValue, Result));
550 EXPECT_EQ(Result, TypeParam(0));
553 TYPED_TEST(OverflowTest, AddOverflowToMax) {
554 TypeParam Result;
555 auto MinValue = std::numeric_limits<TypeParam>::min();
556 EXPECT_TRUE(AddOverflow<TypeParam>(MinValue, TypeParam(-1), Result));
557 EXPECT_EQ(Result, std::numeric_limits<TypeParam>::max());
560 TYPED_TEST(OverflowTest, SubNoOverflow) {
561 TypeParam Result;
562 EXPECT_FALSE(SubOverflow<TypeParam>(1, 2, Result));
563 EXPECT_EQ(Result, TypeParam(-1));
566 TYPED_TEST(OverflowTest, SubOverflowToMax) {
567 TypeParam Result;
568 auto MinValue = std::numeric_limits<TypeParam>::min();
569 EXPECT_TRUE(SubOverflow<TypeParam>(0, MinValue, Result));
570 EXPECT_EQ(Result, MinValue);
573 TYPED_TEST(OverflowTest, SubOverflowToMin) {
574 TypeParam Result;
575 auto MinValue = std::numeric_limits<TypeParam>::min();
576 EXPECT_TRUE(SubOverflow<TypeParam>(0, MinValue, Result));
577 EXPECT_EQ(Result, MinValue);
580 TYPED_TEST(OverflowTest, SubOverflowToNegative) {
581 TypeParam Result;
582 auto MaxValue = std::numeric_limits<TypeParam>::max();
583 auto MinValue = std::numeric_limits<TypeParam>::min();
584 EXPECT_TRUE(SubOverflow<TypeParam>(MaxValue, MinValue, Result));
585 EXPECT_EQ(Result, TypeParam(-1));
588 TYPED_TEST(OverflowTest, SubOverflowToPositive) {
589 TypeParam Result;
590 auto MaxValue = std::numeric_limits<TypeParam>::max();
591 auto MinValue = std::numeric_limits<TypeParam>::min();
592 EXPECT_TRUE(SubOverflow<TypeParam>(MinValue, MaxValue, Result));
593 EXPECT_EQ(Result, TypeParam(1));
596 TYPED_TEST(OverflowTest, MulNoOverflow) {
597 TypeParam Result;
598 EXPECT_FALSE(MulOverflow<TypeParam>(1, 2, Result));
599 EXPECT_EQ(Result, 2);
600 EXPECT_FALSE(MulOverflow<TypeParam>(-1, 3, Result));
601 EXPECT_EQ(Result, -3);
602 EXPECT_FALSE(MulOverflow<TypeParam>(4, -2, Result));
603 EXPECT_EQ(Result, -8);
604 EXPECT_FALSE(MulOverflow<TypeParam>(-6, -5, Result));
605 EXPECT_EQ(Result, 30);
608 TYPED_TEST(OverflowTest, MulNoOverflowToMax) {
609 TypeParam Result;
610 auto MaxValue = std::numeric_limits<TypeParam>::max();
611 auto MinValue = std::numeric_limits<TypeParam>::min();
612 EXPECT_FALSE(MulOverflow<TypeParam>(MinValue + 1, -1, Result));
613 EXPECT_EQ(Result, MaxValue);
616 TYPED_TEST(OverflowTest, MulOverflowToMin) {
617 TypeParam Result;
618 auto MinValue = std::numeric_limits<TypeParam>::min();
619 EXPECT_TRUE(MulOverflow<TypeParam>(MinValue, -1, Result));
620 EXPECT_EQ(Result, MinValue);
623 TYPED_TEST(OverflowTest, MulOverflowMax) {
624 TypeParam Result;
625 auto MinValue = std::numeric_limits<TypeParam>::min();
626 auto MaxValue = std::numeric_limits<TypeParam>::max();
627 EXPECT_TRUE(MulOverflow<TypeParam>(MinValue, MinValue, Result));
628 EXPECT_EQ(Result, 0);
629 EXPECT_TRUE(MulOverflow<TypeParam>(MaxValue, MaxValue, Result));
630 EXPECT_EQ(Result, 1);
633 TYPED_TEST(OverflowTest, MulResultZero) {
634 TypeParam Result;
635 EXPECT_FALSE(MulOverflow<TypeParam>(4, 0, Result));
636 EXPECT_EQ(Result, TypeParam(0));
637 EXPECT_FALSE(MulOverflow<TypeParam>(-5, 0, Result));
638 EXPECT_EQ(Result, TypeParam(0));
639 EXPECT_FALSE(MulOverflow<TypeParam>(0, 5, Result));
640 EXPECT_EQ(Result, TypeParam(0));
641 EXPECT_FALSE(MulOverflow<TypeParam>(0, -5, Result));
642 EXPECT_EQ(Result, TypeParam(0));
645 } // namespace