ADSD Fall2011 09 Fixed Point Representation

download ADSD Fall2011 09 Fixed Point Representation

of 41

Transcript of ADSD Fall2011 09 Fixed Point Representation

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    1/41

    Dr. Rehan Hafiz Lecture # 08

    ADSD Fall 2011

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    2/41

    Course Website for ADSD Fall 2011

    http://lms.nust.edu.pk/

    2

    Lectures: Tuesday @ 5:30-6:20 pm, Friday @ 6:30-7:20 pm

    Contact: By appointment/EmailOffice: VISpro Lab above SEECS Library

    Acknowledgement: Material from the following sources has been consulted/used in theseslides:1. [CIL] Advanced Digital Design with the Verilog HDL, M D. Ciletti2. [SHO] Digital Design of Signal Processing System by Dr Shoab A Khan3. [STV] Advanced FPGA Design, Steve Kilts4. Ercegovacs Book: Digital Arithmetic 20045. Dr. Shoab A Khans CASE Lectures on Advanced Digital System Design

    Material/Slides from these slides CAN be used with following citing reference:

    Dr. Rehan Hafiz: Advanced Digital System Design 2010

    Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

    http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/http://creativecommons.org/licenses/by-nc-sa/3.0/
  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    3/41

    3

    1 Introduction Outline & Introduction, Initial Assessment of students, Digital design

    methodology & design flow2 Verilog+

    Combinational Logic

    Combinational Logic Review + Verilog Introduction, Combinational Building

    Blocks in Verilog

    3 Verilog + Sequential Logic Sequential Common Structure in Verilog (LFSR /CRC+ Counters + RAMS),

    Sequential Logic in Verilog4 Synthesis in Verilog Synthesis of Blocking/Non-Blocking Statements5 Micro-Architecture

    Design Partitioning + RISC Microprocessor + Micro architecture Document

    6 Optimizing Speed Architecting Speed in Digital System Design: [Throughput, Latency, Timing]7 Optimizing Area Architecting Area in Digital System Design: [Area Optimization]8 FIR Implementation FIR Implementations + Pipelining & Parallelism in Non Recursive DFGs10 CDC Issues Cross-Clock Domain Issues & RESET circuits11 Fixed-Point Arithmetic Arithmetic Operations: Review Fixed Point Representation12 Adders Adders & Fast Adders Multi-Operand Addition13 Multipliers Multiplication , Multiplication by Constants + BOOTH Multipliers13 CORDIC CORDIC (sine, cosine, magnitude, division, etc), CORDIC in HW14 Algorithmic

    Transformations for

    System DesignDFG representation of DSP Algorithms, Iteration Bound

    & Retiming

    15 Algorithmic

    TransformationsUnfolding

    Look ahead transformations16 Project Course Review & Project Presentations

    17 Project Project Presentations

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    4/41

    Lecture Overview

    4

    Algorithms, Arithmetic & Numbers

    2s Complement Representation

    Generating Overflow Flag

    Fixed point / Floating point

    Qn.m format

    Truncating/ Rounding

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    5/41

    Algorithms in Hardware

    5

    Primitives

    Addition/subtraction

    Multi operand addition

    Logical and arithmetic shift

    Multiplication by constant

    Multiplication

    Division Algorithms

    Sequenced Composition of Primitives

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    6/41

    Numbers in hardware

    6

    Number

    SignedNumber

    Positive

    Number

    NegativeNumber

    UnsignedNumbers

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    7/41

    Representing Numbers

    7

    n-bit unsigned Binary Numbers

    B = bn-1, bn-2,.., b1, b0 with bi from {0,1}

    Negative Number Representation

    Signed Magnitude

    Ones Complement

    Twos Complement

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    8/41

    Signed Magnitude

    8

    Designate left-most bit as a signbit with no arithmetic weight

    1-> negative, 0-> positive.

    Positive and negative zero (0000vs. 1000)

    Difficult to add numbers ofdifferent sign or subtractnumbers of same sign(comparison)

    Same sign bits Add and usesame sign

    Different signsLogic to decideadd or sub & final sign

    N-1 bits are available torepresent magnitude

    Range of N-bit signed-magnitudenumber is:

    -(2N-1 1) to (2N-1 1)

    [CIL]

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    9/41

    Ones Complement

    9

    Flipped bits as negative

    number

    Positive and negative zero

    (0000 vs. 1000)

    Subtraction is more

    complicated than in 2s-

    comp

    Range of N-bit signed-

    magnitude number is:

    -(2N-1 1) to (2N-1 1)

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    10/41

    Twos Complement

    10

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    11/41

    Unified Representation for both positive

    & negative numbers in 2s Complement11

    Equivalent unsigned representation

    Example

    For N=8, -10 b11110110

    For N=5, -10 b10110

    Paper & Pencil

    conversion by

    scanning from right

    to left, leaving the

    first one and flipping

    all the remaining bits

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    12/41

    Twos Complement

    12

    Arithmetic

    +, -, *, / performed in hardware with a unit that cancarry out binary operations of addition and bitwisecomplement.

    Addition: Same hardware as binary numbers butreduced dynamic range

    Subtraction: Bitwise complements and addition of oneand a number !

    Multiplication: Repeated additions Division involves repeated subtraction

    Negative of a number : Complement & add 1

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    13/41

    Addition in 2s Complement13

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    14/41

    Addition/Subtraction in 2s

    Complement14

    Addition in 2s Complement (a+b)

    Normal Binary Addition (a+b)

    Subtraction in 2s Complement (a-b)

    Equivalent to Normal Binary Addition with (a+(-b))

    Rather than subtracting add the negative of the

    number

    Example: (-10)-(10) Drop the Carry outs

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    15/41

    Negating a number in HW

    15

    Addition of number similar to unsigned binary addition

    Flip all the bits & Add ONE

    Problem Need a Carry Propagate Adder

    Solution: Full Adders

    Overflow issue due to asymmetric range

    Example: Negative of 1000

    dd / b f

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    16/41

    Adder/Subtractor for 2s

    Complement16

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    17/41

    Knowing Overflow17

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    18/41

    18

    Signed and Unsigned Integers

    A Hardware Viewpoint

    Hardware cannot distinguish between signed and

    unsigned integers

    YOU are solely responsible for using the correct

    data type with each instruction

    Verilog does not provide synthesizable signed

    representation

    Consider an adder adding two 2s complementnumbers

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    19/41

    Overflow Examples

    19

    +3 0011 +5 0101 +5 0101

    +4 0100 +6 0110 -6 1010

    -------- -------- --------

    +7 0111 +11 1011 -1 1111(signed overflow but not an unsigned overflow)

    -5 1011 -3 1101 -5 1011

    +6 0110 -4 1100 -6 1010

    -------- -------- --------+1 0001 -7 1001 -11 0101

    (unsigned & signed overflow)

    overflow occurs when:

    POS+POS=NEG or

    NEG+NEG=POS

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    20/41

    20

    Overflow and Carry Flags

    A Hardware Viewpoint

    Overflow Unsigned

    Carry Out of MSB

    2s Complement

    Carryout does not indicate overflow

    ADDITION

    OF = (carry out of the MSB) XOR (carry into the MSB)

    SUBTRACTON NEG the source and ADD it to the destination

    OF = (carry out of the MSB) XOR (carry into the MSB)

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    21/41

    Knowing your 2s Complement Number for Corner Cases

    (Because of the slightly asymmetric range, negation may lead to

    overflow! )

    21

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    22/41

    Corner Cases

    22

    There is no equal opposite of -2N-1 in Nbits

    2s complement representation

    For example, while multiplying two ve

    numbers like -4 x -4 , we can get16 and wecant represent this as a 5-bit 2s complement

    No.

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    23/41

    Knowing if a number is Negative

    23

    S(N-1) is the MSB of the Sum

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    24/41

    Generating the Flags

    24

    Needs Correction

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    25/41

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    26/41

    Floating Point Numbers

    26

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    27/41

    Representing Fixed Point Numbers

    27

    Insert implicit binary point between two bits

    bits to left of point have value 1

    bits to right of point have value < 1

    29 28 27 26 25 24 23 22 21 20(512) (256) (128) (64) (32) (16) (8) (4) (2) (1)

    21 2223242526

    (0.5)(0.25)

    (0.125)(0.0625)

    (0.03125)(0.015625)

    0 1 0 0 0 0 1 1 0 1 0 1 1 1 1 0

    269.46875

    binary point

    .

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    28/41

    Qn.m Format

    28

    Qn.m format is a fixed positional number system

    for representing floating-point numbers

    Qn.msimply means thatN-bit binary number has

    n bits to the left and m bits to the right of thebinary point

    In case of signed-numbers the MSB is used for

    sign

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    29/41

    There is NO Decimal in HW

    29

    1. Define the total No. of bits to represent a No.

    (Lets say 10 bits)

    2. Fix a Decimal some where in the No.

    (Lets say after 2 locations)

    Note that there is no decimal in H/W.

    FractionalBits

    Zeroth Bit

    SignBit

    -21 20 2-1 2-2 2-3 2-4 2-5 2-6 2-7 2-8

    Floating Point to Fixed Point

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    30/41

    Floating Point to Fixed Point

    Conversion

    30

    In Qn.m format,

    ndepends upon the range of our required integer

    m depends upon how much precision we want in the

    fractional part of our computation.

    Example

    Q3.13

    1 bit for thesign and 2bits arereserved for

    the integerpart

    13 bits arereserved forthe fractionalpart

    Floating Point to Fixed Point

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    31/41

    Floating Point to Fixed Point

    Conversion31

    The algorithm is analyzed for all set of inputs

    Range of each variable is observed

    Depending upon the precision required, n and m

    are specified.

    For implementation on programmable

    processors, N=16, or 32

    For Application Specific Hardware, an optimal

    value of N is specified.

    xamp e

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    32/41

    xamp eOptimization: Different blocks can have

    different range/precision requirement32

    AGC Filter

    Compress

    SpeechSamples

    At every point,we check theranges

    Q9.12 Format

    Q1.17 Format

    Q3.18 Format

    O/p

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    33/41

    Arithmetic in Q Format33

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    34/41

    Addition in Q Format

    34

    Adding 2 different Qn.m Format numbers we get

    Q=QMax(n1,n2) + Max(m1.m2)

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    35/41

    Addition in Q Format

    35

    If two numbers a and b of Qn1.m1 and Qn2.m2 formatsrespectively are added, the result is in Qn.m format, where n islarger of n1 and n2 and m is larger of m1 and m2.

    Example

    0111111001101110

    0100Qn1.m1 =

    Qn2.m2 =

    Qn.m =

    Q2.2

    Q4.4

    Q4.4

    ImpliedDecimal

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    36/41

    Sign Extension

    36

    We need to extend the sign bit to its left.

    Copying the sign bit to its left doesnt change

    the original value of the number

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    37/41

    Addition in Q Format

    37

    If two numbers a and b of Qn1.m1 and Qn2.m2 formatsrespectively are added, the result is in Qn.m format, where n islarger of n1 and n2 and m is larger of m1 and m2.

    Example

    0111011001101110

    011111Qn1.m1 =

    Qn2.m2 =

    Qn.m =

    Q2.2 = - 2 + 1 + 0.5 = -0.5

    Q4.4 = 1 + 2+ 4 + 0.25 + 0.125 = 7.375

    Q4.4 = 2 + 4+ 0.5 + 0.25 + 0.125 = 6.875

    ImpliedDecimal

    May Require Extension of Fixed Point Numbers

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    38/41

    Bit Growth

    38

    Due the demand for increased number ofbits effective Width of the words willincrease

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    39/41

    Multiplication in Q-Format

    39

    c = a x b

    Q n1.m1

    Q n2.m2

    ( a )

    ( b )

    Q (n1+n2) . (m1+m2) ( c )

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    40/41

    Rounding & Truncating

    40

    Truncation

    Simple Truncate

    An operation similar to floor

    Rounding

    Or ADD a copy of bit after the truncation point to itself

    Rounding Vs. Truncation

    Rounding basically weighted truncation.

    Algorithmically it is better to round and then truncate inH/W.

    Resource-wise Rounding adds overhead

  • 8/3/2019 ADSD Fall2011 09 Fixed Point Representation

    41/41

    Thanks

    41