brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.3 KiB · fe8ef4f Raw
137 lines · cpp
1//===-- Unittests for powf ------------------------------------------------===//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/powf.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 LlvmLibcPowfTest = LIBC_NAMESPACE::testing::FPTest<float>;25using LIBC_NAMESPACE::testing::tlog;26 27namespace mpfr = LIBC_NAMESPACE::testing::mpfr;28 29TEST_F(LlvmLibcPowfTest, TrickyInputs) {30  constexpr int N = 13;31  constexpr mpfr::BinaryInput<float> INPUTS[N] = {32      {0x1.290bbp-124f, 0x1.1e6d92p-25f},33      {0x1.2e9fb6p+5f, -0x1.1b82b6p-18f},34      {0x1.6877f6p+60f, -0x1.75f1c6p-4f},35      {0x1.0936acp-63f, -0x1.55200ep-15f},36      {0x1.d6d72ap+43f, -0x1.749ccap-5f},37      {0x1.4afb2ap-40f, 0x1.063198p+0f},38      {0x1.0124dep+0f, -0x1.fdb016p+9f},39      {0x1.1058p+0f, 0x1.ap+64f},40      {0x1.1058p+0f, -0x1.ap+64f},41      {0x1.1058p+0f, 0x1.ap+64f},42      {0x1.fa32d4p-1f, 0x1.67a62ep+12f},43      {-0x1.8p-49, 0x1.8p+1},44      {0x1.8p-48, 0x1.8p+1},45  };46 47  for (int i = 0; i < N; ++i) {48    float x = INPUTS[i].x;49    float y = INPUTS[i].y;50    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Pow, INPUTS[i],51                                   LIBC_NAMESPACE::powf(x, y), TOLERANCE + 0.5);52  }53}54 55TEST_F(LlvmLibcPowfTest, InFloatRange) {56  constexpr uint32_t X_COUNT = 1'23;57  constexpr uint32_t X_START = FPBits(0.25f).uintval();58  constexpr uint32_t X_STOP = FPBits(4.0f).uintval();59  constexpr uint32_t X_STEP = (X_STOP - X_START) / X_COUNT;60 61  constexpr uint32_t Y_COUNT = 1'37;62  constexpr uint32_t Y_START = FPBits(0.25f).uintval();63  constexpr uint32_t Y_STOP = FPBits(4.0f).uintval();64  constexpr uint32_t Y_STEP = (Y_STOP - Y_START) / Y_COUNT;65 66  auto test = [&](mpfr::RoundingMode rounding_mode) {67    mpfr::ForceRoundingMode __r(rounding_mode);68    if (!__r.success)69      return;70 71    uint64_t fails = 0;72    uint64_t count = 0;73    uint64_t cc = 0;74    float mx, my, mr = 0.0;75    double tol = 0.5;76 77    for (uint32_t i = 0, v = X_START; i <= X_COUNT; ++i, v += X_STEP) {78      float x = FPBits(v).get_val();79      if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)80        continue;81 82      for (uint32_t j = 0, w = Y_START; j <= Y_COUNT; ++j, w += Y_STEP) {83        float y = FPBits(w).get_val();84        if (FPBits(w).is_nan() || FPBits(w).is_inf())85          continue;86 87        libc_errno = 0;88        float result = LIBC_NAMESPACE::powf(x, y);89        ++cc;90        if (FPBits(result).is_nan() || FPBits(result).is_inf())91          continue;92 93        ++count;94        mpfr::BinaryInput<float> inputs{x, y};95 96        if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Pow, inputs,97                                               result, TOLERANCE + 0.5,98                                               rounding_mode)) {99          ++fails;100          while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(101              mpfr::Operation::Pow, inputs, result, tol, rounding_mode)) {102            mx = x;103            my = y;104            mr = result;105 106            if (tol > 1000.0)107              break;108 109            tol *= 2.0;110          }111        }112      }113    }114    if (fails || (count < cc)) {115      tlog << " Powf failed: " << fails << "/" << count << "/" << cc116           << " tests.\n"117           << "   Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";118    }119    if (fails) {120      mpfr::BinaryInput<float> inputs{mx, my};121      EXPECT_MPFR_MATCH(mpfr::Operation::Pow, inputs, mr, 0.5, rounding_mode);122    }123  };124 125  tlog << " Test Rounding To Nearest...\n";126  test(mpfr::RoundingMode::Nearest);127 128  tlog << " Test Rounding Downward...\n";129  test(mpfr::RoundingMode::Downward);130 131  tlog << " Test Rounding Upward...\n";132  test(mpfr::RoundingMode::Upward);133 134  tlog << " Test Rounding Toward Zero...\n";135  test(mpfr::RoundingMode::TowardZero);136}137