124 lines · cpp
1//===-- Unittests for 2^x -------------------------------------------------===//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/exp2.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 LlvmLibcExp2Test = LIBC_NAMESPACE::testing::FPTest<double>;19 20namespace mpfr = LIBC_NAMESPACE::testing::mpfr;21using LIBC_NAMESPACE::testing::tlog;22 23TEST_F(LlvmLibcExp2Test, SpecialNumbers) {24 EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::exp2(aNaN));25 EXPECT_FP_EQ(inf, LIBC_NAMESPACE::exp2(inf));26 EXPECT_FP_EQ_ALL_ROUNDING(zero, LIBC_NAMESPACE::exp2(neg_inf));27 EXPECT_FP_EQ_WITH_EXCEPTION(zero, LIBC_NAMESPACE::exp2(-0x1.0p20),28 FE_UNDERFLOW);29 EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::exp2(0x1.0p20), FE_OVERFLOW);30 EXPECT_FP_EQ_ALL_ROUNDING(1.0, LIBC_NAMESPACE::exp2(0.0));31 EXPECT_FP_EQ_ALL_ROUNDING(1.0, LIBC_NAMESPACE::exp2(-0.0));32}33 34TEST_F(LlvmLibcExp2Test, TrickyInputs) {35 constexpr int N = 16;36 constexpr uint64_t INPUTS[N] = {37 0x3FD79289C6E6A5C0,38 0x3FD05DE80A173EA0, // 0x1.05de80a173eap-239 0xbf1eb7a4cb841fcc, // -0x1.eb7a4cb841fccp-1440 0xbf19a61fb925970d,41 0x3fda7b764e2cf47a, // 0x1.a7b764e2cf47ap-242 0xc04757852a4b93aa, // -0x1.757852a4b93aap+543 0x4044c19e5712e377, // x=0x1.4c19e5712e377p+544 0xbf19a61fb925970d, // x=-0x1.9a61fb925970dp-1445 0xc039a74cdab36c28, // x=-0x1.9a74cdab36c28p+446 0xc085b3e4e2e3bba9, // x=-0x1.5b3e4e2e3bba9p+947 0xc086960d591aec34, // x=-0x1.6960d591aec34p+948 0xc086232c09d58d91, // x=-0x1.6232c09d58d91p+949 0xc0874910d52d3051, // x=-0x1.74910d52d3051p950 0xc0867a172ceb0990, // x=-0x1.67a172ceb099p+951 0xc08ff80000000000, // x=-0x1.ff8p+952 0xbc971547652b82fe, // x=-0x1.71547652b82fep-5453 };54 for (int i = 0; i < N; ++i) {55 double x = FPBits(INPUTS[i]).get_val();56 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Exp2, x,57 LIBC_NAMESPACE::exp2(x), 0.5);58 }59}60 61TEST_F(LlvmLibcExp2Test, InDoubleRange) {62 constexpr uint64_t COUNT = 1'231;63 uint64_t START = LIBC_NAMESPACE::fputil::FPBits<double>(0.25).uintval();64 uint64_t STOP = LIBC_NAMESPACE::fputil::FPBits<double>(4.0).uintval();65 uint64_t STEP = (STOP - START) / COUNT;66 67 auto test = [&](mpfr::RoundingMode rounding_mode) {68 mpfr::ForceRoundingMode __r(rounding_mode);69 if (!__r.success)70 return;71 72 uint64_t fails = 0;73 uint64_t count = 0;74 uint64_t cc = 0;75 double mx, mr = 0.0;76 double tol = 0.5;77 78 for (uint64_t i = 0, v = START; i <= COUNT; ++i, v += STEP) {79 double x = FPBits(v).get_val();80 if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)81 continue;82 double result = LIBC_NAMESPACE::exp2(x);83 ++cc;84 if (FPBits(result).is_nan() || FPBits(result).is_inf())85 continue;86 87 ++count;88 89 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Exp2, x, result,90 0.5, rounding_mode)) {91 ++fails;92 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Exp2, x,93 result, tol, rounding_mode)) {94 mx = x;95 mr = result;96 97 if (tol > 1000.0)98 break;99 100 tol *= 2.0;101 }102 }103 }104 if (fails) {105 tlog << " Exp2 failed: " << fails << "/" << count << "/" << cc106 << " tests.\n";107 tlog << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";108 EXPECT_MPFR_MATCH(mpfr::Operation::Exp2, mx, mr, 0.5, rounding_mode);109 }110 };111 112 tlog << " Test Rounding To Nearest...\n";113 test(mpfr::RoundingMode::Nearest);114 115 tlog << " Test Rounding Downward...\n";116 test(mpfr::RoundingMode::Downward);117 118 tlog << " Test Rounding Upward...\n";119 test(mpfr::RoundingMode::Upward);120 121 tlog << " Test Rounding Toward Zero...\n";122 test(mpfr::RoundingMode::TowardZero);123}124