-
+ 46157DCF363B52695F6EC10671307155E62702A1EA79861A067F56716B03ACB8573988BED3E2350401AD8222F7351F96D22383F2B7B5FC8EBC1D9901E24B6F63
ffa/libffa/fz_gcd.adb
(0 . 0)(1 . 89)
670 ------------------------------------------------------------------------------
671 ------------------------------------------------------------------------------
672 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'. --
673 -- --
674 -- (C) 2019 Stanislav Datskovskiy ( www.loper-os.org ) --
675 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html --
676 -- --
677 -- You do not have, nor can you ever acquire the right to use, copy or --
678 -- distribute this software ; Should you use this software for any purpose, --
679 -- or copy and distribute it to anyone or in any manner, you are breaking --
680 -- the laws of whatever soi-disant jurisdiction, and you promise to --
681 -- continue doing so for the indefinite future. In any case, please --
682 -- always : read and understand any software ; verify any PGP signatures --
683 -- that you use - for any purpose. --
684 -- --
685 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . --
686 ------------------------------------------------------------------------------
687
688 with Words; use Words;
689 with FZ_Basic; use FZ_Basic;
690 with FZ_Shift; use FZ_Shift;
691 with FZ_QShft; use FZ_QShft;
692 with FZ_Arith; use FZ_Arith;
693 with FZ_Pred; use FZ_Pred;
694
695
696 package body FZ_GCD is
697
698 -- Find Greatest Common Divisor (GCD) of X and Y.
699 -- Note that by convention, GCD(0, 0) = 0.
700 procedure FZ_Greatest_Common_Divisor(X : in FZ;
701 Y : in FZ;
702 Result : out FZ) is
703
704 -- Widths of X, Y, and Result are equal
705 subtype Width is Word_Index range X'Range;
706
707 -- Working buffers for GCD computation, initially equal to the inputs
708 A : FZ(Width) := X; -- GCD will appear in A in the end
709 B : FZ(Width) := Y;
710
711 -- Evenness (negation of lowest bit) of A and B respectively
712 Ae, Be : WBool;
713
714 -- Common power-of-2 factor
715 Twos : Word := 0;
716
717 -- |A - B|
718 D : FZ(Width);
719
720 -- This flag is set iff A < B
721 A_lt_B : WBool;
722
723 begin
724
725 -- For convergence, requires number of shots equal to 2 * FZ_Bitness:
726 for i in 1 .. 2 * FZ_Bitness(X) loop
727
728 -- If A is even, A := A >> 1; otherwise A := A
729 Ae := 1 - FZ_OddP(A);
730 FZ_ShiftRight(A, A, WBit_Index(Ae));
731
732 -- If B is even, B := B >> 1; otherwise B := B
733 Be := 1 - FZ_OddP(B);
734 FZ_ShiftRight(B, B, WBit_Index(Be));
735
736 -- If both A and B were even, increment the common power-of-two
737 Twos := Twos + (Ae and Be);
738
739 -- D := |A - B|
740 FZ_Sub_Abs(X => A, Y => B, Difference => D, Underflow => A_lt_B);
741
742 -- B' := min(A, B)
743 FZ_Mux(X => B, Y => A, Result => B, Sel => A_lt_B);
744
745 -- A' := |A - B|
746 A := D;
747
748 end loop;
749
750 -- Reintroduce the common power-of-2 factor stored in 'Twos'
751 FZ_Quiet_ShiftLeft(N => A, ShiftedN => A, Count => Indices(Twos));
752
753 -- Output final result
754 Result := A;
755
756 end FZ_Greatest_Common_Divisor;
757
758 end FZ_GCD;