libzigc/math: Implement frexp

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=frexp -fqemu -fwasmtime --summary line
Build Summary: 737/737 steps succeeded
```

The tests were passing even when it was a straightforward calling of Zig
std library, but I wanted the `x is NaN` special case to match the
behaviour described in `libc` manpages, and for it to be consistent
with how infinities as arguments are handled in Zig.
This commit is contained in:
mihael
2026-03-18 20:32:18 +01:00
parent ba4af7aa8a
commit dcffee0672
4 changed files with 20 additions and 25 deletions
+20
View File
@@ -60,6 +60,7 @@ comptime {
symbol(&exp10, "exp10");
symbol(&exp10f, "exp10f");
symbol(&fdim, "fdim");
symbol(&frexp, "frexp");
symbol(&hypot, "hypot");
symbol(&modf, "modf");
symbol(&pow, "pow");
@@ -161,6 +162,25 @@ fn fdim(x: f64, y: f64) callconv(.c) f64 {
return 0;
}
fn frexp(x: f64, e: *c_int) callconv(.c) f64 {
// 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
// according to the type (int). Therefore, we're setting it to a valid
// 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.
if (math.isNan(x)) {
e.* = 0;
return x;
}
const r = math.frexp(x);
e.* = r.exponent;
return r.significand;
}
fn hypot(x: f64, y: f64) callconv(.c) f64 {
return math.hypot(x, y);
}
-23
View File
@@ -1,23 +0,0 @@
#include <math.h>
#include <stdint.h>
double frexp(double x, int *e)
{
union { double d; uint64_t i; } y = { x };
int ee = y.i>>52 & 0x7ff;
if (!ee) {
if (x) {
x = frexp(x*0x1p64, e);
*e -= 64;
} else *e = 0;
return x;
} else if (ee == 0x7ff) {
return x;
}
*e = ee - 0x3fe;
y.i &= 0x800fffffffffffffull;
y.i |= 0x3fe0000000000000ull;
return y.d;
}
-1
View File
@@ -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/frexp.c",
"musl/src/math/frexpf.c",
"musl/src/math/frexpl.c",
"musl/src/math/i386/acosl.s",
-1
View File
@@ -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/frexp.c",
"musl/src/math/frexpf.c",
"musl/src/math/frexpl.c",
"musl/src/math/ilogb.c",