|
| 1 | +//===----------------------------------------------------------------------===// |
| 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 | +/// \file |
| 10 | +/// Unittests for LIBC_DO_NOT_OPTIMIZE and double-double utilities. |
| 11 | +/// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "src/__support/FPUtil/double_double.h" |
| 15 | +#include "src/__support/macros/config.h" |
| 16 | +#include "src/__support/macros/optimization.h" |
| 17 | +#include "test/UnitTest/FEnvSafeTest.h" |
| 18 | +#include "test/UnitTest/FPMatcher.h" |
| 19 | +#include "test/UnitTest/RoundingModeUtils.h" |
| 20 | +#include "test/UnitTest/Test.h" |
| 21 | + |
| 22 | +using LIBC_NAMESPACE::fputil::DoubleDouble; |
| 23 | +using LIBC_NAMESPACE::fputil::testing::ForceRoundingMode; |
| 24 | +using LIBC_NAMESPACE::fputil::testing::RoundingMode; |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +#if LIBC_HAS_CONSTANT_EVALUATION |
| 29 | +constexpr float constexpr_barrier_check_f(float x) { |
| 30 | + LIBC_DO_NOT_OPTIMIZE(x); |
| 31 | + LIBC_DO_NOT_OPTIMIZE_ROUNDING(x); |
| 32 | + return x + 1.0f; |
| 33 | +} |
| 34 | +constexpr double constexpr_barrier_check_d(double x) { |
| 35 | + LIBC_DO_NOT_OPTIMIZE(x); |
| 36 | + LIBC_DO_NOT_OPTIMIZE_ROUNDING(x); |
| 37 | + return x + 1.0; |
| 38 | +} |
| 39 | +static_assert(constexpr_barrier_check_f(2.5f) == 3.5f); |
| 40 | +static_assert(constexpr_barrier_check_d(2.5) == 3.5); |
| 41 | +#endif |
| 42 | + |
| 43 | +} // namespace |
| 44 | + |
| 45 | +class LlvmLibcDoubleDoubleTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { |
| 46 | +}; |
| 47 | + |
| 48 | +TEST_F(LlvmLibcDoubleDoubleTest, DoNotOptimizeFloatAndDouble) { |
| 49 | + float f = 1.5f; |
| 50 | + LIBC_DO_NOT_OPTIMIZE(f); |
| 51 | + LIBC_DO_NOT_OPTIMIZE_ROUNDING(f); |
| 52 | + EXPECT_FP_EQ(f, 1.5f); |
| 53 | + |
| 54 | + double d = -2.25; |
| 55 | + LIBC_DO_NOT_OPTIMIZE(d); |
| 56 | + LIBC_DO_NOT_OPTIMIZE_ROUNDING(d); |
| 57 | + EXPECT_FP_EQ(d, -2.25); |
| 58 | +} |
| 59 | + |
| 60 | +#if !defined(LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY) && \ |
| 61 | + !defined(LIBC_COMPILER_IS_MSVC) |
| 62 | +TEST_F(LlvmLibcDoubleDoubleTest, DoNotOptimizeBlocksConstantFolding) { |
| 63 | + { |
| 64 | + ForceRoundingMode r(RoundingMode::Upward); |
| 65 | + if (r.success) { |
| 66 | + float one_f = 1.0f; |
| 67 | + LIBC_DO_NOT_OPTIMIZE(one_f); |
| 68 | + EXPECT_FP_EQ(one_f + 0x1.0p-50f, 0x1.000002p0f); |
| 69 | + |
| 70 | + double one_d = 1.0; |
| 71 | + LIBC_DO_NOT_OPTIMIZE_ROUNDING(one_d); |
| 72 | + EXPECT_FP_EQ(one_d + 0x1.0p-100, 0x1.0000000000001p0); |
| 73 | + } |
| 74 | + } |
| 75 | + { |
| 76 | + ForceRoundingMode r(RoundingMode::Downward); |
| 77 | + if (r.success) { |
| 78 | + float neg_one_f = -1.0f; |
| 79 | + LIBC_DO_NOT_OPTIMIZE(neg_one_f); |
| 80 | + EXPECT_FP_EQ(neg_one_f - 0x1.0p-50f, -0x1.000002p0f); |
| 81 | + |
| 82 | + double neg_one_d = -1.0; |
| 83 | + LIBC_DO_NOT_OPTIMIZE_ROUNDING(neg_one_d); |
| 84 | + EXPECT_FP_EQ(neg_one_d - 0x1.0p-100, -0x1.0000000000001p0); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +#endif |
| 89 | + |
| 90 | +TEST_F(LlvmLibcDoubleDoubleTest, ExactMultDirectedRounding) { |
| 91 | + if constexpr (LIBC_NAMESPACE::fputil::TargetHasFmaInstruction< |
| 92 | + double>::VALUE) { |
| 93 | + // Exact product: (1 + 2^-52)^2 = (1 + 2^-51) + 2^-104 |
| 94 | + // = (1 + 3*2^-52) + (-2^-52 + 2^-104). |
| 95 | + auto check_square = [](RoundingMode mode, double expected_hi, |
| 96 | + double expected_lo) { |
| 97 | + ForceRoundingMode r(mode); |
| 98 | + if (!r.success) |
| 99 | + return; |
| 100 | + double a = 1.0 + 0x1.0p-52; |
| 101 | + LIBC_DO_NOT_OPTIMIZE(a); |
| 102 | + DoubleDouble prod = LIBC_NAMESPACE::fputil::exact_mult(a, a); |
| 103 | + EXPECT_FP_EQ(prod.hi, expected_hi); |
| 104 | + EXPECT_FP_EQ(prod.lo, expected_lo); |
| 105 | + }; |
| 106 | + |
| 107 | + check_square(RoundingMode::Nearest, 1.0 + 0x1.0p-51, 0x1.0p-104); |
| 108 | +#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY |
| 109 | + check_square(RoundingMode::Upward, 1.0 + 0x1.8p-51, |
| 110 | + -0x1.0p-52 + 0x1.0p-104); |
| 111 | + check_square(RoundingMode::Downward, 1.0 + 0x1.0p-51, 0x1.0p-104); |
| 112 | + check_square(RoundingMode::TowardZero, 1.0 + 0x1.0p-51, 0x1.0p-104); |
| 113 | +#endif |
| 114 | + |
| 115 | + // Four-way exact_mult + exact_add<false> cancellation pattern (as in |
| 116 | + // sincos, b/586440175): |
| 117 | + // p0 = (1 + 2^-52) * (1 + 2^-52) = 1 + 2^-51 + 2^-104 |
| 118 | + // p1 = (1 + 2^-51) * -(1 + 2^-51) = -1 - 2^-50 - 2^-102 |
| 119 | + // p0 + p1 = -(2^-51 + 1.5 * 2^-103) (exact midpoint in ulp(2^-51)) |
| 120 | + // p2 = (1 + 2^-50) * (1 + 2^-50) = 1 + 2^-49 + 2^-100 |
| 121 | + // p3 = (1 + 2^-49) * -(1 + 2^-49) = -1 - 2^-48 - 2^-98 |
| 122 | + // p2 + p3 = -(2^-49 + 3 * 2^-100) (exact binary64: |
| 123 | + // -0x1.0000000000006p-49) |
| 124 | + auto check_four_way = [](RoundingMode mode, double expected_s0, |
| 125 | + double expected_s1) { |
| 126 | + ForceRoundingMode r(mode); |
| 127 | + if (!r.success) |
| 128 | + return; |
| 129 | + |
| 130 | + double a0 = 1.0 + 0x1.0p-52; |
| 131 | + double b0 = 1.0 + 0x1.0p-52; |
| 132 | + double a1 = 1.0 + 0x1.0p-51; |
| 133 | + double b1 = -(1.0 + 0x1.0p-51); |
| 134 | + double a2 = 1.0 + 0x1.0p-50; |
| 135 | + double b2 = 1.0 + 0x1.0p-50; |
| 136 | + double a3 = 1.0 + 0x1.0p-49; |
| 137 | + double b3 = -(1.0 + 0x1.0p-49); |
| 138 | + LIBC_DO_NOT_OPTIMIZE(a0); |
| 139 | + LIBC_DO_NOT_OPTIMIZE(b0); |
| 140 | + LIBC_DO_NOT_OPTIMIZE(a1); |
| 141 | + LIBC_DO_NOT_OPTIMIZE(b1); |
| 142 | + LIBC_DO_NOT_OPTIMIZE(a2); |
| 143 | + LIBC_DO_NOT_OPTIMIZE(b2); |
| 144 | + LIBC_DO_NOT_OPTIMIZE(a3); |
| 145 | + LIBC_DO_NOT_OPTIMIZE(b3); |
| 146 | + |
| 147 | + DoubleDouble p0 = LIBC_NAMESPACE::fputil::exact_mult(a0, b0); |
| 148 | + DoubleDouble p1 = LIBC_NAMESPACE::fputil::exact_mult(a1, b1); |
| 149 | + DoubleDouble p2 = LIBC_NAMESPACE::fputil::exact_mult(a2, b2); |
| 150 | + DoubleDouble p3 = LIBC_NAMESPACE::fputil::exact_mult(a3, b3); |
| 151 | + |
| 152 | + DoubleDouble s0 = LIBC_NAMESPACE::fputil::exact_add<false>(p0.hi, p1.hi); |
| 153 | + DoubleDouble s1 = LIBC_NAMESPACE::fputil::exact_add<false>(p2.hi, p3.hi); |
| 154 | + s0.lo += p0.lo + p1.lo; |
| 155 | + s1.lo += p2.lo + p3.lo; |
| 156 | + |
| 157 | + EXPECT_FP_EQ(s0.hi + s0.lo, expected_s0); |
| 158 | + EXPECT_FP_EQ(s1.hi + s1.lo, expected_s1); |
| 159 | + }; |
| 160 | + |
| 161 | + check_four_way(RoundingMode::Nearest, -0x1.0000000000002p-51, |
| 162 | + -0x1.0000000000006p-49); |
| 163 | +#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY |
| 164 | + check_four_way(RoundingMode::Upward, -0x1.0000000000001p-51, |
| 165 | + -0x1.0000000000006p-49); |
| 166 | + check_four_way(RoundingMode::Downward, -0x1.0000000000002p-51, |
| 167 | + -0x1.0000000000006p-49); |
| 168 | + check_four_way(RoundingMode::TowardZero, -0x1.0000000000001p-51, |
| 169 | + -0x1.0000000000006p-49); |
| 170 | +#endif |
| 171 | + } |
| 172 | +} |
0 commit comments