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 |
|
|
|
36 |
|
|
`timescale 1ns / 100ps
|
37 |
|
|
|
38 |
|
|
module fpu_round( clk, rst, enable, round_mode, sign_term,
|
39 |
|
|
mantissa_term, exponent_term, round_out, exponent_final);
|
40 |
|
|
input clk;
|
41 |
|
|
input rst;
|
42 |
|
|
input enable;
|
43 |
|
|
input [1:0] round_mode;
|
44 |
|
|
input sign_term;
|
45 |
|
|
input [55:0] mantissa_term;
|
46 |
|
|
input [11:0] exponent_term;
|
47 |
|
|
output [63:0] round_out;
|
48 |
|
|
output [11:0] exponent_final;
|
49 |
|
|
|
50 |
|
|
wire [55:0] rounding_amount = { 53'b0, 1'b1, 2'b0};
|
51 |
|
|
wire round_nearest = (round_mode == 2'b00);
|
52 |
|
|
wire round_to_zero = (round_mode == 2'b01);
|
53 |
|
|
wire round_to_pos_inf = (round_mode == 2'b10);
|
54 |
|
|
wire round_to_neg_inf = (round_mode == 2'b11);
|
55 |
|
|
wire round_nearest_trigger = round_nearest & mantissa_term[1];
|
56 |
|
|
wire round_to_pos_inf_trigger = !sign_term & |mantissa_term[1:0];
|
57 |
|
|
wire round_to_neg_inf_trigger = sign_term & |mantissa_term[1:0];
|
58 |
|
|
wire round_trigger = ( round_nearest & round_nearest_trigger)
|
59 |
|
|
| (round_to_pos_inf & round_to_pos_inf_trigger)
|
60 |
|
|
| (round_to_neg_inf & round_to_neg_inf_trigger);
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
reg [55:0] sum_round;
|
64 |
|
|
wire sum_round_overflow = sum_round[55];
|
65 |
|
|
// will be 0 if no carry, 1 if overflow from the rounding unit
|
66 |
|
|
// overflow from rounding is extremely rare, but possible
|
67 |
|
|
reg [55:0] sum_round_2;
|
68 |
|
|
reg [11:0] exponent_round;
|
69 |
|
|
reg [55:0] sum_final;
|
70 |
|
|
reg [11:0] exponent_final;
|
71 |
|
|
reg [63:0] round_out;
|
72 |
|
|
|
73 |
|
|
always @(posedge clk)
|
74 |
|
|
begin
|
75 |
|
|
if (rst) begin
|
76 |
|
|
sum_round <= 0;
|
77 |
|
|
sum_round_2 <= 0;
|
78 |
|
|
exponent_round <= 0;
|
79 |
|
|
sum_final <= 0;
|
80 |
|
|
exponent_final <= 0;
|
81 |
|
|
round_out <= 0;
|
82 |
|
|
end
|
83 |
|
|
else begin
|
84 |
|
|
sum_round <= rounding_amount + mantissa_term;
|
85 |
|
|
sum_round_2 <= sum_round_overflow ? sum_round >> 1 : sum_round;
|
86 |
|
|
exponent_round <= sum_round_overflow ? (exponent_term + 1) : exponent_term;
|
87 |
|
|
sum_final <= round_trigger ? sum_round_2 : mantissa_term;
|
88 |
|
|
exponent_final <= round_trigger ? exponent_round : exponent_term;
|
89 |
|
|
round_out <= { sign_term, exponent_final[10:0], sum_final[53:2] };
|
90 |
|
|
end
|
91 |
|
|
end
|
92 |
|
|
endmodule
|