summaryrefslogtreecommitdiff
path: root/libc/test/src/math/smoke/FmaTest.h
blob: 0d775acad95fde40d698d78a94af1e375f1ab108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//===-- Utility class to test different flavors of fma --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_TEST_SRC_MATH_FMATEST_H
#define LLVM_LIBC_TEST_SRC_MATH_FMATEST_H

#include "src/__support/FPUtil/cast.h"
#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

template <typename OutType, typename InType = OutType>
class FmaTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {

  struct OutConstants {
    DECLARE_SPECIAL_CONSTANTS(OutType)
  };

  struct InConstants {
    DECLARE_SPECIAL_CONSTANTS(InType)
  };

  using OutFPBits = typename OutConstants::FPBits;
  using OutStorageType = typename OutConstants::StorageType;
  using InFPBits = typename InConstants::FPBits;
  using InStorageType = typename InConstants::StorageType;

  static constexpr OutStorageType OUT_MIN_NORMAL_U =
      OutFPBits::min_normal().uintval();
  static constexpr InStorageType IN_MIN_NORMAL_U =
      InFPBits::min_normal().uintval();

  OutConstants out;
  InConstants in;

  const InType in_out_min_normal =
      LIBC_NAMESPACE::fputil::cast<InType>(out.min_normal);
  const InType in_out_min_denormal =
      LIBC_NAMESPACE::fputil::cast<InType>(out.min_denormal);

public:
  using FmaFunc = OutType (*)(InType, InType, InType);

  void test_special_numbers(FmaFunc func) {
    EXPECT_FP_EQ(out.zero, func(in.zero, in.zero, in.zero));
    EXPECT_FP_EQ(out.neg_zero, func(in.zero, in.neg_zero, in.neg_zero));
    EXPECT_FP_EQ(out.inf, func(in.inf, in.inf, in.zero));
    EXPECT_FP_EQ(out.neg_inf, func(in.neg_inf, in.inf, in.neg_inf));
    EXPECT_FP_EQ(out.aNaN, func(in.inf, in.zero, in.zero));
    EXPECT_FP_EQ(out.aNaN, func(in.inf, in.neg_inf, in.inf));
    EXPECT_FP_EQ(out.aNaN, func(in.aNaN, in.zero, in.inf));
    EXPECT_FP_EQ(out.aNaN, func(in.inf, in.neg_inf, in.aNaN));

    // Test underflow rounding up.
    EXPECT_FP_EQ(OutFPBits(OutStorageType(2)).get_val(),
                 func(InType(0.5), in_out_min_denormal, in_out_min_denormal));

    if constexpr (sizeof(OutType) < sizeof(InType)) {
      EXPECT_FP_EQ(out.zero,
                   func(InType(0.5), in.min_denormal, in.min_denormal));
    }

    // Test underflow rounding down.
    OutType v = OutFPBits(static_cast<OutStorageType>(OUT_MIN_NORMAL_U +
                                                      OutStorageType(1)))
                    .get_val();
    EXPECT_FP_EQ(v, func(InType(1) / InType(OUT_MIN_NORMAL_U << 1),
                         LIBC_NAMESPACE::fputil::cast<InType>(v),
                         in_out_min_normal));

    if constexpr (sizeof(OutType) < sizeof(InType)) {
      InFPBits tmp = InFPBits::one();
      tmp.set_biased_exponent(InFPBits::EXP_BIAS - InFPBits::FRACTION_LEN - 1);
      InType reciprocal_value = tmp.get_val();

      InType v = InFPBits(static_cast<InStorageType>(IN_MIN_NORMAL_U +
                                                     InStorageType(1)))
                     .get_val();
      EXPECT_FP_EQ(out.min_normal,
                   func(reciprocal_value, v, in_out_min_normal));
    }

    // Test overflow.
    InType in_z = LIBC_NAMESPACE::fputil::cast<InType>(out.max_normal);
    EXPECT_FP_EQ_ALL_ROUNDING(func(InType(-0.25), in_z, in_z),
                              func(InType(1.75), in_z, -in_z));

    // Exact cancellation.
    EXPECT_FP_EQ_ROUNDING_NEAREST(
        out.zero, func(InType(3.0), InType(5.0), InType(-15.0)));
    EXPECT_FP_EQ_ROUNDING_UPWARD(out.zero,
                                 func(InType(3.0), InType(5.0), InType(-15.0)));
    EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO(
        out.zero, func(InType(3.0), InType(5.0), InType(-15.0)));
    EXPECT_FP_EQ_ROUNDING_DOWNWARD(
        out.neg_zero, func(InType(3.0), InType(5.0), InType(-15.0)));

    EXPECT_FP_EQ_ROUNDING_NEAREST(
        out.zero, func(InType(-3.0), InType(5.0), InType(15.0)));
    EXPECT_FP_EQ_ROUNDING_UPWARD(out.zero,
                                 func(InType(-3.0), InType(5.0), InType(15.0)));
    EXPECT_FP_EQ_ROUNDING_TOWARD_ZERO(
        out.zero, func(InType(-3.0), InType(5.0), InType(15.0)));
    EXPECT_FP_EQ_ROUNDING_DOWNWARD(
        out.neg_zero, func(InType(-3.0), InType(5.0), InType(15.0)));
  }
};

#define LIST_FMA_TESTS(T, func)                                                \
  using LlvmLibcFmaTest = FmaTestTemplate<T>;                                  \
  TEST_F(LlvmLibcFmaTest, SpecialNumbers) { test_special_numbers(&func); }

#define LIST_NARROWING_FMA_TESTS(OutType, InType, func)                        \
  using LlvmLibcFmaTest = FmaTestTemplate<OutType, InType>;                    \
  TEST_F(LlvmLibcFmaTest, SpecialNumbers) { test_special_numbers(&func); }

#endif // LLVM_LIBC_TEST_SRC_MATH_FMATEST_H