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
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.
34 * See catrig.c for complete comments.
36 * XXX comments were removed automatically, and even short ones on the right
37 * of statements were removed (all of them), contrary to normal style. Only
38 * a few comments on the right of declarations remain.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
48 #include "math_private.h"
51 #define isinf(x) (fabsf(x) == INFINITY)
53 #define isnan(x) ((x) != (x))
54 #define raise_inexact() do { volatile float junk = 1 + tiny; } while(0)
56 #define signbit(x) (__builtin_signbitf(x))
61 FOUR_SQRT_MIN = 0x1p-61,
62 QUARTER_SQRT_MAX = 0x1p61,
63 m_e = 2.7182818285e0, /* 0xadf854.0p-22 */
64 m_ln2 = 6.9314718056e-1, /* 0xb17218.0p-24 */
65 pio2_hi = 1.5707962513e0, /* 0xc90fda.0p-23 */
66 RECIP_EPSILON = 1 / FLT_EPSILON,
67 SQRT_3_EPSILON = 5.9801995673e-4, /* 0x9cc471.0p-34 */
68 SQRT_6_EPSILON = 8.4572793338e-4, /* 0xddb3d7.0p-34 */
71 static const volatile float
72 pio2_lo = 7.5497899549e-8, /* 0xa22169.0p-47 */
75 static float complex clog_for_large_values(float complex z);
78 f(float a, float b, float hypot_a_b)
81 return ((hypot_a_b - b) / 2);
84 return (a * a / (hypot_a_b + b) / 2);
88 do_hard_work(float x, float y, float *rx, int *B_is_usable, float *B,
89 float *sqrt_A2my2, float *new_y)
101 if (A < A_crossover) {
102 if (y == 1 && x < FLT_EPSILON * FLT_EPSILON / 128) {
104 } else if (x >= FLT_EPSILON * fabsf(y - 1)) {
105 Am1 = f(x, 1 + y, R) + f(x, 1 - y, S);
106 *rx = log1pf(Am1 + sqrtf(Am1 * (A + 1)));
108 *rx = x / sqrtf((1 - y) * (1 + y));
110 *rx = log1pf((y - 1) + sqrtf((y - 1) * (y + 1)));
113 *rx = logf(A + sqrtf(A * A - 1));
118 if (y < FOUR_SQRT_MIN) {
120 *sqrt_A2my2 = A * (2 / FLT_EPSILON);
121 *new_y = y * (2 / FLT_EPSILON);
128 if (*B > B_crossover) {
130 if (y == 1 && x < FLT_EPSILON / 128) {
131 *sqrt_A2my2 = sqrtf(x) * sqrtf((A + y) / 2);
132 } else if (x >= FLT_EPSILON * fabsf(y - 1)) {
133 Amy = f(x, y + 1, R) + f(x, y - 1, S);
134 *sqrt_A2my2 = sqrtf(Amy * (A + y));
136 *sqrt_A2my2 = x * (4 / FLT_EPSILON / FLT_EPSILON) * y /
137 sqrtf((y + 1) * (y - 1));
138 *new_y = y * (4 / FLT_EPSILON / FLT_EPSILON);
140 *sqrt_A2my2 = sqrtf((1 - y) * (1 + y));
146 casinhf(float complex z)
148 float x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y;
157 if (isnan(x) || isnan(y)) {
159 return (cpackf(x, y + y));
161 return (cpackf(y, x + x));
163 return (cpackf(x + x, y));
164 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
167 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
169 w = clog_for_large_values(z) + m_ln2;
171 w = clog_for_large_values(-z) + m_ln2;
172 return (cpackf(copysignf(crealf(w), x),
173 copysignf(cimagf(w), y)));
176 if (x == 0 && y == 0)
181 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
184 do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y);
188 ry = atan2f(new_y, sqrt_A2my2);
189 return (cpackf(copysignf(rx, x), copysignf(ry, y)));
193 casinf(float complex z)
195 float complex w = casinhf(cpackf(cimagf(z), crealf(z)));
197 return (cpackf(cimagf(w), crealf(w)));
201 cacosf(float complex z)
203 float x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x;
215 if (isnan(x) || isnan(y)) {
217 return (cpackf(y + y, -INFINITY));
219 return (cpackf(x + x, -y));
221 return (cpackf(pio2_hi + pio2_lo, y + y));
222 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
225 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
226 w = clog_for_large_values(z);
227 rx = fabsf(cimagf(w));
228 ry = crealf(w) + m_ln2;
231 return (cpackf(rx, ry));
234 if (x == 1 && y == 0)
235 return (cpackf(0, -y));
239 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
240 return (cpackf(pio2_hi - (x - pio2_lo), -y));
242 do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x);
250 rx = atan2f(sqrt_A2mx2, new_x);
252 rx = atan2f(sqrt_A2mx2, -new_x);
256 return (cpackf(rx, ry));
260 cacoshf(float complex z)
268 if (isnan(rx) && isnan(ry))
269 return (cpackf(ry, rx));
271 return (cpackf(fabsf(ry), rx));
273 return (cpackf(ry, ry));
274 return (cpackf(fabsf(ry), copysignf(rx, cimagf(z))));
278 clog_for_large_values(float complex z)
293 if (ax > FLT_MAX / 2)
294 return (cpackf(logf(hypotf(x / m_e, y / m_e)) + 1,
297 if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN)
298 return (cpackf(logf(hypotf(x, y)), atan2f(y, x)));
300 return (cpackf(logf(ax * ax + ay * ay) / 2, atan2f(y, x)));
304 sum_squares(float x, float y)
310 return (x * x + y * y);
314 real_part_reciprocal(float x, float y)
320 GET_FLOAT_WORD(hx, x);
321 ix = hx & 0x7f800000;
322 GET_FLOAT_WORD(hy, y);
323 iy = hy & 0x7f800000;
324 #define BIAS (FLT_MAX_EXP - 1)
325 #define CUTOFF (FLT_MANT_DIG / 2 + 1)
326 if (ix - iy >= CUTOFF << 23 || isinf(x))
328 if (iy - ix >= CUTOFF << 23)
330 if (ix <= (BIAS + FLT_MAX_EXP / 2 - CUTOFF) << 23)
331 return (x / (x * x + y * y));
332 SET_FLOAT_WORD(scale, 0x7f800000 - ix);
335 return (x / (x * x + y * y) * scale);
339 catanhf(float complex z)
341 float x, y, ax, ay, rx, ry;
348 if (y == 0 && ax <= 1)
349 return (cpackf(atanhf(x), y));
352 return (cpackf(x, atanf(y)));
354 if (isnan(x) || isnan(y)) {
356 return (cpackf(copysignf(0, x), y + y));
358 return (cpackf(copysignf(0, x),
359 copysignf(pio2_hi + pio2_lo, y)));
360 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
363 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON)
364 return (cpackf(real_part_reciprocal(x, y),
365 copysignf(pio2_hi + pio2_lo, y)));
367 if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
372 if (ax == 1 && ay < FLT_EPSILON)
373 rx = (m_ln2 - logf(ay)) / 2;
375 rx = log1pf(4 * ax / sum_squares(ax - 1, ay)) / 4;
378 ry = atan2f(2, -ay) / 2;
379 else if (ay < FLT_EPSILON)
380 ry = atan2f(2 * ay, (1 - ax) * (1 + ax)) / 2;
382 ry = atan2f(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;
384 return (cpackf(copysignf(rx, x), copysignf(ry, y)));
388 catanf(float complex z)
390 float complex w = catanhf(cpackf(cimagf(z), crealf(z)));
392 return (cpackf(cimagf(w), crealf(w)));