1 //===-- Unittests for remquof ---------------------------------------------===//
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/remquof.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<float>;
18 using UIntType = FPBits::UIntType;
19 
20 namespace mpfr = __llvm_libc::testing::mpfr;
21 
22 static const float zero = FPBits::zero();
23 static const float negZero = FPBits::negZero();
24 static const float nan = FPBits::buildNaN(1);
25 static const float inf = FPBits::inf();
26 static const float negInf = FPBits::negInf();
27 
28 TEST(RemquofTest, SpecialNumbers) {
29   int exponent;
30   float x, y;
31 
32   y = 1.0f;
33   x = inf;
34   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
35   x = negInf;
36   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
37 
38   x = 1.0f;
39   y = zero;
40   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
41   y = negZero;
42   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
43 
44   y = nan;
45   x = 1.0f;
46   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
47 
48   y = 1.0f;
49   x = nan;
50   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
51 
52   x = nan;
53   y = nan;
54   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
55 
56   x = zero;
57   y = 1.0f;
58   EXPECT_FP_EQ(__llvm_libc::remquof(x, y, &exponent), zero);
59 
60   x = negZero;
61   y = 1.0f;
62   EXPECT_FP_EQ(__llvm_libc::remquof(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     float x = FPBits(v), y = FPBits(w);
73     mpfr::BinaryOutput<float> result;
74     mpfr::BinaryInput<float> input{x, y};
75     result.f = __llvm_libc::remquof(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     float x = FPBits(v), y = FPBits(w);
86     mpfr::BinaryOutput<float> result;
87     mpfr::BinaryInput<float> input{x, y};
88     result.f = __llvm_libc::remquof(x, y, &result.i);
89     ASSERT_MPFR_MATCH(mpfr::Operation::RemQuo, input, result, 0.0);
90   }
91 }
92