Skip to content

Commit 97ac442

Browse files
lntueaokblast
authored andcommitted
[libc][math] Add tolerance to math tests so that they still work when accurate path is skipped. (llvm#164522)
1 parent 3f295bf commit 97ac442

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+579
-190
lines changed

libc/src/__support/math/atan2f.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
#include "src/__support/FPUtil/nearest_integer.h"
1919
#include "src/__support/macros/config.h"
2020
#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
21+
#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
2122

2223
#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) && \
23-
defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT)
24+
defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT) && \
25+
defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT)
2426

2527
// We use float-float implementation to reduce size.
2628
#include "atan2f_float.h"

libc/test/UnitTest/FPMatcher.h

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "src/__support/FPUtil/fpbits_str.h"
1717
#include "src/__support/libc_errno.h"
1818
#include "src/__support/macros/config.h"
19+
#include "src/__support/macros/optimization.h"
1920
#include "src/__support/macros/properties/architectures.h"
2021
#include "test/UnitTest/ErrnoCheckingTest.h"
2122
#include "test/UnitTest/RoundingModeUtils.h"
@@ -294,42 +295,46 @@ struct ModifyMXCSR {
294295

295296
#define EXPECT_MATH_ERRNO(expected) \
296297
do { \
297-
if (math_errhandling & MATH_ERRNO) { \
298-
int actual = libc_errno; \
299-
libc_errno = 0; \
300-
EXPECT_EQ(actual, expected); \
301-
} \
298+
if ((LIBC_MATH & LIBC_MATH_NO_ERRNO) == 0) \
299+
if (math_errhandling & MATH_ERRNO) { \
300+
int actual = libc_errno; \
301+
libc_errno = 0; \
302+
EXPECT_EQ(actual, expected); \
303+
} \
302304
} while (0)
303305

304306
#define ASSERT_MATH_ERRNO(expected) \
305307
do { \
306-
if (math_errhandling & MATH_ERRNO) { \
307-
int actual = libc_errno; \
308-
libc_errno = 0; \
309-
ASSERT_EQ(actual, expected); \
310-
} \
308+
if ((LIBC_MATH & LIBC_MATH_NO_ERRNO) == 0) \
309+
if (math_errhandling & MATH_ERRNO) { \
310+
int actual = libc_errno; \
311+
libc_errno = 0; \
312+
ASSERT_EQ(actual, expected); \
313+
} \
311314
} while (0)
312315

313316
#define EXPECT_FP_EXCEPTION(expected) \
314317
do { \
315-
if (math_errhandling & MATH_ERREXCEPT) { \
316-
EXPECT_EQ( \
317-
LIBC_NAMESPACE::fputil::test_except( \
318-
static_cast<int>(FE_ALL_EXCEPT)) & \
319-
((expected) ? (expected) : static_cast<int>(FE_ALL_EXCEPT)), \
320-
(expected)); \
321-
} \
318+
if ((LIBC_MATH & LIBC_MATH_NO_EXCEPT) == 0) \
319+
if (math_errhandling & MATH_ERREXCEPT) { \
320+
EXPECT_EQ( \
321+
LIBC_NAMESPACE::fputil::test_except( \
322+
static_cast<int>(FE_ALL_EXCEPT)) & \
323+
((expected) ? (expected) : static_cast<int>(FE_ALL_EXCEPT)), \
324+
(expected)); \
325+
} \
322326
} while (0)
323327

324328
#define ASSERT_FP_EXCEPTION(expected) \
325329
do { \
326-
if (math_errhandling & MATH_ERREXCEPT) { \
327-
ASSERT_EQ( \
328-
LIBC_NAMESPACE::fputil::test_except( \
329-
static_cast<int>(FE_ALL_EXCEPT)) & \
330-
((expected) ? (expected) : static_cast<int>(FE_ALL_EXCEPT)), \
331-
(expected)); \
332-
} \
330+
if ((LIBC_MATH & LIBC_MATH_NO_EXCEPT) == 0) \
331+
if (math_errhandling & MATH_ERREXCEPT) { \
332+
ASSERT_EQ( \
333+
LIBC_NAMESPACE::fputil::test_except( \
334+
static_cast<int>(FE_ALL_EXCEPT)) & \
335+
((expected) ? (expected) : static_cast<int>(FE_ALL_EXCEPT)), \
336+
(expected)); \
337+
} \
333338
} while (0)
334339

335340
#define EXPECT_FP_EQ_WITH_EXCEPTION(expected_val, actual_val, expected_except) \

libc/test/src/math/acos_test.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/__support/macros/optimization.h"
910
#include "src/math/acos.h"
1011
#include "test/UnitTest/FPMatcher.h"
1112
#include "test/UnitTest/Test.h"
1213
#include "utils/MPFRWrapper/MPFRUtils.h"
1314

15+
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
16+
#define TOLERANCE 8
17+
#else
18+
#define TOLERANCE 0
19+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
20+
1421
using LlvmLibcAcosTest = LIBC_NAMESPACE::testing::FPTest<double>;
1522

1623
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
@@ -46,7 +53,7 @@ TEST_F(LlvmLibcAcosTest, InDoubleRange) {
4653
++count;
4754

4855
if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Acos, x, result,
49-
0.5, rounding_mode)) {
56+
TOLERANCE + 0.5, rounding_mode)) {
5057
++fails;
5158
while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Acos, x,
5259
result, tol, rounding_mode)) {

libc/test/src/math/acosf16_test.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/__support/macros/optimization.h"
910
#include "src/math/acosf16.h"
1011
#include "test/UnitTest/FPMatcher.h"
1112
#include "test/UnitTest/Test.h"
1213
#include "utils/MPFRWrapper/MPFRUtils.h"
1314

15+
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
16+
#define TOLERANCE 1
17+
#else
18+
#define TOLERANCE 0
19+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
20+
1421
using LlvmLibcAcosf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
1522

1623
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
@@ -28,7 +35,7 @@ TEST_F(LlvmLibcAcosf16Test, PositiveRange) {
2835
float16 x = FPBits(v).get_val();
2936

3037
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, x,
31-
LIBC_NAMESPACE::acosf16(x), 0.5);
38+
LIBC_NAMESPACE::acosf16(x), TOLERANCE + 0.5);
3239
}
3340
}
3441

@@ -37,6 +44,6 @@ TEST_F(LlvmLibcAcosf16Test, NegativeRange) {
3744
float16 x = FPBits(v).get_val();
3845

3946
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, x,
40-
LIBC_NAMESPACE::acosf16(x), 0.5);
47+
LIBC_NAMESPACE::acosf16(x), TOLERANCE + 0.5);
4148
}
4249
}

libc/test/src/math/acosf_test.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@
1010
#include "hdr/math_macros.h"
1111
#include "hdr/stdint_proxy.h"
1212
#include "src/__support/FPUtil/FPBits.h"
13+
#include "src/__support/macros/optimization.h"
1314
#include "src/math/acosf.h"
1415
#include "test/UnitTest/FPMatcher.h"
1516
#include "test/UnitTest/Test.h"
1617
#include "utils/MPFRWrapper/MPFRUtils.h"
1718

19+
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
20+
#define TOLERANCE 1
21+
#else
22+
#define TOLERANCE 0
23+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
24+
1825
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
1926

2027
using LlvmLibcAcosfTest = LIBC_NAMESPACE::testing::FPTest<float>;
@@ -72,8 +79,8 @@ TEST_F(LlvmLibcAcosfTest, SpecificBitPatterns) {
7279
for (int i = 0; i < N; ++i) {
7380
float x = FPBits(INPUTS[i]).get_val();
7481
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, x,
75-
LIBC_NAMESPACE::acosf(x), 0.5);
82+
LIBC_NAMESPACE::acosf(x), TOLERANCE + 0.5);
7683
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, -x,
77-
LIBC_NAMESPACE::acosf(-x), 0.5);
84+
LIBC_NAMESPACE::acosf(-x), TOLERANCE + 0.5);
7885
}
7986
}

libc/test/src/math/acoshf_test.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@
1010
#include "hdr/math_macros.h"
1111
#include "hdr/stdint_proxy.h"
1212
#include "src/__support/FPUtil/FPBits.h"
13+
#include "src/__support/macros/optimization.h"
1314
#include "src/math/acoshf.h"
1415
#include "test/UnitTest/FPMatcher.h"
1516
#include "test/UnitTest/Test.h"
1617
#include "utils/MPFRWrapper/MPFRUtils.h"
1718

19+
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
20+
#define TOLERANCE 1
21+
#else
22+
#define TOLERANCE 0
23+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
24+
1825
using LlvmLibcAcoshfTest = LIBC_NAMESPACE::testing::FPTest<float>;
1926

2027
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
@@ -68,6 +75,6 @@ TEST_F(LlvmLibcAcoshfTest, SpecificBitPatterns) {
6875
for (int i = 0; i < N; ++i) {
6976
float x = FPBits(INPUTS[i]).get_val();
7077
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acosh, x,
71-
LIBC_NAMESPACE::acoshf(x), 0.5);
78+
LIBC_NAMESPACE::acoshf(x), TOLERANCE + 0.5);
7279
}
7380
}

libc/test/src/math/asin_test.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/__support/macros/optimization.h"
910
#include "src/math/asin.h"
1011
#include "test/UnitTest/FPMatcher.h"
1112
#include "test/UnitTest/Test.h"
1213
#include "utils/MPFRWrapper/MPFRUtils.h"
1314

15+
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
16+
#define TOLERANCE 6
17+
#else
18+
#define TOLERANCE 0
19+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
20+
1421
using LlvmLibcAsinTest = LIBC_NAMESPACE::testing::FPTest<double>;
1522

1623
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
@@ -47,7 +54,7 @@ TEST_F(LlvmLibcAsinTest, InDoubleRange) {
4754
++count;
4855

4956
if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Asin, x, result,
50-
0.5, rounding_mode)) {
57+
TOLERANCE + 0.5, rounding_mode)) {
5158
++fails;
5259
while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Asin, x,
5360
result, tol, rounding_mode)) {
@@ -72,6 +79,7 @@ TEST_F(LlvmLibcAsinTest, InDoubleRange) {
7279
tlog << " Test Rounding To Nearest...\n";
7380
test(mpfr::RoundingMode::Nearest);
7481

82+
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
7583
tlog << " Test Rounding Downward...\n";
7684
test(mpfr::RoundingMode::Downward);
7785

@@ -80,4 +88,5 @@ TEST_F(LlvmLibcAsinTest, InDoubleRange) {
8088

8189
tlog << " Test Rounding Toward Zero...\n";
8290
test(mpfr::RoundingMode::TowardZero);
91+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
8392
}

libc/test/src/math/asinf_test.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@
1111
#include "hdr/math_macros.h"
1212
#include "hdr/stdint_proxy.h"
1313
#include "src/__support/FPUtil/FPBits.h"
14+
#include "src/__support/macros/optimization.h"
1415
#include "src/math/asinf.h"
1516
#include "test/UnitTest/FPMatcher.h"
1617
#include "test/UnitTest/Test.h"
1718
#include "utils/MPFRWrapper/MPFRUtils.h"
1819

20+
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
21+
#define TOLERANCE 1
22+
#else
23+
#define TOLERANCE 0
24+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
25+
1926
using LlvmLibcAsinfTest = LIBC_NAMESPACE::testing::FPTest<float>;
2027

2128
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
@@ -68,8 +75,8 @@ TEST_F(LlvmLibcAsinfTest, SpecificBitPatterns) {
6875
for (int i = 0; i < N; ++i) {
6976
float x = FPBits(INPUTS[i]).get_val();
7077
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asin, x,
71-
LIBC_NAMESPACE::asinf(x), 0.5);
78+
LIBC_NAMESPACE::asinf(x), TOLERANCE + 0.5);
7279
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asin, -x,
73-
LIBC_NAMESPACE::asinf(-x), 0.5);
80+
LIBC_NAMESPACE::asinf(-x), TOLERANCE + 0.5);
7481
}
7582
}

libc/test/src/math/asinhf16_test.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "src/__support/macros/optimization.h"
910
#include "src/math/asinhf16.h"
1011
#include "test/UnitTest/FPMatcher.h"
1112
#include "test/UnitTest/Test.h"
1213
#include "utils/MPFRWrapper/MPFRUtils.h"
1314

15+
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
16+
#define TOLERANCE 1
17+
#else
18+
#define TOLERANCE 0
19+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
20+
1421
using LlvmLibcAsinhf16Test = LIBC_NAMESPACE::testing::FPTest<float16>;
1522

1623
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
@@ -28,7 +35,8 @@ TEST_F(LlvmLibcAsinhf16Test, PositiveRange) {
2835
float16 x = FPBits(v).get_val();
2936

3037
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinh, x,
31-
LIBC_NAMESPACE::asinhf16(x), 0.5);
38+
LIBC_NAMESPACE::asinhf16(x),
39+
TOLERANCE + 0.5);
3240
}
3341
}
3442

@@ -37,6 +45,7 @@ TEST_F(LlvmLibcAsinhf16Test, NegativeRange) {
3745
float16 x = FPBits(v).get_val();
3846

3947
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinh, x,
40-
LIBC_NAMESPACE::asinhf16(x), 0.5);
48+
LIBC_NAMESPACE::asinhf16(x),
49+
TOLERANCE + 0.5);
4150
}
4251
}

libc/test/src/math/asinhf_test.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99
#include "hdr/math_macros.h"
1010
#include "hdr/stdint_proxy.h"
1111
#include "src/__support/FPUtil/FPBits.h"
12+
#include "src/__support/macros/optimization.h"
1213
#include "src/math/asinhf.h"
1314
#include "test/UnitTest/FPMatcher.h"
1415
#include "test/UnitTest/Test.h"
1516
#include "utils/MPFRWrapper/MPFRUtils.h"
1617

18+
#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
19+
#define TOLERANCE 1
20+
#else
21+
#define TOLERANCE 0
22+
#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
23+
1724
using LlvmLibcAsinhfTest = LIBC_NAMESPACE::testing::FPTest<float>;
1825

1926
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
@@ -68,8 +75,8 @@ TEST_F(LlvmLibcAsinhfTest, SpecificBitPatterns) {
6875
for (int i = 0; i < N; ++i) {
6976
float x = FPBits(INPUTS[i]).get_val();
7077
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinh, x,
71-
LIBC_NAMESPACE::asinhf(x), 0.5);
78+
LIBC_NAMESPACE::asinhf(x), TOLERANCE + 0.5);
7279
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asinh, -x,
73-
LIBC_NAMESPACE::asinhf(-x), 0.5);
80+
LIBC_NAMESPACE::asinhf(-x), TOLERANCE + 0.5);
7481
}
7582
}

0 commit comments

Comments
 (0)