1 //===-- Unittests for sinf ------------------------------------------------===//
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/CPP/Array.h"
10 #include "src/__support/FPUtil/FPBits.h"
11 #include "src/math/sinf.h"
12 #include "test/src/math/sdcomp26094.h"
13 #include "utils/MPFRWrapper/MPFRUtils.h"
14 #include "utils/UnitTest/FPMatcher.h"
15 #include "utils/UnitTest/Test.h"
16 #include <math.h>
17 
18 #include <errno.h>
19 #include <stdint.h>
20 
21 using __llvm_libc::testing::SDCOMP26094_VALUES;
22 using FPBits = __llvm_libc::fputil::FPBits<float>;
23 
24 namespace mpfr = __llvm_libc::testing::mpfr;
25 
26 DECLARE_SPECIAL_CONSTANTS(float)
27 
TEST(LlvmLibcSinfTest,SpecialNumbers)28 TEST(LlvmLibcSinfTest, SpecialNumbers) {
29   errno = 0;
30 
31   EXPECT_FP_EQ(aNaN, __llvm_libc::sinf(aNaN));
32   EXPECT_MATH_ERRNO(0);
33 
34   EXPECT_FP_EQ(0.0f, __llvm_libc::sinf(0.0f));
35   EXPECT_MATH_ERRNO(0);
36 
37   EXPECT_FP_EQ(-0.0f, __llvm_libc::sinf(-0.0f));
38   EXPECT_MATH_ERRNO(0);
39 
40   EXPECT_FP_EQ(aNaN, __llvm_libc::sinf(inf));
41   EXPECT_MATH_ERRNO(EDOM);
42 
43   EXPECT_FP_EQ(aNaN, __llvm_libc::sinf(neg_inf));
44   EXPECT_MATH_ERRNO(EDOM);
45 }
46 
TEST(LlvmLibcSinfTest,InFloatRange)47 TEST(LlvmLibcSinfTest, InFloatRange) {
48   constexpr uint32_t COUNT = 1000000;
49   constexpr uint32_t STEP = UINT32_MAX / COUNT;
50   for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
51     float x = float(FPBits(v));
52     if (isnan(x) || isinf(x))
53       continue;
54     ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x,
55                                    __llvm_libc::sinf(x), 0.5);
56   }
57 }
58 
TEST(LlvmLibcSinfTest,SpecificBitPatterns)59 TEST(LlvmLibcSinfTest, SpecificBitPatterns) {
60   constexpr int N = 36;
61   constexpr uint32_t INPUTS[N] = {
62       0x3f06'0a92U, // x = pi/6
63       0x3f3a'dc51U, // x = 0x1.75b8a2p-1f
64       0x3f49'0fdbU, // x = pi/4
65       0x3f86'0a92U, // x = pi/3
66       0x3fa7'832aU, // x = 0x1.4f0654p+0f
67       0x3fc9'0fdbU, // x = pi/2
68       0x4017'1973U, // x = 0x1.2e32e6p+1f
69       0x4049'0fdbU, // x = pi
70       0x4096'cbe4U, // x = 0x1.2d97c8p+2f
71       0x40c9'0fdbU, // x = 2*pi
72       0x433b'7490U, // x = 0x1.76e92p+7f
73       0x437c'e5f1U, // x = 0x1.f9cbe2p+7f
74       0x4619'9998U, // x = 0x1.33333p+13f
75       0x474d'246fU, // x = 0x1.9a48dep+15f
76       0x4afd'ece4U, // x = 0x1.fbd9c8p+22f
77       0x4c23'32e9U, // x = 0x1.4665d2p+25f
78       0x50a3'e87fU, // x = 0x1.47d0fep+34f
79       0x5239'47f6U, // x = 0x1.728fecp+37f
80       0x53b1'46a6U, // x = 0x1.628d4cp+40f
81       0x55ca'fb2aU, // x = 0x1.95f654p+44f
82       0x588e'f060U, // x = 0x1.1de0cp+50f
83       0x5c07'bcd0U, // x = 0x1.0f79ap+57f
84       0x5ebc'fddeU, // x = 0x1.79fbbcp+62f
85       0x5fa6'eba7U, // x = 0x1.4dd74ep+64f
86       0x61a4'0b40U, // x = 0x1.48168p+68f
87       0x6386'134eU, // x = 0x1.0c269cp+72f
88       0x6589'8498U, // x = 0x1.13093p+76f
89       0x6600'0001U, // x = 0x1.000002p+77f
90       0x664e'46e4U, // x = 0x1.9c8dc8p+77f
91       0x66b0'14aaU, // x = 0x1.602954p+78f
92       0x67a9'242bU, // x = 0x1.524856p+80f
93       0x6a19'76f1U, // x = 0x1.32ede2p+85f
94       0x6c55'da58U, // x = 0x1.abb4bp+89f
95       0x6f79'be45U, // x = 0x1.f37c8ap+95f
96       0x7276'69d4U, // x = 0x1.ecd3a8p+101f
97       0x7758'4625U, // x = 0x1.b08c4ap+111f
98   };
99 
100   for (int i = 0; i < N; ++i) {
101     float x = float(FPBits(INPUTS[i]));
102     EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x,
103                                    __llvm_libc::sinf(x), 0.5);
104     EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, -x,
105                                    __llvm_libc::sinf(-x), 0.5);
106   }
107 }
108 
109 // For small values, sin(x) is x.
TEST(LlvmLibcSinfTest,SmallValues)110 TEST(LlvmLibcSinfTest, SmallValues) {
111   float x = float(FPBits(0x1780'0000U));
112   EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x, __llvm_libc::sinf(x),
113                                  0.5);
114 
115   x = float(FPBits(0x0040'0000U));
116   EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x, __llvm_libc::sinf(x),
117                                  0.5);
118 }
119 
120 // SDCOMP-26094: check sinf in the cases for which the range reducer
121 // returns values furthest beyond its nominal upper bound of pi/4.
TEST(LlvmLibcSinfTest,SDCOMP_26094)122 TEST(LlvmLibcSinfTest, SDCOMP_26094) {
123   for (uint32_t v : SDCOMP26094_VALUES) {
124     float x = float(FPBits((v)));
125     EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x,
126                                    __llvm_libc::sinf(x), 0.5);
127   }
128 }
129