| author | |
| committer | |
| log | 37d14a4f3ba8993533cc0cf76cf295ba50ea8fed |
| tree | de59859c5dbcdcaaa9c453039cb4d88ea7a9401f |
| parent | 171459f678a5e1397498d3cb3d40d4cf007baaba |
| parent | 4aadb5e4a5e09d6b2018fecb364c0fabec028c9a |
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31078
Reviewed-by: Andrew Kelley <andrew@ziglang.org>5 files changed, 10 insertions(+), 173 deletions(-)
lib/c/math.zig+10| ... | ... | @@ -39,6 +39,8 @@ comptime { |
| 39 | 39 | @export(&atanf, .{ .name = "atanf", .linkage = common.linkage, .visibility = common.visibility }); |
| 40 | 40 | @export(&atan, .{ .name = "atan", .linkage = common.linkage, .visibility = common.visibility }); |
| 41 | 41 | @export(&atanl, .{ .name = "atanl", .linkage = common.linkage, .visibility = common.visibility }); |
| 42 | @export(&cbrt, .{ .name = "cbrt", .linkage = common.linkage, .visibility = common.visibility }); | |
| 43 | @export(&cbrtf, .{ .name = "cbrtf", .linkage = common.linkage, .visibility = common.visibility }); | |
| 42 | 44 | } |
| 43 | 45 | |
| 44 | 46 | if (builtin.target.isMuslLibC()) { |
| ... | ... | @@ -106,3 +108,11 @@ fn copysign(x: f64, y: f64) callconv(.c) f64 { |
| 106 | 108 | fn copysignl(x: c_longdouble, y: c_longdouble) callconv(.c) c_longdouble { |
| 107 | 109 | return math.copysign(x, y); |
| 108 | 110 | } |
| 111 | ||
| 112 | fn cbrt(x: f64) callconv(.c) f64 { | |
| 113 | return math.cbrt(x); | |
| 114 | } | |
| 115 | ||
| 116 | fn cbrtf(x: f32) callconv(.c) f32 { | |
| 117 | return math.cbrt(x); | |
| 118 | } |
lib/libc/musl/src/math/cbrt.c deleted-103| ... | ... | @@ -1,103 +0,0 @@ |
| 1 | /* origin: FreeBSD /usr/src/lib/msun/src/s_cbrt.c */ | |
| 2 | /* | |
| 3 | * ==================================================== | |
| 4 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. | |
| 5 | * | |
| 6 | * Developed at SunPro, a Sun Microsystems, Inc. business. | |
| 7 | * Permission to use, copy, modify, and distribute this | |
| 8 | * software is freely granted, provided that this notice | |
| 9 | * is preserved. | |
| 10 | * ==================================================== | |
| 11 | * | |
| 12 | * Optimized by Bruce D. Evans. | |
| 13 | */ | |
| 14 | /* cbrt(x) | |
| 15 | * Return cube root of x | |
| 16 | */ | |
| 17 | ||
| 18 | #include <math.h> | |
| 19 | #include <stdint.h> | |
| 20 | ||
| 21 | static const uint32_t | |
| 22 | B1 = 715094163, /* B1 = (1023-1023/3-0.03306235651)*2**20 */ | |
| 23 | B2 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */ | |
| 24 | ||
| 25 | /* |1/cbrt(x) - p(x)| < 2**-23.5 (~[-7.93e-8, 7.929e-8]). */ | |
| 26 | static const double | |
| 27 | P0 = 1.87595182427177009643, /* 0x3ffe03e6, 0x0f61e692 */ | |
| 28 | P1 = -1.88497979543377169875, /* 0xbffe28e0, 0x92f02420 */ | |
| 29 | P2 = 1.621429720105354466140, /* 0x3ff9f160, 0x4a49d6c2 */ | |
| 30 | P3 = -0.758397934778766047437, /* 0xbfe844cb, 0xbee751d9 */ | |
| 31 | P4 = 0.145996192886612446982; /* 0x3fc2b000, 0xd4e4edd7 */ | |
| 32 | ||
| 33 | double cbrt(double x) | |
| 34 | { | |
| 35 | 	union {double f; uint64_t i;} u = {x}; | |
| 36 | 	double_t r,s,t,w; | |
| 37 | 	uint32_t hx = u.i>>32 & 0x7fffffff; | |
| 38 | ||
| 39 | 	if (hx >= 0x7ff00000) /* cbrt(NaN,INF) is itself */ | |
| 40 | 		return x+x; | |
| 41 | ||
| 42 | 	/* | |
| 43 | 	 * Rough cbrt to 5 bits: | |
| 44 | 	 * cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3) | |
| 45 | 	 * where e is integral and >= 0, m is real and in [0, 1), and "/" and | |
| 46 | 	 * "%" are integer division and modulus with rounding towards minus | |
| 47 | 	 * infinity. The RHS is always >= the LHS and has a maximum relative | |
| 48 | 	 * error of about 1 in 16. Adding a bias of -0.03306235651 to the | |
| 49 | 	 * (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE | |
| 50 | 	 * floating point representation, for finite positive normal values, | |
| 51 | 	 * ordinary integer divison of the value in bits magically gives | |
| 52 | 	 * almost exactly the RHS of the above provided we first subtract the | |
| 53 | 	 * exponent bias (1023 for doubles) and later add it back. We do the | |
| 54 | 	 * subtraction virtually to keep e >= 0 so that ordinary integer | |
| 55 | 	 * division rounds towards minus infinity; this is also efficient. | |
| 56 | 	 */ | |
| 57 | 	if (hx < 0x00100000) { /* zero or subnormal? */ | |
| 58 | 		u.f = x*0x1p54; | |
| 59 | 		hx = u.i>>32 & 0x7fffffff; | |
| 60 | 		if (hx == 0) | |
| 61 | 			return x; /* cbrt(0) is itself */ | |
| 62 | 		hx = hx/3 + B2; | |
| 63 | 	} else | |
| 64 | 		hx = hx/3 + B1; | |
| 65 | 	u.i &= 1ULL<<63; | |
| 66 | 	u.i |= (uint64_t)hx << 32; | |
| 67 | 	t = u.f; | |
| 68 | ||
| 69 | 	/* | |
| 70 | 	 * New cbrt to 23 bits: | |
| 71 | 	 * cbrt(x) = t*cbrt(x/t**3) ~= t*P(t**3/x) | |
| 72 | 	 * where P(r) is a polynomial of degree 4 that approximates 1/cbrt(r) | |
| 73 | 	 * to within 2**-23.5 when |r - 1| < 1/10. The rough approximation | |
| 74 | 	 * has produced t such than |t/cbrt(x) - 1| ~< 1/32, and cubing this | |
| 75 | 	 * gives us bounds for r = t**3/x. | |
| 76 | 	 * | |
| 77 | 	 * Try to optimize for parallel evaluation as in __tanf.c. | |
| 78 | 	 */ | |
| 79 | 	r = (t*t)*(t/x); | |
| 80 | 	t = t*((P0+r*(P1+r*P2))+((r*r)*r)*(P3+r*P4)); | |
| 81 | ||
| 82 | 	/* | |
| 83 | 	 * Round t away from zero to 23 bits (sloppily except for ensuring that | |
| 84 | 	 * the result is larger in magnitude than cbrt(x) but not much more than | |
| 85 | 	 * 2 23-bit ulps larger). With rounding towards zero, the error bound | |
| 86 | 	 * would be ~5/6 instead of ~4/6. With a maximum error of 2 23-bit ulps | |
| 87 | 	 * in the rounded t, the infinite-precision error in the Newton | |
| 88 | 	 * approximation barely affects third digit in the final error | |
| 89 | 	 * 0.667; the error in the rounded t can be up to about 3 23-bit ulps | |
| 90 | 	 * before the final error is larger than 0.667 ulps. | |
| 91 | 	 */ | |
| 92 | 	u.f = t; | |
| 93 | 	u.i = (u.i + 0x80000000) & 0xffffffffc0000000ULL; | |
| 94 | 	t = u.f; | |
| 95 | ||
| 96 | 	/* one step Newton iteration to 53 bits with error < 0.667 ulps */ | |
| 97 | 	s = t*t; /* t*t is exact */ | |
| 98 | 	r = x/s; /* error <= 0.5 ulps; |r| < |t| */ | |
| 99 | 	w = t+t; /* t+t is exact */ | |
| 100 | 	r = (r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */ | |
| 101 | 	t = t+t*r; /* error <= 0.5 + 0.5/3 + epsilon */ | |
| 102 | 	return t; | |
| 103 | } |
lib/libc/musl/src/math/cbrtf.c deleted-66| ... | ... | @@ -1,66 +0,0 @@ |
| 1 | /* origin: FreeBSD /usr/src/lib/msun/src/s_cbrtf.c */ | |
| 2 | /* | |
| 3 | * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. | |
| 4 | * Debugged and optimized by Bruce D. Evans. | |
| 5 | */ | |
| 6 | /* | |
| 7 | * ==================================================== | |
| 8 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. | |
| 9 | * | |
| 10 | * Developed at SunPro, a Sun Microsystems, Inc. business. | |
| 11 | * Permission to use, copy, modify, and distribute this | |
| 12 | * software is freely granted, provided that this notice | |
| 13 | * is preserved. | |
| 14 | * ==================================================== | |
| 15 | */ | |
| 16 | /* cbrtf(x) | |
| 17 | * Return cube root of x | |
| 18 | */ | |
| 19 | ||
| 20 | #include <math.h> | |
| 21 | #include <stdint.h> | |
| 22 | ||
| 23 | static const unsigned | |
| 24 | B1 = 709958130, /* B1 = (127-127.0/3-0.03306235651)*2**23 */ | |
| 25 | B2 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */ | |
| 26 | ||
| 27 | float cbrtf(float x) | |
| 28 | { | |
| 29 | 	double_t r,T; | |
| 30 | 	union {float f; uint32_t i;} u = {x}; | |
| 31 | 	uint32_t hx = u.i & 0x7fffffff; | |
| 32 | ||
| 33 | 	if (hx >= 0x7f800000) /* cbrt(NaN,INF) is itself */ | |
| 34 | 		return x + x; | |
| 35 | ||
| 36 | 	/* rough cbrt to 5 bits */ | |
| 37 | 	if (hx < 0x00800000) { /* zero or subnormal? */ | |
| 38 | 		if (hx == 0) | |
| 39 | 			return x; /* cbrt(+-0) is itself */ | |
| 40 | 		u.f = x*0x1p24f; | |
| 41 | 		hx = u.i & 0x7fffffff; | |
| 42 | 		hx = hx/3 + B2; | |
| 43 | 	} else | |
| 44 | 		hx = hx/3 + B1; | |
| 45 | 	u.i &= 0x80000000; | |
| 46 | 	u.i |= hx; | |
| 47 | ||
| 48 | 	/* | |
| 49 | 	 * First step Newton iteration (solving t*t-x/t == 0) to 16 bits. In | |
| 50 | 	 * double precision so that its terms can be arranged for efficiency | |
| 51 | 	 * without causing overflow or underflow. | |
| 52 | 	 */ | |
| 53 | 	T = u.f; | |
| 54 | 	r = T*T*T; | |
| 55 | 	T = T*((double_t)x+x+r)/(x+r+r); | |
| 56 | ||
| 57 | 	/* | |
| 58 | 	 * Second step Newton iteration to 47 bits. In double precision for | |
| 59 | 	 * efficiency and accuracy. | |
| 60 | 	 */ | |
| 61 | 	r = T*T*T; | |
| 62 | 	T = T*((double_t)x+x+r)/(x+r+r); | |
| 63 | ||
| 64 | 	/* rounding to 24 bits is perfect in round-to-nearest mode */ | |
| 65 | 	return T; | |
| 66 | } |
src/libs/musl.zig-2| ... | ... | @@ -837,8 +837,6 @@ const src_files = [_][]const u8{ |
| 837 | 837 | "musl/src/math/atanh.c", |
| 838 | 838 | "musl/src/math/atanhf.c", |
| 839 | 839 | "musl/src/math/atanhl.c", |
| 840 | "musl/src/math/cbrt.c", | |
| 841 | "musl/src/math/cbrtf.c", | |
| 842 | 840 | "musl/src/math/cbrtl.c", |
| 843 | 841 | "musl/src/math/__cos.c", |
| 844 | 842 | "musl/src/math/__cosdf.c", |
src/libs/wasi_libc.zig-2| ... | ... | @@ -701,8 +701,6 @@ const libc_top_half_src_files = [_][]const u8{ |
| 701 | 701 | "musl/src/math/atanh.c", |
| 702 | 702 | "musl/src/math/atanhf.c", |
| 703 | 703 | "musl/src/math/atanhl.c", |
| 704 | "musl/src/math/cbrt.c", | |
| 705 | "musl/src/math/cbrtf.c", | |
| 706 | 704 | "musl/src/math/cbrtl.c", |
| 707 | 705 | "musl/src/math/__cos.c", |
| 708 | 706 | "musl/src/math/__cosdf.c", |