1 //===-- Unittests for round -----------------------------------------------===//
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/round.h"
11 #include "utils/FPUtil/FPBits.h"
12 #include "utils/FPUtil/TestHelpers.h"
13 #include "utils/MPFRWrapper/MPFRUtils.h"
14 #include "utils/UnitTest/Test.h"
15 
16 using FPBits = __llvm_libc::fputil::FPBits<double>;
17 
18 namespace mpfr = __llvm_libc::testing::mpfr;
19 
20 static const double zero = FPBits::zero();
21 static const double negZero = FPBits::negZero();
22 static const double nan = FPBits::buildNaN(1);
23 static const double inf = FPBits::inf();
24 static const double negInf = FPBits::negInf();
25 
26 // Zero tolerance; As in, exact match with MPFR result.
27 static constexpr mpfr::Tolerance tolerance{mpfr::Tolerance::floatPrecision, 0,
28                                            0};
29 TEST(RoundTest, SpecialNumbers) {
30   EXPECT_FP_EQ(zero, __llvm_libc::round(zero));
31   EXPECT_FP_EQ(negZero, __llvm_libc::round(negZero));
32 
33   EXPECT_FP_EQ(inf, __llvm_libc::round(inf));
34   EXPECT_FP_EQ(negInf, __llvm_libc::round(negInf));
35 
36   ASSERT_NE(isnan(nan), 0);
37   ASSERT_NE(isnan(__llvm_libc::round(nan)), 0);
38 }
39 
40 TEST(RoundTest, RoundedNumbers) {
41   EXPECT_FP_EQ(1.0, __llvm_libc::round(1.0));
42   EXPECT_FP_EQ(-1.0, __llvm_libc::round(-1.0));
43   EXPECT_FP_EQ(10.0, __llvm_libc::round(10.0));
44   EXPECT_FP_EQ(-10.0, __llvm_libc::round(-10.0));
45   EXPECT_FP_EQ(1234.0, __llvm_libc::round(1234.0));
46   EXPECT_FP_EQ(-1234.0, __llvm_libc::round(-1234.0));
47 }
48 
49 TEST(RoundTest, Fractions) {
50   EXPECT_FP_EQ(1.0, __llvm_libc::round(0.5));
51   EXPECT_FP_EQ(-1.0, __llvm_libc::round(-0.5));
52   EXPECT_FP_EQ(0.0, __llvm_libc::round(0.115));
53   EXPECT_FP_EQ(-0.0, __llvm_libc::round(-0.115));
54   EXPECT_FP_EQ(1.0, __llvm_libc::round(0.715));
55   EXPECT_FP_EQ(-1.0, __llvm_libc::round(-0.715));
56   EXPECT_FP_EQ(1.0, __llvm_libc::round(1.3));
57   EXPECT_FP_EQ(-1.0, __llvm_libc::round(-1.3));
58   EXPECT_FP_EQ(2.0, __llvm_libc::round(1.5));
59   EXPECT_FP_EQ(-2.0, __llvm_libc::round(-1.5));
60   EXPECT_FP_EQ(2.0, __llvm_libc::round(1.75));
61   EXPECT_FP_EQ(-2.0, __llvm_libc::round(-1.75));
62   EXPECT_FP_EQ(10.0, __llvm_libc::round(10.32));
63   EXPECT_FP_EQ(-10.0, __llvm_libc::round(-10.32));
64   EXPECT_FP_EQ(11.0, __llvm_libc::round(10.65));
65   EXPECT_FP_EQ(-11.0, __llvm_libc::round(-10.65));
66   EXPECT_FP_EQ(1234.0, __llvm_libc::round(1234.38));
67   EXPECT_FP_EQ(-1234.0, __llvm_libc::round(-1234.38));
68   EXPECT_FP_EQ(1235.0, __llvm_libc::round(1234.96));
69   EXPECT_FP_EQ(-1235.0, __llvm_libc::round(-1234.96));
70 }
71 
72 TEST(RoundTest, InDoubleRange) {
73   using UIntType = FPBits::UIntType;
74   constexpr UIntType count = 10000000;
75   constexpr UIntType step = UIntType(-1) / count;
76   for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
77     double x = FPBits(v);
78     if (isnan(x) || isinf(x))
79       continue;
80 
81     ASSERT_MPFR_MATCH(mpfr::Operation::Round, x, __llvm_libc::round(x),
82                       tolerance);
83   }
84 }
85