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 "test/UnitTest/FEnvSafeTest.h"
14#include "test/UnitTest/FPMatcher.h"
15#include "test/UnitTest/Test.h"
16
17template <typename T>
18class FmaTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
19private:
20 using Func = T (*)(T, T, T);
21 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
22 using StorageType = typename FPBits::StorageType;
23
24 const T inf = FPBits::inf(Sign::POS).get_val();
25 const T neg_inf = FPBits::inf(Sign::NEG).get_val();
26 const T zero = FPBits::zero(Sign::POS).get_val();
27 const T neg_zero = FPBits::zero(Sign::NEG).get_val();
28 const T nan = FPBits::quiet_nan().get_val();
29
30public:
31 void test_special_numbers(Func func) {
32 EXPECT_FP_EQ(func(zero, zero, zero), zero);
33 EXPECT_FP_EQ(func(zero, neg_zero, neg_zero), neg_zero);
34 EXPECT_FP_EQ(func(inf, inf, zero), inf);
35 EXPECT_FP_EQ(func(neg_inf, inf, neg_inf), neg_inf);
36 EXPECT_FP_EQ(func(inf, zero, zero), nan);
37 EXPECT_FP_EQ(func(inf, neg_inf, inf), nan);
38 EXPECT_FP_EQ(func(nan, zero, inf), nan);
39 EXPECT_FP_EQ(func(inf, neg_inf, nan), nan);
40
41 // Test underflow rounding up.
42 EXPECT_FP_EQ(func(T(0.5), FPBits::min_subnormal().get_val(),
43 FPBits::min_subnormal().get_val()),
44 FPBits(StorageType(2)).get_val());
45 // Test underflow rounding down.
46 StorageType MIN_NORMAL = FPBits::min_normal().uintval();
47 T v = FPBits(MIN_NORMAL + StorageType(1)).get_val();
48 EXPECT_FP_EQ(
49 func(T(1) / T(MIN_NORMAL << 1), v, FPBits::min_normal().get_val()), v);
50 // Test overflow.
51 T z = FPBits::max_normal().get_val();
52 EXPECT_FP_EQ(func(T(1.75), z, -z), T(0.75) * z);
53 // Exact cancellation.
54 EXPECT_FP_EQ(func(T(3.0), T(5.0), -T(15.0)), T(0.0));
55 EXPECT_FP_EQ(func(T(-3.0), T(5.0), T(15.0)), T(0.0));
56 }
57};
58
59#endif // LLVM_LIBC_TEST_SRC_MATH_FMATEST_H
60

source code of libc/test/src/math/smoke/FmaTest.h