1 
   2 /*
   3  * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.  Oracle designates this
   9  * particular file as subject to the "Classpath" exception as provided
  10  * by Oracle in the LICENSE file that accompanied this code.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23  * or visit www.oracle.com if you need additional information or have any
  24  * questions.
  25  */
  26 
  27 #include "fdlibm.h"
  28 
  29 /* cbrt(x)
  30  * Return cube root of x
  31  */
  32 #ifdef __STDC__
  33 static const unsigned
  34 #else
  35 static unsigned
  36 #endif
  37         B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
  38         B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
  39 
  40 #ifdef __STDC__
  41 static const double
  42 #else
  43 static double
  44 #endif
  45 C =  5.42857142857142815906e-01, /* 19/35     = 0x3FE15F15, 0xF15F15F1 */
  46 D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */
  47 E =  1.41428571428571436819e+00, /* 99/70     = 0x3FF6A0EA, 0x0EA0EA0F */
  48 F =  1.60714285714285720630e+00, /* 45/28     = 0x3FF9B6DB, 0x6DB6DB6E */
  49 G =  3.57142857142857150787e-01; /* 5/14      = 0x3FD6DB6D, 0xB6DB6DB7 */
  50 
  51 #ifdef __STDC__
  52         double cbrt(double x)
  53 #else
  54         double cbrt(x)
  55         double x;
  56 #endif
  57 {
  58         int     hx;
  59         double r,s,t=0.0,w;
  60         unsigned sign;
  61 
  62 
  63         hx = __HI(x);           /* high word of x */
  64         sign=hx&0x80000000;             /* sign= sign(x) */
  65         hx  ^=sign;
  66         if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
  67         if((hx|__LO(x))==0)
  68             return(x);          /* cbrt(0) is itself */
  69 
  70         __HI(x) = hx;   /* x <- |x| */
  71     /* rough cbrt to 5 bits */
  72         if(hx<0x00100000)               /* subnormal number */
  73           {__HI(t)=0x43500000;          /* set t= 2**54 */
  74            t*=x; __HI(t)=__HI(t)/3+B2;
  75           }
  76         else
  77           __HI(t)=hx/3+B1;
  78 
  79 
  80     /* new cbrt to 23 bits, may be implemented in single precision */
  81         r=t*t/x;
  82         s=C+r*t;
  83         t*=G+F/(s+E+D/s);
  84 
  85     /* chopped to 20 bits and make it larger than cbrt(x) */
  86         __LO(t)=0; __HI(t)+=0x00000001;
  87 
  88 
  89     /* one step newton iteration to 53 bits with error less than 0.667 ulps */
  90         s=t*t;          /* t*t is exact */
  91         r=x/s;
  92         w=t+t;
  93         r=(r-t)/(w+r);  /* r-s is exact */
  94         t=t+t*r;
  95 
  96     /* retore the sign bit */
  97         __HI(t) |= sign;
  98         return(t);
  99 }