Post on 02-May-2020
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 1
Computer Arithmetic(Tietokonearitmetiikka)
Stallings: Ch 9
Integer representation (Kokonaislukuesitys)Integer arithmetic (Kokonaislukuaritmetiikka)Floating-point representation (Liukulukuesitys)Floating-point arithmetic (Liukulukuaritmetiikka)
Lecture 5
ALU
(Sta06 Fig 9.1)
2?
+ - * ?? ylivuoto?
tulos
ALU = Arithmetic Logic Unit (Aritmeettis-looginen yksikkö)Actually performs operations on data
Integer and floating-point arithmeticComparisons (vertailut), left and right shifts (sivuttaissiirrot)Copy bits from one register to anotherAddress calculations (Osoitelaskenta): branch and jump (hypyt),memory references (muistiviittaukset)
Data from/to internal registers (latches)Input copied from normal registers(or from memory)Output goes to reg (or memory)
OperationBased on instruction register, control unit
29.1.2010 2Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 2
Computer Organization II
Integer representation
(kokonaislukujen esitys)
29.1.2010 3Computer Organization II, Spring 2010, Tiina Niklander
Integer Representation (Kokonaislukuesitys)
Binary representation, bit sequence, only 0 and 1”Weight” of the number based on position
Most significant bit, MSB (eniten merkitsevä bitti)Least significant bit, LSB (vähiten merkitsevä bitti)
57 = 5*101 + 7*100
= 32 + 16 + 8 + 1= 1*25 + 1*24 + 1*23 + 0*22 + 0*21 + 1*20
= 0011 1001= 0x39= 3*161 + 9*160
hexadecimal
29.1.2010 4Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 3
Integer Representation (Kokonaislukuesitys)
Negative numbers?Sign magnitude (Etumerkki-suuruus)Twos complement (2:n komplementtimuoto)
Computers use twos complementJust one zero (no +0 and -0)
- Comparison to zero easyMath is easy to implement
- No need to consider sign- Subtraction becomes addition
Simple hardware and circuit
-57 = 1011 1001
-57 = 1100 0111
Sign(etumerkki)
+2 = 0000 0010+1 = 0000 00010 = 0000 0000
-1 = 1111 1111-2 = 1111 1110
29.1.2010 5Computer Organization II, Spring 2010, Tiina Niklander
Twos complement (2:n komplementti)Example
8-bit sequence, value -57
Easy to expand. As a 16-bit sequence
57 = 0011 1001 unsigned value (itseisarvo)1100 0110 invert bit (ones complement)
1100 01101 add 1
1100 0111 twos complement
57 = 0011 1001 = 0000 0000 0011 1001-57 = 1100 0111 = 1111 1111 1100 0111
signextension
Rejectoverflow
29.1.2010 6Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 4
Twos complement
Value range (arvoalue): -2n-1 … 2n-1 -1
Addition overflow (yhteenlaskun ylivuoto) easy to detectNo overflow, if different signs in operandsOverflow, if same sign (etumerkki)and the results sign differs from the operands
8 bits: -27 … 27-1 = -128 … 12732 bits: -231 … 231-1 = -2 147 483 648 … 2 147 483 647
57 = 0011 1001+ 80 = 0101 0000
137 = 1000 1001 Overflow!
29.1.2010 7Computer Organization II, Spring 2010, Tiina Niklander
Twos complementSubtraction as addition (vähennyslasku yhteenlaskuna)!
Forget the sign, handle as if unsigned!Complement 2nd term, the subtrahend, then add (lisää 2:nkomplementti vähentäjästä)Simple hardware
Check- Overflow? (same rule as in addition)- sign= 1, result is negative
3= 0011
11001
1101
+1 = 0001-3 = 1101-2 = 1110
(Sta06 Table 9.1)
-3 in two complement
29.1.2010 8Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 5
Computer Organization II
Integer arithmetics(kokonaislukuaritmetiikkaa)
Negation (negaatio)Addition (yhteenlasku)Subtraction (vähennyslasku)Multiplication (kertolasku)Division (jakolasku)
29.1.2010 9Computer Organization II, Spring 2010, Tiina Niklander
Negation = Twos complement
1: invert all bits2: add 13: Special cases
Ignore carry bit (ylivuotobitti)Sign really changed?
- Cannot negate smallest negative- Result in exception
Simple hardware
-57 = 1100 01110011 1000
10011 1001
= 57
-128 = 1000 00000111 1111
11000 0000
29.1.2010 10Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 6
Normal binary additionIn subtraction: complement the 2. operand, subtrahend (vähentäjä) andadd to 1. operand, minuend (vähennettävä)
Ignore carryCheck sign!
Overflow indicationSimple hardware function
Two circuits:Complement and addition
1100 =-4 1100 = -4+1111 =-1 +1011 = -511011 =-5 10111 = ?
Addition (and subtraction)
(Sta06 Fig 9.6)Overflow
29.1.2010 11Computer Organization II, Spring 2010, Tiina Niklander
Integer multiplication
”Just like” you learned at schoolEasy with just 0 and 1!
Hardware?ComplexSeveral algorithms
Overflow?32 b operands result 64 b?
Simpler, if only unsigned numbersJust multiple additionsOr additions and shifts
- Shift left = multiply by 2- esim: 5 * => add, shift, shift, add
(Sta06 Fig 9.7)
2* 10011 => 100110
Example: 5*11add=> 1011shift=> 10110shift=> 101100add=>110111 (= 55)
29.1.2010 12Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 7
Unsigned multiplication example(kerrottava)
(Sta06 Fig 9.8a)
(kertoja)+ half of result
Result ( tulo(s) )
If Q0=0 just shiftelse add, shift
29.1.2010 13Computer Organization II, Spring 2010, Tiina Niklander
13 * 11 = ???
1011
110100000
take next sum
C
M
A Q10110
11100101
result bit from Afrom C skip next sumjust do SHIFT
ADD
SHIFTSHIFT
0010 1111
take next sum
ADD
1101 1111
SHIFT
0110 1111
take next sum
ADD
00011 1111
SHIFT
Result on left,multiplier on right!
= 1000 1111 = 128+8+4+2+1 = 143
Overflow? No.
Unsigned Multiplication Example
0 1000 1111
(Sta06Fig. 9.8)
29.1.2010 14Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 8
Unsigned multiplicationQ * M = 1101 * 1011 = 1000 1111 eli 13*11 = 143
(Sta06 Fig 9.8b)
29.1.2010 15Computer Organization II, Spring 2010, Tiina Niklander
Unsigned multiplication
(Sta06 Fig 9.9)
29.1.2010 16Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 9
Multiplication with negative values?
The preceeding algorithm for unsigned numbers does NOTwork for negative numbers
Could do with unsigned numbersChange operands to positive valuesDo multiplication with positive valuesCheck signs and negate the result if needed
This works, but there are better and faster mechanismsavailable
29.1.2010 17Computer Organization II, Spring 2010, Tiina Niklander
Booth’s Algorithm
Unsigned multiplication:Addition (only) for every ”1” bit in multiplier (kertoja)
Booth’s algorithm (improvement)Combine all adjacent 1’s in multiplier together,Replace all additions by one subtraction and one additionExample: 7*x = 8*x +(–x)
111*x = 1000*x +(-x) =add, shift, shift, shift, complement, add
(in reality, the complement would be first)
Works for twos complement! Also negative values!
00101000 4011111011 -5
100100011 = 35
5 * 7 = 0101 * 0111= 0101 * (1000-0001)
29.1.2010 18Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 10
Booth’s algorithm(Sta06 Fig 9.12)
Arithmetic Shift Right:= fill with sign
1000 1000
1100 0100
Why does it work?M*(01111111) = 27 - 1M*(00011110) = 25 -2 1
M*(01111010) = 27 -23+22-2 1
Current bitis the first ofblock of 1’s Previous bit
was the lastof block of 1’s
Continuing block of 1’s
Continuing block of 0’s
Sign bit extending
29.1.2010 19Computer Organization II, Spring 2010, Tiina Niklander
Booth’s Algorithm for Twos ComplementMultiplication
Q-1
arithmetic shift right
+/-
operands
result
(Sta06 Fig 9.12)
29.1.2010 20Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 11
Booth’s Algorithm Example
Q-1
+/-
Arithmetic Shift Right
M:
A: Q:0000 0011 0
0111
10 subtract A-M
1001 0011 0
Arithm SHIFT1100 1001 1
11 just SHIFT1 bit of result
Arithm SHIFT
sign extended
1110 0100 1
01 ADD
0101 0100 1
Arithm SHIFT
Carry bit was lost
0010 1010 0
00 just SHIFT
Arithm SHIFT
0001 0101 0
7 * 3 = ?= 0001 0101 = 21
Fig. 9.12 [Sta06]
29.1.2010 21Computer Organization II, Spring 2010, Tiina Niklander
Booth’s Algorithm, example
(Sta06 Fig 9.13)
Q * M = 0011 * 0111 = 0001 0101 eli 3*7 = 211-0 subtract (vähennys)
0-1 add (lisäys)
29.1.2010 22Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 12
Integer division (kokonaislukujen jakolasku)
(Sta06 Fig 9.15)
(jaettava)(jakaja)(osamäärä)
(jakojäännös)
Like in school algorithmEasy: new quotient digit always 0 or 1
Hardware needs as in multiplicationShift left = consider new digit
29.1.2010 23Computer Organization II, Spring 2010, Tiina Niklander
Integer division (kokonaislukujen jakolasku)
Sta06 Fig 9.16
Works only for positive integers,Negatives need some extra work,Step-by-step example Fig 9.17 [Sta06 ]
A Q Q0SHL
Guess failed,restores A’s previous value
Guess, that the next bit is 1
”expand” with one more digit
29.1.2010 24Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 13
Example: twos complement division
Division: 7/3 A+ Q = 7 = 0000 0111 M= 3 = 0011
0000 0111 initial value0000 1110 shift left1101 subtract M0000 1110 restore0001 1100 shift left1110 subtract M0001 1100 restore0011 1000 shift left0000 subtract M0000 1001 set Q0=10001 0010 shift1110 subtract M0001 0010 restore
A Q
Q = quotient = 2A = remainder = 1
Subtract M = Add (–M)-M = -3 = 1101
If subtraction succesful, Q0 = 1
Repeat as many times as Q has bits.Sta09 Fig 9.17
First try, if you can do the subtraction(or add if different signs).If the sign changed, subtraction failedand A must be restored, Q0 = 0
29.1.2010 25Computer Organization II, Spring 2010, Tiina Niklander
Computer Organization II
Floating Point Representation(Liukulukuesitys)
29.1.2010 26Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 14
Floating Point Representation
Significant digits (Merkitsevät numerot) and exponent (suuruusluokka)Normalized number (Normeerattu muoto)
Most significant digit is nonzero >0Commonly just one digit before the radix point (desim. pilkku)
-0.000 000 000 123 = -1.23 * 10-10
0.123 = +1.23 * 10-1
123.0 = +1.23 * 102
123 000 000 000 000 = +1.23 * 1014
or mantissa
29.1.2010 27Computer Organization II, Spring 2010, Tiina Niklander
IEEE 754 (floating point) formats
(Sta06 Table 9.3)
29.1.2010 28Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 15
32-bit floating point
1 b sign1 = “-”, 0 = “+”
8 b exponentBiased representation, no sign (Ei etumerkkiä, vaan erillinen
nollataso)- Exp=5 store 127+5, Exp=-5 store 127-5 (bias127)
23 b significant (mantissa)In normalized form the radix point is preceeded with 1, whichis not stored. (hidden bit, Zuse Z3 1939)
The binary value of the floating point representation-1Sign * 1.Mantissa * 2Exponent-127
29.1.2010 29Computer Organization II, Spring 2010, Tiina Niklander
Example
1.0 = +1.0000 * 20 = ?
exponent mantissasign
0 000 0000 0000 0000 0000 0000
23.0 = +10111.0 * 20 = +1.0111 * 24 =?
exponent mantissasign
0 011 1000 0000 0000 0000 00001000 0011127+4=131
0111 11110+127 = 127
29.1.2010 30Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 16
Example
exponent mantissasign
0 111 1000 0000 0000 0000 00001000 0000
X = ?
= (1 + 0.5 + 0.25 + 0.125+0.0625) * 2
= (1+ 1/2 + 1/4 + 1/8 + 1/16) * 2
= 1.11112 * 2
= 1.9375 * 2 = 3.875
X = (-1)0 * 1.1111 * 2(128-127)
29.1.2010 31Computer Organization II, Spring 2010, Tiina Niklander
Accuracy (tarkkuus) (32b)
Value range (arvoalue)8 b exponent 2-126 ... 2127 ~ -10-38 ... 1038
Not exact value24 b mantissa 224 ~ 1.7 * 10-7 ~ 6 decimals
Balancing between range and precision
Numerical errors: Patriot Missile (1991), Ariane 5 (1996)http://ta.twi.tudelft.nl/nw/users/vuik/wi211/disasters.html
29.1.2010 32Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 17
Interpretation of IEEE 754 Floating-Point Numbers
(Sta06 Table 9.4)
Not a Number
DoublePrecisionsimilarly
29.1.2010 33Computer Organization II, Spring 2010, Tiina Niklander
NaN: Not a Number
(Sta06 Table 9.6)
29.1.2010 34Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 18
Computer Organization II
Floating Point Arithmetics(Liukulukuaritmetiikkaa)
IEEE-754 StandardAdditionSubtractionMultiplicationDivision
29.1.2010 35Computer Organization II, Spring 2010, Tiina Niklander
Floating point arithmetics
Calculations need wide registersGuard bits - pad right end of significandMore bits for the significand (mantissa)Using Denormalized formats
Addition and subtractionMore complex than multiplicationOperands must have same exponent
- Denormalize the smaller operand (alignment!)- Loss of digits (less precise and missing information)
Result (must) be normalisedMultiplication and division
Significand and exponent handled separately
29.1.2010 36Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 19
Floating point arithmetics
(Sta06 Table 9.5)
29.1.2010 37Computer Organization II, Spring 2010, Tiina Niklander
Addition and Subtraction
(Sta06 Fig 9.22)
Lesser operandmay be fully lost!
29.1.2010 38Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 20
Special casesExponent overflow (eksponentin ylivuoto)
Very large number (above max)Value or - , alternatively cause exception
Exponent underflow (eksponentin alivuoto)Very small number (below min)Value 0 (or cause exception)
Significand overflow (mantissan ylivuoto)Normalise!
Significand underflow (mantissan alivuoto)Denormalizing may lose the significand accuracyAll significant bits lost?
Programmable option
Programmable option
Ooops, lost data!
Fix it!
29.1.2010 39Computer Organization II, Spring 2010, Tiina Niklander
Rounding (pyöristys)
ExampleValue has four decimalsPresent it using only 3 decimals
Normal rounding ruleround to nearest value
Always towards (ylöspäin)Always towards - (alaspäin)Always towards 0
For example, Intel Itanium supports all of these alternatives
3.1234, -4.5678
3.123, -4.568
3.124, -4.5673.123, -4.5683.123, -4.567
29.1.2010 40Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 21
Multiplication
(Sta06 Fig 9.23)
29.1.2010 41Computer Organization II, Spring 2010, Tiina Niklander
Division
(Sta06 Fig 9.24)
29.1.2010 42Computer Organization II, Spring 2010, Tiina Niklander
Lecture 5: Computer arithmetic 29.1.2010
Comp. Org II, Spring 2010 22
Review Questions / Kertauskysymyksiä
Why we use twos complement?How does twos complement “expand” to a large numberof bits (8b 16 b)?Format of single-precision floating point number?When does underflow happen?
Miksi käytetään 2:n komplementtimuotoa?Miten 2:n komplementtiesitys laajenee “suurempaantilaan” (esim. 8b esitys 16 b:n esitys)?Millainen on yksinkertaisen tarkkuuden liukuluvunesitysmuoto?Milloin tulee liukuluvun alivuoto?
29.1.2010 43Computer Organization II, Spring 2010, Tiina Niklander