Home | History | Annotate | Line # | Download | only in math
      1  1.1  mrg /* Return arc hyperbolic sine for a complex float type, with the
      2  1.1  mrg    imaginary part of the result possibly adjusted for use in
      3  1.1  mrg    computing other functions.
      4  1.1  mrg    Copyright (C) 1997-2018 Free Software Foundation, Inc.
      5  1.1  mrg    This file is part of the GNU C Library.
      6  1.1  mrg 
      7  1.1  mrg    The GNU C Library is free software; you can redistribute it and/or
      8  1.1  mrg    modify it under the terms of the GNU Lesser General Public
      9  1.1  mrg    License as published by the Free Software Foundation; either
     10  1.1  mrg    version 2.1 of the License, or (at your option) any later version.
     11  1.1  mrg 
     12  1.1  mrg    The GNU C Library is distributed in the hope that it will be useful,
     13  1.1  mrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  mrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  1.1  mrg    Lesser General Public License for more details.
     16  1.1  mrg 
     17  1.1  mrg    You should have received a copy of the GNU Lesser General Public
     18  1.1  mrg    License along with the GNU C Library; if not, see
     19  1.1  mrg    <http://www.gnu.org/licenses/>.  */
     20  1.1  mrg 
     21  1.1  mrg #include "quadmath-imp.h"
     22  1.1  mrg 
     23  1.1  mrg /* Return the complex inverse hyperbolic sine of finite nonzero Z,
     24  1.1  mrg    with the imaginary part of the result subtracted from pi/2 if ADJ
     25  1.1  mrg    is nonzero.  */
     26  1.1  mrg 
     27  1.1  mrg __complex128
     28  1.1  mrg __quadmath_kernel_casinhq (__complex128 x, int adj)
     29  1.1  mrg {
     30  1.1  mrg   __complex128 res;
     31  1.1  mrg   __float128 rx, ix;
     32  1.1  mrg   __complex128 y;
     33  1.1  mrg 
     34  1.1  mrg   /* Avoid cancellation by reducing to the first quadrant.  */
     35  1.1  mrg   rx = fabsq (__real__ x);
     36  1.1  mrg   ix = fabsq (__imag__ x);
     37  1.1  mrg 
     38  1.1  mrg   if (rx >= 1 / FLT128_EPSILON || ix >= 1 / FLT128_EPSILON)
     39  1.1  mrg     {
     40  1.1  mrg       /* For large x in the first quadrant, x + csqrt (1 + x * x)
     41  1.1  mrg 	 is sufficiently close to 2 * x to make no significant
     42  1.1  mrg 	 difference to the result; avoid possible overflow from
     43  1.1  mrg 	 the squaring and addition.  */
     44  1.1  mrg       __real__ y = rx;
     45  1.1  mrg       __imag__ y = ix;
     46  1.1  mrg 
     47  1.1  mrg       if (adj)
     48  1.1  mrg 	{
     49  1.1  mrg 	  __float128 t = __real__ y;
     50  1.1  mrg 	  __real__ y = copysignq (__imag__ y, __imag__ x);
     51  1.1  mrg 	  __imag__ y = t;
     52  1.1  mrg 	}
     53  1.1  mrg 
     54  1.1  mrg       res = clogq (y);
     55  1.1  mrg       __real__ res += (__float128) M_LN2q;
     56  1.1  mrg     }
     57  1.1  mrg   else if (rx >= 0.5Q && ix < FLT128_EPSILON / 8)
     58  1.1  mrg     {
     59  1.1  mrg       __float128 s = hypotq (1, rx);
     60  1.1  mrg 
     61  1.1  mrg       __real__ res = logq (rx + s);
     62  1.1  mrg       if (adj)
     63  1.1  mrg 	__imag__ res = atan2q (s, __imag__ x);
     64  1.1  mrg       else
     65  1.1  mrg 	__imag__ res = atan2q (ix, s);
     66  1.1  mrg     }
     67  1.1  mrg   else if (rx < FLT128_EPSILON / 8 && ix >= 1.5Q)
     68  1.1  mrg     {
     69  1.1  mrg       __float128 s = sqrtq ((ix + 1) * (ix - 1));
     70  1.1  mrg 
     71  1.1  mrg       __real__ res = logq (ix + s);
     72  1.1  mrg       if (adj)
     73  1.1  mrg 	__imag__ res = atan2q (rx, copysignq (s, __imag__ x));
     74  1.1  mrg       else
     75  1.1  mrg 	__imag__ res = atan2q (s, rx);
     76  1.1  mrg     }
     77  1.1  mrg   else if (ix > 1 && ix < 1.5Q && rx < 0.5Q)
     78  1.1  mrg     {
     79  1.1  mrg       if (rx < FLT128_EPSILON * FLT128_EPSILON)
     80  1.1  mrg 	{
     81  1.1  mrg 	  __float128 ix2m1 = (ix + 1) * (ix - 1);
     82  1.1  mrg 	  __float128 s = sqrtq (ix2m1);
     83  1.1  mrg 
     84  1.1  mrg 	  __real__ res = log1pq (2 * (ix2m1 + ix * s)) / 2;
     85  1.1  mrg 	  if (adj)
     86  1.1  mrg 	    __imag__ res = atan2q (rx, copysignq (s, __imag__ x));
     87  1.1  mrg 	  else
     88  1.1  mrg 	    __imag__ res = atan2q (s, rx);
     89  1.1  mrg 	}
     90  1.1  mrg       else
     91  1.1  mrg 	{
     92  1.1  mrg 	  __float128 ix2m1 = (ix + 1) * (ix - 1);
     93  1.1  mrg 	  __float128 rx2 = rx * rx;
     94  1.1  mrg 	  __float128 f = rx2 * (2 + rx2 + 2 * ix * ix);
     95  1.1  mrg 	  __float128 d = sqrtq (ix2m1 * ix2m1 + f);
     96  1.1  mrg 	  __float128 dp = d + ix2m1;
     97  1.1  mrg 	  __float128 dm = f / dp;
     98  1.1  mrg 	  __float128 r1 = sqrtq ((dm + rx2) / 2);
     99  1.1  mrg 	  __float128 r2 = rx * ix / r1;
    100  1.1  mrg 
    101  1.1  mrg 	  __real__ res = log1pq (rx2 + dp + 2 * (rx * r1 + ix * r2)) / 2;
    102  1.1  mrg 	  if (adj)
    103  1.1  mrg 	    __imag__ res = atan2q (rx + r1, copysignq (ix + r2, __imag__ x));
    104  1.1  mrg 	  else
    105  1.1  mrg 	    __imag__ res = atan2q (ix + r2, rx + r1);
    106  1.1  mrg 	}
    107  1.1  mrg     }
    108  1.1  mrg   else if (ix == 1 && rx < 0.5Q)
    109  1.1  mrg     {
    110  1.1  mrg       if (rx < FLT128_EPSILON / 8)
    111  1.1  mrg 	{
    112  1.1  mrg 	  __real__ res = log1pq (2 * (rx + sqrtq (rx))) / 2;
    113  1.1  mrg 	  if (adj)
    114  1.1  mrg 	    __imag__ res = atan2q (sqrtq (rx), copysignq (1, __imag__ x));
    115  1.1  mrg 	  else
    116  1.1  mrg 	    __imag__ res = atan2q (1, sqrtq (rx));
    117  1.1  mrg 	}
    118  1.1  mrg       else
    119  1.1  mrg 	{
    120  1.1  mrg 	  __float128 d = rx * sqrtq (4 + rx * rx);
    121  1.1  mrg 	  __float128 s1 = sqrtq ((d + rx * rx) / 2);
    122  1.1  mrg 	  __float128 s2 = sqrtq ((d - rx * rx) / 2);
    123  1.1  mrg 
    124  1.1  mrg 	  __real__ res = log1pq (rx * rx + d + 2 * (rx * s1 + s2)) / 2;
    125  1.1  mrg 	  if (adj)
    126  1.1  mrg 	    __imag__ res = atan2q (rx + s1, copysignq (1 + s2, __imag__ x));
    127  1.1  mrg 	  else
    128  1.1  mrg 	    __imag__ res = atan2q (1 + s2, rx + s1);
    129  1.1  mrg 	}
    130  1.1  mrg     }
    131  1.1  mrg   else if (ix < 1 && rx < 0.5Q)
    132  1.1  mrg     {
    133  1.1  mrg       if (ix >= FLT128_EPSILON)
    134  1.1  mrg 	{
    135  1.1  mrg 	  if (rx < FLT128_EPSILON * FLT128_EPSILON)
    136  1.1  mrg 	    {
    137  1.1  mrg 	      __float128 onemix2 = (1 + ix) * (1 - ix);
    138  1.1  mrg 	      __float128 s = sqrtq (onemix2);
    139  1.1  mrg 
    140  1.1  mrg 	      __real__ res = log1pq (2 * rx / s) / 2;
    141  1.1  mrg 	      if (adj)
    142  1.1  mrg 		__imag__ res = atan2q (s, __imag__ x);
    143  1.1  mrg 	      else
    144  1.1  mrg 		__imag__ res = atan2q (ix, s);
    145  1.1  mrg 	    }
    146  1.1  mrg 	  else
    147  1.1  mrg 	    {
    148  1.1  mrg 	      __float128 onemix2 = (1 + ix) * (1 - ix);
    149  1.1  mrg 	      __float128 rx2 = rx * rx;
    150  1.1  mrg 	      __float128 f = rx2 * (2 + rx2 + 2 * ix * ix);
    151  1.1  mrg 	      __float128 d = sqrtq (onemix2 * onemix2 + f);
    152  1.1  mrg 	      __float128 dp = d + onemix2;
    153  1.1  mrg 	      __float128 dm = f / dp;
    154  1.1  mrg 	      __float128 r1 = sqrtq ((dp + rx2) / 2);
    155  1.1  mrg 	      __float128 r2 = rx * ix / r1;
    156  1.1  mrg 
    157  1.1  mrg 	      __real__ res = log1pq (rx2 + dm + 2 * (rx * r1 + ix * r2)) / 2;
    158  1.1  mrg 	      if (adj)
    159  1.1  mrg 		__imag__ res = atan2q (rx + r1, copysignq (ix + r2,
    160  1.1  mrg 							     __imag__ x));
    161  1.1  mrg 	      else
    162  1.1  mrg 		__imag__ res = atan2q (ix + r2, rx + r1);
    163  1.1  mrg 	    }
    164  1.1  mrg 	}
    165  1.1  mrg       else
    166  1.1  mrg 	{
    167  1.1  mrg 	  __float128 s = hypotq (1, rx);
    168  1.1  mrg 
    169  1.1  mrg 	  __real__ res = log1pq (2 * rx * (rx + s)) / 2;
    170  1.1  mrg 	  if (adj)
    171  1.1  mrg 	    __imag__ res = atan2q (s, __imag__ x);
    172  1.1  mrg 	  else
    173  1.1  mrg 	    __imag__ res = atan2q (ix, s);
    174  1.1  mrg 	}
    175  1.1  mrg       math_check_force_underflow_nonneg (__real__ res);
    176  1.1  mrg     }
    177  1.1  mrg   else
    178  1.1  mrg     {
    179  1.1  mrg       __real__ y = (rx - ix) * (rx + ix) + 1;
    180  1.1  mrg       __imag__ y = 2 * rx * ix;
    181  1.1  mrg 
    182  1.1  mrg       y = csqrtq (y);
    183  1.1  mrg 
    184  1.1  mrg       __real__ y += rx;
    185  1.1  mrg       __imag__ y += ix;
    186  1.1  mrg 
    187  1.1  mrg       if (adj)
    188  1.1  mrg 	{
    189  1.1  mrg 	  __float128 t = __real__ y;
    190  1.1  mrg 	  __real__ y = copysignq (__imag__ y, __imag__ x);
    191  1.1  mrg 	  __imag__ y = t;
    192  1.1  mrg 	}
    193  1.1  mrg 
    194  1.1  mrg       res = clogq (y);
    195  1.1  mrg     }
    196  1.1  mrg 
    197  1.1  mrg   /* Give results the correct sign for the original argument.  */
    198  1.1  mrg   __real__ res = copysignq (__real__ res, __real__ x);
    199  1.1  mrg   __imag__ res = copysignq (__imag__ res, (adj ? 1 : __imag__ x));
    200  1.1  mrg 
    201  1.1  mrg   return res;
    202  1.1  mrg }
    203