1 //===-- Unittests for roundf ----------------------------------------------===//
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/roundf.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<float>;
17 
18 namespace mpfr = __llvm_libc::testing::mpfr;
19 
20 static const float zero = FPBits::zero();
21 static const float negZero = FPBits::negZero();
22 static const float nan = FPBits::buildNaN(1);
23 static const float inf = FPBits::inf();
24 static const float 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(RoundfTest, SpecialNumbers) {
30   EXPECT_FP_EQ(zero, __llvm_libc::roundf(zero));
31   EXPECT_FP_EQ(negZero, __llvm_libc::roundf(negZero));
32 
33   EXPECT_FP_EQ(inf, __llvm_libc::roundf(inf));
34   EXPECT_FP_EQ(negInf, __llvm_libc::roundf(negInf));
35 
36   ASSERT_NE(isnan(nan), 0);
37   ASSERT_NE(isnan(__llvm_libc::roundf(nan)), 0);
38 }
39 
40 TEST(RoundfTest, RoundedNumbers) {
41   EXPECT_FP_EQ(1.0f, __llvm_libc::roundf(1.0f));
42   EXPECT_FP_EQ(-1.0f, __llvm_libc::roundf(-1.0f));
43   EXPECT_FP_EQ(10.0f, __llvm_libc::roundf(10.0f));
44   EXPECT_FP_EQ(-10.0f, __llvm_libc::roundf(-10.0f));
45   EXPECT_FP_EQ(1234.0f, __llvm_libc::roundf(1234.0f));
46   EXPECT_FP_EQ(-1234.0f, __llvm_libc::roundf(-1234.0f));
47 }
48 
49 TEST(RoundfTest, Fractions) {
50   EXPECT_FP_EQ(1.0f, __llvm_libc::roundf(0.5f));
51   EXPECT_FP_EQ(-1.0f, __llvm_libc::roundf(-0.5f));
52   EXPECT_FP_EQ(0.0f, __llvm_libc::roundf(0.115f));
53   EXPECT_FP_EQ(-0.0f, __llvm_libc::roundf(-0.115f));
54   EXPECT_FP_EQ(1.0f, __llvm_libc::roundf(0.715f));
55   EXPECT_FP_EQ(-1.0f, __llvm_libc::roundf(-0.715f));
56   EXPECT_FP_EQ(1.0f, __llvm_libc::roundf(1.3f));
57   EXPECT_FP_EQ(-1.0f, __llvm_libc::roundf(-1.3f));
58   EXPECT_FP_EQ(2.0f, __llvm_libc::roundf(1.5f));
59   EXPECT_FP_EQ(-2.0f, __llvm_libc::roundf(-1.5f));
60   EXPECT_FP_EQ(2.0f, __llvm_libc::roundf(1.75f));
61   EXPECT_FP_EQ(-2.0f, __llvm_libc::roundf(-1.75f));
62   EXPECT_FP_EQ(10.0f, __llvm_libc::roundf(10.32f));
63   EXPECT_FP_EQ(-10.0f, __llvm_libc::roundf(-10.32f));
64   EXPECT_FP_EQ(11.0f, __llvm_libc::roundf(10.65f));
65   EXPECT_FP_EQ(-11.0f, __llvm_libc::roundf(-10.65f));
66   EXPECT_FP_EQ(1234.0f, __llvm_libc::roundf(1234.38f));
67   EXPECT_FP_EQ(-1234.0f, __llvm_libc::roundf(-1234.38f));
68   EXPECT_FP_EQ(1235.0f, __llvm_libc::roundf(1234.96f));
69   EXPECT_FP_EQ(-1235.0f, __llvm_libc::roundf(-1234.96f));
70 }
71 
72 TEST(RoundfTest, InFloatRange) {
73   using UIntType = FPBits::UIntType;
74   constexpr UIntType count = 1000000;
75   constexpr UIntType step = UIntType(-1) / count;
76   for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
77     float x = FPBits(v);
78     if (isnan(x) || isinf(x))
79       continue;
80 
81     ASSERT_MPFR_MATCH(mpfr::Operation::Round, x, __llvm_libc::roundf(x),
82                       tolerance);
83   }
84 }
85