1 |
2 |
davidklun |
---------------------------------------------------------------------
|
2 |
|
|
---- ----
|
3 |
|
|
---- FPU ----
|
4 |
|
|
---- Floating Point Unit (Double precision) ----
|
5 |
|
|
---- ----
|
6 |
|
|
---- Author: David Lundgren ----
|
7 |
|
|
---- davidklun@gmail.com ----
|
8 |
|
|
---- ----
|
9 |
|
|
---------------------------------------------------------------------
|
10 |
|
|
---- ----
|
11 |
|
|
---- Copyright (C) 2009 David Lundgren ----
|
12 |
|
|
---- davidklun@gmail.com ----
|
13 |
|
|
---- ----
|
14 |
|
|
---- This source file may be used and distributed without ----
|
15 |
|
|
---- restriction provided that this copyright statement is not ----
|
16 |
|
|
---- removed from the file and that any derivative work contains ----
|
17 |
|
|
---- the original copyright notice and the associated disclaimer.----
|
18 |
|
|
---- ----
|
19 |
|
|
---- THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY ----
|
20 |
|
|
---- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED ----
|
21 |
|
|
---- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ----
|
22 |
|
|
---- FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR ----
|
23 |
|
|
---- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, ----
|
24 |
|
|
---- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ----
|
25 |
|
|
---- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ----
|
26 |
|
|
---- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR ----
|
27 |
|
|
---- BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ----
|
28 |
|
|
---- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ----
|
29 |
|
|
---- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ----
|
30 |
|
|
---- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ----
|
31 |
|
|
---- POSSIBILITY OF SUCH DAMAGE. ----
|
32 |
|
|
---- ----
|
33 |
|
|
---------------------------------------------------------------------
|
34 |
|
|
|
35 |
|
|
LIBRARY ieee;
|
36 |
|
|
USE ieee.std_logic_1164.all;
|
37 |
|
|
USE ieee.std_logic_arith.all;
|
38 |
|
|
use ieee.std_logic_unsigned.all;
|
39 |
|
|
use ieee.std_logic_misc.all;
|
40 |
|
|
|
41 |
|
|
|
42 |
|
|
ENTITY fpu_round IS
|
43 |
|
|
|
44 |
|
|
PORT(
|
45 |
|
|
clk, rst, enable : IN std_logic;
|
46 |
|
|
round_mode : IN std_logic_vector (1 DOWNTO 0);
|
47 |
|
|
sign_term : IN std_logic;
|
48 |
|
|
mantissa_term : IN std_logic_vector (55 DOWNTO 0);
|
49 |
|
|
exponent_term : IN std_logic_vector (11 DOWNTO 0);
|
50 |
|
|
round_out : OUT std_logic_vector (63 DOWNTO 0);
|
51 |
|
|
exponent_final : OUT std_logic_vector (11 DOWNTO 0)
|
52 |
|
|
);
|
53 |
|
|
|
54 |
|
|
END fpu_round;
|
55 |
|
|
|
56 |
|
|
|
57 |
|
|
architecture rtl of fpu_round is
|
58 |
|
|
|
59 |
|
|
signal rounding_amount : std_logic_vector(55 downto 0);
|
60 |
|
|
signal round_nearest : std_logic;
|
61 |
|
|
signal round_to_zero : std_logic;
|
62 |
|
|
signal round_to_pos_inf : std_logic;
|
63 |
|
|
signal round_to_neg_inf : std_logic;
|
64 |
|
|
signal round_nearest_trigger : std_logic;
|
65 |
|
|
signal round_to_pos_inf_trigger : std_logic;
|
66 |
|
|
signal round_to_neg_inf_trigger : std_logic;
|
67 |
|
|
signal round_trigger : std_logic;
|
68 |
|
|
signal sum_round : std_logic_vector(55 downto 0);
|
69 |
|
|
signal sum_round_overflow : std_logic;
|
70 |
|
|
-- will be 0 if no carry, 1 if overflow from the rounding unit
|
71 |
|
|
-- overflow from rounding is extremely rare, but possible
|
72 |
|
|
signal sum_round_2 : std_logic_vector(55 downto 0);
|
73 |
|
|
signal exponent_round : std_logic_vector(11 downto 0);
|
74 |
|
|
signal exponent_final_2 : std_logic_vector(11 downto 0);
|
75 |
|
|
signal sum_final : std_logic_vector(55 downto 0);
|
76 |
|
|
|
77 |
|
|
begin
|
78 |
|
|
|
79 |
|
|
rounding_amount <= "00000000000000000000000000000000000000000000000000000100";
|
80 |
|
|
round_nearest <= '1' when (round_mode = "00") else '0';
|
81 |
|
|
round_to_zero <= '1' when (round_mode = "01") else '0';
|
82 |
|
|
round_to_pos_inf <= '1' when (round_mode = "10") else '0';
|
83 |
|
|
round_to_neg_inf <= '1' when (round_mode = "11") else '0';
|
84 |
|
|
round_nearest_trigger <= '1' when round_nearest = '1' and mantissa_term(1) = '1'
|
85 |
|
|
else '0';
|
86 |
|
|
round_to_pos_inf_trigger <= '1' when sign_term = '0' and
|
87 |
|
|
or_reduce(mantissa_term(1 downto 0)) = '1' else '0';
|
88 |
|
|
round_to_neg_inf_trigger <= '1' when sign_term = '1' and
|
89 |
|
|
or_reduce(mantissa_term(1 downto 0)) = '1' else '0';
|
90 |
|
|
round_trigger <= '1' when ( round_nearest = '1' and round_nearest_trigger = '1')
|
91 |
|
|
or (round_to_pos_inf = '1' and round_to_pos_inf_trigger = '1')
|
92 |
|
|
or (round_to_neg_inf = '1' and round_to_neg_inf_trigger = '1')
|
93 |
|
|
else '0';
|
94 |
|
|
sum_round_overflow <= sum_round(55);
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
process
|
98 |
|
|
begin
|
99 |
|
|
wait until clk'event and clk = '1';
|
100 |
|
|
if (rst = '1') then
|
101 |
|
|
sum_round <= (others =>'0');
|
102 |
|
|
sum_round_2 <= (others =>'0');
|
103 |
|
|
exponent_round <= (others =>'0');
|
104 |
|
|
sum_final <= (others =>'0');
|
105 |
|
|
exponent_final <= (others =>'0');
|
106 |
|
|
exponent_final_2 <= (others =>'0');
|
107 |
|
|
round_out <= (others =>'0');
|
108 |
|
|
else
|
109 |
|
|
sum_round <= rounding_amount + mantissa_term;
|
110 |
|
|
if sum_round_overflow = '1' then
|
111 |
|
|
sum_round_2 <= shr(sum_round, conv_std_logic_vector('1', 56));
|
112 |
|
|
exponent_round <= exponent_term + "000000000001";
|
113 |
|
|
else
|
114 |
|
|
sum_round_2 <= sum_round;
|
115 |
|
|
exponent_round <= exponent_term;
|
116 |
|
|
end if;
|
117 |
|
|
if round_trigger = '1' then
|
118 |
|
|
sum_final <= sum_round_2;
|
119 |
|
|
exponent_final_2 <= exponent_round;
|
120 |
|
|
else
|
121 |
|
|
sum_final <= mantissa_term;
|
122 |
|
|
exponent_final_2 <= exponent_term;
|
123 |
|
|
end if;
|
124 |
|
|
exponent_final <= exponent_final_2;
|
125 |
|
|
round_out <= sign_term & exponent_final_2(10 downto 0) & sum_final(53 downto 2);
|
126 |
|
|
end if;
|
127 |
|
|
end process;
|
128 |
|
|
end rtl;
|