xref: /freebsd/lib/msun/src/catrigl.c (revision 4c9a0adad18263ec8725d9bfc5f560c6ad1da8bd)
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 <sys/cdefs.h>
43 #include <complex.h>
44 #include <float.h>
45 
46 #include "invtrig.h"
47 #include "math.h"
48 #include "math_private.h"
49 
50 #undef isinf
51 #define isinf(x)	(fabsl(x) == INFINITY)
52 #undef isnan
53 #define isnan(x)	((x) != (x))
54 #define	raise_inexact()	do { volatile float junk __unused = 1 + tiny; } while(0)
55 #undef signbit
56 #define signbit(x)	(__builtin_signbitl(x))
57 
58 #if LDBL_MAX_EXP != 0x4000
59 #error "Unsupported long double format"
60 #endif
61 
62 static const long double
63 A_crossover =		10,
64 B_crossover =		0.6417,
65 FOUR_SQRT_MIN =		0x1p-8189L,
66 HALF_MAX =		0x1p16383L,
67 QUARTER_SQRT_MAX =	0x1p8189L,
68 RECIP_EPSILON =		1 / LDBL_EPSILON,
69 SQRT_MIN =		0x1p-8191L;
70 
71 #if LDBL_MANT_DIG == 64
72 static const union IEEEl2bits
73 um_e =		LD80C(0xadf85458a2bb4a9b,  1, 2.71828182845904523536e+0L),
74 um_ln2 =	LD80C(0xb17217f7d1cf79ac, -1, 6.93147180559945309417e-1L);
75 #define		m_e	um_e.e
76 #define		m_ln2	um_ln2.e
77 static const long double
78 /* The next 2 literals for non-i386.  Misrounding them on i386 is harmless. */
79 SQRT_3_EPSILON = 5.70316273435758915310e-10,	/*  0x9cc470a0490973e8.0p-94 */
80 SQRT_6_EPSILON = 8.06549008734932771664e-10;	/*  0xddb3d742c265539e.0p-94 */
81 #elif LDBL_MANT_DIG == 113
82 static const long double
83 m_e =		2.71828182845904523536028747135266250e0L,	/* 0x15bf0a8b1457695355fb8ac404e7a.0p-111 */
84 m_ln2 =		6.93147180559945309417232121458176568e-1L,	/* 0x162e42fefa39ef35793c7673007e6.0p-113 */
85 SQRT_3_EPSILON = 2.40370335797945490975336727199878124e-17,	/*  0x1bb67ae8584caa73b25742d7078b8.0p-168 */
86 SQRT_6_EPSILON = 3.39934988877629587239082586223300391e-17;	/*  0x13988e1409212e7d0321914321a55.0p-167 */
87 #else
88 #error "Unsupported long double format"
89 #endif
90 
91 static const volatile float
92 tiny =			0x1p-100;
93 
94 static long double complex clog_for_large_values(long double complex z);
95 
96 static inline long double
97 f(long double a, long double b, long double hypot_a_b)
98 {
99 	if (b < 0)
100 		return ((hypot_a_b - b) / 2);
101 	if (b == 0)
102 		return (a / 2);
103 	return (a * a / (hypot_a_b + b) / 2);
104 }
105 
106 static inline void
107 do_hard_work(long double x, long double y, long double *rx, int *B_is_usable,
108     long double *B, long double *sqrt_A2my2, long double *new_y)
109 {
110 	long double R, S, A;
111 	long double Am1, Amy;
112 
113 	R = hypotl(x, y + 1);
114 	S = hypotl(x, y - 1);
115 
116 	A = (R + S) / 2;
117 	if (A < 1)
118 		A = 1;
119 
120 	if (A < A_crossover) {
121 		if (y == 1 && x < LDBL_EPSILON * LDBL_EPSILON / 128) {
122 			*rx = sqrtl(x);
123 		} else if (x >= LDBL_EPSILON * fabsl(y - 1)) {
124 			Am1 = f(x, 1 + y, R) + f(x, 1 - y, S);
125 			*rx = log1pl(Am1 + sqrtl(Am1 * (A + 1)));
126 		} else if (y < 1) {
127 			*rx = x / sqrtl((1 - y) * (1 + y));
128 		} else {
129 			*rx = log1pl((y - 1) + sqrtl((y - 1) * (y + 1)));
130 		}
131 	} else {
132 		*rx = logl(A + sqrtl(A * A - 1));
133 	}
134 
135 	*new_y = y;
136 
137 	if (y < FOUR_SQRT_MIN) {
138 		*B_is_usable = 0;
139 		*sqrt_A2my2 = A * (2 / LDBL_EPSILON);
140 		*new_y = y * (2 / LDBL_EPSILON);
141 		return;
142 	}
143 
144 	*B = y / A;
145 	*B_is_usable = 1;
146 
147 	if (*B > B_crossover) {
148 		*B_is_usable = 0;
149 		if (y == 1 && x < LDBL_EPSILON / 128) {
150 			*sqrt_A2my2 = sqrtl(x) * sqrtl((A + y) / 2);
151 		} else if (x >= LDBL_EPSILON * fabsl(y - 1)) {
152 			Amy = f(x, y + 1, R) + f(x, y - 1, S);
153 			*sqrt_A2my2 = sqrtl(Amy * (A + y));
154 		} else if (y > 1) {
155 			*sqrt_A2my2 = x * (4 / LDBL_EPSILON / LDBL_EPSILON) * y /
156 			    sqrtl((y + 1) * (y - 1));
157 			*new_y = y * (4 / LDBL_EPSILON / LDBL_EPSILON);
158 		} else {
159 			*sqrt_A2my2 = sqrtl((1 - y) * (1 + y));
160 		}
161 	}
162 }
163 
164 long double complex
165 casinhl(long double complex z)
166 {
167 	long double x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y;
168 	int B_is_usable;
169 	long double complex w;
170 
171 	x = creall(z);
172 	y = cimagl(z);
173 	ax = fabsl(x);
174 	ay = fabsl(y);
175 
176 	if (isnan(x) || isnan(y)) {
177 		if (isinf(x))
178 			return (CMPLXL(x, y + y));
179 		if (isinf(y))
180 			return (CMPLXL(y, x + x));
181 		if (y == 0)
182 			return (CMPLXL(x + x, y));
183 		return (CMPLXL(nan_mix(x, y), nan_mix(x, y)));
184 	}
185 
186 	if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
187 		if (signbit(x) == 0)
188 			w = clog_for_large_values(z) + m_ln2;
189 		else
190 			w = clog_for_large_values(-z) + m_ln2;
191 		return (CMPLXL(copysignl(creall(w), x),
192 		    copysignl(cimagl(w), y)));
193 	}
194 
195 	if (x == 0 && y == 0)
196 		return (z);
197 
198 	raise_inexact();
199 
200 	if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
201 		return (z);
202 
203 	do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y);
204 	if (B_is_usable)
205 		ry = asinl(B);
206 	else
207 		ry = atan2l(new_y, sqrt_A2my2);
208 	return (CMPLXL(copysignl(rx, x), copysignl(ry, y)));
209 }
210 
211 long double complex
212 casinl(long double complex z)
213 {
214 	long double complex w;
215 
216 	w = casinhl(CMPLXL(cimagl(z), creall(z)));
217 	return (CMPLXL(cimagl(w), creall(w)));
218 }
219 
220 long double complex
221 cacosl(long double complex z)
222 {
223 	long double x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x;
224 	int sx, sy;
225 	int B_is_usable;
226 	long double complex w;
227 
228 	x = creall(z);
229 	y = cimagl(z);
230 	sx = signbit(x);
231 	sy = signbit(y);
232 	ax = fabsl(x);
233 	ay = fabsl(y);
234 
235 	if (isnan(x) || isnan(y)) {
236 		if (isinf(x))
237 			return (CMPLXL(y + y, -INFINITY));
238 		if (isinf(y))
239 			return (CMPLXL(x + x, -y));
240 		if (x == 0)
241 			return (CMPLXL(pio2_hi + pio2_lo, y + y));
242 		return (CMPLXL(nan_mix(x, y), nan_mix(x, y)));
243 	}
244 
245 	if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
246 		w = clog_for_large_values(z);
247 		rx = fabsl(cimagl(w));
248 		ry = creall(w) + m_ln2;
249 		if (sy == 0)
250 			ry = -ry;
251 		return (CMPLXL(rx, ry));
252 	}
253 
254 	if (x == 1 && y == 0)
255 		return (CMPLXL(0, -y));
256 
257 	raise_inexact();
258 
259 	if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
260 		return (CMPLXL(pio2_hi - (x - pio2_lo), -y));
261 
262 	do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x);
263 	if (B_is_usable) {
264 		if (sx == 0)
265 			rx = acosl(B);
266 		else
267 			rx = acosl(-B);
268 	} else {
269 		if (sx == 0)
270 			rx = atan2l(sqrt_A2mx2, new_x);
271 		else
272 			rx = atan2l(sqrt_A2mx2, -new_x);
273 	}
274 	if (sy == 0)
275 		ry = -ry;
276 	return (CMPLXL(rx, ry));
277 }
278 
279 long double complex
280 cacoshl(long double complex z)
281 {
282 	long double complex w;
283 	long double rx, ry;
284 
285 	w = cacosl(z);
286 	rx = creall(w);
287 	ry = cimagl(w);
288 	if (isnan(rx) && isnan(ry))
289 		return (CMPLXL(ry, rx));
290 	if (isnan(rx))
291 		return (CMPLXL(fabsl(ry), rx));
292 	if (isnan(ry))
293 		return (CMPLXL(ry, ry));
294 	return (CMPLXL(fabsl(ry), copysignl(rx, cimagl(z))));
295 }
296 
297 static long double complex
298 clog_for_large_values(long double complex z)
299 {
300 	long double x, y;
301 	long double ax, ay, t;
302 
303 	x = creall(z);
304 	y = cimagl(z);
305 	ax = fabsl(x);
306 	ay = fabsl(y);
307 	if (ax < ay) {
308 		t = ax;
309 		ax = ay;
310 		ay = t;
311 	}
312 
313 	if (ax > HALF_MAX)
314 		return (CMPLXL(logl(hypotl(x / m_e, y / m_e)) + 1,
315 		    atan2l(y, x)));
316 
317 	if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN)
318 		return (CMPLXL(logl(hypotl(x, y)), atan2l(y, x)));
319 
320 	return (CMPLXL(logl(ax * ax + ay * ay) / 2, atan2l(y, x)));
321 }
322 
323 static inline long double
324 sum_squares(long double x, long double y)
325 {
326 
327 	if (y < SQRT_MIN)
328 		return (x * x);
329 
330 	return (x * x + y * y);
331 }
332 
333 static inline long double
334 real_part_reciprocal(long double x, long double y)
335 {
336 	long double scale;
337 	uint16_t hx, hy;
338 	int16_t ix, iy;
339 
340 	GET_LDBL_EXPSIGN(hx, x);
341 	ix = hx & 0x7fff;
342 	GET_LDBL_EXPSIGN(hy, y);
343 	iy = hy & 0x7fff;
344 #define	BIAS	(LDBL_MAX_EXP - 1)
345 #define	CUTOFF	(LDBL_MANT_DIG / 2 + 1)
346 	if (ix - iy >= CUTOFF || isinf(x))
347 		return (1 / x);
348 	if (iy - ix >= CUTOFF)
349 		return (x / y / y);
350 	if (ix <= BIAS + LDBL_MAX_EXP / 2 - CUTOFF)
351 		return (x / (x * x + y * y));
352 	scale = 1;
353 	SET_LDBL_EXPSIGN(scale, 0x7fff - ix);
354 	x *= scale;
355 	y *= scale;
356 	return (x / (x * x + y * y) * scale);
357 }
358 
359 long double complex
360 catanhl(long double complex z)
361 {
362 	long double x, y, ax, ay, rx, ry;
363 
364 	x = creall(z);
365 	y = cimagl(z);
366 	ax = fabsl(x);
367 	ay = fabsl(y);
368 
369 	if (y == 0 && ax <= 1)
370 		return (CMPLXL(atanhl(x), y));
371 
372 	if (x == 0)
373 		return (CMPLXL(x, atanl(y)));
374 
375 	if (isnan(x) || isnan(y)) {
376 		if (isinf(x))
377 			return (CMPLXL(copysignl(0, x), y + y));
378 		if (isinf(y))
379 			return (CMPLXL(copysignl(0, x),
380 			    copysignl(pio2_hi + pio2_lo, y)));
381 		return (CMPLXL(nan_mix(x, y), nan_mix(x, y)));
382 	}
383 
384 	if (ax > RECIP_EPSILON || ay > RECIP_EPSILON)
385 		return (CMPLXL(real_part_reciprocal(x, y),
386 		    copysignl(pio2_hi + pio2_lo, y)));
387 
388 	if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
389 		raise_inexact();
390 		return (z);
391 	}
392 
393 	if (ax == 1 && ay < LDBL_EPSILON)
394 		rx = (m_ln2 - logl(ay)) / 2;
395 	else
396 		rx = log1pl(4 * ax / sum_squares(ax - 1, ay)) / 4;
397 
398 	if (ax == 1)
399 		ry = atan2l(2, -ay) / 2;
400 	else if (ay < LDBL_EPSILON)
401 		ry = atan2l(2 * ay, (1 - ax) * (1 + ax)) / 2;
402 	else
403 		ry = atan2l(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;
404 
405 	return (CMPLXL(copysignl(rx, x), copysignl(ry, y)));
406 }
407 
408 long double complex
409 catanl(long double complex z)
410 {
411 	long double complex w;
412 
413 	w = catanhl(CMPLXL(cimagl(z), creall(z)));
414 	return (CMPLXL(cimagl(w), creall(w)));
415 }
416