• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Unittests for atanf -----------------------------------------------===//
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/atanf.h"
13 #include "test/UnitTest/FPMatcher.h"
14 #include "test/UnitTest/Test.h"
15 #include "utils/MPFRWrapper/MPFRUtils.h"
16 
17 #include <errno.h>
18 #include <stdint.h>
19 
20 using LlvmLibcAtanfTest = LIBC_NAMESPACE::testing::FPTest<float>;
21 
22 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
23 
24 // TODO: This test needs to have its checks for exceptions, errno
25 // tightened
TEST_F(LlvmLibcAtanfTest,SpecialNumbers)26 TEST_F(LlvmLibcAtanfTest, SpecialNumbers) {
27   LIBC_NAMESPACE::libc_errno = 0;
28   LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
29   EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::atanf(aNaN));
30   // TODO: Uncomment these checks later, RoundingMode affects running
31   // tests in this way https://github.com/llvm/llvm-project/issues/90653.
32   // EXPECT_FP_EXCEPTION(0);
33   EXPECT_MATH_ERRNO(0);
34 
35   LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
36   EXPECT_FP_EQ_ALL_ROUNDING(0.0f, LIBC_NAMESPACE::atanf(0.0f));
37   // TODO: Uncomment these checks later, RoundingMode affects running
38   // tests in this way https://github.com/llvm/llvm-project/issues/90653.
39   // EXPECT_FP_EXCEPTION(0);
40   EXPECT_MATH_ERRNO(0);
41 
42   LIBC_NAMESPACE::fputil::clear_except(FE_ALL_EXCEPT);
43   EXPECT_FP_EQ_ALL_ROUNDING(-0.0f, LIBC_NAMESPACE::atanf(-0.0f));
44   // TODO: Uncomment these checks later, RoundingMode affects running
45   // tests in this way https://github.com/llvm/llvm-project/issues/90653.
46   // EXPECT_FP_EXCEPTION(0);
47   EXPECT_MATH_ERRNO(0);
48 }
49 
TEST_F(LlvmLibcAtanfTest,InFloatRange)50 TEST_F(LlvmLibcAtanfTest, InFloatRange) {
51   constexpr uint32_t COUNT = 100'000;
52   const uint32_t STEP = FPBits(inf).uintval() / COUNT;
53   for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
54     float x = FPBits(v).get_val();
55     EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan, x,
56                                    LIBC_NAMESPACE::atanf(x), 0.5);
57     EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan, -x,
58                                    LIBC_NAMESPACE::atanf(-x), 0.5);
59   }
60 }
61 
62 // For small values, tanh(x) is x.
TEST_F(LlvmLibcAtanfTest,SpecialValues)63 TEST_F(LlvmLibcAtanfTest, SpecialValues) {
64   uint32_t val_arr[] = {
65       0x3d8d6b23U, // x = 0x1.1ad646p-4f
66       0x3dbb6ac7U, // x = 0x1.76d58ep-4f
67       0x3feefcfbU, // x = 0x1.ddf9f6p+0f
68       0x3ffe2ec1U, // x = 0x1.fc5d82p+0f
69       0xbd8d6b23U, // x = -0x1.1ad646p-4f
70       0xbdbb6ac7U, // x = -0x1.76d58ep-4f
71       0xbfeefcfbU, // x = -0x1.ddf9f6p+0f
72       0xbffe2ec1U, // x = -0x1.fc5d82p+0
73       0x7F800000U, // x = +Inf
74       0xFF800000U, // x = -Inf
75   };
76   for (uint32_t v : val_arr) {
77     float x = FPBits(v).get_val();
78     EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan, x,
79                                    LIBC_NAMESPACE::atanf(x), 0.5);
80   }
81 }
82