commit 4cad6f5372f6f17e249c24fba4d23466fa853fc0 (tree)
parent 014178725744eda46db04ccfe44187ef616ceaf7
Author: mihael <hi@mihaelm.com>
Date: Sun, 15 Mar 2026 21:22:01 +0100
`libzigc`: Implement `modfl`
The changes were tested by running:
```
$ ./build/stage3/bin/zig build -p stage4 -Denable-llvm -Dno-lib
$ stage4/bin/zig build test-libc -Dlibc-test-path=<LIBC-TEST-PATH> -Dtest-filter=modfl -fqemu -fwasmtime --summary line
Build Summary: 369/369 steps succeeded
```
Diffstat:
6 files changed, 9 insertions(+), 99 deletions(-)
diff --git a/lib/c/math.zig b/lib/c/math.zig
@@ -39,6 +39,7 @@ comptime {
symbol(&hypotf, "hypotf");
symbol(&hypotl, "hypotl");
symbol(&modff, "modff");
+ symbol(&modfl, "modfl");
symbol(&nan, "nan");
symbol(&nanf, "nanf");
symbol(&nanl, "nanl");
@@ -201,11 +202,16 @@ fn modff(x: f32, iptr: *f32) callconv(.c) f32 {
return modfGeneric(f32, x, iptr);
}
+fn modfl(x: c_longdouble, iptr: *c_longdouble) callconv(.c) c_longdouble {
+ return modfGeneric(c_longdouble, x, iptr);
+}
+
fn testModf(comptime T: type) !void {
// Choose the appropriate `modf` impl to test based on type
const f = switch (T) {
- f64 => modf,
f32 => modff,
+ f64 => modf,
+ c_longdouble => modfl,
else => @compileError("modf not implemented for " ++ @typeName(T)),
};
@@ -248,8 +254,9 @@ fn testModf(comptime T: type) !void {
}
test "modf" {
- try testModf(f64);
try testModf(f32);
+ try testModf(f64);
+ try testModf(c_longdouble);
}
fn nan(_: [*:0]const c_char) callconv(.c) f64 {
diff --git a/lib/libc/mingw/math/modfl.c b/lib/libc/mingw/math/modfl.c
@@ -1,41 +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 <fenv.h>
-#include <math.h>
-#include <errno.h>
-
-long double
-modfl (long double value, long double* iptr)
-{
- long double int_part = 0.0L;
- /* truncate */
-#if (defined(_AMD64_) && !defined(_ARM64EC_)) || (defined(__x86_64__) && !defined(__arm64ec__))
- asm volatile ("subq $8, %%rsp\n"
- "fnstcw 4(%%rsp)\n"
- "movzwl 4(%%rsp), %%eax\n"
- "orb $12, %%ah\n"
- "movw %%ax, (%%rsp)\n"
- "fldcw (%%rsp)\n"
- "frndint\n"
- "fldcw 4(%%rsp)\n"
- "addq $8, %%rsp\n" : "=t" (int_part) : "0" (value) : "eax"); /* round */
-#elif defined(_X86_) || defined(__i386__)
- asm volatile ("push %%eax\n\tsubl $8, %%esp\n"
- "fnstcw 4(%%esp)\n"
- "movzwl 4(%%esp), %%eax\n"
- "orb $12, %%ah\n"
- "movw %%ax, (%%esp)\n"
- "fldcw (%%esp)\n"
- "frndint\n"
- "fldcw 4(%%esp)\n"
- "addl $8, %%esp\n\tpop %%eax\n" : "=t" (int_part) : "0" (value) : "eax"); /* round */
-#else
- int_part = truncl(value);
-#endif
- if (iptr)
- *iptr = int_part;
- return (isinf (value) ? 0.0L : value - int_part);
-}
diff --git a/lib/libc/musl/src/math/modfl.c b/lib/libc/musl/src/math/modfl.c
@@ -1,53 +0,0 @@
-#include "libm.h"
-
-#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
-long double modfl(long double x, long double *iptr)
-{
- double d;
- long double r;
-
- r = modf(x, &d);
- *iptr = d;
- return r;
-}
-#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
-
-static const long double toint = 1/LDBL_EPSILON;
-
-long double modfl(long double x, long double *iptr)
-{
- union ldshape u = {x};
- int e = (u.i.se & 0x7fff) - 0x3fff;
- int s = u.i.se >> 15;
- long double absx;
- long double y;
-
- /* no fractional part */
- if (e >= LDBL_MANT_DIG-1) {
- *iptr = x;
- if (isnan(x))
- return x;
- return s ? -0.0 : 0.0;
- }
-
- /* no integral part*/
- if (e < 0) {
- *iptr = s ? -0.0 : 0.0;
- return x;
- }
-
- /* raises spurious inexact */
- absx = s ? -x : x;
- y = absx + toint - toint - absx;
- if (y == 0) {
- *iptr = x;
- return s ? -0.0 : 0.0;
- }
- if (y > 0)
- y -= 1;
- if (s)
- y = -y;
- *iptr = x + y;
- return -y;
-}
-#endif
diff --git a/src/libs/mingw.zig b/src/libs/mingw.zig
@@ -619,7 +619,6 @@ const mingw32_generic_src = [_][]const u8{
"math" ++ path.sep_str ++ "lgamma.c",
"math" ++ path.sep_str ++ "lgammaf.c",
"math" ++ path.sep_str ++ "lgammal.c",
- "math" ++ path.sep_str ++ "modfl.c",
"math" ++ path.sep_str ++ "powi.c",
"math" ++ path.sep_str ++ "powif.c",
"math" ++ path.sep_str ++ "powil.c",
diff --git a/src/libs/musl.zig b/src/libs/musl.zig
@@ -934,7 +934,6 @@ const src_files = [_][]const u8{
"musl/src/math/__math_uflowf.c",
"musl/src/math/__math_xflow.c",
"musl/src/math/__math_xflowf.c",
- "musl/src/math/modfl.c",
"musl/src/math/nearbyint.c",
"musl/src/math/nearbyintf.c",
"musl/src/math/nearbyintl.c",
diff --git a/src/libs/wasi_libc.zig b/src/libs/wasi_libc.zig
@@ -755,7 +755,6 @@ const libc_top_half_src_files = [_][]const u8{
"musl/src/math/__math_uflowf.c",
"musl/src/math/__math_xflow.c",
"musl/src/math/__math_xflowf.c",
- "musl/src/math/modfl.c",
"musl/src/math/nearbyintl.c",
"musl/src/math/nextafter.c",
"musl/src/math/nextafterf.c",