[libc-commits] [libc] [libc][math][C23] Implement double precision sinpi correctly rounded for all rounding modes (PR #134921)

via libc-commits libc-commits at lists.llvm.org
Tue Apr 8 13:02:59 PDT 2025


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 HEAD~1 HEAD --extensions cpp -- libc/src/math/generic/sinpi.cpp libc/test/src/math/sinpi_tests.cpp libc/test/src/math/smoke/sinpi_test.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/libc/src/math/generic/sinpi.cpp b/libc/src/math/generic/sinpi.cpp
index 31aa5f105..fdc16548c 100644
--- a/libc/src/math/generic/sinpi.cpp
+++ b/libc/src/math/generic/sinpi.cpp
@@ -3,22 +3,22 @@
 #include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/multiply_add.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
 #include "src/__support/FPUtil/double_double.h"
 #include "src/__support/FPUtil/generic/mul.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"
 #include "src/math/pow.h"
-//#include "range_reduction_double_nofma.h"
-//#include "src/__support/FPUtil/multiply_add.h"
-//#include "src/math/generic/range_reduction_double_common.h"
+// #include "range_reduction_double_nofma.h"
+// #include "src/__support/FPUtil/multiply_add.h"
+// #include "src/math/generic/range_reduction_double_common.h"
 #include <iostream>
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(double, sinpi, (double x)) {
-  // Given x * pi = y - (k * (pi/128)) 
+  // Given x * pi = y - (k * (pi/128))
   // find y and k such that
   // y = x * pi - (k * pi/128) = x * pi - kpi/128
   // k = round(x, 128)
@@ -29,14 +29,16 @@ LLVM_LIBC_FUNCTION(double, sinpi, (double x)) {
   FPBits xbits(x);
 
   double k = fputil::nearest_integer(x * 128);
-  int  k_int = static_cast<int>(k);
+  int k_int = static_cast<int>(k);
 
   std::cout << "k" << k << std::endl;
 
-  double yk = x - k/128;
+  double yk = x - k / 128;
 
-  DoubleDouble yy = fputil::exact_mult(yk, 3.141592653589793115997963468544185161590576171875);
-  yy.lo = fputil::multiply_add(yk, 1.2246467991473532071737640294583966046256921246776e-16, yy.lo);
+  DoubleDouble yy = fputil::exact_mult(
+      yk, 3.141592653589793115997963468544185161590576171875);
+  yy.lo = fputil::multiply_add(
+      yk, 1.2246467991473532071737640294583966046256921246776e-16, yy.lo);
 
   uint64_t abs_u = xbits.uintval();
 
@@ -84,12 +86,13 @@ LLVM_LIBC_FUNCTION(double, sinpi, (double x)) {
 
   rr.lo += sin_y_cos_k.lo + cos_y_sin_k.lo;
 
-  std::cout << "rrlo2: " << rr.lo << std::endl;  
+  std::cout << "rrlo2: " << rr.lo << std::endl;
   std::cout << "cos_y_sin_k:" << cos_y_sin_k.hi << std::endl;
   std::cout << "siny*cosk.lo:" << sin_y_cos_k.lo << std::endl;
-  std::cout << "rrhi + rrlo + sink.hi " << rr.hi + rr.lo + sin_k.hi + sin_k.lo << std::endl;
+  std::cout << "rrhi + rrlo + sink.hi " << rr.hi + rr.lo + sin_k.hi + sin_k.lo
+            << std::endl;
   std::cout << "rrhi + rrlo " << rr.hi + rr.lo << std::endl;
 
   return rr.hi + rr.lo;
- }
-} // namespace LIBC_NAMESPACE_DE
+}
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/math/sinpi_tests.cpp b/libc/test/src/math/sinpi_tests.cpp
index 514c43774..4b103170d 100644
--- a/libc/test/src/math/sinpi_tests.cpp
+++ b/libc/test/src/math/sinpi_tests.cpp
@@ -1,4 +1,5 @@
-@@ -0,0 +1,87 @@
+@ @-0, 0 + 1,
+    87 @ @
 //===-- Exhaustive test for sinpif16 --------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
@@ -14,7 +15,7 @@
 
 #include <iostream>
 
-using LlvmLibcSinpiTest = LIBC_NAMESPACE::testing::FPTest<double>;
+    using LlvmLibcSinpiTest = LIBC_NAMESPACE::testing::FPTest<double>;
 using namespace std;
 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
 
@@ -50,7 +51,6 @@ TEST_F(LlvmLibcSinpiTest, InDoubleRange) {
 
       ++count;
 
-
       if (!TEST_MPFR_MATCH_ROUNDING_SILENTLY(mpfr::Operation::Sinpi, x, result,
                                              2.0, rounding_mode)) {
         ++fails;
@@ -72,7 +72,6 @@ TEST_F(LlvmLibcSinpiTest, InDoubleRange) {
       tlog << "   Max ULPs is at most: " << static_cast<uint64_t>(tol) << ".\n";
       EXPECT_MPFR_MATCH(mpfr::Operation::Sinpi, mx, mr, 2.0, rounding_mode);
     }
-
   };
   tlog << " Test Rounding To Nearest...\n";
   test(mpfr::RoundingMode::Nearest);
diff --git a/libc/test/src/math/smoke/sinpi_test.cpp b/libc/test/src/math/smoke/sinpi_test.cpp
index 2b39189a3..2dd034c2c 100644
--- a/libc/test/src/math/smoke/sinpi_test.cpp
+++ b/libc/test/src/math/smoke/sinpi_test.cpp
@@ -29,7 +29,7 @@ TEST_F(LlvmLibcSinpiTest, SpecialNumbers) {
 }
 TEST_F(LlvmLibcSinpiTest, Integers) {
   EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpi(-0x1.0000000000003p52));
-  
+
   ASSERT_FP_EQ(-1.0, LIBC_NAMESPACE::sinpi(4499003037990983.5));
   EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpi(-0x1.0000000000005p52));
   EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpi(-0x1.0000000000006p52));
@@ -41,7 +41,11 @@ TEST_F(LlvmLibcSinpiTest, Integers) {
 */
 
 TEST_F(LlvmLibcSinpiTest, Integers) {
-  //ASSERT_FP_EQ(zero, LIBC_NAMESPACE::sinpi(4503563146482784.00000000000000000000000000000000000000000000000000));
+  // ASSERT_FP_EQ(zero,
+  // LIBC_NAMESPACE::sinpi(4503563146482784.00000000000000000000000000000000000000000000000000));
 
-  ASSERT_FP_EQ(-1.0, LIBC_NAMESPACE::sinpi(4499003037990983.50000000000000000000000000000000000000000000000000));
+  ASSERT_FP_EQ(
+      -1.0,
+      LIBC_NAMESPACE::sinpi(
+          4499003037990983.50000000000000000000000000000000000000000000000000));
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/134921


More information about the libc-commits mailing list