numbers and arithmetic · dr. shadrokh samavi 5 course learning objectives 1. explain the relative...

47
Dr. Shadrokh Samavi 1 Numbers and Arithmetic

Upload: others

Post on 22-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

  • ‹#›Dr. Shadrokh Samavi 1

    Numbers and Arithmetic

  • ‹#›Dr. Shadrokh Samavi 2

    Computer Arithmetic

    Website: http://ece.iut.ac.ir/faculty/samavi/computer_arithmetic.htm

    Text: Parhami, Behrooz, “Computer Arithmetic: Algorithms and Hardware Designs” Oxford University Press, 2000. http://www.ece.ucsb.edu/Faculty/Parhami/text_comp_arit.htm

    Part 1: Number RepresentationPart 2: Addition/SubtractionPart 3: MultiplicationPart 4: DivisionPart 5: Real Arithmetic (Floating-Point)Part 6: Function EvaluationPart 7: Implementation Topics

    Slides are intended to illustrate the content of Parhami’s book.

  • ‹#›Dr. Shadrokh Samavi 3

    Part I Number Representation

    1 Numbers and Arithmetic

    2 Representing Signed Numbers

    3 Redundant Number Systems

    4 Residue Number Systems

    Part II Addition/Subtraction

    5 Basic Addition and Counting

    6 Carry-Lookahead Adders

    7 Variations in Fast Adders

    8 Multioperand Addition

    Part III Multiplication

    9 Basic Multiplication Schemes

    10 High-Radix Multipliers

    11 Tree and Array Multipliers

    12 Variations in Multipliers

  • ‹#›Dr. Shadrokh Samavi 4

    Part IV Division

    13 Basic Division Schemes

    14 High-Radix Dividers

    15 Variations in Dividers

    16 Division by Convergence

    Part V Real Arithmetic

    17 Floating-Point Representations

    18 Floating-Point Operations

    19 Errors and Error Control

    20 Precise and Certifiable Arithmetic

    Part VI Function Evaluation

    21 Square-Rooting Methods

    22 The CORDIC Algorithms

    23 Variations in Function Evaluation

    24 Arithmetic by Table Lookup

    Part VII Implementation Topics

    25 High-Throughput Arithmetic

    26 Low-Power Arithmetic

    27 Fault-Tolerant Arithmetic

    28 Past, Present, and Future

  • ‹#›Dr. Shadrokh Samavi 5

    Course Learning Objectives

    1. explain the relative merits of number systems usedby arithmetic circuits including both fixed- andfloating-point number systems.

    2. demonstrate the use of key acceleration algorithmsand hardware for addition/subtraction, multiplication,and division, plus certain functions.

    3. distinguish between the relative theoretical meritsof the different acceleration schemes.

  • ‹#›Dr. Shadrokh Samavi 6

    4. identify the implementation limitations constrainingthe speed of acceleration schemes

    5. evaluate, design, and optimize arithmetic circuitsfor low-power

    6. evaluate, design, and optimize arithmetic circuitsfor precision

    Course Learning Objectives

  • ‹#›Dr. Shadrokh Samavi 7

    7. design, simulate, and evaluate an arithmetic circuitusing appropriate references including current journaland conference literature.

    8. write a paper compatible with journal formatstandards on an arithmetic design.

    9. make a professional presentation with strongtechnical content and audience interaction.

    Course Learning Objectives

  • ‹#›Dr. Shadrokh Samavi 8

    1.1 What Is Computer Arithmetic?

  • ‹#›Dr. Shadrokh Samavi 9

    Pentium Division Bug (1994-95): Pentium’s radix-4 SRTalgorithm occasionally produced an incorrect quotient. Firstnoted in 1994 by T. Nicely who computed sums of reciprocals oftwin primes:

    1/5 + 1/7 + 1/11 + 1/13 + . . . +1/p + 1/(p + 2) + . . .

    Worst-case example of division error in Pentium:

    4 195 8353 145 727

    1.333 820 44...1.333 739 06...

    c ==Correct quotient

    double FLP value;accurate to only 14 bits(worse than single!)

  • ‹#›Dr. Shadrokh Samavi 10

    Scope of computer arithmetic.

  • ‹#›Dr. Shadrokh Samavi 11

    1.2 A Motivating Example

  • ‹#›Dr. Shadrokh Samavi 12

  • ‹#›Dr. Shadrokh Samavi 13

    Patriot Missile battery once failed to intercept an incomingScud missile which killed 28. Reported cause: “softwareproblem” (inaccurate calculation of the time since boot).Specifics of the problem: time in tenths of second asmeasured by the system’s internal clock was multiplied by1/10 to get the time in seconds. Internal registers were24 bits wide

    Patriot Missile

  • ‹#›Dr. Shadrokh Samavi 14

    1/10 = 0.0001 1001 1001 1001 1001 100 (chopped to 24 b)Error ≅ 0.1100 1100 × 2–23 ≅ 9.5 × 10–8Error in 100-hr operation period:≅ 9.5 × 10–8 × 100 × 60 × 60 × 10 = 0.34 sDistance traveled by Scud = (0.34 s) × (1676 m/s) ≅ 570 mThis put the Scud outside the Patriot’s “range gate”.Ironically, the fact that the bad time calculation had beenimproved in some (but not all) code parts. It meant thatinaccuracies did not cancel out.

    Patriot Missile

  • ‹#›Dr. Shadrokh Samavi 15

    Importance of Computer Arithmetic

    3.2 GHz Pentium has a clock cycle of 0.31 ns. Can oneinteger addition be done < 0.31 ns in execution stageof Pipeline?

    What if you had to build a 32-bit adder – ripple carryand a gate delay was approximately 0.1 ns?

    11011110

    11011-Note: added from right to left.

    STEP 1

  • ‹#›Dr. Shadrokh Samavi 16

    Ripple-carry Structure

    STEP 2 – Design a circuit

    – Each box is a full-adder

    c32 z31 z1 z0

    c31 c2 c1 c0=0

    x31 y31 x1 y1 x0 y0

  • ‹#›Dr. Shadrokh Samavi 17

    Full-Adder Implementation

    x y cin z cout0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1 1 1 1

    xy

    cin00 01 11 10

    0

    1

    0 1 0 1

    1 0 1 0

    xycin

    00 01 11 10

    0

    1

    0 0 1 0

    0 1 1 1

    in in in in

    in

    z c x y c x y c x y c x y

    c x y

    ( )out inc c x y xy cout = cinx + ciny + x y

  • ‹#›Dr. Shadrokh Samavi 18

    Adder Circuit Analysis

    STEP 3 – Analysis

    Critical Path is carry chain, 2 gate delays/bit2 32 = 64(64)(0.1ns) = 6.4ns6.4ns >> 0.31nsMust Use Faster Adder and/or Pipeline More!!!!

  • ‹#›Dr. Shadrokh Samavi 19

    Addition Paradigms

    • right to left serial1

    147865+30921178786

    • right to left, parallel147865+30921177786001000178786

  • ‹#›Dr. Shadrokh Samavi 20

    1.3 Numbers and Their Encodings

  • ‹#›Dr. Shadrokh Samavi 21

    Numbers versus their representations (numerals)The number “twenty-seven” can be represented in differentways using numerals or numeration systems:||||| ||||| ||||| ||||| ||||| || sticks or unary code 27 radix-10 ordecimal code (27)ten 11011 radix-2 or binary code (11011)twoXXVII Roman numeralsEncoding of digit sets as binary strings: BCD example

    Digit BCD representation0 0 0 0 01 0 0 0 12 0 0 1 03 0 0 1 14 0 1 0 05 0 1 0 16 0 1 1 07 0 1 1 18 1 0 0 09 1 0 0 1

  • ‹#›Dr. Shadrokh Samavi 22

    Number Systems: “Roman” Numeral System

    Symbolic Digitssymbol value

    I 1 V 5 X 10 L 50 C 100 D 500 M 1000

    RULES:• If symbol is repeated or lies to

    the right of another higher-valuedsymbol, value is additive

    XX=10+10=20CXX=100+10+10=120

    • If symbol is repeated or lies to theleft of a higher-valued symbol,value is subtractive

    XXC = - (10+10) + 100 = 80XLVIII = -(10) + 50 + 5 + 3 = 48

  • ‹#›Dr. Shadrokh Samavi 23

    Weighted Positional Number System

    Example: “Arabic” Number System

    symbol (digit)

    value (in 1’s position)

    0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

  • ‹#›Dr. Shadrokh Samavi 24

    1.4 Fixed-Radix Positional Number Systems

  • ‹#›Dr. Shadrokh Samavi 25

    Binary Number System

    • n-ordered sequence:

    • each xi{0,1} is a BInary digiT (BIT)

    • magnitude of n is important• sequence is a short-hand notation• more precise definition is:

    1 2 2 1 0n nx x x x x

    • This is a radix-polynomial form

  • ‹#›Dr. Shadrokh Samavi 26

    Number SystemA Number System is defined if the followings exist

    Example: The binary number system 1.2.3. Addition operator defined by addition table4. Multiplication operator defined by multiplication table

    1. A digit set2. A radix or base value3. An addition operation4. A multiplication operation

    {0,1}ix 2

    + 0 1 0 0 1 1 1 10

    0 10 0 01 0 1

  • ‹#›Dr. Shadrokh Samavi 27

    Number System Observations• Cardinality of digit set (2) is equal to radix value• Addition operator XOR, Multiplication is AND

    •How many integers exist?Mathematically, there are an infinite number, In computer, finite range due to register length

    minX

    maxX

    min max[ , ]X X

    smallest representable numberlargest representable number

    range of representable numbers[-inclusive; (-exclusive interval bounds

    •When ALU produces a result >Xmax or

  • ‹#›Dr. Shadrokh Samavi 28

    Encoding of numbers in 4 bits

  • ‹#›Dr. Shadrokh Samavi 29

    Encoding of numbers in 4 bits

  • ‹#›Dr. Shadrokh Samavi 30

    Negative Radix Number System

  • ‹#›Dr. Shadrokh Samavi 31

    Example•Assume 4-bit registers, unsigned binary numbers

    min 2 100000 0X max 2 101111 15X

    min max 2 2[ , ] [0000 ,1111 ]X X

    max 2 101 10000 16X

    max 2 21 [0000 ,1111 ] (mod16)X X

    1101 130110 6

    1 0011 19

    XY 19(mod16) 3

    Answer in register is 00112=310Overflow

  • ‹#›Dr. Shadrokh Samavi 32

    4 3 1 07 8 6 8 2 8 4 8X

    876024X

    Since octal is fixed-radix and positional,we can rewrite this value using shorthand notation

    Note the importance of the use of 0 to serve as acoefficient of the weight value w2=82

    Example

  • ‹#›Dr. Shadrokh Samavi 33

    Fixed-Radix Systems

    1 2 1 01 2 1 0

    11

    1

    k kk k

    km i

    m ii m

    X x x x x

    x x x

    Register of length n can represent a number with a fractional part and an integral partk – number of integral digitsm – number of fractional digitsn = k + m

    1 2 1 0 1.k k mx x x x x x radix point

    A programmer can use an implied radix point in any position

  • ‹#›Dr. Shadrokh Samavi 34

    Scaling Factors

    Fixed point arithmetic can utilize scaling factors to adjust radix point position

    a – scaling factor

    2

    ( )aX aY a X YaX aY a XY

    aX XaY Y

  • ‹#›Dr. Shadrokh Samavi 35

    Unit in the Last Position (ulp)

    • Given w0=r-m and n, the position of the radix point is determined

    • Simpler to disregard position of the radix point in fixed point by using unit in least (significant) position ulp

    For fractionsFor integers

    mulp r

    Example98.675101 ulp = 110-3=0.001

    1 ulp is the smallest amount a fixed point number may increase or decrease

    ulp = 1

  • ‹#›Dr. Shadrokh Samavi 36

    1.5 Number Radix Conversion

  • ‹#›Dr. Shadrokh Samavi 37

    Converting whole part w: (105)ten = (?)fiveRepeatedly divide by five Quotient Remainder

    105 0 21 1 4 4 0

    Therefore, (105)ten = (410)five

    Converting fractional part v: (105.486)ten = (410.?) Repeatedly multiply by five Whole Part Fraction

    .486 2 .430 2 .150 0 .750 3 .750 3 .750

    Therefore, (105.486)ten ≅ (410.22033)five = w.v

    Radix Conversion

  • ‹#›Dr. Shadrokh Samavi 38

    Horner’s rule used to convert (22033)five to decimal.

    Radix Conversion

  • ‹#›Dr. Shadrokh Samavi 39

    Horner’s rule used to convert (0.22033)five to decimal.

    Radix Conversion

  • ‹#›Dr. Shadrokh Samavi 40

    Radix Conversion

    Given a number in old radix r, conversion to the new radix R representation

    - can be accomplished doing the arithmetic in the old or new radix

    - old and new representations may not be exactly equal

  • ‹#›Dr. Shadrokh Samavi 41

    Given a value X represented in source system with radix s, represent the same number in a destination system with radix d

    Consider the integral part of the number, XI, in the d system1 2 1 0

    1 2 1 0

    1 2 2 1 0{[( ) ] }0

    k kI k d k d d d

    k d k d d d

    i d

    X x x x xx x x x x

    x

    1 2 2 1

    0

    {[( ) ] }k d k d dQ x x x xDesired LSD x

    Can Repeatedly Divide to Obtain Converted Value

    Consider the integral part of the number, XI, in the d system

    If XI is divided by d , we obtain x0 as a remainder and quotient

    Radix Conversion

  • ‹#›Dr. Shadrokh Samavi 42

    Radix Conversion Example

    XI = 34610 s=10 d =3

    5 4 3 2 1 0

    10 10

    1 3 1 3 0 3 2 3 1 3 1 3[243 81 18 3 1] 346

    Fixed-point Decimal to Ternary Integer Conversion, (arithmetic in old radix)

    Check by evaluating the radix polynomial

    XI = 1102113

  • ‹#›Dr. Shadrokh Samavi 43

    Radix Conversion (fractional)

    Consider the fractional part of the value in d Fixed point system

    Thus, PI is the desired digitWe can repeatedly multiply by the d value

    1 2 ( 1)1 2 ( 1)

    1 1 11 2 3

    11 1

    2 3

    { [ ( )]}

    [ ( )]

    m mF d d m d m d

    d d d

    d F I F

    I

    F d d

    X x x x x

    x x xX P PP x

    P x x

  • ‹#›Dr. Shadrokh Samavi 44

    XI = 0.29110 s=10 d =5

    0.291 5 1.455 1

    Fixed-point Decimal to Pentary Fractional Conversion(arithmetic in old radix)

    0.29110 is Finite Fraction for s=10, but infinite fraction for d =5

    0.455 5 2.275 2 0.275 5 1.375 1 0.375 5 1.875 1 0.875 5 4.375 4 0.375 5 1.875 1 0.875 5 4.375 4

    10 50.291 (0.12114141414 )

    Radix Conversion Example

  • ‹#›Dr. Shadrokh Samavi 45

    1.6 Classes of Number Representations

  • ‹#›Dr. Shadrokh Samavi 46

    1. Integers (fixed-point), unsigned & signed

    2. Signed-magnitude, biased, complement

    3. Signed-digit (redundant numbers )

    4. Residue number system: (RNS)

    5. Real numbers, floating-point

    6. Real numbers, exact

    Classes of Number Representations

  • ‹#›Dr. Shadrokh Samavi 47

    Machine RepresentationsMost familiar number systems are:

    1. Non-redundant – every value is uniquely represented by a radix polynomial

    2. Weighted – sequence of weights

    determines the value of the n-tuple formed from the digit set

    3. Positional – wi depends only on position i4. Conventional number systems

    where ß is a constant. These are fixed-radix systems.

    1 2 2 1 0, , , , ,n nw w w w w

    1 2 2 1 0, , , , ,n nx x x x x 1

    0

    n

    i ii

    X x w

    iiw