From 9399fcddce0bcd8e987b053f3946aa1b0ff2ef0a Mon Sep 17 00:00:00 2001 From: David Senoner Date: Tue, 26 Aug 2025 14:16:55 +0200 Subject: [PATCH] libc: use zig isnan and derivates for mingw --- lib/c.zig | 1 + lib/c/math.zig | 26 ++++++++++++++++++++++++++ lib/libc/mingw/math/isnan.c | 31 ------------------------------- lib/libc/mingw/math/isnanf.c | 27 --------------------------- lib/libc/mingw/math/isnanl.c | 32 -------------------------------- src/libs/mingw.zig | 3 --- 6 files changed, 27 insertions(+), 93 deletions(-) create mode 100644 lib/c/math.zig delete mode 100644 lib/libc/mingw/math/isnan.c delete mode 100644 lib/libc/mingw/math/isnanf.c delete mode 100644 lib/libc/mingw/math/isnanl.c diff --git a/lib/c.zig b/lib/c.zig index dec072e409..d59ef950f4 100644 --- a/lib/c.zig +++ b/lib/c.zig @@ -16,6 +16,7 @@ else comptime { _ = @import("c/inttypes.zig"); _ = @import("c/stdlib.zig"); + _ = @import("c/math.zig"); if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) { // Files specific to musl and wasi-libc. diff --git a/lib/c/math.zig b/lib/c/math.zig new file mode 100644 index 0000000000..161e400e6d --- /dev/null +++ b/lib/c/math.zig @@ -0,0 +1,26 @@ +const std = @import("std"); +const common = @import("common.zig"); +const builtin = @import("builtin"); + +comptime { + if (builtin.target.isMinGW()) { + @export(&isnan, .{ .name = "isnan", .linkage = common.linkage, .visibility = common.visibility }); + @export(&isnan, .{ .name = "__isnan", .linkage = common.linkage, .visibility = common.visibility }); + @export(&isnanf, .{ .name = "isnanf", .linkage = common.linkage, .visibility = common.visibility }); + @export(&isnanf, .{ .name = "__isnanf", .linkage = common.linkage, .visibility = common.visibility }); + @export(&isnanl, .{ .name = "isnanl", .linkage = common.linkage, .visibility = common.visibility }); + @export(&isnanl, .{ .name = "__isnanl", .linkage = common.linkage, .visibility = common.visibility }); + } +} + +fn isnan(x: f64) callconv(.c) c_int { + return if (std.math.isNan(x)) 1 else 0; +} + +fn isnanf(x: f32) callconv(.c) c_int { + return if (std.math.isNan(x)) 1 else 0; +} + +fn isnanl(x: c_longdouble) callconv(.c) c_int { + return if (std.math.isNan(x)) 1 else 0; +} diff --git a/lib/libc/mingw/math/isnan.c b/lib/libc/mingw/math/isnan.c deleted file mode 100644 index 57423c2930..0000000000 --- a/lib/libc/mingw/math/isnan.c +++ /dev/null @@ -1,31 +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 - -int -__isnan (double _x) -{ -#if defined(__x86_64__) || defined(_AMD64_) || defined(__arm__) || defined(_ARM_) || defined(__aarch64__) || defined(_ARM64_) - __mingw_dbl_type_t hlp; - int l, h; - - hlp.x = _x; - l = hlp.lh.low; - h = hlp.lh.high & 0x7fffffff; - h |= (unsigned int) (l | -l) >> 31; - h = 0x7ff00000 - h; - return (int) ((unsigned int) h) >> 31; -#elif defined(__i386__) || defined(_X86_) - unsigned short _sw; - __asm__ __volatile__ ("fxam;" - "fstsw %%ax": "=a" (_sw) : "t" (_x)); - return (_sw & (FP_NAN | FP_NORMAL | FP_INFINITE | FP_ZERO | FP_SUBNORMAL)) - == FP_NAN; -#endif -} - -#undef isnan -int __attribute__ ((alias ("__isnan"))) isnan (double); diff --git a/lib/libc/mingw/math/isnanf.c b/lib/libc/mingw/math/isnanf.c deleted file mode 100644 index 0c8e5041e7..0000000000 --- a/lib/libc/mingw/math/isnanf.c +++ /dev/null @@ -1,27 +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 -int -__isnanf (float _x) -{ -#if defined(__x86_64__) || defined(_AMD64_) || defined(__arm__) || defined(_ARM_) || defined(__aarch64__) || defined(_ARM64_) - __mingw_flt_type_t hlp; - int i; - - hlp.x = _x; - i = hlp.val & 0x7fffffff; - i = 0x7f800000 - i; - return (int) (((unsigned int) i) >> 31); -#elif defined(__i386__) || defined(_X86_) - unsigned short _sw; - __asm__ __volatile__ ("fxam;" - "fstsw %%ax": "=a" (_sw) : "t" (_x) ); - return (_sw & (FP_NAN | FP_NORMAL | FP_INFINITE | FP_ZERO | FP_SUBNORMAL)) - == FP_NAN; -#endif -} - -int __attribute__ ((alias ("__isnanf"))) isnanf (float); diff --git a/lib/libc/mingw/math/isnanl.c b/lib/libc/mingw/math/isnanl.c deleted file mode 100644 index fdc2529a9f..0000000000 --- a/lib/libc/mingw/math/isnanl.c +++ /dev/null @@ -1,32 +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 - -int -__isnanl (long double _x) -{ -#if __SIZEOF_LONG_DOUBLE__ == __SIZEOF_DOUBLE__ - return __isnan(_x); -#elif defined(__x86_64__) || defined(_AMD64_) - __mingw_ldbl_type_t ld; - int xx, signexp; - - ld.x = _x; - signexp = (ld.lh.sign_exponent & 0x7fff) << 1; - xx = (int) (ld.lh.low | (ld.lh.high & 0x7fffffffu)); /* explicit */ - signexp |= (unsigned int) (xx | (-xx)) >> 31; - signexp = 0xfffe - signexp; - return (int) ((unsigned int) signexp) >> 16; -#elif defined(__i386__) || defined(_X86_) - unsigned short _sw; - __asm__ __volatile__ ("fxam;" - "fstsw %%ax": "=a" (_sw) : "t" (_x)); - return (_sw & (FP_NAN | FP_NORMAL | FP_INFINITE | FP_ZERO | FP_SUBNORMAL)) - == FP_NAN; -#endif -} - -int __attribute__ ((alias ("__isnanl"))) isnanl (long double); diff --git a/src/libs/mingw.zig b/src/libs/mingw.zig index 612a5a0db6..1ef660fee2 100644 --- a/src/libs/mingw.zig +++ b/src/libs/mingw.zig @@ -577,9 +577,6 @@ const mingw32_generic_src = [_][]const u8{ "math" ++ path.sep_str ++ "frexpl.c", "math" ++ path.sep_str ++ "hypotf.c", "math" ++ path.sep_str ++ "hypotl.c", - "math" ++ path.sep_str ++ "isnan.c", - "math" ++ path.sep_str ++ "isnanf.c", - "math" ++ path.sep_str ++ "isnanl.c", "math" ++ path.sep_str ++ "ldexpf.c", "math" ++ path.sep_str ++ "lgamma.c", "math" ++ path.sep_str ++ "lgammaf.c",