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