Back to index

lightning-sunbird  0.9+nobinonly
s_cbrt.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Sun Microsystems, Inc.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /* @(#)s_cbrt.c 1.3 95/01/18 */
00041 /*
00042  * ====================================================
00043  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00044  *
00045  * Developed at SunSoft, a Sun Microsystems, Inc. business.
00046  * Permission to use, copy, modify, and distribute this
00047  * software is freely granted, provided that this notice 
00048  * is preserved.
00049  * ====================================================
00050  *
00051  */
00052 
00053 #include "fdlibm.h"
00054 
00055 /* cbrt(x)
00056  * Return cube root of x
00057  */
00058 #ifdef __STDC__
00059 static const unsigned 
00060 #else
00061 static unsigned 
00062 #endif
00063        B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
00064        B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
00065 
00066 #ifdef __STDC__
00067 static const double
00068 #else
00069 static double
00070 #endif
00071 C =  5.42857142857142815906e-01, /* 19/35     = 0x3FE15F15, 0xF15F15F1 */
00072 D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */
00073 E =  1.41428571428571436819e+00, /* 99/70     = 0x3FF6A0EA, 0x0EA0EA0F */
00074 F =  1.60714285714285720630e+00, /* 45/28     = 0x3FF9B6DB, 0x6DB6DB6E */
00075 G =  3.57142857142857150787e-01; /* 5/14      = 0x3FD6DB6D, 0xB6DB6DB7 */
00076 
00077 #ifdef __STDC__
00078        double fd_cbrt(double x) 
00079 #else
00080        double fd_cbrt(x) 
00081        double x;
00082 #endif
00083 {
00084         fd_twoints u;
00085        int    hx;
00086        double r,s,t=0.0,w;
00087        unsigned sign;
00088 
00089         u.d = x;
00090        hx = __HI(u);        /* high word of x */
00091        sign=hx&0x80000000;         /* sign= sign(x) */
00092        hx  ^=sign;
00093        if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
00094        if((hx|__LO(u))==0) {
00095             x = u.d;
00096            return(x);              /* cbrt(0) is itself */
00097         }
00098         u.d = x;
00099        __HI(u) = hx; /* x <- |x| */
00100         x = u.d;
00101     /* rough cbrt to 5 bits */
00102        if(hx<0x00100000)           /* subnormal number */
00103          {u.d = t; __HI(u)=0x43500000; t=u.d;           /* set t= 2**54 */
00104           t*=x; __HI(u)=__HI(u)/3+B2;
00105          }
00106        else {
00107          u.d = t; __HI(u)=hx/3+B1; t = u.d;
00108         }
00109 
00110 
00111     /* new cbrt to 23 bits, may be implemented in single precision */
00112        r=t*t/x;
00113        s=C+r*t;
00114        t*=G+F/(s+E+D/s);    
00115 
00116     /* chopped to 20 bits and make it larger than cbrt(x) */ 
00117         u.d = t;
00118        __LO(u)=0; __HI(u)+=0x00000001;
00119         t = u.d;
00120 
00121     /* one step newton iteration to 53 bits with error less than 0.667 ulps */
00122        s=t*t;        /* t*t is exact */
00123        r=x/s;
00124        w=t+t;
00125        r=(r-t)/(w+r);       /* r-s is exact */
00126        t=t+t*r;
00127 
00128     /* retore the sign bit */
00129         u.d = t;
00130        __HI(u) |= sign;
00131         t = u.d;
00132        return(t);
00133 }