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