102 lines · cpp
1//===-- Unittests for atan2f128 -------------------------------------------===//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/math/atan2f128.h"10#include "test/UnitTest/FPMatcher.h"11#include "test/UnitTest/Test.h"12#include "utils/MPFRWrapper/MPFRUtils.h"13 14using LlvmLibcAtan2f128Test = LIBC_NAMESPACE::testing::FPTest<float128>;15using LIBC_NAMESPACE::testing::tlog;16 17namespace mpfr = LIBC_NAMESPACE::testing::mpfr;18 19TEST_F(LlvmLibcAtan2f128Test, InQuadRange) {20 constexpr StorageType X_COUNT = 123;21 constexpr StorageType X_START =22 FPBits(static_cast<float128>(0.25q)).uintval();23 constexpr StorageType X_STOP = FPBits(static_cast<float128>(4.0q)).uintval();24 constexpr StorageType X_STEP = (X_STOP - X_START) / X_COUNT;25 26 constexpr StorageType Y_COUNT = 137;27 constexpr StorageType Y_START =28 FPBits(static_cast<float128>(0.25q)).uintval();29 constexpr StorageType Y_STOP = FPBits(static_cast<float128>(4.0q)).uintval();30 constexpr StorageType Y_STEP = (Y_STOP - Y_START) / Y_COUNT;31 32 auto test = [&](mpfr::RoundingMode rounding_mode) {33 mpfr::ForceRoundingMode __r(rounding_mode);34 if (!__r.success)35 return;36 37 uint64_t fails = 0;38 uint64_t finite_count = 0;39 uint64_t total_count = 0;40 float128 failed_x = 0.0, failed_y = 0.0, failed_r = 0.0;41 double tol = 0.5;42 43 for (StorageType i = 0, v = X_START; i <= X_COUNT; ++i, v += X_STEP) {44 float128 x = FPBits(v).get_val();45 if (FPBits(x).is_inf_or_nan() || x < 0.0q)46 continue;47 48 for (StorageType j = 0, w = Y_START; j <= Y_COUNT; ++j, w += Y_STEP) {49 float128 y = FPBits(w).get_val();50 if (FPBits(y).is_inf_or_nan())51 continue;52 53 float128 result = LIBC_NAMESPACE::atan2f128(x, y);54 ++total_count;55 if (FPBits(result).is_inf_or_nan())56 continue;57 58 ++finite_count;59 mpfr::BinaryInput<float128> inputs{x, y};60 61 if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Atan2, inputs,62 result, 2.0, rounding_mode)) {63 ++fails;64 while (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(65 mpfr::Operation::Atan2, inputs, result, tol, rounding_mode)) {66 failed_x = x;67 failed_y = y;68 failed_r = result;69 70 if (tol > 1000.0)71 break;72 73 tol *= 2.0;74 }75 }76 }77 }78 if (fails || (finite_count < total_count)) {79 tlog << " Atan2 failed: " << fails << "/" << finite_count << "/"80 << total_count << " tests.\n"81 << " Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";82 }83 if (fails) {84 mpfr::BinaryInput<float128> inputs{failed_x, failed_y};85 EXPECT_MPFR_MATCH(mpfr::Operation::Atan2, inputs, failed_r, 0.5,86 rounding_mode);87 }88 };89 90 tlog << " Test Rounding To Nearest...\n";91 test(mpfr::RoundingMode::Nearest);92 93 tlog << " Test Rounding Downward...\n";94 test(mpfr::RoundingMode::Downward);95 96 tlog << " Test Rounding Upward...\n";97 test(mpfr::RoundingMode::Upward);98 99 tlog << " Test Rounding Toward Zero...\n";100 test(mpfr::RoundingMode::TowardZero);101}102