| 1 |
75 |
robfinch |
// ============================================================================
|
| 2 |
|
|
// __
|
| 3 |
|
|
// \\__/ o\ (C) 2018-2022 Robert Finch, Waterloo
|
| 4 |
|
|
// \ __ / All rights reserved.
|
| 5 |
|
|
// \/_// robfinch@finitron.ca
|
| 6 |
|
|
// ||
|
| 7 |
|
|
//
|
| 8 |
|
|
// DFPSqrt96.sv
|
| 9 |
|
|
// - decimal floating point square root
|
| 10 |
|
|
// - parameterized width
|
| 11 |
|
|
// - IEEE 754 representation
|
| 12 |
|
|
//
|
| 13 |
|
|
//
|
| 14 |
|
|
// BSD 3-Clause License
|
| 15 |
|
|
// Redistribution and use in source and binary forms, with or without
|
| 16 |
|
|
// modification, are permitted provided that the following conditions are met:
|
| 17 |
|
|
//
|
| 18 |
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
| 19 |
|
|
// list of conditions and the following disclaimer.
|
| 20 |
|
|
//
|
| 21 |
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
| 22 |
|
|
// this list of conditions and the following disclaimer in the documentation
|
| 23 |
|
|
// and/or other materials provided with the distribution.
|
| 24 |
|
|
//
|
| 25 |
|
|
// 3. Neither the name of the copyright holder nor the names of its
|
| 26 |
|
|
// contributors may be used to endorse or promote products derived from
|
| 27 |
|
|
// this software without specific prior written permission.
|
| 28 |
|
|
//
|
| 29 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 30 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 31 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 32 |
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 33 |
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 34 |
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 35 |
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 36 |
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 37 |
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 38 |
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 39 |
|
|
//
|
| 40 |
|
|
// ============================================================================
|
| 41 |
|
|
|
| 42 |
|
|
import DFPPkg::*;
|
| 43 |
|
|
|
| 44 |
|
|
module DFPSqrt96(rst, clk, ce, ld, a, o, done, sqrinf, sqrneg);
|
| 45 |
|
|
parameter N=25;
|
| 46 |
|
|
input rst;
|
| 47 |
|
|
input clk;
|
| 48 |
|
|
input ce;
|
| 49 |
|
|
input ld;
|
| 50 |
|
|
input DFP96 a;
|
| 51 |
|
|
output DFP96UD o;
|
| 52 |
|
|
output done;
|
| 53 |
|
|
output sqrinf;
|
| 54 |
|
|
output sqrneg;
|
| 55 |
|
|
|
| 56 |
|
|
// registered outputs
|
| 57 |
|
|
reg sign_exe;
|
| 58 |
|
|
reg inf;
|
| 59 |
|
|
reg overflow;
|
| 60 |
|
|
reg underflow;
|
| 61 |
78 |
robfinch |
wire sign = 1'b0;
|
| 62 |
75 |
robfinch |
|
| 63 |
|
|
wire so;
|
| 64 |
|
|
wire [13:0] xo;
|
| 65 |
|
|
wire [(N+1)*4*2-1:0] mo;
|
| 66 |
|
|
|
| 67 |
|
|
// constants
|
| 68 |
|
|
wire [13:0] infXp = 12'hBFF; // infinite / NaN - all ones
|
| 69 |
|
|
// The following is a template for a quiet nan. (MSB=1)
|
| 70 |
|
|
wire [N*4-1:0] qNaN = {4'h1,{N*4-4{1'b0}}};
|
| 71 |
|
|
|
| 72 |
|
|
// variables
|
| 73 |
|
|
wire [13:0] ex1; // sum of exponents
|
| 74 |
|
|
wire ex1c;
|
| 75 |
|
|
wire [(N+1)*4*2-1:0] sqrto;
|
| 76 |
|
|
|
| 77 |
|
|
// Operands
|
| 78 |
|
|
reg sa; // sign bit
|
| 79 |
|
|
reg [11:0] xa; // exponent bits
|
| 80 |
|
|
reg [N*4-1:0] siga;
|
| 81 |
|
|
reg a_dn; // a/b is denormalized
|
| 82 |
|
|
reg az;
|
| 83 |
|
|
reg aInf;
|
| 84 |
|
|
reg aNan;
|
| 85 |
|
|
wire done1;
|
| 86 |
|
|
wire [7:0] lzcnt;
|
| 87 |
|
|
wire [N*4-1:0] aa;
|
| 88 |
|
|
DFP96U au;
|
| 89 |
|
|
|
| 90 |
|
|
// -----------------------------------------------------------
|
| 91 |
|
|
// - decode the input operand
|
| 92 |
|
|
// - derive basic information
|
| 93 |
|
|
// - calculate exponent
|
| 94 |
|
|
// - calculate fraction
|
| 95 |
|
|
// -----------------------------------------------------------
|
| 96 |
|
|
|
| 97 |
|
|
DFPUnpack96 u01 (a, au);
|
| 98 |
|
|
always @(posedge clk)
|
| 99 |
|
|
if (ce) sa <= au.sign;
|
| 100 |
|
|
always @(posedge clk)
|
| 101 |
|
|
if (ce) xa <= au.exp;
|
| 102 |
|
|
always @(posedge clk)
|
| 103 |
|
|
if (ce) siga <= au.sig;
|
| 104 |
|
|
always @(posedge clk)
|
| 105 |
|
|
if (ce) a_dn <= au.exp==12'd0;
|
| 106 |
|
|
always @(posedge clk)
|
| 107 |
|
|
if (ce) az <= au.exp==12'd0 && au.sig==100'd0;
|
| 108 |
|
|
always @(posedge clk)
|
| 109 |
|
|
if (ce) aInf <= au.infinity;
|
| 110 |
|
|
always @(posedge clk)
|
| 111 |
|
|
if (ce) aNan <= au.nan;
|
| 112 |
|
|
|
| 113 |
|
|
assign ex1 = xa + 1'd1;
|
| 114 |
|
|
assign xo = ex1 >> 1'd1;
|
| 115 |
|
|
|
| 116 |
|
|
assign so = 1'b0; // square root of positive numbers only
|
| 117 |
|
|
assign mo = aNan ? {4'h1,aa[N*4-1:0],{N*4{1'b0}}} : sqrto; //(sqrto << pShiftAmt);
|
| 118 |
|
|
assign sqrinf = aInf;
|
| 119 |
|
|
assign sqrneg = !az & so;
|
| 120 |
|
|
|
| 121 |
|
|
wire [(N+1)*4-1:0] siga1 = xa[0] ? {siga,4'h0} : {4'h0,siga};
|
| 122 |
|
|
|
| 123 |
|
|
wire ldd;
|
| 124 |
|
|
delay1 #(1) u3 (.clk(clk), .ce(ce), .i(ld), .o(ldd));
|
| 125 |
|
|
|
| 126 |
|
|
// Ensure an even number of digits are processed.
|
| 127 |
|
|
dfisqrt #((N+2)&-2) u2
|
| 128 |
|
|
(
|
| 129 |
|
|
.rst(rst),
|
| 130 |
|
|
.clk(clk),
|
| 131 |
|
|
.ce(ce),
|
| 132 |
|
|
.ld(ldd),
|
| 133 |
|
|
.a({4'h0,siga1}),
|
| 134 |
|
|
.o(sqrto),
|
| 135 |
|
|
.done(done)
|
| 136 |
|
|
);
|
| 137 |
|
|
|
| 138 |
|
|
always @*
|
| 139 |
|
|
casez({aNan,sqrinf,sqrneg})
|
| 140 |
|
|
3'b1??:
|
| 141 |
|
|
begin
|
| 142 |
|
|
o.sign <= sign;
|
| 143 |
|
|
o.nan <= 1'b1;
|
| 144 |
|
|
o.exp <= 12'hBFF;
|
| 145 |
|
|
o.sig <= {siga,{N*4-4{1'b0}}};
|
| 146 |
|
|
end
|
| 147 |
|
|
3'b01?:
|
| 148 |
|
|
begin
|
| 149 |
|
|
o.sign <= sign;
|
| 150 |
|
|
o.nan <= 1'b1;
|
| 151 |
|
|
o.exp <= 12'hBFF;
|
| 152 |
|
|
o.sig <= {4'h1,qNaN|4'h5,{N*4-4{1'b0}}};
|
| 153 |
|
|
end
|
| 154 |
|
|
3'b001:
|
| 155 |
|
|
begin
|
| 156 |
|
|
o.sign <= sign;
|
| 157 |
|
|
o.nan <= 1'b1;
|
| 158 |
|
|
o.exp <= 12'hBFF;
|
| 159 |
|
|
o.sig <= {4'h1,qNaN|4'h6,{N*4-4{1'b0}}};
|
| 160 |
|
|
end
|
| 161 |
|
|
default:
|
| 162 |
|
|
begin
|
| 163 |
|
|
o.sign <= 1'b0;
|
| 164 |
|
|
o.nan <= 1'b0;
|
| 165 |
|
|
o.exp <= xo;
|
| 166 |
|
|
o.sig <= mo;
|
| 167 |
|
|
end
|
| 168 |
|
|
endcase
|
| 169 |
|
|
|
| 170 |
|
|
|
| 171 |
|
|
endmodule
|
| 172 |
|
|
|
| 173 |
|
|
module DFPSqrt96nr(rst, clk, ce, ld, a, o, rm, done, inf, sqrinf, sqrneg);
|
| 174 |
|
|
parameter N=25;
|
| 175 |
|
|
input rst;
|
| 176 |
|
|
input clk;
|
| 177 |
|
|
input ce;
|
| 178 |
|
|
input ld;
|
| 179 |
|
|
input DFP96 a;
|
| 180 |
|
|
output DFP96 o;
|
| 181 |
|
|
input [2:0] rm;
|
| 182 |
|
|
output done;
|
| 183 |
|
|
output inf;
|
| 184 |
|
|
output sqrinf;
|
| 185 |
|
|
output sqrneg;
|
| 186 |
|
|
|
| 187 |
|
|
wire DFP96UD o1;
|
| 188 |
|
|
wire inf1;
|
| 189 |
|
|
wire DFP96UN fpn0;
|
| 190 |
|
|
wire done1;
|
| 191 |
|
|
wire done2;
|
| 192 |
|
|
|
| 193 |
|
|
DFPSqrt96 #(.N(N)) u1 (rst, clk, ce, ld, a, o1, done1, sqrinf, sqrneg);
|
| 194 |
|
|
DFPNormalize96 #(.N(N)) u2(.clk(clk), .ce(ce), .under_i(1'b0), .i(o1), .o(fpn0) );
|
| 195 |
|
|
DFPRound96 #(.N(N)) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
|
| 196 |
|
|
delay2 #(1) u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
|
| 197 |
|
|
delay2 #(1) u8(.clk(clk), .ce(ce), .i(done1), .o(done2));
|
| 198 |
|
|
assign done = done1&done2;
|
| 199 |
|
|
|
| 200 |
|
|
endmodule
|