1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
18 *
19 * The Initial Developer of the Original Code is
20 * Sun Microsystems, Inc.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
40 /* @(#)s_cbrt.c 1.3 95/01/18 */
41 /*
42 * ====================================================
43 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
44 *
45 * Developed at SunSoft, a Sun Microsystems, Inc. business.
46 * Permission to use, copy, modify, and distribute this
47 * software is freely granted, provided that this notice
48 * is preserved.
49 * ====================================================
50 *
51 */
53 #include "fdlibm.h"
55 /* cbrt(x)
56 * Return cube root of x
57 */
58 #ifdef __STDC__
59 static const unsigned
60 #else
61 static unsigned
62 #endif
63 B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
64 B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
66 #ifdef __STDC__
67 static const double
68 #else
69 static double
70 #endif
71 C = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */
72 D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */
73 E = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */
74 F = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */
75 G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */
77 #ifdef __STDC__
78 double fd_cbrt(double x)
79 #else
80 double fd_cbrt(x)
81 double x;
82 #endif
83 {
84 fd_twoints u;
85 int hx;
86 double r,s,t=0.0,w;
87 unsigned sign;
89 u.d = x;
90 hx = __HI(u); /* high word of x */
91 sign=hx&0x80000000; /* sign= sign(x) */
92 hx ^=sign;
93 if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
94 if((hx|__LO(u))==0) {
95 x = u.d;
96 return(x); /* cbrt(0) is itself */
97 }
98 u.d = x;
99 __HI(u) = hx; /* x <- |x| */
100 x = u.d;
101 /* rough cbrt to 5 bits */
102 if(hx<0x00100000) /* subnormal number */
103 {u.d = t; __HI(u)=0x43500000; t=u.d; /* set t= 2**54 */
104 t*=x; __HI(u)=__HI(u)/3+B2;
105 }
106 else {
107 u.d = t; __HI(u)=hx/3+B1; t = u.d;
108 }
111 /* new cbrt to 23 bits, may be implemented in single precision */
112 r=t*t/x;
113 s=C+r*t;
114 t*=G+F/(s+E+D/s);
116 /* chopped to 20 bits and make it larger than cbrt(x) */
117 u.d = t;
118 __LO(u)=0; __HI(u)+=0x00000001;
119 t = u.d;
121 /* one step newton iteration to 53 bits with error less than 0.667 ulps */
122 s=t*t; /* t*t is exact */
123 r=x/s;
124 w=t+t;
125 r=(r-t)/(w+r); /* r-s is exact */
126 t=t+t*r;
128 /* retore the sign bit */
129 u.d = t;
130 __HI(u) |= sign;
131 t = u.d;
132 return(t);
133 }