Residue Number System

advertisement
CENG536
Computer Engineering department
Çankaya University
Residue number systems (RNS) were invented by the thirdcentury Chinese scholar Sun Tzu – a different Sun Tzu from
the author of the famous Art of War. In Problem 26 of his
Suan Ching (Calculation Classic), our Sun Tzu posed a
mathematical riddle:
We have things of which we do not know the number
If we count them by threes, we have two left over
If we count them by fives, we have three left over
If we count them by sevens, we have two left over
How many things are there?
2
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
In other words, “What number yields the remainders 2, 3,
and 2 when divided by 3, 5, and 7, respectively?" In modern
terminology, 2, 3, and 2 are residues, and 3, 5, and 7, are
moduli. Sun Tzu gave a rule, the Tai-Yen (Great
Generalization) for the solution of his puzzle. In 1247,
another Chinese mathematician, Qin Jiushao, generalized
the Great Generalization into what we now call the Chinese
Remainder Theorem, a mathematical jewel.
3
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
In the 1950s, RNS were rediscovered by computer
scientists, who sought to put them to use in the
implementation of fast arithmetic and fault-tolerant
computing. Three properties of RNS make them well suited
for these.
The first is absence of carry propagation in addition and
multiplication, carry-propagation being the most significant
speed-limiting factor in these operations.
4
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
The second is that because the residue representations carry
no weight-information, an error in any digit-position in a
given representation does not affect other digit-positions.
And the third is that there is no significance-ordering of
digits in an RNS representation, which means that faulty
digit-positions may be discarded with no effect other than a
reduction in dynamic range.
5
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Papers printed by Czech scientists M. Valach and A.
Svoboda were the impulse to start explorations in RNS
mathematic (1955 – 1957).
Soviet scientists I. Akushskiy, D. Yuditskiy, V. Amerbayev
and B. Pak analyzing and developing this system have
offered computer arithmetic (1955 – 1962).
RNS was used in few military systems of USSR designed in
period 1958 – 1970.
6
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Anti-missile defense system A-35 Exoatmospheric missile А-350Ж (1958-1968)
(distance of flight 400 km, height range of defeat 50-400 km,
7
time of flight (max) 3 min, nuclear warhead)
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Anti-missile defense system A-35
Exoatmospheric missile А-350Ж
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
8
Anti-missile defense system A-35
Radar "Дунай-3У" (1958-1968)
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
9
Anti-missile defense system A-35
Control center
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
10
Soviet military computer 5Э53 – designed for anti-missile defense
system A-35 (1967-1972). Work in residue number system. Rate 40
MIPS. Clocking frequency 6 MHz. RAM 10 Mbit. ROM 2,9 Mbit.
External memory 3 Gbit. Harvard architecture. Pipelined arithmetic.
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
11
Front bomber СУ24 (First flight – 1967).
Equipped by control computer based on RNC.
12
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Western definition of RNS :
Residue number systems are based on the congruence
relation, which is defined as follows. Two integers a and b
are said to be congruent modulo m if m divides exactly the
difference of a and b; it is common, especially in
mathematics tests, to write a  b (mod m) to denote this.
Thus, for example, 10  7 (mod 3); 10  4 (mod 3); 10  1
(mod 3), and 10  – 2 (mod 3). The number m is a modulus
or base, and we shall assume that its values exclude unity,
which produces only trivial congruences.
13
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Western definition of RNS :
If q and r are the quotient and remainder, respectively, of
the integer division of a by m – that is, a = q:m + r – then,
by definition, we have a  r (mod m). The number r is said
to be the residue of a with respect to m, and is usually
denoted by r = |a|m. The set of m smallest values {0, 1, 2,
…., m – 1}, that the residue may assume is called the set of
least positive residues modulo m. Unless otherwise
specified, we shall assume that these are the only residues
in use.
14
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Russian definition of RNC:
Let is given set of positive integer relatively prime numbers
that are the moduli (bases) of the system.
The residue number system is that system in which integer positive
number is presented by set of residui (remainders) on selected moduli
where each i is determined according equation
for each
where operation [ . ] means separation of integer part.
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
15
Theory of numbers proves that, if numbers pi are relatively prime, the
number described by digits
has
the
unique
representation.
The range of numbers (dynamic range), represented in this system is
Like in positional system range of numbers increases as product of
moduli and number of digits to represent N increases as the sum of
word lengths of the same bases.
16
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example:
p1 = 3, p2 = 5, p3 = 7 – the moduli of the RNS system
P = 3 ∙ 5 ∙ 7 = 105 – range of numbers presentation
0 ≡ ( 0, 0, 0 )
1 ≡ ( 1, 1, 1 )
2 ≡ ( 2, 2, 2 )
3 ≡ ( 0, 3, 3 )
4 ≡ ( 1, 4, 4 )
5 ≡ ( 2, 0, 5 )
6 ≡ ( 0, 1, 6 )
7 ≡ ( 1, 2, 0 )
8 ≡ ( 2, 3, 1 )
.
.
.
103 ≡ ( 1, 3, 5 )
104 ≡ ( 2, 4, 6 )
105 ≡ ( 0, 0, 0 )
106 ≡ ( 1, 1, 1 )
17
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
included
not included
Lets analyze the rules of execution of arithmetic operations for the
case of both operands and result are in range [ 0, P ).
and the conditions
are satisfied.
18
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Residui for addition and multiplication will be determined as
and the digit of results is taken from
19
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
For addition we can write
Representation of A and B are
where ki and li are non negative integers.
Then
From which
20
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
In case of multiplication
Substituting representation of A an B gives
From which
21
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 1:
Let the moduli of the system are
We want add numbers A = 17 and B = 63.
For the given moduli numbers are represented as
Result of addition is
Its easy check that (2, 0, 3) is 80 and is equal to the sum of operands.
22
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 2:
For the system with the same moduli
we want multiply numbers
In accordance with the multiplication rules result is
Its easy to check, that this number is 102 and is equal to product of
operands.
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
23
Advantages of the system:
- Independence of creating (determining) of the digits, as
each residue carry information about whole original number.
- Independence of digits of each other and from this
possibility of parallel processing of residui.
- Small number of bits to represent each residue, that opens
possibility to process data using tables, instead of units on
the base of logical elements.
24
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Disadvantages of the system:
- Impossibility to visual comparing of the numbers.
- Absence of simple indication of overflow and underflow
over the range [ 0, P ).
- Limitedness of the system by the area of integer positive
numbers.
- Obtaining in all cases accurate results and absence of
possibilities of rounding and truncation.
25
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
How to introduce negative numbers?
Let be realized subtraction of two numbers for the same system. Result is
presented by residui i for the same bases
And let conditions
Then for subtraction we have
are satisfied.
26
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Sign of result is not determined. Strongly say, sign of operands were not
processed (in representation of numbers they are absent) and the sign of
result is not created.
Let for the system with the bases
the range of
numbers is
Let the base
We denote
or
In residue number system this number is presented as
27
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Accepting this number as zero we can represent positive numbers
in form
and negative numbers
as
Realizing algebraic addition positive and negative numbers will be
represented as
This form is referred to as artificial form and we always will deal with
positive numbers.
But, the numbers in range
artificial form, and in range
will reflect negative numbers in this
– positive.
28
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Let we add two numbers N1 and N2. The artificial forms are
and their sum is
Artificial form of the sum is presented by
then from this
and because
is half of the range, we can write
29
0
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 1:
Given the system with the moduli and half of range
Let
We create artificial forms
And determining sum of this numbers we get
The number
is artificial form of the sum, that may be
checked by conversion to decimal
30
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 2:
For the same system with the moduli and half of range
Let
The artificial forms are
Determining sum of this numbers we get
positive
negative
Result is artificial form of the sum, that may be checked by conversion
to decimal
31
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 3:
For the same system we add numbers
We create artificial forms
negative numbers
Adding this numbers we get
Result is artificial form of the sum, that may be checked by conversion
to decimal
32
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Transform of positive numbers to negative and vice versa, i.e. creating
of its complement is realized by subtracting of this number from number
For analyzed examples this transform may be illustrated as
Note: If subtrahend is in artificial form, to form the complement it
should be subtracted not from
but from
33
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example:
Given numbers
Their artificial forms are
complement code of N2 is
Sum of
and
is
From one of preceding examples is known that
34
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example:
Given numbers
Lets create complement code of N2 and add these numbers
Producing mathematical operations in decimal we get
35
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Lets analyze multiplication. Artificial forms of the two numbers are
then
from which
Taking in account that
artificial form we get
and because that numbers are in
Because P is odd number, we have
The rule for multiplication is defined in form:
if N1 and N2 are of the same parity
if N1 and N2 are of different parity
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
36
Because one of the basis of the system is 2, we can
determine parity (odd, even) of the number analyzing
residue by this modulo. Odd numbers have residue
of 1 and even – 0.
37
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 1:
For the same system let
Artificial form of numbers are
Product of numbers is
Because both numbers are of the same parity we get
Applying decimal conversion gives
38
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 2:
For the same system let
Artificial form of numbers are
Product of numbers is
Because both numbers are of the same parity we get
Applying decimal conversion gives
39
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 3:
For the same system let
Artificial form of numbers are
and their product is
From Example 1 we have
same result gives
and the
40
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 4:
Now let numbers for the same system are of different parity
Artificial form of numbers are
and their product is
Applying rule for multiplicand and multiplier of different parity
gives
In decimal system we get
41
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 5:
Now let numbers for the same system are of different parity
Artificial form of numbers are
and their product is
Applying rule for multiplicand and multiplier of different parity
gives
In decimal system we get
42
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
The general approach to the introduction of
negative numbers.
Let moduli of the system are
and let
The range of numbers
is divided in two parts
and
.
Let
43
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Let the number
Positive numbers
and negative one
will represent zero for the system.
will be represented in form
will be of form
General description for all numbers is
Such representation is referred to as general artificial form of
the number.
44
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
If no overflow/underflow while arithmetic operation is
processed, addition and subtraction may be realized in artificial
form
Artificial form of the sum is
Lets denote
as
Here we have
And sum of numbers in artificial code is
45
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Complement code of N may be obtained by subtracting the
number from
If the number is represented in artificial form, complement code
should be obtained by subtracting from
46
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 1:
Let system has moduli
For this system
and
Let
The artificial forms are
According to introduced rules
And in decimal system
47
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 2:
Let
The artificial forms are
And in decimal system
48
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Now lets analyze multiplication representing numbers in
general artificial form. Let numbers are
and
then
Denoting
we have
from which
We can denote
Integer part
Fraction part
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
49
Now new edition of this expression is of form
If  is even, then
and
If  is odd, then
and
that gives
in this case
50
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Lets introduce variable t which is defined as
if  is even, then
if  is odd, then
and we have
Best form for  is in artificial representation of operands
51
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 1:
Let
Artificial forms are
Value of  and t are determined as
Product is
In decimal
52
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Example 2:
Let
Artificial forms are
Value of  and t are determined as
Product is
In decimal
53
CENG 536 - Spring 2012-2013 Dr. Yuriy ALYEKSYEYENKOV
Download