function log2(x : natural) return integer;
-------------------------------------------------------------------------------
-- Function log2 -- returns number of bits needed to encode x choices
-- x = 0 returns 0
-- x = 1 returns 0
-- x = 2 returns 1
-- x = 4 returns 2, etc.
-------------------------------------------------------------------------------
--
function log2(x : natural) return integer is
variable i : integer := 0;
variable val: integer := 1;
begin
if x = 0 then return 0;
else
for j in 0 to 29 loop -- for loop for XST
if val >= x then null;
else
i := i+1;
val := val*2;
end if;
end loop;
assert val >= x
report "Function log2 received argument larger than its capability of 2^30. "
severity failure;
return i;
end if;
end function log2;
--- find minimum number of bits required to
--- represent N as an unsigned binary number
---
function log2_ceil(N: natural) return positive is
begin
if N < 2 then
return 1;
else
return 1 + log2_ceil(N/2);
end if;
end;
function log2_ceil_x(N : integer) return integer is
begin
if (N <= 2) then
return 1;
else
if (N mod 2 = 0) then
return 1 + log2_ceil(N/2);
else
return 1 + log2_ceil((N+1)/2);
end if;
end if;
end function log2_ceil_x;
function log2_floor (x : positive) return natural is
begin
if x <= 1 then
return 0;
else
return log2_floor (x / 2) + 1;
end if;
end log2_floor;
function log2_ceil (x : positive) return natural is
begin
if x <= 1 then
return 0;
else
return log2_floor (x - 1) + 1;
end if;
end log2_ceil;
文章评论(0条评论)
登录后参与讨论