xref: /freebsd/lib/msun/src/s_atanl.c (revision 3494f7c0)
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 
13 #include <sys/cdefs.h>
14 /*
15  * See comments in s_atan.c.
16  * Converted to long double by David Schultz <das@FreeBSD.ORG>.
17  */
18 
19 #include <float.h>
20 
21 #include "invtrig.h"
22 #include "math.h"
23 #include "math_private.h"
24 
25 static const long double
26 one   = 1.0,
27 huge   = 1.0e300;
28 
29 long double
30 atanl(long double x)
31 {
32 	union IEEEl2bits u;
33 	long double w,s1,s2,z;
34 	int id;
35 	int16_t expsign, expt;
36 	int32_t expman;
37 
38 	u.e = x;
39 	expsign = u.xbits.expsign;
40 	expt = expsign & 0x7fff;
41 	if(expt >= ATAN_CONST) {	/* if |x| is large, atan(x)~=pi/2 */
42 	    if(expt == BIAS + LDBL_MAX_EXP &&
43 	       ((u.bits.manh&~LDBL_NBIT)|u.bits.manl)!=0)
44 		return x+x;		/* NaN */
45 	    if(expsign>0) return  atanhi[3]+atanlo[3];
46 	    else     return -atanhi[3]-atanlo[3];
47 	}
48 	/* Extract the exponent and the first few bits of the mantissa. */
49 	/* XXX There should be a more convenient way to do this. */
50 	expman = (expt << 8) | ((u.bits.manh >> (MANH_SIZE - 9)) & 0xff);
51 	if (expman < ((BIAS - 2) << 8) + 0xc0) {	/* |x| < 0.4375 */
52 	    if (expt < ATAN_LINEAR) {	/* if |x| is small, atanl(x)~=x */
53 		if(huge+x>one) return x;	/* raise inexact */
54 	    }
55 	    id = -1;
56 	} else {
57 	x = fabsl(x);
58 	if (expman < (BIAS << 8) + 0x30) {		/* |x| < 1.1875 */
59 	    if (expman < ((BIAS - 1) << 8) + 0x60) {	/* 7/16 <=|x|<11/16 */
60 		id = 0; x = (2.0*x-one)/(2.0+x);
61 	    } else {			/* 11/16<=|x|< 19/16 */
62 		id = 1; x  = (x-one)/(x+one);
63 	    }
64 	} else {
65 	    if (expman < ((BIAS + 1) << 8) + 0x38) {	/* |x| < 2.4375 */
66 		id = 2; x  = (x-1.5)/(one+1.5*x);
67 	    } else {			/* 2.4375 <= |x| < 2^ATAN_CONST */
68 		id = 3; x  = -1.0/x;
69 	    }
70 	}}
71     /* end of argument reduction */
72 	z = x*x;
73 	w = z*z;
74     /* break sum aT[i]z**(i+1) into odd and even poly */
75 	s1 = z*T_even(w);
76 	s2 = w*T_odd(w);
77 	if (id<0) return x - x*(s1+s2);
78 	else {
79 	    z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
80 	    return (expsign<0)? -z:z;
81 	}
82 }
83