1 | //===-- Unittests for asinf |
2 | //------------------------------------------------===// |
3 | // |
4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
5 | // See https://llvm.org/LICENSE.txt for license information. |
6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | #include "hdr/math_macros.h" |
11 | #include "src/__support/FPUtil/FPBits.h" |
12 | #include "src/errno/libc_errno.h" |
13 | #include "src/math/asinf.h" |
14 | #include "test/UnitTest/FPMatcher.h" |
15 | #include "test/UnitTest/Test.h" |
16 | #include "utils/MPFRWrapper/MPFRUtils.h" |
17 | |
18 | #include <errno.h> |
19 | #include <stdint.h> |
20 | |
21 | using LlvmLibcAsinfTest = LIBC_NAMESPACE::testing::FPTest<float>; |
22 | |
23 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
24 | |
25 | TEST_F(LlvmLibcAsinfTest, SpecialNumbers) { |
26 | LIBC_NAMESPACE::libc_errno = 0; |
27 | |
28 | EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::asinf(aNaN)); |
29 | EXPECT_MATH_ERRNO(0); |
30 | |
31 | EXPECT_FP_EQ_ALL_ROUNDING(0.0f, LIBC_NAMESPACE::asinf(0.0f)); |
32 | EXPECT_MATH_ERRNO(0); |
33 | |
34 | EXPECT_FP_EQ_ALL_ROUNDING(-0.0f, LIBC_NAMESPACE::asinf(-0.0f)); |
35 | EXPECT_MATH_ERRNO(0); |
36 | |
37 | EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::asinf(inf)); |
38 | EXPECT_MATH_ERRNO(EDOM); |
39 | |
40 | EXPECT_FP_EQ_ALL_ROUNDING(aNaN, LIBC_NAMESPACE::asinf(neg_inf)); |
41 | EXPECT_MATH_ERRNO(EDOM); |
42 | } |
43 | |
44 | TEST_F(LlvmLibcAsinfTest, InFloatRange) { |
45 | constexpr uint32_t COUNT = 100'000; |
46 | constexpr uint32_t STEP = UINT32_MAX / COUNT; |
47 | for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) { |
48 | float x = FPBits(v).get_val(); |
49 | if (isnan(x: x) || isinf(x: x)) |
50 | continue; |
51 | ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asin, x, |
52 | LIBC_NAMESPACE::asinf(x), 0.5); |
53 | } |
54 | } |
55 | |
56 | TEST_F(LlvmLibcAsinfTest, SpecificBitPatterns) { |
57 | constexpr int N = 11; |
58 | constexpr uint32_t INPUTS[N] = { |
59 | 0x3f000000, // x = 0.5f |
60 | 0x3f3504f3, // x = sqrt(2)/2, FE_DOWNWARD |
61 | 0x3f3504f4, // x = sqrt(2)/2, FE_UPWARD |
62 | 0x3f5db3d7, // x = sqrt(3)/2, FE_DOWNWARD |
63 | 0x3f5db3d8, // x = sqrt(3)/2, FE_UPWARD |
64 | 0x3f800000, // x = 1.0f |
65 | 0x40000000, // x = 2.0f |
66 | 0x3d09bf86, // x = 0x1.137f0cp-5f |
67 | 0x3de5fa1e, // x = 0x1.cbf43cp-4f |
68 | 0x3f083a1a, // x = 0x1.107434p-1f |
69 | 0x3f7741b6, // x = 0x1.ee836cp-1f |
70 | }; |
71 | |
72 | for (int i = 0; i < N; ++i) { |
73 | float x = FPBits(INPUTS[i]).get_val(); |
74 | EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asin, x, |
75 | LIBC_NAMESPACE::asinf(x), 0.5); |
76 | EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Asin, -x, |
77 | LIBC_NAMESPACE::asinf(-x), 0.5); |
78 | } |
79 | } |
80 | |