ffa_ch1_genesis.kv 1 ------------------------------------------------------------------------------
ffa_ch1_genesis.kv 2 ------------------------------------------------------------------------------
ffa_ch1_genesis.kv 3 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'. --
ffa_ch1_genesis.kv 4 -- --
ffa_ch15_gcd.kv 5 -- (C) 2019 Stanislav Datskovskiy ( www.loper-os.org ) --
ffa_ch1_genesis.kv 6 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html --
ffa_ch1_genesis.kv 7 -- --
ffa_ch1_genesis.kv 8 -- You do not have, nor can you ever acquire the right to use, copy or --
ffa_ch1_genesis.kv 9 -- distribute this software ; Should you use this software for any purpose, --
ffa_ch1_genesis.kv 10 -- or copy and distribute it to anyone or in any manner, you are breaking --
ffa_ch1_genesis.kv 11 -- the laws of whatever soi-disant jurisdiction, and you promise to --
ffa_ch1_genesis.kv 12 -- continue doing so for the indefinite future. In any case, please --
ffa_ch1_genesis.kv 13 -- always : read and understand any software ; verify any PGP signatures --
ffa_ch1_genesis.kv 14 -- that you use - for any purpose. --
ffa_ch1_genesis.kv 15 -- --
ffa_ch1_genesis.kv 16 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm . --
ffa_ch1_genesis.kv 17 ------------------------------------------------------------------------------
ffa_ch1_genesis.kv 18 ------------------------------------------------------------------------------
ffa_ch1_genesis.kv 19
ffa_ch1_genesis.kv 20 with W_Shifts; use W_Shifts;
ffa_ch1_genesis.kv 21
ffa_ch1_genesis.kv 22 -- Fundamental Operations on Words:
ffa_ch1_genesis.kv 23 package body Word_Ops is
ffa_ch1_genesis.kv 24
ffa_ch1_genesis.kv 25 -- Ada (like C) does not (portably) tell us over/underflow from arithmetic.
ffa_ch1_genesis.kv 26 -- And there existed in the past, and apparently exist today, CPUs made
ffa_ch1_genesis.kv 27 -- by idiots and wreckers (e.g. 'RISC-V') that do not have flags at all!
ffa_ch1_genesis.kv 28
ffa_ch1_genesis.kv 29 -- However, for multi-word addition, subtraction, the inner loop of
ffa_ch1_genesis.kv 30 -- Comba's multiplication, and for a handful of other ops, we need it!
ffa_ch1_genesis.kv 31
ffa_ch1_genesis.kv 32 -- So we derive the Carry or Borrow at the 'eldest' binary position.
ffa_ch1_genesis.kv 33 -- See the elementary proof (base case: 1 bit) further below:
ffa_ch1_genesis.kv 34
ffa_ch1_genesis.kv 35 -- Find the Carry, from an addition where it is known that A + B == S:
ffa_ch1_genesis.kv 36 function W_Carry(A : in Word; B : in Word; S : in Word)
ffa_ch1_genesis.kv 37 return WBool is
ffa_ch1_genesis.kv 38 begin
ffa_ch1_genesis.kv 39 return WBool(Shift_Right((A and B) or ((A or B) and (not S)),
ffa_ch1_genesis.kv 40 Bitness - 1));
ffa_ch1_genesis.kv 41 end W_Carry;
ffa_ch1_genesis.kv 42
ffa_ch1_genesis.kv 43
ffa_ch1_genesis.kv 44 -- Find the Borrow, from a subtraction where it is known that A - B == D:
ffa_ch1_genesis.kv 45 function W_Borrow(A : in Word; B : in Word; D : in Word)
ffa_ch1_genesis.kv 46 return WBool is
ffa_ch1_genesis.kv 47 begin
ffa_w_borrow_expr.kv 48 return WBool(Shift_Right((B and D) or ((B or D) and (not A)),
ffa_ch1_genesis.kv 49 Bitness - 1));
ffa_ch1_genesis.kv 50 end W_Borrow;
ffa_ch1_genesis.kv 51
ffa_ch1_genesis.kv 52 -- A+B+C is the output bit of 1-bit adder; C is carry-in;
ffa_ch1_genesis.kv 53 -- A-B-C is the output bit of 1-bit subber; C is borrow-in.
ffa_ch1_genesis.kv 54 -- Observe that A+B+C is equal to A-B-C for all A,B,C !
ffa_ch1_genesis.kv 55 -- +-+-+-+-----+--------------+-----+----------------+
ffa_ch1_genesis.kv 56 -- | | 'Carry' out: | | 'Borrow' out: |
ffa_ch1_genesis.kv 57 -- +-+-+-+-----+--------------+-----+----------------+
ffa_ch13_measure_... 58 -- | | | | |(a and b) or | |(b and (A-B-C)) |
ffa_ch13_measure_... 59 -- |A|B|C|A+B+C| ((a or b) and|A-B-C| or |
ffa_ch13_measure_... 60 -- | | | | | ~(A+B+C)) | |((b or (A-B-C)) |
ffa_ch13_measure_... 61 -- | | | | | | | and ~a) |
ffa_ch1_genesis.kv 62 -- +-+-+-+-----+--------------+-----+----------------+
ffa_ch1_genesis.kv 63 -- |0|0|0| 0 | 0 | 0 | 0 |
ffa_ch1_genesis.kv 64 -- +-+-+-+-----+--------------+-----+----------------+
ffa_ch1_genesis.kv 65 -- |1|0|0| 1 | 0 | 1 | 0 |
ffa_ch1_genesis.kv 66 -- +-+-+-+-----+--------------+-----+----------------+
ffa_ch1_genesis.kv 67 -- |0|1|0| 1 | 0 | 1 | 1 |
ffa_ch1_genesis.kv 68 -- +-+-+-+-----+--------------+-----+----------------+
ffa_ch1_genesis.kv 69 -- |1|1|0| 0 | 1 | 0 | 0 |
ffa_ch1_genesis.kv 70 -- +-+-+-+-----+--------------+-----+----------------+
ffa_ch1_genesis.kv 71 -- |0|0|1| 1 | 0 | 1 | 1 |
ffa_ch1_genesis.kv 72 -- +-+-+-+-----+--------------+-----+----------------+
ffa_ch1_genesis.kv 73 -- |1|0|1| 0 | 1 | 0 | 0 |
ffa_ch1_genesis.kv 74 -- +-+-+-+-----+--------------+-----+----------------+
ffa_ch1_genesis.kv 75 -- |0|1|1| 0 | 1 | 0 | 1 |
ffa_ch1_genesis.kv 76 -- +-+-+-+-----+--------------+-----+----------------+
ffa_ch1_genesis.kv 77 -- |1|1|1| 1 | 1 | 1 | 1 |
ffa_ch1_genesis.kv 78 -- +-+-+-+-----+--------------+-----+----------------+
ffa_ch1_genesis.kv 79 -- This base case extends to any N bit register, since
ffa_ch1_genesis.kv 80 -- both equations depend ~strictly~ on A, B, and C.
ffa_ch1_genesis.kv 81
ffa_ch1_genesis.kv 82 -- Without any branching: if Sel == 0, return A; if Sel == 1, return B.
ffa_ch1_genesis.kv 83 function W_Mux(A : in Word; B : in Word; Sel : in WBool) return Word is
ffa_ch1_genesis.kv 84 begin
ffa_ch1_genesis.kv 85 return B xor ((Sel - 1) and (A xor B));
ffa_ch1_genesis.kv 86 end W_Mux;
ffa_ch1_genesis.kv 87
ffa_ch1_genesis.kv 88 end Word_Ops;