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

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