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 251404 2013-06-05 05:33:01Z 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 * See catrig.c for complete comments.
38 * XXX comments were removed automatically, and even short ones on the right
39 * of statements were removed (all of them), contrary to normal style. Only
40 * a few comments on the right of declarations remain.
47 #include "math_private.h"
50 #define isinf(x) (fabsf(x) == INFINITY)
52 #define isnan(x) ((x) != (x))
53 #define raise_inexact() do { volatile float junk = 1 + tiny; } while(0)
55 #define signbit(x) (__builtin_signbitf(x))
60 FOUR_SQRT_MIN = 0x1p-61,
61 QUARTER_SQRT_MAX = 0x1p61,
62 m_e = 2.7182818285e0, /* 0xadf854.0p-22 */
63 m_ln2 = 6.9314718056e-1, /* 0xb17218.0p-24 */
64 pio2_hi = 1.5707962513e0, /* 0xc90fda.0p-23 */
65 RECIP_EPSILON = 1 / FLT_EPSILON,
66 SQRT_3_EPSILON = 5.9801995673e-4, /* 0x9cc471.0p-34 */
67 SQRT_6_EPSILON = 8.4572793338e-4, /* 0xddb3d7.0p-34 */
70 static const volatile float
71 pio2_lo = 7.5497899549e-8, /* 0xa22169.0p-47 */
74 static float complex clog_for_large_values(float complex z);
77 f(float a, float b, float hypot_a_b)
80 return ((hypot_a_b - b) / 2);
83 return (a * a / (hypot_a_b + b) / 2);
87 do_hard_work(float x, float y, float *rx, int *B_is_usable, float *B,
88 float *sqrt_A2my2, float *new_y)
100 if (A < A_crossover) {
101 if (y == 1 && x < FLT_EPSILON * FLT_EPSILON / 128) {
103 } else if (x >= FLT_EPSILON * fabsf(y - 1)) {
104 Am1 = f(x, 1 + y, R) + f(x, 1 - y, S);
105 *rx = log1pf(Am1 + sqrtf(Am1 * (A + 1)));
107 *rx = x / sqrtf((1 - y) * (1 + y));
109 *rx = log1pf((y - 1) + sqrtf((y - 1) * (y + 1)));
112 *rx = logf(A + sqrtf(A * A - 1));
117 if (y < FOUR_SQRT_MIN) {
119 *sqrt_A2my2 = A * (2 / FLT_EPSILON);
120 *new_y = y * (2 / FLT_EPSILON);
127 if (*B > B_crossover) {
129 if (y == 1 && x < FLT_EPSILON / 128) {
130 *sqrt_A2my2 = sqrtf(x) * sqrtf((A + y) / 2);
131 } else if (x >= FLT_EPSILON * fabsf(y - 1)) {
132 Amy = f(x, y + 1, R) + f(x, y - 1, S);
133 *sqrt_A2my2 = sqrtf(Amy * (A + y));
135 *sqrt_A2my2 = x * (4 / FLT_EPSILON / FLT_EPSILON) * y /
136 sqrtf((y + 1) * (y - 1));
137 *new_y = y * (4 / FLT_EPSILON / FLT_EPSILON);
139 *sqrt_A2my2 = sqrtf((1 - y) * (1 + y));
145 casinhf(float complex z)
147 float x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y;
156 if (isnan(x) || isnan(y)) {
158 return (cpackf(x, y + y));
160 return (cpackf(y, x + x));
162 return (cpackf(x + x, y));
163 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
166 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
168 w = clog_for_large_values(z) + m_ln2;
170 w = clog_for_large_values(-z) + m_ln2;
171 return (cpackf(copysignf(crealf(w), x),
172 copysignf(cimagf(w), y)));
175 if (x == 0 && y == 0)
180 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
183 do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y);
187 ry = atan2f(new_y, sqrt_A2my2);
188 return (cpackf(copysignf(rx, x), copysignf(ry, y)));
192 casinf(float complex z)
194 float complex w = casinhf(cpackf(cimagf(z), crealf(z)));
196 return (cpackf(cimagf(w), crealf(w)));
200 cacosf(float complex z)
202 float x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x;
214 if (isnan(x) || isnan(y)) {
216 return (cpackf(y + y, -INFINITY));
218 return (cpackf(x + x, -y));
220 return (cpackf(pio2_hi + pio2_lo, y + y));
221 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
224 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
225 w = clog_for_large_values(z);
226 rx = fabsf(cimagf(w));
227 ry = crealf(w) + m_ln2;
230 return (cpackf(rx, ry));
233 if (x == 1 && y == 0)
234 return (cpackf(0, -y));
238 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
239 return (cpackf(pio2_hi - (x - pio2_lo), -y));
241 do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x);
249 rx = atan2f(sqrt_A2mx2, new_x);
251 rx = atan2f(sqrt_A2mx2, -new_x);
255 return (cpackf(rx, ry));
259 cacoshf(float complex z)
267 if (isnan(rx) && isnan(ry))
268 return (cpackf(ry, rx));
270 return (cpackf(fabsf(ry), rx));
272 return (cpackf(ry, ry));
273 return (cpackf(fabsf(ry), copysignf(rx, cimagf(z))));
277 clog_for_large_values(float complex z)
292 if (ax > FLT_MAX / 2)
293 return (cpackf(logf(hypotf(x / m_e, y / m_e)) + 1,
296 if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN)
297 return (cpackf(logf(hypotf(x, y)), atan2f(y, x)));
299 return (cpackf(logf(ax * ax + ay * ay) / 2, atan2f(y, x)));
303 sum_squares(float x, float y)
309 return (x * x + y * y);
313 real_part_reciprocal(float x, float y)
319 GET_FLOAT_WORD(hx, x);
320 ix = hx & 0x7f800000;
321 GET_FLOAT_WORD(hy, y);
322 iy = hy & 0x7f800000;
323 #define BIAS (FLT_MAX_EXP - 1)
324 #define CUTOFF (FLT_MANT_DIG / 2 + 1)
325 if (ix - iy >= CUTOFF << 23 || isinf(x))
327 if (iy - ix >= CUTOFF << 23)
329 if (ix <= (BIAS + FLT_MAX_EXP / 2 - CUTOFF) << 23)
330 return (x / (x * x + y * y));
331 SET_FLOAT_WORD(scale, 0x7f800000 - ix);
334 return (x / (x * x + y * y) * scale);
338 catanhf(float complex z)
340 float x, y, ax, ay, rx, ry;
347 if (y == 0 && ax <= 1)
348 return (cpackf(atanhf(x), y));
351 return (cpackf(x, atanf(y)));
353 if (isnan(x) || isnan(y)) {
355 return (cpackf(copysignf(0, x), y + y));
357 return (cpackf(copysignf(0, x),
358 copysignf(pio2_hi + pio2_lo, y)));
359 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
362 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON)
363 return (cpackf(real_part_reciprocal(x, y),
364 copysignf(pio2_hi + pio2_lo, y)));
366 if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
371 if (ax == 1 && ay < FLT_EPSILON)
372 rx = (m_ln2 - logf(ay)) / 2;
374 rx = log1pf(4 * ax / sum_squares(ax - 1, ay)) / 4;
377 ry = atan2f(2, -ay) / 2;
378 else if (ay < FLT_EPSILON)
379 ry = atan2f(2 * ay, (1 - ax) * (1 + ax)) / 2;
381 ry = atan2f(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;
383 return (cpackf(copysignf(rx, x), copysignf(ry, y)));
387 catanf(float complex z)
389 float complex w = catanhf(cpackf(cimagf(z), crealf(z)));
391 return (cpackf(cimagf(w), crealf(w)));