74 changes: 74 additions & 0 deletions libc/test/src/math/DivTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//===-- Utility class to test different flavors of float div ----*- 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_TEST_SRC_MATH_DIVTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_DIVTEST_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>
class DivTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {

struct InConstants {
DECLARE_SPECIAL_CONSTANTS(InType)
};

using InFPBits = typename InConstants::FPBits;
using InStorageType = typename InConstants::StorageType;

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();

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

void test_subnormal_range(DivFunc func) {
constexpr InStorageType COUNT = 100'001;
constexpr InStorageType STEP =
(IN_MAX_SUBNORMAL_U - IN_MIN_SUBNORMAL_U) / COUNT;
for (InStorageType i = 0, v = 0, w = IN_MAX_SUBNORMAL_U; i <= COUNT;
++i, v += STEP, w -= STEP) {
InType x = InFPBits(v).get_val();
InType y = InFPBits(w).get_val();
mpfr::BinaryInput<InType> input{x, y};
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Div, input, func(x, y),
0.5);
}
}

void test_normal_range(DivFunc func) {
constexpr InStorageType COUNT = 100'001;
constexpr InStorageType STEP = (IN_MAX_NORMAL_U - IN_MIN_NORMAL_U) / COUNT;
for (InStorageType i = 0, v = 0, w = IN_MAX_NORMAL_U; i <= COUNT;
++i, v += STEP, w -= STEP) {
InType x = InFPBits(v).get_val();
InType y = InFPBits(w).get_val();
mpfr::BinaryInput<InType> input{x, y};
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Div, input, func(x, y),
0.5);
}
}
};

#define LIST_DIV_TESTS(OutType, InType, func) \
using LlvmLibcDivTest = DivTest<OutType, InType>; \
TEST_F(LlvmLibcDivTest, SubnormalRange) { test_subnormal_range(&func); } \
TEST_F(LlvmLibcDivTest, NormalRange) { test_normal_range(&func); }

#endif // LLVM_LIBC_TEST_SRC_MATH_DIVTEST_H
13 changes: 13 additions & 0 deletions libc/test/src/math/f16divf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for f16divf ---------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "DivTest.h"

#include "src/math/f16divf.h"

LIST_DIV_TESTS(float16, float, LIBC_NAMESPACE::f16divf)
14 changes: 14 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3630,6 +3630,20 @@ add_fp_unittest(
libc.src.math.setpayloadsigf16
)

add_fp_unittest(
f16divf_test
SUITE
libc-math-smoke-tests
SRCS
f16divf_test.cpp
HDRS
DivTest.h
DEPENDS
libc.hdr.fenv_macros
libc.src.__support.FPUtil.basic_operations
libc.src.math.f16divf
)

add_fp_unittest(
f16fmaf_test
SUITE
Expand Down
171 changes: 171 additions & 0 deletions libc/test/src/math/smoke/DivTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
//===-- Utility class to test different flavors of float div --------------===//
//
// 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_SMOKE_DIVTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_DIVTEST_H

#include "hdr/fenv_macros.h"
#include "src/__support/FPUtil/BasicOperations.h"
#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/RoundingModeUtils.h"
#include "test/UnitTest/Test.h"

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

DECLARE_SPECIAL_CONSTANTS(OutType)

struct InConstants {
DECLARE_SPECIAL_CONSTANTS(InType)
};

using InFPBits = typename InConstants::FPBits;
using InStorageType = typename InConstants::StorageType;

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

void test_special_numbers(DivFunc func) {
EXPECT_FP_IS_NAN(func(aNaN, aNaN));
EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(sNaN, sNaN), FE_INVALID);

InType qnan_42 = InFPBits::quiet_nan(Sign::POS, 0x42).get_val();
EXPECT_FP_EQ(InType(0x42.0p+0),
LIBC_NAMESPACE::fputil::getpayload(func(qnan_42, zero)));
EXPECT_FP_EQ(InType(0x42.0p+0),
LIBC_NAMESPACE::fputil::getpayload(func(zero, qnan_42)));

if constexpr (sizeof(OutType) < sizeof(InType)) {
InStorageType max_payload = InFPBits::FRACTION_MASK >> 1;
InType qnan_max = InFPBits::quiet_nan(Sign::POS, max_payload).get_val();
EXPECT_FP_EQ(zero,
LIBC_NAMESPACE::fputil::getpayload(func(qnan_max, zero)));
EXPECT_FP_EQ(zero,
LIBC_NAMESPACE::fputil::getpayload(func(zero, qnan_max)));
EXPECT_FP_EQ(InType(0x42.0p+0),
LIBC_NAMESPACE::fputil::getpayload(func(qnan_max, qnan_42)));
EXPECT_FP_EQ(InType(0x42.0p+0),
LIBC_NAMESPACE::fputil::getpayload(func(qnan_42, qnan_max)));
}

EXPECT_FP_EQ(inf, func(inf, zero));
EXPECT_FP_EQ(neg_inf, func(neg_inf, zero));
EXPECT_FP_EQ(neg_inf, func(inf, neg_zero));
EXPECT_FP_EQ(inf, func(neg_inf, neg_zero));
}

void test_division_by_zero(DivFunc func) {
EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(InType(1.0), zero), FE_DIVBYZERO);
EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, func(InType(-1.0), zero),
FE_DIVBYZERO);
EXPECT_FP_EQ_WITH_EXCEPTION(neg_inf, func(InType(1.0), neg_zero),
FE_DIVBYZERO);
EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(InType(1.0), zero), FE_DIVBYZERO);
}

void test_invalid_operations(DivFunc func) {
EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(zero, zero), FE_INVALID);
EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(neg_zero, zero), FE_INVALID);
EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(zero, neg_zero), FE_INVALID);
EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(neg_zero, neg_zero), FE_INVALID);

EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(inf, inf), FE_INVALID);
EXPECT_MATH_ERRNO(EDOM);
EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(neg_inf, inf), FE_INVALID);
EXPECT_MATH_ERRNO(EDOM);
EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(inf, neg_inf), FE_INVALID);
EXPECT_MATH_ERRNO(EDOM);
EXPECT_FP_IS_NAN_WITH_EXCEPTION(func(neg_inf, neg_inf), FE_INVALID);
EXPECT_MATH_ERRNO(EDOM);
}

void test_range_errors(DivFunc func) {
using namespace LIBC_NAMESPACE::fputil::testing;

if (ForceRoundingMode r(RoundingMode::Nearest); r.success) {
EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(max_normal, min_normal),
FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION(-inf, func(neg_max_normal, min_denormal),
FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);

EXPECT_FP_EQ_WITH_EXCEPTION(zero, func(min_denormal, max_normal),
FE_UNDERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION(neg_zero, func(neg_min_denormal, max_normal),
FE_UNDERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);
}

if (ForceRoundingMode r(RoundingMode::TowardZero); r.success) {
EXPECT_FP_EQ_WITH_EXCEPTION(max_normal, func(max_normal, min_normal),
FE_OVERFLOW | FE_INEXACT);
EXPECT_FP_EQ_WITH_EXCEPTION(neg_max_normal,
func(neg_max_normal, min_denormal),
FE_OVERFLOW | FE_INEXACT);

EXPECT_FP_EQ_WITH_EXCEPTION(zero, func(min_denormal, max_normal),
FE_UNDERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION(neg_zero, func(neg_min_denormal, max_normal),
FE_UNDERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);
}

if (ForceRoundingMode r(RoundingMode::Downward); r.success) {
EXPECT_FP_EQ_WITH_EXCEPTION(max_normal, func(max_normal, min_normal),
FE_OVERFLOW | FE_INEXACT);
EXPECT_FP_EQ_WITH_EXCEPTION(-inf, func(neg_max_normal, min_denormal),
FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);

EXPECT_FP_EQ_WITH_EXCEPTION(zero, func(min_denormal, max_normal),
FE_UNDERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION(neg_min_denormal,
func(neg_min_denormal, max_normal),
FE_UNDERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);
}

if (ForceRoundingMode r(RoundingMode::Upward); r.success) {
EXPECT_FP_EQ_WITH_EXCEPTION(inf, func(max_normal, min_normal),
FE_OVERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION(neg_max_normal,
func(neg_max_normal, min_denormal),
FE_OVERFLOW | FE_INEXACT);

EXPECT_FP_EQ_WITH_EXCEPTION(min_denormal, func(min_denormal, max_normal),
FE_UNDERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);
EXPECT_FP_EQ_WITH_EXCEPTION(neg_zero, func(neg_min_denormal, max_normal),
FE_UNDERFLOW | FE_INEXACT);
EXPECT_MATH_ERRNO(ERANGE);
}
}

void test_inexact_results(DivFunc func) {
func(InType(1.0), InType(3.0));
EXPECT_FP_EXCEPTION(FE_INEXACT);
}
};

#define LIST_DIV_TESTS(OutType, InType, func) \
using LlvmLibcDivTest = DivTest<OutType, InType>; \
TEST_F(LlvmLibcDivTest, SpecialNumbers) { test_special_numbers(&func); } \
TEST_F(LlvmLibcDivTest, DivisionByZero) { test_division_by_zero(&func); } \
TEST_F(LlvmLibcDivTest, InvalidOperations) { \
test_invalid_operations(&func); \
} \
TEST_F(LlvmLibcDivTest, RangeErrors) { test_range_errors(&func); } \
TEST_F(LlvmLibcDivTest, InexactResults) { test_inexact_results(&func); }

#endif // LLVM_LIBC_TEST_SRC_MATH_SMOKE_DIVTEST_H
13 changes: 13 additions & 0 deletions libc/test/src/math/smoke/f16divf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for f16divf ---------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "DivTest.h"

#include "src/math/f16divf.h"

LIST_DIV_TESTS(float16, float, LIBC_NAMESPACE::f16divf)
84 changes: 53 additions & 31 deletions libc/utils/MPFRWrapper/MPFRUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ class MPFRNumber {
return result;
}

MPFRNumber div(const MPFRNumber &b) const {
MPFRNumber result(*this);
mpfr_div(result.value, value, b.value, mpfr_rounding);
return result;
}

MPFRNumber floor() const {
MPFRNumber result(*this);
mpfr_floor(result.value, value);
Expand Down Expand Up @@ -708,6 +714,8 @@ binary_operation_one_output(Operation op, InputType x, InputType y,
switch (op) {
case Operation::Atan2:
return inputX.atan2(inputY);
case Operation::Div:
return inputX.div(inputY);
case Operation::Fmod:
return inputX.fmod(inputY);
case Operation::Hypot:
Expand Down Expand Up @@ -885,42 +893,47 @@ template void explain_binary_operation_two_outputs_error<long double>(
Operation, const BinaryInput<long double> &,
const BinaryOutput<long double> &, double, RoundingMode);

template <typename T>
void explain_binary_operation_one_output_error(Operation op,
const BinaryInput<T> &input,
T libc_result,
double ulp_tolerance,
RoundingMode rounding) {
unsigned int precision = get_precision<T>(ulp_tolerance);
template <typename InputType, typename OutputType>
void explain_binary_operation_one_output_error(
Operation op, const BinaryInput<InputType> &input, OutputType libc_result,
double ulp_tolerance, RoundingMode rounding) {
unsigned int precision = get_precision<InputType>(ulp_tolerance);
MPFRNumber mpfrX(input.x, precision);
MPFRNumber mpfrY(input.y, precision);
FPBits<T> xbits(input.x);
FPBits<T> ybits(input.y);
FPBits<InputType> xbits(input.x);
FPBits<InputType> ybits(input.y);
MPFRNumber mpfr_result =
binary_operation_one_output(op, input.x, input.y, precision, rounding);
MPFRNumber mpfrMatchValue(libc_result);

tlog << "Input decimal: x: " << mpfrX.str() << " y: " << mpfrY.str() << '\n';
tlog << "First input bits: " << str(FPBits<T>(input.x)) << '\n';
tlog << "Second input bits: " << str(FPBits<T>(input.y)) << '\n';
tlog << "First input bits: " << str(FPBits<InputType>(input.x)) << '\n';
tlog << "Second input bits: " << str(FPBits<InputType>(input.y)) << '\n';

tlog << "Libc result: " << mpfrMatchValue.str() << '\n'
<< "MPFR result: " << mpfr_result.str() << '\n';
tlog << "Libc floating point result bits: " << str(FPBits<T>(libc_result))
<< '\n';
tlog << "Libc floating point result bits: "
<< str(FPBits<OutputType>(libc_result)) << '\n';
tlog << " MPFR rounded bits: "
<< str(FPBits<T>(mpfr_result.as<T>())) << '\n';
<< str(FPBits<OutputType>(mpfr_result.as<OutputType>())) << '\n';
tlog << "ULP error: " << mpfr_result.ulp_as_mpfr_number(libc_result).str()
<< '\n';
}

template void explain_binary_operation_one_output_error<float>(
Operation, const BinaryInput<float> &, float, double, RoundingMode);
template void explain_binary_operation_one_output_error<double>(
template void
explain_binary_operation_one_output_error(Operation, const BinaryInput<float> &,
float, double, RoundingMode);
template void explain_binary_operation_one_output_error(
Operation, const BinaryInput<double> &, double, double, RoundingMode);
template void explain_binary_operation_one_output_error<long double>(
Operation, const BinaryInput<long double> &, long double, double,
RoundingMode);
template void
explain_binary_operation_one_output_error(Operation,
const BinaryInput<long double> &,
long double, double, RoundingMode);
#ifdef LIBC_TYPES_HAS_FLOAT16
template void
explain_binary_operation_one_output_error(Operation, const BinaryInput<float> &,
float16, double, RoundingMode);
#endif

template <typename InputType, typename OutputType>
void explain_ternary_operation_one_output_error(
Expand Down Expand Up @@ -1051,26 +1064,35 @@ template bool compare_binary_operation_two_outputs<long double>(
Operation, const BinaryInput<long double> &,
const BinaryOutput<long double> &, double, RoundingMode);

template <typename T>
template <typename InputType, typename OutputType>
bool compare_binary_operation_one_output(Operation op,
const BinaryInput<T> &input,
T libc_result, double ulp_tolerance,
const BinaryInput<InputType> &input,
OutputType libc_result,
double ulp_tolerance,
RoundingMode rounding) {
unsigned int precision = get_precision<T>(ulp_tolerance);
unsigned int precision = get_precision<InputType>(ulp_tolerance);
MPFRNumber mpfr_result =
binary_operation_one_output(op, input.x, input.y, precision, rounding);
double ulp = mpfr_result.ulp(libc_result);

return (ulp <= ulp_tolerance);
}

template bool compare_binary_operation_one_output<float>(
Operation, const BinaryInput<float> &, float, double, RoundingMode);
template bool compare_binary_operation_one_output<double>(
Operation, const BinaryInput<double> &, double, double, RoundingMode);
template bool compare_binary_operation_one_output<long double>(
Operation, const BinaryInput<long double> &, long double, double,
RoundingMode);
template bool compare_binary_operation_one_output(Operation,
const BinaryInput<float> &,
float, double, RoundingMode);
template bool compare_binary_operation_one_output(Operation,
const BinaryInput<double> &,
double, double, RoundingMode);
template bool
compare_binary_operation_one_output(Operation, const BinaryInput<long double> &,
long double, double, RoundingMode);
#ifdef LIBC_TYPES_HAS_FLOAT16
template bool compare_binary_operation_one_output(Operation,
const BinaryInput<float> &,
float16, double,
RoundingMode);
#endif

template <typename InputType, typename OutputType>
bool compare_ternary_operation_one_output(Operation op,
Expand Down
39 changes: 28 additions & 11 deletions libc/utils/MPFRWrapper/MPFRUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ enum class Operation : int {
// output.
BeginBinaryOperationsSingleOutput,
Atan2,
Div,
Fmod,
Hypot,
Pow,
Expand Down Expand Up @@ -129,6 +130,14 @@ struct AreMatchingBinaryInputAndBinaryOutput<BinaryInput<T>, BinaryOutput<T>> {
static constexpr bool VALUE = cpp::is_floating_point_v<T>;
};

template <typename T> struct IsBinaryInput {
static constexpr bool VALUE = false;
};

template <typename T> struct IsBinaryInput<BinaryInput<T>> {
static constexpr bool VALUE = true;
};

template <typename T> struct IsTernaryInput {
static constexpr bool VALUE = false;
};
Expand All @@ -139,6 +148,9 @@ template <typename T> struct IsTernaryInput<TernaryInput<T>> {

template <typename T> struct MakeScalarInput : cpp::type_identity<T> {};

template <typename T>
struct MakeScalarInput<BinaryInput<T>> : cpp::type_identity<T> {};

template <typename T>
struct MakeScalarInput<TernaryInput<T>> : cpp::type_identity<T> {};

Expand All @@ -159,10 +171,11 @@ bool compare_binary_operation_two_outputs(Operation op,
double ulp_tolerance,
RoundingMode rounding);

template <typename T>
template <typename InputType, typename OutputType>
bool compare_binary_operation_one_output(Operation op,
const BinaryInput<T> &input,
T libc_output, double ulp_tolerance,
const BinaryInput<InputType> &input,
OutputType libc_output,
double ulp_tolerance,
RoundingMode rounding);

template <typename InputType, typename OutputType>
Expand All @@ -187,12 +200,10 @@ void explain_binary_operation_two_outputs_error(
const BinaryOutput<T> &match_value, double ulp_tolerance,
RoundingMode rounding);

template <typename T>
void explain_binary_operation_one_output_error(Operation op,
const BinaryInput<T> &input,
T 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 <typename InputType, typename OutputType>
void explain_ternary_operation_one_output_error(
Expand Down Expand Up @@ -235,7 +246,8 @@ class MPFRMatcher : public testing::Matcher<OutputType> {
rounding);
}

template <typename T> bool match(const BinaryInput<T> &in, T out) {
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);
}
Expand Down Expand Up @@ -268,7 +280,8 @@ class MPFRMatcher : public testing::Matcher<OutputType> {
rounding);
}

template <typename T> void explain_error(const BinaryInput<T> &in, T out) {
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);
}
Expand All @@ -290,6 +303,10 @@ constexpr bool is_valid_operation() {
(op == Operation::Sqrt && cpp::is_floating_point_v<InputType> &&
cpp::is_floating_point_v<OutputType> &&
sizeof(OutputType) <= sizeof(InputType)) ||
(op == Operation::Div && internal::IsBinaryInput<InputType>::VALUE &&
cpp::is_floating_point_v<
typename internal::MakeScalarInput<InputType>::type> &&
cpp::is_floating_point_v<OutputType>) ||
(op == Operation::Fma && internal::IsTernaryInput<InputType>::VALUE &&
cpp::is_floating_point_v<
typename internal::MakeScalarInput<InputType>::type> &&
Expand Down