Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 1 | /* 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 |
| 21 | long 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 Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 27 | |
| 28 | long double atan2l(long double y, long double x) |
| 29 | { |
Szabolcs Nagy | bcd797a | 2013-09-03 15:02:10 +0000 | [diff] [blame] | 30 | union ldshape ux, uy; |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 31 | long double z; |
Szabolcs Nagy | bcd797a | 2013-09-03 15:02:10 +0000 | [diff] [blame] | 32 | int m, ex, ey; |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 33 | |
Szabolcs Nagy | 6d85096 | 2013-08-15 14:05:19 +0000 | [diff] [blame] | 34 | if (isnan(x) || isnan(y)) |
| 35 | return x+y; |
| 36 | if (x == 1) |
| 37 | return atanl(y); |
Szabolcs Nagy | bcd797a | 2013-09-03 15:02:10 +0000 | [diff] [blame] | 38 | 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 Nagy | 6d85096 | 2013-08-15 14:05:19 +0000 | [diff] [blame] | 43 | if (y == 0) { |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 44 | switch(m) { |
| 45 | case 0: |
Szabolcs Nagy | 45ff9d8 | 2012-11-14 01:01:38 +0100 | [diff] [blame] | 46 | case 1: return y; /* atan(+-0,+anything)=+-0 */ |
Szabolcs Nagy | bcd797a | 2013-09-03 15:02:10 +0000 | [diff] [blame] | 47 | case 2: return 2*pio2_hi; /* atan(+0,-anything) = pi */ |
| 48 | case 3: return -2*pio2_hi; /* atan(-0,-anything) =-pi */ |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 49 | } |
| 50 | } |
Szabolcs Nagy | 6d85096 | 2013-08-15 14:05:19 +0000 | [diff] [blame] | 51 | if (x == 0) |
Szabolcs Nagy | bcd797a | 2013-09-03 15:02:10 +0000 | [diff] [blame] | 52 | return m&1 ? -pio2_hi : pio2_hi; |
| 53 | if (ex == 0x7fff) { |
| 54 | if (ey == 0x7fff) { |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 55 | switch(m) { |
Szabolcs Nagy | bcd797a | 2013-09-03 15:02:10 +0000 | [diff] [blame] | 56 | 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 Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 60 | } |
| 61 | } else { |
| 62 | switch(m) { |
Szabolcs Nagy | 45ff9d8 | 2012-11-14 01:01:38 +0100 | [diff] [blame] | 63 | case 0: return 0.0; /* atan(+...,+INF) */ |
| 64 | case 1: return -0.0; /* atan(-...,+INF) */ |
Szabolcs Nagy | bcd797a | 2013-09-03 15:02:10 +0000 | [diff] [blame] | 65 | case 2: return 2*pio2_hi; /* atan(+...,-INF) */ |
| 66 | case 3: return -2*pio2_hi; /* atan(-...,-INF) */ |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
Szabolcs Nagy | bcd797a | 2013-09-03 15:02:10 +0000 | [diff] [blame] | 70 | 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 Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 74 | z = 0.0; |
Szabolcs Nagy | 6d85096 | 2013-08-15 14:05:19 +0000 | [diff] [blame] | 75 | else |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 76 | z = atanl(fabsl(y/x)); |
| 77 | switch (m) { |
Szabolcs Nagy | 45ff9d8 | 2012-11-14 01:01:38 +0100 | [diff] [blame] | 78 | case 0: return z; /* atan(+,+) */ |
| 79 | case 1: return -z; /* atan(-,+) */ |
Szabolcs Nagy | b12a73d | 2012-12-11 23:56:59 +0100 | [diff] [blame] | 80 | case 2: return 2*pio2_hi-(z-2*pio2_lo); /* atan(+,-) */ |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 81 | default: /* case 3 */ |
Szabolcs Nagy | b12a73d | 2012-12-11 23:56:59 +0100 | [diff] [blame] | 82 | return (z-2*pio2_lo)-2*pio2_hi; /* atan(-,-) */ |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | #endif |