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