ceil and log2 functions

Guest #3892761
Rate this post
• ▲ useful
▼ not useful
I have the following line of code:

constant DPRAM_DEPTH : integer := integer(ceil(log2(real(NUM_SLOTS))));

When NUM_SLOTS equals 0, I want my DPRAM_WIDTH to be 0, however log2 of 
0 is negative infinity so I get an invalid result. How can I obtain 0 
when NUM_SLOTS is 0, and still get the functionality I want out of that 
snippet of code?

Thanks
#3892878
Rate this post
• ▲ useful
▼ not useful
If you just need this to define a constant, you can do something like 
this:
1
function log2ceil(m : integer) return integer is
2
begin
3
  for i in 0 to integer'high loop
4
        if 2 ** i >= m then
5
            return i;
6
        end if;
7
    end loop;
8
end function log2ceil;
9

10
constant x   : integer := log2ceil(16);

This way, it will not use up any logic during synthesis.

Reply

Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.

Formatting
  • [c]C-Code[/c]
  • [vhdl]VHDL-Code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.