| author | |
| committer | |
| log | dcffee067290c03f89c5afcacab1ba543ee28e7c |
| tree | 6ad4837f6be1f73f68b938750a526196ea0c25e3 |
| parent | ba4af7aa8a33d19bbf7b0225a2d1407d5a756c06 |
| signature |
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.4 files changed, 20 insertions(+), 25 deletions(-)
lib/c/math.zig+20| ... | @@ -60,6 +60,7 @@ comptime { | ... | @@ -60,6 +60,7 @@ comptime { |
| 60 | symbol(&exp10, "exp10"); | 60 | symbol(&exp10, "exp10"); |
| 61 | symbol(&exp10f, "exp10f"); | 61 | symbol(&exp10f, "exp10f"); |
| 62 | symbol(&fdim, "fdim"); | 62 | symbol(&fdim, "fdim"); |
| 63 | symbol(&frexp, "frexp"); | ||
| 63 | symbol(&hypot, "hypot"); | 64 | symbol(&hypot, "hypot"); |
| 64 | symbol(&modf, "modf"); | 65 | symbol(&modf, "modf"); |
| 65 | symbol(&pow, "pow"); | 66 | symbol(&pow, "pow"); |
| ... | @@ -161,6 +162,25 @@ fn fdim(x: f64, y: f64) callconv(.c) f64 { | ... | @@ -161,6 +162,25 @@ fn fdim(x: f64, y: f64) callconv(.c) f64 { |
| 161 | return 0; | 162 | return 0; |
| 162 | } | 163 | } |
| 163 | 164 | ||
| 165 | fn frexp(x: f64, e: *c_int) callconv(.c) f64 { | ||
| 166 | // libc expects `*e` to be unspecified in this case; an unspecified C value | ||
| 167 | // should be a valid value of the relevant type, yet Zig's std | ||
| 168 | // implementation sets it to `undefined` -- which can even be nonsense | ||
| 169 | // according to the type (int). Therefore, we're setting it to a valid | ||
| 170 | // int value in Zig -- a zero. | ||
| 171 | // | ||
| 172 | // This mirrors the handling of infinities, where libc also expects | ||
| 173 | // unspecified for the value `*e` and Zig std sets it to a zero. | ||
| 174 | if (math.isNan(x)) { | ||
| 175 | e.* = 0; | ||
| 176 | return x; | ||
| 177 | } | ||
| 178 | |||
| 179 | const r = math.frexp(x); | ||
| 180 | e.* = r.exponent; | ||
| 181 | return r.significand; | ||
| 182 | } | ||
| 183 | |||
| 164 | fn hypot(x: f64, y: f64) callconv(.c) f64 { | 184 | fn hypot(x: f64, y: f64) callconv(.c) f64 { |
| 165 | return math.hypot(x, y); | 185 | return math.hypot(x, y); |
| 166 | } | 186 | } |
lib/libc/musl/src/math/frexp.c deleted-23| ... | @@ -1,23 +0,0 @@ | ||
| 1 | #include <math.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | |||
| 4 | double frexp(double x, int *e) | ||
| 5 | { | ||
| 6 | 	union { double d; uint64_t i; } y = { x }; | ||
| 7 | 	int ee = y.i>>52 & 0x7ff; | ||
| 8 | |||
| 9 | 	if (!ee) { | ||
| 10 | 		if (x) { | ||
| 11 | 			x = frexp(x*0x1p64, e); | ||
| 12 | 			*e -= 64; | ||
| 13 | 		} else *e = 0; | ||
| 14 | 		return x; | ||
| 15 | 	} else if (ee == 0x7ff) { | ||
| 16 | 		return x; | ||
| 17 | 	} | ||
| 18 | |||
| 19 | 	*e = ee - 0x3fe; | ||
| 20 | 	y.i &= 0x800fffffffffffffull; | ||
| 21 | 	y.i |= 0x3fe0000000000000ull; | ||
| 22 | 	return y.d; | ||
| 23 | } | ||
src/libs/musl.zig-1| ... | @@ -839,7 +839,6 @@ const src_files = [_][]const u8{ | ... | @@ -839,7 +839,6 @@ const src_files = [_][]const u8{ |
| 839 | "musl/src/math/__fpclassify.c", | 839 | "musl/src/math/__fpclassify.c", |
| 840 | "musl/src/math/__fpclassifyf.c", | 840 | "musl/src/math/__fpclassifyf.c", |
| 841 | "musl/src/math/__fpclassifyl.c", | 841 | "musl/src/math/__fpclassifyl.c", |
| 842 | "musl/src/math/frexp.c", | ||
| 843 | "musl/src/math/frexpf.c", | 842 | "musl/src/math/frexpf.c", |
| 844 | "musl/src/math/frexpl.c", | 843 | "musl/src/math/frexpl.c", |
| 845 | "musl/src/math/i386/acosl.s", | 844 | "musl/src/math/i386/acosl.s", |
src/libs/wasi_libc.zig-1| ... | @@ -701,7 +701,6 @@ const libc_top_half_src_files = [_][]const u8{ | ... | @@ -701,7 +701,6 @@ const libc_top_half_src_files = [_][]const u8{ |
| 701 | "musl/src/math/finitef.c", | 701 | "musl/src/math/finitef.c", |
| 702 | "musl/src/math/fma.c", | 702 | "musl/src/math/fma.c", |
| 703 | "musl/src/math/fmaf.c", | 703 | "musl/src/math/fmaf.c", |
| 704 | "musl/src/math/frexp.c", | ||
| 705 | "musl/src/math/frexpf.c", | 704 | "musl/src/math/frexpf.c", |
| 706 | "musl/src/math/frexpl.c", | 705 | "musl/src/math/frexpl.c", |
| 707 | "musl/src/math/ilogb.c", | 706 | "musl/src/math/ilogb.c", |