f44a319bad5d28b947838da113e70f0743657e60
[dragonfly.git] / lib / libm / src / catrigf.c
1 /*-
2  * Copyright (c) 2012 Stephen Montgomery-Smith <stephen@FreeBSD.ORG>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: head/lib/msun/src/catrigf.c 251121 2013-05-30 04:49:26Z das $
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  * The code for catrig.c contains complete comments.
37  */
38
39 #include <complex.h>
40 #include <float.h>
41
42 #include "math.h"
43 #include "math_private.h"
44
45 #undef isinf
46 #define isinf(x)        (fabsf(x) == INFINITY)
47 #undef isnan
48 #define isnan(x)        ((x) != (x))
49 #define raise_inexact() do { volatile float junk = 1 + tiny; } while(0)
50 #undef signbit
51 #define signbit(x)      (__builtin_signbitf(x))
52
53 static const float
54 A_crossover =           10,
55 B_crossover =           0.6417,
56 FOUR_SQRT_MIN =         0x1p-61,
57 QUARTER_SQRT_MAX =      0x1p61,
58 m_e =                   2.7182818285e0,         /*  0xadf854.0p-22 */
59 m_ln2 =                 6.9314718056e-1,        /*  0xb17218.0p-24 */
60 pio2_hi =               1.5707962513e0,         /*  0xc90fda.0p-23 */
61 RECIP_EPSILON =         1 / FLT_EPSILON,
62 SQRT_3_EPSILON =        5.9801995673e-4,        /*  0x9cc471.0p-34 */
63 SQRT_6_EPSILON =        8.4572793338e-4,        /*  0xddb3d7.0p-34 */
64 SQRT_MIN =              0x1p-63;
65
66 static const volatile float
67 pio2_lo =               7.5497899549e-8,        /*  0xa22169.0p-47 */
68 tiny =                  0x1p-100;
69
70 static float complex clog_for_large_values(float complex z);
71
72 static inline float
73 f(float a, float b, float hypot_a_b)
74 {
75         if (b < 0)
76                 return ((hypot_a_b - b) / 2);
77         if (b == 0)
78                 return (a / 2);
79         return (a * a / (hypot_a_b + b) / 2);
80 }
81
82 static inline void
83 do_hard_work(float x, float y, float *rx, int *B_is_usable, float *B,
84              float *sqrt_A2my2, float *new_y)
85 {
86         float R, S, A;
87         float Am1, Amy;
88
89         R = hypotf(x, y + 1);
90         S = hypotf(x, y - 1);
91
92         A = (R + S) / 2;
93         if (A < 1)
94                 A = 1;
95
96         if (A < A_crossover) {
97                 if (y == 1 && x < FLT_EPSILON * FLT_EPSILON / 128) {
98                         *rx = sqrtf(x);
99                 } else if (x >= FLT_EPSILON * fabsf(y - 1)) {
100                         Am1 = f(x, 1 + y, R) + f(x, 1 - y, S);
101                         *rx = log1pf(Am1 + sqrtf(Am1 * (A + 1)));
102                 } else if (y < 1) {
103                         *rx = x / sqrtf((1 - y)*(1 + y));
104                 } else {
105                         *rx = log1pf((y - 1) + sqrtf((y - 1) * (y + 1)));
106                 }
107         } else {
108                 *rx = logf(A + sqrtf(A * A - 1));
109         }
110
111         *new_y = y;
112
113         if (y < FOUR_SQRT_MIN) {
114                 *B_is_usable = 0;
115                 *sqrt_A2my2 = A * (2 / FLT_EPSILON);
116                 *new_y = y * (2 / FLT_EPSILON);
117                 return;
118         }
119
120         *B = y / A;
121         *B_is_usable = 1;
122
123         if (*B > B_crossover) {
124                 *B_is_usable = 0;
125                 if (y == 1 && x < FLT_EPSILON / 128) {
126                         *sqrt_A2my2 = sqrtf(x) * sqrtf((A + y) / 2);
127                 } else if (x >= FLT_EPSILON * fabsf(y - 1)) {
128                         Amy = f(x, y + 1, R) + f(x, y - 1, S);
129                         *sqrt_A2my2 = sqrtf(Amy * (A + y));
130                 } else if (y > 1) {
131                         *sqrt_A2my2 = x * (4 / FLT_EPSILON / FLT_EPSILON) * y /
132                                 sqrtf((y + 1) * (y - 1));
133                         *new_y = y * (4 / FLT_EPSILON / FLT_EPSILON);
134                 } else {
135                         *sqrt_A2my2 = sqrtf((1 - y) * (1 + y));
136                 }
137         }
138 }
139
140 float complex
141 casinhf(float complex z)
142 {
143         float x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y;
144         int B_is_usable;
145         float complex w;
146
147         x = crealf(z);
148         y = cimagf(z);
149         ax = fabsf(x);
150         ay = fabsf(y);
151
152         if (isnan(x) || isnan(y)) {
153                 if (isinf(x))
154                         return (cpackf(x, y + y));
155                 if (isinf(y))
156                         return (cpackf(y, x + x));
157                 if (y == 0)
158                         return (cpackf(x + x, y));
159                 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
160         }
161
162         if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
163                 if (signbit(x) == 0)
164                         w = clog_for_large_values(z) + m_ln2;
165                 else
166                         w = clog_for_large_values(-z) + m_ln2;
167                 return (cpackf(copysignf(crealf(w), x),
168                                copysignf(cimagf(w), y)));
169         }
170
171         if (x == 0 && y == 0)
172                 return (z);
173
174         raise_inexact();
175
176         if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
177                 return (z);
178
179         do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y);
180         if (B_is_usable)
181                 ry = asinf(B);
182         else
183                 ry = atan2f(new_y, sqrt_A2my2);
184         return (cpackf(copysignf(rx, x), copysignf(ry, y)));
185 }
186
187 float complex
188 casinf(float complex z)
189 {
190         float complex w = casinhf(cpackf(cimagf(z), crealf(z)));
191         return (cpackf(cimagf(w), crealf(w)));
192 }
193
194 float complex
195 cacosf(float complex z)
196 {
197         float x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x;
198         int sx, sy;
199         int B_is_usable;
200         float complex w;
201
202         x = crealf(z);
203         y = cimagf(z);
204         sx = signbit(x);
205         sy = signbit(y);
206         ax = fabsf(x);
207         ay = fabsf(y);
208
209         if (isnan(x) || isnan(y)) {
210                 if (isinf(x))
211                         return (cpackf(y + y, -INFINITY));
212                 if (isinf(y))
213                         return (cpackf(x + x, -y));
214                 if (x == 0) return (cpackf(pio2_hi + pio2_lo, y + y));
215                 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
216         }
217
218         if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
219                 w = clog_for_large_values(z);
220                 rx = fabsf(cimagf(w));
221                 ry = crealf(w) + m_ln2;
222                 if (sy == 0)
223                         ry = -ry;
224                 return (cpackf(rx, ry));
225         }
226
227         if (x == 1 && y == 0)
228                 return (cpackf(0, -y));
229
230         raise_inexact();
231
232         if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
233                 return (cpackf(pio2_hi - (x - pio2_lo), -y));
234
235         do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x);
236         if (B_is_usable) {
237                 if (sx==0)
238                         rx = acosf(B);
239                 else
240                         rx = acosf(-B);
241         } else {
242                 if (sx==0)
243                         rx = atan2f(sqrt_A2mx2, new_x);
244                 else
245                         rx = atan2f(sqrt_A2mx2, -new_x);
246         }
247         if (sy==0)
248                 ry = -ry;
249         return (cpackf(rx, ry));
250 }
251
252 float complex
253 cacoshf(float complex z)
254 {
255         float complex w;
256         float rx, ry;
257
258         w = cacosf(z);
259         rx = crealf(w);
260         ry = cimagf(w);
261         if (isnan(rx) && isnan(ry))
262                 return (cpackf(ry, rx));
263         if (isnan(rx))
264                 return (cpackf(fabsf(ry), rx));
265         if (isnan(ry))
266                 return (cpackf(ry, ry));
267         return (cpackf(fabsf(ry), copysignf(rx, cimagf(z))));
268 }
269
270 static float complex
271 clog_for_large_values(float complex z)
272 {
273         float x, y;
274         float ax, ay, t;
275
276         x = crealf(z);
277         y = cimagf(z);
278         ax = fabsf(x);
279         ay = fabsf(y);
280         if (ax < ay) {
281                 t = ax;
282                 ax = ay;
283                 ay = t;
284         }
285
286         if (ax > FLT_MAX / 2) {
287                 return (cpackf(logf(hypotf(x / m_e, y / m_e)) + 1,
288                                atan2f(y, x)));
289         }
290
291         if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN)
292                 return (cpackf(logf(hypotf(x, y)), atan2f(y, x)));
293
294         return (cpackf(logf(ax * ax + ay * ay) / 2, atan2f(y, x)));
295 }
296
297 static inline float
298 sum_squares(float x, float y)
299 {
300
301         if (y < SQRT_MIN)
302                 return (x*x);
303         return (x*x + y*y);
304 }
305
306 static inline float
307 real_part_reciprocal(float x, float y)
308 {
309         float scale;
310         uint32_t hx, hy;
311         int32_t ix, iy;
312
313         GET_FLOAT_WORD(hx, x);
314         ix = hx & 0x7f800000;
315         GET_FLOAT_WORD(hy, y);
316         iy = hy & 0x7f800000;
317 #define BIAS    (FLT_MAX_EXP - 1)
318 #define CUTOFF  (FLT_MANT_DIG / 2 + 1)
319         if (ix - iy >= CUTOFF << 23 || isinf(x))
320                 return (1/x);
321         if (iy - ix >= CUTOFF << 23)
322                 return (x/y/y);
323         if (ix <= (BIAS + FLT_MAX_EXP / 2 - CUTOFF) << 23)
324                 return (x / (x * x + y * y));
325         SET_FLOAT_WORD(scale, 0x7f800000 - ix);
326         x *= scale;
327         y *= scale;
328         return (x / (x * x + y * y) * scale);
329 }
330
331 float complex
332 catanhf(float complex z)
333 {
334         float x, y, ax, ay, rx, ry;
335
336         x = crealf(z);
337         y = cimagf(z);
338         ax = fabsf(x);
339         ay = fabsf(y);
340
341         if (y == 0 && ax <= 1)
342                 return (cpackf(atanhf(x), y));
343
344         if (x == 0)
345                 return (cpackf(x, atanf(y)));
346
347         if (isnan(x) || isnan(y)) {
348                 if (isinf(x))
349                         return (cpackf(copysignf(0, x), y+y));
350                 if (isinf(y)) {
351                         return (cpackf(copysignf(0, x),
352                                        copysignf(pio2_hi + pio2_lo, y)));
353                 }
354                 return (cpackf(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
355         }
356
357         if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
358                 return (cpackf(real_part_reciprocal(x, y),
359                                copysignf(pio2_hi + pio2_lo, y)));
360         }
361
362         if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
363                 raise_inexact();
364                 return (z);
365         }
366
367         if (ax == 1 && ay < FLT_EPSILON)
368                 rx = (logf(ay) - m_ln2) / -2;
369         else
370                 rx = log1pf(4 * ax / sum_squares(ax - 1, ay)) / 4;
371
372         if (ax == 1)
373                 ry = atan2f(2, -ay) / 2;
374         else if (ay < FLT_EPSILON)
375                 ry = atan2f(2 * ay, (1 - ax) * (1 + ax)) / 2;
376         else
377                 ry = atan2f(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;
378
379         return (cpackf(copysignf(rx, x), copysignf(ry, y)));
380 }
381
382 float complex
383 catanf(float complex z)
384 {
385         float complex w = catanhf(cpackf(cimagf(z), crealf(z)));
386         return (cpackf(cimagf(w), crealf(w)));
387 }