Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / math / ModfTest.h
blob21adef8a08d8d4948dfb3f369934cf4709d1d1c3
1 //===-- Utility class to test floor[f|l] ------------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 #include "src/__support/FPUtil/BasicOperations.h"
10 #include "src/__support/FPUtil/NearestIntegerOperations.h"
11 #include "test/UnitTest/FPMatcher.h"
12 #include "test/UnitTest/Test.h"
13 #include "utils/MPFRWrapper/MPFRUtils.h"
15 #include <math.h>
17 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
19 template <typename T> class ModfTest : public LIBC_NAMESPACE::testing::Test {
21 DECLARE_SPECIAL_CONSTANTS(T)
23 public:
24 typedef T (*ModfFunc)(T, T *);
26 void testSpecialNumbers(ModfFunc func) {
27 T integral;
29 EXPECT_FP_EQ(zero, func(zero, &integral));
30 EXPECT_FP_EQ(integral, zero);
31 EXPECT_FP_EQ(neg_zero, func(neg_zero, &integral));
32 EXPECT_FP_EQ(integral, neg_zero);
34 EXPECT_FP_EQ(zero, func(inf, &integral));
35 EXPECT_FP_EQ(inf, integral);
36 EXPECT_FP_EQ(neg_zero, func(neg_inf, &integral));
37 EXPECT_FP_EQ(neg_inf, integral);
39 EXPECT_FP_EQ(aNaN, func(aNaN, &integral));
42 void testIntegers(ModfFunc func) {
43 T integral;
45 EXPECT_FP_EQ(T(0.0), func(T(1.0), &integral));
46 EXPECT_FP_EQ(T(1.0), integral);
48 EXPECT_FP_EQ(T(-0.0), func(T(-1.0), &integral));
49 EXPECT_FP_EQ(T(-1.0), integral);
51 EXPECT_FP_EQ(T(0.0), func(T(10.0), &integral));
52 EXPECT_FP_EQ(T(10.0), integral);
54 EXPECT_FP_EQ(T(-0.0), func(T(-10.0), &integral));
55 EXPECT_FP_EQ(T(-10.0), integral);
57 EXPECT_FP_EQ(T(0.0), func(T(12345.0), &integral));
58 EXPECT_FP_EQ(T(12345.0), integral);
60 EXPECT_FP_EQ(T(-0.0), func(T(-12345.0), &integral));
61 EXPECT_FP_EQ(T(-12345.0), integral);
64 void testFractions(ModfFunc func) {
65 T integral;
67 EXPECT_FP_EQ(T(0.5), func(T(1.5), &integral));
68 EXPECT_FP_EQ(integral, T(1.0));
70 EXPECT_FP_EQ(T(-0.5), func(T(-1.5), &integral));
71 EXPECT_FP_EQ(integral, T(-1.0));
73 EXPECT_FP_EQ(T(0.75), func(T(10.75), &integral));
74 EXPECT_FP_EQ(integral, T(10.0));
76 EXPECT_FP_EQ(T(-0.75), func(T(-10.75), &integral));
77 EXPECT_FP_EQ(integral, T(-10.0));
79 EXPECT_FP_EQ(T(0.125), func(T(100.125), &integral));
80 EXPECT_FP_EQ(integral, T(100.0));
82 EXPECT_FP_EQ(T(-0.125), func(T(-100.125), &integral));
83 EXPECT_FP_EQ(integral, T(-100.0));
86 void testRange(ModfFunc func) {
87 constexpr UIntType COUNT = 100'000;
88 constexpr UIntType STEP = UIntType(-1) / COUNT;
89 for (UIntType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
90 T x = T(FPBits(v));
91 if (isnan(x) || isinf(x) || x == T(0.0))
92 continue;
94 T integral;
95 T frac = func(x, &integral);
96 ASSERT_TRUE(LIBC_NAMESPACE::fputil::abs(frac) < 1.0l);
97 ASSERT_TRUE(LIBC_NAMESPACE::fputil::trunc(x) == integral);
98 ASSERT_TRUE(integral + frac == x);
103 #define LIST_MODF_TESTS(T, func) \
104 using LlvmLibcModfTest = ModfTest<T>; \
105 TEST_F(LlvmLibcModfTest, SpecialNumbers) { testSpecialNumbers(&func); } \
106 TEST_F(LlvmLibcModfTest, RoundedNubmers) { testIntegers(&func); } \
107 TEST_F(LlvmLibcModfTest, Fractions) { testFractions(&func); } \
108 TEST_F(LlvmLibcModfTest, Range) { testRange(&func); }