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 __casinl = casinl 3125c28e83SPiotr Jasiukajtis 3225c28e83SPiotr Jasiukajtis #include "libm.h" /* asinl/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_4 = 0.7853981633974483095739921312272713294078130L, 4825c28e83SPiotr Jasiukajtis pi_4_l = 4.1668714592604391641479322342670193036704898e-20L, 4925c28e83SPiotr Jasiukajtis pi_2 = 1.5707963267948966191479842624545426588156260L, 5025c28e83SPiotr Jasiukajtis pi_2_l = 8.3337429185208783282958644685340386073409796e-20L; 5125c28e83SPiotr Jasiukajtis 5225c28e83SPiotr Jasiukajtis #else 5325c28e83SPiotr Jasiukajtis E = 9.6296497219361792652798897129246365926905e-35L, /* 2**-113 */ 5425c28e83SPiotr Jasiukajtis pi_4 = 0.7853981633974483096156608458198756993697670L, 5525c28e83SPiotr Jasiukajtis pi_4_l = 2.1679525325309452561992610065108379921905808e-35L, 5625c28e83SPiotr Jasiukajtis pi_2 = 1.5707963267948966192313216916397513987395340L, 5725c28e83SPiotr Jasiukajtis pi_2_l = 4.3359050650618905123985220130216759843811616e-35L; 5825c28e83SPiotr Jasiukajtis 5925c28e83SPiotr Jasiukajtis #endif 6025c28e83SPiotr Jasiukajtis /* INDENT ON */ 6125c28e83SPiotr Jasiukajtis 6225c28e83SPiotr Jasiukajtis #if defined(__x86) 6325c28e83SPiotr Jasiukajtis static const int ip1 = 0x40400000; /* 2**65 */ 6425c28e83SPiotr Jasiukajtis #else 6525c28e83SPiotr Jasiukajtis static const int ip1 = 0x40710000; /* 2**114 */ 6625c28e83SPiotr Jasiukajtis #endif 6725c28e83SPiotr Jasiukajtis 6825c28e83SPiotr Jasiukajtis ldcomplex 6925c28e83SPiotr Jasiukajtis casinl(ldcomplex z) { 7025c28e83SPiotr Jasiukajtis long double x, y, t, R, S, A, Am1, B, y2, xm1, xp1, Apx; 7125c28e83SPiotr Jasiukajtis int ix, iy, hx, hy; 7225c28e83SPiotr Jasiukajtis ldcomplex ans; 7325c28e83SPiotr Jasiukajtis 7425c28e83SPiotr Jasiukajtis x = LD_RE(z); 7525c28e83SPiotr Jasiukajtis y = LD_IM(z); 7625c28e83SPiotr Jasiukajtis hx = HI_XWORD(x); 7725c28e83SPiotr Jasiukajtis hy = HI_XWORD(y); 7825c28e83SPiotr Jasiukajtis ix = hx & 0x7fffffff; 7925c28e83SPiotr Jasiukajtis iy = hy & 0x7fffffff; 8025c28e83SPiotr Jasiukajtis x = fabsl(x); 8125c28e83SPiotr Jasiukajtis y = fabsl(y); 8225c28e83SPiotr Jasiukajtis 8325c28e83SPiotr Jasiukajtis /* special cases */ 8425c28e83SPiotr Jasiukajtis 8525c28e83SPiotr Jasiukajtis /* x is inf or NaN */ 8625c28e83SPiotr Jasiukajtis if (ix >= 0x7fff0000) { /* x is inf or NaN */ 8725c28e83SPiotr Jasiukajtis if (isinfl(x)) { /* x is INF */ 8825c28e83SPiotr Jasiukajtis LD_IM(ans) = x; 8925c28e83SPiotr Jasiukajtis if (iy >= 0x7fff0000) { 9025c28e83SPiotr Jasiukajtis if (isinfl(y)) 9125c28e83SPiotr Jasiukajtis /* casin(inf + i inf) = pi/4 + i inf */ 9225c28e83SPiotr Jasiukajtis LD_RE(ans) = pi_4 + pi_4_l; 9325c28e83SPiotr Jasiukajtis else /* casin(inf + i NaN) = NaN + i inf */ 9425c28e83SPiotr Jasiukajtis LD_RE(ans) = y + y; 9525c28e83SPiotr Jasiukajtis } else /* casin(inf + iy) = pi/2 + i inf */ 9625c28e83SPiotr Jasiukajtis LD_RE(ans) = pi_2 + pi_2_l; 9725c28e83SPiotr Jasiukajtis } else { /* x is NaN */ 9825c28e83SPiotr Jasiukajtis if (iy >= 0x7fff0000) { 9925c28e83SPiotr Jasiukajtis /* INDENT OFF */ 10025c28e83SPiotr Jasiukajtis /* 10125c28e83SPiotr Jasiukajtis * casin(NaN + i inf) = NaN + i inf 10225c28e83SPiotr Jasiukajtis * casin(NaN + i NaN) = NaN + i NaN 10325c28e83SPiotr Jasiukajtis */ 10425c28e83SPiotr Jasiukajtis /* INDENT ON */ 10525c28e83SPiotr Jasiukajtis LD_IM(ans) = y + y; 10625c28e83SPiotr Jasiukajtis LD_RE(ans) = x + x; 10725c28e83SPiotr Jasiukajtis } else { 10825c28e83SPiotr Jasiukajtis /* INDENT OFF */ 10925c28e83SPiotr Jasiukajtis /* casin(NaN + i y ) = NaN + i NaN */ 11025c28e83SPiotr Jasiukajtis /* INDENT ON */ 11125c28e83SPiotr Jasiukajtis LD_IM(ans) = LD_RE(ans) = x + y; 11225c28e83SPiotr Jasiukajtis } 11325c28e83SPiotr Jasiukajtis } 11425c28e83SPiotr Jasiukajtis if (hx < 0) 11525c28e83SPiotr Jasiukajtis LD_RE(ans) = -LD_RE(ans); 11625c28e83SPiotr Jasiukajtis if (hy < 0) 11725c28e83SPiotr Jasiukajtis LD_IM(ans) = -LD_IM(ans); 11825c28e83SPiotr Jasiukajtis return (ans); 11925c28e83SPiotr Jasiukajtis } 12025c28e83SPiotr Jasiukajtis 12125c28e83SPiotr Jasiukajtis /* casin(+0 + i 0) = 0 + i 0. */ 12225c28e83SPiotr Jasiukajtis if (x == zero && y == zero) 12325c28e83SPiotr Jasiukajtis return (z); 12425c28e83SPiotr Jasiukajtis 12525c28e83SPiotr Jasiukajtis if (iy >= 0x7fff0000) { /* y is inf or NaN */ 12625c28e83SPiotr Jasiukajtis if (isinfl(y)) { /* casin(x + i inf) = 0 + i inf */ 12725c28e83SPiotr Jasiukajtis LD_IM(ans) = y; 12825c28e83SPiotr Jasiukajtis LD_RE(ans) = zero; 12925c28e83SPiotr Jasiukajtis } else { /* casin(x + i NaN) = NaN + i NaN */ 13025c28e83SPiotr Jasiukajtis LD_IM(ans) = x + y; 13125c28e83SPiotr Jasiukajtis if (x == zero) 13225c28e83SPiotr Jasiukajtis LD_RE(ans) = x; 13325c28e83SPiotr Jasiukajtis else 13425c28e83SPiotr Jasiukajtis LD_RE(ans) = y; 13525c28e83SPiotr Jasiukajtis } 13625c28e83SPiotr Jasiukajtis if (hx < 0) 13725c28e83SPiotr Jasiukajtis LD_RE(ans) = -LD_RE(ans); 13825c28e83SPiotr Jasiukajtis if (hy < 0) 13925c28e83SPiotr Jasiukajtis LD_IM(ans) = -LD_IM(ans); 14025c28e83SPiotr Jasiukajtis return (ans); 14125c28e83SPiotr Jasiukajtis } 14225c28e83SPiotr Jasiukajtis 14325c28e83SPiotr Jasiukajtis if (y == zero) { /* region 1: y=0 */ 14425c28e83SPiotr Jasiukajtis if (ix < 0x3fff0000) { /* |x| < 1 */ 14525c28e83SPiotr Jasiukajtis LD_RE(ans) = asinl(x); 14625c28e83SPiotr Jasiukajtis LD_IM(ans) = zero; 14725c28e83SPiotr Jasiukajtis } else { 14825c28e83SPiotr Jasiukajtis LD_RE(ans) = pi_2 + pi_2_l; 14925c28e83SPiotr Jasiukajtis if (ix >= ip1) /* |x| >= i386 ? 2**65 : 2**114 */ 15025c28e83SPiotr Jasiukajtis LD_IM(ans) = ln2 + logl(x); 15125c28e83SPiotr Jasiukajtis else if (ix >= 0x3fff8000) /* x > Acrossover */ 15225c28e83SPiotr Jasiukajtis LD_IM(ans) = logl(x + sqrtl((x - one) * (x + 15325c28e83SPiotr Jasiukajtis one))); 15425c28e83SPiotr Jasiukajtis else { 15525c28e83SPiotr Jasiukajtis xm1 = x - one; 15625c28e83SPiotr Jasiukajtis LD_IM(ans) = log1pl(xm1 + sqrtl(xm1 * (x + 15725c28e83SPiotr Jasiukajtis one))); 15825c28e83SPiotr Jasiukajtis } 15925c28e83SPiotr Jasiukajtis } 16025c28e83SPiotr Jasiukajtis } else if (y <= E * fabsl(x - one)) { /* region 2: y < tiny*|x-1| */ 16125c28e83SPiotr Jasiukajtis if (ix < 0x3fff0000) { /* x < 1 */ 16225c28e83SPiotr Jasiukajtis LD_RE(ans) = asinl(x); 16325c28e83SPiotr Jasiukajtis LD_IM(ans) = y / sqrtl((one + x) * (one - x)); 16425c28e83SPiotr Jasiukajtis } else { 16525c28e83SPiotr Jasiukajtis LD_RE(ans) = pi_2 + pi_2_l; 16625c28e83SPiotr Jasiukajtis if (ix >= ip1) /* i386 ? 2**65 : 2**114 */ 16725c28e83SPiotr Jasiukajtis LD_IM(ans) = ln2 + logl(x); 16825c28e83SPiotr Jasiukajtis else if (ix >= 0x3fff8000) /* x > Acrossover */ 16925c28e83SPiotr Jasiukajtis LD_IM(ans) = logl(x + sqrtl((x - one) * (x + 17025c28e83SPiotr Jasiukajtis one))); 17125c28e83SPiotr Jasiukajtis else 17225c28e83SPiotr Jasiukajtis LD_IM(ans) = log1pl((x - one) + sqrtl((x - 17325c28e83SPiotr Jasiukajtis one) * (x + one))); 17425c28e83SPiotr Jasiukajtis } 17525c28e83SPiotr Jasiukajtis } else if (y < Foursqrtu) { /* region 3 */ 17625c28e83SPiotr Jasiukajtis t = sqrtl(y); 17725c28e83SPiotr Jasiukajtis LD_RE(ans) = pi_2 - (t - pi_2_l); 17825c28e83SPiotr Jasiukajtis LD_IM(ans) = t; 17925c28e83SPiotr Jasiukajtis } else if (E * y - one >= x) { /* region 4 */ 18025c28e83SPiotr Jasiukajtis LD_RE(ans) = x / y; /* need to fix underflow cases */ 18125c28e83SPiotr Jasiukajtis LD_IM(ans) = ln2 + logl(y); 18225c28e83SPiotr Jasiukajtis } else if (ix >= 0x5ffb0000 || iy >= 0x5ffb0000) { 18325c28e83SPiotr Jasiukajtis /* region 5: x+1 and y are both (>= sqrt(max)/8) i.e. 2**8188 */ 18425c28e83SPiotr Jasiukajtis t = x / y; 18525c28e83SPiotr Jasiukajtis LD_RE(ans) = atanl(t); 18625c28e83SPiotr Jasiukajtis LD_IM(ans) = ln2 + logl(y) + half * log1pl(t * t); 18725c28e83SPiotr Jasiukajtis } else if (x < Foursqrtu) { 18825c28e83SPiotr Jasiukajtis /* region 6: x is very small, < 4sqrt(min) */ 18925c28e83SPiotr Jasiukajtis A = sqrtl(one + y * y); 19025c28e83SPiotr Jasiukajtis LD_RE(ans) = x / A; /* may underflow */ 19125c28e83SPiotr Jasiukajtis if (iy >= 0x3fff8000) /* if y > Acrossover */ 19225c28e83SPiotr Jasiukajtis LD_IM(ans) = logl(y + A); 19325c28e83SPiotr Jasiukajtis else 19425c28e83SPiotr Jasiukajtis LD_IM(ans) = half * log1pl((y + y) * (y + A)); 19525c28e83SPiotr Jasiukajtis } else { /* safe region */ 19625c28e83SPiotr Jasiukajtis y2 = y * y; 19725c28e83SPiotr Jasiukajtis xp1 = x + one; 19825c28e83SPiotr Jasiukajtis xm1 = x - one; 19925c28e83SPiotr Jasiukajtis R = sqrtl(xp1 * xp1 + y2); 20025c28e83SPiotr Jasiukajtis S = sqrtl(xm1 * xm1 + y2); 20125c28e83SPiotr Jasiukajtis A = half * (R + S); 20225c28e83SPiotr Jasiukajtis B = x / A; 20325c28e83SPiotr Jasiukajtis if (B <= Bcrossover) 20425c28e83SPiotr Jasiukajtis LD_RE(ans) = asinl(B); 20525c28e83SPiotr Jasiukajtis else { /* use atan and an accurate approx to a-x */ 20625c28e83SPiotr Jasiukajtis Apx = A + x; 20725c28e83SPiotr Jasiukajtis if (x <= one) 20825c28e83SPiotr Jasiukajtis LD_RE(ans) = atanl(x / sqrtl(half * Apx * (y2 / 20925c28e83SPiotr Jasiukajtis (R + xp1) + (S - xm1)))); 21025c28e83SPiotr Jasiukajtis else 21125c28e83SPiotr Jasiukajtis LD_RE(ans) = atanl(x / (y * sqrtl(half * (Apx / 21225c28e83SPiotr Jasiukajtis (R + xp1) + Apx / (S + xm1))))); 21325c28e83SPiotr Jasiukajtis } 21425c28e83SPiotr Jasiukajtis if (A <= Acrossover) { 21525c28e83SPiotr Jasiukajtis /* use log1p and an accurate approx to A-1 */ 21625c28e83SPiotr Jasiukajtis if (x < one) 21725c28e83SPiotr Jasiukajtis Am1 = half * (y2 / (R + xp1) + y2 / (S - xm1)); 21825c28e83SPiotr Jasiukajtis else 21925c28e83SPiotr Jasiukajtis Am1 = half * (y2 / (R + xp1) + (S + xm1)); 22025c28e83SPiotr Jasiukajtis LD_IM(ans) = log1pl(Am1 + sqrtl(Am1 * (A + one))); 22125c28e83SPiotr Jasiukajtis } else { 22225c28e83SPiotr Jasiukajtis LD_IM(ans) = logl(A + sqrtl(A * A - one)); 22325c28e83SPiotr Jasiukajtis } 22425c28e83SPiotr Jasiukajtis } 22525c28e83SPiotr Jasiukajtis 22625c28e83SPiotr Jasiukajtis if (hx < 0) 22725c28e83SPiotr Jasiukajtis LD_RE(ans) = -LD_RE(ans); 22825c28e83SPiotr Jasiukajtis if (hy < 0) 22925c28e83SPiotr Jasiukajtis LD_IM(ans) = -LD_IM(ans); 23025c28e83SPiotr Jasiukajtis 23125c28e83SPiotr Jasiukajtis return (ans); 23225c28e83SPiotr Jasiukajtis } 233