0% found this document useful (0 votes)
9 views

Computer arithmetic

Uploaded by

kumharvivek6791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Computer arithmetic

Uploaded by

kumharvivek6791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

COD Ch.

4
Arithmetic for Computers
Arithmetic
 Where we've been:
 performance
 abstractions
 instruction set architecture
 assembly language and machine language
 What's up ahead:
 implementing the architecture
operation

32 ALU
result
32
b

32
Numbers
 Bits are just bits (no inherent meaning)
 conventions define relationship between bits and numbers
 Binary integers (base 2)
 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001...

 decimal: 0, …, 2n-1 n bits

 Of course it gets more complicated:


 bit strings are finite, but
 for some fractions and real numbers, finitely many bits is not enough, so
 overflow & approximation errors: e.g., represent 1/3 as binary!
 negative integers
 How do we represent negative integers?
 which bit patterns will represent which integers?
Possible Representations
 Sign Magnitude: One's Complement Two's Complement
000 = 0 000 = 0 000 = 0

ambiguous zero
001 = +1 001 = +1 001 = +1

ambiguous zero
010 = +2 010 = +2 010 = +2
011 = +3 011 = +3 011 = +3
100 = 0 100 = -3 100 = -4
101 = -1 101 = -2 101 = -3
110 = -2 110 = -1 110 = -2
111 = -3 111 = 0 111 = -1
 Issues:
 balance – equal number of negatives and positives
 ambiguous zero – whether more than one zero representation
 ease of arithmetic operations
 Which representation is best? Can we get both balance and non-ambiguous
zero?
Representation Formulae
 Two’s complement:

xn xn-1…x0 = xn * -2n + xn-1 * 2n-1 + … + x0 * 20


or
xnX’ = xn * -2n + X’ (writing rightmost n bits xn-1…x0 as X’)

= X’, if xn = 0
-2n + X’, if xn = 1

 One’s complement:

xnX’ = X’, if xn = 0
-2n + 1 + X’, if xn = 1
Two's Complement Operations
 Negation Shortcut: To negate any two's complement integer
(except for minint) invert all bits and add 1
 note that negate and invert are different operations!
 why does this work? Remember we don’t know how to add in 2’s
complement yet! Later…!
 Sign Extension Shortcut: To convert an n-bit integer into an
integer with more than n bits – i.e., to make a narrow integer fill
a wider word – replicate the most significant bit (msb) of the
original number to fill the new bits to its left
 Example: 4-bit 8-bit
0010 = 0000 0010
1010 = 1111 1010
 why is this correct? Prove!
Two’s Complement Addition
 Perform add just as in junior school (carry/borrow 1s)
 Examples (4-bits):
0101 0110 1011 1001 1111
0001 0101 0111 1010 1110
Do these sums now!! Remember all registers are 4-bit including result register!
So you have to throw away the carry-out from the msb!!
 Have to beware of overflow : if the fixed number of bits (4, 8, 16,
32, etc.) in a register cannot represent the result of the operation
 terminology alert: overflow does not mean there was a carry-out from
the msb that we lost (though it sounds like that!) – it means simply
that the result in the fixed-sized register is incorrect
 as can be seen from the above examples there are cases when the result
is correct even after losing the carry-out from the msb
Two’s Complement Addition:
Verifying Carry/Borrow method
 Two (n+1)-bit integers: X = xnX’, Y = ynY’
Carry/borrow 0  X’ + Y’  2n 2n  X’ + Y’  2n+1 –
add X + Y (no CarryIn to last bit) 1
(CarryIn to last bit)
xn = 0, yn = 0 ok not ok(overflow!)
xn = 1, yn = 0 ok ok
xn = 0, yn = 1 ok ok
xn = 1, yn = 1 not ok(overflow!) ok

 Prove the cases above!


 Prove if there is one more bit (total n+2 then) available for the result
then there is no problem with overflow in add!
Two's Complement Operations
 Now verify the negation shortcut!
 consider X + (X +1) = (X + X) + 1:
associative law – but what if there is overflow in one of the adds on
either side, i.e., the result is wrong…!
 think minint !
 Examples:
 –0101 = 1010 + 1 = 1011
 –1100 = 0011 + 1 = 0100
 –1000 = 0111 + 1 = 1000
Detecting Overflow
 No overflow when adding a positive and a negative number
 No overflow when subtracting numbers with the same sign
 Overflow occurs when the result has “wrong” sign (verify!):

Operation Operand A Operand B Result


Indicating Overflow

A + B  0  0  0
A + B  0  0  0
A – B  0  0  0
A – B  0  0  0

 Consider the operations A + B, and A – B


 can overflow occur if B is 0 ?
 can overflow occur if A is 0 ?
Effects of Overflow
 If an exception (interrupt) occurs
 control jumps to predefined address for exception
 interrupted address is saved for possible resumption

 Details based on software system/language


 SPIM: see the EPC and Cause registers

 Don't always want to cause exception on overflow


 add, addi, sub cause exceptions on overflow
 addu, addiu, subu do not cause exceptions on overflow
What about Subtraction (a – b) ?
 Two's complement approach: just negate b and add.
 How do we negate?
 recall negation shortcut : invert each bit of b and set CarryIn to
least significant bit (ALU0) to 1
Binvert Operation
CarryIn

a
0

1
Result

b 0 2

CarryOut
Multiply
 Grade school shift-add method:
Multiplicand 1000
Multiplier x 1001
1000
0000
0000
1000
Product 01001000
 m bits x n bits = m+n bit product
 Binary makes it easy:
 multiplier bit 1 => copy multiplicand (1 x multiplicand)
 multiplier bit 0 => place 0 (0 x multiplicand)
 3 versions of multiply hardware & algorithm:
Shift-add Multiplier Version 1
Start

Multiplier0 = 1 1. Test Multiplier0 = 0


Multiplier0
32-bit multiplicand starts at right half of multiplicand register

Multiplicand 1a. Add multiplicand to product and


Shift left place the result in Product register
64 bits

Multiplier
64-bit ALU Shift right
2. Shift the Multiplicand register left 1 bit
32 bits

Product
Control test
Write 3. Shift the Multiplier register right 1 bit
64 bits

Product register is initialized at 0


No: < 32 repetitions
Multiplicand register, product register, ALU are 32nd repetition?

64-bit wide; multiplier register is 32-bit wide


Yes: 32 repetitions

Done Algorithm
Shift-add Multiplier Version1
Start

Multiplier0 = 1 1. Test Multiplier0 = 0 Example: 0010 * 0011:


Multiplier0

Itera Step Multiplier Multiplicand Product


-tion
1a. Add multiplicand to product and
place the result in Product register 0 init 0011 0000 0010 0000 0000
values
1 1a 0011 0000 0010 0000 0010
2 0011 0000 0100 0000 0010
2. Shift the Multiplicand register left 1 bit
3 0001 0000 0100 0000 0010
2 …
3. Shift the Multiplier register right 1 bit

No: < 32 repetitions


32nd repetition?

Yes: 32 repetitions

Done

Algorithm
Observations on Multiply
Version 1
 1 step per clock cycle  nearly 100 clock cycles to multiply two
32-bit numbers
 Half the bits in the multiplicand register always 0
 64-bit adder is wasted
 0’s inserted to right as multiplicand is shifted left
 least significant bits of product never
change once formed

 Intuition: instead of shifting multiplicand to left, shift product to


right…
Shift-add Multiplier Version 2
Start

Multiplier0 = 1 1. Test Multiplier0 = 0


Multiplier0

Multiplicand 1a. Add multiplicand to the left half of


the product and place the result in
32 bits the left half of the Product register

Multiplier
32-bit ALU Shift right
32 bits 2. Shift the Product register right 1 bit

Shift right
Product Control test
Write
3. Shift the Multiplier register right 1 bit
64 bits

Product register is initialized at 0


No: < 32 repetitions
Multiplicand register, multiplier register, ALU 32nd repetition?

are 32-bit wide; product register is 64-bit wide; Yes: 32 repetitions


multiplicand adds to left half of product register
Done Algorithm
Shift-add Multiplier Version 2
Start

Multiplier0 = 1 1. Test
Multiplier0
Multiplier0 = 0
Example: 0010 * 0011:

Itera Step Multiplier Multiplicand Product


1a. Add multiplicand to the left half of
the product and place the result in
-tion
the left half of the Product register 0 init 0011 0010 0000 0000
values
1 1a 0011 0010 0010 0000
2. Shift the Product register right 1 bit
2 0011 0010 0001 0000
3 0001 0010 0001 0000

3. Shift the Multiplier register right 1 bit


2 …

No: < 32 repetitions


32nd repetition?

Yes: 32 repetitions

Done

Algorithm
Observations on Multiply
Version 2

 Each step the product register wastes space that exactly matches
the current size of the multiplier

 Intuition: combine multiplier register and product register…


Shift-add Multiplier Version 3
Start

Product0 = 1 1. Test Product0 = 0


Product0

Multiplicand

32 bits 1a. Add multiplicand to the left half of


the product and place the result in
the left half of the Product register

32-bit ALU

Shift right Control 2. Shift the Product register right 1 bit


Product
Write test
64 bits

No: < 32 repetitions


32nd repetition?
Product register is initialized with multiplier on right

No separate multiplier register; multiplier


Yes: 32 repetitions

placed on right side of 64-bit product register


Done Algorithm
Shift-add Multiplier Version 3
Start

Product0 = 1 1. Test Product0 = 0


Product0 Example: 0010 * 0011:

Itera Step Multiplicand Product


-tion
1a. Add multiplicand to the left half of
the product and place the result in
0 init 0010 0000 0011
the left half of the Product register values
1 1a 0010 0010 0011
2 0010 0001 0001
2 …
2. Shift the Product register right 1 bit

No: < 32 repetitions


32nd repetition?

Yes: 32 repetitions

Done

Algorithm
Observations on Multiply
Version 3
 2 steps per bit because multiplier & product combined
 What about signed multiplication?
 easiest solution is to make both positive and remember whether to
negate product when done, i.e., leave out the sign bit, run for 31 steps,
then negate if multiplier and multiplicand have opposite signs

 Booth’s Algorithm is an elegant way to multiply signed numbers using


same hardware – it also often quicker…
Motivating Booth’s algorithm
 Example 0010 * 0110. Traditional:
0010
x 0110
0000 shift (0 in multiplier)
0010 add (1 in multiplier)
0010 add (1 in multiplier)
0000 shift (0 in multiplier)
00001100
 Same example. But observe there are two successive 1’s in multiplier
0110 = 22 + 21 = 23 – 21, so can replace successive 1’s by subtract and
then add:
0010
0110
0000 shift (0 in multiplier)
-0010 sub (first 1 in multiplier)
0000 shift (middle of string of 1’s)
0010 add (previous step had last 1)
00001100
Motivating Booth’s Algorithm
middle of run
end of run beginning of run
0 1 1 1 1 0
bit = 2n bit = 2m

 Math idea: string of 1’s …011…10… has


successive 1’s

value the sum 2n + 2n-1 + … + 2m = 2n+1 – 2m

 Replace a string of 1s in multiplier with an initial subtract when we


first see a one and then later add after the last one
 What if the string of 1’s started from the left of the (2’s complement) number,
e.g., 11110001 – would the formula above have to be modified?!
Booth from Multiply Version 3
 Modify Step 1 of the algorithm Multiply Version 3 to consider 2 bits of
the multiplier: the current bit and the bit to the right (i.e., the current
bit of the previous step). Instead of two outcomes, now there are four:

Case Current Bit Bit to the Right Explanation Example Op


1a 0 0 Middle of run of 0s 0001111000 none
1b 0 1 End of run of 1s 0001111000 add
1c 1 0 Begins run of 1s 0001111000 sub
1d 1 1 Middle of run of 1s 0001111000 none

 Modify Step 2 of Multiply Version 3 to sign extend when the product is


shifted right (arithmetic right shift, rather than logical right shift)
because the product is a signed number
 Now draw the flowchart for Booth’s algorithm !
 Multiply Version 3 and Booth share the same hardware, except Booth
requires one extra flipflop to remember the bit to the right of the
current bit in the product register – which is the bit pushed out by the
preceding right shift
Booth Example (2 x 7)
Operation Multiplicand Product next?
0. initial value 0010 0000 0111 0 10 -> sub P = P - M

1c. 0010 1110 0111 0 shift P (sign ext)


2. 0010 1111 0011 1 11 -> nop
1d. 0010 1111 0011 1 shift P (sign ext)
2. 0010 1111 1001 1 11 -> nop
1d. 0010 1111 1001 1 shift P (sign ext)
2. 0010 1111 1100 1 01 -> add P = P + M
1b. 0010 0001 1100 1 shift P (sign ext)
2. 0010 0000 1110 0 done
Booth Algorithm (2 * -3)
Operation Multiplicand Product next?
0.initial value 0010 0000 1101 0 10 -> sub P = P - M
1c. 0010 1110 1101 0 shift P (sign ext)
2. 0010 1111 0110 1 01 -> add P = P + M
1b. 0010 0001 0110 1 shift P (sign ext)
2. 0010 0000 1011 0 10 -> sub P = P - M
1c. 0010 1110 1011 0 shift P
2. 0010 1111 0101 1 11 -> nop
1d. 0010 1111 0101 1 shift P
2. 0010 1111 1010 1 done
Verifying Booth’s Algorithm
 multiplier a = a31 a32… a0, multiplicand = b
 ai ai-1 Operation
0 0 nop
0 1 add b
1 0 sub b
1 1 nop
 0, nop
I.e., if ai-1 – ai = +1, add b
–1, sub b
 Therefore, Booth computes sum:
(a–1 – a0) * b * 20
+ (a0 – a 1) * b * 21
+ (a1 – a2) * b * 22

+ (a30 – a31) * b * 231
= … simplify telescopic sum! …
MIPS Notes
 MIPS provides two 32-bit registers Hi and Lo to hold a 64-bit
product
 mult, multu (unsigned) put the product of two 32-bit register
operands into Hi and Lo: overflow is ignored by MIPS but can
be detected by programmer by examining contents of Hi
 mflo, mfhi moves content of Hi or Lo to a general-purpose
register
 Pseudo-instructions mul (without overflow), mulo (with
overflow), mulou (unsigned with overflow) take three 32-bit
register operands, putting the product of two registers into the
third
Divide
1001 Quotient
Divisor 1000 1001010 Dividend
–1000
10
101
1010
–1000
10 Remainder

 Junior school method: see how big a multiple of the divisor can be
subtracted, creating quotient digit at each step
 Binary makes it easy  first, try 1 * divisor; if too big, 0 * divisor
 Dividend = (Quotient * Divisor) + Remainder
 3 versions of divide hardware & algorithm:
Start

Divide Version 1 1. Subtract the Divisor register from the


Remainder register and place the
result in the Remainder register

32-bit divisor starts at left half of divisor register

Remainder –> 0 Remainder < 0


Test Remainder
Divisor Quotient register is
Shift right
initialized to be 0
64 bits

2a. Shift the Quotient register to the left, 2b. Restore the original value by adding
Quotient setting the new rightmost bit to 1 the Divisor register to the Remainder
64-bit ALU Shift left register and place the sum in the
32 bits Remainder register. Also shift the
Quotient register to the left, setting the
new least significant bit to 0
Remainder Control
Write test
64 bits

3. Shift the Divisor register right 1 bit


Remainder register is initialized with the dividend at right

Divisor register, remainder register, ALU are 33rd repetition?


No: < 33 repetitions

64-bit wide; quotient register is 32-bit wide


Yes: 33 repetitions

Why 33? We shall see later… Done Algorithm


Divide Version 1
Start

Example: 0111 / 0010:


1. Subtract the Divisor register from the
Remainder register and place the
result in the Remainder register
Itera- Step Quotient Divisor Remainder
tion
0 init 0000 0010 0000 0000 0111
Remainder –> 0 Remainder < 0
1 1 0000 0010 0000 1110 0111
Test Remainder
2b 0000 0010 0000 0000 0111
3 0000 0001 0000 0000 0111
2 …
2a. Shift the Quotient register to the left, 2b. Restore the original value by adding
setting the new rightmost bit to 1 the Divisor register to the Remainder 3
register and place the sum in the
Remainder register. Also shift the 4
Quotient register to the left, setting the
new least significant bit to 0 5

3. Shift the Divisor register right 1 bit

No: < 33 repetitions


33rd repetition?

Yes: 33 repetitions

Done Algorithm
Observations on Divide Version 1
 Half the bits in divisor always 0
  1/2 of 64-bit adder is wasted

  1/2 of divisor register is wasted

 Intuition: instead of shifting divisor to right, shift remainder to left…

 Step 1 cannot produce a 1 in quotient bit – as all bits corresponding


to the divisor in the remainder register are 0 (remember all operands
are 32-bit)
 Intuition: switch order to shift first and then subtract – can save 1
iteration…
Divide Version 2
Start

1. Shift the Remainder register left 1 bit

Divisor Remainder register is initialized 2. Subtract the Divisor register from the
left half of the Remainder register and
with the dividend at right place the result in the left half of the
32 bits
Remainder register

Quotient
32-bit ALU Shift left
Remainder >
– 0 Remainder < 0
32 bits Test Remainder

Shift left Control


Remainder
Write test
64 bits
3a. Shift the Remainder register to the 3b. Restore the original value by adding
left, setting the new rightmost bit to 0. the Divisor register to the left half of the
Also shift the Quotient register to the left Remainder register and place the sum
setting to the new rightmost bit to 1. in the left half of the Remainder register.
Divisor register, quotient register, Also shift the Remainder register to the
left, setting the new rightmost bit to 0
ALU are 32-bit wide; remainder Also shift the Quotient register to the left
setting to the new rightmost bit to 0.
register is 64-bit wide

No: < 32 repetitions


32nd repetition?

Yes: 32 repetitions

Why this correction step? We shall see later… Algorithm


Done. Shift left half of Remainder right 1 bit
Observations on Divide Version 2

 Each step the remainder register wastes space that exactly matches
the current size of the quotient

 Intuition: combine quotient register and remainder register…


Start

Divide Version 3 1. Shift the Remainder register left 1 bit

2. Subtract the Divisor register from the


left half of the Remainder register and
place the result in the left half of the
Remainder register

Divisor
Remainder >
– 0 Remainder < 0
32 bits Test Remainder

32-bit ALU
3a. Shift the Remainder register to the 3b. Restore the original value by adding
left, setting the new rightmost bit to 1 the Divisor register to the left half of the
Remainder register and place the sum
Shift right in the left half of the Remainder register.
Remainder Control Also shift the Remainder register to the
Shift left
test left, setting the new rightmost bit to 0
Write
64 bits

Remainder register is initialized with the dividend at right


No separate quotient register; quotient
No: < 32 repetitions
32nd repetition?

is entered on the right side of the 64-bit


remainder register Yes: 32 repetitions
Algorithm
Why this correction step? We shall see later… Done. Shift left half of Remainder right 1 bit
Divide Version 3
Start

Example: 0111 / 0010:


1. Shift the Remainder register left 1 bit

Itera- Step Divisor Remainder


2. Subtract the Divisor register from the tion
left half of the Remainder register and 0 init 0010 0000 0111
place the result in the left half of the
Remainder register 1 0010 0000 1110
1 2 0010 1110 1110
3b 0010 0001 1100
Remainder >
– 0
Test Remainder
Remainder < 0
2 …
3
4
3a. Shift the Remainder register to the 3b. Restore the original value by adding
left, setting the new rightmost bit to 1 the Divisor register to the left half of the
Remainder register and place the sum
in the left half of the Remainder register.
Also shift the Remainder register to the
left, setting the new rightmost bit to 0

No: < 32 repetitions


32nd repetition?

Yes: 32 repetitions

Done. Shift left half of Remainder right 1 bit Algorithm


Number of Iterations
 Why the extra iteration in Version 1?
 Why the final correction step in Versions 2 & 3?
Ovals represent loop iterations
Main insight – sub(i+1) must actually follow shifti of
the divisor (or remainder, depending on version) and
Shift: see the version descriptions the resulting bit in the quotient appears on shift(i+1)
for which registers are shifted

shift1 shift2 … shift32 shift33

sub1 sub2 sub3 … sub32 sub33

V1 starts loop One loop iteration


Critical situation! Only the quotient shift is
here: unnecessary necessary as it corresponds to the
sub step V2 & 3 start outcome of the previous sub.
loop here So V1 is ok even though the last divisor
shift is redundant, as final divisor is ignored
V2 & 3 initial step:
any way; V2 & 3 must repair remainder
before loop starts as it has shifted left one time too many
Observations on Divide Version 3
 Same hardware as Multiply Version 3

 Signed divide:
 make both divisor and dividend positive and perform division
 negate the quotient if divisor and dividend were of opposite signs
 make the sign of the remainder match that of the dividend
 this ensures always
 dividend = (quotient * divisor) + remainder
 –quotient (x/y) = quotient (–x/y) (e.g. 7 = 3*2 + 1 & –7 = –3*2 – 1)
MIPS Notes
 div (signed), divu (unsigned), with two 32-bit register
operands, divide the contents of the operands and put
remainder in Hi register and quotient in Lo; overflow is ignored
in both cases

 pseudo-instructions div (signed with overflow), divu


(unsigned without overflow) with three 32-bit register operands
puts quotients of two registers into third
Floating Point
 We need a way to represent
 numbers with fractions, e.g., 3.1416
 very small numbers (in absolute value), e.g., .00000000023
 very large numbers (in absolute value) , e.g., –3.15576 * 1046
 Representation:
binary point
 scientific: sign, exponent, significand form:
 (–1)sign * significand * 2exponent . E.g., –101.001101 * 2111001
 more bits for significand gives more accuracy
 more bits for exponent increases range
 if 1  significand  10two(=2ten) then number is normalized, except
for number 0 which is normalized to significand 0
 E.g., –101.001101 * 2111001 = –1.01001101 * 2111011 (normalized)
IEEE 754 Floating-point Standard
 IEEE 754 floating point standard:
 single precision: one word
31 bits 30 to 23 bits 22 to 0

sign 8-bit exponent 23-bit significand

 double precision: two words

31 bits 30 to 20 bits 19 to 0

sign 11-bit exponent upper 20 bits of 52-bit significand

bits 31 to 0

lower 32 bits of 52-bit significand


IEEE 754 Floating-point Standard
 Sign bit is 0 for positive numbers, 1 for negative numbers

 Number is assumed normalized and leading 1 bit of significand left of


binary point (for non-zero numbers) is assumed and not shown
 e.g., significand 1.1001… is represented as 1001…,
 exception is number 0 which is represented as all 0s (see next slide)
 for other numbers:
value = (–1)sign * (1 + significand) * 2exponent value

 Exponent is biased to make sorting easier


 all 0s is smallest exponent, all 1s is largest
 bias of 127 for single precision and 1023 for double precision
equals exponent value
 therefore, for non-0 numbers:
value = (–1)sign * (1 + significand) * 2(exponent – bias)
IEEE 754 Floating-point Standard
 Special treatment of 0:
 if exponent is all 0 and significand is all 0, then the value is
0 (sign bit may be 0 or 1)
 if exponent is all 0 and significand is not all 0, then the value is
(–1)sign * (1 + significand) * 2-127
 therefore, all 0s is taken to be 0 and not 2-127 (as would be for a non-zero
normalized number); similarly, 1 followed by all 0’s is taken to be 0 and not
- 2-127

 Example : Represent –0.75ten in IEEE 754 single precision


 decimal: –0.75 = –3/4 = –3/22
 binary: –11/100 = –.11 = –1.1 x 2-1
 IEEE single precision floating point exponent = bias + exponent value
= 127 + (-1) = 126ten = 01111110two
 IEEE single precision: 10111111010000000000000000000000
exponent significand
sign
Start

Floating Point 1. Compare the exponents of the two numbers.

Addition
Shift the smaller number to the right until its
exponent would match the larger exponent

2. Add the significands

 Algorithm: 3. Normalize the sum, either shifting right and


incrementing the exponent or shifting left
and decrementing the exponent

Overflow or Yes
underflow?

No
Exception

4. Round the significand to the appropriate


number of bits

No
Still normalized?

Yes

Done
Floating Point
Addition Sign Exponent Significand Sign Exponent Significand

Compare
Small ALU exponents
 Hardware:
Exponent
difference

0 1 0 1 0 1

Shift smaller
Control Shift right
number right

Add
Big ALU

0 1 0 1

Increment or
decrement Shift left or right Normalize

Rounding hardware Round

Sign Exponent Significand


Start

Floating Point 1. Add the biased exponents of the two


numbers, subtracting the bias from the sum

Multpication
to get the new biased exponent

2. Multiply the significands

 Algorithm: 3. Normalize the product if necessary, shifting


it right and incrementing the exponent

Overflow or Yes
underflow?

No
Exception

4. Round the significand to the appropriate


number of bits

No
Still normalized?

Yes

5. Set the sign of the product to positive if the


signs of the original operands are the same;
if they differ make the sign negative

Done
Floating Point Complexities
 In addition to overflow we can have underflow (number too
small)
 Accuracy is the problem with both overflow and underflow
because we have only a finite number of bits to represent
numbers that may actually require arbitrarily many bits
 limited precision  rounding  rounding error
 IEEE 754 keeps two extra bits, guard and round
 four rounding modes
 positive divided by zero yields infinity
 zero divide by zero yields not a number
 other complexities
 Implementing the standard can be tricky
 Not implementing the standard can be even worse
 see text for discussion of Pentium bug!
MIPS Floating Point
 MIPS has a floating point coprocessor (numbered 1, SPIM) with
thirty-two 32-bit registers $f0 - $f31. Two of these are required
to hold doubles. Floating point instructions must use only even-
numbered registers (including those operating on single floats).
SPIM simulates MIPS floating point.

 Floating point arithmetic: add.s (single addition), add.d


(double addition), sub.s, sub.d, mul.s, mul.d, div.s,
div.d

 Floating point comparison: c.x.s (single), c.x.d (double),


where x may be eq, neq, lt, le, gt, ge

 Other instructions…
Summary
 Computer arithmetic is constrained by limited precision
 Bit patterns have no inherent meaning but standards do exist:
 two’s complement
 IEEE 754 floating point
 Computer instructions determine meaning of the bit patterns.
 Performance and accuracy are important so there are many
complexities in real machines (i.e., algorithms and
implementation)

 Read Computer Arithmetic Algorithms by I. Koren


 it is easy-to-read and shows new algorithms for arithmetic
 there will be assignment and projects based on Koren’s material

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy