xref: /titanic_44/usr/src/lib/libm/common/complex/cacos.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 __cacos = cacos
3125c28e83SPiotr Jasiukajtis 
3225c28e83SPiotr Jasiukajtis /* INDENT OFF */
3325c28e83SPiotr Jasiukajtis /*
3425c28e83SPiotr Jasiukajtis  * dcomplex cacos(dcomplex z);
3525c28e83SPiotr Jasiukajtis  *
3625c28e83SPiotr Jasiukajtis  * Alogrithm
3725c28e83SPiotr Jasiukajtis  * (based on T.E.Hull, Thomas F. Fairgrieve and Ping Tak Peter Tang's
3825c28e83SPiotr Jasiukajtis  * paper "Implementing the Complex Arcsine and Arccosine Functins Using
3925c28e83SPiotr Jasiukajtis  * Exception Handling", ACM TOMS, Vol 23, pp 299-335)
4025c28e83SPiotr Jasiukajtis  *
4125c28e83SPiotr Jasiukajtis  * The principal value of complex inverse cosine function cacos(z),
4225c28e83SPiotr Jasiukajtis  * where z = x+iy, can be defined by
4325c28e83SPiotr Jasiukajtis  *
4425c28e83SPiotr Jasiukajtis  * 	cacos(z) = acos(B) - i sign(y) log (A + sqrt(A*A-1)),
4525c28e83SPiotr Jasiukajtis  *
4625c28e83SPiotr Jasiukajtis  * where the log function is the natural log, and
4725c28e83SPiotr Jasiukajtis  *             ____________           ____________
4825c28e83SPiotr Jasiukajtis  *       1    /     2    2      1    /     2    2
4925c28e83SPiotr Jasiukajtis  *  A = ---  / (x+1)  + y   +  ---  / (x-1)  + y
5025c28e83SPiotr Jasiukajtis  *       2 \/                   2 \/
5125c28e83SPiotr Jasiukajtis  *             ____________           ____________
5225c28e83SPiotr Jasiukajtis  *       1    /     2    2      1    /     2    2
5325c28e83SPiotr Jasiukajtis  *  B = ---  / (x+1)  + y   -  ---  / (x-1)  + y   .
5425c28e83SPiotr Jasiukajtis  *       2 \/                   2 \/
5525c28e83SPiotr Jasiukajtis  *
5625c28e83SPiotr Jasiukajtis  * The Branch cuts are on the real line from -inf to -1 and from 1 to inf.
5725c28e83SPiotr Jasiukajtis  * The real and imaginary parts are based on Abramowitz and Stegun
5825c28e83SPiotr Jasiukajtis  * [Handbook of Mathematic Functions, 1972].  The sign of the imaginary
5925c28e83SPiotr Jasiukajtis  * part is chosen to be the generally considered the principal value of
6025c28e83SPiotr Jasiukajtis  * this function.
6125c28e83SPiotr Jasiukajtis  *
6225c28e83SPiotr Jasiukajtis  * Notes:1. A is the average of the distances from z to the points (1,0)
6325c28e83SPiotr Jasiukajtis  *          and (-1,0) in the complex z-plane, and in particular A>=1.
6425c28e83SPiotr Jasiukajtis  *       2. B is in [-1,1], and A*B = x
6525c28e83SPiotr Jasiukajtis  *
6625c28e83SPiotr Jasiukajtis  * Basic relations
6725c28e83SPiotr Jasiukajtis  *    cacos(conj(z)) = conj(cacos(z))
6825c28e83SPiotr Jasiukajtis  *    cacos(-z)      = pi   - cacos(z)
6925c28e83SPiotr Jasiukajtis  *    cacos( z)      = pi/2 - casin(z)
7025c28e83SPiotr Jasiukajtis  *
7125c28e83SPiotr Jasiukajtis  * Special cases (conform to ISO/IEC 9899:1999(E)):
7225c28e83SPiotr Jasiukajtis  *    cacos(+-0  + i y  ) = pi/2 - i y for y is +-0, +-inf, NaN
7325c28e83SPiotr Jasiukajtis  *    cacos( x   + i inf) = pi/2 - i inf for all x
7425c28e83SPiotr Jasiukajtis  *    cacos( x   + i NaN) = NaN  + i NaN with invalid for non-zero finite x
7525c28e83SPiotr Jasiukajtis  *    cacos(-inf + i y  ) = pi   - i inf for finite +y
7625c28e83SPiotr Jasiukajtis  *    cacos( inf + i y  ) = 0    - i inf for finite +y
7725c28e83SPiotr Jasiukajtis  *    cacos(-inf + i inf) = 3pi/4- i inf
7825c28e83SPiotr Jasiukajtis  *    cacos( inf + i inf) = pi/4 - i inf
7925c28e83SPiotr Jasiukajtis  *    cacos(+-inf+ i NaN) = NaN  - i inf (sign of imaginary is unspecified)
8025c28e83SPiotr Jasiukajtis  *    cacos(NaN  + i y  ) = NaN  + i NaN with invalid for finite y
8125c28e83SPiotr Jasiukajtis  *    cacos(NaN  + i inf) = NaN  - i inf
8225c28e83SPiotr Jasiukajtis  *    cacos(NaN  + i NaN) = NaN  + i NaN
8325c28e83SPiotr Jasiukajtis  *
8425c28e83SPiotr Jasiukajtis  * Special Regions (better formula for accuracy and for avoiding spurious
8525c28e83SPiotr Jasiukajtis  * overflow or underflow) (all x and y are assumed nonnegative):
8625c28e83SPiotr Jasiukajtis  *  case 1: y = 0
8725c28e83SPiotr Jasiukajtis  *  case 2: tiny y relative to x-1: y <= ulp(0.5)*|x-1|
8825c28e83SPiotr Jasiukajtis  *  case 3: tiny y: y < 4 sqrt(u), where u = minimum normal number
8925c28e83SPiotr Jasiukajtis  *  case 4: huge y relative to x+1: y >= (1+x)/ulp(0.5)
9025c28e83SPiotr Jasiukajtis  *  case 5: huge x and y: x and y >= sqrt(M)/8, where M = maximum normal number
9125c28e83SPiotr Jasiukajtis  *  case 6: tiny x: x < 4 sqrt(u)
9225c28e83SPiotr Jasiukajtis  *  --------
9325c28e83SPiotr Jasiukajtis  *  case	1 & 2. y=0 or y/|x-1| is tiny. We have
9425c28e83SPiotr Jasiukajtis  *             ____________              _____________
9525c28e83SPiotr Jasiukajtis  *            /      2    2             /       y    2
9625c28e83SPiotr Jasiukajtis  *           / (x+-1)  + y   =  |x+-1| / 1 + (------)
9725c28e83SPiotr Jasiukajtis  *         \/                        \/       |x+-1|
9825c28e83SPiotr Jasiukajtis  *
9925c28e83SPiotr Jasiukajtis  *                                            1     y    2
10025c28e83SPiotr Jasiukajtis  *                           ~  |x+-1| ( 1 + --- (------)  )
10125c28e83SPiotr Jasiukajtis  *                                            2   |x+-1|
10225c28e83SPiotr Jasiukajtis  *
10325c28e83SPiotr Jasiukajtis  *                                          2
10425c28e83SPiotr Jasiukajtis  *                                         y
10525c28e83SPiotr Jasiukajtis  *                           = |x+-1| + --------.
10625c28e83SPiotr Jasiukajtis  *                                      2|x+-1|
10725c28e83SPiotr Jasiukajtis  *
10825c28e83SPiotr Jasiukajtis  *	Consequently, it is not difficult to see that
10925c28e83SPiotr Jasiukajtis  *                                 2
11025c28e83SPiotr Jasiukajtis  *                                y
11125c28e83SPiotr Jasiukajtis  *                    [ 1 + ------------ ,     if x < 1,
11225c28e83SPiotr Jasiukajtis  *                    [      2(1+x)(1-x)
11325c28e83SPiotr Jasiukajtis  *                    [
11425c28e83SPiotr Jasiukajtis  *                    [
11525c28e83SPiotr Jasiukajtis  *                    [ x,                     if x = 1 (y = 0),
11625c28e83SPiotr Jasiukajtis  *                    [
11725c28e83SPiotr Jasiukajtis  *		A ~=  [             2
11825c28e83SPiotr Jasiukajtis  *                    [        x * y
11925c28e83SPiotr Jasiukajtis  *                    [ x + ------------ ~ x,  if x > 1
12025c28e83SPiotr Jasiukajtis  *                    [      2(x+1)(x-1)
12125c28e83SPiotr Jasiukajtis  *
12225c28e83SPiotr Jasiukajtis  *	and hence
12325c28e83SPiotr Jasiukajtis  *                      ______                                 2
12425c28e83SPiotr Jasiukajtis  *                     / 2                    y               y
12525c28e83SPiotr Jasiukajtis  *               A + \/ A  - 1  ~  1 + ---------------- + -----------, if x < 1,
12625c28e83SPiotr Jasiukajtis  *                                     sqrt((x+1)(1-x))   2(x+1)(1-x)
12725c28e83SPiotr Jasiukajtis  *
12825c28e83SPiotr Jasiukajtis  *
12925c28e83SPiotr Jasiukajtis  *			        ~  x + sqrt((x-1)*(x+1)),             if x >= 1.
13025c28e83SPiotr Jasiukajtis  *
13125c28e83SPiotr Jasiukajtis  *                                         2
13225c28e83SPiotr Jasiukajtis  *                                        y
13325c28e83SPiotr Jasiukajtis  *                          [ x(1 - -----------) ~ x,  if x < 1,
13425c28e83SPiotr Jasiukajtis  *                          [       2(1+x)(1-x)
13525c28e83SPiotr Jasiukajtis  *		B = x/A  ~  [
13625c28e83SPiotr Jasiukajtis  *                          [ 1,                       if x = 1,
13725c28e83SPiotr Jasiukajtis  *			    [
13825c28e83SPiotr Jasiukajtis  *                          [           2
13925c28e83SPiotr Jasiukajtis  *                          [          y
14025c28e83SPiotr Jasiukajtis  *                          [ 1 - ------------ ,       if x > 1,
14125c28e83SPiotr Jasiukajtis  *                          [      2(x+1)(x-1)
14225c28e83SPiotr Jasiukajtis  *	Thus
14325c28e83SPiotr Jasiukajtis  *                            [ acos(x) - i y/sqrt((x-1)*(x+1)),      if x < 1,
14425c28e83SPiotr Jasiukajtis  *                            [
14525c28e83SPiotr Jasiukajtis  *		cacos(x+i*y)~ [ 0 - i 0,                              if x = 1,
14625c28e83SPiotr Jasiukajtis  *                            [
14725c28e83SPiotr Jasiukajtis  *                            [ y/sqrt(x*x-1) - i log(x+sqrt(x*x-1)), if x > 1.
14825c28e83SPiotr Jasiukajtis  *
14925c28e83SPiotr Jasiukajtis  *      Note: y/sqrt(x*x-1) ~ y/x when x >= 2**26.
15025c28e83SPiotr Jasiukajtis  *  case 3. y < 4 sqrt(u), where u = minimum normal x.
15125c28e83SPiotr Jasiukajtis  *	After case 1 and 2, this will only occurs when x=1. When x=1, we have
15225c28e83SPiotr Jasiukajtis  *	   A = (sqrt(4+y*y)+y)/2 ~ 1 + y/2 + y^2/8 + ...
15325c28e83SPiotr Jasiukajtis  *	and
15425c28e83SPiotr Jasiukajtis  *	   B = 1/A = 1 - y/2 + y^2/8 + ...
15525c28e83SPiotr Jasiukajtis  * 	Since
15625c28e83SPiotr Jasiukajtis  *         cos(sqrt(y)) ~ 1 - y/2 + ...
15725c28e83SPiotr Jasiukajtis  *      we have, for the real part,
15825c28e83SPiotr Jasiukajtis  *         acos(B) ~ acos(1 - y/2) ~ sqrt(y)
15925c28e83SPiotr Jasiukajtis  *	For the imaginary part,
16025c28e83SPiotr Jasiukajtis  *	   log(A+sqrt(A*A-1)) ~ log(1+y/2+sqrt(2*y/2))
16125c28e83SPiotr Jasiukajtis  *	                      = log(1+y/2+sqrt(y))
16225c28e83SPiotr Jasiukajtis  *	                      = (y/2+sqrt(y)) - (y/2+sqrt(y))^2/2 + ...
16325c28e83SPiotr Jasiukajtis  *	                      ~ sqrt(y) - y*(sqrt(y)+y/2)/2
16425c28e83SPiotr Jasiukajtis  *	                      ~ sqrt(y)
16525c28e83SPiotr Jasiukajtis  *
16625c28e83SPiotr Jasiukajtis  *  case 4. y >= (x+1)/ulp(0.5). In this case, A ~ y and B ~ x/y. Thus
16725c28e83SPiotr Jasiukajtis  *	   real part = acos(B) ~ pi/2
16825c28e83SPiotr Jasiukajtis  * 	and
16925c28e83SPiotr Jasiukajtis  *	   imag part = log(y+sqrt(y*y-one))
17025c28e83SPiotr Jasiukajtis  *
17125c28e83SPiotr Jasiukajtis  *  case 5. Both x and y are large: x and y > sqrt(M)/8, where M = maximum x
17225c28e83SPiotr Jasiukajtis  *	In this case,
17325c28e83SPiotr Jasiukajtis  *	   A ~ sqrt(x*x+y*y)
17425c28e83SPiotr Jasiukajtis  *	   B ~ x/sqrt(x*x+y*y).
17525c28e83SPiotr Jasiukajtis  *	Thus
17625c28e83SPiotr Jasiukajtis  *	   real part = acos(B) = atan(y/x),
17725c28e83SPiotr Jasiukajtis  *	   imag part = log(A+sqrt(A*A-1)) ~ log(2A)
17825c28e83SPiotr Jasiukajtis  *	             = log(2) + 0.5*log(x*x+y*y)
17925c28e83SPiotr Jasiukajtis  *	             = log(2) + log(y) + 0.5*log(1+(x/y)^2)
18025c28e83SPiotr Jasiukajtis  *
18125c28e83SPiotr Jasiukajtis  *  case 6. x < 4 sqrt(u). In this case, we have
18225c28e83SPiotr Jasiukajtis  *	    A ~ sqrt(1+y*y), B = x/sqrt(1+y*y).
18325c28e83SPiotr Jasiukajtis  *	Since B is tiny, we have
18425c28e83SPiotr Jasiukajtis  *	    real part = acos(B) ~ pi/2
18525c28e83SPiotr Jasiukajtis  *	    imag part = log(A+sqrt(A*A-1)) = log (A+sqrt(y*y))
18625c28e83SPiotr Jasiukajtis  *	              = log(y+sqrt(1+y*y))
18725c28e83SPiotr Jasiukajtis  *	              = 0.5*log(y^2+2ysqrt(1+y^2)+1+y^2)
18825c28e83SPiotr Jasiukajtis  *	              = 0.5*log(1+2y(y+sqrt(1+y^2)));
18925c28e83SPiotr Jasiukajtis  *	              = 0.5*log1p(2y(y+A));
19025c28e83SPiotr Jasiukajtis  *
19125c28e83SPiotr Jasiukajtis  * 	cacos(z) = acos(B) - i sign(y) log (A + sqrt(A*A-1)),
19225c28e83SPiotr Jasiukajtis  */
19325c28e83SPiotr Jasiukajtis /* INDENT ON */
19425c28e83SPiotr Jasiukajtis 
19525c28e83SPiotr Jasiukajtis #include "libm.h"
19625c28e83SPiotr Jasiukajtis #include "complex_wrapper.h"
19725c28e83SPiotr Jasiukajtis 
19825c28e83SPiotr Jasiukajtis /* INDENT OFF */
19925c28e83SPiotr Jasiukajtis static const double
20025c28e83SPiotr Jasiukajtis 	zero = 0.0,
20125c28e83SPiotr Jasiukajtis 	one = 1.0,
20225c28e83SPiotr Jasiukajtis 	E = 1.11022302462515654042e-16,			/* 2**-53 */
20325c28e83SPiotr Jasiukajtis 	ln2 = 6.93147180559945286227e-01,
20425c28e83SPiotr Jasiukajtis 	pi = 3.1415926535897931159979634685,
20525c28e83SPiotr Jasiukajtis 	pi_l = 1.224646799147353177e-16,
20625c28e83SPiotr Jasiukajtis 	pi_2 = 1.570796326794896558e+00,
20725c28e83SPiotr Jasiukajtis 	pi_2_l = 6.123233995736765886e-17,
20825c28e83SPiotr Jasiukajtis 	pi_4 = 0.78539816339744827899949,
20925c28e83SPiotr Jasiukajtis 	pi_4_l = 3.061616997868382943e-17,
21025c28e83SPiotr Jasiukajtis 	pi3_4 = 2.356194490192344836998,
21125c28e83SPiotr Jasiukajtis 	pi3_4_l = 9.184850993605148829195e-17,
21225c28e83SPiotr Jasiukajtis 	Foursqrtu = 5.96667258496016539463e-154,	/* 2**(-509) */
21325c28e83SPiotr Jasiukajtis 	Acrossover = 1.5,
21425c28e83SPiotr Jasiukajtis 	Bcrossover = 0.6417,
21525c28e83SPiotr Jasiukajtis 	half = 0.5;
21625c28e83SPiotr Jasiukajtis /* INDENT ON */
21725c28e83SPiotr Jasiukajtis 
21825c28e83SPiotr Jasiukajtis dcomplex
cacos(dcomplex z)21925c28e83SPiotr Jasiukajtis cacos(dcomplex z) {
22025c28e83SPiotr Jasiukajtis 	double x, y, t, R, S, A, Am1, B, y2, xm1, xp1, Apx;
22125c28e83SPiotr Jasiukajtis 	int ix, iy, hx, hy;
22225c28e83SPiotr Jasiukajtis 	unsigned lx, ly;
22325c28e83SPiotr Jasiukajtis 	dcomplex ans;
22425c28e83SPiotr Jasiukajtis 
22525c28e83SPiotr Jasiukajtis 	x = D_RE(z);
22625c28e83SPiotr Jasiukajtis 	y = D_IM(z);
22725c28e83SPiotr Jasiukajtis 	hx = HI_WORD(x);
22825c28e83SPiotr Jasiukajtis 	lx = LO_WORD(x);
22925c28e83SPiotr Jasiukajtis 	hy = HI_WORD(y);
23025c28e83SPiotr Jasiukajtis 	ly = LO_WORD(y);
23125c28e83SPiotr Jasiukajtis 	ix = hx & 0x7fffffff;
23225c28e83SPiotr Jasiukajtis 	iy = hy & 0x7fffffff;
23325c28e83SPiotr Jasiukajtis 
23425c28e83SPiotr Jasiukajtis 	/* x is 0 */
23525c28e83SPiotr Jasiukajtis 	if ((ix | lx) == 0) {
23625c28e83SPiotr Jasiukajtis 		if (((iy | ly) == 0) || (iy >= 0x7ff00000)) {
23725c28e83SPiotr Jasiukajtis 			D_RE(ans) = pi_2;
23825c28e83SPiotr Jasiukajtis 			D_IM(ans) = -y;
23925c28e83SPiotr Jasiukajtis 			return (ans);
24025c28e83SPiotr Jasiukajtis 		}
24125c28e83SPiotr Jasiukajtis 	}
24225c28e83SPiotr Jasiukajtis 
24325c28e83SPiotr Jasiukajtis 	/* |y| is inf or NaN */
24425c28e83SPiotr Jasiukajtis 	if (iy >= 0x7ff00000) {
24525c28e83SPiotr Jasiukajtis 		if (ISINF(iy, ly)) {	/* cacos(x + i inf) = pi/2  - i inf */
24625c28e83SPiotr Jasiukajtis 			D_IM(ans) = -y;
24725c28e83SPiotr Jasiukajtis 			if (ix < 0x7ff00000) {
24825c28e83SPiotr Jasiukajtis 				D_RE(ans) = pi_2 + pi_2_l;
24925c28e83SPiotr Jasiukajtis 			} else if (ISINF(ix, lx)) {
25025c28e83SPiotr Jasiukajtis 				if (hx >= 0)
25125c28e83SPiotr Jasiukajtis 					D_RE(ans) = pi_4 + pi_4_l;
25225c28e83SPiotr Jasiukajtis 				else
25325c28e83SPiotr Jasiukajtis 					D_RE(ans) = pi3_4 + pi3_4_l;
25425c28e83SPiotr Jasiukajtis 			} else {
25525c28e83SPiotr Jasiukajtis 				D_RE(ans) = x;
25625c28e83SPiotr Jasiukajtis 			}
25725c28e83SPiotr Jasiukajtis 		} else {		/* cacos(x + i NaN) = NaN  + i NaN */
25825c28e83SPiotr Jasiukajtis 			D_RE(ans) = y + x;
25925c28e83SPiotr Jasiukajtis 			if (ISINF(ix, lx))
26025c28e83SPiotr Jasiukajtis 				D_IM(ans) = -fabs(x);
26125c28e83SPiotr Jasiukajtis 			else
26225c28e83SPiotr Jasiukajtis 				D_IM(ans) = y;
26325c28e83SPiotr Jasiukajtis 		}
26425c28e83SPiotr Jasiukajtis 		return (ans);
26525c28e83SPiotr Jasiukajtis 	}
26625c28e83SPiotr Jasiukajtis 
26725c28e83SPiotr Jasiukajtis 	x = fabs(x);
26825c28e83SPiotr Jasiukajtis 	y = fabs(y);
26925c28e83SPiotr Jasiukajtis 
27025c28e83SPiotr Jasiukajtis 	/* x is inf or NaN */
27125c28e83SPiotr Jasiukajtis 	if (ix >= 0x7ff00000) {	/* x is inf or NaN */
27225c28e83SPiotr Jasiukajtis 		if (ISINF(ix, lx)) {	/* x is INF */
27325c28e83SPiotr Jasiukajtis 			D_IM(ans) = -x;
27425c28e83SPiotr Jasiukajtis 			if (iy >= 0x7ff00000) {
27525c28e83SPiotr Jasiukajtis 				if (ISINF(iy, ly)) {
27625c28e83SPiotr Jasiukajtis 					/* INDENT OFF */
27725c28e83SPiotr Jasiukajtis 					/* cacos(inf + i inf) = pi/4 - i inf */
27825c28e83SPiotr Jasiukajtis 					/* cacos(-inf+ i inf) =3pi/4 - i inf */
27925c28e83SPiotr Jasiukajtis 					/* INDENT ON */
28025c28e83SPiotr Jasiukajtis 					if (hx >= 0)
28125c28e83SPiotr Jasiukajtis 						D_RE(ans) = pi_4 + pi_4_l;
28225c28e83SPiotr Jasiukajtis 					else
28325c28e83SPiotr Jasiukajtis 						D_RE(ans) = pi3_4 + pi3_4_l;
28425c28e83SPiotr Jasiukajtis 				} else
28525c28e83SPiotr Jasiukajtis 					/* INDENT OFF */
28625c28e83SPiotr Jasiukajtis 					/* cacos(inf + i NaN) = NaN  - i inf  */
28725c28e83SPiotr Jasiukajtis 					/* INDENT ON */
28825c28e83SPiotr Jasiukajtis 					D_RE(ans) = y + y;
28925c28e83SPiotr Jasiukajtis 			} else
29025c28e83SPiotr Jasiukajtis 				/* INDENT OFF */
29125c28e83SPiotr Jasiukajtis 				/* cacos(inf + iy ) = 0  - i inf */
29225c28e83SPiotr Jasiukajtis 				/* cacos(-inf+ iy  ) = pi - i inf */
29325c28e83SPiotr Jasiukajtis 				/* INDENT ON */
29425c28e83SPiotr Jasiukajtis 			if (hx >= 0)
29525c28e83SPiotr Jasiukajtis 				D_RE(ans) = zero;
29625c28e83SPiotr Jasiukajtis 			else
29725c28e83SPiotr Jasiukajtis 				D_RE(ans) = pi + pi_l;
29825c28e83SPiotr Jasiukajtis 		} else {		/* x is NaN */
29925c28e83SPiotr Jasiukajtis 			/* INDENT OFF */
30025c28e83SPiotr Jasiukajtis 			/*
30125c28e83SPiotr Jasiukajtis 			 * cacos(NaN + i inf) = NaN  - i inf
30225c28e83SPiotr Jasiukajtis 			 * cacos(NaN + i y  ) = NaN  + i NaN
30325c28e83SPiotr Jasiukajtis 			 * cacos(NaN + i NaN) = NaN  + i NaN
30425c28e83SPiotr Jasiukajtis 			 */
30525c28e83SPiotr Jasiukajtis 			/* INDENT ON */
30625c28e83SPiotr Jasiukajtis 			D_RE(ans) = x + y;
30725c28e83SPiotr Jasiukajtis 			if (iy >= 0x7ff00000) {
30825c28e83SPiotr Jasiukajtis 				D_IM(ans) = -y;
30925c28e83SPiotr Jasiukajtis 			} else {
31025c28e83SPiotr Jasiukajtis 				D_IM(ans) = x;
31125c28e83SPiotr Jasiukajtis 			}
31225c28e83SPiotr Jasiukajtis 		}
31325c28e83SPiotr Jasiukajtis 		if (hy < 0)
31425c28e83SPiotr Jasiukajtis 			D_IM(ans) = -D_IM(ans);
31525c28e83SPiotr Jasiukajtis 		return (ans);
31625c28e83SPiotr Jasiukajtis 	}
31725c28e83SPiotr Jasiukajtis 
31825c28e83SPiotr Jasiukajtis 	if ((iy | ly) == 0) {	/* region 1: y=0 */
31925c28e83SPiotr Jasiukajtis 		if (ix < 0x3ff00000) {	/* |x| < 1 */
32025c28e83SPiotr Jasiukajtis 			D_RE(ans) = acos(x);
32125c28e83SPiotr Jasiukajtis 			D_IM(ans) = zero;
32225c28e83SPiotr Jasiukajtis 		} else {
32325c28e83SPiotr Jasiukajtis 			D_RE(ans) = zero;
32425c28e83SPiotr Jasiukajtis 			if (ix >= 0x43500000)	/* |x| >= 2**54 */
32525c28e83SPiotr Jasiukajtis 				D_IM(ans) = ln2 + log(x);
32625c28e83SPiotr Jasiukajtis 			else if (ix >= 0x3ff80000)	/* x > Acrossover */
32725c28e83SPiotr Jasiukajtis 				D_IM(ans) = log(x + sqrt((x - one) * (x +
32825c28e83SPiotr Jasiukajtis 					one)));
32925c28e83SPiotr Jasiukajtis 			else {
33025c28e83SPiotr Jasiukajtis 				xm1 = x - one;
33125c28e83SPiotr Jasiukajtis 				D_IM(ans) = log1p(xm1 + sqrt(xm1 * (x + one)));
33225c28e83SPiotr Jasiukajtis 			}
33325c28e83SPiotr Jasiukajtis 		}
33425c28e83SPiotr Jasiukajtis 	} else if (y <= E * fabs(x - one)) {	/* region 2: y < tiny*|x-1| */
33525c28e83SPiotr Jasiukajtis 		if (ix < 0x3ff00000) {	/* x < 1 */
33625c28e83SPiotr Jasiukajtis 			D_RE(ans) = acos(x);
33725c28e83SPiotr Jasiukajtis 			D_IM(ans) = y / sqrt((one + x) * (one - x));
33825c28e83SPiotr Jasiukajtis 		} else if (ix >= 0x43500000) {	/* |x| >= 2**54 */
33925c28e83SPiotr Jasiukajtis 			D_RE(ans) = y / x;
34025c28e83SPiotr Jasiukajtis 			D_IM(ans) = ln2 + log(x);
34125c28e83SPiotr Jasiukajtis 		} else {
34225c28e83SPiotr Jasiukajtis 			t = sqrt((x - one) * (x + one));
34325c28e83SPiotr Jasiukajtis 			D_RE(ans) = y / t;
34425c28e83SPiotr Jasiukajtis 			if (ix >= 0x3ff80000)	/* x > Acrossover */
34525c28e83SPiotr Jasiukajtis 				D_IM(ans) = log(x + t);
34625c28e83SPiotr Jasiukajtis 			else
34725c28e83SPiotr Jasiukajtis 				D_IM(ans) = log1p((x - one) + t);
34825c28e83SPiotr Jasiukajtis 		}
34925c28e83SPiotr Jasiukajtis 	} else if (y < Foursqrtu) {	/* region 3 */
35025c28e83SPiotr Jasiukajtis 		t = sqrt(y);
35125c28e83SPiotr Jasiukajtis 		D_RE(ans) = t;
35225c28e83SPiotr Jasiukajtis 		D_IM(ans) = t;
35325c28e83SPiotr Jasiukajtis 	} else if (E * y - one >= x) {	/* region 4 */
35425c28e83SPiotr Jasiukajtis 		D_RE(ans) = pi_2;
35525c28e83SPiotr Jasiukajtis 		D_IM(ans) = ln2 + log(y);
35625c28e83SPiotr Jasiukajtis 	} else if (ix >= 0x5fc00000 || iy >= 0x5fc00000) {	/* x,y>2**509 */
35725c28e83SPiotr Jasiukajtis 		/* region 5: x+1 or y is very large (>= sqrt(max)/8) */
35825c28e83SPiotr Jasiukajtis 		t = x / y;
35925c28e83SPiotr Jasiukajtis 		D_RE(ans) = atan(y / x);
36025c28e83SPiotr Jasiukajtis 		D_IM(ans) = ln2 + log(y) + half * log1p(t * t);
36125c28e83SPiotr Jasiukajtis 	} else if (x < Foursqrtu) {
36225c28e83SPiotr Jasiukajtis 		/* region 6: x is very small, < 4sqrt(min) */
36325c28e83SPiotr Jasiukajtis 		D_RE(ans) = pi_2;
36425c28e83SPiotr Jasiukajtis 		A = sqrt(one + y * y);
36525c28e83SPiotr Jasiukajtis 		if (iy >= 0x3ff80000)	/* if y > Acrossover */
36625c28e83SPiotr Jasiukajtis 			D_IM(ans) = log(y + A);
36725c28e83SPiotr Jasiukajtis 		else
36825c28e83SPiotr Jasiukajtis 			D_IM(ans) = half * log1p((y + y) * (y + A));
36925c28e83SPiotr Jasiukajtis 	} else {	/* safe region */
37025c28e83SPiotr Jasiukajtis 		y2 = y * y;
37125c28e83SPiotr Jasiukajtis 		xp1 = x + one;
37225c28e83SPiotr Jasiukajtis 		xm1 = x - one;
37325c28e83SPiotr Jasiukajtis 		R = sqrt(xp1 * xp1 + y2);
37425c28e83SPiotr Jasiukajtis 		S = sqrt(xm1 * xm1 + y2);
37525c28e83SPiotr Jasiukajtis 		A = half * (R + S);
37625c28e83SPiotr Jasiukajtis 		B = x / A;
37725c28e83SPiotr Jasiukajtis 		if (B <= Bcrossover)
37825c28e83SPiotr Jasiukajtis 			D_RE(ans) = acos(B);
37925c28e83SPiotr Jasiukajtis 		else {		/* use atan and an accurate approx to a-x */
38025c28e83SPiotr Jasiukajtis 			Apx = A + x;
38125c28e83SPiotr Jasiukajtis 			if (x <= one)
38225c28e83SPiotr Jasiukajtis 				D_RE(ans) = atan(sqrt(half * Apx * (y2 / (R +
38325c28e83SPiotr Jasiukajtis 					xp1) + (S - xm1))) / x);
38425c28e83SPiotr Jasiukajtis 			else
38525c28e83SPiotr Jasiukajtis 				D_RE(ans) = atan((y * sqrt(half * (Apx / (R +
38625c28e83SPiotr Jasiukajtis 					xp1) + Apx / (S + xm1)))) / x);
38725c28e83SPiotr Jasiukajtis 		}
38825c28e83SPiotr Jasiukajtis 		if (A <= Acrossover) {
38925c28e83SPiotr Jasiukajtis 			/* use log1p and an accurate approx to A-1 */
39025c28e83SPiotr Jasiukajtis 			if (x < one)
39125c28e83SPiotr Jasiukajtis 				Am1 = half * (y2 / (R + xp1) + y2 / (S - xm1));
39225c28e83SPiotr Jasiukajtis 			else
39325c28e83SPiotr Jasiukajtis 				Am1 = half * (y2 / (R + xp1) + (S + xm1));
39425c28e83SPiotr Jasiukajtis 			D_IM(ans) = log1p(Am1 + sqrt(Am1 * (A + one)));
39525c28e83SPiotr Jasiukajtis 		} else {
39625c28e83SPiotr Jasiukajtis 			D_IM(ans) = log(A + sqrt(A * A - one));
39725c28e83SPiotr Jasiukajtis 		}
39825c28e83SPiotr Jasiukajtis 	}
39925c28e83SPiotr Jasiukajtis 	if (hx < 0)
40025c28e83SPiotr Jasiukajtis 		D_RE(ans) = pi - D_RE(ans);
40125c28e83SPiotr Jasiukajtis 	if (hy >= 0)
40225c28e83SPiotr Jasiukajtis 		D_IM(ans) = -D_IM(ans);
40325c28e83SPiotr Jasiukajtis 	return (ans);
40425c28e83SPiotr Jasiukajtis }
405