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