Permalink
Browse files

fix bug in number_of_bits for integer'high

  • Loading branch information...
1 parent 814a30d commit 294f821547c7405054f3adeb8c7d1e9cbb0ceaeb @martinjthompson committed Jan 16, 2013
Showing with 5 additions and 2 deletions.
  1. +5 −2 libv.vhd
View
@@ -120,10 +120,12 @@ package body libv is
function number_of_bits (
value : positive)
return positive is
- variable bits : positive := 1;
+ variable bits : natural := 0;
+ variable val : natural := value;
begin
- while 2**bits <= value loop
+ while val > 0 loop
bits := bits + 1;
+ val := val / 2;
end loop;
return bits;
end function number_of_bits;
@@ -251,6 +253,7 @@ begin -- architecture test
assert_equal("number_of_bits", number_of_bits(8), 4);
assert_equal("number_of_bits", number_of_bits(200), 8);
assert_equal("number_of_bits", number_of_bits(1200), 11);
+ assert_equal("number_of_bits", number_of_bits(integer'high), 31);
assert_equal("str(int)", str(0), "0");
assert_equal("str(int)", str(10), "10");

0 comments on commit 294f821

Please sign in to comment.