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 
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:
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   }
65 
66   void test_subnormal_range(Func func) {
67     constexpr UIntType COUNT = 1000001;
68     constexpr UIntType STEP =
69         (FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / COUNT;
70     for (UIntType v = FPBits::MIN_SUBNORMAL, w = FPBits::MAX_SUBNORMAL;
71          v <= FPBits::MAX_SUBNORMAL && w >= FPBits::MIN_SUBNORMAL;
72          v += STEP, w -= STEP) {
73       T x = T(FPBits(get_random_bit_pattern())), y = T(FPBits(v)),
74         z = T(FPBits(w));
75       T result = func(x, y, z);
76       mpfr::TernaryInput<T> input{x, y, z};
77       ASSERT_MPFR_MATCH(mpfr::Operation::Fma, input, result, 0.5);
78     }
79   }
80 
81   void test_normal_range(Func func) {
82     constexpr UIntType COUNT = 1000001;
83     constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
84     for (UIntType v = FPBits::MIN_NORMAL, w = FPBits::MAX_NORMAL;
85          v <= FPBits::MAX_NORMAL && w >= FPBits::MIN_NORMAL;
86          v += STEP, w -= STEP) {
87       T x = T(FPBits(v)), y = T(FPBits(w)),
88         z = T(FPBits(get_random_bit_pattern()));
89       T result = func(x, y, z);
90       mpfr::TernaryInput<T> input{x, y, z};
91       ASSERT_MPFR_MATCH(mpfr::Operation::Fma, input, result, 0.5);
92     }
93   }
94 };
95 
96 #endif // LLVM_LIBC_TEST_SRC_MATH_FMATEST_H
97