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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [DFPSqrt96.sv] - Blame information for rev 75

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

Line No. Rev Author Line
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
import fp::*;
44
 
45
module DFPSqrt96(rst, clk, ce, ld, a, o, done, sqrinf, sqrneg);
46
parameter N=25;
47
localparam pShiftAmt =
48
        FPWID==80 ? 48 :
49
        FPWID==64 ? 36 :
50
        FPWID==32 ? 7 : (FMSB+1-16);
51
input rst;
52
input clk;
53
input ce;
54
input ld;
55
input DFP96 a;
56
output DFP96UD o;
57
output done;
58
output sqrinf;
59
output sqrneg;
60
 
61
// registered outputs
62
reg sign_exe;
63
reg inf;
64
reg     overflow;
65
reg     underflow;
66
 
67
wire so;
68
wire [13:0] xo;
69
wire [(N+1)*4*2-1:0] mo;
70
 
71
// constants
72
wire [13:0] infXp = 12'hBFF;    // infinite / NaN - all ones
73
// The following is a template for a quiet nan. (MSB=1)
74
wire [N*4-1:0] qNaN  = {4'h1,{N*4-4{1'b0}}};
75
 
76
// variables
77
wire [13:0] ex1;        // sum of exponents
78
wire ex1c;
79
wire [(N+1)*4*2-1:0] sqrto;
80
 
81
// Operands
82
reg sa;                 // sign bit
83
reg [11:0] xa;  // exponent bits
84
reg [N*4-1:0] siga;
85
reg a_dn;                       // a/b is denormalized
86
reg az;
87
reg aInf;
88
reg aNan;
89
wire done1;
90
wire [7:0] lzcnt;
91
wire [N*4-1:0] aa;
92
DFP96U au;
93
 
94
// -----------------------------------------------------------
95
// - decode the input operand
96
// - derive basic information
97
// - calculate exponent
98
// - calculate fraction
99
// -----------------------------------------------------------
100
 
101
DFPUnpack96 u01 (a, au);
102
always @(posedge clk)
103
        if (ce) sa <= au.sign;
104
always @(posedge clk)
105
        if (ce) xa <= au.exp;
106
always @(posedge clk)
107
        if (ce) siga <= au.sig;
108
always @(posedge clk)
109
        if (ce) a_dn <= au.exp==12'd0;
110
always @(posedge clk)
111
        if (ce) az <= au.exp==12'd0 && au.sig==100'd0;
112
always @(posedge clk)
113
        if (ce) aInf <= au.infinity;
114
always @(posedge clk)
115
        if (ce) aNan <= au.nan;
116
 
117
assign ex1 = xa + 1'd1;
118
assign xo = ex1 >> 1'd1;
119
 
120
assign so = 1'b0;                               // square root of positive numbers only
121
assign mo = aNan ? {4'h1,aa[N*4-1:0],{N*4{1'b0}}} : sqrto;      //(sqrto << pShiftAmt);
122
assign sqrinf = aInf;
123
assign sqrneg = !az & so;
124
 
125
wire [(N+1)*4-1:0] siga1 = xa[0] ? {siga,4'h0} : {4'h0,siga};
126
 
127
wire ldd;
128
delay1 #(1) u3 (.clk(clk), .ce(ce), .i(ld), .o(ldd));
129
 
130
// Ensure an even number of digits are processed.
131
dfisqrt #((N+2)&-2) u2
132
(
133
        .rst(rst),
134
        .clk(clk),
135
        .ce(ce),
136
        .ld(ldd),
137
        .a({4'h0,siga1}),
138
        .o(sqrto),
139
        .done(done)
140
);
141
 
142
always @*
143
casez({aNan,sqrinf,sqrneg})
144
3'b1??:
145
        begin
146
                o.sign <= sign;
147
                o.nan <= 1'b1;
148
                o.exp <= 12'hBFF;
149
                o.sig <= {siga,{N*4-4{1'b0}}};
150
        end
151
3'b01?:
152
        begin
153
                o.sign <= sign;
154
                o.nan <= 1'b1;
155
                o.exp <= 12'hBFF;
156
                o.sig <= {4'h1,qNaN|4'h5,{N*4-4{1'b0}}};
157
        end
158
3'b001:
159
        begin
160
                o.sign <= sign;
161
                o.nan <= 1'b1;
162
                o.exp <= 12'hBFF;
163
                o.sig <= {4'h1,qNaN|4'h6,{N*4-4{1'b0}}};
164
        end
165
default:
166
        begin
167
                o.sign <= 1'b0;
168
                o.nan <= 1'b0;
169
                o.exp <= xo;
170
                o.sig <= mo;
171
        end
172
endcase
173
 
174
 
175
endmodule
176
 
177
module DFPSqrt96nr(rst, clk, ce, ld, a, o, rm, done, inf, sqrinf, sqrneg);
178
parameter N=25;
179
input rst;
180
input clk;
181
input ce;
182
input ld;
183
input  DFP96 a;
184
output DFP96 o;
185
input [2:0] rm;
186
output done;
187
output inf;
188
output sqrinf;
189
output sqrneg;
190
 
191
wire DFP96UD o1;
192
wire inf1;
193
wire DFP96UN fpn0;
194
wire done1;
195
wire done2;
196
 
197
DFPSqrt96      #(.N(N)) u1 (rst, clk, ce, ld, a, o1, done1, sqrinf, sqrneg);
198
DFPNormalize96 #(.N(N)) u2(.clk(clk), .ce(ce), .under_i(1'b0), .i(o1), .o(fpn0) );
199
DFPRound96     #(.N(N)) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
200
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
201
delay2          #(1)   u8(.clk(clk), .ce(ce), .i(done1), .o(done2));
202
assign done = done1&done2;
203
 
204
endmodule

powered by: WebSVN 2.1.0

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