summaryrefslogtreecommitdiffstats
path: root/src/sat
diff options
context:
space:
mode:
authorAlan Mishchenko <alanmi@berkeley.edu>2007-10-01 08:01:00 -0700
committerAlan Mishchenko <alanmi@berkeley.edu>2007-10-01 08:01:00 -0700
commit4812c90424dfc40d26725244723887a2d16ddfd9 (patch)
treeb32ace96e7e2d84d586e09ba605463b6f49c3271 /src/sat
parente54d9691616b9a0326e2fdb3156bb4eeb8abfcd7 (diff)
downloadabc-4812c90424dfc40d26725244723887a2d16ddfd9.tar.gz
abc-4812c90424dfc40d26725244723887a2d16ddfd9.tar.bz2
abc-4812c90424dfc40d26725244723887a2d16ddfd9.zip
Version abc71001
Diffstat (limited to 'src/sat')
-rw-r--r--src/sat/bsat/module.make6
-rw-r--r--src/sat/bsat/satInter.c991
-rw-r--r--src/sat/bsat/satMem.c527
-rw-r--r--src/sat/bsat/satMem.h80
-rw-r--r--src/sat/bsat/satSolver.c1358
-rw-r--r--src/sat/bsat/satSolver.h191
-rw-r--r--src/sat/bsat/satStore.c437
-rw-r--r--src/sat/bsat/satStore.h137
-rw-r--r--src/sat/bsat/satTrace.c109
-rw-r--r--src/sat/bsat/satUtil.c234
-rw-r--r--src/sat/bsat/satVec.h83
-rw-r--r--src/sat/csat/csat_apis.c769
-rw-r--r--src/sat/csat/csat_apis.h222
-rw-r--r--src/sat/csat/module.make1
-rw-r--r--src/sat/fraig/fraig.h267
-rw-r--r--src/sat/fraig/fraigApi.c297
-rw-r--r--src/sat/fraig/fraigCanon.c218
-rw-r--r--src/sat/fraig/fraigChoice.c241
-rw-r--r--src/sat/fraig/fraigFanout.c175
-rw-r--r--src/sat/fraig/fraigFeed.c909
-rw-r--r--src/sat/fraig/fraigInt.h451
-rw-r--r--src/sat/fraig/fraigMan.c540
-rw-r--r--src/sat/fraig/fraigMem.c246
-rw-r--r--src/sat/fraig/fraigNode.c313
-rw-r--r--src/sat/fraig/fraigPrime.c144
-rw-r--r--src/sat/fraig/fraigSat.c1455
-rw-r--r--src/sat/fraig/fraigTable.c657
-rw-r--r--src/sat/fraig/fraigUtil.c1034
-rw-r--r--src/sat/fraig/fraigVec.c545
-rw-r--r--src/sat/fraig/module.make12
-rw-r--r--src/sat/msat/module.make13
-rw-r--r--src/sat/msat/msat.h172
-rw-r--r--src/sat/msat/msatActivity.c160
-rw-r--r--src/sat/msat/msatClause.c529
-rw-r--r--src/sat/msat/msatClauseVec.c232
-rw-r--r--src/sat/msat/msatInt.h306
-rw-r--r--src/sat/msat/msatMem.c529
-rw-r--r--src/sat/msat/msatOrderH.c405
-rw-r--r--src/sat/msat/msatOrderJ.c472
-rw-r--r--src/sat/msat/msatQueue.c157
-rw-r--r--src/sat/msat/msatRead.c268
-rw-r--r--src/sat/msat/msatSolverApi.c500
-rw-r--r--src/sat/msat/msatSolverCore.c212
-rw-r--r--src/sat/msat/msatSolverIo.c177
-rw-r--r--src/sat/msat/msatSolverSearch.c629
-rw-r--r--src/sat/msat/msatSort.c173
-rw-r--r--src/sat/msat/msatVec.c495
-rw-r--r--src/sat/proof/pr.c1263
-rw-r--r--src/sat/proof/pr.h65
-rw-r--r--src/sat/proof/stats.txt66
50 files changed, 19472 insertions, 0 deletions
diff --git a/src/sat/bsat/module.make b/src/sat/bsat/module.make
new file mode 100644
index 00000000..563c8dfc
--- /dev/null
+++ b/src/sat/bsat/module.make
@@ -0,0 +1,6 @@
+SRC += src/sat/bsat/satMem.c \
+ src/sat/bsat/satInter.c \
+ src/sat/bsat/satSolver.c \
+ src/sat/bsat/satStore.c \
+ src/sat/bsat/satTrace.c \
+ src/sat/bsat/satUtil.c
diff --git a/src/sat/bsat/satInter.c b/src/sat/bsat/satInter.c
new file mode 100644
index 00000000..8759aa09
--- /dev/null
+++ b/src/sat/bsat/satInter.c
@@ -0,0 +1,991 @@
+/**CFile****************************************************************
+
+ FileName [satInter.c]
+
+ SystemName [ABC: Logic synthesis and verification system.]
+
+ PackageName [SAT sat_solver.]
+
+ Synopsis [Interpolation package.]
+
+ Author [Alan Mishchenko]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - June 20, 2005.]
+
+ Revision [$Id: satInter.c,v 1.4 2005/09/16 22:55:03 casem Exp $]
+
+***********************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+#include "satStore.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+// variable assignments
+static const lit LIT_UNDEF = 0xffffffff;
+
+// interpolation manager
+struct Int_Man_t_
+{
+ // clauses of the problems
+ Sto_Man_t * pCnf; // the set of CNF clauses for A and B
+ // various parameters
+ int fVerbose; // verbosiness flag
+ int fProofVerif; // verifies the proof
+ int fProofWrite; // writes the proof file
+ int nVarsAlloc; // the allocated size of var arrays
+ int nClosAlloc; // the allocated size of clause arrays
+ // internal BCP
+ int nRootSize; // the number of root level assignments
+ int nTrailSize; // the number of assignments made
+ lit * pTrail; // chronological order of assignments (size nVars)
+ lit * pAssigns; // assignments by variable (size nVars)
+ char * pSeens; // temporary mark (size nVars)
+ Sto_Cls_t ** pReasons; // reasons for each assignment (size nVars)
+ Sto_Cls_t ** pWatches; // watched clauses for each literal (size 2*nVars)
+ // interpolation data
+ int nVarsAB; // the number of global variables
+ char * pVarTypes; // variable type (size nVars) [1=A, 0=B, <0=AB]
+ unsigned * pInters; // storage for interpolants as truth tables (size nClauses)
+ int nIntersAlloc; // the allocated size of truth table array
+ int nWords; // the number of words in the truth table
+ // proof recording
+ int Counter; // counter of resolved clauses
+ int * pProofNums; // the proof numbers for each clause (size nClauses)
+ FILE * pFile; // the file for proof recording
+ // internal verification
+ lit * pResLits; // the literals of the resolvent
+ int nResLits; // the number of literals of the resolvent
+ int nResLitsAlloc;// the number of literals of the resolvent
+ // runtime stats
+ int timeBcp; // the runtime for BCP
+ int timeTrace; // the runtime of trace construction
+ int timeTotal; // the total runtime of interpolation
+};
+
+// procedure to get hold of the clauses' truth table
+static inline unsigned * Int_ManTruthRead( Int_Man_t * p, Sto_Cls_t * pCls ) { return p->pInters + pCls->Id * p->nWords; }
+static inline void Int_ManTruthClear( unsigned * p, int nWords ) { int i; for ( i = nWords - 1; i >= 0; i-- ) p[i] = 0; }
+static inline void Int_ManTruthFill( unsigned * p, int nWords ) { int i; for ( i = nWords - 1; i >= 0; i-- ) p[i] = ~0; }
+static inline void Int_ManTruthCopy( unsigned * p, unsigned * q, int nWords ) { int i; for ( i = nWords - 1; i >= 0; i-- ) p[i] = q[i]; }
+static inline void Int_ManTruthAnd( unsigned * p, unsigned * q, int nWords ) { int i; for ( i = nWords - 1; i >= 0; i-- ) p[i] &= q[i]; }
+static inline void Int_ManTruthOr( unsigned * p, unsigned * q, int nWords ) { int i; for ( i = nWords - 1; i >= 0; i-- ) p[i] |= q[i]; }
+static inline void Int_ManTruthOrNot( unsigned * p, unsigned * q, int nWords ) { int i; for ( i = nWords - 1; i >= 0; i-- ) p[i] |= ~q[i]; }
+
+// reading/writing the proof for a clause
+static inline int Int_ManProofGet( Int_Man_t * p, Sto_Cls_t * pCls ) { return p->pProofNums[pCls->Id]; }
+static inline void Int_ManProofSet( Int_Man_t * p, Sto_Cls_t * pCls, int n ) { p->pProofNums[pCls->Id] = n; }
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Allocate proof manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Int_Man_t * Int_ManAlloc( int nVarsAlloc )
+{
+ Int_Man_t * p;
+ // allocate the manager
+ p = (Int_Man_t *)malloc( sizeof(Int_Man_t) );
+ memset( p, 0, sizeof(Int_Man_t) );
+ // verification
+ p->nResLitsAlloc = (1<<16);
+ p->pResLits = malloc( sizeof(lit) * p->nResLitsAlloc );
+ // parameters
+ p->fProofWrite = 0;
+ p->fProofVerif = 1;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Count common variables in the clauses of A and B.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Int_ManGlobalVars( Int_Man_t * p )
+{
+ Sto_Cls_t * pClause;
+ int Var, nVarsAB, v;
+
+ // mark the variable encountered in the clauses of A
+ Sto_ManForEachClauseRoot( p->pCnf, pClause )
+ {
+ if ( !pClause->fA )
+ break;
+ for ( v = 0; v < (int)pClause->nLits; v++ )
+ p->pVarTypes[lit_var(pClause->pLits[v])] = 1;
+ }
+
+ // check variables that appear in clauses of B
+ nVarsAB = 0;
+ Sto_ManForEachClauseRoot( p->pCnf, pClause )
+ {
+ if ( pClause->fA )
+ continue;
+ for ( v = 0; v < (int)pClause->nLits; v++ )
+ {
+ Var = lit_var(pClause->pLits[v]);
+ if ( p->pVarTypes[Var] == 1 ) // var of A
+ {
+ // change it into a global variable
+ nVarsAB++;
+ p->pVarTypes[Var] = -1;
+ }
+ }
+ }
+
+ // order global variables
+ nVarsAB = 0;
+ for ( v = 0; v < p->pCnf->nVars; v++ )
+ if ( p->pVarTypes[v] == -1 )
+ p->pVarTypes[v] -= nVarsAB++;
+//printf( "There are %d global variables.\n", nVarsAB );
+ return nVarsAB;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Resize proof manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Int_ManResize( Int_Man_t * p )
+{
+ // check if resizing is needed
+ if ( p->nVarsAlloc < p->pCnf->nVars )
+ {
+ // find the new size
+ if ( p->nVarsAlloc == 0 )
+ p->nVarsAlloc = 1;
+ while ( p->nVarsAlloc < p->pCnf->nVars )
+ p->nVarsAlloc *= 2;
+ // resize the arrays
+ p->pTrail = (lit *) realloc( p->pTrail, sizeof(lit) * p->nVarsAlloc );
+ p->pAssigns = (lit *) realloc( p->pAssigns, sizeof(lit) * p->nVarsAlloc );
+ p->pSeens = (char *) realloc( p->pSeens, sizeof(char) * p->nVarsAlloc );
+ p->pVarTypes = (char *) realloc( p->pVarTypes, sizeof(char) * p->nVarsAlloc );
+ p->pReasons = (Sto_Cls_t **)realloc( p->pReasons, sizeof(Sto_Cls_t *) * p->nVarsAlloc );
+ p->pWatches = (Sto_Cls_t **)realloc( p->pWatches, sizeof(Sto_Cls_t *) * p->nVarsAlloc*2 );
+ }
+
+ // clean the free space
+ memset( p->pAssigns , 0xff, sizeof(lit) * p->pCnf->nVars );
+ memset( p->pSeens , 0, sizeof(char) * p->pCnf->nVars );
+ memset( p->pVarTypes, 0, sizeof(char) * p->pCnf->nVars );
+ memset( p->pReasons , 0, sizeof(Sto_Cls_t *) * p->pCnf->nVars );
+ memset( p->pWatches , 0, sizeof(Sto_Cls_t *) * p->pCnf->nVars*2 );
+
+ // compute the number of common variables
+ p->nVarsAB = Int_ManGlobalVars( p );
+ // compute the number of words in the truth table
+ p->nWords = (p->nVarsAB <= 5 ? 1 : (1 << (p->nVarsAB - 5)));
+
+ // check if resizing of clauses is needed
+ if ( p->nClosAlloc < p->pCnf->nClauses )
+ {
+ // find the new size
+ if ( p->nClosAlloc == 0 )
+ p->nClosAlloc = 1;
+ while ( p->nClosAlloc < p->pCnf->nClauses )
+ p->nClosAlloc *= 2;
+ // resize the arrays
+ p->pProofNums = (int *) realloc( p->pProofNums, sizeof(int) * p->nClosAlloc );
+ }
+ memset( p->pProofNums, 0, sizeof(int) * p->pCnf->nClauses );
+
+ // check if resizing of truth tables is needed
+ if ( p->nIntersAlloc < p->nWords * p->pCnf->nClauses )
+ {
+ p->nIntersAlloc = p->nWords * p->pCnf->nClauses;
+ p->pInters = (unsigned *) realloc( p->pInters, sizeof(unsigned) * p->nIntersAlloc );
+ }
+// memset( p->pInters, 0, sizeof(unsigned) * p->nWords * p->pCnf->nClauses );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Deallocate proof manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Int_ManFree( Int_Man_t * p )
+{
+/*
+ printf( "Runtime stats:\n" );
+PRT( "BCP ", p->timeBcp );
+PRT( "Trace ", p->timeTrace );
+PRT( "TOTAL ", p->timeTotal );
+*/
+ free( p->pInters );
+ free( p->pProofNums );
+ free( p->pTrail );
+ free( p->pAssigns );
+ free( p->pSeens );
+ free( p->pVarTypes );
+ free( p->pReasons );
+ free( p->pWatches );
+ free( p->pResLits );
+ free( p );
+}
+
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Prints the clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Int_ManPrintClause( Int_Man_t * p, Sto_Cls_t * pClause )
+{
+ int i;
+ printf( "Clause ID = %d. Proof = %d. {", pClause->Id, Int_ManProofGet(p, pClause) );
+ for ( i = 0; i < (int)pClause->nLits; i++ )
+ printf( " %d", pClause->pLits[i] );
+ printf( " }\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prints the resolvent.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Int_ManPrintResolvent( lit * pResLits, int nResLits )
+{
+ int i;
+ printf( "Resolvent: {" );
+ for ( i = 0; i < nResLits; i++ )
+ printf( " %d", pResLits[i] );
+ printf( " }\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Records the proof.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Extra_PrintBinary__( FILE * pFile, unsigned Sign[], int nBits )
+{
+ int Remainder, nWords;
+ int w, i;
+
+ Remainder = (nBits%(sizeof(unsigned)*8));
+ nWords = (nBits/(sizeof(unsigned)*8)) + (Remainder>0);
+
+ for ( w = nWords-1; w >= 0; w-- )
+ for ( i = ((w == nWords-1 && Remainder)? Remainder-1: 31); i >= 0; i-- )
+ fprintf( pFile, "%c", '0' + (int)((Sign[w] & (1<<i)) > 0) );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prints the interpolant for one clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Int_ManPrintInterOne( Int_Man_t * p, Sto_Cls_t * pClause )
+{
+ printf( "Clause %2d : ", pClause->Id );
+ Extra_PrintBinary__( stdout, Int_ManTruthRead(p, pClause), (1 << p->nVarsAB) );
+ printf( "\n" );
+}
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Adds one clause to the watcher list.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static inline void Int_ManWatchClause( Int_Man_t * p, Sto_Cls_t * pClause, lit Lit )
+{
+ assert( lit_check(Lit, p->pCnf->nVars) );
+ if ( pClause->pLits[0] == Lit )
+ pClause->pNext0 = p->pWatches[lit_neg(Lit)];
+ else
+ {
+ assert( pClause->pLits[1] == Lit );
+ pClause->pNext1 = p->pWatches[lit_neg(Lit)];
+ }
+ p->pWatches[lit_neg(Lit)] = pClause;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Records implication.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static inline int Int_ManEnqueue( Int_Man_t * p, lit Lit, Sto_Cls_t * pReason )
+{
+ int Var = lit_var(Lit);
+ if ( p->pAssigns[Var] != LIT_UNDEF )
+ return p->pAssigns[Var] == Lit;
+ p->pAssigns[Var] = Lit;
+ p->pReasons[Var] = pReason;
+ p->pTrail[p->nTrailSize++] = Lit;
+//printf( "assigning var %d value %d\n", Var, !lit_sign(Lit) );
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Records implication.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static inline void Int_ManCancelUntil( Int_Man_t * p, int Level )
+{
+ lit Lit;
+ int i, Var;
+ for ( i = p->nTrailSize - 1; i >= Level; i-- )
+ {
+ Lit = p->pTrail[i];
+ Var = lit_var( Lit );
+ p->pReasons[Var] = NULL;
+ p->pAssigns[Var] = LIT_UNDEF;
+//printf( "cancelling var %d\n", Var );
+ }
+ p->nTrailSize = Level;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Propagate one assignment.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static inline Sto_Cls_t * Int_ManPropagateOne( Int_Man_t * p, lit Lit )
+{
+ Sto_Cls_t ** ppPrev, * pCur, * pTemp;
+ lit LitF = lit_neg(Lit);
+ int i;
+ // iterate through the literals
+ ppPrev = p->pWatches + Lit;
+ for ( pCur = p->pWatches[Lit]; pCur; pCur = *ppPrev )
+ {
+ // make sure the false literal is in the second literal of the clause
+ if ( pCur->pLits[0] == LitF )
+ {
+ pCur->pLits[0] = pCur->pLits[1];
+ pCur->pLits[1] = LitF;
+ pTemp = pCur->pNext0;
+ pCur->pNext0 = pCur->pNext1;
+ pCur->pNext1 = pTemp;
+ }
+ assert( pCur->pLits[1] == LitF );
+
+ // if the first literal is true, the clause is satisfied
+ if ( pCur->pLits[0] == p->pAssigns[lit_var(pCur->pLits[0])] )
+ {
+ ppPrev = &pCur->pNext1;
+ continue;
+ }
+
+ // look for a new literal to watch
+ for ( i = 2; i < (int)pCur->nLits; i++ )
+ {
+ // skip the case when the literal is false
+ if ( lit_neg(pCur->pLits[i]) == p->pAssigns[lit_var(pCur->pLits[i])] )
+ continue;
+ // the literal is either true or unassigned - watch it
+ pCur->pLits[1] = pCur->pLits[i];
+ pCur->pLits[i] = LitF;
+ // remove this clause from the watch list of Lit
+ *ppPrev = pCur->pNext1;
+ // add this clause to the watch list of pCur->pLits[i] (now it is pCur->pLits[1])
+ Int_ManWatchClause( p, pCur, pCur->pLits[1] );
+ break;
+ }
+ if ( i < (int)pCur->nLits ) // found new watch
+ continue;
+
+ // clause is unit - enqueue new implication
+ if ( Int_ManEnqueue(p, pCur->pLits[0], pCur) )
+ {
+ ppPrev = &pCur->pNext1;
+ continue;
+ }
+
+ // conflict detected - return the conflict clause
+ return pCur;
+ }
+ return NULL;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Propagate the current assignments.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Sto_Cls_t * Int_ManPropagate( Int_Man_t * p, int Start )
+{
+ Sto_Cls_t * pClause;
+ int i;
+ int clk = clock();
+ for ( i = Start; i < p->nTrailSize; i++ )
+ {
+ pClause = Int_ManPropagateOne( p, p->pTrail[i] );
+ if ( pClause )
+ {
+p->timeBcp += clock() - clk;
+ return pClause;
+ }
+ }
+p->timeBcp += clock() - clk;
+ return NULL;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Writes one root clause into a file.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Int_ManProofWriteOne( Int_Man_t * p, Sto_Cls_t * pClause )
+{
+ Int_ManProofSet( p, pClause, ++p->Counter );
+
+ if ( p->fProofWrite )
+ {
+ int v;
+ fprintf( p->pFile, "%d", Int_ManProofGet(p, pClause) );
+ for ( v = 0; v < (int)pClause->nLits; v++ )
+ fprintf( p->pFile, " %d", lit_print(pClause->pLits[v]) );
+ fprintf( p->pFile, " 0 0\n" );
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Traces the proof for one clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Int_ManProofTraceOne( Int_Man_t * p, Sto_Cls_t * pConflict, Sto_Cls_t * pFinal )
+{
+ Sto_Cls_t * pReason;
+ int i, v, Var, PrevId;
+ int fPrint = 0;
+ int clk = clock();
+
+ // collect resolvent literals
+ if ( p->fProofVerif )
+ {
+ assert( (int)pConflict->nLits <= p->nResLitsAlloc );
+ memcpy( p->pResLits, pConflict->pLits, sizeof(lit) * pConflict->nLits );
+ p->nResLits = pConflict->nLits;
+ }
+
+ // mark all the variables in the conflict as seen
+ for ( v = 0; v < (int)pConflict->nLits; v++ )
+ p->pSeens[lit_var(pConflict->pLits[v])] = 1;
+
+ // start the anticedents
+// pFinal->pAntis = Vec_PtrAlloc( 32 );
+// Vec_PtrPush( pFinal->pAntis, pConflict );
+
+ if ( p->pCnf->nClausesA )
+ Int_ManTruthCopy( Int_ManTruthRead(p, pFinal), Int_ManTruthRead(p, pConflict), p->nWords );
+
+ // follow the trail backwards
+ PrevId = Int_ManProofGet(p, pConflict);
+ for ( i = p->nTrailSize - 1; i >= 0; i-- )
+ {
+ // skip literals that are not involved
+ Var = lit_var(p->pTrail[i]);
+ if ( !p->pSeens[Var] )
+ continue;
+ p->pSeens[Var] = 0;
+
+ // skip literals of the resulting clause
+ pReason = p->pReasons[Var];
+ if ( pReason == NULL )
+ continue;
+ assert( p->pTrail[i] == pReason->pLits[0] );
+
+ // add the variables to seen
+ for ( v = 1; v < (int)pReason->nLits; v++ )
+ p->pSeens[lit_var(pReason->pLits[v])] = 1;
+
+
+ // record the reason clause
+ assert( Int_ManProofGet(p, pReason) > 0 );
+ p->Counter++;
+ if ( p->fProofWrite )
+ fprintf( p->pFile, "%d * %d %d 0\n", p->Counter, PrevId, Int_ManProofGet(p, pReason) );
+ PrevId = p->Counter;
+
+ if ( p->pCnf->nClausesA )
+ {
+ if ( p->pVarTypes[Var] == 1 ) // var of A
+ Int_ManTruthOr( Int_ManTruthRead(p, pFinal), Int_ManTruthRead(p, pReason), p->nWords );
+ else
+ Int_ManTruthAnd( Int_ManTruthRead(p, pFinal), Int_ManTruthRead(p, pReason), p->nWords );
+ }
+
+ // resolve the temporary resolvent with the reason clause
+ if ( p->fProofVerif )
+ {
+ int v1, v2;
+ if ( fPrint )
+ Int_ManPrintResolvent( p->pResLits, p->nResLits );
+ // check that the var is present in the resolvent
+ for ( v1 = 0; v1 < p->nResLits; v1++ )
+ if ( lit_var(p->pResLits[v1]) == Var )
+ break;
+ if ( v1 == p->nResLits )
+ printf( "Recording clause %d: Cannot find variable %d in the temporary resolvent.\n", pFinal->Id, Var );
+ if ( p->pResLits[v1] != lit_neg(pReason->pLits[0]) )
+ printf( "Recording clause %d: The resolved variable %d is in the wrong polarity.\n", pFinal->Id, Var );
+ // remove this variable from the resolvent
+ assert( lit_var(p->pResLits[v1]) == Var );
+ p->nResLits--;
+ for ( ; v1 < p->nResLits; v1++ )
+ p->pResLits[v1] = p->pResLits[v1+1];
+ // add variables of the reason clause
+ for ( v2 = 1; v2 < (int)pReason->nLits; v2++ )
+ {
+ for ( v1 = 0; v1 < p->nResLits; v1++ )
+ if ( lit_var(p->pResLits[v1]) == lit_var(pReason->pLits[v2]) )
+ break;
+ // if it is a new variable, add it to the resolvent
+ if ( v1 == p->nResLits )
+ {
+ if ( p->nResLits == p->nResLitsAlloc )
+ printf( "Recording clause %d: Ran out of space for intermediate resolvent.\n", pFinal->Id );
+ p->pResLits[ p->nResLits++ ] = pReason->pLits[v2];
+ continue;
+ }
+ // if the variable is the same, the literal should be the same too
+ if ( p->pResLits[v1] == pReason->pLits[v2] )
+ continue;
+ // the literal is different
+ printf( "Recording clause %d: Trying to resolve the clause with more than one opposite literal.\n", pFinal->Id );
+ }
+ }
+
+// Vec_PtrPush( pFinal->pAntis, pReason );
+ }
+
+ // unmark all seen variables
+// for ( i = p->nTrailSize - 1; i >= 0; i-- )
+// p->pSeens[lit_var(p->pTrail[i])] = 0;
+ // check that the literals are unmarked
+// for ( i = p->nTrailSize - 1; i >= 0; i-- )
+// assert( p->pSeens[lit_var(p->pTrail[i])] == 0 );
+
+ // use the resulting clause to check the correctness of resolution
+ if ( p->fProofVerif )
+ {
+ int v1, v2;
+ if ( fPrint )
+ Int_ManPrintResolvent( p->pResLits, p->nResLits );
+ for ( v1 = 0; v1 < p->nResLits; v1++ )
+ {
+ for ( v2 = 0; v2 < (int)pFinal->nLits; v2++ )
+ if ( pFinal->pLits[v2] == p->pResLits[v1] )
+ break;
+ if ( v2 < (int)pFinal->nLits )
+ continue;
+ break;
+ }
+ if ( v1 < p->nResLits )
+ {
+ printf( "Recording clause %d: The final resolvent is wrong.\n", pFinal->Id );
+ Int_ManPrintClause( p, pConflict );
+ Int_ManPrintResolvent( p->pResLits, p->nResLits );
+ Int_ManPrintClause( p, pFinal );
+ }
+ }
+p->timeTrace += clock() - clk;
+
+ // return the proof pointer
+ if ( p->pCnf->nClausesA )
+ {
+// Int_ManPrintInterOne( p, pFinal );
+ }
+ Int_ManProofSet( p, pFinal, p->Counter );
+ return p->Counter;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Records the proof for one clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Int_ManProofRecordOne( Int_Man_t * p, Sto_Cls_t * pClause )
+{
+ Sto_Cls_t * pConflict;
+ int i;
+
+ // empty clause never ends up there
+ assert( pClause->nLits > 0 );
+ if ( pClause->nLits == 0 )
+ printf( "Error: Empty clause is attempted.\n" );
+
+ // add assumptions to the trail
+ assert( !pClause->fRoot );
+ assert( p->nTrailSize == p->nRootSize );
+ for ( i = 0; i < (int)pClause->nLits; i++ )
+ if ( !Int_ManEnqueue( p, lit_neg(pClause->pLits[i]), NULL ) )
+ {
+ assert( 0 ); // impossible
+ return 0;
+ }
+
+ // propagate the assumptions
+ pConflict = Int_ManPropagate( p, p->nRootSize );
+ if ( pConflict == NULL )
+ {
+ assert( 0 ); // cannot prove
+ return 0;
+ }
+
+ // construct the proof
+ Int_ManProofTraceOne( p, pConflict, pClause );
+
+ // undo to the root level
+ Int_ManCancelUntil( p, p->nRootSize );
+
+ // add large clauses to the watched lists
+ if ( pClause->nLits > 1 )
+ {
+ Int_ManWatchClause( p, pClause, pClause->pLits[0] );
+ Int_ManWatchClause( p, pClause, pClause->pLits[1] );
+ return 1;
+ }
+ assert( pClause->nLits == 1 );
+
+ // if the clause proved is unit, add it and propagate
+ if ( !Int_ManEnqueue( p, pClause->pLits[0], pClause ) )
+ {
+ assert( 0 ); // impossible
+ return 0;
+ }
+
+ // propagate the assumption
+ pConflict = Int_ManPropagate( p, p->nRootSize );
+ if ( pConflict )
+ {
+ // construct the proof
+ Int_ManProofTraceOne( p, pConflict, p->pCnf->pEmpty );
+ if ( p->fVerbose )
+ printf( "Found last conflict after adding unit clause number %d!\n", pClause->Id );
+ return 0;
+ }
+
+ // update the root level
+ p->nRootSize = p->nTrailSize;
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Propagate the root clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Int_ManProcessRoots( Int_Man_t * p )
+{
+ Sto_Cls_t * pClause;
+ int Counter;
+
+ // make sure the root clauses are preceeding the learnt clauses
+ Counter = 0;
+ Sto_ManForEachClause( p->pCnf, pClause )
+ {
+ assert( (int)pClause->fA == (Counter < (int)p->pCnf->nClausesA) );
+ assert( (int)pClause->fRoot == (Counter < (int)p->pCnf->nRoots) );
+ Counter++;
+ }
+ assert( p->pCnf->nClauses == Counter );
+
+ // make sure the last clause if empty
+ assert( p->pCnf->pTail->nLits == 0 );
+
+ // go through the root unit clauses
+ p->nTrailSize = 0;
+ Sto_ManForEachClauseRoot( p->pCnf, pClause )
+ {
+ // create watcher lists for the root clauses
+ if ( pClause->nLits > 1 )
+ {
+ Int_ManWatchClause( p, pClause, pClause->pLits[0] );
+ Int_ManWatchClause( p, pClause, pClause->pLits[1] );
+ }
+ // empty clause and large clauses
+ if ( pClause->nLits != 1 )
+ continue;
+ // unit clause
+ assert( lit_check(pClause->pLits[0], p->pCnf->nVars) );
+ if ( !Int_ManEnqueue( p, pClause->pLits[0], pClause ) )
+ {
+ // detected root level conflict
+ printf( "Error in Int_ManProcessRoots(): Detected a root-level conflict too early!\n" );
+ assert( 0 );
+ return 0;
+ }
+ }
+
+ // propagate the root unit clauses
+ pClause = Int_ManPropagate( p, 0 );
+ if ( pClause )
+ {
+ // detected root level conflict
+ Int_ManProofTraceOne( p, pClause, p->pCnf->pEmpty );
+ if ( p->fVerbose )
+ printf( "Found root level conflict!\n" );
+ return 0;
+ }
+
+ // set the root level
+ p->nRootSize = p->nTrailSize;
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Records the proof.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Int_ManPrepareInter( Int_Man_t * p )
+{
+ // elementary truth tables
+ unsigned uTruths[8][8] = {
+ { 0xAAAAAAAA,0xAAAAAAAA,0xAAAAAAAA,0xAAAAAAAA,0xAAAAAAAA,0xAAAAAAAA,0xAAAAAAAA,0xAAAAAAAA },
+ { 0xCCCCCCCC,0xCCCCCCCC,0xCCCCCCCC,0xCCCCCCCC,0xCCCCCCCC,0xCCCCCCCC,0xCCCCCCCC,0xCCCCCCCC },
+ { 0xF0F0F0F0,0xF0F0F0F0,0xF0F0F0F0,0xF0F0F0F0,0xF0F0F0F0,0xF0F0F0F0,0xF0F0F0F0,0xF0F0F0F0 },
+ { 0xFF00FF00,0xFF00FF00,0xFF00FF00,0xFF00FF00,0xFF00FF00,0xFF00FF00,0xFF00FF00,0xFF00FF00 },
+ { 0xFFFF0000,0xFFFF0000,0xFFFF0000,0xFFFF0000,0xFFFF0000,0xFFFF0000,0xFFFF0000,0xFFFF0000 },
+ { 0x00000000,0xFFFFFFFF,0x00000000,0xFFFFFFFF,0x00000000,0xFFFFFFFF,0x00000000,0xFFFFFFFF },
+ { 0x00000000,0x00000000,0xFFFFFFFF,0xFFFFFFFF,0x00000000,0x00000000,0xFFFFFFFF,0xFFFFFFFF },
+ { 0x00000000,0x00000000,0x00000000,0x00000000,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF,0xFFFFFFFF }
+ };
+ Sto_Cls_t * pClause;
+ int Var, VarAB, v;
+ assert( p->nVarsAB <= 8 );
+
+ // set interpolants for root clauses
+ Sto_ManForEachClauseRoot( p->pCnf, pClause )
+ {
+ if ( !pClause->fA ) // clause of B
+ {
+ Int_ManTruthFill( Int_ManTruthRead(p, pClause), p->nWords );
+// Int_ManPrintInterOne( p, pClause );
+ continue;
+ }
+ // clause of A
+ Int_ManTruthClear( Int_ManTruthRead(p, pClause), p->nWords );
+ for ( v = 0; v < (int)pClause->nLits; v++ )
+ {
+ Var = lit_var(pClause->pLits[v]);
+ if ( p->pVarTypes[Var] < 0 ) // global var
+ {
+ VarAB = -p->pVarTypes[Var]-1;
+ assert( VarAB >= 0 && VarAB < p->nVarsAB );
+ if ( lit_sign(pClause->pLits[v]) ) // negative var
+ Int_ManTruthOrNot( Int_ManTruthRead(p, pClause), uTruths[VarAB], p->nWords );
+ else
+ Int_ManTruthOr( Int_ManTruthRead(p, pClause), uTruths[VarAB], p->nWords );
+ }
+ }
+// Int_ManPrintInterOne( p, pClause );
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Computes interpolant for the given CNF.]
+
+ Description [Returns the number of common variable found and interpolant.
+ Returns 0, if something did not work.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Int_ManInterpolate( Int_Man_t * p, Sto_Man_t * pCnf, int fVerbose, unsigned ** ppResult )
+{
+ Sto_Cls_t * pClause;
+ int RetValue = 1;
+ int clkTotal = clock();
+
+ // check that the CNF makes sense
+ assert( pCnf->nVars > 0 && pCnf->nClauses > 0 );
+ p->pCnf = pCnf;
+ p->fVerbose = fVerbose;
+ *ppResult = NULL;
+
+ // adjust the manager
+ Int_ManResize( p );
+
+ // prepare the interpolant computation
+ Int_ManPrepareInter( p );
+
+ // construct proof for each clause
+ // start the proof
+ if ( p->fProofWrite )
+ {
+ p->pFile = fopen( "proof.cnf_", "w" );
+ p->Counter = 0;
+ }
+
+ // write the root clauses
+ Sto_ManForEachClauseRoot( p->pCnf, pClause )
+ Int_ManProofWriteOne( p, pClause );
+
+ // propagate root level assignments
+ if ( Int_ManProcessRoots( p ) )
+ {
+ // if there is no conflict, consider learned clauses
+ Sto_ManForEachClause( p->pCnf, pClause )
+ {
+ if ( pClause->fRoot )
+ continue;
+ if ( !Int_ManProofRecordOne( p, pClause ) )
+ {
+ RetValue = 0;
+ break;
+ }
+ }
+ }
+
+ // stop the proof
+ if ( p->fProofWrite )
+ {
+ fclose( p->pFile );
+ p->pFile = NULL;
+ }
+
+ if ( fVerbose )
+ {
+ printf( "Vars = %d. Roots = %d. Learned = %d. Resol steps = %d. Ave = %.2f. Mem = %.2f Mb\n",
+ p->pCnf->nVars, p->pCnf->nRoots, p->pCnf->nClauses-p->pCnf->nRoots, p->Counter,
+ 1.0*(p->Counter-p->pCnf->nRoots)/(p->pCnf->nClauses-p->pCnf->nRoots),
+ 1.0*Sto_ManMemoryReport(p->pCnf)/(1<<20) );
+p->timeTotal += clock() - clkTotal;
+ }
+
+ *ppResult = Int_ManTruthRead( p, p->pCnf->pTail );
+ return p->nVarsAB;
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/bsat/satMem.c b/src/sat/bsat/satMem.c
new file mode 100644
index 00000000..7fa1a824
--- /dev/null
+++ b/src/sat/bsat/satMem.c
@@ -0,0 +1,527 @@
+/**CFile****************************************************************
+
+ FileName [satMem.c]
+
+ PackageName [SAT solver.]
+
+ Synopsis [Memory management.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: satMem.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "satMem.h"
+#include "vec.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+struct Sat_MmFixed_t_
+{
+ // information about individual entries
+ int nEntrySize; // the size of one entry
+ int nEntriesAlloc; // the total number of entries allocated
+ int nEntriesUsed; // the number of entries in use
+ int nEntriesMax; // the max number of entries in use
+ char * pEntriesFree; // the linked list of free entries
+
+ // this is where the memory is stored
+ int nChunkSize; // the size of one chunk
+ int nChunksAlloc; // the maximum number of memory chunks
+ int nChunks; // the current number of memory chunks
+ char ** pChunks; // the allocated memory
+
+ // statistics
+ int nMemoryUsed; // memory used in the allocated entries
+ int nMemoryAlloc; // memory allocated
+};
+
+struct Sat_MmFlex_t_
+{
+ // information about individual entries
+ int nEntriesUsed; // the number of entries allocated
+ char * pCurrent; // the current pointer to free memory
+ char * pEnd; // the first entry outside the free memory
+
+ // this is where the memory is stored
+ int nChunkSize; // the size of one chunk
+ int nChunksAlloc; // the maximum number of memory chunks
+ int nChunks; // the current number of memory chunks
+ char ** pChunks; // the allocated memory
+
+ // statistics
+ int nMemoryUsed; // memory used in the allocated entries
+ int nMemoryAlloc; // memory allocated
+};
+
+struct Sat_MmStep_t_
+{
+ int nMems; // the number of fixed memory managers employed
+ Sat_MmFixed_t ** pMems; // memory managers: 2^1 words, 2^2 words, etc
+ int nMapSize; // the size of the memory array
+ Sat_MmFixed_t ** pMap; // maps the number of bytes into its memory manager
+};
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Allocates memory pieces of fixed size.]
+
+ Description [The size of the chunk is computed as the minimum of
+ 1024 entries and 64K. Can only work with entry size at least 4 byte long.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Sat_MmFixed_t * Sat_MmFixedStart( int nEntrySize )
+{
+ Sat_MmFixed_t * p;
+
+ p = ALLOC( Sat_MmFixed_t, 1 );
+ memset( p, 0, sizeof(Sat_MmFixed_t) );
+
+ p->nEntrySize = nEntrySize;
+ p->nEntriesAlloc = 0;
+ p->nEntriesUsed = 0;
+ p->pEntriesFree = NULL;
+
+ if ( nEntrySize * (1 << 10) < (1<<16) )
+ p->nChunkSize = (1 << 10);
+ else
+ p->nChunkSize = (1<<16) / nEntrySize;
+ if ( p->nChunkSize < 8 )
+ p->nChunkSize = 8;
+
+ p->nChunksAlloc = 64;
+ p->nChunks = 0;
+ p->pChunks = ALLOC( char *, p->nChunksAlloc );
+
+ p->nMemoryUsed = 0;
+ p->nMemoryAlloc = 0;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_MmFixedStop( Sat_MmFixed_t * p, int fVerbose )
+{
+ int i;
+ if ( p == NULL )
+ return;
+ if ( fVerbose )
+ {
+ printf( "Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n",
+ p->nEntrySize, p->nChunkSize, p->nChunks );
+ printf( " Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n",
+ p->nEntriesUsed, p->nEntriesMax, p->nEntrySize * p->nEntriesUsed, p->nMemoryAlloc );
+ }
+ for ( i = 0; i < p->nChunks; i++ )
+ free( p->pChunks[i] );
+ free( p->pChunks );
+ free( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+char * Sat_MmFixedEntryFetch( Sat_MmFixed_t * p )
+{
+ char * pTemp;
+ int i;
+
+ // check if there are still free entries
+ if ( p->nEntriesUsed == p->nEntriesAlloc )
+ { // need to allocate more entries
+ assert( p->pEntriesFree == NULL );
+ if ( p->nChunks == p->nChunksAlloc )
+ {
+ p->nChunksAlloc *= 2;
+ p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc );
+ }
+ p->pEntriesFree = ALLOC( char, p->nEntrySize * p->nChunkSize );
+ p->nMemoryAlloc += p->nEntrySize * p->nChunkSize;
+ // transform these entries into a linked list
+ pTemp = p->pEntriesFree;
+ for ( i = 1; i < p->nChunkSize; i++ )
+ {
+ *((char **)pTemp) = pTemp + p->nEntrySize;
+ pTemp += p->nEntrySize;
+ }
+ // set the last link
+ *((char **)pTemp) = NULL;
+ // add the chunk to the chunk storage
+ p->pChunks[ p->nChunks++ ] = p->pEntriesFree;
+ // add to the number of entries allocated
+ p->nEntriesAlloc += p->nChunkSize;
+ }
+ // incrememt the counter of used entries
+ p->nEntriesUsed++;
+ if ( p->nEntriesMax < p->nEntriesUsed )
+ p->nEntriesMax = p->nEntriesUsed;
+ // return the first entry in the free entry list
+ pTemp = p->pEntriesFree;
+ p->pEntriesFree = *((char **)pTemp);
+ return pTemp;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_MmFixedEntryRecycle( Sat_MmFixed_t * p, char * pEntry )
+{
+ // decrement the counter of used entries
+ p->nEntriesUsed--;
+ // add the entry to the linked list of free entries
+ *((char **)pEntry) = p->pEntriesFree;
+ p->pEntriesFree = pEntry;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description [Relocates all the memory except the first chunk.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_MmFixedRestart( Sat_MmFixed_t * p )
+{
+ int i;
+ char * pTemp;
+
+ // deallocate all chunks except the first one
+ for ( i = 1; i < p->nChunks; i++ )
+ free( p->pChunks[i] );
+ p->nChunks = 1;
+ // transform these entries into a linked list
+ pTemp = p->pChunks[0];
+ for ( i = 1; i < p->nChunkSize; i++ )
+ {
+ *((char **)pTemp) = pTemp + p->nEntrySize;
+ pTemp += p->nEntrySize;
+ }
+ // set the last link
+ *((char **)pTemp) = NULL;
+ // set the free entry list
+ p->pEntriesFree = p->pChunks[0];
+ // set the correct statistics
+ p->nMemoryAlloc = p->nEntrySize * p->nChunkSize;
+ p->nMemoryUsed = 0;
+ p->nEntriesAlloc = p->nChunkSize;
+ p->nEntriesUsed = 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Sat_MmFixedReadMemUsage( Sat_MmFixed_t * p )
+{
+ return p->nMemoryAlloc;
+}
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Allocates entries of flexible size.]
+
+ Description [Can only work with entry size at least 4 byte long.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Sat_MmFlex_t * Sat_MmFlexStart()
+{
+ Sat_MmFlex_t * p;
+
+ p = ALLOC( Sat_MmFlex_t, 1 );
+ memset( p, 0, sizeof(Sat_MmFlex_t) );
+
+ p->nEntriesUsed = 0;
+ p->pCurrent = NULL;
+ p->pEnd = NULL;
+
+ p->nChunkSize = (1 << 12);
+ p->nChunksAlloc = 64;
+ p->nChunks = 0;
+ p->pChunks = ALLOC( char *, p->nChunksAlloc );
+
+ p->nMemoryUsed = 0;
+ p->nMemoryAlloc = 0;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_MmFlexStop( Sat_MmFlex_t * p, int fVerbose )
+{
+ int i;
+ if ( p == NULL )
+ return;
+ if ( fVerbose )
+ {
+ printf( "Flexible memory manager: Chunk size = %d. Chunks used = %d.\n",
+ p->nChunkSize, p->nChunks );
+ printf( " Entries used = %d. Memory used = %d. Memory alloc = %d.\n",
+ p->nEntriesUsed, p->nMemoryUsed, p->nMemoryAlloc );
+ }
+ for ( i = 0; i < p->nChunks; i++ )
+ free( p->pChunks[i] );
+ free( p->pChunks );
+ free( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+char * Sat_MmFlexEntryFetch( Sat_MmFlex_t * p, int nBytes )
+{
+ char * pTemp;
+ // check if there are still free entries
+ if ( p->pCurrent == NULL || p->pCurrent + nBytes > p->pEnd )
+ { // need to allocate more entries
+ if ( p->nChunks == p->nChunksAlloc )
+ {
+ p->nChunksAlloc *= 2;
+ p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc );
+ }
+ if ( nBytes > p->nChunkSize )
+ {
+ // resize the chunk size if more memory is requested than it can give
+ // (ideally, this should never happen)
+ p->nChunkSize = 2 * nBytes;
+ }
+ p->pCurrent = ALLOC( char, p->nChunkSize );
+ p->pEnd = p->pCurrent + p->nChunkSize;
+ p->nMemoryAlloc += p->nChunkSize;
+ // add the chunk to the chunk storage
+ p->pChunks[ p->nChunks++ ] = p->pCurrent;
+ }
+ assert( p->pCurrent + nBytes <= p->pEnd );
+ // increment the counter of used entries
+ p->nEntriesUsed++;
+ // keep track of the memory used
+ p->nMemoryUsed += nBytes;
+ // return the next entry
+ pTemp = p->pCurrent;
+ p->pCurrent += nBytes;
+ return pTemp;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Sat_MmFlexReadMemUsage( Sat_MmFlex_t * p )
+{
+ return p->nMemoryAlloc;
+}
+
+
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Starts the hierarchical memory manager.]
+
+ Description [This manager can allocate entries of any size.
+ Iternally they are mapped into the entries with the number of bytes
+ equal to the power of 2. The smallest entry size is 8 bytes. The
+ next one is 16 bytes etc. So, if the user requests 6 bytes, he gets
+ 8 byte entry. If we asks for 25 bytes, he gets 32 byte entry etc.
+ The input parameters "nSteps" says how many fixed memory managers
+ are employed internally. Calling this procedure with nSteps equal
+ to 10 results in 10 hierarchically arranged internal memory managers,
+ which can allocate up to 4096 (1Kb) entries. Requests for larger
+ entries are handed over to malloc() and then free()ed.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Sat_MmStep_t * Sat_MmStepStart( int nSteps )
+{
+ Sat_MmStep_t * p;
+ int i, k;
+ p = ALLOC( Sat_MmStep_t, 1 );
+ p->nMems = nSteps;
+ // start the fixed memory managers
+ p->pMems = ALLOC( Sat_MmFixed_t *, p->nMems );
+ for ( i = 0; i < p->nMems; i++ )
+ p->pMems[i] = Sat_MmFixedStart( (8<<i) );
+ // set up the mapping of the required memory size into the corresponding manager
+ p->nMapSize = (4<<p->nMems);
+ p->pMap = ALLOC( Sat_MmFixed_t *, p->nMapSize+1 );
+ p->pMap[0] = NULL;
+ for ( k = 1; k <= 4; k++ )
+ p->pMap[k] = p->pMems[0];
+ for ( i = 0; i < p->nMems; i++ )
+ for ( k = (4<<i)+1; k <= (8<<i); k++ )
+ p->pMap[k] = p->pMems[i];
+//for ( i = 1; i < 100; i ++ )
+//printf( "%10d: size = %10d\n", i, p->pMap[i]->nEntrySize );
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Stops the memory manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_MmStepStop( Sat_MmStep_t * p, int fVerbose )
+{
+ int i;
+ for ( i = 0; i < p->nMems; i++ )
+ Sat_MmFixedStop( p->pMems[i], fVerbose );
+ free( p->pMems );
+ free( p->pMap );
+ free( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Creates the entry.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+char * Sat_MmStepEntryFetch( Sat_MmStep_t * p, int nBytes )
+{
+ if ( nBytes == 0 )
+ return NULL;
+ if ( nBytes > p->nMapSize )
+ {
+// printf( "Allocating %d bytes.\n", nBytes );
+ return ALLOC( char, nBytes );
+ }
+ return Sat_MmFixedEntryFetch( p->pMap[nBytes] );
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Recycles the entry.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_MmStepEntryRecycle( Sat_MmStep_t * p, char * pEntry, int nBytes )
+{
+ if ( nBytes == 0 )
+ return;
+ if ( nBytes > p->nMapSize )
+ {
+ free( pEntry );
+ return;
+ }
+ Sat_MmFixedEntryRecycle( p->pMap[nBytes], pEntry );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Sat_MmStepReadMemUsage( Sat_MmStep_t * p )
+{
+ int i, nMemTotal = 0;
+ for ( i = 0; i < p->nMems; i++ )
+ nMemTotal += p->pMems[i]->nMemoryAlloc;
+ return nMemTotal;
+}
diff --git a/src/sat/bsat/satMem.h b/src/sat/bsat/satMem.h
new file mode 100644
index 00000000..22b7a87c
--- /dev/null
+++ b/src/sat/bsat/satMem.h
@@ -0,0 +1,80 @@
+/**CFile****************************************************************
+
+ FileName [satMem.h]
+
+ PackageName [SAT solver.]
+
+ Synopsis [Memory management.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: satMem.h,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#ifndef __SAT_MEM_H__
+#define __SAT_MEM_H__
+
+////////////////////////////////////////////////////////////////////////
+/// INCLUDES ///
+////////////////////////////////////////////////////////////////////////
+
+//#include "leaks.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+////////////////////////////////////////////////////////////////////////
+/// PARAMETERS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// STRUCTURE DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+typedef struct Sat_MmFixed_t_ Sat_MmFixed_t;
+typedef struct Sat_MmFlex_t_ Sat_MmFlex_t;
+typedef struct Sat_MmStep_t_ Sat_MmStep_t;
+
+////////////////////////////////////////////////////////////////////////
+/// GLOBAL VARIABLES ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// MACRO DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+// fixed-size-block memory manager
+extern Sat_MmFixed_t * Sat_MmFixedStart( int nEntrySize );
+extern void Sat_MmFixedStop( Sat_MmFixed_t * p, int fVerbose );
+extern char * Sat_MmFixedEntryFetch( Sat_MmFixed_t * p );
+extern void Sat_MmFixedEntryRecycle( Sat_MmFixed_t * p, char * pEntry );
+extern void Sat_MmFixedRestart( Sat_MmFixed_t * p );
+extern int Sat_MmFixedReadMemUsage( Sat_MmFixed_t * p );
+// flexible-size-block memory manager
+extern Sat_MmFlex_t * Sat_MmFlexStart();
+extern void Sat_MmFlexStop( Sat_MmFlex_t * p, int fVerbose );
+extern char * Sat_MmFlexEntryFetch( Sat_MmFlex_t * p, int nBytes );
+extern int Sat_MmFlexReadMemUsage( Sat_MmFlex_t * p );
+// hierarchical memory manager
+extern Sat_MmStep_t * Sat_MmStepStart( int nSteps );
+extern void Sat_MmStepStop( Sat_MmStep_t * p, int fVerbose );
+extern char * Sat_MmStepEntryFetch( Sat_MmStep_t * p, int nBytes );
+extern void Sat_MmStepEntryRecycle( Sat_MmStep_t * p, char * pEntry, int nBytes );
+extern int Sat_MmStepReadMemUsage( Sat_MmStep_t * p );
+
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
diff --git a/src/sat/bsat/satSolver.c b/src/sat/bsat/satSolver.c
new file mode 100644
index 00000000..fbc9874d
--- /dev/null
+++ b/src/sat/bsat/satSolver.c
@@ -0,0 +1,1358 @@
+/**************************************************************************************************
+MiniSat -- Copyright (c) 2005, Niklas Sorensson
+http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
+associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute,
+sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or
+substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
+NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
+OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+**************************************************************************************************/
+// Modified to compile with MS Visual Studio 6.0 by Alan Mishchenko
+
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <math.h>
+
+#include "satSolver.h"
+
+//#define SAT_USE_SYSTEM_MEMORY_MANAGEMENT
+
+//=================================================================================================
+// Debug:
+
+//#define VERBOSEDEBUG
+
+// For derivation output (verbosity level 2)
+#define L_IND "%-*d"
+#define L_ind sat_solver_dlevel(s)*3+3,sat_solver_dlevel(s)
+#define L_LIT "%sx%d"
+#define L_lit(p) lit_sign(p)?"~":"", (lit_var(p))
+
+// Just like 'assert()' but expression will be evaluated in the release version as well.
+static inline void check(int expr) { assert(expr); }
+
+static void printlits(lit* begin, lit* end)
+{
+ int i;
+ for (i = 0; i < end - begin; i++)
+ printf(L_LIT" ",L_lit(begin[i]));
+}
+
+//=================================================================================================
+// Random numbers:
+
+
+// Returns a random float 0 <= x < 1. Seed must never be 0.
+static inline double drand(double* seed) {
+ int q;
+ *seed *= 1389796;
+ q = (int)(*seed / 2147483647);
+ *seed -= (double)q * 2147483647;
+ return *seed / 2147483647; }
+
+
+// Returns a random integer 0 <= x < size. Seed must never be 0.
+static inline int irand(double* seed, int size) {
+ return (int)(drand(seed) * size); }
+
+
+//=================================================================================================
+// Predeclarations:
+
+static void sat_solver_sort(void** array, int size, int(*comp)(const void *, const void *));
+
+//=================================================================================================
+// Clause datatype + minor functions:
+
+struct clause_t
+{
+ int size_learnt;
+ lit lits[0];
+};
+
+static inline int clause_size (clause* c) { return c->size_learnt >> 1; }
+static inline lit* clause_begin (clause* c) { return c->lits; }
+static inline int clause_learnt (clause* c) { return c->size_learnt & 1; }
+static inline float clause_activity (clause* c) { return *((float*)&c->lits[c->size_learnt>>1]); }
+static inline void clause_setactivity(clause* c, float a) { *((float*)&c->lits[c->size_learnt>>1]) = a; }
+
+//=================================================================================================
+// Encode literals in clause pointers:
+
+static inline clause* clause_from_lit (lit l) { return (clause*)((unsigned long)l + (unsigned long)l + 1); }
+static inline bool clause_is_lit (clause* c) { return ((unsigned long)c & 1); }
+static inline lit clause_read_lit (clause* c) { return (lit)((unsigned long)c >> 1); }
+
+//=================================================================================================
+// Simple helpers:
+
+static inline int sat_solver_dlevel(sat_solver* s) { return veci_size(&s->trail_lim); }
+static inline vecp* sat_solver_read_wlist(sat_solver* s, lit l) { return &s->wlists[l]; }
+static inline void vecp_remove(vecp* v, void* e)
+{
+ void** ws = vecp_begin(v);
+ int j = 0;
+ for (; ws[j] != e ; j++);
+ assert(j < vecp_size(v));
+ for (; j < vecp_size(v)-1; j++) ws[j] = ws[j+1];
+ vecp_resize(v,vecp_size(v)-1);
+}
+
+//=================================================================================================
+// Variable order functions:
+
+static inline void order_update(sat_solver* s, int v) // updateorder
+{
+ int* orderpos = s->orderpos;
+ double* activity = s->activity;
+ int* heap = veci_begin(&s->order);
+ int i = orderpos[v];
+ int x = heap[i];
+ int parent = (i - 1) / 2;
+
+ assert(s->orderpos[v] != -1);
+
+ while (i != 0 && activity[x] > activity[heap[parent]]){
+ heap[i] = heap[parent];
+ orderpos[heap[i]] = i;
+ i = parent;
+ parent = (i - 1) / 2;
+ }
+ heap[i] = x;
+ orderpos[x] = i;
+}
+
+static inline void order_assigned(sat_solver* s, int v)
+{
+}
+
+static inline void order_unassigned(sat_solver* s, int v) // undoorder
+{
+ int* orderpos = s->orderpos;
+ if (orderpos[v] == -1){
+ orderpos[v] = veci_size(&s->order);
+ veci_push(&s->order,v);
+ order_update(s,v);
+//printf( "+%d ", v );
+ }
+}
+
+static inline int order_select(sat_solver* s, float random_var_freq) // selectvar
+{
+ int* heap;
+ double* activity;
+ int* orderpos;
+
+ lbool* values = s->assigns;
+
+ // Random decision:
+ if (drand(&s->random_seed) < random_var_freq){
+ int next = irand(&s->random_seed,s->size);
+ assert(next >= 0 && next < s->size);
+ if (values[next] == l_Undef)
+ return next;
+ }
+
+ // Activity based decision:
+
+ heap = veci_begin(&s->order);
+ activity = s->activity;
+ orderpos = s->orderpos;
+
+
+ while (veci_size(&s->order) > 0){
+ int next = heap[0];
+ int size = veci_size(&s->order)-1;
+ int x = heap[size];
+
+ veci_resize(&s->order,size);
+
+ orderpos[next] = -1;
+
+ if (size > 0){
+ double act = activity[x];
+
+ int i = 0;
+ int child = 1;
+
+
+ while (child < size){
+ if (child+1 < size && activity[heap[child]] < activity[heap[child+1]])
+ child++;
+
+ assert(child < size);
+
+ if (act >= activity[heap[child]])
+ break;
+
+ heap[i] = heap[child];
+ orderpos[heap[i]] = i;
+ i = child;
+ child = 2 * child + 1;
+ }
+ heap[i] = x;
+ orderpos[heap[i]] = i;
+ }
+
+//printf( "-%d ", next );
+ if (values[next] == l_Undef)
+ return next;
+ }
+
+ return var_Undef;
+}
+
+//=================================================================================================
+// Activity functions:
+
+static inline void act_var_rescale(sat_solver* s) {
+ double* activity = s->activity;
+ int i;
+ for (i = 0; i < s->size; i++)
+ activity[i] *= 1e-100;
+ s->var_inc *= 1e-100;
+}
+
+static inline void act_var_bump(sat_solver* s, int v) {
+ s->activity[v] += s->var_inc;
+ if (s->activity[v] > 1e100)
+ act_var_rescale(s);
+ //printf("bump %d %f\n", v-1, activity[v]);
+ if (s->orderpos[v] != -1)
+ order_update(s,v);
+}
+
+static inline void act_var_bump_factor(sat_solver* s, int v) {
+ s->activity[v] += (s->var_inc * s->factors[v]);
+ if (s->activity[v] > 1e100)
+ act_var_rescale(s);
+ //printf("bump %d %f\n", v-1, activity[v]);
+ if (s->orderpos[v] != -1)
+ order_update(s,v);
+}
+
+static inline void act_var_decay(sat_solver* s) { s->var_inc *= s->var_decay; }
+
+static inline void act_clause_rescale(sat_solver* s) {
+ clause** cs = (clause**)vecp_begin(&s->learnts);
+ int i;
+ for (i = 0; i < vecp_size(&s->learnts); i++){
+ float a = clause_activity(cs[i]);
+ clause_setactivity(cs[i], a * (float)1e-20);
+ }
+ s->cla_inc *= (float)1e-20;
+}
+
+
+static inline void act_clause_bump(sat_solver* s, clause *c) {
+ float a = clause_activity(c) + s->cla_inc;
+ clause_setactivity(c,a);
+ if (a > 1e20) act_clause_rescale(s);
+}
+
+static inline void act_clause_decay(sat_solver* s) { s->cla_inc *= s->cla_decay; }
+
+//=================================================================================================
+// Clause functions:
+
+/* pre: size > 1 && no variable occurs twice
+ */
+static clause* clause_new(sat_solver* s, lit* begin, lit* end, int learnt)
+{
+ int size;
+ clause* c;
+ int i;
+
+ assert(end - begin > 1);
+ assert(learnt >= 0 && learnt < 2);
+ size = end - begin;
+// c = (clause*)malloc(sizeof(clause) + sizeof(lit) * size + learnt * sizeof(float));
+#ifdef SAT_USE_SYSTEM_MEMORY_MANAGEMENT
+ c = (clause*)malloc(sizeof(clause) + sizeof(lit) * size + learnt * sizeof(float));
+#else
+ c = (clause*)Sat_MmStepEntryFetch( s->pMem, sizeof(clause) + sizeof(lit) * size + learnt * sizeof(float) );
+#endif
+
+ c->size_learnt = (size << 1) | learnt;
+ assert(((unsigned long)c & 1) == 0);
+
+ for (i = 0; i < size; i++)
+ c->lits[i] = begin[i];
+
+ if (learnt)
+ *((float*)&c->lits[size]) = 0.0;
+
+ assert(begin[0] >= 0);
+ assert(begin[0] < s->size*2);
+ assert(begin[1] >= 0);
+ assert(begin[1] < s->size*2);
+
+ assert(lit_neg(begin[0]) < s->size*2);
+ assert(lit_neg(begin[1]) < s->size*2);
+
+ //vecp_push(sat_solver_read_wlist(s,lit_neg(begin[0])),(void*)c);
+ //vecp_push(sat_solver_read_wlist(s,lit_neg(begin[1])),(void*)c);
+
+ vecp_push(sat_solver_read_wlist(s,lit_neg(begin[0])),(void*)(size > 2 ? c : clause_from_lit(begin[1])));
+ vecp_push(sat_solver_read_wlist(s,lit_neg(begin[1])),(void*)(size > 2 ? c : clause_from_lit(begin[0])));
+
+ return c;
+}
+
+
+static void clause_remove(sat_solver* s, clause* c)
+{
+ lit* lits = clause_begin(c);
+ assert(lit_neg(lits[0]) < s->size*2);
+ assert(lit_neg(lits[1]) < s->size*2);
+
+ //vecp_remove(sat_solver_read_wlist(s,lit_neg(lits[0])),(void*)c);
+ //vecp_remove(sat_solver_read_wlist(s,lit_neg(lits[1])),(void*)c);
+
+ assert(lits[0] < s->size*2);
+ vecp_remove(sat_solver_read_wlist(s,lit_neg(lits[0])),(void*)(clause_size(c) > 2 ? c : clause_from_lit(lits[1])));
+ vecp_remove(sat_solver_read_wlist(s,lit_neg(lits[1])),(void*)(clause_size(c) > 2 ? c : clause_from_lit(lits[0])));
+
+ if (clause_learnt(c)){
+ s->stats.learnts--;
+ s->stats.learnts_literals -= clause_size(c);
+ }else{
+ s->stats.clauses--;
+ s->stats.clauses_literals -= clause_size(c);
+ }
+
+#ifdef SAT_USE_SYSTEM_MEMORY_MANAGEMENT
+ free(c);
+#else
+ Sat_MmStepEntryRecycle( s->pMem, (char *)c, sizeof(clause) + sizeof(lit) * clause_size(c) + clause_learnt(c) * sizeof(float) );
+#endif
+}
+
+
+static lbool clause_simplify(sat_solver* s, clause* c)
+{
+ lit* lits = clause_begin(c);
+ lbool* values = s->assigns;
+ int i;
+
+ assert(sat_solver_dlevel(s) == 0);
+
+ for (i = 0; i < clause_size(c); i++){
+ lbool sig = !lit_sign(lits[i]); sig += sig - 1;
+ if (values[lit_var(lits[i])] == sig)
+ return l_True;
+ }
+ return l_False;
+}
+
+//=================================================================================================
+// Minor (solver) functions:
+
+void sat_solver_setnvars(sat_solver* s,int n)
+{
+ int var;
+
+ if (s->cap < n){
+
+ while (s->cap < n) s->cap = s->cap*2+1;
+
+ s->wlists = (vecp*) realloc(s->wlists, sizeof(vecp)*s->cap*2);
+ s->activity = (double*) realloc(s->activity, sizeof(double)*s->cap);
+ s->factors = (double*) realloc(s->factors, sizeof(double)*s->cap);
+ s->assigns = (lbool*) realloc(s->assigns, sizeof(lbool)*s->cap);
+ s->orderpos = (int*) realloc(s->orderpos, sizeof(int)*s->cap);
+ s->reasons = (clause**)realloc(s->reasons, sizeof(clause*)*s->cap);
+ s->levels = (int*) realloc(s->levels, sizeof(int)*s->cap);
+ s->tags = (lbool*) realloc(s->tags, sizeof(lbool)*s->cap);
+ s->trail = (lit*) realloc(s->trail, sizeof(lit)*s->cap);
+ }
+
+ for (var = s->size; var < n; var++){
+ vecp_new(&s->wlists[2*var]);
+ vecp_new(&s->wlists[2*var+1]);
+ s->activity [var] = 0;
+ s->factors [var] = 0;
+ s->assigns [var] = l_Undef;
+ s->orderpos [var] = veci_size(&s->order);
+ s->reasons [var] = (clause*)0;
+ s->levels [var] = 0;
+ s->tags [var] = l_Undef;
+
+ /* does not hold because variables enqueued at top level will not be reinserted in the heap
+ assert(veci_size(&s->order) == var);
+ */
+ veci_push(&s->order,var);
+ order_update(s, var);
+ }
+
+ s->size = n > s->size ? n : s->size;
+}
+
+
+static inline bool enqueue(sat_solver* s, lit l, clause* from)
+{
+ lbool* values = s->assigns;
+ int v = lit_var(l);
+ lbool val = values[v];
+#ifdef VERBOSEDEBUG
+ printf(L_IND"enqueue("L_LIT")\n", L_ind, L_lit(l));
+#endif
+
+ lbool sig = !lit_sign(l); sig += sig - 1;
+ if (val != l_Undef){
+ return val == sig;
+ }else{
+ // New fact -- store it.
+#ifdef VERBOSEDEBUG
+ printf(L_IND"bind("L_LIT")\n", L_ind, L_lit(l));
+#endif
+ int* levels = s->levels;
+ clause** reasons = s->reasons;
+
+ values [v] = sig;
+ levels [v] = sat_solver_dlevel(s);
+ reasons[v] = from;
+ s->trail[s->qtail++] = l;
+
+ order_assigned(s, v);
+ return true;
+ }
+}
+
+
+static inline void assume(sat_solver* s, lit l){
+ assert(s->qtail == s->qhead);
+ assert(s->assigns[lit_var(l)] == l_Undef);
+#ifdef VERBOSEDEBUG
+ printf(L_IND"assume("L_LIT")\n", L_ind, L_lit(l));
+#endif
+ veci_push(&s->trail_lim,s->qtail);
+ enqueue(s,l,(clause*)0);
+}
+
+
+static void sat_solver_canceluntil(sat_solver* s, int level) {
+ lit* trail;
+ lbool* values;
+ clause** reasons;
+ int bound;
+ int c;
+
+ if (sat_solver_dlevel(s) <= level)
+ return;
+
+ trail = s->trail;
+ values = s->assigns;
+ reasons = s->reasons;
+ bound = (veci_begin(&s->trail_lim))[level];
+
+ ////////////////////////////////////////
+ // added to cancel all assignments
+// if ( level == -1 )
+// bound = 0;
+ ////////////////////////////////////////
+
+ for (c = s->qtail-1; c >= bound; c--) {
+ int x = lit_var(trail[c]);
+ values [x] = l_Undef;
+ reasons[x] = (clause*)0;
+ }
+
+ for (c = s->qhead-1; c >= bound; c--)
+ order_unassigned(s,lit_var(trail[c]));
+
+ s->qhead = s->qtail = bound;
+ veci_resize(&s->trail_lim,level);
+}
+
+static void sat_solver_record(sat_solver* s, veci* cls)
+{
+ lit* begin = veci_begin(cls);
+ lit* end = begin + veci_size(cls);
+ clause* c = (veci_size(cls) > 1) ? clause_new(s,begin,end,1) : (clause*)0;
+ enqueue(s,*begin,c);
+
+ ///////////////////////////////////
+ // add clause to internal storage
+ if ( s->pStore )
+ {
+ extern int Sto_ManAddClause( void * p, lit * pBeg, lit * pEnd );
+ int RetValue = Sto_ManAddClause( s->pStore, begin, end );
+ assert( RetValue );
+ }
+ ///////////////////////////////////
+
+ assert(veci_size(cls) > 0);
+
+ if (c != 0) {
+ vecp_push(&s->learnts,c);
+ act_clause_bump(s,c);
+ s->stats.learnts++;
+ s->stats.learnts_literals += veci_size(cls);
+ }
+}
+
+
+static double sat_solver_progress(sat_solver* s)
+{
+ lbool* values = s->assigns;
+ int* levels = s->levels;
+ int i;
+
+ double progress = 0;
+ double F = 1.0 / s->size;
+ for (i = 0; i < s->size; i++)
+ if (values[i] != l_Undef)
+ progress += pow(F, levels[i]);
+ return progress / s->size;
+}
+
+//=================================================================================================
+// Major methods:
+
+static bool sat_solver_lit_removable(sat_solver* s, lit l, int minl)
+{
+ lbool* tags = s->tags;
+ clause** reasons = s->reasons;
+ int* levels = s->levels;
+ int top = veci_size(&s->tagged);
+
+ assert(lit_var(l) >= 0 && lit_var(l) < s->size);
+ assert(reasons[lit_var(l)] != 0);
+ veci_resize(&s->stack,0);
+ veci_push(&s->stack,lit_var(l));
+
+ while (veci_size(&s->stack) > 0){
+ clause* c;
+ int v = veci_begin(&s->stack)[veci_size(&s->stack)-1];
+ assert(v >= 0 && v < s->size);
+ veci_resize(&s->stack,veci_size(&s->stack)-1);
+ assert(reasons[v] != 0);
+ c = reasons[v];
+
+ if (clause_is_lit(c)){
+ int v = lit_var(clause_read_lit(c));
+ if (tags[v] == l_Undef && levels[v] != 0){
+ if (reasons[v] != 0 && ((1 << (levels[v] & 31)) & minl)){
+ veci_push(&s->stack,v);
+ tags[v] = l_True;
+ veci_push(&s->tagged,v);
+ }else{
+ int* tagged = veci_begin(&s->tagged);
+ int j;
+ for (j = top; j < veci_size(&s->tagged); j++)
+ tags[tagged[j]] = l_Undef;
+ veci_resize(&s->tagged,top);
+ return false;
+ }
+ }
+ }else{
+ lit* lits = clause_begin(c);
+ int i, j;
+
+ for (i = 1; i < clause_size(c); i++){
+ int v = lit_var(lits[i]);
+ if (tags[v] == l_Undef && levels[v] != 0){
+ if (reasons[v] != 0 && ((1 << (levels[v] & 31)) & minl)){
+
+ veci_push(&s->stack,lit_var(lits[i]));
+ tags[v] = l_True;
+ veci_push(&s->tagged,v);
+ }else{
+ int* tagged = veci_begin(&s->tagged);
+ for (j = top; j < veci_size(&s->tagged); j++)
+ tags[tagged[j]] = l_Undef;
+ veci_resize(&s->tagged,top);
+ return false;
+ }
+ }
+ }
+ }
+ }
+
+ return true;
+}
+
+static void sat_solver_analyze(sat_solver* s, clause* c, veci* learnt)
+{
+ lit* trail = s->trail;
+ lbool* tags = s->tags;
+ clause** reasons = s->reasons;
+ int* levels = s->levels;
+ int cnt = 0;
+ lit p = lit_Undef;
+ int ind = s->qtail-1;
+ lit* lits;
+ int i, j, minl;
+ int* tagged;
+
+ veci_push(learnt,lit_Undef);
+
+ do{
+ assert(c != 0);
+
+ if (clause_is_lit(c)){
+ lit q = clause_read_lit(c);
+ assert(lit_var(q) >= 0 && lit_var(q) < s->size);
+ if (tags[lit_var(q)] == l_Undef && levels[lit_var(q)] > 0){
+ tags[lit_var(q)] = l_True;
+ veci_push(&s->tagged,lit_var(q));
+ act_var_bump(s,lit_var(q));
+ if (levels[lit_var(q)] == sat_solver_dlevel(s))
+ cnt++;
+ else
+ veci_push(learnt,q);
+ }
+ }else{
+
+ if (clause_learnt(c))
+ act_clause_bump(s,c);
+
+ lits = clause_begin(c);
+ //printlits(lits,lits+clause_size(c)); printf("\n");
+ for (j = (p == lit_Undef ? 0 : 1); j < clause_size(c); j++){
+ lit q = lits[j];
+ assert(lit_var(q) >= 0 && lit_var(q) < s->size);
+ if (tags[lit_var(q)] == l_Undef && levels[lit_var(q)] > 0){
+ tags[lit_var(q)] = l_True;
+ veci_push(&s->tagged,lit_var(q));
+ act_var_bump(s,lit_var(q));
+ if (levels[lit_var(q)] == sat_solver_dlevel(s))
+ cnt++;
+ else
+ veci_push(learnt,q);
+ }
+ }
+ }
+
+ while (tags[lit_var(trail[ind--])] == l_Undef);
+
+ p = trail[ind+1];
+ c = reasons[lit_var(p)];
+ cnt--;
+
+ }while (cnt > 0);
+
+ *veci_begin(learnt) = lit_neg(p);
+
+ lits = veci_begin(learnt);
+ minl = 0;
+ for (i = 1; i < veci_size(learnt); i++){
+ int lev = levels[lit_var(lits[i])];
+ minl |= 1 << (lev & 31);
+ }
+
+ // simplify (full)
+ for (i = j = 1; i < veci_size(learnt); i++){
+ if (reasons[lit_var(lits[i])] == 0 || !sat_solver_lit_removable(s,lits[i],minl))
+ lits[j++] = lits[i];
+ }
+
+ // update size of learnt + statistics
+ s->stats.max_literals += veci_size(learnt);
+ veci_resize(learnt,j);
+ s->stats.tot_literals += j;
+
+ // clear tags
+ tagged = veci_begin(&s->tagged);
+ for (i = 0; i < veci_size(&s->tagged); i++)
+ tags[tagged[i]] = l_Undef;
+ veci_resize(&s->tagged,0);
+
+#ifdef DEBUG
+ for (i = 0; i < s->size; i++)
+ assert(tags[i] == l_Undef);
+#endif
+
+#ifdef VERBOSEDEBUG
+ printf(L_IND"Learnt {", L_ind);
+ for (i = 0; i < veci_size(learnt); i++) printf(" "L_LIT, L_lit(lits[i]));
+#endif
+ if (veci_size(learnt) > 1){
+ int max_i = 1;
+ int max = levels[lit_var(lits[1])];
+ lit tmp;
+
+ for (i = 2; i < veci_size(learnt); i++)
+ if (levels[lit_var(lits[i])] > max){
+ max = levels[lit_var(lits[i])];
+ max_i = i;
+ }
+
+ tmp = lits[1];
+ lits[1] = lits[max_i];
+ lits[max_i] = tmp;
+ }
+#ifdef VERBOSEDEBUG
+ {
+ int lev = veci_size(learnt) > 1 ? levels[lit_var(lits[1])] : 0;
+ printf(" } at level %d\n", lev);
+ }
+#endif
+}
+
+
+clause* sat_solver_propagate(sat_solver* s)
+{
+ lbool* values = s->assigns;
+ clause* confl = (clause*)0;
+ lit* lits;
+
+ //printf("sat_solver_propagate\n");
+ while (confl == 0 && s->qtail - s->qhead > 0){
+ lit p = s->trail[s->qhead++];
+ vecp* ws = sat_solver_read_wlist(s,p);
+ clause **begin = (clause**)vecp_begin(ws);
+ clause **end = begin + vecp_size(ws);
+ clause **i, **j;
+
+ s->stats.propagations++;
+ s->simpdb_props--;
+
+ //printf("checking lit %d: "L_LIT"\n", veci_size(ws), L_lit(p));
+ for (i = j = begin; i < end; ){
+ if (clause_is_lit(*i)){
+// s->stats.inspects2++;
+ *j++ = *i;
+ if (!enqueue(s,clause_read_lit(*i),clause_from_lit(p))){
+ confl = s->binary;
+ (clause_begin(confl))[1] = lit_neg(p);
+ (clause_begin(confl))[0] = clause_read_lit(*i++);
+ // Copy the remaining watches:
+// s->stats.inspects2 += end - i;
+ while (i < end)
+ *j++ = *i++;
+ }
+ }else{
+ lit false_lit;
+ lbool sig;
+
+ lits = clause_begin(*i);
+
+ // Make sure the false literal is data[1]:
+ false_lit = lit_neg(p);
+ if (lits[0] == false_lit){
+ lits[0] = lits[1];
+ lits[1] = false_lit;
+ }
+ assert(lits[1] == false_lit);
+ //printf("checking clause: "); printlits(lits, lits+clause_size(*i)); printf("\n");
+
+ // If 0th watch is true, then clause is already satisfied.
+ sig = !lit_sign(lits[0]); sig += sig - 1;
+ if (values[lit_var(lits[0])] == sig){
+ *j++ = *i;
+ }else{
+ // Look for new watch:
+ lit* stop = lits + clause_size(*i);
+ lit* k;
+ for (k = lits + 2; k < stop; k++){
+ lbool sig = lit_sign(*k); sig += sig - 1;
+ if (values[lit_var(*k)] != sig){
+ lits[1] = *k;
+ *k = false_lit;
+ vecp_push(sat_solver_read_wlist(s,lit_neg(lits[1])),*i);
+ goto next; }
+ }
+
+ *j++ = *i;
+ // Clause is unit under assignment:
+ if (!enqueue(s,lits[0], *i)){
+ confl = *i++;
+ // Copy the remaining watches:
+// s->stats.inspects2 += end - i;
+ while (i < end)
+ *j++ = *i++;
+ }
+ }
+ }
+ next:
+ i++;
+ }
+
+ s->stats.inspects += j - (clause**)vecp_begin(ws);
+ vecp_resize(ws,j - (clause**)vecp_begin(ws));
+ }
+
+ return confl;
+}
+
+static inline int clause_cmp (const void* x, const void* y) {
+ return clause_size((clause*)x) > 2 && (clause_size((clause*)y) == 2 || clause_activity((clause*)x) < clause_activity((clause*)y)) ? -1 : 1; }
+
+void sat_solver_reducedb(sat_solver* s)
+{
+ int i, j;
+ double extra_lim = s->cla_inc / vecp_size(&s->learnts); // Remove any clause below this activity
+ clause** learnts = (clause**)vecp_begin(&s->learnts);
+ clause** reasons = s->reasons;
+
+ sat_solver_sort(vecp_begin(&s->learnts), vecp_size(&s->learnts), &clause_cmp);
+
+ for (i = j = 0; i < vecp_size(&s->learnts) / 2; i++){
+ if (clause_size(learnts[i]) > 2 && reasons[lit_var(*clause_begin(learnts[i]))] != learnts[i])
+ clause_remove(s,learnts[i]);
+ else
+ learnts[j++] = learnts[i];
+ }
+ for (; i < vecp_size(&s->learnts); i++){
+ if (clause_size(learnts[i]) > 2 && reasons[lit_var(*clause_begin(learnts[i]))] != learnts[i] && clause_activity(learnts[i]) < extra_lim)
+ clause_remove(s,learnts[i]);
+ else
+ learnts[j++] = learnts[i];
+ }
+
+ //printf("reducedb deleted %d\n", vecp_size(&s->learnts) - j);
+
+
+ vecp_resize(&s->learnts,j);
+}
+
+static lbool sat_solver_search(sat_solver* s, sint64 nof_conflicts, sint64 nof_learnts)
+{
+ int* levels = s->levels;
+ double var_decay = 0.95;
+ double clause_decay = 0.999;
+ double random_var_freq = 0.02;
+
+ sint64 conflictC = 0;
+ veci learnt_clause;
+ int i;
+
+ assert(s->root_level == sat_solver_dlevel(s));
+
+ s->nRestarts++;
+ s->stats.starts++;
+ s->var_decay = (float)(1 / var_decay );
+ s->cla_decay = (float)(1 / clause_decay);
+ veci_resize(&s->model,0);
+ veci_new(&learnt_clause);
+
+ // use activity factors in every even restart
+ if ( (s->nRestarts & 1) && veci_size(&s->act_vars) > 0 )
+ for ( i = 0; i < s->act_vars.size; i++ )
+ act_var_bump_factor(s, s->act_vars.ptr[i]);
+
+ for (;;){
+ clause* confl = sat_solver_propagate(s);
+ if (confl != 0){
+ // CONFLICT
+ int blevel;
+
+#ifdef VERBOSEDEBUG
+ printf(L_IND"**CONFLICT**\n", L_ind);
+#endif
+ s->stats.conflicts++; conflictC++;
+ if (sat_solver_dlevel(s) == s->root_level){
+ veci_delete(&learnt_clause);
+ return l_False;
+ }
+
+ veci_resize(&learnt_clause,0);
+ sat_solver_analyze(s, confl, &learnt_clause);
+ blevel = veci_size(&learnt_clause) > 1 ? levels[lit_var(veci_begin(&learnt_clause)[1])] : s->root_level;
+ blevel = s->root_level > blevel ? s->root_level : blevel;
+ sat_solver_canceluntil(s,blevel);
+ sat_solver_record(s,&learnt_clause);
+ act_var_decay(s);
+ act_clause_decay(s);
+
+ }else{
+ // NO CONFLICT
+ int next;
+
+ if (nof_conflicts >= 0 && conflictC >= nof_conflicts){
+ // Reached bound on number of conflicts:
+ s->progress_estimate = sat_solver_progress(s);
+ sat_solver_canceluntil(s,s->root_level);
+ veci_delete(&learnt_clause);
+ return l_Undef; }
+
+ if ( (s->nConfLimit && s->stats.conflicts > s->nConfLimit) ||
+ (s->nInsLimit && s->stats.inspects > s->nInsLimit) )
+ {
+ // Reached bound on number of conflicts:
+ s->progress_estimate = sat_solver_progress(s);
+ sat_solver_canceluntil(s,s->root_level);
+ veci_delete(&learnt_clause);
+ return l_Undef;
+ }
+
+ if (sat_solver_dlevel(s) == 0 && !s->fSkipSimplify)
+ // Simplify the set of problem clauses:
+ sat_solver_simplify(s);
+
+ if (nof_learnts >= 0 && vecp_size(&s->learnts) - s->qtail >= nof_learnts)
+ // Reduce the set of learnt clauses:
+ sat_solver_reducedb(s);
+
+ // New variable decision:
+ s->stats.decisions++;
+ next = order_select(s,(float)random_var_freq);
+
+ if (next == var_Undef){
+ // Model found:
+ lbool* values = s->assigns;
+ int i;
+ veci_resize(&s->model, 0);
+ for (i = 0; i < s->size; i++)
+ veci_push(&s->model,(int)values[i]);
+ sat_solver_canceluntil(s,s->root_level);
+ veci_delete(&learnt_clause);
+
+ /*
+ veci apa; veci_new(&apa);
+ for (i = 0; i < s->size; i++)
+ veci_push(&apa,(int)(s->model.ptr[i] == l_True ? toLit(i) : lit_neg(toLit(i))));
+ printf("model: "); printlits((lit*)apa.ptr, (lit*)apa.ptr + veci_size(&apa)); printf("\n");
+ veci_delete(&apa);
+ */
+
+ return l_True;
+ }
+
+ assume(s,lit_neg(toLit(next)));
+ }
+ }
+
+ return l_Undef; // cannot happen
+}
+
+//=================================================================================================
+// External solver functions:
+
+sat_solver* sat_solver_new(void)
+{
+ sat_solver* s = (sat_solver*)malloc(sizeof(sat_solver));
+ memset( s, 0, sizeof(sat_solver) );
+
+ // initialize vectors
+ vecp_new(&s->clauses);
+ vecp_new(&s->learnts);
+ veci_new(&s->order);
+ veci_new(&s->trail_lim);
+ veci_new(&s->tagged);
+ veci_new(&s->stack);
+ veci_new(&s->model);
+ veci_new(&s->act_vars);
+
+ // initialize arrays
+ s->wlists = 0;
+ s->activity = 0;
+ s->factors = 0;
+ s->assigns = 0;
+ s->orderpos = 0;
+ s->reasons = 0;
+ s->levels = 0;
+ s->tags = 0;
+ s->trail = 0;
+
+
+ // initialize other vars
+ s->size = 0;
+ s->cap = 0;
+ s->qhead = 0;
+ s->qtail = 0;
+ s->cla_inc = 1;
+ s->cla_decay = 1;
+ s->var_inc = 1;
+ s->var_decay = 1;
+ s->root_level = 0;
+ s->simpdb_assigns = 0;
+ s->simpdb_props = 0;
+ s->random_seed = 91648253;
+ s->progress_estimate = 0;
+ s->binary = (clause*)malloc(sizeof(clause) + sizeof(lit)*2);
+ s->binary->size_learnt = (2 << 1);
+ s->verbosity = 0;
+
+ s->stats.starts = 0;
+ s->stats.decisions = 0;
+ s->stats.propagations = 0;
+ s->stats.inspects = 0;
+ s->stats.conflicts = 0;
+ s->stats.clauses = 0;
+ s->stats.clauses_literals = 0;
+ s->stats.learnts = 0;
+ s->stats.learnts_literals = 0;
+ s->stats.max_literals = 0;
+ s->stats.tot_literals = 0;
+
+#ifdef SAT_USE_SYSTEM_MEMORY_MANAGEMENT
+ s->pMem = NULL;
+#else
+ s->pMem = Sat_MmStepStart( 10 );
+#endif
+ return s;
+}
+
+
+void sat_solver_delete(sat_solver* s)
+{
+
+#ifdef SAT_USE_SYSTEM_MEMORY_MANAGEMENT
+ int i;
+ for (i = 0; i < vecp_size(&s->clauses); i++)
+ free(vecp_begin(&s->clauses)[i]);
+ for (i = 0; i < vecp_size(&s->learnts); i++)
+ free(vecp_begin(&s->learnts)[i]);
+#else
+ Sat_MmStepStop( s->pMem, 0 );
+#endif
+
+ // delete vectors
+ vecp_delete(&s->clauses);
+ vecp_delete(&s->learnts);
+ veci_delete(&s->order);
+ veci_delete(&s->trail_lim);
+ veci_delete(&s->tagged);
+ veci_delete(&s->stack);
+ veci_delete(&s->model);
+ veci_delete(&s->act_vars);
+ free(s->binary);
+
+ // delete arrays
+ if (s->wlists != 0){
+ int i;
+ for (i = 0; i < s->size*2; i++)
+ vecp_delete(&s->wlists[i]);
+
+ // if one is different from null, all are
+ free(s->wlists );
+ free(s->activity );
+ free(s->factors );
+ free(s->assigns );
+ free(s->orderpos );
+ free(s->reasons );
+ free(s->levels );
+ free(s->trail );
+ free(s->tags );
+ }
+
+ sat_solver_store_free(s);
+ free(s);
+}
+
+
+bool sat_solver_addclause(sat_solver* s, lit* begin, lit* end)
+{
+ lit *i,*j;
+ int maxvar;
+ lbool* values;
+ lit last;
+
+ if (begin == end) return false;
+
+ //printlits(begin,end); printf("\n");
+ // insertion sort
+ maxvar = lit_var(*begin);
+ for (i = begin + 1; i < end; i++){
+ lit l = *i;
+ maxvar = lit_var(l) > maxvar ? lit_var(l) : maxvar;
+ for (j = i; j > begin && *(j-1) > l; j--)
+ *j = *(j-1);
+ *j = l;
+ }
+ sat_solver_setnvars(s,maxvar+1);
+// sat_solver_setnvars(s, lit_var(*(end-1))+1 );
+
+ //printlits(begin,end); printf("\n");
+ values = s->assigns;
+
+ // delete duplicates
+ last = lit_Undef;
+ for (i = j = begin; i < end; i++){
+ //printf("lit: "L_LIT", value = %d\n", L_lit(*i), (lit_sign(*i) ? -values[lit_var(*i)] : values[lit_var(*i)]));
+ lbool sig = !lit_sign(*i); sig += sig - 1;
+ if (*i == lit_neg(last) || sig == values[lit_var(*i)])
+ return true; // tautology
+ else if (*i != last && values[lit_var(*i)] == l_Undef)
+ last = *j++ = *i;
+ }
+
+ //printf("final: "); printlits(begin,j); printf("\n");
+
+ if (j == begin) // empty clause
+ return false;
+
+ ///////////////////////////////////
+ // add clause to internal storage
+ if ( s->pStore )
+ {
+ extern int Sto_ManAddClause( void * p, lit * pBeg, lit * pEnd );
+ int RetValue = Sto_ManAddClause( s->pStore, begin, j );
+ assert( RetValue );
+ }
+ ///////////////////////////////////
+
+ if (j - begin == 1) // unit clause
+ return enqueue(s,*begin,(clause*)0);
+
+ // create new clause
+ vecp_push(&s->clauses,clause_new(s,begin,j,0));
+
+
+ s->stats.clauses++;
+ s->stats.clauses_literals += j - begin;
+
+ return true;
+}
+
+
+bool sat_solver_simplify(sat_solver* s)
+{
+ clause** reasons;
+ int type;
+
+ assert(sat_solver_dlevel(s) == 0);
+
+ if (sat_solver_propagate(s) != 0)
+ return false;
+
+ if (s->qhead == s->simpdb_assigns || s->simpdb_props > 0)
+ return true;
+
+ reasons = s->reasons;
+ for (type = 0; type < 2; type++){
+ vecp* cs = type ? &s->learnts : &s->clauses;
+ clause** cls = (clause**)vecp_begin(cs);
+
+ int i, j;
+ for (j = i = 0; i < vecp_size(cs); i++){
+ if (reasons[lit_var(*clause_begin(cls[i]))] != cls[i] &&
+ clause_simplify(s,cls[i]) == l_True)
+ clause_remove(s,cls[i]);
+ else
+ cls[j++] = cls[i];
+ }
+ vecp_resize(cs,j);
+ }
+
+ s->simpdb_assigns = s->qhead;
+ // (shouldn't depend on 'stats' really, but it will do for now)
+ s->simpdb_props = (int)(s->stats.clauses_literals + s->stats.learnts_literals);
+
+ return true;
+}
+
+
+int sat_solver_solve(sat_solver* s, lit* begin, lit* end, sint64 nConfLimit, sint64 nInsLimit, sint64 nConfLimitGlobal, sint64 nInsLimitGlobal)
+{
+ sint64 nof_conflicts = 100;
+ sint64 nof_learnts = sat_solver_nclauses(s) / 3;
+ lbool status = l_Undef;
+ lbool* values = s->assigns;
+ lit* i;
+
+ // set the external limits
+ s->nCalls++;
+ s->nRestarts = 0;
+ s->nConfLimit = 0;
+ s->nInsLimit = 0;
+ if ( nConfLimit )
+ s->nConfLimit = s->stats.conflicts + nConfLimit;
+ if ( nInsLimit )
+ s->nInsLimit = s->stats.inspects + nInsLimit;
+ if ( nConfLimitGlobal && (s->nConfLimit == 0 || s->nConfLimit > nConfLimitGlobal) )
+ s->nConfLimit = nConfLimitGlobal;
+ if ( nInsLimitGlobal && (s->nInsLimit == 0 || s->nInsLimit > nInsLimitGlobal) )
+ s->nInsLimit = nInsLimitGlobal;
+
+ //printf("solve: "); printlits(begin, end); printf("\n");
+ for (i = begin; i < end; i++){
+ switch (lit_sign(*i) ? -values[lit_var(*i)] : values[lit_var(*i)]){
+ case 1: /* l_True: */
+ break;
+ case 0: /* l_Undef */
+ assume(s, *i);
+ if (sat_solver_propagate(s) == NULL)
+ break;
+ // fallthrough
+ case -1: /* l_False */
+ sat_solver_canceluntil(s, 0);
+ return l_False;
+ }
+ }
+ s->nCalls2++;
+
+ s->root_level = sat_solver_dlevel(s);
+
+ if (s->verbosity >= 1){
+ printf("==================================[MINISAT]===================================\n");
+ printf("| Conflicts | ORIGINAL | LEARNT | Progress |\n");
+ printf("| | Clauses Literals | Limit Clauses Literals Lit/Cl | |\n");
+ printf("==============================================================================\n");
+ }
+
+ while (status == l_Undef){
+ double Ratio = (s->stats.learnts == 0)? 0.0 :
+ s->stats.learnts_literals / (double)s->stats.learnts;
+
+ if (s->verbosity >= 1)
+ {
+ printf("| %9.0f | %7.0f %8.0f | %7.0f %7.0f %8.0f %7.1f | %6.3f %% |\n",
+ (double)s->stats.conflicts,
+ (double)s->stats.clauses,
+ (double)s->stats.clauses_literals,
+ (double)nof_learnts,
+ (double)s->stats.learnts,
+ (double)s->stats.learnts_literals,
+ Ratio,
+ s->progress_estimate*100);
+ fflush(stdout);
+ }
+ status = sat_solver_search(s, nof_conflicts, nof_learnts);
+ nof_conflicts = nof_conflicts * 3 / 2; //*= 1.5;
+ nof_learnts = nof_learnts * 11 / 10; //*= 1.1;
+
+ // quit the loop if reached an external limit
+ if ( s->nConfLimit && s->stats.conflicts > s->nConfLimit )
+ {
+// printf( "Reached the limit on the number of conflicts (%d).\n", s->nConfLimit );
+ break;
+ }
+ if ( s->nInsLimit && s->stats.inspects > s->nInsLimit )
+ {
+// printf( "Reached the limit on the number of implications (%d).\n", s->nInsLimit );
+ break;
+ }
+ }
+ if (s->verbosity >= 1)
+ printf("==============================================================================\n");
+
+ sat_solver_canceluntil(s,0);
+
+ ////////////////////////////////////////////////
+ if ( status == l_False && s->pStore )
+ {
+ extern int Sto_ManAddClause( void * p, lit * pBeg, lit * pEnd );
+ int RetValue = Sto_ManAddClause( s->pStore, NULL, NULL );
+ assert( RetValue );
+ }
+ ////////////////////////////////////////////////
+ return status;
+}
+
+
+int sat_solver_nvars(sat_solver* s)
+{
+ return s->size;
+}
+
+
+int sat_solver_nclauses(sat_solver* s)
+{
+ return vecp_size(&s->clauses);
+}
+
+
+int sat_solver_nconflicts(sat_solver* s)
+{
+ return (int)s->stats.conflicts;
+}
+
+//=================================================================================================
+// Clause storage functions:
+
+void sat_solver_store_alloc( sat_solver * s )
+{
+ extern void * Sto_ManAlloc();
+ assert( s->pStore == NULL );
+ s->pStore = Sto_ManAlloc();
+}
+
+void sat_solver_store_write( sat_solver * s, char * pFileName )
+{
+ extern void Sto_ManDumpClauses( void * p, char * pFileName );
+ if ( s->pStore ) Sto_ManDumpClauses( s->pStore, pFileName );
+}
+
+void sat_solver_store_free( sat_solver * s )
+{
+ extern void Sto_ManFree( void * p );
+ if ( s->pStore ) Sto_ManFree( s->pStore );
+ s->pStore = NULL;
+}
+
+void sat_solver_store_mark_roots( sat_solver * s )
+{
+ extern void Sto_ManMarkRoots( void * p );
+ if ( s->pStore ) Sto_ManMarkRoots( s->pStore );
+}
+
+void sat_solver_store_mark_clauses_a( sat_solver * s )
+{
+ extern void Sto_ManMarkClausesA( void * p );
+ if ( s->pStore ) Sto_ManMarkClausesA( s->pStore );
+}
+
+void * sat_solver_store_release( sat_solver * s )
+{
+ void * pTemp;
+ if ( s->pStore == NULL )
+ return NULL;
+ pTemp = s->pStore;
+ s->pStore = NULL;
+ return pTemp;
+}
+
+//=================================================================================================
+// Sorting functions (sigh):
+
+static inline void selectionsort(void** array, int size, int(*comp)(const void *, const void *))
+{
+ int i, j, best_i;
+ void* tmp;
+
+ for (i = 0; i < size-1; i++){
+ best_i = i;
+ for (j = i+1; j < size; j++){
+ if (comp(array[j], array[best_i]) < 0)
+ best_i = j;
+ }
+ tmp = array[i]; array[i] = array[best_i]; array[best_i] = tmp;
+ }
+}
+
+
+static void sortrnd(void** array, int size, int(*comp)(const void *, const void *), double* seed)
+{
+ if (size <= 15)
+ selectionsort(array, size, comp);
+
+ else{
+ void* pivot = array[irand(seed, size)];
+ void* tmp;
+ int i = -1;
+ int j = size;
+
+ for(;;){
+ do i++; while(comp(array[i], pivot)<0);
+ do j--; while(comp(pivot, array[j])<0);
+
+ if (i >= j) break;
+
+ tmp = array[i]; array[i] = array[j]; array[j] = tmp;
+ }
+
+ sortrnd(array , i , comp, seed);
+ sortrnd(&array[i], size-i, comp, seed);
+ }
+}
+
+void sat_solver_sort(void** array, int size, int(*comp)(const void *, const void *))
+{
+ double seed = 91648253;
+ sortrnd(array,size,comp,&seed);
+}
diff --git a/src/sat/bsat/satSolver.h b/src/sat/bsat/satSolver.h
new file mode 100644
index 00000000..542b9895
--- /dev/null
+++ b/src/sat/bsat/satSolver.h
@@ -0,0 +1,191 @@
+/**************************************************************************************************
+MiniSat -- Copyright (c) 2005, Niklas Sorensson
+http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
+associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute,
+sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or
+substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
+NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
+OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+**************************************************************************************************/
+// Modified to compile with MS Visual Studio 6.0 by Alan Mishchenko
+
+#ifndef satSolver_h
+#define satSolver_h
+
+#ifdef _WIN32
+#define inline __inline // compatible with MS VS 6.0
+#endif
+
+#include "satVec.h"
+#include "satMem.h"
+
+//=================================================================================================
+// Simple types:
+
+// does not work for c++
+//typedef int bool;
+#ifndef __cplusplus
+#ifndef bool
+#define bool int
+#endif
+#endif
+
+static const bool true = 1;
+static const bool false = 0;
+
+typedef int lit;
+typedef char lbool;
+
+#ifndef SINT64
+#define SINT64
+
+#ifdef _WIN32
+typedef signed __int64 sint64; // compatible with MS VS 6.0
+#else
+typedef long long sint64;
+#endif
+
+#endif
+
+static const int var_Undef = -1;
+static const lit lit_Undef = -2;
+
+static const lbool l_Undef = 0;
+static const lbool l_True = 1;
+static const lbool l_False = -1;
+
+static inline lit toLit (int v) { return v + v; }
+static inline lit toLitCond(int v, int c) { return v + v + (c != 0); }
+static inline lit lit_neg (lit l) { return l ^ 1; }
+static inline int lit_var (lit l) { return l >> 1; }
+static inline int lit_sign (lit l) { return l & 1; }
+static inline int lit_print(lit l) { return lit_sign(l)? -lit_var(l)-1 : lit_var(l)+1; }
+static inline lit lit_read (int s) { return s > 0 ? toLit(s-1) : lit_neg(toLit(-s-1)); }
+
+
+//=================================================================================================
+// Public interface:
+
+struct sat_solver_t;
+typedef struct sat_solver_t sat_solver;
+
+extern sat_solver* sat_solver_new(void);
+extern void sat_solver_delete(sat_solver* s);
+
+extern bool sat_solver_addclause(sat_solver* s, lit* begin, lit* end);
+extern bool sat_solver_simplify(sat_solver* s);
+extern int sat_solver_solve(sat_solver* s, lit* begin, lit* end, sint64 nConfLimit, sint64 nInsLimit, sint64 nConfLimitGlobal, sint64 nInsLimitGlobal);
+
+extern int sat_solver_nvars(sat_solver* s);
+extern int sat_solver_nclauses(sat_solver* s);
+extern int sat_solver_nconflicts(sat_solver* s);
+
+extern void sat_solver_setnvars(sat_solver* s,int n);
+
+struct stats_t
+{
+ sint64 starts, decisions, propagations, inspects, conflicts;
+ sint64 clauses, clauses_literals, learnts, learnts_literals, max_literals, tot_literals;
+};
+typedef struct stats_t stats;
+
+extern void Sat_SolverWriteDimacs( sat_solver * p, char * pFileName, lit* assumptionsBegin, lit* assumptionsEnd, int incrementVars );
+extern void Sat_SolverPrintStats( FILE * pFile, sat_solver * p );
+extern int * Sat_SolverGetModel( sat_solver * p, int * pVars, int nVars );
+extern void Sat_SolverDoubleClauses( sat_solver * p, int iVar );
+
+// trace recording
+extern void Sat_SolverTraceStart( sat_solver * pSat, char * pName );
+extern void Sat_SolverTraceStop( sat_solver * pSat );
+extern void Sat_SolverTraceWrite( sat_solver * pSat, int * pBeg, int * pEnd, int fRoot );
+
+// clause storage
+extern void sat_solver_store_alloc( sat_solver * s );
+extern void sat_solver_store_write( sat_solver * s, char * pFileName );
+extern void sat_solver_store_free( sat_solver * s );
+extern void sat_solver_store_mark_roots( sat_solver * s );
+extern void sat_solver_store_mark_clauses_a( sat_solver * s );
+extern void * sat_solver_store_release( sat_solver * s );
+
+//=================================================================================================
+// Solver representation:
+
+struct clause_t;
+typedef struct clause_t clause;
+
+struct sat_solver_t
+{
+ int size; // nof variables
+ int cap; // size of varmaps
+ int qhead; // Head index of queue.
+ int qtail; // Tail index of queue.
+
+ // clauses
+ vecp clauses; // List of problem constraints. (contains: clause*)
+ vecp learnts; // List of learnt clauses. (contains: clause*)
+
+ // activities
+ double var_inc; // Amount to bump next variable with.
+ double var_decay; // INVERSE decay factor for variable activity: stores 1/decay.
+ float cla_inc; // Amount to bump next clause with.
+ float cla_decay; // INVERSE decay factor for clause activity: stores 1/decay.
+
+ vecp* wlists; //
+ double* activity; // A heuristic measurement of the activity of a variable.
+ lbool* assigns; // Current values of variables.
+ int* orderpos; // Index in variable order.
+ clause** reasons; //
+ int* levels; //
+ lit* trail;
+
+ clause* binary; // A temporary binary clause
+ lbool* tags; //
+ veci tagged; // (contains: var)
+ veci stack; // (contains: var)
+
+ veci order; // Variable order. (heap) (contains: var)
+ veci trail_lim; // Separator indices for different decision levels in 'trail'. (contains: int)
+ veci model; // If problem is solved, this vector contains the model (contains: lbool).
+
+ int root_level; // Level of first proper decision.
+ int simpdb_assigns;// Number of top-level assignments at last 'simplifyDB()'.
+ int simpdb_props; // Number of propagations before next 'simplifyDB()'.
+ double random_seed;
+ double progress_estimate;
+ int verbosity; // Verbosity level. 0=silent, 1=some progress report, 2=everything
+
+ stats stats;
+
+ sint64 nConfLimit; // external limit on the number of conflicts
+ sint64 nInsLimit; // external limit on the number of implications
+
+ veci act_vars; // variables whose activity has changed
+ double* factors; // the activity factors
+ int nRestarts; // the number of local restarts
+ int nCalls; // the number of local restarts
+ int nCalls2; // the number of local restarts
+
+ Sat_MmStep_t * pMem;
+
+ int fSkipSimplify; // set to one to skip simplification of the clause database
+
+ // clause store
+ void * pStore;
+
+ // trace recording
+ FILE * pFile;
+ int nClauses;
+ int nRoots;
+};
+
+#endif
diff --git a/src/sat/bsat/satStore.c b/src/sat/bsat/satStore.c
new file mode 100644
index 00000000..f968162e
--- /dev/null
+++ b/src/sat/bsat/satStore.c
@@ -0,0 +1,437 @@
+/**CFile****************************************************************
+
+ FileName [satStore.c]
+
+ SystemName [ABC: Logic synthesis and verification system.]
+
+ PackageName [SAT solver.]
+
+ Synopsis [Records the trace of SAT solving in the CNF form.]
+
+ Author [Alan Mishchenko]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - June 20, 2005.]
+
+ Revision [$Id: satStore.c,v 1.4 2005/09/16 22:55:03 casem Exp $]
+
+***********************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+#include "satStore.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Fetches memory.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+char * Sto_ManMemoryFetch( Sto_Man_t * p, int nBytes )
+{
+ char * pMem;
+ if ( p->pChunkLast == NULL || nBytes > p->nChunkSize - p->nChunkUsed )
+ {
+ pMem = (char *)malloc( p->nChunkSize );
+ *(char **)pMem = p->pChunkLast;
+ p->pChunkLast = pMem;
+ p->nChunkUsed = sizeof(char *);
+ }
+ pMem = p->pChunkLast + p->nChunkUsed;
+ p->nChunkUsed += nBytes;
+ return pMem;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Frees memory manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sto_ManMemoryStop( Sto_Man_t * p )
+{
+ char * pMem, * pNext;
+ if ( p->pChunkLast == NULL )
+ return;
+ for ( pMem = p->pChunkLast; (pNext = *(char **)pMem); pMem = pNext )
+ free( pMem );
+ free( pMem );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reports memory usage in bytes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Sto_ManMemoryReport( Sto_Man_t * p )
+{
+ int Total;
+ char * pMem, * pNext;
+ if ( p->pChunkLast == NULL )
+ return 0;
+ Total = p->nChunkUsed;
+ for ( pMem = p->pChunkLast; (pNext = *(char **)pMem); pMem = pNext )
+ Total += p->nChunkSize;
+ return Total;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Allocate proof manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Sto_Man_t * Sto_ManAlloc()
+{
+ Sto_Man_t * p;
+ // allocate the manager
+ p = (Sto_Man_t *)malloc( sizeof(Sto_Man_t) );
+ memset( p, 0, sizeof(Sto_Man_t) );
+ // memory management
+ p->nChunkSize = (1<<16); // use 64K chunks
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Deallocate proof manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sto_ManFree( Sto_Man_t * p )
+{
+ Sto_ManMemoryStop( p );
+ free( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Adds one clause to the manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Sto_ManAddClause( Sto_Man_t * p, lit * pBeg, lit * pEnd )
+{
+ Sto_Cls_t * pClause;
+ lit Lit, * i, * j;
+ int nSize;
+
+ // process the literals
+ if ( pBeg < pEnd )
+ {
+ // insertion sort
+ for ( i = pBeg + 1; i < pEnd; i++ )
+ {
+ Lit = *i;
+ for ( j = i; j > pBeg && *(j-1) > Lit; j-- )
+ *j = *(j-1);
+ *j = Lit;
+ }
+ // make sure there is no duplicated variables
+ for ( i = pBeg + 1; i < pEnd; i++ )
+ if ( lit_var(*(i-1)) == lit_var(*i) )
+ {
+ printf( "The clause contains two literals of the same variable: %d and %d.\n", *(i-1), *i );
+ return 0;
+ }
+ // check the largest var size
+ p->nVars = STO_MAX( p->nVars, lit_var(*(pEnd-1)) + 1 );
+ }
+
+ // get memory for the clause
+ nSize = sizeof(Sto_Cls_t) + sizeof(lit) * (pEnd - pBeg);
+ pClause = (Sto_Cls_t *)Sto_ManMemoryFetch( p, nSize );
+ memset( pClause, 0, sizeof(Sto_Cls_t) );
+
+ // assign the clause
+ pClause->Id = p->nClauses++;
+ pClause->nLits = pEnd - pBeg;
+ memcpy( pClause->pLits, pBeg, sizeof(lit) * (pEnd - pBeg) );
+
+ // add the clause to the list
+ if ( p->pHead == NULL )
+ p->pHead = pClause;
+ if ( p->pTail == NULL )
+ p->pTail = pClause;
+ else
+ {
+ p->pTail->pNext = pClause;
+ p->pTail = pClause;
+ }
+
+ // add the empty clause
+ if ( pClause->nLits == 0 )
+ {
+ if ( p->pEmpty )
+ {
+ printf( "More than one empty clause!\n" );
+ return 0;
+ }
+ p->pEmpty = pClause;
+ }
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Mark all clauses added so far as root clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sto_ManMarkRoots( Sto_Man_t * p )
+{
+ Sto_Cls_t * pClause;
+ p->nRoots = 0;
+ Sto_ManForEachClause( p, pClause )
+ {
+ pClause->fRoot = 1;
+ p->nRoots++;
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Mark all clauses added so far as clause of A.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sto_ManMarkClausesA( Sto_Man_t * p )
+{
+ Sto_Cls_t * pClause;
+ p->nClausesA = 0;
+ Sto_ManForEachClause( p, pClause )
+ {
+ pClause->fA = 1;
+ p->nClausesA++;
+ }
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Writes the stored clauses into a file.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sto_ManDumpClauses( Sto_Man_t * p, char * pFileName )
+{
+ FILE * pFile;
+ Sto_Cls_t * pClause;
+ int i;
+ // start the file
+ pFile = fopen( pFileName, "w" );
+ if ( pFile == NULL )
+ {
+ printf( "Error: Cannot open output file (%s).\n", pFileName );
+ return;
+ }
+ // write the data
+ fprintf( pFile, "p %d %d %d %d\n", p->nVars, p->nClauses, p->nRoots, p->nClausesA );
+ Sto_ManForEachClause( p, pClause )
+ {
+ for ( i = 0; i < (int)pClause->nLits; i++ )
+ fprintf( pFile, " %d", lit_print(pClause->pLits[i]) );
+ fprintf( pFile, "\n" );
+ }
+ fprintf( pFile, " 0\n" );
+ fclose( pFile );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reads one literal from file.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Sto_ManLoadNumber( FILE * pFile, int * pNumber )
+{
+ int Char, Number = 0, Sign = 0;
+ // skip space-like chars
+ do {
+ Char = fgetc( pFile );
+ if ( Char == EOF )
+ return 0;
+ } while ( Char == ' ' || Char == '\t' || Char == '\r' || Char == '\n' );
+ // read the literal
+ while ( 1 )
+ {
+ // get the next character
+ Char = fgetc( pFile );
+ if ( Char == ' ' || Char == '\t' || Char == '\r' || Char == '\n' )
+ break;
+ // check that the char is a digit
+ if ( (Char < '0' || Char > '9') && Char != '-' )
+ {
+ printf( "Error: Wrong char (%c) in the input file.\n", Char );
+ return 0;
+ }
+ // check if this is a minus
+ if ( Char == '-' )
+ Sign = 1;
+ else
+ Number = 10 * Number + Char;
+ }
+ // return the number
+ *pNumber = Sign? -Number : Number;
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reads CNF from file.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Sto_Man_t * Sto_ManLoadClauses( char * pFileName )
+{
+ FILE * pFile;
+ Sto_Man_t * p;
+ Sto_Cls_t * pClause;
+ char pBuffer[1024];
+ int nLits, nLitsAlloc, Counter, Number;
+ lit * pLits;
+
+ // start the file
+ pFile = fopen( pFileName, "r" );
+ if ( pFile == NULL )
+ {
+ printf( "Error: Cannot open input file (%s).\n", pFileName );
+ return NULL;
+ }
+
+ // create the manager
+ p = Sto_ManAlloc();
+
+ // alloc the array of literals
+ nLitsAlloc = 1024;
+ pLits = (lit *)malloc( sizeof(lit) * nLitsAlloc );
+
+ // read file header
+ p->nVars = p->nClauses = p->nRoots = p->nClausesA = 0;
+ while ( fgets( pBuffer, 1024, pFile ) )
+ {
+ if ( pBuffer[0] == 'c' )
+ continue;
+ if ( pBuffer[0] == 'p' )
+ {
+ sscanf( pBuffer + 1, "%d %d %d %d", &p->nVars, &p->nClauses, &p->nRoots, &p->nClausesA );
+ break;
+ }
+ printf( "Warning: Skipping line: \"%s\"\n", pBuffer );
+ }
+
+ // read the clauses
+ nLits = 0;
+ while ( Sto_ManLoadNumber(pFile, &Number) )
+ {
+ if ( Number == 0 )
+ {
+ int RetValue;
+ RetValue = Sto_ManAddClause( p, pLits, pLits + nLits );
+ assert( RetValue );
+ nLits = 0;
+ continue;
+ }
+ if ( nLits == nLitsAlloc )
+ {
+ nLitsAlloc *= 2;
+ pLits = (lit *)realloc( pLits, sizeof(lit) * nLitsAlloc );
+ }
+ pLits[ nLits++ ] = lit_read(Number);
+ }
+ if ( nLits > 0 )
+ printf( "Error: The last clause was not saved.\n" );
+
+ // count clauses
+ Counter = 0;
+ Sto_ManForEachClause( p, pClause )
+ Counter++;
+
+ // check the number of clauses
+ if ( p->nClauses != Counter )
+ {
+ printf( "Error: The actual number of clauses (%d) is different than declared (%d).\n", Counter, p->nClauses );
+ Sto_ManFree( p );
+ return NULL;
+ }
+
+ free( pLits );
+ fclose( pFile );
+ return p;
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/bsat/satStore.h b/src/sat/bsat/satStore.h
new file mode 100644
index 00000000..346b59df
--- /dev/null
+++ b/src/sat/bsat/satStore.h
@@ -0,0 +1,137 @@
+/**CFile****************************************************************
+
+ FileName [pr.h]
+
+ SystemName [ABC: Logic synthesis and verification system.]
+
+ PackageName [Proof recording.]
+
+ Synopsis [External declarations.]
+
+ Author [Alan Mishchenko]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - June 20, 2005.]
+
+ Revision [$Id: pr.h,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#ifndef __PR_H__
+#define __PR_H__
+
+/*
+ The trace of SAT solving contains the original clause of the problem
+ along with the learned clauses derived during SAT solving.
+ The first line of the resulting file contains 3 numbers instead of 2:
+ c <num_vars> <num_all_clauses> <num_root_clauses>
+*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef _WIN32
+#define inline __inline // compatible with MS VS 6.0
+#endif
+
+#ifndef PRT
+#define PRT(a,t) printf("%s = ", (a)); printf("%6.2f sec\n", (float)(t)/(float)(CLOCKS_PER_SEC))
+#endif
+
+#define STO_MAX(a,b) ((a) > (b) ? (a) : (b))
+
+////////////////////////////////////////////////////////////////////////
+/// INCLUDES ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// PARAMETERS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// BASIC TYPES ///
+////////////////////////////////////////////////////////////////////////
+
+typedef unsigned lit;
+
+typedef struct Sto_Cls_t_ Sto_Cls_t;
+struct Sto_Cls_t_
+{
+ Sto_Cls_t * pNext; // the next clause
+ Sto_Cls_t * pNext0; // the next 0-watch
+ Sto_Cls_t * pNext1; // the next 0-watch
+ int Id; // the clause ID
+ unsigned fA : 1; // belongs to A
+ unsigned fRoot : 1; // original clause
+ unsigned fVisit : 1; // visited clause
+ unsigned nLits : 24; // the number of literals
+ lit pLits[0]; // literals of this clause
+};
+
+typedef struct Sto_Man_t_ Sto_Man_t;
+struct Sto_Man_t_
+{
+ // general data
+ int nVars; // the number of variables
+ int nRoots; // the number of root clauses
+ int nClauses; // the number of all clauses
+ int nClausesA; // the number of clauses of A
+ Sto_Cls_t * pHead; // the head clause
+ Sto_Cls_t * pTail; // the tail clause
+ Sto_Cls_t * pEmpty; // the empty clause
+ // memory management
+ int nChunkSize; // the number of bytes in a chunk
+ int nChunkUsed; // the number of bytes used in the last chunk
+ char * pChunkLast; // the last memory chunk
+};
+
+// variable/literal conversions (taken from MiniSat)
+static inline lit toLit (int v) { return v + v; }
+static inline lit toLitCond(int v, int c) { return v + v + (c != 0); }
+static inline lit lit_neg (lit l) { return l ^ 1; }
+static inline int lit_var (lit l) { return l >> 1; }
+static inline int lit_sign (lit l) { return l & 1; }
+static inline int lit_print(lit l) { return lit_sign(l)? -lit_var(l)-1 : lit_var(l)+1; }
+static inline lit lit_read (int s) { return s > 0 ? toLit(s-1) : lit_neg(toLit(-s-1)); }
+static inline int lit_check(lit l, int n) { return l >= 0 && lit_var(l) < n; }
+
+// iterators through the clauses
+#define Sto_ManForEachClause( p, pCls ) for( pCls = p->pHead; pCls; pCls = pCls->pNext )
+#define Sto_ManForEachClauseRoot( p, pCls ) for( pCls = p->pHead; pCls && pCls->fRoot; pCls = pCls->pNext )
+
+////////////////////////////////////////////////////////////////////////
+/// MACRO DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/*=== satStore.c ==========================================================*/
+extern Sto_Man_t * Sto_ManAlloc();
+extern void Sto_ManFree( Sto_Man_t * p );
+extern int Sto_ManAddClause( Sto_Man_t * p, lit * pBeg, lit * pEnd );
+extern int Sto_ManMemoryReport( Sto_Man_t * p );
+extern void Sto_ManMarkRoots( Sto_Man_t * p );
+extern void Sto_ManMarkClausesA( Sto_Man_t * p );
+extern void Sto_ManDumpClauses( Sto_Man_t * p, char * pFileName );
+extern Sto_Man_t * Sto_ManLoadClauses( char * pFileName );
+
+/*=== satInter.c ==========================================================*/
+typedef struct Int_Man_t_ Int_Man_t;
+extern Int_Man_t * Int_ManAlloc( int nVarsAlloc );
+extern void Int_ManFree( Int_Man_t * p );
+extern int Int_ManInterpolate( Int_Man_t * p, Sto_Man_t * pCnf, int fVerbose, unsigned ** ppResult );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
diff --git a/src/sat/bsat/satTrace.c b/src/sat/bsat/satTrace.c
new file mode 100644
index 00000000..111e8dfb
--- /dev/null
+++ b/src/sat/bsat/satTrace.c
@@ -0,0 +1,109 @@
+/**CFile****************************************************************
+
+ FileName [satTrace.c]
+
+ SystemName [ABC: Logic synthesis and verification system.]
+
+ PackageName [SAT sat_solver.]
+
+ Synopsis [Records the trace of SAT solving in the CNF form.]
+
+ Author [Alan Mishchenko]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - June 20, 2005.]
+
+ Revision [$Id: satTrace.c,v 1.4 2005/09/16 22:55:03 casem Exp $]
+
+***********************************************************************/
+
+#include <stdio.h>
+#include <assert.h>
+#include "satSolver.h"
+
+/*
+ The trace of SAT solving contains the original clause of the problem
+ along with the learned clauses derived during SAT solving.
+ The first line of the resulting file contains 3 numbers instead of 2:
+ c <num_vars> <num_all_clauses> <num_root_clauses>
+*/
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Start the trace recording.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_SolverTraceStart( sat_solver * pSat, char * pName )
+{
+ assert( pSat->pFile == NULL );
+ pSat->pFile = fopen( pName, "w" );
+ fprintf( pSat->pFile, " \n" );
+ pSat->nClauses = 0;
+ pSat->nRoots = 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Stops the trace recording.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_SolverTraceStop( sat_solver * pSat )
+{
+ if ( pSat->pFile == NULL )
+ return;
+ rewind( pSat->pFile );
+ fprintf( pSat->pFile, "p %d %d %d", sat_solver_nvars(pSat), pSat->nClauses, pSat->nRoots );
+ fclose( pSat->pFile );
+ pSat->pFile = NULL;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Writes one clause into the trace file.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_SolverTraceWrite( sat_solver * pSat, int * pBeg, int * pEnd, int fRoot )
+{
+ if ( pSat->pFile == NULL )
+ return;
+ pSat->nClauses++;
+ pSat->nRoots += fRoot;
+ for ( ; pBeg < pEnd ; pBeg++ )
+ fprintf( pSat->pFile, " %d", lit_print(*pBeg) );
+ fprintf( pSat->pFile, " 0\n" );
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/bsat/satUtil.c b/src/sat/bsat/satUtil.c
new file mode 100644
index 00000000..3961cf7e
--- /dev/null
+++ b/src/sat/bsat/satUtil.c
@@ -0,0 +1,234 @@
+/**CFile****************************************************************
+
+ FileName [satUtil.c]
+
+ SystemName [ABC: Logic synthesis and verification system.]
+
+ PackageName [C-language MiniSat solver.]
+
+ Synopsis [Additional SAT solver procedures.]
+
+ Author [Alan Mishchenko]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - June 20, 2005.]
+
+ Revision [$Id: satUtil.c,v 1.4 2005/09/16 22:55:03 casem Exp $]
+
+***********************************************************************/
+
+#include <stdio.h>
+#include <assert.h>
+#include "satSolver.h"
+#include "vec.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+struct clause_t
+{
+ int size_learnt;
+ lit lits[0];
+};
+
+static inline int clause_size( clause* c ) { return c->size_learnt >> 1; }
+static inline lit* clause_begin( clause* c ) { return c->lits; }
+
+static void Sat_SolverClauseWriteDimacs( FILE * pFile, clause * pC, bool fIncrement );
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Write the clauses in the solver into a file in DIMACS format.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_SolverWriteDimacs( sat_solver * p, char * pFileName, lit* assumptionsBegin, lit* assumptionsEnd, int incrementVars )
+{
+ FILE * pFile;
+ void ** pClauses;
+ int nClauses, i;
+
+ // count the number of clauses
+ nClauses = p->clauses.size + p->learnts.size;
+ for ( i = 0; i < p->size; i++ )
+ if ( p->levels[i] == 0 && p->assigns[i] != l_Undef )
+ nClauses++;
+
+ // start the file
+ pFile = fopen( pFileName, "wb" );
+ if ( pFile == NULL )
+ {
+ printf( "Sat_SolverWriteDimacs(): Cannot open the ouput file.\n" );
+ return;
+ }
+// fprintf( pFile, "c CNF generated by ABC on %s\n", Extra_TimeStamp() );
+ fprintf( pFile, "p cnf %d %d\n", p->size, nClauses );
+
+ // write the original clauses
+ nClauses = p->clauses.size;
+ pClauses = p->clauses.ptr;
+ for ( i = 0; i < nClauses; i++ )
+ Sat_SolverClauseWriteDimacs( pFile, pClauses[i], incrementVars );
+
+ // write the learned clauses
+ nClauses = p->learnts.size;
+ pClauses = p->learnts.ptr;
+ for ( i = 0; i < nClauses; i++ )
+ Sat_SolverClauseWriteDimacs( pFile, pClauses[i], incrementVars );
+
+ // write zero-level assertions
+ for ( i = 0; i < p->size; i++ )
+ if ( p->levels[i] == 0 && p->assigns[i] != l_Undef )
+ fprintf( pFile, "%s%d%s\n",
+ (p->assigns[i] == l_False)? "-": "",
+ i + (int)(incrementVars>0),
+ (incrementVars) ? " 0" : "");
+
+ // write the assumptions
+ if (assumptionsBegin) {
+ for (; assumptionsBegin != assumptionsEnd; assumptionsBegin++) {
+ fprintf( pFile, "%s%d%s\n",
+ lit_sign(*assumptionsBegin)? "-": "",
+ lit_var(*assumptionsBegin) + (int)(incrementVars>0),
+ (incrementVars) ? " 0" : "");
+ }
+ }
+
+ fprintf( pFile, "\n" );
+ fclose( pFile );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Writes the given clause in a file in DIMACS format.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_SolverClauseWriteDimacs( FILE * pFile, clause * pC, bool fIncrement )
+{
+ lit * pLits = clause_begin(pC);
+ int nLits = clause_size(pC);
+ int i;
+
+ for ( i = 0; i < nLits; i++ )
+ fprintf( pFile, "%s%d ", (lit_sign(pLits[i])? "-": ""), lit_var(pLits[i]) + (int)(fIncrement>0) );
+ if ( fIncrement )
+ fprintf( pFile, "0" );
+ fprintf( pFile, "\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Writes the given clause in a file in DIMACS format.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_SolverPrintStats( FILE * pFile, sat_solver * p )
+{
+// printf( "calls : %8d (%d)\n", (int)p->nCalls, (int)p->nCalls2 );
+ printf( "starts : %8d\n", (int)p->stats.starts );
+ printf( "conflicts : %8d\n", (int)p->stats.conflicts );
+ printf( "decisions : %8d\n", (int)p->stats.decisions );
+ printf( "propagations : %8d\n", (int)p->stats.propagations );
+ printf( "inspects : %8d\n", (int)p->stats.inspects );
+// printf( "inspects2 : %8d\n", (int)p->stats.inspects2 );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns a counter-example.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int * Sat_SolverGetModel( sat_solver * p, int * pVars, int nVars )
+{
+ int * pModel;
+ int i;
+ pModel = ALLOC( int, nVars );
+ for ( i = 0; i < nVars; i++ )
+ {
+ assert( pVars[i] >= 0 && pVars[i] < p->size );
+ pModel[i] = (int)(p->model.ptr[pVars[i]] == l_True);
+ }
+ return pModel;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Duplicates all clauses, complements unit clause of the given var.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Sat_SolverDoubleClauses( sat_solver * p, int iVar )
+{
+ clause * pClause;
+ lit Lit, * pLits;
+ int RetValue, nClauses, nVarsOld, nLitsOld, nLits, c, v;
+ // get the number of variables
+ nVarsOld = p->size;
+ nLitsOld = 2 * p->size;
+ // extend the solver to depend on two sets of variables
+ sat_solver_setnvars( p, 2 * p->size );
+ // duplicate implications
+ for ( v = 0; v < nVarsOld; v++ )
+ if ( p->assigns[v] != l_Undef )
+ {
+ Lit = nLitsOld + toLitCond( v, p->assigns[v]==l_False );
+ if ( v == iVar )
+ Lit = lit_neg(Lit);
+ RetValue = sat_solver_addclause( p, &Lit, &Lit + 1 );
+ assert( RetValue );
+ }
+ // duplicate clauses
+ nClauses = vecp_size(&p->clauses);
+ for ( c = 0; c < nClauses; c++ )
+ {
+ pClause = p->clauses.ptr[c];
+ nLits = clause_size(pClause);
+ pLits = clause_begin(pClause);
+ for ( v = 0; v < nLits; v++ )
+ pLits[v] += nLitsOld;
+ RetValue = sat_solver_addclause( p, pLits, pLits + nLits );
+ assert( RetValue );
+ for ( v = 0; v < nLits; v++ )
+ pLits[v] -= nLitsOld;
+ }
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/bsat/satVec.h b/src/sat/bsat/satVec.h
new file mode 100644
index 00000000..d7fce5c0
--- /dev/null
+++ b/src/sat/bsat/satVec.h
@@ -0,0 +1,83 @@
+/**************************************************************************************************
+MiniSat -- Copyright (c) 2005, Niklas Sorensson
+http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
+associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute,
+sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or
+substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
+NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
+OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+**************************************************************************************************/
+// Modified to compile with MS Visual Studio 6.0 by Alan Mishchenko
+
+#ifndef satVec_h
+#define satVec_h
+
+#include <stdlib.h>
+
+// vector of 32-bit intergers (added for 64-bit portability)
+struct veci_t {
+ int size;
+ int cap;
+ int* ptr;
+};
+typedef struct veci_t veci;
+
+static inline void veci_new (veci* v) {
+ v->size = 0;
+ v->cap = 4;
+ v->ptr = (int*)malloc(sizeof(int)*v->cap);
+}
+
+static inline void veci_delete (veci* v) { free(v->ptr); }
+static inline int* veci_begin (veci* v) { return v->ptr; }
+static inline int veci_size (veci* v) { return v->size; }
+static inline void veci_resize (veci* v, int k) { v->size = k; } // only safe to shrink !!
+static inline void veci_push (veci* v, int e)
+{
+ if (v->size == v->cap) {
+ int newsize = v->cap * 2;//+1;
+ v->ptr = (int*)realloc(v->ptr,sizeof(int)*newsize);
+ v->cap = newsize; }
+ v->ptr[v->size++] = e;
+}
+
+
+// vector of 32- or 64-bit pointers
+struct vecp_t {
+ int size;
+ int cap;
+ void** ptr;
+};
+typedef struct vecp_t vecp;
+
+static inline void vecp_new (vecp* v) {
+ v->size = 0;
+ v->cap = 4;
+ v->ptr = (void**)malloc(sizeof(void*)*v->cap);
+}
+
+static inline void vecp_delete (vecp* v) { free(v->ptr); }
+static inline void** vecp_begin (vecp* v) { return v->ptr; }
+static inline int vecp_size (vecp* v) { return v->size; }
+static inline void vecp_resize (vecp* v, int k) { v->size = k; } // only safe to shrink !!
+static inline void vecp_push (vecp* v, void* e)
+{
+ if (v->size == v->cap) {
+ int newsize = v->cap * 2;//+1;
+ v->ptr = (void**)realloc(v->ptr,sizeof(void*)*newsize);
+ v->cap = newsize; }
+ v->ptr[v->size++] = e;
+}
+
+
+#endif
diff --git a/src/sat/csat/csat_apis.c b/src/sat/csat/csat_apis.c
new file mode 100644
index 00000000..5872f5bc
--- /dev/null
+++ b/src/sat/csat/csat_apis.c
@@ -0,0 +1,769 @@
+/**CFile****************************************************************
+
+ FileName [csat_apis.h]
+
+ PackageName [Interface to CSAT.]
+
+ Synopsis [APIs, enums, and data structures expected from the use of CSAT.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - August 28, 2005]
+
+ Revision [$Id: csat_apis.h,v 1.00 2005/08/28 00:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "abc.h"
+#include "fraig.h"
+#include "csat_apis.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+#define ABC_DEFAULT_CONF_LIMIT 0 // limit on conflicts
+#define ABC_DEFAULT_IMP_LIMIT 0 // limit on implications
+
+
+struct ABC_ManagerStruct_t
+{
+ // information about the problem
+ stmm_table * tName2Node; // the hash table mapping names to nodes
+ stmm_table * tNode2Name; // the hash table mapping nodes to names
+ Abc_Ntk_t * pNtk; // the starting ABC network
+ Abc_Ntk_t * pTarget; // the AIG representing the target
+ char * pDumpFileName; // the name of the file to dump the target network
+ Extra_MmFlex_t * pMmNames; // memory manager for signal names
+ // solving parameters
+ int mode; // 0 = resource-aware integration; 1 = brute-force SAT
+ Prove_Params_t Params; // integrated CEC parameters
+ // information about the target
+ int nog; // the numbers of gates in the target
+ Vec_Ptr_t * vNodes; // the gates in the target
+ Vec_Int_t * vValues; // the values of gate's outputs in the target
+ // solution
+ CSAT_Target_ResultT * pResult; // the result of solving the target
+};
+
+static CSAT_Target_ResultT * ABC_TargetResAlloc( int nVars );
+static char * ABC_GetNodeName( ABC_Manager mng, Abc_Obj_t * pNode );
+
+// procedures to start and stop the ABC framework
+extern void Abc_Start();
+extern void Abc_Stop();
+
+// some external procedures
+extern int Io_WriteBench( Abc_Ntk_t * pNtk, char * FileName );
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Creates a new manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+ABC_Manager ABC_InitManager()
+{
+ ABC_Manager_t * mng;
+ Abc_Start();
+ mng = ALLOC( ABC_Manager_t, 1 );
+ memset( mng, 0, sizeof(ABC_Manager_t) );
+ mng->pNtk = Abc_NtkAlloc( ABC_NTK_LOGIC, ABC_FUNC_SOP, 1 );
+ mng->pNtk->pName = Extra_UtilStrsav("csat_network");
+ mng->tName2Node = stmm_init_table(strcmp, stmm_strhash);
+ mng->tNode2Name = stmm_init_table(stmm_ptrcmp, stmm_ptrhash);
+ mng->pMmNames = Extra_MmFlexStart();
+ mng->vNodes = Vec_PtrAlloc( 100 );
+ mng->vValues = Vec_IntAlloc( 100 );
+ mng->mode = 0; // set "resource-aware integration" as the default mode
+ // set default parameters for CEC
+ Prove_ParamsSetDefault( &mng->Params );
+ // set infinite resource limit for the final mitering
+// mng->Params.nMiteringLimitLast = ABC_INFINITY;
+ return mng;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Deletes the manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_ReleaseManager( ABC_Manager mng )
+{
+ CSAT_Target_ResultT * p_res = ABC_Get_Target_Result( mng,0 );
+ ABC_TargetResFree(p_res);
+ if ( mng->tNode2Name ) stmm_free_table( mng->tNode2Name );
+ if ( mng->tName2Node ) stmm_free_table( mng->tName2Node );
+ if ( mng->pMmNames ) Extra_MmFlexStop( mng->pMmNames );
+ if ( mng->pNtk ) Abc_NtkDelete( mng->pNtk );
+ if ( mng->pTarget ) Abc_NtkDelete( mng->pTarget );
+ if ( mng->vNodes ) Vec_PtrFree( mng->vNodes );
+ if ( mng->vValues ) Vec_IntFree( mng->vValues );
+ FREE( mng->pDumpFileName );
+ free( mng );
+ Abc_Stop();
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sets solver options for learning.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_SetSolveOption( ABC_Manager mng, enum CSAT_OptionT option )
+{
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sets solving mode by brute-force SAT.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_UseOnlyCoreSatSolver( ABC_Manager mng )
+{
+ mng->mode = 1; // switch to "brute-force SAT" as the solving option
+}
+
+/**Function*************************************************************
+
+ Synopsis [Adds a gate to the circuit.]
+
+ Description [The meaning of the parameters are:
+ type: the type of the gate to be added
+ name: the name of the gate to be added, name should be unique in a circuit.
+ nofi: number of fanins of the gate to be added;
+ fanins: the name array of fanins of the gate to be added.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int ABC_AddGate( ABC_Manager mng, enum GateType type, char * name, int nofi, char ** fanins, int dc_attr )
+{
+ Abc_Obj_t * pObj, * pFanin;
+ char * pSop, * pNewName;
+ int i;
+
+ // save the name in the local memory manager
+ pNewName = Extra_MmFlexEntryFetch( mng->pMmNames, strlen(name) + 1 );
+ strcpy( pNewName, name );
+ name = pNewName;
+
+ // consider different cases, create the node, and map the node into the name
+ switch( type )
+ {
+ case CSAT_BPI:
+ case CSAT_BPPI:
+ if ( nofi != 0 )
+ { printf( "ABC_AddGate: The PI/PPI gate \"%s\" has fanins.\n", name ); return 0; }
+ // create the PI
+ pObj = Abc_NtkCreatePi( mng->pNtk );
+ stmm_insert( mng->tNode2Name, (char *)pObj, name );
+ break;
+ case CSAT_CONST:
+ case CSAT_BAND:
+ case CSAT_BNAND:
+ case CSAT_BOR:
+ case CSAT_BNOR:
+ case CSAT_BXOR:
+ case CSAT_BXNOR:
+ case CSAT_BINV:
+ case CSAT_BBUF:
+ // create the node
+ pObj = Abc_NtkCreateNode( mng->pNtk );
+ // create the fanins
+ for ( i = 0; i < nofi; i++ )
+ {
+ if ( !stmm_lookup( mng->tName2Node, fanins[i], (char **)&pFanin ) )
+ { printf( "ABC_AddGate: The fanin gate \"%s\" is not in the network.\n", fanins[i] ); return 0; }
+ Abc_ObjAddFanin( pObj, pFanin );
+ }
+ // create the node function
+ switch( type )
+ {
+ case CSAT_CONST:
+ if ( nofi != 0 )
+ { printf( "ABC_AddGate: The constant gate \"%s\" has fanins.\n", name ); return 0; }
+ pSop = Abc_SopCreateConst1( mng->pNtk->pManFunc );
+ break;
+ case CSAT_BAND:
+ if ( nofi < 1 )
+ { printf( "ABC_AddGate: The AND gate \"%s\" no fanins.\n", name ); return 0; }
+ pSop = Abc_SopCreateAnd( mng->pNtk->pManFunc, nofi, NULL );
+ break;
+ case CSAT_BNAND:
+ if ( nofi < 1 )
+ { printf( "ABC_AddGate: The NAND gate \"%s\" no fanins.\n", name ); return 0; }
+ pSop = Abc_SopCreateNand( mng->pNtk->pManFunc, nofi );
+ break;
+ case CSAT_BOR:
+ if ( nofi < 1 )
+ { printf( "ABC_AddGate: The OR gate \"%s\" no fanins.\n", name ); return 0; }
+ pSop = Abc_SopCreateOr( mng->pNtk->pManFunc, nofi, NULL );
+ break;
+ case CSAT_BNOR:
+ if ( nofi < 1 )
+ { printf( "ABC_AddGate: The NOR gate \"%s\" no fanins.\n", name ); return 0; }
+ pSop = Abc_SopCreateNor( mng->pNtk->pManFunc, nofi );
+ break;
+ case CSAT_BXOR:
+ if ( nofi < 1 )
+ { printf( "ABC_AddGate: The XOR gate \"%s\" no fanins.\n", name ); return 0; }
+ if ( nofi > 2 )
+ { printf( "ABC_AddGate: The XOR gate \"%s\" has more than two fanins.\n", name ); return 0; }
+ pSop = Abc_SopCreateXor( mng->pNtk->pManFunc, nofi );
+ break;
+ case CSAT_BXNOR:
+ if ( nofi < 1 )
+ { printf( "ABC_AddGate: The XNOR gate \"%s\" no fanins.\n", name ); return 0; }
+ if ( nofi > 2 )
+ { printf( "ABC_AddGate: The XNOR gate \"%s\" has more than two fanins.\n", name ); return 0; }
+ pSop = Abc_SopCreateNxor( mng->pNtk->pManFunc, nofi );
+ break;
+ case CSAT_BINV:
+ if ( nofi != 1 )
+ { printf( "ABC_AddGate: The inverter gate \"%s\" does not have exactly one fanin.\n", name ); return 0; }
+ pSop = Abc_SopCreateInv( mng->pNtk->pManFunc );
+ break;
+ case CSAT_BBUF:
+ if ( nofi != 1 )
+ { printf( "ABC_AddGate: The buffer gate \"%s\" does not have exactly one fanin.\n", name ); return 0; }
+ pSop = Abc_SopCreateBuf( mng->pNtk->pManFunc );
+ break;
+ default :
+ break;
+ }
+ Abc_ObjSetData( pObj, pSop );
+ break;
+ case CSAT_BPPO:
+ case CSAT_BPO:
+ if ( nofi != 1 )
+ { printf( "ABC_AddGate: The PO/PPO gate \"%s\" does not have exactly one fanin.\n", name ); return 0; }
+ // create the PO
+ pObj = Abc_NtkCreatePo( mng->pNtk );
+ stmm_insert( mng->tNode2Name, (char *)pObj, name );
+ // connect to the PO fanin
+ if ( !stmm_lookup( mng->tName2Node, fanins[0], (char **)&pFanin ) )
+ { printf( "ABC_AddGate: The fanin gate \"%s\" is not in the network.\n", fanins[0] ); return 0; }
+ Abc_ObjAddFanin( pObj, pFanin );
+ break;
+ default:
+ printf( "ABC_AddGate: Unknown gate type.\n" );
+ break;
+ }
+
+ // map the name into the node
+ if ( stmm_insert( mng->tName2Node, name, (char *)pObj ) )
+ { printf( "ABC_AddGate: The same gate \"%s\" is added twice.\n", name ); return 0; }
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [This procedure also finalizes construction of the ABC network.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_Network_Finalize( ABC_Manager mng )
+{
+ Abc_Ntk_t * pNtk = mng->pNtk;
+ Abc_Obj_t * pObj;
+ int i;
+ Abc_NtkForEachPi( pNtk, pObj, i )
+ Abc_ObjAssignName( pObj, ABC_GetNodeName(mng, pObj), NULL );
+ Abc_NtkForEachPo( pNtk, pObj, i )
+ Abc_ObjAssignName( pObj, ABC_GetNodeName(mng, pObj), NULL );
+ assert( Abc_NtkLatchNum(pNtk) == 0 );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Checks integraty of the manager.]
+
+ Description [Checks if there are gates that are not used by any primary output.
+ If no such gates exist, return 1 else return 0.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int ABC_Check_Integrity( ABC_Manager mng )
+{
+ Abc_Ntk_t * pNtk = mng->pNtk;
+ Abc_Obj_t * pObj;
+ int i;
+
+ // check that there are no dangling nodes
+ Abc_NtkForEachNode( pNtk, pObj, i )
+ {
+ if ( i == 0 )
+ continue;
+ if ( Abc_ObjFanoutNum(pObj) == 0 )
+ {
+// printf( "ABC_Check_Integrity: The network has dangling nodes.\n" );
+ return 0;
+ }
+ }
+
+ // make sure everything is okay with the network structure
+ if ( !Abc_NtkDoCheck( pNtk ) )
+ {
+ printf( "ABC_Check_Integrity: The internal network check has failed.\n" );
+ return 0;
+ }
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sets time limit for solving a target.]
+
+ Description [Runtime: time limit (in second).]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_SetTimeLimit( ABC_Manager mng, int runtime )
+{
+// printf( "ABC_SetTimeLimit: The resource limit is not implemented (warning).\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_SetLearnLimit( ABC_Manager mng, int num )
+{
+// printf( "ABC_SetLearnLimit: The resource limit is not implemented (warning).\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_SetLearnBacktrackLimit( ABC_Manager mng, int num )
+{
+// printf( "ABC_SetLearnBacktrackLimit: The resource limit is not implemented (warning).\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_SetSolveBacktrackLimit( ABC_Manager mng, int num )
+{
+ mng->Params.nMiteringLimitLast = num;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_SetSolveImplicationLimit( ABC_Manager mng, int num )
+{
+// printf( "ABC_SetSolveImplicationLimit: The resource limit is not implemented (warning).\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_SetTotalBacktrackLimit( ABC_Manager mng, uint64 num )
+{
+ mng->Params.nTotalBacktrackLimit = num;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_SetTotalInspectLimit( ABC_Manager mng, uint64 num )
+{
+ mng->Params.nTotalInspectLimit = num;
+}
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+uint64 ABC_GetTotalBacktracksMade( ABC_Manager mng )
+{
+ return mng->Params.nTotalBacktracksMade;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+uint64 ABC_GetTotalInspectsMade( ABC_Manager mng )
+{
+ return mng->Params.nTotalInspectsMade;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sets the file name to dump the structurally hashed network used for solving.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_EnableDump( ABC_Manager mng, char * dump_file )
+{
+ FREE( mng->pDumpFileName );
+ mng->pDumpFileName = Extra_UtilStrsav( dump_file );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Adds a new target to the manager.]
+
+ Description [The meaning of the parameters are:
+ nog: number of gates that are in the targets,
+ names: name array of gates,
+ values: value array of the corresponding gates given in "names" to be solved.
+ The relation of them is AND.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int ABC_AddTarget( ABC_Manager mng, int nog, char ** names, int * values )
+{
+ Abc_Obj_t * pObj;
+ int i;
+ if ( nog < 1 )
+ { printf( "ABC_AddTarget: The target has no gates.\n" ); return 0; }
+ // clear storage for the target
+ mng->nog = 0;
+ Vec_PtrClear( mng->vNodes );
+ Vec_IntClear( mng->vValues );
+ // save the target
+ for ( i = 0; i < nog; i++ )
+ {
+ if ( !stmm_lookup( mng->tName2Node, names[i], (char **)&pObj ) )
+ { printf( "ABC_AddTarget: The target gate \"%s\" is not in the network.\n", names[i] ); return 0; }
+ Vec_PtrPush( mng->vNodes, pObj );
+ if ( values[i] < 0 || values[i] > 1 )
+ { printf( "ABC_AddTarget: The value of gate \"%s\" is not 0 or 1.\n", names[i] ); return 0; }
+ Vec_IntPush( mng->vValues, values[i] );
+ }
+ mng->nog = nog;
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Initialize the solver internal data structure.]
+
+ Description [Prepares the solver to work on one specific target
+ set by calling ABC_AddTarget before.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_SolveInit( ABC_Manager mng )
+{
+ // check if the target is available
+ assert( mng->nog == Vec_PtrSize(mng->vNodes) );
+ if ( mng->nog == 0 )
+ { printf( "ABC_SolveInit: Target is not specified by ABC_AddTarget().\n" ); return; }
+
+ // free the previous target network if present
+ if ( mng->pTarget ) Abc_NtkDelete( mng->pTarget );
+
+ // set the new target network
+// mng->pTarget = Abc_NtkCreateTarget( mng->pNtk, mng->vNodes, mng->vValues );
+ mng->pTarget = Abc_NtkStrash( mng->pNtk, 0, 1, 0 );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Currently not implemented.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_AnalyzeTargets( ABC_Manager mng )
+{
+}
+
+/**Function*************************************************************
+
+ Synopsis [Solves the targets added by ABC_AddTarget().]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+enum CSAT_StatusT ABC_Solve( ABC_Manager mng )
+{
+ Prove_Params_t * pParams = &mng->Params;
+ int RetValue, i;
+
+ // check if the target network is available
+ if ( mng->pTarget == NULL )
+ { printf( "ABC_Solve: Target network is not derived by ABC_SolveInit().\n" ); return UNDETERMINED; }
+
+ // try to prove the miter using a number of techniques
+ if ( mng->mode )
+ RetValue = Abc_NtkMiterSat( mng->pTarget, (sint64)pParams->nMiteringLimitLast, (sint64)0, 0, NULL, NULL );
+ else
+// RetValue = Abc_NtkMiterProve( &mng->pTarget, pParams ); // old CEC engine
+ RetValue = Abc_NtkIvyProve( &mng->pTarget, pParams ); // new CEC engine
+
+ // analyze the result
+ mng->pResult = ABC_TargetResAlloc( Abc_NtkCiNum(mng->pTarget) );
+ if ( RetValue == -1 )
+ mng->pResult->status = UNDETERMINED;
+ else if ( RetValue == 1 )
+ mng->pResult->status = UNSATISFIABLE;
+ else if ( RetValue == 0 )
+ {
+ mng->pResult->status = SATISFIABLE;
+ // create the array of PI names and values
+ for ( i = 0; i < mng->pResult->no_sig; i++ )
+ {
+ mng->pResult->names[i] = Extra_UtilStrsav( ABC_GetNodeName(mng, Abc_NtkCi(mng->pNtk, i)) );
+ mng->pResult->values[i] = mng->pTarget->pModel[i];
+ }
+ FREE( mng->pTarget->pModel );
+ }
+ else assert( 0 );
+
+ // delete the target
+ Abc_NtkDelete( mng->pTarget );
+ mng->pTarget = NULL;
+ // return the status
+ return mng->pResult->status;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Gets the solve status of a target.]
+
+ Description [TargetID: the target id returned by ABC_AddTarget().]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+CSAT_Target_ResultT * ABC_Get_Target_Result( ABC_Manager mng, int TargetID )
+{
+ return mng->pResult;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Dumps the original network into the BENCH file.]
+
+ Description [This procedure should be modified to dump the target.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_Dump_Bench_File( ABC_Manager mng )
+{
+ Abc_Ntk_t * pNtkTemp, * pNtkAig;
+ char * pFileName;
+
+ // derive the netlist
+ pNtkAig = Abc_NtkStrash( mng->pNtk, 0, 0, 0 );
+ pNtkTemp = Abc_NtkToNetlistBench( pNtkAig );
+ Abc_NtkDelete( pNtkAig );
+ if ( pNtkTemp == NULL )
+ { printf( "ABC_Dump_Bench_File: Dumping BENCH has failed.\n" ); return; }
+ pFileName = mng->pDumpFileName? mng->pDumpFileName: "abc_test.bench";
+ Io_WriteBench( pNtkTemp, pFileName );
+ Abc_NtkDelete( pNtkTemp );
+}
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Allocates the target result.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+CSAT_Target_ResultT * ABC_TargetResAlloc( int nVars )
+{
+ CSAT_Target_ResultT * p;
+ p = ALLOC( CSAT_Target_ResultT, 1 );
+ memset( p, 0, sizeof(CSAT_Target_ResultT) );
+ p->no_sig = nVars;
+ p->names = ALLOC( char *, nVars );
+ p->values = ALLOC( int, nVars );
+ memset( p->names, 0, sizeof(char *) * nVars );
+ memset( p->values, 0, sizeof(int) * nVars );
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Deallocates the target result.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void ABC_TargetResFree( CSAT_Target_ResultT * p )
+{
+ if ( p == NULL )
+ return;
+ if( p->names )
+ {
+ int i = 0;
+ for ( i = 0; i < p->no_sig; i++ )
+ {
+ FREE(p->names[i]);
+ }
+ }
+ FREE( p->names );
+ FREE( p->values );
+ free( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Dumps the target AIG into the BENCH file.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+char * ABC_GetNodeName( ABC_Manager mng, Abc_Obj_t * pNode )
+{
+ char * pName = NULL;
+ if ( !stmm_lookup( mng->tNode2Name, (char *)pNode, (char **)&pName ) )
+ {
+ assert( 0 );
+ }
+ return pName;
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/csat/csat_apis.h b/src/sat/csat/csat_apis.h
new file mode 100644
index 00000000..b80eddbf
--- /dev/null
+++ b/src/sat/csat/csat_apis.h
@@ -0,0 +1,222 @@
+/**CFile****************************************************************
+
+ FileName [csat_apis.h]
+
+ PackageName [Interface to CSAT.]
+
+ Synopsis [APIs, enums, and data structures expected from the use of CSAT.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - August 28, 2005]
+
+ Revision [$Id: csat_apis.h,v 1.5 2005/12/30 10:54:40 rmukherj Exp $]
+
+***********************************************************************/
+
+#ifndef __ABC_APIS_H__
+#define __ABC_APIS_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// INCLUDES ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// PARAMETERS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// STRUCTURE DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+
+typedef struct ABC_ManagerStruct_t ABC_Manager_t;
+typedef struct ABC_ManagerStruct_t * ABC_Manager;
+
+
+// GateType defines the gate type that can be added to circuit by
+// ABC_AddGate();
+#ifndef _ABC_GATE_TYPE_
+#define _ABC_GATE_TYPE_
+enum GateType
+{
+ CSAT_CONST = 0, // constant gate
+ CSAT_BPI, // boolean PI
+ CSAT_BPPI, // bit level PSEUDO PRIMARY INPUT
+ CSAT_BAND, // bit level AND
+ CSAT_BNAND, // bit level NAND
+ CSAT_BOR, // bit level OR
+ CSAT_BNOR, // bit level NOR
+ CSAT_BXOR, // bit level XOR
+ CSAT_BXNOR, // bit level XNOR
+ CSAT_BINV, // bit level INVERTER
+ CSAT_BBUF, // bit level BUFFER
+ CSAT_BMUX, // bit level MUX --not supported
+ CSAT_BDFF, // bit level D-type FF
+ CSAT_BSDFF, // bit level scan FF --not supported
+ CSAT_BTRIH, // bit level TRISTATE gate with active high control --not supported
+ CSAT_BTRIL, // bit level TRISTATE gate with active low control --not supported
+ CSAT_BBUS, // bit level BUS --not supported
+ CSAT_BPPO, // bit level PSEUDO PRIMARY OUTPUT
+ CSAT_BPO, // boolean PO
+ CSAT_BCNF, // boolean constraint
+ CSAT_BDC, // boolean don't care gate (2 input)
+};
+#endif
+
+
+//CSAT_StatusT defines the return value by ABC_Solve();
+#ifndef _ABC_STATUS_
+#define _ABC_STATUS_
+enum CSAT_StatusT
+{
+ UNDETERMINED = 0,
+ UNSATISFIABLE,
+ SATISFIABLE,
+ TIME_OUT,
+ FRAME_OUT,
+ NO_TARGET,
+ ABORTED,
+ SEQ_SATISFIABLE
+};
+#endif
+
+
+// to identify who called the CSAT solver
+#ifndef _ABC_CALLER_
+#define _ABC_CALLER_
+enum CSAT_CallerT
+{
+ BLS = 0,
+ SATORI,
+ NONE
+};
+#endif
+
+
+// CSAT_OptionT defines the solver option about learning
+// which is used by ABC_SetSolveOption();
+#ifndef _ABC_OPTION_
+#define _ABC_OPTION_
+enum CSAT_OptionT
+{
+ BASE_LINE = 0,
+ IMPLICT_LEARNING, //default
+ EXPLICT_LEARNING
+};
+#endif
+
+
+#ifndef _ABC_Target_Result
+#define _ABC_Target_Result
+typedef struct _CSAT_Target_ResultT CSAT_Target_ResultT;
+struct _CSAT_Target_ResultT
+{
+ enum CSAT_StatusT status; // solve status of the target
+ int num_dec; // num of decisions to solve the target
+ int num_imp; // num of implications to solve the target
+ int num_cftg; // num of conflict gates learned
+ int num_cfts; // num of conflict signals in conflict gates
+ double time; // time(in second) used to solve the target
+ int no_sig; // if "status" is SATISFIABLE, "no_sig" is the number of
+ // primary inputs, if the "status" is TIME_OUT, "no_sig" is the
+ // number of constant signals found.
+ char** names; // if the "status" is SATISFIABLE, "names" is the name array of
+ // primary inputs, "values" is the value array of primary
+ // inputs that satisfy the target.
+ // if the "status" is TIME_OUT, "names" is the name array of
+ // constant signals found (signals at the root of decision
+ // tree), "values" is the value array of constant signals found.
+ int* values;
+};
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+// create a new manager
+extern ABC_Manager ABC_InitManager(void);
+
+// release a manager
+extern void ABC_ReleaseManager(ABC_Manager mng);
+
+// set solver options for learning
+extern void ABC_SetSolveOption(ABC_Manager mng, enum CSAT_OptionT option);
+
+// enable checking by brute-force SAT solver (MiniSat-1.14)
+extern void ABC_UseOnlyCoreSatSolver(ABC_Manager mng);
+
+
+// add a gate to the circuit
+// the meaning of the parameters are:
+// type: the type of the gate to be added
+// name: the name of the gate to be added, name should be unique in a circuit.
+// nofi: number of fanins of the gate to be added;
+// fanins: the name array of fanins of the gate to be added
+extern int ABC_AddGate(ABC_Manager mng,
+ enum GateType type,
+ char* name,
+ int nofi,
+ char** fanins,
+ int dc_attr);
+
+// check if there are gates that are not used by any primary ouput.
+// if no such gates exist, return 1 else return 0;
+extern int ABC_Check_Integrity(ABC_Manager mng);
+
+// THIS PROCEDURE SHOULD BE CALLED AFTER THE NETWORK IS CONSTRUCTED!!!
+extern void ABC_Network_Finalize( ABC_Manager mng );
+
+// set time limit for solving a target.
+// runtime: time limit (in second).
+extern void ABC_SetTimeLimit(ABC_Manager mng, int runtime);
+extern void ABC_SetLearnLimit(ABC_Manager mng, int num);
+extern void ABC_SetSolveBacktrackLimit(ABC_Manager mng, int num);
+extern void ABC_SetLearnBacktrackLimit(ABC_Manager mng, int num);
+extern void ABC_EnableDump(ABC_Manager mng, char* dump_file);
+
+extern void ABC_SetTotalBacktrackLimit( ABC_Manager mng, uint64 num );
+extern void ABC_SetTotalInspectLimit( ABC_Manager mng, uint64 num );
+extern uint64 ABC_GetTotalBacktracksMade( ABC_Manager mng );
+extern uint64 ABC_GetTotalInspectsMade( ABC_Manager mng );
+
+// the meaning of the parameters are:
+// nog: number of gates that are in the targets
+// names: name array of gates
+// values: value array of the corresponding gates given in "names" to be
+// solved. the relation of them is AND.
+extern int ABC_AddTarget(ABC_Manager mng, int nog, char**names, int* values);
+
+// initialize the solver internal data structure.
+extern void ABC_SolveInit(ABC_Manager mng);
+extern void ABC_AnalyzeTargets(ABC_Manager mng);
+
+// solve the targets added by ABC_AddTarget()
+extern enum CSAT_StatusT ABC_Solve(ABC_Manager mng);
+
+// get the solve status of a target
+// TargetID: the target id returned by ABC_AddTarget().
+extern CSAT_Target_ResultT * ABC_Get_Target_Result(ABC_Manager mng, int TargetID);
+extern void ABC_Dump_Bench_File(ABC_Manager mng);
+
+// ADDED PROCEDURES:
+extern void ABC_TargetResFree( CSAT_Target_ResultT * p );
+
+extern void CSAT_SetCaller(ABC_Manager mng, enum CSAT_CallerT caller);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
diff --git a/src/sat/csat/module.make b/src/sat/csat/module.make
new file mode 100644
index 00000000..5b71a03c
--- /dev/null
+++ b/src/sat/csat/module.make
@@ -0,0 +1 @@
+SRC += src/sat/csat/csat_apis.c
diff --git a/src/sat/fraig/fraig.h b/src/sat/fraig/fraig.h
new file mode 100644
index 00000000..1dad21e2
--- /dev/null
+++ b/src/sat/fraig/fraig.h
@@ -0,0 +1,267 @@
+/**CFile****************************************************************
+
+ FileName [fraig.h]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [External declarations of the FRAIG package.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraig.h,v 1.18 2005/07/08 01:01:30 alanmi Exp $]
+
+***********************************************************************/
+
+#ifndef __FRAIG_H__
+#define __FRAIG_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// INCLUDES ///
+////////////////////////////////////////////////////////////////////////
+
+#ifndef SINT64
+#define SINT64
+
+#ifdef _WIN32
+typedef signed __int64 sint64; // compatible with MS VS 6.0
+#else
+typedef long long sint64;
+#endif
+
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// PARAMETERS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// STRUCTURE DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+typedef struct Fraig_ManStruct_t_ Fraig_Man_t;
+typedef struct Fraig_NodeStruct_t_ Fraig_Node_t;
+typedef struct Fraig_NodeVecStruct_t_ Fraig_NodeVec_t;
+typedef struct Fraig_HashTableStruct_t_ Fraig_HashTable_t;
+typedef struct Fraig_ParamsStruct_t_ Fraig_Params_t;
+typedef struct Fraig_PatternsStruct_t_ Fraig_Patterns_t;
+typedef struct Prove_ParamsStruct_t_ Prove_Params_t;
+
+struct Fraig_ParamsStruct_t_
+{
+ int nPatsRand; // the number of words of random simulation info
+ int nPatsDyna; // the number of words of dynamic simulation info
+ int nBTLimit; // the max number of backtracks to perform
+ int nSeconds; // the timeout for the final proof
+ int fFuncRed; // performs only one level hashing
+ int fFeedBack; // enables solver feedback
+ int fDist1Pats; // enables distance-1 patterns
+ int fDoSparse; // performs equiv tests for sparse functions
+ int fChoicing; // enables recording structural choices
+ int fTryProve; // tries to solve the final miter
+ int fVerbose; // the verbosiness flag
+ int fVerboseP; // the verbosiness flag (for proof reporting)
+ int fInternal; // is set to 1 for internal fraig calls
+ int nConfLimit; // the limit on the number of conflicts
+ sint64 nInspLimit; // the limit on the number of inspections
+};
+
+struct Prove_ParamsStruct_t_
+{
+ // general parameters
+ int fUseFraiging; // enables fraiging
+ int fUseRewriting; // enables rewriting
+ int fUseBdds; // enables BDD construction when other methods fail
+ int fVerbose; // prints verbose stats
+ // iterations
+ int nItersMax; // the number of iterations
+ // mitering
+ int nMiteringLimitStart; // starting mitering limit
+ float nMiteringLimitMulti; // multiplicative coefficient to increase the limit in each iteration
+ // rewriting
+ int nRewritingLimitStart; // the number of rewriting iterations
+ float nRewritingLimitMulti; // multiplicative coefficient to increase the limit in each iteration
+ // fraiging
+ int nFraigingLimitStart; // starting backtrack(conflict) limit
+ float nFraigingLimitMulti; // multiplicative coefficient to increase the limit in each iteration
+ // last-gasp BDD construction
+ int nBddSizeLimit; // the number of BDD nodes when construction is aborted
+ int fBddReorder; // enables dynamic BDD variable reordering
+ // last-gasp mitering
+ int nMiteringLimitLast; // final mitering limit
+ // global SAT solver limits
+ sint64 nTotalBacktrackLimit; // global limit on the number of backtracks
+ sint64 nTotalInspectLimit; // global limit on the number of clause inspects
+ // global resources applied
+ sint64 nTotalBacktracksMade; // the total number of backtracks made
+ sint64 nTotalInspectsMade; // the total number of inspects made
+};
+
+////////////////////////////////////////////////////////////////////////
+/// GLOBAL VARIABLES ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// MACRO DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+// macros working with complemented attributes of the nodes
+#define Fraig_IsComplement(p) (((int)((unsigned long) (p) & 01)))
+#define Fraig_Regular(p) ((Fraig_Node_t *)((unsigned long)(p) & ~01))
+#define Fraig_Not(p) ((Fraig_Node_t *)((unsigned long)(p) ^ 01))
+#define Fraig_NotCond(p,c) ((Fraig_Node_t *)((unsigned long)(p) ^ (c)))
+
+// these are currently not used
+#define Fraig_Ref(p)
+#define Fraig_Deref(p)
+#define Fraig_RecursiveDeref(p,c)
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/*=== fraigApi.c =============================================================*/
+extern Fraig_NodeVec_t * Fraig_ManReadVecInputs( Fraig_Man_t * p );
+extern Fraig_NodeVec_t * Fraig_ManReadVecOutputs( Fraig_Man_t * p );
+extern Fraig_NodeVec_t * Fraig_ManReadVecNodes( Fraig_Man_t * p );
+extern Fraig_Node_t ** Fraig_ManReadInputs ( Fraig_Man_t * p );
+extern Fraig_Node_t ** Fraig_ManReadOutputs( Fraig_Man_t * p );
+extern Fraig_Node_t ** Fraig_ManReadNodes( Fraig_Man_t * p );
+extern int Fraig_ManReadInputNum ( Fraig_Man_t * p );
+extern int Fraig_ManReadOutputNum( Fraig_Man_t * p );
+extern int Fraig_ManReadNodeNum( Fraig_Man_t * p );
+extern Fraig_Node_t * Fraig_ManReadConst1 ( Fraig_Man_t * p );
+extern Fraig_Node_t * Fraig_ManReadIthVar( Fraig_Man_t * p, int i );
+extern Fraig_Node_t * Fraig_ManReadIthNode( Fraig_Man_t * p, int i );
+extern char ** Fraig_ManReadInputNames( Fraig_Man_t * p );
+extern char ** Fraig_ManReadOutputNames( Fraig_Man_t * p );
+extern char * Fraig_ManReadVarsInt( Fraig_Man_t * p );
+extern char * Fraig_ManReadSat( Fraig_Man_t * p );
+extern int Fraig_ManReadFuncRed( Fraig_Man_t * p );
+extern int Fraig_ManReadFeedBack( Fraig_Man_t * p );
+extern int Fraig_ManReadDoSparse( Fraig_Man_t * p );
+extern int Fraig_ManReadChoicing( Fraig_Man_t * p );
+extern int Fraig_ManReadVerbose( Fraig_Man_t * p );
+extern int * Fraig_ManReadModel( Fraig_Man_t * p );
+extern int Fraig_ManReadPatternNumRandom( Fraig_Man_t * p );
+extern int Fraig_ManReadPatternNumDynamic( Fraig_Man_t * p );
+extern int Fraig_ManReadPatternNumDynamicFiltered( Fraig_Man_t * p );
+extern int Fraig_ManReadSatFails( Fraig_Man_t * p );
+extern int Fraig_ManReadConflicts( Fraig_Man_t * p );
+extern int Fraig_ManReadInspects( Fraig_Man_t * p );
+
+extern void Fraig_ManSetFuncRed( Fraig_Man_t * p, int fFuncRed );
+extern void Fraig_ManSetFeedBack( Fraig_Man_t * p, int fFeedBack );
+extern void Fraig_ManSetDoSparse( Fraig_Man_t * p, int fDoSparse );
+extern void Fraig_ManSetChoicing( Fraig_Man_t * p, int fChoicing );
+extern void Fraig_ManSetTryProve( Fraig_Man_t * p, int fTryProve );
+extern void Fraig_ManSetVerbose( Fraig_Man_t * p, int fVerbose );
+extern void Fraig_ManSetTimeToGraph( Fraig_Man_t * p, int Time );
+extern void Fraig_ManSetTimeToNet( Fraig_Man_t * p, int Time );
+extern void Fraig_ManSetTimeTotal( Fraig_Man_t * p, int Time );
+extern void Fraig_ManSetOutputNames( Fraig_Man_t * p, char ** ppNames );
+extern void Fraig_ManSetInputNames( Fraig_Man_t * p, char ** ppNames );
+extern void Fraig_ManSetPo( Fraig_Man_t * p, Fraig_Node_t * pNode );
+
+extern Fraig_Node_t * Fraig_NodeReadData0( Fraig_Node_t * p );
+extern Fraig_Node_t * Fraig_NodeReadData1( Fraig_Node_t * p );
+extern int Fraig_NodeReadNum( Fraig_Node_t * p );
+extern Fraig_Node_t * Fraig_NodeReadOne( Fraig_Node_t * p );
+extern Fraig_Node_t * Fraig_NodeReadTwo( Fraig_Node_t * p );
+extern Fraig_Node_t * Fraig_NodeReadNextE( Fraig_Node_t * p );
+extern Fraig_Node_t * Fraig_NodeReadRepr( Fraig_Node_t * p );
+extern int Fraig_NodeReadNumRefs( Fraig_Node_t * p );
+extern int Fraig_NodeReadNumFanouts( Fraig_Node_t * p );
+extern int Fraig_NodeReadSimInv( Fraig_Node_t * p );
+extern int Fraig_NodeReadNumOnes( Fraig_Node_t * p );
+extern unsigned * Fraig_NodeReadPatternsRandom( Fraig_Node_t * p );
+extern unsigned * Fraig_NodeReadPatternsDynamic( Fraig_Node_t * p );
+
+extern void Fraig_NodeSetData0( Fraig_Node_t * p, Fraig_Node_t * pData );
+extern void Fraig_NodeSetData1( Fraig_Node_t * p, Fraig_Node_t * pData );
+
+extern int Fraig_NodeIsConst( Fraig_Node_t * p );
+extern int Fraig_NodeIsVar( Fraig_Node_t * p );
+extern int Fraig_NodeIsAnd( Fraig_Node_t * p );
+extern int Fraig_NodeComparePhase( Fraig_Node_t * p1, Fraig_Node_t * p2 );
+
+extern Fraig_Node_t * Fraig_NodeOr( Fraig_Man_t * p, Fraig_Node_t * p1, Fraig_Node_t * p2 );
+extern Fraig_Node_t * Fraig_NodeAnd( Fraig_Man_t * p, Fraig_Node_t * p1, Fraig_Node_t * p2 );
+extern Fraig_Node_t * Fraig_NodeOr( Fraig_Man_t * p, Fraig_Node_t * p1, Fraig_Node_t * p2 );
+extern Fraig_Node_t * Fraig_NodeExor( Fraig_Man_t * p, Fraig_Node_t * p1, Fraig_Node_t * p2 );
+extern Fraig_Node_t * Fraig_NodeMux( Fraig_Man_t * p, Fraig_Node_t * pNode, Fraig_Node_t * pNodeT, Fraig_Node_t * pNodeE );
+extern void Fraig_NodeSetChoice( Fraig_Man_t * pMan, Fraig_Node_t * pNodeOld, Fraig_Node_t * pNodeNew );
+
+/*=== fraigMan.c =============================================================*/
+extern void Prove_ParamsSetDefault( Prove_Params_t * pParams );
+extern void Fraig_ParamsSetDefault( Fraig_Params_t * pParams );
+extern void Fraig_ParamsSetDefaultFull( Fraig_Params_t * pParams );
+extern Fraig_Man_t * Fraig_ManCreate( Fraig_Params_t * pParams );
+extern void Fraig_ManFree( Fraig_Man_t * pMan );
+extern void Fraig_ManPrintStats( Fraig_Man_t * p );
+extern Fraig_NodeVec_t * Fraig_ManGetSimInfo( Fraig_Man_t * p );
+extern int Fraig_ManCheckClauseUsingSimInfo( Fraig_Man_t * p, Fraig_Node_t * pNode1, Fraig_Node_t * pNode2 );
+extern void Fraig_ManAddClause( Fraig_Man_t * p, Fraig_Node_t ** ppNodes, int nNodes );
+
+/*=== fraigDfs.c =============================================================*/
+extern Fraig_NodeVec_t * Fraig_Dfs( Fraig_Man_t * pMan, int fEquiv );
+extern Fraig_NodeVec_t * Fraig_DfsOne( Fraig_Man_t * pMan, Fraig_Node_t * pNode, int fEquiv );
+extern Fraig_NodeVec_t * Fraig_DfsNodes( Fraig_Man_t * pMan, Fraig_Node_t ** ppNodes, int nNodes, int fEquiv );
+extern Fraig_NodeVec_t * Fraig_DfsReverse( Fraig_Man_t * pMan );
+extern int Fraig_CountNodes( Fraig_Man_t * pMan, int fEquiv );
+extern int Fraig_CheckTfi( Fraig_Man_t * pMan, Fraig_Node_t * pOld, Fraig_Node_t * pNew );
+extern int Fraig_CountLevels( Fraig_Man_t * pMan );
+
+/*=== fraigSat.c =============================================================*/
+extern int Fraig_NodesAreEqual( Fraig_Man_t * p, Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int nBTLimit, int nTimeLimit );
+extern int Fraig_NodeIsEquivalent( Fraig_Man_t * p, Fraig_Node_t * pOld, Fraig_Node_t * pNew, int nBTLimit, int nTimeLimit );
+extern void Fraig_ManProveMiter( Fraig_Man_t * p );
+extern int Fraig_ManCheckMiter( Fraig_Man_t * p );
+extern int Fraig_ManCheckClauseUsingSat( Fraig_Man_t * p, Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int nBTLimit );
+
+/*=== fraigVec.c ===============================================================*/
+extern Fraig_NodeVec_t * Fraig_NodeVecAlloc( int nCap );
+extern void Fraig_NodeVecFree( Fraig_NodeVec_t * p );
+extern Fraig_NodeVec_t * Fraig_NodeVecDup( Fraig_NodeVec_t * p );
+extern Fraig_Node_t ** Fraig_NodeVecReadArray( Fraig_NodeVec_t * p );
+extern int Fraig_NodeVecReadSize( Fraig_NodeVec_t * p );
+extern void Fraig_NodeVecGrow( Fraig_NodeVec_t * p, int nCapMin );
+extern void Fraig_NodeVecShrink( Fraig_NodeVec_t * p, int nSizeNew );
+extern void Fraig_NodeVecClear( Fraig_NodeVec_t * p );
+extern void Fraig_NodeVecPush( Fraig_NodeVec_t * p, Fraig_Node_t * Entry );
+extern int Fraig_NodeVecPushUnique( Fraig_NodeVec_t * p, Fraig_Node_t * Entry );
+extern void Fraig_NodeVecPushOrder( Fraig_NodeVec_t * p, Fraig_Node_t * pNode );
+extern int Fraig_NodeVecPushUniqueOrder( Fraig_NodeVec_t * p, Fraig_Node_t * pNode );
+extern void Fraig_NodeVecPushOrderByLevel( Fraig_NodeVec_t * p, Fraig_Node_t * pNode );
+extern int Fraig_NodeVecPushUniqueOrderByLevel( Fraig_NodeVec_t * p, Fraig_Node_t * pNode );
+extern Fraig_Node_t * Fraig_NodeVecPop( Fraig_NodeVec_t * p );
+extern void Fraig_NodeVecRemove( Fraig_NodeVec_t * p, Fraig_Node_t * Entry );
+extern void Fraig_NodeVecWriteEntry( Fraig_NodeVec_t * p, int i, Fraig_Node_t * Entry );
+extern Fraig_Node_t * Fraig_NodeVecReadEntry( Fraig_NodeVec_t * p, int i );
+extern void Fraig_NodeVecSortByLevel( Fraig_NodeVec_t * p, int fIncreasing );
+extern void Fraig_NodeVecSortByNumber( Fraig_NodeVec_t * p );
+
+/*=== fraigUtil.c ===============================================================*/
+extern void Fraig_ManMarkRealFanouts( Fraig_Man_t * p );
+extern int Fraig_ManCheckConsistency( Fraig_Man_t * p );
+extern int Fraig_GetMaxLevel( Fraig_Man_t * pMan );
+extern void Fraig_ManReportChoices( Fraig_Man_t * pMan );
+extern void Fraig_MappingSetChoiceLevels( Fraig_Man_t * pMan, int fMaximum );
+extern Fraig_NodeVec_t * Fraig_CollectSupergate( Fraig_Node_t * pNode, int fStopAtMux );
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/src/sat/fraig/fraigApi.c b/src/sat/fraig/fraigApi.c
new file mode 100644
index 00000000..79a7c224
--- /dev/null
+++ b/src/sat/fraig/fraigApi.c
@@ -0,0 +1,297 @@
+/**CFile****************************************************************
+
+ FileName [fraigApi.c]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [Access APIs for the FRAIG manager and node.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigApi.c,v 1.2 2005/07/08 01:01:30 alanmi Exp $]
+
+***********************************************************************/
+
+#include "fraigInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Access functions to read the data members of the manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_NodeVec_t * Fraig_ManReadVecInputs( Fraig_Man_t * p ) { return p->vInputs; }
+Fraig_NodeVec_t * Fraig_ManReadVecOutputs( Fraig_Man_t * p ) { return p->vOutputs; }
+Fraig_NodeVec_t * Fraig_ManReadVecNodes( Fraig_Man_t * p ) { return p->vNodes; }
+Fraig_Node_t ** Fraig_ManReadInputs ( Fraig_Man_t * p ) { return p->vInputs->pArray; }
+Fraig_Node_t ** Fraig_ManReadOutputs( Fraig_Man_t * p ) { return p->vOutputs->pArray; }
+Fraig_Node_t ** Fraig_ManReadNodes( Fraig_Man_t * p ) { return p->vNodes->pArray; }
+int Fraig_ManReadInputNum ( Fraig_Man_t * p ) { return p->vInputs->nSize; }
+int Fraig_ManReadOutputNum( Fraig_Man_t * p ) { return p->vOutputs->nSize; }
+int Fraig_ManReadNodeNum( Fraig_Man_t * p ) { return p->vNodes->nSize; }
+Fraig_Node_t * Fraig_ManReadConst1 ( Fraig_Man_t * p ) { return p->pConst1; }
+Fraig_Node_t * Fraig_ManReadIthNode( Fraig_Man_t * p, int i ) { assert ( i < p->vNodes->nSize ); return p->vNodes->pArray[i]; }
+char ** Fraig_ManReadInputNames( Fraig_Man_t * p ) { return p->ppInputNames; }
+char ** Fraig_ManReadOutputNames( Fraig_Man_t * p ) { return p->ppOutputNames; }
+char * Fraig_ManReadVarsInt( Fraig_Man_t * p ) { return (char *)p->vVarsInt; }
+char * Fraig_ManReadSat( Fraig_Man_t * p ) { return (char *)p->pSat; }
+int Fraig_ManReadFuncRed( Fraig_Man_t * p ) { return p->fFuncRed; }
+int Fraig_ManReadFeedBack( Fraig_Man_t * p ) { return p->fFeedBack; }
+int Fraig_ManReadDoSparse( Fraig_Man_t * p ) { return p->fDoSparse; }
+int Fraig_ManReadChoicing( Fraig_Man_t * p ) { return p->fChoicing; }
+int Fraig_ManReadVerbose( Fraig_Man_t * p ) { return p->fVerbose; }
+int * Fraig_ManReadModel( Fraig_Man_t * p ) { return p->pModel; }
+// returns the number of patterns used for random simulation (this number is fixed for the FRAIG run)
+int Fraig_ManReadPatternNumRandom( Fraig_Man_t * p ) { return p->nWordsRand * 32; }
+// returns the number of dynamic patterns accumulated at runtime (include SAT solver counter-examples and distance-1 patterns derived from them)
+int Fraig_ManReadPatternNumDynamic( Fraig_Man_t * p ) { return p->iWordStart * 32; }
+// returns the number of dynamic patterns proved useful to distinquish some FRAIG nodes (this number is more than 0 after the first garbage collection of patterns)
+int Fraig_ManReadPatternNumDynamicFiltered( Fraig_Man_t * p ) { return p->iPatsPerm; }
+// returns the number of times FRAIG package timed out
+int Fraig_ManReadSatFails( Fraig_Man_t * p ) { return p->nSatFailsReal; }
+// returns the number of conflicts in the SAT solver
+int Fraig_ManReadConflicts( Fraig_Man_t * p ) { return p->pSat? Msat_SolverReadBackTracks(p->pSat) : 0; }
+// returns the number of inspections in the SAT solver
+int Fraig_ManReadInspects( Fraig_Man_t * p ) { return p->pSat? Msat_SolverReadInspects(p->pSat) : 0; }
+
+/**Function*************************************************************
+
+ Synopsis [Access functions to set the data members of the manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ManSetFuncRed( Fraig_Man_t * p, int fFuncRed ) { p->fFuncRed = fFuncRed; }
+void Fraig_ManSetFeedBack( Fraig_Man_t * p, int fFeedBack ) { p->fFeedBack = fFeedBack; }
+void Fraig_ManSetDoSparse( Fraig_Man_t * p, int fDoSparse ) { p->fDoSparse = fDoSparse; }
+void Fraig_ManSetChoicing( Fraig_Man_t * p, int fChoicing ) { p->fChoicing = fChoicing; }
+void Fraig_ManSetTryProve( Fraig_Man_t * p, int fTryProve ) { p->fTryProve = fTryProve; }
+void Fraig_ManSetVerbose( Fraig_Man_t * p, int fVerbose ) { p->fVerbose = fVerbose; }
+void Fraig_ManSetTimeToGraph( Fraig_Man_t * p, int Time ) { p->timeToAig = Time; }
+void Fraig_ManSetTimeToNet( Fraig_Man_t * p, int Time ) { p->timeToNet = Time; }
+void Fraig_ManSetTimeTotal( Fraig_Man_t * p, int Time ) { p->timeTotal = Time; }
+void Fraig_ManSetOutputNames( Fraig_Man_t * p, char ** ppNames ) { p->ppOutputNames = ppNames; }
+void Fraig_ManSetInputNames( Fraig_Man_t * p, char ** ppNames ) { p->ppInputNames = ppNames; }
+
+/**Function*************************************************************
+
+ Synopsis [Access functions to read the data members of the node.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_NodeReadData0( Fraig_Node_t * p ) { return p->pData0; }
+Fraig_Node_t * Fraig_NodeReadData1( Fraig_Node_t * p ) { return p->pData1; }
+int Fraig_NodeReadNum( Fraig_Node_t * p ) { return p->Num; }
+Fraig_Node_t * Fraig_NodeReadOne( Fraig_Node_t * p ) { assert (!Fraig_IsComplement(p)); return p->p1; }
+Fraig_Node_t * Fraig_NodeReadTwo( Fraig_Node_t * p ) { assert (!Fraig_IsComplement(p)); return p->p2; }
+Fraig_Node_t * Fraig_NodeReadNextE( Fraig_Node_t * p ) { return p->pNextE; }
+Fraig_Node_t * Fraig_NodeReadRepr( Fraig_Node_t * p ) { return p->pRepr; }
+int Fraig_NodeReadNumRefs( Fraig_Node_t * p ) { return p->nRefs; }
+int Fraig_NodeReadNumFanouts( Fraig_Node_t * p ) { return p->nFanouts; }
+int Fraig_NodeReadSimInv( Fraig_Node_t * p ) { return p->fInv; }
+int Fraig_NodeReadNumOnes( Fraig_Node_t * p ) { return p->nOnes; }
+// returns the pointer to the random simulation patterns (their number is returned by Fraig_ManReadPatternNumRandom)
+// memory pointed to by this and the following procedure is maintained by the FRAIG package and exists as long as the package runs
+unsigned * Fraig_NodeReadPatternsRandom( Fraig_Node_t * p ) { return p->puSimR; }
+// returns the pointer to the dynamic simulation patterns (their number is returned by Fraig_ManReadPatternNumDynamic or Fraig_ManReadPatternNumDynamicFiltered)
+// if the number of patterns is not evenly divisible by 32, the patterns beyond the given number contain garbage
+unsigned * Fraig_NodeReadPatternsDynamic( Fraig_Node_t * p ) { return p->puSimD; }
+
+/**Function*************************************************************
+
+ Synopsis [Access functions to set the data members of the node.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeSetData0( Fraig_Node_t * p, Fraig_Node_t * pData ) { p->pData0 = pData; }
+void Fraig_NodeSetData1( Fraig_Node_t * p, Fraig_Node_t * pData ) { p->pData1 = pData; }
+
+/**Function*************************************************************
+
+ Synopsis [Checks the type of the node.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeIsConst( Fraig_Node_t * p ) { return (Fraig_Regular(p))->Num == 0; }
+int Fraig_NodeIsVar( Fraig_Node_t * p ) { return (Fraig_Regular(p))->NumPi >= 0; }
+int Fraig_NodeIsAnd( Fraig_Node_t * p ) { return (Fraig_Regular(p))->NumPi < 0 && (Fraig_Regular(p))->Num > 0; }
+int Fraig_NodeComparePhase( Fraig_Node_t * p1, Fraig_Node_t * p2 ) { assert( !Fraig_IsComplement(p1) ); assert( !Fraig_IsComplement(p2) ); return p1->fInv ^ p2->fInv; }
+
+/**Function*************************************************************
+
+ Synopsis [Returns a new primary input node.]
+
+ Description [If the node with this number does not exist,
+ create a new PI node with this number.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_ManReadIthVar( Fraig_Man_t * p, int i )
+{
+ int k;
+ if ( i < 0 )
+ {
+ printf( "Requesting a PI with a negative number\n" );
+ return NULL;
+ }
+ // create the PIs to fill in the interval
+ if ( i >= p->vInputs->nSize )
+ for ( k = p->vInputs->nSize; k <= i; k++ )
+ Fraig_NodeCreatePi( p );
+ return p->vInputs->pArray[i];
+}
+
+/**Function*************************************************************
+
+ Synopsis [Creates a new PO node.]
+
+ Description [This procedure may take a complemented node.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ManSetPo( Fraig_Man_t * p, Fraig_Node_t * pNode )
+{
+ // internal node may be a PO two times
+ Fraig_Regular(pNode)->fNodePo = 1;
+ Fraig_NodeVecPush( p->vOutputs, pNode );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Perfoms the AND operation with functional hashing.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_NodeAnd( Fraig_Man_t * p, Fraig_Node_t * p1, Fraig_Node_t * p2 )
+{
+ return Fraig_NodeAndCanon( p, p1, p2 );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Perfoms the OR operation with functional hashing.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_NodeOr( Fraig_Man_t * p, Fraig_Node_t * p1, Fraig_Node_t * p2 )
+{
+ return Fraig_Not( Fraig_NodeAndCanon( p, Fraig_Not(p1), Fraig_Not(p2) ) );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Perfoms the EXOR operation with functional hashing.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_NodeExor( Fraig_Man_t * p, Fraig_Node_t * p1, Fraig_Node_t * p2 )
+{
+ return Fraig_NodeMux( p, p1, Fraig_Not(p2), p2 );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Perfoms the MUX operation with functional hashing.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_NodeMux( Fraig_Man_t * p, Fraig_Node_t * pC, Fraig_Node_t * pT, Fraig_Node_t * pE )
+{
+ Fraig_Node_t * pAnd1, * pAnd2, * pRes;
+ pAnd1 = Fraig_NodeAndCanon( p, pC, pT ); Fraig_Ref( pAnd1 );
+ pAnd2 = Fraig_NodeAndCanon( p, Fraig_Not(pC), pE ); Fraig_Ref( pAnd2 );
+ pRes = Fraig_NodeOr( p, pAnd1, pAnd2 );
+ Fraig_RecursiveDeref( p, pAnd1 );
+ Fraig_RecursiveDeref( p, pAnd2 );
+ Fraig_Deref( pRes );
+ return pRes;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Sets the node to be equivalent to the given one.]
+
+ Description [This procedure is a work-around for the equivalence check.
+ Does not verify the equivalence. Use at the user's risk.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeSetChoice( Fraig_Man_t * pMan, Fraig_Node_t * pNodeOld, Fraig_Node_t * pNodeNew )
+{
+// assert( pMan->fChoicing );
+ pNodeNew->pNextE = pNodeOld->pNextE;
+ pNodeOld->pNextE = pNodeNew;
+ pNodeNew->pRepr = pNodeOld;
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/fraig/fraigCanon.c b/src/sat/fraig/fraigCanon.c
new file mode 100644
index 00000000..89bc924f
--- /dev/null
+++ b/src/sat/fraig/fraigCanon.c
@@ -0,0 +1,218 @@
+/**CFile****************************************************************
+
+ FileName [fraigCanon.c]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [AND-node creation and elementary AND-operation.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigCanon.c,v 1.4 2005/07/08 01:01:31 alanmi Exp $]
+
+***********************************************************************/
+
+#include <limits.h>
+#include "fraigInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [The internal AND operation for the two FRAIG nodes.]
+
+ Description [This procedure is the core of the FRAIG package, because
+ it performs the two-step canonicization of FRAIG nodes. The first step
+ involves the lookup in the structural hash table (which hashes two ANDs
+ into a node that has them as fanins, if such a node exists). If the node
+ is not found in the structural hash table, an attempt is made to find a
+ functionally equivalent node in another hash table (which hashes the
+ simulation info into the nodes, which has this simulation info). Some
+ tricks used on the way are described in the comments to the code and
+ in the paper "FRAIGs: Functionally reduced AND-INV graphs".]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_NodeAndCanon( Fraig_Man_t * pMan, Fraig_Node_t * p1, Fraig_Node_t * p2 )
+{
+ Fraig_Node_t * pNodeNew, * pNodeOld, * pNodeRepr;
+ int fUseSatCheck;
+// int RetValue;
+
+ // check for trivial cases
+ if ( p1 == p2 )
+ return p1;
+ if ( p1 == Fraig_Not(p2) )
+ return Fraig_Not(pMan->pConst1);
+ if ( Fraig_NodeIsConst(p1) )
+ {
+ if ( p1 == pMan->pConst1 )
+ return p2;
+ return Fraig_Not(pMan->pConst1);
+ }
+ if ( Fraig_NodeIsConst(p2) )
+ {
+ if ( p2 == pMan->pConst1 )
+ return p1;
+ return Fraig_Not(pMan->pConst1);
+ }
+/*
+ // check for less trivial cases
+ if ( Fraig_IsComplement(p1) )
+ {
+ if ( RetValue = Fraig_NodeIsInSupergate( Fraig_Regular(p1), p2 ) )
+ {
+ if ( RetValue == -1 )
+ pMan->nImplies0++;
+ else
+ pMan->nImplies1++;
+
+ if ( RetValue == -1 )
+ return p2;
+ }
+ }
+ else
+ {
+ if ( RetValue = Fraig_NodeIsInSupergate( p1, p2 ) )
+ {
+ if ( RetValue == 1 )
+ pMan->nSimplifies1++;
+ else
+ pMan->nSimplifies0++;
+
+ if ( RetValue == 1 )
+ return p1;
+ return Fraig_Not(pMan->pConst1);
+ }
+ }
+
+ if ( Fraig_IsComplement(p2) )
+ {
+ if ( RetValue = Fraig_NodeIsInSupergate( Fraig_Regular(p2), p1 ) )
+ {
+ if ( RetValue == -1 )
+ pMan->nImplies0++;
+ else
+ pMan->nImplies1++;
+
+ if ( RetValue == -1 )
+ return p1;
+ }
+ }
+ else
+ {
+ if ( RetValue = Fraig_NodeIsInSupergate( p2, p1 ) )
+ {
+ if ( RetValue == 1 )
+ pMan->nSimplifies1++;
+ else
+ pMan->nSimplifies0++;
+
+ if ( RetValue == 1 )
+ return p2;
+ return Fraig_Not(pMan->pConst1);
+ }
+ }
+*/
+ // perform level-one structural hashing
+ if ( Fraig_HashTableLookupS( pMan, p1, p2, &pNodeNew ) ) // the node with these children is found
+ {
+ // if the existent node is part of the cone of unused logic
+ // (that is logic feeding the node which is equivalent to the given node)
+ // return the canonical representative of this node
+ // determine the phase of the given node, with respect to its canonical form
+ pNodeRepr = Fraig_Regular(pNodeNew)->pRepr;
+ if ( pMan->fFuncRed && pNodeRepr )
+ return Fraig_NotCond( pNodeRepr, Fraig_IsComplement(pNodeNew) ^ Fraig_NodeComparePhase(Fraig_Regular(pNodeNew), pNodeRepr) );
+ // otherwise, the node is itself a canonical representative, return it
+ return pNodeNew;
+ }
+ // the same node is not found, but the new one is created
+
+ // if one level hashing is requested (without functionality hashing), return
+ if ( !pMan->fFuncRed )
+ return pNodeNew;
+
+ // check if the new node is unique using the simulation info
+ if ( pNodeNew->nOnes == 0 || pNodeNew->nOnes == (unsigned)pMan->nWordsRand * 32 )
+ {
+ pMan->nSatZeros++;
+ if ( !pMan->fDoSparse ) // if we do not do sparse functions, skip
+ return pNodeNew;
+ // check the sparse function simulation hash table
+ pNodeOld = Fraig_HashTableLookupF0( pMan, pNodeNew );
+ if ( pNodeOld == NULL ) // the node is unique (it is added to the table)
+ return pNodeNew;
+ }
+ else
+ {
+ // check the simulation hash table
+ pNodeOld = Fraig_HashTableLookupF( pMan, pNodeNew );
+ if ( pNodeOld == NULL ) // the node is unique
+ return pNodeNew;
+ }
+ assert( pNodeOld->pRepr == 0 );
+ // there is another node which looks the same according to simulation
+
+ // use SAT to resolve the ambiguity
+ fUseSatCheck = (pMan->nInspLimit == 0 || Fraig_ManReadInspects(pMan) < pMan->nInspLimit);
+ if ( fUseSatCheck && Fraig_NodeIsEquivalent( pMan, pNodeOld, pNodeNew, pMan->nBTLimit, 1000000 ) )
+ {
+ // set the node to be equivalent with this node
+ // to prevent loops, only set if the old node is not in the TFI of the new node
+ // the loop may happen in the following case: suppose
+ // NodeC = AND(NodeA, NodeB) and at the same time NodeA => NodeB
+ // in this case, NodeA and NodeC are functionally equivalent
+ // however, NodeA is a fanin of node NodeC (this leads to the loop)
+ // add the node to the list of equivalent nodes or dereference it
+ if ( pMan->fChoicing && !Fraig_CheckTfi( pMan, pNodeOld, pNodeNew ) )
+ {
+ // if the old node is not in the TFI of the new node and choicing
+ // is enabled, add the new node to the list of equivalent ones
+ pNodeNew->pNextE = pNodeOld->pNextE;
+ pNodeOld->pNextE = pNodeNew;
+ }
+ // set the canonical representative of this node
+ pNodeNew->pRepr = pNodeOld;
+ // return the equivalent node
+ return Fraig_NotCond( pNodeOld, Fraig_NodeComparePhase(pNodeOld, pNodeNew) );
+ }
+
+ // now we add another member to this simulation class
+ if ( pNodeNew->nOnes == 0 || pNodeNew->nOnes == (unsigned)pMan->nWordsRand * 32 )
+ {
+ Fraig_Node_t * pNodeTemp;
+ assert( pMan->fDoSparse );
+ pNodeTemp = Fraig_HashTableLookupF0( pMan, pNodeNew );
+// assert( pNodeTemp == NULL );
+// Fraig_HashTableInsertF0( pMan, pNodeNew );
+ }
+ else
+ {
+ pNodeNew->pNextD = pNodeOld->pNextD;
+ pNodeOld->pNextD = pNodeNew;
+ }
+ // return the new node
+ assert( pNodeNew->pRepr == 0 );
+ return pNodeNew;
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/fraig/fraigChoice.c b/src/sat/fraig/fraigChoice.c
new file mode 100644
index 00000000..896e5d2d
--- /dev/null
+++ b/src/sat/fraig/fraigChoice.c
@@ -0,0 +1,241 @@
+/**CFile****************************************************************
+
+ FileName [fraigTrans.c]
+
+ PackageName [MVSIS 1.3: Multi-valued logic synthesis system.]
+
+ Synopsis [Adds the additive and distributive choices to the AIG.]
+
+ Author [MVSIS Group]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - February 1, 2003.]
+
+ Revision [$Id: fraigTrans.c,v 1.1 2005/02/28 05:34:34 alanmi Exp $]
+
+***********************************************************************/
+
+#include "fraigInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Adds choice nodes based on associativity.]
+
+ Description [Make nLimit big AND gates and add all decompositions
+ to the Fraig.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ManAddChoices( Fraig_Man_t * pMan, int fVerbose, int nLimit )
+{
+// ProgressBar * pProgress;
+ char Buffer[100];
+ int clkTotal = clock();
+ int i, nNodesBefore, nNodesAfter, nInputs, nMaxNodes;
+ int /*nMaxLevel,*/ nDistributive;
+ Fraig_Node_t *pNode, *pRepr;
+ Fraig_Node_t *pX, *pA, *pB, *pC, /* *pD,*/ *pN, /* *pQ, *pR,*/ *pT;
+ int fShortCut = 0;
+
+ nDistributive = 0;
+
+// Fraig_ManSetApprox( pMan, 1 );
+
+ // NO functional reduction
+ if (fShortCut) Fraig_ManSetFuncRed( pMan, 0 );
+
+ // First we mark critical functions i.e. compute those
+ // nodes which lie on the critical path. Note that this
+ // doesn't update the required times on any choice nodes
+ // which are not the representatives
+/*
+ nMaxLevel = Fraig_GetMaxLevel( pMan );
+ for ( i = 0; i < pMan->nOutputs; i++ )
+ {
+ Fraig_SetNodeRequired( pMan, pMan->pOutputs[i], nMaxLevel );
+ }
+*/
+ nNodesBefore = Fraig_ManReadNodeNum( pMan );
+ nInputs = Fraig_ManReadInputNum( pMan );
+ nMaxNodes = nInputs + nLimit * ( nNodesBefore - nInputs );
+
+ printf ("Limit = %d, Before = %d\n", nMaxNodes, nNodesBefore );
+
+ if (0)
+ {
+ char buffer[128];
+ sprintf (buffer, "test" );
+// Fraig_MappingShow( pMan, buffer );
+ }
+
+// pProgress = Extra_ProgressBarStart( stdout, nMaxNodes );
+Fraig_ManCheckConsistency( pMan );
+
+ for ( i = nInputs+1; (i < Fraig_ManReadNodeNum( pMan ))
+ && (nMaxNodes > Fraig_ManReadNodeNum( pMan )); ++i )
+ {
+// if ( i == nNodesBefore )
+// break;
+
+ pNode = Fraig_ManReadIthNode( pMan, i );
+ assert ( pNode );
+
+ pRepr = pNode->pRepr ? pNode->pRepr : pNode;
+ //printf ("Slack: %d\n", Fraig_NodeReadSlack( pRepr ));
+
+ // All the new associative choices we add will have huge slack
+ // since we do not redo timing, and timing doesnt handle choices
+ // well anyway. However every newly added node is a choice of an
+ // existing critical node, so they are considered critical.
+// if ( (Fraig_NodeReadSlack( pRepr ) > 3) && (i < nNodesBefore) )
+// continue;
+
+// if ( pNode->pRepr )
+// continue;
+
+ // Try ((ab)c), x = ab -> (a(bc)) and (b(ac))
+ pX = Fraig_NodeReadOne(pNode);
+ pC = Fraig_NodeReadTwo(pNode);
+ if (Fraig_NodeIsAnd(pX) && !Fraig_IsComplement(pX))
+ {
+ pA = Fraig_NodeReadOne(Fraig_Regular(pX));
+ pB = Fraig_NodeReadTwo(Fraig_Regular(pX));
+
+// pA = Fraig_NodeGetRepr( pA );
+// pB = Fraig_NodeGetRepr( pB );
+// pC = Fraig_NodeGetRepr( pC );
+
+ if (fShortCut)
+ {
+ pT = Fraig_NodeAnd(pMan, pB, pC);
+ if ( !pT->pRepr )
+ {
+ pN = Fraig_NodeAnd(pMan, pA, pT);
+// Fraig_NodeAddChoice( pMan, pNode, pN );
+ }
+ }
+ else
+ pN = Fraig_NodeAnd(pMan, pA, Fraig_NodeAnd(pMan, pB, pC));
+ // assert ( Fraig_NodesEqual(pN, pNode) );
+
+
+ if (fShortCut)
+ {
+ pT = Fraig_NodeAnd(pMan, pA, pC);
+ if ( !pT->pRepr )
+ {
+ pN = Fraig_NodeAnd(pMan, pB, pT);
+// Fraig_NodeAddChoice( pMan, pNode, pN );
+ }
+ }
+ else
+ pN = Fraig_NodeAnd(pMan, pB, Fraig_NodeAnd(pMan, pA, pC));
+ // assert ( Fraig_NodesEqual(pN, pNode) );
+ }
+
+
+ // Try (a(bc)), x = bc -> ((ab)c) and ((ac)b)
+ pA = Fraig_NodeReadOne(pNode);
+ pX = Fraig_NodeReadTwo(pNode);
+ if (Fraig_NodeIsAnd(pX) && !Fraig_IsComplement(pX))
+ {
+ pB = Fraig_NodeReadOne(Fraig_Regular(pX));
+ pC = Fraig_NodeReadTwo(Fraig_Regular(pX));
+
+// pA = Fraig_NodeGetRepr( pA );
+// pB = Fraig_NodeGetRepr( pB );
+// pC = Fraig_NodeGetRepr( pC );
+
+ if (fShortCut)
+ {
+ pT = Fraig_NodeAnd(pMan, pA, pB);
+ if ( !pT->pRepr )
+ {
+ pN = Fraig_NodeAnd(pMan, pC, pT);
+// Fraig_NodeAddChoice( pMan, pNode, pN );
+ }
+ }
+ else
+ pN = Fraig_NodeAnd(pMan, Fraig_NodeAnd(pMan, pA, pB), pC);
+ // assert ( Fraig_NodesEqual(pN, pNode) );
+
+ if (fShortCut)
+ {
+ pT = Fraig_NodeAnd(pMan, pA, pC);
+ if ( !pT->pRepr )
+ {
+ pN = Fraig_NodeAnd(pMan, pB, pT);
+// Fraig_NodeAddChoice( pMan, pNode, pN );
+ }
+ }
+ else
+ pN = Fraig_NodeAnd(pMan, Fraig_NodeAnd(pMan, pA, pC), pB);
+ // assert ( Fraig_NodesEqual(pN, pNode) );
+ }
+
+
+/*
+ // Try distributive transform
+ pQ = Fraig_NodeReadOne(pNode);
+ pR = Fraig_NodeReadTwo(pNode);
+ if ( (Fraig_IsComplement(pQ) && Fraig_NodeIsAnd(pQ))
+ && (Fraig_IsComplement(pR) && Fraig_NodeIsAnd(pR)) )
+ {
+ pA = Fraig_NodeReadOne(Fraig_Regular(pQ));
+ pB = Fraig_NodeReadTwo(Fraig_Regular(pQ));
+ pC = Fraig_NodeReadOne(Fraig_Regular(pR));
+ pD = Fraig_NodeReadTwo(Fraig_Regular(pR));
+
+ // Now detect the !(xy + xz) pattern, store
+ // x in pA, y in pB and z in pC and set pD = 0 to indicate
+ // pattern was found
+ assert (pD != 0);
+ if (pA == pC) { pC = pD; pD = 0; }
+ if (pA == pD) { pD = 0; }
+ if (pB == pC) { pB = pA; pA = pC; pC = pD; pD = 0; }
+ if (pB == pD) { pB = pA; pA = pD; pD = 0; }
+ if (pD == 0)
+ {
+ nDistributive++;
+ pN = Fraig_Not(Fraig_NodeAnd(pMan, pA,
+ Fraig_NodeOr(pMan, pB, pC)));
+ if (fShortCut) Fraig_NodeAddChoice( pMan, pNode, pN );
+ // assert ( Fraig_NodesEqual(pN, pNode) );
+ }
+ }
+*/
+ if ( i % 1000 == 0 )
+ {
+ sprintf( Buffer, "Adding choice %6d...", i - nNodesBefore );
+// Extra_ProgressBarUpdate( pProgress, i, Buffer );
+ }
+ }
+
+// Extra_ProgressBarStop( pProgress );
+
+Fraig_ManCheckConsistency( pMan );
+
+ nNodesAfter = Fraig_ManReadNodeNum( pMan );
+ printf ( "Nodes before = %6d. Nodes with associative choices = %6d. Increase = %4.2f %%.\n",
+ nNodesBefore, nNodesAfter, ((float)(nNodesAfter - nNodesBefore)) * 100.0/(nNodesBefore - nInputs) );
+ printf ( "Distributive = %d\n", nDistributive );
+
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/fraig/fraigFanout.c b/src/sat/fraig/fraigFanout.c
new file mode 100644
index 00000000..789bffca
--- /dev/null
+++ b/src/sat/fraig/fraigFanout.c
@@ -0,0 +1,175 @@
+/**CFile****************************************************************
+
+ FileName [fraigFanout.c]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [Procedures to manipulate fanouts of the FRAIG nodes.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigFanout.c,v 1.5 2005/07/08 01:01:31 alanmi Exp $]
+
+***********************************************************************/
+
+#include "fraigInt.h"
+
+#ifdef FRAIG_ENABLE_FANOUTS
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Add the fanout to the node.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeAddFaninFanout( Fraig_Node_t * pFanin, Fraig_Node_t * pFanout )
+{
+ Fraig_Node_t * pPivot;
+
+ // pFanins is a fanin of pFanout
+ assert( !Fraig_IsComplement(pFanin) );
+ assert( !Fraig_IsComplement(pFanout) );
+ assert( Fraig_Regular(pFanout->p1) == pFanin || Fraig_Regular(pFanout->p2) == pFanin );
+
+ pPivot = pFanin->pFanPivot;
+ if ( pPivot == NULL )
+ {
+ pFanin->pFanPivot = pFanout;
+ return;
+ }
+
+ if ( Fraig_Regular(pPivot->p1) == pFanin )
+ {
+ if ( Fraig_Regular(pFanout->p1) == pFanin )
+ {
+ pFanout->pFanFanin1 = pPivot->pFanFanin1;
+ pPivot->pFanFanin1 = pFanout;
+ }
+ else // if ( Fraig_Regular(pFanout->p2) == pFanin )
+ {
+ pFanout->pFanFanin2 = pPivot->pFanFanin1;
+ pPivot->pFanFanin1 = pFanout;
+ }
+ }
+ else // if ( Fraig_Regular(pPivot->p2) == pFanin )
+ {
+ assert( Fraig_Regular(pPivot->p2) == pFanin );
+ if ( Fraig_Regular(pFanout->p1) == pFanin )
+ {
+ pFanout->pFanFanin1 = pPivot->pFanFanin2;
+ pPivot->pFanFanin2 = pFanout;
+ }
+ else // if ( Fraig_Regular(pFanout->p2) == pFanin )
+ {
+ pFanout->pFanFanin2 = pPivot->pFanFanin2;
+ pPivot->pFanFanin2 = pFanout;
+ }
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Add the fanout to the node.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeRemoveFaninFanout( Fraig_Node_t * pFanin, Fraig_Node_t * pFanoutToRemove )
+{
+ Fraig_Node_t * pFanout, * pFanout2, ** ppFanList;
+ // start the linked list of fanouts
+ ppFanList = &pFanin->pFanPivot;
+ // go through the fanouts
+ Fraig_NodeForEachFanoutSafe( pFanin, pFanout, pFanout2 )
+ {
+ // skip the fanout-to-remove
+ if ( pFanout == pFanoutToRemove )
+ continue;
+ // add useful fanouts to the list
+ *ppFanList = pFanout;
+ ppFanList = Fraig_NodeReadNextFanoutPlace( pFanin, pFanout );
+ }
+ *ppFanList = NULL;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Transfers fanout to a different node.]
+
+ Description [Assumes that the other node currently has no fanouts.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeTransferFanout( Fraig_Node_t * pNodeFrom, Fraig_Node_t * pNodeTo )
+{
+ Fraig_Node_t * pFanout;
+ assert( pNodeTo->pFanPivot == NULL );
+ assert( pNodeTo->pFanFanin1 == NULL );
+ assert( pNodeTo->pFanFanin2 == NULL );
+ // go through the fanouts and update their fanins
+ Fraig_NodeForEachFanout( pNodeFrom, pFanout )
+ {
+ if ( Fraig_Regular(pFanout->p1) == pNodeFrom )
+ pFanout->p1 = Fraig_NotCond( pNodeTo, Fraig_IsComplement(pFanout->p1) );
+ else if ( Fraig_Regular(pFanout->p2) == pNodeFrom )
+ pFanout->p2 = Fraig_NotCond( pNodeTo, Fraig_IsComplement(pFanout->p2) );
+ }
+ // move the pointers
+ pNodeTo->pFanPivot = pNodeFrom->pFanPivot;
+ pNodeTo->pFanFanin1 = pNodeFrom->pFanFanin1;
+ pNodeTo->pFanFanin2 = pNodeFrom->pFanFanin2;
+ pNodeFrom->pFanPivot = NULL;
+ pNodeFrom->pFanFanin1 = NULL;
+ pNodeFrom->pFanFanin2 = NULL;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns the number of fanouts of a node.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeGetFanoutNum( Fraig_Node_t * pNode )
+{
+ Fraig_Node_t * pFanout;
+ int Counter = 0;
+ Fraig_NodeForEachFanout( pNode, pFanout )
+ Counter++;
+ return Counter;
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+#endif
+
diff --git a/src/sat/fraig/fraigFeed.c b/src/sat/fraig/fraigFeed.c
new file mode 100644
index 00000000..8a3cc6c7
--- /dev/null
+++ b/src/sat/fraig/fraigFeed.c
@@ -0,0 +1,909 @@
+/**CFile****************************************************************
+
+ FileName [fraigFeed.c]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [Procedures to support the solver feedback.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigFeed.c,v 1.8 2005/07/08 01:01:31 alanmi Exp $]
+
+***********************************************************************/
+
+#include "fraigInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+static int Fraig_FeedBackPrepare( Fraig_Man_t * p, int * pModel, Msat_IntVec_t * vVars );
+static int Fraig_FeedBackInsert( Fraig_Man_t * p, int nVarsPi );
+static void Fraig_FeedBackVerify( Fraig_Man_t * p, Fraig_Node_t * pOld, Fraig_Node_t * pNew );
+
+static void Fraig_FeedBackCovering( Fraig_Man_t * p, Msat_IntVec_t * vPats );
+static Fraig_NodeVec_t * Fraig_FeedBackCoveringStart( Fraig_Man_t * pMan );
+static int Fraig_GetSmallestColumn( int * pHits, int nHits );
+static int Fraig_GetHittingPattern( unsigned * pSims, int nWords );
+static void Fraig_CancelCoveredColumns( Fraig_NodeVec_t * vColumns, int * pHits, int iPat );
+static void Fraig_FeedBackCheckTable( Fraig_Man_t * p );
+static void Fraig_FeedBackCheckTableF0( Fraig_Man_t * p );
+static void Fraig_ReallocateSimulationInfo( Fraig_Man_t * p );
+
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Initializes the feedback information.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_FeedBackInit( Fraig_Man_t * p )
+{
+ p->vCones = Fraig_NodeVecAlloc( 500 );
+ p->vPatsReal = Msat_IntVecAlloc( 1000 );
+ p->pSimsReal = (unsigned *)Fraig_MemFixedEntryFetch( p->mmSims );
+ memset( p->pSimsReal, 0, sizeof(unsigned) * p->nWordsDyna );
+ p->pSimsTemp = (unsigned *)Fraig_MemFixedEntryFetch( p->mmSims );
+ p->pSimsDiff = (unsigned *)Fraig_MemFixedEntryFetch( p->mmSims );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Processes the feedback from teh solver.]
+
+ Description [Array pModel gives the value of each variable in the SAT
+ solver. Array vVars is the array of integer numbers of variables
+ involves in this conflict.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_FeedBack( Fraig_Man_t * p, int * pModel, Msat_IntVec_t * vVars, Fraig_Node_t * pOld, Fraig_Node_t * pNew )
+{
+ int nVarsPi, nWords;
+ int i, clk = clock();
+
+ // get the number of PI vars in the feedback (also sets the PI values)
+ nVarsPi = Fraig_FeedBackPrepare( p, pModel, vVars );
+
+ // set the PI values
+ nWords = Fraig_FeedBackInsert( p, nVarsPi );
+ assert( p->iWordStart + nWords <= p->nWordsDyna );
+
+ // resimulates the words from p->iWordStart to iWordStop
+ for ( i = 1; i < p->vNodes->nSize; i++ )
+ if ( Fraig_NodeIsAnd(p->vNodes->pArray[i]) )
+ Fraig_NodeSimulate( p->vNodes->pArray[i], p->iWordStart, p->iWordStart + nWords, 0 );
+
+ if ( p->fDoSparse )
+ Fraig_TableRehashF0( p, 0 );
+
+ if ( !p->fChoicing )
+ Fraig_FeedBackVerify( p, pOld, pNew );
+
+ // if there is no room left, compress the patterns
+ if ( p->iWordStart + nWords == p->nWordsDyna )
+ p->iWordStart = Fraig_FeedBackCompress( p );
+ else // otherwise, update the starting word
+ p->iWordStart += nWords;
+
+p->timeFeed += clock() - clk;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Get the number and values of the PI variables.]
+
+ Description [Returns the number of PI variables involved in this feedback.
+ Fills in the internal presence and value data for the primary inputs.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_FeedBackPrepare( Fraig_Man_t * p, int * pModel, Msat_IntVec_t * vVars )
+{
+ Fraig_Node_t * pNode;
+ int i, nVars, nVarsPis, * pVars;
+
+ // clean the presence flag for all PIs
+ for ( i = 0; i < p->vInputs->nSize; i++ )
+ {
+ pNode = p->vInputs->pArray[i];
+ pNode->fFeedUse = 0;
+ }
+
+ // get the variables involved in the feedback
+ nVars = Msat_IntVecReadSize(vVars);
+ pVars = Msat_IntVecReadArray(vVars);
+
+ // set the values for the present variables
+ nVarsPis = 0;
+ for ( i = 0; i < nVars; i++ )
+ {
+ pNode = p->vNodes->pArray[ pVars[i] ];
+ if ( !Fraig_NodeIsVar(pNode) )
+ continue;
+ // set its value
+ pNode->fFeedUse = 1;
+ pNode->fFeedVal = !MSAT_LITSIGN(pModel[pVars[i]]);
+ nVarsPis++;
+ }
+ return nVarsPis;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Inserts the new simulation patterns.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_FeedBackInsert( Fraig_Man_t * p, int nVarsPi )
+{
+ Fraig_Node_t * pNode;
+ int nWords, iPatFlip, nPatFlipLimit, i, w;
+ int fUseNoPats = 0;
+ int fUse2Pats = 0;
+
+ // get the number of words
+ if ( fUse2Pats )
+ nWords = FRAIG_NUM_WORDS( 2 * nVarsPi + 1 );
+ else if ( fUseNoPats )
+ nWords = 1;
+ else
+ nWords = FRAIG_NUM_WORDS( nVarsPi + 1 );
+ // update the number of words if they do not fit into the simulation info
+ if ( nWords > p->nWordsDyna - p->iWordStart )
+ nWords = p->nWordsDyna - p->iWordStart;
+ // determine the bound on the flipping bit
+ nPatFlipLimit = nWords * 32 - 2;
+
+ // mark the real pattern
+ Msat_IntVecPush( p->vPatsReal, p->iWordStart * 32 );
+ // record the real pattern
+ Fraig_BitStringSetBit( p->pSimsReal, p->iWordStart * 32 );
+
+ // set the values at the PIs
+ iPatFlip = 1;
+ for ( i = 0; i < p->vInputs->nSize; i++ )
+ {
+ pNode = p->vInputs->pArray[i];
+ for ( w = p->iWordStart; w < p->iWordStart + nWords; w++ )
+ if ( !pNode->fFeedUse )
+ pNode->puSimD[w] = FRAIG_RANDOM_UNSIGNED;
+ else if ( pNode->fFeedVal )
+ pNode->puSimD[w] = FRAIG_FULL;
+ else // if ( !pNode->fFeedVal )
+ pNode->puSimD[w] = 0;
+
+ if ( fUse2Pats )
+ {
+ // flip two patterns
+ if ( pNode->fFeedUse && 2 * iPatFlip < nPatFlipLimit )
+ {
+ Fraig_BitStringXorBit( pNode->puSimD + p->iWordStart, 2 * iPatFlip - 1 );
+ Fraig_BitStringXorBit( pNode->puSimD + p->iWordStart, 2 * iPatFlip );
+ Fraig_BitStringXorBit( pNode->puSimD + p->iWordStart, 2 * iPatFlip + 1 );
+ iPatFlip++;
+ }
+ }
+ else if ( fUseNoPats )
+ {
+ }
+ else
+ {
+ // flip the diagonal
+ if ( pNode->fFeedUse && iPatFlip < nPatFlipLimit )
+ {
+ Fraig_BitStringXorBit( pNode->puSimD + p->iWordStart, iPatFlip );
+ iPatFlip++;
+ // Extra_PrintBinary( stdout, &pNode->puSimD, 45 ); printf( "\n" );
+ }
+ }
+ // clean the use mask
+ pNode->fFeedUse = 0;
+
+ // add the info to the D hash value of the PIs
+ for ( w = p->iWordStart; w < p->iWordStart + nWords; w++ )
+ pNode->uHashD ^= pNode->puSimD[w] * s_FraigPrimes[w];
+
+ }
+ return nWords;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Checks that the SAT solver pattern indeed distinquishes the nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_FeedBackVerify( Fraig_Man_t * p, Fraig_Node_t * pOld, Fraig_Node_t * pNew )
+{
+ int fValue1, fValue2, iPat;
+ iPat = Msat_IntVecReadEntry( p->vPatsReal, Msat_IntVecReadSize(p->vPatsReal)-1 );
+ fValue1 = (Fraig_BitStringHasBit( pOld->puSimD, iPat ));
+ fValue2 = (Fraig_BitStringHasBit( pNew->puSimD, iPat ));
+/*
+Fraig_PrintNode( p, pOld );
+printf( "\n" );
+Fraig_PrintNode( p, pNew );
+printf( "\n" );
+*/
+// assert( fValue1 != fValue2 );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Compress the simulation patterns.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_FeedBackCompress( Fraig_Man_t * p )
+{
+ unsigned * pSims;
+ unsigned uHash;
+ int i, w, t, nPats, * pPats;
+ int fPerformChecks = (p->nBTLimit == -1);
+
+ // solve the covering problem
+ if ( fPerformChecks )
+ {
+ Fraig_FeedBackCheckTable( p );
+ if ( p->fDoSparse )
+ Fraig_FeedBackCheckTableF0( p );
+ }
+
+ // solve the covering problem
+ Fraig_FeedBackCovering( p, p->vPatsReal );
+
+
+ // get the number of additional patterns
+ nPats = Msat_IntVecReadSize( p->vPatsReal );
+ pPats = Msat_IntVecReadArray( p->vPatsReal );
+ // get the new starting word
+ p->iWordStart = FRAIG_NUM_WORDS( p->iPatsPerm + nPats );
+
+ // set the simulation info for the PIs
+ for ( i = 0; i < p->vInputs->nSize; i++ )
+ {
+ // get hold of the simulation info for this PI
+ pSims = p->vInputs->pArray[i]->puSimD;
+ // clean the storage for the new patterns
+ for ( w = p->iWordPerm; w < p->iWordStart; w++ )
+ p->pSimsTemp[w] = 0;
+ // set the patterns
+ for ( t = 0; t < nPats; t++ )
+ if ( Fraig_BitStringHasBit( pSims, pPats[t] ) )
+ {
+ // check if this pattern falls into temporary storage
+ if ( p->iPatsPerm + t < p->iWordPerm * 32 )
+ Fraig_BitStringSetBit( pSims, p->iPatsPerm + t );
+ else
+ Fraig_BitStringSetBit( p->pSimsTemp, p->iPatsPerm + t );
+ }
+ // copy the pattern
+ for ( w = p->iWordPerm; w < p->iWordStart; w++ )
+ pSims[w] = p->pSimsTemp[w];
+ // recompute the hashing info
+ uHash = 0;
+ for ( w = 0; w < p->iWordStart; w++ )
+ uHash ^= pSims[w] * s_FraigPrimes[w];
+ p->vInputs->pArray[i]->uHashD = uHash;
+ }
+
+ // update info about the permanently stored patterns
+ p->iWordPerm = p->iWordStart;
+ p->iPatsPerm += nPats;
+ assert( p->iWordPerm == FRAIG_NUM_WORDS( p->iPatsPerm ) );
+
+ // resimulate and recompute the hash values
+ for ( i = 1; i < p->vNodes->nSize; i++ )
+ if ( Fraig_NodeIsAnd(p->vNodes->pArray[i]) )
+ {
+ p->vNodes->pArray[i]->uHashD = 0;
+ Fraig_NodeSimulate( p->vNodes->pArray[i], 0, p->iWordPerm, 0 );
+ }
+
+ // double-check that the nodes are still distinguished
+ if ( fPerformChecks )
+ Fraig_FeedBackCheckTable( p );
+
+ // rehash the values in the F0 table
+ if ( p->fDoSparse )
+ {
+ Fraig_TableRehashF0( p, 0 );
+ if ( fPerformChecks )
+ Fraig_FeedBackCheckTableF0( p );
+ }
+
+ // check if we need to resize the simulation info
+ // if less than FRAIG_WORDS_STORE words are left, reallocate simulation info
+ if ( p->iWordPerm + FRAIG_WORDS_STORE > p->nWordsDyna )
+ Fraig_ReallocateSimulationInfo( p );
+
+ // set the real patterns
+ Msat_IntVecClear( p->vPatsReal );
+ memset( p->pSimsReal, 0, sizeof(unsigned)*p->nWordsDyna );
+ for ( w = 0; w < p->iWordPerm; w++ )
+ p->pSimsReal[w] = FRAIG_FULL;
+ if ( p->iPatsPerm % 32 > 0 )
+ p->pSimsReal[p->iWordPerm-1] = FRAIG_MASK( p->iPatsPerm % 32 );
+// printf( "The number of permanent words = %d.\n", p->iWordPerm );
+ return p->iWordStart;
+}
+
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Checks the correctness of the functional simulation table.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_FeedBackCovering( Fraig_Man_t * p, Msat_IntVec_t * vPats )
+{
+ Fraig_NodeVec_t * vColumns;
+ unsigned * pSims;
+ int * pHits, iPat, iCol, i;
+ int nOnesTotal, nSolStarting;
+ int fVeryVerbose = 0;
+
+ // collect the pairs to be distinguished
+ vColumns = Fraig_FeedBackCoveringStart( p );
+ // collect the number of 1s in each simulation vector
+ nOnesTotal = 0;
+ pHits = ALLOC( int, vColumns->nSize );
+ for ( i = 0; i < vColumns->nSize; i++ )
+ {
+ pSims = (unsigned *)vColumns->pArray[i];
+ pHits[i] = Fraig_BitStringCountOnes( pSims, p->iWordStart );
+ nOnesTotal += pHits[i];
+// assert( pHits[i] > 0 );
+ }
+
+ // go through the patterns
+ nSolStarting = Msat_IntVecReadSize(vPats);
+ while ( (iCol = Fraig_GetSmallestColumn( pHits, vColumns->nSize )) != -1 )
+ {
+ // find the pattern, which hits this column
+ iPat = Fraig_GetHittingPattern( (unsigned *)vColumns->pArray[iCol], p->iWordStart );
+ // cancel the columns covered by this pattern
+ Fraig_CancelCoveredColumns( vColumns, pHits, iPat );
+ // save the pattern
+ Msat_IntVecPush( vPats, iPat );
+ }
+
+ // free the set of columns
+ for ( i = 0; i < vColumns->nSize; i++ )
+ Fraig_MemFixedEntryRecycle( p->mmSims, (char *)vColumns->pArray[i] );
+
+ // print stats related to the covering problem
+ if ( p->fVerbose && fVeryVerbose )
+ {
+ printf( "%3d\\%3d\\%3d ", p->nWordsRand, p->nWordsDyna, p->iWordPerm );
+ printf( "Col (pairs) = %5d. ", vColumns->nSize );
+ printf( "Row (pats) = %5d. ", p->iWordStart * 32 );
+ printf( "Dns = %6.2f %%. ", vColumns->nSize==0? 0.0 : 100.0 * nOnesTotal / vColumns->nSize / p->iWordStart / 32 );
+ printf( "Sol = %3d (%3d). ", Msat_IntVecReadSize(vPats), nSolStarting );
+ printf( "\n" );
+ }
+ Fraig_NodeVecFree( vColumns );
+ free( pHits );
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Checks the correctness of the functional simulation table.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_NodeVec_t * Fraig_FeedBackCoveringStart( Fraig_Man_t * p )
+{
+ Fraig_NodeVec_t * vColumns;
+ Fraig_HashTable_t * pT = p->pTableF;
+ Fraig_Node_t * pEntF, * pEntD;
+ unsigned * pSims;
+ unsigned * pUnsigned1, * pUnsigned2;
+ int i, k, m, w;//, nOnes;
+
+ // start the set of columns
+ vColumns = Fraig_NodeVecAlloc( 100 );
+
+ // go through the pairs of nodes to be distinguished
+ for ( i = 0; i < pT->nBins; i++ )
+ Fraig_TableBinForEachEntryF( pT->pBins[i], pEntF )
+ {
+ p->vCones->nSize = 0;
+ Fraig_TableBinForEachEntryD( pEntF, pEntD )
+ Fraig_NodeVecPush( p->vCones, pEntD );
+ if ( p->vCones->nSize == 1 )
+ continue;
+ //////////////////////////////// bug fix by alanmi, September 14, 2006
+ if ( p->vCones->nSize > 20 )
+ continue;
+ ////////////////////////////////
+
+ for ( k = 0; k < p->vCones->nSize; k++ )
+ for ( m = k+1; m < p->vCones->nSize; m++ )
+ {
+ if ( !Fraig_CompareSimInfoUnderMask( p->vCones->pArray[k], p->vCones->pArray[m], p->iWordStart, 0, p->pSimsReal ) )
+ continue;
+
+ // primary simulation patterns (counter-examples) cannot distinguish this pair
+ // get memory to store the feasible simulation patterns
+ pSims = (unsigned *)Fraig_MemFixedEntryFetch( p->mmSims );
+ // find the pattern that distinguish this column, exept the primary ones
+ pUnsigned1 = p->vCones->pArray[k]->puSimD;
+ pUnsigned2 = p->vCones->pArray[m]->puSimD;
+ for ( w = 0; w < p->iWordStart; w++ )
+ pSims[w] = (pUnsigned1[w] ^ pUnsigned2[w]) & ~p->pSimsReal[w];
+ // store the pattern
+ Fraig_NodeVecPush( vColumns, (Fraig_Node_t *)pSims );
+// nOnes = Fraig_BitStringCountOnes(pSims, p->iWordStart);
+// assert( nOnes > 0 );
+ }
+ }
+
+ // if the flag is not set, do not consider sparse nodes in p->pTableF0
+ if ( !p->fDoSparse )
+ return vColumns;
+
+ // recalculate their hash values based on p->pSimsReal
+ pT = p->pTableF0;
+ for ( i = 0; i < pT->nBins; i++ )
+ Fraig_TableBinForEachEntryF( pT->pBins[i], pEntF )
+ {
+ pSims = pEntF->puSimD;
+ pEntF->uHashD = 0;
+ for ( w = 0; w < p->iWordStart; w++ )
+ pEntF->uHashD ^= (pSims[w] & p->pSimsReal[w]) * s_FraigPrimes[w];
+ }
+
+ // rehash the table using these values
+ Fraig_TableRehashF0( p, 1 );
+
+ // collect the classes of equivalent node pairs
+ for ( i = 0; i < pT->nBins; i++ )
+ Fraig_TableBinForEachEntryF( pT->pBins[i], pEntF )
+ {
+ p->vCones->nSize = 0;
+ Fraig_TableBinForEachEntryD( pEntF, pEntD )
+ Fraig_NodeVecPush( p->vCones, pEntD );
+ if ( p->vCones->nSize == 1 )
+ continue;
+
+ // primary simulation patterns (counter-examples) cannot distinguish all these pairs
+ for ( k = 0; k < p->vCones->nSize; k++ )
+ for ( m = k+1; m < p->vCones->nSize; m++ )
+ {
+ // get memory to store the feasible simulation patterns
+ pSims = (unsigned *)Fraig_MemFixedEntryFetch( p->mmSims );
+ // find the patterns that are not distinquished
+ pUnsigned1 = p->vCones->pArray[k]->puSimD;
+ pUnsigned2 = p->vCones->pArray[m]->puSimD;
+ for ( w = 0; w < p->iWordStart; w++ )
+ pSims[w] = (pUnsigned1[w] ^ pUnsigned2[w]) & ~p->pSimsReal[w];
+ // store the pattern
+ Fraig_NodeVecPush( vColumns, (Fraig_Node_t *)pSims );
+// nOnes = Fraig_BitStringCountOnes(pSims, p->iWordStart);
+// assert( nOnes > 0 );
+ }
+ }
+ return vColumns;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Selects the column, which has the smallest number of hits.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_GetSmallestColumn( int * pHits, int nHits )
+{
+ int i, iColMin = -1, nHitsMin = 1000000;
+ for ( i = 0; i < nHits; i++ )
+ {
+ // skip covered columns
+ if ( pHits[i] == 0 )
+ continue;
+ // take the column if it can only be covered by one pattern
+ if ( pHits[i] == 1 )
+ return i;
+ // find the column, which requires the smallest number of patterns
+ if ( nHitsMin > pHits[i] )
+ {
+ nHitsMin = pHits[i];
+ iColMin = i;
+ }
+ }
+ return iColMin;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Select the pattern, which hits this column.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_GetHittingPattern( unsigned * pSims, int nWords )
+{
+ int i, b;
+ for ( i = 0; i < nWords; i++ )
+ {
+ if ( pSims[i] == 0 )
+ continue;
+ for ( b = 0; b < 32; b++ )
+ if ( pSims[i] & (1 << b) )
+ return i * 32 + b;
+ }
+ return -1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Cancel covered patterns.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_CancelCoveredColumns( Fraig_NodeVec_t * vColumns, int * pHits, int iPat )
+{
+ unsigned * pSims;
+ int i;
+ for ( i = 0; i < vColumns->nSize; i++ )
+ {
+ pSims = (unsigned *)vColumns->pArray[i];
+ if ( Fraig_BitStringHasBit( pSims, iPat ) )
+ pHits[i] = 0;
+ }
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Checks the correctness of the functional simulation table.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_FeedBackCheckTable( Fraig_Man_t * p )
+{
+ Fraig_HashTable_t * pT = p->pTableF;
+ Fraig_Node_t * pEntF, * pEntD;
+ int i, k, m, nPairs;
+ int clk = clock();
+
+ nPairs = 0;
+ for ( i = 0; i < pT->nBins; i++ )
+ Fraig_TableBinForEachEntryF( pT->pBins[i], pEntF )
+ {
+ p->vCones->nSize = 0;
+ Fraig_TableBinForEachEntryD( pEntF, pEntD )
+ Fraig_NodeVecPush( p->vCones, pEntD );
+ if ( p->vCones->nSize == 1 )
+ continue;
+ for ( k = 0; k < p->vCones->nSize; k++ )
+ for ( m = k+1; m < p->vCones->nSize; m++ )
+ {
+ if ( Fraig_CompareSimInfo( p->vCones->pArray[k], p->vCones->pArray[m], p->iWordStart, 0 ) )
+ printf( "Nodes %d and %d have the same D simulation info.\n",
+ p->vCones->pArray[k]->Num, p->vCones->pArray[m]->Num );
+ nPairs++;
+ }
+ }
+// printf( "\nThe total of %d node pairs have been verified.\n", nPairs );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Checks the correctness of the functional simulation table.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_FeedBackCheckTableF0( Fraig_Man_t * p )
+{
+ Fraig_HashTable_t * pT = p->pTableF0;
+ Fraig_Node_t * pEntF;
+ int i, k, m, nPairs;
+
+ nPairs = 0;
+ for ( i = 0; i < pT->nBins; i++ )
+ {
+ p->vCones->nSize = 0;
+ Fraig_TableBinForEachEntryF( pT->pBins[i], pEntF )
+ Fraig_NodeVecPush( p->vCones, pEntF );
+ if ( p->vCones->nSize == 1 )
+ continue;
+ for ( k = 0; k < p->vCones->nSize; k++ )
+ for ( m = k+1; m < p->vCones->nSize; m++ )
+ {
+ if ( Fraig_CompareSimInfo( p->vCones->pArray[k], p->vCones->pArray[m], p->iWordStart, 0 ) )
+ printf( "Nodes %d and %d have the same D simulation info.\n",
+ p->vCones->pArray[k]->Num, p->vCones->pArray[m]->Num );
+ nPairs++;
+ }
+ }
+// printf( "\nThe total of %d node pairs have been verified.\n", nPairs );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Doubles the size of simulation info.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ReallocateSimulationInfo( Fraig_Man_t * p )
+{
+ Fraig_MemFixed_t * mmSimsNew; // new memory manager for simulation info
+ Fraig_Node_t * pNode;
+ unsigned * pSimsNew;
+ unsigned uSignOld;
+ int i;
+
+ // allocate a new memory manager
+ p->nWordsDyna *= 2;
+ mmSimsNew = Fraig_MemFixedStart( sizeof(unsigned) * (p->nWordsRand + p->nWordsDyna) );
+
+ // set the new data for the constant node
+ pNode = p->pConst1;
+ pNode->puSimR = (unsigned *)Fraig_MemFixedEntryFetch( mmSimsNew );
+ pNode->puSimD = pNode->puSimR + p->nWordsRand;
+ memset( pNode->puSimR, 0, sizeof(unsigned) * p->nWordsRand );
+ memset( pNode->puSimD, 0, sizeof(unsigned) * p->nWordsDyna );
+
+ // copy the simulation info of the PIs
+ for ( i = 0; i < p->vInputs->nSize; i++ )
+ {
+ pNode = p->vInputs->pArray[i];
+ // copy the simulation info
+ pSimsNew = (unsigned *)Fraig_MemFixedEntryFetch( mmSimsNew );
+ memmove( pSimsNew, pNode->puSimR, sizeof(unsigned) * (p->nWordsRand + p->iWordStart) );
+ // attach the new info
+ pNode->puSimR = pSimsNew;
+ pNode->puSimD = pNode->puSimR + p->nWordsRand;
+ // signatures remain without changes
+ }
+
+ // replace the manager to free up some memory
+ Fraig_MemFixedStop( p->mmSims, 0 );
+ p->mmSims = mmSimsNew;
+
+ // resimulate the internal nodes (this should lead to the same signatures)
+ for ( i = 1; i < p->vNodes->nSize; i++ )
+ {
+ pNode = p->vNodes->pArray[i];
+ if ( !Fraig_NodeIsAnd(pNode) )
+ continue;
+ // allocate memory for the simulation info
+ pNode->puSimR = (unsigned *)Fraig_MemFixedEntryFetch( mmSimsNew );
+ pNode->puSimD = pNode->puSimR + p->nWordsRand;
+ // derive random simulation info
+ uSignOld = pNode->uHashR;
+ pNode->uHashR = 0;
+ Fraig_NodeSimulate( pNode, 0, p->nWordsRand, 1 );
+ assert( uSignOld == pNode->uHashR );
+ // derive dynamic simulation info
+ uSignOld = pNode->uHashD;
+ pNode->uHashD = 0;
+ Fraig_NodeSimulate( pNode, 0, p->iWordStart, 0 );
+ assert( uSignOld == pNode->uHashD );
+ }
+
+ // realloc temporary storage
+ p->pSimsReal = (unsigned *)Fraig_MemFixedEntryFetch( mmSimsNew );
+ memset( p->pSimsReal, 0, sizeof(unsigned) * p->nWordsDyna );
+ p->pSimsTemp = (unsigned *)Fraig_MemFixedEntryFetch( mmSimsNew );
+ p->pSimsDiff = (unsigned *)Fraig_MemFixedEntryFetch( mmSimsNew );
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Generated trivial counter example.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int * Fraig_ManAllocCounterExample( Fraig_Man_t * p )
+{
+ int * pModel;
+ pModel = ALLOC( int, p->vInputs->nSize );
+ memset( pModel, 0, sizeof(int) * p->vInputs->nSize );
+ return pModel;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Saves the counter example.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_ManSimulateBitNode_rec( Fraig_Man_t * p, Fraig_Node_t * pNode )
+{
+ int Value0, Value1;
+ if ( Fraig_NodeIsTravIdCurrent( p, pNode ) )
+ return pNode->fMark3;
+ Fraig_NodeSetTravIdCurrent( p, pNode );
+ Value0 = Fraig_ManSimulateBitNode_rec( p, Fraig_Regular(pNode->p1) );
+ Value1 = Fraig_ManSimulateBitNode_rec( p, Fraig_Regular(pNode->p2) );
+ Value0 ^= Fraig_IsComplement(pNode->p1);
+ Value1 ^= Fraig_IsComplement(pNode->p2);
+ pNode->fMark3 = Value0 & Value1;
+ return pNode->fMark3;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Simulates one bit.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_ManSimulateBitNode( Fraig_Man_t * p, Fraig_Node_t * pNode, int * pModel )
+{
+ int fCompl, RetValue, i;
+ // set the PI values
+ Fraig_ManIncrementTravId( p );
+ for ( i = 0; i < p->vInputs->nSize; i++ )
+ {
+ Fraig_NodeSetTravIdCurrent( p, p->vInputs->pArray[i] );
+ p->vInputs->pArray[i]->fMark3 = pModel[i];
+ }
+ // perform the traversal
+ fCompl = Fraig_IsComplement(pNode);
+ RetValue = Fraig_ManSimulateBitNode_rec( p, Fraig_Regular(pNode) );
+ return fCompl ^ RetValue;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Saves the counter example.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int * Fraig_ManSaveCounterExample( Fraig_Man_t * p, Fraig_Node_t * pNode )
+{
+ int * pModel;
+ int iPattern;
+ int i, fCompl;
+
+ // the node can be complemented
+ fCompl = Fraig_IsComplement(pNode);
+ // because we compare with constant 0, p->pConst1 should also be complemented
+ fCompl = !fCompl;
+
+ // derive the model
+ pModel = Fraig_ManAllocCounterExample( p );
+ iPattern = Fraig_FindFirstDiff( p->pConst1, Fraig_Regular(pNode), fCompl, p->nWordsRand, 1 );
+ if ( iPattern >= 0 )
+ {
+ for ( i = 0; i < p->vInputs->nSize; i++ )
+ if ( Fraig_BitStringHasBit( p->vInputs->pArray[i]->puSimR, iPattern ) )
+ pModel[i] = 1;
+/*
+printf( "SAT solver's pattern:\n" );
+for ( i = 0; i < p->vInputs->nSize; i++ )
+ printf( "%d", pModel[i] );
+printf( "\n" );
+*/
+ assert( Fraig_ManSimulateBitNode( p, pNode, pModel ) );
+ return pModel;
+ }
+ iPattern = Fraig_FindFirstDiff( p->pConst1, Fraig_Regular(pNode), fCompl, p->iWordStart, 0 );
+ if ( iPattern >= 0 )
+ {
+ for ( i = 0; i < p->vInputs->nSize; i++ )
+ if ( Fraig_BitStringHasBit( p->vInputs->pArray[i]->puSimD, iPattern ) )
+ pModel[i] = 1;
+/*
+printf( "SAT solver's pattern:\n" );
+for ( i = 0; i < p->vInputs->nSize; i++ )
+ printf( "%d", pModel[i] );
+printf( "\n" );
+*/
+ assert( Fraig_ManSimulateBitNode( p, pNode, pModel ) );
+ return pModel;
+ }
+ FREE( pModel );
+ return NULL;
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/fraig/fraigInt.h b/src/sat/fraig/fraigInt.h
new file mode 100644
index 00000000..9c6e0d47
--- /dev/null
+++ b/src/sat/fraig/fraigInt.h
@@ -0,0 +1,451 @@
+/**CFile****************************************************************
+
+ FileName [fraigInt.h]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [Internal declarations of the FRAIG package.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigInt.h,v 1.15 2005/07/08 01:01:31 alanmi Exp $]
+
+***********************************************************************/
+
+#ifndef __FRAIG_INT_H__
+#define __FRAIG_INT_H__
+
+////////////////////////////////////////////////////////////////////////
+/// INCLUDES ///
+////////////////////////////////////////////////////////////////////////
+
+//#include "leaks.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+
+#include "fraig.h"
+#include "msat.h"
+
+////////////////////////////////////////////////////////////////////////
+/// PARAMETERS ///
+////////////////////////////////////////////////////////////////////////
+
+/*
+ The AIG node policy:
+ - Each node has its main number (pNode->Num)
+ This is the number of this node in the array of all nodes and its SAT variable number
+ - The PI nodes are stored along with other nodes
+ Additionally, PI nodes have a PI number, by which they are stored in the PI node array
+ - The constant node is has number 0 and is also stored in the array
+*/
+
+////////////////////////////////////////////////////////////////////////
+/// MACRO DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+// enable this macro to support the fanouts
+#define FRAIG_ENABLE_FANOUTS
+#define FRAIG_PATTERNS_RANDOM 2048 // should not be less than 128 and more than 32768 (2^15)
+#define FRAIG_PATTERNS_DYNAMIC 2048 // should not be less than 256 and more than 32768 (2^15)
+#define FRAIG_MAX_PRIMES 1024 // the maximum number of primes used for hashing
+
+// this parameter determines when simulation info is extended
+// it will be extended when the free storage in the dynamic simulation
+// info is less or equal to this number of words (FRAIG_WORDS_STORE)
+// this is done because if the free storage for dynamic simulation info
+// is not sufficient, computation becomes inefficient
+#define FRAIG_WORDS_STORE 5
+
+// the bit masks
+#define FRAIG_MASK(n) ((~((unsigned)0)) >> (32-(n)))
+#define FRAIG_FULL (~((unsigned)0))
+#define FRAIG_NUM_WORDS(n) (((n)>>5) + (((n)&31) > 0))
+
+// maximum/minimum operators
+#define FRAIG_MIN(a,b) (((a) < (b))? (a) : (b))
+#define FRAIG_MAX(a,b) (((a) > (b))? (a) : (b))
+
+// generating random unsigned (#define RAND_MAX 0x7fff)
+#define FRAIG_RANDOM_UNSIGNED ((((unsigned)rand()) << 24) ^ (((unsigned)rand()) << 12) ^ ((unsigned)rand()))
+
+// macros to get hold of the bits in a bit string
+#define Fraig_BitStringSetBit(p,i) ((p)[(i)>>5] |= (1<<((i) & 31)))
+#define Fraig_BitStringXorBit(p,i) ((p)[(i)>>5] ^= (1<<((i) & 31)))
+#define Fraig_BitStringHasBit(p,i) (((p)[(i)>>5] & (1<<((i) & 31))) > 0)
+
+// macros to get hold of the bits in the support info
+//#define Fraig_NodeSetVarStr(p,i) (Fraig_Regular(p)->pSuppStr[((i)%FRAIG_SUPP_SIGN)>>5] |= (1<<(((i)%FRAIG_SUPP_SIGN) & 31)))
+//#define Fraig_NodeHasVarStr(p,i) ((Fraig_Regular(p)->pSuppStr[((i)%FRAIG_SUPP_SIGN)>>5] & (1<<(((i)%FRAIG_SUPP_SIGN) & 31))) > 0)
+#define Fraig_NodeSetVarStr(p,i) Fraig_BitStringSetBit(Fraig_Regular(p)->pSuppStr,i)
+#define Fraig_NodeHasVarStr(p,i) Fraig_BitStringHasBit(Fraig_Regular(p)->pSuppStr,i)
+
+// copied from "util.h" for standaloneness
+#ifndef ALLOC
+# define ALLOC(type, num) \
+ ((type *) malloc(sizeof(type) * (num)))
+#endif
+
+#ifndef REALLOC
+# define REALLOC(type, obj, num) \
+ (obj) ? ((type *) realloc((char *) obj, sizeof(type) * (num))) : \
+ ((type *) malloc(sizeof(type) * (num)))
+#endif
+
+#ifndef FREE
+# define FREE(obj) \
+ ((obj) ? (free((char *) (obj)), (obj) = 0) : 0)
+#endif
+
+// copied from "extra.h" for stand-aloneness
+#define Fraig_PrintTime(a,t) printf( "%s = ", (a) ); printf( "%6.2f sec\n", (float)(t)/(float)(CLOCKS_PER_SEC) )
+
+#define Fraig_HashKey2(a,b,TSIZE) (((unsigned)(a) + (unsigned)(b) * 12582917) % TSIZE)
+//#define Fraig_HashKey2(a,b,TSIZE) (( ((unsigned)(a)->Num * 19) ^ ((unsigned)(b)->Num * 1999) ) % TSIZE)
+//#define Fraig_HashKey2(a,b,TSIZE) ( ((unsigned)((a)->Num + (b)->Num) * ((a)->Num + (b)->Num + 1) / 2) % TSIZE)
+// the other two hash functions give bad distribution of hash chain lengths (not clear why)
+
+#ifndef PRT
+#define PRT(a,t) printf( "%s = ", (a) ); printf( "%6.2f sec\n", (float)(t)/(float)(CLOCKS_PER_SEC) )
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// STRUCTURE DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+typedef struct Fraig_MemFixed_t_ Fraig_MemFixed_t;
+
+// the mapping manager
+struct Fraig_ManStruct_t_
+{
+ // the AIG nodes
+ Fraig_NodeVec_t * vInputs; // the array of primary inputs
+ Fraig_NodeVec_t * vNodes; // the array of all nodes, including primary inputs
+ Fraig_NodeVec_t * vOutputs; // the array of primary outputs (some internal nodes)
+ Fraig_Node_t * pConst1; // the pointer to the constant node (vNodes->pArray[0])
+
+ // info about the original circuit
+ char ** ppInputNames; // the primary input names
+ char ** ppOutputNames; // the primary output names
+
+ // various hash-tables
+ Fraig_HashTable_t * pTableS; // hashing by structure
+ Fraig_HashTable_t * pTableF; // hashing by simulation info
+ Fraig_HashTable_t * pTableF0; // hashing by simulation info (sparse functions)
+
+ // parameters
+ int nWordsRand; // the number of words of random simulation info
+ int nWordsDyna; // the number of words of dynamic simulation info
+ int nBTLimit; // the max number of backtracks to perform
+ int nSeconds; // the runtime limit for the miter proof
+ int fFuncRed; // performs only one level hashing
+ int fFeedBack; // enables solver feedback
+ int fDist1Pats; // enables solver feedback
+ int fDoSparse; // performs equiv tests for sparse functions
+ int fChoicing; // enables recording structural choices
+ int fTryProve; // tries to solve the final miter
+ int fVerbose; // the verbosiness flag
+ int fVerboseP; // the verbosiness flag
+ sint64 nInspLimit; // the inspection limit
+
+ int nTravIds; // the traversal counter
+ int nTravIds2; // the traversal counter
+
+ // info related to the solver feedback
+ int iWordStart; // the first word to use for simulation
+ int iWordPerm; // the number of words stored permanently
+ int iPatsPerm; // the number of patterns stored permanently
+ Fraig_NodeVec_t * vCones; // the temporary array of internal variables
+ Msat_IntVec_t * vPatsReal; // the array of real pattern numbers
+ unsigned * pSimsReal; // used for simulation patterns
+ unsigned * pSimsDiff; // used for simulation patterns
+ unsigned * pSimsTemp; // used for simulation patterns
+
+ // the support information
+ int nSuppWords;
+ unsigned ** pSuppS;
+ unsigned ** pSuppF;
+
+ // the memory managers
+ Fraig_MemFixed_t * mmNodes; // the memory manager for nodes
+ Fraig_MemFixed_t * mmSims; // the memory manager for simulation info
+
+ // solving the SAT problem
+ Msat_Solver_t * pSat; // the SAT solver
+ Msat_IntVec_t * vProj; // the temporary array of projection vars
+ int nSatNums; // the counter of SAT variables
+ int * pModel; // the assignment, which satisfies the miter
+ // these arrays belong to the solver
+ Msat_IntVec_t * vVarsInt; // the temporary array of variables
+ Msat_ClauseVec_t * vAdjacents; // the temporary storage for connectivity
+ Msat_IntVec_t * vVarsUsed; // the array marking vars appearing in the cone
+
+ // various statistic variables
+ int nSatCalls; // the number of times equivalence checking was called
+ int nSatProof; // the number of times a proof was found
+ int nSatCounter; // the number of times a counter example was found
+ int nSatFails; // the number of times the SAT solver failed to complete due to resource limit or prediction
+ int nSatFailsReal; // the number of times the SAT solver failed to complete due to resource limit
+
+ int nSatCallsImp; // the number of times equivalence checking was called
+ int nSatProofImp; // the number of times a proof was found
+ int nSatCounterImp;// the number of times a counter example was found
+ int nSatFailsImp; // the number of times the SAT solver failed to complete
+
+ int nSatZeros; // the number of times the simulation vector is zero
+ int nSatSupps; // the number of times the support info was useful
+ int nRefErrors; // the number of ref counting errors
+ int nImplies; // the number of implication cases
+ int nSatImpls; // the number of implication SAT calls
+ int nVarsClauses; // the number of variables with clauses
+ int nSimplifies0;
+ int nSimplifies1;
+ int nImplies0;
+ int nImplies1;
+
+ // runtime statistics
+ int timeToAig; // time to transfer to the mapping structure
+ int timeSims; // time to compute k-feasible cuts
+ int timeTrav; // time to traverse the network
+ int timeFeed; // time for solver feedback (recording and resimulating)
+ int timeImply; // time to analyze implications
+ int timeSat; // time to compute the truth table for each cut
+ int timeToNet; // time to transfer back to the network
+ int timeTotal; // the total mapping time
+ int time1; // time to perform one task
+ int time2; // time to perform another task
+ int time3; // time to perform another task
+ int time4; // time to perform another task
+};
+
+// the mapping node
+struct Fraig_NodeStruct_t_
+{
+ // various numbers associated with the node
+ int Num; // the unique number (SAT var number) of this node
+ int NumPi; // if the node is a PI, this is its variable number
+ int Level; // the level of the node
+ int nRefs; // the number of references of the node
+ int TravId; // the traversal ID (use to avoid cleaning marks)
+ int TravId2; // the traversal ID (use to avoid cleaning marks)
+
+ // general information about the node
+ unsigned fInv : 1; // the mark to show that simulation info is complemented
+ unsigned fNodePo : 1; // the mark used for primary outputs
+ unsigned fClauses : 1; // the clauses for this node are loaded
+ unsigned fMark0 : 1; // the mark used for traversals
+ unsigned fMark1 : 1; // the mark used for traversals
+ unsigned fMark2 : 1; // the mark used for traversals
+ unsigned fMark3 : 1; // the mark used for traversals
+ unsigned fFeedUse : 1; // the presence of the variable in the feedback
+ unsigned fFeedVal : 1; // the value of the variable in the feedback
+ unsigned fFailTfo : 1; // the node is in the TFO of the failed SAT run
+ unsigned nFanouts : 2; // the indicator of fanouts (none, one, or many)
+ unsigned nOnes : 20; // the number of 1's in the random sim info
+
+ // the children of the node
+ Fraig_Node_t * p1; // the first child
+ Fraig_Node_t * p2; // the second child
+ Fraig_NodeVec_t * vFanins; // the fanins of the supergate rooted at this node
+// Fraig_NodeVec_t * vFanouts; // the fanouts of the supergate rooted at this node
+
+ // various linked lists
+ Fraig_Node_t * pNextS; // the next node in the structural hash table
+ Fraig_Node_t * pNextF; // the next node in the functional (simulation) hash table
+ Fraig_Node_t * pNextD; // the next node in the list of nodes based on dynamic simulation
+ Fraig_Node_t * pNextE; // the next structural choice (functionally-equivalent node)
+ Fraig_Node_t * pRepr; // the canonical functional representative of the node
+
+ // simulation data
+ unsigned uHashR; // the hash value for random information
+ unsigned uHashD; // the hash value for dynamic information
+ unsigned * puSimR; // the simulation information (random)
+ unsigned * puSimD; // the simulation information (dynamic)
+
+ // misc information
+ Fraig_Node_t * pData0; // temporary storage for the corresponding network node
+ Fraig_Node_t * pData1; // temporary storage for the corresponding network node
+
+#ifdef FRAIG_ENABLE_FANOUTS
+ // representation of node's fanouts
+ Fraig_Node_t * pFanPivot; // the first fanout of this node
+ Fraig_Node_t * pFanFanin1; // the next fanout of p1
+ Fraig_Node_t * pFanFanin2; // the next fanout of p2
+#endif
+};
+
+// the vector of nodes
+struct Fraig_NodeVecStruct_t_
+{
+ int nCap; // the number of allocated entries
+ int nSize; // the number of entries in the array
+ Fraig_Node_t ** pArray; // the array of nodes
+};
+
+// the hash table
+struct Fraig_HashTableStruct_t_
+{
+ Fraig_Node_t ** pBins; // the table bins
+ int nBins; // the size of the table
+ int nEntries; // the total number of entries in the table
+};
+
+// getting hold of the next fanout of the node
+#define Fraig_NodeReadNextFanout( pNode, pFanout ) \
+ ( ( pFanout == NULL )? NULL : \
+ ((Fraig_Regular((pFanout)->p1) == (pNode))? \
+ (pFanout)->pFanFanin1 : (pFanout)->pFanFanin2) )
+// getting hold of the place where the next fanout will be attached
+#define Fraig_NodeReadNextFanoutPlace( pNode, pFanout ) \
+ ( (Fraig_Regular((pFanout)->p1) == (pNode))? \
+ &(pFanout)->pFanFanin1 : &(pFanout)->pFanFanin2 )
+// iterator through the fanouts of the node
+#define Fraig_NodeForEachFanout( pNode, pFanout ) \
+ for ( pFanout = (pNode)->pFanPivot; pFanout; \
+ pFanout = Fraig_NodeReadNextFanout(pNode, pFanout) )
+// safe iterator through the fanouts of the node
+#define Fraig_NodeForEachFanoutSafe( pNode, pFanout, pFanout2 ) \
+ for ( pFanout = (pNode)->pFanPivot, \
+ pFanout2 = Fraig_NodeReadNextFanout(pNode, pFanout); \
+ pFanout; \
+ pFanout = pFanout2, \
+ pFanout2 = Fraig_NodeReadNextFanout(pNode, pFanout) )
+
+// iterators through the entries in the linked lists of nodes
+// the list of nodes in the structural hash table
+#define Fraig_TableBinForEachEntryS( pBin, pEnt ) \
+ for ( pEnt = pBin; \
+ pEnt; \
+ pEnt = pEnt->pNextS )
+#define Fraig_TableBinForEachEntrySafeS( pBin, pEnt, pEnt2 ) \
+ for ( pEnt = pBin, \
+ pEnt2 = pEnt? pEnt->pNextS: NULL; \
+ pEnt; \
+ pEnt = pEnt2, \
+ pEnt2 = pEnt? pEnt->pNextS: NULL )
+// the list of nodes in the functional (simulation) hash table
+#define Fraig_TableBinForEachEntryF( pBin, pEnt ) \
+ for ( pEnt = pBin; \
+ pEnt; \
+ pEnt = pEnt->pNextF )
+#define Fraig_TableBinForEachEntrySafeF( pBin, pEnt, pEnt2 ) \
+ for ( pEnt = pBin, \
+ pEnt2 = pEnt? pEnt->pNextF: NULL; \
+ pEnt; \
+ pEnt = pEnt2, \
+ pEnt2 = pEnt? pEnt->pNextF: NULL )
+// the list of nodes with the same simulation and different functionality
+#define Fraig_TableBinForEachEntryD( pBin, pEnt ) \
+ for ( pEnt = pBin; \
+ pEnt; \
+ pEnt = pEnt->pNextD )
+#define Fraig_TableBinForEachEntrySafeD( pBin, pEnt, pEnt2 ) \
+ for ( pEnt = pBin, \
+ pEnt2 = pEnt? pEnt->pNextD: NULL; \
+ pEnt; \
+ pEnt = pEnt2, \
+ pEnt2 = pEnt? pEnt->pNextD: NULL )
+// the list of nodes with the same functionality
+#define Fraig_TableBinForEachEntryE( pBin, pEnt ) \
+ for ( pEnt = pBin; \
+ pEnt; \
+ pEnt = pEnt->pNextE )
+#define Fraig_TableBinForEachEntrySafeE( pBin, pEnt, pEnt2 ) \
+ for ( pEnt = pBin, \
+ pEnt2 = pEnt? pEnt->pNextE: NULL; \
+ pEnt; \
+ pEnt = pEnt2, \
+ pEnt2 = pEnt? pEnt->pNextE: NULL )
+
+////////////////////////////////////////////////////////////////////////
+/// GLOBAL VARIABLES ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/*=== fraigCanon.c =============================================================*/
+extern Fraig_Node_t * Fraig_NodeAndCanon( Fraig_Man_t * pMan, Fraig_Node_t * p1, Fraig_Node_t * p2 );
+/*=== fraigFanout.c =============================================================*/
+extern void Fraig_NodeAddFaninFanout( Fraig_Node_t * pFanin, Fraig_Node_t * pFanout );
+extern void Fraig_NodeRemoveFaninFanout( Fraig_Node_t * pFanin, Fraig_Node_t * pFanoutToRemove );
+extern int Fraig_NodeGetFanoutNum( Fraig_Node_t * pNode );
+/*=== fraigFeed.c =============================================================*/
+extern void Fraig_FeedBackInit( Fraig_Man_t * p );
+extern void Fraig_FeedBack( Fraig_Man_t * p, int * pModel, Msat_IntVec_t * vVars, Fraig_Node_t * pOld, Fraig_Node_t * pNew );
+extern void Fraig_FeedBackTest( Fraig_Man_t * p );
+extern int Fraig_FeedBackCompress( Fraig_Man_t * p );
+extern int * Fraig_ManAllocCounterExample( Fraig_Man_t * p );
+extern int * Fraig_ManSaveCounterExample( Fraig_Man_t * p, Fraig_Node_t * pNode );
+/*=== fraigMan.c =============================================================*/
+extern void Fraig_ManCreateSolver( Fraig_Man_t * p );
+/*=== fraigMem.c =============================================================*/
+extern Fraig_MemFixed_t * Fraig_MemFixedStart( int nEntrySize );
+extern void Fraig_MemFixedStop( Fraig_MemFixed_t * p, int fVerbose );
+extern char * Fraig_MemFixedEntryFetch( Fraig_MemFixed_t * p );
+extern void Fraig_MemFixedEntryRecycle( Fraig_MemFixed_t * p, char * pEntry );
+extern void Fraig_MemFixedRestart( Fraig_MemFixed_t * p );
+extern int Fraig_MemFixedReadMemUsage( Fraig_MemFixed_t * p );
+/*=== fraigNode.c =============================================================*/
+extern Fraig_Node_t * Fraig_NodeCreateConst( Fraig_Man_t * p );
+extern Fraig_Node_t * Fraig_NodeCreatePi( Fraig_Man_t * p );
+extern Fraig_Node_t * Fraig_NodeCreate( Fraig_Man_t * p, Fraig_Node_t * p1, Fraig_Node_t * p2 );
+extern void Fraig_NodeSimulate( Fraig_Node_t * pNode, int iWordStart, int iWordStop, int fUseRand );
+/*=== fraigPrime.c =============================================================*/
+extern int s_FraigPrimes[FRAIG_MAX_PRIMES];
+extern unsigned int Cudd_PrimeFraig( unsigned int p );
+/*=== fraigSat.c ===============================================================*/
+extern int Fraig_NodeIsImplication( Fraig_Man_t * p, Fraig_Node_t * pOld, Fraig_Node_t * pNew, int nBTLimit );
+/*=== fraigTable.c =============================================================*/
+extern Fraig_HashTable_t * Fraig_HashTableCreate( int nSize );
+extern void Fraig_HashTableFree( Fraig_HashTable_t * p );
+extern int Fraig_HashTableLookupS( Fraig_Man_t * pMan, Fraig_Node_t * p1, Fraig_Node_t * p2, Fraig_Node_t ** ppNodeRes );
+extern Fraig_Node_t * Fraig_HashTableLookupF( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
+extern Fraig_Node_t * Fraig_HashTableLookupF0( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
+extern void Fraig_HashTableInsertF0( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
+extern int Fraig_CompareSimInfo( Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int iWordLast, int fUseRand );
+extern int Fraig_CompareSimInfoUnderMask( Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int iWordLast, int fUseRand, unsigned * puMask );
+extern int Fraig_FindFirstDiff( Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int fCompl, int iWordLast, int fUseRand );
+extern void Fraig_CollectXors( Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int iWordLast, int fUseRand, unsigned * puMask );
+extern void Fraig_TablePrintStatsS( Fraig_Man_t * pMan );
+extern void Fraig_TablePrintStatsF( Fraig_Man_t * pMan );
+extern void Fraig_TablePrintStatsF0( Fraig_Man_t * pMan );
+extern int Fraig_TableRehashF0( Fraig_Man_t * pMan, int fLinkEquiv );
+/*=== fraigUtil.c ===============================================================*/
+extern int Fraig_NodeCountPis( Msat_IntVec_t * vVars, int nVarsPi );
+extern int Fraig_NodeCountSuppVars( Fraig_Man_t * p, Fraig_Node_t * pNode, int fSuppStr );
+extern int Fraig_NodesCompareSupps( Fraig_Man_t * p, Fraig_Node_t * pOld, Fraig_Node_t * pNew );
+extern int Fraig_NodeAndSimpleCase_rec( Fraig_Node_t * pOld, Fraig_Node_t * pNew );
+extern int Fraig_NodeIsExorType( Fraig_Node_t * pNode );
+extern void Fraig_ManSelectBestChoice( Fraig_Man_t * p );
+extern int Fraig_BitStringCountOnes( unsigned * pString, int nWords );
+extern void Fraig_PrintBinary( FILE * pFile, unsigned * pSign, int nBits );
+extern int Fraig_NodeIsExorType( Fraig_Node_t * pNode );
+extern int Fraig_NodeIsExor( Fraig_Node_t * pNode );
+extern int Fraig_NodeIsMuxType( Fraig_Node_t * pNode );
+extern Fraig_Node_t * Fraig_NodeRecognizeMux( Fraig_Node_t * pNode, Fraig_Node_t ** ppNodeT, Fraig_Node_t ** ppNodeE );
+extern int Fraig_ManCountExors( Fraig_Man_t * pMan );
+extern int Fraig_ManCountMuxes( Fraig_Man_t * pMan );
+extern int Fraig_NodeSimsContained( Fraig_Man_t * pMan, Fraig_Node_t * pNode1, Fraig_Node_t * pNode2 );
+extern int Fraig_NodeIsInSupergate( Fraig_Node_t * pOld, Fraig_Node_t * pNew );
+extern Fraig_NodeVec_t * Fraig_CollectSupergate( Fraig_Node_t * pNode, int fStopAtMux );
+extern int Fraig_CountPis( Fraig_Man_t * p, Msat_IntVec_t * vVarNums );
+extern void Fraig_ManIncrementTravId( Fraig_Man_t * pMan );
+extern void Fraig_NodeSetTravIdCurrent( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
+extern int Fraig_NodeIsTravIdCurrent( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
+extern int Fraig_NodeIsTravIdPrevious( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
+/*=== fraigVec.c ===============================================================*/
+extern void Fraig_NodeVecSortByRefCount( Fraig_NodeVec_t * p );
+
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
diff --git a/src/sat/fraig/fraigMan.c b/src/sat/fraig/fraigMan.c
new file mode 100644
index 00000000..7fd937d5
--- /dev/null
+++ b/src/sat/fraig/fraigMan.c
@@ -0,0 +1,540 @@
+/**CFile****************************************************************
+
+ FileName [fraigMan.c]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [Implementation of the FRAIG manager.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigMan.c,v 1.11 2005/07/08 01:01:31 alanmi Exp $]
+
+***********************************************************************/
+
+#include "fraigInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+int timeSelect;
+int timeAssign;
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Sets the default parameters of the package.]
+
+ Description [This set of parameters is tuned for equivalence checking.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Prove_ParamsSetDefault( Prove_Params_t * pParams )
+{
+ // clean the parameter structure
+ memset( pParams, 0, sizeof(Prove_Params_t) );
+ // general parameters
+ pParams->fUseFraiging = 1; // enables fraiging
+ pParams->fUseRewriting = 1; // enables rewriting
+ pParams->fUseBdds = 0; // enables BDD construction when other methods fail
+ pParams->fVerbose = 0; // prints verbose stats
+ // iterations
+ pParams->nItersMax = 6; // the number of iterations
+ // mitering
+ pParams->nMiteringLimitStart = 300; // starting mitering limit
+ pParams->nMiteringLimitMulti = 2.0; // multiplicative coefficient to increase the limit in each iteration
+ // rewriting (currently not used)
+ pParams->nRewritingLimitStart = 3; // the number of rewriting iterations
+ pParams->nRewritingLimitMulti = 1.0; // multiplicative coefficient to increase the limit in each iteration
+ // fraiging
+ pParams->nFraigingLimitStart = 2; // starting backtrack(conflict) limit
+ pParams->nFraigingLimitMulti = 8.0; // multiplicative coefficient to increase the limit in each iteration
+ // last-gasp BDD construction
+ pParams->nBddSizeLimit = 1000000; // the number of BDD nodes when construction is aborted
+ pParams->fBddReorder = 1; // enables dynamic BDD variable reordering
+ // last-gasp mitering
+// pParams->nMiteringLimitLast = 1000000; // final mitering limit
+ pParams->nMiteringLimitLast = 0; // final mitering limit
+ // global SAT solver limits
+ pParams->nTotalBacktrackLimit = 0; // global limit on the number of backtracks
+ pParams->nTotalInspectLimit = 0; // global limit on the number of clause inspects
+// pParams->nTotalInspectLimit = 100000000; // global limit on the number of clause inspects
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prints out the current values of CEC engine parameters.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Prove_ParamsPrint( Prove_Params_t * pParams )
+{
+ printf( "CEC enging parameters:\n" );
+ printf( "Fraiging enabled: %s\n", pParams->fUseFraiging? "yes":"no" );
+ printf( "Rewriting enabled: %s\n", pParams->fUseRewriting? "yes":"no" );
+ printf( "BDD construction enabled: %s\n", pParams->fUseBdds? "yes":"no" );
+ printf( "Verbose output enabled: %s\n", pParams->fVerbose? "yes":"no" );
+ printf( "Solver iterations: %d\n", pParams->nItersMax );
+ printf( "Starting mitering limit: %d\n", pParams->nMiteringLimitStart );
+ printf( "Multiplicative coeficient for mitering: %.2f\n", pParams->nMiteringLimitMulti );
+ printf( "Starting number of rewriting iterations: %d\n", pParams->nRewritingLimitStart );
+ printf( "Multiplicative coeficient for rewriting: %.2f\n", pParams->nRewritingLimitMulti );
+ printf( "Starting number of conflicts in fraiging: %d\n", pParams->nFraigingLimitMulti );
+ printf( "Multiplicative coeficient for fraiging: %.2f\n", pParams->nRewritingLimitMulti );
+ printf( "BDD size limit for bailing out: %.2f\n", pParams->nBddSizeLimit );
+ printf( "BDD reordering enabled: %s\n", pParams->fBddReorder? "yes":"no" );
+ printf( "Last-gasp mitering limit: %d\n", pParams->nMiteringLimitLast );
+ printf( "Total conflict limit: %d\n", pParams->nTotalBacktrackLimit );
+ printf( "Total inspection limit: %d\n", pParams->nTotalInspectLimit );
+ printf( "Parameter dump complete.\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sets the default parameters of the package.]
+
+ Description [This set of parameters is tuned for equivalence checking.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ParamsSetDefault( Fraig_Params_t * pParams )
+{
+ memset( pParams, 0, sizeof(Fraig_Params_t) );
+ pParams->nPatsRand = FRAIG_PATTERNS_RANDOM; // the number of words of random simulation info
+ pParams->nPatsDyna = FRAIG_PATTERNS_DYNAMIC; // the number of words of dynamic simulation info
+ pParams->nBTLimit = 99; // the max number of backtracks to perform
+ pParams->nSeconds = 20; // the max number of seconds to solve the miter
+ pParams->fFuncRed = 1; // performs only one level hashing
+ pParams->fFeedBack = 1; // enables solver feedback
+ pParams->fDist1Pats = 1; // enables distance-1 patterns
+ pParams->fDoSparse = 0; // performs equiv tests for sparse functions
+ pParams->fChoicing = 0; // enables recording structural choices
+ pParams->fTryProve = 1; // tries to solve the final miter
+ pParams->fVerbose = 0; // the verbosiness flag
+ pParams->fVerboseP = 0; // the verbose flag for reporting the proof
+ pParams->fInternal = 0; // the flag indicates the internal run
+ pParams->nConfLimit = 0; // the limit on the number of conflicts
+ pParams->nInspLimit = 0; // the limit on the number of inspections
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sets the default parameters of the package.]
+
+ Description [This set of parameters is tuned for complete FRAIGing.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ParamsSetDefaultFull( Fraig_Params_t * pParams )
+{
+ memset( pParams, 0, sizeof(Fraig_Params_t) );
+ pParams->nPatsRand = FRAIG_PATTERNS_RANDOM; // the number of words of random simulation info
+ pParams->nPatsDyna = FRAIG_PATTERNS_DYNAMIC; // the number of words of dynamic simulation info
+ pParams->nBTLimit = -1; // the max number of backtracks to perform
+ pParams->nSeconds = 20; // the max number of seconds to solve the miter
+ pParams->fFuncRed = 1; // performs only one level hashing
+ pParams->fFeedBack = 1; // enables solver feedback
+ pParams->fDist1Pats = 1; // enables distance-1 patterns
+ pParams->fDoSparse = 1; // performs equiv tests for sparse functions
+ pParams->fChoicing = 0; // enables recording structural choices
+ pParams->fTryProve = 0; // tries to solve the final miter
+ pParams->fVerbose = 0; // the verbosiness flag
+ pParams->fVerboseP = 0; // the verbose flag for reporting the proof
+ pParams->fInternal = 0; // the flag indicates the internal run
+ pParams->nConfLimit = 0; // the limit on the number of conflicts
+ pParams->nInspLimit = 0; // the limit on the number of inspections
+}
+
+/**Function*************************************************************
+
+ Synopsis [Creates the new FRAIG manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Man_t * Fraig_ManCreate( Fraig_Params_t * pParams )
+{
+ Fraig_Params_t Params;
+ Fraig_Man_t * p;
+
+ // set the random seed for simulation
+// srand( 0xFEEDDEAF );
+ srand( 0xDEADCAFE );
+
+ // set parameters for equivalence checking
+ if ( pParams == NULL )
+ Fraig_ParamsSetDefault( pParams = &Params );
+ // adjust the amount of simulation info
+ if ( pParams->nPatsRand < 128 )
+ pParams->nPatsRand = 128;
+ if ( pParams->nPatsRand > 32768 )
+ pParams->nPatsRand = 32768;
+ if ( pParams->nPatsDyna < 128 )
+ pParams->nPatsDyna = 128;
+ if ( pParams->nPatsDyna > 32768 )
+ pParams->nPatsDyna = 32768;
+ // if reduction is not performed, allocate minimum simulation info
+ if ( !pParams->fFuncRed )
+ pParams->nPatsRand = pParams->nPatsDyna = 128;
+
+ // start the manager
+ p = ALLOC( Fraig_Man_t, 1 );
+ memset( p, 0, sizeof(Fraig_Man_t) );
+
+ // set the default parameters
+ p->nWordsRand = FRAIG_NUM_WORDS( pParams->nPatsRand ); // the number of words of random simulation info
+ p->nWordsDyna = FRAIG_NUM_WORDS( pParams->nPatsDyna ); // the number of patterns for dynamic simulation info
+ p->nBTLimit = pParams->nBTLimit; // -1 means infinite backtrack limit
+ p->nSeconds = pParams->nSeconds; // the timeout for the final miter
+ p->fFuncRed = pParams->fFuncRed; // enables functional reduction (otherwise, only one-level hashing is performed)
+ p->fFeedBack = pParams->fFeedBack; // enables solver feedback (the use of counter-examples in simulation)
+ p->fDist1Pats = pParams->fDist1Pats; // enables solver feedback (the use of counter-examples in simulation)
+ p->fDoSparse = pParams->fDoSparse; // performs equivalence checking for sparse functions (whose sim-info is 0)
+ p->fChoicing = pParams->fChoicing; // disable accumulation of structural choices (keeps only the first choice)
+ p->fTryProve = pParams->fTryProve; // disable accumulation of structural choices (keeps only the first choice)
+ p->fVerbose = pParams->fVerbose; // disable verbose output
+ p->fVerboseP = pParams->fVerboseP; // disable verbose output
+ p->nInspLimit = pParams->nInspLimit; // the limit on the number of inspections
+
+ // start memory managers
+ p->mmNodes = Fraig_MemFixedStart( sizeof(Fraig_Node_t) );
+ p->mmSims = Fraig_MemFixedStart( sizeof(unsigned) * (p->nWordsRand + p->nWordsDyna) );
+ // allocate node arrays
+ p->vInputs = Fraig_NodeVecAlloc( 1000 ); // the array of primary inputs
+ p->vOutputs = Fraig_NodeVecAlloc( 1000 ); // the array of primary outputs
+ p->vNodes = Fraig_NodeVecAlloc( 1000 ); // the array of internal nodes
+ // start the tables
+ p->pTableS = Fraig_HashTableCreate( 1000 ); // hashing by structure
+ p->pTableF = Fraig_HashTableCreate( 1000 ); // hashing by function
+ p->pTableF0 = Fraig_HashTableCreate( 1000 ); // hashing by function (for sparse functions)
+ // create the constant node
+ p->pConst1 = Fraig_NodeCreateConst( p );
+ // initialize SAT solver feedback data structures
+ Fraig_FeedBackInit( p );
+ // initialize other variables
+ p->vProj = Msat_IntVecAlloc( 10 );
+ p->nTravIds = 1;
+ p->nTravIds2 = 1;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Deallocates the mapping manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ManFree( Fraig_Man_t * p )
+{
+ int i;
+ if ( p->fVerbose )
+ {
+ if ( p->fChoicing ) Fraig_ManReportChoices( p );
+ Fraig_ManPrintStats( p );
+// Fraig_TablePrintStatsS( p );
+// Fraig_TablePrintStatsF( p );
+// Fraig_TablePrintStatsF0( p );
+ }
+
+ for ( i = 0; i < p->vNodes->nSize; i++ )
+ if ( p->vNodes->pArray[i]->vFanins )
+ {
+ Fraig_NodeVecFree( p->vNodes->pArray[i]->vFanins );
+ p->vNodes->pArray[i]->vFanins = NULL;
+ }
+
+ if ( p->vInputs ) Fraig_NodeVecFree( p->vInputs );
+ if ( p->vNodes ) Fraig_NodeVecFree( p->vNodes );
+ if ( p->vOutputs ) Fraig_NodeVecFree( p->vOutputs );
+
+ if ( p->pTableS ) Fraig_HashTableFree( p->pTableS );
+ if ( p->pTableF ) Fraig_HashTableFree( p->pTableF );
+ if ( p->pTableF0 ) Fraig_HashTableFree( p->pTableF0 );
+
+ if ( p->pSat ) Msat_SolverFree( p->pSat );
+ if ( p->vProj ) Msat_IntVecFree( p->vProj );
+ if ( p->vCones ) Fraig_NodeVecFree( p->vCones );
+ if ( p->vPatsReal ) Msat_IntVecFree( p->vPatsReal );
+ if ( p->pModel ) free( p->pModel );
+
+ Fraig_MemFixedStop( p->mmNodes, 0 );
+ Fraig_MemFixedStop( p->mmSims, 0 );
+
+ if ( p->pSuppS )
+ {
+ FREE( p->pSuppS[0] );
+ FREE( p->pSuppS );
+ }
+ if ( p->pSuppF )
+ {
+ FREE( p->pSuppF[0] );
+ FREE( p->pSuppF );
+ }
+
+ FREE( p->ppOutputNames );
+ FREE( p->ppInputNames );
+ FREE( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prepares the SAT solver to run on the two nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ManCreateSolver( Fraig_Man_t * p )
+{
+ extern int timeSelect;
+ extern int timeAssign;
+ assert( p->pSat == NULL );
+ // allocate data for SAT solving
+ p->pSat = Msat_SolverAlloc( 500, 1, 1, 1, 1, 0 );
+ p->vVarsInt = Msat_SolverReadConeVars( p->pSat );
+ p->vAdjacents = Msat_SolverReadAdjacents( p->pSat );
+ p->vVarsUsed = Msat_SolverReadVarsUsed( p->pSat );
+ timeSelect = 0;
+ timeAssign = 0;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Deallocates the mapping manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ManPrintStats( Fraig_Man_t * p )
+{
+ double nMemory;
+ int clk = clock();
+ nMemory = ((double)(p->vInputs->nSize + p->vNodes->nSize) *
+ (sizeof(Fraig_Node_t) + sizeof(unsigned)*(p->nWordsRand + p->nWordsDyna) /*+ p->nSuppWords*sizeof(unsigned)*/))/(1<<20);
+ printf( "Words: Random = %d. Dynamic = %d. Used = %d. Memory = %0.2f Mb.\n",
+ p->nWordsRand, p->nWordsDyna, p->iWordPerm, nMemory );
+ printf( "Proof = %d. Counter-example = %d. Fail = %d. FailReal = %d. Zero = %d.\n",
+ p->nSatProof, p->nSatCounter, p->nSatFails, p->nSatFailsReal, p->nSatZeros );
+ printf( "Nodes: Final = %d. Total = %d. Mux = %d. (Exor = %d.) ClaVars = %d.\n",
+ Fraig_CountNodes(p,0), p->vNodes->nSize, Fraig_ManCountMuxes(p), Fraig_ManCountExors(p), p->nVarsClauses );
+ if ( p->pSat ) Msat_SolverPrintStats( p->pSat );
+ Fraig_PrintTime( "AIG simulation ", p->timeSims );
+ Fraig_PrintTime( "AIG traversal ", p->timeTrav );
+ Fraig_PrintTime( "Solver feedback ", p->timeFeed );
+ Fraig_PrintTime( "SAT solving ", p->timeSat );
+ Fraig_PrintTime( "Network update ", p->timeToNet );
+ Fraig_PrintTime( "TOTAL RUNTIME ", p->timeTotal );
+ if ( p->time1 > 0 ) { Fraig_PrintTime( "time1", p->time1 ); }
+ if ( p->time2 > 0 ) { Fraig_PrintTime( "time2", p->time2 ); }
+ if ( p->time3 > 0 ) { Fraig_PrintTime( "time3", p->time3 ); }
+ if ( p->time4 > 0 ) { Fraig_PrintTime( "time4", p->time4 ); }
+// PRT( "Selection ", timeSelect );
+// PRT( "Assignment", timeAssign );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Allocates simulation information for all nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_NodeVec_t * Fraig_UtilInfoAlloc( int nSize, int nWords, bool fClean )
+{
+ Fraig_NodeVec_t * vInfo;
+ unsigned * pUnsigned;
+ int i;
+ assert( nSize > 0 && nWords > 0 );
+ vInfo = Fraig_NodeVecAlloc( nSize );
+ pUnsigned = ALLOC( unsigned, nSize * nWords );
+ vInfo->pArray[0] = (Fraig_Node_t *)pUnsigned;
+ if ( fClean )
+ memset( pUnsigned, 0, sizeof(unsigned) * nSize * nWords );
+ for ( i = 1; i < nSize; i++ )
+ vInfo->pArray[i] = (Fraig_Node_t *)(((unsigned *)vInfo->pArray[i-1]) + nWords);
+ vInfo->nSize = nSize;
+ return vInfo;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns simulation info of all nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_NodeVec_t * Fraig_ManGetSimInfo( Fraig_Man_t * p )
+{
+ Fraig_NodeVec_t * vInfo;
+ Fraig_Node_t * pNode;
+ unsigned * pUnsigned;
+ int nRandom, nDynamic;
+ int i, k, nWords;
+
+ nRandom = Fraig_ManReadPatternNumRandom( p );
+ nDynamic = Fraig_ManReadPatternNumDynamic( p );
+ nWords = nRandom / 32 + nDynamic / 32;
+
+ vInfo = Fraig_UtilInfoAlloc( p->vNodes->nSize, nWords, 0 );
+ for ( i = 0; i < p->vNodes->nSize; i++ )
+ {
+ pNode = p->vNodes->pArray[i];
+ assert( i == pNode->Num );
+ pUnsigned = (unsigned *)vInfo->pArray[i];
+ for ( k = 0; k < nRandom / 32; k++ )
+ pUnsigned[k] = pNode->puSimR[k];
+ for ( k = 0; k < nDynamic / 32; k++ )
+ pUnsigned[nRandom / 32 + k] = pNode->puSimD[k];
+ }
+ return vInfo;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns 1 if A v B is always true based on the siminfo.]
+
+ Description [A v B is always true iff A' * B' is always false.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_ManCheckClauseUsingSimInfo( Fraig_Man_t * p, Fraig_Node_t * pNode1, Fraig_Node_t * pNode2 )
+{
+ int fCompl1, fCompl2, i;
+
+ fCompl1 = 1 ^ Fraig_IsComplement(pNode1) ^ Fraig_Regular(pNode1)->fInv;
+ fCompl2 = 1 ^ Fraig_IsComplement(pNode2) ^ Fraig_Regular(pNode2)->fInv;
+
+ pNode1 = Fraig_Regular(pNode1);
+ pNode2 = Fraig_Regular(pNode2);
+ assert( pNode1 != pNode2 );
+
+ // check the simulation info
+ if ( fCompl1 && fCompl2 )
+ {
+ for ( i = 0; i < p->nWordsRand; i++ )
+ if ( ~pNode1->puSimR[i] & ~pNode2->puSimR[i] )
+ return 0;
+ for ( i = 0; i < p->iWordStart; i++ )
+ if ( ~pNode1->puSimD[i] & ~pNode2->puSimD[i] )
+ return 0;
+ return 1;
+ }
+ if ( !fCompl1 && fCompl2 )
+ {
+ for ( i = 0; i < p->nWordsRand; i++ )
+ if ( pNode1->puSimR[i] & ~pNode2->puSimR[i] )
+ return 0;
+ for ( i = 0; i < p->iWordStart; i++ )
+ if ( pNode1->puSimD[i] & ~pNode2->puSimD[i] )
+ return 0;
+ return 1;
+ }
+ if ( fCompl1 && !fCompl2 )
+ {
+ for ( i = 0; i < p->nWordsRand; i++ )
+ if ( ~pNode1->puSimR[i] & pNode2->puSimR[i] )
+ return 0;
+ for ( i = 0; i < p->iWordStart; i++ )
+ if ( ~pNode1->puSimD[i] & pNode2->puSimD[i] )
+ return 0;
+ return 1;
+ }
+// if ( fCompl1 && fCompl2 )
+ {
+ for ( i = 0; i < p->nWordsRand; i++ )
+ if ( pNode1->puSimR[i] & pNode2->puSimR[i] )
+ return 0;
+ for ( i = 0; i < p->iWordStart; i++ )
+ if ( pNode1->puSimD[i] & pNode2->puSimD[i] )
+ return 0;
+ return 1;
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Adds clauses to the solver.]
+
+ Description [This procedure is used to add external clauses to the solver.
+ The clauses are given by sets of nodes. Each node stands for one literal.
+ If the node is complemented, the literal is negated.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ManAddClause( Fraig_Man_t * p, Fraig_Node_t ** ppNodes, int nNodes )
+{
+ Fraig_Node_t * pNode;
+ int i, fComp, RetValue;
+ if ( p->pSat == NULL )
+ Fraig_ManCreateSolver( p );
+ // create four clauses
+ Msat_IntVecClear( p->vProj );
+ for ( i = 0; i < nNodes; i++ )
+ {
+ pNode = Fraig_Regular(ppNodes[i]);
+ fComp = Fraig_IsComplement(ppNodes[i]);
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode->Num, fComp) );
+// printf( "%d(%d) ", pNode->Num, fComp );
+ }
+// printf( "\n" );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
diff --git a/src/sat/fraig/fraigMem.c b/src/sat/fraig/fraigMem.c
new file mode 100644
index 00000000..500431c6
--- /dev/null
+++ b/src/sat/fraig/fraigMem.c
@@ -0,0 +1,246 @@
+/**CFile****************************************************************
+
+ FileName [fraigMem.c]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [Fixed-size-entry memory manager for the FRAIG package.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigMem.c,v 1.4 2005/07/08 01:01:31 alanmi Exp $]
+
+***********************************************************************/
+
+#include "fraigInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+struct Fraig_MemFixed_t_
+{
+ // information about individual entries
+ int nEntrySize; // the size of one entry
+ int nEntriesAlloc; // the total number of entries allocated
+ int nEntriesUsed; // the number of entries in use
+ int nEntriesMax; // the max number of entries in use
+ char * pEntriesFree; // the linked list of free entries
+
+ // this is where the memory is stored
+ int nChunkSize; // the size of one chunk
+ int nChunksAlloc; // the maximum number of memory chunks
+ int nChunks; // the current number of memory chunks
+ char ** pChunks; // the allocated memory
+
+ // statistics
+ int nMemoryUsed; // memory used in the allocated entries
+ int nMemoryAlloc; // memory allocated
+};
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Starts the internal memory manager.]
+
+ Description [Can only work with entry size at least 4 byte long.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_MemFixed_t * Fraig_MemFixedStart( int nEntrySize )
+{
+ Fraig_MemFixed_t * p;
+
+ p = ALLOC( Fraig_MemFixed_t, 1 );
+ memset( p, 0, sizeof(Fraig_MemFixed_t) );
+
+ p->nEntrySize = nEntrySize;
+ p->nEntriesAlloc = 0;
+ p->nEntriesUsed = 0;
+ p->pEntriesFree = NULL;
+
+ if ( nEntrySize * (1 << 10) < (1<<16) )
+ p->nChunkSize = (1 << 10);
+ else
+ p->nChunkSize = (1<<16) / nEntrySize;
+ if ( p->nChunkSize < 8 )
+ p->nChunkSize = 8;
+
+ p->nChunksAlloc = 64;
+ p->nChunks = 0;
+ p->pChunks = ALLOC( char *, p->nChunksAlloc );
+
+ p->nMemoryUsed = 0;
+ p->nMemoryAlloc = 0;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Stops the internal memory manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_MemFixedStop( Fraig_MemFixed_t * p, int fVerbose )
+{
+ int i;
+ if ( p == NULL )
+ return;
+ if ( fVerbose )
+ {
+ printf( "Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n",
+ p->nEntrySize, p->nChunkSize, p->nChunks );
+ printf( " Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n",
+ p->nEntriesUsed, p->nEntriesMax, p->nEntrySize * p->nEntriesUsed, p->nMemoryAlloc );
+ }
+ for ( i = 0; i < p->nChunks; i++ )
+ free( p->pChunks[i] );
+ free( p->pChunks );
+ free( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Extracts one entry from the memory manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+char * Fraig_MemFixedEntryFetch( Fraig_MemFixed_t * p )
+{
+ char * pTemp;
+ int i;
+
+ // check if there are still free entries
+ if ( p->nEntriesUsed == p->nEntriesAlloc )
+ { // need to allocate more entries
+ assert( p->pEntriesFree == NULL );
+ if ( p->nChunks == p->nChunksAlloc )
+ {
+ p->nChunksAlloc *= 2;
+ p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc );
+ }
+ p->pEntriesFree = ALLOC( char, p->nEntrySize * p->nChunkSize );
+ p->nMemoryAlloc += p->nEntrySize * p->nChunkSize;
+ // transform these entries into a linked list
+ pTemp = p->pEntriesFree;
+ for ( i = 1; i < p->nChunkSize; i++ )
+ {
+ *((char **)pTemp) = pTemp + p->nEntrySize;
+ pTemp += p->nEntrySize;
+ }
+ // set the last link
+ *((char **)pTemp) = NULL;
+ // add the chunk to the chunk storage
+ p->pChunks[ p->nChunks++ ] = p->pEntriesFree;
+ // add to the number of entries allocated
+ p->nEntriesAlloc += p->nChunkSize;
+ }
+ // incrememt the counter of used entries
+ p->nEntriesUsed++;
+ if ( p->nEntriesMax < p->nEntriesUsed )
+ p->nEntriesMax = p->nEntriesUsed;
+ // return the first entry in the free entry list
+ pTemp = p->pEntriesFree;
+ p->pEntriesFree = *((char **)pTemp);
+ return pTemp;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns one entry into the memory manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_MemFixedEntryRecycle( Fraig_MemFixed_t * p, char * pEntry )
+{
+ // decrement the counter of used entries
+ p->nEntriesUsed--;
+ // add the entry to the linked list of free entries
+ *((char **)pEntry) = p->pEntriesFree;
+ p->pEntriesFree = pEntry;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Frees all associated memory and resets the manager.]
+
+ Description [Relocates all the memory except the first chunk.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_MemFixedRestart( Fraig_MemFixed_t * p )
+{
+ int i;
+ char * pTemp;
+
+ // deallocate all chunks except the first one
+ for ( i = 1; i < p->nChunks; i++ )
+ free( p->pChunks[i] );
+ p->nChunks = 1;
+ // transform these entries into a linked list
+ pTemp = p->pChunks[0];
+ for ( i = 1; i < p->nChunkSize; i++ )
+ {
+ *((char **)pTemp) = pTemp + p->nEntrySize;
+ pTemp += p->nEntrySize;
+ }
+ // set the last link
+ *((char **)pTemp) = NULL;
+ // set the free entry list
+ p->pEntriesFree = p->pChunks[0];
+ // set the correct statistics
+ p->nMemoryAlloc = p->nEntrySize * p->nChunkSize;
+ p->nMemoryUsed = 0;
+ p->nEntriesAlloc = p->nChunkSize;
+ p->nEntriesUsed = 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reports the memory usage.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_MemFixedReadMemUsage( Fraig_MemFixed_t * p )
+{
+ return p->nMemoryAlloc;
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/fraig/fraigNode.c b/src/sat/fraig/fraigNode.c
new file mode 100644
index 00000000..6e3d3c7d
--- /dev/null
+++ b/src/sat/fraig/fraigNode.c
@@ -0,0 +1,313 @@
+/**CFile****************************************************************
+
+ FileName [fraigNode.c]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [Implementation of the FRAIG node.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigNode.c,v 1.3 2005/07/08 01:01:32 alanmi Exp $]
+
+***********************************************************************/
+
+#include "fraigInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+// returns the complemented attribute of the node
+#define Fraig_NodeIsSimComplement(p) (Fraig_IsComplement(p)? !(Fraig_Regular(p)->fInv) : (p)->fInv)
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Creates the constant 1 node.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_NodeCreateConst( Fraig_Man_t * p )
+{
+ Fraig_Node_t * pNode;
+
+ // create the node
+ pNode = (Fraig_Node_t *)Fraig_MemFixedEntryFetch( p->mmNodes );
+ memset( pNode, 0, sizeof(Fraig_Node_t) );
+
+ // assign the number and add to the array of nodes
+ pNode->Num = p->vNodes->nSize;
+ Fraig_NodeVecPush( p->vNodes, pNode );
+ pNode->NumPi = -1; // this is not a PI, so its number is -1
+ pNode->Level = 0; // just like a PI, it has 0 level
+ pNode->nRefs = 1; // it is a persistent node, which comes referenced
+ pNode->fInv = 1; // the simulation info is complemented
+
+ // create the simulation info
+ pNode->puSimR = (unsigned *)Fraig_MemFixedEntryFetch( p->mmSims );
+ pNode->puSimD = pNode->puSimR + p->nWordsRand;
+ memset( pNode->puSimR, 0, sizeof(unsigned) * p->nWordsRand );
+ memset( pNode->puSimD, 0, sizeof(unsigned) * p->nWordsDyna );
+
+ // count the number of ones in the simulation vector
+ pNode->nOnes = p->nWordsRand * sizeof(unsigned) * 8;
+
+ // insert it into the hash table
+ Fraig_HashTableLookupF0( p, pNode );
+ return pNode;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Creates a primary input node.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_NodeCreatePi( Fraig_Man_t * p )
+{
+ Fraig_Node_t * pNode, * pNodeRes;
+ int i, clk;
+
+ // create the node
+ pNode = (Fraig_Node_t *)Fraig_MemFixedEntryFetch( p->mmNodes );
+ memset( pNode, 0, sizeof(Fraig_Node_t) );
+ pNode->puSimR = (unsigned *)Fraig_MemFixedEntryFetch( p->mmSims );
+ pNode->puSimD = pNode->puSimR + p->nWordsRand;
+ memset( pNode->puSimD, 0, sizeof(unsigned) * p->nWordsDyna );
+
+ // assign the number and add to the array of nodes
+ pNode->Num = p->vNodes->nSize;
+ Fraig_NodeVecPush( p->vNodes, pNode );
+
+ // assign the PI number and add to the array of primary inputs
+ pNode->NumPi = p->vInputs->nSize;
+ Fraig_NodeVecPush( p->vInputs, pNode );
+
+ pNode->Level = 0; // PI has 0 level
+ pNode->nRefs = 1; // it is a persistent node, which comes referenced
+ pNode->fInv = 0; // the simulation info of the PI is not complemented
+
+ // derive the simulation info for the new node
+clk = clock();
+ // set the random simulation info for the primary input
+ pNode->uHashR = 0;
+ for ( i = 0; i < p->nWordsRand; i++ )
+ {
+ // generate the simulation info
+ pNode->puSimR[i] = FRAIG_RANDOM_UNSIGNED;
+ // for reasons that take very long to explain, it makes sense to have (0000000...)
+ // pattern in the set (this helps if we need to return the counter-examples)
+ if ( i == 0 )
+ pNode->puSimR[i] <<= 1;
+ // compute the hash key
+ pNode->uHashR ^= pNode->puSimR[i] * s_FraigPrimes[i];
+ }
+ // count the number of ones in the simulation vector
+ pNode->nOnes = Fraig_BitStringCountOnes( pNode->puSimR, p->nWordsRand );
+
+ // set the systematic simulation info for the primary input
+ pNode->uHashD = 0;
+ for ( i = 0; i < p->iWordStart; i++ )
+ {
+ // generate the simulation info
+ pNode->puSimD[i] = FRAIG_RANDOM_UNSIGNED;
+ // compute the hash key
+ pNode->uHashD ^= pNode->puSimD[i] * s_FraigPrimes[i];
+ }
+p->timeSims += clock() - clk;
+
+ // insert it into the hash table
+ pNodeRes = Fraig_HashTableLookupF( p, pNode );
+ assert( pNodeRes == NULL );
+ // add to the runtime of simulation
+ return pNode;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Creates a new node.]
+
+ Description [This procedure should be called to create the constant
+ node and the PI nodes first.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_NodeCreate( Fraig_Man_t * p, Fraig_Node_t * p1, Fraig_Node_t * p2 )
+{
+ Fraig_Node_t * pNode;
+ int clk;
+
+ // create the node
+ pNode = (Fraig_Node_t *)Fraig_MemFixedEntryFetch( p->mmNodes );
+ memset( pNode, 0, sizeof(Fraig_Node_t) );
+
+ // assign the children
+ pNode->p1 = p1; Fraig_Ref(p1); Fraig_Regular(p1)->nRefs++;
+ pNode->p2 = p2; Fraig_Ref(p2); Fraig_Regular(p2)->nRefs++;
+
+ // assign the number and add to the array of nodes
+ pNode->Num = p->vNodes->nSize;
+ Fraig_NodeVecPush( p->vNodes, pNode );
+
+ // assign the PI number
+ pNode->NumPi = -1;
+
+ // compute the level of this node
+ pNode->Level = 1 + FRAIG_MAX(Fraig_Regular(p1)->Level, Fraig_Regular(p2)->Level);
+ pNode->fInv = Fraig_NodeIsSimComplement(p1) & Fraig_NodeIsSimComplement(p2);
+ pNode->fFailTfo = Fraig_Regular(p1)->fFailTfo | Fraig_Regular(p2)->fFailTfo;
+
+ // derive the simulation info
+clk = clock();
+ // allocate memory for the simulation info
+ pNode->puSimR = (unsigned *)Fraig_MemFixedEntryFetch( p->mmSims );
+ pNode->puSimD = pNode->puSimR + p->nWordsRand;
+ // derive random simulation info
+ pNode->uHashR = 0;
+ Fraig_NodeSimulate( pNode, 0, p->nWordsRand, 1 );
+ // derive dynamic simulation info
+ pNode->uHashD = 0;
+ Fraig_NodeSimulate( pNode, 0, p->iWordStart, 0 );
+ // count the number of ones in the random simulation info
+ pNode->nOnes = Fraig_BitStringCountOnes( pNode->puSimR, p->nWordsRand );
+ if ( pNode->fInv )
+ pNode->nOnes = p->nWordsRand * 32 - pNode->nOnes;
+ // add to the runtime of simulation
+p->timeSims += clock() - clk;
+
+#ifdef FRAIG_ENABLE_FANOUTS
+ // create the fanout info
+ Fraig_NodeAddFaninFanout( Fraig_Regular(p1), pNode );
+ Fraig_NodeAddFaninFanout( Fraig_Regular(p2), pNode );
+#endif
+ return pNode;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Simulates the node.]
+
+ Description [Simulates the random or dynamic simulation info through
+ the node. Uses phases of the children to determine their real simulation
+ info. Uses phase of the node to determine the way its simulation info
+ is stored. The resulting info is guaranteed to be 0 for the first pattern.]
+
+ SideEffects [This procedure modified the hash value of the simulation info.]
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeSimulate( Fraig_Node_t * pNode, int iWordStart, int iWordStop, int fUseRand )
+{
+ unsigned * pSims, * pSims1, * pSims2;
+ unsigned uHash;
+ int fCompl, fCompl1, fCompl2, i;
+
+ assert( !Fraig_IsComplement(pNode) );
+
+ // get hold of the simulation information
+ pSims = fUseRand? pNode->puSimR : pNode->puSimD;
+ pSims1 = fUseRand? Fraig_Regular(pNode->p1)->puSimR : Fraig_Regular(pNode->p1)->puSimD;
+ pSims2 = fUseRand? Fraig_Regular(pNode->p2)->puSimR : Fraig_Regular(pNode->p2)->puSimD;
+
+ // get complemented attributes of the children using their random info
+ fCompl = pNode->fInv;
+ fCompl1 = Fraig_NodeIsSimComplement(pNode->p1);
+ fCompl2 = Fraig_NodeIsSimComplement(pNode->p2);
+
+ // simulate
+ uHash = 0;
+ if ( fCompl1 && fCompl2 )
+ {
+ if ( fCompl )
+ for ( i = iWordStart; i < iWordStop; i++ )
+ {
+ pSims[i] = (pSims1[i] | pSims2[i]);
+ uHash ^= pSims[i] * s_FraigPrimes[i];
+ }
+ else
+ for ( i = iWordStart; i < iWordStop; i++ )
+ {
+ pSims[i] = ~(pSims1[i] | pSims2[i]);
+ uHash ^= pSims[i] * s_FraigPrimes[i];
+ }
+ }
+ else if ( fCompl1 && !fCompl2 )
+ {
+ if ( fCompl )
+ for ( i = iWordStart; i < iWordStop; i++ )
+ {
+ pSims[i] = (pSims1[i] | ~pSims2[i]);
+ uHash ^= pSims[i] * s_FraigPrimes[i];
+ }
+ else
+ for ( i = iWordStart; i < iWordStop; i++ )
+ {
+ pSims[i] = (~pSims1[i] & pSims2[i]);
+ uHash ^= pSims[i] * s_FraigPrimes[i];
+ }
+ }
+ else if ( !fCompl1 && fCompl2 )
+ {
+ if ( fCompl )
+ for ( i = iWordStart; i < iWordStop; i++ )
+ {
+ pSims[i] = (~pSims1[i] | pSims2[i]);
+ uHash ^= pSims[i] * s_FraigPrimes[i];
+ }
+ else
+ for ( i = iWordStart; i < iWordStop; i++ )
+ {
+ pSims[i] = (pSims1[i] & ~pSims2[i]);
+ uHash ^= pSims[i] * s_FraigPrimes[i];
+ }
+ }
+ else // if ( !fCompl1 && !fCompl2 )
+ {
+ if ( fCompl )
+ for ( i = iWordStart; i < iWordStop; i++ )
+ {
+ pSims[i] = ~(pSims1[i] & pSims2[i]);
+ uHash ^= pSims[i] * s_FraigPrimes[i];
+ }
+ else
+ for ( i = iWordStart; i < iWordStop; i++ )
+ {
+ pSims[i] = (pSims1[i] & pSims2[i]);
+ uHash ^= pSims[i] * s_FraigPrimes[i];
+ }
+ }
+
+ if ( fUseRand )
+ pNode->uHashR ^= uHash;
+ else
+ pNode->uHashD ^= uHash;
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
diff --git a/src/sat/fraig/fraigPrime.c b/src/sat/fraig/fraigPrime.c
new file mode 100644
index 00000000..127ad478
--- /dev/null
+++ b/src/sat/fraig/fraigPrime.c
@@ -0,0 +1,144 @@
+/**CFile****************************************************************
+
+ FileName [fraigPrime.c]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [The table of the first 1000 primes.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigPrime.c,v 1.4 2005/07/08 01:01:32 alanmi Exp $]
+
+***********************************************************************/
+
+#include "fraigInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+// The 1,024 smallest prime numbers used to compute the hash value
+// http://www.math.utah.edu/~alfeld/math/primelist.html
+int s_FraigPrimes[FRAIG_MAX_PRIMES] = { 2, 3, 5,
+7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
+101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191,
+193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283,
+293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401,
+409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509,
+521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631,
+641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751,
+757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877,
+881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997,
+1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091,
+1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193,
+1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291,
+1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423,
+1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493,
+1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601,
+1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699,
+1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811,
+1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931,
+1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029,
+2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137,
+2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267,
+2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357,
+2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459,
+2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593,
+2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693,
+2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791,
+2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903,
+2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023,
+3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167,
+3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271,
+3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373,
+3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511,
+3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607,
+3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709,
+3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833,
+3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931,
+3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057,
+4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177,
+4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283,
+4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423,
+4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547,
+4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657,
+4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789,
+4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931,
+4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011,
+5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147,
+5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279,
+5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413,
+5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507,
+5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647,
+5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743,
+5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857,
+5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007,
+6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121,
+6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247,
+6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343,
+6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473,
+6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607,
+6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733,
+6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857,
+6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971,
+6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103,
+7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229,
+7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369,
+7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517,
+7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603,
+7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723,
+7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873,
+7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009,
+8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123,
+8147, 8161 };
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function********************************************************************
+
+ Synopsis [Returns the next prime &gt;= p.]
+
+ Description [Copied from CUDD, for stand-aloneness.]
+
+ SideEffects [None]
+
+ SeeAlso []
+
+******************************************************************************/
+unsigned int Cudd_PrimeFraig( unsigned int p)
+{
+ int i,pn;
+
+ p--;
+ do {
+ p++;
+ if (p&1) {
+ pn = 1;
+ i = 3;
+ while ((unsigned) (i * i) <= p) {
+ if (p % i == 0) {
+ pn = 0;
+ break;
+ }
+ i += 2;
+ }
+ } else {
+ pn = 0;
+ }
+ } while (!pn);
+ return(p);
+
+} /* end of Cudd_Prime */
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/fraig/fraigSat.c b/src/sat/fraig/fraigSat.c
new file mode 100644
index 00000000..53057fc3
--- /dev/null
+++ b/src/sat/fraig/fraigSat.c
@@ -0,0 +1,1455 @@
+/**CFile****************************************************************
+
+ FileName [fraigSat.c]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [Proving functional equivalence using SAT.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigSat.c,v 1.10 2005/07/08 01:01:32 alanmi Exp $]
+
+***********************************************************************/
+
+#include "fraigInt.h"
+#include "math.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+static void Fraig_OrderVariables( Fraig_Man_t * pMan, Fraig_Node_t * pOld, Fraig_Node_t * pNew );
+static void Fraig_SetupAdjacent( Fraig_Man_t * pMan, Msat_IntVec_t * vConeVars );
+static void Fraig_SetupAdjacentMark( Fraig_Man_t * pMan, Msat_IntVec_t * vConeVars );
+static void Fraig_PrepareCones( Fraig_Man_t * pMan, Fraig_Node_t * pOld, Fraig_Node_t * pNew );
+static void Fraig_PrepareCones_rec( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
+
+static void Fraig_SupergateAddClauses( Fraig_Man_t * pMan, Fraig_Node_t * pNode, Fraig_NodeVec_t * vSuper );
+static void Fraig_SupergateAddClausesExor( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
+static void Fraig_SupergateAddClausesMux( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
+//static void Fraig_DetectFanoutFreeCone( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
+static void Fraig_DetectFanoutFreeConeMux( Fraig_Man_t * pMan, Fraig_Node_t * pNode );
+static void Fraig_SetActivity( Fraig_Man_t * pMan, Fraig_Node_t * pOld, Fraig_Node_t * pNew );
+
+extern void * Msat_ClauseVecReadEntry( void * p, int i );
+
+// The lesson learned seems to be that variable should be in reverse topological order
+// from the output of the miter. The ordering of adjacency lists is very important.
+// The best way seems to be fanins followed by fanouts. Slight changes to this order
+// leads to big degradation in quality.
+
+static int nMuxes;
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Checks equivalence of two nodes.]
+
+ Description [Returns 1 iff the nodes are equivalent.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodesAreEqual( Fraig_Man_t * p, Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int nBTLimit, int nTimeLimit )
+{
+ if ( pNode1 == pNode2 )
+ return 1;
+ if ( pNode1 == Fraig_Not(pNode2) )
+ return 0;
+ return Fraig_NodeIsEquivalent( p, Fraig_Regular(pNode1), Fraig_Regular(pNode2), nBTLimit, nTimeLimit );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Tries to prove the final miter.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ManProveMiter( Fraig_Man_t * p )
+{
+ Fraig_Node_t * pNode;
+ int i, clk;
+
+ if ( !p->fTryProve )
+ return;
+
+ clk = clock();
+ // consider all outputs of the multi-output miter
+ for ( i = 0; i < p->vOutputs->nSize; i++ )
+ {
+ pNode = Fraig_Regular(p->vOutputs->pArray[i]);
+ // skip already constant nodes
+ if ( pNode == p->pConst1 )
+ continue;
+ // skip nodes that are different according to simulation
+ if ( !Fraig_CompareSimInfo( pNode, p->pConst1, p->nWordsRand, 1 ) )
+ continue;
+ if ( Fraig_NodeIsEquivalent( p, p->pConst1, pNode, -1, p->nSeconds ) )
+ {
+ if ( Fraig_IsComplement(p->vOutputs->pArray[i]) ^ Fraig_NodeComparePhase(p->pConst1, pNode) )
+ p->vOutputs->pArray[i] = Fraig_Not(p->pConst1);
+ else
+ p->vOutputs->pArray[i] = p->pConst1;
+ }
+ }
+ if ( p->fVerboseP )
+ {
+// PRT( "Final miter proof time", clock() - clk );
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns 1 if the miter is unsat; 0 if sat; -1 if undecided.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_ManCheckMiter( Fraig_Man_t * p )
+{
+ Fraig_Node_t * pNode;
+ int i;
+ FREE( p->pModel );
+ for ( i = 0; i < p->vOutputs->nSize; i++ )
+ {
+ // get the output node (it can be complemented!)
+ pNode = p->vOutputs->pArray[i];
+ // if the miter is constant 0, the problem is UNSAT
+ if ( pNode == Fraig_Not(p->pConst1) )
+ continue;
+ // consider the special case when the miter is constant 1
+ if ( pNode == p->pConst1 )
+ {
+ // in this case, any counter example will do to distinquish it from constant 0
+ // here we pick the counter example composed of all zeros
+ p->pModel = Fraig_ManAllocCounterExample( p );
+ return 0;
+ }
+ // save the counter example
+ p->pModel = Fraig_ManSaveCounterExample( p, pNode );
+ // if the model is not found, return undecided
+ if ( p->pModel == NULL )
+ return -1;
+ else
+ return 0;
+ }
+ return 1;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Returns 1 if pOld is in the TFI of pNew.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_MarkTfi_rec( Fraig_Man_t * pMan, Fraig_Node_t * pNode )
+{
+ // skip the visited node
+ if ( pNode->TravId == pMan->nTravIds )
+ return 0;
+ pNode->TravId = pMan->nTravIds;
+ // skip the PI node
+ if ( pNode->NumPi >= 0 )
+ return 1;
+ // check the children
+ return Fraig_MarkTfi_rec( pMan, Fraig_Regular(pNode->p1) ) +
+ Fraig_MarkTfi_rec( pMan, Fraig_Regular(pNode->p2) );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns 1 if pOld is in the TFI of pNew.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_MarkTfi2_rec( Fraig_Man_t * pMan, Fraig_Node_t * pNode )
+{
+ // skip the visited node
+ if ( pNode->TravId == pMan->nTravIds )
+ return 0;
+ // skip the boundary node
+ if ( pNode->TravId == pMan->nTravIds-1 )
+ {
+ pNode->TravId = pMan->nTravIds;
+ return 1;
+ }
+ pNode->TravId = pMan->nTravIds;
+ // skip the PI node
+ if ( pNode->NumPi >= 0 )
+ return 1;
+ // check the children
+ return Fraig_MarkTfi2_rec( pMan, Fraig_Regular(pNode->p1) ) +
+ Fraig_MarkTfi2_rec( pMan, Fraig_Regular(pNode->p2) );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns 1 if pOld is in the TFI of pNew.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_MarkTfi3_rec( Fraig_Man_t * pMan, Fraig_Node_t * pNode )
+{
+ // skip the visited node
+ if ( pNode->TravId == pMan->nTravIds )
+ return 1;
+ // skip the boundary node
+ if ( pNode->TravId == pMan->nTravIds-1 )
+ {
+ pNode->TravId = pMan->nTravIds;
+ return 1;
+ }
+ pNode->TravId = pMan->nTravIds;
+ // skip the PI node
+ if ( pNode->NumPi >= 0 )
+ return 0;
+ // check the children
+ return Fraig_MarkTfi3_rec( pMan, Fraig_Regular(pNode->p1) ) *
+ Fraig_MarkTfi3_rec( pMan, Fraig_Regular(pNode->p2) );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_VarsStudy( Fraig_Man_t * p, Fraig_Node_t * pOld, Fraig_Node_t * pNew )
+{
+ int NumPis, NumCut, fContain;
+
+ // mark the TFI of pNew
+ p->nTravIds++;
+ NumPis = Fraig_MarkTfi_rec( p, pNew );
+ printf( "(%d)(%d,%d):", NumPis, pOld->Level, pNew->Level );
+
+ // check if the old is in the TFI
+ if ( pOld->TravId == p->nTravIds )
+ {
+ printf( "* " );
+ return;
+ }
+
+ // count the boundary of nodes in pOld
+ p->nTravIds++;
+ NumCut = Fraig_MarkTfi2_rec( p, pOld );
+ printf( "%d", NumCut );
+
+ // check if the new is contained in the old's support
+ p->nTravIds++;
+ fContain = Fraig_MarkTfi3_rec( p, pNew );
+ printf( "%c ", fContain? '+':'-' );
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Checks whether two nodes are functinally equivalent.]
+
+ Description [The flag (fComp) tells whether the nodes to be checked
+ are in the opposite polarity. The second flag (fSkipZeros) tells whether
+ the checking should be performed if the simulation vectors are zeros.
+ Returns 1 if the nodes are equivalent; 0 othewise.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeIsEquivalent( Fraig_Man_t * p, Fraig_Node_t * pOld, Fraig_Node_t * pNew, int nBTLimit, int nTimeLimit )
+{
+ int RetValue, RetValue1, i, fComp, clk;
+ int fVerbose = 0;
+ int fSwitch = 0;
+
+ // make sure the nodes are not complemented
+ assert( !Fraig_IsComplement(pNew) );
+ assert( !Fraig_IsComplement(pOld) );
+ assert( pNew != pOld );
+
+ // if at least one of the nodes is a failed node, perform adjustments:
+ // if the backtrack limit is small, simply skip this node
+ // if the backtrack limit is > 10, take the quare root of the limit
+ if ( nBTLimit > 0 && (pOld->fFailTfo || pNew->fFailTfo) )
+ {
+ p->nSatFails++;
+// return 0;
+// if ( nBTLimit > 10 )
+// nBTLimit /= 10;
+ if ( nBTLimit <= 10 )
+ return 0;
+ nBTLimit = (int)sqrt(nBTLimit);
+// fSwitch = 1;
+ }
+
+ p->nSatCalls++;
+
+ // make sure the solver is allocated and has enough variables
+ if ( p->pSat == NULL )
+ Fraig_ManCreateSolver( p );
+ // make sure the SAT solver has enough variables
+ for ( i = Msat_SolverReadVarNum(p->pSat); i < p->vNodes->nSize; i++ )
+ Msat_SolverAddVar( p->pSat, p->vNodes->pArray[i]->Level );
+
+
+
+/*
+ {
+ Fraig_Node_t * ppNodes[2] = { pOld, pNew };
+ extern void Fraig_MappingShowNodes( Fraig_Man_t * pMan, Fraig_Node_t ** ppRoots, int nRoots, char * pFileName );
+ Fraig_MappingShowNodes( p, ppNodes, 2, "temp_aig" );
+ }
+*/
+
+ nMuxes = 0;
+
+
+ // get the logic cone
+clk = clock();
+// Fraig_VarsStudy( p, pOld, pNew );
+ Fraig_OrderVariables( p, pOld, pNew );
+// Fraig_PrepareCones( p, pOld, pNew );
+p->timeTrav += clock() - clk;
+
+// printf( "The number of MUXes detected = %d (%5.2f %% of logic). ", nMuxes, 300.0*nMuxes/(p->vNodes->nSize - p->vInputs->nSize) );
+// PRT( "Time", clock() - clk );
+
+if ( fVerbose )
+ printf( "%d(%d) - ", Fraig_CountPis(p,p->vVarsInt), Msat_IntVecReadSize(p->vVarsInt) );
+
+
+ // prepare variable activity
+ Fraig_SetActivity( p, pOld, pNew );
+
+ // get the complemented attribute
+ fComp = Fraig_NodeComparePhase( pOld, pNew );
+//Msat_SolverPrintClauses( p->pSat );
+
+ ////////////////////////////////////////////
+ // prepare the solver to run incrementally on these variables
+//clk = clock();
+ Msat_SolverPrepare( p->pSat, p->vVarsInt );
+//p->time3 += clock() - clk;
+
+
+ // solve under assumptions
+ // A = 1; B = 0 OR A = 1; B = 1
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pOld->Num, 0) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNew->Num, !fComp) );
+
+//Msat_SolverWriteDimacs( p->pSat, "temp_fraig.cnf" );
+
+ // run the solver
+clk = clock();
+ RetValue1 = Msat_SolverSolve( p->pSat, p->vProj, nBTLimit, nTimeLimit );
+p->timeSat += clock() - clk;
+
+ if ( RetValue1 == MSAT_FALSE )
+ {
+//p->time1 += clock() - clk;
+
+if ( fVerbose )
+{
+ printf( "unsat %d ", Msat_SolverReadBackTracks(p->pSat) );
+PRT( "time", clock() - clk );
+}
+
+ // add the clause
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pOld->Num, 1) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNew->Num, fComp) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+ // continue solving the other implication
+ }
+ else if ( RetValue1 == MSAT_TRUE )
+ {
+//p->time2 += clock() - clk;
+
+if ( fVerbose )
+{
+ printf( "sat %d ", Msat_SolverReadBackTracks(p->pSat) );
+PRT( "time", clock() - clk );
+}
+
+ // record the counter example
+ Fraig_FeedBack( p, Msat_SolverReadModelArray(p->pSat), p->vVarsInt, pOld, pNew );
+
+// if ( pOld->fFailTfo || pNew->fFailTfo )
+// printf( "*" );
+// printf( "s(%d)", pNew->Level );
+ if ( fSwitch )
+ printf( "s(%d)", pNew->Level );
+ p->nSatCounter++;
+ return 0;
+ }
+ else // if ( RetValue1 == MSAT_UNKNOWN )
+ {
+p->time3 += clock() - clk;
+
+// if ( pOld->fFailTfo || pNew->fFailTfo )
+// printf( "*" );
+// printf( "T(%d)", pNew->Level );
+
+ // mark the node as the failed node
+ if ( pOld != p->pConst1 )
+ pOld->fFailTfo = 1;
+ pNew->fFailTfo = 1;
+// p->nSatFails++;
+ if ( fSwitch )
+ printf( "T(%d)", pNew->Level );
+ p->nSatFailsReal++;
+ return 0;
+ }
+
+ // if the old node was constant 0, we already know the answer
+ if ( pOld == p->pConst1 )
+ return 1;
+
+ ////////////////////////////////////////////
+ // prepare the solver to run incrementally
+//clk = clock();
+ Msat_SolverPrepare( p->pSat, p->vVarsInt );
+//p->time3 += clock() - clk;
+ // solve under assumptions
+ // A = 0; B = 1 OR A = 0; B = 0
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pOld->Num, 1) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNew->Num, fComp) );
+ // run the solver
+clk = clock();
+ RetValue1 = Msat_SolverSolve( p->pSat, p->vProj, nBTLimit, nTimeLimit );
+p->timeSat += clock() - clk;
+
+ if ( RetValue1 == MSAT_FALSE )
+ {
+//p->time1 += clock() - clk;
+
+if ( fVerbose )
+{
+ printf( "unsat %d ", Msat_SolverReadBackTracks(p->pSat) );
+PRT( "time", clock() - clk );
+}
+
+ // add the clause
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pOld->Num, 0) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNew->Num, !fComp) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+ // continue solving the other implication
+ }
+ else if ( RetValue1 == MSAT_TRUE )
+ {
+//p->time2 += clock() - clk;
+
+if ( fVerbose )
+{
+ printf( "sat %d ", Msat_SolverReadBackTracks(p->pSat) );
+PRT( "time", clock() - clk );
+}
+
+ // record the counter example
+ Fraig_FeedBack( p, Msat_SolverReadModelArray(p->pSat), p->vVarsInt, pOld, pNew );
+ p->nSatCounter++;
+
+// if ( pOld->fFailTfo || pNew->fFailTfo )
+// printf( "*" );
+// printf( "s(%d)", pNew->Level );
+ if ( fSwitch )
+ printf( "s(%d)", pNew->Level );
+ return 0;
+ }
+ else // if ( RetValue1 == MSAT_UNKNOWN )
+ {
+p->time3 += clock() - clk;
+
+// if ( pOld->fFailTfo || pNew->fFailTfo )
+// printf( "*" );
+// printf( "T(%d)", pNew->Level );
+ if ( fSwitch )
+ printf( "T(%d)", pNew->Level );
+
+ // mark the node as the failed node
+ pOld->fFailTfo = 1;
+ pNew->fFailTfo = 1;
+// p->nSatFails++;
+ p->nSatFailsReal++;
+ return 0;
+ }
+
+ // return SAT proof
+ p->nSatProof++;
+
+// if ( pOld->fFailTfo || pNew->fFailTfo )
+// printf( "*" );
+// printf( "u(%d)", pNew->Level );
+
+ if ( fSwitch )
+ printf( "u(%d)", pNew->Level );
+
+ return 1;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Checks whether pOld => pNew.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeIsImplication( Fraig_Man_t * p, Fraig_Node_t * pOld, Fraig_Node_t * pNew, int nBTLimit )
+{
+ int RetValue, RetValue1, i, fComp, clk;
+ int fVerbose = 0;
+
+ // make sure the nodes are not complemented
+ assert( !Fraig_IsComplement(pNew) );
+ assert( !Fraig_IsComplement(pOld) );
+ assert( pNew != pOld );
+
+ p->nSatCallsImp++;
+
+ // make sure the solver is allocated and has enough variables
+ if ( p->pSat == NULL )
+ Fraig_ManCreateSolver( p );
+ // make sure the SAT solver has enough variables
+ for ( i = Msat_SolverReadVarNum(p->pSat); i < p->vNodes->nSize; i++ )
+ Msat_SolverAddVar( p->pSat, p->vNodes->pArray[i]->Level );
+
+ // get the logic cone
+clk = clock();
+ Fraig_OrderVariables( p, pOld, pNew );
+// Fraig_PrepareCones( p, pOld, pNew );
+p->timeTrav += clock() - clk;
+
+if ( fVerbose )
+ printf( "%d(%d) - ", Fraig_CountPis(p,p->vVarsInt), Msat_IntVecReadSize(p->vVarsInt) );
+
+
+ // get the complemented attribute
+ fComp = Fraig_NodeComparePhase( pOld, pNew );
+//Msat_SolverPrintClauses( p->pSat );
+
+ ////////////////////////////////////////////
+ // prepare the solver to run incrementally on these variables
+//clk = clock();
+ Msat_SolverPrepare( p->pSat, p->vVarsInt );
+//p->time3 += clock() - clk;
+
+ // solve under assumptions
+ // A = 1; B = 0 OR A = 1; B = 1
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pOld->Num, 0) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNew->Num, !fComp) );
+ // run the solver
+clk = clock();
+ RetValue1 = Msat_SolverSolve( p->pSat, p->vProj, nBTLimit, 1000000 );
+p->timeSat += clock() - clk;
+
+ if ( RetValue1 == MSAT_FALSE )
+ {
+//p->time1 += clock() - clk;
+
+if ( fVerbose )
+{
+ printf( "unsat %d ", Msat_SolverReadBackTracks(p->pSat) );
+PRT( "time", clock() - clk );
+}
+
+ // add the clause
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pOld->Num, 1) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNew->Num, fComp) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+// p->nSatProofImp++;
+ return 1;
+ }
+ else if ( RetValue1 == MSAT_TRUE )
+ {
+//p->time2 += clock() - clk;
+
+if ( fVerbose )
+{
+ printf( "sat %d ", Msat_SolverReadBackTracks(p->pSat) );
+PRT( "time", clock() - clk );
+}
+ // record the counter example
+ Fraig_FeedBack( p, Msat_SolverReadModelArray(p->pSat), p->vVarsInt, pOld, pNew );
+ p->nSatCounterImp++;
+ return 0;
+ }
+ else // if ( RetValue1 == MSAT_UNKNOWN )
+ {
+p->time3 += clock() - clk;
+ p->nSatFailsImp++;
+ return 0;
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prepares the SAT solver to run on the two nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_ManCheckClauseUsingSat( Fraig_Man_t * p, Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int nBTLimit )
+{
+ Fraig_Node_t * pNode1R, * pNode2R;
+ int RetValue, RetValue1, i, clk;
+ int fVerbose = 0;
+
+ pNode1R = Fraig_Regular(pNode1);
+ pNode2R = Fraig_Regular(pNode2);
+ assert( pNode1R != pNode2R );
+
+ // make sure the solver is allocated and has enough variables
+ if ( p->pSat == NULL )
+ Fraig_ManCreateSolver( p );
+ // make sure the SAT solver has enough variables
+ for ( i = Msat_SolverReadVarNum(p->pSat); i < p->vNodes->nSize; i++ )
+ Msat_SolverAddVar( p->pSat, p->vNodes->pArray[i]->Level );
+
+ // get the logic cone
+clk = clock();
+ Fraig_OrderVariables( p, pNode1R, pNode2R );
+// Fraig_PrepareCones( p, pNode1R, pNode2R );
+p->timeTrav += clock() - clk;
+
+ ////////////////////////////////////////////
+ // prepare the solver to run incrementally on these variables
+//clk = clock();
+ Msat_SolverPrepare( p->pSat, p->vVarsInt );
+//p->time3 += clock() - clk;
+
+ // solve under assumptions
+ // A = 1; B = 0 OR A = 1; B = 1
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode1R->Num, !Fraig_IsComplement(pNode1)) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode2R->Num, !Fraig_IsComplement(pNode2)) );
+ // run the solver
+clk = clock();
+ RetValue1 = Msat_SolverSolve( p->pSat, p->vProj, nBTLimit, 1000000 );
+p->timeSat += clock() - clk;
+
+ if ( RetValue1 == MSAT_FALSE )
+ {
+//p->time1 += clock() - clk;
+
+if ( fVerbose )
+{
+ printf( "unsat %d ", Msat_SolverReadBackTracks(p->pSat) );
+PRT( "time", clock() - clk );
+}
+
+ // add the clause
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode1R->Num, Fraig_IsComplement(pNode1)) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode2R->Num, Fraig_IsComplement(pNode2)) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+// p->nSatProofImp++;
+ return 1;
+ }
+ else if ( RetValue1 == MSAT_TRUE )
+ {
+//p->time2 += clock() - clk;
+
+if ( fVerbose )
+{
+ printf( "sat %d ", Msat_SolverReadBackTracks(p->pSat) );
+PRT( "time", clock() - clk );
+}
+ // record the counter example
+// Fraig_FeedBack( p, Msat_SolverReadModelArray(p->pSat), p->vVarsInt, pNode1R, pNode2R );
+ p->nSatCounterImp++;
+ return 0;
+ }
+ else // if ( RetValue1 == MSAT_UNKNOWN )
+ {
+p->time3 += clock() - clk;
+ p->nSatFailsImp++;
+ return 0;
+ }
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Prepares the SAT solver to run on the two nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_PrepareCones( Fraig_Man_t * pMan, Fraig_Node_t * pOld, Fraig_Node_t * pNew )
+{
+// Msat_IntVec_t * vAdjs;
+// int * pVars, nVars, i, k;
+ int nVarsAlloc;
+
+ assert( pOld != pNew );
+ assert( !Fraig_IsComplement(pOld) );
+ assert( !Fraig_IsComplement(pNew) );
+ // clean the variables
+ nVarsAlloc = Msat_IntVecReadSize(pMan->vVarsUsed);
+ Msat_IntVecFill( pMan->vVarsUsed, nVarsAlloc, 0 );
+ Msat_IntVecClear( pMan->vVarsInt );
+
+ pMan->nTravIds++;
+ Fraig_PrepareCones_rec( pMan, pNew );
+ Fraig_PrepareCones_rec( pMan, pOld );
+
+
+/*
+ nVars = Msat_IntVecReadSize( pMan->vVarsInt );
+ pVars = Msat_IntVecReadArray( pMan->vVarsInt );
+ for ( i = 0; i < nVars; i++ )
+ {
+ // process its connections
+ vAdjs = (Msat_IntVec_t *)Msat_ClauseVecReadEntry( pMan->vAdjacents, pVars[i] );
+ printf( "%d=%d { ", pVars[i], Msat_IntVecReadSize(vAdjs) );
+ for ( k = 0; k < Msat_IntVecReadSize(vAdjs); k++ )
+ printf( "%d ", Msat_IntVecReadEntry(vAdjs,k) );
+ printf( "}\n" );
+
+ }
+ i = 0;
+*/
+}
+
+/**Function*************************************************************
+
+ Synopsis [Traverses the cone, collects the numbers and adds the clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_PrepareCones_rec( Fraig_Man_t * pMan, Fraig_Node_t * pNode )
+{
+ Fraig_Node_t * pFanin;
+ Msat_IntVec_t * vAdjs;
+ int fUseMuxes = 1, i;
+ int fItIsTime;
+
+ // skip if the node is aleady visited
+ assert( !Fraig_IsComplement(pNode) );
+ if ( pNode->TravId == pMan->nTravIds )
+ return;
+ pNode->TravId = pMan->nTravIds;
+
+ // collect the node's number (closer to reverse topological order)
+ Msat_IntVecPush( pMan->vVarsInt, pNode->Num );
+ Msat_IntVecWriteEntry( pMan->vVarsUsed, pNode->Num, 1 );
+ if ( !Fraig_NodeIsAnd( pNode ) )
+ return;
+
+ // if the node does not have fanins, create them
+ fItIsTime = 0;
+ if ( pNode->vFanins == NULL )
+ {
+ fItIsTime = 1;
+ // create the fanins of the supergate
+ assert( pNode->fClauses == 0 );
+ if ( fUseMuxes && Fraig_NodeIsMuxType(pNode) )
+ {
+ pNode->vFanins = Fraig_NodeVecAlloc( 4 );
+ Fraig_NodeVecPushUnique( pNode->vFanins, Fraig_Regular(Fraig_Regular(pNode->p1)->p1) );
+ Fraig_NodeVecPushUnique( pNode->vFanins, Fraig_Regular(Fraig_Regular(pNode->p1)->p2) );
+ Fraig_NodeVecPushUnique( pNode->vFanins, Fraig_Regular(Fraig_Regular(pNode->p2)->p1) );
+ Fraig_NodeVecPushUnique( pNode->vFanins, Fraig_Regular(Fraig_Regular(pNode->p2)->p2) );
+ Fraig_SupergateAddClausesMux( pMan, pNode );
+ }
+ else
+ {
+ pNode->vFanins = Fraig_CollectSupergate( pNode, fUseMuxes );
+ Fraig_SupergateAddClauses( pMan, pNode, pNode->vFanins );
+ }
+ assert( pNode->vFanins->nSize > 1 );
+ pNode->fClauses = 1;
+ pMan->nVarsClauses++;
+
+ // add fanins
+ vAdjs = (Msat_IntVec_t *)Msat_ClauseVecReadEntry( pMan->vAdjacents, pNode->Num );
+ assert( Msat_IntVecReadSize( vAdjs ) == 0 );
+ for ( i = 0; i < pNode->vFanins->nSize; i++ )
+ {
+ pFanin = Fraig_Regular(pNode->vFanins->pArray[i]);
+ Msat_IntVecPush( vAdjs, pFanin->Num );
+ }
+ }
+
+ // recursively visit the fanins
+ for ( i = 0; i < pNode->vFanins->nSize; i++ )
+ Fraig_PrepareCones_rec( pMan, Fraig_Regular(pNode->vFanins->pArray[i]) );
+
+ if ( fItIsTime )
+ {
+ // recursively visit the fanins
+ for ( i = 0; i < pNode->vFanins->nSize; i++ )
+ {
+ pFanin = Fraig_Regular(pNode->vFanins->pArray[i]);
+ vAdjs = (Msat_IntVec_t *)Msat_ClauseVecReadEntry( pMan->vAdjacents, pFanin->Num );
+ Msat_IntVecPush( vAdjs, pNode->Num );
+ }
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Collect variables using their proximity from the nodes.]
+
+ Description [This procedure creates a variable order based on collecting
+ first the nodes that are the closest to the given two target nodes.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_OrderVariables( Fraig_Man_t * pMan, Fraig_Node_t * pOld, Fraig_Node_t * pNew )
+{
+ Fraig_Node_t * pNode, * pFanin;
+ int i, k, Number, fUseMuxes = 1;
+ int nVarsAlloc;
+
+ assert( pOld != pNew );
+ assert( !Fraig_IsComplement(pOld) );
+ assert( !Fraig_IsComplement(pNew) );
+
+ pMan->nTravIds++;
+
+ // clean the variables
+ nVarsAlloc = Msat_IntVecReadSize(pMan->vVarsUsed);
+ Msat_IntVecFill( pMan->vVarsUsed, nVarsAlloc, 0 );
+ Msat_IntVecClear( pMan->vVarsInt );
+
+ // add the first node
+ Msat_IntVecPush( pMan->vVarsInt, pOld->Num );
+ Msat_IntVecWriteEntry( pMan->vVarsUsed, pOld->Num, 1 );
+ pOld->TravId = pMan->nTravIds;
+
+ // add the second node
+ Msat_IntVecPush( pMan->vVarsInt, pNew->Num );
+ Msat_IntVecWriteEntry( pMan->vVarsUsed, pNew->Num, 1 );
+ pNew->TravId = pMan->nTravIds;
+
+ // create the variable order
+ for ( i = 0; i < Msat_IntVecReadSize(pMan->vVarsInt); i++ )
+ {
+ // get the new node on the frontier
+ Number = Msat_IntVecReadEntry(pMan->vVarsInt, i);
+ pNode = pMan->vNodes->pArray[Number];
+ if ( !Fraig_NodeIsAnd(pNode) )
+ continue;
+
+ // if the node does not have fanins, create them
+ if ( pNode->vFanins == NULL )
+ {
+ // create the fanins of the supergate
+ assert( pNode->fClauses == 0 );
+ // detecting a fanout-free cone (experiment only)
+// Fraig_DetectFanoutFreeCone( pMan, pNode );
+
+ if ( fUseMuxes && Fraig_NodeIsMuxType(pNode) )
+ {
+ pNode->vFanins = Fraig_NodeVecAlloc( 4 );
+ Fraig_NodeVecPushUnique( pNode->vFanins, Fraig_Regular(Fraig_Regular(pNode->p1)->p1) );
+ Fraig_NodeVecPushUnique( pNode->vFanins, Fraig_Regular(Fraig_Regular(pNode->p1)->p2) );
+ Fraig_NodeVecPushUnique( pNode->vFanins, Fraig_Regular(Fraig_Regular(pNode->p2)->p1) );
+ Fraig_NodeVecPushUnique( pNode->vFanins, Fraig_Regular(Fraig_Regular(pNode->p2)->p2) );
+ Fraig_SupergateAddClausesMux( pMan, pNode );
+// Fraig_DetectFanoutFreeConeMux( pMan, pNode );
+
+ nMuxes++;
+ }
+ else
+ {
+ pNode->vFanins = Fraig_CollectSupergate( pNode, fUseMuxes );
+ Fraig_SupergateAddClauses( pMan, pNode, pNode->vFanins );
+ }
+ assert( pNode->vFanins->nSize > 1 );
+ pNode->fClauses = 1;
+ pMan->nVarsClauses++;
+
+ pNode->fMark2 = 1; // goes together with Fraig_SetupAdjacentMark()
+ }
+
+ // explore the implication fanins of pNode
+ for ( k = 0; k < pNode->vFanins->nSize; k++ )
+ {
+ pFanin = Fraig_Regular(pNode->vFanins->pArray[k]);
+ if ( pFanin->TravId == pMan->nTravIds ) // already collected
+ continue;
+ // collect and mark
+ Msat_IntVecPush( pMan->vVarsInt, pFanin->Num );
+ Msat_IntVecWriteEntry( pMan->vVarsUsed, pFanin->Num, 1 );
+ pFanin->TravId = pMan->nTravIds;
+ }
+ }
+
+ // set up the adjacent variable information
+// Fraig_SetupAdjacent( pMan, pMan->vVarsInt );
+ Fraig_SetupAdjacentMark( pMan, pMan->vVarsInt );
+}
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Set up the adjacent variable information.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_SetupAdjacent( Fraig_Man_t * pMan, Msat_IntVec_t * vConeVars )
+{
+ Fraig_Node_t * pNode, * pFanin;
+ Msat_IntVec_t * vAdjs;
+ int * pVars, nVars, i, k;
+
+ // clean the adjacents for the variables
+ nVars = Msat_IntVecReadSize( vConeVars );
+ pVars = Msat_IntVecReadArray( vConeVars );
+ for ( i = 0; i < nVars; i++ )
+ {
+ // process its connections
+ vAdjs = (Msat_IntVec_t *)Msat_ClauseVecReadEntry( pMan->vAdjacents, pVars[i] );
+ Msat_IntVecClear( vAdjs );
+
+ pNode = pMan->vNodes->pArray[pVars[i]];
+ if ( !Fraig_NodeIsAnd(pNode) )
+ continue;
+
+ // add fanins
+ vAdjs = (Msat_IntVec_t *)Msat_ClauseVecReadEntry( pMan->vAdjacents, pVars[i] );
+ for ( k = 0; k < pNode->vFanins->nSize; k++ )
+// for ( k = pNode->vFanins->nSize - 1; k >= 0; k-- )
+ {
+ pFanin = Fraig_Regular(pNode->vFanins->pArray[k]);
+ Msat_IntVecPush( vAdjs, pFanin->Num );
+// Msat_IntVecPushUniqueOrder( vAdjs, pFanin->Num );
+ }
+ }
+ // add the fanouts
+ for ( i = 0; i < nVars; i++ )
+ {
+ pNode = pMan->vNodes->pArray[pVars[i]];
+ if ( !Fraig_NodeIsAnd(pNode) )
+ continue;
+
+ // add the edges
+ for ( k = 0; k < pNode->vFanins->nSize; k++ )
+// for ( k = pNode->vFanins->nSize - 1; k >= 0; k-- )
+ {
+ pFanin = Fraig_Regular(pNode->vFanins->pArray[k]);
+ vAdjs = (Msat_IntVec_t *)Msat_ClauseVecReadEntry( pMan->vAdjacents, pFanin->Num );
+ Msat_IntVecPush( vAdjs, pNode->Num );
+// Msat_IntVecPushUniqueOrder( vAdjs, pFanin->Num );
+ }
+ }
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Set up the adjacent variable information.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_SetupAdjacentMark( Fraig_Man_t * pMan, Msat_IntVec_t * vConeVars )
+{
+ Fraig_Node_t * pNode, * pFanin;
+ Msat_IntVec_t * vAdjs;
+ int * pVars, nVars, i, k;
+
+ // clean the adjacents for the variables
+ nVars = Msat_IntVecReadSize( vConeVars );
+ pVars = Msat_IntVecReadArray( vConeVars );
+ for ( i = 0; i < nVars; i++ )
+ {
+ pNode = pMan->vNodes->pArray[pVars[i]];
+ if ( pNode->fMark2 == 0 )
+ continue;
+// pNode->fMark2 = 0;
+
+ // process its connections
+// vAdjs = (Msat_IntVec_t *)Msat_ClauseVecReadEntry( pMan->vAdjacents, pVars[i] );
+// Msat_IntVecClear( vAdjs );
+
+ if ( !Fraig_NodeIsAnd(pNode) )
+ continue;
+
+ // add fanins
+ vAdjs = (Msat_IntVec_t *)Msat_ClauseVecReadEntry( pMan->vAdjacents, pVars[i] );
+ for ( k = 0; k < pNode->vFanins->nSize; k++ )
+// for ( k = pNode->vFanins->nSize - 1; k >= 0; k-- )
+ {
+ pFanin = Fraig_Regular(pNode->vFanins->pArray[k]);
+ Msat_IntVecPush( vAdjs, pFanin->Num );
+// Msat_IntVecPushUniqueOrder( vAdjs, pFanin->Num );
+ }
+ }
+ // add the fanouts
+ for ( i = 0; i < nVars; i++ )
+ {
+ pNode = pMan->vNodes->pArray[pVars[i]];
+ if ( pNode->fMark2 == 0 )
+ continue;
+ pNode->fMark2 = 0;
+
+ if ( !Fraig_NodeIsAnd(pNode) )
+ continue;
+
+ // add the edges
+ for ( k = 0; k < pNode->vFanins->nSize; k++ )
+// for ( k = pNode->vFanins->nSize - 1; k >= 0; k-- )
+ {
+ pFanin = Fraig_Regular(pNode->vFanins->pArray[k]);
+ vAdjs = (Msat_IntVec_t *)Msat_ClauseVecReadEntry( pMan->vAdjacents, pFanin->Num );
+ Msat_IntVecPush( vAdjs, pNode->Num );
+// Msat_IntVecPushUniqueOrder( vAdjs, pFanin->Num );
+ }
+ }
+}
+
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Adds clauses to the solver.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_SupergateAddClauses( Fraig_Man_t * p, Fraig_Node_t * pNode, Fraig_NodeVec_t * vSuper )
+{
+ int fComp1, RetValue, nVars, Var, Var1, i;
+
+ assert( Fraig_NodeIsAnd( pNode ) );
+ nVars = Msat_SolverReadVarNum(p->pSat);
+
+ Var = pNode->Num;
+ assert( Var < nVars );
+ for ( i = 0; i < vSuper->nSize; i++ )
+ {
+ // get the predecessor nodes
+ // get the complemented attributes of the nodes
+ fComp1 = Fraig_IsComplement(vSuper->pArray[i]);
+ // determine the variable numbers
+ Var1 = Fraig_Regular(vSuper->pArray[i])->Num;
+ // check that the variables are in the SAT manager
+ assert( Var1 < nVars );
+
+ // suppose the AND-gate is A * B = C
+ // add !A => !C or A + !C
+ // fprintf( pFile, "%d %d 0%c", Var1, -Var, 10 );
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(Var1, fComp1) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(Var, 1) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+ }
+
+ // add A & B => C or !A + !B + C
+// fprintf( pFile, "%d %d %d 0%c", -Var1, -Var2, Var, 10 );
+ Msat_IntVecClear( p->vProj );
+ for ( i = 0; i < vSuper->nSize; i++ )
+ {
+ // get the predecessor nodes
+ // get the complemented attributes of the nodes
+ fComp1 = Fraig_IsComplement(vSuper->pArray[i]);
+ // determine the variable numbers
+ Var1 = Fraig_Regular(vSuper->pArray[i])->Num;
+
+ // add this variable to the array
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(Var1, !fComp1) );
+ }
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(Var, 0) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Adds clauses to the solver.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_SupergateAddClausesExor( Fraig_Man_t * p, Fraig_Node_t * pNode )
+{
+ Fraig_Node_t * pNode1, * pNode2;
+ int fComp, RetValue;
+
+ assert( !Fraig_IsComplement( pNode ) );
+ assert( Fraig_NodeIsExorType( pNode ) );
+ // get nodes
+ pNode1 = Fraig_Regular(Fraig_Regular(pNode->p1)->p1);
+ pNode2 = Fraig_Regular(Fraig_Regular(pNode->p1)->p2);
+ // get the complemented attribute of the EXOR/NEXOR gate
+ fComp = Fraig_NodeIsExor( pNode ); // 1 if EXOR, 0 if NEXOR
+
+ // create four clauses
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode->Num, fComp) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode1->Num, fComp) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode2->Num, fComp) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode->Num, fComp) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode1->Num, !fComp) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode2->Num, !fComp) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode->Num, !fComp) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode1->Num, fComp) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode2->Num, !fComp) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode->Num, !fComp) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode1->Num, !fComp) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(pNode2->Num, fComp) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Adds clauses to the solver.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_SupergateAddClausesMux( Fraig_Man_t * p, Fraig_Node_t * pNode )
+{
+ Fraig_Node_t * pNodeI, * pNodeT, * pNodeE;
+ int RetValue, VarF, VarI, VarT, VarE, fCompT, fCompE;
+
+ assert( !Fraig_IsComplement( pNode ) );
+ assert( Fraig_NodeIsMuxType( pNode ) );
+ // get nodes (I = if, T = then, E = else)
+ pNodeI = Fraig_NodeRecognizeMux( pNode, &pNodeT, &pNodeE );
+ // get the variable numbers
+ VarF = pNode->Num;
+ VarI = pNodeI->Num;
+ VarT = Fraig_Regular(pNodeT)->Num;
+ VarE = Fraig_Regular(pNodeE)->Num;
+ // get the complementation flags
+ fCompT = Fraig_IsComplement(pNodeT);
+ fCompE = Fraig_IsComplement(pNodeE);
+
+ // f = ITE(i, t, e)
+
+ // i' + t' + f
+ // i' + t + f'
+ // i + e' + f
+ // i + e + f'
+
+ // create four clauses
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarI, 1) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarT, 1^fCompT) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarF, 0) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarI, 1) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarT, 0^fCompT) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarF, 1) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarI, 0) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarE, 1^fCompE) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarF, 0) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarI, 0) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarE, 0^fCompE) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarF, 1) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+
+ // two additional clauses
+ // t' & e' -> f'
+ // t & e -> f
+
+ // t + e + f'
+ // t' + e' + f
+
+ if ( VarT == VarE )
+ {
+// assert( fCompT == !fCompE );
+ return;
+ }
+
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarT, 0^fCompT) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarE, 0^fCompE) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarF, 1) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+ Msat_IntVecClear( p->vProj );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarT, 1^fCompT) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarE, 1^fCompE) );
+ Msat_IntVecPush( p->vProj, MSAT_VAR2LIT(VarF, 0) );
+ RetValue = Msat_SolverAddClause( p->pSat, p->vProj );
+ assert( RetValue );
+
+}
+
+
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Returns the array of nodes to be combined into one multi-input AND-gate.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_DetectFanoutFreeCone_rec( Fraig_Node_t * pNode, Fraig_NodeVec_t * vSuper, Fraig_NodeVec_t * vInside, int fFirst )
+{
+ // make the pointer regular
+ pNode = Fraig_Regular(pNode);
+ // if the new node is complemented or a PI, another gate begins
+ if ( (!fFirst && pNode->nRefs > 1) || Fraig_NodeIsVar(pNode) )
+ {
+ Fraig_NodeVecPushUnique( vSuper, pNode );
+ return;
+ }
+ // go through the branches
+ Fraig_DetectFanoutFreeCone_rec( pNode->p1, vSuper, vInside, 0 );
+ Fraig_DetectFanoutFreeCone_rec( pNode->p2, vSuper, vInside, 0 );
+ // add the node
+ Fraig_NodeVecPushUnique( vInside, pNode );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns the array of nodes to be combined into one multi-input AND-gate.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+/*
+void Fraig_DetectFanoutFreeCone( Fraig_Man_t * pMan, Fraig_Node_t * pNode )
+{
+ Fraig_NodeVec_t * vFanins;
+ Fraig_NodeVec_t * vInside;
+ int nCubes;
+ extern int Fraig_CutSopCountCubes( Fraig_Man_t * pMan, Fraig_NodeVec_t * vFanins, Fraig_NodeVec_t * vInside );
+
+ vFanins = Fraig_NodeVecAlloc( 8 );
+ vInside = Fraig_NodeVecAlloc( 8 );
+
+ Fraig_DetectFanoutFreeCone_rec( pNode, vFanins, vInside, 1 );
+ assert( vInside->pArray[vInside->nSize-1] == pNode );
+
+ nCubes = Fraig_CutSopCountCubes( pMan, vFanins, vInside );
+
+printf( "%d(%d)", vFanins->nSize, nCubes );
+ Fraig_NodeVecFree( vFanins );
+ Fraig_NodeVecFree( vInside );
+}
+*/
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Returns the array of nodes to be combined into one multi-input AND-gate.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_DetectFanoutFreeConeMux_rec( Fraig_Node_t * pNode, Fraig_NodeVec_t * vSuper, Fraig_NodeVec_t * vInside, int fFirst )
+{
+ // make the pointer regular
+ pNode = Fraig_Regular(pNode);
+ // if the new node is complemented or a PI, another gate begins
+ if ( (!fFirst && pNode->nRefs > 1) || Fraig_NodeIsVar(pNode) || !Fraig_NodeIsMuxType(pNode) )
+ {
+ Fraig_NodeVecPushUnique( vSuper, pNode );
+ return;
+ }
+ // go through the branches
+ Fraig_DetectFanoutFreeConeMux_rec( Fraig_Regular(pNode->p1)->p1, vSuper, vInside, 0 );
+ Fraig_DetectFanoutFreeConeMux_rec( Fraig_Regular(pNode->p1)->p2, vSuper, vInside, 0 );
+ Fraig_DetectFanoutFreeConeMux_rec( Fraig_Regular(pNode->p2)->p1, vSuper, vInside, 0 );
+ Fraig_DetectFanoutFreeConeMux_rec( Fraig_Regular(pNode->p2)->p2, vSuper, vInside, 0 );
+ // add the node
+ Fraig_NodeVecPushUnique( vInside, pNode );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns the array of nodes to be combined into one multi-input AND-gate.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_DetectFanoutFreeConeMux( Fraig_Man_t * pMan, Fraig_Node_t * pNode )
+{
+ Fraig_NodeVec_t * vFanins;
+ Fraig_NodeVec_t * vInside;
+ int nCubes;
+ extern int Fraig_CutSopCountCubes( Fraig_Man_t * pMan, Fraig_NodeVec_t * vFanins, Fraig_NodeVec_t * vInside );
+
+ vFanins = Fraig_NodeVecAlloc( 8 );
+ vInside = Fraig_NodeVecAlloc( 8 );
+
+ Fraig_DetectFanoutFreeConeMux_rec( pNode, vFanins, vInside, 1 );
+ assert( vInside->pArray[vInside->nSize-1] == pNode );
+
+// nCubes = Fraig_CutSopCountCubes( pMan, vFanins, vInside );
+ nCubes = 0;
+
+printf( "%d(%d)", vFanins->nSize, nCubes );
+ Fraig_NodeVecFree( vFanins );
+ Fraig_NodeVecFree( vInside );
+}
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Collect variables using their proximity from the nodes.]
+
+ Description [This procedure creates a variable order based on collecting
+ first the nodes that are the closest to the given two target nodes.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_SetActivity( Fraig_Man_t * pMan, Fraig_Node_t * pOld, Fraig_Node_t * pNew )
+{
+ Fraig_Node_t * pNode;
+ int i, Number, MaxLevel;
+ float * pFactors = Msat_SolverReadFactors(pMan->pSat);
+ if ( pFactors == NULL )
+ return;
+ MaxLevel = FRAIG_MAX( pOld->Level, pNew->Level );
+ // create the variable order
+ for ( i = 0; i < Msat_IntVecReadSize(pMan->vVarsInt); i++ )
+ {
+ // get the new node on the frontier
+ Number = Msat_IntVecReadEntry(pMan->vVarsInt, i);
+ pNode = pMan->vNodes->pArray[Number];
+ pFactors[pNode->Num] = (float)pow( 0.97, MaxLevel - pNode->Level );
+// if ( pNode->Num % 50 == 0 )
+// printf( "(%d) %.2f ", MaxLevel - pNode->Level, pFactors[pNode->Num] );
+ }
+// printf( "\n" );
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/fraig/fraigTable.c b/src/sat/fraig/fraigTable.c
new file mode 100644
index 00000000..b68bbe0e
--- /dev/null
+++ b/src/sat/fraig/fraigTable.c
@@ -0,0 +1,657 @@
+/**CFile****************************************************************
+
+ FileName [fraigTable.c]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [Structural and functional hash tables.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigTable.c,v 1.7 2005/07/08 01:01:34 alanmi Exp $]
+
+***********************************************************************/
+
+#include "fraigInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+static void Fraig_TableResizeS( Fraig_HashTable_t * p );
+static void Fraig_TableResizeF( Fraig_HashTable_t * p, int fUseSimR );
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Allocates the hash table.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_HashTable_t * Fraig_HashTableCreate( int nSize )
+{
+ Fraig_HashTable_t * p;
+ // allocate the table
+ p = ALLOC( Fraig_HashTable_t, 1 );
+ memset( p, 0, sizeof(Fraig_HashTable_t) );
+ // allocate and clean the bins
+ p->nBins = Cudd_PrimeFraig(nSize);
+ p->pBins = ALLOC( Fraig_Node_t *, p->nBins );
+ memset( p->pBins, 0, sizeof(Fraig_Node_t *) * p->nBins );
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Deallocates the supergate hash table.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_HashTableFree( Fraig_HashTable_t * p )
+{
+ FREE( p->pBins );
+ FREE( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Looks up an entry in the structural hash table.]
+
+ Description [If the entry with the same children does not exists,
+ creates it, inserts it into the table, and returns 0. If the entry
+ with the same children exists, finds it, and return 1. In both cases,
+ the new/old entry is returned in ppNodeRes.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_HashTableLookupS( Fraig_Man_t * pMan, Fraig_Node_t * p1, Fraig_Node_t * p2, Fraig_Node_t ** ppNodeRes )
+{
+ Fraig_HashTable_t * p = pMan->pTableS;
+ Fraig_Node_t * pEnt;
+ unsigned Key;
+
+ // order the arguments
+ if ( Fraig_Regular(p1)->Num > Fraig_Regular(p2)->Num )
+ pEnt = p1, p1 = p2, p2 = pEnt;
+
+ Key = Fraig_HashKey2( p1, p2, p->nBins );
+ Fraig_TableBinForEachEntryS( p->pBins[Key], pEnt )
+ if ( pEnt->p1 == p1 && pEnt->p2 == p2 )
+ {
+ *ppNodeRes = pEnt;
+ return 1;
+ }
+ // check if it is a good time for table resizing
+ if ( p->nEntries >= 2 * p->nBins )
+ {
+ Fraig_TableResizeS( p );
+ Key = Fraig_HashKey2( p1, p2, p->nBins );
+ }
+ // create the new node
+ pEnt = Fraig_NodeCreate( pMan, p1, p2 );
+ // add the node to the corresponding linked list in the table
+ pEnt->pNextS = p->pBins[Key];
+ p->pBins[Key] = pEnt;
+ *ppNodeRes = pEnt;
+ p->nEntries++;
+ return 0;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Insert the entry in the functional hash table.]
+
+ Description [If the entry with the same key exists, return it right away.
+ If the entry with the same key does not exists, inserts it and returns NULL. ]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_HashTableLookupF( Fraig_Man_t * pMan, Fraig_Node_t * pNode )
+{
+ Fraig_HashTable_t * p = pMan->pTableF;
+ Fraig_Node_t * pEnt, * pEntD;
+ unsigned Key;
+
+ // go through the hash table entries
+ Key = pNode->uHashR % p->nBins;
+ Fraig_TableBinForEachEntryF( p->pBins[Key], pEnt )
+ {
+ // if their simulation info differs, skip
+ if ( !Fraig_CompareSimInfo( pNode, pEnt, pMan->nWordsRand, 1 ) )
+ continue;
+ // equivalent up to the complement
+ Fraig_TableBinForEachEntryD( pEnt, pEntD )
+ {
+ // if their simulation info differs, skip
+ if ( !Fraig_CompareSimInfo( pNode, pEntD, pMan->iWordStart, 0 ) )
+ continue;
+ // found a simulation-equivalent node
+ return pEntD;
+ }
+ // did not find a simulation equivalent node
+ // add the node to the corresponding linked list
+ pNode->pNextD = pEnt->pNextD;
+ pEnt->pNextD = pNode;
+ // return NULL, because there is no functional equivalence in this case
+ return NULL;
+ }
+
+ // check if it is a good time for table resizing
+ if ( p->nEntries >= 2 * p->nBins )
+ {
+ Fraig_TableResizeF( p, 1 );
+ Key = pNode->uHashR % p->nBins;
+ }
+
+ // add the node to the corresponding linked list in the table
+ pNode->pNextF = p->pBins[Key];
+ p->pBins[Key] = pNode;
+ p->nEntries++;
+ // return NULL, because there is no functional equivalence in this case
+ return NULL;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Insert the entry in the functional hash table.]
+
+ Description [If the entry with the same key exists, return it right away.
+ If the entry with the same key does not exists, inserts it and returns NULL. ]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_HashTableLookupF0( Fraig_Man_t * pMan, Fraig_Node_t * pNode )
+{
+ Fraig_HashTable_t * p = pMan->pTableF0;
+ Fraig_Node_t * pEnt;
+ unsigned Key;
+
+ // go through the hash table entries
+ Key = pNode->uHashD % p->nBins;
+ Fraig_TableBinForEachEntryF( p->pBins[Key], pEnt )
+ {
+ // if their simulation info differs, skip
+ if ( !Fraig_CompareSimInfo( pNode, pEnt, pMan->iWordStart, 0 ) )
+ continue;
+ // found a simulation-equivalent node
+ return pEnt;
+ }
+
+ // check if it is a good time for table resizing
+ if ( p->nEntries >= 2 * p->nBins )
+ {
+ Fraig_TableResizeF( p, 0 );
+ Key = pNode->uHashD % p->nBins;
+ }
+
+ // add the node to the corresponding linked list in the table
+ pNode->pNextF = p->pBins[Key];
+ p->pBins[Key] = pNode;
+ p->nEntries++;
+ // return NULL, because there is no functional equivalence in this case
+ return NULL;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Insert the entry in the functional hash table.]
+
+ Description [Unconditionally add the node to the corresponding
+ linked list in the table.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_HashTableInsertF0( Fraig_Man_t * pMan, Fraig_Node_t * pNode )
+{
+ Fraig_HashTable_t * p = pMan->pTableF0;
+ unsigned Key = pNode->uHashD % p->nBins;
+
+ pNode->pNextF = p->pBins[Key];
+ p->pBins[Key] = pNode;
+ p->nEntries++;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Resizes the table.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_TableResizeS( Fraig_HashTable_t * p )
+{
+ Fraig_Node_t ** pBinsNew;
+ Fraig_Node_t * pEnt, * pEnt2;
+ int nBinsNew, Counter, i, clk;
+ unsigned Key;
+
+clk = clock();
+ // get the new table size
+ nBinsNew = Cudd_PrimeFraig(2 * p->nBins);
+ // allocate a new array
+ pBinsNew = ALLOC( Fraig_Node_t *, nBinsNew );
+ memset( pBinsNew, 0, sizeof(Fraig_Node_t *) * nBinsNew );
+ // rehash the entries from the old table
+ Counter = 0;
+ for ( i = 0; i < p->nBins; i++ )
+ Fraig_TableBinForEachEntrySafeS( p->pBins[i], pEnt, pEnt2 )
+ {
+ Key = Fraig_HashKey2( pEnt->p1, pEnt->p2, nBinsNew );
+ pEnt->pNextS = pBinsNew[Key];
+ pBinsNew[Key] = pEnt;
+ Counter++;
+ }
+ assert( Counter == p->nEntries );
+// printf( "Increasing the structural table size from %6d to %6d. ", p->nBins, nBinsNew );
+// PRT( "Time", clock() - clk );
+ // replace the table and the parameters
+ free( p->pBins );
+ p->pBins = pBinsNew;
+ p->nBins = nBinsNew;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Resizes the table.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_TableResizeF( Fraig_HashTable_t * p, int fUseSimR )
+{
+ Fraig_Node_t ** pBinsNew;
+ Fraig_Node_t * pEnt, * pEnt2;
+ int nBinsNew, Counter, i, clk;
+ unsigned Key;
+
+clk = clock();
+ // get the new table size
+ nBinsNew = Cudd_PrimeFraig(2 * p->nBins);
+ // allocate a new array
+ pBinsNew = ALLOC( Fraig_Node_t *, nBinsNew );
+ memset( pBinsNew, 0, sizeof(Fraig_Node_t *) * nBinsNew );
+ // rehash the entries from the old table
+ Counter = 0;
+ for ( i = 0; i < p->nBins; i++ )
+ Fraig_TableBinForEachEntrySafeF( p->pBins[i], pEnt, pEnt2 )
+ {
+ if ( fUseSimR )
+ Key = pEnt->uHashR % nBinsNew;
+ else
+ Key = pEnt->uHashD % nBinsNew;
+ pEnt->pNextF = pBinsNew[Key];
+ pBinsNew[Key] = pEnt;
+ Counter++;
+ }
+ assert( Counter == p->nEntries );
+// printf( "Increasing the functional table size from %6d to %6d. ", p->nBins, nBinsNew );
+// PRT( "Time", clock() - clk );
+ // replace the table and the parameters
+ free( p->pBins );
+ p->pBins = pBinsNew;
+ p->nBins = nBinsNew;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Compares two pieces of simulation info.]
+
+ Description [Returns 1 if they are equal.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_CompareSimInfo( Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int iWordLast, int fUseRand )
+{
+ int i;
+ assert( !Fraig_IsComplement(pNode1) );
+ assert( !Fraig_IsComplement(pNode2) );
+ if ( fUseRand )
+ {
+ // if their signatures differ, skip
+ if ( pNode1->uHashR != pNode2->uHashR )
+ return 0;
+ // check the simulation info
+ for ( i = 0; i < iWordLast; i++ )
+ if ( pNode1->puSimR[i] != pNode2->puSimR[i] )
+ return 0;
+ }
+ else
+ {
+ // if their signatures differ, skip
+ if ( pNode1->uHashD != pNode2->uHashD )
+ return 0;
+ // check the simulation info
+ for ( i = 0; i < iWordLast; i++ )
+ if ( pNode1->puSimD[i] != pNode2->puSimD[i] )
+ return 0;
+ }
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Find the number of the different pattern.]
+
+ Description [Returns -1 if there is no such pattern]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_FindFirstDiff( Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int fCompl, int iWordLast, int fUseRand )
+{
+ int i, v;
+ assert( !Fraig_IsComplement(pNode1) );
+ assert( !Fraig_IsComplement(pNode2) );
+ // take into account possible internal complementation
+ fCompl ^= pNode1->fInv;
+ fCompl ^= pNode2->fInv;
+ // find the pattern
+ if ( fCompl )
+ {
+ if ( fUseRand )
+ {
+ for ( i = 0; i < iWordLast; i++ )
+ if ( pNode1->puSimR[i] != ~pNode2->puSimR[i] )
+ for ( v = 0; v < 32; v++ )
+ if ( (pNode1->puSimR[i] ^ ~pNode2->puSimR[i]) & (1 << v) )
+ return i * 32 + v;
+ }
+ else
+ {
+ for ( i = 0; i < iWordLast; i++ )
+ if ( pNode1->puSimD[i] != ~pNode2->puSimD[i] )
+ for ( v = 0; v < 32; v++ )
+ if ( (pNode1->puSimD[i] ^ ~pNode2->puSimD[i]) & (1 << v) )
+ return i * 32 + v;
+ }
+ }
+ else
+ {
+ if ( fUseRand )
+ {
+ for ( i = 0; i < iWordLast; i++ )
+ if ( pNode1->puSimR[i] != pNode2->puSimR[i] )
+ for ( v = 0; v < 32; v++ )
+ if ( (pNode1->puSimR[i] ^ pNode2->puSimR[i]) & (1 << v) )
+ return i * 32 + v;
+ }
+ else
+ {
+ for ( i = 0; i < iWordLast; i++ )
+ if ( pNode1->puSimD[i] != pNode2->puSimD[i] )
+ for ( v = 0; v < 32; v++ )
+ if ( (pNode1->puSimD[i] ^ pNode2->puSimD[i]) & (1 << v) )
+ return i * 32 + v;
+ }
+ }
+ return -1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Compares two pieces of simulation info.]
+
+ Description [Returns 1 if they are equal.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_CompareSimInfoUnderMask( Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int iWordLast, int fUseRand, unsigned * puMask )
+{
+ unsigned * pSims1, * pSims2;
+ int i;
+ assert( !Fraig_IsComplement(pNode1) );
+ assert( !Fraig_IsComplement(pNode2) );
+ // get hold of simulation info
+ pSims1 = fUseRand? pNode1->puSimR : pNode1->puSimD;
+ pSims2 = fUseRand? pNode2->puSimR : pNode2->puSimD;
+ // check the simulation info
+ for ( i = 0; i < iWordLast; i++ )
+ if ( (pSims1[i] & puMask[i]) != (pSims2[i] & puMask[i]) )
+ return 0;
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Compares two pieces of simulation info.]
+
+ Description [Returns 1 if they are equal.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_CollectXors( Fraig_Node_t * pNode1, Fraig_Node_t * pNode2, int iWordLast, int fUseRand, unsigned * puMask )
+{
+ unsigned * pSims1, * pSims2;
+ int i;
+ assert( !Fraig_IsComplement(pNode1) );
+ assert( !Fraig_IsComplement(pNode2) );
+ // get hold of simulation info
+ pSims1 = fUseRand? pNode1->puSimR : pNode1->puSimD;
+ pSims2 = fUseRand? pNode2->puSimR : pNode2->puSimD;
+ // check the simulation info
+ for ( i = 0; i < iWordLast; i++ )
+ puMask[i] = ( pSims1[i] ^ pSims2[i] );
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Prints stats of the structural table.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_TablePrintStatsS( Fraig_Man_t * pMan )
+{
+ Fraig_HashTable_t * pT = pMan->pTableS;
+ Fraig_Node_t * pNode;
+ int i, Counter;
+
+ printf( "Structural table. Table size = %d. Number of entries = %d.\n", pT->nBins, pT->nEntries );
+ for ( i = 0; i < pT->nBins; i++ )
+ {
+ Counter = 0;
+ Fraig_TableBinForEachEntryS( pT->pBins[i], pNode )
+ Counter++;
+ if ( Counter > 1 )
+ {
+ printf( "%d ", Counter );
+ if ( Counter > 50 )
+ printf( "{%d} ", i );
+ }
+ }
+ printf( "\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prints stats of the structural table.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_TablePrintStatsF( Fraig_Man_t * pMan )
+{
+ Fraig_HashTable_t * pT = pMan->pTableF;
+ Fraig_Node_t * pNode;
+ int i, Counter;
+
+ printf( "Functional table. Table size = %d. Number of entries = %d.\n", pT->nBins, pT->nEntries );
+ for ( i = 0; i < pT->nBins; i++ )
+ {
+ Counter = 0;
+ Fraig_TableBinForEachEntryF( pT->pBins[i], pNode )
+ Counter++;
+ if ( Counter > 1 )
+ printf( "{%d} ", Counter );
+ }
+ printf( "\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prints stats of the structural table.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_TablePrintStatsF0( Fraig_Man_t * pMan )
+{
+ Fraig_HashTable_t * pT = pMan->pTableF0;
+ Fraig_Node_t * pNode;
+ int i, Counter;
+
+ printf( "Zero-node table. Table size = %d. Number of entries = %d.\n", pT->nBins, pT->nEntries );
+ for ( i = 0; i < pT->nBins; i++ )
+ {
+ Counter = 0;
+ Fraig_TableBinForEachEntryF( pT->pBins[i], pNode )
+ Counter++;
+ if ( Counter == 0 )
+ continue;
+/*
+ printf( "\nBin = %4d : Number of entries = %4d\n", i, Counter );
+ Fraig_TableBinForEachEntryF( pT->pBins[i], pNode )
+ printf( "Node %5d. Hash = %10d.\n", pNode->Num, pNode->uHashD );
+*/
+ }
+ printf( "\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Rehashes the table after the simulation info has changed.]
+
+ Description [Assumes that the hash values have been updated after performing
+ additional simulation. Rehashes the table using the new hash values.
+ Uses pNextF to link the entries in the bins. Uses pNextD to link the entries
+ with identical hash values. Returns 1 if the identical entries have been found.
+ Note that identical hash values may mean that the simulation data is different.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_TableRehashF0( Fraig_Man_t * pMan, int fLinkEquiv )
+{
+ Fraig_HashTable_t * pT = pMan->pTableF0;
+ Fraig_Node_t ** pBinsNew;
+ Fraig_Node_t * pEntF, * pEntF2, * pEnt, * pEntD2, * pEntN;
+ int ReturnValue, Counter, i;
+ unsigned Key;
+
+ // allocate a new array of bins
+ pBinsNew = ALLOC( Fraig_Node_t *, pT->nBins );
+ memset( pBinsNew, 0, sizeof(Fraig_Node_t *) * pT->nBins );
+
+ // rehash the entries in the table
+ // go through all the nodes in the F-lists (and possible in D-lists, if used)
+ Counter = 0;
+ ReturnValue = 0;
+ for ( i = 0; i < pT->nBins; i++ )
+ Fraig_TableBinForEachEntrySafeF( pT->pBins[i], pEntF, pEntF2 )
+ Fraig_TableBinForEachEntrySafeD( pEntF, pEnt, pEntD2 )
+ {
+ // decide where to put entry pEnt
+ Key = pEnt->uHashD % pT->nBins;
+ if ( fLinkEquiv )
+ {
+ // go through the entries in the new bin
+ Fraig_TableBinForEachEntryF( pBinsNew[Key], pEntN )
+ {
+ // if they have different values skip
+ if ( pEnt->uHashD != pEntN->uHashD )
+ continue;
+ // they have the same hash value, add pEnt to the D-list pEnt3
+ pEnt->pNextD = pEntN->pNextD;
+ pEntN->pNextD = pEnt;
+ ReturnValue = 1;
+ Counter++;
+ break;
+ }
+ if ( pEntN != NULL ) // already linked
+ continue;
+ // we did not find equal entry
+ }
+ // link the new entry
+ pEnt->pNextF = pBinsNew[Key];
+ pBinsNew[Key] = pEnt;
+ pEnt->pNextD = NULL;
+ Counter++;
+ }
+ assert( Counter == pT->nEntries );
+ // replace the table and the parameters
+ free( pT->pBins );
+ pT->pBins = pBinsNew;
+ return ReturnValue;
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/fraig/fraigUtil.c b/src/sat/fraig/fraigUtil.c
new file mode 100644
index 00000000..342a7111
--- /dev/null
+++ b/src/sat/fraig/fraigUtil.c
@@ -0,0 +1,1034 @@
+/**CFile****************************************************************
+
+ FileName [fraigUtil.c]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [Various utilities.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigUtil.c,v 1.15 2005/07/08 01:01:34 alanmi Exp $]
+
+***********************************************************************/
+
+#include "fraigInt.h"
+#include <limits.h>
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+static int bit_count[256] = {
+ 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,
+ 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,
+ 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,
+ 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,
+ 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,
+ 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,
+ 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,
+ 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
+};
+
+static void Fraig_Dfs_rec( Fraig_Man_t * pMan, Fraig_Node_t * pNode, Fraig_NodeVec_t * vNodes, int fEquiv );
+static int Fraig_CheckTfi_rec( Fraig_Man_t * pMan, Fraig_Node_t * pNode, Fraig_Node_t * pOld );
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Computes the DFS ordering of the nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_NodeVec_t * Fraig_Dfs( Fraig_Man_t * pMan, int fEquiv )
+{
+ Fraig_NodeVec_t * vNodes;
+ int i;
+ pMan->nTravIds++;
+ vNodes = Fraig_NodeVecAlloc( 100 );
+ for ( i = 0; i < pMan->vOutputs->nSize; i++ )
+ Fraig_Dfs_rec( pMan, Fraig_Regular(pMan->vOutputs->pArray[i]), vNodes, fEquiv );
+ return vNodes;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Computes the DFS ordering of the nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_NodeVec_t * Fraig_DfsOne( Fraig_Man_t * pMan, Fraig_Node_t * pNode, int fEquiv )
+{
+ Fraig_NodeVec_t * vNodes;
+ pMan->nTravIds++;
+ vNodes = Fraig_NodeVecAlloc( 100 );
+ Fraig_Dfs_rec( pMan, Fraig_Regular(pNode), vNodes, fEquiv );
+ return vNodes;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Computes the DFS ordering of the nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_NodeVec_t * Fraig_DfsNodes( Fraig_Man_t * pMan, Fraig_Node_t ** ppNodes, int nNodes, int fEquiv )
+{
+ Fraig_NodeVec_t * vNodes;
+ int i;
+ pMan->nTravIds++;
+ vNodes = Fraig_NodeVecAlloc( 100 );
+ for ( i = 0; i < nNodes; i++ )
+ Fraig_Dfs_rec( pMan, Fraig_Regular(ppNodes[i]), vNodes, fEquiv );
+ return vNodes;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Recursively computes the DFS ordering of the nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_Dfs_rec( Fraig_Man_t * pMan, Fraig_Node_t * pNode, Fraig_NodeVec_t * vNodes, int fEquiv )
+{
+ assert( !Fraig_IsComplement(pNode) );
+ // skip the visited node
+ if ( pNode->TravId == pMan->nTravIds )
+ return;
+ pNode->TravId = pMan->nTravIds;
+ // visit the transitive fanin
+ if ( Fraig_NodeIsAnd(pNode) )
+ {
+ Fraig_Dfs_rec( pMan, Fraig_Regular(pNode->p1), vNodes, fEquiv );
+ Fraig_Dfs_rec( pMan, Fraig_Regular(pNode->p2), vNodes, fEquiv );
+ }
+ if ( fEquiv && pNode->pNextE )
+ Fraig_Dfs_rec( pMan, pNode->pNextE, vNodes, fEquiv );
+ // save the node
+ Fraig_NodeVecPush( vNodes, pNode );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Computes the DFS ordering of the nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_CountNodes( Fraig_Man_t * pMan, int fEquiv )
+{
+ Fraig_NodeVec_t * vNodes;
+ int RetValue;
+ vNodes = Fraig_Dfs( pMan, fEquiv );
+ RetValue = vNodes->nSize;
+ Fraig_NodeVecFree( vNodes );
+ return RetValue;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns 1 if pOld is in the TFI of pNew.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_CheckTfi( Fraig_Man_t * pMan, Fraig_Node_t * pOld, Fraig_Node_t * pNew )
+{
+ assert( !Fraig_IsComplement(pOld) );
+ assert( !Fraig_IsComplement(pNew) );
+ pMan->nTravIds++;
+ return Fraig_CheckTfi_rec( pMan, pNew, pOld );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns 1 if pOld is in the TFI of pNew.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_CheckTfi_rec( Fraig_Man_t * pMan, Fraig_Node_t * pNode, Fraig_Node_t * pOld )
+{
+ // check the trivial cases
+ if ( pNode == NULL )
+ return 0;
+ if ( pNode->Num < pOld->Num && !pMan->fChoicing )
+ return 0;
+ if ( pNode == pOld )
+ return 1;
+ // skip the visited node
+ if ( pNode->TravId == pMan->nTravIds )
+ return 0;
+ pNode->TravId = pMan->nTravIds;
+ // check the children
+ if ( Fraig_CheckTfi_rec( pMan, Fraig_Regular(pNode->p1), pOld ) )
+ return 1;
+ if ( Fraig_CheckTfi_rec( pMan, Fraig_Regular(pNode->p2), pOld ) )
+ return 1;
+ // check equivalent nodes
+ return Fraig_CheckTfi_rec( pMan, pNode->pNextE, pOld );
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Returns 1 if pOld is in the TFI of pNew.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_CheckTfi2( Fraig_Man_t * pMan, Fraig_Node_t * pOld, Fraig_Node_t * pNew )
+{
+ Fraig_NodeVec_t * vNodes;
+ int RetValue;
+ vNodes = Fraig_DfsOne( pMan, pNew, 1 );
+ RetValue = (pOld->TravId == pMan->nTravIds);
+ Fraig_NodeVecFree( vNodes );
+ return RetValue;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sets the number of fanouts (none, one, or many).]
+
+ Description [This procedure collects the nodes reachable from
+ the POs of the AIG and sets the type of fanout counter (none, one,
+ or many) for each node. This procedure is useful to determine
+ fanout-free cones of AND-nodes, which is helpful for rebalancing
+ the AIG (see procedure Fraig_ManRebalance, or something like that).]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ManMarkRealFanouts( Fraig_Man_t * p )
+{
+ Fraig_NodeVec_t * vNodes;
+ Fraig_Node_t * pNodeR;
+ int i;
+ // collect the nodes reachable
+ vNodes = Fraig_Dfs( p, 0 );
+ // clean the fanouts field
+ for ( i = 0; i < vNodes->nSize; i++ )
+ {
+ vNodes->pArray[i]->nFanouts = 0;
+ vNodes->pArray[i]->pData0 = NULL;
+ }
+ // mark reachable nodes by setting the two-bit counter pNode->nFans
+ for ( i = 0; i < vNodes->nSize; i++ )
+ {
+ pNodeR = Fraig_Regular(vNodes->pArray[i]->p1);
+ if ( pNodeR && ++pNodeR->nFanouts == 3 )
+ pNodeR->nFanouts = 2;
+ pNodeR = Fraig_Regular(vNodes->pArray[i]->p2);
+ if ( pNodeR && ++pNodeR->nFanouts == 3 )
+ pNodeR->nFanouts = 2;
+ }
+ Fraig_NodeVecFree( vNodes );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Creates the constant 1 node.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_BitStringCountOnes( unsigned * pString, int nWords )
+{
+ unsigned char * pSuppBytes = (unsigned char *)pString;
+ int i, nOnes, nBytes = sizeof(unsigned) * nWords;
+ // count the number of ones in the simulation vector
+ for ( i = nOnes = 0; i < nBytes; i++ )
+ nOnes += bit_count[pSuppBytes[i]];
+ return nOnes;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Verify one useful property.]
+
+ Description [This procedure verifies one useful property. After
+ the FRAIG construction with choice nodes is over, each primary node
+ should have fanins that are primary nodes. The primary nodes is the
+ one that does not have pNode->pRepr set to point to another node.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_ManCheckConsistency( Fraig_Man_t * p )
+{
+ Fraig_Node_t * pNode;
+ Fraig_NodeVec_t * pVec;
+ int i;
+ pVec = Fraig_Dfs( p, 0 );
+ for ( i = 0; i < pVec->nSize; i++ )
+ {
+ pNode = pVec->pArray[i];
+ if ( Fraig_NodeIsVar(pNode) )
+ {
+ if ( pNode->pRepr )
+ printf( "Primary input %d is a secondary node.\n", pNode->Num );
+ }
+ else if ( Fraig_NodeIsConst(pNode) )
+ {
+ if ( pNode->pRepr )
+ printf( "Constant 1 %d is a secondary node.\n", pNode->Num );
+ }
+ else
+ {
+ if ( pNode->pRepr )
+ printf( "Internal node %d is a secondary node.\n", pNode->Num );
+ if ( Fraig_Regular(pNode->p1)->pRepr )
+ printf( "Internal node %d has first fanin %d that is a secondary node.\n",
+ pNode->Num, Fraig_Regular(pNode->p1)->Num );
+ if ( Fraig_Regular(pNode->p2)->pRepr )
+ printf( "Internal node %d has second fanin %d that is a secondary node.\n",
+ pNode->Num, Fraig_Regular(pNode->p2)->Num );
+ }
+ }
+ Fraig_NodeVecFree( pVec );
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prints the node.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_PrintNode( Fraig_Man_t * p, Fraig_Node_t * pNode )
+{
+ Fraig_NodeVec_t * vNodes;
+ Fraig_Node_t * pTemp;
+ int fCompl1, fCompl2, i;
+
+ vNodes = Fraig_DfsOne( p, pNode, 0 );
+ for ( i = 0; i < vNodes->nSize; i++ )
+ {
+ pTemp = vNodes->pArray[i];
+ if ( Fraig_NodeIsVar(pTemp) )
+ {
+ printf( "%3d : PI ", pTemp->Num );
+ Fraig_PrintBinary( stdout, (unsigned *)&pTemp->puSimR, 20 );
+ printf( " " );
+ Fraig_PrintBinary( stdout, (unsigned *)&pTemp->puSimD, 20 );
+ printf( " %d\n", pTemp->fInv );
+ continue;
+ }
+
+ fCompl1 = Fraig_IsComplement(pTemp->p1);
+ fCompl2 = Fraig_IsComplement(pTemp->p2);
+ printf( "%3d : %c%3d %c%3d ", pTemp->Num,
+ (fCompl1? '-':'+'), Fraig_Regular(pTemp->p1)->Num,
+ (fCompl2? '-':'+'), Fraig_Regular(pTemp->p2)->Num );
+ Fraig_PrintBinary( stdout, (unsigned *)&pTemp->puSimR, 20 );
+ printf( " " );
+ Fraig_PrintBinary( stdout, (unsigned *)&pTemp->puSimD, 20 );
+ printf( " %d\n", pTemp->fInv );
+ }
+ Fraig_NodeVecFree( vNodes );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prints the bit string.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_PrintBinary( FILE * pFile, unsigned * pSign, int nBits )
+{
+ int Remainder, nWords;
+ int w, i;
+
+ Remainder = (nBits%(sizeof(unsigned)*8));
+ nWords = (nBits/(sizeof(unsigned)*8)) + (Remainder>0);
+
+ for ( w = nWords-1; w >= 0; w-- )
+ for ( i = ((w == nWords-1 && Remainder)? Remainder-1: 31); i >= 0; i-- )
+ fprintf( pFile, "%c", '0' + (int)((pSign[w] & (1<<i)) > 0) );
+
+// fprintf( pFile, "\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sets up the mask.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_GetMaxLevel( Fraig_Man_t * pMan )
+{
+ int nLevelMax, i;
+ nLevelMax = 0;
+ for ( i = 0; i < pMan->vOutputs->nSize; i++ )
+ nLevelMax = nLevelMax > Fraig_Regular(pMan->vOutputs->pArray[i])->Level?
+ nLevelMax : Fraig_Regular(pMan->vOutputs->pArray[i])->Level;
+ return nLevelMax;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Analyses choice nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_MappingUpdateLevel_rec( Fraig_Man_t * pMan, Fraig_Node_t * pNode, int fMaximum )
+{
+ Fraig_Node_t * pTemp;
+ int Level1, Level2, LevelE;
+ assert( !Fraig_IsComplement(pNode) );
+ if ( !Fraig_NodeIsAnd(pNode) )
+ return pNode->Level;
+ // skip the visited node
+ if ( pNode->TravId == pMan->nTravIds )
+ return pNode->Level;
+ pNode->TravId = pMan->nTravIds;
+ // compute levels of the children nodes
+ Level1 = Fraig_MappingUpdateLevel_rec( pMan, Fraig_Regular(pNode->p1), fMaximum );
+ Level2 = Fraig_MappingUpdateLevel_rec( pMan, Fraig_Regular(pNode->p2), fMaximum );
+ pNode->Level = 1 + FRAIG_MAX( Level1, Level2 );
+ if ( pNode->pNextE )
+ {
+ LevelE = Fraig_MappingUpdateLevel_rec( pMan, pNode->pNextE, fMaximum );
+ if ( fMaximum )
+ {
+ if ( pNode->Level < LevelE )
+ pNode->Level = LevelE;
+ }
+ else
+ {
+ if ( pNode->Level > LevelE )
+ pNode->Level = LevelE;
+ }
+ // set the level of all equivalent nodes to be the same minimum
+ if ( pNode->pRepr == NULL ) // the primary node
+ for ( pTemp = pNode->pNextE; pTemp; pTemp = pTemp->pNextE )
+ pTemp->Level = pNode->Level;
+ }
+ return pNode->Level;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Resets the levels of the nodes in the choice graph.]
+
+ Description [Makes the level of the choice nodes to be equal to the
+ maximum of the level of the nodes in the equivalence class. This way
+ sorting by level leads to the reverse topological order, which is
+ needed for the required time computation.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_MappingSetChoiceLevels( Fraig_Man_t * pMan, int fMaximum )
+{
+ int i;
+ pMan->nTravIds++;
+ for ( i = 0; i < pMan->vOutputs->nSize; i++ )
+ Fraig_MappingUpdateLevel_rec( pMan, Fraig_Regular(pMan->vOutputs->pArray[i]), fMaximum );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reports statistics on choice nodes.]
+
+ Description [The number of choice nodes is the number of primary nodes,
+ which has pNextE set to a pointer. The number of choices is the number
+ of entries in the equivalent-node lists of the primary nodes.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ManReportChoices( Fraig_Man_t * pMan )
+{
+ Fraig_Node_t * pNode, * pTemp;
+ int nChoiceNodes, nChoices;
+ int i, LevelMax1, LevelMax2;
+
+ // report the number of levels
+ LevelMax1 = Fraig_GetMaxLevel( pMan );
+ Fraig_MappingSetChoiceLevels( pMan, 0 );
+ LevelMax2 = Fraig_GetMaxLevel( pMan );
+
+ // report statistics about choices
+ nChoiceNodes = nChoices = 0;
+ for ( i = 0; i < pMan->vNodes->nSize; i++ )
+ {
+ pNode = pMan->vNodes->pArray[i];
+ if ( pNode->pRepr == NULL && pNode->pNextE != NULL )
+ { // this is a choice node = the primary node that has equivalent nodes
+ nChoiceNodes++;
+ for ( pTemp = pNode; pTemp; pTemp = pTemp->pNextE )
+ nChoices++;
+ }
+ }
+ printf( "Maximum level: Original = %d. Reduced due to choices = %d.\n", LevelMax1, LevelMax2 );
+ printf( "Choice stats: Choice nodes = %d. Total choices = %d.\n", nChoiceNodes, nChoices );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns 1 if the node is the root of EXOR/NEXOR gate.]
+
+ Description [The node can be complemented.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeIsExorType( Fraig_Node_t * pNode )
+{
+ Fraig_Node_t * pNode1, * pNode2;
+ // make the node regular (it does not matter for EXOR/NEXOR)
+ pNode = Fraig_Regular(pNode);
+ // if the node or its children are not ANDs or not compl, this cannot be EXOR type
+ if ( !Fraig_NodeIsAnd(pNode) )
+ return 0;
+ if ( !Fraig_NodeIsAnd(pNode->p1) || !Fraig_IsComplement(pNode->p1) )
+ return 0;
+ if ( !Fraig_NodeIsAnd(pNode->p2) || !Fraig_IsComplement(pNode->p2) )
+ return 0;
+
+ // get children
+ pNode1 = Fraig_Regular(pNode->p1);
+ pNode2 = Fraig_Regular(pNode->p2);
+ assert( pNode1->Num < pNode2->Num );
+
+ // compare grandchildren
+ return pNode1->p1 == Fraig_Not(pNode2->p1) && pNode1->p2 == Fraig_Not(pNode2->p2);
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns 1 if the node is the root of MUX or EXOR/NEXOR.]
+
+ Description [The node can be complemented.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeIsMuxType( Fraig_Node_t * pNode )
+{
+ Fraig_Node_t * pNode1, * pNode2;
+
+ // make the node regular (it does not matter for EXOR/NEXOR)
+ pNode = Fraig_Regular(pNode);
+ // if the node or its children are not ANDs or not compl, this cannot be EXOR type
+ if ( !Fraig_NodeIsAnd(pNode) )
+ return 0;
+ if ( !Fraig_NodeIsAnd(pNode->p1) || !Fraig_IsComplement(pNode->p1) )
+ return 0;
+ if ( !Fraig_NodeIsAnd(pNode->p2) || !Fraig_IsComplement(pNode->p2) )
+ return 0;
+
+ // get children
+ pNode1 = Fraig_Regular(pNode->p1);
+ pNode2 = Fraig_Regular(pNode->p2);
+ assert( pNode1->Num < pNode2->Num );
+
+ // compare grandchildren
+ // node is an EXOR/NEXOR
+ if ( pNode1->p1 == Fraig_Not(pNode2->p1) && pNode1->p2 == Fraig_Not(pNode2->p2) )
+ return 1;
+
+ // otherwise the node is MUX iff it has a pair of equal grandchildren
+ return pNode1->p1 == Fraig_Not(pNode2->p1) ||
+ pNode1->p1 == Fraig_Not(pNode2->p2) ||
+ pNode1->p2 == Fraig_Not(pNode2->p1) ||
+ pNode1->p2 == Fraig_Not(pNode2->p2);
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns 1 if the node is EXOR, 0 if it is NEXOR.]
+
+ Description [The node should be EXOR type and not complemented.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeIsExor( Fraig_Node_t * pNode )
+{
+ Fraig_Node_t * pNode1;
+ assert( !Fraig_IsComplement(pNode) );
+ assert( Fraig_NodeIsExorType(pNode) );
+ assert( Fraig_IsComplement(pNode->p1) );
+ // get children
+ pNode1 = Fraig_Regular(pNode->p1);
+ return Fraig_IsComplement(pNode1->p1) == Fraig_IsComplement(pNode1->p2);
+}
+
+/**Function*************************************************************
+
+ Synopsis [Recognizes what nodes are control and data inputs of a MUX.]
+
+ Description [If the node is a MUX, returns the control variable C.
+ Assigns nodes T and E to be the then and else variables of the MUX.
+ Node C is never complemented. Nodes T and E can be complemented.
+ This function also recognizes EXOR/NEXOR gates as MUXes.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_NodeRecognizeMux( Fraig_Node_t * pNode, Fraig_Node_t ** ppNodeT, Fraig_Node_t ** ppNodeE )
+{
+ Fraig_Node_t * pNode1, * pNode2;
+ assert( !Fraig_IsComplement(pNode) );
+ assert( Fraig_NodeIsMuxType(pNode) );
+ // get children
+ pNode1 = Fraig_Regular(pNode->p1);
+ pNode2 = Fraig_Regular(pNode->p2);
+ // find the control variable
+ if ( pNode1->p1 == Fraig_Not(pNode2->p1) )
+ {
+ if ( Fraig_IsComplement(pNode1->p1) )
+ { // pNode2->p1 is positive phase of C
+ *ppNodeT = Fraig_Not(pNode2->p2);
+ *ppNodeE = Fraig_Not(pNode1->p2);
+ return pNode2->p1;
+ }
+ else
+ { // pNode1->p1 is positive phase of C
+ *ppNodeT = Fraig_Not(pNode1->p2);
+ *ppNodeE = Fraig_Not(pNode2->p2);
+ return pNode1->p1;
+ }
+ }
+ else if ( pNode1->p1 == Fraig_Not(pNode2->p2) )
+ {
+ if ( Fraig_IsComplement(pNode1->p1) )
+ { // pNode2->p2 is positive phase of C
+ *ppNodeT = Fraig_Not(pNode2->p1);
+ *ppNodeE = Fraig_Not(pNode1->p2);
+ return pNode2->p2;
+ }
+ else
+ { // pNode1->p1 is positive phase of C
+ *ppNodeT = Fraig_Not(pNode1->p2);
+ *ppNodeE = Fraig_Not(pNode2->p1);
+ return pNode1->p1;
+ }
+ }
+ else if ( pNode1->p2 == Fraig_Not(pNode2->p1) )
+ {
+ if ( Fraig_IsComplement(pNode1->p2) )
+ { // pNode2->p1 is positive phase of C
+ *ppNodeT = Fraig_Not(pNode2->p2);
+ *ppNodeE = Fraig_Not(pNode1->p1);
+ return pNode2->p1;
+ }
+ else
+ { // pNode1->p2 is positive phase of C
+ *ppNodeT = Fraig_Not(pNode1->p1);
+ *ppNodeE = Fraig_Not(pNode2->p2);
+ return pNode1->p2;
+ }
+ }
+ else if ( pNode1->p2 == Fraig_Not(pNode2->p2) )
+ {
+ if ( Fraig_IsComplement(pNode1->p2) )
+ { // pNode2->p2 is positive phase of C
+ *ppNodeT = Fraig_Not(pNode2->p1);
+ *ppNodeE = Fraig_Not(pNode1->p1);
+ return pNode2->p2;
+ }
+ else
+ { // pNode1->p2 is positive phase of C
+ *ppNodeT = Fraig_Not(pNode1->p1);
+ *ppNodeE = Fraig_Not(pNode2->p1);
+ return pNode1->p2;
+ }
+ }
+ assert( 0 ); // this is not MUX
+ return NULL;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Counts the number of EXOR type nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_ManCountExors( Fraig_Man_t * pMan )
+{
+ int i, nExors;
+ nExors = 0;
+ for ( i = 0; i < pMan->vNodes->nSize; i++ )
+ nExors += Fraig_NodeIsExorType( pMan->vNodes->pArray[i] );
+ return nExors;
+
+}
+
+/**Function*************************************************************
+
+ Synopsis [Counts the number of EXOR type nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_ManCountMuxes( Fraig_Man_t * pMan )
+{
+ int i, nMuxes;
+ nMuxes = 0;
+ for ( i = 0; i < pMan->vNodes->nSize; i++ )
+ nMuxes += Fraig_NodeIsMuxType( pMan->vNodes->pArray[i] );
+ return nMuxes;
+
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns 1 if siminfo of Node1 is contained in siminfo of Node2.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeSimsContained( Fraig_Man_t * pMan, Fraig_Node_t * pNode1, Fraig_Node_t * pNode2 )
+{
+ unsigned * pUnsigned1, * pUnsigned2;
+ int i;
+
+ // compare random siminfo
+ pUnsigned1 = pNode1->puSimR;
+ pUnsigned2 = pNode2->puSimR;
+ for ( i = 0; i < pMan->nWordsRand; i++ )
+ if ( pUnsigned1[i] & ~pUnsigned2[i] )
+ return 0;
+
+ // compare systematic siminfo
+ pUnsigned1 = pNode1->puSimD;
+ pUnsigned2 = pNode2->puSimD;
+ for ( i = 0; i < pMan->iWordStart; i++ )
+ if ( pUnsigned1[i] & ~pUnsigned2[i] )
+ return 0;
+
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Count the number of PI variables.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_CountPis( Fraig_Man_t * p, Msat_IntVec_t * vVarNums )
+{
+ int * pVars, nVars, i, Counter;
+
+ nVars = Msat_IntVecReadSize(vVarNums);
+ pVars = Msat_IntVecReadArray(vVarNums);
+ Counter = 0;
+ for ( i = 0; i < nVars; i++ )
+ Counter += Fraig_NodeIsVar( p->vNodes->pArray[pVars[i]] );
+ return Counter;
+}
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Counts the number of EXOR type nodes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_ManPrintRefs( Fraig_Man_t * pMan )
+{
+ Fraig_NodeVec_t * vPivots;
+ Fraig_Node_t * pNode, * pNode2;
+ int i, k, Counter, nProved;
+ int clk;
+
+ vPivots = Fraig_NodeVecAlloc( 1000 );
+ for ( i = 0; i < pMan->vNodes->nSize; i++ )
+ {
+ pNode = pMan->vNodes->pArray[i];
+
+ if ( pNode->nOnes == 0 || pNode->nOnes == (unsigned)pMan->nWordsRand * 32 )
+ continue;
+
+ if ( pNode->nRefs > 5 )
+ {
+ Fraig_NodeVecPush( vPivots, pNode );
+// printf( "Node %6d : nRefs = %2d Level = %3d.\n", pNode->Num, pNode->nRefs, pNode->Level );
+ }
+ }
+ printf( "Total nodes = %d. Referenced nodes = %d.\n", pMan->vNodes->nSize, vPivots->nSize );
+
+clk = clock();
+ // count implications
+ Counter = nProved = 0;
+ for ( i = 0; i < vPivots->nSize; i++ )
+ for ( k = i+1; k < vPivots->nSize; k++ )
+ {
+ pNode = vPivots->pArray[i];
+ pNode2 = vPivots->pArray[k];
+ if ( Fraig_NodeSimsContained( pMan, pNode, pNode2 ) )
+ {
+ if ( Fraig_NodeIsImplication( pMan, pNode, pNode2, -1 ) )
+ nProved++;
+ Counter++;
+ }
+ else if ( Fraig_NodeSimsContained( pMan, pNode2, pNode ) )
+ {
+ if ( Fraig_NodeIsImplication( pMan, pNode2, pNode, -1 ) )
+ nProved++;
+ Counter++;
+ }
+ }
+ printf( "Number of candidate pairs = %d. Proved = %d.\n", Counter, nProved );
+PRT( "Time", clock() - clk );
+ return 0;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Checks if pNew exists among the implication fanins of pOld.]
+
+ Description [If pNew is an implication fanin of pOld, returns 1.
+ If Fraig_Not(pNew) is an implication fanin of pOld, return -1.
+ Otherwise returns 0.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeIsInSupergate( Fraig_Node_t * pOld, Fraig_Node_t * pNew )
+{
+ int RetValue1, RetValue2;
+ if ( Fraig_Regular(pOld) == Fraig_Regular(pNew) )
+ return (pOld == pNew)? 1 : -1;
+ if ( Fraig_IsComplement(pOld) || Fraig_NodeIsVar(pOld) )
+ return 0;
+ RetValue1 = Fraig_NodeIsInSupergate( pOld->p1, pNew );
+ RetValue2 = Fraig_NodeIsInSupergate( pOld->p2, pNew );
+ if ( RetValue1 == -1 || RetValue2 == -1 )
+ return -1;
+ if ( RetValue1 == 1 || RetValue2 == 1 )
+ return 1;
+ return 0;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Returns the array of nodes to be combined into one multi-input AND-gate.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_CollectSupergate_rec( Fraig_Node_t * pNode, Fraig_NodeVec_t * vSuper, int fFirst, int fStopAtMux )
+{
+ // if the new node is complemented or a PI, another gate begins
+// if ( Fraig_IsComplement(pNode) || Fraig_NodeIsVar(pNode) || Fraig_NodeIsMuxType(pNode) )
+ if ( (!fFirst && Fraig_Regular(pNode)->nRefs > 1) ||
+ Fraig_IsComplement(pNode) || Fraig_NodeIsVar(pNode) ||
+ (fStopAtMux && Fraig_NodeIsMuxType(pNode)) )
+ {
+ Fraig_NodeVecPushUnique( vSuper, pNode );
+ return;
+ }
+ // go through the branches
+ Fraig_CollectSupergate_rec( pNode->p1, vSuper, 0, fStopAtMux );
+ Fraig_CollectSupergate_rec( pNode->p2, vSuper, 0, fStopAtMux );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns the array of nodes to be combined into one multi-input AND-gate.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_NodeVec_t * Fraig_CollectSupergate( Fraig_Node_t * pNode, int fStopAtMux )
+{
+ Fraig_NodeVec_t * vSuper;
+ vSuper = Fraig_NodeVecAlloc( 8 );
+ Fraig_CollectSupergate_rec( pNode, vSuper, 1, fStopAtMux );
+ return vSuper;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ManIncrementTravId( Fraig_Man_t * pMan )
+{
+ pMan->nTravIds2++;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeSetTravIdCurrent( Fraig_Man_t * pMan, Fraig_Node_t * pNode )
+{
+ pNode->TravId2 = pMan->nTravIds2;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeIsTravIdCurrent( Fraig_Man_t * pMan, Fraig_Node_t * pNode )
+{
+ return pNode->TravId2 == pMan->nTravIds2;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeIsTravIdPrevious( Fraig_Man_t * pMan, Fraig_Node_t * pNode )
+{
+ return pNode->TravId2 == pMan->nTravIds2 - 1;
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/fraig/fraigVec.c b/src/sat/fraig/fraigVec.c
new file mode 100644
index 00000000..ba3feecd
--- /dev/null
+++ b/src/sat/fraig/fraigVec.c
@@ -0,0 +1,545 @@
+/**CFile****************************************************************
+
+ FileName [fraigVec.c]
+
+ PackageName [FRAIG: Functionally reduced AND-INV graphs.]
+
+ Synopsis [Vector of FRAIG nodes.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 2.0. Started - October 1, 2004]
+
+ Revision [$Id: fraigVec.c,v 1.7 2005/07/08 01:01:34 alanmi Exp $]
+
+***********************************************************************/
+
+#include "fraigInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Allocates a vector with the given capacity.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_NodeVec_t * Fraig_NodeVecAlloc( int nCap )
+{
+ Fraig_NodeVec_t * p;
+ p = ALLOC( Fraig_NodeVec_t, 1 );
+ if ( nCap > 0 && nCap < 8 )
+ nCap = 8;
+ p->nSize = 0;
+ p->nCap = nCap;
+ p->pArray = p->nCap? ALLOC( Fraig_Node_t *, p->nCap ) : NULL;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeVecFree( Fraig_NodeVec_t * p )
+{
+ FREE( p->pArray );
+ FREE( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Duplicates the integer array.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_NodeVec_t * Fraig_NodeVecDup( Fraig_NodeVec_t * pVec )
+{
+ Fraig_NodeVec_t * p;
+ p = ALLOC( Fraig_NodeVec_t, 1 );
+ p->nSize = pVec->nSize;
+ p->nCap = pVec->nCap;
+ p->pArray = p->nCap? ALLOC( Fraig_Node_t *, p->nCap ) : NULL;
+ memcpy( p->pArray, pVec->pArray, sizeof(Fraig_Node_t *) * pVec->nSize );
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t ** Fraig_NodeVecReadArray( Fraig_NodeVec_t * p )
+{
+ return p->pArray;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeVecReadSize( Fraig_NodeVec_t * p )
+{
+ return p->nSize;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Resizes the vector to the given capacity.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeVecGrow( Fraig_NodeVec_t * p, int nCapMin )
+{
+ if ( p->nCap >= nCapMin )
+ return;
+ p->pArray = REALLOC( Fraig_Node_t *, p->pArray, nCapMin );
+ p->nCap = nCapMin;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeVecShrink( Fraig_NodeVec_t * p, int nSizeNew )
+{
+ assert( p->nSize >= nSizeNew );
+ p->nSize = nSizeNew;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeVecClear( Fraig_NodeVec_t * p )
+{
+ p->nSize = 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeVecPush( Fraig_NodeVec_t * p, Fraig_Node_t * Entry )
+{
+ if ( p->nSize == p->nCap )
+ {
+ if ( p->nCap < 16 )
+ Fraig_NodeVecGrow( p, 16 );
+ else
+ Fraig_NodeVecGrow( p, 2 * p->nCap );
+ }
+ p->pArray[p->nSize++] = Entry;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Add the element while ensuring uniqueness.]
+
+ Description [Returns 1 if the element was found, and 0 if it was new. ]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeVecPushUnique( Fraig_NodeVec_t * p, Fraig_Node_t * Entry )
+{
+ int i;
+ for ( i = 0; i < p->nSize; i++ )
+ if ( p->pArray[i] == Entry )
+ return 1;
+ Fraig_NodeVecPush( p, Entry );
+ return 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Inserts a new node in the order by arrival times.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeVecPushOrder( Fraig_NodeVec_t * p, Fraig_Node_t * pNode )
+{
+ Fraig_Node_t * pNode1, * pNode2;
+ int i;
+ Fraig_NodeVecPush( p, pNode );
+ // find the p of the node
+ for ( i = p->nSize-1; i > 0; i-- )
+ {
+ pNode1 = p->pArray[i ];
+ pNode2 = p->pArray[i-1];
+ if ( pNode1 >= pNode2 )
+ break;
+ p->pArray[i ] = pNode2;
+ p->pArray[i-1] = pNode1;
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Add the element while ensuring uniqueness in the order.]
+
+ Description [Returns 1 if the element was found, and 0 if it was new. ]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeVecPushUniqueOrder( Fraig_NodeVec_t * p, Fraig_Node_t * pNode )
+{
+ int i;
+ for ( i = 0; i < p->nSize; i++ )
+ if ( p->pArray[i] == pNode )
+ return 1;
+ Fraig_NodeVecPushOrder( p, pNode );
+ return 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Inserts a new node in the order by arrival times.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeVecPushOrderByLevel( Fraig_NodeVec_t * p, Fraig_Node_t * pNode )
+{
+ Fraig_Node_t * pNode1, * pNode2;
+ int i;
+ Fraig_NodeVecPush( p, pNode );
+ // find the p of the node
+ for ( i = p->nSize-1; i > 0; i-- )
+ {
+ pNode1 = p->pArray[i ];
+ pNode2 = p->pArray[i-1];
+ if ( Fraig_Regular(pNode1)->Level <= Fraig_Regular(pNode2)->Level )
+ break;
+ p->pArray[i ] = pNode2;
+ p->pArray[i-1] = pNode1;
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Add the element while ensuring uniqueness in the order.]
+
+ Description [Returns 1 if the element was found, and 0 if it was new. ]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeVecPushUniqueOrderByLevel( Fraig_NodeVec_t * p, Fraig_Node_t * pNode )
+{
+ int i;
+ for ( i = 0; i < p->nSize; i++ )
+ if ( p->pArray[i] == pNode )
+ return 1;
+ Fraig_NodeVecPushOrderByLevel( p, pNode );
+ return 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_NodeVecPop( Fraig_NodeVec_t * p )
+{
+ return p->pArray[--p->nSize];
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeVecRemove( Fraig_NodeVec_t * p, Fraig_Node_t * Entry )
+{
+ int i;
+ for ( i = 0; i < p->nSize; i++ )
+ if ( p->pArray[i] == Entry )
+ break;
+ assert( i < p->nSize );
+ for ( i++; i < p->nSize; i++ )
+ p->pArray[i-1] = p->pArray[i];
+ p->nSize--;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeVecWriteEntry( Fraig_NodeVec_t * p, int i, Fraig_Node_t * Entry )
+{
+ assert( i >= 0 && i < p->nSize );
+ p->pArray[i] = Entry;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Fraig_Node_t * Fraig_NodeVecReadEntry( Fraig_NodeVec_t * p, int i )
+{
+ assert( i >= 0 && i < p->nSize );
+ return p->pArray[i];
+}
+
+/**Function*************************************************************
+
+ Synopsis [Comparison procedure for two clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeVecCompareLevelsIncreasing( Fraig_Node_t ** pp1, Fraig_Node_t ** pp2 )
+{
+ int Level1 = Fraig_Regular(*pp1)->Level;
+ int Level2 = Fraig_Regular(*pp2)->Level;
+ if ( Level1 < Level2 )
+ return -1;
+ if ( Level1 > Level2 )
+ return 1;
+ return 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Comparison procedure for two clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeVecCompareLevelsDecreasing( Fraig_Node_t ** pp1, Fraig_Node_t ** pp2 )
+{
+ int Level1 = Fraig_Regular(*pp1)->Level;
+ int Level2 = Fraig_Regular(*pp2)->Level;
+ if ( Level1 > Level2 )
+ return -1;
+ if ( Level1 < Level2 )
+ return 1;
+ return 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Comparison procedure for two clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeVecCompareNumbers( Fraig_Node_t ** pp1, Fraig_Node_t ** pp2 )
+{
+ int Num1 = Fraig_Regular(*pp1)->Num;
+ int Num2 = Fraig_Regular(*pp2)->Num;
+ if ( Num1 < Num2 )
+ return -1;
+ if ( Num1 > Num2 )
+ return 1;
+ return 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Comparison procedure for two clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Fraig_NodeVecCompareRefCounts( Fraig_Node_t ** pp1, Fraig_Node_t ** pp2 )
+{
+ int nRefs1 = Fraig_Regular(*pp1)->nRefs;
+ int nRefs2 = Fraig_Regular(*pp2)->nRefs;
+
+ if ( nRefs1 < nRefs2 )
+ return -1;
+ if ( nRefs1 > nRefs2 )
+ return 1;
+
+ nRefs1 = Fraig_Regular(*pp1)->Level;
+ nRefs2 = Fraig_Regular(*pp2)->Level;
+
+ if ( nRefs1 < nRefs2 )
+ return -1;
+ if ( nRefs1 > nRefs2 )
+ return 1;
+ return 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sorting the entries by their integer value.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeVecSortByLevel( Fraig_NodeVec_t * p, int fIncreasing )
+{
+ if ( fIncreasing )
+ qsort( (void *)p->pArray, p->nSize, sizeof(Fraig_Node_t *),
+ (int (*)(const void *, const void *)) Fraig_NodeVecCompareLevelsIncreasing );
+ else
+ qsort( (void *)p->pArray, p->nSize, sizeof(Fraig_Node_t *),
+ (int (*)(const void *, const void *)) Fraig_NodeVecCompareLevelsDecreasing );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sorting the entries by their integer value.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeVecSortByNumber( Fraig_NodeVec_t * p )
+{
+ qsort( (void *)p->pArray, p->nSize, sizeof(Fraig_Node_t *),
+ (int (*)(const void *, const void *)) Fraig_NodeVecCompareNumbers );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sorting the entries by their integer value.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_NodeVecSortByRefCount( Fraig_NodeVec_t * p )
+{
+ qsort( (void *)p->pArray, p->nSize, sizeof(Fraig_Node_t *),
+ (int (*)(const void *, const void *)) Fraig_NodeVecCompareRefCounts );
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
diff --git a/src/sat/fraig/module.make b/src/sat/fraig/module.make
new file mode 100644
index 00000000..cc6eb9d3
--- /dev/null
+++ b/src/sat/fraig/module.make
@@ -0,0 +1,12 @@
+SRC += src/sat/fraig/fraigApi.c \
+ src/sat/fraig/fraigCanon.c \
+ src/sat/fraig/fraigFanout.c \
+ src/sat/fraig/fraigFeed.c \
+ src/sat/fraig/fraigMan.c \
+ src/sat/fraig/fraigMem.c \
+ src/sat/fraig/fraigNode.c \
+ src/sat/fraig/fraigPrime.c \
+ src/sat/fraig/fraigSat.c \
+ src/sat/fraig/fraigTable.c \
+ src/sat/fraig/fraigUtil.c \
+ src/sat/fraig/fraigVec.c
diff --git a/src/sat/msat/module.make b/src/sat/msat/module.make
new file mode 100644
index 00000000..0dadfbe1
--- /dev/null
+++ b/src/sat/msat/module.make
@@ -0,0 +1,13 @@
+SRC += src/sat/msat/msatActivity.c \
+ src/sat/msat/msatClause.c \
+ src/sat/msat/msatClauseVec.c \
+ src/sat/msat/msatMem.c \
+ src/sat/msat/msatOrderJ.c \
+ src/sat/msat/msatQueue.c \
+ src/sat/msat/msatRead.c \
+ src/sat/msat/msatSolverApi.c \
+ src/sat/msat/msatSolverCore.c \
+ src/sat/msat/msatSolverIo.c \
+ src/sat/msat/msatSolverSearch.c \
+ src/sat/msat/msatSort.c \
+ src/sat/msat/msatVec.c
diff --git a/src/sat/msat/msat.h b/src/sat/msat/msat.h
new file mode 100644
index 00000000..53353ba6
--- /dev/null
+++ b/src/sat/msat/msat.h
@@ -0,0 +1,172 @@
+/**CFile****************************************************************
+
+ FileName [msat.h]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [External definitions of the solver.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msat.h,v 1.6 2004/05/12 06:30:20 satrajit Exp $]
+
+***********************************************************************/
+
+#ifndef __MSAT_H__
+#define __MSAT_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// INCLUDES ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// PARAMETERS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// STRUCTURE DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+#ifdef bool
+#undef bool
+#endif
+
+#ifndef __MVTYPES_H__
+typedef int bool;
+#endif
+
+typedef struct Msat_Solver_t_ Msat_Solver_t;
+
+// the vector of intergers and of clauses
+typedef struct Msat_IntVec_t_ Msat_IntVec_t;
+typedef struct Msat_ClauseVec_t_ Msat_ClauseVec_t;
+typedef struct Msat_VarHeap_t_ Msat_VarHeap_t;
+
+// the return value of the solver
+typedef enum { MSAT_FALSE = -1, MSAT_UNKNOWN = 0, MSAT_TRUE = 1 } Msat_Type_t;
+
+// representation of variables and literals
+// the literal (l) is the variable (v) and the sign (s)
+// s = 0 the variable is positive
+// s = 1 the variable is negative
+#define MSAT_VAR2LIT(v,s) (2*(v)+(s))
+#define MSAT_LITNOT(l) ((l)^1)
+#define MSAT_LITSIGN(l) ((l)&1)
+#define MSAT_LIT2VAR(l) ((l)>>1)
+
+////////////////////////////////////////////////////////////////////////
+/// GLOBAL VARIABLES ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// MACRO DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/*=== satRead.c ============================================================*/
+extern bool Msat_SolverParseDimacs( FILE * pFile, Msat_Solver_t ** p, int fVerbose );
+/*=== satSolver.c ===========================================================*/
+// adding vars, clauses, simplifying the database, and solving
+extern bool Msat_SolverAddVar( Msat_Solver_t * p, int Level );
+extern bool Msat_SolverAddClause( Msat_Solver_t * p, Msat_IntVec_t * pLits );
+extern bool Msat_SolverSimplifyDB( Msat_Solver_t * p );
+extern bool Msat_SolverSolve( Msat_Solver_t * p, Msat_IntVec_t * pVecAssumps, int nBackTrackLimit, int nTimeLimit );
+// printing stats, assignments, and clauses
+extern void Msat_SolverPrintStats( Msat_Solver_t * p );
+extern void Msat_SolverPrintAssignment( Msat_Solver_t * p );
+extern void Msat_SolverPrintClauses( Msat_Solver_t * p );
+extern void Msat_SolverWriteDimacs( Msat_Solver_t * p, char * pFileName );
+// access to the solver internal data
+extern int Msat_SolverReadVarNum( Msat_Solver_t * p );
+extern int Msat_SolverReadClauseNum( Msat_Solver_t * p );
+extern int Msat_SolverReadVarAllocNum( Msat_Solver_t * p );
+extern int * Msat_SolverReadAssignsArray( Msat_Solver_t * p );
+extern int * Msat_SolverReadModelArray( Msat_Solver_t * p );
+extern unsigned Msat_SolverReadTruth( Msat_Solver_t * p );
+extern int Msat_SolverReadBackTracks( Msat_Solver_t * p );
+extern int Msat_SolverReadInspects( Msat_Solver_t * p );
+extern void Msat_SolverSetVerbosity( Msat_Solver_t * p, int fVerbose );
+extern void Msat_SolverSetProofWriting( Msat_Solver_t * p, int fProof );
+extern void Msat_SolverSetVarTypeA( Msat_Solver_t * p, int Var );
+extern void Msat_SolverSetVarMap( Msat_Solver_t * p, Msat_IntVec_t * vVarMap );
+extern void Msat_SolverMarkLastClauseTypeA( Msat_Solver_t * p );
+extern void Msat_SolverMarkClausesStart( Msat_Solver_t * p );
+extern float * Msat_SolverReadFactors( Msat_Solver_t * p );
+// returns the solution after incremental solving
+extern int Msat_SolverReadSolutions( Msat_Solver_t * p );
+extern int * Msat_SolverReadSolutionsArray( Msat_Solver_t * p );
+extern Msat_ClauseVec_t * Msat_SolverReadAdjacents( Msat_Solver_t * p );
+extern Msat_IntVec_t * Msat_SolverReadConeVars( Msat_Solver_t * p );
+extern Msat_IntVec_t * Msat_SolverReadVarsUsed( Msat_Solver_t * p );
+/*=== satSolverSearch.c ===========================================================*/
+extern void Msat_SolverRemoveLearned( Msat_Solver_t * p );
+extern void Msat_SolverRemoveMarked( Msat_Solver_t * p );
+/*=== satSolverApi.c ===========================================================*/
+// allocation, cleaning, and freeing the solver
+extern Msat_Solver_t * Msat_SolverAlloc( int nVars, double dClaInc, double dClaDecay, double dVarInc, double dVarDecay, bool fVerbose );
+extern void Msat_SolverResize( Msat_Solver_t * pMan, int nVarsAlloc );
+extern void Msat_SolverClean( Msat_Solver_t * p, int nVars );
+extern void Msat_SolverPrepare( Msat_Solver_t * pSat, Msat_IntVec_t * vVars );
+extern void Msat_SolverFree( Msat_Solver_t * p );
+/*=== satVec.c ===========================================================*/
+extern Msat_IntVec_t * Msat_IntVecAlloc( int nCap );
+extern Msat_IntVec_t * Msat_IntVecAllocArray( int * pArray, int nSize );
+extern Msat_IntVec_t * Msat_IntVecAllocArrayCopy( int * pArray, int nSize );
+extern Msat_IntVec_t * Msat_IntVecDup( Msat_IntVec_t * pVec );
+extern Msat_IntVec_t * Msat_IntVecDupArray( Msat_IntVec_t * pVec );
+extern void Msat_IntVecFree( Msat_IntVec_t * p );
+extern void Msat_IntVecFill( Msat_IntVec_t * p, int nSize, int Entry );
+extern int * Msat_IntVecReleaseArray( Msat_IntVec_t * p );
+extern int * Msat_IntVecReadArray( Msat_IntVec_t * p );
+extern int Msat_IntVecReadSize( Msat_IntVec_t * p );
+extern int Msat_IntVecReadEntry( Msat_IntVec_t * p, int i );
+extern int Msat_IntVecReadEntryLast( Msat_IntVec_t * p );
+extern void Msat_IntVecWriteEntry( Msat_IntVec_t * p, int i, int Entry );
+extern void Msat_IntVecGrow( Msat_IntVec_t * p, int nCapMin );
+extern void Msat_IntVecShrink( Msat_IntVec_t * p, int nSizeNew );
+extern void Msat_IntVecClear( Msat_IntVec_t * p );
+extern void Msat_IntVecPush( Msat_IntVec_t * p, int Entry );
+extern int Msat_IntVecPushUnique( Msat_IntVec_t * p, int Entry );
+extern void Msat_IntVecPushUniqueOrder( Msat_IntVec_t * p, int Entry, int fIncrease );
+extern int Msat_IntVecPop( Msat_IntVec_t * p );
+extern void Msat_IntVecSort( Msat_IntVec_t * p, int fReverse );
+/*=== satHeap.c ===========================================================*/
+extern Msat_VarHeap_t * Msat_VarHeapAlloc();
+extern void Msat_VarHeapSetActivity( Msat_VarHeap_t * p, double * pActivity );
+extern void Msat_VarHeapStart( Msat_VarHeap_t * p, int * pVars, int nVars, int nVarsAlloc );
+extern void Msat_VarHeapGrow( Msat_VarHeap_t * p, int nSize );
+extern void Msat_VarHeapStop( Msat_VarHeap_t * p );
+extern void Msat_VarHeapPrint( FILE * pFile, Msat_VarHeap_t * p );
+extern void Msat_VarHeapCheck( Msat_VarHeap_t * p );
+extern void Msat_VarHeapCheckOne( Msat_VarHeap_t * p, int iVar );
+extern int Msat_VarHeapContainsVar( Msat_VarHeap_t * p, int iVar );
+extern void Msat_VarHeapInsert( Msat_VarHeap_t * p, int iVar );
+extern void Msat_VarHeapUpdate( Msat_VarHeap_t * p, int iVar );
+extern void Msat_VarHeapDelete( Msat_VarHeap_t * p, int iVar );
+extern double Msat_VarHeapReadMaxWeight( Msat_VarHeap_t * p );
+extern int Msat_VarHeapCountNodes( Msat_VarHeap_t * p, double WeightLimit );
+extern int Msat_VarHeapReadMax( Msat_VarHeap_t * p );
+extern int Msat_VarHeapGetMax( Msat_VarHeap_t * p );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
diff --git a/src/sat/msat/msatActivity.c b/src/sat/msat/msatActivity.c
new file mode 100644
index 00000000..1cd795bd
--- /dev/null
+++ b/src/sat/msat/msatActivity.c
@@ -0,0 +1,160 @@
+/**CFile****************************************************************
+
+ FileName [msatActivity.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [Procedures controlling activity of variables and clauses.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatActivity.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverVarBumpActivity( Msat_Solver_t * p, Msat_Lit_t Lit )
+{
+ Msat_Var_t Var;
+ if ( p->dVarDecay < 0 ) // (negative decay means static variable order -- don't bump)
+ return;
+ Var = MSAT_LIT2VAR(Lit);
+ p->pdActivity[Var] += p->dVarInc;
+// p->pdActivity[Var] += p->dVarInc * p->pFactors[Var];
+ if ( p->pdActivity[Var] > 1e100 )
+ Msat_SolverVarRescaleActivity( p );
+ Msat_OrderUpdate( p->pOrder, Var );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverVarDecayActivity( Msat_Solver_t * p )
+{
+ if ( p->dVarDecay >= 0 )
+ p->dVarInc *= p->dVarDecay;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Divide all variable activities by 1e100.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverVarRescaleActivity( Msat_Solver_t * p )
+{
+ int i;
+ for ( i = 0; i < p->nVars; i++ )
+ p->pdActivity[i] *= 1e-100;
+ p->dVarInc *= 1e-100;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverClaBumpActivity( Msat_Solver_t * p, Msat_Clause_t * pC )
+{
+ float Activ;
+ Activ = Msat_ClauseReadActivity(pC);
+ if ( Activ + p->dClaInc > 1e20 )
+ {
+ Msat_SolverClaRescaleActivity( p );
+ Activ = Msat_ClauseReadActivity( pC );
+ }
+ Msat_ClauseWriteActivity( pC, Activ + (float)p->dClaInc );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverClaDecayActivity( Msat_Solver_t * p )
+{
+ p->dClaInc *= p->dClaDecay;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Divide all constraint activities by 1e20.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverClaRescaleActivity( Msat_Solver_t * p )
+{
+ Msat_Clause_t ** pLearned;
+ int nLearned, i;
+ float Activ;
+ nLearned = Msat_ClauseVecReadSize( p->vLearned );
+ pLearned = Msat_ClauseVecReadArray( p->vLearned );
+ for ( i = 0; i < nLearned; i++ )
+ {
+ Activ = Msat_ClauseReadActivity( pLearned[i] );
+ Msat_ClauseWriteActivity( pLearned[i], Activ * (float)1e-20 );
+ }
+ p->dClaInc *= 1e-20;
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/msat/msatClause.c b/src/sat/msat/msatClause.c
new file mode 100644
index 00000000..2ba8cd32
--- /dev/null
+++ b/src/sat/msat/msatClause.c
@@ -0,0 +1,529 @@
+/**CFile****************************************************************
+
+ FileName [msatClause.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [Procedures working with SAT clauses.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatClause.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+struct Msat_Clause_t_
+{
+ int Num; // unique number of the clause
+ unsigned fLearned : 1; // 1 if the clause is learned
+ unsigned fMark : 1; // used to mark visited clauses during proof recording
+ unsigned fTypeA : 1; // used to mark clauses belonging to A for interpolant computation
+ unsigned nSize : 14; // the number of literals in the clause
+ unsigned nSizeAlloc : 15; // the number of bytes allocated for the clause
+ Msat_Lit_t pData[0];
+};
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Creates a new clause.]
+
+ Description [Returns FALSE if top-level conflict detected (must be handled);
+ TRUE otherwise. 'pClause_out' may be set to NULL if clause is already
+ satisfied by the top-level assignment.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_ClauseCreate( Msat_Solver_t * p, Msat_IntVec_t * vLits, bool fLearned, Msat_Clause_t ** pClause_out )
+{
+ int * pAssigns = Msat_SolverReadAssignsArray(p);
+ Msat_ClauseVec_t ** pvWatched;
+ Msat_Clause_t * pC;
+ int * pLits;
+ int nLits, i, j;
+ int nBytes;
+ Msat_Var_t Var;
+ bool Sign;
+
+ *pClause_out = NULL;
+
+ nLits = Msat_IntVecReadSize(vLits);
+ pLits = Msat_IntVecReadArray(vLits);
+
+ if ( !fLearned )
+ {
+ int * pSeen = Msat_SolverReadSeenArray( p );
+ int nSeenId;
+ assert( Msat_SolverReadDecisionLevel(p) == 0 );
+ // sorting literals makes the code trace-equivalent
+ // with to the original C++ solver
+ Msat_IntVecSort( vLits, 0 );
+ // increment the counter of seen twice
+ nSeenId = Msat_SolverIncrementSeenId( p );
+ nSeenId = Msat_SolverIncrementSeenId( p );
+ // nSeenId - 1 stands for negative
+ // nSeenId stands for positive
+ // Remove false literals
+
+ // there is a bug here!!!!
+ // when the same var in opposite polarities is given, it drops one polarity!!!
+
+ for ( i = j = 0; i < nLits; i++ ) {
+ // get the corresponding variable
+ Var = MSAT_LIT2VAR(pLits[i]);
+ Sign = MSAT_LITSIGN(pLits[i]); // Sign=0 for positive
+ // check if we already saw this variable in the this clause
+ if ( pSeen[Var] >= nSeenId - 1 )
+ {
+ if ( (pSeen[Var] != nSeenId) == Sign ) // the same lit
+ continue;
+ return 1; // two opposite polarity lits -- don't add the clause
+ }
+ // mark the variable as seen
+ pSeen[Var] = nSeenId - !Sign;
+
+ // analize the value of this literal
+ if ( pAssigns[Var] != MSAT_VAR_UNASSIGNED )
+ {
+ if ( pAssigns[Var] == pLits[i] )
+ return 1; // the clause is always true -- don't add anything
+ // the literal has no impact - skip it
+ continue;
+ }
+ // otherwise, add this literal to the clause
+ pLits[j++] = pLits[i];
+ }
+ Msat_IntVecShrink( vLits, j );
+ nLits = j;
+/*
+ // the problem with this code is that performance is very
+ // sensitive to the ordering of adjacency lits
+ // the best ordering requires fanins first, next fanouts
+ // this ordering is more convenient to make from FRAIG
+
+ // create the adjacency information
+ if ( nLits > 2 )
+ {
+ Msat_Var_t VarI, VarJ;
+ Msat_IntVec_t * pAdjI, * pAdjJ;
+
+ for ( i = 0; i < nLits; i++ )
+ {
+ VarI = MSAT_LIT2VAR(pLits[i]);
+ pAdjI = (Msat_IntVec_t *)p->vAdjacents->pArray[VarI];
+
+ for ( j = i+1; j < nLits; j++ )
+ {
+ VarJ = MSAT_LIT2VAR(pLits[j]);
+ pAdjJ = (Msat_IntVec_t *)p->vAdjacents->pArray[VarJ];
+
+ Msat_IntVecPushUniqueOrder( pAdjI, VarJ, 1 );
+ Msat_IntVecPushUniqueOrder( pAdjJ, VarI, 1 );
+ }
+ }
+ }
+*/
+ }
+ // 'vLits' is now the (possibly) reduced vector of literals.
+ if ( nLits == 0 )
+ return 0;
+ if ( nLits == 1 )
+ return Msat_SolverEnqueue( p, pLits[0], NULL );
+
+ // Allocate clause:
+// nBytes = sizeof(unsigned)*(nLits + 1 + (int)fLearned);
+ nBytes = sizeof(unsigned)*(nLits + 2 + (int)fLearned);
+#ifdef USE_SYSTEM_MEMORY_MANAGEMENT
+ pC = (Msat_Clause_t *)ALLOC( char, nBytes );
+#else
+ pC = (Msat_Clause_t *)Msat_MmStepEntryFetch( Msat_SolverReadMem(p), nBytes );
+#endif
+ pC->Num = p->nClauses++;
+ pC->fTypeA = 0;
+ pC->fMark = 0;
+ pC->fLearned = fLearned;
+ pC->nSize = nLits;
+ pC->nSizeAlloc = nBytes;
+ memcpy( pC->pData, pLits, sizeof(int)*nLits );
+
+ // For learnt clauses only:
+ if ( fLearned )
+ {
+ int * pLevel = Msat_SolverReadDecisionLevelArray( p );
+ int iLevelMax, iLevelCur, iLitMax;
+
+ // Put the second watch on the literal with highest decision level:
+ iLitMax = 1;
+ iLevelMax = pLevel[ MSAT_LIT2VAR(pLits[1]) ];
+ for ( i = 2; i < nLits; i++ )
+ {
+ iLevelCur = pLevel[ MSAT_LIT2VAR(pLits[i]) ];
+ assert( iLevelCur != -1 );
+ if ( iLevelMax < iLevelCur )
+ // this is very strange - shouldn't it be???
+ // if ( iLevelMax > iLevelCur )
+ iLevelMax = iLevelCur, iLitMax = i;
+ }
+ pC->pData[1] = pLits[iLitMax];
+ pC->pData[iLitMax] = pLits[1];
+
+ // Bumping:
+ // (newly learnt clauses should be considered active)
+ Msat_ClauseWriteActivity( pC, 0.0 );
+ Msat_SolverClaBumpActivity( p, pC );
+// if ( nLits < 20 )
+ for ( i = 0; i < nLits; i++ )
+ {
+ Msat_SolverVarBumpActivity( p, pLits[i] );
+// Msat_SolverVarBumpActivity( p, pLits[i] );
+// p->pFreq[ MSAT_LIT2VAR(pLits[i]) ]++;
+ }
+ }
+
+ // Store clause:
+ pvWatched = Msat_SolverReadWatchedArray( p );
+ Msat_ClauseVecPush( pvWatched[ MSAT_LITNOT(pC->pData[0]) ], pC );
+ Msat_ClauseVecPush( pvWatched[ MSAT_LITNOT(pC->pData[1]) ], pC );
+ *pClause_out = pC;
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Deallocates the clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClauseFree( Msat_Solver_t * p, Msat_Clause_t * pC, bool fRemoveWatched )
+{
+ if ( fRemoveWatched )
+ {
+ Msat_Lit_t Lit;
+ Msat_ClauseVec_t ** pvWatched;
+ pvWatched = Msat_SolverReadWatchedArray( p );
+ Lit = MSAT_LITNOT( pC->pData[0] );
+ Msat_ClauseRemoveWatch( pvWatched[Lit], pC );
+ Lit = MSAT_LITNOT( pC->pData[1] );
+ Msat_ClauseRemoveWatch( pvWatched[Lit], pC );
+ }
+
+#ifdef USE_SYSTEM_MEMORY_MANAGEMENT
+ free( pC );
+#else
+ Msat_MmStepEntryRecycle( Msat_SolverReadMem(p), (char *)pC, pC->nSizeAlloc );
+#endif
+
+}
+
+/**Function*************************************************************
+
+ Synopsis [Access the data field of the clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_ClauseReadLearned( Msat_Clause_t * pC ) { return pC->fLearned; }
+int Msat_ClauseReadSize( Msat_Clause_t * pC ) { return pC->nSize; }
+int * Msat_ClauseReadLits( Msat_Clause_t * pC ) { return pC->pData; }
+bool Msat_ClauseReadMark( Msat_Clause_t * pC ) { return pC->fMark; }
+int Msat_ClauseReadNum( Msat_Clause_t * pC ) { return pC->Num; }
+bool Msat_ClauseReadTypeA( Msat_Clause_t * pC ) { return pC->fTypeA; }
+
+void Msat_ClauseSetMark( Msat_Clause_t * pC, bool fMark ) { pC->fMark = fMark; }
+void Msat_ClauseSetNum( Msat_Clause_t * pC, int Num ) { pC->Num = Num; }
+void Msat_ClauseSetTypeA( Msat_Clause_t * pC, bool fTypeA ) { pC->fTypeA = fTypeA; }
+
+/**Function*************************************************************
+
+ Synopsis [Checks whether the learned clause is locked.]
+
+ Description [The clause may be locked if it is the reason of a
+ recent conflict. Such clause cannot be removed from the database.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_ClauseIsLocked( Msat_Solver_t * p, Msat_Clause_t * pC )
+{
+ Msat_Clause_t ** pReasons = Msat_SolverReadReasonArray( p );
+ return (bool)(pReasons[MSAT_LIT2VAR(pC->pData[0])] == pC);
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reads the activity of the given clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+float Msat_ClauseReadActivity( Msat_Clause_t * pC )
+{
+ return *((float *)(pC->pData + pC->nSize));
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sets the activity of the clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClauseWriteActivity( Msat_Clause_t * pC, float Num )
+{
+ *((float *)(pC->pData + pC->nSize)) = Num;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Propages the assignment.]
+
+ Description [The literal that has become true (Lit) is given to this
+ procedure. The array of current variable assignments is given for
+ efficiency. The output literal (pLit_out) can be the second watched
+ literal (if TRUE is returned) or the conflict literal (if FALSE is
+ returned). This messy interface is used to improve performance.
+ This procedure accounts for ~50% of the runtime of the solver.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_ClausePropagate( Msat_Clause_t * pC, Msat_Lit_t Lit, int * pAssigns, Msat_Lit_t * pLit_out )
+{
+ // make sure the false literal is pC->pData[1]
+ Msat_Lit_t LitF = MSAT_LITNOT(Lit);
+ if ( pC->pData[0] == LitF )
+ pC->pData[0] = pC->pData[1], pC->pData[1] = LitF;
+ assert( pC->pData[1] == LitF );
+ // if the 0-th watch is true, clause is already satisfied
+ if ( pAssigns[MSAT_LIT2VAR(pC->pData[0])] == pC->pData[0] )
+ return 1;
+ // look for a new watch
+ if ( pC->nSize > 2 )
+ {
+ int i;
+ for ( i = 2; i < (int)pC->nSize; i++ )
+ if ( pAssigns[MSAT_LIT2VAR(pC->pData[i])] != MSAT_LITNOT(pC->pData[i]) )
+ {
+ pC->pData[1] = pC->pData[i], pC->pData[i] = LitF;
+ *pLit_out = MSAT_LITNOT(pC->pData[1]);
+ return 1;
+ }
+ }
+ // clause is unit under assignment
+ *pLit_out = pC->pData[0];
+ return 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Simplifies the clause.]
+
+ Description [Assumes everything has been propagated! (esp. watches
+ in clauses are NOT unsatisfied)]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_ClauseSimplify( Msat_Clause_t * pC, int * pAssigns )
+{
+ Msat_Var_t Var;
+ int i, j;
+ for ( i = j = 0; i < (int)pC->nSize; i++ )
+ {
+ Var = MSAT_LIT2VAR(pC->pData[i]);
+ if ( pAssigns[Var] == MSAT_VAR_UNASSIGNED )
+ {
+ pC->pData[j++] = pC->pData[i];
+ continue;
+ }
+ if ( pAssigns[Var] == pC->pData[i] )
+ return 1;
+ // otherwise, the value of the literal is false
+ // make sure, this literal is not watched
+ assert( i >= 2 );
+ }
+ // if the size has changed, update it and move activity
+ if ( j < (int)pC->nSize )
+ {
+ float Activ = Msat_ClauseReadActivity(pC);
+ pC->nSize = j;
+ Msat_ClauseWriteActivity(pC, Activ);
+ }
+ return 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Computes reason of conflict in the given clause.]
+
+ Description [If the literal is unassigned, finds the reason by
+ complementing literals in the given cluase (pC). If the literal is
+ assigned, makes sure that this literal is the first one in the clause
+ and computes the complement of all other literals in the clause.
+ Returns the reason in the given array (vLits_out). If the clause is
+ learned, bumps its activity.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClauseCalcReason( Msat_Solver_t * p, Msat_Clause_t * pC, Msat_Lit_t Lit, Msat_IntVec_t * vLits_out )
+{
+ int i;
+ // clear the reason
+ Msat_IntVecClear( vLits_out );
+ assert( Lit == MSAT_LIT_UNASSIGNED || Lit == pC->pData[0] );
+ for ( i = (Lit != MSAT_LIT_UNASSIGNED); i < (int)pC->nSize; i++ )
+ {
+ assert( Msat_SolverReadAssignsArray(p)[MSAT_LIT2VAR(pC->pData[i])] == MSAT_LITNOT(pC->pData[i]) );
+ Msat_IntVecPush( vLits_out, MSAT_LITNOT(pC->pData[i]) );
+ }
+ if ( pC->fLearned )
+ Msat_SolverClaBumpActivity( p, pC );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Removes the given clause from the watched list.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClauseRemoveWatch( Msat_ClauseVec_t * vClauses, Msat_Clause_t * pC )
+{
+ Msat_Clause_t ** pClauses;
+ int nClauses, i;
+ nClauses = Msat_ClauseVecReadSize( vClauses );
+ pClauses = Msat_ClauseVecReadArray( vClauses );
+ for ( i = 0; pClauses[i] != pC; i++ )
+ assert( i < nClauses );
+ for ( ; i < nClauses - 1; i++ )
+ pClauses[i] = pClauses[i+1];
+ Msat_ClauseVecPop( vClauses );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prints the given clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClausePrint( Msat_Clause_t * pC )
+{
+ int i;
+ if ( pC == NULL )
+ printf( "NULL pointer" );
+ else
+ {
+ if ( pC->fLearned )
+ printf( "Act = %.4f ", Msat_ClauseReadActivity(pC) );
+ for ( i = 0; i < (int)pC->nSize; i++ )
+ printf( " %s%d", ((pC->pData[i]&1)? "-": ""), pC->pData[i]/2 + 1 );
+ }
+ printf( "\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Writes the given clause in a file in DIMACS format.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClauseWriteDimacs( FILE * pFile, Msat_Clause_t * pC, bool fIncrement )
+{
+ int i;
+ for ( i = 0; i < (int)pC->nSize; i++ )
+ fprintf( pFile, "%s%d ", ((pC->pData[i]&1)? "-": ""), pC->pData[i]/2 + (int)(fIncrement>0) );
+ if ( fIncrement )
+ fprintf( pFile, "0" );
+ fprintf( pFile, "\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prints the given clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClausePrintSymbols( Msat_Clause_t * pC )
+{
+ int i;
+ if ( pC == NULL )
+ printf( "NULL pointer" );
+ else
+ {
+// if ( pC->fLearned )
+// printf( "Act = %.4f ", Msat_ClauseReadActivity(pC) );
+ for ( i = 0; i < (int)pC->nSize; i++ )
+ printf(" "L_LIT, L_lit(pC->pData[i]));
+ }
+ printf( "\n" );
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/msat/msatClauseVec.c b/src/sat/msat/msatClauseVec.c
new file mode 100644
index 00000000..04691cf2
--- /dev/null
+++ b/src/sat/msat/msatClauseVec.c
@@ -0,0 +1,232 @@
+/**CFile****************************************************************
+
+ FileName [msatClauseVec.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [Procedures working with arrays of SAT clauses.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatClauseVec.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Allocates a vector with the given capacity.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_ClauseVec_t * Msat_ClauseVecAlloc( int nCap )
+{
+ Msat_ClauseVec_t * p;
+ p = ALLOC( Msat_ClauseVec_t, 1 );
+ if ( nCap > 0 && nCap < 16 )
+ nCap = 16;
+ p->nSize = 0;
+ p->nCap = nCap;
+ p->pArray = p->nCap? ALLOC( Msat_Clause_t *, p->nCap ) : NULL;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClauseVecFree( Msat_ClauseVec_t * p )
+{
+ FREE( p->pArray );
+ FREE( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_Clause_t ** Msat_ClauseVecReadArray( Msat_ClauseVec_t * p )
+{
+ return p->pArray;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_ClauseVecReadSize( Msat_ClauseVec_t * p )
+{
+ return p->nSize;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Resizes the vector to the given capacity.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClauseVecGrow( Msat_ClauseVec_t * p, int nCapMin )
+{
+ if ( p->nCap >= nCapMin )
+ return;
+ p->pArray = REALLOC( Msat_Clause_t *, p->pArray, nCapMin );
+ p->nCap = nCapMin;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClauseVecShrink( Msat_ClauseVec_t * p, int nSizeNew )
+{
+ assert( p->nSize >= nSizeNew );
+ p->nSize = nSizeNew;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClauseVecClear( Msat_ClauseVec_t * p )
+{
+ p->nSize = 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClauseVecPush( Msat_ClauseVec_t * p, Msat_Clause_t * Entry )
+{
+ if ( p->nSize == p->nCap )
+ {
+ if ( p->nCap < 16 )
+ Msat_ClauseVecGrow( p, 16 );
+ else
+ Msat_ClauseVecGrow( p, 2 * p->nCap );
+ }
+ p->pArray[p->nSize++] = Entry;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_Clause_t * Msat_ClauseVecPop( Msat_ClauseVec_t * p )
+{
+ return p->pArray[--p->nSize];
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_ClauseVecWriteEntry( Msat_ClauseVec_t * p, int i, Msat_Clause_t * Entry )
+{
+ assert( i >= 0 && i < p->nSize );
+ p->pArray[i] = Entry;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_Clause_t * Msat_ClauseVecReadEntry( Msat_ClauseVec_t * p, int i )
+{
+ assert( i >= 0 && i < p->nSize );
+ return p->pArray[i];
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/msat/msatInt.h b/src/sat/msat/msatInt.h
new file mode 100644
index 00000000..03903abe
--- /dev/null
+++ b/src/sat/msat/msatInt.h
@@ -0,0 +1,306 @@
+/**CFile****************************************************************
+
+ FileName [msatInt.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [Internal definitions of the solver.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatInt.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#ifndef __MSAT_INT_H__
+#define __MSAT_INT_H__
+
+////////////////////////////////////////////////////////////////////////
+/// INCLUDES ///
+////////////////////////////////////////////////////////////////////////
+
+//#include "leaks.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+#include <math.h>
+#include "msat.h"
+
+////////////////////////////////////////////////////////////////////////
+/// PARAMETERS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// STRUCTURE DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+#ifdef _MSC_VER
+typedef __int64 int64;
+#else
+typedef long long int64;
+#endif
+
+// outputs the runtime in seconds
+#define PRT(a,t) \
+ printf( "%s = ", (a) ); printf( "%6.2f sec\n", (float)(t)/(float)(CLOCKS_PER_SEC) )
+
+// memory management macros
+#define ALLOC(type, num) \
+ ((type *) malloc(sizeof(type) * (num)))
+#define REALLOC(type, obj, num) \
+ ((obj) ? ((type *) realloc((char *)(obj), sizeof(type) * (num))) : \
+ ((type *) malloc(sizeof(type) * (num))))
+#define FREE(obj) \
+ ((obj) ? (free((char *)(obj)), (obj) = 0) : 0)
+
+// By default, custom memory management is used
+// which guarantees constant time allocation/deallocation
+// for SAT clauses and other frequently modified objects.
+// For debugging, it is possible use system memory management
+// directly. In which case, uncomment the macro below.
+//#define USE_SYSTEM_MEMORY_MANAGEMENT
+
+// internal data structures
+typedef struct Msat_Clause_t_ Msat_Clause_t;
+typedef struct Msat_Queue_t_ Msat_Queue_t;
+typedef struct Msat_Order_t_ Msat_Order_t;
+// memory managers (duplicated from Extra for stand-aloneness)
+typedef struct Msat_MmFixed_t_ Msat_MmFixed_t;
+typedef struct Msat_MmFlex_t_ Msat_MmFlex_t;
+typedef struct Msat_MmStep_t_ Msat_MmStep_t;
+// variables and literals
+typedef int Msat_Lit_t;
+typedef int Msat_Var_t;
+// the type of return value
+#define MSAT_VAR_UNASSIGNED (-1)
+#define MSAT_LIT_UNASSIGNED (-2)
+#define MSAT_ORDER_UNKNOWN (-3)
+
+// printing the search tree
+#define L_IND "%-*d"
+#define L_ind Msat_SolverReadDecisionLevel(p)*3+3,Msat_SolverReadDecisionLevel(p)
+#define L_LIT "%s%d"
+#define L_lit(Lit) MSAT_LITSIGN(Lit)?"-":"", MSAT_LIT2VAR(Lit)+1
+
+typedef struct Msat_SolverStats_t_ Msat_SolverStats_t;
+struct Msat_SolverStats_t_
+{
+ int64 nStarts; // the number of restarts
+ int64 nDecisions; // the number of decisions
+ int64 nPropagations; // the number of implications
+ int64 nInspects; // the number of times clauses are vising while watching them
+ int64 nConflicts; // the number of conflicts
+ int64 nSuccesses; // the number of sat assignments found
+};
+
+typedef struct Msat_SearchParams_t_ Msat_SearchParams_t;
+struct Msat_SearchParams_t_
+{
+ double dVarDecay;
+ double dClaDecay;
+};
+
+// sat solver data structure visible through all the internal files
+struct Msat_Solver_t_
+{
+ int nClauses; // the total number of clauses
+ int nClausesStart; // the number of clauses before adding
+ Msat_ClauseVec_t * vClauses; // problem clauses
+ Msat_ClauseVec_t * vLearned; // learned clauses
+ double dClaInc; // Amount to bump next clause with.
+ double dClaDecay; // INVERSE decay factor for clause activity: stores 1/decay.
+
+ double * pdActivity; // A heuristic measurement of the activity of a variable.
+ float * pFactors; // the multiplicative factors of variable activity
+ double dVarInc; // Amount to bump next variable with.
+ double dVarDecay; // INVERSE decay factor for variable activity: stores 1/decay. Use negative value for static variable order.
+ Msat_Order_t * pOrder; // Keeps track of the decision variable order.
+
+ Msat_ClauseVec_t ** pvWatched; // 'pvWatched[lit]' is a list of constraints watching 'lit' (will go there if literal becomes true).
+ Msat_Queue_t * pQueue; // Propagation queue.
+
+ int nVars; // the current number of variables
+ int nVarsAlloc; // the maximum allowed number of variables
+ int * pAssigns; // The current assignments (literals or MSAT_VAR_UNKOWN)
+ int * pModel; // The satisfying assignment
+ Msat_IntVec_t * vTrail; // List of assignments made.
+ Msat_IntVec_t * vTrailLim; // Separator indices for different decision levels in 'trail'.
+ Msat_Clause_t ** pReasons; // 'reason[var]' is the clause that implied the variables current value, or 'NULL' if none.
+ int * pLevel; // 'level[var]' is the decision level at which assignment was made.
+ int nLevelRoot; // Level of first proper decision.
+
+ double dRandSeed; // For the internal random number generator (makes solver deterministic over different platforms).
+
+ bool fVerbose; // the verbosity flag
+ double dProgress; // Set by 'search()'.
+
+ // the variable cone and variable connectivity
+ Msat_IntVec_t * vConeVars;
+ Msat_IntVec_t * vVarsUsed;
+ Msat_ClauseVec_t * vAdjacents;
+
+ // internal data used during conflict analysis
+ int * pSeen; // time when a lit was seen for the last time
+ int nSeenId; // the id of current seeing
+ Msat_IntVec_t * vReason; // the temporary array of literals
+ Msat_IntVec_t * vTemp; // the temporary array of literals
+ int * pFreq; // the number of times each var participated in conflicts
+
+ // the memory manager
+ Msat_MmStep_t * pMem;
+
+ // statistics
+ Msat_SolverStats_t Stats;
+ int nTwoLits;
+ int nTwoLitsL;
+ int nClausesInit;
+ int nClausesAlloc;
+ int nClausesAllocL;
+ int nBackTracks;
+};
+
+struct Msat_ClauseVec_t_
+{
+ Msat_Clause_t ** pArray;
+ int nSize;
+ int nCap;
+};
+
+struct Msat_IntVec_t_
+{
+ int * pArray;
+ int nSize;
+ int nCap;
+};
+
+////////////////////////////////////////////////////////////////////////
+/// GLOBAL VARIABLES ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// MACRO DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/*=== satActivity.c ===========================================================*/
+extern void Msat_SolverVarDecayActivity( Msat_Solver_t * p );
+extern void Msat_SolverVarRescaleActivity( Msat_Solver_t * p );
+extern void Msat_SolverClaDecayActivity( Msat_Solver_t * p );
+extern void Msat_SolverClaRescaleActivity( Msat_Solver_t * p );
+/*=== satSolverApi.c ===========================================================*/
+extern Msat_Clause_t * Msat_SolverReadClause( Msat_Solver_t * p, int Num );
+/*=== satSolver.c ===========================================================*/
+extern int Msat_SolverReadDecisionLevel( Msat_Solver_t * p );
+extern int * Msat_SolverReadDecisionLevelArray( Msat_Solver_t * p );
+extern Msat_Clause_t ** Msat_SolverReadReasonArray( Msat_Solver_t * p );
+extern Msat_Type_t Msat_SolverReadVarValue( Msat_Solver_t * p, Msat_Var_t Var );
+extern Msat_ClauseVec_t * Msat_SolverReadLearned( Msat_Solver_t * p );
+extern Msat_ClauseVec_t ** Msat_SolverReadWatchedArray( Msat_Solver_t * p );
+extern int * Msat_SolverReadSeenArray( Msat_Solver_t * p );
+extern int Msat_SolverIncrementSeenId( Msat_Solver_t * p );
+extern Msat_MmStep_t * Msat_SolverReadMem( Msat_Solver_t * p );
+extern void Msat_SolverClausesIncrement( Msat_Solver_t * p );
+extern void Msat_SolverClausesDecrement( Msat_Solver_t * p );
+extern void Msat_SolverClausesIncrementL( Msat_Solver_t * p );
+extern void Msat_SolverClausesDecrementL( Msat_Solver_t * p );
+extern void Msat_SolverVarBumpActivity( Msat_Solver_t * p, Msat_Lit_t Lit );
+extern void Msat_SolverClaBumpActivity( Msat_Solver_t * p, Msat_Clause_t * pC );
+extern bool Msat_SolverEnqueue( Msat_Solver_t * p, Msat_Lit_t Lit, Msat_Clause_t * pC );
+extern double Msat_SolverProgressEstimate( Msat_Solver_t * p );
+/*=== satSolverSearch.c ===========================================================*/
+extern bool Msat_SolverAssume( Msat_Solver_t * p, Msat_Lit_t Lit );
+extern Msat_Clause_t * Msat_SolverPropagate( Msat_Solver_t * p );
+extern void Msat_SolverCancelUntil( Msat_Solver_t * p, int Level );
+extern Msat_Type_t Msat_SolverSearch( Msat_Solver_t * p, int nConfLimit, int nLearnedLimit, int nBackTrackLimit, Msat_SearchParams_t * pPars );
+/*=== satQueue.c ===========================================================*/
+extern Msat_Queue_t * Msat_QueueAlloc( int nVars );
+extern void Msat_QueueFree( Msat_Queue_t * p );
+extern int Msat_QueueReadSize( Msat_Queue_t * p );
+extern void Msat_QueueInsert( Msat_Queue_t * p, int Lit );
+extern int Msat_QueueExtract( Msat_Queue_t * p );
+extern void Msat_QueueClear( Msat_Queue_t * p );
+/*=== satOrder.c ===========================================================*/
+extern Msat_Order_t * Msat_OrderAlloc( Msat_Solver_t * pSat );
+extern void Msat_OrderSetBounds( Msat_Order_t * p, int nVarsMax );
+extern void Msat_OrderClean( Msat_Order_t * p, Msat_IntVec_t * vCone );
+extern int Msat_OrderCheck( Msat_Order_t * p );
+extern void Msat_OrderFree( Msat_Order_t * p );
+extern int Msat_OrderVarSelect( Msat_Order_t * p );
+extern void Msat_OrderVarAssigned( Msat_Order_t * p, int Var );
+extern void Msat_OrderVarUnassigned( Msat_Order_t * p, int Var );
+extern void Msat_OrderUpdate( Msat_Order_t * p, int Var );
+/*=== satClause.c ===========================================================*/
+extern bool Msat_ClauseCreate( Msat_Solver_t * p, Msat_IntVec_t * vLits, bool fLearnt, Msat_Clause_t ** pClause_out );
+extern Msat_Clause_t * Msat_ClauseCreateFake( Msat_Solver_t * p, Msat_IntVec_t * vLits );
+extern Msat_Clause_t * Msat_ClauseCreateFakeLit( Msat_Solver_t * p, Msat_Lit_t Lit );
+extern bool Msat_ClauseReadLearned( Msat_Clause_t * pC );
+extern int Msat_ClauseReadSize( Msat_Clause_t * pC );
+extern int * Msat_ClauseReadLits( Msat_Clause_t * pC );
+extern bool Msat_ClauseReadMark( Msat_Clause_t * pC );
+extern void Msat_ClauseSetMark( Msat_Clause_t * pC, bool fMark );
+extern int Msat_ClauseReadNum( Msat_Clause_t * pC );
+extern void Msat_ClauseSetNum( Msat_Clause_t * pC, int Num );
+extern bool Msat_ClauseReadTypeA( Msat_Clause_t * pC );
+extern void Msat_ClauseSetTypeA( Msat_Clause_t * pC, bool fTypeA );
+extern bool Msat_ClauseIsLocked( Msat_Solver_t * p, Msat_Clause_t * pC );
+extern float Msat_ClauseReadActivity( Msat_Clause_t * pC );
+extern void Msat_ClauseWriteActivity( Msat_Clause_t * pC, float Num );
+extern void Msat_ClauseFree( Msat_Solver_t * p, Msat_Clause_t * pC, bool fRemoveWatched );
+extern bool Msat_ClausePropagate( Msat_Clause_t * pC, Msat_Lit_t Lit, int * pAssigns, Msat_Lit_t * pLit_out );
+extern bool Msat_ClauseSimplify( Msat_Clause_t * pC, int * pAssigns );
+extern void Msat_ClauseCalcReason( Msat_Solver_t * p, Msat_Clause_t * pC, Msat_Lit_t Lit, Msat_IntVec_t * vLits_out );
+extern void Msat_ClauseRemoveWatch( Msat_ClauseVec_t * vClauses, Msat_Clause_t * pC );
+extern void Msat_ClausePrint( Msat_Clause_t * pC );
+extern void Msat_ClausePrintSymbols( Msat_Clause_t * pC );
+extern void Msat_ClauseWriteDimacs( FILE * pFile, Msat_Clause_t * pC, bool fIncrement );
+extern unsigned Msat_ClauseComputeTruth( Msat_Solver_t * p, Msat_Clause_t * pC );
+/*=== satSort.c ===========================================================*/
+extern void Msat_SolverSortDB( Msat_Solver_t * p );
+/*=== satClauseVec.c ===========================================================*/
+extern Msat_ClauseVec_t * Msat_ClauseVecAlloc( int nCap );
+extern void Msat_ClauseVecFree( Msat_ClauseVec_t * p );
+extern Msat_Clause_t ** Msat_ClauseVecReadArray( Msat_ClauseVec_t * p );
+extern int Msat_ClauseVecReadSize( Msat_ClauseVec_t * p );
+extern void Msat_ClauseVecGrow( Msat_ClauseVec_t * p, int nCapMin );
+extern void Msat_ClauseVecShrink( Msat_ClauseVec_t * p, int nSizeNew );
+extern void Msat_ClauseVecClear( Msat_ClauseVec_t * p );
+extern void Msat_ClauseVecPush( Msat_ClauseVec_t * p, Msat_Clause_t * Entry );
+extern Msat_Clause_t * Msat_ClauseVecPop( Msat_ClauseVec_t * p );
+extern void Msat_ClauseVecWriteEntry( Msat_ClauseVec_t * p, int i, Msat_Clause_t * Entry );
+extern Msat_Clause_t * Msat_ClauseVecReadEntry( Msat_ClauseVec_t * p, int i );
+
+/*=== satMem.c ===========================================================*/
+// fixed-size-block memory manager
+extern Msat_MmFixed_t * Msat_MmFixedStart( int nEntrySize );
+extern void Msat_MmFixedStop( Msat_MmFixed_t * p, int fVerbose );
+extern char * Msat_MmFixedEntryFetch( Msat_MmFixed_t * p );
+extern void Msat_MmFixedEntryRecycle( Msat_MmFixed_t * p, char * pEntry );
+extern void Msat_MmFixedRestart( Msat_MmFixed_t * p );
+extern int Msat_MmFixedReadMemUsage( Msat_MmFixed_t * p );
+// flexible-size-block memory manager
+extern Msat_MmFlex_t * Msat_MmFlexStart();
+extern void Msat_MmFlexStop( Msat_MmFlex_t * p, int fVerbose );
+extern char * Msat_MmFlexEntryFetch( Msat_MmFlex_t * p, int nBytes );
+extern int Msat_MmFlexReadMemUsage( Msat_MmFlex_t * p );
+// hierarchical memory manager
+extern Msat_MmStep_t * Msat_MmStepStart( int nSteps );
+extern void Msat_MmStepStop( Msat_MmStep_t * p, int fVerbose );
+extern char * Msat_MmStepEntryFetch( Msat_MmStep_t * p, int nBytes );
+extern void Msat_MmStepEntryRecycle( Msat_MmStep_t * p, char * pEntry, int nBytes );
+extern int Msat_MmStepReadMemUsage( Msat_MmStep_t * p );
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+#endif
diff --git a/src/sat/msat/msatMem.c b/src/sat/msat/msatMem.c
new file mode 100644
index 00000000..30bf4a96
--- /dev/null
+++ b/src/sat/msat/msatMem.c
@@ -0,0 +1,529 @@
+/**CFile****************************************************************
+
+ FileName [msatMem.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [Memory managers borrowed from Extra.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatMem.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+struct Msat_MmFixed_t_
+{
+ // information about individual entries
+ int nEntrySize; // the size of one entry
+ int nEntriesAlloc; // the total number of entries allocated
+ int nEntriesUsed; // the number of entries in use
+ int nEntriesMax; // the max number of entries in use
+ char * pEntriesFree; // the linked list of free entries
+
+ // this is where the memory is stored
+ int nChunkSize; // the size of one chunk
+ int nChunksAlloc; // the maximum number of memory chunks
+ int nChunks; // the current number of memory chunks
+ char ** pChunks; // the allocated memory
+
+ // statistics
+ int nMemoryUsed; // memory used in the allocated entries
+ int nMemoryAlloc; // memory allocated
+};
+
+struct Msat_MmFlex_t_
+{
+ // information about individual entries
+ int nEntriesUsed; // the number of entries allocated
+ char * pCurrent; // the current pointer to free memory
+ char * pEnd; // the first entry outside the free memory
+
+ // this is where the memory is stored
+ int nChunkSize; // the size of one chunk
+ int nChunksAlloc; // the maximum number of memory chunks
+ int nChunks; // the current number of memory chunks
+ char ** pChunks; // the allocated memory
+
+ // statistics
+ int nMemoryUsed; // memory used in the allocated entries
+ int nMemoryAlloc; // memory allocated
+};
+
+
+struct Msat_MmStep_t_
+{
+ int nMems; // the number of fixed memory managers employed
+ Msat_MmFixed_t ** pMems; // memory managers: 2^1 words, 2^2 words, etc
+ int nMapSize; // the size of the memory array
+ Msat_MmFixed_t ** pMap; // maps the number of bytes into its memory manager
+};
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Allocates memory pieces of fixed size.]
+
+ Description [The size of the chunk is computed as the minimum of
+ 1024 entries and 64K. Can only work with entry size at least 4 byte long.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_MmFixed_t * Msat_MmFixedStart( int nEntrySize )
+{
+ Msat_MmFixed_t * p;
+
+ p = ALLOC( Msat_MmFixed_t, 1 );
+ memset( p, 0, sizeof(Msat_MmFixed_t) );
+
+ p->nEntrySize = nEntrySize;
+ p->nEntriesAlloc = 0;
+ p->nEntriesUsed = 0;
+ p->pEntriesFree = NULL;
+
+ if ( nEntrySize * (1 << 10) < (1<<16) )
+ p->nChunkSize = (1 << 10);
+ else
+ p->nChunkSize = (1<<16) / nEntrySize;
+ if ( p->nChunkSize < 8 )
+ p->nChunkSize = 8;
+
+ p->nChunksAlloc = 64;
+ p->nChunks = 0;
+ p->pChunks = ALLOC( char *, p->nChunksAlloc );
+
+ p->nMemoryUsed = 0;
+ p->nMemoryAlloc = 0;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_MmFixedStop( Msat_MmFixed_t * p, int fVerbose )
+{
+ int i;
+ if ( p == NULL )
+ return;
+ if ( fVerbose )
+ {
+ printf( "Fixed memory manager: Entry = %5d. Chunk = %5d. Chunks used = %5d.\n",
+ p->nEntrySize, p->nChunkSize, p->nChunks );
+ printf( " Entries used = %8d. Entries peak = %8d. Memory used = %8d. Memory alloc = %8d.\n",
+ p->nEntriesUsed, p->nEntriesMax, p->nEntrySize * p->nEntriesUsed, p->nMemoryAlloc );
+ }
+ for ( i = 0; i < p->nChunks; i++ )
+ free( p->pChunks[i] );
+ free( p->pChunks );
+ free( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+char * Msat_MmFixedEntryFetch( Msat_MmFixed_t * p )
+{
+ char * pTemp;
+ int i;
+
+ // check if there are still free entries
+ if ( p->nEntriesUsed == p->nEntriesAlloc )
+ { // need to allocate more entries
+ assert( p->pEntriesFree == NULL );
+ if ( p->nChunks == p->nChunksAlloc )
+ {
+ p->nChunksAlloc *= 2;
+ p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc );
+ }
+ p->pEntriesFree = ALLOC( char, p->nEntrySize * p->nChunkSize );
+ p->nMemoryAlloc += p->nEntrySize * p->nChunkSize;
+ // transform these entries into a linked list
+ pTemp = p->pEntriesFree;
+ for ( i = 1; i < p->nChunkSize; i++ )
+ {
+ *((char **)pTemp) = pTemp + p->nEntrySize;
+ pTemp += p->nEntrySize;
+ }
+ // set the last link
+ *((char **)pTemp) = NULL;
+ // add the chunk to the chunk storage
+ p->pChunks[ p->nChunks++ ] = p->pEntriesFree;
+ // add to the number of entries allocated
+ p->nEntriesAlloc += p->nChunkSize;
+ }
+ // incrememt the counter of used entries
+ p->nEntriesUsed++;
+ if ( p->nEntriesMax < p->nEntriesUsed )
+ p->nEntriesMax = p->nEntriesUsed;
+ // return the first entry in the free entry list
+ pTemp = p->pEntriesFree;
+ p->pEntriesFree = *((char **)pTemp);
+ return pTemp;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_MmFixedEntryRecycle( Msat_MmFixed_t * p, char * pEntry )
+{
+ // decrement the counter of used entries
+ p->nEntriesUsed--;
+ // add the entry to the linked list of free entries
+ *((char **)pEntry) = p->pEntriesFree;
+ p->pEntriesFree = pEntry;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description [Relocates all the memory except the first chunk.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_MmFixedRestart( Msat_MmFixed_t * p )
+{
+ int i;
+ char * pTemp;
+
+ // deallocate all chunks except the first one
+ for ( i = 1; i < p->nChunks; i++ )
+ free( p->pChunks[i] );
+ p->nChunks = 1;
+ // transform these entries into a linked list
+ pTemp = p->pChunks[0];
+ for ( i = 1; i < p->nChunkSize; i++ )
+ {
+ *((char **)pTemp) = pTemp + p->nEntrySize;
+ pTemp += p->nEntrySize;
+ }
+ // set the last link
+ *((char **)pTemp) = NULL;
+ // set the free entry list
+ p->pEntriesFree = p->pChunks[0];
+ // set the correct statistics
+ p->nMemoryAlloc = p->nEntrySize * p->nChunkSize;
+ p->nMemoryUsed = 0;
+ p->nEntriesAlloc = p->nChunkSize;
+ p->nEntriesUsed = 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_MmFixedReadMemUsage( Msat_MmFixed_t * p )
+{
+ return p->nMemoryAlloc;
+}
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Allocates entries of flexible size.]
+
+ Description [Can only work with entry size at least 4 byte long.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_MmFlex_t * Msat_MmFlexStart()
+{
+ Msat_MmFlex_t * p;
+
+ p = ALLOC( Msat_MmFlex_t, 1 );
+ memset( p, 0, sizeof(Msat_MmFlex_t) );
+
+ p->nEntriesUsed = 0;
+ p->pCurrent = NULL;
+ p->pEnd = NULL;
+
+ p->nChunkSize = (1 << 12);
+ p->nChunksAlloc = 64;
+ p->nChunks = 0;
+ p->pChunks = ALLOC( char *, p->nChunksAlloc );
+
+ p->nMemoryUsed = 0;
+ p->nMemoryAlloc = 0;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_MmFlexStop( Msat_MmFlex_t * p, int fVerbose )
+{
+ int i;
+ if ( p == NULL )
+ return;
+ if ( fVerbose )
+ {
+ printf( "Flexible memory manager: Chunk size = %d. Chunks used = %d.\n",
+ p->nChunkSize, p->nChunks );
+ printf( " Entries used = %d. Memory used = %d. Memory alloc = %d.\n",
+ p->nEntriesUsed, p->nMemoryUsed, p->nMemoryAlloc );
+ }
+ for ( i = 0; i < p->nChunks; i++ )
+ free( p->pChunks[i] );
+ free( p->pChunks );
+ free( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+char * Msat_MmFlexEntryFetch( Msat_MmFlex_t * p, int nBytes )
+{
+ char * pTemp;
+ // check if there are still free entries
+ if ( p->pCurrent == NULL || p->pCurrent + nBytes > p->pEnd )
+ { // need to allocate more entries
+ if ( p->nChunks == p->nChunksAlloc )
+ {
+ p->nChunksAlloc *= 2;
+ p->pChunks = REALLOC( char *, p->pChunks, p->nChunksAlloc );
+ }
+ if ( nBytes > p->nChunkSize )
+ {
+ // resize the chunk size if more memory is requested than it can give
+ // (ideally, this should never happen)
+ p->nChunkSize = 2 * nBytes;
+ }
+ p->pCurrent = ALLOC( char, p->nChunkSize );
+ p->pEnd = p->pCurrent + p->nChunkSize;
+ p->nMemoryAlloc += p->nChunkSize;
+ // add the chunk to the chunk storage
+ p->pChunks[ p->nChunks++ ] = p->pCurrent;
+ }
+ assert( p->pCurrent + nBytes <= p->pEnd );
+ // increment the counter of used entries
+ p->nEntriesUsed++;
+ // keep track of the memory used
+ p->nMemoryUsed += nBytes;
+ // return the next entry
+ pTemp = p->pCurrent;
+ p->pCurrent += nBytes;
+ return pTemp;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_MmFlexReadMemUsage( Msat_MmFlex_t * p )
+{
+ return p->nMemoryAlloc;
+}
+
+
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Starts the hierarchical memory manager.]
+
+ Description [This manager can allocate entries of any size.
+ Iternally they are mapped into the entries with the number of bytes
+ equal to the power of 2. The smallest entry size is 8 bytes. The
+ next one is 16 bytes etc. So, if the user requests 6 bytes, he gets
+ 8 byte entry. If we asks for 25 bytes, he gets 32 byte entry etc.
+ The input parameters "nSteps" says how many fixed memory managers
+ are employed internally. Calling this procedure with nSteps equal
+ to 10 results in 10 hierarchically arranged internal memory managers,
+ which can allocate up to 4096 (1Kb) entries. Requests for larger
+ entries are handed over to malloc() and then free()ed.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_MmStep_t * Msat_MmStepStart( int nSteps )
+{
+ Msat_MmStep_t * p;
+ int i, k;
+ p = ALLOC( Msat_MmStep_t, 1 );
+ p->nMems = nSteps;
+ // start the fixed memory managers
+ p->pMems = ALLOC( Msat_MmFixed_t *, p->nMems );
+ for ( i = 0; i < p->nMems; i++ )
+ p->pMems[i] = Msat_MmFixedStart( (8<<i) );
+ // set up the mapping of the required memory size into the corresponding manager
+ p->nMapSize = (4<<p->nMems);
+ p->pMap = ALLOC( Msat_MmFixed_t *, p->nMapSize+1 );
+ p->pMap[0] = NULL;
+ for ( k = 1; k <= 4; k++ )
+ p->pMap[k] = p->pMems[0];
+ for ( i = 0; i < p->nMems; i++ )
+ for ( k = (4<<i)+1; k <= (8<<i); k++ )
+ p->pMap[k] = p->pMems[i];
+//for ( i = 1; i < 100; i ++ )
+//printf( "%10d: size = %10d\n", i, p->pMap[i]->nEntrySize );
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Stops the memory manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_MmStepStop( Msat_MmStep_t * p, int fVerbose )
+{
+ int i;
+ for ( i = 0; i < p->nMems; i++ )
+ Msat_MmFixedStop( p->pMems[i], fVerbose );
+ free( p->pMems );
+ free( p->pMap );
+ free( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Creates the entry.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+char * Msat_MmStepEntryFetch( Msat_MmStep_t * p, int nBytes )
+{
+ if ( nBytes == 0 )
+ return NULL;
+ if ( nBytes > p->nMapSize )
+ {
+// printf( "Allocating %d bytes.\n", nBytes );
+ return ALLOC( char, nBytes );
+ }
+ return Msat_MmFixedEntryFetch( p->pMap[nBytes] );
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Recycles the entry.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_MmStepEntryRecycle( Msat_MmStep_t * p, char * pEntry, int nBytes )
+{
+ if ( nBytes == 0 )
+ return;
+ if ( nBytes > p->nMapSize )
+ {
+ free( pEntry );
+ return;
+ }
+ Msat_MmFixedEntryRecycle( p->pMap[nBytes], pEntry );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_MmStepReadMemUsage( Msat_MmStep_t * p )
+{
+ int i, nMemTotal = 0;
+ for ( i = 0; i < p->nMems; i++ )
+ nMemTotal += p->pMems[i]->nMemoryAlloc;
+ return nMemTotal;
+}
diff --git a/src/sat/msat/msatOrderH.c b/src/sat/msat/msatOrderH.c
new file mode 100644
index 00000000..956e7fc6
--- /dev/null
+++ b/src/sat/msat/msatOrderH.c
@@ -0,0 +1,405 @@
+/**CFile****************************************************************
+
+ FileName [msatOrder.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [The manager of variable assignment.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatOrder.c,v 1.0 2005/05/30 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+// the variable package data structure
+struct Msat_Order_t_
+{
+ Msat_Solver_t * pSat; // the SAT solver
+ Msat_IntVec_t * vIndex; // the heap
+ Msat_IntVec_t * vHeap; // the mapping of var num into its heap num
+};
+
+//The solver can communicate to the variable order the following parts:
+//- the array of current assignments (pSat->pAssigns)
+//- the array of variable activities (pSat->pdActivity)
+//- the array of variables currently in the cone (pSat->vConeVars)
+//- the array of arrays of variables adjucent to each(pSat->vAdjacents)
+
+#define HLEFT(i) ((i)<<1)
+#define HRIGHT(i) (((i)<<1)+1)
+#define HPARENT(i) ((i)>>1)
+#define HCOMPARE(p, i, j) ((p)->pSat->pdActivity[i] > (p)->pSat->pdActivity[j])
+#define HHEAP(p, i) ((p)->vHeap->pArray[i])
+#define HSIZE(p) ((p)->vHeap->nSize)
+#define HOKAY(p, i) ((i) >= 0 && (i) < (p)->vIndex->nSize)
+#define HINHEAP(p, i) (HOKAY(p, i) && (p)->vIndex->pArray[i] != 0)
+#define HEMPTY(p) (HSIZE(p) == 1)
+
+static int Msat_HeapCheck_rec( Msat_Order_t * p, int i );
+static int Msat_HeapGetTop( Msat_Order_t * p );
+static void Msat_HeapInsert( Msat_Order_t * p, int n );
+static void Msat_HeapIncrease( Msat_Order_t * p, int n );
+static void Msat_HeapPercolateUp( Msat_Order_t * p, int i );
+static void Msat_HeapPercolateDown( Msat_Order_t * p, int i );
+
+extern int timeSelect;
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Allocates the ordering structure.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_Order_t * Msat_OrderAlloc( Msat_Solver_t * pSat )
+{
+ Msat_Order_t * p;
+ p = ALLOC( Msat_Order_t, 1 );
+ memset( p, 0, sizeof(Msat_Order_t) );
+ p->pSat = pSat;
+ p->vIndex = Msat_IntVecAlloc( 0 );
+ p->vHeap = Msat_IntVecAlloc( 0 );
+ Msat_OrderSetBounds( p, pSat->nVarsAlloc );
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sets the bound of the ordering structure.]
+
+ Description [Should be called whenever the SAT solver is resized.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderSetBounds( Msat_Order_t * p, int nVarsMax )
+{
+ Msat_IntVecGrow( p->vIndex, nVarsMax );
+ Msat_IntVecGrow( p->vHeap, nVarsMax + 1 );
+ p->vIndex->nSize = nVarsMax;
+ p->vHeap->nSize = 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Cleans the ordering structure.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderClean( Msat_Order_t * p, Msat_IntVec_t * vCone )
+{
+ int i;
+ for ( i = 0; i < p->vIndex->nSize; i++ )
+ p->vIndex->pArray[i] = 0;
+ for ( i = 0; i < vCone->nSize; i++ )
+ {
+ assert( i+1 < p->vHeap->nCap );
+ p->vHeap->pArray[i+1] = vCone->pArray[i];
+
+ assert( vCone->pArray[i] < p->vIndex->nSize );
+ p->vIndex->pArray[vCone->pArray[i]] = i+1;
+ }
+ p->vHeap->nSize = vCone->nSize + 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Checks that the J-boundary is okay.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_OrderCheck( Msat_Order_t * p )
+{
+ return Msat_HeapCheck_rec( p, 1 );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Frees the ordering structure.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderFree( Msat_Order_t * p )
+{
+ Msat_IntVecFree( p->vHeap );
+ Msat_IntVecFree( p->vIndex );
+ free( p );
+}
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Selects the next variable to assign.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_OrderVarSelect( Msat_Order_t * p )
+{
+ // Activity based decision:
+// while (!heap.empty()){
+// Var next = heap.getmin();
+// if (toLbool(assigns[next]) == l_Undef)
+// return next;
+// }
+// return var_Undef;
+
+ int Var;
+ int clk = clock();
+
+ while ( !HEMPTY(p) )
+ {
+ Var = Msat_HeapGetTop(p);
+ if ( (p)->pSat->pAssigns[Var] == MSAT_VAR_UNASSIGNED )
+ {
+//assert( Msat_OrderCheck(p) );
+timeSelect += clock() - clk;
+ return Var;
+ }
+ }
+ return MSAT_ORDER_UNKNOWN;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Updates J-boundary when the variable is assigned.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderVarAssigned( Msat_Order_t * p, int Var )
+{
+}
+
+/**Function*************************************************************
+
+ Synopsis [Updates the order after a variable is unassigned.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderVarUnassigned( Msat_Order_t * p, int Var )
+{
+// if (!heap.inHeap(x))
+// heap.insert(x);
+
+ int clk = clock();
+ if ( !HINHEAP(p,Var) )
+ Msat_HeapInsert( p, Var );
+timeSelect += clock() - clk;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Updates the order after a variable changed weight.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderUpdate( Msat_Order_t * p, int Var )
+{
+// if (heap.inHeap(x))
+// heap.increase(x);
+
+ int clk = clock();
+ if ( HINHEAP(p,Var) )
+ Msat_HeapIncrease( p, Var );
+timeSelect += clock() - clk;
+}
+
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Checks the heap property recursively.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_HeapCheck_rec( Msat_Order_t * p, int i )
+{
+ return i >= HSIZE(p) ||
+ ( HPARENT(i) == 0 || !HCOMPARE(p, HHEAP(p, i), HHEAP(p, HPARENT(i))) ) &&
+ Msat_HeapCheck_rec( p, HLEFT(i) ) && Msat_HeapCheck_rec( p, HRIGHT(i) );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Retrieves the minimum element.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_HeapGetTop( Msat_Order_t * p )
+{
+ int Result, NewTop;
+ Result = HHEAP(p, 1);
+ NewTop = Msat_IntVecPop( p->vHeap );
+ p->vHeap->pArray[1] = NewTop;
+ p->vIndex->pArray[NewTop] = 1;
+ p->vIndex->pArray[Result] = 0;
+ if ( p->vHeap->nSize > 1 )
+ Msat_HeapPercolateDown( p, 1 );
+ return Result;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Inserts the new element.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_HeapInsert( Msat_Order_t * p, int n )
+{
+ assert( HOKAY(p, n) );
+ p->vIndex->pArray[n] = HSIZE(p);
+ Msat_IntVecPush( p->vHeap, n );
+ Msat_HeapPercolateUp( p, p->vIndex->pArray[n] );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Inserts the new element.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_HeapIncrease( Msat_Order_t * p, int n )
+{
+ Msat_HeapPercolateUp( p, p->vIndex->pArray[n] );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Moves the entry up.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_HeapPercolateUp( Msat_Order_t * p, int i )
+{
+ int x = HHEAP(p, i);
+ while ( HPARENT(i) != 0 && HCOMPARE(p, x, HHEAP(p, HPARENT(i))) )
+ {
+ p->vHeap->pArray[i] = HHEAP(p, HPARENT(i));
+ p->vIndex->pArray[HHEAP(p, i)] = i;
+ i = HPARENT(i);
+ }
+ p->vHeap->pArray[i] = x;
+ p->vIndex->pArray[x] = i;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Moves the entry down.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_HeapPercolateDown( Msat_Order_t * p, int i )
+{
+ int x = HHEAP(p, i);
+ int Child;
+ while ( HLEFT(i) < HSIZE(p) )
+ {
+ if ( HRIGHT(i) < HSIZE(p) && HCOMPARE(p, HHEAP(p, HRIGHT(i)), HHEAP(p, HLEFT(i))) )
+ Child = HRIGHT(i);
+ else
+ Child = HLEFT(i);
+ if ( !HCOMPARE(p, HHEAP(p, Child), x) )
+ break;
+ p->vHeap->pArray[i] = HHEAP(p, Child);
+ p->vIndex->pArray[HHEAP(p, i)] = i;
+ i = Child;
+ }
+ p->vHeap->pArray[i] = x;
+ p->vIndex->pArray[x] = i;
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/msat/msatOrderJ.c b/src/sat/msat/msatOrderJ.c
new file mode 100644
index 00000000..4db7ff7b
--- /dev/null
+++ b/src/sat/msat/msatOrderJ.c
@@ -0,0 +1,472 @@
+/**CFile****************************************************************
+
+ FileName [msatOrder.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [The manager of variable assignment.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatOrder.c,v 1.0 2005/05/30 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+/*
+The J-boundary (justification boundary) is defined as a set of unassigned
+variables belonging to the cone of interest, such that for each of them,
+there exist an adjacent assigned variable in the cone of interest.
+*/
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+typedef struct Msat_OrderVar_t_ Msat_OrderVar_t;
+typedef struct Msat_OrderRing_t_ Msat_OrderRing_t;
+
+// the variable data structure
+struct Msat_OrderVar_t_
+{
+ Msat_OrderVar_t * pNext;
+ Msat_OrderVar_t * pPrev;
+ int Num;
+};
+
+// the ring of variables data structure (J-boundary)
+struct Msat_OrderRing_t_
+{
+ Msat_OrderVar_t * pRoot;
+ int nItems;
+};
+
+// the variable package data structure
+struct Msat_Order_t_
+{
+ Msat_Solver_t * pSat; // the SAT solver
+ Msat_OrderVar_t * pVars; // the storage for variables
+ int nVarsAlloc; // the number of variables allocated
+ Msat_OrderRing_t rVars; // the J-boundary as a ring of variables
+};
+
+//The solver can communicate to the variable order the following parts:
+//- the array of current assignments (pSat->pAssigns)
+//- the array of variable activities (pSat->pdActivity)
+//- the array of variables currently in the cone (pSat->vConeVars)
+//- the array of arrays of variables adjucent to each(pSat->vAdjacents)
+
+#define Msat_OrderVarIsInBoundary( p, i ) ((p)->pVars[i].pNext)
+#define Msat_OrderVarIsAssigned( p, i ) ((p)->pSat->pAssigns[i] != MSAT_VAR_UNASSIGNED)
+#define Msat_OrderVarIsUsedInCone( p, i ) ((p)->pSat->vVarsUsed->pArray[i])
+
+// iterator through the entries in J-boundary
+#define Msat_OrderRingForEachEntry( pRing, pVar, pNext ) \
+ for ( pVar = pRing, \
+ pNext = pVar? pVar->pNext : NULL; \
+ pVar; \
+ pVar = (pNext != pRing)? pNext : NULL, \
+ pNext = pVar? pVar->pNext : NULL )
+
+static void Msat_OrderRingAddLast( Msat_OrderRing_t * pRing, Msat_OrderVar_t * pVar );
+static void Msat_OrderRingRemove( Msat_OrderRing_t * pRing, Msat_OrderVar_t * pVar );
+
+extern int timeSelect;
+extern int timeAssign;
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Allocates the ordering structure.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_Order_t * Msat_OrderAlloc( Msat_Solver_t * pSat )
+{
+ Msat_Order_t * p;
+ p = ALLOC( Msat_Order_t, 1 );
+ memset( p, 0, sizeof(Msat_Order_t) );
+ p->pSat = pSat;
+ Msat_OrderSetBounds( p, pSat->nVarsAlloc );
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sets the bound of the ordering structure.]
+
+ Description [Should be called whenever the SAT solver is resized.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderSetBounds( Msat_Order_t * p, int nVarsMax )
+{
+ int i;
+ // add variables if they are missing
+ if ( p->nVarsAlloc < nVarsMax )
+ {
+ p->pVars = REALLOC( Msat_OrderVar_t, p->pVars, nVarsMax );
+ for ( i = p->nVarsAlloc; i < nVarsMax; i++ )
+ {
+ p->pVars[i].pNext = p->pVars[i].pPrev = NULL;
+ p->pVars[i].Num = i;
+ }
+ p->nVarsAlloc = nVarsMax;
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Cleans the ordering structure.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderClean( Msat_Order_t * p, Msat_IntVec_t * vCone )
+{
+ Msat_OrderVar_t * pVar, * pNext;
+ // quickly undo the ring
+ Msat_OrderRingForEachEntry( p->rVars.pRoot, pVar, pNext )
+ pVar->pNext = pVar->pPrev = NULL;
+ p->rVars.pRoot = NULL;
+ p->rVars.nItems = 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Checks that the J-boundary is okay.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_OrderCheck( Msat_Order_t * p )
+{
+ Msat_OrderVar_t * pVar, * pNext;
+ Msat_IntVec_t * vRound;
+ int * pRound, nRound;
+ int * pVars, nVars, i, k;
+ int Counter = 0;
+
+ // go through all the variables in the boundary
+ Msat_OrderRingForEachEntry( p->rVars.pRoot, pVar, pNext )
+ {
+ assert( !Msat_OrderVarIsAssigned(p, pVar->Num) );
+ // go though all the variables in the neighborhood
+ // and check that it is true that there is least one assigned
+ vRound = (Msat_IntVec_t *)Msat_ClauseVecReadEntry( p->pSat->vAdjacents, pVar->Num );
+ nRound = Msat_IntVecReadSize( vRound );
+ pRound = Msat_IntVecReadArray( vRound );
+ for ( i = 0; i < nRound; i++ )
+ {
+ if ( !Msat_OrderVarIsUsedInCone(p, pRound[i]) )
+ continue;
+ if ( Msat_OrderVarIsAssigned(p, pRound[i]) )
+ break;
+ }
+// assert( i != nRound );
+// if ( i == nRound )
+// return 0;
+ if ( i == nRound )
+ Counter++;
+ }
+ if ( Counter > 0 )
+ printf( "%d(%d) ", Counter, p->rVars.nItems );
+
+ // we may also check other unassigned variables in the cone
+ // to make sure that if they are not in J-boundary,
+ // then they do not have an assigned neighbor
+ nVars = Msat_IntVecReadSize( p->pSat->vConeVars );
+ pVars = Msat_IntVecReadArray( p->pSat->vConeVars );
+ for ( i = 0; i < nVars; i++ )
+ {
+ assert( Msat_OrderVarIsUsedInCone(p, pVars[i]) );
+ // skip assigned vars, vars in the boundary, and vars not used in the cone
+ if ( Msat_OrderVarIsAssigned(p, pVars[i]) ||
+ Msat_OrderVarIsInBoundary(p, pVars[i]) )
+ continue;
+ // make sure, it does not have assigned neighbors
+ vRound = (Msat_IntVec_t *)Msat_ClauseVecReadEntry( p->pSat->vAdjacents, pVars[i] );
+ nRound = Msat_IntVecReadSize( vRound );
+ pRound = Msat_IntVecReadArray( vRound );
+ for ( k = 0; k < nRound; k++ )
+ {
+ if ( !Msat_OrderVarIsUsedInCone(p, pRound[k]) )
+ continue;
+ if ( Msat_OrderVarIsAssigned(p, pRound[k]) )
+ break;
+ }
+// assert( k == nRound );
+// if ( k != nRound )
+// return 0;
+ }
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Frees the ordering structure.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderFree( Msat_Order_t * p )
+{
+ free( p->pVars );
+ free( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Selects the next variable to assign.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_OrderVarSelect( Msat_Order_t * p )
+{
+ Msat_OrderVar_t * pVar, * pNext, * pVarBest;
+ double * pdActs = p->pSat->pdActivity;
+ double dfActBest;
+// int clk = clock();
+
+ pVarBest = NULL;
+ dfActBest = -1.0;
+ Msat_OrderRingForEachEntry( p->rVars.pRoot, pVar, pNext )
+ {
+ if ( dfActBest < pdActs[pVar->Num] )
+ {
+ dfActBest = pdActs[pVar->Num];
+ pVarBest = pVar;
+ }
+ }
+//timeSelect += clock() - clk;
+//timeAssign += clock() - clk;
+
+//if ( pVarBest && pVarBest->Num % 1000 == 0 )
+//printf( "%d ", p->rVars.nItems );
+
+// Msat_OrderCheck( p );
+ if ( pVarBest )
+ {
+ assert( Msat_OrderVarIsUsedInCone(p, pVarBest->Num) );
+ return pVarBest->Num;
+ }
+ return MSAT_ORDER_UNKNOWN;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Updates J-boundary when the variable is assigned.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderVarAssigned( Msat_Order_t * p, int Var )
+{
+ Msat_IntVec_t * vRound;
+ int i;//, clk = clock();
+
+ // make sure the variable is in the boundary
+ assert( Var < p->nVarsAlloc );
+ // if it is not in the boundary (initial decision, random decision), do not remove
+ if ( Msat_OrderVarIsInBoundary( p, Var ) )
+ Msat_OrderRingRemove( &p->rVars, &p->pVars[Var] );
+ // add to the boundary those neighbors that are (1) unassigned, (2) not in boundary
+ // because for them we know that there is a variable (Var) which is assigned
+ vRound = (Msat_IntVec_t *)p->pSat->vAdjacents->pArray[Var];
+ for ( i = 0; i < vRound->nSize; i++ )
+ {
+ if ( !Msat_OrderVarIsUsedInCone(p, vRound->pArray[i]) )
+ continue;
+ if ( Msat_OrderVarIsAssigned(p, vRound->pArray[i]) )
+ continue;
+ if ( Msat_OrderVarIsInBoundary(p, vRound->pArray[i]) )
+ continue;
+ Msat_OrderRingAddLast( &p->rVars, &p->pVars[vRound->pArray[i]] );
+ }
+//timeSelect += clock() - clk;
+// Msat_OrderCheck( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Updates the order after a variable is unassigned.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderVarUnassigned( Msat_Order_t * p, int Var )
+{
+ Msat_IntVec_t * vRound, * vRound2;
+ int i, k;//, clk = clock();
+
+ // make sure the variable is not in the boundary
+ assert( Var < p->nVarsAlloc );
+ assert( !Msat_OrderVarIsInBoundary( p, Var ) );
+ // go through its neigbors - if one of them is assigned add this var
+ // add to the boundary those neighbors that are not there already
+ // this will also get rid of variable outside of the current cone
+ // because they are unassigned in Msat_SolverPrepare()
+ vRound = (Msat_IntVec_t *)p->pSat->vAdjacents->pArray[Var];
+ for ( i = 0; i < vRound->nSize; i++ )
+ if ( Msat_OrderVarIsAssigned(p, vRound->pArray[i]) )
+ break;
+ if ( i != vRound->nSize )
+ Msat_OrderRingAddLast( &p->rVars, &p->pVars[Var] );
+
+ // unassigning a variable may lead to its adjacents dropping from the boundary
+ for ( i = 0; i < vRound->nSize; i++ )
+ if ( Msat_OrderVarIsInBoundary(p, vRound->pArray[i]) )
+ { // the neighbor is in the J-boundary (and unassigned)
+ assert( !Msat_OrderVarIsAssigned(p, vRound->pArray[i]) );
+ vRound2 = (Msat_IntVec_t *)p->pSat->vAdjacents->pArray[vRound->pArray[i]];
+ // go through its neighbors and determine if there is at least one assigned
+ for ( k = 0; k < vRound2->nSize; k++ )
+ if ( Msat_OrderVarIsAssigned(p, vRound2->pArray[k]) )
+ break;
+ if ( k == vRound2->nSize ) // there is no assigned vars, delete this one
+ Msat_OrderRingRemove( &p->rVars, &p->pVars[vRound->pArray[i]] );
+ }
+//timeSelect += clock() - clk;
+// Msat_OrderCheck( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Updates the order after a variable changed weight.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderUpdate( Msat_Order_t * p, int Var )
+{
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Adds node to the end of the ring.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderRingAddLast( Msat_OrderRing_t * pRing, Msat_OrderVar_t * pVar )
+{
+//printf( "adding %d\n", pVar->Num );
+ // check that the node is not in a ring
+ assert( pVar->pPrev == NULL );
+ assert( pVar->pNext == NULL );
+ // if the ring is empty, make the node point to itself
+ pRing->nItems++;
+ if ( pRing->pRoot == NULL )
+ {
+ pRing->pRoot = pVar;
+ pVar->pPrev = pVar;
+ pVar->pNext = pVar;
+ return;
+ }
+ // if the ring is not empty, add it as the last entry
+ pVar->pPrev = pRing->pRoot->pPrev;
+ pVar->pNext = pRing->pRoot;
+ pVar->pPrev->pNext = pVar;
+ pVar->pNext->pPrev = pVar;
+
+ // move the root so that it points to the new entry
+// pRing->pRoot = pRing->pRoot->pPrev;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Removes the node from the ring.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_OrderRingRemove( Msat_OrderRing_t * pRing, Msat_OrderVar_t * pVar )
+{
+//printf( "removing %d\n", pVar->Num );
+ // check that the var is in a ring
+ assert( pVar->pPrev );
+ assert( pVar->pNext );
+ pRing->nItems--;
+ if ( pRing->nItems == 0 )
+ {
+ assert( pRing->pRoot == pVar );
+ pVar->pPrev = NULL;
+ pVar->pNext = NULL;
+ pRing->pRoot = NULL;
+ return;
+ }
+ // move the root if needed
+ if ( pRing->pRoot == pVar )
+ pRing->pRoot = pVar->pNext;
+ // move the root to the next entry after pVar
+ // this way all the additions to the list will be traversed first
+// pRing->pRoot = pVar->pPrev;
+ // delete the node
+ pVar->pPrev->pNext = pVar->pNext;
+ pVar->pNext->pPrev = pVar->pPrev;
+ pVar->pPrev = NULL;
+ pVar->pNext = NULL;
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/msat/msatQueue.c b/src/sat/msat/msatQueue.c
new file mode 100644
index 00000000..5938e042
--- /dev/null
+++ b/src/sat/msat/msatQueue.c
@@ -0,0 +1,157 @@
+/**CFile****************************************************************
+
+ FileName [msatQueue.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [The manager of the assignment propagation queue.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatQueue.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+struct Msat_Queue_t_
+{
+ int nVars;
+ int * pVars;
+ int iFirst;
+ int iLast;
+};
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Allocates the variable propagation queue.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_Queue_t * Msat_QueueAlloc( int nVars )
+{
+ Msat_Queue_t * p;
+ p = ALLOC( Msat_Queue_t, 1 );
+ memset( p, 0, sizeof(Msat_Queue_t) );
+ p->nVars = nVars;
+ p->pVars = ALLOC( int, nVars );
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Deallocate the variable propagation queue.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_QueueFree( Msat_Queue_t * p )
+{
+ free( p->pVars );
+ free( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reads the queue size.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_QueueReadSize( Msat_Queue_t * p )
+{
+ return p->iLast - p->iFirst;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Insert an entry into the queue.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_QueueInsert( Msat_Queue_t * p, int Lit )
+{
+ if ( p->iLast == p->nVars )
+ {
+ int i;
+ assert( 0 );
+ for ( i = 0; i < p->iLast; i++ )
+ printf( "entry = %2d lit = %2d var = %2d \n", i, p->pVars[i], p->pVars[i]/2 );
+ }
+ assert( p->iLast < p->nVars );
+ p->pVars[p->iLast++] = Lit;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Extracts an entry from the queue.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_QueueExtract( Msat_Queue_t * p )
+{
+ if ( p->iFirst == p->iLast )
+ return -1;
+ return p->pVars[p->iFirst++];
+}
+
+/**Function*************************************************************
+
+ Synopsis [Resets the queue.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_QueueClear( Msat_Queue_t * p )
+{
+ p->iFirst = 0;
+ p->iLast = 0;
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/msat/msatRead.c b/src/sat/msat/msatRead.c
new file mode 100644
index 00000000..738562ef
--- /dev/null
+++ b/src/sat/msat/msatRead.c
@@ -0,0 +1,268 @@
+/**CFile****************************************************************
+
+ FileName [msatRead.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [The reader of the CNF formula in DIMACS format.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatRead.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+static char * Msat_FileRead( FILE * pFile );
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Read the file into the internal buffer.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+char * Msat_FileRead( FILE * pFile )
+{
+ int nFileSize;
+ char * pBuffer;
+ // get the file size, in bytes
+ fseek( pFile, 0, SEEK_END );
+ nFileSize = ftell( pFile );
+ // move the file current reading position to the beginning
+ rewind( pFile );
+ // load the contents of the file into memory
+ pBuffer = ALLOC( char, nFileSize + 3 );
+ fread( pBuffer, nFileSize, 1, pFile );
+ // terminate the string with '\0'
+ pBuffer[ nFileSize + 0] = '\n';
+ pBuffer[ nFileSize + 1] = '\0';
+ return pBuffer;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static void Msat_ReadWhitespace( char ** pIn )
+{
+ while ((**pIn >= 9 && **pIn <= 13) || **pIn == 32)
+ (*pIn)++;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static void Msat_ReadNotWhitespace( char ** pIn )
+{
+ while ( !((**pIn >= 9 && **pIn <= 13) || **pIn == 32) )
+ (*pIn)++;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static void skipLine( char ** pIn )
+{
+ while ( 1 )
+ {
+ if (**pIn == 0)
+ return;
+ if (**pIn == '\n')
+ {
+ (*pIn)++;
+ return;
+ }
+ (*pIn)++;
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static int Msat_ReadInt( char ** pIn )
+{
+ int val = 0;
+ bool neg = 0;
+
+ Msat_ReadWhitespace( pIn );
+ if ( **pIn == '-' )
+ neg = 1,
+ (*pIn)++;
+ else if ( **pIn == '+' )
+ (*pIn)++;
+ if ( **pIn < '0' || **pIn > '9' )
+ fprintf(stderr, "PARSE ERROR! Unexpected char: %c\n", **pIn),
+ exit(1);
+ while ( **pIn >= '0' && **pIn <= '9' )
+ val = val*10 + (**pIn - '0'),
+ (*pIn)++;
+ return neg ? -val : val;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static void Msat_ReadClause( char ** pIn, Msat_Solver_t * p, Msat_IntVec_t * pLits )
+{
+ int nVars = Msat_SolverReadVarNum( p );
+ int parsed_lit, var, sign;
+
+ Msat_IntVecClear( pLits );
+ while ( 1 )
+ {
+ parsed_lit = Msat_ReadInt(pIn);
+ if ( parsed_lit == 0 )
+ break;
+ var = abs(parsed_lit) - 1;
+ sign = (parsed_lit > 0);
+ if ( var >= nVars )
+ {
+ printf( "Variable %d is larger than the number of allocated variables (%d).\n", var+1, nVars );
+ exit(1);
+ }
+ Msat_IntVecPush( pLits, MSAT_VAR2LIT(var, !sign) );
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static bool Msat_ReadDimacs( char * pText, Msat_Solver_t ** pS, bool fVerbose )
+{
+ Msat_Solver_t * p;
+ Msat_IntVec_t * pLits;
+ char * pIn = pText;
+ int nVars, nClas;
+ while ( 1 )
+ {
+ Msat_ReadWhitespace( &pIn );
+ if ( *pIn == 0 )
+ break;
+ else if ( *pIn == 'c' )
+ skipLine( &pIn );
+ else if ( *pIn == 'p' )
+ {
+ pIn++;
+ Msat_ReadWhitespace( &pIn );
+ Msat_ReadNotWhitespace( &pIn );
+
+ nVars = Msat_ReadInt( &pIn );
+ nClas = Msat_ReadInt( &pIn );
+ skipLine( &pIn );
+ // start the solver
+ p = Msat_SolverAlloc( nVars, 1, 1, 1, 1, 0 );
+ Msat_SolverClean( p, nVars );
+ Msat_SolverSetVerbosity( p, fVerbose );
+ // allocate the vector
+ pLits = Msat_IntVecAlloc( nVars );
+ }
+ else
+ {
+ if ( p == NULL )
+ {
+ printf( "There is no parameter line.\n" );
+ exit(1);
+ }
+ Msat_ReadClause( &pIn, p, pLits );
+ if ( !Msat_SolverAddClause( p, pLits ) )
+ return 0;
+ }
+ }
+ Msat_IntVecFree( pLits );
+ *pS = p;
+ return Msat_SolverSimplifyDB( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Starts the solver and reads the DIMAC file.]
+
+ Description [Returns FALSE upon immediate conflict.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_SolverParseDimacs( FILE * pFile, Msat_Solver_t ** p, int fVerbose )
+{
+ char * pText;
+ bool Value;
+ pText = Msat_FileRead( pFile );
+ Value = Msat_ReadDimacs( pText, p, fVerbose );
+ free( pText );
+ return Value;
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/msat/msatSolverApi.c b/src/sat/msat/msatSolverApi.c
new file mode 100644
index 00000000..ee3507a6
--- /dev/null
+++ b/src/sat/msat/msatSolverApi.c
@@ -0,0 +1,500 @@
+/**CFile****************************************************************
+
+ FileName [msatSolverApi.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [APIs of the SAT solver.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatSolverApi.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+static void Msat_SolverSetupTruthTables( unsigned uTruths[][2] );
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Simple SAT solver APIs.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_SolverReadVarNum( Msat_Solver_t * p ) { return p->nVars; }
+int Msat_SolverReadClauseNum( Msat_Solver_t * p ) { return p->nClauses; }
+int Msat_SolverReadVarAllocNum( Msat_Solver_t * p ) { return p->nVarsAlloc; }
+int Msat_SolverReadDecisionLevel( Msat_Solver_t * p ) { return Msat_IntVecReadSize(p->vTrailLim); }
+int * Msat_SolverReadDecisionLevelArray( Msat_Solver_t * p ) { return p->pLevel; }
+Msat_Clause_t ** Msat_SolverReadReasonArray( Msat_Solver_t * p ) { return p->pReasons; }
+Msat_Lit_t Msat_SolverReadVarValue( Msat_Solver_t * p, Msat_Var_t Var ) { return p->pAssigns[Var]; }
+Msat_ClauseVec_t * Msat_SolverReadLearned( Msat_Solver_t * p ) { return p->vLearned; }
+Msat_ClauseVec_t ** Msat_SolverReadWatchedArray( Msat_Solver_t * p ) { return p->pvWatched; }
+int * Msat_SolverReadAssignsArray( Msat_Solver_t * p ) { return p->pAssigns; }
+int * Msat_SolverReadModelArray( Msat_Solver_t * p ) { return p->pModel; }
+int Msat_SolverReadBackTracks( Msat_Solver_t * p ) { return (int)p->Stats.nConflicts; }
+int Msat_SolverReadInspects( Msat_Solver_t * p ) { return (int)p->Stats.nInspects; }
+Msat_MmStep_t * Msat_SolverReadMem( Msat_Solver_t * p ) { return p->pMem; }
+int * Msat_SolverReadSeenArray( Msat_Solver_t * p ) { return p->pSeen; }
+int Msat_SolverIncrementSeenId( Msat_Solver_t * p ) { return ++p->nSeenId; }
+void Msat_SolverSetVerbosity( Msat_Solver_t * p, int fVerbose ) { p->fVerbose = fVerbose; }
+void Msat_SolverClausesIncrement( Msat_Solver_t * p ) { p->nClausesAlloc++; }
+void Msat_SolverClausesDecrement( Msat_Solver_t * p ) { p->nClausesAlloc--; }
+void Msat_SolverClausesIncrementL( Msat_Solver_t * p ) { p->nClausesAllocL++; }
+void Msat_SolverClausesDecrementL( Msat_Solver_t * p ) { p->nClausesAllocL--; }
+void Msat_SolverMarkLastClauseTypeA( Msat_Solver_t * p ) { Msat_ClauseSetTypeA( Msat_ClauseVecReadEntry( p->vClauses, Msat_ClauseVecReadSize(p->vClauses)-1 ), 1 ); }
+void Msat_SolverMarkClausesStart( Msat_Solver_t * p ) { p->nClausesStart = Msat_ClauseVecReadSize(p->vClauses); }
+float * Msat_SolverReadFactors( Msat_Solver_t * p ) { return p->pFactors; }
+
+/**Function*************************************************************
+
+ Synopsis [Reads the clause with the given number.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_Clause_t * Msat_SolverReadClause( Msat_Solver_t * p, int Num )
+{
+ int nClausesP;
+ assert( Num < p->nClauses );
+ nClausesP = Msat_ClauseVecReadSize( p->vClauses );
+ if ( Num < nClausesP )
+ return Msat_ClauseVecReadEntry( p->vClauses, Num );
+ return Msat_ClauseVecReadEntry( p->vLearned, Num - nClausesP );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reads the clause with the given number.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_ClauseVec_t * Msat_SolverReadAdjacents( Msat_Solver_t * p )
+{
+ return p->vAdjacents;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reads the clause with the given number.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_IntVec_t * Msat_SolverReadConeVars( Msat_Solver_t * p )
+{
+ return p->vConeVars;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reads the clause with the given number.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_IntVec_t * Msat_SolverReadVarsUsed( Msat_Solver_t * p )
+{
+ return p->vVarsUsed;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Allocates the solver.]
+
+ Description [After the solver is allocated, the procedure
+ Msat_SolverClean() should be called to set the number of variables.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_Solver_t * Msat_SolverAlloc( int nVarsAlloc,
+ double dClaInc, double dClaDecay,
+ double dVarInc, double dVarDecay,
+ bool fVerbose )
+{
+ Msat_Solver_t * p;
+ int i;
+
+ assert(sizeof(Msat_Lit_t) == sizeof(unsigned));
+ assert(sizeof(float) == sizeof(unsigned));
+
+ p = ALLOC( Msat_Solver_t, 1 );
+ memset( p, 0, sizeof(Msat_Solver_t) );
+
+ p->nVarsAlloc = nVarsAlloc;
+ p->nVars = 0;
+
+ p->nClauses = 0;
+ p->vClauses = Msat_ClauseVecAlloc( 512 );
+ p->vLearned = Msat_ClauseVecAlloc( 512 );
+
+ p->dClaInc = dClaInc;
+ p->dClaDecay = dClaDecay;
+ p->dVarInc = dVarInc;
+ p->dVarDecay = dVarDecay;
+
+ p->pdActivity = ALLOC( double, p->nVarsAlloc );
+ p->pFactors = ALLOC( float, p->nVarsAlloc );
+ for ( i = 0; i < p->nVarsAlloc; i++ )
+ {
+ p->pdActivity[i] = 0.0;
+ p->pFactors[i] = 1.0;
+ }
+
+ p->pAssigns = ALLOC( int, p->nVarsAlloc );
+ p->pModel = ALLOC( int, p->nVarsAlloc );
+ for ( i = 0; i < p->nVarsAlloc; i++ )
+ p->pAssigns[i] = MSAT_VAR_UNASSIGNED;
+// p->pOrder = Msat_OrderAlloc( p->pAssigns, p->pdActivity, p->nVarsAlloc );
+ p->pOrder = Msat_OrderAlloc( p );
+
+ p->pvWatched = ALLOC( Msat_ClauseVec_t *, 2 * p->nVarsAlloc );
+ for ( i = 0; i < 2 * p->nVarsAlloc; i++ )
+ p->pvWatched[i] = Msat_ClauseVecAlloc( 16 );
+ p->pQueue = Msat_QueueAlloc( p->nVarsAlloc );
+
+ p->vTrail = Msat_IntVecAlloc( p->nVarsAlloc );
+ p->vTrailLim = Msat_IntVecAlloc( p->nVarsAlloc );
+ p->pReasons = ALLOC( Msat_Clause_t *, p->nVarsAlloc );
+ memset( p->pReasons, 0, sizeof(Msat_Clause_t *) * p->nVarsAlloc );
+ p->pLevel = ALLOC( int, p->nVarsAlloc );
+ for ( i = 0; i < p->nVarsAlloc; i++ )
+ p->pLevel[i] = -1;
+ p->dRandSeed = 91648253;
+ p->fVerbose = fVerbose;
+ p->dProgress = 0.0;
+// p->pModel = Msat_IntVecAlloc( p->nVarsAlloc );
+ p->pMem = Msat_MmStepStart( 10 );
+
+ p->vConeVars = Msat_IntVecAlloc( p->nVarsAlloc );
+ p->vAdjacents = Msat_ClauseVecAlloc( p->nVarsAlloc );
+ for ( i = 0; i < p->nVarsAlloc; i++ )
+ Msat_ClauseVecPush( p->vAdjacents, (Msat_Clause_t *)Msat_IntVecAlloc(5) );
+ p->vVarsUsed = Msat_IntVecAlloc( p->nVarsAlloc );
+ Msat_IntVecFill( p->vVarsUsed, p->nVarsAlloc, 1 );
+
+
+ p->pSeen = ALLOC( int, p->nVarsAlloc );
+ memset( p->pSeen, 0, sizeof(int) * p->nVarsAlloc );
+ p->nSeenId = 1;
+ p->vReason = Msat_IntVecAlloc( p->nVarsAlloc );
+ p->vTemp = Msat_IntVecAlloc( p->nVarsAlloc );
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Resizes the solver.]
+
+ Description [Assumes that the solver contains some clauses, and that
+ it is currently between the calls. Resizes the solver to accomodate
+ more variables.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverResize( Msat_Solver_t * p, int nVarsAlloc )
+{
+ int nVarsAllocOld, i;
+
+ nVarsAllocOld = p->nVarsAlloc;
+ p->nVarsAlloc = nVarsAlloc;
+
+ p->pdActivity = REALLOC( double, p->pdActivity, p->nVarsAlloc );
+ p->pFactors = REALLOC( float, p->pFactors, p->nVarsAlloc );
+ for ( i = nVarsAllocOld; i < p->nVarsAlloc; i++ )
+ {
+ p->pdActivity[i] = 0.0;
+ p->pFactors[i] = 1.0;
+ }
+
+ p->pAssigns = REALLOC( int, p->pAssigns, p->nVarsAlloc );
+ p->pModel = REALLOC( int, p->pModel, p->nVarsAlloc );
+ for ( i = nVarsAllocOld; i < p->nVarsAlloc; i++ )
+ p->pAssigns[i] = MSAT_VAR_UNASSIGNED;
+
+// Msat_OrderRealloc( p->pOrder, p->pAssigns, p->pdActivity, p->nVarsAlloc );
+ Msat_OrderSetBounds( p->pOrder, p->nVarsAlloc );
+
+ p->pvWatched = REALLOC( Msat_ClauseVec_t *, p->pvWatched, 2 * p->nVarsAlloc );
+ for ( i = 2 * nVarsAllocOld; i < 2 * p->nVarsAlloc; i++ )
+ p->pvWatched[i] = Msat_ClauseVecAlloc( 16 );
+
+ Msat_QueueFree( p->pQueue );
+ p->pQueue = Msat_QueueAlloc( p->nVarsAlloc );
+
+ p->pReasons = REALLOC( Msat_Clause_t *, p->pReasons, p->nVarsAlloc );
+ p->pLevel = REALLOC( int, p->pLevel, p->nVarsAlloc );
+ for ( i = nVarsAllocOld; i < p->nVarsAlloc; i++ )
+ {
+ p->pReasons[i] = NULL;
+ p->pLevel[i] = -1;
+ }
+
+ p->pSeen = REALLOC( int, p->pSeen, p->nVarsAlloc );
+ for ( i = nVarsAllocOld; i < p->nVarsAlloc; i++ )
+ p->pSeen[i] = 0;
+
+ Msat_IntVecGrow( p->vTrail, p->nVarsAlloc );
+ Msat_IntVecGrow( p->vTrailLim, p->nVarsAlloc );
+
+ // make sure the array of adjucents has room to store the variable numbers
+ for ( i = Msat_ClauseVecReadSize(p->vAdjacents); i < p->nVarsAlloc; i++ )
+ Msat_ClauseVecPush( p->vAdjacents, (Msat_Clause_t *)Msat_IntVecAlloc(5) );
+ Msat_IntVecFill( p->vVarsUsed, p->nVarsAlloc, 1 );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prepares the solver.]
+
+ Description [Cleans the solver assuming that the problem will involve
+ the given number of variables (nVars). This procedure is useful
+ for many small (incremental) SAT problems, to prevent the solver
+ from being reallocated each time.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverClean( Msat_Solver_t * p, int nVars )
+{
+ int i;
+ // free the clauses
+ int nClauses;
+ Msat_Clause_t ** pClauses;
+
+ assert( p->nVarsAlloc >= nVars );
+ p->nVars = nVars;
+ p->nClauses = 0;
+
+ nClauses = Msat_ClauseVecReadSize( p->vClauses );
+ pClauses = Msat_ClauseVecReadArray( p->vClauses );
+ for ( i = 0; i < nClauses; i++ )
+ Msat_ClauseFree( p, pClauses[i], 0 );
+// Msat_ClauseVecFree( p->vClauses );
+ Msat_ClauseVecClear( p->vClauses );
+
+ nClauses = Msat_ClauseVecReadSize( p->vLearned );
+ pClauses = Msat_ClauseVecReadArray( p->vLearned );
+ for ( i = 0; i < nClauses; i++ )
+ Msat_ClauseFree( p, pClauses[i], 0 );
+// Msat_ClauseVecFree( p->vLearned );
+ Msat_ClauseVecClear( p->vLearned );
+
+// FREE( p->pdActivity );
+ for ( i = 0; i < p->nVars; i++ )
+ p->pdActivity[i] = 0;
+
+// Msat_OrderFree( p->pOrder );
+// Msat_OrderClean( p->pOrder, p->nVars, NULL );
+ Msat_OrderSetBounds( p->pOrder, p->nVars );
+
+ for ( i = 0; i < 2 * p->nVars; i++ )
+// Msat_ClauseVecFree( p->pvWatched[i] );
+ Msat_ClauseVecClear( p->pvWatched[i] );
+// FREE( p->pvWatched );
+// Msat_QueueFree( p->pQueue );
+ Msat_QueueClear( p->pQueue );
+
+// FREE( p->pAssigns );
+ for ( i = 0; i < p->nVars; i++ )
+ p->pAssigns[i] = MSAT_VAR_UNASSIGNED;
+// Msat_IntVecFree( p->vTrail );
+ Msat_IntVecClear( p->vTrail );
+// Msat_IntVecFree( p->vTrailLim );
+ Msat_IntVecClear( p->vTrailLim );
+// FREE( p->pReasons );
+ memset( p->pReasons, 0, sizeof(Msat_Clause_t *) * p->nVars );
+// FREE( p->pLevel );
+ for ( i = 0; i < p->nVars; i++ )
+ p->pLevel[i] = -1;
+// Msat_IntVecFree( p->pModel );
+// Msat_MmStepStop( p->pMem, 0 );
+ p->dRandSeed = 91648253;
+ p->dProgress = 0.0;
+
+// FREE( p->pSeen );
+ memset( p->pSeen, 0, sizeof(int) * p->nVars );
+ p->nSeenId = 1;
+// Msat_IntVecFree( p->vReason );
+ Msat_IntVecClear( p->vReason );
+// Msat_IntVecFree( p->vTemp );
+ Msat_IntVecClear( p->vTemp );
+// printf(" The number of clauses remaining = %d (%d).\n", p->nClausesAlloc, p->nClausesAllocL );
+// FREE( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Frees the solver.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverFree( Msat_Solver_t * p )
+{
+ int i;
+
+ // free the clauses
+ int nClauses;
+ Msat_Clause_t ** pClauses;
+//printf( "clauses = %d. learned = %d.\n", Msat_ClauseVecReadSize( p->vClauses ),
+// Msat_ClauseVecReadSize( p->vLearned ) );
+
+ nClauses = Msat_ClauseVecReadSize( p->vClauses );
+ pClauses = Msat_ClauseVecReadArray( p->vClauses );
+ for ( i = 0; i < nClauses; i++ )
+ Msat_ClauseFree( p, pClauses[i], 0 );
+ Msat_ClauseVecFree( p->vClauses );
+
+ nClauses = Msat_ClauseVecReadSize( p->vLearned );
+ pClauses = Msat_ClauseVecReadArray( p->vLearned );
+ for ( i = 0; i < nClauses; i++ )
+ Msat_ClauseFree( p, pClauses[i], 0 );
+ Msat_ClauseVecFree( p->vLearned );
+
+ FREE( p->pdActivity );
+ FREE( p->pFactors );
+ Msat_OrderFree( p->pOrder );
+
+ for ( i = 0; i < 2 * p->nVarsAlloc; i++ )
+ Msat_ClauseVecFree( p->pvWatched[i] );
+ FREE( p->pvWatched );
+ Msat_QueueFree( p->pQueue );
+
+ FREE( p->pAssigns );
+ FREE( p->pModel );
+ Msat_IntVecFree( p->vTrail );
+ Msat_IntVecFree( p->vTrailLim );
+ FREE( p->pReasons );
+ FREE( p->pLevel );
+
+ Msat_MmStepStop( p->pMem, 0 );
+
+ nClauses = Msat_ClauseVecReadSize( p->vAdjacents );
+ pClauses = Msat_ClauseVecReadArray( p->vAdjacents );
+ for ( i = 0; i < nClauses; i++ )
+ Msat_IntVecFree( (Msat_IntVec_t *)pClauses[i] );
+ Msat_ClauseVecFree( p->vAdjacents );
+ Msat_IntVecFree( p->vConeVars );
+ Msat_IntVecFree( p->vVarsUsed );
+
+ FREE( p->pSeen );
+ Msat_IntVecFree( p->vReason );
+ Msat_IntVecFree( p->vTemp );
+ FREE( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prepares the solver to run on a subset of variables.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverPrepare( Msat_Solver_t * p, Msat_IntVec_t * vVars )
+{
+
+ int i;
+ // undo the previous data
+ for ( i = 0; i < p->nVarsAlloc; i++ )
+ {
+ p->pAssigns[i] = MSAT_VAR_UNASSIGNED;
+ p->pReasons[i] = NULL;
+ p->pLevel[i] = -1;
+ p->pdActivity[i] = 0.0;
+ }
+
+ // set the new variable order
+ Msat_OrderClean( p->pOrder, vVars );
+
+ Msat_QueueClear( p->pQueue );
+ Msat_IntVecClear( p->vTrail );
+ Msat_IntVecClear( p->vTrailLim );
+ p->dProgress = 0.0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sets up the truth tables.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverSetupTruthTables( unsigned uTruths[][2] )
+{
+ int m, v;
+ // set up the truth tables
+ for ( m = 0; m < 32; m++ )
+ for ( v = 0; v < 5; v++ )
+ if ( m & (1 << v) )
+ uTruths[v][0] |= (1 << m);
+ // make adjustments for the case of 6 variables
+ for ( v = 0; v < 5; v++ )
+ uTruths[v][1] = uTruths[v][0];
+ uTruths[5][0] = 0;
+ uTruths[5][1] = ~((unsigned)0);
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/msat/msatSolverCore.c b/src/sat/msat/msatSolverCore.c
new file mode 100644
index 00000000..f9fee73c
--- /dev/null
+++ b/src/sat/msat/msatSolverCore.c
@@ -0,0 +1,212 @@
+/**CFile****************************************************************
+
+ FileName [msatSolverCore.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [The SAT solver core procedures.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatSolverCore.c,v 1.2 2004/05/12 03:37:40 satrajit Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Adds one variable to the solver.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_SolverAddVar( Msat_Solver_t * p, int Level )
+{
+ if ( p->nVars == p->nVarsAlloc )
+ Msat_SolverResize( p, 2 * p->nVarsAlloc );
+ p->pLevel[p->nVars] = Level;
+ p->nVars++;
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Adds one clause to the solver's clause database.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_SolverAddClause( Msat_Solver_t * p, Msat_IntVec_t * vLits )
+{
+ Msat_Clause_t * pC;
+ bool Value;
+ Value = Msat_ClauseCreate( p, vLits, 0, &pC );
+ if ( pC != NULL )
+ Msat_ClauseVecPush( p->vClauses, pC );
+// else if ( p->fProof )
+// Msat_ClauseCreateFake( p, vLits );
+ return Value;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Returns search-space coverage. Not extremely reliable.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+double Msat_SolverProgressEstimate( Msat_Solver_t * p )
+{
+ double dProgress = 0.0;
+ double dF = 1.0 / p->nVars;
+ int i;
+ for ( i = 0; i < p->nVars; i++ )
+ if ( p->pAssigns[i] != MSAT_VAR_UNASSIGNED )
+ dProgress += pow( dF, p->pLevel[i] );
+ return dProgress / p->nVars;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prints statistics about the solver.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverPrintStats( Msat_Solver_t * p )
+{
+ printf("C solver (%d vars; %d clauses; %d learned):\n",
+ p->nVars, Msat_ClauseVecReadSize(p->vClauses), Msat_ClauseVecReadSize(p->vLearned) );
+ printf("starts : %lld\n", p->Stats.nStarts);
+ printf("conflicts : %lld\n", p->Stats.nConflicts);
+ printf("decisions : %lld\n", p->Stats.nDecisions);
+ printf("propagations : %lld\n", p->Stats.nPropagations);
+ printf("inspects : %lld\n", p->Stats.nInspects);
+}
+
+/**Function*************************************************************
+
+ Synopsis [Top-level solve.]
+
+ Description [If using assumptions (non-empty 'assumps' vector), you must
+ call 'simplifyDB()' first to see that no top-level conflict is present
+ (which would put the solver in an undefined state. If the last argument
+ is given (vProj), the solver enumerates through the satisfying solutions,
+ which are projected on the variables listed in this array. Note that the
+ variables in the array may be complemented, in which case the derived
+ assignment for the variable is complemented.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_SolverSolve( Msat_Solver_t * p, Msat_IntVec_t * vAssumps, int nBackTrackLimit, int nTimeLimit )
+{
+ Msat_SearchParams_t Params = { 0.95, 0.999 };
+ double nConflictsLimit, nLearnedLimit;
+ Msat_Type_t Status;
+ int timeStart = clock();
+ int64 nConflictsOld = p->Stats.nConflicts;
+ int64 nDecisionsOld = p->Stats.nDecisions;
+
+// p->pFreq = ALLOC( int, p->nVarsAlloc );
+// memset( p->pFreq, 0, sizeof(int) * p->nVarsAlloc );
+
+ if ( vAssumps )
+ {
+ int * pAssumps, nAssumps, i;
+
+ assert( Msat_IntVecReadSize(p->vTrailLim) == 0 );
+
+ nAssumps = Msat_IntVecReadSize( vAssumps );
+ pAssumps = Msat_IntVecReadArray( vAssumps );
+ for ( i = 0; i < nAssumps; i++ )
+ {
+ if ( !Msat_SolverAssume(p, pAssumps[i]) || Msat_SolverPropagate(p) )
+ {
+ Msat_QueueClear( p->pQueue );
+ Msat_SolverCancelUntil( p, 0 );
+ return MSAT_FALSE;
+ }
+ }
+ }
+ p->nLevelRoot = Msat_SolverReadDecisionLevel(p);
+ p->nClausesInit = Msat_ClauseVecReadSize( p->vClauses );
+ nConflictsLimit = 100;
+ nLearnedLimit = Msat_ClauseVecReadSize(p->vClauses) / 3;
+ Status = MSAT_UNKNOWN;
+ p->nBackTracks = (int)p->Stats.nConflicts;
+ while ( Status == MSAT_UNKNOWN )
+ {
+ if ( p->fVerbose )
+ printf("Solving -- conflicts=%d learnts=%d progress=%.4f %%\n",
+ (int)nConflictsLimit, (int)nLearnedLimit, p->dProgress*100);
+ Status = Msat_SolverSearch( p, (int)nConflictsLimit, (int)nLearnedLimit, nBackTrackLimit, &Params );
+ nConflictsLimit *= 1.5;
+ nLearnedLimit *= 1.1;
+ // if the limit on the number of backtracks is given, quit the restart loop
+ if ( nBackTrackLimit > 0 && (int)p->Stats.nConflicts - p->nBackTracks > nBackTrackLimit )
+ break;
+ // if the runtime limit is exceeded, quit the restart loop
+ if ( nTimeLimit > 0 && clock() - timeStart >= nTimeLimit * CLOCKS_PER_SEC )
+ break;
+ }
+ Msat_SolverCancelUntil( p, 0 );
+ p->nBackTracks = (int)p->Stats.nConflicts - p->nBackTracks;
+/*
+ PRT( "True solver runtime", clock() - timeStart );
+ // print the statistics
+ {
+ int i, Counter = 0;
+ for ( i = 0; i < p->nVars; i++ )
+ if ( p->pFreq[i] > 0 )
+ {
+ printf( "%d ", p->pFreq[i] );
+ Counter++;
+ }
+ if ( Counter )
+ printf( "\n" );
+ printf( "Total = %d. Used = %d. Decisions = %d. Imps = %d. Conflicts = %d. ", p->nVars, Counter, (int)p->Stats.nDecisions, (int)p->Stats.nPropagations, (int)p->Stats.nConflicts );
+ PRT( "Time", clock() - timeStart );
+ }
+*/
+ return Status;
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/msat/msatSolverIo.c b/src/sat/msat/msatSolverIo.c
new file mode 100644
index 00000000..05b7f6a9
--- /dev/null
+++ b/src/sat/msat/msatSolverIo.c
@@ -0,0 +1,177 @@
+/**CFile****************************************************************
+
+ FileName [msatSolverIo.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [Input/output of CNFs.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatSolverIo.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+static char * Msat_TimeStamp();
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverPrintAssignment( Msat_Solver_t * p )
+{
+ int i;
+ printf( "Current assignments are: \n" );
+ for ( i = 0; i < p->nVars; i++ )
+ printf( "%d", i % 10 );
+ printf( "\n" );
+ for ( i = 0; i < p->nVars; i++ )
+ if ( p->pAssigns[i] == MSAT_VAR_UNASSIGNED )
+ printf( "." );
+ else
+ {
+ assert( i == MSAT_LIT2VAR(p->pAssigns[i]) );
+ if ( MSAT_LITSIGN(p->pAssigns[i]) )
+ printf( "0" );
+ else
+ printf( "1" );
+ }
+ printf( "\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverPrintClauses( Msat_Solver_t * p )
+{
+ Msat_Clause_t ** pClauses;
+ int nClauses, i;
+
+ printf( "Original clauses: \n" );
+ nClauses = Msat_ClauseVecReadSize( p->vClauses );
+ pClauses = Msat_ClauseVecReadArray( p->vClauses );
+ for ( i = 0; i < nClauses; i++ )
+ {
+ printf( "%3d: ", i );
+ Msat_ClausePrint( pClauses[i] );
+ }
+
+ printf( "Learned clauses: \n" );
+ nClauses = Msat_ClauseVecReadSize( p->vLearned );
+ pClauses = Msat_ClauseVecReadArray( p->vLearned );
+ for ( i = 0; i < nClauses; i++ )
+ {
+ printf( "%3d: ", i );
+ Msat_ClausePrint( pClauses[i] );
+ }
+
+ printf( "Variable activity: \n" );
+ for ( i = 0; i < p->nVars; i++ )
+ printf( "%3d : %.4f\n", i, p->pdActivity[i] );
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverWriteDimacs( Msat_Solver_t * p, char * pFileName )
+{
+ FILE * pFile;
+ Msat_Clause_t ** pClauses;
+ int nClauses, i;
+
+ nClauses = Msat_ClauseVecReadSize(p->vClauses) + Msat_ClauseVecReadSize(p->vLearned);
+ for ( i = 0; i < p->nVars; i++ )
+ nClauses += ( p->pLevel[i] == 0 );
+
+ pFile = fopen( pFileName, "wb" );
+ fprintf( pFile, "c Produced by Msat_SolverWriteDimacs() on %s\n", Msat_TimeStamp() );
+ fprintf( pFile, "p cnf %d %d\n", p->nVars, nClauses );
+
+ nClauses = Msat_ClauseVecReadSize( p->vClauses );
+ pClauses = Msat_ClauseVecReadArray( p->vClauses );
+ for ( i = 0; i < nClauses; i++ )
+ Msat_ClauseWriteDimacs( pFile, pClauses[i], 1 );
+
+ nClauses = Msat_ClauseVecReadSize( p->vLearned );
+ pClauses = Msat_ClauseVecReadArray( p->vLearned );
+ for ( i = 0; i < nClauses; i++ )
+ Msat_ClauseWriteDimacs( pFile, pClauses[i], 1 );
+
+ // write zero-level assertions
+ for ( i = 0; i < p->nVars; i++ )
+ if ( p->pLevel[i] == 0 )
+ fprintf( pFile, "%s%d 0\n", ((p->pAssigns[i]&1)? "-": ""), i + 1 );
+
+ fprintf( pFile, "\n" );
+ fclose( pFile );
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Returns the time stamp.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+char * Msat_TimeStamp()
+{
+ static char Buffer[100];
+ time_t ltime;
+ char * TimeStamp;
+ // get the current time
+ time( &ltime );
+ TimeStamp = asctime( localtime( &ltime ) );
+ TimeStamp[ strlen(TimeStamp) - 1 ] = 0;
+ strcpy( Buffer, TimeStamp );
+ return Buffer;
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/msat/msatSolverSearch.c b/src/sat/msat/msatSolverSearch.c
new file mode 100644
index 00000000..11a6540c
--- /dev/null
+++ b/src/sat/msat/msatSolverSearch.c
@@ -0,0 +1,629 @@
+/**CFile****************************************************************
+
+ FileName [msatSolverSearch.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [The search part of the solver.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatSolverSearch.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+static void Msat_SolverUndoOne( Msat_Solver_t * p );
+static void Msat_SolverCancel( Msat_Solver_t * p );
+static Msat_Clause_t * Msat_SolverRecord( Msat_Solver_t * p, Msat_IntVec_t * vLits );
+static void Msat_SolverAnalyze( Msat_Solver_t * p, Msat_Clause_t * pC, Msat_IntVec_t * vLits_out, int * pLevel_out );
+static void Msat_SolverReduceDB( Msat_Solver_t * p );
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Makes the next assumption (Lit).]
+
+ Description [Returns FALSE if immediate conflict.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_SolverAssume( Msat_Solver_t * p, Msat_Lit_t Lit )
+{
+ assert( Msat_QueueReadSize(p->pQueue) == 0 );
+ if ( p->fVerbose )
+ printf(L_IND"assume("L_LIT")\n", L_ind, L_lit(Lit));
+ Msat_IntVecPush( p->vTrailLim, Msat_IntVecReadSize(p->vTrail) );
+// assert( Msat_IntVecReadSize(p->vTrailLim) <= Msat_IntVecReadSize(p->vTrail) + 1 );
+// assert( Msat_IntVecReadSize( p->vTrailLim ) < p->nVars );
+ return Msat_SolverEnqueue( p, Lit, NULL );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reverts one variable binding on the trail.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverUndoOne( Msat_Solver_t * p )
+{
+ Msat_Lit_t Lit;
+ Msat_Var_t Var;
+ Lit = Msat_IntVecPop( p->vTrail );
+ Var = MSAT_LIT2VAR(Lit);
+ p->pAssigns[Var] = MSAT_VAR_UNASSIGNED;
+ p->pReasons[Var] = NULL;
+ p->pLevel[Var] = -1;
+// Msat_OrderUndo( p->pOrder, Var );
+ Msat_OrderVarUnassigned( p->pOrder, Var );
+
+ if ( p->fVerbose )
+ printf(L_IND"unbind("L_LIT")\n", L_ind, L_lit(Lit));
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reverts to the state before last Msat_SolverAssume().]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverCancel( Msat_Solver_t * p )
+{
+ int c;
+ assert( Msat_QueueReadSize(p->pQueue) == 0 );
+ if ( p->fVerbose )
+ {
+ if ( Msat_IntVecReadSize(p->vTrail) != Msat_IntVecReadEntryLast(p->vTrailLim) )
+ {
+ Msat_Lit_t Lit;
+ Lit = Msat_IntVecReadEntry( p->vTrail, Msat_IntVecReadEntryLast(p->vTrailLim) );
+ printf(L_IND"cancel("L_LIT")\n", L_ind, L_lit(Lit));
+ }
+ }
+ for ( c = Msat_IntVecReadSize(p->vTrail) - Msat_IntVecPop( p->vTrailLim ); c != 0; c-- )
+ Msat_SolverUndoOne( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reverts to the state at given level.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverCancelUntil( Msat_Solver_t * p, int Level )
+{
+ while ( Msat_IntVecReadSize(p->vTrailLim) > Level )
+ Msat_SolverCancel(p);
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Record a clause and drive backtracking.]
+
+ Description [vLits[0] must contain the asserting literal.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_Clause_t * Msat_SolverRecord( Msat_Solver_t * p, Msat_IntVec_t * vLits )
+{
+ Msat_Clause_t * pC;
+ int Value;
+ assert( Msat_IntVecReadSize(vLits) != 0 );
+ Value = Msat_ClauseCreate( p, vLits, 1, &pC );
+ assert( Value );
+ Value = Msat_SolverEnqueue( p, Msat_IntVecReadEntry(vLits,0), pC );
+ assert( Value );
+ if ( pC )
+ Msat_ClauseVecPush( p->vLearned, pC );
+ return pC;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Enqueues one variable assignment.]
+
+ Description [Puts a new fact on the propagation queue and immediately
+ updates the variable value. Should a conflict arise, FALSE is returned.
+ Otherwise returns TRUE.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_SolverEnqueue( Msat_Solver_t * p, Msat_Lit_t Lit, Msat_Clause_t * pC )
+{
+ Msat_Var_t Var = MSAT_LIT2VAR(Lit);
+
+ // skip literals that are not in the current cone
+ if ( !Msat_IntVecReadEntry( p->vVarsUsed, Var ) )
+ return 1;
+
+// assert( Msat_QueueReadSize(p->pQueue) == Msat_IntVecReadSize(p->vTrail) );
+ // if the literal is assigned
+ // return 1 if the assignment is consistent
+ // return 0 if the assignment is inconsistent (conflict)
+ if ( p->pAssigns[Var] != MSAT_VAR_UNASSIGNED )
+ return p->pAssigns[Var] == Lit;
+ // new fact - store it
+ if ( p->fVerbose )
+ {
+// printf(L_IND"bind("L_LIT")\n", L_ind, L_lit(Lit));
+ printf(L_IND"bind("L_LIT") ", L_ind, L_lit(Lit));
+ Msat_ClausePrintSymbols( pC );
+ }
+ p->pAssigns[Var] = Lit;
+ p->pLevel[Var] = Msat_IntVecReadSize(p->vTrailLim);
+// p->pReasons[Var] = p->pLevel[Var]? pC: NULL;
+ p->pReasons[Var] = pC;
+ Msat_IntVecPush( p->vTrail, Lit );
+ Msat_QueueInsert( p->pQueue, Lit );
+
+ Msat_OrderVarAssigned( p->pOrder, Var );
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Propagates the assignments in the queue.]
+
+ Description [Propagates all enqueued facts. If a conflict arises,
+ the conflicting clause is returned, otherwise NULL.]
+
+ SideEffects [The propagation queue is empty, even if there was a conflict.]
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_Clause_t * Msat_SolverPropagate( Msat_Solver_t * p )
+{
+ Msat_ClauseVec_t ** pvWatched = p->pvWatched;
+ Msat_Clause_t ** pClauses;
+ Msat_Clause_t * pConflict;
+ Msat_Lit_t Lit, Lit_out;
+ int i, j, nClauses;
+
+ // propagate all the literals in the queue
+ while ( (Lit = Msat_QueueExtract( p->pQueue )) >= 0 )
+ {
+ p->Stats.nPropagations++;
+ // get the clauses watched by this literal
+ nClauses = Msat_ClauseVecReadSize( pvWatched[Lit] );
+ pClauses = Msat_ClauseVecReadArray( pvWatched[Lit] );
+ // go through the watched clauses and decide what to do with them
+ for ( i = j = 0; i < nClauses; i++ )
+ {
+ p->Stats.nInspects++;
+ // clear the returned literal
+ Lit_out = -1;
+ // propagate the clause
+ if ( !Msat_ClausePropagate( pClauses[i], Lit, p->pAssigns, &Lit_out ) )
+ { // the clause is unit
+ // "Lit_out" contains the new assignment to be enqueued
+ if ( Msat_SolverEnqueue( p, Lit_out, pClauses[i] ) )
+ { // consistent assignment
+ // no changes to the implication queue; the watch is the same too
+ pClauses[j++] = pClauses[i];
+ continue;
+ }
+ // remember the reason of conflict (will be returned)
+ pConflict = pClauses[i];
+ // leave the remaning clauses in the same watched list
+ for ( ; i < nClauses; i++ )
+ pClauses[j++] = pClauses[i];
+ Msat_ClauseVecShrink( pvWatched[Lit], j );
+ // clear the propagation queue
+ Msat_QueueClear( p->pQueue );
+ return pConflict;
+ }
+ // the clause is not unit
+ // in this case "Lit_out" contains the new watch if it has changed
+ if ( Lit_out >= 0 )
+ Msat_ClauseVecPush( pvWatched[Lit_out], pClauses[i] );
+ else // the watch did not change
+ pClauses[j++] = pClauses[i];
+ }
+ Msat_ClauseVecShrink( pvWatched[Lit], j );
+ }
+ return NULL;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Simplifies the data base.]
+
+ Description [Simplify all constraints according to the current top-level
+ assigment (redundant constraints may be removed altogether).]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_SolverSimplifyDB( Msat_Solver_t * p )
+{
+ Msat_ClauseVec_t * vClauses;
+ Msat_Clause_t ** pClauses;
+ int nClauses, Type, i, j;
+ int * pAssigns;
+ int Counter;
+
+ assert( Msat_SolverReadDecisionLevel(p) == 0 );
+ if ( Msat_SolverPropagate(p) != NULL )
+ return 0;
+//Msat_SolverPrintClauses( p );
+//Msat_SolverPrintAssignment( p );
+//printf( "Simplification\n" );
+
+ // simplify and reassign clause numbers
+ Counter = 0;
+ pAssigns = Msat_SolverReadAssignsArray( p );
+ for ( Type = 0; Type < 2; Type++ )
+ {
+ vClauses = Type? p->vLearned : p->vClauses;
+ nClauses = Msat_ClauseVecReadSize( vClauses );
+ pClauses = Msat_ClauseVecReadArray( vClauses );
+ for ( i = j = 0; i < nClauses; i++ )
+ if ( Msat_ClauseSimplify( pClauses[i], pAssigns ) )
+ Msat_ClauseFree( p, pClauses[i], 1 );
+ else
+ {
+ pClauses[j++] = pClauses[i];
+ Msat_ClauseSetNum( pClauses[i], Counter++ );
+ }
+ Msat_ClauseVecShrink( vClauses, j );
+ }
+ p->nClauses = Counter;
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Cleans the clause databased from the useless learnt clauses.]
+
+ Description [Removes half of the learnt clauses, minus the clauses locked
+ by the current assignment. Locked clauses are clauses that are reason
+ to a some assignment.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverReduceDB( Msat_Solver_t * p )
+{
+ Msat_Clause_t ** pLearned;
+ int nLearned, i, j;
+ double dExtraLim = p->dClaInc / Msat_ClauseVecReadSize(p->vLearned);
+ // Remove any clause below this activity
+
+ // sort the learned clauses in the increasing order of activity
+ Msat_SolverSortDB( p );
+
+ // discard the first half the clauses (the less active ones)
+ nLearned = Msat_ClauseVecReadSize( p->vLearned );
+ pLearned = Msat_ClauseVecReadArray( p->vLearned );
+ for ( i = j = 0; i < nLearned / 2; i++ )
+ if ( !Msat_ClauseIsLocked( p, pLearned[i]) )
+ Msat_ClauseFree( p, pLearned[i], 1 );
+ else
+ pLearned[j++] = pLearned[i];
+ // filter the more active clauses and leave those above the limit
+ for ( ; i < nLearned; i++ )
+ if ( !Msat_ClauseIsLocked( p, pLearned[i] ) &&
+ Msat_ClauseReadActivity(pLearned[i]) < dExtraLim )
+ Msat_ClauseFree( p, pLearned[i], 1 );
+ else
+ pLearned[j++] = pLearned[i];
+ Msat_ClauseVecShrink( p->vLearned, j );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Removes the learned clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverRemoveLearned( Msat_Solver_t * p )
+{
+ Msat_Clause_t ** pLearned;
+ int nLearned, i;
+
+ // discard the learned clauses
+ nLearned = Msat_ClauseVecReadSize( p->vLearned );
+ pLearned = Msat_ClauseVecReadArray( p->vLearned );
+ for ( i = 0; i < nLearned; i++ )
+ {
+ assert( !Msat_ClauseIsLocked( p, pLearned[i]) );
+
+ Msat_ClauseFree( p, pLearned[i], 1 );
+ }
+ Msat_ClauseVecShrink( p->vLearned, 0 );
+ p->nClauses = Msat_ClauseVecReadSize(p->vClauses);
+
+ for ( i = 0; i < p->nVarsAlloc; i++ )
+ p->pReasons[i] = NULL;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Removes the recently added clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverRemoveMarked( Msat_Solver_t * p )
+{
+ Msat_Clause_t ** pLearned, ** pClauses;
+ int nLearned, nClauses, i;
+
+ // discard the learned clauses
+ nClauses = Msat_ClauseVecReadSize( p->vClauses );
+ pClauses = Msat_ClauseVecReadArray( p->vClauses );
+ for ( i = p->nClausesStart; i < nClauses; i++ )
+ {
+// assert( !Msat_ClauseIsLocked( p, pClauses[i]) );
+ Msat_ClauseFree( p, pClauses[i], 1 );
+ }
+ Msat_ClauseVecShrink( p->vClauses, p->nClausesStart );
+
+ // discard the learned clauses
+ nLearned = Msat_ClauseVecReadSize( p->vLearned );
+ pLearned = Msat_ClauseVecReadArray( p->vLearned );
+ for ( i = 0; i < nLearned; i++ )
+ {
+// assert( !Msat_ClauseIsLocked( p, pLearned[i]) );
+ Msat_ClauseFree( p, pLearned[i], 1 );
+ }
+ Msat_ClauseVecShrink( p->vLearned, 0 );
+ p->nClauses = Msat_ClauseVecReadSize(p->vClauses);
+/*
+ // undo the previous data
+ for ( i = 0; i < p->nVarsAlloc; i++ )
+ {
+ p->pAssigns[i] = MSAT_VAR_UNASSIGNED;
+ p->pReasons[i] = NULL;
+ p->pLevel[i] = -1;
+ p->pdActivity[i] = 0.0;
+ }
+ Msat_OrderClean( p->pOrder, p->nVars, NULL );
+ Msat_QueueClear( p->pQueue );
+*/
+}
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Analyze conflict and produce a reason clause.]
+
+ Description [Current decision level must be greater than root level.]
+
+ SideEffects [vLits_out[0] is the asserting literal at level pLevel_out.]
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverAnalyze( Msat_Solver_t * p, Msat_Clause_t * pC, Msat_IntVec_t * vLits_out, int * pLevel_out )
+{
+ Msat_Lit_t LitQ, Lit = MSAT_LIT_UNASSIGNED;
+ Msat_Var_t VarQ, Var;
+ int * pReasonArray, nReasonSize;
+ int j, pathC = 0, nLevelCur = Msat_IntVecReadSize(p->vTrailLim);
+ int iStep = Msat_IntVecReadSize(p->vTrail) - 1;
+
+ // increment the seen counter
+ p->nSeenId++;
+ // empty the vector array
+ Msat_IntVecClear( vLits_out );
+ Msat_IntVecPush( vLits_out, -1 ); // (leave room for the asserting literal)
+ *pLevel_out = 0;
+ do {
+ assert( pC != NULL ); // (otherwise should be UIP)
+ // get the reason of conflict
+ Msat_ClauseCalcReason( p, pC, Lit, p->vReason );
+ nReasonSize = Msat_IntVecReadSize( p->vReason );
+ pReasonArray = Msat_IntVecReadArray( p->vReason );
+ for ( j = 0; j < nReasonSize; j++ ) {
+ LitQ = pReasonArray[j];
+ VarQ = MSAT_LIT2VAR(LitQ);
+ if ( p->pSeen[VarQ] != p->nSeenId ) {
+ p->pSeen[VarQ] = p->nSeenId;
+
+ // added to better fine-tune the search
+ Msat_SolverVarBumpActivity( p, LitQ );
+
+ // skip all the literals on this decision level
+ if ( p->pLevel[VarQ] == nLevelCur )
+ pathC++;
+ else if ( p->pLevel[VarQ] > 0 ) {
+ // add the literals on other decision levels but
+ // exclude variables from decision level 0
+ Msat_IntVecPush( vLits_out, MSAT_LITNOT(LitQ) );
+ if ( *pLevel_out < p->pLevel[VarQ] )
+ *pLevel_out = p->pLevel[VarQ];
+ }
+ }
+ }
+ // Select next clause to look at:
+ do {
+// Lit = Msat_IntVecReadEntryLast(p->vTrail);
+ Lit = Msat_IntVecReadEntry( p->vTrail, iStep-- );
+ Var = MSAT_LIT2VAR(Lit);
+ pC = p->pReasons[Var];
+// Msat_SolverUndoOne( p );
+ } while ( p->pSeen[Var] != p->nSeenId );
+ pathC--;
+ } while ( pathC > 0 );
+ // we do not unbind the variables above
+ // this will be done after conflict analysis
+
+ Msat_IntVecWriteEntry( vLits_out, 0, MSAT_LITNOT(Lit) );
+ if ( p->fVerbose )
+ {
+ printf( L_IND"Learnt {", L_ind );
+ nReasonSize = Msat_IntVecReadSize( vLits_out );
+ pReasonArray = Msat_IntVecReadArray( vLits_out );
+ for ( j = 0; j < nReasonSize; j++ )
+ printf(" "L_LIT, L_lit(pReasonArray[j]));
+ printf(" } at level %d\n", *pLevel_out);
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [The search procedure called between the restarts.]
+
+ Description [Search for a satisfying solution as long as the number of
+ conflicts does not exceed the limit (nConfLimit) while keeping the number
+ of learnt clauses below the provided limit (nLearnedLimit). NOTE! Use
+ negative value for nConfLimit or nLearnedLimit to indicate infinity.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_Type_t Msat_SolverSearch( Msat_Solver_t * p, int nConfLimit, int nLearnedLimit, int nBackTrackLimit, Msat_SearchParams_t * pPars )
+{
+ Msat_Clause_t * pConf;
+ Msat_Var_t Var;
+ int nLevelBack, nConfs, nAssigns, Value;
+ int i;
+
+ assert( Msat_SolverReadDecisionLevel(p) == p->nLevelRoot );
+ p->Stats.nStarts++;
+ p->dVarDecay = 1 / pPars->dVarDecay;
+ p->dClaDecay = 1 / pPars->dClaDecay;
+
+ // reset the activities
+ for ( i = 0; i < p->nVars; i++ )
+ p->pdActivity[i] = (double)p->pFactors[i];
+// p->pdActivity[i] = 0.0;
+
+ nConfs = 0;
+ while ( 1 )
+ {
+ pConf = Msat_SolverPropagate( p );
+ if ( pConf != NULL ){
+ // CONFLICT
+ if ( p->fVerbose )
+ {
+// printf(L_IND"**CONFLICT**\n", L_ind);
+ printf(L_IND"**CONFLICT** ", L_ind);
+ Msat_ClausePrintSymbols( pConf );
+ }
+ // count conflicts
+ p->Stats.nConflicts++;
+ nConfs++;
+
+ // if top level, return UNSAT
+ if ( Msat_SolverReadDecisionLevel(p) == p->nLevelRoot )
+ return MSAT_FALSE;
+
+ // perform conflict analysis
+ Msat_SolverAnalyze( p, pConf, p->vTemp, &nLevelBack );
+ Msat_SolverCancelUntil( p, (p->nLevelRoot > nLevelBack)? p->nLevelRoot : nLevelBack );
+ Msat_SolverRecord( p, p->vTemp );
+
+ // it is important that recording is done after cancelling
+ // because canceling cleans the queue while recording adds to it
+ Msat_SolverVarDecayActivity( p );
+ Msat_SolverClaDecayActivity( p );
+
+ }
+ else{
+ // NO CONFLICT
+ if ( Msat_IntVecReadSize(p->vTrailLim) == 0 ) {
+ // Simplify the set of problem clauses:
+// Value = Msat_SolverSimplifyDB(p);
+// assert( Value );
+ }
+ nAssigns = Msat_IntVecReadSize( p->vTrail );
+ if ( nLearnedLimit >= 0 && Msat_ClauseVecReadSize(p->vLearned) >= nLearnedLimit + nAssigns ) {
+ // Reduce the set of learnt clauses:
+ Msat_SolverReduceDB(p);
+ }
+
+ Var = Msat_OrderVarSelect( p->pOrder );
+ if ( Var == MSAT_ORDER_UNKNOWN ) {
+ // Model found and stored in p->pAssigns
+ memcpy( p->pModel, p->pAssigns, sizeof(int) * p->nVars );
+ Msat_QueueClear( p->pQueue );
+ Msat_SolverCancelUntil( p, p->nLevelRoot );
+ return MSAT_TRUE;
+ }
+ if ( nConfLimit > 0 && nConfs > nConfLimit ) {
+ // Reached bound on number of conflicts:
+ p->dProgress = Msat_SolverProgressEstimate( p );
+ Msat_QueueClear( p->pQueue );
+ Msat_SolverCancelUntil( p, p->nLevelRoot );
+ return MSAT_UNKNOWN;
+ }
+ else if ( nBackTrackLimit > 0 && (int)p->Stats.nConflicts - p->nBackTracks > nBackTrackLimit ) {
+ // Reached bound on number of conflicts:
+ Msat_QueueClear( p->pQueue );
+ Msat_SolverCancelUntil( p, p->nLevelRoot );
+ return MSAT_UNKNOWN;
+ }
+ else{
+ // New variable decision:
+ p->Stats.nDecisions++;
+ assert( Var != MSAT_ORDER_UNKNOWN && Var >= 0 && Var < p->nVars );
+ Value = Msat_SolverAssume(p, MSAT_VAR2LIT(Var,0) );
+ assert( Value );
+ }
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/msat/msatSort.c b/src/sat/msat/msatSort.c
new file mode 100644
index 00000000..3b89d102
--- /dev/null
+++ b/src/sat/msat/msatSort.c
@@ -0,0 +1,173 @@
+/**CFile****************************************************************
+
+ FileName [msatSort.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [Sorting clauses.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatSort.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+static int Msat_SolverSortCompare( Msat_Clause_t ** ppC1, Msat_Clause_t ** ppC2 );
+
+// Returns a random float 0 <= x < 1. Seed must never be 0.
+static double drand(double seed) {
+ int q;
+ seed *= 1389796;
+ q = (int)(seed / 2147483647);
+ seed -= (double)q * 2147483647;
+ return seed / 2147483647; }
+
+// Returns a random integer 0 <= x < size. Seed must never be 0.
+static int irand(double seed, int size) {
+ return (int)(drand(seed) * size); }
+
+static void Msat_SolverSort( Msat_Clause_t ** array, int size, double seed );
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Msat_SolverSort the learned clauses in the increasing order of activity.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverSortDB( Msat_Solver_t * p )
+{
+ Msat_ClauseVec_t * pVecClauses;
+ Msat_Clause_t ** pLearned;
+ int nLearned;
+ // read the parameters
+ pVecClauses = Msat_SolverReadLearned( p );
+ nLearned = Msat_ClauseVecReadSize( pVecClauses );
+ pLearned = Msat_ClauseVecReadArray( pVecClauses );
+ // Msat_SolverSort the array
+// qMsat_SolverSort( (void *)pLearned, nLearned, sizeof(Msat_Clause_t *),
+// (int (*)(const void *, const void *)) Msat_SolverSortCompare );
+// printf( "Msat_SolverSorting.\n" );
+ Msat_SolverSort( pLearned, nLearned, 91648253 );
+/*
+ if ( nLearned > 2 )
+ {
+ printf( "Clause 1: %0.20f\n", Msat_ClauseReadActivity(pLearned[0]) );
+ printf( "Clause 2: %0.20f\n", Msat_ClauseReadActivity(pLearned[1]) );
+ printf( "Clause 3: %0.20f\n", Msat_ClauseReadActivity(pLearned[2]) );
+ }
+*/
+}
+
+/**Function*************************************************************
+
+ Synopsis [Comparison procedure for two clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_SolverSortCompare( Msat_Clause_t ** ppC1, Msat_Clause_t ** ppC2 )
+{
+ float Value1 = Msat_ClauseReadActivity( *ppC1 );
+ float Value2 = Msat_ClauseReadActivity( *ppC2 );
+ if ( Value1 < Value2 )
+ return -1;
+ if ( Value1 > Value2 )
+ return 1;
+ return 0;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Selection sort for small array size.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverSortSelection( Msat_Clause_t ** array, int size )
+{
+ Msat_Clause_t * tmp;
+ int i, j, best_i;
+ for ( i = 0; i < size-1; i++ )
+ {
+ best_i = i;
+ for (j = i+1; j < size; j++)
+ {
+ if ( Msat_ClauseReadActivity(array[j]) < Msat_ClauseReadActivity(array[best_i]) )
+ best_i = j;
+ }
+ tmp = array[i]; array[i] = array[best_i]; array[best_i] = tmp;
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [The original MiniSat sorting procedure.]
+
+ Description [This procedure is used to preserve trace-equivalence
+ with the orignal C++ implemenation of the solver.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_SolverSort( Msat_Clause_t ** array, int size, double seed )
+{
+ if (size <= 15)
+ Msat_SolverSortSelection( array, size );
+ else
+ {
+ Msat_Clause_t * pivot = array[irand(seed, size)];
+ Msat_Clause_t * tmp;
+ int i = -1;
+ int j = size;
+
+ for(;;)
+ {
+ do i++; while( Msat_ClauseReadActivity(array[i]) < Msat_ClauseReadActivity(pivot) );
+ do j--; while( Msat_ClauseReadActivity(pivot) < Msat_ClauseReadActivity(array[j]) );
+
+ if ( i >= j ) break;
+
+ tmp = array[i]; array[i] = array[j]; array[j] = tmp;
+ }
+ Msat_SolverSort(array , i , seed);
+ Msat_SolverSort(&array[i], size-i, seed);
+ }
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/msat/msatVec.c b/src/sat/msat/msatVec.c
new file mode 100644
index 00000000..75f53047
--- /dev/null
+++ b/src/sat/msat/msatVec.c
@@ -0,0 +1,495 @@
+/**CFile****************************************************************
+
+ FileName [msatVec.c]
+
+ PackageName [A C version of SAT solver MINISAT, originally developed
+ in C++ by Niklas Een and Niklas Sorensson, Chalmers University of
+ Technology, Sweden: http://www.cs.chalmers.se/~een/Satzoo.]
+
+ Synopsis [Integer vector borrowed from Extra.]
+
+ Author [Alan Mishchenko <alanmi@eecs.berkeley.edu>]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - January 1, 2004.]
+
+ Revision [$Id: msatVec.c,v 1.0 2004/01/01 1:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include "msatInt.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+static int Msat_IntVecSortCompare1( int * pp1, int * pp2 );
+static int Msat_IntVecSortCompare2( int * pp1, int * pp2 );
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Allocates a vector with the given capacity.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_IntVec_t * Msat_IntVecAlloc( int nCap )
+{
+ Msat_IntVec_t * p;
+ p = ALLOC( Msat_IntVec_t, 1 );
+ if ( nCap > 0 && nCap < 16 )
+ nCap = 16;
+ p->nSize = 0;
+ p->nCap = nCap;
+ p->pArray = p->nCap? ALLOC( int, p->nCap ) : NULL;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Creates the vector from an integer array of the given size.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_IntVec_t * Msat_IntVecAllocArray( int * pArray, int nSize )
+{
+ Msat_IntVec_t * p;
+ p = ALLOC( Msat_IntVec_t, 1 );
+ p->nSize = nSize;
+ p->nCap = nSize;
+ p->pArray = pArray;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Creates the vector from an integer array of the given size.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_IntVec_t * Msat_IntVecAllocArrayCopy( int * pArray, int nSize )
+{
+ Msat_IntVec_t * p;
+ p = ALLOC( Msat_IntVec_t, 1 );
+ p->nSize = nSize;
+ p->nCap = nSize;
+ p->pArray = ALLOC( int, nSize );
+ memcpy( p->pArray, pArray, sizeof(int) * nSize );
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Duplicates the integer array.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_IntVec_t * Msat_IntVecDup( Msat_IntVec_t * pVec )
+{
+ Msat_IntVec_t * p;
+ p = ALLOC( Msat_IntVec_t, 1 );
+ p->nSize = pVec->nSize;
+ p->nCap = pVec->nCap;
+ p->pArray = p->nCap? ALLOC( int, p->nCap ) : NULL;
+ memcpy( p->pArray, pVec->pArray, sizeof(int) * pVec->nSize );
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Transfers the array into another vector.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Msat_IntVec_t * Msat_IntVecDupArray( Msat_IntVec_t * pVec )
+{
+ Msat_IntVec_t * p;
+ p = ALLOC( Msat_IntVec_t, 1 );
+ p->nSize = pVec->nSize;
+ p->nCap = pVec->nCap;
+ p->pArray = pVec->pArray;
+ pVec->nSize = 0;
+ pVec->nCap = 0;
+ pVec->pArray = NULL;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_IntVecFree( Msat_IntVec_t * p )
+{
+ FREE( p->pArray );
+ FREE( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Fills the vector with given number of entries.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_IntVecFill( Msat_IntVec_t * p, int nSize, int Entry )
+{
+ int i;
+ Msat_IntVecGrow( p, nSize );
+ p->nSize = nSize;
+ for ( i = 0; i < p->nSize; i++ )
+ p->pArray[i] = Entry;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int * Msat_IntVecReleaseArray( Msat_IntVec_t * p )
+{
+ int * pArray = p->pArray;
+ p->nCap = 0;
+ p->nSize = 0;
+ p->pArray = NULL;
+ return pArray;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int * Msat_IntVecReadArray( Msat_IntVec_t * p )
+{
+ return p->pArray;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_IntVecReadSize( Msat_IntVec_t * p )
+{
+ return p->nSize;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_IntVecReadEntry( Msat_IntVec_t * p, int i )
+{
+ assert( i >= 0 && i < p->nSize );
+ return p->pArray[i];
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_IntVecWriteEntry( Msat_IntVec_t * p, int i, int Entry )
+{
+ assert( i >= 0 && i < p->nSize );
+ p->pArray[i] = Entry;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_IntVecReadEntryLast( Msat_IntVec_t * p )
+{
+ return p->pArray[p->nSize-1];
+}
+
+/**Function*************************************************************
+
+ Synopsis [Resizes the vector to the given capacity.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_IntVecGrow( Msat_IntVec_t * p, int nCapMin )
+{
+ if ( p->nCap >= nCapMin )
+ return;
+ p->pArray = REALLOC( int, p->pArray, nCapMin );
+ p->nCap = nCapMin;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_IntVecShrink( Msat_IntVec_t * p, int nSizeNew )
+{
+ assert( p->nSize >= nSizeNew );
+ p->nSize = nSizeNew;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_IntVecClear( Msat_IntVec_t * p )
+{
+ p->nSize = 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis []
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_IntVecPush( Msat_IntVec_t * p, int Entry )
+{
+ if ( p->nSize == p->nCap )
+ {
+ if ( p->nCap < 16 )
+ Msat_IntVecGrow( p, 16 );
+ else
+ Msat_IntVecGrow( p, 2 * p->nCap );
+ }
+ p->pArray[p->nSize++] = Entry;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Add the element while ensuring uniqueness.]
+
+ Description [Returns 1 if the element was found, and 0 if it was new. ]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_IntVecPushUnique( Msat_IntVec_t * p, int Entry )
+{
+ int i;
+ for ( i = 0; i < p->nSize; i++ )
+ if ( p->pArray[i] == Entry )
+ return 1;
+ Msat_IntVecPush( p, Entry );
+ return 0;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Inserts the element while sorting in the increasing order.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_IntVecPushUniqueOrder( Msat_IntVec_t * p, int Entry, int fIncrease )
+{
+ int Entry1, Entry2;
+ int i;
+ Msat_IntVecPushUnique( p, Entry );
+ // find the p of the node
+ for ( i = p->nSize-1; i > 0; i-- )
+ {
+ Entry1 = p->pArray[i ];
+ Entry2 = p->pArray[i-1];
+ if ( fIncrease && Entry1 >= Entry2 ||
+ !fIncrease && Entry1 <= Entry2 )
+ break;
+ p->pArray[i ] = Entry2;
+ p->pArray[i-1] = Entry1;
+ }
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Returns the last entry and removes it from the list.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_IntVecPop( Msat_IntVec_t * p )
+{
+ assert( p->nSize > 0 );
+ return p->pArray[--p->nSize];
+}
+
+/**Function*************************************************************
+
+ Synopsis [Sorting the entries by their integer value.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Msat_IntVecSort( Msat_IntVec_t * p, int fReverse )
+{
+ if ( fReverse )
+ qsort( (void *)p->pArray, p->nSize, sizeof(int),
+ (int (*)(const void *, const void *)) Msat_IntVecSortCompare2 );
+ else
+ qsort( (void *)p->pArray, p->nSize, sizeof(int),
+ (int (*)(const void *, const void *)) Msat_IntVecSortCompare1 );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Comparison procedure for two clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_IntVecSortCompare1( int * pp1, int * pp2 )
+{
+ // for some reason commenting out lines (as shown) led to crashing of the release version
+ if ( *pp1 < *pp2 )
+ return -1;
+ if ( *pp1 > *pp2 ) //
+ return 1;
+ return 0; //
+}
+
+/**Function*************************************************************
+
+ Synopsis [Comparison procedure for two clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_IntVecSortCompare2( int * pp1, int * pp2 )
+{
+ // for some reason commenting out lines (as shown) led to crashing of the release version
+ if ( *pp1 > *pp2 )
+ return -1;
+ if ( *pp1 < *pp2 ) //
+ return 1;
+ return 0; //
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/proof/pr.c b/src/sat/proof/pr.c
new file mode 100644
index 00000000..2d1ab2d1
--- /dev/null
+++ b/src/sat/proof/pr.c
@@ -0,0 +1,1263 @@
+/**CFile****************************************************************
+
+ FileName [pr.c]
+
+ SystemName [ABC: Logic synthesis and verification system.]
+
+ PackageName [Proof recording.]
+
+ Synopsis [Core procedures of the package.]
+
+ Author [Alan Mishchenko]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - June 20, 2005.]
+
+ Revision [$Id: pr.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+//#include "vec.h"
+#include "pr.h"
+
+////////////////////////////////////////////////////////////////////////
+/// DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+typedef unsigned lit;
+
+typedef struct Pr_Cls_t_ Pr_Cls_t;
+struct Pr_Cls_t_
+{
+ unsigned uTruth; // interpolant
+ void * pProof; // the proof node
+// void * pAntis; // the anticedents
+ Pr_Cls_t * pNext; // the next clause
+ Pr_Cls_t * pNext0; // the next 0-watch
+ Pr_Cls_t * pNext1; // the next 0-watch
+ int Id; // the clause ID
+ unsigned fA : 1; // belongs to A
+ unsigned fRoot : 1; // original clause
+ unsigned fVisit : 1; // visited clause
+ unsigned nLits : 24; // the number of literals
+ lit pLits[0]; // literals of this clause
+};
+
+struct Pr_Man_t_
+{
+ // general data
+ int fProofWrite; // writes the proof file
+ int fProofVerif; // verifies the proof
+ int nVars; // the number of variables
+ int nVarsAB; // the number of global variables
+ int nRoots; // the number of root clauses
+ int nClauses; // the number of all clauses
+ int nClausesA; // the number of clauses of A
+ Pr_Cls_t * pHead; // the head clause
+ Pr_Cls_t * pTail; // the tail clause
+ Pr_Cls_t * pLearnt; // the tail clause
+ Pr_Cls_t * pEmpty; // the empty clause
+ // internal BCP
+ int nRootSize; // the number of root level assignments
+ int nTrailSize; // the number of assignments made
+ lit * pTrail; // chronological order of assignments (size nVars)
+ lit * pAssigns; // assignments by variable (size nVars)
+ char * pSeens; // temporary mark (size nVars)
+ char * pVarTypes; // variable type (size nVars) [1=A, 0=B, <0=AB]
+ Pr_Cls_t ** pReasons; // reasons for each assignment (size nVars)
+ Pr_Cls_t ** pWatches; // watched clauses for each literal (size 2*nVars)
+ int nVarsAlloc; // the allocated size of arrays
+ // proof recording
+ void * pManProof; // proof manager
+ int Counter; // counter of resolved clauses
+ // memory management
+ int nChunkSize; // the number of bytes in a chunk
+ int nChunkUsed; // the number of bytes used in the last chunk
+ char * pChunkLast; // the last memory chunk
+ // internal verification
+ lit * pResLits; // the literals of the resolvent
+ int nResLits; // the number of literals of the resolvent
+ int nResLitsAlloc;// the number of literals of the resolvent
+ // runtime stats
+ int timeBcp;
+ int timeTrace;
+ int timeRead;
+ int timeTotal;
+};
+
+#ifndef PRT
+#define PRT(a,t) printf("%s = ", (a)); printf("%6.2f sec\n", (float)(t)/(float)(CLOCKS_PER_SEC))
+#endif
+
+// variable assignments
+static const lit LIT_UNDEF = 0xffffffff;
+
+// variable/literal conversions (taken from MiniSat)
+static inline lit toLit (int v) { return v + v; }
+static inline lit toLitCond(int v, int c) { return v + v + (c != 0); }
+static inline lit lit_neg (lit l) { return l ^ 1; }
+static inline int lit_var (lit l) { return l >> 1; }
+static inline int lit_sign (lit l) { return l & 1; }
+static inline int lit_print(lit l) { return lit_sign(l)? -lit_var(l)-1 : lit_var(l)+1; }
+static inline lit lit_read (int s) { return s > 0 ? toLit(s-1) : lit_neg(toLit(-s-1)); }
+static inline int lit_check(lit l, int n) { return l >= 0 && lit_var(l) < n; }
+
+// iterators through the clauses
+#define Pr_ManForEachClause( p, pCls ) for( pCls = p->pHead; pCls; pCls = pCls->pNext )
+#define Pr_ManForEachClauseRoot( p, pCls ) for( pCls = p->pHead; pCls != p->pLearnt; pCls = pCls->pNext )
+#define Pr_ManForEachClauseLearnt( p, pCls ) for( pCls = p->pLearnt; pCls; pCls = pCls->pNext )
+
+// static procedures
+static char * Pr_ManMemoryFetch( Pr_Man_t * p, int nBytes );
+static void Pr_ManMemoryStop( Pr_Man_t * p );
+static void Pr_ManResize( Pr_Man_t * p, int nVarsNew );
+
+// exported procedures
+extern Pr_Man_t * Pr_ManAlloc( int nVarsAlloc );
+extern void Pr_ManFree( Pr_Man_t * p );
+extern int Pr_ManAddClause( Pr_Man_t * p, lit * pBeg, lit * pEnd, int fRoot, int fClauseA );
+extern int Pr_ManProofWrite( Pr_Man_t * p );
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Allocate proof manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Pr_Man_t * Pr_ManAlloc( int nVarsAlloc )
+{
+ Pr_Man_t * p;
+ // allocate the manager
+ p = (Pr_Man_t *)malloc( sizeof(Pr_Man_t) );
+ memset( p, 0, sizeof(Pr_Man_t) );
+ // allocate internal arrays
+ Pr_ManResize( p, nVarsAlloc? nVarsAlloc : 256 );
+ // set the starting number of variables
+ p->nVars = 0;
+ // memory management
+ p->nChunkSize = (1<<16); // use 64K chunks
+ // verification
+ p->nResLitsAlloc = (1<<16);
+ p->pResLits = malloc( sizeof(lit) * p->nResLitsAlloc );
+ // parameters
+ p->fProofWrite = 0;
+ p->fProofVerif = 0;
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Resize proof manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Pr_ManResize( Pr_Man_t * p, int nVarsNew )
+{
+ // check if resizing is needed
+ if ( p->nVarsAlloc < nVarsNew )
+ {
+ int nVarsAllocOld = p->nVarsAlloc;
+ // find the new size
+ if ( p->nVarsAlloc == 0 )
+ p->nVarsAlloc = 1;
+ while ( p->nVarsAlloc < nVarsNew )
+ p->nVarsAlloc *= 2;
+ // resize the arrays
+ p->pTrail = (lit *) realloc( p->pTrail, sizeof(lit) * p->nVarsAlloc );
+ p->pAssigns = (lit *) realloc( p->pAssigns, sizeof(lit) * p->nVarsAlloc );
+ p->pSeens = (char *) realloc( p->pSeens, sizeof(char) * p->nVarsAlloc );
+ p->pVarTypes = (char *) realloc( p->pVarTypes, sizeof(char) * p->nVarsAlloc );
+ p->pReasons = (Pr_Cls_t **)realloc( p->pReasons, sizeof(Pr_Cls_t *) * p->nVarsAlloc );
+ p->pWatches = (Pr_Cls_t **)realloc( p->pWatches, sizeof(Pr_Cls_t *) * p->nVarsAlloc*2 );
+ // clean the free space
+ memset( p->pAssigns + nVarsAllocOld, 0xff, sizeof(lit) * (p->nVarsAlloc - nVarsAllocOld) );
+ memset( p->pSeens + nVarsAllocOld, 0, sizeof(char) * (p->nVarsAlloc - nVarsAllocOld) );
+ memset( p->pVarTypes + nVarsAllocOld, 0, sizeof(char) * (p->nVarsAlloc - nVarsAllocOld) );
+ memset( p->pReasons + nVarsAllocOld, 0, sizeof(Pr_Cls_t *) * (p->nVarsAlloc - nVarsAllocOld) );
+ memset( p->pWatches + nVarsAllocOld, 0, sizeof(Pr_Cls_t *) * (p->nVarsAlloc - nVarsAllocOld)*2 );
+ }
+ // adjust the number of variables
+ if ( p->nVars < nVarsNew )
+ p->nVars = nVarsNew;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Deallocate proof manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Pr_ManFree( Pr_Man_t * p )
+{
+ printf( "Runtime stats:\n" );
+PRT( "Reading ", p->timeRead );
+PRT( "BCP ", p->timeBcp );
+PRT( "Trace ", p->timeTrace );
+PRT( "TOTAL ", p->timeTotal );
+
+ Pr_ManMemoryStop( p );
+ free( p->pTrail );
+ free( p->pAssigns );
+ free( p->pSeens );
+ free( p->pVarTypes );
+ free( p->pReasons );
+ free( p->pWatches );
+ free( p->pResLits );
+ free( p );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Adds one clause to the watcher list.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static inline void Pr_ManWatchClause( Pr_Man_t * p, Pr_Cls_t * pClause, lit Lit )
+{
+ assert( lit_check(Lit, p->nVars) );
+ if ( pClause->pLits[0] == Lit )
+ pClause->pNext0 = p->pWatches[lit_neg(Lit)];
+ else
+ {
+ assert( pClause->pLits[1] == Lit );
+ pClause->pNext1 = p->pWatches[lit_neg(Lit)];
+ }
+ p->pWatches[lit_neg(Lit)] = pClause;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Adds one clause to the manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Pr_ManAddClause( Pr_Man_t * p, lit * pBeg, lit * pEnd, int fRoot, int fClauseA )
+{
+ Pr_Cls_t * pClause;
+ lit Lit, * i, * j;
+ int nSize, VarMax;
+
+ // process the literals
+ if ( pBeg < pEnd )
+ {
+ // insertion sort
+ VarMax = lit_var( *pBeg );
+ for ( i = pBeg + 1; i < pEnd; i++ )
+ {
+ Lit = *i;
+ VarMax = lit_var(Lit) > VarMax ? lit_var(Lit) : VarMax;
+ for ( j = i; j > pBeg && *(j-1) > Lit; j-- )
+ *j = *(j-1);
+ *j = Lit;
+ }
+ // make sure there is no duplicated variables
+ for ( i = pBeg + 1; i < pEnd; i++ )
+ assert( lit_var(*(i-1)) != lit_var(*i) );
+ // resize the manager
+ Pr_ManResize( p, VarMax+1 );
+ }
+
+ // get memory for the clause
+ nSize = sizeof(Pr_Cls_t) + sizeof(lit) * (pEnd - pBeg);
+ pClause = (Pr_Cls_t *)Pr_ManMemoryFetch( p, nSize );
+ memset( pClause, 0, sizeof(Pr_Cls_t) );
+
+ // assign the clause
+ assert( !fClauseA || fRoot ); // clause of A is always a root clause
+ p->nRoots += fRoot;
+ p->nClausesA += fClauseA;
+ pClause->Id = p->nClauses++;
+ pClause->fA = fClauseA;
+ pClause->fRoot = fRoot;
+ pClause->nLits = pEnd - pBeg;
+ memcpy( pClause->pLits, pBeg, sizeof(lit) * (pEnd - pBeg) );
+
+ // add the clause to the list
+ if ( p->pHead == NULL )
+ p->pHead = pClause;
+ if ( p->pTail == NULL )
+ p->pTail = pClause;
+ else
+ {
+ p->pTail->pNext = pClause;
+ p->pTail = pClause;
+ }
+
+ // mark the first learnt clause
+ if ( p->pLearnt == NULL && !pClause->fRoot )
+ p->pLearnt = pClause;
+
+ // add the empty clause
+ if ( pClause->nLits == 0 )
+ {
+ if ( p->pEmpty )
+ printf( "More than one empty clause!\n" );
+ p->pEmpty = pClause;
+ }
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Fetches memory.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+char * Pr_ManMemoryFetch( Pr_Man_t * p, int nBytes )
+{
+ char * pMem;
+ if ( p->pChunkLast == NULL || nBytes > p->nChunkSize - p->nChunkUsed )
+ {
+ pMem = (char *)malloc( p->nChunkSize );
+ *(char **)pMem = p->pChunkLast;
+ p->pChunkLast = pMem;
+ p->nChunkUsed = sizeof(char *);
+ }
+ pMem = p->pChunkLast + p->nChunkUsed;
+ p->nChunkUsed += nBytes;
+ return pMem;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Frees memory manager.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Pr_ManMemoryStop( Pr_Man_t * p )
+{
+ char * pMem, * pNext;
+ if ( p->pChunkLast == NULL )
+ return;
+ for ( pMem = p->pChunkLast; pNext = *(char **)pMem; pMem = pNext )
+ free( pMem );
+ free( pMem );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reports memory usage in bytes.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Pr_ManMemoryReport( Pr_Man_t * p )
+{
+ int Total;
+ char * pMem, * pNext;
+ if ( p->pChunkLast == NULL )
+ return 0;
+ Total = p->nChunkUsed;
+ for ( pMem = p->pChunkLast; pNext = *(char **)pMem; pMem = pNext )
+ Total += p->nChunkSize;
+ return Total;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Records the proof.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Extra_PrintBinary_( FILE * pFile, unsigned Sign[], int nBits )
+{
+ int Remainder, nWords;
+ int w, i;
+
+ Remainder = (nBits%(sizeof(unsigned)*8));
+ nWords = (nBits/(sizeof(unsigned)*8)) + (Remainder>0);
+
+ for ( w = nWords-1; w >= 0; w-- )
+ for ( i = ((w == nWords-1 && Remainder)? Remainder-1: 31); i >= 0; i-- )
+ fprintf( pFile, "%c", '0' + (int)((Sign[w] & (1<<i)) > 0) );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prints the interpolant for one clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Pr_ManPrintInterOne( Pr_Man_t * p, Pr_Cls_t * pClause )
+{
+ printf( "Clause %2d : ", pClause->Id );
+ Extra_PrintBinary_( stdout, &pClause->uTruth, (1 << p->nVarsAB) );
+ printf( "\n" );
+}
+
+
+
+/**Function*************************************************************
+
+ Synopsis [Records implication.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static inline int Pr_ManEnqueue( Pr_Man_t * p, lit Lit, Pr_Cls_t * pReason )
+{
+ int Var = lit_var(Lit);
+ if ( p->pAssigns[Var] != LIT_UNDEF )
+ return p->pAssigns[Var] == Lit;
+ p->pAssigns[Var] = Lit;
+ p->pReasons[Var] = pReason;
+ p->pTrail[p->nTrailSize++] = Lit;
+//printf( "assigning var %d value %d\n", Var, !lit_sign(Lit) );
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Records implication.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static inline void Pr_ManCancelUntil( Pr_Man_t * p, int Level )
+{
+ lit Lit;
+ int i, Var;
+ for ( i = p->nTrailSize - 1; i >= Level; i-- )
+ {
+ Lit = p->pTrail[i];
+ Var = lit_var( Lit );
+ p->pReasons[Var] = NULL;
+ p->pAssigns[Var] = LIT_UNDEF;
+//printf( "cancelling var %d\n", Var );
+ }
+ p->nTrailSize = Level;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Propagate one assignment.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+static inline Pr_Cls_t * Pr_ManPropagateOne( Pr_Man_t * p, lit Lit )
+{
+ Pr_Cls_t ** ppPrev, * pCur, * pTemp;
+ lit LitF = lit_neg(Lit);
+ int i;
+ // iterate through the literals
+ ppPrev = p->pWatches + Lit;
+ for ( pCur = p->pWatches[Lit]; pCur; pCur = *ppPrev )
+ {
+ // make sure the false literal is in the second literal of the clause
+ if ( pCur->pLits[0] == LitF )
+ {
+ pCur->pLits[0] = pCur->pLits[1];
+ pCur->pLits[1] = LitF;
+ pTemp = pCur->pNext0;
+ pCur->pNext0 = pCur->pNext1;
+ pCur->pNext1 = pTemp;
+ }
+ assert( pCur->pLits[1] == LitF );
+
+ // if the first literal is true, the clause is satisfied
+ if ( pCur->pLits[0] == p->pAssigns[lit_var(pCur->pLits[0])] )
+ {
+ ppPrev = &pCur->pNext1;
+ continue;
+ }
+
+ // look for a new literal to watch
+ for ( i = 2; i < (int)pCur->nLits; i++ )
+ {
+ // skip the case when the literal is false
+ if ( lit_neg(pCur->pLits[i]) == p->pAssigns[lit_var(pCur->pLits[i])] )
+ continue;
+ // the literal is either true or unassigned - watch it
+ pCur->pLits[1] = pCur->pLits[i];
+ pCur->pLits[i] = LitF;
+ // remove this clause from the watch list of Lit
+ *ppPrev = pCur->pNext1;
+ // add this clause to the watch list of pCur->pLits[i] (now it is pCur->pLits[1])
+ Pr_ManWatchClause( p, pCur, pCur->pLits[1] );
+ break;
+ }
+ if ( i < (int)pCur->nLits ) // found new watch
+ continue;
+
+ // clause is unit - enqueue new implication
+ if ( Pr_ManEnqueue(p, pCur->pLits[0], pCur) )
+ {
+ ppPrev = &pCur->pNext1;
+ continue;
+ }
+
+ // conflict detected - return the conflict clause
+ return pCur;
+ }
+ return NULL;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Propagate the current assignments.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Pr_Cls_t * Pr_ManPropagate( Pr_Man_t * p, int Start )
+{
+ Pr_Cls_t * pClause;
+ int i;
+ int clk = clock();
+ for ( i = Start; i < p->nTrailSize; i++ )
+ {
+ pClause = Pr_ManPropagateOne( p, p->pTrail[i] );
+ if ( pClause )
+ {
+p->timeBcp += clock() - clk;
+ return pClause;
+ }
+ }
+p->timeBcp += clock() - clk;
+ return NULL;
+}
+
+
+/**Function*************************************************************
+
+ Synopsis [Prints the clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Pr_ManPrintClause( Pr_Cls_t * pClause )
+{
+ int i;
+ printf( "Clause ID = %d. Proof = %d. {", pClause->Id, (int)pClause->pProof );
+ for ( i = 0; i < (int)pClause->nLits; i++ )
+ printf( " %d", pClause->pLits[i] );
+ printf( " }\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Prints the resolvent.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Pr_ManPrintResolvent( lit * pResLits, int nResLits )
+{
+ int i;
+ printf( "Resolvent: {" );
+ for ( i = 0; i < nResLits; i++ )
+ printf( " %d", pResLits[i] );
+ printf( " }\n" );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Writes one root clause into a file.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Pr_ManProofWriteOne( Pr_Man_t * p, Pr_Cls_t * pClause )
+{
+ pClause->pProof = (void *)++p->Counter;
+
+ if ( p->fProofWrite )
+ {
+ int v;
+ fprintf( p->pManProof, "%d", (int)pClause->pProof );
+ for ( v = 0; v < (int)pClause->nLits; v++ )
+ fprintf( p->pManProof, " %d", lit_print(pClause->pLits[v]) );
+ fprintf( p->pManProof, " 0 0\n" );
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Traces the proof for one clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Pr_ManProofTraceOne( Pr_Man_t * p, Pr_Cls_t * pConflict, Pr_Cls_t * pFinal )
+{
+ Pr_Cls_t * pReason;
+ int i, v, Var, PrevId;
+ int fPrint = 0;
+ int clk = clock();
+
+ // collect resolvent literals
+ if ( p->fProofVerif )
+ {
+ assert( (int)pConflict->nLits <= p->nResLitsAlloc );
+ memcpy( p->pResLits, pConflict->pLits, sizeof(lit) * pConflict->nLits );
+ p->nResLits = pConflict->nLits;
+ }
+
+ // mark all the variables in the conflict as seen
+ for ( v = 0; v < (int)pConflict->nLits; v++ )
+ p->pSeens[lit_var(pConflict->pLits[v])] = 1;
+
+ // start the anticedents
+// pFinal->pAntis = Vec_PtrAlloc( 32 );
+// Vec_PtrPush( pFinal->pAntis, pConflict );
+
+ if ( p->nClausesA )
+ pFinal->uTruth = pConflict->uTruth;
+
+ // follow the trail backwards
+ PrevId = (int)pConflict->pProof;
+ for ( i = p->nTrailSize - 1; i >= 0; i-- )
+ {
+ // skip literals that are not involved
+ Var = lit_var(p->pTrail[i]);
+ if ( !p->pSeens[Var] )
+ continue;
+ p->pSeens[Var] = 0;
+
+ // skip literals of the resulting clause
+ pReason = p->pReasons[Var];
+ if ( pReason == NULL )
+ continue;
+ assert( p->pTrail[i] == pReason->pLits[0] );
+
+ // add the variables to seen
+ for ( v = 1; v < (int)pReason->nLits; v++ )
+ p->pSeens[lit_var(pReason->pLits[v])] = 1;
+
+
+ // record the reason clause
+ assert( pReason->pProof > 0 );
+ p->Counter++;
+ if ( p->fProofWrite )
+ fprintf( p->pManProof, "%d * %d %d 0\n", p->Counter, PrevId, (int)pReason->pProof );
+ PrevId = p->Counter;
+
+ if ( p->nClausesA )
+ {
+ if ( p->pVarTypes[Var] == 1 ) // var of A
+ pFinal->uTruth |= pReason->uTruth;
+ else
+ pFinal->uTruth &= pReason->uTruth;
+ }
+
+ // resolve the temporary resolvent with the reason clause
+ if ( p->fProofVerif )
+ {
+ int v1, v2;
+ if ( fPrint )
+ Pr_ManPrintResolvent( p->pResLits, p->nResLits );
+ // check that the var is present in the resolvent
+ for ( v1 = 0; v1 < p->nResLits; v1++ )
+ if ( lit_var(p->pResLits[v1]) == Var )
+ break;
+ if ( v1 == p->nResLits )
+ printf( "Recording clause %d: Cannot find variable %d in the temporary resolvent.\n", pFinal->Id, Var );
+ if ( p->pResLits[v1] != lit_neg(pReason->pLits[0]) )
+ printf( "Recording clause %d: The resolved variable %d is in the wrong polarity.\n", pFinal->Id, Var );
+ // remove this variable from the resolvent
+ assert( lit_var(p->pResLits[v1]) == Var );
+ p->nResLits--;
+ for ( ; v1 < p->nResLits; v1++ )
+ p->pResLits[v1] = p->pResLits[v1+1];
+ // add variables of the reason clause
+ for ( v2 = 1; v2 < (int)pReason->nLits; v2++ )
+ {
+ for ( v1 = 0; v1 < p->nResLits; v1++ )
+ if ( lit_var(p->pResLits[v1]) == lit_var(pReason->pLits[v2]) )
+ break;
+ // if it is a new variable, add it to the resolvent
+ if ( v1 == p->nResLits )
+ {
+ if ( p->nResLits == p->nResLitsAlloc )
+ printf( "Recording clause %d: Ran out of space for intermediate resolvent.\n, pFinal->Id" );
+ p->pResLits[ p->nResLits++ ] = pReason->pLits[v2];
+ continue;
+ }
+ // if the variable is the same, the literal should be the same too
+ if ( p->pResLits[v1] == pReason->pLits[v2] )
+ continue;
+ // the literal is different
+ printf( "Recording clause %d: Trying to resolve the clause with more than one opposite literal.\n", pFinal->Id );
+ }
+ }
+
+// Vec_PtrPush( pFinal->pAntis, pReason );
+ }
+
+ // unmark all seen variables
+// for ( i = p->nTrailSize - 1; i >= 0; i-- )
+// p->pSeens[lit_var(p->pTrail[i])] = 0;
+ // check that the literals are unmarked
+// for ( i = p->nTrailSize - 1; i >= 0; i-- )
+// assert( p->pSeens[lit_var(p->pTrail[i])] == 0 );
+
+ // use the resulting clause to check the correctness of resolution
+ if ( p->fProofVerif )
+ {
+ int v1, v2;
+ if ( fPrint )
+ Pr_ManPrintResolvent( p->pResLits, p->nResLits );
+ for ( v1 = 0; v1 < p->nResLits; v1++ )
+ {
+ for ( v2 = 0; v2 < (int)pFinal->nLits; v2++ )
+ if ( pFinal->pLits[v2] == p->pResLits[v1] )
+ break;
+ if ( v2 < (int)pFinal->nLits )
+ continue;
+ break;
+ }
+ if ( v1 < p->nResLits )
+ {
+ printf( "Recording clause %d: The final resolvent is wrong.\n", pFinal->Id );
+ Pr_ManPrintClause( pConflict );
+ Pr_ManPrintResolvent( p->pResLits, p->nResLits );
+ Pr_ManPrintClause( pFinal );
+ }
+ }
+p->timeTrace += clock() - clk;
+
+ // return the proof pointer
+ if ( p->nClausesA )
+ {
+ Pr_ManPrintInterOne( p, pFinal );
+ }
+ return p->Counter;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Records the proof for one clause.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Pr_ManProofRecordOne( Pr_Man_t * p, Pr_Cls_t * pClause )
+{
+ Pr_Cls_t * pConflict;
+ int i;
+
+ // empty clause never ends up there
+ assert( pClause->nLits > 0 );
+ if ( pClause->nLits == 0 )
+ printf( "Error: Empty clause is attempted.\n" );
+
+ // add assumptions to the trail
+ assert( !pClause->fRoot );
+ assert( p->nTrailSize == p->nRootSize );
+ for ( i = 0; i < (int)pClause->nLits; i++ )
+ if ( !Pr_ManEnqueue( p, lit_neg(pClause->pLits[i]), NULL ) )
+ {
+ assert( 0 ); // impossible
+ return 0;
+ }
+
+ // propagate the assumptions
+ pConflict = Pr_ManPropagate( p, p->nRootSize );
+ if ( pConflict == NULL )
+ {
+ assert( 0 ); // cannot prove
+ return 0;
+ }
+
+ // construct the proof
+ pClause->pProof = (void *)Pr_ManProofTraceOne( p, pConflict, pClause );
+
+ // undo to the root level
+ Pr_ManCancelUntil( p, p->nRootSize );
+
+ // add large clauses to the watched lists
+ if ( pClause->nLits > 1 )
+ {
+ Pr_ManWatchClause( p, pClause, pClause->pLits[0] );
+ Pr_ManWatchClause( p, pClause, pClause->pLits[1] );
+ return 1;
+ }
+ assert( pClause->nLits == 1 );
+
+ // if the clause proved is unit, add it and propagate
+ if ( !Pr_ManEnqueue( p, pClause->pLits[0], pClause ) )
+ {
+ assert( 0 ); // impossible
+ return 0;
+ }
+
+ // propagate the assumption
+ pConflict = Pr_ManPropagate( p, p->nRootSize );
+ if ( pConflict )
+ {
+ // construct the proof
+ p->pEmpty->pProof = (void *)Pr_ManProofTraceOne( p, pConflict, p->pEmpty );
+ printf( "Found last conflict after adding unit clause number %d!\n", pClause->Id );
+ return 0;
+ }
+
+ // update the root level
+ p->nRootSize = p->nTrailSize;
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Propagate the root clauses.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Pr_ManProcessRoots( Pr_Man_t * p )
+{
+ Pr_Cls_t * pClause;
+ int Counter;
+
+ // make sure the root clauses are preceeding the learnt clauses
+ Counter = 0;
+ Pr_ManForEachClause( p, pClause )
+ {
+ assert( (int)pClause->fA == (Counter < (int)p->nClausesA) );
+ assert( (int)pClause->fRoot == (Counter < (int)p->nRoots) );
+ Counter++;
+ }
+ assert( p->nClauses == Counter );
+
+ // make sure the last clause if empty
+ assert( p->pTail->nLits == 0 );
+
+ // go through the root unit clauses
+ p->nTrailSize = 0;
+ Pr_ManForEachClauseRoot( p, pClause )
+ {
+ // create watcher lists for the root clauses
+ if ( pClause->nLits > 1 )
+ {
+ Pr_ManWatchClause( p, pClause, pClause->pLits[0] );
+ Pr_ManWatchClause( p, pClause, pClause->pLits[1] );
+ }
+ // empty clause and large clauses
+ if ( pClause->nLits != 1 )
+ continue;
+ // unit clause
+ assert( lit_check(pClause->pLits[0], p->nVars) );
+ if ( !Pr_ManEnqueue( p, pClause->pLits[0], pClause ) )
+ {
+ // detected root level conflict
+ printf( "Pr_ManProcessRoots(): Detected a root-level conflict\n" );
+ assert( 0 );
+ return 0;
+ }
+ }
+
+ // propagate the root unit clauses
+ pClause = Pr_ManPropagate( p, 0 );
+ if ( pClause )
+ {
+ // detected root level conflict
+ printf( "Pr_ManProcessRoots(): Detected a root-level conflict\n" );
+ assert( 0 );
+ return 0;
+ }
+
+ // set the root level
+ p->nRootSize = p->nTrailSize;
+ return 1;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Records the proof.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Pr_ManPrepareInter( Pr_Man_t * p )
+{
+ unsigned uTruths[5] = { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };
+ Pr_Cls_t * pClause;
+ int Var, v;
+
+ // mark the variable encountered in the clauses of A
+ Pr_ManForEachClauseRoot( p, pClause )
+ {
+ if ( !pClause->fA )
+ break;
+ for ( v = 0; v < (int)pClause->nLits; v++ )
+ p->pVarTypes[lit_var(pClause->pLits[v])] = 1;
+ }
+
+ // check variables that appear in clauses of B
+ p->nVarsAB = 0;
+ Pr_ManForEachClauseRoot( p, pClause )
+ {
+ if ( pClause->fA )
+ continue;
+ for ( v = 0; v < (int)pClause->nLits; v++ )
+ {
+ Var = lit_var(pClause->pLits[v]);
+ if ( p->pVarTypes[Var] == 1 ) // var of A
+ {
+ // change it into a global variable
+ p->nVarsAB++;
+ p->pVarTypes[Var] = -1;
+ }
+ }
+ }
+
+ // order global variables
+ p->nVarsAB = 0;
+ for ( v = 0; v < p->nVars; v++ )
+ if ( p->pVarTypes[v] == -1 )
+ p->pVarTypes[v] -= p->nVarsAB++;
+printf( "There are %d global variables.\n", p->nVarsAB );
+
+ // set interpolants for root clauses
+ Pr_ManForEachClauseRoot( p, pClause )
+ {
+ if ( !pClause->fA ) // clause of B
+ {
+ pClause->uTruth = ~0;
+ Pr_ManPrintInterOne( p, pClause );
+ continue;
+ }
+ // clause of A
+ pClause->uTruth = 0;
+ for ( v = 0; v < (int)pClause->nLits; v++ )
+ {
+ Var = lit_var(pClause->pLits[v]);
+ if ( p->pVarTypes[Var] < 0 ) // global var
+ {
+ if ( lit_sign(pClause->pLits[v]) ) // negative var
+ pClause->uTruth |= ~uTruths[ -p->pVarTypes[Var]-1 ];
+ else
+ pClause->uTruth |= uTruths[ -p->pVarTypes[Var]-1 ];
+ }
+ }
+ Pr_ManPrintInterOne( p, pClause );
+ }
+}
+
+/**Function*************************************************************
+
+ Synopsis [Records the proof.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Pr_ManProofWrite( Pr_Man_t * p )
+{
+ Pr_Cls_t * pClause;
+ int RetValue = 1;
+
+ // propagate root level assignments
+ Pr_ManProcessRoots( p );
+
+ // prepare the interpolant computation
+ if ( p->nClausesA )
+ Pr_ManPrepareInter( p );
+
+ // construct proof for each clause
+ // start the proof
+ if ( p->fProofWrite )
+ p->pManProof = fopen( "proof.cnf_", "w" );
+ p->Counter = 0;
+
+ // write the root clauses
+ Pr_ManForEachClauseRoot( p, pClause )
+ Pr_ManProofWriteOne( p, pClause );
+
+ // consider each learned clause
+ Pr_ManForEachClauseLearnt( p, pClause )
+ {
+ if ( !Pr_ManProofRecordOne( p, pClause ) )
+ {
+ RetValue = 0;
+ break;
+ }
+ }
+
+ if ( p->nClausesA )
+ {
+ printf( "Interpolant: " );
+ }
+
+
+ // stop the proof
+ if ( p->fProofWrite )
+ {
+ fclose( p->pManProof );
+ p->pManProof = NULL;
+ }
+ return RetValue;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Reads clauses from file.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+Pr_Man_t * Pr_ManProofRead( char * pFileName )
+{
+ Pr_Man_t * p = NULL;
+ char * pCur, * pBuffer = NULL;
+ int * pArray = NULL;
+ FILE * pFile;
+ int RetValue, Counter, nNumbers, Temp;
+ int nClauses, nClausesA, nRoots, nVars;
+
+ // open the file
+ pFile = fopen( pFileName, "r" );
+ if ( pFile == NULL )
+ {
+ printf( "Count not open input file \"%s\".\n", pFileName );
+ return NULL;
+ }
+
+ // read the file
+ pBuffer = (char *)malloc( (1<<16) );
+ for ( Counter = 0; fgets( pBuffer, (1<<16), pFile ); )
+ {
+ if ( pBuffer[0] == 'c' )
+ continue;
+ if ( pBuffer[0] == 'p' )
+ {
+ assert( p == NULL );
+ nClausesA = 0;
+ RetValue = sscanf( pBuffer + 1, "%d %d %d %d", &nVars, &nClauses, &nRoots, &nClausesA );
+ if ( RetValue != 3 && RetValue != 4 )
+ {
+ printf( "Wrong input file format.\n" );
+ }
+ p = Pr_ManAlloc( nVars );
+ pArray = (int *)malloc( sizeof(int) * (nVars + 10) );
+ continue;
+ }
+ // skip empty lines
+ for ( pCur = pBuffer; *pCur; pCur++ )
+ if ( !(*pCur == ' ' || *pCur == '\t' || *pCur == '\r' || *pCur == '\n') )
+ break;
+ if ( *pCur == 0 )
+ continue;
+ // scan the numbers from file
+ nNumbers = 0;
+ pCur = pBuffer;
+ while ( *pCur )
+ {
+ // skip spaces
+ for ( ; *pCur && *pCur == ' '; pCur++ );
+ // read next number
+ Temp = 0;
+ sscanf( pCur, "%d", &Temp );
+ if ( Temp == 0 )
+ break;
+ pArray[ nNumbers++ ] = lit_read( Temp );
+ // skip non-spaces
+ for ( ; *pCur && *pCur != ' '; pCur++ );
+ }
+ // add the clause
+ if ( !Pr_ManAddClause( p, pArray, pArray + nNumbers, Counter < nRoots, Counter < nClausesA ) )
+ {
+ printf( "Bad clause number %d.\n", Counter );
+ return NULL;
+ }
+ // count the clauses
+ Counter++;
+ }
+ // check the number of clauses
+ if ( Counter != nClauses )
+ printf( "Expected %d clauses but read %d.\n", nClauses, Counter );
+
+ // finish
+ if ( pArray ) free( pArray );
+ if ( pBuffer ) free( pBuffer );
+ fclose( pFile );
+ return p;
+}
+
+/**Function*************************************************************
+
+ Synopsis [Records the proof.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+/*
+int Pr_ManProofCount_rec( Pr_Cls_t * pClause )
+{
+ Pr_Cls_t * pNext;
+ int i, Counter;
+ if ( pClause->fRoot )
+ return 0;
+ if ( pClause->fVisit )
+ return 0;
+ pClause->fVisit = 1;
+ // count the number of visited clauses
+ Counter = 1;
+ Vec_PtrForEachEntry( pClause->pAntis, pNext, i )
+ Counter += Pr_ManProofCount_rec( pNext );
+ return Counter;
+}
+*/
+
+/**Function*************************************************************
+
+ Synopsis [Records the proof.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Pr_ManProofTest( char * pFileName )
+{
+ Pr_Man_t * p;
+ int clk, clkTotal = clock();
+
+clk = clock();
+ p = Pr_ManProofRead( pFileName );
+p->timeRead = clock() - clk;
+ if ( p == NULL )
+ return 0;
+
+ Pr_ManProofWrite( p );
+
+ // print stats
+/*
+ nUsed = Pr_ManProofCount_rec( p->pEmpty );
+ printf( "Roots = %d. Learned = %d. Total = %d. Steps = %d. Ave = %.2f. Used = %d. Ratio = %.2f. \n",
+ p->nRoots, p->nClauses-p->nRoots, p->nClauses, p->Counter,
+ 1.0*(p->Counter-p->nRoots)/(p->nClauses-p->nRoots),
+ nUsed, 1.0*nUsed/(p->nClauses-p->nRoots) );
+*/
+ printf( "Vars = %d. Roots = %d. Learned = %d. Resol steps = %d. Ave = %.2f. Mem = %.2f Mb\n",
+ p->nVars, p->nRoots, p->nClauses-p->nRoots, p->Counter,
+ 1.0*(p->Counter-p->nRoots)/(p->nClauses-p->nRoots),
+ 1.0*Pr_ManMemoryReport(p)/(1<<20) );
+
+p->timeTotal = clock() - clkTotal;
+ Pr_ManFree( p );
+ return 1;
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/proof/pr.h b/src/sat/proof/pr.h
new file mode 100644
index 00000000..1e71a2d3
--- /dev/null
+++ b/src/sat/proof/pr.h
@@ -0,0 +1,65 @@
+/**CFile****************************************************************
+
+ FileName [pr.h]
+
+ SystemName [ABC: Logic synthesis and verification system.]
+
+ PackageName [Proof recording.]
+
+ Synopsis [External declarations.]
+
+ Author [Alan Mishchenko]
+
+ Affiliation [UC Berkeley]
+
+ Date [Ver. 1.0. Started - June 20, 2005.]
+
+ Revision [$Id: pr.h,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#ifndef __PR_H__
+#define __PR_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef _WIN32
+#define inline __inline // compatible with MS VS 6.0
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// INCLUDES ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// PARAMETERS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// BASIC TYPES ///
+////////////////////////////////////////////////////////////////////////
+
+typedef struct Pr_Man_t_ Pr_Man_t;
+
+////////////////////////////////////////////////////////////////////////
+/// MACRO DEFINITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DECLARATIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/*=== pr.c ==========================================================*/
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
diff --git a/src/sat/proof/stats.txt b/src/sat/proof/stats.txt
new file mode 100644
index 00000000..470b1630
--- /dev/null
+++ b/src/sat/proof/stats.txt
@@ -0,0 +1,66 @@
+UC Berkeley, ABC 1.01 (compiled Jan 20 2007 16:47:34)
+abc.rc: No such file or directory
+Loaded "abc.rc" from the parent directory.
+abc 01> test
+Found last conflict after adding unit clause number 10229!
+Roots = 7184. Learned = 3047. Total = 10231. Steps = 196361. Ave = 62.09. Used = 2224. Ratio = 0.73.
+Runtime stats:
+Reading = 0.03 sec
+BCP = 0.32 sec
+Trace = 0.06 sec
+TOTAL = 0.43 sec
+abc 01> test
+Found last conflict after adding unit clause number 7676!
+Roots = 6605. Learned = 1073. Total = 7678. Steps = 52402. Ave = 42.68. Used = 1011. Ratio = 0.94.
+Runtime stats:
+Reading = 0.01 sec
+BCP = 0.02 sec
+Trace = 0.02 sec
+TOTAL = 0.06 sec
+abc 01> test
+Found last conflict after adding unit clause number 37868!
+Roots = 15443. Learned = 22427. Total = 37870. Steps = 2365472. Ave = 104.79. Used = 19763. Ratio = 0.88.
+Runtime stats:
+Reading = 0.20 sec
+BCP = 14.67 sec
+Trace = 0.56 sec
+TOTAL = 15.74 sec
+abc 01>
+
+
+abc 05> wb ibm_bmc/len25u_renc.blif
+abc 05> ps
+(no name) : i/o = 348/ 1 lat = 0 nd = 3648 bdd = 15522 lev = 246
+abc 05> sat -v
+==================================[MINISAT]===================================
+| Conflicts | ORIGINAL | LEARNT | Progress |
+| | Clauses Literals | Limit Clauses Literals Lit/Cl | |
+==============================================================================
+| 0 | 17413 54996 | 5804 0 0 0.0 | 0.000 % |
+| 100 | 17413 54996 | 6384 100 606 6.1 | 0.417 % |
+| 250 | 17413 54996 | 7023 250 1586 6.3 | 0.417 % |
+| 476 | 17413 54996 | 7725 476 3288 6.9 | 0.417 % |
+| 813 | 17413 54996 | 8498 813 7586 9.3 | 0.417 % |
+| 1319 | 17403 54970 | 9347 1318 14848 11.3 | 0.442 % |
+| 2078 | 17403 54970 | 10282 2076 40186 19.4 | 0.466 % |
+| 3217 | 17397 54948 | 11310 3208 99402 31.0 | 0.466 % |
+| 4926 | 17392 54930 | 12441 4911 131848 26.8 | 0.491 % |
+| 7489 | 17392 54930 | 13686 7474 204217 27.3 | 0.491 % |
+| 11336 | 17357 54829 | 15054 11310 332863 29.4 | 0.638 % |
+| 17103 | 17346 54794 | 16559 9130 203029 22.2 | 0.687 % |
+| 25752 | 17288 54606 | 18215 9083 176982 19.5 | 0.834 % |
+| 38727 | 17266 54536 | 20037 12674 278949 22.0 | 0.883 % |
+| 58188 | 17240 54453 | 22041 11905 255255 21.4 | 0.957 % |
+==============================================================================
+Start = 15. Conf = 79435. Dec = 130967. Prop = 24083434. Insp = 136774586.
+Total runtime = 18.66 sec. Var select = 0.00 sec. Var update = 0.00 sec.
+UNSATISFIABLE Time = 18.69 sec
+abc 05>
+abc 05> test
+Found last conflict after adding unit clause number 96902!
+Roots = 17469. Learned = 79435. Total = 96904. Steps = 9700042. Ave = 121.89. Used = 57072. Ratio = 0.72.
+Runtime stats:
+Reading = 1.26 sec
+BCP = 204.99 sec
+Trace = 2.85 sec
+TOTAL = 209.85 sec \ No newline at end of file