-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] Add sinpif16 function #110994
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
[libc] Add sinpif16 function #110994
Changes from 6 commits
1dd121c
d7bbbc2
3255302
663fbd5
0f038fe
c54defc
a8f7484
3f2282c
6617bf9
348eea1
0ad7d39
0f4af07
24f1173
cacbf19
a543283
743625d
9f85a85
ffcbd23
d6fad4c
06c9b4b
87faa10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,197 @@ | ||||
| //===-- Half-precision sinpif 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/sinpif16.h" | ||||
overmighty marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| #include "src/__support/FPUtil/FEnvImpl.h" | ||||
| #include "src/__support/FPUtil/FPBits.h" | ||||
| #include "src/__support/FPUtil/PolyEval.h" | ||||
| #include "src/__support/FPUtil/multiply_add.h" | ||||
| #include "src/__support/FPUtil/nearest_integer.h" | ||||
| #include "src/__support/common.h" | ||||
| #include "src/__support/macros/config.h" | ||||
|
|
||||
| // TODO: Should probably create a new file; sincospif16_utils.h | ||||
| // To store the following helper functions and constants. | ||||
| // I'd defer to @lntue for suggestions regarding that | ||||
|
||||
|
|
||||
| // HELPER_START | ||||
overmighty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| namespace LIBC_NAMESPACE_DECL { | ||||
|
|
||||
| constexpr float PI_OVER_32 = 0x1.921fb6p-4f; | ||||
|
|
||||
| // In Sollya generate 10 coeffecients for a degree-9 chebyshev polynomial | ||||
| // approximating the sine function in [-pi / 32, pi / 32] with the following | ||||
| // commands: | ||||
| // > prec=24; | ||||
| // > TL = chebyshevform(sin(x), 9, [-pi / 32, pi / 32]); | ||||
| // > TL[0]; | ||||
| const float SIN_COEFF[10] = { | ||||
overmighty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| 0x1.d333p-26, 0x1.000048p0, -0x1.a5d2p-14, -0x1.628588p-3, 0x1.c1eep-5, | ||||
| 0x1.4455p1, -0x1.317a8p3, -0x1.6bb9p8, 0x1.00ef8p9, 0x1.0edcp14}; | ||||
| // In Sollya generate 10 coefficients for a degree-9 chebyshev polynomial | ||||
| // approximating the sine function in [-pi/32, pi/32] with the following | ||||
| // commands: | ||||
| // > prec = 24; | ||||
| // > TL = chebyshevform(cos(x), 9, [-pi / 32, pi / 32]); | ||||
| // > TL[0]; | ||||
| const float COS_COEFF[10] = { | ||||
| 0x1.000006p0, 0x1.e1eap-15, -0x1.0071p-1, -0x1.3b56p-4, 0x1.f3dfp-2, | ||||
| 0x1.ccbap4, -0x1.3034p6, -0x1.f817p11, 0x1.fc59p11, 0x1.7079p17}; | ||||
| // Lookup table for sin(k * pi / 32) with k = 0, ..., 63. | ||||
| // Table is generated with Sollya as follows: | ||||
| // > display = hexadecimmal; | ||||
| // > prec = 24; | ||||
| // > for k from 0 to 63 do {sin(k * pi/32);}; | ||||
lntue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
|
|
||||
| const float SIN_K_PI_OVER_32[64] = {0, | ||||
overmighty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
lntue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| 0x1.917a6cp-4, | ||||
| 0x1.8f8b84p-3, | ||||
| 0x1.294062p-2, | ||||
| 0x1.87de2ap-2, | ||||
| 0x1.e2b5d4p-2, | ||||
| 0x1.1c73b4p-1, | ||||
| 0x1.44cf32p-1, | ||||
| 0x1.6a09e6p-1, | ||||
| 0x1.8bc806p-1, | ||||
| 0x1.a9b662p-1, | ||||
| 0x1.c38b3p-1, | ||||
| 0x1.d906bcp-1, | ||||
| 0x1.e9f416p-1, | ||||
| 0x1.f6297cp-1, | ||||
| 0x1.fd88dap-1, | ||||
| 0x1p0, | ||||
| 0x1.fd88dap-1, | ||||
| 0x1.f6297cp-1, | ||||
| 0x1.e9f416p-1, | ||||
| 0x1.d906bcp-1, | ||||
| 0x1.c38b3p-1, | ||||
| 0x1.a9b662p-1, | ||||
| 0x1.8bc806p-1, | ||||
| 0x1.6a09e6p-1, | ||||
| 0x1.44cf32p-1, | ||||
| 0x1.1c73b4p-1, | ||||
| 0x1.e2b5d4p-2, | ||||
| 0x1.87de2ap-2, | ||||
| 0x1.294062p-2, | ||||
| 0x1.8f8b84p-3, | ||||
| 0x1.917a6cp-4, | ||||
| 0, | ||||
| -0x1.917a6cp-4, | ||||
| -0x1.8f8b84p-3, | ||||
| -0x1.294062p-2, | ||||
| -0x1.87de2ap-2, | ||||
| -0x1.e2b5d4p-2, | ||||
| -0x1.1c73b4p-1, | ||||
| -0x1.44cf32p-1, | ||||
| -0x1.6a09e6p-1, | ||||
| -0x1.8bc806p-1, | ||||
| -0x1.a9b662p-1, | ||||
| -0x1.c38b3p-1, | ||||
| -0x1.d906bcp-1, | ||||
| -0x1.e9f416p-1, | ||||
| -0x1.f6297ep-1, | ||||
| -0x1.fd88dap-1, | ||||
| -0x1p0, | ||||
| -0x1.fd88dap-1, | ||||
| -0x1.f6297cp-1, | ||||
| -0x1.e9f416p-1, | ||||
| -0x1.d906bcp-1, | ||||
| -0x1.c38b3p-1, | ||||
| -0x1.a9b662p-1, | ||||
| -0x1.8bc806p-1, | ||||
| -0x1.6a09e6p-1, | ||||
| -0x1.44cf32p-1, | ||||
| -0x1.1c73b4p-1, | ||||
| -0x1.e2b5d4p-2, | ||||
| -0x1.87de2ap-2, | ||||
| -0x1.294062p-2, | ||||
| -0x1.8f8b84p-3, | ||||
| -0x1.917a6cp-4}; | ||||
overmighty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
|
|
||||
| int32_t range_reduction(float x, float &y) { | ||||
lntue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| float kf = fputil::nearest_integer(x * 32); | ||||
| y = fputil::multiply_add<float>(x, 32.0, -kf); | ||||
|
|
||||
| return static_cast<int32_t>(kf); | ||||
| } | ||||
|
||||
| static LIBC_INLINE int64_t range_reduction_sincospi(double x, double &y) { |
static LIBC_INLINE int64_t range_reduction_sincospi(double x, double &y) {
double kd = fputil::nearest_integer(x * 32);
y = fputil::multiply_add<double>(x, 32.0, -kd);
return static_cast<int64_t>(kd);
}
lntue marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
overmighty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
overmighty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
overmighty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //===-- Implementation header for sinpif16 ---------------------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache Licese 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_SINPIF16_H | ||
| #define LLVM_LIBC_SRC_MATH_SINPIF16_H | ||
|
|
||
| #include "include/llvm-libc-macros/float16-macros.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/__support/macros/properties/types.h" | ||
overmighty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| float16 sinpif16(float16 x); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_MATH_SINPIF16_H | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| //===-- Unittests for sinpif16 --------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| // | ||
| // ===----------------------------------------------------------------------==// | ||
overmighty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
overmighty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #include "src/errno/libc_errno.h" | ||
| #include "src/math/sinpif16.h" | ||
| #include "test/UnitTest/FPMatcher.h" | ||
|
|
||
| #include <stdint.h> | ||
overmighty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| using LlvmLibcSinpif16Test = LIBC_NAMESPACE::testing::FPTest<float16>; | ||
|
|
||
| TEST_F(LlvmLibcSinpif16Test, SpecialNumbers) { | ||
| LIBC_NAMESPACE::libc_errno = 0; | ||
|
|
||
| EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinpif16(aNaN)); | ||
| EXPECT_MATH_ERRNO(0); | ||
|
|
||
| EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::sinpif16(0.0f)); | ||
| EXPECT_MATH_ERRNO(0); | ||
|
|
||
| EXPECT_FP_EQ(-0.0f, LIBC_NAMESPACE::sinpif16(-0.0f)); | ||
| EXPECT_MATH_ERRNO(0); | ||
|
|
||
| EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinpif16(inf)); | ||
| EXPECT_MATH_ERRNO(EDOM); | ||
|
|
||
| EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinpif16(neg_inf)); | ||
| EXPECT_MATH_ERRNO(EDOM); | ||
| } | ||
|
|
||
| TEST_F(LlvmLibcSinpif16Test, Integers) { | ||
| EXPECT_FP_EQ(-0.0, LIBC_NAMESPACE::sinpif16(-0x420)); | ||
| EXPECT_FP_EQ(-0.0, LIBC_NAMESPACE::sinpif16(-0x1p+10)); | ||
| EXPECT_FP_EQ(-0.0, LIBC_NAMESPACE::sinpif16(-0x1.4p+14)); | ||
| EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::sinpif16(0x420)); | ||
| EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::sinpif16(0x1.cp+15)); | ||
| EXPECT_FP_EQ(0.0, LIBC_NAMESPACE::sinpif16(0x1.cp+7)); | ||
| } | ||
overmighty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.