/math/k_casinhl.c

https://gitlab.com/Namal/glibc · C · 219 lines · 167 code · 22 blank · 30 comment · 35 complexity · f1e610c303309ecfec27652e5a516e55 MD5 · raw file

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