Skip to content

Commit 4a6c541

Browse files
Siva Chandra Reddymemfrob
authored andcommitted
[libc][NFC] Remove few deprecated FPUtil header files and test patterns.
Few tests have been converted to the new test patterns to facilitate this.
1 parent 8094071 commit 4a6c541

25 files changed

+496
-1487
lines changed

libc/test/src/math/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,8 @@ add_fp_unittest(
656656
libc_math_unittests
657657
SRCS
658658
frexp_test.cpp
659+
HDRS
660+
FrexpTest.h
659661
DEPENDS
660662
libc.include.math
661663
libc.src.math.frexp
@@ -669,6 +671,8 @@ add_fp_unittest(
669671
libc_math_unittests
670672
SRCS
671673
frexpf_test.cpp
674+
HDRS
675+
FrexpTest.h
672676
DEPENDS
673677
libc.include.math
674678
libc.src.math.frexpf
@@ -682,6 +686,8 @@ add_fp_unittest(
682686
libc_math_unittests
683687
SRCS
684688
frexpl_test.cpp
689+
HDRS
690+
FrexpTest.h
685691
DEPENDS
686692
libc.include.math
687693
libc.src.math.frexpl
@@ -802,6 +808,8 @@ add_fp_unittest(
802808
libc_math_unittests
803809
SRCS
804810
logbl_test.cpp
811+
HDRS
812+
LogbTest.h
805813
DEPENDS
806814
libc.include.math
807815
libc.src.math.logbl
@@ -814,6 +822,8 @@ add_fp_unittest(
814822
libc_math_unittests
815823
SRCS
816824
modf_test.cpp
825+
HDRS
826+
ModfTest.h
817827
DEPENDS
818828
libc.include.math
819829
libc.src.math.modf
@@ -826,6 +836,8 @@ add_fp_unittest(
826836
libc_math_unittests
827837
SRCS
828838
modff_test.cpp
839+
HDRS
840+
ModfTest.h
829841
DEPENDS
830842
libc.include.math
831843
libc.src.math.modff
@@ -838,6 +850,8 @@ add_fp_unittest(
838850
libc_math_unittests
839851
SRCS
840852
modfl_test.cpp
853+
HDRS
854+
ModfTest.h
841855
DEPENDS
842856
libc.include.math
843857
libc.src.math.modfl

libc/test/src/math/FrexpTest.h

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//===-- Utility class to test frexp[f|l] ------------------------*- C++ -*-===//
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 "utils/FPUtil/BasicOperations.h"
10+
#include "utils/FPUtil/TestHelpers.h"
11+
#include "utils/MPFRWrapper/MPFRUtils.h"
12+
#include "utils/UnitTest/Test.h"
13+
14+
#include <math.h>
15+
16+
namespace mpfr = __llvm_libc::testing::mpfr;
17+
18+
template <typename T> class FrexpTest : public __llvm_libc::testing::Test {
19+
20+
DECLARE_SPECIAL_CONSTANTS(T)
21+
22+
static constexpr UIntType HiddenBit =
23+
UIntType(1) << __llvm_libc::fputil::MantissaWidth<T>::value;
24+
25+
public:
26+
typedef T (*FrexpFunc)(T, int *);
27+
28+
void testSpecialNumbers(FrexpFunc func) {
29+
int exponent;
30+
ASSERT_FP_EQ(aNaN, func(aNaN, &exponent));
31+
ASSERT_FP_EQ(inf, func(inf, &exponent));
32+
ASSERT_FP_EQ(negInf, func(negInf, &exponent));
33+
34+
ASSERT_FP_EQ(0.0, func(0.0, &exponent));
35+
ASSERT_EQ(exponent, 0);
36+
37+
ASSERT_FP_EQ(-0.0, func(-0.0, &exponent));
38+
ASSERT_EQ(exponent, 0);
39+
}
40+
41+
void testPowersOfTwo(FrexpFunc func) {
42+
int exponent;
43+
44+
EXPECT_FP_EQ(T(0.5), func(T(1.0), &exponent));
45+
EXPECT_EQ(exponent, 1);
46+
EXPECT_FP_EQ(T(-0.5), func(T(-1.0), &exponent));
47+
EXPECT_EQ(exponent, 1);
48+
49+
EXPECT_FP_EQ(T(0.5), func(T(2.0), &exponent));
50+
EXPECT_EQ(exponent, 2);
51+
EXPECT_FP_EQ(T(-0.5), func(T(-2.0), &exponent));
52+
EXPECT_EQ(exponent, 2);
53+
54+
EXPECT_FP_EQ(T(0.5), func(T(4.0), &exponent));
55+
EXPECT_EQ(exponent, 3);
56+
EXPECT_FP_EQ(T(-0.5), func(T(-4.0), &exponent));
57+
EXPECT_EQ(exponent, 3);
58+
59+
EXPECT_FP_EQ(T(0.5), func(T(8.0), &exponent));
60+
EXPECT_EQ(exponent, 4);
61+
EXPECT_FP_EQ(T(-0.5), func(T(-8.0), &exponent));
62+
EXPECT_EQ(exponent, 4);
63+
64+
EXPECT_FP_EQ(T(0.5), func(T(16.0), &exponent));
65+
EXPECT_EQ(exponent, 5);
66+
EXPECT_FP_EQ(T(-0.5), func(T(-16.0), &exponent));
67+
EXPECT_EQ(exponent, 5);
68+
69+
EXPECT_FP_EQ(T(0.5), func(T(32.0), &exponent));
70+
EXPECT_EQ(exponent, 6);
71+
EXPECT_FP_EQ(T(-0.5), func(T(-32.0), &exponent));
72+
EXPECT_EQ(exponent, 6);
73+
}
74+
75+
void testSomeIntegers(FrexpFunc func) {
76+
int exponent;
77+
78+
EXPECT_FP_EQ(T(0.75), func(T(24.0), &exponent));
79+
EXPECT_EQ(exponent, 5);
80+
EXPECT_FP_EQ(T(-0.75), func(T(-24.0), &exponent));
81+
EXPECT_EQ(exponent, 5);
82+
83+
EXPECT_FP_EQ(T(0.625), func(T(40.0), &exponent));
84+
EXPECT_EQ(exponent, 6);
85+
EXPECT_FP_EQ(T(-0.625), func(T(-40.0), &exponent));
86+
EXPECT_EQ(exponent, 6);
87+
88+
EXPECT_FP_EQ(T(0.78125), func(T(800.0), &exponent));
89+
EXPECT_EQ(exponent, 10);
90+
EXPECT_FP_EQ(T(-0.78125), func(T(-800.0), &exponent));
91+
EXPECT_EQ(exponent, 10);
92+
}
93+
94+
void testRange(FrexpFunc func) {
95+
using UIntType = typename FPBits::UIntType;
96+
constexpr UIntType count = 10000000;
97+
constexpr UIntType step = UIntType(-1) / count;
98+
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
99+
T x = static_cast<T>(FPBits(v));
100+
if (isnan(x) || isinf(x) || x == 0.0l)
101+
continue;
102+
103+
mpfr::BinaryOutput<T> result;
104+
result.f = func(x, &result.i);
105+
106+
ASSERT_TRUE(__llvm_libc::fputil::abs(result.f) < 1.0);
107+
ASSERT_TRUE(__llvm_libc::fputil::abs(result.f) >= 0.5);
108+
ASSERT_MPFR_MATCH(mpfr::Operation::Frexp, x, result, 0.0);
109+
}
110+
}
111+
};
112+
113+
#define LIST_FREXP_TESTS(T, func) \
114+
using LlvmLibcFrexpTest = FrexpTest<T>; \
115+
TEST_F(LlvmLibcFrexpTest, SpecialNumbers) { testSpecialNumbers(&func); } \
116+
TEST_F(LlvmLibcFrexpTest, PowersOfTwo) { testPowersOfTwo(&func); } \
117+
TEST_F(LlvmLibcFrexpTest, SomeIntegers) { testSomeIntegers(&func); } \
118+
TEST_F(LlvmLibcFrexpTest, InRange) { testRange(&func); }

libc/test/src/math/LogbTest.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//===-- Utility class to test logb[f|l] -------------------------*- C++ -*-===//
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 "utils/FPUtil/ManipulationFunctions.h"
10+
#include "utils/FPUtil/TestHelpers.h"
11+
#include "utils/MPFRWrapper/MPFRUtils.h"
12+
#include "utils/UnitTest/Test.h"
13+
14+
#include <math.h>
15+
16+
namespace mpfr = __llvm_libc::testing::mpfr;
17+
18+
template <typename T> class LogbTest : public __llvm_libc::testing::Test {
19+
20+
DECLARE_SPECIAL_CONSTANTS(T)
21+
22+
static constexpr UIntType HiddenBit =
23+
UIntType(1) << __llvm_libc::fputil::MantissaWidth<T>::value;
24+
25+
public:
26+
typedef T (*LogbFunc)(T);
27+
28+
void testSpecialNumbers(LogbFunc func) {
29+
ASSERT_FP_EQ(aNaN, func(aNaN));
30+
ASSERT_FP_EQ(inf, func(inf));
31+
ASSERT_FP_EQ(inf, func(negInf));
32+
ASSERT_FP_EQ(negInf, func(0.0));
33+
ASSERT_FP_EQ(negInf, func(-0.0));
34+
}
35+
36+
void testPowersOfTwo(LogbFunc func) {
37+
EXPECT_FP_EQ(T(0.0), func(T(1.0)));
38+
EXPECT_FP_EQ(T(0.0), func(T(-1.0)));
39+
40+
EXPECT_FP_EQ(T(1.0), func(T(2.0)));
41+
EXPECT_FP_EQ(T(1.0), func(T(-2.0)));
42+
43+
EXPECT_FP_EQ(T(2.0), func(T(4.0)));
44+
EXPECT_FP_EQ(T(2.0), func(T(-4.0)));
45+
46+
EXPECT_FP_EQ(T(3.0), func(T(8.0)));
47+
EXPECT_FP_EQ(T(3.0), func(T(-8.0)));
48+
49+
EXPECT_FP_EQ(T(4.0), func(T(16.0)));
50+
EXPECT_FP_EQ(T(4.0), func(T(-16.0)));
51+
52+
EXPECT_FP_EQ(T(5.0), func(T(32.0)));
53+
EXPECT_FP_EQ(T(5.0), func(T(-32.0)));
54+
}
55+
56+
void testSomeIntegers(LogbFunc func) {
57+
EXPECT_FP_EQ(T(1.0), func(T(3.0)));
58+
EXPECT_FP_EQ(T(1.0), func(T(-3.0)));
59+
60+
EXPECT_FP_EQ(T(2.0), func(T(7.0)));
61+
EXPECT_FP_EQ(T(2.0), func(T(-7.0)));
62+
63+
EXPECT_FP_EQ(T(3.0), func(T(10.0)));
64+
EXPECT_FP_EQ(T(3.0), func(T(-10.0)));
65+
66+
EXPECT_FP_EQ(T(4.0), func(T(31.0)));
67+
EXPECT_FP_EQ(T(4.0), func(T(-31.0)));
68+
69+
EXPECT_FP_EQ(T(5.0), func(T(55.0)));
70+
EXPECT_FP_EQ(T(5.0), func(T(-55.0)));
71+
}
72+
73+
void testRange(LogbFunc func) {
74+
using UIntType = typename FPBits::UIntType;
75+
constexpr UIntType count = 10000000;
76+
constexpr UIntType step = UIntType(-1) / count;
77+
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
78+
T x = static_cast<T>(FPBits(v));
79+
if (isnan(x) || isinf(x) || x == 0.0l)
80+
continue;
81+
82+
int exponent;
83+
__llvm_libc::fputil::frexp(x, exponent);
84+
ASSERT_FP_EQ(T(exponent), func(x) + T(1.0));
85+
}
86+
}
87+
};
88+
89+
#define LIST_LOGB_TESTS(T, func) \
90+
using LlvmLibcLogbTest = LogbTest<T>; \
91+
TEST_F(LlvmLibcLogbTest, SpecialNumbers) { testSpecialNumbers(&func); } \
92+
TEST_F(LlvmLibcLogbTest, PowersOfTwo) { testPowersOfTwo(&func); } \
93+
TEST_F(LlvmLibcLogbTest, SomeIntegers) { testSomeIntegers(&func); } \
94+
TEST_F(LlvmLibcLogbTest, InRange) { testRange(&func); }

libc/test/src/math/ModfTest.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//===-- Utility class to test floor[f|l] ------------------------*- C++ -*-===//
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 "utils/FPUtil/BasicOperations.h"
10+
#include "utils/FPUtil/NearestIntegerOperations.h"
11+
#include "utils/FPUtil/TestHelpers.h"
12+
#include "utils/MPFRWrapper/MPFRUtils.h"
13+
#include "utils/UnitTest/Test.h"
14+
15+
#include <math.h>
16+
17+
namespace mpfr = __llvm_libc::testing::mpfr;
18+
19+
template <typename T> class ModfTest : public __llvm_libc::testing::Test {
20+
21+
DECLARE_SPECIAL_CONSTANTS(T)
22+
23+
public:
24+
typedef T (*ModfFunc)(T, T *);
25+
26+
void testSpecialNumbers(ModfFunc func) {
27+
T integral;
28+
29+
EXPECT_FP_EQ(zero, func(zero, &integral));
30+
EXPECT_FP_EQ(integral, zero);
31+
EXPECT_FP_EQ(negZero, func(negZero, &integral));
32+
EXPECT_FP_EQ(integral, negZero);
33+
34+
EXPECT_FP_EQ(zero, func(inf, &integral));
35+
EXPECT_FP_EQ(inf, integral);
36+
EXPECT_FP_EQ(negZero, func(negInf, &integral));
37+
EXPECT_FP_EQ(negInf, integral);
38+
39+
EXPECT_FP_EQ(aNaN, func(aNaN, &integral));
40+
}
41+
42+
void testIntegers(ModfFunc func) {
43+
T integral;
44+
45+
EXPECT_FP_EQ(T(0.0), func(T(1.0), &integral));
46+
EXPECT_FP_EQ(T(1.0), integral);
47+
48+
EXPECT_FP_EQ(T(-0.0), func(T(-1.0), &integral));
49+
EXPECT_FP_EQ(T(-1.0), integral);
50+
51+
EXPECT_FP_EQ(T(0.0), func(T(10.0), &integral));
52+
EXPECT_FP_EQ(T(10.0), integral);
53+
54+
EXPECT_FP_EQ(T(-0.0), func(T(-10.0), &integral));
55+
EXPECT_FP_EQ(T(-10.0), integral);
56+
57+
EXPECT_FP_EQ(T(0.0), func(T(12345.0), &integral));
58+
EXPECT_FP_EQ(T(12345.0), integral);
59+
60+
EXPECT_FP_EQ(T(-0.0), func(T(-12345.0), &integral));
61+
EXPECT_FP_EQ(T(-12345.0), integral);
62+
}
63+
64+
void testFractions(ModfFunc func) {
65+
T integral;
66+
67+
EXPECT_FP_EQ(T(0.5), func(T(1.5), &integral));
68+
EXPECT_FP_EQ(integral, T(1.0));
69+
70+
EXPECT_FP_EQ(T(-0.5), func(T(-1.5), &integral));
71+
EXPECT_FP_EQ(integral, T(-1.0));
72+
73+
EXPECT_FP_EQ(T(0.75), func(T(10.75), &integral));
74+
EXPECT_FP_EQ(integral, T(10.0));
75+
76+
EXPECT_FP_EQ(T(-0.75), func(T(-10.75), &integral));
77+
EXPECT_FP_EQ(integral, T(-10.0));
78+
79+
EXPECT_FP_EQ(T(0.125), func(T(100.125), &integral));
80+
EXPECT_FP_EQ(integral, T(100.0));
81+
82+
EXPECT_FP_EQ(T(-0.125), func(T(-100.125), &integral));
83+
EXPECT_FP_EQ(integral, T(-100.0));
84+
}
85+
86+
void testRange(ModfFunc func) {
87+
constexpr UIntType count = 10000000;
88+
constexpr UIntType step = UIntType(-1) / count;
89+
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
90+
T x = T(FPBits(v));
91+
if (isnan(x) || isinf(x) || x == T(0.0))
92+
continue;
93+
94+
T integral;
95+
T frac = func(x, &integral);
96+
ASSERT_TRUE(__llvm_libc::fputil::abs(frac) < 1.0l);
97+
ASSERT_TRUE(__llvm_libc::fputil::trunc(x) == integral);
98+
ASSERT_TRUE(integral + frac == x);
99+
}
100+
}
101+
};
102+
103+
#define LIST_MODF_TESTS(T, func) \
104+
using LlvmLibcModfTest = ModfTest<T>; \
105+
TEST_F(LlvmLibcModfTest, SpecialNumbers) { testSpecialNumbers(&func); } \
106+
TEST_F(LlvmLibcModfTest, RoundedNubmers) { testIntegers(&func); } \
107+
TEST_F(LlvmLibcModfTest, Fractions) { testFractions(&func); } \
108+
TEST_F(LlvmLibcModfTest, Range) { testRange(&func); }

0 commit comments

Comments
 (0)