Академический Документы
Профессиональный Документы
Культура Документы
Number Representation
Representation is consequential
Paolo.Ienne@epfl.ch EPFL I&C LAP Vojin.Oklobdzija@epfl.ch EPFL CSDA and UC Davis ACSEL
Rule of Interpretation
Mapping from set of digit vectors to numbers (e.g., integers, reals)
an 1an 2 ...a2a1a0
Each symbol is a digit
an 1 , an 2 ,K a2 , a1 , a0
Digits usually represent integers from a given sete.g.,
12
twelve
digit vectors
CompArith Number Representation
N, Z, R,
Ienne, Oklobdzjia 2004
Redundancy
A = an 1an 2 ...a2a1a0 = ai wi
where
W = (wn 1 , wn 2 ,...w2 , w1 , w0 )
digit vectors N, Z, R, digit vectors N, Z, R,
Nonredundant
5 CompArith Number Representation
Redundant
Ienne, Oklobdzjia 2004 6 CompArith Number Representation Ienne, Oklobdzjia 2004
Radix Systems
Weights are not arbitrary but related to a radix vector
Mixed-Radix Systems
Fixed-radix if all elements of R are identical
R = (rn 1 , rn 2 ,...r2 , r1 , r0 )
in the following way
wi = r A = ai r i
i i =0
n 1
w0 = 1
wi = wi 1 ri 1 = rj
j =0
i 1
A = an 1an 2 ...a2a1a0 = ai wi
i =0
n 1
ai {0, 1, ..., r 1}
10n possible digit vectors to represent 10n values
It is positional
wi depends only on i
It is fixed-radix Weighted, positional, fixed-radix, nonredundant also called conventional systems
wi = r i
9 CompArith Number Representation Ienne, Oklobdzjia 2004
10
Digit Set
Canonical set
ai {0, 1, ..., r 1}
A canonical system is nonredundant
a1 b1 FA s1
a0 b0 FA s0
Cin
ai { 1, 0, 1, 2}
ai { 1, 0, 1}
ai { ( r / 2 1),..., 0,..., r / 2 1, r / 2}
11 CompArith Number Representation Ienne, Oklobdzjia 2004 12
Slowcritical path contains n full-adders: nave delay is O(n) Must be performed right to left (LSB to MSB)
CompArith Number Representation Ienne, Oklobdzjia 2004
A
2n
B
2n
ai ai-1 bi bi-1 FA
+
2n
FA si si
Critical path now contains only two full-adders: delay is now O(1)
FA si si
Ienne, Oklobdzjia 2004
13
14
One bit represents the sign (typ. 1 neg.) Absolute value represented as unsigned
n2
A = an 2 ...a2a1a0 = ai 10i
i =0
n 2
0 0 0 0 1 1 1
0 0 1 1
0 1 1 1
1 0 1 1
0 0 0 0
1 02 0 12 0 12 0 12
+20
1010 1710
710
17
18
n 1
+ ai 2
Twos Complement
7 6 5 4 3 2 1 0 -1 -2 -3 -4
Amin = 2
n 1
(unsigned)
110
11111111 00000000 -1 0
(signed)
AR
N (unsigned)
CompArith Number Representation
digit vectors
Ienne, Oklobdzjia 2004
Inverse Mapping
Inversely
AR if AR < C / 2 A= AR C if AR > C / 2
AR = A mod C
This is equivalent (for |A| < C) to
True forms
A if A 0 AR = C A = C + A if A < 0
For the representation to be unambiguous, it must be
Complement forms
If AR = C / 2 can be represented, it can be assigned either to A = C / 2 or to A = C / 2, but the representation is then asymmetric ( system not close under sign change operation) If AR = C is can be represented, there are two representations for zero
24 CompArith Number Representation Ienne, Oklobdzjia 2004
A < C/2
or at most equal
23 CompArith Number Representation Ienne, Oklobdzjia 2004
Twos Complement
Ones Complement
7 6 5 4 3 2 1 0 -1
In radix-2
Twos complement: C = 2n Ones complement: C = 2n 1
Twos complement is asymmetric and nonredundant Ones complement is symmetric with a redundant zero
25 CompArith Number Representation Ienne, Oklobdzjia 2004 26
-2 -3 -4
A = AR an 1 = 0
If AR C / 2
A = ai 2i
i =0
n 2
A = AR an 1 = 0
If AR C / 2
n 2 i =0
A = ai 2i
i =0
n 2
A = AR C an 1 = 1
A = 2n + ai 2i = 2n + 2n 1 + ai 2i =
i =0
n 1
A = AR C an 1 = 1
A = (2n 1) + ai 2i = 2n + 2n 1 + 1 + ai 2i =
i =0 i =0
n 1
n 2
= 2n 1 + ai 2i
i =0
n 2
= 2n 1 + 1 + ai 2i
i =0
n 2
A = an 1 2
27
+ ai 2
i =0
n 2
A = an 1 (2
Ienne, Oklobdzjia 2004 28
n 1
+ 1) + ai 2i
i =0
Ienne, Oklobdzjia 2004
n 2
Sign
0 if A 0 sign( A) = 1 if A 0
For sign-and-magnitude is clearly trivial For true-and-complement too, it is
sign( A) = an 1
29 CompArith Number Representation Ienne, Oklobdzjia 2004 30 CompArith Number Representation Ienne, Oklobdzjia 2004
(n)
n 1
12
+20
+ ai 2i
i =0
n2
4 bits
23 +22
310
8 bits
1
27
1
+26
1
+25
1
+24
1
+23
1
+22
12
+20
310
m2
j = n 1
n 1
2 + ai 2i
j i =0
n2
( 2
m 1
+ 2 m 2 + ... + 2 n 1 2 n 1 0
CompArith Number Representation Ienne, Oklobdzjia 2004
) (
32
Addition in True-and-Complement
If there is no overflow (that is, we assume the result of the sum to be representable), S = A + B can be simply computed as
S R = ( AR + BR ) mod C
Easy to prove using (X mod Y) mod Y = X mod Y
Cout
Cin
S
Remember the intuitive derivation of the twos complement representation
34 CompArith Number Representation Ienne, Oklobdzjia 2004
wn = 1 and WR mod (r n 1) = WR r n + 1
Cout
Hence
If wn = 0 S R = WR If wn = 1 S R = WR + 1, subtracting r n = 2n simply by ignoring wn
Cin
S
35 CompArith Number Representation Ienne, Oklobdzjia 2004 36 CompArith Number Representation Ienne, Oklobdzjia 2004
Negation
Bitwise negation
A = not( A) = an 1 an 2 K a2 a1 a0
where, as usual in binary,
because an 1 an 2 an 1 an 2 r 1 r 1 1
38
A + A + 1 = rn
a = ( r 1) a
Example in radix-2 on 4 bits:
a2 a2
a1 a1
a0 a0 1
+ = + =
A A 1 rn
r 1 r 1 r 1 0 0 0
Change of Sign
In ones complement
A= A
A = r n 1 A = A if A 0 A = r n 1 + A = r n ( r n 1 + A) 1 = A if A < 0
A + A = 1
Use twos complement property/definition
n2 n2 n 1 i n 1 an 1 2 + ai 2 + an 1 2 + ai 2i = i =0 i =0
In twos complement
A = A +1
A = r n A = A + 1 if A 0 A + 1 = r n + A + 1 = r n ( r n + A) 1 + 1 = A if A < 0
39 CompArith Number Representation Ienne, Oklobdzjia 2004
= an 1 + an 1 2 n 1 + ai + ai 2i = 2 n 1 + 2i = 1
i =0 i =0
40 CompArith Number Representation Ienne, Oklobdzjia 2004
n2
n2
Sub
Cout
Cin
Overflow?!
Cout
Cin
S
41 CompArith Number Representation Ienne, Oklobdzjia 2004 42
S
CompArith Number Representation Ienne, Oklobdzjia 2004
Overflows if carry out is 1that is, if the first bit which cannot be stored is not null
43 CompArith Number Representation Ienne, Oklobdzjia 2004
Carry out is 1, but now it is ok because the result is negative (think of sign extension)
44 CompArith Number Representation Ienne, Oklobdzjia 2004
Hence
OVF+ = an 1 bn 1 sn 1 + an 1 bn 1 sn 1 OVF = an 1 bn 1 sn 1 + an 1 bn 1 sn 1
46 CompArith Number Representation Ienne, Oklobdzjia 2004
-128
-1 0
127 128
255
45
Overflows Addition
Considering all possible cases around the last FA
an 1 0 0 0 0 1 1 1 1 bn 1 0 0 1 1 0 0 1 1 cn 1 0 1 0 1 0 1 0 1 cn 0 0 0 1 0 1 1 1 sn 1 0 1 1 0 1 0 0 1 Overflow No Yes No No No No Yes No
Fixed Point
In real life, one does not only need integers If one adds a fractional point in a fixed position, hardware for integers works just as well 0 1 0 0 12 + 0 0 0 1 12 0 1 1 0 02
24 23 22 21 20
0 1. 0 0 12 + 0 0. 0 1 12 0 1. 1 0 02
21 20 2-1 2-2 2-3
OVF+ = cn cn 1
47 CompArith Number Representation Ienne, Oklobdzjia 2004
Fixed Point Requires Attention from Programmers Multiplication often introduces the need of arithmetic shifts to the right
0. 1 0 1 02 0. 0 1 1 02 0. 0 0 1 1 1 1 0 02
References
M. D. Ercegovac and T. Lang, Digital Arithmetic, Morgan Kaufmann, 2004 I. Koren, Computer Arithmetic Algorithms, Peters, 2002 A. R. Omondi, Computer Arithmetic Systems Algorithms, Architecture and Implementation, Prentice Hall, 1991 B. Parhami, Computer ArithmeticAlgorithms and Hardware Designs, Oxford, 2000
0. 0 0 1 12