1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012 Stephen Montgomery-Smith <stephen@FreeBSD.ORG>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29/*
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.
35 *
36 * See catrig.c for complete comments.
37 *
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.
41 */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD$");
45
46#include <complex.h>
47#include <float.h>
48
49#include "math.h"
50#include "math_private.h"
51
52#undef isinf
53#define isinf(x)	(fabsf(x) == INFINITY)
54#undef isnan
55#define isnan(x)	((x) != (x))
56#define	raise_inexact()	do { volatile float junk __unused = 1 + tiny; } while(0)
57#undef signbit
58#define signbit(x)	(__builtin_signbitf(x))
59
60static const float
61A_crossover =		10,
62B_crossover =		0.6417,
63FOUR_SQRT_MIN =		0x1p-61,
64QUARTER_SQRT_MAX =	0x1p61,
65m_e =			2.7182818285e0,		/*  0xadf854.0p-22 */
66m_ln2 =			6.9314718056e-1,	/*  0xb17218.0p-24 */
67pio2_hi =		1.5707962513e0,		/*  0xc90fda.0p-23 */
68RECIP_EPSILON =		1 / FLT_EPSILON,
69SQRT_3_EPSILON =	5.9801995673e-4,	/*  0x9cc471.0p-34 */
70SQRT_6_EPSILON =	8.4572793338e-4,	/*  0xddb3d7.0p-34 */
71SQRT_MIN =		0x1p-63;
72
73static const volatile float
74pio2_lo =		7.5497899549e-8,	/*  0xa22169.0p-47 */
75tiny =			0x1p-100;
76
77static float complex clog_for_large_values(float complex z);
78
79static inline float
80f(float a, float b, float hypot_a_b)
81{
82	if (b < 0)
83		return ((hypot_a_b - b) / 2);
84	if (b == 0)
85		return (a / 2);
86	return (a * a / (hypot_a_b + b) / 2);
87}
88
89static inline void
90do_hard_work(float x, float y, float *rx, int *B_is_usable, float *B,
91    float *sqrt_A2my2, float *new_y)
92{
93	float R, S, A;
94	float Am1, Amy;
95
96	R = hypotf(x, y + 1);
97	S = hypotf(x, y - 1);
98
99	A = (R + S) / 2;
100	if (A < 1)
101		A = 1;
102
103	if (A < A_crossover) {
104		if (y == 1 && x < FLT_EPSILON * FLT_EPSILON / 128) {
105			*rx = sqrtf(x);
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)));
109		} else if (y < 1) {
110			*rx = x / sqrtf((1 - y) * (1 + y));
111		} else {
112			*rx = log1pf((y - 1) + sqrtf((y - 1) * (y + 1)));
113		}
114	} else {
115		*rx = logf(A + sqrtf(A * A - 1));
116	}
117
118	*new_y = y;
119
120	if (y < FOUR_SQRT_MIN) {
121		*B_is_usable = 0;
122		*sqrt_A2my2 = A * (2 / FLT_EPSILON);
123		*new_y = y * (2 / FLT_EPSILON);
124		return;
125	}
126
127	*B = y / A;
128	*B_is_usable = 1;
129
130	if (*B > B_crossover) {
131		*B_is_usable = 0;
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));
137		} else if (y > 1) {
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);
141		} else {
142			*sqrt_A2my2 = sqrtf((1 - y) * (1 + y));
143		}
144	}
145}
146
147float complex
148casinhf(float complex z)
149{
150	float x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y;
151	int B_is_usable;
152	float complex w;
153
154	x = crealf(z);
155	y = cimagf(z);
156	ax = fabsf(x);
157	ay = fabsf(y);
158
159	if (isnan(x) || isnan(y)) {
160		if (isinf(x))
161			return (CMPLXF(x, y + y));
162		if (isinf(y))
163			return (CMPLXF(y, x + x));
164		if (y == 0)
165			return (CMPLXF(x + x, y));
166		return (CMPLXF(nan_mix(x, y), nan_mix(x, y)));
167	}
168
169	if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
170		if (signbit(x) == 0)
171			w = clog_for_large_values(z) + m_ln2;
172		else
173			w = clog_for_large_values(-z) + m_ln2;
174		return (CMPLXF(copysignf(crealf(w), x),
175		    copysignf(cimagf(w), y)));
176	}
177
178	if (x == 0 && y == 0)
179		return (z);
180
181	raise_inexact();
182
183	if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
184		return (z);
185
186	do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y);
187	if (B_is_usable)
188		ry = asinf(B);
189	else
190		ry = atan2f(new_y, sqrt_A2my2);
191	return (CMPLXF(copysignf(rx, x), copysignf(ry, y)));
192}
193
194float complex
195casinf(float complex z)
196{
197	float complex w = casinhf(CMPLXF(cimagf(z), crealf(z)));
198
199	return (CMPLXF(cimagf(w), crealf(w)));
200}
201
202float complex
203cacosf(float complex z)
204{
205	float x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x;
206	int sx, sy;
207	int B_is_usable;
208	float complex w;
209
210	x = crealf(z);
211	y = cimagf(z);
212	sx = signbit(x);
213	sy = signbit(y);
214	ax = fabsf(x);
215	ay = fabsf(y);
216
217	if (isnan(x) || isnan(y)) {
218		if (isinf(x))
219			return (CMPLXF(y + y, -INFINITY));
220		if (isinf(y))
221			return (CMPLXF(x + x, -y));
222		if (x == 0)
223			return (CMPLXF(pio2_hi + pio2_lo, y + y));
224		return (CMPLXF(nan_mix(x, y), nan_mix(x, y)));
225	}
226
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;
231		if (sy == 0)
232			ry = -ry;
233		return (CMPLXF(rx, ry));
234	}
235
236	if (x == 1 && y == 0)
237		return (CMPLXF(0, -y));
238
239	raise_inexact();
240
241	if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
242		return (CMPLXF(pio2_hi - (x - pio2_lo), -y));
243
244	do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x);
245	if (B_is_usable) {
246		if (sx == 0)
247			rx = acosf(B);
248		else
249			rx = acosf(-B);
250	} else {
251		if (sx == 0)
252			rx = atan2f(sqrt_A2mx2, new_x);
253		else
254			rx = atan2f(sqrt_A2mx2, -new_x);
255	}
256	if (sy == 0)
257		ry = -ry;
258	return (CMPLXF(rx, ry));
259}
260
261float complex
262cacoshf(float complex z)
263{
264	float complex w;
265	float rx, ry;
266
267	w = cacosf(z);
268	rx = crealf(w);
269	ry = cimagf(w);
270	if (isnan(rx) && isnan(ry))
271		return (CMPLXF(ry, rx));
272	if (isnan(rx))
273		return (CMPLXF(fabsf(ry), rx));
274	if (isnan(ry))
275		return (CMPLXF(ry, ry));
276	return (CMPLXF(fabsf(ry), copysignf(rx, cimagf(z))));
277}
278
279static float complex
280clog_for_large_values(float complex z)
281{
282	float x, y;
283	float ax, ay, t;
284
285	x = crealf(z);
286	y = cimagf(z);
287	ax = fabsf(x);
288	ay = fabsf(y);
289	if (ax < ay) {
290		t = ax;
291		ax = ay;
292		ay = t;
293	}
294
295	if (ax > FLT_MAX / 2)
296		return (CMPLXF(logf(hypotf(x / m_e, y / m_e)) + 1,
297		    atan2f(y, x)));
298
299	if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN)
300		return (CMPLXF(logf(hypotf(x, y)), atan2f(y, x)));
301
302	return (CMPLXF(logf(ax * ax + ay * ay) / 2, atan2f(y, x)));
303}
304
305static inline float
306sum_squares(float x, float y)
307{
308
309	if (y < SQRT_MIN)
310		return (x * x);
311
312	return (x * x + y * y);
313}
314
315static inline float
316real_part_reciprocal(float x, float y)
317{
318	float scale;
319	uint32_t hx, hy;
320	int32_t ix, iy;
321
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))
329		return (1 / x);
330	if (iy - ix >= CUTOFF << 23)
331		return (x / y / y);
332	if (ix <= (BIAS + FLT_MAX_EXP / 2 - CUTOFF) << 23)
333		return (x / (x * x + y * y));
334	SET_FLOAT_WORD(scale, 0x7f800000 - ix);
335	x *= scale;
336	y *= scale;
337	return (x / (x * x + y * y) * scale);
338}
339
340float complex
341catanhf(float complex z)
342{
343	float x, y, ax, ay, rx, ry;
344
345	x = crealf(z);
346	y = cimagf(z);
347	ax = fabsf(x);
348	ay = fabsf(y);
349
350	if (y == 0 && ax <= 1)
351		return (CMPLXF(atanhf(x), y));
352
353	if (x == 0)
354		return (CMPLXF(x, atanf(y)));
355
356	if (isnan(x) || isnan(y)) {
357		if (isinf(x))
358			return (CMPLXF(copysignf(0, x), y + y));
359		if (isinf(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)));
363	}
364
365	if (ax > RECIP_EPSILON || ay > RECIP_EPSILON)
366		return (CMPLXF(real_part_reciprocal(x, y),
367		    copysignf(pio2_hi + pio2_lo, y)));
368
369	if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
370		raise_inexact();
371		return (z);
372	}
373
374	if (ax == 1 && ay < FLT_EPSILON)
375		rx = (m_ln2 - logf(ay)) / 2;
376	else
377		rx = log1pf(4 * ax / sum_squares(ax - 1, ay)) / 4;
378
379	if (ax == 1)
380		ry = atan2f(2, -ay) / 2;
381	else if (ay < FLT_EPSILON)
382		ry = atan2f(2 * ay, (1 - ax) * (1 + ax)) / 2;
383	else
384		ry = atan2f(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;
385
386	return (CMPLXF(copysignf(rx, x), copysignf(ry, y)));
387}
388
389float complex
390catanf(float complex z)
391{
392	float complex w = catanhf(CMPLXF(cimagf(z), crealf(z)));
393
394	return (CMPLXF(cimagf(w), crealf(w)));
395}
396