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