1 |
48 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2020 Robert Finch, Waterloo
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
// fpToPosit.v
|
9 |
|
|
// - floating point to posit number convertor
|
10 |
|
|
// - can issue every clock cycle
|
11 |
|
|
// - parameterized width
|
12 |
|
|
// - IEEE 754 representation
|
13 |
|
|
//
|
14 |
|
|
// Parts of this code originated from FP_to_Posit.v by Manish Kumar Jaiswal
|
15 |
|
|
//
|
16 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
17 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
18 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
19 |
|
|
// (at your option) any later version.
|
20 |
|
|
//
|
21 |
|
|
// This source file is distributed in the hope that it will be useful,
|
22 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
23 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
24 |
|
|
// GNU General Public License for more details.
|
25 |
|
|
//
|
26 |
|
|
// You should have received a copy of the GNU General Public License
|
27 |
|
|
// along with this program. If not, see .
|
28 |
|
|
//
|
29 |
|
|
// ============================================================================
|
30 |
|
|
|
31 |
|
|
`include "positConfig.sv"
|
32 |
|
|
`include "fpConfig.sv"
|
33 |
|
|
`include "fpTypes.sv"
|
34 |
|
|
|
35 |
|
|
module fpToPosit(i, o);
|
36 |
|
|
parameter FPWID = 32;
|
37 |
|
|
`include "fpSize.sv"
|
38 |
|
|
`include "positSize.sv"
|
39 |
|
|
input [FPWID-1:0] i;
|
40 |
|
|
output reg [FPWID-1:0] o;
|
41 |
|
|
|
42 |
|
|
parameter BIAS = {1'b0,{EMSB{1'b1}}};
|
43 |
|
|
localparam N = FPWID;
|
44 |
|
|
localparam E = EMSB+1;
|
45 |
|
|
localparam Bs = $clog2(FPWID-1);
|
46 |
|
|
|
47 |
|
|
// operands sign,exponent,significand
|
48 |
|
|
wire sa;
|
49 |
|
|
wire [EMSB:0] xa;
|
50 |
|
|
wire [FMSB:0] ma;
|
51 |
|
|
wire [FMSB+1:0] fracta;
|
52 |
|
|
wire adn;
|
53 |
|
|
wire az;
|
54 |
|
|
wire xainf;
|
55 |
|
|
wire aInf;
|
56 |
|
|
wire aNan;
|
57 |
|
|
|
58 |
|
|
fpDecomp #(FPWID) u1 (.i(i), .sgn(sa), .exp(xa), .man(ma), .fract(fracta), .xz(adn), .vz(az), .xinf(xaInf), .inf(aInf), .nan(aNan) );
|
59 |
|
|
assign sgno = sa;
|
60 |
|
|
wire [$clog2(FMSB+1):0] lzcnt;
|
61 |
|
|
generate begin : gCntlz
|
62 |
|
|
case(FPWID)
|
63 |
|
|
16: begin cntlz16 u2 ({fracta,5'h1f},lzcnt); end //1-5-10
|
64 |
|
|
20: begin cntlz16 u2 ({fracta,2'h3},lzcnt); end //1-6-13
|
65 |
|
|
32: begin cntlz32 u2 ({fracta,8'hFF},lzcnt); end // 1-8-23
|
66 |
|
|
40: begin cntlz32 u2 ({fracta,2'h3},lzcnt); end // 1-10-29
|
67 |
|
|
52: begin cntlz48 u2 ({fracta,7'h7F},lzcnt); end // 1-11-40
|
68 |
|
|
64: begin cntlz64 u2 ({fracta,11'h7FF},lzcnt); end // 1-11-52
|
69 |
|
|
80: begin cntlz80 u2 ({fracta,15'h7FFF},lzcnt); end // 1-15-64
|
70 |
|
|
default:
|
71 |
|
|
always @*
|
72 |
|
|
begin
|
73 |
|
|
$display("fpToPosit: Unsupported size");
|
74 |
|
|
$finish;
|
75 |
|
|
end
|
76 |
|
|
endcase
|
77 |
|
|
end
|
78 |
|
|
endgenerate
|
79 |
|
|
|
80 |
|
|
wire [N-1:0] sig_tmp = {fracta,{E{1'b0}}} << lzcnt;
|
81 |
|
|
|
82 |
|
|
// Convert exponent to twos complement from BIAS offset
|
83 |
|
|
wire [E:0] exp = xa - BIAS - lzcnt;
|
84 |
|
|
wire sxp = exp[E]; // get exponent sign
|
85 |
|
|
wire [E:0] absexp = sxp ? -exp : exp; // get absolute value
|
86 |
|
|
wire [es-1:0] e_o = (sxp & |absexp[es-1:0]) ? exp[es-1:0] : absexp[es-1:0];
|
87 |
|
|
wire [E-es-1:0] r_o = (~sxp || (sxp & |absexp[es-1:0])) ? {{Bs{1'b0}},absexp[E-1:es]} + 1'b1 : {{Bs{1'b0}},absexp[E-1:es]};
|
88 |
|
|
// Exponent and Significand Packing
|
89 |
|
|
wire [2*N-1:0] tmp = {{N{~sxp}},sxp,e_o,sig_tmp[N-2:es]};
|
90 |
|
|
|
91 |
|
|
// Including Regime bits in Exponent-Significand Packing
|
92 |
|
|
wire [Bs-1:0] diff_b;
|
93 |
|
|
|
94 |
|
|
generate begin : gDiffb
|
95 |
|
|
if (E-es > Bs)
|
96 |
|
|
assign diff_b = |r_o[E-es-1:Bs] ? {{(Bs-2){1'b1}},2'b01} : r_o[Bs-1:0];
|
97 |
|
|
else
|
98 |
|
|
assign diff_b = r_o;
|
99 |
|
|
end
|
100 |
|
|
endgenerate
|
101 |
|
|
|
102 |
|
|
wire [2*N-1:0] tmp1 = tmp >> diff_b;
|
103 |
|
|
wire [N-1:0] tmp1s = sa ? -tmp1[N-1:0] : tmp1[N-1:0];
|
104 |
|
|
|
105 |
|
|
always @*
|
106 |
|
|
casez({az,aInf,aNan,~sig_tmp[N-1]})
|
107 |
|
|
4'b1???: o = {FPWID{1'b0}};
|
108 |
|
|
4'b01??: o = {1'b1,{FPWID-1{1'b0}}};
|
109 |
|
|
4'b001?: o = {1'b1,{FPWID-1{1'b0}}};
|
110 |
|
|
4'b0001: o = {1'b1,{FPWID-1{1'b0}}};
|
111 |
|
|
default: o = {sa,tmp1s[N-1:1]};
|
112 |
|
|
endcase
|
113 |
|
|
|
114 |
|
|
endmodule
|