[OptTable] Fix typo VALUE => VALUES (NFCI) (#121523)
[llvm-project.git] / libc / test / src / math / tanhf_test.cpp
blob389abe4d858979bc4f60653eb1c936b025876460
1 //===-- Unittests for tanhf -----------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "hdr/math_macros.h"
10 #include "src/__support/FPUtil/FPBits.h"
11 #include "src/errno/libc_errno.h"
12 #include "src/math/tanhf.h"
13 #include "test/UnitTest/FPMatcher.h"
14 #include "test/UnitTest/Test.h"
15 #include "utils/MPFRWrapper/MPFRUtils.h"
17 #include <stdint.h>
19 using LlvmLibcTanhfTest = LIBC_NAMESPACE::testing::FPTest<float>;
21 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
23 TEST_F(LlvmLibcTanhfTest, SpecialNumbers) {
24 LIBC_NAMESPACE::libc_errno = 0;
26 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::tanhf(aNaN));
27 EXPECT_MATH_ERRNO(0);
29 EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::tanhf(0.0f));
30 EXPECT_MATH_ERRNO(0);
32 EXPECT_FP_EQ(-0.0f, LIBC_NAMESPACE::tanhf(-0.0f));
33 EXPECT_MATH_ERRNO(0);
35 EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::tanhf(inf));
36 EXPECT_MATH_ERRNO(0);
38 EXPECT_FP_EQ(-1.0f, LIBC_NAMESPACE::tanhf(neg_inf));
39 EXPECT_MATH_ERRNO(0);
42 TEST_F(LlvmLibcTanhfTest, InFloatRange) {
43 constexpr uint32_t COUNT = 100'001;
44 constexpr uint32_t STEP = UINT32_MAX / COUNT;
45 for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
46 float x = FPBits(v).get_val();
47 if (FPBits(v).is_nan() || FPBits(v).is_inf())
48 continue;
49 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh, x,
50 LIBC_NAMESPACE::tanhf(x), 0.5);
54 TEST_F(LlvmLibcTanhfTest, ExceptionalValues) {
55 constexpr int N = 4;
56 constexpr uint32_t INPUTS[N] = {
57 0x0040'0000,
58 0x1780'0000,
59 0x3a12'85ff,
60 0x4058'e0a3,
63 for (int i = 0; i < N; ++i) {
64 float x = FPBits(INPUTS[i]).get_val();
65 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh, x,
66 LIBC_NAMESPACE::tanhf(x), 0.5);
67 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh, -x,
68 LIBC_NAMESPACE::tanhf(-x), 0.5);