1 //===-- Utility class to test different flavors of fma --------------------===//
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 #ifndef LLVM_LIBC_TEST_SRC_MATH_FMATEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_FMATEST_H
11 
12 #include "src/__support/FPUtil/FPBits.h"
13 #include "utils/MPFRWrapper/MPFRUtils.h"
14 #include "utils/UnitTest/FPMatcher.h"
15 #include "utils/UnitTest/Test.h"
16 #include "utils/testutils/RandUtils.h"
17 
18 namespace mpfr = __llvm_libc::testing::mpfr;
19 
20 template <typename T>
21 class FmaTestTemplate : public __llvm_libc::testing::Test {
22 private:
23   using Func = T (*)(T, T, T);
24   using FPBits = __llvm_libc::fputil::FPBits<T>;
25   using UIntType = typename FPBits::UIntType;
26   const T nan = T(__llvm_libc::fputil::FPBits<T>::build_nan(1));
27   const T inf = T(__llvm_libc::fputil::FPBits<T>::inf());
28   const T neg_inf = T(__llvm_libc::fputil::FPBits<T>::neg_inf());
29   const T zero = T(__llvm_libc::fputil::FPBits<T>::zero());
30   const T neg_zero = T(__llvm_libc::fputil::FPBits<T>::neg_zero());
31 
get_random_bit_pattern()32   UIntType get_random_bit_pattern() {
33     UIntType bits{0};
34     for (UIntType i = 0; i < sizeof(UIntType) / 2; ++i) {
35       bits =
36           (bits << 2) + static_cast<uint16_t>(__llvm_libc::testutils::rand());
37     }
38     return bits;
39   }
40 
41 public:
test_special_numbers(Func func)42   void test_special_numbers(Func func) {
43     EXPECT_FP_EQ(func(zero, zero, zero), zero);
44     EXPECT_FP_EQ(func(zero, neg_zero, neg_zero), neg_zero);
45     EXPECT_FP_EQ(func(inf, inf, zero), inf);
46     EXPECT_FP_EQ(func(neg_inf, inf, neg_inf), neg_inf);
47     EXPECT_FP_EQ(func(inf, zero, zero), nan);
48     EXPECT_FP_EQ(func(inf, neg_inf, inf), nan);
49     EXPECT_FP_EQ(func(nan, zero, inf), nan);
50     EXPECT_FP_EQ(func(inf, neg_inf, nan), nan);
51 
52     // Test underflow rounding up.
53     EXPECT_FP_EQ(func(T(0.5), T(FPBits(FPBits::MIN_SUBNORMAL)),
54                       T(FPBits(FPBits::MIN_SUBNORMAL))),
55                  T(FPBits(UIntType(2))));
56     // Test underflow rounding down.
57     T v = T(FPBits(FPBits::MIN_NORMAL + UIntType(1)));
58     EXPECT_FP_EQ(func(T(1) / T(FPBits::MIN_NORMAL << 1), v,
59                       T(FPBits(FPBits::MIN_NORMAL))),
60                  v);
61     // Test overflow.
62     T z = T(FPBits(FPBits::MAX_NORMAL));
63     EXPECT_FP_EQ(func(T(1.75), z, -z), T(0.75) * z);
64     // Exact cancellation.
65     EXPECT_FP_EQ(func(T(3.0), T(5.0), -T(15.0)), T(0.0));
66     EXPECT_FP_EQ(func(T(-3.0), T(5.0), T(15.0)), T(0.0));
67   }
68 
test_subnormal_range(Func func)69   void test_subnormal_range(Func func) {
70     constexpr UIntType COUNT = 1000001;
71     constexpr UIntType STEP =
72         (FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / COUNT;
73     for (UIntType v = FPBits::MIN_SUBNORMAL, w = FPBits::MAX_SUBNORMAL;
74          v <= FPBits::MAX_SUBNORMAL && w >= FPBits::MIN_SUBNORMAL;
75          v += STEP, w -= STEP) {
76       T x = T(FPBits(get_random_bit_pattern())), y = T(FPBits(v)),
77         z = T(FPBits(w));
78       mpfr::TernaryInput<T> input{x, y, z};
79       ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Fma, input, func(x, y, z),
80                                      0.5);
81     }
82   }
83 
test_normal_range(Func func)84   void test_normal_range(Func func) {
85     constexpr UIntType COUNT = 1000001;
86     constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
87     for (UIntType v = FPBits::MIN_NORMAL, w = FPBits::MAX_NORMAL;
88          v <= FPBits::MAX_NORMAL && w >= FPBits::MIN_NORMAL;
89          v += STEP, w -= STEP) {
90       T x = T(FPBits(v)), y = T(FPBits(w)),
91         z = T(FPBits(get_random_bit_pattern()));
92       mpfr::TernaryInput<T> input{x, y, z};
93       ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Fma, input, func(x, y, z),
94                                      0.5);
95     }
96   }
97 };
98 
99 #endif // LLVM_LIBC_TEST_SRC_MATH_FMATEST_H
100