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 {...@@ -14,23 +14,24 @@ comptime {
14 symbol(&isnanl, "isnanl");14 symbol(&isnanl, "isnanl");
15 symbol(&isnanl, "__isnanl");15 symbol(&isnanl, "__isnanl");
1616
17 symbol(&math.floatTrueMin(f64), "__DENORM");
18 symbol(&math.inf(f64), "__INF");
17 symbol(&math.nan(f64), "__QNAN");19 symbol(&math.nan(f64), "__QNAN");
18 symbol(&math.snan(f64), "__SNAN");20 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");
22 symbol(&math.nan(f32), "__QNANF");24 symbol(&math.nan(f32), "__QNANF");
23 symbol(&math.snan(f32), "__SNANF");25 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");
27 symbol(&math.nan(c_longdouble), "__QNANL");29 symbol(&math.nan(c_longdouble), "__QNANL");
28 symbol(&math.snan(c_longdouble), "__SNANL");30 symbol(&math.snan(c_longdouble), "__SNANL");
29 symbol(&math.inf(c_longdouble), "__INFL");
30 symbol(&math.floatTrueMin(c_longdouble), "__DENORML");
31 }31 }
3232
33 if (builtin.target.isMinGW() or builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {33 if (builtin.target.isMinGW() or builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
34 symbol(&coshf, "coshf");
34 symbol(&hypotf, "hypotf");35 symbol(&hypotf, "hypotf");
35 symbol(&hypotl, "hypotl");36 symbol(&hypotl, "hypotl");
36 symbol(&nan, "nan");37 symbol(&nan, "nan");
...@@ -40,25 +41,27 @@ comptime {...@@ -40,25 +41,27 @@ comptime {
4041
41 if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {42 if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
42 symbol(&acos, "acos");43 symbol(&acos, "acos");
43 symbol(&atanf, "atanf");44 symbol(&acosf, "acosf");
44 symbol(&atan, "atan");45 symbol(&atan, "atan");
46 symbol(&atanf, "atanf");
45 symbol(&atanl, "atanl");47 symbol(&atanl, "atanl");
46 symbol(&cbrt, "cbrt");48 symbol(&cbrt, "cbrt");
47 symbol(&cbrtf, "cbrtf");49 symbol(&cbrtf, "cbrtf");
50 symbol(&cosh, "cosh");
48 symbol(&exp10, "exp10");51 symbol(&exp10, "exp10");
49 symbol(&exp10f, "exp10f");52 symbol(&exp10f, "exp10f");
50 symbol(&hypot, "hypot");53 symbol(&hypot, "hypot");
51 symbol(&pow, "pow");54 symbol(&pow, "pow");
52 symbol(&pow10, "pow10");55 symbol(&pow10, "pow10");
53 symbol(&pow10f, "pow10f");56 symbol(&pow10f, "pow10f");
54 symbol(&acosf, "acosf");
55 }57 }
5658
57 if (builtin.target.isMuslLibC()) {59 if (builtin.target.isMuslLibC()) {
58 symbol(&copysignf, "copysignf");
59 symbol(&copysign, "copysign");60 symbol(&copysign, "copysign");
61 symbol(&copysignf, "copysignf");
60 symbol(&rint, "rint");62 symbol(&rint, "rint");
61 }63 }
64
62 symbol(&copysignl, "copysignl");65 symbol(&copysignl, "copysignl");
63}66}
6467
...@@ -66,14 +69,18 @@ fn acos(x: f64) callconv(.c) f64 {...@@ -66,14 +69,18 @@ fn acos(x: f64) callconv(.c) f64 {
66 return math.acos(x);69 return math.acos(x);
67}70}
6871
69fn atanf(x: f32) callconv(.c) f32 {72fn acosf(x: f32) callconv(.c) f32 {
70 return math.atan(x);73 return std.math.acos(x);
71}74}
7275
73fn atan(x: f64) callconv(.c) f64 {76fn atan(x: f64) callconv(.c) f64 {
74 return math.atan(x);77 return math.atan(x);
75}78}
7679
80fn atanf(x: f32) callconv(.c) f32 {
81 return math.atan(x);
82}
83
77fn atanl(x: c_longdouble) callconv(.c) c_longdouble {84fn atanl(x: c_longdouble) callconv(.c) c_longdouble {
78 return switch (@typeInfo(@TypeOf(x)).float.bits) {85 return switch (@typeInfo(@TypeOf(x)).float.bits) {
79 16 => math.atan(@as(f16, @floatCast(x))),86 16 => math.atan(@as(f16, @floatCast(x))),
...@@ -85,39 +92,19 @@ fn atanl(x: c_longdouble) callconv(.c) c_longdouble {...@@ -85,39 +92,19 @@ fn atanl(x: c_longdouble) callconv(.c) c_longdouble {
85 };92 };
86}93}
8794
88fn acosf(x: f32) callconv(.c) f32 {95fn cbrt(x: f64) callconv(.c) f64 {
89 return std.math.acos(x);96 return math.cbrt(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);
110}97}
11198
112fn nanl(_: [*:0]const c_char) callconv(.c) c_longdouble {99fn cbrtf(x: f32) callconv(.c) f32 {
113 return math.nan(c_longdouble);100 return math.cbrt(x);
114}101}
115102
116fn copysignf(x: f32, y: f32) callconv(.c) f32 {103fn copysign(x: f64, y: f64) callconv(.c) f64 {
117 return math.copysign(x, y);104 return math.copysign(x, y);
118}105}
119106
120fn copysign(x: f64, y: f64) callconv(.c) f64 {107fn copysignf(x: f32, y: f32) callconv(.c) f32 {
121 return math.copysign(x, y);108 return math.copysign(x, y);
122}109}
123110
...@@ -125,12 +112,12 @@ fn copysignl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {...@@ -125,12 +112,12 @@ fn copysignl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
125 return math.copysign(x, y);112 return math.copysign(x, y);
126}113}
127114
128fn cbrt(x: f64) callconv(.c) f64 {115fn cosh(x: f64) callconv(.c) f64 {
129 return math.cbrt(x);116 return math.cosh(x);
130}117}
131118
132fn cbrtf(x: f32) callconv(.c) f32 {119fn coshf(x: f32) callconv(.c) f32 {
133 return math.cbrt(x);120 return math.cosh(x);
134}121}
135122
136fn exp10(x: f64) callconv(.c) f64 {123fn exp10(x: f64) callconv(.c) f64 {
...@@ -153,6 +140,30 @@ fn hypotl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {...@@ -153,6 +140,30 @@ fn hypotl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
153 return math.hypot(x, y);140 return math.hypot(x, y);
154}141}
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
156fn pow(x: f64, y: f64) callconv(.c) f64 {167fn pow(x: f64, y: f64) callconv(.c) f64 {
157 return math.pow(f64, x, y);168 return math.pow(f64, x, y);
158}169}
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{...@@ -977,7 +977,6 @@ const mingw32_x86_src = [_][]const u8{
977977
978const mingw32_x86_32_src = [_][]const u8{978const mingw32_x86_32_src = [_][]const u8{
979 // ucrtbase979 // ucrtbase
980 "math" ++ path.sep_str ++ "coshf.c",
981 "math" ++ path.sep_str ++ "modff.c",980 "math" ++ path.sep_str ++ "modff.c",
982 "math" ++ path.sep_str ++ "powf.c",981 "math" ++ path.sep_str ++ "powf.c",
983 "math" ++ path.sep_str ++ "sinhf.c",982 "math" ++ path.sep_str ++ "sinhf.c",
src/libs/musl.zig-2
...@@ -819,8 +819,6 @@ const src_files = [_][]const u8{...@@ -819,8 +819,6 @@ const src_files = [_][]const u8{
819 "musl/src/math/cbrtl.c",819 "musl/src/math/cbrtl.c",
820 "musl/src/math/__cos.c",820 "musl/src/math/__cos.c",
821 "musl/src/math/__cosdf.c",821 "musl/src/math/__cosdf.c",
822 "musl/src/math/cosh.c",
823 "musl/src/math/coshf.c",
824 "musl/src/math/coshl.c",822 "musl/src/math/coshl.c",
825 "musl/src/math/__cosl.c",823 "musl/src/math/__cosl.c",
826 "musl/src/math/cosl.c",824 "musl/src/math/cosl.c",
src/libs/wasi_libc.zig-2
...@@ -1003,8 +1003,6 @@ const libc_top_half_src_files = [_][]const u8{...@@ -1003,8 +1003,6 @@ const libc_top_half_src_files = [_][]const u8{
1003 "wasi/libc-top-half/musl/src/locale/locale_map.c",1003 "wasi/libc-top-half/musl/src/locale/locale_map.c",
1004 "wasi/libc-top-half/musl/src/locale/newlocale.c",1004 "wasi/libc-top-half/musl/src/locale/newlocale.c",
1005 "wasi/libc-top-half/musl/src/locale/uselocale.c",1005 "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",
1008 "wasi/libc-top-half/musl/src/math/__expo2.c",1006 "wasi/libc-top-half/musl/src/math/__expo2.c",
1009 "wasi/libc-top-half/musl/src/math/__expo2f.c",1007 "wasi/libc-top-half/musl/src/math/__expo2f.c",
1010 "wasi/libc-top-half/musl/src/math/fmal.c",1008 "wasi/libc-top-half/musl/src/math/fmal.c",