Post on 04-Mar-2018
Dejan Markovicdejan@ee.ucla.edu
CORDIC, Divider, Square Root
EE219A – Spring 2008Special Topics in Circuits and Signal Processing
Lecture 6
Slide 2EE219A – Spring 2008 Lecture 6
Announcements
This week– Thu, Feb 24, 3-5pm, location 3400 BH
● In-class review discussions
Today’s lecture: CORDIC– Iterative division and square rooting
Slide 3EE219A – Spring 2008 Lecture 6
CORDIC
To do the transformation from
and the inverse, we use the CORDIC algorithm.
θjIr eyjyyty ⇒+=)(
Slide 4EE219A – Spring 2008 Lecture 6
CORDIC -COordinate Rotation DIgital Computer
Use rotations to implement a variety of functions.
yzxyz
xyz
xyz
yxz
xyz
xyzyxz
eyxjyxeg xyj
11
22
tan22
costan
sinhsin
tancos
.1
−−
=
=
=
==
=
=+=
+⇔+−
Slide 5EE219A – Spring 2008 Lecture 6
CORDIC (cont’d)How to do it?Start with general rotation by .
Now the trick is to only do rotations by tan which are a power of 2.
φ
[ ][ ]φφ
φφφφφφ
tancos'tancos'sincos'sincos'
xyyyxxxyyyxx
+=−=+=−=
φ
Slide 6EE219A – Spring 2008 Lecture 6
CORDIC (cont’d)
So to rotate to any arbitrary angle, we do a sequence of rotations to get to that value.
672-60.895°
562-51.790°
452-43.576°
342-37.125°
232-214.036°
122-126.565°
01145°
iKTan θθ
Rotation Number
Slide 7EE219A – Spring 2008 Lecture 6
Basic CORDIC Iteration
[ ][ ]iiiiii
iiiiii
dxyKydyxKx
−+
−+
⋅⋅−=
⋅⋅−=
2)(
2)(
1
1
1211)2cos(tan
2
11
±=+
==−
−−
i
ii
d
K
The di is chosen to rotate by ±φ
If we don’t multiply by Ki we get a gain error which is independent of the direction of the rotation.
The error converges to .61 - May not need to compensate it.
We also can accumulate the rotation angle iiii dZZ −−
+ −= 2tan 11
Slide 8EE219A – Spring 2008 Lecture 6
ExampleOK, let’s do an example:
y0
x0
y0
x0
φ
Initial vector(x02+y0
2)0.5
We want to find φ & 20
20 yx +
Slide 9EE219A – Spring 2008 Lecture 6
Example (cont’d)
1) First, check to see if the angle is positive or negative by checking sign of . If positive, rotate by -45º, if negative, rotate by +45º.
0y
Slide 10EE219A – Spring 2008 Lecture 6
Example (cont’d)2) Check the sign of and rotate positive if negative or rotate
negative if is positive by rotation 1 (26º).1y
1y
Slide 11EE219A – Spring 2008 Lecture 6
Example (cont’d)
3) Repeat 2 for each rotation K
Slide 12EE219A – Spring 2008 Lecture 6
The Gain Factor
)642.1()630.1(
)581.1()414.1(
1
43210
3210
210
10
0
==
==
=
KKKKKKKKK
KKKKK
K
( )58.1
7120
20
3
=+
=
yx
Z Shift & Adds of x0,y0
So we did it Rectangular Polar Coordinates
So, start with ; End up with:00 , yx
Slide 13EE219A – Spring 2008 Lecture 6
The Gain Factor (cont’d)How about the reverse?Start with vector on axis:
x0 φ
φ
==
=
=
00
0
,0 zyAx
eAA j
1,01,0+=>−=<
ii
ii
dzdzρ−
+ ⋅−= 21 iii dZZ
Slide 14EE219A – Spring 2008 Lecture 6
CORDIC Algorithm
Slide 15EE219A – Spring 2008 Lecture 6
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
it = 1
it = 2
it = 3
it = 4it = 5it = 6it = 7it = 8
K(0) = 1.000K(1) = 1.414K(2) = 1.581K(3) = 1.630K(4) = 1.642K(5) = 1.646K(6) = 1.646
Φ = 30.00Φ = -15.00Φ = 11.57Φ = -2.47Φ = 4.65Φ = 1.08Φ = -0.71
∆Φ = -45.00∆Φ = 26.57∆Φ = -14.04∆Φ = 7.13∆Φ = -3.58∆Φ = -1.79
Example 1
Slide 16EE219A – Spring 2008 Lecture 6
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
it = 1
it = 2
it = 3
it = 4it = 5it = 6it = 7it = 8
K(0) = 1.000K(1) = 1.414K(2) = 1.581K(3) = 1.630K(4) = 1.642K(5) = 1.646K(6) = 1.646
Φ = 60.00Φ = 15.00Φ = -11.57Φ = 2.47Φ = -4.65Φ = -1.08Φ = 0.71
∆Φ = -45.00∆Φ = -26.57∆Φ = 14.04∆Φ = -7.13∆Φ = 3.58∆Φ = 1.79
Example 2
Slide 17EE219A – Spring 2008 Lecture 6
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
it = 1it = 2it = 3it = 4it = 5it = 6it = 7it = 8
K(0) = 1.000K(1) = 1.414K(2) = 1.581K(3) = 1.630K(4) = 1.642K(5) = 1.646K(6) = 1.646
Φ = 45.00Φ = 0.00Φ = 0.00Φ = 0.00Φ = 0.00Φ = 0.00Φ = 0.00
∆Φ = -45.00∆Φ = 0.00∆Φ = 0.00∆Φ = 0.00∆Φ = 0.00∆Φ = 0.00
Example 3
Slide 18EE219A – Spring 2008 Lecture 6
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
it = 1
it = 2
it = 3
it = 4it = 5it = 6it = 7it = 8
K(0) = 1.000K(1) = 1.414K(2) = 1.581K(3) = 1.630K(4) = 1.642K(5) = 1.646K(6) = 1.646
Φ = 53.13Φ = 8.13Φ = -18.43Φ = -4.40Φ = 2.73Φ = -0.85Φ = 0.94
∆Φ = -45.00∆Φ = -26.57∆Φ = 14.04∆Φ = 7.13∆Φ = -3.58∆Φ = 1.79
Example 4
Slide 19EE219A – Spring 2008 Lecture 6
Calculating SIN & COS
To calculate SIN & COS, start with , rotate by φ.
sin φ
cos φ
θ
(1/1.64)
y0=sin φ
x0=cos φ
0,64.11
00 == yx
Slide 20EE219A – Spring 2008 Lecture 6
Functions
Slide 21EE219A – Spring 2008 Lecture 6
CORDIC DividerTo do a divide, change CORDIC rotations to a linear function calculator.
Slide 22EE219A – Spring 2008 Lecture 6
Generalized CORDIC
Slide 23EE219A – Spring 2008 Lecture 6
Implementation in an FPGA
[R. Andraka 1998]
Three difference equations directly mapped to hardwareThe decision di is driven by the sign of the y or z register– Vectoring: di = -sign(yi)– Rotation: di = sign(zi)
The initial values loaded via muxesOn each clock cycle– the values from the registers are
passed through shifters and add/sub and the values placed in registers
– The shifters are modified on each iteration to cause the desired shift (state machine)
– Elementary angle stored in ROMLast iteration: results read from reg
Slide 24EE219A – Spring 2008 Lecture 6
Iterative Sqrt and Division
Inputs:– a (14 bits), reset (active high)
Outputs:– zs (16 bits), zd (16 bits) Total: 32 bits
~15k gates
Slide 25EE219A – Spring 2008 Lecture 6
Iterative 1/sqrt()
User defined parameters:- data type- wordlength (#bits, binary pt)- quantization- overflow- latency- sample period
wordlength
latency
xs(k+1) = xs(k) / 2· (3–Z· xs
2(k))
User defined parameters– Wordlength (#bits, binary pt)– Quantization, overflow– Latency, sample period
The choice of initial condition– Determines # iterations– and convergence…
xs
Z
Slide 26EE219A – Spring 2008 Lecture 6
Quadratic Convergence
))(3(2)()1( 2kxNkxkx s
ss ⋅−⋅=+
∞→
→k
s Nkx 1)(
))(2()()1( kxNkxkx ddd ⋅−⋅=+
∞→
→k
d Nkx 1)(
))(3(2)()1( 2kykyky s
ss −⋅=+
∞→→
ks ky 1)(
))(3()(21)1( 2 kekeke sss +⋅⋅−=+
1)()( −= kyke ss
))(2()()1( kykyky ddd −⋅=+
∞→→
kd ky 1)(
2)()1( keke dd −=+
1)()( −= kyke dd
Sqrt Div
Slide 27EE219A – Spring 2008 Lecture 6
-4 -2 0 2 4-4
-2
0
2
4
yd(k)
y d(k+1
)
-4 -2 0 2 4-4
-2
0
2
4
ys(k)
y s(k+1
)
√5 √3
Initial Condition
))(3(2)()1( 2kykyky s
ss −⋅=+ ))(2()()1( kykyky ddd −⋅=+
Sqrt Div
∞→→
ks ky 1)(∞→
→kd ky 1)(
3)0(0 << sy
5)0(3 << sy
5)0( >sy
Convergence:Conv. stripes:Divergence:
2)0(0 << dyConvergence:Divergence: o.w.
Slide 28EE219A – Spring 2008 Lecture 6
1/Sqrt() Formula: Convergence
( )21 32 nn
n xNxx ⋅−⋅=+∞→
→n
n Nx 1
EquilibriumsN1,0 ±
Initial condition– Take:
– Find x0 such that:
– Solution:
21)(
−=N
xxV nn
...)4,3,2,1,0(0)()( 1 =∀<−+ nnxVxV nn
})(:{ 00 axVxS <=
“Level set” V(x0)=a is convergence boundLocal convergence (3 equilibriums)
(Eq.1)
(Eq.2)
(Eq.3)
Slide 29EE219A – Spring 2008 Lecture 6
10-1
100
101
-2
-1.5
-1
-0.5
0
0.5
1
1.5
initial condition x0
f3
N=1/64N=1/16N=1/4N=1N=4N=16
Convergence…
0222
5)1(2
300
20
0 <
−−⋅⋅−⋅N
xNxxNx(Eq.2)
Nx M 3
50 =
Roots:
Nx 11 =
( )17121
3,2 ±−⋅
=N
x
Max:
Slide 30EE219A – Spring 2008 Lecture 6
Initial Condition Circuit
>16
>4
>1
>1/4
>1/16
>1/64 x0=8x0=4x0=2x0=1x0=1/2x0=1/4
N
aN
xaN
+<<−11
0(Eq.3)
Slide 31EE219A – Spring 2008 Lecture 6
Top: Convergence, Bottom: Sampled
Slide 32EE219A – Spring 2008 Lecture 6
Convergence in 8 iterations
Slide 33EE219A – Spring 2008 Lecture 6
Mult by 2 (extended range…)
Slide 34EE219A – Spring 2008 Lecture 6
1/Sqrt() Formula: Convergence
( )21 32 nn
n xNxx ⋅−⋅=+∞→
→n
n Nx 1
Nyx n
n =
( )21 32 nn
n yyy −⋅=+
nn ye −=1
321 2
123
nnn eee ⋅−⋅=+
Error:
∞→→
nny 1
Slide 35EE219A – Spring 2008 Lecture 6
1/Sqrt() Formula: Convergence
∞→→
nny 1
-4 -2 0 2 4-4
-3
-2
-1
0
1
2
3
4
( )21 32 nn
n yyy −⋅=+
35 30 0 << y
53 0 << y
50 >y
⇒ convergence
⇒ conv. stripes
⇒ divergence
Slide 36EE219A – Spring 2008 Lecture 6
Error
-1.5 -1 -0.5 0 0.5 10
1
2
3
4
5
6
en
e n+1
⇒ divergence
⇒ conv. stripes
⇒ convergence
Slide 37EE219A – Spring 2008 Lecture 6
Results…
0 5 10 15 20 25-2.5
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
2.5
iteration
solu
tion
1-1+rt(3)+rt(5)-rt(3)++rt(5)--2.15rt(5)-eps
Slide 38EE219A – Spring 2008 Lecture 6
Results…
2 4 6 8 10 12 14 16 18 20-2.5
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
2.5
iteration
solu
tion
1-1+rt(3)+rt(5)-rt(3)++rt(5)--2.15rt(5)-epsrt(5)+eps
Slide 39EE219A – Spring 2008 Lecture 6
Descending absolute errorSqrt Div
2)1()( −= kk xxE 0)()()( 1 <−= + kkk xExExV k∀
)4()1()1(4
)( 0200
20
00 −+⋅+⋅−⋅= xxxxxxVs )2()1()( 0
2000 −⋅−⋅= xxxxVd
0 1 2 3
-0.2
0
0.2
0.4
0.6
initial condition, x0
sqrt,
V(x
0)
√3
-0.5+√4.25
0 1 2 3
-0.2
0
0.2
0.4
0.6
initial condition, x0
div,
V(x
0)Descending error:
Slide 40EE219A – Spring 2008 Lecture 6
Convergence speed
2 / 12 / 23 / 23 / 3e0: 25%, # iter (sqrt/div)
3 / 24 / 35 / 35 / 4e0: 50%, # iter (sqrt/div)
10%5%1%0.1%Target rel. error (%)
Adaptive algorithm– current result .ic for next iteration
# iterations required for specified accuracy