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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [mult32x32.sv] - Blame information for rev 75

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

Line No. Rev Author Line
1 49 robfinch
// ============================================================================
2
//        __
3 72 robfinch
//   \\__/ o\    (C) 2020-2022  Robert Finch, Waterloo
4 49 robfinch
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      mult32x32.sv
9
//  - Karatsuba multiply
10
//  - six clock cycles
11
//
12
// BSD 3-Clause License
13
// Redistribution and use in source and binary forms, with or without
14
// modification, are permitted provided that the following conditions are met:
15
//
16
// 1. Redistributions of source code must retain the above copyright notice, this
17
//    list of conditions and the following disclaimer.
18
//
19
// 2. Redistributions in binary form must reproduce the above copyright notice,
20
//    this list of conditions and the following disclaimer in the documentation
21
//    and/or other materials provided with the distribution.
22
//
23
// 3. Neither the name of the copyright holder nor the names of its
24
//    contributors may be used to endorse or promote products derived from
25
//    this software without specific prior written permission.
26
//
27
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
31
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
//
38
// ============================================================================
39
 
40 72 robfinch
`define KARATSUBA       1
41
 
42
`ifdef KARATSUBA
43
 
44 49 robfinch
module mult32x32(clk, ce, a, b, o);
45
input clk;
46
input ce;
47
input [31:0] a;
48
input [31:0] b;
49
output reg [63:0] o;
50
 
51
reg [15:0] a2, b2;
52
reg [16:0] a1, b1;
53 73 robfinch
reg [31:0] z0, z2, z0a, z2a, z0b, z2b, z0c, z2c, z0d, z2d, p3;
54
reg [32:0] p4;
55 49 robfinch
reg [32:0] z1;  // extra bit for carry
56
reg sgn2, sgn3, sgn4;
57
 
58
always @(posedge clk)
59
        if (ce) a1 <= a[15: 0] - a[31:16];  // x0-x1
60
always @(posedge clk)
61
        if (ce) b1 <= b[31:16] - b[15: 0];  // y1-y0
62
always @(posedge clk)
63
        if (ce) a2 <= a1[16] ? -a1 : a1;
64
always @(posedge clk)
65
        if (ce) b2 <= b1[16] ? -b1 : b1;
66
always @(posedge clk)
67
  if (ce) sgn2 <= a1[16]^b1[16];
68
always @(posedge clk)
69
  if (ce) sgn3 <= sgn2;
70
always @(posedge clk)
71
  if (ce) sgn4 <= sgn3;
72
 
73
mult16x16 u1 (
74
  .clk(clk),
75
  .ce(ce),
76
  .a(a[31:16]),
77
  .b(b[31:16]),
78
  .o(z2)          // z2 = x1 * y1
79
);
80
 
81
mult16x16 u2 (
82
  .clk(clk),
83
  .ce(ce),
84
  .a(a[15:0]),
85
  .b(b[15:0]),
86
  .o(z0)          // z0 = x0 * y0
87
);
88
 
89
mult16x16 u3 (
90
  .clk(clk),
91
  .ce(ce),
92
  .a(a2[15:0]),
93
  .b(b2[15:0]),
94
  .o(p3)        // p3 = abs(x0-x1) * abs(y1-y0)
95
);
96
 
97
always @(posedge clk)
98
        if (ce) p4 <= sgn3 ? -p3 : p3;
99
 
100
always @(posedge clk)
101
  if (ce) z2a <= z2;
102
always @(posedge clk)
103
  if (ce) z0a <= z0;
104
always @(posedge clk)
105
  if (ce) z2b <= z2a;
106
always @(posedge clk)
107
  if (ce) z0b <= z0a;
108
always @(posedge clk)
109
  if (ce) z2c <= z2b;
110
always @(posedge clk)
111
  if (ce) z0c <= z0b;
112
always @(posedge clk)
113 73 robfinch
        if (ce) z1 <= {{32{p4[32]}},p4} + z2c + z0c;
114 49 robfinch
 
115
always @(posedge clk)
116
  if (ce) z2d <= z2c;
117
always @(posedge clk)
118
  if (ce) z0d <= z0c;
119
always @(posedge clk)
120
        if (ce) o <= {z2d,z0d} + {z1,16'd0};
121
 
122
endmodule
123 72 robfinch
 
124
`else
125
 
126
// This version of the multiply has a parameterized pipeline depth and allows
127
// the tools to perform the multiply. Relies on the ability of tools to retime.
128
 
129
module mult32x32(clk, ce, a, b, o);
130
parameter DEP = 6;
131
input clk;
132
input ce;
133
input [31:0] a;
134
input [31:0] b;
135
output reg [63:0] o;
136
 
137
reg [63:0] prod [0:DEP-1];
138
reg [63:0] prd;
139
integer n;
140
 
141
always_ff @(posedge clk)
142
        if (ce) prd <= a * b;
143
always_ff @(posedge clk)
144
        if (ce) prod[0] <= prd;
145
 
146
always_ff @(posedge clk)
147
        for (n = 0; n < DEP - 1; n = n + 1)
148
                if (ce) prod[n+1] <= prod[n];
149
 
150
always_ff @(posedge clk)
151
        if(ce) o <= prod[DEP-1];
152
 
153
endmodule
154
 
155
`endif

powered by: WebSVN 2.1.0

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