-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[libc][math][c23] Add acoshf16 C23 math function. #130588
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
17 commits
Select commit
Hold shift + click to select a range
3922cae
[libc][math][c23] Add acoshf16 C23 math function.
harrisonGPU 8906a10
[libc][math][c23] Fix some build issues.
harrisonGPU e9eca1b
[libc][math][c23] Fix some test failures.
harrisonGPU c6d781d
[libc][math][c23] Do some change for comments.
harrisonGPU c6582d9
[libc][math][c23] Do some change for comments again.
harrisonGPU 83ea70e
[libc][math][c23] Fix clang format issue.
harrisonGPU 59a1dc3
[libc][math][c23] Reduce MPFR to 1.
harrisonGPU 1fe434e
[libc][math][c23] Reduce MPFR to 0.5.
harrisonGPU 02ecb34
[libc][math][c23] Update accuracy.
harrisonGPU 9420e19
[libc][math][c23] Update for comments.
harrisonGPU 573e9d0
[libc][math][c23] Update for comments again.
harrisonGPU 33c2efa
[libc][math][c23] Do some change.
harrisonGPU 4a22df9
[libc][math][c23] Update test.
harrisonGPU c8f5517
[libc][math][c23] Update range.
harrisonGPU ce34bb7
[libc][math][c23] Do some change and add some tests.
harrisonGPU 45386af
[libc][math][c23] Update for comments.
harrisonGPU 73b34db
[libc][math][c23] Update for comments.
harrisonGPU 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//===-- Implementation header for acoshf16 ----------------------*- 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_ACOSHF16_H | ||
#define LLVM_LIBC_SRC_MATH_ACOSHF16_H | ||
|
||
#include "src/__support/macros/config.h" | ||
#include "src/__support/macros/properties/types.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
float16 acoshf16(float16 x); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_MATH_ACOSHF16_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,110 @@ | ||
//===-- Half-precision acosh(x) 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/acoshf16.h" | ||
harrisonGPU marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include "explogxf.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/PolyEval.h" | ||
#include "src/__support/FPUtil/cast.h" | ||
overmighty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include "src/__support/FPUtil/except_value_utils.h" | ||
#include "src/__support/FPUtil/multiply_add.h" | ||
#include "src/__support/FPUtil/sqrt.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/macros/config.h" | ||
#include "src/__support/macros/optimization.h" | ||
overmighty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
static constexpr size_t N_EXCEPTS = 2; | ||
static constexpr fputil::ExceptValues<float16, N_EXCEPTS> ACOSHF16_EXCEPTS{{ | ||
// (input, RZ output, RU offset, RD offset, RN offset) | ||
// x = 0x1.6dcp+1, acoshf16(x) = 0x1.b6p+0 (RZ) | ||
{0x41B7, 0x3ED8, 1, 0, 0}, | ||
// x = 0x1.39p+0, acoshf16(x) = 0x1.4f8p-1 (RZ) | ||
{0x3CE4, 0x393E, 1, 0, 1}, | ||
}}; | ||
|
||
LLVM_LIBC_FUNCTION(float16, acoshf16, (float16 x)) { | ||
using FPBits = fputil::FPBits<float16>; | ||
FPBits xbits(x); | ||
uint16_t x_u = xbits.uintval(); | ||
|
||
// Check for NaN input first. | ||
if (LIBC_UNLIKELY(xbits.is_inf_or_nan())) { | ||
if (xbits.is_signaling_nan()) { | ||
fputil::raise_except_if_required(FE_INVALID); | ||
return FPBits::quiet_nan().get_val(); | ||
} | ||
if (xbits.is_neg()) { | ||
fputil::set_errno_if_required(EDOM); | ||
fputil::raise_except_if_required(FE_INVALID); | ||
return FPBits::quiet_nan().get_val(); | ||
} | ||
return x; | ||
} | ||
|
||
// Domain error for inputs less than 1.0. | ||
if (LIBC_UNLIKELY(x <= 1.0f)) { | ||
if (x == 1.0f) | ||
return FPBits::zero().get_val(); | ||
fputil::set_errno_if_required(EDOM); | ||
fputil::raise_except_if_required(FE_INVALID); | ||
return FPBits::quiet_nan().get_val(); | ||
} | ||
|
||
if (auto r = ACOSHF16_EXCEPTS.lookup(xbits.uintval()); | ||
LIBC_UNLIKELY(r.has_value())) | ||
return r.value(); | ||
|
||
float xf = x; | ||
// High-precision polynomial approximation for inputs close to 1.0 | ||
// ([1, 1.25)). | ||
// | ||
// Brief derivation: | ||
// 1. Expand acosh(1 + delta) using Taylor series around delta=0: | ||
// acosh(1 + delta) ≈ sqrt(2 * delta) * [1 - delta/12 + 3*delta^2/160 | ||
// - 5*delta^3/896 + 35*delta^4/18432 + ...] | ||
// 2. Truncate the series to fit accurately for delta in [0, 0.25]. | ||
// 3. Polynomial coefficients (from sollya) used here are: | ||
// P(delta) ≈ 1 - 0x1.555556p-4 * delta + 0x1.333334p-6 * delta^2 | ||
// - 0x1.6db6dcp-8 * delta^3 + 0x1.f1c71cp-10 * delta^4 | ||
// 4. The Sollya commands used to generate these coefficients were: | ||
// > display = hexadecimal; | ||
// > round(1/12, SG, RN); | ||
// > round(3/160, SG, RN); | ||
// > round(5/896, SG, RN); | ||
// > round(35/18432, SG, RN); | ||
// With hexadecimal display mode enabled, the outputs were: | ||
// 0x1.555556p-4 | ||
// 0x1.333334p-6 | ||
// 0x1.6db6dcp-8 | ||
// 0x1.f1c71cp-10 | ||
// 5. The maximum absolute error, estimated using: | ||
// dirtyinfnorm(acosh(1 + x) - sqrt(2*x) * P(x), [0, 0.25]) | ||
// is: | ||
// 0x1.d84281p-22 | ||
if (LIBC_UNLIKELY(x_u < 0x3D00U)) { | ||
float delta = xf - 1.0f; | ||
float sqrt_2_delta = fputil::sqrt<float>(2.0 * delta); | ||
float pe = fputil::polyeval(delta, 0x1p+0f, -0x1.555556p-4f, 0x1.333334p-6f, | ||
-0x1.6db6dcp-8f, 0x1.f1c71cp-10f); | ||
float approx = sqrt_2_delta * pe; | ||
return fputil::cast<float16>(approx); | ||
} | ||
|
||
// acosh(x) = log(x + sqrt(x^2 - 1)) | ||
float sqrt_term = fputil::sqrt<float>(fputil::multiply_add(xf, xf, -1.0f)); | ||
float result = static_cast<float>(log_eval(xf + sqrt_term)); | ||
|
||
return fputil::cast<float16>(result); | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
harrisonGPU marked this conversation as resolved.
Show resolved
Hide resolved
|
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,28 @@ | ||
//===-- Unittests for acoshf16 --------------------------------------------===// | ||
// | ||
// 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/errno/libc_errno.h" | ||
#include "src/math/acoshf16.h" | ||
#include "test/UnitTest/FPMatcher.h" | ||
#include "test/UnitTest/Test.h" | ||
#include "utils/MPFRWrapper/MPFRUtils.h" | ||
#include <stdint.h> | ||
|
||
using LlvmLibcAcoshf16Test = LIBC_NAMESPACE::testing::FPTest<float16>; | ||
namespace mpfr = LIBC_NAMESPACE::testing::mpfr; | ||
|
||
static constexpr uint16_t START = 0x3c00U; | ||
static constexpr uint16_t STOP = 0x7c00; | ||
|
||
TEST_F(LlvmLibcAcoshf16Test, PositiveRange) { | ||
for (uint16_t v = START; v <= STOP; ++v) { | ||
float16 x = FPBits(v).get_val(); | ||
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Acosh, x, | ||
LIBC_NAMESPACE::acoshf16(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
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,70 @@ | ||
//===-- Unittests for acoshf16 --------------------------------------------===// | ||
// | ||
// 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/__support/FPUtil/cast.h" | ||
#include "src/errno/libc_errno.h" | ||
#include "src/math/acoshf16.h" | ||
#include "test/UnitTest/FPMatcher.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
using LlvmLibcAcoshf16Test = LIBC_NAMESPACE::testing::FPTest<float16>; | ||
|
||
TEST_F(LlvmLibcAcoshf16Test, SpecialNumbers) { | ||
LIBC_NAMESPACE::libc_errno = 0; | ||
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16(aNaN)); | ||
EXPECT_MATH_ERRNO(0); | ||
|
||
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acoshf16(sNaN), FE_INVALID); | ||
EXPECT_MATH_ERRNO(0); | ||
|
||
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acoshf16(zero), FE_INVALID); | ||
EXPECT_MATH_ERRNO(EDOM); | ||
|
||
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16(neg_zero)); | ||
EXPECT_MATH_ERRNO(EDOM); | ||
|
||
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acoshf16(neg_zero), | ||
FE_INVALID); | ||
EXPECT_MATH_ERRNO(EDOM); | ||
|
||
EXPECT_FP_EQ(inf, LIBC_NAMESPACE::acoshf16(inf)); | ||
EXPECT_MATH_ERRNO(0); | ||
|
||
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16(neg_inf)); | ||
EXPECT_MATH_ERRNO(EDOM); | ||
|
||
EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::acoshf16(neg_inf), | ||
FE_INVALID); | ||
EXPECT_MATH_ERRNO(EDOM); | ||
|
||
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::acoshf16( | ||
LIBC_NAMESPACE::fputil::cast<float16>(1.0))); | ||
EXPECT_MATH_ERRNO(0); | ||
|
||
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::acoshf16( | ||
LIBC_NAMESPACE::fputil::cast<float16>(0.5))); | ||
EXPECT_MATH_ERRNO(EDOM); | ||
|
||
EXPECT_FP_EQ_WITH_EXCEPTION( | ||
aNaN, | ||
LIBC_NAMESPACE::acoshf16(LIBC_NAMESPACE::fputil::cast<float16>(-1.0)), | ||
FE_INVALID); | ||
EXPECT_MATH_ERRNO(EDOM); | ||
|
||
EXPECT_FP_EQ_WITH_EXCEPTION( | ||
aNaN, | ||
LIBC_NAMESPACE::acoshf16(LIBC_NAMESPACE::fputil::cast<float16>(-2.0)), | ||
FE_INVALID); | ||
EXPECT_MATH_ERRNO(EDOM); | ||
|
||
EXPECT_FP_EQ_WITH_EXCEPTION( | ||
aNaN, | ||
LIBC_NAMESPACE::acoshf16(LIBC_NAMESPACE::fputil::cast<float16>(-3.0)), | ||
FE_INVALID); | ||
EXPECT_MATH_ERRNO(EDOM); | ||
} | ||
harrisonGPU marked this conversation as resolved.
Show resolved
Hide resolved
|
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.