1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 #pragma weak __casin = casin
  32 
  33 
  34 /*
  35  * dcomplex casin(dcomplex z);
  36  *
  37  * Alogrithm
  38  * (based on T.E.Hull, Thomas F. Fairgrieve and Ping Tak Peter Tang's
  39  * paper "Implementing the Complex Arcsine and Arccosine Functins Using
  40  * Exception Handling", ACM TOMS, Vol 23, pp 299-335)
  41  *
  42  * The principal value of complex inverse sine function casin(z),
  43  * where z = x+iy, can be defined by
  44  *
  45  *      casin(z) = asin(B) + i sign(y) log (A + sqrt(A*A-1)),
  46  *
  47  * where the log function is the natural log, and
  48  *             ____________           ____________
  49  *       1    /     2    2      1    /     2    2
  50  *  A = ---  / (x+1)  + y   +  ---  / (x-1)  + y
  51  *       2 \/                   2 \/
  52  *             ____________           ____________
  53  *       1    /     2    2      1    /     2    2
  54  *  B = ---  / (x+1)  + y   -  ---  / (x-1)  + y   .
  55  *       2 \/                   2 \/
  56  *
  57  * The Branch cuts are on the real line from -inf to -1 and from 1 to inf.
  58  * The real and imaginary parts are based on Abramowitz and Stegun
  59  * [Handbook of Mathematic Functions, 1972].  The sign of the imaginary
  60  * part is chosen to be the generally considered the principal value of
  61  * this function.
  62  *
  63  * Notes:1. A is the average of the distances from z to the points (1,0)
  64  *          and (-1,0) in the complex z-plane, and in particular A>=1.
  65  *       2. B is in [-1,1], and A*B = x.
  66  *
  67  * Special notes: if casin( x, y) = ( u, v), then
  68  *                  casin(-x, y) = (-u, v),
  69  *                  casin( x,-y) = ( u,-v),
  70  *    in general, we have casin(conj(z))     =  conj(casin(z))
  71  *                       casin(-z)          = -casin(z)
  72  *                       casin(z)           =  pi/2 - cacos(z)
  73  *
  74  * EXCEPTION CASES (conform to ISO/IEC 9899:1999(E)):
  75  *    casin( 0 + i 0   ) =  0    + i 0
  76  *    casin( 0 + i NaN ) =  0    + i NaN
  77  *    casin( x + i inf ) =  0    + i inf for finite x
  78  *    casin( x + i NaN ) =  NaN  + i NaN with invalid for finite x != 0
  79  *    casin(inf + iy   ) =  pi/2 + i inf finite y
  80  *    casin(inf + i inf) =  pi/4 + i inf
  81  *    casin(inf + i NaN) =  NaN  + i inf
  82  *    casin(NaN + i y  ) =  NaN  + i NaN for finite y
  83  *    casin(NaN + i inf) =  NaN  + i inf
  84  *    casin(NaN + i NaN) =  NaN  + i NaN
  85  *
  86  * Special Regions (better formula for accuracy and for avoiding spurious
  87  * overflow or underflow) (all x and y are assumed nonnegative):
  88  *  case 1: y = 0
  89  *  case 2: tiny y relative to x-1: y <= ulp(0.5)*|x-1|
  90  *  case 3: tiny y: y < 4 sqrt(u), where u = minimum normal number
  91  *  case 4: huge y relative to x+1: y >= (1+x)/ulp(0.5)
  92  *  case 5: huge x and y: x and y >= sqrt(M)/8, where M = maximum normal number
  93  *  case 6: tiny x: x < 4 sqrt(u)
  94  *  --------
  95  *  case        1 & 2. y=0 or y/|x-1| is tiny. We have
  96  *             ____________              _____________
  97  *            /      2    2             /       y    2
  98  *           / (x+-1)  + y   =  |x+-1| / 1 + (------)
  99  *         \/                        \/       |x+-1|
 100  *
 101  *                                            1      y   2
 102  *                           ~  |x+-1| ( 1 + --- (------)  )
 103  *                                            2   |x+-1|
 104  *
 105  *                                           2
 106  *                                          y
 107  *                           =  |x+-1| + --------.
 108  *                                       2|x+-1|
 109  *
 110  *      Consequently, it is not difficult to see that
 111  *                                 2
 112  *                                y
 113  *                    [ 1 + ------------ ,  if x < 1,
 114  *                    [      2(1+x)(1-x)
 115  *                    [
 116  *                    [
 117  *                    [ x,                 if x = 1 (y = 0),
 118  *                    [
 119  *              A ~=  [             2
 120  *                    [        x * y
 121  *                    [ x + ------------ ,  if x > 1
 122  *                    [      2(1+x)(x-1)
 123  *
 124  *      and hence
 125  *                      ______                                 2
 126  *                     / 2                    y               y
 127  *               A + \/ A  - 1  ~  1 + ---------------- + -----------, if x < 1,
 128  *                                     sqrt((x+1)(1-x))   2(x+1)(1-x)
 129  *
 130  *
 131  *                             ~  x + sqrt((x-1)*(x+1)),              if x >= 1.
 132  *
 133  *                                         2
 134  *                                        y
 135  *                          [ x(1 - ------------), if x < 1,
 136  *                          [       2(1+x)(1-x)
 137  *              B = x/A  ~  [
 138  *                          [ 1,                  if x = 1,
 139  *                          [
 140  *                          [           2
 141  *                          [          y
 142  *                          [ 1 - ------------ ,   if x > 1,
 143  *                          [      2(1+x)(1-x)
 144  *      Thus
 145  *                            [ asin(x) + i y/sqrt((x-1)*(x+1)), if x <  1
 146  *              casin(x+i*y)=[
 147  *                            [ pi/2    + i log(x+sqrt(x*x-1)),  if x >= 1
 148  *
 149  *  case 3. y < 4 sqrt(u), where u = minimum normal x.
 150  *      After case 1 and 2, this will only occurs when x=1. When x=1, we have
 151  *         A = (sqrt(4+y*y)+y)/2 ~ 1 + y/2 + y^2/8 + ...
 152  *      and
 153  *         B = 1/A = 1 - y/2 + y^2/8 + ...
 154  *      Since
 155  *         asin(x) = pi/2-2*asin(sqrt((1-x)/2))
 156  *         asin(x) = x + x^3/6 + x^5*3/40 + x^7*15/336 + ...
 157  *      we have, for the real part asin(B),
 158  *         asin(1-y/2) ~ pi/2 - 2 asin(sqrt(y/4))
 159  *                     ~ pi/2 - sqrt(y)
 160  *      For the imaginary part,
 161  *         log(A+sqrt(A*A-1)) ~ log(1+y/2+sqrt(2*y/2))
 162  *                            = log(1+y/2+sqrt(y))
 163  *                            = (y/2+sqrt(y)) - (y/2+sqrt(y))^2/2 + ...
 164  *                            ~ sqrt(y) - y*(sqrt(y)+y/2)/2
 165  *                            ~ sqrt(y)
 166  *
 167  *  case 4. y >= (x+1)ulp(0.5). In this case, A ~ y and B ~ x/y. Thus
 168  *         real part = asin(B) ~ x/y (be careful, x/y may underflow)
 169  *      and
 170  *         imag part = log(y+sqrt(y*y-one))
 171  *
 172  *
 173  *  case 5. Both x and y are large: x and y > sqrt(M)/8, where M = maximum x
 174  *      In this case,
 175  *         A ~ sqrt(x*x+y*y)
 176  *         B ~ x/sqrt(x*x+y*y).
 177  *      Thus
 178  *         real part = asin(B) = atan(x/y),
 179  *         imag part = log(A+sqrt(A*A-1)) ~ log(2A)
 180  *                   = log(2) + 0.5*log(x*x+y*y)
 181  *                   = log(2) + log(y) + 0.5*log(1+(x/y)^2)
 182  *
 183  *  case 6. x < 4 sqrt(u). In this case, we have
 184  *          A ~ sqrt(1+y*y), B = x/sqrt(1+y*y).
 185  *      Since B is tiny, we have
 186  *          real part = asin(B) ~ B = x/sqrt(1+y*y)
 187  *          imag part = log(A+sqrt(A*A-1)) = log (A+sqrt(y*y))
 188  *                    = log(y+sqrt(1+y*y))
 189  *                    = 0.5*log(y^2+2ysqrt(1+y^2)+1+y^2)
 190  *                    = 0.5*log(1+2y(y+sqrt(1+y^2)));
 191  *                    = 0.5*log1p(2y(y+A));
 192  *
 193  *      casin(z) = asin(B) + i sign(y) log (A + sqrt(A*A-1)),
 194  */
 195 
 196 #include "libm.h"                       /* asin/atan/fabs/log/log1p/sqrt */
 197 #include "complex_wrapper.h"
 198 
 199 static const double zero = 0.0,
 200         one = 1.0,
 201         E = 1.11022302462515654042e-16, /* 2**-53 */
 202         ln2 = 6.93147180559945286227e-01,
 203         pi_2 = 1.570796326794896558e+00,
 204         pi_2_l = 6.123233995736765886e-17,
 205         pi_4 = 7.85398163397448278999e-01,
 206         Foursqrtu = 5.96667258496016539463e-154, /* 2**(-509) */
 207         Acrossover = 1.5,
 208         Bcrossover = 0.6417,
 209         half = 0.5;
 210 
 211 
 212 dcomplex
 213 casin(dcomplex z)
 214 {
 215         double x, y, t, R, S, A, Am1, B, y2, xm1, xp1, Apx;
 216         int ix, iy, hx, hy;
 217         unsigned lx, ly;
 218         dcomplex ans;
 219 
 220         x = D_RE(z);
 221         y = D_IM(z);
 222         hx = HI_WORD(x);
 223         lx = LO_WORD(x);
 224         hy = HI_WORD(y);
 225         ly = LO_WORD(y);
 226         ix = hx & 0x7fffffff;
 227         iy = hy & 0x7fffffff;
 228         x = fabs(x);
 229         y = fabs(y);
 230 
 231         /* special cases */
 232 
 233         /* x is inf or NaN */
 234         if (ix >= 0x7ff00000) {              /* x is inf or NaN */
 235                 if (ISINF(ix, lx)) {    /* x is INF */
 236                         D_IM(ans) = x;
 237 
 238                         if (iy >= 0x7ff00000) {
 239                                 if (ISINF(iy, ly))
 240                                         /* casin(inf + i inf) = pi/4 + i inf */
 241                                         D_RE(ans) = pi_4;
 242                                 else    /* casin(inf + i NaN) = NaN  + i inf  */
 243                                         D_RE(ans) = y + y;
 244                         } else { /* casin(inf + iy) = pi/2 + i inf */
 245                                 D_RE(ans) = pi_2;
 246                         }
 247                 } else {                /* x is NaN */
 248                         if (iy >= 0x7ff00000) {
 249 
 250                                 /*
 251                                  * casin(NaN + i inf) = NaN + i inf
 252                                  * casin(NaN + i NaN) = NaN + i NaN
 253                                  */
 254                                 D_IM(ans) = y + y;
 255                                 D_RE(ans) = x + x;
 256                         } else {
 257                                 /* casin(NaN + i y ) = NaN  + i NaN */
 258                                 D_IM(ans) = D_RE(ans) = x + y;
 259                         }
 260                 }
 261 
 262                 if (hx < 0)
 263                         D_RE(ans) = -D_RE(ans);
 264 
 265                 if (hy < 0)
 266                         D_IM(ans) = -D_IM(ans);
 267 
 268                 return (ans);
 269         }
 270 
 271         /* casin(+0 + i 0  ) =  0   + i 0. */
 272         if ((ix | lx | iy | ly) == 0)
 273                 return (z);
 274 
 275         if (iy >= 0x7ff00000) {              /* y is inf or NaN */
 276                 if (ISINF(iy, ly)) {    /* casin(x + i inf) =  0   + i inf */
 277                         D_IM(ans) = y;
 278                         D_RE(ans) = zero;
 279                 } else {                /* casin(x + i NaN) = NaN  + i NaN */
 280                         D_IM(ans) = x + y;
 281 
 282                         if ((ix | lx) == 0)
 283                                 D_RE(ans) = x;
 284                         else
 285                                 D_RE(ans) = y;
 286                 }
 287 
 288                 if (hx < 0)
 289                         D_RE(ans) = -D_RE(ans);
 290 
 291                 if (hy < 0)
 292                         D_IM(ans) = -D_IM(ans);
 293 
 294                 return (ans);
 295         }
 296 
 297         if ((iy | ly) == 0) {           /* region 1: y=0 */
 298                 if (ix < 0x3ff00000) {       /* |x| < 1 */
 299                         D_RE(ans) = asin(x);
 300                         D_IM(ans) = zero;
 301                 } else {
 302                         D_RE(ans) = pi_2;
 303 
 304                         if (ix >= 0x43500000) {              /* |x| >= 2**54 */
 305                                 D_IM(ans) = ln2 + log(x);
 306                         } else if (ix >= 0x3ff80000) {       /* x > Acrossover */
 307                                 D_IM(ans) = log(x + sqrt((x - one) * (x +
 308                                     one)));
 309                         } else {
 310                                 xm1 = x - one;
 311                                 D_IM(ans) = log1p(xm1 + sqrt(xm1 * (x + one)));
 312                         }
 313                 }
 314         } else if (y <= E * fabs(x - one)) { /* region 2: y < tiny*|x-1| */
 315                 if (ix < 0x3ff00000) {               /* x < 1 */
 316                         D_RE(ans) = asin(x);
 317                         D_IM(ans) = y / sqrt((one + x) * (one - x));
 318                 } else {
 319                         D_RE(ans) = pi_2;
 320 
 321                         if (ix >= 0x43500000)                /* |x| >= 2**54 */
 322                                 D_IM(ans) = ln2 + log(x);
 323                         else if (ix >= 0x3ff80000)   /* x > Acrossover */
 324                                 D_IM(ans) = log(x + sqrt((x - one) * (x +
 325                                     one)));
 326                         else
 327                                 D_IM(ans) = log1p((x - one) + sqrt((x - one) *
 328                                     (x + one)));
 329                 }
 330         } else if (y < Foursqrtu) {  /* region 3 */
 331                 t = sqrt(y);
 332                 D_RE(ans) = pi_2 - (t - pi_2_l);
 333                 D_IM(ans) = t;
 334         } else if (E * y - one >= x) { /* region 4 */
 335                 D_RE(ans) = x / y; /* need to fix underflow cases */
 336                 D_IM(ans) = ln2 + log(y);
 337         } else if (ix >= 0x5fc00000 || iy >= 0x5fc00000) {        /* x,y>2**509 */
 338                 /* region 5: x+1 or y is very large (>= sqrt(max)/8) */
 339                 t = x / y;
 340                 D_RE(ans) = atan(t);
 341                 D_IM(ans) = ln2 + log(y) + half * log1p(t * t);
 342         } else if (x < Foursqrtu) {
 343                 /* region 6: x is very small, < 4sqrt(min) */
 344                 A = sqrt(one + y * y);
 345                 D_RE(ans) = x / A;      /* may underflow */
 346 
 347                 if (iy >= 0x3ff80000)        /* if y > Acrossover */
 348                         D_IM(ans) = log(y + A);
 349                 else
 350                         D_IM(ans) = half * log1p((y + y) * (y + A));
 351         } else {                        /* safe region */
 352                 y2 = y * y;
 353                 xp1 = x + one;
 354                 xm1 = x - one;
 355                 R = sqrt(xp1 * xp1 + y2);
 356                 S = sqrt(xm1 * xm1 + y2);
 357                 A = half * (R + S);
 358                 B = x / A;
 359 
 360                 if (B <= Bcrossover) {
 361                         D_RE(ans) = asin(B);
 362                 } else {        /* use atan and an accurate approx to a-x */
 363                         Apx = A + x;
 364 
 365                         if (x <= one)
 366                                 D_RE(ans) = atan(x / sqrt(half * Apx * (y2 /
 367                                     (R + xp1) + (S - xm1))));
 368                         else
 369                                 D_RE(ans) = atan(x / (y * sqrt(half * (Apx /
 370                                     (R + xp1) + Apx / (S + xm1)))));
 371                 }
 372 
 373                 if (A <= Acrossover) {
 374                         /* use log1p and an accurate approx to A-1 */
 375                         if (x < one)
 376                                 Am1 = half * (y2 / (R + xp1) + y2 / (S - xm1));
 377                         else
 378                                 Am1 = half * (y2 / (R + xp1) + (S + xm1));
 379 
 380                         D_IM(ans) = log1p(Am1 + sqrt(Am1 * (A + one)));
 381                 } else {
 382                         D_IM(ans) = log(A + sqrt(A * A - one));
 383                 }
 384         }
 385 
 386         if (hx < 0)
 387                 D_RE(ans) = -D_RE(ans);
 388 
 389         if (hy < 0)
 390                 D_IM(ans) = -D_IM(ans);
 391 
 392         return (ans);
 393 }