Fast Arithmetic
Philipp Koehn 27 September 2019
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
Fast Arithmetic Philipp Koehn 27 September 2019 Philipp Koehn - - PowerPoint PPT Presentation
Fast Arithmetic Philipp Koehn 27 September 2019 Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019 1 arithmetic Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019 Addition (Immediate)
Philipp Koehn 27 September 2019
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
1
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
2
li $s0, 2
addi $s1, $s0, 4
addi $s2, $s1, -3
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
3
li $s0, 2
add $s1, $s0, $s5
sub $s2, $s1, $s6
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
4
add, addi, and sub – overflow triggers exceptions – similar to interrupt – register $mfc0 contains address of exception program
addu, addiu, and subu – no overflow handling (as in C programming language)
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
5
→ consult Section 3.2 in Patterson/Hennessy textbook
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
6
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
7
011 +11
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
8
011 +11
1+1 = 0, carry the 1
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
9
011 +11
1+1+1 = 1, carry the 1
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
10
011 +11
copy carry bit
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
11
Carry Lookahead
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
12
1+1 + carry1 = 1, carry 1 1+1 + carry0 = 0, carry 1
0+0 + carry1 = 1, carry 0 0+0 + carry0 = 0, carry 0
0+1 + carry1 = 0, carry 1 0+1 + carry0 = 1, carry 0
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
13
Operand A 0100 1111 Operand B 0110 0001 Generate 0100 0001 Propagate 0110 1111 Carry 1001 111-
ai and bi
ai or bi
?
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
14
– generate: gi = ai and bi – propagate: pi = ai or bi
– c1 = g0 or (p0 and c0) – c2 = g1 or (p1 and g0) or (p1 and p0 and c0) – c3 = g2 or (p2 and g1) or (p2 and p1 and g1) or (p2 and p1 and p0 and c0) – c4 = g3 or (p3 and g2) or (p3 and p2 and g2) or (p3 and p2 and p1 and g1)
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
15
– "super" propagate = P = p0 and p1 and p2 and p3 – "super" generate = g3 or (p3 and g2) or (p3 and p2 and g1)
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
16
compute super propagate Pj and super generate Gj
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
17
– more gates in circuit – faster computation
⇒ speed-up at same clock speed
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
18
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
19
xxxx10101 x 1101
10101 10101
(in decimal: 23x13 = 299)
– shift second operand to right (get last bit) – if carry: add second operand to sum – rotate first operand to left (multiply with binary 10)
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
20
SHIFT LEFT SHIFT RIGHT WRITE WRITE 64 64 32
loop 32 times: if lowest bit of multiplyer=1 add multiplicant to product shift multiplicant left shift multiplyer right
multiplying 32 bit numbers may result in 64 bit product
– 32 iterations – 3 operations each (add + shift + shift) → almost 100 operations
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
21
– add: product – shift left: multiplicant – shift right: multiplyer ⇒ These can be executed in parallel (note: read is executed before write)
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
22
Adder
MULTI- PLICANT SHIFT RIGHT 31 MULTI- PLICANT SHIFT RIGHT 30
Adder
MULTI- PLICANT SHIFT RIGHT 29 MULTI- PLICANT SHIFT RIGHT 28
Adder Adder
MULTI- PLICANT SHIFT RIGHT 3 MULTI- PLICANT SHIFT RIGHT 2
Adder
MULTI- PLICANT SHIFT RIGHT 1 MULTI- PLICANT
Adder
Adder
PRODUCT
AND AND AND AND AND AND AND AND
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
23
– hi: high word – lo: low word
mult $s1, $s2 mflo $s0
mul $s0, $s1, $s2 More on that later
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
24
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
25
xxxx1011 / 10 = 10 1 01 011 10 1 Remainder 1
yes → add result bit 1, carry out subtraction no → add result bit 0
not possible → done, note remainder
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
26
→ shift to maximum left
use 64 register, push 32 positions
yes → add result bit 1, carry out subtraction no → add result bit 0
not possible → done, note remainder
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
27
– shift divisor – subtraction – indication if subtraction should be accepted
iterations cannot be parallelized the same way (sophisticated prediction methods guess outcome of subtractions)
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019
28
– hi: remainder – lo: quotient
div $s1, $s2 mflo $s0
Philipp Koehn Computer Systems Fundamental: Fast Arithmetic 27 September 2019