1 //===-- Unittests for sin -------------------------------------------------===//
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/FPUtil/FPBits.h"
10 #include "src/math/sin.h"
11 #include "test/UnitTest/FPMatcher.h"
12 #include "test/UnitTest/Test.h"
13 #include "utils/MPFRWrapper/MPFRUtils.h"
14
15 using LlvmLibcSinTest = LIBC_NAMESPACE::testing::FPTest<double>;
16
17 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
18
19 using LIBC_NAMESPACE::testing::tlog;
20
TEST_F(LlvmLibcSinTest,TrickyInputs)21 TEST_F(LlvmLibcSinTest, TrickyInputs) {
22 constexpr double INPUTS[] = {
23 0x1.5f09cad750ab1p+3, 0x1.fff781921b61fp15, -0x1.f635b70b92407p-21,
24 -0x1.3ecf146c39c0cp-20, 0x1.6ac5b262ca1ffp849, 0x1.6c6cbc45dc8dep5,
25 0x1.921fb5443p-7, 0x1.940c877fb7dacp-7, 0x1.fffffffffdb6p24,
26 0x1.fd4da4ef37075p29, 0x1.b951f1572eba5p+31, 0x1.55202aefde314p+31,
27 0x1.85fc0f04c0128p101, 0x1.7776c2343ba4ep101, 0x1.678309fa50d58p110,
28 0x1.fffffffffef4ep199, -0x1.ab514bfc61c76p+7, -0x1.f7898d5a756ddp+2,
29 -0x1.f42fb19b5b9b2p-6, -0x1.14823229799c2p+7, -0x1.0285070f9f1bcp-5,
30 0x1.23f40dccdef72p+0, 0x1.43cf16358c9d7p+0, 0x1.addf3b9722265p+0,
31 0x1.48ff1782ca91dp+8, 0x1.a211877de55dbp+4, 0x1.dcbfda0c7559ep+8,
32 0x1.1ffb509f3db15p+5, 0x1.2345d1e090529p+5, 0x1.ae945054939c2p+10,
33 0x1.2e566149bf5fdp+9, 0x1.be886d9c2324dp+6, -0x1.119471e9216cdp+10,
34 -0x1.aaf85537ea4c7p+3, 0x1.cb996c60f437ep+9, 0x1.c96e28eb679f8p+5,
35 -0x1.a5eece87e8606p+4, 0x1.e31b55306f22cp+2, 0x1.ae78d360afa15p+0,
36 0x1.1685973506319p+3, 0x1.4f2b874135d27p+4, 0x1.ae945054939c2p+10,
37 0x1.3eec5912ea7cdp+331, 0x1.dcbfda0c7559ep+8, 0x1.a65d441ea6dcep+4,
38 0x1.e639103a05997p+2, 0x1.13114266f9764p+4, -0x1.3eec5912ea7cdp+331,
39 0x1.08087e9aad90bp+887, 0x1.2b5fe88a9d8d5p+903, -0x1.a880417b7b119p+1023,
40 -0x1.6deb37da81129p+205, 0x1.08087e9aad90bp+887, 0x1.f6d7518808571p+1023,
41 -0x1.8bb5847d49973p+845, 0x1.f08b14e1c4d0fp+890, 0x1.6ac5b262ca1ffp+849,
42 0x1.e0000000001c2p-20,
43 };
44 constexpr int N = sizeof(INPUTS) / sizeof(INPUTS[0]);
45
46 for (int i = 0; i < N; ++i) {
47 double x = INPUTS[i];
48 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x,
49 LIBC_NAMESPACE::sin(x), 0.5);
50 }
51 }
52
TEST_F(LlvmLibcSinTest,InDoubleRange)53 TEST_F(LlvmLibcSinTest, InDoubleRange) {
54 constexpr uint64_t COUNT = 1'234'51;
55 uint64_t START = LIBC_NAMESPACE::fputil::FPBits<double>(0x1.0p-50).uintval();
56 uint64_t STOP = LIBC_NAMESPACE::fputil::FPBits<double>(0x1.0p200).uintval();
57 uint64_t STEP = (STOP - START) / COUNT;
58
59 auto test = [&](mpfr::RoundingMode rounding_mode) {
60 mpfr::ForceRoundingMode __r(rounding_mode);
61 if (!__r.success)
62 return;
63
64 uint64_t fails = 0;
65 uint64_t count = 0;
66 uint64_t cc = 0;
67 double mx, mr = 0.0;
68 double tol = 0.5;
69
70 for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
71 double x = FPBits(v).get_val();
72 if (FPBits(v).is_nan() || FPBits(v).is_inf())
73 continue;
74 LIBC_NAMESPACE::libc_errno = 0;
75 double result = LIBC_NAMESPACE::sin(x);
76 ++cc;
77 if (FPBits(result).is_nan() || FPBits(result).is_inf())
78 continue;
79
80 ++count;
81
82 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Sin, x, result,
83 0.5, rounding_mode)) {
84 ++fails;
85 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Sin, x,
86 result, tol, rounding_mode)) {
87 mx = x;
88 mr = result;
89
90 if (tol > 1000.0)
91 break;
92
93 tol *= 2.0;
94 }
95 }
96 }
97 if (fails) {
98 tlog << " Sin failed: " << fails << "/" << count << "/" << cc
99 << " tests.\n";
100 tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
101 EXPECT_MPFR_MATCH(mpfr::Operation::Sin, mx, mr, 0.5, rounding_mode);
102 }
103 };
104
105 tlog << " Test Rounding To Nearest...\n";
106 test(mpfr::RoundingMode::Nearest);
107
108 tlog << " Test Rounding Downward...\n";
109 test(mpfr::RoundingMode::Downward);
110
111 tlog << " Test Rounding Upward...\n";
112 test(mpfr::RoundingMode::Upward);
113
114 tlog << " Test Rounding Toward Zero...\n";
115 test(mpfr::RoundingMode::TowardZero);
116 }
117