CSE 575 Computer Arithmetic Spring 2003 Mary Jane Irwin (www. cse. psu CSE 575 Computer Arithmetic Spring 2003 Mary Jane Irwin (www.cse.psu.edu/~mji)
Cordic Algorithms A convergence method for evaluating trigonometric (and other) functions if a unit-length vector with end point at (X,Y) = (1,0) is rotated by an angle Z, its new end point will be at (X,Y) = (cos Z, sin Z) simple hardware - shifters, adders, lookup table COordinate Rotations DIgital Computer Family of algorithms: rotation, vector mode circular rotations linear rotations hyperbolic rotations
Real Cordic Rotations If vector OEi is rotated about the origin by an angle i, the new vector OEi+1 will have the coordinates Ei+1 (Xi+1, Yi+1) Ei Ri+1 Real rotation: Ei+1 (Xi, Yi) i Xi+1 = Xi cos i - Yi sin i Yi+1 = Yi cos i + Xi sin i Zi+1 = Zi - i Ri O The problem is that the iterations are not simple operations - have two multiply operations in each = 4 multipliers, plus have to store a lot of constants (cos’s alpha i’s and sin’s of alpha i’s) The variable Z allows us to keep track of the total rotation over several steps. If Z0 is the initial rotation goal and if the i angles are selected at each step such that after n iterations Zn tends to 0, then En will be the end point after rotation by angle Z0
Pseudo Cordic Rotations Pseudo rotations increase the vector length to Ri+1 = Ri(1 + tan2i)1/2 E’i+1 (Xi+1, Yi+1) Ri+1 Pseudo rotation: E’i+1 Ei (Xi, Yi) Xi+1 = Xi - Yi tan i Yi+1 = Yi + Xi tan i Zi+1 = Zi - i i Ri O Now have simpler operations - at first glance looks like two multipliers and a table with the tan alpha values. The R term simplifies since the expansion factor is a constant for a constant set of angles - that is the real key. So, if we have a constant set of angles, we know the tan alpha values to store (and they are a relatively small set). Additionally, if we choose the alpha’s correctly, we can simplify the multiplications to shift (and add) operations! (a + tan2a(i))1/2 = 1/cos a(i) Hi, I downloaded your CSE 757 CORDIC PPS-turned-PDF as guided by Google. It helped me understand CORDIC better. However, there is an error on slide 4, the squared term within the expansion faction K should be alpha[i]^2 without calculating any tangent. Ernesto (ernestox@blackwater.dynip.com) Expansion factor K = (1 + tan2 i)1/2 depends on the rotation angles 1 ,2 …, n. If we always rotate by the same angles, K is a constant.
Basic Cordic Rotations To simplify pseudo rotations, pick i such that tan i = di 2-i where di {-1,1}. Then Xi+1 = Xi - di Yi 2-i Yi+1 = Yi + di Xi 2-i Zi+1 = Zi - di tan-12-i Computation of Xi+1 and Yi+1 requires an i-bit right shift and an add/subtract; Zi+1 only requires an add/subtract and one table lookup Precompute and store the function tan-12-i
Choosing the Angles i i = 2-i Ei=tan-12-i di Zi - diEi = Zi+1 1.000 000 45.000000 1 30.00 – 45.00 = -15.00 0.500000 26.565051 -1 -15.00 + 26.57 = 11.57 2 0.250000 14.036243 11.57 – 14.04 = -2.47 3 0.125000 7.125016 -2.47 + 7.13 = 4.66 4 0.062500 3.576334 4.66 – 3.58 = 1.08 5 0.031250 1.789910 1.08 -1.79 = -0.71 6 0.015625 0.895174 -0.71 + 0.90 = 0.19 7 0.007813 0.447614 0.19 - 0.45 = -0.26 8 0.003906 0.223811 -0.26 + 0.22 = -0.04 9 0.001953 0.111906 -0.04 + 0.11 = 0.07 Zi+1 zero
Rotating the Angles Illustration of the first three rotations for a Z of 30 30 O (X0, Y0) for class handout
Rotating the Angles Illustration of the first three rotations for a Z of 30 30 O (X0, Y0) -45 (X1, Y1) (X2, Y2) +26.6 (X3, Y3) -14 for lecture
Circular Rotation Mode Choosing di = sign(Zi) {-1,1} to force Z to 0 gives the rotation mode Cordic iterations Xi+1 = Xi - di Yi 2-i Yi+1 = Yi + di Xi 2-i Zi+1 = Zi - di Ei where Ei = tan-12-i After n iterations, when Zn is sufficiently close to 0, then we have Z = i and Xn = K(X cos Z - Y sin Z) where K = 1.646 760 258 … Yn = K(Y cos Z + X sin Z) Zn = 0 Rule: Choose di {-1,1} such that Z 0 Computes cos Z and sin Z by starting with X = 1/K = 0.607 252 935 … and Y = 0 For k bits of precision, run it k iterations since for large i > k, tan-12-i 2-i
Convergence Domain Convergence of Z to 0 happens because each angle is more than half the previous angle. The domain of convergence is 0 Z 99.7 which is the sum of all the angles given in slide 6 Outside this range, we can use trig identities to convert to the range cos (Z 2j) = cos Z sin (Z 2j) = sin Z cos (Z - ) = - cos Z sin (Z - ) = - sin Z transformations are particularly easy if angles are represented and manipulated in multiples of pi radians E.g., z = 0.2pi radian or 36degrees gives a domain of convergences of -1/2 to +1/2
Rotation Example Computing cos 30 (= 0.866 025) and sin 30 (=0.500 000) i di Xi+1 cos Yi+1 sin Zi+1 0 1/K = 0.607 253 0.000 000 30.000 000 1 0.607 253 -15.000 000 -1 0.910 880 0.303 627 11.565 051 2 0.834 973 0.531 347 -2.471 192 3 0.901 391 0.426 975 4.653 824 4 0.874 705 0.483 312 1.077 490 5 0.859 602 0.510 647 -0.712 420 6 0.867 581 0.497 216 0.182 754 7 0.863 697 0.503 994 -0.264 860 8 0.865 666 0.500 620 -0.041 049 . . .
Circular Vector Mode Choosing di = - sign(Xi Yi) {-1,1} to force Y to 0 gives the vector mode Cordic iterations Xi+1 = Xi - di Yi 2-i Yi+1 = Yi + di Xi 2-i Zi+1 = Zi - di Ei where Ei = tan-12-i After n iterations, when Yn is sufficiently close to 0, then we have tan (i) = -Y/Z Xn = K (X2 + Y2) where K = 1.646 760 258 … Yn = 0 Zn = Z + tan-1 (Y/X) Rule: Choose di {-1,1} such that Y 0 Computes tan-1 Y by starting with X = 1 and Z = 0 For k bits of precision, run it k iterations since for large i > k, tan-12-i 2-i
Vector Example Computing tan-1 0.414 210 (= 22.499 830) di Xi+1 K(X2 + Y2) Yi+1 0 Zi+1 tan-1 1.000 000 0.414 210 0.000 000 -1 1.414 210 -0.585 790 45.000 000 1 1.707 105 0.121 315 18.434 949 2 1.737 434 -0.305 461 32.471 192 3 1.775 617 -0.088 282 25.346 176 4 1.781 135 0.022 694 21.769 842 5 1.781 844 -0.032 966 23.559 752 6 1.782 359 -0.005 125 22.664 578 7 1.782 399 0.008 800 22.216 964 8 1.782 433 0.000 184 22.440 775 . . . Need to check answers for bugs !!!
Circular Cordic Hardware X shift Xi+1 = Xi – di Yi 2-i i counter shift Yi+1 = Yi – di Xi 2-i Y lookup table stores values of tan-1 2-i Z lookup table Zi+1 = Zi – di Ei values of Ei= tan-12-i di control
Generalized Cordic Generalized Cordic defined as Xi+1 = Xi - di Yi 2-i Yi+1 = Yi + di Xi 2-i Zi+1 = Zi - di Ei Defining and Ei gives = 1 Circular rotations (basic) Ei = tan-12-i = 0 Linear rotations Ei = 2-i = -1 Hyperbolic rotations Ei = tanh-12-i
Generalized Rotations 2(area EOF) angle EOF = --------------- (OW)2 2(area AOB) angle AOB = -------------- (OU)2 U A B = 1 W E F = -1 O = 0 V vector OV
Enhanced Cordic Hardware X X Xi+1 = Xi – di Yi 2-i shift i counter shift Yi+1 = Yi + di Xi 2-i Y Y Zi+1 = Zi - di Ei Z Z Ei lookup table (tan-12-i, 2-i, tanh-12-i) di control
Circular Mode ( = 1) Circular vector mode Circular rotation mode Xn = K(X cos Z - Y sin Z) Yn = K(Y cos Z + X sin Z) Zn = 0 Rule: Choose di {-1,1} such that Z 0 Circular vector mode Xn = K(X2 + Y2)1/2 Yn = 0 Zn = Z + tan-1 (Y/X) Rule: Choose di {-1,1} such that Y 0 Computes functions cos Z and sin Z X = 1/K and Y = 0 Computes function tan-1 Y X = 1 and Z = 0
Linear Mode ( = 0) Linear vector mode Linear rotation mode Xn = X Yn = Y + X * Z Zn = 0 Rule: Choose di {-1,1} such that Z 0 Linear vector mode Yn = 0 Zn = Z + Y/X Rule: Choose di {-1,1} such that Y 0 Computes functions multiply and multiply-add Y = 0 End point of the vector is kept on the line x = x(0) and the vector length is defined by R(i) = x(i) Hence the true length of OV of the scaling factor is 1 (pseudorotations are true linear rotations) Converges trivially for suitably restricted values of z (rotation) or y (vector) Computes functions divide and divide-add Z = 0
Hyperbolic Mode ( = -1) Hyperbolic vector mode Hyperbolic rotation mode Xn = K’(X cosh Z + Y sinh Z) Yn = K’(Y cosh Z + X sinh Z) Zn = 0 Rule: Choose di {-1,1} such that Z 0 K’ = 0.828 159 360 … Hyperbolic vector mode Xn = K’(X2 - Y2)1/2 Yn = 0 Zn = Z + tanh-1 (Y/X) Rule: Choose di {-1,1} such that Y 0 Computes functions hyperbolic sin and cos X = 1/K’ and Y = 0 Vector length actually shrinks because cosh a(i) > 1 More difficult convergence guarantee – for details see book section 22.4 By repeating iterations 4, 13, 40, 121, …, j, 3j+1 (K’ has been computed to take these extra iterations into account) then convergence is guaranteed for |z| < 1.13 (rotation) or |y| < 0.81 (vector) Computes functions hyperbolic tanh-1 y X = 1 and Z = 0
Cordic Computation Unit Rotation mode: di=sign(Zi); Zi0 Vector mode: di= -sign(Xi Yi); Yi0 Ciclr = 1 Ei = tan-12-i X K(X cosZ – Y sinZ) Y K(Y cosZ + X sinZ) Z 0 For cos & sin, set X=1/K, Y=0 tanZ=sinZ/cosZ X K (X2 + Y2) Y 0 Z Z + tan-1(Y/X) For tan-1, set X=1, Z=0 cos-1W=tan-1[(1-W2)/W];sin-1W=tan-1[W/(1-W2)] Linear = 0 Ei = 2-i X X Y Y + X*Z For multiply, set Y=0 X X Y 0 Z Z + Y/X For divide, set Z=0 Hprblc = -1 tanh-1 2-i X K’(X coshZ – Y sinhZ) Y K’(Y coshZ + X sinhZ) For cosh & sinh, set X=1/K’, Y=0 tanhZ=sinhZ/coshZ; Wt=Et lnW Ez=sinhZ+coshZ X K’ (X2 - Y2) Y 0 Z Z + tanh-1(Y/X) For tanh-1, set X=1, Z=0 cosh-1W=ln[W+(1-W2)];sinh-1W=ln[W+(1+W2)] lnW=2tanh-1|(W-1)/(W+1)|;W=((W+¼)2-(W-¼)2) In executing the iterations for = -1, steps 4, 13, 40, 121, …, j, 3j+1, … must be repeated. CORDIC CORDIC CORDIC CORDIC CORDIC CORDIC
Cordic Summary Can compute virtually all trig functions of common interest When the number of iterations is fixed, K and K’ are constants Thus, we can not stop computing if Z (or Y) becomes 0 (except when = 0); we always need k iterations for k digits of precision Cordic can be extended to higher radices (e.g., for base 4, di{-2,-1,1,2} and the number of iterations will be cut in half with essentially the same hardware)
Key References Duprat and Muller, The CORDIC algorithm: New results for fast VLSI implementation, IEEE Trans. on Computers, 42(2):168-178, 1993. Parhami, Computer Arithmetic, Oxford Univ. Press, 1999. Phatak, Double step branching CORDIC, IEEE Trans. on Computers, 47(5)587-603, May 1998. Vachss, The CORDIC magnification function, IEEE Micro, 7(5):83-83, Oct. 1987. Volder, The CORDIC trigonometric computing technique, IRE Trans. Elecronic Computers, Vol 8, pp. 330-335, Sept. 1959. Walther, A unified algorithm for elementary functions, Proc. Spring Joint Computer Conf, pp. 379-385, 1971.