Computer Architecture
Lecture 8.
. Computer Arithmetic.
Instructors
Elena Boldyreva, Associate Professor eaboldyreva@itmo.ru
E-mail for questions itmo-hdu-np@yandex.ru
GOALS OF THIS LECTURE: PLAN:
1. get terminology about 1. Arithmetic & Logic Unit
computer arithmetic 2. Integer Representation
2. more details about the 3. Addition and Subtraction
realization (how the 4. Integer Arithmetic
arithmetic is linked with the 5. Floating-Point Representation
hardware) 6. Floating-Point Arithmetic
©. Stallings, William. Computer organization and architecture : designing for performance 2
What is Computer Arithmetic?
Pentium Division Bug (1994-95): Pentium’s radix-4 SRT algorithm occasionally gave incorrect
quotient
First noted in 1994 by Tom Nicely who computed sums of reciprocals of twin primes:
1/5 + 1/7 + 1/11 + 1/13 + . . . + 1/p + 1/(p + 2) + . . .
Worst-case example of division error in Pentium:
4 195 835 1.333 820 44... Correct quotient
c = =
3 145 727 1.333 739 06... circa 1994 Pentium
double FLP value;
accurate to only 14 bits
(worse than single!)
3
Aspects of, and Topics in, Computer
Arithmetic
Hardware (our focus in this lecture) Software
––––––––––––––––––––––––––––––––––––––––––––––––– ––––––––––––––––––––––––––––––––––––
Design of efficient digital circuits for Numerical methods for solving
primitive and other arithmetic operations systems of linear equations,
such as +, –, , , , log, sin, and cos partial differential eq’ns, and so on
Issues: Algorithms Issues: Algorithms
Error analysis Error analysis
Speed/cost trade-offs Computational complexity
Hardware implementation Programming
Testing, verification Testing, verification
General-purpose Special-purpose
–––––––––––––––––––––– –––––––––––––––––––––––
Flexible data paths Tailored to application
Fast primitive areas such as:
operations like Digital filtering
+, –, , , Image processing
Benchmarking Radar tracking
4
Number Systems
– ALU does calculations with binary numbers
• Decimal number system
– Uses 10 digits (0,1,2,3,4,5,6,7,8,9)
– In decimal system, a number 84, e.g.,
means
84 = (8x10) + 3
– 4728 = (4x1000)+(7x100)+(2x10)+8
– Base or radix of 10: each digit in the
number is multiplied by 10 raised to a
power corresponding to that digit’s
position
– E.g. 83 = (8x101)+ (3x100)
– 4728 = (4x103)+(7x102)+(2x101)+(8x100)
5
Decimal number system…
• Fractional values, e.g.
– 472.83=(4x102)+(7x101)+(2x100)+(8x10-1)+(3x10-2)
– In general, for the decimal representation of
X = {… x2x1x0.x-1x-2x-3 … }
X = i xi10i
6
Binary Number System
• Uses only two digits, 0 and 1
• It is base or radix of 2
• Each digit has a value depending on its position:
– 102 = (1x21)+(0x20) = 210
– 112 = (1x21)+(1x20) = 310
– 1002 = (1x22)+ (0x21)+(0x20) = 410
– 1001.1012 = (1x23)+(0x22)+ (0x21)+(1x20) +(1x2-1)+(0x2-2)+(1x2-3) = 9.62510
Why we use binary system for computers?
7
Decimal to Binary conversion
• Integer and fractional parts are handled • e.g. 11.81 to 1011.11001 (approx)
separately, – 11/2 = 5 remainder 1
– Integer part is handled by repeating – 5/2 = 2 remainder 1
division by 2 – 2/2 = 1 remainder 0
– Factional part is handled by repeating – 1/2 = 0 remainder 1
multiplication by 2 – Binary number 1011
• E.g. convert decimal 11.81 to binary – .81x2 = 1.62 integral part 1
– Integer part 11 – .62x2 = 1.24 integral part 1
– Factional part .81 – .24x2 = 0.48 integral part 0
– .48x2 = 0.96 integral part 0
– .96x2 = 1.92 integral part 1
– Binary number .11001 (approximate)
8
Hexadecimal Notation:
– command ground between computer and Human
• Use 16 digits, (0,1,3,…9,A,B,C,D,E,F)
• 1A16 = (116 x 161)+(A16 x 16o)
= (110 x 161)+(1010 x 160)=2610
• Convert group of four binary digits to/from one hexadecimal digit,
– 0000=0; 0001=1; 0010=2; 0011=3; 0100=4; 0101=5; 0110=6;
0111=7; 1000=8; 1001=9; 1010=A; 1011=B; 1100=C; 1101=D;
1110=E; 1111=F;
• e.g.
– 1101 1110 0001. 1110 1101 = DE1.DE
9
Numbers and Their Encodings
Some 4-bit number representation formats
Unsigned integer ± Signed integer
Radix
Fixed point, 3+1 point
Signed fraction ± 2's-compl fraction
Floating point e s log x Logarithmic
Exponent in Significand in Base-2
{-2, -1, 0, 1} {0, 1, 2, 3} logarithm
10
Arithmetic & Logic Unit
• Does the calculations
• Everything else in the computer is there to
service this unit
• Handles integers
• May handle floating point (real) numbers
• May be separate FPU (maths co-processor)
• May be on chip separate FPU (486DX +)
requested
arithmetic Status e.g
operation overflow?
operands (in) operands (out)
11
Integer Representation
• Positive numbers stored in binary
– e.g. 41=00101001
• Only have 0 & 1 to represent
everything
– No minus sign!
– No period!
– Exponent?
• Two approaches to integers
– Sign-Magnitude
– Twos complement
12
Sign-Magnitude
• Left most bit is sign bit
• 0 means positive
• 1 means negative
• +18 = 00010010
• -18 = 10010010
• Problems
– Need to consider both sign and
magnitude in arithmetic
– Two representations of zero
(+0 and -0)
• Leftmost bit is most
significant bit (msb)
• Rightmost bit is least
significant bit (lsb)
13
Two’s Compliment (representation)
14
Basis of Binary Arithmetic
You should already know that
• 02+12 = 12
• 012+ 012 = 102 (or 12+12 = 02 carry 1) K bit binary
• 112 + 12 = 002 carry 1 number,
• 12 – 12 = 02 (no borrow), 02 – 12 = 12 borrow 1
e.g. K = 5
• 00..002 – 12 = 11..112 borrow 1
K-1
• 000112 = 0•24 + 0•23 + 0•22 + 1•21 + 1•20 = bi 2i
i=0
b2
15
Two’s Complement Benefits
• One representation of zero
• Arithmetic works easily (see later)
• Negating is fairly easy:
– 310 = 000000112
– bitwise complement gives 11111100
– Add 1 11111101 = –310
two’s complement one’s complement
16
Geometric Depiction of Twos Complement
Integers
In n bits, can
store integers from
–2n-1 to + 2n-1 – 1
neg pos
17
Range of Numbers
• 8 bit 2s complement
– +127 = 01111111 = 27 -1
– -128 = 10000000 = -27
• 16 bit 2s complement
– +32767 = 01111111 11111111 = 215 - 1
– -32768 = 10000000 00000000 = -215
• N bit 2s complement Largest positive
– 011111111..11111111 = 2N-1 - 1
– 100000000..00000000 = -2N-1
Smallest (?) negative
18
Negation Special Case 1
0= 00000000
Bitwise not 11111111
Add 1 +1
Result 1 00000000
if this bit is ignored: problem . . .
–0=0
OK
19
Carry vs. Overflow
• So . . . what about the ignored bit on the last slide?
• CARRY: is an artifact of performing addition
– always happens: for binary values, carry = 0 or 1
– exists independently of computers!
• OVERFLOW: an exception condition that results from using
computers to perform operations
– caused by restricting values to fixed number of bits
– occurs when result exceeds range for the number of bits
– important limitation of using computers!
e.g. Y2K bug!
20
Overflow is Implementation Dependent!
• recall example: negation of 0
• binary addition is performed – regardless of interpretation
11111111
+1
1 00000000 for this addition
the answer is:
fixed number 0
of bits to with carry = 1
represent
carry values
21
Unsigned Interpretation -> Overflow
• consider values as unsigned integers
11111111 25510
+1
1 00000000
• but . . . 255 + 1 = 256 ??
• answer = 0 ???
• OVERFLOW occurred!
• WHY? cannot represent 256 as an 8-bit unsigned integer!
22
Signed Interpretation -> No Overflow
• consider values as signed integers
11111111 – 110
+1
1 00000000
• –1 + 1 = 0
• answer is correct!
• OVERFLOW did not occur (even though carry =1!)
• WHY? can represent 0 as an 8-bit signed integer!
23
Negation Special Case 2
– 128 = 10000000
bitwise not 01111111
Add 1 to lsb +1
Result 10000000
• Whoops!
– (– 128) = – 128 Problem! OVERFLOW!
• Need to monitor msb (sign bit)
• It should change during negation? what about negating zero?
24
Conversion Between Lengths, e.g. 8 -> 16
• Positive number: add leading zeros
– +18 = 00010010
– +18 = 00000000 00010010
• Negative numbers: add leading ones
– -18 = 11101110
– -18 = 11111111 11101110
• i.e. pack with msb (sign bit)
called “sign extension”
25
Addition and Subtraction
Hardware for Addition and Subtraction
• Normal binary addition
• 0011 0101 1100
• +0100 + 0100 +1111
• -------- ---------- ------------
• 0111 1001 = overflow 11011
• Monitor sign bit for overflow (sign bit
change as adding two positive numbers
or two negative numbers.)
• Subtraction: Take twos compliment of
subtrahend then add to minuend
– i.e. a - b = a + (-b)
• So we only need addition and
complement circuits
26
Adders
Murdocca, Figure 3-2
Ripple-Carry Adder
Murdocca, Figure 3-6
Addition/Subtraction Unit
27
Multiplication
• Complex
• Work out partial product for each digit
• Take care with place value (column)
• Add partial products
1011 Multiplicand (1110)
x 1101 Multiplier (1310)
1011 Partial products
0000 Note: if multiplier bit is 1
1011 copy multiplicand (place value)
1011 otherwise zero
10001111 Product (14310)
• Note: need double length result – could easily overflow single word
28
Unsigned Binary Multiplication
29
Unsigned Binary Multiplication
30
Array Multiplier
31
Multiplying Negative Numbers
• The previous method does not work!
• Solution 1
– Convert to positive if required
– Multiply as above
– If signs of the original two numbers were different, negate answer
• Solution 2
– Booth’s algorithm
32
Booth’s Algorithm
Booth’s Algorithm : Increase speed of a multiplication when
there are consecutive ones or zeros in the multiplier.
• Based on shifting of multiplier from right-to-left
1. If at boundary of a string of 0’s, add multiplicand to
product (in proper column)
2. If at boundary of a string of 1’s, subtract multiplicand to
product (in proper column)
3. If within string, just shift.
33
Example of Booth’s Algorithm
34
Division
• More complex than multiplication
• However, can utilize most of the same hardware.
• Based on long division
35
Division of Unsigned Binary Integers
36
Real Numbers
• Numbers with fractions
• Could be done in pure binary
– 1001.1010 = 24 + 20 +2-1 + 2-3 =9.625
• Where is the binary point?
• Fixed?
– Very limited
• Moving?
– How do you show where it is?
37
Floating Point
38
Floating Point Examples
39
Signs for Floating Point
• Exponent is in excess or biased notation
– e.g. Excess (bias) 127 means
– 8 bit exponent field for k bit exponent:
– Pure value range 0-255 bias = 2k–1 –1
– Subtract 127 to get correct value
– Range -127 to +128
• The relative magnitudes (order) of the numbers do not change.
– Can be treated as integers for comparison.
40
Normalization //
• FP numbers are usually normalized
• i.e. exponent is adjusted so that leading bit (MSB) of mantissa is 1
• Since it is always 1 there is no need to store it
• (c.f. Scientific notation where numbers are normalized to give a single
digit before the decimal point
• e.g. 3.123 x 103)
41
FP Ranges
• For a 32 bit number
– 8 bit exponent
– +/- 2256 1.5 x 1077
• Accuracy
– The effect of changing lsb of mantissa
– 23 bit mantissa 2-23 1.2 x 10-7
– About 6 decimal places
42
Expressible Numbers
43
IEEE 754
• Standard for floating point storage
• 32 and 64 bit standards
• 8 and 11 bit exponent respectively
• Extended formats (both mantissa and exponent) for intermediate results
• Representation: sign, exponent, faction
– 0: 0, 0, 0
– -0: 1, 0, 0
– Plus infinity: 0, all 1s, 0
– Minus infinity: 1, all 1s, 0
– NaN; 0 or 1, all 1s, =! 0
44
FP Arithmetic +/-
• Check for zeros
• Align significands (adjusting exponents)
• Add or subtract significands
• Normalize result
45
FP Arithmetic x/:
• Check for zero
• Add/subtract exponents
• Multiply/divide significands (watch sign)
• Normalize
• Round
• All intermediate results should be in double length storage
46
Floating Point
Division Multiplication
47
Encoding Numbers in 4 Bits
-16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16
Number
format
Unsigned integers
Signed-magnitude
3 + 1 fixed-point, xxx.x
Signed fraction, .xxx
2’s-compl. fraction, x.xxx
2 + 2 floating-point, s 2 e e s
e in [-2, 1], s in [0, 3]
2 + 2 logarithmic (log = xx.xx) log x
48
Conclusion
49