210 likes | 367 Views
CSE 575 Computer Arithmetic Spring 2003 Mary Jane Irwin (www.cse.psu.edu/~mji). Residue Number System. Positional - value of symbol is position dependent
E N D
CSE 575Computer ArithmeticSpring 2003Mary Jane Irwin (www.cse.psu.edu/~mji)
Residue Number System • Positional - value of symbol is position dependent • Mixed radix - the number is represented by the residues wrt k pairwise relatively prime moduli (bases) mk-1 > … m1 > m0 (e.g., 8 > 7 > 5 > 3) • Digit set - for the ith position is [0, mi -1] xi = X mod mi = (X)mi • Range - number of distinct values is the product M of the k pairwise moduli RNS(8|7|5|3) gives 840 distinct values
RNS Machine Representation • In practice, each residue is encoded in binary • So the representational efficiency is much less than with binary 840/2048 = 41% = 11 bits mod 3 mod 7 mod 8 mod 5
RNS(8|7|5|3) Examples (0|0|0|0)RNS(8|7|5|3) represents 0 or 840 or … (1|1|1|1)RNS(8|7|5|3) represents 1 or 841 or … (2|2|2|2)RNS(8|7|5|3) represents 2 or 842 or … (0|1|3|2)RNS(8|7|5|3) represents 8 or 848 or … (5|0|1|0)RNS(8|7|5|3) represents 21 or 861 or … (0|1|4|1)RNS(8|7|5|3) represents 64 or 904 or … (2|0|0|2)RNS(8|7|5|3) represents -70 or 770 or … (7|6|4|2)RNS(8|7|5|3) represents -1 or 839 or …
Representing Negative Numbers • Since (-X)mi = (M - X)mi can shift the range from 0 to M to –M/2 to M/2 – 1 or any other interval of M consecutive integers • Negative numbers are represented using a complement system with the complementation constant M • Given the RNS of X, the representation of –X can be found by complementing each of the digits xi wrt its moduli mi (0 digits unchanged)
RNS(5|3|2) Complement Coding M = 5x3x2 = 30 distinct values that can be mapped to 0 to 29 or –15 to +14 000 421 111 220 310 0 -1 +1 -2 +2 201 301 -3 +3 increment 120 -4 +4 410 … +5 021 … -13 221 -14 +14 -15 420 110 +13 = 3 | 1 | 1 001 -13 = 5-3 | 3-1 | 2-1 = 2 | 2 | 1
Residue Arithmetic • Negation • Independently complementing each of the moduli digits (e.g., mi – xi) 1 = (1|1|1|1)RNS(8|7|5|3) then -1 = (8-1|7-1|5-1|3-1)RNS(8|7|5|3) = (7|6|4|2)RNS(8|7|5|3) • Similarly, addition, subtraction, and multiplication are performed by independently operating on each moduli digit • Since each digit is a relatively small number, digit operations are quite fast and simple in RNS
RNS Arithmetic Examples RNS( 8 | 7 | 5 | 3 ) RNS ( 5 | 3 | 2 ) X=+5 ( 5 | 5 | 0 | 2 ) ( | | ) Y=-1 ( 7 | 6 | 4 | 2 ) ( | | ) X+Y=+4 5+7| 5+6|0+4|2+2 ( 4 | 4 | 4 | 1 ) ( | | ) X-Y=+6 5-7|5-6|0-4|2-2 ( 6 | 6 | 1 | 0 ) ( | | ) X*Y=-5 5*7|5*6|0*4|2*2 ( 3 | 2 | 0 | 1) ( | | ) 0 2 1 4 2 1 4 1 0 1 0 0 0 1 1
mod logic RNS ALU ops: +, -, * incr. negate mod 5 unit mod 3 unit mod 8 unit mod 7 unit 2 3 3 3
Residue Arithmetic, Con’t • Unfortunately, the complexity of division and auxiliary operations like sign test, magnitude comparison, and overflow detection can nullify gains (7|2|2|1)RNS(8|7|5|3) (2|5|0|1)RNS(8|7|5|3) • which are negative?, which is larger?, does (1|0|2|2)RNS(8|7|5|3) represent their true sum as opposed to the residue of their sum modulo 840 (i.e., overflow)?
Selecting the Moduli • All moduli must be relatively prime • powers of any two prime numbers are relatively prime • Product of the moduli determines the range of the system • so bigger moduli increase the range • Largest module dictates the speed of arithmetic operations (worst case carry) • try for moduli close to together so that all moduli positions take about the same time to do an arithmetic operation
Selecting the Moduli, Con’t • Moduli with powers-of-2 (mi = 2k) give the most efficient encoding into binary • k bits can encode exactly 2k distinct values • Arithmetic units with mi = 2k or mi = 2k - 1 are easiest to design • can use simple 2s’c and 1s’c binary adders/subtractors/ multipliers • don’t have to do the trailing mod operations (hardware does it automatically !)
Picking Good Moduli • Any list of relatively prime numbers ak-2 > … a1 > a0 can be the basis of the k modulus RNS RNS(2ak-2|2ak-2-1| …| 2a1-1| 2a0-1) • To maximize the dynamic range, the even modulus is chosen to be as large as possible
Some Good Moduli Choices RNS(23| 23-1| 22-1) (8|7|3) M = 168; 8b RNS(24| 24-1| 23-1) (16|15|7) M = 1680; 11b RNS(25| 25-1| 23-1|22-1|) (32|31|7|3) M = 20,832; 15b RNS(25| 25-1| 24-1|23-1|) (32|31|15|7) M = 104,160; 17b RNS(26| 26-1| 25-1) (64|63|31) M = 124,992; 17b RNS(27| 27-1| 26-1|25-1|) (128|127|63|31) M = 31,747,968; 25b
RNS(16|15|7) ALU mod 7 1s’c unit mod 16 2s’c unit mod 15 1s’c unit 4 3 4
RNS(8|7|5|3) ALU ops: +, -, * incr. negate mod 5 unit mod 3 1’sc unit mod 8 2s’c unit mod 7 1s’c unit 2 3 3 3 mod logic
RNS Conversion • Conversion from binary/decimal to RNS • Given a number y, find its residues with respect to the moduli mi, 0 i k-1 • Can use some table lookup short cuts to avoid the divisions • Conversion from RNS to binary/decimal • Makes use of the Chinese remainder theorem
RNS Suitable Applications • ASIC parts that don’t have to conform to the floating point standard • 1,000’s of operations after input conversion before output conversion is necessary • Never have to do sign detect, overflow detect, comparison, division • Some signal processing operations (e.g., filters)
Key References Garner, The Residue Number System, IRE Trans. Electronic Computers, Vol. 8, pp. 140-147, June 1959. Jenkins, Finite Arithmetic Concepts, in Handbook for Digital Signal Processing, pp. 611-675, Wiley, 1993. Parhami, Computer Arithmetic, Oxford Univ. Press, 1999. Soderstrand, Jenkins, Jullien, Taylor, Residue Number System Arithmetic, IEEE Press, 1986. Szabo, Tanaka, Residue Arithmetic and Its Applications to Computer Technology, McGraw-Hill, 1967.