1/*-
2 * Copyright (c) 2012 Stephen Montgomery-Smith <stephen@FreeBSD.ORG>
3 * Copyright (c) 2017 Mahdi Mokhtari <mmokhi@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28/*
29 * The algorithm is very close to that in "Implementing the complex arcsine
30 * and arccosine functions using exception handling" by T. E. Hull, Thomas F.
31 * Fairgrieve, and Ping Tak Peter Tang, published in ACM Transactions on
32 * Mathematical Software, Volume 23 Issue 3, 1997, Pages 299-335,
33 * http://dl.acm.org/citation.cfm?id=275324.
34 *
35 * See catrig.c for complete comments.
36 *
37 * XXX comments were removed automatically, and even short ones on the right
38 * of statements were removed (all of them), contrary to normal style.  Only
39 * a few comments on the right of declarations remain.
40 */
41
42#include <complex.h>
43#include <float.h>
44
45#include "invtrig.h"
46#include "math.h"
47#include "math_private.h"
48
49#undef isinf
50#define isinf(x)	(fabsl(x) == INFINITY)
51#undef isnan
52#define isnan(x)	((x) != (x))
53#define	raise_inexact()	do { volatile float junk __unused = 1 + tiny; } while(0)
54#undef signbit
55#define signbit(x)	(__builtin_signbitl(x))
56
57#if LDBL_MAX_EXP != 0x4000
58#error "Unsupported long double format"
59#endif
60
61static const long double
62A_crossover =		10,
63B_crossover =		0.6417,
64FOUR_SQRT_MIN =		0x1p-8189L,
65HALF_MAX =		0x1p16383L,
66QUARTER_SQRT_MAX =	0x1p8189L,
67RECIP_EPSILON =		1 / LDBL_EPSILON,
68SQRT_MIN =		0x1p-8191L;
69
70#if LDBL_MANT_DIG == 64
71static const union IEEEl2bits
72um_e =		LD80C(0xadf85458a2bb4a9b,  1, 2.71828182845904523536e+0L),
73um_ln2 =	LD80C(0xb17217f7d1cf79ac, -1, 6.93147180559945309417e-1L);
74#define		m_e	um_e.e
75#define		m_ln2	um_ln2.e
76static const long double
77/* The next 2 literals for non-i386.  Misrounding them on i386 is harmless. */
78SQRT_3_EPSILON = 5.70316273435758915310e-10,	/*  0x9cc470a0490973e8.0p-94 */
79SQRT_6_EPSILON = 8.06549008734932771664e-10;	/*  0xddb3d742c265539e.0p-94 */
80#elif LDBL_MANT_DIG == 113
81static const long double
82m_e =		2.71828182845904523536028747135266250e0L,	/* 0x15bf0a8b1457695355fb8ac404e7a.0p-111 */
83m_ln2 =		6.93147180559945309417232121458176568e-1L,	/* 0x162e42fefa39ef35793c7673007e6.0p-113 */
84SQRT_3_EPSILON = 2.40370335797945490975336727199878124e-17,	/*  0x1bb67ae8584caa73b25742d7078b8.0p-168 */
85SQRT_6_EPSILON = 3.39934988877629587239082586223300391e-17;	/*  0x13988e1409212e7d0321914321a55.0p-167 */
86#else
87#error "Unsupported long double format"
88#endif
89
90static const volatile float
91tiny =			0x1p-100;
92
93static long double complex clog_for_large_values(long double complex z);
94
95static inline long double
96f(long double a, long double b, long double hypot_a_b)
97{
98	if (b < 0)
99		return ((hypot_a_b - b) / 2);
100	if (b == 0)
101		return (a / 2);
102	return (a * a / (hypot_a_b + b) / 2);
103}
104
105static inline void
106do_hard_work(long double x, long double y, long double *rx, int *B_is_usable,
107    long double *B, long double *sqrt_A2my2, long double *new_y)
108{
109	long double R, S, A;
110	long double Am1, Amy;
111
112	R = hypotl(x, y + 1);
113	S = hypotl(x, y - 1);
114
115	A = (R + S) / 2;
116	if (A < 1)
117		A = 1;
118
119	if (A < A_crossover) {
120		if (y == 1 && x < LDBL_EPSILON * LDBL_EPSILON / 128) {
121			*rx = sqrtl(x);
122		} else if (x >= LDBL_EPSILON * fabsl(y - 1)) {
123			Am1 = f(x, 1 + y, R) + f(x, 1 - y, S);
124			*rx = log1pl(Am1 + sqrtl(Am1 * (A + 1)));
125		} else if (y < 1) {
126			*rx = x / sqrtl((1 - y) * (1 + y));
127		} else {
128			*rx = log1pl((y - 1) + sqrtl((y - 1) * (y + 1)));
129		}
130	} else {
131		*rx = logl(A + sqrtl(A * A - 1));
132	}
133
134	*new_y = y;
135
136	if (y < FOUR_SQRT_MIN) {
137		*B_is_usable = 0;
138		*sqrt_A2my2 = A * (2 / LDBL_EPSILON);
139		*new_y = y * (2 / LDBL_EPSILON);
140		return;
141	}
142
143	*B = y / A;
144	*B_is_usable = 1;
145
146	if (*B > B_crossover) {
147		*B_is_usable = 0;
148		if (y == 1 && x < LDBL_EPSILON / 128) {
149			*sqrt_A2my2 = sqrtl(x) * sqrtl((A + y) / 2);
150		} else if (x >= LDBL_EPSILON * fabsl(y - 1)) {
151			Amy = f(x, y + 1, R) + f(x, y - 1, S);
152			*sqrt_A2my2 = sqrtl(Amy * (A + y));
153		} else if (y > 1) {
154			*sqrt_A2my2 = x * (4 / LDBL_EPSILON / LDBL_EPSILON) * y /
155			    sqrtl((y + 1) * (y - 1));
156			*new_y = y * (4 / LDBL_EPSILON / LDBL_EPSILON);
157		} else {
158			*sqrt_A2my2 = sqrtl((1 - y) * (1 + y));
159		}
160	}
161}
162
163long double complex
164casinhl(long double complex z)
165{
166	long double x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y;
167	int B_is_usable;
168	long double complex w;
169
170	x = creall(z);
171	y = cimagl(z);
172	ax = fabsl(x);
173	ay = fabsl(y);
174
175	if (isnan(x) || isnan(y)) {
176		if (isinf(x))
177			return (CMPLXL(x, y + y));
178		if (isinf(y))
179			return (CMPLXL(y, x + x));
180		if (y == 0)
181			return (CMPLXL(x + x, y));
182		return (CMPLXL(nan_mix(x, y), nan_mix(x, y)));
183	}
184
185	if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
186		if (signbit(x) == 0)
187			w = clog_for_large_values(z) + m_ln2;
188		else
189			w = clog_for_large_values(-z) + m_ln2;
190		return (CMPLXL(copysignl(creall(w), x),
191		    copysignl(cimagl(w), y)));
192	}
193
194	if (x == 0 && y == 0)
195		return (z);
196
197	raise_inexact();
198
199	if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
200		return (z);
201
202	do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y);
203	if (B_is_usable)
204		ry = asinl(B);
205	else
206		ry = atan2l(new_y, sqrt_A2my2);
207	return (CMPLXL(copysignl(rx, x), copysignl(ry, y)));
208}
209
210long double complex
211casinl(long double complex z)
212{
213	long double complex w;
214
215	w = casinhl(CMPLXL(cimagl(z), creall(z)));
216	return (CMPLXL(cimagl(w), creall(w)));
217}
218
219long double complex
220cacosl(long double complex z)
221{
222	long double x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x;
223	int sx, sy;
224	int B_is_usable;
225	long double complex w;
226
227	x = creall(z);
228	y = cimagl(z);
229	sx = signbit(x);
230	sy = signbit(y);
231	ax = fabsl(x);
232	ay = fabsl(y);
233
234	if (isnan(x) || isnan(y)) {
235		if (isinf(x))
236			return (CMPLXL(y + y, -INFINITY));
237		if (isinf(y))
238			return (CMPLXL(x + x, -y));
239		if (x == 0)
240			return (CMPLXL(pio2_hi + pio2_lo, y + y));
241		return (CMPLXL(nan_mix(x, y), nan_mix(x, y)));
242	}
243
244	if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
245		w = clog_for_large_values(z);
246		rx = fabsl(cimagl(w));
247		ry = creall(w) + m_ln2;
248		if (sy == 0)
249			ry = -ry;
250		return (CMPLXL(rx, ry));
251	}
252
253	if (x == 1 && y == 0)
254		return (CMPLXL(0, -y));
255
256	raise_inexact();
257
258	if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
259		return (CMPLXL(pio2_hi - (x - pio2_lo), -y));
260
261	do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x);
262	if (B_is_usable) {
263		if (sx == 0)
264			rx = acosl(B);
265		else
266			rx = acosl(-B);
267	} else {
268		if (sx == 0)
269			rx = atan2l(sqrt_A2mx2, new_x);
270		else
271			rx = atan2l(sqrt_A2mx2, -new_x);
272	}
273	if (sy == 0)
274		ry = -ry;
275	return (CMPLXL(rx, ry));
276}
277
278long double complex
279cacoshl(long double complex z)
280{
281	long double complex w;
282	long double rx, ry;
283
284	w = cacosl(z);
285	rx = creall(w);
286	ry = cimagl(w);
287	if (isnan(rx) && isnan(ry))
288		return (CMPLXL(ry, rx));
289	if (isnan(rx))
290		return (CMPLXL(fabsl(ry), rx));
291	if (isnan(ry))
292		return (CMPLXL(ry, ry));
293	return (CMPLXL(fabsl(ry), copysignl(rx, cimagl(z))));
294}
295
296static long double complex
297clog_for_large_values(long double complex z)
298{
299	long double x, y;
300	long double ax, ay, t;
301
302	x = creall(z);
303	y = cimagl(z);
304	ax = fabsl(x);
305	ay = fabsl(y);
306	if (ax < ay) {
307		t = ax;
308		ax = ay;
309		ay = t;
310	}
311
312	if (ax > HALF_MAX)
313		return (CMPLXL(logl(hypotl(x / m_e, y / m_e)) + 1,
314		    atan2l(y, x)));
315
316	if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN)
317		return (CMPLXL(logl(hypotl(x, y)), atan2l(y, x)));
318
319	return (CMPLXL(logl(ax * ax + ay * ay) / 2, atan2l(y, x)));
320}
321
322static inline long double
323sum_squares(long double x, long double y)
324{
325
326	if (y < SQRT_MIN)
327		return (x * x);
328
329	return (x * x + y * y);
330}
331
332static inline long double
333real_part_reciprocal(long double x, long double y)
334{
335	long double scale;
336	uint16_t hx, hy;
337	int16_t ix, iy;
338
339	GET_LDBL_EXPSIGN(hx, x);
340	ix = hx & 0x7fff;
341	GET_LDBL_EXPSIGN(hy, y);
342	iy = hy & 0x7fff;
343#define	BIAS	(LDBL_MAX_EXP - 1)
344#define	CUTOFF	(LDBL_MANT_DIG / 2 + 1)
345	if (ix - iy >= CUTOFF || isinf(x))
346		return (1 / x);
347	if (iy - ix >= CUTOFF)
348		return (x / y / y);
349	if (ix <= BIAS + LDBL_MAX_EXP / 2 - CUTOFF)
350		return (x / (x * x + y * y));
351	scale = 1;
352	SET_LDBL_EXPSIGN(scale, 0x7fff - ix);
353	x *= scale;
354	y *= scale;
355	return (x / (x * x + y * y) * scale);
356}
357
358long double complex
359catanhl(long double complex z)
360{
361	long double x, y, ax, ay, rx, ry;
362
363	x = creall(z);
364	y = cimagl(z);
365	ax = fabsl(x);
366	ay = fabsl(y);
367
368	if (y == 0 && ax <= 1)
369		return (CMPLXL(atanhl(x), y));
370
371	if (x == 0)
372		return (CMPLXL(x, atanl(y)));
373
374	if (isnan(x) || isnan(y)) {
375		if (isinf(x))
376			return (CMPLXL(copysignl(0, x), y + y));
377		if (isinf(y))
378			return (CMPLXL(copysignl(0, x),
379			    copysignl(pio2_hi + pio2_lo, y)));
380		return (CMPLXL(nan_mix(x, y), nan_mix(x, y)));
381	}
382
383	if (ax > RECIP_EPSILON || ay > RECIP_EPSILON)
384		return (CMPLXL(real_part_reciprocal(x, y),
385		    copysignl(pio2_hi + pio2_lo, y)));
386
387	if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
388		raise_inexact();
389		return (z);
390	}
391
392	if (ax == 1 && ay < LDBL_EPSILON)
393		rx = (m_ln2 - logl(ay)) / 2;
394	else
395		rx = log1pl(4 * ax / sum_squares(ax - 1, ay)) / 4;
396
397	if (ax == 1)
398		ry = atan2l(2, -ay) / 2;
399	else if (ay < LDBL_EPSILON)
400		ry = atan2l(2 * ay, (1 - ax) * (1 + ax)) / 2;
401	else
402		ry = atan2l(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;
403
404	return (CMPLXL(copysignl(rx, x), copysignl(ry, y)));
405}
406
407long double complex
408catanl(long double complex z)
409{
410	long double complex w;
411
412	w = catanhl(CMPLXL(cimagl(z), creall(z)));
413	return (CMPLXL(cimagl(w), creall(w)));
414}
415