1 |
|
|
/* @(#)s_cbrt.c 5.1 93/09/24 */ |
2 |
|
|
/* |
3 |
|
|
* ==================================================== |
4 |
|
|
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
5 |
|
|
* |
6 |
|
|
* Developed at SunPro, a Sun Microsystems, Inc. business. |
7 |
|
|
* Permission to use, copy, modify, and distribute this |
8 |
|
|
* software is freely granted, provided that this notice |
9 |
|
|
* is preserved. |
10 |
|
|
* ==================================================== |
11 |
|
|
*/ |
12 |
|
|
|
13 |
|
|
#include <float.h> |
14 |
|
|
#include <math.h> |
15 |
|
|
|
16 |
|
|
#include "math_private.h" |
17 |
|
|
|
18 |
|
|
/* cbrt(x) |
19 |
|
|
* Return cube root of x |
20 |
|
|
*/ |
21 |
|
|
static const u_int32_t |
22 |
|
|
B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ |
23 |
|
|
B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ |
24 |
|
|
|
25 |
|
|
static const double |
26 |
|
|
C = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */ |
27 |
|
|
D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */ |
28 |
|
|
E = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */ |
29 |
|
|
F = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */ |
30 |
|
|
G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */ |
31 |
|
|
|
32 |
|
|
double |
33 |
|
|
cbrt(double x) |
34 |
|
|
{ |
35 |
|
|
int32_t hx; |
36 |
|
|
double r,s,t=0.0,w; |
37 |
|
|
u_int32_t sign; |
38 |
|
|
u_int32_t high,low; |
39 |
|
|
|
40 |
|
|
GET_HIGH_WORD(hx,x); |
41 |
|
|
sign=hx&0x80000000; /* sign= sign(x) */ |
42 |
|
|
hx ^=sign; |
43 |
|
|
if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ |
44 |
|
|
GET_LOW_WORD(low,x); |
45 |
|
|
if((hx|low)==0) |
46 |
|
|
return(x); /* cbrt(0) is itself */ |
47 |
|
|
|
48 |
|
|
SET_HIGH_WORD(x,hx); /* x <- |x| */ |
49 |
|
|
/* rough cbrt to 5 bits */ |
50 |
|
|
if(hx<0x00100000) /* subnormal number */ |
51 |
|
|
{SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */ |
52 |
|
|
t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/3+B2); |
53 |
|
|
} |
54 |
|
|
else |
55 |
|
|
SET_HIGH_WORD(t,hx/3+B1); |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
/* new cbrt to 23 bits, may be implemented in single precision */ |
59 |
|
|
r=t*t/x; |
60 |
|
|
s=C+r*t; |
61 |
|
|
t*=G+F/(s+E+D/s); |
62 |
|
|
|
63 |
|
|
/* chopped to 20 bits and make it larger than cbrt(x) */ |
64 |
|
|
GET_HIGH_WORD(high,t); |
65 |
|
|
INSERT_WORDS(t,high+0x00000001,0); |
66 |
|
|
|
67 |
|
|
|
68 |
|
|
/* one step newton iteration to 53 bits with error less than 0.667 ulps */ |
69 |
|
|
s=t*t; /* t*t is exact */ |
70 |
|
|
r=x/s; |
71 |
|
|
w=t+t; |
72 |
|
|
r=(r-t)/(w+r); /* r-s is exact */ |
73 |
|
|
t=t+t*r; |
74 |
|
|
|
75 |
|
|
/* retore the sign bit */ |
76 |
|
|
GET_HIGH_WORD(high,t); |
77 |
|
|
SET_HIGH_WORD(t,high|sign); |
78 |
|
|
return(t); |
79 |
|
|
} |
80 |
|
|
DEF_STD(cbrt); |
81 |
|
|
LDBL_MAYBE_UNUSED_CLONE(cbrt); |