4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #pragma weak __cpowl = cpowl
32 #include "libm.h" /* __k_clog_rl/__k_atan2l */
33 /* atan2l/atan2pil/exp2l/expl/fabsl/hypotl/isinfl/logl/powl/sincosl/sincospil */
34 #include "complex_wrapper.h"
35 #include "longdouble.h"
38 #define HALF(x) ((int *) &x)[3] = 0; ((int *) &x)[2] &= 0xfe000000
39 #define LAST(x) ((int *) &x)[3]
41 #define HALF(x) ((int *) &x)[0] = 0
42 #define LAST(x) ((int *) &x)[0]
46 static const int hiinf
= 0x7fff0000;
47 static const long double
51 /* 43 significant bits, 21 trailing zeros */
52 ln2hil
= 0.693147180559890330187045037746429443359375L,
53 ln2lol
= 5.497923018708371174712471612513436025525412068e-14L,
55 /* 0x3FF962E4 2FEFA39E F35793C7 00000000 */
56 ln2hil
= 0.693147180559945309417231592858066493070671489074L,
57 ln2lol
= 5.28600110075004828645286235820646730106802446566153e-25L,
59 invln2
= 1.442695040888963407359924681001892137427e+0000L,
65 * Assuming |t[0]| > |t[1]| and |t[2]| > |t[3]|, sum4fpl subroutine
66 * compute t[0] + t[1] + t[2] + t[3] into two long double fp numbers.
68 static long double sum4fpl(long double ta
[], long double *w
)
70 long double t1
, t2
, t3
, t4
, w1
, w2
, t
;
71 t1
= ta
[0]; t2
= ta
[1]; t3
= ta
[2]; t4
= ta
[3];
73 * Rearrange ti so that |t1| >= |t2| >= |t3| >= |t4|
75 if (fabsl(t4
) > fabsl(t1
)) {
76 t
= t1
; t1
= t3
; t3
= t
;
77 t
= t2
; t2
= t4
; t4
= t
;
78 } else if (fabsl(t3
) > fabsl(t1
)) {
80 if (fabsl(t4
) > fabsl(t2
)) {
81 t3
= t4
; t4
= t2
; t2
= t
;
85 } else if (fabsl(t3
) > fabsl(t2
)) {
87 if (fabsl(t4
) > fabsl(t2
)) {
92 /* summing r = t1 + t2 + t3 + t4 to w1 + w2 */
107 cpowl(ldcomplex z
, ldcomplex w
) {
109 long double x
, y
, u
, v
, t
, c
, s
, r
;
110 long double t1
, t2
, t3
, t4
, x1
, x2
, y1
, y2
, u1
, v1
, b
[4], w1
, w2
;
111 int ix
, iy
, hx
, hy
, hv
, hu
, iu
, iv
, i
, j
, k
;
121 ix
= hx
& 0x7fffffff;
122 iy
= hy
& 0x7fffffff;
123 iu
= hu
& 0x7fffffff;
124 iv
= hv
& 0x7fffffff;
127 if (v
== zero
) { /* z**(real) */
128 if (u
== one
) { /* (anything) ** 1 is itself */
131 } else if (u
== zero
) { /* (anything) ** 0 is 1 */
134 } else if (y
== zero
) { /* real ** real */
136 if (hx
< 0 && ix
< hiinf
&& iu
< hiinf
) {
137 /* -x ** u is exp(i*pi*u)*pow(x,u) */
139 sincospil(u
, &s
, &c
);
140 LD_RE(ans
) = (c
== zero
)? c
: c
* r
;
141 LD_IM(ans
) = (s
== zero
)? s
: s
* r
;
143 LD_RE(ans
) = powl(x
, u
);
144 } else if (x
== zero
|| ix
>= hiinf
|| iy
>= hiinf
) {
145 if (isnanl(x
) || isnanl(y
) || isnanl(u
))
146 LD_RE(ans
) = LD_IM(ans
) = x
+ y
+ u
;
151 r
= fabsl(x
) + fabsl(y
);
153 sincospil(t
* u
, &s
, &c
);
154 LD_RE(ans
) = (c
== zero
)? c
: c
* r
;
155 LD_IM(ans
) = (s
== zero
)? s
: s
* r
;
157 } else if (fabsl(x
) == fabsl(y
)) { /* |x| = |y| */
159 t
= (hy
>= 0)? 0.25L : -0.25L;
160 sincospil(t
* u
, &s
, &c
);
161 } else if ((LAST(u
) & 3) == 0) {
162 t
= (hy
>= 0)? 0.75L : -0.75L;
163 sincospil(t
* u
, &s
, &c
);
165 r
= (hy
>= 0)? u
: -u
;
169 sincospil(w1
, &t1
, &t2
);
170 sincospil(w2
, &t3
, &t4
);
171 s
= t1
* t4
+ t3
* t2
;
172 c
= t2
* t4
- t1
* t3
;
174 if (ix
< 0x3ffe0000) /* |x| < 1/2 */
175 r
= powl(fabsl(x
+ x
), u
) * exp2l(-0.5L * u
);
176 else if (ix
>= 0x3fff0000 || iu
< 0x400cfff8)
177 /* |x| >= 1 or |u| < 16383 */
178 r
= powl(fabsl(x
), u
) * exp2l(0.5L * u
);
179 else /* special treatment */
182 LD_RE(ans
) = (c
== zero
)? c
: c
* r
;
183 LD_IM(ans
) = (s
== zero
)? s
: s
* r
;
190 if (iu
>= hiinf
|| iv
>= hiinf
|| ix
>= hiinf
|| iy
>= hiinf
) {
192 * non-zero imag part(s) with inf component(s) yields NaN
194 t
= fabsl(x
) + fabsl(y
) + fabsl(u
) + fabsl(v
);
195 LD_RE(ans
) = LD_IM(ans
) = t
- t
;
197 k
= 0; /* no scaling */
198 if (iu
> 0x7ffe0000 || iv
> 0x7ffe0000) {
199 u
*= 1.52587890625000000000e-05L;
200 v
*= 1.52587890625000000000e-05L;
201 k
= 1; /* scale u and v by 2**-16 */
204 * Use similated higher precision arithmetic to compute:
205 * r = u * log(hypot(x, y)) - v * atan2(y, x)
206 * q = u * atan2(y, x) + v * log(hypot(x, y))
209 t1
= __k_clog_rl(x
, y
, &t2
);
210 t3
= __k_atan2l(y
, x
, &t4
);
215 x2
= t2
- (x1
- t1
); /* log(hypot(x,y)) = x1 + x2 */
216 y2
= t4
- (y1
- t3
); /* atan2(y,x) = y1 + y2 */
217 /* compute q = u * atan2(y, x) + v * log(hypot(x, y)) */
220 b
[1] = (u
- u1
) * y1
+ u
* y2
;
221 if (j
== 1) { /* v = 0 */
223 w2
= b
[1] - (w1
- b
[0]);
226 b
[3] = (v
- v1
) * x1
+ v
* x2
;
227 w1
= sum4fpl(b
, &w2
);
229 sincosl(w1
, &t1
, &t2
);
230 sincosl(w2
, &t3
, &t4
);
231 s
= t1
* t4
+ t3
* t2
;
232 c
= t2
* t4
- t1
* t3
;
233 if (k
== 1) /* square j times */
234 for (i
= 0; i
< 10; i
++) {
236 c
= (c
+ s
) * (c
- s
);
240 /* compute r = u * (t1, t2) - v * (t3, t4) */
242 b
[1] = (u
- u1
) * x1
+ u
* x2
;
243 if (j
== 1) { /* v = 0 */
245 w2
= b
[1] - (w1
- b
[0]);
248 b
[3] = (v1
- v
) * y1
- v
* y2
;
249 w1
= sum4fpl(b
, &w2
);
251 /* scale back unless w1 is large enough to cause exception */
252 if (k
!= 0 && fabsl(w1
) < 20000.0L) {
253 w1
*= 65536.0L; w2
*= 65536.0L;
256 ix
= hx
& 0x7fffffff;
257 /* compute exp(w1 + w2) */
259 if (ix
< 0x3f8c0000) /* exp(tiny < 2**-115) = 1 */
261 else if (ix
>= 0x400c6760) /* overflow/underflow */
262 r
= (hx
< 0)? tiny
* tiny
: huge
* huge
;
263 else { /* compute exp(w1 + w2) */
264 k
= (int) (invln2
* w1
+ ((hx
>= 0)? 0.5L : -0.5L));
265 t1
= (long double) k
;
266 t2
= w1
- t1
* ln2hil
;
267 t3
= w2
- t1
* ln2lol
;
270 if (c
!= zero
) c
*= r
;
271 if (s
!= zero
) s
*= r
;