|
| 1 | +//===-- Utility class to test different flavors of nearbyint ----*- 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-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LLVM_LIBC_TEST_SRC_MATH_NEARBYINTTEST_H |
| 10 | +#define LLVM_LIBC_TEST_SRC_MATH_NEARBYINTTEST_H |
| 11 | + |
| 12 | +#include "src/__support/CPP/algorithm.h" |
| 13 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 14 | +#include "test/UnitTest/FEnvSafeTest.h" |
| 15 | +#include "test/UnitTest/FPMatcher.h" |
| 16 | +#include "test/UnitTest/Test.h" |
| 17 | +#include "utils/MPFRWrapper/MPFRUtils.h" |
| 18 | + |
| 19 | +#include "hdr/fenv_macros.h" |
| 20 | + |
| 21 | +namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
| 22 | + |
| 23 | +template <typename T> |
| 24 | +class NearbyIntTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
| 25 | + |
| 26 | + DECLARE_SPECIAL_CONSTANTS(T) |
| 27 | + |
| 28 | + static constexpr int ROUNDING_MODES[4] = {FE_UPWARD, FE_DOWNWARD, |
| 29 | + FE_TOWARDZERO, FE_TONEAREST}; |
| 30 | + |
| 31 | + static constexpr StorageType MIN_SUBNORMAL = |
| 32 | + FPBits::min_subnormal().uintval(); |
| 33 | + static constexpr StorageType MAX_SUBNORMAL = |
| 34 | + FPBits::max_subnormal().uintval(); |
| 35 | + static constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval(); |
| 36 | + static constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval(); |
| 37 | + |
| 38 | + static mpfr::RoundingMode to_mpfr_rounding_mode(int mode) { |
| 39 | + switch (mode) { |
| 40 | + case FE_UPWARD: |
| 41 | + return mpfr::RoundingMode::Upward; |
| 42 | + case FE_DOWNWARD: |
| 43 | + return mpfr::RoundingMode::Downward; |
| 44 | + case FE_TOWARDZERO: |
| 45 | + return mpfr::RoundingMode::TowardZero; |
| 46 | + case FE_TONEAREST: |
| 47 | + return mpfr::RoundingMode::Nearest; |
| 48 | + default: |
| 49 | + __builtin_unreachable(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | +public: |
| 54 | + using NearbyIntFunc = T (*)(T); |
| 55 | + |
| 56 | + void test_round_numbers(NearbyIntFunc func) { |
| 57 | + for (int mode : ROUNDING_MODES) { |
| 58 | + LIBC_NAMESPACE::fputil::set_round(mode); |
| 59 | + mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode); |
| 60 | + EXPECT_FP_EQ(func(T(1.0)), mpfr::round(T(1.0), mpfr_mode)); |
| 61 | + EXPECT_FP_EQ(func(T(-1.0)), mpfr::round(T(-1.0), mpfr_mode)); |
| 62 | + EXPECT_FP_EQ(func(T(10.0)), mpfr::round(T(10.0), mpfr_mode)); |
| 63 | + EXPECT_FP_EQ(func(T(-10.0)), mpfr::round(T(-10.0), mpfr_mode)); |
| 64 | + EXPECT_FP_EQ(func(T(1234.0)), mpfr::round(T(1234.0), mpfr_mode)); |
| 65 | + EXPECT_FP_EQ(func(T(-1234.0)), mpfr::round(T(-1234.0), mpfr_mode)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + void test_fractions(NearbyIntFunc func) { |
| 70 | + for (int mode : ROUNDING_MODES) { |
| 71 | + LIBC_NAMESPACE::fputil::set_round(mode); |
| 72 | + mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode); |
| 73 | + EXPECT_FP_EQ(func(T(0.5)), mpfr::round(T(0.5), mpfr_mode)); |
| 74 | + EXPECT_FP_EQ(func(T(-0.5)), mpfr::round(T(-0.5), mpfr_mode)); |
| 75 | + EXPECT_FP_EQ(func(T(0.115)), mpfr::round(T(0.115), mpfr_mode)); |
| 76 | + EXPECT_FP_EQ(func(T(-0.115)), mpfr::round(T(-0.115), mpfr_mode)); |
| 77 | + EXPECT_FP_EQ(func(T(0.715)), mpfr::round(T(0.715), mpfr_mode)); |
| 78 | + EXPECT_FP_EQ(func(T(-0.715)), mpfr::round(T(-0.715), mpfr_mode)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + void test_subnormal_range(NearbyIntFunc func) { |
| 83 | + constexpr int COUNT = 100'001; |
| 84 | + const StorageType STEP = LIBC_NAMESPACE::cpp::max( |
| 85 | + static_cast<StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT), |
| 86 | + StorageType(1)); |
| 87 | + for (StorageType i = MIN_SUBNORMAL; i <= MAX_SUBNORMAL; i += STEP) { |
| 88 | + T x = FPBits(i).get_val(); |
| 89 | + for (int mode : ROUNDING_MODES) { |
| 90 | + LIBC_NAMESPACE::fputil::set_round(mode); |
| 91 | + mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode); |
| 92 | + EXPECT_FP_EQ(func(x), mpfr::round(x, mpfr_mode)); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + void test_normal_range(NearbyIntFunc func) { |
| 98 | + constexpr int COUNT = 100'001; |
| 99 | + const StorageType STEP = LIBC_NAMESPACE::cpp::max( |
| 100 | + static_cast<StorageType>((MAX_NORMAL - MIN_NORMAL) / COUNT), |
| 101 | + StorageType(1)); |
| 102 | + for (StorageType i = MIN_NORMAL; i <= MAX_NORMAL; i += STEP) { |
| 103 | + FPBits xbits(i); |
| 104 | + T x = xbits.get_val(); |
| 105 | + // In normal range on x86 platforms, the long double implicit 1 bit can be |
| 106 | + // zero making the numbers NaN. We will skip them. |
| 107 | + if (xbits.is_nan()) |
| 108 | + continue; |
| 109 | + |
| 110 | + for (int mode : ROUNDING_MODES) { |
| 111 | + LIBC_NAMESPACE::fputil::set_round(mode); |
| 112 | + mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode); |
| 113 | + EXPECT_FP_EQ(func(x), mpfr::round(x, mpfr_mode)); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +#define LIST_NEARBYINT_TESTS(F, func) \ |
| 120 | + using LlvmLibcNearbyIntTest = NearbyIntTestTemplate<F>; \ |
| 121 | + TEST_F(LlvmLibcNearbyIntTest, RoundNumbers) { test_round_numbers(&func); } \ |
| 122 | + TEST_F(LlvmLibcNearbyIntTest, Fractions) { test_fractions(&func); } \ |
| 123 | + TEST_F(LlvmLibcNearbyIntTest, SubnormalRange) { \ |
| 124 | + test_subnormal_range(&func); \ |
| 125 | + } \ |
| 126 | + TEST_F(LlvmLibcNearbyIntTest, NormalRange) { test_normal_range(&func); } |
| 127 | + |
| 128 | +#endif // LLVM_LIBC_TEST_SRC_MATH_NEARBYINTTEST_H |
0 commit comments