03 Numeric Basics
03 Numeric Basics
An Embedded Systems
Approach Using Verilog
Chapter 3
Numeric Basics
Verilog
Numeric Basics
unsigned integers
signed integers
fixed-point real numbers
floating-point real numbers
complex numbers
Digital Design Chapter 3 Numeric Basi
Verilog
Unsigned Integers
Verilog
Binary Representation
Decimal: base 10
Binary: base 2
x xn 1 2 n 1 xn 2 2 n 2 x0 20
Digital Design Chapter 3 Numeric Basi
Verilog
Binary Representation
To represent x: 0 x N 1, need
log2N bits
Computers use
0: 000000; 2n 1: 111111
Verilog
Unsigned Integers in
Verilog
Verilog
Verilog
Extending Unsigned
Numbers
x0
x1
y0
y1
xn 1
yn 1
yn
ym 2
y
wire [3:0] x;
wire [7:0] y;
assign y = {4'b0000, x};
Verilog
Truncating Unsigned
Numbers
x0
x1
yn 1
yn
xn 1
assign x = y[3:0];
ym 2
ym 1
Verilog
Unsigned Addition
0 0 1 1 1 1 0 0 0 0
carry
bits
1 1 0 0 1
1 0 1 0 1 1 1 1 0 0
0 0 1 1 0 1 0 0 1 0
0 1 0 0 1
1 1 1 0 1
1 1 1 0 0 0 1 1 1 0
1 0 0 1 1 0
overflo
w
10
Verilog
Addition Circuits
Half adder
s0 x0 y0
xi
yi
ci
si
ci+1
si xi yi ci
ci 1 xi yi xi yi ci
c1 x0 y0
Full adder
11
Verilog
Ripple-Carry Adder
sn
overflo
w
xi
full
adder
sn1
cn1
ci+1
yi
full
adder
x1
ci
c2
si
y1
full
adder
s1
x0
c1
y0
full
adder
c0
s0
Worst-case delay
from x0, y0 to sn
carry must ripple through intervening
stages, affecting sum bits
12
Verilog
Improving Adder
Performance
Carry kill:
ki xi yi
Carry propagate: pi xi yi
Carry generate:
g i xi yi
Adder equations
si pi ci
xi
yi
ci
si
ci+1
ci 1 g i pi ci
13
Verilog
Fast-Carry-Chain Adder
yi
xi
pi
ci+1
Xilinx
FPGAs
include
this
structure
gi
ci
si
yi
pi
ki
ci+1
ci
si
14
Verilog
Carry Lookahead
ci 1 g i pi ci
c1 g 0 p0 c0
c2 g1 p1 g 0 p0 c0 g1 p1 g 0 p1 p0 c0
c3 g 2 p2 g1 p2 p1 g 0 p2 p1 p0 c0
c4 g 3 p3 g 2 p3 p2 g1
p3 p2 p1 g 0 p3 p2 p1 p0 c0
Digital Design Chapter 3 Numeric Basi
15
Verilog
Carry-Lookahead Adder
y3
g3
p3
x2
y2
g2
p2
x1
y1
g1
p1
x0
y0
g0
c4
c0
p3
s3
p0
c3
p2
s2
c2
p1
s1
c1
p0
s0
16
Verilog
17
Verilog
Adders in Verilog
wire [7:0] a, b, s;
...
assign s = a + b;
wire [8:0] tmp_result;
wire
c;
...
assign tmp_result = {1'b0, a} + {1'b0, b};
assign c
= tmp_result[8];
assign s
= tmp_result[7:0];
assign {c, s} = {1'b0, a} + {1'b0, b};
assign {c, s} = a + b;
18
Verilog
Unsigned Subtraction
As in decimal
b
x:
y:
1 0 1 0 0 1 1 0
0 1 0 0 1 0 1 0
d:
0 1 0 1 1 1 0 0
0 1 0 1 1 0 0 0
borro
w bits
19
Verilog
Subtraction Circuits
For least-significant
bits
d 0 x0 y0
b1 x0 y0
For remaining
bits
d x y b
i
bi 1 xi yi xi yi bi
xi
yi
bi
si
bi+1
20
Verilog
Adder/Subtracter Circuits
Addition
Subtraction
si xi yi ci
d i xi yi bi
ci 1 xi yi xi yi ci
bi 1 xi yi xi yi bi
21
Verilog
Adder/Subtracter Circuits
xn1
x1 x0
yn1
y1
y0
xn1 x1 x0
yn1
y1
cn
y0
c0
sn1
s1
s0
s1/d1
s0/d0
sn1/dn1
depends on constraints
Digital Design Chapter 3 Numeric Basi
22
Verilog
Subtraction in Verilog
module adder_subtracter ( output [11:0]
output
input [11:0]
input
assign {ovf_unf, s} = !mode ? (x + y)
endmodule
s,
ovf_unf,
x, y,
mode );
: (x - y);
23
Verilog
ci 1 xi ci
cn1
half
adder
sn1
xi
ci+1
ci
half
adder
si
x1
c2
c1
x0
half
adder
half
adder
s1
s0
24
Verilog
Increment/Decrement in
Verilog
assign s = x + 1;
// increment x
assign s = x - 1;
// decrement x
Automatically resized
25
Verilog
Equality Comparison
x0
y0
x1
y1
xn1
yn1
In Verilog, x == y
gives a bit result
eq
assign eq = x == y;
26
Verilog
Inequality Comparison
xn1
yn1
xn2
yn2
gt
xn1 = yn1
xn20 > yn20
xn2 = yn2
x1
y1
x1 > y1
x0
y0
x0 > y0
x1 = y1
27
Verilog
Comparison Example in
Verilog
28
Verilog
Scaling by Power of 2
x xn 1 2 n 1 xn 2 2 n 2 x0 20
2 k x xn 1 2 k n 1 xn 2 2 k n 2 x0 2 k 0 2 k 1 (0)2 0
29
Verilog
Scaling by Power of 2
x xn 1 2 n 1 xn 2 2 n 2 x0 2 0
x / 2 k xn 1 2 n 1 k xn 2 2 n 2 k xk 20 xk 1 2 1 x0 2 k
30
Verilog
Scaling in Verilog
s = 000100112 =
1910
s = 000100112 =
1910
assign y = s << 2;
assign y = s >> 2;
y = 010011002 =
7610
y = 0001002 = 410
31
Verilog
Unsigned Multiplication
xy x yn 1 2 n 1 yn 2 2 n 2 y0 20
yn 1 x 2 n 1 yn 2 x 2 n 2 y0 x 20
if yi = 0, then yi x 2i = 0
32
Verilog
xn1 xn2
Unsigned
Multiplication
cn
y0
s0
x1
x0
yn1 yn2
y1
y1
y0
c0
adder
sn1 s2 s1
xn1
y1
xn2 x1 x0
xn2
y0
c0
xn1 xn2
xn1
yn1 yn2
adder
sn1 s2 s1
xn1 xn2
s0
x1
x0
y2
x1 x0
cn
yn1 yn2
y1
sn1 s2 s1
y0
c0
adder
s0
xn1 xn2
cn
sn1
p2n2
x1 x0
s2
s1
pn+1
pn
x1
x0
yn1
p2n1
x1 x0
cn
x0
xn2
xn1
x1
yn1 yn2
y1
y0
c0
pn1
p2
p1
p0
33
Verilog
Product Size
(2 n 1)(2 n 1) 2 2 n 2 n 2 n 1 2 2 n 2 n 1 1
requires n + m bits
// implicit resizing
34
Verilog
Other Unsigned
Operations
Division, remainder
35
Verilog
Gray Codes
Segme
nt
Code
Segme
nt
Code
0000
1100
0001
1101
0011
10
1111
0010
11
1110
0110
12
1010
0111
13
1011
0101
14
1001
0100
15
1000
See book for n-bit Gray code
36
Verilog
Signed Integers
37
Verilog
2s-Complement
Representation
x xn 1 2
x0 2
10000 = 2n1
Most-positive number
xn 2 2
n2
Most-negative number
n 1
01111 = +2n1 1
xn1 = 1 negative,
xn1 = 0 non-negative
Since 2 n 2 2 0 2 n 1 1
Digital Design Chapter 3 Numeric Basi
38
Verilog
2s-Complement Examples
00110101
10110101
00000000
11111111
10000000
01111111
=
=
=
=
0
1
128
+127
39
Verilog
wire
[11:0] s1;
wire signed [11:0] s2;
...
assign s2 = $signed(s1);
// s1 is known to be
// less than 2**11
...
assign s1= $unsigned(s2); // s2 is known to be nonnegative
40
Verilog
41
Verilog
42
Verilog
43
Verilog
sign extension
x0
x1
y0
y1
xn 1
yn 1
yn
ym 2
y
44
Verilog
Signed Negation
Note thatxi 1 xi
x 1 (1 xn 1 )2 n 1 (1 xn 2 )2 n 2 (1 x0 )2 0 1
2 n 1 xn 1 2 n 1 2 n 2 xn 2 2 n 2 20 x0 2 0 1
( xn 1 2 n 1 xn 2 2 n 2 x0 2 0 )
2 n 1 (2 n 2 20 ) 1
x 2 n 1 2 n 1 x
E.g., 43 is 00101011
so 43 is 11010100 + 1 = 11010101
Digital Design Chapter 3 Numeric Basi
45
Verilog
Signed Negation
E.g., assign y = x;
Digital Design Chapter 3 Numeric Basi
46
Verilog
Signed Addition
x xn 1 2 n 1 xn 20
x y ( xn 1 yn 1 )2
y yn 1 2 n 1 yn 20
n 1
xn 20 yn 20
yields cn1
47
Verilog
1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0
72:
49:
0 1 0 0 1 0 0 0
0 0 1 1 0 0 0 1
63:
32:
1 1 0 0 0 0 0 1
1 1 1 0 0 0 0 0
42:
8:
1 1 0 1 0 1 1 0
0 0 0 0 1 0 0 0
121:
0 1 1 1 1 0 0 1
95:
1 0 1 0 0 0 0 1
34:
1 1 0 1 1 1 1 0
no overflow
no overflow
0 1 0 0 1 0 0 0
72:
105:
0 1 0 0 1 0 0 0
0 1 1 0 1 0 0 1
1 0 1 1 0 0 0 1
positive overflow
no overflow
1 0 0 0 0 0 0 0
63:
96:
1 1 1 1 1 0 0 0
1 1 0 0 0 0 0 1
1 0 1 0 0 0 0 0
42:
8:
0 0 1 0 1 0 1 0
1 1 1 1 1 0 0 0
0 1 1 0 0 0 0 1
34:
0 0 1 0 0 0 1 0
negative overflow
no overflow
48
Verilog
49
Verilog
Signed Subtraction
x y x ( y ) x y 1
x1 x0
yn1
y1
y0
xn1 x1 x0
yn1
y1
cn
cn1
y0
c0
sn1
s1
s0
s1/d1
s0/d0
sn1/dn1
50
Verilog
Increment, decrement
Comparison
same as unsigned
=, same as unsigned
xn 1 yn 1
>, compare sign bits using
Multiplication
51
Verilog
Multiplying by 2k
Dividing by 2k
52
Verilog
Fixed-Point Numbers
Fixed-point numbers
53
Verilog
Positional Notation
In decimal
10.2410 1101 0 100 2 10 1 4 10 2
In binary
101.012 1 2 2 0 21 1 20 0 2 1 1 2 2 5.2510
54
Verilog
Unsigned Fixed-Point
x xm 1 2 m 1 x0 2 0 x1 2 1 x f 2 f
Range: 0 to 2m 2f
Precision: 2f
m may be 0, giving fractions only
e.g., m= 2: 0.0001001101
Digital Design Chapter 3 Numeric Basi
55
Verilog
Signed Fixed-Point
x xm 1 2 m 1 x0 2 0 x1 2 1 x f 2 f
56
Verilog
In DSP
57
Verilog
Fixed-Point in Verilog
58
Verilog
Fixed-Point Operations
e.g., addition:
x y (x 2 y 2 ) / 2
f
a7
a6
a5
a4
10-bit
adder
x0
a3
x7
x8
x9
b4
y0
b3
b4
b5
y7
y8
y9
s0
c4
c3
c4
c5
59
Verilog
Floating-Point Numbers
radix
exponent
60
Verilog
s exponen
t
m bits
mantissa
x M 2 (1 s ) 1.mantissa 2
E
exponent 2 e1 1
61
Verilog
Floating-Point Range
Largest value
Range:
2 e1 2
x 2
2 e1
62
Verilog
Floating-Point Precision
m bits of precision
63
Verilog
Example Formats
e = 8, m = 23
range 1.2 1038 to 1.7 1038
precision 7 decimal digits
Application-specific, 22 bits
e = 5, m = 16
range 6.1 105 to 6.6 104
precision 5 decimal digits
Digital Design Chapter 3 Numeric Basi
64
Verilog
Denormal Numbers
2 e1 1
Mantissa = 000...0
x M 2 (1 s ) 0.0 2
E
2 e1 1
0.0
65
Verilog
Infinity
Can be used in subsequent calculations,
avoiding need for overflow check
Not-a-Number (NaN)
Indicates illegal or undefined result
66
Verilog
Floating-Point Operations
E.g., addition
67
Verilog
Summary
Unsigned: x xn 1 2 n 1 xn 2 2 n 2 x0 20
Signed: x xn 1 2 n 1 xn 2 2 n 2 x0 20
Octal and Hex short-hand
Operations: resize, arithmetic,
compare
Arithmetic circuits trade off
speed/area/power
Fixed- and floating-point non-integers
Gray codes for position encoding
Digital Design Chapter 3 Numeric Basi
68