brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.1 KiB · 106a51a Raw
68 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/math_macros.h"10#include "src/__support/FPUtil/FPBits.h"11#include "src/math/tanhf.h"12#include "test/UnitTest/FPMatcher.h"13#include "test/UnitTest/Test.h"14#include "utils/MPFRWrapper/MPFRUtils.h"15 16#include "hdr/stdint_proxy.h"17 18using LlvmLibcTanhfTest = LIBC_NAMESPACE::testing::FPTest<float>;19 20namespace mpfr = LIBC_NAMESPACE::testing::mpfr;21 22TEST_F(LlvmLibcTanhfTest, SpecialNumbers) {23  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::tanhf(aNaN));24  EXPECT_MATH_ERRNO(0);25 26  EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::tanhf(0.0f));27  EXPECT_MATH_ERRNO(0);28 29  EXPECT_FP_EQ(-0.0f, LIBC_NAMESPACE::tanhf(-0.0f));30  EXPECT_MATH_ERRNO(0);31 32  EXPECT_FP_EQ(1.0f, LIBC_NAMESPACE::tanhf(inf));33  EXPECT_MATH_ERRNO(0);34 35  EXPECT_FP_EQ(-1.0f, LIBC_NAMESPACE::tanhf(neg_inf));36  EXPECT_MATH_ERRNO(0);37}38 39TEST_F(LlvmLibcTanhfTest, InFloatRange) {40  constexpr uint32_t COUNT = 100'001;41  constexpr uint32_t STEP = UINT32_MAX / COUNT;42  for (uint32_t i = 0, v = 0; i <= COUNT; ++i, v += STEP) {43    float x = FPBits(v).get_val();44    if (FPBits(v).is_nan() || FPBits(v).is_inf())45      continue;46    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh, x,47                                   LIBC_NAMESPACE::tanhf(x), 0.5);48  }49}50 51TEST_F(LlvmLibcTanhfTest, ExceptionalValues) {52  constexpr int N = 4;53  constexpr uint32_t INPUTS[N] = {54      0x0040'0000,55      0x1780'0000,56      0x3a12'85ff,57      0x4058'e0a3,58  };59 60  for (int i = 0; i < N; ++i) {61    float x = FPBits(INPUTS[i]).get_val();62    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh, x,63                                   LIBC_NAMESPACE::tanhf(x), 0.5);64    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tanh, -x,65                                   LIBC_NAMESPACE::tanhf(-x), 0.5);66  }67}68