Skip to content

Commit fb91e3b

Browse files
committed
Fix remaining issues
1 parent 1dfefaf commit fb91e3b

27 files changed

+129
-85
lines changed

libc/src/__support/FPUtil/generic/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ add_header_library(
5656
HDRS
5757
add_sub.h
5858
DEPENDS
59-
libc.hdr.errno_macros
6059
libc.hdr.fenv_macros
6160
libc.src.__support.CPP.algorithm
6261
libc.src.__support.CPP.bit

libc/src/__support/FPUtil/generic/add_sub.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#ifndef LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_SUB_H
1010
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_ADD_SUB_H
1111

12-
#include "hdr/errno_macros.h"
1312
#include "hdr/fenv_macros.h"
1413
#include "src/__support/CPP/algorithm.h"
1514
#include "src/__support/CPP/bit.h"
@@ -110,12 +109,8 @@ add_or_sub(InType x, InType y) {
110109
return cast<OutType>(tmp);
111110
}
112111

113-
if (y_bits.is_zero()) {
114-
volatile InType tmp = y;
115-
if constexpr (IsSub)
116-
tmp = -tmp;
117-
return cast<OutType>(tmp);
118-
}
112+
if (y_bits.is_zero())
113+
return cast<OutType>(x);
119114
}
120115

121116
InType x_abs = x_bits.abs().get_val();
@@ -174,7 +169,8 @@ add_or_sub(InType x, InType y) {
174169
aligned_min_mant_sticky = true;
175170
else
176171
aligned_min_mant_sticky =
177-
(min_mant << (InFPBits::STORAGE_LEN - alignment)) != 0;
172+
(static_cast<InStorageType>(
173+
min_mant << (InFPBits::STORAGE_LEN - alignment))) != 0;
178174

179175
InStorageType min_mant_sticky(static_cast<int>(aligned_min_mant_sticky));
180176

libc/test/src/math/AddTest.h

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_TEST_SRC_MATH_ADDTEST_H
1010
#define LLVM_LIBC_TEST_SRC_MATH_ADDTEST_H
1111

12+
#include "src/__support/CPP/algorithm.h"
1213
#include "test/UnitTest/FEnvSafeTest.h"
1314
#include "test/UnitTest/FPMatcher.h"
1415
#include "test/UnitTest/Test.h"
@@ -36,37 +37,47 @@ class AddTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
3637
InFPBits::min_subnormal().uintval();
3738

3839
public:
39-
typedef OutType (*AddFunc)(InType, InType);
40+
using AddFunc = OutType (*)(InType, InType);
4041

4142
void test_subnormal_range(AddFunc func) {
42-
constexpr InStorageType COUNT = 100'001;
43-
constexpr InStorageType STEP =
44-
(IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) / COUNT;
45-
for (InStorageType i = 0, v = 0, w = IN_MAX_SUBNORMAL_U; i <= COUNT;
46-
++i, v += STEP, w -= STEP) {
47-
InType x = InFPBits(v).get_val();
48-
InType y = InFPBits(w).get_val();
43+
constexpr int COUNT = 100'001;
44+
constexpr InStorageType STEP = LIBC_NAMESPACE::cpp::max(
45+
static_cast<InStorageType>((IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) /
46+
COUNT),
47+
InStorageType(1));
48+
for (InStorageType i = IN_MIN_SUBNORMAL_U; i <= IN_MAX_SUBNORMAL_U;
49+
i += STEP) {
50+
InType x = InFPBits(i).get_val();
51+
InType y = InFPBits(static_cast<InStorageType>(IN_MAX_SUBNORMAL_U - i))
52+
.get_val();
4953
mpfr::BinaryInput<InType> input{x, y};
5054
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Add, input, func(x, y),
5155
0.5);
5256
}
5357
}
5458

5559
void test_normal_range(AddFunc func) {
56-
constexpr InStorageType COUNT = 100'001;
57-
constexpr InStorageType STEP = (IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT;
58-
for (InStorageType i = 0, v = 0, w = IN_MAX_NORMAL_U; i <= COUNT;
59-
++i, v += STEP, w -= STEP) {
60-
InType x = InFPBits(v).get_val();
61-
InType y = InFPBits(w).get_val();
60+
constexpr int COUNT = 100'001;
61+
constexpr InStorageType STEP = LIBC_NAMESPACE::cpp::max(
62+
static_cast<InStorageType>((IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT),
63+
InStorageType(1));
64+
for (InStorageType i = IN_MIN_NORMAL_U; i <= IN_MAX_NORMAL_U; i += STEP) {
65+
InType x = InFPBits(i).get_val();
66+
InType y =
67+
InFPBits(static_cast<InStorageType>(IN_MAX_NORMAL_U - i)).get_val();
6268
mpfr::BinaryInput<InType> input{x, y};
6369
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Add, input, func(x, y),
6470
0.5);
6571
}
6672
}
6773
};
6874

69-
#define LIST_ADD_TESTS(suffix, OutType, InType, func) \
75+
#define LIST_ADD_TESTS(OutType, InType, func) \
76+
using LlvmLibcAddTest = AddTest<OutType, InType>; \
77+
TEST_F(LlvmLibcAddTest, SubnormalRange) { test_subnormal_range(&func); } \
78+
TEST_F(LlvmLibcAddTest, NormalRange) { test_normal_range(&func); }
79+
80+
#define LIST_ADD_SAME_TYPE_TESTS(suffix, OutType, InType, func) \
7081
using LlvmLibcAddTest##suffix = AddTest<OutType, InType>; \
7182
TEST_F(LlvmLibcAddTest##suffix, SubnormalRange) { \
7283
test_subnormal_range(&func); \

libc/test/src/math/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,7 +2777,8 @@ add_fp_unittest(
27772777
HDRS
27782778
AddTest.h
27792779
DEPENDS
2780-
libc.src.__support.FPUtil.generic.add_or_sub
2780+
libc.src.__support.CPP.algorithm
2781+
libc.src.__support.FPUtil.generic.add_sub
27812782
libc.src.__support.macros.properties.types
27822783
)
27832784

@@ -2791,7 +2792,8 @@ add_fp_unittest(
27912792
HDRS
27922793
SubTest.h
27932794
DEPENDS
2794-
libc.src.__support.FPUtil.generic.add_or_sub
2795+
libc.src.__support.CPP.algorithm
2796+
libc.src.__support.FPUtil.generic.add_sub
27952797
libc.src.__support.macros.properties.types
27962798
)
27972799

libc/test/src/math/SubTest.h

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_TEST_SRC_MATH_SUBTEST_H
1010
#define LLVM_LIBC_TEST_SRC_MATH_SUBTEST_H
1111

12+
#include "src/__support/CPP/algorithm.h"
1213
#include "test/UnitTest/FEnvSafeTest.h"
1314
#include "test/UnitTest/FPMatcher.h"
1415
#include "test/UnitTest/Test.h"
@@ -39,34 +40,44 @@ class SubTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
3940
using SubFunc = OutType (*)(InType, InType);
4041

4142
void test_subnormal_range(SubFunc func) {
42-
constexpr InStorageType COUNT = 100'001;
43-
constexpr InStorageType STEP =
44-
(IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) / COUNT;
45-
for (InStorageType i = 0, v = 0, w = IN_MAX_SUBNORMAL_U; i <= COUNT;
46-
++i, v += STEP, w -= STEP) {
47-
InType x = InFPBits(v).get_val();
48-
InType y = InFPBits(w).get_val();
43+
constexpr int COUNT = 100'001;
44+
constexpr InStorageType STEP = LIBC_NAMESPACE::cpp::max(
45+
static_cast<InStorageType>((IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) /
46+
COUNT),
47+
InStorageType(1));
48+
for (InStorageType i = IN_MIN_SUBNORMAL_U; i <= IN_MAX_SUBNORMAL_U;
49+
i += STEP) {
50+
InType x = InFPBits(i).get_val();
51+
InType y = InFPBits(static_cast<InStorageType>(IN_MAX_SUBNORMAL_U - i))
52+
.get_val();
4953
mpfr::BinaryInput<InType> input{x, y};
5054
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sub, input, func(x, y),
5155
0.5);
5256
}
5357
}
5458

5559
void test_normal_range(SubFunc func) {
56-
constexpr InStorageType COUNT = 100'001;
57-
constexpr InStorageType STEP = (IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT;
58-
for (InStorageType i = 0, v = 0, w = IN_MAX_NORMAL_U; i <= COUNT;
59-
++i, v += STEP, w -= STEP) {
60-
InType x = InFPBits(v).get_val();
61-
InType y = InFPBits(w).get_val();
60+
constexpr int COUNT = 100'001;
61+
constexpr InStorageType STEP = LIBC_NAMESPACE::cpp::max(
62+
static_cast<InStorageType>((IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT),
63+
InStorageType(1));
64+
for (InStorageType i = IN_MIN_NORMAL_U; i <= IN_MAX_NORMAL_U; i += STEP) {
65+
InType x = InFPBits(i).get_val();
66+
InType y =
67+
InFPBits(static_cast<InStorageType>(IN_MAX_NORMAL_U - i)).get_val();
6268
mpfr::BinaryInput<InType> input{x, y};
6369
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sub, input, func(x, y),
6470
0.5);
6571
}
6672
}
6773
};
6874

69-
#define LIST_SUB_TESTS(suffix, OutType, InType, func) \
75+
#define LIST_SUB_TESTS(OutType, InType, func) \
76+
using LlvmLibcSubTest = SubTest<OutType, InType>; \
77+
TEST_F(LlvmLibcSubTest, SubnormalRange) { test_subnormal_range(&func); } \
78+
TEST_F(LlvmLibcSubTest, NormalRange) { test_normal_range(&func); }
79+
80+
#define LIST_SUB_SAME_TYPE_TESTS(suffix, OutType, InType, func) \
7081
using LlvmLibcSubTest##suffix = SubTest<OutType, InType>; \
7182
TEST_F(LlvmLibcSubTest##suffix, SubnormalRange) { \
7283
test_subnormal_range(&func); \

libc/test/src/math/add_same_type_test.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313

1414
#define ADD_FUNC(T) (LIBC_NAMESPACE::fputil::generic::add<T, T>)
1515

16-
LIST_ADD_TESTS(Double, double, double, ADD_FUNC(double))
17-
LIST_ADD_TESTS(Float, float, float, ADD_FUNC(float))
18-
LIST_ADD_TESTS(LongDouble, long double, long double, ADD_FUNC(long double))
16+
LIST_ADD_SAME_TYPE_TESTS(Double, double, double, ADD_FUNC(double))
17+
LIST_ADD_SAME_TYPE_TESTS(Float, float, float, ADD_FUNC(float))
18+
LIST_ADD_SAME_TYPE_TESTS(LongDouble, long double, long double,
19+
ADD_FUNC(long double))
1920
#ifdef LIBC_TYPES_HAS_FLOAT16
20-
LIST_ADD_TESTS(Float16, float16, float16, ADD_FUNC(float16))
21-
#endif
22-
#ifdef LIBC_TYPES_HAS_FLOAT128
23-
LIST_ADD_TESTS(Float128, float128, float128, ADD_FUNC(float128))
21+
LIST_ADD_SAME_TYPE_TESTS(Float16, float16, float16, ADD_FUNC(float16))
2422
#endif

libc/test/src/math/daddl_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#include "src/math/daddl.h"
1212

13-
LIST_ADD_TESTS(DoubleLongDouble, double, long double, LIBC_NAMESPACE::daddl)
13+
LIST_ADD_TESTS(double, long double, LIBC_NAMESPACE::daddl)

libc/test/src/math/f16add_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#include "src/math/f16add.h"
1212

13-
LIST_ADD_TESTS(Float16Double, float16, double, LIBC_NAMESPACE::f16add)
13+
LIST_ADD_TESTS(float16, double, LIBC_NAMESPACE::f16add)

libc/test/src/math/f16addf_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#include "src/math/f16addf.h"
1212

13-
LIST_ADD_TESTS(Float16Float, float16, float, LIBC_NAMESPACE::f16addf)
13+
LIST_ADD_TESTS(float16, float, LIBC_NAMESPACE::f16addf)

libc/test/src/math/f16addl_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#include "src/math/f16addl.h"
1212

13-
LIST_ADD_TESTS(Float16LongDouble, float16, long double, LIBC_NAMESPACE::f16addl)
13+
LIST_ADD_TESTS(float16, long double, LIBC_NAMESPACE::f16addl)

0 commit comments

Comments
 (0)