-----------------------------------------------------------------------
-- $Header: /tmp/cvsroot/VHDL/lib/misc/utils_pkg.vhd,v 1.2 2009-01-02 15:28:01 Jens Exp $
-----------------------------------------------------------------------

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
use IEEE.MATH_REAL.ALL;

package utils_pkg is
	
	-- Constants

	-- Functions
	function NextPowerOfTwo(x : real) return real;
	function NextExpBaseTwo(x : real) return real;
	function NextPowerOfTwo(x : natural) return natural;
	function NextExpBaseTwo(x : natural) return natural;
	function GCD(a, b : natural) return natural;
	function LCM(a, b : natural) return natural;
	
end utils_pkg;


package body utils_pkg is

	function NextExpBaseTwo(x : real) return real is
	begin
		return ceil(log2(x));
	end NextExpBaseTwo;	
	
	function NextPowerOfTwo(x : real) return real is
	begin

		return 2.0**NextExpBaseTwo(x);
	end NextPowerOfTwo;	

	function NextExpBaseTwo(x : natural) return natural is
	begin
		return natural(NextExpBaseTwo(real(x)));
	end NextExpBaseTwo;	
	
	function NextPowerOfTwo(x : natural) return natural is
	begin

		return 2**NextExpBaseTwo(x);
	end NextPowerOfTwo;	
	
	function GCD(a, b : natural) return natural is
	variable aa : natural;
	variable bb : natural;
	begin
		aa := a;
		bb := b;
		while bb /= 0 loop
			if aa > bb then
				aa := aa - bb;
			else
				bb := bb - aa;
			end if;
		end loop;
		
		return aa;

	end GCD;	

	function LCM(a, b : natural) return natural is
	begin

		return (a * b)/GCD(a, b);

	end LCM;	

end utils_pkg;


