1 //===-- Unittests for remquol ---------------------------------------------===// 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 #include "include/math.h" 10 #include "src/math/remquol.h" 11 #include "utils/FPUtil/BasicOperations.h" 12 #include "utils/FPUtil/FPBits.h" 13 #include "utils/FPUtil/TestHelpers.h" 14 #include "utils/MPFRWrapper/MPFRUtils.h" 15 #include "utils/UnitTest/Test.h" 16 17 using FPBits = __llvm_libc::fputil::FPBits<long double>; 18 using UIntType = FPBits::UIntType; 19 20 namespace mpfr = __llvm_libc::testing::mpfr; 21 22 static const long double zero = FPBits::zero(); 23 static const long double negZero = FPBits::negZero(); 24 static const long double nan = FPBits::buildNaN(1); 25 static const long double inf = FPBits::inf(); 26 static const long double negInf = FPBits::negInf(); 27 28 TEST(RemquoTest, SpecialNumbers) { 29 int exponent; 30 long double x, y; 31 32 y = 1.0l; 33 x = inf; 34 EXPECT_NE(isnan(__llvm_libc::remquol(x, y, &exponent)), 0); 35 x = negInf; 36 EXPECT_NE(isnan(__llvm_libc::remquol(x, y, &exponent)), 0); 37 38 x = 1.0l; 39 y = zero; 40 EXPECT_NE(isnan(__llvm_libc::remquol(x, y, &exponent)), 0); 41 y = negZero; 42 EXPECT_NE(isnan(__llvm_libc::remquol(x, y, &exponent)), 0); 43 44 y = nan; 45 x = 1.0l; 46 EXPECT_NE(isnan(__llvm_libc::remquol(x, y, &exponent)), 0); 47 48 y = 1.0l; 49 x = nan; 50 EXPECT_NE(isnan(__llvm_libc::remquol(x, y, &exponent)), 0); 51 52 x = nan; 53 y = nan; 54 EXPECT_NE(isnan(__llvm_libc::remquol(x, y, &exponent)), 0); 55 56 x = zero; 57 y = 1.0l; 58 EXPECT_FP_EQ(__llvm_libc::remquol(x, y, &exponent), zero); 59 60 x = negZero; 61 y = 1.0l; 62 EXPECT_FP_EQ(__llvm_libc::remquol(x, y, &exponent), negZero); 63 } 64 65 TEST(RemquofTest, SubnormalRange) { 66 constexpr UIntType count = 1000001; 67 constexpr UIntType step = 68 (FPBits::maxSubnormal - FPBits::minSubnormal) / count; 69 for (UIntType v = FPBits::minSubnormal, w = FPBits::maxSubnormal; 70 v <= FPBits::maxSubnormal && w >= FPBits::minSubnormal; 71 v += step, w -= step) { 72 long double x = FPBits(v), y = FPBits(w); 73 mpfr::BinaryOutput<long double> result; 74 mpfr::BinaryInput<long double> input{x, y}; 75 result.f = __llvm_libc::remquol(x, y, &result.i); 76 ASSERT_MPFR_MATCH(mpfr::Operation::RemQuo, input, result, 0.0); 77 } 78 } 79 80 TEST(RemquofTest, NormalRange) { 81 constexpr UIntType count = 1000001; 82 constexpr UIntType step = (FPBits::maxNormal - FPBits::minNormal) / count; 83 for (UIntType v = FPBits::minNormal, w = FPBits::maxNormal; 84 v <= FPBits::maxNormal && w >= FPBits::minNormal; v += step, w -= step) { 85 long double x = FPBits(v), y = FPBits(w); 86 mpfr::BinaryOutput<long double> result; 87 result.f = __llvm_libc::remquol(x, y, &result.i); 88 // In normal range on x86 platforms, the implicit 1 bit can be zero making 89 // the numbers NaN. Hence we test for them separately. 90 if (isnan(x) || isnan(y)) { 91 ASSERT_NE(isnan(result.f), 0); 92 } else { 93 mpfr::BinaryInput<long double> input{x, y}; 94 ASSERT_MPFR_MATCH(mpfr::Operation::RemQuo, input, result, 0.0); 95 } 96 } 97 } 98