authorgravatar for badayvedat@gmail.combadayvedat <badayvedat@gmail.com> 2026-04-16 01:11:04+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-04-16 01:11:04+02:00
loga05a25e2bbcd26ce84fa16b1a75ae13b1ecd50d2
treeae3dd0530bbb26dad0facd6b7d77f665fffd437e
parentbf402649411f8ddbfab94dbab6088215d67e0e00

zig libc: export fdiml and fdimf (#31759)

Exports `fdiml` and `fdimf` in zig libc and removes from from musl and mingw libc. Contributes to: https://codeberg.org/ziglang/zig/issues/30978 Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31759 Reviewed-by: Andrew Kelley <andrew@ziglang.org> Co-authored-by: badayvedat <badayvedat@gmail.com> Co-committed-by: badayvedat <badayvedat@gmail.com>

8 files changed, 22 insertions(+), 66 deletions(-)

lib/c/math.zig+20-7
......@@ -45,6 +45,7 @@ comptime {
4545 if ((builtin.target.isMinGW() and @sizeOf(f64) != @sizeOf(c_longdouble)) or builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
4646 symbol(&atanl, "atanl");
4747 symbol(&copysignl, "copysignl");
48 symbol(&fdiml, "fdiml");
4849 symbol(&nanl, "nanl");
4950 }
5051
......@@ -67,6 +68,7 @@ comptime {
6768 symbol(&exp10, "exp10");
6869 symbol(&exp10f, "exp10f");
6970 symbol(&fdim, "fdim");
71 symbol(&fdimf, "fdimf");
7072 symbol(&finite, "finite");
7173 symbol(&finitef, "finitef");
7274 symbol(&frexp, "frexp");
......@@ -160,19 +162,30 @@ fn exp10f(x: f32) callconv(.c) f32 {
160162 return math.pow(f32, 10.0, x);
161163}
162164
163fn fdim(x: f64, y: f64) callconv(.c) f64 {
164 if (math.isNan(x)) {
165fn fdimGeneric(comptime T: type, x: T, y: T) T {
166 if (math.isNan(x))
165167 return x;
166 }
167 if (math.isNan(y)) {
168
169 if (math.isNan(y))
168170 return y;
169 }
170 if (x > y) {
171
172 if (x > y)
171173 return x - y;
172 }
173174 return 0;
174175}
175176
177fn fdim(x: f64, y: f64) callconv(.c) f64 {
178 return fdimGeneric(f64, x, y);
179}
180
181fn fdimf(x: f32, y: f32) callconv(.c) f32 {
182 return fdimGeneric(f32, x, y);
183}
184
185fn fdiml(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble {
186 return fdimGeneric(c_longdouble, x, y);
187}
188
176189fn finite(x: f64) callconv(.c) c_int {
177190 return if (math.isFinite(x)) 1 else 0;
178191}
lib/libc/mingw/math/fdiml.c deleted-24
......@@ -1,24 +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 <errno.h>
7#include <math.h>
8
9long double
10fdiml (long double x, long double y)
11{
12 int cx = fpclassify (x), cy = fpclassify (y);
13 long double r;
14
15 if (cx == FP_NAN || cy == FP_NAN
16 || (y < 0 && cx == FP_INFINITE && cy == FP_INFINITE))
17 return x - y; /* Take care invalid flag is raised. */
18 if (x <= y)
19 return 0.0;
20 r = x - y;
21 if (fpclassify (r) == FP_INFINITE)
22 errno = ERANGE;
23 return r;
24}
lib/libc/musl/src/math/fdimf.c deleted-10
......@@ -1,10 +0,0 @@
1#include <math.h>
2
3float fdimf(float x, float y)
4{
5 if (isnan(x))
6 return x;
7 if (isnan(y))
8 return y;
9 return x > y ? x - y : 0;
10}
lib/libc/musl/src/math/fdiml.c deleted-18
......@@ -1,18 +0,0 @@
1#include <math.h>
2#include <float.h>
3
4#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
5long double fdiml(long double x, long double y)
6{
7 return fdim(x, y);
8}
9#else
10long double fdiml(long double x, long double y)
11{
12 if (isnan(x))
13 return x;
14 if (isnan(y))
15 return y;
16 return x > y ? x - y : 0;
17}
18#endif
src/libs/mingw.zig-1
......@@ -850,7 +850,6 @@ const mingw32_x86_src = [_][]const u8{
850850 "complex" ++ path.sep_str ++ "ctanl.c",
851851 "math" ++ path.sep_str ++ "cbrtl.c",
852852 "math" ++ path.sep_str ++ "erfl.c",
853 "math" ++ path.sep_str ++ "fdiml.c",
854853 "math" ++ path.sep_str ++ "fmal.c",
855854 "math" ++ path.sep_str ++ "llrintl.c",
856855 "math" ++ path.sep_str ++ "llroundl.c",
src/libs/musl.zig-2
......@@ -823,8 +823,6 @@ const src_files = [_][]const u8{
823823 "musl/src/math/expm1l.c",
824824 "musl/src/math/__expo2.c",
825825 "musl/src/math/__expo2f.c",
826 "musl/src/math/fdimf.c",
827 "musl/src/math/fdiml.c",
828826 "musl/src/math/fma.c",
829827 "musl/src/math/fmaf.c",
830828 "musl/src/math/fmal.c",
src/libs/wasi_libc.zig-2
......@@ -692,8 +692,6 @@ const libc_top_half_src_files = [_][]const u8{
692692 "musl/src/math/expm1.c",
693693 "musl/src/math/expm1f.c",
694694 "musl/src/math/expm1l.c",
695 "musl/src/math/fdimf.c",
696 "musl/src/math/fdiml.c",
697695 "musl/src/math/fma.c",
698696 "musl/src/math/fmaf.c",
699697 "musl/src/math/ilogb.c",
test/libc.zig+2-2
......@@ -198,8 +198,8 @@ pub fn addCases(cases: *tests.LibcContext) void {
198198 cases.addLibcTestCase("math/fabsf.c", true, .{});
199199 cases.addLibcTestCase("math/fabsl.c", true, .{});
200200 cases.addLibcTestCase("math/fdim.c", true, .{});
201 // cases.addLibcTestCase("math/fdimf.c", true, .{});
202 // cases.addLibcTestCase("math/fdiml.c", true, .{});
201 cases.addLibcTestCase("math/fdimf.c", true, .{});
202 cases.addLibcTestCase("math/fdiml.c", true, .{});
203203 cases.addLibcTestCase("math/fenv.c", true, .{});
204204 cases.addLibcTestCase("math/floor.c", true, .{});
205205 cases.addLibcTestCase("math/floorf.c", true, .{});