1 //===-- Unittests for math_extras -----------------------------------------===//
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 "src/__support/integer_literals.h"
10 #include "src/__support/macros/config.h"
11 #include "src/__support/math_extras.h"
12 #include "src/__support/uint128.h" // UInt<128>
13 #include "test/UnitTest/Test.h"
15 namespace LIBC_NAMESPACE_DECL
{
17 // TODO: add UInt<128> support.
18 using UnsignedTypesNoBigInt
= testing::TypeList
<
19 #if defined(LIBC_TYPES_HAS_INT128)
21 #endif // LIBC_TYPES_HAS_INT128
22 unsigned char, unsigned short, unsigned int, unsigned long,
23 unsigned long long, UInt
<128>>;
25 TEST(LlvmLibcBlockMathExtrasTest
, mask_trailing_ones
) {
26 EXPECT_EQ(0_u8
, (mask_leading_ones
<uint8_t, 0>()));
27 EXPECT_EQ(0_u8
, (mask_trailing_ones
<uint8_t, 0>()));
28 EXPECT_EQ(0_u16
, (mask_leading_ones
<uint16_t, 0>()));
29 EXPECT_EQ(0_u16
, (mask_trailing_ones
<uint16_t, 0>()));
30 EXPECT_EQ(0_u32
, (mask_leading_ones
<uint32_t, 0>()));
31 EXPECT_EQ(0_u32
, (mask_trailing_ones
<uint32_t, 0>()));
32 EXPECT_EQ(0_u64
, (mask_leading_ones
<uint64_t, 0>()));
33 EXPECT_EQ(0_u64
, (mask_trailing_ones
<uint64_t, 0>()));
35 EXPECT_EQ(0x00000003_u
32, (mask_trailing_ones
<uint32_t, 2>()));
36 EXPECT_EQ(0xC0000000_u
32, (mask_leading_ones
<uint32_t, 2>()));
38 EXPECT_EQ(0x000007FF_u
32, (mask_trailing_ones
<uint32_t, 11>()));
39 EXPECT_EQ(0xFFE00000_u
32, (mask_leading_ones
<uint32_t, 11>()));
41 EXPECT_EQ(0xFFFFFFFF_u
32, (mask_trailing_ones
<uint32_t, 32>()));
42 EXPECT_EQ(0xFFFFFFFF_u
32, (mask_leading_ones
<uint32_t, 32>()));
43 EXPECT_EQ(0xFFFFFFFFFFFFFFFF_u
64, (mask_trailing_ones
<uint64_t, 64>()));
44 EXPECT_EQ(0xFFFFFFFFFFFFFFFF_u
64, (mask_leading_ones
<uint64_t, 64>()));
46 EXPECT_EQ(0x0000FFFFFFFFFFFF_u
64, (mask_trailing_ones
<uint64_t, 48>()));
47 EXPECT_EQ(0xFFFFFFFFFFFF0000_u
64, (mask_leading_ones
<uint64_t, 48>()));
49 EXPECT_EQ(0_u128
, (mask_trailing_ones
<UInt128
, 0>()));
50 EXPECT_EQ(0_u128
, (mask_leading_ones
<UInt128
, 0>()));
52 EXPECT_EQ(0x00000000000000007FFFFFFFFFFFFFFF_u
128,
53 (mask_trailing_ones
<UInt128
, 63>()));
54 EXPECT_EQ(0xFFFFFFFFFFFFFFFE0000000000000000_u
128,
55 (mask_leading_ones
<UInt128
, 63>()));
57 EXPECT_EQ(0x0000000000000000FFFFFFFFFFFFFFFF_u
128,
58 (mask_trailing_ones
<UInt128
, 64>()));
59 EXPECT_EQ(0xFFFFFFFFFFFFFFFF0000000000000000_u
128,
60 (mask_leading_ones
<UInt128
, 64>()));
62 EXPECT_EQ(0x0000000000000001FFFFFFFFFFFFFFFF_u
128,
63 (mask_trailing_ones
<UInt128
, 65>()));
64 EXPECT_EQ(0xFFFFFFFFFFFFFFFF8000000000000000_u
128,
65 (mask_leading_ones
<UInt128
, 65>()));
67 EXPECT_EQ(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u
128,
68 (mask_trailing_ones
<UInt128
, 128>()));
69 EXPECT_EQ(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u
128,
70 (mask_leading_ones
<UInt128
, 128>()));
73 TYPED_TEST(LlvmLibcBitTest
, FirstLeadingZero
, UnsignedTypesNoBigInt
) {
74 EXPECT_EQ(first_leading_zero
<T
>(cpp::numeric_limits
<T
>::max()), 0);
75 for (int i
= 0U; i
!= cpp::numeric_limits
<T
>::digits
; ++i
)
76 EXPECT_EQ(first_leading_zero
<T
>(~(T(1) << i
)),
77 cpp::numeric_limits
<T
>::digits
- i
);
80 TYPED_TEST(LlvmLibcBitTest
, FirstLeadingOne
, UnsignedTypesNoBigInt
) {
81 EXPECT_EQ(first_leading_one
<T
>(static_cast<T
>(0)), 0);
82 for (int i
= 0U; i
!= cpp::numeric_limits
<T
>::digits
; ++i
)
83 EXPECT_EQ(first_leading_one
<T
>(T(1) << i
),
84 cpp::numeric_limits
<T
>::digits
- i
);
87 TYPED_TEST(LlvmLibcBitTest
, FirstTrailingZero
, UnsignedTypesNoBigInt
) {
88 EXPECT_EQ(first_trailing_zero
<T
>(cpp::numeric_limits
<T
>::max()), 0);
89 for (int i
= 0U; i
!= cpp::numeric_limits
<T
>::digits
; ++i
)
90 EXPECT_EQ(first_trailing_zero
<T
>(~(T(1) << i
)), i
+ 1);
93 TYPED_TEST(LlvmLibcBitTest
, FirstTrailingOne
, UnsignedTypesNoBigInt
) {
94 EXPECT_EQ(first_trailing_one
<T
>(cpp::numeric_limits
<T
>::max()), 0);
95 for (int i
= 0U; i
!= cpp::numeric_limits
<T
>::digits
; ++i
)
96 EXPECT_EQ(first_trailing_one
<T
>(T(1) << i
), i
+ 1);
99 TYPED_TEST(LlvmLibcBitTest
, CountZeros
, UnsignedTypesNoBigInt
) {
100 EXPECT_EQ(count_zeros(T(0)), cpp::numeric_limits
<T
>::digits
);
101 for (int i
= 0; i
!= cpp::numeric_limits
<T
>::digits
; ++i
)
102 EXPECT_EQ(count_zeros
<T
>(cpp::numeric_limits
<T
>::max() >> i
), i
);
105 using UnsignedTypes
= testing::TypeList
<
106 #if defined(LIBC_TYPES_HAS_INT128)
109 unsigned char, unsigned short, unsigned int, unsigned long,
112 TYPED_TEST(LlvmLibcBlockMathExtrasTest
, add_overflow
, UnsignedTypes
) {
113 constexpr T ZERO
= cpp::numeric_limits
<T
>::min();
115 constexpr T MAX
= cpp::numeric_limits
<T
>::max();
116 constexpr T BEFORE_MAX
= MAX
- 1;
124 {ZERO
, ONE
, ONE
, false}, // 0x00 + 0x01 = 0x01
125 {BEFORE_MAX
, ONE
, MAX
, false}, // 0xFE + 0x01 = 0xFF
126 {MAX
, ONE
, ZERO
, true}, // 0xFF + 0x01 = 0x00 (carry)
127 {MAX
, MAX
, BEFORE_MAX
, true}, // 0xFF + 0xFF = 0xFE (carry)
129 for (auto tc
: TESTS
) {
131 bool carry
= add_overflow
<T
>(tc
.lhs
, tc
.rhs
, sum
);
132 EXPECT_EQ(sum
, tc
.sum
);
133 EXPECT_EQ(carry
, tc
.carry
);
137 TYPED_TEST(LlvmLibcBlockMathExtrasTest
, sub_overflow
, UnsignedTypes
) {
138 constexpr T ZERO
= cpp::numeric_limits
<T
>::min();
140 constexpr T MAX
= cpp::numeric_limits
<T
>::max();
141 constexpr T BEFORE_MAX
= MAX
- 1;
149 {ONE
, ZERO
, ONE
, false}, // 0x01 - 0x00 = 0x01
150 {MAX
, MAX
, ZERO
, false}, // 0xFF - 0xFF = 0x00
151 {ZERO
, ONE
, MAX
, true}, // 0x00 - 0x01 = 0xFF (carry)
152 {BEFORE_MAX
, MAX
, MAX
, true}, // 0xFE - 0xFF = 0xFF (carry)
154 for (auto tc
: TESTS
) {
156 bool carry
= sub_overflow
<T
>(tc
.lhs
, tc
.rhs
, sub
);
157 EXPECT_EQ(sub
, tc
.sub
);
158 EXPECT_EQ(carry
, tc
.carry
);
162 } // namespace LIBC_NAMESPACE_DECL