117303c62SDavid Schultz /* FreeBSD: head/lib/msun/src/s_atan.c 176451 2008-02-22 02:30:36Z das */
217303c62SDavid Schultz /*
317303c62SDavid Schultz * ====================================================
417303c62SDavid Schultz * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
517303c62SDavid Schultz *
617303c62SDavid Schultz * Developed at SunPro, a Sun Microsystems, Inc. business.
717303c62SDavid Schultz * Permission to use, copy, modify, and distribute this
817303c62SDavid Schultz * software is freely granted, provided that this notice
917303c62SDavid Schultz * is preserved.
1017303c62SDavid Schultz * ====================================================
1117303c62SDavid Schultz */
1217303c62SDavid Schultz
1317303c62SDavid Schultz /*
1417303c62SDavid Schultz * See comments in s_atan.c.
1517303c62SDavid Schultz * Converted to long double by David Schultz <das@FreeBSD.ORG>.
1617303c62SDavid Schultz */
1717303c62SDavid Schultz
1817303c62SDavid Schultz #include <float.h>
1917303c62SDavid Schultz
2017303c62SDavid Schultz #include "invtrig.h"
2117303c62SDavid Schultz #include "math.h"
2217303c62SDavid Schultz #include "math_private.h"
2317303c62SDavid Schultz
2417303c62SDavid Schultz static const long double
2517303c62SDavid Schultz one = 1.0,
2617303c62SDavid Schultz huge = 1.0e300;
2717303c62SDavid Schultz
2817303c62SDavid Schultz long double
atanl(long double x)2917303c62SDavid Schultz atanl(long double x)
3017303c62SDavid Schultz {
3117303c62SDavid Schultz union IEEEl2bits u;
3217303c62SDavid Schultz long double w,s1,s2,z;
3317303c62SDavid Schultz int id;
3417303c62SDavid Schultz int16_t expsign, expt;
3517303c62SDavid Schultz int32_t expman;
3617303c62SDavid Schultz
3717303c62SDavid Schultz u.e = x;
3817303c62SDavid Schultz expsign = u.xbits.expsign;
3917303c62SDavid Schultz expt = expsign & 0x7fff;
4017303c62SDavid Schultz if(expt >= ATAN_CONST) { /* if |x| is large, atan(x)~=pi/2 */
4117303c62SDavid Schultz if(expt == BIAS + LDBL_MAX_EXP &&
4217303c62SDavid Schultz ((u.bits.manh&~LDBL_NBIT)|u.bits.manl)!=0)
4317303c62SDavid Schultz return x+x; /* NaN */
4417303c62SDavid Schultz if(expsign>0) return atanhi[3]+atanlo[3];
4517303c62SDavid Schultz else return -atanhi[3]-atanlo[3];
4617303c62SDavid Schultz }
4717303c62SDavid Schultz /* Extract the exponent and the first few bits of the mantissa. */
4817303c62SDavid Schultz /* XXX There should be a more convenient way to do this. */
4917303c62SDavid Schultz expman = (expt << 8) | ((u.bits.manh >> (MANH_SIZE - 9)) & 0xff);
5017303c62SDavid Schultz if (expman < ((BIAS - 2) << 8) + 0xc0) { /* |x| < 0.4375 */
5117303c62SDavid Schultz if (expt < ATAN_LINEAR) { /* if |x| is small, atanl(x)~=x */
5217303c62SDavid Schultz if(huge+x>one) return x; /* raise inexact */
5317303c62SDavid Schultz }
5417303c62SDavid Schultz id = -1;
5517303c62SDavid Schultz } else {
5617303c62SDavid Schultz x = fabsl(x);
5717303c62SDavid Schultz if (expman < (BIAS << 8) + 0x30) { /* |x| < 1.1875 */
5817303c62SDavid Schultz if (expman < ((BIAS - 1) << 8) + 0x60) { /* 7/16 <=|x|<11/16 */
5917303c62SDavid Schultz id = 0; x = (2.0*x-one)/(2.0+x);
6017303c62SDavid Schultz } else { /* 11/16<=|x|< 19/16 */
6117303c62SDavid Schultz id = 1; x = (x-one)/(x+one);
6217303c62SDavid Schultz }
6317303c62SDavid Schultz } else {
6417303c62SDavid Schultz if (expman < ((BIAS + 1) << 8) + 0x38) { /* |x| < 2.4375 */
6517303c62SDavid Schultz id = 2; x = (x-1.5)/(one+1.5*x);
6617303c62SDavid Schultz } else { /* 2.4375 <= |x| < 2^ATAN_CONST */
6717303c62SDavid Schultz id = 3; x = -1.0/x;
6817303c62SDavid Schultz }
6917303c62SDavid Schultz }}
7017303c62SDavid Schultz /* end of argument reduction */
7117303c62SDavid Schultz z = x*x;
7217303c62SDavid Schultz w = z*z;
7317303c62SDavid Schultz /* break sum aT[i]z**(i+1) into odd and even poly */
7417303c62SDavid Schultz s1 = z*T_even(w);
7517303c62SDavid Schultz s2 = w*T_odd(w);
7617303c62SDavid Schultz if (id<0) return x - x*(s1+s2);
7717303c62SDavid Schultz else {
7817303c62SDavid Schultz z = atanhi[id] - ((x*(s1+s2) - atanlo[id]) - x);
7917303c62SDavid Schultz return (expsign<0)? -z:z;
8017303c62SDavid Schultz }
8117303c62SDavid Schultz }
82