Skip to content

[libc][math] Refactor coshf16 implementation to header-only in src/__support/math folder. #153582

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bassiounix
Copy link
Contributor

@bassiounix bassiounix commented Aug 14, 2025

@bassiounix bassiounix added bazel "Peripheral" support tier build system: utils/bazel libc labels Aug 14, 2025 — with Graphite App
@bassiounix bassiounix requested a review from lntue August 14, 2025 14:15
@bassiounix bassiounix marked this pull request as ready for review August 14, 2025 14:15
@llvmbot
Copy link
Member

llvmbot commented Aug 14, 2025

@llvm/pr-subscribers-libc

Author: Muhammad Bassiouni (bassiounix)

Changes

Part of #147386

in preparation for: https://discourse.llvm.org/t/rfc-make-clang-builtin-math-functions-constexpr-with-llvm-libc-to-support-c-23-constexpr-math-functions/86450


Patch is 31.45 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/153582.diff

18 Files Affected:

  • (modified) libc/shared/math.h (+1)
  • (added) libc/shared/math/coshf16.h (+28)
  • (modified) libc/src/__support/math/CMakeLists.txt (+28)
  • (added) libc/src/__support/math/coshf16.h (+124)
  • (renamed) libc/src/__support/math/expxf16_utils.h (+15-6)
  • (modified) libc/src/math/generic/CMakeLists.txt (+9-31)
  • (modified) libc/src/math/generic/coshf16.cpp (+2-97)
  • (modified) libc/src/math/generic/exp2f16.cpp (+2-1)
  • (modified) libc/src/math/generic/exp2m1f16.cpp (+2-1)
  • (modified) libc/src/math/generic/expm1f16.cpp (+2-1)
  • (modified) libc/src/math/generic/log10f16.cpp (+2-1)
  • (modified) libc/src/math/generic/log2f16.cpp (+2-1)
  • (modified) libc/src/math/generic/logf16.cpp (+2-1)
  • (modified) libc/src/math/generic/sinhf16.cpp (+2-1)
  • (modified) libc/src/math/generic/tanhf16.cpp (+2-1)
  • (modified) libc/test/shared/CMakeLists.txt (+1)
  • (modified) libc/test/shared/shared_math_test.cpp (+1)
  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+36-22)
diff --git a/libc/shared/math.h b/libc/shared/math.h
index c582877563f98..a89aa90ee1ef8 100644
--- a/libc/shared/math.h
+++ b/libc/shared/math.h
@@ -36,6 +36,7 @@
 #include "math/cosf.h"
 #include "math/cosf16.h"
 #include "math/coshf.h"
+#include "math/coshf16.h"
 #include "math/erff.h"
 #include "math/exp.h"
 #include "math/exp10.h"
diff --git a/libc/shared/math/coshf16.h b/libc/shared/math/coshf16.h
new file mode 100644
index 0000000000000..66e8d1431df65
--- /dev/null
+++ b/libc/shared/math/coshf16.h
@@ -0,0 +1,28 @@
+//===-- Shared coshf16 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_COSHF16_H
+#define LLVM_LIBC_SHARED_MATH_COSHF16_H
+
+#include "shared/libc_common.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/math/coshf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace shared {
+
+using math::coshf16;
+
+} // namespace shared
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SHARED_MATH_COSHF16_H
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index e249af93b36c1..cddf336369973 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -419,6 +419,19 @@ add_header_library(
     libc.src.__support.macros.optimization
 )
 
+add_header_library(
+  coshf16
+  HDRS
+    coshf16.h
+  DEPENDS
+    .expxf16_utils
+    libc.src.__support.FPUtil.except_value_utils
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.rounding_mode
+    libc.src.__support.macros.optimization
+)
+
 add_header_library(
   erff
   HDRS
@@ -489,6 +502,21 @@ add_header_library(
     libc.include.llvm-libc-macros.float16_macros
 )
 
+add_header_library(
+  expxf16_utils
+  HDRS
+    expxf16_utils.h
+  DEPENDS
+    libc.hdr.stdint_proxy
+    libc.src.__support.FPUtil.fp_bits
+    libc.src.__support.FPUtil.cast
+    libc.src.__support.FPUtil.multiply_add
+    libc.src.__support.FPUtil.nearest_integer
+    libc.src.__support.macros.attributes
+    libc.src.__support.math.expf16_utils
+    libc.src.__support.math.exp10_float16_constants
+)
+
 add_header_library(
   frexpf128
   HDRS
diff --git a/libc/src/__support/math/coshf16.h b/libc/src/__support/math/coshf16.h
new file mode 100644
index 0000000000000..4c96a78fa5254
--- /dev/null
+++ b/libc/src/__support/math/coshf16.h
@@ -0,0 +1,124 @@
+//===-- Implementation header for coshf16 -----------------------*- 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_SRC___SUPPORT_MATH_COSHF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_COSHF16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "expxf16_utils.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/except_value_utils.h"
+#include "src/__support/FPUtil/rounding_mode.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+LIBC_INLINE static constexpr float16 coshf16(float16 x) {
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+  constexpr fputil::ExceptValues<float16, 9> COSHF16_EXCEPTS_POS = {{
+      // x = 0x1.6ap-5, coshf16(x) = 0x1p+0 (RZ)
+      {0x29a8U, 0x3c00U, 1U, 0U, 1U},
+      // x = 0x1.8c4p+0, coshf16(x) = 0x1.3a8p+1 (RZ)
+      {0x3e31U, 0x40eaU, 1U, 0U, 0U},
+      // x = 0x1.994p+0, coshf16(x) = 0x1.498p+1 (RZ)
+      {0x3e65U, 0x4126U, 1U, 0U, 0U},
+      // x = 0x1.b6p+0, coshf16(x) = 0x1.6d8p+1 (RZ)
+      {0x3ed8U, 0x41b6U, 1U, 0U, 1U},
+      // x = 0x1.aap+1, coshf16(x) = 0x1.be8p+3 (RZ)
+      {0x42a8U, 0x4afaU, 1U, 0U, 1U},
+      // x = 0x1.cc4p+1, coshf16(x) = 0x1.23cp+4 (RZ)
+      {0x4331U, 0x4c8fU, 1U, 0U, 0U},
+      // x = 0x1.288p+2, coshf16(x) = 0x1.9b4p+5 (RZ)
+      {0x44a2U, 0x526dU, 1U, 0U, 0U},
+      // x = 0x1.958p+2, coshf16(x) = 0x1.1a4p+8 (RZ)
+      {0x4656U, 0x5c69U, 1U, 0U, 0U},
+      // x = 0x1.5fp+3, coshf16(x) = 0x1.c54p+14 (RZ)
+      {0x497cU, 0x7715U, 1U, 0U, 1U},
+  }};
+
+  constexpr fputil::ExceptValues<float16, 6> COSHF16_EXCEPTS_NEG = {{
+      // x = -0x1.6ap-5, coshf16(x) = 0x1p+0 (RZ)
+      {0xa9a8U, 0x3c00U, 1U, 0U, 1U},
+      // x = -0x1.b6p+0, coshf16(x) = 0x1.6d8p+1 (RZ)
+      {0xbed8U, 0x41b6U, 1U, 0U, 1U},
+      // x = -0x1.288p+2, coshf16(x) = 0x1.9b4p+5 (RZ)
+      {0xc4a2U, 0x526dU, 1U, 0U, 0U},
+      // x = -0x1.5fp+3, coshf16(x) = 0x1.c54p+14 (RZ)
+      {0xc97cU, 0x7715U, 1U, 0U, 1U},
+      // x = -0x1.8c4p+0, coshf16(x) = 0x1.3a8p+1 (RZ)
+      {0xbe31U, 0x40eaU, 1U, 0U, 0U},
+      // x = -0x1.994p+0, coshf16(x) = 0x1.498p+1 (RZ)
+      {0xbe65U, 0x4126U, 1U, 0U, 0U},
+  }};
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+  using namespace expxf16_internal;
+  using FPBits = fputil::FPBits<float16>;
+  FPBits x_bits(x);
+
+  uint16_t x_u = x_bits.uintval();
+  uint16_t x_abs = x_u & 0x7fffU;
+
+  // When |x| >= acosh(2^16), or x is NaN.
+  if (LIBC_UNLIKELY(x_abs >= 0x49e5U)) {
+    // cosh(NaN) = NaN
+    if (x_bits.is_nan()) {
+      if (x_bits.is_signaling_nan()) {
+        fputil::raise_except_if_required(FE_INVALID);
+        return FPBits::quiet_nan().get_val();
+      }
+
+      return x;
+    }
+
+    // When |x| >= acosh(2^16).
+    if (x_abs >= 0x49e5U) {
+      // cosh(+/-inf) = +inf
+      if (x_bits.is_inf())
+        return FPBits::inf().get_val();
+
+      switch (fputil::quick_get_round()) {
+      case FE_TONEAREST:
+      case FE_UPWARD:
+        fputil::set_errno_if_required(ERANGE);
+        fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
+        return FPBits::inf().get_val();
+      default:
+        return FPBits::max_normal().get_val();
+      }
+    }
+  }
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+  if (x_bits.is_pos()) {
+    if (auto r = COSHF16_EXCEPTS_POS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+      return r.value();
+  } else {
+    if (auto r = COSHF16_EXCEPTS_NEG.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
+      return r.value();
+  }
+#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+  return eval_sinh_or_cosh</*IsSinh=*/false>(x);
+}
+
+} // namespace math
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_COSHF16_H
diff --git a/libc/src/math/generic/expxf16.h b/libc/src/__support/math/expxf16_utils.h
similarity index 95%
rename from libc/src/math/generic/expxf16.h
rename to libc/src/__support/math/expxf16_utils.h
index 562a427fc6cdc..651ef12d8a59d 100644
--- a/libc/src/math/generic/expxf16.h
+++ b/libc/src/__support/math/expxf16_utils.h
@@ -21,7 +21,11 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-LIBC_INLINE ExpRangeReduction exp2_range_reduction(float16 x) {
+namespace math {
+
+namespace expxf16_internal {
+
+LIBC_INLINE static constexpr ExpRangeReduction exp2_range_reduction(float16 x) {
   // For -25 < x < 16, to compute 2^x, we perform the following range reduction:
   // find hi, mid, lo, such that:
   //   x = hi + mid + lo, in which
@@ -117,7 +121,8 @@ static constexpr cpp::array<uint32_t, 32> EXP2_MID_5_BITS = {
 // The main point of these formulas is that the expensive part of calculating
 // the polynomials approximating lower parts of e^x and e^(-x) is shared and
 // only done once.
-template <bool IsSinh> LIBC_INLINE float16 eval_sinh_or_cosh(float16 x) {
+template <bool IsSinh>
+LIBC_INLINE static constexpr float16 eval_sinh_or_cosh(float16 x) {
   float xf = x;
   float kf = fputil::nearest_integer(xf * (LOG2F_E * 0x1.0p+5f));
   int x_hi_mid_p = static_cast<int>(kf);
@@ -174,7 +179,7 @@ template <bool IsSinh> LIBC_INLINE float16 eval_sinh_or_cosh(float16 x) {
 // Generated by Sollya with the following commands:
 //   > display = hexadecimal;
 //   > for i from 0 to 31 do print(round(log(1 + i * 2^-5), SG, RN));
-constexpr cpp::array<float, 32> LOGF_F = {
+static constexpr cpp::array<float, 32> LOGF_F = {
     0x0p+0f,        0x1.f829bp-6f,  0x1.f0a30cp-5f, 0x1.6f0d28p-4f,
     0x1.e27076p-4f, 0x1.29553p-3f,  0x1.5ff308p-3f, 0x1.9525aap-3f,
     0x1.c8ff7cp-3f, 0x1.fb9186p-3f, 0x1.1675cap-2f, 0x1.2e8e2cp-2f,
@@ -188,7 +193,7 @@ constexpr cpp::array<float, 32> LOGF_F = {
 // Generated by Sollya with the following commands:
 //   > display = hexadecimal;
 //   > for i from 0 to 31 do print(round(log2(1 + i * 2^-5), SG, RN));
-constexpr cpp::array<float, 32> LOG2F_F = {
+static constexpr cpp::array<float, 32> LOG2F_F = {
     0x0p+0f,        0x1.6bad38p-5f, 0x1.663f7p-4f,  0x1.08c588p-3f,
     0x1.5c01a4p-3f, 0x1.acf5e2p-3f, 0x1.fbc16cp-3f, 0x1.24407ap-2f,
     0x1.49a784p-2f, 0x1.6e221cp-2f, 0x1.91bba8p-2f, 0x1.b47ecp-2f,
@@ -202,7 +207,7 @@ constexpr cpp::array<float, 32> LOG2F_F = {
 // Generated by Sollya with the following commands:
 //   > display = hexadecimal;
 //   > for i from 0 to 31 do print(round(log10(1 + i * 2^-5), SG, RN));
-constexpr cpp::array<float, 32> LOG10F_F = {
+static constexpr cpp::array<float, 32> LOG10F_F = {
     0x0p+0f,        0x1.b5e908p-7f, 0x1.af5f92p-6f, 0x1.3ed11ap-5f,
     0x1.a30a9ep-5f, 0x1.02428cp-4f, 0x1.31b306p-4f, 0x1.5fe804p-4f,
     0x1.8cf184p-4f, 0x1.b8de4ep-4f, 0x1.e3bc1ap-4f, 0x1.06cbd6p-3f,
@@ -216,7 +221,7 @@ constexpr cpp::array<float, 32> LOG10F_F = {
 // Generated by Sollya with the following commands:
 //   > display = hexadecimal;
 //   > for i from 0 to 31 do print(round(1 / (1 + i * 2^-5), SG, RN));
-constexpr cpp::array<float, 32> ONE_OVER_F_F = {
+static constexpr cpp::array<float, 32> ONE_OVER_F_F = {
     0x1p+0f,        0x1.f07c2p-1f,  0x1.e1e1e2p-1f, 0x1.d41d42p-1f,
     0x1.c71c72p-1f, 0x1.bacf92p-1f, 0x1.af286cp-1f, 0x1.a41a42p-1f,
     0x1.99999ap-1f, 0x1.8f9c18p-1f, 0x1.861862p-1f, 0x1.7d05f4p-1f,
@@ -227,6 +232,10 @@ constexpr cpp::array<float, 32> ONE_OVER_F_F = {
     0x1.111112p-1f, 0x1.0c9714p-1f, 0x1.08421p-1f,  0x1.041042p-1f,
 };
 
+} // namespace expxf16_internal
+
+} // namespace math
+
 } // namespace LIBC_NAMESPACE_DECL
 
 #endif // LLVM_LIBC_SRC_MATH_GENERIC_EXPXF16_H
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 65e4839268443..91b8e3d530ef4 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1395,7 +1395,6 @@ add_entrypoint_object(
   HDRS
     ../exp2f16.h
   DEPENDS
-    .expxf16
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
     libc.src.__support.FPUtil.cast
@@ -1404,6 +1403,7 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.rounding_mode
     libc.src.__support.macros.optimization
+    libc.src.__support.math.expxf16_utils
 )
 
 add_entrypoint_object(
@@ -1433,7 +1433,6 @@ add_entrypoint_object(
   HDRS
     ../exp2m1f16.h
   DEPENDS
-    .expxf16
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
     libc.src.__support.common
@@ -1446,6 +1445,7 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.rounding_mode
     libc.src.__support.macros.optimization
     libc.src.__support.macros.properties.cpu_features
+    libc.src.__support.math.expxf16_utils
 )
 
 add_entrypoint_object(
@@ -1568,7 +1568,6 @@ add_entrypoint_object(
   HDRS
     ../expm1f16.h
   DEPENDS
-    .expxf16
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
     libc.src.__support.FPUtil.cast
@@ -1579,6 +1578,7 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.polyeval
     libc.src.__support.FPUtil.rounding_mode
     libc.src.__support.macros.optimization
+    libc.src.__support.math.expxf16_utils
 )
 
 add_entrypoint_object(
@@ -1949,7 +1949,6 @@ add_entrypoint_object(
   HDRS
     ../log10f16.h
   DEPENDS
-    .expxf16
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
     libc.src.__support.FPUtil.cast
@@ -1960,6 +1959,7 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.polyeval
     libc.src.__support.macros.optimization
     libc.src.__support.macros.properties.cpu_features
+    libc.src.__support.math.expxf16_utils
 )
 
 add_entrypoint_object(
@@ -2038,7 +2038,6 @@ add_entrypoint_object(
   HDRS
     ../log2f16.h
   DEPENDS
-    .expxf16
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
     libc.src.__support.FPUtil.cast
@@ -2049,6 +2048,7 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.polyeval
     libc.src.__support.macros.optimization
     libc.src.__support.macros.properties.cpu_features
+    libc.src.__support.math.expxf16_utils
 )
 
 add_entrypoint_object(
@@ -2093,7 +2093,6 @@ add_entrypoint_object(
   HDRS
     ../logf16.h
   DEPENDS
-    .expxf16
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
     libc.src.__support.FPUtil.cast
@@ -2104,6 +2103,7 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.polyeval
     libc.src.__support.macros.optimization
     libc.src.__support.macros.properties.cpu_features
+    libc.src.__support.math.expxf16_utils
 )
 
 add_entrypoint_object(
@@ -3918,14 +3918,7 @@ add_entrypoint_object(
   HDRS
     ../coshf16.h
   DEPENDS
-    .expxf16
-    libc.hdr.errno_macros
-    libc.hdr.fenv_macros
-    libc.src.__support.FPUtil.except_value_utils
-    libc.src.__support.FPUtil.fenv_impl
-    libc.src.__support.FPUtil.fp_bits
-    libc.src.__support.FPUtil.rounding_mode
-    libc.src.__support.macros.optimization
+    libc.src.__support.math.coshf16
 )
 
 add_entrypoint_object(
@@ -3948,7 +3941,6 @@ add_entrypoint_object(
   HDRS
     ../sinhf16.h
   DEPENDS
-    .expxf16
     libc.hdr.errno_macros
     libc.hdr.fenv_macros
     libc.src.__support.FPUtil.except_value_utils
@@ -3956,6 +3948,7 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.fp_bits
     libc.src.__support.FPUtil.rounding_mode
     libc.src.__support.macros.optimization
+    libc.src.__support.math.expxf16_utils
 )
 
 add_entrypoint_object(
@@ -3980,7 +3973,6 @@ add_entrypoint_object(
   HDRS
     ../tanhf16.h
   DEPENDS
-    .expxf16
     libc.hdr.fenv_macros
     libc.src.__support.CPP.array
     libc.src.__support.FPUtil.cast
@@ -3992,6 +3984,7 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.polyeval
     libc.src.__support.FPUtil.rounding_mode
     libc.src.__support.macros.optimization
+    libc.src.__support.math.expxf16_utils
 )
 
 add_entrypoint_object(
@@ -4937,21 +4930,6 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.generic.mul
 )
 
-add_header_library(
-  expxf16
-  HDRS
-    expxf16.h
-  DEPENDS
-    libc.hdr.stdint_proxy
-    libc.src.__support.FPUtil.fp_bits
-    libc.src.__support.FPUtil.cast
-    libc.src.__support.FPUtil.multiply_add
-    libc.src.__support.FPUtil.nearest_integer
-    libc.src.__support.macros.attributes
-    libc.src.__support.math.expf16_utils
-    libc.src.__support.math.exp10_float16_constants
-)
-
 add_entrypoint_object(
   bf16add
   SRCS
diff --git a/libc/src/math/generic/coshf16.cpp b/libc/src/math/generic/coshf16.cpp
index 689d16a55260f..d86edd98f29e9 100644
--- a/libc/src/math/generic/coshf16.cpp
+++ b/libc/src/math/generic/coshf16.cpp
@@ -7,105 +7,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/coshf16.h"
-#include "expxf16.h"
-#include "hdr/errno_macros.h"
-#include "hdr/fenv_macros.h"
-#include "src/__support/FPUtil/FEnvImpl.h"
-#include "src/__support/FPUtil/FPBits.h"
-#include "src/__support/FPUtil/except_value_utils.h"
-#include "src/__support/FPUtil/rounding_mode.h"
-#include "src/__support/common.h"
-#include "src/__support/macros/config.h"
-#include "src/__support/macros/optimization.h"
+#include "src/__support/math/coshf16.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-static constexpr fputil::ExceptValues<float16, 9> COSHF16_EXCEPTS_POS = {{
-    // x = 0x1.6ap-5, coshf16(x) = 0x1p+0 (RZ)
-    {0x29a8U, 0x3c00U, 1U, 0U, 1U},
-    // x = 0x1.8c4p+0, coshf16(x) = 0x1.3a8p+1 (RZ)
-    {0x3e31U, 0x40eaU, 1U, 0U, 0U},
-    // x = 0x1.994p+0, coshf16(x) = 0x1.498p+1 (RZ)
-    {0x3e65U, 0x4126U, 1U, 0U, 0U},
-    // x = 0x1.b6p+0, coshf16(x) = 0x1.6d8p+1 (RZ)
-    {0x3ed8U, 0x41b6U, 1U, 0U, 1U},
-    // x = 0x1.aap+1, coshf16(x) = 0x1.be8p+3 (RZ)
-    {0x42a8U, 0x4afaU, 1U, 0U, 1U},
-    // x = 0x1.cc4p+1, coshf16(x) = 0x1.23cp+4 (RZ)
-    {0x4331U, 0x4c8fU, 1U, 0U, 0U},
-    // x = 0x1.288p+2, coshf16(x) = 0x1.9b4p+5 (RZ)
-    {0x44a2U, 0x526dU, 1U, 0U, 0U},
-    // x = 0x1.958p+2, coshf16(x) = 0x1.1a4p+8 (RZ)
-    {0x4656U, 0x5c69U, 1U, 0U, 0U},
-    // x = 0x1.5fp+3, coshf16(x) = 0x1.c54p+14 (RZ)
-    {0x497cU, 0x7715U, 1U, 0U, 1U},
-}};
-
-static constexpr fputil::ExceptValues<float16, 6> COSHF16_EXCEPTS_NEG = {{
-    // x = -0x1.6ap-5, coshf16(x) = 0x1p+0 (RZ)
-    {0xa9a8U, 0x3c00U, 1U, 0U, 1U},
-    // x = -0x1.b6p+0, coshf16(x) = 0x1.6d8p+1 (RZ)
-    {0xbed8U, 0x41b6U, 1U, 0U, 1U},
-    // x = -0x1.288p+2, coshf16(x) = 0x1.9b4p+5 (RZ)
-    {0xc4a2U, 0x526dU, 1U, 0U, 0U},
-    // x = -0x1.5fp+3, coshf16(x) = 0x1.c54p+14 (RZ)
-    {0xc97cU, 0x7715U, 1U, 0U, 1U},
-    // x = -0x1.8c4p+0, coshf16(x) = 0x1.3a8p+1 (RZ)
-    {0xbe31U, 0x40eaU, 1U, 0U, 0U},
-    // x = -0x1.994p+0, coshf16(x) = 0x1.498p+1 (RZ)
-    {0xbe65U, 0x4126U, 1U, 0U, 0U},
-}};
-#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-
-LLVM_LIBC_FUNCTION(float16, coshf16, (float16 x)) {
-  using FPBits = fputil::FPBits<float16>;
-  FPBits x_bits(x);
-
-  uint16_t x_u = x_bits.uintval();
-  uint16_t x_abs = x_u & 0x7fffU;
-
-  // When |x| >= acosh(2^16), or x is NaN.
-  if (LIBC_UNLIKELY(x_abs >= 0x49e5U)) {
-    // cosh(NaN) = NaN
-    if (x_bits.is_nan()) {
-      if (x_bits.is_signaling_nan()) {
-        fputil::raise_except_if_required(FE_INVALID);
-        return FPBits::quiet_nan().get_val();
-      }
-
-      return x;
-    }
-
-    // When |x| >= acosh(2^16).
-    if (x_abs >= 0x49e5U) {
-      // cosh(+/-inf) = +inf
-      if (x_bits.is_inf())
-        return FPBits::inf().get_val();
-
-      switch (fputil::quick_get_round()) {
-      case FE_TONEAREST:
-      case FE_UPWARD:
-        fputil::set_errno_if_required(ERANGE);
-        fputil::raise_except_if_required(FE_OVERFLOW | FE_INEXACT);
-        return FPBits::inf().get_val();
-      default:
-        return FPBits::max_normal().get_val();
-      }
-    }
-  }
-
-#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-  if (x_bits.is_pos()) {
-    if (auto r = COSHF16_EXCEPTS_POS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
-      return r.value();
-  } else {
-    if (auto r = COSHF16_EXCEPTS_NEG.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
-      return r.value();
-  }
-#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
-
-  return eval_sinh_or_cosh</*IsSinh=*/false>(x);
-}
+LLVM_LIBC_FUNCTION(float16, coshf16, (float16 x)) { return math::coshf16(x); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/generic/exp2f16.cpp b/libc/src/math/generic/exp2f16.cpp
index 5c039c59df1af..5db0c3a9de08d 100644
--- a/libc/src/math/generic/exp2f16.cpp
+++ b/libc/src/math/generic/exp2f16.cpp
@@ -7,7 +7,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/math/exp2f16.h"
-#include "expxf16.h"
 #include "hdr/errno_macros.h"
 #include "hdr/fenv_macros.h"
 #include "src/__support/FPUtil/FEnvImpl.h"
@@ -18,6 +17,7 @@
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h"
+#include "src/__support/math/expxf16_utils.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
@@ -34,6 +34,7 @@ static constexpr fputil::ExceptValues<float16, 3> EXP2F16_EXCEPTS = {{
 #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
 
 LLVM_LIBC_FUNCTION(float16, exp2f16, (float16 x)) {
+  using namespace math::expxf16_internal;
   using FPBits = fputil::FPBits<float16>;
   FPBits x_bits(x);
 
diff --git a/libc/src/math/generic/exp2m1f16.cpp b/libc/src/math/generic/exp2m1f16.cpp
index 61633cd2cfcfb..ce0cc60748f19 100644
--- a/libc/src/math/generic/exp2m1f16.cpp
+++ b/libc/src/math/generic/exp2m1f16.cpp
@@ -7,7 +...
[truncated]

Copy link
Contributor Author

bassiounix commented Aug 14, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

Base automatically changed from users/bassiounix/spr/08-13-_libc_math_refactor_coshf_implementation_to_header-only_in_src___support_math_folder to main August 14, 2025 14:19
@bassiounix bassiounix force-pushed the users/bassiounix/spr/08-14-_libc_math_refactor_coshf16_implementation_to_header-only_in_src___support_math_folder branch from cb03965 to abcf3bc Compare August 14, 2025 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bazel "Peripheral" support tier build system: utils/bazel libc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants