[libc-commits] [libc] [libc][math][c23] add sinpi function (PR #129379)

via libc-commits libc-commits at lists.llvm.org
Tue Mar 11 18:18:59 PDT 2025


https://github.com/lara254 updated https://github.com/llvm/llvm-project/pull/129379

>From b4d31d0b5c0303e25054dcb43fcd1db41684f1c7 Mon Sep 17 00:00:00 2001
From: Job Hernandez <lara2993 at proton.me>
Date: Sat, 1 Mar 2025 03:29:20 -0800
Subject: [PATCH 1/6] add draft for sinpi

---
 libc/src/math/generic/sinpi.cpp | 63 +++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 libc/src/math/generic/sinpi.cpp

diff --git a/libc/src/math/generic/sinpi.cpp b/libc/src/math/generic/sinpi.cpp
new file mode 100644
index 0000000000000..69116ced9fbc7
--- /dev/null
+++ b/libc/src/math/generic/sinpi.cpp
@@ -0,0 +1,63 @@
+//===-- double-precision sinpi 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/sinpi.h"
+#include "sincos_eval.h"
+#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/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(double, sinpi, (double x)) {
+  using FPBits = typename fputil::FPBits<double>;
+  FPBits xbits(x);
+
+  uint64_t x_u = xbits.uintval();
+  uint64_t x_abs = x_u & 0x7fff'ffffU;
+  long double xd = static_cast<long double>(x);
+
+  if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
+    if (x_abs == 0x7c00) {
+      fputil::set_errno_if_required(EDOM);
+      fputil::raise_except_if_required(FE_INVALID);
+    }
+    return x + FPBits::quiet_nan().get_val();
+  } else {
+    return FPBits::zero(xbits.sign()).get_val();
+  }
+
+  if (LIBC_UNLIKELY(x_abs <= 0x3d80'0000U)) {
+    if (LIBC_UNLIKELY(x_abs < 0x33CD'01D7U)) {
+      if (LIBC_UNLIKELY(x_abs == 0U)) {
+	return x;
+    }
+      long double xdpi = xd * 0x1.921fb5444d18p1;
+       return  static_cast<double>(xdpi);
+    }
+  long double xsq = xd * xd;
+
+  long double result = fputil::polyeval(xsq,0x1.921fb54442d183f07b2385653d8p1, -0x1.4abbce625bd95cdc955aeed9abcp2, 0x1.466bc6769ddfdb085486c0ff3ep1, -0x1.32d2c4a48bfd71fa9cdf60a0e4p-1,  0x1.502cbd2c72e3168ff209bc7656cp-4);
+
+  return static_cast<double>(xd * result);
+  }
+
+  long double sin_k, cos_k, sin_y, cosm1_y;
+  sincospi_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
+
+  if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
+    return FPBits::zero(xbits.sign()).get_val();
+
+  return static_cast<double>(fputil::multiply_add(
+						  sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
+}
+}

>From b85e0dcef49a06f3054cea2882f186c3b78a20a7 Mon Sep 17 00:00:00 2001
From: Job Hernandez <lara2993 at proton.me>
Date: Tue, 11 Mar 2025 11:27:01 -0700
Subject: [PATCH 2/6] add outline

---
 libc/src/math/generic/sinpi.cpp | 46 ++++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 10 deletions(-)

diff --git a/libc/src/math/generic/sinpi.cpp b/libc/src/math/generic/sinpi.cpp
index 69116ced9fbc7..f343e0b74aa4d 100644
--- a/libc/src/math/generic/sinpi.cpp
+++ b/libc/src/math/generic/sinpi.cpp
@@ -8,6 +8,7 @@
 
 #include "src/math/sinpi.h"
 #include "sincos_eval.h"
+#include "src/__support/FPUtil/double_double.h"
 #include "src/__support/FPUtil/FEnvImpl.h"
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/PolyEval.h"
@@ -15,18 +16,43 @@
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+#include "src/math/fmul.h"
 
 namespace LIBC_NAMESPACE_DECL {
+  
+static LIB_INLINE int64_t range_reduction_sincospi(double x, double &y) {
+  double b = 32.0;
+  
+  //fputil::DoubleDouble prod = fputil::exact_mult(x, b);
+  //using DoubleBits = fputil::FPBits<double>;
+  //using DoubleStorageType = typename DoubleBits::StorageType;
+  
+  
+  float result = fmul(x, b);
+  double res = static_cast<double>(result);
+  fputil::DoubleDouble sum = fputil::exact_add(res, -res);
+  double sum_result = sum.hi;
 
+  // do the exceptions here...
+
+  return static_cast<int64_t>(sum_result);
+}
+  
+LIBC_INLINE void sincospi_eval(double xd, double &sin_k, double &cos_k, double &sin_k, double &cosm1_y) {
+    double y;
+    int64_t k = range_reduction_sincospi(xd, y);
+    sincospi_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
+  }
+  
 LLVM_LIBC_FUNCTION(double, sinpi, (double x)) {
   using FPBits = typename fputil::FPBits<double>;
   FPBits xbits(x);
 
   uint64_t x_u = xbits.uintval();
   uint64_t x_abs = x_u & 0x7fff'ffffU;
-  long double xd = static_cast<long double>(x);
 
   if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
+    // If value is equal to infinity
     if (x_abs == 0x7c00) {
       fputil::set_errno_if_required(EDOM);
       fputil::raise_except_if_required(FE_INVALID);
@@ -41,23 +67,23 @@ LLVM_LIBC_FUNCTION(double, sinpi, (double x)) {
       if (LIBC_UNLIKELY(x_abs == 0U)) {
 	return x;
     }
-      long double xdpi = xd * 0x1.921fb5444d18p1;
-       return  static_cast<double>(xdpi);
+      double  xdpi = xd * 0x1.921fb5444d18p1;
+      return xdpi;
+      
     }
-  long double xsq = xd * xd;
-
-  long double result = fputil::polyeval(xsq,0x1.921fb54442d183f07b2385653d8p1, -0x1.4abbce625bd95cdc955aeed9abcp2, 0x1.466bc6769ddfdb085486c0ff3ep1, -0x1.32d2c4a48bfd71fa9cdf60a0e4p-1,  0x1.502cbd2c72e3168ff209bc7656cp-4);
+    double xsq = xd * xd;
+    // todo: generate a new polynomial using double precision
+    double result = fputil::polyeval(xsq,0x1.921fb54442d183f07b2385653d8p1, -0x1.4abbce625bd95cdc955aeed9abcp2, 0x1.466bc6769ddfdb085486c0ff3ep1, -0x1.32d2c4a48bfd71fa9cdf60a0e4p-1,  0x1.502cbd2c72e3168ff209bc7656cp-4);
 
-  return static_cast<double>(xd * result);
+  return (xd * result);
   }
 
-  long double sin_k, cos_k, sin_y, cosm1_y;
+  double sin_k, cos_k, sin_y, cosm1_y;
   sincospi_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
 
   if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
     return FPBits::zero(xbits.sign()).get_val();
 
-  return static_cast<double>(fputil::multiply_add(
-						  sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
+  return fputil::multiply_add(sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
 }
 }

>From a8dae3106a4186095ec24908ac3b0905d3e9f8a3 Mon Sep 17 00:00:00 2001
From: Job Hernandez <lara2993 at proton.me>
Date: Tue, 11 Mar 2025 12:18:14 -0700
Subject: [PATCH 3/6] fix bug in range reduction

---
 libc/src/math/generic/sinpi.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/libc/src/math/generic/sinpi.cpp b/libc/src/math/generic/sinpi.cpp
index f343e0b74aa4d..55d2a9d350e5d 100644
--- a/libc/src/math/generic/sinpi.cpp
+++ b/libc/src/math/generic/sinpi.cpp
@@ -29,13 +29,13 @@ static LIB_INLINE int64_t range_reduction_sincospi(double x, double &y) {
   
   
   float result = fmul(x, b);
-  double res = static_cast<double>(result);
-  fputil::DoubleDouble sum = fputil::exact_add(res, -res);
-  double sum_result = sum.hi;
+  double k = static_cast<double>(result);
+  fputil::DoubleDouble y = fputil::exact_add(res, -res);
+  double y = sum.hi;
 
   // do the exceptions here...
 
-  return static_cast<int64_t>(sum_result);
+  return static_cast<int64_t>(k);
 }
   
 LIBC_INLINE void sincospi_eval(double xd, double &sin_k, double &cos_k, double &sin_k, double &cosm1_y) {

>From 9cd84a103de81438b06803feeba4a1ad2f431096 Mon Sep 17 00:00:00 2001
From: Job Hernandez <lara2993 at proton.me>
Date: Tue, 11 Mar 2025 14:01:29 -0700
Subject: [PATCH 4/6] improve styling

---
 libc/src/math/generic/sinpi.cpp | 40 +++++++++++----------------------
 1 file changed, 13 insertions(+), 27 deletions(-)

diff --git a/libc/src/math/generic/sinpi.cpp b/libc/src/math/generic/sinpi.cpp
index 55d2a9d350e5d..c946f7749b678 100644
--- a/libc/src/math/generic/sinpi.cpp
+++ b/libc/src/math/generic/sinpi.cpp
@@ -13,6 +13,7 @@
 #include "src/__support/FPUtil/FPBits.h"
 #include "src/__support/FPUtil/PolyEval.h"
 #include "src/__support/FPUtil/multiply_add.h"
+#include "src/__support/FPUtil/BasicOperations.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
@@ -49,33 +50,18 @@ LLVM_LIBC_FUNCTION(double, sinpi, (double x)) {
   FPBits xbits(x);
 
   uint64_t x_u = xbits.uintval();
-  uint64_t x_abs = x_u & 0x7fff'ffffU;
-
-  if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
-    // If value is equal to infinity
-    if (x_abs == 0x7c00) {
-      fputil::set_errno_if_required(EDOM);
-      fputil::raise_except_if_required(FE_INVALID);
-    }
-    return x + FPBits::quiet_nan().get_val();
-  } else {
-    return FPBits::zero(xbits.sign()).get_val();
+  double x_abs = fputil::abs(x);
+  double p = 0x1p52; // precision = 52; 2^p
+  
+  if (x_abs >= p) {
+    if (xbits.is_nan())
+      return x;
+    if  (x.bits.is_inf()) {
+    fputil::set_errno_if_required(EDOM);
+    fputil::raise_except_if_required(FE_INVALID);
+    return FPBits::quiet_nan().get_val();
   }
-
-  if (LIBC_UNLIKELY(x_abs <= 0x3d80'0000U)) {
-    if (LIBC_UNLIKELY(x_abs < 0x33CD'01D7U)) {
-      if (LIBC_UNLIKELY(x_abs == 0U)) {
-	return x;
-    }
-      double  xdpi = xd * 0x1.921fb5444d18p1;
-      return xdpi;
-      
-    }
-    double xsq = xd * xd;
-    // todo: generate a new polynomial using double precision
-    double result = fputil::polyeval(xsq,0x1.921fb54442d183f07b2385653d8p1, -0x1.4abbce625bd95cdc955aeed9abcp2, 0x1.466bc6769ddfdb085486c0ff3ep1, -0x1.32d2c4a48bfd71fa9cdf60a0e4p-1,  0x1.502cbd2c72e3168ff209bc7656cp-4);
-
-  return (xd * result);
+    return FPBits::zero(xbits.sign()).get_val();
   }
 
   double sin_k, cos_k, sin_y, cosm1_y;
@@ -86,4 +72,4 @@ LLVM_LIBC_FUNCTION(double, sinpi, (double x)) {
 
   return fputil::multiply_add(sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
 }
-}
+

>From ad06de0756f5ea156ede5aef4475f01fc701c4ee Mon Sep 17 00:00:00 2001
From: Job Hernandez <lara2993 at proton.me>
Date: Tue, 11 Mar 2025 17:38:57 -0700
Subject: [PATCH 5/6] add tests

---
 libc/config/darwin/arm/entrypoints.txt  |  1 +
 libc/include/math.yaml                  |  6 +++
 libc/src/math/CMakeLists.txt            |  2 +
 libc/src/math/generic/CMakeLists.txt    | 21 +++++++++-
 libc/src/math/generic/sinpi.cpp         | 44 +++++++--------------
 libc/src/math/sinpi.h                   | 20 ++++++++++
 libc/test/src/math/CMakeLists.txt       | 15 ++++++++
 libc/test/src/math/sinpi_test.cpp       | 40 +++++++++++++++++++
 libc/test/src/math/smoke/CMakeLists.txt | 12 ++++++
 libc/test/src/math/smoke/sinpi_test.cpp | 51 +++++++++++++++++++++++++
 10 files changed, 180 insertions(+), 32 deletions(-)
 create mode 100644 libc/src/math/sinpi.h
 create mode 100644 libc/test/src/math/sinpi_test.cpp
 create mode 100644 libc/test/src/math/smoke/sinpi_test.cpp

diff --git a/libc/config/darwin/arm/entrypoints.txt b/libc/config/darwin/arm/entrypoints.txt
index 7972d285f963a..9ba935670d915 100644
--- a/libc/config/darwin/arm/entrypoints.txt
+++ b/libc/config/darwin/arm/entrypoints.txt
@@ -253,6 +253,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.sin
     libc.src.math.sinf
     libc.src.math.sinpif
+    libc.src.math.sinpi
     libc.src.math.sqrt
     libc.src.math.sqrtf
     libc.src.math.sqrtl
diff --git a/libc/include/math.yaml b/libc/include/math.yaml
index a66f981030864..5623ff0a73c2a 100644
--- a/libc/include/math.yaml
+++ b/libc/include/math.yaml
@@ -2496,6 +2496,12 @@ functions:
     arguments:
       - type: const long double *
       - type: const long double *
+  - name: sinpi
+    standards:
+      - stdc
+    return_type: double
+    arguments:
+      - type: double
   - name: totalordermag
     standards:
       - stdc
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index f18a73d46f9aa..94f9c24cd9fb0 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -490,6 +490,8 @@ add_math_entrypoint_object(sincosf)
 add_math_entrypoint_object(sin)
 add_math_entrypoint_object(sinf)
 add_math_entrypoint_object(sinf16)
+
+add_math_entrypoint_object(sinpi)
 add_math_entrypoint_object(sinpif)
 add_math_entrypoint_object(sinpif16)
 
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 3114289bad486..c0b024e123dd3 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1,5 +1,3 @@
-
-
 add_entrypoint_object(
   canonicalize
   SRCS
@@ -495,6 +493,25 @@ add_entrypoint_object(
     libc.src.__support.macros.optimization
 )
 
+add_entrypoint_object(
+  sinpi
+  SRCS
+    sinpi.cpp
+  HDRS
+    ../sinpi.h
+  DEPENDS
+    .sincosf_utils
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.fma
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.polyeval
+    libc.src.__support.common
+    libc.src.__support.macros.optimization
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.double_double
+)
+
 add_entrypoint_object(
   sinpif
   SRCS
diff --git a/libc/src/math/generic/sinpi.cpp b/libc/src/math/generic/sinpi.cpp
index c946f7749b678..5fab152646100 100644
--- a/libc/src/math/generic/sinpi.cpp
+++ b/libc/src/math/generic/sinpi.cpp
@@ -14,62 +14,46 @@
 #include "src/__support/FPUtil/PolyEval.h"
 #include "src/__support/FPUtil/multiply_add.h"
 #include "src/__support/FPUtil/BasicOperations.h"
+#include "src/math/pow.h"
+#include "src/math/generic/sincosf_utils.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
 #include "src/math/fmul.h"
 
 namespace LIBC_NAMESPACE_DECL {
-  
-static LIB_INLINE int64_t range_reduction_sincospi(double x, double &y) {
-  double b = 32.0;
-  
-  //fputil::DoubleDouble prod = fputil::exact_mult(x, b);
-  //using DoubleBits = fputil::FPBits<double>;
-  //using DoubleStorageType = typename DoubleBits::StorageType;
-  
-  
-  float result = fmul(x, b);
-  double k = static_cast<double>(result);
-  fputil::DoubleDouble y = fputil::exact_add(res, -res);
-  double y = sum.hi;
-
-  // do the exceptions here...
-
-  return static_cast<int64_t>(k);
-}
-  
-LIBC_INLINE void sincospi_eval(double xd, double &sin_k, double &cos_k, double &sin_k, double &cosm1_y) {
-    double y;
-    int64_t k = range_reduction_sincospi(xd, y);
-    sincospi_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
-  }
-  
+ 
 LLVM_LIBC_FUNCTION(double, sinpi, (double x)) {
   using FPBits = typename fputil::FPBits<double>;
   FPBits xbits(x);
 
-  uint64_t x_u = xbits.uintval();
+  //uint64_t x_u = xbits.uintval();
   double x_abs = fputil::abs(x);
   double p = 0x1p52; // precision = 52; 2^p
+
+  if (LIBC_UNLIKELY(x_abs == 0U))
+    return x;
   
   if (x_abs >= p) {
     if (xbits.is_nan())
       return x;
-    if  (x.bits.is_inf()) {
+    if  (xbits.is_inf()) {
     fputil::set_errno_if_required(EDOM);
     fputil::raise_except_if_required(FE_INVALID);
     return FPBits::quiet_nan().get_val();
   }
     return FPBits::zero(xbits.sign()).get_val();
   }
-
+  double n = pow(2, -52);
+  double k = fputil::nearest_integer(x * n);
+  double y = x - k;
   double sin_k, cos_k, sin_y, cosm1_y;
-  sincospi_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
+  sincosf_poly_eval(k, y,  sin_k, cos_k, sin_y, cosm1_y);
 
   if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
     return FPBits::zero(xbits.sign()).get_val();
 
-  return fputil::multiply_add(sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
+  return fputil::multiply_add(sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k));
+}
 }
 
diff --git a/libc/src/math/sinpi.h b/libc/src/math/sinpi.h
new file mode 100644
index 0000000000000..f4c9d2f9a8a7b
--- /dev/null
+++ b/libc/src/math/sinpi.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for sinpi -------------------------*- 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_SINPI_H
+#define LLVM_LIBC_SRC_MATH_SINPI_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+double sinpi(double x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_SINPI_H
diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index 53ddd301900c0..7c3a4419b4e7f 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -106,6 +106,21 @@ add_fp_unittest(
   DEPENDS
     libc.src.math.sinf16
 )
+add_fp_unittest(
+  sinpi_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    sinpi_test.cpp
+  HDRS
+    sdcomp26094.h
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.math.sinpi
+    libc.src.__support.CPP.array
+    libc.src.__support.FPUtil.fp_bits
+)
 
 add_fp_unittest(
   sinpif_test
diff --git a/libc/test/src/math/sinpi_test.cpp b/libc/test/src/math/sinpi_test.cpp
new file mode 100644
index 0000000000000..a5ea0c1b9528a
--- /dev/null
+++ b/libc/test/src/math/sinpi_test.cpp
@@ -0,0 +1,40 @@
+//===-- Exhaustive test 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
+//
+//===---------------------------------------------------------------------===//
+
+#include "src/math/sinpi.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+
+using LlvmLibcSinpiTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
+
+// Range: [0, Inf]
+static constexpr uint64_t POS_START = 0x0000U;
+static constexpr uint64_t POS_STOP = 0x7c00U;
+
+// Range: [-Inf, 0]
+static constexpr uint16_t NEG_START = 0x8000U;
+static constexpr uint16_t NEG_STOP = 0xfc00U;
+
+TEST_F(LlvmLibcSinpif16Test, PositiveRange) {
+  for (uint64_t v = POS_START; v <= POS_STOP; ++v) {
+    double x = FPBits(v).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sinpi, x,
+                                   LIBC_NAMESPACE::sinpi(x), 0.5);
+  }
+}
+
+TEST_F(LlvmLibcSinpif16Test, NegativeRange) {
+  for (uint64_t v = NEG_START; v <= NEG_STOP; ++v) {
+    double x = FPBits(v).get_val();
+    EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Sinpi, x,
+                                   LIBC_NAMESPACE::sinpi(x), 0.5);
+  }
+}
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 6f94440d826d9..9892f0b4dfa47 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -73,6 +73,18 @@ add_fp_unittest(
     libc.src.__support.FPUtil.cast
 )
 
+add_fp_unittest(
+  sinpi_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    sinpi_test.cpp
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.math.sinpif
+    libc.src.__support.CPP.array
+    libc.src.__support.FPUtil.fp_bits
+)
 add_fp_unittest(
   sinpif_test
   SUITE
diff --git a/libc/test/src/math/smoke/sinpi_test.cpp b/libc/test/src/math/smoke/sinpi_test.cpp
new file mode 100644
index 0000000000000..a4ecffb19a236
--- /dev/null
+++ b/libc/test/src/math/smoke/sinpi_test.cpp
@@ -0,0 +1,51 @@
+//===-- Unittests for sinpi -----------------------------------------------===//
+//
+// 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/sinpi.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcSinpiTest = LIBC_NAMESPACE::testing::FPTest<double>;
+
+TEST_F(LlvmLibcSinpiTest, SpecialNumbers) {
+  LIBC_NAMESPACE::libc_errno = 0;
+
+  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinpi(aNaN));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpi(zero));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpi(neg_zero));
+  EXPECT_MATH_ERRNO(0);
+
+  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinpi(inf));
+  EXPECT_MATH_ERRNO(EDOM);
+
+  EXPECT_FP_EQ(aNaN, LIBC_NAMESPACE::sinpi(neg_inf));
+  EXPECT_MATH_ERRNO(EDOM);
+}
+
+TEST_F(LlvmLibcSinpiTest, Integers) {
+  EXPECT_FP_EQ(neg_zero,
+               LIBC_NAMESPACE::sinpi(
+                   LIBC_NAMESPACE::fputil::cast<double>(-0x420.0p0)));
+  EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpi(
+                             LIBC_NAMESPACE::fputil::cast<double>(-0x1p+10)));
+  EXPECT_FP_EQ(neg_zero,
+               LIBC_NAMESPACE::sinpi(
+                   LIBC_NAMESPACE::fputil::cast<double>(-0x1.4p+14)));
+  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpi(
+                         LIBC_NAMESPACE::fputil::cast<double>(0x420.0p0)));
+  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpi(
+                         LIBC_NAMESPACE::fputil::cast<double>(0x1.cp+15)));
+  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpi(
+                         LIBC_NAMESPACE::fputil::cast<double>(0x1.cp+7)));
+}

>From e26a0ab1d243e4c3adcef1c40cfaad1f4457884f Mon Sep 17 00:00:00 2001
From: Job Hernandez <lara2993 at proton.me>
Date: Tue, 11 Mar 2025 18:18:44 -0700
Subject: [PATCH 6/6] make tests pass

---
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/src/math/generic/CMakeLists.txt      |  1 +
 libc/src/math/generic/sinpi.cpp           |  4 +++-
 libc/test/src/math/smoke/CMakeLists.txt   |  2 +-
 libc/test/src/math/smoke/sinpi_test.cpp   | 26 +++++++++++------------
 5 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 6c2be4d3b0b99..99969de5f0352 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -605,6 +605,7 @@ set(TARGET_LIBM_ENTRYPOINTS
     libc.src.math.sinf
     libc.src.math.sinhf
     libc.src.math.sinpif
+    libc.src.math.sinpi
     libc.src.math.sqrt
     libc.src.math.sqrtf
     libc.src.math.sqrtl
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index c0b024e123dd3..5df1b7ebe56a4 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -506,6 +506,7 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.fma
     libc.src.__support.FPUtil.multiply_add
     libc.src.__support.FPUtil.polyeval
+    libc.src.math.pow
     libc.src.__support.common
     libc.src.__support.macros.optimization
     libc.src.__support.FPUtil.basic_operations
diff --git a/libc/src/math/generic/sinpi.cpp b/libc/src/math/generic/sinpi.cpp
index 5fab152646100..f05839faae1c3 100644
--- a/libc/src/math/generic/sinpi.cpp
+++ b/libc/src/math/generic/sinpi.cpp
@@ -46,9 +46,11 @@ LLVM_LIBC_FUNCTION(double, sinpi, (double x)) {
   }
   double n = pow(2, -52);
   double k = fputil::nearest_integer(x * n);
+  FPBits kbits(x);
+  uint64_t ku = kbits.uintval();
   double y = x - k;
   double sin_k, cos_k, sin_y, cosm1_y;
-  sincosf_poly_eval(k, y,  sin_k, cos_k, sin_y, cosm1_y);
+  sincosf_poly_eval(ku, y,  sin_k, cos_k, sin_y, cosm1_y);
 
   if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
     return FPBits::zero(xbits.sign()).get_val();
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 9892f0b4dfa47..fee94905b4905 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -81,7 +81,7 @@ add_fp_unittest(
     sinpi_test.cpp
   DEPENDS
     libc.src.errno.errno
-    libc.src.math.sinpif
+    libc.src.math.sinpi
     libc.src.__support.CPP.array
     libc.src.__support.FPUtil.fp_bits
 )
diff --git a/libc/test/src/math/smoke/sinpi_test.cpp b/libc/test/src/math/smoke/sinpi_test.cpp
index a4ecffb19a236..0e5ec3d8ab6c8 100644
--- a/libc/test/src/math/smoke/sinpi_test.cpp
+++ b/libc/test/src/math/smoke/sinpi_test.cpp
@@ -34,18 +34,16 @@ TEST_F(LlvmLibcSinpiTest, SpecialNumbers) {
 }
 
 TEST_F(LlvmLibcSinpiTest, Integers) {
-  EXPECT_FP_EQ(neg_zero,
-               LIBC_NAMESPACE::sinpi(
-                   LIBC_NAMESPACE::fputil::cast<double>(-0x420.0p0)));
-  EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpi(
-                             LIBC_NAMESPACE::fputil::cast<double>(-0x1p+10)));
-  EXPECT_FP_EQ(neg_zero,
-               LIBC_NAMESPACE::sinpi(
-                   LIBC_NAMESPACE::fputil::cast<double>(-0x1.4p+14)));
-  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpi(
-                         LIBC_NAMESPACE::fputil::cast<double>(0x420.0p0)));
-  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpi(
-                         LIBC_NAMESPACE::fputil::cast<double>(0x1.cp+15)));
-  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpi(
-                         LIBC_NAMESPACE::fputil::cast<double>(0x1.cp+7)));
+  EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpi(-0x1.0000000000003p52));
+                   
+  EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpi(-0x1.0000000000005p52));
+                             
+  EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::sinpi(-0x1.0000000000006p52));
+                   
+  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpi(0x1.0000000000003p52));
+                         
+  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpi(0x1.0000000000005p52));
+                         
+  EXPECT_FP_EQ(zero, LIBC_NAMESPACE::sinpi(0x1.0000000000006p52));
+                         
 }



More information about the libc-commits mailing list