1 /* @(#)s_atan.c 5.1 93/09/24 */
2 /* FreeBSD: head/lib/msun/src/s_atan.c 176451 2008-02-22 02:30:36Z das */
3 /*
4  * ====================================================
5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6  *
7  * Developed at SunPro, a Sun Microsystems, Inc. business.
8  * Permission to use, copy, modify, and distribute this
9  * software is freely granted, provided that this notice
10  * is preserved.
11  * ====================================================
12  */
13 
14 
15 /*
16  * See comments in s_atan.c.
17  * Converted to long double by David Schultz <das@FreeBSD.ORG>.
18  */
19 
20 #include "invtrig.h"
21 
22 static const long double
23 one   = 1.0l,
24 huge   = 1.0e300l;
25 
26 long double
atanl(long double x)27 atanl(long double x)
28 {
29 	union IEEEl2bits u;
30 	long double w,s1,s2,z;
31 	int id;
32 	int16_t expsign, expt;
33 	int32_t expman;
34 
35 	u.e = x;
36 	expsign = u.xbits.expsign;
37 	expt = expsign & 0x7fff;
38 	if(expt >= ATAN_CONST) {	/* if |x| is large, atan(x)~=pi/2 */
39 	    if(expt == BIAS + LDBL_MAX_EXP &&
40 	       ((u.bits.manh&~LDBL_NBIT)|u.bits.manl)!=0)
41 		return x+x;		/* NaN */
42 	    if(expsign>0) return  atanhi[3]+atanlo[3];
43 	    else     return -atanhi[3]-atanlo[3];
44 	}
45 	/* Extract the exponent and the first few bits of the mantissa. */
46 	/* XXX There should be a more convenient way to do this. */
47 	expman = (expt << 8) | ((u.bits.manh >> (MANH_SIZE - 9)) & 0xff);
48 	if (expman < ((BIAS - 2) << 8) + 0xc0) {	/* |x| < 0.4375 */
49 	    if (expt < ATAN_LINEAR) {	/* if |x| is small, atanl(x)~=x */
50 		if(huge+x>one) return x;	/* raise inexact */
51 	    }
52 	    id = -1;
53 	} else {
54 	x = fabsl(x);
55 	if (expman < (BIAS << 8) + 0x30) {		/* |x| < 1.1875 */
56 	    if (expman < ((BIAS - 1) << 8) + 0x60) {	/* 7/16 <=|x|<11/16 */
57 		id = 0; x = (2.0l*x-one)/(2.0l+x);
58 	    } else {			/* 11/16<=|x|< 19/16 */
59 		id = 1; x  = (x-one)/(x+one);
60 	    }
61 	} else {
62 	    if (expman < ((BIAS + 1) << 8) + 0x38) {	/* |x| < 2.4375 */
63 		id = 2; x  = (x-1.5l)/(one+1.5l*x);
64 	    } else {			/* 2.4375 <= |x| < 2^ATAN_CONST */
65 		id = 3; x  = -1.0l/x;
66 	    }
67 	}}
68     /* end of argument reduction */
69 	z = x*x;
70 	w = z*z;
71     /* break sum aT[i]z**(i+1) into odd and even poly */
72 	s1 = z*T_even(w);
73 	s2 = w*T_odd(w);
74 	if (id<0) return x - x*(s1+s2);
75 	else {
76 	    z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
77 	    return (expsign<0)? -z:z;
78 	}
79 }
80