xref: /titanic_51/usr/src/lib/libm/common/complex/cpowl.c (revision ddc0e0b53c661f6e439e3b7072b3ef353eadb4af)
125c28e83SPiotr Jasiukajtis /*
225c28e83SPiotr Jasiukajtis  * CDDL HEADER START
325c28e83SPiotr Jasiukajtis  *
425c28e83SPiotr Jasiukajtis  * The contents of this file are subject to the terms of the
525c28e83SPiotr Jasiukajtis  * Common Development and Distribution License (the "License").
625c28e83SPiotr Jasiukajtis  * You may not use this file except in compliance with the License.
725c28e83SPiotr Jasiukajtis  *
825c28e83SPiotr Jasiukajtis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
925c28e83SPiotr Jasiukajtis  * or http://www.opensolaris.org/os/licensing.
1025c28e83SPiotr Jasiukajtis  * See the License for the specific language governing permissions
1125c28e83SPiotr Jasiukajtis  * and limitations under the License.
1225c28e83SPiotr Jasiukajtis  *
1325c28e83SPiotr Jasiukajtis  * When distributing Covered Code, include this CDDL HEADER in each
1425c28e83SPiotr Jasiukajtis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1525c28e83SPiotr Jasiukajtis  * If applicable, add the following below this CDDL HEADER, with the
1625c28e83SPiotr Jasiukajtis  * fields enclosed by brackets "[]" replaced with your own identifying
1725c28e83SPiotr Jasiukajtis  * information: Portions Copyright [yyyy] [name of copyright owner]
1825c28e83SPiotr Jasiukajtis  *
1925c28e83SPiotr Jasiukajtis  * CDDL HEADER END
2025c28e83SPiotr Jasiukajtis  */
2125c28e83SPiotr Jasiukajtis 
2225c28e83SPiotr Jasiukajtis /*
2325c28e83SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
2425c28e83SPiotr Jasiukajtis  */
2525c28e83SPiotr Jasiukajtis /*
2625c28e83SPiotr Jasiukajtis  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2725c28e83SPiotr Jasiukajtis  * Use is subject to license terms.
2825c28e83SPiotr Jasiukajtis  */
2925c28e83SPiotr Jasiukajtis 
30*ddc0e0b5SRichard Lowe #pragma weak __cpowl = cpowl
3125c28e83SPiotr Jasiukajtis 
3225c28e83SPiotr Jasiukajtis #include "libm.h"				/* __k_clog_rl/__k_atan2l */
3325c28e83SPiotr Jasiukajtis /* atan2l/atan2pil/exp2l/expl/fabsl/hypotl/isinfl/logl/powl/sincosl/sincospil */
3425c28e83SPiotr Jasiukajtis #include "complex_wrapper.h"
3525c28e83SPiotr Jasiukajtis #include "longdouble.h"
3625c28e83SPiotr Jasiukajtis 
3725c28e83SPiotr Jasiukajtis #if defined(__sparc)
3825c28e83SPiotr Jasiukajtis #define	HALF(x)  ((int *) &x)[3] = 0; ((int *) &x)[2] &= 0xfe000000
3925c28e83SPiotr Jasiukajtis #define	LAST(x)  ((int *) &x)[3]
4025c28e83SPiotr Jasiukajtis #elif defined(__x86)
4125c28e83SPiotr Jasiukajtis #define	HALF(x)  ((int *) &x)[0] = 0
4225c28e83SPiotr Jasiukajtis #define	LAST(x)  ((int *) &x)[0]
4325c28e83SPiotr Jasiukajtis #endif
4425c28e83SPiotr Jasiukajtis 
4525c28e83SPiotr Jasiukajtis /* INDENT OFF */
4625c28e83SPiotr Jasiukajtis static const int hiinf = 0x7fff0000;
4725c28e83SPiotr Jasiukajtis static const long double
4825c28e83SPiotr Jasiukajtis 	tiny = 1.0e-4000L,
4925c28e83SPiotr Jasiukajtis 	huge = 1.0e4000L,
5025c28e83SPiotr Jasiukajtis #if defined(__x86)
5125c28e83SPiotr Jasiukajtis 		/* 43 significant bits, 21 trailing zeros */
5225c28e83SPiotr Jasiukajtis 	ln2hil = 0.693147180559890330187045037746429443359375L,
5325c28e83SPiotr Jasiukajtis 	ln2lol = 5.497923018708371174712471612513436025525412068e-14L,
5425c28e83SPiotr Jasiukajtis #else   /* sparc */
5525c28e83SPiotr Jasiukajtis 		/* 0x3FF962E4 2FEFA39E F35793C7 00000000 */
5625c28e83SPiotr Jasiukajtis 	ln2hil = 0.693147180559945309417231592858066493070671489074L,
5725c28e83SPiotr Jasiukajtis 	ln2lol = 5.28600110075004828645286235820646730106802446566153e-25L,
5825c28e83SPiotr Jasiukajtis #endif
5925c28e83SPiotr Jasiukajtis 	invln2  = 1.442695040888963407359924681001892137427e+0000L,
6025c28e83SPiotr Jasiukajtis 	one = 1.0L,
6125c28e83SPiotr Jasiukajtis 	zero = 0.0L;
6225c28e83SPiotr Jasiukajtis /* INDENT ON */
6325c28e83SPiotr Jasiukajtis 
6425c28e83SPiotr Jasiukajtis /*
6525c28e83SPiotr Jasiukajtis  * Assuming |t[0]| > |t[1]| and |t[2]| > |t[3]|, sum4fpl subroutine
6625c28e83SPiotr Jasiukajtis  * compute t[0] + t[1] + t[2] + t[3] into two long double fp numbers.
6725c28e83SPiotr Jasiukajtis  */
6825c28e83SPiotr Jasiukajtis static long double sum4fpl(long double ta[], long double *w)
6925c28e83SPiotr Jasiukajtis {
7025c28e83SPiotr Jasiukajtis 	long double t1, t2, t3, t4, w1, w2, t;
7125c28e83SPiotr Jasiukajtis 	t1 = ta[0]; t2 = ta[1]; t3 = ta[2]; t4 = ta[3];
7225c28e83SPiotr Jasiukajtis 	/*
7325c28e83SPiotr Jasiukajtis 	 * Rearrange ti so that |t1| >= |t2| >= |t3| >= |t4|
7425c28e83SPiotr Jasiukajtis 	 */
7525c28e83SPiotr Jasiukajtis 	if (fabsl(t4) > fabsl(t1)) {
7625c28e83SPiotr Jasiukajtis 		t = t1; t1 = t3; t3 = t;
7725c28e83SPiotr Jasiukajtis 		t = t2; t2 = t4; t4 = t;
7825c28e83SPiotr Jasiukajtis 	} else if (fabsl(t3) > fabsl(t1)) {
7925c28e83SPiotr Jasiukajtis 		t = t1; t1 = t3;
8025c28e83SPiotr Jasiukajtis 		if (fabsl(t4) > fabsl(t2)) {
8125c28e83SPiotr Jasiukajtis 			t3 = t4; t4 = t2; t2 = t;
8225c28e83SPiotr Jasiukajtis 		} else {
8325c28e83SPiotr Jasiukajtis 			t3 = t2; t2 = t;
8425c28e83SPiotr Jasiukajtis 		}
8525c28e83SPiotr Jasiukajtis 	} else if (fabsl(t3) > fabsl(t2)) {
8625c28e83SPiotr Jasiukajtis 		t = t2; t2 = t3;
8725c28e83SPiotr Jasiukajtis 		if (fabsl(t4) > fabsl(t2)) {
8825c28e83SPiotr Jasiukajtis 			t3 = t4; t4 = t;
8925c28e83SPiotr Jasiukajtis 		} else
9025c28e83SPiotr Jasiukajtis 			t3 = t;
9125c28e83SPiotr Jasiukajtis 	}
9225c28e83SPiotr Jasiukajtis 	/* summing r = t1 + t2 + t3 + t4 to w1 + w2 */
9325c28e83SPiotr Jasiukajtis 	w1 = t3 + t4;
9425c28e83SPiotr Jasiukajtis 	w2 = t4 - (w1 - t3);
9525c28e83SPiotr Jasiukajtis 	t  = t2 + w1;
9625c28e83SPiotr Jasiukajtis 	w2 += w1 - (t - t2);
9725c28e83SPiotr Jasiukajtis 	w1 = t + w2;
9825c28e83SPiotr Jasiukajtis 	w2 += t - w1;
9925c28e83SPiotr Jasiukajtis 	t  = t1 + w1;
10025c28e83SPiotr Jasiukajtis 	w2 += w1 - (t - t1);
10125c28e83SPiotr Jasiukajtis 	w1 = t + w2;
10225c28e83SPiotr Jasiukajtis 	*w = w2 - (w1 - t);
10325c28e83SPiotr Jasiukajtis 	return (w1);
10425c28e83SPiotr Jasiukajtis }
10525c28e83SPiotr Jasiukajtis 
10625c28e83SPiotr Jasiukajtis ldcomplex
10725c28e83SPiotr Jasiukajtis cpowl(ldcomplex z, ldcomplex w) {
10825c28e83SPiotr Jasiukajtis 	ldcomplex ans;
10925c28e83SPiotr Jasiukajtis 	long double x, y, u, v, t, c, s, r;
11025c28e83SPiotr Jasiukajtis 	long double t1, t2, t3, t4, x1, x2, y1, y2, u1, v1, b[4], w1, w2;
11125c28e83SPiotr Jasiukajtis 	int ix, iy, hx, hy, hv, hu, iu, iv, i, j, k;
11225c28e83SPiotr Jasiukajtis 
11325c28e83SPiotr Jasiukajtis 	x = LD_RE(z);
11425c28e83SPiotr Jasiukajtis 	y = LD_IM(z);
11525c28e83SPiotr Jasiukajtis 	u = LD_RE(w);
11625c28e83SPiotr Jasiukajtis 	v = LD_IM(w);
11725c28e83SPiotr Jasiukajtis 	hx = HI_XWORD(x);
11825c28e83SPiotr Jasiukajtis 	hy = HI_XWORD(y);
11925c28e83SPiotr Jasiukajtis 	hu = HI_XWORD(u);
12025c28e83SPiotr Jasiukajtis 	hv = HI_XWORD(v);
12125c28e83SPiotr Jasiukajtis 	ix = hx & 0x7fffffff;
12225c28e83SPiotr Jasiukajtis 	iy = hy & 0x7fffffff;
12325c28e83SPiotr Jasiukajtis 	iu = hu & 0x7fffffff;
12425c28e83SPiotr Jasiukajtis 	iv = hv & 0x7fffffff;
12525c28e83SPiotr Jasiukajtis 
12625c28e83SPiotr Jasiukajtis 	j = 0;
12725c28e83SPiotr Jasiukajtis 	if (v == zero) {	/* z**(real) */
12825c28e83SPiotr Jasiukajtis 		if (u == one) {	/* (anything) ** 1  is itself */
12925c28e83SPiotr Jasiukajtis 			LD_RE(ans) = x;
13025c28e83SPiotr Jasiukajtis 			LD_IM(ans) = y;
13125c28e83SPiotr Jasiukajtis 		} else if (u == zero) {	/* (anything) ** 0  is 1 */
13225c28e83SPiotr Jasiukajtis 			LD_RE(ans) = one;
13325c28e83SPiotr Jasiukajtis 			LD_IM(ans) = zero;
13425c28e83SPiotr Jasiukajtis 		} else if (y == zero) {	/* real ** real */
13525c28e83SPiotr Jasiukajtis 			LD_IM(ans) = zero;
13625c28e83SPiotr Jasiukajtis 			if (hx < 0 && ix < hiinf && iu < hiinf) {
13725c28e83SPiotr Jasiukajtis 			/* -x ** u  is exp(i*pi*u)*pow(x,u) */
13825c28e83SPiotr Jasiukajtis 				r = powl(-x, u);
13925c28e83SPiotr Jasiukajtis 				sincospil(u, &s, &c);
14025c28e83SPiotr Jasiukajtis 				LD_RE(ans) = (c == zero)? c: c * r;
14125c28e83SPiotr Jasiukajtis 				LD_IM(ans) = (s == zero)? s: s * r;
14225c28e83SPiotr Jasiukajtis 			} else
14325c28e83SPiotr Jasiukajtis 				LD_RE(ans) = powl(x, u);
14425c28e83SPiotr Jasiukajtis 		} else if (x == zero || ix >= hiinf || iy >= hiinf) {
14525c28e83SPiotr Jasiukajtis 			if (isnanl(x) || isnanl(y) || isnanl(u))
14625c28e83SPiotr Jasiukajtis 				LD_RE(ans) = LD_IM(ans) = x + y + u;
14725c28e83SPiotr Jasiukajtis 			else {
14825c28e83SPiotr Jasiukajtis 				if (x == zero)
14925c28e83SPiotr Jasiukajtis 					r = fabsl(y);
15025c28e83SPiotr Jasiukajtis 				else
15125c28e83SPiotr Jasiukajtis 					r = fabsl(x) + fabsl(y);
15225c28e83SPiotr Jasiukajtis 				t = atan2pil(y, x);
15325c28e83SPiotr Jasiukajtis 				sincospil(t * u, &s, &c);
15425c28e83SPiotr Jasiukajtis 				LD_RE(ans) = (c == zero)? c: c * r;
15525c28e83SPiotr Jasiukajtis 				LD_IM(ans) = (s == zero)? s: s * r;
15625c28e83SPiotr Jasiukajtis 			}
15725c28e83SPiotr Jasiukajtis 		} else if (fabsl(x) == fabsl(y)) {    /* |x| = |y| */
15825c28e83SPiotr Jasiukajtis 			if (hx >= 0) {
15925c28e83SPiotr Jasiukajtis 				t = (hy >= 0)? 0.25L : -0.25L;
16025c28e83SPiotr Jasiukajtis 				sincospil(t * u, &s, &c);
16125c28e83SPiotr Jasiukajtis 			} else if ((LAST(u) & 3) == 0) {
16225c28e83SPiotr Jasiukajtis 				t = (hy >= 0)? 0.75L : -0.75L;
16325c28e83SPiotr Jasiukajtis 				sincospil(t * u, &s, &c);
16425c28e83SPiotr Jasiukajtis 			} else {
16525c28e83SPiotr Jasiukajtis 				r = (hy >= 0)? u : -u;
16625c28e83SPiotr Jasiukajtis 				t = -0.25L * r;
16725c28e83SPiotr Jasiukajtis 				w1 = r + t;
16825c28e83SPiotr Jasiukajtis 				w2 = t - (w1 - r);
16925c28e83SPiotr Jasiukajtis 				sincospil(w1, &t1, &t2);
17025c28e83SPiotr Jasiukajtis 				sincospil(w2, &t3, &t4);
17125c28e83SPiotr Jasiukajtis 				s = t1 * t4 + t3 * t2;
17225c28e83SPiotr Jasiukajtis 				c = t2 * t4 - t1 * t3;
17325c28e83SPiotr Jasiukajtis 			}
17425c28e83SPiotr Jasiukajtis 			if (ix < 0x3ffe0000)	/* |x| < 1/2 */
17525c28e83SPiotr Jasiukajtis 				r = powl(fabsl(x + x), u) * exp2l(-0.5L * u);
17625c28e83SPiotr Jasiukajtis 			else if (ix >= 0x3fff0000 || iu < 0x400cfff8)
17725c28e83SPiotr Jasiukajtis 				/* |x| >= 1 or |u| < 16383 */
17825c28e83SPiotr Jasiukajtis 				r = powl(fabsl(x), u) * exp2l(0.5L * u);
17925c28e83SPiotr Jasiukajtis 			else   /* special treatment */
18025c28e83SPiotr Jasiukajtis 				j = 2;
18125c28e83SPiotr Jasiukajtis 			if (j == 0) {
18225c28e83SPiotr Jasiukajtis 				LD_RE(ans) = (c == zero)? c: c * r;
18325c28e83SPiotr Jasiukajtis 				LD_IM(ans) = (s == zero)? s: s * r;
18425c28e83SPiotr Jasiukajtis 			}
18525c28e83SPiotr Jasiukajtis 		} else
18625c28e83SPiotr Jasiukajtis 			j = 1;
18725c28e83SPiotr Jasiukajtis 		if (j == 0)
18825c28e83SPiotr Jasiukajtis 			return (ans);
18925c28e83SPiotr Jasiukajtis 	}
19025c28e83SPiotr Jasiukajtis 	if (iu >= hiinf || iv >= hiinf || ix >= hiinf || iy >= hiinf) {
19125c28e83SPiotr Jasiukajtis 		/*
19225c28e83SPiotr Jasiukajtis 		 * non-zero imag part(s) with inf component(s) yields NaN
19325c28e83SPiotr Jasiukajtis 		 */
19425c28e83SPiotr Jasiukajtis 		t = fabsl(x) + fabsl(y) + fabsl(u) + fabsl(v);
19525c28e83SPiotr Jasiukajtis 		LD_RE(ans) = LD_IM(ans) = t - t;
19625c28e83SPiotr Jasiukajtis 	} else {
19725c28e83SPiotr Jasiukajtis 		k = 0;	/* no scaling */
19825c28e83SPiotr Jasiukajtis 		if (iu > 0x7ffe0000 || iv > 0x7ffe0000) {
19925c28e83SPiotr Jasiukajtis 			u *= 1.52587890625000000000e-05L;
20025c28e83SPiotr Jasiukajtis 			v *= 1.52587890625000000000e-05L;
20125c28e83SPiotr Jasiukajtis 			k = 1;	/* scale u and v by 2**-16 */
20225c28e83SPiotr Jasiukajtis 		}
20325c28e83SPiotr Jasiukajtis 		/*
20425c28e83SPiotr Jasiukajtis 		 * Use similated higher precision arithmetic to compute:
20525c28e83SPiotr Jasiukajtis 		 * r = u * log(hypot(x, y)) - v * atan2(y, x)
20625c28e83SPiotr Jasiukajtis 		 * q = u * atan2(y, x) + v * log(hypot(x, y))
20725c28e83SPiotr Jasiukajtis 		 */
20825c28e83SPiotr Jasiukajtis 
20925c28e83SPiotr Jasiukajtis 		t1 = __k_clog_rl(x, y, &t2);
21025c28e83SPiotr Jasiukajtis 		t3 = __k_atan2l(y, x, &t4);
21125c28e83SPiotr Jasiukajtis 		x1 = t1; HALF(x1);
21225c28e83SPiotr Jasiukajtis 		y1 = t3; HALF(y1);
21325c28e83SPiotr Jasiukajtis 		u1 = u; HALF(u1);
21425c28e83SPiotr Jasiukajtis 		v1 = v; HALF(v1);
21525c28e83SPiotr Jasiukajtis 		x2 = t2 - (x1 - t1);    /* log(hypot(x,y)) = x1 + x2 */
21625c28e83SPiotr Jasiukajtis 		y2 = t4 - (y1 - t3);    /* atan2(y,x) = y1 + y2 */
21725c28e83SPiotr Jasiukajtis 		/* compute q = u * atan2(y, x) + v * log(hypot(x, y)) */
21825c28e83SPiotr Jasiukajtis 		if (j != 2) {
21925c28e83SPiotr Jasiukajtis 			b[0] = u1 * y1;
22025c28e83SPiotr Jasiukajtis 			b[1] = (u - u1) * y1 + u * y2;
22125c28e83SPiotr Jasiukajtis 			if (j == 1) {	/* v = 0 */
22225c28e83SPiotr Jasiukajtis 				w1 = b[0] + b[1];
22325c28e83SPiotr Jasiukajtis 				w2 = b[1] - (w1 - b[0]);
22425c28e83SPiotr Jasiukajtis 			} else {
22525c28e83SPiotr Jasiukajtis 				b[2] = v1 * x1;
22625c28e83SPiotr Jasiukajtis 				b[3] = (v - v1) * x1 + v * x2;
22725c28e83SPiotr Jasiukajtis 				w1 = sum4fpl(b, &w2);
22825c28e83SPiotr Jasiukajtis 			}
22925c28e83SPiotr Jasiukajtis 			sincosl(w1, &t1, &t2);
23025c28e83SPiotr Jasiukajtis 			sincosl(w2, &t3, &t4);
23125c28e83SPiotr Jasiukajtis 			s = t1 * t4 + t3 * t2;
23225c28e83SPiotr Jasiukajtis 			c = t2 * t4 - t1 * t3;
23325c28e83SPiotr Jasiukajtis 			if (k == 1)	/* square j times */
23425c28e83SPiotr Jasiukajtis 				for (i = 0; i < 10; i++) {
23525c28e83SPiotr Jasiukajtis 					t1 = s * c;
23625c28e83SPiotr Jasiukajtis 					c = (c + s) * (c - s);
23725c28e83SPiotr Jasiukajtis 					s = t1 + t1;
23825c28e83SPiotr Jasiukajtis 				}
23925c28e83SPiotr Jasiukajtis 		}
24025c28e83SPiotr Jasiukajtis 		/* compute r = u * (t1, t2) - v * (t3, t4) */
24125c28e83SPiotr Jasiukajtis 		b[0] = u1 * x1;
24225c28e83SPiotr Jasiukajtis 		b[1] = (u - u1) * x1 + u * x2;
24325c28e83SPiotr Jasiukajtis 		if (j == 1) {   /* v = 0 */
24425c28e83SPiotr Jasiukajtis 			w1 = b[0] + b[1];
24525c28e83SPiotr Jasiukajtis 			w2 = b[1] - (w1 - b[0]);
24625c28e83SPiotr Jasiukajtis 		} else {
24725c28e83SPiotr Jasiukajtis 			b[2] = -v1 * y1;
24825c28e83SPiotr Jasiukajtis 			b[3] = (v1 - v) * y1 - v * y2;
24925c28e83SPiotr Jasiukajtis 			w1 = sum4fpl(b, &w2);
25025c28e83SPiotr Jasiukajtis 		}
25125c28e83SPiotr Jasiukajtis 		/* scale back unless w1 is large enough to cause exception */
25225c28e83SPiotr Jasiukajtis 		if (k != 0 && fabsl(w1) < 20000.0L) {
25325c28e83SPiotr Jasiukajtis 			w1 *= 65536.0L; w2 *= 65536.0L;
25425c28e83SPiotr Jasiukajtis 		}
25525c28e83SPiotr Jasiukajtis 		hx = HI_XWORD(w1);
25625c28e83SPiotr Jasiukajtis 		ix = hx & 0x7fffffff;
25725c28e83SPiotr Jasiukajtis 		/* compute exp(w1 + w2) */
25825c28e83SPiotr Jasiukajtis 		k = 0;
25925c28e83SPiotr Jasiukajtis 		if (ix < 0x3f8c0000) /* exp(tiny < 2**-115) = 1 */
26025c28e83SPiotr Jasiukajtis 			r = one;
26125c28e83SPiotr Jasiukajtis 		else if (ix >= 0x400c6760) /* overflow/underflow */
26225c28e83SPiotr Jasiukajtis 			r = (hx < 0)? tiny * tiny : huge * huge;
26325c28e83SPiotr Jasiukajtis 		else {  /* compute exp(w1 + w2) */
26425c28e83SPiotr Jasiukajtis 			k = (int) (invln2 * w1 + ((hx >= 0)? 0.5L : -0.5L));
26525c28e83SPiotr Jasiukajtis 			t1 = (long double) k;
26625c28e83SPiotr Jasiukajtis 			t2 = w1 - t1 * ln2hil;
26725c28e83SPiotr Jasiukajtis 			t3 = w2 - t1 * ln2lol;
26825c28e83SPiotr Jasiukajtis 			r = expl(t2 + t3);
26925c28e83SPiotr Jasiukajtis 		}
27025c28e83SPiotr Jasiukajtis 		if (c != zero) c *= r;
27125c28e83SPiotr Jasiukajtis 		if (s != zero) s *= r;
27225c28e83SPiotr Jasiukajtis 		if (k != 0) {
27325c28e83SPiotr Jasiukajtis 			c = scalbnl(c, k);
27425c28e83SPiotr Jasiukajtis 			s = scalbnl(s, k);
27525c28e83SPiotr Jasiukajtis 		}
27625c28e83SPiotr Jasiukajtis 		LD_RE(ans) = c;
27725c28e83SPiotr Jasiukajtis 		LD_IM(ans) = s;
27825c28e83SPiotr Jasiukajtis 	}
27925c28e83SPiotr Jasiukajtis 	return (ans);
28025c28e83SPiotr Jasiukajtis }
281