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 
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 
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(mpfr::Operation::Sin, x, __llvm_libc::sinf(x), 1.0);
55   }
56 }
57 
58 TEST(LlvmLibcSinfTest, SpecificBitPatterns) {
59   float x = float(FPBits(uint32_t(0xc70d39a1)));
60   EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, __llvm_libc::sinf(x), 1.0);
61 }
62 
63 // For small values, sin(x) is x.
64 TEST(LlvmLibcSinfTest, SmallValues) {
65   float x = float(FPBits(uint32_t(0x17800000)));
66   float result = __llvm_libc::sinf(x);
67   EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, result, 1.0);
68   EXPECT_FP_EQ(x, result);
69 
70   x = float(FPBits(uint32_t(0x00400000)));
71   result = __llvm_libc::sinf(x);
72   EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, result, 1.0);
73   EXPECT_FP_EQ(x, result);
74 }
75 
76 // SDCOMP-26094: check sinf in the cases for which the range reducer
77 // returns values furthest beyond its nominal upper bound of pi/4.
78 TEST(LlvmLibcSinfTest, SDCOMP_26094) {
79   for (uint32_t v : SDCOMP26094_VALUES) {
80     float x = float(FPBits((v)));
81     EXPECT_MPFR_MATCH(mpfr::Operation::Sin, x, __llvm_libc::sinf(x), 1.0);
82   }
83 }
84