-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[libc][math][c23] Add rsqrtf16() function #137545
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
12 commits
Select commit
Hold shift + click to select a range
cada218
- rsqrtf16 refactored
amemov be0de94
Clang-formated the files
amemov 4bea2ef
Replaced the computation for valid X with polynomial approximation
amemov 8f1e13c
Added range reduction to the approximation
amemov 1ff0f39
Added Newton-Raphson iterations
amemov cb4f47f
Added separate handling for mantissa == 0.5f. Resulted in fewer errors
amemov b5066ce
- Fixed ULP errors
amemov c17e1e0
clang-formatted the files
amemov a5b246b
Formatted BUILD.Bazel w/ buildifier
amemov 7cb3fb1
- Refactored the rsqrtf16 implementation to temporarily use hardware …
amemov 49cb38c
- Addressed the comments and added entrypoints to other targets in Linux
amemov 57d417b
- Replaced src/__support/libc_errno.h with hdr/errno_macros.h
amemov 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//===-- Shared rsqrtf16 function -------------------------------*- 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_SHARED_MATH_RSQRTF16_H | ||
#define LLVM_LIBC_SHARED_MATH_RSQRTF16_H | ||
|
||
#include "include/llvm-libc-macros/float16-macros.h" | ||
|
||
#ifdef LIBC_TYPES_HAS_FLOAT16 | ||
|
||
#include "shared/libc_common.h" | ||
#include "src/__support/math/rsqrtf16.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace shared { | ||
|
||
using math::rsqrtf16; | ||
|
||
} // namespace shared | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LIBC_TYPES_HAS_FLOAT16 | ||
|
||
#endif // LLVM_LIBC_SHARED_MATH_RSQRTF16_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,83 @@ | ||
//===-- Implementation header for rsqrtf16 ----------------------*- 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_MATH_RSQRTF16_H | ||
#define LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTF16_H | ||
|
||
#include "include/llvm-libc-macros/float16-macros.h" | ||
|
||
#ifdef LIBC_TYPES_HAS_FLOAT16 | ||
|
||
#include "src/__support/FPUtil/FEnvImpl.h" | ||
#include "src/__support/FPUtil/FPBits.h" | ||
#include "src/__support/FPUtil/ManipulationFunctions.h" | ||
#include "src/__support/FPUtil/cast.h" | ||
#include "src/__support/FPUtil/multiply_add.h" | ||
#include "src/__support/FPUtil/sqrt.h" | ||
#include "src/__support/macros/optimization.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace math { | ||
|
||
LIBC_INLINE static constexpr float16 rsqrtf16(float16 x) { | ||
using FPBits = fputil::FPBits<float16>; | ||
FPBits xbits(x); | ||
|
||
const uint16_t x_u = xbits.uintval(); | ||
const uint16_t x_abs = x_u & 0x7fff; | ||
const uint16_t x_sign = x_u >> 15; | ||
|
||
// x is NaN | ||
if (LIBC_UNLIKELY(xbits.is_nan())) { | ||
if (xbits.is_signaling_nan()) { | ||
fputil::raise_except_if_required(FE_INVALID); | ||
return FPBits::quiet_nan().get_val(); | ||
} | ||
return x; | ||
} | ||
|
||
// |x| = 0 | ||
if (LIBC_UNLIKELY(x_abs == 0x0)) { | ||
fputil::raise_except_if_required(FE_DIVBYZERO); | ||
fputil::set_errno_if_required(ERANGE); | ||
return FPBits::inf(Sign::POS).get_val(); | ||
} | ||
|
||
// -inf <= x < 0 | ||
if (LIBC_UNLIKELY(x_sign == 1)) { | ||
fputil::raise_except_if_required(FE_INVALID); | ||
fputil::set_errno_if_required(EDOM); | ||
return FPBits::quiet_nan().get_val(); | ||
} | ||
|
||
// x = +inf => rsqrt(x) = 0 | ||
if (LIBC_UNLIKELY(xbits.is_inf())) | ||
return FPBits::zero().get_val(); | ||
|
||
// TODO: add integer based implementation when LIBC_TARGET_CPU_HAS_FPU_FLOAT | ||
// is not defined | ||
float result = 1.0f / fputil::sqrt<float>(fputil::cast<float>(x)); | ||
|
||
// Targeted post-corrections to ensure correct rounding in half for specific | ||
// mantissa patterns | ||
const uint16_t half_mantissa = x_abs & 0x3ff; | ||
if (LIBC_UNLIKELY(half_mantissa == 0x011F)) { | ||
result = fputil::multiply_add(result, 0x1.0p-21f, result); | ||
} else if (LIBC_UNLIKELY(half_mantissa == 0x0313)) { | ||
result = fputil::multiply_add(result, -0x1.0p-21f, result); | ||
} | ||
|
||
return fputil::cast<float16>(result); | ||
} | ||
|
||
} // namespace math | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LIBC_TYPES_HAS_FLOAT16 | ||
|
||
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTF16_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
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,15 @@ | ||
//===-- Half-precision rsqrt 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/rsqrtf16.h" | ||
#include "src/__support/math/rsqrtf16.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(float16, rsqrtf16, (float16 x)) { return math::rsqrtf16(x); } | ||
} // namespace LIBC_NAMESPACE_DECL |
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,21 @@ | ||
//===-- Implementation header for rsqrtf16 ----------------------*- 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_RSQRTF16_H | ||
#define LLVM_LIBC_SRC_MATH_RSQRTF16_H | ||
|
||
#include "src/__support/macros/config.h" | ||
#include "src/__support/macros/properties/types.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
float16 rsqrtf16(float16 x); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_MATH_RSQRTF16_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
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,42 @@ | ||
//===-- Exhaustive test for rsqrtf16 --------------------------------------===// | ||
// | ||
// 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/rsqrtf16.h" | ||
#include "test/UnitTest/FPMatcher.h" | ||
#include "test/UnitTest/Test.h" | ||
#include "utils/MPFRWrapper/MPFRUtils.h" | ||
|
||
using LlvmLibcRsqrtf16Test = LIBC_NAMESPACE::testing::FPTest<float16>; | ||
|
||
namespace mpfr = LIBC_NAMESPACE::testing::mpfr; | ||
|
||
// Range: [0, Inf] | ||
static constexpr uint16_t POS_START = 0x0000U; | ||
static constexpr uint16_t POS_STOP = 0x7c00U; | ||
|
||
// Range: [-Inf, 0] | ||
static constexpr uint16_t NEG_START = 0x8000U; | ||
static constexpr uint16_t NEG_STOP = 0xfc00U; | ||
|
||
TEST_F(LlvmLibcRsqrtf16Test, PositiveRange) { | ||
for (uint16_t v = POS_START; v <= POS_STOP; ++v) { | ||
float16 x = FPBits(v).get_val(); | ||
|
||
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Rsqrt, x, | ||
LIBC_NAMESPACE::rsqrtf16(x), 0.5); | ||
} | ||
} | ||
|
||
TEST_F(LlvmLibcRsqrtf16Test, NegativeRange) { | ||
for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) { | ||
float16 x = FPBits(v).get_val(); | ||
|
||
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Rsqrt, x, | ||
LIBC_NAMESPACE::rsqrtf16(x), 0.5); | ||
} | ||
} |
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.