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 __cacosl = cacosl
3125c28e83SPiotr Jasiukajtis
3225c28e83SPiotr Jasiukajtis #include "libm.h" /* acosl/atanl/fabsl/isinfl/log1pl/logl/sqrtl */
3325c28e83SPiotr Jasiukajtis #include "complex_wrapper.h"
3425c28e83SPiotr Jasiukajtis #include "longdouble.h"
3525c28e83SPiotr Jasiukajtis
3625c28e83SPiotr Jasiukajtis /* INDENT OFF */
3725c28e83SPiotr Jasiukajtis static const long double
3825c28e83SPiotr Jasiukajtis zero = 0.0L,
3925c28e83SPiotr Jasiukajtis one = 1.0L,
4025c28e83SPiotr Jasiukajtis Acrossover = 1.5L,
4125c28e83SPiotr Jasiukajtis Bcrossover = 0.6417L,
4225c28e83SPiotr Jasiukajtis half = 0.5L,
4325c28e83SPiotr Jasiukajtis ln2 = 6.931471805599453094172321214581765680755e-0001L,
4425c28e83SPiotr Jasiukajtis Foursqrtu = 7.3344154702193886624856495681939326638255e-2466L, /* 2**-8189 */
4525c28e83SPiotr Jasiukajtis #if defined(__x86)
4625c28e83SPiotr Jasiukajtis E = 5.4210108624275221700372640043497085571289e-20L, /* 2**-64 */
4725c28e83SPiotr Jasiukajtis pi = 3.141592653589793238295968524909085317631252110004425048828125L,
4825c28e83SPiotr Jasiukajtis pi_l = 1.666748583704175665659172893706807721468195923078e-19L,
4925c28e83SPiotr Jasiukajtis pi_2 = 1.5707963267948966191479842624545426588156260L,
5025c28e83SPiotr Jasiukajtis pi_2_l = 8.3337429185208783282958644685340386073409796e-20L,
5125c28e83SPiotr Jasiukajtis pi_4 = 0.78539816339744830957399213122727132940781302750110626220703125L,
5225c28e83SPiotr Jasiukajtis pi_4_l = 4.166871459260439164147932234267019303670489807695410e-20L,
5325c28e83SPiotr Jasiukajtis pi3_4 = 2.35619449019234492872197639368181398822343908250331878662109375L,
5425c28e83SPiotr Jasiukajtis pi3_4_l = 1.250061437778131749244379670280105791101146942308e-19L;
5525c28e83SPiotr Jasiukajtis #else
5625c28e83SPiotr Jasiukajtis E = 9.6296497219361792652798897129246365926905e-35L, /* 2**-113 */
5725c28e83SPiotr Jasiukajtis pi = 3.1415926535897932384626433832795027974790680981372955730045043318L,
5825c28e83SPiotr Jasiukajtis pi_l = 8.6718101301237810247970440260433519687623233462565303417759356862e-35L,
5925c28e83SPiotr Jasiukajtis pi_2 = 1.5707963267948966192313216916397513987395340L,
6025c28e83SPiotr Jasiukajtis pi_2_l = 4.3359050650618905123985220130216759843811616e-35L,
6125c28e83SPiotr Jasiukajtis pi_4 = 0.785398163397448309615660845819875699369767024534323893251126L,
6225c28e83SPiotr Jasiukajtis pi_4_l = 2.167952532530945256199261006510837992190580836564132585443e-35L,
6325c28e83SPiotr Jasiukajtis pi3_4 = 2.35619449019234492884698253745962709810930107360297167975337824L,
6425c28e83SPiotr Jasiukajtis pi3_4_l = 6.503857597592835768597783019532513976571742509692397756331e-35L;
6525c28e83SPiotr Jasiukajtis #endif
6625c28e83SPiotr Jasiukajtis /* INDENT ON */
6725c28e83SPiotr Jasiukajtis
6825c28e83SPiotr Jasiukajtis #if defined(__x86)
6925c28e83SPiotr Jasiukajtis static const int ip1 = 0x40400000; /* 2**65 */
7025c28e83SPiotr Jasiukajtis #else
7125c28e83SPiotr Jasiukajtis static const int ip1 = 0x40710000; /* 2**114 */
7225c28e83SPiotr Jasiukajtis #endif
7325c28e83SPiotr Jasiukajtis
7425c28e83SPiotr Jasiukajtis ldcomplex
cacosl(ldcomplex z)7525c28e83SPiotr Jasiukajtis cacosl(ldcomplex z) {
7625c28e83SPiotr Jasiukajtis long double x, y, t, R, S, A, Am1, B, y2, xm1, xp1, Apx;
7725c28e83SPiotr Jasiukajtis int ix, iy, hx, hy;
7825c28e83SPiotr Jasiukajtis ldcomplex ans;
7925c28e83SPiotr Jasiukajtis
8025c28e83SPiotr Jasiukajtis x = LD_RE(z);
8125c28e83SPiotr Jasiukajtis y = LD_IM(z);
8225c28e83SPiotr Jasiukajtis hx = HI_XWORD(x);
8325c28e83SPiotr Jasiukajtis hy = HI_XWORD(y);
8425c28e83SPiotr Jasiukajtis ix = hx & 0x7fffffff;
8525c28e83SPiotr Jasiukajtis iy = hy & 0x7fffffff;
8625c28e83SPiotr Jasiukajtis
8725c28e83SPiotr Jasiukajtis /* x is 0 */
8825c28e83SPiotr Jasiukajtis if (x == zero) {
8925c28e83SPiotr Jasiukajtis if (y == zero || (iy >= 0x7fff0000)) {
9025c28e83SPiotr Jasiukajtis LD_RE(ans) = pi_2 + pi_2_l;
9125c28e83SPiotr Jasiukajtis LD_IM(ans) = -y;
9225c28e83SPiotr Jasiukajtis return (ans);
9325c28e83SPiotr Jasiukajtis }
9425c28e83SPiotr Jasiukajtis }
9525c28e83SPiotr Jasiukajtis
9625c28e83SPiotr Jasiukajtis /* |y| is inf or NaN */
9725c28e83SPiotr Jasiukajtis if (iy >= 0x7fff0000) {
9825c28e83SPiotr Jasiukajtis if (isinfl(y)) { /* cacos(x + i inf) = pi/2 - i inf */
9925c28e83SPiotr Jasiukajtis LD_IM(ans) = -y;
10025c28e83SPiotr Jasiukajtis if (ix < 0x7fff0000) {
10125c28e83SPiotr Jasiukajtis LD_RE(ans) = pi_2 + pi_2_l;
10225c28e83SPiotr Jasiukajtis } else if (isinfl(x)) {
10325c28e83SPiotr Jasiukajtis if (hx >= 0)
10425c28e83SPiotr Jasiukajtis LD_RE(ans) = pi_4 + pi_4_l;
10525c28e83SPiotr Jasiukajtis else
10625c28e83SPiotr Jasiukajtis LD_RE(ans) = pi3_4 + pi3_4_l;
10725c28e83SPiotr Jasiukajtis } else {
10825c28e83SPiotr Jasiukajtis LD_RE(ans) = x;
10925c28e83SPiotr Jasiukajtis }
11025c28e83SPiotr Jasiukajtis } else { /* cacos(x + i NaN) = NaN + i NaN */
11125c28e83SPiotr Jasiukajtis LD_RE(ans) = y + x;
11225c28e83SPiotr Jasiukajtis if (isinfl(x))
11325c28e83SPiotr Jasiukajtis LD_IM(ans) = -fabsl(x);
11425c28e83SPiotr Jasiukajtis else
11525c28e83SPiotr Jasiukajtis LD_IM(ans) = y;
11625c28e83SPiotr Jasiukajtis }
11725c28e83SPiotr Jasiukajtis return (ans);
11825c28e83SPiotr Jasiukajtis }
11925c28e83SPiotr Jasiukajtis
12025c28e83SPiotr Jasiukajtis y = fabsl(y);
12125c28e83SPiotr Jasiukajtis
12225c28e83SPiotr Jasiukajtis if (ix >= 0x7fff0000) { /* x is inf or NaN */
12325c28e83SPiotr Jasiukajtis if (isinfl(x)) { /* x is INF */
12425c28e83SPiotr Jasiukajtis LD_IM(ans) = -fabsl(x);
12525c28e83SPiotr Jasiukajtis if (iy >= 0x7fff0000) {
12625c28e83SPiotr Jasiukajtis if (isinfl(y)) {
12725c28e83SPiotr Jasiukajtis /* INDENT OFF */
12825c28e83SPiotr Jasiukajtis /* cacos(inf + i inf) = pi/4 - i inf */
12925c28e83SPiotr Jasiukajtis /* cacos(-inf+ i inf) =3pi/4 - i inf */
13025c28e83SPiotr Jasiukajtis /* INDENT ON */
13125c28e83SPiotr Jasiukajtis if (hx >= 0)
13225c28e83SPiotr Jasiukajtis LD_RE(ans) = pi_4 + pi_4_l;
13325c28e83SPiotr Jasiukajtis else
13425c28e83SPiotr Jasiukajtis LD_RE(ans) = pi3_4 + pi3_4_l;
13525c28e83SPiotr Jasiukajtis } else
13625c28e83SPiotr Jasiukajtis /* INDENT OFF */
13725c28e83SPiotr Jasiukajtis /* cacos(inf + i NaN) = NaN - i inf */
13825c28e83SPiotr Jasiukajtis /* INDENT ON */
13925c28e83SPiotr Jasiukajtis LD_RE(ans) = y + y;
14025c28e83SPiotr Jasiukajtis } else {
14125c28e83SPiotr Jasiukajtis /* INDENT OFF */
14225c28e83SPiotr Jasiukajtis /* cacos(inf + iy ) = 0 - i inf */
14325c28e83SPiotr Jasiukajtis /* cacos(-inf+ iy ) = pi - i inf */
14425c28e83SPiotr Jasiukajtis /* INDENT ON */
14525c28e83SPiotr Jasiukajtis if (hx >= 0)
14625c28e83SPiotr Jasiukajtis LD_RE(ans) = zero;
14725c28e83SPiotr Jasiukajtis else
14825c28e83SPiotr Jasiukajtis LD_RE(ans) = pi + pi_l;
14925c28e83SPiotr Jasiukajtis }
15025c28e83SPiotr Jasiukajtis } else { /* x is NaN */
15125c28e83SPiotr Jasiukajtis /* INDENT OFF */
15225c28e83SPiotr Jasiukajtis /*
15325c28e83SPiotr Jasiukajtis * cacos(NaN + i inf) = NaN - i inf
15425c28e83SPiotr Jasiukajtis * cacos(NaN + i y ) = NaN + i NaN
15525c28e83SPiotr Jasiukajtis * cacos(NaN + i NaN) = NaN + i NaN
15625c28e83SPiotr Jasiukajtis */
15725c28e83SPiotr Jasiukajtis /* INDENT ON */
15825c28e83SPiotr Jasiukajtis LD_RE(ans) = x + y;
15925c28e83SPiotr Jasiukajtis if (iy >= 0x7fff0000) {
16025c28e83SPiotr Jasiukajtis LD_IM(ans) = -y;
16125c28e83SPiotr Jasiukajtis } else {
16225c28e83SPiotr Jasiukajtis LD_IM(ans) = x;
16325c28e83SPiotr Jasiukajtis }
16425c28e83SPiotr Jasiukajtis }
16525c28e83SPiotr Jasiukajtis if (hy < 0)
16625c28e83SPiotr Jasiukajtis LD_IM(ans) = -LD_IM(ans);
16725c28e83SPiotr Jasiukajtis return (ans);
16825c28e83SPiotr Jasiukajtis }
16925c28e83SPiotr Jasiukajtis
17025c28e83SPiotr Jasiukajtis if (y == zero) { /* region 1: y=0 */
17125c28e83SPiotr Jasiukajtis if (ix < 0x3fff0000) { /* |x| < 1 */
17225c28e83SPiotr Jasiukajtis LD_RE(ans) = acosl(x);
17325c28e83SPiotr Jasiukajtis LD_IM(ans) = zero;
17425c28e83SPiotr Jasiukajtis } else {
17525c28e83SPiotr Jasiukajtis LD_RE(ans) = zero;
17625c28e83SPiotr Jasiukajtis x = fabsl(x);
17725c28e83SPiotr Jasiukajtis if (ix >= ip1) /* i386 ? 2**65 : 2**114 */
17825c28e83SPiotr Jasiukajtis LD_IM(ans) = ln2 + logl(x);
17925c28e83SPiotr Jasiukajtis else if (ix >= 0x3fff8000) /* x > Acrossover */
18025c28e83SPiotr Jasiukajtis LD_IM(ans) = logl(x + sqrtl((x - one) * (x +
18125c28e83SPiotr Jasiukajtis one)));
18225c28e83SPiotr Jasiukajtis else {
18325c28e83SPiotr Jasiukajtis xm1 = x - one;
18425c28e83SPiotr Jasiukajtis LD_IM(ans) = log1pl(xm1 + sqrtl(xm1 * (x +
18525c28e83SPiotr Jasiukajtis one)));
18625c28e83SPiotr Jasiukajtis }
18725c28e83SPiotr Jasiukajtis }
18825c28e83SPiotr Jasiukajtis } else if (y <= E * fabsl(fabsl(x) - one)) {
18925c28e83SPiotr Jasiukajtis /* region 2: y < tiny*||x|-1| */
19025c28e83SPiotr Jasiukajtis if (ix < 0x3fff0000) { /* x < 1 */
19125c28e83SPiotr Jasiukajtis LD_RE(ans) = acosl(x);
19225c28e83SPiotr Jasiukajtis x = fabsl(x);
19325c28e83SPiotr Jasiukajtis LD_IM(ans) = y / sqrtl((one + x) * (one - x));
19425c28e83SPiotr Jasiukajtis } else if (ix >= ip1) { /* i386 ? 2**65 : 2**114 */
19525c28e83SPiotr Jasiukajtis if (hx >= 0)
19625c28e83SPiotr Jasiukajtis LD_RE(ans) = y / x;
19725c28e83SPiotr Jasiukajtis else {
19825c28e83SPiotr Jasiukajtis if (ix >= ip1 + 0x00040000)
19925c28e83SPiotr Jasiukajtis LD_RE(ans) = pi + pi_l;
20025c28e83SPiotr Jasiukajtis else {
20125c28e83SPiotr Jasiukajtis t = pi_l + y / x;
20225c28e83SPiotr Jasiukajtis LD_RE(ans) = pi + t;
20325c28e83SPiotr Jasiukajtis }
20425c28e83SPiotr Jasiukajtis }
20525c28e83SPiotr Jasiukajtis LD_IM(ans) = ln2 + logl(fabsl(x));
20625c28e83SPiotr Jasiukajtis } else {
20725c28e83SPiotr Jasiukajtis x = fabsl(x);
20825c28e83SPiotr Jasiukajtis t = sqrtl((x - one) * (x + one));
20925c28e83SPiotr Jasiukajtis LD_RE(ans) = (hx >= 0)? y / t : pi - (y / t - pi_l);
21025c28e83SPiotr Jasiukajtis if (ix >= 0x3fff8000) /* x > Acrossover */
21125c28e83SPiotr Jasiukajtis LD_IM(ans) = logl(x + t);
21225c28e83SPiotr Jasiukajtis else
21325c28e83SPiotr Jasiukajtis LD_IM(ans) = log1pl(t - (one - x));
21425c28e83SPiotr Jasiukajtis }
21525c28e83SPiotr Jasiukajtis } else if (y < Foursqrtu) { /* region 3 */
21625c28e83SPiotr Jasiukajtis t = sqrtl(y);
21725c28e83SPiotr Jasiukajtis LD_RE(ans) = (hx >= 0)? t : pi + pi_l;
21825c28e83SPiotr Jasiukajtis LD_IM(ans) = t;
21925c28e83SPiotr Jasiukajtis } else if (E * y - one >= fabsl(x)) { /* region 4 */
22025c28e83SPiotr Jasiukajtis LD_RE(ans) = pi_2 + pi_2_l;
22125c28e83SPiotr Jasiukajtis LD_IM(ans) = ln2 + logl(y);
22225c28e83SPiotr Jasiukajtis } else if (ix >= 0x5ffb0000 || iy >= 0x5ffb0000) {
22325c28e83SPiotr Jasiukajtis /* region 5: x+1 and y are both (>= sqrt(max)/8) i.e. 2**8188 */
22425c28e83SPiotr Jasiukajtis t = x / y;
22525c28e83SPiotr Jasiukajtis LD_RE(ans) = atan2l(y, x);
22625c28e83SPiotr Jasiukajtis LD_IM(ans) = ln2 + logl(y) + half * log1pl(t * t);
22725c28e83SPiotr Jasiukajtis } else if (fabsl(x) < Foursqrtu) {
22825c28e83SPiotr Jasiukajtis /* region 6: x is very small, < 4sqrt(min) */
22925c28e83SPiotr Jasiukajtis LD_RE(ans) = pi_2 + pi_2_l;
23025c28e83SPiotr Jasiukajtis A = sqrtl(one + y * y);
23125c28e83SPiotr Jasiukajtis if (iy >= 0x3fff8000) /* if y > Acrossover */
23225c28e83SPiotr Jasiukajtis LD_IM(ans) = logl(y + A);
23325c28e83SPiotr Jasiukajtis else
23425c28e83SPiotr Jasiukajtis LD_IM(ans) = half * log1pl((y + y) * (y + A));
23525c28e83SPiotr Jasiukajtis } else { /* safe region */
23625c28e83SPiotr Jasiukajtis t = fabsl(x);
23725c28e83SPiotr Jasiukajtis y2 = y * y;
23825c28e83SPiotr Jasiukajtis xp1 = t + one;
23925c28e83SPiotr Jasiukajtis xm1 = t - one;
24025c28e83SPiotr Jasiukajtis R = sqrtl(xp1 * xp1 + y2);
24125c28e83SPiotr Jasiukajtis S = sqrtl(xm1 * xm1 + y2);
24225c28e83SPiotr Jasiukajtis A = half * (R + S);
24325c28e83SPiotr Jasiukajtis B = t / A;
24425c28e83SPiotr Jasiukajtis
24525c28e83SPiotr Jasiukajtis if (B <= Bcrossover)
24625c28e83SPiotr Jasiukajtis LD_RE(ans) = (hx >= 0)? acosl(B) : acosl(-B);
24725c28e83SPiotr Jasiukajtis else { /* use atan and an accurate approx to a-x */
24825c28e83SPiotr Jasiukajtis Apx = A + t;
24925c28e83SPiotr Jasiukajtis if (t <= one)
25025c28e83SPiotr Jasiukajtis LD_RE(ans) = atan2l(sqrtl(half * Apx * (y2 /
25125c28e83SPiotr Jasiukajtis (R + xp1) + (S - xm1))), x);
25225c28e83SPiotr Jasiukajtis else
25325c28e83SPiotr Jasiukajtis LD_RE(ans) = atan2l((y * sqrtl(half * (Apx /
25425c28e83SPiotr Jasiukajtis (R + xp1) + Apx / (S + xm1)))), x);
25525c28e83SPiotr Jasiukajtis }
25625c28e83SPiotr Jasiukajtis if (A <= Acrossover) {
25725c28e83SPiotr Jasiukajtis /* use log1p and an accurate approx to A-1 */
25825c28e83SPiotr Jasiukajtis if (ix < 0x3fff0000)
25925c28e83SPiotr Jasiukajtis Am1 = half * (y2 / (R + xp1) + y2 / (S - xm1));
26025c28e83SPiotr Jasiukajtis else
26125c28e83SPiotr Jasiukajtis Am1 = half * (y2 / (R + xp1) + (S + xm1));
26225c28e83SPiotr Jasiukajtis LD_IM(ans) = log1pl(Am1 + sqrtl(Am1 * (A + one)));
26325c28e83SPiotr Jasiukajtis } else {
26425c28e83SPiotr Jasiukajtis LD_IM(ans) = logl(A + sqrtl(A * A - one));
26525c28e83SPiotr Jasiukajtis }
26625c28e83SPiotr Jasiukajtis }
26725c28e83SPiotr Jasiukajtis
26825c28e83SPiotr Jasiukajtis if (hy >= 0)
26925c28e83SPiotr Jasiukajtis LD_IM(ans) = -LD_IM(ans);
27025c28e83SPiotr Jasiukajtis
27125c28e83SPiotr Jasiukajtis return (ans);
27225c28e83SPiotr Jasiukajtis }
273