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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpSigmoid.v] - Blame information for rev 39

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

Line No. Rev Author Line
1 18 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2017-2019  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      sigmoid.v
9
//              - perform sigmoid function
10
//
11
//
12
// This source file is free software: you can redistribute it and/or modify 
13
// it under the terms of the GNU Lesser General Public License as published 
14
// by the Free Software Foundation, either version 3 of the License, or     
15
// (at your option) any later version.                                      
16
//                                                                          
17
// This source file is distributed in the hope that it will be useful,      
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
20
// GNU General Public License for more details.                             
21
//                                                                          
22
// You should have received a copy of the GNU General Public License        
23
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
24
//
25
//
26
// This module returns the sigmoid of a number using a lookup table.
27
// -1.0 or +1.0 is returned for entries outside of the range -8.0 to +8.0
28
//                                                                          
29
// ToTo: check pipelining of values
30
// ============================================================================
31
 
32
`define ONE80                                   80'h3FFF0000000000000000
33
`define EIGHT80                         80'h40020000000000000000
34
`define FIVETWELVE80    80'h40080000000000000000
35
`define ONE64                                   64'h3FF0000000000000
36
`define EIGHT64                         64'h4020000000000000
37
`define FIVETWELVE64    64'h4080000000000000
38
`define ONE40                                   40'h3FE0000000
39
`define EIGHT40                         40'h4040000000
40
`define ONE32                                   32'h7F000000
41
`define EIGHT32                         32'h42000000
42
`define FIVETWELVE32    32'h48000000
43
 
44
module fpSigmoid(clk, ce, a, o);
45
parameter WID = 128;
46 26 robfinch
`include "fpSize.sv"
47 18 robfinch
input clk;
48
input ce;
49
input [WID-1:0] a;
50
output reg [WID-1:0] o;
51
 
52
wire [4:0] cmp1_o;
53
reg [4:0] cmp2_o;
54
 
55
// Just the mantissa is stored in the table to economize on the storate.
56
// The exponent is always the same value (0x3ff). Only the top 32 bits of
57
// the mantissa are stored.
58
(* ram_style="block" *)
59
reg [31:0] SigmoidLUT [0:1023];
60
 
61
// Check if the input is in the range (-8 to +8)
62
// We take the absolute value by trimming off the sign bit.
63
generate begin : ext
64
if (WID==80)
65
fp_cmp_unit #(WID) u1 (.a(a & 80'h7FFFFFFFFFFFFFFFFFFF), .b(`EIGHT80), .o(cmp1_o), .nanx() );
66
else if (WID==64)
67
fp_cmp_unit #(WID) u1 (.a(a & 64'h7FFFFFFFFFFFFFFF), .b(`EIGHT64), .o(cmp1_o), .nanx() );
68
else if (WID==40)
69
fp_cmp_unit #(WID) u1 (.a(a & 40'h7FFFFFFFFF), .b(`EIGHT40), .o(cmp1_o), .nanx() );
70
else if (WID==32)
71
fp_cmp_unit #(WID) u1 (.a(a & 32'h7FFFFFFF), .b(`EIGHT32), .o(cmp1_o), .nanx() );
72
else begin
73
        always @*
74
        begin
75
                $display("Sigmoid: unsupported width.");
76
                $stop;
77
        end
78
end
79
end
80
endgenerate
81
 
82
initial begin
83 26 robfinch
`include "D:\Cores6\nvio\v1\rtl\fpUnit\SigTbl.ver"
84 18 robfinch
end
85
 
86
// Quickly multiply number by 64 (it is in range -8 to 8) then convert to integer to get
87
// table index = add 6 to exponent then convert to integer
88
wire sa;
89
wire [EMSB:0] xa;
90
wire [FMSB:0] ma;
91
fpDecomp #(WID) u1 (.i(a), .sgn(sa), .exp(xa), .man(ma), .fract(), .xz(), .vz(), .xinf(), .inf(), .nan() );
92
 
93
reg [9:0] lutadr;
94
wire [5:0] lzcnt;
95
wire [WID-1:0] a1;
96
wire [WID-1:0] i1, i2;
97
wire [EMSB:0] xa1 = xa + 4'd6;
98
assign a1 = {sa,xa1,ma};        // we know the exponent won't overflow
99
wire [31:0] man32a = SigmoidLUT[lutadr];
100
wire [31:0] man32b = lutadr==10'h3ff ? man32a : SigmoidLUT[lutadr+1];
101
wire [31:0] man32;
102
wire [79:0] sig80;
103
generate begin : la
104
if (WID >= 40) begin
105
wire [15:0] eps = ma[FMSB-10:FMSB-10-15];
106
wire [47:0] p = (man32b - man32a) * eps;
107
assign man32 = man32a + (p >> 26);
108
cntlz32 u3 (man32,lzcnt);
109
end
110
else if (WID==32) begin
111
wire [12:0] eps = ma[FMSB-10:0];
112
wire [43:0] p = (man32b - man32a) * eps;
113
assign man32 = man32a + (p >> 26);
114
cntlz32 u3 (man32,lzcnt);
115
end
116
end
117
endgenerate
118
 
119
wire [31:0] man32s = man32 << (lzcnt + 2'd1);    // +1 to hide leading one
120
 
121
// Convert to integer
122
f2i #(WID) u2
123
(
124
  .clk(clk),
125
  .ce(1'b1),
126
  .i(a1),
127
  .o(i2)
128
);
129
assign i1 = i2 + 512;
130
 
131
always @(posedge clk)
132
  if (ce) cmp2_o <= cmp1_o;
133
 
134
// We know the integer is in range 0 to 1023
135
always @(posedge clk)
136
  if(ce) lutadr <= i1[9:0];
137
reg sa1,sa2;
138
always @(posedge clk)
139
if (ce) sa1 <= a[WID-1];
140
always @(posedge clk)
141
if (ce) sa2 <= sa1;
142
 
143
generate begin : ooo
144
if (WID==80) begin
145
wire [14:0] ex1 = 15'h3ffe - lzcnt;
146
always @(posedge clk)
147
if (ce) begin
148
        if (cmp2_o[1])  // abs(a) less than 8 ?
149
          o <= {1'b0,ex1,man32s[31:0],32'd0};
150
        else
151
          o <= sa1 ? 80'h0 : `ONE80;
152
end
153
end
154
else if (WID==64) begin
155
wire [10:0] ex1 = 11'h3fe - lzcnt;
156
always @(posedge clk)
157
if (ce) begin
158
        if (cmp2_o[1])  // abs(a) less than 8 ?
159
          o <= {1'b0,ex1,man32s[31:0],20'd0};
160
        else
161
          o <= sa1 ? 64'h0 : `ONE64;
162
end
163
end
164
else if (WID==40) begin
165
wire [9:0] ex1 = 10'h1fe - lzcnt;
166
always @(posedge clk)
167
if (ce) begin
168
        if (cmp2_o[1])  // abs(a) less than 8 ?
169
          o <= {1'b0,ex1,man32s[31:3]};
170
        else
171
          o <= sa1 ? 40'h0 : `ONE40;
172
end
173
end
174
else if (WID==32) begin
175
wire [7:0] ex1 = 8'h7e - lzcnt;
176
always @(posedge clk)
177
if (ce) begin
178
        if (cmp2_o[1])  // abs(a) less than 8 ?
179
          o <= {1'b0,ex1,man32s[31:9]};
180
        else
181
          o <= sa1 ? 32'h0 : `ONE32;
182
end
183
end
184
end
185
endgenerate
186
 
187
endmodule

powered by: WebSVN 2.1.0

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