From 521a093334eb92b4fd2430b9018ed94b912857a8 Mon Sep 17 00:00:00 2001 From: mihael Date: Wed, 18 Mar 2026 23:02:53 +0100 Subject: [PATCH] `libzigc/math`: Implement `frexpf` The `frexp` implementation was generalized so it can be used for `f32` and `c_longdouble` types as well. 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= -Dtest-filter=frexp -fqemu -fwasmtime --summary line Build Summary: 737/737 steps succeeded $ stage4/bin/zig build test-libc -Dlibc-test-path= -Dtest-filter=frexpf -fqemu -fwasmtime --summary line Build Summary: 369/369 steps succeeded ``` --- lib/c/math.zig | 13 +++++++++++-- lib/libc/mingw/math/frexpf.c | 13 ------------- lib/libc/musl/src/math/frexpf.c | 23 ----------------------- src/libs/mingw.zig | 1 - src/libs/musl.zig | 1 - src/libs/wasi_libc.zig | 1 - 6 files changed, 11 insertions(+), 41 deletions(-) delete mode 100644 lib/libc/mingw/math/frexpf.c delete mode 100644 lib/libc/musl/src/math/frexpf.c diff --git a/lib/c/math.zig b/lib/c/math.zig index 8fe93716dbebc34bf2bc5276d764c4e93ffd8a16..1f379792a7c61145815193c33496872e118ea38b 100644 --- a/lib/c/math.zig +++ b/lib/c/math.zig @@ -36,6 +36,7 @@ comptime { if (builtin.target.isMinGW() or builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) { symbol(&coshf, "coshf"); + symbol(&frexpf, "frexpf"); symbol(&hypotf, "hypotf"); symbol(&hypotl, "hypotl"); symbol(&modff, "modff"); @@ -162,7 +163,7 @@ fn fdim(x: f64, y: f64) callconv(.c) f64 { return 0; } -fn frexp(x: f64, e: *c_int) callconv(.c) f64 { +fn frexpGeneric(comptime T: type, x: T, e: *c_int) T { // libc expects `*e` to be unspecified in this case; an unspecified C value // should be a valid value of the relevant type, yet Zig's std // implementation sets it to `undefined` -- which can even be nonsense @@ -170,7 +171,7 @@ fn frexp(x: f64, e: *c_int) callconv(.c) f64 { // int value in Zig -- a zero. // // This mirrors the handling of infinities, where libc also expects - // unspecified for the value `*e` and Zig std sets it to a zero. + // unspecified for the value of `*e` and Zig std sets it to a zero. if (math.isNan(x)) { e.* = 0; return x; @@ -181,6 +182,14 @@ fn frexp(x: f64, e: *c_int) callconv(.c) f64 { return r.significand; } +fn frexp(x: f64, e: *c_int) callconv(.c) f64 { + return frexpGeneric(f64, x, e); +} + +fn frexpf(x: f32, e: *c_int) callconv(.c) f32 { + return frexpGeneric(f32, x, e); +} + fn hypot(x: f64, y: f64) callconv(.c) f64 { return math.hypot(x, y); } diff --git a/lib/libc/mingw/math/frexpf.c b/lib/libc/mingw/math/frexpf.c deleted file mode 100644 index d4b7ab36c20475e51168a19909bfd643e4bac1fa..0000000000000000000000000000000000000000 --- a/lib/libc/mingw/math/frexpf.c +++ /dev/null @@ -1,13 +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. - */ -extern double __cdecl frexp(double _X,int *_Y); - -float frexpf (float, int *); -float frexpf (float x, int *expn) -{ - return (float)frexp(x, expn); -} - diff --git a/lib/libc/musl/src/math/frexpf.c b/lib/libc/musl/src/math/frexpf.c deleted file mode 100644 index 0787097527c22a4be256abb600b9a05061a2ec87..0000000000000000000000000000000000000000 --- a/lib/libc/musl/src/math/frexpf.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -float frexpf(float x, int *e) -{ - union { float f; uint32_t i; } y = { x }; - int ee = y.i>>23 & 0xff; - - if (!ee) { - if (x) { - x = frexpf(x*0x1p64, e); - *e -= 64; - } else *e = 0; - return x; - } else if (ee == 0xff) { - return x; - } - - *e = ee - 0x7e; - y.i &= 0x807ffffful; - y.i |= 0x3f000000ul; - return y.f; -} diff --git a/src/libs/mingw.zig b/src/libs/mingw.zig index f7b73cc96e8db83c8012be02c561b98c8b082ede..e9417eb9454b22a88ac4a4c9576b7ad6871576df 100644 --- a/src/libs/mingw.zig +++ b/src/libs/mingw.zig @@ -613,7 +613,6 @@ const mingw32_generic_src = [_][]const u8{ "math" ++ path.sep_str ++ "fpclassify.c", "math" ++ path.sep_str ++ "fpclassifyf.c", "math" ++ path.sep_str ++ "fpclassifyl.c", - "math" ++ path.sep_str ++ "frexpf.c", "math" ++ path.sep_str ++ "frexpl.c", "math" ++ path.sep_str ++ "ldexpf.c", "math" ++ path.sep_str ++ "lgamma.c", diff --git a/src/libs/musl.zig b/src/libs/musl.zig index e90a08f92c5f62a570728a76f27778160b88f8e4..86e0995220842f3d6b35442c2bc497df26384522 100644 --- a/src/libs/musl.zig +++ b/src/libs/musl.zig @@ -839,7 +839,6 @@ const src_files = [_][]const u8{ "musl/src/math/__fpclassify.c", "musl/src/math/__fpclassifyf.c", "musl/src/math/__fpclassifyl.c", - "musl/src/math/frexpf.c", "musl/src/math/frexpl.c", "musl/src/math/i386/acosl.s", "musl/src/math/i386/asinf.s", diff --git a/src/libs/wasi_libc.zig b/src/libs/wasi_libc.zig index d694a0381e385a5ed35b2cb27a0e25e9e144acf3..d0743dbe3d10334e7aeafbef8c71bc3ae7043195 100644 --- a/src/libs/wasi_libc.zig +++ b/src/libs/wasi_libc.zig @@ -701,7 +701,6 @@ const libc_top_half_src_files = [_][]const u8{ "musl/src/math/finitef.c", "musl/src/math/fma.c", "musl/src/math/fmaf.c", - "musl/src/math/frexpf.c", "musl/src/math/frexpl.c", "musl/src/math/ilogb.c", "musl/src/math/ilogbf.c", -- 2.54.0