From 159568a05a222681f54a27310ac15c3043563f95 Mon Sep 17 00:00:00 2001 From: mihael Date: Tue, 10 Mar 2026 00:09:00 +0100 Subject: [PATCH] Reimplement `coshf` 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= -Dtest-filter=coshf -fqemu -fwasmtime --summary line Build Summary: 553/553 steps succeeded ``` --- lib/c/math.zig | 5 +++ lib/libc/mingw/math/coshf.c | 10 ----- lib/libc/musl/src/math/coshf.c | 33 ----------------- .../wasi/libc-top-half/musl/src/math/coshf.c | 37 ------------------- src/libs/mingw.zig | 1 - src/libs/musl.zig | 1 - src/libs/wasi_libc.zig | 1 - 7 files changed, 5 insertions(+), 83 deletions(-) delete mode 100644 lib/libc/mingw/math/coshf.c delete mode 100644 lib/libc/musl/src/math/coshf.c delete mode 100644 lib/libc/wasi/libc-top-half/musl/src/math/coshf.c diff --git a/lib/c/math.zig b/lib/c/math.zig index ae3491e430c4cb433ab01430b93193ed732ed14b..281b898ac2061cdad1c41a9fe56361ae9724a3f7 100644 --- a/lib/c/math.zig +++ b/lib/c/math.zig @@ -31,6 +31,7 @@ comptime { } if (builtin.target.isMinGW() or builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) { + symbol(&coshf, "coshf"); symbol(&hypotf, "hypotf"); symbol(&hypotl, "hypotl"); symbol(&nan, "nan"); @@ -130,6 +131,10 @@ fn cosh(x: f64) callconv(.c) f64 { return math.cosh(x); } +fn coshf(x: f32) callconv(.c) f32 { + return math.cosh(x); +} + fn cbrt(x: f64) callconv(.c) f64 { return math.cbrt(x); } diff --git a/lib/libc/mingw/math/coshf.c b/lib/libc/mingw/math/coshf.c deleted file mode 100644 index 72147f5bfbcb77fdb2754e876472f33583ae2756..0000000000000000000000000000000000000000 --- a/lib/libc/mingw/math/coshf.c +++ /dev/null @@ -1,10 +0,0 @@ -/** - * This file has no copyright assigned and is placed in the Public Domain. - * This file is part of the mingw-w64 runtime package. - * No warranty is given; refer to the file DISCLAIMER.PD within this package. - */ -#include -float coshf (float x) -{ - return (float) cosh (x); -} diff --git a/lib/libc/musl/src/math/coshf.c b/lib/libc/musl/src/math/coshf.c deleted file mode 100644 index e739cff93b1325f931198d8d75b3313359803752..0000000000000000000000000000000000000000 --- a/lib/libc/musl/src/math/coshf.c +++ /dev/null @@ -1,33 +0,0 @@ -#include "libm.h" - -float coshf(float x) -{ - union {float f; uint32_t i;} u = {.f = x}; - uint32_t w; - float t; - - /* |x| */ - u.i &= 0x7fffffff; - x = u.f; - w = u.i; - - /* |x| < log(2) */ - if (w < 0x3f317217) { - if (w < 0x3f800000 - (12<<23)) { - FORCE_EVAL(x + 0x1p120f); - return 1; - } - t = expm1f(x); - return 1 + t*t/(2*(1+t)); - } - - /* |x| < log(FLT_MAX) */ - if (w < 0x42b17217) { - t = expf(x); - return 0.5f*(t + 1/t); - } - - /* |x| > log(FLT_MAX) or nan */ - t = __expo2f(x, 1.0f); - return t; -} diff --git a/lib/libc/wasi/libc-top-half/musl/src/math/coshf.c b/lib/libc/wasi/libc-top-half/musl/src/math/coshf.c deleted file mode 100644 index b946c0b068e6292ac99a53d855fb8b3a6f91bef3..0000000000000000000000000000000000000000 --- a/lib/libc/wasi/libc-top-half/musl/src/math/coshf.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "libm.h" - -float coshf(float x) -{ - union {float f; uint32_t i;} u = {.f = x}; - uint32_t w; - float t; - - /* |x| */ - u.i &= 0x7fffffff; - x = u.f; - w = u.i; - - /* |x| < log(2) */ - if (w < 0x3f317217) { - if (w < 0x3f800000 - (12<<23)) { - FORCE_EVAL(x + 0x1p120f); - return 1; - } - t = expm1f(x); - return 1 + t*t/(2*(1+t)); - } - - /* |x| < log(FLT_MAX) */ - if (w < 0x42b17217) { - t = expf(x); - return 0.5f*(t + 1/t); - } - - /* |x| > log(FLT_MAX) or nan */ -#ifdef __wasilibc_unmodified_upstream // Wasm doesn't have alternate rounding modes - t = __expo2f(x, 1.0f); -#else - t = __expo2f(x); -#endif - return t; -} diff --git a/src/libs/mingw.zig b/src/libs/mingw.zig index 0be2b7c0a8c4a13faaac25ad2b3d654d8bd23d1e..70e0d6e8c019e760144004437e10f2e42c0123f5 100644 --- a/src/libs/mingw.zig +++ b/src/libs/mingw.zig @@ -977,7 +977,6 @@ const mingw32_x86_src = [_][]const u8{ const mingw32_x86_32_src = [_][]const u8{ // ucrtbase - "math" ++ path.sep_str ++ "coshf.c", "math" ++ path.sep_str ++ "modff.c", "math" ++ path.sep_str ++ "powf.c", "math" ++ path.sep_str ++ "sinhf.c", diff --git a/src/libs/musl.zig b/src/libs/musl.zig index eb49d4c4f136f9b166f4dc81593d7d2ecc9b8daf..482c1a1b29190bac0e48368ab61d77ec196ce157 100644 --- a/src/libs/musl.zig +++ b/src/libs/musl.zig @@ -819,7 +819,6 @@ const src_files = [_][]const u8{ "musl/src/math/cbrtl.c", "musl/src/math/__cos.c", "musl/src/math/__cosdf.c", - "musl/src/math/coshf.c", "musl/src/math/coshl.c", "musl/src/math/__cosl.c", "musl/src/math/cosl.c", diff --git a/src/libs/wasi_libc.zig b/src/libs/wasi_libc.zig index 72b19fe2e6e88e9cb8ead76bd4178248ef1c633c..67c1b0e74c2a54bea0e7948fff34205201f3ce7d 100644 --- a/src/libs/wasi_libc.zig +++ b/src/libs/wasi_libc.zig @@ -1003,7 +1003,6 @@ const libc_top_half_src_files = [_][]const u8{ "wasi/libc-top-half/musl/src/locale/locale_map.c", "wasi/libc-top-half/musl/src/locale/newlocale.c", "wasi/libc-top-half/musl/src/locale/uselocale.c", - "wasi/libc-top-half/musl/src/math/coshf.c", "wasi/libc-top-half/musl/src/math/__expo2.c", "wasi/libc-top-half/musl/src/math/__expo2f.c", "wasi/libc-top-half/musl/src/math/fmal.c", -- 2.54.0