catrigl.c revision 1.1.2.2 1 /* $NetBSD: catrigl.c,v 1.1.2.2 2016/11/04 14:48:54 pgoyette Exp $ */
2 /*-
3 * Copyright (c) 2012 Stephen Montgomery-Smith <stephen (at) 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 * The code for catrig.c contains complete comments.
36 */
37 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: catrigl.c,v 1.1.2.2 2016/11/04 14:48:54 pgoyette Exp $");
39
40 #include "namespace.h"
41 #ifdef __weak_alias
42 __weak_alias(casinl, _casinl)
43 #endif
44 #ifdef __weak_alias
45 __weak_alias(catanl, _catanl)
46 #endif
47
48
49 #include <complex.h>
50 #include <float.h>
51 #ifdef __HAVE_LONG_DOUBLE
52
53 #include "math.h"
54 #include "math_private.h"
55
56 #undef isinf
57 #define isinf(x) (fabsl(x) == INFINITY)
58 #undef isnan
59 #define isnan(x) ((x) != (x))
60 #define raise_inexact() do { volatile float junk __unused = /*LINTED*/1 + tiny; } while(/*CONSTCOND*/0)
61 #undef signbit
62 #define signbit(x) (__builtin_signbitl(x))
63
64 #if __HAVE_LONG_DOUBLE + 0 == 128
65 // Ok
66 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
67 // XXX: Byte order
68 struct ieee_ext {
69 uint64_t ext_frac;
70 uint16_t ext_exp:15;
71 uint16_t ext_sign:1;
72 uint16_t ext_pad;
73 };
74 #define extu_exp extu_ext.ext_exp
75 #define extu_sign extu_ext.ext_sign
76 #define extu_frac extu_ext.ext_frac
77 union ieee_ext_u {
78 long double extu_ld;
79 struct ieee_ext extu_ext;
80 };
81 #else
82 #error "unsupported long double format"
83 #endif
84
85 #define GET_LDBL_EXPSIGN(r, s) \
86 do { \
87 union ieee_ext_u u; \
88 u.extu_ld = s; \
89 r = u.extu_sign; \
90 r >>= EXT_EXPBITS - 1;
91 } while (/*CONSTCOND*/0)
92 #define SET_LDBL_EXPSIGN(r, s) \
93 do { \
94 union ieee_ext_u u; \
95 u.extu_ld = s; \
96 u.extu_exp &= __BITS(0, EXT_EXPBITS - 1); \
97 u.extu_exp |= r << (EXT_EXPBITS - 1); \
98 s = u.extu_ld; \
99 } while (/*CONSTCOND*/0)
100
101 static const long double
102 A_crossover = 10,
103 B_crossover = 0.6417,
104 FOUR_SQRT_MIN = 0x1p-8189L,
105 QUARTER_SQRT_MAX = 0x1p8189L,
106 RECIP_EPSILON = 1/LDBL_EPSILON,
107 SQRT_MIN = 0x1p-8191L;
108
109 static const long double
110 m_e = 2.71828182845904523536028747135266250e0L, /* 0x15bf0a8b1457695355fb8ac404e7a.0p-111 */
111 m_ln2 = 6.93147180559945309417232121458176568e-1L, /* 0x162e42fefa39ef35793c7673007e6.0p-113 */
112 pio2_hi = 1.5707963267948966192313216916397514L, /* pi/2 */
113 SQRT_3_EPSILON = 2.40370335797945490975336727199878124e-17L, /* 0x1bb67ae8584caa73b25742d7078b8.0p-168 */
114 SQRT_6_EPSILON = 3.39934988877629587239082586223300391e-17L; /* 0x13988e1409212e7d0321914321a55.0p-167 */
115
116 static const volatile double
117 pio2_lo = 6.1232339957367659e-17; /* 0x11a62633145c07.0p-106 */
118 static const volatile float
119 tiny = 0x1p-100;
120
121 static long double complex clog_for_large_values(long double complex z);
122
123 inline static long double
124 f(long double a, long double b, long double hypot_a_b)
125 {
126 if (b < 0)
127 return ((hypot_a_b - b) / 2);
128 if (b == 0)
129 return (a / 2);
130 return (a * a / (hypot_a_b + b) / 2);
131 }
132
133 inline static void
134 do_hard_work(long double x, long double y, long double *rx, int *B_is_usable, long double *B, long double *sqrt_A2my2, long double *new_y)
135 {
136 long double R, S, A;
137 long double Am1, Amy;
138
139 R = hypotl(x, y+1);
140 S = hypotl(x, y-1);
141
142 A = (R + S) / 2;
143 if (A < 1)
144 A = 1;
145
146 if (A < A_crossover) {
147 if (y == 1 && x < LDBL_EPSILON*LDBL_EPSILON/128) {
148 *rx = sqrtl(x);
149 } else if (x >= LDBL_EPSILON * fabsl(y-1)) {
150 Am1 = f(x, 1+y, R) + f(x, 1-y, S);
151 *rx = log1pl(Am1 + sqrtl(Am1*(A+1)));
152 } else if (y < 1) {
153 *rx = x/sqrtl((1-y)*(1+y));
154 } else {
155 *rx = log1pl((y-1) + sqrtl((y-1)*(y+1)));
156 }
157 } else
158 *rx = logl(A + sqrtl(A*A-1));
159
160 *new_y = y;
161
162 if (y < FOUR_SQRT_MIN) {
163 *B_is_usable = 0;
164 *sqrt_A2my2 = A * (2 / LDBL_EPSILON);
165 *new_y= y * (2 / LDBL_EPSILON);
166 return;
167 }
168
169 *B = y/A;
170 *B_is_usable = 1;
171
172 if (*B > B_crossover) {
173 *B_is_usable = 0;
174 if (y == 1 && x < LDBL_EPSILON/128) {
175 *sqrt_A2my2 = sqrtl(x)*sqrtl((A+y)/2);
176 } else if (x >= LDBL_EPSILON * fabsl(y-1)) {
177 Amy = f(x, y+1, R) + f(x, y-1, S);
178 *sqrt_A2my2 = sqrtl(Amy*(A+y));
179 } else if (y > 1) {
180 *sqrt_A2my2 = x * (4/LDBL_EPSILON/LDBL_EPSILON) * y /
181 sqrtl((y+1)*(y-1));
182 *new_y = y * (4/LDBL_EPSILON/LDBL_EPSILON);
183 } else {
184 *sqrt_A2my2 = sqrtl((1-y)*(1+y));
185 }
186 }
187 }
188
189 long double complex
190 casinhl(long double complex z)
191 {
192 long double x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y;
193 int B_is_usable;
194 long double complex w;
195
196 x = creall(z);
197 y = cimagl(z);
198 ax = fabsl(x);
199 ay = fabsl(y);
200
201 if (isnan(x) || isnan(y)) {
202 if (isinf(x))
203 return (CMPLXL(x, y+y));
204 if (isinf(y))
205 return (CMPLXL(y, x+x));
206 if (y == 0) return (CMPLXL(x+x, y));
207 return (CMPLXL(x+0.0L+(y+0), x+0.0L+(y+0)));
208 }
209
210 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
211 if (signbit(x) == 0)
212 w = clog_for_large_values(z) + m_ln2;
213 else
214 w = clog_for_large_values(-z) + m_ln2;
215 return (CMPLXL(copysignl(creall(w), x), copysignl(cimagl(w), y)));
216 }
217
218 if (x == 0 && y == 0)
219 return (z);
220
221 raise_inexact();
222
223 if (ax < SQRT_6_EPSILON/4 && ay < SQRT_6_EPSILON/4)
224 return (z);
225
226 do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y);
227 if (B_is_usable)
228 ry = asinl(B);
229 else
230 ry = atan2l(new_y, sqrt_A2my2);
231 return (CMPLXL(copysignl(rx, x), copysignl(ry, y)));
232 }
233
234 long double complex
235 casinl(long double complex z)
236 {
237 long double complex w = casinhl(CMPLXL(cimagl(z), creall(z)));
238 return (CMPLXL(cimagl(w), creall(w)));
239 }
240
241 long double complex
242 cacosl(long double complex z)
243 {
244 long double x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x;
245 int sx, sy;
246 int B_is_usable;
247 long double complex w;
248
249 x = creall(z);
250 y = cimagl(z);
251 sx = signbit(x);
252 sy = signbit(y);
253 ax = fabsl(x);
254 ay = fabsl(y);
255
256 if (isnan(x) || isnan(y)) {
257 if (isinf(x))
258 return (CMPLXL(y+y, -INFINITY));
259 if (isinf(y))
260 return (CMPLXL(x+x, -y));
261 if (x == 0) return (CMPLXL(pio2_hi + pio2_lo, y+y));
262 return (CMPLXL(x+0.0L+(y+0), x+0.0L+(y+0)));
263 }
264
265 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
266 w = clog_for_large_values(z);
267 rx = fabsl(cimagl(w));
268 ry = creall(w) + m_ln2;
269 if (sy == 0)
270 ry = -ry;
271 return (CMPLXL(rx, ry));
272 }
273
274 if (x == 1 && y == 0)
275 return (CMPLXL(0, -y));
276
277 raise_inexact();
278
279 if (ax < SQRT_6_EPSILON/4 && ay < SQRT_6_EPSILON/4)
280 return (CMPLXL(pio2_hi - (x - pio2_lo), -y));
281
282 do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x);
283 if (B_is_usable) {
284 if (sx==0)
285 rx = acosl(B);
286 else
287 rx = acosl(-B);
288 } else {
289 if (sx==0)
290 rx = atan2l(sqrt_A2mx2, new_x);
291 else
292 rx = atan2l(sqrt_A2mx2, -new_x);
293 }
294 if (sy==0)
295 ry = -ry;
296 return (CMPLXL(rx, ry));
297 }
298
299 long double complex
300 cacoshl(long double complex z)
301 {
302 long double complex w;
303 long double rx, ry;
304
305 w = cacosl(z);
306 rx = creall(w);
307 ry = cimagl(w);
308 if (isnan(rx) && isnan(ry))
309 return (CMPLXL(ry, rx));
310 if (isnan(rx))
311 return (CMPLXL(fabsl(ry), rx));
312 if (isnan(ry))
313 return (CMPLXL(ry, ry));
314 return (CMPLXL(fabsl(ry), copysignl(rx, cimagl(z))));
315 }
316
317 static long double complex
318 clog_for_large_values(long double complex z)
319 {
320 long double x, y;
321 long double ax, ay, t;
322
323 x = creall(z);
324 y = cimagl(z);
325 ax = fabsl(x);
326 ay = fabsl(y);
327 if (ax < ay) {
328 t = ax;
329 ax = ay;
330 ay = t;
331 }
332
333 if (ax > LDBL_MAX / 2)
334 return (CMPLXL(logl(hypotl(x / m_e, y / m_e)) + 1, atan2l(y, x)));
335
336 if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN)
337 return (CMPLXL(logl(hypotl(x, y)), atan2l(y, x)));
338
339 return (CMPLXL(logl(ax*ax + ay*ay) / 2, atan2l(y, x)));
340 }
341
342 inline static long double
343 sum_squares(long double x, long double y)
344 {
345 if (y < SQRT_MIN)
346 return (x*x);
347
348 return (x*x + y*y);
349 }
350
351 inline static long double
352 real_part_reciprocal(long double x, long double y)
353 {
354 long double scale;
355 uint16_t hx, hy;
356 int16_t ix, iy;
357
358 GET_LDBL_EXPSIGN(hx, x);
359 ix = hx & 0x7fff;
360 GET_LDBL_EXPSIGN(hy, y);
361 iy = hy & 0x7fff;
362 #define BIAS (LDBL_MAX_EXP - 1)
363 #define CUTOFF (LDBL_MANT_DIG / 2 + 1)
364 if (ix - iy >= CUTOFF || isinf(x))
365 return (1/x);
366 if (iy - ix >= CUTOFF)
367 return (x/y/y);
368 if (ix <= BIAS + LDBL_MAX_EXP / 2 - CUTOFF)
369 return (x/(x*x + y*y));
370 scale = 1;
371 SET_LDBL_EXPSIGN(scale, 0x7fff - ix);
372 x *= scale;
373 y *= scale;
374 return (x/(x*x + y*y) * scale);
375 }
376
377 long double complex
378 catanhl(long double complex z)
379 {
380 long double x, y, ax, ay, rx, ry;
381
382 x = creall(z);
383 y = cimagl(z);
384 ax = fabsl(x);
385 ay = fabsl(y);
386
387 if (y == 0 && ax <= 1)
388 return (CMPLXL(atanhl(x), y)); /* XXX need atanhl() */
389
390 if (x == 0)
391 return (CMPLXL(x, atanl(y)));
392
393 if (isnan(x) || isnan(y)) {
394 if (isinf(x))
395 return (CMPLXL(copysignl(0, x), y+y));
396 if (isinf(y))
397 return (CMPLXL(copysignl(0, x), copysignl(pio2_hi + pio2_lo, y)));
398 return (CMPLXL(x+0.0L+(y+0), x+0.0L+(y+0)));
399 }
400
401 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON)
402 return (CMPLXL(real_part_reciprocal(x, y), copysignl(pio2_hi + pio2_lo, y)));
403
404 if (ax < SQRT_3_EPSILON/2 && ay < SQRT_3_EPSILON/2) {
405 raise_inexact();
406 return (z);
407 }
408
409 if (ax == 1 && ay < LDBL_EPSILON) {
410 #if 0
411 if (ay > 2*LDBL_MIN)
412 rx = - logl(ay/2) / 2;
413 else
414 #endif
415 rx = - (logl(ay) - m_ln2) / 2;
416 } else
417 rx = log1pl(4*ax / sum_squares(ax-1, ay)) / 4;
418
419 if (ax == 1)
420 ry = atan2l(2, -ay) / 2;
421 else if (ay < LDBL_EPSILON)
422 ry = atan2l(2*ay, (1-ax)*(1+ax)) / 2;
423 else
424 ry = atan2l(2*ay, (1-ax)*(1+ax) - ay*ay) / 2;
425
426 return (CMPLXL(copysignl(rx, x), copysignl(ry, y)));
427 }
428
429 long double complex
430 catanl(long double complex z)
431 {
432 long double complex w = catanhl(CMPLXL(cimagl(z), creall(z)));
433 return (CMPLXL(cimagl(w), creall(w)));
434 }
435
436 #else
437 __strong_alias(_casinl, casin)
438 __strong_alias(_catanl, catan)
439 __strong_alias(cacoshl, cacosh)
440 __strong_alias(cacosl, cacos)
441 __strong_alias(casinhl, casinh)
442 __strong_alias(catanhl, catanh)
443 #endif
444