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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [positVerilog/] [positSqrt.sv] - Blame information for rev 39

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      positSqrt.v
9
//    - posit number square root function
10
//    - parameterized width
11
//
12
/////////////////////////////////////////////////////////////////////////////////
13
/////////////////////////////////////////////////////////////////////////////////
14
// This function currently only seems to work with even sizes of
15
// exponents.
16
/////////////////////////////////////////////////////////////////////////////////
17
/////////////////////////////////////////////////////////////////////////////////
18
//
19
//
20
// This source file is free software: you can redistribute it and/or modify
21
// it under the terms of the GNU Lesser General Public License as published
22
// by the Free Software Foundation, either version 3 of the License, or
23
// (at your option) any later version.
24
//
25
// This source file is distributed in the hope that it will be useful,
26
// but WITHOUT ANY WARRANTY; without even the implied warranty of
27
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
// GNU General Public License for more details.
29
//
30
// You should have received a copy of the GNU General Public License
31
// along with this program.  If not, see .
32
//
33
// ============================================================================
34
 
35
`include "positConfig.sv"
36
 
37
module positSqrt(clk, ce, i, o, start, done, zero, inf);
38
`include "positSize.sv"
39
localparam rs = $clog2(PSTWID-1)-1;
40
input clk;
41
input ce;
42
input [PSTWID-1:0] i;
43
output reg [PSTWID-1:0] o;
44
input start;
45
output done;
46
output zero;
47
output inf;
48
 
49
wire si, so;
50
wire [rs:0] rgmi;
51
wire rgsi;
52
wire [es-1:0] expi;
53
wire [PSTWID-es-1:0] sigi;
54
wire zeri;
55
wire infi;
56
wire inf = infi;
57
wire zero = zeri;
58
 
59
positDecompose #(PSTWID,es) u1 (
60
  .i(i),
61
  .sgn(si),
62
  .rgs(rgsi),
63
  .rgm(rgmi),
64
  .exp(expi),
65
  .sig(sigi),
66
  .zer(zeri),
67
  .inf(infi)
68
);
69
 
70
assign so = si;                         // square root of positive numbers only
71
wire [rs+1:0] rgm1 = rgsi ? rgmi : -rgmi;
72
wire [rs+es+1:0] rx1 = {rgm1,expi};
73
// If exponent is odd, make it even. May need to shift the significand later.
74
wire [rs+es+1:0] rxtmp = {{2{rx1[rs+es+1]}},rx1} >> 1;   // right shift takes square root of exponent
75
 
76
assign sqrinf = infi;
77
assign sqrneg = so;
78
// If the exponent was made even, shift the significand left.
79
wire [PSTWID-1:0] sig1 = rx1[0] ? {sigi,1'b0} : {1'b0,sigi};
80
 
81
wire ldd;
82
delay1 #(1) u3 (.clk(clk), .ce(ce), .i(start), .o(ldd));
83
wire [PSTWID*3-1:0] sqrto;
84
 
85
wire [rs:0] lzcnt;
86
 
87
// iqsrt2 left aligns the number
88
isqrt2 #(PSTWID*3/2) u2
89
(
90
        .rst(rst),
91
        .clk(clk),
92
        .ce(ce),
93
        .ld(ldd),
94
        .a({sig1,{(PSTWID/2+1-(PSTWID%2)){1'b0}}}),
95
        .o(sqrto),
96
        .done(done),
97
        .lzcnt(lzcnt)
98
);
99
 
100
// There should not be very many leading zeros in the number as the number is
101
// always between 1 and 2, so the square root is between 1.0 and 1.414....
102
// May want to change the leading zero detect to be a little more efficient.
103
//positCntlz #(.PSTWID(PSTWID)) u4 (.i(sqrto[PSTWID-1:0]), .o(lzcnt));
104
wire [PSTWID*2-1:0] sqrt1 = sqrto[PSTWID*3-1:PSTWID];// << (lzcnt + PSTWID/2);
105
 
106
// Make a negative rx positive
107
wire [rs+es+1:0] rxtmp2c = rxtmp[rs+es+1] ? ~rxtmp + 2'd1 : rxtmp;
108
// Break out the exponent and regime portions
109
wire [es-1:0] exp = rxtmp[es-1:0];
110
// Take absolute value of regime portion
111
wire srxtmp = rxtmp[rs+es+1];
112
wire [rs:0] rgm = srxtmp ? -rxtmp[rs+es+1:es] : rxtmp[rs+es+1:es];
113
// Compute the length of the regime bit string, +1 for positive regime
114
wire [rs:0] rgml = srxtmp ? rxtmp2c[rs+es:es] + 2'd1: rxtmp2c[rs+es:es] + 2'd2;
115
// Build expanded posit number:
116
// trim one leading bit off the product bits
117
// and keep guard, round bits, and create sticky bit
118
wire [PSTWID*3-1+9-es:0] tmp;
119
generate begin : gTmp
120
case(es)
121
0: assign tmp = {{PSTWID-1{~srxtmp}},srxtmp,sqrt1[PSTWID*2-2:0],{9-es{1'b0}}};
122
1,2,3,4,5,6:
123
  assign tmp = {{PSTWID-1{~srxtmp}},srxtmp,exp,sqrt1[PSTWID*2-2:0],{9-es{1'b0}}};
124
default:
125
always @*
126
  begin
127
    $display("positSqrt: unsupported es");
128
    $finish;
129
  end
130
endcase
131
end
132
endgenerate
133
wire [PSTWID*3-1+9-es:0] tmp1 = tmp >> rgml;
134
 
135
// Rounding
136
// Guard, Round, and Sticky
137
wire L = tmp1[PSTWID+8], G = tmp1[PSTWID+7], R = tmp1[PSTWID+6], St = |tmp1[PSTWID+5:0],
138
     ulp = ((G & (R | St)) | (L & G & ~(R | St)));
139
wire [PSTWID-1:0] rnd_ulp = {{PSTWID-1{1'b0}},ulp};
140
 
141
wire [PSTWID:0] tmp1_rnd_ulp = tmp1[2*PSTWID+7:PSTWID+8] + rnd_ulp;
142
wire [PSTWID-1:0] tmp1_rnd = (rgml < PSTWID-es-2) ? tmp1_rnd_ulp[PSTWID-1:0] : tmp1[2*PSTWID+7:PSTWID+8];
143
 
144
always @*
145
  casez({infi,sqrinf,sqrneg,zero})
146
  4'b1???:  o = {1'b1,{PSTWID-1{1'b0}}};
147
  4'b01??:  o = {1'b1,{PSTWID-1{1'b0}}};
148
  4'b001?:  o = {1'b1,{PSTWID-1{1'b0}}};
149
  4'b0001:  o = {PSTWID{1'b0}};
150
  default:  o = {1'b0,tmp1_rnd[PSTWID-1:1]};
151
  endcase
152
 
153
endmodule

powered by: WebSVN 2.1.0

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