You are on page 1of 7

Decimal Division Binary Division

• Remember 4th grade long division? 101011 = 43


43 // quotient 12 √521 = 01100 √ 01000001001
12 √521 // divisor √ dividend -384 = - 0110000000
-480 137 = 010001001
41 - 0 = - 0
- 36 137 = 010001001
5 // remainder - 96 = - 01100000
41 = 0101001
- 0 = - 0
• Shift divisor left (multiply by 10) until MSB lines up with dividend’s 41 = 0101001
• Repeat until remaining dividend (remainder) < divisor - 24 = - 011000
• Find largest single digit q such that (q*divisor) < dividend 17 = 010001
• Set LSB of quotient to q - 12 = - 01100
• Subtract (q*divisor) from dividend 5 = 101
• Shift quotient left by one digit (multiply by 10)
• Shift divisor right by one digit (divide by 10)
© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
55 56
from Roth and Lebeck from Roth and Lebeck

Hardware for Binary Division Simple 16-bit Divider Circuit


Divisor Quotient
• Same as decimal division, except (again) 32>>1 16<<1
– More individual steps (base is smaller) 32
+ Each step is simpler
32– control
• Find largest bit q such that (q*divisor) < dividend 32
32 <0
• q = 0 or 1 Remainder write
• Subtract (q*divisor) from dividend 32
• q = 0 or 1 → no actual multiplication, subtract divisor or not dividend

• First: Shift Divisor left to align it with Dividend


• One complication: largest q such that (q*divisor) < dividend
• Then repeat this loop until Divisor<Remainder
• How to know if (1*divisor) < dividend?
• Subtract Divisor from Remainder (Remainder initially = Dividend)
• Human (e.g., ECE 152 student) can eyeball this
• Result >= 0? RemainderResult, write 1 into Quotient LSB
• Computer cannot
• Result < 0? Just write 0 into quotient LSB
• Subtract and see if result is negative
• Shift divisor right 1 bit, shift quotient left 1 bit
© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
57 58
from Roth and Lebeck from Roth and Lebeck
Even Better Divider Circuit Summary of Integer Arithmetic and ALU
Divisor • Addition
16 • Half adder full adder, ripple carry
16 • Fast addition: carry select and carry lookahead
• Subtraction as addition
16– control
16 Dividend • Barrel shifter and shift registers
16 16
Remainder/Quotient • Multiplication
32<<1 • N-step multiplication (3 refined implementations)
• Booth’s algorithm and N/2-step multiplication
• Multiplier circuit optimizations also work for divider • Division
• Shift Remainder left and do 16-bit subtractions
• Combine Quotient with right (unused) half of Remainder
• Booth and modified Booth analogs (but really nasty)
• Multiplication and division in one circuit (how?)
© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
59 60
from Roth and Lebeck from Roth and Lebeck

This Unit: Arithmetic and ALU Design Floating Point Arithmetic


Application Application
• Integer Arithmetic and ALU • Formats
OS OS
• Binary number representations • Precision and range
Compiler Firmware Compiler Firmware
• Addition and subtraction • IEEE 754 standard
CPU I/O • The integer ALU CPU I/O • Operations
• Shifting and rotating • Addition and subtraction
Memory Memory
• Multiplication • Multiplication and division
Digital Circuits • Division Digital Circuits
• Error analysis
Gates & Transistors • Floating Point Arithmetic Gates & Transistors
• Error and bias
• Binary number representations • Rounding and truncation
• FP arithmetic
• Only scientists care?
• Accuracy

© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
61 62
from Roth and Lebeck from Roth and Lebeck
Floating Point (FP) Numbers The World Before Floating Point
• Floating point numbers: numbers in scientific notation • Early computers were built for scientific calculations
• Two uses • ENIAC: ballistic firing tables

• Use #1: real numbers (numbers with non-zero fractions) • But didn’t have primitive floating point data types
• 3.1415926… • Circuits were big
• 2.1878… • Many accuracy problems
• 9.8
• 6.62 * 10–34 • Programmers built scale factors into programs
• 5.875 • Large constant multiplier turns all FP numbers to integers
• Before program starts, inputs multiplied by scale factor manually
• Use #2: really big numbers • After program finishes, outputs divided by scale factor manually
• 3.0 * 108 • Yuck!
• 6.02 * 1023
© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
63 64
from Roth and Lebeck from Roth and Lebeck

The Fixed Width Dilemma Range and Precision


• “Natural” arithmetic has infinite width • Range
• Infinite number of integers • Distance between largest and smallest representable numbers
• Infinite number of reals • Want big range
• Infinitely more reals than integers (head… spinning…) • Precision
• Distance between two consecutive representable numbers
• Hardware arithmetic has finite width N (e.g., 16, 32, 64) • Want small precision
• Can represent 2N numbers
• If you could represent 2N integers, which would they be? • In fixed bit width, can’t have unlimited both
• Easy! The 2N–1 on either size of 0
• If you could represent 2N reals, which would they be?
• 2N reals from 0 to 1, not too useful
• 2N powers of two (1, 2, 4, 8, …), also not too useful
• Something in between: yes, but what?
© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
65 66
from Roth and Lebeck from Roth and Lebeck
Scientific Notation IEEE 754 Standard Precision/Range
• Scientific notation: good compromise • Single precision: float in C
• Number [S,F,E] = S * F * 2E
• 32-bit: 1-bit sign + 8-bit exponent + 23-bit significand
• S: sign
• Range: 2.0 * 10–38 < N < 2.0 * 1038
• F: significand (fraction)
• Precision: ~7 significant (decimal) digits
• E: exponent
• “Floating point”: binary (decimal) point has different magnitude
• Double precision: double in C
+ “Sliding window” of precision using notion of significant digits • 64-bit: 1-bit sign + 11-bit exponent + 52-bit significand
• Small numbers very precise, many places after decimal point • Range: 2.0 * 10–308 < N < 2.0 * 10308
• Big numbers are much less so, not all integers representable • Precision: ~15 significant (decimal) digits
• But for those instances you don’t really care anyway
– Caveat: most representations are just approximations
• Sometimes weirdos like 0.9999999 or 1.0000001 come up • Numbers >10308 don’t come up in many calculations
+ But good enough for most purposes • 1080 ~ number of atoms in universe

© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
67 68
from Roth and Lebeck from Roth and Lebeck

How Do Bits Represent Fractions? Some Examples


• Sign: 0 or 1 → easy • What is 5 in floating point?
• Exponent: signed integer → also easy • Sign: 0
• 5 = 1.25 * 22
• Significand: unsigned fraction → not obvious!
• Significand: 1.25 = 1*20 + 1*2–2 = 101 0000 0000 0000 0000 0000
• Exponent: 2 = 0000 0010
• How do we represent integers?
• Sums of positive powers of two
• What is –0.5 in floating point?
• S-bit unsigned integer A: AS–12S–1 + AS–22S–2 + … + A121 + A020
• Sign: 1
• So how can we represent fractions?
• 0.5 = 0.5 * 20
• Sums of negative powers of two
• Significand: 0.5 = 1*2–1 = 010 0000 0000 0000 0000 0000
• S-bit unsigned fraction A: AS–120 + AS–22–1 + … + A12–S+2 + A02–S+1
• Exponent: 0 = 0000 0000
• More significant bits correspond to larger multipliers

© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
69 70
from Roth and Lebeck from Roth and Lebeck
Normalized Numbers More About Normalization
• Notice • What is –0.5 in normalized floating point?
• 5 is 1.25 * 22 • Sign: 1
• But isn’t it also 0.625 * 23 and 0.3125 * 24 and …? • 0.5 = 1 * 2–1
• With 8-bit exponent, we can have 8 representations of 5 • Significand: 1 = 1*20 = 100 0000 0000 0000 0000 0000
• Multiple representations for one number is bad idea • Exponent: -1 = 1111 1111 (assuming 2’s complement for now)
– Would lead to computational errors
– Would waste bits • IEEE 754: no need to represent coefficient of 20 explicitly
• Solution: choose normal (canonical) form • It’s always 1
• Disallow de-normalized numbers (some exceptions later) + Buy yourself an extra bit of precision
• IEEE 754 normal form: coefficient of 20 is always 1 • Pretty cute trick
• Similar to scientific notation: one non-zero digit left of decimal
• Normalized representation of 5 is 1.25 * 22 (1.25 = 1*20+1*2-2) • Problem: what about 0?
• 0.625 * 23 is de-normalized (0.625 = 0*20+1*2-1+ 1*2-3) • How can we represent 0 if 20 is always implicitly 1?
© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
71 72
from Roth and Lebeck from Roth and Lebeck

IEEE 754: The Whole Story IEEE 754: Continued


• Exponent: signed integer → not so fast • Notice: two exponent bit patterns are “unused”
• Exponent represented in excess or bias notation
• N-bits typically can represent signed numbers from –2N–1 to 2N–1–1 • 0000 0000: represents de-normalized numbers
• But in IEEE 754, they represent exponents from –2N–1+2 to 2N–1–1 • Numbers that have implicit 0 (rather than 1) in 20
• And they represent those as unsigned with an implicit 2N–1–1 added • Zero is a special kind of de-normalized number
• Implicit added quantity is called the bias + Exponent is all 0s, significand is all 0s
• Actual exponent is E–(2N–1–1) – There are both +0 and –0, but they are considered the same
• Example: single precision (8-bit exponent) • Also represent numbers smaller than smallest normalized numbers
• Bias is 127, exponent range is –126 to 127
• –126 is represented as 1 = 0000 0001 • 1111 1111: represents infinity and NaN
• 127 is represented as 254 = 1111 1110 • ± infinities have 0s in the significand
• 0 is represented as 127 = 0111 1111 • ± NaNs do not
• 1 is represented as 128 = 1000 0000
© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
73 74
from Roth and Lebeck from Roth and Lebeck
IEEE 754: To Infinity and Beyond IEEE 754: Final Format
• What are infinity and NaN used for? exp significand
• To allow operations to proceed past overflow/underflow situations
• Overflow: operation yields exponent greater than 2N–1–1 • Biased exponent
• Underflow: operation yields exponent less than –2N–1+2
• Normalized significand
• Exponent uses more significant bits than significand
• IEEE 754 defines operations on infinity and NaN
• Helps when comparing FP numbers
• N / 0 = infinity
• Exponent bias notation helps there too – why?
• N / infinity = 0
• 0 / 0 = NaN
• Infinity / infinity = NaN
• Every computer since about 1980 supports this standard
• Infinity – infinity = NaN • Makes code portable (at the source level at least)
• Anything and NaN = NaN • Makes hardware faster (stand on each other’s shoulders)
• Will not test you on these rules

© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
75 76
from Roth and Lebeck from Roth and Lebeck

Floating Point Arithmetic FP Addition


• We will look at • Assume
• Addition/subtraction • A represented as bit pattern [SA, EA, FA]
• Multiplication/division • B represented as bit pattern [SB, EB, FB]
• What is the bit pattern for A+B [SA+B, EA+B, FA+B]?
• Implementation • [SA+SB, EA+EB, FA+FB]? Nope!
• Basically, integer arithmetic on significand and exponent • So what is it then?
• Using integer ALUs
• Plus extra hardware for normalization

• To help us here, look at toy “quarter” precision format


• 8 bits: 1-bit sign + 3-bit exponent + 4-bit significand
• Bias is 3 (= 2N-1 – 1)

© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
77 78
from Roth and Lebeck from Roth and Lebeck
FP Addition Decimal Example FP Addition (Quarter Precision) Example
• Let’s look at a decimal example first: 99.5 + 0.8 • Now a binary “quarter” example: 7.5 + 0.5
• 9.95*101 + 8.0*10-1 • 7.5 = 1.875*22 = 0 101 11110 (the 1 is the implicit leading 1)
• 1.875 = 1*20+1*2-1+1*2-2+1*2-3
• 0.5 = 1*2-1 = 0 010 10000
• Step I: align exponents (if necessary)
• Temporarily de-normalize operand with smaller exponent
• Add 2 to its exponent → must shift significand right by 2
• Step I: align exponents (if necessary)
• 0 010 10000 → 0 101 00010
• 8.0* 10-1 → 0.08*101
• Add 3 to exponent → shift significand right by 3
• Step II: add significands • Step II: add significands
• 9.95*101 + 0.08*101 → 10.03*101 • 0 101 11110 + 0 101 00010 = 0 101 100000
• Step III: normalize result • Step III: normalize result
• Shift significand right by 1 and then add 1 to exponent • 0 101 100000 → 0 110 10000
• 10.03*101 → 1.003*102 • Shift significand right by 1 → add 1 to exponent

© 2012 Daniel J. Sorin ECE 152 © 2012 Daniel J. Sorin ECE 152
79 80
from Roth and Lebeck from Roth and Lebeck

You might also like