ipiq: Add simple IPI latency measure sysctls (2)
[dragonfly.git] / lib / libm / src / e_atan2l.c
CommitLineData
1a3b704c
JM
1
2/* @(#)e_atan2.c 1.3 95/01/18 */
6ff43c94 3/* $FreeBSD: head/lib/msun/src/e_atan2l.c 181204 2008-08-02 19:17:00Z das $ */
1a3b704c
JM
4/*
5 * ====================================================
6 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7 *
8 * Developed at SunSoft, a Sun Microsystems, Inc. business.
9 * Permission to use, copy, modify, and distribute this
6ff43c94 10 * software is freely granted, provided that this notice
1a3b704c
JM
11 * is preserved.
12 * ====================================================
13 *
14 */
15
16/*
17 * See comments in e_atan2.c.
18 * Converted to long double by David Schultz <das@FreeBSD.ORG>.
19 */
20
21#include <float.h>
22
23#include "invtrig.h"
24#include "math.h"
25#include "math_private.h"
26
27static volatile long double
28tiny = 1.0e-300;
29static const long double
30zero = 0.0;
31
32#ifdef __i386__
33/* XXX Work around the fact that gcc truncates long double constants on i386 */
34static volatile double
35pi1 = 3.14159265358979311600e+00, /* 0x1.921fb54442d18p+1 */
36pi2 = 1.22514845490862001043e-16; /* 0x1.1a80000000000p-53 */
37#define pi ((long double)pi1 + pi2)
38#else
39static const long double
40pi = 3.14159265358979323846264338327950280e+00L;
41#endif
42
43long double
44atan2l(long double y, long double x)
45{
46 union IEEEl2bits ux, uy;
47 long double z;
48 int32_t k,m;
49 int16_t exptx, expsignx, expty, expsigny;
50
51 uy.e = y;
52 expsigny = uy.xbits.expsign;
53 expty = expsigny & 0x7fff;
54 ux.e = x;
55 expsignx = ux.xbits.expsign;
56 exptx = expsignx & 0x7fff;
57
58 if ((exptx==BIAS+LDBL_MAX_EXP &&
59 ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)!=0) || /* x is NaN */
60 (expty==BIAS+LDBL_MAX_EXP &&
61 ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)!=0)) /* y is NaN */
62 return x+y;
63 if (expsignx==BIAS && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
64 return atanl(y); /* x=1.0 */
65 m = ((expsigny>>15)&1)|((expsignx>>14)&2); /* 2*sign(x)+sign(y) */
66
67 /* when y = 0 */
68 if(expty==0 && ((uy.bits.manh&~LDBL_NBIT)|uy.bits.manl)==0) {
69 switch(m) {
6ff43c94 70 case 0:
1a3b704c
JM
71 case 1: return y; /* atan(+-0,+anything)=+-0 */
72 case 2: return pi+tiny;/* atan(+0,-anything) = pi */
73 case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */
74 }
75 }
76 /* when x = 0 */
77 if(exptx==0 && ((ux.bits.manh&~LDBL_NBIT)|ux.bits.manl)==0)
78 return (expsigny<0)? -pio2_hi-tiny: pio2_hi+tiny;
79
80 /* when x is INF */
81 if(exptx==BIAS+LDBL_MAX_EXP) {
82 if(expty==BIAS+LDBL_MAX_EXP) {
83 switch(m) {
84 case 0: return pio2_hi*0.5+tiny;/* atan(+INF,+INF) */
85 case 1: return -pio2_hi*0.5-tiny;/* atan(-INF,+INF) */
86 case 2: return 1.5*pio2_hi+tiny;/*atan(+INF,-INF)*/
87 case 3: return -1.5*pio2_hi-tiny;/*atan(-INF,-INF)*/
88 }
89 } else {
90 switch(m) {
91 case 0: return zero ; /* atan(+...,+INF) */
92 case 1: return -zero ; /* atan(-...,+INF) */
93 case 2: return pi+tiny ; /* atan(+...,-INF) */
94 case 3: return -pi-tiny ; /* atan(-...,-INF) */
95 }
96 }
97 }
98 /* when y is INF */
99 if(expty==BIAS+LDBL_MAX_EXP)
100 return (expsigny<0)? -pio2_hi-tiny: pio2_hi+tiny;
101
102 /* compute y/x */
103 k = expty-exptx;
104 if(k > LDBL_MANT_DIG+2) { /* |y/x| huge */
105 z=pio2_hi+pio2_lo;
106 m&=1;
107 }
108 else if(expsignx<0&&k<-LDBL_MANT_DIG-2) z=0.0; /* |y/x| tiny, x<0 */
109 else z=atanl(fabsl(y/x)); /* safe to do y/x */
110 switch (m) {
111 case 0: return z ; /* atan(+,+) */
112 case 1: return -z ; /* atan(-,+) */
113 case 2: return pi-(z-pi_lo);/* atan(+,-) */
114 default: /* case 3 */
115 return (z-pi_lo)-pi;/* atan(-,-) */
116 }
117}