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_ch5_egypt.kv 5 -- (C) 2017 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_ch5_egypt.kv 22 with FZ_Basic; use FZ_Basic;
ffa_ch5_egypt.kv 23 with FZ_Arith; use FZ_Arith;
ffa_ch5_egypt.kv 24 with FZ_BitOp; use FZ_BitOp;
ffa_ch5_egypt.kv 25 with FZ_Shift; use FZ_Shift;
ffa_ch5_egypt.kv 26
ffa_ch5_egypt.kv 27
ffa_ch5_egypt.kv 28 package body FZ_Divis is
ffa_ch5_egypt.kv 29
ffa_ch5_egypt.kv 30 -- Dividend is divided by Divisor, producing Quotient and Remainder.
ffa_ch5_egypt.kv 31 -- WARNING: NO div0 test here! Caller must test.
ffa_ch5_egypt.kv 32 procedure FZ_IDiv(Dividend : in FZ;
ffa_ch5_egypt.kv 33 Divisor : in FZ;
ffa_ch5_egypt.kv 34 Quotient : out FZ;
ffa_ch5_egypt.kv 35 Remainder : out FZ) is
ffa_ch5_egypt.kv 36
ffa_ch5_egypt.kv 37 -- The working register
ffa_ch5_egypt.kv 38 QR : FZ(1 .. Dividend'Length + Divisor'Length);
ffa_ch5_egypt.kv 39
ffa_ch5_egypt.kv 40 -- Bottom seg of Z will end up containing the Quotient; top - remainder
ffa_ch5_egypt.kv 41 Q : FZ renames QR(1 .. Dividend'Length); -- Quotient
ffa_ch5_egypt.kv 42 R : FZ renames QR(Dividend'Length + 1 .. QR'Last); -- Remainder
ffa_ch5_egypt.kv 43
ffa_ch5_egypt.kv 44 C : WBool := 0; -- Borrow, from comparator
ffa_ch5_egypt.kv 45 begin
ffa_ch5_egypt.kv 46 Q := Dividend; -- Q begins with the Dividend
ffa_ch5_egypt.kv 47 FZ_Clear(R); -- R begins empty
ffa_ch5_egypt.kv 48
ffa_ch5_egypt.kv 49 -- For each bit of Dividend:
ffa_ch5_egypt.kv 50 for i in 1 .. FZ_Bitness(Dividend) loop
ffa_ch5_egypt.kv 51
ffa_ch5_egypt.kv 52 -- Advance QR by 1 bit:
ffa_ch5_egypt.kv 53 FZ_ShiftLeft(QR, QR, 1);
ffa_ch5_egypt.kv 54
ffa_ch5_egypt.kv 55 -- Subtract Divisor from R; Underflow goes into C
ffa_ch5_egypt.kv 56 FZ_Sub(X => R, Y => Divisor, Difference => R, Underflow => C);
ffa_ch5_egypt.kv 57
ffa_ch5_egypt.kv 58 -- If C=1, subtraction underflowed, and then Divisor gets added back:
ffa_ch5_egypt.kv 59 FZ_Add_Gated(X => R, Y => Divisor, Gate => C, Sum => R);
ffa_ch5_egypt.kv 60
ffa_ch5_egypt.kv 61 -- Current result-bit is equal to Not-C, i.e. 1 if Divisor 'went in'
ffa_ch5_egypt.kv 62 FZ_Or_W(Q, W_Not(C));
ffa_ch5_egypt.kv 63
ffa_ch5_egypt.kv 64 end loop;
ffa_ch5_egypt.kv 65
ffa_ch5_egypt.kv 66 Quotient := Q; -- Output the Quotient.
ffa_ch5_egypt.kv 67 Remainder := R; -- Output the Remainder.
ffa_ch5_egypt.kv 68
ffa_ch5_egypt.kv 69 end FZ_IDiv;
ffa_ch5_egypt.kv 70 pragma Inline_Always(FZ_IDiv);
ffa_ch5_egypt.kv 71
ffa_ch5_egypt.kv 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 pragma Inline_Always(FZ_Div);
ffa_ch5_egypt.kv 83
ffa_ch5_egypt.kv 84
ffa_ch5_egypt.kv 85 -- Exactly same thing as IDiv, but keep only the Remainder
ffa_ch5_egypt.kv 86 procedure FZ_Mod(Dividend : in FZ;
ffa_ch5_egypt.kv 87 Divisor : in FZ;
ffa_ch5_egypt.kv 88 Remainder : out FZ) is
ffa_ch5_egypt.kv 89 Quotient : FZ(Dividend'Range);
ffa_ch5_egypt.kv 90 pragma Unreferenced(Quotient);
ffa_ch5_egypt.kv 91 begin
ffa_ch5_egypt.kv 92 FZ_IDiv(Dividend, Divisor, Quotient, Remainder);
ffa_ch5_egypt.kv 93 end FZ_Mod;
ffa_ch5_egypt.kv 94 pragma Inline_Always(FZ_Mod);
ffa_ch5_egypt.kv 95
ffa_ch5_egypt.kv 96 end FZ_Divis;