Skip to content

Commit 8c29f63

Browse files
committed
[libc][math][c23] Update MPFRUtils.cpp from branch libc-math-ceilf16-mpfr-tests
1 parent 69754e6 commit 8c29f63

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

libc/utils/MPFRWrapper/MPFRUtils.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ namespace mpfr {
3131
// precision compared to the floating point precision.
3232
template <typename T> struct ExtraPrecision;
3333

34+
#ifdef LIBC_TYPES_HAS_FLOAT16
3435
template <> struct ExtraPrecision<float16> {
3536
static constexpr unsigned int VALUE = 128;
3637
};
38+
#endif
3739

3840
template <> struct ExtraPrecision<float> {
3941
static constexpr unsigned int VALUE = 128;
@@ -93,9 +95,13 @@ class MPFRNumber {
9395
// precision. We exceptionally allow implicit conversions from float16
9496
// to float, as the MPFR API does not support float16, thus requiring
9597
// conversion to a higher-precision format.
96-
template <typename XType, cpp::enable_if_t<cpp::is_same_v<float, XType> ||
97-
cpp::is_same_v<float16, XType>,
98-
int> = 0>
98+
template <typename XType,
99+
cpp::enable_if_t<cpp::is_same_v<float, XType>
100+
#ifdef LIBC_TYPES_HAS_FLOAT16
101+
|| cpp::is_same_v<float16, XType>
102+
#endif
103+
,
104+
int> = 0>
99105
explicit MPFRNumber(XType x,
100106
unsigned int precision = ExtraPrecision<XType>::VALUE,
101107
RoundingMode rounding = RoundingMode::Nearest)
@@ -537,7 +543,12 @@ class MPFRNumber {
537543
// If the control reaches here, it means that this number and input are
538544
// of the same sign but different exponent. In such a case, ULP error is
539545
// calculated as sum of two parts.
546+
#ifdef LIBC_TYPES_HAS_FLOAT16
547+
// TODO: This will no longer be needed once std::abs supports float16.
540548
using U = cpp::conditional_t<cpp::is_same_v<T, float16>, float, T>;
549+
#else
550+
using U = T;
551+
#endif
541552
thisAsT = std::abs(static_cast<U>(thisAsT));
542553
input = std::abs(static_cast<U>(input));
543554
T min = thisAsT > input ? input : thisAsT;
@@ -594,9 +605,13 @@ template <> long double MPFRNumber::as<long double>() const {
594605
return mpfr_get_ld(value, mpfr_rounding);
595606
}
596607

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

601616
namespace internal {
602617

@@ -776,8 +791,10 @@ template void explain_unary_operation_single_output_error<double>(
776791
Operation op, double, double, double, RoundingMode);
777792
template void explain_unary_operation_single_output_error<long double>(
778793
Operation op, long double, long double, double, RoundingMode);
794+
#ifdef LIBC_TYPES_HAS_FLOAT16
779795
template void explain_unary_operation_single_output_error<float16>(
780796
Operation op, float16, float16, double, RoundingMode);
797+
#endif
781798

782799
template <typename T>
783800
void explain_unary_operation_two_outputs_error(
@@ -957,9 +974,11 @@ template bool compare_unary_operation_single_output<double>(Operation, double,
957974
RoundingMode);
958975
template bool compare_unary_operation_single_output<long double>(
959976
Operation, long double, long double, double, RoundingMode);
977+
#ifdef LIBC_TYPES_HAS_FLOAT16
960978
template bool compare_unary_operation_single_output<float16>(Operation, float16,
961979
float16, double,
962980
RoundingMode);
981+
#endif
963982

964983
template <typename T>
965984
bool compare_unary_operation_two_outputs(Operation op, T input,
@@ -1091,7 +1110,6 @@ template <typename T> T round(T x, RoundingMode mode) {
10911110
template float round<float>(float, RoundingMode);
10921111
template double round<double>(double, RoundingMode);
10931112
template long double round<long double>(long double, RoundingMode);
1094-
template float16 round<float16>(float16, RoundingMode);
10951113

10961114
} // namespace mpfr
10971115
} // namespace testing

0 commit comments

Comments
 (0)