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

Subversion Repositories ft816float

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

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 26 robfinch
//   \\__/ o\    (C) 2018-2019  Robert Finch, Waterloo
5 12 robfinch
//    \  __ /    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 28 robfinch
`include "fpConfig.sv"
33 26 robfinch
`include "fp_defines.v"
34
 
35
module fpSqrt(rst, clk, ce, ld, a, o, done, sqrinf, sqrneg);
36 28 robfinch
parameter FPWID = 32;
37 26 robfinch
`include "fpSize.sv"
38 28 robfinch
localparam pShiftAmt =
39
        FPWID==80 ? 48 :
40
        FPWID==64 ? 36 :
41
        FPWID==32 ? 7 : (FMSB+1-16);
42 12 robfinch
 
43
input rst;
44
input clk;
45
input ce;
46
input ld;
47
input [MSB:0] a;
48 26 robfinch
output reg [EX:0] o;
49 12 robfinch
output done;
50 26 robfinch
output sqrinf;
51
output sqrneg;
52 12 robfinch
 
53
// registered outputs
54
reg sign_exe;
55
reg inf;
56
reg     overflow;
57
reg     underflow;
58
 
59
wire so;
60
wire [EMSB:0] xo;
61
wire [FX:0] mo;
62
 
63
// constants
64
wire [EMSB:0] infXp = {EMSB+1{1'b1}};    // infinite / NaN - all ones
65
// The following is the value for an exponent of zero, with the offset
66
// eg. 8'h7f for eight bit exponent, 11'h7ff for eleven bit exponent, etc.
67
wire [EMSB:0] bias = {1'b0,{EMSB{1'b1}}};        //2^0 exponent
68
// The following is a template for a quiet nan. (MSB=1)
69
wire [FMSB:0] qNaN  = {1'b1,{FMSB{1'b0}}};
70
 
71
// variables
72
wire [EMSB+2:0] ex1;     // sum of exponents
73
wire [FX:0] sqrto;
74
 
75
// Operands
76
wire sa;                        // sign bit
77
wire [EMSB:0] xa;        // exponent bits
78
wire [FMSB+1:0] fracta;
79
wire a_dn;                      // a/b is denormalized
80
wire az;
81
wire aInf;
82
wire aNan;
83
wire done1;
84
wire [7:0] lzcnt;
85
 
86
// -----------------------------------------------------------
87
// - decode the input operand
88
// - derive basic information
89
// - calculate exponent
90
// - calculate fraction
91
// -----------------------------------------------------------
92
 
93 28 robfinch
fpDecomp #(FPWID) u1
94 12 robfinch
(
95
        .i(a),
96
        .sgn(sa),
97
        .exp(xa),
98
        .fract(fracta),
99
        .xz(a_dn),
100
        .vz(az),
101
        .inf(aInf),
102
        .nan(aNan)
103
);
104
 
105
assign ex1 = xa + 8'd1;
106
assign so = 1'b0;                               // square root of positive numbers only
107
assign xo = (ex1 >> 1) + (bias >> 1);   // divide by 2 cuts the bias in half, so 1/2 of it is added back in.
108 28 robfinch
assign mo = aNan ? {1'b1,a[FMSB:0],{FMSB+1{1'b0}}} : (sqrto << pShiftAmt);
109 26 robfinch
assign sqrinf = aInf;
110
assign sqrneg = !az & so;
111 12 robfinch
 
112
wire [FMSB+2:0] fracta1 = ex1[0] ? {1'b0,fracta} << 1 : {2'b0,fracta};
113
 
114
isqrt #(FX+1) u2
115
(
116
        .rst(rst),
117
        .clk(clk),
118
        .ce(ce),
119
        .ld(ld),
120 28 robfinch
        .a({1'b0,fracta1,{FMSB+1{1'b0}}}),
121 12 robfinch
        .o(sqrto),
122
        .done(done)
123
);
124
 
125 26 robfinch
always @*
126
casez({aNan,sqrinf,sqrneg})
127
3'b1??: o <= {sa,xa,mo};
128
3'b01?: o <= {sa,1'b1,qNaN|`QSQRTINF,{FMSB+1{1'b0}}};
129
3'b001: o <= {sa,1'b1,qNaN|`QSQRTNEG,{FMSB+1{1'b0}}};
130
default:        o <= {so,xo,mo};
131
endcase
132
 
133 12 robfinch
 
134
endmodule
135
 
136 26 robfinch
module fpSqrtnr(rst, clk, ce, ld, a, o, rm, done, inf, sqrinf, sqrneg);
137 28 robfinch
parameter FPWID=32;
138 26 robfinch
`include "fpSize.sv"
139 12 robfinch
 
140
input rst;
141
input clk;
142
input ce;
143
input ld;
144
input  [MSB:0] a;
145
output [MSB:0] o;
146
input [2:0] rm;
147
output done;
148
output inf;
149 26 robfinch
output sqrinf;
150
output sqrneg;
151 12 robfinch
 
152
wire [EX:0] o1;
153
wire inf1;
154
wire [MSB+3:0] fpn0;
155
wire done1;
156
 
157 28 robfinch
fpSqrt      #(FPWID) u1 (rst, clk, ce, ld, a, o1, done1, sqrinf, sqrneg);
158
fpNormalize #(FPWID) u2(.clk(clk), .ce(ce), .under_i(1'b0), .i(o1), .o(fpn0) );
159
fpRound  #(FPWID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
160 12 robfinch
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
161
delay2          #(1)   u8(.clk(clk), .ce(ce), .i(done1), .o(done));
162
endmodule
163
 

powered by: WebSVN 2.1.0

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