an update to previous patch fixes nearly all remaining constants used in the math...
[AROS.git] / compiler / stdc / math / e_atan2l.c
blobdeea300aa02c21e185032ca3f30a6a11bf2505da
1 /*
2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 * Developed at SunPro, a Sun Microsystems, Inc. business.
6 * Permission to use, copy, modify, and distribute this
7 * software is freely granted, provided that this notice
8 * is preserved.
9 * ====================================================
12 #ifndef lint
13 static char rcsid[] = "$FreeBSD: src/lib/msun/src/e_atan2l.c,v 1.3 2008/08/02 19:17:00 das Exp $";
14 #endif
16 #include <float.h>
17 #include "math.h"
19 #include "invtrig.h"
20 #include "math_private.h"
22 static const volatile long double
23 tiny __attribute__ ((__section__(".rodata"))) = 1.0e-300;
24 static const long double
25 zero = 0.0;
27 #ifdef __i386__
28 /* XXX Work around the fact that gcc truncates long double constants on i386 */
29 static const volatile double
30 pi1 __attribute__ ((__section__(".rodata"))) = 3.14159265358979311600e+00, /* 0x1.921fb54442d18p+1 */
31 pi2 __attribute__ ((__section__(".rodata"))) = 1.22514845490862001043e-16; /* 0x1.1a80000000000p-53 */
32 #define pi ((long double)pi1 + pi2)
33 #else
34 static const long double
35 pi = 3.14159265358979323846264338327950280e+00L;
36 #endif
38 long double
39 atan2l(long double y, long double x)
41 union IEEEl2bits ux, uy;
42 long double z;
43 int32_t k,m;
44 int16_t exptx, expsignx, expty, expsigny;
46 uy.e = y;
47 expsigny = uy.xbits.expsign;
48 expty = expsigny & 0x7fff;
49 ux.e = x;
50 expsignx = ux.xbits.expsign;
51 exptx = expsignx & 0x7fff;
53 if ((exptx==BIAS+LDBL_MAX_EXP &&
54 ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)!=0) || /* x is NaN */
55 (expty==BIAS+LDBL_MAX_EXP &&
56 ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* y is NaN */
57 return x+y;
58 if (expsignx==BIAS && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
59 return atanl(y); /* x=1.0 */
60 m = ((expsigny>>15)&1)|((expsignx>>14)&2); /* 2*sign(x)+sign(y) */
62 /* when y = 0 */
63 if(expty==0 && ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)==0) {
64 switch(m) {
65 case 0:
66 case 1: return y; /* atan(+-0,+anything)=+-0 */
67 case 2: return pi+tiny;/* atan(+0,-anything) = pi */
68 case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
71 /* when x = 0 */
72 if(exptx==0 && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
73 return (expsigny<0)? -pio2_hi-tiny: pio2_hi+tiny;
75 /* when x is INF */
76 if(exptx==BIAS+LDBL_MAX_EXP) {
77 if(expty==BIAS+LDBL_MAX_EXP) {
78 switch(m) {
79 case 0: return pio2_hi*0.5+tiny;/* atan(+INF,+INF) */
80 case 1: return -pio2_hi*0.5-tiny;/* atan(-INF,+INF) */
81 case 2: return 1.5*pio2_hi+tiny;/*atan(+INF,-INF)*/
82 case 3: return -1.5*pio2_hi-tiny;/*atan(-INF,-INF)*/
84 } else {
85 switch(m) {
86 case 0: return zero ; /* atan(+...,+INF) */
87 case 1: return -zero ; /* atan(-...,+INF) */
88 case 2: return pi+tiny ; /* atan(+...,-INF) */
89 case 3: return -pi-tiny ; /* atan(-...,-INF) */
93 /* when y is INF */
94 if(expty==BIAS+LDBL_MAX_EXP)
95 return (expsigny<0)? -pio2_hi-tiny: pio2_hi+tiny;
97 /* compute y/x */
98 k = expty-exptx;
99 if(k > LDBL_MANT_DIG+2) { /* |y/x| huge */
100 z=pio2_hi+pio2_lo;
101 m&=1;
103 else if(expsignx<0&&k<-LDBL_MANT_DIG-2) z=0.0; /* |y/x| tiny, x<0 */
104 else z=atanl(fabsl(y/x)); /* safe to do y/x */
105 switch (m) {
106 case 0: return z ; /* atan(+,+) */
107 case 1: return -z ; /* atan(-,+) */
108 case 2: return pi-(z-pi_lo);/* atan(+,-) */
109 default: /* case 3 */
110 return (z-pi_lo)-pi;/* atan(-,-) */