xref: /netbsd-src/lib/libm/src/s_atanl.c (revision 345cf9fb81bd0411c53e25d62cd93bdcaa865312)
1 /* FreeBSD: head/lib/msun/src/s_atan.c 176451 2008-02-22 02:30:36Z das */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 #include <sys/cdefs.h>
13 
14 #include "namespace.h"
15 
16 #include <float.h>
17 #include <machine/ieee.h>
18 
19 #include "math.h"
20 #include "math_private.h"
21 
22 #ifdef __weak_alias
23 __weak_alias(atanl, _atanl)
24 #endif
25 
26 #ifdef __HAVE_LONG_DOUBLE
27 
28 /*
29  * See comments in s_atan.c.
30  * Converted to long double by David Schultz <das@FreeBSD.ORG>.
31  */
32 
33 
34 #if LDBL_MANT_DIG == 64
35 #include "../ld80/invtrig.h"
36 #elif LDBL_MANT_DIG == 113
37 #include "../ld128/invtrig.h"
38 #else
39 #error "Unsupported long double format"
40 #endif
41 
42 #ifdef LDBL_IMPLICIT_NBIT
43 #define	LDBL_NBIT	0
44 #endif
45 
46 static const long double
47 one   = 1.0,
48 huge   = 1.0e300;
49 
50 long double
51 atanl(long double x)
52 {
53 	union ieee_ext_u u;
54 	long double w,s1,s2,z;
55 	int id;
56 	int16_t expsign, expt;
57 	int32_t expman;
58 
59 	u.extu_ld = x;
60 	expsign = GET_EXPSIGN(&u);
61 	expt = expsign & 0x7fff;
62 	if(expt >= ATAN_CONST) {	/* if |x| is large, atan(x)~=pi/2 */
63 	    if(expt == BIAS + LDBL_MAX_EXP &&
64 	       ((u.extu_frach&~LDBL_NBIT)|u.extu_fracl)!=0)
65 		return x+x;		/* NaN */
66 	    if(expsign>0) return  atanhi[3]+atanlo[3];
67 	    else     return -atanhi[3]-atanlo[3];
68 	}
69 	/* Extract the exponent and the first few bits of the mantissa. */
70 	/* XXX There should be a more convenient way to do this. */
71 	expman = (expt << 8) | ((u.extu_frach >> (MANH_SIZE - 9)) & 0xff);
72 	if (expman < ((BIAS - 2) << 8) + 0xc0) {	/* |x| < 0.4375 */
73 	    if (expt < ATAN_LINEAR) {	/* if |x| is small, atanl(x)~=x */
74 		if(huge+x>one) return x;	/* raise inexact */
75 	    }
76 	    id = -1;
77 	} else {
78 	x = fabsl(x);
79 	if (expman < (BIAS << 8) + 0x30) {		/* |x| < 1.1875 */
80 	    if (expman < ((BIAS - 1) << 8) + 0x60) {	/* 7/16 <=|x|<11/16 */
81 		id = 0; x = (2.0*x-one)/(2.0+x);
82 	    } else {			/* 11/16<=|x|< 19/16 */
83 		id = 1; x  = (x-one)/(x+one);
84 	    }
85 	} else {
86 	    if (expman < ((BIAS + 1) << 8) + 0x38) {	/* |x| < 2.4375 */
87 		id = 2; x  = (x-1.5)/(one+1.5*x);
88 	    } else {			/* 2.4375 <= |x| < 2^ATAN_CONST */
89 		id = 3; x  = -1.0/x;
90 	    }
91 	}}
92     /* end of argument reduction */
93 	z = x*x;
94 	w = z*z;
95     /* break sum aT[i]z**(i+1) into odd and even poly */
96 	s1 = z*T_even(w);
97 	s2 = w*T_odd(w);
98 	if (id<0) return x - x*(s1+s2);
99 	else {
100 	    z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
101 	    return (expsign<0)? -z:z;
102 	}
103 }
104 #else
105 long double
106 atanl(long double x)
107 {
108 	return atan(x);
109 }
110 #endif
111