00001
00021 #include "abc.h"
00022 #include "resInt.h"
00023
00027
00031
00043 Res_Sim_t * Res_SimAlloc( int nWords )
00044 {
00045 Res_Sim_t * p;
00046 p = ALLOC( Res_Sim_t, 1 );
00047 memset( p, 0, sizeof(Res_Sim_t) );
00048
00049 p->nWords = nWords;
00050 p->nPats = 8 * sizeof(unsigned) * p->nWords;
00051 p->nWordsOut = p->nPats * p->nWords;
00052 p->nPatsOut = p->nPats * p->nPats;
00053
00054 p->vPats = Vec_PtrAllocSimInfo( 1024, p->nWords );
00055 p->vPats0 = Vec_PtrAllocSimInfo( 128, p->nWords );
00056 p->vPats1 = Vec_PtrAllocSimInfo( 128, p->nWords );
00057 p->vOuts = Vec_PtrAllocSimInfo( 128, p->nWordsOut );
00058
00059 p->vCands = Vec_VecStart( 16 );
00060 return p;
00061 }
00062
00074 void Res_SimAdjust( Res_Sim_t * p, Abc_Ntk_t * pAig )
00075 {
00076 srand( 0xABC );
00077
00078 assert( Abc_NtkIsStrash(pAig) );
00079 p->pAig = pAig;
00080 if ( Vec_PtrSize(p->vPats) < Abc_NtkObjNumMax(pAig)+1 )
00081 {
00082 Vec_PtrFree( p->vPats );
00083 p->vPats = Vec_PtrAllocSimInfo( Abc_NtkObjNumMax(pAig)+1, p->nWords );
00084 }
00085 if ( Vec_PtrSize(p->vPats0) < Abc_NtkPiNum(pAig) )
00086 {
00087 Vec_PtrFree( p->vPats0 );
00088 p->vPats0 = Vec_PtrAllocSimInfo( Abc_NtkPiNum(pAig), p->nWords );
00089 }
00090 if ( Vec_PtrSize(p->vPats1) < Abc_NtkPiNum(pAig) )
00091 {
00092 Vec_PtrFree( p->vPats1 );
00093 p->vPats1 = Vec_PtrAllocSimInfo( Abc_NtkPiNum(pAig), p->nWords );
00094 }
00095 if ( Vec_PtrSize(p->vOuts) < Abc_NtkPoNum(pAig) )
00096 {
00097 Vec_PtrFree( p->vOuts );
00098 p->vOuts = Vec_PtrAllocSimInfo( Abc_NtkPoNum(pAig), p->nWordsOut );
00099 }
00100
00101 Abc_InfoClear( Vec_PtrEntry(p->vPats0,0), p->nWords * Abc_NtkPiNum(pAig) );
00102 Abc_InfoClear( Vec_PtrEntry(p->vPats1,0), p->nWords * Abc_NtkPiNum(pAig) );
00103 p->nPats0 = 0;
00104 p->nPats1 = 0;
00105 }
00106
00118 void Res_SimFree( Res_Sim_t * p )
00119 {
00120 Vec_PtrFree( p->vPats );
00121 Vec_PtrFree( p->vPats0 );
00122 Vec_PtrFree( p->vPats1 );
00123 Vec_PtrFree( p->vOuts );
00124 Vec_VecFree( p->vCands );
00125 free( p );
00126 }
00127
00128
00140 void Res_SimSetRandom( Res_Sim_t * p )
00141 {
00142 Abc_Obj_t * pObj;
00143 unsigned * pInfo;
00144 int i;
00145 Abc_NtkForEachPi( p->pAig, pObj, i )
00146 {
00147 pInfo = Vec_PtrEntry( p->vPats, pObj->Id );
00148 Abc_InfoRandom( pInfo, p->nWords );
00149 }
00150 }
00151
00163 void Res_SimSetGiven( Res_Sim_t * p, Vec_Ptr_t * vInfo )
00164 {
00165 Abc_Obj_t * pObj;
00166 unsigned * pInfo, * pInfo2;
00167 int i, w;
00168 Abc_NtkForEachPi( p->pAig, pObj, i )
00169 {
00170 pInfo = Vec_PtrEntry( p->vPats, pObj->Id );
00171 pInfo2 = Vec_PtrEntry( vInfo, i );
00172 for ( w = 0; w < p->nWords; w++ )
00173 pInfo[w] = pInfo2[w];
00174 }
00175 }
00176
00188 void Res_SimPerformOne( Abc_Obj_t * pNode, Vec_Ptr_t * vSimInfo, int nSimWords )
00189 {
00190 unsigned * pInfo, * pInfo1, * pInfo2;
00191 int k, fComp1, fComp2;
00192
00193 assert( Abc_ObjIsNode(pNode) );
00194 pInfo = Vec_PtrEntry(vSimInfo, pNode->Id);
00195 pInfo1 = Vec_PtrEntry(vSimInfo, Abc_ObjFaninId0(pNode));
00196 pInfo2 = Vec_PtrEntry(vSimInfo, Abc_ObjFaninId1(pNode));
00197 fComp1 = Abc_ObjFaninC0(pNode);
00198 fComp2 = Abc_ObjFaninC1(pNode);
00199 if ( fComp1 && fComp2 )
00200 for ( k = 0; k < nSimWords; k++ )
00201 pInfo[k] = ~pInfo1[k] & ~pInfo2[k];
00202 else if ( fComp1 && !fComp2 )
00203 for ( k = 0; k < nSimWords; k++ )
00204 pInfo[k] = ~pInfo1[k] & pInfo2[k];
00205 else if ( !fComp1 && fComp2 )
00206 for ( k = 0; k < nSimWords; k++ )
00207 pInfo[k] = pInfo1[k] & ~pInfo2[k];
00208 else
00209 for ( k = 0; k < nSimWords; k++ )
00210 pInfo[k] = pInfo1[k] & pInfo2[k];
00211 }
00212
00224 void Res_SimTransferOne( Abc_Obj_t * pNode, Vec_Ptr_t * vSimInfo, int nSimWords )
00225 {
00226 unsigned * pInfo, * pInfo1;
00227 int k, fComp1;
00228
00229 assert( Abc_ObjIsCo(pNode) );
00230 pInfo = Vec_PtrEntry(vSimInfo, pNode->Id);
00231 pInfo1 = Vec_PtrEntry(vSimInfo, Abc_ObjFaninId0(pNode));
00232 fComp1 = Abc_ObjFaninC0(pNode);
00233 if ( fComp1 )
00234 for ( k = 0; k < nSimWords; k++ )
00235 pInfo[k] = ~pInfo1[k];
00236 else
00237 for ( k = 0; k < nSimWords; k++ )
00238 pInfo[k] = pInfo1[k];
00239 }
00240
00252 void Res_SimPerformRound( Res_Sim_t * p )
00253 {
00254 Abc_Obj_t * pObj;
00255 int i;
00256 Abc_InfoFill( Vec_PtrEntry(p->vPats,0), p->nWords );
00257 Abc_AigForEachAnd( p->pAig, pObj, i )
00258 Res_SimPerformOne( pObj, p->vPats, p->nWords );
00259 Abc_NtkForEachPo( p->pAig, pObj, i )
00260 Res_SimTransferOne( pObj, p->vPats, p->nWords );
00261 }
00262
00274 void Res_SimProcessPats( Res_Sim_t * p )
00275 {
00276 Abc_Obj_t * pObj;
00277 unsigned * pInfoCare, * pInfoNode;
00278 int i, j, nDcs = 0;
00279 pInfoCare = Vec_PtrEntry( p->vPats, Abc_NtkPo(p->pAig, 0)->Id );
00280 pInfoNode = Vec_PtrEntry( p->vPats, Abc_NtkPo(p->pAig, 1)->Id );
00281 for ( i = 0; i < p->nPats; i++ )
00282 {
00283
00284 if ( !Abc_InfoHasBit(pInfoCare, i) )
00285 {
00286 nDcs++;
00287 continue;
00288 }
00289
00290 if ( !Abc_InfoHasBit(pInfoNode, i) )
00291 {
00292 if ( p->nPats0 >= p->nPats )
00293 continue;
00294 Abc_NtkForEachPi( p->pAig, pObj, j )
00295 if ( Abc_InfoHasBit( Vec_PtrEntry(p->vPats, pObj->Id), i ) )
00296 Abc_InfoSetBit( Vec_PtrEntry(p->vPats0, j), p->nPats0 );
00297 p->nPats0++;
00298 }
00299 else
00300 {
00301 if ( p->nPats1 >= p->nPats )
00302 continue;
00303 Abc_NtkForEachPi( p->pAig, pObj, j )
00304 if ( Abc_InfoHasBit( Vec_PtrEntry(p->vPats, pObj->Id), i ) )
00305 Abc_InfoSetBit( Vec_PtrEntry(p->vPats1, j), p->nPats1 );
00306 p->nPats1++;
00307 }
00308 }
00309 }
00310
00322 void Res_SimPadSimInfo( Vec_Ptr_t * vPats, int nPats, int nWords )
00323 {
00324 unsigned * pInfo;
00325 int i, w, iWords;
00326 assert( nPats > 0 && nPats < nWords * 8 * (int) sizeof(unsigned) );
00327
00328 if ( nPats < 8 * sizeof(unsigned) )
00329 {
00330 Vec_PtrForEachEntry( vPats, pInfo, i )
00331 if ( pInfo[0] & 1 )
00332 pInfo[0] |= ((~0) << nPats);
00333 nPats = 8 * sizeof(unsigned);
00334 }
00335
00336 iWords = nPats / (8 * sizeof(unsigned));
00337 Vec_PtrForEachEntry( vPats, pInfo, i )
00338 {
00339 for ( w = iWords; w < nWords; w++ )
00340 pInfo[w] = pInfo[0];
00341 }
00342 }
00343
00355 void Res_SimDeriveInfoReplicate( Res_Sim_t * p )
00356 {
00357 unsigned * pInfo, * pInfo2;
00358 Abc_Obj_t * pObj;
00359 int i, j, w;
00360 Abc_NtkForEachPo( p->pAig, pObj, i )
00361 {
00362 pInfo = Vec_PtrEntry( p->vPats, pObj->Id );
00363 pInfo2 = Vec_PtrEntry( p->vOuts, i );
00364 for ( j = 0; j < p->nPats; j++ )
00365 for ( w = 0; w < p->nWords; w++ )
00366 *pInfo2++ = pInfo[w];
00367 }
00368 }
00369
00381 void Res_SimDeriveInfoComplement( Res_Sim_t * p )
00382 {
00383 unsigned * pInfo, * pInfo2;
00384 Abc_Obj_t * pObj;
00385 int i, j, w;
00386 Abc_NtkForEachPo( p->pAig, pObj, i )
00387 {
00388 pInfo = Vec_PtrEntry( p->vPats, pObj->Id );
00389 pInfo2 = Vec_PtrEntry( p->vOuts, i );
00390 for ( j = 0; j < p->nPats; j++, pInfo2 += p->nWords )
00391 if ( Abc_InfoHasBit( pInfo, j ) )
00392 for ( w = 0; w < p->nWords; w++ )
00393 pInfo2[w] = ~pInfo2[w];
00394 }
00395 }
00396
00408 void Res_SimReportOne( Res_Sim_t * p )
00409 {
00410 unsigned * pInfoCare, * pInfoNode;
00411 int i, nDcs, nOnes, nZeros;
00412 pInfoCare = Vec_PtrEntry( p->vPats, Abc_NtkPo(p->pAig, 0)->Id );
00413 pInfoNode = Vec_PtrEntry( p->vPats, Abc_NtkPo(p->pAig, 1)->Id );
00414 nDcs = nOnes = nZeros = 0;
00415 for ( i = 0; i < p->nPats; i++ )
00416 {
00417
00418 if ( !Abc_InfoHasBit(pInfoCare, i) )
00419 {
00420 nDcs++;
00421 continue;
00422 }
00423
00424 if ( !Abc_InfoHasBit(pInfoNode, i) )
00425 nZeros++;
00426 else
00427 nOnes++;
00428 }
00429 printf( "On = %3d (%7.2f %%) ", nOnes, 100.0*nOnes/p->nPats );
00430 printf( "Off = %3d (%7.2f %%) ", nZeros, 100.0*nZeros/p->nPats );
00431 printf( "Dc = %3d (%7.2f %%) ", nDcs, 100.0*nDcs/p->nPats );
00432 printf( "P0 = %3d ", p->nPats0 );
00433 printf( "P1 = %3d ", p->nPats1 );
00434 if ( p->nPats0 < 4 || p->nPats1 < 4 )
00435 printf( "*" );
00436 printf( "\n" );
00437 }
00438
00450 void Res_SimPrintOutPatterns( Res_Sim_t * p, Abc_Ntk_t * pAig )
00451 {
00452 Abc_Obj_t * pObj;
00453 unsigned * pInfo2;
00454 int i;
00455 Abc_NtkForEachPo( pAig, pObj, i )
00456 {
00457 pInfo2 = Vec_PtrEntry( p->vOuts, i );
00458 Extra_PrintBinary( stdout, pInfo2, p->nPatsOut );
00459 printf( "\n" );
00460 }
00461 }
00462
00474 int Res_SimPrepare( Res_Sim_t * p, Abc_Ntk_t * pAig, int nTruePis, int fVerbose )
00475 {
00476 int Limit;
00477
00478 Res_SimAdjust( p, pAig );
00479
00480 for ( Limit = 0; Limit < 10; Limit++ )
00481 {
00482 Res_SimSetRandom( p );
00483 Res_SimPerformRound( p );
00484 Res_SimProcessPats( p );
00485 if ( !(p->nPats0 < p->nPats || p->nPats1 < p->nPats) )
00486 break;
00487 }
00488
00489
00490
00491
00492
00493
00494 if ( p->nPats0 < 4 || p->nPats1 < 4 )
00495 {
00496
00497 return 0;
00498 }
00499
00500 if ( p->nPats0 < p->nPats )
00501 Res_SimPadSimInfo( p->vPats0, p->nPats0, p->nWords );
00502 if ( p->nPats1 < p->nPats )
00503 Res_SimPadSimInfo( p->vPats1, p->nPats1, p->nWords );
00504
00505 Res_SimSetGiven( p, p->vPats0 );
00506 Res_SimPerformRound( p );
00507 Res_SimDeriveInfoReplicate( p );
00508
00509 Res_SimSetGiven( p, p->vPats1 );
00510 Res_SimPerformRound( p );
00511 Res_SimDeriveInfoComplement( p );
00512
00513
00514 return 1;
00515 }
00516
00520
00521