authorgravatar for hi@mihaelm.commihael <hi@mihaelm.com> 2026-03-09 23:17:46+01:00
committergravatar for hi@mihaelm.commihael <hi@mihaelm.com> 2026-03-10 00:42:14+01:00
log1f92162875a3f05942dad6696b206cb9a1394f80
tree331e363f84c6a6146587f478cf6e2e5b29512da7
parent3b515fbede945a2927d5aba59212553a8b26b944
signaturebadge-check Signed by SSH key SHA256:aoFoShdYLdrqMichqKXSSieTKUfACUIDJHsKc4V2tQg

Reimplement `cosh` in `libzigc`

The function basically calls the Zig std's implementation. The changes were tested by running: ``` $ ./build/stage3/bin/zig build -p stage4 -Denable-llvm -Dno-lib $ stage4/bin/zig build test-libc -Dlibc-test-path=<LIBC-TEST-PATH> -Dtest-filter=cosh -fqemu -fwasmtime --summary line Build Summary: 921/921 steps succeeded ```

5 files changed, 5 insertions(+), 86 deletions(-)

lib/c/math.zig+5
......@@ -45,6 +45,7 @@ comptime {
4545 symbol(&atanl, "atanl");
4646 symbol(&cbrt, "cbrt");
4747 symbol(&cbrtf, "cbrtf");
48 symbol(&cosh, "cosh");
4849 symbol(&exp10, "exp10");
4950 symbol(&exp10f, "exp10f");
5051 symbol(&hypot, "hypot");
......@@ -125,6 +126,10 @@ fn copysignl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
125126 return math.copysign(x, y);
126127}
127128
129fn cosh(x: f64) callconv(.c) f64 {
130 return math.cosh(x);
131}
132
128133fn cbrt(x: f64) callconv(.c) f64 {
129134 return math.cbrt(x);
130135}
lib/libc/musl/src/math/cosh.c deleted-40
......@@ -1,40 +0,0 @@
1#include "libm.h"
2
3/* cosh(x) = (exp(x) + 1/exp(x))/2
4 * = 1 + 0.5*(exp(x)-1)*(exp(x)-1)/exp(x)
5 * = 1 + x*x/2 + o(x^4)
6 */
7double cosh(double x)
8{
9 union {double f; uint64_t i;} u = {.f = x};
10 uint32_t w;
11 double t;
12
13 /* |x| */
14 u.i &= (uint64_t)-1/2;
15 x = u.f;
16 w = u.i >> 32;
17
18 /* |x| < log(2) */
19 if (w < 0x3fe62e42) {
20 if (w < 0x3ff00000 - (26<<20)) {
21 /* raise inexact if x!=0 */
22 FORCE_EVAL(x + 0x1p120f);
23 return 1;
24 }
25 t = expm1(x);
26 return 1 + t*t/(2*(1+t));
27 }
28
29 /* |x| < log(DBL_MAX) */
30 if (w < 0x40862e42) {
31 t = exp(x);
32 /* note: if x>log(0x1p26) then the 1/t is not needed */
33 return 0.5*(t + 1/t);
34 }
35
36 /* |x| > log(DBL_MAX) or nan */
37 /* note: the result is stored to handle overflow */
38 t = __expo2(x, 1.0);
39 return t;
40}
lib/libc/wasi/libc-top-half/musl/src/math/cosh.c deleted-44
......@@ -1,44 +0,0 @@
1#include "libm.h"
2
3/* cosh(x) = (exp(x) + 1/exp(x))/2
4 * = 1 + 0.5*(exp(x)-1)*(exp(x)-1)/exp(x)
5 * = 1 + x*x/2 + o(x^4)
6 */
7double cosh(double x)
8{
9 union {double f; uint64_t i;} u = {.f = x};
10 uint32_t w;
11 double t;
12
13 /* |x| */
14 u.i &= (uint64_t)-1/2;
15 x = u.f;
16 w = u.i >> 32;
17
18 /* |x| < log(2) */
19 if (w < 0x3fe62e42) {
20 if (w < 0x3ff00000 - (26<<20)) {
21 /* raise inexact if x!=0 */
22 FORCE_EVAL(x + 0x1p120f);
23 return 1;
24 }
25 t = expm1(x);
26 return 1 + t*t/(2*(1+t));
27 }
28
29 /* |x| < log(DBL_MAX) */
30 if (w < 0x40862e42) {
31 t = exp(x);
32 /* note: if x>log(0x1p26) then the 1/t is not needed */
33 return 0.5*(t + 1/t);
34 }
35
36 /* |x| > log(DBL_MAX) or nan */
37 /* note: the result is stored to handle overflow */
38#ifdef __wasilibc_unmodified_upstream // Wasm doesn't have alternate rounding modes
39 t = __expo2(x, 1.0);
40#else
41 t = __expo2(x);
42#endif
43 return t;
44}
src/libs/musl.zig-1
......@@ -819,7 +819,6 @@ const src_files = [_][]const u8{
819819 "musl/src/math/cbrtl.c",
820820 "musl/src/math/__cos.c",
821821 "musl/src/math/__cosdf.c",
822 "musl/src/math/cosh.c",
823822 "musl/src/math/coshf.c",
824823 "musl/src/math/coshl.c",
825824 "musl/src/math/__cosl.c",
src/libs/wasi_libc.zig-1
......@@ -1003,7 +1003,6 @@ const libc_top_half_src_files = [_][]const u8{
10031003 "wasi/libc-top-half/musl/src/locale/locale_map.c",
10041004 "wasi/libc-top-half/musl/src/locale/newlocale.c",
10051005 "wasi/libc-top-half/musl/src/locale/uselocale.c",
1006 "wasi/libc-top-half/musl/src/math/cosh.c",
10071006 "wasi/libc-top-half/musl/src/math/coshf.c",
10081007 "wasi/libc-top-half/musl/src/math/__expo2.c",
10091008 "wasi/libc-top-half/musl/src/math/__expo2f.c",