OpenCores
URL https://opencores.org/ocsvn/ft816float/ft816float/trunk

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [positVerilog/] [intToPosit.sv] - Diff between revs 42 and 48

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 42 Rev 48
// ============================================================================
// ============================================================================
//        __
//        __
//   \\__/ o\    (C) 2020  Robert Finch, Waterloo
//   \\__/ o\    (C) 2020  Robert Finch, Waterloo
//    \  __ /    All rights reserved.
//    \  __ /    All rights reserved.
//     \/_//     robfinch@finitron.ca
//     \/_//     robfinch@finitron.ca
//       ||
//       ||
//
//
//      intToPosit.sv
//      intToPosit.sv
//    - integer to posit number converter
//    - integer to posit number converter
//    - parameterized width
//    - parameterized width
//
//
//
//
// This source file is free software: you can redistribute it and/or modify
// BSD 3-Clause License
// it under the terms of the GNU Lesser General Public License as published
// Redistribution and use in source and binary forms, with or without
// by the Free Software Foundation, either version 3 of the License, or
// modification, are permitted provided that the following conditions are met:
// (at your option) any later version.
 
//
 
// This source file is distributed in the hope that it will be useful,
 
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
// GNU General Public License for more details.
 
//
//
// You should have received a copy of the GNU General Public License
// 1. Redistributions of source code must retain the above copyright notice, this
// along with this program.  If not, see .
//    list of conditions and the following disclaimer.
 
//
 
// 2. Redistributions in binary form must reproduce the above copyright notice,
 
//    this list of conditions and the following disclaimer in the documentation
 
//    and/or other materials provided with the distribution.
 
//
 
// 3. Neither the name of the copyright holder nor the names of its
 
//    contributors may be used to endorse or promote products derived from
 
//    this software without specific prior written permission.
 
//
 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//
// ============================================================================
// ============================================================================
 
 
`include "positConfig.sv"
import posit::*;
 
 
module intToPosit(i, o);
module intToPosit(i, o);
`include "positSize.sv"
 
localparam rs = $clog2(PSTWID-1);
 
localparam lzs = $clog2(PSTWID-1)-1;
localparam lzs = $clog2(PSTWID-1)-1;
input [PSTWID-1:0] i;
input [PSTWID-1:0] i;
output [PSTWID-1:0] o;
output [PSTWID-1:0] o;
wire [PSTWID*2-1+es+3-2:0] tmp, tmp1;
wire [PSTWID*2-1+es+3-2:0] tmp, tmp1;
wire [PSTWID-2:0] ii = i[PSTWID-1] ? -i : i;
wire [PSTWID-2:0] ii = i[PSTWID-1] ? -i : i;
wire [lzs:0] lzcnt;
wire [lzs:0] lzcnt;
wire [PSTWID-1:0] rnd_ulp, tmp2, tmp2_rnd_ulp;
wire [PSTWID-1:0] rnd_ulp, tmp2, tmp2_rnd_ulp;
integer n;
integer n;
positCntlz #(.PSTWID(PSTWID)) u1 (.i(ii[PSTWID-2:0]), .o(lzcnt));
positCntlz #(.PSTWID(PSTWID)) u1 (.i(ii[PSTWID-2:0]), .o(lzcnt));
wire sgn = i[PSTWID-1];
wire sgn = i[PSTWID-1];
wire [rs:0] rgm = (PSTWID - (lzcnt + 2)) >> es;
wire [rs:0] rgm = (PSTWID - (lzcnt + 2)) >> es;
wire [PSTWID-3:0] sig = ii << lzcnt;  // left align significand, chop off leading one
wire [PSTWID-3:0] sig = ii << lzcnt;  // left align significand, chop off leading one
generate begin : gExpandedPosit
generate begin : gExpandedPosit
  // The number is represented as 1.x so for an integer it
  // The number is represented as 1.x so for an integer it
  // always needs to be left shifted.
  // always needs to be left shifted.
  // Add three trailers for guard, round and sticky.
  // Add three trailers for guard, round and sticky.
  if (es > 0) begin
  if (es > 0) begin
    // exp = lzcnt mod (2**es)
    // exp = lzcnt mod (2**es)
    // remember es is constant so there are no shifts really
    // remember es is constant so there are no shifts really
    wire [es-1:0] exp = (PSTWID - (lzcnt + 2)) & {es{1'b1}};
    wire [es-1:0] exp = (PSTWID - (lzcnt + 2)) & {es{1'b1}};
    assign tmp = {{{PSTWID-1{1'b1}},1'b0},exp,sig,3'b0};
    assign tmp = {{{PSTWID-1{1'b1}},1'b0},exp,sig,3'b0};
  end
  end
  else
  else
    assign tmp = {{{PSTWID-1{1'b1}},1'b0},sig,3'b0};
    assign tmp = {{{PSTWID-1{1'b1}},1'b0},sig,3'b0};
end
end
endgenerate
endgenerate
// Compute regime shift amount = number of bits to represent regime
// Compute regime shift amount = number of bits to represent regime
// Need one extra bit for the terminator, and one extra '1' bit.
// Need one extra bit for the terminator, and one extra '1' bit.
wire [rs:0] rgm_sh = rgm + 2'd2;
wire [rs:0] rgm_sh = rgm + 2'd2;
assign tmp1 = tmp >> rgm_sh;
assign tmp1 = tmp >> rgm_sh;
wire L = tmp[rgm_sh-0+es];
wire L = tmp[rgm_sh-0+es];
wire G = tmp[rgm_sh-1+es];
wire G = tmp[rgm_sh-1+es];
wire R = tmp[rgm_sh-2+es];
wire R = tmp[rgm_sh-2+es];
reg S;
reg S;
wire ulp;
wire ulp;
always @*
always @*
begin
begin
  S = 0;
  S = 0;
  for (n = 0; n < PSTWID; n = n + 1) begin
  for (n = 0; n < PSTWID; n = n + 1) begin
    if (n < rgm_sh - 2 + es)
    if (n < rgm_sh - 2 + es)
      S = S | tmp[n];
      S = S | tmp[n];
  end
  end
end
end
// Extract the bits representing the number, note leave off sign bit
// Extract the bits representing the number, note leave off sign bit
assign tmp2 = tmp1[PSTWID-3+es+3:es+2];
assign tmp2 = tmp1[PSTWID-3+es+3:es+2];
// Round
// Round
assign ulp = ((G & (R | S)) | (L & G & ~(R | S)));
assign ulp = ((G & (R | S)) | (L & G & ~(R | S)));
assign rnd_ulp = {{PSTWID-1{1'b0}},ulp};
assign rnd_ulp = {{PSTWID-1{1'b0}},ulp};
assign tmp2_rnd_ulp = tmp2 + rnd_ulp;
assign tmp2_rnd_ulp = tmp2 + rnd_ulp;
// Final output
// Final output
assign o = i=={PSTWID{1'b0}} ? {PSTWID{1'b0}} : sgn ? -tmp2_rnd_ulp : tmp2_rnd_ulp;
assign o = i=={PSTWID{1'b0}} ? {PSTWID{1'b0}} : sgn ? -tmp2_rnd_ulp : tmp2_rnd_ulp;
endmodule
endmodule
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.