-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][math][c23] Add f16divf C23 math function #96131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
167d5ca
[libc][math][c23] Add f16divf C23 math function
overmighty 402df6c
fixup! [libc][math][c23] Add f16divf C23 math function
overmighty b1074ae
fixup! [libc][math][c23] Add f16divf C23 math function
overmighty df87e20
fixup! [libc][math][c23] Add f16divf C23 math function
overmighty 4ff508d
fixup! [libc][math][c23] Add f16divf C23 math function
overmighty 44fb424
fixup! [libc][math][c23] Add f16divf C23 math function
overmighty 771a449
fixup! [libc][math][c23] Add f16divf C23 math function
overmighty 6439428
fixup! [libc][math][c23] Add f16divf C23 math function
overmighty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
//===-- Division of IEEE 754 floating-point numbers -------------*- 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_SRC___SUPPORT_FPUTIL_GENERIC_DIV_H | ||
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_DIV_H | ||
|
||
#include "hdr/errno_macros.h" | ||
#include "hdr/fenv_macros.h" | ||
#include "src/__support/CPP/bit.h" | ||
#include "src/__support/CPP/type_traits.h" | ||
#include "src/__support/FPUtil/BasicOperations.h" | ||
#include "src/__support/FPUtil/FEnvImpl.h" | ||
#include "src/__support/FPUtil/FPBits.h" | ||
#include "src/__support/FPUtil/dyadic_float.h" | ||
#include "src/__support/macros/attributes.h" | ||
#include "src/__support/macros/optimization.h" | ||
|
||
namespace LIBC_NAMESPACE::fputil::generic { | ||
|
||
template <typename OutType, typename InType> | ||
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<OutType> && | ||
cpp::is_floating_point_v<InType> && | ||
sizeof(OutType) <= sizeof(InType), | ||
OutType> | ||
div(InType x, InType y) { | ||
using OutFPBits = FPBits<OutType>; | ||
using OutStorageType = typename OutFPBits::StorageType; | ||
using InFPBits = FPBits<InType>; | ||
using InStorageType = typename InFPBits::StorageType; | ||
using DyadicFloat = | ||
DyadicFloat<cpp::bit_ceil(static_cast<size_t>(InFPBits::FRACTION_LEN))>; | ||
|
||
InFPBits x_bits(x); | ||
InFPBits y_bits(y); | ||
|
||
Sign result_sign = x_bits.sign() == y_bits.sign() ? Sign::POS : Sign::NEG; | ||
|
||
if (LIBC_UNLIKELY(x_bits.is_inf_or_nan() || y_bits.is_inf_or_nan() || | ||
x_bits.is_zero() || y_bits.is_zero())) { | ||
if (x_bits.is_nan() || y_bits.is_nan()) { | ||
if (x_bits.is_signaling_nan() || y_bits.is_signaling_nan()) | ||
raise_except_if_required(FE_INVALID); | ||
|
||
if (x_bits.is_quiet_nan()) { | ||
InStorageType x_payload = static_cast<InStorageType>(getpayload(x)); | ||
if ((x_payload & ~(OutFPBits::FRACTION_MASK >> 1)) == 0) | ||
return OutFPBits::quiet_nan(x_bits.sign(), | ||
static_cast<OutStorageType>(x_payload)) | ||
.get_val(); | ||
} | ||
|
||
if (y_bits.is_quiet_nan()) { | ||
InStorageType y_payload = static_cast<InStorageType>(getpayload(y)); | ||
if ((y_payload & ~(OutFPBits::FRACTION_MASK >> 1)) == 0) | ||
return OutFPBits::quiet_nan(y_bits.sign(), | ||
static_cast<OutStorageType>(y_payload)) | ||
.get_val(); | ||
} | ||
|
||
return OutFPBits::quiet_nan().get_val(); | ||
} | ||
|
||
if (x_bits.is_inf()) { | ||
if (y_bits.is_inf()) { | ||
set_errno_if_required(EDOM); | ||
raise_except_if_required(FE_INVALID); | ||
return OutFPBits::quiet_nan().get_val(); | ||
} | ||
|
||
return OutFPBits::inf(result_sign).get_val(); | ||
} | ||
|
||
if (y_bits.is_inf()) | ||
return OutFPBits::inf(result_sign).get_val(); | ||
|
||
if (y_bits.is_zero()) { | ||
if (x_bits.is_zero()) { | ||
raise_except_if_required(FE_INVALID); | ||
return OutFPBits::quiet_nan().get_val(); | ||
} | ||
|
||
raise_except_if_required(FE_DIVBYZERO); | ||
return OutFPBits::inf(result_sign).get_val(); | ||
} | ||
|
||
if (x_bits.is_zero()) | ||
return OutFPBits::zero(result_sign).get_val(); | ||
} | ||
|
||
DyadicFloat xd(x); | ||
DyadicFloat yd(y); | ||
|
||
// Number of iterations = full output precision + 1 rounding bit + 1 potential | ||
// leading 0. | ||
constexpr size_t NUM_ITERS = OutFPBits::FRACTION_LEN + 3; | ||
int result_exp = xd.exponent - yd.exponent - (NUM_ITERS - 1); | ||
|
||
InStorageType q = 0; | ||
InStorageType r = static_cast<InStorageType>(xd.mantissa >> 2); | ||
InStorageType yd_mant_in = static_cast<InStorageType>(yd.mantissa >> 1); | ||
|
||
for (size_t i = 0; i < NUM_ITERS; ++i) { | ||
q <<= 1; | ||
r <<= 1; | ||
if (r >= yd_mant_in) { | ||
q += 1; | ||
r -= yd_mant_in; | ||
} | ||
} | ||
|
||
DyadicFloat result(result_sign, result_exp, q); | ||
result.mantissa += r != 0; | ||
|
||
return result.template as<OutType, /*ShouldSignalExceptions=*/true>(); | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE::fputil::generic | ||
|
||
#endif // LLVM_LIBC_SRC___SUPPORT_FPUTIL_GENERIC_DIV_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header for f16divf -----------------------*- 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_SRC_MATH_F16DIVF_H | ||
#define LLVM_LIBC_SRC_MATH_F16DIVF_H | ||
|
||
#include "src/__support/macros/properties/types.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
float16 f16divf(float x, float y); | ||
|
||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC_MATH_F16DIVF_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//===-- Implementation of f16divf function --------------------------------===// | ||
// | ||
// 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 "src/math/f16divf.h" | ||
#include "src/__support/FPUtil/generic/div.h" | ||
#include "src/__support/common.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
LLVM_LIBC_FUNCTION(float16, f16divf, (float x, float y)) { | ||
return fputil::generic::div<float16>(x, y); | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.