22 changes: 13 additions & 9 deletions libc/test/src/math/TruncTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_TEST_SRC_MATH_TRUNCTEST_H
#define LLVM_LIBC_TEST_SRC_MATH_TRUNCTEST_H

#include "src/__support/CPP/algorithm.h"
#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
Expand Down Expand Up @@ -62,18 +63,21 @@ class TruncTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
EXPECT_FP_EQ(T(-10.0), func(T(-10.32)));
EXPECT_FP_EQ(T(10.0), func(T(10.65)));
EXPECT_FP_EQ(T(-10.0), func(T(-10.65)));
EXPECT_FP_EQ(T(1234.0), func(T(1234.38)));
EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38)));
EXPECT_FP_EQ(T(1234.0), func(T(1234.96)));
EXPECT_FP_EQ(T(-1234.0), func(T(-1234.96)));
EXPECT_FP_EQ(T(123.0), func(T(123.38)));
EXPECT_FP_EQ(T(-123.0), func(T(-123.38)));
EXPECT_FP_EQ(T(123.0), func(T(123.96)));
EXPECT_FP_EQ(T(-123.0), func(T(-123.96)));
}

void testRange(TruncFunc func) {
constexpr StorageType COUNT = 100'000;
constexpr StorageType STEP = STORAGE_MAX / COUNT;
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
T x = FPBits(v).get_val();
if (isnan(x) || isinf(x))
constexpr int COUNT = 100'000;
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
StorageType v = 0;
for (int i = 0; i <= COUNT; ++i, v += STEP) {
FPBits xbits(v);
T x = xbits.get_val();
if (xbits.is_inf_or_nan())
continue;

ASSERT_MPFR_MATCH(mpfr::Operation::Trunc, x, func(x), 0.0);
Expand Down
13 changes: 13 additions & 0 deletions libc/test/src/math/ceilf16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for ceilf16 ---------------------------------------------===//
//
// 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 "CeilTest.h"

#include "src/math/ceilf16.h"

LIST_CEIL_TESTS(float16, LIBC_NAMESPACE::ceilf16)
13 changes: 13 additions & 0 deletions libc/test/src/math/floorf16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for floorf16 --------------------------------------------===//
//
// 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 "FloorTest.h"

#include "src/math/floorf16.h"

LIST_FLOOR_TESTS(float16, LIBC_NAMESPACE::floorf16)
13 changes: 13 additions & 0 deletions libc/test/src/math/roundevenf16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for roundevenf16 ----------------------------------------===//
//
// 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 "RoundEvenTest.h"

#include "src/math/roundevenf16.h"

LIST_ROUNDEVEN_TESTS(float16, LIBC_NAMESPACE::roundevenf16)
13 changes: 13 additions & 0 deletions libc/test/src/math/roundf16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for roundf16 --------------------------------------------===//
//
// 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 "RoundTest.h"

#include "src/math/roundf16.h"

LIST_ROUND_TESTS(float16, LIBC_NAMESPACE::roundf16)
13 changes: 13 additions & 0 deletions libc/test/src/math/truncf16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-- Unittests for truncf16 --------------------------------------------===//
//
// 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 "TruncTest.h"

#include "src/math/truncf16.h"

LIST_TRUNC_TESTS(float16, LIBC_NAMESPACE::truncf16)
39 changes: 35 additions & 4 deletions libc/utils/MPFRWrapper/MPFRUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "src/__support/CPP/string_view.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/fpbits_str.h"
#include "src/__support/macros/properties/types.h"
#include "test/UnitTest/FPMatcher.h"

#include "hdr/math_macros.h"
Expand All @@ -30,6 +31,12 @@ namespace mpfr {
// precision compared to the floating point precision.
template <typename T> struct ExtraPrecision;

#ifdef LIBC_TYPES_HAS_FLOAT16
template <> struct ExtraPrecision<float16> {
static constexpr unsigned int VALUE = 128;
};
#endif

template <> struct ExtraPrecision<float> {
static constexpr unsigned int VALUE = 128;
};
Expand Down Expand Up @@ -85,9 +92,16 @@ class MPFRNumber {

// We use explicit EnableIf specializations to disallow implicit
// conversions. Implicit conversions can potentially lead to loss of
// precision.
// precision. We exceptionally allow implicit conversions from float16
// to float, as the MPFR API does not support float16, thus requiring
// conversion to a higher-precision format.
template <typename XType,
cpp::enable_if_t<cpp::is_same_v<float, XType>, int> = 0>
cpp::enable_if_t<cpp::is_same_v<float, XType>
#ifdef LIBC_TYPES_HAS_FLOAT16
|| cpp::is_same_v<float16, XType>
#endif
,
int> = 0>
explicit MPFRNumber(XType x,
unsigned int precision = ExtraPrecision<XType>::VALUE,
RoundingMode rounding = RoundingMode::Nearest)
Expand Down Expand Up @@ -529,8 +543,8 @@ class MPFRNumber {
// If the control reaches here, it means that this number and input are
// of the same sign but different exponent. In such a case, ULP error is
// calculated as sum of two parts.
thisAsT = std::abs(thisAsT);
input = std::abs(input);
thisAsT = FPBits<T>(thisAsT).abs().get_val();
input = FPBits<T>(input).abs().get_val();
T min = thisAsT > input ? input : thisAsT;
T max = thisAsT > input ? thisAsT : input;
int minExponent = FPBits<T>(min).get_exponent();
Expand Down Expand Up @@ -585,6 +599,14 @@ template <> long double MPFRNumber::as<long double>() const {
return mpfr_get_ld(value, mpfr_rounding);
}

#ifdef LIBC_TYPES_HAS_FLOAT16
template <> float16 MPFRNumber::as<float16>() const {
// TODO: Either prove that this cast won't cause double-rounding errors, or
// find a better way to get a float16.
return static_cast<float16>(mpfr_get_d(value, mpfr_rounding));
}
#endif

namespace internal {

template <typename InputType>
Expand Down Expand Up @@ -763,6 +785,10 @@ template void explain_unary_operation_single_output_error<double>(
Operation op, double, double, double, RoundingMode);
template void explain_unary_operation_single_output_error<long double>(
Operation op, long double, long double, double, RoundingMode);
#ifdef LIBC_TYPES_HAS_FLOAT16
template void explain_unary_operation_single_output_error<float16>(
Operation op, float16, float16, double, RoundingMode);
#endif

template <typename T>
void explain_unary_operation_two_outputs_error(
Expand Down Expand Up @@ -942,6 +968,11 @@ template bool compare_unary_operation_single_output<double>(Operation, double,
RoundingMode);
template bool compare_unary_operation_single_output<long double>(
Operation, long double, long double, double, RoundingMode);
#ifdef LIBC_TYPES_HAS_FLOAT16
template bool compare_unary_operation_single_output<float16>(Operation, float16,
float16, double,
RoundingMode);
#endif

template <typename T>
bool compare_unary_operation_two_outputs(Operation op, T input,
Expand Down