-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc][math][c23] Add sinf16 C23 math function #116674
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
49b868b
Add sinf16 function
wldfngrs 1d5f809
Fixed rounding errors
wldfngrs 09b7930
small fix
wldfngrs 0eefcc2
Update Docs
wldfngrs d446438
Merge branch 'main' into add_sinf16_function
wldfngrs 33d32ec
Merge branch 'main' into add_sinf16_function
wldfngrs fd28fcd
nit formatting
wldfngrs e234abd
formatting
wldfngrs c6bc857
Added comments, improved code readability
wldfngrs 92bdef5
clang-format
wldfngrs e78022b
Merge branch 'llvm:main' into add_sinf16_function
wldfngrs 7a72ef9
minor change
wldfngrs 2a6e3a5
clang format
wldfngrs c4d06b6
Merge branch 'main' into add_sinf16_function
wldfngrs 37169fb
Merge branch 'main' into add_sinf16_function
wldfngrs ec08e2c
Update libc/src/math/generic/sinf16.cpp
wldfngrs 9d21c3f
Update libc/src/math/generic/CMakeLists.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
//===-- Half-precision sin(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/sinf16.h" | ||
#include "hdr/errno_macros.h" | ||
#include "hdr/fenv_macros.h" | ||
#include "sincosf16_utils.h" | ||
#include "src/__support/FPUtil/FEnvImpl.h" | ||
#include "src/__support/FPUtil/FPBits.h" | ||
#include "src/__support/FPUtil/cast.h" | ||
#include "src/__support/FPUtil/except_value_utils.h" | ||
#include "src/__support/FPUtil/multiply_add.h" | ||
#include "src/__support/macros/optimization.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
constexpr size_t N_EXCEPTS = 4; | ||
|
||
constexpr fputil::ExceptValues<float16, N_EXCEPTS> SINF16_EXCEPTS{{ | ||
// (input, RZ output, RU offset, RD offset, RN offset) | ||
{0x2b45, 0x2b43, 1, 0, 1}, | ||
{0x585c, 0x3ba3, 1, 0, 1}, | ||
{0x5cb0, 0xbbff, 0, 1, 0}, | ||
{0x51f5, 0xb80f, 0, 1, 0}, | ||
}}; | ||
|
||
LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) { | ||
using FPBits = fputil::FPBits<float16>; | ||
FPBits xbits(x); | ||
|
||
uint16_t x_u = xbits.uintval(); | ||
uint16_t x_abs = x_u & 0x7fff; | ||
float xf = x; | ||
|
||
// Range reduction: | ||
// For |x| > pi/32, we perform range reduction as follows: | ||
// Find k and y such that: | ||
// x = (k + y) * pi/32 | ||
// k is an integer, |y| < 0.5 | ||
// | ||
// This is done by performing: | ||
// k = round(x * 32/pi) | ||
// y = x * 32/pi - k | ||
// | ||
// Once k and y are computed, we then deduce the answer by the sine of sum | ||
// formula: | ||
// sin(x) = sin((k + y) * pi/32) | ||
// = sin(k * pi/32) * cos(y * pi/32) + | ||
// sin(y * pi/32) * cos(k * pi/32) | ||
|
||
// Handle exceptional values | ||
if (LIBC_UNLIKELY(x_abs == 0x585c || x_abs == 0x5cb0 || x_abs == 0x51f5 || | ||
x_abs == 0x2b45)) { | ||
bool x_sign = x_u >> 15; | ||
if (auto r = SINF16_EXCEPTS.lookup_odd(x_abs, x_sign); | ||
LIBC_UNLIKELY(r.has_value())) | ||
return r.value(); | ||
} | ||
|
||
int rounding = fputil::quick_get_round(); | ||
|
||
// Exhaustive tests show that for |x| <= 0x1.f4p-11, 1ULP rounding errors | ||
// occur. To fix this, the following apply: | ||
if (LIBC_UNLIKELY(x_abs <= 0x13d0)) { | ||
overmighty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// sin(+/-0) = +/-0 | ||
if (LIBC_UNLIKELY(x_abs == 0U)) | ||
return x; | ||
|
||
// When x > 0, and rounding upward, sin(x) == x. | ||
// When x < 0, and rounding downward, sin(x) == x. | ||
if ((rounding == FE_UPWARD && xbits.is_pos()) || | ||
(rounding == FE_DOWNWARD && xbits.is_neg())) | ||
return x; | ||
|
||
// When x < 0, and rounding upward, sin(x) == (x - 1ULP) | ||
if (rounding == FE_UPWARD && xbits.is_neg()) { | ||
x_u--; | ||
return FPBits(x_u).get_val(); | ||
} | ||
} | ||
|
||
if (xbits.is_inf_or_nan()) { | ||
if (xbits.is_inf()) { | ||
fputil::set_errno_if_required(EDOM); | ||
fputil::raise_except_if_required(FE_INVALID); | ||
} | ||
|
||
return x + FPBits::quiet_nan().get_val(); | ||
} | ||
|
||
float sin_k, cos_k, sin_y, cosm1_y; | ||
sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y); | ||
|
||
if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0)) | ||
return FPBits::zero(xbits.sign()).get_val(); | ||
|
||
// Since, cosm1_y = cos_y - 1, therfore: | ||
// sin(x) = cos_k * sin_y + sin_k + (cosm1_y * sin_k) | ||
return fputil::cast<float16>(fputil::multiply_add( | ||
sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k))); | ||
} | ||
|
||
} // 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
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 sinf16 ------------------------*- 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_SINF16_H | ||
#define LLVM_LIBC_SRC_MATH_SINF16_H | ||
|
||
#include "src/__support/macros/config.h" | ||
#include "src/__support/macros/properties/types.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
float16 sinf16(float16 x); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_MATH_SINF16_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,40 @@ | ||
//===-- Exhaustive test for sinf16 ----------------------------------------===// | ||
// | ||
// 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/sinf16.h" | ||
#include "test/UnitTest/FPMatcher.h" | ||
#include "test/UnitTest/Test.h" | ||
#include "utils/MPFRWrapper/MPFRUtils.h" | ||
|
||
using LlvmLibcSinf16Test = 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(LlvmLibcSinf16Test, PositiveRange) { | ||
for (uint16_t v = POS_START; v <= POS_STOP; ++v) { | ||
float16 x = FPBits(v).get_val(); | ||
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x, | ||
LIBC_NAMESPACE::sinf16(x), 0.5); | ||
} | ||
} | ||
|
||
TEST_F(LlvmLibcSinf16Test, NegativeRange) { | ||
for (uint16_t v = NEG_START; v <= NEG_STOP; ++v) { | ||
float16 x = FPBits(v).get_val(); | ||
EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x, | ||
LIBC_NAMESPACE::sinf16(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,33 @@ | ||
//===-- Unittests for sinf16 ----------------------------------------------===// | ||
// | ||
// 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/sinf16.h" | ||
#include "test/UnitTest/FPMatcher.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
using LlvmLibcSinf16Test = LIBC_NAMESPACE::testing::FPTest<float16>; | ||
|
||
TEST_F(LlvmLibcSinf16Test, SpecialNumbers) { | ||
LIBC_NAMESPACE::libc_errno = 0; | ||
|
||
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinf16(aNaN)); | ||
EXPECT_MATH_ERRNO(0); | ||
|
||
EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinf16(zero)); | ||
EXPECT_MATH_ERRNO(0); | ||
|
||
EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinf16(neg_zero)); | ||
EXPECT_MATH_ERRNO(0); | ||
|
||
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinf16(inf)); | ||
EXPECT_MATH_ERRNO(EDOM); | ||
|
||
EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinf16(neg_inf)); | ||
EXPECT_MATH_ERRNO(EDOM); | ||
} |
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.