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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpSqrt.v] - Blame information for rev 21

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

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

powered by: WebSVN 2.1.0

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