summaryrefslogtreecommitdiffstats
path: root/src/sat
diff options
context:
space:
mode:
Diffstat (limited to 'src/sat')
-rw-r--r--src/sat/asat/added.c126
-rw-r--r--src/sat/asat/main.c195
-rw-r--r--src/sat/asat/module.make2
-rw-r--r--src/sat/asat/solver.c1167
-rw-r--r--src/sat/asat/solver.h137
-rw-r--r--src/sat/asat/solver_vec.h53
-rw-r--r--src/sat/fraig/fraig.h194
-rw-r--r--src/sat/fraig/fraigApi.c280
-rw-r--r--src/sat/fraig/fraigCanon.c216
-rw-r--r--src/sat/fraig/fraigFanout.c175
-rw-r--r--src/sat/fraig/fraigFeed.c772
-rw-r--r--src/sat/fraig/fraigInt.h442
-rw-r--r--src/sat/fraig/fraigMan.c237
-rw-r--r--src/sat/fraig/fraigMem.c246
-rw-r--r--src/sat/fraig/fraigNode.c308
-rw-r--r--src/sat/fraig/fraigPrime.c142
-rw-r--r--src/sat/fraig/fraigSat.c1085
-rw-r--r--src/sat/fraig/fraigTable.c596
-rw-r--r--src/sat/fraig/fraigUtil.c969
-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.h160
-rw-r--r--src/sat/msat/msatActivity.c158
-rw-r--r--src/sat/msat/msatClause.c524
-rw-r--r--src/sat/msat/msatClauseVec.c232
-rw-r--r--src/sat/msat/msatInt.h304
-rw-r--r--src/sat/msat/msatMem.c529
-rw-r--r--src/sat/msat/msatOrderH.c405
-rw-r--r--src/sat/msat/msatOrderJ.c466
-rw-r--r--src/sat/msat/msatQueue.c157
-rw-r--r--src/sat/msat/msatRead.c268
-rw-r--r--src/sat/msat/msatSolverApi.c488
-rw-r--r--src/sat/msat/msatSolverCore.c187
-rw-r--r--src/sat/msat/msatSolverIo.c177
-rw-r--r--src/sat/msat/msatSolverSearch.c623
-rw-r--r--src/sat/msat/msatSort.c173
-rw-r--r--src/sat/msat/msatVec.c495
38 files changed, 13258 insertions, 0 deletions
diff --git a/src/sat/asat/added.c b/src/sat/asat/added.c
new file mode 100644
index 00000000..d7f5b104
--- /dev/null
+++ b/src/sat/asat/added.c
@@ -0,0 +1,126 @@
+/**CFile****************************************************************
+
+ FileName [added.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: added.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
+
+***********************************************************************/
+
+#include <stdio.h>
+#include <assert.h>
+#include "solver.h"
+#include "extra.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 inline int lit_var(lit l) { return l >> 1; }
+static inline int lit_sign(lit l) { return (l & 1); }
+
+static void Asat_ClauseWriteDimacs( FILE * pFile, clause * pC, bool fIncrement );
+
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Write the clauses in the solver into a file in DIMACS format.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Asat_SolverWriteDimacs( solver * p, char * pFileName )
+{
+ 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" );
+ 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++ )
+ Asat_ClauseWriteDimacs( pFile, pClauses[i], 1 );
+
+ // write the learned clauses
+ nClauses = p->learnts.size;
+ pClauses = p->learnts.ptr;
+ for ( i = 0; i < nClauses; i++ )
+ Asat_ClauseWriteDimacs( pFile, pClauses[i], 1 );
+
+ // 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 0\n", (p->assigns[i] == l_False)? "-": "", i + 1 );
+
+ fprintf( pFile, "\n" );
+ fclose( pFile );
+}
+
+/**Function*************************************************************
+
+ Synopsis [Writes the given clause in a file in DIMACS format.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Asat_ClauseWriteDimacs( 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" );
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/asat/main.c b/src/sat/asat/main.c
new file mode 100644
index 00000000..cbad5ba1
--- /dev/null
+++ b/src/sat/asat/main.c
@@ -0,0 +1,195 @@
+/**************************************************************************************************
+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 "solver.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+//#include <unistd.h>
+//#include <signal.h>
+//#include <zlib.h>
+//#include <sys/time.h>
+//#include <sys/resource.h>
+
+//=================================================================================================
+// Helpers:
+
+
+// Reads an input stream to end-of-file and returns the result as a 'char*' terminated by '\0'
+// (dynamic allocation in case 'in' is standard input).
+//
+char* readFile(FILE * in)
+{
+ char* data = malloc(65536);
+ int cap = 65536;
+ int size = 0;
+
+ while (!feof(in)){
+ if (size == cap){
+ cap *= 2;
+ data = realloc(data, cap); }
+ size += fread(&data[size], 1, 65536, in);
+ }
+ data = realloc(data, size+1);
+ data[size] = '\0';
+
+ return data;
+}
+
+//static inline double cpuTime(void) {
+// struct rusage ru;
+// getrusage(RUSAGE_SELF, &ru);
+// return (double)ru.ru_utime.tv_sec + (double)ru.ru_utime.tv_usec / 1000000; }
+
+
+//=================================================================================================
+// DIMACS Parser:
+
+
+static inline void skipWhitespace(char** in) {
+ while ((**in >= 9 && **in <= 13) || **in == 32)
+ (*in)++; }
+
+static inline void skipLine(char** in) {
+ for (;;){
+ if (**in == 0) return;
+ if (**in == '\n') { (*in)++; return; }
+ (*in)++; } }
+
+static inline int parseInt(char** in) {
+ int val = 0;
+ int _neg = 0;
+ skipWhitespace(in);
+ if (**in == '-') _neg = 1, (*in)++;
+ else if (**in == '+') (*in)++;
+ if (**in < '0' || **in > '9') fprintf(stderr, "PARSE ERROR! Unexpected char: %c\n", **in), exit(1);
+ while (**in >= '0' && **in <= '9')
+ val = val*10 + (**in - '0'),
+ (*in)++;
+ return _neg ? -val : val; }
+
+static void readClause(char** in, solver* s, vec* lits) {
+ int parsed_lit, var;
+ vec_resize(lits,0);
+ for (;;){
+ parsed_lit = parseInt(in);
+ if (parsed_lit == 0) break;
+ var = abs(parsed_lit)-1;
+ vec_push(lits, (void*)(parsed_lit > 0 ? toLit(var) : neg(toLit(var))));
+ }
+}
+
+static lbool parse_DIMACS_main(char* in, solver* s) {
+ vec lits;
+ vec_new(&lits);
+
+ for (;;){
+ skipWhitespace(&in);
+ if (*in == 0)
+ break;
+ else if (*in == 'c' || *in == 'p')
+ skipLine(&in);
+ else{
+ lit* begin;
+ readClause(&in, s, &lits);
+ begin = (lit*)vec_begin(&lits);
+ if (solver_addclause(s, begin, begin+vec_size(&lits)) == l_False){
+ vec_delete(&lits);
+ return l_False;
+ }
+ }
+ }
+ vec_delete(&lits);
+ return solver_simplify(s);
+}
+
+
+// Inserts problem into solver. Returns FALSE upon immediate conflict.
+//
+static lbool parse_DIMACS(FILE * in, solver* s) {
+ char* text = readFile(in);
+ lbool ret = parse_DIMACS_main(text, s);
+ free(text);
+ return ret; }
+
+
+//=================================================================================================
+
+
+void printStats(stats* stats, int cpu_time)
+{
+ double Time = (float)(cpu_time)/(float)(CLOCKS_PER_SEC);
+ printf("restarts : %12d\n", stats->starts);
+ printf("conflicts : %12.0f (%9.0f / sec )\n", (double)stats->conflicts , (double)stats->conflicts /Time);
+ printf("decisions : %12.0f (%9.0f / sec )\n", (double)stats->decisions , (double)stats->decisions /Time);
+ printf("propagations : %12.0f (%9.0f / sec )\n", (double)stats->propagations, (double)stats->propagations/Time);
+ printf("inspects : %12.0f (%9.0f / sec )\n", (double)stats->inspects , (double)stats->inspects /Time);
+ printf("conflict literals : %12.0f (%9.2f %% deleted )\n", (double)stats->tot_literals, (double)(stats->max_literals - stats->tot_literals) * 100.0 / (double)stats->max_literals);
+ printf("CPU time : %12.2f sec\n", Time);
+}
+
+//solver* slv;
+//static void SIGINT_handler(int signum) {
+// printf("\n"); printf("*** INTERRUPTED ***\n");
+// printStats(&slv->stats, cpuTime());
+// printf("\n"); printf("*** INTERRUPTED ***\n");
+// exit(0); }
+
+
+//=================================================================================================
+
+
+int main(int argc, char** argv)
+{
+ solver* s = solver_new();
+ lbool st;
+ FILE * in;
+ int clk = clock();
+
+ if (argc != 2)
+ fprintf(stderr, "ERROR! Not enough command line arguments.\n"),
+ exit(1);
+
+ in = fopen(argv[1], "rb");
+ if (in == NULL)
+ fprintf(stderr, "ERROR! Could not open file: %s\n", argc == 1 ? "<stdin>" : argv[1]),
+ exit(1);
+ st = parse_DIMACS(in, s);
+ fclose(in);
+
+ if (st == l_False){
+ solver_delete(s);
+ printf("Trivial problem\nUNSATISFIABLE\n");
+ exit(20);
+ }
+
+ s->verbosity = 1;
+// slv = s;
+// signal(SIGINT,SIGINT_handler);
+ st = solver_solve(s,0,0);
+ printStats(&s->stats, clock() - clk);
+ printf("\n");
+ printf(st == l_True ? "SATISFIABLE\n" : "UNSATISFIABLE\n");
+
+ solver_delete(s);
+ return 0;
+}
diff --git a/src/sat/asat/module.make b/src/sat/asat/module.make
new file mode 100644
index 00000000..882176fa
--- /dev/null
+++ b/src/sat/asat/module.make
@@ -0,0 +1,2 @@
+SRC += src/sat/asat/added.c \
+ src/sat/asat/solver.c
diff --git a/src/sat/asat/solver.c b/src/sat/asat/solver.c
new file mode 100644
index 00000000..c9dadcb4
--- /dev/null
+++ b/src/sat/asat/solver.c
@@ -0,0 +1,1167 @@
+/**************************************************************************************************
+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 <math.h>
+
+#include "solver.h"
+
+//=================================================================================================
+// Simple (var/literal) helpers:
+
+static inline int lit_var(lit l) { return l >> 1; }
+static inline int lit_sign(lit l) { return (l & 1); }
+
+//=================================================================================================
+// Debug:
+
+//#define VERBOSEDEBUG
+
+// For derivation output (verbosity level 2)
+#define L_IND "%-*d"
+#define L_ind solver_dlevel(s)*3+3,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:
+
+void 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:
+
+clause* clause_from_lit (lit l) { return (clause*)(l + l + 1); }
+bool clause_is_lit (clause* c) { return ((unsigned int)c & 1); }
+lit clause_read_lit (clause* c) { return (lit)((unsigned int)c >> 1); }
+
+//=================================================================================================
+// Simple helpers:
+
+static inline int solver_dlevel(solver* s) { return vec_size(&s->trail_lim); }
+static inline vec* solver_read_wlist (solver* s, lit l){ return &s->wlists[l]; }
+static inline void vec_remove(vec* v, void* e)
+{
+ void** ws = vec_begin(v);
+ int j = 0;
+
+ for (; ws[j] != e ; j++);
+ assert(j < vec_size(v));
+ for (; j < vec_size(v)-1; j++) ws[j] = ws[j+1];
+ vec_resize(v,vec_size(v)-1);
+}
+
+//=================================================================================================
+// Variable order functions:
+
+static inline void order_update(solver* s, int v) // updateorder
+{
+ int* orderpos = s->orderpos;
+ double* activity = s->activity;
+ int* heap = (int*)vec_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(solver* s, int v)
+{
+}
+
+static inline void order_unassigned(solver* s, int v) // undoorder
+{
+ int* orderpos = s->orderpos;
+ if (orderpos[v] == -1){
+ orderpos[v] = vec_size(&s->order);
+ vec_push(&s->order,(void*)v);
+ order_update(s,v);
+ }
+}
+
+static int order_select(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 = (int*)vec_begin(&s->order);
+ activity = s->activity;
+ orderpos = s->orderpos;
+
+
+ while (vec_size(&s->order) > 0){
+ int next = heap[0];
+ int size = vec_size(&s->order)-1;
+ int x = heap[size];
+
+ vec_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;
+ }
+
+ if (values[next] == l_Undef)
+ return next;
+ }
+
+ return var_Undef;
+}
+
+//=================================================================================================
+// Activity functions:
+
+static inline void act_var_rescale(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(solver* s, int v) {
+ double* activity = s->activity;
+ if ((activity[v] += s->var_inc) > 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(solver* s) { s->var_inc *= s->var_decay; }
+
+static inline void act_clause_rescale(solver* s) {
+ clause** cs = (clause**)vec_begin(&s->learnts);
+ int i;
+ for (i = 0; i < vec_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(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(solver* s) { s->cla_inc *= s->cla_decay; }
+
+
+//=================================================================================================
+// Clause functions:
+
+/* pre: size > 1 && no variable occurs twice
+ */
+static clause* clause_new(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));
+ c->size_learnt = (size << 1) | learnt;
+ assert(((unsigned int)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(neg(begin[0]) < s->size*2);
+ assert(neg(begin[1]) < s->size*2);
+
+ //vec_push(solver_read_wlist(s,neg(begin[0])),(void*)c);
+ //vec_push(solver_read_wlist(s,neg(begin[1])),(void*)c);
+
+ vec_push(solver_read_wlist(s,neg(begin[0])),(void*)(size > 2 ? c : clause_from_lit(begin[1])));
+ vec_push(solver_read_wlist(s,neg(begin[1])),(void*)(size > 2 ? c : clause_from_lit(begin[0])));
+
+ return c;
+}
+
+
+static void clause_remove(solver* s, clause* c)
+{
+ lit* lits = clause_begin(c);
+ assert(neg(lits[0]) < s->size*2);
+ assert(neg(lits[1]) < s->size*2);
+
+ //vec_remove(solver_read_wlist(s,neg(lits[0])),(void*)c);
+ //vec_remove(solver_read_wlist(s,neg(lits[1])),(void*)c);
+
+ assert(lits[0] < s->size*2);
+ vec_remove(solver_read_wlist(s,neg(lits[0])),(void*)(clause_size(c) > 2 ? c : clause_from_lit(lits[1])));
+ vec_remove(solver_read_wlist(s,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);
+ }
+
+ free(c);
+}
+
+
+static lbool clause_simplify(solver* s, clause* c)
+{
+ lit* lits = clause_begin(c);
+ lbool* values = s->assigns;
+ int i;
+
+ assert(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:
+
+static void solver_setnvars(solver* s,int n)
+{
+ int var;
+ if (s->cap < n){
+
+ while (s->cap < n) s->cap = s->cap*2+1;
+
+ s->wlists = (vec*) realloc(s->wlists, sizeof(vec)*s->cap*2);
+ s->activity = (double*) realloc(s->activity, 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++){
+ vec_new(&s->wlists[2*var]);
+ vec_new(&s->wlists[2*var+1]);
+ s->activity [var] = 0;
+ s->assigns [var] = l_Undef;
+ s->orderpos [var] = var;
+ s->reasons [var] = (clause*)0;
+ s->levels [var] = 0;
+ s->tags [var] = l_Undef;
+
+ assert(vec_size(&s->order) == var);
+ vec_push(&s->order,(void*)var);
+ order_update(s,var);
+ }
+
+ s->size = n > s->size ? n : s->size;
+}
+
+
+static inline bool enqueue(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] = solver_dlevel(s);
+ reasons[v] = from;
+ s->trail[s->qtail++] = l;
+
+ order_assigned(s, v);
+ return true;
+ }
+}
+
+
+static inline void assume(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
+ vec_push(&s->trail_lim,(void*)s->qtail);
+ enqueue(s,l,(clause*)0);
+}
+
+
+static inline void solver_canceluntil(solver* s, int level) {
+ lit* trail;
+ lbool* values;
+ clause** reasons;
+ int bound;
+ int c;
+
+ if (solver_dlevel(s) <= level)
+ return;
+
+ trail = s->trail;
+ values = s->assigns;
+ reasons = s->reasons;
+ bound = ((int*)vec_begin(&s->trail_lim))[level];
+
+ 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;
+ vec_resize(&s->trail_lim,level);
+}
+
+static void solver_record(solver* s, vec* cls)
+{
+ lit* begin = (lit*)vec_begin(cls);
+ lit* end = begin + vec_size(cls);
+ clause* c = (vec_size(cls) > 1) ? clause_new(s,begin,end,1) : (clause*)0;
+ enqueue(s,*begin,c);
+
+ assert(vec_size(cls) > 0);
+
+ if (c != 0) {
+ vec_push(&s->learnts,(void*)c);
+ act_clause_bump(s,c);
+ s->stats.learnts++;
+ s->stats.learnts_literals += vec_size(cls);
+ }
+}
+
+
+static double solver_progress(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 solver_lit_removable(solver* s, lit l, int minl)
+{
+ lbool* tags = s->tags;
+ clause** reasons = s->reasons;
+ int* levels = s->levels;
+ int top = vec_size(&s->tagged);
+
+ assert(lit_var(l) >= 0 && lit_var(l) < s->size);
+ assert(reasons[lit_var(l)] != 0);
+ vec_resize(&s->stack,0);
+ vec_push(&s->stack,(void*)lit_var(l));
+
+ while (vec_size(&s->stack) > 0){
+ clause* c;
+ int v = (int)vec_begin(&s->stack)[vec_size(&s->stack)-1];
+ assert(v >= 0 && v < s->size);
+ vec_resize(&s->stack,vec_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)){
+ vec_push(&s->stack,(void*)v);
+ tags[v] = l_True;
+ vec_push(&s->tagged,(void*)v);
+ }else{
+ int* tagged = (int*)vec_begin(&s->tagged);
+ int j;
+ for (j = top; j < vec_size(&s->tagged); j++)
+ tags[tagged[j]] = l_Undef;
+ vec_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)){
+
+ vec_push(&s->stack,(void*)lit_var(lits[i]));
+ tags[v] = l_True;
+ vec_push(&s->tagged,(void*)v);
+ }else{
+ int* tagged = (int*)vec_begin(&s->tagged);
+ for (j = top; j < vec_size(&s->tagged); j++)
+ tags[tagged[j]] = l_Undef;
+ vec_resize(&s->tagged,top);
+ return false;
+ }
+ }
+ }
+ }
+ }
+
+ return true;
+}
+
+static void solver_analyze(solver* s, clause* c, vec* 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;
+
+ vec_push(learnt,(void*)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;
+ vec_push(&s->tagged,(void*)lit_var(q));
+ act_var_bump(s,lit_var(q));
+ if (levels[lit_var(q)] == solver_dlevel(s))
+ cnt++;
+ else
+ vec_push(learnt,(void*)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;
+ vec_push(&s->tagged,(void*)lit_var(q));
+ act_var_bump(s,lit_var(q));
+ if (levels[lit_var(q)] == solver_dlevel(s))
+ cnt++;
+ else
+ vec_push(learnt,(void*)q);
+ }
+ }
+ }
+
+ while (tags[lit_var(trail[ind--])] == l_Undef);
+
+ p = trail[ind+1];
+ c = reasons[lit_var(p)];
+ cnt--;
+
+ }while (cnt > 0);
+
+ *(lit*)vec_begin(learnt) = neg(p);
+
+ lits = (lit*)vec_begin(learnt);
+ minl = 0;
+ for (i = 1; i < vec_size(learnt); i++){
+ int lev = levels[lit_var(lits[i])];
+ minl |= 1 << (lev & 31);
+ }
+
+ // simplify (full)
+ for (i = j = 1; i < vec_size(learnt); i++){
+ if (reasons[lit_var(lits[i])] == 0 || !solver_lit_removable(s,lits[i],minl))
+ lits[j++] = lits[i];
+ }
+
+ // update size of learnt + statistics
+ s->stats.max_literals += vec_size(learnt);
+ vec_resize(learnt,j);
+ s->stats.tot_literals += j;
+
+ // clear tags
+ tagged = (int*)vec_begin(&s->tagged);
+ for (i = 0; i < vec_size(&s->tagged); i++)
+ tags[tagged[i]] = l_Undef;
+ vec_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 < vec_size(learnt); i++) printf(" "L_LIT, L_lit(lits[i]));
+#endif
+ if (vec_size(learnt) > 1){
+ int max_i = 1;
+ int max = levels[lit_var(lits[1])];
+ lit tmp;
+
+ for (i = 2; i < vec_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 = vec_size(learnt) > 1 ? levels[lit_var(lits[1])] : 0;
+ printf(" } at level %d\n", lev);
+ }
+#endif
+}
+
+
+clause* solver_propagate(solver* s)
+{
+ lbool* values = s->assigns;
+ clause* confl = (clause*)0;
+ lit* lits;
+
+ //printf("solver_propagate\n");
+ while (confl == 0 && s->qtail - s->qhead > 0){
+ lit p = s->trail[s->qhead++];
+ vec* ws = solver_read_wlist(s,p);
+ clause **begin = (clause**)vec_begin(ws);
+ clause **end = begin + vec_size(ws);
+ clause **i, **j;
+
+ s->stats.propagations++;
+ s->simpdb_props--;
+
+ //printf("checking lit %d: "L_LIT"\n", vec_size(ws), L_lit(p));
+ for (i = j = begin; i < end; ){
+ if (clause_is_lit(*i)){
+ *j++ = *i;
+ if (!enqueue(s,clause_read_lit(*i),clause_from_lit(p))){
+ confl = s->binary;
+ (clause_begin(confl))[1] = neg(p);
+ (clause_begin(confl))[0] = clause_read_lit(*i++);
+
+ // Copy the remaining watches:
+ 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 = 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;
+ vec_push(solver_read_wlist(s,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:
+ while (i < end)
+ *j++ = *i++;
+ }
+ }
+ }
+ next:
+ i++;
+ }
+
+ s->stats.inspects += j - (clause**)vec_begin(ws);
+ vec_resize(ws,j - (clause**)vec_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 solver_reducedb(solver* s)
+{
+ int i, j;
+ double extra_lim = s->cla_inc / vec_size(&s->learnts); // Remove any clause below this activity
+ clause** learnts = (clause**)vec_begin(&s->learnts);
+ clause** reasons = s->reasons;
+
+ sort(vec_begin(&s->learnts), vec_size(&s->learnts), &clause_cmp);
+
+ for (i = j = 0; i < vec_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 < vec_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", vec_size(&s->learnts) - j);
+
+
+ vec_resize(&s->learnts,j);
+}
+
+static lbool solver_search(solver* s, int nof_conflicts, int nof_learnts)
+{
+ int* levels = s->levels;
+ double var_decay = 0.95;
+ double clause_decay = 0.999;
+ double random_var_freq = 0.02;
+
+ int conflictC = 0;
+ vec learnt_clause;
+
+ assert(s->root_level == solver_dlevel(s));
+
+ s->stats.starts++;
+ s->var_decay = (float)(1 / var_decay );
+ s->cla_decay = (float)(1 / clause_decay);
+ vec_resize(&s->model,0);
+ vec_new(&learnt_clause);
+
+ for (;;){
+ clause* confl = solver_propagate(s);
+ if (confl != 0){
+ // CONFLICT
+ int blevel;
+
+#ifdef VERBOSEDEBUG
+ printf(L_IND"**CONFLICT**\n", L_ind);
+#endif
+ s->stats.conflicts++; conflictC++;
+ if (solver_dlevel(s) == s->root_level){
+ vec_delete(&learnt_clause);
+ return l_False;
+ }
+
+ vec_resize(&learnt_clause,0);
+ solver_analyze(s, confl, &learnt_clause);
+ blevel = vec_size(&learnt_clause) > 1 ? levels[lit_var(((lit*)vec_begin(&learnt_clause))[1])] : s->root_level;
+ solver_canceluntil(s,blevel);
+ 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 = solver_progress(s);
+ solver_canceluntil(s,s->root_level);
+ vec_delete(&learnt_clause);
+ return l_Undef; }
+
+ if (solver_dlevel(s) == 0)
+ // Simplify the set of problem clauses:
+ solver_simplify(s);
+
+ if (nof_learnts >= 0 && vec_size(&s->learnts) - s->qtail >= nof_learnts)
+ // Reduce the set of learnt clauses:
+ 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;
+ for (i = 0; i < s->size; i++) vec_push(&s->model,(void*)((int)values[i]));
+ solver_canceluntil(s,s->root_level);
+ vec_delete(&learnt_clause);
+ return l_True;
+ }
+
+ assume(s,neg(toLit(next)));
+ }
+ }
+
+ return l_Undef; // cannot happen
+}
+
+//=================================================================================================
+// External solver functions:
+
+solver* solver_new(void)
+{
+ solver* s = (solver*)malloc(sizeof(solver));
+
+ // initialize vectors
+ vec_new(&s->clauses);
+ vec_new(&s->learnts);
+ vec_new(&s->order);
+ vec_new(&s->trail_lim);
+ vec_new(&s->tagged);
+ vec_new(&s->stack);
+ vec_new(&s->model);
+
+ // initialize arrays
+ s->wlists = 0;
+ s->activity = 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;
+
+ return s;
+}
+
+
+void solver_delete(solver* s)
+{
+ int i;
+ for (i = 0; i < vec_size(&s->clauses); i++)
+ free(vec_begin(&s->clauses)[i]);
+
+ for (i = 0; i < vec_size(&s->learnts); i++)
+ free(vec_begin(&s->learnts)[i]);
+
+ // delete vectors
+ vec_delete(&s->clauses);
+ vec_delete(&s->learnts);
+ vec_delete(&s->order);
+ vec_delete(&s->trail_lim);
+ vec_delete(&s->tagged);
+ vec_delete(&s->stack);
+ vec_delete(&s->model);
+ free(s->binary);
+
+ // delete arrays
+ if (s->wlists != 0){
+ int i;
+ for (i = 0; i < s->size*2; i++)
+ vec_delete(&s->wlists[i]);
+
+ // if one is different from null, all are
+ free(s->wlists);
+ free(s->activity );
+ free(s->assigns );
+ free(s->orderpos );
+ free(s->reasons );
+ free(s->levels );
+ free(s->trail );
+ free(s->tags );
+ }
+
+ free(s);
+}
+
+
+bool solver_addclause(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;
+ }
+ solver_setnvars(s,maxvar+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 == 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;
+ else if (j - begin == 1) // unit clause
+ return enqueue(s,*begin,(clause*)0);
+
+ // create new clause
+ vec_push(&s->clauses,clause_new(s,begin,j,0));
+
+
+ s->stats.clauses++;
+ s->stats.clauses_literals += j - begin;
+
+ return true;
+}
+
+
+bool solver_simplify(solver* s)
+{
+ clause** reasons;
+ int type;
+
+ assert(solver_dlevel(s) == 0);
+
+ if (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++){
+ vec* cs = type ? &s->learnts : &s->clauses;
+ clause** cls = (clause**)vec_begin(cs);
+
+ int i, j;
+ for (j = i = 0; i < vec_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];
+ }
+ vec_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;
+}
+
+
+bool solver_solve(solver* s, lit* begin, lit* end)
+{
+ double nof_conflicts = 100;
+ double nof_learnts = solver_nclauses(s) / 3;
+ lbool status = l_Undef;
+ lbool* values = s->assigns;
+ lit* i;
+
+ for (i = begin; i < end; i++)
+ if ((lit_sign(*i) ? -values[lit_var(*i)] : values[lit_var(*i)]) == l_False || (assume(s,*i), solver_propagate(s) != 0)){
+ solver_canceluntil(s,0);
+ return false; }
+
+ s->root_level = 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 = solver_search(s,(int)nof_conflicts, (int)nof_learnts);
+ nof_conflicts *= 1.5;
+ nof_learnts *= 1.1;
+ }
+ if (s->verbosity >= 1)
+ printf("==============================================================================\n");
+
+ solver_canceluntil(s,0);
+ return status != l_False;
+}
+
+
+int solver_nvars(solver* s)
+{
+ return s->size;
+}
+
+
+int solver_nclauses(solver* s)
+{
+ return vec_size(&s->clauses);
+}
+
+//=================================================================================================
+// 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 sort(void** array, int size, int(*comp)(const void *, const void *))
+{
+ double seed = 91648253;
+ sortrnd(array,size,comp,&seed);
+}
diff --git a/src/sat/asat/solver.h b/src/sat/asat/solver.h
new file mode 100644
index 00000000..e04d5780
--- /dev/null
+++ b/src/sat/asat/solver.h
@@ -0,0 +1,137 @@
+/**************************************************************************************************
+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 solver_h
+#define solver_h
+
+#ifdef _WIN32
+#define inline __inline // compatible with MS VS 6.0
+#endif
+
+#include "solver_vec.h"
+
+//=================================================================================================
+// Simple types:
+
+//typedef int bool;
+#ifndef bool
+#define bool int
+#endif
+
+static const bool true = 1;
+static const bool false = 0;
+
+typedef int lit;
+typedef char lbool;
+
+#ifdef _WIN32
+typedef signed __int64 uint64; // compatible with MS VS 6.0
+#else
+typedef unsigned long long uint64;
+#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 neg (lit l) { return l ^ 1; }
+static inline lit toLit (int v) { return v + v; }
+
+//=================================================================================================
+// Public interface:
+
+struct solver_t;
+typedef struct solver_t solver;
+
+extern solver* solver_new(void);
+extern void solver_delete(solver* s);
+
+extern bool solver_addclause(solver* s, lit* begin, lit* end);
+extern bool solver_simplify(solver* s);
+extern bool solver_solve(solver* s, lit* begin, lit* end);
+
+extern int solver_nvars(solver* s);
+extern int solver_nclauses(solver* s);
+
+// additional procedures
+extern void Asat_SolverWriteDimacs( solver * pSat, char * pFileName );
+
+struct stats_t
+{
+ uint64 starts, decisions, propagations, inspects, conflicts;
+ uint64 clauses, clauses_literals, learnts, learnts_literals, max_literals, tot_literals;
+};
+typedef struct stats_t stats;
+
+//=================================================================================================
+// Solver representation:
+
+struct clause_t;
+typedef struct clause_t clause;
+
+struct solver_t
+{
+ int size; // nof variables
+ int cap; // size of varmaps
+ int qhead; // Head index of queue.
+ int qtail; // Tail index of queue.
+
+ // clauses
+ vec clauses; // List of problem constraints. (contains: clause*)
+ vec 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.
+
+ vec* 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; //
+ vec tagged; // (contains: var)
+ vec stack; // (contains: var)
+
+ vec order; // Variable order. (heap) (contains: var)
+ vec trail_lim; // Separator indices for different decision levels in 'trail'. (contains: int)
+ vec 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;
+};
+
+#endif
diff --git a/src/sat/asat/solver_vec.h b/src/sat/asat/solver_vec.h
new file mode 100644
index 00000000..fae313d0
--- /dev/null
+++ b/src/sat/asat/solver_vec.h
@@ -0,0 +1,53 @@
+/**************************************************************************************************
+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 vec_h
+#define vec_h
+
+#include <stdlib.h>
+
+struct vec_t {
+ int size;
+ int cap;
+ void** ptr;
+};
+typedef struct vec_t vec;
+
+static inline void vec_new (vec* v) {
+ v->size = 0;
+ v->cap = 4;
+ v->ptr = (void**)malloc(sizeof(void*)*v->cap);
+}
+
+static inline void vec_delete (vec* v) { free(v->ptr); }
+static inline void** vec_begin (vec* v) { return v->ptr; }
+static inline int vec_size (vec* v) { return v->size; }
+static inline void vec_resize (vec* v, int k) { v->size = k; } // only safe to shrink !!
+static inline void vec_push (vec* 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/fraig/fraig.h b/src/sat/fraig/fraig.h
new file mode 100644
index 00000000..53a46584
--- /dev/null
+++ b/src/sat/fraig/fraig.h
@@ -0,0 +1,194 @@
+/**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__
+
+////////////////////////////////////////////////////////////////////////
+/// INCLUDES ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// 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;
+
+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 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)
+};
+
+////////////////////////////////////////////////////////////////////////
+/// GLOBAL VARIABLES ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// MACRO DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+// macros working with complemented attributes of the nodes
+#define Fraig_IsComplement(p) (((int)((long) (p) & 01)))
+#define Fraig_Regular(p) ((Fraig_Node_t *)((unsigned)(p) & ~01))
+#define Fraig_Not(p) ((Fraig_Node_t *)((long)(p) ^ 01))
+#define Fraig_NotCond(p,c) ((Fraig_Node_t *)((long)(p) ^ (c)))
+
+// these are currently not used
+#define Fraig_Ref(p)
+#define Fraig_Deref(p)
+#define Fraig_RecursiveDeref(p,c)
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/*=== 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 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 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 Fraig_ParamsSetDefault( 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 );
+
+/*=== 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 );
+extern int Fraig_NodeIsEquivalent( Fraig_Man_t * p, Fraig_Node_t * pOld, Fraig_Node_t * pNew, int nBTLimit );
+extern void Fraig_ManProveMiter( Fraig_Man_t * p );
+
+/*=== 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 ///
+////////////////////////////////////////////////////////////////////////
+#endif
diff --git a/src/sat/fraig/fraigApi.c b/src/sat/fraig/fraigApi.c
new file mode 100644
index 00000000..d60c7168
--- /dev/null
+++ b/src/sat/fraig/fraigApi.c
@@ -0,0 +1,280 @@
+/**CFile****************************************************************
+
+ FileName [fraigAccess.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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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; }
+
+/**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; }
+
+/**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 node.]
+
+ Description [This procedure should be called to create the constant
+ node and the PI nodes first.]
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+void Fraig_ManSetPo( Fraig_Man_t * p, Fraig_Node_t * pNode )
+{
+// assert( pNode->fNodePo == 0 );
+ // internal node may be a PO two times
+ 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..ae20531d
--- /dev/null
+++ b/src/sat/fraig/fraigCanon.c
@@ -0,0 +1,216 @@
+/**CFile****************************************************************
+
+ FileName [fraigAnd.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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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 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
+ if ( Fraig_NodeIsEquivalent( pMan, pNodeOld, pNodeNew, pMan->nBTLimit ) )
+ {
+ // 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/fraigFanout.c b/src/sat/fraig/fraigFanout.c
new file mode 100644
index 00000000..b44bacd7
--- /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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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..0a950aba
--- /dev/null
+++ b/src/sat/fraig/fraigFeed.c
@@ -0,0 +1,772 @@
+/**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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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;
+
+ 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 );
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/fraig/fraigInt.h b/src/sat/fraig/fraigInt.h
new file mode 100644
index 00000000..1139bdc0
--- /dev/null
+++ b/src/sat/fraig/fraigInt.h
@@ -0,0 +1,442 @@
+/**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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+// 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)/32 + (((n)%32) > 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 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
+
+ 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
+ // 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
+
+ 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 fFeedUse : 1; // the presence of the variable in the feedback
+ unsigned fFeedVal : 1; // the value of the variable in the feedback
+ unsigned nFanouts : 2; // the indicator of fanouts (none, one, or many)
+ unsigned nOnes : 22; // 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
+// Fraig_Sims_t * pSimsR; // the random simulation info
+// Fraig_Sims_t * pSimsD; // the systematic simulation info
+ 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_
+{
+ Fraig_Node_t ** pArray; // the array of nodes
+ int nSize; // the number of entries in the array
+ int nCap; // the number of allocated entries
+};
+
+// 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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/*=== 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 );
+/*=== 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 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 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 );
+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 );
+/*=== fraigVec.c ===============================================================*/
+extern void Fraig_NodeVecSortByRefCount( Fraig_NodeVec_t * p );
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+#endif
diff --git a/src/sat/fraig/fraigMan.c b/src/sat/fraig/fraigMan.c
new file mode 100644
index 00000000..d41f5d0b
--- /dev/null
+++ b/src/sat/fraig/fraigMan.c
@@ -0,0 +1,237 @@
+/**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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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 )
+{
+ 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->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;
+}
+
+/**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 );
+
+ // 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->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
+
+ // 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 );
+
+ 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 [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. Zero = %d.\n",
+ p->nSatProof, p->nSatCounter, p->nSatFails, 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 );
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
diff --git a/src/sat/fraig/fraigMem.c b/src/sat/fraig/fraigMem.c
new file mode 100644
index 00000000..dbf42da4
--- /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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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;
+
+ // delocate 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..9da9b88c
--- /dev/null
+++ b/src/sat/fraig/fraigNode.c
@@ -0,0 +1,308 @@
+/**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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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;
+ // 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);
+
+ // 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..0f37a586
--- /dev/null
+++ b/src/sat/fraig/fraigPrime.c
@@ -0,0 +1,142 @@
+/**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,000 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 };
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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..ba22cfad
--- /dev/null
+++ b/src/sat/fraig/fraigSat.c
@@ -0,0 +1,1085 @@
+/**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"
+
+////////////////////////////////////////////////////////////////////////
+/// 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 );
+
+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.
+
+////////////////////////////////////////////////////////////////////////
+/// FUNCTION DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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 )
+{
+ if ( pNode1 == pNode2 )
+ return 1;
+ if ( pNode1 == Fraig_Not(pNode2) )
+ return 0;
+ return Fraig_NodeIsEquivalent( p, Fraig_Regular(pNode1), Fraig_Regular(pNode2), nBTLimit );
+}
+
+/**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 ) )
+ {
+ if ( Fraig_IsComplement(p->vOutputs->pArray[i]) )
+ 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 [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 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->nSatCalls++;
+
+ // make sure the solver is allocated and has enough variables
+ if ( p->pSat == NULL )
+ {
+ extern int timeSelect;
+ extern int timeAssign;
+ // 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;
+ }
+ // make sure the SAT solver has enough variables
+ for ( i = Msat_SolverReadVarNum(p->pSat); i < p->vNodes->nSize; i++ )
+ Msat_SolverAddVar( p->pSat );
+
+
+
+/*
+ {
+ 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" );
+ }
+*/
+
+
+ // 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 );
+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 );
+ p->nSatCounter++;
+ return 0;
+ }
+ else // if ( RetValue1 == MSAT_UNKNOWN )
+ {
+p->time3 += clock() - clk;
+ p->nSatFails++;
+ 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 );
+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++;
+ return 0;
+ }
+ else // if ( RetValue1 == MSAT_UNKNOWN )
+ {
+p->time3 += clock() - clk;
+ p->nSatFails++;
+ return 0;
+ }
+
+ // return SAT proof
+ p->nSatProof++;
+ 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 )
+ {
+ extern int timeSelect;
+ extern int timeAssign;
+ // 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;
+ }
+ // make sure the SAT solver has enough variables
+ for ( i = Msat_SolverReadVarNum(p->pSat); i < p->vNodes->nSize; i++ )
+ Msat_SolverAddVar( p->pSat );
+
+
+/*
+ {
+ 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" );
+ }
+*/
+
+
+ // 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 );
+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 []
+
+***********************************************************************/
+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 );
+ }
+ 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 );
+}
+
+
+
+
+
+/**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 );
+}
+
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/fraig/fraigTable.c b/src/sat/fraig/fraigTable.c
new file mode 100644
index 00000000..5318c41e
--- /dev/null
+++ b/src/sat/fraig/fraigTable.c
@@ -0,0 +1,596 @@
+/**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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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 [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..6b7431f2
--- /dev/null
+++ b/src/sat/fraig/fraigUtil.c
@@ -0,0 +1,969 @@
+/**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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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;
+}
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+
+
diff --git a/src/sat/fraig/fraigVec.c b/src/sat/fraig/fraigVec.c
new file mode 100644
index 00000000..2e2603b0
--- /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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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..21ddcb81
--- /dev/null
+++ b/src/sat/msat/msat.h
@@ -0,0 +1,160 @@
+/**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__
+
+////////////////////////////////////////////////////////////////////////
+/// 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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// 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 );
+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 );
+// 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_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 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 );
+// 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 );
+
+////////////////////////////////////////////////////////////////////////
+/// END OF FILE ///
+////////////////////////////////////////////////////////////////////////
+#endif
diff --git a/src/sat/msat/msatActivity.c b/src/sat/msat/msatActivity.c
new file mode 100644
index 00000000..c9a518ce
--- /dev/null
+++ b/src/sat/msat/msatActivity.c
@@ -0,0 +1,158 @@
+/**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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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);
+ if ( (p->pdActivity[Var] += p->dVarInc) > 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..dc39bee6
--- /dev/null
+++ b/src/sat/msat/msatClause.c
@@ -0,0 +1,524 @@
+/**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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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
+ 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] );
+ }
+ }
+
+ // 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..7c24619f
--- /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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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..037616f6
--- /dev/null
+++ b/src/sat/msat/msatInt.h
@@ -0,0 +1,304 @@
+/**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.
+ 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
+
+ // 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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////////
+/// 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..2d178094
--- /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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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;
+
+ // delocate 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..ca034233
--- /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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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..6067b40f
--- /dev/null
+++ b/src/sat/msat/msatOrderJ.c
@@ -0,0 +1,466 @@
+/**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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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;
+
+ // 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;
+ }
+
+ // 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 ( 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;
+ }
+ 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 );
+
+ 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->pNext;
+ // 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..c12cc75d
--- /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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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..b8e585a4
--- /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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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..ba506993
--- /dev/null
+++ b/src/sat/msat/msatSolverApi.c
@@ -0,0 +1,488 @@
+/**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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Simple SAT solver APIs.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+int Msat_SolverReadVarNum( Msat_Solver_t * p ) { return p->nVars; }
+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 p->nBackTracks; }
+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); }
+
+/**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 );
+ for ( i = 0; i < p->nVarsAlloc; i++ )
+ p->pdActivity[i] = 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 );
+ for ( i = nVarsAllocOld; i < p->nVarsAlloc; i++ )
+ p->pdActivity[i] = 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 );
+ 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..b8d9f328
--- /dev/null
+++ b/src/sat/msat/msatSolverCore.c
@@ -0,0 +1,187 @@
+/**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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**Function*************************************************************
+
+ Synopsis [Adds one variable to the solver.]
+
+ Description []
+
+ SideEffects []
+
+ SeeAlso []
+
+***********************************************************************/
+bool Msat_SolverAddVar( Msat_Solver_t * p )
+{
+ if ( p->nVars == p->nVarsAlloc )
+ Msat_SolverResize( p, 2 * p->nVarsAlloc );
+ 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 )
+{
+ Msat_SearchParams_t Params = { 0.95, 0.999 };
+ double nConflictsLimit, nLearnedLimit;
+ Msat_Type_t Status;
+ int64 nConflictsOld = p->Stats.nConflicts;
+ int64 nDecisionsOld = p->Stats.nDecisions;
+
+ 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 )
+ break;
+ }
+ Msat_SolverCancelUntil( p, 0 );
+ p->nBackTracks = (int)p->Stats.nConflicts - p->nBackTracks;
+ 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..f17595a7
--- /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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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..13a0c403
--- /dev/null
+++ b/src/sat/msat/msatSolverSearch.c
@@ -0,0 +1,623 @@
+/**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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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;
+
+ assert( Msat_SolverReadDecisionLevel(p) == p->nLevelRoot );
+ p->Stats.nStarts++;
+ p->dVarDecay = 1 / pPars->dVarDecay;
+ p->dClaDecay = 1 / pPars->dClaDecay;
+
+ 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 && nConfs > 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..2198d303
--- /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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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..951969cf
--- /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 DEFITIONS ///
+////////////////////////////////////////////////////////////////////////
+
+/**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 ///
+////////////////////////////////////////////////////////////////////////
+
+