zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit c2587582c8af957f032bee1951d923d472c89e2f (tree)
parent 5d9660972d4b2f1ed575978e8543c88e1392f6b5
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Wed, 11 Mar 2026 02:10:56 +0100

Merge pull request 'libzigc: Implement `coshf` & `cosh`' (#31434) from mihael/zig:libzigc/implement-coshf-cosh into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31434
Reviewed-by: Andrew Kelley <andrew@ziglang.org>

Diffstat:
Mlib/c/math.zig | 93++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Dlib/libc/mingw/math/coshf.c | 10----------
Dlib/libc/musl/src/math/cosh.c | 40----------------------------------------
Dlib/libc/musl/src/math/coshf.c | 33---------------------------------
Dlib/libc/wasi/libc-top-half/musl/src/math/cosh.c | 44--------------------------------------------
Dlib/libc/wasi/libc-top-half/musl/src/math/coshf.c | 37-------------------------------------
Msrc/libs/mingw.zig | 1-
Msrc/libs/musl.zig | 2--
Msrc/libs/wasi_libc.zig | 2--
9 files changed, 52 insertions(+), 210 deletions(-)

diff --git a/lib/c/math.zig b/lib/c/math.zig @@ -14,23 +14,24 @@ comptime { symbol(&isnanl, "isnanl"); symbol(&isnanl, "__isnanl"); + symbol(&math.floatTrueMin(f64), "__DENORM"); + symbol(&math.inf(f64), "__INF"); symbol(&math.nan(f64), "__QNAN"); symbol(&math.snan(f64), "__SNAN"); - symbol(&math.inf(f64), "__INF"); - symbol(&math.floatTrueMin(f64), "__DENORM"); + symbol(&math.floatTrueMin(f32), "__DENORMF"); + symbol(&math.inf(f32), "__INFF"); symbol(&math.nan(f32), "__QNANF"); symbol(&math.snan(f32), "__SNANF"); - symbol(&math.inf(f32), "__INFF"); - symbol(&math.floatTrueMin(f32), "__DENORMF"); + symbol(&math.floatTrueMin(c_longdouble), "__DENORML"); + symbol(&math.inf(c_longdouble), "__INFL"); symbol(&math.nan(c_longdouble), "__QNANL"); symbol(&math.snan(c_longdouble), "__SNANL"); - symbol(&math.inf(c_longdouble), "__INFL"); - symbol(&math.floatTrueMin(c_longdouble), "__DENORML"); } if (builtin.target.isMinGW() or builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) { + symbol(&coshf, "coshf"); symbol(&hypotf, "hypotf"); symbol(&hypotl, "hypotl"); symbol(&nan, "nan"); @@ -40,25 +41,27 @@ comptime { if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) { symbol(&acos, "acos"); - symbol(&atanf, "atanf"); + symbol(&acosf, "acosf"); symbol(&atan, "atan"); + symbol(&atanf, "atanf"); symbol(&atanl, "atanl"); symbol(&cbrt, "cbrt"); symbol(&cbrtf, "cbrtf"); + symbol(&cosh, "cosh"); symbol(&exp10, "exp10"); symbol(&exp10f, "exp10f"); symbol(&hypot, "hypot"); symbol(&pow, "pow"); symbol(&pow10, "pow10"); symbol(&pow10f, "pow10f"); - symbol(&acosf, "acosf"); } if (builtin.target.isMuslLibC()) { - symbol(&copysignf, "copysignf"); symbol(&copysign, "copysign"); + symbol(&copysignf, "copysignf"); symbol(&rint, "rint"); } + symbol(&copysignl, "copysignl"); } @@ -66,14 +69,18 @@ fn acos(x: f64) callconv(.c) f64 { return math.acos(x); } -fn atanf(x: f32) callconv(.c) f32 { - return math.atan(x); +fn acosf(x: f32) callconv(.c) f32 { + return std.math.acos(x); } fn atan(x: f64) callconv(.c) f64 { return math.atan(x); } +fn atanf(x: f32) callconv(.c) f32 { + return math.atan(x); +} + fn atanl(x: c_longdouble) callconv(.c) c_longdouble { return switch (@typeInfo(@TypeOf(x)).float.bits) { 16 => math.atan(@as(f16, @floatCast(x))), @@ -85,39 +92,19 @@ fn atanl(x: c_longdouble) callconv(.c) c_longdouble { }; } -fn acosf(x: f32) callconv(.c) f32 { - return std.math.acos(x); -} - -fn isnan(x: f64) callconv(.c) c_int { - return if (math.isNan(x)) 1 else 0; -} - -fn isnanf(x: f32) callconv(.c) c_int { - return if (math.isNan(x)) 1 else 0; -} - -fn isnanl(x: c_longdouble) callconv(.c) c_int { - return if (math.isNan(x)) 1 else 0; -} - -fn nan(_: [*:0]const c_char) callconv(.c) f64 { - return math.nan(f64); -} - -fn nanf(_: [*:0]const c_char) callconv(.c) f32 { - return math.nan(f32); +fn cbrt(x: f64) callconv(.c) f64 { + return math.cbrt(x); } -fn nanl(_: [*:0]const c_char) callconv(.c) c_longdouble { - return math.nan(c_longdouble); +fn cbrtf(x: f32) callconv(.c) f32 { + return math.cbrt(x); } -fn copysignf(x: f32, y: f32) callconv(.c) f32 { +fn copysign(x: f64, y: f64) callconv(.c) f64 { return math.copysign(x, y); } -fn copysign(x: f64, y: f64) callconv(.c) f64 { +fn copysignf(x: f32, y: f32) callconv(.c) f32 { return math.copysign(x, y); } @@ -125,12 +112,12 @@ fn copysignl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble { return math.copysign(x, y); } -fn cbrt(x: f64) callconv(.c) f64 { - return math.cbrt(x); +fn cosh(x: f64) callconv(.c) f64 { + return math.cosh(x); } -fn cbrtf(x: f32) callconv(.c) f32 { - return math.cbrt(x); +fn coshf(x: f32) callconv(.c) f32 { + return math.cosh(x); } fn exp10(x: f64) callconv(.c) f64 { @@ -153,6 +140,30 @@ fn hypotl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble { return math.hypot(x, y); } +fn isnan(x: f64) callconv(.c) c_int { + return if (math.isNan(x)) 1 else 0; +} + +fn isnanf(x: f32) callconv(.c) c_int { + return if (math.isNan(x)) 1 else 0; +} + +fn isnanl(x: c_longdouble) callconv(.c) c_int { + return if (math.isNan(x)) 1 else 0; +} + +fn nan(_: [*:0]const c_char) callconv(.c) f64 { + return math.nan(f64); +} + +fn nanf(_: [*:0]const c_char) callconv(.c) f32 { + return math.nan(f32); +} + +fn nanl(_: [*:0]const c_char) callconv(.c) c_longdouble { + return math.nan(c_longdouble); +} + fn pow(x: f64, y: f64) callconv(.c) f64 { return math.pow(f64, x, y); } diff --git a/lib/libc/mingw/math/coshf.c b/lib/libc/mingw/math/coshf.c @@ -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 <math.h> -float coshf (float x) -{ - return (float) cosh (x); -} diff --git a/lib/libc/musl/src/math/cosh.c b/lib/libc/musl/src/math/cosh.c @@ -1,40 +0,0 @@ -#include "libm.h" - -/* cosh(x) = (exp(x) + 1/exp(x))/2 - * = 1 + 0.5*(exp(x)-1)*(exp(x)-1)/exp(x) - * = 1 + x*x/2 + o(x^4) - */ -double cosh(double x) -{ - union {double f; uint64_t i;} u = {.f = x}; - uint32_t w; - double t; - - /* |x| */ - u.i &= (uint64_t)-1/2; - x = u.f; - w = u.i >> 32; - - /* |x| < log(2) */ - if (w < 0x3fe62e42) { - if (w < 0x3ff00000 - (26<<20)) { - /* raise inexact if x!=0 */ - FORCE_EVAL(x + 0x1p120f); - return 1; - } - t = expm1(x); - return 1 + t*t/(2*(1+t)); - } - - /* |x| < log(DBL_MAX) */ - if (w < 0x40862e42) { - t = exp(x); - /* note: if x>log(0x1p26) then the 1/t is not needed */ - return 0.5*(t + 1/t); - } - - /* |x| > log(DBL_MAX) or nan */ - /* note: the result is stored to handle overflow */ - t = __expo2(x, 1.0); - return t; -} diff --git a/lib/libc/musl/src/math/coshf.c b/lib/libc/musl/src/math/coshf.c @@ -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/cosh.c b/lib/libc/wasi/libc-top-half/musl/src/math/cosh.c @@ -1,44 +0,0 @@ -#include "libm.h" - -/* cosh(x) = (exp(x) + 1/exp(x))/2 - * = 1 + 0.5*(exp(x)-1)*(exp(x)-1)/exp(x) - * = 1 + x*x/2 + o(x^4) - */ -double cosh(double x) -{ - union {double f; uint64_t i;} u = {.f = x}; - uint32_t w; - double t; - - /* |x| */ - u.i &= (uint64_t)-1/2; - x = u.f; - w = u.i >> 32; - - /* |x| < log(2) */ - if (w < 0x3fe62e42) { - if (w < 0x3ff00000 - (26<<20)) { - /* raise inexact if x!=0 */ - FORCE_EVAL(x + 0x1p120f); - return 1; - } - t = expm1(x); - return 1 + t*t/(2*(1+t)); - } - - /* |x| < log(DBL_MAX) */ - if (w < 0x40862e42) { - t = exp(x); - /* note: if x>log(0x1p26) then the 1/t is not needed */ - return 0.5*(t + 1/t); - } - - /* |x| > log(DBL_MAX) or nan */ - /* note: the result is stored to handle overflow */ -#ifdef __wasilibc_unmodified_upstream // Wasm doesn't have alternate rounding modes - t = __expo2(x, 1.0); -#else - t = __expo2(x); -#endif - 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 @@ -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 @@ -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 @@ -819,8 +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/cosh.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 @@ -1003,8 +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/cosh.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",