CMS 3D CMS Logo

approx_atan2.h
Go to the documentation of this file.
1 #ifndef DataFormatsMathAPPROX_ATAN2_H
2 #define DataFormatsMathAPPROX_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<> constexpr 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 constexpr float approx_atan2f_P(float x);
44 
45 // degree = 3 => absolute accuracy is 7 bits
46 template<> constexpr 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<> constexpr 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<> constexpr 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<> constexpr 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<> constexpr 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<> constexpr 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<> constexpr 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 constexpr 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 constexpr float unsafe_atan2f(float y, float x) {
106  return unsafe_atan2f_impl<DEGREE>(y,x);
107 
108 }
109 
110 
111 template<int DEGREE>
112 constexpr 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 constexpr float approx_atan2i_P(float x);
128 
129 // degree = 3 => absolute accuracy is 6*10^6
130 template<> constexpr 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<> constexpr 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<> constexpr 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<> constexpr 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<> constexpr 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<> constexpr 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<> constexpr 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 constexpr 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 constexpr int unsafe_atan2i(float y, float x) {
194  return unsafe_atan2i_impl<DEGREE>(y,x);
195 
196 }
197 
198 
199 // short (16bits)
200 
201 template<int DEGREE>
202 constexpr float approx_atan2s_P(float x);
203 
204 
205  // degree = 3 => absolute accuracy is 53
206 template<> constexpr float approx_atan2s_P< 3 >(float x){
207  auto z = x*x;
208  return x * ((-10142.439453125f) + z * 2002.0908203125f);
209 }
210 // degree = 5 => absolute accuracy is 7
211 template<> constexpr float approx_atan2s_P< 5 >(float x){
212  auto z = x*x;
213  return x * ((-10381.9609375f) + z * ((3011.1513671875f) + z * (-827.538330078125f)));
214 }
215 // degree = 7 => absolute accuracy is 2
216 template<> constexpr float approx_atan2s_P< 7 >(float x){
217  auto z = x*x;
218  return x * ((-10422.177734375f) + z * (3349.97412109375f + z * ((-1525.589599609375f) + z * 406.64190673828125f))) ;
219 }
220 // degree = 9 => absolute accuracy is 1
221 template<> constexpr float approx_atan2s_P< 9 >(float x){
222  auto z = x*x;
223  return x * ((-10428.984375f) + z * (3445.20654296875f + z * ((-1879.137939453125f) + z * (888.22314453125f + z * (-217.42669677734375f)))));
224 }
225 
226 
227 template<int DEGREE>
228 constexpr short unsafe_atan2s_impl(float y, float x) {
229 
230 
231  constexpr int maxshort = (int)(std::numeric_limits<short>::max())+1;
232  constexpr short pi4 = short(maxshort/4);
233  constexpr short pi34 = short(3*maxshort/4);
234 
235  auto r= (std::abs(x) - std::abs(y))/(std::abs(x) + std::abs(y));
236  if (x<0) r = -r;
237 
238  auto angle = (x>=0) ? pi4 : pi34;
239  angle += short(approx_atan2s_P<DEGREE>(r));
240 
241 
242  return (y < 0) ? - angle : angle ;
243 
244 }
245 
246 template<int DEGREE>
247 constexpr short unsafe_atan2s(float y, float x) {
248  return unsafe_atan2s_impl<DEGREE>(y,x);
249 }
250 
251 
252 
253 
254 
255 
256 constexpr
257 int phi2int(float x) {
258  constexpr float p2i = ( (long long)(std::numeric_limits<int>::max())+1LL )/M_PI;
259  return std::round(x*p2i);
260 }
261 
262 constexpr
263 float int2phi(int x) {
264  constexpr float i2p = M_PI/( (long long)(std::numeric_limits<int>::max())+1LL );
265  return float(x)*i2p;
266 }
267 
268 constexpr
269 double int2dphi(int x) {
270  constexpr double i2p = M_PI/( (long long)(std::numeric_limits<int>::max())+1LL );
271  return x*i2p;
272 }
273 
274 
275 constexpr
276 short phi2short(float x) {
277  constexpr float p2i = ( (int)(std::numeric_limits<short>::max())+1 )/M_PI;
278  return std::round(x*p2i);
279 }
280 
281 constexpr
282 float short2phi(short x) {
283  constexpr float i2p = M_PI/( (int)(std::numeric_limits<short>::max())+1 );
284  return float(x)*i2p;
285 }
286 
287 
288 #endif
289 
290 
constexpr float approx_atan2f_P< 13 >(float x)
Definition: approx_atan2.h:75
constexpr short phi2short(float x)
Definition: approx_atan2.h:276
constexpr int unsafe_atan2i(float y, float x)
Definition: approx_atan2.h:193
constexpr float approx_atan2s_P< 3 >(float x)
Definition: approx_atan2.h:206
constexpr float approx_atan2i_P< 9 >(float x)
Definition: approx_atan2.h:148
constexpr float approx_atan2f_P< 15 >(float x)
Definition: approx_atan2.h:81
constexpr float approx_atan2i_P< 3 >(float x)
Definition: approx_atan2.h:130
constexpr float approx_atan2s_P< 7 >(float x)
Definition: approx_atan2.h:216
float float float z
constexpr float safe_atan2f(float y, float x)
Definition: approx_atan2.h:112
constexpr float approx_atan2f_P< 7 >(float x)
Definition: approx_atan2.h:57
constexpr float unsafe_atan2f(float y, float x)
Definition: approx_atan2.h:105
constexpr double int2dphi(int x)
Definition: approx_atan2.h:269
constexpr short unsafe_atan2s_impl(float y, float x)
Definition: approx_atan2.h:228
constexpr float approx_atan2s_P(float x)
constexpr float approx_atan2f_P< 5 >(float x)
Definition: approx_atan2.h:51
constexpr float approx_atan2f_P(float x)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
constexpr float approx_atan2i_P< 15 >(float x)
Definition: approx_atan2.h:165
double f[11][100]
constexpr float approx_atan2s_P< 9 >(float x)
Definition: approx_atan2.h:221
constexpr float int2phi(int x)
Definition: approx_atan2.h:263
#define M_PI
constexpr float approx_atan2i_P(float x)
constexpr float approx_atan2i_P< 7 >(float x)
Definition: approx_atan2.h:142
constexpr short unsafe_atan2s(float y, float x)
Definition: approx_atan2.h:247
constexpr float approx_atan2i_P< 5 >(float x)
Definition: approx_atan2.h:136
constexpr float short2phi(short x)
Definition: approx_atan2.h:282
constexpr float approx_atan2f_P< 11 >(float x)
Definition: approx_atan2.h:69
constexpr float approx_atan2s_P< 5 >(float x)
Definition: approx_atan2.h:211
constexpr float approx_atan2i_P< 11 >(float x)
Definition: approx_atan2.h:154
constexpr float unsafe_atan2f_impl(float y, float x)
Definition: approx_atan2.h:88
constexpr float approx_atan2f_P< 9 >(float x)
Definition: approx_atan2.h:63
constexpr int phi2int(float x)
Definition: approx_atan2.h:257
constexpr float approx_atan2i_P< 13 >(float x)
Definition: approx_atan2.h:160
constexpr int unsafe_atan2i_impl(float y, float x)
Definition: approx_atan2.h:172
constexpr float approx_atan2f_P< 3 >(float x)
Definition: approx_atan2.h:46
#define constexpr
T angle(T x1, T y1, T z1, T x2, T y2, T z2)
Definition: angle.h:11