brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.2 KiB · 56b6967 Raw
145 lines · cpp
1//===-- Unittests for atan2f ----------------------------------------------===//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/__support/macros/optimization.h"12#include "src/math/atan2f.h"13#include "test/UnitTest/FPMatcher.h"14#include "test/UnitTest/Test.h"15#include "utils/MPFRWrapper/MPFRUtils.h"16 17#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS18#define TOLERANCE 119#else20#define TOLERANCE 021#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS22 23using LlvmLibcAtan2fTest = LIBC_NAMESPACE::testing::FPTest<float>;24using LIBC_NAMESPACE::testing::tlog;25 26namespace mpfr = LIBC_NAMESPACE::testing::mpfr;27 28TEST_F(LlvmLibcAtan2fTest, TrickyInputs) {29  constexpr int N = 17;30  mpfr::BinaryInput<float> INPUTS[N] = {31      {0x1.0cb3a4p+20f, 0x1.4ebacp+22f},   {0x1.12215p+1f, 0x1.4fabfcp+22f},32      {-0x1.13baaep+41f, 0x1.5bd22ep+23f}, {0x1.1ff7dcp+41f, 0x1.aec0a6p+23f},33      {0x1.2bc794p+23f, 0x1.0bc0c6p+23f},  {0x1.2fba3ap+42f, 0x1.f99456p+23f},34      {0x1.5ea1f8p+27f, 0x1.f2a1aep+23f},  {0x1.7a931p+44f, 0x1.352ac4p+22f},35      {0x1.8802bcp+21f, 0x1.8f130ap+23f},  {0x1.658ef8p+17f, 0x1.3c00f4p+22f},36      {0x1.69fb0cp+21f, 0x1.39e4c4p+23f},  {0x1.8eb24cp+11f, 0x1.36518p+23f},37      {0x1.9e7ebp+30f, 0x1.d80522p+23f},   {0x1.b4bdeep+19f, 0x1.c19b4p+23f},38      {0x1.bc201p+43f, 0x1.617346p+23f},   {0x1.c96c3cp+20f, 0x1.c01d1ep+23f},39      {0x1.781fcp+28f, 0x1.dcb3cap+23f},40  };41 42  for (int i = 0; i < N; ++i) {43    float x = INPUTS[i].x;44    float y = INPUTS[i].y;45    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, INPUTS[i],46                                   LIBC_NAMESPACE::atan2f(x, y),47                                   TOLERANCE + 0.5);48    INPUTS[i].x = -INPUTS[i].x;49    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, INPUTS[i],50                                   LIBC_NAMESPACE::atan2f(-x, y),51                                   TOLERANCE + 0.5);52    INPUTS[i].y = -INPUTS[i].y;53    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, INPUTS[i],54                                   LIBC_NAMESPACE::atan2f(-x, -y),55                                   TOLERANCE + 0.5);56    INPUTS[i].x = -INPUTS[i].x;57    ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Atan2, INPUTS[i],58                                   LIBC_NAMESPACE::atan2f(x, -y),59                                   TOLERANCE + 0.5);60  }61}62 63TEST_F(LlvmLibcAtan2fTest, InFloatRange) {64  constexpr uint32_t X_COUNT = 1'23;65  constexpr uint32_t X_START = FPBits(0.25f).uintval();66  constexpr uint32_t X_STOP = FPBits(4.0f).uintval();67  constexpr uint32_t X_STEP = (X_STOP - X_START) / X_COUNT;68 69  constexpr uint32_t Y_COUNT = 1'37;70  constexpr uint32_t Y_START = FPBits(0.25f).uintval();71  constexpr uint32_t Y_STOP = FPBits(4.0f).uintval();72  constexpr uint32_t Y_STEP = (Y_STOP - Y_START) / Y_COUNT;73 74  auto test = [&](mpfr::RoundingMode rounding_mode) {75    mpfr::ForceRoundingMode __r(rounding_mode);76    if (!__r.success)77      return;78 79    uint64_t fails = 0;80    uint64_t finite_count = 0;81    uint64_t total_count = 0;82    float failed_x, failed_y, failed_r = 0.0;83    double tol = 0.5;84 85    for (uint32_t i = 0, v = X_START; i <= X_COUNT; ++i, v += X_STEP) {86      float x = FPBits(v).get_val();87      if (FPBits(v).is_nan() || FPBits(v).is_inf() || x < 0.0)88        continue;89 90      for (uint32_t j = 0, w = Y_START; j <= Y_COUNT; ++j, w += Y_STEP) {91        float y = FPBits(w).get_val();92        if (FPBits(w).is_nan() || FPBits(w).is_inf())93          continue;94 95        libc_errno = 0;96        float result = LIBC_NAMESPACE::atan2f(x, y);97        ++total_count;98        if (FPBits(result).is_nan() || FPBits(result).is_inf())99          continue;100 101        ++finite_count;102        mpfr::BinaryInput<float> inputs{x, y};103 104        if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Atan2, inputs,105                                               result, 0.5, rounding_mode)) {106          ++fails;107          while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(108              mpfr::Operation::Atan2, inputs, result, tol, rounding_mode)) {109            failed_x = x;110            failed_y = y;111            failed_r = result;112 113            if (tol > 1000.0)114              break;115 116            tol *= 2.0;117          }118        }119      }120    }121    if (fails || (finite_count < total_count)) {122      tlog << " Atan2f failed: " << fails << "/" << finite_count << "/"123           << total_count << " tests.\n"124           << "   Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";125    }126    if (fails) {127      mpfr::BinaryInput<float> inputs{failed_x, failed_y};128      EXPECT_MPFR_MATCH(mpfr::Operation::Atan2, inputs, failed_r, 0.5,129                        rounding_mode);130    }131  };132 133  tlog << " Test Rounding To Nearest...\n";134  test(mpfr::RoundingMode::Nearest);135 136  tlog << " Test Rounding Downward...\n";137  test(mpfr::RoundingMode::Downward);138 139  tlog << " Test Rounding Upward...\n";140  test(mpfr::RoundingMode::Upward);141 142  tlog << " Test Rounding Toward Zero...\n";143  test(mpfr::RoundingMode::TowardZero);144}145