1 |
6 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2007-2016 Robert Finch, Stratford
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch<remove>@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
9 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
10 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
11 |
|
|
// (at your option) any later version.
|
12 |
|
|
//
|
13 |
|
|
// This source file is distributed in the hope that it will be useful,
|
14 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
|
|
// GNU General Public License for more details.
|
17 |
|
|
//
|
18 |
|
|
// You should have received a copy of the GNU General Public License
|
19 |
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
20 |
|
|
//
|
21 |
|
|
// fpCompare.v
|
22 |
|
|
// - floating point comparison unit
|
23 |
|
|
// - parameterized width
|
24 |
|
|
// - IEEE 754 representation
|
25 |
|
|
//
|
26 |
|
|
// Compares two fgloating point numbers and returns a status output.
|
27 |
|
|
// Bit:
|
28 |
|
|
// 0: 1 = equal, 0=not equal
|
29 |
|
|
// 1: 1 = less than,
|
30 |
|
|
// 2: 1 = magnitude less than
|
31 |
|
|
// 3: 1 = unordered (nan in compare)
|
32 |
|
|
// ============================================================================
|
33 |
|
|
//
|
34 |
|
|
module fpCompare(a, b, o, nanx);
|
35 |
|
|
parameter WID = 32;
|
36 |
|
|
localparam MSB = WID-1;
|
37 |
|
|
localparam EMSB = WID==80 ? 14 :
|
38 |
|
|
WID==64 ? 10 :
|
39 |
|
|
WID==52 ? 10 :
|
40 |
|
|
WID==48 ? 10 :
|
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==80 ? 63 :
|
47 |
|
|
WID==64 ? 51 :
|
48 |
|
|
WID==52 ? 39 :
|
49 |
|
|
WID==48 ? 35 :
|
50 |
|
|
WID==44 ? 31 :
|
51 |
|
|
WID==42 ? 29 :
|
52 |
|
|
WID==40 ? 28 :
|
53 |
|
|
WID==32 ? 22 :
|
54 |
|
|
WID==24 ? 15 : 9;
|
55 |
|
|
|
56 |
|
|
input [WID-1:0] a, b;
|
57 |
|
|
output [3:0] o;
|
58 |
|
|
reg [3:0] o;
|
59 |
|
|
output nanx;
|
60 |
|
|
|
61 |
|
|
// Decompose the operands
|
62 |
|
|
wire sa;
|
63 |
|
|
wire sb;
|
64 |
|
|
wire [EMSB:0] xa;
|
65 |
|
|
wire [EMSB:0] xb;
|
66 |
|
|
wire [FMSB:0] ma;
|
67 |
|
|
wire [FMSB:0] mb;
|
68 |
|
|
wire az, bz;
|
69 |
|
|
wire nan_a, nan_b;
|
70 |
|
|
|
71 |
|
|
fpDecompose #(WID) u1(.i(a), .sgn(sa), .exp(xa), .man(ma), .fract(), .xz(), .mz(), .vz(az), .inf(), .xinf(), .qnan(), .snan(), .nan(nan_a) );
|
72 |
|
|
fpDecompose #(WID) u2(.i(b), .sgn(sb), .exp(xb), .man(mb), .fract(), .xz(), .mz(), .vz(bz), .inf(), .xinf(), .qnan(), .snan(), .nan(nan_b) );
|
73 |
|
|
|
74 |
|
|
wire unordered = nan_a | nan_b;
|
75 |
|
|
|
76 |
|
|
wire eq = (az & bz) || (a==b); // special test for zero
|
77 |
|
|
wire gt1 = {xa,ma} > {xb,mb};
|
78 |
|
|
wire lt1 = {xa,ma} < {xb,mb};
|
79 |
|
|
|
80 |
|
|
wire lt = sa ^ sb ? sa & !(az & bz): sa ? gt1 : lt1;
|
81 |
|
|
|
82 |
|
|
always @(unordered or eq or lt)
|
83 |
|
|
begin
|
84 |
|
|
o[0] = eq;
|
85 |
|
|
o[1] = lt;
|
86 |
|
|
o[2] = lt1;
|
87 |
|
|
o[3] = unordered;
|
88 |
|
|
end
|
89 |
|
|
|
90 |
|
|
// an unorder comparison will signal a nan exception
|
91 |
|
|
//assign nanx = op!=`FCOR && op!=`FCUN && unordered;
|
92 |
|
|
assign nanx = 1'b0;
|
93 |
|
|
|
94 |
|
|
endmodule
|
95 |
|
|
|
96 |
|
|
module fpCompare_tb();
|
97 |
|
|
|
98 |
|
|
wire [3:0] o1,o2,o3,o4;
|
99 |
|
|
|
100 |
|
|
fpCompare #(32) u1 (.a(32'h80000000), .b(32'h00000000), .o(o1), .nanx() ); // -0 to +0
|
101 |
|
|
fpCompare #(32) u2 (.a(32'h3F800000), .b(32'hBF800000), .o(o2), .nanx() ); // 1 to -1
|
102 |
|
|
fpCompare #(32) u3 (.a(32'hC1200000), .b(32'h41C80000), .o(o3), .nanx() ); // -10 to 25
|
103 |
|
|
fpCompare #(32) u4 (.a(32'h42C80000), .b(32'h43520000), .o(o4), .nanx() ); // 100 to 210
|
104 |
|
|
|
105 |
|
|
endmodule
|