catrigf.c revision 251121
1/*- 2 * Copyright (c) 2012 Stephen Montgomery-Smith <stephen@FreeBSD.ORG> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27/* 28 * The algorithm is very close to that in "Implementing the complex arcsine 29 * and arccosine functions using exception handling" by T. E. Hull, Thomas F. 30 * Fairgrieve, and Ping Tak Peter Tang, published in ACM Transactions on 31 * Mathematical Software, Volume 23 Issue 3, 1997, Pages 299-335, 32 * http://dl.acm.org/citation.cfm?id=275324. 33 * 34 * The code for catrig.c contains complete comments. 35 */ 36 37#include <sys/cdefs.h> 38__FBSDID("$FreeBSD: head/lib/msun/src/catrigf.c 251121 2013-05-30 04:49:26Z das $"); 39 40#include <complex.h> 41#include <float.h> 42 43#include "math.h" 44#include "math_private.h" 45 46#undef isinf 47#define isinf(x) (fabsf(x) == INFINITY) 48#undef isnan 49#define isnan(x) ((x) != (x)) 50#define raise_inexact() do { volatile float junk = 1 + tiny; } while(0) 51#undef signbit 52#define signbit(x) (__builtin_signbitf(x)) 53 54static const float 55A_crossover = 10, 56B_crossover = 0.6417, 57FOUR_SQRT_MIN = 0x1p-61, 58QUARTER_SQRT_MAX = 0x1p61, 59m_e = 2.7182818285e0, /* 0xadf854.0p-22 */ 60m_ln2 = 6.9314718056e-1, /* 0xb17218.0p-24 */ 61pio2_hi = 1.5707962513e0, /* 0xc90fda.0p-23 */ 62RECIP_EPSILON = 1 / FLT_EPSILON, 63SQRT_3_EPSILON = 5.9801995673e-4, /* 0x9cc471.0p-34 */ 64SQRT_6_EPSILON = 8.4572793338e-4, /* 0xddb3d7.0p-34 */ 65SQRT_MIN = 0x1p-63; 66 67static const volatile float 68pio2_lo = 7.5497899549e-8, /* 0xa22169.0p-47 */ 69tiny = 0x1p-100; 70 71static float complex clog_for_large_values(float complex z); 72 73static inline float 74f(float a, float b, float hypot_a_b) 75{ 76 if (b < 0) 77 return ((hypot_a_b - b) / 2); 78 if (b == 0) 79 return (a / 2); 80 return (a * a / (hypot_a_b + b) / 2); 81} 82 83static inline void 84do_hard_work(float x, float y, float *rx, int *B_is_usable, float *B, 85 float *sqrt_A2my2, float *new_y) 86{ 87 float R, S, A; 88 float Am1, Amy; 89 90 R = hypotf(x, y + 1); 91 S = hypotf(x, y - 1); 92 93 A = (R + S) / 2; 94 if (A < 1) 95 A = 1; 96 97 if (A < A_crossover) { 98 if (y == 1 && x < FLT_EPSILON * FLT_EPSILON / 128) { 99 *rx = sqrtf(x); 100 } else if (x >= FLT_EPSILON * fabsf(y - 1)) { 101 Am1 = f(x, 1 + y, R) + f(x, 1 - y, S); 102 *rx = log1pf(Am1 + sqrtf(Am1 * (A + 1))); 103 } else if (y < 1) { 104 *rx = x / sqrtf((1 - y)*(1 + y)); 105 } else { 106 *rx = log1pf((y - 1) + sqrtf((y - 1) * (y + 1))); 107 } 108 } else { 109 *rx = logf(A + sqrtf(A * A - 1)); 110 } 111 112 *new_y = y; 113 114 if (y < FOUR_SQRT_MIN) { 115 *B_is_usable = 0; 116 *sqrt_A2my2 = A * (2 / FLT_EPSILON); 117 *new_y = y * (2 / FLT_EPSILON); 118 return; 119 } 120 121 *B = y / A; 122 *B_is_usable = 1; 123 124 if (*B > B_crossover) { 125 *B_is_usable = 0; 126 if (y == 1 && x < FLT_EPSILON / 128) { 127 *sqrt_A2my2 = sqrtf(x) * sqrtf((A + y) / 2); 128 } else if (x >= FLT_EPSILON * fabsf(y - 1)) { 129 Amy = f(x, y + 1, R) + f(x, y - 1, S); 130 *sqrt_A2my2 = sqrtf(Amy * (A + y)); 131 } else if (y > 1) { 132 *sqrt_A2my2 = x * (4 / FLT_EPSILON / FLT_EPSILON) * y / 133 sqrtf((y + 1) * (y - 1)); 134 *new_y = y * (4 / FLT_EPSILON / FLT_EPSILON); 135 } else { 136 *sqrt_A2my2 = sqrtf((1 - y) * (1 + y)); 137 } 138 } 139} 140 141float complex 142casinhf(float complex z) 143{ 144 float x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y; 145 int B_is_usable; 146 float complex w; 147 148 x = crealf(z); 149 y = cimagf(z); 150 ax = fabsf(x); 151 ay = fabsf(y); 152 153 if (isnan(x) || isnan(y)) { 154 if (isinf(x)) 155 return (cpackf(x, y + y)); 156 if (isinf(y)) 157 return (cpackf(y, x + x)); 158 if (y == 0) 159 return (cpackf(x + x, y)); 160 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0))); 161 } 162 163 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) { 164 if (signbit(x) == 0) 165 w = clog_for_large_values(z) + m_ln2; 166 else 167 w = clog_for_large_values(-z) + m_ln2; 168 return (cpackf(copysignf(crealf(w), x), 169 copysignf(cimagf(w), y))); 170 } 171 172 if (x == 0 && y == 0) 173 return (z); 174 175 raise_inexact(); 176 177 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4) 178 return (z); 179 180 do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y); 181 if (B_is_usable) 182 ry = asinf(B); 183 else 184 ry = atan2f(new_y, sqrt_A2my2); 185 return (cpackf(copysignf(rx, x), copysignf(ry, y))); 186} 187 188float complex 189casinf(float complex z) 190{ 191 float complex w = casinhf(cpackf(cimagf(z), crealf(z))); 192 return (cpackf(cimagf(w), crealf(w))); 193} 194 195float complex 196cacosf(float complex z) 197{ 198 float x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x; 199 int sx, sy; 200 int B_is_usable; 201 float complex w; 202 203 x = crealf(z); 204 y = cimagf(z); 205 sx = signbit(x); 206 sy = signbit(y); 207 ax = fabsf(x); 208 ay = fabsf(y); 209 210 if (isnan(x) || isnan(y)) { 211 if (isinf(x)) 212 return (cpackf(y + y, -INFINITY)); 213 if (isinf(y)) 214 return (cpackf(x + x, -y)); 215 if (x == 0) return (cpackf(pio2_hi + pio2_lo, y + y)); 216 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0))); 217 } 218 219 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) { 220 w = clog_for_large_values(z); 221 rx = fabsf(cimagf(w)); 222 ry = crealf(w) + m_ln2; 223 if (sy == 0) 224 ry = -ry; 225 return (cpackf(rx, ry)); 226 } 227 228 if (x == 1 && y == 0) 229 return (cpackf(0, -y)); 230 231 raise_inexact(); 232 233 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4) 234 return (cpackf(pio2_hi - (x - pio2_lo), -y)); 235 236 do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x); 237 if (B_is_usable) { 238 if (sx==0) 239 rx = acosf(B); 240 else 241 rx = acosf(-B); 242 } else { 243 if (sx==0) 244 rx = atan2f(sqrt_A2mx2, new_x); 245 else 246 rx = atan2f(sqrt_A2mx2, -new_x); 247 } 248 if (sy==0) 249 ry = -ry; 250 return (cpackf(rx, ry)); 251} 252 253float complex 254cacoshf(float complex z) 255{ 256 float complex w; 257 float rx, ry; 258 259 w = cacosf(z); 260 rx = crealf(w); 261 ry = cimagf(w); 262 if (isnan(rx) && isnan(ry)) 263 return (cpackf(ry, rx)); 264 if (isnan(rx)) 265 return (cpackf(fabsf(ry), rx)); 266 if (isnan(ry)) 267 return (cpackf(ry, ry)); 268 return (cpackf(fabsf(ry), copysignf(rx, cimagf(z)))); 269} 270 271static float complex 272clog_for_large_values(float complex z) 273{ 274 float x, y; 275 float ax, ay, t; 276 277 x = crealf(z); 278 y = cimagf(z); 279 ax = fabsf(x); 280 ay = fabsf(y); 281 if (ax < ay) { 282 t = ax; 283 ax = ay; 284 ay = t; 285 } 286 287 if (ax > FLT_MAX / 2) { 288 return (cpackf(logf(hypotf(x / m_e, y / m_e)) + 1, 289 atan2f(y, x))); 290 } 291 292 if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN) 293 return (cpackf(logf(hypotf(x, y)), atan2f(y, x))); 294 295 return (cpackf(logf(ax * ax + ay * ay) / 2, atan2f(y, x))); 296} 297 298static inline float 299sum_squares(float x, float y) 300{ 301 302 if (y < SQRT_MIN) 303 return (x*x); 304 return (x*x + y*y); 305} 306 307static inline float 308real_part_reciprocal(float x, float y) 309{ 310 float scale; 311 uint32_t hx, hy; 312 int32_t ix, iy; 313 314 GET_FLOAT_WORD(hx, x); 315 ix = hx & 0x7f800000; 316 GET_FLOAT_WORD(hy, y); 317 iy = hy & 0x7f800000; 318#define BIAS (FLT_MAX_EXP - 1) 319#define CUTOFF (FLT_MANT_DIG / 2 + 1) 320 if (ix - iy >= CUTOFF << 23 || isinf(x)) 321 return (1/x); 322 if (iy - ix >= CUTOFF << 23) 323 return (x/y/y); 324 if (ix <= (BIAS + FLT_MAX_EXP / 2 - CUTOFF) << 23) 325 return (x / (x * x + y * y)); 326 SET_FLOAT_WORD(scale, 0x7f800000 - ix); 327 x *= scale; 328 y *= scale; 329 return (x / (x * x + y * y) * scale); 330} 331 332float complex 333catanhf(float complex z) 334{ 335 float x, y, ax, ay, rx, ry; 336 337 x = crealf(z); 338 y = cimagf(z); 339 ax = fabsf(x); 340 ay = fabsf(y); 341 342 if (y == 0 && ax <= 1) 343 return (cpackf(atanhf(x), y)); 344 345 if (x == 0) 346 return (cpackf(x, atanf(y))); 347 348 if (isnan(x) || isnan(y)) { 349 if (isinf(x)) 350 return (cpackf(copysignf(0, x), y+y)); 351 if (isinf(y)) { 352 return (cpackf(copysignf(0, x), 353 copysignf(pio2_hi + pio2_lo, y))); 354 } 355 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0))); 356 } 357 358 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) { 359 return (cpackf(real_part_reciprocal(x, y), 360 copysignf(pio2_hi + pio2_lo, y))); 361 } 362 363 if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) { 364 raise_inexact(); 365 return (z); 366 } 367 368 if (ax == 1 && ay < FLT_EPSILON) 369 rx = (logf(ay) - m_ln2) / -2; 370 else 371 rx = log1pf(4 * ax / sum_squares(ax - 1, ay)) / 4; 372 373 if (ax == 1) 374 ry = atan2f(2, -ay) / 2; 375 else if (ay < FLT_EPSILON) 376 ry = atan2f(2 * ay, (1 - ax) * (1 + ax)) / 2; 377 else 378 ry = atan2f(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2; 379 380 return (cpackf(copysignf(rx, x), copysignf(ry, y))); 381} 382 383float complex 384catanf(float complex z) 385{ 386 float complex w = catanhf(cpackf(cimagf(z), crealf(z))); 387 return (cpackf(cimagf(w), crealf(w))); 388} 389