OpenCores
URL https://opencores.org/ocsvn/thor/thor/trunk

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [rtl/] [fpUnit/] [f2i.v] - Blame information for rev 51

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 51 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2016  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      f2i.v
9
//              - convert floating point to integer
10
//              - single cycle latency floating point unit
11
//              - parameterized width
12
//              - IEEE 754 representation
13
//
14
//
15
// This source file is free software: you can redistribute it and/or modify 
16
// it under the terms of the GNU Lesser General Public License as published 
17
// by the Free Software Foundation, either version 3 of the License, or     
18
// (at your option) any later version.                                      
19
//                                                                          
20
// This source file is distributed in the hope that it will be useful,      
21
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
23
// GNU General Public License for more details.                             
24
//                                                                          
25
// You should have received a copy of the GNU General Public License        
26
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
27
//                                                                          
28
//      i2f - convert integer to floating point
29
//  f2i - convert floating point to integer
30
//
31
// ============================================================================
32
 
33
module f2i
34
#(      parameter WID = 32)
35
(
36
        input clk,
37
        input ce,
38
        input [WID-1:0] i,
39
        output [WID-1:0] o,
40
        output overflow
41
);
42
localparam MSB = WID-1;
43
localparam EMSB = WID==128 ? 14 :
44
                  WID==96 ? 14 :
45
                  WID==80 ? 14 :
46
                  WID==64 ? 10 :
47
                                  WID==52 ? 10 :
48
                                  WID==48 ? 11 :
49
                                  WID==44 ? 10 :
50
                                  WID==42 ? 10 :
51
                                  WID==40 ?  9 :
52
                                  WID==32 ?  7 :
53
                                  WID==24 ?  6 : 4;
54
localparam FMSB = WID==128 ? 111 :
55
                  WID==96 ? 79 :
56
                  WID==80 ? 63 :
57
                  WID==64 ? 51 :
58
                                  WID==52 ? 39 :
59
                                  WID==48 ? 34 :
60
                                  WID==44 ? 31 :
61
                                  WID==42 ? 29 :
62
                                  WID==40 ? 28 :
63
                                  WID==32 ? 22 :
64
                                  WID==24 ? 15 : 9;
65
 
66
 
67
wire [MSB:0] maxInt  = {MSB{1'b1}};              // maximum unsigned integer value
68
wire [EMSB:0] zeroXp = {EMSB{1'b1}};     // simple constant - value of exp for zero
69
 
70
// Decompose fp value
71
reg sgn;                                                                        // sign
72
always @(posedge clk)
73
        if (ce) sgn = i[MSB];
74
wire [EMSB:0] exp = i[MSB-1:FMSB+1];             // exponent
75
wire [FMSB+1:0] man = {exp!=0,i[FMSB:0]};  // mantissa including recreate hidden bit
76
 
77
wire iz = i[MSB-1:0]==0;                                  // zero value (special)
78
 
79
assign overflow  = exp - zeroXp > MSB;          // lots of numbers are too big - don't forget one less bit is available due to signed values
80
wire underflow = exp < zeroXp - 1;                      // value less than 1/2
81
 
82
wire [7:0] shamt = MSB - (exp - zeroXp); // exp - zeroXp will be <= MSB
83
 
84
wire [MSB+1:0] o1 = {man,{EMSB+1{1'b0}},1'b0} >> shamt;  // keep an extra bit for rounding
85
wire [MSB:0] o2 = o1[MSB+1:1] + o1[0];            // round up
86
reg [MSB:0] o3;
87
 
88
always @(posedge clk)
89
        if (ce) begin
90
                if (underflow|iz)
91
                        o3 <= 0;
92
                else if (overflow)
93
                        o3 <= maxInt;
94
                // value between 1/2 and 1 - round up
95
                else if (exp==zeroXp-1)
96
                        o3 <= 1;
97
                // value > 1
98
                else
99
                        o3 <= o2;
100
        end
101
 
102
assign o = sgn ? -o3 : o3;                                      // adjust output for correct signed value
103
 
104
endmodule
105
 
106
module f2i_tb();
107
 
108
wire ov0,ov1;
109
wire [31:0] io0,io1;
110
reg clk;
111
 
112
initial begin
113
        clk = 0;
114
end
115
 
116
always #10 clk = ~clk;
117
 
118
f2i #(32) u1 (.clk(clk), .ce(1'b1), .i(32'h3F800000), .o(io1), .overflow(ov1) );
119
f2i #(32) u2 (.clk(clk), .ce(1'b1), .i(32'h00000000), .o(io0), .overflow(ov0) );
120
f2i #(80) u3 (.clk(clk), .ce(1'b1), .i(80'h3FF80000000000000000), .o(io1), .overflow(ov1) );
121
 
122
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.