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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
//===-- MPCUtils.h ----------------------------------------------*- C++ -*-===//
//
// 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_UTILS_MPCWRAPPER_MPCUTILS_H
#define LLVM_LIBC_UTILS_MPCWRAPPER_MPCUTILS_H
#include "src/__support/CPP/type_traits.h"
#include "src/__support/complex_type.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/complex_types.h"
#include "src/__support/macros/properties/types.h"
#include "test/UnitTest/RoundingModeUtils.h"
#include "test/UnitTest/Test.h"
#include <stdint.h>
namespace LIBC_NAMESPACE_DECL {
namespace testing {
namespace mpc {
enum class Operation {
// Operations which take a single complex floating point number as input
// and produce a single floating point number as output which has the same
// floating point type as the real/imaginary part of the input.
BeginUnaryOperationsSingleOutputDifferentOutputType,
Carg,
Cabs,
EndUnaryOperationsSingleOutputDifferentOutputType,
// Operations which take a single complex floating point number as input
// and produce a single complex floating point number of the same kind
// as output.
BeginUnaryOperationsSingleOutputSameOutputType,
Cproj,
Csqrt,
Clog,
Cexp,
Csinh,
Ccosh,
Ctanh,
Casinh,
Cacosh,
Catanh,
Csin,
Ccos,
Ctan,
Casin,
Cacos,
Catan,
EndUnaryOperationsSingleOutputSameOutputType,
// Operations which take two complex floating point numbers as input
// and produce a single complex floating point number of the same kind
// as output.
BeginBinaryOperationsSingleOutput,
Cpow,
EndBinaryOperationsSingleOutput,
};
using LIBC_NAMESPACE::fputil::testing::RoundingMode;
template <typename T> struct BinaryInput {
static_assert(LIBC_NAMESPACE::cpp::is_complex_v<T>,
"Template parameter of BinaryInput must be a complex floating "
"point type.");
using Type = T;
T x, y;
};
namespace internal {
template <typename InputType, typename OutputType>
bool compare_unary_operation_single_output_same_type(Operation op,
InputType input,
OutputType libc_output,
double ulp_tolerance,
RoundingMode rounding);
template <typename InputType, typename OutputType>
bool compare_unary_operation_single_output_different_type(
Operation op, InputType input, OutputType libc_output, double ulp_tolerance,
RoundingMode rounding);
template <typename InputType, typename OutputType>
bool compare_binary_operation_one_output(Operation op,
const BinaryInput<InputType> &input,
OutputType libc_output,
double ulp_tolerance,
RoundingMode rounding);
template <typename InputType, typename OutputType>
void explain_unary_operation_single_output_same_type_error(
Operation op, InputType input, OutputType match_value, double ulp_tolerance,
RoundingMode rounding);
template <typename InputType, typename OutputType>
void explain_unary_operation_single_output_different_type_error(
Operation op, InputType input, OutputType match_value, double ulp_tolerance,
RoundingMode rounding);
template <typename InputType, typename OutputType>
void explain_binary_operation_one_output_error(
Operation op, const BinaryInput<InputType> &input, OutputType match_value,
double ulp_tolerance, RoundingMode rounding);
template <Operation op, typename InputType, typename OutputType>
class MPCMatcher : public testing::Matcher<OutputType> {
private:
InputType input;
OutputType match_value;
double ulp_tolerance;
RoundingMode rounding;
public:
MPCMatcher(InputType testInput, double ulp_tolerance, RoundingMode rounding)
: input(testInput), ulp_tolerance(ulp_tolerance), rounding(rounding) {}
bool match(OutputType libcResult) {
match_value = libcResult;
return match(input, match_value);
}
void explainError() override { // NOLINT
explain_error(input, match_value);
}
private:
template <typename InType, typename OutType>
bool match(InType in, OutType out) {
if (cpp::is_same_v<InType, OutType>) {
return compare_unary_operation_single_output_same_type(
op, in, out, ulp_tolerance, rounding);
} else {
return compare_unary_operation_single_output_different_type(
op, in, out, ulp_tolerance, rounding);
}
}
template <typename T, typename U>
bool match(const BinaryInput<T> &in, U out) {
return compare_binary_operation_one_output(op, in, out, ulp_tolerance,
rounding);
}
template <typename InType, typename OutType>
void explain_error(InType in, OutType out) {
if (cpp::is_same_v<InType, OutType>) {
explain_unary_operation_single_output_same_type_error(
op, in, out, ulp_tolerance, rounding);
} else {
explain_unary_operation_single_output_different_type_error(
op, in, out, ulp_tolerance, rounding);
}
}
template <typename T, typename U>
void explain_error(const BinaryInput<T> &in, U out) {
explain_binary_operation_one_output_error(op, in, out, ulp_tolerance,
rounding);
}
};
} // namespace internal
// Return true if the input and ouput types for the operation op are valid
// types.
template <Operation op, typename InputType, typename OutputType>
constexpr bool is_valid_operation() {
return (Operation::BeginBinaryOperationsSingleOutput < op &&
op < Operation::EndBinaryOperationsSingleOutput &&
cpp::is_complex_type_same<InputType, OutputType>() &&
cpp::is_complex_v<InputType>) ||
(Operation::BeginUnaryOperationsSingleOutputSameOutputType < op &&
op < Operation::EndUnaryOperationsSingleOutputSameOutputType &&
cpp::is_complex_type_same<InputType, OutputType>() &&
cpp::is_complex_v<InputType>) ||
(Operation::BeginUnaryOperationsSingleOutputDifferentOutputType < op &&
op < Operation::EndUnaryOperationsSingleOutputDifferentOutputType &&
cpp::is_same_v<make_real_t<InputType>, OutputType> &&
cpp::is_complex_v<InputType>);
}
template <Operation op, typename InputType, typename OutputType>
cpp::enable_if_t<is_valid_operation<op, InputType, OutputType>(),
internal::MPCMatcher<op, InputType, OutputType>>
get_mpc_matcher(InputType input, [[maybe_unused]] OutputType output,
double ulp_tolerance, RoundingMode rounding) {
return internal::MPCMatcher<op, InputType, OutputType>(input, ulp_tolerance,
rounding);
}
} // namespace mpc
} // namespace testing
} // namespace LIBC_NAMESPACE_DECL
#define EXPECT_MPC_MATCH_DEFAULT(op, input, match_value, ulp_tolerance) \
EXPECT_THAT(match_value, \
LIBC_NAMESPACE::testing::mpc::get_mpc_matcher<op>( \
input, match_value, ulp_tolerance, \
LIBC_NAMESPACE::fputil::testing::RoundingMode::Nearest))
#define EXPECT_MPC_MATCH_ROUNDING(op, input, match_value, ulp_tolerance, \
rounding) \
EXPECT_THAT(match_value, LIBC_NAMESPACE::testing::mpc::get_mpc_matcher<op>( \
input, match_value, ulp_tolerance, rounding))
#define EXPECT_MPC_MATCH_ALL_ROUNDING_HELPER(op, input, match_value, \
ulp_tolerance, rounding) \
{ \
MPCRND::ForceRoundingMode __r(rounding); \
if (__r.success) { \
EXPECT_MPC_MATCH_ROUNDING(op, input, match_value, ulp_tolerance, \
rounding); \
} \
}
#define EXPECT_MPC_MATCH_ALL_ROUNDING(op, input, match_value, ulp_tolerance) \
{ \
namespace MPCRND = LIBC_NAMESPACE::fputil::testing; \
for (int i = 0; i < 4; i++) { \
MPCRND::RoundingMode r_mode = static_cast<MPCRND::RoundingMode>(i); \
EXPECT_MPC_MATCH_ALL_ROUNDING_HELPER(op, input, match_value, \
ulp_tolerance, r_mode); \
} \
}
#define TEST_MPC_MATCH_ROUNDING(op, input, match_value, ulp_tolerance, \
rounding) \
LIBC_NAMESPACE::testing::mpc::get_mpc_matcher<op>(input, match_value, \
ulp_tolerance, rounding) \
.match(match_value)
#define ASSERT_MPC_MATCH_DEFAULT(op, input, match_value, ulp_tolerance) \
ASSERT_THAT(match_value, \
LIBC_NAMESPACE::testing::mpc::get_mpc_matcher<op>( \
input, match_value, ulp_tolerance, \
LIBC_NAMESPACE::fputil::testing::RoundingMode::Nearest))
#define ASSERT_MPC_MATCH_ROUNDING(op, input, match_value, ulp_tolerance, \
rounding) \
ASSERT_THAT(match_value, LIBC_NAMESPACE::testing::mpc::get_mpc_matcher<op>( \
input, match_value, ulp_tolerance, rounding))
#define ASSERT_MPC_MATCH_ALL_ROUNDING_HELPER(op, input, match_value, \
ulp_tolerance, rounding) \
{ \
MPCRND::ForceRoundingMode __r(rounding); \
if (__r.success) { \
ASSERT_MPC_MATCH_ROUNDING(op, input, match_value, ulp_tolerance, \
rounding); \
} \
}
#define ASSERT_MPC_MATCH_ALL_ROUNDING(op, input, match_value, ulp_tolerance) \
{ \
namespace MPCRND = LIBC_NAMESPACE::fputil::testing; \
for (int i = 0; i < 4; i++) { \
MPCRND::RoundingMode r_mode = static_cast<MPCRND::RoundingMode>(i); \
ASSERT_MPC_MATCH_ALL_ROUNDING_HELPER(op, input, match_value, \
ulp_tolerance, r_mode); \
} \
}
#endif // LLVM_LIBC_UTILS_MPCWRAPPER_MPCUTILS_H
|