1 ------------------------------------------------------------------------------ 2 ------------------------------------------------------------------------------ 3 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'. -- 4 -- -- 5 -- (C) 2019 Stanislav Datskovskiy ( www.loper-os.org ) -- 6 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html -- 7 -- -- 8 -- You do not have, nor can you ever acquire the right to use, copy or -- 9 -- distribute this software ; Should you use this software for any purpose, -- 10 -- or copy and distribute it to anyone or in any manner, you are breaking -- 11 -- the laws of whatever soi-disant jurisdiction, and you promise to -- 12 -- continue doing so for the indefinite future. In any case, please -- 13 -- always : read and understand any software ; verify any PGP signatures -- 14 -- that you use - for any purpose. -- 15 -- -- 16 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . -- 17 ------------------------------------------------------------------------------ 18 19 with Words; use Words; 20 with FZ_Basic; use FZ_Basic; 21 with FZ_Shift; use FZ_Shift; 22 with FZ_QShft; use FZ_QShft; 23 with FZ_Arith; use FZ_Arith; 24 with FZ_BitOp; use FZ_BitOp; 25 with FZ_Pred; use FZ_Pred; 26 27 28 package body FZ_GCD is 29 30 -- Find Greatest Common Divisor (GCD) of X and Y. 31 -- Note that by convention, GCD(0, 0) = 0. 32 procedure FZ_Greatest_Common_Divisor(X : in FZ; 33 Y : in FZ; 34 Result : out FZ) is 35 36 -- Widths of X, Y, and Result are equal 37 subtype Width is Word_Index range X'Range; 38 39 -- Working buffers for GCD computation, initially equal to the inputs 40 A : FZ(Width) := X; 41 B : FZ(Width) := Y; 42 43 -- Evenness (negation of lowest bit) of A and B respectively 44 Ae, Be : WBool; 45 46 -- Common power-of-2 factor: incremented when Ae and Be are both 1 47 Twos : Word := 0; 48 49 -- This flag is set when A and B are BOTH ODD 50 OO : WBool; 51 52 -- |A - B| 53 D : FZ(Width); 54 55 -- This flag is set iff A < B 56 A_lt_B : WBool; 57 58 begin 59 60 -- To converge, requires number of shots equal to (2 * FZ_Bitness) - 1: 61 for i in 1 .. (2 * FZ_Bitness(X)) - 1 loop 62 63 -- Whether A and B are currently BOTH ODD : 64 OO := FZ_OddP(A) and FZ_OddP(B); 65 66 -- D := |A - B| 67 FZ_Sub_Abs(X => A, Y => B, Difference => D, Underflow => A_lt_B); 68 69 -- IFF A,B both ODD, and A < B : B' := A ; otherwise no change : 70 FZ_Mux(X => B, Y => A, Result => B, Sel => OO and A_lt_B); 71 72 -- IFF A,B both ODD: A' := |A - B| ; otherwise no change : 73 FZ_Mux(X => A, Y => D, Result => A, Sel => OO); 74 75 -- If A is now EVEN: A := A >> 1; otherwise no change 76 Ae := 1 - FZ_OddP(A); 77 FZ_ShiftRight(A, A, WBit_Index(Ae)); 78 79 -- If B is now EVEN: B := B >> 1; otherwise no change 80 Be := 1 - FZ_OddP(B); 81 FZ_ShiftRight(B, B, WBit_Index(Be)); 82 83 -- If both A and B were even, increment the common power-of-two 84 Twos := Twos + (Ae and Be); 85 86 end loop; 87 88 -- Normally, B will contain the GCD, but in the (N,0) N > 0 case -- A. 89 -- The other variable will always equal 0. Hence, take Bitwise-OR(A,B): 90 FZ_Or(X => A, Y => B, Result => A); 91 92 -- Reintroduce the common power-of-2 factor stored in 'Twos' 93 FZ_Quiet_ShiftLeft(N => A, ShiftedN => A, Count => Indices(Twos)); 94 95 -- Output final result -- the GCD. 96 Result := A; 97 98 end FZ_Greatest_Common_Divisor; 99 100 end FZ_GCD;