1 |
36 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2020 Robert Finch, Waterloo
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
// intToPosit.sv
|
9 |
|
|
// - integer to posit number converter
|
10 |
|
|
// - parameterized width
|
11 |
|
|
//
|
12 |
|
|
//
|
13 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
14 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
15 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
16 |
|
|
// (at your option) any later version.
|
17 |
|
|
//
|
18 |
|
|
// This source file is distributed in the hope that it will be useful,
|
19 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21 |
|
|
// GNU General Public License for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with this program. If not, see .
|
25 |
|
|
//
|
26 |
|
|
// ============================================================================
|
27 |
|
|
|
28 |
|
|
`include "positConfig.sv"
|
29 |
|
|
|
30 |
|
|
module intToPosit(i, o);
|
31 |
|
|
`include "positSize.sv"
|
32 |
|
|
localparam rs = $clog2(PSTWID-1);
|
33 |
42 |
robfinch |
localparam lzs = $clog2(PSTWID-1)-1;
|
34 |
36 |
robfinch |
input [PSTWID-1:0] i;
|
35 |
|
|
output [PSTWID-1:0] o;
|
36 |
|
|
|
37 |
|
|
wire [PSTWID*2-1+es+3-2:0] tmp, tmp1;
|
38 |
|
|
wire [PSTWID-2:0] ii = i[PSTWID-1] ? -i : i;
|
39 |
|
|
|
40 |
|
|
wire [lzs:0] lzcnt;
|
41 |
|
|
wire [PSTWID-1:0] rnd_ulp, tmp2, tmp2_rnd_ulp;
|
42 |
|
|
|
43 |
|
|
integer n;
|
44 |
|
|
positCntlz #(.PSTWID(PSTWID)) u1 (.i(ii[PSTWID-2:0]), .o(lzcnt));
|
45 |
|
|
|
46 |
|
|
wire sgn = i[PSTWID-1];
|
47 |
|
|
wire [rs:0] rgm = (PSTWID - (lzcnt + 2)) >> es;
|
48 |
|
|
wire [PSTWID-3:0] sig = ii << lzcnt; // left align significand, chop off leading one
|
49 |
|
|
generate begin : gExpandedPosit
|
50 |
|
|
// The number is represented as 1.x so for an integer it
|
51 |
|
|
// always needs to be left shifted.
|
52 |
|
|
// Add three trailers for guard, round and sticky.
|
53 |
|
|
if (es > 0) begin
|
54 |
|
|
// exp = lzcnt mod (2**es)
|
55 |
|
|
// remember es is constant so there are no shifts really
|
56 |
|
|
wire [es-1:0] exp = (PSTWID - (lzcnt + 2)) & {es{1'b1}};
|
57 |
|
|
assign tmp = {{{PSTWID-1{1'b1}},1'b0},exp,sig,3'b0};
|
58 |
|
|
end
|
59 |
|
|
else
|
60 |
|
|
assign tmp = {{{PSTWID-1{1'b1}},1'b0},sig,3'b0};
|
61 |
|
|
end
|
62 |
|
|
endgenerate
|
63 |
|
|
// Compute regime shift amount = number of bits to represent regime
|
64 |
|
|
// Need one extra bit for the terminator, and one extra '1' bit.
|
65 |
|
|
wire [rs:0] rgm_sh = rgm + 2'd2;
|
66 |
|
|
assign tmp1 = tmp >> rgm_sh;
|
67 |
|
|
wire L = tmp[rgm_sh-0+es];
|
68 |
|
|
wire G = tmp[rgm_sh-1+es];
|
69 |
|
|
wire R = tmp[rgm_sh-2+es];
|
70 |
|
|
reg S;
|
71 |
|
|
wire ulp;
|
72 |
|
|
always @*
|
73 |
|
|
begin
|
74 |
|
|
S = 0;
|
75 |
|
|
for (n = 0; n < PSTWID; n = n + 1) begin
|
76 |
|
|
if (n < rgm_sh - 2 + es)
|
77 |
|
|
S = S | tmp[n];
|
78 |
|
|
end
|
79 |
|
|
end
|
80 |
|
|
|
81 |
|
|
// Extract the bits representing the number, note leave off sign bit
|
82 |
|
|
assign tmp2 = tmp1[PSTWID-3+es+3:es+2];
|
83 |
|
|
// Round
|
84 |
|
|
assign ulp = ((G & (R | S)) | (L & G & ~(R | S)));
|
85 |
|
|
assign rnd_ulp = {{PSTWID-1{1'b0}},ulp};
|
86 |
|
|
assign tmp2_rnd_ulp = tmp2 + rnd_ulp;
|
87 |
|
|
// Final output
|
88 |
|
|
assign o = i=={PSTWID{1'b0}} ? {PSTWID{1'b0}} : sgn ? -tmp2_rnd_ulp : tmp2_rnd_ulp;
|
89 |
|
|
|
90 |
|
|
endmodule
|