uart_rx: ein prozessmodell. spart weitere 3 logic elements :P
[hwmod.git] / src / math_pkg.vhd
1 -------------------------------------------------------------------------\r
2 --\r
3 -- Filename: math_pkg.vhd\r
4 -- =========\r
5 --\r
6 -- Short Description:\r
7 -- ==================\r
8 --   Utility Package defining often used mathematical functions\r
9 --\r
10 -------------------------------------------------------------------------\r
11 \r
12 package math_pkg is\r
13   -- Calculates the logarithm dualis of the operand and rounds up\r
14   -- the result to the next integer value.
15   function log2c(constant value : in integer) return integer;\r
16   -- Returns the maximum of the two operands
17   function max(constant value1, value2 : in integer) return integer;\r
18   -- Returns the maximum of the three operands
19   function max(constant value1, value2, value3 : in integer) return integer;
20 end math_pkg;
21
22 package body math_pkg is
23   function log2c(constant value : in integer) return integer is
24     variable ret_value : integer;
25     variable cur_value : integer;
26   begin
27     ret_value := 0;
28     cur_value := 1;
29     
30     while cur_value < value loop
31       ret_value := ret_value + 1;
32       cur_value := cur_value * 2;
33     end loop;
34     return ret_value;
35   end function log2c;
36   
37   function max(constant value1, value2 : in integer) return integer is
38     variable ret_value : integer;
39   begin
40     if value1 > value2 then
41       ret_value := value1;
42     else
43       ret_value := value2;
44     end if;
45     return ret_value;
46   end function max;
47   
48   function max(constant value1, value2, value3 : in integer) return integer is
49   begin
50     return max(max(value1, value2), value3);
51   end function max;
52  end package body math_pkg;
53