[libc-commits] [libc] [libc][math] Refactor sinh to header-only (PR #177745)
via libc-commits
libc-commits at lists.llvm.org
Sat Jan 24 00:17:14 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Pratyush (Pratyush378)
<details>
<summary>Changes</summary>
Refactor `sinh(double)` to header-only following `exp.h`, `cosh.h` pattern.
Uses `expm1()` + small-x approximation.
Part of #<!-- -->177643
---
Full diff: https://github.com/llvm/llvm-project/pull/177745.diff
2 Files Affected:
- (added) libc/src/math/generic/sinh.cpp (+1)
- (modified) libc/src/math/sinh.h (+29-3)
``````````diff
diff --git a/libc/src/math/generic/sinh.cpp b/libc/src/math/generic/sinh.cpp
new file mode 100644
index 0000000000000..993e1a8951dfb
--- /dev/null
+++ b/libc/src/math/generic/sinh.cpp
@@ -0,0 +1 @@
+#include "src/math/sinh.h"
\ No newline at end of file
diff --git a/libc/src/math/sinh.h b/libc/src/math/sinh.h
index b87471c2bfcc2..57489d684c8a6 100644
--- a/libc/src/math/sinh.h
+++ b/libc/src/math/sinh.h
@@ -9,12 +9,38 @@
#ifndef LLVM_LIBC_SRC_MATH_SINH_H
#define LLVM_LIBC_SRC_MATH_SINH_H
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/math/expm1.h"
#include "src/__support/macros/config.h"
+#include "src/__support/macros/optimization.h"
-namespace LIBC_NAMESPACE_DECL {
+namespace LIBC_NAMESPACE {
-double sinh(double x);
+double sinh(double x) {
+ fputil::FPBits<double> xbits(x);
+ uint64_t xbits_u = xbits.bits;
+ uint32_t xpt = (xbits_u >> 52) & 0x7ffU;
-} // namespace LIBC_NAMESPACE_DECL
+ // sinh(±Inf) = ±Inf, sinh(NaN) = NaN
+ if (xpt >= 0x7ff) {
+ if (xbits_u == (0x7ffULL << 52)) // Inf
+ return x;
+ return x + 0.0; // NaN
+ }
+
+ // |x| < 2^-9: sinh(x) ≈ x + x^3/6
+ if (xpt < 0x3c9) {
+ double x2 = x * x;
+ return x + x * x2 * (1.0 / 6.0);
+ }
+
+ // Large |x|: sinh(x) ≈ sign(x)*0.5*exp(|x|)
+ double h = x < 0.0 ? -x : x;
+ double t = expm1(h);
+ double s = 0.5 * (t + 1.0);
+ return x >= 0.0 ? s : -s;
+}
+
+} // namespace LIBC_NAMESPACE
#endif // LLVM_LIBC_SRC_MATH_SINH_H
``````````
</details>
https://github.com/llvm/llvm-project/pull/177745
More information about the libc-commits
mailing list