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 |
48 |
robfinch |
// BSD 3-Clause License
|
14 |
|
|
// Redistribution and use in source and binary forms, with or without
|
15 |
|
|
// modification, are permitted provided that the following conditions are met:
|
16 |
|
|
//
|
17 |
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
18 |
|
|
// list of conditions and the following disclaimer.
|
19 |
|
|
//
|
20 |
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
21 |
|
|
// this list of conditions and the following disclaimer in the documentation
|
22 |
|
|
// and/or other materials provided with the distribution.
|
23 |
|
|
//
|
24 |
|
|
// 3. Neither the name of the copyright holder nor the names of its
|
25 |
|
|
// contributors may be used to endorse or promote products derived from
|
26 |
|
|
// this software without specific prior written permission.
|
27 |
|
|
//
|
28 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
29 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
30 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
31 |
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
32 |
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
33 |
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
34 |
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
35 |
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
36 |
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
37 |
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
38 |
|
|
//
|
39 |
36 |
robfinch |
// ============================================================================
|
40 |
|
|
|
41 |
48 |
robfinch |
import posit::*;
|
42 |
36 |
robfinch |
|
43 |
|
|
module intToPosit(i, o);
|
44 |
42 |
robfinch |
localparam lzs = $clog2(PSTWID-1)-1;
|
45 |
36 |
robfinch |
input [PSTWID-1:0] i;
|
46 |
|
|
output [PSTWID-1:0] o;
|
47 |
|
|
|
48 |
|
|
wire [PSTWID*2-1+es+3-2:0] tmp, tmp1;
|
49 |
|
|
wire [PSTWID-2:0] ii = i[PSTWID-1] ? -i : i;
|
50 |
|
|
|
51 |
|
|
wire [lzs:0] lzcnt;
|
52 |
|
|
wire [PSTWID-1:0] rnd_ulp, tmp2, tmp2_rnd_ulp;
|
53 |
|
|
|
54 |
|
|
integer n;
|
55 |
|
|
positCntlz #(.PSTWID(PSTWID)) u1 (.i(ii[PSTWID-2:0]), .o(lzcnt));
|
56 |
|
|
|
57 |
|
|
wire sgn = i[PSTWID-1];
|
58 |
|
|
wire [rs:0] rgm = (PSTWID - (lzcnt + 2)) >> es;
|
59 |
|
|
wire [PSTWID-3:0] sig = ii << lzcnt; // left align significand, chop off leading one
|
60 |
|
|
generate begin : gExpandedPosit
|
61 |
|
|
// The number is represented as 1.x so for an integer it
|
62 |
|
|
// always needs to be left shifted.
|
63 |
|
|
// Add three trailers for guard, round and sticky.
|
64 |
|
|
if (es > 0) begin
|
65 |
|
|
// exp = lzcnt mod (2**es)
|
66 |
|
|
// remember es is constant so there are no shifts really
|
67 |
|
|
wire [es-1:0] exp = (PSTWID - (lzcnt + 2)) & {es{1'b1}};
|
68 |
|
|
assign tmp = {{{PSTWID-1{1'b1}},1'b0},exp,sig,3'b0};
|
69 |
|
|
end
|
70 |
|
|
else
|
71 |
|
|
assign tmp = {{{PSTWID-1{1'b1}},1'b0},sig,3'b0};
|
72 |
|
|
end
|
73 |
|
|
endgenerate
|
74 |
|
|
// Compute regime shift amount = number of bits to represent regime
|
75 |
|
|
// Need one extra bit for the terminator, and one extra '1' bit.
|
76 |
|
|
wire [rs:0] rgm_sh = rgm + 2'd2;
|
77 |
|
|
assign tmp1 = tmp >> rgm_sh;
|
78 |
|
|
wire L = tmp[rgm_sh-0+es];
|
79 |
|
|
wire G = tmp[rgm_sh-1+es];
|
80 |
|
|
wire R = tmp[rgm_sh-2+es];
|
81 |
|
|
reg S;
|
82 |
|
|
wire ulp;
|
83 |
|
|
always @*
|
84 |
|
|
begin
|
85 |
|
|
S = 0;
|
86 |
|
|
for (n = 0; n < PSTWID; n = n + 1) begin
|
87 |
|
|
if (n < rgm_sh - 2 + es)
|
88 |
|
|
S = S | tmp[n];
|
89 |
|
|
end
|
90 |
|
|
end
|
91 |
|
|
|
92 |
|
|
// Extract the bits representing the number, note leave off sign bit
|
93 |
|
|
assign tmp2 = tmp1[PSTWID-3+es+3:es+2];
|
94 |
|
|
// Round
|
95 |
|
|
assign ulp = ((G & (R | S)) | (L & G & ~(R | S)));
|
96 |
|
|
assign rnd_ulp = {{PSTWID-1{1'b0}},ulp};
|
97 |
|
|
assign tmp2_rnd_ulp = tmp2 + rnd_ulp;
|
98 |
|
|
// Final output
|
99 |
|
|
assign o = i=={PSTWID{1'b0}} ? {PSTWID{1'b0}} : sgn ? -tmp2_rnd_ulp : tmp2_rnd_ulp;
|
100 |
|
|
|
101 |
|
|
endmodule
|