Skip to content

log2ceil Implementation fails for 31 bit values #41

Description

@maleiter

The current log2ceil implementation fails in the range of (2**30 + 1 to positive'high) since the internal tmp variable is multiplied by 2 before comparison.

My testbench:

library ieee;
use ieee.std_logic_1164.all;

entity log2ceil_test is
end entity;

architecture tb of log2ceil_test is

  function log2ceil(arg : positive) return natural is
    variable tmp          : positive := 2;
    variable log          : natural  := 1;
  begin
    if arg = 1 then
      return 0;
    end if;

    while arg > tmp loop
      tmp := tmp * 2;
      log := log + 1;
    end loop;
    return log;
  end function;
  
begin
  stim : process

  begin
    report "2**30-1";
    assert log2ceil(2 ** 30 - 1) = 30 ;

    report "2**30";
    assert log2ceil(2 ** 30) = 30 ;

    report "2**30+1";
    assert log2ceil(2 ** 30 + 1) = 31 ;

    report "2**31-1";
    assert log2ceil(positive'high) = 31 ;
    wait;
  end process;
end architecture;

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions