Docsity

Floating Point Arithmetic: Extra Bits for Accuracy and Rounding | CS 472, Study notes of Computer Architecture and Organization

Partial preview of the text

Download Floating Point Arithmetic: Extra Bits for Accuracy and Rounding | CS 472 and more Study notes Computer Architecture and Organization in PDF only on Docsity!

CS472 1

FP Arithmetic

Extra Bits for Accuracy & Rounding

CS472 2

Floating Point Subtraction

Assume both A, B > 0 and A ≥ B ⇒ e 1 ≥ e (^2) ⇒ we need to increase e 2 to = e (^1) ⇒ shift f 2 right (e 1 ≥ e 2 ) times

case 1: e 1 = e (^2) A = 1.f 1 B = - 1.f 2 0.f 3

May have several leading zeros. Answer will be shifted left to normalize. Zeros will be inserted on the right.

CS472 3

Floating Point Subtraction

case 2: e 1 - e 2 = 1 A = 1.f 1 1. Bnew = - 0.f 2 0. 1 b (^23) 0 < diff < 1. Bit 23 is dropped because we had to shift B right 1 place. If we had one extra bit, we could calculate the exact answer.

CS472 4

Floating Point Subtraction

case 3: e 1 - e 2 ≥ 2 A = 1. Bnew = 0. 01 b 22 b (^23)

Bnew < 0. A - Bnew > 0.

⇒ A - Bnew = 1. or = 0. 1 ⇒ A - Bnew will only have to be shifted left at most once to normalize. We need one extra bit on the right.

CS472 7

IEEE 754 Rounding Modes

  • Always round up (toward + infinity)
  • Always round down (toward - infinity)
  • Truncate
  • Nearest even (the most commonly used mode) Nearest even rounds to the closest number. When we are half way in-between numbers, we round up half the time and round down half the time. Specifically, when the least significant retained bit is odd (a 1), round up. Else round down.

CS472 8

IEEE 754 Rounding Modes

X’s are number that can be represented exactly in binary 0’s are number we want to represent

X 0 X X X 0 X X 0 X X X 0 X

Always Round Up Always Round Down

CS472 9

IEEE 754 Rounding Modes

X’s are number that can be represented exactly in binary 0’s are number we want to represent

X 0 X X X 0 X

Truncate

CS472 10

IEEE 754 Nearest Even Rounding Mode

X’s are number that can be represented exactly in binary 0’s are number we want to represent

X 0 X X X 0 X X 0 X X X 0 X

X 0 X X X X depends on lsb

0 depends on lsb

CS472 13

Average Error

Truncate average error. (x > 0)

x f(x) error = f(x) - x x.00 x 0 x.01 x -1/ x.10 x -1/ x.11 x -3/

average error = -3/

CS472 14

Average Error

Nearest even average error. (x > 0) x f(x) error = f(x) - x x0.00 x0 0 x0.01 x0 -1/ x0.10 x0 -1/ x0.11 x1 1/ x1.00 x1 0 x1.01 x1 -1/ x1.10 x1+1 1/ x1.11 x1+1 1/

average error = 0

CS472 15

Example

Find C = A - B. (Answer is negative because A<0, -B<0) Use the 3 extra bits: Guard, Round, and Sticky. Round to nearest even. A =

S exp fraction 1 1000 0100 0000 1111 1111 1111 1111 111 B =

S exp fraction 0 1000 0000 1111 1111 1111 1111 1111 101

e = 5 e = 1 A = 1.0000 1111 1111 1111 1111 111 0 0 0 Bn = 0.0001 1111 1111 1111 1111 111 1 1 1 Sum = 1.0010 1111 1111 1111 1111 110 1 1 1 normalized 1.0010 1111 1111 1111 1111 111 round up

G R S

CS472 16

Rounding Modes

Let p = proper number of significant bits in the significand Sump is the least significant bit in the significand A blank box means that the p most significant bits in the significand are correctly rounded If a formula evaluates to true, add 1 Sump Else, add 0 to Sump Rounding Mode Sum ≥ 0 Sum< Round Down g+r+s Round Up g+r+s Truncate Nearest Even g(r+s+Sum (^) p) g(r+s+Sum (^) p )