Mnożenie / Multiplication

Download Report

Transcript Mnożenie / Multiplication

Hardware Implementation of Algorithms
Sprzętowa Implementacja Algorytmów
Układy mnożące, konwolwery
Multipliers, convolvers
Ernest Jamro
Kat. Elektroniki AGH, Kraków
Dep. Of Electronics, AGH
Mnożenie / Multiplication
1
0
0
1
1
0
1
1
1
0
0
1
1
0
0
1
0
0
0
0
1
0
0
1
1
1
0
0
X
+
0
1
9 x 11= 99
1
2
Parallel Array Multipliers
Mnożenie równoległe
sl-1
&
+
ck
ai 2ck+sl=
=sl-1+(aibj)+ck-1
bj
ck-1
p7
p6
a2
a1
&
&
&
&
+
&
+
&
+
&
+
&
+
&
+
&
+
&
+
&
+
&
+
&
+
sl
&
+
a3
p5
p4
a0
&
b0
b1
b2
b3
p3
p2
p1
p0
3
FPGA, Built-in multiplier
DSP48
4
Sequential Multiplier /Mnożenie sekwencyjne
Register / Rejestr
A3
A2
A1
A0
B3
B2
PISO
B1
B0
Pn,3
Pn,2
Pn,1
Pn,0
FA
FA
FA
HA
FF
FF
FF
FF
Sumator
Adder
FF
FF
FF
Rejestr (Przesuwny)
(Shift) Register
FF
5
Wallace Tree Multiplier
(with Carry Save Adders)
W układach FPGA nie zaleca się stosowania CSA
In FPGA the CSA are not recommended
6
Mnożenie ze znakiem /
Multiplication of Sign numbers
Znak, Moduł
/ Sign-Module
Standardowe mnożenie liczb dodatnich /
Standard unsigned multiplication
Znak= Znak1 XOR Znak2
Sign= Sign1 xor Sign2
W kodzie uzupełnień do dwóch
Two’s Complement
a  2
N 1
N 2
 a N   2i  ai
(a1+a2)*(b1+b2)= a1b1+ a1b2+a2b1+a2b2
i 0
a b  2
2 N  2
 a N 1bN 1  2
N 1
N 2
 2  aibN 1 2
i
N 1
i 0
N 2
N 2
N 2
N 2
i
2

b
a

(
2

a
)

(
2
 i N 1  i   bi )
i
i
i 0
i 0
i 0
N 2
  2  ai bN   2i  ai bN  1
i
i 0
a b  2
2N 2
 aN bN  2  2
N
i 0
N 1
N 2
2 b a
i
i 0
i
N 1
2
N 1
N 2
2 b
i
i 0
N 2
N 2
a  (  2  ai )  (  2i  bi )
N 1 i
i
i 0
i 0
C. R. Baugh and B. A.Wooley, “A two’s complement parallel array multiplication
7
algorithm,” IEEE Trans. Comput., vol. C-22, pp. 1045–1047, Dec. 1973.
Mnożenie w kodzie uzupełnień do 2 / Two’s complement multiplication
sl-1
!&
+
ck
ai
p7
p6
a2
a1
!&
&
&
!&
+
&
+
&
+
&
+
!&
+
&
+
&
+
&
+
!&
+
!&
+
!&
+
bj
ck-1
sl
sl-1+(aibj)+ck-1=
=2ck+sl
&
+
a3
p5
a0
&
b0
1
p4
b1
b2
b3
p3
p2
p1
p0
8
Układ mnożący o zredukowanej szerokości / Reduced-width multiplier
sl-1
&
+
ck
truncation
line
ai
bj
ck-1
a3
a2
&
&
&
&
+
sl
sl-1+(aibj)+ck-1=
=2ck+sl
&
+
&
+
&
+
&
+
&
+
&
+
&
+
&
+
&
+
&
+
&
+
p7
p6
p5
p4
a1
a0
&
b0
b1
b2
b3
p3
p2
p1
p0
9
Kompensacja błędu redukcji / Truncation error compensation
sl-1
&
+
ck
a3
ai
&
bj
ck-1
sl
sl-1+(aibj)+ck-1=
=2ck+sl
a2
&
b0
a1
&
+
&
+
&
b1
a0
&
+
p7
p6
&
+
&
+
&
+
&
+
&
+
&
+
p5
p4
&
p3
b2
10
Mnożenie przez stały współczynnik / Constant Coefficient Multiplier
Look Up Table (LUT)
Address
LUT
Data
Example: Y= 5*X
Address
Data
0
0
1
5
2
10
3
15 ...
11
LUT-based Multiplier
Constant Coefficient: C
Y = CA = CA(0:3) + 24 CA(4:7)
Input
8
4
4
LUT
B
LUT
A
12
12
8
Adder
4
12
16
12
output
Different ROM sizes
Input data width = 6 bits
in
a)
in
b)
6
in
c)
6
6
4
2
4
5
Mem
161
Mem
161
Mem
161
Mem
321
Adder
Adder
Adder
out
out
out
13
Heteregenous memory usage
Virtex: 161, 321, 4k1, 2k2, 1k4, 5128, 25616
Input data and coefficient width= 14
7
14
7
7
5
25616
321
16
1
4
3161
3
1
7
21
5
25616
1
321
16
21
1
4
1
3161
21
3
1
21
14
7
Adder
21
28
14
Exchange distributed RAM to BRAM
BRAM CLB
7
we
14
7
14
7
4
LUT
161
4
LUT
21
LUT
161
4
LUT
21
LUT
161
5
4
1
7
LUT
21
LUT
161
LUT
21
25616
321
16
1
3161
3
21
25616
321
1
16
1
21
4
3161
3
21
+
14
7
+
wy
5
1
2
15
21
28
21
1
Area [CLB]
for different input and coeffitinent width K
12
Equvalent cost of 1 BRAM
10
Only CLB, scale 1:10
8
6
4
# of BRAM
2
0
4
6
8
10
12
14
16
18
20
22
24
16
MM (Multiplierless Multiplication)
Mnożenie bezmnożne
• Binary Representation, example B= 14= 11102
M= AB= (A<<1)+(A<<2)+(A<<3)
• Sub-structure Sharing (SS) example B= 27= 110112
tmp= A + (A<<1)
M= AB= tmp + (tmp<<3)
• Canonic Sign Digit (CSD)
set {0, 1, -1} (0 – no operation, 1 – addition, -1 (1) – subtraction)
example: B= 7 = 1112
B= 1001CSD
M=B·A= (A<<2) + (A<<1) + A
M= (A<<3)-A
17
BINARNIE  CSD
insert symbol ‘1’ only if the total number of operation is reduced
Coefficient
3
7
11
23
Binary (TC)
11
111
1011
10111
CSD
101
1001
101 01
101 001
MCSD
11
1
100
1011
1
1100
Start
Standard
Modified
i=0
carry= false
Start
(bi=1 and carry)
or
(bi=0 and not carry)
N
Y
j=i+1
i=0, c0=0
bn=bn-1
ci+1= bi+1bi  bici  bi+1ci
di= bi+ci-2ci+1
i= i+1
Y
j w
Y
0Q(i,j)<2
di=0
N
N
Y
Q(i,j)<2
and not
(Y<0 and j=w)
(sign bit)
di= 1
carry= false
N
di= -1
carry= true
i= i+1
N
N
i<n
Y
Y
Y
N
carry and B>0
18
di= 1
Stop
i w
Stop
Applience of different techniques of MM
100%
90%
80%
70%
60%
CSD-SS
50%
SS
CSD
BR
40%
30%
20%
10%
0%
3
4
5
6
7
8
K
9
10
11
12
19
The MM cost for different coefficients
18
16
14
CLBs
12
10
8
6
4
2
0
0
50
100
150
coeff
200
250
20
Filters FIR
N 1
y (i )   h(k )  x(i  k )
k 0
Układ opóźniający / Delay Module
x(i)
x(i)
x(i-1)
x(i-N+1)
y(i)
Układ arytmetyczny / Arithmetic Module
ay+2,x+2
Input
w2
-1
z
w1
ay+2,x+1
-1
z
w0
ay+2,x
+
Output
21
Filter FIR (sposób pośredni/ transposed)
N 1
y (i )   h(k )  x(i  k )
k 0
Układy mnożące / Arithmetic module
x(i)
x(i)
h(0)
x(i-N+1)
h(N-1)
x(i-1)
h(1)
Układ opóźniający
Delay
Input
h(0)
z-1
h(1)
+
y(i)
h(2)
Output
-1
z
+
-1
z
+
22
FIR 2D
ay+2,x+2
Input
w2,2
Line Buf.
z-1
ay+2,x+1
w2,1
ay+1,x+2
w1,2
z-1
z-1
ay+2,x
w2,0
ay+1,x+1
w1,1
z-1
ay+1,x
w1,0
+
ay,x+2
Line Buf.
w0,2
z-1
w0,1
ay,x+1
z-1
by+1,x+1
Output
ay,x
w0,0
23
Examples of 2D FIR Filters
1
2
1
-1
-2
-1
1
1
1
2
4
2
0
0
0
1
-8
1
1
2
1
1
2
1
1
1
1
Low-Pass
Sobel
Laplace
24
FIR Filter N=2
LUT-based multipliers
In
z-1
8
4
4
4
4
LUT
M0
LUT
L0
LUT
M1
LUT
L1
112 1 12
2 2
Adder1
Adder1
112
112
2
2
4
Adder0
Adder0
4
113
13
3
9
Adder2
Multiplier 1
Multiplier
4
Adders Block
14
18
18
2
25
FIR, Arytmetyka w innej kolejności
(Parallel) Distributed Arithmetic
N 1
N 1
L 1
h a  h 2
i 0
i
input
coefficient
i
i 0
i
j 0
L 1
N 1
j
 ai , j
different bits of
the input
  2   hi  ai , j
j
j 0
i 0
Arytmetyka Rozproszona (Distributed Arithmetic)
L 1
N 1
j 0
i 0
j
2
   hi  ai, j
a0,0 a1,0
... aN-1,0
N 1
h a
i 0
WDAC
i
i,0
S0
a0,1 a1,1
... aN-1,1
N 1
h a
i 0
i
i ,1
S1<<1
a0,L-1 a1,L-1 ... aN-1,L-1
...
N 1
h a
i 0
i
i , L 1
SL-1<<(L-1)
L 1
j
2
 Sj
j 0
WDAC=K+log2(N+1)
The same input bit weight
(smaller LUT widths)
WLC= K+WIN
27
Filtry FIR z liniową fazą / Linear Phase Filters
(symetryczne/ symmetric: h(0)=h(N-1), h(1)=h(N-2), ...)
28
FPGA, Built-in multiplier
DSP48
29
Example of sub-structure sharing
for FIR filters
H(z)= 5 + 13z-1 + 5z-2 = 1012 + 11012z-1 + 1012z-2
Example 1:
A= 5 = 1012- temporary expression
H(z)= A + (1000 + A)z-1 + Az-2
Example 2:
A= 1 + z-1
H(z)= 5A + 8z-1 + 5z-2
30
Materiały dodatkowe
The END
31
Szybkie mnożenie w układach FPGA
27a7
b
2 a6
b
6
25a5
b
2 a4
b
4
2 a3
b
26·(2·a7 ·b + a6 ·b)
AND
+
+
AND
+
+
3
2 a2
b
2
21a1
b
2 a0
b
0
AND
+
+
AND
+
Ewentualne
potokowe
rejestry
32
Układy mnożące w FPGA
Przykład:
(a7 and bi) xor (a6 and bi+1)
G4 - a7
G3 - bi
G2 - a6
G1 - bi+1
F4 – a7
F3 – bi-1
F2 – a6
F1 – bi
Fragment of Virtex Configurable Logic Block (CLB)
33