| 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 |
|
|
`include "positConfig.sv"
|
| 29 |
|
|
|
| 30 |
|
|
module positToInt(i, o);
|
| 31 |
|
|
`include "positSize.sv"
|
| 32 |
|
|
input [PSTWID-1:0] i;
|
| 33 |
|
|
output reg [PSTWID-1:0] o;
|
| 34 |
|
|
|
| 35 |
|
|
localparam N = PSTWID;
|
| 36 |
|
|
localparam Bs = $clog2(PSTWID-1);
|
| 37 |
|
|
|
| 38 |
|
|
wire sgn;
|
| 39 |
|
|
wire rgs;
|
| 40 |
|
|
wire [Bs-1:0] rgm;
|
| 41 |
|
|
wire [es-1:0] exp;
|
| 42 |
|
|
wire [N-es-1:0] sig;
|
| 43 |
|
|
wire zer;
|
| 44 |
|
|
wire inf;
|
| 45 |
|
|
|
| 46 |
|
|
positDecompose #(.PSTWID(PSTWID), .es(es)) u1 (.i(i), .sgn(sgn), .rgs(rgs), .rgm(rgm), .exp(exp), .sig(sig), .zer(zer), .inf(inf));
|
| 47 |
|
|
|
| 48 |
|
|
wire [N-1:0] m = {sig,{es{1'b0}}};
|
| 49 |
46 |
robfinch |
wire isZero = zer;
|
| 50 |
|
|
wire [15:0] argm = rgs ? rgm : -rgm;
|
| 51 |
|
|
wire [15:0] ex1 = (argm << es) + exp;
|
| 52 |
|
|
wire exv = ~ex1[15] && ex1 > PSTWID-1;
|
| 53 |
|
|
wire [N*2-1:0] mo = {m,{N{1'b0}}} >> (PSTWID-ex1-1);
|
| 54 |
|
|
wire L = mo[N];
|
| 55 |
|
|
wire G = mo[N-1];
|
| 56 |
|
|
wire R = mo[N-2];
|
| 57 |
|
|
wire St = |mo[N-3:0];
|
| 58 |
|
|
// If regime+exp == -1 then the value is 0.5 or greater, so round up.
|
| 59 |
|
|
// If the regime+exp < -1 then the values is 0.25 or less, do not round up.
|
| 60 |
|
|
// Otherwise use rounding rules.
|
| 61 |
|
|
wire ulp = (~ex1[15] && ((G & (R | St)) | (L & G & ~(R | St)))) ||
|
| 62 |
|
|
(ex1==16'hFFFF);
|
| 63 |
|
|
wire [PSTWID-1:0] rnd_ulp = {{PSTWID-1{1'b0}},ulp};
|
| 64 |
|
|
wire [PSTWID-1:0] tmp = ~rgs ? rnd_ulp : mo[N*2-1:N] + rnd_ulp;
|
| 65 |
45 |
robfinch |
|
| 66 |
|
|
always @*
|
| 67 |
46 |
robfinch |
casez({isZero,inf|exv}) // exponent all ones or exponent overflow?
|
| 68 |
45 |
robfinch |
// convert to +0.0 zero-in zero-out (the sign will always be plus)
|
| 69 |
|
|
2'b1?: o = {PSTWID{1'b0}};
|
| 70 |
|
|
// Infinity in or exponent overflow in conversion = infinity out
|
| 71 |
|
|
2'b01: o = {1'b1,{PSTWID-1{1'b0}}};
|
| 72 |
|
|
// Other numbers
|
| 73 |
46 |
robfinch |
default: o = sgn ? -tmp : tmp;
|
| 74 |
45 |
robfinch |
endcase
|
| 75 |
|
|
|
| 76 |
|
|
endmodule
|