Title: COE 308: Computer Architecture (T041) Dr. Marwan Abu-Amara
1COE 308 Computer Architecture (T041)Dr. Marwan
Abu-Amara
- Integer Floating-Point Arithmetic
- (Appendix A, Computer Architecture A
Quantitative Approach, J. Hennessy D.
Patterson, 1st Edition, 1990)
2Basic Techniques of Integer Arithmetic
- Ripple-Carry Addition
- Half Adder Takes 2 inputs ai bi and produces a
sum bit, si, and a carry bit, ci1, as output - Mathematically si (ai bi) mod 2
- ci1 ?(ai bi) / 2?
- Logically
- ci1 aibi
3Basic Techniques of Integer Arithmetic (cont.)
- Ripple-Carry Addition
- Full Adder Takes 3 inputs ai, bi, ci and
produces a sum bit, si, and a carry bit, ci1, as
output - Logically
- ci1 aibi aici bici
- Problem in building an n-bit adder is propagating
the carries - Ripple-carry adder uses n full adders to build an
n-bit adder - Delay of ci1 is 2 levels of logic ? 2n logic
levels in an n-bit adder (i.e. O(n) to generate
final result) - Can use n-bit adder to get AB by feeding A into
A input inverse of B into B input, and set
c0 to 1
4Basic Techniques of Integer Arithmetic (cont.)
5Radix-2 Multiplication Division
- Multiplication 2 unsigned numbers, A B
(an-1an-2 a1a0 bn-1bn-2 b1b0). Register P
(i.e. product) is initially set to 0. Algorithm - Repeat n times
- If least significant bit of A is 1 ? P P B
- Otherwise ? P P 0
- Shift the register pair P A to right by 1 bit
such that low-order bit of P is moved into As
high-order bit, and low-order bit of A is shifted
out - Final result is in the register pair P A with P
having the high order bits
6Radix-2 Multiplication Division
- Restoring Division 2 unsigned numbers, A B
(an-1an-2 a1a0 bn-1bn-2 b1b0). Register P
is initially set to 0. Algorithm - Repeat n times
- Shift register pair P A one bit left (with
high-order bit of A moved into Ps low-order bit) - Subtract B from P (i.e. P P B)
- If result of step 2 lt 0 (i.e. ()ve) ? Set
low-order bit of A to 0 - Otherwise ? Set low-order bit of A to 1
- If result of step 2 lt 0 (i.e. ()ve) ? P P B
- Final result A quotient, P remainder
7Radix-2 Multiplication Division
8Example of Division
- Divide 14 by 3 ? A 14 11102 B 3 00112
- Iteration Step P A Iteration Step
P A . - Initial. 0 0000 1110
- 1 1 0 0001 110_ 4 1 0 0010 010_
- 2 -0 0011 2 -0 0011
- 3 -0 0010 1100 3 -0 0001 0100
- (add B to P) 4 0 0001 1100 (add B to P) 4
0 0010 0100 - 2 1 0 0011 100_
- 2 -0 0011 ? Quotient A 01002
- 3 0 0000 1001 4
- 4 0 0000 1001 Remainder P 00102
- 3 1 0 0001 001_ 2
- 2 -0 0011
- 3 -0 0010 0010
- (add B to P) 4 0 0001 0010
9Radix-2 Multiplication Division
- Non-Restoring Division 2 unsigned numbers, A B
(an-1an-2 a1a0 bn-1bn-2 b1b0). Register P
is initially set to 0. Algorithm - Repeat n times
- If P is ()ve
- Shift register pair P A one bit left
- P P B
- Else (i.e. If P is not ()ve)
- Shift register pair P A one bit left
- P P B
- If P is ()ve ? Set low-order bit of A to 0
- Otherwise ? Set low-order bit of A to 1
- Final result A quotient, P remainder
10Signed Numbers
- 4 methods
- Sign magnitude
- 1s complement
- 2s complement (most widely used)
- Biased A fixed bias is picked so that sum of
bias represented is always gt 0 (used in
floating-point) - Example Represent 3 in (1) sign magnitude, (2)
1s complement, (3) 2s complement - 3 00112 ? (1) sign magnitude 00112 10112
- ? (2) 1s complement 11002
- ? (3) 2s complement 11012
11Signed Numbers (cont.)
- Overflow in unsigned s when theres a carry-out
of MSB - Overflow in 2s complement when carry-in to MSB
is different from carry-out of MSB - Example
- 5 (7) ? 10 ? different
- 1011 (5) ? overflow
- 1001 (7)
- 0100
- 5 (7) ? 00 ? same
- 0101 (5) ? no overflow
- 1001 (7)
- 1110
12Radix-2 Multiplication Division
- Signed Numbers Multiplication To perform 2s
complement multiplication, use same algorithm as
before but with the following modifications - At the ith multiply step, LSB of A is ai, and
- for 1st step (i.e. when i 0), take ai-1 to be
0 - Shift P arithmetically (i.e. copy sign bit) 1 bit
to right
If ai ai-1 then
0 0 Add 0 to P
0 1 Add B to P
1 0 Sub B from P
1 1 Add 0 to P
13Example 1 of Multiplication
- Multiply -6 by -5 ? A -6 a3a2a1a0 10102 B
-5 10112 - Iteration Step P A ai-1
- 0000 1010 0
- 0 1 0000
- 0 0000 1010
- 2 0000 0101 0
- -B 1 0101
- 1 0101 0101
- 2 0010 1010 1
- B 1 1011
- 2 1101 1010
- 2 1110 1101 0
- -B 1 0101
- 3 0011 1101
- 2 0001 1110 1 ? Product P A 30
14Example 2 of Multiplication
- Multiply -6 by 5 ? A -6 a3a2a1a0 10102 B
5 01012 - Iteration Step P A ai-1
- 0000 1010 0
- 0 1 0000
- 0 0000 1010
- 2 0000 0101 0
- -B 1 1011
- 1 1011 0101
- 2 1101 1010 1
- B 1 0101
- 2 0010 1010
- 2 0001 0101 0
- -B 1 1011
- 3 1100 0101
- 2 1110 0010 1 ? Product P A -30
15Floating Point
- A floating-point number (FP ) is divided into 2
parts - Exponent
- Significand (or Mantissa)
- FP significand ? baseexponent (e.g. exponent
-2 significand 1.5 ? FP 1.5 ? 2-2
0.375) - Single-precision is represented using 32 bits
- 1 for sign
- 8 for exponent
- 23 for fraction
- Exponent is a signed represented using the bias
method with a bias of 127 - Significand Mantissa 1 fraction
- Thus, if e value of exponent field, and f
value of fraction field, then FP represented is
1.f ? 2e127
16Floating Point (cont.)
- Example What single-precision FP does the
following 32-bit word represent? 110000001010000 - 1 10000001 010000 ?
- sign 1 ve
- exponent field e 100000012 129 (?
exponent 129127 2) - fraction field f .0100002 0.012 0.25
- ? FP 1.f ? 2e127 1.25 ? 2129127 1.25
? 4 5 - Range of exponent field (i.e. e) is from 1 to 254
(i.e. exponent is from 126 to 127) - e 0 or 255 are used to represent special values
17Floating Point (cont.)
e f FP Represented Comment
255 0 ?
255 ? 0 NaN Not a Number
0 0 0
0 ? 0 denormal (or subnormal ) Deals with very small values
18Floating Point Addition
- What is the sum of 1,234,823.333 .0011?
- Need to line up the decimal points first
- This is the same as shifting the significand
while changing the exponents - 1,234,823.333 1.234823333 ? 106
- .0011 1.1 ? 10-3 0.0000000011 ? 106
- Add significands (using integer addition)
- Significand sum 1.234823333 0.0000000011
- 1.2348233341
- Normalize the result, if needed
- Result 1.2348233341 ? 106
19Floating Point Addition (cont.)
- Binary FP Addition Algorithm
- Similar to decimal FP addition method
- Let ei exponent, si significand (i.e. 1 fi
24 bits), then steps of algorithm are - If e1 lt e2, swap the operands, calculate d e1
e2 (note that d ? 0), and set exponent of result
to e1 - Shift s2 by d places to the right
- Add s1 result of step 2, and store result in s1
- Normalize
- If result of step 3 (i.e. s1) ? 2 ? Shift s1 by 1
place right add 1 to exponent - If s1 lt 1 ? Shift s1 to left until leftmost
binary digit is 1 subtract of shifts from
exponent - If s1 0 ? Load special zero pattern into
exponent - Otherwise, do nothing (i.e. done)