POK
/home/jaouen/pok_official/pok/trunk/libpok/libm/cbrt.c
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Fri Jan 30 14:41:34 2009 
00015  */
00016 
00017 /* @(#)s_cbrt.c 5.1 93/09/24 */
00018 /*
00019  * ====================================================
00020  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00021  *
00022  * Developed at SunPro, a Sun Microsystems, Inc. business.
00023  * Permission to use, copy, modify, and distribute this
00024  * software is freely granted, provided that this notice
00025  * is preserved.
00026  * ====================================================
00027  */
00028 
00029 #ifdef POK_NEEDS_LIBMATH
00030 
00031 #include <types.h>
00032 #include <libm.h>
00033 #include "math_private.h"
00034 
00035 /* cbrt(x)
00036  * Return cube root of x
00037  */
00038 static const uint32_t
00039         B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
00040         B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
00041 
00042 static const double
00043 C =  5.42857142857142815906e-01, /* 19/35     = 0x3FE15F15, 0xF15F15F1 */
00044 D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */
00045 E =  1.41428571428571436819e+00, /* 99/70     = 0x3FF6A0EA, 0x0EA0EA0F */
00046 F =  1.60714285714285720630e+00, /* 45/28     = 0x3FF9B6DB, 0x6DB6DB6E */
00047 G =  3.57142857142857150787e-01; /* 5/14      = 0x3FD6DB6D, 0xB6DB6DB7 */
00048 
00049 double
00050 cbrt(double x)
00051 {
00052         int32_t hx;
00053         double r,s,t=0.0,w;
00054         uint32_t sign;
00055         uint32_t high,low;
00056 
00057         GET_HIGH_WORD(hx,x);
00058         sign=hx&0x80000000;             /* sign= sign(x) */
00059         hx  ^=sign;
00060         if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
00061         GET_LOW_WORD(low,x);
00062         if((hx|low)==0)
00063             return(x);          /* cbrt(0) is itself */
00064 
00065         SET_HIGH_WORD(x,hx);    /* x <- |x| */
00066     /* rough cbrt to 5 bits */
00067         if(hx<0x00100000)               /* subnormal number */
00068           {SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */
00069            t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/3+B2);
00070           }
00071         else
00072           SET_HIGH_WORD(t,hx/3+B1);
00073 
00074 
00075     /* new cbrt to 23 bits, may be implemented in single precision */
00076         r=t*t/x;
00077         s=C+r*t;
00078         t*=G+F/(s+E+D/s);
00079 
00080     /* chopped to 20 bits and make it larger than cbrt(x) */
00081         GET_HIGH_WORD(high,t);
00082         INSERT_WORDS(t,high+0x00000001,0);
00083 
00084 
00085     /* one step newton iteration to 53 bits with error less than 0.667 ulps */
00086         s=t*t;          /* t*t is exact */
00087         r=x/s;
00088         w=t+t;
00089         r=(r-t)/(w+r);  /* r-s is exact */
00090         t=t+t*r;
00091 
00092     /* retore the sign bit */
00093         GET_HIGH_WORD(high,t);
00094         SET_HIGH_WORD(t,high|sign);
00095         return(t);
00096 }
00097 #endif