1 | //===-- Unittests for erff ------------------------------------------------===// |
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 "hdr/math_macros.h" |
10 | #include "src/__support/FPUtil/FPBits.h" |
11 | #include "src/math/erff.h" |
12 | #include "test/UnitTest/FPMatcher.h" |
13 | #include "test/UnitTest/Test.h" |
14 | #include "utils/MPFRWrapper/MPFRUtils.h" |
15 | |
16 | #include <errno.h> |
17 | #include <stdint.h> |
18 | |
19 | using LlvmLibcErffTest = LIBC_NAMESPACE::testing::FPTest<float>; |
20 | |
21 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
22 | using LIBC_NAMESPACE::testing::tlog; |
23 | |
24 | TEST_F(LlvmLibcErffTest, SpecialNumbers) { |
25 | EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::erff(aNaN)); |
26 | EXPECT_FP_EQ_ALL_ROUNDING(1.0f, LIBC_NAMESPACE::erff(inf)); |
27 | EXPECT_FP_EQ_ALL_ROUNDING(-1.0f, LIBC_NAMESPACE::erff(neg_inf)); |
28 | EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::erff(zero)); |
29 | EXPECT_FP_EQ_ALL_ROUNDING(neg_zero, LIBC_NAMESPACE::erff(neg_zero)); |
30 | } |
31 | |
32 | TEST_F(LlvmLibcErffTest, TrickyInputs) { |
33 | constexpr int N = 2; |
34 | constexpr uint32_t INPUTS[N] = { |
35 | 0x3f65'9229U, // |x| = 0x1.cb2452p-1f |
36 | 0x4004'1e6aU, // |x| = 0x1.083cd4p+1f |
37 | }; |
38 | for (int i = 0; i < N; ++i) { |
39 | float x = FPBits(INPUTS[i]).get_val(); |
40 | EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Erf, x, |
41 | LIBC_NAMESPACE::erff(x), 0.5); |
42 | EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Erf, -x, |
43 | LIBC_NAMESPACE::erff(-x), 0.5); |
44 | } |
45 | } |
46 | |
47 | TEST_F(LlvmLibcErffTest, InFloatRange) { |
48 | constexpr uint32_t COUNT = 234561; |
49 | constexpr uint32_t START = 0; // 0 |
50 | constexpr uint32_t STOP = 0x4080'0000U; // 4.0f |
51 | |
52 | constexpr uint64_t STEP = (STOP - START) / COUNT; |
53 | |
54 | auto test = [&](mpfr::RoundingMode rounding_mode) { |
55 | mpfr::ForceRoundingMode __r(rounding_mode); |
56 | if (!__r.success) |
57 | return; |
58 | |
59 | uint32_t fails = 0; |
60 | uint32_t count = 0; |
61 | uint32_t cc = 0; |
62 | float mx, mr = 0.0; |
63 | double tol = 0.5; |
64 | |
65 | for (uint32_t i = 0, v = START; i <= COUNT; ++i, v += STEP) { |
66 | float x = FPBits(v).get_val(); |
67 | if (isnan(x: x)) |
68 | continue; |
69 | |
70 | float result = LIBC_NAMESPACE::erff(x); |
71 | ++cc; |
72 | if (isnan(x: result)) |
73 | continue; |
74 | |
75 | ++count; |
76 | if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Erf, x, result, |
77 | 0.5, rounding_mode)) { |
78 | ++fails; |
79 | while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Erf, x, |
80 | result, tol, rounding_mode)) { |
81 | mx = x; |
82 | mr = result; |
83 | tol *= 2.0; |
84 | } |
85 | } |
86 | } |
87 | tlog << " Log failed: " << fails << "/" << count << "/" << cc |
88 | << " tests.\n" ; |
89 | tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n" ; |
90 | if (fails) { |
91 | EXPECT_MPFR_MATCH(mpfr::Operation::Erf, mx, mr, 0.5, rounding_mode); |
92 | } |
93 | }; |
94 | |
95 | tlog << " Test Rounding To Nearest...\n" ; |
96 | test(mpfr::RoundingMode::Nearest); |
97 | |
98 | tlog << " Test Rounding Downward...\n" ; |
99 | test(mpfr::RoundingMode::Downward); |
100 | |
101 | tlog << " Test Rounding Upward...\n" ; |
102 | test(mpfr::RoundingMode::Upward); |
103 | |
104 | tlog << " Test Rounding Toward Zero...\n" ; |
105 | test(mpfr::RoundingMode::TowardZero); |
106 | } |
107 | |