1 /*- 2 * ==================================================== 3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 4 * Copyright (c) 2009-2011, Bruce D. Evans, Steven G. Kargl, David Schultz. 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 * The argument reduction and testing for exceptional cases was 13 * written by Steven G. Kargl with input from Bruce D. Evans 14 * and David A. Schultz. 15 */ 16 17 #include <sys/cdefs.h> 18 #if 0 19 __FBSDID("$FreeBSD: src/lib/msun/src/s_cbrtl.c,v 1.1 2011/03/12 19:37:35 kargl Exp $"); 20 #endif 21 22 #include <float.h> 23 #include <ieeefp.h> 24 #include <math.h> 25 26 #include "math_private.h" 27 28 #define BIAS (LDBL_MAX_EXP - 1) 29 30 static const unsigned 31 B1 = 709958130; /* B1 = (127-127.0/3-0.03306235651)*2**23 */ 32 33 long double 34 cbrtl(long double x) 35 { 36 long double v, r, s, t, w; 37 double dr, dt, dx; 38 float ft, fx; 39 uint64_t hx, lx; 40 uint16_t expsign; 41 int k; 42 43 GET_LDOUBLE_MSW64(hx,x); 44 k = (hx>>48)&0x7fff; 45 46 /* 47 * If x = +-Inf, then cbrt(x) = +-Inf. 48 * If x = NaN, then cbrt(x) = NaN. 49 */ 50 if (k == BIAS + LDBL_MAX_EXP) 51 return (x + x); 52 53 if (k == 0) { 54 /* If x = +-0, then cbrt(x) = +-0. */ 55 GET_LDOUBLE_WORDS64(hx,lx,x); 56 if (((hx&0x7fffffffffffffffLL)|lx) == 0) { 57 return (x); 58 } 59 /* Adjust subnormal numbers. */ 60 x *= 0x1.0p514; 61 GET_LDOUBLE_MSW64(hx,x); 62 k = (hx>>48)&0x7fff; 63 k -= BIAS + 514; 64 } else 65 k -= BIAS; 66 GET_LDOUBLE_MSW64(hx,x); 67 hx = (hx&0x8000ffffffffffffLL)|((uint64_t)BIAS<<48); 68 SET_LDOUBLE_MSW64(x,hx); 69 v = 1; 70 71 switch (k % 3) { 72 case 1: 73 case -2: 74 x = 2*x; 75 k--; 76 break; 77 case 2: 78 case -1: 79 x = 4*x; 80 k -= 2; 81 break; 82 } 83 GET_LDOUBLE_MSW64(hx,x); 84 expsign = ((hx>>48) & 0x8000) | (BIAS + k / 3); 85 hx = (hx&0x8000ffffffffffffLL)|((uint64_t)expsign<<48); 86 SET_LDOUBLE_MSW64(x,hx); 87 88 /* 89 * The following is the guts of s_cbrtf, with the handling of 90 * special values removed and extra care for accuracy not taken, 91 * but with most of the extra accuracy not discarded. 92 */ 93 94 /* ~5-bit estimate: */ 95 fx = x; 96 GET_FLOAT_WORD(hx, fx); 97 SET_FLOAT_WORD(ft, ((hx & 0x7fffffff) / 3 + B1)); 98 99 /* ~16-bit estimate: */ 100 dx = x; 101 dt = ft; 102 dr = dt * dt * dt; 103 dt = dt * (dx + dx + dr) / (dx + dr + dr); 104 105 /* ~47-bit estimate: */ 106 dr = dt * dt * dt; 107 dt = dt * (dx + dx + dr) / (dx + dr + dr); 108 109 /* 110 * Round dt away from zero to 47 bits. Since we don't trust the 47, 111 * add 2 47-bit ulps instead of 1 to round up. Rounding is slow and 112 * might be avoidable in this case, since on most machines dt will 113 * have been evaluated in 53-bit precision and the technical reasons 114 * for rounding up might not apply to either case in cbrtl() since 115 * dt is much more accurate than needed. 116 */ 117 t = dt + 0x2.0p-46 + 0x1.0p60L - 0x1.0p60; 118 119 /* 120 * Final step Newton iteration to 64 or 113 bits with 121 * error < 0.667 ulps 122 */ 123 s=t*t; /* t*t is exact */ 124 r=x/s; /* error <= 0.5 ulps; |r| < |t| */ 125 w=t+t; /* t+t is exact */ 126 r=(r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */ 127 t=t+t*r; /* error <= 0.5 + 0.5/3 + epsilon */ 128 129 t *= v; 130 return (t); 131 } 132