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 "cdefs-compat.h" 18 //__FBSDID("$FreeBSD: src/lib/msun/src/s_cbrtl.c,v 1.1 2011/03/12 19:37:35 kargl Exp $"); 19 20 #include <float.h> 21 #include <openlibm_math.h> 22 // VBS 23 //#include <ieeefp.h> 24 25 #include "fpmath.h" 26 #include "math_private.h" 27 #if defined(__i386__) 28 #include "i387/bsd_ieeefp.h" 29 #endif 30 31 #define BIAS (LDBL_MAX_EXP - 1) 32 33 static const unsigned 34 B1 = 709958130; /* B1 = (127-127.0/3-0.03306235651)*2**23 */ 35 36 DLLEXPORT long double 37 cbrtl(long double x) 38 { 39 union IEEEl2bits u, v; 40 long double r, s, t, w; 41 double dr, dt, dx; 42 float ft, fx; 43 u_int32_t hx; 44 u_int16_t expsign; 45 int k; 46 47 u.e = x; 48 expsign = u.xbits.expsign; 49 k = expsign & 0x7fff; 50 51 /* 52 * If x = +-Inf, then cbrt(x) = +-Inf. 53 * If x = NaN, then cbrt(x) = NaN. 54 */ 55 if (k == BIAS + LDBL_MAX_EXP) 56 return (x + x); 57 58 #ifdef __i386__ 59 fp_prec_t oprec; 60 61 oprec = fpgetprec(); 62 if (oprec != FP_PE) 63 fpsetprec(FP_PE); 64 #endif 65 66 if (k == 0) { 67 /* If x = +-0, then cbrt(x) = +-0. */ 68 if ((u.bits.manh | u.bits.manl) == 0) { 69 #ifdef __i386__ 70 if (oprec != FP_PE) 71 fpsetprec(oprec); 72 #endif 73 return (x); 74 } 75 /* Adjust subnormal numbers. */ 76 u.e *= 0x1.0p514; 77 k = u.bits.exp; 78 k -= BIAS + 514; 79 } else 80 k -= BIAS; 81 u.xbits.expsign = BIAS; 82 v.e = 1; 83 84 x = u.e; 85 switch (k % 3) { 86 case 1: 87 case -2: 88 x = 2*x; 89 k--; 90 break; 91 case 2: 92 case -1: 93 x = 4*x; 94 k -= 2; 95 break; 96 } 97 v.xbits.expsign = (expsign & 0x8000) | (BIAS + k / 3); 98 99 /* 100 * The following is the guts of s_cbrtf, with the handling of 101 * special values removed and extra care for accuracy not taken, 102 * but with most of the extra accuracy not discarded. 103 */ 104 105 /* ~5-bit estimate: */ 106 fx = x; 107 GET_FLOAT_WORD(hx, fx); 108 SET_FLOAT_WORD(ft, ((hx & 0x7fffffff) / 3 + B1)); 109 110 /* ~16-bit estimate: */ 111 dx = x; 112 dt = ft; 113 dr = dt * dt * dt; 114 dt = dt * (dx + dx + dr) / (dx + dr + dr); 115 116 /* ~47-bit estimate: */ 117 dr = dt * dt * dt; 118 dt = dt * (dx + dx + dr) / (dx + dr + dr); 119 120 #if LDBL_MANT_DIG == 64 121 /* 122 * dt is cbrtl(x) to ~47 bits (after x has been reduced to 1 <= x < 8). 123 * Round it away from zero to 32 bits (32 so that t*t is exact, and 124 * away from zero for technical reasons). 125 */ 126 volatile double vd2 = 0x1.0p32; 127 volatile double vd1 = 0x1.0p-31; 128 #define vd ((long double)vd2 + vd1) 129 130 t = dt + vd - 0x1.0p32; 131 #elif LDBL_MANT_DIG == 113 132 /* 133 * Round dt away from zero to 47 bits. Since we don't trust the 47, 134 * add 2 47-bit ulps instead of 1 to round up. Rounding is slow and 135 * might be avoidable in this case, since on most machines dt will 136 * have been evaluated in 53-bit precision and the technical reasons 137 * for rounding up might not apply to either case in cbrtl() since 138 * dt is much more accurate than needed. 139 */ 140 t = dt + 0x2.0p-46 + 0x1.0p60L - 0x1.0p60; 141 #else 142 #error "Unsupported long double format" 143 #endif 144 145 /* 146 * Final step Newton iteration to 64 or 113 bits with 147 * error < 0.667 ulps 148 */ 149 s=t*t; /* t*t is exact */ 150 r=x/s; /* error <= 0.5 ulps; |r| < |t| */ 151 w=t+t; /* t+t is exact */ 152 r=(r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */ 153 t=t+t*r; /* error <= 0.5 + 0.5/3 + epsilon */ 154 155 t *= v.e; 156 #ifdef __i386__ 157 if (oprec != FP_PE) 158 fpsetprec(oprec); 159 #endif 160 return (t); 161 } 162