CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
approx_atan2.h
Go to the documentation of this file.
1 #ifndef APPROX_ATAN2_H
2 #define APPROX_ATAN2_H
3 
4 
5 /*
6  * approximate atan2 evaluations
7  *
8  * Polynomials were obtained using Sollya scripts (in comments below)
9  *
10  *
11 */
12 
13 /*
14 f= atan((1-x)/(1+x))-atan(1);
15 I=[-1+10^(-4);1.0];
16 filename="atan.txt";
17 print("") > filename;
18 for deg from 3 to 11 do begin
19  p = fpminimax(f, deg,[|1,23...|],I, floating, absolute);
20  display=decimal;
21  acc=floor(-log2(sup(supnorm(p, f, I, absolute, 2^(-20)))));
22  print( " // degree = ", deg,
23  " => absolute accuracy is ", acc, "bits" ) >> filename;
24  print("template<> inline float approx_atan2f_P<", deg, ">(float x){") >> filename;
25  display=hexadecimal;
26  print(" return ", horner(p) , ";") >> filename;
27  print("}") >> filename;
28 end;
29 */
30 
31 
32 
33 
34 #include <cstdint>
35 #include <cmath>
36 #include <limits>
37 #include <algorithm>
38 
39 
40 // float
41 
42 template<int DEGREE>
43 inline float approx_atan2f_P(float x);
44 
45 // degree = 3 => absolute accuracy is 7 bits
46 template<> inline float approx_atan2f_P< 3 >(float x){
47  return x * (float(-0xf.8eed2p-4) + x*x * float(0x3.1238p-4)) ;
48 }
49 
50 // degree = 5 => absolute accuracy is 10 bits
51 template<> inline float approx_atan2f_P< 5 >(float x){
52  auto z = x*x;
53  return x * (float(-0xf.ecfc8p-4) + z * (float(0x4.9e79dp-4) + z * float(-0x1.44f924p-4) ) );
54 }
55 
56 // degree = 7 => absolute accuracy is 13 bits
57 template<> inline float approx_atan2f_P< 7 >(float x){
58  auto z = x*x;
59  return x * (float(-0xf.fcc7ap-4) + z * (float(0x5.23886p-4) + z * (float(-0x2.571968p-4) + z * float(0x9.fb05p-8) ) ) ) ;
60 }
61 
62 // degree = 9 => absolute accuracy is 16 bits
63 template<> inline float approx_atan2f_P< 9 >(float x){
64  auto z = x*x;
65  return x * (float(-0xf.ff73ep-4) + z * (float(0x5.48ee1p-4) + z * (float(-0x2.e1efe8p-4) + z * (float(0x1.5cce54p-4) + z * float(-0x5.56245p-8) ) ) ) );
66 }
67 
68 // degree = 11 => absolute accuracy is 19 bits
69 template<> inline float approx_atan2f_P< 11 >(float x){
70  auto z = x*x;
71  return x * (float(-0xf.ffe82p-4) + z * (float(0x5.526c8p-4) + z * (float(-0x3.18bea8p-4) + z * (float(0x1.dce3bcp-4) + z * (float(-0xd.7a64ap-8) + z * float(0x3.000eap-8))))));
72 }
73 
74 // degree = 13 => absolute accuracy is 21 bits
75 template<> inline float approx_atan2f_P< 13 >(float x){
76  auto z = x*x;
77  return x * (float(-0xf.fffbep-4) + z * (float(0x5.54adp-4) + z * (float(-0x3.2b4df8p-4) + z * (float(0x2.1df79p-4) + z * (float(-0x1.46081p-4) + z * (float(0x8.99028p-8) + z * float(-0x1.be0bc4p-8))))))) ;
78 }
79 
80 // degree = 15 => absolute accuracy is 24 bits
81 template<> inline float approx_atan2f_P< 15 >(float x){
82  auto z = x*x;
83  return x * (float(-0xf.ffff4p-4) + z * (float(0x5.552f9p-4 + z * (float(-0x3.30f728p-4) + z * (float(0x2.39826p-4) + z * (float(-0x1.8a880cp-4) + z * (float(0xe.484d6p-8) + z * (float(-0x5.93d5p-8) + z * float(0x1.0875dcp-8)))))))));
84 }
85 
86 
87 template<int DEGREE>
88 inline float unsafe_atan2f_impl(float y, float x) {
89 
90  constexpr float pi4f = 3.1415926535897932384626434/4;
91  constexpr float pi34f = 3.1415926535897932384626434*3/4;
92 
93  auto r= (std::abs(x) - std::abs(y))/(std::abs(x) + std::abs(y));
94  if (x<0) r = -r;
95 
96  auto angle = (x>=0) ? pi4f : pi34f;
97  angle += approx_atan2f_P<DEGREE>(r);
98 
99 
100  return ( (y < 0)) ? - angle : angle ;
101 
102 }
103 
104 template<int DEGREE>
105 inline float unsafe_atan2f(float y, float x) {
106  return unsafe_atan2f_impl<DEGREE>(y,x);
107 
108 }
109 
110 
111 template<int DEGREE>
112 inline float safe_atan2f(float y, float x) {
113  return unsafe_atan2f_impl<DEGREE>( y, (y==0.f)&(x==0.f) ? 0.2f : x);
114  // return (y==0.f)&(x==0.f) ? 0.f : unsafe_atan2f_impl<DEGREE>( y, x);
115 }
116 
117 
118 // integer...
119 /*
120  f= (2^31/pi)*(atan((1-x)/(1+x))-atan(1));
121  I=[-1+10^(-4);1.0];
122  p = fpminimax(f, [|1,3,5,7,9,11|],[|23...|],I, floating, absolute);
123  */
124 
125 
126 template<int DEGREE>
127 inline float approx_atan2i_P(float x);
128 
129 // degree = 3 => absolute accuracy is 6*10^6
130 template<> inline float approx_atan2i_P< 3 >(float x){
131  auto z = x*x;
132  return x * (-664694912.f + z * 131209024.f);
133 }
134 
135 // degree = 5 => absolute accuracy is 4*10^5
136 template<> inline float approx_atan2i_P< 5 >(float x){
137  auto z = x*x;
138  return x * (-680392064.f + z * (197338400.f + z * (-54233256.f)));
139 }
140 
141 // degree = 7 => absolute accuracy is 6*10^4
142 template<> inline float approx_atan2i_P< 7 >(float x){
143  auto z = x*x;
144  return x * (-683027840.f + z * (219543904.f + z * (-99981040.f + z * 26649684.f)));
145 }
146 
147 // degree = 9 => absolute accuracy is 8000
148 template<> inline float approx_atan2i_P< 9 >(float x){
149  auto z = x*x;
150  return x * (-683473920.f + z * (225785056.f + z * (-123151184.f + z * (58210592.f + z * (-14249276.f)))));
151 }
152 
153 // degree = 11 => absolute accuracy is 1000
154 template<> inline float approx_atan2i_P< 11 >(float x){
155  auto z = x*x;
156  return x * (-683549696.f + z * (227369312.f + z * (-132297008.f + z * (79584144.f + z * (-35987016.f + z * 8010488.f)))));
157 }
158 
159 // degree = 13 => absolute accuracy is 163
160 template<> inline float approx_atan2i_P< 13 >(float x){
161  auto z = x*x;
162  return x * (-683562624.f + z * (227746080.f + z * (-135400128.f + z * (90460848.f + z * (-54431464.f + z * (22973256.f + z * (-4657049.f)))))));
163 }
164 
165 template<> inline float approx_atan2i_P< 15 >(float x){
166  auto z = x*x;
167  return x * (-683562624.f + z * (227746080.f + z * (-135400128.f + z * (90460848.f + z * (-54431464.f + z * (22973256.f + z * (-4657049.f)))))));
168 }
169 
170 
171 template<int DEGREE>
172 inline int unsafe_atan2i_impl(float y, float x) {
173 
174 
175  constexpr long long maxint = (long long)(std::numeric_limits<int>::max())+1LL;
176  constexpr int pi4 = int(maxint/4LL);
177  constexpr int pi34 = int(3LL*maxint/4LL);
178 
179  auto r= (std::abs(x) - std::abs(y))/(std::abs(x) + std::abs(y));
180  if (x<0) r = -r;
181 
182  auto angle = (x>=0) ? pi4 : pi34;
183  angle += int(approx_atan2i_P<DEGREE>(r));
184  // angle += int(std::round(approx_atan2i_P<DEGREE>(r)));
185 
186 
187  return (y < 0) ? - angle : angle ;
188 
189 
190 }
191 
192 template<int DEGREE>
193 inline int unsafe_atan2i(float y, float x) {
194  return unsafe_atan2i_impl<DEGREE>(y,x);
195 
196 }
197 
198 inline
199 int phi2int(float x) {
200  constexpr float p2i = ( (long long)(std::numeric_limits<int>::max())+1LL )/M_PI;
201  return std::round(x*p2i);
202 }
203 
204 inline
205 float int2phi(int x) {
206  constexpr float i2p = M_PI/( (long long)(std::numeric_limits<int>::max())+1LL );
207  return float(x)*i2p;
208 }
209 
210 inline
211 double int2dphi(int x) {
212  constexpr double i2p = M_PI/( (long long)(std::numeric_limits<int>::max())+1LL );
213  return x*i2p;
214 }
215 
216 
217 inline
218 short phi2short(float x) {
219  constexpr float p2i = ( (int)(std::numeric_limits<short>::max())+1 )/M_PI;
220  return std::round(x*p2i);
221 }
222 
223 inline
224 float short2phi(short x) {
225  constexpr float i2p = M_PI/( (int)(std::numeric_limits<short>::max())+1 );
226  return float(x)*i2p;
227 }
228 
229 
230 #endif
231 
232 
float approx_atan2i_P< 15 >(float x)
Definition: approx_atan2.h:165
float approx_atan2i_P< 11 >(float x)
Definition: approx_atan2.h:154
float approx_atan2i_P< 3 >(float x)
Definition: approx_atan2.h:130
float unsafe_atan2f(float y, float x)
Definition: approx_atan2.h:105
float approx_atan2f_P(float x)
float approx_atan2i_P< 5 >(float x)
Definition: approx_atan2.h:136
#define constexpr
float float float z
int phi2int(float x)
Definition: approx_atan2.h:199
double int2dphi(int x)
Definition: approx_atan2.h:211
int unsafe_atan2i(float y, float x)
Definition: approx_atan2.h:193
T x() const
Cartesian x coordinate.
float approx_atan2f_P< 9 >(float x)
Definition: approx_atan2.h:63
float approx_atan2f_P< 7 >(float x)
Definition: approx_atan2.h:57
float int2phi(int x)
Definition: approx_atan2.h:205
float approx_atan2i_P< 7 >(float x)
Definition: approx_atan2.h:142
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
float approx_atan2f_P< 15 >(float x)
Definition: approx_atan2.h:81
float approx_atan2f_P< 11 >(float x)
Definition: approx_atan2.h:69
short phi2short(float x)
Definition: approx_atan2.h:218
#define M_PI
float approx_atan2i_P< 9 >(float x)
Definition: approx_atan2.h:148
float approx_atan2i_P< 13 >(float x)
Definition: approx_atan2.h:160
float unsafe_atan2f_impl(float y, float x)
Definition: approx_atan2.h:88
float approx_atan2f_P< 5 >(float x)
Definition: approx_atan2.h:51
int unsafe_atan2i_impl(float y, float x)
Definition: approx_atan2.h:172
float approx_atan2i_P(float x)
float short2phi(short x)
Definition: approx_atan2.h:224
float approx_atan2f_P< 13 >(float x)
Definition: approx_atan2.h:75
float approx_atan2f_P< 3 >(float x)
Definition: approx_atan2.h:46
float safe_atan2f(float y, float x)
Definition: approx_atan2.h:112
T angle(T x1, T y1, T z1, T x2, T y2, T z2)
Definition: angle.h:11