00001
00019 #ifndef __FRAIG_INT_H__
00020 #define __FRAIG_INT_H__
00021
00025
00026
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <assert.h>
00031 #include <time.h>
00032
00033 #include "fraig.h"
00034 #include "msat.h"
00035
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00052
00053
00054 #define FRAIG_ENABLE_FANOUTS
00055 #define FRAIG_PATTERNS_RANDOM 2048 // should not be less than 128 and more than 32768 (2^15)
00056 #define FRAIG_PATTERNS_DYNAMIC 2048 // should not be less than 256 and more than 32768 (2^15)
00057 #define FRAIG_MAX_PRIMES 1024 // the maximum number of primes used for hashing
00058
00059
00060
00061
00062
00063
00064 #define FRAIG_WORDS_STORE 5
00065
00066
00067 #define FRAIG_MASK(n) ((~((unsigned)0)) >> (32-(n)))
00068 #define FRAIG_FULL (~((unsigned)0))
00069 #define FRAIG_NUM_WORDS(n) (((n)>>5) + (((n)&31) > 0))
00070
00071
00072 #define FRAIG_MIN(a,b) (((a) < (b))? (a) : (b))
00073 #define FRAIG_MAX(a,b) (((a) > (b))? (a) : (b))
00074
00075
00076 #define FRAIG_RANDOM_UNSIGNED ((((unsigned)rand()) << 24) ^ (((unsigned)rand()) << 12) ^ ((unsigned)rand()))
00077
00078
00079 #define Fraig_BitStringSetBit(p,i) ((p)[(i)>>5] |= (1<<((i) & 31)))
00080 #define Fraig_BitStringXorBit(p,i) ((p)[(i)>>5] ^= (1<<((i) & 31)))
00081 #define Fraig_BitStringHasBit(p,i) (((p)[(i)>>5] & (1<<((i) & 31))) > 0)
00082
00083
00084
00085
00086 #define Fraig_NodeSetVarStr(p,i) Fraig_BitStringSetBit(Fraig_Regular(p)->pSuppStr,i)
00087 #define Fraig_NodeHasVarStr(p,i) Fraig_BitStringHasBit(Fraig_Regular(p)->pSuppStr,i)
00088
00089
00090 #ifndef ALLOC
00091 # define ALLOC(type, num) \
00092 ((type *) malloc(sizeof(type) * (num)))
00093 #endif
00094
00095 #ifndef REALLOC
00096 # define REALLOC(type, obj, num) \
00097 (obj) ? ((type *) realloc((char *) obj, sizeof(type) * (num))) : \
00098 ((type *) malloc(sizeof(type) * (num)))
00099 #endif
00100
00101 #ifndef FREE
00102 # define FREE(obj) \
00103 ((obj) ? (free((char *) (obj)), (obj) = 0) : 0)
00104 #endif
00105
00106
00107 #define Fraig_PrintTime(a,t) printf( "%s = ", (a) ); printf( "%6.2f sec\n", (float)(t)/(float)(CLOCKS_PER_SEC) )
00108
00109 #define Fraig_HashKey2(a,b,TSIZE) (((unsigned)(a) + (unsigned)(b) * 12582917) % TSIZE)
00110
00111
00112
00113
00114 #ifndef PRT
00115 #define PRT(a,t) printf( "%s = ", (a) ); printf( "%6.2f sec\n", (float)(t)/(float)(CLOCKS_PER_SEC) )
00116 #endif
00117
00121
00122 typedef struct Fraig_MemFixed_t_ Fraig_MemFixed_t;
00123
00124
00125 struct Fraig_ManStruct_t_
00126 {
00127
00128 Fraig_NodeVec_t * vInputs;
00129 Fraig_NodeVec_t * vNodes;
00130 Fraig_NodeVec_t * vOutputs;
00131 Fraig_Node_t * pConst1;
00132
00133
00134 char ** ppInputNames;
00135 char ** ppOutputNames;
00136
00137
00138 Fraig_HashTable_t * pTableS;
00139 Fraig_HashTable_t * pTableF;
00140 Fraig_HashTable_t * pTableF0;
00141
00142
00143 int nWordsRand;
00144 int nWordsDyna;
00145 int nBTLimit;
00146 int nSeconds;
00147 int fFuncRed;
00148 int fFeedBack;
00149 int fDist1Pats;
00150 int fDoSparse;
00151 int fChoicing;
00152 int fTryProve;
00153 int fVerbose;
00154 int fVerboseP;
00155 sint64 nInspLimit;
00156
00157 int nTravIds;
00158 int nTravIds2;
00159
00160
00161 int iWordStart;
00162 int iWordPerm;
00163 int iPatsPerm;
00164 Fraig_NodeVec_t * vCones;
00165 Msat_IntVec_t * vPatsReal;
00166 unsigned * pSimsReal;
00167 unsigned * pSimsDiff;
00168 unsigned * pSimsTemp;
00169
00170
00171 int nSuppWords;
00172 unsigned ** pSuppS;
00173 unsigned ** pSuppF;
00174
00175
00176 Fraig_MemFixed_t * mmNodes;
00177 Fraig_MemFixed_t * mmSims;
00178
00179
00180 Msat_Solver_t * pSat;
00181 Msat_IntVec_t * vProj;
00182 int nSatNums;
00183 int * pModel;
00184
00185 Msat_IntVec_t * vVarsInt;
00186 Msat_ClauseVec_t * vAdjacents;
00187 Msat_IntVec_t * vVarsUsed;
00188
00189
00190 int nSatCalls;
00191 int nSatProof;
00192 int nSatCounter;
00193 int nSatFails;
00194 int nSatFailsReal;
00195
00196 int nSatCallsImp;
00197 int nSatProofImp;
00198 int nSatCounterImp;
00199 int nSatFailsImp;
00200
00201 int nSatZeros;
00202 int nSatSupps;
00203 int nRefErrors;
00204 int nImplies;
00205 int nSatImpls;
00206 int nVarsClauses;
00207 int nSimplifies0;
00208 int nSimplifies1;
00209 int nImplies0;
00210 int nImplies1;
00211
00212
00213 int timeToAig;
00214 int timeSims;
00215 int timeTrav;
00216 int timeFeed;
00217 int timeImply;
00218 int timeSat;
00219 int timeToNet;
00220 int timeTotal;
00221 int time1;
00222 int time2;
00223 int time3;
00224 int time4;
00225 };
00226
00227
00228 struct Fraig_NodeStruct_t_
00229 {
00230
00231 int Num;
00232 int NumPi;
00233 int Level;
00234 int nRefs;
00235 int TravId;
00236 int TravId2;
00237
00238
00239 unsigned fInv : 1;
00240 unsigned fNodePo : 1;
00241 unsigned fClauses : 1;
00242 unsigned fMark0 : 1;
00243 unsigned fMark1 : 1;
00244 unsigned fMark2 : 1;
00245 unsigned fMark3 : 1;
00246 unsigned fFeedUse : 1;
00247 unsigned fFeedVal : 1;
00248 unsigned fFailTfo : 1;
00249 unsigned nFanouts : 2;
00250 unsigned nOnes : 20;
00251
00252
00253 Fraig_Node_t * p1;
00254 Fraig_Node_t * p2;
00255 Fraig_NodeVec_t * vFanins;
00256
00257
00258
00259 Fraig_Node_t * pNextS;
00260 Fraig_Node_t * pNextF;
00261 Fraig_Node_t * pNextD;
00262 Fraig_Node_t * pNextE;
00263 Fraig_Node_t * pRepr;
00264
00265
00266 unsigned uHashR;
00267 unsigned uHashD;
00268 unsigned * puSimR;
00269 unsigned * puSimD;
00270
00271
00272 Fraig_Node_t * pData0;
00273 Fraig_Node_t * pData1;
00274
00275 #ifdef FRAIG_ENABLE_FANOUTS
00276
00277 Fraig_Node_t * pFanPivot;
00278 Fraig_Node_t * pFanFanin1;
00279 Fraig_Node_t * pFanFanin2;
00280 #endif
00281 };
00282
00283
00284 struct Fraig_NodeVecStruct_t_
00285 {
00286 int nCap;
00287 int nSize;
00288 Fraig_Node_t ** pArray;
00289 };
00290
00291
00292 struct Fraig_HashTableStruct_t_
00293 {
00294 Fraig_Node_t ** pBins;
00295 int nBins;
00296 int nEntries;
00297 };
00298
00299
00300 #define Fraig_NodeReadNextFanout( pNode, pFanout ) \
00301 ( ( pFanout == NULL )? NULL : \
00302 ((Fraig_Regular((pFanout)->p1) == (pNode))? \
00303 (pFanout)->pFanFanin1 : (pFanout)->pFanFanin2) )
00304
00305 #define Fraig_NodeReadNextFanoutPlace( pNode, pFanout ) \
00306 ( (Fraig_Regular((pFanout)->p1) == (pNode))? \
00307 &(pFanout)->pFanFanin1 : &(pFanout)->pFanFanin2 )
00308
00309 #define Fraig_NodeForEachFanout( pNode, pFanout ) \
00310 for ( pFanout = (pNode)->pFanPivot; pFanout; \
00311 pFanout = Fraig_NodeReadNextFanout(pNode, pFanout) )
00312
00313 #define Fraig_NodeForEachFanoutSafe( pNode, pFanout, pFanout2 ) \
00314 for ( pFanout = (pNode)->pFanPivot, \
00315 pFanout2 = Fraig_NodeReadNextFanout(pNode, pFanout); \
00316 pFanout; \
00317 pFanout = pFanout2, \
00318 pFanout2 = Fraig_NodeReadNextFanout(pNode, pFanout) )
00319
00320
00321
00322 #define Fraig_TableBinForEachEntryS( pBin, pEnt ) \
00323 for ( pEnt = pBin; \
00324 pEnt; \
00325 pEnt = pEnt->pNextS )
00326 #define Fraig_TableBinForEachEntrySafeS( pBin, pEnt, pEnt2 ) \
00327 for ( pEnt = pBin, \
00328 pEnt2 = pEnt? pEnt->pNextS: NULL; \
00329 pEnt; \
00330 pEnt = pEnt2, \
00331 pEnt2 = pEnt? pEnt->pNextS: NULL )
00332
00333 #define Fraig_TableBinForEachEntryF( pBin, pEnt ) \
00334 for ( pEnt = pBin; \
00335 pEnt; \
00336 pEnt = pEnt->pNextF )
00337 #define Fraig_TableBinForEachEntrySafeF( pBin, pEnt, pEnt2 ) \
00338 for ( pEnt = pBin, \
00339 pEnt2 = pEnt? pEnt->pNextF: NULL; \
00340 pEnt; \
00341 pEnt = pEnt2, \
00342 pEnt2 = pEnt? pEnt->pNextF: NULL )
00343
00344 #define Fraig_TableBinForEachEntryD( pBin, pEnt ) \
00345 for ( pEnt = pBin; \
00346 pEnt; \
00347 pEnt = pEnt->pNextD )
00348 #define Fraig_TableBinForEachEntrySafeD( pBin, pEnt, pEnt2 ) \
00349 for ( pEnt = pBin, \
00350 pEnt2 = pEnt? pEnt->pNextD: NULL; \
00351 pEnt; \
00352 pEnt = pEnt2, \
00353 pEnt2 = pEnt? pEnt->pNextD: NULL )
00354
00355 #define Fraig_TableBinForEachEntryE( pBin, pEnt ) \
00356 for ( pEnt = pBin; \
00357 pEnt; \
00358 pEnt = pEnt->pNextE )
00359 #define Fraig_TableBinForEachEntrySafeE( pBin, pEnt, pEnt2 ) \
00360 for ( pEnt = pBin, \
00361 pEnt2 = pEnt? pEnt->pNextE: NULL; \
00362 pEnt; \
00363 pEnt = pEnt2, \
00364 pEnt2 = pEnt? pEnt->pNextE: NULL )
00365
00369
00373
00374
00375 extern Fraig_Node_t * Fraig_NodeAndCanon( Fraig_Man_t * pMan, Fraig_Node_t * p1, Fraig_Node_t * p2 );
00376
00377 extern void Fraig_NodeAddFaninFanout( Fraig_Node_t * pFanin, Fraig_Node_t * pFanout );
00378 extern void Fraig_NodeRemoveFaninFanout( Fraig_Node_t * pFanin, Fraig_Node_t * pFanoutToRemove );
00379 extern int Fraig_NodeGetFanoutNum( Fraig_Node_t * pNode );
00380
00381 extern void Fraig_FeedBackInit( Fraig_Man_t * p );
00382 extern void Fraig_FeedBack( Fraig_Man_t * p, int * pModel, Msat_IntVec_t * vVars, Fraig_Node_t * pOld, Fraig_Node_t * pNew );
00383 extern void Fraig_FeedBackTest( Fraig_Man_t * p );
00384 extern int Fraig_FeedBackCompress( Fraig_Man_t * p );
00385 extern int * Fraig_ManAllocCounterExample( Fraig_Man_t * p );
00386 extern int * Fraig_ManSaveCounterExample( Fraig_Man_t * p, Fraig_Node_t * pNode );
00387
00388 extern void Fraig_ManCreateSolver( Fraig_Man_t * p );
00389
00390 extern Fraig_MemFixed_t * Fraig_MemFixedStart( int nEntrySize );
00391 extern void Fraig_MemFixedStop( Fraig_MemFixed_t * p, int fVerbose );
00392 extern char * Fraig_MemFixedEntryFetch( Fraig_MemFixed_t * p );
00393 extern void Fraig_MemFixedEntryRecycle( Fraig_MemFixed_t * p, char * pEntry );
00394 extern void Fraig_MemFixedRestart( Fraig_MemFixed_t * p );
00395 extern int Fraig_MemFixedReadMemUsage( Fraig_MemFixed_t * p );
00396
00397 extern Fraig_Node_t * Fraig_NodeCreateConst( Fraig_Man_t * p );
00398 extern Fraig_Node_t * Fraig_NodeCreatePi( Fraig_Man_t * p );
00399 extern Fraig_Node_t * Fraig_NodeCreate( Fraig_Man_t * p, Fraig_Node_t * p1, Fraig_Node_t * p2 );
00400 extern void Fraig_NodeSimulate( Fraig_Node_t * pNode, int iWordStart, int iWordStop, int fUseRand );
00401
00402 extern int s_FraigPrimes[FRAIG_MAX_PRIMES];
00403 extern unsigned int Cudd_PrimeFraig( unsigned int p );
00404
00405 extern int Fraig_NodeIsImplication( Fraig_Man_t * p, Fraig_Node_t * pOld, Fraig_Node_t * pNew, int nBTLimit );
00406
00407 extern Fraig_HashTable_t * Fraig_HashTableCreate( int nSize );
00408 extern void Fraig_HashTableFree( Fraig_HashTable_t * p );
00409 extern int Fraig_HashTableLookupS( Fraig_Man_t * pMan, Fraig_Node_t * p1, Fraig_Node_t * p2, Fraig_Node_t ** ppNodeRes );
00410 extern Fraig_Node_t * Fraig_HashTableLookupF( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
00411 extern Fraig_Node_t * Fraig_HashTableLookupF0( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
00412 extern void Fraig_HashTableInsertF0( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
00413 extern int Fraig_CompareSimInfo( Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int iWordLast, int fUseRand );
00414 extern int Fraig_CompareSimInfoUnderMask( Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int iWordLast, int fUseRand, unsigned * puMask );
00415 extern int Fraig_FindFirstDiff( Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int fCompl, int iWordLast, int fUseRand );
00416 extern void Fraig_CollectXors( Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int iWordLast, int fUseRand, unsigned * puMask );
00417 extern void Fraig_TablePrintStatsS( Fraig_Man_t * pMan );
00418 extern void Fraig_TablePrintStatsF( Fraig_Man_t * pMan );
00419 extern void Fraig_TablePrintStatsF0( Fraig_Man_t * pMan );
00420 extern int Fraig_TableRehashF0( Fraig_Man_t * pMan, int fLinkEquiv );
00421
00422 extern int Fraig_NodeCountPis( Msat_IntVec_t * vVars, int nVarsPi );
00423 extern int Fraig_NodeCountSuppVars( Fraig_Man_t * p, Fraig_Node_t * pNode, int fSuppStr );
00424 extern int Fraig_NodesCompareSupps( Fraig_Man_t * p, Fraig_Node_t * pOld, Fraig_Node_t * pNew );
00425 extern int Fraig_NodeAndSimpleCase_rec( Fraig_Node_t * pOld, Fraig_Node_t * pNew );
00426 extern int Fraig_NodeIsExorType( Fraig_Node_t * pNode );
00427 extern void Fraig_ManSelectBestChoice( Fraig_Man_t * p );
00428 extern int Fraig_BitStringCountOnes( unsigned * pString, int nWords );
00429 extern void Fraig_PrintBinary( FILE * pFile, unsigned * pSign, int nBits );
00430 extern int Fraig_NodeIsExorType( Fraig_Node_t * pNode );
00431 extern int Fraig_NodeIsExor( Fraig_Node_t * pNode );
00432 extern int Fraig_NodeIsMuxType( Fraig_Node_t * pNode );
00433 extern Fraig_Node_t * Fraig_NodeRecognizeMux( Fraig_Node_t * pNode, Fraig_Node_t ** ppNodeT, Fraig_Node_t ** ppNodeE );
00434 extern int Fraig_ManCountExors( Fraig_Man_t * pMan );
00435 extern int Fraig_ManCountMuxes( Fraig_Man_t * pMan );
00436 extern int Fraig_NodeSimsContained( Fraig_Man_t * pMan, Fraig_Node_t * pNode1, Fraig_Node_t * pNode2 );
00437 extern int Fraig_NodeIsInSupergate( Fraig_Node_t * pOld, Fraig_Node_t * pNew );
00438 extern Fraig_NodeVec_t * Fraig_CollectSupergate( Fraig_Node_t * pNode, int fStopAtMux );
00439 extern int Fraig_CountPis( Fraig_Man_t * p, Msat_IntVec_t * vVarNums );
00440 extern void Fraig_ManIncrementTravId( Fraig_Man_t * pMan );
00441 extern void Fraig_NodeSetTravIdCurrent( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
00442 extern int Fraig_NodeIsTravIdCurrent( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
00443 extern int Fraig_NodeIsTravIdPrevious( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
00444
00445 extern void Fraig_NodeVecSortByRefCount( Fraig_NodeVec_t * p );
00446
00447 #endif
00448