1 /*- 2 * Copyright (c) 2012 Stephen Montgomery-Smith <stephen@FreeBSD.ORG> 3 * Copyright (c) 2017 Mahdi Mokhtari <mmokhi@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * The algorithm is very close to that in "Implementing the complex arcsine 30 * and arccosine functions using exception handling" by T. E. Hull, Thomas F. 31 * Fairgrieve, and Ping Tak Peter Tang, published in ACM Transactions on 32 * Mathematical Software, Volume 23 Issue 3, 1997, Pages 299-335, 33 * http://dl.acm.org/citation.cfm?id=275324. 34 * 35 * See catrig.c for complete comments. 36 * 37 * XXX comments were removed automatically, and even short ones on the right 38 * of statements were removed (all of them), contrary to normal style. Only 39 * a few comments on the right of declarations remain. 40 */ 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <complex.h> 46 #include <float.h> 47 48 #include "invtrig.h" 49 #include "math.h" 50 #include "math_private.h" 51 52 #undef isinf 53 #define isinf(x) (fabsl(x) == INFINITY) 54 #undef isnan 55 #define isnan(x) ((x) != (x)) 56 #define raise_inexact() do { volatile float junk = 1 + tiny; } while(0) 57 #undef signbit 58 #define signbit(x) (__builtin_signbitl(x)) 59 60 static const long double 61 A_crossover = 10, 62 B_crossover = 0.6417, 63 FOUR_SQRT_MIN = 0x1p-8189L, 64 QUARTER_SQRT_MAX = 0x1p8189L, 65 RECIP_EPSILON = 1 / LDBL_EPSILON, 66 SQRT_MIN = 0x1p-8191L; 67 68 #if LDBL_MANT_DIG == 64 69 static const union IEEEl2bits 70 um_e = LD80C(0xadf85458a2bb4a9b, 1, 2.71828182845904523536e+0L), 71 um_ln2 = LD80C(0xb17217f7d1cf79ac, -1, 6.93147180559945309417e-1L); 72 #define m_e um_e.e 73 #define m_ln2 um_ln2.e 74 static const long double 75 /* The next 2 literals for non-i386. Misrounding them on i386 is harmless. */ 76 SQRT_3_EPSILON = 5.70316273435758915310e-10, /* 0x9cc470a0490973e8.0p-94 */ 77 SQRT_6_EPSILON = 8.06549008734932771664e-10; /* 0xddb3d742c265539e.0p-94 */ 78 #elif LDBL_MANT_DIG == 113 79 static const long double 80 m_e = 2.71828182845904523536028747135266250e0L, /* 0x15bf0a8b1457695355fb8ac404e7a.0p-111 */ 81 m_ln2 = 6.93147180559945309417232121458176568e-1L, /* 0x162e42fefa39ef35793c7673007e6.0p-113 */ 82 SQRT_3_EPSILON = 2.40370335797945490975336727199878124e-17, /* 0x1bb67ae8584caa73b25742d7078b8.0p-168 */ 83 SQRT_6_EPSILON = 3.39934988877629587239082586223300391e-17; /* 0x13988e1409212e7d0321914321a55.0p-167 */ 84 #else 85 #error "Unsupported long double format" 86 #endif 87 88 static const volatile float 89 tiny = 0x1p-100; 90 91 static long double complex clog_for_large_values(long double complex z); 92 93 static inline long double 94 f(long double a, long double b, long double hypot_a_b) 95 { 96 if (b < 0) 97 return ((hypot_a_b - b) / 2); 98 if (b == 0) 99 return (a / 2); 100 return (a * a / (hypot_a_b + b) / 2); 101 } 102 103 static inline void 104 do_hard_work(long double x, long double y, long double *rx, int *B_is_usable, 105 long double *B, long double *sqrt_A2my2, long double *new_y) 106 { 107 long double R, S, A; 108 long double Am1, Amy; 109 110 R = hypotl(x, y + 1); 111 S = hypotl(x, y - 1); 112 113 A = (R + S) / 2; 114 if (A < 1) 115 A = 1; 116 117 if (A < A_crossover) { 118 if (y == 1 && x < LDBL_EPSILON * LDBL_EPSILON / 128) { 119 *rx = sqrtl(x); 120 } else if (x >= LDBL_EPSILON * fabsl(y - 1)) { 121 Am1 = f(x, 1 + y, R) + f(x, 1 - y, S); 122 *rx = log1pl(Am1 + sqrtl(Am1 * (A + 1))); 123 } else if (y < 1) { 124 *rx = x / sqrtl((1 - y) * (1 + y)); 125 } else { 126 *rx = log1pl((y - 1) + sqrtl((y - 1) * (y + 1))); 127 } 128 } else { 129 *rx = logl(A + sqrtl(A * A - 1)); 130 } 131 132 *new_y = y; 133 134 if (y < FOUR_SQRT_MIN) { 135 *B_is_usable = 0; 136 *sqrt_A2my2 = A * (2 / LDBL_EPSILON); 137 *new_y = y * (2 / LDBL_EPSILON); 138 return; 139 } 140 141 *B = y / A; 142 *B_is_usable = 1; 143 144 if (*B > B_crossover) { 145 *B_is_usable = 0; 146 if (y == 1 && x < LDBL_EPSILON / 128) { 147 *sqrt_A2my2 = sqrtl(x) * sqrtl((A + y) / 2); 148 } else if (x >= LDBL_EPSILON * fabsl(y - 1)) { 149 Amy = f(x, y + 1, R) + f(x, y - 1, S); 150 *sqrt_A2my2 = sqrtl(Amy * (A + y)); 151 } else if (y > 1) { 152 *sqrt_A2my2 = x * (4 / LDBL_EPSILON / LDBL_EPSILON) * y / 153 sqrtl((y + 1) * (y - 1)); 154 *new_y = y * (4 / LDBL_EPSILON / LDBL_EPSILON); 155 } else { 156 *sqrt_A2my2 = sqrtl((1 - y) * (1 + y)); 157 } 158 } 159 } 160 161 long double complex 162 casinhl(long double complex z) 163 { 164 long double x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y; 165 int B_is_usable; 166 long double complex w; 167 168 x = creall(z); 169 y = cimagl(z); 170 ax = fabsl(x); 171 ay = fabsl(y); 172 173 if (isnan(x) || isnan(y)) { 174 if (isinf(x)) 175 return (CMPLXL(x, y + y)); 176 if (isinf(y)) 177 return (CMPLXL(y, x + x)); 178 if (y == 0) 179 return (CMPLXL(x + x, y)); 180 return (CMPLXL(x + 0.0L + (y + 0), x + 0.0L + (y + 0))); 181 } 182 183 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) { 184 if (signbit(x) == 0) 185 w = clog_for_large_values(z) + m_ln2; 186 else 187 w = clog_for_large_values(-z) + m_ln2; 188 return (CMPLXL(copysignl(creall(w), x), 189 copysignl(cimagl(w), y))); 190 } 191 192 if (x == 0 && y == 0) 193 return (z); 194 195 raise_inexact(); 196 197 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4) 198 return (z); 199 200 do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y); 201 if (B_is_usable) 202 ry = asinl(B); 203 else 204 ry = atan2l(new_y, sqrt_A2my2); 205 return (CMPLXL(copysignl(rx, x), copysignl(ry, y))); 206 } 207 208 long double complex 209 casinl(long double complex z) 210 { 211 long double complex w; 212 213 w = casinhl(CMPLXL(cimagl(z), creall(z))); 214 return (CMPLXL(cimagl(w), creall(w))); 215 } 216 217 long double complex 218 cacosl(long double complex z) 219 { 220 long double x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x; 221 int sx, sy; 222 int B_is_usable; 223 long double complex w; 224 225 x = creall(z); 226 y = cimagl(z); 227 sx = signbit(x); 228 sy = signbit(y); 229 ax = fabsl(x); 230 ay = fabsl(y); 231 232 if (isnan(x) || isnan(y)) { 233 if (isinf(x)) 234 return (CMPLXL(y + y, -INFINITY)); 235 if (isinf(y)) 236 return (CMPLXL(x + x, -y)); 237 if (x == 0) 238 return (CMPLXL(pio2_hi + pio2_lo, y + y)); 239 return (CMPLXL(x + 0.0L + (y + 0), x + 0.0L + (y + 0))); 240 } 241 242 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) { 243 w = clog_for_large_values(z); 244 rx = fabsl(cimagl(w)); 245 ry = creall(w) + m_ln2; 246 if (sy == 0) 247 ry = -ry; 248 return (CMPLXL(rx, ry)); 249 } 250 251 if (x == 1 && y == 0) 252 return (CMPLXL(0, -y)); 253 254 raise_inexact(); 255 256 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4) 257 return (CMPLXL(pio2_hi - (x - pio2_lo), -y)); 258 259 do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x); 260 if (B_is_usable) { 261 if (sx == 0) 262 rx = acosl(B); 263 else 264 rx = acosl(-B); 265 } else { 266 if (sx == 0) 267 rx = atan2l(sqrt_A2mx2, new_x); 268 else 269 rx = atan2l(sqrt_A2mx2, -new_x); 270 } 271 if (sy == 0) 272 ry = -ry; 273 return (CMPLXL(rx, ry)); 274 } 275 276 long double complex 277 cacoshl(long double complex z) 278 { 279 long double complex w; 280 long double rx, ry; 281 282 w = cacosl(z); 283 rx = creall(w); 284 ry = cimagl(w); 285 if (isnan(rx) && isnan(ry)) 286 return (CMPLXL(ry, rx)); 287 if (isnan(rx)) 288 return (CMPLXL(fabsl(ry), rx)); 289 if (isnan(ry)) 290 return (CMPLXL(ry, ry)); 291 return (CMPLXL(fabsl(ry), copysignl(rx, cimagl(z)))); 292 } 293 294 static long double complex 295 clog_for_large_values(long double complex z) 296 { 297 long double x, y; 298 long double ax, ay, t; 299 300 x = creall(z); 301 y = cimagl(z); 302 ax = fabsl(x); 303 ay = fabsl(y); 304 if (ax < ay) { 305 t = ax; 306 ax = ay; 307 ay = t; 308 } 309 310 if (ax > LDBL_MAX / 2) 311 return (CMPLXL(logl(hypotl(x / m_e, y / m_e)) + 1, 312 atan2l(y, x))); 313 314 if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN) 315 return (CMPLXL(logl(hypotl(x, y)), atan2l(y, x))); 316 317 return (CMPLXL(logl(ax * ax + ay * ay) / 2, atan2l(y, x))); 318 } 319 320 static inline long double 321 sum_squares(long double x, long double y) 322 { 323 324 if (y < SQRT_MIN) 325 return (x * x); 326 327 return (x * x + y * y); 328 } 329 330 static inline long double 331 real_part_reciprocal(long double x, long double y) 332 { 333 long double scale; 334 uint16_t hx, hy; 335 int16_t ix, iy; 336 337 GET_LDBL_EXPSIGN(hx, x); 338 ix = hx & 0x7fff; 339 GET_LDBL_EXPSIGN(hy, y); 340 iy = hy & 0x7fff; 341 #define BIAS (LDBL_MAX_EXP - 1) 342 #define CUTOFF (LDBL_MANT_DIG / 2 + 1) 343 if (ix - iy >= CUTOFF || isinf(x)) 344 return (1 / x); 345 if (iy - ix >= CUTOFF) 346 return (x / y / y); 347 if (ix <= BIAS + LDBL_MAX_EXP / 2 - CUTOFF) 348 return (x / (x * x + y * y)); 349 scale = 1; 350 SET_LDBL_EXPSIGN(scale, 0x7fff - ix); 351 x *= scale; 352 y *= scale; 353 return (x / (x * x + y * y) * scale); 354 } 355 356 long double complex 357 catanhl(long double complex z) 358 { 359 long double x, y, ax, ay, rx, ry; 360 361 x = creall(z); 362 y = cimagl(z); 363 ax = fabsl(x); 364 ay = fabsl(y); 365 366 if (y == 0 && ax <= 1) 367 return (CMPLXL(atanhl(x), y)); 368 369 if (x == 0) 370 return (CMPLXL(x, atanl(y))); 371 372 if (isnan(x) || isnan(y)) { 373 if (isinf(x)) 374 return (CMPLXL(copysignl(0, x), y + y)); 375 if (isinf(y)) 376 return (CMPLXL(copysignl(0, x), 377 copysignl(pio2_hi + pio2_lo, y))); 378 return (CMPLXL(x + 0.0L + (y + 0), x + 0.0L + (y + 0))); 379 } 380 381 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) 382 return (CMPLXL(real_part_reciprocal(x, y), 383 copysignl(pio2_hi + pio2_lo, y))); 384 385 if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) { 386 raise_inexact(); 387 return (z); 388 } 389 390 if (ax == 1 && ay < LDBL_EPSILON) 391 rx = (m_ln2 - logl(ay)) / 2; 392 else 393 rx = log1pl(4 * ax / sum_squares(ax - 1, ay)) / 4; 394 395 if (ax == 1) 396 ry = atan2l(2, -ay) / 2; 397 else if (ay < LDBL_EPSILON) 398 ry = atan2l(2 * ay, (1 - ax) * (1 + ax)) / 2; 399 else 400 ry = atan2l(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2; 401 402 return (CMPLXL(copysignl(rx, x), copysignl(ry, y))); 403 } 404 405 long double complex 406 catanl(long double complex z) 407 { 408 long double complex w; 409 410 w = catanhl(CMPLXL(cimagl(z), creall(z))); 411 return (CMPLXL(cimagl(w), creall(w))); 412 } 413