xref: /aosp_15_r20/external/llvm-libc/test/src/math/sincos_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for sincos ----------------------------------------------===//
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/sincos.h"
11 #include "test/UnitTest/FPMatcher.h"
12 #include "test/UnitTest/Test.h"
13 #include "utils/MPFRWrapper/MPFRUtils.h"
14 
15 using LlvmLibcSincosTest = LIBC_NAMESPACE::testing::FPTest<double>;
16 
17 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
18 
19 using LIBC_NAMESPACE::testing::tlog;
20 
21 #define ASSERT_SINCOS_MATCH_ALL_ROUNDING(input)                                \
22   do {                                                                         \
23     double sin_x, cos_x;                                                       \
24     namespace mpfr = LIBC_NAMESPACE::testing::mpfr;                            \
25                                                                                \
26     mpfr::ForceRoundingMode __r1(mpfr::RoundingMode::Nearest);                 \
27     if (__r1.success) {                                                        \
28       LIBC_NAMESPACE::sincos(input, &sin_x, &cos_x);                           \
29       ASSERT_MPFR_MATCH(mpfr::Operation::Sin, input, sin_x, 0.5,               \
30                         mpfr::RoundingMode::Nearest);                          \
31       ASSERT_MPFR_MATCH(mpfr::Operation::Cos, input, cos_x, 0.5,               \
32                         mpfr::RoundingMode::Nearest);                          \
33     }                                                                          \
34                                                                                \
35     mpfr::ForceRoundingMode __r2(mpfr::RoundingMode::Upward);                  \
36     if (__r2.success) {                                                        \
37       LIBC_NAMESPACE::sincos(input, &sin_x, &cos_x);                           \
38       ASSERT_MPFR_MATCH(mpfr::Operation::Sin, input, sin_x, 0.5,               \
39                         mpfr::RoundingMode::Upward);                           \
40       ASSERT_MPFR_MATCH(mpfr::Operation::Cos, input, cos_x, 0.5,               \
41                         mpfr::RoundingMode::Upward);                           \
42     }                                                                          \
43                                                                                \
44     mpfr::ForceRoundingMode __r3(mpfr::RoundingMode::Downward);                \
45     if (__r3.success) {                                                        \
46       LIBC_NAMESPACE::sincos(input, &sin_x, &cos_x);                           \
47       ASSERT_MPFR_MATCH(mpfr::Operation::Sin, input, sin_x, 0.5,               \
48                         mpfr::RoundingMode::Downward);                         \
49       ASSERT_MPFR_MATCH(mpfr::Operation::Cos, input, cos_x, 0.5,               \
50                         mpfr::RoundingMode::Downward);                         \
51     }                                                                          \
52                                                                                \
53     mpfr::ForceRoundingMode __r4(mpfr::RoundingMode::TowardZero);              \
54     if (__r4.success) {                                                        \
55       LIBC_NAMESPACE::sincos(input, &sin_x, &cos_x);                           \
56       ASSERT_MPFR_MATCH(mpfr::Operation::Sin, input, sin_x, 0.5,               \
57                         mpfr::RoundingMode::TowardZero);                       \
58       ASSERT_MPFR_MATCH(mpfr::Operation::Cos, input, cos_x, 0.5,               \
59                         mpfr::RoundingMode::TowardZero);                       \
60     }                                                                          \
61   } while (0)
62 
TEST_F(LlvmLibcSincosTest,TrickyInputs)63 TEST_F(LlvmLibcSincosTest, TrickyInputs) {
64   constexpr double INPUTS[] = {
65       0x1.8000000000009p-23,    0x1.8000000000024p-22,
66       0x1.800000000009p-21,     0x1.20000000000f3p-20,
67       0x1.800000000024p-20,     0x1.e0000000001c2p-20,
68       0x1.940c877fb7dacp-7,     -0x1.f42fb19b5b9b2p-6,
69       -0x1.0285070f9f1bcp-5,    0x1.23f40dccdef72p+0,
70       0x1.43cf16358c9d7p+0,     0x1.addf3b9722265p+0,
71       0x1.ae78d360afa15p+0,     0x1.e31b55306f22cp+2,
72       0x1.e639103a05997p+2,     -0x1.f7898d5a756ddp+2,
73       0x1.1685973506319p+3,     0x1.5f09cad750ab1p+3,
74       -0x1.aaf85537ea4c7p+3,    0x1.4f2b874135d27p+4,
75       0x1.13114266f9764p+4,     0x1.a211877de55dbp+4,
76       -0x1.a5eece87e8606p+4,    0x1.a65d441ea6dcep+4,
77       0x1.1ffb509f3db15p+5,     0x1.2345d1e090529p+5,
78       0x1.c96e28eb679f8p+5,     0x1.be886d9c2324dp+6,
79       -0x1.ab514bfc61c76p+7,    -0x1.14823229799c2p+7,
80       0x1.48ff1782ca91dp+8,     0x1.dcbfda0c7559ep+8,
81       0x1.dcbfda0c7559ep+8,     0x1.2e566149bf5fdp+9,
82       0x1.cb996c60f437ep+9,     0x1.ae945054939c2p+10,
83       -0x1.119471e9216cdp+10,   0x1.ae945054939c2p+10,
84       0x1.fffffffffdb6p+24,     0x1.fd4da4ef37075p+29,
85       0x1.55202aefde314p+31,    0x1.b951f1572eba5p+31,
86       0x1.7776c2343ba4ep+101,   0x1.85fc0f04c0128p+101,
87       0x1.678309fa50d58p+110,   0x1.fffffffffef4ep+199,
88       0x1.3eec5912ea7cdp+331,   -0x1.3eec5912ea7cdp+331,
89       -0x1.6deb37da81129p+205,  0x1.08087e9aad90bp+887,
90       -0x1.08087e9aad90bp+887,  -0x1.8bb5847d49973p+845,
91       0x1.6ac5b262ca1ffp+849,   0x1.f08b14e1c4d0fp+890,
92       0x1.2b5fe88a9d8d5p+903,   0x1.f6d7518808571p+1023,
93       -0x1.a880417b7b119p+1023, 0x1.00a33764a0a83p-7,
94       0x1.fe81868fc47fep+1,     0x1.0da8cc189b47dp-10,
95       0x1.da1838053b866p+5,
96 
97   };
98   constexpr int N = sizeof(INPUTS) / sizeof(INPUTS[0]);
99 
100   for (int i = 0; i < N; ++i) {
101     ASSERT_SINCOS_MATCH_ALL_ROUNDING(INPUTS[i]);
102   }
103 }
104 
TEST_F(LlvmLibcSincosTest,InDoubleRange)105 TEST_F(LlvmLibcSincosTest, InDoubleRange) {
106   constexpr uint64_t COUNT = 123'41;
107   uint64_t START = LIBC_NAMESPACE::fputil::FPBits<double>(0x1.0p-50).uintval();
108   uint64_t STOP = LIBC_NAMESPACE::fputil::FPBits<double>(0x1.0p200).uintval();
109   uint64_t STEP = (STOP - START) / COUNT;
110 
111   for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {
112     double x = FPBits(v).get_val();
113     if (FPBits(v).is_nan() || FPBits(v).is_inf())
114       continue;
115 
116     ASSERT_SINCOS_MATCH_ALL_ROUNDING(x);
117   }
118 }
119