xref: /aosp_15_r20/external/llvm-libc/test/src/math/coshf_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for coshf -----------------------------------------------===//
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/CPP/array.h"
11 #include "src/__support/FPUtil/FPBits.h"
12 #include "src/errno/libc_errno.h"
13 #include "src/math/coshf.h"
14 #include "test/UnitTest/FPMatcher.h"
15 #include "test/UnitTest/Test.h"
16 #include "utils/MPFRWrapper/MPFRUtils.h"
17 
18 #include <stdint.h>
19 
20 using LlvmLibcCoshfTest = LIBC_NAMESPACE::testing::FPTest<float>;
21 
22 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
23 
TEST_F(LlvmLibcCoshfTest,SpecialNumbers)24 TEST_F(LlvmLibcCoshfTest, SpecialNumbers) {
25   LIBC_NAMESPACE::libc_errno = 0;
26 
27   EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::coshf(aNaN));
28   EXPECT_MATH_ERRNO(0);
29 
30   EXPECT_FP_EQ(inf, LIBC_NAMESPACE::coshf(inf));
31   EXPECT_MATH_ERRNO(0);
32 
33   EXPECT_FP_EQ(inf, LIBC_NAMESPACE::coshf(neg_inf));
34   EXPECT_MATH_ERRNO(0);
35 
36   EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::coshf(0.0f));
37   EXPECT_MATH_ERRNO(0);
38 
39   EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::coshf(-0.0f));
40   EXPECT_MATH_ERRNO(0);
41 }
42 
TEST_F(LlvmLibcCoshfTest,Overflow)43 TEST_F(LlvmLibcCoshfTest, Overflow) {
44   LIBC_NAMESPACE::libc_errno = 0;
45   EXPECT_FP_EQ_WITH_EXCEPTION(
46       inf, LIBC_NAMESPACE::coshf(FPBits(0x7f7fffffU).get_val()), FE_OVERFLOW);
47   EXPECT_MATH_ERRNO(ERANGE);
48 
49   EXPECT_FP_EQ_WITH_EXCEPTION(
50       inf, LIBC_NAMESPACE::coshf(FPBits(0x42cffff8U).get_val()), FE_OVERFLOW);
51   EXPECT_MATH_ERRNO(ERANGE);
52 
53   EXPECT_FP_EQ_WITH_EXCEPTION(
54       inf, LIBC_NAMESPACE::coshf(FPBits(0x42d00008U).get_val()), FE_OVERFLOW);
55   EXPECT_MATH_ERRNO(ERANGE);
56 }
57 
TEST_F(LlvmLibcCoshfTest,InFloatRange)58 TEST_F(LlvmLibcCoshfTest, InFloatRange) {
59   constexpr uint32_t COUNT = 100'000;
60   constexpr uint32_t STEP = UINT32_MAX / COUNT;
61   for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
62     float x = FPBits(v).get_val();
63     if (FPBits(v).is_nan() || FPBits(v).is_inf())
64       continue;
65     ASSERT_MPFR_MATCH(mpfr::Operation::Cosh, x, LIBC_NAMESPACE::coshf(x), 0.5);
66   }
67 }
68 
TEST_F(LlvmLibcCoshfTest,SmallValues)69 TEST_F(LlvmLibcCoshfTest, SmallValues) {
70   float x = FPBits(0x17800000U).get_val();
71   float result = LIBC_NAMESPACE::coshf(x);
72   EXPECT_MPFR_MATCH(mpfr::Operation::Cosh, x, result, 0.5);
73   EXPECT_FP_EQ(1.0f, result);
74 
75   x = FPBits(0x0040000U).get_val();
76   result = LIBC_NAMESPACE::coshf(x);
77   EXPECT_MPFR_MATCH(mpfr::Operation::Cosh, x, result, 0.5);
78   EXPECT_FP_EQ(1.0f, result);
79 }
80