raw
ffa_ch5_egypt.kv        1 ------------------------------------------------------------------------------
ffa_ch5_egypt.kv 2 ------------------------------------------------------------------------------
ffa_ch5_egypt.kv 3 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'. --
ffa_ch5_egypt.kv 4 -- --
ffa_ch15_gcd.kv 5 -- (C) 2019 Stanislav Datskovskiy ( www.loper-os.org ) --
ffa_ch5_egypt.kv 6 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html --
ffa_ch5_egypt.kv 7 -- --
ffa_ch5_egypt.kv 8 -- You do not have, nor can you ever acquire the right to use, copy or --
ffa_ch5_egypt.kv 9 -- distribute this software ; Should you use this software for any purpose, --
ffa_ch5_egypt.kv 10 -- or copy and distribute it to anyone or in any manner, you are breaking --
ffa_ch5_egypt.kv 11 -- the laws of whatever soi-disant jurisdiction, and you promise to --
ffa_ch5_egypt.kv 12 -- continue doing so for the indefinite future. In any case, please --
ffa_ch5_egypt.kv 13 -- always : read and understand any software ; verify any PGP signatures --
ffa_ch5_egypt.kv 14 -- that you use - for any purpose. --
ffa_ch5_egypt.kv 15 -- --
ffa_ch5_egypt.kv 16 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . --
ffa_ch5_egypt.kv 17 ------------------------------------------------------------------------------
ffa_ch5_egypt.kv 18 ------------------------------------------------------------------------------
ffa_ch5_egypt.kv 19
ffa_ch5_egypt.kv 20 with Words; use Words;
ffa_ch5_egypt.kv 21 with W_Pred; use W_Pred;
ffa_ch7_turbo_egy... 22 with W_Shifts; use W_Shifts;
ffa_ch5_egypt.kv 23 with FZ_Basic; use FZ_Basic;
ffa_ch5_egypt.kv 24 with FZ_Arith; use FZ_Arith;
ffa_ch5_egypt.kv 25 with FZ_BitOp; use FZ_BitOp;
ffa_ch5_egypt.kv 26 with FZ_Shift; use FZ_Shift;
ffa_ch5_egypt.kv 27
ffa_ch5_egypt.kv 28
ffa_ch5_egypt.kv 29 package body FZ_Divis is
ffa_ch5_egypt.kv 30
ffa_ch5_egypt.kv 31 -- Dividend is divided by Divisor, producing Quotient and Remainder.
ffa_ch5_egypt.kv 32 -- WARNING: NO div0 test here! Caller must test.
ffa_ch5_egypt.kv 33 procedure FZ_IDiv(Dividend : in FZ;
ffa_ch5_egypt.kv 34 Divisor : in FZ;
ffa_ch5_egypt.kv 35 Quotient : out FZ;
ffa_ch5_egypt.kv 36 Remainder : out FZ) is
ffa_ch5_egypt.kv 37
ffa_ch5_egypt.kv 38 -- The working register
ffa_ch5_egypt.kv 39 QR : FZ(1 .. Dividend'Length + Divisor'Length);
ffa_ch5_egypt.kv 40
ffa_ch5_egypt.kv 41 -- Bottom seg of Z will end up containing the Quotient; top - remainder
ffa_ch5_egypt.kv 42 Q : FZ renames QR(1 .. Dividend'Length); -- Quotient
ffa_ch5_egypt.kv 43 R : FZ renames QR(Dividend'Length + 1 .. QR'Last); -- Remainder
ffa_ch5_egypt.kv 44
ffa_ch5_egypt.kv 45 C : WBool := 0; -- Borrow, from comparator
ffa_ch5_egypt.kv 46 begin
ffa_ch5_egypt.kv 47 Q := Dividend; -- Q begins with the Dividend
ffa_ch5_egypt.kv 48 FZ_Clear(R); -- R begins empty
ffa_ch5_egypt.kv 49
ffa_ch5_egypt.kv 50 -- For each bit of Dividend:
ffa_ch5_egypt.kv 51 for i in 1 .. FZ_Bitness(Dividend) loop
ffa_ch5_egypt.kv 52
ffa_ch5_egypt.kv 53 -- Advance QR by 1 bit:
ffa_ch5_egypt.kv 54 FZ_ShiftLeft(QR, QR, 1);
ffa_ch5_egypt.kv 55
ffa_ch5_egypt.kv 56 -- Subtract Divisor from R; Underflow goes into C
ffa_ch5_egypt.kv 57 FZ_Sub(X => R, Y => Divisor, Difference => R, Underflow => C);
ffa_ch5_egypt.kv 58
ffa_ch5_egypt.kv 59 -- If C=1, subtraction underflowed, and then Divisor gets added back:
ffa_ch5_egypt.kv 60 FZ_Add_Gated(X => R, Y => Divisor, Gate => C, Sum => R);
ffa_ch5_egypt.kv 61
ffa_ch5_egypt.kv 62 -- Current result-bit is equal to Not-C, i.e. 1 if Divisor 'went in'
ffa_ch5_egypt.kv 63 FZ_Or_W(Q, W_Not(C));
ffa_ch5_egypt.kv 64
ffa_ch5_egypt.kv 65 end loop;
ffa_ch5_egypt.kv 66
ffa_ch5_egypt.kv 67 Quotient := Q; -- Output the Quotient.
ffa_ch5_egypt.kv 68 Remainder := R; -- Output the Remainder.
ffa_ch5_egypt.kv 69
ffa_ch5_egypt.kv 70 end FZ_IDiv;
ffa_ch5_egypt.kv 71
ffa_ch19_peh_tuni... 72
ffa_ch5_egypt.kv 73 -- Exactly same thing as IDiv, but keep only the Quotient
ffa_ch5_egypt.kv 74 procedure FZ_Div(Dividend : in FZ;
ffa_ch5_egypt.kv 75 Divisor : in FZ;
ffa_ch5_egypt.kv 76 Quotient : out FZ) is
ffa_ch5_egypt.kv 77 Remainder : FZ(Divisor'Range);
ffa_ch5_egypt.kv 78 pragma Unreferenced(Remainder);
ffa_ch5_egypt.kv 79 begin
ffa_ch5_egypt.kv 80 FZ_IDiv(Dividend, Divisor, Quotient, Remainder);
ffa_ch5_egypt.kv 81 end FZ_Div;
ffa_ch5_egypt.kv 82
ffa_ch19_peh_tuni... 83
ffa_ch7_turbo_egy... 84 -- Modulus. Permits the asymmetric Dividend and Divisor in FZ_Mod_Exp.
ffa_ch5_egypt.kv 85 procedure FZ_Mod(Dividend : in FZ;
ffa_ch5_egypt.kv 86 Divisor : in FZ;
ffa_ch5_egypt.kv 87 Remainder : out FZ) is
ffa_ch7_turbo_egy... 88
ffa_ch7_turbo_egy... 89 -- Length of Divisor and Remainder; <= Dividend'Length
ffa_ch7_turbo_egy... 90 L : constant Indices := Divisor'Length;
ffa_ch7_turbo_egy... 91
ffa_ch7_turbo_egy... 92 -- Remainder register, starts as zero
ffa_ch7_turbo_egy... 93 R : FZ(1 .. L) := (others => 0);
ffa_ch7_turbo_egy... 94
ffa_ch7_turbo_egy... 95 -- Indices into the words of Dividend
ffa_ch7_turbo_egy... 96 subtype Dividend_Index is Word_Index range Dividend'Range;
ffa_ch7_turbo_egy... 97
ffa_ch7_turbo_egy... 98 -- Permissible 'cuts' for the Slice operation
ffa_ch7_turbo_egy... 99 subtype Divisor_Cuts is Word_Index range 2 .. Divisor'Length;
ffa_ch7_turbo_egy... 100
ffa_ch7_turbo_egy... 101 -- Performs Restoring Division on a given segment of Dividend:Divisor
ffa_ch7_turbo_egy... 102 procedure Slice(Index : Dividend_Index;
ffa_ch7_turbo_egy... 103 Cut : Divisor_Cuts) is
ffa_ch19_peh_tuni... 104
ffa_ch19_peh_tuni... 105 -- Borrow, from comparator
ffa_ch19_peh_tuni... 106 C : WBool;
ffa_ch19_peh_tuni... 107
ffa_ch19_peh_tuni... 108 -- Left-Shift Overflow
ffa_ch19_peh_tuni... 109 LsO : WBool;
ffa_ch19_peh_tuni... 110
ffa_ch19_peh_tuni... 111 -- Current cut of Remainder register
ffa_ch19_peh_tuni... 112 Rs : FZ renames R(1 .. Cut);
ffa_ch19_peh_tuni... 113
ffa_ch19_peh_tuni... 114 -- Current cut of Divisor
ffa_ch19_peh_tuni... 115 Ds : FZ renames Divisor(1 .. Cut);
ffa_ch19_peh_tuni... 116
ffa_ch19_peh_tuni... 117 -- Current word of Dividend, starting from the highest
ffa_ch19_peh_tuni... 118 W : Word := Dividend(Dividend'Last + 1 - Index);
ffa_ch19_peh_tuni... 119
ffa_ch7_turbo_egy... 120 begin
ffa_ch7_turbo_egy... 121
ffa_ch19_peh_tuni... 122 -- For each bit in the current Dividend word:
ffa_ch19_peh_tuni... 123 for b in 1 .. Bitness loop
ffa_ch7_turbo_egy... 124
ffa_ch19_peh_tuni... 125 -- Send top bit of current Dividend word to the bottom of W
ffa_ch19_peh_tuni... 126 W := Rotate_Left(W, 1);
ffa_ch7_turbo_egy... 127
ffa_ch19_peh_tuni... 128 -- Advance Rs, shifting in the current Dividend bit
ffa_ch19_peh_tuni... 129 FZ_ShiftLeft_O_I(N => Rs, ShiftedN => Rs, Count => 1,
ffa_ch19_peh_tuni... 130 OF_In => W and 1,
ffa_ch19_peh_tuni... 131 Overflow => LsO);
ffa_ch7_turbo_egy... 132
ffa_ch19_peh_tuni... 133 -- Subtract Divisor-Cut from R-Cut; Underflow goes into C
ffa_ch19_peh_tuni... 134 FZ_Sub(X => Rs, Y => Ds, Difference => Rs, Underflow => C);
ffa_ch7_turbo_egy... 135
ffa_ch19_peh_tuni... 136 -- If C=1, subtraction underflowed, and we must undo it:
ffa_ch19_peh_tuni... 137 FZ_Add_Gated(X => Rs, Y => Ds, Sum => Rs,
ffa_ch19_peh_tuni... 138 Gate => C and W_Not(LsO));
ffa_ch7_turbo_egy... 139
ffa_ch19_peh_tuni... 140 end loop;
ffa_ch7_turbo_egy... 141
ffa_ch7_turbo_egy... 142 end Slice;
ffa_ch7_turbo_egy... 143
ffa_ch5_egypt.kv 144 begin
ffa_ch7_turbo_egy... 145
ffa_ch7_turbo_egy... 146 -- Process bottom half of dividend:
ffa_ch7_turbo_egy... 147 for i in 1 .. L - 1 loop
ffa_ch7_turbo_egy... 148
ffa_ch7_turbo_egy... 149 Slice(i, i + 1); -- stay ahead by a word to handle carry
ffa_ch7_turbo_egy... 150
ffa_ch7_turbo_egy... 151 end loop;
ffa_ch7_turbo_egy... 152
ffa_ch7_turbo_egy... 153 -- Process top half of dividend
ffa_ch7_turbo_egy... 154 for i in L .. Dividend'Length loop
ffa_ch7_turbo_egy... 155
ffa_ch7_turbo_egy... 156 Slice(i, L);
ffa_ch7_turbo_egy... 157
ffa_ch7_turbo_egy... 158 end loop;
ffa_ch7_turbo_egy... 159
ffa_ch7_turbo_egy... 160 -- Output the Remainder.
ffa_ch7_turbo_egy... 161 Remainder := R;
ffa_ch7_turbo_egy... 162
ffa_ch5_egypt.kv 163 end FZ_Mod;
ffa_ch7_turbo_egy... 164
ffa_ch5_egypt.kv 165 end FZ_Divis;