2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Stephen Montgomery-Smith <stephen@FreeBSD.ORG>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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.
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
50 #include "math_private.h"
53 #define isinf(x) (fabsf(x) == INFINITY)
55 #define isnan(x) ((x) != (x))
56 #define raise_inexact() do { volatile float junk __unused = 1 + tiny; } while(0)
58 #define signbit(x) (__builtin_signbitf(x))
63 FOUR_SQRT_MIN = 0x1p-61,
64 QUARTER_SQRT_MAX = 0x1p61,
65 m_e = 2.7182818285e0, /* 0xadf854.0p-22 */
66 m_ln2 = 6.9314718056e-1, /* 0xb17218.0p-24 */
67 pio2_hi = 1.5707962513e0, /* 0xc90fda.0p-23 */
68 RECIP_EPSILON = 1 / FLT_EPSILON,
69 SQRT_3_EPSILON = 5.9801995673e-4, /* 0x9cc471.0p-34 */
70 SQRT_6_EPSILON = 8.4572793338e-4, /* 0xddb3d7.0p-34 */
73 static const volatile float
74 pio2_lo = 7.5497899549e-8, /* 0xa22169.0p-47 */
77 static float complex clog_for_large_values(float complex z);
80 f(float a, float b, float hypot_a_b)
83 return ((hypot_a_b - b) / 2);
86 return (a * a / (hypot_a_b + b) / 2);
90 do_hard_work(float x, float y, float *rx, int *B_is_usable, float *B,
91 float *sqrt_A2my2, float *new_y)
103 if (A < A_crossover) {
104 if (y == 1 && x < FLT_EPSILON * FLT_EPSILON / 128) {
106 } else if (x >= FLT_EPSILON * fabsf(y - 1)) {
107 Am1 = f(x, 1 + y, R) + f(x, 1 - y, S);
108 *rx = log1pf(Am1 + sqrtf(Am1 * (A + 1)));
110 *rx = x / sqrtf((1 - y) * (1 + y));
112 *rx = log1pf((y - 1) + sqrtf((y - 1) * (y + 1)));
115 *rx = logf(A + sqrtf(A * A - 1));
120 if (y < FOUR_SQRT_MIN) {
122 *sqrt_A2my2 = A * (2 / FLT_EPSILON);
123 *new_y = y * (2 / FLT_EPSILON);
130 if (*B > B_crossover) {
132 if (y == 1 && x < FLT_EPSILON / 128) {
133 *sqrt_A2my2 = sqrtf(x) * sqrtf((A + y) / 2);
134 } else if (x >= FLT_EPSILON * fabsf(y - 1)) {
135 Amy = f(x, y + 1, R) + f(x, y - 1, S);
136 *sqrt_A2my2 = sqrtf(Amy * (A + y));
138 *sqrt_A2my2 = x * (4 / FLT_EPSILON / FLT_EPSILON) * y /
139 sqrtf((y + 1) * (y - 1));
140 *new_y = y * (4 / FLT_EPSILON / FLT_EPSILON);
142 *sqrt_A2my2 = sqrtf((1 - y) * (1 + y));
148 casinhf(float complex z)
150 float x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y;
159 if (isnan(x) || isnan(y)) {
161 return (CMPLXF(x, y + y));
163 return (CMPLXF(y, x + x));
165 return (CMPLXF(x + x, y));
166 return (CMPLXF(nan_mix(x, y), nan_mix(x, y)));
169 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
171 w = clog_for_large_values(z) + m_ln2;
173 w = clog_for_large_values(-z) + m_ln2;
174 return (CMPLXF(copysignf(crealf(w), x),
175 copysignf(cimagf(w), y)));
178 if (x == 0 && y == 0)
183 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
186 do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y);
190 ry = atan2f(new_y, sqrt_A2my2);
191 return (CMPLXF(copysignf(rx, x), copysignf(ry, y)));
195 casinf(float complex z)
197 float complex w = casinhf(CMPLXF(cimagf(z), crealf(z)));
199 return (CMPLXF(cimagf(w), crealf(w)));
203 cacosf(float complex z)
205 float x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x;
217 if (isnan(x) || isnan(y)) {
219 return (CMPLXF(y + y, -INFINITY));
221 return (CMPLXF(x + x, -y));
223 return (CMPLXF(pio2_hi + pio2_lo, y + y));
224 return (CMPLXF(nan_mix(x, y), nan_mix(x, y)));
227 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
228 w = clog_for_large_values(z);
229 rx = fabsf(cimagf(w));
230 ry = crealf(w) + m_ln2;
233 return (CMPLXF(rx, ry));
236 if (x == 1 && y == 0)
237 return (CMPLXF(0, -y));
241 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
242 return (CMPLXF(pio2_hi - (x - pio2_lo), -y));
244 do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x);
252 rx = atan2f(sqrt_A2mx2, new_x);
254 rx = atan2f(sqrt_A2mx2, -new_x);
258 return (CMPLXF(rx, ry));
262 cacoshf(float complex z)
270 if (isnan(rx) && isnan(ry))
271 return (CMPLXF(ry, rx));
273 return (CMPLXF(fabsf(ry), rx));
275 return (CMPLXF(ry, ry));
276 return (CMPLXF(fabsf(ry), copysignf(rx, cimagf(z))));
280 clog_for_large_values(float complex z)
295 if (ax > FLT_MAX / 2)
296 return (CMPLXF(logf(hypotf(x / m_e, y / m_e)) + 1,
299 if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN)
300 return (CMPLXF(logf(hypotf(x, y)), atan2f(y, x)));
302 return (CMPLXF(logf(ax * ax + ay * ay) / 2, atan2f(y, x)));
306 sum_squares(float x, float y)
312 return (x * x + y * y);
316 real_part_reciprocal(float x, float y)
322 GET_FLOAT_WORD(hx, x);
323 ix = hx & 0x7f800000;
324 GET_FLOAT_WORD(hy, y);
325 iy = hy & 0x7f800000;
326 #define BIAS (FLT_MAX_EXP - 1)
327 #define CUTOFF (FLT_MANT_DIG / 2 + 1)
328 if (ix - iy >= CUTOFF << 23 || isinf(x))
330 if (iy - ix >= CUTOFF << 23)
332 if (ix <= (BIAS + FLT_MAX_EXP / 2 - CUTOFF) << 23)
333 return (x / (x * x + y * y));
334 SET_FLOAT_WORD(scale, 0x7f800000 - ix);
337 return (x / (x * x + y * y) * scale);
341 catanhf(float complex z)
343 float x, y, ax, ay, rx, ry;
350 if (y == 0 && ax <= 1)
351 return (CMPLXF(atanhf(x), y));
354 return (CMPLXF(x, atanf(y)));
356 if (isnan(x) || isnan(y)) {
358 return (CMPLXF(copysignf(0, x), y + y));
360 return (CMPLXF(copysignf(0, x),
361 copysignf(pio2_hi + pio2_lo, y)));
362 return (CMPLXF(nan_mix(x, y), nan_mix(x, y)));
365 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON)
366 return (CMPLXF(real_part_reciprocal(x, y),
367 copysignf(pio2_hi + pio2_lo, y)));
369 if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
374 if (ax == 1 && ay < FLT_EPSILON)
375 rx = (m_ln2 - logf(ay)) / 2;
377 rx = log1pf(4 * ax / sum_squares(ax - 1, ay)) / 4;
380 ry = atan2f(2, -ay) / 2;
381 else if (ay < FLT_EPSILON)
382 ry = atan2f(2 * ay, (1 - ax) * (1 + ax)) / 2;
384 ry = atan2f(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;
386 return (CMPLXF(copysignf(rx, x), copysignf(ry, y)));
390 catanf(float complex z)
392 float complex w = catanhf(CMPLXF(cimagf(z), crealf(z)));
394 return (CMPLXF(cimagf(w), crealf(w)));