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

Subversion Repositories ft816float

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

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 26 robfinch
`include "fp_defines.v"
33
 
34
module fpSqrt(rst, clk, ce, ld, a, o, done, sqrinf, sqrneg);
35 12 robfinch
parameter WID = 128;
36 26 robfinch
`include "fpSize.sv"
37 12 robfinch
 
38
input rst;
39
input clk;
40
input ce;
41
input ld;
42
input [MSB:0] a;
43 26 robfinch
output reg [EX:0] o;
44 12 robfinch
output done;
45 26 robfinch
output sqrinf;
46
output sqrneg;
47 12 robfinch
 
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
 
81
// -----------------------------------------------------------
82
// - decode the input operand
83
// - derive basic information
84
// - calculate exponent
85
// - calculate fraction
86
// -----------------------------------------------------------
87
 
88
fpDecomp #(WID) u1
89
(
90
        .i(a),
91
        .sgn(sa),
92
        .exp(xa),
93
        .fract(fracta),
94
        .xz(a_dn),
95
        .vz(az),
96
        .inf(aInf),
97
        .nan(aNan)
98
);
99
 
100
assign ex1 = xa + 8'd1;
101
assign so = 1'b0;                               // square root of positive numbers only
102
assign xo = (ex1 >> 1) + (bias >> 1);   // divide by 2 cuts the bias in half, so 1/2 of it is added back in.
103
assign mo = aNan ? {1'b1,a[FMSB:0],{FMSB+1{1'b0}}} : (sqrto << 36);
104 26 robfinch
assign sqrinf = aInf;
105
assign sqrneg = !az & so;
106 12 robfinch
 
107
wire [FMSB+2:0] fracta1 = ex1[0] ? {1'b0,fracta} << 1 : {2'b0,fracta};
108
 
109
isqrt #(FX+1) u2
110
(
111
        .rst(rst),
112
        .clk(clk),
113
        .ce(ce),
114
        .ld(ld),
115
        .a({fracta1,{FMSB+1{1'b0}}}),
116
        .o(sqrto),
117
        .done(done)
118
);
119
 
120 26 robfinch
always @*
121
casez({aNan,sqrinf,sqrneg})
122
3'b1??: o <= {sa,xa,mo};
123
3'b01?: o <= {sa,1'b1,qNaN|`QSQRTINF,{FMSB+1{1'b0}}};
124
3'b001: o <= {sa,1'b1,qNaN|`QSQRTNEG,{FMSB+1{1'b0}}};
125
default:        o <= {so,xo,mo};
126
endcase
127
 
128 12 robfinch
 
129
endmodule
130
 
131 26 robfinch
module fpSqrtnr(rst, clk, ce, ld, a, o, rm, done, inf, sqrinf, sqrneg);
132 12 robfinch
parameter WID=32;
133 26 robfinch
`include "fpSize.sv"
134 12 robfinch
 
135
input rst;
136
input clk;
137
input ce;
138
input ld;
139
input  [MSB:0] a;
140
output [MSB:0] o;
141
input [2:0] rm;
142
output done;
143
output inf;
144 26 robfinch
output sqrinf;
145
output sqrneg;
146 12 robfinch
 
147
wire [EX:0] o1;
148
wire inf1;
149
wire [MSB+3:0] fpn0;
150
wire done1;
151
 
152 26 robfinch
fpSqrt      #(WID) u1 (rst, clk, ce, ld, a, o1, done1, sqrinf, sqrneg);
153 12 robfinch
fpNormalize #(WID) u2(.clk(clk), .ce(ce), .under(1'b0), .i(o1), .o(fpn0) );
154
fpRoundReg  #(WID) u3(.clk(clk), .ce(ce), .rm(rm), .i(fpn0), .o(o) );
155
delay2      #(1)   u5(.clk(clk), .ce(ce), .i(inf1), .o(inf));
156
delay2          #(1)   u8(.clk(clk), .ce(ce), .i(done1), .o(done));
157
endmodule
158
 

powered by: WebSVN 2.1.0

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