summaryrefslogtreecommitdiff
path: root/src/math/atan2l.c
blob: e0167d09eac0bc11d8848b4623b840e57ef0cad2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* origin: FreeBSD /usr/src/lib/msun/src/e_atan2l.c */
/*
 * ====================================================
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
 *
 * Developed at SunSoft, a Sun Microsystems, Inc. business.
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice
 * is preserved.
 * ====================================================
 *
 */
/*
 * See comments in atan2.c.
 * Converted to long double by David Schultz <das@FreeBSD.ORG>.
 */

#include "libm.h"

#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
long double atan2l(long double y, long double x)
{
	return atan2(y, x);
}
#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
#include "__invtrigl.h"

long double atan2l(long double y, long double x)
{
	union IEEEl2bits ux, uy;
	long double z;
	int m;
	uint16_t exptx, expsignx, expty, expsigny;

	if (isnan(x) || isnan(y))
		return x+y;
	if (x == 1)
		return atanl(y);
	uy.e = y;
	expsigny = uy.xbits.expsign;
	expty = expsigny & 0x7fff;
	ux.e = x;
	expsignx = ux.xbits.expsign;
	exptx = expsignx & 0x7fff;
	m = ((expsigny>>15)&1) | ((expsignx>>14)&2);  /* 2*sign(x)+sign(y) */

	if (y == 0) {
		switch(m) {
		case 0:
		case 1: return y;           /* atan(+-0,+anything)=+-0 */
		case 2: return  2*pio2_hi+0x1p-120f; /* atan(+0,-anything) = pi */
		case 3: return -2*pio2_hi-0x1p-120f; /* atan(-0,-anything) =-pi */
		}
	}
	if (x == 0)
		return m&1 ? -pio2_hi-0x1p-120f : pio2_hi+0x1p-120f;
	/* when x is INF */
	if (exptx == 0x7fff) {
		if (expty == 0x7fff) {
			switch(m) {
			case 0: return  pio2_hi*0.5+0x1p-120f; /* atan(+INF,+INF) */
			case 1: return -pio2_hi*0.5-0x1p-120f; /* atan(-INF,+INF) */
			case 2: return  1.5*pio2_hi+0x1p-120f; /* atan(+INF,-INF) */
			case 3: return -1.5*pio2_hi-0x1p-120f; /* atan(-INF,-INF) */
			}
		} else {
			switch(m) {
			case 0: return  0.0;        /* atan(+...,+INF) */
			case 1: return -0.0;        /* atan(-...,+INF) */
			case 2: return  2*pio2_hi+0x1p-120f; /* atan(+...,-INF) */
			case 3: return -2*pio2_hi-0x1p-120f; /* atan(-...,-INF) */
			}
		}
	}
	/* when y is INF */
	if (exptx+120 < expty || expty == 0x7fff)
		return m&1 ? -pio2_hi-0x1p-120f : pio2_hi+0x1p-120f;

	if ((m&2) && expty+120 < exptx) /* |y/x| tiny, x<0 */
		z = 0.0;
	else
		z = atanl(fabsl(y/x));
	switch (m) {
	case 0: return z;               /* atan(+,+) */
	case 1: return -z;              /* atan(-,+) */
	case 2: return 2*pio2_hi-(z-2*pio2_lo); /* atan(+,-) */
	default: /* case 3 */
		return (z-2*pio2_lo)-2*pio2_hi; /* atan(-,-) */
	}
}
#endif