1//===-- Unittests for math_extras -----------------------------------------===//
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 "src/__support/integer_literals.h"
10#include "src/__support/math_extras.h"
11#include "src/__support/uint128.h" // UInt<128>
12#include "test/UnitTest/Test.h"
13
14namespace LIBC_NAMESPACE {
15
16// TODO: add UInt<128> support.
17using UnsignedTypesNoBigInt = testing::TypeList<
18#if defined(LIBC_TYPES_HAS_INT128)
19 __uint128_t,
20#endif // LIBC_TYPES_HAS_INT128
21 unsigned char, unsigned short, unsigned int, unsigned long,
22 unsigned long long, UInt<128>>;
23
24TEST(LlvmLibcBlockMathExtrasTest, mask_trailing_ones) {
25 EXPECT_EQ(0_u8, (mask_leading_ones<uint8_t, 0>()));
26 EXPECT_EQ(0_u8, (mask_trailing_ones<uint8_t, 0>()));
27 EXPECT_EQ(0_u16, (mask_leading_ones<uint16_t, 0>()));
28 EXPECT_EQ(0_u16, (mask_trailing_ones<uint16_t, 0>()));
29 EXPECT_EQ(0_u32, (mask_leading_ones<uint32_t, 0>()));
30 EXPECT_EQ(0_u32, (mask_trailing_ones<uint32_t, 0>()));
31 EXPECT_EQ(0_u64, (mask_leading_ones<uint64_t, 0>()));
32 EXPECT_EQ(0_u64, (mask_trailing_ones<uint64_t, 0>()));
33
34 EXPECT_EQ(0x00000003_u32, (mask_trailing_ones<uint32_t, 2>()));
35 EXPECT_EQ(0xC0000000_u32, (mask_leading_ones<uint32_t, 2>()));
36
37 EXPECT_EQ(0x000007FF_u32, (mask_trailing_ones<uint32_t, 11>()));
38 EXPECT_EQ(0xFFE00000_u32, (mask_leading_ones<uint32_t, 11>()));
39
40 EXPECT_EQ(0xFFFFFFFF_u32, (mask_trailing_ones<uint32_t, 32>()));
41 EXPECT_EQ(0xFFFFFFFF_u32, (mask_leading_ones<uint32_t, 32>()));
42 EXPECT_EQ(0xFFFFFFFFFFFFFFFF_u64, (mask_trailing_ones<uint64_t, 64>()));
43 EXPECT_EQ(0xFFFFFFFFFFFFFFFF_u64, (mask_leading_ones<uint64_t, 64>()));
44
45 EXPECT_EQ(0x0000FFFFFFFFFFFF_u64, (mask_trailing_ones<uint64_t, 48>()));
46 EXPECT_EQ(0xFFFFFFFFFFFF0000_u64, (mask_leading_ones<uint64_t, 48>()));
47
48 EXPECT_EQ(0_u128, (mask_trailing_ones<UInt128, 0>()));
49 EXPECT_EQ(0_u128, (mask_leading_ones<UInt128, 0>()));
50
51 EXPECT_EQ(0x00000000000000007FFFFFFFFFFFFFFF_u128,
52 (mask_trailing_ones<UInt128, 63>()));
53 EXPECT_EQ(0xFFFFFFFFFFFFFFFE0000000000000000_u128,
54 (mask_leading_ones<UInt128, 63>()));
55
56 EXPECT_EQ(0x0000000000000000FFFFFFFFFFFFFFFF_u128,
57 (mask_trailing_ones<UInt128, 64>()));
58 EXPECT_EQ(0xFFFFFFFFFFFFFFFF0000000000000000_u128,
59 (mask_leading_ones<UInt128, 64>()));
60
61 EXPECT_EQ(0x0000000000000001FFFFFFFFFFFFFFFF_u128,
62 (mask_trailing_ones<UInt128, 65>()));
63 EXPECT_EQ(0xFFFFFFFFFFFFFFFF8000000000000000_u128,
64 (mask_leading_ones<UInt128, 65>()));
65
66 EXPECT_EQ(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u128,
67 (mask_trailing_ones<UInt128, 128>()));
68 EXPECT_EQ(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF_u128,
69 (mask_leading_ones<UInt128, 128>()));
70}
71
72TYPED_TEST(LlvmLibcBitTest, FirstLeadingZero, UnsignedTypesNoBigInt) {
73 EXPECT_EQ(first_leading_zero<T>(cpp::numeric_limits<T>::max()), 0);
74 for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
75 EXPECT_EQ(first_leading_zero<T>(~(T(1) << i)),
76 cpp::numeric_limits<T>::digits - i);
77}
78
79TYPED_TEST(LlvmLibcBitTest, FirstLeadingOne, UnsignedTypesNoBigInt) {
80 EXPECT_EQ(first_leading_one<T>(static_cast<T>(0)), 0);
81 for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
82 EXPECT_EQ(first_leading_one<T>(T(1) << i),
83 cpp::numeric_limits<T>::digits - i);
84}
85
86TYPED_TEST(LlvmLibcBitTest, FirstTrailingZero, UnsignedTypesNoBigInt) {
87 EXPECT_EQ(first_trailing_zero<T>(cpp::numeric_limits<T>::max()), 0);
88 for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
89 EXPECT_EQ(first_trailing_zero<T>(~(T(1) << i)), i + 1);
90}
91
92TYPED_TEST(LlvmLibcBitTest, FirstTrailingOne, UnsignedTypesNoBigInt) {
93 EXPECT_EQ(first_trailing_one<T>(cpp::numeric_limits<T>::max()), 0);
94 for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
95 EXPECT_EQ(first_trailing_one<T>(T(1) << i), i + 1);
96}
97
98TYPED_TEST(LlvmLibcBitTest, CountZeros, UnsignedTypesNoBigInt) {
99 EXPECT_EQ(count_zeros(T(0)), cpp::numeric_limits<T>::digits);
100 for (int i = 0; i != cpp::numeric_limits<T>::digits; ++i)
101 EXPECT_EQ(count_zeros<T>(cpp::numeric_limits<T>::max() >> i), i);
102}
103
104using UnsignedTypes = testing::TypeList<
105#if defined(__SIZEOF_INT128__)
106 __uint128_t,
107#endif
108 unsigned char, unsigned short, unsigned int, unsigned long,
109 unsigned long long>;
110
111TYPED_TEST(LlvmLibcBlockMathExtrasTest, add_overflow, UnsignedTypes) {
112 constexpr T ZERO = cpp::numeric_limits<T>::min();
113 constexpr T ONE(1);
114 constexpr T MAX = cpp::numeric_limits<T>::max();
115 constexpr T BEFORE_MAX = MAX - 1;
116
117 const struct {
118 T lhs;
119 T rhs;
120 T sum;
121 bool carry;
122 } TESTS[] = {
123 {ZERO, ONE, ONE, false}, // 0x00 + 0x01 = 0x01
124 {BEFORE_MAX, ONE, MAX, false}, // 0xFE + 0x01 = 0xFF
125 {MAX, ONE, ZERO, true}, // 0xFF + 0x01 = 0x00 (carry)
126 {MAX, MAX, BEFORE_MAX, true}, // 0xFF + 0xFF = 0xFE (carry)
127 };
128 for (auto tc : TESTS) {
129 T sum;
130 bool carry = add_overflow<T>(tc.lhs, tc.rhs, sum);
131 EXPECT_EQ(sum, tc.sum);
132 EXPECT_EQ(carry, tc.carry);
133 }
134}
135
136TYPED_TEST(LlvmLibcBlockMathExtrasTest, sub_overflow, UnsignedTypes) {
137 constexpr T ZERO = cpp::numeric_limits<T>::min();
138 constexpr T ONE(1);
139 constexpr T MAX = cpp::numeric_limits<T>::max();
140 constexpr T BEFORE_MAX = MAX - 1;
141
142 const struct {
143 T lhs;
144 T rhs;
145 T sub;
146 bool carry;
147 } TESTS[] = {
148 {ONE, ZERO, ONE, false}, // 0x01 - 0x00 = 0x01
149 {MAX, MAX, ZERO, false}, // 0xFF - 0xFF = 0x00
150 {ZERO, ONE, MAX, true}, // 0x00 - 0x01 = 0xFF (carry)
151 {BEFORE_MAX, MAX, MAX, true}, // 0xFE - 0xFF = 0xFF (carry)
152 };
153 for (auto tc : TESTS) {
154 T sub;
155 bool carry = sub_overflow<T>(tc.lhs, tc.rhs, sub);
156 EXPECT_EQ(sub, tc.sub);
157 EXPECT_EQ(carry, tc.carry);
158 }
159}
160
161} // namespace LIBC_NAMESPACE
162

source code of libc/test/src/__support/math_extras_test.cpp