cvw/pipelined/src/fpu/unpackinput.sv

270 lines
12 KiB
Systemverilog
Raw Normal View History

2022-07-07 23:01:33 +00:00
///////////////////////////////////////////
//
// Written: me@KatherineParry.com
// Modified: 7/5/2022
//
// Purpose: unpack input
//
// A component of the Wally configurable RISC-V project.
//
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
//
// MIT LICENSE
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////////////////////
2022-05-31 16:18:50 +00:00
`include "wally-config.vh"
module unpackinput (
input logic [`FLEN-1:0] In, // inputs from register file
input logic En, // enable the input
input logic [`FMTBITS-1:0] Fmt, // format signal 00 - single 01 - double 11 - quad 10 - half
2022-05-31 16:18:50 +00:00
output logic Sgn, // sign bits of XYZ
output logic [`NE-1:0] Exp, // exponents of XYZ (converted to largest supported precision)
output logic [`NF:0] Man, // mantissas of XYZ (converted to largest supported precision)
output logic NaN, // is XYZ a NaN
output logic SNaN, // is XYZ a signaling NaN
output logic Zero, // is XYZ zero
output logic Inf, // is XYZ infinity
2022-06-01 16:52:21 +00:00
output logic ExpNonZero, // is the exponent not zero
output logic FracZero, // is the fraction zero
output logic ExpMax // does In have the maximum exponent (NaN or Inf)
2022-05-31 16:18:50 +00:00
);
logic [`NF-1:0] Frac; //Fraction of XYZ
logic ExpZero;
2022-06-01 16:52:21 +00:00
logic BadNaNBox;
2022-05-31 16:18:50 +00:00
if (`FPSIZES == 1) begin // if there is only one floating point format supported
2022-06-01 16:52:21 +00:00
assign BadNaNBox = 0;
assign Sgn = In[`FLEN-1]; // sign bit
assign Frac = In[`NF-1:0]; // fraction (no assumed 1)
2022-06-01 16:52:21 +00:00
assign ExpNonZero = |In[`FLEN-2:`NF]; // is the exponent non-zero
assign Exp = {In[`FLEN-2:`NF+1], In[`NF]|~ExpNonZero}; // exponent. Denormalized numbers have effective biased exponent of 1
assign ExpMax = &In[`FLEN-2:`NF]; // is the exponent all 1's
2022-05-31 16:18:50 +00:00
end else if (`FPSIZES == 2) begin // if there are 2 floating point formats supported
//***need better names for these constants
// largest format | smaller format
//----------------------------------
// `FLEN | `LEN1 length of floating point number
// `NE | `NE1 length of exponent
// `NF | `NF1 length of fraction
// `BIAS | `BIAS1 exponent's bias value
// `FMT | `FMT1 precision's format value - Q=11 D=01 S=00 H=10
// Possible combinantions specified by spec:
// double and single
// single and half
// Not needed but can also handle:
// quad and double
// quad and single
// quad and half
// double and half
assign BadNaNBox = ~(Fmt|(&In[`FLEN-1:`LEN1])); // Check NaN boxing
2022-05-31 16:18:50 +00:00
// choose sign bit depending on format - 1=larger precsion 0=smaller precision
assign Sgn = Fmt ? In[`FLEN-1] : In[`LEN1-1];
2022-05-31 16:18:50 +00:00
2022-05-31 17:40:34 +00:00
// extract the fraction, add trailing zeroes to the mantissa if nessisary
assign Frac = Fmt ? In[`NF-1:0] : {In[`NF1-1:0], (`NF-`NF1)'(0)};
2022-05-31 17:40:34 +00:00
// is the exponent non-zero
assign ExpNonZero = Fmt ? |In[`FLEN-2:`NF] : |In[`LEN1-2:`NF1];
2022-05-31 17:40:34 +00:00
2022-05-31 16:18:50 +00:00
// example double to single conversion:
// 1023 = 0011 1111 1111
// 127 = 0000 0111 1111 (subtract this)
// 896 = 0011 1000 0000
// sexp = 0000 bbbb bbbb (add this) b = bit d = ~b
// dexp = 0bdd dbbb bbbb
// also need to take into account possible zero/denorm/inf/NaN values
// extract the exponent, converting the smaller exponent into the larger precision if nessisary
// - if the original precision had a denormal number convert the exponent value 1
assign Exp = Fmt ? {In[`FLEN-2:`NF+1], In[`NF]|~ExpNonZero} : {In[`LEN1-2], {`NE-`NE1{~In[`LEN1-2]}}, In[`LEN1-3:`NF1+1], In[`NF1]|~ExpNonZero};
2022-05-31 17:40:34 +00:00
2022-05-31 16:18:50 +00:00
// is the exponent all 1's
assign ExpMax = Fmt ? &In[`FLEN-2:`NF] : &In[`LEN1-2:`NF1];
2022-05-31 16:18:50 +00:00
end else if (`FPSIZES == 3) begin // three floating point precsions supported
//***need better names for these constants
// largest format | larger format | smallest format
//---------------------------------------------------
// `FLEN | `LEN1 | `LEN2 length of floating point number
// `NE | `NE1 | `NE2 length of exponent
// `NF | `NF1 | `NF2 length of fraction
// `BIAS | `BIAS1 | `BIAS2 exponent's bias value
// `FMT | `FMT1 | `FMT2 precision's format value - Q=11 D=01 S=00 H=10
// Possible combinantions specified by spec:
// quad and double and single
// double and single and half
// Not needed but can also handle:
// quad and double and half
// quad and single and half
2022-06-01 16:52:21 +00:00
// Check NaN boxing
always_comb
case (Fmt)
2022-06-01 16:52:21 +00:00
`FMT: BadNaNBox = 0;
`FMT1: BadNaNBox = ~&In[`FLEN-1:`LEN1];
`FMT2: BadNaNBox = ~&In[`FLEN-1:`LEN2];
2022-06-14 23:58:39 +00:00
default: BadNaNBox = 1'bx;
2022-06-01 16:52:21 +00:00
endcase
2022-05-31 16:18:50 +00:00
2022-05-31 17:40:34 +00:00
// extract the sign bit
always_comb
case (Fmt)
2022-05-31 17:40:34 +00:00
`FMT: Sgn = In[`FLEN-1];
2022-06-01 16:52:21 +00:00
`FMT1: Sgn = In[`LEN1-1];
`FMT2: Sgn = In[`LEN2-1];
2022-06-14 23:58:39 +00:00
default: Sgn = 1'bx;
2022-05-31 17:40:34 +00:00
endcase
2022-05-31 16:18:50 +00:00
2022-05-31 17:40:34 +00:00
// extract the fraction
always_comb
case (Fmt)
2022-05-31 17:40:34 +00:00
`FMT: Frac = In[`NF-1:0];
2022-06-01 16:52:21 +00:00
`FMT1: Frac = {In[`NF1-1:0], (`NF-`NF1)'(0)};
`FMT2: Frac = {In[`NF2-1:0], (`NF-`NF2)'(0)};
2022-06-14 23:58:39 +00:00
default: Frac = {`NF{1'bx}};
2022-05-31 17:40:34 +00:00
endcase
2022-05-31 16:18:50 +00:00
2022-05-31 17:40:34 +00:00
// is the exponent non-zero
always_comb
case (Fmt)
2022-05-31 17:40:34 +00:00
`FMT: ExpNonZero = |In[`FLEN-2:`NF]; // if input is largest precision (`FLEN - ie quad or double)
2022-06-01 16:52:21 +00:00
`FMT1: ExpNonZero = |In[`LEN1-2:`NF1]; // if input is larger precsion (`LEN1 - double or single)
`FMT2: ExpNonZero = |In[`LEN2-2:`NF2]; // if input is smallest precsion (`LEN2 - single or half)
2022-06-14 23:58:39 +00:00
default: ExpNonZero = 1'bx;
2022-05-31 17:40:34 +00:00
endcase
// example double to single conversion:
// 1023 = 0011 1111 1111
// 127 = 0000 0111 1111 (subtract this)
// 896 = 0011 1000 0000
// sexp = 0000 bbbb bbbb (add this) b = bit d = ~b
// dexp = 0bdd dbbb bbbb
// also need to take into account possible zero/denorm/inf/NaN values
2022-05-31 16:18:50 +00:00
2022-05-31 17:40:34 +00:00
// convert the larger precision's exponent to use the largest precision's bias
always_comb
case (Fmt)
2022-06-01 16:52:21 +00:00
`FMT: Exp = {In[`FLEN-2:`NF+1], In[`NF]|~ExpNonZero};
`FMT1: Exp = {In[`LEN1-2], {`NE-`NE1{~In[`LEN1-2]}}, In[`LEN1-3:`NF1+1], In[`NF1]|~ExpNonZero};
`FMT2: Exp = {In[`LEN2-2], {`NE-`NE2{~In[`LEN2-2]}}, In[`LEN2-3:`NF2+1], In[`NF2]|~ExpNonZero};
2022-06-14 23:58:39 +00:00
default: Exp = {`NE{1'bx}};
2022-05-31 16:18:50 +00:00
endcase
2022-05-31 17:40:34 +00:00
// is the exponent all 1's
always_comb
case (Fmt)
2022-05-31 17:40:34 +00:00
`FMT: ExpMax = &In[`FLEN-2:`NF];
2022-06-01 16:52:21 +00:00
`FMT1: ExpMax = &In[`LEN1-2:`NF1];
`FMT2: ExpMax = &In[`LEN2-2:`NF2];
2022-06-14 23:58:39 +00:00
default: ExpMax = 1'bx;
2022-05-31 16:18:50 +00:00
endcase
end else if (`FPSIZES == 4) begin // if all precsisons are supported - quad, double, single, and half
// quad | double | single | half
//-------------------------------------------------------------------
// `Q_LEN | `D_LEN | `S_LEN | `H_LEN length of floating point number
// `Q_NE | `D_NE | `S_NE | `H_NE length of exponent
// `Q_NF | `D_NF | `S_NF | `H_NF length of fraction
// `Q_BIAS | `D_BIAS | `S_BIAS | `H_BIAS exponent's bias value
// `Q_FMT | `D_FMT | `S_FMT | `H_FMT precision's format value - Q=11 D=01 S=00 H=10
2022-06-01 16:52:21 +00:00
// Check NaN boxing
always_comb
case (Fmt)
2022-06-01 16:52:21 +00:00
2'b11: BadNaNBox = 0;
2'b01: BadNaNBox = ~&In[`Q_LEN-1:`D_LEN];
2'b00: BadNaNBox = ~&In[`Q_LEN-1:`S_LEN];
2'b10: BadNaNBox = ~&In[`Q_LEN-1:`H_LEN];
endcase
2022-05-31 16:18:50 +00:00
2022-05-31 17:40:34 +00:00
// extract sign bit
always_comb
case (Fmt)
2022-05-31 17:40:34 +00:00
2'b11: Sgn = In[`Q_LEN-1];
2022-06-01 16:52:21 +00:00
2'b01: Sgn = In[`D_LEN-1];
2'b00: Sgn = In[`S_LEN-1];
2'b10: Sgn = In[`H_LEN-1];
2022-05-31 17:40:34 +00:00
endcase
2022-05-31 16:18:50 +00:00
2022-05-31 17:40:34 +00:00
// extract the fraction
always_comb
case (Fmt)
2022-05-31 17:40:34 +00:00
2'b11: Frac = In[`Q_NF-1:0];
2022-06-01 16:52:21 +00:00
2'b01: Frac = {In[`D_NF-1:0], (`Q_NF-`D_NF)'(0)};
2'b00: Frac = {In[`S_NF-1:0], (`Q_NF-`S_NF)'(0)};
2'b10: Frac = {In[`H_NF-1:0], (`Q_NF-`H_NF)'(0)};
2022-05-31 17:40:34 +00:00
endcase
2022-05-31 16:18:50 +00:00
2022-05-31 17:40:34 +00:00
// is the exponent non-zero
always_comb
case (Fmt)
2022-05-31 17:40:34 +00:00
2'b11: ExpNonZero = |In[`Q_LEN-2:`Q_NF];
2022-06-01 16:52:21 +00:00
2'b01: ExpNonZero = |In[`D_LEN-2:`D_NF];
2'b00: ExpNonZero = |In[`S_LEN-2:`S_NF];
2'b10: ExpNonZero = |In[`H_LEN-2:`H_NF];
2022-05-31 17:40:34 +00:00
endcase
2022-05-31 16:18:50 +00:00
2022-05-31 17:40:34 +00:00
// example double to single conversion:
// 1023 = 0011 1111 1111
// 127 = 0000 0111 1111 (subtract this)
// 896 = 0011 1000 0000
// sexp = 0000 bbbb bbbb (add this) b = bit d = ~b
// dexp = 0bdd dbbb bbbb
// also need to take into account possible zero/denorm/inf/NaN values
// convert the double precsion exponent into quad precsion
always_comb
case (Fmt)
2022-06-01 16:52:21 +00:00
2'b11: Exp = {In[`Q_LEN-2:`Q_NF+1], In[`Q_NF]|~ExpNonZero};
2'b01: Exp = {In[`D_LEN-2], {`Q_NE-`D_NE{~In[`D_LEN-2]}}, In[`D_LEN-3:`D_NF+1], In[`D_NF]|~ExpNonZero};
2'b00: Exp = {In[`S_LEN-2], {`Q_NE-`S_NE{~In[`S_LEN-2]}}, In[`S_LEN-3:`S_NF+1], In[`S_NF]|~ExpNonZero};
2'b10: Exp = {In[`H_LEN-2], {`Q_NE-`H_NE{~In[`H_LEN-2]}}, In[`H_LEN-3:`H_NF+1], In[`H_NF]|~ExpNonZero};
2022-05-31 16:18:50 +00:00
endcase
2022-05-31 17:40:34 +00:00
// is the exponent all 1's
always_comb
case (Fmt)
2022-05-31 17:40:34 +00:00
2'b11: ExpMax = &In[`Q_LEN-2:`Q_NF];
2022-06-01 16:52:21 +00:00
2'b01: ExpMax = &In[`D_LEN-2:`D_NF];
2'b00: ExpMax = &In[`S_LEN-2:`S_NF];
2'b10: ExpMax = &In[`H_LEN-2:`H_NF];
2022-05-31 16:18:50 +00:00
endcase
end
2022-05-31 22:31:21 +00:00
// Output logic
2022-06-01 16:52:21 +00:00
assign FracZero = ~|Frac; // is the fraction zero?
2022-05-31 22:31:21 +00:00
assign Man = {ExpNonZero, Frac}; // add the assumed one (or zero if denormal or zero) to create the significand
assign NaN = ((ExpMax & ~FracZero)|BadNaNBox)&En; // is the input a NaN?
2022-06-01 16:52:21 +00:00
assign SNaN = NaN&~Frac[`NF-1]&~BadNaNBox; // is the input a singnaling NaN?
assign Inf = ExpMax & FracZero &En; // is the input infinity?
2022-06-01 16:52:21 +00:00
assign Zero = ~ExpNonZero & FracZero; // is the input zero?
2022-05-31 16:18:50 +00:00
endmodule