-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc][math][c23] Add rsqrtf() function #159615
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 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e84a1e6
- Initial implementation of rsqrt for single precision float
amemov 5b8b21a
- clang-formatted the files
amemov 26674a6
- Created exhaustive test for rsqrt in libc/test/src/math/exhaustive/
amemov 0d8e57a
- Added comments to RsqrtTest.h template, to clarify use case
amemov 1cc47f3
Addressed the comments, fixed include in rsqrtf16_test.cpp and cmake …
amemov 0faacb6
Clang-formatted the files in question, ran Buildifier on .bazel
amemov 471ef62
Addressed the comments, except last Bazel one
amemov f7387dd
Fixed typo in result variable
amemov 1e89686
Fixed bazel for rsqrtf and rsqrtf16
amemov bb9a399
Merge branch 'main' into rsqrtf-c23
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
Some comments aren't visible on the classic Files Changed page.
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,23 @@ | ||
//===-- Shared rsqrtf 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_RSQRTF_H | ||
#define LLVM_LIBC_SHARED_MATH_RSQRTF_H | ||
|
||
#include "shared/libc_common.h" | ||
#include "src/__support/math/rsqrtf.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace shared { | ||
|
||
using math::rsqrtf; | ||
|
||
} // namespace shared | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SHARED_MATH_RSQRTF_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,71 @@ | ||
//===-- Implementation header for rsqrtf ------------------------*- 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_RSQRTF_H | ||
#define LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTF_H | ||
|
||
#include "hdr/errno_macros.h" | ||
#include "hdr/fenv_macros.h" | ||
#include "src/__support/FPUtil/FEnvImpl.h" | ||
#include "src/__support/FPUtil/FPBits.h" | ||
#include "src/__support/FPUtil/cast.h" | ||
#include "src/__support/FPUtil/sqrt.h" | ||
#include "src/__support/macros/optimization.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace math { | ||
|
||
LIBC_INLINE static constexpr float rsqrtf(float x) { | ||
using FPBits = fputil::FPBits<float>; | ||
FPBits xbits(x); | ||
|
||
uint32_t x_u = xbits.uintval(); | ||
uint32_t x_abs = x_u & 0x7fff'ffffU; | ||
|
||
constexpr uint32_t INF_BITS = FPBits::inf().uintval(); | ||
|
||
// x is 0, inf/nan, or negative. | ||
if (LIBC_UNLIKELY(x_u == 0 || x_u >= INF_BITS)) { | ||
// x is NaN | ||
if (x_abs > INF_BITS) { | ||
if (xbits.is_signaling_nan()) { | ||
fputil::raise_except_if_required(FE_INVALID); | ||
return FPBits::quiet_nan().get_val(); | ||
} | ||
return x; | ||
} | ||
|
||
// |x| = 0 | ||
if (x_abs == 0) { | ||
fputil::raise_except_if_required(FE_DIVBYZERO); | ||
fputil::set_errno_if_required(ERANGE); | ||
return FPBits::inf(xbits.sign()).get_val(); | ||
} | ||
|
||
// -inf <= x < 0 | ||
if (x_u > 0x7fff'ffffU) { | ||
fputil::raise_except_if_required(FE_INVALID); | ||
fputil::set_errno_if_required(EDOM); | ||
return FPBits::quiet_nan().get_val(); | ||
} | ||
|
||
// x = +inf => rsqrt(x) = 0 | ||
return FPBits::zero(xbits.sign()).get_val(); | ||
} | ||
|
||
// TODO: add float based approximation when | ||
// LIBC_TARGET_CPU_HAS_FPU_DOUBLE is not defined | ||
double result = 1.0 / fputil::sqrt<double>(fputil::cast<double>(x)); | ||
|
||
return fputil::cast<float>(result); | ||
} | ||
|
||
} // namespace math | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC___SUPPORT_MATH_RSQRTF_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,16 @@ | ||
//===-- Single-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/rsqrtf.h" | ||
#include "src/__support/math/rsqrtf.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(float, rsqrtf, (float x)) { return math::rsqrtf(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,20 @@ | ||
//===-- Implementation header for rsqrtf ------------------------*- 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_RSQRTF_H | ||
#define LLVM_LIBC_SRC_MATH_RSQRTF_H | ||
|
||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
float rsqrtf(float x); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_MATH_RSQRTF_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,74 @@ | ||
//===-- Utility class to test rsqrt[f|l] ------------------------*- C++ -*-===// | ||
amemov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// 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_TEST_SRC_MATH_RSQRTTEST_H | ||
#define LLVM_LIBC_TEST_SRC_MATH_RSQRTTEST_H | ||
|
||
#include "test/UnitTest/FEnvSafeTest.h" | ||
#include "test/UnitTest/FPMatcher.h" | ||
#include "test/UnitTest/Test.h" | ||
#include "utils/MPFRWrapper/MPFRUtils.h" | ||
|
||
namespace mpfr = LIBC_NAMESPACE::testing::mpfr; | ||
|
||
template <typename OutType, typename InType = OutType> | ||
class RsqrtTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { | ||
|
||
DECLARE_SPECIAL_CONSTANTS(InType) | ||
|
||
static constexpr StorageType HIDDEN_BIT = | ||
StorageType(1) << LIBC_NAMESPACE::fputil::FPBits<InType>::FRACTION_LEN; | ||
|
||
public: | ||
using RsqrtFunc = OutType (*)(InType); | ||
|
||
// Subnormal inputs: probe both power-of-two mantissas and an even sampling | ||
// across the subnormal range. | ||
void test_denormal_values(RsqrtFunc func) { | ||
// Powers of two in the subnormal mantissa space. | ||
for (StorageType mant = 1; mant < HIDDEN_BIT; mant <<= 1) { | ||
FPBits denormal(zero); | ||
denormal.set_mantissa(mant); | ||
InType x = denormal.get_val(); | ||
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Rsqrt, x, func(x), 0.5); | ||
} | ||
|
||
// Even sampling across all subnormals. | ||
constexpr StorageType COUNT = 200'001; | ||
constexpr StorageType STEP = HIDDEN_BIT / COUNT; | ||
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) { | ||
InType x = FPBits(i).get_val(); | ||
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Rsqrt, x, func(x), 0.5); | ||
} | ||
} | ||
|
||
// Positive normal range sampling: skip NaNs and negative values. | ||
void test_normal_range(RsqrtFunc func) { | ||
constexpr StorageType COUNT = 200'001; | ||
constexpr StorageType STEP = STORAGE_MAX / COUNT; | ||
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) { | ||
FPBits x_bits(v); | ||
InType x = x_bits.get_val(); | ||
if (x_bits.is_nan() || x_bits.is_neg()) | ||
continue; | ||
ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Rsqrt, x, func(x), 0.5); | ||
} | ||
} | ||
}; | ||
|
||
#define LIST_RSQRT_TESTS(T, func) \ | ||
using LlvmLibcRsqrtTest = RsqrtTest<T, T>; \ | ||
TEST_F(LlvmLibcRsqrtTest, DenormalValues) { test_denormal_values(&func); } \ | ||
TEST_F(LlvmLibcRsqrtTest, NormalRange) { test_normal_range(&func); } | ||
|
||
#define LIST_NARROWING_RSQRT_TESTS(OutType, InType, func) \ | ||
using LlvmLibcRsqrtTest = RsqrtTest<OutType, InType>; \ | ||
TEST_F(LlvmLibcRsqrtTest, DenormalValues) { test_denormal_values(&func); } \ | ||
TEST_F(LlvmLibcRsqrtTest, NormalRange) { test_normal_range(&func); } | ||
|
||
#endif // LLVM_LIBC_TEST_SRC_MATH_RSQRTTEST_H |
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.