ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
extraUtilMacc.c
Go to the documentation of this file.
1
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <assert.h>
25
26#include "misc/vec/vec.h"
27#include "misc/vec/vecMem.h"
28#include "misc/extra/extra.h"
29#include "misc/util/utilTruth.h"
30
32
36
40
52void Macc_ConstMultSpecOne2( FILE * pFile, int n, int nBits, int nWidth )
53{
54 int nTotal = nWidth+nBits;
55 int Bound = 1 << (nBits-1);
56 assert( -Bound <= n && n < Bound );
57 fprintf( pFile, "// %d-bit multiplier-accumulator with constant %d generated by ABC on %s\n", nTotal, n, Extra_TimeStamp() );
58 fprintf( pFile, "module mulacc%03d%s (\n", Abc_AbsInt(n), n < 0 ? "_neg" : "_pos" );
59 fprintf( pFile, " input [%d:0] i,\n", nTotal-1 );
60 fprintf( pFile, " input [%d:0] s,\n", nTotal-1 );
61 fprintf( pFile, " output [%d:0] o\n", nTotal-1 );
62 fprintf( pFile, ");\n" );
63 fprintf( pFile, " wire [%d:0] c = %d\'h%x;\n", nTotal-1, nTotal, Abc_AbsInt(n) );
64 fprintf( pFile, " wire [%d:0] m = i * c;\n", nTotal-1 );
65 fprintf( pFile, " assign o = s %c m;\n", n < 0 ? '-' : '+' );
66 fprintf( pFile, "endmodule\n\n" );
67}
68void Macc_ConstMultSpecOne( FILE * pFile, int n, int nBits, int nWidth )
69{
70 int nTotal = nWidth+nBits;
71 int Bound = 1 << (nBits-1);
72 assert( -Bound <= n && n < Bound );
73 fprintf( pFile, "// %d-bit multiplier by %d-bit constant %d generated by ABC\n", nWidth, nBits, n );
74 fprintf( pFile, "module mul%03d%s (\n", Abc_AbsInt(n), n < 0 ? "_neg" : "_pos" );
75 fprintf( pFile, " input [%d:0] i,\n", nWidth-1 );
76 fprintf( pFile, " output [%d:0] o\n", nWidth-1 );
77 fprintf( pFile, ");\n" );
78 fprintf( pFile, " wire [%d:0] c = %d\'h%x;\n", nBits-1, nBits, Abc_AbsInt(n) );
79 fprintf( pFile, " wire [%d:0] I = {{%d{i[%d]}}, i};\n", nTotal-1, nBits, nWidth-1 );
80 fprintf( pFile, " wire [%d:0] m = I * c;\n", nTotal-1 );
81 fprintf( pFile, " wire [%d:0] t = %cm;\n", nTotal-1, n < 0 ? '-' : ' ' );
82 fprintf( pFile, " assign o = t[%d:%d];\n", nTotal-1, nBits );
83 fprintf( pFile, "endmodule\n\n" );
84}
86{
87 int nBits = 8;
88 int nWidth = 16;
89 int Bound = 1 << (nBits-1);
90 int i;
91 char Buffer[100];
92 FILE * pFile;
93 for ( i = -Bound; i < Bound; i++ )
94 {
95 sprintf( Buffer, "const_mul//macc_spec_%03d.v", 0xFF & i );
96 pFile = fopen( Buffer, "wb" );
97 Macc_ConstMultSpecOne2( pFile, i, nBits, nWidth );
98 fclose( pFile );
99 }
100}
101
113unsigned * Macc_ConstMultGenerate( int nBits )
114{
115 unsigned Mask = Abc_InfoMask( nBits );
116 Vec_Wec_t * vDivs = Vec_WecStart( 2*nBits );
117 Vec_Int_t * vOne, * vTwo, * vThree;
118 unsigned * pPlace = ABC_CALLOC( unsigned, (1 << nBits) );
119 int n, a, b, One, Two, i, k, New, Bound = 1 << (nBits-1);
120 // skip trivial
121 pPlace[0] = ~0;
122 pPlace[1] = ~0;
123 pPlace[Mask & (-1)] = ~0;
124 // skip div by 2
125 for ( i = 2; i < (1 << nBits); i++ )
126 if ( (i & 1) == 0 )
127 pPlace[i] = ~0;
128 // collect trivial
129 printf( "Generating numbers using %d adders:\n", 0 );
130 for ( i = 0; i < nBits; i++ )
131 {
132 Vec_WecPush( vDivs, 0, 1<<i );
133 printf( "%d = %d << %d\n", 1<<i, 1, i );
134 }
135 // generate for each adder count
136 for ( n = 1; n < nBits; n++ )
137 {
138 // quit if all of them are finished
139 for ( a = 1; a < (1 << nBits); a++ )
140 if ( pPlace[a] == 0 )
141 break;
142 if ( a == (1 << nBits) )
143 break;
144
145 printf( "Generating numbers using %d adders:\n", n );
146 vThree = Vec_WecEntry( vDivs, n );
147 for ( a = 0; a < nBits; a++ )
148 for ( b = 0; b < nBits; b++ )
149 {
150 if ( a + b != n-1 )
151 continue;
152 vOne = Vec_WecEntry( vDivs, a );
153 vTwo = Vec_WecEntry( vDivs, b );
154 Vec_IntForEachEntry( vOne, One, i )
155 Vec_IntForEachEntry( vTwo, Two, k )
156 {
157 New = One + Two;
158 if ( New >= -Bound && New < Bound && pPlace[Mask & New] == 0 )
159 {
160 if ( New > 0 )
161 Vec_IntPush( vThree, New );
162
163 pPlace[Mask & New] = (One << 16) | Two;
164 printf( "%d = %d + %d\n", New, One, Two );
165 }
166 New = One - Two;
167 if ( New >= -Bound && New < Bound && pPlace[Mask & New] == 0 )
168 {
169 if ( New > 0 )
170 Vec_IntPush( vThree, New );
171
172 pPlace[Mask & New] = (One << 16) | Two | (1 << 15);
173 printf( "%d = %d - %d\n", New, One, Two );
174 }
175 New = Two - One;
176 if ( New >= -Bound && New < Bound && pPlace[Mask & New] == 0 )
177 {
178 if ( New > 0 )
179 Vec_IntPush( vThree, New );
180
181 pPlace[Mask & New] = (Two << 16) | One | (1 << 15);
182 printf( "%d = %d - %d\n", New, Two, One );
183 }
184 }
185 printf( "Adding one incrementor:\n" );
186 Vec_IntForEachEntry( vThree, One, i )
187 {
188 if ( One < 0 )
189 continue;
190 New = -One;
191 if ( pPlace[Mask & New] == 0 )
192 {
193 pPlace[Mask & New] = One << 16;
194 printf( "-%d = ~%d + 1\n", One, One );
195 }
196 }
197 }
198 }
199 Vec_WecPrint( vDivs, 0 );
200 Vec_WecFree( vDivs );
201 //ABC_FREE( pPlace );
202 return pPlace;
203}
205{
206 int nBits = 8;
207 unsigned * p = Macc_ConstMultGenerate( nBits );
208 ABC_FREE( p );
209}
210
211void Macc_ConstMultGenOne_rec( FILE * pFile, unsigned * p, int n, int nBits, int nWidth )
212{
213 unsigned Mask = Abc_InfoMask( nBits );
214 unsigned New = Mask & (unsigned)n;
215 int nTotal = nWidth+nBits;
216 int One = p[New] >> 16;
217 int Two = p[New] & 0x7FFF;
218 char Sign = n < 0 ? 'N' : 'n';
219 char Oper = ((p[New] >> 15) & 1) ? '-' : '+';
220 if ( p[New] == ~0 )
221 {
222 // count trailing zeros
223 int nn, nZeros;
224 for ( nZeros = 0; nZeros < nBits; nZeros++ )
225 if ( (n >> nZeros) & 1 )
226 break;
227 nn = n >> nZeros;
228 if ( nn == -1 )
229 fprintf( pFile, " wire [%d:0] N1 = -n1;\n", nTotal-1 );
230 if ( Abc_AbsInt(nn) != 1 )
231 Macc_ConstMultGenOne_rec( pFile, p, nn, nBits, nWidth );
232 if ( nZeros > 0 )
233 fprintf( pFile, " wire [%d:0] %c%d = %c%d << %d;\n", nTotal-1, Sign, Abc_AbsInt(n), Sign, Abc_AbsInt(nn), nZeros );
234 }
235 else if ( One && Two ) // add/sub
236 {
237 Macc_ConstMultGenOne_rec( pFile, p, One, nBits, nWidth );
238 Macc_ConstMultGenOne_rec( pFile, p, Two, nBits, nWidth );
239 fprintf( pFile, " wire [%d:0] %c%d = n%d %c n%d;\n", nTotal-1, Sign, Abc_AbsInt(n), One, Oper, Two );
240 }
241 else if ( Two == 0 ) // minus
242 {
243 Macc_ConstMultGenOne_rec( pFile, p, One, nBits, nWidth );
244 fprintf( pFile, " wire [%d:0] N%d = -n%d;\n", nTotal-1, One, One );
245 }
246}
247void Macc_ConstMultGenMult( FILE * pFile, unsigned * p, int n, int nBits, int nWidth )
248{
249 int nTotal = nWidth+nBits;
250 int Bound = 1 << (nBits-1);
251 char Sign = n < 0 ? 'N' : 'n';
252 assert( -Bound <= n && n < Bound );
253 fprintf( pFile, "// %d-bit multiplier by %d-bit constant %d generated by ABC\n", nWidth, nBits, n );
254 fprintf( pFile, "module mul%03d%s (\n", Abc_AbsInt(n), n < 0 ? "_neg" : "_pos" );
255 fprintf( pFile, " input [%d:0] i,\n", nWidth-1 );
256 fprintf( pFile, " output [%d:0] o\n", nWidth-1 );
257 fprintf( pFile, ");\n" );
258 if ( n == 0 )
259 fprintf( pFile, " assign o = %d\'h0;\n", nWidth );
260 else
261 {
262 fprintf( pFile, " wire [%d:0] n1 = {{%d{i[%d]}}, i};\n", nTotal-1, nBits, nWidth-1 );
263 Macc_ConstMultGenOne_rec( pFile, p, n, nBits, nWidth );
264 fprintf( pFile, " assign o = %c%d[%d:%d];\n", Sign, Abc_AbsInt(n), nTotal-1, nBits );
265 }
266 fprintf( pFile, "endmodule\n\n" );
267}
268void Macc_ConstMultGenMacc( FILE * pFile, unsigned * p, int n, int nBits, int nWidth )
269{
270 int nTotal = nWidth+nBits;
271 int Bound = 1 << (nBits-1);
272 char Sign = n < 0 ? 'N' : 'n';
273 assert( -Bound <= n && n < Bound );
274 fprintf( pFile, "// %d-bit multiplier-accumulator by %d-bit constant %d generated by ABC\n", nWidth, nBits, n );
275 fprintf( pFile, "module macc%03d%s (\n", Abc_AbsInt(n), n < 0 ? "_neg" : "_pos" );
276 fprintf( pFile, " input [%d:0] i,\n", nWidth-1 );
277 fprintf( pFile, " input [%d:0] c,\n", nWidth-1 );
278 fprintf( pFile, " output [%d:0] o\n", nWidth-1 );
279 fprintf( pFile, ");\n" );
280 if ( n == 0 )
281 fprintf( pFile, " assign o = c;\n" );
282 else
283 {
284 fprintf( pFile, " wire [%d:0] n1 = {{%d{i[%d]}}, i};\n", nTotal-1, nBits, nWidth-1 );
285 Macc_ConstMultGenOne_rec( pFile, p, n, nBits, nWidth );
286 fprintf( pFile, " wire [%d:0] s = %c%d[%d:%d];\n", nWidth-1, Sign, Abc_AbsInt(n), nTotal-1, nBits );
287 fprintf( pFile, " assign o = s + c;\n" );
288 }
289 fprintf( pFile, "endmodule\n\n" );
290}
291void Macc_ConstMultGenMacc2( FILE * pFile, unsigned * p, int n, int nBits, int nWidth )
292{
293 int nTotal = nWidth+nBits;
294 int Bound = 1 << (nBits-1);
295 char Sign = n < 0 ? 'N' : 'n';
296 assert( -Bound <= n && n < Bound );
297 fprintf( pFile, "// %d-bit multiplier-accumulator by constant %d generated by ABC on %s\n", nTotal, n, Extra_TimeStamp() );
298 fprintf( pFile, "module macc%03d%s (\n", Abc_AbsInt(n), n < 0 ? "_neg" : "_pos" );
299 fprintf( pFile, " input [%d:0] i,\n", nTotal-1 );
300 fprintf( pFile, " input [%d:0] s,\n", nTotal-1 );
301 fprintf( pFile, " output [%d:0] o\n", nTotal-1 );
302 fprintf( pFile, ");\n" );
303 if ( n == 0 )
304 fprintf( pFile, " assign o = s;\n" );
305 else
306 {
307 fprintf( pFile, " wire [%d:0] n1 = i;\n", nTotal-1 );
308 Macc_ConstMultGenOne_rec( pFile, p, n, nBits, nWidth );
309 fprintf( pFile, " assign o = s + %c%d;\n", Sign, Abc_AbsInt(n) );
310 }
311 fprintf( pFile, "endmodule\n\n" );
312}
314{
315 int nBits = 8;
316 int nWidth = 16;
317 int Bound = 1 << (nBits-1);
318 unsigned * p = Macc_ConstMultGenerate( nBits );
319 int i;
320 char Buffer[100];
321 FILE * pFile;
322 for ( i = -Bound; i < Bound; i++ )
323 {
324 sprintf( Buffer, "const_mul//macc%03d.v", 0xFF & i );
325 pFile = fopen( Buffer, "wb" );
326 Macc_ConstMultGenMacc2( pFile, p, i, nBits, nWidth );
327 fclose( pFile );
328 }
329 ABC_FREE( p );
330}
331
335
336
338
#define ABC_CALLOC(type, num)
Definition abc_global.h:265
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
int nTotal
DECLARATIONS ///.
Definition cutTruth.c:37
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
Definition bblif.c:37
Cube * p
Definition exorList.c:222
void Macc_ConstMultGenTest()
void Macc_ConstMultGenMult(FILE *pFile, unsigned *p, int n, int nBits, int nWidth)
void Macc_ConstMultSpecTest()
void Macc_ConstMultGenTest0()
void Macc_ConstMultGenOne_rec(FILE *pFile, unsigned *p, int n, int nBits, int nWidth)
void Macc_ConstMultGenMacc(FILE *pFile, unsigned *p, int n, int nBits, int nWidth)
void Macc_ConstMultSpecOne(FILE *pFile, int n, int nBits, int nWidth)
unsigned * Macc_ConstMultGenerate(int nBits)
ABC_NAMESPACE_IMPL_START void Macc_ConstMultSpecOne2(FILE *pFile, int n, int nBits, int nWidth)
DECLARATIONS ///.
void Macc_ConstMultGenMacc2(FILE *pFile, unsigned *p, int n, int nBits, int nWidth)
char * Extra_TimeStamp()
#define assert(ex)
Definition util_old.h:213
char * sprintf()
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
Definition vecInt.h:54
typedefABC_NAMESPACE_HEADER_START struct Vec_Wec_t_ Vec_Wec_t
INCLUDES ///.
Definition vecWec.h:42