1 |
36 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2020 Robert Finch, Waterloo
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
48 |
robfinch |
// fpToPosit.sv
|
9 |
36 |
robfinch |
// - 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 |
48 |
robfinch |
import fp::*;
|
32 |
|
|
import posit::*;
|
33 |
36 |
robfinch |
|
34 |
|
|
module fpToPosit(i, o);
|
35 |
|
|
input [FPWID-1:0] i;
|
36 |
|
|
output reg [FPWID-1:0] o;
|
37 |
|
|
|
38 |
|
|
parameter BIAS = {1'b0,{EMSB{1'b1}}};
|
39 |
|
|
localparam N = FPWID;
|
40 |
|
|
localparam E = EMSB+1;
|
41 |
|
|
localparam Bs = $clog2(FPWID-1);
|
42 |
|
|
|
43 |
|
|
// operands sign,exponent,significand
|
44 |
|
|
wire sa;
|
45 |
|
|
wire [EMSB:0] xa;
|
46 |
|
|
wire [FMSB:0] ma;
|
47 |
|
|
wire [FMSB+1:0] fracta;
|
48 |
|
|
wire adn;
|
49 |
|
|
wire az;
|
50 |
|
|
wire xainf;
|
51 |
|
|
wire aInf;
|
52 |
|
|
wire aNan;
|
53 |
|
|
|
54 |
48 |
robfinch |
fpDecomp #(.FPWID(FPWID)) u1 (.i(i), .sgn(sa), .exp(xa), .man(ma), .fract(fracta), .xz(adn), .vz(az), .xinf(xaInf), .inf(aInf), .nan(aNan) );
|
55 |
36 |
robfinch |
assign sgno = sa;
|
56 |
|
|
wire [$clog2(FMSB+1):0] lzcnt;
|
57 |
|
|
generate begin : gCntlz
|
58 |
|
|
case(FPWID)
|
59 |
|
|
16: begin cntlz16 u2 ({fracta,5'h1f},lzcnt); end //1-5-10
|
60 |
|
|
20: begin cntlz16 u2 ({fracta,2'h3},lzcnt); end //1-6-13
|
61 |
|
|
32: begin cntlz32 u2 ({fracta,8'hFF},lzcnt); end // 1-8-23
|
62 |
|
|
40: begin cntlz32 u2 ({fracta,2'h3},lzcnt); end // 1-10-29
|
63 |
|
|
52: begin cntlz48 u2 ({fracta,7'h7F},lzcnt); end // 1-11-40
|
64 |
|
|
64: begin cntlz64 u2 ({fracta,11'h7FF},lzcnt); end // 1-11-52
|
65 |
|
|
80: begin cntlz80 u2 ({fracta,15'h7FFF},lzcnt); end // 1-15-64
|
66 |
|
|
default:
|
67 |
|
|
always @*
|
68 |
|
|
begin
|
69 |
|
|
$display("fpToPosit: Unsupported size");
|
70 |
|
|
$finish;
|
71 |
|
|
end
|
72 |
|
|
endcase
|
73 |
|
|
end
|
74 |
|
|
endgenerate
|
75 |
|
|
|
76 |
|
|
wire [N-1:0] sig_tmp = {fracta,{E{1'b0}}} << lzcnt;
|
77 |
|
|
|
78 |
|
|
// Convert exponent to twos complement from BIAS offset
|
79 |
|
|
wire [E:0] exp = xa - BIAS - lzcnt;
|
80 |
|
|
wire sxp = exp[E]; // get exponent sign
|
81 |
|
|
wire [E:0] absexp = sxp ? -exp : exp; // get absolute value
|
82 |
|
|
wire [es-1:0] e_o = (sxp & |absexp[es-1:0]) ? exp[es-1:0] : absexp[es-1:0];
|
83 |
|
|
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]};
|
84 |
|
|
// Exponent and Significand Packing
|
85 |
|
|
wire [2*N-1:0] tmp = {{N{~sxp}},sxp,e_o,sig_tmp[N-2:es]};
|
86 |
|
|
|
87 |
|
|
// Including Regime bits in Exponent-Significand Packing
|
88 |
|
|
wire [Bs-1:0] diff_b;
|
89 |
|
|
|
90 |
|
|
generate begin : gDiffb
|
91 |
|
|
if (E-es > Bs)
|
92 |
|
|
assign diff_b = |r_o[E-es-1:Bs] ? {{(Bs-2){1'b1}},2'b01} : r_o[Bs-1:0];
|
93 |
|
|
else
|
94 |
|
|
assign diff_b = r_o;
|
95 |
|
|
end
|
96 |
|
|
endgenerate
|
97 |
|
|
|
98 |
|
|
wire [2*N-1:0] tmp1 = tmp >> diff_b;
|
99 |
|
|
wire [N-1:0] tmp1s = sa ? -tmp1[N-1:0] : tmp1[N-1:0];
|
100 |
|
|
|
101 |
|
|
always @*
|
102 |
|
|
casez({az,aInf,aNan,~sig_tmp[N-1]})
|
103 |
|
|
4'b1???: o = {FPWID{1'b0}};
|
104 |
|
|
4'b01??: o = {1'b1,{FPWID-1{1'b0}}};
|
105 |
|
|
4'b001?: o = {1'b1,{FPWID-1{1'b0}}};
|
106 |
|
|
4'b0001: o = {1'b1,{FPWID-1{1'b0}}};
|
107 |
|
|
default: o = {sa,tmp1s[N-1:1]};
|
108 |
|
|
endcase
|
109 |
|
|
|
110 |
|
|
endmodule
|