1 |
2 |
feddischso |
/***************************************************************************
|
2 |
|
|
* *
|
3 |
|
|
* File : cordic_iterative.c *
|
4 |
|
|
* Project : YAC (Yet Another CORDIC Core) *
|
5 |
|
|
* Creation : Feb. 2014 *
|
6 |
|
|
* Limitations : *
|
7 |
|
|
* Platform : Linux, Mac, Windows *
|
8 |
|
|
* Target : Octave, Matlab, Standalone-Application *
|
9 |
|
|
* *
|
10 |
|
|
* Author(s): : Christian Haettich *
|
11 |
|
|
* Email : feddischson@opencores.org *
|
12 |
|
|
* *
|
13 |
|
|
* *
|
14 |
|
|
** **
|
15 |
|
|
* *
|
16 |
|
|
* Description *
|
17 |
|
|
* C-implementation of an interative cordic. *
|
18 |
|
|
* General information about the CORDIC algorithm can be found *
|
19 |
|
|
* here: - http://en.wikipedia.org/wiki/CORDIC *
|
20 |
|
|
* - http://en.wikibooks.org/wiki/Digital_Circuits/CORDIC *
|
21 |
|
|
* *
|
22 |
|
|
* *
|
23 |
|
|
** **
|
24 |
|
|
* *
|
25 |
|
|
* TODO *
|
26 |
|
|
* Some documentation and function description *
|
27 |
|
|
* *
|
28 |
|
|
* *
|
29 |
|
|
* *
|
30 |
|
|
* *
|
31 |
|
|
****************************************************************************
|
32 |
|
|
* *
|
33 |
|
|
* Copyright Notice *
|
34 |
|
|
* *
|
35 |
|
|
* This file is part of YAC - Yet Another CORDIC Core *
|
36 |
|
|
* Copyright (c) 2014, Author(s), All rights reserved. *
|
37 |
|
|
* *
|
38 |
|
|
* YAC is free software; you can redistribute it and/or *
|
39 |
|
|
* modify it under the terms of the GNU Lesser General Public *
|
40 |
|
|
* License as published by the Free Software Foundation; either *
|
41 |
|
|
* version 3.0 of the License, or (at your option) any later version. *
|
42 |
|
|
* *
|
43 |
|
|
* YAC is distributed in the hope that it will be useful, *
|
44 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
45 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
|
46 |
|
|
* Lesser General Public License for more details. *
|
47 |
|
|
* *
|
48 |
|
|
* You should have received a copy of the GNU Lesser General Public *
|
49 |
|
|
* License along with this library. If not, download it from *
|
50 |
|
|
* http://www.gnu.org/licenses/lgpl *
|
51 |
|
|
* *
|
52 |
|
|
***************************************************************************/
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
#include <stdio.h>
|
57 |
|
|
#include <stdlib.h>
|
58 |
|
|
#include <math.h>
|
59 |
|
|
|
60 |
7 |
feddischso |
#ifndef OR32_TARGET
|
61 |
|
|
#include "mex.h"
|
62 |
|
|
#endif
|
63 |
|
|
|
64 |
2 |
feddischso |
/* enable debug output */
|
65 |
|
|
#define PRINT_DEBUG 0
|
66 |
|
|
|
67 |
|
|
/* #define CORDIC_ROUNDING 0.5 */
|
68 |
|
|
#define CORDIC_ROUNDING 0.0
|
69 |
|
|
|
70 |
|
|
|
71 |
|
|
/* the supported modes */
|
72 |
|
|
#define C_FLAG_VEC_ROT 0x08
|
73 |
|
|
#define C_FLAG_ATAN_3 0x04
|
74 |
|
|
#define C_MODE_MSK 0x03
|
75 |
|
|
#define C_MODE_CIR 0x00
|
76 |
|
|
#define C_MODE_LIN 0x01
|
77 |
|
|
#define C_MODE_HYP 0x02
|
78 |
|
|
|
79 |
|
|
|
80 |
7 |
feddischso |
#ifndef OR32_TARGET
|
81 |
|
|
#define PRINT mexPrintf
|
82 |
|
|
#endif
|
83 |
2 |
feddischso |
|
84 |
|
|
|
85 |
|
|
void cordic_int( long long int x_i,
|
86 |
|
|
long long int y_i,
|
87 |
|
|
long long int a_i,
|
88 |
|
|
long long int * x_o,
|
89 |
|
|
long long int * y_o,
|
90 |
|
|
long long int * a_o,
|
91 |
|
|
int * it_o,
|
92 |
|
|
int mode,
|
93 |
|
|
int XY_WIDTH,
|
94 |
|
|
int A_WIDTH,
|
95 |
|
|
int GUARD_BITS,
|
96 |
|
|
int RM_GAIN );
|
97 |
11 |
feddischso |
void cordic_int_dbg ( long long int x,
|
98 |
2 |
feddischso |
long long int y,
|
99 |
|
|
long long int a,
|
100 |
|
|
int mode,
|
101 |
|
|
int it,
|
102 |
|
|
char* msg );
|
103 |
|
|
int cordic_int_init ( long long int *x,
|
104 |
|
|
long long int *y,
|
105 |
|
|
long long int *a,
|
106 |
|
|
int mode,
|
107 |
|
|
int A_WIDTH,
|
108 |
|
|
int XY_WIDTH );
|
109 |
|
|
void cordic_int_rm_gain( long long int *x,
|
110 |
|
|
long long int *y,
|
111 |
|
|
int mode,
|
112 |
|
|
int rm_gain );
|
113 |
|
|
int cordic_int_rotate( long long int * x,
|
114 |
|
|
long long int * y,
|
115 |
|
|
long long int * a,
|
116 |
|
|
int mode,
|
117 |
|
|
int A_WIDTH );
|
118 |
|
|
long long int cordic_int_lut ( int mode,
|
119 |
|
|
int it,
|
120 |
|
|
int A_WIDTH );
|
121 |
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
7 |
feddischso |
#ifndef OR32_TARGET
|
125 |
2 |
feddischso |
|
126 |
|
|
|
127 |
|
|
void mexFunction(int nlhs, mxArray *plhs[],
|
128 |
|
|
int nrhs, const mxArray *prhs[])
|
129 |
|
|
{
|
130 |
|
|
double *inx,*iny,*inz,*outx,*outy,*outz,*outi;
|
131 |
|
|
double mode;
|
132 |
|
|
double xy_width;
|
133 |
|
|
double a_width;
|
134 |
|
|
double guard_bits;
|
135 |
|
|
double rm_gain;
|
136 |
|
|
|
137 |
|
|
int mrowsx,ncolsx,mrowsy,ncolsy,mrowsz,ncolsz;
|
138 |
|
|
int i;
|
139 |
|
|
int it;
|
140 |
|
|
if(nrhs!=8 )
|
141 |
|
|
mexErrMsgTxt("8 input arguments required");
|
142 |
|
|
if(nlhs!=4)
|
143 |
|
|
mexErrMsgTxt("4 output arguments required");
|
144 |
7 |
feddischso |
|
145 |
2 |
feddischso |
if (!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]))
|
146 |
|
|
mexErrMsgTxt("Input x must be double and non-complex");
|
147 |
7 |
feddischso |
|
148 |
2 |
feddischso |
if (!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]))
|
149 |
|
|
mexErrMsgTxt("Input y must be double and non-complex");
|
150 |
7 |
feddischso |
|
151 |
2 |
feddischso |
if (!mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]))
|
152 |
|
|
mexErrMsgTxt("Input a must be double and non-complex");
|
153 |
7 |
feddischso |
|
154 |
2 |
feddischso |
mrowsx = mxGetM(prhs[0]);
|
155 |
|
|
ncolsx = mxGetN(prhs[0]);
|
156 |
|
|
mrowsy = mxGetM(prhs[1]);
|
157 |
|
|
ncolsy = mxGetN(prhs[1]);
|
158 |
|
|
mrowsz = mxGetM(prhs[2]);
|
159 |
|
|
ncolsz = mxGetN(prhs[2]);
|
160 |
|
|
|
161 |
7 |
feddischso |
|
162 |
|
|
|
163 |
2 |
feddischso |
if (mrowsx > 1 || mrowsy >1 || mrowsz > 1)
|
164 |
|
|
mexErrMsgTxt("Input vector must have the size Nx1.");
|
165 |
|
|
|
166 |
|
|
/* printf("%d %d %d\n", ncolsx, ncolsy, ncolsa); */
|
167 |
7 |
feddischso |
|
168 |
2 |
feddischso |
if (ncolsx!=ncolsy || ncolsx!=ncolsz || ncolsy!=ncolsz)
|
169 |
|
|
mexErrMsgTxt("Input vectors don't have the same length!");
|
170 |
7 |
feddischso |
|
171 |
|
|
|
172 |
2 |
feddischso |
plhs[0] = mxCreateDoubleMatrix(mrowsx,ncolsx,mxREAL);
|
173 |
|
|
plhs[1] = mxCreateDoubleMatrix(mrowsy,ncolsy,mxREAL);
|
174 |
|
|
plhs[2] = mxCreateDoubleMatrix(mrowsz,ncolsz,mxREAL);
|
175 |
|
|
plhs[3] = mxCreateDoubleMatrix(mrowsz,ncolsz,mxREAL);
|
176 |
7 |
feddischso |
|
177 |
2 |
feddischso |
inx = mxGetPr(prhs[0]);
|
178 |
|
|
iny = mxGetPr(prhs[1]);
|
179 |
|
|
inz = mxGetPr(prhs[2]);
|
180 |
|
|
mode = mxGetScalar(prhs[3]);
|
181 |
|
|
xy_width = mxGetScalar(prhs[4]);
|
182 |
|
|
a_width = mxGetScalar(prhs[5]);
|
183 |
|
|
guard_bits = mxGetScalar(prhs[6]);
|
184 |
|
|
rm_gain = mxGetScalar(prhs[7]);
|
185 |
|
|
|
186 |
|
|
outx= mxGetPr(plhs[0]);
|
187 |
|
|
outy= mxGetPr(plhs[1]);
|
188 |
|
|
outz= mxGetPr(plhs[2]);
|
189 |
|
|
outi= mxGetPr(plhs[3]);
|
190 |
|
|
|
191 |
|
|
for( i = 0; i < ncolsx; i++ )
|
192 |
|
|
{
|
193 |
|
|
long long int inx_i = inx[ i ];
|
194 |
|
|
long long int iny_i = iny[ i ];
|
195 |
|
|
long long int inz_i = inz[ i ];
|
196 |
|
|
long long int outx_i = 0;
|
197 |
|
|
long long int outy_i = 0;
|
198 |
|
|
long long int outz_i = 0;
|
199 |
|
|
/* PRINT("x: %lld, y: %lld, a: %lld\n", inx_i, iny_i, inz_i ); */
|
200 |
|
|
|
201 |
|
|
cordic_int( inx_i, iny_i, inz_i,
|
202 |
|
|
&outx_i, &outy_i, &outz_i,
|
203 |
|
|
&it, mode,
|
204 |
|
|
xy_width, a_width, guard_bits, rm_gain );
|
205 |
|
|
outx[i] = outx_i;
|
206 |
|
|
outy[i] = outy_i;
|
207 |
|
|
outz[i] = outz_i;
|
208 |
|
|
outi[i] = it;
|
209 |
|
|
|
210 |
7 |
feddischso |
|
211 |
2 |
feddischso |
}
|
212 |
|
|
}
|
213 |
|
|
|
214 |
7 |
feddischso |
#endif
|
215 |
2 |
feddischso |
|
216 |
|
|
|
217 |
|
|
|
218 |
|
|
|
219 |
|
|
void cordic_int( long long int x_i,
|
220 |
|
|
long long int y_i,
|
221 |
|
|
long long int a_i,
|
222 |
|
|
long long int * x_o,
|
223 |
|
|
long long int * y_o,
|
224 |
|
|
long long int * a_o,
|
225 |
|
|
int * it_o,
|
226 |
|
|
int mode,
|
227 |
|
|
int XY_WIDTH,
|
228 |
|
|
int A_WIDTH,
|
229 |
|
|
int GUARD_BITS,
|
230 |
|
|
int RM_GAIN )
|
231 |
|
|
{
|
232 |
|
|
long long int x;
|
233 |
|
|
long long int y;
|
234 |
|
|
long long int a;
|
235 |
|
|
long long int s;
|
236 |
|
|
int ov;
|
237 |
|
|
int it = 0;
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
|
241 |
7 |
feddischso |
|
242 |
2 |
feddischso |
/* total with, including guard bits */
|
243 |
|
|
int XY_WIDTH_G = XY_WIDTH + GUARD_BITS;
|
244 |
7 |
feddischso |
|
245 |
2 |
feddischso |
cordic_int_dbg( x_i, y_i, a_i, mode, 0, "input" );
|
246 |
|
|
|
247 |
|
|
if( !cordic_int_init( &x_i, &y_i, &a_i, mode, A_WIDTH, XY_WIDTH ) )
|
248 |
|
|
{
|
249 |
|
|
|
250 |
|
|
it = cordic_int_rotate( &x_i, &y_i, &a_i, mode, A_WIDTH );
|
251 |
|
|
|
252 |
|
|
cordic_int_rm_gain( &x_i, &y_i, mode, RM_GAIN );
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
*x_o = x_i;
|
256 |
|
|
*y_o = y_i;
|
257 |
|
|
*a_o = a_i;
|
258 |
|
|
*it_o = it;
|
259 |
|
|
|
260 |
|
|
}
|
261 |
|
|
|
262 |
|
|
|
263 |
|
|
|
264 |
|
|
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
int cordic_int_init( long long int *x,
|
269 |
|
|
long long int *y,
|
270 |
|
|
long long int *a,
|
271 |
|
|
int mode,
|
272 |
|
|
int A_WIDTH,
|
273 |
|
|
int XY_WIDTH )
|
274 |
|
|
{
|
275 |
|
|
int already_done = 0;
|
276 |
|
|
|
277 |
|
|
|
278 |
|
|
long long int PI = ( long long int )( M_PI * pow( 2, A_WIDTH-1 ) + 0.5 );
|
279 |
|
|
long long int PI_H = (long long int)( M_PI * pow( 2, A_WIDTH-2 ) + 0.5 );
|
280 |
|
|
|
281 |
4 |
feddischso |
long long int XY_MAX = pow( 2, XY_WIDTH-1 )-1;
|
282 |
7 |
feddischso |
|
283 |
4 |
feddischso |
cordic_int_dbg( *x, *y, *a, mode, 0, "before init" );
|
284 |
|
|
|
285 |
|
|
|
286 |
7 |
feddischso |
if( 0 != ( mode & C_FLAG_VEC_ROT ) && *y == 0 )
|
287 |
|
|
{
|
288 |
|
|
already_done = 1;
|
289 |
|
|
*a = 0;
|
290 |
|
|
#if PRINT_DEBUG > 0
|
291 |
|
|
PRINT( "Zero input, skipping rotations \n" );
|
292 |
|
|
#endif
|
293 |
|
|
}
|
294 |
|
|
else if( 0 == ( mode & C_FLAG_VEC_ROT ) && *a == 0 )
|
295 |
|
|
{
|
296 |
|
|
#if PRINT_DEBUG > 0
|
297 |
|
|
PRINT("zero angular input\n" );
|
298 |
|
|
#endif
|
299 |
|
|
already_done = 1;
|
300 |
|
|
}
|
301 |
|
|
|
302 |
2 |
feddischso |
/* Circular rotation mode */
|
303 |
7 |
feddischso |
else if( 0 == ( mode & C_FLAG_VEC_ROT ) &&
|
304 |
2 |
feddischso |
C_MODE_CIR == ( mode & C_MODE_MSK ) )
|
305 |
|
|
{
|
306 |
7 |
feddischso |
|
307 |
|
|
|
308 |
2 |
feddischso |
/* move from third quadrant to first
|
309 |
|
|
quadrant if necessary */
|
310 |
|
|
if( *a < - PI_H )
|
311 |
|
|
{
|
312 |
|
|
if( ! (mode & C_FLAG_ATAN_3) )
|
313 |
|
|
*a += PI;
|
314 |
|
|
*x = -*x;
|
315 |
|
|
*y = -*y;
|
316 |
|
|
#if PRINT_DEBUG > 0
|
317 |
|
|
PRINT("move from third quadrand");
|
318 |
|
|
#endif
|
319 |
|
|
}
|
320 |
|
|
/* move from second quadrant to fourth
|
321 |
|
|
quadrant if necessary */
|
322 |
|
|
else if( *a > PI_H )
|
323 |
|
|
{
|
324 |
|
|
if( ! (mode & C_FLAG_ATAN_3) )
|
325 |
|
|
*a -= PI;
|
326 |
|
|
*x = -*x;
|
327 |
|
|
*y = -*y;
|
328 |
|
|
#if PRINT_DEBUG > 0
|
329 |
|
|
PRINT("move from second quadrand\n" );
|
330 |
|
|
#endif
|
331 |
|
|
}
|
332 |
|
|
}
|
333 |
|
|
|
334 |
|
|
/* circular vector mode */
|
335 |
|
|
else if ( 0 != ( mode & C_FLAG_VEC_ROT ) &&
|
336 |
|
|
C_MODE_CIR == ( mode & C_MODE_MSK ) )
|
337 |
|
|
{
|
338 |
|
|
|
339 |
7 |
feddischso |
if( *x == XY_MAX && *y == XY_MAX )
|
340 |
2 |
feddischso |
{
|
341 |
|
|
#if PRINT_DEBUG > 0
|
342 |
|
|
PRINT( "All max, skipping rotations 1\n" );
|
343 |
|
|
#endif
|
344 |
7 |
feddischso |
*x = sqrt( 2 ) * pow( 2, XY_WIDTH-1 ) + 0.5;
|
345 |
|
|
*y = 0;
|
346 |
2 |
feddischso |
*a = cordic_int_lut( mode, 0, A_WIDTH );
|
347 |
|
|
already_done = 1;
|
348 |
|
|
}
|
349 |
|
|
else if( *x == -XY_MAX && *y == -XY_MAX )
|
350 |
|
|
{
|
351 |
|
|
#if PRINT_DEBUG > 0
|
352 |
|
|
PRINT( "All max, skipping rotations 2\n" );
|
353 |
|
|
#endif
|
354 |
7 |
feddischso |
*x = sqrt( 2 ) * pow( 2, XY_WIDTH-1 ) + 0.5;
|
355 |
|
|
*y = 0;
|
356 |
2 |
feddischso |
*a = cordic_int_lut( mode, 0, A_WIDTH ) - PI;
|
357 |
|
|
already_done = 1;
|
358 |
|
|
}
|
359 |
|
|
else if( *x == XY_MAX && *y == -XY_MAX )
|
360 |
|
|
{
|
361 |
|
|
#if PRINT_DEBUG > 0
|
362 |
|
|
PRINT( "All max, skipping rotations 3\n" );
|
363 |
|
|
#endif
|
364 |
7 |
feddischso |
*x = sqrt( 2 ) * pow( 2, XY_WIDTH-1 ) + 0.5;
|
365 |
|
|
*y = 0;
|
366 |
2 |
feddischso |
*a = -cordic_int_lut( mode, 0, A_WIDTH );
|
367 |
|
|
already_done = 1;
|
368 |
|
|
}
|
369 |
|
|
else if( *x == -XY_MAX && *y == XY_MAX )
|
370 |
|
|
{
|
371 |
|
|
#if PRINT_DEBUG > 0
|
372 |
|
|
PRINT( "All max, skipping rotations 4\n" );
|
373 |
|
|
#endif
|
374 |
7 |
feddischso |
*x = sqrt( 2 ) * pow( 2, XY_WIDTH-1 ) + 0.5;
|
375 |
|
|
*y = 0;
|
376 |
2 |
feddischso |
*a = PI - cordic_int_lut( mode, 0, A_WIDTH );
|
377 |
|
|
already_done = 1;
|
378 |
|
|
}
|
379 |
|
|
|
380 |
|
|
|
381 |
|
|
|
382 |
|
|
else if( *x == 0 && *y > 0 )
|
383 |
|
|
{
|
384 |
|
|
*a = PI_H;
|
385 |
|
|
*x = *y;
|
386 |
|
|
already_done = 1;
|
387 |
|
|
#if PRINT_DEBUG > 0
|
388 |
7 |
feddischso |
PRINT( "Fixed value of pi/2, skipping rotations\n" );
|
389 |
2 |
feddischso |
#endif
|
390 |
|
|
*y = 0;
|
391 |
|
|
}
|
392 |
|
|
else if( *x == 0 && *y < 0 )
|
393 |
|
|
{
|
394 |
|
|
*a = -PI_H;
|
395 |
|
|
*x = -*y;
|
396 |
|
|
*y = 0;
|
397 |
|
|
already_done = 1;
|
398 |
|
|
#if PRINT_DEBUG > 0
|
399 |
7 |
feddischso |
PRINT( "Fixed value of -pi/2, skipping rotations\n" );
|
400 |
2 |
feddischso |
#endif
|
401 |
|
|
}
|
402 |
|
|
else if( *x < 0 && *y >= 0 )
|
403 |
|
|
{
|
404 |
|
|
*x = -*x;
|
405 |
|
|
*y = -*y;
|
406 |
|
|
*a = PI;
|
407 |
|
|
#if PRINT_DEBUG > 0
|
408 |
|
|
PRINT("pre-rotation from second to the fourth quadrant\n" );
|
409 |
|
|
#endif
|
410 |
|
|
}
|
411 |
|
|
else if( *x < 0 && *y < 0 )
|
412 |
|
|
{
|
413 |
|
|
*x = -*x;
|
414 |
|
|
*y = -*y;
|
415 |
|
|
*a = -PI;
|
416 |
|
|
#if PRINT_DEBUG > 0
|
417 |
|
|
PRINT("pre-rotation from third to first quadrand\n" );
|
418 |
|
|
#endif
|
419 |
|
|
}
|
420 |
|
|
else
|
421 |
|
|
*a = 0;
|
422 |
|
|
}
|
423 |
|
|
/* linear vector mode */
|
424 |
|
|
else if ( 0 != ( mode & C_FLAG_VEC_ROT ) &&
|
425 |
|
|
C_MODE_LIN == ( mode & C_MODE_MSK ) )
|
426 |
|
|
{
|
427 |
|
|
if( *x < 0 )
|
428 |
|
|
{
|
429 |
|
|
*x = -*x;
|
430 |
|
|
*y = -*y;
|
431 |
|
|
}
|
432 |
|
|
*a = 0;
|
433 |
|
|
}
|
434 |
|
|
cordic_int_dbg( *x, *y, *a, mode, 0, "after init" );
|
435 |
|
|
return already_done;
|
436 |
|
|
}
|
437 |
|
|
|
438 |
|
|
|
439 |
|
|
|
440 |
11 |
feddischso |
void cordic_int_dbg( long long int x,
|
441 |
2 |
feddischso |
long long int y,
|
442 |
|
|
long long int a,
|
443 |
|
|
int mode,
|
444 |
|
|
int it,
|
445 |
|
|
char* msg )
|
446 |
|
|
{
|
447 |
|
|
#if PRINT_DEBUG > 0
|
448 |
|
|
PRINT( "%20s: mode = %d, iteration %d, x = % 10.lld, y = %10.lld, a = %10.lld \n",
|
449 |
|
|
msg,mode,it,x,y,a );
|
450 |
|
|
#endif
|
451 |
|
|
}
|
452 |
|
|
|
453 |
|
|
int cordic_int_repeat( iteration, mode )
|
454 |
|
|
{
|
455 |
|
|
int i = 4;
|
456 |
7 |
feddischso |
|
457 |
2 |
feddischso |
if( C_MODE_HYP != ( mode & C_MODE_MSK ) )
|
458 |
|
|
return 0;
|
459 |
7 |
feddischso |
|
460 |
|
|
|
461 |
2 |
feddischso |
while( 1 )
|
462 |
|
|
{
|
463 |
|
|
if( i == iteration )
|
464 |
|
|
return 1;
|
465 |
|
|
else if( i > iteration )
|
466 |
|
|
return 0;
|
467 |
|
|
i = i * 3 + 1;
|
468 |
|
|
}
|
469 |
|
|
}
|
470 |
|
|
|
471 |
|
|
|
472 |
|
|
|
473 |
|
|
|
474 |
|
|
int cordic_int_rotate( long long int * x,
|
475 |
|
|
long long int * y,
|
476 |
|
|
long long int * a,
|
477 |
|
|
int mode,
|
478 |
|
|
int A_WIDTH )
|
479 |
|
|
{
|
480 |
|
|
int it = 0;
|
481 |
|
|
long long int xsh, ysh;
|
482 |
|
|
long long int ylst, alst;
|
483 |
|
|
int sign;
|
484 |
|
|
int repeat = 0;
|
485 |
|
|
|
486 |
|
|
while( 1 )
|
487 |
|
|
{
|
488 |
|
|
/* get the sign */
|
489 |
|
|
if( 0 == ( mode & C_FLAG_VEC_ROT ) )
|
490 |
|
|
sign = ( *a >= 0 );
|
491 |
|
|
else
|
492 |
|
|
sign = !( *y >= 0 );
|
493 |
|
|
|
494 |
|
|
/* shift operation: hyperbolic case*/
|
495 |
|
|
if( C_MODE_HYP == ( mode & C_MODE_MSK ) )
|
496 |
|
|
{
|
497 |
|
|
xsh = *x >> (it+1);
|
498 |
|
|
ysh = *y >> (it+1);
|
499 |
|
|
}
|
500 |
|
|
/* shift operation: circular and linear case*/
|
501 |
|
|
else
|
502 |
|
|
{
|
503 |
|
|
xsh = *x >> it;
|
504 |
|
|
ysh = *y >> it;
|
505 |
|
|
}
|
506 |
|
|
|
507 |
|
|
if( sign == 1 )
|
508 |
|
|
{
|
509 |
|
|
*a -= cordic_int_lut( mode, it, A_WIDTH );
|
510 |
|
|
|
511 |
|
|
if( C_MODE_CIR == ( mode & C_MODE_MSK ) )
|
512 |
|
|
{
|
513 |
|
|
*x = *x - ysh;
|
514 |
|
|
*y = *y + xsh;
|
515 |
|
|
}
|
516 |
|
|
else
|
517 |
|
|
if( C_MODE_LIN == ( mode & C_MODE_MSK ) )
|
518 |
|
|
{
|
519 |
|
|
*x = *x;
|
520 |
|
|
*y = *y + xsh;
|
521 |
|
|
}
|
522 |
|
|
else
|
523 |
|
|
{
|
524 |
|
|
*x = *x + ysh;
|
525 |
|
|
*y = *y + xsh;
|
526 |
|
|
}
|
527 |
|
|
}
|
528 |
|
|
else
|
529 |
|
|
{
|
530 |
|
|
*a += cordic_int_lut( mode, it, A_WIDTH );
|
531 |
|
|
if( C_MODE_CIR == ( mode & C_MODE_MSK ) )
|
532 |
|
|
{
|
533 |
|
|
*x = *x + ysh;
|
534 |
|
|
*y = *y - xsh;
|
535 |
|
|
}
|
536 |
|
|
else
|
537 |
|
|
if( C_MODE_LIN == ( mode & C_MODE_MSK ) )
|
538 |
|
|
{
|
539 |
|
|
*x = *x;
|
540 |
|
|
*y = *y - xsh;
|
541 |
|
|
}
|
542 |
|
|
else
|
543 |
|
|
{
|
544 |
|
|
*x = *x - ysh;
|
545 |
|
|
*y = *y - xsh;
|
546 |
|
|
}
|
547 |
|
|
}
|
548 |
|
|
cordic_int_dbg( *x, *y, *a, mode, it, "after rotation" );
|
549 |
7 |
feddischso |
|
550 |
2 |
feddischso |
/* abort condition */
|
551 |
|
|
if( ( mode & C_FLAG_VEC_ROT ) == 0 &&
|
552 |
|
|
( *a == 0 /* || *a == -1 */ ) )
|
553 |
|
|
break;
|
554 |
|
|
if( ( mode & C_FLAG_VEC_ROT ) == 0 &&
|
555 |
|
|
( *a == alst ) )
|
556 |
|
|
break;
|
557 |
7 |
feddischso |
|
558 |
2 |
feddischso |
if( ( mode & C_FLAG_VEC_ROT ) != 0 &&
|
559 |
|
|
( *y == 0 /*|| *y == -1 */ ) )
|
560 |
|
|
break;
|
561 |
|
|
if( ( mode & C_FLAG_VEC_ROT ) != 0 &&
|
562 |
|
|
( *y == ylst ) )
|
563 |
|
|
break;
|
564 |
|
|
|
565 |
|
|
else if( it > 40 )
|
566 |
|
|
{
|
567 |
7 |
feddischso |
#if PRINT_DEBUG
|
568 |
2 |
feddischso |
PRINT( "ERROR: abort %lld %lld %lld %lld - %d - %d!\n", *a, alst, *y, ylst, mode, *y == ylst );
|
569 |
7 |
feddischso |
#endif
|
570 |
2 |
feddischso |
it = -1;
|
571 |
|
|
break;
|
572 |
|
|
}
|
573 |
|
|
|
574 |
|
|
|
575 |
|
|
ylst = *y;
|
576 |
|
|
alst = *a;
|
577 |
|
|
if( repeat == 0 && cordic_int_repeat( it, mode ) )
|
578 |
|
|
{
|
579 |
|
|
repeat = 1;
|
580 |
|
|
#if PRINT_DEBUG
|
581 |
|
|
mexPrintf( "repeat it %d\n" , it );
|
582 |
|
|
#endif
|
583 |
|
|
}
|
584 |
|
|
else
|
585 |
|
|
{
|
586 |
|
|
repeat = 0;
|
587 |
|
|
it++;
|
588 |
|
|
}
|
589 |
|
|
}
|
590 |
|
|
return it;
|
591 |
|
|
}
|
592 |
|
|
|
593 |
|
|
|
594 |
|
|
|
595 |
|
|
#define SCALE_VAL( _W_ )( (double)( (long long int) 1 << (long long int)( _W_ -1 ) ) )
|
596 |
|
|
|
597 |
|
|
long long int cordic_int_lut( int mode, int it, int A_WIDTH )
|
598 |
|
|
{
|
599 |
|
|
long long int lut_val;
|
600 |
|
|
if( C_MODE_CIR == ( mode & C_MODE_MSK ) )
|
601 |
|
|
{
|
602 |
|
|
if( it <= 10 )
|
603 |
|
|
lut_val = (long long int)( atan( pow( 2, -it ) ) * pow( 2, A_WIDTH-1 ) );
|
604 |
|
|
else
|
605 |
|
|
lut_val = pow( 2, A_WIDTH-1-it ); /* (long long int)( SCALE_VAL( A_WIDTH-it ) ); */
|
606 |
|
|
}
|
607 |
|
|
else
|
608 |
|
|
if( C_MODE_LIN == ( mode & C_MODE_MSK ) )
|
609 |
|
|
{
|
610 |
|
|
lut_val = (long long int)( 1.0 / (double)( ( long long int )1 << (long long int)it )
|
611 |
|
|
* SCALE_VAL( A_WIDTH ) -1 );
|
612 |
|
|
}
|
613 |
|
|
else
|
614 |
|
|
{
|
615 |
|
|
lut_val = (long long int)( atanh( 1.0 / (double)( (long long int)1 << ( long long int )(it+1) ) )
|
616 |
|
|
* SCALE_VAL( A_WIDTH ) );
|
617 |
|
|
}
|
618 |
|
|
return lut_val;
|
619 |
|
|
}
|
620 |
|
|
|
621 |
|
|
|
622 |
|
|
|
623 |
|
|
|
624 |
|
|
/**
|
625 |
|
|
*
|
626 |
|
|
* Cordic gain:
|
627 |
|
|
*
|
628 |
|
|
*
|
629 |
|
|
*
|
630 |
|
|
*/
|
631 |
|
|
void cordic_int_rm_gain( long long int *x,
|
632 |
|
|
long long int *y,
|
633 |
|
|
int mode,
|
634 |
|
|
int rm_gain )
|
635 |
|
|
{
|
636 |
|
|
/* for the non-linear case: remove cordic gain if RM_GAIN > 0 */
|
637 |
|
|
if( C_MODE_CIR == ( mode & C_MODE_MSK ) )
|
638 |
|
|
{
|
639 |
|
|
|
640 |
|
|
|
641 |
|
|
switch( rm_gain )
|
642 |
|
|
{
|
643 |
|
|
case 1: *x = + ( *x >> 1 ) ; break; /* error: 0.1072529350 */
|
644 |
|
|
case 2: *x = + ( *x >> 1 ) - ( *x >> 4 ) ; break; /* error: 0.1697529350 */
|
645 |
|
|
case 3: *x = + ( *x >> 1 ) + ( *x >> 3 ) - ( *x >> 5 ) ; break; /* error: 0.0135029350 */
|
646 |
|
|
case 4: *x = + ( *x >> 1 ) + ( *x >> 3 ) - ( *x >> 6 ) - ( *x >> 8 ) ; break; /* error: 0.0017841850 */
|
647 |
|
|
case 5: *x = + ( *x >> 1 ) + ( *x >> 3 ) - ( *x >> 6 ) - ( *x >> 9 ) - ( *x >> 12 ) ; break; /* error: 0.0000752006 */
|
648 |
|
|
case 6: *x = + ( *x >> 1 ) + ( *x >> 3 ) - ( *x >> 6 ) - ( *x >> 9 ) - ( *x >> 13 ) - ( *x >> 14 ) ; break; /* error: 0.0000141655 */
|
649 |
|
|
case 7: *x = + ( *x >> 1 ) + ( *x >> 3 ) - ( *x >> 6 ) - ( *x >> 9 ) - ( *x >> 13 ) - ( *x >> 14 ) - ( *x >> 17 ) ; break; /* error: 0.0000217949 */
|
650 |
|
|
case 8: *x = + ( *x >> 1 ) + ( *x >> 3 ) - ( *x >> 6 ) - ( *x >> 9 ) - ( *x >> 13 ) - ( *x >> 14 ) + ( *x >> 16 ) - ( *x >> 19 ) ; break; /* error: 0.0000008140 */
|
651 |
|
|
case 9: *x = + ( *x >> 1 ) + ( *x >> 3 ) - ( *x >> 6 ) - ( *x >> 9 ) - ( *x >> 13 ) - ( *x >> 14 ) + ( *x >> 16 ) - ( *x >> 20 ) - ( *x >> 22 ) ; break; /* error: 0.0000000988 */
|
652 |
|
|
case 10: *x = + ( *x >> 1 ) + ( *x >> 3 ) - ( *x >> 6 ) - ( *x >> 9 ) - ( *x >> 13 ) - ( *x >> 14 ) + ( *x >> 16 ) - ( *x >> 20 ) - ( *x >> 23 ) - ( *x >> 25 ) ; break; /* error: 0.0000000094 */
|
653 |
|
|
case 11: *x = + ( *x >> 1 ) + ( *x >> 3 ) - ( *x >> 6 ) - ( *x >> 9 ) - ( *x >> 13 ) - ( *x >> 14 ) + ( *x >> 16 ) - ( *x >> 20 ) - ( *x >> 23 ) - ( *x >> 26 ) - ( *x >> 27 ) ; break; /* error: 0.0000000019 */
|
654 |
|
|
case 12: *x = + ( *x >> 1 ) + ( *x >> 3 ) - ( *x >> 6 ) - ( *x >> 9 ) - ( *x >> 13 ) - ( *x >> 14 ) + ( *x >> 16 ) - ( *x >> 20 ) - ( *x >> 23 ) - ( *x >> 26 ) - ( *x >> 28 ) - ( *x >> 29 ) ; break; /* error: 0.0000000001 */
|
655 |
|
|
default: *x = *x; break;
|
656 |
|
|
}
|
657 |
|
|
switch( rm_gain )
|
658 |
|
|
{
|
659 |
|
|
case 1: *y = + ( *y >> 1 ) ; break; /* error: 0.1072529350 */
|
660 |
|
|
case 2: *y = + ( *y >> 1 ) - ( *y >> 4 ) ; break; /* error: 0.1697529350 */
|
661 |
|
|
case 3: *y = + ( *y >> 1 ) + ( *y >> 3 ) - ( *y >> 5 ) ; break; /* error: 0.0135029350 */
|
662 |
|
|
case 4: *y = + ( *y >> 1 ) + ( *y >> 3 ) - ( *y >> 6 ) - ( *y >> 8 ) ; break; /* error: 0.0017841850 */
|
663 |
|
|
case 5: *y = + ( *y >> 1 ) + ( *y >> 3 ) - ( *y >> 6 ) - ( *y >> 9 ) - ( *y >> 12 ) ; break; /* error: 0.0000752006 */
|
664 |
|
|
case 6: *y = + ( *y >> 1 ) + ( *y >> 3 ) - ( *y >> 6 ) - ( *y >> 9 ) - ( *y >> 13 ) - ( *y >> 14 ) ; break; /* error: 0.0000141655 */
|
665 |
|
|
case 7: *y = + ( *y >> 1 ) + ( *y >> 3 ) - ( *y >> 6 ) - ( *y >> 9 ) - ( *y >> 13 ) - ( *y >> 14 ) - ( *y >> 17 ) ; break; /* error: 0.0000217949 */
|
666 |
|
|
case 8: *y = + ( *y >> 1 ) + ( *y >> 3 ) - ( *y >> 6 ) - ( *y >> 9 ) - ( *y >> 13 ) - ( *y >> 14 ) + ( *y >> 16 ) - ( *y >> 19 ) ; break; /* error: 0.0000008140 */
|
667 |
|
|
case 9: *y = + ( *y >> 1 ) + ( *y >> 3 ) - ( *y >> 6 ) - ( *y >> 9 ) - ( *y >> 13 ) - ( *y >> 14 ) + ( *y >> 16 ) - ( *y >> 20 ) - ( *y >> 22 ) ; break; /* error: 0.0000000988 */
|
668 |
|
|
case 10: *y = + ( *y >> 1 ) + ( *y >> 3 ) - ( *y >> 6 ) - ( *y >> 9 ) - ( *y >> 13 ) - ( *y >> 14 ) + ( *y >> 16 ) - ( *y >> 20 ) - ( *y >> 23 ) - ( *y >> 25 ) ; break; /* error: 0.0000000094 */
|
669 |
|
|
case 11: *y = + ( *y >> 1 ) + ( *y >> 3 ) - ( *y >> 6 ) - ( *y >> 9 ) - ( *y >> 13 ) - ( *y >> 14 ) + ( *y >> 16 ) - ( *y >> 20 ) - ( *y >> 23 ) - ( *y >> 26 ) - ( *y >> 27 ) ; break; /* error: 0.0000000019 */
|
670 |
|
|
case 12: *y = + ( *y >> 1 ) + ( *y >> 3 ) - ( *y >> 6 ) - ( *y >> 9 ) - ( *y >> 13 ) - ( *y >> 14 ) + ( *y >> 16 ) - ( *y >> 20 ) - ( *y >> 23 ) - ( *y >> 26 ) - ( *y >> 28 ) - ( *y >> 29 ) ; break; /* error: 0.0000000001 */
|
671 |
|
|
default: *x = *y; break;
|
672 |
|
|
}
|
673 |
|
|
|
674 |
|
|
}
|
675 |
|
|
else
|
676 |
|
|
if( C_MODE_HYP == ( mode & C_MODE_MSK ) )
|
677 |
|
|
{
|
678 |
|
|
switch( rm_gain )
|
679 |
|
|
{
|
680 |
|
|
case 1: *x = *x - ( *x >> 3 ) ; break; /* error: 0.3324970678 */
|
681 |
|
|
case 2: *x = *x + ( *x >> 2 ) - ( *x >> 4 ) ; break; /* error: 0.0199970678 */
|
682 |
|
|
case 3: *x = *x + ( *x >> 2 ) - ( *x >> 5 ) - ( *x >> 6 ) ; break; /* error: 0.0043720678 */
|
683 |
|
|
case 4: *x = *x + ( *x >> 2 ) - ( *x >> 5 ) - ( *x >> 7 ) - ( *x >> 8 ) ; break; /* error: 0.0004658178 */
|
684 |
|
|
case 5: *x = *x + ( *x >> 2 ) - ( *x >> 5 ) - ( *x >> 7 ) - ( *x >> 8 ) - ( *x >> 12 ) ; break; /* error: 0.0007099584 */
|
685 |
|
|
case 6: *x = *x + ( *x >> 2 ) - ( *x >> 5 ) - ( *x >> 7 ) - ( *x >> 8 ) + ( *x >> 11 ) - ( *x >> 15 ) ; break; /* error: 0.0000080541 */
|
686 |
|
|
case 7: *x = *x + ( *x >> 2 ) - ( *x >> 5 ) - ( *x >> 7 ) - ( *x >> 8 ) + ( *x >> 11 ) - ( *x >> 16 ) - ( *x >> 17 ) ; break; /* error: 0.0000004247 */
|
687 |
|
|
case 8: *x = *x + ( *x >> 2 ) - ( *x >> 5 ) - ( *x >> 7 ) - ( *x >> 8 ) + ( *x >> 11 ) - ( *x >> 16 ) - ( *x >> 17 ) - ( *x >> 22 ) ; break; /* error: 0.0000006631 */
|
688 |
|
|
case 9: *x = *x + ( *x >> 2 ) - ( *x >> 5 ) - ( *x >> 7 ) - ( *x >> 8 ) + ( *x >> 11 ) - ( *x >> 16 ) - ( *x >> 17 ) + ( *x >> 21 ) - ( *x >> 24 ) ; break; /* error: 0.0000000075 */
|
689 |
|
|
case 10: *x = *x + ( *x >> 2 ) - ( *x >> 5 ) - ( *x >> 7 ) - ( *x >> 8 ) + ( *x >> 11 ) - ( *x >> 16 ) - ( *x >> 17 ) + ( *x >> 21 ) - ( *x >> 24 ) + ( *x >> 27 ) ; break; /* error: 0.0000000000 */
|
690 |
|
|
case 11: *x = *x + ( *x >> 2 ) - ( *x >> 5 ) - ( *x >> 7 ) - ( *x >> 8 ) + ( *x >> 11 ) - ( *x >> 16 ) - ( *x >> 17 ) + ( *x >> 21 ) - ( *x >> 24 ) + ( *x >> 27 ) - ( *x >> 37 ) ; break; /* error: 0.0000000000 */
|
691 |
|
|
case 12: *x = *x + ( *x >> 2 ) - ( *x >> 5 ) - ( *x >> 7 ) - ( *x >> 8 ) + ( *x >> 11 ) - ( *x >> 16 ) - ( *x >> 17 ) + ( *x >> 21 ) - ( *x >> 24 ) + ( *x >> 27 ) + ( *x >> 36 ) - ( *x >> 39 ) ; break; /* error: 0.0000000000 */
|
692 |
|
|
}
|
693 |
|
|
switch( rm_gain )
|
694 |
|
|
{
|
695 |
|
|
case 1: *y = *y - ( *y >> 3 ) ; break; /* error: 0.3324970678 */
|
696 |
|
|
case 2: *y = *y + ( *y >> 2 ) - ( *y >> 4 ) ; break; /* error: 0.0199970678 */
|
697 |
|
|
case 3: *y = *y + ( *y >> 2 ) - ( *y >> 5 ) - ( *y >> 6 ) ; break; /* error: 0.0043720678 */
|
698 |
|
|
case 4: *y = *y + ( *y >> 2 ) - ( *y >> 5 ) - ( *y >> 7 ) - ( *y >> 8 ) ; break; /* error: 0.0004658178 */
|
699 |
|
|
case 5: *y = *y + ( *y >> 2 ) - ( *y >> 5 ) - ( *y >> 7 ) - ( *y >> 8 ) - ( *y >> 12 ) ; break; /* error: 0.0007099584 */
|
700 |
|
|
case 6: *y = *y + ( *y >> 2 ) - ( *y >> 5 ) - ( *y >> 7 ) - ( *y >> 8 ) + ( *y >> 11 ) - ( *y >> 15 ) ; break; /* error: 0.0000080541 */
|
701 |
|
|
case 7: *y = *y + ( *y >> 2 ) - ( *y >> 5 ) - ( *y >> 7 ) - ( *y >> 8 ) + ( *y >> 11 ) - ( *y >> 16 ) - ( *y >> 17 ) ; break; /* error: 0.0000004247 */
|
702 |
|
|
case 8: *y = *y + ( *y >> 2 ) - ( *y >> 5 ) - ( *y >> 7 ) - ( *y >> 8 ) + ( *y >> 11 ) - ( *y >> 16 ) - ( *y >> 17 ) - ( *y >> 22 ) ; break; /* error: 0.0000006631 */
|
703 |
|
|
case 9: *y = *y + ( *y >> 2 ) - ( *y >> 5 ) - ( *y >> 7 ) - ( *y >> 8 ) + ( *y >> 11 ) - ( *y >> 16 ) - ( *y >> 17 ) + ( *y >> 21 ) - ( *y >> 24 ) ; break; /* error: 0.0000000075 */
|
704 |
|
|
case 10: *y = *y + ( *y >> 2 ) - ( *y >> 5 ) - ( *y >> 7 ) - ( *y >> 8 ) + ( *y >> 11 ) - ( *y >> 16 ) - ( *y >> 17 ) + ( *y >> 21 ) - ( *y >> 24 ) + ( *y >> 27 ) ; break; /* error: 0.0000000000 */
|
705 |
|
|
case 11: *y = *y + ( *y >> 2 ) - ( *y >> 5 ) - ( *y >> 7 ) - ( *y >> 8 ) + ( *y >> 11 ) - ( *y >> 16 ) - ( *y >> 17 ) + ( *y >> 21 ) - ( *y >> 24 ) + ( *y >> 27 ) - ( *y >> 37 ) ; break; /* error: 0.0000000000 */
|
706 |
|
|
case 12: *y = *y + ( *y >> 2 ) - ( *y >> 5 ) - ( *y >> 7 ) - ( *y >> 8 ) + ( *y >> 11 ) - ( *y >> 16 ) - ( *y >> 17 ) + ( *y >> 21 ) - ( *y >> 24 ) + ( *y >> 27 ) + ( *y >> 36 ) - ( *y >> 39 ) ; break; /* error: 0.0000000000 */
|
707 |
|
|
}
|
708 |
|
|
}
|
709 |
|
|
}
|
710 |
|
|
|