ee108a lecture 4: numbers and arithmetic -...

21
EE108a 10/3/2007 1 EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 1 10/3/2007 EE108A Lecture 4: Numbers and Arithmetic EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 2 10/3/2007 Announcements Put your Section Leader’s name on your homework Get started on Lab 1. 99% of the work is in the prelab due on Monday. (Hint: starting on the lab before your section gives you a great chance to ask your section leader if you get stuck.)

Upload: nguyenquynh

Post on 30-Aug-2018

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

1

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 110/3/2007

EE108A

Lecture 4: Numbers and Arithmetic

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 210/3/2007

Announcements

• Put your Section Leader’s name on your homework

• Get started on Lab 1. 99% of the work is in the prelab due on Monday.(Hint: starting on the lab before your section gives you a great chance toask your section leader if you get stuck.)

Page 2: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

2

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 310/3/2007

Numbers

• Digital logic works with binary numbers– Each bit has a value based on its position– For binary we use power-of-two values:

• Each bit is worth 2n, where n is the position of the bit• E.g.: (left-to-right): 24=16, 23=8, 22=4, 21=2, 20=1

– Example: What is 101012 (binary) in base 10 (decimal)?

– Bit values (left-to-right): 1*24 + 0*23 + 1*22 + 0*21 + 1*20

= 1+4+16 = 21

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 410/3/2007

From Decimal To Binary

• Example: 1710

• Convert number to binary base:17 2 = 8 remainder 1 8 2 = 4 remainder 0 4 2 = 2 remainder 0 2 2 = 1 remainder 0 1 2 = 0 remainder 1

• 1710 = 100012

• Or, 17=16+1 = 24+20 = 100012

....

...... MSB

LSB

Page 3: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

3

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 510/3/2007

From Decimal To Binary To Hexadecimal

• Example: 196310 = 1024 + 512 + 256 + 128 + 32 + 8 + 2 + 1

• 196310 = 0111 1010 10112

7 A Bhex

Hex notation:0 01 12 23 34 45 56 67 78 89 910 A11 B12 C13 D14 E15 F

Very useful shorthandfor 4-bit values!

Binary addition1-bit 0+0 = 0, 1+0 = 1, 1+1 = 10

1

6 110+3 011

5 101+7 111

0_10_

01001

110_

10019

0

1_

00

11_

100

111_

110012

c[i] b[i] a[i] count c[i+1] s[i]0 0 0 0 0 00 0 1 1 0 10 1 0 1 0 10 1 1 2 1 01 0 0 1 0 11 0 1 2 1 01 1 0 2 1 01 1 1 3 1 1

Adder:

{c[i+1],s[i]} = a[i]+b[i]+c[i]

We’re adding a+b for the ith bit,plus the carry in c, and generating

the sum s, for the ith bit, and thecarry in c, for the next (i+1th) bit.

Page 4: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

4

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 710/3/2007

One bit of an adder

• Counts the number of “1” bits on its input• Outputs the result in binary• For a half-adder, 2 inputs, output can be 0, 1, or 2 ( {c,s}=a+b )• For a full-adder, 3 inputs, output can be 0, 1, 2, or 3 ( {c,s}=a+b+c )

c[i] b[i] a[i] count c[i+1] s[i]0 0 0 0 0 00 0 1 1 0 10 1 0 1 0 10 1 1 2 1 01 0 0 1 0 11 0 1 2 1 01 1 0 2 1 01 1 1 3 1 1

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 810/3/2007

Half Adder

HA

a

b

c

s

b

ac

s

// half addermodule HalfAdder(a,b,c,s); input a,b ; output c,s ; // carry and sum wire s = a ^ b ; wire c = a & b ;endmodule

# 00 00# 01 01# 10 01# 11 10

Page 5: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

5

// full adder - from half addersmodule FullAdder1(a,b,cin,cout,s) ; input a,b,cin ; output cout,s ; // carry and sum wire g,p ; // generate and propagate wire cp ; HalfAdder ha1(a,b,g,p) ; HalfAdder ha2(cin,p,cp,s) ; or o1(cout,g,cp) ;endmodule

# 000 00# 001 01# 010 01# 011 10# 100 01# 101 10# 110 10# 111 11

HA

a

b

g

p

c

s

a

b

HA

c

s

a

bcin

cp

s

HA

c

s

a

b

(1)

(1)

(1) (2)

(1) (2)

(1)

cout(2)HA

a

b

g

p

c

s

a

b

HA

c

s

a

bcin

cp

s(1)

(1)

(1) (2)

(1) (2)

(1)

cout(2)

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 1010/3/2007

Full adder from a truth table

cin

Majority

ab

s

cout

c[i] b[i] a[i] count c[i+1] s[i]0 0 0 0 0 00 0 1 1 0 10 1 0 1 0 10 1 1 2 1 01 0 0 1 0 11 0 1 2 1 01 1 0 2 1 01 1 1 3 1 1

3-input XORTrue if odd # of inputs true

Page 6: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

6

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 1110/3/2007

// full adder - logicalmodule FullAdder2(a,b,cin,cout,s) ; input a,b,cin ; output cout,s ; // carry and sum wire s = a ^ b ^ cin ; wire cout = (a & b)|(a & cin)|(b & cin) ; // majorityendmodule

Full adder from truth table

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 1210/3/2007

CMOS Version of Full Adder

g'a

b

p'

cin

cout

s

Page 7: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

7

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 1310/3/2007

Multi-bit Adder

FA

a[0]

b[0]

a

bcin

cout

ss[0]

cin

FA

a[1]

b[1]

a

bcin

cout

ss[1]

c[1]

FA

a[n-1]

b[n-1]

a

bcin

cout

ss[n-1]

cout

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 1410/3/2007

// multi-bit adder - behavioralmodule Adder1(a,b,cin,cout,s) ; parameter n = 8 ; input [n-1:0] a, b ; input cin ; output [n-1:0] s ; output cout ; wire [n-1:0] s; wire cout ;

assign {cout, s} = a + b + cin ;endmodule

Adder in verilog - behavioral

Page 8: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

8

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 1510/3/2007

Common Verilog Errors

• wire [2:0] sum, a, b;sum = a+b;

• What happens if a=7 (1112) and b=1 (0012)?sum = 0002 WRONG!

• wire[2:0] sum, a, b;{carry, sum} = a+b;

• What happens if a=7 (1112) and b=1 (0012)?carry = 1, sum = 0002

• wire[3:0] sum;wire[2:0] a,b;sum = a+b;

• What happens if a=7 (1112) and b=1 (0012)?sum = 10002

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 1610/3/2007

// multi-bit adder – with vectorsmodule Adder2(a,b,cin,cout,s) ; parameter n = 8 ; input [n-1:0] a, b ; input cin ; output [n-1:0] s ; output cout ;

wire [n-1:0] p = a ^ b ; wire [n-1:0] g = a & b ; wire [n:0] c = {g | (p & c[n-1:0]), cin} ; wire [n-1:0] s = p ^ c[n-1:0] ; wire cout = c[n] ;endmodule

Ripple-carry adder – bit-slice notation

g'a

b

p'

cin

cout

s

g'a

b

p'

cin

cout

s

g'a

b

p'

cin

cout

s

Page 9: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

9

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 1710/3/2007

Negative Integers

• Thus far we have only addressed positive integers. What aboutnegative numbers?

• 3 ways to represent negative integers in binary:– Sign Magnitude– One’s complement– Two’s complement

• Example: Consider +23 and –23– Sign Magnitude 0 10111 1 10111– One’s complement 0 10111 1 01000– Two’s complement 0 10111 1 01001

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 1810/3/2007

2’s complement makes subtraction easy

Represent negative number, –x as 2n – x

All arithmetic is done modulo 2n so no adjustments are necessary

x + (– y) = x + (2n – y) (mod 2n)

consider 4-bit numbers

4 – 3 = 4 + (16 – 3) (mod 16) = 4 + (15 – 3 + 1)

= 0100 + (1111 – 0011) + 0001= 0100 + 1100 + 0001= 0001

Why do we all use 2’s complement?

Page 10: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

10

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 1910/3/2007

2’s Complement

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 2010/3/2007

i

n

i

inn

bbv 22

2

0

11 !

"

="

" +"=

Page 11: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

11

Adder

a

n

n

b

n

sub

a

b

cin

cout

sout

n

ovf

Adder

a[n-2:0]

n-1b[n-2:0]

n-1

sub

a

bcin

cout

sout[n-2:0]

Adder

a

bcin

cout

s

n-1n-1

out[n-1]a[n-1]

b[n-1]

Subtraction Circuit:

Adder with input inverted + 1 (ci=1)

4 – 3 = 4 + (16 – 3) (mod 16) = 4 + (15 – 3 + 1)

= 0100 + (1111 – 0011) + 0001= 0100 + 1100 + 0001= 0001

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 2210/3/2007

// add a+b or subtract a-b, check for overflowmodule AddSub(a,b,sub,s,ovf) ; parameter n = 8 ; input [n-1:0] a, b ; input sub ; // subtract if sub=1, otherwise add output [n-1:0] s ; output ovf ; // 1 if overflow wire c1, c2 ; // carry out of last two bits wire ovf = c1 ^ c2 ; // overflow if signs don't match

// add non sign bits Adder1 #(n-1) ai(a[n-2:0],b[n-2:0]^{n-1{sub}},sub,c1,s[n-2:0]) ; // add sign bits Adder1 #(1) as(a[n-1],b[n-1]^sub,c1,c2,s[n-1]) ;endmodule

Page 12: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

12

# 57 + 28 = 7f ovf = 0# 57 - 28 = 2f ovf = 0# 58 + 28 = 80 ovf = 1# e9 - 17 = d2 ovf = 0# e9 + 97 = 80 ovf = 0# e9 - 6a = 7f ovf = 1

// test scriptmodule test2 ;

reg [7:0] in0, in1 ; wire [7:0] out ; reg sub ; wire ovf ;

AddSub #(8) a(in0,in1,sub,out,ovf) ;

initial begin in0 = 8'd87 ; in1 = 8'd40 ; sub = 0 ; #100 $display("%03h + %03h = %03h ovf = %b", in0, in1, out, ovf) ; in0 = 8'd87 ; in1 = 8'd40 ; sub = 1 ; #100 $display("%03h - %03h = %03h ovf = %b", in0, in1, out, ovf) ; in0 = 8'd88 ; in1 = 8'd40 ; sub = 0 ; #100 $display("%03h + %03h = %03h ovf = %b", in0, in1, out, ovf) ; in0 = 8'he9 ; in1 = 8'h17 ; sub = 1 ; /* -23 - 23 */ #100 $display("%03h - %03h = %03h ovf = %b", in0, in1, out, ovf) ; in0 = 8'he9 ; in1 = 8'h97 ; sub = 0 ; /* -23 - 105 = -128 no ovf */ #100 $display("%03h + %03h = %03h ovf = %b", in0, in1, out, ovf) ; in0 = 8'he9 ; in1 = 8'd106 ; sub = 1 ; /* overflow */ #100 $display("%03h - %03h = %03h ovf = %b", in0, in1, out, ovf) ; endendmodule

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 2410/3/2007

Compare two numbers with subtraction

diff = a – b

if diff is negative (sign bit = 1), a<b

If diff is zero, a=bExample, compare a = 0101 and b = 0110

diff = a – b = 1111 => a<b

Compare a=0111 and b = 0111, diff = 0 => a=b

Compare

a

b

lt

eqn

n

Page 13: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

13

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 2510/3/2007

Multiplication

Shifting left multiplies by 2

e.g., 5 = 101, 10 = 1010, 20 = 10100

To multiply by 3, compute 3x = x + 2x

Example, 7 x 5

1 1 1 x 1 0 1

1 1 1 0 0 0 1 1 1

1 0 0 0 1 1

Multiplication

a3

a2

a1

b0

b1

b2

b3

a0

First generate partial products: pij = ai ∧ bj has weight i+j

Page 14: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

14

a3 a2 a1

b0

b1

b2

b3

a0

FA 0

p2

p1

p0

FA

FA

FA

FA

FA

p3

FA

FA

p4

FAFA

FA

p5

p6

FA

0

0

0

p7

Then sum partial products to get sum

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 2810/3/2007

// 4-bit multipliermodule mul4(a,b,p) ; input [3:0] a,b ; output [7:0] p ;

// form partial products wire [3:0] pp0 = a & {4{b[0]}} ; // x1 wire [3:0] pp1 = a & {4{b[1]}} ; // x2 wire [3:0] pp2 = a & {4{b[2]}} ; // x4 wire [3:0] pp3 = a & {4{b[3]}} ; // x8

// sum up partial products wire cout1, cout2, cout3 ; wire [3:0] s1, s2, s3 ; Adder1 #(4) a1(pp1, {0,pp0[3:1]}, 0, cout1, s1) ; Adder1 #(4) a2(pp2, {cout1,s1[3:1]}, 0, cout2, s2) ; Adder1 #(4) a3(pp3, {cout2,s2[3:1]}, 0, cout3, s3) ;

// collect the result wire [7:0] p = {cout3, s3, s2[0], s1[0], pp0[0]} ;endmodule

Page 15: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

15

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 2910/3/2007

// test scriptmodule test3 ;

reg [3:0] in0, in1 ; wire [7:0] out ;

Mul4 mul(in0,in1,out) ;

initial begin in0 = 0 ; repeat (16) begin in1 = 0 ; repeat (in0+1) begin #100 $display("%03d * %03d = %03d",in0,in1,out) ; in1 = in1 + 1 ; end in0 = in0 + 1 ; end endendmodule

# 0 * 0 = 0# 1 * 0 = 0# 1 * 1 = 1# 2 * 0 = 0# 2 * 1 = 2# 2 * 2 = 4# 3 * 0 = 0# 3 * 1 = 3# 3 * 2 = 6# 3 * 3 = 9# 4 * 0 = 0# 4 * 1 = 4# 4 * 2 = 8# 4 * 3 = 12# 4 * 4 = 16# 5 * 0 = 0# 5 * 1 = 5# 5 * 2 = 10# 5 * 3 = 15# 5 * 4 = 20# 5 * 5 = 25# 6 * 0 = 0# 6 * 1 = 6# 6 * 2 = 12# 6 * 3 = 18# 6 * 4 = 24# 6 * 5 = 30# 6 * 6 = 36# 7 * 0 = 0# 7 * 1 = 7# 7 * 2 = 14# 7 * 3 = 21# 7 * 4 = 28# 7 * 5 = 35# 7 * 6 = 42# 7 * 7 = 49# 8 * 0 = 0# 8 * 1 = 8# 8 * 2 = 16# 8 * 3 = 24# 8 * 4 = 32# 8 * 5 = 40# 8 * 6 = 48# 8 * 7 = 56# 8 * 8 = 64

# 9 * 0 = 0# 9 * 1 = 9# 9 * 2 = 18# 9 * 3 = 27# 9 * 4 = 36# 9 * 5 = 45# 9 * 6 = 54# 9 * 7 = 63# 9 * 8 = 72# 9 * 9 = 81# 10 * 0 = 0# 10 * 1 = 10# 10 * 2 = 20# 10 * 3 = 30# 10 * 4 = 40# 10 * 5 = 50# 10 * 6 = 60# 10 * 7 = 70# 10 * 8 = 80# 10 * 9 = 90# 10 * 10 = 100# 11 * 0 = 0# 11 * 1 = 11# 11 * 2 = 22# 11 * 3 = 33# 11 * 4 = 44# 11 * 5 = 55# 11 * 6 = 66# 11 * 7 = 77# 11 * 8 = 88# 11 * 9 = 99# 11 * 10 = 110# 11 * 11 = 121# 12 * 0 = 0# 12 * 1 = 12# 12 * 2 = 24# 12 * 3 = 36# 12 * 4 = 48# 12 * 5 = 60# 12 * 6 = 72# 12 * 7 = 84# 12 * 8 = 96# 12 * 9 = 108# 12 * 10 = 120# 12 * 11 = 132# 12 * 12 = 144

# 13 * 0 = 0# 13 * 1 = 13# 13 * 2 = 26# 13 * 3 = 39# 13 * 4 = 52# 13 * 5 = 65# 13 * 6 = 78# 13 * 7 = 91# 13 * 8 = 104# 13 * 9 = 117# 13 * 10 = 130# 13 * 11 = 143# 13 * 12 = 156# 13 * 13 = 169# 14 * 0 = 0# 14 * 1 = 14# 14 * 2 = 28# 14 * 3 = 42# 14 * 4 = 56# 14 * 5 = 70# 14 * 6 = 84# 14 * 7 = 98# 14 * 8 = 112# 14 * 9 = 126# 14 * 10 = 140# 14 * 11 = 154# 14 * 12 = 168# 14 * 13 = 182# 14 * 14 = 196# 15 * 0 = 0# 15 * 1 = 15# 15 * 2 = 30# 15 * 3 = 45# 15 * 4 = 60# 15 * 5 = 75# 15 * 6 = 90# 15 * 7 = 105# 15 * 8 = 120# 15 * 9 = 135# 15 * 10 = 150# 15 * 11 = 165# 15 * 12 = 180# 15 * 13 = 195# 15 * 14 = 210# 15 * 15 = 225

Page 16: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

16

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 3110/3/2007

Accuracy

Resolution

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 3210/3/2007

Page 17: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

17

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 3310/3/2007

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 3410/3/2007

Value and Representation functions

• R(x) is binary number representing real number x• V(b) is value of binary number b

• Absolute error– ea= |V(R(x)) - x|– e.g., absolute error =

| actual value(binary representation(number x)) - number x |

• Relative error– er = |(V(R(x)) - x)/x|

Page 18: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

18

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 3510/3/2007

Common Representations

• Integer– Standard base 2 (unsigned/2’s complement)– Unsigned: abcd = a*23 + b*22 + c*21 + d*20

– 2’s Complement: abcd = -a*23 + b*22 + c*21 + d*20

• Fixed point– Standard base 2 (unsigned/2’s complement) with binary point– Unsigned: ab.cd = a*21 + b*20 + c*2-1 + d*2-2

= a*2 + b*1 + c*1/2 + d*1/4– 2’s Complement: ab.cd = -a*21 + b*20 + c*2-1 + d*2-2

= -a*2 + b*1 + c*1/2 + d*1/4• Floating point

– Sign plus Mantissa*2Exponent

– Exponent in binary tells you how much to shift the Mantissa to the leftor right of the binary point. Then you interpret the Mantissa with thenew binary point.

– You need to know where the implied binary point is in the Mantissa!

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 3610/3/2007

Example

• Suppose you need to represent temperatures from 0 to 100C to 0.1C• What representation would you use?

– What is the resolution of this representation?– What is its maximum absolute error?

Page 19: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

19

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 3710/3/2007

Example

• Suppose you need to represent distances from 1mm to 1m with anaccuracy of 0.1%

• How many bits are required to do this with a binary fixed-point number?• How many bits are needed with binary floating point?• What floating-point representation is needed?

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 3810/3/2007

Binary Floating Point

Floating-Point Representation

S Exponent Mantissa

Floating-Point Format: (-1)S x M x 2E

022233031

8 bits 23 bits

IEEE 754 Floating-Point Representation

Floating-Point Format: (-1)S x (1 + M) x 2(E-Bias)

S Exponent Mantissa

022233031

8 bits 23 bits

Page 20: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

20

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 3910/3/2007

6.4 FP

Value 10-bit integerError % Exp Mantissa Value Error %

477.49 0111011101 0.49 0.10% 1110 1110111 476 1.49 0.31%

47.749 0000110000 0.251 0.53% 1011 1011111 47.5 0.249 0.52%

4.7749 0000000101 0.2251 4.71% 1000 1001100 4.75 0.0249 0.52%

0.4775 0000000000 0.47749 100.00% 0100 1111010 0.476563 0.0009275 0.19%

0.0477 0000000000 0.047749 100.00% 0001 1100010 0.047852 0.000102562 0.21%

Shift mantissa by exp-12

Binary point is shifted by exp-5 from left of implied one

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 4010/3/2007

0

8

16

24

32

40

48

56

64

0 8 16 24 32 40 48 56 64

6-bit integer

3.3 floating point

x=y

Page 21: EE108A Lecture 4: Numbers and Arithmetic - …cva.stanford.edu/people/davidbbs/classes/ee108a/fall0708 lectures... · EE108A Lecture 4: Numbers and Arithmetic ... From Decimal To

EE108a 10/3/2007

21

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 4110/3/2007

0

1

2

3

4

5

6

7

8

0 1 2 3 4 5 6 7 8

6-bit integer

3.3 floating point

x=y

EE 108A Lecture 4 (c) 2007 W. J. Dally and D. Black-Schaffer 4210/3/2007

Summary

• Binary number representation• Add numbers a bit at a time• 2’s complement –x = (2n – x) = (2n – 1) – x + 1 = neg(x) + 1• Subtract by 2’s complement and add• Multiply – form partial products pij and sum (a lot of adders!)

• Number representation – accuracy and resolution.• Fixed and floating point (you’ll see more in labs 4 and 2, respectively)

• Hint: Verilog doesn’t know what number format you’re using. It’s upto you to interpret the bits the correct way.