1 1.2 rillig /* $NetBSD: catrigf.c,v 1.2 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 * See catrig.c for complete comments. 36 1.1 christos * 37 1.1 christos * XXX comments were removed automatically, and even short ones on the right 38 1.1 christos * of statements were removed (all of them), contrary to normal style. Only 39 1.1 christos * a few comments on the right of declarations remain. 40 1.1 christos */ 41 1.1 christos 42 1.1 christos #include <sys/cdefs.h> 43 1.1 christos #if 0 44 1.1 christos __FBSDID("$FreeBSD: head/lib/msun/src/catrigf.c 275819 2014-12-16 09:21:56Z ed $"); 45 1.1 christos #endif 46 1.2 rillig __RCSID("$NetBSD: catrigf.c,v 1.2 2022/04/19 20:32:16 rillig Exp $"); 47 1.1 christos 48 1.1 christos #include "namespace.h" 49 1.1 christos #ifdef __weak_alias 50 1.1 christos __weak_alias(casinf, _casinf) 51 1.1 christos #endif 52 1.1 christos #ifdef __weak_alias 53 1.1 christos __weak_alias(catanf, _catanf) 54 1.1 christos #endif 55 1.1 christos 56 1.1 christos 57 1.1 christos #include <complex.h> 58 1.1 christos #include <float.h> 59 1.1 christos 60 1.1 christos #include "math.h" 61 1.1 christos #include "math_private.h" 62 1.1 christos 63 1.1 christos #undef isinf 64 1.1 christos #define isinf(x) (fabsf(x) == INFINITY) 65 1.1 christos #undef isnan 66 1.1 christos #define isnan(x) ((x) != (x)) 67 1.2 rillig #define raise_inexact() do { volatile float junk __unused = /*LINTED*/1 + tiny; } while (0) 68 1.1 christos #undef signbit 69 1.1 christos #define signbit(x) (__builtin_signbitf(x)) 70 1.1 christos 71 1.1 christos static const float 72 1.1 christos A_crossover = 10, 73 1.1 christos B_crossover = 0.6417, 74 1.1 christos FOUR_SQRT_MIN = 0x1p-61, 75 1.1 christos QUARTER_SQRT_MAX = 0x1p61, 76 1.1 christos m_e = 2.7182818285e0, /* 0xadf854.0p-22 */ 77 1.1 christos m_ln2 = 6.9314718056e-1, /* 0xb17218.0p-24 */ 78 1.1 christos pio2_hi = 1.5707962513e0, /* 0xc90fda.0p-23 */ 79 1.1 christos RECIP_EPSILON = 1 / FLT_EPSILON, 80 1.1 christos SQRT_3_EPSILON = 5.9801995673e-4, /* 0x9cc471.0p-34 */ 81 1.1 christos SQRT_6_EPSILON = 8.4572793338e-4, /* 0xddb3d7.0p-34 */ 82 1.1 christos SQRT_MIN = 0x1p-63; 83 1.1 christos 84 1.1 christos static const volatile float 85 1.1 christos pio2_lo = 7.5497899549e-8, /* 0xa22169.0p-47 */ 86 1.1 christos tiny = 0x1p-100; 87 1.1 christos 88 1.1 christos static float complex clog_for_large_values(float complex z); 89 1.1 christos 90 1.1 christos static inline float 91 1.1 christos f(float a, float b, float hypot_a_b) 92 1.1 christos { 93 1.1 christos if (b < 0) 94 1.1 christos return ((hypot_a_b - b) / 2); 95 1.1 christos if (b == 0) 96 1.1 christos return (a / 2); 97 1.1 christos return (a * a / (hypot_a_b + b) / 2); 98 1.1 christos } 99 1.1 christos 100 1.1 christos static inline void 101 1.1 christos do_hard_work(float x, float y, float *rx, int *B_is_usable, float *B, 102 1.1 christos float *sqrt_A2my2, float *new_y) 103 1.1 christos { 104 1.1 christos float R, S, A; 105 1.1 christos float Am1, Amy; 106 1.1 christos 107 1.1 christos R = hypotf(x, y + 1); 108 1.1 christos S = hypotf(x, y - 1); 109 1.1 christos 110 1.1 christos A = (R + S) / 2; 111 1.1 christos if (A < 1) 112 1.1 christos A = 1; 113 1.1 christos 114 1.1 christos if (A < A_crossover) { 115 1.1 christos if (y == 1 && x < FLT_EPSILON * FLT_EPSILON / 128) { 116 1.1 christos *rx = sqrtf(x); 117 1.1 christos } else if (x >= FLT_EPSILON * fabsf(y - 1)) { 118 1.1 christos Am1 = f(x, 1 + y, R) + f(x, 1 - y, S); 119 1.1 christos *rx = log1pf(Am1 + sqrtf(Am1 * (A + 1))); 120 1.1 christos } else if (y < 1) { 121 1.1 christos *rx = x / sqrtf((1 - y) * (1 + y)); 122 1.1 christos } else { 123 1.1 christos *rx = log1pf((y - 1) + sqrtf((y - 1) * (y + 1))); 124 1.1 christos } 125 1.1 christos } else { 126 1.1 christos *rx = logf(A + sqrtf(A * A - 1)); 127 1.1 christos } 128 1.1 christos 129 1.1 christos *new_y = y; 130 1.1 christos 131 1.1 christos if (y < FOUR_SQRT_MIN) { 132 1.1 christos *B_is_usable = 0; 133 1.1 christos *sqrt_A2my2 = A * (2 / FLT_EPSILON); 134 1.1 christos *new_y = y * (2 / FLT_EPSILON); 135 1.1 christos return; 136 1.1 christos } 137 1.1 christos 138 1.1 christos *B = y / A; 139 1.1 christos *B_is_usable = 1; 140 1.1 christos 141 1.1 christos if (*B > B_crossover) { 142 1.1 christos *B_is_usable = 0; 143 1.1 christos if (y == 1 && x < FLT_EPSILON / 128) { 144 1.1 christos *sqrt_A2my2 = sqrtf(x) * sqrtf((A + y) / 2); 145 1.1 christos } else if (x >= FLT_EPSILON * fabsf(y - 1)) { 146 1.1 christos Amy = f(x, y + 1, R) + f(x, y - 1, S); 147 1.1 christos *sqrt_A2my2 = sqrtf(Amy * (A + y)); 148 1.1 christos } else if (y > 1) { 149 1.1 christos *sqrt_A2my2 = x * (4 / FLT_EPSILON / FLT_EPSILON) * y / 150 1.1 christos sqrtf((y + 1) * (y - 1)); 151 1.1 christos *new_y = y * (4 / FLT_EPSILON / FLT_EPSILON); 152 1.1 christos } else { 153 1.1 christos *sqrt_A2my2 = sqrtf((1 - y) * (1 + y)); 154 1.1 christos } 155 1.1 christos } 156 1.1 christos } 157 1.1 christos 158 1.1 christos float complex 159 1.1 christos casinhf(float complex z) 160 1.1 christos { 161 1.1 christos float x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y; 162 1.1 christos int B_is_usable; 163 1.1 christos float complex w; 164 1.1 christos 165 1.1 christos x = crealf(z); 166 1.1 christos y = cimagf(z); 167 1.1 christos ax = fabsf(x); 168 1.1 christos ay = fabsf(y); 169 1.1 christos 170 1.1 christos if (isnan(x) || isnan(y)) { 171 1.1 christos if (isinf(x)) 172 1.1 christos return (CMPLXF(x, y + y)); 173 1.1 christos if (isinf(y)) 174 1.1 christos return (CMPLXF(y, x + x)); 175 1.1 christos if (y == 0) 176 1.1 christos return (CMPLXF(x + x, y)); 177 1.1 christos return (CMPLXF(x + 0.0L + (y + 0), x + 0.0L + (y + 0))); 178 1.1 christos } 179 1.1 christos 180 1.1 christos if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) { 181 1.1 christos if (signbit(x) == 0) 182 1.1 christos w = clog_for_large_values(z) + m_ln2; 183 1.1 christos else 184 1.1 christos w = clog_for_large_values(-z) + m_ln2; 185 1.1 christos return (CMPLXF(copysignf(crealf(w), x), 186 1.1 christos copysignf(cimagf(w), y))); 187 1.1 christos } 188 1.1 christos 189 1.1 christos if (x == 0 && y == 0) 190 1.1 christos return (z); 191 1.1 christos 192 1.1 christos raise_inexact(); 193 1.1 christos 194 1.1 christos if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4) 195 1.1 christos return (z); 196 1.1 christos 197 1.1 christos do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y); 198 1.1 christos if (B_is_usable) 199 1.1 christos ry = asinf(B); 200 1.1 christos else 201 1.1 christos ry = atan2f(new_y, sqrt_A2my2); 202 1.1 christos return (CMPLXF(copysignf(rx, x), copysignf(ry, y))); 203 1.1 christos } 204 1.1 christos 205 1.1 christos float complex 206 1.1 christos casinf(float complex z) 207 1.1 christos { 208 1.1 christos float complex w = casinhf(CMPLXF(cimagf(z), crealf(z))); 209 1.1 christos 210 1.1 christos return (CMPLXF(cimagf(w), crealf(w))); 211 1.1 christos } 212 1.1 christos 213 1.1 christos float complex 214 1.1 christos cacosf(float complex z) 215 1.1 christos { 216 1.1 christos float x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x; 217 1.1 christos int sx, sy; 218 1.1 christos int B_is_usable; 219 1.1 christos float complex w; 220 1.1 christos 221 1.1 christos x = crealf(z); 222 1.1 christos y = cimagf(z); 223 1.1 christos sx = signbit(x); 224 1.1 christos sy = signbit(y); 225 1.1 christos ax = fabsf(x); 226 1.1 christos ay = fabsf(y); 227 1.1 christos 228 1.1 christos if (isnan(x) || isnan(y)) { 229 1.1 christos if (isinf(x)) 230 1.1 christos return (CMPLXF(y + y, -INFINITY)); 231 1.1 christos if (isinf(y)) 232 1.1 christos return (CMPLXF(x + x, -y)); 233 1.1 christos if (x == 0) 234 1.1 christos return (CMPLXF(pio2_hi + pio2_lo, y + y)); 235 1.1 christos return (CMPLXF(x + 0.0L + (y + 0), x + 0.0L + (y + 0))); 236 1.1 christos } 237 1.1 christos 238 1.1 christos if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) { 239 1.1 christos w = clog_for_large_values(z); 240 1.1 christos rx = fabsf(cimagf(w)); 241 1.1 christos ry = crealf(w) + m_ln2; 242 1.1 christos if (sy == 0) 243 1.1 christos ry = -ry; 244 1.1 christos return (CMPLXF(rx, ry)); 245 1.1 christos } 246 1.1 christos 247 1.1 christos if (x == 1 && y == 0) 248 1.1 christos return (CMPLXF(0, -y)); 249 1.1 christos 250 1.1 christos raise_inexact(); 251 1.1 christos 252 1.1 christos if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4) 253 1.1 christos return (CMPLXF(pio2_hi - (x - pio2_lo), -y)); 254 1.1 christos 255 1.1 christos do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x); 256 1.1 christos if (B_is_usable) { 257 1.1 christos if (sx == 0) 258 1.1 christos rx = acosf(B); 259 1.1 christos else 260 1.1 christos rx = acosf(-B); 261 1.1 christos } else { 262 1.1 christos if (sx == 0) 263 1.1 christos rx = atan2f(sqrt_A2mx2, new_x); 264 1.1 christos else 265 1.1 christos rx = atan2f(sqrt_A2mx2, -new_x); 266 1.1 christos } 267 1.1 christos if (sy == 0) 268 1.1 christos ry = -ry; 269 1.1 christos return (CMPLXF(rx, ry)); 270 1.1 christos } 271 1.1 christos 272 1.1 christos float complex 273 1.1 christos cacoshf(float complex z) 274 1.1 christos { 275 1.1 christos float complex w; 276 1.1 christos float rx, ry; 277 1.1 christos 278 1.1 christos w = cacosf(z); 279 1.1 christos rx = crealf(w); 280 1.1 christos ry = cimagf(w); 281 1.1 christos if (isnan(rx) && isnan(ry)) 282 1.1 christos return (CMPLXF(ry, rx)); 283 1.1 christos if (isnan(rx)) 284 1.1 christos return (CMPLXF(fabsf(ry), rx)); 285 1.1 christos if (isnan(ry)) 286 1.1 christos return (CMPLXF(ry, ry)); 287 1.1 christos return (CMPLXF(fabsf(ry), copysignf(rx, cimagf(z)))); 288 1.1 christos } 289 1.1 christos 290 1.1 christos static float complex 291 1.1 christos clog_for_large_values(float complex z) 292 1.1 christos { 293 1.1 christos float x, y; 294 1.1 christos float ax, ay, t; 295 1.1 christos 296 1.1 christos x = crealf(z); 297 1.1 christos y = cimagf(z); 298 1.1 christos ax = fabsf(x); 299 1.1 christos ay = fabsf(y); 300 1.1 christos if (ax < ay) { 301 1.1 christos t = ax; 302 1.1 christos ax = ay; 303 1.1 christos ay = t; 304 1.1 christos } 305 1.1 christos 306 1.1 christos if (ax > FLT_MAX / 2) 307 1.1 christos return (CMPLXF(logf(hypotf(x / m_e, y / m_e)) + 1, 308 1.1 christos atan2f(y, x))); 309 1.1 christos 310 1.1 christos if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN) 311 1.1 christos return (CMPLXF(logf(hypotf(x, y)), atan2f(y, x))); 312 1.1 christos 313 1.1 christos return (CMPLXF(logf(ax * ax + ay * ay) / 2, atan2f(y, x))); 314 1.1 christos } 315 1.1 christos 316 1.1 christos static inline float 317 1.1 christos sum_squares(float x, float y) 318 1.1 christos { 319 1.1 christos 320 1.1 christos if (y < SQRT_MIN) 321 1.1 christos return (x * x); 322 1.1 christos 323 1.1 christos return (x * x + y * y); 324 1.1 christos } 325 1.1 christos 326 1.1 christos static inline float 327 1.1 christos real_part_reciprocal(float x, float y) 328 1.1 christos { 329 1.1 christos float scale; 330 1.1 christos uint32_t hx, hy; 331 1.1 christos int32_t ix, iy; 332 1.1 christos 333 1.1 christos GET_FLOAT_WORD(hx, x); 334 1.1 christos ix = hx & 0x7f800000; 335 1.1 christos GET_FLOAT_WORD(hy, y); 336 1.1 christos iy = hy & 0x7f800000; 337 1.1 christos #define BIAS (FLT_MAX_EXP - 1) 338 1.1 christos #define CUTOFF (FLT_MANT_DIG / 2 + 1) 339 1.1 christos if (ix - iy >= CUTOFF << 23 || isinf(x)) 340 1.1 christos return (1 / x); 341 1.1 christos if (iy - ix >= CUTOFF << 23) 342 1.1 christos return (x / y / y); 343 1.1 christos if (ix <= (BIAS + FLT_MAX_EXP / 2 - CUTOFF) << 23) 344 1.1 christos return (x / (x * x + y * y)); 345 1.1 christos SET_FLOAT_WORD(scale, 0x7f800000 - ix); 346 1.1 christos x *= scale; 347 1.1 christos y *= scale; 348 1.1 christos return (x / (x * x + y * y) * scale); 349 1.1 christos } 350 1.1 christos 351 1.1 christos float complex 352 1.1 christos catanhf(float complex z) 353 1.1 christos { 354 1.1 christos float x, y, ax, ay, rx, ry; 355 1.1 christos 356 1.1 christos x = crealf(z); 357 1.1 christos y = cimagf(z); 358 1.1 christos ax = fabsf(x); 359 1.1 christos ay = fabsf(y); 360 1.1 christos 361 1.1 christos if (y == 0 && ax <= 1) 362 1.1 christos return (CMPLXF(atanhf(x), y)); 363 1.1 christos 364 1.1 christos if (x == 0) 365 1.1 christos return (CMPLXF(x, atanf(y))); 366 1.1 christos 367 1.1 christos if (isnan(x) || isnan(y)) { 368 1.1 christos if (isinf(x)) 369 1.1 christos return (CMPLXF(copysignf(0, x), y + y)); 370 1.1 christos if (isinf(y)) 371 1.1 christos return (CMPLXF(copysignf(0, x), 372 1.1 christos copysignf(pio2_hi + pio2_lo, y))); 373 1.1 christos return (CMPLXF(x + 0.0L + (y + 0), x + 0.0L + (y + 0))); 374 1.1 christos } 375 1.1 christos 376 1.1 christos if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) 377 1.1 christos return (CMPLXF(real_part_reciprocal(x, y), 378 1.1 christos copysignf(pio2_hi + pio2_lo, y))); 379 1.1 christos 380 1.1 christos if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) { 381 1.1 christos raise_inexact(); 382 1.1 christos return (z); 383 1.1 christos } 384 1.1 christos 385 1.1 christos if (ax == 1 && ay < FLT_EPSILON) 386 1.1 christos rx = (m_ln2 - logf(ay)) / 2; 387 1.1 christos else 388 1.1 christos rx = log1pf(4 * ax / sum_squares(ax - 1, ay)) / 4; 389 1.1 christos 390 1.1 christos if (ax == 1) 391 1.1 christos ry = atan2f(2, -ay) / 2; 392 1.1 christos else if (ay < FLT_EPSILON) 393 1.1 christos ry = atan2f(2 * ay, (1 - ax) * (1 + ax)) / 2; 394 1.1 christos else 395 1.1 christos ry = atan2f(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2; 396 1.1 christos 397 1.1 christos return (CMPLXF(copysignf(rx, x), copysignf(ry, y))); 398 1.1 christos } 399 1.1 christos 400 1.1 christos float complex 401 1.1 christos catanf(float complex z) 402 1.1 christos { 403 1.1 christos float complex w = catanhf(CMPLXF(cimagf(z), crealf(z))); 404 1.1 christos 405 1.1 christos return (CMPLXF(cimagf(w), crealf(w))); 406 1.1 christos } 407