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