Forum: FPGA, VHDL & Verilog ceil and log2 functions

von Matt (Guest)

Rate this post
0 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?


von Markus F. (mfro)

Rate this post
0 useful
not useful
If you just need this to define a constant, you can do something like 
function log2ceil(m : integer) return integer is
  for i in 0 to integer'high loop
        if 2 ** i >= m then
            return i;
        end if;
    end loop;
end function log2ceil;

constant x   : integer := log2ceil(16);

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


Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [vhdl]VHDL code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.