5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 */
25 /*
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
28 */
29
30 #pragma weak __cpowl = cpowl
31
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"
36
37 #if defined(__sparc)
38 #define HALF(x) ((int *) &x)[3] = 0; ((int *) &x)[2] &= 0xfe000000
39 #define LAST(x) ((int *) &x)[3]
40 #elif defined(__x86)
41 #define HALF(x) ((int *) &x)[0] = 0
42 #define LAST(x) ((int *) &x)[0]
43 #endif
44
45 /* INDENT OFF */
46 static const int hiinf = 0x7fff0000;
47 static const long double
48 tiny = 1.0e-4000L,
49 huge = 1.0e4000L,
50 #if defined(__x86)
51 /* 43 significant bits, 21 trailing zeros */
52 ln2hil = 0.693147180559890330187045037746429443359375L,
53 ln2lol = 5.497923018708371174712471612513436025525412068e-14L,
54 #else /* sparc */
55 /* 0x3FF962E4 2FEFA39E F35793C7 00000000 */
56 ln2hil = 0.693147180559945309417231592858066493070671489074L,
57 ln2lol = 5.28600110075004828645286235820646730106802446566153e-25L,
58 #endif
59 invln2 = 1.442695040888963407359924681001892137427e+0000L,
60 one = 1.0L,
61 zero = 0.0L;
62 /* INDENT ON */
63
64 /*
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.
67 */
68 static long double sum4fpl(long double ta[], long double *w)
69 {
70 long double t1, t2, t3, t4, w1, w2, t;
71 t1 = ta[0]; t2 = ta[1]; t3 = ta[2]; t4 = ta[3];
72 /*
73 * Rearrange ti so that |t1| >= |t2| >= |t3| >= |t4|
74 */
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)) {
79 t = t1; t1 = t3;
80 if (fabsl(t4) > fabsl(t2)) {
81 t3 = t4; t4 = t2; t2 = t;
82 } else {
83 t3 = t2; t2 = t;
84 }
85 } else if (fabsl(t3) > fabsl(t2)) {
86 t = t2; t2 = t3;
87 if (fabsl(t4) > fabsl(t2)) {
88 t3 = t4; t4 = t;
89 } else
90 t3 = t;
91 }
92 /* summing r = t1 + t2 + t3 + t4 to w1 + w2 */
93 w1 = t3 + t4;
94 w2 = t4 - (w1 - t3);
95 t = t2 + w1;
96 w2 += w1 - (t - t2);
97 w1 = t + w2;
98 w2 += t - w1;
99 t = t1 + w1;
100 w2 += w1 - (t - t1);
101 w1 = t + w2;
102 *w = w2 - (w1 - t);
103 return (w1);
104 }
105
106 ldcomplex
107 cpowl(ldcomplex z, ldcomplex w) {
108 ldcomplex ans;
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;
112
113 x = LD_RE(z);
114 y = LD_IM(z);
115 u = LD_RE(w);
116 v = LD_IM(w);
117 hx = HI_XWORD(x);
118 hy = HI_XWORD(y);
119 hu = HI_XWORD(u);
120 hv = HI_XWORD(v);
121 ix = hx & 0x7fffffff;
122 iy = hy & 0x7fffffff;
123 iu = hu & 0x7fffffff;
124 iv = hv & 0x7fffffff;
125
126 j = 0;
127 if (v == zero) { /* z**(real) */
128 if (u == one) { /* (anything) ** 1 is itself */
129 LD_RE(ans) = x;
130 LD_IM(ans) = y;
131 } else if (u == zero) { /* (anything) ** 0 is 1 */
132 LD_RE(ans) = one;
133 LD_IM(ans) = zero;
134 } else if (y == zero) { /* real ** real */
135 LD_IM(ans) = zero;
136 if (hx < 0 && ix < hiinf && iu < hiinf) {
137 /* -x ** u is exp(i*pi*u)*pow(x,u) */
138 r = powl(-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;
142 } else
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;
147 else {
148 if (x == zero)
149 r = fabsl(y);
150 else
151 r = fabsl(x) + fabsl(y);
152 t = atan2pil(y, x);
153 sincospil(t * u, &s, &c);
154 LD_RE(ans) = (c == zero)? c: c * r;
155 LD_IM(ans) = (s == zero)? s: s * r;
156 }
157 } else if (fabsl(x) == fabsl(y)) { /* |x| = |y| */
158 if (hx >= 0) {
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);
164 } else {
165 r = (hy >= 0)? u : -u;
166 t = -0.25L * r;
167 w1 = r + t;
168 w2 = t - (w1 - r);
169 sincospil(w1, &t1, &t2);
170 sincospil(w2, &t3, &t4);
171 s = t1 * t4 + t3 * t2;
172 c = t2 * t4 - t1 * t3;
173 }
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 */
180 j = 2;
181 if (j == 0) {
182 LD_RE(ans) = (c == zero)? c: c * r;
183 LD_IM(ans) = (s == zero)? s: s * r;
184 }
185 } else
186 j = 1;
187 if (j == 0)
188 return (ans);
189 }
190 if (iu >= hiinf || iv >= hiinf || ix >= hiinf || iy >= hiinf) {
191 /*
192 * non-zero imag part(s) with inf component(s) yields NaN
193 */
194 t = fabsl(x) + fabsl(y) + fabsl(u) + fabsl(v);
195 LD_RE(ans) = LD_IM(ans) = t - t;
196 } else {
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 */
202 }
203 /*
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))
207 */
208
209 t1 = __k_clog_rl(x, y, &t2);
210 t3 = __k_atan2l(y, x, &t4);
211 x1 = t1; HALF(x1);
212 y1 = t3; HALF(y1);
213 u1 = u; HALF(u1);
214 v1 = v; HALF(v1);
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)) */
218 if (j != 2) {
219 b[0] = u1 * y1;
220 b[1] = (u - u1) * y1 + u * y2;
221 if (j == 1) { /* v = 0 */
222 w1 = b[0] + b[1];
223 w2 = b[1] - (w1 - b[0]);
224 } else {
225 b[2] = v1 * x1;
226 b[3] = (v - v1) * x1 + v * x2;
227 w1 = sum4fpl(b, &w2);
228 }
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++) {
235 t1 = s * c;
236 c = (c + s) * (c - s);
237 s = t1 + t1;
238 }
239 }
240 /* compute r = u * (t1, t2) - v * (t3, t4) */
241 b[0] = u1 * x1;
242 b[1] = (u - u1) * x1 + u * x2;
243 if (j == 1) { /* v = 0 */
244 w1 = b[0] + b[1];
245 w2 = b[1] - (w1 - b[0]);
246 } else {
247 b[2] = -v1 * y1;
248 b[3] = (v1 - v) * y1 - v * y2;
249 w1 = sum4fpl(b, &w2);
250 }
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;
254 }
255 hx = HI_XWORD(w1);
256 ix = hx & 0x7fffffff;
257 /* compute exp(w1 + w2) */
258 k = 0;
259 if (ix < 0x3f8c0000) /* exp(tiny < 2**-115) = 1 */
260 r = one;
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;
268 r = expl(t2 + t3);
269 }
270 if (c != zero) c *= r;
271 if (s != zero) s *= r;
272 if (k != 0) {
273 c = scalbnl(c, k);
274 s = scalbnl(s, k);
275 }
276 LD_RE(ans) = c;
277 LD_IM(ans) = s;
278 }
279 return (ans);
280 }
|
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 */
25
26 /*
27 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
29 */
30
31 #pragma weak __cpowl = cpowl
32
33 #include "libm.h" /* __k_clog_rl/__k_atan2l */
34 /* atan2l/atan2pil/exp2l/expl/fabsl/hypotl/isinfl/logl/powl/sincosl/sincospil */
35 #include "complex_wrapper.h"
36 #include "longdouble.h"
37
38 #if defined(__sparc)
39 #define HALF(x) ((int *)&x)[3] = 0; ((int *)&x)[2] &= 0xfe000000
40 #define LAST(x) ((int *)&x)[3]
41 #elif defined(__x86)
42 #define HALF(x) ((int *)&x)[0] = 0
43 #define LAST(x) ((int *)&x)[0]
44 #endif
45
46 static const int hiinf = 0x7fff0000;
47 static const long double tiny = 1.0e-4000L,
48 huge = 1.0e4000L,
49 #if defined(__x86)
50 /* 43 significant bits, 21 trailing zeros */
51 ln2hil = 0.693147180559890330187045037746429443359375L,
52 ln2lol = 5.497923018708371174712471612513436025525412068e-14L,
53 #else /* sparc */
54 /* 0x3FF962E4 2FEFA39E F35793C7 00000000 */
55 ln2hil = 0.693147180559945309417231592858066493070671489074L,
56 ln2lol = 5.28600110075004828645286235820646730106802446566153e-25L,
57 #endif
58 invln2 = 1.442695040888963407359924681001892137427e+0000L,
59 one = 1.0L,
60 zero = 0.0L;
61
62 /*
63 * Assuming |t[0]| > |t[1]| and |t[2]| > |t[3]|, sum4fpl subroutine
64 * compute t[0] + t[1] + t[2] + t[3] into two long double fp numbers.
65 */
66 static long double
67 sum4fpl(long double ta[], long double *w)
68 {
69 long double t1, t2, t3, t4, w1, w2, t;
70
71 t1 = ta[0];
72 t2 = ta[1];
73 t3 = ta[2];
74 t4 = ta[3];
75
76 /*
77 * Rearrange ti so that |t1| >= |t2| >= |t3| >= |t4|
78 */
79 if (fabsl(t4) > fabsl(t1)) {
80 t = t1;
81 t1 = t3;
82 t3 = t;
83 t = t2;
84 t2 = t4;
85 t4 = t;
86 } else if (fabsl(t3) > fabsl(t1)) {
87 t = t1;
88 t1 = t3;
89
90 if (fabsl(t4) > fabsl(t2)) {
91 t3 = t4;
92 t4 = t2;
93 t2 = t;
94 } else {
95 t3 = t2;
96 t2 = t;
97 }
98 } else if (fabsl(t3) > fabsl(t2)) {
99 t = t2;
100 t2 = t3;
101
102 if (fabsl(t4) > fabsl(t2)) {
103 t3 = t4;
104 t4 = t;
105 } else {
106 t3 = t;
107 }
108 }
109
110 /* summing r = t1 + t2 + t3 + t4 to w1 + w2 */
111 w1 = t3 + t4;
112 w2 = t4 - (w1 - t3);
113 t = t2 + w1;
114 w2 += w1 - (t - t2);
115 w1 = t + w2;
116 w2 += t - w1;
117 t = t1 + w1;
118 w2 += w1 - (t - t1);
119 w1 = t + w2;
120 *w = w2 - (w1 - t);
121 return (w1);
122 }
123
124 ldcomplex
125 cpowl(ldcomplex z, ldcomplex w)
126 {
127 ldcomplex ans;
128 long double x, y, u, v, t, c, s, r;
129 long double t1, t2, t3, t4, x1, x2, y1, y2, u1, v1, b[4], w1, w2;
130 int ix, iy, hx, hy, hv, hu, iu, iv, i, j, k;
131
132 x = LD_RE(z);
133 y = LD_IM(z);
134 u = LD_RE(w);
135 v = LD_IM(w);
136 hx = HI_XWORD(x);
137 hy = HI_XWORD(y);
138 hu = HI_XWORD(u);
139 hv = HI_XWORD(v);
140 ix = hx & 0x7fffffff;
141 iy = hy & 0x7fffffff;
142 iu = hu & 0x7fffffff;
143 iv = hv & 0x7fffffff;
144
145 j = 0;
146
147 if (v == zero) { /* z**(real) */
148 if (u == one) { /* (anything) ** 1 is itself */
149 LD_RE(ans) = x;
150 LD_IM(ans) = y;
151 } else if (u == zero) { /* (anything) ** 0 is 1 */
152 LD_RE(ans) = one;
153 LD_IM(ans) = zero;
154 } else if (y == zero) { /* real ** real */
155 LD_IM(ans) = zero;
156
157 if (hx < 0 && ix < hiinf && iu < hiinf) {
158 /* -x ** u is exp(i*pi*u)*pow(x,u) */
159 r = powl(-x, u);
160 sincospil(u, &s, &c);
161 LD_RE(ans) = (c == zero) ? c : c *r;
162 LD_IM(ans) = (s == zero) ? s : s *r;
163 } else {
164 LD_RE(ans) = powl(x, u);
165 }
166 } else if (x == zero || ix >= hiinf || iy >= hiinf) {
167 if (isnanl(x) || isnanl(y) || isnanl(u)) {
168 LD_RE(ans) = LD_IM(ans) = x + y + u;
169 } else {
170 if (x == zero)
171 r = fabsl(y);
172 else
173 r = fabsl(x) + fabsl(y);
174
175 t = atan2pil(y, x);
176 sincospil(t * u, &s, &c);
177 LD_RE(ans) = (c == zero) ? c : c *r;
178 LD_IM(ans) = (s == zero) ? s : s *r;
179 }
180 } else if (fabsl(x) == fabsl(y)) { /* |x| = |y| */
181 if (hx >= 0) {
182 t = (hy >= 0) ? 0.25L : -0.25L;
183 sincospil(t * u, &s, &c);
184 } else if ((LAST(u) & 3) == 0) {
185 t = (hy >= 0) ? 0.75L : -0.75L;
186 sincospil(t * u, &s, &c);
187 } else {
188 r = (hy >= 0) ? u : -u;
189 t = -0.25L * r;
190 w1 = r + t;
191 w2 = t - (w1 - r);
192 sincospil(w1, &t1, &t2);
193 sincospil(w2, &t3, &t4);
194 s = t1 * t4 + t3 * t2;
195 c = t2 * t4 - t1 * t3;
196 }
197
198 if (ix < 0x3ffe0000) /* |x| < 1/2 */
199 r = powl(fabsl(x + x), u) * exp2l(-0.5L * u);
200 else if (ix >= 0x3fff0000 || iu < 0x400cfff8)
201 /* |x| >= 1 or |u| < 16383 */
202 r = powl(fabsl(x), u) * exp2l(0.5L * u);
203 else /* special treatment */
204 j = 2;
205
206 if (j == 0) {
207 LD_RE(ans) = (c == zero) ? c : c *r;
208 LD_IM(ans) = (s == zero) ? s : s *r;
209 }
210 } else {
211 j = 1;
212 }
213
214 if (j == 0)
215 return (ans);
216 }
217
218 if (iu >= hiinf || iv >= hiinf || ix >= hiinf || iy >= hiinf) {
219 /*
220 * non-zero imag part(s) with inf component(s) yields NaN
221 */
222 t = fabsl(x) + fabsl(y) + fabsl(u) + fabsl(v);
223 LD_RE(ans) = LD_IM(ans) = t - t;
224 } else {
225 k = 0; /* no scaling */
226
227 if (iu > 0x7ffe0000 || iv > 0x7ffe0000) {
228 u *= 1.52587890625000000000e-05L;
229 v *= 1.52587890625000000000e-05L;
230 k = 1; /* scale u and v by 2**-16 */
231 }
232
233 /*
234 * Use similated higher precision arithmetic to compute:
235 * r = u * log(hypot(x, y)) - v * atan2(y, x)
236 * q = u * atan2(y, x) + v * log(hypot(x, y))
237 */
238
239 t1 = __k_clog_rl(x, y, &t2);
240 t3 = __k_atan2l(y, x, &t4);
241 x1 = t1;
242 HALF(x1);
243 y1 = t3;
244 HALF(y1);
245 u1 = u;
246 HALF(u1);
247 v1 = v;
248 HALF(v1);
249 x2 = t2 - (x1 - t1); /* log(hypot(x,y)) = x1 + x2 */
250 y2 = t4 - (y1 - t3); /* atan2(y,x) = y1 + y2 */
251
252 /* compute q = u * atan2(y, x) + v * log(hypot(x, y)) */
253 if (j != 2) {
254 b[0] = u1 * y1;
255 b[1] = (u - u1) * y1 + u * y2;
256
257 if (j == 1) { /* v = 0 */
258 w1 = b[0] + b[1];
259 w2 = b[1] - (w1 - b[0]);
260 } else {
261 b[2] = v1 * x1;
262 b[3] = (v - v1) * x1 + v * x2;
263 w1 = sum4fpl(b, &w2);
264 }
265
266 sincosl(w1, &t1, &t2);
267 sincosl(w2, &t3, &t4);
268 s = t1 * t4 + t3 * t2;
269 c = t2 * t4 - t1 * t3;
270
271 if (k == 1) { /* square j times */
272 for (i = 0; i < 10; i++) {
273 t1 = s * c;
274 c = (c + s) * (c - s);
275 s = t1 + t1;
276 }
277 }
278 }
279
280 /* compute r = u * (t1, t2) - v * (t3, t4) */
281 b[0] = u1 * x1;
282 b[1] = (u - u1) * x1 + u * x2;
283
284 if (j == 1) { /* v = 0 */
285 w1 = b[0] + b[1];
286 w2 = b[1] - (w1 - b[0]);
287 } else {
288 b[2] = -v1 * y1;
289 b[3] = (v1 - v) * y1 - v * y2;
290 w1 = sum4fpl(b, &w2);
291 }
292
293 /* scale back unless w1 is large enough to cause exception */
294 if (k != 0 && fabsl(w1) < 20000.0L) {
295 w1 *= 65536.0L;
296 w2 *= 65536.0L;
297 }
298
299 hx = HI_XWORD(w1);
300 ix = hx & 0x7fffffff;
301 /* compute exp(w1 + w2) */
302 k = 0;
303
304 if (ix < 0x3f8c0000) { /* exp(tiny < 2**-115) = 1 */
305 r = one;
306 } else if (ix >= 0x400c6760) { /* overflow/underflow */
307 r = (hx < 0) ? tiny * tiny : huge * huge;
308 } else { /* compute exp(w1 + w2) */
309 k = (int)(invln2 * w1 + ((hx >= 0) ? 0.5L : -0.5L));
310 t1 = (long double)k;
311 t2 = w1 - t1 * ln2hil;
312 t3 = w2 - t1 * ln2lol;
313 r = expl(t2 + t3);
314 }
315
316 if (c != zero)
317 c *= r;
318
319 if (s != zero)
320 s *= r;
321
322 if (k != 0) {
323 c = scalbnl(c, k);
324 s = scalbnl(s, k);
325 }
326
327 LD_RE(ans) = c;
328 LD_IM(ans) = s;
329 }
330
331 return (ans);
332 }
|