| 1 | /** |
| 2 | * This file has no copyright assigned and is placed in the Public Domain. |
| 3 | * This file is part of the mingw-w64 runtime package. |
| 4 | * No warranty is given; refer to the file DISCLAIMER.PD within this package. |
| 5 | */ |
| 6 | #include "cephes_mconf.h" |
| 7 | |
| 8 | static const long double CBRT2 = 1.2599210498948731647672L; |
| 9 | static const long double CBRT4 = 1.5874010519681994747517L; |
| 10 | static const long double CBRT2I = 0.79370052598409973737585L; |
| 11 | static const long double CBRT4I = 0.62996052494743658238361L; |
| 12 | |
| 13 | extern long double ldexpl(long double,int); |
| 14 | |
| 15 | long double cbrtl(long double x) |
| 16 | { |
| 17 | 	int e, rem, sign; |
| 18 | 	long double z; |
| 19 | |
| 20 | 	if (!isfinite (x) || x == 0.0L) |
| 21 | 		return (x); |
| 22 | |
| 23 | 	if (x > 0) |
| 24 | 		sign = 1; |
| 25 | 	else |
| 26 | 	{ |
| 27 | 		sign = -1; |
| 28 | 		x = -x; |
| 29 | 	} |
| 30 | |
| 31 | 	z = x; |
| 32 | 	/* extract power of 2, leaving |
| 33 | 	 * mantissa between 0.5 and 1 |
| 34 | 	 */ |
| 35 | 	x = frexpl(x, &e); |
| 36 | |
| 37 | 	/* Approximate cube root of number between .5 and 1, |
| 38 | 	 * peak relative error = 1.2e-6 |
| 39 | 	 */ |
| 40 | 	x = (((( 1.3584464340920900529734e-1L * x |
| 41 | 	 - 6.3986917220457538402318e-1L) * x |
| 42 | 	 + 1.2875551670318751538055e0L) * x |
| 43 | 	 - 1.4897083391357284957891e0L) * x |
| 44 | 	 + 1.3304961236013647092521e0L) * x |
| 45 | 	 + 3.7568280825958912391243e-1L; |
| 46 | |
| 47 | 	/* exponent divided by 3 */ |
| 48 | 	if (e >= 0) |
| 49 | 	{ |
| 50 | 		rem = e; |
| 51 | 		e /= 3; |
| 52 | 		rem -= 3*e; |
| 53 | 		if (rem == 1) |
| 54 | 			x *= CBRT2; |
| 55 | 		else if (rem == 2) |
| 56 | 			x *= CBRT4; |
| 57 | 	} |
| 58 | 	else |
| 59 | 	{ /* argument less than 1 */ |
| 60 | 		e = -e; |
| 61 | 		rem = e; |
| 62 | 		e /= 3; |
| 63 | 		rem -= 3*e; |
| 64 | 		if (rem == 1) |
| 65 | 			x *= CBRT2I; |
| 66 | 		else if (rem == 2) |
| 67 | 			x *= CBRT4I; |
| 68 | 		e = -e; |
| 69 | 	} |
| 70 | |
| 71 | 	/* multiply by power of 2 */ |
| 72 | 	x = ldexpl(x, e); |
| 73 | |
| 74 | 	/* Newton iteration */ |
| 75 | |
| 76 | 	x -= ( x - (z/(x*x)) )*0.3333333333333333333333L; |
| 77 | 	x -= ( x - (z/(x*x)) )*0.3333333333333333333333L; |
| 78 | |
| 79 | 	if (sign < 0) |
| 80 | 		x = -x; |
| 81 | 	return (x); |
| 82 | } |