From a05a25e2bbcd26ce84fa16b1a75ae13b1ecd50d2 Mon Sep 17 00:00:00 2001 From: badayvedat Date: Thu, 16 Apr 2026 01:11:04 +0200 Subject: [PATCH] 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 Co-authored-by: badayvedat Co-committed-by: badayvedat --- lib/c/math.zig | 27 ++++++++++++++++++++------- lib/libc/mingw/math/fdiml.c | 24 ------------------------ lib/libc/musl/src/math/fdimf.c | 10 ---------- lib/libc/musl/src/math/fdiml.c | 18 ------------------ src/libs/mingw.zig | 1 - src/libs/musl.zig | 2 -- src/libs/wasi_libc.zig | 2 -- test/libc.zig | 4 ++-- 8 files changed, 22 insertions(+), 66 deletions(-) delete mode 100644 lib/libc/mingw/math/fdiml.c delete mode 100644 lib/libc/musl/src/math/fdimf.c delete mode 100644 lib/libc/musl/src/math/fdiml.c diff --git a/lib/c/math.zig b/lib/c/math.zig index 21d694f6e9..18ba8d9e75 100644 --- a/lib/c/math.zig +++ b/lib/c/math.zig @@ -45,6 +45,7 @@ comptime { if ((builtin.target.isMinGW() and @sizeOf(f64) != @sizeOf(c_longdouble)) or builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) { symbol(&atanl, "atanl"); symbol(©signl, "copysignl"); + symbol(&fdiml, "fdiml"); symbol(&nanl, "nanl"); } @@ -67,6 +68,7 @@ comptime { symbol(&exp10, "exp10"); symbol(&exp10f, "exp10f"); symbol(&fdim, "fdim"); + symbol(&fdimf, "fdimf"); symbol(&finite, "finite"); symbol(&finitef, "finitef"); symbol(&frexp, "frexp"); @@ -160,19 +162,30 @@ fn exp10f(x: f32) callconv(.c) f32 { return math.pow(f32, 10.0, x); } -fn fdim(x: f64, y: f64) callconv(.c) f64 { - if (math.isNan(x)) { +fn fdimGeneric(comptime T: type, x: T, y: T) T { + if (math.isNan(x)) return x; - } - if (math.isNan(y)) { + + if (math.isNan(y)) return y; - } - if (x > y) { + + if (x > y) return x - y; - } return 0; } +fn fdim(x: f64, y: f64) callconv(.c) f64 { + return fdimGeneric(f64, x, y); +} + +fn fdimf(x: f32, y: f32) callconv(.c) f32 { + return fdimGeneric(f32, x, y); +} + +fn fdiml(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble { + return fdimGeneric(c_longdouble, x, y); +} + fn finite(x: f64) callconv(.c) c_int { return if (math.isFinite(x)) 1 else 0; } diff --git a/lib/libc/mingw/math/fdiml.c b/lib/libc/mingw/math/fdiml.c deleted file mode 100644 index 3be0679acd..0000000000 --- a/lib/libc/mingw/math/fdiml.c +++ /dev/null @@ -1,24 +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 -#include - -long double -fdiml (long double x, long double y) -{ - int cx = fpclassify (x), cy = fpclassify (y); - long double r; - - if (cx == FP_NAN || cy == FP_NAN - || (y < 0 && cx == FP_INFINITE && cy == FP_INFINITE)) - return x - y; /* Take care invalid flag is raised. */ - if (x <= y) - return 0.0; - r = x - y; - if (fpclassify (r) == FP_INFINITE) - errno = ERANGE; - return r; -} diff --git a/lib/libc/musl/src/math/fdimf.c b/lib/libc/musl/src/math/fdimf.c deleted file mode 100644 index 543c3648e3..0000000000 --- a/lib/libc/musl/src/math/fdimf.c +++ /dev/null @@ -1,10 +0,0 @@ -#include - -float fdimf(float x, float y) -{ - if (isnan(x)) - return x; - if (isnan(y)) - return y; - return x > y ? x - y : 0; -} diff --git a/lib/libc/musl/src/math/fdiml.c b/lib/libc/musl/src/math/fdiml.c deleted file mode 100644 index 62e29b7df3..0000000000 --- a/lib/libc/musl/src/math/fdiml.c +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include - -#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 -long double fdiml(long double x, long double y) -{ - return fdim(x, y); -} -#else -long double fdiml(long double x, long double y) -{ - if (isnan(x)) - return x; - if (isnan(y)) - return y; - return x > y ? x - y : 0; -} -#endif diff --git a/src/libs/mingw.zig b/src/libs/mingw.zig index 85fc522385..6ad4e65d16 100644 --- a/src/libs/mingw.zig +++ b/src/libs/mingw.zig @@ -850,7 +850,6 @@ const mingw32_x86_src = [_][]const u8{ "complex" ++ path.sep_str ++ "ctanl.c", "math" ++ path.sep_str ++ "cbrtl.c", "math" ++ path.sep_str ++ "erfl.c", - "math" ++ path.sep_str ++ "fdiml.c", "math" ++ path.sep_str ++ "fmal.c", "math" ++ path.sep_str ++ "llrintl.c", "math" ++ path.sep_str ++ "llroundl.c", diff --git a/src/libs/musl.zig b/src/libs/musl.zig index 0b9b1f7d1f..bf02e2b82c 100644 --- a/src/libs/musl.zig +++ b/src/libs/musl.zig @@ -823,8 +823,6 @@ const src_files = [_][]const u8{ "musl/src/math/expm1l.c", "musl/src/math/__expo2.c", "musl/src/math/__expo2f.c", - "musl/src/math/fdimf.c", - "musl/src/math/fdiml.c", "musl/src/math/fma.c", "musl/src/math/fmaf.c", "musl/src/math/fmal.c", diff --git a/src/libs/wasi_libc.zig b/src/libs/wasi_libc.zig index df27410446..1fde2f0ef9 100644 --- a/src/libs/wasi_libc.zig +++ b/src/libs/wasi_libc.zig @@ -692,8 +692,6 @@ const libc_top_half_src_files = [_][]const u8{ "musl/src/math/expm1.c", "musl/src/math/expm1f.c", "musl/src/math/expm1l.c", - "musl/src/math/fdimf.c", - "musl/src/math/fdiml.c", "musl/src/math/fma.c", "musl/src/math/fmaf.c", "musl/src/math/ilogb.c", diff --git a/test/libc.zig b/test/libc.zig index 999e55e763..bd10130c99 100644 --- a/test/libc.zig +++ b/test/libc.zig @@ -198,8 +198,8 @@ pub fn addCases(cases: *tests.LibcContext) void { cases.addLibcTestCase("math/fabsf.c", true, .{}); cases.addLibcTestCase("math/fabsl.c", true, .{}); cases.addLibcTestCase("math/fdim.c", true, .{}); - // cases.addLibcTestCase("math/fdimf.c", true, .{}); - // cases.addLibcTestCase("math/fdiml.c", true, .{}); + cases.addLibcTestCase("math/fdimf.c", true, .{}); + cases.addLibcTestCase("math/fdiml.c", true, .{}); cases.addLibcTestCase("math/fenv.c", true, .{}); cases.addLibcTestCase("math/floor.c", true, .{}); cases.addLibcTestCase("math/floorf.c", true, .{});