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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [mult128x128.sv] - Blame information for rev 90

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

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

powered by: WebSVN 2.1.0

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