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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpSqrt.sv] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2018-2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      fpSqrt.v
9
//    - floating point square root
10
//    - parameterized width
11
//    - IEEE 754 representation
12
//
13
//
14
// This source file is free software: you can redistribute it and/or modify
15
// it under the terms of the GNU Lesser General Public License as published
16
// by the Free Software Foundation, either version 3 of the License, or
17
// (at your option) any later version.
18
//
19
// This source file is distributed in the hope that it will be useful,
20
// but WITHOUT ANY WARRANTY; without even the implied warranty of
21
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
// GNU General Public License for more details.
23
//
24
// You should have received a copy of the GNU General Public License
25
// along with this program.  If not, see .
26
//
27
//      Floating Point Multiplier / Divider
28
//
29
// ============================================================================
30
 
31
import fp::*;
32
 
33
module fpSqrt(rst, clk, ce, ld, a, o, done, sqrinf, sqrneg);
34
localparam pShiftAmt =
35
        FPWID==80 ? 48 :
36
        FPWID==64 ? 36 :
37
        FPWID==32 ? 7 : (FMSB+1-16);
38
input rst;
39
input clk;
40
input ce;
41
input ld;
42
input [MSB:0] a;
43
output reg [EX:0] o;
44
output done;
45
output sqrinf;
46
output sqrneg;
47
 
48
// registered outputs
49
reg sign_exe;
50
reg inf;
51
reg     overflow;
52
reg     underflow;
53
 
54
wire so;
55
wire [EMSB:0] xo;
56
wire [FX:0] mo;
57
 
58
// constants
59
wire [EMSB:0] infXp = {EMSB+1{1'b1}};   // infinite / NaN - all ones
60
// The following is the value for an exponent of zero, with the offset
61
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
62
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}};       //2^0 exponent
63
// The following is a template for a quiet nan. (MSB=1)
64
wire [FMSB:0] qNaN  = {1'b1,{FMSB{1'b0}}};
65
 
66
// variables
67
wire [EMSB+2:0] ex1;    // sum of exponents
68
wire [FX:0] sqrto;
69
 
70
// Operands
71
wire sa;                        // sign bit
72
wire [EMSB:0] xa;       // exponent bits
73
wire [FMSB+1:0] fracta;
74
wire a_dn;                      // a/b is denormalized
75
wire az;
76
wire aInf;
77
wire aNan;
78
wire done1;
79
wire [7:0] lzcnt;
80
wire [MSB:0] aa;
81
 
82
// -----------------------------------------------------------
83
// - decode the input operand
84
// - derive basic information
85
// - calculate exponent
86
// - calculate fraction
87
// -----------------------------------------------------------
88
 
89
fpDecompReg u1
90
(
91
        .clk(clk),
92
        .ce(ce),
93
        .i(a),
94
        .o(aa),
95
        .sgn(sa),
96
        .exp(xa),
97
        .fract(fracta),
98
        .xz(a_dn),
99
        .vz(az),
100
        .inf(aInf),
101
        .nan(aNan)
102
);
103
 
104
assign ex1 = xa + 8'd1;
105
assign so = 1'b0;                               // square root of positive numbers only
106
assign xo = (ex1 >> 1) + (bias >> 1);   // divide by 2 cuts the bias in half, so 1/2 of it is added back in.
107
assign mo = aNan ? {1'b1,aa[FMSB:0],{FMSB+1{1'b0}}} : (sqrto << pShiftAmt);
108
assign sqrinf = aInf;
109
assign sqrneg = !az & so;
110
 
111
wire [FMSB+2:0] fracta1 = ex1[0] ? {1'b0,fracta} << 1 : {2'b0,fracta};
112
 
113
wire ldd;
114
delay1 #(1) u3 (.clk(clk), .ce(ce), .i(ld), .o(ldd));
115
 
116
isqrt #(FX+1) u2
117
(
118
        .rst(rst),
119
        .clk(clk),
120
        .ce(ce),
121
        .ld(ldd),
122
        .a({1'b0,fracta1,{FMSB+1{1'b0}}}),
123
        .o(sqrto),
124
        .done(done)
125
);
126
 
127
always @*
128
casez({aNan,sqrinf,sqrneg})
129
3'b1??: o <= {sa,xa,mo};
130
3'b01?: o <= {sa,1'b1,qNaN|`QSQRTINF,{FMSB+1{1'b0}}};
131
3'b001: o <= {sa,1'b1,qNaN|`QSQRTNEG,{FMSB+1{1'b0}}};
132
default:        o <= {so,xo,mo};
133
endcase
134
 
135
 
136
endmodule
137
 
138
module fpSqrtnr(rst, clk, ce, ld, a, o, rm, done, inf, sqrinf, sqrneg);
139
input rst;
140
input clk;
141
input ce;
142
input ld;
143
input  [MSB:0] a;
144
output [MSB:0] o;
145
input [2:0] rm;
146
output done;
147
output inf;
148
output sqrinf;
149
output sqrneg;
150
 
151
wire [EX:0] o1;
152
wire inf1;
153
wire [MSB+3:0] fpn0;
154
wire done1;
155
 
156
fpSqrt      #(FPWID) u1 (rst, clk, ce, ld, a, o1, done1, sqrinf, sqrneg);
157
fpNormalize #(FPWID) u2(.clk(clk), .ce(ce), .under_i(1'b0), .i(o1), .o(fpn0) );
158
fpRound  #(FPWID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
159
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
160
delay2          #(1)   u8(.clk(clk), .ce(ce), .i(done1), .o(done));
161
endmodule
162
 

powered by: WebSVN 2.1.0

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