blob: f0937a9794986068b712972342a28fb3500333fa [file] [log] [blame]
Rich Felkerb69f6952012-03-13 01:17:53 -04001/* origin: FreeBSD /usr/src/lib/msun/src/e_atan2l.c */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunSoft, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 *
12 */
13/*
14 * See comments in atan2.c.
15 * Converted to long double by David Schultz <das@FreeBSD.ORG>.
16 */
17
18#include "libm.h"
19
20#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
21long double atan2l(long double y, long double x)
22{
23 return atan2(y, x);
24}
25#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
26#include "__invtrigl.h"
Rich Felkerb69f6952012-03-13 01:17:53 -040027
28long double atan2l(long double y, long double x)
29{
Szabolcs Nagybcd797a2013-09-03 15:02:10 +000030 union ldshape ux, uy;
Rich Felkerb69f6952012-03-13 01:17:53 -040031 long double z;
Szabolcs Nagybcd797a2013-09-03 15:02:10 +000032 int m, ex, ey;
Rich Felkerb69f6952012-03-13 01:17:53 -040033
Szabolcs Nagy6d850962013-08-15 14:05:19 +000034 if (isnan(x) || isnan(y))
35 return x+y;
36 if (x == 1)
37 return atanl(y);
Szabolcs Nagybcd797a2013-09-03 15:02:10 +000038 ux.f = x;
39 uy.f = y;
40 ex = ux.i.se & 0x7fff;
41 ey = uy.i.se & 0x7fff;
42 m = 2*(ux.i.se>>15) | uy.i.se>>15;
Szabolcs Nagy6d850962013-08-15 14:05:19 +000043 if (y == 0) {
Rich Felkerb69f6952012-03-13 01:17:53 -040044 switch(m) {
45 case 0:
Szabolcs Nagy45ff9d82012-11-14 01:01:38 +010046 case 1: return y; /* atan(+-0,+anything)=+-0 */
Szabolcs Nagybcd797a2013-09-03 15:02:10 +000047 case 2: return 2*pio2_hi; /* atan(+0,-anything) = pi */
48 case 3: return -2*pio2_hi; /* atan(-0,-anything) =-pi */
Rich Felkerb69f6952012-03-13 01:17:53 -040049 }
50 }
Szabolcs Nagy6d850962013-08-15 14:05:19 +000051 if (x == 0)
Szabolcs Nagybcd797a2013-09-03 15:02:10 +000052 return m&1 ? -pio2_hi : pio2_hi;
53 if (ex == 0x7fff) {
54 if (ey == 0x7fff) {
Rich Felkerb69f6952012-03-13 01:17:53 -040055 switch(m) {
Szabolcs Nagybcd797a2013-09-03 15:02:10 +000056 case 0: return pio2_hi/2; /* atan(+INF,+INF) */
57 case 1: return -pio2_hi/2; /* atan(-INF,+INF) */
58 case 2: return 1.5*pio2_hi; /* atan(+INF,-INF) */
59 case 3: return -1.5*pio2_hi; /* atan(-INF,-INF) */
Rich Felkerb69f6952012-03-13 01:17:53 -040060 }
61 } else {
62 switch(m) {
Szabolcs Nagy45ff9d82012-11-14 01:01:38 +010063 case 0: return 0.0; /* atan(+...,+INF) */
64 case 1: return -0.0; /* atan(-...,+INF) */
Szabolcs Nagybcd797a2013-09-03 15:02:10 +000065 case 2: return 2*pio2_hi; /* atan(+...,-INF) */
66 case 3: return -2*pio2_hi; /* atan(-...,-INF) */
Rich Felkerb69f6952012-03-13 01:17:53 -040067 }
68 }
69 }
Szabolcs Nagybcd797a2013-09-03 15:02:10 +000070 if (ex+120 < ey || ey == 0x7fff)
71 return m&1 ? -pio2_hi : pio2_hi;
72 /* z = atan(|y/x|) without spurious underflow */
73 if ((m&2) && ey+120 < ex) /* |y/x| < 0x1p-120, x<0 */
Rich Felkerb69f6952012-03-13 01:17:53 -040074 z = 0.0;
Szabolcs Nagy6d850962013-08-15 14:05:19 +000075 else
Rich Felkerb69f6952012-03-13 01:17:53 -040076 z = atanl(fabsl(y/x));
77 switch (m) {
Szabolcs Nagy45ff9d82012-11-14 01:01:38 +010078 case 0: return z; /* atan(+,+) */
79 case 1: return -z; /* atan(-,+) */
Szabolcs Nagyb12a73d2012-12-11 23:56:59 +010080 case 2: return 2*pio2_hi-(z-2*pio2_lo); /* atan(+,-) */
Rich Felkerb69f6952012-03-13 01:17:53 -040081 default: /* case 3 */
Szabolcs Nagyb12a73d2012-12-11 23:56:59 +010082 return (z-2*pio2_lo)-2*pio2_hi; /* atan(-,-) */
Rich Felkerb69f6952012-03-13 01:17:53 -040083 }
84}
85#endif