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::sdcomp26094Values;
22 using FPBits = __llvm_libc::fputil::FPBits<float>;
23 
24 namespace mpfr = __llvm_libc::testing::mpfr;
25 
26 DECLARE_SPECIAL_CONSTANTS(float)
27 
28 TEST(LlvmLibcSinfTest, SpecialNumbers) {
29   errno = 0;
30 
31   EXPECT_FP_EQ(aNaN, __llvm_libc::sinf(aNaN));
32   EXPECT_EQ(errno, 0);
33 
34   EXPECT_FP_EQ(0.0f, __llvm_libc::sinf(0.0f));
35   EXPECT_EQ(errno, 0);
36 
37   EXPECT_FP_EQ(-0.0f, __llvm_libc::sinf(-0.0f));
38   EXPECT_EQ(errno, 0);
39 
40   errno = 0;
41   EXPECT_FP_EQ(aNaN, __llvm_libc::sinf(inf));
42   EXPECT_EQ(errno, EDOM);
43 
44   errno = 0;
45   EXPECT_FP_EQ(aNaN, __llvm_libc::sinf(neg_inf));
46   EXPECT_EQ(errno, EDOM);
47 }
48 
49 TEST(LlvmLibcSinfTest, InFloatRange) {
50   constexpr uint32_t count = 1000000;
51   constexpr uint32_t step = UINT32_MAX / count;
52   for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) {
53     float x = float(FPBits(v));
54     if (isnan(x) || isinf(x))
55       continue;
56     ASSERT_MPFR_MATCH(mpfr::Operation::Sin, x, __llvm_libc::sinf(x), 1.0);
57   }
58 }
59 
60 TEST(LlvmLibcSinfTest, SpecificBitPatterns) {
61   float x = float(FPBits(uint32_t(0xc70d39a1)));
62   EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, __llvm_libc::sinf(x), 1.0);
63 }
64 
65 // For small values, sin(x) is x.
66 TEST(LlvmLibcSinfTest, SmallValues) {
67   float x = float(FPBits(uint32_t(0x17800000)));
68   float result = __llvm_libc::sinf(x);
69   EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, result, 1.0);
70   EXPECT_FP_EQ(x, result);
71 
72   x = float(FPBits(uint32_t(0x00400000)));
73   result = __llvm_libc::sinf(x);
74   EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, result, 1.0);
75   EXPECT_FP_EQ(x, result);
76 }
77 
78 // SDCOMP-26094: check sinf in the cases for which the range reducer
79 // returns values furthest beyond its nominal upper bound of pi/4.
80 TEST(LlvmLibcSinfTest, SDCOMP_26094) {
81   for (uint32_t v : sdcomp26094Values) {
82     float x = float(FPBits((v)));
83     EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, __llvm_libc::sinf(x), 1.0);
84   }
85 }
86