authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-03-11 02:10:56+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-03-11 02:10:56+01:00
logc2587582c8af957f032bee1951d923d472c89e2f
tree3db2fd27a453108c5fe55abed1a83b12ae53527d
parent5d9660972d4b2f1ed575978e8543c88e1392f6b5
parent4a0be5613b865c345e2b9daade65ee6fbb139e39

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>

9 files changed, 52 insertions(+), 210 deletions(-)

lib/c/math.zig+52-41
......@@ -14,23 +14,24 @@ comptime {
1414 symbol(&isnanl, "isnanl");
1515 symbol(&isnanl, "__isnanl");
1616
17 symbol(&math.floatTrueMin(f64), "__DENORM");
18 symbol(&math.inf(f64), "__INF");
1719 symbol(&math.nan(f64), "__QNAN");
1820 symbol(&math.snan(f64), "__SNAN");
19 symbol(&math.inf(f64), "__INF");
20 symbol(&math.floatTrueMin(f64), "__DENORM");
2121
22 symbol(&math.floatTrueMin(f32), "__DENORMF");
23 symbol(&math.inf(f32), "__INFF");
2224 symbol(&math.nan(f32), "__QNANF");
2325 symbol(&math.snan(f32), "__SNANF");
24 symbol(&math.inf(f32), "__INFF");
25 symbol(&math.floatTrueMin(f32), "__DENORMF");
2626
27 symbol(&math.floatTrueMin(c_longdouble), "__DENORML");
28 symbol(&math.inf(c_longdouble), "__INFL");
2729 symbol(&math.nan(c_longdouble), "__QNANL");
2830 symbol(&math.snan(c_longdouble), "__SNANL");
29 symbol(&math.inf(c_longdouble), "__INFL");
30 symbol(&math.floatTrueMin(c_longdouble), "__DENORML");
3131 }
3232
3333 if (builtin.target.isMinGW() or builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
34 symbol(&coshf, "coshf");
3435 symbol(&hypotf, "hypotf");
3536 symbol(&hypotl, "hypotl");
3637 symbol(&nan, "nan");
......@@ -40,25 +41,27 @@ comptime {
4041
4142 if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
4243 symbol(&acos, "acos");
43 symbol(&atanf, "atanf");
44 symbol(&acosf, "acosf");
4445 symbol(&atan, "atan");
46 symbol(&atanf, "atanf");
4547 symbol(&atanl, "atanl");
4648 symbol(&cbrt, "cbrt");
4749 symbol(&cbrtf, "cbrtf");
50 symbol(&cosh, "cosh");
4851 symbol(&exp10, "exp10");
4952 symbol(&exp10f, "exp10f");
5053 symbol(&hypot, "hypot");
5154 symbol(&pow, "pow");
5255 symbol(&pow10, "pow10");
5356 symbol(&pow10f, "pow10f");
54 symbol(&acosf, "acosf");
5557 }
5658
5759 if (builtin.target.isMuslLibC()) {
58 symbol(&copysignf, "copysignf");
5960 symbol(&copysign, "copysign");
61 symbol(&copysignf, "copysignf");
6062 symbol(&rint, "rint");
6163 }
64
6265 symbol(&copysignl, "copysignl");
6366}
6467
......@@ -66,14 +69,18 @@ fn acos(x: f64) callconv(.c) f64 {
6669 return math.acos(x);
6770}
6871
69fn atanf(x: f32) callconv(.c) f32 {
70 return math.atan(x);
72fn acosf(x: f32) callconv(.c) f32 {
73 return std.math.acos(x);
7174}
7275
7376fn atan(x: f64) callconv(.c) f64 {
7477 return math.atan(x);
7578}
7679
80fn atanf(x: f32) callconv(.c) f32 {
81 return math.atan(x);
82}
83
7784fn atanl(x: c_longdouble) callconv(.c) c_longdouble {
7885 return switch (@typeInfo(@TypeOf(x)).float.bits) {
7986 16 => math.atan(@as(f16, @floatCast(x))),
......@@ -85,39 +92,19 @@ fn atanl(x: c_longdouble) callconv(.c) c_longdouble {
8592 };
8693}
8794
88fn acosf(x: f32) callconv(.c) f32 {
89 return std.math.acos(x);
90}
91
92fn isnan(x: f64) callconv(.c) c_int {
93 return if (math.isNan(x)) 1 else 0;
94}
95
96fn isnanf(x: f32) callconv(.c) c_int {
97 return if (math.isNan(x)) 1 else 0;
98}
99
100fn isnanl(x: c_longdouble) callconv(.c) c_int {
101 return if (math.isNan(x)) 1 else 0;
102}
103
104fn nan(_: [*:0]const c_char) callconv(.c) f64 {
105 return math.nan(f64);
106}
107
108fn nanf(_: [*:0]const c_char) callconv(.c) f32 {
109 return math.nan(f32);
95fn cbrt(x: f64) callconv(.c) f64 {
96 return math.cbrt(x);
11097}
11198
112fn nanl(_: [*:0]const c_char) callconv(.c) c_longdouble {
113 return math.nan(c_longdouble);
99fn cbrtf(x: f32) callconv(.c) f32 {
100 return math.cbrt(x);
114101}
115102
116fn copysignf(x: f32, y: f32) callconv(.c) f32 {
103fn copysign(x: f64, y: f64) callconv(.c) f64 {
117104 return math.copysign(x, y);
118105}
119106
120fn copysign(x: f64, y: f64) callconv(.c) f64 {
107fn copysignf(x: f32, y: f32) callconv(.c) f32 {
121108 return math.copysign(x, y);
122109}
123110
......@@ -125,12 +112,12 @@ fn copysignl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
125112 return math.copysign(x, y);
126113}
127114
128fn cbrt(x: f64) callconv(.c) f64 {
129 return math.cbrt(x);
115fn cosh(x: f64) callconv(.c) f64 {
116 return math.cosh(x);
130117}
131118
132fn cbrtf(x: f32) callconv(.c) f32 {
133 return math.cbrt(x);
119fn coshf(x: f32) callconv(.c) f32 {
120 return math.cosh(x);
134121}
135122
136123fn exp10(x: f64) callconv(.c) f64 {
......@@ -153,6 +140,30 @@ fn hypotl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
153140 return math.hypot(x, y);
154141}
155142
143fn isnan(x: f64) callconv(.c) c_int {
144 return if (math.isNan(x)) 1 else 0;
145}
146
147fn isnanf(x: f32) callconv(.c) c_int {
148 return if (math.isNan(x)) 1 else 0;
149}
150
151fn isnanl(x: c_longdouble) callconv(.c) c_int {
152 return if (math.isNan(x)) 1 else 0;
153}
154
155fn nan(_: [*:0]const c_char) callconv(.c) f64 {
156 return math.nan(f64);
157}
158
159fn nanf(_: [*:0]const c_char) callconv(.c) f32 {
160 return math.nan(f32);
161}
162
163fn nanl(_: [*:0]const c_char) callconv(.c) c_longdouble {
164 return math.nan(c_longdouble);
165}
166
156167fn pow(x: f64, y: f64) callconv(.c) f64 {
157168 return math.pow(f64, x, y);
158169}
lib/libc/mingw/math/coshf.c deleted-10
......@@ -1,10 +0,0 @@
1/**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6#include <math.h>
7float coshf (float x)
8{
9 return (float) cosh (x);
10}
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/musl/src/math/coshf.c deleted-33
......@@ -1,33 +0,0 @@
1#include "libm.h"
2
3float coshf(float x)
4{
5 union {float f; uint32_t i;} u = {.f = x};
6 uint32_t w;
7 float t;
8
9 /* |x| */
10 u.i &= 0x7fffffff;
11 x = u.f;
12 w = u.i;
13
14 /* |x| < log(2) */
15 if (w < 0x3f317217) {
16 if (w < 0x3f800000 - (12<<23)) {
17 FORCE_EVAL(x + 0x1p120f);
18 return 1;
19 }
20 t = expm1f(x);
21 return 1 + t*t/(2*(1+t));
22 }
23
24 /* |x| < log(FLT_MAX) */
25 if (w < 0x42b17217) {
26 t = expf(x);
27 return 0.5f*(t + 1/t);
28 }
29
30 /* |x| > log(FLT_MAX) or nan */
31 t = __expo2f(x, 1.0f);
32 return t;
33}
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}
lib/libc/wasi/libc-top-half/musl/src/math/coshf.c deleted-37
......@@ -1,37 +0,0 @@
1#include "libm.h"
2
3float coshf(float x)
4{
5 union {float f; uint32_t i;} u = {.f = x};
6 uint32_t w;
7 float t;
8
9 /* |x| */
10 u.i &= 0x7fffffff;
11 x = u.f;
12 w = u.i;
13
14 /* |x| < log(2) */
15 if (w < 0x3f317217) {
16 if (w < 0x3f800000 - (12<<23)) {
17 FORCE_EVAL(x + 0x1p120f);
18 return 1;
19 }
20 t = expm1f(x);
21 return 1 + t*t/(2*(1+t));
22 }
23
24 /* |x| < log(FLT_MAX) */
25 if (w < 0x42b17217) {
26 t = expf(x);
27 return 0.5f*(t + 1/t);
28 }
29
30 /* |x| > log(FLT_MAX) or nan */
31#ifdef __wasilibc_unmodified_upstream // Wasm doesn't have alternate rounding modes
32 t = __expo2f(x, 1.0f);
33#else
34 t = __expo2f(x);
35#endif
36 return t;
37}
src/libs/mingw.zig-1
......@@ -977,7 +977,6 @@ const mingw32_x86_src = [_][]const u8{
977977
978978const mingw32_x86_32_src = [_][]const u8{
979979 // ucrtbase
980 "math" ++ path.sep_str ++ "coshf.c",
981980 "math" ++ path.sep_str ++ "modff.c",
982981 "math" ++ path.sep_str ++ "powf.c",
983982 "math" ++ path.sep_str ++ "sinhf.c",
src/libs/musl.zig-2
......@@ -819,8 +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",
823 "musl/src/math/coshf.c",
824822 "musl/src/math/coshl.c",
825823 "musl/src/math/__cosl.c",
826824 "musl/src/math/cosl.c",
src/libs/wasi_libc.zig-2
......@@ -1003,8 +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",
1007 "wasi/libc-top-half/musl/src/math/coshf.c",
10081006 "wasi/libc-top-half/musl/src/math/__expo2.c",
10091007 "wasi/libc-top-half/musl/src/math/__expo2f.c",
10101008 "wasi/libc-top-half/musl/src/math/fmal.c",