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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [mult32x32combo.sv] - Blame information for rev 79

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

Line No. Rev Author Line
1 74 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2020-2022  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      mult32x32combo.sv
9
//  - Karatsuba multiply
10
//  - combinational logic only
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
`define KARATSUBA       1
41
 
42
`ifdef KARATSUBA
43
 
44
module mult32x32combo(a, b, o);
45
input [31:0] a;
46
input [31:0] b;
47
output reg [63:0] o='d0;
48
 
49
reg [15:0] a2='d0, b2='d0;
50
reg [16:0] a1='d0, b1='d0;
51
reg [31:0] z0, z2, z0a, z2a, z0b, z2b, z0c, z2c, z0d, z2d, p3;
52
reg [32:0] p4;
53
reg [32:0] z1;  // extra bit for carry
54
reg sgn2, sgn3, sgn4;
55
 
56
always_comb
57
        a1 <= a[15: 0] - a[31:16];  // x0-x1
58
always_comb
59
        b1 <= b[31:16] - b[15: 0];  // y1-y0
60
always_comb
61
        a2 <= a1[16] ? -a1 : a1;
62
always_comb
63
        b2 <= b1[16] ? -b1 : b1;
64
always_comb
65
  sgn2 <= a1[16]^b1[16];
66
always_comb
67
  sgn3 <= sgn2;
68
always_comb
69
  sgn4 <= sgn3;
70
 
71
mult16x16combo u1 (
72
  .a(a[31:16]),
73
  .b(b[31:16]),
74
  .o(z2)          // z2 = x1 * y1
75
);
76
 
77
mult16x16combo u2 (
78
  .a(a[15:0]),
79
  .b(b[15:0]),
80
  .o(z0)          // z0 = x0 * y0
81
);
82
 
83
mult16x16combo u3 (
84
  .a(a2[15:0]),
85
  .b(b2[15:0]),
86
  .o(p3)        // p3 = abs(x0-x1) * abs(y1-y0)
87
);
88
 
89
always_comb
90
        p4 <= sgn3 ? -p3 : p3;
91
 
92
always_comb
93
  z2a <= z2;
94
always_comb
95
  z0a <= z0;
96
always_comb
97
  z2b <= z2a;
98
always_comb
99
  z0b <= z0a;
100
always_comb
101
  z2c <= z2b;
102
always_comb
103
  z0c <= z0b;
104
always_comb
105
        z1 <= {{32{p4[32]}},p4} + z2c + z0c;
106
 
107
always_comb
108
  z2d <= z2c;
109
always_comb
110
  z0d <= z0c;
111
always_comb
112
        o <= {z2d,z0d} + {z1,16'd0};
113
 
114
endmodule
115
 
116
`else
117
 
118
// This version of the multiply has a parameterized pipeline depth and allows
119
// the tools to perform the multiply. Relies on the ability of tools to retime.
120
 
121
module mult32x32combo(a, b, o);
122
parameter DEP = 6;
123
input [31:0] a;
124
input [31:0] b;
125
output reg [63:0] o;
126
 
127
reg [63:0] prod [0:DEP-1];
128
reg [63:0] prd;
129
integer n;
130
 
131
always_comb
132
        prd <= a * b;
133
always_comb
134
        prod[0] <= prd;
135
 
136
always_comb
137
        for (n = 0; n < DEP - 1; n = n + 1)
138
                prod[n+1] <= prod[n];
139
 
140
always_comb
141
        o <= prod[DEP-1];
142
 
143
endmodule
144
 
145
`endif

powered by: WebSVN 2.1.0

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