| 1 |
45 |
robfinch |
// ============================================================================
|
| 2 |
|
|
// __
|
| 3 |
|
|
// \\__/ o\ (C) 2020 Robert Finch, Waterloo
|
| 4 |
|
|
// \ __ / All rights reserved.
|
| 5 |
|
|
// \/_// robfinch@finitron.ca
|
| 6 |
|
|
// ||
|
| 7 |
|
|
//
|
| 8 |
|
|
// positToInt.sv
|
| 9 |
|
|
// - posit number to integer convertor
|
| 10 |
|
|
// - can issue every clock cycle
|
| 11 |
|
|
// - parameterized width
|
| 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 |
48 |
robfinch |
import posit::*;
|
| 29 |
45 |
robfinch |
|
| 30 |
48 |
robfinch |
module positToInt(clk, ce, i, o);
|
| 31 |
|
|
input clk;
|
| 32 |
|
|
input ce;
|
| 33 |
45 |
robfinch |
input [PSTWID-1:0] i;
|
| 34 |
|
|
output reg [PSTWID-1:0] o;
|
| 35 |
|
|
|
| 36 |
|
|
localparam N = PSTWID;
|
| 37 |
|
|
localparam Bs = $clog2(PSTWID-1);
|
| 38 |
|
|
|
| 39 |
|
|
wire sgn;
|
| 40 |
|
|
wire rgs;
|
| 41 |
|
|
wire [Bs-1:0] rgm;
|
| 42 |
|
|
wire [es-1:0] exp;
|
| 43 |
|
|
wire [N-es-1:0] sig;
|
| 44 |
|
|
wire zer;
|
| 45 |
|
|
wire inf;
|
| 46 |
|
|
|
| 47 |
48 |
robfinch |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 48 |
|
|
// Clock #1
|
| 49 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 50 |
45 |
robfinch |
|
| 51 |
48 |
robfinch |
positDecomposeReg #(.PSTWID(PSTWID)) u1 (
|
| 52 |
|
|
.clk(clk),
|
| 53 |
|
|
.ce(ce),
|
| 54 |
|
|
.i(i),
|
| 55 |
|
|
.sgn(sgn),
|
| 56 |
|
|
.rgs(rgs),
|
| 57 |
|
|
.rgm(rgm),
|
| 58 |
|
|
.exp(exp),
|
| 59 |
|
|
.sig(sig),
|
| 60 |
|
|
.zer(zer),
|
| 61 |
|
|
.inf(inf)
|
| 62 |
|
|
);
|
| 63 |
|
|
|
| 64 |
45 |
robfinch |
wire [N-1:0] m = {sig,{es{1'b0}}};
|
| 65 |
46 |
robfinch |
wire isZero = zer;
|
| 66 |
48 |
robfinch |
|
| 67 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 68 |
|
|
// Clock #2
|
| 69 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 70 |
|
|
reg [15:0] argm2;
|
| 71 |
|
|
reg [es-1:0] exp2;
|
| 72 |
|
|
reg [N-1:0] m2;
|
| 73 |
|
|
always @(posedge clk)
|
| 74 |
|
|
if (ce) exp2 <= exp;
|
| 75 |
|
|
always @(posedge clk)
|
| 76 |
|
|
if (ce) m2 <= m;
|
| 77 |
|
|
always @(posedge clk)
|
| 78 |
|
|
if (ce) argm2 <= rgs ? rgm : -rgm;
|
| 79 |
|
|
|
| 80 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 81 |
|
|
// Clock #3
|
| 82 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 83 |
|
|
reg [15:0] ex3;
|
| 84 |
|
|
reg [N-1:0] m3;
|
| 85 |
|
|
always @(posedge clk)
|
| 86 |
|
|
if (ce) ex3 <= (argm2 << es) + exp2;
|
| 87 |
|
|
always @(posedge clk)
|
| 88 |
|
|
if (ce) m3 <= m2;
|
| 89 |
|
|
|
| 90 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 91 |
|
|
// Clock #4
|
| 92 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 93 |
|
|
reg exv4;
|
| 94 |
|
|
reg [15:0] ex4;
|
| 95 |
|
|
reg [N*2-1:0] mo4;
|
| 96 |
|
|
always @(posedge clk)
|
| 97 |
|
|
if (ce) exv4 <= ~ex3[15] && ex3 > PSTWID-1;
|
| 98 |
|
|
always @(posedge clk)
|
| 99 |
|
|
if (ce) ex4 <= ex3;
|
| 100 |
|
|
always @(posedge clk)
|
| 101 |
|
|
if (ce) mo4 = {m3,{N{1'b0}}} >> (PSTWID-ex3-1);
|
| 102 |
|
|
|
| 103 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 104 |
|
|
// Clock #5
|
| 105 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 106 |
|
|
reg [15:0] ex5;
|
| 107 |
|
|
reg [N*2-1:0] mo5;
|
| 108 |
|
|
reg L, G, R, St;
|
| 109 |
|
|
always @(posedge clk)
|
| 110 |
|
|
if (ce) ex5 <= ex4;
|
| 111 |
|
|
always @(posedge clk)
|
| 112 |
|
|
if (ce) mo5 = mo4;
|
| 113 |
|
|
always @(posedge clk)
|
| 114 |
|
|
if (ce) L <= mo4[N];
|
| 115 |
|
|
always @(posedge clk)
|
| 116 |
|
|
if (ce) G <= mo4[N-1];
|
| 117 |
|
|
always @(posedge clk)
|
| 118 |
|
|
if (ce) R <= mo4[N-2];
|
| 119 |
|
|
always @(posedge clk)
|
| 120 |
|
|
if (ce) St <= |mo4[N-3:0];
|
| 121 |
|
|
|
| 122 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 123 |
|
|
// Clock #6
|
| 124 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 125 |
46 |
robfinch |
// If regime+exp == -1 then the value is 0.5 or greater, so round up.
|
| 126 |
|
|
// If the regime+exp < -1 then the values is 0.25 or less, do not round up.
|
| 127 |
|
|
// Otherwise use rounding rules.
|
| 128 |
48 |
robfinch |
reg [N*2-1:0] mo6;
|
| 129 |
|
|
reg ulp;
|
| 130 |
|
|
always @(posedge clk)
|
| 131 |
|
|
if (ce) mo6 = mo5;
|
| 132 |
|
|
always @(posedge clk)
|
| 133 |
|
|
if (ce) ulp <= (~ex5[15] && ((G & (R | St)) | (L & G & ~(R | St)))) ||
|
| 134 |
|
|
(ex5==16'hFFFF);
|
| 135 |
46 |
robfinch |
wire [PSTWID-1:0] rnd_ulp = {{PSTWID-1{1'b0}},ulp};
|
| 136 |
45 |
robfinch |
|
| 137 |
48 |
robfinch |
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 138 |
|
|
// Clock #7
|
| 139 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 140 |
|
|
reg [PSTWID-1:0] tmp7;
|
| 141 |
|
|
wire rgs6;
|
| 142 |
|
|
delay #(.WID(1), .DEP(5)) ud1 (.clk(clk), .ce(ce), .i(rgs), .o(rgs6));
|
| 143 |
|
|
|
| 144 |
|
|
always @(posedge clk)
|
| 145 |
|
|
if (ce) tmp7 <= ~rgs6 ? rnd_ulp : mo6[N*2-1:N] + rnd_ulp;
|
| 146 |
|
|
|
| 147 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 148 |
|
|
// Clock #8
|
| 149 |
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| 150 |
|
|
wire isZero7, inf7, sgn7, exv7;
|
| 151 |
|
|
delay #(.WID(1), .DEP(6)) ud2 (.clk(clk), .ce(ce), .i(isZero), .o(isZero7));
|
| 152 |
|
|
delay #(.WID(1), .DEP(6)) ud3 (.clk(clk), .ce(ce), .i(inf), .o(inf7));
|
| 153 |
|
|
delay #(.WID(1), .DEP(6)) ud4 (.clk(clk), .ce(ce), .i(sgn), .o(sgn7));
|
| 154 |
|
|
delay #(.WID(1), .DEP(3)) ud5 (.clk(clk), .ce(ce), .i(exv4), .o(exv7));
|
| 155 |
|
|
|
| 156 |
|
|
always @(posedge clk)
|
| 157 |
|
|
if (ce)
|
| 158 |
|
|
casez({isZero7,inf7|exv7,sgn7}) // exponent all ones or exponent overflow?
|
| 159 |
45 |
robfinch |
// convert to +0.0 zero-in zero-out (the sign will always be plus)
|
| 160 |
48 |
robfinch |
3'b1??: o <= {PSTWID{1'b0}};
|
| 161 |
45 |
robfinch |
// Infinity in or exponent overflow in conversion = infinity out
|
| 162 |
48 |
robfinch |
3'b01?: o <= {1'b1,{PSTWID-1{1'b0}}};
|
| 163 |
|
|
3'b001: o <= ~tmp7 + 2'd1;
|
| 164 |
|
|
3'b000: o <= tmp7;
|
| 165 |
45 |
robfinch |
endcase
|
| 166 |
|
|
|
| 167 |
|
|
endmodule
|