ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
fpgaCut.c
Go to the documentation of this file.
1
18
19#include "fpgaInt.h"
20
22
23
27
30{
31 Fpga_Cut_t ** pBins; // the table used for linear probing
32 int nBins; // the size of the table
33 int * pCuts; // the array of cuts currently stored
34 int nCuts; // the number of cuts currently stored
35 Fpga_Cut_t ** pArray; // the temporary array of cuts
36 Fpga_Cut_t ** pCuts1; // the temporary array of cuts
37 Fpga_Cut_t ** pCuts2; // the temporary array of cuts
38};
39
40// the largest number of cuts considered
41//#define FPGA_CUTS_MAX_COMPUTE 500
42#define FPGA_CUTS_MAX_COMPUTE 2000
43// the largest number of cuts used
44//#define FPGA_CUTS_MAX_USE 200
45#define FPGA_CUTS_MAX_USE 1000
46
47// primes used to compute the hash key
48static int s_HashPrimes[10] = { 109, 499, 557, 619, 631, 709, 797, 881, 907, 991 };
49
50static int bit_count[256] = {
51 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
52 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
53 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
54 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
55 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
56 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
57 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
58 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
59};
60
61#define FPGA_COUNT_ONES(u) (bit_count[(u)&255]+bit_count[((u)>>8)&255]+bit_count[((u)>>16)&255]+bit_count[(u)>>24])
62
63static Fpga_Cut_t * Fpga_CutCompute( Fpga_Man_t * p, Fpga_CutTable_t * pTable, Fpga_Node_t * pNode );
64static void Fpga_CutFilter( Fpga_Man_t * p, Fpga_Node_t * pNode );
65static Fpga_Cut_t * Fpga_CutMergeLists( Fpga_Man_t * p, Fpga_CutTable_t * pTable, Fpga_Cut_t * pList1, Fpga_Cut_t * pList2, int fComp1, int fComp2, int fPivot1, int fPivot2 );
66static int Fpga_CutMergeTwo( Fpga_Cut_t * pCut1, Fpga_Cut_t * pCut2, Fpga_Node_t * ppNodes[], int nNodesMax );
67static Fpga_Cut_t * Fpga_CutUnionLists( Fpga_Cut_t * pList1, Fpga_Cut_t * pList2 );
68static int Fpga_CutBelongsToList( Fpga_Cut_t * pList, Fpga_Node_t * ppNodes[], int nNodes );
70extern int Fpga_CutCountAll( Fpga_Man_t * pMan );
71
72static void Fpga_CutListPrint( Fpga_Man_t * pMan, Fpga_Node_t * pRoot );
73static void Fpga_CutListPrint2( Fpga_Man_t * pMan, Fpga_Node_t * pRoot );
74static void Fpga_CutPrint_( Fpga_Man_t * pMan, Fpga_Cut_t * pCut, Fpga_Node_t * pRoot );
75
76static Fpga_CutTable_t * Fpga_CutTableStart( Fpga_Man_t * pMan );
77static void Fpga_CutTableStop( Fpga_CutTable_t * p );
78static unsigned Fpga_CutTableHash( Fpga_Node_t * ppNodes[], int nNodes );
79static int Fpga_CutTableLookup( Fpga_CutTable_t * p, Fpga_Node_t * ppNodes[], int nNodes );
80static Fpga_Cut_t * Fpga_CutTableConsider( Fpga_Man_t * pMan, Fpga_CutTable_t * p, Fpga_Node_t * ppNodes[], int nNodes );
81static void Fpga_CutTableRestart( Fpga_CutTable_t * p );
82
83static int Fpga_CutSortCutsCompare( Fpga_Cut_t ** pC1, Fpga_Cut_t ** pC2 );
84static Fpga_Cut_t * Fpga_CutSortCuts( Fpga_Man_t * pMan, Fpga_CutTable_t * p, Fpga_Cut_t * pList );
85static int Fpga_CutList2Array( Fpga_Cut_t ** pArray, Fpga_Cut_t * pList );
86static Fpga_Cut_t * Fpga_CutArray2List( Fpga_Cut_t ** pArray, int nCuts );
87
88
89// iterator through all the cuts of the list
90#define Fpga_ListForEachCut( pList, pCut ) \
91 for ( pCut = pList; \
92 pCut; \
93 pCut = pCut->pNext )
94#define Fpga_ListForEachCutSafe( pList, pCut, pCut2 ) \
95 for ( pCut = pList, \
96 pCut2 = pCut? pCut->pNext: NULL; \
97 pCut; \
98 pCut = pCut2, \
99 pCut2 = pCut? pCut->pNext: NULL )
100
101
105
131{
132 ProgressBar * pProgress;
133 Fpga_CutTable_t * pTable;
134 Fpga_Node_t * pNode;
135 int nCuts, nNodes, i;
136 clock_t clk = clock();
137
138 // set the elementary cuts for the PI variables
139 assert( p->nVarsMax > 1 && p->nVarsMax < 11 );
141
142 // compute the cuts for the internal nodes
143 nNodes = p->vAnds->nSize;
144 pProgress = Extra_ProgressBarStart( stdout, nNodes );
145 pTable = Fpga_CutTableStart( p );
146 for ( i = 0; i < nNodes; i++ )
147 {
148 Extra_ProgressBarUpdate( pProgress, i, "Cuts ..." );
149 pNode = p->vAnds->pArray[i];
150 if ( !Fpga_NodeIsAnd( pNode ) )
151 continue;
152 Fpga_CutCompute( p, pTable, pNode );
153 }
154 Extra_ProgressBarStop( pProgress );
155 Fpga_CutTableStop( pTable );
156
157 // report the stats
158 if ( p->fVerbose )
159 {
160 nCuts = Fpga_CutCountAll(p);
161 printf( "Nodes = %6d. Total %d-cuts = %d. Cuts per node = %.1f. ",
162 p->nNodes, p->nVarsMax, nCuts, ((float)nCuts)/p->nNodes );
163 ABC_PRT( "Time", clock() - clk );
164 }
165
166 // print the cuts for the first primary output
167// Fpga_CutListPrint( p, Fpga_Regular(p->pOutputs[0]) );
168}
169
182{
183 Fpga_Cut_t * pCut;
184 int i;
185
186 // set the elementary cuts for the PI variables
187 for ( i = 0; i < p->nInputs; i++ )
188 {
189 pCut = Fpga_CutAlloc( p );
190 pCut->nLeaves = 1;
191 pCut->ppLeaves[0] = p->pInputs[i];
192 pCut->uSign = (1 << (i%31));
193 p->pInputs[i]->pCuts = pCut;
194 p->pInputs[i]->pCutBest = pCut;
195 // set the input arrival times
196// p->pInputs[i]->pCut[1]->tArrival = p->pInputArrivals[i];
197 }
198}
199
211Fpga_Cut_t * Fpga_CutCompute( Fpga_Man_t * p, Fpga_CutTable_t * pTable, Fpga_Node_t * pNode )
212{
213 Fpga_Node_t * pTemp;
214 Fpga_Cut_t * pList, * pList1, * pList2;
215 Fpga_Cut_t * pCut;
216 int fTree = 0;
217 int fPivot1 = fTree && (Fpga_NodeReadRef(pNode->p1)>2);
218 int fPivot2 = fTree && (Fpga_NodeReadRef(pNode->p2)>2);
219
220 // if the cuts are computed return them
221 if ( pNode->pCuts )
222 return pNode->pCuts;
223
224 // compute the cuts for the children
225 pList1 = Fpga_Regular(pNode->p1)->pCuts;
226 pList2 = Fpga_Regular(pNode->p2)->pCuts;
227 // merge the lists
228 pList = Fpga_CutMergeLists( p, pTable, pList1, pList2,
229 Fpga_IsComplement(pNode->p1), Fpga_IsComplement(pNode->p2),
230 fPivot1, fPivot2 );
231 // if there are functionally equivalent nodes, union them with this list
232 assert( pList );
233 // only add to the list of cuts if the node is a representative one
234 if ( pNode->pRepr == NULL )
235 {
236 for ( pTemp = pNode->pNextE; pTemp; pTemp = pTemp->pNextE )
237 {
238 assert( pTemp->pCuts );
239 pList = Fpga_CutUnionLists( pList, pTemp->pCuts );
240 assert( pTemp->pCuts );
241 pList = Fpga_CutSortCuts( p, pTable, pList );
242 }
243 }
244 // add the new cut
245 pCut = Fpga_CutAlloc( p );
246 pCut->nLeaves = 1;
247 pCut->ppLeaves[0] = pNode;
248 pCut->uSign = (1 << (pNode->Num%31));
249 pCut->fLevel = (float)pCut->ppLeaves[0]->Level;
250 // append (it is important that the elementary cut is appended first)
251 pCut->pNext = pList;
252 // set at the node
253 pNode->pCuts = pCut;
254 // remove the dominated cuts
255// Fpga_CutFilter( p, pNode );
256 // set the phase correctly
257 if ( pNode->pRepr && Fpga_NodeComparePhase(pNode, pNode->pRepr) )
258 {
259 Fpga_ListForEachCut( pNode->pCuts, pCut )
260 pCut->Phase = 1;
261 }
262
263
264/*
265 {
266 Fpga_Cut_t * pPrev;
267 int i, Counter = 0;
268 for ( pCut = pNode->pCuts->pNext, pPrev = pNode->pCuts; pCut; pCut = pCut->pNext )
269 {
270 for ( i = 0; i < pCut->nLeaves; i++ )
271 if ( pCut->ppLeaves[i]->Level >= pNode->Level )
272 break;
273 if ( i != pCut->nLeaves )
274 pPrev->pNext = pCut->pNext;
275 else
276 pPrev = pCut;
277 }
278 }
279 {
280 int i, Counter = 0;;
281 for ( pCut = pNode->pCuts->pNext; pCut; pCut = pCut->pNext )
282 for ( i = 0; i < pCut->nLeaves; i++ )
283 Counter += (pCut->ppLeaves[i]->Level >= pNode->Level);
284 if ( Counter )
285 printf( " %d", Counter );
286 }
287*/
288
289 return pCut;
290}
291
303void Fpga_CutFilter( Fpga_Man_t * p, Fpga_Node_t * pNode )
304{
305 Fpga_Cut_t * pTemp, * pPrev, * pCut, * pCut2;
306 int i, k, Counter;
307
308 Counter = 0;
309 pPrev = pNode->pCuts;
310 Fpga_ListForEachCutSafe( pNode->pCuts->pNext, pCut, pCut2 )
311 {
312 // go through all the previous cuts up to pCut
313 for ( pTemp = pNode->pCuts->pNext; pTemp != pCut; pTemp = pTemp->pNext )
314 {
315 // check if every node in pTemp is contained in pCut
316 for ( i = 0; i < pTemp->nLeaves; i++ )
317 {
318 for ( k = 0; k < pCut->nLeaves; k++ )
319 if ( pTemp->ppLeaves[i] == pCut->ppLeaves[k] )
320 break;
321 if ( k == pCut->nLeaves ) // node i in pTemp is not contained in pCut
322 break;
323 }
324 if ( i == pTemp->nLeaves ) // every node in pTemp is contained in pCut
325 {
326 Counter++;
327 break;
328 }
329 }
330 if ( pTemp != pCut ) // pTemp contain pCut
331 {
332 pPrev->pNext = pCut->pNext; // skip pCut
333 // recycle pCut
334 Fpga_CutFree( p, pCut );
335 }
336 else
337 pPrev = pCut;
338 }
339// printf( "Dominated = %3d. \n", Counter );
340}
341
342
354Fpga_Cut_t * Fpga_CutMergeLists( Fpga_Man_t * p, Fpga_CutTable_t * pTable,
355 Fpga_Cut_t * pList1, Fpga_Cut_t * pList2, int fComp1, int fComp2, int fPivot1, int fPivot2 )
356{
357 Fpga_Node_t * ppNodes[FPGA_MAX_LEAVES];
358 Fpga_Cut_t * pListNew, ** ppListNew, * pLists[FPGA_MAX_LEAVES+1] = { NULL };
359 Fpga_Cut_t * pCut, * pPrev, * pTemp1, * pTemp2;
360 int nNodes, Counter, i;
361 Fpga_Cut_t ** ppArray1, ** ppArray2, ** ppArray3;
362 int nCuts1, nCuts2, nCuts3, k, fComp3;
363
364 ppArray1 = pTable->pCuts1;
365 ppArray2 = pTable->pCuts2;
366 nCuts1 = Fpga_CutList2Array( ppArray1, pList1 );
367 nCuts2 = Fpga_CutList2Array( ppArray2, pList2 );
368 if ( fPivot1 )
369 nCuts1 = 1;
370 if ( fPivot2 )
371 nCuts2 = 1;
372 // swap the lists based on their length
373 if ( nCuts1 > nCuts2 )
374 {
375 ppArray3 = ppArray1;
376 ppArray1 = ppArray2;
377 ppArray2 = ppArray3;
378
379 nCuts3 = nCuts1;
380 nCuts1 = nCuts2;
381 nCuts2 = nCuts3;
382
383 fComp3 = fComp1;
384 fComp1 = fComp2;
385 fComp2 = fComp3;
386 }
387 // pList1 is shorter or equal length compared to pList2
388
389 // prepare the manager for the cut computation
390 Fpga_CutTableRestart( pTable );
391 // go through the cut pairs
392 Counter = 0;
393// for ( pTemp1 = pList1; pTemp1; pTemp1 = fPivot1? NULL: pTemp1->pNext )
394// for ( pTemp2 = pList2; pTemp2; pTemp2 = fPivot2? NULL: pTemp2->pNext )
395 for ( i = 0; i < nCuts1; i++ )
396 {
397 for ( k = 0; k <= i; k++ )
398 {
399 pTemp1 = ppArray1[i];
400 pTemp2 = ppArray2[k];
401
402 if ( pTemp1->nLeaves == p->nVarsMax && pTemp2->nLeaves == p->nVarsMax )
403 {
404 if ( pTemp1->ppLeaves[0] != pTemp2->ppLeaves[0] )
405 continue;
406 if ( pTemp1->ppLeaves[1] != pTemp2->ppLeaves[1] )
407 continue;
408 }
409
410 // check if k-feasible cut exists
411 nNodes = Fpga_CutMergeTwo( pTemp1, pTemp2, ppNodes, p->nVarsMax );
412 if ( nNodes == 0 )
413 continue;
414 // consider the cut for possible addition to the set of new cuts
415 pCut = Fpga_CutTableConsider( p, pTable, ppNodes, nNodes );
416 if ( pCut == NULL )
417 continue;
418 // add data to the cut
419 pCut->pOne = Fpga_CutNotCond( pTemp1, fComp1 );
420 pCut->pTwo = Fpga_CutNotCond( pTemp2, fComp2 );
421 // create the signature
422 pCut->uSign = pTemp1->uSign | pTemp2->uSign;
423 // add it to the corresponding list
424 pCut->pNext = pLists[(int)pCut->nLeaves];
425 pLists[(int)pCut->nLeaves] = pCut;
426 // count this cut and quit if limit is reached
427 Counter++;
428 if ( Counter == FPGA_CUTS_MAX_COMPUTE )
429 goto QUITS;
430 }
431 for ( k = 0; k < i; k++ )
432 {
433 pTemp1 = ppArray1[k];
434 pTemp2 = ppArray2[i];
435
436 if ( pTemp1->nLeaves == p->nVarsMax && pTemp2->nLeaves == p->nVarsMax )
437 {
438 if ( pTemp1->ppLeaves[0] != pTemp2->ppLeaves[0] )
439 continue;
440 if ( pTemp1->ppLeaves[1] != pTemp2->ppLeaves[1] )
441 continue;
442 }
443
444
445 // check if k-feasible cut exists
446 nNodes = Fpga_CutMergeTwo( pTemp1, pTemp2, ppNodes, p->nVarsMax );
447 if ( nNodes == 0 )
448 continue;
449 // consider the cut for possible addition to the set of new cuts
450 pCut = Fpga_CutTableConsider( p, pTable, ppNodes, nNodes );
451 if ( pCut == NULL )
452 continue;
453 // add data to the cut
454 pCut->pOne = Fpga_CutNotCond( pTemp1, fComp1 );
455 pCut->pTwo = Fpga_CutNotCond( pTemp2, fComp2 );
456 // create the signature
457 pCut->uSign = pTemp1->uSign | pTemp2->uSign;
458 // add it to the corresponding list
459 pCut->pNext = pLists[(int)pCut->nLeaves];
460 pLists[(int)pCut->nLeaves] = pCut;
461 // count this cut and quit if limit is reached
462 Counter++;
463 if ( Counter == FPGA_CUTS_MAX_COMPUTE )
464 goto QUITS;
465 }
466 }
467 // consider the rest of them
468 for ( i = nCuts1; i < nCuts2; i++ )
469 for ( k = 0; k < nCuts1; k++ )
470 {
471 pTemp1 = ppArray1[k];
472 pTemp2 = ppArray2[i];
473
474 if ( pTemp1->nLeaves == p->nVarsMax && pTemp2->nLeaves == p->nVarsMax )
475 {
476 if ( pTemp1->ppLeaves[0] != pTemp2->ppLeaves[0] )
477 continue;
478 if ( pTemp1->ppLeaves[1] != pTemp2->ppLeaves[1] )
479 continue;
480 if ( pTemp1->ppLeaves[2] != pTemp2->ppLeaves[2] )
481 continue;
482 }
483
484
485 // check if k-feasible cut exists
486 nNodes = Fpga_CutMergeTwo( pTemp1, pTemp2, ppNodes, p->nVarsMax );
487 if ( nNodes == 0 )
488 continue;
489 // consider the cut for possible addition to the set of new cuts
490 pCut = Fpga_CutTableConsider( p, pTable, ppNodes, nNodes );
491 if ( pCut == NULL )
492 continue;
493 // add data to the cut
494 pCut->pOne = Fpga_CutNotCond( pTemp1, fComp1 );
495 pCut->pTwo = Fpga_CutNotCond( pTemp2, fComp2 );
496 // create the signature
497 pCut->uSign = pTemp1->uSign | pTemp2->uSign;
498 // add it to the corresponding list
499 pCut->pNext = pLists[(int)pCut->nLeaves];
500 pLists[(int)pCut->nLeaves] = pCut;
501 // count this cut and quit if limit is reached
502 Counter++;
503 if ( Counter == FPGA_CUTS_MAX_COMPUTE )
504 goto QUITS;
505 }
506QUITS :
507 // combine all the lists into one
508 pListNew = NULL;
509 ppListNew = &pListNew;
510 for ( i = 1; i <= p->nVarsMax; i++ )
511 {
512 if ( pLists[i] == NULL )
513 continue;
514 // find the last entry
515 for ( pPrev = pLists[i], pCut = pPrev->pNext; pCut;
516 pPrev = pCut, pCut = pCut->pNext );
517 // connect these lists
518 *ppListNew = pLists[i];
519 ppListNew = &pPrev->pNext;
520 }
521 *ppListNew = NULL;
522 // sort the cuts by arrival times and use only the first FPGA_CUTS_MAX_USE
523 pListNew = Fpga_CutSortCuts( p, pTable, pListNew );
524 return pListNew;
525}
526
527
540 Fpga_Cut_t * pList1, Fpga_Cut_t * pList2, int fComp1, int fComp2, int fPivot1, int fPivot2 )
541{
542 Fpga_Node_t * ppNodes[FPGA_MAX_LEAVES];
543 Fpga_Cut_t * pListNew, ** ppListNew, * pLists[FPGA_MAX_LEAVES+1] = { NULL };
544 Fpga_Cut_t * pCut, * pPrev, * pTemp1, * pTemp2;
545 int nNodes, Counter, i;
546
547 // prepare the manager for the cut computation
548 Fpga_CutTableRestart( pTable );
549 // go through the cut pairs
550 Counter = 0;
551 for ( pTemp1 = pList1; pTemp1; pTemp1 = fPivot1? NULL: pTemp1->pNext )
552 for ( pTemp2 = pList2; pTemp2; pTemp2 = fPivot2? NULL: pTemp2->pNext )
553 {
554 // check if k-feasible cut exists
555 nNodes = Fpga_CutMergeTwo( pTemp1, pTemp2, ppNodes, p->nVarsMax );
556 if ( nNodes == 0 )
557 continue;
558 // consider the cut for possible addition to the set of new cuts
559 pCut = Fpga_CutTableConsider( p, pTable, ppNodes, nNodes );
560 if ( pCut == NULL )
561 continue;
562 // add data to the cut
563 pCut->pOne = Fpga_CutNotCond( pTemp1, fComp1 );
564 pCut->pTwo = Fpga_CutNotCond( pTemp2, fComp2 );
565 // add it to the corresponding list
566 pCut->pNext = pLists[(int)pCut->nLeaves];
567 pLists[(int)pCut->nLeaves] = pCut;
568 // count this cut and quit if limit is reached
569 Counter++;
570 if ( Counter == FPGA_CUTS_MAX_COMPUTE )
571 goto QUITS;
572 }
573QUITS :
574 // combine all the lists into one
575 pListNew = NULL;
576 ppListNew = &pListNew;
577 for ( i = 1; i <= p->nVarsMax; i++ )
578 {
579 if ( pLists[i] == NULL )
580 continue;
581 // find the last entry
582 for ( pPrev = pLists[i], pCut = pPrev->pNext; pCut;
583 pPrev = pCut, pCut = pCut->pNext );
584 // connect these lists
585 *ppListNew = pLists[i];
586 ppListNew = &pPrev->pNext;
587 }
588 *ppListNew = NULL;
589 // sort the cuts by arrival times and use only the first FPGA_CUTS_MAX_USE
590 pListNew = Fpga_CutSortCuts( p, pTable, pListNew );
591 return pListNew;
592}
593
606int Fpga_CutMergeTwo( Fpga_Cut_t * pCut1, Fpga_Cut_t * pCut2, Fpga_Node_t * ppNodes[], int nNodesMax )
607{
608 Fpga_Node_t * pNodeTemp;
609 int nTotal, i, k, min, Counter;
610 unsigned uSign;
611
612 // use quick prefiltering
613 uSign = pCut1->uSign | pCut2->uSign;
614 Counter = FPGA_COUNT_ONES(uSign);
615 if ( Counter > nNodesMax )
616 return 0;
617/*
618 // check the special case when at least of the cuts is the largest
619 if ( pCut1->nLeaves == nNodesMax )
620 {
621 if ( pCut2->nLeaves == nNodesMax )
622 {
623 // return 0 if the cuts are different
624 for ( i = 0; i < nNodesMax; i++ )
625 if ( pCut1->ppLeaves[i] != pCut2->ppLeaves[i] )
626 return 0;
627 // return nNodesMax if they are the same
628 for ( i = 0; i < nNodesMax; i++ )
629 ppNodes[i] = pCut1->ppLeaves[i];
630 return nNodesMax;
631 }
632 else if ( pCut2->nLeaves == nNodesMax - 1 )
633 {
634 // return 0 if the cuts are different
635 fMismatch = 0;
636 for ( i = 0; i < nNodesMax; i++ )
637 if ( pCut1->ppLeaves[i] != pCut2->ppLeaves[i - fMismatch] )
638 {
639 if ( fMismatch == 1 )
640 return 0;
641 fMismatch = 1;
642 }
643 // return nNodesMax if they are the same
644 for ( i = 0; i < nNodesMax; i++ )
645 ppNodes[i] = pCut1->ppLeaves[i];
646 return nNodesMax;
647 }
648 }
649 else if ( pCut1->nLeaves == nNodesMax - 1 && pCut2->nLeaves == nNodesMax )
650 {
651 // return 0 if the cuts are different
652 fMismatch = 0;
653 for ( i = 0; i < nNodesMax; i++ )
654 if ( pCut1->ppLeaves[i - fMismatch] != pCut2->ppLeaves[i] )
655 {
656 if ( fMismatch == 1 )
657 return 0;
658 fMismatch = 1;
659 }
660 // return nNodesMax if they are the same
661 for ( i = 0; i < nNodesMax; i++ )
662 ppNodes[i] = pCut2->ppLeaves[i];
663 return nNodesMax;
664 }
665*/
666 // count the number of unique entries in pCut2
667 nTotal = pCut1->nLeaves;
668 for ( i = 0; i < pCut2->nLeaves; i++ )
669 {
670 // try to find this entry among the leaves of pCut1
671 for ( k = 0; k < pCut1->nLeaves; k++ )
672 if ( pCut2->ppLeaves[i] == pCut1->ppLeaves[k] )
673 break;
674 if ( k < pCut1->nLeaves ) // found
675 continue;
676 // we found a new entry to add
677 if ( nTotal == nNodesMax )
678 return 0;
679 ppNodes[nTotal++] = pCut2->ppLeaves[i];
680 }
681 // we know that the feasible cut exists
682
683 // add the starting entries
684 for ( k = 0; k < pCut1->nLeaves; k++ )
685 ppNodes[k] = pCut1->ppLeaves[k];
686
687 // selection-sort the entries
688 for ( i = 0; i < nTotal - 1; i++ )
689 {
690 min = i;
691 for ( k = i+1; k < nTotal; k++ )
692// if ( ppNodes[k] < ppNodes[min] ) // reported bug fix (non-determinism!)
693 if ( ppNodes[k]->Num < ppNodes[min]->Num )
694 min = k;
695 pNodeTemp = ppNodes[i];
696 ppNodes[i] = ppNodes[min];
697 ppNodes[min] = pNodeTemp;
698 }
699
700 return nTotal;
701}
702
714Fpga_Cut_t * Fpga_CutUnionLists( Fpga_Cut_t * pList1, Fpga_Cut_t * pList2 )
715{
716 Fpga_Cut_t * pTemp, * pRoot;
717 // find the last cut in the first list
718 pRoot = pList1;
719 Fpga_ListForEachCut( pList1, pTemp )
720 pRoot = pTemp;
721 // attach the non-trival part of the second cut to the end of the first
722 assert( pRoot->pNext == NULL );
723 pRoot->pNext = pList2->pNext;
724 pList2->pNext = NULL;
725 return pList1;
726}
727
728
741int Fpga_CutBelongsToList( Fpga_Cut_t * pList, Fpga_Node_t * ppNodes[], int nNodes )
742{
743 Fpga_Cut_t * pTemp;
744 int i;
745 for ( pTemp = pList; pTemp; pTemp = pTemp->pNext )
746 {
747 for ( i = 0; i < nNodes; i++ )
748 if ( pTemp->ppLeaves[i] != ppNodes[i] )
749 break;
750 if ( i == nNodes )
751 return 1;
752 }
753 return 0;
754}
755
768{
769 Fpga_Node_t * pNode;
770 Fpga_Cut_t * pCut;
771 int i, nCuts;
772 // go through all the nodes in the unique table of the manager
773 nCuts = 0;
774 for ( i = 0; i < pMan->nBins; i++ )
775 for ( pNode = pMan->pBins[i]; pNode; pNode = pNode->pNext )
776 for ( pCut = pNode->pCuts; pCut; pCut = pCut->pNext )
777 if ( pCut->nLeaves > 1 ) // skip the elementary cuts
778 {
779// Fpga_CutVolume( pCut );
780 nCuts++;
781 }
782 return nCuts;
783}
784
785
798{
799 Fpga_Node_t * pNode;
800 Fpga_Cut_t * pCut;
801 int i;
802 for ( i = 0; i < pMan->nBins; i++ )
803 for ( pNode = pMan->pBins[i]; pNode; pNode = pNode->pNext )
804 for ( pCut = pNode->pCuts; pCut; pCut = pCut->pNext )
805 pCut->uSign = 0;
806}
807
820{
821 Fpga_Node_t * pNode;
822 Fpga_Cut_t * pCut;
823 int i;
824 for ( i = 0; i < pMan->nBins; i++ )
825 for ( pNode = pMan->pBins[i]; pNode; pNode = pNode->pNext )
826 for ( pCut = pNode->pCuts; pCut; pCut = pCut->pNext )
827 pCut->pRoot = NULL;
828}
829
830
831
843void Fpga_CutListPrint( Fpga_Man_t * pMan, Fpga_Node_t * pRoot )
844{
845 Fpga_Cut_t * pTemp;
846 int Counter;
847 for ( Counter = 0, pTemp = pRoot->pCuts; pTemp; pTemp = pTemp->pNext, Counter++ )
848 {
849 printf( "%2d : ", Counter + 1 );
850 Fpga_CutPrint_( pMan, pTemp, pRoot );
851 }
852}
853
865void Fpga_CutListPrint2( Fpga_Man_t * pMan, Fpga_Node_t * pRoot )
866{
867 Fpga_Cut_t * pTemp;
868 int Counter;
869 for ( Counter = 0, pTemp = pRoot->pCuts; pTemp; pTemp = pTemp->pNext, Counter++ )
870 {
871 printf( "%2d : ", Counter + 1 );
872 Fpga_CutPrint_( pMan, pTemp, pRoot );
873 }
874}
875
887void Fpga_CutPrint_( Fpga_Man_t * pMan, Fpga_Cut_t * pCut, Fpga_Node_t * pRoot )
888{
889 int i;
890 printf( "(%3d) {", pRoot->Num );
891 for ( i = 0; i < pMan->nVarsMax; i++ )
892 if ( pCut->ppLeaves[i] )
893 printf( " %3d", pCut->ppLeaves[i]->Num );
894 printf( " }\n" );
895}
896
897
898
899
900
901
902
903
915Fpga_CutTable_t * Fpga_CutTableStart( Fpga_Man_t * pMan )
916{
918 // allocate the table
920 memset( p, 0, sizeof(Fpga_CutTable_t) );
921 p->nBins = Abc_PrimeCudd( 10 * FPGA_CUTS_MAX_COMPUTE );
922 p->pBins = ABC_ALLOC( Fpga_Cut_t *, p->nBins );
923 memset( p->pBins, 0, sizeof(Fpga_Cut_t *) * p->nBins );
924 p->pCuts = ABC_ALLOC( int, 2 * FPGA_CUTS_MAX_COMPUTE );
925 p->pArray = ABC_ALLOC( Fpga_Cut_t *, 2 * FPGA_CUTS_MAX_COMPUTE );
926 p->pCuts1 = ABC_ALLOC( Fpga_Cut_t *, 2 * FPGA_CUTS_MAX_COMPUTE );
927 p->pCuts2 = ABC_ALLOC( Fpga_Cut_t *, 2 * FPGA_CUTS_MAX_COMPUTE );
928 return p;
929}
930
942void Fpga_CutTableStop( Fpga_CutTable_t * p )
943{
944 ABC_FREE( p->pCuts1 );
945 ABC_FREE( p->pCuts2 );
946 ABC_FREE( p->pArray );
947 ABC_FREE( p->pBins );
948 ABC_FREE( p->pCuts );
949 ABC_FREE( p );
950}
951
963unsigned Fpga_CutTableHash( Fpga_Node_t * ppNodes[], int nNodes )
964{
965 unsigned uRes;
966 int i;
967 uRes = 0;
968 for ( i = 0; i < nNodes; i++ )
969 uRes += s_HashPrimes[i] * ppNodes[i]->Num;
970 return uRes;
971}
972
985int Fpga_CutTableLookup( Fpga_CutTable_t * p, Fpga_Node_t * ppNodes[], int nNodes )
986{
987 Fpga_Cut_t * pCut;
988 unsigned Key;
989 int b, i;
990
991 Key = Fpga_CutTableHash(ppNodes, nNodes) % p->nBins;
992 for ( b = Key; p->pBins[b]; b = (b+1) % p->nBins )
993 {
994 pCut = p->pBins[b];
995 if ( pCut->nLeaves != nNodes )
996 continue;
997 for ( i = 0; i < nNodes; i++ )
998 if ( pCut->ppLeaves[i] != ppNodes[i] )
999 break;
1000 if ( i == nNodes )
1001 return -1;
1002 }
1003 return b;
1004}
1005
1006
1018Fpga_Cut_t * Fpga_CutTableConsider( Fpga_Man_t * pMan, Fpga_CutTable_t * p, Fpga_Node_t * ppNodes[], int nNodes )
1019{
1020 Fpga_Cut_t * pCut;
1021 int Place, i;
1022 // check the cut
1023 Place = Fpga_CutTableLookup( p, ppNodes, nNodes );
1024 if ( Place == -1 )
1025 return NULL;
1026 assert( nNodes > 0 );
1027 // create the new cut
1028 pCut = Fpga_CutAlloc( pMan );
1029 pCut->nLeaves = nNodes;
1030 pCut->fLevel = 0.0;
1031 for ( i = 0; i < nNodes; i++ )
1032 {
1033 pCut->ppLeaves[i] = ppNodes[i];
1034 pCut->fLevel += ppNodes[i]->Level;
1035 }
1036 pCut->fLevel /= nNodes;
1037 // add the cut to the table
1038 assert( p->pBins[Place] == NULL );
1039 p->pBins[Place] = pCut;
1040 // add the cut to the new list
1041 p->pCuts[ p->nCuts++ ] = Place;
1042 return pCut;
1043}
1044
1057void Fpga_CutTableRestart( Fpga_CutTable_t * p )
1058{
1059 int i;
1060 for ( i = 0; i < p->nCuts; i++ )
1061 {
1062 assert( p->pBins[ p->pCuts[i] ] );
1063 p->pBins[ p->pCuts[i] ] = NULL;
1064 }
1065 p->nCuts = 0;
1066}
1067
1068
1069
1081int Fpga_CutSortCutsCompare( Fpga_Cut_t ** pC1, Fpga_Cut_t ** pC2 )
1082{
1083 if ( (*pC1)->nLeaves < (*pC2)->nLeaves )
1084 return -1;
1085 if ( (*pC1)->nLeaves > (*pC2)->nLeaves )
1086 return 1;
1087/*
1088 if ( (*pC1)->fLevel > (*pC2)->fLevel )
1089 return -1;
1090 if ( (*pC1)->fLevel < (*pC2)->fLevel )
1091 return 1;
1092*/
1093 return 0;
1094}
1095
1107Fpga_Cut_t * Fpga_CutSortCuts( Fpga_Man_t * pMan, Fpga_CutTable_t * p, Fpga_Cut_t * pList )
1108{
1109 Fpga_Cut_t * pListNew;
1110 int nCuts, i;
1111 // move the cuts from the list into the array
1112 nCuts = Fpga_CutList2Array( p->pCuts1, pList );
1113 assert( nCuts <= FPGA_CUTS_MAX_COMPUTE );
1114 // sort the cuts
1115 qsort( (void *)p->pCuts1, (size_t)nCuts, sizeof(void *),
1116 (int (*)(const void *, const void *)) Fpga_CutSortCutsCompare );
1117 // move them back into the list
1118 if ( nCuts > FPGA_CUTS_MAX_USE - 1 )
1119 {
1120// printf( "*" );
1121 // free the remaining cuts
1122 for ( i = FPGA_CUTS_MAX_USE - 1; i < nCuts; i++ )
1123 Extra_MmFixedEntryRecycle( pMan->mmCuts, (char *)p->pCuts1[i] );
1124 // update the number of cuts
1125 nCuts = FPGA_CUTS_MAX_USE - 1;
1126 }
1127 pListNew = Fpga_CutArray2List( p->pCuts1, nCuts );
1128 return pListNew;
1129}
1130
1142int Fpga_CutList2Array( Fpga_Cut_t ** pArray, Fpga_Cut_t * pList )
1143{
1144 int i;
1145 for ( i = 0; pList; pList = pList->pNext, i++ )
1146 pArray[i] = pList;
1147 return i;
1148}
1149
1161Fpga_Cut_t * Fpga_CutArray2List( Fpga_Cut_t ** pArray, int nCuts )
1162{
1163 Fpga_Cut_t * pListNew, ** ppListNew;
1164 int i;
1165 pListNew = NULL;
1166 ppListNew = &pListNew;
1167 for ( i = 0; i < nCuts; i++ )
1168 {
1169 // connect these lists
1170 *ppListNew = pArray[i];
1171 ppListNew = &pArray[i]->pNext;
1172//printf( " %d(%.2f)", pArray[i]->nLeaves, pArray[i]->fLevel );
1173 }
1174//printf( "\n" );
1175
1176 *ppListNew = NULL;
1177 return pListNew;
1178}
1179
1180
1184
1186
#define ABC_PRT(a, t)
Definition abc_global.h:255
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#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
ABC_NAMESPACE_IMPL_START typedef char ProgressBar
Definition bbrNtbdd.c:27
Cube * p
Definition exorList.c:222
void Extra_ProgressBarStop(ProgressBar *p)
void Extra_MmFixedEntryRecycle(Extra_MmFixed_t *p, char *pEntry)
ProgressBar * Extra_ProgressBarStart(FILE *pFile, int nItemsTotal)
FUNCTION DEFINITIONS ///.
void Fpga_CutFree(Fpga_Man_t *p, Fpga_Cut_t *pCut)
#define FPGA_CUTS_MAX_USE
Definition fpgaCut.c:45
Fpga_Cut_t * Fpga_CutMergeLists2(Fpga_Man_t *p, Fpga_CutTable_t *pTable, Fpga_Cut_t *pList1, Fpga_Cut_t *pList2, int fComp1, int fComp2, int fPivot1, int fPivot2)
Definition fpgaCut.c:539
#define Fpga_ListForEachCutSafe(pList, pCut, pCut2)
Definition fpgaCut.c:94
typedefABC_NAMESPACE_IMPL_START struct Fpga_CutTableStrutct_t Fpga_CutTable_t
DECLARATIONS ///.
Definition fpgaCut.c:28
Fpga_Cut_t * Fpga_CutAlloc(Fpga_Man_t *p)
DECLARATIONS ///.
#define FPGA_COUNT_ONES(u)
Definition fpgaCut.c:61
void Fpga_MappingCreatePiCuts(Fpga_Man_t *p)
Definition fpgaCut.c:181
#define FPGA_CUTS_MAX_COMPUTE
Definition fpgaCut.c:42
void Fpga_CutsCleanSign(Fpga_Man_t *pMan)
Definition fpgaCut.c:797
int Fpga_CutCountAll(Fpga_Man_t *pMan)
Definition fpgaCut.c:767
#define Fpga_ListForEachCut(pList, pCut)
Definition fpgaCut.c:90
void Fpga_CutsCleanRoot(Fpga_Man_t *pMan)
Definition fpgaCut.c:819
void Fpga_MappingCuts(Fpga_Man_t *p)
FUNCTION DEFINITIONS ///.
Definition fpgaCut.c:130
#define Fpga_NodeReadRef(p)
Definition fpgaInt.h:85
#define Fpga_CutNotCond(p, c)
Definition fpgaInt.h:76
#define FPGA_MAX_LEAVES
INCLUDES ///.
Definition fpgaInt.h:52
struct Fpga_NodeStruct_t_ Fpga_Node_t
Definition fpga.h:44
struct Fpga_ManStruct_t_ Fpga_Man_t
STRUCTURE DEFINITIONS ///.
Definition fpga.h:43
int Fpga_NodeComparePhase(Fpga_Node_t *p1, Fpga_Node_t *p2)
Definition fpgaCreate.c:128
struct Fpga_CutStruct_t_ Fpga_Cut_t
Definition fpga.h:46
#define Fpga_IsComplement(p)
GLOBAL VARIABLES ///.
Definition fpga.h:57
int Fpga_NodeIsAnd(Fpga_Node_t *p)
Definition fpgaCreate.c:127
#define Fpga_Regular(p)
Definition fpga.h:58
Fpga_Node_t * pRoot
Definition fpgaInt.h:236
Fpga_Node_t * ppLeaves[FPGA_MAX_LEAVES+1]
Definition fpgaInt.h:237
Fpga_Cut_t * pTwo
Definition fpgaInt.h:235
Fpga_Cut_t * pNext
Definition fpgaInt.h:246
unsigned uSign
Definition fpgaInt.h:239
Fpga_Cut_t * pOne
Definition fpgaInt.h:234
Fpga_Cut_t ** pBins
Definition fpgaCut.c:31
Fpga_Cut_t ** pCuts2
Definition fpgaCut.c:37
Fpga_Cut_t ** pCuts1
Definition fpgaCut.c:36
Fpga_Cut_t ** pArray
Definition fpgaCut.c:35
Fpga_Node_t ** pBins
Definition fpgaInt.h:102
Extra_MmFixed_t * mmCuts
Definition fpgaInt.h:141
Fpga_Cut_t * pCuts
Definition fpgaInt.h:224
Fpga_Node_t * pNextE
Definition fpgaInt.h:202
Fpga_Node_t * pRepr
Definition fpgaInt.h:203
Fpga_Node_t * p1
Definition fpgaInt.h:200
Fpga_Node_t * p2
Definition fpgaInt.h:201
Fpga_Node_t * pNext
Definition fpgaInt.h:183
#define assert(ex)
Definition util_old.h:213
char * memset()