1 | //===-- Unittests for acosf -----------------------------------------------===// |
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/acosf.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 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
21 | |
22 | using LlvmLibcAcosfTest = LIBC_NAMESPACE::testing::FPTest<float>; |
23 | |
24 | TEST_F(LlvmLibcAcosfTest, SpecialNumbers) { |
25 | LIBC_NAMESPACE::libc_errno = 0; |
26 | |
27 | EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosf(aNaN)); |
28 | EXPECT_MATH_ERRNO(0); |
29 | |
30 | EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosf(inf)); |
31 | EXPECT_MATH_ERRNO(EDOM); |
32 | |
33 | EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosf(neg_inf)); |
34 | EXPECT_MATH_ERRNO(EDOM); |
35 | |
36 | EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::acosf(1.0f)); |
37 | EXPECT_MATH_ERRNO(0); |
38 | |
39 | EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosf(2.0f)); |
40 | EXPECT_MATH_ERRNO(EDOM); |
41 | |
42 | EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::acosf(-2.0f)); |
43 | EXPECT_MATH_ERRNO(EDOM); |
44 | } |
45 | |
46 | TEST_F(LlvmLibcAcosfTest, InFloatRange) { |
47 | constexpr uint32_t COUNT = 100'000; |
48 | constexpr uint32_t STEP = UINT32_MAX / COUNT; |
49 | for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) { |
50 | float x = FPBits(v).get_val(); |
51 | if (isnan(x: x) || isinf(x: x)) |
52 | continue; |
53 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, x, |
54 | LIBC_NAMESPACE::acosf(x), 0.5); |
55 | } |
56 | } |
57 | |
58 | TEST_F(LlvmLibcAcosfTest, SpecificBitPatterns) { |
59 | constexpr int N = 13; |
60 | constexpr uint32_t INPUTS[N] = { |
61 | 0x3f000000, // x = 0.5f |
62 | 0x3f3504f3, // x = sqrt(2)/2, FE_DOWNWARD |
63 | 0x3f3504f4, // x = sqrt(2)/2, FE_UPWARD |
64 | 0x3f5db3d7, // x = sqrt(3)/2, FE_DOWNWARD |
65 | 0x3f5db3d8, // x = sqrt(3)/2, FE_UPWARD |
66 | 0x3f800000, // x = 1.0f |
67 | 0x40000000, // x = 2.0f |
68 | 0x328885a3, // x = 0x1.110b46p-26 |
69 | 0x39826222, // x = 0x1.04c444p-12 |
70 | 0x3d09bf86, // x = 0x1.137f0cp-5f |
71 | 0x3de5fa1e, // x = 0x1.cbf43cp-4f |
72 | 0x3f083a1a, // x = 0x1.107434p-1f |
73 | 0x3f7741b6, // x = 0x1.ee836cp-1f |
74 | }; |
75 | |
76 | for (int i = 0; i < N; ++i) { |
77 | float x = FPBits(INPUTS[i]).get_val(); |
78 | EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, x, |
79 | LIBC_NAMESPACE::acosf(x), 0.5); |
80 | EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acos, -x, |
81 | LIBC_NAMESPACE::acosf(-x), 0.5); |
82 | } |
83 | } |
84 | |