[libc-commits] [libc] [llvm] [libc][math] Refactor tanpif to header only (PR #181525)

Xinlong Chen via libc-commits libc-commits at lists.llvm.org
Sat Feb 14 21:21:30 PST 2026


https://github.com/Xinlong-Chen created https://github.com/llvm/llvm-project/pull/181525

Part of https://github.com/llvm/llvm-project/issues/147386

>From b586437cd5821ae2387403ec46fc3a247cdd821d Mon Sep 17 00:00:00 2001
From: xinlongchen <xinlongchen at tencent.com>
Date: Sun, 15 Feb 2026 12:49:48 +0800
Subject: [PATCH] [libc][math] Refactor tanpif implementation to header-only in
 src/__support/math folder.

---
 libc/shared/math.h                            |   1 +
 libc/shared/math/tanpif.h                     |  23 ++++
 libc/src/__support/math/CMakeLists.txt        |  16 +++
 libc/src/__support/math/tanpif.h              | 120 ++++++++++++++++++
 libc/src/math/generic/CMakeLists.txt          |   7 +-
 libc/src/math/generic/tanpif.cpp              |  95 +-------------
 libc/test/shared/CMakeLists.txt               |   1 +
 libc/test/shared/shared_math_test.cpp         |   1 +
 .../llvm-project-overlay/libc/BUILD.bazel     |  22 +++-
 9 files changed, 182 insertions(+), 104 deletions(-)
 create mode 100644 libc/shared/math/tanpif.h
 create mode 100644 libc/src/__support/math/tanpif.h

diff --git a/libc/shared/math.h b/libc/shared/math.h
index e4487d7305e4b..c7128046b44ed 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -124,5 +124,6 @@
 #include "math/tanf.h"
 #include "math/tanhf.h"
 #include "math/tanhf16.h"
+#include "math/tanpif.h"
 
 #endif // LLVM_LIBC_SHARED_MATH_H
diff --git a/libc/shared/math/tanpif.h b/libc/shared/math/tanpif.h
new file mode 100644
index 0000000000000..4c1f691ddb1d2
--- /dev/null
+++ b/libc/shared/math/tanpif.h
@@ -0,0 +1,23 @@
+//===-- Shared tanpif function ----------------------------------*- 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_SHARED_MATH_TANPIF_H
+#define LLVM_LIBC_SHARED_MATH_TANPIF_H
+
+#include "shared/libc_common.h"
+#include "src/__support/math/tanpif.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::tanpif;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SHARED_MATH_TANPIF_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index 8027180e2cf08..84d7719a8f3b7 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1786,3 +1786,19 @@ add_header_library(
     libc.src.__support.macros.optimization
     libc.include.llvm-libc-macros.float16_macros
 )
+
+add_header_library(
+  tanpif
+  HDRS
+    tanpif.h
+  DEPENDS
+    .sincosf16_utils
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.except_value_utils
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.optimization
+)
\ No newline at end of file
diff --git a/libc/src/__support/math/tanpif.h b/libc/src/__support/math/tanpif.h
new file mode 100644
index 0000000000000..36be1bfbec631
--- /dev/null
+++ b/libc/src/__support/math/tanpif.h
@@ -0,0 +1,120 @@
+//===-- Single-precision tanpi 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_TANPIF_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_TANPIF_H
+
+#include "sincosf_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/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace tanpif_internal {
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+LIBC_INLINE_VAR constexpr size_t N_EXCEPTS = 3;
+
+LIBC_INLINE_VAR constexpr fputil::ExceptValues<float, N_EXCEPTS> TANPIF_EXCEPTS{
+    {
+        // (input, RZ output, RU offset, RD offset, RN offset)
+        {0x38F26685, 0x39BE6182, 1, 0, 0},
+        {0x3E933802, 0x3FA267DD, 1, 0, 0},
+        {0x3F3663FF, 0xBFA267DD, 0, 1, 0},
+    }};
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+} // namespace tanpif_internal
+
+LIBC_INLINE float tanpif(float x) {
+  using namespace sincosf_utils_internal;
+  using namespace tanpif_internal;
+
+  using FPBits = typename fputil::FPBits<float>;
+  FPBits xbits(x);
+
+  uint32_t x_u = xbits.uintval();
+  uint32_t x_abs = x_u & 0x7fff'ffffU;
+  double xd = static_cast<double>(xbits.get_val());
+
+  // Handle exceptional values
+  if (LIBC_UNLIKELY(x_abs <= 0x3F3663FF)) {
+    if (LIBC_UNLIKELY(x_abs == 0U))
+      return x;
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+    bool x_sign = x_u >> 31;
+
+    if (auto r = TANPIF_EXCEPTS.lookup_odd(x_abs, x_sign);
+        LIBC_UNLIKELY(r.has_value()))
+      return r.value();
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+  }
+
+  // Numbers greater or equal to 2^23 are always integers, or infinity, or NaN
+  if (LIBC_UNLIKELY(x_abs >= 0x4B00'0000)) {
+    // x is inf or NaN.
+    if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
+      if (xbits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
+
+      if (x_abs == 0x7f80'0000U) {
+        fputil::set_errno_if_required(EDOM);
+        fputil::raise_except_if_required(FE_INVALID);
+      }
+
+      return x + FPBits::quiet_nan().get_val();
+    }
+
+    return FPBits::zero(xbits.sign()).get_val();
+  }
+
+  // Range reduction:
+  // For |x| > 1/32, we perform range reduction as follows:
+  // Find k and y such that:
+  //   x = (k + y) * 1/32
+  //   k is an integer
+  //   |y| < 0.5
+  //
+  // This is done by performing:
+  //   k = round(x * 32)
+  //   y = x * 32 - k
+  //
+  // Once k and y are computed, we then deduce the answer by the formula:
+  // tan(x) = sin(x) / cos(x)
+  //        = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k)
+  double sin_k, cos_k, sin_y, cosm1_y;
+  sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
+
+  if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) {
+    fputil::set_errno_if_required(EDOM);
+    fputil::raise_except_if_required(FE_DIVBYZERO);
+
+    int32_t x_mp5_i = static_cast<int32_t>(xd - 0.5);
+    return FPBits::inf((x_mp5_i & 0x1) ? Sign::NEG : Sign::POS).get_val();
+  }
+
+  using fputil::multiply_add;
+  return fputil::cast<float>(
+      multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) /
+      multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k)));
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_TANPIF_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index b7c1f5a911831..39282a4db978e 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -494,12 +494,7 @@ add_entrypoint_object(
   HDRS
     ../tanpif.h
   DEPENDS
-    libc.src.__support.math.sincosf_utils
-    libc.src.__support.FPUtil.except_value_utils
-    libc.src.__support.FPUtil.fenv_impl
-    libc.src.__support.FPUtil.fp_bits
-    libc.src.__support.FPUtil.multiply_add
-    libc.src.__support.macros.optimization
+    libc.src.__support.math.tanpif
 )
 
 add_entrypoint_object(
diff --git a/libc/src/math/generic/tanpif.cpp b/libc/src/math/generic/tanpif.cpp
index 44df22b517a46..e3568d6e9d35c 100644
--- a/libc/src/math/generic/tanpif.cpp
+++ b/libc/src/math/generic/tanpif.cpp
@@ -7,101 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/tanpif.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/common.h"
-#include "src/__support/macros/config.h"
-#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
-#include "src/__support/math/sincosf_utils.h"
+#include "src/__support/math/tanpif.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-constexpr size_t N_EXCEPTS = 3;
-
-constexpr fputil::ExceptValues<float, N_EXCEPTS> TANPIF_EXCEPTS{{
-    // (input, RZ output, RU offset, RD offset, RN offset)
-    {0x38F26685, 0x39BE6182, 1, 0, 0},
-    {0x3E933802, 0x3FA267DD, 1, 0, 0},
-    {0x3F3663FF, 0xBFA267DD, 0, 1, 0},
-}};
-#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-
-LLVM_LIBC_FUNCTION(float, tanpif, (float x)) {
-  using namespace math::sincosf_utils_internal;
-  using FPBits = typename fputil::FPBits<float>;
-  FPBits xbits(x);
-
-  uint32_t x_u = xbits.uintval();
-  uint32_t x_abs = x_u & 0x7fff'ffffU;
-  double xd = static_cast<double>(xbits.get_val());
-
-  // Handle exceptional values
-  if (LIBC_UNLIKELY(x_abs <= 0x3F3663FF)) {
-    if (LIBC_UNLIKELY(x_abs == 0U))
-      return x;
-
-#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-    bool x_sign = x_u >> 31;
-
-    if (auto r = TANPIF_EXCEPTS.lookup_odd(x_abs, x_sign);
-        LIBC_UNLIKELY(r.has_value()))
-      return r.value();
-#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-  }
-
-  // Numbers greater or equal to 2^23 are always integers, or infinity, or NaN
-  if (LIBC_UNLIKELY(x_abs >= 0x4B00'0000)) {
-    // x is inf or NaN.
-    if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
-      if (xbits.is_signaling_nan()) {
-        fputil::raise_except_if_required(FE_INVALID);
-        return FPBits::quiet_nan().get_val();
-      }
-
-      if (x_abs == 0x7f80'0000U) {
-        fputil::set_errno_if_required(EDOM);
-        fputil::raise_except_if_required(FE_INVALID);
-      }
-
-      return x + FPBits::quiet_nan().get_val();
-    }
-
-    return FPBits::zero(xbits.sign()).get_val();
-  }
-
-  // Range reduction:
-  // For |x| > 1/32, we perform range reduction as follows:
-  // Find k and y such that:
-  //   x = (k + y) * 1/32
-  //   k is an integer
-  //   |y| < 0.5
-  //
-  // This is done by performing:
-  //   k = round(x * 32)
-  //   y = x * 32 - k
-  //
-  // Once k and y are computed, we then deduce the answer by the formula:
-  // tan(x) = sin(x) / cos(x)
-  //        = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k)
-  double sin_k, cos_k, sin_y, cosm1_y;
-  sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
-
-  if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) {
-    fputil::set_errno_if_required(EDOM);
-    fputil::raise_except_if_required(FE_DIVBYZERO);
-
-    int32_t x_mp5_i = static_cast<int32_t>(xd - 0.5);
-    return FPBits::inf((x_mp5_i & 0x1) ? Sign::NEG : Sign::POS).get_val();
-  }
-
-  using fputil::multiply_add;
-  return fputil::cast<float>(
-      multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) /
-      multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k)));
-}
+LLVM_LIBC_FUNCTION(float, tanpif, (float x)) { return math::tanpif(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/shared/CMakeLists.txt b/libc/test/shared/CMakeLists.txt
index 1a0559af51acd..9fd3edfca1af9 100644
--- a/libc/test/shared/CMakeLists.txt
+++ b/libc/test/shared/CMakeLists.txt
@@ -121,4 +121,5 @@ add_fp_unittest(
     libc.src.__support.math.tanf
     libc.src.__support.math.tanhf
     libc.src.__support.math.tanhf16
+    libc.src.__support.math.tanpif
 )
diff --git a/libc/test/shared/shared_math_test.cpp b/libc/test/shared/shared_math_test.cpp
index fc6e6c74b3720..cb3893f81d9e0 100644
--- a/libc/test/shared/shared_math_test.cpp
+++ b/libc/test/shared/shared_math_test.cpp
@@ -129,6 +129,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat) {
   EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::sqrtf(0.0f));
   EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::tanf(0.0f));
   EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::tanhf(0.0f));
+  EXPECT_FP_EQ(0.0f, LIBC_NAMESPACE::shared::tanpif(0.0f));
 }
 
 TEST(LlvmLibcSharedMathTest, AllDouble) {
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 0eaff89f04215..5862864830c89 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3982,6 +3982,22 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_math_tanpif",
+    hdrs = ["src/__support/math/tanpif.h"],
+    deps = [
+        ":__support_common",
+        ":__support_fputil_cast",
+        ":__support_fputil_except_value_utils",
+        ":__support_fputil_fenv_impl",
+        ":__support_fputil_fp_bits",
+        ":__support_fputil_multiply_add",
+        ":__support_macros_config",
+        ":__support_macros_optimization",
+        ":__support_sincosf_utils",
+    ],
+)
+
 ############################### complex targets ################################
 
 libc_function(
@@ -5495,11 +5511,7 @@ libc_math_function(
 libc_math_function(
     name = "tanpif",
     additional_deps = [
-        ":__support_sincosf_utils",
-        ":hdr_fenv_macros",
-        ":__support_macros_config",
-        ":__support_macros_optimization",
-        ":__support_fputil_multiply_add",
+        ":__support_math_tanpif",
     ],
 )
 



More information about the libc-commits mailing list