The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* e_atan2f.c -- float version of e_atan2.c. |
| 2 | * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * ==================================================== |
| 7 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
| 8 | * |
| 9 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
| 10 | * Permission to use, copy, modify, and distribute this |
| 11 | * software is freely granted, provided that this notice |
| 12 | * is preserved. |
| 13 | * ==================================================== |
| 14 | */ |
| 15 | |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 16 | #include <sys/cdefs.h> |
Elliott Hughes | bac0ebb | 2021-01-26 14:17:20 -0800 | [diff] [blame^] | 17 | __FBSDID("$FreeBSD$"); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 18 | |
| 19 | #include "math.h" |
| 20 | #include "math_private.h" |
| 21 | |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 22 | static volatile float |
| 23 | tiny = 1.0e-30; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 24 | static const float |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 25 | zero = 0.0, |
| 26 | pi_o_4 = 7.8539818525e-01, /* 0x3f490fdb */ |
| 27 | pi_o_2 = 1.5707963705e+00, /* 0x3fc90fdb */ |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 28 | pi = 3.1415927410e+00; /* 0x40490fdb */ |
| 29 | static volatile float |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 30 | pi_lo = -8.7422776573e-08; /* 0xb3bbbd2e */ |
| 31 | |
| 32 | float |
| 33 | __ieee754_atan2f(float y, float x) |
| 34 | { |
| 35 | float z; |
| 36 | int32_t k,m,hx,hy,ix,iy; |
| 37 | |
| 38 | GET_FLOAT_WORD(hx,x); |
| 39 | ix = hx&0x7fffffff; |
| 40 | GET_FLOAT_WORD(hy,y); |
| 41 | iy = hy&0x7fffffff; |
| 42 | if((ix>0x7f800000)|| |
| 43 | (iy>0x7f800000)) /* x or y is NaN */ |
Elliott Hughes | ab52807 | 2018-07-24 00:01:52 +0000 | [diff] [blame] | 44 | return nan_mix(x, y); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 45 | if(hx==0x3f800000) return atanf(y); /* x=1.0 */ |
| 46 | m = ((hy>>31)&1)|((hx>>30)&2); /* 2*sign(x)+sign(y) */ |
| 47 | |
| 48 | /* when y = 0 */ |
| 49 | if(iy==0) { |
| 50 | switch(m) { |
| 51 | case 0: |
| 52 | case 1: return y; /* atan(+-0,+anything)=+-0 */ |
| 53 | case 2: return pi+tiny;/* atan(+0,-anything) = pi */ |
| 54 | case 3: return -pi-tiny;/* atan(-0,-anything) =-pi */ |
| 55 | } |
| 56 | } |
| 57 | /* when x = 0 */ |
| 58 | if(ix==0) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; |
| 59 | |
| 60 | /* when x is INF */ |
| 61 | if(ix==0x7f800000) { |
| 62 | if(iy==0x7f800000) { |
| 63 | switch(m) { |
| 64 | case 0: return pi_o_4+tiny;/* atan(+INF,+INF) */ |
| 65 | case 1: return -pi_o_4-tiny;/* atan(-INF,+INF) */ |
| 66 | case 2: return (float)3.0*pi_o_4+tiny;/*atan(+INF,-INF)*/ |
| 67 | case 3: return (float)-3.0*pi_o_4-tiny;/*atan(-INF,-INF)*/ |
| 68 | } |
| 69 | } else { |
| 70 | switch(m) { |
| 71 | case 0: return zero ; /* atan(+...,+INF) */ |
| 72 | case 1: return -zero ; /* atan(-...,+INF) */ |
| 73 | case 2: return pi+tiny ; /* atan(+...,-INF) */ |
| 74 | case 3: return -pi-tiny ; /* atan(-...,-INF) */ |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | /* when y is INF */ |
| 79 | if(iy==0x7f800000) return (hy<0)? -pi_o_2-tiny: pi_o_2+tiny; |
| 80 | |
| 81 | /* compute y/x */ |
| 82 | k = (iy-ix)>>23; |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 83 | if(k > 26) { /* |y/x| > 2**26 */ |
| 84 | z=pi_o_2+(float)0.5*pi_lo; |
| 85 | m&=1; |
| 86 | } |
| 87 | else if(k<-26&&hx<0) z=0.0; /* 0 > |y|/x > -2**-26 */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 88 | else z=atanf(fabsf(y/x)); /* safe to do y/x */ |
| 89 | switch (m) { |
| 90 | case 0: return z ; /* atan(+,+) */ |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 91 | case 1: return -z ; /* atan(-,+) */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 92 | case 2: return pi-(z-pi_lo);/* atan(+,-) */ |
| 93 | default: /* case 3 */ |
| 94 | return (z-pi_lo)-pi;/* atan(-,-) */ |
| 95 | } |
| 96 | } |