Finite Field Arithmetic

fz_shift.adb


   1 ------------------------------------------------------------------------------
   2 ------------------------------------------------------------------------------
   3 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'.               --
   4 --                                                                          --
   5 -- (C) 2019 Stanislav Datskovskiy ( www.loper-os.org )                      --
   6 -- http://wot.deedbot.org/17215D118B7239507FAFED98B98228A001ABFFC7.html     --
   7 --                                                                          --
   8 -- You do not have, nor can you ever acquire the right to use, copy or      --
   9 -- distribute this software ; Should you use this software for any purpose, --
  10 -- or copy and distribute it to anyone or in any manner, you are breaking   --
  11 -- the laws of whatever soi-disant jurisdiction, and you promise to         --
  12 -- continue doing so for the indefinite future. In any case, please         --
  13 -- always : read and understand any software ; verify any PGP signatures    --
  14 -- that you use - for any purpose.                                          --
  15 --                                                                          --
  16 -- See also http://trilema.com/2015/a-new-software-licensing-paradigm .     --
  17 ------------------------------------------------------------------------------
  18 ------------------------------------------------------------------------------
  19 
  20 with W_Shifts; use W_Shifts;
  21 
  22 
  23 package body FZ_Shift is
  24    
  25    --------------------------------------------------------------
  26    -- Shift Right
  27    --------------------------------------------------------------
  28    
  29    -- ShiftedN := N >> Count (with Overflow Input and Output)
  30    -- WARNING: OF_in MUST be of valid bit-width for the shift!
  31    procedure FZ_ShiftRight_O_I(N        : in  FZ;
  32                                ShiftedN : out FZ;
  33                                Count    : in  WBit_Index;
  34                                Overflow : out Word;
  35                                OF_in    : in  Word) is
  36       Ni    : Word;
  37       Carry : Word := OF_in;
  38    begin
  39       for i in reverse N'Range loop
  40          Ni := N(i);
  41          ShiftedN(i) := Shift_Right(Ni, Count) or Carry;
  42          Carry := Shift_Left(Ni, Bitness - Count);
  43       end loop;
  44       Overflow := Carry;
  45    end FZ_ShiftRight_O_I;
  46    
  47    
  48    -- ShiftedN := N >> Count (with Overflow Output only)
  49    procedure FZ_ShiftRight_O(N        : in  FZ;
  50                              ShiftedN : out FZ;
  51                              Count    : in  WBit_Index;
  52                              Overflow : out Word) is
  53    begin
  54       FZ_ShiftRight_O_I(N, ShiftedN, Count, Overflow, 0);
  55    end FZ_ShiftRight_O;
  56    
  57    
  58    -- ShiftedN := N >> Count (no Overflow output or input)
  59    procedure FZ_ShiftRight(N        : in  FZ;
  60                            ShiftedN : out FZ;
  61                            Count    : in  WBit_Index) is
  62       Overflow : Word;
  63       pragma Unreferenced(Overflow);
  64    begin
  65       FZ_ShiftRight_O_I(N, ShiftedN, Count, Overflow, 0);
  66    end FZ_ShiftRight;
  67    
  68    --------------------------------------------------------------
  69    -- Shift Left
  70    --------------------------------------------------------------
  71    
  72    -- ShiftedN := N << Count (with Overflow Input and Output)
  73    -- WARNING: OF_in MUST be of valid bit-width for the shift!
  74    procedure FZ_ShiftLeft_O_I(N        : in  FZ;
  75                               ShiftedN : out FZ;
  76                               Count    : in  WBit_Index;
  77                               Overflow : out Word;
  78                               OF_in    : in  Word) is
  79       Ni    : Word;
  80       Carry : Word := OF_in;
  81    begin
  82       for i in N'Range loop
  83          Ni := N(i);
  84          ShiftedN(i) := Shift_Left(Ni, Count) or Carry;
  85          Carry := Shift_Right(Ni, Bitness - Count);
  86       end loop;
  87       Overflow := Carry;
  88    end FZ_ShiftLeft_O_I;
  89    
  90    
  91    -- ShiftedN := N << Count (with Overflow Output only)
  92    procedure FZ_ShiftLeft_O(N        : in  FZ;
  93                             ShiftedN : out FZ;
  94                             Count    : in  WBit_Index;
  95                             Overflow : out Word) is
  96    begin
  97       FZ_ShiftLeft_O_I(N, ShiftedN, Count, Overflow, 0);
  98    end FZ_ShiftLeft_O;
  99    
 100    
 101    -- ShiftedN := N << Count (no Overflow output or input)
 102    procedure FZ_ShiftLeft(N        : in  FZ;
 103                           ShiftedN : out FZ;
 104                           Count    : in  WBit_Index) is
 105       Overflow : Word;
 106       pragma Unreferenced(Overflow);
 107    begin
 108       FZ_ShiftLeft_O_I(N, ShiftedN, Count, Overflow, 0);
 109    end FZ_ShiftLeft;
 110    
 111 end FZ_Shift;