1 //===-- Unittests for floor -----------------------------------------------===//
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/floor.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<double>;
17 
18 DECLARE_SPECIAL_CONSTANTS(double)
19 
20 namespace mpfr = __llvm_libc::testing::mpfr;
21 
TEST(FloorTest,SpecialNumbers)22 TEST(FloorTest, SpecialNumbers) {
23   EXPECT_FP_EQ(zero, __llvm_libc::floor(zero));
24   EXPECT_FP_EQ(negZero, __llvm_libc::floor(negZero));
25 
26   EXPECT_FP_EQ(inf, __llvm_libc::floor(inf));
27   EXPECT_FP_EQ(negInf, __llvm_libc::floor(negInf));
28 
29   ASSERT_NE(isnan(nan), 0);
30   ASSERT_NE(isnan(__llvm_libc::floor(nan)), 0);
31 }
32 
TEST(FloorTest,RoundedNumbers)33 TEST(FloorTest, RoundedNumbers) {
34   EXPECT_FP_EQ(1.0, __llvm_libc::floor(1.0));
35   EXPECT_FP_EQ(-1.0, __llvm_libc::floor(-1.0));
36   EXPECT_FP_EQ(10.0, __llvm_libc::floor(10.0));
37   EXPECT_FP_EQ(-10.0, __llvm_libc::floor(-10.0));
38   EXPECT_FP_EQ(1234.0, __llvm_libc::floor(1234.0));
39   EXPECT_FP_EQ(-1234.0, __llvm_libc::floor(-1234.0));
40 }
41 
TEST(FloorTest,Fractions)42 TEST(FloorTest, Fractions) {
43   EXPECT_FP_EQ(0.0, __llvm_libc::floor(0.5));
44   EXPECT_FP_EQ(-1.0, __llvm_libc::floor(-0.5));
45   EXPECT_FP_EQ(0.0, __llvm_libc::floor(0.115));
46   EXPECT_FP_EQ(-1.0, __llvm_libc::floor(-0.115));
47   EXPECT_FP_EQ(0.0, __llvm_libc::floor(0.715));
48   EXPECT_FP_EQ(-1.0, __llvm_libc::floor(-0.715));
49   EXPECT_FP_EQ(1.0, __llvm_libc::floor(1.3));
50   EXPECT_FP_EQ(-2.0, __llvm_libc::floor(-1.3));
51   EXPECT_FP_EQ(1.0, __llvm_libc::floor(1.5));
52   EXPECT_FP_EQ(-2.0, __llvm_libc::floor(-1.5));
53   EXPECT_FP_EQ(1.0, __llvm_libc::floor(1.75));
54   EXPECT_FP_EQ(-2.0, __llvm_libc::floor(-1.75));
55   EXPECT_FP_EQ(10.0, __llvm_libc::floor(10.32));
56   EXPECT_FP_EQ(-11.0, __llvm_libc::floor(-10.32));
57   EXPECT_FP_EQ(10.0, __llvm_libc::floor(10.65));
58   EXPECT_FP_EQ(-11.0, __llvm_libc::floor(-10.65));
59   EXPECT_FP_EQ(1234.0, __llvm_libc::floor(1234.38));
60   EXPECT_FP_EQ(-1235.0, __llvm_libc::floor(-1234.38));
61   EXPECT_FP_EQ(1234.0, __llvm_libc::floor(1234.96));
62   EXPECT_FP_EQ(-1235.0, __llvm_libc::floor(-1234.96));
63 }
64 
TEST(FloorTest,InDoubleRange)65 TEST(FloorTest, InDoubleRange) {
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     double x = FPBits(v);
71     if (isnan(x) || isinf(x))
72       continue;
73 
74     ASSERT_MPFR_MATCH(mpfr::Operation::Floor, x, __llvm_libc::floor(x), 0.0);
75   }
76 }
77