File : fz_shift.adb


   1 ------------------------------------------------------------------------------
   2 ------------------------------------------------------------------------------
   3 -- This file is part of 'Finite Field Arithmetic', aka 'FFA'.               --
   4 --                                                                          --
   5 -- (C) 2017 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    procedure FZ_ShiftRight_O_I(N        : in  FZ;
  31                                ShiftedN : out FZ;
  32                                Count    : in  WBit_Index;
  33                                Overflow : out Word;
  34                                OF_in    : in  Word) is
  35       Ni    : Word;
  36       Carry : Word := OF_in;
  37    begin
  38       for i in reverse N'Range loop
  39          Ni := N(i);
  40          ShiftedN(i) := Shift_Right(Ni, Count) or Carry;
  41          Carry := Shift_Left(Ni, Bitness - Count);
  42       end loop;
  43       Overflow := Carry;
  44    end FZ_ShiftRight_O_I;
  45    
  46    
  47    -- ShiftedN := N >> Count (with Overflow Output only)
  48    procedure FZ_ShiftRight_O(N        : in  FZ;
  49                              ShiftedN : out FZ;
  50                              Count    : in  WBit_Index;
  51                              Overflow : out Word) is
  52    begin
  53       FZ_ShiftRight_O_I(N, ShiftedN, Count, Overflow, 0);
  54    end FZ_ShiftRight_O;
  55    
  56    
  57    -- ShiftedN := N >> Count (no Overflow output or input)
  58    procedure FZ_ShiftRight(N        : in  FZ;
  59                            ShiftedN : out FZ;
  60                            Count    : in  WBit_Index) is
  61       Overflow : Word;
  62       pragma Unreferenced(Overflow);
  63    begin
  64       FZ_ShiftRight_O_I(N, ShiftedN, Count, Overflow, 0);
  65    end FZ_ShiftRight;
  66    
  67    --------------------------------------------------------------
  68    -- Shift Left
  69    --------------------------------------------------------------
  70    
  71    -- ShiftedN := N << Count (with Overflow Input and Output)
  72    procedure FZ_ShiftLeft_O_I(N        : in  FZ;
  73                               ShiftedN : out FZ;
  74                               Count    : in  WBit_Index;
  75                               Overflow : out Word;
  76                               OF_in    : in  Word) is
  77       Ni    : Word;
  78       Carry : Word := OF_in;
  79    begin
  80       for i in N'Range loop
  81          Ni := N(i);
  82          ShiftedN(i) := Shift_Left(Ni, Count) or Carry;
  83          Carry := Shift_Right(Ni, Bitness - Count);
  84       end loop;
  85       Overflow := Carry;
  86    end FZ_ShiftLeft_O_I;
  87    
  88    
  89    -- ShiftedN := N << Count (with Overflow Output only)
  90    procedure FZ_ShiftLeft_O(N        : in  FZ;
  91                             ShiftedN : out FZ;
  92                             Count    : in  WBit_Index;
  93                             Overflow : out Word) is
  94    begin
  95       FZ_ShiftLeft_O_I(N, ShiftedN, Count, Overflow, 0);
  96    end FZ_ShiftLeft_O;
  97    
  98    
  99    -- ShiftedN := N << Count (no Overflow output or input)
 100    procedure FZ_ShiftLeft(N        : in  FZ;
 101                           ShiftedN : out FZ;
 102                           Count    : in  WBit_Index) is
 103       Overflow : Word;
 104       pragma Unreferenced(Overflow);
 105    begin
 106       FZ_ShiftLeft_O_I(N, ShiftedN, Count, Overflow, 0);
 107    end FZ_ShiftLeft;
 108    
 109 end FZ_Shift;