00001
00019 #include "fpgaInt.h"
00020
00024
00025 typedef struct Fpga_CutTableStrutct_t Fpga_CutTable_t;
00026 struct Fpga_CutTableStrutct_t
00027 {
00028 Fpga_Cut_t ** pBins;
00029 int nBins;
00030 int * pCuts;
00031 int nCuts;
00032 Fpga_Cut_t ** pArray;
00033 Fpga_Cut_t ** pCuts1;
00034 Fpga_Cut_t ** pCuts2;
00035 };
00036
00037
00038
00039 #define FPGA_CUTS_MAX_COMPUTE 2000
00040
00041
00042 #define FPGA_CUTS_MAX_USE 1000
00043
00044
00045 static int s_HashPrimes[10] = { 109, 499, 557, 619, 631, 709, 797, 881, 907, 991 };
00046
00047 static int bit_count[256] = {
00048 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,
00049 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,
00050 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,
00051 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,
00052 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,
00053 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,
00054 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,
00055 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
00056 };
00057
00058 #define FPGA_COUNT_ONES(u) (bit_count[(u)&255]+bit_count[((u)>>8)&255]+bit_count[((u)>>16)&255]+bit_count[(u)>>24])
00059
00060 static Fpga_Cut_t * Fpga_CutCompute( Fpga_Man_t * p, Fpga_CutTable_t * pTable, Fpga_Node_t * pNode );
00061 static void Fpga_CutFilter( Fpga_Man_t * p, Fpga_Node_t * pNode );
00062 static 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 );
00063 static int Fpga_CutMergeTwo( Fpga_Cut_t * pCut1, Fpga_Cut_t * pCut2, Fpga_Node_t * ppNodes[], int nNodesMax );
00064 static Fpga_Cut_t * Fpga_CutUnionLists( Fpga_Cut_t * pList1, Fpga_Cut_t * pList2 );
00065 static int Fpga_CutBelongsToList( Fpga_Cut_t * pList, Fpga_Node_t * ppNodes[], int nNodes );
00066 extern Fpga_Cut_t * Fpga_CutAlloc( Fpga_Man_t * p );
00067 extern int Fpga_CutCountAll( Fpga_Man_t * pMan );
00068
00069 static void Fpga_CutListPrint( Fpga_Man_t * pMan, Fpga_Node_t * pRoot );
00070 static void Fpga_CutListPrint2( Fpga_Man_t * pMan, Fpga_Node_t * pRoot );
00071 static void Fpga_CutPrint_( Fpga_Man_t * pMan, Fpga_Cut_t * pCut, Fpga_Node_t * pRoot );
00072
00073 static Fpga_CutTable_t * Fpga_CutTableStart( Fpga_Man_t * pMan );
00074 static void Fpga_CutTableStop( Fpga_CutTable_t * p );
00075 static unsigned Fpga_CutTableHash( Fpga_Node_t * ppNodes[], int nNodes );
00076 static int Fpga_CutTableLookup( Fpga_CutTable_t * p, Fpga_Node_t * ppNodes[], int nNodes );
00077 static Fpga_Cut_t * Fpga_CutTableConsider( Fpga_Man_t * pMan, Fpga_CutTable_t * p, Fpga_Node_t * ppNodes[], int nNodes );
00078 static void Fpga_CutTableRestart( Fpga_CutTable_t * p );
00079
00080 static int Fpga_CutSortCutsCompare( Fpga_Cut_t ** pC1, Fpga_Cut_t ** pC2 );
00081 static Fpga_Cut_t * Fpga_CutSortCuts( Fpga_Man_t * pMan, Fpga_CutTable_t * p, Fpga_Cut_t * pList );
00082 static int Fpga_CutList2Array( Fpga_Cut_t ** pArray, Fpga_Cut_t * pList );
00083 static Fpga_Cut_t * Fpga_CutArray2List( Fpga_Cut_t ** pArray, int nCuts );
00084
00085
00086
00087 #define Fpga_ListForEachCut( pList, pCut ) \
00088 for ( pCut = pList; \
00089 pCut; \
00090 pCut = pCut->pNext )
00091 #define Fpga_ListForEachCutSafe( pList, pCut, pCut2 ) \
00092 for ( pCut = pList, \
00093 pCut2 = pCut? pCut->pNext: NULL; \
00094 pCut; \
00095 pCut = pCut2, \
00096 pCut2 = pCut? pCut->pNext: NULL )
00097
00098
00102
00127 void Fpga_MappingCuts( Fpga_Man_t * p )
00128 {
00129 ProgressBar * pProgress;
00130 Fpga_CutTable_t * pTable;
00131 Fpga_Node_t * pNode;
00132 int nCuts, nNodes, i;
00133 int clk = clock();
00134
00135
00136 assert( p->nVarsMax > 1 && p->nVarsMax < 11 );
00137 Fpga_MappingCreatePiCuts( p );
00138
00139
00140 nNodes = p->vAnds->nSize;
00141 pProgress = Extra_ProgressBarStart( stdout, nNodes );
00142 pTable = Fpga_CutTableStart( p );
00143 for ( i = 0; i < nNodes; i++ )
00144 {
00145 Extra_ProgressBarUpdate( pProgress, i, "Cuts ..." );
00146 pNode = p->vAnds->pArray[i];
00147 if ( !Fpga_NodeIsAnd( pNode ) )
00148 continue;
00149 Fpga_CutCompute( p, pTable, pNode );
00150 }
00151 Extra_ProgressBarStop( pProgress );
00152 Fpga_CutTableStop( pTable );
00153
00154
00155 if ( p->fVerbose )
00156 {
00157 nCuts = Fpga_CutCountAll(p);
00158 printf( "Nodes = %6d. Total %d-cuts = %d. Cuts per node = %.1f. ",
00159 p->nNodes, p->nVarsMax, nCuts, ((float)nCuts)/p->nNodes );
00160 PRT( "Time", clock() - clk );
00161 }
00162
00163
00164
00165 }
00166
00178 void Fpga_MappingCreatePiCuts( Fpga_Man_t * p )
00179 {
00180 Fpga_Cut_t * pCut;
00181 int i;
00182
00183
00184 for ( i = 0; i < p->nInputs; i++ )
00185 {
00186 pCut = Fpga_CutAlloc( p );
00187 pCut->nLeaves = 1;
00188 pCut->ppLeaves[0] = p->pInputs[i];
00189 pCut->uSign = (1 << (i%31));
00190 p->pInputs[i]->pCuts = pCut;
00191 p->pInputs[i]->pCutBest = pCut;
00192
00193
00194 }
00195 }
00196
00208 Fpga_Cut_t * Fpga_CutCompute( Fpga_Man_t * p, Fpga_CutTable_t * pTable, Fpga_Node_t * pNode )
00209 {
00210 Fpga_Node_t * pTemp;
00211 Fpga_Cut_t * pList, * pList1, * pList2;
00212 Fpga_Cut_t * pCut;
00213 int fTree = 0;
00214 int fPivot1 = fTree && (Fpga_NodeReadRef(pNode->p1)>2);
00215 int fPivot2 = fTree && (Fpga_NodeReadRef(pNode->p2)>2);
00216
00217
00218 if ( pNode->pCuts )
00219 return pNode->pCuts;
00220
00221
00222 pList1 = Fpga_Regular(pNode->p1)->pCuts;
00223 pList2 = Fpga_Regular(pNode->p2)->pCuts;
00224
00225 pList = Fpga_CutMergeLists( p, pTable, pList1, pList2,
00226 Fpga_IsComplement(pNode->p1), Fpga_IsComplement(pNode->p2),
00227 fPivot1, fPivot2 );
00228
00229 assert( pList );
00230
00231 if ( pNode->pRepr == NULL )
00232 {
00233 for ( pTemp = pNode->pNextE; pTemp; pTemp = pTemp->pNextE )
00234 {
00235 assert( pTemp->pCuts );
00236 pList = Fpga_CutUnionLists( pList, pTemp->pCuts );
00237 assert( pTemp->pCuts );
00238 pList = Fpga_CutSortCuts( p, pTable, pList );
00239 }
00240 }
00241
00242 pCut = Fpga_CutAlloc( p );
00243 pCut->nLeaves = 1;
00244 pCut->ppLeaves[0] = pNode;
00245 pCut->uSign = (1 << (pNode->Num%31));
00246 pCut->fLevel = (float)pCut->ppLeaves[0]->Level;
00247
00248 pCut->pNext = pList;
00249
00250 pNode->pCuts = pCut;
00251
00252
00253
00254 if ( pNode->pRepr && Fpga_NodeComparePhase(pNode, pNode->pRepr) )
00255 {
00256 Fpga_ListForEachCut( pNode->pCuts, pCut )
00257 pCut->Phase = 1;
00258 }
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 return pCut;
00287 }
00288
00300 void Fpga_CutFilter( Fpga_Man_t * p, Fpga_Node_t * pNode )
00301 {
00302 Fpga_Cut_t * pTemp, * pPrev, * pCut, * pCut2;
00303 int i, k, Counter;
00304
00305 Counter = 0;
00306 pPrev = pNode->pCuts;
00307 Fpga_ListForEachCutSafe( pNode->pCuts->pNext, pCut, pCut2 )
00308 {
00309
00310 for ( pTemp = pNode->pCuts->pNext; pTemp != pCut; pTemp = pTemp->pNext )
00311 {
00312
00313 for ( i = 0; i < pTemp->nLeaves; i++ )
00314 {
00315 for ( k = 0; k < pCut->nLeaves; k++ )
00316 if ( pTemp->ppLeaves[i] == pCut->ppLeaves[k] )
00317 break;
00318 if ( k == pCut->nLeaves )
00319 break;
00320 }
00321 if ( i == pTemp->nLeaves )
00322 {
00323 Counter++;
00324 break;
00325 }
00326 }
00327 if ( pTemp != pCut )
00328 {
00329 pPrev->pNext = pCut->pNext;
00330
00331 Fpga_CutFree( p, pCut );
00332 }
00333 else
00334 pPrev = pCut;
00335 }
00336
00337 }
00338
00339
00351 Fpga_Cut_t * Fpga_CutMergeLists( Fpga_Man_t * p, Fpga_CutTable_t * pTable,
00352 Fpga_Cut_t * pList1, Fpga_Cut_t * pList2, int fComp1, int fComp2, int fPivot1, int fPivot2 )
00353 {
00354 Fpga_Node_t * ppNodes[FPGA_MAX_LEAVES];
00355 Fpga_Cut_t * pListNew, ** ppListNew, * pLists[FPGA_MAX_LEAVES+1] = { NULL };
00356 Fpga_Cut_t * pCut, * pPrev, * pTemp1, * pTemp2;
00357 int nNodes, Counter, i;
00358 Fpga_Cut_t ** ppArray1, ** ppArray2, ** ppArray3;
00359 int nCuts1, nCuts2, nCuts3, k, fComp3;
00360
00361 ppArray1 = pTable->pCuts1;
00362 ppArray2 = pTable->pCuts2;
00363 nCuts1 = Fpga_CutList2Array( ppArray1, pList1 );
00364 nCuts2 = Fpga_CutList2Array( ppArray2, pList2 );
00365 if ( fPivot1 )
00366 nCuts1 = 1;
00367 if ( fPivot2 )
00368 nCuts2 = 1;
00369
00370 if ( nCuts1 > nCuts2 )
00371 {
00372 ppArray3 = ppArray1;
00373 ppArray1 = ppArray2;
00374 ppArray2 = ppArray3;
00375
00376 nCuts3 = nCuts1;
00377 nCuts1 = nCuts2;
00378 nCuts2 = nCuts3;
00379
00380 fComp3 = fComp1;
00381 fComp1 = fComp2;
00382 fComp2 = fComp3;
00383 }
00384
00385
00386
00387 Fpga_CutTableRestart( pTable );
00388
00389 Counter = 0;
00390
00391
00392 for ( i = 0; i < nCuts1; i++ )
00393 {
00394 for ( k = 0; k <= i; k++ )
00395 {
00396 pTemp1 = ppArray1[i];
00397 pTemp2 = ppArray2[k];
00398
00399 if ( pTemp1->nLeaves == p->nVarsMax && pTemp2->nLeaves == p->nVarsMax )
00400 {
00401 if ( pTemp1->ppLeaves[0] != pTemp2->ppLeaves[0] )
00402 continue;
00403 if ( pTemp1->ppLeaves[1] != pTemp2->ppLeaves[1] )
00404 continue;
00405 }
00406
00407
00408 nNodes = Fpga_CutMergeTwo( pTemp1, pTemp2, ppNodes, p->nVarsMax );
00409 if ( nNodes == 0 )
00410 continue;
00411
00412 pCut = Fpga_CutTableConsider( p, pTable, ppNodes, nNodes );
00413 if ( pCut == NULL )
00414 continue;
00415
00416 pCut->pOne = Fpga_CutNotCond( pTemp1, fComp1 );
00417 pCut->pTwo = Fpga_CutNotCond( pTemp2, fComp2 );
00418
00419 pCut->uSign = pTemp1->uSign | pTemp2->uSign;
00420
00421 pCut->pNext = pLists[pCut->nLeaves];
00422 pLists[pCut->nLeaves] = pCut;
00423
00424 Counter++;
00425 if ( Counter == FPGA_CUTS_MAX_COMPUTE )
00426 goto QUITS;
00427 }
00428 for ( k = 0; k < i; k++ )
00429 {
00430 pTemp1 = ppArray1[k];
00431 pTemp2 = ppArray2[i];
00432
00433 if ( pTemp1->nLeaves == p->nVarsMax && pTemp2->nLeaves == p->nVarsMax )
00434 {
00435 if ( pTemp1->ppLeaves[0] != pTemp2->ppLeaves[0] )
00436 continue;
00437 if ( pTemp1->ppLeaves[1] != pTemp2->ppLeaves[1] )
00438 continue;
00439 }
00440
00441
00442
00443 nNodes = Fpga_CutMergeTwo( pTemp1, pTemp2, ppNodes, p->nVarsMax );
00444 if ( nNodes == 0 )
00445 continue;
00446
00447 pCut = Fpga_CutTableConsider( p, pTable, ppNodes, nNodes );
00448 if ( pCut == NULL )
00449 continue;
00450
00451 pCut->pOne = Fpga_CutNotCond( pTemp1, fComp1 );
00452 pCut->pTwo = Fpga_CutNotCond( pTemp2, fComp2 );
00453
00454 pCut->uSign = pTemp1->uSign | pTemp2->uSign;
00455
00456 pCut->pNext = pLists[pCut->nLeaves];
00457 pLists[pCut->nLeaves] = pCut;
00458
00459 Counter++;
00460 if ( Counter == FPGA_CUTS_MAX_COMPUTE )
00461 goto QUITS;
00462 }
00463 }
00464
00465 for ( i = nCuts1; i < nCuts2; i++ )
00466 for ( k = 0; k < nCuts1; k++ )
00467 {
00468 pTemp1 = ppArray1[k];
00469 pTemp2 = ppArray2[i];
00470
00471 if ( pTemp1->nLeaves == p->nVarsMax && pTemp2->nLeaves == p->nVarsMax )
00472 {
00473 if ( pTemp1->ppLeaves[0] != pTemp2->ppLeaves[0] )
00474 continue;
00475 if ( pTemp1->ppLeaves[1] != pTemp2->ppLeaves[1] )
00476 continue;
00477 if ( pTemp1->ppLeaves[2] != pTemp2->ppLeaves[2] )
00478 continue;
00479 }
00480
00481
00482
00483 nNodes = Fpga_CutMergeTwo( pTemp1, pTemp2, ppNodes, p->nVarsMax );
00484 if ( nNodes == 0 )
00485 continue;
00486
00487 pCut = Fpga_CutTableConsider( p, pTable, ppNodes, nNodes );
00488 if ( pCut == NULL )
00489 continue;
00490
00491 pCut->pOne = Fpga_CutNotCond( pTemp1, fComp1 );
00492 pCut->pTwo = Fpga_CutNotCond( pTemp2, fComp2 );
00493
00494 pCut->uSign = pTemp1->uSign | pTemp2->uSign;
00495
00496 pCut->pNext = pLists[pCut->nLeaves];
00497 pLists[pCut->nLeaves] = pCut;
00498
00499 Counter++;
00500 if ( Counter == FPGA_CUTS_MAX_COMPUTE )
00501 goto QUITS;
00502 }
00503 QUITS :
00504
00505 pListNew = NULL;
00506 ppListNew = &pListNew;
00507 for ( i = 1; i <= p->nVarsMax; i++ )
00508 {
00509 if ( pLists[i] == NULL )
00510 continue;
00511
00512 for ( pPrev = pLists[i], pCut = pPrev->pNext; pCut;
00513 pPrev = pCut, pCut = pCut->pNext );
00514
00515 *ppListNew = pLists[i];
00516 ppListNew = &pPrev->pNext;
00517 }
00518 *ppListNew = NULL;
00519
00520 pListNew = Fpga_CutSortCuts( p, pTable, pListNew );
00521 return pListNew;
00522 }
00523
00524
00536 Fpga_Cut_t * Fpga_CutMergeLists2( Fpga_Man_t * p, Fpga_CutTable_t * pTable,
00537 Fpga_Cut_t * pList1, Fpga_Cut_t * pList2, int fComp1, int fComp2, int fPivot1, int fPivot2 )
00538 {
00539 Fpga_Node_t * ppNodes[FPGA_MAX_LEAVES];
00540 Fpga_Cut_t * pListNew, ** ppListNew, * pLists[FPGA_MAX_LEAVES+1] = { NULL };
00541 Fpga_Cut_t * pCut, * pPrev, * pTemp1, * pTemp2;
00542 int nNodes, Counter, i;
00543
00544
00545 Fpga_CutTableRestart( pTable );
00546
00547 Counter = 0;
00548 for ( pTemp1 = pList1; pTemp1; pTemp1 = fPivot1? NULL: pTemp1->pNext )
00549 for ( pTemp2 = pList2; pTemp2; pTemp2 = fPivot2? NULL: pTemp2->pNext )
00550 {
00551
00552 nNodes = Fpga_CutMergeTwo( pTemp1, pTemp2, ppNodes, p->nVarsMax );
00553 if ( nNodes == 0 )
00554 continue;
00555
00556 pCut = Fpga_CutTableConsider( p, pTable, ppNodes, nNodes );
00557 if ( pCut == NULL )
00558 continue;
00559
00560 pCut->pOne = Fpga_CutNotCond( pTemp1, fComp1 );
00561 pCut->pTwo = Fpga_CutNotCond( pTemp2, fComp2 );
00562
00563 pCut->pNext = pLists[pCut->nLeaves];
00564 pLists[pCut->nLeaves] = pCut;
00565
00566 Counter++;
00567 if ( Counter == FPGA_CUTS_MAX_COMPUTE )
00568 goto QUITS;
00569 }
00570 QUITS :
00571
00572 pListNew = NULL;
00573 ppListNew = &pListNew;
00574 for ( i = 1; i <= p->nVarsMax; i++ )
00575 {
00576 if ( pLists[i] == NULL )
00577 continue;
00578
00579 for ( pPrev = pLists[i], pCut = pPrev->pNext; pCut;
00580 pPrev = pCut, pCut = pCut->pNext );
00581
00582 *ppListNew = pLists[i];
00583 ppListNew = &pPrev->pNext;
00584 }
00585 *ppListNew = NULL;
00586
00587 pListNew = Fpga_CutSortCuts( p, pTable, pListNew );
00588 return pListNew;
00589 }
00590
00603 int Fpga_CutMergeTwo( Fpga_Cut_t * pCut1, Fpga_Cut_t * pCut2, Fpga_Node_t * ppNodes[], int nNodesMax )
00604 {
00605 Fpga_Node_t * pNodeTemp;
00606 int nTotal, i, k, min, Counter;
00607 unsigned uSign;
00608
00609
00610 uSign = pCut1->uSign | pCut2->uSign;
00611 Counter = FPGA_COUNT_ONES(uSign);
00612 if ( Counter > nNodesMax )
00613 return 0;
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664 nTotal = pCut1->nLeaves;
00665 for ( i = 0; i < pCut2->nLeaves; i++ )
00666 {
00667
00668 for ( k = 0; k < pCut1->nLeaves; k++ )
00669 if ( pCut2->ppLeaves[i] == pCut1->ppLeaves[k] )
00670 break;
00671 if ( k < pCut1->nLeaves )
00672 continue;
00673
00674 if ( nTotal == nNodesMax )
00675 return 0;
00676 ppNodes[nTotal++] = pCut2->ppLeaves[i];
00677 }
00678
00679
00680
00681 for ( k = 0; k < pCut1->nLeaves; k++ )
00682 ppNodes[k] = pCut1->ppLeaves[k];
00683
00684
00685 for ( i = 0; i < nTotal - 1; i++ )
00686 {
00687 min = i;
00688 for ( k = i+1; k < nTotal; k++ )
00689
00690 if ( ppNodes[k]->Num < ppNodes[min]->Num )
00691 min = k;
00692 pNodeTemp = ppNodes[i];
00693 ppNodes[i] = ppNodes[min];
00694 ppNodes[min] = pNodeTemp;
00695 }
00696
00697 return nTotal;
00698 }
00699
00711 Fpga_Cut_t * Fpga_CutUnionLists( Fpga_Cut_t * pList1, Fpga_Cut_t * pList2 )
00712 {
00713 Fpga_Cut_t * pTemp, * pRoot;
00714
00715 pRoot = pList1;
00716 Fpga_ListForEachCut( pList1, pTemp )
00717 pRoot = pTemp;
00718
00719 assert( pRoot->pNext == NULL );
00720 pRoot->pNext = pList2->pNext;
00721 pList2->pNext = NULL;
00722 return pList1;
00723 }
00724
00725
00738 int Fpga_CutBelongsToList( Fpga_Cut_t * pList, Fpga_Node_t * ppNodes[], int nNodes )
00739 {
00740 Fpga_Cut_t * pTemp;
00741 int i;
00742 for ( pTemp = pList; pTemp; pTemp = pTemp->pNext )
00743 {
00744 for ( i = 0; i < nNodes; i++ )
00745 if ( pTemp->ppLeaves[i] != ppNodes[i] )
00746 break;
00747 if ( i == nNodes )
00748 return 1;
00749 }
00750 return 0;
00751 }
00752
00764 int Fpga_CutCountAll( Fpga_Man_t * pMan )
00765 {
00766 Fpga_Node_t * pNode;
00767 Fpga_Cut_t * pCut;
00768 int i, nCuts;
00769
00770 nCuts = 0;
00771 for ( i = 0; i < pMan->nBins; i++ )
00772 for ( pNode = pMan->pBins[i]; pNode; pNode = pNode->pNext )
00773 for ( pCut = pNode->pCuts; pCut; pCut = pCut->pNext )
00774 if ( pCut->nLeaves > 1 )
00775 {
00776
00777 nCuts++;
00778 }
00779 return nCuts;
00780 }
00781
00782
00794 void Fpga_CutsCleanSign( Fpga_Man_t * pMan )
00795 {
00796 Fpga_Node_t * pNode;
00797 Fpga_Cut_t * pCut;
00798 int i;
00799 for ( i = 0; i < pMan->nBins; i++ )
00800 for ( pNode = pMan->pBins[i]; pNode; pNode = pNode->pNext )
00801 for ( pCut = pNode->pCuts; pCut; pCut = pCut->pNext )
00802 pCut->uSign = 0;
00803 }
00804
00805
00806
00818 void Fpga_CutListPrint( Fpga_Man_t * pMan, Fpga_Node_t * pRoot )
00819 {
00820 Fpga_Cut_t * pTemp;
00821 int Counter;
00822 for ( Counter = 0, pTemp = pRoot->pCuts; pTemp; pTemp = pTemp->pNext, Counter++ )
00823 {
00824 printf( "%2d : ", Counter + 1 );
00825 Fpga_CutPrint_( pMan, pTemp, pRoot );
00826 }
00827 }
00828
00840 void Fpga_CutListPrint2( Fpga_Man_t * pMan, Fpga_Node_t * pRoot )
00841 {
00842 Fpga_Cut_t * pTemp;
00843 int Counter;
00844 for ( Counter = 0, pTemp = pRoot->pCuts; pTemp; pTemp = pTemp->pNext, Counter++ )
00845 {
00846 printf( "%2d : ", Counter + 1 );
00847 Fpga_CutPrint_( pMan, pTemp, pRoot );
00848 }
00849 }
00850
00862 void Fpga_CutPrint_( Fpga_Man_t * pMan, Fpga_Cut_t * pCut, Fpga_Node_t * pRoot )
00863 {
00864 int i;
00865 printf( "(%3d) {", pRoot->Num );
00866 for ( i = 0; i < pMan->nVarsMax; i++ )
00867 if ( pCut->ppLeaves[i] )
00868 printf( " %3d", pCut->ppLeaves[i]->Num );
00869 printf( " }\n" );
00870 }
00871
00872
00873
00874
00875
00876
00877
00878
00890 Fpga_CutTable_t * Fpga_CutTableStart( Fpga_Man_t * pMan )
00891 {
00892 Fpga_CutTable_t * p;
00893
00894 p = ALLOC( Fpga_CutTable_t, 1 );
00895 memset( p, 0, sizeof(Fpga_CutTable_t) );
00896 p->nBins = Cudd_Prime( 10 * FPGA_CUTS_MAX_COMPUTE );
00897 p->pBins = ALLOC( Fpga_Cut_t *, p->nBins );
00898 memset( p->pBins, 0, sizeof(Fpga_Cut_t *) * p->nBins );
00899 p->pCuts = ALLOC( int, 2 * FPGA_CUTS_MAX_COMPUTE );
00900 p->pArray = ALLOC( Fpga_Cut_t *, 2 * FPGA_CUTS_MAX_COMPUTE );
00901 p->pCuts1 = ALLOC( Fpga_Cut_t *, 2 * FPGA_CUTS_MAX_COMPUTE );
00902 p->pCuts2 = ALLOC( Fpga_Cut_t *, 2 * FPGA_CUTS_MAX_COMPUTE );
00903 return p;
00904 }
00905
00917 void Fpga_CutTableStop( Fpga_CutTable_t * p )
00918 {
00919 free( p->pCuts1 );
00920 free( p->pCuts2 );
00921 free( p->pArray );
00922 free( p->pBins );
00923 free( p->pCuts );
00924 free( p );
00925 }
00926
00938 unsigned Fpga_CutTableHash( Fpga_Node_t * ppNodes[], int nNodes )
00939 {
00940 unsigned uRes;
00941 int i;
00942 uRes = 0;
00943 for ( i = 0; i < nNodes; i++ )
00944 uRes += s_HashPrimes[i] * ppNodes[i]->Num;
00945 return uRes;
00946 }
00947
00960 int Fpga_CutTableLookup( Fpga_CutTable_t * p, Fpga_Node_t * ppNodes[], int nNodes )
00961 {
00962 Fpga_Cut_t * pCut;
00963 unsigned Key;
00964 int b, i;
00965
00966 Key = Fpga_CutTableHash(ppNodes, nNodes) % p->nBins;
00967 for ( b = Key; p->pBins[b]; b = (b+1) % p->nBins )
00968 {
00969 pCut = p->pBins[b];
00970 if ( pCut->nLeaves != nNodes )
00971 continue;
00972 for ( i = 0; i < nNodes; i++ )
00973 if ( pCut->ppLeaves[i] != ppNodes[i] )
00974 break;
00975 if ( i == nNodes )
00976 return -1;
00977 }
00978 return b;
00979 }
00980
00981
00993 Fpga_Cut_t * Fpga_CutTableConsider( Fpga_Man_t * pMan, Fpga_CutTable_t * p, Fpga_Node_t * ppNodes[], int nNodes )
00994 {
00995 Fpga_Cut_t * pCut;
00996 int Place, i;
00997
00998 Place = Fpga_CutTableLookup( p, ppNodes, nNodes );
00999 if ( Place == -1 )
01000 return NULL;
01001 assert( nNodes > 0 );
01002
01003 pCut = Fpga_CutAlloc( pMan );
01004 pCut->nLeaves = nNodes;
01005 pCut->fLevel = 0.0;
01006 for ( i = 0; i < nNodes; i++ )
01007 {
01008 pCut->ppLeaves[i] = ppNodes[i];
01009 pCut->fLevel += ppNodes[i]->Level;
01010 }
01011 pCut->fLevel /= nNodes;
01012
01013 assert( p->pBins[Place] == NULL );
01014 p->pBins[Place] = pCut;
01015
01016 p->pCuts[ p->nCuts++ ] = Place;
01017 return pCut;
01018 }
01019
01032 void Fpga_CutTableRestart( Fpga_CutTable_t * p )
01033 {
01034 int i;
01035 for ( i = 0; i < p->nCuts; i++ )
01036 {
01037 assert( p->pBins[ p->pCuts[i] ] );
01038 p->pBins[ p->pCuts[i] ] = NULL;
01039 }
01040 p->nCuts = 0;
01041 }
01042
01043
01044
01056 int Fpga_CutSortCutsCompare( Fpga_Cut_t ** pC1, Fpga_Cut_t ** pC2 )
01057 {
01058 if ( (*pC1)->nLeaves < (*pC2)->nLeaves )
01059 return -1;
01060 if ( (*pC1)->nLeaves > (*pC2)->nLeaves )
01061 return 1;
01062
01063
01064
01065
01066
01067
01068 return 0;
01069 }
01070
01082 Fpga_Cut_t * Fpga_CutSortCuts( Fpga_Man_t * pMan, Fpga_CutTable_t * p, Fpga_Cut_t * pList )
01083 {
01084 Fpga_Cut_t * pListNew;
01085 int nCuts, i;
01086
01087 nCuts = Fpga_CutList2Array( p->pCuts1, pList );
01088 assert( nCuts <= FPGA_CUTS_MAX_COMPUTE );
01089
01090 qsort( (void *)p->pCuts1, nCuts, sizeof(void *),
01091 (int (*)(const void *, const void *)) Fpga_CutSortCutsCompare );
01092
01093 if ( nCuts > FPGA_CUTS_MAX_USE - 1 )
01094 {
01095
01096
01097 for ( i = FPGA_CUTS_MAX_USE - 1; i < nCuts; i++ )
01098 Extra_MmFixedEntryRecycle( pMan->mmCuts, (char *)p->pCuts1[i] );
01099
01100 nCuts = FPGA_CUTS_MAX_USE - 1;
01101 }
01102 pListNew = Fpga_CutArray2List( p->pCuts1, nCuts );
01103 return pListNew;
01104 }
01105
01117 int Fpga_CutList2Array( Fpga_Cut_t ** pArray, Fpga_Cut_t * pList )
01118 {
01119 int i;
01120 for ( i = 0; pList; pList = pList->pNext, i++ )
01121 pArray[i] = pList;
01122 return i;
01123 }
01124
01136 Fpga_Cut_t * Fpga_CutArray2List( Fpga_Cut_t ** pArray, int nCuts )
01137 {
01138 Fpga_Cut_t * pListNew, ** ppListNew;
01139 int i;
01140 pListNew = NULL;
01141 ppListNew = &pListNew;
01142 for ( i = 0; i < nCuts; i++ )
01143 {
01144
01145 *ppListNew = pArray[i];
01146 ppListNew = &pArray[i]->pNext;
01147
01148 }
01149
01150
01151 *ppListNew = NULL;
01152 return pListNew;
01153 }
01154
01155
01159