2 * Copyright (c) 2012 Stephen Montgomery-Smith <stephen@FreeBSD.ORG>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: head/lib/msun/src/catrigf.c 251121 2013-05-30 04:49:26Z das $
30 * The algorithm is very close to that in "Implementing the complex arcsine
31 * and arccosine functions using exception handling" by T. E. Hull, Thomas F.
32 * Fairgrieve, and Ping Tak Peter Tang, published in ACM Transactions on
33 * Mathematical Software, Volume 23 Issue 3, 1997, Pages 299-335,
34 * http://dl.acm.org/citation.cfm?id=275324.
36 * The code for catrig.c contains complete comments.
43 #include "math_private.h"
46 #define isinf(x) (fabsf(x) == INFINITY)
48 #define isnan(x) ((x) != (x))
49 #define raise_inexact() do { volatile float junk = 1 + tiny; } while(0)
51 #define signbit(x) (__builtin_signbitf(x))
56 FOUR_SQRT_MIN = 0x1p-61,
57 QUARTER_SQRT_MAX = 0x1p61,
58 m_e = 2.7182818285e0, /* 0xadf854.0p-22 */
59 m_ln2 = 6.9314718056e-1, /* 0xb17218.0p-24 */
60 pio2_hi = 1.5707962513e0, /* 0xc90fda.0p-23 */
61 RECIP_EPSILON = 1 / FLT_EPSILON,
62 SQRT_3_EPSILON = 5.9801995673e-4, /* 0x9cc471.0p-34 */
63 SQRT_6_EPSILON = 8.4572793338e-4, /* 0xddb3d7.0p-34 */
66 static const volatile float
67 pio2_lo = 7.5497899549e-8, /* 0xa22169.0p-47 */
70 static float complex clog_for_large_values(float complex z);
73 f(float a, float b, float hypot_a_b)
76 return ((hypot_a_b - b) / 2);
79 return (a * a / (hypot_a_b + b) / 2);
83 do_hard_work(float x, float y, float *rx, int *B_is_usable, float *B,
84 float *sqrt_A2my2, float *new_y)
96 if (A < A_crossover) {
97 if (y == 1 && x < FLT_EPSILON * FLT_EPSILON / 128) {
99 } else if (x >= FLT_EPSILON * fabsf(y - 1)) {
100 Am1 = f(x, 1 + y, R) + f(x, 1 - y, S);
101 *rx = log1pf(Am1 + sqrtf(Am1 * (A + 1)));
103 *rx = x / sqrtf((1 - y)*(1 + y));
105 *rx = log1pf((y - 1) + sqrtf((y - 1) * (y + 1)));
108 *rx = logf(A + sqrtf(A * A - 1));
113 if (y < FOUR_SQRT_MIN) {
115 *sqrt_A2my2 = A * (2 / FLT_EPSILON);
116 *new_y = y * (2 / FLT_EPSILON);
123 if (*B > B_crossover) {
125 if (y == 1 && x < FLT_EPSILON / 128) {
126 *sqrt_A2my2 = sqrtf(x) * sqrtf((A + y) / 2);
127 } else if (x >= FLT_EPSILON * fabsf(y - 1)) {
128 Amy = f(x, y + 1, R) + f(x, y - 1, S);
129 *sqrt_A2my2 = sqrtf(Amy * (A + y));
131 *sqrt_A2my2 = x * (4 / FLT_EPSILON / FLT_EPSILON) * y /
132 sqrtf((y + 1) * (y - 1));
133 *new_y = y * (4 / FLT_EPSILON / FLT_EPSILON);
135 *sqrt_A2my2 = sqrtf((1 - y) * (1 + y));
141 casinhf(float complex z)
143 float x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y;
152 if (isnan(x) || isnan(y)) {
154 return (cpackf(x, y + y));
156 return (cpackf(y, x + x));
158 return (cpackf(x + x, y));
159 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
162 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
164 w = clog_for_large_values(z) + m_ln2;
166 w = clog_for_large_values(-z) + m_ln2;
167 return (cpackf(copysignf(crealf(w), x),
168 copysignf(cimagf(w), y)));
171 if (x == 0 && y == 0)
176 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
179 do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y);
183 ry = atan2f(new_y, sqrt_A2my2);
184 return (cpackf(copysignf(rx, x), copysignf(ry, y)));
188 casinf(float complex z)
190 float complex w = casinhf(cpackf(cimagf(z), crealf(z)));
191 return (cpackf(cimagf(w), crealf(w)));
195 cacosf(float complex z)
197 float x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x;
209 if (isnan(x) || isnan(y)) {
211 return (cpackf(y + y, -INFINITY));
213 return (cpackf(x + x, -y));
214 if (x == 0) return (cpackf(pio2_hi + pio2_lo, y + y));
215 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
218 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
219 w = clog_for_large_values(z);
220 rx = fabsf(cimagf(w));
221 ry = crealf(w) + m_ln2;
224 return (cpackf(rx, ry));
227 if (x == 1 && y == 0)
228 return (cpackf(0, -y));
232 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
233 return (cpackf(pio2_hi - (x - pio2_lo), -y));
235 do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x);
243 rx = atan2f(sqrt_A2mx2, new_x);
245 rx = atan2f(sqrt_A2mx2, -new_x);
249 return (cpackf(rx, ry));
253 cacoshf(float complex z)
261 if (isnan(rx) && isnan(ry))
262 return (cpackf(ry, rx));
264 return (cpackf(fabsf(ry), rx));
266 return (cpackf(ry, ry));
267 return (cpackf(fabsf(ry), copysignf(rx, cimagf(z))));
271 clog_for_large_values(float complex z)
286 if (ax > FLT_MAX / 2) {
287 return (cpackf(logf(hypotf(x / m_e, y / m_e)) + 1,
291 if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN)
292 return (cpackf(logf(hypotf(x, y)), atan2f(y, x)));
294 return (cpackf(logf(ax * ax + ay * ay) / 2, atan2f(y, x)));
298 sum_squares(float x, float y)
307 real_part_reciprocal(float x, float y)
313 GET_FLOAT_WORD(hx, x);
314 ix = hx & 0x7f800000;
315 GET_FLOAT_WORD(hy, y);
316 iy = hy & 0x7f800000;
317 #define BIAS (FLT_MAX_EXP - 1)
318 #define CUTOFF (FLT_MANT_DIG / 2 + 1)
319 if (ix - iy >= CUTOFF << 23 || isinf(x))
321 if (iy - ix >= CUTOFF << 23)
323 if (ix <= (BIAS + FLT_MAX_EXP / 2 - CUTOFF) << 23)
324 return (x / (x * x + y * y));
325 SET_FLOAT_WORD(scale, 0x7f800000 - ix);
328 return (x / (x * x + y * y) * scale);
332 catanhf(float complex z)
334 float x, y, ax, ay, rx, ry;
341 if (y == 0 && ax <= 1)
342 return (cpackf(atanhf(x), y));
345 return (cpackf(x, atanf(y)));
347 if (isnan(x) || isnan(y)) {
349 return (cpackf(copysignf(0, x), y+y));
351 return (cpackf(copysignf(0, x),
352 copysignf(pio2_hi + pio2_lo, y)));
354 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
357 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
358 return (cpackf(real_part_reciprocal(x, y),
359 copysignf(pio2_hi + pio2_lo, y)));
362 if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
367 if (ax == 1 && ay < FLT_EPSILON)
368 rx = (logf(ay) - m_ln2) / -2;
370 rx = log1pf(4 * ax / sum_squares(ax - 1, ay)) / 4;
373 ry = atan2f(2, -ay) / 2;
374 else if (ay < FLT_EPSILON)
375 ry = atan2f(2 * ay, (1 - ax) * (1 + ax)) / 2;
377 ry = atan2f(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;
379 return (cpackf(copysignf(rx, x), copysignf(ry, y)));
383 catanf(float complex z)
385 float complex w = catanhf(cpackf(cimagf(z), crealf(z)));
386 return (cpackf(cimagf(w), crealf(w)));