Commit 9399fcddce

David Senoner <seda18@rolmail.net>
2025-08-26 14:16:55
libc: use zig isnan and derivates for mingw
1 parent aae5560
Changed files (6)
lib
src
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;
+}
lib/libc/mingw/math/isnan.c
@@ -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 <math.h>
-
-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);
lib/libc/mingw/math/isnanf.c
@@ -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 <math.h>
-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);
lib/libc/mingw/math/isnanl.c
@@ -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 <math.h>
-
-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);
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.
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",