aboutsummaryrefslogtreecommitdiff
path: root/libc/test/src/math/FmaTest.h
blob: 902ff0413b0fa9ee033f6d97cfbc7efa7e20daed (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
//===-- 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/stdlib/rand.h"
#include "src/stdlib/srand.h"
#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include "utils/MPFRWrapper/MPFRUtils.h"

namespace mpfr = LIBC_NAMESPACE::testing::mpfr;

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_MAX_NORMAL_U =
      InFPBits::max_normal().uintval();
  static constexpr InStorageType IN_MIN_NORMAL_U =
      InFPBits::min_normal().uintval();
  static constexpr InStorageType IN_MAX_SUBNORMAL_U =
      InFPBits::max_subnormal().uintval();
  static constexpr InStorageType IN_MIN_SUBNORMAL_U =
      InFPBits::min_subnormal().uintval();

  InStorageType get_random_bit_pattern() {
    InStorageType bits{0};
    for (InStorageType i = 0; i < sizeof(InStorageType) / 2; ++i) {
      bits = static_cast<InStorageType>(
          (bits << 2) + static_cast<uint16_t>(LIBC_NAMESPACE::rand()));
    }
    return bits;
  }

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

  void test_subnormal_range(FmaFunc func) {
    constexpr InStorageType COUNT = 10'001;
    constexpr InStorageType RAW_STEP =
        (IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) / COUNT;
    constexpr InStorageType STEP = (RAW_STEP == 0 ? 1 : RAW_STEP);
    LIBC_NAMESPACE::srand(1);
    for (InStorageType v = IN_MIN_SUBNORMAL_U, w = IN_MAX_SUBNORMAL_U;
         v <= IN_MAX_SUBNORMAL_U && w >= IN_MIN_SUBNORMAL_U;
         v += STEP, w -= STEP) {
      InType x = InFPBits(get_random_bit_pattern()).get_val();
      InType y = InFPBits(v).get_val();
      InType z = InFPBits(w).get_val();
      mpfr::TernaryInput<InType> input{x, y, z};
      ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Fma, input, func(x, y, z),
                                     0.5);
    }
  }

  void test_normal_range(FmaFunc func) {
    constexpr InStorageType COUNT = 10'001;
    constexpr InStorageType RAW_STEP =
        (IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT;
    constexpr InStorageType STEP = (RAW_STEP == 0 ? 1 : RAW_STEP);
    LIBC_NAMESPACE::srand(1);
    for (InStorageType v = IN_MIN_NORMAL_U, w = IN_MAX_NORMAL_U;
         v <= IN_MAX_NORMAL_U && w >= IN_MIN_NORMAL_U; v += STEP, w -= STEP) {
      InType x = InFPBits(v).get_val();
      InType y = InFPBits(w).get_val();
      InType z = InFPBits(get_random_bit_pattern()).get_val();
      mpfr::TernaryInput<InType> input{x, y, z};
      ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Fma, input, func(x, y, z),
                                     0.5);
    }
  }
};

#define LIST_FMA_TESTS(T, func)                                                \
  using LlvmLibcFmaTest = FmaTestTemplate<T>;                                  \
  TEST_F(LlvmLibcFmaTest, SubnormalRange) { test_subnormal_range(&func); }     \
  TEST_F(LlvmLibcFmaTest, NormalRange) { test_normal_range(&func); }

#define LIST_NARROWING_FMA_TESTS(OutType, InType, func)                        \
  using LlvmLibcFmaTest = FmaTestTemplate<OutType, InType>;                    \
  TEST_F(LlvmLibcFmaTest, SubnormalRange) { test_subnormal_range(&func); }     \
  TEST_F(LlvmLibcFmaTest, NormalRange) { test_normal_range(&func); }

#endif // LLVM_LIBC_TEST_SRC_MATH_FMATEST_H