I\'m trying to create a reusable barrel shifter; it takes an input array of bits and shifts them a certain number of positions (determined by another input). I want to parameter
When I was using the method mentioned by Khan, I encountered rounding errors. So I wrote my own versions, that are immune to rounding errors and can, in principle handle more than 32 bit. You can substitute the type of L with any Type that has a logical shift left operator.
Most of the time you want to use log2ceil which is the amount of bits required to store the given number, while log2floor can be more described as the highest bit set.
In most cases those functions are fine for synthesis as they are used for generating constants. So no hardware is inferred for them.
function log2ceil (L: POSITIVE) return NATURAL is
variable i, bitCount : natural;
begin
i := L-1;
bitCount:=0;
while (i > 0) loop
bitCount := bitCount + 1;
i:=srlInteger(i,1);
end loop;
return bitCount;
end log2ceil;
function log2floor (L: POSITIVE) return NATURAL is
variable i, bitCount : natural;
begin
i := L;
bitCount:=0;
while (i > 1) loop
bitCount := bitCount + 1;
i:=srlInteger(i,1);
end loop;
return bitCount;
end log2floor;
function srlInteger(arg: integer; s:natural) return integer is
begin
return to_integer(SHIFT_RIGHT(to_UNSIGNED(ARG,32), s));
end srlInteger;