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