Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/src/__support/FPUtil/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ add_header_library(
Hypot.h
DEPENDS
.basic_operations
.cast
.fenv_impl
.fp_bits
.rounding_mode
Expand Down
15 changes: 13 additions & 2 deletions libc/src/__support/FPUtil/Hypot.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "BasicOperations.h"
#include "FEnvImpl.h"
#include "FPBits.h"
#include "cast.h"
#include "rounding_mode.h"
#include "src/__support/CPP/bit.h"
#include "src/__support/CPP/type_traits.h"
Expand Down Expand Up @@ -133,8 +134,18 @@ LIBC_INLINE T hypot(T x, T y) {
uint16_t a_exp = a_bits.get_biased_exponent();
uint16_t b_exp = b_bits.get_biased_exponent();

if ((a_exp - b_exp >= FPBits_t::FRACTION_LEN + 2) || (x == 0) || (y == 0))
return x_abs.get_val() + y_abs.get_val();
if ((a_exp - b_exp >= FPBits_t::FRACTION_LEN + 2) || (x == 0) || (y == 0)) {
#ifdef LIBC_TYPES_HAS_FLOAT16
if constexpr (cpp::is_same_v<T, float16>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails on Windows, which doesn't have float16 type, so you may need to additionally gate it with float16 availability somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

// Compiler runtime for basic operations of float16 might not be correctly
// rounded for all rounding modes.
float af = fputil::cast<float>(x_abs.get_val());
float bf = fputil::cast<float>(y_abs.get_val());
return fputil::cast<float16>(af + bf);
} else
#endif // LIBC_TYPES_HAS_FLOAT16
return x_abs.get_val() + y_abs.get_val();
}

uint64_t out_exp = a_exp;
StorageType a_mant = a_bits.get_mantissa();
Expand Down
13 changes: 6 additions & 7 deletions libc/src/math/generic/hypotf16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,15 @@ LLVM_LIBC_FUNCTION(float16, hypotf16, (float16 x, float16 y)) {
return a_bits.get_val();
}

// TODO: Investigate why replacing the return line below with:
// return x_bits.get_val() + y_bits.get_val();
// fails the hypotf16 smoke tests.
float af = fputil::cast<float>(a_bits.get_val());
float bf = fputil::cast<float>(b_bits.get_val());

// Compiler runtime basic operations for float16 might not be correctly
// rounded for all rounding modes.
if (LIBC_UNLIKELY(a_u - b_u >=
static_cast<uint16_t>((FPBits::FRACTION_LEN + 2)
<< FPBits::FRACTION_LEN)))
return a_bits.get_val() + b_bits.get_val();

float af = fputil::cast<float>(a_bits.get_val());
float bf = fputil::cast<float>(b_bits.get_val());
return fputil::cast<float16>(af + bf);

// These squares are exact.
float a_sq = af * af;
Expand Down
1 change: 1 addition & 0 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,7 @@ libc_support_library(
":__support_cpp_bit",
":__support_cpp_type_traits",
":__support_fputil_basic_operations",
":__support_fputil_cast",
":__support_fputil_fenv_impl",
":__support_fputil_fp_bits",
":__support_fputil_rounding_mode",
Expand Down
Loading