xref: /titanic_44/usr/src/lib/libm/common/complex/casin.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 __casin = casin
3125c28e83SPiotr Jasiukajtis 
3225c28e83SPiotr Jasiukajtis /* INDENT OFF */
3325c28e83SPiotr Jasiukajtis /*
3425c28e83SPiotr Jasiukajtis  * dcomplex casin(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 sine function casin(z),
4225c28e83SPiotr Jasiukajtis  * where z = x+iy, can be defined by
4325c28e83SPiotr Jasiukajtis  *
4425c28e83SPiotr Jasiukajtis  * 	casin(z) = asin(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  * Special notes: if casin( x, y) = ( u, v), then
6725c28e83SPiotr Jasiukajtis  *		    casin(-x, y) = (-u, v),
6825c28e83SPiotr Jasiukajtis  *		    casin( x,-y) = ( u,-v),
6925c28e83SPiotr Jasiukajtis  *    in general, we have casin(conj(z))     =  conj(casin(z))
7025c28e83SPiotr Jasiukajtis  *                       casin(-z)          = -casin(z)
7125c28e83SPiotr Jasiukajtis  *			 casin(z)           =  pi/2 - cacos(z)
7225c28e83SPiotr Jasiukajtis  *
7325c28e83SPiotr Jasiukajtis  * EXCEPTION CASES (conform to ISO/IEC 9899:1999(E)):
7425c28e83SPiotr Jasiukajtis  *    casin( 0 + i 0   ) =  0    + i 0
7525c28e83SPiotr Jasiukajtis  *    casin( 0 + i NaN ) =  0    + i NaN
7625c28e83SPiotr Jasiukajtis  *    casin( x + i inf ) =  0    + i inf for finite x
7725c28e83SPiotr Jasiukajtis  *    casin( x + i NaN ) =  NaN  + i NaN with invalid for finite x != 0
7825c28e83SPiotr Jasiukajtis  *    casin(inf + iy   ) =  pi/2 + i inf finite y
7925c28e83SPiotr Jasiukajtis  *    casin(inf + i inf) =  pi/4 + i inf
8025c28e83SPiotr Jasiukajtis  *    casin(inf + i NaN) =  NaN  + i inf
8125c28e83SPiotr Jasiukajtis  *    casin(NaN + i y  ) =  NaN  + i NaN for finite y
8225c28e83SPiotr Jasiukajtis  *    casin(NaN + i inf) =  NaN  + i inf
8325c28e83SPiotr Jasiukajtis  *    casin(NaN + i NaN) =  NaN  + i NaN
8425c28e83SPiotr Jasiukajtis  *
8525c28e83SPiotr Jasiukajtis  * Special Regions (better formula for accuracy and for avoiding spurious
8625c28e83SPiotr Jasiukajtis  * overflow or underflow) (all x and y are assumed nonnegative):
8725c28e83SPiotr Jasiukajtis  *  case 1: y = 0
8825c28e83SPiotr Jasiukajtis  *  case 2: tiny y relative to x-1: y <= ulp(0.5)*|x-1|
8925c28e83SPiotr Jasiukajtis  *  case 3: tiny y: y < 4 sqrt(u), where u = minimum normal number
9025c28e83SPiotr Jasiukajtis  *  case 4: huge y relative to x+1: y >= (1+x)/ulp(0.5)
9125c28e83SPiotr Jasiukajtis  *  case 5: huge x and y: x and y >= sqrt(M)/8, where M = maximum normal number
9225c28e83SPiotr Jasiukajtis  *  case 6: tiny x: x < 4 sqrt(u)
9325c28e83SPiotr Jasiukajtis  *  --------
9425c28e83SPiotr Jasiukajtis  *  case	1 & 2. y=0 or y/|x-1| is tiny. We have
9525c28e83SPiotr Jasiukajtis  *             ____________              _____________
9625c28e83SPiotr Jasiukajtis  *            /      2    2             /       y    2
9725c28e83SPiotr Jasiukajtis  *           / (x+-1)  + y   =  |x+-1| / 1 + (------)
9825c28e83SPiotr Jasiukajtis  *         \/                        \/       |x+-1|
9925c28e83SPiotr Jasiukajtis  *
10025c28e83SPiotr Jasiukajtis  *                                            1      y   2
10125c28e83SPiotr Jasiukajtis  *                           ~  |x+-1| ( 1 + --- (------)  )
10225c28e83SPiotr Jasiukajtis  *                                            2   |x+-1|
10325c28e83SPiotr Jasiukajtis  *
10425c28e83SPiotr Jasiukajtis  *                                           2
10525c28e83SPiotr Jasiukajtis  *                                          y
10625c28e83SPiotr Jasiukajtis  *                           =  |x+-1| + --------.
10725c28e83SPiotr Jasiukajtis  *                                       2|x+-1|
10825c28e83SPiotr Jasiukajtis  *
10925c28e83SPiotr Jasiukajtis  *	Consequently, it is not difficult to see that
11025c28e83SPiotr Jasiukajtis  *                                 2
11125c28e83SPiotr Jasiukajtis  *                                y
11225c28e83SPiotr Jasiukajtis  *                    [ 1 + ------------ ,  if x < 1,
11325c28e83SPiotr Jasiukajtis  *                    [      2(1+x)(1-x)
11425c28e83SPiotr Jasiukajtis  *                    [
11525c28e83SPiotr Jasiukajtis  *                    [
11625c28e83SPiotr Jasiukajtis  *                    [ x,                 if x = 1 (y = 0),
11725c28e83SPiotr Jasiukajtis  *                    [
11825c28e83SPiotr Jasiukajtis  *		A ~=  [             2
11925c28e83SPiotr Jasiukajtis  *                    [        x * y
12025c28e83SPiotr Jasiukajtis  *                    [ x + ------------ ,  if x > 1
12125c28e83SPiotr Jasiukajtis  *                    [      2(1+x)(x-1)
12225c28e83SPiotr Jasiukajtis  *
12325c28e83SPiotr Jasiukajtis  *	and hence
12425c28e83SPiotr Jasiukajtis  *                      ______                                 2
12525c28e83SPiotr Jasiukajtis  *                     / 2                    y               y
12625c28e83SPiotr Jasiukajtis  *               A + \/ A  - 1  ~  1 + ---------------- + -----------, if x < 1,
12725c28e83SPiotr Jasiukajtis  *                                     sqrt((x+1)(1-x))   2(x+1)(1-x)
12825c28e83SPiotr Jasiukajtis  *
12925c28e83SPiotr Jasiukajtis  *
13025c28e83SPiotr Jasiukajtis  *			       ~  x + sqrt((x-1)*(x+1)),              if x >= 1.
13125c28e83SPiotr Jasiukajtis  *
13225c28e83SPiotr Jasiukajtis  *                                         2
13325c28e83SPiotr Jasiukajtis  *                                        y
13425c28e83SPiotr Jasiukajtis  *                          [ x(1 - ------------), if x < 1,
13525c28e83SPiotr Jasiukajtis  *                          [       2(1+x)(1-x)
13625c28e83SPiotr Jasiukajtis  *		B = x/A  ~  [
13725c28e83SPiotr Jasiukajtis  *                          [ 1,                  if x = 1,
13825c28e83SPiotr Jasiukajtis  *			    [
13925c28e83SPiotr Jasiukajtis  *                          [           2
14025c28e83SPiotr Jasiukajtis  *                          [          y
14125c28e83SPiotr Jasiukajtis  *                          [ 1 - ------------ ,   if x > 1,
14225c28e83SPiotr Jasiukajtis  *                          [      2(1+x)(1-x)
14325c28e83SPiotr Jasiukajtis  *	Thus
14425c28e83SPiotr Jasiukajtis  *                            [ asin(x) + i y/sqrt((x-1)*(x+1)), if x <  1
14525c28e83SPiotr Jasiukajtis  *		casin(x+i*y)=[
14625c28e83SPiotr Jasiukajtis  *                            [ pi/2    + i log(x+sqrt(x*x-1)),  if x >= 1
14725c28e83SPiotr Jasiukajtis  *
14825c28e83SPiotr Jasiukajtis  *  case 3. y < 4 sqrt(u), where u = minimum normal x.
14925c28e83SPiotr Jasiukajtis  *	After case 1 and 2, this will only occurs when x=1. When x=1, we have
15025c28e83SPiotr Jasiukajtis  *	   A = (sqrt(4+y*y)+y)/2 ~ 1 + y/2 + y^2/8 + ...
15125c28e83SPiotr Jasiukajtis  *	and
15225c28e83SPiotr Jasiukajtis  *	   B = 1/A = 1 - y/2 + y^2/8 + ...
15325c28e83SPiotr Jasiukajtis  * 	Since
15425c28e83SPiotr Jasiukajtis  *	   asin(x) = pi/2-2*asin(sqrt((1-x)/2))
15525c28e83SPiotr Jasiukajtis  *	   asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
15625c28e83SPiotr Jasiukajtis  *	we have, for the real part asin(B),
15725c28e83SPiotr Jasiukajtis  *	   asin(1-y/2) ~ pi/2 - 2 asin(sqrt(y/4))
15825c28e83SPiotr Jasiukajtis  *	               ~ pi/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 = asin(B) ~ x/y (be careful, x/y may underflow)
16825c28e83SPiotr Jasiukajtis  * 	and
16925c28e83SPiotr Jasiukajtis  *	   imag part = log(y+sqrt(y*y-one))
17025c28e83SPiotr Jasiukajtis  *
17125c28e83SPiotr Jasiukajtis  *
17225c28e83SPiotr Jasiukajtis  *  case 5. Both x and y are large: x and y > sqrt(M)/8, where M = maximum x
17325c28e83SPiotr Jasiukajtis  *	In this case,
17425c28e83SPiotr Jasiukajtis  *	   A ~ sqrt(x*x+y*y)
17525c28e83SPiotr Jasiukajtis  *	   B ~ x/sqrt(x*x+y*y).
17625c28e83SPiotr Jasiukajtis  *	Thus
17725c28e83SPiotr Jasiukajtis  *	   real part = asin(B) = atan(x/y),
17825c28e83SPiotr Jasiukajtis  *	   imag part = log(A+sqrt(A*A-1)) ~ log(2A)
17925c28e83SPiotr Jasiukajtis  *	             = log(2) + 0.5*log(x*x+y*y)
18025c28e83SPiotr Jasiukajtis  *	             = log(2) + log(y) + 0.5*log(1+(x/y)^2)
18125c28e83SPiotr Jasiukajtis  *
18225c28e83SPiotr Jasiukajtis  *  case 6. x < 4 sqrt(u). In this case, we have
18325c28e83SPiotr Jasiukajtis  *	    A ~ sqrt(1+y*y), B = x/sqrt(1+y*y).
18425c28e83SPiotr Jasiukajtis  *	Since B is tiny, we have
18525c28e83SPiotr Jasiukajtis  *	    real part = asin(B) ~ B = x/sqrt(1+y*y)
18625c28e83SPiotr Jasiukajtis  *	    imag part = log(A+sqrt(A*A-1)) = log (A+sqrt(y*y))
18725c28e83SPiotr Jasiukajtis  *	              = log(y+sqrt(1+y*y))
18825c28e83SPiotr Jasiukajtis  *	              = 0.5*log(y^2+2ysqrt(1+y^2)+1+y^2)
18925c28e83SPiotr Jasiukajtis  *	              = 0.5*log(1+2y(y+sqrt(1+y^2)));
19025c28e83SPiotr Jasiukajtis  *	              = 0.5*log1p(2y(y+A));
19125c28e83SPiotr Jasiukajtis  *
19225c28e83SPiotr Jasiukajtis  * 	casin(z) = asin(B) + i sign(y) log (A + sqrt(A*A-1)),
19325c28e83SPiotr Jasiukajtis  */
19425c28e83SPiotr Jasiukajtis /* INDENT ON */
19525c28e83SPiotr Jasiukajtis 
19625c28e83SPiotr Jasiukajtis #include "libm.h"		/* asin/atan/fabs/log/log1p/sqrt */
19725c28e83SPiotr Jasiukajtis #include "complex_wrapper.h"
19825c28e83SPiotr Jasiukajtis 
19925c28e83SPiotr Jasiukajtis /* INDENT OFF */
20025c28e83SPiotr Jasiukajtis static const double
20125c28e83SPiotr Jasiukajtis 	zero = 0.0,
20225c28e83SPiotr Jasiukajtis 	one = 1.0,
20325c28e83SPiotr Jasiukajtis 	E = 1.11022302462515654042e-16,			/* 2**-53 */
20425c28e83SPiotr Jasiukajtis 	ln2 = 6.93147180559945286227e-01,
20525c28e83SPiotr Jasiukajtis 	pi_2 = 1.570796326794896558e+00,
20625c28e83SPiotr Jasiukajtis 	pi_2_l = 6.123233995736765886e-17,
20725c28e83SPiotr Jasiukajtis 	pi_4 = 7.85398163397448278999e-01,
20825c28e83SPiotr Jasiukajtis 	Foursqrtu = 5.96667258496016539463e-154,	/* 2**(-509) */
20925c28e83SPiotr Jasiukajtis 	Acrossover = 1.5,
21025c28e83SPiotr Jasiukajtis 	Bcrossover = 0.6417,
21125c28e83SPiotr Jasiukajtis 	half = 0.5;
21225c28e83SPiotr Jasiukajtis /* INDENT ON */
21325c28e83SPiotr Jasiukajtis 
21425c28e83SPiotr Jasiukajtis dcomplex
casin(dcomplex z)21525c28e83SPiotr Jasiukajtis casin(dcomplex z) {
21625c28e83SPiotr Jasiukajtis 	double x, y, t, R, S, A, Am1, B, y2, xm1, xp1, Apx;
21725c28e83SPiotr Jasiukajtis 	int ix, iy, hx, hy;
21825c28e83SPiotr Jasiukajtis 	unsigned lx, ly;
21925c28e83SPiotr Jasiukajtis 	dcomplex ans;
22025c28e83SPiotr Jasiukajtis 
22125c28e83SPiotr Jasiukajtis 	x = D_RE(z);
22225c28e83SPiotr Jasiukajtis 	y = D_IM(z);
22325c28e83SPiotr Jasiukajtis 	hx = HI_WORD(x);
22425c28e83SPiotr Jasiukajtis 	lx = LO_WORD(x);
22525c28e83SPiotr Jasiukajtis 	hy = HI_WORD(y);
22625c28e83SPiotr Jasiukajtis 	ly = LO_WORD(y);
22725c28e83SPiotr Jasiukajtis 	ix = hx & 0x7fffffff;
22825c28e83SPiotr Jasiukajtis 	iy = hy & 0x7fffffff;
22925c28e83SPiotr Jasiukajtis 	x = fabs(x);
23025c28e83SPiotr Jasiukajtis 	y = fabs(y);
23125c28e83SPiotr Jasiukajtis 
23225c28e83SPiotr Jasiukajtis 	/* special cases */
23325c28e83SPiotr Jasiukajtis 
23425c28e83SPiotr Jasiukajtis 	/* x is inf or NaN */
23525c28e83SPiotr Jasiukajtis 	if (ix >= 0x7ff00000) {	/* x is inf or NaN */
23625c28e83SPiotr Jasiukajtis 		if (ISINF(ix, lx)) {	/* x is INF */
23725c28e83SPiotr Jasiukajtis 			D_IM(ans) = x;
23825c28e83SPiotr Jasiukajtis 			if (iy >= 0x7ff00000) {
23925c28e83SPiotr Jasiukajtis 				if (ISINF(iy, ly))
24025c28e83SPiotr Jasiukajtis 					/* casin(inf + i inf) = pi/4 + i inf */
24125c28e83SPiotr Jasiukajtis 					D_RE(ans) = pi_4;
24225c28e83SPiotr Jasiukajtis 				else	/* casin(inf + i NaN) = NaN  + i inf  */
24325c28e83SPiotr Jasiukajtis 					D_RE(ans) = y + y;
24425c28e83SPiotr Jasiukajtis 			} else	/* casin(inf + iy) = pi/2 + i inf */
24525c28e83SPiotr Jasiukajtis 				D_RE(ans) = pi_2;
24625c28e83SPiotr Jasiukajtis 		} else {		/* x is NaN */
24725c28e83SPiotr Jasiukajtis 			if (iy >= 0x7ff00000) {
24825c28e83SPiotr Jasiukajtis 				/* INDENT OFF */
24925c28e83SPiotr Jasiukajtis 				/*
25025c28e83SPiotr Jasiukajtis 				 * casin(NaN + i inf) = NaN + i inf
25125c28e83SPiotr Jasiukajtis 				 * casin(NaN + i NaN) = NaN + i NaN
25225c28e83SPiotr Jasiukajtis 				 */
25325c28e83SPiotr Jasiukajtis 				/* INDENT ON */
25425c28e83SPiotr Jasiukajtis 				D_IM(ans) = y + y;
25525c28e83SPiotr Jasiukajtis 				D_RE(ans) = x + x;
25625c28e83SPiotr Jasiukajtis 			} else {
25725c28e83SPiotr Jasiukajtis 				/* casin(NaN + i y ) = NaN  + i NaN */
25825c28e83SPiotr Jasiukajtis 				D_IM(ans) = D_RE(ans) = x + y;
25925c28e83SPiotr Jasiukajtis 			}
26025c28e83SPiotr Jasiukajtis 		}
26125c28e83SPiotr Jasiukajtis 		if (hx < 0)
26225c28e83SPiotr Jasiukajtis 			D_RE(ans) = -D_RE(ans);
26325c28e83SPiotr Jasiukajtis 		if (hy < 0)
26425c28e83SPiotr Jasiukajtis 			D_IM(ans) = -D_IM(ans);
26525c28e83SPiotr Jasiukajtis 		return (ans);
26625c28e83SPiotr Jasiukajtis 	}
26725c28e83SPiotr Jasiukajtis 
26825c28e83SPiotr Jasiukajtis 	/* casin(+0 + i 0  ) =  0   + i 0. */
26925c28e83SPiotr Jasiukajtis 	if ((ix | lx | iy | ly) == 0)
27025c28e83SPiotr Jasiukajtis 		return (z);
27125c28e83SPiotr Jasiukajtis 
27225c28e83SPiotr Jasiukajtis 	if (iy >= 0x7ff00000) {	/* y is inf or NaN */
27325c28e83SPiotr Jasiukajtis 		if (ISINF(iy, ly)) {	/* casin(x + i inf) =  0   + i inf */
27425c28e83SPiotr Jasiukajtis 			D_IM(ans) = y;
27525c28e83SPiotr Jasiukajtis 			D_RE(ans) = zero;
27625c28e83SPiotr Jasiukajtis 		} else {		/* casin(x + i NaN) = NaN  + i NaN */
27725c28e83SPiotr Jasiukajtis 			D_IM(ans) = x + y;
27825c28e83SPiotr Jasiukajtis 			if ((ix | lx) == 0)
27925c28e83SPiotr Jasiukajtis 				D_RE(ans) = x;
28025c28e83SPiotr Jasiukajtis 			else
28125c28e83SPiotr Jasiukajtis 				D_RE(ans) = y;
28225c28e83SPiotr Jasiukajtis 		}
28325c28e83SPiotr Jasiukajtis 		if (hx < 0)
28425c28e83SPiotr Jasiukajtis 			D_RE(ans) = -D_RE(ans);
28525c28e83SPiotr Jasiukajtis 		if (hy < 0)
28625c28e83SPiotr Jasiukajtis 			D_IM(ans) = -D_IM(ans);
28725c28e83SPiotr Jasiukajtis 		return (ans);
28825c28e83SPiotr Jasiukajtis 	}
28925c28e83SPiotr Jasiukajtis 
29025c28e83SPiotr Jasiukajtis 	if ((iy | ly) == 0) {	/* region 1: y=0 */
29125c28e83SPiotr Jasiukajtis 		if (ix < 0x3ff00000) {	/* |x| < 1 */
29225c28e83SPiotr Jasiukajtis 			D_RE(ans) = asin(x);
29325c28e83SPiotr Jasiukajtis 			D_IM(ans) = zero;
29425c28e83SPiotr Jasiukajtis 		} else {
29525c28e83SPiotr Jasiukajtis 			D_RE(ans) = pi_2;
29625c28e83SPiotr Jasiukajtis 			if (ix >= 0x43500000)	/* |x| >= 2**54 */
29725c28e83SPiotr Jasiukajtis 				D_IM(ans) = ln2 + log(x);
29825c28e83SPiotr Jasiukajtis 			else if (ix >= 0x3ff80000)	/* x > Acrossover */
29925c28e83SPiotr Jasiukajtis 				D_IM(ans) = log(x + sqrt((x - one) * (x +
30025c28e83SPiotr Jasiukajtis 					one)));
30125c28e83SPiotr Jasiukajtis 			else {
30225c28e83SPiotr Jasiukajtis 				xm1 = x - one;
30325c28e83SPiotr Jasiukajtis 				D_IM(ans) = log1p(xm1 + sqrt(xm1 * (x + one)));
30425c28e83SPiotr Jasiukajtis 			}
30525c28e83SPiotr Jasiukajtis 		}
30625c28e83SPiotr Jasiukajtis 	} else if (y <= E * fabs(x - one)) {	/* region 2: y < tiny*|x-1| */
30725c28e83SPiotr Jasiukajtis 		if (ix < 0x3ff00000) {	/* x < 1 */
30825c28e83SPiotr Jasiukajtis 			D_RE(ans) = asin(x);
30925c28e83SPiotr Jasiukajtis 			D_IM(ans) = y / sqrt((one + x) * (one - x));
31025c28e83SPiotr Jasiukajtis 		} else {
31125c28e83SPiotr Jasiukajtis 			D_RE(ans) = pi_2;
31225c28e83SPiotr Jasiukajtis 			if (ix >= 0x43500000) {	/* |x| >= 2**54 */
31325c28e83SPiotr Jasiukajtis 				D_IM(ans) = ln2 + log(x);
31425c28e83SPiotr Jasiukajtis 			} else if (ix >= 0x3ff80000)	/* x > Acrossover */
31525c28e83SPiotr Jasiukajtis 				D_IM(ans) = log(x + sqrt((x - one) * (x +
31625c28e83SPiotr Jasiukajtis 					one)));
31725c28e83SPiotr Jasiukajtis 			else
31825c28e83SPiotr Jasiukajtis 				D_IM(ans) = log1p((x - one) + sqrt((x - one) *
31925c28e83SPiotr Jasiukajtis 					(x + one)));
32025c28e83SPiotr Jasiukajtis 		}
32125c28e83SPiotr Jasiukajtis 	} else if (y < Foursqrtu) {	/* region 3 */
32225c28e83SPiotr Jasiukajtis 		t = sqrt(y);
32325c28e83SPiotr Jasiukajtis 		D_RE(ans) = pi_2 - (t - pi_2_l);
32425c28e83SPiotr Jasiukajtis 		D_IM(ans) = t;
32525c28e83SPiotr Jasiukajtis 	} else if (E * y - one >= x) {	/* region 4 */
32625c28e83SPiotr Jasiukajtis 		D_RE(ans) = x / y;	/* need to fix underflow cases */
32725c28e83SPiotr Jasiukajtis 		D_IM(ans) = ln2 + log(y);
32825c28e83SPiotr Jasiukajtis 	} else if (ix >= 0x5fc00000 || iy >= 0x5fc00000) {	/* x,y>2**509 */
32925c28e83SPiotr Jasiukajtis 		/* region 5: x+1 or y is very large (>= sqrt(max)/8) */
33025c28e83SPiotr Jasiukajtis 		t = x / y;
33125c28e83SPiotr Jasiukajtis 		D_RE(ans) = atan(t);
33225c28e83SPiotr Jasiukajtis 		D_IM(ans) = ln2 + log(y) + half * log1p(t * t);
33325c28e83SPiotr Jasiukajtis 	} else if (x < Foursqrtu) {
33425c28e83SPiotr Jasiukajtis 		/* region 6: x is very small, < 4sqrt(min) */
33525c28e83SPiotr Jasiukajtis 		A = sqrt(one + y * y);
33625c28e83SPiotr Jasiukajtis 		D_RE(ans) = x / A;	/* may underflow */
33725c28e83SPiotr Jasiukajtis 		if (iy >= 0x3ff80000)	/* if y > Acrossover */
33825c28e83SPiotr Jasiukajtis 			D_IM(ans) = log(y + A);
33925c28e83SPiotr Jasiukajtis 		else
34025c28e83SPiotr Jasiukajtis 			D_IM(ans) = half * log1p((y + y) * (y + A));
34125c28e83SPiotr Jasiukajtis 	} else {	/* safe region */
34225c28e83SPiotr Jasiukajtis 		y2 = y * y;
34325c28e83SPiotr Jasiukajtis 		xp1 = x + one;
34425c28e83SPiotr Jasiukajtis 		xm1 = x - one;
34525c28e83SPiotr Jasiukajtis 		R = sqrt(xp1 * xp1 + y2);
34625c28e83SPiotr Jasiukajtis 		S = sqrt(xm1 * xm1 + y2);
34725c28e83SPiotr Jasiukajtis 		A = half * (R + S);
34825c28e83SPiotr Jasiukajtis 		B = x / A;
34925c28e83SPiotr Jasiukajtis 
35025c28e83SPiotr Jasiukajtis 		if (B <= Bcrossover)
35125c28e83SPiotr Jasiukajtis 			D_RE(ans) = asin(B);
35225c28e83SPiotr Jasiukajtis 		else {		/* use atan and an accurate approx to a-x */
35325c28e83SPiotr Jasiukajtis 			Apx = A + x;
35425c28e83SPiotr Jasiukajtis 			if (x <= one)
35525c28e83SPiotr Jasiukajtis 				D_RE(ans) = atan(x / sqrt(half * Apx * (y2 /
35625c28e83SPiotr Jasiukajtis 					(R + xp1) + (S - xm1))));
35725c28e83SPiotr Jasiukajtis 			else
35825c28e83SPiotr Jasiukajtis 				D_RE(ans) = atan(x / (y * sqrt(half * (Apx /
35925c28e83SPiotr Jasiukajtis 					(R + xp1) + Apx / (S + xm1)))));
36025c28e83SPiotr Jasiukajtis 		}
36125c28e83SPiotr Jasiukajtis 		if (A <= Acrossover) {
36225c28e83SPiotr Jasiukajtis 			/* use log1p and an accurate approx to A-1 */
36325c28e83SPiotr Jasiukajtis 			if (x < one)
36425c28e83SPiotr Jasiukajtis 				Am1 = half * (y2 / (R + xp1) + y2 / (S - xm1));
36525c28e83SPiotr Jasiukajtis 			else
36625c28e83SPiotr Jasiukajtis 				Am1 = half * (y2 / (R + xp1) + (S + xm1));
36725c28e83SPiotr Jasiukajtis 			D_IM(ans) = log1p(Am1 + sqrt(Am1 * (A + one)));
36825c28e83SPiotr Jasiukajtis 		} else {
36925c28e83SPiotr Jasiukajtis 			D_IM(ans) = log(A + sqrt(A * A - one));
37025c28e83SPiotr Jasiukajtis 		}
37125c28e83SPiotr Jasiukajtis 	}
37225c28e83SPiotr Jasiukajtis 
37325c28e83SPiotr Jasiukajtis 	if (hx < 0)
37425c28e83SPiotr Jasiukajtis 		D_RE(ans) = -D_RE(ans);
37525c28e83SPiotr Jasiukajtis 	if (hy < 0)
37625c28e83SPiotr Jasiukajtis 		D_IM(ans) = -D_IM(ans);
37725c28e83SPiotr Jasiukajtis 
37825c28e83SPiotr Jasiukajtis 	return (ans);
37925c28e83SPiotr Jasiukajtis }
380