Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3362 | Serge | 1 | |
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 | |||
14 | |||
15 | FUNCTION |
||
16 | < |
||
17 | |||
18 | |||
19 | cbrt |
||
20 | INDEX |
||
21 | cbrtf |
||
22 | |||
23 | |||
24 | #include |
||
25 | double cbrt(double <[x]>); |
||
26 | float cbrtf(float <[x]>); |
||
27 | |||
28 | |||
29 | #include |
||
30 | double cbrt(<[x]>); |
||
31 | float cbrtf(<[x]>); |
||
32 | |||
33 | |||
34 | < |
||
35 | |||
36 | |||
37 | The cube root is returned. |
||
38 | |||
39 | |||
40 | < |
||
41 | */ |
||
42 | |||
43 | |||
44 | |||
45 | |||
46 | |||
47 | |||
48 | * Return cube root of x |
||
49 | */ |
||
50 | #ifdef __STDC__ |
||
51 | static const __uint32_t |
||
52 | #else |
||
53 | static __uint32_t |
||
54 | #endif |
||
55 | B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ |
||
56 | B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ |
||
57 | |||
58 | |||
59 | static const double |
||
60 | #else |
||
61 | static double |
||
62 | #endif |
||
63 | C = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */ |
||
64 | D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */ |
||
65 | E = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */ |
||
66 | F = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */ |
||
67 | G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */ |
||
68 | |||
69 | |||
70 | double cbrt(double x) |
||
71 | #else |
||
72 | double cbrt(x) |
||
73 | double x; |
||
74 | #endif |
||
75 | { |
||
76 | __int32_t hx; |
||
77 | double r,s,t=0.0,w; |
||
78 | __uint32_t sign; |
||
79 | __uint32_t high,low; |
||
80 | |||
81 | |||
82 | sign=hx&0x80000000; /* sign= sign(x) */ |
||
83 | hx ^=sign; |
||
84 | if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ |
||
85 | GET_LOW_WORD(low,x); |
||
86 | if((hx|low)==0) |
||
87 | return(x); /* cbrt(0) is itself */ |
||
88 | |||
89 | |||
90 | /* rough cbrt to 5 bits */ |
||
91 | if(hx<0x00100000) /* subnormal number */ |
||
92 | {SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */ |
||
93 | t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/3+B2); |
||
94 | } |
||
95 | else |
||
96 | SET_HIGH_WORD(t,hx/3+B1); |
||
97 | |||
98 | |||
99 | |||
100 | r=t*t/x; |
||
101 | s=C+r*t; |
||
102 | t*=G+F/(s+E+D/s); |
||
103 | |||
104 | |||
105 | GET_HIGH_WORD(high,t); |
||
106 | INSERT_WORDS(t,high+0x00000001,0); |
||
107 | |||
108 | |||
109 | |||
110 | s=t*t; /* t*t is exact */ |
||
111 | r=x/s; |
||
112 | w=t+t; |
||
113 | r=(r-t)/(w+r); /* r-s is exact */ |
||
114 | t=t+t*r; |
||
115 | |||
116 | |||
117 | GET_HIGH_WORD(high,t); |
||
118 | SET_HIGH_WORD(t,high|sign); |
||
119 | return(t); |
||
120 | } |
||
121 | |||
122 | |||
123 |