Reconfigurable Computing Systems (252-2210-00L) all 2012 Computer Arithmetic L. Liu Department of Computer Science, ETH Zürich all semester, 2012 Source: ixed-point arithmetic slides come from Prof. Jarmo Takala, Tempere university of technology, inland 1
Number Representation Representation allows an exact value to be assigned to a binary number What is the decimal value of binary number 1011? Is 1011 negative or positive? 2
ixed-point Arithmetic Digital systems are finite bit systems Arithmetic operations are performed with finite-precision numbers Numbers are defined over a finite ring instead of a read field Overflow is possible Integers (integer arithmetic) Two s complement representation ractions 3
Unsigned Integer (b n-1 b n-2 b 0 ) Decimal Value x Quantization Step q n 1 10 = bn 1 + + 2 = 1 2 b + b 1111 0000 1 0 0 1110 15 14 1101 1100 13 12 1 0001 2 0010 0011 3 4 0100 Number Range [0,2 n x10 1] 11 5 1011 0101 10 6 1010 9 7 0110 8 1001 0111 1000 4
Signed Integer (b n-1 b n-2 b 0 ) Decimal Value x n 1 10 = bn 1 + + 2 Quantization Step q = 1 2 b + b 1 0 1110 1101-3 1100-4 1111-2 -1 0000 0 1 0001 2 0010 0011 3 4 0100 Number Range n 1 n 1 x10 [ 2,2 1] -5 5 1011 0101-6 6 1010-7 7 0110-8 1001 0111 1000 5
Properties Multiplication of two unsigned b-bit numbers yields a 2b-bit result law of conservation of bits multiplication of integers means high probability for overflow Multiplication of two signed b-bit numbers yields a 2b bit result only multiplication of 2 b-1 2 b-1 requires 2b-bits in this case, the dynamic range provided by the extra bit is wasted Addition of two b-bit numbers yields a (b+1)-bit result for N additions allocate log2(n) extra bits for result for avoiding overflow for example, addition of 256 numbers without overflow requires 8 extra bits 6
Accumulative Overflow Any chain of arithmetic operations whose final result is in the representable range can be calculated correctly even if overflow has occurred in some intermediate results Calculate 7+6-8=5 with 4-bit signed representation. + + 0111 +7 0110 +6 1101-3 overflow! 1000-8 0101 +5 7
ractional Representation Number field normalized into range [-1, 1) Overflow avoided in multiplication!! -1-1 = 1, 1 [-1, 1)!! As 1 cannot be represented, -1 is not allowed as a multiplier coefficient Also known as Q x representation x is the number of fraction bits or example, Q15 representation contains one bit for sign and 15 bits for fraction b 15 b 14 b 13 b 12 b 11 b 10 b 9 S b 8 b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 binary point 8
ractional Representation Location of the radix-point is just a tool for programming Location is not necessarily fixed The bits of a fractional binary word can also be indexed starting from the MSB, which has an index 0, and continuing with negative indexes With this arrangement, the index of the bit readily tells the weight of that bit, and conversion to the decimal system is easy b 2 4 4 = = 0.0625 b -2 S b 0 b -1 b -3 b -4 b -5 b -6 b -7 b -8 b -9 b -10 b -11 b -12 b -13 b -14 b -15 binary point 9
ractional ixed-point (b n-1 b n-2 b 0 ) Decimal Value x 1 ( n 1) 10 = bn 1 + 2 bn 2 + + 2 b0 Quantization Step q Number Range x 10 = ( 1) 2 n [ 1,1 2 ( n 1) ] 1101 1100 1110 1111-0.125-0.25-0.375-0.5 0000 0001 0 0.125 0.25 0.375 0010 0.5 0011 0100-0.625 0.625 1011 0101-0.75 0.75 1010-0.875 0.875 0110-1 1001 0111 1000 10
ixed-point Overflow overflow characteristics of fixed-point fractional number Saturation arithmetic Source: Bhattacharyya, S.S.; Deprettere,E..; Leupers,R.; Takala,J., Handbook of Signal Processing System. 11
Example 4-Bit Integer Arithmetic Q3 ractional Arithmetic 1101 0011 11111101 1111101 000000 00000 = -3 10 = 3 10 Remember Sign Extension! 1.101 0.011 11.111101 11.11101 00.0000 00.000 = -0.375 10 = 0.375 10 11110111 = -9 10 11.110111 = -0.140625 10 11110111 -> 0111 = 7 10 11.110111 -> 1.110 = -0.250 10 INTEGER ULL PRECISION RACTIONAL 12
Guard Bits Extra bits, which are located to the right of the fraction at the arithmetic unit can be used to avoid the loss of precision during the normalization (left shift) as extra information for rounding 1.0011 b *2 2 1.00110 b *2 2 1.0001 b *2 1 0.10001 b *2 2 guard bit Loss of precision can be avoided by allocating extra bits (guard bits) to the right of fraction in the arithmetic unit 13
Arithmetic Operations ixed-point operations can be carried out precisely, if the result is representable with the given number of bits intermediate overflows produce no error to the final result Successive arithmetic operations increase the wordlength quantization is needed to reduce the number of bits can be implemented as truncation or rounding often, a few guard bits are used in the data path, and only the final result is quantized Arithmetic operations in all finite wordlength systems can introduce error 14
Quantization in ixed-point Quantization Curve Rounding-to-Nearest Q r [x] x Truncation of Two s Complement Q 2t [x] x Truncation of Magnitude Q mt [x] x Quantization Error e r (x) e 2t (x) e mt (x) Amplitude Distribution of Error ρ r (e r ) 1/q -q/2 q/2 x: 1.0110101 + 1 Q[x]: 1.0111 ρ 2t (e 2t ) ρ mt (e mt ) 1/q 1/(2q) e r -q q e 2t -q q e mt x: 1.0110101 Q[x]: 1.0110 x: 1.0110101 + 1 Q[x]: 1.0111 15
Shift-and-Add Multiplication Assume A is the multiplicant, B is the multiplier, this algorithm adds the multiplicant A to itself B times. Calculate 8 * 3 = 24 with two s complement representation. x 01000 011 multiplicant multiplier + 01000 01000 00000 0011000 product 16
Left-Shift Multiplicant Algorithm start A: multiplicant B: multiplier P: product X := A; Y := B; P:=0; N := WordLen(B) no Y0 = 1 yes Steps (N) Mulplicant (X) Multiplier (Y) Product (P) 3 01000 011 00000000 P := P+X 2 010000 01 +00001000 00001000 Shift X left Shift Y right 1 0100000 0 +00010000 00011000 0 01000000 _ +00000000 00011000 N := N-1 no N=0 yes Return P 17
Right-Shift Product Algorithm start A: multiplicant B: multiplier P: product X := A; Y := B; P:=0; N := WordLen(B) no Y0 = 1 yes Steps (N) Mulplicant (X) Multiplier (Y) Product (P) Shift P righ P := P+X 3 01000 011 00000000 2 01000 01 +01000000 01000000 00100000 no Shift Y right N := N-1 N=0 yes Return P 1 01000 0 +01000000 01100000 00110000 0 01000 _ +00000000 00110000 00011000 18
Unsigned Left-Shift Multiplier
Question Which shift-and-add multiplication algorithm is more resource efficient? 20