114 lines · cpp
1//===-- Unittests for e^x - 1 ---------------------------------------------===//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 "hdr/stdint_proxy.h"11#include "src/__support/FPUtil/FPBits.h"12#include "src/__support/macros/optimization.h"13#include "src/math/expm1.h"14#include "test/UnitTest/FPMatcher.h"15#include "test/UnitTest/Test.h"16#include "utils/MPFRWrapper/MPFRUtils.h"17 18#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS19#define TOLERANCE 120#else21#define TOLERANCE 022#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS23 24using LlvmLibcExpm1Test = LIBC_NAMESPACE::testing::FPTest<double>;25 26namespace mpfr = LIBC_NAMESPACE::testing::mpfr;27using LIBC_NAMESPACE::testing::tlog;28 29TEST_F(LlvmLibcExpm1Test, TrickyInputs) {30 constexpr double INPUTS[] = {31 0x1.71547652b82fep-54, 0x1.465655f122ff6p-49, 0x1.bc8ee6b28659ap-46,32 0x1.8442b169f672dp-14, 0x1.9a61fb925970dp-14, 0x1.eb7a4cb841fccp-14,33 0x1.05de80a173eap-2, 0x1.79289c6e6a5cp-2, 0x1.a7b764e2cf47ap-2,34 0x1.b4f0cfb15ca0fp+3, 0x1.9a74cdab36c28p+4, 0x1.2b708872320ddp+5,35 0x1.4c19e5712e377p+5, 0x1.757852a4b93aap+5, 0x1.77f74111e0894p+6,36 0x1.a6c3780bbf824p+6, 0x1.e3d57e4c557f6p+6, 0x1.f07560077985ap+6,37 0x1.1f0da93354198p+7, 0x1.71018579c0758p+7, 0x1.204684c1167e9p+8,38 0x1.5b3e4e2e3bba9p+9, 0x1.6232c09d58d91p+9, 0x1.67a172ceb099p+9,39 0x1.6960d591aec34p+9, 0x1.74910d52d3051p+9, 0x1.ff8p+9,40 };41 constexpr int N = sizeof(INPUTS) / sizeof(INPUTS[0]);42 for (int i = 0; i < N; ++i) {43 double x = INPUTS[i];44 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, x,45 LIBC_NAMESPACE::expm1(x), TOLERANCE + 0.5);46 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Expm1, -x,47 LIBC_NAMESPACE::expm1(-x), TOLERANCE + 0.5);48 }49}50 51TEST_F(LlvmLibcExpm1Test, InDoubleRange) {52 constexpr uint64_t COUNT = 1'231;53 uint64_t START = LIBC_NAMESPACE::fputil::FPBits<double>(0.25).uintval();54 uint64_t STOP = LIBC_NAMESPACE::fputil::FPBits<double>(4.0).uintval();55 uint64_t STEP = (STOP - START) / COUNT;56 57 auto test = [&](mpfr::RoundingMode rounding_mode) {58 mpfr::ForceRoundingMode __r(rounding_mode);59 if (!__r.success)60 return;61 62 uint64_t fails = 0;63 uint64_t count = 0;64 uint64_t cc = 0;65 double mx, mr = 0.0;66 double tol = 0.5;67 68 for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {69 double x = FPBits(v).get_val();70 if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)71 continue;72 double result = LIBC_NAMESPACE::expm1(x);73 ++cc;74 if (FPBits(result).is_nan() || FPBits(result).is_inf())75 continue;76 77 ++count;78 79 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Expm1, x, result,80 0.5, rounding_mode)) {81 ++fails;82 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Expm1, x,83 result, tol, rounding_mode)) {84 mx = x;85 mr = result;86 87 if (tol > 1000.0)88 break;89 90 tol *= 2.0;91 }92 }93 }94 if (fails) {95 tlog << " Expm1 failed: " << fails << "/" << count << "/" << cc96 << " tests.\n";97 tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";98 EXPECT_MPFR_MATCH(mpfr::Operation::Expm1, mx, mr, 0.5, rounding_mode);99 }100 };101 102 tlog << " Test Rounding To Nearest...\n";103 test(mpfr::RoundingMode::Nearest);104 105 tlog << " Test Rounding Downward...\n";106 test(mpfr::RoundingMode::Downward);107 108 tlog << " Test Rounding Upward...\n";109 test(mpfr::RoundingMode::Upward);110 111 tlog << " Test Rounding Toward Zero...\n";112 test(mpfr::RoundingMode::TowardZero);113}114