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