brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.3 KiB · 62f8994 Raw
134 lines · cpp
1//===-- Unittests for tan -------------------------------------------------===//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 "src/__support/FPUtil/FPBits.h"10#include "src/__support/macros/optimization.h"11#include "src/math/tan.h"12#include "test/UnitTest/FPMatcher.h"13#include "test/UnitTest/Test.h"14#include "utils/MPFRWrapper/MPFRUtils.h"15 16#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS17#define TOLERANCE 418#else19#define TOLERANCE 020#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS21 22using LlvmLibcTanTest = LIBC_NAMESPACE::testing::FPTest<double>;23 24namespace mpfr = LIBC_NAMESPACE::testing::mpfr;25 26using LIBC_NAMESPACE::testing::tlog;27 28TEST_F(LlvmLibcTanTest, TrickyInputs) {29  constexpr double INPUTS[] = {30      0x0.0000000000001p-1022, 0x1.d130383d17321p-27,  0x1.8000000000009p-23,31      0x1.8000000000024p-22,   0x1.800000000009p-21,   0x1.20000000000f3p-20,32      0x1.800000000024p-20,    0x1.e0000000001c2p-20,  0x1.00452f0e0134dp-13,33      0x1.0da8cc189b47dp-10,   0x1.00a33764a0a83p-7,   0x1.911a18779813fp-7,34      0x1.940c877fb7dacp-7,    0x1.f42fb19b5b9b2p-6,   0x1.0285070f9f1bcp-5,35      0x1.90e833c6969c7p-4,    0x1.91d4b77c527eap-3,   0x1.89f0f5241255bp-2,36      0x1.6ca9ef729af76p-1,    0x1.23f40dccdef72p+0,   0x1.43cf16358c9d7p+0,37      0x1.90f422b49115ep+0,    0x1.9220efee9fc7ep+0,   0x1.a224411cdebcep+0,38      0x1.addf3b9722265p+0,    0x1.ae78d360afa15p+0,   0x1.fe81868fc47fep+1,39      0x1.e31b55306f22cp+2,    0x1.e639103a05997p+2,   0x1.f69d074a3358fp+2,40      0x1.f7898d5a756ddp+2,    0x1.1685973506319p+3,   0x1.5f09cad750ab1p+3,41      0x1.aaf85537ea4c7p+3,    0x1.c50ddc4f513b4p+3,   0x1.13114266f9764p+4,42      0x1.4f2b874135d27p+4,    0x1.a211877de55dbp+4,   0x1.a5eece87e8606p+4,43      0x1.a65d441ea6dcep+4,    0x1.ab8c2f8ab5b7p+4,    0x1.045457ae3994p+5,44      0x1.1ffb509f3db15p+5,    0x1.2345d1e090529p+5,   0x1.c96e28eb679f8p+5,45      0x1.da1838053b866p+5,    0x1.be886d9c2324dp+6,   0x1.ab514bfc61c76p+7,46      0x1.14823229799c2p+7,    0x1.48ff1782ca91dp+8,   0x1.dcbfda0c7559ep+8,47      0x1.dcbfda0c7559ep+8,    0x1.2e566149bf5fdp+9,   0x1.cb996c60f437ep+9,48      0x1.119471e9216cdp+10,   0x1.ae945054939c2p+10,  0x1.fffffffffdb6p+24,49      0x1.fd4da4ef37075p+29,   0x1.55202aefde314p+31,  0x1.b951f1572eba5p+31,50      0x1.76e86a7485a46p59,    0x1.7776c2343ba4ep+101, 0x1.85fc0f04c0128p+101,51      0x1.678309fa50d58p+110,  0x1.524489232dc4ap+178, 0x1.fffffffffef4ep+199,52      0x1.6deb37da81129p+205,  0x1.3eec5912ea7cdp+331, 0x1.08087e9aad90bp+887,53      0x1.6ac5b262ca1ffp+843,  0x1.8bb5847d49973p+845, 0x1.6ac5b262ca1ffp+849,54      0x1.f08b14e1c4d0fp+890,  0x1.2b5fe88a9d8d5p+903, 0x1.a880417b7b119p+1023,55      0x1.f6d7518808571p+1023,56 57  };58  constexpr int N = sizeof(INPUTS) / sizeof(INPUTS[0]);59 60  for (int i = 0; i < N; ++i) {61    double x = INPUTS[i];62    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tan, x,63                                   LIBC_NAMESPACE::tan(x), TOLERANCE + 0.5);64    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Tan, -x,65                                   LIBC_NAMESPACE::tan(-x), TOLERANCE + 0.5);66  }67}68 69TEST_F(LlvmLibcTanTest, InDoubleRange) {70  constexpr uint64_t COUNT = 1'234'51;71  uint64_t START = LIBC_NAMESPACE::fputil::FPBits<double>(0x1.0p-50).uintval();72  uint64_t STOP = LIBC_NAMESPACE::fputil::FPBits<double>(0x1.0p200).uintval();73  uint64_t STEP = (STOP - START) / COUNT;74 75  auto test = [&](mpfr::RoundingMode rounding_mode) {76    mpfr::ForceRoundingMode force_rounding(rounding_mode);77    if (!force_rounding.success)78      return;79 80    uint64_t fails = 0;81    uint64_t tested = 0;82    uint64_t total = 0;83    double worst_input, worst_output = 0.0;84    double ulp = 0.5;85 86    for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {87      double x = FPBits(v).get_val();88      if (FPBits(v).is_nan() || FPBits(v).is_inf())89        continue;90 91      double result = LIBC_NAMESPACE::tan(x);92      ++total;93      if (FPBits(result).is_nan() || FPBits(result).is_inf())94        continue;95 96      ++tested;97 98      if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Tan, x, result,99                                             TOLERANCE + 0.5, rounding_mode)) {100        ++fails;101        while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Tan, x,102                                                  result, ulp, rounding_mode)) {103          worst_input = x;104          worst_output = result;105 106          if (ulp > 1000.0)107            break;108 109          ulp *= 2.0;110        }111      }112    }113    if (fails) {114      tlog << " Tan failed: " << fails << "/" << tested << "/" << total115           << " tests.\n";116      tlog << "   Max ULPs is at most: " << static_cast<uint64_t>(ulp) << ".\n";117      EXPECT_MPFR_MATCH(mpfr::Operation::Tan, worst_input, worst_output, 0.5,118                        rounding_mode);119    }120  };121 122  tlog << " Test Rounding To Nearest...\n";123  test(mpfr::RoundingMode::Nearest);124 125  tlog << " Test Rounding Downward...\n";126  test(mpfr::RoundingMode::Downward);127 128  tlog << " Test Rounding Upward...\n";129  test(mpfr::RoundingMode::Upward);130 131  tlog << " Test Rounding Toward Zero...\n";132  test(mpfr::RoundingMode::TowardZero);133}134