genericopenlibs/openenvcore/libm/src/s_cbrt.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*--------------------------------------------------------------------
       
     2  *© Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
     3  *--------------------------------------------------------------------
       
     4 */
       
     5 /* @(#)s_cbrt.c 5.1 93/09/24 */
       
     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 #ifndef __SYMBIAN32__
       
    17 #ifndef lint
       
    18 static char rcsid[] = "$FreeBSD: src/lib/msun/src/s_cbrt.c,v 1.7 2002/05/28 18:15:04 alfred Exp $";
       
    19 #endif
       
    20 #endif //__SYMBIAN32__
       
    21 
       
    22 #include <math.h>
       
    23 #include "math_private.h"
       
    24 
       
    25 /* cbrt(x)
       
    26  * Return cube root of x
       
    27  */
       
    28 static const u_int32_t
       
    29 	B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
       
    30 	B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
       
    31 
       
    32 static const double
       
    33 C =  5.42857142857142815906e-01, /* 19/35     = 0x3FE15F15, 0xF15F15F1 */
       
    34 D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */
       
    35 E =  1.41428571428571436819e+00, /* 99/70     = 0x3FF6A0EA, 0x0EA0EA0F */
       
    36 F =  1.60714285714285720630e+00, /* 45/28     = 0x3FF9B6DB, 0x6DB6DB6E */
       
    37 G =  3.57142857142857150787e-01; /* 5/14      = 0x3FD6DB6D, 0xB6DB6DB7 */
       
    38 
       
    39 EXPORT_C double
       
    40 cbrt(double x)
       
    41 {
       
    42 	int32_t	hx;
       
    43 	double r,s,t=0.0,w;
       
    44 	u_int32_t sign;
       
    45 	u_int32_t high,low;
       
    46 
       
    47 	GET_HIGH_WORD(hx,x);
       
    48 	sign=hx&0x80000000; 		/* sign= sign(x) */
       
    49 	hx  ^=sign;
       
    50 	if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
       
    51 	GET_LOW_WORD(low,x);
       
    52 	if((hx|low)==0)
       
    53 	    return(x);		/* cbrt(0) is itself */
       
    54 
       
    55 	SET_HIGH_WORD(x,hx);	/* x <- |x| */
       
    56     /* rough cbrt to 5 bits */
       
    57 	if(hx<0x00100000) 		/* subnormal number */
       
    58 	  {SET_HIGH_WORD(t,0x43500000);	/* set t= 2**54 */
       
    59 	   t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/3+B2);
       
    60 	  }
       
    61 	else
       
    62 	  SET_HIGH_WORD(t,hx/3+B1);
       
    63 
       
    64 
       
    65     /* new cbrt to 23 bits, may be implemented in single precision */
       
    66 	r=t*t/x;
       
    67 	s=C+r*t;
       
    68 	t*=G+F/(s+E+D/s);
       
    69 
       
    70     /* chopped to 20 bits and make it larger than cbrt(x) */
       
    71 	GET_HIGH_WORD(high,t);
       
    72 	INSERT_WORDS(t,high+0x00000001,0);
       
    73 
       
    74 
       
    75     /* one step newton iteration to 53 bits with error less than 0.667 ulps */
       
    76 	s=t*t;		/* t*t is exact */
       
    77 	r=x/s;
       
    78 	w=t+t;
       
    79 	r=(r-t)/(w+r);	/* r-s is exact */
       
    80 	t=t+t*r;
       
    81 
       
    82     /* retore the sign bit */
       
    83 	GET_HIGH_WORD(high,t);
       
    84 	SET_HIGH_WORD(t,high|sign);
       
    85 	return(t);
       
    86 }