VHDL - native log2 and ceil for value 1

Status
Not open for further replies.

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,302
Hello,

Using the combination of log2 and ceil - one can determine the vector length to store n number of addresses.

Code:
number_of_bits := integer ( ceil ( log2 ( real ( number_of_addresses  ) ) ) ) ;

But this works well only when "number_of_addresses" is equal to or greater than 2.

For the number_of_addresses = 1 - The above fails and returns number_of_bits = 0.
Which is wrong - because to represent one address you need one bit...

Of course I can write a custom function to solve that, but for the sake of having fun - I decided to avoid that.
Do you have an I idea how to tweak the above to solve the case for the value: 1 ?
 

Why not this?

Code:
number_of_bits := integer ( ceil ( log2 ( real ( number_of_addresses  ) ) ) ) ;
if number_of_bits = 0 then -- or -- if number_of_addresses < 2 then
number_of_bits := 1; 
end if;
 

Because this implies a custom function.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…