s_cbrt.c revision 153306
1210284Sjmallett/* @(#)s_cbrt.c 5.1 93/09/24 */ 2232812Sjmallett/* 3215990Sjmallett * ==================================================== 4210284Sjmallett * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5210284Sjmallett * 6215990Sjmallett * Developed at SunPro, a Sun Microsystems, Inc. business. 7215990Sjmallett * Permission to use, copy, modify, and distribute this 8215990Sjmallett * software is freely granted, provided that this notice 9210284Sjmallett * is preserved. 10215990Sjmallett * ==================================================== 11215990Sjmallett */ 12210284Sjmallett 13215990Sjmallett#ifndef lint 14215990Sjmallettstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/s_cbrt.c 153306 2005-12-11 19:51:30Z bde $"; 15215990Sjmallett#endif 16215990Sjmallett 17215990Sjmallett#include "math.h" 18232812Sjmallett#include "math_private.h" 19215990Sjmallett 20215990Sjmallett/* cbrt(x) 21215990Sjmallett * Return cube root of x 22215990Sjmallett */ 23215990Sjmallettstatic const u_int32_t 24215990Sjmallett B1 = 715094163, /* B1 = (1023-1023/3-0.03306235651)*2**20 */ 25215990Sjmallett B2 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */ 26215990Sjmallett 27215990Sjmallettstatic const double 28215990SjmallettC = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */ 29232812SjmallettD = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */ 30215990SjmallettE = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */ 31215990SjmallettF = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */ 32215990SjmallettG = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */ 33215990Sjmallett 34215990Sjmallettdouble 35215990Sjmallettcbrt(double x) 36215990Sjmallett{ 37215990Sjmallett int32_t hx; 38210284Sjmallett double r,s,t=0.0,w; 39210284Sjmallett u_int32_t sign; 40215990Sjmallett u_int32_t high,low; 41210284Sjmallett 42210284Sjmallett GET_HIGH_WORD(hx,x); 43210284Sjmallett sign=hx&0x80000000; /* sign= sign(x) */ 44210284Sjmallett hx ^=sign; 45210284Sjmallett if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ 46210284Sjmallett GET_LOW_WORD(low,x); 47210284Sjmallett if((hx|low)==0) 48210284Sjmallett return(x); /* cbrt(0) is itself */ 49232812Sjmallett 50210284Sjmallett SET_HIGH_WORD(x,hx); /* x <- |x| */ 51210284Sjmallett /* 52210284Sjmallett * Rough cbrt to 5 bits: 53210284Sjmallett * cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3) 54215990Sjmallett * where e is integral and >= 0, m is real and in [0, 1), and "/" and 55215990Sjmallett * "%" are integer division and modulus with rounding towards minus 56215990Sjmallett * infinity. The RHS is always >= the LHS and has a maximum relative 57232812Sjmallett * error of about 1 in 16. Adding a bias of -0.03306235651 to the 58215990Sjmallett * (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE 59215990Sjmallett * floating point representation, for finite positive normal values, 60215990Sjmallett * ordinary integer divison of the value in bits magically gives 61215990Sjmallett * almost exactly the RHS of the above provided we first subtract the 62215990Sjmallett * exponent bias (1023 for doubles) and later add it back. We do the 63232812Sjmallett * subtraction virtually to keep e >= 0 so that ordinary integer 64232812Sjmallett * division rounds towards minus infinity; this is also efficient. 65215990Sjmallett */ 66215990Sjmallett if(hx<0x00100000) /* subnormal number */ 67215990Sjmallett {SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */ 68232812Sjmallett t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/3+B2); 69232812Sjmallett } 70215990Sjmallett else 71215990Sjmallett SET_HIGH_WORD(t,hx/3+B1); 72215990Sjmallett 73215990Sjmallett /* new cbrt to 23 bits; may be implemented in single precision */ 74215990Sjmallett r=t*t/x; 75215990Sjmallett s=C+r*t; 76215990Sjmallett t*=G+F/(s+E+D/s); 77215990Sjmallett 78215990Sjmallett /* chop t to 20 bits and make it larger than cbrt(x) */ 79215990Sjmallett GET_HIGH_WORD(high,t); 80215990Sjmallett INSERT_WORDS(t,high+0x00000001,0); 81215990Sjmallett 82215990Sjmallett /* one step Newton iteration to 53 bits with error less than 0.667 ulps */ 83215990Sjmallett s=t*t; /* t*t is exact */ 84215990Sjmallett r=x/s; 85215990Sjmallett w=t+t; 86215990Sjmallett r=(r-t)/(w+r); /* r-t is exact */ 87215990Sjmallett t=t+t*r; 88215990Sjmallett 89215990Sjmallett /* restore the sign bit */ 90215990Sjmallett GET_HIGH_WORD(high,t); 91215990Sjmallett SET_HIGH_WORD(t,high|sign); 92215990Sjmallett return(t); 93215990Sjmallett} 94215990Sjmallett