xref: /aosp_15_r20/external/llvm-libc/test/src/math/sincosf_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for sincosf ---------------------------------------------===//
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/errno/libc_errno.h"
12 #include "src/math/sincosf.h"
13 #include "test/UnitTest/FPMatcher.h"
14 #include "test/UnitTest/Test.h"
15 #include "test/src/math/sdcomp26094.h"
16 #include "utils/MPFRWrapper/MPFRUtils.h"
17 
18 #include <stdint.h>
19 
20 using LlvmLibcSinCosfTest = LIBC_NAMESPACE::testing::FPTest<float>;
21 
22 using LIBC_NAMESPACE::testing::SDCOMP26094_VALUES;
23 
24 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
25 
TEST_F(LlvmLibcSinCosfTest,SpecialNumbers)26 TEST_F(LlvmLibcSinCosfTest, SpecialNumbers) {
27   LIBC_NAMESPACE::libc_errno = 0;
28   float sin, cos;
29 
30   LIBC_NAMESPACE::sincosf(aNaN, &sin, &cos);
31   EXPECT_FP_EQ(aNaN, cos);
32   EXPECT_FP_EQ(aNaN, sin);
33   EXPECT_MATH_ERRNO(0);
34 
35   LIBC_NAMESPACE::sincosf(0.0f, &sin, &cos);
36   EXPECT_FP_EQ(1.0f, cos);
37   EXPECT_FP_EQ(0.0f, sin);
38   EXPECT_MATH_ERRNO(0);
39 
40   LIBC_NAMESPACE::sincosf(-0.0f, &sin, &cos);
41   EXPECT_FP_EQ(1.0f, cos);
42   EXPECT_FP_EQ(-0.0f, sin);
43   EXPECT_MATH_ERRNO(0);
44 
45   LIBC_NAMESPACE::sincosf(inf, &sin, &cos);
46   EXPECT_FP_EQ(aNaN, cos);
47   EXPECT_FP_EQ(aNaN, sin);
48   EXPECT_MATH_ERRNO(EDOM);
49 
50   LIBC_NAMESPACE::sincosf(neg_inf, &sin, &cos);
51   EXPECT_FP_EQ(aNaN, cos);
52   EXPECT_FP_EQ(aNaN, sin);
53   EXPECT_MATH_ERRNO(EDOM);
54 }
55 
56 #define EXPECT_SINCOS_MATCH_ALL_ROUNDING(input)                                \
57   {                                                                            \
58     float sin, cos;                                                            \
59     namespace mpfr = LIBC_NAMESPACE::testing::mpfr;                            \
60                                                                                \
61     mpfr::ForceRoundingMode __r1(mpfr::RoundingMode::Nearest);                 \
62     if (__r1.success) {                                                        \
63       LIBC_NAMESPACE::sincosf(input, &sin, &cos);                              \
64       EXPECT_MPFR_MATCH(mpfr::Operation::Sin, input, sin, 0.5,                 \
65                         mpfr::RoundingMode::Nearest);                          \
66       EXPECT_MPFR_MATCH(mpfr::Operation::Cos, input, cos, 0.5,                 \
67                         mpfr::RoundingMode::Nearest);                          \
68     }                                                                          \
69                                                                                \
70     mpfr::ForceRoundingMode __r2(mpfr::RoundingMode::Upward);                  \
71     if (__r2.success) {                                                        \
72       LIBC_NAMESPACE::sincosf(input, &sin, &cos);                              \
73       EXPECT_MPFR_MATCH(mpfr::Operation::Sin, input, sin, 0.5,                 \
74                         mpfr::RoundingMode::Upward);                           \
75       EXPECT_MPFR_MATCH(mpfr::Operation::Cos, input, cos, 0.5,                 \
76                         mpfr::RoundingMode::Upward);                           \
77     }                                                                          \
78                                                                                \
79     mpfr::ForceRoundingMode __r3(mpfr::RoundingMode::Downward);                \
80     if (__r3.success) {                                                        \
81       LIBC_NAMESPACE::sincosf(input, &sin, &cos);                              \
82       EXPECT_MPFR_MATCH(mpfr::Operation::Sin, input, sin, 0.5,                 \
83                         mpfr::RoundingMode::Downward);                         \
84       EXPECT_MPFR_MATCH(mpfr::Operation::Cos, input, cos, 0.5,                 \
85                         mpfr::RoundingMode::Downward);                         \
86     }                                                                          \
87                                                                                \
88     mpfr::ForceRoundingMode __r4(mpfr::RoundingMode::TowardZero);              \
89     if (__r4.success) {                                                        \
90       LIBC_NAMESPACE::sincosf(input, &sin, &cos);                              \
91       EXPECT_MPFR_MATCH(mpfr::Operation::Sin, input, sin, 0.5,                 \
92                         mpfr::RoundingMode::TowardZero);                       \
93       EXPECT_MPFR_MATCH(mpfr::Operation::Cos, input, cos, 0.5,                 \
94                         mpfr::RoundingMode::TowardZero);                       \
95     }                                                                          \
96   }
97 
TEST_F(LlvmLibcSinCosfTest,InFloatRange)98 TEST_F(LlvmLibcSinCosfTest, InFloatRange) {
99   constexpr uint32_t COUNT = 1'001;
100   constexpr uint32_t STEP = UINT32_MAX / COUNT;
101   for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
102     float x = FPBits(v).get_val();
103     if (FPBits(v).is_nan() || FPBits(v).is_inf())
104       continue;
105 
106     EXPECT_SINCOS_MATCH_ALL_ROUNDING(x);
107     EXPECT_SINCOS_MATCH_ALL_ROUNDING(-x);
108   }
109 }
110 
111 // For hard to round inputs.
TEST_F(LlvmLibcSinCosfTest,SpecialValues)112 TEST_F(LlvmLibcSinCosfTest, SpecialValues) {
113   constexpr int N = 43;
114   constexpr uint32_t INPUTS[N] = {
115       0x3b56'37f5U, // x = 0x1.ac6feap-9f
116       0x3f06'0a92U, // x = pi/6
117       0x3f3a'dc51U, // x = 0x1.75b8a2p-1f
118       0x3f49'0fdbU, // x = pi/4
119       0x3f86'0a92U, // x = pi/3
120       0x3fa7'832aU, // x = 0x1.4f0654p+0f
121       0x3fc9'0fdbU, // x = pi/2
122       0x4017'1973U, // x = 0x1.2e32e6p+1f
123       0x4049'0fdbU, // x = pi
124       0x4096'cbe4U, // x = 0x1.2d97c8p+2f
125       0x40c9'0fdbU, // x = 2*pi
126       0x433b'7490U, // x = 0x1.76e92p+7f
127       0x437c'e5f1U, // x = 0x1.f9cbe2p+7f
128       0x4619'9998U, // x = 0x1.33333p+13f
129       0x474d'246fU, // x = 0x1.9a48dep+15f
130       0x4afd'ece4U, // x = 0x1.fbd9c8p+22f
131       0x4c23'32e9U, // x = 0x1.4665d2p+25f
132       0x50a3'e87fU, // x = 0x1.47d0fep+34f
133       0x5239'47f6U, // x = 0x1.728fecp+37f
134       0x53b1'46a6U, // x = 0x1.628d4cp+40f
135       0x5532'5019U, // x = 0x1.64a032p+43f
136       0x55ca'fb2aU, // x = 0x1.95f654p+44f
137       0x588e'f060U, // x = 0x1.1de0cp+50f
138       0x5922'aa80U, // x = 0x1.4555p+51f
139       0x5aa4'542cU, // x = 0x1.48a858p+54f
140       0x5c07'bcd0U, // x = 0x1.0f79ap+57f
141       0x5ebc'fddeU, // x = 0x1.79fbbcp+62f
142       0x5f18'b878U, // x = 0x1.3170fp+63f
143       0x5fa6'eba7U, // x = 0x1.4dd74ep+64f
144       0x6115'cb11U, // x = 0x1.2b9622p+67f
145       0x61a4'0b40U, // x = 0x1.48168p+68f
146       0x6386'134eU, // x = 0x1.0c269cp+72f
147       0x6589'8498U, // x = 0x1.13093p+76f
148       0x6600'0001U, // x = 0x1.000002p+77f
149       0x664e'46e4U, // x = 0x1.9c8dc8p+77f
150       0x66b0'14aaU, // x = 0x1.602954p+78f
151       0x67a9'242bU, // x = 0x1.524856p+80f
152       0x6a19'76f1U, // x = 0x1.32ede2p+85f
153       0x6c55'da58U, // x = 0x1.abb4bp+89f
154       0x6f79'be45U, // x = 0x1.f37c8ap+95f
155       0x7276'69d4U, // x = 0x1.ecd3a8p+101f
156       0x7758'4625U, // x = 0x1.b08c4ap+111f
157       0x7bee'f5efU, // x = 0x1.ddebdep+120f
158   };
159 
160   for (int i = 0; i < N; ++i) {
161     float x = FPBits(INPUTS[i]).get_val();
162     EXPECT_SINCOS_MATCH_ALL_ROUNDING(x);
163     EXPECT_SINCOS_MATCH_ALL_ROUNDING(-x);
164   }
165 }
166 
167 // SDCOMP-26094: check sinf in the cases for which the range reducer
168 // returns values furthest beyond its nominal upper bound of pi/4.
TEST_F(LlvmLibcSinCosfTest,SDCOMP_26094)169 TEST_F(LlvmLibcSinCosfTest, SDCOMP_26094) {
170   for (uint32_t v : SDCOMP26094_VALUES) {
171     float x = FPBits(v).get_val();
172     EXPECT_SINCOS_MATCH_ALL_ROUNDING(x);
173     EXPECT_SINCOS_MATCH_ALL_ROUNDING(-x);
174   }
175 }
176