1 a redundant binary euclidean gcd algorithm s. n. parikh, d. w. matula computer arithmetic 10th...
Post on 21-Dec-2015
214 views
TRANSCRIPT
1
A Redundant Binary Euclidean GCD Algorithm
S. N. Parikh, D. W. MatulaComputer Arithmetic10th IEEE Symposium
pp.220-2251991
陳正昇
2
Outline Binary Euclidean Algorithm Redundant Binary Representation
and normalization Redundant Binary Euclidean
Algorithm
3
Binary Euclidean Algorithm Normalized
數字介於 [1/2,1),(-1,-1/2] 之間 Time complexity
O(nlogn) with O(n) hardware
4
Binary Euclidean Algorithm
1. While P and Q not normalized do //normalize P and Q 2. leftshift P,Q,Up,Uq; 3. While Q != 0 do begin //find GCD(P,Q) , terminates when Q=0 4. While Q not normalized do //normalize Q 5. leftshift Q and Uq; 6. Loop //find the remainder of P/Q 7. While P not normailzed do //normalize P 8. Exit Loop if Uq=Up; 9. Leftshift P and Up; 10. End; 11. If sign(P)=sign(Q) then //find the difference of P and Q 12. P=P-Q 13. else P=P+Q; 14. EndLoop; 15. Swap(P,Q);Swap(Up,Uq); //interchange of divisor and remain
der 16. End; 17. While Up != 1 do rightshift P and Up; //shift back the P
5
Binary Euclidean Algorithm
1. While P and Q not normalized do //normalize P and Q 2. leftshift P,Q,Up,Uq; 3. While Q != 0 do begin //find GCD(P,Q) 4. While Q not normalized do //normalize Q 5. leftshift Q and Uq; 6. Loop //find the remainder of
P/Q 7. While P not normailzed do //normalize P 8. Exit Loop if Uq=Up; 9. Leftshift P and Up; 10. End;
6
Binary Euclidean Algorithm
11. If sign(P)=sign(Q) then //find the difference of P and Q
12. P=P-Q 13. else P=P+Q; 14. EndLoop; 15. Swap(P,Q);Swap(Up,Uq); //interchange of divisor and rema
inder 16. End; 17. While Up != 1 do rightshift P and Up; //shift back the P
7
EX. of Binary Euclidean Algorithm
P = 27(dec) = 11011(bin) 27/32 = 0.11011(bf)
Q = -12(dec) = -1100(bin) -12/32 = -0.011(bf)
GCD(P,Q) = 3(dec) = 11(bin) 3/32 = 0.00011(bf)
8
EX. of Binary Euclidean Algorithm
Step( 演算法第幾行 )
P(dec) Q(dec) P(bin) Q(bin) Up Uq
27/32 -12/32 0.11011 -0.011 1 1
2. -24/32 -0.11 2
12. 3/32 0.00011
15. -24/32 3/32 -0.11 0.0001
1
2 1
9
EX. of Binary Euclidean Algorithm
Step( 演算法第幾行 )
P(dec) Q(dec) P(bin) Q(bin) Up Uq
-24/32 3/32 -0.11 0.0001
1
2 1
5. 24/32 0.11 8
13. 0 0
15. 24/32 0 0.11 0 8 2
10
EX. of Binary Euclidean Algorithm
3/32 = 0.00011(bf) GCD(P,Q) = 11(bin) = 3(dec)
Step( 演算法第幾行 )
P(dec) Q(dec) P(bin) Q(bin) Up Uq
24/32 0 0.11 0 8 2
17. 3/32 0.00011 1
11
Redundant Binary Representation and normalization
Binary redundant representation
(b = 2, -1 aj 1 )
n = (1864)10 = (11101001000)2= (1001
01001000)2
1 = -1
12
Redundant Binary Representation and normalization
Signed bit fraction
b0.b1b3b4```bk , bi = 1,0,1 for all i
1 = -1
Normalized standard form :
b0=0 , b1!=0 and b2!=-b1
Complement standard form :
b0!=0 , b1=0
14
Redundant Binary Representation and normalization
Simshift(P)=
0.b1b3b4```bk , if b1!=0 and b1=-b2
0.b2b3b4```bk , if b1=0
在 Redundant Binary 形式下的 leftshift (*2)
15
Redundant Binary Representation and normalization
decomp(P)=
0.b0b2b3```bk , if b0=-b1!=0
P , if b0=0
轉成 Normalized standard form
16
Redundant Binary Representation and normalization
a diff b= a-b for ab >= 0 (a,b 同號 ) a+b for ab < 0 (a,b 不同號 )
求 a 和 b 的差
17
Redundant Binary Euclidean Algorithm
1. While P and Q not normalized do 2. Simshift P,Q and leftshift Up,Uq //Simshift P and Q 3. LoopA 4. While Q not normalized do //Simshift P 5. Simshift Q and leftshift Uq; 6. Exit LoopA if Uq overflows; 7. LoopB //find the remainder of P/Q 8. While P not normalized do //Simshift Q 9. Exit LoopB if Up=Uq; 10. Simshift P and leftshift Up; 11. If(P diff Q) is normalized then //find the difference of P and Q 12. If sign(P diff q) != sign(P) then 13. If Up != Uq then 14. leftshift Up; P = 2P diff Q; 15. Else exit LoopB; 16. Else P = P diff 2Q; 17. Else P = P diff Q; 18. P = decomp(P); //transform p to normalized standard for
m 19. Simshift P; //Simshift P 20. If Up != Uq then leftshift Up; 21. Else leftshift Up and exit LoopB; 22. End LoopB; 23. Swap(P,Q);Swap(Up,Uq); //interchange of divisor and remainder 24. End LoopA; 25. While Up != 1 do rightshift P,Up //shift back the P
18
Redundant Binary Euclidean Algorithm
1. While P and Q not normalized do 2. Simshift P,Q and leftshift Up,Uq //Simshift P and Q 3. LoopA 4. While Q not normalized do //Simshift P 5. Simshift Q and leftshift Uq; 6. Exit LoopA if Uq overflows; 7. LoopB //find the remainder of P/Q 8. While P not normalized do //Simshift Q 9. Exit LoopB if Up=Uq; 10. Simshift P and leftshift Up;
//find the difference of P and Q
11. If(P diff Q) is normalized then 12. If sign(P diff q) != sign(P) then 13. If Up != Uq then 14. leftshift Up; P = 2P diff Q; 15. Else exit LoopB;
19
Redundant Binary Euclidean Algorithm
16. Else P = P diff 2Q; 17. Else P = P diff Q;
//transform p to normalized standard form 18. P = decomp(P); 19. Simshift P; //Simshift P 20. If Up != Uq then leftshift Up; 21. Else leftshift Up and exit LoopB; 22. End LoopB;
//interchange of divisor and remainder 23. Swap(P,Q);Swap(Up,Uq); 24. End LoopA; 25. While Up != 1 do rightshift P,Up //shift back the P
20
Redundant Binary Euclidean Algorithm
Time complexity Addition is O(n) 4-2 signed 1-bit
adders O(n) with O(n) bit level
processors
21
EX. of Redundant Binary Euclidean Algorithm
P = 27(dec) = 011011(bin) 27/32 = 0.11011(sbf)
Q = -12(dec) = 111001(1`sc) -12/32 = 0.011(sbf) GCD(P,
Q) = 3(dec) = 000011(bin) 0.00011 (sbf) = 3/32
22
EX. of Redundant Binary Euclidean Algorithm
Step( 演算法第幾行 )
P Q P(sbf) Q(sbf) Up Uq P diff
Q
27/32 -12/32 0.1101
1
0.011 1 1
11. 15/32
16. 3/32 0.0001
1
19. 6/32 0.0011
21. 2
23. -
12/32
6/32 0.011 0.0011 1 2
23
EX. of Redundant Binary Euclidean Algorithm
Step( 演算法第幾行 )
P Q P(sbf) Q(sbf) Up Uq P diff
Q
-
12/32
6/32 0.011 0.0011 1 2
5. 12/32 0.011 4
11. 0
17. 0 0.00
20. 2
24
EX. of Redundant Binary Euclidean Algorithm
Step( 演算法第幾行 )
P Q P(sbf) Q(sbf) Up Uq P diff
Q
0 12/32 0.00 0.011 2 4
10. 0 0.00 4 4
23. 12/32 0 0.011 0.00 4 4