# Lecture 13 - Floating Point

## Practical Details

1. Assignment 2, giving back.
2. Assignment 3
3. One assignment 1 without a name
4. Starting to add to `Printed Notes'.

## Multiplication

Long multiplication, the algorithm you know and love from grade school

1. Multiply the right most digit by the multiplicand
• result is first step toward product
2. Repeat until no more digits
1. multiply next digit by multiplicant
3. shift result left by amount in shifter

This is hard because multiplying base ten is hard

• What is 7 times 8?
• Was I carrying five or six?

But we can all multiply by 1 and 0, no problem, so try this algorithm

product is zero

Repeat 32 times

1. If LSB of multiplier is 1 then
2. Shift multiplier 1 bit right
3. Shift multiplicand 1 bit left

#### Version 2

Shift product register instead of multiplicand register

#### Version 3

High word of product is zero; low word of product is multiplier

Repeat 32 times

1. If LSB of product is 1 then
• add multiplicand to high word of product
2. Shift product 1 bit right.

### Barrel shifter

I hope that you (not equal to `You may have') noticed that a lot of shifting is done.

• For this reason the barrel shifter is just as important as the ALU in making a CPU fast.

A barrel shifter can be

• left/right shifting (LS/RS)
• cyclic:
• RS: low order bits fill in high order bits
• LS: high order bits fill in low order bits
• sign-extending:
• RS: ones or zeros fill in high order bits
• LS: ones or zeros fill in low order bits
• etc.

Once you understand the basic hardware implementation of a barrel shifter the variations are obvious

#### Implementation

Implement using multiplexors

Example: 4-bit barrel shifter

• from 4to1 multiplexors
• from 2 to 1 multiplexors

# Floating Point Numbers

## Representation

#### Sign, exponent, significand (mantissa)

• 1 bit for sign
• 8 bits for exponent (7 bit bias in representation)
• 23 bits for significand
• the fractional part of a binary number between 1 and 2: 1.xxxxxxxxxxxx (23 x's)
• Store only the x's
• This is what it means for the significand to be normalized.

Value is sign * (1 + significand) * 2^(exponent - bias)

#### Distribution

Clustering matters in practice

• smallest significant: ss = 1
• largest significand: ls = 2^(23)
```Exponent |    smallest |    largest |     delta
================================================
bias  |          -2 |          2 |    2^(-23)
0  |   -2^(-127) |   2^(-127) |   2^(-151)
256  |    -2^(129) |    2^(129) |    2^(105)
================================================
```

#### Double Precision

64 bit number

• 1 bit for sign
• 11 bits for exponent
• 52 bits for significand

And there is even extended precision

#### What goes wrong

2. Multiplication/division

Importance of NaN.

Where are these problems observed?

• Computer graphics
• Statistics

## Algorithms for Addition and Multiplication

1. Equalize exponents
1. Shift right significand of number with smaller exponent
2. Add 1 to its exponent
3. Until exponents are equal
3. Normalize result
1. Shift significand left or right
2. Add 1 to or subtract 1 from exponent
3. Until significand is normalized

#### Multiplication (Division)

2. Multiply significands
3. Normalize result

#### Pathologies

Exponent can overflow or underflow.

Significand can underflow

#### NaN (Not a Number)

Possible values

1. Too large to represent
2. Too small to represent
3. Infinity
4. Underflow