[libc-commits] [libc] Add sinf16 function (PR #116674)
via libc-commits
libc-commits at lists.llvm.org
Mon Nov 18 10:42:10 PST 2024
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff 4f2651c36361468cf35cdcdf841d3abed9d0d1cc 49b868b06308000df1c8bf50ccf41ea0d4c4dcfb --extensions h,cpp -- libc/src/math/generic/sinf16.cpp libc/src/math/sinf16.h libc/test/src/math/sinf16_test.cpp libc/test/src/math/smoke/sinf16_test.cpp libc/src/math/generic/sincosf16_utils.h
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/libc/src/math/generic/sincosf16_utils.h b/libc/src/math/generic/sincosf16_utils.h
index fe6e58c1f9..3d7c437bca 100644
--- a/libc/src/math/generic/sincosf16_utils.h
+++ b/libc/src/math/generic/sincosf16_utils.h
@@ -40,9 +40,8 @@ constexpr float SIN_K_PI_OVER_32[64] = {
-0x1.87de2ap-2, -0x1.294062p-2, -0x1.8f8b84p-3, -0x1.917a6cp-4};
static constexpr float THIRTYTWO_OVER_PI[4] = {
-//0x1.45fp3, 0x1.837p-12, -0x1.b1b8p-28, -0x1.f568p-43
- 0x1.46p3, -0x1.9f4p-10, 0x1.b94p-22, -0x1.bcp-36
-};
+ // 0x1.45fp3, 0x1.837p-12, -0x1.b1b8p-28, -0x1.f568p-43
+ 0x1.46p3, -0x1.9f4p-10, 0x1.b94p-22, -0x1.bcp-36};
LIBC_INLINE int32_t range_reduction_sincospif16(float x, float &y) {
float kf = fputil::nearest_integer(x * 32);
@@ -61,8 +60,10 @@ LIBC_INLINE int32_t range_reduction_sincosf16(float x, float &y) {
return static_cast<int32_t>(kf);
}
-static LIBC_INLINE void sincosf16_poly_eval(int32_t k, float y, float &sin_k, float &cos_k, float &sin_y, float &cosm1_y) {
-
+static LIBC_INLINE void sincosf16_poly_eval(int32_t k, float y, float &sin_k,
+ float &cos_k, float &sin_y,
+ float &cosm1_y) {
+
sin_k = SIN_K_PI_OVER_32[k & 63];
cos_k = SIN_K_PI_OVER_32[(k + 16) & 63];
@@ -84,7 +85,8 @@ static LIBC_INLINE void sincosf16_poly_eval(int32_t k, float y, float &sin_k, fl
0x1.a6f7a2p-29f);
}
-LIBC_INLINE void sincosf16_eval(float xf, float &sin_k, float &cos_k, float &sin_y, float &cosm1_y) {
+LIBC_INLINE void sincosf16_eval(float xf, float &sin_k, float &cos_k,
+ float &sin_y, float &cosm1_y) {
float y;
int32_t k = range_reduction_sincosf16(xf, y);
@@ -95,7 +97,7 @@ LIBC_INLINE void sincospif16_eval(float xf, float &sin_k, float &cos_k,
float &sin_y, float &cosm1_y) {
float y;
int32_t k = range_reduction_sincospif16(xf, y);
-
+
sincosf16_poly_eval(k, y, sin_k, cos_k, sin_y, cosm1_y);
}
diff --git a/libc/src/math/generic/sinf16.cpp b/libc/src/math/generic/sinf16.cpp
index 7f1c8c7cf6..4e9b1adab5 100644
--- a/libc/src/math/generic/sinf16.cpp
+++ b/libc/src/math/generic/sinf16.cpp
@@ -25,7 +25,7 @@ LLVM_LIBC_FUNCTION(float16, sinf16, (float16 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:
@@ -41,14 +41,14 @@ LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) {
// sin(x) = sin((k + y) * pi/32)
// = sin(k * pi/32) * cos(y * pi/32) +
// sin(y * pi/32) * cos(k * pi/32)
-
+
if (LIBC_UNLIKELY(x_abs <= 0x13d0)) {
int rounding = fputil::quick_get_round();
-
+
// For signed zeros
if ((LIBC_UNLIKELY(x_abs == 0U)) ||
- (rounding == FE_UPWARD && xbits.is_pos()) ||
- (rounding == FE_DOWNWARD && xbits.is_neg()))
+ (rounding == FE_UPWARD && xbits.is_pos()) ||
+ (rounding == FE_DOWNWARD && xbits.is_neg()))
return x;
}
@@ -61,7 +61,6 @@ LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) {
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);
@@ -71,7 +70,8 @@ LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) {
// 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)));
+ return fputil::cast<float16>(fputil::multiply_add(
+ sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/sinf16_test.cpp b/libc/test/src/math/sinf16_test.cpp
index cdb69d388f..b469e1dcb8 100644
--- a/libc/test/src/math/sinf16_test.cpp
+++ b/libc/test/src/math/sinf16_test.cpp
@@ -26,13 +26,15 @@ 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);
+ 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);
+ EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sin, x,
+ LIBC_NAMESPACE::sinf16(x), 0.5);
}
}
diff --git a/libc/test/src/math/smoke/sinf16_test.cpp b/libc/test/src/math/smoke/sinf16_test.cpp
index 301e91a683..2966c3c952 100644
--- a/libc/test/src/math/smoke/sinf16_test.cpp
+++ b/libc/test/src/math/smoke/sinf16_test.cpp
@@ -6,7 +6,6 @@
//
//===----------------------------------------------------------------------===//
-
#include "src/errno/libc_errno.h"
#include "src/math/sinf16.h"
#include "test/UnitTest/FPMatcher.h"
@@ -16,7 +15,7 @@ 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);
``````````
</details>
https://github.com/llvm/llvm-project/pull/116674
More information about the libc-commits
mailing list