EE 5324 – VLSI Design II Part IX: CORDIC Algorithms Kia Bazargan University of Minnesota Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan 368 References and Copyright • Textbook referenced [Par00] B. Parhami “Computer Arithmetic: Algorithms and Hardware Designs” Oxford University Press, 2000. • Slides used(Modified by Kia when necessary) [©Oxford U Press] © Oxford University Press, New York, 2000 Slides for [Par00] (With permission from the author) http://www•ece•ucsb•edu/Faculty/Parhami/files_n_docs•htm [©Wilde] © Prof. Doran Wilde, Lecture notes on the Computer Arithmetic Course, ECE Dept., Brigham Young University Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan 369 What is CORDIC? • How to evaluate trigonometric functions? Table lookup Polynomial approximations CORDIC • CORDIC (COordinate Rotation DIgital Computer) Introduced in 1959 by Jack E. Volder Rotate vector (1,0) by f to get (cos f, sin f) Can evaluate many functions Rotation reduced to shift-add operations Convergence method (iterative) o N iterations for N-bit accuracy Delay / hardware costs comparable to division or square rooting! Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan 370 Basic CORDIC Transformations Y sin f • Basic idea f Rotate (1,0) by f degrees to get (x,y): x=cos(f), y=sin(f) cos f Y • Rotation of any (x,y) vector: (x’,y’) x x. cos(f ) y. sin( f ) y y. cos(f ) x. sin( f ) f • Rearrange as: x cos(f ).[ x y. tan( f )] y cos(f ).[ y x. tan( f )] Spring 2006 X Note : EE 5324 - VLSI Design II - © Kia Bazargan (x,y) X sin( f ) tan(f ) cos(f ) 371 Rotation and Magnitude Components x cos(f ).[ x y. tan( f )] y cos(f ).[ y x. tan( f )] • Two components: cos(f) Y o Reduces the magnitude of the vector o If don’t multiply pseudo rotation (x’,y’) tan(f) o Rotates the vector o Break f into a series of successively O shrinking angles ai such that: Shift operation tan (ai) =2-i o Should we use all ai ’s? Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan f (x,y) X [© Oxford U Press] 372 Pre-computation of tan(ai) • Find ai such that tan(ai)=2-i : (or, ai=tan-1(2-i) ) i ai tan(ai) 0 1 2 3 4 5 6 7 8 9 45.0° 26.6° 14.0° 7.1° 3.6° 1.8° 0.9° 0.4° 0.2° 0.1° 1 0.5 0.25 0.125 0.0625 0.03125 0.015625 0.0078125 0.00390625 0.001953125 = = = = = = = = = = 2-0 2-1 2-2 2-3 2-4 2-5 2-6 2-7 2-8 2-9 • Note: decreasing ai. Possible to write any angle f = a0 a1 … a9 as long as -99.7° f 99.7° (which covers –90..90) Convergence possible: ai ≤ Nj=i+1ai+1 (10-5 deg accuracy) Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan 373 Example: Rewriting Angles in Terms of ai • Example: f=30.0° Start with a0 = 45.0 45.0 – 26.6 = 18.4 18.4 + 14.0 = 32.4 32.4 – 7.1 = 25.3 25.3 + 3.6 = 28.9 28.9 + 1.8 = 30.7 ... Y (> (< (> (< (< (> 30.0) 30.0) 30.0) 30.0) 30.0) 30.0) 45° 30° X f = 30.0 45.0 – 26.6 + 14.0 – 7.1 + 3.6 + 1.8 – 0.9 + 0.4 – 0.2 + 0.1 = 30.1 Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan 374 Rotation Reduction x cos(f ).[ x y. tan( f )] y cos(f ).[ y x. tan( f )] • Rewrite in terms of ai: (0 i n) xi 1 cos(a i ).[ xi yi .di . tan(a i )] yi 1 cos(a i ).[ yi xi .d i . tan(a i )] i xi 1 K i .[ xi yi .d i .2 ] i yi 1 K i .[ yi xi .d i .2 ] • Where: K i cos(a i ) cos(tan (2 )) 1 i di 1 Note : cos(a i ) cos( a i ) • What about Ki’s? Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan 375 Taking Care of the Magnitude xi 1 K i .[ xi yi .d i .2i ] yi 1 K i .[ yi xi .d i .2 i ] • Observations: We choose to always use ALL ai terms, with +/- signs Ki = cos(ai) = cos(-ai) At each step, we multiply by cos(ai) [constant?] • Let the multiplications aggregate to: n K Ki n , K 0.607 252 935... i 0 Multiply this constant ONLY ONCE at the end Even better: start with (K,0) instead of (1,0) no multiplication at all! Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan 376 Hardware Realization: CORDIC Rotation Mode First rotate by f, then rotate by –di.ai to get 0 (no subtraction to compare f and current angle) • Algorithm: (z is the current angle) Spring 2006 Similar to non-restor’g sqrt • To simplify the hardware: Mode: rotation: “at each step, try to make z zero” Initialize x=0.607253,y=0,z=f For i=0 n x0,y0 y di = 1 when z>0, else -1 x2,y2 xi+1= xi – di . 2-i . yi -45 30 x10 x -14 yi+1 = yi + di . 2-i . xi +26.6 x3,y3 zi+1 = zi – di . ai x1,y1 Result: xn=cos(f), yn=sin(f) Precision: n bits (tan-1(2-i) 2-i) [© Oxford U Press] EE 5324 - VLSI Design II - © Kia Bazargan 377 CORDIC Rotation Mode C Code 0.6072529350 ((long int)((X) * 65536.0)) // downloaded (and modified by Kia) from // www.execpc.com/~geezer/embed/cordic.c #include <stdio.h> #include <math.h> #define AG_CONST #define FXD(X) typedef long int fixed; /* 16.16 fixed-point */ static const fixed Alpha[]={ FXD(45.0), FXD(26.565), FXD(14.0362), FXD(7.12502), FXD(3.57633), FXD(1.78991), FXD(0.895174), FXD(0.447614),FXD(0.223811), FXD(0.111906), FXD(0.055953),FXD(0.027977) }; int main(void){ fixed X, Y, CurrAngle; unsigned i; 378 EE 5324 - VLSI Design II - © Kia Bazargan Spring 2006 X=FXD(AG_CONST); /* AG_CONST * cos(0) */ Y=0; /* AG_CONST * sin(0) */ CurrAngle=FXD(28.027); for(i=0; i < 12; i++){ fixed NewX; if(CurrAngle > 0) { NewX=X - (Y >> i); Y+=(X >> i); X=NewX; CurrAngle -= Alpha[i]; } else { NewX=X + (Y >> i); Y-=(X >> i); X=NewX; CurrAngle += Alpha[i]; } // if-else } // for (i=… printf(“cos(28.027)=%6.4f, sin()=%6.4f\n”, x/65536.0, y/65536.0); } // main Only used: >0, >>, + Integer ops CORDIC Hardware x shift y shift z ± ± ± Lookup Table [© Oxford U Press] Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan 379 CORDIC Vectoring Mode • Difference with rotation mode? When choosing di, instead of trying to make z converge to 0, try to make yi zero di = –sign (xi . yi) • Variables will converge to: xn = 1/K (x2 + y2)½ yn = 0 Zn = z + tan-1(y/x) • Application? If start with x=1, z=0, the final z is tan-1(y) Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan 380 Generalized CORDIC 1 y • Generalized CORDIC iteration: xi+1= xi – . di . 2-i . yi yi+1 = yi + di . 2-i . xi zi+1 = zi – di . e(i) • Variations: 1 0 -1 Spring 2006 Function Circular rotation (basic CORDIC) Linear rotation Hyperbolic rotation EE 5324 - VLSI Design II - © Kia Bazargan B A F O 0 1 E W V U x C D e(i) tan-1(2-i) 2-i tanh-1(2-i) [© Oxford U Press] 381 Various CORDIC Applications • Directly computes: sin, cos, sinh, cosh tan-1, tanh-1 Division, multiplication • Also directly computes: tan-1(y/x) y + x.z (x2+y2)1/2 (x2-y2)1/2 ez = sinh(z) + cosh(z) [© Wilde] Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan 382 Various CORDIC Applications (cont.) • Indirectly computes: sin z tan z cos z sinh z tanh z cosh z ln w 2 tanh 1 w e t Spring 2006 t ln w 1 1 1 cos w tan sin w tan w 1 w 1 log b w K . ln w 1 w ln w 1 w w w 1 w 2 2 1 w cosh 1 w ln w 1 w2 sinh 1 2 w ( w 1 / 4) ( w 1 / 4) 2 2 [© Wilde] EE 5324 - VLSI Design II - © Kia Bazargan 383 Summary of CORDIC Applications Rotation mode di=sign(zi), zi 0 = 1 Circular e(i) = tan-12-i = 0 Linear e(i)= 2-i x y z C O R D I C K(x cos z - y sin z) K(y cos z + x sin z) 0 For cos & sin, set x=1/K, y=0 tan z = sin z / cos z x y z C O R D I C Vectoring mode di=-sign(xi yi), yi 0 C K (x2 + y2)1/2 O R y 0 D -1 I z z + tan (y/x) C For tan-1, set x = 1, z = 0 cos-1w = tan-1[(1-w2)1/2/w] sin-1w = tan-1[w/(1-w2)1/2] x x x y + xz y 0 For multiplication, set y = 0 z C O R D I C x 0 z + y/x For division, set z = 0 Note: in linear mode, limited input range (convergence) Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan [© Oxford U Press] [Par00], p. 371 384 Summary of CORDIC Applications (cont.) Rotation mode di=sign(zi), zi 0 Vectoring mode di=-sign(xi yi), yi 0 C K’(x2 - y2)1/2 O R = -1 y K'(y cosh z+x sinh z) y 0 D -1 I Hyperz z 0 z + tanh (y/x) C bolic For tanh-1 set x = 1, z = 0 For cosh & sinh, set x=1/K', y=0 e(i) = ln w=2 tanh-1 |(w - 1)/(w + 1)| tanh z = sinh z / cosh z w1/2=[(w+1/4)2 - (w-1/4)2]1/2 -1 -i tanh 2 exp(z) = sinh z + cosh z cosh-1w = ln (w + (1-w2)1/2) w t = exp(t ln w) sinh-1w = ln (w + (1+w2)1/2) In the = -1 case, steps 4, 13, 40, 121, ... , j , 3j + 1, ... must be repeated for the method to converge. These repetitions are incorporated in the constant K' below. xi+1= xi – . di . 2-i . yi {-1,0,1}, di {-1,1} yi+1 = yi + di . 2-i . xi K = 1.646 760 258 121… [Par00], p. 371 zi+1 = zi – di . e(i) K’= 0.828 159 360 960 2… x C O R D I C K'(x cosh z-y sinh z) x [© Oxford U Press] Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan 385 To Probe Further… • Tutorials http://cnmat.cnmat.berkeley.edu/~norbert/cordic/node3.html http://www.execpc.com/~geezer/embed/cordic.htm (including C code) • Papers Survey paper on FPGA implementation of CORDIC algorithms: http://www.andraka.com/files/crdcsrvy.pdf http://www.taygeta.com/cordic_refs.html • Hardware implementations http://www.free-ip.com/cordic/ http://www.stanford.edu/~chet/cordic.html Spring 2006 EE 5324 - VLSI Design II - © Kia Bazargan 386