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 DECLARE_SPECIAL_CONSTANTS(float)
23 
24 TEST(RemquofTest, SpecialNumbers) {
25   int exponent;
26   float x, y;
27 
28   y = 1.0f;
29   x = inf;
30   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
31   x = negInf;
32   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
33 
34   x = 1.0f;
35   y = zero;
36   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
37   y = negZero;
38   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
39 
40   y = nan;
41   x = 1.0f;
42   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
43 
44   y = 1.0f;
45   x = nan;
46   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
47 
48   x = nan;
49   y = nan;
50   EXPECT_NE(isnan(__llvm_libc::remquof(x, y, &exponent)), 0);
51 
52   x = zero;
53   y = 1.0f;
54   EXPECT_FP_EQ(__llvm_libc::remquof(x, y, &exponent), zero);
55 
56   x = negZero;
57   y = 1.0f;
58   EXPECT_FP_EQ(__llvm_libc::remquof(x, y, &exponent), negZero);
59 }
60 
61 TEST(RemquofTest, SubnormalRange) {
62   constexpr UIntType count = 1000001;
63   constexpr UIntType step =
64       (FPBits::maxSubnormal - FPBits::minSubnormal) / count;
65   for (UIntType v = FPBits::minSubnormal, w = FPBits::maxSubnormal;
66        v <= FPBits::maxSubnormal && w >= FPBits::minSubnormal;
67        v += step, w -= step) {
68     float x = FPBits(v), y = FPBits(w);
69     mpfr::BinaryOutput<float> result;
70     mpfr::BinaryInput<float> input{x, y};
71     result.f = __llvm_libc::remquof(x, y, &result.i);
72     ASSERT_MPFR_MATCH(mpfr::Operation::RemQuo, input, result, 0.0);
73   }
74 }
75 
76 TEST(RemquofTest, NormalRange) {
77   constexpr UIntType count = 1000001;
78   constexpr UIntType step = (FPBits::maxNormal - FPBits::minNormal) / count;
79   for (UIntType v = FPBits::minNormal, w = FPBits::maxNormal;
80        v <= FPBits::maxNormal && w >= FPBits::minNormal; v += step, w -= step) {
81     float x = FPBits(v), y = FPBits(w);
82     mpfr::BinaryOutput<float> result;
83     mpfr::BinaryInput<float> input{x, y};
84     result.f = __llvm_libc::remquof(x, y, &result.i);
85     ASSERT_MPFR_MATCH(mpfr::Operation::RemQuo, input, result, 0.0);
86   }
87 }
88