brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.9 KiB · d11d708 Raw
75 lines · c
1//===-- Utility class to test rsqrt[f|l] ------------------------*- C++ -*-===//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#ifndef LLVM_LIBC_TEST_SRC_MATH_RSQRTTEST_H10#define LLVM_LIBC_TEST_SRC_MATH_RSQRTTEST_H11 12#include "test/UnitTest/FEnvSafeTest.h"13#include "test/UnitTest/FPMatcher.h"14#include "test/UnitTest/Test.h"15#include "utils/MPFRWrapper/MPFRUtils.h"16 17namespace mpfr = LIBC_NAMESPACE::testing::mpfr;18 19template <typename OutType, typename InType = OutType>20class RsqrtTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {21 22  DECLARE_SPECIAL_CONSTANTS(InType)23 24  static constexpr StorageType HIDDEN_BIT =25      StorageType(1) << LIBC_NAMESPACE::fputil::FPBits<InType>::FRACTION_LEN;26 27public:28  using RsqrtFunc = OutType (*)(InType);29 30  // Subnormal inputs: probe both power-of-two mantissas and an even sampling31  // across the subnormal range.32  void test_denormal_values(RsqrtFunc func) {33    // Powers of two in the subnormal mantissa space.34    for (StorageType mant = 1; mant < HIDDEN_BIT; mant <<= 1) {35      FPBits denormal(zero);36      denormal.set_mantissa(mant);37      InType x = denormal.get_val();38      ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Rsqrt, x, func(x), 0.5);39    }40 41    // Even sampling across all subnormals.42    constexpr StorageType COUNT = 200'001;43    constexpr StorageType STEP = HIDDEN_BIT / COUNT;44    for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {45      InType x = FPBits(i).get_val();46      ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Rsqrt, x, func(x), 0.5);47    }48  }49 50  // Positive normal range sampling: skip NaNs and negative values.51  void test_normal_range(RsqrtFunc func) {52    constexpr StorageType COUNT = 200'001;53    constexpr StorageType STEP = STORAGE_MAX / COUNT;54    for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {55      FPBits x_bits(v);56      InType x = x_bits.get_val();57      if (x_bits.is_nan() || x_bits.is_neg())58        continue;59      ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Rsqrt, x, func(x), 0.5);60    }61  }62};63 64#define LIST_RSQRT_TESTS(T, func)                                              \65  using LlvmLibcRsqrtTest = RsqrtTest<T, T>;                                   \66  TEST_F(LlvmLibcRsqrtTest, DenormalValues) { test_denormal_values(&func); }   \67  TEST_F(LlvmLibcRsqrtTest, NormalRange) { test_normal_range(&func); }68 69#define LIST_NARROWING_RSQRT_TESTS(OutType, InType, func)                      \70  using LlvmLibcRsqrtTest = RsqrtTest<OutType, InType>;                        \71  TEST_F(LlvmLibcRsqrtTest, DenormalValues) { test_denormal_values(&func); }   \72  TEST_F(LlvmLibcRsqrtTest, NormalRange) { test_normal_range(&func); }73 74#endif // LLVM_LIBC_TEST_SRC_MATH_RSQRTTEST_H75