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