Residual Number System
Residual Number System
Overview of
Residue Number System ((RNS)
RNS)
NTUEE 吳安宇 教授
ACCESS IC LAB
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Outline
History
Why RNS is needed ?
Fundamental concepts in RNS.
Conversion between Decimal and Residue.
Conventional complex RNS
Difficult arithmetic operations
Applications
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
History
The ancient study of the residue numbering
system begins with a verse from a third-
centry book, Suan-Ching, by Sun Tzu.
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
History
We commemorate this contribution as the
Chinese Remainder Theorem (CRT).
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
a4 b4 a3 b3 a2 b2 a1 b1 a0 b0
FA FA FA FA HA
cout s4 s3 s2 s1 s0
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Background
The RNS is defined in terms of a set of
relatively prime moduli.
If P denotes the moduli set, then
P = {p1 , p2 , , pL }
GCD( pi , p j ) = 1, for i ≠ j
The dynamic range M is
M = p1 p2 pL
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Background
Any integer in the residue class ZM has a
unique L-tuple representation given by
( X 1, X 2 ,
X →RNS
, XL)
where Xi=X mod pi and is called the ith
residue.
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Background
P = {p1 , p2 } = {3,5} M = 3 × 5 = 15
X → X1 X2 X → X1 X2 X → X1 X2
0 → 0 0 5 → 2 0 10 → 1 0
1 → 1 1 6 → 0 1 11 → 2 1
2 → 2 2 7 → 1 2 12 → 0 2
3 → 0 3 8 → 2 3 13 → 1 3
4 → 1 4 9 → 0 4 14 → 2 4
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Background
P = {p1 , p2 , p3 } = {3,4,5} M = 3 × 4 × 5 = 60
X → X1 X2 X3 X → X1 X2 X3 X → X1 X2 X3 X → X1 X2 X3 X → X1 X2 X3
0 → 0 0 0 12 → 0 0 2 24 → 0 0 4 36 → 0 0 1 48 → 0 0 3
1 → 1 1 1 13 → 1 1 3 25 → 1 1 0 37 → 1 1 2 49 → 1 1 4
2 → 2 2 2 14 → 2 2 4 26 → 2 2 1 38 → 2 2 3 50 → 2 2 0
3 → 0 3 3 15 → 0 3 0 27 → 0 3 2 39 → 0 3 4 51 → 0 3 1
4 → 1 0 4 16 → 1 0 1 28 → 1 0 3 40 → 1 0 0 52 → 1 0 2
5 → 2 1 0 17 → 2 1 2 29 → 2 1 4 41 → 2 1 1 53 → 2 1 3
6 → 0 2 1 18 → 0 2 3 30 → 0 2 0 42 → 0 2 2 54 → 0 2 4
7 → 1 3 2 19 → 1 3 4 31 → 1 3 1 43 → 1 3 3 55 → 1 3 0
8 → 2 0 3 20 → 2 0 0 32 → 2 0 2 44 → 2 0 4 56 → 2 0 1
9 → 0 1 4 21 → 0 1 1 33 → 0 1 3 45 → 0 1 0 57 → 0 1 2
10 → 1 2 0 22 → 1 2 2 34 → 1 2 4 46 → 1 2 1 58 → 1 2 3
11 → 2 3 1 23 → 2 3 3 35 → 2 3 0 47 → 2 3 2 59 → 2 3 4
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Background
For signed RNS, any integer in (-M/2, M/2],
has a unique RNS L-tuple representation
where xi=(X mod pi) if X>0,
and (M-|X|) mod pi otherwise.
P = {p1 , p2 } = {3,4}
X → X1 X2 X → X1 X2 X → X1 X2
-6 → 0 2 -2 → 1 2 2 → 2 2
-5 → 1 3 -1 → 2 3 3 → 0 3
-4 → 2 0 0 → 0 0 4 → 1 0
-3 → 0 1 1 → 1 1 5 → 2 1
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Background
→( X 1 , X 2 ,
X RNS
, XL)
→(Y1 , Y2 ,
Y RNS
, YL )
Z=X Y
present addition, subtractio n and multiplica tion
→(Z1 , Z 2 ,
Z RNS
, ZL )
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Background
7→(1, 3, 2 )
+3→(0, 3, 3 )
10→(1 mod 3, 6 mod 4, 5 mod 5) = (1,2,0)
7→(1, 3, 2 )
- 3→(0, 3, 3 )
4→(1 mod 3, 0 mod 4, -1 mod 5) = (1,0,4)
7→(1, 3, 2 )
*3→(0, 3, 3 )
21→(0 mod 3, 9 mod 4, 6 mod 5) = (0,1,1)
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Background
Xi (n bits)
RAM or ROM
Zi (n bits)
Table Lookup
Yi (n bits)
Zi = (Xi o Yi) mod pi
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Decimal-to-residue conversion
To be competitive system, the speed of data
acquisition and the accompanying decimal-to-
residue conversion must be equally fast.
N −1
X 2 's −comp = (bN bN −1 b2b1b0 ) = −bN 2 N + ∑ b j 2 j
j =0
X i ≡ X mod pi
N −1
= bN ( pi − (2 mod pi )) + ∑ b j (2 mod pi ) mod pi
N j
j =0
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Decimal-to-residue conversion
P = ( p1 , p2 ) = (5,7 ) X = (29 )decimal = (011101)binary
N −1
bN ( pi − (2 mod pi )) + ∑ b j (2 mod pi )
N j
j =0
pi-(25mod pi) 2j mod pi j=4 j=3 j=2 j=1 j=0
i=1 3 i=1 1 3 4 2 1
i=2 3 i=2 2 1 4 2 1
X 1 = (0 ⋅ 3 + 1 ⋅ 1 + 1 ⋅ 3 + 1 ⋅ 4 + 0 ⋅ 2 + 1 ⋅ 1) mod 5 = 4
X 2 = (0 ⋅ 3 + 1 ⋅ 2 + 1 ⋅ 1 + 1 ⋅ 4 + 0 ⋅ 2 + 1 ⋅ 1) mod 7 = 1
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Residue-to-decimal conversion
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Multiplicative inverse
The multiplicative inverse of g of modulo pi is
denoted as gi-1 and satisfies
(g g ) mod p
−1
i i =1
g 0 1 2 3 4
Pi=5
gi-1 Χ 1 3 2 4
Example: g 0 1 2 3 4 5
Pi=6
gi-1 Χ 1 Χ Χ Χ 5
g 0 1 2 3 4 5 6
Pi=7
gi-1 Χ 1 4 5 2 3 6
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
X
RNS
( X 1, X 2 , , XL)
(Xˆ , Xˆ ,
1 2 , Xˆ L ) MRC
(
X = v1 Xˆ 1 + v2 Xˆ 2 + + vL Xˆ L mod M)
where v1=1, v2=p1, v3=p1p2, v4=p1p2p3, …
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
X mod p1 = X 1 = Xˆ 1
X − Xˆ 1 = p1 Xˆ 2 + p1 p2 Xˆ 3 + + p1 p2 … pL−1 Xˆ L
( )
⇒ p1−1 X − Xˆ 1 = Xˆ 2 + p2 Xˆ 3 + + p2 … pL−1 Xˆ L
⇒ [p (X − Xˆ )]mod p
−1
1, 2 1 2 = Xˆ 2
[p ( X
−1
1, 2 2 ]
− X 1 ) mod p2 = Xˆ 2
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
{ p [p (X − Xˆ ) − Xˆ ] } mod p
−1
2,3
−1
1, 3 3 1 2 3 = Xˆ 3
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
X2 X̂ 2
p12-1 M
X3 X̂ 3
p13-1 M p23-1 M
XL X̂ L
p1L-1 M p2L-1 M pL-1,L-1 M
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
L
[
X = ∑ si (X i si )mod pi
−1
]
mod M
i =1
where si=M/pi and si-1 is the multiplicative
inverse of si mod pi, so that
(s s )mod p
−1
i i i =1
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
L
[
X = ∑ si (X i si )mod pi
−1
]
mod M
i =1
X1
X1 s1-1 mod p1 s1
X2
X2 s2-1 mod p2 s2
X
mod M
XL
XL sL-1 mod pL sL
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
1*2 mod 3 20
X =4
X = (1,0,4 ) 0*3 mod 4 15 mod 60
4*3 mod 5 12
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Z = X + jY RNS →(Z1 , Z 2 , , Z L ); X ∈ Z M , Y ∈ Z M
Z i = (( X mod pi ) + j (Y mod pi ))
Z 3 = Z1 + Z 2 = (Z 31 , Z 32 , , Z 3L )
Z 3i = ( X 1i + X 2i ) + j (Y1i + Y2i )
Z 3 = Z1 ∗ Z 2 = (Z 31 , Z 32 , , Z 3L )
Z 3i = ( X 1i X 2i − Y1iY2i ) + j ( X 1iY2i + X 2iY1i )
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Magnitude comparison
Sign Detection
X = (2,3,3) Y = (1,1,0 ) Z = (3,3,3)
X >Y ? Y > 0? Z > 6?
Not a weighted number system.
Every digits are equally important.
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Division
Blend of nested subtractions and magnitude
comparisons, so it is more difficult in the RNS.
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Base extension
Increase the dynamic range
Increase the resolution.
Moduli
R/D conversion extension
D/R conversion
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Overflow detection
Take the form of magnitude comparison.
To be avoided by using scaling operation
during run-time.
台灣大學 吳安宇 教授
ACCESS IC LAB Graduate Institute of Electronics Engineering, NTU
Applications
Filtering (FIR, IIR)
Adaptive system
Linear transformations
台灣大學 吳安宇 教授