sizing

extras/sizing.vhdl

Dependencies

None

Description

This package provides functions used to compute integer approximations of logarithms. The primary use of these functions is to determine the size of arrays using the bit_size() and encoding_size() functions. When put to maximal use it is possible to create designs that eliminate hardcoded ranges and automatically resize their signals and variables by changing a few key constants or generics.

These functions can be used in most synthesizers to compute ranges for arrays. The core functionality is provided in the ceil_log() and floor_log() subprograms. These compute the logarithm in any integer base. For convenenience, base-2 functions are also provided along with the array sizing functions. See the bcd_conversion implementation of decimal_size() for a practical example of computing integer logs in base-10 using this package.

Example usage

constant MAX_COUNT  : natural := 1000;
constant COUNT_SIZE : natural := bit_size(MAX_COUNT);
signal counter : unsigned(COUNT_SIZE-1 downto 0);
...
counter <= to_unsigned(MAX_COUNT, COUNT_SIZE);
-- counter will resize itself as MAX_COUNT is changed

Subprograms

sizing.floor_log (n : positive; b : positive) → natural
Compute the integer result of the function floor(log(n)).
Parameters:
  • n (positive) – Number to take logarithm of
  • b (positive) – Base for the logarithm
Returns:

Approximate logarithm of n rounded down.

Example:

size := floor_log(20, 2);
sizing.ceil_log (n : positive; b : positive) → natural
Compute the integer result of the function ceil(log(n)) where b is the base.
Parameters:
  • n (positive) – Number to take logarithm of
  • b (positive) – Base for the logarithm
Returns:

Approximate logarithm of n rounded up.

Example:

size := ceil_log(20, 2);
sizing.floor_log2 (n : positive) → natural
Compute the integer result of the function floor(log2(n)).
Parameters:
  • n (positive) – Number to take logarithm of
Returns:

Approximate base-2 logarithm of n rounded down.

sizing.ceil_log2 (n : positive) → natural
Compute the integer result of the function ceil(log2(n)).
Parameters:
  • n (positive) – Number to take logarithm of
Returns:

Approximate base-2 logarithm of n rounded up.

sizing.bit_size (n : natural) → natural
Compute the total number of bits needed to represent a number in binary.
Parameters:
  • n (natural) – Number to compute size from
Returns:

Number of bits.

sizing.encoding_size (n : positive) → natural
Compute the number of bits needed to encode n items.
Parameters:
  • n (positive) – Number to compute size from
Returns:

Number of bits.

sizing.signed_size (n : integer) → natural
Compute the total number of bits to represent a 2’s complement signed integer in binary.
Parameters:
  • n (integer) – Number to compute size from
Returns:

Number of bits.