• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Unittests for cosf ------------------------------------------------===//
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 "include/errno.h"
10 #include "src/errno/llvmlibc_errno.h"
11 #include "src/math/cosf.h"
12 #include "test/src/math/sdcomp26094.h"
13 #include "utils/CPP/Array.h"
14 #include "utils/FPUtil/BitPatterns.h"
15 #include "utils/FPUtil/ClassificationFunctions.h"
16 #include "utils/FPUtil/FloatOperations.h"
17 #include "utils/FPUtil/FloatProperties.h"
18 #include "utils/MPFRWrapper/MPFRUtils.h"
19 #include "utils/UnitTest/Test.h"
20 #include <math.h>
21 
22 #include <stdint.h>
23 
24 using __llvm_libc::fputil::isNegativeQuietNaN;
25 using __llvm_libc::fputil::isQuietNaN;
26 using __llvm_libc::fputil::valueAsBits;
27 using __llvm_libc::fputil::valueFromBits;
28 
29 using BitPatterns = __llvm_libc::fputil::BitPatterns<float>;
30 
31 using __llvm_libc::testing::sdcomp26094Values;
32 
33 namespace mpfr = __llvm_libc::testing::mpfr;
34 
TEST(CosfTest,SpecialNumbers)35 TEST(CosfTest, SpecialNumbers) {
36   llvmlibc_errno = 0;
37 
38   EXPECT_TRUE(
39       isQuietNaN(__llvm_libc::cosf(valueFromBits(BitPatterns::aQuietNaN))));
40   EXPECT_EQ(llvmlibc_errno, 0);
41 
42   EXPECT_TRUE(isNegativeQuietNaN(
43       __llvm_libc::cosf(valueFromBits(BitPatterns::aNegativeQuietNaN))));
44   EXPECT_EQ(llvmlibc_errno, 0);
45 
46   EXPECT_TRUE(isQuietNaN(
47       __llvm_libc::cosf(valueFromBits(BitPatterns::aSignallingNaN))));
48   EXPECT_EQ(llvmlibc_errno, 0);
49 
50   EXPECT_TRUE(isNegativeQuietNaN(
51       __llvm_libc::cosf(valueFromBits(BitPatterns::aNegativeSignallingNaN))));
52   EXPECT_EQ(llvmlibc_errno, 0);
53 
54   EXPECT_EQ(BitPatterns::one,
55             valueAsBits(__llvm_libc::cosf(valueFromBits(BitPatterns::zero))));
56   EXPECT_EQ(llvmlibc_errno, 0);
57 
58   EXPECT_EQ(BitPatterns::one, valueAsBits(__llvm_libc::cosf(
59                                   valueFromBits(BitPatterns::negZero))));
60   EXPECT_EQ(llvmlibc_errno, 0);
61 
62   llvmlibc_errno = 0;
63   EXPECT_TRUE(isQuietNaN(__llvm_libc::cosf(valueFromBits(BitPatterns::inf))));
64   EXPECT_EQ(llvmlibc_errno, EDOM);
65 
66   llvmlibc_errno = 0;
67   EXPECT_TRUE(
68       isQuietNaN(__llvm_libc::cosf(valueFromBits(BitPatterns::negInf))));
69   EXPECT_EQ(llvmlibc_errno, EDOM);
70 }
71 
TEST(CosfTest,InFloatRange)72 TEST(CosfTest, InFloatRange) {
73   constexpr uint32_t count = 1000000;
74   constexpr uint32_t step = UINT32_MAX / count;
75   for (uint32_t i = 0, v = 0; i <= count; ++i, v += step) {
76     float x = valueFromBits(v);
77     if (isnan(x) || isinf(x))
78       continue;
79     ASSERT_MPFR_MATCH(mpfr::Operation::Cos, x, __llvm_libc::cosf(x), 1.0);
80   }
81 }
82 
83 // For small values, cos(x) is 1.
TEST(CosfTest,SmallValues)84 TEST(CosfTest, SmallValues) {
85   float x = valueFromBits(0x17800000U);
86   float result = __llvm_libc::cosf(x);
87   EXPECT_MPFR_MATCH(mpfr::Operation::Cos, x, result, 1.0);
88   EXPECT_EQ(BitPatterns::one, valueAsBits(result));
89 
90   x = valueFromBits(0x0040000U);
91   result = __llvm_libc::cosf(x);
92   EXPECT_MPFR_MATCH(mpfr::Operation::Cos, x, result, 1.0);
93   EXPECT_EQ(BitPatterns::one, valueAsBits(result));
94 }
95 
96 // SDCOMP-26094: check cosf in the cases for which the range reducer
97 // returns values furthest beyond its nominal upper bound of pi/4.
TEST(CosfTest,SDCOMP_26094)98 TEST(CosfTest, SDCOMP_26094) {
99   for (uint32_t v : sdcomp26094Values) {
100     float x = valueFromBits(v);
101     ASSERT_MPFR_MATCH(mpfr::Operation::Cos, x, __llvm_libc::cosf(x), 1.0);
102   }
103 }
104