diff --git a/Makefile b/Makefile index 2da81734b..a546f65fd 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ MODULES := \ src/opt/cut src/opt/fxu src/opt/fxch src/opt/rwr src/opt/mfs src/opt/sim \ src/opt/ret src/opt/fret src/opt/res src/opt/lpk src/opt/nwk src/opt/rwt src/opt/rar \ src/opt/cgt src/opt/csw src/opt/dar src/opt/dau src/opt/dsc src/opt/sfm src/opt/sbd \ - src/sat/bsat src/sat/xsat src/sat/satoko src/sat/csat src/sat/msat src/sat/psat src/sat/cnf src/sat/bmc src/sat/glucose src/sat/glucose2 src/sat/kissat \ + src/sat/bsat src/sat/xsat src/sat/satoko src/sat/csat src/sat/msat src/sat/psat src/sat/cnf src/sat/bmc src/sat/glucose src/sat/glucose2 src/sat/kissat src/sat/cadical \ src/bool/bdc src/bool/deco src/bool/dec src/bool/kit src/bool/lucky \ src/bool/rsb src/bool/rpo \ src/proof/pdr src/proof/abs src/proof/live src/proof/ssc src/proof/int \ diff --git a/abclib.dsp b/abclib.dsp index 7f6af6f81..921030b34 100644 --- a/abclib.dsp +++ b/abclib.dsp @@ -2886,6 +2886,374 @@ SOURCE=.\src\sat\kissat\watch.c SOURCE=.\src\sat\kissat\weaken.c # End Source File # End Group +# Begin Group "cadical" + +# PROP Default_Filter "" +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_kitten.c +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_analyze.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_arena.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_assume.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_averages.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_backtrack.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_backward.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_bins.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_block.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_ccadical.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_checker.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_clause.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_collect.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_compact.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_condition.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_config.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_congruence.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_constrain.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_contract.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_cover.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_decide.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_decompose.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_deduplicate.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_definition.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_drattracer.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_elim.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_elimfast.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_ema.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_extend.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_external.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_external_propagate.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_factor.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_file.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_flags.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_flip.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_format.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_frattracer.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_gates.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_idruptracer.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_instantiate.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_internal.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_ipasir.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_lidruptracer.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_limit.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_logging.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_lookahead.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_lratchecker.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_lrattracer.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_lucky.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_message.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_minimize.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_occs.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_options.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_parse.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_phases.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_probe.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_profile.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_proof.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_propagate.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_queue.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_random.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_reap.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_reduce.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_rephase.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_report.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_resources.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_restart.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_restore.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_score.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_shrink.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_signal.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_solution.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_solver.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_stable.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_stats.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_subsume.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_sweep.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_terminal.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_ternary.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_tier.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_transred.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_unstable.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_util.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_var.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_veripbtracer.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_version.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_vivify.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_walk.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadical_watch.cpp +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadicalSolver.c +# End Source File +# Begin Source File + +SOURCE=.\src\sat\cadical\cadicalTest.c +# End Source File +# End Group # End Group # Begin Group "opt" diff --git a/src/base/abci/abc.c b/src/base/abci/abc.c index 0cd356d36..d3131c705 100644 --- a/src/base/abci/abc.c +++ b/src/base/abci/abc.c @@ -56048,6 +56048,9 @@ int Abc_CommandAbc9Test( Abc_Frame_t * pAbc, int argc, char ** argv ) } } + extern void cadical_solver_test(); + cadical_solver_test(); + return 0; extern void kissat_solver_test(); kissat_solver_test(); return 0; diff --git a/src/sat/cadical/arena.hpp b/src/sat/cadical/arena.hpp index 6dbd51121..522fce36e 100644 --- a/src/sat/cadical/arena.hpp +++ b/src/sat/cadical/arena.hpp @@ -1,6 +1,10 @@ #ifndef _arena_hpp_INCLUDED #define _arena_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // This memory allocation arena provides fixed size pre-allocated memory for @@ -36,7 +40,7 @@ namespace CaDiCaL { // q1 = arena.copy (p1, bytes1); // ... // qn = arena.copy (pn, bytesn); -// assert (bytes1 + ... + bytesn <= bytes); +// CADICAL_assert (bytes1 + ... + bytesn <= bytes); // arena.swap (); // ... // if (!arena.contains (q)) delete q; @@ -45,7 +49,7 @@ namespace CaDiCaL { // q1 = arena.copy (p1, bytes1); // ... // qn = arena.copy (pn, bytesn); -// assert (bytes1 + ... + bytesn <= bytes); +// CADICAL_assert (bytes1 + ... + bytesn <= bytes); // arena.swap (); // ... // @@ -88,7 +92,7 @@ public: char *copy (const char *p, size_t bytes) { char *res = to.top; to.top += bytes; - assert (to.top <= to.end); + CADICAL_assert (to.top <= to.end); memcpy (res, p, bytes); return res; } @@ -102,4 +106,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/averages.hpp b/src/sat/cadical/averages.hpp index 9f259509d..3ebdcddfa 100644 --- a/src/sat/cadical/averages.hpp +++ b/src/sat/cadical/averages.hpp @@ -1,8 +1,12 @@ #ifndef _averages_hpp_INCLUDED #define _averages_hpp_INCLUDED +#include "global.h" + #include "ema.hpp" // alphabetically after 'averages.hpp' +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Averages { @@ -34,4 +38,6 @@ struct Averages { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/bins.hpp b/src/sat/cadical/bins.hpp index ec0f0f1dc..aeefeceeb 100644 --- a/src/sat/cadical/bins.hpp +++ b/src/sat/cadical/bins.hpp @@ -1,8 +1,12 @@ #ifndef _bins_hpp_INCLUDED #define _bins_hpp_INCLUDED +#include "global.h" + #include "util.hpp" // Alphabetically after 'bins'. +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { using namespace std; @@ -19,4 +23,6 @@ inline void erase_bins (Bins &bs) { erase_vector (bs); } } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/block.hpp b/src/sat/cadical/block.hpp index c56707563..202355f82 100644 --- a/src/sat/cadical/block.hpp +++ b/src/sat/cadical/block.hpp @@ -1,8 +1,12 @@ #ifndef _block_hpp_INCLUDED #define _block_hpp_INCLUDED +#include "global.h" + #include "heap.hpp" // Alphabetically after 'block.hpp'. +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Internal; @@ -34,4 +38,6 @@ class Blocker { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/cadical.hpp b/src/sat/cadical/cadical.hpp index 8dacaf3dc..c9ed41332 100644 --- a/src/sat/cadical/cadical.hpp +++ b/src/sat/cadical/cadical.hpp @@ -1,10 +1,14 @@ #ifndef _cadical_hpp_INCLUDED #define _cadical_hpp_INCLUDED +#include "global.h" + #include #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { /*========================================================================*/ @@ -49,13 +53,13 @@ enum Status { // solver->add (-TIE), solver->add (-SHIRT), solver->add (0); // // int res = solver->solve (); // Solve instance. -// assert (res == 10); // Check it is 'SATISFIABLE'. +// CADICAL_assert (res == 10); // Check it is 'SATISFIABLE'. // // res = solver->val (TIE); // Obtain assignment of 'TIE'. -// assert (res < 0); // Check 'TIE' assigned to 'false'. +// CADICAL_assert (res < 0); // Check 'TIE' assigned to 'false'. // // res = solver->val (SHIRT); // Obtain assignment of 'SHIRT'. -// assert (res > 0); // Check 'SHIRT' assigned to 'true'. +// CADICAL_assert (res > 0); // Check 'SHIRT' assigned to 'true'. // // // ------------------------------------------------------------------ // // Incrementally solve again under one assumption. @@ -63,13 +67,13 @@ enum Status { // solver->assume (TIE); // Now force 'TIE' to true. // // res = solver->solve (); // Solve again incrementally. -// assert (res == 20); // Check it is 'UNSATISFIABLE'. +// CADICAL_assert (res == 20); // Check it is 'UNSATISFIABLE'. // // res = solver->failed (TIE); // Check 'TIE' responsible. -// assert (res); // Yes, 'TIE' in core. +// CADICAL_assert (res); // Yes, 'TIE' in core. // // res = solver->failed (SHIRT); // Check 'SHIRT' responsible. -// assert (!res); // No, 'SHIRT' not in core. +// CADICAL_assert (!res); // No, 'SHIRT' not in core. // // // ------------------------------------------------------------------ // // Incrementally solve once more under another assumption. @@ -77,13 +81,13 @@ enum Status { // solver->assume (-SHIRT); // Now force 'SHIRT' to false. // // res = solver->solve (); // Solve again incrementally. -// assert (res == 20); // Check it is 'UNSATISFIABLE'. +// CADICAL_assert (res == 20); // Check it is 'UNSATISFIABLE'. // // res = solver->failed (TIE); // Check 'TIE' responsible. -// assert (!res); // No, 'TIE' not in core. +// CADICAL_assert (!res); // No, 'TIE' not in core. // // res = solver->failed (-SHIRT); // Check '!SHIRT' responsible. -// assert (res); // Yes, '!SHIRT' in core. +// CADICAL_assert (res); // Yes, '!SHIRT' in core. // // // ------------------------------------------------------------------ // @@ -595,7 +599,7 @@ public: // int reserve_difference (int number_of_vars); -#ifndef NTRACING +#ifndef CADICAL_NTRACING //------------------------------------------------------------------------ // This function can be used to write API calls to a file. The same // format is used which 'mobical' can read, execute and also shrink @@ -1029,7 +1033,7 @@ private: friend class Testing; // Access to 'internal' for testing only! -#ifndef NTRACING +#ifndef CADICAL_NTRACING // The API calls to the solver can be traced by setting the environment // variable 'CADICAL_API_TRACE' to point to the path of a file to which // API calls are written. The same format is used which 'mobical' can @@ -1092,10 +1096,13 @@ private: // // TODO: support for other compilers (beside 'gcc' and 'clang'). + /* #define CADICAL_ATTRIBUTE_FORMAT(FORMAT_POSITION, \ VARIADIC_ARGUMENT_POSITION) \ __attribute__ ((format (PRINTF_FORMAT, FORMAT_POSITION, \ VARIADIC_ARGUMENT_POSITION))) + */ +#define CADICAL_ATTRIBUTE_FORMAT(FORMAT_POSITION, VARIADIC_ARGUMENT_POSITION) // Messages in a common style. // @@ -1339,4 +1346,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/cadicalSolver.c b/src/sat/cadical/cadicalSolver.c new file mode 100644 index 000000000..9caea6605 --- /dev/null +++ b/src/sat/cadical/cadicalSolver.c @@ -0,0 +1,305 @@ +/**CFile**************************************************************** + + FileName [cadicalSolver.c] + + SystemName [ABC: Logic synthesis and verification system.] + + PackageName [SAT solver CaDiCaL by Armin Biere, University of Freiburg] + + Synopsis [https://github.com/arminbiere/cadical] + + Author [Integrated into ABC by Yukio Miyasaka] + + Affiliation [UC Berkeley] + + Date [Ver. 1.0. Started - June 20, 2005.] + + Revision [$Id: cadicalSolver.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $] + +***********************************************************************/ + +#include "ccadical.h" +#include "cadicalSolver.h" + +ABC_NAMESPACE_IMPL_START + +//////////////////////////////////////////////////////////////////////// +/// DECLARATIONS /// +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +/// FUNCTION DEFINITIONS /// +//////////////////////////////////////////////////////////////////////// + +/**Function************************************************************* + + Synopsis [allocate solver] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +cadical_solver* cadical_solver_new(void) { + cadical_solver* s = (cadical_solver*)malloc(sizeof(cadical_solver)); + s->p = (void*)ccadical_init(); + s->nVars = 0; + s->vAssumptions = NULL; + s->vCore = NULL; + return s; +} + +/**Function************************************************************* + + Synopsis [delete solver] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +void cadical_solver_delete(cadical_solver* s) { + ccadical_release((CCaDiCaL*)s->p); + if(s->vAssumptions) { + Vec_IntFree(s->vAssumptions); + } + if(s->vCore) { + Vec_IntFree(s->vCore); + } + free(s); +} + +/**Function************************************************************* + + Synopsis [add clause] + + Description [cadical takes x and -x as a literal for a variable x > 0, + where 0 is an indicator of the end of a clause. + since variables start from 0 in abc, a variable v is + translated into v + 1 in cadical.] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int cadical_solver_addclause(cadical_solver* s, int* begin, int* end) { + for(;begin != end; begin++) { + if(*begin & 1) { + ccadical_add((CCaDiCaL*)s->p, -(1 + ((*begin) >> 1))); + } else { + ccadical_add((CCaDiCaL*)s->p, 1 + ((*begin) >> 1) ); + } + } + ccadical_add((CCaDiCaL*)s->p, 0); + return !ccadical_is_inconsistent((CCaDiCaL*)s->p); +} + +/**Function************************************************************* + + Synopsis [solve with resource limits] + + Description [assumptions and inspection limits are not supported.] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int cadical_solver_solve(cadical_solver* s, int* begin, int* end, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, ABC_INT64_T nConfLimitGlobal, ABC_INT64_T nInsLimitGlobal) { + // inspection limits are not supported + assert(nInsLimit == 0); + assert(nInsLimitGlobal == 0); + // set conflict limits + if(nConfLimit) + ccadical_limit((CCaDiCaL*)s->p, "conflicts", nConfLimit); + if(nConfLimitGlobal && (nConfLimit == 0 || nConfLimit > nConfLimitGlobal)) + ccadical_limit((CCaDiCaL*)s->p, "conflicts", nConfLimitGlobal); + // assumptions + if(begin != end) { + // save + if(s->vAssumptions == NULL) { + s->vAssumptions = Vec_IntAllocArrayCopy(begin, end - begin); + } else { + Vec_IntClear(s->vAssumptions); + Vec_IntGrow(s->vAssumptions, end - begin); + Vec_IntPushArray(s->vAssumptions, begin, end - begin); + } + // assume + for(;begin != end; begin++) { + if(*begin & 1) { + ccadical_assume((CCaDiCaL*)s->p, -(1 + ((*begin) >> 1))); + } else { + ccadical_assume((CCaDiCaL*)s->p, 1 + ((*begin) >> 1) ); + } + } + } + // solve + int res = ccadical_solve((CCaDiCaL*)s->p); + // translate this cadical return value into a corresponding ABC status value + switch(res) { + case 0: // UNDETERMINED + return 0; + case 10: // SATISFIABLE + return 1; + case 20: // UNSATISFIABLE + return -1; + default: + assert(0); + } + return 0; +} + +/**Function************************************************************* + + Synopsis [get unsat core] + + Description [following minisat, return number of literals in core, + which consists of responsible assumptions, negated. + array will be freed by solver.] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int cadical_solver_final(cadical_solver* s, int** ppArray) { + int v, i; + if(s->vCore == NULL) { + s->vCore = Vec_IntAlloc(Vec_IntSize(s->vAssumptions)); + } else { + Vec_IntClear(s->vCore); + } + Vec_IntForEachEntry(s->vAssumptions, v, i) { + int failed; + if(v & 1) { + failed = ccadical_failed((CCaDiCaL*)s->p, -(1 + (v >> 1))); + } else { + failed = ccadical_failed((CCaDiCaL*)s->p, 1 + (v >> 1) ); + } + if(failed) { + Vec_IntPush(s->vCore, Abc_LitNot(v)); + } + } + *ppArray = Vec_IntArray(s->vCore); + return Vec_IntSize(s->vCore); +} + +/**Function************************************************************* + + Synopsis [get number of variables] + + Description [emulated using "nVars".] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int cadical_solver_nvars(cadical_solver* s) { + return s->nVars; +} + +/**Function************************************************************* + + Synopsis [add new variable] + + Description [emulated using "nVars".] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int cadical_solver_addvar(cadical_solver* s) { + return s->nVars++; +} + +/**Function************************************************************* + + Synopsis [set number of variables] + + Description [not only emulate with "nVars" but also reserve memory.] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +void cadical_solver_setnvars(cadical_solver* s,int n) { + s->nVars = n; + ccadical_reserve((CCaDiCaL*)s->p, n); +} + +/**Function************************************************************* + + Synopsis [get value of variable] + + Description [cadical returns x (true) or -x (false) for a variable x. + note a variable v was translated into v + 1 in cadical.] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +int cadical_solver_get_var_value(cadical_solver* s, int v) { + return ccadical_val((CCaDiCaL*)s->p, v + 1) > 0; +} + + +/**Function************************************************************* + + Synopsis [Solves the given CNF using cadical.] + + Description [] + + SideEffects [] + + SeeAlso [] + +***********************************************************************/ +Vec_Int_t * cadical_solve_cnf( Cnf_Dat_t * pCnf, char * pArgs, int nConfs, int nTimeLimit, int fSat, int fUnsat, int fPrintCex, int fVerbose ) +{ + abctime clk = Abc_Clock(); + Vec_Int_t * vRes = NULL; + int i, * pBeg, * pEnd, RetValue; + if ( fVerbose ) + printf( "CNF stats: Vars = %6d. Clauses = %7d. Literals = %8d. ", pCnf->nVars, pCnf->nClauses, pCnf->nLiterals ); + cadical_solver *pSat = cadical_solver_new(); + cadical_solver_setnvars(pSat, pCnf->nVars); + assert(cadical_solver_nvars(pSat) == pCnf->nVars); + Cnf_CnfForClause( pCnf, pBeg, pEnd, i ) { + if ( !cadical_solver_addclause(pSat, pBeg, pEnd) ) + { + assert( 0 ); // if it happens, can return 1 (unsatisfiable) + return NULL; + } + } + RetValue = cadical_solver_solve(pSat, NULL, NULL, 0, 0, 0, 0); + if ( RetValue == 1 ) + printf( "Result: Satisfiable. " ); + else if ( RetValue == -1 ) + printf( "Result: Unsatisfiable. " ); + else + printf( "Result: Undecided. " ); + if ( RetValue == 1 ) { + vRes = Vec_IntAlloc( pCnf->nVars ); + for ( i = 0; i < pCnf->nVars; i++ ) + Vec_IntPush( vRes, cadical_solver_get_var_value(pSat, i) ); + } + cadical_solver_delete(pSat); + Abc_PrintTime( 1, "Time", Abc_Clock() - clk ); + return vRes; +} + + +//////////////////////////////////////////////////////////////////////// +/// END OF FILE /// +//////////////////////////////////////////////////////////////////////// + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/cadicalSolver.h b/src/sat/cadical/cadicalSolver.h new file mode 100644 index 000000000..0192ada3c --- /dev/null +++ b/src/sat/cadical/cadicalSolver.h @@ -0,0 +1,76 @@ +/**CFile**************************************************************** + + FileName [cadicalSolver.h] + + SystemName [ABC: Logic synthesis and verification system.] + + PackageName [SAT solver CaDiCaL by Armin Biere, University of Freiburg] + + Synopsis [https://github.com/arminbiere/cadical] + + Author [Integrated into ABC by Yukio Miyasaka] + + Affiliation [UC Berkeley] + + Date [Ver. 1.0. Started - June 20, 2005.] + + Revision [$Id: cadicalSolver.h,v 1.00 2005/06/20 00:00:00 alanmi Exp $] + +***********************************************************************/ + +#ifndef ABC_SAT_CADICAL_SOLVER_H_ +#define ABC_SAT_CADICAL_SOLVER_H_ + +//////////////////////////////////////////////////////////////////////// +/// INCLUDES /// +//////////////////////////////////////////////////////////////////////// + +#include "aig/gia/gia.h" +#include "sat/cnf/cnf.h" + +//////////////////////////////////////////////////////////////////////// +/// PARAMETERS /// +//////////////////////////////////////////////////////////////////////// + +ABC_NAMESPACE_HEADER_START + +//////////////////////////////////////////////////////////////////////// +/// BASIC TYPES /// +//////////////////////////////////////////////////////////////////////// + +typedef struct cadical_solver_ cadical_solver; +struct cadical_solver_ +{ + void* p; + int nVars; + Vec_Int_t* vAssumptions; + Vec_Int_t* vCore; +}; + + +//////////////////////////////////////////////////////////////////////// +/// MACRO DEFINITIONS /// +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +/// FUNCTION DECLARATIONS /// +//////////////////////////////////////////////////////////////////////// + +extern cadical_solver* cadical_solver_new(void); +extern void cadical_solver_delete(cadical_solver* s); +extern int cadical_solver_addclause(cadical_solver* s, int* begin, int* end); +extern int cadical_solver_solve(cadical_solver* s, int* begin, int* end, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, ABC_INT64_T nConfLimitGlobal, ABC_INT64_T nInsLimitGlobal); +extern int cadical_solver_final(cadical_solver* s, int** ppArray); +extern int cadical_solver_nvars(cadical_solver* s); +extern int cadical_solver_addvar(cadical_solver* s); +extern void cadical_solver_setnvars(cadical_solver* s,int n); +extern int cadical_solver_get_var_value(cadical_solver* s, int v); +extern Vec_Int_t * cadical_solve_cnf( Cnf_Dat_t * pCnf, char * pArgs, int nConfs, int nTimeLimit, int fSat, int fUnsat, int fPrintCex, int fVerbose ); + +ABC_NAMESPACE_HEADER_END + +#endif + +//////////////////////////////////////////////////////////////////////// +/// END OF FILE /// +//////////////////////////////////////////////////////////////////////// diff --git a/src/sat/cadical/cadicalTest.c b/src/sat/cadical/cadicalTest.c new file mode 100644 index 000000000..f3919160b --- /dev/null +++ b/src/sat/cadical/cadicalTest.c @@ -0,0 +1,210 @@ +/**CFile**************************************************************** + + FileName [cadicalTest.c] + + SystemName [ABC: Logic synthesis and verification system.] + + PackageName [] + + Synopsis [] + + Author [Alan Mishchenko] + + Affiliation [UC Berkeley] + + Date [Ver. 1.0. Started - June 20, 2005.] + + Revision [$Id: cadicalTest.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $] + +***********************************************************************/ + +#include "cadicalSolver.h" + +ABC_NAMESPACE_IMPL_START + +//////////////////////////////////////////////////////////////////////// +/// DECLARATIONS /// +//////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////// +/// FUNCTION DEFINITIONS /// +//////////////////////////////////////////////////////////////////////// +void cadical_solver_test() { + int RetValue; + int Lits[3]; + // test 1 + { + cadical_solver *pSat = cadical_solver_new(); + int a = cadical_solver_addvar(pSat); + int b = cadical_solver_addvar(pSat); + int c = cadical_solver_addvar(pSat); + assert(cadical_solver_nvars(pSat) == 3); + Lits[0] = Abc_Var2Lit(a, 0); + Lits[1] = Abc_Var2Lit(b, 0); + Lits[2] = Abc_Var2Lit(c, 0); + printf("adding (a, b, c)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 3); + assert(RetValue); + Lits[0] = Abc_Var2Lit(a, 0); + Lits[1] = Abc_Var2Lit(b, 1); + printf("adding (a, !b)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 2); + assert(RetValue); + Lits[0] = Abc_Var2Lit(a, 1); + printf("adding (!a)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 1); + assert(RetValue); + RetValue = cadical_solver_solve(pSat, NULL, NULL, 0, 0, 0, 0); + printf("solved: %d\n", RetValue); + assert(RetValue == 1); + int a_val = cadical_solver_get_var_value(pSat, a); + int b_val = cadical_solver_get_var_value(pSat, b); + int c_val = cadical_solver_get_var_value(pSat, c); + printf("a = %d, b = %d, c = %d\n", a_val, b_val, c_val); + assert(a_val == 0); + assert(b_val == 0); + assert(c_val == 1); + cadical_solver_delete(pSat); + printf("test 1 passed\n"); + } + // test 2 + { + cadical_solver *pSat = cadical_solver_new(); + cadical_solver_setnvars(pSat, 2); + assert(cadical_solver_nvars(pSat) == 2); + Lits[0] = Abc_Var2Lit(0, 0); + Lits[1] = Abc_Var2Lit(1, 0); + printf("adding (x0, x1)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 2); + assert(RetValue); + Lits[0] = Abc_Var2Lit(0, 0); + Lits[1] = Abc_Var2Lit(1, 1); + printf("adding (x0, !x1)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 2); + assert(RetValue); + Lits[0] = Abc_Var2Lit(0, 1); + Lits[1] = Abc_Var2Lit(1, 1); + printf("adding (!x0, !x1)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 2); + assert(RetValue); + RetValue = cadical_solver_solve(pSat, NULL, NULL, 0, 0, 0, 0); + printf("solved: %d\n", RetValue); + assert(RetValue == 1); + printf("x0 = %d, x1 = %d\n", cadical_solver_get_var_value(pSat, 0), cadical_solver_get_var_value(pSat, 1)); + assert(cadical_solver_get_var_value(pSat, 0) == 1); + assert(cadical_solver_get_var_value(pSat, 1) == 0); + cadical_solver_delete(pSat); + printf("test 2 passed\n"); + } + // test 3 + { + cadical_solver *pSat = cadical_solver_new(); + cadical_solver_setnvars(pSat, 3); + assert(cadical_solver_nvars(pSat) == 3); + Lits[0] = Abc_Var2Lit(0, 1); + Lits[1] = Abc_Var2Lit(1, 0); + Lits[2] = Abc_Var2Lit(2, 1); + printf("adding (!x0, x1, !x2)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 3); + assert(RetValue); + Lits[0] = Abc_Var2Lit(0, 0); + printf("adding (x0)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 1); + assert(RetValue); + Lits[0] = Abc_Var2Lit(1, 1); + printf("adding (!x1)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 1); + assert(RetValue); + Lits[0] = Abc_Var2Lit(2, 0); + printf("adding (x2)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 1); + RetValue = cadical_solver_solve(pSat, NULL, NULL, 0, 0, 0, 0); + printf("solved: %d\n", RetValue); + assert(RetValue == -1); + cadical_solver_delete(pSat); + printf("test 3 passed\n"); + } + // test 4 + { + cadical_solver *pSat = cadical_solver_new(); + cadical_solver_setnvars(pSat, 3); + assert(cadical_solver_nvars(pSat) == 3); + Lits[0] = Abc_Var2Lit(0, 1); + Lits[1] = Abc_Var2Lit(1, 0); + Lits[2] = Abc_Var2Lit(2, 1); + printf("adding (!x0, x1, !x2)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 3); + assert(RetValue); + Lits[0] = Abc_Var2Lit(0, 0); + printf("adding (x0)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 1); + assert(RetValue); + Lits[0] = Abc_Var2Lit(1, 1); + printf("assume (!x1)\n"); + RetValue = cadical_solver_solve(pSat, Lits, Lits + 1, 0, 0, 0, 0); + printf("solved: %d\n", RetValue); + assert(RetValue == 1); + printf("x0 = %d, x1 = %d, x2 = %d\n", cadical_solver_get_var_value(pSat, 0), cadical_solver_get_var_value(pSat, 1), cadical_solver_get_var_value(pSat, 2)); + assert(cadical_solver_get_var_value(pSat, 0) == 1); + assert(cadical_solver_get_var_value(pSat, 1) == 0); + assert(cadical_solver_get_var_value(pSat, 2) == 0); + cadical_solver_delete(pSat); + printf("test 4 passed\n"); + } + // test 5 + { + cadical_solver *pSat = cadical_solver_new(); + cadical_solver_setnvars(pSat, 3); + assert(cadical_solver_nvars(pSat) == 3); + Lits[0] = Abc_Var2Lit(0, 1); + Lits[1] = Abc_Var2Lit(1, 0); + Lits[2] = Abc_Var2Lit(2, 1); + printf("adding (!x0, x1, !x2)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 3); + assert(RetValue); + Lits[0] = Abc_Var2Lit(0, 1); + Lits[1] = Abc_Var2Lit(1, 1); + Lits[2] = Abc_Var2Lit(2, 1); + printf("adding (!x0, !x1, !x2)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 3); + assert(RetValue); + Lits[0] = Abc_Var2Lit(0, 0); + printf("adding (x0)\n"); + RetValue = cadical_solver_addclause(pSat, Lits, Lits + 1); + assert(RetValue); + Lits[0] = Abc_Var2Lit(1, 0); + Lits[1] = Abc_Var2Lit(2, 0); + printf("assume (x1, x2)\n"); + RetValue = cadical_solver_solve(pSat, Lits, Lits + 2, 0, 0, 0, 0); + printf("solved: %d\n", RetValue); + assert(RetValue == -1); + int *pCore; + int nSize = cadical_solver_final(pSat, &pCore); + printf("core: "); + for(int i = 0; i < nSize; i++) { + if(i) { + printf(", "); + } + if(Abc_LitIsCompl(pCore[i])) { + printf("!"); + } + printf("x%d", Abc_Lit2Var(pCore[i])); + } + printf("\n"); + int neg_x2_in_core = 0; + for(int i = 0; i < nSize; i++) { + if(Abc_LitIsCompl(pCore[i]) && Abc_Lit2Var(pCore[i]) == 2) { + neg_x2_in_core = 1; + } + } + assert(neg_x2_in_core); + cadical_solver_delete(pSat); + printf("test 5 passed\n"); + } +} + +//////////////////////////////////////////////////////////////////////// +/// END OF FILE /// +//////////////////////////////////////////////////////////////////////// + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/analyze.cpp b/src/sat/cadical/cadical_analyze.cpp similarity index 91% rename from src/sat/cadical/analyze.cpp rename to src/sat/cadical/cadical_analyze.cpp index 667d59633..74b67fb06 100644 --- a/src/sat/cadical/analyze.cpp +++ b/src/sat/cadical/cadical_analyze.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -13,7 +17,7 @@ namespace CaDiCaL { /*------------------------------------------------------------------------*/ void Internal::learn_empty_clause () { - assert (!unsat); + CADICAL_assert (!unsat); build_chain_for_empty (); LOG ("learned empty clause"); external->check_learned_empty_clause (); @@ -29,7 +33,7 @@ void Internal::learn_empty_clause () { } void Internal::learn_unit_clause (int lit) { - assert (!unsat); + CADICAL_assert (!unsat); LOG ("learned unit clause %d, stored at position %d", lit, vlit (lit)); external->check_learned_unit_clause (lit); int64_t id = ++clause_id; @@ -50,13 +54,13 @@ void Internal::learn_unit_clause (int lit) { // whether the 'queue.assigned' pointer has to be moved in 'unassign'. void Internal::bump_queue (int lit) { - assert (opts.bump); + CADICAL_assert (opts.bump); const int idx = vidx (lit); if (!links[idx].next) return; queue.dequeue (links, idx); queue.enqueue (links, idx); - assert (stats.bumped != INT64_MAX); + CADICAL_assert (stats.bumped != INT64_MAX); btab[idx] = ++stats.bumped; LOG ("moved to front variable %d and bumped to %" PRId64 "", idx, btab[idx]); @@ -74,7 +78,7 @@ void Internal::bump_queue (int lit) { // and simply put a hard limit here. It is less elegant but easy to port. static inline bool evsids_limit_hit (double score) { - assert (sizeof (score) == 8); // assume IEEE 754 64-bit double + CADICAL_assert (sizeof (score) == 8); // assume IEEE 754 64-bit double return score > 1e150; // MAX_DOUBLE is around 1.8e308 } @@ -92,7 +96,7 @@ void Internal::rescale_variable_scores () { } PHASE ("rescore", stats.rescored, "rescoring %d variable scores by 1/%g", max_var, divider); - assert (divider > 0); + CADICAL_assert (divider > 0); double factor = 1.0 / divider; for (auto idx : vars) stab[idx] *= factor; @@ -103,19 +107,19 @@ void Internal::rescale_variable_scores () { } void Internal::bump_variable_score (int lit) { - assert (opts.bump); + CADICAL_assert (opts.bump); int idx = vidx (lit); double old_score = score (idx); - assert (!evsids_limit_hit (old_score)); + CADICAL_assert (!evsids_limit_hit (old_score)); double new_score = old_score + score_inc; if (evsids_limit_hit (new_score)) { LOG ("bumping %g score of %d hits EVSIDS score limit", old_score, idx); rescale_variable_scores (); old_score = score (idx); - assert (!evsids_limit_hit (old_score)); + CADICAL_assert (!evsids_limit_hit (old_score)); new_score = old_score + score_inc; } - assert (!evsids_limit_hit (new_score)); + CADICAL_assert (!evsids_limit_hit (new_score)); LOG ("new %g score of %d", new_score, idx); score (idx) = new_score; if (scores.contains (idx)) @@ -135,8 +139,8 @@ void Internal::bump_variable (int lit) { // factor (if we are currently using scores). void Internal::bump_variable_score_inc () { - assert (use_scores ()); - assert (!evsids_limit_hit (score_inc)); + CADICAL_assert (use_scores ()); + CADICAL_assert (!evsids_limit_hit (score_inc)); double f = 1e3 / opts.scorefactor; double new_score_inc = score_inc * f; if (evsids_limit_hit (new_score_inc)) { @@ -145,7 +149,7 @@ void Internal::bump_variable_score_inc () { rescale_variable_scores (); new_score_inc = score_inc * f; } - assert (!evsids_limit_hit (new_score_inc)); + CADICAL_assert (!evsids_limit_hit (new_score_inc)); LOG ("bumped score increment from %g to %g with factor %g", score_inc, new_score_inc, f); score_inc = new_score_inc; @@ -174,7 +178,7 @@ struct analyze_bumped_smaller { void Internal::bump_variables () { - assert (opts.bump); + CADICAL_assert (opts.bump); START (bump); @@ -208,7 +212,7 @@ int Internal::recompute_glue (Clause *c) { const int64_t stamp = ++stats.recomputed; for (const auto &lit : *c) { int level = var (lit).level; - assert (gtab[level] <= stamp); + CADICAL_assert (gtab[level] <= stamp); if (gtab[level] == stamp) continue; gtab[level] = stamp; @@ -252,7 +256,7 @@ void Internal::bump_clause2 (Clause *c) { bump_clause (c); } inline void Internal::analyze_literal (int lit, int &open, int &resolvent_size, int &antecedent_size) { - assert (lit); + CADICAL_assert (lit); Var &v = var (lit); Flags &f = flags (lit); @@ -261,7 +265,7 @@ inline void Internal::analyze_literal (int lit, int &open, return; f.seen = true; unit_analyzed.push_back (lit); - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); int64_t id = unit_id (-lit); unit_chain.push_back (id); return; @@ -272,10 +276,10 @@ inline void Internal::analyze_literal (int lit, int &open, // before marking as seen, get reason and check for missed unit - assert (val (lit) < 0); - assert (v.level <= level); + CADICAL_assert (val (lit) < 0); + CADICAL_assert (v.level <= level); if (v.reason == external_reason) { - assert (!opts.exteagerreasons); + CADICAL_assert (!opts.exteagerreasons); v.reason = learn_external_reason_clause (-lit, 0, true); if (!v.reason) { // actually a unit --antecedent_size; @@ -284,10 +288,10 @@ inline void Internal::analyze_literal (int lit, int &open, return; f.seen = true; unit_analyzed.push_back (lit); - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); const unsigned uidx = vlit (-lit); int64_t id = unit_clauses (uidx); - assert (id); + CADICAL_assert (id); unit_chain.push_back (id); return; } @@ -296,7 +300,7 @@ inline void Internal::analyze_literal (int lit, int &open, f.seen = true; analyzed.push_back (lit); - assert (v.reason != external_reason); + CADICAL_assert (v.reason != external_reason); if (v.level < level) clause.push_back (lit); Level &l = control[v.level]; @@ -315,8 +319,8 @@ inline void Internal::analyze_literal (int lit, int &open, inline void Internal::analyze_reason (int lit, Clause *reason, int &open, int &resolvent_size, int &antecedent_size) { - assert (reason); - assert (reason != external_reason); + CADICAL_assert (reason); + CADICAL_assert (reason != external_reason); bump_clause (reason); if (lrat) lrat_chain.push_back (reason->id); @@ -339,8 +343,8 @@ inline void Internal::analyze_reason (int lit, Clause *reason, int &open, // 30 million propagations, which then is increased geometrically by 10%. inline bool Internal::bump_also_reason_literal (int lit) { - assert (lit); - assert (val (lit) < 0); + CADICAL_assert (lit); + CADICAL_assert (val (lit) < 0); Flags &f = flags (lit); if (f.seen) return false; @@ -357,10 +361,10 @@ inline bool Internal::bump_also_reason_literal (int lit) { inline void Internal::bump_also_reason_literals (int lit, int depth_limit, size_t analyzed_limit) { - assert (lit); - assert (depth_limit > 0); + CADICAL_assert (lit); + CADICAL_assert (depth_limit > 0); const Var &v = var (lit); - assert (val (lit)); + CADICAL_assert (val (lit)); if (!v.level) return; Clause *reason = v.reason; @@ -381,7 +385,7 @@ inline void Internal::bump_also_reason_literals (int lit, int depth_limit, } inline void Internal::bump_also_all_reason_literals () { - assert (opts.bump); + CADICAL_assert (opts.bump); if (!opts.bumpreason) return; if (averages.current.decisions > opts.bumpreasonrate) { @@ -395,7 +399,7 @@ inline void Internal::bump_also_all_reason_literals () { delay[stable].bumpreasons.limit--; return; } - assert (opts.bumpreasondepth > 0); + CADICAL_assert (opts.bumpreasondepth > 0); const int depth_limit = opts.bumpreasondepth + stable; size_t saved_analyzed = analyzed.size (); size_t analyzed_limit = saved_analyzed * opts.bumpreasonlimit; @@ -409,7 +413,7 @@ inline void Internal::bump_also_all_reason_literals () { for (size_t i = saved_analyzed; i != analyzed.size (); i++) { const int lit = analyzed[i]; Flags &f = flags (lit); - assert (f.seen); + CADICAL_assert (f.seen); f.seen = false; } delay[stable].bumpreasons.interval++; @@ -428,12 +432,12 @@ void Internal::clear_unit_analyzed_literals () { LOG ("clearing %zd unit analyzed literals", unit_analyzed.size ()); for (const auto &lit : unit_analyzed) { Flags &f = flags (lit); - assert (f.seen); - assert (!var (lit).level); + CADICAL_assert (f.seen); + CADICAL_assert (!var (lit).level); f.seen = false; - assert (!f.keep); - assert (!f.poison); - assert (!f.removable); + CADICAL_assert (!f.keep); + CADICAL_assert (!f.poison); + CADICAL_assert (!f.removable); } unit_analyzed.clear (); } @@ -442,11 +446,11 @@ void Internal::clear_analyzed_literals () { LOG ("clearing %zd analyzed literals", analyzed.size ()); for (const auto &lit : analyzed) { Flags &f = flags (lit); - assert (f.seen); + CADICAL_assert (f.seen); f.seen = false; - assert (!f.keep); - assert (!f.poison); - assert (!f.removable); + CADICAL_assert (!f.keep); + CADICAL_assert (!f.poison); + CADICAL_assert (!f.removable); } analyzed.clear (); #if 0 // to expensive, even for debugging mode @@ -454,7 +458,7 @@ void Internal::clear_analyzed_literals () { return; for (auto idx : vars) { Flags &f = flags (idx); - assert (!f.seen); + CADICAL_assert (!f.seen); } #endif } @@ -517,7 +521,7 @@ Clause *Internal::new_driving_clause (const int glue, int &jump) { } else { - assert (clause.size () > 1); + CADICAL_assert (clause.size () > 1); // We have to get the last assigned literals into the watch position. // Sorting all literals with respect to reverse assignment order is @@ -545,7 +549,7 @@ Clause *Internal::new_driving_clause (const int glue, int &jump) { // not have to fix the clause inline int Internal::otfs_find_backtrack_level (int &forced) { - assert (opts.otfs); + CADICAL_assert (opts.otfs); int res = 0; for (const auto &lit : *conflict) { @@ -570,8 +574,8 @@ inline int Internal::otfs_find_backtrack_level (int &forced) { inline int Internal::find_conflict_level (int &forced) { - assert (conflict); - assert (opts.chrono || opts.otfs || external_prop); + CADICAL_assert (conflict); + CADICAL_assert (opts.chrono || opts.otfs || external_prop); int res = 0, count = 0; @@ -649,7 +653,7 @@ inline int Internal::determine_actual_backtrack_level (int jump) { int res; - assert (level > jump); + CADICAL_assert (level > jump); if (!opts.chrono) { res = jump; @@ -693,7 +697,7 @@ inline int Internal::determine_actual_backtrack_level (int jump) { } LOG ("best variable bumped %" PRId64 "", bumped (best_idx)); } - assert (best_idx); + CADICAL_assert (best_idx); LOG ("best variable %d at trail position %d", best_idx, best_pos); // Now find the frame and decision level in the control stack of that @@ -725,7 +729,7 @@ inline int Internal::determine_actual_backtrack_level (int jump) { /*------------------------------------------------------------------------*/ void Internal::eagerly_subsume_recently_learned_clauses (Clause *c) { - assert (opts.eagersubsume); + CADICAL_assert (opts.eagersubsume); LOG (c, "trying eager subsumption with"); mark (c); int64_t lim = stats.eagertried + opts.eagersubsumelim; @@ -767,20 +771,20 @@ void Internal::eagerly_subsume_recently_learned_clauses (Clause *c) { /*------------------------------------------------------------------------*/ Clause *Internal::on_the_fly_strengthen (Clause *new_conflict, int uip) { - assert (new_conflict); - assert (new_conflict->size > 2); + CADICAL_assert (new_conflict); + CADICAL_assert (new_conflict->size > 2); LOG (new_conflict, "applying OTFS on lit %d", uip); auto sorted = std::vector (); sorted.reserve (new_conflict->size); - assert (sorted.empty ()); + CADICAL_assert (sorted.empty ()); ++stats.otfs.strengthened; int *lits = new_conflict->literals; - assert (lits[0] == uip || lits[1] == uip); + CADICAL_assert (lits[0] == uip || lits[1] == uip); const int other_init = lits[0] ^ lits[1] ^ uip; - assert (mini_chain.empty ()); + CADICAL_assert (mini_chain.empty ()); const int old_size = new_conflict->size; int new_size = 0; @@ -793,7 +797,7 @@ Clause *Internal::on_the_fly_strengthen (Clause *new_conflict, int uip) { LOG (new_conflict, "removing all units in"); - assert (lits[0] == uip || lits[1] == uip); + CADICAL_assert (lits[0] == uip || lits[1] == uip); const int other = lits[0] ^ lits[1] ^ uip; lits[0] = other; lits[1] = lits[--new_size]; @@ -803,9 +807,9 @@ Clause *Internal::on_the_fly_strengthen (Clause *new_conflict, int uip) { remove_watch (watches (other_init), new_conflict); remove_watch (watches (uip), new_conflict); - assert (!lrat || lrat_chain.back () == new_conflict->id); + CADICAL_assert (!lrat || lrat_chain.back () == new_conflict->id); if (lrat) { - assert (!lrat_chain.empty ()); + CADICAL_assert (!lrat_chain.empty ()); for (const auto &id : unit_chain) { mini_chain.push_back (id); } @@ -819,16 +823,16 @@ Clause *Internal::on_the_fly_strengthen (Clause *new_conflict, int uip) { clear_unit_analyzed_literals (); unit_chain.clear (); } - assert (unit_analyzed.empty ()); + CADICAL_assert (unit_analyzed.empty ()); // sort the clause { int highest_pos = 0; int highest_level = 0; for (int i = 1; i < new_size; i++) { const unsigned other = lits[i]; - assert (val (other) < 0); + CADICAL_assert (val (other) < 0); const int level = var (other).level; - assert (level); + CADICAL_assert (level); LOG ("checking %d", other); if (level <= highest_level) continue; @@ -842,11 +846,11 @@ Clause *Internal::on_the_fly_strengthen (Clause *new_conflict, int uip) { if (new_size == 1) { LOG (new_conflict, "new size = 1, so interrupting"); - assert (!opts.exteagerreasons); + CADICAL_assert (!opts.exteagerreasons); return 0; } else { otfs_strengthen_clause (new_conflict, uip, new_size, sorted); - assert (new_size == new_conflict->size); + CADICAL_assert (new_size == new_conflict->size); } } @@ -867,7 +871,7 @@ Clause *Internal::on_the_fly_strengthen (Clause *new_conflict, int uip) { inline void Internal::otfs_subsume_clause (Clause *subsuming, Clause *subsumed) { stats.subsumed++; - assert (subsuming->size <= subsumed->size); + CADICAL_assert (subsuming->size <= subsumed->size); LOG (subsumed, "subsumed"); if (subsumed->redundant) stats.subred++; @@ -885,9 +889,9 @@ inline void Internal::otfs_subsume_clause (Clause *subsuming, stats.current.irredundant++; stats.added.irredundant++; stats.irrlits += subsuming->size; - assert (stats.current.redundant > 0); + CADICAL_assert (stats.current.redundant > 0); stats.current.redundant--; - assert (stats.added.redundant > 0); + CADICAL_assert (stats.added.redundant > 0); stats.added.redundant--; // ... and keep 'stats.added.total'. } @@ -899,7 +903,7 @@ inline void Internal::otfs_subsume_clause (Clause *subsuming, void Internal::otfs_strengthen_clause (Clause *c, int lit, int new_size, const std::vector &old) { stats.strengthened++; - assert (c->size > 2); + CADICAL_assert (c->size > 2); (void) shrink_clause (c, new_size); if (proof) { proof->otfs_strengthen_clause (c, old, mini_chain); @@ -940,11 +944,11 @@ void Internal::analyze () { START (analyze); - assert (conflict); - assert (lrat_chain.empty ()); - assert (unit_chain.empty ()); - assert (unit_analyzed.empty ()); - assert (clause.empty ()); + CADICAL_assert (conflict); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (unit_chain.empty ()); + CADICAL_assert (unit_analyzed.empty ()); + CADICAL_assert (clause.empty ()); // First update moving averages of trail height at conflict. // @@ -975,8 +979,8 @@ void Internal::analyze () { if (forced) { - assert (forced); - assert (conflict_level > 0); + CADICAL_assert (forced); + CADICAL_assert (conflict_level > 0); LOG ("single highest level literal %d", forced); // The pseudo code in the SAT'18 paper actually backtracks to the @@ -1042,8 +1046,8 @@ void Internal::analyze () { Clause *reason = conflict; LOG (reason, "analyzing conflict"); - assert (clause.empty ()); - assert (lrat_chain.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (lrat_chain.empty ()); const auto &t = &trail; int i = t->size (); // Start at end-of-trail. @@ -1060,43 +1064,43 @@ void Internal::analyze () { analyze_reason (uip, reason, open, resolvent_size, antecedent_size); if (resolved == 0) conflict_size = antecedent_size - 1; - assert (resolvent_size == open + (int) clause.size ()); + CADICAL_assert (resolvent_size == open + (int) clause.size ()); if (otfs && resolved > 0 && antecedent_size > 2 && resolvent_size < antecedent_size) { - assert (reason != conflict); + CADICAL_assert (reason != conflict); LOG (analyzed, "found candidate for OTFS conflict"); LOG (clause, "found candidate for OTFS conflict"); LOG (reason, "found candidate (size %d) for OTFS resolvent", antecedent_size); const int other = reason->literals[0] ^ reason->literals[1] ^ uip; - assert (other != uip); + CADICAL_assert (other != uip); reason = on_the_fly_strengthen (reason, uip); if (opts.bump) bump_variables (); - assert (conflict_size); + CADICAL_assert (conflict_size); if (!reason) { uip = -other; - assert (open == 1); + CADICAL_assert (open == 1); LOG ("clause is actually unit %d, stopping", -uip); reverse (begin (mini_chain), end (mini_chain)); for (auto id : mini_chain) lrat_chain.push_back (id); mini_chain.clear (); clear_analyzed_levels (); - assert (!opts.exteagerreasons); + CADICAL_assert (!opts.exteagerreasons); clause.clear (); break; } - assert (conflict_size >= 2); + CADICAL_assert (conflict_size >= 2); if (resolved == 1 && resolvent_size < conflict_size) { // here both clauses are part of the CNF, so one subsumes the other otfs_subsume_clause (reason, conflict); LOG (reason, "changing conflict to"); --conflict_size; - assert (conflict_size == reason->size); + CADICAL_assert (conflict_size == reason->size); ++stats.otfs.subsumed; ++stats.subsumed; } @@ -1134,14 +1138,14 @@ void Internal::analyze () { open = 0; analyze_reason (0, reason, open, resolvent_size, antecedent_size); conflict_size = antecedent_size - 1; - assert (open > 1); + CADICAL_assert (open > 1); } ++resolved; uip = 0; while (!uip) { - assert (i > 0); + CADICAL_assert (i > 0); const int lit = (*t)[--i]; if (!flags (lit).seen) continue; @@ -1152,13 +1156,13 @@ void Internal::analyze () { break; reason = var (uip).reason; if (reason == external_reason) { - assert (!opts.exteagerreasons); + CADICAL_assert (!opts.exteagerreasons); reason = learn_external_reason_clause (-uip, 0, true); var (uip).reason = reason; } - assert (reason != external_reason); + CADICAL_assert (reason != external_reason); LOG (reason, "analyzing %d reason", uip); - assert (resolvent_size); + CADICAL_assert (resolvent_size); --resolvent_size; } LOG ("first UIP %d", uip); @@ -1173,7 +1177,7 @@ void Internal::analyze () { UPDATE_AVERAGE (averages.current.glue.slow, glue); stats.learned.literals += size; stats.learned.clauses++; - assert (glue < size); + CADICAL_assert (glue < size); // up to this point lrat_chain contains the proof for current clause in // reversed order. in minimize and shrink the clause is changed and @@ -1277,3 +1281,5 @@ void Internal::iterate () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/arena.cpp b/src/sat/cadical/cadical_arena.cpp similarity index 85% rename from src/sat/cadical/arena.cpp rename to src/sat/cadical/cadical_arena.cpp index e8e2eea64..a8a9b183f 100644 --- a/src/sat/cadical/arena.cpp +++ b/src/sat/cadical/cadical_arena.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { Arena::Arena (Internal *i) { @@ -14,7 +18,7 @@ Arena::~Arena () { void Arena::prepare (size_t bytes) { LOG ("preparing 'to' space of arena with %zd bytes", bytes); - assert (!to.start); + CADICAL_assert (!to.start); to.top = to.start = new char[bytes]; to.end = to.start + bytes; } @@ -28,3 +32,5 @@ void Arena::swap () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/assume.cpp b/src/sat/cadical/cadical_assume.cpp similarity index 88% rename from src/sat/cadical/assume.cpp rename to src/sat/cadical/cadical_assume.cpp index a49d36af0..e7d5b79b7 100644 --- a/src/sat/cadical/assume.cpp +++ b/src/sat/cadical/cadical_assume.cpp @@ -1,6 +1,10 @@ +#include "global.h" + #include "internal.hpp" #include "options.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // Failed literal handling as pioneered by MiniSAT. This first function @@ -27,26 +31,26 @@ void Internal::assume (int lit) { // for non-lrat use BFS. TODO: maybe derecursify to avoid stack overflow // void Internal::assume_analyze_literal (int lit) { - assert (lit); + CADICAL_assert (lit); Flags &f = flags (lit); if (f.seen) return; f.seen = true; analyzed.push_back (lit); Var &v = var (lit); - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); if (v.reason == external_reason) { v.reason = wrapped_learn_external_reason_clause (-lit); - assert (v.reason || !v.level); + CADICAL_assert (v.reason || !v.level); } - assert (v.reason != external_reason); + CADICAL_assert (v.reason != external_reason); if (!v.level) { int64_t id = unit_id (-lit); lrat_chain.push_back (id); return; } if (v.reason) { - assert (v.level); + CADICAL_assert (v.level); LOG (v.reason, "analyze reason"); for (const auto &other : *v.reason) { assume_analyze_literal (other); @@ -54,16 +58,16 @@ void Internal::assume_analyze_literal (int lit) { lrat_chain.push_back (v.reason->id); return; } - assert (assumed (-lit)); + CADICAL_assert (assumed (-lit)); LOG ("failed assumption %d", -lit); clause.push_back (lit); } void Internal::assume_analyze_reason (int lit, Clause *reason) { - assert (reason); - assert (lrat_chain.empty ()); - assert (reason != external_reason); - assert (lrat); + CADICAL_assert (reason); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (reason != external_reason); + CADICAL_assert (lrat); for (const auto &other : *reason) if (other != lit) assume_analyze_literal (other); @@ -80,11 +84,11 @@ void Internal::failing () { LOG ("analyzing failing assumptions"); - assert (analyzed.empty ()); - assert (clause.empty ()); - assert (lrat_chain.empty ()); - assert (!marked_failed); - assert (!conflict_id); + CADICAL_assert (analyzed.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (!marked_failed); + CADICAL_assert (!conflict_id); if (!unsat_constraint) { // Search for failing assumptions in the (internal) assumption stack. @@ -130,7 +134,7 @@ void Internal::failing () { for (const auto &other : *ev.reason) { if (other == -lit) continue; - assert (val (other)); + CADICAL_assert (val (other)); int tmp = var (other).level; if (tmp > ev.level) ev.level = tmp; @@ -141,7 +145,7 @@ void Internal::failing () { break; } } - assert (v.reason != external_reason); + CADICAL_assert (v.reason != external_reason); if (!v.reason) { failed_clashing = lit; efailed = elit; @@ -152,7 +156,7 @@ void Internal::failing () { } } - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); // Get the 'failed' assumption from one of the three cases. int failed; @@ -162,25 +166,25 @@ void Internal::failing () { failed = failed_clashing; else failed = first_failed; - assert (failed); - assert (efailed); + CADICAL_assert (failed); + CADICAL_assert (efailed); // In any case mark literal 'failed' as failed assumption. { Flags &f = flags (failed); const unsigned bit = bign (failed); - assert (!(f.failed & bit)); + CADICAL_assert (!(f.failed & bit)); f.failed |= bit; } // First case (1). if (failed_unit) { - assert (failed == failed_unit); + CADICAL_assert (failed == failed_unit); LOG ("root-level falsified assumption %d", failed); if (proof) { if (lrat) { unsigned eidx = (efailed > 0) + 2u * (unsigned) abs (efailed); - assert ((size_t) eidx < external->ext_units.size ()); + CADICAL_assert ((size_t) eidx < external->ext_units.size ()); const int64_t id = external->ext_units[eidx]; if (id) { lrat_chain.push_back (id); @@ -198,11 +202,11 @@ void Internal::failing () { // Second case (2). if (failed_clashing) { - assert (failed == failed_clashing); + CADICAL_assert (failed == failed_clashing); LOG ("clashing assumptions %d and %d", failed, -failed); Flags &f = flags (-failed); const unsigned bit = bign (-failed); - assert (!(f.failed & bit)); + CADICAL_assert (!(f.failed & bit)); f.failed |= bit; if (proof) { vector clash = {externalize (failed), externalize (-failed)}; @@ -217,8 +221,8 @@ void Internal::failing () { "%d", first_failed, failed_level); - assert (first_failed); - assert (failed_level > 0); + CADICAL_assert (first_failed); + CADICAL_assert (failed_level > 0); // The 'analyzed' stack serves as working stack for a BFS through the // implication graph until decisions, which are all assumptions, or @@ -227,9 +231,9 @@ void Internal::failing () { { LOG ("failed assumption %d", first_failed); Flags &f = flags (first_failed); - assert (!f.seen); + CADICAL_assert (!f.seen); f.seen = true; - assert (f.failed & bign (first_failed)); + CADICAL_assert (f.failed & bign (first_failed)); analyzed.push_back (-first_failed); clause.push_back (-first_failed); } @@ -239,7 +243,7 @@ void Internal::failing () { // collected. for (auto lit : constraint) { lit *= -1; - assert (lit != INT_MIN); + CADICAL_assert (lit != INT_MIN); flags (lit).seen = true; analyzed.push_back (lit); } @@ -271,7 +275,7 @@ void Internal::failing () { size_t next = 0; while (next < analyzed.size ()) { const int lit = analyzed[next++]; - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); Var &v = var (lit); if (!v.level) continue; @@ -282,38 +286,38 @@ void Internal::failing () { continue; } } - assert (v.reason != external_reason); + CADICAL_assert (v.reason != external_reason); if (v.reason) { - assert (v.level); + CADICAL_assert (v.level); LOG (v.reason, "analyze reason"); for (const auto &other : *v.reason) { Flags &f = flags (other); if (f.seen) continue; f.seen = true; - assert (val (other) < 0); + CADICAL_assert (val (other) < 0); analyzed.push_back (-other); } } else { - assert (assumed (lit)); + CADICAL_assert (assumed (lit)); LOG ("failed assumption %d", lit); clause.push_back (-lit); Flags &f = flags (lit); const unsigned bit = bign (lit); - assert (!(f.failed & bit)); + CADICAL_assert (!(f.failed & bit)); f.failed |= bit; } } clear_analyzed_literals (); } else if (!unsat_constraint) { // LRAT for case (3) - assert (clause.size () == 1); + CADICAL_assert (clause.size () == 1); const int lit = clause[0]; Var &v = var (lit); - assert (v.reason); + CADICAL_assert (v.reason); if (v.reason == external_reason) { // does this even happen? v.reason = wrapped_learn_external_reason_clause (lit); } - assert (v.reason != external_reason); + CADICAL_assert (v.reason != external_reason); if (v.reason) assume_analyze_reason (lit, v.reason); else { @@ -328,13 +332,13 @@ void Internal::failing () { } clear_analyzed_literals (); } else { // LRAT for unsat_constraint - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); clear_analyzed_literals (); for (auto lit : constraint) { // make sure nothing gets marked failed twice // also might shortcut the case where // lrat_chain is empty because clause is tautological - assert (lit != INT_MIN); + CADICAL_assert (lit != INT_MIN); assume_analyze_literal (lit); vector empty; vector empty2; @@ -346,7 +350,7 @@ void Internal::failing () { const unsigned bit = bign (-ign); if (!(f.failed & bit)) { sum_constraints.push_back (ign); - assert (!(f.failed & bit)); + CADICAL_assert (!(f.failed & bit)); f.failed |= bit; } } @@ -386,7 +390,7 @@ void Internal::failing () { conclusion.push_back (clause_id); } } else { - assert (!lrat || (constraint.size () == constraint_clauses.size () && + CADICAL_assert (!lrat || (constraint.size () == constraint_clauses.size () && constraint.size () == constraint_chains.size ())); for (auto p = constraint.rbegin (); p != constraint.rend (); p++) { const auto &lit = *p; @@ -419,7 +423,7 @@ void Internal::failing () { for (auto &elit : econstraints) { if (lrat) { unsigned eidx = (elit > 0) + 2u * (unsigned) abs (elit); - assert ((size_t) eidx < external->ext_units.size ()); + CADICAL_assert ((size_t) eidx < external->ext_units.size ()); const int64_t id = external->ext_units[eidx]; if (id) { lrat_chain.push_back (id); @@ -463,7 +467,7 @@ void Internal::conclude_unsat () { return; concluded = true; if (!marked_failed) { - assert (conclusion.empty ()); + CADICAL_assert (conclusion.empty ()); if (!conflict_id) failing (); marked_failed = true; @@ -486,7 +490,7 @@ void Internal::reset_concluded () { concluded = false; } if (conflict_id) { - assert (conclusion.size () == 1); + CADICAL_assert (conclusion.size () == 1); return; } conclusion.clear (); @@ -548,7 +552,7 @@ struct sort_assumptions_smaller { // to the first place where the assumptions and the current trail differ. void Internal::sort_and_reuse_assumptions () { - assert (opts.ilbassumptions); + CADICAL_assert (opts.ilbassumptions); if (assumptions.empty ()) return; MSORT (opts.radixsortlim, assumptions.begin (), assumptions.end (), @@ -565,7 +569,7 @@ void Internal::sort_and_reuse_assumptions () { } const unsigned size = min (level + 1u, max_level + 1); - assert ((size_t) level == control.size () - 1); + CADICAL_assert ((size_t) level == control.size () - 1); LOG (assumptions, "sorted assumptions"); int target = 0; for (unsigned i = 1, j = 0; i < size;) { @@ -585,7 +589,7 @@ void Internal::sort_and_reuse_assumptions () { break; } ++i, ++j; - assert (var (lit).level == lev); + CADICAL_assert (var (lit).level == lev); if (l.decision == alit) { continue; } @@ -605,3 +609,5 @@ void Internal::sort_and_reuse_assumptions () { stats.assumptionsreused += level; } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/averages.cpp b/src/sat/cadical/cadical_averages.cpp similarity index 88% rename from src/sat/cadical/averages.cpp rename to src/sat/cadical/cadical_averages.cpp index 971598bd8..d40a98cdb 100644 --- a/src/sat/cadical/averages.cpp +++ b/src/sat/cadical/cadical_averages.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void Internal::init_averages () { @@ -18,7 +22,7 @@ void Internal::init_averages () { INIT_EMA (averages.current.trail.fast, opts.ematrailfast); INIT_EMA (averages.current.trail.slow, opts.ematrailslow); - assert (!averages.swapped); + CADICAL_assert (!averages.swapped); } void Internal::swap_averages () { @@ -32,3 +36,5 @@ void Internal::swap_averages () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/backtrack.cpp b/src/sat/cadical/cadical_backtrack.cpp similarity index 91% rename from src/sat/cadical/backtrack.cpp rename to src/sat/cadical/cadical_backtrack.cpp index 3a70b2775..b3c519ba2 100644 --- a/src/sat/cadical/backtrack.cpp +++ b/src/sat/cadical/cadical_backtrack.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // The global assignment stack can only be (partially) reset through @@ -8,7 +12,7 @@ namespace CaDiCaL { // specialization for 'probe' nor 'vivify' and thus it is shared. inline void Internal::unassign (int lit) { - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); set_val (lit, 0); int idx = vidx (lit); @@ -74,7 +78,7 @@ void Internal::update_target_and_best () { /*------------------------------------------------------------------------*/ void Internal::backtrack (int new_level) { - assert (new_level <= level); + CADICAL_assert (new_level <= level); if (new_level == level) return; @@ -84,13 +88,13 @@ void Internal::backtrack (int new_level) { void Internal::backtrack_without_updating_phases (int new_level) { - assert (new_level <= level); + CADICAL_assert (new_level <= level); if (new_level == level) return; stats.backtracks++; - assert (num_assigned == trail.size ()); + CADICAL_assert (num_assigned == trail.size ()); const size_t assigned = control[new_level + 1].trail; @@ -127,7 +131,7 @@ void Internal::backtrack_without_updating_phases (int new_level) { // backtracking. It is possible to just keep out-of-order assigned // literals on the trail without breaking the solver (after some // modifications to 'analyze' - see 'opts.chrono' guarded code there). - assert (opts.chrono || external_prop || did_external_prop); + CADICAL_assert (opts.chrono || external_prop || did_external_prop); #ifdef LOGGING if (!v.level) LOG ("reassign %d @ 0 unit clause %d", lit, lit); @@ -154,7 +158,7 @@ void Internal::backtrack_without_updating_phases (int new_level) { propergated = 0; // Always go back to root-level. - assert (notified <= assigned + reassigned); + CADICAL_assert (notified <= assigned + reassigned); if (reassigned) { notify_assignments (); } @@ -162,12 +166,14 @@ void Internal::backtrack_without_updating_phases (int new_level) { control.resize (new_level + 1); level = new_level; if (tainted_literal) { - assert (opts.ilb); + CADICAL_assert (opts.ilb); if (!val (tainted_literal)) { tainted_literal = 0; } } - assert (num_assigned == trail.size ()); + CADICAL_assert (num_assigned == trail.size ()); } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/backward.cpp b/src/sat/cadical/cadical_backward.cpp similarity index 90% rename from src/sat/cadical/backward.cpp rename to src/sat/cadical/cadical_backward.cpp index e7228c521..5778029a5 100644 --- a/src/sat/cadical/backward.cpp +++ b/src/sat/cadical/cadical_backward.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -24,7 +28,7 @@ Clause *Eliminator::dequeue () { return 0; Clause *res = backward.front (); backward.pop (); - assert (res->enqueued); + CADICAL_assert (res->enqueued); res->enqueued = false; LOG (res, "backward dequeue"); return res; @@ -38,8 +42,8 @@ Eliminator::~Eliminator () { /*------------------------------------------------------------------------*/ void Internal::elim_backward_clause (Eliminator &eliminator, Clause *c) { - assert (opts.elimbackward); - assert (!c->redundant); + CADICAL_assert (opts.elimbackward); + CADICAL_assert (!c->redundant); if (c->garbage) return; LOG (c, "attempting backward subsumption and strengthening with"); @@ -47,7 +51,7 @@ void Internal::elim_backward_clause (Eliminator &eliminator, Clause *c) { unsigned size = 0; int best = 0; bool satisfied = false; - assert (mini_chain.empty ()); + CADICAL_assert (mini_chain.empty ()); for (const auto &lit : *c) { const signed char tmp = val (lit); if (tmp > 0) { @@ -70,7 +74,7 @@ void Internal::elim_backward_clause (Eliminator &eliminator, Clause *c) { } else if (len > (size_t) opts.elimocclim) { LOG ("skipping backward subsumption due to too many occurrences"); } else { - assert (len); + CADICAL_assert (len); LOG ("literal %d has smallest number of occurrences %zd", best, len); LOG ("marked %d literals in clause of size %d", size, c->size); for (auto &d : occs (best)) { @@ -117,9 +121,9 @@ void Internal::elim_backward_clause (Eliminator &eliminator, Clause *c) { stats.elimbwsub++; } else { int unit = 0; - assert (minimize_chain.empty ()); - assert (analyzed.empty ()); - assert (lrat_chain.empty ()); + CADICAL_assert (minimize_chain.empty ()); + CADICAL_assert (analyzed.empty ()); + CADICAL_assert (lrat_chain.empty ()); // figure out wether we strengthen c or get a new unit for (const auto &lit : *d) { const signed char tmp = val (lit); @@ -127,7 +131,7 @@ void Internal::elim_backward_clause (Eliminator &eliminator, Clause *c) { if (!lrat) continue; Flags &f = flags (lit); - assert (!f.seen); + CADICAL_assert (!f.seen); if (f.seen) continue; f.seen = true; @@ -151,7 +155,7 @@ void Internal::elim_backward_clause (Eliminator &eliminator, Clause *c) { // {c\d}U{d\c} otherwise just the unit ids from {c\d} for (const auto &lit : *c) { const signed char tmp = val (lit); - assert (tmp <= 0); + CADICAL_assert (tmp <= 0); if (tmp >= 0) continue; Flags &f = flags (lit); @@ -183,11 +187,11 @@ void Internal::elim_backward_clause (Eliminator &eliminator, Clause *c) { } else if (lrat) clear_analyzed_literals (); if (satisfied) { - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); mark_garbage (d); elim_update_removed_clause (eliminator, d); } else if (unit && unit != INT_MIN) { - assert (unit); + CADICAL_assert (unit); LOG (d, "unit %d through hyper unary resolution with", unit); assign_unit (unit); elim_propagate (eliminator, unit); @@ -198,7 +202,7 @@ void Internal::elim_backward_clause (Eliminator &eliminator, Clause *c) { remove_occs (occs (negated), d); elim_update_removed_lit (eliminator, negated); stats.elimbwstr++; - assert (negated != best); + CADICAL_assert (negated != best); eliminator.enqueue (d); } lrat_chain.clear (); @@ -214,7 +218,7 @@ void Internal::elim_backward_clause (Eliminator &eliminator, Clause *c) { void Internal::elim_backward_clauses (Eliminator &eliminator) { if (!opts.elimbackward) { - assert (eliminator.backward.empty ()); + CADICAL_assert (eliminator.backward.empty ()); return; } START (backward); @@ -229,3 +233,5 @@ void Internal::elim_backward_clauses (Eliminator &eliminator) { /*------------------------------------------------------------------------*/ } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/bins.cpp b/src/sat/cadical/cadical_bins.cpp similarity index 75% rename from src/sat/cadical/bins.cpp rename to src/sat/cadical/cadical_bins.cpp index 406074eff..fba96f216 100644 --- a/src/sat/cadical/bins.cpp +++ b/src/sat/cadical/cadical_bins.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -7,16 +11,18 @@ namespace CaDiCaL { // Binary implication graph lists. void Internal::init_bins () { - assert (big.empty ()); + CADICAL_assert (big.empty ()); if (big.size () < 2 * vsize) big.resize (2 * vsize, Bins ()); LOG ("initialized binary implication graph"); } void Internal::reset_bins () { - assert (!big.empty ()); + CADICAL_assert (!big.empty ()); erase_vector (big); LOG ("reset binary implication graph"); } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/block.cpp b/src/sat/cadical/cadical_block.cpp similarity index 87% rename from src/sat/cadical/block.cpp rename to src/sat/cadical/cadical_block.cpp index 11721d8bd..12f852f08 100644 --- a/src/sat/cadical/block.cpp +++ b/src/sat/cadical/cadical_block.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -44,13 +48,13 @@ bool Internal::is_blocked_clause (Clause *c, int lit) { LOG (c, "trying to block on %d", lit); - assert (c->size >= opts.blockminclslim); - assert (c->size <= opts.blockmaxclslim); - assert (active (lit)); - assert (!val (lit)); - assert (!c->garbage); - assert (!c->redundant); - assert (!level); + CADICAL_assert (c->size >= opts.blockminclslim); + CADICAL_assert (c->size <= opts.blockmaxclslim); + CADICAL_assert (active (lit)); + CADICAL_assert (!val (lit)); + CADICAL_assert (!c->garbage); + CADICAL_assert (!c->redundant); + CADICAL_assert (!level); mark (c); // First mark all literals in 'c'. @@ -72,9 +76,9 @@ bool Internal::is_blocked_clause (Clause *c, int lit) { // Clause *d = *i; - assert (!d->garbage); - assert (!d->redundant); - assert (d->size <= opts.blockmaxclslim); + CADICAL_assert (!d->garbage); + CADICAL_assert (!d->redundant); + CADICAL_assert (d->size <= opts.blockmaxclslim); *i = prev_d; // Move previous non-tautological clause prev_d = d; // backwards but remember clause at this position. @@ -99,9 +103,9 @@ bool Internal::is_blocked_clause (Clause *c, int lit) { prev_other = other; if (other == -lit) continue; - assert (other != lit); - assert (active (other)); - assert (!val (other)); + CADICAL_assert (other != lit); + CADICAL_assert (active (other)); + CADICAL_assert (!val (other)); if (marked (other) < 0) { LOG ("found tautological literal %d", other); d->literals[0] = other; // Move to front of 'd'. @@ -132,7 +136,7 @@ bool Internal::is_blocked_clause (Clause *c, int lit) { // restore the old order of clauses in the occurrence list of '-lit'. // if (res) { - assert (i == end_of_os); + CADICAL_assert (i == end_of_os); const auto boc = os.begin (); while (i != boc) { Clause *d = *--i; @@ -172,8 +176,8 @@ void Internal::block_schedule (Blocker &blocker) { continue; for (const auto &lit : *c) { - assert (active (lit)); - assert (!val (lit)); + CADICAL_assert (active (lit)); + CADICAL_assert (!val (lit)); occs (lit).push_back (c); } } @@ -187,7 +191,7 @@ void Internal::block_schedule (Blocker &blocker) { for (auto lit : lits) { if (!active (lit)) continue; - assert (!val (lit)); + CADICAL_assert (!val (lit)); Occs &os = occs (lit); noccs (lit) = os.size (); } @@ -199,7 +203,7 @@ void Internal::block_schedule (Blocker &blocker) { // schedule can not be fused with the previous loop (easily) since we // first have to initialize 'noccs' for both 'lit' and '-lit'. -#ifndef QUIET +#ifndef CADICAL_QUIET int skipped = 0; #endif @@ -207,16 +211,16 @@ void Internal::block_schedule (Blocker &blocker) { if (!active (idx)) continue; if (frozen (idx)) { -#ifndef QUIET +#ifndef CADICAL_QUIET skipped += 2; #endif continue; } - assert (!val (idx)); + CADICAL_assert (!val (idx)); for (int sign = -1; sign <= 1; sign += 2) { const int lit = sign * idx; if (marked_skip (lit)) { -#ifndef QUIET +#ifndef CADICAL_QUIET skipped++; #endif continue; @@ -249,15 +253,15 @@ void Internal::block_schedule (Blocker &blocker) { void Internal::block_pure_literal (Blocker &blocker, int lit) { if (frozen (lit)) return; - assert (active (lit)); + CADICAL_assert (active (lit)); Occs &pos = occs (lit); Occs &nos = occs (-lit); - assert (!noccs (-lit)); -#ifndef NDEBUG + CADICAL_assert (!noccs (-lit)); +#ifndef CADICAL_NDEBUG for (const auto &c : nos) - assert (c->garbage); + CADICAL_assert (c->garbage); #endif stats.blockpurelits++; LOG ("found pure literal %d", lit); @@ -267,7 +271,7 @@ void Internal::block_pure_literal (Blocker &blocker, int lit) { for (const auto &c : pos) { if (c->garbage) continue; - assert (!c->redundant); + CADICAL_assert (!c->redundant); LOG (c, "pure literal %d in", lit); blocker.reschedule.push_back (c); if (proof) { @@ -297,25 +301,25 @@ void Internal::block_pure_literal (Blocker &blocker, int lit) { void Internal::block_literal_with_one_negative_occ (Blocker &blocker, int lit) { - assert (active (lit)); - assert (!frozen (lit)); - assert (noccs (lit) > 0); - assert (noccs (-lit) == 1); + CADICAL_assert (active (lit)); + CADICAL_assert (!frozen (lit)); + CADICAL_assert (noccs (lit) > 0); + CADICAL_assert (noccs (-lit) == 1); Occs &nos = occs (-lit); - assert (nos.size () >= 1); + CADICAL_assert (nos.size () >= 1); Clause *d = 0; for (const auto &c : nos) { if (c->garbage) continue; - assert (!d); + CADICAL_assert (!d); d = c; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG break; #endif } - assert (d); + CADICAL_assert (d); nos.resize (1); nos[0] = d; @@ -324,9 +328,9 @@ void Internal::block_literal_with_one_negative_occ (Blocker &blocker, return; } - assert (!d->garbage); - assert (!d->redundant); - assert (d->size <= opts.blockmaxclslim); + CADICAL_assert (!d->garbage); + CADICAL_assert (!d->redundant); + CADICAL_assert (d->size <= opts.blockmaxclslim); LOG (d, "common %d antecedent", lit); mark (d); @@ -379,9 +383,9 @@ void Internal::block_literal_with_one_negative_occ (Blocker &blocker, prev_other = other; if (other == lit) continue; - assert (other != -lit); - assert (active (other)); - assert (!val (other)); + CADICAL_assert (other != -lit); + CADICAL_assert (active (other)); + CADICAL_assert (!val (other)); if (marked (other) < 0) { LOG ("found tautological literal %d", other); c->literals[0] = other; // Move to front of 'c'. @@ -434,13 +438,13 @@ void Internal::block_literal_with_one_negative_occ (Blocker &blocker, size_t Internal::block_candidates (Blocker &blocker, int lit) { - assert (blocker.candidates.empty ()); + CADICAL_assert (blocker.candidates.empty ()); Occs &pos = occs (lit); // Positive occurrences of 'lit'. Occs &nos = occs (-lit); - assert ((size_t) noccs (lit) <= pos.size ()); - assert ((size_t) noccs (-lit) == nos.size ()); // Already flushed. + CADICAL_assert ((size_t) noccs (lit) <= pos.size ()); + CADICAL_assert ((size_t) noccs (-lit) == nos.size ()); // Already flushed. // Mark all literals in clauses with '-lit'. Note that 'mark2' uses // separate bits for 'lit' and '-lit'. @@ -457,7 +461,7 @@ size_t Internal::block_candidates (Blocker &blocker, int lit) { j--; continue; } - assert (!c->redundant); + CADICAL_assert (!c->redundant); if (c->size > opts.blockmaxclslim) continue; if (c->size < opts.blockminclslim) @@ -468,9 +472,9 @@ size_t Internal::block_candidates (Blocker &blocker, int lit) { const int other = *l; if (other == lit) continue; - assert (other != -lit); - assert (active (other)); - assert (!val (other)); + CADICAL_assert (other != -lit); + CADICAL_assert (active (other)); + CADICAL_assert (!val (other)); if (marked2 (-other)) break; } @@ -482,7 +486,7 @@ size_t Internal::block_candidates (Blocker &blocker, int lit) { else pos.resize (j - pos.begin ()); - assert (pos.size () == (size_t) noccs (lit)); // Now also flushed. + CADICAL_assert (pos.size () == (size_t) noccs (lit)); // Now also flushed. for (const auto &c : nos) unmark (c); @@ -498,8 +502,8 @@ size_t Internal::block_candidates (Blocker &blocker, int lit) { // resolvent with that clause. Clause *Internal::block_impossible (Blocker &blocker, int lit) { - assert (noccs (-lit) > 1); - assert (blocker.candidates.size () > 1); + CADICAL_assert (noccs (-lit) > 1); + CADICAL_assert (blocker.candidates.size () > 1); for (const auto &c : blocker.candidates) mark2 (c); @@ -508,18 +512,18 @@ Clause *Internal::block_impossible (Blocker &blocker, int lit) { Clause *res = 0; for (const auto &c : nos) { - assert (!c->garbage); - assert (!c->redundant); - assert (c->size <= opts.blockmaxclslim); + CADICAL_assert (!c->garbage); + CADICAL_assert (!c->redundant); + CADICAL_assert (c->size <= opts.blockmaxclslim); const const_literal_iterator eoc = c->end (); const_literal_iterator l; for (l = c->begin (); l != eoc; l++) { const int other = *l; if (other == -lit) continue; - assert (other != lit); - assert (active (other)); - assert (!val (other)); + CADICAL_assert (other != lit); + CADICAL_assert (active (other)); + CADICAL_assert (!val (other)); if (marked2 (-other)) break; } @@ -544,13 +548,13 @@ Clause *Internal::block_impossible (Blocker &blocker, int lit) { void Internal::block_literal_with_at_least_two_negative_occs ( Blocker &blocker, int lit) { - assert (active (lit)); - assert (!frozen (lit)); - assert (noccs (lit) > 0); - assert (noccs (-lit) > 1); + CADICAL_assert (active (lit)); + CADICAL_assert (!frozen (lit)); + CADICAL_assert (noccs (lit) > 0); + CADICAL_assert (noccs (-lit) > 1); Occs &nos = occs (-lit); - assert ((size_t) noccs (-lit) <= nos.size ()); + CADICAL_assert ((size_t) noccs (-lit) <= nos.size ()); int max_size = 0; @@ -571,8 +575,8 @@ void Internal::block_literal_with_at_least_two_negative_occs ( else nos.resize (j - nos.begin ()); - assert (nos.size () == (size_t) noccs (-lit)); - assert (nos.size () > 1); + CADICAL_assert (nos.size () == (size_t) noccs (-lit)); + CADICAL_assert (nos.size () > 1); // If the maximum size of a negative clause (with '-lit') exceeds the // maximum clause size limit ignore this candidate literal. @@ -604,7 +608,7 @@ void Internal::block_literal_with_at_least_two_negative_occs ( // if (candidates > 1 && block_impossible (blocker, lit)) { LOG ("impossible to block any candidate clause on %d", lit); - assert (blocker.candidates.empty ()); + CADICAL_assert (blocker.candidates.empty ()); return; } @@ -616,8 +620,8 @@ void Internal::block_literal_with_at_least_two_negative_occs ( // Go over all remaining candidates and try to block them on 'lit'. // for (const auto &c : blocker.candidates) { - assert (!c->garbage); - assert (!c->redundant); + CADICAL_assert (!c->garbage); + CADICAL_assert (!c->redundant); if (!is_blocked_clause (c, lit)) continue; blocked++; @@ -646,15 +650,15 @@ void Internal::block_literal_with_at_least_two_negative_occs ( void Internal::block_reschedule_clause (Blocker &blocker, int lit, Clause *c) { -#ifdef NDEBUG +#ifdef CADICAL_NDEBUG (void) lit; #endif - assert (c->garbage); + CADICAL_assert (c->garbage); for (const auto &other : *c) { int64_t &n = noccs (other); - assert (n > 0); + CADICAL_assert (n > 0); n--; LOG ("updating %d with %" PRId64 " positive and %" PRId64 @@ -669,7 +673,7 @@ void Internal::block_reschedule_clause (Blocker &blocker, int lit, } if (blocker.schedule.contains (vlit (other))) { - assert (other != lit); + CADICAL_assert (other != lit); blocker.schedule.update (vlit (other)); } } @@ -688,14 +692,14 @@ void Internal::block_reschedule (Blocker &blocker, int lit) { /*------------------------------------------------------------------------*/ void Internal::block_literal (Blocker &blocker, int lit) { - assert (!marked_skip (lit)); + CADICAL_assert (!marked_skip (lit)); if (!active (lit)) return; // Pure literal '-lit'. if (frozen (lit)) return; - assert (!val (lit)); + CADICAL_assert (!val (lit)); // If the maximum number of a negative clauses (with '-lit') exceeds the // occurrence limit ignore this candidate literal. @@ -709,8 +713,8 @@ void Internal::block_literal (Blocker &blocker, int lit) { stats.blockcands++; - assert (blocker.reschedule.empty ()); - assert (blocker.candidates.empty ()); + CADICAL_assert (blocker.reschedule.empty ()); + CADICAL_assert (blocker.candidates.empty ()); if (!noccs (-lit)) block_pure_literal (blocker, lit); @@ -728,7 +732,7 @@ void Internal::block_literal (Blocker &blocker, int lit) { // have to try blocked clause elimination on it again until irredundant // clauses with its negation are removed. // - assert (!frozen (lit)); // just to be sure ... + CADICAL_assert (!frozen (lit)); // just to be sure ... unmark_block (lit); } @@ -752,7 +756,7 @@ bool Internal::block () { if (!propagate ()) { LOG ("propagating units results in empty clause"); learn_empty_clause (); - assert (unsat); + CADICAL_assert (unsat); } clear_watches (); reset_watches (); @@ -766,9 +770,9 @@ bool Internal::block () { LOG ("block-%" PRId64 "", stats.blockings); - assert (!level); - assert (!watching ()); - assert (!occurring ()); + CADICAL_assert (!level); + CADICAL_assert (!watching ()); + CADICAL_assert (!occurring ()); mark_satisfied_clauses_as_garbage (); @@ -822,3 +826,5 @@ bool Internal::block () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/ccadical.cpp b/src/sat/cadical/cadical_ccadical.cpp similarity index 93% rename from src/sat/cadical/ccadical.cpp rename to src/sat/cadical/cadical_ccadical.cpp index 91438932b..ae5c1093f 100644 --- a/src/sat/cadical/ccadical.cpp +++ b/src/sat/cadical/cadical_ccadical.cpp @@ -1,8 +1,12 @@ +#include "global.h" + #include "cadical.hpp" #include #include +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { struct Wrapper : Learner, Terminator { @@ -63,12 +67,14 @@ struct Wrapper : Learner, Terminator { } // namespace CaDiCaL -using namespace CaDiCaL; - -extern "C" { +ABC_NAMESPACE_IMPL_END #include "ccadical.h" +ABC_NAMESPACE_IMPL_START + +using namespace CaDiCaL; + const char *ccadical_signature (void) { return Solver::signature (); } CCaDiCaL *ccadical_init (void) { return (CCaDiCaL *) new Wrapper (); } @@ -193,4 +199,13 @@ int ccadical_vars (CCaDiCaL *ptr) { int ccadical_reserve_difference (CCaDiCaL *ptr, int number_of_vars) { return ((Wrapper *) ptr)->solver->reserve_difference (number_of_vars); } + +void ccadical_reserve(CCaDiCaL *ptr, int min_max_var) { + ((Wrapper *) ptr)->solver->reserve(min_max_var); } + +int ccadical_is_inconsistent(CCaDiCaL *ptr) { + return ((Wrapper *) ptr)->solver->inconsistent (); +} + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/checker.cpp b/src/sat/cadical/cadical_checker.cpp similarity index 91% rename from src/sat/cadical/checker.cpp rename to src/sat/cadical/cadical_checker.cpp index cfb60f0f7..63a5f2b10 100644 --- a/src/sat/cadical/checker.cpp +++ b/src/sat/cadical/cadical_checker.cpp @@ -1,12 +1,16 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ inline unsigned Checker::l2u (int lit) { - assert (lit); - assert (lit != INT_MIN); + CADICAL_assert (lit); + CADICAL_assert (lit != INT_MIN); unsigned res = 2 * (abs (lit) - 1); if (lit < 0) res++; @@ -14,22 +18,22 @@ inline unsigned Checker::l2u (int lit) { } inline signed char Checker::val (int lit) { - assert (lit); - assert (lit != INT_MIN); - assert (abs (lit) < size_vars); - assert (vals[lit] == -vals[-lit]); + CADICAL_assert (lit); + CADICAL_assert (lit != INT_MIN); + CADICAL_assert (abs (lit) < size_vars); + CADICAL_assert (vals[lit] == -vals[-lit]); return vals[lit]; } signed char &Checker::mark (int lit) { const unsigned u = l2u (lit); - assert (u < marks.size ()); + CADICAL_assert (u < marks.size ()); return marks[u]; } inline CheckerWatcher &Checker::watcher (int lit) { const unsigned u = l2u (lit); - assert (u < watchers.size ()); + CADICAL_assert (u < watchers.size ()); return watchers[u]; } @@ -37,7 +41,7 @@ inline CheckerWatcher &Checker::watcher (int lit) { CheckerClause *Checker::new_clause () { const size_t size = simplified.size (); - assert (size > 1), assert (size <= UINT_MAX); + CADICAL_assert (size > 1), CADICAL_assert (size <= UINT_MAX); const size_t bytes = sizeof (CheckerClause) + (size - 2) * sizeof (int); CheckerClause *res = (CheckerClause *) new char[bytes]; DeferDeleteArray delete_res ((char *) res); @@ -63,8 +67,8 @@ CheckerClause *Checker::new_clause () { break; } } - assert (!val (literals[0])); - assert (!val (literals[1])); + CADICAL_assert (!val (literals[0])); + CADICAL_assert (!val (literals[1])); watcher (literals[0]).push_back (CheckerWatch (literals[1], res)); watcher (literals[1]).push_back (CheckerWatch (literals[0], res)); @@ -74,18 +78,18 @@ CheckerClause *Checker::new_clause () { void Checker::delete_clause (CheckerClause *c) { if (c->size) { - assert (c->size > 1); - assert (num_clauses); + CADICAL_assert (c->size > 1); + CADICAL_assert (num_clauses); num_clauses--; } else { - assert (num_garbage); + CADICAL_assert (num_garbage); num_garbage--; } delete[] (char *) c; } void Checker::enlarge_clauses () { - assert (num_clauses == size_clauses); + CADICAL_assert (num_clauses == size_clauses); const uint64_t new_size_clauses = size_clauses ? 2 * size_clauses : 1; LOG ("CHECKER enlarging clauses of checker from %" PRIu64 " to %" PRIu64, (uint64_t) size_clauses, (uint64_t) new_size_clauses); @@ -131,7 +135,7 @@ void Checker::collect_garbage_clauses () { c->next = garbage; garbage = c; num_garbage++; - assert (num_clauses); + CADICAL_assert (num_clauses); num_clauses--; } else p = &c->next; @@ -163,7 +167,7 @@ void Checker::collect_garbage_clauses () { for (CheckerClause *c = garbage, *next; c; c = next) next = c->next, delete_clause (c); - assert (!num_garbage); + CADICAL_assert (!num_garbage); garbage = 0; } @@ -181,7 +185,7 @@ Checker::Checker (Internal *i) uint64_t nonce = random.next (); if (!(nonce & 1)) nonce++; - assert (nonce), assert (nonce & 1); + CADICAL_assert (nonce), CADICAL_assert (nonce & 1); nonces[n] = nonce; } @@ -213,7 +217,7 @@ Checker::~Checker () { void Checker::enlarge_vars (int64_t idx) { - assert (0 < idx), assert (idx <= INT_MAX); + CADICAL_assert (0 < idx), CADICAL_assert (idx <= INT_MAX); int64_t new_size_vars = size_vars ? 2 * size_vars : 2; while (idx >= new_size_vars) @@ -237,12 +241,12 @@ void Checker::enlarge_vars (int64_t idx) { watchers.resize (2 * new_size_vars); marks.resize (2 * new_size_vars); - assert (idx < new_size_vars); + CADICAL_assert (idx < new_size_vars); } inline void Checker::import_literal (int lit) { - assert (lit); - assert (lit != INT_MIN); + CADICAL_assert (lit); + CADICAL_assert (lit != INT_MIN); int idx = abs (lit); if (idx >= size_vars) enlarge_vars (idx); @@ -289,7 +293,7 @@ bool Checker::tautological () { /*------------------------------------------------------------------------*/ uint64_t Checker::reduce_hash (uint64_t hash, uint64_t size) { - assert (size > 0); + CADICAL_assert (size > 0); unsigned shift = 32; uint64_t res = hash; while ((((uint64_t) 1) << shift) > size) { @@ -297,7 +301,7 @@ uint64_t Checker::reduce_hash (uint64_t hash, uint64_t size) { shift >>= 1; } res &= size - 1; - assert (res < size); + CADICAL_assert (res < size); return res; } @@ -344,7 +348,7 @@ void Checker::insert () { /*------------------------------------------------------------------------*/ inline void Checker::assign (int lit) { - assert (!val (lit)); + CADICAL_assert (!val (lit)); vals[lit] = 1; vals[-lit] = -1; trail.push_back (lit); @@ -354,26 +358,26 @@ inline void Checker::assume (int lit) { signed char tmp = val (lit); if (tmp > 0) return; - assert (!tmp); + CADICAL_assert (!tmp); stats.assumptions++; assign (lit); } void Checker::backtrack (unsigned previously_propagated) { - assert (previously_propagated <= trail.size ()); + CADICAL_assert (previously_propagated <= trail.size ()); while (trail.size () > previously_propagated) { int lit = trail.back (); - assert (val (lit) > 0); - assert (val (-lit) < 0); + CADICAL_assert (val (lit) > 0); + CADICAL_assert (val (-lit) < 0); vals[lit] = vals[-lit] = 0; trail.pop_back (); } trail.resize (previously_propagated); next_to_propagate = previously_propagated; - assert (trail.size () == next_to_propagate); + CADICAL_assert (trail.size () == next_to_propagate); } /*------------------------------------------------------------------------*/ @@ -386,15 +390,15 @@ bool Checker::propagate () { while (res && next_to_propagate < trail.size ()) { int lit = trail[next_to_propagate++]; stats.propagations++; - assert (val (lit) > 0); - assert (abs (lit) < size_vars); + CADICAL_assert (val (lit) > 0); + CADICAL_assert (abs (lit) < size_vars); CheckerWatcher &ws = watcher (-lit); const auto end = ws.end (); auto j = ws.begin (), i = j; for (; res && i != end; i++) { CheckerWatch &w = *j++ = *i; const int blit = w.blit; - assert (blit != -lit); + CADICAL_assert (blit != -lit); const signed char blit_val = val (blit); if (blit_val > 0) continue; @@ -405,16 +409,16 @@ bool Checker::propagate () { else assign (w.blit); // but still sound } else { - assert (size > 2); + CADICAL_assert (size > 2); CheckerClause *c = w.clause; if (!c->size) { j--; continue; } // skip garbage clauses - assert (size == c->size); + CADICAL_assert (size == c->size); int *lits = c->literals; int other = lits[0] ^ lits[1] ^ (-lit); - assert (other != -lit); + CADICAL_assert (other != -lit); signed char other_val = val (other); if (other_val > 0) { j[-1].blit = other; @@ -511,7 +515,7 @@ void Checker::add_clause (const char *type) { const signed char tmp = val (lit); if (tmp < 0) continue; - assert (!tmp); + CADICAL_assert (!tmp); if (unit) { unit = INT_MIN; break; @@ -598,10 +602,10 @@ void Checker::delete_clause (int64_t id, bool, const vector &c) { if (!tautological ()) { CheckerClause **p = find (), *d = *p; if (d) { - assert (d->size > 1); + CADICAL_assert (d->size > 1); // Remove from hash table, mark as garbage, connect to garbage list. num_garbage++; - assert (num_clauses); + CADICAL_assert (num_clauses); num_clauses--; *p = d->next; d->next = garbage; @@ -646,3 +650,5 @@ void Checker::dump () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/clause.cpp b/src/sat/cadical/cadical_clause.cpp similarity index 88% rename from src/sat/cadical/clause.cpp rename to src/sat/cadical/cadical_clause.cpp index aa011bedd..1c84994a6 100644 --- a/src/sat/cadical/clause.cpp +++ b/src/sat/cadical/cadical_clause.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -41,7 +45,7 @@ void Internal::unmark_clause () { void Internal::mark_removed (Clause *c, int except) { LOG (c, "marking removed"); - assert (!c->redundant); + CADICAL_assert (!c->redundant); for (const auto &lit : *c) if (lit != except) mark_removed (lit); @@ -68,7 +72,7 @@ inline void Internal::mark_added (int lit, int size, bool redundant) { void Internal::mark_added (Clause *c) { LOG (c, "marking added"); - assert (likely_to_be_kept_clause (c)); + CADICAL_assert (likely_to_be_kept_clause (c)); for (const auto &lit : *c) mark_added (lit, c->size, c->redundant); } @@ -77,9 +81,9 @@ void Internal::mark_added (Clause *c) { Clause *Internal::new_clause (bool red, int glue) { - assert (clause.size () <= (size_t) INT_MAX); + CADICAL_assert (clause.size () <= (size_t) INT_MAX); const int size = (int) clause.size (); - assert (size >= 2); + CADICAL_assert (size >= 2); if (glue > size) glue = size; @@ -119,7 +123,7 @@ Clause *Internal::new_clause (bool red, int glue) { // Just checking that we did not mess up our sophisticated memory layout. // This might be compiler dependent though. Crucial for correctness. // - assert (c->bytes () == bytes); + CADICAL_assert (c->bytes () == bytes); stats.current.total++; stats.added.total++; @@ -146,7 +150,7 @@ Clause *Internal::new_clause (bool red, int glue) { /*------------------------------------------------------------------------*/ void Internal::promote_clause (Clause *c, int new_glue) { - assert (c->redundant); + CADICAL_assert (c->redundant); const int tier1limit = tier1[false]; const int tier2limit = max (tier1limit, tier2[false]); if (!c->redundant) @@ -173,7 +177,7 @@ void Internal::promote_clause (Clause *c, int new_glue) { /*------------------------------------------------------------------------*/ void Internal::promote_clause_glue_only (Clause *c, int new_glue) { - assert (c->redundant); + CADICAL_assert (c->redundant); if (c->hyper) return; int old_glue = c->glue; @@ -209,10 +213,10 @@ void Internal::promote_clause_glue_only (Clause *c, int new_glue) { size_t Internal::shrink_clause (Clause *c, int new_size) { if (opts.check && is_external_forgettable (c->id)) mark_garbage_external_forgettable (c->id); - assert (new_size >= 2); + CADICAL_assert (new_size >= 2); int old_size = c->size; - assert (new_size < old_size); -#ifndef NDEBUG + CADICAL_assert (new_size < old_size); +#ifndef CADICAL_NDEBUG for (int i = c->size; i < new_size; i++) c->literals[i] = 0; #endif @@ -229,7 +233,7 @@ size_t Internal::shrink_clause (Clause *c, int new_size) { promote_clause_glue_only (c, min (c->size - 1, c->glue)); else { int delta_size = old_size - new_size; - assert (stats.irrlits >= delta_size); + CADICAL_assert (stats.irrlits >= delta_size); stats.irrlits -= delta_size; } @@ -256,11 +260,11 @@ void Internal::delete_clause (Clause *c) { size_t bytes = c->bytes (); stats.collected += bytes; if (c->garbage) { - assert (stats.garbage.bytes >= (int64_t) bytes); + CADICAL_assert (stats.garbage.bytes >= (int64_t) bytes); stats.garbage.bytes -= bytes; - assert (stats.garbage.clauses > 0); + CADICAL_assert (stats.garbage.clauses > 0); stats.garbage.clauses--; - assert (stats.garbage.literals >= c->size); + CADICAL_assert (stats.garbage.literals >= c->size); stats.garbage.literals -= c->size; // See the discussion in 'propagate' on avoiding to eagerly trace binary @@ -296,7 +300,7 @@ void Internal::delete_clause (Clause *c) { // void Internal::mark_garbage (Clause *c) { - assert (!c->garbage); + CADICAL_assert (!c->garbage); // Delay tracing deletion of binary clauses. See the discussion above in // 'delete_clause' and also in 'propagate'. @@ -312,17 +316,17 @@ void Internal::mark_garbage (Clause *c) { if (opts.check && is_external_forgettable (c->id)) mark_garbage_external_forgettable (c->id); - assert (stats.current.total > 0); + CADICAL_assert (stats.current.total > 0); stats.current.total--; size_t bytes = c->bytes (); if (c->redundant) { - assert (stats.current.redundant > 0); + CADICAL_assert (stats.current.redundant > 0); stats.current.redundant--; } else { - assert (stats.current.irredundant > 0); + CADICAL_assert (stats.current.irredundant > 0); stats.current.irredundant--; - assert (stats.irrlits >= c->size); + CADICAL_assert (stats.irrlits >= c->size); stats.irrlits -= c->size; mark_removed (c); } @@ -341,11 +345,11 @@ void Internal::mark_garbage (Clause *c) { // to learn a new unit clause (which was confusing in log files). void Internal::assign_original_unit (int64_t id, int lit) { - assert (!level || opts.chrono); - assert (!unsat); + CADICAL_assert (!level || opts.chrono); + CADICAL_assert (!unsat); const int idx = vidx (lit); - assert (!vals[idx]); - assert (!flags (idx).eliminated ()); + CADICAL_assert (!vals[idx]); + CADICAL_assert (!flags (idx).eliminated ()); Var &v = var (idx); v.level = 0; v.trail = (int) trail.size (); @@ -358,13 +362,13 @@ void Internal::assign_original_unit (int64_t id, int lit) { if (lrat || frat) unit_clauses (uidx) = id; LOG ("original unit assign %d", lit); - assert (num_assigned == trail.size () || level); + CADICAL_assert (num_assigned == trail.size () || level); mark_fixed (lit); if (level) return; if (propagate ()) return; - assert (conflict); + CADICAL_assert (conflict); LOG ("propagation of original unit results in conflict"); learn_empty_clause (); } @@ -383,14 +387,14 @@ void Internal::add_new_original_clause (int64_t id) { if (!from_propagator && level && !opts.ilb) { backtrack (); } else if (tainted_literal) { - assert (val (tainted_literal)); + CADICAL_assert (val (tainted_literal)); int new_level = var (tainted_literal).level - 1; - assert (new_level >= 0); + CADICAL_assert (new_level >= 0); backtrack (new_level); } - assert (!tainted_literal); + CADICAL_assert (!tainted_literal); LOG (original, "original clause"); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); bool skip = false; unordered_set learned_levels; size_t unassigned = 0; @@ -399,7 +403,7 @@ void Internal::add_new_original_clause (int64_t id) { LOG ("skipping clause since formula is already inconsistent"); skip = true; } else { - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); for (const auto &lit : original) { int tmp = marked (lit); if (tmp > 0) { @@ -425,7 +429,7 @@ void Internal::add_new_original_clause (int64_t id) { skip = true; } else { clause.push_back (lit); - assert (flags (lit).status != Flags::UNUSED); + CADICAL_assert (flags (lit).status != Flags::UNUSED); tmp = val (lit); if (tmp) learned_levels.insert (var (lit).level); @@ -477,7 +481,7 @@ void Internal::add_new_original_clause (int64_t id) { if (!size) { if (from_propagator) stats.ext_prop.elearn_conf++; - assert (!unsat); + CADICAL_assert (!unsat); if (!original.size ()) VERBOSE (1, "found empty original clause"); else @@ -488,12 +492,12 @@ void Internal::add_new_original_clause (int64_t id) { conclusion.push_back (new_id); } else if (size == 1) { if (force_no_backtrack) { - assert (level); + CADICAL_assert (level); const int idx = vidx (clause[0]); - assert (val (clause[0]) >= 0); - assert (!flags (idx).eliminated ()); + CADICAL_assert (val (clause[0]) >= 0); + CADICAL_assert (!flags (idx).eliminated ()); Var &v = var (idx); - assert (val (clause[0])); + CADICAL_assert (val (clause[0])); v.level = 0; v.reason = 0; const unsigned uidx = vlit (clause[0]); @@ -502,20 +506,20 @@ void Internal::add_new_original_clause (int64_t id) { mark_fixed (clause[0]); } else { const int lit = clause[0]; - assert (!val (lit) || var (lit).level); + CADICAL_assert (!val (lit) || var (lit).level); if (val (lit) < 0) backtrack (var (lit).level - 1); - assert (val (lit) >= 0); + CADICAL_assert (val (lit) >= 0); handle_external_clause (0); assign_original_unit (new_id, lit); } } else { move_literals_to_watch (); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG check_watched_literal_invariants (); #endif int glue = (int) (learned_levels.size () + unassigned); - assert (glue <= (int) clause.size ()); + CADICAL_assert (glue <= (int) clause.size ()); bool clause_redundancy = from_propagator && ext_clause_forgettable; Clause *c = new_clause (clause_redundancy, glue); c->id = new_id; @@ -536,18 +540,18 @@ void Internal::add_new_original_clause (int64_t id) { // are assigned at the highest decision level. // Clause *Internal::new_learned_redundant_clause (int glue) { - assert (clause.size () > 1); -#ifndef NDEBUG + CADICAL_assert (clause.size () > 1); +#ifndef CADICAL_NDEBUG for (size_t i = 2; i < clause.size (); i++) - assert (var (clause[0]).level >= var (clause[i]).level), - assert (var (clause[1]).level >= var (clause[i]).level); + CADICAL_assert (var (clause[0]).level >= var (clause[i]).level), + CADICAL_assert (var (clause[1]).level >= var (clause[i]).level); #endif external->check_learned_clause (); Clause *res = new_clause (true, glue); if (proof) { proof->add_derived_clause (res, lrat_chain); } - assert (watching ()); + CADICAL_assert (watching ()); watch_clause (res); return res; } @@ -560,7 +564,7 @@ Clause *Internal::new_hyper_binary_resolved_clause (bool red, int glue) { if (proof) { proof->add_derived_clause (res, lrat_chain); } - assert (watching ()); + CADICAL_assert (watching ()); watch_clause (res); return res; } @@ -574,7 +578,7 @@ Clause *Internal::new_hyper_ternary_resolved_clause (bool red) { if (proof) { proof->add_derived_clause (res, lrat_chain); } - assert (!watching ()); + CADICAL_assert (!watching ()); return res; } @@ -586,8 +590,8 @@ Clause *Internal::new_factor_clause () { if (proof) { proof->add_derived_clause (res, lrat_chain); } - assert (!watching ()); - assert (occurring ()); + CADICAL_assert (!watching ()); + CADICAL_assert (occurring ()); for (const auto &lit : *res) { occs (lit).push_back (res); } @@ -606,7 +610,7 @@ Internal::new_hyper_ternary_resolved_clause_and_watch (bool red, proof->add_derived_clause (res, lrat_chain); } if (full_watching) { - assert (watching ()); + CADICAL_assert (watching ()); watch_clause (res); } return res; @@ -622,7 +626,7 @@ Clause *Internal::new_clause_as (const Clause *orig) { if (proof) { proof->add_derived_clause (res, lrat_chain); } - assert (watching ()); + CADICAL_assert (watching ()); watch_clause (res); return res; } @@ -636,8 +640,10 @@ Clause *Internal::new_resolved_irredundant_clause () { proof->add_derived_clause (clause_id + 1, false, clause, lrat_chain); } Clause *res = new_clause (false); - assert (!watching ()); + CADICAL_assert (!watching ()); return res; } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/collect.cpp b/src/sat/cadical/cadical_collect.cpp similarity index 93% rename from src/sat/cadical/collect.cpp rename to src/sat/cadical/cadical_collect.cpp index 2e583c9c2..c36e063b2 100644 --- a/src/sat/cadical/collect.cpp +++ b/src/sat/cadical/cadical_collect.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -56,7 +60,7 @@ void Internal::remove_falsified_literals (Clause *c) { literal_iterator j = c->begin (); for (i = j; i != end; i++) { const int lit = *j++ = *i, tmp = fixed (lit); - assert (tmp <= 0); + CADICAL_assert (tmp <= 0); if (tmp >= 0) continue; LOG ("flushing %d", lit); @@ -102,23 +106,23 @@ void Internal::mark_satisfied_clauses_as_garbage () { void Internal::protect_reasons () { LOG ("protecting reason clauses of all assigned variables on trail"); - assert (!protected_reasons); + CADICAL_assert (!protected_reasons); #ifdef LOGGING size_t count = 0; #endif for (const auto &lit : trail) { if (!active (lit)) continue; - assert (val (lit)); + CADICAL_assert (val (lit)); Var &v = var (lit); - assert (v.level > 0); + CADICAL_assert (v.level > 0); Clause *reason = v.reason; if (!reason) continue; if (reason == external_reason) continue; LOG (reason, "protecting assigned %d reason %p", lit, (void *) reason); - assert (!reason->reason); + CADICAL_assert (!reason->reason); reason->reason = true; #ifdef LOGGING count++; @@ -135,16 +139,16 @@ void Internal::protect_reasons () { void Internal::unprotect_reasons () { LOG ("unprotecting reasons clauses of all assigned variables on trail"); - assert (protected_reasons); + CADICAL_assert (protected_reasons); #ifdef LOGGING size_t count = 0; #endif for (const auto &lit : trail) { if (!active (lit)) continue; - assert (val (lit)); + CADICAL_assert (val (lit)); Var &v = var (lit); - assert (v.level > 0); + CADICAL_assert (v.level > 0); Clause *reason = v.reason; if (!reason) continue; @@ -152,7 +156,7 @@ void Internal::unprotect_reasons () { continue; LOG (reason, "unprotecting assigned %d reason %p", lit, (void *) reason); - assert (reason->reason); + CADICAL_assert (reason->reason); reason->reason = false; #ifdef LOGGING count++; @@ -181,7 +185,7 @@ size_t Internal::flush_occs (int lit) { if (c->collect ()) continue; *j++ = c->moved ? c->copy : c; - // assert (!c->redundant); // -> not true in sweeping + // CADICAL_assert (!c->redundant); // -> not true in sweeping res++; } os.resize (j - os.begin ()); @@ -196,7 +200,7 @@ size_t Internal::flush_occs (int lit) { // preprocessing is actually redundant. inline void Internal::flush_watches (int lit, Watches &saved) { - assert (saved.empty ()); + CADICAL_assert (saved.empty ()); Watches &ws = watches (lit); const const_watch_iterator end = ws.end (); watch_iterator j = ws.begin (); @@ -211,7 +215,7 @@ inline void Internal::flush_watches (int lit, Watches &saved) { w.size = c->size; const int new_blit_pos = (c->literals[0] == lit); LOG (c, "clause in flush_watch starting from %d", lit); - assert (c->literals[!new_blit_pos] == lit); /*FW1*/ + CADICAL_assert (c->literals[!new_blit_pos] == lit); /*FW1*/ w.blit = c->literals[new_blit_pos]; if (w.binary ()) *j++ = w; @@ -254,8 +258,8 @@ void Internal::update_reason_references () { if (c == external_reason) continue; LOG (c, "updating assigned %d reason", lit); - assert (c->reason); - assert (c->moved); + CADICAL_assert (c->reason); + CADICAL_assert (c->moved); Clause *d = c->copy; v.reason = d; #ifdef LOGGING @@ -277,7 +281,7 @@ void Internal::delete_garbage_clauses () { flush_all_occs_and_watches (); LOG ("deleting garbage clauses"); -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t collected_bytes = 0, collected_clauses = 0; #endif const auto end = clauses.end (); @@ -286,7 +290,7 @@ void Internal::delete_garbage_clauses () { Clause *c = *j++ = *i++; if (!c->collect ()) continue; -#ifndef QUIET +#ifndef CADICAL_QUIET collected_bytes += c->bytes (); collected_clauses++; #endif @@ -310,7 +314,7 @@ void Internal::delete_garbage_clauses () { // void Internal::copy_clause (Clause *c) { LOG (c, "moving"); - assert (!c->moved); + CADICAL_assert (!c->moved); char *p = (char *) c; char *q = arena.copy (p, c->bytes ()); c->copy = (Clause *) q; @@ -390,7 +394,7 @@ void Internal::copy_non_garbage_clauses () { // the decision queue and also uses saved phases. It seems faster than // the MiniSAT version and thus we keep 'opts.arenatype == 3'. - assert (opts.arenatype == 3); + CADICAL_assert (opts.arenatype == 3); for (int sign = -1; sign <= 1; sign += 2) for (int idx = queue.last; idx; idx = link (idx).prev) @@ -418,7 +422,7 @@ void Internal::copy_non_garbage_clauses () { if (c->collect ()) delete_clause (c); else - assert (c->moved), *j++ = c->copy, deallocate_clause (c); + CADICAL_assert (c->moved), *j++ = c->copy, deallocate_clause (c); } clauses.resize (j - clauses.begin ()); if (clauses.size () < clauses.capacity () / 2) @@ -446,7 +450,7 @@ void Internal::copy_non_garbage_clauses () { // statistics are updated correctly. void Internal::check_clause_stats () { -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG int64_t irredundant = 0, redundant = 0, total = 0, irrlits = 0; for (const auto &c : clauses) { if (c->garbage) @@ -459,10 +463,10 @@ void Internal::check_clause_stats () { irrlits += c->size; total++; } - assert (stats.current.irredundant == irredundant); - assert (stats.current.redundant == redundant); - assert (stats.current.total == total); - assert (stats.irrlits == irrlits); + CADICAL_assert (stats.current.irredundant == irredundant); + CADICAL_assert (stats.current.redundant == redundant); + CADICAL_assert (stats.current.total == total); + CADICAL_assert (stats.irrlits == irrlits); #endif } @@ -481,7 +485,7 @@ void Internal::remove_garbage_binaries () { Watches saved; for (auto v : vars) { for (auto lit : {-v, v}) { - assert (saved.empty ()); + CADICAL_assert (saved.empty ()); Watches &ws = watches (lit); const const_watch_iterator end = ws.end (); watch_iterator j = ws.begin (); @@ -495,7 +499,7 @@ void Internal::remove_garbage_binaries () { continue; if (c->reason && c->garbage) { COVER (true); - assert (c->size == 2); + CADICAL_assert (c->size == 2); backtrack_level = min (backtrack_level, var (c->literals[0]).level); LOG ("need to backtrack to before level %d", backtrack_level); @@ -543,3 +547,5 @@ void Internal::garbage_collection () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/compact.cpp b/src/sat/cadical/cadical_compact.cpp similarity index 87% rename from src/sat/cadical/compact.cpp rename to src/sat/cadical/cadical_compact.cpp index 1bf423993..6747218cb 100644 --- a/src/sat/cadical/compact.cpp +++ b/src/sat/cadical/cadical_compact.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -18,7 +22,7 @@ bool Internal::compacting () { if (stats.conflicts < lim.compact) return false; int inactive = max_var - active (); - assert (inactive >= 0); + CADICAL_assert (inactive >= 0); if (!inactive) return false; if (inactive < opts.compactmin) @@ -52,7 +56,7 @@ struct Mapper { table = new int[internal->max_var + 1u]; clear_n (table, internal->max_var + 1u); - assert (!internal->level); + CADICAL_assert (!internal->level); for (auto src : internal->vars) { const Flags &f = internal->flags (src); @@ -72,10 +76,10 @@ struct Mapper { // Map old variable indices. A result of zero means not mapped. // int map_idx (int src) { - assert (0 < src); - assert (src <= internal->max_var); + CADICAL_assert (0 < src); + CADICAL_assert (src <= internal->max_var); const int res = table[src]; - assert (res <= new_max_var); + CADICAL_assert (res <= new_max_var); return res; } @@ -89,14 +93,14 @@ struct Mapper { if (!res) { const signed char tmp = internal->val (src); if (tmp) { - assert (first_fixed); + CADICAL_assert (first_fixed); res = map_first_fixed; if (tmp != first_fixed_val) res = -res; } } else if ((src) < 0) res = -res; - assert (abs (res) <= new_max_var); + CADICAL_assert (abs (res) <= new_max_var); return res; } @@ -108,8 +112,8 @@ struct Mapper { const int dst = map_idx (src); if (!dst) continue; - assert (0 < dst); - assert (dst <= src); + CADICAL_assert (0 < dst); + CADICAL_assert (dst <= src); v[dst] = v[src]; } v.resize (new_vsize); @@ -124,8 +128,8 @@ struct Mapper { const int dst = map_idx (src); if (!dst) continue; - assert (0 < dst); - assert (dst <= src); + CADICAL_assert (0 < dst); + CADICAL_assert (dst <= src); v[2 * dst] = v[2 * src]; v[2 * dst + 1] = v[2 * src + 1]; } @@ -143,7 +147,7 @@ struct Mapper { for (; i != end; i++) { const int src = *i; int dst = map_idx (abs (src)); - assert (abs (dst) <= abs (src)); + CADICAL_assert (abs (dst) <= abs (src)); if (!dst) continue; if (src < 0) @@ -163,19 +167,19 @@ void Internal::compact () { START (compact); - assert (active () < max_var); + CADICAL_assert (active () < max_var); stats.compacts++; - assert (!level); - assert (!unsat); - assert (!conflict); - assert (clause.empty ()); - assert (levels.empty ()); - assert (analyzed.empty ()); - assert (minimized.empty ()); - assert (control.size () == 1); - assert (propagated == trail.size ()); + CADICAL_assert (!level); + CADICAL_assert (!unsat); + CADICAL_assert (!conflict); + CADICAL_assert (clause.empty ()); + CADICAL_assert (levels.empty ()); + CADICAL_assert (analyzed.empty ()); + CADICAL_assert (minimized.empty ()); + CADICAL_assert (control.size () == 1); + CADICAL_assert (propagated == trail.size ()); garbage_collection (); @@ -188,14 +192,14 @@ void Internal::compact () { LOG ("no variable fixed"); if (!assumptions.empty ()) { - assert (!external->assumptions.empty ()); + CADICAL_assert (!external->assumptions.empty ()); LOG ("temporarily reset internal assumptions"); reset_assumptions (); } const bool is_constraint = !constraint.empty (); if (is_constraint) { - assert (!external->constraint.empty ()); + CADICAL_assert (!external->constraint.empty ()); LOG ("temporarily reset internal constraint"); reset_constraint (); } @@ -213,11 +217,11 @@ void Internal::compact () { continue; } if (lrat || frat) { - assert (eidx > 0); - assert (external->ext_units.size () >= (size_t) 2 * eidx + 1); + CADICAL_assert (eidx > 0); + CADICAL_assert (external->ext_units.size () >= (size_t) 2 * eidx + 1); int64_t id1 = external->ext_units[2 * eidx]; int64_t id2 = external->ext_units[2 * eidx + 1]; - assert (!id1 || !id2); + CADICAL_assert (!id1 || !id2); if (!id1 && !id2) { int64_t new_id1 = unit_clauses (2 * src); int64_t new_id2 = unit_clauses (2 * src + 1); @@ -237,7 +241,7 @@ void Internal::compact () { if (lrat || frat) { for (auto src : internal->vars) { const int dst = mapper.map_idx (src); - assert (dst <= src); + CADICAL_assert (dst <= src); const signed char tmp = internal->val (src); if (!dst && !tmp) { unit_clauses (2 * src) = 0; @@ -245,10 +249,10 @@ void Internal::compact () { continue; } if (!tmp || src == mapper.first_fixed) { - assert (0 < dst); + CADICAL_assert (0 < dst); if (dst == src) continue; - assert (!unit_clauses (2 * dst) && !unit_clauses (2 * dst + 1)); + CADICAL_assert (!unit_clauses (2 * dst) && !unit_clauses (2 * dst + 1)); unit_clauses (2 * dst) = unit_clauses (2 * src); unit_clauses (2 * dst + 1) = unit_clauses (2 * src + 1); unit_clauses (2 * src) = 0; @@ -263,7 +267,7 @@ void Internal::compact () { } unit_clauses (2 * src) = 0; unit_clauses (2 * src + 1) = 0; - assert (id); + CADICAL_assert (id); } unit_clauses_idx.resize (2 * mapper.new_vsize); shrink_vector (unit_clauses_idx); @@ -271,12 +275,12 @@ void Internal::compact () { // Map the literals in all clauses. // for (const auto &c : clauses) { - assert (!c->garbage); + CADICAL_assert (!c->garbage); for (auto &src : *c) { - assert (!val (src)); + CADICAL_assert (!val (src)); int dst; dst = mapper.map_lit (src); - assert (dst || c->garbage); + CADICAL_assert (dst || c->garbage); src = dst; } } @@ -299,7 +303,7 @@ void Internal::compact () { const int dst = mapper.map_idx (idx); if (!dst) continue; - assert (active (idx)); + CADICAL_assert (active (idx)); if (prev) links[prev].next = dst; else @@ -319,15 +323,15 @@ void Internal::compact () { // In the second part we map, flush and shrink arrays. /*======================================================================*/ - assert (trail.size () == num_assigned); + CADICAL_assert (trail.size () == num_assigned); mapper.map_flush_and_shrink_lits (trail); propagated = trail.size (); num_assigned = trail.size (); if (mapper.first_fixed) { - assert (trail.size () == 1); + CADICAL_assert (trail.size () == 1); var (mapper.first_fixed).trail = 0; // before mapping 'vtab' } else - assert (trail.empty ()); + CADICAL_assert (trail.empty ()); if (!probes.empty ()) mapper.map_flush_and_shrink_lits (probes); @@ -360,7 +364,7 @@ void Internal::compact () { continue; if (src == dst) continue; - assert (dst < src); + CADICAL_assert (dst < src); if ((size_t) src >= frozentab.size ()) break; if ((size_t) dst >= frozentab.size ()) @@ -380,7 +384,7 @@ void Internal::compact () { continue; if (src == dst) continue; - assert (dst < src); + CADICAL_assert (dst < src); relevanttab[dst] += relevanttab[src]; relevanttab[src] = 0; @@ -394,12 +398,12 @@ void Internal::compact () { if (!external->assumptions.empty ()) { for (const auto &elit : external->assumptions) { - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); int eidx = abs (elit); - assert (eidx <= external->max_var); + CADICAL_assert (eidx <= external->max_var); int ilit = external->e2i[eidx]; - assert (ilit); // Because we froze all!!! + CADICAL_assert (ilit); // Because we froze all!!! if (elit < 0) ilit = -ilit; assume (ilit); @@ -428,14 +432,14 @@ void Internal::compact () { // 'constrain' uses 'val', so this code has to be after remapping that if (is_constraint) { - assert (!level); - assert (!external->constraint.back ()); + CADICAL_assert (!level); + CADICAL_assert (!external->constraint.back ()); for (auto elit : external->constraint) { - assert (elit != INT_MIN); + CADICAL_assert (elit != INT_MIN); int eidx = abs (elit); - assert (eidx <= external->max_var); + CADICAL_assert (eidx <= external->max_var); int ilit = external->e2i[eidx]; - assert (!ilit == !elit); + CADICAL_assert (!ilit == !elit); if (elit < 0) ilit = -ilit; LOG ("re adding lit external %d internal %d to constraint", elit, @@ -475,7 +479,7 @@ void Internal::compact () { // elements with 'zero' destination should be flushed. vector saved; - assert (saved.empty ()); + CADICAL_assert (saved.empty ()); if (!scores.empty ()) { while (!scores.empty ()) { const int src = scores.front (); @@ -549,3 +553,5 @@ void Internal::compact () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/condition.cpp b/src/sat/cadical/cadical_condition.cpp similarity index 91% rename from src/sat/cadical/condition.cpp rename to src/sat/cadical/cadical_condition.cpp index 7fd70a465..8c2f72cf7 100644 --- a/src/sat/cadical/condition.cpp +++ b/src/sat/cadical/cadical_condition.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -39,7 +43,7 @@ bool Internal::conditioning () { if (!preprocessing && !opts.inprocessing) return false; if (preprocessing) - assert (lim.preprocessing); + CADICAL_assert (lim.preprocessing); // Triggered in regular 'opts.conditionint' conflict intervals. // @@ -79,13 +83,13 @@ bool Internal::conditioning () { void Internal::condition_unassign (int lit) { LOG ("condition unassign %d", lit); - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); set_val (lit, 0); } void Internal::condition_assign (int lit) { LOG ("condition assign %d", lit); - assert (!val (lit)); + CADICAL_assert (!val (lit)); set_val (lit, 1); } @@ -104,15 +108,15 @@ inline bool Internal::is_autarky_literal (int lit) const { inline void Internal::mark_as_conditional_literal (int lit) { LOG ("marking %d as conditional literal", lit); - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); setbit (lit, 0); - assert (is_conditional_literal (lit)); - assert (!is_autarky_literal (lit)); + CADICAL_assert (is_conditional_literal (lit)); + CADICAL_assert (!is_autarky_literal (lit)); } inline void Internal::unmark_as_conditional_literal (int lit) { LOG ("unmarking %d as conditional literal", lit); - assert (is_conditional_literal (lit)); + CADICAL_assert (is_conditional_literal (lit)); unsetbit (lit, 0); } @@ -130,13 +134,13 @@ inline bool Internal::is_in_candidate_clause (int lit) const { inline void Internal::mark_in_candidate_clause (int lit) { LOG ("marking %d as literal of the candidate clause", lit); mark67 (lit); - assert (is_in_candidate_clause (lit)); - assert (!is_in_candidate_clause (-lit)); + CADICAL_assert (is_in_candidate_clause (lit)); + CADICAL_assert (!is_in_candidate_clause (-lit)); } inline void Internal::unmark_in_candidate_clause (int lit) { LOG ("unmarking %d as literal of the candidate clause", lit); - assert (is_in_candidate_clause (lit)); + CADICAL_assert (is_in_candidate_clause (lit)); unmark67 (lit); } @@ -155,7 +159,7 @@ struct less_conditioned { long Internal::condition_round (long delta) { long limit; -#ifndef QUIET +#ifndef CADICAL_QUIET long props = 0; #endif if (LONG_MAX - delta < stats.condprops) @@ -170,7 +174,7 @@ long Internal::condition_round (long delta) { protect_reasons (); -#if defined(LOGGING) || !defined(NDEBUG) +#if defined(LOGGING) || !defined(CADICAL_NDEBUG) int additionally_assigned = 0; #endif @@ -202,7 +206,7 @@ long Internal::condition_round (long delta) { condition_assign (lit); v.level = level; trail.push_back (lit); -#if defined(LOGGING) || !defined(NDEBUG) +#if defined(LOGGING) || !defined(CADICAL_NDEBUG) additionally_assigned++; #endif } @@ -260,7 +264,7 @@ long Internal::condition_round (long delta) { vector conditional; vector candidates; // Gather candidate clauses. -#ifndef QUIET +#ifndef CADICAL_QUIET size_t watched = 0; // Number of watched clauses. #endif @@ -350,12 +354,12 @@ long Internal::condition_round (long delta) { // if (negative > 0 && positive > 0) { LOG (c, "found %d negative literals in candidate", negative); - assert (watch); - assert (val (watch) > 0); + CADICAL_assert (watch); + CADICAL_assert (val (watch) > 0); Occs &os = occs (watch); - assert (os.size () == minsize); + CADICAL_assert (os.size () == minsize); os.push_back (c); -#ifndef QUIET +#ifndef CADICAL_QUIET watched++; #endif LOG (c, "watching %d with %zd occurrences in", watch, minsize); @@ -375,7 +379,7 @@ long Internal::condition_round (long delta) { signed char tmp = val (lit); if (!tmp) continue; - assert (tmp < 0); + CADICAL_assert (tmp < 0); if (!var (lit).level) continue; // Not unassigned yet! if (is_conditional_literal (-lit)) @@ -389,7 +393,7 @@ long Internal::condition_round (long delta) { new_conditionals); initial.conditional += new_conditionals; - assert (initial.autarky >= new_conditionals); + CADICAL_assert (initial.autarky >= new_conditionals); initial.autarky -= new_conditionals; } @@ -408,14 +412,14 @@ long Internal::condition_round (long delta) { #ifdef LOGGING for (size_t i = 0; i < conditional.size (); i++) { LOG ("initial conditional %d", conditional[i]); - assert (is_conditional_literal (conditional[i])); + CADICAL_assert (is_conditional_literal (conditional[i])); } for (size_t i = 0; i < trail.size (); i++) if (is_autarky_literal (trail[i])) LOG ("initial autarky %d", trail[i]); #endif - assert (initial.conditional == conditional.size ()); - assert (initial.assigned == initial.conditional + initial.autarky); + CADICAL_assert (initial.conditional == conditional.size ()); + CADICAL_assert (initial.assigned == initial.conditional + initial.autarky); stats.condassinit += initial.assigned; stats.condcondinit += initial.conditional; @@ -448,7 +452,7 @@ long Internal::condition_round (long delta) { // // TODO consider computing conditioned and unconditioned over all clauses. // - assert (conditioned + unconditioned == candidates.size ()); + CADICAL_assert (conditioned + unconditioned == candidates.size ()); if (conditioned && unconditioned) { stable_sort (candidates.begin (), candidates.end (), less_conditioned ()); @@ -457,13 +461,13 @@ long Internal::condition_round (long delta) { unconditioned, percent (unconditioned, candidates.size ())); } else if (conditioned && !unconditioned) { for (auto const &c : candidates) { - assert (c->conditioned); + CADICAL_assert (c->conditioned); c->conditioned = false; // Reset 'conditioned' bit. } PHASE ("condition", stats.conditionings, "all %zd candidates tried before", conditioned); } else { - assert (!conditioned); + CADICAL_assert (!conditioned); PHASE ("condition", stats.conditionings, "all %zd candidates are fresh", unconditioned); } @@ -478,7 +482,7 @@ long Internal::condition_round (long delta) { // long blocked = 0; // Number of Successfully blocked clauses. // -#ifndef QUIET +#ifndef CADICAL_QUIET size_t untried = candidates.size (); #endif for (const auto &c : candidates) { @@ -503,11 +507,11 @@ long Internal::condition_round (long delta) { untried, percent (untried, candidates.size ()), props); break; } -#ifndef QUIET +#ifndef CADICAL_QUIET untried--; #endif - assert (!c->garbage); - assert (!c->redundant); + CADICAL_assert (!c->garbage); + CADICAL_assert (!c->redundant); LOG (c, "candidate"); c->conditioned = 1; // Next time later. @@ -559,17 +563,17 @@ long Internal::condition_round (long delta) { size_t conditional, unassigned; } next = {0, 0}; - assert (unassigned.empty ()); - assert (conditional.size () == initial.conditional); + CADICAL_assert (unassigned.empty ()); + CADICAL_assert (conditional.size () == initial.conditional); while (watched_autarky_literal && stats.condprops < limit && next.conditional < conditional.size ()) { - assert (next.unassigned == unassigned.size ()); + CADICAL_assert (next.unassigned == unassigned.size ()); const int conditional_lit = conditional[next.conditional++]; LOG ("processing next conditional %d", conditional_lit); - assert (is_conditional_literal (conditional_lit)); + CADICAL_assert (is_conditional_literal (conditional_lit)); if (is_in_candidate_clause (-conditional_lit)) { LOG ("conditional %d negated in candidate clause", conditional_lit); @@ -580,11 +584,11 @@ long Internal::condition_round (long delta) { conditional_lit); condition_unassign (conditional_lit); - assert (!is_conditional_literal (conditional_lit)); + CADICAL_assert (!is_conditional_literal (conditional_lit)); unassigned.push_back (conditional_lit); - assert (remain.assigned > 0); - assert (remain.conditional > 0); + CADICAL_assert (remain.assigned > 0); + CADICAL_assert (remain.conditional > 0); remain.conditional--; remain.assigned--; @@ -592,8 +596,8 @@ long Internal::condition_round (long delta) { next.unassigned < unassigned.size ()) { const int unassigned_lit = unassigned[next.unassigned++]; LOG ("processing next unassigned %d", unassigned_lit); - assert (!val (unassigned_lit)); -#ifndef QUIET + CADICAL_assert (!val (unassigned_lit)); +#ifndef CADICAL_QUIET props++; #endif stats.condprops++; @@ -635,7 +639,7 @@ long Internal::condition_round (long delta) { i--; // Drop watch! LOG (d, "watching %d in", replacement); - assert (replacement != unassigned_lit); + CADICAL_assert (replacement != unassigned_lit); occs (replacement).push_back (d); continue; // ... with next watched clause 'd'. @@ -661,7 +665,7 @@ long Internal::condition_round (long delta) { conditional.push_back (-lit); remain.conditional++; - assert (remain.autarky > 0); + CADICAL_assert (remain.autarky > 0); remain.autarky--; if (-lit != watched_autarky_literal) @@ -713,9 +717,9 @@ long Internal::condition_round (long delta) { LOG ("remaining conditional part of size %zd", remain.conditional); LOG ("remaining autarky part of size %zd", remain.autarky); // - assert (remain.assigned - remain.conditional == remain.autarky); + CADICAL_assert (remain.assigned - remain.conditional == remain.autarky); // -#if defined(LOGGING) || !defined(NDEBUG) +#if defined(LOGGING) || !defined(CADICAL_NDEBUG) // // This is a sanity check, that the size of our implicit representation // of the autarky part matches our 'remain' counts. We need the same @@ -732,21 +736,21 @@ long Internal::condition_round (long delta) { check.assigned++; if (is_conditional_literal (lit)) { LOG ("remaining conditional %d", lit); - assert (!is_autarky_literal (lit)); + CADICAL_assert (!is_autarky_literal (lit)); check.conditional++; } else { - assert (is_autarky_literal (lit)); + CADICAL_assert (is_autarky_literal (lit)); LOG ("remaining autarky %d", lit); check.autarky++; } } else { - assert (!is_autarky_literal (lit)); - assert (!is_conditional_literal (lit)); + CADICAL_assert (!is_autarky_literal (lit)); + CADICAL_assert (!is_conditional_literal (lit)); } } - assert (remain.assigned == check.assigned); - assert (remain.conditional == check.conditional); - assert (remain.autarky == check.autarky); + CADICAL_assert (remain.assigned == check.assigned); + CADICAL_assert (remain.conditional == check.conditional); + CADICAL_assert (remain.autarky == check.autarky); #endif // Success if an autarky literal is left in the clause and @@ -754,8 +758,8 @@ long Internal::condition_round (long delta) { // limit was hit. // if (watched_autarky_literal && stats.condprops < limit) { - assert (is_autarky_literal (watched_autarky_literal)); - assert (is_in_candidate_clause (watched_autarky_literal)); + CADICAL_assert (is_autarky_literal (watched_autarky_literal)); + CADICAL_assert (is_in_candidate_clause (watched_autarky_literal)); blocked++; stats.conditioned++; @@ -848,20 +852,20 @@ long Internal::condition_round (long delta) { // Unassign additionally assigned literals. // -#if defined(LOGGING) || !defined(NDEBUG) +#if defined(LOGGING) || !defined(CADICAL_NDEBUG) int additionally_unassigned = 0; #endif while (trail.size () > initial_trail_level) { int lit = trail.back (); trail.pop_back (); condition_unassign (lit); -#if defined(LOGGING) || !defined(NDEBUG) +#if defined(LOGGING) || !defined(CADICAL_NDEBUG) additionally_unassigned++; #endif } LOG ("unassigned %d additionally assigned literals", additionally_unassigned); - assert (additionally_unassigned == additionally_assigned); + CADICAL_assert (additionally_unassigned == additionally_assigned); if (level > initial_level) { LOG ("reset condition decision level"); @@ -877,14 +881,14 @@ long Internal::condition_round (long delta) { for (size_t i = 0; i < initial_trail_level; i++) { const int lit = trail[i]; const signed char tmp = val (lit); - assert (tmp >= 0); + CADICAL_assert (tmp >= 0); if (!tmp) condition_assign (lit); } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (const auto &lit : trail) - assert (!marked (lit)); + CADICAL_assert (!marked (lit)); #endif unprotect_reasons (); @@ -913,7 +917,7 @@ void Internal::condition (bool update_limits) { limit = opts.conditionmineff; if (limit > opts.conditionmaxeff) limit = opts.conditionmaxeff; - assert (stats.current.irredundant); + CADICAL_assert (stats.current.irredundant); limit *= 2.0 * active () / (double) stats.current.irredundant; limit = max (limit, 2l * active ()); @@ -938,3 +942,5 @@ void Internal::condition (bool update_limits) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/config.cpp b/src/sat/cadical/cadical_config.cpp similarity index 95% rename from src/sat/cadical/config.cpp rename to src/sat/cadical/cadical_config.cpp index f63d1e4b7..d4268790c 100644 --- a/src/sat/cadical/config.cpp +++ b/src/sat/cadical/cadical_config.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -75,7 +79,7 @@ bool Config::set (Options &opts, const char *name) { const NameVal *BEGIN = N##_config; \ const NameVal *END = BEGIN + sizeof N##_config / sizeof (NameVal); \ for (const NameVal *P = BEGIN; P != END; P++) { \ - assert (Options::has (P->name)); \ + CADICAL_assert (Options::has (P->name)); \ opts.set (P->name, P->val); \ } \ return true; \ @@ -99,3 +103,5 @@ const char **Config::begin () { return configs; } const char **Config::end () { return &configs[num_configs]; } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/congruence.cpp b/src/sat/cadical/cadical_congruence.cpp similarity index 86% rename from src/sat/cadical/congruence.cpp rename to src/sat/cadical/cadical_congruence.cpp index 4c8de9d52..8fbceee24 100644 --- a/src/sat/cadical/congruence.cpp +++ b/src/sat/cadical/cadical_congruence.cpp @@ -1,3 +1,5 @@ +#include "global.h" + #include "congruence.hpp" #include "internal.hpp" #include @@ -6,6 +8,8 @@ #include #include +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { Closure::Closure (Internal *i) @@ -45,52 +49,52 @@ void update_ite_flags (Gate *g) { f |= Special_ITE_GATE::NO_NEG_ELSE; } g->degenerated_ite = f; - assert (lhs != -then_lit); - assert (lhs != -else_lit); - assert (cond != then_lit); - assert (cond != else_lit); - assert (cond != -then_lit); - assert (cond != -else_lit); + CADICAL_assert (lhs != -then_lit); + CADICAL_assert (lhs != -else_lit); + CADICAL_assert (cond != then_lit); + CADICAL_assert (cond != else_lit); + CADICAL_assert (cond != -then_lit); + CADICAL_assert (cond != -else_lit); } void check_correct_ite_flags (const Gate *const g) { -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG const int8_t f = g->degenerated_ite; const int lhs = g->lhs; const int cond = g->rhs [0]; const int then_lit = g->rhs[1]; const int else_lit = g->rhs[2]; - assert (g->pos_lhs_ids.size () == 4); + CADICAL_assert (g->pos_lhs_ids.size () == 4); if (g->pos_lhs_ids[0].clause == nullptr) - assert ((f & Special_ITE_GATE::NO_PLUS_THEN)); + CADICAL_assert ((f & Special_ITE_GATE::NO_PLUS_THEN)); if (g->pos_lhs_ids[1].clause == nullptr) - assert (f & Special_ITE_GATE::NO_NEG_THEN); + CADICAL_assert (f & Special_ITE_GATE::NO_NEG_THEN); if (g->pos_lhs_ids[2].clause == nullptr) - assert (f & Special_ITE_GATE::NO_PLUS_ELSE); + CADICAL_assert (f & Special_ITE_GATE::NO_PLUS_ELSE); if (g->pos_lhs_ids[3].clause == nullptr) - assert (f & Special_ITE_GATE::NO_NEG_ELSE); + CADICAL_assert (f & Special_ITE_GATE::NO_NEG_ELSE); if (lhs == cond) { - assert (f & Special_ITE_GATE::NO_NEG_THEN); - assert (f & Special_ITE_GATE::NO_PLUS_ELSE); + CADICAL_assert (f & Special_ITE_GATE::NO_NEG_THEN); + CADICAL_assert (f & Special_ITE_GATE::NO_PLUS_ELSE); } if (lhs == -cond) { - assert (f & Special_ITE_GATE::NO_PLUS_THEN); - assert (f & Special_ITE_GATE::NO_NEG_ELSE); + CADICAL_assert (f & Special_ITE_GATE::NO_PLUS_THEN); + CADICAL_assert (f & Special_ITE_GATE::NO_NEG_ELSE); } if (lhs == then_lit) { - assert (f & Special_ITE_GATE::NO_PLUS_THEN); - assert (f & Special_ITE_GATE::NO_NEG_THEN); + CADICAL_assert (f & Special_ITE_GATE::NO_PLUS_THEN); + CADICAL_assert (f & Special_ITE_GATE::NO_NEG_THEN); } if (lhs == else_lit) { - assert (f & Special_ITE_GATE::NO_PLUS_ELSE); - assert (f & Special_ITE_GATE::NO_NEG_ELSE); + CADICAL_assert (f & Special_ITE_GATE::NO_PLUS_ELSE); + CADICAL_assert (f & Special_ITE_GATE::NO_NEG_ELSE); } - assert (lhs != -then_lit); - assert (lhs != -else_lit); - assert (cond != then_lit); - assert (cond != else_lit); - assert (cond != -then_lit); - assert (cond != -else_lit); + CADICAL_assert (lhs != -then_lit); + CADICAL_assert (lhs != -else_lit); + CADICAL_assert (cond != then_lit); + CADICAL_assert (cond != else_lit); + CADICAL_assert (cond != -then_lit); + CADICAL_assert (cond != -else_lit); #else (void)g; #endif @@ -116,7 +120,7 @@ static size_t hash_lits (std::array &nonces, } size_t Hash::operator() (const Gate *const g) const { - assert (hash_lits (nonces, g->rhs) == g->hash); + CADICAL_assert (hash_lits (nonces, g->rhs) == g->hash); return g->hash; } @@ -144,7 +148,7 @@ bool Closure::find_binary (int lit, int other) const { const auto size = offsize.second; const auto begin = std::begin (binaries) + offset; const auto end = std::begin (binaries) + size; - assert (end <= std::end (binaries)); + CADICAL_assert (end <= std::end (binaries)); const CompactBinary target = CompactBinary (nullptr, 0, lit, other); auto it = std::lower_bound (begin, end, target, compact_binary_order ()); // search_binary only returns a bool @@ -172,7 +176,7 @@ void Closure::extract_binaries () { continue; if (c->size > 2) continue; - assert (c->size == 2); + CADICAL_assert (c->size == 2); const int lit = c->literals[0]; const int other = c->literals[1]; const bool already_sorted = @@ -195,8 +199,8 @@ void Closure::extract_binaries () { while (j < size && binaries[j].lit1 == lit) { ++j; } - assert (j >= i); - assert (j <= size); + CADICAL_assert (j >= i); + CADICAL_assert (j <= size); offsetsize[internal->vlit (lit)] = make_pair (i, j); i = j; } @@ -257,7 +261,7 @@ void Closure::extract_binaries () { { size_t i = 0; for (size_t j = 1; j < new_size; ++j) { - assert (i < j); + CADICAL_assert (i < j); if (binaries[i].lit1 == binaries[j].lit1 && binaries[i].lit2 == binaries[j].lit2) { // subsuming later clause @@ -268,7 +272,7 @@ void Closure::extract_binaries () { binaries[++i] = binaries[j]; } } - assert (i <= new_size); + CADICAL_assert (i <= new_size); binaries.resize (i); } binaries.clear (); @@ -281,26 +285,26 @@ void Closure::extract_binaries () { /*------------------------------------------------------------------------*/ // marking structure for congruence closure, by reference signed char &Closure::marked (int lit) { - assert (internal->vlit (lit) < marks.size ()); + CADICAL_assert (internal->vlit (lit) < marks.size ()); return marks[internal->vlit (lit)]; } void Closure::unmark_all () { for (auto lit : internal->analyzed) { - assert (marked (lit)); + CADICAL_assert (marked (lit)); marked (lit) = 0; } internal->analyzed.clear (); } void Closure::set_mu1_reason (int lit, Clause *c) { - assert (marked (lit) & 1); + CADICAL_assert (marked (lit) & 1); LOG (c, "mu1 %d -> %zd", lit, c->id); mu1_ids[internal->vlit (lit)] = LitClausePair (lit, c); } void Closure::set_mu2_reason (int lit, Clause *c) { - assert (marked (lit) & 2); + CADICAL_assert (marked (lit) & 2); if (!internal->lrat) return; LOG (c, "mu2 %d -> %zd", lit, c->id); @@ -308,7 +312,7 @@ void Closure::set_mu2_reason (int lit, Clause *c) { } void Closure::set_mu4_reason (int lit, Clause *c) { - assert (marked (lit) & 4); + CADICAL_assert (marked (lit) & 4); if (!internal->lrat) return; LOG (c, "mu4 %d -> %zd", lit, c->id); @@ -401,21 +405,21 @@ void Closure::sort_literals_by_var (vector &rhs) { } /*------------------------------------------------------------------------*/ int &Closure::representative (int lit) { - assert (internal->vlit (lit) < representant.size ()); + CADICAL_assert (internal->vlit (lit) < representant.size ()); return representant[internal->vlit (lit)]; } int Closure::representative (int lit) const { - assert (internal->vlit (lit) < representant.size ()); + CADICAL_assert (internal->vlit (lit) < representant.size ()); return representant[internal->vlit (lit)]; } int &Closure::eager_representative (int lit) { - assert (internal->vlit (lit) < eager_representant.size ()); + CADICAL_assert (internal->vlit (lit) < eager_representant.size ()); return eager_representant[internal->vlit (lit)]; } int Closure::eager_representative (int lit) const { - assert (internal->vlit (lit) < eager_representant.size ()); + CADICAL_assert (internal->vlit (lit) < eager_representant.size ()); return eager_representant[internal->vlit (lit)]; } @@ -479,7 +483,7 @@ int Closure::find_representative_and_compress (int lit, bool update_eager) { eager_representative (lit) = res; if (internal->lrat) eager_representative_id (lit) = representative_id (lit); - assert (!internal->lrat || eager_representative_id (lit)); + CADICAL_assert (!internal->lrat || eager_representative_id (lit)); } } @@ -493,7 +497,7 @@ int Closure::find_representative_and_compress (int lit, bool update_eager) { void Closure::push_lrat_unit (int lit) { if (!internal->lrat) return; - assert (internal->val (lit) > 0); + CADICAL_assert (internal->val (lit) > 0); LRAT_ID id = internal->unit_id (lit); lrat_chain.push_back (id); } @@ -521,7 +525,7 @@ int Closure::find_eager_representative_and_compress (int lit) { ++path_length; } while (nxt != res); - // assert (res == find_representative (lit)); + // CADICAL_assert (res == find_representative (lit)); // we have to do path compression to support LRAT proofs if (path_length > 2) { LOG ("learning new rewriting from %d to %d (current path length: %d)", @@ -545,10 +549,10 @@ int Closure::find_eager_representative_and_compress (int lit) { } else if (path_length == 2) { LOG ("duplicated information %d -> %d to eager with clause %" PRIu64, lit, res, eager_representative_id (lit)); - assert (eager_representative (lit) == res); - assert (!internal->lrat || eager_representative_id (lit)); + CADICAL_assert (eager_representative (lit) == res); + CADICAL_assert (!internal->lrat || eager_representative_id (lit)); } - assert (internal->clause.empty ()); + CADICAL_assert (internal->clause.empty ()); return res; } @@ -566,14 +570,14 @@ void Closure::import_lazy_and_find_eager_representative_and_compress_both ( } void Closure::produce_representative_lrat (int lit) { - assert (internal->lrat); + CADICAL_assert (internal->lrat); LOG ("production of LRAT chain for %d with representative %" PRIu64, lit, representative_id (lit)); - assert (internal->lrat); - assert (lrat_chain.empty ()); + CADICAL_assert (internal->lrat); + CADICAL_assert (lrat_chain.empty ()); int res = lit; int nxt = lit; - assert (nxt != representative (nxt)); + CADICAL_assert (nxt != representative (nxt)); do { res = nxt; nxt = representative (nxt); @@ -585,14 +589,14 @@ void Closure::produce_representative_lrat (int lit) { } void Closure::produce_eager_representative_lrat (int lit) { - assert (internal->lrat); + CADICAL_assert (internal->lrat); LOG ("production of LRAT chain for %d with representative %" PRIu64, lit, eager_representative_id (lit)); - assert (internal->lrat); - assert (lrat_chain.empty ()); + CADICAL_assert (internal->lrat); + CADICAL_assert (lrat_chain.empty ()); int res = lit; int nxt = lit; - assert (nxt != eager_representative (nxt)); + CADICAL_assert (nxt != eager_representative (nxt)); do { res = nxt; nxt = eager_representative (nxt); @@ -607,13 +611,13 @@ LRAT_ID Closure::find_representative_lrat (int lit) { if (!internal->lrat) return 0; int res = lit; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG int nxt = representative (res); - assert (nxt == representative (res)); + CADICAL_assert (nxt == representative (res)); #endif LOG ("checking for existing LRAT chain for %d with clause %" PRIu64, lit, eager_representative_id (res)); - assert (representative_id (res)); + CADICAL_assert (representative_id (res)); return representative_id (res); } @@ -621,44 +625,44 @@ LRAT_ID Closure::find_eager_representative_lrat (int lit) { if (!internal->lrat) return 0; int res = lit; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG int nxt = eager_representative (res); - assert (nxt == eager_representative (res)); + CADICAL_assert (nxt == eager_representative (res)); #endif LOG ("checking for existing LRAT chain for %d with clause %" PRIu64, lit, eager_representative_id (res)); - assert (eager_representative_id (res)); + CADICAL_assert (eager_representative_id (res)); return eager_representative_id (res); } LRAT_ID &Closure::eager_representative_id (int lit) { - assert (internal->vlit (lit) < eager_representant_id.size ()); + CADICAL_assert (internal->vlit (lit) < eager_representant_id.size ()); return eager_representant_id[internal->vlit (lit)]; } LRAT_ID Closure::eager_representative_id (int lit) const { - assert (internal->vlit (lit) < eager_representant_id.size ()); + CADICAL_assert (internal->vlit (lit) < eager_representant_id.size ()); return eager_representant_id[internal->vlit (lit)]; } LRAT_ID &Closure::representative_id (int lit) { - assert (internal->vlit (lit) < representant_id.size ()); + CADICAL_assert (internal->vlit (lit) < representant_id.size ()); return representant_id[internal->vlit (lit)]; } LRAT_ID Closure::representative_id (int lit) const { - assert (internal->vlit (lit) < representant_id.size ()); + CADICAL_assert (internal->vlit (lit) < representant_id.size ()); return representant_id[internal->vlit (lit)]; } void Closure::mark_garbage (Gate *g) { LOG (g, "marking as garbage"); - assert (!g->garbage); + CADICAL_assert (!g->garbage); g->garbage = true; garbage.push_back (g); } bool Closure::remove_gate (GatesTable::iterator git) { - assert (git != end (table)); - assert (!internal->unsat); + CADICAL_assert (git != end (table)); + CADICAL_assert (!internal->unsat); (*git)->indexed = false; LOG ((*git), "removing from hash table"); table.erase (git); @@ -668,8 +672,8 @@ bool Closure::remove_gate (GatesTable::iterator git) { bool Closure::remove_gate (Gate *g) { if (!g->indexed) return false; - assert (!internal->unsat); - assert (table.find (g) != end (table)); + CADICAL_assert (!internal->unsat); + CADICAL_assert (table.find (g) != end (table)); table.erase (table.find (g)); g->indexed = false; LOG (g, "removing from hash table"); @@ -677,10 +681,10 @@ bool Closure::remove_gate (Gate *g) { } void Closure::index_gate (Gate *g) { - assert (!g->indexed); - assert (!internal->unsat); - assert (g->arity () > 1); - assert (g->hash == hash_lits (nonces, g->rhs)); + CADICAL_assert (!g->indexed); + CADICAL_assert (!internal->unsat); + CADICAL_assert (g->arity () > 1); + CADICAL_assert (g->hash == hash_lits (nonces, g->rhs)); LOG (g, "adding to hash table"); table.insert (g); g->indexed = true; @@ -688,9 +692,9 @@ void Closure::index_gate (Gate *g) { void Closure::produce_rewritten_clause_lrat_and_clean ( std::vector &litIds, int except_lhs, bool remove_units) { - assert (internal->lrat_chain.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); for (auto &litId : litIds) { - assert (litId.clause); + CADICAL_assert (litId.clause); litId.clause = produce_rewritten_clause_lrat (litId.clause, except_lhs, remove_units); litId.current_lit = find_eager_representative (litId.current_lit); @@ -704,11 +708,11 @@ void Closure::produce_rewritten_clause_lrat_and_clean ( void Closure::produce_rewritten_clause_lrat_and_clean ( std::vector &litIds, int except_lhs, size_t &old_position1, size_t &old_position2, bool remove_units) { - assert (internal->lrat_chain.empty ()); - assert (old_position1 != old_position2); + CADICAL_assert (internal->lrat_chain.empty ()); + CADICAL_assert (old_position1 != old_position2); size_t j = 0; for (size_t i = 0; i < litIds.size (); ++i) { - assert (j <= i); + CADICAL_assert (j <= i); litIds[j].clause = produce_rewritten_clause_lrat ( litIds[i].clause, except_lhs, remove_units); litIds[j].current_lit = @@ -732,10 +736,10 @@ void Closure::produce_rewritten_clause_lrat_and_clean ( } void Closure::compute_rewritten_clause_lrat_simple (Clause *c, int except) { - assert (internal->clause.empty ()); - assert (internal->lrat_chain.empty ()); - assert (clause.empty ()); - assert (lrat_chain.empty ()); + CADICAL_assert (internal->clause.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (lrat_chain.empty ()); bool changed = false; bool tautology = false; for (auto lit : *c) { @@ -804,23 +808,23 @@ void Closure::compute_rewritten_clause_lrat_simple (Clause *c, int except) { LOG (lrat_chain, "LRAT chain"); } else { LOG (c, "oops this should not happen"); - assert (false); + CADICAL_assert (false); } } Clause *Closure::new_tmp_clause (std::vector &clause) { - assert (internal->lrat); - assert (!clause.empty ()); - assert (!lrat_chain.empty ()); + CADICAL_assert (internal->lrat); + CADICAL_assert (!clause.empty ()); + CADICAL_assert (!lrat_chain.empty ()); bool clear = false; LOG (clause, "learn new tmp clause"); - assert (clause.size () >= 2); + CADICAL_assert (clause.size () >= 2); internal->external->check_learned_clause (); - assert (internal->clause.size () <= (size_t) INT_MAX); + CADICAL_assert (internal->clause.size () <= (size_t) INT_MAX); const int size = (int) clause.size (); - assert (size >= 2); + CADICAL_assert (size >= 2); size_t bytes = Clause::bytes (size); Clause *c = (Clause *) new char[bytes]; @@ -857,7 +861,7 @@ Clause *Closure::new_tmp_clause (std::vector &clause) { // Just checking that we did not mess up our sophisticated memory layout. // This might be compiler dependent though. Crucial for correctness. // - assert (c->bytes () == bytes); + CADICAL_assert (c->bytes () == bytes); clause_delete.release (); LOG (c, "new pointer %p", (void *) c); @@ -869,12 +873,12 @@ Clause *Closure::new_tmp_clause (std::vector &clause) { internal->proof->add_derived_clause (c, lrat_chain); } extra_clauses.push_back (c); - assert (internal->lrat_chain.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); return c; } Clause *Closure::new_clause () { - assert (internal->clause.empty () || clause.empty ()); + CADICAL_assert (internal->clause.empty () || clause.empty ()); bool clear = false; if (internal->clause.empty ()) { swap (internal->clause, clause); @@ -897,7 +901,7 @@ Clause *Closure::new_clause () { // probably do not need that. void Closure::produce_rewritten_clause_lrat ( std::vector &litIds, int except_lhs, bool remove_units) { - assert (internal->lrat_chain.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); for (auto &litId : litIds) { if (!litId.clause) continue; @@ -909,14 +913,14 @@ void Closure::produce_rewritten_clause_lrat ( Clause *Closure::produce_rewritten_clause_lrat (Clause *c, int except_lhs, bool remove_units, bool fail_on_unit) { - assert (internal->clause.empty ()); - assert (internal->lrat_chain.empty ()); + CADICAL_assert (internal->clause.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); auto tmp_lrat (std::move (lrat_chain)); lrat_chain.clear (); LOG (c, "rewriting clause for LRAT proof, except for rewriting %d", except_lhs); - assert (internal->clause.empty ()); - assert (lrat_chain.empty ()); + CADICAL_assert (internal->clause.empty ()); + CADICAL_assert (lrat_chain.empty ()); bool changed = false; bool tautology = false; for (auto lit : *c) { @@ -978,7 +982,7 @@ Clause *Closure::produce_rewritten_clause_lrat (Clause *c, int except_lhs, lrat_chain.push_back (c->id); Clause *d; - assert (internal->clause.empty ()); + CADICAL_assert (internal->clause.empty ()); if (tautology) { LOG ("generated clause is a tautology"); d = nullptr; @@ -987,7 +991,7 @@ Clause *Closure::produce_rewritten_clause_lrat (Clause *c, int except_lhs, LOG (lrat_chain, "LRAT chain"); if (fail_on_unit) { d = nullptr; - assert (false && "rewriting produced a unit clause"); + CADICAL_assert (false && "rewriting produced a unit clause"); } else { d = c; } @@ -1002,7 +1006,7 @@ Clause *Closure::produce_rewritten_clause_lrat (Clause *c, int except_lhs, } clause.clear (); lrat_chain = std::move (tmp_lrat); - assert (internal->clause.empty ()); + CADICAL_assert (internal->clause.empty ()); return d; } @@ -1025,11 +1029,11 @@ void Closure::push_id_and_rewriting_lrat_unit (Clause *c, Rewrite rewrite1, ", %" PRIu64 ") and skipping %d and %d", rewrite1.src, rewrite1.id1, rewrite1.id2, rewrite2.src, rewrite2.id1, rewrite2.id2, except_lhs, except_lhs2); - assert (c); + CADICAL_assert (c); std::vector units, rewriting; for (auto other : *c) { // unclear how to achieve this in the simplify context where other == - // g->lhs might be set assert (internal->val (other) <= 0 || other == + // g->lhs might be set CADICAL_assert (internal->val (other) <= 0 || other == // except); if (other == except_lhs || other == -except_lhs) { // do nothing; @@ -1048,9 +1052,9 @@ void Closure::push_id_and_rewriting_lrat_unit (Clause *c, Rewrite rewrite1, } else if (other == -rewrite2.src && rewrite2.id2) { push_id_on_chain (rewriting, rewrite2, other); } else if (other != find_eager_representative_and_compress (other)) { -#if defined(LOGGING) || !defined(NDEBUG) +#if defined(LOGGING) || !defined(CADICAL_NDEBUG) const int rewritten_other = eager_representative (other); - assert (other != rewritten_other); + CADICAL_assert (other != rewritten_other); LOG ("reason for representative of %d %d is %" PRIu64 "", other, rewritten_other, find_eager_representative_lrat (other)); #endif @@ -1086,12 +1090,12 @@ void Closure::push_id_and_rewriting_lrat_full (Clause *c, Rewrite rewrite1, ") and skipping %d and %d", rewrite1.src, rewrite1.id1, rewrite1.id2, rewrite2.src, rewrite2.id1, rewrite2.id2, except_lhs, except_lhs2); - assert (c); + CADICAL_assert (c); if (!insert_id_after) chain.push_back (c->id); for (auto other : *c) { // unclear how to achieve this in the simplify context where other == - // g->lhs might be set assert (internal->val (other) <= 0 || other == + // g->lhs might be set CADICAL_assert (internal->val (other) <= 0 || other == // except); if (other == except_lhs) { // do nothing; @@ -1100,7 +1104,7 @@ void Closure::push_id_and_rewriting_lrat_full (Clause *c, Rewrite rewrite1, } else if (internal->val (other) < 0) { LOG ("found unit %d", -other); LRAT_ID id = internal->unit_id (-other); - assert (id); + CADICAL_assert (id); chain.push_back (id); } else if (other == rewrite1.src && rewrite1.id1) { push_id_on_chain (chain, rewrite1, other); @@ -1111,7 +1115,7 @@ void Closure::push_id_and_rewriting_lrat_full (Clause *c, Rewrite rewrite1, } else if (other == -rewrite2.src && rewrite2.id2) { push_id_on_chain (chain, rewrite2, other); } else { - assert (other == find_eager_representative (other)); + CADICAL_assert (other == find_eager_representative (other)); LOG ("no rewriting needed for %d", other); } } @@ -1123,7 +1127,7 @@ void Closure::push_id_and_rewriting_lrat_full (Clause *c, Rewrite rewrite1, // because we can force rewriting that way that have not been done eagerly // yet. void Closure::push_id_on_chain (std::vector &chain, Clause *c) { - assert (c); + CADICAL_assert (c); chain.push_back (c->id); LOG (lrat_chain, "chain"); } @@ -1144,9 +1148,9 @@ void Closure::learn_congruence_unit_when_lhs_set (Gate *g, int src, if (!internal->lrat) return; LOG ("calculating LRAT chain learn_congruence_unit_when_lhs_set"); - assert (!g->pos_lhs_ids.empty ()); - assert (internal->analyzed.empty ()); - assert (internal->val (g->lhs) < 0); + CADICAL_assert (!g->pos_lhs_ids.empty ()); + CADICAL_assert (internal->analyzed.empty ()); + CADICAL_assert (internal->val (g->lhs) < 0); switch (g->tag) { case Gate_Type::And_Gate: LOG (lrat_chain, "lrat"); @@ -1157,7 +1161,7 @@ void Closure::learn_congruence_unit_when_lhs_set (Gate *g, int src, LOG (lrat_chain, "lrat"); break; default: - assert (false); + CADICAL_assert (false); } } @@ -1167,9 +1171,9 @@ void Closure::learn_congruence_unit_falsifies_lrat_chain ( Gate *g, int src, int dst, int clashing, int falsified, int unit) { if (!internal->lrat) return; - assert (!g->pos_lhs_ids.empty ()); - assert (internal->analyzed.empty ()); - assert (lrat_chain.empty ()); + CADICAL_assert (!g->pos_lhs_ids.empty ()); + CADICAL_assert (internal->analyzed.empty ()); + CADICAL_assert (lrat_chain.empty ()); std::vector proof_chain; switch (g->tag) { case Gate_Type::And_Gate: @@ -1211,7 +1215,7 @@ void Closure::learn_congruence_unit_falsifies_lrat_chain ( LOG (proof_chain, "produced lrat chain so far"); } } - assert (!proof_chain.empty ()); + CADICAL_assert (!proof_chain.empty ()); } else { LOG ("degenerated AND gate with conflict without LHS"); for (const auto &litId : g->pos_lhs_ids) { @@ -1251,7 +1255,7 @@ void Closure::learn_congruence_unit_falsifies_lrat_chain ( } } } else { - assert (unit); + CADICAL_assert (unit); // Example is 1 = 2&3 where 2 and 3 are false for (auto litId : g->neg_lhs_ids) { push_id_and_rewriting_lrat_unit (litId.clause, Rewrite (), @@ -1263,7 +1267,7 @@ void Closure::learn_congruence_unit_falsifies_lrat_chain ( lrat_chain = std::move (proof_chain); break; default: - assert (false); + CADICAL_assert (false); } (void) unit; } @@ -1272,8 +1276,8 @@ bool Closure::fully_propagate () { if (internal->unsat) return false; LOG ("fully propagating"); - assert (internal->watching ()); - assert (full_watching); + CADICAL_assert (internal->watching ()); + CADICAL_assert (full_watching); bool no_conflict = internal->propagate (); if (no_conflict) @@ -1298,10 +1302,10 @@ bool Closure::learn_congruence_unit (int lit, bool delay_propagation, bool force } LOG ("adding unit %s", LOGLIT (lit)); ++internal->stats.congruence.units; - assert (!internal->lrat || !lrat_chain.empty ()); + CADICAL_assert (!internal->lrat || !lrat_chain.empty ()); if (val_lit < 0) { if (internal->lrat) { - assert (internal->lrat_chain.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); LRAT_ID id = internal->unit_id (-lit); internal->lrat_chain.push_back (id); for (auto id : lrat_chain) @@ -1315,8 +1319,8 @@ bool Closure::learn_congruence_unit (int lit, bool delay_propagation, bool force LOG (lrat_chain, "assigning due to LRAT chain"); swap (lrat_chain, internal->lrat_chain); internal->assign_unit (lit); - assert (lrat_chain.empty ()); - assert (internal->lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); if (delay_propagation) return false; else return fully_propagate (); @@ -1329,9 +1333,9 @@ bool Closure::merge_literals_lrat ( Gate *g, Gate *h, int lit, int other, const std::vector &extra_reasons_lit, const std::vector &extra_reasons_ulit) { - assert (!internal->unsat); - assert (g->lhs == lit); - assert (g == h || h->lhs == other); + CADICAL_assert (!internal->unsat); + CADICAL_assert (g->lhs == lit); + CADICAL_assert (g == h || h->lhs == other); (void) g, (void) h; LOG ("merging literals %s and %s", LOGLIT(lit), LOGLIT(other)); // TODO: this should not update_eager but still calculate the LRAT chain @@ -1392,10 +1396,10 @@ bool Closure::merge_literals_lrat ( swap (smaller_chain, larger_chain); } - assert (find_representative (smaller_repr) == smaller_repr); - assert (find_representative (larger_repr) == larger_repr); + CADICAL_assert (find_representative (smaller_repr) == smaller_repr); + CADICAL_assert (find_representative (larger_repr) == larger_repr); if (lit == -other) { - assert (chain.empty ()); + CADICAL_assert (chain.empty ()); LOG ("merging clashing %d and %d", lit, other); if (internal->proof) { if (internal->lrat) { @@ -1425,7 +1429,7 @@ bool Closure::merge_literals_lrat ( lrat_chain = *smaller_chain; eq1_tmp = add_tmp_binary_clause (-larger, smaller); } - assert (!internal->lrat || eq1_tmp); + CADICAL_assert (!internal->lrat || eq1_tmp); Clause *eq2_tmp = nullptr; if (internal->lrat) { @@ -1436,7 +1440,7 @@ bool Closure::merge_literals_lrat ( // the order in the clause is important for the // repr_lit == -repr_other to get the right chain } - assert (!internal->lrat || eq2_tmp); + CADICAL_assert (!internal->lrat || eq2_tmp); if (internal->lrat) lrat_chain.clear (); @@ -1484,7 +1488,7 @@ bool Closure::merge_literals_lrat ( if (val_lit == -val_other) { LOG ("merging lits %d and %d assigned to inconsistent value", lit, other); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); if (internal->lrat) { Clause *c = val_lit ? eq2_tmp : eq1_tmp; int pos = val_lit ? other : lit; @@ -1499,9 +1503,9 @@ bool Closure::merge_literals_lrat ( return false; } - assert (!val_other); + CADICAL_assert (!val_other); LOG ("merging assigned %d and unassigned %d", lit, other); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); const int unit = (val_lit < 0) ? -other : other; if (internal->lrat) { Clause *c; @@ -1527,7 +1531,7 @@ bool Closure::merge_literals_lrat ( if (!val_lit && val_other) { LOG ("merging assigned %d and unassigned %d", lit, other); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); const int unit = (val_other < 0) ? -lit : lit; if (internal->lrat) { Clause *c; @@ -1611,10 +1615,10 @@ bool Closure::merge_literals_lrat ( if (internal->lrat) { representative_id (larger_repr) = eq1_repr->id; - assert (std::find (begin (*eq1_repr), end (*eq1_repr), -larger_repr) != + CADICAL_assert (std::find (begin (*eq1_repr), end (*eq1_repr), -larger_repr) != end (*eq1_repr)); representative_id (-larger_repr) = eq2_repr->id; - assert (std::find (begin (*eq2_repr), end (*eq2_repr), larger_repr) != + CADICAL_assert (std::find (begin (*eq2_repr), end (*eq2_repr), larger_repr) != end (*eq2_repr)); } LOG ("updating %d -> %d", larger_repr, smaller_repr); @@ -1622,7 +1626,7 @@ bool Closure::merge_literals_lrat ( representative (-larger_repr) = -smaller_repr; schedule_literal (larger_repr); ++internal->stats.congruence.congruent; - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); return true; } @@ -1632,7 +1636,7 @@ bool Closure::merge_literals_lrat ( bool Closure::merge_literals_lrat ( int lit, int other, const std::vector &extra_reasons_lit, const std::vector &extra_reasons_ulit) { - assert (!internal->unsat); + CADICAL_assert (!internal->unsat); LOG ("merging literals %s and %s", LOGLIT (lit), LOGLIT (other)); // TODO: this should not update_eager but still calculate the LRAT chain // below! @@ -1688,8 +1692,8 @@ bool Closure::merge_literals_lrat ( swap (val_smaller, val_larger); } - assert (find_representative (smaller_repr) == smaller_repr); - assert (find_representative (larger_repr) == larger_repr); + CADICAL_assert (find_representative (smaller_repr) == smaller_repr); + CADICAL_assert (find_representative (larger_repr) == larger_repr); if (lit == -other) { LOG ("merging clashing %d and %d", lit, other); if (!val_smaller) { @@ -1701,7 +1705,7 @@ bool Closure::merge_literals_lrat ( push_lrat_unit (smaller); if (internal->lrat) { - assert (internal->lrat_chain.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); swap (internal->lrat_chain, lrat_chain); for (auto id : *larger_chain) internal->lrat_chain.push_back (id); @@ -1747,9 +1751,9 @@ bool Closure::merge_literals_lrat ( if (internal->lrat) { lrat_chain = *smaller_chain; eq1_tmp = add_tmp_binary_clause (-larger, smaller); - assert (eq1_tmp); + CADICAL_assert (eq1_tmp); } - assert (!internal->lrat || eq1_tmp); + CADICAL_assert (!internal->lrat || eq1_tmp); Clause *eq2_tmp = nullptr; if (internal->lrat) { @@ -1758,10 +1762,10 @@ bool Closure::merge_literals_lrat ( // the order in the clause is important for the // repr_lit == -repr_other to get the right chain eq2_tmp = add_tmp_binary_clause (larger, -smaller); - assert (eq2_tmp); + CADICAL_assert (eq2_tmp); } - assert (!internal->lrat || eq2_tmp); + CADICAL_assert (!internal->lrat || eq2_tmp); if (internal->lrat) lrat_chain.clear (); @@ -1781,7 +1785,7 @@ bool Closure::merge_literals_lrat ( rew2); swap (lrat_chain, internal->lrat_chain); } - assert (val_larger == internal->val (larger_repr)); + CADICAL_assert (val_larger == internal->val (larger_repr)); if (!val_larger) { // not assigned, first assign one internal->assign_unit (-larger_repr); @@ -1819,7 +1823,7 @@ bool Closure::merge_literals_lrat ( if (val_lit == -val_other) { LOG ("merging lits %d and %d assigned to inconsistent value", lit, other); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); if (internal->lrat) { Clause *c = val_lit ? eq2_tmp : eq1_tmp; int pos = val_lit ? other : lit; @@ -1834,9 +1838,9 @@ bool Closure::merge_literals_lrat ( return false; } - assert (!val_other); + CADICAL_assert (!val_other); LOG ("merging assigned %d and unassigned %d", lit, other); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); const int unit = (val_lit < 0) ? -other : other; if (internal->lrat) { Clause *c; @@ -1862,12 +1866,12 @@ bool Closure::merge_literals_lrat ( if (!val_lit && val_other) { LOG ("merging assigned %d and unassigned %d", lit, other); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); const int unit = (val_other < 0) ? -lit : lit; if (internal->lrat) { Clause *c; if (lit == smaller) { - assert (other == larger); + CADICAL_assert (other == larger); if (val_other > 0) c = eq1_tmp; else @@ -1945,12 +1949,12 @@ bool Closure::merge_literals_lrat ( if (internal->lrat) { representative_id (larger_repr) = eq1_repr->id; - assert (std::find (begin (*eq1_repr), end (*eq1_repr), -larger_repr) != + CADICAL_assert (std::find (begin (*eq1_repr), end (*eq1_repr), -larger_repr) != end (*eq1_repr)); representative_id (-larger_repr) = eq2_repr->id; check_not_tmp_binary_clause (eq1_repr); check_not_tmp_binary_clause (eq2_repr); - assert (std::find (begin (*eq2_repr), end (*eq2_repr), larger_repr) != + CADICAL_assert (std::find (begin (*eq2_repr), end (*eq2_repr), larger_repr) != end (*eq2_repr)); } LOG ("updating %d -> %d", larger_repr, smaller_repr); @@ -1958,7 +1962,7 @@ bool Closure::merge_literals_lrat ( representative (-larger_repr) = -smaller_repr; schedule_literal (larger_repr); ++internal->stats.congruence.congruent; - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); return true; } @@ -1976,9 +1980,9 @@ inline void Closure::promote_clause (Clause *c) { internal->stats.current.irredundant++; internal->stats.added.irredundant++; internal->stats.irrlits += c->size; - assert (internal->stats.current.redundant > 0); + CADICAL_assert (internal->stats.current.redundant > 0); internal->stats.current.redundant--; - assert (internal->stats.added.redundant > 0); + CADICAL_assert (internal->stats.added.redundant > 0); internal->stats.added.redundant--; // ... and keep 'stats.added.total'. } @@ -1989,18 +1993,18 @@ inline void Closure::promote_clause (Clause *c) { // vice-versa bool Closure::merge_literals_equivalence (int lit, int other, Clause *c1, Clause *c2) { - assert (!internal->unsat); + CADICAL_assert (!internal->unsat); LRAT_ID id1 = c1 ? c1->id : 0; LRAT_ID id2 = c2 ? c2->id : 0; if (internal->lrat) { - assert (c1); - assert (c2); - assert (c1->size == 2); - assert (c2->size == 2); - assert (c1->literals[0] == lit || c1->literals[1] == lit); - assert (c2->literals[0] == other || c2->literals[1] == other); - assert (c1->literals[0] == -other || c1->literals[1] == -other); - assert (c2->literals[0] == -lit || c2->literals[1] == -lit); + CADICAL_assert (c1); + CADICAL_assert (c2); + CADICAL_assert (c1->size == 2); + CADICAL_assert (c2->size == 2); + CADICAL_assert (c1->literals[0] == lit || c1->literals[1] == lit); + CADICAL_assert (c2->literals[0] == other || c2->literals[1] == other); + CADICAL_assert (c1->literals[0] == -other || c1->literals[1] == -other); + CADICAL_assert (c2->literals[0] == -lit || c2->literals[1] == -lit); check_not_tmp_binary_clause (c1); check_not_tmp_binary_clause (c2); } @@ -2033,7 +2037,7 @@ bool Closure::merge_literals_equivalence (int lit, int other, Clause *c1, return false; } - assert (!val_other); + CADICAL_assert (!val_other); LOG ("merging assigned %d and unassigned %d", lit, other); const int unit = (val_lit < 0) ? -other : other; if (internal->lrat) @@ -2070,8 +2074,8 @@ bool Closure::merge_literals_equivalence (int lit, int other, Clause *c1, swap (smaller, larger); } - assert (find_representative (smaller_repr) == smaller_repr); - assert (find_representative (larger_repr) == larger_repr); + CADICAL_assert (find_representative (smaller_repr) == smaller_repr); + CADICAL_assert (find_representative (larger_repr) == larger_repr); if (repr_lit == -repr_other) { LOG ("merging clashing %d [=%d] and %d[=%d], smaller: %d", lit, @@ -2093,7 +2097,7 @@ bool Closure::merge_literals_equivalence (int lit, int other, Clause *c1, if (internal->lrat) { lrat_chain.clear (); LRAT_ID id = internal->unit_id (repr_lit); - assert (id); + CADICAL_assert (id); lrat_chain.push_back (id); if (lit != repr_lit) { const LRAT_ID repr_id2 = find_representative_lrat (-lit); @@ -2140,20 +2144,20 @@ bool Closure::merge_literals_equivalence (int lit, int other, Clause *c1, if (internal->lrat) { lrat_chain.clear (); if (smaller_repr == repr_lit) { - assert (larger_repr == repr_other); + CADICAL_assert (larger_repr == repr_other); representative_id (-larger_repr) = eq2->id; - assert (std::find (eq2->begin (), eq2->end (), larger_repr) != + CADICAL_assert (std::find (eq2->begin (), eq2->end (), larger_repr) != eq2->end ()); representative_id (larger_repr) = eq1->id; - assert (std::find (eq1->begin (), eq1->end (), -larger_repr) != + CADICAL_assert (std::find (eq1->begin (), eq1->end (), -larger_repr) != eq1->end ()); } else { - assert (larger_repr == repr_lit); + CADICAL_assert (larger_repr == repr_lit); representative_id (-larger_repr) = eq1->id; - assert (std::find (eq1->begin (), eq1->end (), larger_repr) != + CADICAL_assert (std::find (eq1->begin (), eq1->end (), larger_repr) != eq1->end ()); representative_id (larger_repr) = eq2->id; - assert (std::find (eq2->begin (), eq2->end (), -larger_repr) != + CADICAL_assert (std::find (eq2->begin (), eq2->end (), -larger_repr) != eq2->end ()); } } @@ -2187,13 +2191,13 @@ GOccs &Closure::goccs (int lit) { return gtab[internal->vlit (lit)]; } void Closure::connect_goccs (Gate *g, int lit) { LOG (g, "connect %d to", lit); // incorrect for ITE - // assert (std::find(begin (goccs (lit)), end (goccs (lit)), g) == + // CADICAL_assert (std::find(begin (goccs (lit)), end (goccs (lit)), g) == // std::end (goccs (lit))); goccs (lit).push_back (g); } uint64_t &Closure::largecount (int lit) { - assert (internal->vlit (lit) < glargecounts.size ()); + CADICAL_assert (internal->vlit (lit) < glargecounts.size ()); return glargecounts[internal->vlit (lit)]; } @@ -2216,7 +2220,7 @@ void Closure::init_closure () { mu2_ids.resize (2 * internal->max_var + 3); mu4_ids.resize (2 * internal->max_var + 3); } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (auto &it : mu1_ids) it.current_lit = 0, it.clause = nullptr; for (auto &it : mu2_ids) @@ -2253,7 +2257,7 @@ void Closure::init_and_gate_extraction () { continue; if (c->size > 2) continue; - assert (c->size == 2); + CADICAL_assert (c->size == 2); const int lit = c->literals[0]; const int other = c->literals[1]; internal->noccs (lit)++; @@ -2264,8 +2268,8 @@ void Closure::init_and_gate_extraction () { /*------------------------------------------------------------------------*/ void Closure::check_and_gate_implied (Gate *g) { - assert (internal->clause.empty ()); - assert (g->tag == Gate_Type::And_Gate); + CADICAL_assert (internal->clause.empty ()); + CADICAL_assert (g->tag == Gate_Type::And_Gate); if (internal->lrat) return; LOG (g, "checking implied"); @@ -2280,14 +2284,14 @@ void Closure::check_and_gate_implied (Gate *g) { void Closure::delete_proof_chain () { if (!internal->proof) { - assert (chain.empty ()); + CADICAL_assert (chain.empty ()); return; } if (chain.empty ()) return; LOG ("starting deletion of proof chain"); auto &clause = internal->clause; - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); uint32_t id1 = UINT32_MAX, id2 = UINT32_MAX; LRAT_ID id = 0; @@ -2308,13 +2312,13 @@ void Closure::delete_proof_chain () { LOG ("found %d as literal in chain", lit); clause.push_back (lit); } else { - assert (id); + CADICAL_assert (id); internal->proof->delete_clause (id, false, clause); clause.clear (); id = 0, id1 = UINT32_MAX, id2 = UINT32_MAX; } } - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); chain.clear (); LOG ("finished deletion of proof chain"); } @@ -2322,7 +2326,7 @@ void Closure::delete_proof_chain () { /*------------------------------------------------------------------------*/ // Simplification bool Closure::skip_and_gate (Gate *g) { - assert (g->tag == Gate_Type::And_Gate); + CADICAL_assert (g->tag == Gate_Type::And_Gate); if (g->garbage) return true; const int lhs = g->lhs; @@ -2331,15 +2335,15 @@ bool Closure::skip_and_gate (Gate *g) { return true; } - assert (g->arity () > 1); + CADICAL_assert (g->arity () > 1); return false; } bool Closure::skip_xor_gate (Gate *g) { - assert (g->tag == Gate_Type::XOr_Gate); + CADICAL_assert (g->tag == Gate_Type::XOr_Gate); if (g->garbage) return true; - assert (g->arity () > 1); + CADICAL_assert (g->arity () > 1); return false; } @@ -2371,17 +2375,17 @@ void Closure::update_and_gate_unit_build_lrat_chain ( // with a rewriting 4 -> 1 (unchanged clause) // and later 1 -> 3 (unchanged clause) // but you do not know anymore from the gate that it is degenerated - assert (g->pos_lhs_ids.size () == 1); + CADICAL_assert (g->pos_lhs_ids.size () == 1); push_id_and_rewriting_lrat_unit (g->pos_lhs_ids[0].clause, Rewrite (), extra_reasons_ulit, true, Rewrite ()); return; } - assert (g->lhs == g->rhs[0] || (g->lhs == src && g->rhs[0] == dst)); - assert (g->pos_lhs_ids.size () <= 1); // either degenerated or empty A = A + CADICAL_assert (g->lhs == g->rhs[0] || (g->lhs == src && g->rhs[0] == dst)); + CADICAL_assert (g->pos_lhs_ids.size () <= 1); // either degenerated or empty A = A return; } - assert (g->neg_lhs_ids.size () == 1); - assert (!g->pos_lhs_ids.empty ()); + CADICAL_assert (g->neg_lhs_ids.size () == 1); + CADICAL_assert (!g->pos_lhs_ids.empty ()); const int repr_lit = find_representative (g->lhs); const int repr_other = find_representative (g->rhs[0]); @@ -2397,7 +2401,7 @@ void Closure::update_and_gate_unit_build_lrat_chain ( lrat_chain.clear (); - assert (!g->pos_lhs_ids.empty ()); + CADICAL_assert (!g->pos_lhs_ids.empty ()); for (auto litId : g->pos_lhs_ids) push_id_and_rewriting_lrat_unit ( litId.clause, Rewrite (src, dst, id1, id2), extra_reasons_lit, true, @@ -2408,7 +2412,7 @@ void Closure::update_and_gate_unit_build_lrat_chain ( void Closure::update_and_gate_build_lrat_chain ( Gate *g, Gate *h, std::vector &extra_reasons_lit, std::vector &extra_reasons_ulit, bool remove_units) { - assert (g != h); + CADICAL_assert (g != h); LOG (g, "merging"); LOG (h, "with"); // If the LHS are identical, do not even attempt to build the LRAT chain @@ -2425,41 +2429,41 @@ void Closure::update_and_gate_build_lrat_chain ( // and all gate clauses (except one binary) are actually tautologies for (auto &litId : g->pos_lhs_ids) { if (litId.current_lit == h->lhs) { - assert (extra_reasons_lit.empty ()); + CADICAL_assert (extra_reasons_lit.empty ()); LOG (litId.clause, "binary clause to push into the reason"); litId.clause = produce_rewritten_clause_lrat (litId.clause, g->lhs, remove_units); - assert (litId.clause); + CADICAL_assert (litId.clause); extra_reasons_lit.push_back (litId.clause->id); } } - assert (!extra_reasons_lit.empty ()); - assert (extra_reasons_lit.size () == 1); + CADICAL_assert (!extra_reasons_lit.empty ()); + CADICAL_assert (extra_reasons_lit.size () == 1); for (auto &litId : h->pos_lhs_ids) { if (litId.current_lit == g->lhs) { - assert (extra_reasons_ulit.empty ()); - assert (litId.clause); + CADICAL_assert (extra_reasons_ulit.empty ()); + CADICAL_assert (litId.clause); LOG (litId.clause, "binary clause to push into the reason"); litId.clause = produce_rewritten_clause_lrat (litId.clause, h->lhs, remove_units); - assert (litId.clause); + CADICAL_assert (litId.clause); extra_reasons_ulit.push_back (litId.clause->id); } } - assert (!extra_reasons_ulit.empty ()); - assert (extra_reasons_ulit.size () == 1); + CADICAL_assert (!extra_reasons_ulit.empty ()); + CADICAL_assert (extra_reasons_ulit.size () == 1); return; } if (g_tautology || h_tautology) { // special case: actually we have an equivalence due to binary clauses // and some of the clauses from the gate are actually tautologies - assert (g_tautology != h_tautology); + CADICAL_assert (g_tautology != h_tautology); Gate *tauto = (g_tautology ? g : h); Gate *other = (g_tautology ? h : g); LOG (tauto, "one gate is a tautology"); - assert (tauto != other); - assert (tauto == h || tauto == g); + CADICAL_assert (tauto != other); + CADICAL_assert (tauto == h || tauto == g); auto &extra_reasons_tauto = (!g_tautology ? extra_reasons_lit : extra_reasons_ulit); @@ -2469,32 +2473,32 @@ void Closure::update_and_gate_build_lrat_chain ( // one direction: the binary clause already exists for (auto &litId : other->pos_lhs_ids) { if (litId.current_lit == tauto->lhs) { - assert (litId.clause); - assert (extra_reasons_tauto.empty ()); + CADICAL_assert (litId.clause); + CADICAL_assert (extra_reasons_tauto.empty ()); LOG (litId.clause, "binary clause to push into the reason"); litId.clause = produce_rewritten_clause_lrat ( litId.clause, other->lhs, remove_units); - assert (litId.clause); + CADICAL_assert (litId.clause); extra_reasons_tauto.push_back (litId.clause->id); } } - assert (!extra_reasons_tauto.empty ()); + CADICAL_assert (!extra_reasons_tauto.empty ()); // other direction, we have to resolve LOG (tauto, "now the other direction"); for (auto &litId : tauto->pos_lhs_ids) { - assert (litId.clause); + CADICAL_assert (litId.clause); LOG (litId.clause, "binary clause from %d to push into the reason [avoiding %d]", litId.current_lit, tauto->lhs); if (litId.current_lit != tauto->lhs) { LOG (litId.clause, "binary clause to push into the reason"); - assert (litId.clause); + CADICAL_assert (litId.clause); litId.clause = produce_rewritten_clause_lrat ( litId.clause, tauto->lhs, remove_units); if (!litId.clause) // degenerated but does not know yet continue; - assert (litId.clause); + CADICAL_assert (litId.clause); extra_reasons_other.push_back (litId.clause->id); } tauto->pos_lhs_ids.erase (std::remove_if (begin (tauto->pos_lhs_ids), @@ -2504,12 +2508,12 @@ void Closure::update_and_gate_build_lrat_chain ( }), end (tauto->pos_lhs_ids)); } - assert (!extra_reasons_other.empty ()); + CADICAL_assert (!extra_reasons_other.empty ()); produce_rewritten_clause_lrat_and_clean (other->neg_lhs_ids, other->lhs, remove_units); push_id_on_chain (extra_reasons_other, other->neg_lhs_ids); - assert (!extra_reasons_tauto.empty ()); - assert (!extra_reasons_other.empty ()); + CADICAL_assert (!extra_reasons_tauto.empty ()); + CADICAL_assert (!extra_reasons_other.empty ()); return; } // default: resolve all clauses @@ -2517,16 +2521,16 @@ void Closure::update_and_gate_build_lrat_chain ( // TODO: do we really need dest as second exclusion? produce_rewritten_clause_lrat_and_clean (h->pos_lhs_ids, -h->lhs, remove_units); - assert (internal->clause.empty ()); + CADICAL_assert (internal->clause.empty ()); produce_rewritten_clause_lrat_and_clean (h->neg_lhs_ids, -h->lhs, remove_units); - assert (internal->clause.empty ()); + CADICAL_assert (internal->clause.empty ()); produce_rewritten_clause_lrat_and_clean (g->pos_lhs_ids, -g->lhs, remove_units); - assert (internal->clause.empty ()); + CADICAL_assert (internal->clause.empty ()); produce_rewritten_clause_lrat_and_clean (g->neg_lhs_ids, -g->lhs, remove_units); - assert (internal->clause.empty ()); + CADICAL_assert (internal->clause.empty ()); push_id_on_chain (extra_reasons_ulit, h->pos_lhs_ids); push_id_on_chain (extra_reasons_ulit, g->neg_lhs_ids[0].clause); @@ -2538,15 +2542,15 @@ void Closure::update_and_gate_build_lrat_chain ( lrat_chain.clear (); LOG (extra_reasons_lit, "lrat chain for positive side"); - assert (!extra_reasons_lit.empty ()); - assert (!extra_reasons_ulit.empty ()); + CADICAL_assert (!extra_reasons_lit.empty ()); + CADICAL_assert (!extra_reasons_ulit.empty ()); } void Closure::update_and_gate (Gate *g, GatesTable::iterator it, int src, int dst, LRAT_ID id1, LRAT_ID id2, int falsifies, int clashing) { LOG (g, "update and gate of arity %ld", g->arity ()); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); bool garbage = true; if (falsifies || clashing) { if (internal->lrat) @@ -2589,12 +2593,12 @@ void Closure::update_and_gate (Gate *g, GatesTable::iterator it, int src, } } } else { - assert (g->arity () > 1); + CADICAL_assert (g->arity () > 1); sort_literals_by_var (g->rhs); Gate *h = find_and_lits (g->rhs, g); - assert (g != h); + CADICAL_assert (g != h); if (h) { - assert (garbage); + CADICAL_assert (garbage); std::vector extra_reasons_lit2; std::vector extra_reasons_ulit2; if (internal->lrat) @@ -2610,7 +2614,7 @@ void Closure::update_and_gate (Gate *g, GatesTable::iterator it, int src, } g->hash = hash_lits (nonces, g->rhs); LOG (g, "inserting gate into table"); - assert (table.count (g) == 0); + CADICAL_assert (table.count (g) == 0); table.insert (g); g->indexed = true; garbage = false; @@ -2624,23 +2628,23 @@ void Closure::update_and_gate (Gate *g, GatesTable::iterator it, int src, } void Closure::update_xor_gate (Gate *g, GatesTable::iterator git) { - assert (g->tag == Gate_Type::XOr_Gate); - assert (!internal->unsat && chain.empty ()); + CADICAL_assert (g->tag == Gate_Type::XOr_Gate); + CADICAL_assert (!internal->unsat && chain.empty ()); LOG (g, "updating"); bool garbage = true; // TODO Florian LRAT for learn_congruence_unit - assert (g->arity () == 0 || internal->clause.empty ()); - assert (clause.empty ()); + CADICAL_assert (g->arity () == 0 || internal->clause.empty ()); + CADICAL_assert (clause.empty ()); if (g->arity () == 0) { if (internal->clause.size ()) { - assert (!internal->proof || (internal->clause.size () == 1 && + CADICAL_assert (!internal->proof || (internal->clause.size () == 1 && internal->clause.back () == -g->lhs)); - assert (!internal->lrat || lrat_chain.size ()); + CADICAL_assert (!internal->lrat || lrat_chain.size ()); internal->clause.clear (); } else if (internal->lrat) { simplify_unit_xor_lrat_clauses (g->pos_lhs_ids, g->lhs); - assert (clause.size () && clause.back () == -g->lhs); + CADICAL_assert (clause.size () && clause.back () == -g->lhs); clause.clear (); } @@ -2653,14 +2657,14 @@ void Closure::update_xor_gate (Gate *g, GatesTable::iterator git) { } else learn_congruence_unit (-g->lhs); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); } else if (g->arity () == 1) { std::vector reasons_implication, reasons_back; if (internal->lrat) { vector first; simplify_and_sort_xor_lrat_clauses (g->pos_lhs_ids, first, g->lhs); g->pos_lhs_ids = first; - assert (g->pos_lhs_ids.size () == 2); + CADICAL_assert (g->pos_lhs_ids.size () == 2); reasons_implication.push_back (g->pos_lhs_ids[0].clause->id); reasons_back.push_back (g->pos_lhs_ids[1].clause->id); } @@ -2683,11 +2687,11 @@ void Closure::update_xor_gate (Gate *g, GatesTable::iterator git) { ++internal->stats.congruence.unaries; ++internal->stats.congruence.unary_and; } - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); } else { Gate *h = find_xor_gate (g); if (h) { - assert (garbage); + CADICAL_assert (garbage); std::vector reasons_implication, reasons_back; add_xor_matching_proof_chain (g, g->lhs, h->pos_lhs_ids, h->lhs, reasons_implication, reasons_back); @@ -2703,21 +2707,21 @@ void Closure::update_xor_gate (Gate *g, GatesTable::iterator git) { LOG (g, "reinserting in table"); table.insert (g); g->indexed = true; - assert (table.find (g) != end (table)); + CADICAL_assert (table.find (g) != end (table)); garbage = false; } - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); } if (garbage && !internal->unsat) mark_garbage (g); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); } void Closure::simplify_and_gate (Gate *g) { if (skip_and_gate (g)) return; GatesTable::iterator git = (g->indexed ? table.find (g) : end (table)); - assert (!g->indexed || git != end (table)); + CADICAL_assert (!g->indexed || git != end (table)); LOG (g, "simplifying"); int falsifies = 0; std::vector::iterator it = begin (g->rhs); @@ -2756,9 +2760,9 @@ void Closure::simplify_and_gate (Gate *g) { g->pos_lhs_ids.resize (i); } - assert (it <= end (g->rhs)); // can be equal when ITE are converted to + CADICAL_assert (it <= end (g->rhs)); // can be equal when ITE are converted to // ands leading to - assert (it >= begin (g->rhs)); + CADICAL_assert (it >= begin (g->rhs)); LOG (g, "shrunken"); g->shrunken = true; @@ -2774,7 +2778,7 @@ void Closure::simplify_and_gate (Gate *g) { ++internal->stats.congruence.simplified; if (ulhs_in_rhs) { // missing in Kissat, TODO: port back - assert (gate_contains (g, -g->lhs)); + CADICAL_assert (gate_contains (g, -g->lhs)); if (internal->lrat) { for (auto litId : g->pos_lhs_ids) { if (litId.current_lit == g->lhs) { @@ -2799,18 +2803,18 @@ bool Closure::simplify_gate (Gate *g) { simplify_ite_gate (g); break; default: - assert (false); + CADICAL_assert (false); break; } - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); return !internal->unsat; } bool Closure::simplify_gates (int lit) { const auto &occs = goccs (lit); for (Gate *g : occs) { - assert (lrat_chain.empty ()); - assert (clause.empty ()); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (clause.empty ()); if (!simplify_gate (g)) return false; } @@ -2820,7 +2824,7 @@ bool Closure::simplify_gates (int lit) { // AND gates Gate *Closure::find_and_lits (const vector &rhs, Gate *except) { - assert (is_sorted (begin (rhs), end (rhs), + CADICAL_assert (is_sorted (begin (rhs), end (rhs), sort_literals_by_var_smaller (internal))); return find_gate_lits (rhs, Gate_Type::And_Gate, except); } @@ -2845,7 +2849,7 @@ Gate *Closure::find_gate_lits (const vector &rhs, Gate_Type typ, LOG ((*it), "checking gate in the table"); if (*it == except) continue; - assert ((*it)->lhs != g->lhs); + CADICAL_assert ((*it)->lhs != g->lhs); if ((*it)->tag != g->tag) continue; if ((*it)->rhs != g->rhs) @@ -2874,12 +2878,12 @@ Gate *Closure::new_and_gate (Clause *base_clause, int lhs) { for (auto lit : lits) { if (lhs != lit) { - assert (lhs != -lit); + CADICAL_assert (lhs != -lit); rhs.push_back (-lit); } } - assert (rhs.size () + 1 == lits.size ()); + CADICAL_assert (rhs.size () + 1 == lits.size ()); sort_literals_by_var (this->rhs); Gate *h = find_and_lits (this->rhs); @@ -2920,7 +2924,7 @@ Gate *Closure::new_and_gate (Clause *base_clause, int lhs) { return nullptr; } else { g->rhs = {rhs}; - assert (!internal->lrat || + CADICAL_assert (!internal->lrat || g->pos_lhs_ids.size () == g->arity ()); // otherwise we need intermediate clauses g->garbage = false; @@ -2942,7 +2946,7 @@ Gate *Closure::new_and_gate (Clause *base_clause, int lhs) { } Gate *Closure::find_first_and_gate (Clause *base_clause, int lhs) { - assert (internal->analyzed.empty ()); + CADICAL_assert (internal->analyzed.empty ()); const int not_lhs = -lhs; LOG ("trying to find AND gate with first LHS %d", (lhs)); LOG ("negated LHS %d occurs in %zd binary clauses", (not_lhs), @@ -2953,15 +2957,15 @@ Gate *Closure::find_first_and_gate (Clause *base_clause, int lhs) { for (auto w : internal->watches (not_lhs)) { LOG (w.clause, "checking clause for candidates"); - assert (w.binary ()); - assert (w.clause->size == 2); - assert (w.clause->literals[0] == -lhs || w.clause->literals[1] == -lhs); + CADICAL_assert (w.binary ()); + CADICAL_assert (w.clause->size == 2); + CADICAL_assert (w.clause->literals[0] == -lhs || w.clause->literals[1] == -lhs); const int other = w.blit; signed char &mark = marked (other); if (mark) { LOG ("marking %d mu2", other); ++matched; - assert (~(mark & 2)); + CADICAL_assert (~(mark & 2)); mark |= 2; internal->analyzed.push_back (other); set_mu2_reason (other, w.clause); @@ -2996,10 +3000,10 @@ Clause *Closure::learn_binary_tmp_or_full_clause (int a, int b) { } Clause *Closure::maybe_add_binary_clause (int a, int b) { - assert (internal->clause.empty ()); - assert (internal->lrat_chain.empty ()); - assert (!internal->lrat); - assert (lrat_chain.empty ()); + CADICAL_assert (internal->clause.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); + CADICAL_assert (!internal->lrat); + CADICAL_assert (lrat_chain.empty ()); LOG ("learning binary clause %d %d", a, b); if (internal->unsat) return nullptr; @@ -3024,18 +3028,18 @@ Clause *Closure::maybe_add_binary_clause (int a, int b) { learn_congruence_unit (unit); return nullptr; } - assert (!a_value), assert (!b_value); + CADICAL_assert (!a_value), CADICAL_assert (!b_value); } return add_binary_clause (a, b); } Clause *Closure::add_binary_clause (int a, int b) { - assert (internal->clause.empty ()); + CADICAL_assert (internal->clause.empty ()); internal->clause.push_back (a); internal->clause.push_back (b); if (internal->lrat) { - assert (lrat_chain.size () >= 1); - assert (internal->lrat_chain.empty ()); + CADICAL_assert (lrat_chain.size () >= 1); + CADICAL_assert (internal->lrat_chain.empty ()); swap (internal->lrat_chain, lrat_chain); } LOG (internal->lrat_chain, "chain"); @@ -3051,19 +3055,19 @@ Clause *Closure::add_binary_clause (int a, int b) { if (internal->lrat) { internal->lrat_chain.clear (); } - assert (internal->clause.empty ()); - assert (internal->lrat_chain.empty ()); + CADICAL_assert (internal->clause.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); return res; } void Closure::check_not_tmp_binary_clause (Clause *c) { -#ifndef NDEBUG - assert (internal->lrat); - assert (internal->lrat_chain.empty ()); - assert (c->size == 2); +#ifndef CADICAL_NDEBUG + CADICAL_assert (internal->lrat); + CADICAL_assert (internal->lrat_chain.empty ()); + CADICAL_assert (c->size == 2); if (internal->val (c->literals[0]) || internal->val (c->literals[1])) return; - assert (std::find (begin (extra_clauses), end (extra_clauses), c) == + CADICAL_assert (std::find (begin (extra_clauses), end (extra_clauses), c) == end (extra_clauses)); #else (void) c; @@ -3071,12 +3075,12 @@ void Closure::check_not_tmp_binary_clause (Clause *c) { }; Clause *Closure::maybe_promote_tmp_binary_clause (Clause *c) { - assert (internal->lrat); - assert (internal->lrat_chain.empty ()); - assert (c->size == 2); + CADICAL_assert (internal->lrat); + CADICAL_assert (internal->lrat_chain.empty ()); + CADICAL_assert (c->size == 2); LOG (c, "promoting tmp"); -#ifndef NDEBUG - assert (std::find (begin (extra_clauses), end (extra_clauses), c) != +#ifndef CADICAL_NDEBUG + CADICAL_assert (std::find (begin (extra_clauses), end (extra_clauses), c) != end (extra_clauses)); #endif if (internal->val (c->literals[0]) || internal->val (c->literals[1])) @@ -3088,20 +3092,20 @@ Clause *Closure::maybe_promote_tmp_binary_clause (Clause *c) { }; Clause *Closure::add_tmp_binary_clause (int a, int b) { - assert (internal->clause.empty ()); - assert (internal->lrat_chain.empty ()); - assert (internal->lrat); + CADICAL_assert (internal->clause.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); + CADICAL_assert (internal->lrat); LOG ("learning tmp binary clause %d %d", a, b); if (internal->unsat) return nullptr; if (a == -b) return nullptr; - assert (internal->clause.empty ()); + CADICAL_assert (internal->clause.empty ()); internal->clause.push_back (a); internal->clause.push_back (b); if (internal->lrat) { - assert (lrat_chain.size () >= 1); - assert (internal->lrat_chain.empty ()); + CADICAL_assert (lrat_chain.size () >= 1); + CADICAL_assert (internal->lrat_chain.empty ()); } LOG (lrat_chain, "chain"); Clause *res = new_tmp_clause (internal->clause); @@ -3109,8 +3113,8 @@ Clause *Closure::add_tmp_binary_clause (int a, int b) { if (internal->lrat) { lrat_chain.clear (); } - assert (internal->clause.empty ()); - assert (internal->lrat_chain.empty ()); + CADICAL_assert (internal->clause.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); LOG (res, "promoted to"); return res; } @@ -3129,15 +3133,15 @@ Gate *Closure::find_remaining_and_gate (Clause *base_clause, int lhs) { const size_t arity = lits.size () - 1; size_t matched = 0; - assert (1 < arity); + CADICAL_assert (1 < arity); for (auto w : internal->watches (not_lhs)) { - assert (w.binary ()); + CADICAL_assert (w.binary ()); #ifdef LOGGING Clause *c = w.clause; LOG (c, "checking"); - assert (c->size == 2); - assert (c->literals[0] == not_lhs || c->literals[1] == not_lhs); + CADICAL_assert (c->size == 2); + CADICAL_assert (c->literals[0] == not_lhs || c->literals[1] == not_lhs); #endif const int other = w.blit; signed char &mark = marked (other); @@ -3150,7 +3154,7 @@ Gate *Closure::find_remaining_and_gate (Clause *base_clause, int lhs) { continue; } LOG ("marking %d mu4", other); - assert (!(mark & 4)); + CADICAL_assert (!(mark & 4)); mark |= 4; lrat_chain_and_gate.push_back (LitClausePair (other, w.clause)); if (internal->lrat) @@ -3159,8 +3163,8 @@ Gate *Closure::find_remaining_and_gate (Clause *base_clause, int lhs) { { auto q = begin (internal->analyzed); - assert (!internal->analyzed.empty ()); - assert (marked (not_lhs) == 3); + CADICAL_assert (!internal->analyzed.empty ()); + CADICAL_assert (marked (not_lhs) == 3); for (auto lit : internal->analyzed) { signed char &mark = marked (lit); if (lit == not_lhs) { @@ -3168,7 +3172,7 @@ Gate *Closure::find_remaining_and_gate (Clause *base_clause, int lhs) { continue; } - assert ((mark & 3) == 3); + CADICAL_assert ((mark & 3) == 3); if (mark & 4) { mark = 3; *q = lit; @@ -3179,8 +3183,8 @@ Gate *Closure::find_remaining_and_gate (Clause *base_clause, int lhs) { mark = 1; } } - assert (q != end (internal->analyzed)); - assert (marked (not_lhs) == 1); + CADICAL_assert (q != end (internal->analyzed)); + CADICAL_assert (marked (not_lhs) == 1); internal->analyzed.resize (q - begin (internal->analyzed)); LOG ("after filtering %zu LHS candidate remain", internal->analyzed.size ()); @@ -3219,8 +3223,8 @@ struct congruence_occurrences_larger { }; void Closure::extract_and_gates_with_base_clause (Clause *c) { - assert (!c->garbage); - assert (lrat_chain.empty ()); + CADICAL_assert (!c->garbage); + CADICAL_assert (lrat_chain.empty ()); LOG (c, "extracting and gates with clause"); unsigned size = 0; const unsigned arity_limit = @@ -3236,7 +3240,7 @@ void Closure::extract_and_gates_with_base_clause (Clause *c) { continue; } if (v > 0) { - assert (!internal->level); + CADICAL_assert (!internal->level); LOG (c, "found satisfied clause"); internal->mark_garbage (c); if (internal->lrat) @@ -3268,7 +3272,7 @@ void Closure::extract_and_gates_with_base_clause (Clause *c) { LOG (c, "is actually too small, thus skipping"); if (internal->lrat) lrat_chain.clear (); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); return; } @@ -3300,9 +3304,9 @@ void Closure::extract_and_gates_with_base_clause (Clause *c) { } } const size_t reduced_size = clause_size - reduced; - assert (reduced_size); + CADICAL_assert (reduced_size); LOG (c, "trying as base arity %lu AND gate", arity); - assert (begin (lits) + reduced_size <= end (lits)); + CADICAL_assert (begin (lits) + reduced_size <= end (lits)); MSORT (internal->opts.radixsortlim, begin (lits), begin (lits) + reduced_size, congruence_occurrences_rank (internal), @@ -3311,7 +3315,7 @@ void Closure::extract_and_gates_with_base_clause (Clause *c) { unsigned extracted = 0; for (size_t i = 0; i < clause_size; ++i) { - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); if (internal->unsat) break; if (c->garbage) @@ -3322,16 +3326,16 @@ void Closure::extract_and_gates_with_base_clause (Clause *c) { if (first) { first = false; - assert (internal->analyzed.empty ()); + CADICAL_assert (internal->analyzed.empty ()); if (find_first_and_gate (c, lhs) != nullptr) { - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); ++extracted; } } else if (internal->analyzed.empty ()) { LOG ("early abort AND gate search"); break; } else if (find_remaining_and_gate (c, lhs)) { - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); ++extracted; } } @@ -3352,7 +3356,7 @@ void Closure::reset_and_gate_extraction () { } void Closure::extract_and_gates () { - assert (!full_watching); + CADICAL_assert (!full_watching); if (!internal->opts.congruenceand) return; START (extractands); @@ -3362,7 +3366,7 @@ void Closure::extract_and_gates () { const size_t size = internal->clauses.size (); for (size_t i = 0; i < size && !internal->terminated_asynchronously (); ++i) { // we can learn new binary clauses, but no for loop - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); Clause *c = internal->clauses[i]; if (c->garbage) continue; @@ -3373,7 +3377,7 @@ void Closure::extract_and_gates () { if (c->redundant) continue; extract_and_gates_with_base_clause (c); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); } reset_and_gate_extraction (); @@ -3384,12 +3388,12 @@ void Closure::extract_and_gates () { // XOR gates uint64_t &Closure::new_largecounts (int lit) { - assert (internal->vlit (lit) < gnew_largecounts.size ()); + CADICAL_assert (internal->vlit (lit) < gnew_largecounts.size ()); return gnew_largecounts[internal->vlit (lit)]; } uint64_t &Closure::largecounts (int lit) { - assert (internal->vlit (lit) < glargecounts.size ()); + CADICAL_assert (internal->vlit (lit) < glargecounts.size ()); return glargecounts[internal->vlit (lit)]; } @@ -3410,11 +3414,11 @@ void inc_lits (vector &lits) { } void Closure::check_ternary (int a, int b, int c) { - assert (internal->clause.empty ()); + CADICAL_assert (internal->clause.empty ()); if (internal->lrat) return; auto &clause = internal->clause; - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); clause.push_back (a); clause.push_back (b); clause.push_back (c); @@ -3429,11 +3433,11 @@ void Closure::check_ternary (int a, int b, int c) { } void Closure::check_binary_implied (int a, int b) { - assert (internal->clause.empty ()); + CADICAL_assert (internal->clause.empty ()); if (internal->lrat) return; auto &clause = internal->clause; - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); clause.push_back (a); clause.push_back (b); check_implied (); @@ -3447,8 +3451,8 @@ void Closure::check_implied () { } void Closure::add_xor_shrinking_proof_chain (Gate *g, int pivot) { - assert (internal->clause.empty ()); - assert (clause.empty ()); + CADICAL_assert (internal->clause.empty ()); + CADICAL_assert (clause.empty ()); if (!internal->proof) return; LOG (g, "starting XOR shrinking proof chain"); @@ -3468,10 +3472,10 @@ void Closure::add_xor_shrinking_proof_chain (Gate *g, int pivot) { clause.push_back (lit); const bool parity = (lhs > 0); - assert (parity == parity_lits (clause)); + CADICAL_assert (parity == parity_lits (clause)); const size_t size = clause.size (); const unsigned end = 1u << (size - 1); - assert (!internal->lrat || first.size () == 2 * end); + CADICAL_assert (!internal->lrat || first.size () == 2 * end); #ifdef LOGGING for (auto pair : first) { LOG (pair.clause, "key %d", pair.current_lit); @@ -3492,7 +3496,7 @@ void Closure::add_xor_shrinking_proof_chain (Gate *g, int pivot) { clause.pop_back (); } if (internal->lrat) { - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); lrat_chain.push_back (first[2 * i].clause->id); lrat_chain.push_back (first[2 * i + 1].clause->id); } @@ -3515,17 +3519,17 @@ void Closure::add_xor_shrinking_proof_chain (Gate *g, int pivot) { } void Closure::check_xor_gate_implied (Gate const *const g) { - assert (internal->clause.empty ()); - assert (g->tag == Gate_Type::XOr_Gate); + CADICAL_assert (internal->clause.empty ()); + CADICAL_assert (g->tag == Gate_Type::XOr_Gate); if (internal->lrat) { return; } const int lhs = g->lhs; LOG (g, "checking implied"); auto &clause = internal->clause; - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); for (auto other : g->rhs) { - assert (other > 0); + CADICAL_assert (other > 0); clause.push_back (other); } clause.push_back (-lhs); @@ -3548,14 +3552,14 @@ void Closure::check_xor_gate_implied (Gate const *const g) { } Gate *Closure::find_xor_lits (const vector &rhs) { - assert (is_sorted (begin (rhs), end (rhs), + CADICAL_assert (is_sorted (begin (rhs), end (rhs), sort_literals_by_var_smaller (internal))); return find_gate_lits (rhs, Gate_Type::XOr_Gate); } Gate *Closure::find_xor_gate (Gate *g) { - assert (g->tag == Gate_Type::XOr_Gate); - assert (is_sorted (begin (g->rhs), end (g->rhs), + CADICAL_assert (g->tag == Gate_Type::XOr_Gate); + CADICAL_assert (is_sorted (begin (g->rhs), end (g->rhs), sort_literals_by_var_smaller (internal))); return find_gate_lits (g->rhs, Gate_Type::XOr_Gate); } @@ -3564,7 +3568,7 @@ void Closure::reset_xor_gate_extraction () { internal->clear_occs (); } bool Closure::normalize_ite_lits_gate (Gate *g) { auto &rhs = g->rhs; - assert (rhs.size () == 3); + CADICAL_assert (rhs.size () == 3); if (internal->lrat) check_correct_ite_flags (g); LOG (rhs, "RHS = "); @@ -3572,7 +3576,7 @@ bool Closure::normalize_ite_lits_gate (Gate *g) { rhs[0] = -rhs[0]; std::swap (rhs[1], rhs[2]); if (internal->lrat) { - assert (g->pos_lhs_ids.size () == 4); + CADICAL_assert (g->pos_lhs_ids.size () == 4); std::swap (g->pos_lhs_ids[0], g->pos_lhs_ids[2]); std::swap (g->pos_lhs_ids[1], g->pos_lhs_ids[3]); const int8_t flag = g->degenerated_ite; @@ -3588,8 +3592,8 @@ bool Closure::normalize_ite_lits_gate (Gate *g) { : Special_ITE_GATE::NORMAL) | (neg_else ? Special_ITE_GATE::NO_NEG_THEN : Special_ITE_GATE::NORMAL); - assert (g->pos_lhs_ids[0].current_lit == rhs[1]); - assert (g->pos_lhs_ids[2].current_lit == rhs[2]); + CADICAL_assert (g->pos_lhs_ids[0].current_lit == rhs[1]); + CADICAL_assert (g->pos_lhs_ids[2].current_lit == rhs[2]); if (internal->lrat) check_correct_ite_flags (g); } @@ -3602,7 +3606,7 @@ bool Closure::normalize_ite_lits_gate (Gate *g) { rhs[2] = -rhs[2]; LOG (rhs, "RHS = "); if (internal->lrat) { - assert (g->pos_lhs_ids.size () == 4); + CADICAL_assert (g->pos_lhs_ids.size () == 4); std::swap (g->pos_lhs_ids[0], g->pos_lhs_ids[1]); std::swap (g->pos_lhs_ids[2], g->pos_lhs_ids[3]); const int8_t flag = g->degenerated_ite; @@ -3618,8 +3622,8 @@ bool Closure::normalize_ite_lits_gate (Gate *g) { : Special_ITE_GATE::NORMAL) | (neg_else ? Special_ITE_GATE::NO_PLUS_ELSE : Special_ITE_GATE::NORMAL); - assert (g->pos_lhs_ids[0].current_lit == rhs[1]); - assert (g->pos_lhs_ids[2].current_lit == rhs[2]); + CADICAL_assert (g->pos_lhs_ids[0].current_lit == rhs[1]); + CADICAL_assert (g->pos_lhs_ids[2].current_lit == rhs[2]); // incorrect as we have not negated the LHS yet! // check_correct_ite_flags (g); } @@ -3628,10 +3632,10 @@ bool Closure::normalize_ite_lits_gate (Gate *g) { return true; } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG bool is_tautological_ite_gate (Gate *g) { - assert (g->tag == Gate_Type::ITE_Gate); - assert (g->rhs.size () == 3); + CADICAL_assert (g->tag == Gate_Type::ITE_Gate); + CADICAL_assert (g->rhs.size () == 3); const int cond_lit = g->rhs[0]; const int then_lit = g->rhs[1]; const int else_lit = g->rhs[2]; @@ -3650,8 +3654,8 @@ LRAT_ID Closure::check_and_add_to_proof_chain (vector &clause) { const LRAT_ID id = ++internal->clause_id; if (internal->proof) { if (internal->lrat) { - assert (internal->lrat_chain.empty ()); - assert (lrat_chain.size () >= 1); + CADICAL_assert (internal->lrat_chain.empty ()); + CADICAL_assert (lrat_chain.size () >= 1); } internal->proof->add_derived_clause (id, true, clause, lrat_chain); lrat_chain.clear (); @@ -3663,7 +3667,7 @@ void Closure::add_clause_to_chain (std::vector unsimplified, LRAT_ID id) { const uint32_t id2_higher = (id >> 32); const uint32_t id2_lower = (uint32_t) (id & (LRAT_ID) (uint32_t) (-1)); - assert (id == ((LRAT_ID) id2_higher << 32) + (LRAT_ID) id2_lower); + CADICAL_assert (id == ((LRAT_ID) id2_higher << 32) + (LRAT_ID) id2_lower); chain.push_back (id2_higher); chain.push_back (id2_lower); LOG (unsimplified, "pushing to chain"); @@ -3674,10 +3678,10 @@ void Closure::add_clause_to_chain (std::vector unsimplified, LRAT_ID Closure::simplify_and_add_to_proof_chain (vector &unsimplified, LRAT_ID delete_id) { vector &clause = internal->clause; - assert (clause.empty ()); -#ifndef NDEBUG + CADICAL_assert (clause.empty ()); +#ifndef CADICAL_NDEBUG for (auto lit : unsimplified) { - assert (!(marked (lit) & 4)); + CADICAL_assert (!(marked (lit) & 4)); } #endif @@ -3696,7 +3700,7 @@ LRAT_ID Closure::simplify_and_add_to_proof_chain (vector &unsimplified, } for (auto lit : clause) { signed char &mark = marked (lit); - assert (mark & 4); + CADICAL_assert (mark & 4); mark &= ~4u; } @@ -3725,8 +3729,8 @@ void Closure::add_ite_turned_and_binary_clauses (Gate *g) { if (internal->lrat) return; LOG ("starting ITE turned AND supporting binary clauses"); - assert (unsimplified.empty ()); - assert (chain.empty ()); + CADICAL_assert (unsimplified.empty ()); + CADICAL_assert (chain.empty ()); int not_lhs = -g->lhs; unsimplified.push_back (not_lhs); unsimplified.push_back (g->rhs[0]); @@ -3738,18 +3742,18 @@ void Closure::add_ite_turned_and_binary_clauses (Gate *g) { } void Closure::simplify_unit_xor_lrat_clauses ( const vector &source, int lhs) { - assert (internal->lrat); + CADICAL_assert (internal->lrat); for (auto pair : source) { compute_rewritten_clause_lrat_simple (pair.clause, lhs); if (lrat_chain.size ()) break; } - assert (clause.size () == 1); + CADICAL_assert (clause.size () == 1); } void Closure::simplify_and_sort_xor_lrat_clauses ( const vector &source, vector &target, int lhs, int except2, bool flip) { - assert (internal->lrat); + CADICAL_assert (internal->lrat); for (auto pair : source) { Clause *c = produce_rewritten_clause_lrat (pair.clause, lhs); if (c) { @@ -3765,7 +3769,7 @@ void Closure::add_xor_matching_proof_chain ( return; if (!internal->proof) return; - assert (unsimplified.empty ()); + CADICAL_assert (unsimplified.empty ()); unsimplified = g->rhs; vector first; vector second; @@ -3804,21 +3808,21 @@ void Closure::add_xor_matching_proof_chain ( do { vector first_tmp; vector second_tmp; - assert (!unsimplified.empty ()); + CADICAL_assert (!unsimplified.empty ()); unsimplified.pop_back (); const size_t size = unsimplified.size (); - assert (size < 32); + CADICAL_assert (size < 32); const size_t off = 1u << size; for (size_t i = 0; i != off; ++i) { int32_t n = 0; if (internal->lrat) { n = number_from_xor_reason_reversed (unsimplified); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); for (auto pair : first_ids) { if (pair.lit == n) lrat_chain.push_back (pair.id); } - assert (lrat_chain.size () == 2); + CADICAL_assert (lrat_chain.size () == 2); } unsimplified.push_back (-lhs1); unsimplified.push_back (lhs2); @@ -3833,7 +3837,7 @@ void Closure::add_xor_matching_proof_chain ( if (pair.lit == n) lrat_chain.push_back (pair.id); } - assert (lrat_chain.size () == 2); + CADICAL_assert (lrat_chain.size () == 2); } unsimplified.push_back (lhs1); unsimplified.push_back (-lhs2); @@ -3852,15 +3856,15 @@ void Closure::add_xor_matching_proof_chain ( } } while (!unsimplified.empty ()); if (internal->lrat) { - assert (first_ids.size () == 1); - assert (second_ids.size () == 1); + CADICAL_assert (first_ids.size () == 1); + CADICAL_assert (second_ids.size () == 1); to_lrat.push_back (first_ids.back ().id); back_lrat.push_back (second_ids.back ().id); } - assert (!internal->lrat || to_lrat.size () == 1); - assert (!internal->lrat || back_lrat.size () == 1); + CADICAL_assert (!internal->lrat || to_lrat.size () == 1); + CADICAL_assert (!internal->lrat || back_lrat.size () == 1); LOG ("finished XOR matching proof"); - assert (unsimplified.empty ()); + CADICAL_assert (unsimplified.empty ()); } // this function needs to either put the clauses from @@ -3872,11 +3876,11 @@ Gate *Closure::new_xor_gate (const vector &glauses, for (auto lit : lits) { if (lhs != lit && -lhs != lit) { - assert (lit > 0); + CADICAL_assert (lit > 0); rhs.push_back (lit); } } - assert (rhs.size () + 1 == lits.size ()); + CADICAL_assert (rhs.size () + 1 == lits.size ()); sort_literals_by_var (rhs); Gate *g = find_xor_lits (this->rhs); if (g) { @@ -3889,7 +3893,7 @@ Gate *Closure::new_xor_gate (const vector &glauses, ++internal->stats.congruence.xors; } delete_proof_chain (); - assert (internal->unsat || chain.empty ()); + CADICAL_assert (internal->unsat || chain.empty ()); } else { g = new Gate; g->lhs = lhs; @@ -3917,9 +3921,9 @@ Gate *Closure::new_xor_gate (const vector &glauses, uint32_t Closure::number_from_xor_reason_reversed (const std::vector &rhs) { uint32_t n = 0; - assert (is_sorted (rhs.rbegin (), rhs.rend (), + CADICAL_assert (is_sorted (rhs.rbegin (), rhs.rend (), sort_literals_by_var_smaller_except (internal, 0, 0))); - assert (rhs.size () <= 32); + CADICAL_assert (rhs.size () <= 32); for (auto r = rhs.rbegin (); r != rhs.rend (); r++) { int lit = *r; n *= 2; @@ -3931,11 +3935,11 @@ Closure::number_from_xor_reason_reversed (const std::vector &rhs) { uint32_t Closure::number_from_xor_reason (const std::vector &rhs, int lhs, int except, bool flip) { uint32_t n = 0; - assert (is_sorted ( + CADICAL_assert (is_sorted ( begin (rhs), end (rhs), sort_literals_by_var_smaller_except (internal, lhs, except))); (void) lhs, (void) except; - assert (rhs.size () <= 32); + CADICAL_assert (rhs.size () <= 32); for (auto lit : rhs) { n *= 2; n += !(lit > 0) ^ flip; @@ -3948,7 +3952,7 @@ uint32_t Closure::number_from_xor_reason (const std::vector &rhs, // Look at this first void Closure::gate_sort_lrat_reasons (LitClausePair &litId, int lhs, int except2, bool flip) { - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); std::copy (begin (*litId.clause), end (*litId.clause), back_inserter (clause)); sort_literals_by_var_except (clause, lhs, except2); @@ -3965,24 +3969,24 @@ struct smaller_pair_first_rank { // Look at this first void Closure::gate_sort_lrat_reasons (std::vector &xs, int lhs, int except2, bool flip) { - assert (clause.empty ()); - assert (!xs.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (!xs.empty ()); for (auto &litId : xs) { gate_sort_lrat_reasons (litId, lhs, except2, flip); } rsort (begin (xs), end (xs), smaller_pair_first_rank ()); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG std::for_each (begin (xs), end (xs), [&xs] (const LitClausePair &x) { - assert (x.clause->size == xs[1].clause->size); + CADICAL_assert (x.clause->size == xs[1].clause->size); }); #endif } void Closure::init_xor_gate_extraction (std::vector &candidates) { const unsigned arity_limit = internal->opts.congruencexorarity; - assert (arity_limit < 32); // we use unsigned int. + CADICAL_assert (arity_limit < 32); // we use unsigned int. const unsigned size_limit = arity_limit + 1; glargecounts.resize (2 * internal->vsize, 0); @@ -4043,8 +4047,8 @@ void Closure::init_xor_gate_extraction (std::vector &candidates) { if (!internal->val (lit)) ++size; } - assert (3 <= size); - assert (size <= size_limit); + CADICAL_assert (3 <= size); + CADICAL_assert (size <= size_limit); const unsigned arity = size - 1; const unsigned needed_clauses = 1u << (arity - 1); for (auto lit : *c) { @@ -4082,31 +4086,31 @@ Clause *Closure::find_large_xor_side_clause (std::vector &lits) { unsigned least_occurring_literal = 0; unsigned count_least_occurring = UINT_MAX; const size_t size_lits = lits.size (); -#if defined(LOGGING) || !defined(NDEBUG) +#if defined(LOGGING) || !defined(CADICAL_NDEBUG) const unsigned arity = size_lits - 1; #endif -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG const unsigned count_limit = 1u << (arity - 1); #endif LOG (lits, "trying to find arity %u XOR side clause", arity); for (auto lit : lits) { - assert (!internal->val (lit)); + CADICAL_assert (!internal->val (lit)); marked (lit) = 1; unsigned count = largecount (lit); - assert (count_limit <= count); + CADICAL_assert (count_limit <= count); if (count >= count_least_occurring) continue; count_least_occurring = count; least_occurring_literal = lit; } Clause *res = 0; - assert (least_occurring_literal); + CADICAL_assert (least_occurring_literal); LOG ("searching XOR side clause watched by %d#%u", least_occurring_literal, count_least_occurring); LOG ("searching for size %ld", size_lits); for (auto c : internal->occs (least_occurring_literal)) { LOG (c, "checking"); - assert (c->size != 2); // TODO kissat has break + CADICAL_assert (c->size != 2); // TODO kissat has break if (c->garbage) continue; if ((size_t) c->size < size_lits) @@ -4119,7 +4123,7 @@ Clause *Closure::find_large_xor_side_clause (std::vector &lits) { if (value > 0) { LOG (c, "found satisfied %d in", other); internal->mark_garbage (c); - assert (c->garbage); + CADICAL_assert (c->garbage); break; } if (marked (other)) @@ -4172,8 +4176,8 @@ void Closure::extract_xor_gates_with_base_clause (Clause *c) { largest = smallest = lit; first = false; } else { - assert (smallest); - assert (largest); + CADICAL_assert (smallest); + CADICAL_assert (largest); if (internal->vlit (lit) < internal->vlit (smallest)) { LOG ("new smallest %d", lit); smallest = lit; @@ -4198,7 +4202,7 @@ void Closure::extract_xor_gates_with_base_clause (Clause *c) { lits.push_back (lit); ++size; } - assert (size == lits.size ()); + CADICAL_assert (size == lits.size ()); if (size < 3) { LOG (c, "short XOR base clause"); return; @@ -4221,9 +4225,9 @@ void Closure::extract_xor_gates_with_base_clause (Clause *c) { } LOG ("checking for XOR side clauses"); - assert (smallest && largest); + CADICAL_assert (smallest && largest); const unsigned end = 1u << arity; - assert (negated == parity_lits (lits)); + CADICAL_assert (negated == parity_lits (lits)); unsigned found = 0; vector glauses; glauses.push_back (LitClausePair (0, c)); @@ -4236,10 +4240,10 @@ void Closure::extract_xor_gates_with_base_clause (Clause *c) { Clause *d = find_large_xor_side_clause (lits); if (!d) return; - assert (!d->redundant); + CADICAL_assert (!d->redundant); glauses.push_back (LitClausePair (i, d)); } else - assert (!c->redundant); + CADICAL_assert (!c->redundant); inc_lits (lits); ++found; } @@ -4247,7 +4251,7 @@ void Closure::extract_xor_gates_with_base_clause (Clause *c) { while (parity_lits (lits) != negated) inc_lits (lits); LOG (lits, "found all needed %u matching clauses:", found); - assert (found == 1u << arity); + CADICAL_assert (found == 1u << arity); if (negated) { auto p = begin (lits); int lit; @@ -4271,7 +4275,7 @@ void Closure::extract_xor_gates_with_base_clause (Clause *c) { LOG ("no arity %u XOR gate extracted", arity); } void Closure::extract_xor_gates () { - assert (!full_watching); + CADICAL_assert (!full_watching); if (!internal->opts.congruencexor) return; START (extractxors); @@ -4325,13 +4329,13 @@ void Closure::find_units () { } unmark_all (); } - assert (internal->analyzed.empty ()); + CADICAL_assert (internal->analyzed.empty ()); } LOG ("found %zd units", units); } void Closure::find_equivalences () { - assert (!internal->unsat); + CADICAL_assert (!internal->unsat); for (auto v : internal->vars) { RESTART: @@ -4341,7 +4345,7 @@ void Closure::find_equivalences () { for (auto w : internal->watches (lit)) { if (!w.binary ()) break; - assert (w.size == 2); + CADICAL_assert (w.size == 2); const int other = w.blit; if (internal->vlit (lit) > internal->vlit (other)) continue; @@ -4361,7 +4365,7 @@ void Closure::find_equivalences () { const int other = w.blit; if (internal->vlit (-lit) > internal->vlit (other)) continue; - assert (-lit != other); + CADICAL_assert (-lit != other); LOG ("binary clause %d %d", -lit, other); if (marked (-other)) { int lit_repr = find_representative (lit); @@ -4377,7 +4381,7 @@ void Closure::find_equivalences () { // // therefore you actually need to learn the clauses 2->3->4 // and -2->1 and vice-versa eager_representative_id (other) = // marked_mu1 (-other).clause->id; eager_representative_id - // (-other) = w.clause->id; assert (eager_representative_id + // (-other) = w.clause->id; CADICAL_assert (eager_representative_id // (other) != -1); LOG ("lrat: %d (%zd) %d (%zd)", other, // eager_representative_id (other), -other, // eager_representative_id (-other)); @@ -4402,7 +4406,7 @@ void Closure::find_equivalences () { } unmark_all (); } - assert (internal->analyzed.empty ()); + CADICAL_assert (internal->analyzed.empty ()); LOG ("found %zd equivalences", schedule.size ()); } @@ -4421,9 +4425,9 @@ void Closure::rewrite_and_gate (Gate *g, int dst, int src, LRAT_ID id1, simplify_and_gate (g); return; } - assert (src); - assert (dst); - assert (internal->val (src) == internal->val (dst)); + CADICAL_assert (src); + CADICAL_assert (dst); + CADICAL_assert (internal->val (src) == internal->val (dst)); GatesTable::iterator git = (g->indexed ? table.find (g) : end (table)); LOG (g, "rewriting %d into %d in", src, dst); int clashing = 0, falsifies = 0; @@ -4460,12 +4464,12 @@ void Closure::rewrite_and_gate (Gate *g, int dst, int src, LRAT_ID id1, } if (lit == -dst) { if (dst_count) { - assert (!not_dst_count); + CADICAL_assert (!not_dst_count); LOG ("clashing literals %d and %d", (dst), (-dst)); clashing = -dst; break; } - assert (!not_dst_count); + CADICAL_assert (!not_dst_count); ++not_dst_count; } *q++ = lit; @@ -4482,7 +4486,7 @@ void Closure::rewrite_and_gate (Gate *g, int dst, int src, LRAT_ID id1, // that we can produce an LRAT chain size_t i = 0, size = g->pos_lhs_ids.size (); bool found = false; - assert (!falsifies || !clashing); + CADICAL_assert (!falsifies || !clashing); const int orig_falsifies = falsifies == dst ? src : falsifies; const int orig_clashing = clashing == -dst ? -src : (clashing == dst ? src : clashing); @@ -4522,7 +4526,7 @@ void Closure::rewrite_and_gate (Gate *g, int dst, int src, LRAT_ID id1, ++i; } LOG ("resizing to %zd", i); - assert (i); + CADICAL_assert (i); g->pos_lhs_ids.resize (i); } @@ -4530,13 +4534,13 @@ void Closure::rewrite_and_gate (Gate *g, int dst, int src, LRAT_ID id1, g->rhs.resize (q - begin (g->rhs)); g->shrunken = true; } - assert (dst_count <= 2); - assert (not_dst_count <= 1); + CADICAL_assert (dst_count <= 2); + CADICAL_assert (not_dst_count <= 1); std::vector reasons_lrat_src, reasons_lrat_usrc; shrink_and_gate (g, falsifies, clashing); LOG (g, "rewritten as"); - assert (!internal->lrat || !g->pos_lhs_ids.empty ()); + CADICAL_assert (!internal->lrat || !g->pos_lhs_ids.empty ()); // check_and_gate_implied (g); update_and_gate (g, git, src, dst, id1, id2, falsifies, clashing); ++internal->stats.congruence.rewritten_ands; @@ -4555,17 +4559,17 @@ bool Closure::rewrite_gate (Gate *g, int dst, int src, LRAT_ID id1, rewrite_ite_gate (g, dst, src); break; default: - assert (false); + CADICAL_assert (false); break; } - assert (internal->unsat || lrat_chain.empty ()); + CADICAL_assert (internal->unsat || lrat_chain.empty ()); return !internal->unsat; } bool Closure::rewrite_gates (int dst, int src, LRAT_ID id1, LRAT_ID id2) { const auto &occs = goccs (src); for (auto g : occs) { - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); if (!rewrite_gate (g, dst, src, id1, id2)) return false; else if (!g->garbage && gate_contains (g, dst)) @@ -4573,15 +4577,15 @@ bool Closure::rewrite_gates (int dst, int src, LRAT_ID id1, LRAT_ID id2) { } goccs (src).clear (); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (const auto &occs : gtab) { for (auto g : occs) { - assert (g); - assert (g->garbage || !gate_contains (g, src)); + CADICAL_assert (g); + CADICAL_assert (g->garbage || !gate_contains (g, src)); } } #endif - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); return true; } @@ -4610,7 +4614,7 @@ void Closure::rewrite_xor_gate (Gate *g, int dst, int src) { const size_t size = g->rhs.size (); for (size_t i = 0; i < size; ++i) { int lit = g->rhs[i]; - assert (lit > 0); + CADICAL_assert (lit > 0); if (lit == src) lit = dst; const signed char v = internal->val (lit); @@ -4628,7 +4632,7 @@ void Closure::rewrite_xor_gate (Gate *g, int dst, int src) { LOG ("flipping LHS %d", g->lhs); g->lhs = -g->lhs; } - assert (dst_count <= 2); + CADICAL_assert (dst_count <= 2); if (dst_count == 2) { LOG ("destination found twice, removing"); size_t k = 0; @@ -4637,10 +4641,10 @@ void Closure::rewrite_xor_gate (Gate *g, int dst, int src) { if (lit != dst) g->rhs[k++] = g->rhs[i]; } - assert (k == j - 2); + CADICAL_assert (k == j - 2); g->rhs.resize (k); g->shrunken = true; - assert (is_sorted (begin (g->rhs), end (g->rhs), + CADICAL_assert (is_sorted (begin (g->rhs), end (g->rhs), sort_literals_by_var_smaller (internal))); g->hash = hash_lits (nonces, g->rhs); } else if (j != size) { @@ -4651,17 +4655,17 @@ void Closure::rewrite_xor_gate (Gate *g, int dst, int src) { nonces, g->rhs); // all but one (the dst) is sorted correctly actually } else { - assert (j == size); + CADICAL_assert (j == size); sort_literals_by_var (g->rhs); } - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); // LRAT for add_xor_shrinking_proof_chain // this should be unnecessary... // TODO check if really unnecessary if (dst_count > 1) add_xor_shrinking_proof_chain (g, dst); - assert (internal->clause.size () <= 1); + CADICAL_assert (internal->clause.size () <= 1); update_xor_gate (g, git); if (!g->garbage && !internal->unsat && original_dst_negated && @@ -4685,7 +4689,7 @@ void Closure::simplify_xor_gate (Gate *g) { size_t j = 0; for (size_t i = 0; i < size; ++i) { int lit = g->rhs[i]; - assert (lit > 0); + CADICAL_assert (lit > 0); const signed char v = internal->val (lit); if (v > 0) negate ^= 1; @@ -4701,15 +4705,15 @@ void Closure::simplify_xor_gate (Gate *g) { LOG ("shrunken gate"); g->shrunken = true; g->rhs.resize (j); - assert (is_sorted (begin (g->rhs), end (g->rhs), + CADICAL_assert (is_sorted (begin (g->rhs), end (g->rhs), sort_literals_by_var_smaller (internal))); g->hash = hash_lits (nonces, g->rhs); } else { - assert (g->hash == hash_lits (nonces, g->rhs)); + CADICAL_assert (g->hash == hash_lits (nonces, g->rhs)); } check_xor_gate_implied (g); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); update_xor_gate (g, git); LOG (g, "simplified"); check_xor_gate_implied (g); @@ -4725,7 +4729,7 @@ void Closure::schedule_literal (int lit) { return; scheduled[idx] = true; schedule.push (lit); - assert (lit != find_representative (lit)); + CADICAL_assert (lit != find_representative (lit)); LOG ("scheduled literal %d", lit); } @@ -4757,7 +4761,7 @@ bool Closure::propagate_equivalence (int lit) { const int repr = find_eager_representative_and_compress (lit); const LRAT_ID id1 = find_eager_representative_lrat (lit); const LRAT_ID id2 = find_eager_representative_lrat (-lit); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); return rewrite_gates (repr, lit, id1, id2) && rewrite_gates (-repr, -lit, id2, id1); } @@ -4766,10 +4770,10 @@ size_t Closure::propagate_units_and_equivalences () { START (congruencemerge); size_t propagated = 0; LOG ("propagating at least %zd units", schedule.size ()); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); while (propagate_units () && !schedule.empty ()) { - assert (!internal->unsat); - assert (lrat_chain.empty ()); + CADICAL_assert (!internal->unsat); + CADICAL_assert (lrat_chain.empty ()); ++propagated; int lit = schedule.front (); schedule.pop (); @@ -4778,28 +4782,28 @@ size_t Closure::propagate_units_and_equivalences () { break; } - assert (internal->unsat || schedule.empty ()); - assert (internal->unsat || lrat_chain.empty ()); + CADICAL_assert (internal->unsat || schedule.empty ()); + CADICAL_assert (internal->unsat || lrat_chain.empty ()); LOG ("propagated %zu congruence units", units); LOG ("propagated %zu congruence equivalences", propagated); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG if (!internal->unsat) { for (const auto &occs : gtab) { for (auto g : occs) { if (g->garbage) continue; - assert (g->tag == Gate_Type::ITE_Gate || + CADICAL_assert (g->tag == Gate_Type::ITE_Gate || g->tag == Gate_Type::XOr_Gate || !gate_contains (g, -g->lhs)); // TODO: this would be nice to have! - // assert (g->tag != Gate_Type::ITE_Gate || (g->rhs.size() == 3 + // CADICAL_assert (g->tag != Gate_Type::ITE_Gate || (g->rhs.size() == 3 // && g->rhs[1] != -g->lhs && g->rhs[2] != -g->lhs)); - // assert (table.count(g) == 1); + // CADICAL_assert (table.count(g) == 1); for (auto lit : g->rhs) { - assert (!internal->val (lit)); - assert (representative (lit) == lit); + CADICAL_assert (!internal->val (lit)); + CADICAL_assert (representative (lit) == lit); } } } @@ -4807,7 +4811,7 @@ size_t Closure::propagate_units_and_equivalences () { if (g->garbage) continue; if (g->tag == Gate_Type::And_Gate) { - // assert (find_and_lits(g->arity, g->rhs)); + // CADICAL_assert (find_and_lits(g->arity, g->rhs)); } } } @@ -4832,7 +4836,7 @@ std::string string_of_gate (Gate_Type t) { void Closure::reset_closure () { scheduled.clear (); for (Gate *g : table) { - assert (g->indexed); + CADICAL_assert (g->indexed); LOG (g, "deleting"); if (!g->garbage) delete g; @@ -4849,15 +4853,15 @@ void Closure::reset_closure () { garbage.clear (); if (internal->lrat) { - assert (internal->proof); + CADICAL_assert (internal->proof); for (auto c : extra_clauses) { - assert (!c->garbage); + CADICAL_assert (!c->garbage); internal->proof->delete_clause (c); delete c; } extra_clauses.clear (); } else { - assert (extra_clauses.empty ()); + CADICAL_assert (extra_clauses.empty ()); } } @@ -4942,7 +4946,7 @@ void Closure::forward_subsume_matching_clauses () { continue; if (c->size == 2) continue; - assert (analyzed.empty ()); + CADICAL_assert (analyzed.empty ()); bool contains_matchable = false; for (auto lit : *c) { const signed char v = internal->val (lit); @@ -4960,7 +4964,7 @@ void Closure::forward_subsume_matching_clauses () { } const int repr = find_representative (lit); - assert (!internal->val (repr)); + CADICAL_assert (!internal->val (repr)); if (marked (repr)) continue; const int not_repr = -repr; @@ -4990,7 +4994,7 @@ void Closure::forward_subsume_matching_clauses () { size_t tried = 0, subsumed = 0; internal->init_occs (); for (auto c : candidates) { - assert (c->size != 2); + CADICAL_assert (c->size != 2); // TODO if terminated ++tried; if (find_subsuming_clause (c)) { @@ -5008,11 +5012,11 @@ void Closure::forward_subsume_matching_clauses () { // from subsume void Closure::subsume_clause (Clause *subsuming, Clause *subsumed) { - // assert (!subsuming->redundant); - // assert (!subsumed->redundant); + // CADICAL_assert (!subsuming->redundant); + // CADICAL_assert (!subsumed->redundant); auto &stats = internal->stats; stats.subsumed++; - assert (subsuming->size <= subsumed->size); + CADICAL_assert (subsuming->size <= subsumed->size); LOG (subsumed, "subsumed"); if (subsumed->redundant) stats.subred++; @@ -5030,26 +5034,26 @@ void Closure::subsume_clause (Clause *subsuming, Clause *subsumed) { stats.current.irredundant++; stats.added.irredundant++; stats.irrlits += subsuming->size; - assert (stats.current.redundant > 0); + CADICAL_assert (stats.current.redundant > 0); stats.current.redundant--; - assert (stats.added.redundant > 0); + CADICAL_assert (stats.added.redundant > 0); stats.added.redundant--; // ... and keep 'stats.added.total'. } bool Closure::find_subsuming_clause (Clause *subsumed) { - assert (!subsumed->garbage); + CADICAL_assert (!subsumed->garbage); Clause *subsuming = nullptr; for (auto lit : *subsumed) { - assert (internal->val (lit) <= 0); + CADICAL_assert (internal->val (lit) <= 0); const int repr_lit = find_representative (lit); const signed char repr_val = internal->val (repr_lit); - assert (repr_val <= 0); + CADICAL_assert (repr_val <= 0); if (repr_val < 0) continue; if (marked (repr_lit)) continue; - assert (!marked (-repr_lit)); + CADICAL_assert (!marked (-repr_lit)); marked (repr_lit) = 1; } int least_occuring_lit = 0; @@ -5059,21 +5063,21 @@ bool Closure::find_subsuming_clause (Clause *subsumed) { for (auto lit : *subsumed) { const int repr_lit = find_representative (lit); const size_t count = internal->occs (lit).size (); - assert (count <= UINT_MAX); + CADICAL_assert (count <= UINT_MAX); if (count < count_least_occurring) { count_least_occurring = count; least_occuring_lit = repr_lit; } for (auto d : internal->occs (lit)) { - assert (!d->garbage); - assert (subsumed != d); + CADICAL_assert (!d->garbage); + CADICAL_assert (subsumed != d); if (!subsumed->redundant && d->redundant) continue; for (auto other : *d) { const signed char v = internal->val (other); if (v < 0) continue; - assert (!v); + CADICAL_assert (!v); const int repr_other = find_representative (other); if (!marked (repr_other)) goto CONTINUE_WITH_NEXT_CLAUSE; @@ -5085,7 +5089,7 @@ bool Closure::find_subsuming_clause (Clause *subsumed) { CONTINUE_WITH_NEXT_CLAUSE:; } } - assert (least_occuring_lit); + CADICAL_assert (least_occuring_lit); FOUND_SUBSUMING: for (auto lit : *subsumed) { @@ -5108,7 +5112,7 @@ FOUND_SUBSUMING: /*------------------------------------------------------------------------*/ static bool skip_ite_gate (Gate *g) { - assert (g->tag == Gate_Type::ITE_Gate); + CADICAL_assert (g->tag == Gate_Type::ITE_Gate); if (g->garbage) return true; return false; @@ -5117,12 +5121,12 @@ static bool skip_ite_gate (Gate *g) { void Closure::produce_ite_merge_then_else_reasons ( Gate *g, int src, int dst, std::vector &reasons_implication, std::vector &reasons_back) { - assert (!g->garbage); + CADICAL_assert (!g->garbage); if (!internal->lrat) return; check_correct_ite_flags (g); // no merge is happening actually - assert (g->rhs[1] == find_eager_representative(g->rhs[1]) || g->rhs[2] == find_eager_representative(g->rhs[2])); + CADICAL_assert (g->rhs[1] == find_eager_representative(g->rhs[1]) || g->rhs[2] == find_eager_representative(g->rhs[2])); if (find_eager_representative (g->lhs) == g->rhs[1] || find_eager_representative (g->lhs) == g->rhs[2]) return; if ((g->rhs[1] == src && g->lhs == dst && g->rhs[2] == g->lhs) || @@ -5131,12 +5135,12 @@ void Closure::produce_ite_merge_then_else_reasons ( (g->rhs[2] == -src && g->lhs == -dst && g->rhs[1] == g->lhs)) return; check_ite_lrat_reasons (g, false); - assert (g->rhs.size () == 3); - assert (src == g->rhs[1] || src == g->rhs[2]); - assert (dst == g->rhs[1] || dst == g->rhs[2]); + CADICAL_assert (g->rhs.size () == 3); + CADICAL_assert (src == g->rhs[1] || src == g->rhs[2]); + CADICAL_assert (dst == g->rhs[1] || dst == g->rhs[2]); const int8_t flag = g->degenerated_ite; - assert (!ite_flags_no_then_clauses (flag)); // e = lhs: already merged - assert (!ite_flags_no_else_clauses (flag)); // t = lhs: already merged + CADICAL_assert (!ite_flags_no_then_clauses (flag)); // e = lhs: already merged + CADICAL_assert (!ite_flags_no_else_clauses (flag)); // t = lhs: already merged produce_rewritten_clause_lrat (g->pos_lhs_ids, g->lhs, false); if (ite_flags_neg_cond_lhs (flag)) { LOG ("degenerated case with lhs = -cond"); @@ -5148,8 +5152,8 @@ void Closure::produce_ite_merge_then_else_reasons ( } if (ite_flags_cond_lhs (flag)) { LOG ("degenerated case with lhs = cond"); - assert (g->pos_lhs_ids[0].clause); - assert (g->pos_lhs_ids[3].clause); + CADICAL_assert (g->pos_lhs_ids[0].clause); + CADICAL_assert (g->pos_lhs_ids[3].clause); reasons_back.push_back (g->pos_lhs_ids[3].clause->id); reasons_implication.push_back (g->pos_lhs_ids[0].clause->id); return; @@ -5166,7 +5170,7 @@ void Closure::rewrite_ite_gate_update_lrat_reasons (Gate *g, int src, return; LOG (g, "updating lrat from"); for (auto &litId : g->pos_lhs_ids) { - assert (litId.clause); + CADICAL_assert (litId.clause); if (litId.current_lit == src) litId.current_lit = dst; if (litId.current_lit == -src) @@ -5178,8 +5182,8 @@ void Closure::rewrite_ite_gate_update_lrat_reasons (Gate *g, int src, bool Closure::rewrite_ite_gate_to_and ( Gate *g, int src, int dst, size_t idx1, size_t idx2, int cond_lit_to_learn_if_degenerated) { - assert (internal->lrat_chain.empty ()); - assert (!g->garbage); + CADICAL_assert (internal->lrat_chain.empty ()); + CADICAL_assert (!g->garbage); LOG (g, "rewriting to proper AND"); if (internal->val (g->lhs) > 0) { { @@ -5204,7 +5208,7 @@ bool Closure::rewrite_ite_gate_to_and ( if (!internal->unsat) { const int lit = g->rhs[1]; const char v = internal->val (lit); - assert (dst == g->rhs[0] || dst == g->rhs[1] || -dst == g->rhs[0] || + CADICAL_assert (dst == g->rhs[0] || dst == g->rhs[1] || -dst == g->rhs[0] || -dst == g->rhs[1]); const int other = (dst == g->rhs[0] || dst == g->rhs[1]) ? dst ^ g->rhs[0] ^ g->rhs[1] @@ -5235,10 +5239,10 @@ bool Closure::rewrite_ite_gate_to_and ( LOG ("updating flags"); g->degenerated_and_neg = (g->rhs[1] == -g->lhs || g->rhs[0] == -g->lhs); g->degenerated_and_pos = (g->rhs[0] == g->lhs || g->rhs[1] == g->lhs); - assert (g->rhs.size () == 3); - assert (g->pos_lhs_ids.size () == 4); - assert (idx1 < g->pos_lhs_ids.size ()); - assert (idx2 < g->pos_lhs_ids.size ()); + CADICAL_assert (g->rhs.size () == 3); + CADICAL_assert (g->pos_lhs_ids.size () == 4); + CADICAL_assert (idx1 < g->pos_lhs_ids.size ()); + CADICAL_assert (idx2 < g->pos_lhs_ids.size ()); int lit = g->pos_lhs_ids[idx2].current_lit, other = g->lhs; // TODO: remove argument (void) src; @@ -5250,59 +5254,59 @@ bool Closure::rewrite_ite_gate_to_and ( return false; } - assert (idx1 != (size_t) -1); - assert (idx2 != (size_t) -1); - assert (idx1 < g->pos_lhs_ids.size ()); - assert (idx2 < g->pos_lhs_ids.size ()); + CADICAL_assert (idx1 != (size_t) -1); + CADICAL_assert (idx2 != (size_t) -1); + CADICAL_assert (idx1 < g->pos_lhs_ids.size ()); + CADICAL_assert (idx2 < g->pos_lhs_ids.size ()); Clause *c = g->pos_lhs_ids[idx1].clause; - assert (c->size == 2); + CADICAL_assert (c->size == 2); Clause *d = g->pos_lhs_ids[idx2].clause; - assert (c != d); - assert (c); - assert (d); + CADICAL_assert (c != d); + CADICAL_assert (c); + CADICAL_assert (d); g->pos_lhs_ids.erase (std::remove_if (begin (g->pos_lhs_ids), end (g->pos_lhs_ids), [d] (const LitClausePair &p) { return p.clause == d || !p.clause; }), end (g->pos_lhs_ids)); - assert (g->pos_lhs_ids.size () == 2); - assert (lit); - assert (other); - assert (lit != dst); - assert (other != dst); - assert (lit != other); + CADICAL_assert (g->pos_lhs_ids.size () == 2); + CADICAL_assert (lit); + CADICAL_assert (other); + CADICAL_assert (lit != dst); + CADICAL_assert (other != dst); + CADICAL_assert (lit != other); lrat_chain.push_back (c->id); lrat_chain.push_back (d->id); Clause *e = learn_binary_tmp_or_full_clause (lit, -other); - assert (e); + CADICAL_assert (e); auto long_clause = std::find_if (begin (g->pos_lhs_ids), end (g->pos_lhs_ids), [] (LitClausePair l) { return l.clause->size == 3; }); - assert (long_clause != end (g->pos_lhs_ids)); + CADICAL_assert (long_clause != end (g->pos_lhs_ids)); LOG (long_clause->clause, "new long clause"); g->neg_lhs_ids.push_back (*long_clause); g->pos_lhs_ids.erase (long_clause); - assert (g->pos_lhs_ids.size () == 1); + CADICAL_assert (g->pos_lhs_ids.size () == 1); (void) maybe_promote_tmp_binary_clause (g->pos_lhs_ids[0].clause); g->pos_lhs_ids.push_back ({lit, e}); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (auto litId : g->pos_lhs_ids) { bool found = false; - assert (litId.clause); + CADICAL_assert (litId.clause); for (auto other : *litId.clause) { found = (find_eager_representative (other) == litId.current_lit); if (found) break; } - assert (found); + CADICAL_assert (found); } for (auto id : g->pos_lhs_ids) { LOG (id.clause, "clause after rewriting:"); - assert (id.clause->size == 2); + CADICAL_assert (id.clause->size == 2); } #endif @@ -5332,13 +5336,13 @@ void Closure::produce_ite_merge_lhs_then_else_reasons ( LOG ("cond: %d, merging %d and rewriting to %d", cond_lit, lit_to_merge, other_lit); if (internal->lrat) { - assert (internal->lrat); - assert (g->pos_lhs_ids.size () == 4); + CADICAL_assert (internal->lrat); + CADICAL_assert (g->pos_lhs_ids.size () == 4); if (repr_lhs == -repr_other_lit) { LOG ("special case: %s=%s, checking if other: %s %s", LOGLIT (g->lhs), LOGLIT (-lit_to_merge), LOGLIT (cond_lit), LOGLIT (other_lit)); - assert (repr_lit_to_merge != -repr_lhs); // should have been rewritten before + CADICAL_assert (repr_lit_to_merge != -repr_lhs); // should have been rewritten before if (rewritting_then && repr_cond_lit == repr_lhs) { LOG ("t=-lhs/c=lhs"); @@ -5353,7 +5357,7 @@ void Closure::produce_ite_merge_lhs_then_else_reasons ( reasons_implication.push_back (id_unit); g->pos_lhs_ids[3].clause = produce_rewritten_clause_lrat ( g->pos_lhs_ids[3].clause, g->lhs, false); - assert (g->pos_lhs_ids[3].clause); + CADICAL_assert (g->pos_lhs_ids[3].clause); reasons_implication.push_back (g->pos_lhs_ids[3].clause->id); unsimplified.clear (); return; @@ -5371,7 +5375,7 @@ void Closure::produce_ite_merge_lhs_then_else_reasons ( reasons_implication.push_back (id_unit); g->pos_lhs_ids[0].clause = produce_rewritten_clause_lrat ( g->pos_lhs_ids[0].clause, g->lhs, false); - assert (g->pos_lhs_ids[0].clause); + CADICAL_assert (g->pos_lhs_ids[0].clause); reasons_implication.push_back (g->pos_lhs_ids[0].clause->id); unsimplified.clear (); return; @@ -5394,7 +5398,7 @@ void Closure::produce_ite_merge_lhs_then_else_reasons ( reasons_unit = {id_unit}; g->pos_lhs_ids[1].clause = produce_rewritten_clause_lrat ( g->pos_lhs_ids[1].clause, g->lhs, false); - assert (g->pos_lhs_ids[1].clause); + CADICAL_assert (g->pos_lhs_ids[1].clause); // don't bother finding out which one is used reasons_implication.push_back (id_unit); @@ -5412,7 +5416,7 @@ void Closure::produce_ite_merge_lhs_then_else_reasons ( reasons_unit = {id_unit}; g->pos_lhs_ids[2].clause = produce_rewritten_clause_lrat ( g->pos_lhs_ids[2].clause, g->lhs, false); - assert (g->pos_lhs_ids[2].clause); + CADICAL_assert (g->pos_lhs_ids[2].clause); reasons_implication.push_back (id_unit); reasons_implication.push_back (g->pos_lhs_ids[2].clause->id); @@ -5426,8 +5430,8 @@ void Closure::produce_ite_merge_lhs_then_else_reasons ( g->pos_lhs_ids[idx1].clause, g->lhs, false); g->pos_lhs_ids[idx2].clause = produce_rewritten_clause_lrat ( g->pos_lhs_ids[idx2].clause, g->lhs, false); - assert (g->pos_lhs_ids[idx1].clause); - assert (g->pos_lhs_ids[idx2].clause); + CADICAL_assert (g->pos_lhs_ids[idx1].clause); + CADICAL_assert (g->pos_lhs_ids[idx2].clause); lrat_chain.push_back (g->pos_lhs_ids[idx1].clause->id); lrat_chain.push_back (g->pos_lhs_ids[idx2].clause->id); unsimplified.push_back (-cond_lit); @@ -5444,8 +5448,8 @@ void Closure::produce_ite_merge_lhs_then_else_reasons ( g->pos_lhs_ids[idx1].clause, g->lhs, false); g->pos_lhs_ids[idx2].clause = produce_rewritten_clause_lrat ( g->pos_lhs_ids[idx2].clause, g->lhs, false); - assert (g->pos_lhs_ids[idx1].clause); - assert (g->pos_lhs_ids[idx2].clause); + CADICAL_assert (g->pos_lhs_ids[idx1].clause); + CADICAL_assert (g->pos_lhs_ids[idx2].clause); lrat_chain.push_back (g->pos_lhs_ids[idx1].clause->id); lrat_chain.push_back (g->pos_lhs_ids[idx2].clause->id); unsimplified.push_back (cond_lit); @@ -5463,23 +5467,23 @@ void Closure::produce_ite_merge_lhs_then_else_reasons ( g->pos_lhs_ids[idx1].clause, g->lhs, false); g->pos_lhs_ids[idx2].clause = produce_rewritten_clause_lrat ( g->pos_lhs_ids[idx2].clause, g->lhs, false); - assert (g->pos_lhs_ids[idx1].clause); - assert (g->pos_lhs_ids[idx2].clause); + CADICAL_assert (g->pos_lhs_ids[idx1].clause); + CADICAL_assert (g->pos_lhs_ids[idx2].clause); reasons_unit.push_back (g->pos_lhs_ids[idx1].clause->id); reasons_unit.push_back (g->pos_lhs_ids[idx2].clause->id); return; } // if (other_lit == -g->lhs) { - // assert (false); + // CADICAL_assert (false); // } // if (other_lit == g->lhs) { - // assert (false); + // CADICAL_assert (false); // } } LOG ("normal path"); produce_rewritten_clause_lrat (g->pos_lhs_ids, g->lhs, false); - assert (g->pos_lhs_ids.size () == 4); + CADICAL_assert (g->pos_lhs_ids.size () == 4); reasons_unit.push_back (g->pos_lhs_ids[idx1].clause->id); reasons_unit.push_back (g->pos_lhs_ids[idx2].clause->id); @@ -5520,15 +5524,15 @@ void Closure::produce_ite_merge_lhs_then_else_reasons ( } void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { - assert (unsimplified.empty ()); + CADICAL_assert (unsimplified.empty ()); if (skip_ite_gate (g)) return; if (!gate_contains (g, src)) return; LOG (g, "rewriting %d by %d in", src, dst); - assert (!g->shrunken); - assert (g->rhs.size () == 3); - assert (!internal->lrat || g->pos_lhs_ids.size () == 4); + CADICAL_assert (!g->shrunken); + CADICAL_assert (g->rhs.size () == 3); + CADICAL_assert (!internal->lrat || g->pos_lhs_ids.size () == 4); auto &rhs = g->rhs; const int lhs = g->lhs; const int cond = g->rhs[0]; @@ -5544,16 +5548,16 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { bool garbage = false; bool shrink = true; const auto git = g->indexed ? table.find (g) : end (table); - assert (!g->indexed || git != end (table)); - assert (*git == g); + CADICAL_assert (!g->indexed || git != end (table)); + CADICAL_assert (*git == g); if (internal->val (cond) && internal->val (then_lit) && internal->val (else_lit)) { // propagation has set all value anyway LOG (g, "all values are set"); - assert (internal->val (g->lhs)); + CADICAL_assert (internal->val (g->lhs)); garbage = true; } else if (internal->val (g->lhs) && internal->val (cond)) { LOG (g, "all values are set 2"); - assert (internal->val (g->lhs)); + CADICAL_assert (internal->val (g->lhs)); garbage = true; } // this code is taken one-to-one from kissat @@ -5576,14 +5580,14 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { // !then_lit & then_lit | then_lit & else_lit // then_lit & else_lit rhs[0] = else_lit; - assert (rhs[1] == then_lit); + CADICAL_assert (rhs[1] == then_lit); garbage = rewrite_ite_gate_to_and (g, src, dst, 0, 2, -dst); } else if (dst == else_lit) { // else_list ? then_lit : else_lit // else_list & then_lit | !else_list & else_lit // else_list & then_lit rhs[0] = else_lit; - assert (rhs[1] == then_lit); + CADICAL_assert (rhs[1] == then_lit); garbage = rewrite_ite_gate_to_and (g, src, dst, 2, 0, dst); } else if (not_dst == else_lit) { // !else_list ? then_lit : else_lit @@ -5680,7 +5684,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { produce_rewritten_clause_lrat_and_clean (g->pos_lhs_ids, g->lhs, false); if (internal->lrat) { - assert (g->pos_lhs_ids.size () == 2); + CADICAL_assert (g->pos_lhs_ids.size () == 2); lrat_chain.push_back (g->pos_lhs_ids[0].clause->id); lrat_chain.push_back (g->pos_lhs_ids[1].clause->id); } @@ -5689,9 +5693,9 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { } else { LOG ("changing to xor"); new_tag = Gate_Type::XOr_Gate; - assert (rhs[0] == cond); + CADICAL_assert (rhs[0] == cond); rhs[1] = else_lit; - assert (!internal->lrat || !g->pos_lhs_ids.empty ()); + CADICAL_assert (!internal->lrat || !g->pos_lhs_ids.empty ()); { #ifdef LOGGING for (auto litId : g->pos_lhs_ids) { @@ -5713,7 +5717,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { rewrite_ite_gate_update_lrat_reasons (g, src, dst); } } else { - assert (src == else_lit); + CADICAL_assert (src == else_lit); if (not_dst == g->lhs) { // TODO not in kissat rhs[2] = dst; std::vector reasons_implication, reasons_back, reasons_unit; @@ -5749,8 +5753,8 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { // cond ? then_lit : cond // cond & then_lit | !cond & cond // cond & then_lit - assert (rhs[0] == cond); - assert (rhs[1] == then_lit); + CADICAL_assert (rhs[0] == cond); + CADICAL_assert (rhs[1] == then_lit); garbage = rewrite_ite_gate_to_and (g, src, dst, 2, 0, cond); } else if (not_dst == cond) { // cond ? then_lit : !cond @@ -5759,7 +5763,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { // then_lit | !cond // !(!then_lit & cond) g->lhs = not_lhs; - assert (rhs[0] == cond); + CADICAL_assert (rhs[0] == cond); rhs[1] = not_then_lit; if (then_lit == lhs || cond == lhs) garbage = true; @@ -5785,7 +5789,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { produce_rewritten_clause_lrat_and_clean (g->pos_lhs_ids, not_lhs, false); if (internal->lrat) { - assert (g->pos_lhs_ids.size () == 2); + CADICAL_assert (g->pos_lhs_ids.size () == 2); lrat_chain.push_back (g->pos_lhs_ids[0].clause->id); lrat_chain.push_back (g->pos_lhs_ids[1].clause->id); } @@ -5795,7 +5799,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { produce_rewritten_clause_lrat_and_clean (g->pos_lhs_ids, not_lhs, false); if (internal->lrat) { - assert (g->pos_lhs_ids.size () == 2); + CADICAL_assert (g->pos_lhs_ids.size () == 2); lrat_chain.push_back (g->pos_lhs_ids[0].clause->id); lrat_chain.push_back (g->pos_lhs_ids[1].clause->id); } @@ -5805,7 +5809,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { produce_rewritten_clause_lrat_and_clean (g->pos_lhs_ids, not_lhs, false); if (internal->lrat) { - assert (g->pos_lhs_ids.size () == 2); + CADICAL_assert (g->pos_lhs_ids.size () == 2); lrat_chain.push_back (g->pos_lhs_ids[0].clause->id); lrat_chain.push_back (g->pos_lhs_ids[1].clause->id); } @@ -5814,10 +5818,10 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { } else { new_tag = Gate_Type::XOr_Gate; g->lhs = not_lhs; - assert (rhs[0] == cond); - assert (rhs[1] == then_lit); - assert (rhs[0] != g->lhs); - assert (rhs[1] != g->lhs); + CADICAL_assert (rhs[0] == cond); + CADICAL_assert (rhs[1] == then_lit); + CADICAL_assert (rhs[0] != g->lhs); + CADICAL_assert (rhs[1] != g->lhs); produce_rewritten_clause_lrat_and_clean (g->pos_lhs_ids, not_lhs, false); } @@ -5828,8 +5832,8 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { } } if (!garbage && !internal->unsat) { - assert (new_tag != Gate_Type::ITE_Gate || g->lhs != -rhs[1]); - assert (new_tag != Gate_Type::ITE_Gate || g->lhs != -rhs[2]); + CADICAL_assert (new_tag != Gate_Type::ITE_Gate || g->lhs != -rhs[1]); + CADICAL_assert (new_tag != Gate_Type::ITE_Gate || g->lhs != -rhs[2]); if (shrink) { if (new_tag == Gate_Type::XOr_Gate) { bool negate_lhs = false; @@ -5848,15 +5852,15 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { internal->vlit ( rhs[1])) { // unlike kissat, we need to do it after negating std::swap (rhs[0], rhs[1]); - assert (new_tag != Gate_Type::ITE_Gate); + CADICAL_assert (new_tag != Gate_Type::ITE_Gate); } - assert (internal->vlit (rhs[0]) < internal->vlit (rhs[1])); - assert (!g->shrunken); + CADICAL_assert (internal->vlit (rhs[0]) < internal->vlit (rhs[1])); + CADICAL_assert (!g->shrunken); g->shrunken = true; rhs[2] = 0; g->tag = new_tag; rhs.resize (2); - assert (rhs[0] != -rhs[1]); + CADICAL_assert (rhs[0] != -rhs[1]); if (new_tag == Gate_Type::XOr_Gate) { if (rhs[0] == -g->lhs || rhs[1] == -g->lhs) { LOG (g, "special XOR:"); @@ -5864,7 +5868,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { produce_rewritten_clause_lrat_and_clean (g->pos_lhs_ids, not_lhs, false); if (internal->lrat) { - assert (g->pos_lhs_ids.size () == 2); + CADICAL_assert (g->pos_lhs_ids.size () == 2); lrat_chain.push_back (g->pos_lhs_ids[0].clause->id); lrat_chain.push_back (g->pos_lhs_ids[1].clause->id); } @@ -5876,7 +5880,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { produce_rewritten_clause_lrat_and_clean (g->pos_lhs_ids, not_lhs, false); if (internal->lrat) { - assert (g->pos_lhs_ids.size () == 2); + CADICAL_assert (g->pos_lhs_ids.size () == 2); lrat_chain.push_back (g->pos_lhs_ids[0].clause->id); lrat_chain.push_back (g->pos_lhs_ids[1].clause->id); } @@ -5886,7 +5890,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { int i = 0; bool negated = false; for (int j = 0; j < 2; ++i, ++j) { - assert (i <= j); + CADICAL_assert (i <= j); const int lit = rhs[i] = rhs[j]; const char v = internal->val (lit); if (v > 0) { @@ -5896,7 +5900,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { --i; } } - assert (i <= 2); + CADICAL_assert (i <= 2); rhs.resize (i); if (negated) { g->lhs = -g->lhs; @@ -5913,10 +5917,10 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { } #endif produce_rewritten_clause_lrat_and_clean (g->pos_lhs_ids, g->lhs); - assert (!internal->lrat || g->pos_lhs_ids.size () == 2); + CADICAL_assert (!internal->lrat || g->pos_lhs_ids.size () == 2); Clause *c1 = nullptr, *c2 = nullptr; if (internal->lrat) { - assert (g->pos_lhs_ids[0].clause); + CADICAL_assert (g->pos_lhs_ids[0].clause); bool rhs_as_src_first = g->pos_lhs_ids[0].clause->literals[0] == g->lhs || g->pos_lhs_ids[0].clause->literals[1] == g->lhs; @@ -5935,10 +5939,10 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { } } if (!garbage) { - assert (rhs[0] != g->lhs); - assert (rhs[1] != g->lhs); - assert (rhs[0] != -g->lhs); - assert (rhs[1] != -g->lhs); + CADICAL_assert (rhs[0] != g->lhs); + CADICAL_assert (rhs[1] != g->lhs); + CADICAL_assert (rhs[0] != -g->lhs); + CADICAL_assert (rhs[1] != -g->lhs); } } @@ -5948,10 +5952,10 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { if (internal->lrat) { if (new_tag == Gate_Type::XOr_Gate) { -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG std::for_each (begin (g->pos_lhs_ids), end (g->pos_lhs_ids), [g] (LitClausePair l) { - assert ((size_t) l.clause->size == + CADICAL_assert ((size_t) l.clause->size == 1 + g->arity ()); }); #endif @@ -5959,25 +5963,29 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { // we have to get rid of one clause, two become binaries, and // becomes ternary -#if defined(LOGGING) || !defined(NDEBUG) +#if defined(LOGGING) || !defined(CADICAL_NDEBUG) for (auto id : g->pos_lhs_ids) { LOG (id.clause, "clause after rewriting:"); - assert (id.clause->size == 2); + CADICAL_assert (id.clause->size == 2); } #endif - assert (g->pos_lhs_ids.size () == 2 || + CADICAL_assert (g->pos_lhs_ids.size () == 2 || gate_contains (g, g->lhs)); - assert (g->neg_lhs_ids.size () == 1 || + CADICAL_assert (g->neg_lhs_ids.size () == 1 || gate_contains (g, g->lhs)); - assert (g->arity () == 2); -#ifndef NDEBUG + CADICAL_assert (g->arity () == 2); +#ifndef CADICAL_NDEBUG std::for_each ( begin (g->pos_lhs_ids), end (g->pos_lhs_ids), - [] (LitClausePair l) { assert (l.clause->size == 2); }); + [] (LitClausePair l) { CADICAL_assert (l.clause->size == 2); }); #endif } else { - assert (false); + CADICAL_assert (false); +#ifdef WIN32 + __assume(false); +#else __builtin_unreachable (); +#endif } } Gate *h; @@ -5985,7 +5993,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { check_and_gate_implied (g); h = find_and_lits (rhs); } else { - assert (new_tag == Gate_Type::XOr_Gate); + CADICAL_assert (new_tag == Gate_Type::XOr_Gate); check_xor_gate_implied (g); h = find_xor_gate (g); } @@ -6015,7 +6023,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { if (g->indexed) remove_gate (git); index_gate (g); - assert (g->arity () == 2); + CADICAL_assert (g->arity () == 2); for (auto lit : g->rhs) if (lit != dst) if (lit != cond && lit != then_lit && lit != else_lit) @@ -6029,18 +6037,18 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { LOG (g, "rewritten"); if (internal->lrat) update_ite_flags (g), check_correct_ite_flags(g); - assert (rhs[0] != rhs[1]); - assert (rhs[0] != rhs[2]); - assert (rhs[1] != rhs[2]); - assert (rhs[0] != -(rhs[1])); - assert (rhs[0] != -(rhs[2])); - assert (rhs[1] != -(rhs[2])); + CADICAL_assert (rhs[0] != rhs[1]); + CADICAL_assert (rhs[0] != rhs[2]); + CADICAL_assert (rhs[1] != rhs[2]); + CADICAL_assert (rhs[0] != -(rhs[1])); + CADICAL_assert (rhs[0] != -(rhs[2])); + CADICAL_assert (rhs[1] != -(rhs[2])); check_ite_gate_implied (g); check_ite_lrat_reasons (g, false); bool negate_lhs; Gate *h = find_ite_gate (g, negate_lhs); - assert (lhs == g->lhs); - assert (not_lhs == -(g->lhs)); + CADICAL_assert (lhs == g->lhs); + CADICAL_assert (not_lhs == -(g->lhs)); if (negate_lhs) g->lhs = -lhs; check_ite_lrat_reasons (g); @@ -6061,7 +6069,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { extra_reasons_ulit)) ++internal->stats.congruence.ites; delete_proof_chain (); - assert (internal->unsat || chain.empty ()); + CADICAL_assert (internal->unsat || chain.empty ()); } else { garbage = false; if (g->indexed) @@ -6069,7 +6077,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { LOG (g, "normalized"); g->hash = hash_lits (nonces, g->rhs); index_gate (g); - assert (g->arity () == 3); + CADICAL_assert (g->arity () == 3); for (auto lit : g->rhs) if (lit != dst) if (lit != cond && lit != then_lit && lit != else_lit) @@ -6080,7 +6088,7 @@ void Closure::rewrite_ite_gate (Gate *g, int dst, int src) { if (garbage && !internal->unsat) mark_garbage (g); - assert (chain.empty ()); + CADICAL_assert (chain.empty ()); } void Closure::simplify_ite_gate_produce_unit_lrat (Gate *g, int lit, @@ -6091,13 +6099,13 @@ void Closure::simplify_ite_gate_produce_unit_lrat (Gate *g, int lit, // TODO if (internal->val (lit) > 0) return; - assert (internal->lrat); - assert (g); - assert (idx1 < g->pos_lhs_ids.size ()); - assert (idx2 < g->pos_lhs_ids.size ()); - assert (g->pos_lhs_ids.size () == 4); + CADICAL_assert (internal->lrat); + CADICAL_assert (g); + CADICAL_assert (idx1 < g->pos_lhs_ids.size ()); + CADICAL_assert (idx2 < g->pos_lhs_ids.size ()); + CADICAL_assert (g->pos_lhs_ids.size () == 4); - assert (idx1 != idx2); + CADICAL_assert (idx1 != idx2); Clause *c = g->pos_lhs_ids[idx1].clause; Clause *d = g->pos_lhs_ids[idx2].clause; @@ -6105,22 +6113,22 @@ void Closure::simplify_ite_gate_produce_unit_lrat (Gate *g, int lit, LOG ("special case of LHS=-cond where only one clause in LRAT is needed is needed"); size_t idx = (internal->val (g->rhs[1]) < 0 ? idx2 : idx1); c = produce_rewritten_clause_lrat (g->pos_lhs_ids[idx].clause, g->lhs, false, false); - assert (c); + CADICAL_assert (c); // not possible to do this in a single lrat chain push_id_and_rewriting_lrat_unit (c, Rewrite (), lrat_chain, true, Rewrite (), g->lhs); - assert (d); + CADICAL_assert (d); return; } if (g->lhs == g->rhs[0]) { LOG ("special case of LHS=cond where only one clause in LRAT is needed is needed"); size_t idx = (internal->val (g->rhs[1]) > 0 ? idx2 : idx1); c = produce_rewritten_clause_lrat (g->pos_lhs_ids[idx].clause, g->lhs, false, false); - assert (c); + CADICAL_assert (c); // not possible to do this in a single lrat chain push_id_and_rewriting_lrat_unit (c, Rewrite (), lrat_chain, true, Rewrite (), g->lhs); - assert (d); + CADICAL_assert (d); return; } @@ -6133,7 +6141,7 @@ void Closure::simplify_ite_gate_produce_unit_lrat (Gate *g, int lit, } else if (!c) { push_id_and_rewriting_lrat_unit (d, Rewrite (), lrat_chain, true, Rewrite (), g->lhs); - assert (d); + CADICAL_assert (d); lrat_chain.push_back (d->id); } } @@ -6142,10 +6150,10 @@ void Closure::simplify_ite_gate_produce_unit_lrat (Gate *g, int lit, void Closure::merge_and_gate_lrat_produce_lrat ( Gate *g, Gate *h, std::vector &reasons_lrat_src, std::vector &reasons_lrat_usrc, bool remove_units) { - assert (internal->lrat); - assert (g->tag == Gate_Type::And_Gate); - assert (h->tag == Gate_Type::And_Gate); - assert (g->neg_lhs_ids.size () <= 1); + CADICAL_assert (internal->lrat); + CADICAL_assert (g->tag == Gate_Type::And_Gate); + CADICAL_assert (h->tag == Gate_Type::And_Gate); + CADICAL_assert (g->neg_lhs_ids.size () <= 1); update_and_gate_build_lrat_chain (g, h, reasons_lrat_src, reasons_lrat_usrc, remove_units); } @@ -6153,8 +6161,8 @@ void Closure::merge_and_gate_lrat_produce_lrat ( // odd copy of rewrite_ite_gate_lrat_and bool Closure::simplify_ite_gate_to_and (Gate *g, size_t idx1, size_t idx2, int removed_lit) { - assert (internal->lrat_chain.empty ()); - assert (g->rhs.size () == 3); + CADICAL_assert (internal->lrat_chain.empty ()); + CADICAL_assert (g->rhs.size () == 3); #ifdef LOGGING for (auto litId : g->pos_lhs_ids) { LOG (litId.clause, "%d ->", litId.current_lit); @@ -6164,7 +6172,7 @@ bool Closure::simplify_ite_gate_to_and (Gate *g, size_t idx1, size_t idx2, if (internal->lrat) { Clause *c = g->pos_lhs_ids[idx1].clause; push_id_and_rewriting_lrat_unit (c, Rewrite (), lrat_chain); - assert (!lrat_chain.empty ()); + CADICAL_assert (!lrat_chain.empty ()); } learn_congruence_unit (-g->lhs); return true; @@ -6174,9 +6182,9 @@ bool Closure::simplify_ite_gate_to_and (Gate *g, size_t idx1, size_t idx2, g->degenerated_and_neg = (g->degenerated_and_neg || g->rhs[1] == -g->lhs || g->rhs[0] == -g->lhs); g->degenerated_and_pos = (g->degenerated_and_pos || g->rhs[0] == g->lhs || g->rhs[1] == g->lhs); - assert (g->pos_lhs_ids.size () == 4); - assert (idx1 < g->pos_lhs_ids.size ()); - assert (idx2 < g->pos_lhs_ids.size ()); + CADICAL_assert (g->pos_lhs_ids.size () == 4); + CADICAL_assert (idx1 < g->pos_lhs_ids.size ()); + CADICAL_assert (idx2 < g->pos_lhs_ids.size ()); int lit = g->pos_lhs_ids[idx2].current_lit, other = g->lhs; size_t new_idx1 = idx1; size_t new_idx2 = idx2; @@ -6187,7 +6195,7 @@ bool Closure::simplify_ite_gate_to_and (Gate *g, size_t idx1, size_t idx2, LOG (g, "degenerated AND gate"); const int replacement_lit = (g->rhs[1] == g->lhs ? g->rhs[0] : g->rhs[1]); for (auto &litId : g->pos_lhs_ids) { - assert (litId.clause); + CADICAL_assert (litId.clause); LOG (litId.clause, "%d ->", litId.current_lit); if (litId.current_lit == removed_lit) litId.current_lit = -replacement_lit; @@ -6195,32 +6203,32 @@ bool Closure::simplify_ite_gate_to_and (Gate *g, size_t idx1, size_t idx2, litId.current_lit = replacement_lit; LOG (litId.clause, "%d ->", litId.current_lit); // TODO we need a replacement index - assert (std::find (begin (*litId.clause), end (*litId.clause), litId.current_lit) != + CADICAL_assert (std::find (begin (*litId.clause), end (*litId.clause), litId.current_lit) != end (*litId.clause)); - assert (std::find (begin (g->rhs), end (g->rhs), litId.current_lit) != + CADICAL_assert (std::find (begin (g->rhs), end (g->rhs), litId.current_lit) != end (g->rhs)); } return false; } - assert (new_idx1 < g->pos_lhs_ids.size ()); - assert (new_idx2 < g->pos_lhs_ids.size ()); + CADICAL_assert (new_idx1 < g->pos_lhs_ids.size ()); + CADICAL_assert (new_idx2 < g->pos_lhs_ids.size ()); Clause *c = g->pos_lhs_ids[new_idx1].clause; - assert (c->size == 2); - assert (new_idx1 != new_idx2); + CADICAL_assert (c->size == 2); + CADICAL_assert (new_idx1 != new_idx2); Clause *d = g->pos_lhs_ids[new_idx2].clause; - assert (c != d); - assert (c); - assert (d); + CADICAL_assert (c != d); + CADICAL_assert (c); + CADICAL_assert (d); g->pos_lhs_ids.erase (std::remove_if (begin (g->pos_lhs_ids), end (g->pos_lhs_ids), [d] (const LitClausePair &p) { return p.clause == d; }), end (g->pos_lhs_ids)); - assert (g->pos_lhs_ids.size () == 2); - assert (lit); - assert (other); - assert (lit != other); + CADICAL_assert (g->pos_lhs_ids.size () == 2); + CADICAL_assert (lit); + CADICAL_assert (other); + CADICAL_assert (lit != other); lrat_chain.push_back (c->id); lrat_chain.push_back (d->id); Clause *e = add_tmp_binary_clause (lit, -other); @@ -6228,20 +6236,20 @@ bool Closure::simplify_ite_gate_to_and (Gate *g, size_t idx1, size_t idx2, auto long_clause = std::find_if (begin (g->pos_lhs_ids), end (g->pos_lhs_ids), [] (LitClausePair l) { return l.clause->size == 3; }); - assert (long_clause != end (g->pos_lhs_ids)); + CADICAL_assert (long_clause != end (g->pos_lhs_ids)); LOG (long_clause->clause, "new long clause"); g->neg_lhs_ids.push_back (*long_clause); g->pos_lhs_ids.erase (long_clause); - assert (g->pos_lhs_ids.size () == 1); + CADICAL_assert (g->pos_lhs_ids.size () == 1); g->pos_lhs_ids.push_back ({lit, e}); const int first_lit = g->rhs[0]; const int second_lit = g->rhs[1]; for (auto &litId : g->pos_lhs_ids) { - assert (litId.clause); + CADICAL_assert (litId.clause); LOG (litId.clause, "%s ->", LOGLIT (litId.current_lit)); if (internal->val (litId.current_lit)) { - assert (litId.clause->size == 2); + CADICAL_assert (litId.clause->size == 2); int replacement_lit = 0; for (int i = 0; i < 2; ++i) { if (litId.clause->literals[i] == first_lit) { @@ -6252,7 +6260,7 @@ bool Closure::simplify_ite_gate_to_and (Gate *g, size_t idx1, size_t idx2, break; } } - assert (replacement_lit); + CADICAL_assert (replacement_lit); litId.current_lit = replacement_lit; } else if (litId.current_lit == removed_lit) @@ -6261,9 +6269,9 @@ bool Closure::simplify_ite_gate_to_and (Gate *g, size_t idx1, size_t idx2, litId.current_lit = g->rhs[0]; LOG (litId.clause, "%d ->", litId.current_lit); // TODO we need a replacement index - assert (std::find (begin (g->rhs), end (g->rhs), litId.current_lit) != + CADICAL_assert (std::find (begin (g->rhs), end (g->rhs), litId.current_lit) != end (g->rhs)); - assert (std::find (begin (*litId.clause), end (*litId.clause), + CADICAL_assert (std::find (begin (*litId.clause), end (*litId.clause), litId.current_lit) != end (*litId.clause)); } return false; @@ -6273,9 +6281,9 @@ void Closure::merge_ite_gate_same_then_else_lrat ( std::vector &clauses, std::vector &reasons_implication, std::vector &reasons_back) { - assert (clauses.size () == 4); + CADICAL_assert (clauses.size () == 4); produce_rewritten_clause_lrat_and_clean (clauses); - assert (clauses.size () == 4); + CADICAL_assert (clauses.size () == 4); auto then_imp = clauses[0]; auto neg_then_imp = clauses[1]; auto else_imp = clauses[2]; @@ -6289,8 +6297,8 @@ void Closure::merge_ite_gate_same_then_else_lrat ( void Closure::simplify_ite_gate_then_else_set ( Gate *g, std::vector &reasons_implication, std::vector &reasons_back, size_t idx1, size_t idx2) { - assert (idx1 < g->pos_lhs_ids.size ()); - assert (idx2 < g->pos_lhs_ids.size ()); + CADICAL_assert (idx1 < g->pos_lhs_ids.size ()); + CADICAL_assert (idx2 < g->pos_lhs_ids.size ()); Clause *c = g->pos_lhs_ids[idx1].clause; Clause *d = g->pos_lhs_ids[idx2].clause; push_id_and_rewriting_lrat_unit (c, Rewrite (), reasons_back, true, @@ -6300,11 +6308,11 @@ void Closure::simplify_ite_gate_then_else_set ( LOG (reasons_back, "LRAT"); LOG (reasons_implication, "LRAT"); // c = produce_rewritten_clause_lrat (c, g->lhs, false); - // assert (c); + // CADICAL_assert (c); // d = produce_rewritten_clause_lrat (d, g->lhs, false); - // assert (d); + // CADICAL_assert (d); // const int cond = g->rhs[0]; - // assert (internal->val (cond)); + // CADICAL_assert (internal->val (cond)); // reasons_implication.push_back(internal->unit_id (internal->val (cond) > // 0 ? cond : -cond)); reasons_implication.push_back(c->id); // reasons_back.push_back(internal->unit_id (internal->val (cond) > 0 ? @@ -6314,12 +6322,12 @@ void Closure::simplify_ite_gate_then_else_set ( void Closure::simplify_ite_gate_condition_set ( Gate *g, std::vector &reasons_lrat, std::vector &reasons_back_lrat, size_t idx1, size_t idx2) { - assert (internal->lrat); + CADICAL_assert (internal->lrat); Clause *c = g->pos_lhs_ids[idx1].clause; Clause *d = g->pos_lhs_ids[idx2].clause; -#if defined(LOGGING) || !defined(NDEBUG) +#if defined(LOGGING) || !defined(CADICAL_NDEBUG) const int cond = g->rhs[0]; - assert (internal->val (cond)); + CADICAL_assert (internal->val (cond)); LOG ("cond = %d", cond); #endif #ifdef LOGGING @@ -6336,7 +6344,7 @@ void Closure::simplify_ite_gate (Gate *g) { if (skip_ite_gate (g)) return; LOG (g, "simplifying"); - assert (g->arity () == 3); + CADICAL_assert (g->arity () == 3); bool garbage = true; int lhs = g->lhs; auto &rhs = g->rhs; @@ -6349,7 +6357,7 @@ void Closure::simplify_ite_gate (Gate *g) { std::vector reasons_lrat, reasons_back_lrat; if (v_cond && v_else && v_then) { // propagation has set all value anyway LOG (g, "all values are set"); - assert (internal->val (g->lhs)); + CADICAL_assert (internal->val (g->lhs)); garbage = true; } else if (v_cond > 0) { if (internal->lrat) @@ -6372,7 +6380,7 @@ void Closure::simplify_ite_gate (Gate *g) { } else { LOG ("then %d: %d; else %d: %d", then_lit, v_then, else_lit, v_else); std::vector extra_reasons, extra_reasons_back; - assert (v_then || v_else); + CADICAL_assert (v_then || v_else); if (v_then > 0 && v_else > 0) { simplify_ite_gate_produce_unit_lrat (g, lhs, 1, 3); learn_congruence_unit (lhs); @@ -6398,9 +6406,9 @@ void Closure::simplify_ite_gate (Gate *g) { ++internal->stats.congruence.unaries; } } else { - assert (!!v_then + !!v_else == 1); + CADICAL_assert (!!v_then + !!v_else == 1); auto git = g->indexed ? table.find (g) : end (table); - assert (!g->indexed || git != end (table)); + CADICAL_assert (!g->indexed || git != end (table)); if (v_then > 0) { g->lhs = -lhs; rhs[0] = -cond; @@ -6416,7 +6424,7 @@ void Closure::simplify_ite_gate (Gate *g) { rhs[1] = cond; simplify_ite_gate_to_and (g, 3, 1, else_lit); } else { - assert (v_else < 0); + CADICAL_assert (v_else < 0); rhs[0] = then_lit; rhs[1] = cond; simplify_ite_gate_to_and (g, 2, 0, -else_lit); @@ -6428,13 +6436,13 @@ void Closure::simplify_ite_gate (Gate *g) { g->shrunken = true; g->tag = Gate_Type::And_Gate; rhs.resize (2); - assert (is_sorted (begin (rhs), end (rhs), + CADICAL_assert (is_sorted (begin (rhs), end (rhs), sort_literals_by_var_smaller (internal))); g->hash = hash_lits (nonces, rhs); check_and_gate_implied (g); Gate *h = find_and_lits (rhs); if (h) { - assert (garbage); + CADICAL_assert (garbage); std::vector reasons_lrat, reasons_lrat_back; if (internal->lrat) merge_and_gate_lrat_produce_lrat (g, h, reasons_lrat, @@ -6471,16 +6479,16 @@ void Closure::add_ite_matching_proof_chain ( std::vector &reasons2) { check_ite_lrat_reasons (g); check_ite_lrat_reasons (h, false); - assert (g->lhs == lhs1); - assert (h->lhs == lhs2); + CADICAL_assert (g->lhs == lhs1); + CADICAL_assert (h->lhs == lhs2); if (lhs1 == lhs2) return; if (!internal->proof) return; LOG (g, "starting ITE matching proof chain"); LOG (h, "starting ITE matching proof chain with"); - assert (unsimplified.empty ()); - assert (chain.empty ()); + CADICAL_assert (unsimplified.empty ()); + CADICAL_assert (chain.empty ()); if (internal->lrat) check_correct_ite_flags (g); const auto &rhs = g->rhs; @@ -6497,11 +6505,11 @@ void Closure::add_ite_matching_proof_chain ( const bool degenerated_g_cond = ite_flags_cond_lhs (flags_g); const bool degenerated_h_cond = ite_flags_cond_lhs (flags_h); - assert (!(degenerated_g_cond && degenerated_h_cond)); + CADICAL_assert (!(degenerated_g_cond && degenerated_h_cond)); const bool degenerated_g_not_cond = ite_flags_neg_cond_lhs (flags_g); const bool degenerated_h_not_cond = ite_flags_neg_cond_lhs (flags_h); - assert (!(degenerated_g_not_cond && degenerated_h_not_cond)); + CADICAL_assert (!(degenerated_g_not_cond && degenerated_h_not_cond)); if (internal->lrat) { // the code can produce tautologies in the case that: @@ -6517,7 +6525,7 @@ void Closure::add_ite_matching_proof_chain ( // b = (c ? t : a) // (no clauses with (then) index a and (else) index -e) LOG (g, "get ids from"); - assert (g->pos_lhs_ids.size () == 4); + CADICAL_assert (g->pos_lhs_ids.size () == 4); auto &g_then_clause = g->pos_lhs_ids[0].clause; g_then_clause = g_then_clause ? produce_rewritten_clause_lrat (g_then_clause, g->lhs, false) : nullptr; @@ -6543,7 +6551,7 @@ void Closure::add_ite_matching_proof_chain ( g_neg_else_id = g_neg_else_clause->id; LOG (h, "now clauses from"); - assert (h->pos_lhs_ids.size () == 4); + CADICAL_assert (h->pos_lhs_ids.size () == 4); auto &h_then_clause = h->pos_lhs_ids[0].clause; h_then_clause = h_then_clause ? produce_rewritten_clause_lrat (h_then_clause, h->lhs, false) : nullptr; @@ -6591,8 +6599,8 @@ void Closure::add_ite_matching_proof_chain ( } id2 = simplify_and_add_to_proof_chain (unsimplified); - assert (!internal->lrat || id1); - assert (!internal->lrat || id2); + CADICAL_assert (!internal->lrat || id1); + CADICAL_assert (!internal->lrat || id2); reasons1.push_back (id1); reasons2.push_back (id2); unsimplified.clear (); @@ -6620,8 +6628,8 @@ void Closure::add_ite_matching_proof_chain ( } id2 = simplify_and_add_to_proof_chain (unsimplified); - assert (!internal->lrat || id1); - assert (!internal->lrat || id2); + CADICAL_assert (!internal->lrat || id1); + CADICAL_assert (!internal->lrat || id2); reasons2.push_back (id1); reasons1.push_back (id2); unsimplified.clear (); @@ -6634,7 +6642,7 @@ void Closure::add_ite_matching_proof_chain ( unsimplified.push_back (-lhs2); LRAT_ID id1 = 0; if (internal->lrat) { - assert (g_neg_then_id && h_then_id && g_else_id && h_neg_else_id); + CADICAL_assert (g_neg_then_id && h_then_id && g_else_id && h_neg_else_id); lrat_chain.push_back (g_neg_then_id); lrat_chain.push_back (h_then_id); } @@ -6650,8 +6658,8 @@ void Closure::add_ite_matching_proof_chain ( lrat_chain.push_back (h_neg_else_id); } id2 = simplify_and_add_to_proof_chain (unsimplified); - assert (!internal->lrat || id1); - assert (!internal->lrat || id2); + CADICAL_assert (!internal->lrat || id1); + CADICAL_assert (!internal->lrat || id2); reasons2.push_back (id1); reasons1.push_back (id2); unsimplified.clear (); @@ -6664,7 +6672,7 @@ void Closure::add_ite_matching_proof_chain ( unsimplified.push_back (-lhs2); LRAT_ID id1 = 0; if (internal->lrat) { - assert (g_neg_then_id && h_then_id && g_else_id && h_neg_else_id); + CADICAL_assert (g_neg_then_id && h_then_id && g_else_id && h_neg_else_id); lrat_chain.push_back (h_neg_then_id); lrat_chain.push_back (g_then_id); } @@ -6680,8 +6688,8 @@ void Closure::add_ite_matching_proof_chain ( lrat_chain.push_back (g_neg_else_id); } id2 = simplify_and_add_to_proof_chain (unsimplified); - assert (!internal->lrat || id1); - assert (!internal->lrat || id2); + CADICAL_assert (!internal->lrat || id1); + CADICAL_assert (!internal->lrat || id2); reasons2.push_back (id1); reasons1.push_back (id2); unsimplified.clear (); @@ -6689,16 +6697,16 @@ void Closure::add_ite_matching_proof_chain ( } LOG ("normal path"); - assert (!internal->lrat || degenerated_g_then || + CADICAL_assert (!internal->lrat || degenerated_g_then || (g_then_id && g_neg_then_id)); - assert (!internal->lrat || degenerated_g_else || + CADICAL_assert (!internal->lrat || degenerated_g_else || (g_else_id && g_neg_else_id)); - assert (!internal->lrat || degenerated_h_then || + CADICAL_assert (!internal->lrat || degenerated_h_then || (h_then_id && h_neg_then_id)); - assert (!internal->lrat || degenerated_h_else || + CADICAL_assert (!internal->lrat || degenerated_h_else || (h_else_id && h_neg_else_id)); - assert (!internal->lrat || g_then_id || h_neg_then_id); - assert (!internal->lrat || g_neg_then_id || h_then_id); + CADICAL_assert (!internal->lrat || g_then_id || h_neg_then_id); + CADICAL_assert (!internal->lrat || g_neg_then_id || h_then_id); unsimplified.push_back (-lhs1); unsimplified.push_back (lhs2); unsimplified.push_back (-cond); @@ -6731,7 +6739,7 @@ void Closure::add_ite_matching_proof_chain ( unsimplified.push_back (lhs1); unsimplified.push_back (-lhs2); unsimplified.push_back (-cond); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); LRAT_ID id3 = 0; if (degenerated_g_then || degenerated_h_then) { @@ -6747,7 +6755,7 @@ void Closure::add_ite_matching_proof_chain ( } unsimplified.pop_back (); unsimplified.push_back (cond); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); LRAT_ID id4 = 0; if (degenerated_g_else || degenerated_h_else) { @@ -6762,7 +6770,7 @@ void Closure::add_ite_matching_proof_chain ( unsimplified.pop_back (); if (internal->lrat) { - assert (!internal->lrat || (id1 && id2 && id3 && id4)); + CADICAL_assert (!internal->lrat || (id1 && id2 && id3 && id4)); reasons1.push_back (id1), reasons1.push_back (id2); reasons2.push_back (id3), reasons2.push_back (id4); } @@ -6774,7 +6782,7 @@ void Closure::add_ite_matching_proof_chain ( Gate *Closure::new_ite_gate (int lhs, int cond, int then_lit, int else_lit, std::vector &&clauses) { - assert (chain.empty ()); + CADICAL_assert (chain.empty ()); if (else_lit == -then_lit) { if (then_lit < 0) LOG ("skipping ternary XOR %d := %d ^ %d", lhs, cond, -then_lit); @@ -6857,15 +6865,15 @@ Gate *Closure::new_ite_gate (int lhs, int cond, int then_lit, int else_lit, } void check_ite_lits_normalized (const std::vector &lits) { - assert (lits[0] > 0); - assert (lits[1] > 0); - assert (lits[0] != lits[1]); - assert (lits[0] != lits[2]); - assert (lits[1] != lits[2]); - assert (lits[0] != -lits[1]); - assert (lits[0] != -lits[2]); - assert (lits[1] != -lits[2]); -#ifdef NDEBUG + CADICAL_assert (lits[0] > 0); + CADICAL_assert (lits[1] > 0); + CADICAL_assert (lits[0] != lits[1]); + CADICAL_assert (lits[0] != lits[2]); + CADICAL_assert (lits[1] != lits[2]); + CADICAL_assert (lits[0] != -lits[1]); + CADICAL_assert (lits[0] != -lits[2]); + CADICAL_assert (lits[1] != -lits[2]); +#ifdef CADICAL_NDEBUG (void) lits; #endif } @@ -6883,46 +6891,46 @@ void Closure::check_ite_implied (int lhs, int cond, int then_lit, void Closure::check_contained_module_rewriting (Clause *c, int lit, bool normalized, int except) { -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG if (lit == except) // happens for degenerated cases except = 0; const int normalize_lit = (lit == except ? except : find_eager_representative (lit)); - assert (!normalized || lit == -except || normalize_lit == lit); + CADICAL_assert (!normalized || lit == -except || normalize_lit == lit); bool found = false; LOG (c, "looking for (normalized) %d in ", lit); for (auto other : *c) { const int normalize_other = (other == except ? except : find_eager_representative (other)); LOG ("%d -> %d ", other, normalize_other); - assert (!normalized || other == -except || normalize_other == other); + CADICAL_assert (!normalized || other == -except || normalize_other == other); if (normalize_other == normalize_lit) { found = true; break; } } - assert (found); + CADICAL_assert (found); #else (void) c, (void) lit, (void) normalized, (void) except; #endif } void Closure::check_ite_lrat_reasons (Gate *g, bool normalized) { -#ifndef NDEBUG - assert (g->tag == Gate_Type::ITE_Gate); +#ifndef CADICAL_NDEBUG + CADICAL_assert (g->tag == Gate_Type::ITE_Gate); if (!internal->lrat) return; - assert (g->rhs.size () == 3); - assert (is_tautological_ite_gate (g) || g->pos_lhs_ids.size () == 4); - assert (g->neg_lhs_ids.empty ()); - assert (g->pos_lhs_ids.size () == 4); + CADICAL_assert (g->rhs.size () == 3); + CADICAL_assert (is_tautological_ite_gate (g) || g->pos_lhs_ids.size () == 4); + CADICAL_assert (g->neg_lhs_ids.empty ()); + CADICAL_assert (g->pos_lhs_ids.size () == 4); #else (void) g, (void) normalized; #endif } void Closure::check_ite_gate_implied (Gate *g) { - assert (g->tag == Gate_Type::ITE_Gate); + CADICAL_assert (g->tag == Gate_Type::ITE_Gate); if (internal->lrat) return; check_ite_implied (g->lhs, g->rhs[0], g->rhs[1], g->rhs[2]); @@ -6941,7 +6949,7 @@ void Closure::init_ite_gate_extraction ( continue; unsigned size = 0; - assert (!c->garbage); + CADICAL_assert (!c->garbage); for (auto lit : *c) { const signed char v = internal->val (lit); if (v < 0) @@ -6957,7 +6965,7 @@ void Closure::init_ite_gate_extraction ( } if (size < 3) continue; - assert (size == 3); + CADICAL_assert (size == 3); ternary.push_back (c); LOG (c, "counting original ITE gate base"); for (auto lit : *c) { @@ -6968,8 +6976,8 @@ void Closure::init_ite_gate_extraction ( } for (auto c : ternary) { - assert (!c->garbage); - assert (!c->redundant); + CADICAL_assert (!c->garbage); + CADICAL_assert (!c->redundant); unsigned positive = 0, negative = 0, twice = 0; for (auto lit : *c) { if (internal->val (lit)) @@ -6978,7 +6986,7 @@ void Closure::init_ite_gate_extraction ( if (!count_not_lit) goto CONTINUE_WITH_NEXT_TERNARY_CLAUSE; const unsigned count_lit = largecount (lit); - assert (count_lit); + CADICAL_assert (count_lit); if (count_lit > 1 && count_not_lit > 1) ++twice; if (lit < 0) @@ -6988,7 +6996,7 @@ void Closure::init_ite_gate_extraction ( } if (twice < 2) goto CONTINUE_WITH_NEXT_TERNARY_CLAUSE; - assert (c->size != 2); + CADICAL_assert (c->size != 2); for (auto lit : *c) internal->occs (lit).push_back (c); if (positive && negative) @@ -7010,9 +7018,9 @@ void Closure::reset_ite_gate_extraction () { void Closure::copy_conditional_equivalences (int lit, lit_implications &condbin) { - assert (condbin.empty ()); + CADICAL_assert (condbin.empty ()); for (auto c : internal->occs (lit)) { - assert (c->size != 2); + CADICAL_assert (c->size != 2); int first = 0, second = 0; for (auto other : *c) { if (internal->val (other)) @@ -7022,21 +7030,21 @@ void Closure::copy_conditional_equivalences (int lit, if (!first) first = other; else { - assert (!second); + CADICAL_assert (!second); second = other; } } - assert (first), assert (second); + CADICAL_assert (first), CADICAL_assert (second); lit_implication p (first, second, c); if (internal->vlit (first) < internal->vlit (second)) { - assert (p.first == first); - assert (p.second == second); + CADICAL_assert (p.first == first); + CADICAL_assert (p.second == second); } else { - assert (internal->vlit (second) < internal->vlit (first)); + CADICAL_assert (internal->vlit (second) < internal->vlit (first)); p.swap (); - assert (p.first == second); - assert (p.second == first); + CADICAL_assert (p.first == second); + CADICAL_assert (p.second == first); } LOG (c, "literal %d condition binary clause %d %d", lit, first, second); condbin.push_back (p); @@ -7088,7 +7096,7 @@ Closure::find_lit_implication_second_literal ( [] (const lit_implication &a, const lit_implication &b) { return a.second < b.second; }); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG auto found2 = std::binary_search ( begin, end, lit_implication{lit, lit}, [] (const lit_implication &a, const lit_implication &b) { @@ -7097,7 +7105,7 @@ Closure::find_lit_implication_second_literal ( #endif if (found < end && found->second == lit) { - assert (found2 == (found < end)); + CADICAL_assert (found2 == (found < end)); return found; } return end; @@ -7110,24 +7118,24 @@ void Closure::search_condeq (int lit, int pos_lit, lit_implications::const_iterator neg_begin, lit_implications::const_iterator neg_end, lit_equivalences &condeq) { - assert (neg_lit == -pos_lit); - assert (pos_begin < pos_end); - assert (neg_begin < neg_end); - assert (pos_begin->first == pos_lit); - assert (neg_begin->first == neg_lit); - assert (pos_end <= neg_begin || neg_end <= pos_begin); + CADICAL_assert (neg_lit == -pos_lit); + CADICAL_assert (pos_begin < pos_end); + CADICAL_assert (neg_begin < neg_end); + CADICAL_assert (pos_begin->first == pos_lit); + CADICAL_assert (neg_begin->first == neg_lit); + CADICAL_assert (pos_end <= neg_begin || neg_end <= pos_begin); for (lit_implications::const_iterator p = pos_begin; p != pos_end; p++) { const int other = p->second; const int not_other = -other; const lit_implications::const_iterator other_clause = find_lit_implication_second_literal (not_other, neg_begin, neg_end); - assert (std::find (begin (*p->clause), end (*p->clause), lit) != + CADICAL_assert (std::find (begin (*p->clause), end (*p->clause), lit) != end (*p->clause)); if (other_clause != neg_end) { - assert (std::find (begin (*other_clause->clause), + CADICAL_assert (std::find (begin (*other_clause->clause), end (*other_clause->clause), neg_lit) != end (*other_clause->clause)); - assert (std::find (begin (*p->clause), end (*p->clause), other) != + CADICAL_assert (std::find (begin (*p->clause), end (*p->clause), other) != end (*p->clause)); lit_equivalence equivalence (neg_lit, other_clause->clause, other, p->clause); @@ -7138,8 +7146,8 @@ void Closure::search_condeq (int lit, int pos_lit, equivalence.check_invariant (); LOG ("found conditional %d equivalence %d = %d", lit, equivalence.first, equivalence.second); - assert (equivalence.first > 0); - assert (internal->vlit (equivalence.first) < + CADICAL_assert (equivalence.first > 0); + CADICAL_assert (internal->vlit (equivalence.first) < internal->vlit (equivalence.second)); check_ternary (lit, neg_lit, -other); check_ternary (lit, -neg_lit, other); @@ -7187,9 +7195,9 @@ void Closure::extract_condeq_pairs (int lit, lit_implications &condbin, } for (;;) { - assert (pos_begin != end); - assert (next_lit == pos_begin->first); - assert (next_lit > 0); + CADICAL_assert (pos_begin != end); + CADICAL_assert (next_lit == pos_begin->first); + CADICAL_assert (next_lit > 0); const int pos_lit = next_lit; lit_implications::const_iterator pos_end = pos_begin + 1; LOG ("searching for first other literal after finding lit %d", @@ -7202,8 +7210,8 @@ void Closure::extract_condeq_pairs (int lit, lit_implications &condbin, break; pos_end++; } - assert (pos_end != end); - assert (next_lit == pos_end->first); + CADICAL_assert (pos_end != end); + CADICAL_assert (next_lit == pos_end->first); const int neg_lit = -pos_lit; if (next_lit != neg_lit) { if (next_lit < 0) { @@ -7254,7 +7262,7 @@ void Closure::extract_condeq_pairs (int lit, lit_implications &condbin, } if (neg_end == end) return; - assert (next_lit == neg_end->first); + CADICAL_assert (next_lit == neg_end->first); if (next_lit < 0) { pos_begin = neg_end + 1; for (;;) { @@ -7273,9 +7281,9 @@ void Closure::extract_condeq_pairs (int lit, lit_implications &condbin, void Closure::find_conditional_equivalences (int lit, lit_implications &condbin, lit_equivalences &condeq) { - assert (condbin.empty ()); - assert (condeq.empty ()); - assert (internal->occs (lit).size () > 1); + CADICAL_assert (condbin.empty ()); + CADICAL_assert (condeq.empty ()); + CADICAL_assert (internal->occs (lit).size () > 1); copy_conditional_equivalences (lit, condbin); MSORT (internal->opts.radixsortlim, begin (condbin), end (condbin), litpair_rank (this->internal), litpair_smaller (this->internal)); @@ -7303,7 +7311,7 @@ void Closure::merge_condeq (int cond, lit_equivalences &condeq, const int then_lit = p.second; if (internal->lrat) p.check_invariant (); - assert (lhs > 0); + CADICAL_assert (lhs > 0); while (q != end_not_condeq && q->first < lhs) ++q; while (q != end_not_condeq && q->first == lhs) { @@ -7315,8 +7323,8 @@ void Closure::merge_condeq (int cond, lit_equivalences &condeq, if (internal->lrat) { // The then/else literal is the second of the pair, hence the swap // of the reasons - assert (p.first_clause && p.second_clause); - assert (not_cond_pair.first_clause && not_cond_pair.second_clause); + CADICAL_assert (p.first_clause && p.second_clause); + CADICAL_assert (not_cond_pair.first_clause && not_cond_pair.second_clause); LOG (p.second_clause, "pairing %d", then_lit); LOG (p.first_clause, "pairing %d", -then_lit); LOG (not_cond_pair.second_clause, "pairing %d", else_lit); @@ -7374,7 +7382,7 @@ void Closure::extract_ite_gates_of_variable (int idx) { } void Closure::extract_ite_gates () { - assert (!full_watching); + CADICAL_assert (!full_watching); if (!internal->opts.congruenceite) return; START (extractites); @@ -7398,15 +7406,15 @@ void Closure::extract_ite_gates () { void Closure::extract_gates () { START (extract); extract_and_gates (); - assert (internal->unsat || lrat_chain.empty ()); - assert (internal->unsat || chain.empty ()); + CADICAL_assert (internal->unsat || lrat_chain.empty ()); + CADICAL_assert (internal->unsat || chain.empty ()); if (internal->unsat || internal->terminated_asynchronously ()) { STOP (extract); return; } extract_xor_gates (); - assert (internal->unsat || lrat_chain.empty ()); - assert (internal->unsat || chain.empty ()); + CADICAL_assert (internal->unsat || lrat_chain.empty ()); + CADICAL_assert (internal->unsat || chain.empty ()); if (internal->unsat || internal->terminated_asynchronously ()) { STOP (extract); @@ -7459,30 +7467,30 @@ bool Internal::extract_gates () { Closure closure (this); closure.init_closure (); - assert (unsat || closure.chain.empty ()); - assert (unsat || lrat_chain.empty ()); + CADICAL_assert (unsat || closure.chain.empty ()); + CADICAL_assert (unsat || lrat_chain.empty ()); closure.extract_binaries (); - assert (unsat || closure.chain.empty ()); - assert (unsat || lrat_chain.empty ()); + CADICAL_assert (unsat || closure.chain.empty ()); + CADICAL_assert (unsat || lrat_chain.empty ()); closure.extract_gates (); - assert (unsat || closure.chain.empty ()); - assert (unsat || lrat_chain.empty ()); + CADICAL_assert (unsat || closure.chain.empty ()); + CADICAL_assert (unsat || lrat_chain.empty ()); internal->clear_watches (); internal->connect_watches (); closure.reset_extraction (); if (!unsat) { closure.find_units (); - assert (unsat || closure.chain.empty ()); - assert (unsat || lrat_chain.empty ()); + CADICAL_assert (unsat || closure.chain.empty ()); + CADICAL_assert (unsat || lrat_chain.empty ()); if (!internal->unsat) { closure.find_equivalences (); - assert (unsat || closure.chain.empty ()); - assert (unsat || lrat_chain.empty ()); + CADICAL_assert (unsat || closure.chain.empty ()); + CADICAL_assert (unsat || lrat_chain.empty ()); if (!unsat) { const int propagated = closure.propagate_units_and_equivalences (); - assert (unsat || closure.chain.empty ()); + CADICAL_assert (unsat || closure.chain.empty ()); if (!unsat && propagated) closure.forward_subsume_matching_clauses (); } @@ -7496,29 +7504,31 @@ bool Internal::extract_gates () { propagated2 = propagated = 0; propagate (); } - assert (closure.new_unwatched_binary_clauses.empty ()); + CADICAL_assert (closure.new_unwatched_binary_clauses.empty ()); internal->reset_occs (); internal->reset_noccs (); - assert (!internal->occurring ()); - assert (lrat_chain.empty ()); + CADICAL_assert (!internal->occurring ()); + CADICAL_assert (lrat_chain.empty ()); const int64_t new_merged = stats.congruence.congruent; +#ifndef CADICAL_QUIET phase ("congruence-phase", stats.congruence.rounds, "merged %ld literals", new_merged - old_merged); +#endif if (!unsat && !internal->propagate ()) unsat = true; STOP_SIMPLIFIER (congruence, CONGRUENCE); report ('c', !opts.reportall && !(stats.congruence.congruent - old)); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG size_t watched = 0; for (auto v : vars) { for (auto sgn = -1; sgn <= 1; sgn += 2) { const int lit = v * sgn; for (auto w : watches (lit)) { if (w.binary ()) - assert (!w.clause->garbage); + CADICAL_assert (!w.clause->garbage); if (w.clause->garbage) continue; ++watched; @@ -7534,9 +7544,9 @@ bool Internal::extract_gates () { LOG (c, "watched"); ++nb_clauses; } - assert (watched == nb_clauses * 2); + CADICAL_assert (watched == nb_clauses * 2); #endif - assert (!internal->occurring ()); + CADICAL_assert (!internal->occurring ()); if (new_merged == old_merged) { congruence_delay.bumpreasons.interval++; @@ -7553,3 +7563,5 @@ bool Internal::extract_gates () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/constrain.cpp b/src/sat/cadical/cadical_constrain.cpp similarity index 96% rename from src/sat/cadical/constrain.cpp rename to src/sat/cadical/cadical_constrain.cpp index e7f7b7ef9..47f14e3d9 100644 --- a/src/sat/cadical/constrain.cpp +++ b/src/sat/cadical/cadical_constrain.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void Internal::constrain (int lit) { @@ -62,3 +66,5 @@ void Internal::reset_constraint () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/contract.cpp b/src/sat/cadical/cadical_contract.cpp similarity index 86% rename from src/sat/cadical/contract.cpp rename to src/sat/cadical/cadical_contract.cpp index 8547d0306..7485ff4aa 100644 --- a/src/sat/cadical/contract.cpp +++ b/src/sat/cadical/cadical_contract.cpp @@ -1,7 +1,11 @@ -#ifndef NCONTRACTS +#include "global.h" + +#ifndef CADICAL_NCONTRACTS #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void fatal_message_start (); @@ -24,4 +28,6 @@ void require_solver_pointer_to_be_non_zero (const void *ptr, } // namespace CaDiCaL +ABC_NAMESPACE_IMPL_END + #endif diff --git a/src/sat/cadical/cover.cpp b/src/sat/cadical/cadical_cover.cpp similarity index 93% rename from src/sat/cadical/cover.cpp rename to src/sat/cadical/cadical_cover.cpp index bd8781630..0d3253e98 100644 --- a/src/sat/cadical/cover.cpp +++ b/src/sat/cadical/cadical_cover.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -52,10 +56,10 @@ inline void Internal::cover_push_extension (int lit, Coveror &coveror) { bool found = false; for (const auto &other : coveror.covered) if (lit == other) - assert (!found), found = true; + CADICAL_assert (!found), found = true; else coveror.extend.push_back (other); - assert (found); + CADICAL_assert (found); (void) found; } @@ -63,11 +67,11 @@ inline void Internal::cover_push_extension (int lit, Coveror &coveror) { inline void Internal::covered_literal_addition (int lit, Coveror &coveror) { require_mode (COVER); - assert (level == 1); + CADICAL_assert (level == 1); cover_push_extension (lit, coveror); for (const auto &other : coveror.intersection) { LOG ("covered literal addition %d", other); - assert (!vals[other]), assert (!vals[-other]); + CADICAL_assert (!vals[other]), CADICAL_assert (!vals[-other]); set_val (other, -1); coveror.covered.push_back (other); coveror.added.push_back (other); @@ -81,9 +85,9 @@ inline void Internal::covered_literal_addition (int lit, Coveror &coveror) { inline void Internal::asymmetric_literal_addition (int lit, Coveror &coveror) { require_mode (COVER); - assert (level == 1); + CADICAL_assert (level == 1); LOG ("initial asymmetric literal addition %d", lit); - assert (!vals[lit]), assert (!vals[-lit]); + CADICAL_assert (!vals[lit]), CADICAL_assert (!vals[-lit]); set_val (lit, -1); coveror.added.push_back (lit); coveror.alas++; @@ -101,7 +105,7 @@ bool Internal::cover_propagate_asymmetric (int lit, Clause *ignore, Coveror &coveror) { require_mode (COVER); stats.propagations.cover++; - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); bool subsumed = false; LOG ("asymmetric literal propagation of %d", lit); Watches &ws = watches (lit); @@ -141,12 +145,12 @@ bool Internal::cover_propagate_asymmetric (int lit, Clause *ignore, k++; if (v < 0) { k = lits + 2; - assert (w.clause->pos <= size); + CADICAL_assert (w.clause->pos <= size); while (k != middle && (v = val (r = *k)) < 0) k++; } w.clause->pos = k - lits; - assert (lits + 2 <= k), assert (k <= w.clause->end ()); + CADICAL_assert (lits + 2 <= k), CADICAL_assert (k <= w.clause->end ()); if (v > 0) j[-1].blit = r; else if (!v) { @@ -156,10 +160,10 @@ bool Internal::cover_propagate_asymmetric (int lit, Clause *ignore, watch_literal (r, lit, w.clause); j--; } else if (!u) { - assert (v < 0); + CADICAL_assert (v < 0); asymmetric_literal_addition (-other, coveror); } else { - assert (u < 0), assert (v < 0); + CADICAL_assert (u < 0), CADICAL_assert (v < 0); LOG (w.clause, "found subsuming"); subsumed = true; break; @@ -181,7 +185,7 @@ bool Internal::cover_propagate_asymmetric (int lit, Clause *ignore, bool Internal::cover_propagate_covered (int lit, Coveror &coveror) { require_mode (COVER); - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); if (frozen (lit)) { LOG ("no covered propagation on frozen literal %d", lit); return false; @@ -190,7 +194,7 @@ bool Internal::cover_propagate_covered (int lit, Coveror &coveror) { stats.propagations.cover++; LOG ("covered propagation of %d", lit); - assert (coveror.intersection.empty ()); + CADICAL_assert (coveror.intersection.empty ()); Occs &os = occs (-lit); const auto end = os.end (); @@ -237,7 +241,7 @@ bool Internal::cover_propagate_covered (int lit, Coveror &coveror) { const signed char tmp = val (other); if (tmp < 0) continue; - assert (!tmp); + CADICAL_assert (!tmp); coveror.intersection.push_back (other); mark (other); } @@ -254,7 +258,7 @@ bool Internal::cover_propagate_covered (int lit, Coveror &coveror) { signed char tmp = val (other); if (tmp < 0) continue; - assert (!tmp); + CADICAL_assert (!tmp); tmp = marked (other); if (tmp > 0) unmark (other); @@ -267,7 +271,7 @@ bool Internal::cover_propagate_covered (int lit, Coveror &coveror) { for (auto k = j; k != end; k++) { const int other = *j++ = *k; const int tmp = marked (other); - assert (tmp >= 0); + CADICAL_assert (tmp >= 0); if (tmp) j--, unmark (other); // remove marked and unmark it else @@ -298,7 +302,7 @@ bool Internal::cover_propagate_covered (int lit, Coveror &coveror) { bool res = false; if (first) { LOG ("all resolution candidates with %d blocked", -lit); - assert (coveror.intersection.empty ()); + CADICAL_assert (coveror.intersection.empty ()); cover_push_extension (lit, coveror); res = true; } else if (coveror.intersection.empty ()) { @@ -323,7 +327,7 @@ bool Internal::cover_propagate_covered (int lit, Coveror &coveror) { bool Internal::cover_clause (Clause *c, Coveror &coveror) { require_mode (COVER); - assert (!c->garbage); + CADICAL_assert (!c->garbage); LOG (c, "trying covered clauses elimination on"); bool satisfied = false; @@ -337,11 +341,11 @@ bool Internal::cover_clause (Clause *c, Coveror &coveror) { return false; } - assert (coveror.added.empty ()); - assert (coveror.extend.empty ()); - assert (coveror.covered.empty ()); + CADICAL_assert (coveror.added.empty ()); + CADICAL_assert (coveror.extend.empty ()); + CADICAL_assert (coveror.covered.empty ()); - assert (!level); + CADICAL_assert (!level); level = 1; LOG ("assuming literals of candidate clause"); for (const auto &lit : *c) { @@ -379,7 +383,7 @@ bool Internal::cover_clause (Clause *c, Coveror &coveror) { bool already_pushed = false; int64_t last_id = 0; LOG (c, "covered tautological"); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); LOG (coveror.extend, "extension = "); for (const auto &other : coveror.extend) { if (!prev) { @@ -450,7 +454,7 @@ bool Internal::cover_clause (Clause *c, Coveror &coveror) { // Backtrack and 'unassign' all literals. - assert (level == 1); + CADICAL_assert (level == 1); for (const auto &lit : coveror.added) set_val (lit, 0); level = 0; @@ -505,12 +509,12 @@ int64_t Internal::cover_round () { // First connect all clauses and find all not yet tried clauses. // -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t untried = 0; #endif // for (auto c : clauses) { - assert (!c->frozen); + CADICAL_assert (!c->frozen); if (c->garbage) continue; if (c->redundant) @@ -539,7 +543,7 @@ int64_t Internal::cover_round () { if (c->covered) continue; schedule.push_back (c); -#ifndef QUIET +#ifndef CADICAL_QUIET untried++; #endif } @@ -561,7 +565,7 @@ int64_t Internal::cover_round () { continue; if (c->size > opts.covermaxclslim) continue; - assert (c->covered); + CADICAL_assert (c->covered); c->covered = false; schedule.push_back (c); } @@ -589,7 +593,7 @@ int64_t Internal::cover_round () { stable_sort (schedule.begin (), schedule.end (), clause_covered_or_smaller ()); -#ifndef QUIET +#ifndef CADICAL_QUIET const size_t scheduled = schedule.size (); PHASE ("cover", stats.cover.count, "scheduled %zd clauses %.0f%% with %" PRId64 " untried %.0f%%", @@ -621,7 +625,7 @@ int64_t Internal::cover_round () { covered++; } -#ifndef QUIET +#ifndef CADICAL_QUIET const size_t remain = schedule.size (); const size_t tried = scheduled - remain; PHASE ("cover", stats.cover.count, @@ -687,11 +691,11 @@ bool Internal::cover () { LOG ("propagating units before covered clause elimination " "results in empty clause"); learn_empty_clause (); - assert (unsat); + CADICAL_assert (unsat); } reset_watches (); } - assert (unsat || propagated == trail.size ()); + CADICAL_assert (unsat || propagated == trail.size ()); int64_t covered = cover_round (); @@ -702,3 +706,5 @@ bool Internal::cover () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/decide.cpp b/src/sat/cadical/cadical_decide.cpp similarity index 94% rename from src/sat/cadical/decide.cpp rename to src/sat/cadical/cadical_decide.cpp index ca56ccdd7..0874cffd7 100644 --- a/src/sat/cadical/decide.cpp +++ b/src/sat/cadical/cadical_decide.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // This function determines the next decision variable on the queue, without @@ -65,7 +69,7 @@ int Internal::decide_phase (int idx, bool target) { phase = phases.saved[idx]; // The following should not be necessary and in some version we had even - // a hard 'COVER' assertion here to check for this. Unfortunately it + // a hard 'COVER' CADICAL_assertion here to check for this. Unfortunately it // triggered for some users and we could not get to the root cause of // 'phase' still not being set here. The logic for phase and target // saving is pretty complex, particularly in combination with local @@ -100,7 +104,7 @@ bool Internal::satisfied () { return false; if (num_assigned < (size_t) max_var) return false; - assert (num_assigned == (size_t) max_var); + CADICAL_assert (num_assigned == (size_t) max_var); if (propagated < trail.size ()) return false; size_t assigned = num_assigned; @@ -120,12 +124,12 @@ bool Internal::better_decision (int lit, int other) { // that not all variables are assigned. int Internal::decide () { - assert (!satisfied ()); + CADICAL_assert (!satisfied ()); START (decide); int res = 0; if ((size_t) level < assumptions.size ()) { const int lit = assumptions[level]; - assert (assumed (lit)); + CADICAL_assert (assumed (lit)); const signed char tmp = val (lit); if (tmp < 0) { LOG ("assumption %d falsified", lit); @@ -147,7 +151,7 @@ int Internal::decide () { const size_t size_constraint = constraint.size (); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG unsigned sum = 0; for (auto lit : constraint) sum += lit; @@ -172,7 +176,7 @@ int Internal::decide () { break; } - assert (!tmp); + CADICAL_assert (!tmp); LOG ("constraint literal %d unassigned", lit); if (!unassigned_lit || better_decision (lit, unassigned_lit)) @@ -218,10 +222,10 @@ int Internal::decide () { } } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (auto lit : constraint) sum -= lit; - assert (!sum); // Checksum of literal should not change! + CADICAL_assert (!sum); // Checksum of literal should not change! #endif } else { @@ -250,3 +254,5 @@ int Internal::decide () { return res; } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/decompose.cpp b/src/sat/cadical/cadical_decompose.cpp similarity index 89% rename from src/sat/cadical/decompose.cpp rename to src/sat/cadical/cadical_decompose.cpp index 7e44ab742..9081321b4 100644 --- a/src/sat/cadical/decompose.cpp +++ b/src/sat/cadical/cadical_decompose.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void Internal::decompose_analyze_binary_chain (DFS *dfs, int from) { @@ -10,7 +14,7 @@ void Internal::decompose_analyze_binary_chain (DFS *dfs, int from) { Clause *reason = from_dfs.parent; if (!reason) return; - assert (reason->size == 2); + CADICAL_assert (reason->size == 2); mini_chain.push_back (reason->id); int other = reason->literals[0]; other = other == from ? -reason->literals[1] : -other; @@ -30,7 +34,7 @@ vector Internal::decompose_analyze_binary_clauses (DFS *dfs, Clause *reason = from_dfs.parent; while (reason) { result.push_back (reason); - assert (reason->size == 2); + CADICAL_assert (reason->size == 2); int other = reason->literals[0]; other = other == from ? -reason->literals[1] : -other; Flags &f = flags (other); @@ -48,8 +52,8 @@ vector Internal::decompose_analyze_binary_clauses (DFS *dfs, void Internal::decompose_conflicting_scc_lrat (DFS *dfs, vector &scc) { if (!lrat) return; - assert (lrat_chain.empty ()); - assert (mini_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (mini_chain.empty ()); for (auto &lit : scc) { Flags &f = flags (lit); if (f.seen) @@ -67,10 +71,10 @@ void Internal::decompose_conflicting_scc_lrat (DFS *dfs, vector &scc) { void Internal::build_lrat_for_clause ( const vector> &dfs_chains, bool invert) { - assert (lrat); + CADICAL_assert (lrat); LOG ("building chain for not subsumed clause"); - assert (lrat_chain.empty ()); - assert (sign_marked.empty ()); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (sign_marked.empty ()); // build chain for each replaced literal for (const auto lit : clause) { auto other = lit; @@ -82,7 +86,7 @@ void Internal::build_lrat_for_clause ( lrat_chain.push_back (id); continue; } - assert (mini_chain.empty ()); + CADICAL_assert (mini_chain.empty ()); for (auto p : dfs_chains[vlit (other)]) { if (marked_decomposed (other)) continue; @@ -116,7 +120,7 @@ void Internal::build_lrat_for_clause ( void Internal::clear_sign_marked_literals () { LOG ("clearing %zd marked literals", sign_marked.size ()); for (const auto &lit : sign_marked) { - // assert (marked_signed (lit)); violated on purpose in factor + // CADICAL_assert (marked_signed (lit)); violated on purpose in factor unmark_decomposed (lit); } sign_marked.clear (); @@ -136,7 +140,7 @@ bool Internal::decompose_round () { if (terminated_asynchronously ()) return false; - assert (!level); + CADICAL_assert (!level); START_SIMPLIFIER (decompose, DECOMP); @@ -158,7 +162,7 @@ bool Internal::decompose_round () { } int substituted = 0; -#ifndef QUIET +#ifndef CADICAL_QUIET int non_trivial_sccs = 0; int before = active (); #endif @@ -180,17 +184,17 @@ bool Internal::decompose_round () { if (dfs[vlit (root)].min == TRAVERSED) continue; // skip traversed LOG ("new dfs search starting at root %d", root); - assert (work.empty ()); - assert (scc.empty ()); + CADICAL_assert (work.empty ()); + CADICAL_assert (scc.empty ()); work.push_back (root); while (!unsat && !work.empty ()) { int parent = work.back (); DFS &parent_dfs = dfs[vlit (parent)]; if (parent_dfs.min == TRAVERSED) { // skip traversed - assert (reprs[vlit (parent)]); + CADICAL_assert (reprs[vlit (parent)]); work.pop_back (); } else { - assert (!reprs[vlit (parent)]); + CADICAL_assert (!reprs[vlit (parent)]); // Go over all implied literals, thus need to iterate over all // binary watched clauses with the negation of 'parent'. @@ -236,12 +240,12 @@ bool Internal::decompose_round () { // becomes unsatisfiable. if (lrat) { - assert (analyzed.empty ()); + CADICAL_assert (analyzed.empty ()); int other, first = 0; bool conflicting = false; size_t j = scc.size (); do { - assert (j > 0); + CADICAL_assert (j > 0); other = scc[--j]; if (!first || vlit (other) < vlit (first)) first = other; @@ -256,7 +260,7 @@ bool Internal::decompose_round () { analyzed.push_back (other); } while (other != parent); - assert (!conflicting || first > 0); + CADICAL_assert (!conflicting || first > 0); vector to_justify; if (conflicting) { LOG ("conflicting scc simulating up at %d", parent); @@ -287,13 +291,13 @@ bool Internal::decompose_round () { } int other, repr = parent; -#ifndef QUIET +#ifndef CADICAL_QUIET int size = 0; #endif - assert (!scc.empty ()); + CADICAL_assert (!scc.empty ()); size_t j = scc.size (); do { - assert (j > 0); + CADICAL_assert (j > 0); other = scc[--j]; if (other == -parent) { LOG ("both %d and %d in one SCC", parent, -parent); @@ -307,17 +311,17 @@ bool Internal::decompose_round () { mini_chain.clear (); } assign_unit (parent); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG bool ok = #endif propagate (); - assert (!ok); + CADICAL_assert (!ok); learn_empty_clause (); lrat_chain.clear (); } else { if (abs (other) < abs (repr)) repr = other; -#ifndef QUIET +#ifndef CADICAL_QUIET size++; #endif } @@ -325,11 +329,11 @@ bool Internal::decompose_round () { if (unsat) break; -#ifndef QUIET +#ifndef CADICAL_QUIET LOG ("SCC of representative %d of size %d", repr, size); #endif do { - assert (!scc.empty ()); + CADICAL_assert (!scc.empty ()); other = scc.back (); scc.pop_back (); dfs[vlit (other)].min = TRAVERSED; @@ -344,7 +348,7 @@ bool Internal::decompose_round () { LOG ("literal %d in SCC of %d", other, repr); if (!lrat) continue; - assert (mini_chain.empty ()); + CADICAL_assert (mini_chain.empty ()); Flags &f = flags (repr); f.seen = true; analyzed.push_back (repr); @@ -355,7 +359,7 @@ bool Internal::decompose_round () { clear_analyzed_literals (); } while (other != parent); -#ifndef QUIET +#ifndef CADICAL_QUIET if (size > 1) non_trivial_sccs++; #endif @@ -372,7 +376,7 @@ bool Internal::decompose_round () { } else { // pre-fix dfs_idx++; - assert (dfs_idx < TRAVERSED); + CADICAL_assert (dfs_idx < TRAVERSED); parent_dfs.idx = parent_dfs.min = dfs_idx; scc.push_back (parent); @@ -429,17 +433,17 @@ bool Internal::decompose_round () { int other = reprs[vlit (idx)]; if (other == idx) continue; - assert (!flags (other).eliminated ()); - assert (!flags (other).substituted ()); + CADICAL_assert (!flags (other).eliminated ()); + CADICAL_assert (!flags (other).substituted ()); LOG ("marking equivalence of %d and %d", idx, other); - assert (clause.empty ()); - assert (lrat_chain.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (lrat_chain.empty ()); clause.push_back (other); clause.push_back (-idx); if (lrat) { build_lrat_for_clause (dfs_chains); - assert (!lrat_chain.empty ()); + CADICAL_assert (!lrat_chain.empty ()); } const int64_t id1 = ++clause_id; @@ -454,13 +458,13 @@ bool Internal::decompose_round () { lrat_chain.clear (); clause.clear (); - assert (clause.empty ()); - assert (lrat_chain.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (lrat_chain.empty ()); clause.push_back (idx); clause.push_back (-other); if (lrat) { build_lrat_for_clause (dfs_chains); - assert (!lrat_chain.empty ()); + CADICAL_assert (!lrat_chain.empty ()); } const int64_t id2 = ++clause_id; if (proof) { @@ -480,7 +484,7 @@ bool Internal::decompose_round () { // should be substituted by their representative. size_t clauses_size = clauses.size (); -#ifndef QUIET +#ifndef CADICAL_QUIET size_t garbage = 0, replaced = 0; #endif for (size_t i = 0; substituted && !unsat && i < clauses_size; i++) { @@ -497,7 +501,7 @@ bool Internal::decompose_round () { if (j == size) continue; -#ifndef QUIET +#ifndef CADICAL_QUIET replaced++; #endif LOG (c, "first substituted literal %d in", substituted); @@ -507,9 +511,9 @@ bool Internal::decompose_round () { // literal occurs in both phases or is assigned to true the clause is // satisfied and can be marked as garbage. - assert (clause.empty ()); - assert (lrat_chain.empty ()); - assert (analyzed.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (analyzed.empty ()); bool satisfied = false; for (int k = 0; !satisfied && k < size; k++) { @@ -544,7 +548,7 @@ bool Internal::decompose_round () { if (other == lit) continue; int64_t id = decompose_ids[vlit (-lit)]; - assert (id); + CADICAL_assert (id); lrat_chain.push_back (id); continue; } else if (tmp > 0) @@ -562,7 +566,7 @@ bool Internal::decompose_round () { if (!lrat) continue; int64_t id = decompose_ids[vlit (-lit)]; - assert (id); + CADICAL_assert (id); lrat_chain.push_back (id); } } @@ -574,7 +578,7 @@ bool Internal::decompose_round () { if (satisfied) { LOG (c, "satisfied after substitution (postponed)"); postponed_garbage.push_back (c); -#ifndef QUIET +#ifndef CADICAL_QUIET garbage++; #endif } else if (!clause.size ()) { @@ -585,7 +589,7 @@ bool Internal::decompose_round () { assign_unit (clause[0]); mark_garbage (c); new_unit = true; -#ifndef QUIET +#ifndef CADICAL_QUIET garbage++; #endif } else if (c->literals[0] != clause[0] || c->literals[1] != clause[1]) { @@ -594,16 +598,16 @@ bool Internal::decompose_round () { new_binary_clause = true; size_t d_clause_idx = clauses.size (); Clause *d = new_clause_as (c); - assert (clauses[d_clause_idx] == d); + CADICAL_assert (clauses[d_clause_idx] == d); clauses[d_clause_idx] = c; clauses[i] = d; mark_garbage (c); -#ifndef QUIET +#ifndef CADICAL_QUIET garbage++; #endif } else { LOG ("simply shrinking clause since watches did not change"); - assert (c->size > 2); + CADICAL_assert (c->size > 2); if (!c->redundant) mark_removed (c); if (proof) { @@ -624,11 +628,11 @@ bool Internal::decompose_round () { (void) shrink_clause (c, l); } else if (likely_to_be_kept_clause (c)) mark_added (c); - // we have shrunken c->size to l so even though there is an assertion + // we have shrunken c->size to l so even though there is an CADICAL_assertion // for c->size > 2 at the beginning of this else block, the new size // can be 2 now. if (c->size == 2) { // cheaper to update only new binary clauses - assert (new_binary_clause); + CADICAL_assert (new_binary_clause); update_watch_size (watches (c->literals[0]), c->literals[1], c); update_watch_size (watches (c->literals[1]), c->literals[0], c); } @@ -637,7 +641,7 @@ bool Internal::decompose_round () { while (!clause.empty ()) { int lit = clause.back (); clause.pop_back (); - assert (marked (lit) > 0); + CADICAL_assert (marked (lit) > 0); unmark (lit); } lrat_chain.clear (); @@ -653,9 +657,9 @@ bool Internal::decompose_round () { if (!id1) continue; int other = reprs[vlit (idx)]; - assert (other != idx); - assert (!flags (other).eliminated ()); - assert (!flags (other).substituted ()); + CADICAL_assert (other != idx); + CADICAL_assert (!flags (other).eliminated ()); + CADICAL_assert (!flags (other).substituted ()); clause.push_back (other); clause.push_back (-idx); @@ -702,8 +706,8 @@ bool Internal::decompose_round () { int other = reprs[vlit (idx)]; if (other == idx) continue; - assert (!flags (other).eliminated ()); - assert (!flags (other).substituted ()); + CADICAL_assert (!flags (other).eliminated ()); + CADICAL_assert (!flags (other).substituted ()); if (!flags (other).fixed ()) mark_substituted (idx); } @@ -731,3 +735,5 @@ void Internal::decompose () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/deduplicate.cpp b/src/sat/cadical/cadical_deduplicate.cpp similarity index 94% rename from src/sat/cadical/deduplicate.cpp rename to src/sat/cadical/cadical_deduplicate.cpp index 08a5d01c6..49d26a571 100644 --- a/src/sat/cadical/deduplicate.cpp +++ b/src/sat/cadical/cadical_deduplicate.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // Equivalent literal substitution in 'decompose' and shrinking in 'subsume' @@ -29,8 +33,8 @@ void Internal::mark_duplicated_binary_clauses_as_garbage () { START_SIMPLIFIER (deduplicate, DEDUP); stats.deduplications++; - assert (!level); - assert (watching ()); + CADICAL_assert (!level); + CADICAL_assert (watching ()); vector stack; // To save marked literals and unmark them later. @@ -49,7 +53,7 @@ void Internal::mark_duplicated_binary_clauses_as_garbage () { const int lit = sign * idx; // Consider all literals. - assert (stack.empty ()); + CADICAL_assert (stack.empty ()); Watches &ws = watches (lit); // We are removing references to garbage clause. Thus no 'auto'. @@ -81,7 +85,7 @@ void Internal::mark_duplicated_binary_clauses_as_garbage () { if (!c->redundant) { watch_iterator k; for (k = ws.begin ();; k++) { - assert (k != i); + CADICAL_assert (k != i); if (!k->binary ()) continue; if (k->blit != other) @@ -109,12 +113,12 @@ void Internal::mark_duplicated_binary_clauses_as_garbage () { unit = lit; if (lrat) { // taken from fradical - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); lrat_chain.push_back (c->id); // We've forgotten where the other binary clause is, so go find // it again for (watch_iterator k = ws.begin ();; k++) { - assert (k != i); + CADICAL_assert (k != i); if (!k->binary ()) continue; if (k->blit != -other) @@ -168,3 +172,5 @@ void Internal::mark_duplicated_binary_clauses_as_garbage () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/definition.cpp b/src/sat/cadical/cadical_definition.cpp similarity index 77% rename from src/sat/cadical/definition.cpp rename to src/sat/cadical/cadical_definition.cpp index 1b704b2b9..7c8fe4fbb 100644 --- a/src/sat/cadical/definition.cpp +++ b/src/sat/cadical/cadical_definition.cpp @@ -1,10 +1,14 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { #define INVALID_LIT UINT_MAX -// functions below are passed to kitten +// functions below are passed to cadical_kitten // struct definition_extractor { Eliminator *eliminator; @@ -17,7 +21,7 @@ struct definition_extractor { extern "C" { -// used to extract definitions from kitten +// used to extract definitions from cadical_kitten // static void traverse_definition_core (void *state, unsigned id) { definition_extractor *extractor = (definition_extractor *) state; @@ -27,17 +31,17 @@ static void traverse_definition_core (void *state, unsigned id) { Eliminator *eliminator = extractor->eliminator; const size_t size_clauses0 = clauses0.size (); const size_t size_clauses1 = clauses1.size (); - assert (size_clauses0 <= UINT_MAX); + CADICAL_assert (size_clauses0 <= UINT_MAX); unsigned sign; - assert (id < size_clauses0 + size_clauses1); + CADICAL_assert (id < size_clauses0 + size_clauses1); if (id < size_clauses0) { clause = clauses0[id]; sign = 1; } else { unsigned tmp = id - size_clauses0; -#ifndef NDEBUG - assert (size_clauses1 <= UINT_MAX); - assert (tmp < size_clauses1); +#ifndef CADICAL_NDEBUG + CADICAL_assert (size_clauses1 <= UINT_MAX); + CADICAL_assert (tmp < size_clauses1); #endif clause = clauses1[tmp]; sign = 2; @@ -73,7 +77,7 @@ static void traverse_one_sided_core_lemma (void *state, bool learned, for (const unsigned *p = lits; p != end; p++) pc.literals.push_back (internal->citten2lit (*p)); // conversion proof_clauses.push_back (pc); - assert (proof); + CADICAL_assert (proof); proof->add_derived_clause (pc.id, true, pc.literals, pc.chain); } else { internal->assign_unit (unit); @@ -97,28 +101,28 @@ static void traverse_one_sided_core_lemma_with_lrat ( const vector &clauses0 = extractor->clauses[0]; const vector &clauses1 = extractor->clauses[1]; vector &proof_clauses = eliminator->proof_clauses; - if (!learned) { // remember clauses for mapping to kitten internal - assert (size); - assert (!chain_size); + if (!learned) { // remember clauses for mapping to cadical_kitten internal + CADICAL_assert (size); + CADICAL_assert (!chain_size); proof_clause pc; pc.cid = cid; pc.learned = false; const size_t size_clauses0 = clauses0.size (); - assert (size_clauses0 <= UINT_MAX); + CADICAL_assert (size_clauses0 <= UINT_MAX); if (id < size_clauses0) { pc.id = clauses0[id]->id; } else { unsigned tmp = id - size_clauses0; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG const size_t size_clauses1 = clauses1.size (); - assert (size_clauses1 <= UINT_MAX); - assert (tmp < size_clauses1); + CADICAL_assert (size_clauses1 <= UINT_MAX); + CADICAL_assert (tmp < size_clauses1); #endif pc.id = clauses1[tmp]->id; } proof_clauses.push_back (pc); } else { // actually add to proof - assert (chain_size); + CADICAL_assert (chain_size); if (size) { proof_clause pc; pc.id = ++(internal->clause_id); @@ -136,14 +140,14 @@ static void traverse_one_sided_core_lemma_with_lrat ( break; } } - assert (id); + CADICAL_assert (id); pc.chain.push_back (id); } proof_clauses.push_back (pc); - assert (proof); + CADICAL_assert (proof); proof->add_derived_clause (pc.id, true, pc.literals, pc.chain); } else { // learn unit finish proof - assert (internal->lrat_chain.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); for (const unsigned *p = chain + chain_size; p != chain; p--) { int64_t id = 0; for (const auto &cpc : proof_clauses) { @@ -152,11 +156,11 @@ static void traverse_one_sided_core_lemma_with_lrat ( break; } } - assert (id); + CADICAL_assert (id); internal->lrat_chain.push_back (id); } internal->assign_unit (unit); - assert (internal->lrat_chain.empty ()); + CADICAL_assert (internal->lrat_chain.empty ()); for (const auto &pc : proof_clauses) { if (pc.learned) proof->delete_clause (pc.id, true, pc.literals); @@ -171,8 +175,8 @@ static void traverse_one_sided_core_lemma_with_lrat ( // Code ported from kissat. Kitten (and kissat) use unsigned representation // for literals whereas CaDiCaL uses signed representation. Conversion is // necessary for communication using lit2citten and citten2lit. -// This code is called in elim and kitten is initialized beforehand. -// To avoid confusion all cadical interal definitions with kitten are called +// This code is called in elim and cadical_kitten is initialized beforehand. +// To avoid confusion all cadical interal definitions with cadical_kitten are called // citten. // void Internal::find_definition (Eliminator &eliminator, int lit) { @@ -184,9 +188,9 @@ void Internal::find_definition (Eliminator &eliminator, int lit) { return; if (!eliminator.gates.empty ()) return; - assert (!val (lit)); - assert (!level); - assert (citten); + CADICAL_assert (!val (lit)); + CADICAL_assert (!level); + CADICAL_assert (citten); const int not_lit = -lit; definition_extractor extractor; extractor.lit = lit; @@ -200,9 +204,9 @@ void Internal::find_definition (Eliminator &eliminator, int lit) { const unsigned except = sign ? lit2citten (not_lit) : lit2citten (lit); for (auto c : extractor.clauses[sign]) { // to avoid copying the literals of c in their unsigned - // representation we instead implement the translation in kitten + // representation we instead implement the translation in cadical_kitten if (!c->garbage) { - LOG (c, "adding to kitten"); + LOG (c, "adding to cadical_kitten"); citten_clause_with_id_and_exception (citten, exported, c->size, c->literals, except); } @@ -211,42 +215,42 @@ void Internal::find_definition (Eliminator &eliminator, int lit) { } stats.definitions_checked++; const size_t limit = opts.elimdefticks; - kitten_set_ticks_limit (citten, limit); - int status = kitten_solve (citten); + cadical_kitten_set_ticks_limit (citten, limit); + int status = cadical_kitten_solve (citten); if (!exported) goto ABORT; if (status == 20) { LOG ("sub-solver result UNSAT shows definition exists"); uint64_t learned; - unsigned reduced = kitten_compute_clausal_core (citten, &learned); + unsigned reduced = cadical_kitten_compute_clausal_core (citten, &learned); LOG ("1st sub-solver core of size %u original clauses out of %u", reduced, exported); for (int i = 2; i <= opts.elimdefcores; i++) { - kitten_shrink_to_clausal_core (citten); - kitten_shuffle_clauses (citten); - kitten_set_ticks_limit (citten, 10 * limit); - int tmp = kitten_solve (citten); - assert (!tmp || tmp == 20); + cadical_kitten_shrink_to_clausal_core (citten); + cadical_kitten_shuffle_clauses (citten); + cadical_kitten_set_ticks_limit (citten, 10 * limit); + int tmp = cadical_kitten_solve (citten); + CADICAL_assert (!tmp || tmp == 20); if (!tmp) { LOG ("aborting core extraction"); goto ABORT; } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG unsigned previous = reduced; #endif - reduced = kitten_compute_clausal_core (citten, &learned); + reduced = cadical_kitten_compute_clausal_core (citten, &learned); LOG ("%d sub-solver core of size %u original clauses out of %u", i, reduced, exported); - assert (reduced <= previous); -#if not defined(LOGGING) && defined(NDEBUG) + CADICAL_assert (reduced <= previous); +#if not defined(LOGGING) && defined(CADICAL_NDEBUG) (void) reduced; #endif } stats.definitions_extracted++; eliminator.gatetype = DEF; eliminator.definition_unit = 0; - kitten_traverse_core_ids (citten, &extractor, traverse_definition_core); - assert (eliminator.definition_unit); + cadical_kitten_traverse_core_ids (citten, &extractor, traverse_definition_core); + CADICAL_assert (eliminator.definition_unit); int unit = 0; if (eliminator.definition_unit == 2) { unit = not_lit; @@ -261,11 +265,11 @@ void Internal::find_definition (Eliminator &eliminator, int lit) { if (proof) { if (lrat) { extractor.unit = unit; - kitten_trace_core (citten, &extractor, + cadical_kitten_trace_core (citten, &extractor, traverse_one_sided_core_lemma_with_lrat); } else { extractor.unit = unit; - kitten_traverse_core_clauses (citten, &extractor, + cadical_kitten_traverse_core_clauses (citten, &extractor, traverse_one_sided_core_lemma); } } else @@ -276,8 +280,10 @@ void Internal::find_definition (Eliminator &eliminator, int lit) { ABORT: LOG ("sub-solver failed to show that definition exists"); } - stats.definition_ticks += kitten_current_ticks (citten); + stats.definition_ticks += cadical_kitten_current_ticks (citten); return; } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/drattracer.cpp b/src/sat/cadical/cadical_drattracer.cpp similarity index 87% rename from src/sat/cadical/drattracer.cpp rename to src/sat/cadical/cadical_drattracer.cpp index 057c2d141..eb73b4c95 100644 --- a/src/sat/cadical/drattracer.cpp +++ b/src/sat/cadical/cadical_drattracer.cpp @@ -1,12 +1,16 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ DratTracer::DratTracer (Internal *i, File *f, bool b) : internal (i), file (f), binary (b) -#ifndef QUIET +#ifndef CADICAL_QUIET , added (0), deleted (0) #endif @@ -28,17 +32,17 @@ DratTracer::~DratTracer () { /*------------------------------------------------------------------------*/ inline void DratTracer::put_binary_zero () { - assert (binary); - assert (file); + CADICAL_assert (binary); + CADICAL_assert (file); file->put ((unsigned char) 0); } inline void DratTracer::put_binary_lit (int lit) { - assert (binary); - assert (file); - assert (lit != INT_MIN); + CADICAL_assert (binary); + CADICAL_assert (file); + CADICAL_assert (lit != INT_MIN); unsigned idx = abs (lit); - assert (idx < (1u << 31)); + CADICAL_assert (idx < (1u << 31)); unsigned x = 2u * idx + (lit < 0); unsigned char ch; while (x & ~0x7f) { @@ -90,7 +94,7 @@ void DratTracer::add_derived_clause (int64_t, bool, return; LOG ("DRAT TRACER tracing addition of derived clause"); drat_add_clause (clause); -#ifndef QUIET +#ifndef CADICAL_QUIET added++; #endif } @@ -100,7 +104,7 @@ void DratTracer::delete_clause (int64_t, bool, const vector &clause) { return; LOG ("DRAT TRACER tracing deletion of clause"); drat_delete_clause (clause); -#ifndef QUIET +#ifndef CADICAL_QUIET deleted++; #endif } @@ -109,7 +113,7 @@ void DratTracer::delete_clause (int64_t, bool, const vector &clause) { bool DratTracer::closed () { return file->closed (); } -#ifndef QUIET +#ifndef CADICAL_QUIET void DratTracer::print_statistics () { uint64_t bytes = file->bytes (); @@ -125,9 +129,9 @@ void DratTracer::print_statistics () { #endif void DratTracer::close (bool print) { - assert (!closed ()); + CADICAL_assert (!closed ()); file->close (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { MSG ("DRAT proof file '%s' closed", file->name ()); print_statistics (); @@ -138,9 +142,9 @@ void DratTracer::close (bool print) { } void DratTracer::flush (bool print) { - assert (!closed ()); + CADICAL_assert (!closed ()); file->flush (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { MSG ("DRAT proof file '%s' flushed", file->name ()); print_statistics (); @@ -151,3 +155,5 @@ void DratTracer::flush (bool print) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/elim.cpp b/src/sat/cadical/cadical_elim.cpp similarity index 93% rename from src/sat/cadical/elim.cpp rename to src/sat/cadical/cadical_elim.cpp index 9a5157436..951dfee74 100644 --- a/src/sat/cadical/elim.cpp +++ b/src/sat/cadical/cadical_elim.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -19,7 +23,7 @@ namespace CaDiCaL { /*------------------------------------------------------------------------*/ inline double Internal::compute_elim_score (unsigned lit) { - assert (1 <= lit), assert (lit <= (unsigned) max_var); + CADICAL_assert (1 <= lit), CADICAL_assert (lit <= (unsigned) max_var); const unsigned uidx = 2 * lit; const double pos = internal->ntab[uidx]; const double neg = internal->ntab[uidx + 1]; @@ -65,7 +69,7 @@ bool Internal::ineliminating () { if (!preprocessing && !opts.inprocessing) return false; if (preprocessing) - assert (lim.preprocessing); + CADICAL_assert (lim.preprocessing); // Respect (increasing) conflict limit. // @@ -90,7 +94,7 @@ bool Internal::ineliminating () { void Internal::elim_update_added_clause (Eliminator &eliminator, Clause *c) { - assert (!c->redundant); + CADICAL_assert (!c->redundant); ElimSchedule &schedule = eliminator.schedule; for (const auto &lit : *c) { if (!active (lit)) @@ -111,7 +115,7 @@ void Internal::elim_update_removed_lit (Eliminator &eliminator, int lit) { if (frozen (lit)) return; int64_t &score = noccs (lit); - assert (score > 0); + CADICAL_assert (score > 0); score--; const int idx = abs (lit); ElimSchedule &schedule = eliminator.schedule; @@ -125,11 +129,11 @@ void Internal::elim_update_removed_lit (Eliminator &eliminator, int lit) { void Internal::elim_update_removed_clause (Eliminator &eliminator, Clause *c, int except) { - assert (!c->redundant); + CADICAL_assert (!c->redundant); for (const auto &lit : *c) { if (lit == except) continue; - assert (lit != -except); + CADICAL_assert (lit != -except); elim_update_removed_lit (eliminator, lit); } } @@ -141,14 +145,14 @@ void Internal::elim_update_removed_clause (Eliminator &eliminator, // and also marks clauses satisfied by those units as garbage immediately. void Internal::elim_propagate (Eliminator &eliminator, int root) { - assert (val (root) > 0); + CADICAL_assert (val (root) > 0); vector work; size_t i = 0; work.push_back (root); while (i < work.size ()) { int lit = work[i++]; LOG ("elimination propagation of %d", lit); - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); const Occs &ns = occs (-lit); for (const auto &c : ns) { if (c->garbage) @@ -212,12 +216,12 @@ void Internal::elim_on_the_fly_self_subsumption (Eliminator &eliminator, LOG (c, "pivot %d on-the-fly self-subsuming resolution", pivot); stats.elimotfstr++; stats.strengthened++; - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); for (const auto &lit : *c) { if (lit == pivot) continue; const signed char tmp = val (lit); - assert (tmp <= 0); + CADICAL_assert (tmp <= 0); if (tmp < 0) continue; clause.push_back (lit); @@ -266,8 +270,8 @@ bool Internal::resolve_clauses (Eliminator &eliminator, Clause *c, int pivot, Clause *d, const bool propagate_eagerly) { - assert (!c->redundant); - assert (!d->redundant); + CADICAL_assert (!c->redundant); + CADICAL_assert (!d->redundant); stats.elimres++; @@ -278,8 +282,8 @@ bool Internal::resolve_clauses (Eliminator &eliminator, Clause *c, swap (c, d); } - assert (!level); - assert (clause.empty ()); + CADICAL_assert (!level); + CADICAL_assert (clause.empty ()); int satisfied = 0; // Contains this satisfying literal. int tautological = 0; // Clashing literal if tautological. @@ -295,7 +299,7 @@ bool Internal::resolve_clauses (Eliminator &eliminator, Clause *c, s++; continue; } - assert (lit != -pivot); + CADICAL_assert (lit != -pivot); const signed char tmp = val (lit); if (tmp > 0) { satisfied = lit; @@ -334,7 +338,7 @@ bool Internal::resolve_clauses (Eliminator &eliminator, Clause *c, t++; continue; } - assert (lit != pivot); + CADICAL_assert (lit != pivot); signed char tmp = val (lit); if (tmp > 0) { satisfied = lit; @@ -356,7 +360,7 @@ bool Internal::resolve_clauses (Eliminator &eliminator, Clause *c, } else if (!tmp) clause.push_back (lit), t++; else - assert (tmp > 0), t++; + CADICAL_assert (tmp > 0), t++; } clear_analyzed_literals (); @@ -405,15 +409,15 @@ bool Internal::resolve_clauses (Eliminator &eliminator, Clause *c, } LOG (clause, "resolvent"); - assert (!lrat || !lrat_chain.empty ()); + CADICAL_assert (!lrat || !lrat_chain.empty ()); // Double self-subsuming resolution. The clauses 'c' and 'd' are // identical except for the pivot which occurs in different phase. The // resolvent subsumes both antecedents. if (s > size && t > size) { - assert (s == size + 1); - assert (t == size + 1); + CADICAL_assert (s == size + 1); + CADICAL_assert (t == size + 1); clause.clear (); // LRAT is c + d (+ eventual units) elim_on_the_fly_self_subsumption (eliminator, c, pivot); @@ -430,7 +434,7 @@ bool Internal::resolve_clauses (Eliminator &eliminator, Clause *c, // with 'pivot' removed and then marking 'c' as garbage. if (s > size) { - assert (s == size + 1); + CADICAL_assert (s == size + 1); clause.clear (); // LRAT is c + d (+ eventual units) elim_on_the_fly_self_subsumption (eliminator, c, pivot); @@ -440,7 +444,7 @@ bool Internal::resolve_clauses (Eliminator &eliminator, Clause *c, // Same single self-subsuming resolution situation, but only for 'd'. if (t > size) { - assert (t == size + 1); + CADICAL_assert (t == size + 1); clause.clear (); // LRAT is c + d (+ eventual units) -> same. elim_on_the_fly_self_subsumption (eliminator, d, -pivot); @@ -469,8 +473,8 @@ bool Internal::elim_resolvents_are_bounded (Eliminator &eliminator, stats.elimtried++; - assert (!unsat); - assert (active (pivot)); + CADICAL_assert (!unsat); + CADICAL_assert (active (pivot)); const Occs &ps = occs (pivot); const Occs &ns = occs (-pivot); @@ -492,11 +496,11 @@ bool Internal::elim_resolvents_are_bounded (Eliminator &eliminator, int64_t resolvents = 0; // Non-tautological resolvents. for (const auto &c : ps) { - assert (!c->redundant); + CADICAL_assert (!c->redundant); if (c->garbage) continue; for (const auto &d : ns) { - assert (!d->redundant); + CADICAL_assert (!d->redundant); if (d->garbage) continue; if (!resolve_gates && substitute && c->gate == d->gate) @@ -562,13 +566,13 @@ inline void Internal::elim_add_resolvents (Eliminator &eliminator, stats.eliminated_def++; break; default: - assert (eliminator.gatetype == NO); + CADICAL_assert (eliminator.gatetype == NO); } LOG ("adding all resolvents on %d", pivot); - assert (!val (pivot)); - assert (!flags (pivot).eliminated ()); + CADICAL_assert (!val (pivot)); + CADICAL_assert (!flags (pivot).eliminated ()); const Occs &ps = occs (pivot); const Occs &ns = occs (-pivot); @@ -589,7 +593,7 @@ inline void Internal::elim_add_resolvents (Eliminator &eliminator, continue; if (!resolve_clauses (eliminator, c, pivot, d, false)) continue; - assert (!lrat || !lrat_chain.empty ()); + CADICAL_assert (!lrat || !lrat_chain.empty ()); Clause *r = new_resolved_irredundant_clause (); elim_update_added_clause (eliminator, r); eliminator.enqueue (r); @@ -611,28 +615,28 @@ inline void Internal::elim_add_resolvents (Eliminator &eliminator, void Internal::mark_eliminated_clauses_as_garbage ( Eliminator &eliminator, int pivot, bool &deleted_binary_clause) { - assert (!unsat); + CADICAL_assert (!unsat); LOG ("marking irredundant clauses with %d as garbage", pivot); const int64_t substitute = eliminator.gates.size (); if (substitute) LOG ("pushing %" PRId64 " gate clauses on extension stack", substitute); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG int64_t pushed = 0; #endif Occs &ps = occs (pivot); for (const auto &c : ps) { if (c->garbage) continue; - assert (!c->redundant); + CADICAL_assert (!c->redundant); if (!substitute || c->gate) { if (proof) proof->weaken_minus (c); if (c->size == 2) deleted_binary_clause = true; external->push_clause_on_extension_stack (c, pivot); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG pushed++; #endif } @@ -647,14 +651,14 @@ void Internal::mark_eliminated_clauses_as_garbage ( for (const auto &d : ns) { if (d->garbage) continue; - assert (!d->redundant); + CADICAL_assert (!d->redundant); if (!substitute || d->gate) { if (proof) proof->weaken_minus (d); if (d->size == 2) deleted_binary_clause = true; external->push_clause_on_extension_stack (d, -pivot); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG pushed++; #endif } @@ -664,7 +668,7 @@ void Internal::mark_eliminated_clauses_as_garbage ( erase_occs (ns); if (substitute) - assert (pushed <= substitute); + CADICAL_assert (pushed <= substitute); // Unfortunately, we can not use the trick by Niklas Soerensson anymore, // which avoids saving all clauses on the extension stack. This would @@ -679,7 +683,7 @@ void Internal::try_to_eliminate_variable (Eliminator &eliminator, int pivot, if (!active (pivot)) return; - assert (!frozen (pivot)); + CADICAL_assert (!frozen (pivot)); // First flush garbage clauses. // @@ -693,17 +697,17 @@ void Internal::try_to_eliminate_variable (Eliminator &eliminator, int pivot, LOG ("pivot %d occurs positively %" PRId64 " times and negatively %" PRId64 " times", pivot, pos, neg); - assert (!eliminator.schedule.contains (abs (pivot))); - assert (pos <= neg); + CADICAL_assert (!eliminator.schedule.contains (abs (pivot))); + CADICAL_assert (pos <= neg); if (pos && neg > opts.elimocclim) { LOG ("too many occurrences thus not eliminated %d", pivot); - assert (!eliminator.schedule.contains (abs (pivot))); + CADICAL_assert (!eliminator.schedule.contains (abs (pivot))); return; } LOG ("trying to eliminate %d", pivot); - assert (!flags (pivot).eliminated ()); + CADICAL_assert (!flags (pivot).eliminated ()); // Sort occurrence lists, such that shorter clauses come first. Occs &ps = occs (pivot); @@ -766,15 +770,15 @@ void Internal:: int Internal::elim_round (bool &completed, bool &deleted_binary_clause) { - assert (opts.elim); - assert (!unsat); + CADICAL_assert (opts.elim); + CADICAL_assert (!unsat); START_SIMPLIFIER (elim, ELIM); stats.elimrounds++; int64_t marked_before = last.elim.marked; last.elim.marked = stats.mark.elim; - assert (!level); + CADICAL_assert (!level); int64_t resolution_limit; @@ -813,7 +817,7 @@ int Internal::elim_round (bool &completed, bool &deleted_binary_clause) { else if (tmp < 0) falsified = true; else - assert (active (lit)); + CADICAL_assert (active (lit)); } if (satisfied) mark_garbage (c); // forces more precise counts @@ -832,7 +836,7 @@ int Internal::elim_round (bool &completed, bool &deleted_binary_clause) { Eliminator eliminator (this); ElimSchedule &schedule = eliminator.schedule; - assert (schedule.empty ()); + CADICAL_assert (schedule.empty ()); // Now find elimination candidates which occurred in clauses removed since // the last time we ran bounded variable elimination, which in turned @@ -851,7 +855,7 @@ int Internal::elim_round (bool &completed, bool &deleted_binary_clause) { schedule.shrink (); -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t scheduled = schedule.size (); #endif @@ -867,7 +871,7 @@ int Internal::elim_round (bool &completed, bool &deleted_binary_clause) { if (active (lit)) occs (lit).push_back (c); -#ifndef QUIET +#ifndef CADICAL_QUIET const int64_t old_resolutions = stats.elimres; #endif const int old_eliminated = stats.all.eliminated; @@ -882,7 +886,7 @@ int Internal::elim_round (bool &completed, bool &deleted_binary_clause) { // schedule is updated dynamically and variables are potentially // rescheduled to be tried again if they occur in a removed clause. // -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t tried = 0; #endif while (!unsat && !terminated_asynchronously () && @@ -891,7 +895,7 @@ int Internal::elim_round (bool &completed, bool &deleted_binary_clause) { schedule.pop_front (); flags (idx).elim = false; try_to_eliminate_variable (eliminator, idx, deleted_binary_clause); -#ifndef QUIET +#ifndef CADICAL_QUIET tried++; #endif if (stats.garbage.literals <= garbage_limit) @@ -931,7 +935,7 @@ int Internal::elim_round (bool &completed, bool &deleted_binary_clause) { mark_redundant_clauses_with_eliminated_variables_as_garbage (); int eliminated = stats.all.eliminated - old_eliminated; -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t resolutions = stats.elimres - old_resolutions; PHASE ("elim-round", stats.elimrounds, "eliminated %d variables %.0f%% in %" PRId64 " resolutions", @@ -1000,13 +1004,13 @@ void Internal::increase_elimination_bound () { void Internal::init_citten () { if (!opts.elimdef) return; - assert (!citten); - citten = kitten_init (); + CADICAL_assert (!citten); + citten = cadical_kitten_init (); } void Internal::reset_citten () { if (citten) { - kitten_release (citten); + cadical_kitten_release (citten); citten = 0; } } @@ -1029,11 +1033,11 @@ void Internal::elim (bool update_limits) { "starting at most %d elimination rounds", opts.elimrounds); if (external_prop) { - assert (!level); + CADICAL_assert (!level); private_steps = true; } -#ifndef QUIET +#ifndef CADICAL_QUIET int old_active_variables = active (); int old_eliminated = stats.all.eliminated; #endif @@ -1062,13 +1066,13 @@ void Internal::elim (bool update_limits) { bool phase_complete = false, deleted_binary_clause = false; int round = 1; -#ifndef QUIET +#ifndef CADICAL_QUIET int eliminated = 0; #endif bool round_complete = false; while (!unsat && !phase_complete && !terminated_asynchronously ()) { -#ifndef QUIET +#ifndef CADICAL_QUIET int eliminated = #endif elim_round (round_complete, deleted_binary_clause); @@ -1076,7 +1080,7 @@ void Internal::elim (bool update_limits) { if (!round_complete) { PHASE ("elim-phase", stats.elimphases, "last round %d incomplete %s", round, eliminated ? "but successful" : "and unsuccessful"); - assert (!phase_complete); + CADICAL_assert (!phase_complete); break; } @@ -1085,7 +1089,7 @@ void Internal::elim (bool update_limits) { round - 1, eliminated ? "though last round successful" : "last round unsuccessful anyhow"); - assert (!phase_complete); + CADICAL_assert (!phase_complete); break; } @@ -1105,7 +1109,7 @@ void Internal::elim (bool update_limits) { PHASE ("elim-phase", stats.elimphases, "no new variable elimination candidates"); - assert (round_complete); + CADICAL_assert (round_complete); phase_complete = true; } @@ -1145,14 +1149,14 @@ void Internal::elim (bool update_limits) { if (phase_complete) increase_elimination_bound (); -#ifndef QUIET +#ifndef CADICAL_QUIET eliminated = stats.all.eliminated - old_eliminated; PHASE ("elim-phase", stats.elimphases, "eliminated %d variables %.2f%%", eliminated, percent (eliminated, old_active_variables)); #endif if (external_prop) { - assert (!level); + CADICAL_assert (!level); private_steps = false; } @@ -1170,3 +1174,5 @@ void Internal::elim (bool update_limits) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/elimfast.cpp b/src/sat/cadical/cadical_elimfast.cpp similarity index 91% rename from src/sat/cadical/elimfast.cpp rename to src/sat/cadical/cadical_elimfast.cpp index 6806a0637..90dacf8c0 100644 --- a/src/sat/cadical/elimfast.cpp +++ b/src/sat/cadical/cadical_elimfast.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -30,7 +34,7 @@ int64_t Internal::flush_elimfast_occs (int lit) { break; } if (++res > occslim) { - assert (opts.fastelimbound < 0 || res == failed); + CADICAL_assert (opts.fastelimbound < 0 || res == failed); break; } } @@ -54,13 +58,13 @@ int64_t Internal::flush_elimfast_occs (int lit) { bool Internal::elimfast_resolvents_are_bounded (Eliminator &eliminator, int pivot) { - assert (eliminator.gates.empty ()); - assert (!eliminator.definition_unit); + CADICAL_assert (eliminator.gates.empty ()); + CADICAL_assert (!eliminator.definition_unit); stats.elimtried++; - assert (!unsat); - assert (active (pivot)); + CADICAL_assert (!unsat); + CADICAL_assert (active (pivot)); const Occs &ps = occs (pivot); const Occs &ns = occs (-pivot); @@ -95,11 +99,11 @@ bool Internal::elimfast_resolvents_are_bounded (Eliminator &eliminator, int64_t resolvents = 0; // Non-tautological resolvents. for (const auto &c : ps) { - assert (!c->redundant); + CADICAL_assert (!c->redundant); if (c->garbage) continue; for (const auto &d : ns) { - assert (!d->redundant); + CADICAL_assert (!d->redundant); if (d->garbage) continue; if (resolve_clauses (eliminator, c, pivot, d, true)) { @@ -139,13 +143,13 @@ bool Internal::elimfast_resolvents_are_bounded (Eliminator &eliminator, inline void Internal::elimfast_add_resolvents (Eliminator &eliminator, int pivot) { - assert (eliminator.gates.empty ()); - assert (!eliminator.definition_unit); + CADICAL_assert (eliminator.gates.empty ()); + CADICAL_assert (!eliminator.definition_unit); LOG ("adding all resolvents on %d", pivot); - assert (!val (pivot)); - assert (!flags (pivot).eliminated ()); + CADICAL_assert (!val (pivot)); + CADICAL_assert (!flags (pivot).eliminated ()); const Occs &ps = occs (pivot); const Occs &ns = occs (-pivot); @@ -164,7 +168,7 @@ inline void Internal::elimfast_add_resolvents (Eliminator &eliminator, continue; if (!resolve_clauses (eliminator, c, pivot, d, false)) continue; - assert (!lrat || !lrat_chain.empty ()); + CADICAL_assert (!lrat || !lrat_chain.empty ()); Clause *r = new_resolved_irredundant_clause (); elim_update_added_clause (eliminator, r); eliminator.enqueue (r); @@ -188,7 +192,7 @@ void Internal::try_to_fasteliminate_variable (Eliminator &eliminator, if (!active (pivot)) return; - assert (!frozen (pivot)); + CADICAL_assert (!frozen (pivot)); // First flush garbage clauses and check limits. @@ -197,14 +201,14 @@ void Internal::try_to_fasteliminate_variable (Eliminator &eliminator, int64_t pos = flush_elimfast_occs (pivot); if (pos > bound) { LOG ("too many occurrences thus not eliminated %d", pivot); - assert (!eliminator.schedule.contains (abs (pivot))); + CADICAL_assert (!eliminator.schedule.contains (abs (pivot))); return; } int64_t neg = flush_elimfast_occs (-pivot); if (neg > bound) { LOG ("too many occurrences thus not eliminated %d", -pivot); - assert (!eliminator.schedule.contains (abs (pivot))); + CADICAL_assert (!eliminator.schedule.contains (abs (pivot))); return; } @@ -222,11 +226,11 @@ void Internal::try_to_fasteliminate_variable (Eliminator &eliminator, " times and negatively %" PRId64 " times", pivot, pos, neg); - assert (!eliminator.schedule.contains (abs (pivot))); - assert (pos <= neg); + CADICAL_assert (!eliminator.schedule.contains (abs (pivot))); + CADICAL_assert (pos <= neg); LOG ("trying to eliminate %d", pivot); - assert (!flags (pivot).eliminated ()); + CADICAL_assert (!flags (pivot).eliminated ()); // Sort occurrence lists, such that shorter clauses come first. Occs &ps = occs (pivot); @@ -264,14 +268,14 @@ void Internal::try_to_fasteliminate_variable (Eliminator &eliminator, int Internal::elimfast_round (bool &completed, bool &deleted_binary_clause) { - assert (opts.fastelim); - assert (!unsat); + CADICAL_assert (opts.fastelim); + CADICAL_assert (!unsat); START_SIMPLIFIER (fastelim, ELIM); stats.elimfastrounds++; - assert (!level); + CADICAL_assert (!level); int64_t resolution_limit; @@ -310,7 +314,7 @@ int Internal::elimfast_round (bool &completed, else if (tmp < 0) falsified = true; else - assert (active (lit)); + CADICAL_assert (active (lit)); } if (satisfied) mark_garbage (c); // forces more precise counts @@ -329,7 +333,7 @@ int Internal::elimfast_round (bool &completed, Eliminator eliminator (this); ElimSchedule &schedule = eliminator.schedule; - assert (schedule.empty ()); + CADICAL_assert (schedule.empty ()); // Now find elimination candidates which occurred in clauses removed since // the last time we ran bounded variable elimination, which in turned @@ -348,7 +352,7 @@ int Internal::elimfast_round (bool &completed, schedule.shrink (); -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t scheduled = schedule.size (); #endif @@ -364,7 +368,7 @@ int Internal::elimfast_round (bool &completed, if (active (lit)) occs (lit).push_back (c); -#ifndef QUIET +#ifndef CADICAL_QUIET const int64_t old_resolutions = stats.elimres; #endif const int old_eliminated = stats.all.eliminated; @@ -379,7 +383,7 @@ int Internal::elimfast_round (bool &completed, // schedule is updated dynamically and variables are potentially // rescheduled to be tried again if they occur in a removed clause. // -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t tried = 0; #endif while (!unsat && !terminated_asynchronously () && @@ -388,7 +392,7 @@ int Internal::elimfast_round (bool &completed, schedule.pop_front (); flags (idx).elim = false; try_to_fasteliminate_variable (eliminator, idx, deleted_binary_clause); -#ifndef QUIET +#ifndef CADICAL_QUIET tried++; #endif if (stats.garbage.literals <= garbage_limit) @@ -419,7 +423,7 @@ int Internal::elimfast_round (bool &completed, int eliminated = stats.all.eliminated - old_eliminated; stats.all.fasteliminated += eliminated; -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t resolutions = stats.elimres - old_resolutions; PHASE ("fastelim-round", stats.elimfastrounds, "eliminated %d variables %.0f%% in %" PRId64 " resolutions", @@ -451,11 +455,11 @@ void Internal::elimfast () { "starting at most %d elimination rounds", opts.fastelimrounds); if (external_prop) { - assert (!level); + CADICAL_assert (!level); private_steps = true; } -#ifndef QUIET +#ifndef CADICAL_QUIET int old_active_variables = active (); int old_eliminated = stats.all.eliminated; #endif @@ -476,13 +480,13 @@ void Internal::elimfast () { bool phase_complete = false, deleted_binary_clause = false; int round = 1; -#ifndef QUIET +#ifndef CADICAL_QUIET int eliminated = 0; #endif bool round_complete = false; while (!unsat && !phase_complete && !terminated_asynchronously ()) { -#ifndef QUIET +#ifndef CADICAL_QUIET int eliminated = #endif elimfast_round (round_complete, deleted_binary_clause); @@ -491,7 +495,7 @@ void Internal::elimfast () { PHASE ("fastelim-phase", stats.elimphases, "last round %d incomplete %s", round, eliminated ? "but successful" : "and unsuccessful"); - assert (!phase_complete); + CADICAL_assert (!phase_complete); break; } @@ -500,7 +504,7 @@ void Internal::elimfast () { round - 1, eliminated ? "though last round successful" : "last round unsuccessful anyhow"); - assert (!phase_complete); + CADICAL_assert (!phase_complete); break; } @@ -516,7 +520,7 @@ void Internal::elimfast () { PHASE ("fastelim-phase", stats.elimphases, "no new variable elimination candidates"); - assert (round_complete); + CADICAL_assert (round_complete); phase_complete = true; } @@ -554,7 +558,7 @@ void Internal::elimfast () { } } -#ifndef QUIET +#ifndef CADICAL_QUIET eliminated = stats.all.eliminated - old_eliminated; PHASE ("fastelim-phase", stats.elimphases, "eliminated %d variables %.2f%%", eliminated, @@ -562,9 +566,11 @@ void Internal::elimfast () { #endif if (external_prop) { - assert (!level); + CADICAL_assert (!level); private_steps = false; } } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/ema.cpp b/src/sat/cadical/cadical_ema.cpp similarity index 96% rename from src/sat/cadical/ema.cpp rename to src/sat/cadical/cadical_ema.cpp index ef6581482..e8faf5d76 100644 --- a/src/sat/cadical/ema.cpp +++ b/src/sat/cadical/cadical_ema.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // Updating an exponential moving average is placed here since we want to @@ -70,10 +74,10 @@ void EMA::update (Internal *internal, double y, const char *name) { double new_exp, div, new_value; if (old_exp) { new_exp = old_exp * beta; - assert (new_exp < 1); + CADICAL_assert (new_exp < 1); exp = new_exp; div = 1 - new_exp; - assert (div > 0); + CADICAL_assert (div > 0); new_value = new_biased / div; } else { new_value = new_biased; @@ -93,3 +97,5 @@ void EMA::update (Internal *internal, double y, const char *name) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/extend.cpp b/src/sat/cadical/cadical_extend.cpp similarity index 86% rename from src/sat/cadical/extend.cpp rename to src/sat/cadical/cadical_extend.cpp index 26f081360..c549e90fa 100644 --- a/src/sat/cadical/extend.cpp +++ b/src/sat/cadical/cadical_extend.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void External::push_zero_on_extension_stack () { @@ -16,18 +20,18 @@ void External::push_id_on_extension_stack (int64_t id) { } void External::push_clause_literal_on_extension_stack (int ilit) { - assert (ilit); + CADICAL_assert (ilit); const int elit = internal->externalize (ilit); - assert (elit); + CADICAL_assert (elit); extension.push_back (elit); LOG ("pushing clause literal %d on extension stack (internal %d)", elit, ilit); } void External::push_witness_literal_on_extension_stack (int ilit) { - assert (ilit); + CADICAL_assert (ilit); const int elit = internal->externalize (ilit); - assert (elit); + CADICAL_assert (elit); extension.push_back (elit); LOG ("pushing witness literal %d on extension stack (internal %d)", elit, ilit); @@ -77,10 +81,10 @@ void External::push_binary_clause_on_extension_stack (int64_t id, int pivot, void External::push_external_clause_and_witness_on_extension_stack ( const vector &c, const vector &w, int64_t id) { - assert (id); + CADICAL_assert (id); extension.push_back (0); for (const auto &elit : w) { - assert (elit != INT_MIN); + CADICAL_assert (elit != INT_MIN); init (abs (elit)); extension.push_back (elit); mark (witness, elit); @@ -92,7 +96,7 @@ void External::push_external_clause_and_witness_on_extension_stack ( extension.push_back (lower_bits); extension.push_back (0); for (const auto &elit : c) { - assert (elit != INT_MIN); + CADICAL_assert (elit != INT_MIN); init (abs (elit)); extension.push_back (elit); } @@ -112,7 +116,7 @@ void External::push_external_clause_and_witness_on_extension_stack ( void External::extend () { - assert (!extended); + CADICAL_assert (!extended); START (extend); internal->stats.extensions++; @@ -120,7 +124,7 @@ void External::extend () { "mapping internal %d assignments to %d assignments", internal->max_var, max_var); -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t updated = 0; #endif for (unsigned i = 1; i <= (unsigned) max_var; i++) { @@ -130,7 +134,7 @@ void External::extend () { if (i >= vals.size ()) vals.resize (i + 1, false); vals[i] = (internal->val (ilit) > 0); -#ifndef QUIET +#ifndef CADICAL_QUIET updated++; #endif } @@ -141,50 +145,50 @@ void External::extend () { extension.size ()); const auto begin = extension.begin (); auto i = extension.end (); -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t flipped = 0; #endif while (i != begin) { bool satisfied = false; int lit; - assert (i != begin); + CADICAL_assert (i != begin); while ((lit = *--i)) { if (satisfied) continue; if (ival (lit) == lit) satisfied = true; - assert (i != begin); + CADICAL_assert (i != begin); } - assert (i != begin); + CADICAL_assert (i != begin); LOG ("id=%" PRId64, ((int64_t) *i << 32) + *(i - 1)); - assert (*i || *(i - 1)); + CADICAL_assert (*i || *(i - 1)); --i; - assert (i != begin); + CADICAL_assert (i != begin); --i; - assert (i != begin); - assert (!*i); + CADICAL_assert (i != begin); + CADICAL_assert (!*i); --i; - assert (i != begin); + CADICAL_assert (i != begin); if (satisfied) while (*--i) - assert (i != begin); + CADICAL_assert (i != begin); else { while ((lit = *--i)) { const int tmp = ival (lit); // not 'signed char'!!! if (tmp != lit) { LOG ("flipping blocking literal %d", lit); - assert (lit); - assert (lit != INT_MIN); + CADICAL_assert (lit); + CADICAL_assert (lit != INT_MIN); size_t idx = abs (lit); if (idx >= vals.size ()) vals.resize (idx + 1, false); vals[idx] = !vals[idx]; internal->stats.extended++; -#ifndef QUIET +#ifndef CADICAL_QUIET flipped++; #endif } - assert (i != begin); + CADICAL_assert (i != begin); } } } @@ -207,14 +211,14 @@ bool External::traverse_witnesses_backward (WitnessIterator &it) { int lit; while ((lit = *--i)) clause.push_back (lit); - assert (!lit); + CADICAL_assert (!lit); --i; const int64_t id = ((int64_t) * (i - 1) << 32) + static_cast (*i); - assert (id); + CADICAL_assert (id); i -= 2; - assert (!*i); - assert (i != begin); + CADICAL_assert (!*i); + CADICAL_assert (i != begin); while ((lit = *--i)) witness.push_back (lit); reverse (clause.begin (), clause.end ()); @@ -237,18 +241,18 @@ bool External::traverse_witnesses_forward (WitnessIterator &it) { if (i != end) { int lit = *i++; do { - assert (!lit), (void) lit; + CADICAL_assert (!lit), (void) lit; while ((lit = *i++)) witness.push_back (lit); - assert (!lit); - assert (i != end); - assert (!*i); + CADICAL_assert (!lit); + CADICAL_assert (i != end); + CADICAL_assert (!*i); const int64_t id = ((int64_t) *i << 32) + static_cast (*(i + 1)); - assert (id > 0); + CADICAL_assert (id > 0); i += 3; - assert (*i); - assert (i != end); + CADICAL_assert (*i); + CADICAL_assert (i != end); while (i != end && (lit = *i++)) clause.push_back (lit); if (!it.witness (clause, witness, id)) @@ -279,3 +283,5 @@ void External::conclude_sat () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/external.cpp b/src/sat/cadical/cadical_external.cpp similarity index 90% rename from src/sat/cadical/external.cpp rename to src/sat/cadical/cadical_external.cpp index acb11a9d6..67d1f918d 100644 --- a/src/sat/cadical/external.cpp +++ b/src/sat/cadical/cadical_external.cpp @@ -1,14 +1,18 @@ +#include "global.h" + #include "internal.hpp" #include +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { External::External (Internal *i) : internal (i), max_var (0), vsize (0), extended (false), concluded (false), terminator (0), learner (0), fixed_listener (0), propagator (0), solution (0), vars (max_var) { - assert (internal); - assert (!internal->external); + CADICAL_assert (internal); + CADICAL_assert (!internal->external); internal->external = this; } @@ -19,7 +23,7 @@ External::~External () { void External::enlarge (int new_max_var) { - assert (!extended); + CADICAL_assert (!extended); size_t new_vsize = vsize ? 2 * vsize : 1 + (size_t) new_max_var; while (new_vsize <= (size_t) new_max_var) @@ -29,7 +33,7 @@ void External::enlarge (int new_max_var) { } void External::init (int new_max_var, bool extension) { - assert (!extended); + CADICAL_assert (!extended); if (new_max_var <= max_var) return; int new_vars = new_max_var - max_var; @@ -40,31 +44,31 @@ void External::init (int new_max_var, bool extension) { enlarge (new_max_var); LOG ("initialized %d external variables", new_vars); if (!max_var) { - assert (e2i.empty ()); + CADICAL_assert (e2i.empty ()); e2i.push_back (0); ext_units.push_back (0); ext_units.push_back (0); ext_flags.push_back (0); ervars.push_back (0); - assert (internal->i2e.empty ()); + CADICAL_assert (internal->i2e.empty ()); internal->i2e.push_back (0); } else { - assert (e2i.size () == (size_t) max_var + 1); - assert (internal->i2e.size () == (size_t) old_internal_max_var + 1); + CADICAL_assert (e2i.size () == (size_t) max_var + 1); + CADICAL_assert (internal->i2e.size () == (size_t) old_internal_max_var + 1); } unsigned iidx = old_internal_max_var + 1, eidx; for (eidx = max_var + 1u; eidx <= (unsigned) new_max_var; eidx++, iidx++) { LOG ("mapping external %u to internal %u", eidx, iidx); - assert (e2i.size () == eidx); + CADICAL_assert (e2i.size () == eidx); e2i.push_back (iidx); ext_units.push_back (0); ext_units.push_back (0); ext_flags.push_back (0); ervars.push_back (0); internal->i2e.push_back (eidx); - assert (internal->i2e[iidx] == (int) eidx); - assert (e2i[eidx] == (int) iidx); + CADICAL_assert (internal->i2e[iidx] == (int) eidx); + CADICAL_assert (e2i[eidx] == (int) iidx); } if (extension) internal->stats.variables_extension += new_vars; @@ -75,9 +79,9 @@ void External::init (int new_max_var, bool extension) { if (internal->opts.checkfrozen) if (new_max_var >= (int64_t) moltentab.size ()) moltentab.resize (1 + (size_t) new_max_var, false); - assert (iidx == (size_t) new_internal_max_var + 1); - assert (eidx == (size_t) new_max_var + 1); - assert (ext_units.size () == (size_t) new_max_var * 2 + 2); + CADICAL_assert (iidx == (size_t) new_internal_max_var + 1); + CADICAL_assert (eidx == (size_t) new_max_var + 1); + CADICAL_assert (ext_units.size () == (size_t) new_max_var * 2 + 2); max_var = new_max_var; } @@ -116,7 +120,7 @@ void External::reset_limits () { internal->reset_limits (); } int External::internalize (int elit, bool extension) { int ilit; if (elit) { - assert (elit != INT_MIN); + CADICAL_assert (elit != INT_MIN); const int eidx = abs (elit); if (extension && eidx <= max_var) FATAL ("can not add a definition for an already used variable %d", @@ -125,27 +129,27 @@ int External::internalize (int elit, bool extension) { init (eidx, extension); } if (extension) { - assert (ervars.size () > (size_t) eidx); + CADICAL_assert (ervars.size () > (size_t) eidx); ervars[eidx] = true; } ilit = e2i[eidx]; if (elit < 0) ilit = -ilit; if (!ilit) { - assert (internal->max_var < INT_MAX); + CADICAL_assert (internal->max_var < INT_MAX); ilit = internal->max_var + 1u; internal->init_vars (ilit); e2i[eidx] = ilit; LOG ("mapping external %d to internal %d", eidx, ilit); e2i[eidx] = ilit; internal->i2e.push_back (eidx); - assert (internal->i2e[ilit] == eidx); - assert (e2i[eidx] == ilit); + CADICAL_assert (internal->i2e[ilit] == eidx); + CADICAL_assert (e2i[eidx] == ilit); if (elit < 0) ilit = -ilit; } if (internal->opts.checkfrozen) { - assert (eidx < (int64_t) moltentab.size ()); + CADICAL_assert (eidx < (int64_t) moltentab.size ()); if (moltentab[eidx]) FATAL ("can not reuse molten literal %d", eidx); } @@ -155,7 +159,7 @@ int External::internalize (int elit, bool extension) { else if (f.status != Flags::ACTIVE && f.status != Flags::FIXED) internal->reactivate (ilit); if (!marked (tainted, elit) && marked (witness, -elit)) { - assert (!internal->opts.checkfrozen); + CADICAL_assert (!internal->opts.checkfrozen); LOG ("marking tainted %d", elit); mark (tainted, elit); } @@ -165,7 +169,7 @@ int External::internalize (int elit, bool extension) { } void External::add (int elit) { - assert (elit != INT_MIN); + CADICAL_assert (elit != INT_MIN); reset_extended (); bool forgettable = false; @@ -186,7 +190,7 @@ void External::add (int elit) { } const int ilit = internalize (elit); - assert (!elit == !ilit); + CADICAL_assert (!elit == !ilit); // The external literals of the new clause must be saved for later // when the proof is printed during add_original_lit (0) @@ -195,7 +199,7 @@ void External::add (int elit) { if (internal->lrat) { // actually find unit of -elit (flips elit < 0) unsigned eidx = (elit > 0) + 2u * (unsigned) abs (elit); - assert ((size_t) eidx < ext_units.size ()); + CADICAL_assert ((size_t) eidx < ext_units.size ()); const int64_t id = ext_units[eidx]; bool added = ext_flags[abs (elit)]; if (id && !added) { @@ -221,21 +225,21 @@ void External::add (int elit) { } void External::assume (int elit) { - assert (elit); + CADICAL_assert (elit); reset_extended (); if (internal->proof) internal->proof->add_assumption (elit); assumptions.push_back (elit); const int ilit = internalize (elit); - assert (ilit); + CADICAL_assert (ilit); LOG ("assuming external %d as internal %d", elit, ilit); internal->assume (ilit); } bool External::flip (int elit) { - assert (elit); - assert (elit != INT_MIN); - assert (!propagator); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); + CADICAL_assert (!propagator); int eidx = abs (elit); if (eidx > max_var) @@ -252,9 +256,9 @@ bool External::flip (int elit) { } bool External::flippable (int elit) { - assert (elit); - assert (elit != INT_MIN); - assert (!propagator); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); + CADICAL_assert (!propagator); int eidx = abs (elit); if (eidx > max_var) @@ -268,8 +272,8 @@ bool External::flippable (int elit) { } bool External::failed (int elit) { - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); int eidx = abs (elit); if (eidx > max_var) return 0; @@ -286,10 +290,10 @@ void External::constrain (int elit) { LOG (constraint, "replacing previous constraint"); reset_constraint (); } - assert (elit != INT_MIN); + CADICAL_assert (elit != INT_MIN); reset_extended (); const int ilit = internalize (elit); - assert (!elit == !ilit); + CADICAL_assert (!elit == !ilit); if (elit) LOG ("adding external %d as internal %d to constraint", elit, ilit); else if (!elit && internal->proof) { @@ -304,15 +308,15 @@ bool External::failed_constraint () { } void External::phase (int elit) { - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); const int ilit = internalize (elit); internal->phase (ilit); } void External::unphase (int elit) { - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); int eidx = abs (elit); if (eidx > max_var) { UNUSED: @@ -341,15 +345,15 @@ void External::add_observed_var (int elit) { return; } - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); reset_extended (); // tainting! int eidx = abs (elit); if (eidx <= max_var && (marked (witness, elit) || marked (witness, -elit))) { LOG ("Error, only clean variables are allowed to become observed."); - assert (false); + CADICAL_assert (false); // TODO: here needs to come the taint and restore of the newly // observed variable. Restore_clauses must be called before continue. @@ -395,7 +399,7 @@ void External::add_observed_var (int elit) { unit); // internal add-observed-var had to backtrack to root-level already - assert (!internal->level); + CADICAL_assert (!internal->level); std::vector assigned = {unit}; propagator->notify_assignment (assigned); @@ -425,7 +429,7 @@ void External::remove_observed_var (int elit) { void External::reset_observed_vars () { // Shouldn't be called if there is no connected propagator - assert (propagator); + CADICAL_assert (propagator); reset_extended (); internal->notified = 0; @@ -434,11 +438,11 @@ void External::reset_observed_vars () { if (!is_observed.size ()) return; - assert (!max_var || (size_t) max_var + 1 == is_observed.size ()); + CADICAL_assert (!max_var || (size_t) max_var + 1 == is_observed.size ()); for (auto elit : vars) { int eidx = abs (elit); - assert (eidx <= max_var); + CADICAL_assert (eidx <= max_var); if (is_observed[eidx]) { int ilit = internalize (elit); internal->remove_observed_var (ilit); @@ -450,8 +454,8 @@ void External::reset_observed_vars () { } bool External::observed (int elit) { - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); int eidx = abs (elit); if (eidx > max_var) return false; @@ -462,8 +466,8 @@ bool External::observed (int elit) { } bool External::is_witness (int elit) { - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); int eidx = abs (elit); if (eidx > max_var) return false; @@ -471,8 +475,8 @@ bool External::is_witness (int elit) { } bool External::is_decision (int elit) { - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); int eidx = abs (elit); if (eidx > max_var) return false; @@ -513,7 +517,7 @@ void External::implied (std::vector &trailed) { trailed.clear(); for (const auto &ilit : ilit_implicants) { - assert (ilit); + CADICAL_assert (ilit); const int elit = internal->externalize (ilit); const int eidx = abs (elit); const bool is_extension_var = ervars[eidx]; @@ -578,7 +582,7 @@ void External::check_solve_result (int res) { void External::update_molten_literals () { if (!internal->opts.checkfrozen) return; - assert ((size_t) max_var + 1 == moltentab.size ()); + CADICAL_assert ((size_t) max_var + 1 == moltentab.size ()); #ifdef LOGGING int registered = 0, molten = 0; #endif @@ -667,9 +671,9 @@ void External::melt (int elit) { reset_extended (); int ilit = internalize (elit); unsigned eidx = vidx (elit); - assert (eidx < frozentab.size ()); + CADICAL_assert (eidx < frozentab.size ()); unsigned &ref = frozentab[eidx]; - assert (ref > 0); + CADICAL_assert (ref > 0); if (ref < UINT_MAX) { if (!--ref) { if (observed (elit)) { @@ -700,10 +704,10 @@ void External::check_assignment (int (External::*a) (int) const) { int value_idx = (this->*a) (idx); int value_neg_idx = (this->*a) (-idx); if (value_idx == idx) - assert (value_neg_idx == idx); + CADICAL_assert (value_neg_idx == idx); else { - assert (value_idx == -idx); - assert (value_neg_idx == -idx); + CADICAL_assert (value_idx == -idx); + CADICAL_assert (value_neg_idx == -idx); } if (value_idx != value_neg_idx) FATAL ("inconsistently assigned literals %d and %d", idx, -idx); @@ -714,7 +718,7 @@ void External::check_assignment (int (External::*a) (int) const) { bool satisfied = false; const auto end = original.end (); auto start = original.begin (), i = start; -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t count = 0; #endif for (; i != end; i++) { @@ -730,7 +734,7 @@ void External::check_assignment (int (External::*a) (int) const) { } satisfied = false; start = i + 1; -#ifndef QUIET +#ifndef CADICAL_QUIET count++; #endif } else if (!satisfied && (this->*a) (lit) == lit) @@ -747,7 +751,7 @@ void External::check_assignment (int (External::*a) (int) const) { break; presence_flag = true; satisfied = false; -#ifndef QUIET +#ifndef CADICAL_QUIET count++; #endif std::vector literals; @@ -778,7 +782,7 @@ void External::check_assignment (int (External::*a) (int) const) { fatal_message_end (); } } -#ifndef QUIET +#ifndef CADICAL_QUIET VERBOSE (1, "satisfying assignment checked on %" PRId64 " clauses", count); #endif @@ -945,7 +949,7 @@ bool External::traverse_all_non_frozen_units_as_witnesses ( const int ilit = e2i[idx] * (tmp < 0 ? -1 : 1); // heurstically add + max_var to the id to avoid reusing ids const int64_t id = internal->lrat ? internal->unit_id (ilit) : 1; - assert (id); + CADICAL_assert (id); clause_and_witness.push_back (unit); if (!it.witness (clause_and_witness, clause_and_witness, id + max_var)) return false; @@ -972,8 +976,8 @@ void External::copy_flags (External &other) const { continue; if (!other.internal->active (other_ilit)) continue; - assert (this_ilit != INT_MIN); - assert (other_ilit != INT_MIN); + CADICAL_assert (this_ilit != INT_MIN); + CADICAL_assert (other_ilit != INT_MIN); const Flags &this_flags = this_ftab[abs (this_ilit)]; Flags &other_flags = other_ftab[abs (other_ilit)]; this_flags.copy (other_flags); @@ -983,7 +987,7 @@ void External::copy_flags (External &other) const { /*------------------------------------------------------------------------*/ void External::export_learned_empty_clause () { - assert (learner); + CADICAL_assert (learner); if (learner->learning (0)) { LOG ("exporting learned empty clause"); learner->learn (0); @@ -992,11 +996,11 @@ void External::export_learned_empty_clause () { } void External::export_learned_unit_clause (int ilit) { - assert (learner); + CADICAL_assert (learner); if (learner->learning (1)) { LOG ("exporting learned unit clause"); const int elit = internal->externalize (ilit); - assert (elit); + CADICAL_assert (elit); learner->learn (elit); learner->learn (0); } else @@ -1004,14 +1008,14 @@ void External::export_learned_unit_clause (int ilit) { } void External::export_learned_large_clause (const vector &clause) { - assert (learner); + CADICAL_assert (learner); size_t size = clause.size (); - assert (size <= (unsigned) INT_MAX); + CADICAL_assert (size <= (unsigned) INT_MAX); if (learner->learning ((int) size)) { LOG ("exporting learned clause of size %zu", size); for (auto ilit : clause) { const int elit = internal->externalize (ilit); - assert (elit); + CADICAL_assert (elit); learner->learn (elit); } learner->learn (0); @@ -1020,3 +1024,5 @@ void External::export_learned_large_clause (const vector &clause) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/external_propagate.cpp b/src/sat/cadical/cadical_external_propagate.cpp similarity index 92% rename from src/sat/cadical/external_propagate.cpp rename to src/sat/cadical/cadical_external_propagate.cpp index 5b1605a7f..215170c8b 100644 --- a/src/sat/cadical/external_propagate.cpp +++ b/src/sat/cadical/cadical_external_propagate.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*----------------------------------------------------------------------------*/ @@ -43,12 +47,12 @@ void Internal::remove_observed_var (int ilit) { backtrack (); } - assert (fixed (ilit) || !level); + CADICAL_assert (fixed (ilit) || !level); const int idx = vidx (ilit); - assert ((size_t) idx < relevanttab.size ()); + CADICAL_assert ((size_t) idx < relevanttab.size ()); unsigned &ref = relevanttab[idx]; - assert (fixed (ilit) || ref > 0); + CADICAL_assert (fixed (ilit) || ref > 0); if (fixed (ilit)) ref = 0; else if (ref < UINT_MAX) { @@ -66,7 +70,7 @@ void Internal::remove_observed_var (int ilit) { // Supposed to be used only by mobical. // bool Internal::observed (int ilit) const { - assert ((size_t) vidx (ilit) < relevanttab.size ()); + CADICAL_assert ((size_t) vidx (ilit) < relevanttab.size ()); return relevanttab[vidx (ilit)] > 0; } @@ -87,7 +91,7 @@ void Internal::set_tainted_literal () { tainted_literal = idx; continue; } - assert (val (tainted_literal)); + CADICAL_assert (val (tainted_literal)); if (var (idx).level < var (tainted_literal).level) { tainted_literal = idx; } @@ -100,7 +104,7 @@ void Internal::renotify_trail_after_ilb () { return; } LOG ("notify external propagator about new assignments (after ilb)"); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG LOG ("(decision level: %d, trail size: %zd, notified %zd)", level, trail.size (), notified); #endif @@ -113,7 +117,7 @@ void Internal::renotify_trail_after_local_search () { } LOG ("notify external propagator about new assignments (after local " "search)"); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG LOG ("(decision level: %d, trail size: %zd, notified %zd)", level, trail.size (), notified); #endif @@ -161,12 +165,12 @@ void Internal::renotify_full_trail () { continue; int elit = externalize (ilit); // TODO: double-check tainting - assert (elit); + CADICAL_assert (elit); // Fixed variables might get mapped (during compact) to another // non-observed but fixed variable. // This happens on root level, so notification about their assignment is // already done. - assert (external->observed (elit) || fixed (ilit)); + CADICAL_assert (external->observed (elit) || fixed (ilit)); if (!external->ervars[abs (elit)]) assigned.push_back (elit); } @@ -195,7 +199,7 @@ bool Internal::is_decision (int ilit) { const int idx = vidx (ilit); Var &v = var (idx); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG LOG (v.reason, "is_decision: i%d (current level: %d, is_fixed: %d, v.level: %d, " "is_external_reason: %d, v.reason: )", @@ -203,7 +207,7 @@ bool Internal::is_decision (int ilit) { #endif if (!v.level || v.reason) return false; - assert (!v.reason); + CADICAL_assert (!v.reason); return true; } @@ -211,7 +215,7 @@ void Internal::force_backtrack (size_t new_level) { if (!forced_backt_allowed || level <= 0 || new_level >= (size_t) level) return; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG LOG ("external propagator forces backtrack to decision level" "%zd (from level %d)", new_level, level); @@ -231,13 +235,13 @@ void Internal::force_backtrack (size_t new_level) { bool Internal::external_propagate () { if (level) require_mode (SEARCH); - assert (!unsat); + CADICAL_assert (!unsat); size_t before = num_assigned; bool cb_repropagate_needed = true; while (cb_repropagate_needed && !conflict && external_prop && !external_prop_is_lazy && !private_steps) { -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG LOG ("external propagation starts (decision level: %d, trail size: " "%zd, notified %zd)", level, trail.size (), notified); @@ -251,13 +255,13 @@ bool Internal::external_propagate () { stats.ext_prop.ext_cb++; stats.ext_prop.eprop_call++; while (elit) { - assert (external->is_observed[abs (elit)]); + CADICAL_assert (external->is_observed[abs (elit)]); int ilit = external->e2i[abs (elit)]; if (elit < 0) ilit = -ilit; int tmp = val (ilit); -#ifndef NDEBUG - assert (fixed (ilit) || observed (ilit)); +#ifndef CADICAL_NDEBUG + CADICAL_assert (fixed (ilit) || observed (ilit)); LOG ("External propagation of e%d (i%d val: %d)", elit, ilit, tmp); #endif if (!tmp) { @@ -308,7 +312,7 @@ bool Internal::external_propagate () { stats.ext_prop.eprop_call++; } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG LOG ("External propagation ends (decision level: %d, trail size: %zd, " "notified %zd)", level, trail.size (), notified); @@ -331,7 +335,7 @@ bool Internal::external_propagate () { notify_assignments (); } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG if (has_external_clause) LOG ("New external clauses are to be added."); else @@ -367,7 +371,7 @@ bool Internal::external_propagate () { stats.ext_prop.elearn_call++; } } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG LOG ("External clause addition ends on decision level %d at trail " "size " "%zd (notified %zd)", @@ -433,7 +437,7 @@ void Internal::move_literals_to_watch () { highest_level = other_level; highest_value = other_value; } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG LOG ("highest position: %d highest level: %d highest value: %d", highest_position, highest_level, highest_value); #endif @@ -473,7 +477,7 @@ void Internal::move_literals_to_watch () { // void Internal::add_external_clause (int propagated_elit, bool no_backtrack) { - assert (original.empty ()); + CADICAL_assert (original.empty ()); int elit = 0; if (propagated_elit) { @@ -482,7 +486,7 @@ void Internal::add_external_clause (int propagated_elit, // propagator can add them as an explicit unforgettable external clause // or set 'are_reasons_forgettable' to false. ext_clause_forgettable = external->propagator->are_reasons_forgettable; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG LOG ("add external reason of propagated lit: %d", propagated_elit); #endif elit = external->propagator->cb_add_reason_clause_lit (propagated_elit); @@ -497,15 +501,15 @@ void Internal::add_external_clause (int propagated_elit, clause.clear (); // Read out the external lemma into original and simplify it into clause - assert (clause.empty ()); - assert (original.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (original.empty ()); - assert (!force_no_backtrack); - assert (!from_propagator); + CADICAL_assert (!force_no_backtrack); + CADICAL_assert (!from_propagator); force_no_backtrack = no_backtrack; from_propagator = true; while (elit) { - assert (external->is_observed[abs (elit)]); + CADICAL_assert (external->is_observed[abs (elit)]); external->add (elit); if (propagated_elit) elit = @@ -514,8 +518,8 @@ void Internal::add_external_clause (int propagated_elit, elit = external->propagator->cb_add_external_clause_lit (); } external->add (elit); - assert (original.empty ()); - assert (clause.empty ()); + CADICAL_assert (original.empty ()); + CADICAL_assert (clause.empty ()); force_no_backtrack = false; from_propagator = false; lrat_chain = std::move (lrat_chain_ext); @@ -530,11 +534,11 @@ void Internal::add_external_clause (int propagated_elit, void Internal::explain_reason (int ilit, Clause *reason, int &open) { if (!opts.exteagerreasons) return; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG LOG (reason, "explain_reason of %d (open: %d)", ilit, open); #endif - assert (reason); - assert (reason != external_reason); + CADICAL_assert (reason); + CADICAL_assert (reason != external_reason); for (const auto &other : *reason) { if (other == ilit) continue; @@ -544,8 +548,8 @@ void Internal::explain_reason (int ilit, Clause *reason, int &open) { Var &v = var (other); if (!v.level) continue; - assert (val (other) < 0); - assert (v.level <= level); + CADICAL_assert (val (other) < 0); + CADICAL_assert (v.level <= level); if (v.reason == external_reason) { v.reason = learn_external_reason_clause (-other, 0, true); } @@ -577,8 +581,8 @@ void Internal::explain_reason (int ilit, Clause *reason, int &open) { // over-approximated. // void Internal::explain_external_propagations () { - assert (conflict); - assert (clause.empty ()); + CADICAL_assert (conflict); + CADICAL_assert (clause.empty ()); Clause *reason = conflict; std::vector seen_lits; @@ -601,16 +605,16 @@ void Internal::explain_external_propagations () { if (!open) break; } - assert (!open); + CADICAL_assert (!open); if (!opts.exteagerrecalc) { for (auto lit : seen_lits) { Flags &f = flags (lit); f.seen = false; } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (auto idx : vars) { - assert (!flags (idx).seen); + CADICAL_assert (!flags (idx).seen); } #endif } @@ -636,7 +640,7 @@ void Internal::explain_external_propagations () { lrat_chain.clear (); v.reason = 0; } - assert (v.level >= real_level); + CADICAL_assert (v.level >= real_level); if (v.level > real_level) { v.level = real_level; } @@ -646,7 +650,7 @@ void Internal::explain_external_propagations () { #if 0 // has been fuzzed extensively for (auto idx : vars) { - assert (!flags (idx).seen); + CADICAL_assert (!flags (idx).seen); } #endif } @@ -664,18 +668,18 @@ void Internal::explain_external_propagations () { Clause *Internal::learn_external_reason_clause (int ilit, int falsified_elit, bool no_backtrack) { - assert (external->propagator); + CADICAL_assert (external->propagator); // we cannot modify clause during analysis auto clause_tmp = std::move (clause); - assert (clause.empty ()); - assert (original.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (original.empty ()); stats.ext_prop.eprop_expl++; int elit = 0; if (!falsified_elit) { - assert (!fixed (ilit)); + CADICAL_assert (!fixed (ilit)); elit = externalize (ilit); } else elit = falsified_elit; @@ -683,7 +687,7 @@ Clause *Internal::learn_external_reason_clause (int ilit, LOG ("ilit: %d, elit: %d", ilit, elit); add_external_clause (elit, no_backtrack); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG if (!falsified_elit && newest_clause) { // Check if external propagation is correct wrt to the topological order // defined by the trail. In case it is a falsified external propagation @@ -691,7 +695,7 @@ Clause *Internal::learn_external_reason_clause (int ilit, // falsified clause. const int propagated_ilit = ilit; for (auto const reason_ilit : *newest_clause) { - assert (var (reason_ilit).trail <= var (propagated_ilit).trail); + CADICAL_assert (var (reason_ilit).trail <= var (propagated_ilit).trail); } } #endif @@ -721,12 +725,12 @@ Clause *Internal::wrapped_learn_external_reason_clause (int ilit) { // The learn_external_reason clause can leave a literal in clause when // there is a falsified elit arg. Here it is not allowed to // happen. - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); clause = std::move (clause_tmp); clause_tmp.clear (); } - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); lrat_chain = std::move (chain_tmp); chain_tmp.clear (); return res; @@ -747,8 +751,8 @@ void Internal::handle_external_clause (Clause *res) { if (from_propagator) stats.ext_prop.elearn_prop++; // new unit clause. For now just backtrack. - assert (!force_no_backtrack); - assert (level); + CADICAL_assert (!force_no_backtrack); + CADICAL_assert (level); // if (!opts.chrono) { backtrack (); // } @@ -756,18 +760,18 @@ void Internal::handle_external_clause (Clause *res) { } if (from_propagator) stats.ext_prop.elearned++; - assert (res->size >= 2); + CADICAL_assert (res->size >= 2); const int pos0 = res->literals[0]; const int pos1 = res->literals[1]; if (force_no_backtrack) { - assert (val (pos1) < 0); - assert (val (pos0) >= 0); + CADICAL_assert (val (pos1) < 0); + CADICAL_assert (val (pos0) >= 0); return; // TODO: maybe fix levels } const int l1 = var (pos1).level; if (val (pos0) < 0) { // conflicting or propagating clause - assert (0 < l1 && l1 <= var (pos0).level); + CADICAL_assert (0 < l1 && l1 <= var (pos0).level); if (!opts.chrono) { backtrack (l1); } @@ -777,7 +781,7 @@ void Internal::handle_external_clause (Clause *res) { // its better to backtrack instead of analyze backtrack (l1 - 1); conflict = 0; - assert (!val (pos0) && !val (pos1)); + CADICAL_assert (!val (pos0) && !val (pos1)); } } else { search_assign_driving (pos0, res); @@ -850,7 +854,7 @@ bool Internal::external_check_solution () { continue; const int lit = external->ival (idx); etrail.push_back (lit); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG #ifdef LOGGING bool p = external->vals[idx]; LOG ("evals[%d]: %d ival(%d): %d", idx, p, idx, lit); @@ -943,14 +947,14 @@ void Internal::notify_assignments () { continue; int elit = externalize (ilit); // TODO: double-check tainting - assert (elit); + CADICAL_assert (elit); if (external->ervars[abs (elit)]) continue; // Fixed variables might get mapped (during compact) to another // non-observed but fixed variable. // This happens on root level, so notification about their assignment is // already done. - assert (external->observed (elit) || + CADICAL_assert (external->observed (elit) || (fixed (ilit) && !external->ervars[abs (elit)])); assigned.push_back (elit); } @@ -995,8 +999,8 @@ int Internal::ask_decision () { if (!external_prop || external_prop_is_lazy || private_steps) return 0; - assert (!unsat); - assert (!conflict); + CADICAL_assert (!unsat); + CADICAL_assert (!conflict); notify_assignments (); int level_before = level; forced_backt_allowed = true; @@ -1007,8 +1011,8 @@ int Internal::ask_decision () { if (level_before != level) { propagate (); - assert (!unsat); - assert (!conflict); + CADICAL_assert (!unsat); + CADICAL_assert (!conflict); notify_assignments (); // In case the external propagator forced to backtrack below the @@ -1023,7 +1027,7 @@ int Internal::ask_decision () { if (!elit) return 0; LOG ("external propagator proposes decision: %d", elit); - assert (external->is_observed[abs (elit)]); + CADICAL_assert (external->is_observed[abs (elit)]); if (!external->is_observed[abs (elit)]) return 0; @@ -1031,7 +1035,7 @@ int Internal::ask_decision () { if (elit < 0) ilit = -ilit; - assert (fixed (ilit) || observed (ilit)); + CADICAL_assert (fixed (ilit) || observed (ilit)); LOG ("Asking external propagator for decision returned: %d (internal: " "%d, fixed: %d, val: %d)", @@ -1052,7 +1056,7 @@ int Internal::ask_decision () { // propagator. // bool Internal::is_external_forgettable (int64_t id) { - assert (opts.check); + CADICAL_assert (opts.check); return (external->forgettable_original.find (id) != external->forgettable_original.end ()); } @@ -1064,8 +1068,8 @@ bool Internal::is_external_forgettable (int64_t id) { // ignore it. // void Internal::mark_garbage_external_forgettable (int64_t id) { - assert (opts.check); - assert (is_external_forgettable (id)); + CADICAL_assert (opts.check); + CADICAL_assert (is_external_forgettable (id)); LOG (external->forgettable_original[id], "forgettable external lemma is deleted:"); @@ -1079,7 +1083,7 @@ void Internal::mark_garbage_external_forgettable (int64_t id) { // internally for debug purposes. // void Internal::check_watched_literal_invariants () { -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG int v0 = 0; int v1 = 0; @@ -1092,19 +1096,19 @@ void Internal::check_watched_literal_invariants () { v1 = 1; else if (val (clause[1]) < 0) v1 = -1; - assert (v0 >= v1); + CADICAL_assert (v0 >= v1); #endif if (val (clause[0]) > 0) { if (val (clause[1]) > 0) { // Case 1: Both literals are satisfied // They are ordered by lower to higher decision level - assert (var (clause[0]).level <= var (clause[1]).level); + CADICAL_assert (var (clause[0]).level <= var (clause[1]).level); // Every other literal of the clause is either // - satisfied at higher level // - unassigned // - falsified for (size_t i = 2; i < clause.size (); i++) - assert (val (clause[i]) <= 0 || + CADICAL_assert (val (clause[i]) <= 0 || (var (clause[1]).level <= var (clause[i]).level)); } else if (val (clause[1]) == @@ -1114,14 +1118,14 @@ void Internal::check_watched_literal_invariants () { // - unassigned // - falsified for (size_t i = 2; i < clause.size (); i++) - assert (val (clause[i]) <= 0); + CADICAL_assert (val (clause[i]) <= 0); } else { // Case 3: First satisfied, next falsified -> could have been a // reason of a previous propagation // Every other literal of the clause is falsified but at a lower // decision level for (size_t i = 2; i < clause.size (); i++) - assert (val (clause[i]) < 0 && + CADICAL_assert (val (clause[i]) < 0 && (var (clause[1]).level >= var (clause[i]).level)); } } else if (val (clause[0]) == 0) { @@ -1131,30 +1135,30 @@ void Internal::check_watched_literal_invariants () { // - unassigned // - falsified for (size_t i = 2; i < clause.size (); i++) - assert (val (clause[i]) <= 0); + CADICAL_assert (val (clause[i]) <= 0); } else { // Case 5: First unassigned, next falsified -> PROPAGATE // Every other literal of the clause is falsified but at a lower // decision level for (size_t i = 2; i < clause.size (); i++) - assert (val (clause[i]) < 0 && + CADICAL_assert (val (clause[i]) < 0 && (var (clause[1]).level >= var (clause[i]).level)); } } else { - assert (val (clause[0]) < 0 && + CADICAL_assert (val (clause[0]) < 0 && val (clause[1]) < 0); // Case 6: Both literals are falsified // They are ordered by higher to lower decision level - assert (var (clause[0]).level >= var (clause[1]).level); + CADICAL_assert (var (clause[0]).level >= var (clause[1]).level); // Every other literal of the clause is falsified, but at a lower level for (size_t i = 2; i < clause.size (); i++) - assert (val (clause[i]) < 0 && + CADICAL_assert (val (clause[i]) < 0 && (var (clause[1]).level >= var (clause[i]).level)); } } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG /*----------------------------------------------------------------------------*/ // @@ -1224,3 +1228,5 @@ void Internal::get_all_fixed_literals (std::vector &fixed_lits) { #endif } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/factor.cpp b/src/sat/cadical/cadical_factor.cpp similarity index 90% rename from src/sat/cadical/factor.cpp rename to src/sat/cadical/cadical_factor.cpp index dbb5a84e5..3fee40685 100644 --- a/src/sat/cadical/factor.cpp +++ b/src/sat/cadical/cadical_factor.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { #define FACTORS 1 @@ -20,7 +24,7 @@ inline bool factor_occs_size::operator() (unsigned a, unsigned b) { void Internal::factor_mode () { reset_watches (); - assert (!watching ()); + CADICAL_assert (!watching ()); init_occs (); const int size_limit = opts.factorsize; @@ -121,9 +125,9 @@ Factoring::Factoring (Internal *i, int64_t l) } Factoring::~Factoring () { - assert (counted.empty ()); - assert (nounted.empty ()); - assert (flauses.empty ()); + CADICAL_assert (counted.empty ()); + CADICAL_assert (nounted.empty ()); + CADICAL_assert (flauses.empty ()); internal->release_quotients (*this); schedule.erase (); // actually not necessary } @@ -143,7 +147,7 @@ double Internal::tied_next_factor_score (int lit) { // use markfact, unmarkfact, getfact for this purpose. // Quotient *Internal::new_quotient (Factoring &factoring, int factor) { - assert (!getfact (factor, FACTORS)); + CADICAL_assert (!getfact (factor, FACTORS)); markfact (factor, FACTORS); Quotient *res = new Quotient (factor); res->next = 0; @@ -151,12 +155,12 @@ Quotient *Internal::new_quotient (Factoring &factoring, int factor) { Quotient *last = factoring.quotients.last; res->bid = 0; if (last) { - assert (factoring.quotients.first); - assert (!last->next); + CADICAL_assert (factoring.quotients.first); + CADICAL_assert (!last->next); last->next = res; res->id = last->id + 1; } else { - assert (!factoring.quotients.first); + CADICAL_assert (!factoring.quotients.first); factoring.quotients.first = res; res->id = 0; } @@ -170,7 +174,7 @@ void Internal::release_quotients (Factoring &factoring) { for (Quotient *q = factoring.quotients.first, *next; q; q = next) { next = q->next; int factor = q->factor; - assert (getfact (factor, FACTORS)); + CADICAL_assert (getfact (factor, FACTORS)); unmarkfact (factor, FACTORS); delete q; } @@ -178,7 +182,7 @@ void Internal::release_quotients (Factoring &factoring) { } size_t Internal::first_factor (Factoring &factoring, int factor) { - assert (!factoring.quotients.first); + CADICAL_assert (!factoring.quotients.first); Quotient *quotient = new_quotient (factoring, factor); vector &qlauses = quotient->qlauses; int64_t ticks = 0; @@ -189,14 +193,14 @@ size_t Internal::first_factor (Factoring &factoring, int factor) { size_t res = qlauses.size (); LOG ("quotient[0] factor %d size %zu", factor, res); // This invariant can of course be broken by previous factorings - // assert (res > 1); + // CADICAL_assert (res > 1); stats.ticks.factor += ticks; return res; } void Internal::clear_nounted (vector &nounted) { for (const auto &lit : nounted) { - assert (getfact (lit, NOUNTED)); + CADICAL_assert (getfact (lit, NOUNTED)); unmarkfact (lit, NOUNTED); } nounted.clear (); @@ -204,7 +208,7 @@ void Internal::clear_nounted (vector &nounted) { void Internal::clear_flauses (vector &flauses) { for (auto c : flauses) { - assert (c->swept); + CADICAL_assert (c->swept); c->swept = false; } flauses.clear (); @@ -248,17 +252,17 @@ Quotient *Internal::best_quotient (Factoring &factoring, int Internal::next_factor (Factoring &factoring, unsigned *next_count_ptr) { Quotient *last_quotient = factoring.quotients.last; - assert (last_quotient); + CADICAL_assert (last_quotient); vector &last_clauses = last_quotient->qlauses; vector &count = factoring.count; vector &counted = factoring.counted; vector &flauses = factoring.flauses; - assert (counted.empty ()); - assert (flauses.empty ()); + CADICAL_assert (counted.empty ()); + CADICAL_assert (flauses.empty ()); const int initial = factoring.initial; int64_t ticks = 1 + cache_lines (last_clauses.size (), sizeof (Clause *)); for (auto c : last_clauses) { - assert (!c->swept); + CADICAL_assert (!c->swept); int min_lit = 0; unsigned factors = 0; size_t min_size = 0; @@ -268,7 +272,7 @@ int Internal::next_factor (Factoring &factoring, unsigned *next_count_ptr) { if (factors++) break; } else { - assert (!getfact (other, QUOTIENT)); + CADICAL_assert (!getfact (other, QUOTIENT)); markfact (other, QUOTIENT); const size_t other_size = occs (other).size (); if (!min_lit || other_size < min_size) { @@ -277,12 +281,12 @@ int Internal::next_factor (Factoring &factoring, unsigned *next_count_ptr) { } } } - assert (factors); + CADICAL_assert (factors); if (factors == 1) { - assert (min_lit); + CADICAL_assert (min_lit); const int c_size = c->size; vector &nounted = factoring.nounted; - assert (nounted.empty ()); + CADICAL_assert (nounted.empty ()); ticks += 1 + cache_lines (occs (min_lit).size (), sizeof (Clause *)); for (auto d : occs (min_lit)) { if (c == d) @@ -304,13 +308,13 @@ int Internal::next_factor (Factoring &factoring, unsigned *next_count_ptr) { goto CONTINUE_WITH_NEXT_MIN_WATCH; next = other; } - assert (next); + CADICAL_assert (next); if (abs (next) > abs (initial)) continue; if (!active (next)) continue; - assert (!getfact (next, FACTORS)); - assert (!getfact (next, NOUNTED)); + CADICAL_assert (!getfact (next, FACTORS)); + CADICAL_assert (!getfact (next, NOUNTED)); markfact (next, NOUNTED); nounted.push_back (next); d->swept = true; @@ -340,10 +344,10 @@ int Internal::next_factor (Factoring &factoring, unsigned *next_count_ptr) { if (lit_count < next_count) continue; if (lit_count == next_count) { - assert (lit_count); + CADICAL_assert (lit_count); ties++; } else { - assert (lit_count > next_count); + CADICAL_assert (lit_count > next_count); next_count = lit_count; next = lit; ties = 1; @@ -361,25 +365,25 @@ int Internal::next_factor (Factoring &factoring, unsigned *next_count_ptr) { if (lit_count != next_count) continue; double lit_score = tied_next_factor_score (lit); - assert (lit_score >= 0); + CADICAL_assert (lit_score >= 0); LOG ("score %g of next factor candidate %d", lit_score, lit); if (lit_score <= next_score) continue; next_score = lit_score; next = lit; } - assert (next_score >= 0); - assert (next); + CADICAL_assert (next_score >= 0); + CADICAL_assert (next); LOG ("best score %g of next factor %d", next_score, next); } else { - assert (ties == 1); + CADICAL_assert (ties == 1); LOG ("single next factor %d with count %u", next, next_count); } } for (const auto &lit : counted) count[vlit (lit)] = 0; counted.clear (); - assert (!next || next_count > 1); + CADICAL_assert (!next || next_count > 1); *next_count_ptr = next_count; return next; } @@ -389,19 +393,19 @@ void Internal::factorize_next (Factoring &factoring, int next, Quotient *last_quotient = factoring.quotients.last; Quotient *next_quotient = new_quotient (factoring, next); - assert (last_quotient); + CADICAL_assert (last_quotient); vector &last_clauses = last_quotient->qlauses; vector &next_clauses = next_quotient->qlauses; vector &matches = next_quotient->matches; vector &flauses = factoring.flauses; - assert (flauses.empty ()); + CADICAL_assert (flauses.empty ()); int64_t ticks = 1 + cache_lines (last_clauses.size (), sizeof (Clause *)); size_t i = 0; for (auto c : last_clauses) { - assert (!c->swept); + CADICAL_assert (!c->swept); int min_lit = 0; unsigned factors = 0; size_t min_size = 0; @@ -411,7 +415,7 @@ void Internal::factorize_next (Factoring &factoring, int next, if (factors++) break; } else { - assert (!getfact (other, QUOTIENT)); + CADICAL_assert (!getfact (other, QUOTIENT)); markfact (other, QUOTIENT); const size_t other_size = occs (other).size (); if (!min_lit || other_size < min_size) { @@ -420,9 +424,9 @@ void Internal::factorize_next (Factoring &factoring, int next, } } } - assert (factors); + CADICAL_assert (factors); if (factors == 1) { - assert (min_lit); + CADICAL_assert (min_lit); const int c_size = c->size; ticks += 1 + cache_lines (occs (min_lit).size (), sizeof (Clause *)); for (auto d : occs (min_lit)) { @@ -459,14 +463,14 @@ void Internal::factorize_next (Factoring &factoring, int next, clear_flauses (flauses); stats.ticks.factor += ticks; - assert (expected_next_count <= next_clauses.size ()); + CADICAL_assert (expected_next_count <= next_clauses.size ()); (void) expected_next_count; } // We only need to enlarge factoring.count as everything else is // initialized in internal void Internal::resize_factoring (Factoring &factoring, int lit) { - assert (lit > 0); + CADICAL_assert (lit > 0); size_t new_var_size = lit + 1; size_t new_lit_size = 2 * new_var_size; enlarge_zero (factoring.count, new_lit_size); @@ -477,13 +481,13 @@ void Internal::flush_unmatched_clauses (Quotient *q) { vector &q_matches = q->matches, &prev_matches = prev->matches; vector &q_clauses = q->qlauses, &prev_clauses = prev->qlauses; const size_t n = q_clauses.size (); - assert (n == q_matches.size ()); + CADICAL_assert (n == q_matches.size ()); bool prev_is_first = !prev->id; size_t i = 0; while (i < q_matches.size ()) { size_t j = q_matches[i]; q_matches[i] = i; - assert (i <= j); + CADICAL_assert (i <= j); if (!prev_is_first) { size_t matches = prev_matches[j]; prev_matches[i] = matches; @@ -506,7 +510,7 @@ void Internal::flush_unmatched_clauses (Quotient *q) { void Internal::add_self_subsuming_factor (Quotient *q, Quotient *p) { const int factor = q->factor; const int not_factor = p->factor; - assert (-factor == not_factor); + CADICAL_assert (-factor == not_factor); LOG ( "adding self subsuming factor because blocked clause is a tautology"); for (auto c : q->qlauses) { @@ -533,7 +537,7 @@ void Internal::add_self_subsuming_factor (Quotient *q, Quotient *p) { } } lrat_chain.push_back (c->id); - assert (lrat_chain.size () == 2); + CADICAL_assert (lrat_chain.size () == 2); } if (clause.size () > 1) { new_factor_clause (); @@ -566,7 +570,7 @@ bool Internal::self_subsuming_factor (Quotient *q) { const int factor = p->factor; Flags &f = flags (factor); if (f.seen) { - assert (std::find (analyzed.begin (), analyzed.end (), -factor) != + CADICAL_assert (std::find (analyzed.begin (), analyzed.end (), -factor) != analyzed.end ()); found = true; x = p; @@ -581,7 +585,7 @@ bool Internal::self_subsuming_factor (Quotient *q) { analyzed.push_back (factor); f.seen = true; } - assert (!found || (x && y)); + CADICAL_assert (!found || (x && y)); clear_analyzed_literals (); if (found) { add_self_subsuming_factor (x, y); @@ -611,11 +615,11 @@ void Internal::blocked_clause (Quotient *q, int not_fresh) { return; int64_t new_id = ++clause_id; q->bid = new_id; - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); for (Quotient *p = q; p; p = p->prev) clause.push_back (-p->factor); clause.push_back (not_fresh); - assert (!lrat || mini_chain.size ()); + CADICAL_assert (!lrat || mini_chain.size ()); proof->add_derived_clause (new_id, true, clause, mini_chain); mini_chain.clear (); clause.clear (); @@ -627,11 +631,11 @@ void Internal::blocked_clause (Quotient *q, int not_fresh) { void Internal::add_factored_quotient (Quotient *q, int not_fresh) { LOG ("adding factored quotient[%zu] clauses", q->id); const int factor = q->factor; - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); auto qlauses = q->qlauses; for (unsigned idx = 0; idx < qlauses.size (); idx++) { const auto c = qlauses[idx]; - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); for (const auto &other : *c) { if (other == factor) { continue; @@ -639,8 +643,8 @@ void Internal::add_factored_quotient (Quotient *q, int not_fresh) { clause.push_back (other); } if (lrat) { - assert (proof); - assert (q->bid); + CADICAL_assert (proof); + CADICAL_assert (q->bid); unsigned idxtoo = idx; for (Quotient *p = q; p; p = p->prev) { lrat_chain.push_back (p->qlauses[idxtoo]->id); @@ -677,7 +681,7 @@ void Internal::eagerly_remove_from_occurences (Clause *c) { if (*q != c) q++; } - assert (q + 1 == p); + CADICAL_assert (q + 1 == p); occ.resize (q - begin); } } @@ -730,7 +734,7 @@ bool Internal::apply_factoring (Factoring &factoring, Quotient *q) { delete_unfactored (p); for (Quotient *p = q; p; p = p->prev) update_factored (factoring, p); - assert (fresh > 0); + CADICAL_assert (fresh > 0); resize_factoring (factoring, fresh); return true; } @@ -758,7 +762,7 @@ void Internal::schedule_factorization (Factoring &factoring) { update_factor_candidate (factoring, not_lit); } } -#ifndef QUIET +#ifndef CADICAL_QUIET size_t size_cands = factoring.schedule.size (); VERBOSE (2, "scheduled %zu factorization candidate literals %.0f %%", size_cands, percent (size_cands, max_var)); @@ -769,7 +773,7 @@ bool Internal::run_factorization (int64_t limit) { Factoring factoring = Factoring (this, limit); schedule_factorization (factoring); bool done = false; -#ifndef QUIET +#ifndef CADICAL_QUIET unsigned factored = 0; #endif int64_t *ticks = &stats.ticks.factor; @@ -804,7 +808,7 @@ bool Internal::run_factorization (int64_t limit) { const int next = next_factor (factoring, &next_count); if (next == 0) break; - assert (next_count > 1); + CADICAL_assert (next_count > 1); if (next_count < 2) break; factorize_next (factoring, next, next_count); @@ -813,7 +817,7 @@ bool Internal::run_factorization (int64_t limit) { Quotient *q = best_quotient (factoring, &reduction); if (q && (int) reduction > factoring.bound) { if (apply_factoring (factoring, q)) { -#ifndef QUIET +#ifndef CADICAL_QUIET factored++; #endif } else @@ -832,7 +836,7 @@ bool Internal::run_factorization (int64_t limit) { } // kissat initializes scores for new variables at this point, however // this is actually done already during resize of internal -#ifndef QUIET +#ifndef CADICAL_QUIET report ('f', !factored); #endif return completed; @@ -848,7 +852,7 @@ int Internal::get_new_extension_variable () { reset_watches (); // it does not enlarge otab, however, so we do this manually init_occs (); - assert (vlit (new_internal)); + CADICAL_assert (vlit (new_internal)); return new_internal; } @@ -859,9 +863,9 @@ bool Internal::factor () { return false; if (!opts.factor) return false; - // The following assertion fails if there are *only* user propagator + // The following CADICAL_assertion fails if there are *only* user propagator // clauses (which are redundant). - // assert (stats.mark.factor || clauses.empty ()); + // CADICAL_assert (stats.mark.factor || clauses.empty ()); if (last.factor.marked >= stats.mark.factor) { VERBOSE (3, "factorization skipped as no literals have been" @@ -869,7 +873,7 @@ bool Internal::factor () { last.factor.marked, stats.mark.factor); return false; } - assert (!level); + CADICAL_assert (!level); SET_EFFORT_LIMIT (limit, factor, stats.factor); if (!stats.factor) @@ -878,7 +882,7 @@ bool Internal::factor () { START_SIMPLIFIER (factor, FACTOR); stats.factor++; -#ifndef QUIET +#ifndef CADICAL_QUIET struct { int64_t variables, clauses, ticks; } before, after, delta; @@ -896,7 +900,7 @@ bool Internal::factor () { learn_empty_clause (); } -#ifndef QUIET +#ifndef CADICAL_QUIET after.variables = stats.variables_extension + stats.variables_original; after.clauses = stats.current.irredundant; after.ticks = stats.ticks.factor; @@ -919,3 +923,5 @@ bool Internal::factor () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/file.cpp b/src/sat/cadical/cadical_file.cpp similarity index 94% rename from src/sat/cadical/file.cpp rename to src/sat/cadical/cadical_file.cpp index 7f8a82dea..98f19f752 100644 --- a/src/sat/cadical/file.cpp +++ b/src/sat/cadical/cadical_file.cpp @@ -1,3 +1,5 @@ +#include "global.h" + #include "internal.hpp" /*------------------------------------------------------------------------*/ @@ -11,13 +13,28 @@ extern "C" { #include #include #include -#include } -#ifndef _WIN32 +#ifdef WIN32 + +#include +#include +#include + +#define access _access +#define popen _popen +#define pclose _pclose +#define R_OK 4 +#define W_OK 2 +#define S_IFIFO _S_IFIFO +#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO) +#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) + +#else extern "C" { #include +#include } #endif @@ -33,6 +50,8 @@ extern "C" { #endif +ABC_NAMESPACE_IMPL_START + /*------------------------------------------------------------------------*/ namespace CaDiCaL { @@ -43,13 +62,13 @@ namespace CaDiCaL { File::File (Internal *i, bool w, int c, int p, FILE *f, const char *n) : internal (i), -#if !defined(QUIET) || !defined(NDEBUG) +#if !defined(CADICAL_QUIET) || !defined(CADICAL_NDEBUG) writing (w), #endif close_file (c), child_pid (p), file (f), _name (strdup (n)), _lineno (1), _bytes (0) { (void) w; - assert (f), assert (n); + CADICAL_assert (f), CADICAL_assert (n); } /*------------------------------------------------------------------------*/ @@ -129,7 +148,7 @@ static int sig7z[] = {0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C, EOF}; static int lzmasig[] = {0x5D, EOF}; bool File::match (Internal *internal, const char *path, const int *sig) { - assert (path); + CADICAL_assert (path); FILE *tmp = fopen (path, "r"); if (!tmp) { WARNING ("failed to open '%s' to check signature", path); @@ -170,7 +189,7 @@ char *File::find_program (const char *prg) { size_t pathlen = (q - p) + prglen; char *path = new char[pathlen + 1]; snprintf (path, pathlen + 1, "%s/%s", p, prg); - assert (strlen (path) == pathlen); + CADICAL_assert (strlen (path) == pathlen); if (exists (path)) res = path; else @@ -226,7 +245,7 @@ void File::delete_str_vector (std::vector &argv) { FILE *File::open_pipe (Internal *internal, const char *fmt, const char *path, const char *mode) { -#ifdef QUIET +#ifdef CADICAL_QUIET (void) internal; #endif size_t prglen = 0; @@ -274,14 +293,14 @@ static std::mutex compressed_file_writing_mutex; FILE *File::write_pipe (Internal *internal, const char *command, const char *path, int &child_pid) { - assert (command[0] && command[0] != ' '); + CADICAL_assert (command[0] && command[0] != ' '); MSG ("writing through command '%s' to '%s'", command, path); -#ifdef QUIET +#ifdef CADICAL_QUIET (void) internal; #endif std::vector args; split_str (command, args); - assert (!args.empty ()); + CADICAL_assert (!args.empty ()); args.push_back (0); char **argv = args.data (); char *absolute_command_path = find_program (argv[0]); @@ -328,7 +347,7 @@ FILE *File::write_pipe (Internal *internal, const char *command, // be closed by the parent process we have to close all of the // erroneously cloned fds here. -#ifndef NCLOSEFROM +#ifndef CADICAL_NCLOSEFROM ::closefrom (3); #else // Simplistic replacement on Unix without 'closefrom'. @@ -341,7 +360,7 @@ FILE *File::write_pipe (Internal *internal, const char *command, if (absolute_command_path) delete[] absolute_command_path; delete_str_vector (args); -#ifdef QUIET +#ifdef CADICAL_QUIET (void) internal; #endif #if defined(__APPLE__) || defined(__MACH__) @@ -421,8 +440,8 @@ File *File::write (Internal *internal, const char *path) { } void File::close (bool print) { - assert (file); -#ifndef QUIET + CADICAL_assert (file); +#ifndef CADICAL_QUIET if (internal->opts.quiet) print = false; else if (internal->opts.verbose > 0) @@ -457,7 +476,7 @@ void File::close (bool print) { // TODO what about error checking for 'fclose', 'pclose' or 'waitpid'? -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { if (writing) { uint64_t written_bytes = bytes (); @@ -493,7 +512,7 @@ void File::close (bool print) { } void File::flush () { - assert (file); + CADICAL_assert (file); fflush (file); } @@ -504,3 +523,5 @@ File::~File () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/flags.cpp b/src/sat/cadical/cadical_flags.cpp similarity index 64% rename from src/sat/cadical/flags.cpp rename to src/sat/cadical/cadical_flags.cpp index de7e13b4f..5c06a33b4 100644 --- a/src/sat/cadical/flags.cpp +++ b/src/sat/cadical/cadical_flags.cpp @@ -1,104 +1,108 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void Internal::mark_fixed (int lit) { if (external->fixed_listener) { int elit = externalize (lit); - assert (elit); + CADICAL_assert (elit); const int eidx = abs (elit); if (!external->ervars[eidx]) external->fixed_listener->notify_fixed_assignment (elit); } Flags &f = flags (lit); - assert (f.status == Flags::ACTIVE); + CADICAL_assert (f.status == Flags::ACTIVE); f.status = Flags::FIXED; LOG ("fixed %d", abs (lit)); stats.all.fixed++; stats.now.fixed++; stats.inactive++; - assert (stats.active); + CADICAL_assert (stats.active); stats.active--; - assert (!active (lit)); - assert (f.fixed ()); + CADICAL_assert (!active (lit)); + CADICAL_assert (f.fixed ()); if (external_prop && private_steps) { // If pre/inprocessing found a fixed assignment, we want the propagator // to know about it. // But at that point it is not guaranteed to be already on the trail, so // notification will happen only later. - assert (!level); + CADICAL_assert (!level); } } void Internal::mark_eliminated (int lit) { Flags &f = flags (lit); - assert (f.status == Flags::ACTIVE); + CADICAL_assert (f.status == Flags::ACTIVE); f.status = Flags::ELIMINATED; LOG ("eliminated %d", abs (lit)); stats.all.eliminated++; stats.now.eliminated++; stats.inactive++; - assert (stats.active); + CADICAL_assert (stats.active); stats.active--; - assert (!active (lit)); - assert (f.eliminated ()); + CADICAL_assert (!active (lit)); + CADICAL_assert (f.eliminated ()); } void Internal::mark_pure (int lit) { Flags &f = flags (lit); - assert (f.status == Flags::ACTIVE); + CADICAL_assert (f.status == Flags::ACTIVE); f.status = Flags::PURE; LOG ("pure %d", abs (lit)); stats.all.pure++; stats.now.pure++; stats.inactive++; - assert (stats.active); + CADICAL_assert (stats.active); stats.active--; - assert (!active (lit)); - assert (f.pure ()); + CADICAL_assert (!active (lit)); + CADICAL_assert (f.pure ()); } void Internal::mark_substituted (int lit) { Flags &f = flags (lit); - assert (f.status == Flags::ACTIVE); + CADICAL_assert (f.status == Flags::ACTIVE); f.status = Flags::SUBSTITUTED; LOG ("substituted %d", abs (lit)); stats.all.substituted++; stats.now.substituted++; stats.inactive++; - assert (stats.active); + CADICAL_assert (stats.active); stats.active--; - assert (!active (lit)); - assert (f.substituted ()); + CADICAL_assert (!active (lit)); + CADICAL_assert (f.substituted ()); } void Internal::mark_active (int lit) { Flags &f = flags (lit); - assert (f.status == Flags::UNUSED); + CADICAL_assert (f.status == Flags::UNUSED); f.status = Flags::ACTIVE; LOG ("activate %d previously unused", abs (lit)); - assert (stats.inactive); + CADICAL_assert (stats.inactive); stats.inactive--; - assert (stats.unused); + CADICAL_assert (stats.unused); stats.unused--; stats.active++; - assert (active (lit)); + CADICAL_assert (active (lit)); } void Internal::reactivate (int lit) { - assert (!active (lit)); + CADICAL_assert (!active (lit)); Flags &f = flags (lit); - assert (f.status != Flags::FIXED); - assert (f.status != Flags::UNUSED); + CADICAL_assert (f.status != Flags::FIXED); + CADICAL_assert (f.status != Flags::UNUSED); #ifdef LOGGING const char *msg = 0; #endif switch (f.status) { default: case Flags::ELIMINATED: - assert (f.status == Flags::ELIMINATED); - assert (stats.now.eliminated > 0); + CADICAL_assert (f.status == Flags::ELIMINATED); + CADICAL_assert (stats.now.eliminated > 0); stats.now.eliminated--; #ifdef LOGGING msg = "eliminated"; @@ -108,28 +112,30 @@ void Internal::reactivate (int lit) { #ifdef LOGGING msg = "substituted"; #endif - assert (stats.now.substituted > 0); + CADICAL_assert (stats.now.substituted > 0); stats.now.substituted--; break; case Flags::PURE: #ifdef LOGGING msg = "pure literal"; #endif - assert (stats.now.pure > 0); + CADICAL_assert (stats.now.pure > 0); stats.now.pure--; break; } #ifdef LOGGING - assert (msg); + CADICAL_assert (msg); LOG ("reactivate previously %s %d", msg, abs (lit)); #endif f.status = Flags::ACTIVE; f.sweep = false; - assert (active (lit)); + CADICAL_assert (active (lit)); stats.reactivated++; - assert (stats.inactive > 0); + CADICAL_assert (stats.inactive > 0); stats.inactive--; stats.active++; } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/flip.cpp b/src/sat/cadical/cadical_flip.cpp similarity index 86% rename from src/sat/cadical/flip.cpp rename to src/sat/cadical/cadical_flip.cpp index 0df508246..a89e3abb7 100644 --- a/src/sat/cadical/flip.cpp +++ b/src/sat/cadical/cadical_flip.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { bool Internal::flip (int lit) { @@ -11,7 +15,7 @@ bool Internal::flip (int lit) { /* if (flags (lit).unused ()) { - assert (lit <= max_var); + CADICAL_assert (lit <= max_var); mark_active (lit); set_val (lit, 1); return true; @@ -31,9 +35,9 @@ bool Internal::flip (int lit) { const int idx = vidx (lit); const signed char original_value = vals[idx]; - assert (original_value); + CADICAL_assert (original_value); lit = original_value < 0 ? -idx : idx; - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); // Here we go over all the clauses in which 'lit' is watched by 'lit' and // check whether assigning 'lit' to false would break watching invariants @@ -57,7 +61,7 @@ bool Internal::flip (int lit) { const signed char b = val (w.blit); if (b > 0) continue; - assert (b < 0); + CADICAL_assert (b < 0); res = false; break; } @@ -96,7 +100,7 @@ bool Internal::flip (int lit) { k++; if (v < 0) { k = lits + 2; - assert (w.clause->pos <= size); + CADICAL_assert (w.clause->pos <= size); while (k != middle && (v = val (r = *k)) < 0) k++; } @@ -106,8 +110,8 @@ bool Internal::flip (int lit) { break; } - assert (v > 0); - assert (lits + 2 <= k), assert (k <= w.clause->end ()); + CADICAL_assert (v > 0); + CADICAL_assert (lits + 2 <= k), CADICAL_assert (k <= w.clause->end ()); w.clause->pos = k - lits; lits[0] = other, lits[1] = r, *k = lit; watch_literal (r, lit, w.clause); @@ -133,24 +137,24 @@ bool Internal::flip (int lit) { const int idx = vidx (lit); const signed char original_value = vals[idx]; - assert (original_value); + CADICAL_assert (original_value); lit = original_value < 0 ? -idx : idx; - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); LOG ("flipping value of %d = 1 to %d = -1", lit, lit); set_val (idx, -original_value); - assert (val (-lit) > 0); - assert (val (lit) < 0); + CADICAL_assert (val (-lit) > 0); + CADICAL_assert (val (lit) < 0); Var &v = var (idx); - assert (trail[v.trail] == lit); + CADICAL_assert (trail[v.trail] == lit); trail[v.trail] = -lit; if (opts.ilb) { if (!tainted_literal) tainted_literal = lit; else { - assert (val (tainted_literal)); + CADICAL_assert (val (tainted_literal)); if (v.level < var (tainted_literal).level) { tainted_literal = lit; } @@ -171,7 +175,7 @@ bool Internal::flippable (int lit) { /* if (flags (lit).unused ()) { - assert (lit <= max_var); + CADICAL_assert (lit <= max_var); mark_active (lit); return true; } @@ -189,9 +193,9 @@ bool Internal::flippable (int lit) { const int idx = vidx (lit); const signed char original_value = vals[idx]; - assert (original_value); + CADICAL_assert (original_value); lit = original_value < 0 ? -idx : idx; - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); // Here we go over all the clauses in which 'lit' is watched by 'lit' and // check whether assigning 'lit' to false would break watching invariants @@ -209,7 +213,7 @@ bool Internal::flippable (int lit) { const signed char b = val (w.blit); if (b > 0) continue; - assert (b < 0); + CADICAL_assert (b < 0); if (w.binary ()) { res = false; @@ -239,7 +243,7 @@ bool Internal::flippable (int lit) { k++; if (v < 0) { k = lits + 2; - assert (w.clause->pos <= size); + CADICAL_assert (w.clause->pos <= size); while (k != middle && (v = val (r = *k)) < 0) k++; } @@ -249,9 +253,9 @@ bool Internal::flippable (int lit) { break; } - assert (v > 0); - assert (lits + 2 <= k); - assert (k <= w.clause->end ()); + CADICAL_assert (v > 0); + CADICAL_assert (lits + 2 <= k); + CADICAL_assert (k <= w.clause->end ()); w.clause->pos = k - lits; i->blit = r; } @@ -267,3 +271,5 @@ bool Internal::flippable (int lit) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/format.cpp b/src/sat/cadical/cadical_format.cpp similarity index 94% rename from src/sat/cadical/format.cpp rename to src/sat/cadical/cadical_format.cpp index 87421a0fb..942d9d74a 100644 --- a/src/sat/cadical/format.cpp +++ b/src/sat/cadical/cadical_format.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void Format::enlarge () { @@ -34,7 +38,7 @@ void Format::push_uint64 (uint64_t u) { } static bool match_format (const char *&str, const char *pattern) { - assert (pattern); + CADICAL_assert (pattern); const char *p = str; const char *q = pattern; while (*q) @@ -87,3 +91,5 @@ const char *Format::append (const char *fmt, ...) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/frattracer.cpp b/src/sat/cadical/cadical_frattracer.cpp similarity index 93% rename from src/sat/cadical/frattracer.cpp rename to src/sat/cadical/cadical_frattracer.cpp index 4f9193b42..d35a18271 100644 --- a/src/sat/cadical/frattracer.cpp +++ b/src/sat/cadical/cadical_frattracer.cpp @@ -1,12 +1,16 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ FratTracer::FratTracer (Internal *i, File *f, bool b, bool a) : internal (i), file (f), binary (b), with_antecedents (a) -#ifndef QUIET +#ifndef CADICAL_QUIET , added (0), deleted (0), finalized (0), original (0) #endif @@ -28,15 +32,15 @@ FratTracer::~FratTracer () { /*------------------------------------------------------------------------*/ inline void FratTracer::put_binary_zero () { - assert (binary); - assert (file); + CADICAL_assert (binary); + CADICAL_assert (file); file->put ((unsigned char) 0); } inline void FratTracer::put_binary_lit (int lit) { - assert (binary); - assert (file); - assert (lit != INT_MIN); + CADICAL_assert (binary); + CADICAL_assert (file); + CADICAL_assert (lit != INT_MIN); unsigned x = 2 * abs (lit) + (lit < 0); unsigned char ch; while (x & ~0x7f) { @@ -49,8 +53,8 @@ inline void FratTracer::put_binary_lit (int lit) { } inline void FratTracer::put_binary_id (int64_t id, bool can_be_negative) { - assert (binary); - assert (file); + CADICAL_assert (binary); + CADICAL_assert (file); uint64_t x = abs (id); if (can_be_negative) { x = 2 * x + (id < 0); @@ -202,7 +206,7 @@ void FratTracer::add_derived_clause (int64_t id, bool, frat_add_derived_clause (id, clause, chain); else frat_add_derived_clause (id, clause); -#ifndef QUIET +#ifndef CADICAL_QUIET added++; #endif } @@ -213,7 +217,7 @@ void FratTracer::delete_clause (int64_t id, bool, return; LOG ("FRAT TRACER tracing deletion of clause"); frat_delete_clause (id, clause); -#ifndef QUIET +#ifndef CADICAL_QUIET deleted++; #endif } @@ -229,7 +233,7 @@ void FratTracer::finalize_clause (int64_t id, const vector &clause) { bool FratTracer::closed () { return file->closed (); } -#ifndef QUIET +#ifndef CADICAL_QUIET void FratTracer::print_statistics () { uint64_t bytes = file->bytes (); @@ -249,9 +253,9 @@ void FratTracer::print_statistics () { #endif void FratTracer::close (bool print) { - assert (!closed ()); + CADICAL_assert (!closed ()); file->close (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { MSG ("FRAT proof file '%s' closed", file->name ()); print_statistics (); @@ -262,9 +266,9 @@ void FratTracer::close (bool print) { } void FratTracer::flush (bool print) { - assert (!closed ()); + CADICAL_assert (!closed ()); file->flush (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { MSG ("FRAT proof file '%s' flushed", file->name ()); print_statistics (); @@ -275,3 +279,5 @@ void FratTracer::flush (bool print) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/gates.cpp b/src/sat/cadical/cadical_gates.cpp similarity index 92% rename from src/sat/cadical/gates.cpp rename to src/sat/cadical/cadical_gates.cpp index 78c6d0a09..06a73d1eb 100644 --- a/src/sat/cadical/gates.cpp +++ b/src/sat/cadical/cadical_gates.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -19,7 +23,7 @@ namespace CaDiCaL { int Internal::second_literal_in_binary_clause (Eliminator &eliminator, Clause *c, int first) { - assert (!c->garbage); + CADICAL_assert (!c->garbage); int second = 0; for (const auto &lit : *c) { if (lit == first) @@ -42,7 +46,7 @@ int Internal::second_literal_in_binary_clause (Eliminator &eliminator, return 0; if (second == INT_MIN) return 0; - assert (active (second)); + CADICAL_assert (active (second)); #ifdef LOGGING if (c->size == 2) LOG (c, "found binary"); @@ -115,8 +119,8 @@ void Internal::mark_binary_literals (Eliminator &eliminator, int first) { if (!eliminator.gates.empty ()) return; - assert (!marked (first)); - assert (eliminator.marked.empty ()); + CADICAL_assert (!marked (first)); + CADICAL_assert (eliminator.marked.empty ()); const Occs &os = occs (first); for (const auto &c : os) { @@ -133,11 +137,11 @@ void Internal::mark_binary_literals (Eliminator &eliminator, int first) { LOG ("found binary resolved unit %d", first); if (lrat) { Clause *d = find_binary_clause (first, -second); - assert (d); + CADICAL_assert (d); for (auto &lit : *d) { if (lit == first || lit == -second) continue; - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); Flags &f = flags (lit); if (f.seen) continue; @@ -150,7 +154,7 @@ void Internal::mark_binary_literals (Eliminator &eliminator, int first) { for (auto &lit : *c) { if (lit == first || lit == second) continue; - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); Flags &f = flags (lit); if (f.seen) continue; @@ -202,7 +206,7 @@ void Internal::find_equivalence (Eliminator &eliminator, int pivot) { if (!opts.elimequivs) return; - assert (opts.elimsubst); + CADICAL_assert (opts.elimsubst); if (unsat) return; @@ -231,11 +235,11 @@ void Internal::find_equivalence (Eliminator &eliminator, int pivot) { // still solved potential issues with flags if (lrat) { Clause *d = find_binary_clause (pivot, second); - assert (d); + CADICAL_assert (d); for (auto &lit : *d) { if (lit == pivot || lit == second) continue; - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); Flags &f = flags (lit); if (f.seen) continue; @@ -248,7 +252,7 @@ void Internal::find_equivalence (Eliminator &eliminator, int pivot) { for (auto &lit : *c) { if (lit == -pivot || lit == second) continue; - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); Flags &f = flags (lit); if (f.seen) continue; @@ -279,7 +283,7 @@ void Internal::find_equivalence (Eliminator &eliminator, int pivot) { stats.elimgates++; LOG (c, "first gate clause"); - assert (!c->gate); + CADICAL_assert (!c->gate); c->gate = true; eliminator.gates.push_back (c); @@ -295,10 +299,10 @@ void Internal::find_equivalence (Eliminator &eliminator, int pivot) { break; } } - assert (d); + CADICAL_assert (d); LOG (d, "second gate clause"); - assert (!d->gate); + CADICAL_assert (!d->gate); d->gate = true; eliminator.gates.push_back (d); eliminator.gatetype = EQUI; @@ -321,7 +325,7 @@ void Internal::find_and_gate (Eliminator &eliminator, int pivot) { if (!opts.elimands) return; - assert (opts.elimsubst); + CADICAL_assert (opts.elimsubst); if (unsat) return; @@ -348,7 +352,7 @@ void Internal::find_and_gate (Eliminator &eliminator, int pivot) { for (const auto &lit : *c) { if (lit == -pivot) continue; - assert (lit != pivot); + CADICAL_assert (lit != pivot); signed char tmp = val (lit); if (tmp < 0) continue; @@ -383,7 +387,7 @@ void Internal::find_and_gate (Eliminator &eliminator, int pivot) { for (const auto &lit : *c) { if (lit == -pivot) continue; - assert (lit != pivot); + CADICAL_assert (lit != pivot); if (!first) fputs (" & ", stdout); printf ("%d", -lit); @@ -399,18 +403,18 @@ void Internal::find_and_gate (Eliminator &eliminator, int pivot) { eliminator.gatetype = AND; (void) arity; - assert (!c->gate); + CADICAL_assert (!c->gate); c->gate = true; eliminator.gates.push_back (c); for (const auto &lit : *c) { if (lit == -pivot) continue; - assert (lit != pivot); + CADICAL_assert (lit != pivot); signed char tmp = val (lit); if (tmp < 0) continue; - assert (!tmp); - assert (marked (lit) < 0); + CADICAL_assert (!tmp); + CADICAL_assert (marked (lit) < 0); marks[vidx (lit)] *= 2; } @@ -426,12 +430,12 @@ void Internal::find_and_gate (Eliminator &eliminator, int pivot) { if (tmp != 2) continue; LOG (d, "AND gate binary side clause"); - assert (!d->gate); + CADICAL_assert (!d->gate); d->gate = true; eliminator.gates.push_back (d); count++; } - assert (count >= arity); + CADICAL_assert (count >= arity); (void) count; break; @@ -503,7 +507,7 @@ void Internal::find_if_then_else (Eliminator &eliminator, int pivot) { if (!opts.elimites) return; - assert (opts.elimsubst); + CADICAL_assert (opts.elimsubst); if (unsat) return; @@ -523,7 +527,7 @@ void Internal::find_if_then_else (Eliminator &eliminator, int pivot) { swap (ai, bi); if (ci == pivot) swap (ai, ci); - assert (ai == pivot); + CADICAL_assert (ai == pivot); for (auto j = i + 1; j != end; j++) { Clause *dj = *j; int aj, bj, cj; @@ -533,7 +537,7 @@ void Internal::find_if_then_else (Eliminator &eliminator, int pivot) { swap (aj, bj); if (cj == pivot) swap (aj, cj); - assert (aj == pivot); + CADICAL_assert (aj == pivot); if (abs (bi) == abs (cj)) swap (bj, cj); if (abs (ci) == abs (cj)) @@ -551,10 +555,10 @@ void Internal::find_if_then_else (Eliminator &eliminator, int pivot) { LOG (d1, "3rd if-then-else"); LOG (d2, "4th if-then-else"); LOG ("found ITE gate %d == (%d ? %d : %d)", pivot, -bi, -ci, -cj); - assert (!di->gate); - assert (!dj->gate); - assert (!d1->gate); - assert (!d2->gate); + CADICAL_assert (!di->gate); + CADICAL_assert (!dj->gate); + CADICAL_assert (!d1->gate); + CADICAL_assert (!d2->gate); di->gate = true; dj->gate = true; d1->gate = true; @@ -634,7 +638,7 @@ void Internal::find_xor_gate (Eliminator &eliminator, int pivot) { if (!opts.elimxors) return; - assert (opts.elimsubst); + CADICAL_assert (opts.elimsubst); if (unsat) return; @@ -658,7 +662,7 @@ void Internal::find_xor_gate (Eliminator &eliminator, int pivot) { if (arity > opts.elimxorlim) continue; - assert (eliminator.gates.empty ()); + CADICAL_assert (eliminator.gates.empty ()); unsigned needed = (1u << arity) - 1; // additional clauses unsigned signs = 0; // literals to negate @@ -685,7 +689,7 @@ void Internal::find_xor_gate (Eliminator &eliminator, int pivot) { } eliminator.gates.push_back (d); - assert (eliminator.gates.size () == (1u << arity)); + CADICAL_assert (eliminator.gates.size () == (1u << arity)); #ifdef LOGGING if (opts.log) { @@ -696,7 +700,7 @@ void Internal::find_xor_gate (Eliminator &eliminator, int pivot) { for (const auto &lit : *d) { if (lit == pivot) continue; - assert (lit != -pivot); + CADICAL_assert (lit != -pivot); if (!first) fputs (" ^ ", stdout); printf ("%d", lit); @@ -741,7 +745,7 @@ void Internal::find_gate_clauses (Eliminator &eliminator, int pivot) { if (val (pivot)) return; - assert (eliminator.gates.empty ()); + CADICAL_assert (eliminator.gates.empty ()); find_equivalence (eliminator, pivot); find_and_gate (eliminator, pivot); @@ -754,7 +758,7 @@ void Internal::find_gate_clauses (Eliminator &eliminator, int pivot) { void Internal::unmark_gate_clauses (Eliminator &eliminator) { LOG ("unmarking %zd gate clauses", eliminator.gates.size ()); for (const auto &c : eliminator.gates) { - assert (c->gate); + CADICAL_assert (c->gate); c->gate = false; } eliminator.gates.clear (); @@ -764,3 +768,5 @@ void Internal::unmark_gate_clauses (Eliminator &eliminator) { /*------------------------------------------------------------------------*/ } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/idruptracer.cpp b/src/sat/cadical/cadical_idruptracer.cpp similarity index 91% rename from src/sat/cadical/idruptracer.cpp rename to src/sat/cadical/cadical_idruptracer.cpp index f4cc0f3f6..ec6467163 100644 --- a/src/sat/cadical/idruptracer.cpp +++ b/src/sat/cadical/cadical_idruptracer.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -7,7 +11,7 @@ namespace CaDiCaL { IdrupTracer::IdrupTracer (Internal *i, File *f, bool b) : internal (i), file (f), binary (b), num_clauses (0), size_clauses (0), clauses (0), last_hash (0), last_id (0), last_clause (0) -#ifndef QUIET +#ifndef CADICAL_QUIET , added (0), deleted (0) #endif @@ -21,10 +25,10 @@ IdrupTracer::IdrupTracer (Internal *i, File *f, bool b) uint64_t nonce = random.next (); if (!(nonce & 1)) nonce++; - assert (nonce), assert (nonce & 1); + CADICAL_assert (nonce), CADICAL_assert (nonce & 1); nonces[n] = nonce; } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG binary = b; #else (void) b; @@ -50,7 +54,7 @@ IdrupTracer::~IdrupTracer () { /*------------------------------------------------------------------------*/ void IdrupTracer::enlarge_clauses () { - assert (num_clauses == size_clauses); + CADICAL_assert (num_clauses == size_clauses); const uint64_t new_size_clauses = size_clauses ? 2 * size_clauses : 1; LOG ("IDRUP Tracer enlarging clauses of tracer from %" PRIu64 " to %" PRIu64, @@ -73,7 +77,7 @@ void IdrupTracer::enlarge_clauses () { IdrupClause *IdrupTracer::new_clause () { const size_t size = imported_clause.size (); - assert (size <= UINT_MAX); + CADICAL_assert (size <= UINT_MAX); const int off = size ? -1 : 0; const size_t bytes = sizeof (IdrupClause) + (size - off) * sizeof (int); IdrupClause *res = (IdrupClause *) new char[bytes]; @@ -91,13 +95,13 @@ IdrupClause *IdrupTracer::new_clause () { } void IdrupTracer::delete_clause (IdrupClause *c) { - assert (c); + CADICAL_assert (c); num_clauses--; delete[] (char *) c; } uint64_t IdrupTracer::reduce_hash (uint64_t hash, uint64_t size) { - assert (size > 0); + CADICAL_assert (size > 0); unsigned shift = 32; uint64_t res = hash; while ((((uint64_t) 1) << shift) > size) { @@ -105,12 +109,12 @@ uint64_t IdrupTracer::reduce_hash (uint64_t hash, uint64_t size) { shift >>= 1; } res &= size - 1; - assert (res < size); + CADICAL_assert (res < size); return res; } uint64_t IdrupTracer::compute_hash (const int64_t id) { - assert (id > 0); + CADICAL_assert (id > 0); unsigned j = id % num_nonces; uint64_t tmp = nonces[j] * (uint64_t) id; return last_hash = tmp; @@ -131,7 +135,7 @@ bool IdrupTracer::find_and_delete (const int64_t id) { } if (!c) return false; - assert (c && res); + CADICAL_assert (c && res); *res = c->next; int *begin = c->literals; for (size_t i = 0; i < c->size; i++) { @@ -158,15 +162,15 @@ inline void IdrupTracer::flush_if_piping () { } inline void IdrupTracer::put_binary_zero () { - assert (binary); - assert (file); + CADICAL_assert (binary); + CADICAL_assert (file); file->put ((unsigned char) 0); } inline void IdrupTracer::put_binary_lit (int lit) { - assert (binary); - assert (file); - assert (lit != INT_MIN); + CADICAL_assert (binary); + CADICAL_assert (file); + CADICAL_assert (lit != INT_MIN); unsigned x = 2 * abs (lit) + (lit < 0); unsigned char ch; while (x & ~0x7f) { @@ -179,8 +183,8 @@ inline void IdrupTracer::put_binary_lit (int lit) { } inline void IdrupTracer::put_binary_id (int64_t id, bool can_be_negative) { - assert (binary); - assert (file); + CADICAL_assert (binary); + CADICAL_assert (file); uint64_t x = abs (id); if (can_be_negative) { x = 2 * x + (id < 0); @@ -251,12 +255,12 @@ void IdrupTracer::idrup_add_original_clause (const vector &clause) { void IdrupTracer::idrup_delete_clause (int64_t id, const vector &clause) { if (find_and_delete (id)) { - assert (imported_clause.empty ()); + CADICAL_assert (imported_clause.empty ()); if (binary) file->put ('w'); else file->put ("w "); -#ifndef QUIET +#ifndef CADICAL_QUIET weakened++; #endif } else { @@ -264,7 +268,7 @@ void IdrupTracer::idrup_delete_clause (int64_t id, file->put ('d'); else file->put ("d "); -#ifndef QUIET +#ifndef CADICAL_QUIET deleted++; #endif } @@ -392,10 +396,10 @@ void IdrupTracer::add_derived_clause (int64_t, bool, const vector &) { if (file->closed ()) return; - assert (imported_clause.empty ()); + CADICAL_assert (imported_clause.empty ()); LOG (clause, "IDRUP TRACER tracing addition of derived clause"); idrup_add_derived_clause (clause); -#ifndef QUIET +#ifndef CADICAL_QUIET added++; #endif } @@ -405,7 +409,7 @@ void IdrupTracer::add_assumption_clause (int64_t id, const vector &) { if (file->closed ()) return; - assert (imported_clause.empty ()); + CADICAL_assert (imported_clause.empty ()); LOG (clause, "IDRUP TRACER tracing addition of assumption clause"); for (auto &lit : clause) imported_clause.push_back (lit); @@ -418,7 +422,7 @@ void IdrupTracer::delete_clause (int64_t id, bool, const vector &clause) { if (file->closed ()) return; - assert (imported_clause.empty ()); + CADICAL_assert (imported_clause.empty ()); LOG ("IDRUP TRACER tracing deletion of clause[%" PRId64 "]", id); idrup_delete_clause (id, clause); } @@ -426,11 +430,11 @@ void IdrupTracer::delete_clause (int64_t id, bool, void IdrupTracer::weaken_minus (int64_t id, const vector &) { if (file->closed ()) return; - assert (imported_clause.empty ()); + CADICAL_assert (imported_clause.empty ()); LOG ("IDRUP TRACER tracing weaken minus of clause[%" PRId64 "]", id); last_id = id; insert (); -#ifndef QUIET +#ifndef CADICAL_QUIET weakened++; #endif } @@ -439,7 +443,7 @@ void IdrupTracer::conclude_unsat (ConclusionType, const vector &conclusion) { if (file->closed ()) return; - assert (imported_clause.empty ()); + CADICAL_assert (imported_clause.empty ()); LOG (conclusion, "IDRUP TRACER tracing conclusion of clause(s)"); idrup_conclude_and_delete (conclusion); } @@ -451,12 +455,12 @@ void IdrupTracer::add_original_clause (int64_t id, bool, return; if (!restored) { LOG (clause, "IDRUP TRACER tracing addition of original clause"); -#ifndef QUIET +#ifndef CADICAL_QUIET original++; #endif return idrup_add_original_clause (clause); } - assert (restored); + CADICAL_assert (restored); if (find_and_delete (id)) { LOG (clause, "IDRUP TRACER the clause was not yet weakened, so no restore"); @@ -464,7 +468,7 @@ void IdrupTracer::add_original_clause (int64_t id, bool, } LOG (clause, "IDRUP TRACER tracing addition of restored clause"); idrup_add_restored_clause (clause); -#ifndef QUIET +#ifndef CADICAL_QUIET restore++; #endif } @@ -495,7 +499,7 @@ void IdrupTracer::solve_query () { return; LOG (assumptions, "IDRUP TRACER tracing solve query with assumptions"); idrup_solve_query (); -#ifndef QUIET +#ifndef CADICAL_QUIET solved++; #endif } @@ -514,7 +518,7 @@ void IdrupTracer::reset_assumptions () { bool IdrupTracer::closed () { return file->closed (); } -#ifndef QUIET +#ifndef CADICAL_QUIET void IdrupTracer::print_statistics () { // TODO complete this. @@ -538,9 +542,9 @@ void IdrupTracer::print_statistics () { #endif void IdrupTracer::close (bool print) { - assert (!closed ()); + CADICAL_assert (!closed ()); file->close (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { MSG ("IDRUP proof file '%s' closed", file->name ()); print_statistics (); @@ -551,9 +555,9 @@ void IdrupTracer::close (bool print) { } void IdrupTracer::flush (bool print) { - assert (!closed ()); + CADICAL_assert (!closed ()); file->flush (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { MSG ("IDRUP proof file '%s' flushed", file->name ()); print_statistics (); @@ -564,3 +568,5 @@ void IdrupTracer::flush (bool print) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/instantiate.cpp b/src/sat/cadical/cadical_instantiate.cpp similarity index 89% rename from src/sat/cadical/instantiate.cpp rename to src/sat/cadical/cadical_instantiate.cpp index 494686b81..2c34015b2 100644 --- a/src/sat/cadical/instantiate.cpp +++ b/src/sat/cadical/cadical_instantiate.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -13,7 +17,7 @@ namespace CaDiCaL { void Internal::collect_instantiation_candidates ( Instantiator &instantiator) { - assert (occurring ()); + CADICAL_assert (occurring ()); for (auto idx : vars) { if (frozen (idx)) continue; @@ -63,8 +67,8 @@ void Internal::collect_instantiation_candidates ( inline void Internal::inst_assign (int lit) { LOG ("instantiate assign %d", lit); - assert (!val (lit)); - assert ((int) num_assigned < max_var); + CADICAL_assert (!val (lit)); + CADICAL_assert ((int) num_assigned < max_var); num_assigned++; set_val (lit, 1); trail.push_back (lit); @@ -123,12 +127,12 @@ bool Internal::inst_propagate () { // Adapted from 'propagate'. k++; if (v < 0) { k = lits + 2; - assert (w.clause->pos <= size); + CADICAL_assert (w.clause->pos <= size); while (k != middle && (v = val (r = *k)) < 0) k++; } w.clause->pos = k - lits; - assert (lits + 2 <= k), assert (k <= w.clause->end ()); + CADICAL_assert (lits + 2 <= k), CADICAL_assert (k <= w.clause->end ()); if (v > 0) { j[-1].blit = r; } else if (!v) { @@ -138,14 +142,14 @@ bool Internal::inst_propagate () { // Adapted from 'propagate'. watch_literal (r, lit, w.clause); j--; } else if (!u) { - assert (v < 0); + CADICAL_assert (v < 0); if (lrat) { inst_chain.push_back (w.clause); } inst_assign (other); } else { - assert (u < 0); - assert (v < 0); + CADICAL_assert (u < 0); + CADICAL_assert (v < 0); if (lrat) { inst_chain.push_back (w.clause); } @@ -176,7 +180,7 @@ bool Internal::instantiate_candidate (int lit, Clause *c) { stats.instried++; if (c->garbage) return false; - assert (!level); + CADICAL_assert (!level); bool found = false, satisfied = false, inactive = false; int unassigned = 0; for (const auto &other : *c) { @@ -203,13 +207,13 @@ bool Internal::instantiate_candidate (int lit, Clause *c) { if (unassigned < 3) return false; size_t before = trail.size (); - assert (propagated == before); - assert (active (lit)); - assert (inst_chain.empty ()); + CADICAL_assert (propagated == before); + CADICAL_assert (active (lit)); + CADICAL_assert (inst_chain.empty ()); LOG (c, "trying to instantiate %d in", lit); - assert (!c->garbage); + CADICAL_assert (!c->garbage); c->instantiated = true; - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); level++; inst_assign (lit); // Assume 'lit' to true. for (const auto &other : *c) { @@ -217,23 +221,23 @@ bool Internal::instantiate_candidate (int lit, Clause *c) { continue; const signed char tmp = val (other); if (tmp) { - assert (tmp < 0); + CADICAL_assert (tmp < 0); continue; } inst_assign (-other); // Assume other to false. } bool ok = inst_propagate (); // Propagate. - assert (lrat_chain.empty ()); // chain will be built here + CADICAL_assert (lrat_chain.empty ()); // chain will be built here if (ok) { inst_chain.clear (); } else if (lrat) { // analyze conflict for lrat - assert (inst_chain.size ()); + CADICAL_assert (inst_chain.size ()); Clause *reason = inst_chain.back (); inst_chain.pop_back (); lrat_chain.push_back (reason->id); for (const auto &other : *reason) { Flags &f = flags (other); - assert (!f.seen); + CADICAL_assert (!f.seen); f.seen = true; analyzed.push_back (other); } @@ -242,7 +246,7 @@ bool Internal::instantiate_candidate (int lit, Clause *c) { const int other = trail.back (); LOG ("instantiate unassign %d", other); trail.pop_back (); - assert (val (other) > 0); + CADICAL_assert (val (other) > 0); num_assigned--; set_val (other, 0); // this is a variant of conflict analysis which is only needed for lrat @@ -263,7 +267,7 @@ bool Internal::instantiate_candidate (int lit, Clause *c) { inst_chain.pop_back (); } } - assert (inst_chain.empty ()); + CADICAL_assert (inst_chain.empty ()); // post processing step for lrat if (!ok && lrat) { if (flags (lit).seen) @@ -284,12 +288,12 @@ bool Internal::instantiate_candidate (int lit, Clause *c) { clear_analyzed_literals (); reverse (lrat_chain.begin (), lrat_chain.end ()); } - assert (analyzed.empty ()); + CADICAL_assert (analyzed.empty ()); propagated = before; - assert (level == 1); + CADICAL_assert (level == 1); level = 0; if (ok) { - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); LOG ("instantiation failed"); return false; } @@ -298,7 +302,7 @@ bool Internal::instantiate_candidate (int lit, Clause *c) { strengthen_clause (c, lit); watch_clause (c); lrat_chain.clear (); - assert (c->size > 1); + CADICAL_assert (c->size > 1); LOG ("instantiation succeeded"); stats.instantiated++; return true; @@ -310,10 +314,10 @@ bool Internal::instantiate_candidate (int lit, Clause *c) { // 'collect_instantiation_candidates' routine. void Internal::instantiate (Instantiator &instantiator) { - assert (opts.instantiate); + CADICAL_assert (opts.instantiate); START (instantiate); stats.instrounds++; -#ifndef QUIET +#ifndef CADICAL_QUIET const int64_t candidates = instantiator.candidates.size (); int64_t tried = 0; #endif @@ -324,7 +328,7 @@ void Internal::instantiate (Instantiator &instantiator) { if (!propagate ()) { LOG ("propagation after connecting watches failed"); learn_empty_clause (); - assert (unsat); + CADICAL_assert (unsat); } } PHASE ("instantiate", stats.instrounds, @@ -335,7 +339,7 @@ void Internal::instantiate (Instantiator &instantiator) { !instantiator.candidates.empty ()) { Instantiator::Candidate cand = instantiator.candidates.back (); instantiator.candidates.pop_back (); -#ifndef QUIET +#ifndef CADICAL_QUIET tried++; #endif if (!active (cand.lit)) @@ -363,3 +367,5 @@ void Internal::instantiate (Instantiator &instantiator) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/internal.cpp b/src/sat/cadical/cadical_internal.cpp similarity index 94% rename from src/sat/cadical/internal.cpp rename to src/sat/cadical/cadical_internal.cpp index 4ef1df058..abe18c59c 100644 --- a/src/sat/cadical/internal.cpp +++ b/src/sat/cadical/cadical_internal.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -25,7 +29,7 @@ Internal::Internal () target_assigned (0), no_conflict_until (0), unsat_constraint (false), marked_failed (true), sweep_incomplete (false), citten (0), num_assigned (0), proof (0), opts (this), -#ifndef QUIET +#ifndef CADICAL_QUIET profiles (this), force_phase_messages (false), #endif arena (this), prefix ("c "), internal (this), external (0), @@ -51,7 +55,7 @@ Internal::Internal () Internal::~Internal () { // If a memory exception ocurred a profile might still be active. -#ifndef QUIET +#ifndef CADICAL_QUIET #define PROFILE(NAME, LEVEL) \ if (PROFILE_ACTIVE (NAME)) \ STOP (NAME); @@ -108,7 +112,7 @@ void Internal::enlarge_vals (size_t new_vsize) { vals -= vsize; delete[] vals; } else - assert (!vsize); + CADICAL_assert (!vsize); vals = new_vals; } @@ -156,16 +160,16 @@ void Internal::init_vars (int new_max_var) { new_max_var - max_var, max_var + 1, new_max_var); if ((size_t) new_max_var >= vsize) enlarge (new_max_var); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (int64_t i = -new_max_var; i < -max_var; i++) - assert (!vals[i]); + CADICAL_assert (!vals[i]); for (unsigned i = max_var + 1; i <= (unsigned) new_max_var; i++) - assert (!vals[i]), assert (!btab[i]), assert (!gtab[i]); + CADICAL_assert (!vals[i]), CADICAL_assert (!btab[i]), CADICAL_assert (!gtab[i]); for (uint64_t i = 2 * ((uint64_t) max_var + 1); i <= 2 * (uint64_t) new_max_var + 1; i++) - assert (ptab[i] == -1); + CADICAL_assert (ptab[i] == -1); #endif - assert (!btab[0]); + CADICAL_assert (!btab[0]); int old_max_var = max_var; max_var = new_max_var; init_queue (old_max_var, new_max_var); @@ -178,7 +182,7 @@ void Internal::init_vars (int new_max_var) { } void Internal::add_original_lit (int lit) { - assert (abs (lit) <= max_var); + CADICAL_assert (abs (lit) <= max_var); if (lit) { original.push_back (lit); } else { @@ -187,14 +191,14 @@ void Internal::add_original_lit (int lit) { if (proof) { // Use the external form of the clause for printing in proof // Externalize(internalized literal) != external literal - assert (!original.size () || !external->eclause.empty ()); + CADICAL_assert (!original.size () || !external->eclause.empty ()); proof->add_external_original_clause (id, false, external->eclause); } if (internal->opts.check && (internal->opts.checkwitness || internal->opts.checkfailed)) { bool forgettable = from_propagator && ext_clause_forgettable; if (forgettable && opts.check) { - assert (!original.size () || !external->eclause.empty ()); + CADICAL_assert (!original.size () || !external->eclause.empty ()); // First integer is the presence-flag (even if the clause is empty) external->forgettable_original[id] = {1}; @@ -216,7 +220,7 @@ void Internal::finish_added_clause_with_id (int64_t id, bool restore) { if (proof) { // Use the external form of the clause for printing in proof // Externalize(internalized literal) != external literal - assert (!original.size () || !external->eclause.empty ()); + CADICAL_assert (!original.size () || !external->eclause.empty ()); proof->add_external_original_clause (id, false, external->eclause, restore); } @@ -229,8 +233,8 @@ void Internal::finish_added_clause_with_id (int64_t id, bool restore) { void Internal::reserve_ids (int number) { // return; LOG ("reserving %d ids", number); - assert (number >= 0); - assert (!clause_id && !reserved_ids && !original_id); + CADICAL_assert (number >= 0); + CADICAL_assert (!clause_id && !reserved_ids && !original_id); clause_id = reserved_ids = number; if (proof) proof->begin_proof (reserved_ids); @@ -350,7 +354,7 @@ int Internal::propagate_assumptions () { stats.ilbsuccess += (level > 0); stats.levelsreused += level; if (level) { - assert (control.size () > 1); + CADICAL_assert (control.size () > 1); stats.literalsreused += num_assigned - control[1].trail; } } @@ -577,7 +581,7 @@ void Internal::init_search_limits () { init_averages (); } else if (opts.stabilize && opts.stabilizeonly) { LOG ("keeping always forced stable phase"); - assert (stable); + CADICAL_assert (stable); } else if (stable) { LOG ("switching back to default non-stable phase"); stable = false; @@ -655,7 +659,7 @@ bool Internal::preprocess_round (int round) { before.vars = active (); before.clauses = stats.current.irredundant; stats.preprocessings++; - assert (!preprocessing); + CADICAL_assert (!preprocessing); preprocessing = true; PHASE ("preprocessing", stats.preprocessings, "starting round %d with %" PRId64 " variables and %" PRId64 @@ -671,7 +675,7 @@ bool Internal::preprocess_round (int round) { after.vars = active (); after.clauses = stats.current.irredundant; - assert (preprocessing); + CADICAL_assert (preprocessing); preprocessing = false; PHASE ("preprocessing", stats.preprocessings, "finished round %d with %" PRId64 " variables and %" PRId64 @@ -703,7 +707,7 @@ void Internal::preprocess_quickly () { before.vars = active (); before.clauses = stats.current.irredundant; // stats.preprocessings++; - assert (!preprocessing); + CADICAL_assert (!preprocessing); preprocessing = true; PHASE ("preprocessing", stats.preprocessings, "starting with %" PRId64 " variables and %" PRId64 " clauses", @@ -724,7 +728,7 @@ void Internal::preprocess_quickly () { // condition (false); after.vars = active (); after.clauses = stats.current.irredundant; - assert (preprocessing); + CADICAL_assert (preprocessing); preprocessing = false; PHASE ("preprocessing", stats.preprocessings, "finished with %" PRId64 " variables and %" PRId64 " clauses", @@ -747,12 +751,12 @@ int Internal::preprocess () { int Internal::try_to_satisfy_formula_by_saved_phases () { LOG ("satisfying formula by saved phases"); - assert (!level); - assert (!force_saved_phase); - assert (propagated == trail.size ()); + CADICAL_assert (!level); + CADICAL_assert (!force_saved_phase); + CADICAL_assert (propagated == trail.size ()); force_saved_phase = true; if (external_prop) { - assert (!level); + CADICAL_assert (!level); LOG ("external notifications are turned off during preprocessing."); private_steps = true; } @@ -766,14 +770,14 @@ int Internal::try_to_satisfy_formula_by_saved_phases () { res = 20; } else if (!propagate ()) { LOG ("saved phases do not satisfy redundant clauses"); - assert (level > 0); + CADICAL_assert (level > 0); backtrack (); conflict = 0; // ignore conflict - assert (!res); + CADICAL_assert (!res); break; } } - assert (force_saved_phase); + CADICAL_assert (force_saved_phase); force_saved_phase = false; if (external_prop) { private_steps = false; @@ -791,10 +795,10 @@ int Internal::try_to_satisfy_formula_by_saved_phases () { void Internal::produce_failed_assumptions () { LOG ("producing failed assumptions"); - assert (!level); - assert (!assumptions.empty ()); + CADICAL_assert (!level); + CADICAL_assert (!assumptions.empty ()); while (!unsat) { - assert (!satisfied ()); + CADICAL_assert (!satisfied ()); notify_assignments (); if (decide ()) break; @@ -812,7 +816,7 @@ void Internal::produce_failed_assumptions () { int Internal::local_search_round (int round) { - assert (round > 0); + CADICAL_assert (round > 0); if (unsat) return false; @@ -820,7 +824,7 @@ int Internal::local_search_round (int round) { return false; START_OUTER_WALK (); - assert (!localsearching); + CADICAL_assert (!localsearching); localsearching = true; // Determine propagation limit quadratically scaled with rounds. @@ -834,7 +838,7 @@ int Internal::local_search_round (int round) { int res = walk_round (limit, true); - assert (localsearching); + CADICAL_assert (localsearching); localsearching = false; STOP_OUTER_WALK (); @@ -861,11 +865,11 @@ int Internal::local_search () { if (res == 10) { LOG ("local search determined formula to be satisfiable"); - assert (!stats.walk.minimum); + CADICAL_assert (!stats.walk.minimum); res = try_to_satisfy_formula_by_saved_phases (); } else if (res == 20) { LOG ("local search determined assumptions to be inconsistent"); - assert (!assumptions.empty ()); + CADICAL_assert (!assumptions.empty ()); produce_failed_assumptions (); } @@ -878,7 +882,7 @@ int Internal::local_search () { // such that we do not have to backtrack to level 0. // int Internal::solve (bool preprocess_only) { - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); START (solve); if (proof) proof->solve_query (); @@ -889,7 +893,7 @@ int Internal::solve (bool preprocess_only) { stats.ilbsuccess += (level > 0); stats.levelsreused += level; if (level) { - assert (control.size () > 1); + CADICAL_assert (control.size () > 1); stats.literalsreused += num_assigned - control[1].trail; } if (external->propagator) @@ -998,9 +1002,9 @@ int Internal::restore_clauses () { } int Internal::lookahead () { - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); START (lookahead); - assert (!lookingahead); + CADICAL_assert (!lookingahead); lookingahead = true; if (external_prop) { if (level) { @@ -1021,7 +1025,7 @@ int Internal::lookahead () { res = 0; reset_solving (); report_solving (tmp); - assert (lookingahead); + CADICAL_assert (lookingahead); lookingahead = false; STOP (lookahead); if (external_prop) { @@ -1041,7 +1045,7 @@ void Internal::finalize (int res) { // finalize external units if (frat) { for (const auto &evar : external->vars) { - assert (evar > 0); + CADICAL_assert (evar > 0); const auto eidx = 2 * evar; int sign = 1; int64_t id = external->ext_units[eidx]; @@ -1060,7 +1064,7 @@ void Internal::finalize (int res) { const unsigned eidx = (elit < 0) + 2u * (unsigned) abs (elit); const int64_t id = external->ext_units[eidx]; if (id) { - assert (unit_clauses (vlit (lit)) == id); + CADICAL_assert (unit_clauses (vlit (lit)) == id); continue; } } @@ -1188,3 +1192,5 @@ bool Internal::traverse_clauses (ClauseIterator &it) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/ipasir.cpp b/src/sat/cadical/cadical_ipasir.cpp similarity index 93% rename from src/sat/cadical/ipasir.cpp rename to src/sat/cadical/cadical_ipasir.cpp index 0eae5e95f..cb88deb4b 100644 --- a/src/sat/cadical/ipasir.cpp +++ b/src/sat/cadical/cadical_ipasir.cpp @@ -1,7 +1,9 @@ +#include "global.h" + #include "ipasir.h" #include "ccadical.h" -extern "C" { +ABC_NAMESPACE_IMPL_START const char *ipasir_signature () { return ccadical_signature (); } @@ -40,4 +42,5 @@ void ipasir_set_learn (void *solver, void *state, int max_length, void (*learn) (void *state, int *clause)) { ccadical_set_learn ((CCaDiCaL *) solver, state, max_length, learn); } -} + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/cadical_kitten.c b/src/sat/cadical/cadical_kitten.c new file mode 100644 index 000000000..da3738980 --- /dev/null +++ b/src/sat/cadical/cadical_kitten.c @@ -0,0 +1,2609 @@ +#include "global.h" + +#include "kitten.h" +#include "random.h" +#include "stack.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +ABC_NAMESPACE_IMPL_START + +typedef signed char value; + +static void die (const char *fmt, ...) { + fputs ("cadical_kitten: error: ", stderr); + va_list ap; + va_start (ap, fmt); + vfprintf (stderr, fmt, ap); + va_end (ap); + fputc ('\n', stderr); + exit (1); +} + +static inline void *cadical_kitten_calloc (size_t n, size_t size) { + void *res = calloc (n, size); + if (n && size && !res) + die ("out of memory allocating '%zu * %zu' bytes", n, size); + return res; +} + +#define CALLOC(T, P, N) \ + do { \ + (P) = (T*)cadical_kitten_calloc (N, sizeof *(P)); \ + } while (0) +#define DEALLOC(P, N) free (P) + +#undef ENLARGE_STACK + +#define ENLARGE_STACK(S) \ + do { \ + CADICAL_assert (FULL_STACK (S)); \ + const size_t SIZE = SIZE_STACK (S); \ + const size_t OLD_CAPACITY = CAPACITY_STACK (S); \ + const size_t NEW_CAPACITY = OLD_CAPACITY ? 2 * OLD_CAPACITY : 1; \ + const size_t BYTES = NEW_CAPACITY * sizeof *(S).begin; \ + (S).begin = (unsigned*)realloc ((S).begin, BYTES); \ + if (!(S).begin) \ + die ("out of memory reallocating '%zu' bytes", BYTES); \ + (S).allocated = (S).begin + NEW_CAPACITY; \ + (S).end = (S).begin + SIZE; \ + } while (0) + +// Beside allocators above also use stand alone statistics counters. + +#define INC(NAME) \ + do { \ + statistics *statistics = &cadical_kitten->statistics; \ + CADICAL_assert (statistics->NAME < UINT64_MAX); \ + statistics->NAME++; \ + } while (0) + +#define ADD(NAME, DELTA) \ + do { \ + statistics *statistics = &cadical_kitten->statistics; \ + CADICAL_assert (statistics->NAME <= UINT64_MAX - (DELTA)); \ + statistics->NAME += (DELTA); \ + } while (0) + +#define KITTEN_TICKS (cadical_kitten->statistics.cadical_kitten_ticks) + +#define INVALID UINT_MAX +#define MAX_VARS ((1u << 31) - 1) + +#define CORE_FLAG (1u) +#define LEARNED_FLAG (2u) + +// clang-format off + +typedef struct kar kar; +typedef struct kink kink; +typedef struct klause klause; +typedef STACK (unsigned) klauses; +typedef unsigneds katches; + +// clang-format on + +struct kar { + unsigned level; + unsigned reason; +}; + +struct kink { + unsigned next; + unsigned prev; + uint64_t stamp; +}; + +struct klause { + unsigned aux; + unsigned size; + unsigned flags; + unsigned lits[1]; +}; + +typedef struct statistics statistics; + +struct statistics { + uint64_t learned; + uint64_t original; + uint64_t cadical_kitten_flip; + uint64_t cadical_kitten_flipped; + uint64_t cadical_kitten_sat; + uint64_t cadical_kitten_solved; + uint64_t cadical_kitten_conflicts; + uint64_t cadical_kitten_decisions; + uint64_t cadical_kitten_propagations; + uint64_t cadical_kitten_ticks; + uint64_t cadical_kitten_unknown; + uint64_t cadical_kitten_unsat; +}; + +typedef struct kimits kimits; + +struct kimits { + uint64_t ticks; +}; + +struct cadical_kitten { + // First zero initialized field in 'clear_cadical_kitten' is 'status'. + // + int status; + +#if defined(LOGGING) + bool logging; +#endif + bool antecedents; + bool learned; + + unsigned level; + unsigned propagated; + unsigned unassigned; + unsigned inconsistent; + unsigned failing; + + uint64_t generator; + + size_t lits; + size_t evars; + + size_t end_original_ref; + + struct { + unsigned first, last; + uint64_t stamp; + unsigned search; + } queue; + + // The 'size' field below is the first not zero reinitialized field + // by 'memset' in 'clear_cadical_kitten' (after 'kissat'). + + size_t size; + size_t esize; + + kar *vars; + kink *links; + value *marks; + value *values; + bool *failed; + unsigned char *phases; + unsigned *import; + katches *watches; + + unsigneds analyzed; + unsigneds assumptions; + unsigneds core; + unsigneds rcore; + unsigneds eclause; + unsigneds export_; + unsigneds klause; + unsigneds klauses; + unsigneds resolved; + unsigneds trail; + unsigneds units; + unsigneds prime[2]; + + kimits limits; + int (*terminator) (void *); + void *terminator_data; + unsigneds clause; + uint64_t initialized; + statistics statistics; +}; + +/*------------------------------------------------------------------------*/ + +static inline bool is_core_klause (klause *c) { + return c->flags & CORE_FLAG; +} + +static inline bool is_learned_klause (klause *c) { + return c->flags & LEARNED_FLAG; +} + +static inline void set_core_klause (klause *c) { c->flags |= CORE_FLAG; } + +static inline void unset_core_klause (klause *c) { c->flags &= ~CORE_FLAG; } + +static inline klause *dereference_klause (cadical_kitten *cadical_kitten, unsigned ref) { + unsigned *res = BEGIN_STACK (cadical_kitten->klauses) + ref; + CADICAL_assert (res < END_STACK (cadical_kitten->klauses)); + return (klause *) res; +} + +static inline unsigned reference_klause (cadical_kitten *cadical_kitten, const klause *c) { + const unsigned *const begin = BEGIN_STACK (cadical_kitten->klauses); + const unsigned *p = (const unsigned *) c; + CADICAL_assert (begin <= p); + CADICAL_assert (p < END_STACK (cadical_kitten->klauses)); + const unsigned res = p - begin; + return res; +} + +/*------------------------------------------------------------------------*/ + +#define KATCHES(KIT) (cadical_kitten->watches[CADICAL_assert ((KIT) < cadical_kitten->lits), (KIT)]) + +#define all_klauses(C) \ + klause *C = begin_klauses (cadical_kitten), *end_##C = end_klauses (cadical_kitten); \ + (C) != end_##C; \ + (C) = next_klause (cadical_kitten, C) + +#define all_original_klauses(C) \ + klause *C = begin_klauses (cadical_kitten), \ + *end_##C = end_original_klauses (cadical_kitten); \ + (C) != end_##C; \ + (C) = next_klause (cadical_kitten, C) + +#define all_learned_klauses(C) \ + klause *C = begin_learned_klauses (cadical_kitten), \ + *end_##C = end_klauses (cadical_kitten); \ + (C) != end_##C; \ + (C) = next_klause (cadical_kitten, C) + +#define all_kits(KIT) \ + size_t KIT = 0, KIT_##END = cadical_kitten->lits; \ + KIT != KIT_##END; \ + KIT++ + +#define BEGIN_KLAUSE(C) (C)->lits + +#define END_KLAUSE(C) (BEGIN_KLAUSE (C) + (C)->size) + +#define all_literals_in_klause(KIT, C) \ + unsigned KIT, *KIT##_PTR = BEGIN_KLAUSE (C), \ + *KIT##_END = END_KLAUSE (C); \ + KIT##_PTR != KIT##_END && ((KIT = *KIT##_PTR), true); \ + ++KIT##_PTR + +#define all_antecedents(REF, C) \ + unsigned REF, *REF##_PTR = antecedents (C), \ + *REF##_END = REF##_PTR + (C)->aux; \ + REF##_PTR != REF##_END && ((REF = *REF##_PTR), true); \ + ++REF##_PTR + +#ifdef LOGGING + +#define logging (cadical_kitten->logging) + +static void log_basic (cadical_kitten *, const char *, ...) + __attribute__ ((format (printf, 2, 3))); + +static void log_basic (cadical_kitten *cadical_kitten, const char *fmt, ...) { + CADICAL_assert (logging); + printf ("c KITTEN %u ", cadical_kitten->level); + va_list ap; + va_start (ap, fmt); + vprintf (fmt, ap); + va_end (ap); + fputc ('\n', stdout); + fflush (stdout); +} + +static void log_reference (cadical_kitten *, unsigned, const char *, ...) + __attribute__ ((format (printf, 3, 4))); + +static void log_reference (cadical_kitten *cadical_kitten, unsigned ref, const char *fmt, + ...) { + klause *c = dereference_klause (cadical_kitten, ref); + CADICAL_assert (logging); + printf ("c KITTEN %u ", cadical_kitten->level); + va_list ap; + va_start (ap, fmt); + vprintf (fmt, ap); + va_end (ap); + if (is_learned_klause (c)) { + fputs (" learned", stdout); + if (c->aux) + printf ("[%u]", c->aux); + } else { + fputs (" original", stdout); + if (c->aux != INVALID) + printf ("[%u]", c->aux); + } + printf (" size %u clause[%u]", c->size, ref); + value *values = cadical_kitten->values; + kar *vars = cadical_kitten->vars; + for (all_literals_in_klause (lit, c)) { + printf (" %u", lit); + const value value = values[lit]; + if (value) + printf ("@%u=%d", vars[lit / 2].level, (int) value); + } + fputc ('\n', stdout); + fflush (stdout); +} + +static void log_literals (cadical_kitten *, unsigned *, unsigned, const char *, ...) + __attribute__ ((format (printf, 4, 5))); + +static void log_literals (cadical_kitten *cadical_kitten, unsigned *lits, unsigned size, + const char *fmt, ...) { + CADICAL_assert (logging); + printf ("c KITTEN %u ", cadical_kitten->level); + va_list ap; + va_start (ap, fmt); + vprintf (fmt, ap); + va_end (ap); + value *values = cadical_kitten->values; + kar *vars = cadical_kitten->vars; + for (unsigned i = 0; i < size; i++) { + const unsigned lit = lits[i]; + printf (" %u", lit); + const value value = values[lit]; + if (value) + printf ("@%u=%d", vars[lit / 2].level, (int) value); + } + fputc ('\n', stdout); + fflush (stdout); +} + +#define LOG(...) \ + do { \ + if (logging) \ + log_basic (cadical_kitten, __VA_ARGS__); \ + } while (0) + +#define ROG(...) \ + do { \ + if (logging) \ + log_reference (cadical_kitten, __VA_ARGS__); \ + } while (0) + +#define LOGLITS(...) \ + do { \ + if (logging) \ + log_literals (cadical_kitten, __VA_ARGS__); \ + } while (0) + +#else + +#define LOG(...) \ + do { \ + } while (0) +#define ROG(...) \ + do { \ + } while (0) +#define LOGLITS(...) \ + do { \ + } while (0) + +#endif + +static void check_queue (cadical_kitten *cadical_kitten) { +#ifdef CHECK_KITTEN + const unsigned vars = cadical_kitten->lits / 2; + unsigned found = 0, prev = INVALID; + kink *links = cadical_kitten->links; + uint64_t stamp = 0; + for (unsigned idx = cadical_kitten->queue.first, next; idx != INVALID; + idx = next) { + kink *link = links + idx; + CADICAL_assert (link->prev == prev); + CADICAL_assert (!found || stamp < link->stamp); + CADICAL_assert (link->stamp < cadical_kitten->queue.stamp); + stamp = link->stamp; + next = link->next; + prev = idx; + found++; + } + CADICAL_assert (found == vars); + unsigned next = INVALID; + found = 0; + for (unsigned idx = cadical_kitten->queue.last, prev; idx != INVALID; + idx = prev) { + kink *link = links + idx; + CADICAL_assert (link->next == next); + prev = link->prev; + next = idx; + found++; + } + CADICAL_assert (found == vars); + value *values = cadical_kitten->values; + bool first = true; + for (unsigned idx = cadical_kitten->queue.search, next; idx != INVALID; + idx = next) { + kink *link = links + idx; + next = link->next; + const unsigned lit = 2 * idx; + CADICAL_assert (first || values[lit]); + first = false; + } +#else + (void) cadical_kitten; +#endif +} + +static void update_search (cadical_kitten *cadical_kitten, unsigned idx) { + if (cadical_kitten->queue.search == idx) + return; + cadical_kitten->queue.search = idx; + LOG ("search updated to %u stamped %" PRIu64, idx, + cadical_kitten->links[idx].stamp); +} + +static void enqueue (cadical_kitten *cadical_kitten, unsigned idx) { + LOG ("enqueue %u", idx); + kink *links = cadical_kitten->links; + kink *l = links + idx; + const unsigned last = cadical_kitten->queue.last; + if (last == INVALID) + cadical_kitten->queue.first = idx; + else + links[last].next = idx; + l->prev = last; + l->next = INVALID; + cadical_kitten->queue.last = idx; + l->stamp = cadical_kitten->queue.stamp++; + LOG ("stamp %" PRIu64, l->stamp); +} + +static void dequeue (cadical_kitten *cadical_kitten, unsigned idx) { + LOG ("dequeue %u", idx); + kink *links = cadical_kitten->links; + kink *l = links + idx; + const unsigned prev = l->prev; + const unsigned next = l->next; + if (prev == INVALID) + cadical_kitten->queue.first = next; + else + links[prev].next = next; + if (next == INVALID) + cadical_kitten->queue.last = prev; + else + links[next].prev = prev; +} + +static void init_queue (cadical_kitten *cadical_kitten, size_t old_vars, size_t new_vars) { + for (size_t idx = old_vars; idx < new_vars; idx++) { + CADICAL_assert (!cadical_kitten->values[2 * idx]); + CADICAL_assert (cadical_kitten->unassigned < UINT_MAX); + cadical_kitten->unassigned++; + enqueue (cadical_kitten, idx); + } + LOG ("initialized decision queue from %zu to %zu", old_vars, new_vars); + update_search (cadical_kitten, cadical_kitten->queue.last); + check_queue (cadical_kitten); +} + +static void initialize_cadical_kitten (cadical_kitten *cadical_kitten) { + cadical_kitten->queue.first = INVALID; + cadical_kitten->queue.last = INVALID; + cadical_kitten->inconsistent = INVALID; + cadical_kitten->failing = INVALID; + cadical_kitten->queue.search = INVALID; + cadical_kitten->terminator = 0; + cadical_kitten->terminator_data = 0; + cadical_kitten->limits.ticks = UINT64_MAX; + cadical_kitten->generator = cadical_kitten->initialized++; +} + +static void clear_cadical_kitten (cadical_kitten *cadical_kitten) { + size_t bytes = (char *) &cadical_kitten->size - (char *) &cadical_kitten->status; + memset (&cadical_kitten->status, 0, bytes); + memset (&cadical_kitten->statistics, 0, sizeof (statistics)); + initialize_cadical_kitten (cadical_kitten); +} + +#define RESIZE1(T, P) \ + do { \ + void *OLD_PTR = (P); \ + CALLOC (T, (P), new_size / 2); \ + const size_t BYTES = old_vars * sizeof *(P); \ + memcpy ((P), OLD_PTR, BYTES); \ + void *NEW_PTR = (P); \ + (P) = (T*)OLD_PTR; \ + DEALLOC ((P), old_size / 2); \ + (P) = (T*)NEW_PTR; \ + } while (0) + +#define RESIZE2(T, P) \ + do { \ + void *OLD_PTR = (P); \ + CALLOC (T, (P), new_size); \ + const size_t BYTES = old_lits * sizeof *(P); \ + memcpy ((P), OLD_PTR, BYTES); \ + void *NEW_PTR = (P); \ + (P) = (T*)OLD_PTR; \ + DEALLOC ((P), old_size); \ + (P) = (T*)NEW_PTR; \ + } while (0) + +static void enlarge_internal (cadical_kitten *cadical_kitten, size_t lit) { + const size_t new_lits = (lit | 1) + 1; + const size_t old_lits = cadical_kitten->lits; + CADICAL_assert (old_lits <= lit); + CADICAL_assert (old_lits < new_lits); + CADICAL_assert ((lit ^ 1) < new_lits); + CADICAL_assert (lit < new_lits); + const size_t old_size = cadical_kitten->size; + const unsigned new_vars = new_lits / 2; + const unsigned old_vars = old_lits / 2; + if (old_size < new_lits) { + size_t new_size = old_size ? 2 * old_size : 2; + while (new_size <= lit) + new_size *= 2; + LOG ("internal literals resized to %zu from %zu (requested %zu)", + new_size, old_size, new_lits); + + RESIZE1 (value, cadical_kitten->marks); + RESIZE1 (unsigned char, cadical_kitten->phases); + RESIZE2 (value, cadical_kitten->values); + RESIZE2 (bool, cadical_kitten->failed); + RESIZE1 (kar, cadical_kitten->vars); + RESIZE1 (kink, cadical_kitten->links); + RESIZE2 (katches, cadical_kitten->watches); + + cadical_kitten->size = new_size; + } + cadical_kitten->lits = new_lits; + init_queue (cadical_kitten, old_vars, new_vars); + LOG ("internal literals activated until %zu literals", new_lits); + return; +} + +static const char *status_to_string (int status) { + switch (status) { + case 10: + return "formula satisfied"; + case 11: + return "formula satisfied and prime implicant computed"; + case 20: + return "formula inconsistent"; + case 21: + return "formula inconsistent and core computed"; + default: + CADICAL_assert (!status); + return "formula unsolved"; + } +} + +static void invalid_api_usage (const char *fun, const char *fmt, ...) { + fprintf (stderr, "cadical_kitten: fatal error: invalid API usage in '%s': ", fun); + va_list ap; + va_start (ap, fmt); + vfprintf (stderr, fmt, ap); + va_end (ap); + fputc ('\n', stderr); + fflush (stderr); + abort (); +} + +#define INVALID_API_USAGE(...) invalid_api_usage (__func__, __VA_ARGS__) + +#define REQUIRE_INITIALIZED() \ + do { \ + if (!cadical_kitten) \ + INVALID_API_USAGE ("solver argument zero"); \ + } while (0) + +#define REQUIRE_STATUS(EXPECTED) \ + do { \ + REQUIRE_INITIALIZED (); \ + if (cadical_kitten->status != (EXPECTED)) \ + INVALID_API_USAGE ("invalid status '%s' (expected '%s')", \ + status_to_string (cadical_kitten->status), \ + status_to_string (EXPECTED)); \ + } while (0) + +#define UPDATE_STATUS(STATUS) \ + do { \ + if (cadical_kitten->status != (STATUS)) \ + LOG ("updating status from '%s' to '%s'", \ + status_to_string (cadical_kitten->status), status_to_string (STATUS)); \ + else \ + LOG ("keeping status at '%s'", status_to_string (STATUS)); \ + cadical_kitten->status = (STATUS); \ + } while (0) + +cadical_kitten *cadical_kitten_init (void) { + cadical_kitten *cadical_kitten; + CALLOC (struct cadical_kitten, cadical_kitten, 1); + initialize_cadical_kitten (cadical_kitten); + return cadical_kitten; +} + +#ifdef LOGGING +void cadical_kitten_set_logging (cadical_kitten *cadical_kitten) { logging = true; } +#endif + +void cadical_kitten_track_antecedents (cadical_kitten *cadical_kitten) { + REQUIRE_STATUS (0); + + if (cadical_kitten->learned) + INVALID_API_USAGE ("can not start tracking antecedents after learning"); + + LOG ("enabling antecedents tracking"); + cadical_kitten->antecedents = true; +} + +void cadical_kitten_randomize_phases (cadical_kitten *cadical_kitten) { + REQUIRE_INITIALIZED (); + + LOG ("randomizing phases"); + + unsigned char *phases = cadical_kitten->phases; + const unsigned vars = cadical_kitten->size / 2; + + uint64_t random = kissat_next_random64 (&cadical_kitten->generator); + + unsigned i = 0; + const unsigned rest = vars & ~63u; + + while (i != rest) { + uint64_t *p = (uint64_t *) (phases + i); + p[0] = (random >> 0) & 0x0101010101010101; + p[1] = (random >> 1) & 0x0101010101010101; + p[2] = (random >> 2) & 0x0101010101010101; + p[3] = (random >> 3) & 0x0101010101010101; + p[4] = (random >> 4) & 0x0101010101010101; + p[5] = (random >> 5) & 0x0101010101010101; + p[6] = (random >> 6) & 0x0101010101010101; + p[7] = (random >> 7) & 0x0101010101010101; + random = kissat_next_random64 (&cadical_kitten->generator); + i += 64; + } + + unsigned shift = 0; + while (i != vars) + phases[i++] = (random >> shift++) & 1; +} + +void cadical_kitten_flip_phases (cadical_kitten *cadical_kitten) { + REQUIRE_INITIALIZED (); + + LOG ("flipping phases"); + + unsigned char *phases = cadical_kitten->phases; + const unsigned vars = cadical_kitten->size / 2; + + unsigned i = 0; + const unsigned rest = vars & ~7u; + + while (i != rest) { + uint64_t *p = (uint64_t *) (phases + i); + *p ^= 0x0101010101010101; + i += 8; + } + + while (i != vars) + phases[i++] ^= 1; +} + +void cadical_kitten_no_ticks_limit (cadical_kitten *cadical_kitten) { + REQUIRE_INITIALIZED (); + LOG ("forcing no ticks limit"); + cadical_kitten->limits.ticks = UINT64_MAX; +} + +uint64_t cadical_kitten_current_ticks (cadical_kitten *cadical_kitten) { + REQUIRE_INITIALIZED (); + const uint64_t current = KITTEN_TICKS; + return current; +} + +void cadical_kitten_set_ticks_limit (cadical_kitten *cadical_kitten, uint64_t delta) { + REQUIRE_INITIALIZED (); + const uint64_t current = KITTEN_TICKS; + uint64_t limit; + if (UINT64_MAX - delta <= current) { + LOG ("forcing unlimited ticks limit"); + limit = UINT64_MAX; + } else { + limit = current + delta; + LOG ("new limit of %" PRIu64 " ticks after %" PRIu64, limit, delta); + } + + cadical_kitten->limits.ticks = limit; +} + +void cadical_kitten_no_terminator (cadical_kitten *cadical_kitten) { + REQUIRE_INITIALIZED (); + LOG ("removing terminator"); + cadical_kitten->terminator = 0; + cadical_kitten->terminator_data = 0; +} + +void cadical_kitten_set_terminator (cadical_kitten *cadical_kitten, void *data, + int (*terminator) (void *)) { + REQUIRE_INITIALIZED (); + LOG ("setting terminator"); + cadical_kitten->terminator = terminator; + cadical_kitten->terminator_data = data; +} + +static void shuffle_unsigned_array (cadical_kitten *cadical_kitten, size_t size, + unsigned *a) { + for (size_t i = 0; i < size; i++) { + const size_t j = kissat_pick_random (&cadical_kitten->generator, 0, i); + if (j == i) + continue; + const unsigned first = a[i]; + const unsigned second = a[j]; + a[i] = second; + a[j] = first; + } +} + +static void shuffle_unsigned_stack (cadical_kitten *cadical_kitten, unsigneds *stack) { + const size_t size = SIZE_STACK (*stack); + unsigned *a = BEGIN_STACK (*stack); + shuffle_unsigned_array (cadical_kitten, size, a); +} + +static void shuffle_katches (cadical_kitten *cadical_kitten) { + LOG ("shuffling watch lists"); + for (size_t lit = 0; lit < cadical_kitten->lits; lit++) + shuffle_unsigned_stack (cadical_kitten, &KATCHES (lit)); +} + +static void shuffle_queue (cadical_kitten *cadical_kitten) { + LOG ("shuffling variable decision order"); + + const unsigned vars = cadical_kitten->lits / 2; + for (unsigned i = 0; i < vars; i++) { + const unsigned idx = kissat_pick_random (&cadical_kitten->generator, 0, vars); + dequeue (cadical_kitten, idx); + enqueue (cadical_kitten, idx); + } + update_search (cadical_kitten, cadical_kitten->queue.last); +} + +static void shuffle_units (cadical_kitten *cadical_kitten) { + LOG ("shuffling units"); + shuffle_unsigned_stack (cadical_kitten, &cadical_kitten->units); +} + +void cadical_kitten_shuffle_clauses (cadical_kitten *cadical_kitten) { + REQUIRE_STATUS (0); + shuffle_queue (cadical_kitten); + shuffle_katches (cadical_kitten); + shuffle_units (cadical_kitten); +} + +static inline unsigned *antecedents (klause *c) { + CADICAL_assert (is_learned_klause (c)); + return c->lits + c->size; +} + +static inline void watch_klause (cadical_kitten *cadical_kitten, unsigned lit, + unsigned ref) { + ROG (ref, "watching %u in", lit); + katches *watches = &KATCHES (lit); + PUSH_STACK (*watches, ref); +} + +static inline void connect_new_klause (cadical_kitten *cadical_kitten, unsigned ref) { + ROG (ref, "new"); + + klause *c = dereference_klause (cadical_kitten, ref); + + if (!c->size) { + if (cadical_kitten->inconsistent == INVALID) { + ROG (ref, "registering inconsistent empty"); + cadical_kitten->inconsistent = ref; + } else + ROG (ref, "ignoring inconsistent empty"); + } else if (c->size == 1) { + ROG (ref, "watching unit"); + PUSH_STACK (cadical_kitten->units, ref); + } else { + watch_klause (cadical_kitten, c->lits[0], ref); + watch_klause (cadical_kitten, c->lits[1], ref); + } +} + +static unsigned new_reference (cadical_kitten *cadical_kitten) { + size_t ref = SIZE_STACK (cadical_kitten->klauses); + if (ref >= INVALID) { + die ("maximum number of literals exhausted"); + } + const unsigned res = (unsigned) ref; + CADICAL_assert (res != INVALID); + INC (cadical_kitten_ticks); + return res; +} + +static void new_original_klause (cadical_kitten *cadical_kitten, unsigned id) { + unsigned res = new_reference (cadical_kitten); + unsigned size = SIZE_STACK (cadical_kitten->klause); + unsigneds *klauses = &cadical_kitten->klauses; + PUSH_STACK (*klauses, id); + PUSH_STACK (*klauses, size); + PUSH_STACK (*klauses, 0); + for (all_stack (unsigned, lit, cadical_kitten->klause)) + PUSH_STACK (*klauses, lit); + connect_new_klause (cadical_kitten, res); + cadical_kitten->end_original_ref = SIZE_STACK (*klauses); + cadical_kitten->statistics.original++; +} + +static void enlarge_external (cadical_kitten *cadical_kitten, size_t eidx) { + const size_t old_size = cadical_kitten->esize; + const unsigned old_evars = cadical_kitten->evars; + CADICAL_assert (old_evars <= eidx); + const unsigned new_evars = eidx + 1; + if (old_size <= eidx) { + size_t new_size = old_size ? 2 * old_size : 1; + while (new_size <= eidx) + new_size *= 2; + LOG ("external resizing to %zu variables from %zu (requested %u)", + new_size, old_size, new_evars); + unsigned *old_import = cadical_kitten->import; + CALLOC (unsigned, cadical_kitten->import, new_size); + const size_t bytes = old_evars * sizeof *cadical_kitten->import; + memcpy (cadical_kitten->import, old_import, bytes); + DEALLOC (old_import, old_size); + cadical_kitten->esize = new_size; + } + cadical_kitten->evars = new_evars; + LOG ("external variables enlarged to %u", new_evars); +} + +static unsigned import_literal (cadical_kitten *cadical_kitten, unsigned elit) { + const unsigned eidx = elit / 2; + if (eidx >= cadical_kitten->evars) + enlarge_external (cadical_kitten, eidx); + + unsigned iidx = cadical_kitten->import[eidx]; + if (!iidx) { + iidx = SIZE_STACK (cadical_kitten->export_); + PUSH_STACK (cadical_kitten->export_, eidx); + cadical_kitten->import[eidx] = iidx + 1; + } else + iidx--; + unsigned ilit = 2 * iidx + (elit & 1); + LOG ("imported external literal %u as internal literal %u", elit, ilit); + if (ilit >= cadical_kitten->lits) + enlarge_internal (cadical_kitten, ilit); + return ilit; +} + +static unsigned export_literal (cadical_kitten *cadical_kitten, unsigned ilit) { + const unsigned iidx = ilit / 2; + CADICAL_assert (iidx < SIZE_STACK (cadical_kitten->export_)); + const unsigned eidx = PEEK_STACK (cadical_kitten->export_, iidx); + const unsigned elit = 2 * eidx + (ilit & 1); + return elit; +} + +unsigned cadical_new_learned_klause (cadical_kitten *cadical_kitten) { + unsigned res = new_reference (cadical_kitten); + unsigneds *klauses = &cadical_kitten->klauses; + const size_t size = SIZE_STACK (cadical_kitten->klause); + CADICAL_assert (size <= UINT_MAX); + const size_t aux = + cadical_kitten->antecedents ? SIZE_STACK (cadical_kitten->resolved) : 0; + CADICAL_assert (aux <= UINT_MAX); + PUSH_STACK (*klauses, (unsigned) aux); + PUSH_STACK (*klauses, (unsigned) size); + PUSH_STACK (*klauses, LEARNED_FLAG); + for (all_stack (unsigned, lit, cadical_kitten->klause)) + PUSH_STACK (*klauses, lit); + if (aux) + for (all_stack (unsigned, ref, cadical_kitten->resolved)) + PUSH_STACK (*klauses, ref); + connect_new_klause (cadical_kitten, res); + cadical_kitten->learned = true; + cadical_kitten->statistics.learned++; + return res; +} + +void cadical_kitten_clear (cadical_kitten *cadical_kitten) { + LOG ("clear cadical_kitten of size %zu", cadical_kitten->size); + + CADICAL_assert (EMPTY_STACK (cadical_kitten->analyzed)); + CADICAL_assert (EMPTY_STACK (cadical_kitten->eclause)); + CADICAL_assert (EMPTY_STACK (cadical_kitten->resolved)); + + CLEAR_STACK (cadical_kitten->assumptions); + CLEAR_STACK (cadical_kitten->core); + CLEAR_STACK (cadical_kitten->klause); + CLEAR_STACK (cadical_kitten->klauses); + CLEAR_STACK (cadical_kitten->trail); + CLEAR_STACK (cadical_kitten->units); + CLEAR_STACK (cadical_kitten->clause); + CLEAR_STACK (cadical_kitten->prime[0]); + CLEAR_STACK (cadical_kitten->prime[1]); + + for (all_kits (kit)) + CLEAR_STACK (KATCHES (kit)); + + while (!EMPTY_STACK (cadical_kitten->export_)) + cadical_kitten->import[POP_STACK (cadical_kitten->export_)] = 0; + + const size_t lits = cadical_kitten->size; + const unsigned vars = lits / 2; + +#ifndef CADICAL_NDEBUG + for (unsigned i = 0; i < vars; i++) + CADICAL_assert (!cadical_kitten->marks[i]); +#endif + + memset (cadical_kitten->phases, 0, vars); + memset (cadical_kitten->values, 0, lits); + memset (cadical_kitten->failed, 0, lits); + memset (cadical_kitten->vars, 0, vars); + + clear_cadical_kitten (cadical_kitten); +} + +void cadical_kitten_release (cadical_kitten *cadical_kitten) { + RELEASE_STACK (cadical_kitten->analyzed); + RELEASE_STACK (cadical_kitten->assumptions); + RELEASE_STACK (cadical_kitten->core); + RELEASE_STACK (cadical_kitten->eclause); + RELEASE_STACK (cadical_kitten->export_); + RELEASE_STACK (cadical_kitten->klause); + RELEASE_STACK (cadical_kitten->klauses); + RELEASE_STACK (cadical_kitten->resolved); + RELEASE_STACK (cadical_kitten->trail); + RELEASE_STACK (cadical_kitten->units); + RELEASE_STACK (cadical_kitten->clause); + RELEASE_STACK (cadical_kitten->prime[0]); + RELEASE_STACK (cadical_kitten->prime[1]); + + for (size_t lit = 0; lit < cadical_kitten->size; lit++) + RELEASE_STACK (cadical_kitten->watches[lit]); + + const size_t lits = cadical_kitten->size; + const unsigned vars = lits / 2; + DEALLOC (cadical_kitten->marks, vars); + DEALLOC (cadical_kitten->phases, vars); + DEALLOC (cadical_kitten->values, lits); + DEALLOC (cadical_kitten->failed, lits); + DEALLOC (cadical_kitten->vars, vars); + DEALLOC (cadical_kitten->links, vars); + DEALLOC (cadical_kitten->watches, lits); + DEALLOC (cadical_kitten->import, cadical_kitten->esize); + (void) vars; + + free (cadical_kitten); +} + +static inline void move_to_front (cadical_kitten *cadical_kitten, unsigned idx) { + if (idx == cadical_kitten->queue.last) + return; + LOG ("move to front variable %u", idx); + dequeue (cadical_kitten, idx); + enqueue (cadical_kitten, idx); + CADICAL_assert (cadical_kitten->values[2 * idx]); +} + +static inline void assign (cadical_kitten *cadical_kitten, unsigned lit, unsigned reason) { +#ifdef LOGGING + if (reason == INVALID) + LOG ("assign %u as decision", lit); + else + ROG (reason, "assign %u reason", lit); +#endif + value *values = cadical_kitten->values; + const unsigned not_lit = lit ^ 1; + CADICAL_assert (!values[lit]); + CADICAL_assert (!values[not_lit]); + values[lit] = 1; + values[not_lit] = -1; + const unsigned idx = lit / 2; + const unsigned sign = lit & 1; + cadical_kitten->phases[idx] = sign; + PUSH_STACK (cadical_kitten->trail, lit); + kar *v = cadical_kitten->vars + idx; + v->level = cadical_kitten->level; + if (!v->level) { + CADICAL_assert (reason != INVALID); + klause *c = dereference_klause (cadical_kitten, reason); + if (c->size > 1) { + if (cadical_kitten->antecedents) { + PUSH_STACK (cadical_kitten->resolved, reason); + for (all_literals_in_klause (other, c)) + if (other != lit) { + const unsigned other_idx = other / 2; + const unsigned other_ref = cadical_kitten->vars[other_idx].reason; + CADICAL_assert (other_ref != INVALID); + PUSH_STACK (cadical_kitten->resolved, other_ref); + } + } + PUSH_STACK (cadical_kitten->klause, lit); + reason = cadical_new_learned_klause (cadical_kitten); + CLEAR_STACK (cadical_kitten->resolved); + CLEAR_STACK (cadical_kitten->klause); + } + } + v->reason = reason; + CADICAL_assert (cadical_kitten->unassigned); + cadical_kitten->unassigned--; +} + +static inline unsigned propagate_literal (cadical_kitten *cadical_kitten, unsigned lit) { + LOG ("propagating %u", lit); + value *values = cadical_kitten->values; + CADICAL_assert (values[lit] > 0); + const unsigned not_lit = lit ^ 1; + katches *watches = cadical_kitten->watches + not_lit; + unsigned conflict = INVALID; + unsigned *q = BEGIN_STACK (*watches); + const unsigned *const end_watches = END_STACK (*watches); + unsigned const *p = q; + uint64_t ticks = (((char *) end_watches - (char *) q) >> 7) + 1; + while (p != end_watches) { + const unsigned ref = *q++ = *p++; + klause *c = dereference_klause (cadical_kitten, ref); + CADICAL_assert (c->size > 1); + unsigned *lits = c->lits; + const unsigned other = lits[0] ^ lits[1] ^ not_lit; + const value other_value = values[other]; + ticks++; + if (other_value > 0) + continue; + value replacement_value = -1; + unsigned replacement = INVALID; + const unsigned *const end_lits = lits + c->size; + unsigned *r; + for (r = lits + 2; r != end_lits; r++) { + replacement = *r; + replacement_value = values[replacement]; + if (replacement_value >= 0) + break; + } + if (replacement_value >= 0) { + CADICAL_assert (replacement != INVALID); + ROG (ref, "unwatching %u in", not_lit); + lits[0] = other; + lits[1] = replacement; + *r = not_lit; + watch_klause (cadical_kitten, replacement, ref); + q--; + } else if (other_value < 0) { + ROG (ref, "conflict"); + INC (cadical_kitten_conflicts); + conflict = ref; + break; + } else { + CADICAL_assert (!other_value); + assign (cadical_kitten, other, ref); + } + } + while (p != end_watches) + *q++ = *p++; + SET_END_OF_STACK (*watches, q); + ADD (cadical_kitten_ticks, ticks); + return conflict; +} + +static inline unsigned propagate (cadical_kitten *cadical_kitten) { + CADICAL_assert (cadical_kitten->inconsistent == INVALID); + unsigned propagated = 0; + unsigned conflict = INVALID; + while (conflict == INVALID && + cadical_kitten->propagated < SIZE_STACK (cadical_kitten->trail)) { + if (cadical_kitten->terminator && + cadical_kitten->terminator (cadical_kitten->terminator_data)) { + break; + } + const unsigned lit = PEEK_STACK (cadical_kitten->trail, cadical_kitten->propagated); + conflict = propagate_literal (cadical_kitten, lit); + cadical_kitten->propagated++; + propagated++; + } + ADD (cadical_kitten_propagations, propagated); + return conflict; +} + +static void bump (cadical_kitten *cadical_kitten) { + value *marks = cadical_kitten->marks; + for (all_stack (unsigned, idx, cadical_kitten->analyzed)) { + marks[idx] = 0; + move_to_front (cadical_kitten, idx); + } + check_queue (cadical_kitten); +} + +static inline void unassign (cadical_kitten *cadical_kitten, value *values, unsigned lit) { + const unsigned not_lit = lit ^ 1; + CADICAL_assert (values[lit]); + CADICAL_assert (values[not_lit]); + const unsigned idx = lit / 2; +#ifdef LOGGING + kar *var = cadical_kitten->vars + idx; + cadical_kitten->level = var->level; + LOG ("unassign %u", lit); +#endif + values[lit] = values[not_lit] = 0; + CADICAL_assert (cadical_kitten->unassigned < cadical_kitten->lits / 2); + cadical_kitten->unassigned++; + kink *links = cadical_kitten->links; + kink *link = links + idx; + if (link->stamp > links[cadical_kitten->queue.search].stamp) + update_search (cadical_kitten, idx); +} + +static void backtrack (cadical_kitten *cadical_kitten, unsigned jump) { + check_queue (cadical_kitten); + CADICAL_assert (jump < cadical_kitten->level); + LOG ("back%s to level %u", + (cadical_kitten->level == jump + 1 ? "tracking" : "jumping"), jump); + kar *vars = cadical_kitten->vars; + value *values = cadical_kitten->values; + unsigneds *trail = &cadical_kitten->trail; + while (!EMPTY_STACK (*trail)) { + const unsigned lit = TOP_STACK (*trail); + const unsigned idx = lit / 2; + const unsigned level = vars[idx].level; + if (level == jump) + break; + (void) POP_STACK (*trail); + unassign (cadical_kitten, values, lit); + } + cadical_kitten->propagated = SIZE_STACK (*trail); + cadical_kitten->level = jump; + check_queue (cadical_kitten); +} + +void cadical_completely_backtrack_to_root_level (cadical_kitten *cadical_kitten) { + check_queue (cadical_kitten); + LOG ("completely backtracking to level 0"); + value *values = cadical_kitten->values; + unsigneds *trail = &cadical_kitten->trail; + unsigneds *units = &cadical_kitten->units; +#ifndef CADICAL_NDEBUG + kar *vars = cadical_kitten->vars; +#endif + for (all_stack (unsigned, lit, *trail)) { + CADICAL_assert (vars[lit / 2].level); + unassign (cadical_kitten, values, lit); + } + CLEAR_STACK (*trail); + for (all_stack (unsigned, ref, *units)) { + klause *c = dereference_klause (cadical_kitten, ref); + CADICAL_assert (c->size == 1); + const unsigned unit = c->lits[0]; + const value value = values[unit]; + if (value <= 0) + continue; + unassign (cadical_kitten, values, unit); + } + cadical_kitten->propagated = 0; + cadical_kitten->level = 0; + check_queue (cadical_kitten); +} + +static void analyze (cadical_kitten *cadical_kitten, unsigned conflict) { + CADICAL_assert (cadical_kitten->level); + CADICAL_assert (cadical_kitten->inconsistent == INVALID); + CADICAL_assert (EMPTY_STACK (cadical_kitten->analyzed)); + CADICAL_assert (EMPTY_STACK (cadical_kitten->resolved)); + CADICAL_assert (EMPTY_STACK (cadical_kitten->klause)); + PUSH_STACK (cadical_kitten->klause, INVALID); + unsigned reason = conflict; + value *marks = cadical_kitten->marks; + const kar *const vars = cadical_kitten->vars; + const unsigned level = cadical_kitten->level; + unsigned const *p = END_STACK (cadical_kitten->trail); + unsigned open = 0, jump = 0, size = 1, uip; + for (;;) { + CADICAL_assert (reason != INVALID); + klause *c = dereference_klause (cadical_kitten, reason); + CADICAL_assert (c); + ROG (reason, "analyzing"); + PUSH_STACK (cadical_kitten->resolved, reason); + for (all_literals_in_klause (lit, c)) { + const unsigned idx = lit / 2; + if (marks[idx]) + continue; + CADICAL_assert (cadical_kitten->values[lit] < 0); + LOG ("analyzed %u", lit); + marks[idx] = true; + PUSH_STACK (cadical_kitten->analyzed, idx); + const kar *const v = vars + idx; + const unsigned tmp = v->level; + if (tmp < level) { + if (tmp > jump) { + jump = tmp; + if (size > 1) { + const unsigned other = PEEK_STACK (cadical_kitten->klause, 1); + POKE_STACK (cadical_kitten->klause, 1, lit); + lit = other; + } + } + PUSH_STACK (cadical_kitten->klause, lit); + size++; + } else + open++; + } + unsigned idx; + do { + CADICAL_assert (BEGIN_STACK (cadical_kitten->trail) < p); + uip = *--p; + } while (!marks[idx = uip / 2]); + CADICAL_assert (open); + if (!--open) + break; + reason = vars[idx].reason; + } + const unsigned not_uip = uip ^ 1; + LOG ("first UIP %u jump level %u size %u", not_uip, jump, size); + POKE_STACK (cadical_kitten->klause, 0, not_uip); + bump (cadical_kitten); + CLEAR_STACK (cadical_kitten->analyzed); + const unsigned learned_ref = cadical_new_learned_klause (cadical_kitten); + CLEAR_STACK (cadical_kitten->resolved); + CLEAR_STACK (cadical_kitten->klause); + backtrack (cadical_kitten, jump); + assign (cadical_kitten, not_uip, learned_ref); +} + +static void failing (cadical_kitten *cadical_kitten) { + CADICAL_assert (cadical_kitten->inconsistent == INVALID); + CADICAL_assert (!EMPTY_STACK (cadical_kitten->assumptions)); + CADICAL_assert (EMPTY_STACK (cadical_kitten->analyzed)); + CADICAL_assert (EMPTY_STACK (cadical_kitten->resolved)); + CADICAL_assert (EMPTY_STACK (cadical_kitten->klause)); + LOG ("analyzing failing assumptions"); + const value *const values = cadical_kitten->values; + const kar *const vars = cadical_kitten->vars; + unsigned failed_clashing = INVALID; + unsigned first_failed = INVALID; + unsigned failed_unit = INVALID; + for (all_stack (unsigned, lit, cadical_kitten->assumptions)) { + if (values[lit] >= 0) + continue; + if (first_failed == INVALID) + first_failed = lit; + const unsigned failed_idx = lit / 2; + const kar *const failed_var = vars + failed_idx; + if (!failed_var->level) { + failed_unit = lit; + break; + } + if (failed_clashing == INVALID && failed_var->reason == INVALID) + failed_clashing = lit; + } + unsigned failed; + if (failed_unit != INVALID) + failed = failed_unit; + else if (failed_clashing != INVALID) + failed = failed_clashing; + else + failed = first_failed; + CADICAL_assert (failed != INVALID); + const unsigned failed_idx = failed / 2; + const kar *const failed_var = vars + failed_idx; + const unsigned failed_reason = failed_var->reason; + LOG ("first failed assumption %u", failed); + cadical_kitten->failed[failed] = true; + + if (failed_unit != INVALID) { + CADICAL_assert (dereference_klause (cadical_kitten, failed_reason)->size == 1); + LOG ("root-level falsified assumption %u", failed); + cadical_kitten->failing = failed_reason; + ROG (cadical_kitten->failing, "failing reason"); + return; + } + + const unsigned not_failed = failed ^ 1; + if (failed_clashing != INVALID) { + LOG ("clashing with negated assumption %u", not_failed); + cadical_kitten->failed[not_failed] = true; + CADICAL_assert (cadical_kitten->failing == INVALID); + return; + } + + value *marks = cadical_kitten->marks; + CADICAL_assert (!marks[failed_idx]); + marks[failed_idx] = true; + PUSH_STACK (cadical_kitten->analyzed, failed_idx); + PUSH_STACK (cadical_kitten->klause, not_failed); + + unsigneds work; + INIT_STACK (work); + + LOGLITS (BEGIN_STACK (cadical_kitten->trail), SIZE_STACK (cadical_kitten->trail), + "trail"); + + CADICAL_assert (SIZE_STACK (cadical_kitten->trail)); + unsigned const *p = END_STACK (cadical_kitten->trail); + unsigned open = 1; + for (;;) { + if (!open) + break; + open--; + unsigned idx, uip; + do { + CADICAL_assert (BEGIN_STACK (cadical_kitten->trail) < p); + uip = *--p; + } while (!marks[idx = uip / 2]); + + const kar *var = vars + idx; + const unsigned reason = var->reason; + if (reason == INVALID) { + unsigned lit = 2 * idx; + if (values[lit] < 0) + lit ^= 1; + LOG ("failed assumption %u", lit); + CADICAL_assert (!cadical_kitten->failed[lit]); + cadical_kitten->failed[lit] = true; + const unsigned not_lit = lit ^ 1; + PUSH_STACK (cadical_kitten->klause, not_lit); + } else { + ROG (reason, "analyzing"); + PUSH_STACK (cadical_kitten->resolved, reason); + klause *c = dereference_klause (cadical_kitten, reason); + for (all_literals_in_klause (other, c)) { + const unsigned other_idx = other / 2; + if (marks[other_idx]) + continue; + CADICAL_assert (other_idx != idx); + marks[other_idx] = true; + CADICAL_assert (values[other]); + if (vars[other_idx].level) + open++; + else + PUSH_STACK (work, other_idx); + PUSH_STACK (cadical_kitten->analyzed, other_idx); + + LOG ("analyzing final literal %u", other ^ 1); + } + } + } + for (size_t next = 0; next < SIZE_STACK (work); next++) { + const unsigned idx = PEEK_STACK (work, next); + const kar *var = vars + idx; + const unsigned reason = var->reason; + if (reason == INVALID) { + unsigned lit = 2 * idx; + if (values[lit] < 0) + lit ^= 1; + LOG ("failed assumption %u", lit); + CADICAL_assert (!cadical_kitten->failed[lit]); + cadical_kitten->failed[lit] = true; + const unsigned not_lit = lit ^ 1; + PUSH_STACK (cadical_kitten->klause, not_lit); + } else { + ROG (reason, "analyzing unit"); + PUSH_STACK (cadical_kitten->resolved, reason); + } + } + + // this is bfs not dfs so it does not work for lrat :/ + /* + for (size_t next = 0; next < SIZE_STACK (cadical_kitten->analyzed); next++) { + const unsigned idx = PEEK_STACK (cadical_kitten->analyzed, next); + CADICAL_assert (marks[idx]); + const kar *var = vars + idx; + const unsigned reason = var->reason; + if (reason == INVALID) { + unsigned lit = 2 * idx; + if (values[lit] < 0) + lit ^= 1; + LOG ("failed assumption %u", lit); + CADICAL_assert (!cadical_kitten->failed[lit]); + cadical_kitten->failed[lit] = true; + const unsigned not_lit = lit ^ 1; + PUSH_STACK (cadical_kitten->klause, not_lit); + } else { + ROG (reason, "analyzing"); + PUSH_STACK (cadical_kitten->resolved, reason); + klause *c = dereference_klause (cadical_kitten, reason); + for (all_literals_in_klause (other, c)) { + const unsigned other_idx = other / 2; + if (other_idx == idx) + continue; + if (marks[other_idx]) + continue; + marks[other_idx] = true; + PUSH_STACK (cadical_kitten->analyzed, other_idx); + LOG ("analyzing final literal %u", other ^ 1); + } + } + } + */ + + for (all_stack (unsigned, idx, cadical_kitten->analyzed)) + CADICAL_assert (marks[idx]), marks[idx] = 0; + CLEAR_STACK (cadical_kitten->analyzed); + + RELEASE_STACK (work); + + const size_t resolved = SIZE_STACK (cadical_kitten->resolved); + CADICAL_assert (resolved); + + if (resolved == 1) { + cadical_kitten->failing = PEEK_STACK (cadical_kitten->resolved, 0); + ROG (cadical_kitten->failing, "reusing as core"); + } else { + cadical_kitten->failing = cadical_new_learned_klause (cadical_kitten); + ROG (cadical_kitten->failing, "new core"); + } + + CLEAR_STACK (cadical_kitten->resolved); + CLEAR_STACK (cadical_kitten->klause); +} + +static void flush_trail (cadical_kitten *cadical_kitten) { + unsigneds *trail = &cadical_kitten->trail; + LOG ("flushing %zu root-level literals from trail", SIZE_STACK (*trail)); + CADICAL_assert (!cadical_kitten->level); + cadical_kitten->propagated = 0; + CLEAR_STACK (*trail); +} + +static int decide (cadical_kitten *cadical_kitten) { + if (!cadical_kitten->level && !EMPTY_STACK (cadical_kitten->trail)) + flush_trail (cadical_kitten); + + const value *const values = cadical_kitten->values; + unsigned decision = INVALID; + const size_t assumptions = SIZE_STACK (cadical_kitten->assumptions); + while (cadical_kitten->level < assumptions) { + unsigned assumption = PEEK_STACK (cadical_kitten->assumptions, cadical_kitten->level); + value value = values[assumption]; + if (value < 0) { + LOG ("found failing assumption %u", assumption); + failing (cadical_kitten); + return 20; + } else if (value > 0) { + + cadical_kitten->level++; + LOG ("pseudo decision level %u for already satisfied assumption %u", + cadical_kitten->level, assumption); + } else { + decision = assumption; + LOG ("using assumption %u as decision", decision); + break; + } + } + + if (!cadical_kitten->unassigned) + return 10; + + if (KITTEN_TICKS >= cadical_kitten->limits.ticks) { + LOG ("ticks limit %" PRIu64 " hit after %" PRIu64 " ticks", + cadical_kitten->limits.ticks, KITTEN_TICKS); + return -1; + } + + if (cadical_kitten->terminator && cadical_kitten->terminator (cadical_kitten->terminator_data)) { + LOG ("terminator requested termination"); + return -1; + } + + if (decision == INVALID) { + unsigned idx = cadical_kitten->queue.search; + const kink *const links = cadical_kitten->links; + for (;;) { + CADICAL_assert (idx != INVALID); + if (!values[2 * idx]) + break; + idx = links[idx].prev; + } + update_search (cadical_kitten, idx); + const unsigned phase = cadical_kitten->phases[idx]; + decision = 2 * idx + phase; + LOG ("decision %u variable %u phase %u", decision, idx, phase); + } + INC (cadical_kitten_decisions); + cadical_kitten->level++; + assign (cadical_kitten, decision, INVALID); + return 0; +} + +static void inconsistent (cadical_kitten *cadical_kitten, unsigned ref) { + CADICAL_assert (ref != INVALID); + CADICAL_assert (cadical_kitten->inconsistent == INVALID); + + if (!cadical_kitten->antecedents) { + cadical_kitten->inconsistent = ref; + ROG (ref, "registering inconsistent virtually empty"); + return; + } + + unsigneds *analyzed = &cadical_kitten->analyzed; + unsigneds *resolved = &cadical_kitten->resolved; + + CADICAL_assert (EMPTY_STACK (*analyzed)); + CADICAL_assert (EMPTY_STACK (*resolved)); + + value *marks = cadical_kitten->marks; + const kar *const vars = cadical_kitten->vars; + unsigned next = 0; + + for (;;) { + CADICAL_assert (ref != INVALID); + klause *c = dereference_klause (cadical_kitten, ref); + CADICAL_assert (c); + ROG (ref, "analyzing inconsistent"); + PUSH_STACK (*resolved, ref); + for (all_literals_in_klause (lit, c)) { + const unsigned idx = lit / 2; + CADICAL_assert (!vars[idx].level); + if (marks[idx]) + continue; + CADICAL_assert (cadical_kitten->values[lit] < 0); + LOG ("analyzed %u", lit); + marks[idx] = true; + PUSH_STACK (cadical_kitten->analyzed, idx); + } + if (next == SIZE_STACK (cadical_kitten->analyzed)) + break; + const unsigned idx = PEEK_STACK (cadical_kitten->analyzed, next); + next++; + const kar *const v = vars + idx; + CADICAL_assert (!v->level); + ref = v->reason; + } + CADICAL_assert (EMPTY_STACK (cadical_kitten->klause)); + ref = cadical_new_learned_klause (cadical_kitten); + ROG (ref, "registering final inconsistent empty"); + cadical_kitten->inconsistent = ref; + + for (all_stack (unsigned, idx, *analyzed)) + marks[idx] = 0; + + CLEAR_STACK (*analyzed); + CLEAR_STACK (*resolved); +} + +static int propagate_units (cadical_kitten *cadical_kitten) { + if (cadical_kitten->inconsistent != INVALID) + return 20; + + if (EMPTY_STACK (cadical_kitten->units)) { + LOG ("no root level unit clauses"); + return 0; + } + + LOG ("propagating %zu root level unit clauses", + SIZE_STACK (cadical_kitten->units)); + + const value *const values = cadical_kitten->values; + + for (size_t next = 0; next < SIZE_STACK (cadical_kitten->units); next++) { + const unsigned ref = PEEK_STACK (cadical_kitten->units, next); + CADICAL_assert (ref != INVALID); + klause *c = dereference_klause (cadical_kitten, ref); + CADICAL_assert (c->size == 1); + ROG (ref, "propagating unit"); + const unsigned unit = c->lits[0]; + const value value = values[unit]; + if (value > 0) + continue; + if (value < 0) { + inconsistent (cadical_kitten, ref); + return 20; + } + assign (cadical_kitten, unit, ref); + } + const unsigned conflict = propagate (cadical_kitten); + if (conflict == INVALID) + return 0; + inconsistent (cadical_kitten, conflict); + return 20; +} + +/*------------------------------------------------------------------------*/ + +static klause *begin_klauses (cadical_kitten *cadical_kitten) { + return (klause *) BEGIN_STACK (cadical_kitten->klauses); +} + +static klause *end_original_klauses (cadical_kitten *cadical_kitten) { + return (klause *) (BEGIN_STACK (cadical_kitten->klauses) + + cadical_kitten->end_original_ref); +} + +static klause *end_klauses (cadical_kitten *cadical_kitten) { + return (klause *) END_STACK (cadical_kitten->klauses); +} + +static klause *next_klause (cadical_kitten *cadical_kitten, klause *c) { + CADICAL_assert (begin_klauses (cadical_kitten) <= c); + CADICAL_assert (c < end_klauses (cadical_kitten)); + unsigned *res = c->lits + c->size; + if (cadical_kitten->antecedents && is_learned_klause (c)) + res += c->aux; + return (klause *) res; +} + +/*------------------------------------------------------------------------*/ + +static void reset_core (cadical_kitten *cadical_kitten) { + LOG ("resetting core clauses"); + size_t reset = 0; + for (all_klauses (c)) + if (is_core_klause (c)) + unset_core_klause (c), reset++; + LOG ("reset %zu core clauses", reset); + CLEAR_STACK (cadical_kitten->core); +} + +static void reset_assumptions (cadical_kitten *cadical_kitten) { + LOG ("reset %zu assumptions", SIZE_STACK (cadical_kitten->assumptions)); + while (!EMPTY_STACK (cadical_kitten->assumptions)) { + const unsigned assumption = POP_STACK (cadical_kitten->assumptions); + cadical_kitten->failed[assumption] = false; + } +#ifndef CADICAL_NDEBUG + for (size_t i = 0; i < cadical_kitten->size; i++) + CADICAL_assert (!cadical_kitten->failed[i]); +#endif + CLEAR_STACK (cadical_kitten->assumptions); + if (cadical_kitten->failing != INVALID) { + ROG (cadical_kitten->failing, "reset failed assumption reason"); + cadical_kitten->failing = INVALID; + } +} + +static void reset_incremental (cadical_kitten *cadical_kitten) { + // if (cadical_kitten->level) + cadical_completely_backtrack_to_root_level (cadical_kitten); + if (!EMPTY_STACK (cadical_kitten->assumptions)) + reset_assumptions (cadical_kitten); + else + CADICAL_assert (cadical_kitten->failing == INVALID); + if (cadical_kitten->status == 21) + reset_core (cadical_kitten); + UPDATE_STATUS (0); +} + +/*------------------------------------------------------------------------*/ + +static bool flip_literal (cadical_kitten *cadical_kitten, unsigned lit) { + INC (cadical_kitten_flip); + signed char *values = cadical_kitten->values; + if (values[lit] < 0) + lit ^= 1; + LOG ("trying to flip value of satisfied literal %u", lit); + CADICAL_assert (values[lit] > 0); + katches *watches = cadical_kitten->watches + lit; + unsigned *q = BEGIN_STACK (*watches); + const unsigned *const end_watches = END_STACK (*watches); + unsigned const *p = q; + uint64_t ticks = (((char *) end_watches - (char *) q) >> 7) + 1; + bool res = true; + while (p != end_watches) { + const unsigned ref = *q++ = *p++; + klause *c = dereference_klause (cadical_kitten, ref); + unsigned *lits = c->lits; + const unsigned other = lits[0] ^ lits[1] ^ lit; + const value other_value = values[other]; + ticks++; + if (other_value > 0) + continue; + value replacement_value = -1; + unsigned replacement = INVALID; + const unsigned *const end_lits = lits + c->size; + unsigned *r; + for (r = lits + 2; r != end_lits; r++) { + replacement = *r; + CADICAL_assert (replacement != lit); + replacement_value = values[replacement]; + CADICAL_assert (replacement_value); + if (replacement_value > 0) + break; + } + if (replacement_value > 0) { + CADICAL_assert (replacement != INVALID); + ROG (ref, "unwatching %u in", lit); + lits[0] = other; + lits[1] = replacement; + *r = lit; + watch_klause (cadical_kitten, replacement, ref); + q--; + } else { + CADICAL_assert (replacement_value < 0); + ROG (ref, "single satisfied"); + res = false; + break; + } + } + while (p != end_watches) + *q++ = *p++; + SET_END_OF_STACK (*watches, q); + ADD (cadical_kitten_ticks, ticks); + if (res) { + LOG ("flipping value of %u", lit); + values[lit] = -1; + const unsigned not_lit = lit ^ 1; + values[not_lit] = 1; + INC (cadical_kitten_flipped); + } else + LOG ("failed to flip value of %u", lit); + return res; +} + +/*------------------------------------------------------------------------*/ + +// this cadical specific clause addition avoids copying clauses multiple +// times just to convert literals to unsigned representation. +// +static unsigned int2u (int lit) { + CADICAL_assert (lit != 0); + int idx = abs (lit) - 1; + return (lit < 0) + 2u * (unsigned) idx; +} + +void cadical_kitten_assume (cadical_kitten *cadical_kitten, unsigned elit) { + REQUIRE_INITIALIZED (); + if (cadical_kitten->status) + reset_incremental (cadical_kitten); + const unsigned ilit = import_literal (cadical_kitten, elit); + LOG ("registering assumption %u", ilit); + PUSH_STACK (cadical_kitten->assumptions, ilit); +} + +void cadical_kitten_assume_signed (cadical_kitten *cadical_kitten, int elit) { + unsigned kelit = int2u (elit); + cadical_kitten_assume (cadical_kitten, kelit); +} + +void cadical_kitten_clause_with_id_and_exception (cadical_kitten *cadical_kitten, unsigned id, + size_t size, + const unsigned *elits, + unsigned except) { + REQUIRE_INITIALIZED (); + if (cadical_kitten->status) + reset_incremental (cadical_kitten); + CADICAL_assert (EMPTY_STACK (cadical_kitten->klause)); + const unsigned *const end = elits + size; + for (const unsigned *p = elits; p != end; p++) { + const unsigned elit = *p; + if (elit == except) + continue; + const unsigned ilit = import_literal (cadical_kitten, elit); + CADICAL_assert (ilit < cadical_kitten->lits); + const unsigned iidx = ilit / 2; + if (cadical_kitten->marks[iidx]) + INVALID_API_USAGE ("variable '%u' of literal '%u' occurs twice", + elit / 2, elit); + cadical_kitten->marks[iidx] = true; + PUSH_STACK (cadical_kitten->klause, ilit); + } + for (unsigned *p = cadical_kitten->klause.begin; p != cadical_kitten->klause.end; p++) + cadical_kitten->marks[*p / 2] = false; + new_original_klause (cadical_kitten, id); + CLEAR_STACK (cadical_kitten->klause); +} + +void citten_clause_with_id_and_exception (cadical_kitten *cadical_kitten, unsigned id, + size_t size, const int *elits, + unsigned except) { + REQUIRE_INITIALIZED (); + if (cadical_kitten->status) + reset_incremental (cadical_kitten); + CADICAL_assert (EMPTY_STACK (cadical_kitten->klause)); + const int *const end = elits + size; + for (const int *p = elits; p != end; p++) { + const unsigned elit = int2u (*p); // this is the conversion + if (elit == except) + continue; + const unsigned ilit = import_literal (cadical_kitten, elit); + CADICAL_assert (ilit < cadical_kitten->lits); + const unsigned iidx = ilit / 2; + if (cadical_kitten->marks[iidx]) + INVALID_API_USAGE ("variable '%u' of literal '%u' occurs twice", + elit / 2, elit); + cadical_kitten->marks[iidx] = true; + PUSH_STACK (cadical_kitten->klause, ilit); + } + for (unsigned *p = cadical_kitten->klause.begin; p != cadical_kitten->klause.end; p++) + cadical_kitten->marks[*p / 2] = false; + new_original_klause (cadical_kitten, id); + CLEAR_STACK (cadical_kitten->klause); +} + +void citten_clause_with_id_and_equivalence (cadical_kitten *cadical_kitten, unsigned id, + size_t size, const int *elits, + unsigned lit, unsigned other) { + REQUIRE_INITIALIZED (); + if (cadical_kitten->status) + reset_incremental (cadical_kitten); + CADICAL_assert (EMPTY_STACK (cadical_kitten->klause)); + bool sat = false; + const int *const end = elits + size; + for (const int *p = elits; p != end; p++) { + const unsigned elit = int2u (*p); // this is the conversion + if (elit == (lit ^ 1u) || elit == (other ^ 1u)) + continue; + if (elit == lit || elit == other) { + sat = true; + break; + } + const unsigned ilit = import_literal (cadical_kitten, elit); + CADICAL_assert (ilit < cadical_kitten->lits); + const unsigned iidx = ilit / 2; + if (cadical_kitten->marks[iidx]) + INVALID_API_USAGE ("variable '%u' of literal '%u' occurs twice", + elit / 2, elit); + cadical_kitten->marks[iidx] = true; + PUSH_STACK (cadical_kitten->klause, ilit); + } + for (unsigned *p = cadical_kitten->klause.begin; p != cadical_kitten->klause.end; p++) + cadical_kitten->marks[*p / 2] = false; + if (!sat) + new_original_klause (cadical_kitten, id); + CLEAR_STACK (cadical_kitten->klause); +} + +void cadical_kitten_clause (cadical_kitten *cadical_kitten, size_t size, unsigned *elits) { + cadical_kitten_clause_with_id_and_exception (cadical_kitten, INVALID, size, elits, + INVALID); +} + +void citten_clause_with_id (cadical_kitten *cadical_kitten, unsigned id, size_t size, + int *elits) { + citten_clause_with_id_and_exception (cadical_kitten, id, size, elits, INVALID); +} + +void cadical_kitten_unit (cadical_kitten *cadical_kitten, unsigned lit) { + cadical_kitten_clause (cadical_kitten, 1, &lit); +} + +void cadical_kitten_binary (cadical_kitten *cadical_kitten, unsigned a, unsigned b) { + unsigned clause[2] = {a, b}; + cadical_kitten_clause (cadical_kitten, 2, clause); +} + +int cadical_kitten_solve (cadical_kitten *cadical_kitten) { + REQUIRE_INITIALIZED (); + if (cadical_kitten->status) + reset_incremental (cadical_kitten); + else // if (cadical_kitten->level) + cadical_completely_backtrack_to_root_level (cadical_kitten); + + LOG ("starting solving under %zu assumptions", + SIZE_STACK (cadical_kitten->assumptions)); + + INC (cadical_kitten_solved); + + int res = propagate_units (cadical_kitten); + while (!res) { + const unsigned conflict = propagate (cadical_kitten); + if (cadical_kitten->terminator && + cadical_kitten->terminator (cadical_kitten->terminator_data)) { + LOG ("terminator requested termination"); + res = -1; + break; + } + if (conflict != INVALID) { + if (cadical_kitten->level) + analyze (cadical_kitten, conflict); + else { + inconsistent (cadical_kitten, conflict); + res = 20; + } + } else + res = decide (cadical_kitten); + } + + if (res < 0) + res = 0; + + if (!res && !EMPTY_STACK (cadical_kitten->assumptions)) + reset_assumptions (cadical_kitten); + + UPDATE_STATUS (res); + + if (res == 10) + INC (cadical_kitten_sat); + else if (res == 20) + INC (cadical_kitten_unsat); + else + INC (cadical_kitten_unknown); + + LOG ("finished solving with result %d", res); + + return res; +} + +int cadical_kitten_status (cadical_kitten *cadical_kitten) { return cadical_kitten->status; } + +unsigned cadical_kitten_compute_clausal_core (cadical_kitten *cadical_kitten, + uint64_t *learned_ptr) { + REQUIRE_STATUS (20); + + if (!cadical_kitten->antecedents) + INVALID_API_USAGE ("antecedents not tracked"); + + LOG ("computing clausal core"); + + unsigneds *resolved = &cadical_kitten->resolved; + CADICAL_assert (EMPTY_STACK (*resolved)); + + unsigned original = 0; + uint64_t learned = 0; + + unsigned reason_ref = cadical_kitten->inconsistent; + + if (reason_ref == INVALID) { + CADICAL_assert (!EMPTY_STACK (cadical_kitten->assumptions)); + reason_ref = cadical_kitten->failing; + if (reason_ref == INVALID) { + LOG ("assumptions mutually inconsistent"); + //goto DONE; + if (learned_ptr) + *learned_ptr = learned; + + LOG ("clausal core of %u original clauses", original); + LOG ("clausal core of %" PRIu64 " learned clauses", learned); + cadical_kitten->statistics.original = original; + cadical_kitten->statistics.learned = 0; + UPDATE_STATUS (21); + + return original; + } + } + + PUSH_STACK (*resolved, reason_ref); + unsigneds *core = &cadical_kitten->core; + CADICAL_assert (EMPTY_STACK (*core)); + + while (!EMPTY_STACK (*resolved)) { + const unsigned c_ref = POP_STACK (*resolved); + if (c_ref == INVALID) { + const unsigned d_ref = POP_STACK (*resolved); + ROG (d_ref, "core[%zu]", SIZE_STACK (*core)); + PUSH_STACK (*core, d_ref); + klause *d = dereference_klause (cadical_kitten, d_ref); + CADICAL_assert (!is_core_klause (d)); + set_core_klause (d); + if (is_learned_klause (d)) + learned++; + else + original++; + } else { + klause *c = dereference_klause (cadical_kitten, c_ref); + if (is_core_klause (c)) + continue; + PUSH_STACK (*resolved, c_ref); + PUSH_STACK (*resolved, INVALID); + ROG (c_ref, "analyzing antecedent core"); + if (!is_learned_klause (c)) + continue; + for (all_antecedents (d_ref, c)) { + klause *d = dereference_klause (cadical_kitten, d_ref); + if (!is_core_klause (d)) + PUSH_STACK (*resolved, d_ref); + } + } + } + + //DONE: + + if (learned_ptr) + *learned_ptr = learned; + + LOG ("clausal core of %u original clauses", original); + LOG ("clausal core of %" PRIu64 " learned clauses", learned); + cadical_kitten->statistics.original = original; + cadical_kitten->statistics.learned = 0; + UPDATE_STATUS (21); + + return original; +} + +void cadical_kitten_traverse_core_ids (cadical_kitten *cadical_kitten, void *state, + void (*traverse) (void *, unsigned)) { + REQUIRE_STATUS (21); + + LOG ("traversing core of original clauses"); + + unsigned traversed = 0; + + for (all_original_klauses (c)) { + // only happens for 'true' incremental calls, i.e. if add happens after + // solve + if (is_learned_klause (c)) + continue; + if (!is_core_klause (c)) + continue; + ROG (reference_klause (cadical_kitten, c), "traversing"); + traverse (state, c->aux); + traversed++; + } + + LOG ("traversed %u original core clauses", traversed); + (void) traversed; + + CADICAL_assert (cadical_kitten->status == 21); +} + +void cadical_kitten_traverse_core_clauses (cadical_kitten *cadical_kitten, void *state, + void (*traverse) (void *, bool, size_t, + const unsigned *)) { + REQUIRE_STATUS (21); + + LOG ("traversing clausal core"); + + unsigned traversed = 0; + + for (all_stack (unsigned, c_ref, cadical_kitten->core)) { + klause *c = dereference_klause (cadical_kitten, c_ref); + CADICAL_assert (is_core_klause (c)); + const bool learned = is_learned_klause (c); + unsigneds *eclause = &cadical_kitten->eclause; + CADICAL_assert (EMPTY_STACK (*eclause)); + for (all_literals_in_klause (ilit, c)) { + const unsigned elit = export_literal (cadical_kitten, ilit); + PUSH_STACK (*eclause, elit); + } + const size_t size = SIZE_STACK (*eclause); + const unsigned *elits = eclause->begin; + ROG (reference_klause (cadical_kitten, c), "traversing"); + traverse (state, learned, size, elits); + CLEAR_STACK (*eclause); + traversed++; + } + + LOG ("traversed %u core clauses", traversed); + (void) traversed; + + CADICAL_assert (cadical_kitten->status == 21); +} + +void cadical_kitten_traverse_core_clauses_with_id ( + cadical_kitten *cadical_kitten, void *state, + void (*traverse) (void *state, unsigned, bool learned, size_t, + const unsigned *)) { + REQUIRE_STATUS (21); + + LOG ("traversing clausal core"); + + unsigned traversed = 0; + + for (all_stack (unsigned, c_ref, cadical_kitten->core)) { + klause *c = dereference_klause (cadical_kitten, c_ref); + CADICAL_assert (is_core_klause (c)); + const bool learned = is_learned_klause (c); + unsigneds *eclause = &cadical_kitten->eclause; + CADICAL_assert (EMPTY_STACK (*eclause)); + for (all_literals_in_klause (ilit, c)) { + const unsigned elit = export_literal (cadical_kitten, ilit); + PUSH_STACK (*eclause, elit); + } + const size_t size = SIZE_STACK (*eclause); + const unsigned *elits = eclause->begin; + ROG (reference_klause (cadical_kitten, c), "traversing"); + unsigned ctag = learned ? 0 : c->aux; + traverse (state, ctag, learned, size, elits); + CLEAR_STACK (*eclause); + traversed++; + } + + LOG ("traversed %u core clauses", traversed); + (void) traversed; + + CADICAL_assert (cadical_kitten->status == 21); +} + +void cadical_kitten_trace_core (cadical_kitten *cadical_kitten, void *state, + void (*trace) (void *, unsigned, unsigned, bool, + size_t, const unsigned *, size_t, + const unsigned *)) { + REQUIRE_STATUS (21); + + LOG ("tracing clausal core"); + + unsigned traced = 0; + + for (all_stack (unsigned, c_ref, cadical_kitten->core)) { + klause *c = dereference_klause (cadical_kitten, c_ref); + CADICAL_assert (is_core_klause (c)); + const bool learned = is_learned_klause (c); + unsigneds *eclause = &cadical_kitten->eclause; + CADICAL_assert (EMPTY_STACK (*eclause)); + for (all_literals_in_klause (ilit, c)) { + const unsigned elit = export_literal (cadical_kitten, ilit); + PUSH_STACK (*eclause, elit); + } + const size_t size = SIZE_STACK (*eclause); + const unsigned *elits = eclause->begin; + + unsigneds *resolved = &cadical_kitten->resolved; + CADICAL_assert (EMPTY_STACK (*resolved)); + if (learned) { + for (all_antecedents (ref, c)) { + PUSH_STACK (*resolved, ref); + } + } + const size_t rsize = SIZE_STACK (*resolved); + const unsigned *rids = resolved->begin; + + unsigned cid = reference_klause (cadical_kitten, c); + unsigned ctag = learned ? 0 : c->aux; + ROG (cid, "tracing"); + trace (state, cid, ctag, learned, size, elits, rsize, rids); + CLEAR_STACK (*eclause); + CLEAR_STACK (*resolved); + traced++; + } + + LOG ("traced %u core clauses", traced); + (void) traced; + + CADICAL_assert (cadical_kitten->status == 21); +} + +void cadical_kitten_shrink_to_clausal_core (cadical_kitten *cadical_kitten) { + REQUIRE_STATUS (21); + + LOG ("shrinking formula to core of original clauses"); + + CLEAR_STACK (cadical_kitten->trail); + + cadical_kitten->unassigned = cadical_kitten->lits / 2; + cadical_kitten->propagated = 0; + cadical_kitten->level = 0; + + update_search (cadical_kitten, cadical_kitten->queue.last); + + memset (cadical_kitten->values, 0, cadical_kitten->lits); + + for (all_kits (lit)) + CLEAR_STACK (KATCHES (lit)); + + CADICAL_assert (cadical_kitten->inconsistent != INVALID); + klause *inconsistent = dereference_klause (cadical_kitten, cadical_kitten->inconsistent); + if (is_learned_klause (inconsistent) || inconsistent->size) { + ROG (cadical_kitten->inconsistent, "resetting inconsistent"); + cadical_kitten->inconsistent = INVALID; + } else + ROG (cadical_kitten->inconsistent, "keeping inconsistent"); + + CLEAR_STACK (cadical_kitten->units); + + klause *begin = begin_klauses (cadical_kitten), *q = begin; + klause const *const end = end_original_klauses (cadical_kitten); +#ifdef LOGGING + unsigned original = 0; +#endif + for (klause *c = begin, *next; c != end; c = next) { + next = next_klause (cadical_kitten, c); + // CADICAL_assert (!is_learned_klause (c)); not necessarily true + if (is_learned_klause (c)) + continue; + if (!is_core_klause (c)) + continue; + unset_core_klause (c); + const unsigned dst = (unsigned *) q - (unsigned *) begin; + const unsigned size = c->size; + if (!size) { + if (cadical_kitten->inconsistent != INVALID) + cadical_kitten->inconsistent = dst; + } else if (size == 1) { + PUSH_STACK (cadical_kitten->units, dst); + ROG (dst, "keeping"); + } else { + watch_klause (cadical_kitten, c->lits[0], dst); + watch_klause (cadical_kitten, c->lits[1], dst); + } + if (c == q) + q = next; + else { + const size_t bytes = (char *) next - (char *) c; + memmove (q, c, bytes); + q = (klause *) ((char *) q + bytes); + } +#ifdef LOGGING + original++; +#endif + } + SET_END_OF_STACK (cadical_kitten->klauses, (unsigned *) q); + cadical_kitten->end_original_ref = SIZE_STACK (cadical_kitten->klauses); + LOG ("end of original clauses at %zu", cadical_kitten->end_original_ref); + LOG ("%u original clauses left", original); + + CLEAR_STACK (cadical_kitten->core); + + UPDATE_STATUS (0); +} + +signed char cadical_kitten_signed_value (cadical_kitten *cadical_kitten, int selit) { + REQUIRE_STATUS (10); + const unsigned elit = int2u (selit); + const unsigned eidx = elit / 2; + if (eidx >= cadical_kitten->evars) + return 0; + unsigned iidx = cadical_kitten->import[eidx]; + if (!iidx) + return 0; + const unsigned ilit = 2 * (iidx - 1) + (elit & 1); + return cadical_kitten->values[ilit]; +} + +signed char cadical_kitten_value (cadical_kitten *cadical_kitten, unsigned elit) { + REQUIRE_STATUS (10); + const unsigned eidx = elit / 2; + if (eidx >= cadical_kitten->evars) + return 0; + unsigned iidx = cadical_kitten->import[eidx]; + if (!iidx) + return 0; + const unsigned ilit = 2 * (iidx - 1) + (elit & 1); + return cadical_kitten->values[ilit]; +} + +signed char cadical_kitten_fixed (cadical_kitten *cadical_kitten, unsigned elit) { + const unsigned eidx = elit / 2; + if (eidx >= cadical_kitten->evars) + return 0; + unsigned iidx = cadical_kitten->import[eidx]; + if (!iidx) + return 0; + iidx--; + const unsigned ilit = 2 * iidx + (elit & 1); + signed char res = cadical_kitten->values[ilit]; + if (!res) + return 0; + kar *v = cadical_kitten->vars + iidx; + if (v->level) + return 0; + return res; +} + +signed char cadical_kitten_fixed_signed (cadical_kitten *cadical_kitten, int elit) { + unsigned kelit = int2u (elit); + return cadical_kitten_fixed (cadical_kitten, kelit); +} + +bool cadical_kitten_flip_literal (cadical_kitten *cadical_kitten, unsigned elit) { + REQUIRE_STATUS (10); + const unsigned eidx = elit / 2; + if (eidx >= cadical_kitten->evars) + return false; + unsigned iidx = cadical_kitten->import[eidx]; + if (!iidx) + return false; + const unsigned ilit = 2 * (iidx - 1) + (elit & 1); + if (cadical_kitten_fixed (cadical_kitten, elit)) + return false; + return flip_literal (cadical_kitten, ilit); +} + +bool cadical_kitten_flip_signed_literal (cadical_kitten *cadical_kitten, int elit) { + REQUIRE_STATUS (10); + unsigned kelit = int2u (elit); + return cadical_kitten_flip_literal (cadical_kitten, kelit); +} + +bool cadical_kitten_failed (cadical_kitten *cadical_kitten, unsigned elit) { + REQUIRE_STATUS (20); + const unsigned eidx = elit / 2; + if (eidx >= cadical_kitten->evars) + return false; + unsigned iidx = cadical_kitten->import[eidx]; + if (!iidx) + return false; + const unsigned ilit = 2 * (iidx - 1) + (elit & 1); + return cadical_kitten->failed[ilit]; +} + +// checks both watches for clauses with only one literal positively +// assigned. if such a clause is found, return false. Otherwise fix watch +// invariant and return true +static bool prime_propagate (cadical_kitten *cadical_kitten, const unsigned idx, + void *state, const bool ignoring, + bool (*ignore) (void *, unsigned)) { + unsigned lit = 2 * idx; + unsigned conflict = INVALID; + value *values = cadical_kitten->values; + for (int i = 0; i < 2; i++) { + if (conflict != INVALID) + break; + lit = lit ^ i; + const unsigned not_lit = lit ^ 1; + katches *watches = cadical_kitten->watches + not_lit; + unsigned *q = BEGIN_STACK (*watches); + const unsigned *const end_watches = END_STACK (*watches); + unsigned const *p = q; + uint64_t ticks = (((char *) end_watches - (char *) q) >> 7) + 1; + while (p != end_watches) { + const unsigned ref = *q++ = *p++; + klause *c = dereference_klause (cadical_kitten, ref); + if (is_learned_klause (c) || ignore (state, c->aux) == ignoring) + continue; + CADICAL_assert (c->size > 1); + unsigned *lits = c->lits; + const unsigned other = lits[0] ^ lits[1] ^ not_lit; + const value other_value = values[other]; + ticks++; + if (other_value > 0) + continue; + value replacement_value = -1; + unsigned replacement = INVALID; + const unsigned *const end_lits = lits + c->size; + unsigned *r; + for (r = lits + 2; r != end_lits; r++) { + replacement = *r; + replacement_value = values[replacement]; + if (replacement_value > 0) + break; + } + if (replacement_value > 0) { + CADICAL_assert (replacement != INVALID); + ROG (ref, "unwatching %u in", not_lit); + lits[0] = other; + lits[1] = replacement; + *r = not_lit; + watch_klause (cadical_kitten, replacement, ref); + q--; + } else { + ROG (ref, "idx %u forced prime by", idx); + conflict = ref; + break; + } + } + while (p != end_watches) + *q++ = *p++; + SET_END_OF_STACK (*watches, q); + ADD (cadical_kitten_ticks, ticks); + } + return conflict == INVALID; +} + +void cadical_kitten_add_prime_implicant (cadical_kitten *cadical_kitten, void *state, int side, + void (*add_implicant) (void *, int, size_t, + const unsigned *)) { + REQUIRE_STATUS (11); + // might be possible in some edge cases + unsigneds *prime = &cadical_kitten->prime[side]; + unsigneds *prime2 = &cadical_kitten->prime[!side]; + CADICAL_assert (!EMPTY_STACK (*prime) || !EMPTY_STACK (*prime2)); + CLEAR_STACK (*prime2); + + for (all_stack (unsigned, lit, *prime)) { + const unsigned not_lit = lit ^ 1; + const unsigned elit = export_literal (cadical_kitten, not_lit); + PUSH_STACK (*prime2, elit); + } + + // adds a clause which will reset cadical_kitten status and backtrack + add_implicant (state, side, SIZE_STACK (*prime2), BEGIN_STACK (*prime2)); + CLEAR_STACK (*prime); + CLEAR_STACK (*prime2); +} + +// computes two prime implicants, only considering clauses based on ignore +// return -1 if no prime implicant has been computed, otherwise returns +// index of shorter implicant. +// TODO does not work if flip has been called beforehand +int cadical_kitten_compute_prime_implicant (cadical_kitten *cadical_kitten, void *state, + bool (*ignore) (void *, unsigned)) { + REQUIRE_STATUS (10); + + value *values = cadical_kitten->values; + kar *vars = cadical_kitten->vars; + unsigneds unassigned; + INIT_STACK (unassigned); + bool limit_hit = 0; + CADICAL_assert (EMPTY_STACK (cadical_kitten->prime[0]) && EMPTY_STACK (cadical_kitten->prime[1])); + for (int i = 0; i < 2; i++) { + const bool ignoring = i; + for (all_stack (unsigned, lit, cadical_kitten->trail)) { + if (KITTEN_TICKS >= cadical_kitten->limits.ticks) { + LOG ("ticks limit %" PRIu64 " hit after %" PRIu64 " ticks", + cadical_kitten->limits.ticks, KITTEN_TICKS); + limit_hit = 1; + break; + } + CADICAL_assert (values[lit] > 0); + const unsigned idx = lit / 2; + const unsigned ref = vars[idx].reason; + CADICAL_assert (vars[idx].level); + klause *c = 0; + if (ref != INVALID) + c = dereference_klause (cadical_kitten, ref); + if (ref == INVALID || is_learned_klause (c) || + ignore (state, c->aux) == ignoring) { + LOG ("non-prime candidate var %d", idx); + if (prime_propagate (cadical_kitten, idx, state, ignoring, ignore)) { + values[lit] = 0; + values[lit ^ 1] = 0; + PUSH_STACK (unassigned, lit); + } else + CADICAL_assert (values[lit] > 0); + } + } + unsigneds *prime = &cadical_kitten->prime[i]; + // push on prime implicant stack. + for (all_kits (lit)) { + if (values[lit] > 0) + PUSH_STACK (*prime, lit); + } + // reassign all literals on + for (all_stack (unsigned, lit, unassigned)) { + CADICAL_assert (!values[lit]); + values[lit] = 1; + values[lit ^ 1] = -1; + } + CLEAR_STACK (unassigned); + } + RELEASE_STACK (unassigned); + + if (limit_hit) { + CLEAR_STACK (cadical_kitten->prime[0]); + CLEAR_STACK (cadical_kitten->prime[1]); + return -1; + } + // the only case when one of the prime implicants is allowed to be empty + // is if ignore returns always true or always false. + CADICAL_assert (!EMPTY_STACK (cadical_kitten->prime[0]) || + !EMPTY_STACK (cadical_kitten->prime[1])); + UPDATE_STATUS (11); + + int res = SIZE_STACK (cadical_kitten->prime[0]) > SIZE_STACK (cadical_kitten->prime[1]); + return res; +} + +static bool contains_blit (cadical_kitten *cadical_kitten, klause *c, const unsigned blit) { + for (all_literals_in_klause (lit, c)) { + if (lit == blit) + return true; + } + return false; +} + +static bool prime_propagate_blit (cadical_kitten *cadical_kitten, const unsigned idx, + const unsigned blit) { + unsigned lit = 2 * idx; + unsigned conflict = INVALID; + value *values = cadical_kitten->values; + LOG ("prime propagating idx %u for blit %u", idx, blit); + for (int i = 0; i < 2; i++) { + if (conflict != INVALID) + break; + lit = lit ^ i; + if (lit == blit) + continue; + const unsigned not_lit = lit ^ 1; + katches *watches = cadical_kitten->watches + not_lit; + unsigned *q = BEGIN_STACK (*watches); + const unsigned *const end_watches = END_STACK (*watches); + unsigned const *p = q; + uint64_t ticks = (((char *) end_watches - (char *) q) >> 7) + 1; + while (p != end_watches) { + const unsigned ref = *q++ = *p++; + klause *c = dereference_klause (cadical_kitten, ref); + if (is_learned_klause (c)) + continue; + ROG (ref, "checking with blit %u", blit); + CADICAL_assert (c->size > 1); + unsigned *lits = c->lits; + const unsigned other = lits[0] ^ lits[1] ^ not_lit; + const value other_value = values[other]; + ticks++; + bool use = other == blit || not_lit == blit; + if (other_value > 0) + continue; + value replacement_value = -1; + unsigned replacement = INVALID; + const unsigned *const end_lits = lits + c->size; + unsigned *r; + for (r = lits + 2; r != end_lits; r++) { + replacement = *r; + replacement_value = values[replacement]; + use = use || replacement == blit; + if (replacement_value > 0) + break; + } + if (replacement_value > 0) { + CADICAL_assert (replacement != INVALID); + ROG (ref, "unwatching %u in", not_lit); + lits[0] = other; + lits[1] = replacement; + *r = not_lit; + watch_klause (cadical_kitten, replacement, ref); + q--; + } else if (!use) { + continue; + } else { + ROG (ref, "idx %u forced prime by", idx); + conflict = ref; + break; + } + } + while (p != end_watches) + *q++ = *p++; + SET_END_OF_STACK (*watches, q); + ADD (cadical_kitten_ticks, ticks); + } + return conflict == INVALID; +} + +static int compute_prime_implicant_for (cadical_kitten *cadical_kitten, unsigned blit) { + value *values = cadical_kitten->values; + kar *vars = cadical_kitten->vars; + unsigneds unassigned; + INIT_STACK (unassigned); + bool limit_hit = false; + CADICAL_assert (EMPTY_STACK (cadical_kitten->prime[0]) && EMPTY_STACK (cadical_kitten->prime[1])); + for (int i = 0; i < 2; i++) { + const unsigned block = blit ^ i; + const bool ignoring = i; + if (prime_propagate_blit (cadical_kitten, block / 2, block)) { + value tmp = values[blit]; + CADICAL_assert (tmp); + values[blit] = 0; + values[blit ^ 1] = 0; + PUSH_STACK (unassigned, tmp > 0 ? blit : blit ^ 1); + PUSH_STACK (cadical_kitten->prime[i], block); // will be negated! + } else + CADICAL_assert (false); + for (all_stack (unsigned, lit, cadical_kitten->trail)) { + if (KITTEN_TICKS >= cadical_kitten->limits.ticks) { + LOG ("ticks limit %" PRIu64 " hit after %" PRIu64 " ticks", + cadical_kitten->limits.ticks, KITTEN_TICKS); + limit_hit = true; + break; + } + if (!values[lit]) + continue; + CADICAL_assert (values[lit]); // not true when flipping is involved + const unsigned idx = lit / 2; + const unsigned ref = vars[idx].reason; + CADICAL_assert (vars[idx].level); + LOG ("non-prime candidate var %d", idx); + if (prime_propagate_blit (cadical_kitten, idx, block)) { + value tmp = values[lit]; + CADICAL_assert (tmp); + values[lit] = 0; + values[lit ^ 1] = 0; + PUSH_STACK (unassigned, tmp > 0 ? lit : lit ^ 1); + } + } + unsigneds *prime = &cadical_kitten->prime[i]; + // push on prime implicant stack. + for (all_kits (lit)) { + if (values[lit] > 0) + PUSH_STACK (*prime, lit); + } + // reassign all literals on + for (all_stack (unsigned, lit, unassigned)) { + CADICAL_assert (!values[lit]); + values[lit] = 1; + values[lit ^ 1] = -1; + } + CLEAR_STACK (unassigned); + } + RELEASE_STACK (unassigned); + + if (limit_hit) { + CLEAR_STACK (cadical_kitten->prime[0]); + CLEAR_STACK (cadical_kitten->prime[1]); + return -1; + } + // the only case when one of the prime implicants is allowed to be empty + // is if ignore returns always true or always false. + CADICAL_assert (!EMPTY_STACK (cadical_kitten->prime[0]) || + !EMPTY_STACK (cadical_kitten->prime[1])); + LOGLITS (BEGIN_STACK (cadical_kitten->prime[0]), SIZE_STACK (cadical_kitten->prime[0]), + "first implicant %u", blit); + LOGLITS (BEGIN_STACK (cadical_kitten->prime[1]), SIZE_STACK (cadical_kitten->prime[1]), + "second implicant %u", blit ^ 1); + UPDATE_STATUS (11); + + int res = SIZE_STACK (cadical_kitten->prime[0]) > SIZE_STACK (cadical_kitten->prime[1]); + return res; +} + +int cadical_kitten_flip_and_implicant_for_signed_literal (cadical_kitten *cadical_kitten, + int elit) { + REQUIRE_STATUS (10); + unsigned kelit = int2u (elit); + if (!cadical_kitten_flip_literal (cadical_kitten, kelit)) { + return -2; + } + const unsigned eidx = kelit / 2; + unsigned iidx = cadical_kitten->import[eidx]; + CADICAL_assert (iidx); + const unsigned ilit = 2 * (iidx - 1) + (kelit & 1); + return compute_prime_implicant_for (cadical_kitten, ilit); +} + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/lidruptracer.cpp b/src/sat/cadical/cadical_lidruptracer.cpp similarity index 91% rename from src/sat/cadical/lidruptracer.cpp rename to src/sat/cadical/cadical_lidruptracer.cpp index 453f87df4..db07e36e6 100644 --- a/src/sat/cadical/lidruptracer.cpp +++ b/src/sat/cadical/cadical_lidruptracer.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -7,7 +11,7 @@ namespace CaDiCaL { LidrupTracer::LidrupTracer (Internal *i, File *f, bool b) : internal (i), file (f), binary (b), num_clauses (0), size_clauses (0), clauses (0), last_hash (0), last_id (0), last_clause (0) -#ifndef QUIET +#ifndef CADICAL_QUIET , added (0), deleted (0) #endif @@ -21,10 +25,10 @@ LidrupTracer::LidrupTracer (Internal *i, File *f, bool b) uint64_t nonce = random.next (); if (!(nonce & 1)) nonce++; - assert (nonce), assert (nonce & 1); + CADICAL_assert (nonce), CADICAL_assert (nonce & 1); nonces[n] = nonce; } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG binary = b; #else (void) b; @@ -50,7 +54,7 @@ LidrupTracer::~LidrupTracer () { /*------------------------------------------------------------------------*/ void LidrupTracer::enlarge_clauses () { - assert (num_clauses == size_clauses); + CADICAL_assert (num_clauses == size_clauses); const uint64_t new_size_clauses = size_clauses ? 2 * size_clauses : 1; LOG ("LIDRUP Tracer enlarging clauses of tracer from %" PRIu64 " to %" PRIu64, @@ -88,13 +92,13 @@ LidrupClause *LidrupTracer::new_clause () { } void LidrupTracer::delete_clause (LidrupClause *c) { - assert (c); + CADICAL_assert (c); num_clauses--; delete c; } uint64_t LidrupTracer::reduce_hash (uint64_t hash, uint64_t size) { - assert (size > 0); + CADICAL_assert (size > 0); unsigned shift = 32; uint64_t res = hash; while ((((uint64_t) 1) << shift) > size) { @@ -102,12 +106,12 @@ uint64_t LidrupTracer::reduce_hash (uint64_t hash, uint64_t size) { shift >>= 1; } res &= size - 1; - assert (res < size); + CADICAL_assert (res < size); return res; } uint64_t LidrupTracer::compute_hash (const int64_t id) { - assert (id > 0); + CADICAL_assert (id > 0); unsigned j = id % num_nonces; uint64_t tmp = nonces[j] * (uint64_t) id; return last_hash = tmp; @@ -128,7 +132,7 @@ bool LidrupTracer::find_and_delete (const int64_t id) { } if (!c) return false; - assert (c && res); + CADICAL_assert (c && res); *res = c->next; for (auto &lit : c->literals) { imported_clause.push_back (lit); @@ -157,15 +161,15 @@ inline void LidrupTracer::flush_if_piping () { } inline void LidrupTracer::put_binary_zero () { - assert (binary); - assert (file); + CADICAL_assert (binary); + CADICAL_assert (file); file->put ((unsigned char) 0); } inline void LidrupTracer::put_binary_lit (int lit) { - assert (binary); - assert (file); - assert (lit != INT_MIN); + CADICAL_assert (binary); + CADICAL_assert (file); + CADICAL_assert (lit != INT_MIN); unsigned x = 2 * abs (lit) + (lit < 0); unsigned char ch; while (x & ~0x7f) { @@ -178,8 +182,8 @@ inline void LidrupTracer::put_binary_lit (int lit) { } inline void LidrupTracer::put_binary_id (int64_t id, bool can_be_negative) { - assert (binary); - assert (file); + CADICAL_assert (binary); + CADICAL_assert (file); uint64_t x = abs (id); if (can_be_negative) { x = 2 * x + (id < 0); @@ -256,7 +260,7 @@ void LidrupTracer::lidrup_add_original_clause (int64_t id, } void LidrupTracer::lidrup_batch_weaken_restore_and_delete () { - assert (batch_weaken.empty () || batch_delete.empty ()); + CADICAL_assert (batch_weaken.empty () || batch_delete.empty ()); if (!batch_weaken.empty ()) { if (binary) { file->put ('w'); @@ -274,7 +278,7 @@ void LidrupTracer::lidrup_batch_weaken_restore_and_delete () { put_binary_zero (); else file->put ("0\n"); -#ifndef QUIET +#ifndef CADICAL_QUIET batched++; #endif } @@ -295,7 +299,7 @@ void LidrupTracer::lidrup_batch_weaken_restore_and_delete () { put_binary_zero (); else file->put ("0\n"); -#ifndef QUIET +#ifndef CADICAL_QUIET batched++; #endif } @@ -316,7 +320,7 @@ void LidrupTracer::lidrup_batch_weaken_restore_and_delete () { put_binary_zero (); else file->put ("0\n"); -#ifndef QUIET +#ifndef CADICAL_QUIET batched++; #endif } @@ -341,8 +345,8 @@ void LidrupTracer::lidrup_conclude_and_delete ( else file->put ("u "); if (!find_and_delete (id)) { - assert (imported_clause.empty ()); - assert (conclusion.size () == 1); + CADICAL_assert (imported_clause.empty ()); + CADICAL_assert (conclusion.size () == 1); if (binary) { put_binary_zero (); put_binary_id (id); @@ -463,10 +467,10 @@ void LidrupTracer::add_derived_clause (int64_t id, bool, const vector &chain) { if (file->closed ()) return; - assert (imported_clause.empty ()); + CADICAL_assert (imported_clause.empty ()); LOG (clause, "LIDRUP TRACER tracing addition of derived clause"); lidrup_add_derived_clause (id, clause, chain); -#ifndef QUIET +#ifndef CADICAL_QUIET added++; #endif } @@ -476,7 +480,7 @@ void LidrupTracer::add_assumption_clause (int64_t id, const vector &chain) { if (file->closed ()) return; - assert (imported_clause.empty ()); + CADICAL_assert (imported_clause.empty ()); LOG (clause, "LIDRUP TRACER tracing addition of assumption clause[%" PRId64 "]", id); @@ -493,21 +497,21 @@ void LidrupTracer::add_assumption_clause (int64_t id, void LidrupTracer::delete_clause (int64_t id, bool, const vector &) { if (file->closed ()) return; - assert (imported_clause.empty ()); + CADICAL_assert (imported_clause.empty ()); LOG ("LIDRUP TRACER tracing deletion of clause[%" PRId64 "]", id); if (find_and_delete (id)) { - assert (imported_clause.empty ()); + CADICAL_assert (imported_clause.empty ()); if (!batch_delete.empty () || !batch_restore.empty ()) lidrup_batch_weaken_restore_and_delete (); batch_weaken.push_back (id); -#ifndef QUIET +#ifndef CADICAL_QUIET weakened++; #endif } else { if (!batch_weaken.empty () || !batch_restore.empty ()) lidrup_batch_weaken_restore_and_delete (); batch_delete.push_back (id); -#ifndef QUIET +#ifndef CADICAL_QUIET deleted++; #endif } @@ -516,7 +520,7 @@ void LidrupTracer::delete_clause (int64_t id, bool, const vector &) { void LidrupTracer::weaken_minus (int64_t id, const vector &) { if (file->closed ()) return; - assert (imported_clause.empty ()); + CADICAL_assert (imported_clause.empty ()); LOG ("LIDRUP TRACER tracing weaken minus of clause[%" PRId64 "]", id); last_id = id; insert (); @@ -526,7 +530,7 @@ void LidrupTracer::conclude_unsat (ConclusionType, const vector &conclusion) { if (file->closed ()) return; - assert (imported_clause.empty ()); + CADICAL_assert (imported_clause.empty ()); LOG (conclusion, "LIDRUP TRACER tracing conclusion of clause(s)"); lidrup_conclude_and_delete (conclusion); } @@ -538,12 +542,12 @@ void LidrupTracer::add_original_clause (int64_t id, bool, return; if (!restored) { LOG (clause, "LIDRUP TRACER tracing addition of original clause"); -#ifndef QUIET +#ifndef CADICAL_QUIET original++; #endif return lidrup_add_original_clause (id, clause); } - assert (restored); + CADICAL_assert (restored); if (find_and_delete (id)) { LOG (clause, "LIDRUP TRACER the clause was not yet weakened, so no restore"); @@ -551,7 +555,7 @@ void LidrupTracer::add_original_clause (int64_t id, bool, } LOG (clause, "LIDRUP TRACER tracing addition of restored clause"); lidrup_add_restored_clause (id); -#ifndef QUIET +#ifndef CADICAL_QUIET restore++; #endif } @@ -582,7 +586,7 @@ void LidrupTracer::solve_query () { return; LOG (assumptions, "LIDRUP TRACER tracing solve query with assumptions"); lidrup_solve_query (); -#ifndef QUIET +#ifndef CADICAL_QUIET solved++; #endif } @@ -601,7 +605,7 @@ void LidrupTracer::reset_assumptions () { bool LidrupTracer::closed () { return file->closed (); } -#ifndef QUIET +#ifndef CADICAL_QUIET void LidrupTracer::print_statistics () { // TODO complete this. @@ -627,9 +631,9 @@ void LidrupTracer::print_statistics () { #endif void LidrupTracer::close (bool print) { - assert (!closed ()); + CADICAL_assert (!closed ()); file->close (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { MSG ("LIDRUP proof file '%s' closed", file->name ()); print_statistics (); @@ -640,10 +644,10 @@ void LidrupTracer::close (bool print) { } void LidrupTracer::flush (bool print) { - assert (!closed ()); + CADICAL_assert (!closed ()); lidrup_batch_weaken_restore_and_delete (); file->flush (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { MSG ("LIDRUP proof file '%s' flushed", file->name ()); print_statistics (); @@ -654,3 +658,5 @@ void LidrupTracer::flush (bool print) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/limit.cpp b/src/sat/cadical/cadical_limit.cpp similarity index 97% rename from src/sat/cadical/limit.cpp rename to src/sat/cadical/cadical_limit.cpp index 8a5259b2a..69417a473 100644 --- a/src/sat/cadical/limit.cpp +++ b/src/sat/cadical/cadical_limit.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { Limit::Limit () { memset (this, 0, sizeof *this); } @@ -127,3 +131,5 @@ void Internal::reset_limits () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/logging.cpp b/src/sat/cadical/cadical_logging.cpp similarity index 98% rename from src/sat/cadical/logging.cpp rename to src/sat/cadical/cadical_logging.cpp index a677a99e1..589bf097e 100644 --- a/src/sat/cadical/logging.cpp +++ b/src/sat/cadical/cadical_logging.cpp @@ -1,7 +1,11 @@ +#include "global.h" + #ifdef LOGGING #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void Logger::print_log_prefix (Internal *internal) { @@ -212,4 +216,6 @@ string Logger::loglit (Internal *internal, int lit) { } } // namespace CaDiCaL +ABC_NAMESPACE_IMPL_END + #endif diff --git a/src/sat/cadical/lookahead.cpp b/src/sat/cadical/cadical_lookahead.cpp similarity index 96% rename from src/sat/cadical/lookahead.cpp rename to src/sat/cadical/cadical_lookahead.cpp index 3d60a9a01..de84858cc 100644 --- a/src/sat/cadical/lookahead.cpp +++ b/src/sat/cadical/cadical_lookahead.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { struct literal_occ { @@ -93,7 +97,7 @@ struct probe_negated_noccs_rank { void Internal::lookahead_flush_probes () { - assert (!probes.empty ()); + CADICAL_assert (!probes.empty ()); init_noccs (); for (const auto &c : clauses) { @@ -116,14 +120,14 @@ void Internal::lookahead_flush_probes () { continue; if (have_pos_bin_occs) lit = -lit; - assert (!noccs (lit)), assert (noccs (-lit) > 0); + CADICAL_assert (!noccs (lit)), CADICAL_assert (noccs (-lit) > 0); if (propfixed (lit) >= stats.all.fixed) continue; MSG ("keeping probe %d negated occs %" PRId64 "", lit, noccs (-lit)); *j++ = lit; } size_t remain = j - probes.begin (); -#ifndef QUIET +#ifndef CADICAL_QUIET size_t flushed = probes.size () - remain; #endif probes.resize (remain); @@ -140,7 +144,7 @@ void Internal::lookahead_flush_probes () { void Internal::lookahead_generate_probes () { - assert (probes.empty ()); + CADICAL_assert (probes.empty ()); // First determine all the literals which occur in binary clauses. It is // way faster to go over the clauses once, instead of walking the watch @@ -294,7 +298,7 @@ int Internal::lookahead_probing () { termination_forced = false; -#ifndef QUIET +#ifndef CADICAL_QUIET int old_failed = stats.failed; int64_t old_probed = stats.probed; #endif @@ -335,7 +339,7 @@ int Internal::lookahead_probing () { for (int idx = 1; idx <= max_var; idx++) propfixed (idx) = propfixed (-idx) = -1; - assert (unsat || propagated == trail.size ()); + CADICAL_assert (unsat || propagated == trail.size ()); propagated = propagated2 = trail.size (); int probe; @@ -382,7 +386,7 @@ int Internal::lookahead_probing () { sort_watches (); } -#ifndef QUIET +#ifndef CADICAL_QUIET int failed = stats.failed - old_failed; int64_t probed = stats.probed - old_probed; #endif @@ -435,12 +439,12 @@ CubesWithStatus Internal::generate_cubes (int depth, int min_depth) { reset_limits (); MSG ("generate cubes with %zu assumptions\n", assumptions.size ()); - assert (ntab.empty ()); + CADICAL_assert (ntab.empty ()); std::vector current_assumptions{assumptions}; std::vector> cubes{{assumptions}}; auto loccs{lookahead_populate_locc ()}; LOG ("loccs populated\n"); - assert (ntab.empty ()); + CADICAL_assert (ntab.empty ()); for (int i = 0; i < depth; ++i) { LOG ("Probing at depth %i, currently %zu have been generated", i, @@ -449,8 +453,8 @@ CubesWithStatus Internal::generate_cubes (int depth, int min_depth) { cubes.clear (); for (size_t j = 0; j < cubes2.size (); ++j) { - assert (ntab.empty ()); - assert (!unsat); + CADICAL_assert (ntab.empty ()); + CADICAL_assert (!unsat); reset_assumptions (); for (auto lit : cubes2[j]) assume (lit); @@ -478,7 +482,7 @@ CubesWithStatus Internal::generate_cubes (int depth, int min_depth) { continue; } - assert (res != 0); + CADICAL_assert (res != 0); LOG ("splitting on lit %i", res); std::vector cube1{cubes2[j]}; cube1.push_back (res); @@ -492,7 +496,7 @@ CubesWithStatus Internal::generate_cubes (int depth, int min_depth) { break; } - assert (std::for_each ( + CADICAL_assert (std::for_each ( std::begin (cubes), std::end (cubes), [] (std::vector cube) { return non_tautological_cube (cube); })); reset_assumptions (); @@ -518,3 +522,5 @@ CubesWithStatus Internal::generate_cubes (int depth, int min_depth) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/lratchecker.cpp b/src/sat/cadical/cadical_lratchecker.cpp similarity index 94% rename from src/sat/cadical/lratchecker.cpp rename to src/sat/cadical/cadical_lratchecker.cpp index 1dcead375..5f342a370 100644 --- a/src/sat/cadical/lratchecker.cpp +++ b/src/sat/cadical/cadical_lratchecker.cpp @@ -1,12 +1,16 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ inline unsigned LratChecker::l2u (int lit) { - assert (lit); - assert (lit != INT_MIN); + CADICAL_assert (lit); + CADICAL_assert (lit != INT_MIN); unsigned res = 2 * (abs (lit) - 1); if (lit < 0) res++; @@ -15,13 +19,13 @@ inline unsigned LratChecker::l2u (int lit) { signed char &LratChecker::mark (int lit) { const unsigned u = l2u (lit); - assert (u < marks.size ()); + CADICAL_assert (u < marks.size ()); return marks[u]; } signed char &LratChecker::checked_lit (int lit) { const unsigned u = l2u (lit); - assert (u < checked_lits.size ()); + CADICAL_assert (u < checked_lits.size ()); return checked_lits[u]; } @@ -29,7 +33,7 @@ signed char &LratChecker::checked_lit (int lit) { LratCheckerClause *LratChecker::new_clause () { const size_t size = imported_clause.size (); - assert (size <= UINT_MAX); + CADICAL_assert (size <= UINT_MAX); const int off = size ? 1 : 0; const size_t bytes = sizeof (LratCheckerClause) + (size - off) * sizeof (int); @@ -42,9 +46,9 @@ LratCheckerClause *LratChecker::new_clause () { res->used = false; res->tautological = false; int *literals = res->literals, *p = literals; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (auto &b : checked_lits) - assert (!b); // = false; + CADICAL_assert (!b); // = false; #endif for (const auto &lit : imported_clause) { *p++ = lit; @@ -62,19 +66,19 @@ LratCheckerClause *LratChecker::new_clause () { } void LratChecker::delete_clause (LratCheckerClause *c) { - assert (c); + CADICAL_assert (c); if (!c->garbage) { - assert (num_clauses); + CADICAL_assert (num_clauses); num_clauses--; } else { - assert (num_garbage); + CADICAL_assert (num_garbage); num_garbage--; } delete[] (char *) c; } void LratChecker::enlarge_clauses () { - assert (num_clauses == size_clauses); + CADICAL_assert (num_clauses == size_clauses); const uint64_t new_size_clauses = size_clauses ? 2 * size_clauses : 1; LOG ("LRAT CHECKER enlarging clauses of checker from %" PRIu64 " to %" PRIu64, @@ -107,7 +111,7 @@ void LratChecker::collect_garbage_clauses () { for (LratCheckerClause *c = garbage, *next; c; c = next) next = c->next, delete_clause (c); - assert (!num_garbage); + CADICAL_assert (!num_garbage); garbage = 0; } @@ -125,7 +129,7 @@ LratChecker::LratChecker (Internal *i) uint64_t nonce = random.next (); if (!(nonce & 1)) nonce++; - assert (nonce), assert (nonce & 1); + CADICAL_assert (nonce), CADICAL_assert (nonce & 1); nonces[n] = nonce; } @@ -151,7 +155,7 @@ LratChecker::~LratChecker () { void LratChecker::enlarge_vars (int64_t idx) { - assert (0 < idx), assert (idx <= INT_MAX); + CADICAL_assert (0 < idx), CADICAL_assert (idx <= INT_MAX); int64_t new_size_vars = size_vars ? 2 * size_vars : 2; while (idx >= new_size_vars) @@ -163,13 +167,13 @@ void LratChecker::enlarge_vars (int64_t idx) { marks.resize (2 * new_size_vars); checked_lits.resize (2 * new_size_vars); - assert (idx < new_size_vars); + CADICAL_assert (idx < new_size_vars); size_vars = new_size_vars; } inline void LratChecker::import_literal (int lit) { - assert (lit); - assert (lit != INT_MIN); + CADICAL_assert (lit); + CADICAL_assert (lit != INT_MIN); int idx = abs (lit); if (idx >= size_vars) enlarge_vars (idx); @@ -184,7 +188,7 @@ void LratChecker::import_clause (const vector &c) { /*------------------------------------------------------------------------*/ uint64_t LratChecker::reduce_hash (uint64_t hash, uint64_t size) { - assert (size > 0); + CADICAL_assert (size > 0); unsigned shift = 32; uint64_t res = hash; while ((((uint64_t) 1) << shift) > size) { @@ -192,12 +196,12 @@ uint64_t LratChecker::reduce_hash (uint64_t hash, uint64_t size) { shift >>= 1; } res &= size - 1; - assert (res < size); + CADICAL_assert (res < size); return res; } uint64_t LratChecker::compute_hash (const int64_t id) { - assert (id > 0); + CADICAL_assert (id > 0); unsigned j = id % num_nonces; uint64_t tmp = nonces[j] * (uint64_t) id; return last_hash = tmp; @@ -236,23 +240,23 @@ bool LratChecker::check_resolution (vector proof_chain) { return true; } // LOG (imported_clause, "LRAT CHECKER checking clause with resolution"); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (auto &b : checked_lits) - assert (!b); // = false; + CADICAL_assert (!b); // = false; #endif if (!proof_chain.size () || proof_chain.back () < 0) return false; LratCheckerClause *c = *find (proof_chain.back ()); - assert (c); + CADICAL_assert (c); for (int *i = c->literals; i < c->literals + c->size; i++) { int lit = *i; checked_lit (lit) = true; - assert (!checked_lit (-lit)); + CADICAL_assert (!checked_lit (-lit)); } for (auto p = proof_chain.end () - 2; p >= proof_chain.begin (); p--) { auto &id = *p; c = *find (id); - assert (c); // since this is checked in check already + CADICAL_assert (c); // since this is checked in check already for (int *i = c->literals; i < c->literals + c->size; i++) { int lit = *i; if (!checked_lit (-lit)) @@ -298,16 +302,16 @@ bool LratChecker::check_resolution (vector proof_chain) { bool LratChecker::check (vector proof_chain) { LOG (imported_clause, "LRAT CHECKER checking clause"); stats.checks++; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (auto &b : checked_lits) - assert (!b); // = false; + CADICAL_assert (!b); // = false; #endif bool taut = false; for (const auto &lit : imported_clause) { // tautological clauses checked_lit (-lit) = true; if (checked_lit (lit)) { LOG (imported_clause, "LRAT CHECKER clause tautological"); - assert (!proof_chain.size ()); // would be unnecessary hence a bug + CADICAL_assert (!proof_chain.size ()); // would be unnecessary hence a bug taut = true; } } @@ -468,7 +472,7 @@ void LratChecker::add_original_clause (int64_t id, bool, fatal_message_end (); } } - assert (id); + CADICAL_assert (id); insert (); imported_clause.clear (); STOP (checking); @@ -483,7 +487,7 @@ void LratChecker::add_derived_clause (int64_t id, bool, stats.derived++; import_clause (c); last_id = id; - assert (id == current_id + 1); + CADICAL_assert (id == current_id + 1); current_id = id; if (size_clauses) { LratCheckerClause **p = find (id), *d = *p; @@ -495,7 +499,7 @@ void LratChecker::add_derived_clause (int64_t id, bool, fatal_message_end (); } } - assert (id); + CADICAL_assert (id); bool failed = true; if (check (proof_chain) && check_resolution (proof_chain)) { failed = false; @@ -547,7 +551,7 @@ void LratChecker::add_assumption (int a) { assumptions.push_back (a); } void LratChecker::add_constraint (const vector &c) { constraint.clear (); for (auto &lit : c) { - assert (lit); + CADICAL_assert (lit); if (std::find (constraint.begin (), constraint.end (), lit) != constraint.end ()) continue; @@ -591,7 +595,7 @@ void LratChecker::conclude_unsat (ConclusionType conclusion, } return; } else { - assert (conclusion == CONSTRAINT); + CADICAL_assert (conclusion == CONSTRAINT); if (constraint.size () != ids.size ()) { fatal_message_start (); fputs ("not complete conclusion given for constraint\n", stderr); @@ -646,7 +650,7 @@ void LratChecker::delete_clause (int64_t id, bool, const vector &c) { // Remove from hash table, mark as garbage, connect to garbage list. num_garbage++; - assert (num_clauses); + CADICAL_assert (num_clauses); num_clauses--; *p = d->next; d->next = garbage; @@ -676,7 +680,7 @@ void LratChecker::weaken_minus (int64_t id, const vector &c) { LOG (c, "LRAT CHECKER saving clause[%" PRId64 "] to restore later", id); import_clause (c); - assert (id <= current_id); + CADICAL_assert (id <= current_id); last_id = id; LratCheckerClause **p = find (id), *d = *p; if (d) { @@ -756,7 +760,7 @@ void LratChecker::finalize_clause (int64_t id, const vector &c) { stats.finalized++; num_finalized++; import_clause (c); - assert (id <= current_id); + CADICAL_assert (id <= current_id); last_id = id; LratCheckerClause **p = find (id), *d = *p; if (d) { @@ -827,3 +831,5 @@ void LratChecker::dump () { void LratChecker::begin_proof (int64_t id) { current_id = id; } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/lrattracer.cpp b/src/sat/cadical/cadical_lrattracer.cpp similarity index 89% rename from src/sat/cadical/lrattracer.cpp rename to src/sat/cadical/cadical_lrattracer.cpp index 0bcd9a35b..4da728e18 100644 --- a/src/sat/cadical/lrattracer.cpp +++ b/src/sat/cadical/cadical_lrattracer.cpp @@ -1,14 +1,18 @@ +#include "global.h" + #include "internal.hpp" #include +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ LratTracer::LratTracer (Internal *i, File *f, bool b) : internal (i), file (f), binary (b) -#ifndef QUIET +#ifndef CADICAL_QUIET , added (0), deleted (0) #endif @@ -31,17 +35,17 @@ LratTracer::~LratTracer () { /*------------------------------------------------------------------------*/ inline void LratTracer::put_binary_zero () { - assert (binary); - assert (file); + CADICAL_assert (binary); + CADICAL_assert (file); file->put ((unsigned char) 0); } inline void LratTracer::put_binary_lit (int lit) { - assert (binary); - assert (file); - assert (lit != INT_MIN); + CADICAL_assert (binary); + CADICAL_assert (file); + CADICAL_assert (lit != INT_MIN); unsigned idx = abs (lit); - assert (idx < (1u << 31)); + CADICAL_assert (idx < (1u << 31)); unsigned x = 2 * idx + (lit < 0); unsigned char ch; while (x & ~0x7f) { @@ -54,8 +58,8 @@ inline void LratTracer::put_binary_lit (int lit) { } inline void LratTracer::put_binary_id (int64_t id) { - assert (binary); - assert (file); + CADICAL_assert (binary); + CADICAL_assert (file); uint64_t x = abs (id); x = 2 * x + (id < 0); unsigned char ch; @@ -130,7 +134,7 @@ void LratTracer::add_derived_clause (int64_t id, bool, return; LOG ("LRAT TRACER tracing addition of derived clause"); lrat_add_clause (id, clause, chain); -#ifndef QUIET +#ifndef CADICAL_QUIET added++; #endif } @@ -140,7 +144,7 @@ void LratTracer::delete_clause (int64_t id, bool, const vector &) { return; LOG ("LRAT TRACER tracing deletion of clause"); lrat_delete_clause (id); -#ifndef QUIET +#ifndef CADICAL_QUIET deleted++; #endif } @@ -156,7 +160,7 @@ void LratTracer::begin_proof (int64_t id) { bool LratTracer::closed () { return file->closed (); } -#ifndef QUIET +#ifndef CADICAL_QUIET void LratTracer::print_statistics () { uint64_t bytes = file->bytes (); @@ -172,9 +176,9 @@ void LratTracer::print_statistics () { #endif void LratTracer::close (bool print) { - assert (!closed ()); + CADICAL_assert (!closed ()); file->close (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { MSG ("LRAT proof file '%s' closed", file->name ()); print_statistics (); @@ -185,9 +189,9 @@ void LratTracer::close (bool print) { } void LratTracer::flush (bool print) { - assert (!closed ()); + CADICAL_assert (!closed ()); file->flush (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { MSG ("LRAT proof file '%s' flushed", file->name ()); print_statistics (); @@ -198,3 +202,5 @@ void LratTracer::flush (bool print) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/lucky.cpp b/src/sat/cadical/cadical_lucky.cpp similarity index 89% rename from src/sat/cadical/lucky.cpp rename to src/sat/cadical/cadical_lucky.cpp index 19da3ee28..d67782db4 100644 --- a/src/sat/cadical/lucky.cpp +++ b/src/sat/cadical/cadical_lucky.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // It turns out that even in the competition there are formulas which are @@ -29,8 +33,8 @@ int Internal::unlucky (int res) { int Internal::trivially_false_satisfiable () { LOG ("checking that all clauses contain a negative literal"); - assert (!level); - assert (assumptions.empty ()); + CADICAL_assert (!level); + CADICAL_assert (assumptions.empty ()); for (const auto &c : clauses) { if (terminated_asynchronously (100)) return unlucky (-1); @@ -66,7 +70,7 @@ int Internal::trivially_false_satisfiable () { search_assume_decision (-idx); if (propagate ()) continue; - assert (level > 0); + CADICAL_assert (level > 0); LOG ("propagation failed including redundant clauses"); return unlucky (0); } @@ -76,8 +80,8 @@ int Internal::trivially_false_satisfiable () { int Internal::trivially_true_satisfiable () { LOG ("checking that all clauses contain a positive literal"); - assert (!level); - assert (assumptions.empty ()); + CADICAL_assert (!level); + CADICAL_assert (assumptions.empty ()); for (const auto &c : clauses) { if (terminated_asynchronously (100)) return unlucky (-1); @@ -113,7 +117,7 @@ int Internal::trivially_true_satisfiable () { search_assume_decision (idx); if (propagate ()) continue; - assert (level > 0); + CADICAL_assert (level > 0); LOG ("propagation failed including redundant clauses"); return unlucky (0); } @@ -136,7 +140,7 @@ inline bool Internal::lucky_propagate_discrepency (int dec) { return true; } else { analyze (); - assert (!level); + CADICAL_assert (!level); no_conflict = propagate (); if (!no_conflict) { analyze (); @@ -149,9 +153,9 @@ inline bool Internal::lucky_propagate_discrepency (int dec) { int Internal::forward_false_satisfiable () { LOG ("checking increasing variable index false assignment"); - assert (!unsat); - assert (!level); - assert (assumptions.empty ()); + CADICAL_assert (!unsat); + CADICAL_assert (!level); + CADICAL_assert (assumptions.empty ()); for (auto idx : vars) { START: if (terminated_asynchronously (100)) @@ -167,16 +171,16 @@ int Internal::forward_false_satisfiable () { goto START; } VERBOSE (1, "forward assuming variables false satisfies formula"); - assert (satisfied ()); + CADICAL_assert (satisfied ()); stats.lucky.forward.zero++; return 10; } int Internal::forward_true_satisfiable () { LOG ("checking increasing variable index true assignment"); - assert (!unsat); - assert (!level); - assert (assumptions.empty ()); + CADICAL_assert (!unsat); + CADICAL_assert (!level); + CADICAL_assert (assumptions.empty ()); for (auto idx : vars) { START: if (terminated_asynchronously (10)) @@ -192,7 +196,7 @@ int Internal::forward_true_satisfiable () { goto START; } VERBOSE (1, "forward assuming variables true satisfies formula"); - assert (satisfied ()); + CADICAL_assert (satisfied ()); stats.lucky.forward.one++; return 10; } @@ -201,9 +205,9 @@ int Internal::forward_true_satisfiable () { int Internal::backward_false_satisfiable () { LOG ("checking decreasing variable index false assignment"); - assert (!unsat); - assert (!level); - assert (assumptions.empty ()); + CADICAL_assert (!unsat); + CADICAL_assert (!level); + CADICAL_assert (assumptions.empty ()); for (int idx = max_var; idx > 0; idx--) { START: if (terminated_asynchronously (10)) @@ -219,16 +223,16 @@ int Internal::backward_false_satisfiable () { goto START; } VERBOSE (1, "backward assuming variables false satisfies formula"); - assert (satisfied ()); + CADICAL_assert (satisfied ()); stats.lucky.backward.zero++; return 10; } int Internal::backward_true_satisfiable () { LOG ("checking decreasing variable index true assignment"); - assert (!unsat); - assert (!level); - assert (assumptions.empty ()); + CADICAL_assert (!unsat); + CADICAL_assert (!level); + CADICAL_assert (assumptions.empty ()); for (int idx = max_var; idx > 0; idx--) { START: if (terminated_asynchronously (10)) @@ -244,7 +248,7 @@ int Internal::backward_true_satisfiable () { goto START; } VERBOSE (1, "backward assuming variables true satisfies formula"); - assert (satisfied ()); + CADICAL_assert (satisfied ()); stats.lucky.backward.one++; return 10; } @@ -259,8 +263,8 @@ int Internal::backward_true_satisfiable () { int Internal::positive_horn_satisfiable () { LOG ("checking that all clauses are positive horn satisfiable"); - assert (!level); - assert (assumptions.empty ()); + CADICAL_assert (!level); + CADICAL_assert (assumptions.empty ()); for (const auto &c : clauses) { if (terminated_asynchronously (10)) return unlucky (-1); @@ -289,7 +293,7 @@ int Internal::positive_horn_satisfiable () { LOG (c, "no positive unassigned literal in"); return unlucky (0); } - assert (positive_literal > 0); + CADICAL_assert (positive_literal > 0); LOG (c, "found positive literal %d in", positive_literal); search_assume_decision (positive_literal); if (propagate ()) @@ -310,16 +314,16 @@ int Internal::positive_horn_satisfiable () { return unlucky (0); } VERBOSE (1, "clauses are positive horn satisfied"); - assert (!conflict); - assert (satisfied ()); + CADICAL_assert (!conflict); + CADICAL_assert (satisfied ()); stats.lucky.horn.positive++; return 10; } int Internal::negative_horn_satisfiable () { LOG ("checking that all clauses are negative horn satisfiable"); - assert (!level); - assert (assumptions.empty ()); + CADICAL_assert (!level); + CADICAL_assert (assumptions.empty ()); for (const auto &c : clauses) { if (terminated_asynchronously (10)) return unlucky (-1); @@ -350,7 +354,7 @@ int Internal::negative_horn_satisfiable () { LOG (c, "no negative unassigned literal in"); return unlucky (0); } - assert (negative_literal < 0); + CADICAL_assert (negative_literal < 0); LOG (c, "found negative literal %d in", negative_literal); search_assume_decision (negative_literal); if (propagate ()) @@ -371,8 +375,8 @@ int Internal::negative_horn_satisfiable () { return unlucky (0); } VERBOSE (1, "clauses are negative horn satisfied"); - assert (!conflict); - assert (satisfied ()); + CADICAL_assert (!conflict); + CADICAL_assert (satisfied ()); stats.lucky.horn.negative++; return 10; } @@ -380,7 +384,7 @@ int Internal::negative_horn_satisfiable () { /*------------------------------------------------------------------------*/ int Internal::lucky_phases () { - assert (!level); + CADICAL_assert (!level); require_mode (SEARCH); if (!opts.lucky) return 0; @@ -394,7 +398,7 @@ int Internal::lucky_phases () { START (search); START (lucky); - assert (!searching_lucky_phases); + CADICAL_assert (!searching_lucky_phases); searching_lucky_phases = true; stats.lucky.tried++; const int64_t active_before = stats.active; @@ -414,11 +418,11 @@ int Internal::lucky_phases () { if (!res) res = negative_horn_satisfiable (); if (res < 0) - assert (termination_forced), res = 0; + CADICAL_assert (termination_forced), res = 0; if (res == 10) stats.lucky.succeeded++; report ('l', !res); - assert (searching_lucky_phases); + CADICAL_assert (searching_lucky_phases); const int64_t units = active_before - stats.active; @@ -432,3 +436,5 @@ int Internal::lucky_phases () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/message.cpp b/src/sat/cadical/cadical_message.cpp similarity index 97% rename from src/sat/cadical/message.cpp rename to src/sat/cadical/cadical_message.cpp index 9f9a212ab..42d8c0e48 100644 --- a/src/sat/cadical/message.cpp +++ b/src/sat/cadical/cadical_message.cpp @@ -1,9 +1,13 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ -#ifndef QUIET +#ifndef CADICAL_QUIET /*------------------------------------------------------------------------*/ void Internal::print_prefix () { fputs (prefix.c_str (), stdout); } @@ -130,7 +134,7 @@ void Internal::phase (const char *phase, int64_t count, const char *fmt, } /*------------------------------------------------------------------------*/ -#endif // ifndef QUIET +#endif // ifndef CADICAL_QUIET /*------------------------------------------------------------------------*/ void Internal::warning (const char *fmt, ...) { @@ -210,3 +214,5 @@ void fatal (const char *fmt, ...) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/minimize.cpp b/src/sat/cadical/cadical_minimize.cpp similarity index 90% rename from src/sat/cadical/minimize.cpp rename to src/sat/cadical/cadical_minimize.cpp index ff9b89df4..97af5fbe6 100644 --- a/src/sat/cadical/minimize.cpp +++ b/src/sat/cadical/cadical_minimize.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // Functions for learned clause minimization. We only have the recursive @@ -16,7 +20,7 @@ namespace CaDiCaL { bool Internal::minimize_literal (int lit, int depth) { LOG ("attempt to minimize lit %d at depth %d", lit, depth); - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); Flags &f = flags (lit); Var &v = var (lit); if (!v.level || f.removable || f.keep) @@ -31,18 +35,18 @@ bool Internal::minimize_literal (int lit, int depth) { if (depth > opts.minimizedepth) return false; bool res = true; - assert (v.reason); + CADICAL_assert (v.reason); if (opts.minimizeticks) stats.ticks.search[stable]++; if (v.reason == external_reason) { - assert (!opts.exteagerreasons); + CADICAL_assert (!opts.exteagerreasons); v.reason = learn_external_reason_clause (lit, 0, true); if (!v.reason) { - assert (!v.level); + CADICAL_assert (!v.level); return true; } } - assert (v.reason != external_reason); + CADICAL_assert (v.reason != external_reason); const const_literal_iterator end = v.reason->end (); const_literal_iterator i; for (i = v.reason->begin (); res && i != end; i++) { @@ -71,7 +75,7 @@ struct minimize_trail_positive_rank { minimize_trail_positive_rank (Internal *s) : internal (s) {} typedef unsigned Type; Type operator() (const int &a) const { - assert (internal->val (a)); + CADICAL_assert (internal->val (a)); return (unsigned) internal->var (a).trail; } }; @@ -89,7 +93,7 @@ struct minimize_trail_level_positive_rank { minimize_trail_level_positive_rank (Internal *s) : internal (s) {} typedef uint64_t Type; Type operator() (const int &a) const { - assert (internal->val (a)); + CADICAL_assert (internal->val (a)); Var &v = internal->var (a); uint64_t res = v.level; res <<= 32; @@ -114,15 +118,15 @@ void Internal::minimize_clause () { external->check_learned_clause (); // check 1st UIP learned clause first minimize_sort_clause (); - assert (minimized.empty ()); - assert (minimize_chain.empty ()); + CADICAL_assert (minimized.empty ()); + CADICAL_assert (minimize_chain.empty ()); const auto end = clause.end (); auto j = clause.begin (), i = j; std::vector stack; for (; i != end; i++) { if (minimize_literal (-*i)) { if (lrat) { - assert (mini_chain.empty ()); + CADICAL_assert (mini_chain.empty ()); calculate_minimize_chain (-*i, stack); for (auto p : mini_chain) { minimize_chain.push_back (p); @@ -153,19 +157,19 @@ void Internal::minimize_clause () { // minimize version. Unlike the minimize version, we have to keep literals // on the stack in order to push its reason later. void Internal::calculate_minimize_chain (int lit, std::vector &stack) { - assert (stack.empty ()); + CADICAL_assert (stack.empty ()); stack.push_back (vidx (lit)); while (!stack.empty ()) { const int idx = stack.back (); - assert (idx); + CADICAL_assert (idx); stack.pop_back (); if (idx < 0) { Var &v = var (idx); mini_chain.push_back (v.reason->id); continue; } - assert (idx); + CADICAL_assert (idx); Flags &f = flags (idx); Var &v = var (idx); if (f.keep || f.added || f.poison) { @@ -182,7 +186,7 @@ void Internal::calculate_minimize_chain (int lit, std::vector &stack) { continue; } f.added = true; - assert (v.reason && f.removable); + CADICAL_assert (v.reason && f.removable); const const_literal_iterator end = v.reason->end (); const_literal_iterator i; LOG (v.reason, "LRAT chain for lit %d at depth %zd by going over", lit, @@ -195,7 +199,7 @@ void Internal::calculate_minimize_chain (int lit, std::vector &stack) { stack.push_back (vidx (other)); } } - assert (stack.empty ()); + CADICAL_assert (stack.empty ()); } // Sort the literals in reverse assignment order (thus trail order) to @@ -215,10 +219,12 @@ void Internal::clear_minimized_literals () { f.poison = f.removable = f.shrinkable = f.added = false; } for (const auto &lit : clause) - assert (!flags (lit).shrinkable), flags (lit).keep = + CADICAL_assert (!flags (lit).shrinkable), flags (lit).keep = flags (lit).shrinkable = flags (lit).added = false; minimized.clear (); } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/occs.cpp b/src/sat/cadical/cadical_occs.cpp similarity index 80% rename from src/sat/cadical/occs.cpp rename to src/sat/cadical/cadical_occs.cpp index c1d987416..f79d7a690 100644 --- a/src/sat/cadical/occs.cpp +++ b/src/sat/cadical/cadical_occs.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -13,13 +17,13 @@ void Internal::init_occs () { } void Internal::reset_occs () { - assert (occurring ()); + CADICAL_assert (occurring ()); erase_vector (otab); LOG ("reset occurrence lists"); } void Internal::clear_occs () { - assert (occurring ()); + CADICAL_assert (occurring ()); for (auto &occ : otab) occ.clear (); LOG ("clear occurrence lists"); @@ -30,23 +34,25 @@ void Internal::clear_occs () { // One-sided occurrence counter (each literal has its own counter). void Internal::init_noccs () { - assert (ntab.empty ()); + CADICAL_assert (ntab.empty ()); if (ntab.size () < 2 * vsize) ntab.resize (2 * vsize, 0); LOG ("initialized two-sided occurrence counters"); } void Internal::clear_noccs () { - assert (!ntab.empty ()); + CADICAL_assert (!ntab.empty ()); for (auto &nt : ntab) nt = 0; LOG ("clear two-sided occurrence counters"); } void Internal::reset_noccs () { - assert (!max_var || !ntab.empty ()); + CADICAL_assert (!max_var || !ntab.empty ()); erase_vector (ntab); LOG ("reset two-sided occurrence counters"); } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/options.cpp b/src/sat/cadical/cadical_options.cpp similarity index 93% rename from src/sat/cadical/options.cpp rename to src/sat/cadical/cadical_options.cpp index 26dcc3d54..88eba53ab 100644 --- a/src/sat/cadical/options.cpp +++ b/src/sat/cadical/cadical_options.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -81,12 +85,12 @@ void Options::initialize_from_environment (int &val, const char *name, const int L, const int H) { char key[80], *q; const char *p; - assert (strlen (name) + strlen ("CADICAL_") + 1 < sizeof (key)); + CADICAL_assert (strlen (name) + strlen ("CADICAL_") + 1 < sizeof (key)); for (p = "CADICAL_", q = key; *p; p++) *q++ = *p; for (p = name; *p; p++) *q++ = toupper (*p); - assert (q < key + sizeof (key)); + CADICAL_assert (q < key + sizeof (key)); *q = 0; const char *val_str = getenv (key); if (!val_str) @@ -102,7 +106,7 @@ void Options::initialize_from_environment (int &val, const char *name, // Initialize all the options to their default value 'V'. Options::Options (Internal *s) : internal (s) { - assert (number_of_options == sizeof Options::table / sizeof (Option)); + CADICAL_assert (number_of_options == sizeof Options::table / sizeof (Option)); // First initialize them according to defaults in 'options.hpp'. // @@ -119,12 +123,12 @@ Options::Options (Internal *s) : internal (s) { if (strcmp (prev, #N) > 0) \ FATAL ("'%s' ordered before '" #N "' in 'options.hpp'", prev); \ N = (int) (V); \ - assert (&val (i) == &N); \ + CADICAL_assert (&val (i) == &N); \ /* The order of initializing static data is undefined and thus */ \ /* it might be the case that the 'table' is not initialized yet. */ \ /* Thus this construction just reinitializes the table too even */ \ /* though it might not be necessary. */ \ - assert (!table[i].name || !strcmp (table[i].name, #N)); \ + CADICAL_assert (!table[i].name || !strcmp (table[i].name, #N)); \ table[i] = {#N, (int) (V), (int) (L), (int) (H), \ (int) (O), (bool) (P), D}; \ prev = #N; \ @@ -135,11 +139,11 @@ Options::Options (Internal *s) : internal (s) { // Check consistency in debugging mode. // -#ifndef NDEBUG - assert (i == number_of_options); - assert (!has ("aaaaa")); - assert (!has ("non-existing-option")); - assert (!has ("zzzzz")); +#ifndef CADICAL_NDEBUG + CADICAL_assert (i == number_of_options); + CADICAL_assert (!has ("aaaaa")); + CADICAL_assert (!has ("non-existing-option")); + CADICAL_assert (!has ("zzzzz")); #endif // Now overwrite default options with environment values. @@ -153,7 +157,7 @@ Options::Options (Internal *s) : internal (s) { /*------------------------------------------------------------------------*/ void Options::set (Option *o, int new_val) { - assert (o); + CADICAL_assert (o); int &val = o->val (this), old_val = val; if (old_val == new_val) { LOG ("keeping value '%d' of option '%s'", old_val, o->name); @@ -193,7 +197,7 @@ int Options::get (const char *name) { void Options::print () { unsigned different = 0; -#ifdef QUIET +#ifdef CADICAL_QUIET const bool verbose = false; #endif char buffer[256]; @@ -278,7 +282,7 @@ void Options::optimize (int val) { LOG ("optimization mode '%d' for '%s' " \ "gives '%" PRId64 "' instead of '%d", \ val, #N, new_val, (int) (V)); \ - assert (new_val <= INT_MAX); \ + CADICAL_assert (new_val <= INT_MAX); \ N = (int) new_val; \ increased++; \ } while (0); @@ -299,8 +303,8 @@ void Options::disable_preprocessing () { if (!(N)) \ break; \ LOG ("plain mode disables '%s'", #N); \ - assert ((L) == 0); \ - assert ((H) == 1); \ + CADICAL_assert ((L) == 0); \ + CADICAL_assert ((H) == 1); \ count++; \ N = 0; \ } while (0); @@ -357,3 +361,5 @@ void Options::copy (Options &other) const { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/parse.cpp b/src/sat/cadical/cadical_parse.cpp similarity index 96% rename from src/sat/cadical/parse.cpp rename to src/sat/cadical/cadical_parse.cpp index 25334385e..0668aca7d 100644 --- a/src/sat/cadical/parse.cpp +++ b/src/sat/cadical/cadical_parse.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + /*------------------------------------------------------------------------*/ namespace CaDiCaL { @@ -37,7 +41,7 @@ inline const char *Parser::parse_string (const char *str, char prev) { inline const char *Parser::parse_positive_int (int &ch, int &res, const char *name) { - assert (isdigit (ch)); + CADICAL_assert (isdigit (ch)); res = ch - '0'; while (isdigit (ch = parse_char ())) { int digit = ch - '0'; @@ -90,7 +94,7 @@ inline const char *Parser::parse_lit (int &ch, int &lit, int &vars, const char *Parser::parse_dimacs_non_profiled (int &vars, int strict) { -#ifndef QUIET +#ifndef CADICAL_QUIET double start = internal->time (); #endif @@ -143,7 +147,7 @@ const char *Parser::parse_dimacs_non_profiled (int &vars, int strict) { // or 'p inccnf' of incremental 'INCCNF' file. // if (ch == 'c') { - assert (!found_inccnf_header); + CADICAL_assert (!found_inccnf_header); if (strict == STRICT) { const char *err = parse_string ("nf ", 'c'); if (err) @@ -266,19 +270,19 @@ const char *Parser::parse_dimacs_non_profiled (int &vars, int strict) { if (!found_inccnf_header && parsed < clauses && strict != FORCED) PER ("clause missing"); -#ifndef QUIET +#ifndef CADICAL_QUIET double end = internal->time (); MSG ("parsed %d clauses in %.2f seconds %s time", parsed, end - start, internal->opts.realtime ? "real" : "process"); #endif -#ifndef QUIET +#ifndef CADICAL_QUIET start = end; size_t num_cubes = 0; #endif if (ch == 'a') { - assert (parse_inccnf_too); - assert (found_inccnf_header); + CADICAL_assert (parse_inccnf_too); + CADICAL_assert (found_inccnf_header); if (!*parse_inccnf_too) *parse_inccnf_too = true; for (;;) { @@ -305,7 +309,7 @@ const char *Parser::parse_dimacs_non_profiled (int &vars, int strict) { if (cubes) cubes->push_back (lit); if (!lit) { -#ifndef QUIET +#ifndef CADICAL_QUIET num_cubes++; #endif for (;;) { @@ -332,7 +336,7 @@ const char *Parser::parse_dimacs_non_profiled (int &vars, int strict) { if (lit) PER ("last cube without terminating '0'"); } -#ifndef QUIET +#ifndef CADICAL_QUIET if (found_inccnf_header) { double end = internal->time (); MSG ("parsed %zd cubes in %.2f seconds %s time", num_cubes, end - start, @@ -372,7 +376,7 @@ const char *Parser::parse_solution_non_profiled () { ch = parse_char (); if (ch != '\n') PER ("expected new-line after 's SATISFIABLE'"); -#ifndef QUIET +#ifndef CADICAL_QUIET int count = 0; #endif for (;;) { @@ -399,7 +403,7 @@ const char *Parser::parse_solution_non_profiled () { PER ("variable %d occurs twice", abs (lit)); LOG ("solution %d", lit); external->solution[abs (lit)] = sign (lit); -#ifndef QUIET +#ifndef CADICAL_QUIET count++; #endif if (ch == '\r') @@ -419,7 +423,7 @@ const char *Parser::parse_solution_non_profiled () { // implicit 'return' in PER in the non-profiled versions. const char *Parser::parse_dimacs (int &vars, int strict) { - assert (strict == FORCED || strict == RELAXED || strict == STRICT); + CADICAL_assert (strict == FORCED || strict == RELAXED || strict == STRICT); START (parse); const char *err = parse_dimacs_non_profiled (vars, strict); STOP (parse); @@ -434,3 +438,5 @@ const char *Parser::parse_solution () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/phases.cpp b/src/sat/cadical/cadical_phases.cpp similarity index 94% rename from src/sat/cadical/phases.cpp rename to src/sat/cadical/cadical_phases.cpp index 92edd28a0..f7e4eaffd 100644 --- a/src/sat/cadical/phases.cpp +++ b/src/sat/cadical/cadical_phases.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void Internal::copy_phases (vector &dst) { @@ -42,3 +46,5 @@ void Internal::unphase (int lit) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/probe.cpp b/src/sat/cadical/cadical_probe.cpp similarity index 90% rename from src/sat/cadical/probe.cpp rename to src/sat/cadical/cadical_probe.cpp index acab6405b..19ff0961f 100644 --- a/src/sat/cadical/probe.cpp +++ b/src/sat/cadical/cadical_probe.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -19,7 +23,7 @@ bool Internal::inprobing () { if (!preprocessing && !opts.inprocessing) return false; if (preprocessing) - assert (lim.preprocessing); + CADICAL_assert (lim.preprocessing); if (stats.inprobingphases && last.inprobe.reductions == stats.reductions) return false; return lim.inprobe <= stats.conflicts; @@ -86,9 +90,9 @@ void Internal::init_probehbr_lrat () { void Internal::get_probehbr_lrat (int lit, int uip) { if (!lrat || opts.probehbr) return; - assert (lit); - assert (lrat_chain.empty ()); - assert (val (uip) < 0); + CADICAL_assert (lit); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (val (uip) < 0); lrat_chain = probehbr_chains[vlit (lit)][vlit (uip)]; int64_t id = unit_id (-uip); lrat_chain.push_back (id); @@ -100,9 +104,9 @@ void Internal::get_probehbr_lrat (int lit, int uip) { void Internal::set_probehbr_lrat (int lit, int uip) { if (!lrat || opts.probehbr) return; - assert (lit); - assert (lrat_chain.size ()); - assert (probehbr_chains[vlit (lit)][vlit (uip)].empty ()); + CADICAL_assert (lit); + CADICAL_assert (lrat_chain.size ()); + CADICAL_assert (probehbr_chains[vlit (lit)][vlit (uip)].empty ()); probehbr_chains[vlit (lit)][vlit (uip)] = lrat_chain; lrat_chain.clear (); } @@ -151,20 +155,20 @@ int Internal::probe_dominator (int a, int b) { require_mode (PROBE); int l = a, k = b; Var *u = &var (l), *v = &var (k); - assert (val (l) > 0), assert (val (k) > 0); - assert (u->level == 1), assert (v->level == 1); + CADICAL_assert (val (l) > 0), CADICAL_assert (val (k) > 0); + CADICAL_assert (u->level == 1), CADICAL_assert (v->level == 1); while (l != k) { if (u->trail > v->trail) swap (l, k), swap (u, v); if (!get_parent_reason_literal (l)) return l; int parent = get_parent_reason_literal (k); - assert (parent), assert (val (parent) > 0); + CADICAL_assert (parent), CADICAL_assert (val (parent) > 0); v = &var (k = parent); - assert (v->level == 1); + CADICAL_assert (v->level == 1); } LOG ("dominator %d of %d and %d", l, a, b); - assert (val (l) > 0); + CADICAL_assert (val (l) > 0); return l; } @@ -217,17 +221,17 @@ int Internal::probe_dominator (int a, int b) { inline int Internal::hyper_binary_resolve (Clause *reason) { require_mode (PROBE); - assert (level == 1); - assert (reason->size > 2); + CADICAL_assert (level == 1); + CADICAL_assert (reason->size > 2); const const_literal_iterator end = reason->end (); const int *lits = reason->literals; const_literal_iterator k; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG // First literal unassigned, all others false. - assert (!val (lits[0])); + CADICAL_assert (!val (lits[0])); for (k = lits + 1; k != end; k++) - assert (val (*k) < 0); - assert (var (lits[1]).level == 1); + CADICAL_assert (val (*k) < 0); + CADICAL_assert (var (lits[1]).level == 1); #endif LOG (reason, "hyper binary resolving"); stats.hbrs++; @@ -236,7 +240,7 @@ inline int Internal::hyper_binary_resolve (Clause *reason) { int dom = -lit, non_root_level_literals = 0; for (k = lits + 2; k != end; k++) { const int other = -*k; - assert (val (other) > 0); + CADICAL_assert (val (other) > 0); if (!var (other).level) continue; dom = probe_dominator (dom, other); @@ -252,7 +256,7 @@ inline int Internal::hyper_binary_resolve (Clause *reason) { stats.hbreds++; LOG ("new %s hyper binary resolvent %d %d", (red ? "redundant" : "irredundant"), -dom, lits[0]); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); clause.push_back (-dom); clause.push_back (lits[0]); probe_dominator_lrat (dom, reason); @@ -271,7 +275,7 @@ inline int Internal::hyper_binary_resolve (Clause *reason) { } } else if (non_root_level_literals && lrat) { // still calculate LRAT and remember for later - assert (!opts.probehbr); + CADICAL_assert (!opts.probehbr); probe_dominator_lrat (dom, reason); clear_analyzed_literals (); set_probehbr_lrat (dom, lits[0]); @@ -292,13 +296,13 @@ inline int Internal::hyper_binary_resolve (Clause *reason) { inline void Internal::probe_assign (int lit, int parent) { require_mode (PROBE); int idx = vidx (lit); - assert (!val (idx)); - assert (!flags (idx).eliminated () || !parent); - assert (!parent || val (parent) > 0); + CADICAL_assert (!val (idx)); + CADICAL_assert (!flags (idx).eliminated () || !parent); + CADICAL_assert (!parent || val (parent) > 0); Var &v = var (idx); v.level = level; v.trail = (int) trail.size (); - assert ((int) num_assigned < max_var); + CADICAL_assert ((int) num_assigned < max_var); num_assigned++; v.reason = level ? probe_reason : 0; probe_reason = 0; @@ -306,11 +310,11 @@ inline void Internal::probe_assign (int lit, int parent) { if (!level) learn_unit_clause (lit); else - assert (level == 1); + CADICAL_assert (level == 1); const signed char tmp = sign (lit); set_val (idx, tmp); - assert (val (lit) > 0); - assert (val (-lit) < 0); + CADICAL_assert (val (lit) > 0); + CADICAL_assert (val (-lit) < 0); trail.push_back (lit); // Do not save the current phase during inprocessing but remember the @@ -331,8 +335,8 @@ inline void Internal::probe_assign (int lit, int parent) { void Internal::probe_assign_decision (int lit) { require_mode (PROBE); - assert (!level); - assert (propagated == trail.size ()); + CADICAL_assert (!level); + CADICAL_assert (propagated == trail.size ()); level++; control.push_back (Level (lit, trail.size ())); probe_assign (lit, 0); @@ -340,8 +344,8 @@ void Internal::probe_assign_decision (int lit) { void Internal::probe_assign_unit (int lit) { require_mode (PROBE); - assert (!level); - assert (active (lit)); + CADICAL_assert (!level); + CADICAL_assert (active (lit)); probe_assign (lit, 0); } @@ -355,12 +359,12 @@ inline void Internal::probe_lrat_for_units (int lit) { if (level) return; // not decision level 0 LOG ("building chain for units"); - assert (lrat_chain.empty ()); - assert (probe_reason); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (probe_reason); for (auto &reason_lit : *probe_reason) { if (lit == reason_lit) continue; - assert (val (reason_lit)); + CADICAL_assert (val (reason_lit)); if (!val (reason_lit)) continue; const int signed_reason_lit = val (reason_lit) * reason_lit; @@ -398,8 +402,8 @@ inline void Internal::probe_propagate2 () { if (b < 0) conflict = w.clause; // but continue else { - assert (lrat_chain.empty ()); - assert (!probe_reason); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (!probe_reason); probe_reason = w.clause; probe_lrat_for_units (w.blit); probe_assign (w.blit, -lit); @@ -411,7 +415,7 @@ inline void Internal::probe_propagate2 () { bool Internal::probe_propagate () { require_mode (PROBE); - assert (!unsat); + CADICAL_assert (!unsat); START (propagate); int64_t before = propagated2 = propagated; int64_t &ticks = stats.ticks.probe; @@ -452,12 +456,12 @@ bool Internal::probe_propagate () { k++; if (v < 0) { k = lits + 2; - assert (w.clause->pos <= size); + CADICAL_assert (w.clause->pos <= size); while (k != middle && (v = val (r = *k)) < 0) k++; } w.clause->pos = k - lits; - assert (lits + 2 <= k), assert (k <= w.clause->end ()); + CADICAL_assert (lits + 2 <= k), CADICAL_assert (k <= w.clause->end ()); if (v > 0) ws[j - 1].blit = r; else if (!v) { @@ -472,14 +476,14 @@ bool Internal::probe_propagate () { ticks++; if (level == 1) { lits[0] = other, lits[1] = lit; - assert (lrat_chain.empty ()); - assert (!probe_reason); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (!probe_reason); int dom = hyper_binary_resolve (w.clause); probe_assign (other, dom); } else { ticks++; - assert (lrat_chain.empty ()); - assert (!probe_reason); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (!probe_reason); probe_reason = w.clause; probe_lrat_for_units (other); probe_assign_unit (other); @@ -516,11 +520,11 @@ void Internal::failed_literal (int failed) { stats.failed++; stats.probefailed++; - assert (!unsat); - assert (conflict); - assert (level == 1); - assert (analyzed.empty ()); - assert (lrat_chain.empty ()); + CADICAL_assert (!unsat); + CADICAL_assert (conflict); + CADICAL_assert (level == 1); + CADICAL_assert (analyzed.empty ()); + CADICAL_assert (lrat_chain.empty ()); START (analyze); @@ -530,7 +534,7 @@ void Internal::failed_literal (int failed) { for (const auto &lit : *conflict) { const int other = -lit; if (!var (other).level) { - assert (val (other) > 0); + CADICAL_assert (val (other) > 0); continue; } uip = uip ? probe_dominator (uip, other) : other; @@ -540,7 +544,7 @@ void Internal::failed_literal (int failed) { clear_analyzed_literals (); LOG ("found probing UIP %d", uip); - assert (uip); + CADICAL_assert (uip); vector work; @@ -548,14 +552,14 @@ void Internal::failed_literal (int failed) { while (parent != failed) { const int next = get_parent_reason_literal (parent); parent = next; - assert (parent); + CADICAL_assert (parent); work.push_back (parent); } backtrack (); conflict = 0; - assert (!val (uip)); + CADICAL_assert (!val (uip)); probe_assign_unit (-uip); lrat_chain.clear (); @@ -564,16 +568,16 @@ void Internal::failed_literal (int failed) { size_t j = 0; while (!unsat && j < work.size ()) { - // assert (!opts.probehbr); assertion fails ... + // CADICAL_assert (!opts.probehbr); CADICAL_assertion fails ... const int parent = work[j++]; const signed char tmp = val (parent); if (tmp > 0) { - assert (!opts.probehbr); // ... assertion should hold here + CADICAL_assert (!opts.probehbr); // ... CADICAL_assertion should hold here get_probehbr_lrat (parent, uip); LOG ("clashing failed parent %d", parent); learn_empty_clause (); } else if (tmp == 0) { - assert (!opts.probehbr); // ... and here + CADICAL_assert (!opts.probehbr); // ... and here LOG ("found unassigned failed parent %d", parent); get_probehbr_lrat (parent, uip); // this is computed during probe_assign_unit (-parent); // propagation and can include @@ -588,13 +592,13 @@ void Internal::failed_literal (int failed) { STOP (analyze); - assert (unsat || val (failed) < 0); + CADICAL_assert (unsat || val (failed) < 0); } /*------------------------------------------------------------------------*/ bool Internal::is_binary_clause (Clause *c, int &a, int &b) { - assert (!level); + CADICAL_assert (!level); if (c->garbage) return false; int first = 0, second = 0; @@ -632,7 +636,7 @@ struct probe_negated_noccs_rank { void Internal::generate_probes () { - assert (probes.empty ()); + CADICAL_assert (probes.empty ()); int64_t &ticks = stats.ticks.probe; @@ -697,7 +701,7 @@ void Internal::generate_probes () { void Internal::flush_probes () { - assert (!probes.empty ()); + CADICAL_assert (!probes.empty ()); int64_t &ticks = stats.ticks.probe; init_noccs (); @@ -724,14 +728,14 @@ void Internal::flush_probes () { continue; if (have_pos_bin_occs) lit = -lit; - assert (!noccs (lit)), assert (noccs (-lit) > 0); + CADICAL_assert (!noccs (lit)), CADICAL_assert (noccs (-lit) > 0); if (propfixed (lit) >= stats.all.fixed) continue; LOG ("keeping probe %d negated occs %" PRId64 "", lit, noccs (-lit)); *j++ = lit; } size_t remain = j - probes.begin (); -#ifndef QUIET +#ifndef CADICAL_QUIET size_t flushed = probes.size () - remain; #endif probes.resize (remain); @@ -808,7 +812,7 @@ bool Internal::probe () { "probing limit of %" PRId64 " propagations ", limit); int old_failed = stats.failed; -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t old_probed = stats.probed; #endif int64_t old_hbrs = stats.hbrs; @@ -823,7 +827,7 @@ bool Internal::probe () { for (auto idx : vars) propfixed (idx) = propfixed (-idx) = -1; - assert (unsat || propagated == trail.size ()); + CADICAL_assert (unsat || propagated == trail.size ()); propagated = propagated2 = trail.size (); int probe; @@ -853,7 +857,7 @@ bool Internal::probe () { } int failed = stats.failed - old_failed; -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t probed = stats.probed - old_probed; #endif int64_t hbrs = stats.hbrs - old_hbrs; @@ -924,7 +928,7 @@ void CaDiCaL::Internal::inprobe (bool update_limits) { stats.inprobingphases++; if (external_prop) { - assert (!level); + CADICAL_assert (!level); private_steps = true; } const int before = active (); @@ -950,7 +954,7 @@ void CaDiCaL::Internal::inprobe (bool update_limits) { } if (external_prop) { - assert (!level); + CADICAL_assert (!level); private_steps = false; } @@ -960,9 +964,9 @@ void CaDiCaL::Internal::inprobe (bool update_limits) { const int after = active (); const int after_extended = stats.variables_extension; const int diff_extended = after_extended - before_extended; - assert (diff_extended >= 0); + CADICAL_assert (diff_extended >= 0); const int removed = before - after + diff_extended; - assert (removed >= 0); + CADICAL_assert (removed >= 0); if (removed) { stats.inprobesuccess++; @@ -985,3 +989,5 @@ void CaDiCaL::Internal::inprobe (bool update_limits) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/profile.cpp b/src/sat/cadical/cadical_profile.cpp similarity index 87% rename from src/sat/cadical/profile.cpp rename to src/sat/cadical/cadical_profile.cpp index 68d80cbed..a95c97661 100644 --- a/src/sat/cadical/profile.cpp +++ b/src/sat/cadical/cadical_profile.cpp @@ -1,7 +1,11 @@ -#ifndef QUIET +#include "global.h" + +#ifndef CADICAL_QUIET #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // Initialize all profile counters with constant name and profiling level. @@ -15,15 +19,15 @@ Profiles::Profiles (Internal *s) } void Internal::start_profiling (Profile &profile, double s) { - assert (profile.level <= opts.profile); - assert (!profile.active); + CADICAL_assert (profile.level <= opts.profile); + CADICAL_assert (!profile.active); profile.started = s; profile.active = true; } void Internal::stop_profiling (Profile &profile, double s) { - assert (profile.level <= opts.profile); - assert (profile.active); + CADICAL_assert (profile.level <= opts.profile); + CADICAL_assert (profile.active); profile.value += s - profile.started; profile.active = false; } @@ -34,7 +38,7 @@ double Internal::update_profiles () { do { \ Profile &profile = profiles.NAME; \ if (profile.active) { \ - assert (profile.level <= opts.profile); \ + CADICAL_assert (profile.level <= opts.profile); \ profile.value += now - profile.started; \ profile.started = now; \ } \ @@ -77,7 +81,7 @@ void Internal::print_profile () { PROFILES #undef PROFILE - assert (n <= size); + CADICAL_assert (n <= size); // Explicit bubble sort to avoid heap allocation since 'print_profile' // is also called during catching a signal after out of heap memory. @@ -104,4 +108,6 @@ void Internal::print_profile () { } // namespace CaDiCaL -#endif // ifndef QUIET +ABC_NAMESPACE_IMPL_END + +#endif // ifndef CADICAL_QUIET diff --git a/src/sat/cadical/proof.cpp b/src/sat/cadical/cadical_proof.cpp similarity index 92% rename from src/sat/cadical/proof.cpp rename to src/sat/cadical/cadical_proof.cpp index 36b629423..13c9a1a5d 100644 --- a/src/sat/cadical/proof.cpp +++ b/src/sat/cadical/cadical_proof.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { using namespace std; @@ -23,7 +27,7 @@ void Internal::resize_unit_clauses_idx () { void Internal::force_lrat () { if (lrat) return; - assert (proof); + CADICAL_assert (proof); lrat = true; } @@ -83,7 +87,7 @@ bool Internal::disconnect_proof_tracer (Tracer *tracer) { auto it = std::find (tracers.begin (), tracers.end (), tracer); if (it != tracers.end ()) { tracers.erase (it); - assert (proof); + CADICAL_assert (proof); proof->disconnect (tracer); return true; } @@ -94,7 +98,7 @@ bool Internal::disconnect_proof_tracer (StatTracer *tracer) { auto it = std::find (stat_tracers.begin (), stat_tracers.end (), tracer); if (it != stat_tracers.end ()) { stat_tracers.erase (it); - assert (proof); + CADICAL_assert (proof); proof->disconnect (tracer); return true; } @@ -105,7 +109,7 @@ bool Internal::disconnect_proof_tracer (FileTracer *tracer) { auto it = std::find (file_tracers.begin (), file_tracers.end (), tracer); if (it != file_tracers.end ()) { file_tracers.erase (it); - assert (proof); + CADICAL_assert (proof); proof->disconnect (tracer); return true; } @@ -230,7 +234,7 @@ void Proof::add_external_original_clause (int64_t id, bool r, const vector &c, bool restore) { // literals of c are already external - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); for (auto const &lit : c) clause.push_back (lit); clause_id = id; @@ -241,7 +245,7 @@ void Proof::add_external_original_clause (int64_t id, bool r, void Proof::delete_external_original_clause (int64_t id, bool r, const vector &c) { // literals of c are already external - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); for (auto const &lit : c) clause.push_back (lit); clause_id = id; @@ -252,8 +256,8 @@ void Proof::delete_external_original_clause (int64_t id, bool r, void Proof::add_derived_empty_clause (int64_t id, const vector &chain) { LOG ("PROOF adding empty clause"); - assert (clause.empty ()); - assert (proof_chain.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (proof_chain.empty ()); for (const auto &cid : chain) proof_chain.push_back (cid); clause_id = id; @@ -264,8 +268,8 @@ void Proof::add_derived_empty_clause (int64_t id, void Proof::add_derived_unit_clause (int64_t id, int internal_unit, const vector &chain) { LOG ("PROOF adding unit clause %d", internal_unit); - assert (proof_chain.empty ()); - assert (clause.empty ()); + CADICAL_assert (proof_chain.empty ()); + CADICAL_assert (clause.empty ()); add_literal (internal_unit); for (const auto &cid : chain) proof_chain.push_back (cid); @@ -278,8 +282,8 @@ void Proof::add_derived_unit_clause (int64_t id, int internal_unit, void Proof::add_derived_clause (Clause *c, const vector &chain) { LOG (c, "PROOF adding to proof derived"); - assert (clause.empty ()); - assert (proof_chain.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (proof_chain.empty ()); add_literals (c); for (const auto &cid : chain) proof_chain.push_back (cid); @@ -291,8 +295,8 @@ void Proof::add_derived_clause (Clause *c, const vector &chain) { void Proof::add_derived_clause (int64_t id, bool r, const vector &c, const vector &chain) { LOG (c, "PROOF adding derived clause"); - assert (clause.empty ()); - assert (proof_chain.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (proof_chain.empty ()); for (const auto &lit : c) add_literal (lit); for (const auto &cid : chain) @@ -305,8 +309,8 @@ void Proof::add_derived_clause (int64_t id, bool r, const vector &c, void Proof::add_assumption_clause (int64_t id, const vector &c, const vector &chain) { // literals of c are already external - assert (clause.empty ()); - assert (proof_chain.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (proof_chain.empty ()); for (const auto &lit : c) clause.push_back (lit); for (const auto &cid : chain) @@ -317,16 +321,16 @@ void Proof::add_assumption_clause (int64_t id, const vector &c, void Proof::add_assumption (int a) { // a is already external - assert (clause.empty ()); - assert (proof_chain.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (proof_chain.empty ()); clause.push_back (a); add_assumption (); } void Proof::add_constraint (const vector &c) { // literals of c are already external - assert (clause.empty ()); - assert (proof_chain.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (proof_chain.empty ()); for (const auto &lit : c) clause.push_back (lit); add_constraint (); @@ -334,8 +338,8 @@ void Proof::add_constraint (const vector &c) { void Proof::add_assumption_clause (int64_t id, int lit, const vector &chain) { - assert (clause.empty ()); - assert (proof_chain.empty ()); + CADICAL_assert (clause.empty ()); + CADICAL_assert (proof_chain.empty ()); clause.push_back (lit); for (const auto &cid : chain) proof_chain.push_back (cid); @@ -354,7 +358,7 @@ void Proof::delete_clause (Clause *c) { void Proof::delete_clause (int64_t id, bool r, const vector &c) { LOG (c, "PROOF deleting from proof"); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); add_literals (c); clause_id = id; redundant = r; @@ -363,7 +367,7 @@ void Proof::delete_clause (int64_t id, bool r, const vector &c) { void Proof::weaken_minus (Clause *c) { LOG (c, "PROOF weaken minus of"); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); add_literals (c); clause_id = c->id; weaken_minus (); @@ -371,7 +375,7 @@ void Proof::weaken_minus (Clause *c) { void Proof::weaken_minus (int64_t id, const vector &c) { LOG (c, "PROOF deleting from proof"); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); add_literals (c); clause_id = id; weaken_minus (); @@ -389,7 +393,7 @@ void Proof::weaken_plus (int64_t id, const vector &c) { void Proof::delete_unit_clause (int64_t id, const int lit) { LOG ("PROOF deleting unit from proof %d", lit); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); add_literal (lit); clause_id = id; redundant = false; @@ -398,7 +402,7 @@ void Proof::delete_unit_clause (int64_t id, const int lit) { void Proof::finalize_clause (Clause *c) { LOG (c, "PROOF finalizing clause"); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); add_literals (c); clause_id = c->id; finalize_clause (); @@ -406,7 +410,7 @@ void Proof::finalize_clause (Clause *c) { void Proof::finalize_clause (int64_t id, const vector &c) { LOG (c, "PROOF finalizing clause"); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); for (const auto &lit : c) add_literal (lit); clause_id = id; @@ -415,7 +419,7 @@ void Proof::finalize_clause (int64_t id, const vector &c) { void Proof::finalize_unit (int64_t id, int lit) { LOG ("PROOF finalizing clause %d", lit); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); add_literal (lit); clause_id = id; finalize_clause (); @@ -423,7 +427,7 @@ void Proof::finalize_unit (int64_t id, int lit) { void Proof::finalize_external_unit (int64_t id, int lit) { LOG ("PROOF finalizing clause %d", lit); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); clause.push_back (lit); clause_id = id; finalize_clause (); @@ -437,7 +441,7 @@ void Proof::finalize_external_unit (int64_t id, int lit) { void Proof::flush_clause (Clause *c) { LOG (c, "PROOF flushing falsified literals in"); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); const bool antecedents = (internal->lrat || internal->frat); for (int i = 0; i < c->size; i++) { int internal_lit = c->literals[i]; @@ -468,7 +472,7 @@ void Proof::flush_clause (Clause *c) { void Proof::strengthen_clause (Clause *c, int remove, const vector &chain) { LOG (c, "PROOF strengthen by removing %d in", remove); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); for (int i = 0; i < c->size; i++) { int internal_lit = c->literals[i]; if (internal_lit == remove) @@ -488,7 +492,7 @@ void Proof::strengthen_clause (Clause *c, int remove, void Proof::otfs_strengthen_clause (Clause *c, const std::vector &old, const vector &chain) { LOG (c, "PROOF otfs strengthen"); - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); for (int i = 0; i < c->size; i++) { int internal_lit = c->literals[i]; add_literal (internal_lit); @@ -512,7 +516,7 @@ void Proof::strengthen (int64_t id) { void Proof::add_original_clause (bool restore) { LOG (clause, "PROOF adding original external clause"); - assert (clause_id); + CADICAL_assert (clause_id); for (auto &tracer : tracers) { tracer->add_original_clause (clause_id, false, clause, restore); @@ -524,7 +528,7 @@ void Proof::add_original_clause (bool restore) { void Proof::add_derived_clause () { LOG (clause, "PROOF adding derived external clause (redundant: %d)", redundant); - assert (clause_id); + CADICAL_assert (clause_id); for (auto &tracer : tracers) { tracer->add_derived_clause (clause_id, redundant, clause, proof_chain); } @@ -544,7 +548,7 @@ void Proof::delete_clause () { void Proof::demote_clause () { LOG (clause, "PROOF demoting external clause"); - assert (!redundant); + CADICAL_assert (!redundant); for (auto &tracer : tracers) { tracer->demote_clause (clause_id, clause); } @@ -589,7 +593,7 @@ void Proof::add_assumption_clause () { void Proof::add_assumption () { LOG (clause, "PROOF adding assumption"); - assert (clause.size () == 1); + CADICAL_assert (clause.size () == 1); for (auto &tracer : tracers) { tracer->add_assumption (clause.back ()); } @@ -655,3 +659,5 @@ void Proof::conclude_unknown (const vector &trail) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/propagate.cpp b/src/sat/cadical/cadical_propagate.cpp similarity index 90% rename from src/sat/cadical/propagate.cpp rename to src/sat/cadical/cadical_propagate.cpp index de3dfbb45..a4f21ee6a 100644 --- a/src/sat/cadical/propagate.cpp +++ b/src/sat/cadical/cadical_propagate.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -37,7 +41,7 @@ static Clause *decision_reason = &decision_reason_clause; inline int Internal::assignment_level (int lit, Clause *reason) { - assert (opts.chrono || external_prop); + CADICAL_assert (opts.chrono || external_prop); if (!reason || reason == external_reason) return level; @@ -46,7 +50,7 @@ inline int Internal::assignment_level (int lit, Clause *reason) { for (const auto &other : *reason) { if (other == lit) continue; - assert (val (other)); + CADICAL_assert (val (other)); int tmp = var (other).level; if (tmp > res) res = tmp; @@ -65,11 +69,11 @@ void Internal::build_chain_for_units (int lit, Clause *reason, return; else if (!opts.chrono && level && !forced) return; // not decision level 0 - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); for (auto &reason_lit : *reason) { if (lit == reason_lit) continue; - assert (val (reason_lit)); + CADICAL_assert (val (reason_lit)); if (!val (reason_lit)) continue; const int signed_reason_lit = val (reason_lit) * reason_lit; @@ -85,12 +89,12 @@ void Internal::build_chain_for_units (int lit, Clause *reason, void Internal::build_chain_for_empty () { if (!lrat || !lrat_chain.empty ()) return; - assert (!level); - assert (lrat_chain.empty ()); - assert (conflict); + CADICAL_assert (!level); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (conflict); LOG (conflict, "lrat for global empty clause with conflict"); for (auto &lit : *conflict) { - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); int64_t id = unit_id (-lit); lrat_chain.push_back (id); } @@ -106,12 +110,12 @@ inline void Internal::search_assign (int lit, Clause *reason) { const int idx = vidx (lit); const bool from_external = reason == external_reason; - assert (!val (idx)); - assert (!flags (idx).eliminated () || reason == decision_reason || + CADICAL_assert (!val (idx)); + CADICAL_assert (!flags (idx).eliminated () || reason == decision_reason || reason == external_reason); Var &v = var (idx); int lit_level; - assert (!lrat || level || reason == external_reason || + CADICAL_assert (!lrat || level || reason == external_reason || reason == decision_reason || !lrat_chain.empty ()); // The following cases are explained in the two comments above before // 'decision_reason' and 'assignment_level'. @@ -137,16 +141,16 @@ inline void Internal::search_assign (int lit, Clause *reason) { v.level = lit_level; v.trail = trail.size (); v.reason = reason; - assert ((int) num_assigned < max_var); - assert (num_assigned == trail.size ()); + CADICAL_assert ((int) num_assigned < max_var); + CADICAL_assert (num_assigned == trail.size ()); num_assigned++; if (!lit_level && !from_external) learn_unit_clause (lit); // increases 'stats.fixed' - assert (lit_level || !from_external); + CADICAL_assert (lit_level || !from_external); const signed char tmp = sign (lit); set_val (idx, tmp); - assert (val (lit) > 0); // Just a bit paranoid but useful. - assert (val (-lit) < 0); // Ditto. + CADICAL_assert (val (lit) > 0); // Just a bit paranoid but useful. + CADICAL_assert (val (-lit) < 0); // Ditto. if (!searching_lucky_phases) phases.saved[idx] = tmp; // phase saving during search trail.push_back (lit); @@ -161,7 +165,9 @@ inline void Internal::search_assign (int lit, Clause *reason) { const Watches &ws = watches (-lit); if (!ws.empty ()) { const Watch &w = ws[0]; +#ifndef WIN32 __builtin_prefetch (&w, 0, 1); +#endif } } lrat_chain.clear (); @@ -176,7 +182,7 @@ inline void Internal::search_assign (int lit, Clause *reason) { // which is called directly in 'propagate' below and thus is inlined. void Internal::assign_unit (int lit) { - assert (!level); + CADICAL_assert (!level); search_assign (lit, 0); } @@ -185,7 +191,7 @@ void Internal::assign_unit (int lit) { void Internal::search_assume_decision (int lit) { require_mode (SEARCH); - assert (propagated == trail.size ()); + CADICAL_assert (propagated == trail.size ()); new_trail_level (lit); notify_decision (); LOG ("search decide %d", lit); @@ -227,7 +233,7 @@ bool Internal::propagate () { if (level) require_mode (SEARCH); - assert (!unsat); + CADICAL_assert (!unsat); LOG ("starting propagate"); START (propagate); @@ -259,7 +265,7 @@ bool Internal::propagate () { if (w.binary ()) { - // assert (w.clause->redundant || !w.clause->garbage); + // CADICAL_assert (w.clause->redundant || !w.clause->garbage); // In principle we can ignore garbage binary clauses too, but that // would require to dereference the clause pointer all the time with @@ -296,7 +302,7 @@ bool Internal::propagate () { } } else { - assert (w.clause->size > 2); + CADICAL_assert (w.clause->size > 2); if (conflict) break; // Stop if there was a binary conflict already. @@ -355,14 +361,14 @@ bool Internal::propagate () { if (v < 0) { // need second search starting at the head? k = lits + 2; - assert (w.clause->pos <= size); + CADICAL_assert (w.clause->pos <= size); while (k != middle && (v = val (r = *k)) < 0) k++; } w.clause->pos = k - lits; // always save position - assert (lits + 2 <= k), assert (k <= w.clause->end ()); + CADICAL_assert (lits + 2 <= k), CADICAL_assert (k <= w.clause->end ()); if (v > 0) { @@ -388,7 +394,7 @@ bool Internal::propagate () { } else if (!u) { - assert (v < 0); + CADICAL_assert (v < 0); // The other watch is unassigned ('!u') and all other literals // assigned to false (still 'v < 0'), thus we found a unit. @@ -414,7 +420,7 @@ bool Internal::propagate () { // another literal in the clause at that higher assignment // level and watch that instead of 'lit'. - assert (size > 2); + CADICAL_assert (size > 2); int pos, s = 0; @@ -422,8 +428,8 @@ bool Internal::propagate () { if (var (s = lits[pos]).level == other_level) break; - assert (s); - assert (pos < size); + CADICAL_assert (s); + CADICAL_assert (pos < size); LOG (w.clause, "unwatch %d in", lit); lits[pos] = lit; @@ -436,8 +442,8 @@ bool Internal::propagate () { } } else { - assert (u < 0); - assert (v < 0); + CADICAL_assert (u < 0); + CADICAL_assert (v < 0); // The other watch is assigned false ('u < 0') and all other // literals as well (still 'v < 0'), thus we found a conflict. @@ -495,8 +501,8 @@ bool Internal::propagate () { void Internal::propergate () { - assert (!conflict); - assert (propagated == trail.size ()); + CADICAL_assert (!conflict); + CADICAL_assert (propagated == trail.size ()); while (propergated != trail.size ()) { @@ -513,7 +519,7 @@ void Internal::propergate () { const Watch w = *j++ = *i++; if (w.binary ()) { - assert (val (w.blit) > 0); + CADICAL_assert (val (w.blit) > 0); continue; } if (w.clause->garbage) { @@ -529,7 +535,7 @@ void Internal::propergate () { // TODO: check if u == 0 can happen. if (u > 0) continue; - assert (u < 0); + CADICAL_assert (u < 0); const int size = w.clause->size; const literal_iterator middle = lits + w.clause->pos; @@ -544,15 +550,15 @@ void Internal::propergate () { if (v < 0) { k = lits + 2; - assert (w.clause->pos <= size); + CADICAL_assert (w.clause->pos <= size); while (k != middle && (v = val (r = *k)) < 0) k++; } - assert (lits + 2 <= k), assert (k <= w.clause->end ()); + CADICAL_assert (lits + 2 <= k), CADICAL_assert (k <= w.clause->end ()); w.clause->pos = k - lits; - assert (v > 0); + CADICAL_assert (v > 0); LOG (w.clause, "unwatch %d in", lit); @@ -576,3 +582,5 @@ void Internal::propergate () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/queue.cpp b/src/sat/cadical/cadical_queue.cpp similarity index 87% rename from src/sat/cadical/queue.cpp rename to src/sat/cadical/cadical_queue.cpp index cd2b19491..c8b5b8030 100644 --- a/src/sat/cadical/queue.cpp +++ b/src/sat/cadical/cadical_queue.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // Slightly different than 'bump_variable' since the variable is not @@ -10,15 +14,15 @@ inline void Internal::init_enqueue (int idx) { if (opts.reverse) { l.prev = 0; if (queue.first) { - assert (!links[queue.first].prev); + CADICAL_assert (!links[queue.first].prev); links[queue.first].prev = idx; btab[idx] = btab[queue.first] - 1; } else { - assert (!queue.last); + CADICAL_assert (!queue.last); queue.last = idx; btab[idx] = 0; } - assert (btab[idx] <= stats.bumped); + CADICAL_assert (btab[idx] <= stats.bumped); l.next = queue.first; queue.first = idx; if (!queue.unassigned) @@ -26,10 +30,10 @@ inline void Internal::init_enqueue (int idx) { } else { l.next = 0; if (queue.last) { - assert (!links[queue.last].next); + CADICAL_assert (!links[queue.last].next); links[queue.last].next = idx; } else { - assert (!queue.first); + CADICAL_assert (!queue.first); queue.first = idx; } btab[idx] = ++stats.bumped; @@ -47,7 +51,7 @@ inline void Internal::init_enqueue (int idx) { void Internal::init_queue (int old_max_var, int new_max_var) { LOG ("initializing VMTF queue from %d to %d", old_max_var + 1, new_max_var); - assert (old_max_var < new_max_var); + CADICAL_assert (old_max_var < new_max_var); // New variables can be created that can invoke enlarge anytime (eg via // calls during ipasir-up call-backs), thus assuming (!level) is not // correct @@ -88,3 +92,5 @@ void Internal::shuffle_queue () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/random.cpp b/src/sat/cadical/cadical_random.cpp similarity index 94% rename from src/sat/cadical/random.cpp rename to src/sat/cadical/cadical_random.cpp index b5f02e71e..85d022e8f 100644 --- a/src/sat/cadical/random.cpp +++ b/src/sat/cadical/cadical_random.cpp @@ -1,3 +1,5 @@ +#include "global.h" + #include "internal.hpp" /*------------------------------------------------------------------------*/ @@ -46,6 +48,8 @@ // does not have any effect. TODO: add a similar machine identity hashing // function for other operating systems (Windows and macOS). +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { static uint64_t hash_machine_identifier () { @@ -55,7 +59,7 @@ static uint64_t hash_machine_identifier () { char buffer[128]; memset (buffer, 0, sizeof buffer); size_t bytes = fread (buffer, 1, sizeof buffer - 1, file); - assert (bytes); + CADICAL_assert (bytes); fclose (file); if (bytes && bytes < sizeof buffer) { buffer[bytes] = 0; @@ -68,6 +72,8 @@ static uint64_t hash_machine_identifier () { } // namespace CaDiCaL +ABC_NAMESPACE_IMPL_END + /*------------------------------------------------------------------------*/ // On our Linux cluster where we used an NFS mounted root disk the @@ -78,7 +84,7 @@ static uint64_t hash_machine_identifier () { // work. As an additional measure to increase the possibility to get // different seeds we are now also using network addresses (explicitly). -#ifndef __WIN32 +#ifndef WIN32 extern "C" { #include @@ -91,6 +97,8 @@ extern "C" { #endif +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { static uint64_t hash_network_addresses () { @@ -102,7 +110,7 @@ static uint64_t hash_network_addresses () { // you really need to run 'mobical' on a Windows cluster where each node // has identical IP addresses. -#ifndef __WIN32 +#ifndef WIN32 struct ifaddrs *addrs; if (!getifaddrs (&addrs)) { for (struct ifaddrs *addr = addrs; addr; addr = addr->ifa_next) { @@ -135,6 +143,8 @@ static uint64_t hash_network_addresses () { } // namespace CaDiCaL +ABC_NAMESPACE_IMPL_END + /*------------------------------------------------------------------------*/ // Hash the current wall-clock time in seconds. @@ -143,6 +153,8 @@ extern "C" { #include } +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { static uint64_t hash_time () { @@ -153,15 +165,23 @@ static uint64_t hash_time () { } // namespace CaDiCaL +ABC_NAMESPACE_IMPL_END + /*------------------------------------------------------------------------*/ // Hash the process identified. extern "C" { #include +#ifdef WIN32 +#include +#else #include +#endif } +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { static uint64_t hash_process () { @@ -172,12 +192,16 @@ static uint64_t hash_process () { } // namespace CaDiCaL +ABC_NAMESPACE_IMPL_END + /*------------------------------------------------------------------------*/ // Hash the current number of clock cycles. #include +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { static uint64_t hash_clock_cycles () { @@ -205,3 +229,5 @@ Random::Random () : state (1) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/reap.cpp b/src/sat/cadical/cadical_reap.cpp similarity index 70% rename from src/sat/cadical/reap.cpp rename to src/sat/cadical/cadical_reap.cpp index 8c383cb66..4625c44db 100644 --- a/src/sat/cadical/reap.cpp +++ b/src/sat/cadical/cadical_reap.cpp @@ -1,15 +1,28 @@ +#include "global.h" + #include "reap.hpp" #include #include #include +#ifdef _MSC_VER +#include +static inline int __builtin_clz(unsigned x) { + unsigned long r; + _BitScanReverse(&r, x); + return (int)(r ^ 31); +} +#endif + +ABC_NAMESPACE_IMPL_START + void Reap::init () { for (auto &bucket : buckets) bucket = {0}; - assert (!num_elements); - assert (!last_deleted); + CADICAL_assert (!num_elements); + CADICAL_assert (!last_deleted); min_bucket = 32; - assert (!max_bucket); + CADICAL_assert (!max_bucket); } void Reap::release () { @@ -31,7 +44,7 @@ static inline unsigned leading_zeroes_of_unsigned (unsigned x) { } void Reap::push (unsigned e) { - assert (last_deleted <= e); + CADICAL_assert (last_deleted <= e); const unsigned diff = e ^ last_deleted; const unsigned bucket = 32 - leading_zeroes_of_unsigned (diff); buckets[bucket].push_back (e); @@ -39,16 +52,16 @@ void Reap::push (unsigned e) { min_bucket = bucket; if (max_bucket < bucket) max_bucket = bucket; - assert (num_elements != UINT_MAX); + CADICAL_assert (num_elements != UINT_MAX); num_elements++; } unsigned Reap::pop () { - assert (num_elements > 0); + CADICAL_assert (num_elements > 0); unsigned i = min_bucket; for (;;) { - assert (i < 33); - assert (i <= max_bucket); + CADICAL_assert (i < 33); + CADICAL_assert (i <= max_bucket); std::vector &s = buckets[i]; if (s.empty ()) { min_bucket = ++i; @@ -60,7 +73,7 @@ unsigned Reap::pop () { const auto begin = std::begin (s); const auto end = std::end (s); auto q = std::begin (s); - assert (begin < end); + CADICAL_assert (begin < end); for (auto p = begin; p != end; ++p) { const unsigned tmp = *p; if (tmp >= res) @@ -74,9 +87,9 @@ unsigned Reap::pop () { continue; const unsigned other = *p; const unsigned diff = other ^ res; - assert (sizeof (unsigned) == 4); + CADICAL_assert (sizeof (unsigned) == 4); const unsigned j = 32 - leading_zeroes_of_unsigned (diff); - assert (j < i); + CADICAL_assert (j < i); buckets[j].push_back (other); if (min_bucket > j) min_bucket = j; @@ -85,31 +98,31 @@ unsigned Reap::pop () { s.clear (); if (i && max_bucket == i) { -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (unsigned j = i + 1; j < 33; j++) - assert (buckets[j].empty ()); + CADICAL_assert (buckets[j].empty ()); #endif if (s.empty ()) max_bucket = i - 1; } } else { res = last_deleted; - assert (!buckets[0].empty ()); - assert (buckets[0].at (0) == res); + CADICAL_assert (!buckets[0].empty ()); + CADICAL_assert (buckets[0].at (0) == res); buckets[0].pop_back (); } if (min_bucket == i) { -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (unsigned j = 0; j < i; j++) - assert (buckets[j].empty ()); + CADICAL_assert (buckets[j].empty ()); #endif if (s.empty ()) min_bucket = std::min ((int) (i + 1), 32); } --num_elements; - assert (last_deleted <= res); + CADICAL_assert (last_deleted <= res); last_deleted = res; return res; @@ -117,7 +130,7 @@ unsigned Reap::pop () { } void Reap::clear () { - assert (max_bucket <= 32); + CADICAL_assert (max_bucket <= 32); for (unsigned i = 0; i < 33; i++) buckets[i].clear (); num_elements = 0; @@ -125,3 +138,5 @@ void Reap::clear () { min_bucket = 32; max_bucket = 0; } + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/reduce.cpp b/src/sat/cadical/cadical_reduce.cpp similarity index 96% rename from src/sat/cadical/reduce.cpp rename to src/sat/cadical/cadical_reduce.cpp index 632db3673..cc6f35228 100644 --- a/src/sat/cadical/reduce.cpp +++ b/src/sat/cadical/cadical_reduce.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -114,7 +118,7 @@ void Internal::mark_useless_redundant_clauses_as_garbage () { if (c->glue <= tier2limit && used >= max_used - 1) continue; if (c->hyper) { // Hyper binary and ternary resolvents - assert (c->size <= 3); // are only kept for one reduce round + CADICAL_assert (c->size <= 3); // are only kept for one reduce round if (!used) mark_garbage (c); // unless continue; // used recently. @@ -167,7 +171,7 @@ void Internal::mark_useless_redundant_clauses_as_garbage () { // If chronological backtracking produces out-of-order assigned units, then // it is necessary to completely propagate them at the root level in order // to derive all implied units. Otherwise the blocking literals in -// 'flush_watches' are messed up and assertion 'FW1' fails. +// 'flush_watches' are messed up and CADICAL_assertion 'FW1' fails. bool Internal::propagate_out_of_order_units () { if (!level) @@ -175,7 +179,7 @@ bool Internal::propagate_out_of_order_units () { int oou = 0; for (size_t i = control[1].trail; !oou && i < trail.size (); i++) { const int lit = trail[i]; - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); if (var (lit).level) continue; LOG ("found out-of-order assigned unit %d", oou); @@ -183,7 +187,7 @@ bool Internal::propagate_out_of_order_units () { } if (!oou) return true; - assert (opts.chrono || external_prop); + CADICAL_assert (opts.chrono || external_prop); backtrack (0); if (propagate ()) return true; @@ -276,3 +280,5 @@ DONE: } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/rephase.cpp b/src/sat/cadical/cadical_rephase.cpp similarity index 98% rename from src/sat/cadical/rephase.cpp rename to src/sat/cadical/cadical_rephase.cpp index 74ebb988e..3b2e62526 100644 --- a/src/sat/cadical/rephase.cpp +++ b/src/sat/cadical/cadical_rephase.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -281,7 +285,7 @@ void Internal::rephase () { break; } } else { - assert (!stable && opts.walk && opts.walknonstable); + CADICAL_assert (!stable && opts.walk && opts.walknonstable); // flipping,(random,best,walk,flipping,best,walk)^\omega if (!count) type = rephase_flipping (); @@ -310,7 +314,7 @@ void Internal::rephase () { break; } } - assert (type); + CADICAL_assert (type); int64_t delta = opts.rephaseint * (stats.rephased.total + 1); lim.rephase = stats.conflicts + delta; @@ -334,3 +338,5 @@ void Internal::rephase () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/report.cpp b/src/sat/cadical/cadical_report.cpp similarity index 97% rename from src/sat/cadical/report.cpp rename to src/sat/cadical/cadical_report.cpp index cda252edb..18271d17e 100644 --- a/src/sat/cadical/report.cpp +++ b/src/sat/cadical/cadical_report.cpp @@ -1,8 +1,12 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { -#ifndef QUIET +#ifndef CADICAL_QUIET /*------------------------------------------------------------------------*/ @@ -167,7 +171,7 @@ void Internal::report (char type, int verbose) { if (opts.quiet || (verbose > opts.verbose)) return; if (!reported) { - assert (!lim.report); + CADICAL_assert (!lim.report); reported = true; MSG ("%stime measured in %s time %s%s", tout.magenta_code (), internal->opts.realtime ? "real" : "process", @@ -177,7 +181,7 @@ void Internal::report (char type, int verbose) { Report reports[num_reports]; int n = 0; #define REPORT(HEAD, PREC, MIN, EXPR) \ - assert (n < num_reports); \ + CADICAL_assert (n < num_reports); \ reports[n++] = Report (HEAD, PREC, MIN, (double) (EXPR)); REPORTS #undef REPORT @@ -287,10 +291,12 @@ void Internal::report (char type, int verbose) { fflush (stdout); } -#else // ifndef QUIET +#else // ifndef CADICAL_QUIET void Internal::report (char, int) {} #endif } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/resources.cpp b/src/sat/cadical/cadical_resources.cpp similarity index 97% rename from src/sat/cadical/resources.cpp rename to src/sat/cadical/cadical_resources.cpp index 712a8ec67..9a2b421e5 100644 --- a/src/sat/cadical/resources.cpp +++ b/src/sat/cadical/cadical_resources.cpp @@ -1,3 +1,5 @@ +#include "global.h" + #include "internal.hpp" /*------------------------------------------------------------------------*/ @@ -9,7 +11,7 @@ extern "C" { -#ifdef __WIN32 +#ifdef WIN32 #ifndef __WIN32_WINNT #define __WIN32_WINNT 0x0600 @@ -37,11 +39,13 @@ extern "C" { #include } +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ -#ifdef __WIN32 +#ifdef WIN32 double absolute_real_time () { FILETIME f; @@ -104,7 +108,7 @@ double Internal::process_time () const { /*------------------------------------------------------------------------*/ -#ifdef __WIN32 +#ifdef WIN32 uint64_t current_resident_set_size () { PROCESS_MEMORY_COUNTERS pmc; @@ -158,3 +162,5 @@ uint64_t current_resident_set_size () { /*------------------------------------------------------------------------*/ } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/restart.cpp b/src/sat/cadical/cadical_restart.cpp similarity index 97% rename from src/sat/cadical/restart.cpp rename to src/sat/cadical/cadical_restart.cpp index 799df94e9..1c6dc44c8 100644 --- a/src/sat/cadical/restart.cpp +++ b/src/sat/cadical/cadical_restart.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // As observed by Chanseok Oh and implemented in MapleSAT solvers too, @@ -21,7 +25,7 @@ bool Internal::stabilizing () { if (stable && opts.stabilizeonly) return true; if (!inc.stabilize) { - assert (!stable); + CADICAL_assert (!stable); if (stats.conflicts <= lim.stabilize) return false; } else if (stats.ticks.search[stable] <= lim.stabilize) @@ -112,7 +116,7 @@ int Internal::reuse_trail () { if (!opts.restartreusetrail) return trivial_decisions; int next_decision = next_decision_variable (); - assert (1 <= next_decision); + CADICAL_assert (1 <= next_decision); int res = trivial_decisions; if (use_scores ()) { while (res < level) { @@ -157,3 +161,5 @@ void Internal::restart () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/restore.cpp b/src/sat/cadical/cadical_restore.cpp similarity index 90% rename from src/sat/cadical/restore.cpp rename to src/sat/cadical/cadical_restore.cpp index cd1c9721d..f9551fb5e 100644 --- a/src/sat/cadical/restore.cpp +++ b/src/sat/cadical/cadical_restore.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -51,14 +55,14 @@ void External::restore_clause (const vector::const_iterator &begin, const vector::const_iterator &end, const int64_t id) { LOG (begin, end, "restoring external clause[%" PRId64 "]", id); - assert (eclause.empty ()); - assert (id); + CADICAL_assert (eclause.empty ()); + CADICAL_assert (id); for (auto p = begin; p != end; p++) { eclause.push_back (*p); if (internal->proof && internal->lrat) { const auto &elit = *p; unsigned eidx = (elit > 0) + 2u * (unsigned) abs (elit); - assert ((size_t) eidx < ext_units.size ()); + CADICAL_assert ((size_t) eidx < ext_units.size ()); const int64_t id = ext_units[eidx]; bool added = ext_flags[abs (elit)]; if (id && !added) { @@ -83,7 +87,7 @@ void External::restore_clause (const vector::const_iterator &begin, void External::restore_clauses () { - assert (internal->opts.restoreall == 2 || !tainted.empty ()); + CADICAL_assert (internal->opts.restoreall == 2 || !tainted.empty ()); START (restore); internal->stats.restorations++; @@ -97,7 +101,7 @@ void External::restore_clauses () { PHASE ("restore", internal->stats.restorations, "forced to restore all clauses"); -#ifndef QUIET +#ifndef CADICAL_QUIET { unsigned numtainted = 0; for (const auto b : tainted) @@ -120,7 +124,7 @@ void External::restore_clauses () { clauses.weakened++; - assert (!*p); + CADICAL_assert (!*p); const auto saved = q; // Save old start. *q++ = *p++; // Copy zero '0'. @@ -129,7 +133,7 @@ void External::restore_clauses () { int tlit = 0; // Negation tainted. int elit; // - assert (p != end_of_extension); + CADICAL_assert (p != end_of_extension); // while ((elit = *q++ = *p++)) { @@ -138,7 +142,7 @@ void External::restore_clauses () { LOG ("negation of witness literal %d tainted", tlit); } - assert (p != end_of_extension); + CADICAL_assert (p != end_of_extension); } // now copy the id of the clause @@ -146,8 +150,8 @@ void External::restore_clauses () { LOG ("id is %" PRId64, id); *q++ = *p++; *q++ = *p++; - assert (id); - assert (!*p); + CADICAL_assert (id); + CADICAL_assert (!*p); *q++ = *p++; // Now find 'end_of_clause' (clause starts at 'p') and at the same time @@ -160,7 +164,7 @@ void External::restore_clauses () { satisfied = elit; end_of_clause++; } - assert (id); + CADICAL_assert (id); // Do not apply our 'FLUSH' rule to remove satisfied (implied) clauses // if the corresponding option is set simply by resetting 'satisfied'. @@ -199,7 +203,7 @@ void External::restore_clauses () { extension.resize (q - extension.begin ()); shrink_vector (extension); -#ifndef QUIET +#ifndef CADICAL_QUIET if (clauses.satisfied) PHASE ("restore", internal->stats.restorations, "removed %" PRId64 " satisfied %.0f%% of %" PRId64 @@ -244,20 +248,20 @@ void External::restore_clauses () { p = extension.end (); while (p != begin_of_extension) { while (*--p) - assert (p != begin_of_extension); + CADICAL_assert (p != begin_of_extension); int elit; - assert (p != begin_of_extension); + CADICAL_assert (p != begin_of_extension); --p; - assert (p != begin_of_extension); - assert (*p || *(p - 1)); + CADICAL_assert (p != begin_of_extension); + CADICAL_assert (*p || *(p - 1)); --p; - assert (p != begin_of_extension); - assert (!*p); + CADICAL_assert (p != begin_of_extension); + CADICAL_assert (!*p); --p; - assert (p != begin_of_extension); + CADICAL_assert (p != begin_of_extension); while ((elit = *--p)) { mark (witness, elit); - assert (p != begin_of_extension); + CADICAL_assert (p != begin_of_extension); } } @@ -265,3 +269,5 @@ void External::restore_clauses () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/score.cpp b/src/sat/cadical/cadical_score.cpp similarity index 92% rename from src/sat/cadical/score.cpp rename to src/sat/cadical/cadical_score.cpp index 578d553a8..420357929 100644 --- a/src/sat/cadical/score.cpp +++ b/src/sat/cadical/cadical_score.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // This initializes variables on the binary 'scores' heap also with @@ -20,7 +24,7 @@ void Internal::shuffle_scores () { return; if (!opts.shufflescores) return; - assert (!level); + CADICAL_assert (!level); stats.shuffled++; LOG ("shuffling scores"); vector shuffle; @@ -49,3 +53,5 @@ void Internal::shuffle_scores () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/shrink.cpp b/src/sat/cadical/cadical_shrink.cpp similarity index 82% rename from src/sat/cadical/shrink.cpp rename to src/sat/cadical/cadical_shrink.cpp index e61935117..201c220e7 100644 --- a/src/sat/cadical/shrink.cpp +++ b/src/sat/cadical/cadical_shrink.cpp @@ -1,6 +1,10 @@ +#include "global.h" + #include "internal.hpp" #include "reap.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void Internal::reset_shrinkable () { @@ -10,7 +14,7 @@ void Internal::reset_shrinkable () { for (const auto &lit : shrinkable) { LOG ("resetting lit %i", lit); Flags &f = flags (lit); - assert (f.shrinkable); + CADICAL_assert (f.shrinkable); f.shrinkable = false; #ifdef LOGGING ++reset; @@ -24,14 +28,14 @@ void Internal::mark_shrinkable_as_removable ( #ifdef LOGGING size_t marked = 0, reset = 0; #endif -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG unsigned kept = 0, minireset = 0; for (; minimized_start < minimized.size (); ++minimized_start) { const int lit = minimized[minimized_start]; Flags &f = flags (lit); const Var &v = var (lit); if (v.level == blevel) { - assert (!f.poison); + CADICAL_assert (!f.poison); ++minireset; } else ++kept; @@ -45,8 +49,8 @@ void Internal::mark_shrinkable_as_removable ( for (const int lit : shrinkable) { Flags &f = flags (lit); - assert (f.shrinkable); - assert (!f.poison); + CADICAL_assert (f.shrinkable); + CADICAL_assert (!f.poison); f.shrinkable = false; #ifdef LOGGING ++reset; @@ -65,11 +69,11 @@ void Internal::mark_shrinkable_as_removable ( int inline Internal::shrink_literal (int lit, int blevel, unsigned max_trail) { - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); Flags &f = flags (lit); Var &v = var (lit); - assert (v.level <= blevel); + CADICAL_assert (v.level <= blevel); if (!v.level) { LOG ("skipping root level assigned %d", (lit)); @@ -77,14 +81,14 @@ int inline Internal::shrink_literal (int lit, int blevel, } if (v.reason == external_reason) { - assert (!opts.exteagerreasons); + CADICAL_assert (!opts.exteagerreasons); v.reason = learn_external_reason_clause (-lit, 0, true); if (!v.reason) { - assert (!v.level); + CADICAL_assert (!v.level); return 0; } } - assert (v.reason != external_reason); + CADICAL_assert (v.reason != external_reason); if (f.shrinkable) { LOG ("skipping already shrinkable literal %d", (lit)); return 0; @@ -110,7 +114,7 @@ int inline Internal::shrink_literal (int lit, int blevel, f.poison = false; shrinkable.push_back (lit); if (opts.shrinkreap) { - assert (max_trail < trail.size ()); + CADICAL_assert (max_trail < trail.size ()); const unsigned dist = max_trail - v.trail; reap.push (dist); } @@ -121,11 +125,11 @@ unsigned Internal::shrunken_block_uip ( int uip, int blevel, std::vector::reverse_iterator &rbegin_block, std::vector::reverse_iterator &rend_block, std::vector::size_type minimized_start, const int uip0) { - assert (clause[0] == uip0); + CADICAL_assert (clause[0] == uip0); LOG ("UIP on level %u, uip: %i (replacing by %i)", blevel, uip, uip0); - assert (rend_block > rbegin_block); - assert (rend_block < clause.rend ()); + CADICAL_assert (rend_block > rbegin_block); + CADICAL_assert (rend_block < clause.rend ()); unsigned block_shrunken = 0; *rbegin_block = -uip; Var &v = var (-uip); @@ -147,10 +151,10 @@ unsigned Internal::shrunken_block_uip ( *p = uip0; // if (lit == -uip) continue; ++block_shrunken; - assert (clause[0] == uip0); + CADICAL_assert (clause[0] == uip0); } mark_shrinkable_as_removable (blevel, minimized_start); - assert (clause[0] == uip0); + CADICAL_assert (clause[0] == uip0); return block_shrunken; } @@ -160,18 +164,18 @@ void inline Internal::shrunken_block_no_uip ( unsigned &block_minimized, const int uip0) { STOP (shrink); START (minimize); - assert (rend_block > rbegin_block); + CADICAL_assert (rend_block > rbegin_block); LOG ("no UIP found, now minimizing"); for (auto p = rbegin_block; p != rend_block; ++p) { - assert (p != clause.rend () - 1); + CADICAL_assert (p != clause.rend () - 1); const int lit = *p; if (opts.minimize && minimize_literal (-lit)) { - assert (!flags (lit).keep); + CADICAL_assert (!flags (lit).keep); ++block_minimized; *p = uip0; } else { flags (lit).keep = true; - assert (flags (lit).keep); + CADICAL_assert (flags (lit).keep); } } STOP (minimize); @@ -182,17 +186,17 @@ void Internal::push_literals_of_block ( const std::vector::reverse_iterator &rbegin_block, const std::vector::reverse_iterator &rend_block, int blevel, unsigned max_trail) { - assert (rbegin_block < rend_block); + CADICAL_assert (rbegin_block < rend_block); for (auto p = rbegin_block; p != rend_block; ++p) { - assert (p != clause.rend () - 1); - assert (!flags (*p).keep); + CADICAL_assert (p != clause.rend () - 1); + CADICAL_assert (!flags (*p).keep); const int lit = *p; LOG ("pushing lit %i of blevel %i", lit, var (lit).level); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG int tmp = #endif shrink_literal (lit, blevel, max_trail); - assert (tmp > 0); + CADICAL_assert (tmp > 0); } } @@ -200,24 +204,24 @@ unsigned inline Internal::shrink_next (int blevel, unsigned &open, unsigned &max_trail) { const auto &t = &trail; if (opts.shrinkreap) { - assert (!reap.empty ()); + CADICAL_assert (!reap.empty ()); const unsigned dist = reap.pop (); --open; - assert (dist <= max_trail); + CADICAL_assert (dist <= max_trail); const unsigned pos = max_trail - dist; - assert (pos < t->size ()); + CADICAL_assert (pos < t->size ()); const int uip = (*t)[pos]; - assert (val (uip) > 0); + CADICAL_assert (val (uip) > 0); LOG ("trying to shrink literal %d at trail[%u] and level %d", uip, pos, blevel); return uip; } else { int uip; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG unsigned init_max_trail = max_trail; #endif do { - assert (max_trail <= init_max_trail); + CADICAL_assert (max_trail <= init_max_trail); uip = (*t)[max_trail--]; } while (!flags (uip).shrinkable); --open; @@ -233,14 +237,14 @@ unsigned inline Internal::shrink_along_reason (int uip, int blevel, unsigned max_trail) { LOG ("shrinking along the reason of lit %i", uip); unsigned open = 0; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG const Flags &f = flags (uip); #endif const Var &v = var (uip); - assert (f.shrinkable); - assert (v.level == blevel); - assert (v.reason); + CADICAL_assert (f.shrinkable); + CADICAL_assert (v.level == blevel); + CADICAL_assert (v.reason); if (opts.minimizeticks) stats.ticks.search[stable]++; @@ -251,7 +255,7 @@ unsigned inline Internal::shrink_along_reason (int uip, int blevel, for (int lit : c) { if (lit == uip) continue; - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); int tmp = shrink_literal (lit, blevel, max_trail); if (tmp < 0) { failed_ptr = true; @@ -273,13 +277,13 @@ Internal::shrink_block (std::vector::reverse_iterator &rbegin_lits, int blevel, unsigned &open, unsigned &block_minimized, const int uip0, unsigned max_trail) { - assert (shrinkable.empty ()); - assert (blevel <= this->level); - assert (open < clause.size ()); - assert (rbegin_lits >= clause.rbegin ()); - assert (rend_block < clause.rend ()); - assert (rbegin_lits < rend_block); - assert (opts.shrink); + CADICAL_assert (shrinkable.empty ()); + CADICAL_assert (blevel <= this->level); + CADICAL_assert (open < clause.size ()); + CADICAL_assert (rbegin_lits >= clause.rbegin ()); + CADICAL_assert (rend_block < clause.rend ()); + CADICAL_assert (rbegin_lits < rend_block); + CADICAL_assert (opts.shrink); #ifdef LOGGING @@ -301,18 +305,18 @@ Internal::shrink_block (std::vector::reverse_iterator &rbegin_lits, if (!failed) { push_literals_of_block (rbegin_lits, rend_block, blevel, max_trail); - assert (!opts.shrinkreap || reap.size () == open); + CADICAL_assert (!opts.shrinkreap || reap.size () == open); - assert (open > 0); + CADICAL_assert (open > 0); while (!failed) { - assert (!opts.shrinkreap || reap.size () == open); + CADICAL_assert (!opts.shrinkreap || reap.size () == open); uip = shrink_next (blevel, open, max_trail); if (open == 0) { break; } open += shrink_along_reason (uip, blevel, resolve_large_clauses, failed, max_trail2); - assert (open >= 1); + CADICAL_assert (open >= 1); } if (!failed) @@ -370,7 +374,7 @@ std::vector::reverse_iterator Internal::minimize_and_shrink_block ( { LOG ("shrinking block"); - assert (rbegin_block < clause.rend () - 1); + CADICAL_assert (rbegin_block < clause.rend () - 1); int blevel; unsigned open = 0; unsigned max_trail; @@ -378,7 +382,7 @@ std::vector::reverse_iterator Internal::minimize_and_shrink_block ( // find begining of block; std::vector::reverse_iterator rend_block; { - assert (rbegin_block <= clause.rend ()); + CADICAL_assert (rbegin_block <= clause.rend ()); const int lit = *rbegin_block; const int idx = vidx (lit); blevel = vtab[idx].level; @@ -388,7 +392,7 @@ std::vector::reverse_iterator Internal::minimize_and_shrink_block ( rend_block = rbegin_block; bool finished; do { - assert (rend_block < clause.rend () - 1); + CADICAL_assert (rend_block < clause.rend () - 1); const int lit = *(++rend_block); const int idx = vidx (lit); finished = (blevel != vtab[idx].level); @@ -401,10 +405,10 @@ std::vector::reverse_iterator Internal::minimize_and_shrink_block ( } while (!finished); } - assert (open > 0); - assert (open < clause.size ()); - assert (rbegin_block < clause.rend ()); - assert (rend_block < clause.rend ()); + CADICAL_assert (open > 0); + CADICAL_assert (open < clause.size ()); + CADICAL_assert (rbegin_block < clause.rend ()); + CADICAL_assert (rend_block < clause.rend ()); unsigned block_shrunken = 0, block_minimized = 0; if (open < 2) { @@ -424,7 +428,7 @@ std::vector::reverse_iterator Internal::minimize_and_shrink_block ( } void Internal::shrink_and_minimize_clause () { - assert (opts.minimize || opts.shrink > 0); + CADICAL_assert (opts.minimize || opts.shrink > 0); LOG (clause, "shrink first UIP clause"); START (shrink); @@ -434,7 +438,7 @@ void Internal::shrink_and_minimize_clause () { unsigned total_shrunken = 0; unsigned total_minimized = 0; - LOG (clause, "shrink first UIP clause (asserting lit: %i)", clause[0]); + LOG (clause, "shrink first UIP clause (CADICAL_asserting lit: %i)", clause[0]); auto rend_lits = clause.rend () - 1; auto rend_block = clause.rbegin (); @@ -442,7 +446,7 @@ void Internal::shrink_and_minimize_clause () { // for direct LRAT we remember how the clause used to look vector old_clause_lrat; - assert (minimize_chain.empty ()); + CADICAL_assert (minimize_chain.empty ()); if (lrat) for (auto &i : clause) old_clause_lrat.push_back (i); @@ -455,18 +459,18 @@ void Internal::shrink_and_minimize_clause () { LOG (clause, "post shrink pass (with uips, not removed) first UIP clause"); LOG (old_clause_lrat, "(used for lratdirect) before shrink: clause"); -#if defined(LOGGING) || !defined(NDEBUG) +#if defined(LOGGING) || !defined(CADICAL_NDEBUG) const unsigned old_size = clause.size (); #endif std::vector stack; { std::vector::size_type i = 1; for (std::vector::size_type j = 1; j < clause.size (); ++j) { - assert (i <= j); + CADICAL_assert (i <= j); clause[i] = clause[j]; if (lrat) { - assert (j < old_clause_lrat.size ()); - assert (mini_chain.empty ()); + CADICAL_assert (j < old_clause_lrat.size ()); + CADICAL_assert (mini_chain.empty ()); if (clause[j] != old_clause_lrat[j]) { calculate_minimize_chain (-old_clause_lrat[j], stack); for (auto p : mini_chain) { @@ -478,13 +482,13 @@ void Internal::shrink_and_minimize_clause () { if (clause[j] == uip0) { continue; } - assert (flags (clause[i]).keep); + CADICAL_assert (flags (clause[i]).keep); ++i; LOG ("keeping literal %i", clause[j]); } clause.resize (i); } - assert (old_size == + CADICAL_assert (old_size == (unsigned) clause.size () + total_shrunken + total_minimized); LOG (clause, "after shrinking first UIP clause"); LOG ("clause shrunken by %zd literals (including %u minimized)", @@ -505,3 +509,5 @@ void Internal::shrink_and_minimize_clause () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/signal.cpp b/src/sat/cadical/cadical_signal.cpp similarity index 90% rename from src/sat/cadical/signal.cpp rename to src/sat/cadical/cadical_signal.cpp index 384ba986f..099277f03 100644 --- a/src/sat/cadical/signal.cpp +++ b/src/sat/cadical/cadical_signal.cpp @@ -1,3 +1,5 @@ +#include "global.h" + #include "signal.hpp" #include "cadical.hpp" #include "resources.hpp" @@ -9,9 +11,13 @@ /*------------------------------------------------------------------------*/ +#ifndef WIN32 extern "C" { #include } +#endif + +ABC_NAMESPACE_IMPL_START /*------------------------------------------------------------------------*/ @@ -22,7 +28,7 @@ namespace CaDiCaL { static volatile bool caught_signal = false; static Handler *signal_handler; -#ifndef __WIN32 +#ifndef WIN32 static volatile bool caught_alarm = false; static volatile bool alarm_set = false; @@ -42,7 +48,7 @@ void Handler::catch_alarm () { catch_signal (SIGALRM); } SIGNALS #undef SIGNAL -#ifndef __WIN32 +#ifndef WIN32 static void (*SIGALRM_handler) (int); @@ -65,7 +71,7 @@ void Signal::reset () { SIG##_handler = 0; SIGNALS #undef SIGNAL -#ifndef __WIN32 +#ifndef WIN32 reset_alarm (); #endif caught_signal = false; @@ -77,7 +83,7 @@ const char *Signal::name (int sig) { return #SIG; SIGNALS #undef SIGNAL -#ifndef __WIN32 +#ifndef WIN32 if (sig == SIGALRM) return "SIGALRM"; #endif @@ -91,7 +97,7 @@ const char *Signal::name (int sig) { // exclusive access to. All these solutions are painful and not elegant. static void catch_signal (int sig) { -#ifndef __WIN32 +#ifndef WIN32 if (sig == SIGALRM && absolute_real_time () >= alarm_time) { if (!caught_alarm) { caught_alarm = true; @@ -119,12 +125,12 @@ void Signal::set (Handler *h) { #undef SIGNAL } -#ifndef __WIN32 +#ifndef WIN32 void Signal::alarm (int seconds) { - assert (seconds >= 0); - assert (!alarm_set); - assert (alarm_time < 0); + CADICAL_assert (seconds >= 0); + CADICAL_assert (!alarm_set); + CADICAL_assert (alarm_time < 0); SIGALRM_handler = signal (SIGALRM, catch_signal); alarm_set = true; alarm_time = absolute_real_time () + seconds; @@ -134,3 +140,5 @@ void Signal::alarm (int seconds) { #endif } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/solution.cpp b/src/sat/cadical/cadical_solution.cpp similarity index 89% rename from src/sat/cadical/solution.cpp rename to src/sat/cadical/cadical_solution.cpp index 8427d14e3..1d7514e42 100644 --- a/src/sat/cadical/solution.cpp +++ b/src/sat/cadical/cadical_solution.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // Sam Buss suggested to debug the case where a solver incorrectly claims @@ -11,7 +15,7 @@ namespace CaDiCaL { // a symbolic debugger immediately. void External::check_solution_on_learned_clause () { - assert (solution); + CADICAL_assert (solution); for (const auto &lit : internal->clause) if (sol (internal->externalize (lit)) == lit) return; @@ -24,7 +28,7 @@ void External::check_solution_on_learned_clause () { } void External::check_solution_on_shrunken_clause (Clause *c) { - assert (solution); + CADICAL_assert (solution); for (const auto &lit : *c) if (sol (internal->externalize (lit)) == lit) return; @@ -36,15 +40,17 @@ void External::check_solution_on_shrunken_clause (Clause *c) { } void External::check_no_solution_after_learning_empty_clause () { - assert (solution); + CADICAL_assert (solution); FATAL ("learned empty clause but got solution"); } void External::check_solution_on_learned_unit_clause (int unit) { - assert (solution); + CADICAL_assert (solution); if (sol (internal->externalize (unit)) == unit) return; FATAL ("learned unit %d contradicts solution", unit); } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/solver.cpp b/src/sat/cadical/cadical_solver.cpp similarity index 97% rename from src/sat/cadical/solver.cpp rename to src/sat/cadical/cadical_solver.cpp index f125cc32f..df051f4de 100644 --- a/src/sat/cadical/solver.cpp +++ b/src/sat/cadical/cadical_solver.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + /*------------------------------------------------------------------------*/ namespace CaDiCaL { @@ -29,7 +33,7 @@ namespace CaDiCaL { #define STATE(S) \ do { \ - assert (is_power_of_two (S)); \ + CADICAL_assert (is_power_of_two (S)); \ if (_state == S) \ break; \ _state = S; \ @@ -249,7 +253,7 @@ static void log_api_call_returns (Internal *internal, const char *name, /*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/ -#ifndef NTRACING +#ifndef CADICAL_NTRACING /*------------------------------------------------------------------------*/ #define TRACE(...) \ @@ -264,28 +268,28 @@ static void log_api_call_returns (Internal *internal, const char *name, } while (0) void Solver::trace_api_call (const char *s0) const { - assert (trace_api_file); + CADICAL_assert (trace_api_file); LOG ("TRACE %s", s0); fprintf (trace_api_file, "%s\n", s0); fflush (trace_api_file); } void Solver::trace_api_call (const char *s0, int i1) const { - assert (trace_api_file); + CADICAL_assert (trace_api_file); LOG ("TRACE %s %d", s0, i1); fprintf (trace_api_file, "%s %d\n", s0, i1); fflush (trace_api_file); } void Solver::trace_api_call (const char *s0, const char *s1) const { - assert (trace_api_file); + CADICAL_assert (trace_api_file); LOG ("TRACE %s %s", s0, s1); fprintf (trace_api_file, "%s %s\n", s0, s1); fflush (trace_api_file); } void Solver::trace_api_call (const char *s0, const char *s1, int i2) const { - assert (trace_api_file); + CADICAL_assert (trace_api_file); LOG ("TRACE %s %s %d", s0, s1, i2); fprintf (trace_api_file, "%s %s %d\n", s0, s1, i2); fflush (trace_api_file); @@ -306,7 +310,7 @@ void Solver::trace_api_call (const char *s0, const char *s1, int i2) const { static bool tracing_api_calls_through_environment_variable_method; /*------------------------------------------------------------------------*/ -#else // NTRACING +#else // CADICAL_NTRACING /*------------------------------------------------------------------------*/ #define TRACE(...) \ @@ -321,7 +325,7 @@ static bool tracing_nb_lidrup_env_var_method = false; Solver::Solver () { -#ifndef NTRACING +#ifndef CADICAL_NTRACING const char *path = getenv ("CADICAL_API_TRACE"); if (!path) path = getenv ("CADICALAPITRACE"); @@ -351,7 +355,7 @@ Solver::Solver () { external = new External (internal); DeferDeletePtr delete_external (external); STATE (CONFIGURING); -#ifndef NTRACING +#ifndef CADICAL_NTRACING if (tracing_api_calls_through_environment_variable_method) message ("tracing API calls to '%s'", path); #endif @@ -399,11 +403,11 @@ Solver::~Solver () { delete internal; delete external; -#ifndef NTRACING +#ifndef CADICAL_NTRACING if (close_trace_api_file) { close_trace_api_file = false; - assert (trace_api_file); - assert (tracing_api_calls_through_environment_variable_method); + CADICAL_assert (trace_api_file); + CADICAL_assert (tracing_api_calls_through_environment_variable_method); fclose (trace_api_file); tracing_api_calls_through_environment_variable_method = false; } @@ -454,7 +458,7 @@ int Solver::reserve_difference (int number_of_vars) { } /*------------------------------------------------------------------------*/ -#ifndef NTRACING +#ifndef CADICAL_NTRACING void Solver::trace_api_calls (FILE *file) { LOG_API_CALL_BEGIN ("trace_api_calls"); @@ -739,7 +743,7 @@ void Solver::implied (std::vector &entrailed) { int Solver::call_external_solve_and_check_results (bool preprocess_only) { transition_to_steady_state (); - assert (state () & READY); + CADICAL_assert (state () & READY); STATE (SOLVING); const int res = external->solve (preprocess_only); if (res == 10) @@ -812,8 +816,8 @@ int Solver::val (int lit) { external->conclude_sat (); int res = external->ival (lit); LOG_API_CALL_RETURNS ("val", lit, res); - assert (state () == SATISFIED); - assert (res == lit || res == -lit); + CADICAL_assert (state () == SATISFIED); + CADICAL_assert (res == lit || res == -lit); return res; } @@ -826,7 +830,7 @@ bool Solver::flip (int lit) { "can only flip when no external propagator is present"); bool res = external->flip (lit); LOG_API_CALL_RETURNS ("flip", lit, res); - assert (state () == SATISFIED); + CADICAL_assert (state () == SATISFIED); return res; } @@ -839,7 +843,7 @@ bool Solver::flippable (int lit) { "can only flip when no external propagator is present"); bool res = external->flippable (lit); LOG_API_CALL_RETURNS ("flippable", lit, res); - assert (state () == SATISFIED); + CADICAL_assert (state () == SATISFIED); return res; } @@ -851,7 +855,7 @@ bool Solver::failed (int lit) { "can only get failed assumptions in unsatisfied state"); bool res = external->failed (lit); LOG_API_CALL_RETURNS ("failed", lit, res); - assert (state () == UNSATISFIED); + CADICAL_assert (state () == UNSATISFIED); return res; } @@ -862,7 +866,7 @@ bool Solver::constraint_failed () { "can only determine if constraint failed in unsatisfied state"); bool res = external->failed_constraint (); LOG_API_CALL_RETURNS ("constraint_failed", res); - assert (state () == UNSATISFIED); + CADICAL_assert (state () == UNSATISFIED); return res; } @@ -1125,7 +1129,7 @@ bool Solver::trace_proof (FILE *external_file, const char *name) { "can only start proof tracing to '%s' right after initialization", name); File *internal_file = File::write (internal, external_file, name); - assert (internal_file); + CADICAL_assert (internal_file); internal->trace (internal_file); LOG_API_CALL_RETURNS ("trace_proof", name, true); return true; @@ -1252,7 +1256,7 @@ void Solver::conclude () { external->conclude_sat (); else if (state () == INCONCLUSIVE) external->conclude_unknown (); - assert (state () == UNSATISFIED || state () == SATISFIED || + CADICAL_assert (state () == UNSATISFIED || state () == SATISFIED || state () == INCONCLUSIVE); LOG_API_CALL_END ("conclude"); } @@ -1261,7 +1265,7 @@ void Solver::conclude () { void Solver::build (FILE *file, const char *prefix) { - assert (file == stdout || file == stderr); + CADICAL_assert (file == stdout || file == stderr); Terminal *terminal; @@ -1278,7 +1282,7 @@ void Solver::build (FILE *file, const char *prefix) { const char *b = date (); const char *f = flags (); - assert (v); + CADICAL_assert (v); fputs (prefix, file); if (terminal) @@ -1375,7 +1379,7 @@ const char *Solver::read_dimacs (FILE *external_file, const char *name, REQUIRE (state () == CONFIGURING, "can only read DIMACS file right after initialization"); File *file = File::read (internal, external_file, name); - assert (file); + CADICAL_assert (file); const char *err = read_dimacs (file, vars, strict); delete file; LOG_API_CALL_RETURNS ("read_dimacs", name, err); @@ -1405,7 +1409,7 @@ const char *Solver::read_dimacs (FILE *external_file, const char *name, REQUIRE (state () == CONFIGURING, "can only read DIMACS file right after initialization"); File *file = File::read (internal, external_file, name); - assert (file); + CADICAL_assert (file); const char *err = read_dimacs (file, vars, strict, &incremental, &cubes); delete file; LOG_API_CALL_RETURNS ("read_dimacs", name, err); @@ -1532,7 +1536,7 @@ public: ClauseCounter () : vars (0), clauses (0) {} bool clause (const vector &c) { for (const auto &lit : c) { - assert (lit != INT_MIN); + CADICAL_assert (lit != INT_MIN); int idx = abs (lit); if (idx > vars) vars = idx; @@ -1561,7 +1565,7 @@ public: const char *Solver::write_dimacs (const char *path, int min_max_var) { LOG_API_CALL_BEGIN ("write_dimacs", path, min_max_var); REQUIRE_VALID_STATE (); -#ifndef QUIET +#ifndef CADICAL_QUIET const double start = internal->time (); #endif internal->restore_clauses (); @@ -1588,7 +1592,7 @@ const char *Solver::write_dimacs (const char *path, int min_max_var) { } else res = internal->error_message.init ( "failed to open DIMACS file '%s' for writing", path); -#ifndef QUIET +#ifndef CADICAL_QUIET if (!res) { const double end = internal->time (); MSG ("wrote %" PRId64 " clauses in %.2f seconds %s time", @@ -1633,7 +1637,7 @@ const char *Solver::write_extension (const char *path) { LOG_API_CALL_BEGIN ("write_extension", path); REQUIRE_VALID_STATE (); const char *res = 0; -#ifndef QUIET +#ifndef CADICAL_QUIET const double start = internal->time (); #endif File *file = File::write (internal, path); @@ -1646,7 +1650,7 @@ const char *Solver::write_extension (const char *path) { } else res = internal->error_message.init ( "failed to open extension file '%s' for writing", path); -#ifndef QUIET +#ifndef CADICAL_QUIET if (!res) { const double end = internal->time (); MSG ("wrote %" PRId64 " witnesses in %.2f seconds %s time", @@ -1700,7 +1704,7 @@ void Solver::copy (Solver &other) const { void Solver::section (const char *title) { if (state () == DELETING) return; -#ifdef QUIET +#ifdef CADICAL_QUIET (void) title; #endif REQUIRE_INITIALIZED (); @@ -1710,7 +1714,7 @@ void Solver::section (const char *title) { void Solver::message (const char *fmt, ...) { if (state () == DELETING) return; -#ifdef QUIET +#ifdef CADICAL_QUIET (void) fmt; #else REQUIRE_INITIALIZED (); @@ -1725,7 +1729,7 @@ void Solver::message () { if (state () == DELETING) return; REQUIRE_INITIALIZED (); -#ifndef QUIET +#ifndef CADICAL_QUIET internal->message (); #endif } @@ -1734,7 +1738,7 @@ void Solver::verbose (int level, const char *fmt, ...) { if (state () == DELETING) return; REQUIRE_VALID_OR_SOLVING_STATE (); -#ifdef QUIET +#ifdef CADICAL_QUIET (void) level; (void) fmt; #else @@ -1756,3 +1760,5 @@ void Solver::error (const char *fmt, ...) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/stable.cpp b/src/sat/cadical/cadical_stable.cpp similarity index 67% rename from src/sat/cadical/stable.cpp rename to src/sat/cadical/cadical_stable.cpp index 44c9d1021..f177139ac 100644 --- a/src/sat/cadical/stable.cpp +++ b/src/sat/cadical/cadical_stable.cpp @@ -1,11 +1,15 @@ +#include "global.h" + #ifdef PROFILE_MODE #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { bool Internal::propagate_stable () { - assert (stable); + CADICAL_assert (stable); START (propstable); bool res = propagate (); STOP (propstable); @@ -13,19 +17,23 @@ bool Internal::propagate_stable () { } void Internal::analyze_stable () { - assert (stable); + CADICAL_assert (stable); START (analyzestable); analyze (); STOP (analyzestable); } int Internal::decide_stable () { - assert (stable); + CADICAL_assert (stable); return decide (); } }; // namespace CaDiCaL +ABC_NAMESPACE_IMPL_END + #else +ABC_NAMESPACE_IMPL_START int stable_if_not_profile_mode_dummy; +ABC_NAMESPACE_IMPL_END #endif diff --git a/src/sat/cadical/stats.cpp b/src/sat/cadical/cadical_stats.cpp similarity index 99% rename from src/sat/cadical/stats.cpp rename to src/sat/cadical/cadical_stats.cpp index e390e1d40..8b07745d6 100644 --- a/src/sat/cadical/stats.cpp +++ b/src/sat/cadical/cadical_stats.cpp @@ -1,7 +1,11 @@ // vim: set tw=300: set VIM text width to 300 characters for this file. +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -28,7 +32,7 @@ Stats::Stats () { void Stats::print (Internal *internal) { -#ifdef QUIET +#ifdef CADICAL_QUIET (void) internal; #else @@ -684,7 +688,7 @@ void Stats::print (Internal *internal) { if (all || stats.walk.count) { PRT ("walked: %15" PRId64 " %10.2f interval", stats.walk.count, relative (stats.conflicts, stats.walk.count)); -#ifndef QUIET +#ifndef CADICAL_QUIET if (internal->profiles.walk.value > 0) PRT (" flips: %15" PRId64 " %10.2f M per second", stats.walk.flips, @@ -746,11 +750,11 @@ void Stats::print (Internal *internal) { tout.magenta_code (), internal->opts.realtime ? "real" : "process", tout.normal_code ()); -#endif // ifndef QUIET +#endif // ifndef CADICAL_QUIET } void Internal::print_resource_usage () { -#ifndef QUIET +#ifndef CADICAL_QUIET SECTION ("resources"); uint64_t m = maximum_resident_set_size (); MSG ("total process time since initialization: %12.2f seconds", @@ -818,3 +822,5 @@ void LratChecker::print_stats () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/subsume.cpp b/src/sat/cadical/cadical_subsume.cpp similarity index 95% rename from src/sat/cadical/subsume.cpp rename to src/sat/cadical/cadical_subsume.cpp index a125a10f4..b75040ca8 100644 --- a/src/sat/cadical/subsume.cpp +++ b/src/sat/cadical/cadical_subsume.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -72,16 +76,16 @@ namespace CaDiCaL { // removed is returned. inline int Internal::subsume_check (Clause *subsuming, Clause *subsumed) { -#ifdef NDEBUG +#ifdef CADICAL_NDEBUG (void) subsumed; #endif - // Only use 'subsumed' for these following assertion checks. Otherwise we + // Only use 'subsumed' for these following CADICAL_assertion checks. Otherwise we // only require that 'subsumed' has all its literals marked. // - assert (!subsumed->garbage); - assert (!subsuming->garbage); - assert (subsuming != subsumed); - assert (subsuming->size <= subsumed->size); + CADICAL_assert (!subsumed->garbage); + CADICAL_assert (!subsuming->garbage); + CADICAL_assert (subsuming != subsumed); + CADICAL_assert (subsuming->size <= subsumed->size); stats.subchecks++; if (subsuming->size == 2) @@ -104,8 +108,8 @@ inline int Internal::subsume_check (Clause *subsuming, Clause *subsumed) { else flipped = lit; } - assert (prev); - assert (!subsuming->literals[0]); + CADICAL_assert (prev); + CADICAL_assert (!subsuming->literals[0]); subsuming->literals[0] = prev; if (failed) return 0; @@ -124,7 +128,7 @@ inline int Internal::subsume_check (Clause *subsuming, Clause *subsumed) { inline void Internal::subsume_clause (Clause *subsuming, Clause *subsumed) { stats.subsumed++; - assert (subsuming->size <= subsumed->size); + CADICAL_assert (subsuming->size <= subsumed->size); LOG (subsumed, "subsumed"); if (subsumed->redundant) stats.subred++; @@ -142,9 +146,9 @@ inline void Internal::subsume_clause (Clause *subsuming, Clause *subsumed) { stats.current.irredundant++; stats.added.irredundant++; stats.irrlits += subsuming->size; - assert (stats.current.redundant > 0); + CADICAL_assert (stats.current.redundant > 0); stats.current.redundant--; - assert (stats.added.redundant > 0); + CADICAL_assert (stats.added.redundant > 0); stats.added.redundant--; // ... and keep 'stats.added.total'. } @@ -157,7 +161,7 @@ void Internal::strengthen_clause (Clause *c, int lit) { if (opts.check && is_external_forgettable (c->id)) mark_garbage_external_forgettable (c->id); stats.strengthened++; - assert (c->size > 2); + CADICAL_assert (c->size > 2); LOG (c, "removing %d in", lit); if (proof) { LOG (lrat_chain, "strengthening clause with chain"); @@ -166,7 +170,7 @@ void Internal::strengthen_clause (Clause *c, int lit) { if (!c->redundant) mark_removed (lit); auto new_end = remove (c->begin (), c->end (), lit); - assert (new_end + 1 == c->end ()), (void) new_end; + CADICAL_assert (new_end + 1 == c->end ()), (void) new_end; (void) shrink_clause (c, c->size - 1); // bump_clause2 (c); LOG (c, "strengthened"); @@ -185,7 +189,7 @@ inline int Internal::try_to_subsume_clause (Clause *c, vector &shrunken) { stats.subtried++; - assert (!level); + CADICAL_assert (!level); LOG (c, "trying to subsume"); mark (c); // signed! @@ -262,7 +266,7 @@ inline int Internal::try_to_subsume_clause (Clause *c, // const Occs &os = occs (sign * lit); for (const auto &e : os) { - assert (!e->garbage); // sanity check + CADICAL_assert (!e->garbage); // sanity check if (e->garbage) continue; // defensive: not needed flipped = subsume_check (e, c); @@ -288,7 +292,7 @@ inline int Internal::try_to_subsume_clause (Clause *c, if (flipped) { LOG (d, "strengthening"); if (lrat) { - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); lrat_chain.push_back (c->id); lrat_chain.push_back (d->id); } @@ -296,7 +300,7 @@ inline int Internal::try_to_subsume_clause (Clause *c, c->used = d->used; strengthen_clause (c, -flipped); lrat_chain.clear (); - assert (likely_to_be_kept_clause (c)); + CADICAL_assert (likely_to_be_kept_clause (c)); shrunken.push_back (c); return -1; } @@ -369,7 +373,7 @@ bool Internal::subsume_round () { int old_marked_candidate_variables_for_elimination = stats.mark.elim; - assert (!level); + CADICAL_assert (!level); // Allocate schedule and occurrence lists. // @@ -432,7 +436,7 @@ bool Internal::subsume_round () { if (cs.clause->size > 2) cs.clause->subsume = true; -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t scheduled = schedule.size (); int64_t total = stats.current.irredundant + stats.current.redundant; PHASE ("subsume-round", stats.subsumerounds, @@ -461,7 +465,7 @@ bool Internal::subsume_round () { break; Clause *c = s.clause; - assert (!c->garbage); + CADICAL_assert (!c->garbage); checked++; @@ -621,7 +625,7 @@ void Internal::subsume () { stats.subsumephases++; if (external_prop) { - assert (!level); + CADICAL_assert (!level); private_steps = true; } @@ -638,9 +642,11 @@ void Internal::subsume () { transred (); if (external_prop) { - assert (!level); + CADICAL_assert (!level); private_steps = false; } } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/sweep.cpp b/src/sat/cadical/cadical_sweep.cpp similarity index 86% rename from src/sat/cadical/sweep.cpp rename to src/sat/cadical/cadical_sweep.cpp index 508315736..4dca2b4bc 100644 --- a/src/sat/cadical/sweep.cpp +++ b/src/sat/cadical/cadical_sweep.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { Sweeper::Sweeper (Internal *i) @@ -19,9 +23,9 @@ Sweeper::~Sweeper () { int Internal::sweep_solve () { START (sweepsolve); - kitten_randomize_phases (citten); + cadical_kitten_randomize_phases (citten); stats.sweep_solved++; - int res = kitten_solve (citten); + int res = cadical_kitten_solve (citten); if (res == 10) stats.sweep_sat++; if (res == 20) @@ -32,25 +36,25 @@ int Internal::sweep_solve () { bool Internal::sweep_flip (int lit) { START (sweepflip); - bool res = kitten_flip_signed_literal (citten, lit); + bool res = cadical_kitten_flip_signed_literal (citten, lit); STOP (sweepflip); return res; } int Internal::sweep_flip_and_implicant (int lit) { START (sweepimplicant); - int res = kitten_flip_and_implicant_for_signed_literal (citten, lit); + int res = cadical_kitten_flip_and_implicant_for_signed_literal (citten, lit); STOP (sweepimplicant); return res; } -void Internal::sweep_set_kitten_ticks_limit (Sweeper &sweeper) { +void Internal::sweep_set_cadical_kitten_ticks_limit (Sweeper &sweeper) { uint64_t remaining = 0; const uint64_t current = sweeper.current_ticks; if (current < sweeper.limit.ticks) remaining = sweeper.limit.ticks - current; - LOG ("'kitten_ticks' remaining %" PRIu64, remaining); - kitten_set_ticks_limit (citten, remaining); + LOG ("'cadical_kitten_ticks' remaining %" PRIu64, remaining); + cadical_kitten_set_ticks_limit (citten, remaining); } void Internal::sweep_update_noccs (Clause *c) { @@ -133,7 +137,7 @@ void Internal::sweep_dense_propagate (Sweeper &sweeper) { while (i < work.size ()) { int lit = work[i++]; LOG ("sweeping propagation of %d", lit); - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); ticks += 1 + cache_lines (occs (-lit).size (), sizeof (Clause *)); const Occs &ns = occs (-lit); for (const auto &c : ns) { @@ -194,11 +198,11 @@ void Internal::sweep_dense_propagate (Sweeper &sweeper) { work.clear (); } -bool Internal::kitten_ticks_limit_hit (Sweeper &sweeper, const char *when) { +bool Internal::cadical_kitten_ticks_limit_hit (Sweeper &sweeper, const char *when) { const uint64_t current = - kitten_current_ticks (citten) + sweeper.current_ticks; + cadical_kitten_current_ticks (citten) + sweeper.current_ticks; if (current >= sweeper.limit.ticks) { - LOG ("'kitten_ticks' limit of %" PRIu64 " ticks hit after %" PRIu64 + LOG ("'cadical_kitten_ticks' limit of %" PRIu64 " ticks hit after %" PRIu64 " ticks during %s", sweeper.limit.ticks, current, when); return true; @@ -225,8 +229,8 @@ void Internal::init_sweeper (Sweeper &sweeper) { .size (); // initialize with the cost of building full occ list. sweeper.current_ticks += 2 + 2 * cache_lines (clauses.size (), sizeof (Clause *)); - assert (!citten); - citten = kitten_init (); + CADICAL_assert (!citten); + citten = cadical_kitten_init (); citten_clear_track_log_terminate (); sweep_dense_mode_and_watch_irredundant (); // full occurence list @@ -284,7 +288,7 @@ void Internal::release_sweeper (Sweeper &sweeper) { for (unsigned i = 0; i < 2; i++) erase_vector (sweeper.core[i]); - kitten_release (citten); + cadical_kitten_release (citten); citten = 0; stats.ticks.sweep += sweeper.current_ticks; sweep_sparse_mode (); @@ -293,23 +297,23 @@ void Internal::release_sweeper (Sweeper &sweeper) { void Internal::clear_sweeper (Sweeper &sweeper) { LOG ("clearing sweeping environment"); - sweeper.current_ticks += kitten_current_ticks (citten); + sweeper.current_ticks += cadical_kitten_current_ticks (citten); citten_clear_track_log_terminate (); for (auto &idx : sweeper.vars) { - assert (sweeper.depths[idx]); + CADICAL_assert (sweeper.depths[idx]); sweeper.depths[idx] = 0; } sweeper.vars.clear (); for (auto c : sweeper.clauses) { - assert (c->swept); + CADICAL_assert (c->swept); c->swept = false; } sweeper.clauses.clear (); sweeper.backbone.clear (); sweeper.partition.clear (); sweeper.encoded = 0; - sweep_set_kitten_ticks_limit (sweeper); + sweep_set_cadical_kitten_ticks_limit (sweeper); } int Internal::sweep_repr (Sweeper &sweeper, int lit) { @@ -331,7 +335,7 @@ int Internal::sweep_repr (Sweeper &sweeper, int lit) { sweeper.reprs[prev] = res; prev = next; } - assert (sweeper.reprs[-prev] == not_res); + CADICAL_assert (sweeper.reprs[-prev] == not_res); } return res; } @@ -344,16 +348,16 @@ void Internal::add_literal_to_environment (Sweeper &sweeper, unsigned depth, const int idx = abs (lit); if (sweeper.depths[idx]) return; - assert (depth < UINT_MAX); + CADICAL_assert (depth < UINT_MAX); sweeper.depths[idx] = depth + 1; - assert (idx); + CADICAL_assert (idx); sweeper.vars.push_back (idx); LOG ("sweeping[%u] adding literal %d", depth, lit); } void Internal::sweep_add_clause (Sweeper &sweeper, unsigned depth) { - // TODO: assertion fails, check if this an issue or can be avoided - // assert (sweeper.clause.size () > 1); + // TODO: CADICAL_assertion fails, check if this an issue or can be avoided + // CADICAL_assert (sweeper.clause.size () > 1); for (const auto &lit : sweeper.clause) add_literal_to_environment (sweeper, depth, lit); citten_clause_with_id (citten, sweeper.clauses.size (), @@ -366,9 +370,9 @@ void Internal::sweep_add_clause (Sweeper &sweeper, unsigned depth) { void Internal::sweep_clause (Sweeper &sweeper, unsigned depth, Clause *c) { if (c->swept) return; - assert (can_sweep_clause (c)); + CADICAL_assert (can_sweep_clause (c)); LOG (c, "sweeping[%u]", depth); - assert (sweeper.clause.empty ()); + CADICAL_assert (sweeper.clause.empty ()); for (const auto &lit : *c) { const signed char tmp = val (lit); if (tmp > 0) { @@ -403,7 +407,7 @@ static void save_core_clause (void *state, unsigned id, bool learned, pc.cad_id = INVALID64; // delay giving ids } else { pc.sweep_id = id; // necessary - assert (id < sweeper->clauses.size ()); + CADICAL_assert (id < sweeper->clauses.size ()); pc.cad_id = sweeper->clauses[id]->id; } pc.kit_id = 0; @@ -436,17 +440,17 @@ static void save_core_clause_with_lrat (void *state, unsigned cid, pc.sweep_id = INVALID; pc.cad_id = INVALID64; if (!learned) { - assert (size); - assert (!chain_size); - assert (id < clauses.size ()); + CADICAL_assert (size); + CADICAL_assert (!chain_size); + CADICAL_assert (id < clauses.size ()); pc.sweep_id = id; - assert (id < clauses.size ()); + CADICAL_assert (id < clauses.size ()); pc.cad_id = clauses[id]->id; for (const auto &lit : *clauses[id]) { pc.literals.push_back (lit); } } else { - assert (chain_size); + CADICAL_assert (chain_size); pc.sweep_id = INVALID; pc.cad_id = INVALID64; // delay giving ids const unsigned *end = lits + size; @@ -462,7 +466,7 @@ static void save_core_clause_with_lrat (void *state, unsigned cid, LOG (pc.literals, "traced %s", pc.learned == true ? "learned" : "original"); else { - assert (id < clauses.size ()); + CADICAL_assert (id < clauses.size ()); LOG (clauses[id], "traced"); } #endif @@ -476,14 +480,14 @@ static int citten_terminate (void *data) { } // end extern C void Internal::citten_clear_track_log_terminate () { - assert (citten); - kitten_clear (citten); - kitten_track_antecedents (citten); + CADICAL_assert (citten); + cadical_kitten_clear (citten); + cadical_kitten_track_antecedents (citten); if (external->terminator) - kitten_set_terminator (citten, internal, citten_terminate); + cadical_kitten_set_terminator (citten, internal, citten_terminate); #ifdef LOGGING if (opts.log) - kitten_set_logging (citten); + cadical_kitten_set_logging (citten); #endif } @@ -491,10 +495,10 @@ void Internal::add_core (Sweeper &sweeper, unsigned core_idx) { if (unsat) return; LOG ("check and add extracted core[%u] lemmas to proof", core_idx); - assert (core_idx == 0 || core_idx == 1); + CADICAL_assert (core_idx == 0 || core_idx == 1); vector &core = sweeper.core[core_idx]; - assert (!lrat || proof); + CADICAL_assert (!lrat || proof); unsigned unsat_size = 0; for (auto &pc : core) { @@ -502,18 +506,18 @@ void Internal::add_core (Sweeper &sweeper, unsigned core_idx) { if (!pc.learned) { LOG (pc.literals, - "not adding already present core[%u] kitten[%u] clause", + "not adding already present core[%u] cadical_kitten[%u] clause", core_idx, pc.kit_id); continue; } - LOG (pc.literals, "adding extracted core[%u] kitten[%u] lemma", + LOG (pc.literals, "adding extracted core[%u] cadical_kitten[%u] lemma", core_idx, pc.kit_id); const unsigned new_size = pc.literals.size (); if (lrat) { - assert (pc.cad_id == INVALID64); + CADICAL_assert (pc.cad_id == INVALID64); for (auto &cid : pc.chain) { int64_t id = 0; for (const auto &cpc : core) { @@ -522,8 +526,8 @@ void Internal::add_core (Sweeper &sweeper, unsigned core_idx) { id = cpc.cad_id; else { id = cpc.cad_id; - assert (cpc.cad_id == sweeper.clauses[cpc.sweep_id]->id); - assert (!sweeper.clauses[cpc.sweep_id]->garbage); + CADICAL_assert (cpc.cad_id == sweeper.clauses[cpc.sweep_id]->id); + CADICAL_assert (!sweeper.clauses[cpc.sweep_id]->garbage); // avoid duplicate ids of units with seen flags for (const auto &lit : cpc.literals) { if (val (lit) >= 0) @@ -542,7 +546,7 @@ void Internal::add_core (Sweeper &sweeper, unsigned core_idx) { break; } } - assert (id); + CADICAL_assert (id); if (id != INVALID64) lrat_chain.push_back (id); } @@ -581,8 +585,8 @@ void Internal::add_core (Sweeper &sweeper, unsigned core_idx) { continue; } - assert (new_size > 1); - assert (pc.learned); + CADICAL_assert (new_size > 1); + CADICAL_assert (pc.learned); if (proof) { pc.cad_id = ++clause_id; @@ -594,19 +598,19 @@ void Internal::add_core (Sweeper &sweeper, unsigned core_idx) { void Internal::save_core (Sweeper &sweeper, unsigned core) { LOG ("saving extracted core[%u] lemmas", core); - assert (core == 0 || core == 1); - assert (sweeper.core[core].empty ()); + CADICAL_assert (core == 0 || core == 1); + CADICAL_assert (sweeper.core[core].empty ()); sweeper.save = core; - kitten_compute_clausal_core (citten, 0); + cadical_kitten_compute_clausal_core (citten, 0); if (lrat) - kitten_trace_core (citten, &sweeper, save_core_clause_with_lrat); + cadical_kitten_trace_core (citten, &sweeper, save_core_clause_with_lrat); else - kitten_traverse_core_clauses_with_id (citten, &sweeper, + cadical_kitten_traverse_core_clauses_with_id (citten, &sweeper, save_core_clause); } void Internal::clear_core (Sweeper &sweeper, unsigned core_idx) { - assert (core_idx == 0 || core_idx == 1); + CADICAL_assert (core_idx == 0 || core_idx == 1); LOG ("clearing core[%u] lemmas", core_idx); vector &core = sweeper.core[core_idx]; if (proof) { @@ -632,10 +636,10 @@ void Internal::init_backbone_and_partition (Sweeper &sweeper) { for (const auto &idx : sweeper.vars) { if (!active (idx)) continue; - assert (idx > 0); + CADICAL_assert (idx > 0); const int lit = idx; const int not_lit = -lit; - const signed char tmp = kitten_signed_value (citten, lit); + const signed char tmp = cadical_kitten_signed_value (citten, lit); const int candidate = (tmp < 0) ? not_lit : lit; LOG ("sweeping candidate %d", candidate); sweeper.backbone.push_back (candidate); @@ -648,9 +652,9 @@ void Internal::init_backbone_and_partition (Sweeper &sweeper) { } void Internal::sweep_empty_clause (Sweeper &sweeper) { - assert (!unsat); + CADICAL_assert (!unsat); save_add_clear_core (sweeper); - assert (unsat); + CADICAL_assert (unsat); } void Internal::sweep_refine_partition (Sweeper &sweeper) { @@ -671,7 +675,7 @@ void Internal::sweep_refine_partition (Sweeper &sweeper) { continue; if (val (other)) continue; - signed char value = kitten_signed_value (citten, other); + signed char value = cadical_kitten_signed_value (citten, other); if (!value) LOG ("dropping sub-solver unassigned %d", other); else if (value > 0) { @@ -708,7 +712,7 @@ void Internal::sweep_refine_partition (Sweeper &sweeper) { continue; if (val (other)) continue; - signed char value = kitten_signed_value (citten, other); + signed char value = cadical_kitten_signed_value (citten, other); if (value < 0) { new_partition.push_back (other); assigned_false++; @@ -746,7 +750,7 @@ void Internal::sweep_refine_backbone (Sweeper &sweeper) { const int lit = *p; if (val (lit)) continue; - signed char value = kitten_signed_value (citten, lit); + signed char value = cadical_kitten_signed_value (citten, lit); if (!value) LOG ("dropping sub-solver unassigned %d", lit); else if (value > 0) @@ -756,7 +760,7 @@ void Internal::sweep_refine_backbone (Sweeper &sweeper) { } void Internal::sweep_refine (Sweeper &sweeper) { - assert (kitten_status (citten) == 10); + CADICAL_assert (cadical_kitten_status (citten) == 10); if (sweeper.backbone.empty ()) LOG ("no need to refine empty backbone candidates"); else @@ -771,8 +775,8 @@ void Internal::flip_backbone_literals (Sweeper &sweeper) { const unsigned max_rounds = opts.sweepfliprounds; if (!max_rounds) return; - assert (sweeper.backbone.size ()); - if (kitten_status (citten) != 10) + CADICAL_assert (sweeper.backbone.size ()); + if (cadical_kitten_status (citten) != 10) return; #ifdef LOGGING unsigned total_flipped = 0; @@ -811,7 +815,7 @@ void Internal::flip_backbone_literals (Sweeper &sweeper) { break; if (terminated_asynchronously ()) break; - if (kitten_ticks_limit_hit (sweeper, "backbone flipping")) + if (cadical_kitten_ticks_limit_hit (sweeper, "backbone flipping")) break; if (refine) sweep_refine (sweeper); @@ -823,13 +827,13 @@ void Internal::flip_backbone_literals (Sweeper &sweeper) { bool Internal::sweep_extract_fixed (Sweeper &sweeper, int lit) { const int not_lit = -lit; stats.sweep_solved_backbone++; - kitten_assume_signed (citten, not_lit); + cadical_kitten_assume_signed (citten, not_lit); int res = sweep_solve (); if (!res) { stats.sweep_unknown_backbone++; return false; } - assert (res == 20); + CADICAL_assert (res == 20); LOG ("sweep unit %d", lit); save_add_clear_core (sweeper); stats.sweep_unsat_backbone++; @@ -838,10 +842,10 @@ bool Internal::sweep_extract_fixed (Sweeper &sweeper, int lit) { bool Internal::sweep_backbone_candidate (Sweeper &sweeper, int lit) { LOG ("trying backbone candidate %d", lit); - signed char value = kitten_fixed_signed (citten, lit); + signed char value = cadical_kitten_fixed_signed (citten, lit); if (value) { stats.sweep_fixed_backbone++; - assert (value > 0); + CADICAL_assert (value > 0); if (val (lit) <= 0) { return sweep_extract_fixed (sweeper, lit); } else @@ -849,7 +853,7 @@ bool Internal::sweep_backbone_candidate (Sweeper &sweeper, int lit) { return false; } - int res = kitten_status (citten); + int res = cadical_kitten_status (citten); if (res != 10) { LOG ("not flipping due to status %d != 10", res); } @@ -864,7 +868,7 @@ bool Internal::sweep_backbone_candidate (Sweeper &sweeper, int lit) { LOG ("flipping %d failed", lit); const int not_lit = -lit; stats.sweep_solved_backbone++; - kitten_assume_signed (citten, not_lit); + cadical_kitten_assume_signed (citten, not_lit); res = sweep_solve (); if (res == 10) { LOG ("sweeping backbone candidate %d failed", lit); @@ -876,7 +880,7 @@ bool Internal::sweep_backbone_candidate (Sweeper &sweeper, int lit) { if (res == 20) { LOG ("sweep unit %d", lit); save_add_clear_core (sweeper); - assert (val (lit)); + CADICAL_assert (val (lit)); stats.sweep_unsat_backbone++; return true; } @@ -894,11 +898,11 @@ bool Internal::sweep_backbone_candidate (Sweeper &sweeper, int lit) { // int64_t Internal::add_sweep_binary (sweep_proof_clause pc, int lit, int other) { - assert (!unsat); + CADICAL_assert (!unsat); if (unsat) return 0; // sanity check, should be fuzzed - assert (!val (lit) && !val (other)); + CADICAL_assert (!val (lit) && !val (other)); if (val (lit) || val (other)) return 0; @@ -940,28 +944,28 @@ bool Internal::scheduled_variable (Sweeper &sweeper, int idx) { } void Internal::schedule_inner (Sweeper &sweeper, int idx) { - assert (idx); + CADICAL_assert (idx); if (!active (idx)) return; const int next = sweeper.next[idx]; if (next != 0) { LOG ("rescheduling inner %d as last", idx); const unsigned prev = sweeper.prev[idx]; - assert (sweeper.prev[next] == idx); + CADICAL_assert (sweeper.prev[next] == idx); sweeper.prev[next] = prev; if (prev == 0) { - assert (sweeper.first == idx); + CADICAL_assert (sweeper.first == idx); sweeper.first = next; } else { - assert (sweeper.next[prev] == idx); + CADICAL_assert (sweeper.next[prev] == idx); sweeper.next[prev] = next; } const unsigned last = sweeper.last; if (last == 0) { - assert (sweeper.first == 0); + CADICAL_assert (sweeper.first == 0); sweeper.first = idx; } else { - assert (sweeper.next[last] == 0); + CADICAL_assert (sweeper.next[last] == 0); sweeper.next[last] = idx; } sweeper.prev[idx] = last; @@ -971,13 +975,13 @@ void Internal::schedule_inner (Sweeper &sweeper, int idx) { LOG ("scheduling inner %d as last", idx); const unsigned last = sweeper.last; if (last == 0) { - assert (sweeper.first == 0); + CADICAL_assert (sweeper.first == 0); sweeper.first = idx; } else { - assert (sweeper.next[last] == 0); + CADICAL_assert (sweeper.next[last] == 0); sweeper.next[last] = idx; } - assert (sweeper.next[idx] == 0); + CADICAL_assert (sweeper.next[idx] == 0); sweeper.prev[idx] = last; sweeper.last = idx; } else @@ -985,17 +989,17 @@ void Internal::schedule_inner (Sweeper &sweeper, int idx) { } void Internal::schedule_outer (Sweeper &sweeper, int idx) { - assert (!scheduled_variable (sweeper, idx)); - assert (active (idx)); + CADICAL_assert (!scheduled_variable (sweeper, idx)); + CADICAL_assert (active (idx)); const int first = sweeper.first; if (first == 0) { - assert (sweeper.last == 0); + CADICAL_assert (sweeper.last == 0); sweeper.last = idx; } else { - assert (sweeper.prev[first] == 0); + CADICAL_assert (sweeper.prev[first] == 0); sweeper.prev[first] = idx; } - assert (sweeper.prev[idx] == 0); + CADICAL_assert (sweeper.prev[idx] == 0); sweeper.next[idx] = first; sweeper.first = idx; LOG ("scheduling outer %d as first", idx); @@ -1007,16 +1011,16 @@ int Internal::next_scheduled (Sweeper &sweeper) { LOG ("no more scheduled variables left"); return 0; } - assert (res > 0); + CADICAL_assert (res > 0); LOG ("dequeuing next scheduled %d", res); const unsigned prev = sweeper.prev[res]; - assert (sweeper.next[res] == 0); + CADICAL_assert (sweeper.next[res] == 0); sweeper.prev[res] = 0; if (prev == 0) { - assert (sweeper.first == res); + CADICAL_assert (sweeper.first == res); sweeper.first = 0; } else { - assert (sweeper.next[prev] == res); + CADICAL_assert (sweeper.next[prev] == res); sweeper.next[prev] = 0; } sweeper.last = prev; @@ -1027,7 +1031,7 @@ void Internal::sweep_substitute_lrat (Clause *c, int64_t id) { if (!lrat) return; for (const auto &lit : *c) { - assert (val (lit) <= 0); + CADICAL_assert (val (lit) <= 0); if (val (lit) < 0) { int64_t id = unit_id (-lit); lrat_chain.push_back (id); @@ -1054,11 +1058,11 @@ void Internal::substitute_connected_clauses (Sweeper &sweeper, int lit, return; LOG ("substituting %d with %d in all irredundant clauses", lit, repr); - assert (lit != repr); - assert (lit != -repr); + CADICAL_assert (lit != repr); + CADICAL_assert (lit != -repr); - assert (active (lit)); - assert (active (repr)); + CADICAL_assert (active (lit)); + CADICAL_assert (active (repr)); uint64_t &ticks = sweeper.current_ticks; @@ -1074,25 +1078,25 @@ void Internal::substitute_connected_clauses (Sweeper &sweeper, int lit, ticks++; if (c->garbage) continue; - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); bool satisfied = false; bool repr_already_watched = false; const int not_repr = -repr; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG bool found = false; #endif for (const auto &other : *c) { if (other == lit) { -#ifndef NDEBUG - assert (!found); +#ifndef CADICAL_NDEBUG + CADICAL_assert (!found); found = true; #endif clause.push_back (repr); continue; } - assert (other != -lit); + CADICAL_assert (other != -lit); if (other == repr) { - assert (!repr_already_watched); + CADICAL_assert (!repr_already_watched); repr_already_watched = true; continue; } @@ -1115,12 +1119,12 @@ void Internal::substitute_connected_clauses (Sweeper &sweeper, int lit, sweep_update_noccs (c); continue; } - assert (found); + CADICAL_assert (found); const unsigned new_size = clause.size (); sweep_substitute_lrat (c, id); if (new_size == 0) { LOG (c, "substituted empty clause"); - assert (!unsat); + CADICAL_assert (!unsat); learn_empty_clause (); break; } @@ -1136,7 +1140,7 @@ void Internal::substitute_connected_clauses (Sweeper &sweeper, int lit, stats.sweep_units++; break; } - assert (c->size >= 2); + CADICAL_assert (c->size >= 2); if (!c->redundant) mark_removed (c); uint64_t new_id = ++clause_id; @@ -1179,7 +1183,7 @@ void Internal::sweep_substitute_new_equivalences (Sweeper &sweeper) { return; unsigned count = 0; - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); for (const auto &sb : sweeper.binaries) { count++; @@ -1190,7 +1194,7 @@ void Internal::sweep_substitute_new_equivalences (Sweeper &sweeper) { } else { substitute_connected_clauses (sweeper, -lit, other, sb.id); } - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); if (val (lit) < 0) { if (lrat) { const int64_t lid = unit_id (-lit); @@ -1218,7 +1222,7 @@ void Internal::sweep_substitute_new_equivalences (Sweeper &sweeper) { } assign_unit (lit); } else - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); } lrat_chain.clear (); delete_sweep_binary (sb); @@ -1230,19 +1234,19 @@ void Internal::sweep_substitute_new_equivalences (Sweeper &sweeper) { } count = 0; } - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); } sweeper.binaries.clear (); } void Internal::sweep_remove (Sweeper &sweeper, int lit) { - assert (sweeper.reprs[lit] != lit); + CADICAL_assert (sweeper.reprs[lit] != lit); vector &partition = sweeper.partition; const auto begin_partition = partition.begin (); auto p = begin_partition; const auto end_partition = partition.end (); for (; *p != lit; p++) - assert (p + 1 != end_partition); + CADICAL_assert (p + 1 != end_partition); auto begin_class = p; while (begin_class != begin_partition && begin_class[-1] != 0) begin_class--; @@ -1252,7 +1256,7 @@ void Internal::sweep_remove (Sweeper &sweeper, int lit) { const unsigned size = end_class - begin_class; LOG ("removing non-representative %d from equivalence class of size %u", lit, size); - assert (size > 1); + CADICAL_assert (size > 1); auto q = begin_class; if (size == 2) { LOG ("completely squashing equivalence class of %d", lit); @@ -1270,8 +1274,8 @@ void Internal::flip_partition_literals (Sweeper &sweeper) { const unsigned max_rounds = opts.sweepfliprounds; if (!max_rounds) return; - assert (sweeper.partition.size ()); - if (kitten_status (citten) != 10) + CADICAL_assert (sweeper.partition.size ()); + if (cadical_kitten_status (citten) != 10) return; #ifdef LOGGING unsigned total_flipped = 0; @@ -1286,17 +1290,17 @@ void Internal::flip_partition_literals (Sweeper &sweeper) { const auto end = sweeper.partition.end (); while (src != end) { auto end_src = src; - while (assert (end_src != end), *end_src != 0) + while (CADICAL_assert (end_src != end), *end_src != 0) end_src++; unsigned size = end_src - src; - assert (size > 1); + CADICAL_assert (size > 1); auto q = dst; for (auto p = src; p != end_src; p++) { const int lit = *p; if (limit_hit) { *q++ = lit; continue; - } else if (kitten_ticks_limit_hit (sweeper, "partition flipping")) { + } else if (cadical_kitten_ticks_limit_hit (sweeper, "partition flipping")) { *q++ = lit; limit_hit = true; continue; @@ -1325,7 +1329,7 @@ void Internal::flip_partition_literals (Sweeper &sweeper) { LOG ("flipped %u equivalence candidates in round %u", flipped, round); if (terminated_asynchronously ()) break; - if (kitten_ticks_limit_hit (sweeper, "partition flipping")) + if (cadical_kitten_ticks_limit_hit (sweeper, "partition flipping")) break; if (refine) sweep_refine (sweeper); @@ -1339,11 +1343,11 @@ bool Internal::sweep_equivalence_candidates (Sweeper &sweeper, int lit, LOG ("trying equivalence candidates %d = %d", lit, other); const auto begin = sweeper.partition.begin (); auto const end = sweeper.partition.end (); - assert (begin + 3 <= end); - assert (end[-3] == lit); - assert (end[-2] == other); + CADICAL_assert (begin + 3 <= end); + CADICAL_assert (end[-3] == lit); + CADICAL_assert (end[-2] == other); const int third = (end - begin == 3) ? 0 : end[-4]; - int res = kitten_status (citten); + int res = cadical_kitten_status (citten); if (res == 10) { stats.sweep_flip_equivalences++; if (sweep_flip (lit)) { @@ -1406,8 +1410,8 @@ bool Internal::sweep_equivalence_candidates (Sweeper &sweeper, int lit, const int not_other = -other; const int not_lit = -lit; LOG ("flipping %d and %d both failed", lit, other); - kitten_assume_signed (citten, not_lit); - kitten_assume_signed (citten, other); + cadical_kitten_assume_signed (citten, not_lit); + cadical_kitten_assume_signed (citten, other); stats.sweep_solved_equivalences++; res = sweep_solve (); if (res == 10) { @@ -1427,8 +1431,8 @@ bool Internal::sweep_equivalence_candidates (Sweeper &sweeper, int lit, save_core (sweeper, 0); - kitten_assume_signed (citten, lit); - kitten_assume_signed (citten, not_other); + cadical_kitten_assume_signed (citten, lit); + cadical_kitten_assume_signed (citten, not_other); res = sweep_solve (); stats.sweep_solved_equivalences++; if (res == 10) { @@ -1446,7 +1450,7 @@ bool Internal::sweep_equivalence_candidates (Sweeper &sweeper, int lit, return false; } - assert (res == 20); + CADICAL_assert (res == 20); stats.sweep_unsat_equivalences++; LOG ("second sweeping implication %d <- %d succeeded too", other, lit); @@ -1455,15 +1459,15 @@ bool Internal::sweep_equivalence_candidates (Sweeper &sweeper, int lit, LOG ("sweep equivalence %d = %d", lit, other); - // If kitten behaves as expected, the two binaries of the equivalence + // If cadical_kitten behaves as expected, the two binaries of the equivalence // should be stored at sweeper.core[i].back () for i in {0, 1}. // We pick the smaller absolute valued literal as representative and // store the equivalence . add_core (sweeper, 0); add_core (sweeper, 1); if (!val (lit) && !val (other)) { - assert (sweeper.core[0].size ()); - assert (sweeper.core[1].size ()); + CADICAL_assert (sweeper.core[0].size ()); + CADICAL_assert (sweeper.core[1].size ()); stats.sweep_equivalences++; sweep_binary bin1; sweep_binary bin2; @@ -1508,22 +1512,22 @@ bool Internal::sweep_equivalence_candidates (Sweeper &sweeper, int lit, } const char *Internal::sweep_variable (Sweeper &sweeper, int idx) { - assert (!unsat); + CADICAL_assert (!unsat); if (!active (idx)) return "inactive variable"; const int start = idx; if (sweeper.reprs[start] != start) return "non-representative variable"; - assert (sweeper.vars.empty ()); - assert (sweeper.clauses.empty ()); - assert (sweeper.backbone.empty ()); - assert (sweeper.partition.empty ()); - assert (!sweeper.encoded); + CADICAL_assert (sweeper.vars.empty ()); + CADICAL_assert (sweeper.clauses.empty ()); + CADICAL_assert (sweeper.backbone.empty ()); + CADICAL_assert (sweeper.partition.empty ()); + CADICAL_assert (!sweeper.encoded); stats.sweep_variables++; LOG ("sweeping %d", idx); - assert (!val (start)); + CADICAL_assert (!val (start)); LOG ("starting sweeping[0]"); add_literal_to_environment (sweeper, 0, start); LOG ("finished sweeping[0]"); @@ -1559,7 +1563,7 @@ const char *Internal::sweep_variable (Sweeper &sweeper, int idx) { if (opts.sweeprand && choices > 1) { const unsigned swaps = sweeper.random.pick_int (0, choices - 1); if (swaps) { - assert (expand + swaps < sweeper.vars.size ()); + CADICAL_assert (expand + swaps < sweeper.vars.size ()); swap (sweeper.vars[expand], sweeper.vars[expand + swaps]); } } @@ -1602,14 +1606,14 @@ const char *Internal::sweep_variable (Sweeper &sweeper, int idx) { LOG ("sub-solver returns '%d'", res); if (res == 10) { init_backbone_and_partition (sweeper); -#ifndef QUIET +#ifndef CADICAL_QUIET uint64_t units = stats.sweep_units; uint64_t solved = stats.sweep_solved; #endif START (sweepbackbone); while (sweeper.backbone.size ()) { if (unsat || terminated_asynchronously () || - kitten_ticks_limit_hit (sweeper, "backbone refinement")) { + cadical_kitten_ticks_limit_hit (sweeper, "backbone refinement")) { limit_reached = true; STOP_SWEEP_BACKBONE: STOP (sweepbackbone); @@ -1617,7 +1621,7 @@ const char *Internal::sweep_variable (Sweeper &sweeper, int idx) { } flip_backbone_literals (sweeper); if (terminated_asynchronously () || - kitten_ticks_limit_hit (sweeper, "backbone refinement")) { + cadical_kitten_ticks_limit_hit (sweeper, "backbone refinement")) { limit_reached = true; goto STOP_SWEEP_BACKBONE; } @@ -1631,7 +1635,7 @@ const char *Internal::sweep_variable (Sweeper &sweeper, int idx) { success = true; } STOP (sweepbackbone); -#ifndef QUIET +#ifndef CADICAL_QUIET units = stats.sweep_units - units; solved = stats.sweep_solved - solved; #endif @@ -1639,15 +1643,15 @@ const char *Internal::sweep_variable (Sweeper &sweeper, int idx) { "complete swept variable %d backbone with %" PRIu64 " units in %" PRIu64 " solver calls", externalize (idx), units, solved); - assert (sweeper.backbone.empty ()); -#ifndef QUIET + CADICAL_assert (sweeper.backbone.empty ()); +#ifndef CADICAL_QUIET uint64_t equivalences = stats.sweep_equivalences; solved = stats.sweep_solved; #endif START (sweepequivalences); while (sweeper.partition.size ()) { if (unsat || terminated_asynchronously () || - kitten_ticks_limit_hit (sweeper, "partition refinement")) { + cadical_kitten_ticks_limit_hit (sweeper, "partition refinement")) { limit_reached = true; STOP_SWEEP_EQUIVALENCES: STOP (sweepequivalences); @@ -1655,7 +1659,7 @@ const char *Internal::sweep_variable (Sweeper &sweeper, int idx) { } flip_partition_literals (sweeper); if (terminated_asynchronously () || - kitten_ticks_limit_hit (sweeper, "backbone refinement")) { + cadical_kitten_ticks_limit_hit (sweeper, "backbone refinement")) { limit_reached = true; goto STOP_SWEEP_EQUIVALENCES; } @@ -1663,7 +1667,7 @@ const char *Internal::sweep_variable (Sweeper &sweeper, int idx) { break; if (sweeper.partition.size () > 2) { const auto end = sweeper.partition.end (); - assert (end[-1] == 0); + CADICAL_assert (end[-1] == 0); int lit = end[-3]; int other = end[-2]; if (sweep_equivalence_candidates (sweeper, lit, other)) @@ -1672,7 +1676,7 @@ const char *Internal::sweep_variable (Sweeper &sweeper, int idx) { sweeper.partition.clear (); } STOP (sweepequivalences); -#ifndef QUIET +#ifndef CADICAL_QUIET equivalences = stats.sweep_equivalences - equivalences; solved = stats.sweep_solved - solved; if (equivalences) @@ -1698,7 +1702,7 @@ DONE: return "unsuccessfully without reaching limit"; else if (success && !limit_reached) return "successfully without reaching limit"; - assert (!success && limit_reached); + CADICAL_assert (!success && limit_reached); return "unsuccessfully and reached limit"; } @@ -1709,8 +1713,8 @@ struct sweep_candidate { struct rank_sweep_candidate { bool operator() (sweep_candidate a, sweep_candidate b) const { - assert (a.rank && b.rank); - assert (a.idx > 0 && b.idx > 0); + CADICAL_assert (a.rank && b.rank); + CADICAL_assert (a.idx > 0 && b.idx > 0); if (a.rank < b.rank) return true; if (b.rank < a.rank) @@ -1759,7 +1763,7 @@ unsigned Internal::schedule_all_other_not_scheduled_yet (Sweeper &sweeper) { fresh.push_back (cand); } const size_t size = fresh.size (); - assert (size <= UINT_MAX); + CADICAL_assert (size <= UINT_MAX); sort (fresh.begin (), fresh.end (), rank_sweep_candidate ()); for (auto &cand : fresh) schedule_outer (sweeper, cand.idx); @@ -1807,7 +1811,7 @@ void Internal::mark_incomplete (Sweeper &sweeper) { } } sweep_incomplete = true; -#ifndef QUIET +#ifndef CADICAL_QUIET VERBOSE (2, "marked %u scheduled sweeping variables as incomplete", marked); #else @@ -1820,7 +1824,7 @@ unsigned Internal::schedule_sweeping (Sweeper &sweeper) { const unsigned fresh = schedule_all_other_not_scheduled_yet (sweeper); const unsigned scheduled = fresh + rescheduled; const unsigned incomplete = incomplete_variables (); -#ifndef QUIET +#ifndef CADICAL_QUIET PHASE ("sweep", stats.sweep, "scheduled %u variables %.0f%% " "(%u rescheduled %.0f%%, %u incomplete %.0f%%)", @@ -1829,7 +1833,7 @@ unsigned Internal::schedule_sweeping (Sweeper &sweeper) { percent (incomplete, scheduled)); #endif if (incomplete) - assert (sweep_incomplete); + CADICAL_assert (sweep_incomplete); else { if (sweep_incomplete) stats.sweep_completed++; @@ -1840,17 +1844,17 @@ unsigned Internal::schedule_sweeping (Sweeper &sweeper) { void Internal::unschedule_sweeping (Sweeper &sweeper, unsigned swept, unsigned scheduled) { -#ifdef QUIET +#ifdef CADICAL_QUIET (void) scheduled, (void) swept; #endif - assert (sweep_schedule.empty ()); - assert (sweep_incomplete); + CADICAL_assert (sweep_schedule.empty ()); + CADICAL_assert (sweep_incomplete); for (all_scheduled (idx)) if (active (idx)) { sweep_schedule.push_back (idx); LOG ("untried scheduled %d", idx); } -#ifndef QUIET +#ifndef CADICAL_QUIET const unsigned retained = sweep_schedule.size (); #endif VERBOSE (3, "retained %u variables %.0f%% to be swept next time", @@ -1883,7 +1887,7 @@ bool Internal::sweep () { SET_EFFORT_LIMIT (tickslimit, sweep, !opts.sweepcomplete); delaying_sweep.bumpreasons.unbypass_delay (); - assert (!level); + CADICAL_assert (!level); START_SIMPLIFIER (sweep, SWEEP); stats.sweep++; uint64_t equivalences = stats.sweep_equivalences; @@ -1893,7 +1897,7 @@ bool Internal::sweep () { sweeper.limit.ticks = INT64_MAX; else sweeper.limit.ticks = tickslimit - stats.ticks.sweep; - sweep_set_kitten_ticks_limit (sweeper); + sweep_set_cadical_kitten_ticks_limit (sweeper); const unsigned scheduled = schedule_sweeping (sweeper); uint64_t swept = 0, limit = 10; for (;;) { @@ -1901,13 +1905,13 @@ bool Internal::sweep () { break; if (terminated_asynchronously ()) break; - if (kitten_ticks_limit_hit (sweeper, "sweeping loop")) + if (cadical_kitten_ticks_limit_hit (sweeper, "sweeping loop")) break; int idx = next_scheduled (sweeper); if (idx == 0) break; flags (idx).sweep = false; -#ifndef QUIET +#ifndef CADICAL_QUIET const char *res = #endif sweep_variable (sweeper, idx); @@ -1951,3 +1955,5 @@ bool Internal::sweep () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/terminal.cpp b/src/sat/cadical/cadical_terminal.cpp similarity index 78% rename from src/sat/cadical/terminal.cpp rename to src/sat/cadical/cadical_terminal.cpp index 919d81c9a..c8aeea2ab 100644 --- a/src/sat/cadical/terminal.cpp +++ b/src/sat/cadical/cadical_terminal.cpp @@ -1,11 +1,20 @@ +#include "global.h" + #include "internal.hpp" +#ifdef WIN32 +#include +#define isatty _isatty +#endif + +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { Terminal::Terminal (FILE *f) : file (f), reset_on_exit (false) { - assert (file); + CADICAL_assert (file); int fd = fileno (f); - assert (fd == 1 || fd == 2); + CADICAL_assert (fd == 1 || fd == 2); use_colors = connected = isatty (fd); } @@ -36,3 +45,5 @@ Terminal tout (stdout); Terminal terr (stderr); } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/ternary.cpp b/src/sat/cadical/cadical_ternary.cpp similarity index 92% rename from src/sat/cadical/ternary.cpp rename to src/sat/cadical/cadical_ternary.cpp index 259404fd8..9f99e2279 100644 --- a/src/sat/cadical/ternary.cpp +++ b/src/sat/cadical/cadical_ternary.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -23,9 +27,9 @@ namespace CaDiCaL { // literals already exists. bool Internal::ternary_find_binary_clause (int a, int b) { - assert (occurring ()); - assert (active (a)); - assert (active (b)); + CADICAL_assert (occurring ()); + CADICAL_assert (active (a)); + CADICAL_assert (active (b)); size_t s = occs (a).size (); size_t t = occs (b).size (); int lit = s < t ? a : b; @@ -49,10 +53,10 @@ bool Internal::ternary_find_binary_clause (int a, int b) { // literals already exists or is subsumed by an existing binary clause. bool Internal::ternary_find_ternary_clause (int a, int b, int c) { - assert (occurring ()); - assert (active (a)); - assert (active (b)); - assert (active (c)); + CADICAL_assert (occurring ()); + CADICAL_assert (active (a)); + CADICAL_assert (active (b)); + CADICAL_assert (active (c)); size_t r = occs (a).size (); size_t s = occs (b).size (); size_t t = occs (c).size (); @@ -79,7 +83,7 @@ bool Internal::ternary_find_ternary_clause (int a, int b, int c) { if (lits[0] == c && lits[1] == b) return true; } else { - assert (d->size == 3); + CADICAL_assert (d->size == 3); if (lits[0] == a && lits[1] == b && lits[2] == c) return true; if (lits[0] == a && lits[1] == c && lits[2] == b) @@ -111,9 +115,9 @@ bool Internal::hyper_ternary_resolve (Clause *c, int pivot, Clause *d) { LOG (c, "1st antecedent"); LOG (d, "2nd antecedent"); stats.ternres++; - assert (c->size == 3); - assert (d->size == 3); - assert (clause.empty ()); + CADICAL_assert (c->size == 3); + CADICAL_assert (d->size == 3); + CADICAL_assert (clause.empty ()); for (const auto &lit : *c) if (lit != pivot) clause.push_back (lit); @@ -160,7 +164,7 @@ void Internal::ternary_lit (int pivot, int64_t &steps, int64_t &htrs) { if (c->garbage) continue; if (c->size != 3) { - assert (c->size == 2); + CADICAL_assert (c->size == 2); continue; } if (--steps < 0) @@ -182,7 +186,7 @@ void Internal::ternary_lit (int pivot, int64_t &steps, int64_t &htrs) { if (d->garbage) continue; if (d->size != 3) { - assert (d->size == 2); + CADICAL_assert (d->size == 2); continue; } for (const auto &lit : *d) @@ -192,13 +196,13 @@ void Internal::ternary_lit (int pivot, int64_t &steps, int64_t &htrs) { } if (assigned) continue; - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); htrs--; if (hyper_ternary_resolve (c, pivot, d)) { size_t size = clause.size (); bool red = (size == 3 || (c->redundant && d->redundant)); if (lrat) { - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); lrat_chain.push_back (c->id); lrat_chain.push_back (d->id); } @@ -218,7 +222,7 @@ void Internal::ternary_lit (int pivot, int64_t &steps, int64_t &htrs) { stats.htrs2++; break; } else { - assert (r->size == 3); + CADICAL_assert (r->size == 3); stats.htrs3++; } } else { @@ -235,8 +239,8 @@ void Internal::ternary_lit (int pivot, int64_t &steps, int64_t &htrs) { // number of positive and negative occurrence. void Internal::ternary_idx (int idx, int64_t &steps, int64_t &htrs) { - assert (0 < idx); - assert (idx <= max_var); + CADICAL_assert (0 < idx); + CADICAL_assert (idx <= max_var); steps -= 3; if (!active (idx)) return; @@ -266,9 +270,9 @@ void Internal::ternary_idx (int idx, int64_t &steps, int64_t &htrs) { bool Internal::ternary_round (int64_t &steps_limit, int64_t &htrs_limit) { - assert (!unsat); + CADICAL_assert (!unsat); -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t bincon = 0; int64_t terncon = 0; #endif @@ -294,14 +298,14 @@ bool Internal::ternary_round (int64_t &steps_limit, int64_t &htrs_limit) { if (assigned) continue; if (c->size == 2) { -#ifndef QUIET +#ifndef CADICAL_QUIET bincon++; #endif } else { - assert (c->size == 3); + CADICAL_assert (c->size == 3); if (!marked) continue; -#ifndef QUIET +#ifndef CADICAL_QUIET terncon++; #endif } @@ -347,7 +351,7 @@ bool Internal::ternary_round (int64_t &steps_limit, int64_t &htrs_limit) { PHASE ("ternary", stats.ternary, "completed hyper ternary resolution"); reset_occs (); - assert (!unsat); + CADICAL_assert (!unsat); return remain; // Are there variables that should be tried again? } @@ -373,9 +377,9 @@ bool Internal::ternary () { START_SIMPLIFIER (ternary, TERNARY); stats.ternary++; - assert (!level); + CADICAL_assert (!level); - assert (!unsat); + CADICAL_assert (!unsat); if (watching ()) reset_watches (); @@ -430,8 +434,8 @@ bool Internal::ternary () { break; } - assert (!occurring ()); - assert (!unsat); + CADICAL_assert (!occurring ()); + CADICAL_assert (!unsat); init_watches (); connect_watches (); if (!propagate ()) { @@ -448,3 +452,5 @@ bool Internal::ternary () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/tier.cpp b/src/sat/cadical/cadical_tier.cpp similarity index 89% rename from src/sat/cadical/tier.cpp rename to src/sat/cadical/cadical_tier.cpp index 8112d81ac..92faf507f 100644 --- a/src/sat/cadical/tier.cpp +++ b/src/sat/cadical/cadical_tier.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void Internal::recompute_tier () { @@ -12,11 +16,11 @@ void Internal::recompute_tier () { lim.recompute_tier = stats.conflicts + delta; LOG ("rescheduling in %zd at %zd (conflicts at %zd)", delta, lim.recompute_tier, stats.conflicts); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG uint64_t total_used = 0; for (auto u : stats.used[stable]) total_used += u; - assert (total_used == stats.bump_used[stable]); + CADICAL_assert (total_used == stats.bump_used[stable]); #endif if (!stats.bump_used[stable]) { @@ -50,4 +54,6 @@ void Internal::recompute_tier () { stable ? "stable" : "focused"); } -} // namespace CaDiCaL \ No newline at end of file +} // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/transred.cpp b/src/sat/cadical/cadical_transred.cpp similarity index 94% rename from src/sat/cadical/transred.cpp rename to src/sat/cadical/cadical_transred.cpp index f9dc0cafa..82a961dee 100644 --- a/src/sat/cadical/transred.cpp +++ b/src/sat/cadical/cadical_transred.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { // Implement transitive reduction in the binary implication graph. This is @@ -18,8 +22,8 @@ void Internal::transred () { if (!stats.current.redundant && !stats.current.irredundant) return; - assert (opts.transred); - assert (!level); + CADICAL_assert (opts.transred); + CADICAL_assert (!level); START_SIMPLIFIER (transred, TRANSRED); stats.transreds++; @@ -127,7 +131,7 @@ void Internal::transred () { // const bool irredundant = !c->redundant; - assert (work.empty ()); + CADICAL_assert (work.empty ()); mark (src); work.push_back (src); LOG ("transred assign %d", src); @@ -137,13 +141,13 @@ void Internal::transred () { size_t j = 0; // 'propagated' in BFS - assert (lrat_chain.empty ()); - assert (mini_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); + CADICAL_assert (mini_chain.empty ()); vector parents; while (!transitive && !failed && j < work.size ()) { const int lit = work[j++]; - assert (marked (lit) > 0); + CADICAL_assert (marked (lit) > 0); LOG ("transred propagating %d", lit); propagations++; const Watches &ws = watches (-lit); @@ -198,7 +202,7 @@ void Internal::transred () { const int lit = work.back (); work.pop_back (); if (lrat && failed && !work.empty ()) { - assert (!parents.empty () && !mini_chain.empty ()); + CADICAL_assert (!parents.empty () && !mini_chain.empty ()); LOG ("transred LRAT current lit %d next pos %d next neg %d", lit, next_pos, next_neg); if (lit == failed_lit || lit == next_pos) { @@ -214,7 +218,7 @@ void Internal::transred () { unmark (lit); } mini_chain.clear (); - assert (mini_chain.empty ()); + CADICAL_assert (mini_chain.empty ()); if (lrat && failed) { reverse (lrat_chain.begin (), lrat_chain.end ()); } @@ -251,3 +255,5 @@ void Internal::transred () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/unstable.cpp b/src/sat/cadical/cadical_unstable.cpp similarity index 67% rename from src/sat/cadical/unstable.cpp rename to src/sat/cadical/cadical_unstable.cpp index 5b23164be..c3162d70a 100644 --- a/src/sat/cadical/unstable.cpp +++ b/src/sat/cadical/cadical_unstable.cpp @@ -1,10 +1,14 @@ +#include "global.h" + #ifdef PROFILE_MODE #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { bool Internal::propagate_unstable () { - assert (!stable); + CADICAL_assert (!stable); START (propunstable); bool res = propagate (); STOP (propunstable); @@ -12,18 +16,23 @@ bool Internal::propagate_unstable () { } void Internal::analyze_unstable () { - assert (!stable); + CADICAL_assert (!stable); START (analyzeunstable); analyze (); STOP (analyzeunstable); } int Internal::decide_unstable () { - assert (!stable); + CADICAL_assert (!stable); return decide (); } }; // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END + #else +ABC_NAMESPACE_IMPL_START int unstable_if_no_profile_mode; +ABC_NAMESPACE_IMPL_END #endif diff --git a/src/sat/cadical/util.cpp b/src/sat/cadical/cadical_util.cpp similarity index 94% rename from src/sat/cadical/util.cpp rename to src/sat/cadical/cadical_util.cpp index 5b7c52f30..fd9b5b29d 100644 --- a/src/sat/cadical/util.cpp +++ b/src/sat/cadical/cadical_util.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -46,7 +50,7 @@ bool parse_int_str (const char *val_str, int &val) { } else if (ch) return false; - assert (exponent <= 10); + CADICAL_assert (exponent <= 10); int64_t val64 = mantissa; for (int i = 0; i < exponent; i++) val64 *= 10; @@ -60,8 +64,8 @@ bool parse_int_str (const char *val_str, int &val) { val64 = INT_MAX; } - assert (INT_MIN <= val64); - assert (val64 <= INT_MAX); + CADICAL_assert (INT_MIN <= val64); + CADICAL_assert (val64 <= INT_MAX); val = val64; } @@ -127,3 +131,5 @@ uint64_t hash_string (const char *str) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/var.cpp b/src/sat/cadical/cadical_var.cpp similarity index 63% rename from src/sat/cadical/var.cpp rename to src/sat/cadical/cadical_var.cpp index 0baa0b291..e2348da7b 100644 --- a/src/sat/cadical/var.cpp +++ b/src/sat/cadical/cadical_var.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void Internal::reset_subsume_bits () { @@ -9,7 +13,7 @@ void Internal::reset_subsume_bits () { } void Internal::check_var_stats () { -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG int64_t fixed = 0, eliminated = 0, substituted = 0, pure = 0, unused = 0; for (auto idx : vars) { Flags &f = flags (idx); @@ -26,14 +30,16 @@ void Internal::check_var_stats () { if (f.pure ()) pure++; } - assert (stats.now.fixed == fixed); - assert (stats.now.eliminated == eliminated); - assert (stats.now.substituted == substituted); - assert (stats.now.pure == pure); + CADICAL_assert (stats.now.fixed == fixed); + CADICAL_assert (stats.now.eliminated == eliminated); + CADICAL_assert (stats.now.substituted == substituted); + CADICAL_assert (stats.now.pure == pure); int64_t inactive = unused + fixed + eliminated + substituted + pure; - assert (stats.inactive == inactive); - assert (max_var == stats.active + stats.inactive); + CADICAL_assert (stats.inactive == inactive); + CADICAL_assert (max_var == stats.active + stats.inactive); #endif } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/veripbtracer.cpp b/src/sat/cadical/cadical_veripbtracer.cpp similarity index 92% rename from src/sat/cadical/veripbtracer.cpp rename to src/sat/cadical/cadical_veripbtracer.cpp index 3a8c817f7..b1f7c95a7 100644 --- a/src/sat/cadical/veripbtracer.cpp +++ b/src/sat/cadical/cadical_veripbtracer.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -8,7 +12,7 @@ VeripbTracer::VeripbTracer (Internal *i, File *f, bool b, bool a, bool c) : internal (i), file (f), with_antecedents (a), checked_deletions (c), num_clauses (0), size_clauses (0), clauses (0), last_hash (0), last_id (0), last_clause (0) -#ifndef QUIET +#ifndef CADICAL_QUIET , added (0), deleted (0) #endif @@ -22,10 +26,10 @@ VeripbTracer::VeripbTracer (Internal *i, File *f, bool b, bool a, bool c) uint64_t nonce = random.next (); if (!(nonce & 1)) nonce++; - assert (nonce), assert (nonce & 1); + CADICAL_assert (nonce), CADICAL_assert (nonce & 1); nonces[n] = nonce; } -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG binary = b; #else (void) b; @@ -50,7 +54,7 @@ VeripbTracer::~VeripbTracer () { /*------------------------------------------------------------------------*/ void VeripbTracer::enlarge_clauses () { - assert (num_clauses == size_clauses); + CADICAL_assert (num_clauses == size_clauses); const uint64_t new_size_clauses = size_clauses ? 2 * size_clauses : 1; LOG ("VeriPB Tracer enlarging clauses from %" PRIu64 " to %" PRIu64, (uint64_t) size_clauses, (uint64_t) new_size_clauses); @@ -81,13 +85,13 @@ HashId *VeripbTracer::new_clause () { } void VeripbTracer::delete_clause (HashId *c) { - assert (c); + CADICAL_assert (c); num_clauses--; delete c; } uint64_t VeripbTracer::reduce_hash (uint64_t hash, uint64_t size) { - assert (size > 0); + CADICAL_assert (size > 0); unsigned shift = 32; uint64_t res = hash; while ((((uint64_t) 1) << shift) > size) { @@ -95,12 +99,12 @@ uint64_t VeripbTracer::reduce_hash (uint64_t hash, uint64_t size) { shift >>= 1; } res &= size - 1; - assert (res < size); + CADICAL_assert (res < size); return res; } uint64_t VeripbTracer::compute_hash (const int64_t id) { - assert (id > 0); + CADICAL_assert (id > 0); unsigned j = id % num_nonces; // Dont know if this is a good uint64_t tmp = nonces[j] * (uint64_t) id; // hash funktion or even better return last_hash = tmp; // than just using id. @@ -129,7 +133,7 @@ bool VeripbTracer::find_and_delete (const int64_t id) { } if (!c) return false; - assert (c && res); + CADICAL_assert (c && res); *res = c->next; delete_clause (c); return true; @@ -147,15 +151,15 @@ void VeripbTracer::insert () { /*------------------------------------------------------------------------*/ inline void VeripbTracer::put_binary_zero () { - assert (binary); - assert (file); + CADICAL_assert (binary); + CADICAL_assert (file); file->put ((unsigned char) 0); } inline void VeripbTracer::put_binary_lit (int lit) { - assert (binary); - assert (file); - assert (lit != INT_MIN); + CADICAL_assert (binary); + CADICAL_assert (file); + CADICAL_assert (lit != INT_MIN); unsigned x = 2 * abs (lit) + (lit < 0); unsigned char ch; while (x & ~0x7f) { @@ -168,8 +172,8 @@ inline void VeripbTracer::put_binary_lit (int lit) { } inline void VeripbTracer::put_binary_id (int64_t id, bool can_be_negative) { - assert (binary); - assert (file); + CADICAL_assert (binary); + CADICAL_assert (file); uint64_t x = abs (id); if (can_be_negative) { x = 2 * x + (id < 0); @@ -300,7 +304,7 @@ void VeripbTracer::add_derived_clause (int64_t id, bool redundant, veripb_add_derived_clause (id, redundant, clause, chain); else veripb_add_derived_clause (id, redundant, clause); -#ifndef QUIET +#ifndef CADICAL_QUIET added++; #endif } @@ -311,7 +315,7 @@ void VeripbTracer::delete_clause (int64_t id, bool redundant, return; LOG ("VERIPB TRACER tracing deletion of clause[%" PRId64 "]", id); veripb_delete_clause (id, redundant); -#ifndef QUIET +#ifndef CADICAL_QUIET deleted++; #endif } @@ -349,7 +353,7 @@ void VeripbTracer::strengthen (int64_t id) { bool VeripbTracer::closed () { return file->closed (); } -#ifndef QUIET +#ifndef CADICAL_QUIET void VeripbTracer::print_statistics () { // TODO complete @@ -366,9 +370,9 @@ void VeripbTracer::print_statistics () { #endif void VeripbTracer::close (bool print) { - assert (!closed ()); + CADICAL_assert (!closed ()); file->close (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { MSG ("VeriPB proof file '%s' closed", file->name ()); print_statistics (); @@ -379,9 +383,9 @@ void VeripbTracer::close (bool print) { } void VeripbTracer::flush (bool print) { - assert (!closed ()); + CADICAL_assert (!closed ()); file->flush (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (print) { MSG ("VeriPB proof file '%s' flushed", file->name ()); print_statistics (); @@ -392,3 +396,5 @@ void VeripbTracer::flush (bool print) { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/version.cpp b/src/sat/cadical/cadical_version.cpp similarity index 89% rename from src/sat/cadical/version.cpp rename to src/sat/cadical/cadical_version.cpp index c96540fb8..a0cceef63 100644 --- a/src/sat/cadical/version.cpp +++ b/src/sat/cadical/cadical_version.cpp @@ -1,10 +1,12 @@ +#include "global.h" + /*------------------------------------------------------------------------*/ // To simplify the build process without 'make', you can disable the // generation of 'build.hpp' through '../scripts/make-build-header.sh' by -// defining '-DNBUILD'. Then we try to guess part of the configuration. +// defining '-DCADICAL_NBUILD'. Then we try to guess part of the configuration. -#ifndef NBUILD +#ifndef CADICAL_NBUILD #if __GNUC__ > 4 #if __has_include() #include "build.hpp" @@ -12,7 +14,7 @@ #else #include "build.hpp" #endif // __GNUC > 4 -#endif // NBUILD +#endif // CADICAL_NBUILD /*------------------------------------------------------------------------*/ @@ -22,11 +24,11 @@ // the file '../VERSION' with '../scripts/update-version.sh'. The standard // build process relies on 'VERSION' to be defined in 'build.hpp'. -#ifdef NBUILD +#ifdef CADICAL_NBUILD #ifndef VERSION #define VERSION "2.2.0-rc1" #endif // VERSION -#endif // NBUILD +#endif // CADICAL_NBUILD /*------------------------------------------------------------------------*/ @@ -40,7 +42,7 @@ static const char *AFFILIATIONS = /*------------------------------------------------------------------------*/ -// Again if we do not have 'NBUILD' or if something during configuration is +// Again if we do not have 'CADICAL_NBUILD' or if something during configuration is // broken we still want to be able to compile the solver. In this case we // then try our best to figure out 'COMPILER' and 'DATE', but for // 'IDENTIFIER' and 'FLAGS' we can only use the '0' string, which marks @@ -92,6 +94,8 @@ static const char *AFFILIATIONS = #include "version.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { const char *version () { return VERSION; } @@ -105,3 +109,5 @@ const char *date () { return DATE; } const char *flags () { return FLAGS; } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/vivify.cpp b/src/sat/cadical/cadical_vivify.cpp similarity index 92% rename from src/sat/cadical/vivify.cpp rename to src/sat/cadical/cadical_vivify.cpp index cf6ec1fb8..8d99c2932 100644 --- a/src/sat/cadical/vivify.cpp +++ b/src/sat/cadical/cadical_vivify.cpp @@ -1,3 +1,5 @@ +#include "global.h" + #include "vivify.hpp" #include "internal.hpp" #include "util.hpp" @@ -5,6 +7,8 @@ #include #include +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -50,26 +54,26 @@ inline void Internal::vivify_subsume_clause (Clause *subsuming, Clause *subsumed) { stats.subsumed++; stats.vivifysubs++; -#ifndef NDEBUG - assert (subsuming); - assert (subsumed); - assert (subsuming != subsumed); - assert (!subsumed->garbage); +#ifndef CADICAL_NDEBUG + CADICAL_assert (subsuming); + CADICAL_assert (subsumed); + CADICAL_assert (subsuming != subsumed); + CADICAL_assert (!subsumed->garbage); // size after removeing units; int real_size_subsuming = 0, real_size_subsumed = 0; for (auto lit : *subsuming) { if (!val (lit) || var (lit).level) ++real_size_subsuming; else - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); } for (auto lit : *subsumed) { if (!val (lit) || var (lit).level) ++real_size_subsumed; else - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); } - assert (real_size_subsuming <= real_size_subsumed); + CADICAL_assert (real_size_subsuming <= real_size_subsumed); #endif LOG (subsumed, "subsumed"); if (subsumed->redundant) { @@ -80,7 +84,7 @@ inline void Internal::vivify_subsume_clause (Clause *subsuming, ++stats.vivifysubirr; } if (subsuming->garbage) { - assert (subsuming->size == 2); + CADICAL_assert (subsuming->size == 2); LOG (subsuming, "binary subsuming clause was already deleted, so undeleting"); subsuming->garbage = false; @@ -104,9 +108,9 @@ inline void Internal::vivify_subsume_clause (Clause *subsuming, stats.current.irredundant++; stats.added.irredundant++; stats.irrlits += subsuming->size; - assert (stats.current.redundant > 0); + CADICAL_assert (stats.current.redundant > 0); stats.current.redundant--; - assert (stats.added.redundant > 0); + CADICAL_assert (stats.added.redundant > 0); stats.added.redundant--; // ... and keep 'stats.added.total'. } @@ -117,12 +121,12 @@ inline void Internal::demote_clause (Clause *c) { stats.subsumed++; stats.vivifydemote++; LOG (c, "demoting"); - assert (!c->redundant); + CADICAL_assert (!c->redundant); mark_removed (c); c->redundant = true; - assert (stats.current.irredundant > 0); + CADICAL_assert (stats.current.irredundant > 0); stats.current.irredundant--; - assert (stats.added.irredundant > 0); + CADICAL_assert (stats.added.irredundant > 0); stats.added.irredundant--; stats.irrlits -= c->size; stats.current.redundant++; @@ -142,12 +146,12 @@ inline void Internal::demote_clause (Clause *c) { inline void Internal::vivify_assign (int lit, Clause *reason) { require_mode (VIVIFY); const int idx = vidx (lit); - assert (!vals[idx]); - assert (!flags (idx).eliminated () || !reason); + CADICAL_assert (!vals[idx]); + CADICAL_assert (!flags (idx).eliminated () || !reason); Var &v = var (idx); v.level = level; // required to reuse decisions v.trail = (int) trail.size (); // used in 'vivify_better_watch' - assert ((int) num_assigned < max_var); + CADICAL_assert ((int) num_assigned < max_var); num_assigned++; v.reason = level ? reason : 0; // for conflict analysis if (!level) @@ -155,8 +159,8 @@ inline void Internal::vivify_assign (int lit, Clause *reason) { const signed char tmp = sign (lit); vals[idx] = tmp; vals[-idx] = -tmp; - assert (val (lit) > 0); - assert (val (-lit) < 0); + CADICAL_assert (val (lit) > 0); + CADICAL_assert (val (-lit) < 0); trail.push_back (lit); LOG (reason, "vivify assign %d", lit); } @@ -168,8 +172,8 @@ void Internal::vivify_assume (int lit) { level++; control.push_back (Level (lit, trail.size ())); LOG ("vivify decide %d", lit); - assert (level > 0); - assert (propagated == trail.size ()); + CADICAL_assert (level > 0); + CADICAL_assert (propagated == trail.size ()); vivify_assign (lit, 0); } @@ -179,7 +183,7 @@ void Internal::vivify_assume (int lit) { bool Internal::vivify_propagate (int64_t &ticks) { require_mode (VIVIFY); - assert (!unsat); + CADICAL_assert (!unsat); START (propagate); int64_t before = propagated2 = propagated; for (;;) { @@ -239,12 +243,12 @@ bool Internal::vivify_propagate (int64_t &ticks) { k++; if (v < 0) { k = lits + 2; - assert (w.clause->pos <= size); + CADICAL_assert (w.clause->pos <= size); while (k != middle && (v = val (r = *k)) < 0) k++; } w.clause->pos = k - lits; - assert (lits + 2 <= k), assert (k <= w.clause->end ()); + CADICAL_assert (lits + 2 <= k), CADICAL_assert (k <= w.clause->end ()); if (v > 0) j[-1].blit = r; else if (!v) { @@ -261,7 +265,7 @@ bool Internal::vivify_propagate (int64_t &ticks) { continue; } ticks++; - assert (v < 0); + CADICAL_assert (v < 0); vivify_chain_for_units (other, w.clause); vivify_assign (other, w.clause); lrat_chain.clear (); @@ -270,8 +274,8 @@ bool Internal::vivify_propagate (int64_t &ticks) { LOG ("ignoring conflict due to clause to vivify"); continue; } - assert (u < 0); - assert (v < 0); + CADICAL_assert (u < 0); + CADICAL_assert (v < 0); conflict = w.clause; break; } @@ -395,7 +399,7 @@ struct vivify_clause_later { // Among redundant clauses (in redundant mode) prefer small glue. // if (a->redundant) { - assert (b->redundant); + CADICAL_assert (b->redundant); if (a->glue > b->glue) return true; if (a->glue < b->glue) @@ -474,9 +478,9 @@ void Internal::flush_vivification_schedule (std::vector &schedule, if (k == eop) { LOG (c, "found subsumed"); LOG (prev, "subsuming"); - assert (!c->garbage); - assert (!prev->garbage); - assert (c->redundant || !prev->redundant); + CADICAL_assert (!c->garbage); + CADICAL_assert (!prev->garbage); + CADICAL_assert (c->redundant || !prev->redundant); mark_garbage (c); subsumed++; j--; @@ -494,7 +498,7 @@ void Internal::flush_vivification_schedule (std::vector &schedule, schedule.resize (j - schedule.begin ()); shrink_vector (schedule); } else - assert (j == end); + CADICAL_assert (j == end); } /*------------------------------------------------------------------------*/ @@ -513,7 +517,7 @@ bool Internal::consider_to_vivify_clause (Clause *c) { return false; if (!c->redundant) return true; - assert (c->redundant); + CADICAL_assert (c->redundant); // likely_to_be_kept_clause is too aggressive at removing tier-3 clauses return true; @@ -550,14 +554,14 @@ struct vivify_better_watch { void Internal::vivify_strengthen (Clause *c) { - assert (!clause.empty ()); + CADICAL_assert (!clause.empty ()); if (clause.size () == 1) { backtrack_without_updating_phases (); const int unit = clause[0]; LOG (c, "vivification shrunken to unit %d", unit); - assert (!val (unit)); + CADICAL_assert (!val (unit)); assign_unit (unit); // lrat_chain.clear (); done in search_assign stats.vivifyunits++; @@ -590,12 +594,12 @@ void Internal::vivify_strengthen (Clause *c) { new_level = level1 - 1; } - assert (new_level >= 0); + CADICAL_assert (new_level >= 0); if (new_level < level) backtrack (new_level); - assert (val (lit0) >= 0); - assert (val (lit1) >= 0 || (val (lit0) > 0 && val (lit1) < 0 && + CADICAL_assert (val (lit0) >= 0); + CADICAL_assert (val (lit1) >= 0 || (val (lit0) > 0 && val (lit1) < 0 && var (lit0).level <= var (lit1).level)); Clause *d = new_clause_as (c); @@ -631,12 +635,12 @@ void Internal::vivify_sort_watched (Clause *c) { new_level = level1 - 1; } - assert (new_level >= 0); + CADICAL_assert (new_level >= 0); if (new_level < level) backtrack (new_level); - assert (val (lit0) >= 0); - assert (val (lit1) >= 0 || (val (lit0) > 0 && val (lit1) < 0 && + CADICAL_assert (val (lit0) >= 0); + CADICAL_assert (val (lit1) >= 0 || (val (lit0) > 0 && val (lit1) < 0 && var (lit0).level <= var (lit1).level)); } // Conflict analysis from 'start' which learns a decision only clause. @@ -652,8 +656,8 @@ void Internal::vivify_analyze (Clause *start, bool &subsumes, const auto &t = &trail; // normal trail, so next_trail is wrong int i = t->size (); // Start at end-of-trail. Clause *reason = start; - assert (reason); - assert (!trail.empty ()); + CADICAL_assert (reason); + CADICAL_assert (!trail.empty ()); int uip = trail.back (); bool mark_implied = (implied); @@ -692,7 +696,7 @@ void Internal::vivify_analyze (Clause *start, bool &subsumes, continue; } - assert (val (other)); + CADICAL_assert (val (other)); if (f.seen) continue; LOG ("pushing lit %d", other); @@ -704,7 +708,7 @@ void Internal::vivify_analyze (Clause *start, bool &subsumes, promote_clause (start, new_glue); } if (subsumes) { - assert (reason); + CADICAL_assert (reason); LOG (reason, "clause found subsuming"); LOG (candidate, "clause found subsumed"); *subsuming = reason; @@ -718,7 +722,7 @@ void Internal::vivify_analyze (Clause *start, bool &subsumes, uip = 0; while (!uip && i > 0) { - assert (i > 0); + CADICAL_assert (i > 0); const int lit = (*t)[--i]; if (!var (lit).level) continue; @@ -739,16 +743,16 @@ void Internal::vivify_analyze (Clause *start, bool &subsumes, void Internal::vivify_deduce (Clause *candidate, Clause *conflict, int implied, Clause **subsuming, bool &redundant) { - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); bool subsumes; Clause *reason; - assert (clause.empty ()); + CADICAL_assert (clause.empty ()); if (implied) { reason = candidate; mark2 (candidate); const int not_implied = -implied; - assert (var (not_implied).level); + CADICAL_assert (var (not_implied).level); Flags &f = flags (not_implied); f.seen = true; LOG ("pushing implied lit %d", not_implied); @@ -756,8 +760,8 @@ void Internal::vivify_deduce (Clause *candidate, Clause *conflict, clause.push_back (implied); } else { reason = (conflict ? conflict : candidate); - assert (reason); - assert (!reason->garbage); + CADICAL_assert (reason); + CADICAL_assert (!reason->garbage); mark2 (candidate); subsumes = (candidate != reason); redundant = reason->redundant; @@ -767,7 +771,7 @@ void Internal::vivify_deduce (Clause *candidate, Clause *conflict, for (auto lit : *reason) { const Var &v = var (lit); Flags &f = flags (lit); - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); if (!v.level) { if (!lrat) continue; @@ -783,7 +787,7 @@ void Internal::vivify_deduce (Clause *candidate, Clause *conflict, analyzed.push_back (lit); continue; } - assert (v.level); + CADICAL_assert (v.level); if (!marked2 (lit)) { LOG ("lit %d is not marked", lit); subsumes = false; @@ -798,8 +802,8 @@ void Internal::vivify_deduce (Clause *candidate, Clause *conflict, promote_clause (reason, new_glue); } if (subsumes) { - assert (candidate != reason); -#ifndef NDEBUG + CADICAL_assert (candidate != reason); +#ifndef CADICAL_NDEBUG int nonfalse_reason = 0; for (auto lit : *reason) if (!fixed (lit)) @@ -810,7 +814,7 @@ void Internal::vivify_deduce (Clause *candidate, Clause *conflict, if (!fixed (lit)) ++nonfalse_candidate; - assert (nonfalse_reason <= nonfalse_candidate); + CADICAL_assert (nonfalse_reason <= nonfalse_candidate); #endif LOG (candidate, "vivify subsumed 0"); LOG (reason, "vivify subsuming 0"); @@ -826,7 +830,7 @@ void Internal::vivify_deduce (Clause *candidate, Clause *conflict, redundant); unmark (candidate); if (subsumes) { - assert (*subsuming); + CADICAL_assert (*subsuming); LOG (candidate, "vivify subsumed"); LOG (*subsuming, "vivify subsuming"); if (lrat) @@ -854,7 +858,7 @@ bool Internal::vivify_shrinkable (const std::vector &sorted, return true; } } else { - assert (value < 0); + CADICAL_assert (value < 0); const Var &v = var (lit); const Flags &f = flags (lit); if (!v.level) @@ -885,7 +889,7 @@ inline void Internal::vivify_increment_stats (const Vivifier &vivifier) { ++stats.vivifystred3; break; default: - assert (vivifier.tier == Vivify_Mode::IRREDUNDANT); + CADICAL_assert (vivifier.tier == Vivify_Mode::IRREDUNDANT); ++stats.vivifystrirr; break; } @@ -902,11 +906,11 @@ bool Internal::vivify_instantiate ( conflict = nullptr; const int lit = sorted.back (); LOG ("vivify instantiation"); - assert (!var (lit).reason); - assert (var (lit).level); - assert (val (lit)); + CADICAL_assert (!var (lit).reason); + CADICAL_assert (var (lit).level); + CADICAL_assert (val (lit)); backtrack (level - 1); - assert (val (lit) == 0); + CADICAL_assert (val (lit) == 0); stats.vivifydecs++; vivify_assume (lit); bool ok = vivify_propagate (ticks); @@ -916,7 +920,7 @@ bool Internal::vivify_instantiate ( // strengthen clause if (lrat) { clear_analyzed_literals (); - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); vivify_build_lrat (0, c, lrat_stack); vivify_build_lrat (0, conflict, lrat_stack); clear_analyzed_literals (); @@ -928,7 +932,7 @@ bool Internal::vivify_instantiate ( strengthen_clause (c, remove); vivify_sort_watched (c); watch_clause (c); - assert (!conflict); + CADICAL_assert (!conflict); return true; } else { LOG ("vivify instantiation failed"); @@ -942,13 +946,13 @@ bool Internal::vivify_instantiate ( bool Internal::vivify_clause (Vivifier &vivifier, Clause *c) { - assert (c->size > 2); // see (NO-BINARY) below - assert (analyzed.empty ()); + CADICAL_assert (c->size > 2); // see (NO-BINARY) below + CADICAL_assert (analyzed.empty ()); c->vivify = false; // mark as checked / tried c->vivified = true; // and globally remember - assert (!c->garbage); + CADICAL_assert (!c->garbage); auto &lrat_stack = vivifier.lrat_stack; auto &ticks = vivifier.ticks; @@ -973,7 +977,7 @@ bool Internal::vivify_clause (Vivifier &vivifier, Clause *c) { sorted.push_back (lit); } - assert (sorted.size () > 1); + CADICAL_assert (sorted.size () > 1); if (sorted.size () == 2) { LOG ("skipping actual binary"); return false; @@ -1027,7 +1031,7 @@ bool Internal::vivify_clause (Vivifier &vivifier, Clause *c) { } if (forced) { LOG ("clause is reason forcing %d", forced); - assert (var (forced).level); + CADICAL_assert (var (forced).level); backtrack_without_updating_phases (var (forced).level - 1); } @@ -1039,7 +1043,7 @@ bool Internal::vivify_clause (Vivifier &vivifier, Clause *c) { int l = 1; // This is the decision level we want to reuse. for (const auto &lit : sorted) { - assert (!fixed (lit)); + CADICAL_assert (!fixed (lit)); const int decision = control[l].decision; if (-lit == decision) { LOG ("reusing decision %d at decision level %d", decision, l); @@ -1103,25 +1107,25 @@ bool Internal::vivify_clause (Vivifier &vivifier, Clause *c) { if (tmp) { // literal already assigned const Var &v = var (lit); - assert (v.level); + CADICAL_assert (v.level); if (!v.reason) { LOG ("skipping decision %d", lit); continue; } if (tmp < 0) { - assert (v.level); + CADICAL_assert (v.level); LOG ("literal %d is already false and can be removed", lit); continue; } - assert (tmp > 0); + CADICAL_assert (tmp > 0); LOG ("subsumed since literal %d already true", lit); subsume = lit; // will be able to subsume candidate '@5' break; } - assert (!tmp); + CADICAL_assert (!tmp); stats.vivifydecs++; vivify_assume (-lit); @@ -1150,7 +1154,7 @@ bool Internal::vivify_clause (Vivifier &vivifier, Clause *c) { Clause *subsuming = nullptr; bool redundant = false; const int level_after_assumptions = level; - assert (level_after_assumptions); + CADICAL_assert (level_after_assumptions); vivify_deduce (c, conflict, subsume, &subsuming, redundant); bool res; @@ -1166,7 +1170,7 @@ bool Internal::vivify_clause (Vivifier &vivifier, Clause *c) { } if (subsuming) { - assert (c != subsuming); + CADICAL_assert (c != subsuming); LOG (c, "deleting subsumed clause"); if (c->redundant && subsuming->redundant && c->glue < subsuming->glue) { promote_clause (c, c->glue); @@ -1203,19 +1207,19 @@ bool Internal::vivify_clause (Vivifier &vivifier, Clause *c) { } else if (subsume) { LOG (c, "no vivification instantiation with implied literal %d", subsume); - assert (!c->redundant); - assert (redundant); + CADICAL_assert (!c->redundant); + CADICAL_assert (redundant); res = false; ++stats.vivifyimplied; } else { - assert (level > 2); - assert ((size_t) level == sorted.size ()); + CADICAL_assert (level > 2); + CADICAL_assert ((size_t) level == sorted.size ()); LOG (c, "vivification failed on"); lrat_chain.clear (); - assert (!subsume); + CADICAL_assert (!subsume); if (!subsume && opts.vivifyinst) { res = vivify_instantiate (sorted, c, lrat_stack, ticks); - assert (!conflict); + CADICAL_assert (!conflict); } else { LOG ("cannot apply instantiation"); res = false; @@ -1253,7 +1257,7 @@ bool Internal::vivify_clause (Vivifier &vivifier, Clause *c) { void Internal::vivify_build_lrat ( int lit, Clause *reason, std::vector> &stack) { - assert (stack.empty ()); + CADICAL_assert (stack.empty ()); stack.push_back ({lit, reason, false}); while (!stack.empty ()) { int lit; @@ -1271,8 +1275,8 @@ void Internal::vivify_build_lrat ( if (lit && reason) { Flags &f = flags (lit); f.seen = true; - analyzed.push_back (lit); // assert (val (other) < 0); - assert (flags (lit).seen); + analyzed.push_back (lit); // CADICAL_assert (val (other) < 0); + CADICAL_assert (flags (lit).seen); } continue; } else @@ -1309,11 +1313,11 @@ inline void Internal::vivify_chain_for_units (int lit, Clause *reason) { // equivalence if (opts.chrono && assignment_level (lit, reason)) return; if (level) return; // not decision level 0 - assert (lrat_chain.empty ()); + CADICAL_assert (lrat_chain.empty ()); for (auto &reason_lit : *reason) { if (lit == reason_lit) continue; - assert (val (reason_lit)); + CADICAL_assert (val (reason_lit)); const int signed_reason_lit = val (reason_lit) * reason_lit; int64_t id = unit_id (signed_reason_lit); lrat_chain.push_back (id); @@ -1344,7 +1348,7 @@ vivify_ref create_ref (Internal *internal, Clause *c) { } { const int64_t lit_count = internal->noccs (lit); - assert (lit_count); + CADICAL_assert (lit_count); LOG ("checking literal %d with %zd occurrences", lit, lit_count); if (lit_count <= best_count) continue; @@ -1353,9 +1357,9 @@ vivify_ref create_ref (Internal *internal, Clause *c) { } CONTINUE_WITH_NEXT_LITERAL:; } - assert (best); - assert (best_count); - assert (best_count < UINT32_MAX); + CADICAL_assert (best); + CADICAL_assert (best_count); + CADICAL_assert (best_count < UINT32_MAX); ref.count[i] = ((uint64_t) best_count << 32) + (uint64_t) internal->vlit (best); LOG ("final count at position %d is %d - %d: %lu", i, best, best_count, @@ -1402,7 +1406,7 @@ void Internal::vivify_initialize (Vivifier &vivifier, int64_t &ticks) { // Disconnect all watches since we sort literals within clauses. // - assert (watching ()); + CADICAL_assert (watching ()); #if 0 clear_watches (); #endif @@ -1497,7 +1501,11 @@ inline std::vector ¤t_refs_schedule (Vivifier &vivifier) { return vivifier.refs_schedule_irred; break; } +#ifdef WIN32 + __assume(false); +#else __builtin_unreachable (); +#endif } inline std::vector ¤t_schedule (Vivifier &vivifier) { @@ -1515,13 +1523,17 @@ inline std::vector ¤t_schedule (Vivifier &vivifier) { return vivifier.schedule_irred; break; } +#ifdef WIN32 + __assume(false); +#else __builtin_unreachable (); +#endif } struct vivify_refcount_rank { int offset; vivify_refcount_rank (int j) : offset (j) { - assert (offset < COUNTREF_COUNTS); + CADICAL_assert (offset < COUNTREF_COUNTS); } typedef uint64_t Type; Type operator() (const vivify_ref &a) const { return a.count[offset]; } @@ -1530,7 +1542,7 @@ struct vivify_refcount_rank { struct vivify_refcount_smaller { int offset; vivify_refcount_smaller (int j) : offset (j) { - assert (offset < COUNTREF_COUNTS); + CADICAL_assert (offset < COUNTREF_COUNTS); } bool operator() (const vivify_ref &a, const vivify_ref &b) const { const auto s = vivify_refcount_rank (offset) (a); @@ -1576,7 +1588,7 @@ void Internal::vivify_round (Vivifier &vivifier, int64_t ticks_limit) { "starting %c vivification round ticks limit %" PRId64 "", vivifier.tag, ticks_limit); - assert (watching ()); + CADICAL_assert (watching ()); auto &refs_schedule = current_refs_schedule (vivifier); auto &schedule = current_schedule (vivifier); @@ -1645,7 +1657,7 @@ void Internal::vivify_round (Vivifier &vivifier, int64_t ticks_limit) { // Limit the number of propagations during vivification as in 'probe'. // const int64_t limit = ticks_limit - stats.ticks.vivify; - assert (limit >= 0); + CADICAL_assert (limit >= 0); // the clauses might still contain set literals, so propagation since the // beginning @@ -1770,7 +1782,7 @@ void set_vivifier_mode (Vivifier &vivifier, Vivify_Mode tier) { vivifier.tag = 'w'; break; default: - assert (tier == Vivify_Mode::IRREDUNDANT); + CADICAL_assert (tier == Vivify_Mode::IRREDUNDANT); vivifier.tag = 'x'; break; } @@ -1801,8 +1813,8 @@ bool Internal::vivify () { return false; if (level) backtrack (); - assert (opts.vivify); - assert (!level); + CADICAL_assert (opts.vivify); + CADICAL_assert (!level); SET_EFFORT_LIMIT (totallimit, vivify, true); @@ -1854,12 +1866,12 @@ bool Internal::vivify () { if (limit < stats.ticks.vivify) limit = stats.ticks.vivify; const double effort = (total * tier1effort) / sumeffort; - assert (std::numeric_limits::max () - (int64_t) effort >= + CADICAL_assert (std::numeric_limits::max () - (int64_t) effort >= limit); limit += effort; if (limit - shared_effort > stats.ticks.vivify) { limit -= shared_effort; - assert (limit >= 0); + CADICAL_assert (limit >= 0); vivify_round (vivifier, limit); } else { LOG ("building the schedule already used our entire ticks budget for " @@ -1874,12 +1886,12 @@ bool Internal::vivify () { if (limit < stats.ticks.vivify) limit = stats.ticks.vivify; const double effort = (total * tier2effort) / sumeffort; - assert (std::numeric_limits::max () - (int64_t) effort >= + CADICAL_assert (std::numeric_limits::max () - (int64_t) effort >= limit); limit += effort; if (limit - shared_effort > stats.ticks.vivify) { limit -= shared_effort; - assert (limit >= 0); + CADICAL_assert (limit >= 0); set_vivifier_mode (vivifier, Vivify_Mode::TIER2); vivify_round (vivifier, limit); } else { @@ -1893,12 +1905,12 @@ bool Internal::vivify () { if (limit < stats.ticks.vivify) limit = stats.ticks.vivify; const double effort = (total * tier3effort) / sumeffort; - assert (std::numeric_limits::max () - (int64_t) effort >= + CADICAL_assert (std::numeric_limits::max () - (int64_t) effort >= limit); limit += effort; if (limit - shared_effort > stats.ticks.vivify) { limit -= shared_effort; - assert (limit >= 0); + CADICAL_assert (limit >= 0); set_vivifier_mode (vivifier, Vivify_Mode::TIER3); vivify_round (vivifier, limit); } else { @@ -1912,12 +1924,12 @@ bool Internal::vivify () { if (limit < stats.ticks.vivify) limit = stats.ticks.vivify; const double effort = (total * irreffort) / sumeffort; - assert (std::numeric_limits::max () - (int64_t) effort >= + CADICAL_assert (std::numeric_limits::max () - (int64_t) effort >= limit); limit += effort; if (limit - shared_effort > stats.ticks.vivify) { limit -= shared_effort; - assert (limit >= 0); + CADICAL_assert (limit >= 0); set_vivifier_mode (vivifier, Vivify_Mode::IRREDUNDANT); const int old = stats.vivifystrirr; const int old_tried = stats.vivifychecks; @@ -1946,3 +1958,5 @@ bool Internal::vivify () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/walk.cpp b/src/sat/cadical/cadical_walk.cpp similarity index 93% rename from src/sat/cadical/walk.cpp rename to src/sat/cadical/cadical_walk.cpp index ac671a7ad..13b1bf9f4 100644 --- a/src/sat/cadical/walk.cpp +++ b/src/sat/cadical/cadical_walk.cpp @@ -1,5 +1,9 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -52,9 +56,9 @@ inline static double fitcbval (double size) { const double x2 = cbvals[i + 1][0], x1 = cbvals[i][0]; const double y2 = cbvals[i + 1][1], y1 = cbvals[i][1]; const double dx = x2 - x1, dy = y2 - y1; - assert (dx); + CADICAL_assert (dx); const double res = dy * (size - x1) / dx + y1; - assert (res > 0); + CADICAL_assert (res > 0); return res; } @@ -71,7 +75,7 @@ Walker::Walker (Internal *i, double size, int64_t l) // const bool use_size_based_cb = (internal->stats.walk.count & 1); const double cb = use_size_based_cb ? fitcbval (size) : 2.0; - assert (cb); + CADICAL_assert (cb); const double base = 1 / cb; // scores are 'base^0,base^1,base^2,... double next = 1; @@ -95,7 +99,7 @@ inline double Walker::score (unsigned i) { Clause *Internal::walk_pick_clause (Walker &walker) { require_mode (WALK); - assert (!walker.broken.empty ()); + CADICAL_assert (!walker.broken.empty ()); int64_t size = walker.broken.size (); if (size > INT_MAX) size = INT_MAX; @@ -113,12 +117,12 @@ Clause *Internal::walk_pick_clause (Walker &walker) { unsigned Internal::walk_break_value (int lit) { require_mode (WALK); - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); unsigned res = 0; // The computed break-count of 'lit'. for (auto &w : watches (lit)) { - assert (w.blit != lit); + CADICAL_assert (w.blit != lit); if (val (w.blit) > 0) continue; if (w.binary ()) { @@ -127,7 +131,7 @@ unsigned Internal::walk_break_value (int lit) { } Clause *c = w.clause; - assert (lit == c->literals[0]); + CADICAL_assert (lit == c->literals[0]); // Now try to find a second satisfied literal starting at 'literals[1]' // shifting all the traversed literals to right by one position in order @@ -186,16 +190,16 @@ unsigned Internal::walk_break_value (int lit) { int Internal::walk_pick_lit (Walker &walker, Clause *c) { LOG ("picking literal by break-count"); - assert (walker.scores.empty ()); + CADICAL_assert (walker.scores.empty ()); double sum = 0; int64_t propagations = 0; for (const auto lit : *c) { - assert (active (lit)); + CADICAL_assert (active (lit)); if (var (lit).level == 1) { LOG ("skipping assumption %d for scoring", -lit); continue; } - assert (active (lit)); + CADICAL_assert (active (lit)); propagations++; unsigned tmp = walk_break_value (-lit); double score = walker.score (tmp); @@ -204,10 +208,10 @@ int Internal::walk_pick_lit (Walker &walker, Clause *c) { sum += score; } LOG ("scored %zd literals", walker.scores.size ()); - assert (!walker.scores.empty ()); + CADICAL_assert (!walker.scores.empty ()); walker.propagations += propagations; stats.propagations.walk += propagations; - assert (walker.scores.size () <= (size_t) c->size); + CADICAL_assert (walker.scores.size () <= (size_t) c->size); const double lim = sum * walker.random.generate_double (); LOG ("score sum %g limit %g", sum, lim); const auto end = c->end (); @@ -215,7 +219,7 @@ int Internal::walk_pick_lit (Walker &walker, Clause *c) { auto j = walker.scores.begin (); int res; for (;;) { - assert (i != end); + CADICAL_assert (i != end); res = *i++; if (var (res).level > 1) break; @@ -241,14 +245,14 @@ void Internal::walk_flip_lit (Walker &walker, int lit) { require_mode (WALK); LOG ("flipping assign %d", lit); - assert (val (lit) < 0); + CADICAL_assert (val (lit) < 0); // First flip the literal value. // const int tmp = sign (lit); const int idx = abs (lit); set_val (idx, tmp); - assert (val (lit) > 0); + CADICAL_assert (val (lit) > 0); // Then remove 'c' and all other now satisfied (made) clauses. { @@ -288,12 +292,12 @@ void Internal::walk_flip_lit (Walker &walker, int lit) { const int size = d->size; for (int i = 0; i < size; i++) { const int other = literals[i]; - assert (active (other)); + CADICAL_assert (active (other)); literals[i] = prev; prev = other; if (other == lit) break; - assert (val (other) < 0); + CADICAL_assert (val (other) < 0); } // If 'lit' is in 'd' then move it to the front to watch it. @@ -348,11 +352,11 @@ void Internal::walk_flip_lit (Walker &walker, int lit) { Clause *d = w.clause; LOG (d, "unwatch %d in", -lit); int *literals = d->literals, replacement = 0, prev = -lit; - assert (literals[0] == -lit); + CADICAL_assert (literals[0] == -lit); const int size = d->size; for (int i = 1; i < size; i++) { const int other = literals[i]; - assert (active (other)); + CADICAL_assert (active (other)); literals[i] = prev; // shift all to right prev = other; const signed char tmp = val (other); @@ -364,7 +368,7 @@ void Internal::walk_flip_lit (Walker &walker, int lit) { if (replacement) { literals[1] = -lit; literals[0] = replacement; - assert (-lit != replacement); + CADICAL_assert (-lit != replacement); watch_literal (replacement, -lit, d); } else { for (int i = size - 1; i > 0; i--) { // undo shift @@ -372,7 +376,7 @@ void Internal::walk_flip_lit (Walker &walker, int lit) { literals[i] = prev; prev = other; } - assert (literals[0] == -lit); + CADICAL_assert (literals[0] == -lit); LOG (d, "broken"); walker.broken.push_back (d); #ifdef LOGGING @@ -422,11 +426,11 @@ int Internal::walk_round (int64_t limit, bool prev) { if (last.collect.fixed < stats.all.fixed) garbage_collection (); -#ifndef QUIET +#ifndef CADICAL_QUIET // We want to see more messages during initial local search. // if (localsearching) { - assert (!force_phase_messages); + CADICAL_assert (!force_phase_messages); force_phase_messages = true; } #endif @@ -483,7 +487,7 @@ int Internal::walk_round (int64_t limit, bool prev) { const int idx = abs (lit); LOG ("initial assign %d to assumption phase", tmp < 0 ? -idx : idx); set_val (idx, tmp); - assert (level == 1); + CADICAL_assert (level == 1); var (idx).level = 1; } if (!failed) @@ -500,7 +504,7 @@ int Internal::walk_round (int64_t limit, bool prev) { continue; } if (vals[idx]) { - assert (var (idx).level == 1); + CADICAL_assert (var (idx).level == 1); LOG ("skipping assumed variable %d", idx); continue; } @@ -509,9 +513,9 @@ int Internal::walk_round (int64_t limit, bool prev) { tmp = phases.prev[idx]; if (!tmp) tmp = sign (decide_phase (idx, true)); - assert (tmp == 1 || tmp == -1); + CADICAL_assert (tmp == 1 || tmp == -1); set_val (idx, tmp); - assert (level == 2); + CADICAL_assert (level == 2); var (idx).level = 2; LOG ("initial assign %d to decision phase", tmp < 0 ? -idx : idx); } @@ -542,7 +546,7 @@ int Internal::walk_round (int64_t limit, bool prev) { // for (int i = 0; satisfied < 2 && i < size; i++) { const int lit = lits[i]; - assert (active (lit)); // Due to garbage collection. + CADICAL_assert (active (lit)); // Due to garbage collection. if (val (lit) > 0) { swap (lits[satisfied], lits[i]); if (!satisfied++) @@ -566,7 +570,7 @@ int Internal::walk_round (int64_t limit, bool prev) { watched++; #endif } else { - assert (satisfiable); // at least one non-assumed variable ... + CADICAL_assert (satisfiable); // at least one non-assumed variable ... LOG (c, "broken"); walker.broken.push_back (c); } @@ -599,12 +603,12 @@ int Internal::walk_round (int64_t limit, bool prev) { walk_save_minimum (walker); int64_t minimum = broken; -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t flips = 0; #endif while (!terminated_asynchronously () && !walker.broken.empty () && walker.propagations < walker.limit) { -#ifndef QUIET +#ifndef CADICAL_QUIET flips++; #endif stats.walk.flips++; @@ -673,15 +677,15 @@ int Internal::walk_round (int64_t limit, bool prev) { if (active (idx)) set_val (idx, 0); - assert (level == 2); + CADICAL_assert (level == 2); level = 0; clear_watches (); connect_watches (); -#ifndef QUIET +#ifndef CADICAL_QUIET if (localsearching) { - assert (force_phase_messages); + CADICAL_assert (force_phase_messages); force_phase_messages = false; } #endif @@ -702,3 +706,5 @@ void Internal::walk () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/watch.cpp b/src/sat/cadical/cadical_watch.cpp similarity index 90% rename from src/sat/cadical/watch.cpp rename to src/sat/cadical/cadical_watch.cpp index 2456866d3..84c51ef2c 100644 --- a/src/sat/cadical/watch.cpp +++ b/src/sat/cadical/cadical_watch.cpp @@ -1,9 +1,13 @@ +#include "global.h" + #include "internal.hpp" +ABC_NAMESPACE_IMPL_START + namespace CaDiCaL { void Internal::init_watches () { - assert (wtab.empty ()); + CADICAL_assert (wtab.empty ()); if (wtab.size () < 2 * vsize) wtab.resize (2 * vsize, Watches ()); LOG ("initialized watcher tables"); @@ -15,7 +19,7 @@ void Internal::clear_watches () { } void Internal::reset_watches () { - assert (!wtab.empty ()); + CADICAL_assert (!wtab.empty ()); erase_vector (wtab); LOG ("reset watcher tables"); } @@ -25,7 +29,7 @@ void Internal::reset_watches () { void Internal::connect_watches (bool irredundant_only) { START (connect); - assert (watching ()); + CADICAL_assert (watching ()); LOG ("watching all %sclauses", irredundant_only ? "irredundant " : ""); @@ -81,7 +85,7 @@ void Internal::connect_watches (bool irredundant_only) { void Internal::connect_binary_watches () { START (connect); - assert (watching ()); + CADICAL_assert (watching ()); LOG ("watching binary clauses"); @@ -97,7 +101,7 @@ void Internal::connect_binary_watches () { } void Internal::sort_watches () { - assert (watching ()); + CADICAL_assert (watching ()); LOG ("sorting watches"); Watches saved; for (auto lit : lits) { @@ -107,7 +111,7 @@ void Internal::sort_watches () { watch_iterator j = ws.begin (); const_watch_iterator i; - assert (saved.empty ()); + CADICAL_assert (saved.empty ()); for (i = j; i != end; i++) { const Watch w = *i; @@ -124,3 +128,5 @@ void Internal::sort_watches () { } } // namespace CaDiCaL + +ABC_NAMESPACE_IMPL_END diff --git a/src/sat/cadical/ccadical.h b/src/sat/cadical/ccadical.h index fb99e3b42..ec5292a79 100644 --- a/src/sat/cadical/ccadical.h +++ b/src/sat/cadical/ccadical.h @@ -1,10 +1,10 @@ #ifndef _ccadical_h_INCLUDED #define _ccadical_h_INCLUDED +#include "global.h" + /*------------------------------------------------------------------------*/ -#ifdef __cplusplus -extern "C" { -#endif +ABC_NAMESPACE_HEADER_START /*------------------------------------------------------------------------*/ #include @@ -54,6 +54,11 @@ int ccadical_simplify (CCaDiCaL *); int ccadical_vars (CCaDiCaL *); int ccadical_reserve_difference (CCaDiCaL *, int number_of_vars); +// Extra + +void ccadical_reserve(CCaDiCaL *, int min_max_var); +int ccadical_is_inconsistent(CCaDiCaL *); + /*------------------------------------------------------------------------*/ // Support legacy names used before moving to more IPASIR conforming names. @@ -63,9 +68,7 @@ int ccadical_reserve_difference (CCaDiCaL *, int number_of_vars); #define ccadical_deref ccadical_val /*------------------------------------------------------------------------*/ -#ifdef __cplusplus -} -#endif +ABC_NAMESPACE_HEADER_END /*------------------------------------------------------------------------*/ #endif diff --git a/src/sat/cadical/checker.hpp b/src/sat/cadical/checker.hpp index fba087095..8c351e326 100644 --- a/src/sat/cadical/checker.hpp +++ b/src/sat/cadical/checker.hpp @@ -1,10 +1,14 @@ #ifndef _checker_hpp_INCLUDED #define _checker_hpp_INCLUDED +#include "global.h" + #include "tracer.hpp" // Alphabetically after 'checker'. #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -169,4 +173,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/clause.hpp b/src/sat/cadical/clause.hpp index eed019cde..e5d0290f6 100644 --- a/src/sat/cadical/clause.hpp +++ b/src/sat/cadical/clause.hpp @@ -1,11 +1,15 @@ #ifndef _clause_hpp_INCLUDED #define _clause_hpp_INCLUDED +#include "global.h" + #include "util.hpp" #include #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -138,7 +142,7 @@ struct Clause { // of a clause is 8 bytes anyhow, we just allocate 8 byte aligned memory // all the time (even if allocated outside of the arena). // - assert (size > 1); + CADICAL_assert (size > 1); const size_t header_bytes = sizeof (Clause); const size_t actual_literal_bytes = size * sizeof (int); size_t combined_bytes = header_bytes + actual_literal_bytes; @@ -185,4 +189,6 @@ struct clause_lit_less_than { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/config.hpp b/src/sat/cadical/config.hpp index 5510ce0eb..d49b05c84 100644 --- a/src/sat/cadical/config.hpp +++ b/src/sat/cadical/config.hpp @@ -1,6 +1,10 @@ #ifndef _config_hpp_INCLUDED #define _config_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { class Options; @@ -17,4 +21,6 @@ struct Config { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/congruence.hpp b/src/sat/cadical/congruence.hpp index 5686238d8..e0acf9b00 100644 --- a/src/sat/cadical/congruence.hpp +++ b/src/sat/cadical/congruence.hpp @@ -1,6 +1,8 @@ #ifndef _congruenc_hpp_INCLUDED #define _congruenc_hpp_INCLUDED +#include "global.h" + #include #include #include @@ -17,6 +19,8 @@ #include "util.hpp" #include "watch.hpp" +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { typedef int64_t LRAT_ID; @@ -91,15 +95,15 @@ struct lit_equivalence { Clause *first_clause; Clause *second_clause; void check_invariant () { - assert (second_clause); - assert (first_clause); - assert (std::find (begin (*first_clause), end (*first_clause), first) != + CADICAL_assert (second_clause); + CADICAL_assert (first_clause); + CADICAL_assert (std::find (begin (*first_clause), end (*first_clause), first) != end (*first_clause)); - assert (std::find (begin (*second_clause), end (*second_clause), + CADICAL_assert (std::find (begin (*second_clause), end (*second_clause), second) != end (*second_clause)); - assert (std::find (begin (*first_clause), end (*first_clause), + CADICAL_assert (std::find (begin (*first_clause), end (*first_clause), -second) != end (*first_clause)); - assert (std::find (begin (*second_clause), end (*second_clause), + CADICAL_assert (std::find (begin (*second_clause), end (*second_clause), -first) != end (*second_clause)); } lit_equivalence (int f, Clause *f_id, int s, Clause *s_id) @@ -711,4 +715,6 @@ struct Closure { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/contract.hpp b/src/sat/cadical/contract.hpp index 3e98867c0..4a3ba066a 100644 --- a/src/sat/cadical/contract.hpp +++ b/src/sat/cadical/contract.hpp @@ -1,8 +1,12 @@ #ifndef _contract_hpp_INCLUDED #define _contract_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + /*------------------------------------------------------------------------*/ -#ifndef NCONTRACTS +#ifndef CADICAL_NCONTRACTS /*------------------------------------------------------------------------*/ // If the user violates API contracts while calling functions declared in @@ -104,7 +108,7 @@ void require_solver_pointer_to_be_non_zero (const void *ptr, } while (0) /*------------------------------------------------------------------------*/ -#else // NCONTRACTS +#else // CADICAL_NCONTRACTS /*------------------------------------------------------------------------*/ #define REQUIRE(...) \ @@ -133,4 +137,6 @@ void require_solver_pointer_to_be_non_zero (const void *ptr, #endif /*------------------------------------------------------------------------*/ +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/cover.hpp b/src/sat/cadical/cover.hpp index 67e1bea77..47e6f1b82 100644 --- a/src/sat/cadical/cover.hpp +++ b/src/sat/cadical/cover.hpp @@ -1,6 +1,8 @@ #ifndef _cover_hpp_INCLUDED #define _cover_hpp_INCLUDED +#include "global.h" + /*------------------------------------------------------------------------*/ // This header only provides the 'COVER' macro for testing. It is unrelated @@ -10,8 +12,8 @@ /*------------------------------------------------------------------------*/ -// Coverage goal, used similar to 'assert' (but with flipped condition) and -// also included even if 'NDEBUG' is defined (in optimizing compilation). +// Coverage goal, used similar to 'CADICAL_assert' (but with flipped condition) and +// also included even if 'CADICAL_NDEBUG' is defined (in optimizing compilation). // // This should in essence not be used in production code. // diff --git a/src/sat/cadical/decompose.hpp b/src/sat/cadical/decompose.hpp index d62b6ba4a..9c93e8448 100644 --- a/src/sat/cadical/decompose.hpp +++ b/src/sat/cadical/decompose.hpp @@ -1,6 +1,10 @@ #ifndef _decompose_hpp_INCLUDED #define _decompose_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // This implements Tarjan's algorithm for decomposing the binary implication @@ -20,4 +24,6 @@ struct DFS { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/delay.hpp b/src/sat/cadical/delay.hpp index e1e10ec82..9ee9a9234 100644 --- a/src/sat/cadical/delay.hpp +++ b/src/sat/cadical/delay.hpp @@ -1,9 +1,13 @@ #ifndef _delay_hpp_INCLUDED #define _delay_hpp_INCLUDED +#include "global.h" + #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Delay { unsigned count; @@ -35,4 +39,6 @@ struct Delay { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/drattracer.hpp b/src/sat/cadical/drattracer.hpp index 42e238838..8c2c023d5 100644 --- a/src/sat/cadical/drattracer.hpp +++ b/src/sat/cadical/drattracer.hpp @@ -1,8 +1,12 @@ #ifndef _drattracer_h_INCLUDED #define _drattracer_h_INCLUDED +#include "global.h" + #include "tracer.hpp" +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { class DratTracer : public FileTracer { @@ -10,7 +14,7 @@ class DratTracer : public FileTracer { Internal *internal; File *file; bool binary; -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t added, deleted; #endif void put_binary_zero (); @@ -40,7 +44,7 @@ public: void report_status (int, int64_t) override {} // skip -#ifndef QUIET +#ifndef CADICAL_QUIET void print_statistics (); #endif bool closed () override; @@ -50,4 +54,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/elim.hpp b/src/sat/cadical/elim.hpp index 3f5fb63ac..73e1bf547 100644 --- a/src/sat/cadical/elim.hpp +++ b/src/sat/cadical/elim.hpp @@ -1,8 +1,12 @@ #ifndef _elim_hpp_INCLUDED #define _elim_hpp_INCLUDED +#include "global.h" + #include "heap.hpp" // Alphabetically after 'elim.hpp'. +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Internal; @@ -19,7 +23,7 @@ struct proof_clause { int64_t id; vector literals; // for lrat - unsigned cid; // kitten id + unsigned cid; // cadical_kitten id bool learned; vector chain; }; @@ -50,4 +54,6 @@ struct Eliminator { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/ema.hpp b/src/sat/cadical/ema.hpp index 39eba390a..51eb5f8c1 100644 --- a/src/sat/cadical/ema.hpp +++ b/src/sat/cadical/ema.hpp @@ -1,8 +1,12 @@ #ifndef _ema_hpp_INCLUDED #define _ema_hpp_INCLUDED +#include "global.h" + #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Internal; @@ -36,7 +40,7 @@ struct EMA { updated (0), #endif value (0), biased (0), alpha (a), beta (1 - a), exp (!!beta) { - assert (beta >= 0); + CADICAL_assert (beta >= 0); } operator double () const { return value; } @@ -56,7 +60,7 @@ struct EMA { #define INIT_EMA(E, WINDOW) \ do { \ - assert ((WINDOW) >= 1); \ + CADICAL_assert ((WINDOW) >= 1); \ double ALPHA = 1.0 / (double) (WINDOW); \ E = EMA (ALPHA); \ LOG ("init " #E " EMA target alpha %g window %d", ALPHA, \ @@ -65,4 +69,6 @@ struct EMA { /*------------------------------------------------------------------------*/ +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/external.hpp b/src/sat/cadical/external.hpp index 25e21affb..7618fb936 100644 --- a/src/sat/cadical/external.hpp +++ b/src/sat/cadical/external.hpp @@ -1,6 +1,8 @@ #ifndef _external_hpp_INCLUDED #define _external_hpp_INCLUDED +#include "global.h" + /*------------------------------------------------------------------------*/ #include "range.hpp" @@ -9,6 +11,8 @@ /*------------------------------------------------------------------------*/ +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { using namespace std; @@ -146,26 +150,26 @@ struct External { /*==== end of state ====================================================*/ - // These two just factor out common sanity (assertion) checking code. + // These two just factor out common sanity (CADICAL_assertion) checking code. inline int vidx (int elit) const { - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); int res = abs (elit); - assert (res <= max_var); + CADICAL_assert (res <= max_var); return res; } inline int vlit (int elit) const { - assert (elit); - assert (elit != INT_MIN); - assert (abs (elit) <= max_var); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); + CADICAL_assert (abs (elit) <= max_var); return elit; } inline bool is_valid_input (int elit) { - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); int eidx = abs (elit); return eidx > max_var || !ervars[eidx]; } @@ -202,10 +206,10 @@ struct External { // Marking external literals. unsigned elit2ulit (int elit) const { - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); const int idx = abs (elit) - 1; - assert (idx <= max_var); + CADICAL_assert (idx <= max_var); return 2u * idx + (elit < 0); } @@ -251,8 +255,8 @@ struct External { void melt (int elit); bool frozen (int elit) { - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); int eidx = abs (elit); if (eidx > max_var) return false; @@ -319,7 +323,7 @@ struct External { // a negative value into a positive one (happened in 'extend'). // inline int ival (int elit) const { - assert (elit != INT_MIN); + CADICAL_assert (elit != INT_MIN); int eidx = abs (elit); bool val = false; if (eidx <= max_var && (size_t) eidx < vals.size ()) @@ -440,8 +444,8 @@ struct External { // already). // inline int sol (int elit) const { - assert (solution); - assert (elit != INT_MIN); + CADICAL_assert (solution); + CADICAL_assert (elit != INT_MIN); int eidx = abs (elit); if (eidx > max_var) return 0; @@ -458,4 +462,6 @@ struct External { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/factor.hpp b/src/sat/cadical/factor.hpp index ac01d35c5..3aa5ea6c6 100644 --- a/src/sat/cadical/factor.hpp +++ b/src/sat/cadical/factor.hpp @@ -1,9 +1,13 @@ #ifndef _factor_hpp_INCLUDED #define _factor_hpp_INCLUDED +#include "global.h" + #include "clause.hpp" #include "heap.hpp" +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Internal; @@ -51,4 +55,6 @@ struct Factoring { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/file.hpp b/src/sat/cadical/file.hpp index a42b31bbb..860e9613a 100644 --- a/src/sat/cadical/file.hpp +++ b/src/sat/cadical/file.hpp @@ -1,17 +1,23 @@ #ifndef _file_hpp_INCLUDED #define _file_hpp_INCLUDED +#include "global.h" + #include #include #include #include #include -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG #include #endif /*------------------------------------------------------------------------*/ +#ifdef WIN32 +#define cadical_putc_unlocked putc +#define cadical_getc_unlocked getc +#else #ifndef NUNLOCKED #define cadical_putc_unlocked putc_unlocked #define cadical_getc_unlocked getc_unlocked @@ -19,8 +25,11 @@ #define cadical_putc_unlocked putc #define cadical_getc_unlocked getc #endif +#endif /*------------------------------------------------------------------------*/ +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // Wraps a 'C' file 'FILE' with name and supports zipped reading and writing @@ -33,7 +42,7 @@ struct Internal; class File { Internal *internal; -#if !defined(QUIET) || !defined(NDEBUG) +#if !defined(CADICAL_QUIET) || !defined(CADICAL_NDEBUG) bool writing; #endif @@ -57,7 +66,7 @@ class File { const char *mode); static FILE *read_pipe (Internal *, const char *fmt, const int *sig, const char *path); -#ifndef __WIN32 +#ifndef WIN32 static FILE *write_pipe (Internal *, const char *fmt, const char *path, int &child_pid); #endif @@ -95,7 +104,7 @@ public: // threads, which on the other hand currently is impossible. int get () { - assert (!writing); + CADICAL_assert (!writing); int res = cadical_getc_unlocked (file); if (res == '\n') _lineno++; @@ -105,7 +114,7 @@ public: } bool put (char ch) { - assert (writing); + CADICAL_assert (writing); if (cadical_putc_unlocked (ch, file) == EOF) return false; _bytes++; @@ -115,7 +124,7 @@ public: bool endl () { return put ('\n'); } bool put (unsigned char ch) { - assert (writing); + CADICAL_assert (writing); if (cadical_putc_unlocked (ch, file) == EOF) return false; _bytes++; @@ -130,20 +139,20 @@ public: } bool put (int lit) { - assert (writing); + CADICAL_assert (writing); if (!lit) return put ('0'); else if (lit == -2147483648) { - assert (lit == INT_MIN); + CADICAL_assert (lit == INT_MIN); return put ("-2147483648"); } else { char buffer[11]; int i = sizeof buffer; buffer[--i] = 0; - assert (lit != INT_MIN); + CADICAL_assert (lit != INT_MIN); unsigned idx = abs (lit); while (idx) { - assert (i > 0); + CADICAL_assert (i > 0); buffer[--i] = '0' + idx % 10; idx /= 10; } @@ -154,20 +163,20 @@ public: } bool put (int64_t l) { - assert (writing); + CADICAL_assert (writing); if (!l) return put ('0'); else if (l == INT64_MIN) { - assert (sizeof l == 8); + CADICAL_assert (sizeof l == 8); return put ("-9223372036854775808"); } else { char buffer[21]; int i = sizeof buffer; buffer[--i] = 0; - assert (l != INT64_MIN); + CADICAL_assert (l != INT64_MIN); uint64_t k = l < 0 ? -l : l; while (k) { - assert (i > 0); + CADICAL_assert (i > 0); buffer[--i] = '0' + k % 10; k /= 10; } @@ -178,7 +187,7 @@ public: } bool put (uint64_t l) { - assert (writing); + CADICAL_assert (writing); if (!l) return put ('0'); else { @@ -186,7 +195,7 @@ public: int i = sizeof buffer; buffer[--i] = 0; while (l) { - assert (i > 0); + CADICAL_assert (i > 0); buffer[--i] = '0' + l % 10; l /= 10; } @@ -207,4 +216,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/flags.hpp b/src/sat/cadical/flags.hpp index d5ba3e2a4..995438c7a 100644 --- a/src/sat/cadical/flags.hpp +++ b/src/sat/cadical/flags.hpp @@ -1,6 +1,10 @@ #ifndef _flags_hpp_INCLUDED #define _flags_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Flags { // Variable flags. @@ -82,4 +86,6 @@ struct Flags { // Variable flags. } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/format.hpp b/src/sat/cadical/format.hpp index 8ad892cf4..cf229e760 100644 --- a/src/sat/cadical/format.hpp +++ b/src/sat/cadical/format.hpp @@ -1,9 +1,13 @@ #ifndef _format_hpp_INCLUDED #define _format_hpp_INCLUDED +#include "global.h" + #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // This class provides a 'printf' style formatting utility. @@ -33,4 +37,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/frattracer.hpp b/src/sat/cadical/frattracer.hpp index f9a1cccf3..cadfa3721 100644 --- a/src/sat/cadical/frattracer.hpp +++ b/src/sat/cadical/frattracer.hpp @@ -1,6 +1,10 @@ #ifndef _frattracer_h_INCLUDED #define _frattracer_h_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { class FratTracer : public FileTracer { @@ -10,7 +14,7 @@ class FratTracer : public FileTracer { bool binary; bool with_antecedents; -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t added, deleted; int64_t finalized, original; #endif @@ -49,7 +53,7 @@ public: void report_status (int, int64_t) override {} // skip -#ifndef QUIET +#ifndef CADICAL_QUIET void print_statistics (); #endif bool closed () override; @@ -59,4 +63,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/global.h b/src/sat/cadical/global.h new file mode 100644 index 000000000..916246cab --- /dev/null +++ b/src/sat/cadical/global.h @@ -0,0 +1,21 @@ +#ifndef ABC_SAT_CADICAL_GLOBAL_HPP_ +#define ABC_SAT_CADICAL_GLOBAL_HPP_ + +// comment out next line to enable cadical debug mode +#define CADICAL_NDEBUG + +#define CADICAL_NBUILD +#define CADICAL_QUIET +#define CADICAL_NCONTRACTS +#define CADICAL_NTRACING +#define CADICAL_NCLOSEFROM + +#ifdef CADICAL_NDEBUG +#define CADICAL_assert(ignore) ((void)0) +#else +#define CADICAL_assert(cond) assert(cond) +#endif + +#include "misc/util/abc_global.h" + +#endif diff --git a/src/sat/cadical/heap.hpp b/src/sat/cadical/heap.hpp index f3f0ab0e4..c42941413 100644 --- a/src/sat/cadical/heap.hpp +++ b/src/sat/cadical/heap.hpp @@ -1,10 +1,14 @@ #ifndef _heap_hpp_INCLUDED #define _heap_hpp_INCLUDED +#include "global.h" + #include "util.hpp" // Alphabetically after 'heap.hpp'. #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { using namespace std; @@ -33,7 +37,7 @@ template class heap { if (e >= pos.size ()) pos.resize (1 + (size_t) e, invalid_heap_position); unsigned &res = pos[e]; - assert (res == invalid_heap_position || (size_t) res < array.size ()); + CADICAL_assert (res == invalid_heap_position || (size_t) res < array.size ()); return res; } @@ -46,17 +50,17 @@ template class heap { } unsigned parent (unsigned e) { - assert (has_parent (e)); + CADICAL_assert (has_parent (e)); return array[(index (e) - 1) / 2]; } unsigned left (unsigned e) { - assert (has_left (e)); + CADICAL_assert (has_left (e)); return array[2 * index (e) + 1]; } unsigned right (unsigned e) { - assert (has_right (e)); + CADICAL_assert (has_right (e)); return array[2 * index (e) + 2]; } @@ -100,21 +104,21 @@ template class heap { void check () { #if 0 // EXPENSIVE HEAP CHECKING IF ENABLED #warning "expensive checking in heap enabled" - assert (array.size () <= invalid_heap_position); + CADICAL_assert (array.size () <= invalid_heap_position); for (size_t i = 0; i < array.size (); i++) { size_t l = 2*i + 1, r = 2*i + 2; - if (l < array.size ()) assert (!less (array[i], array[l])); - if (r < array.size ()) assert (!less (array[i], array[r])); - assert (array[i] >= 0); + if (l < array.size ()) CADICAL_assert (!less (array[i], array[l])); + if (r < array.size ()) CADICAL_assert (!less (array[i], array[r])); + CADICAL_assert (array[i] >= 0); { - assert ((size_t) array[i] < pos.size ()); - assert (i == (size_t) pos[array[i]]); + CADICAL_assert ((size_t) array[i] < pos.size ()); + CADICAL_assert (i == (size_t) pos[array[i]]); } } for (size_t i = 0; i < pos.size (); i++) { if (pos[i] == invalid_heap_position) continue; - assert (pos[i] < array.size ()); - assert (array[pos[i]] == (unsigned) i); + CADICAL_assert (pos[i] < array.size ()); + CADICAL_assert (array[pos[i]] == (unsigned) i); } #endif } @@ -141,9 +145,9 @@ public: // Add a new (not contained) element 'e' to the heap. // void push_back (unsigned e) { - assert (!contains (e)); + CADICAL_assert (!contains (e)); size_t i = array.size (); - assert (i < (size_t) invalid_heap_position); + CADICAL_assert (i < (size_t) invalid_heap_position); array.push_back (e); index (e) = (unsigned) i; up (e); @@ -154,14 +158,14 @@ public: // Returns the maximum element in the heap. // unsigned front () const { - assert (!empty ()); + CADICAL_assert (!empty ()); return array[0]; } // Removes the maximum element in the heap. // unsigned pop_front () { - assert (!empty ()); + CADICAL_assert (!empty ()); unsigned res = array[0], last = array.back (); if (size () > 1) exchange (res, last); @@ -176,7 +180,7 @@ public: // Notify the heap, that evaluation of 'less' has changed for 'e'. // void update (unsigned e) { - assert (contains (e)); + CADICAL_assert (contains (e)); up (e); down (e); check (); @@ -209,4 +213,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/idruptracer.hpp b/src/sat/cadical/idruptracer.hpp index 0b43e7a9c..4ea478705 100644 --- a/src/sat/cadical/idruptracer.hpp +++ b/src/sat/cadical/idruptracer.hpp @@ -1,6 +1,10 @@ #ifndef _idruptracer_h_INCLUDED #define _idruptracer_h_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + class FileTracer; namespace CaDiCaL { @@ -46,7 +50,7 @@ class IdrupTracer : public FileTracer { bool find_and_delete (const int64_t); // find clause position in hash table -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t added, deleted, weakened, restore, original, solved; #endif @@ -97,7 +101,7 @@ public: // logging and file io void connect_internal (Internal *i) override; -#ifndef QUIET +#ifndef CADICAL_QUIET void print_statistics (); #endif bool closed () override; @@ -107,4 +111,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/instantiate.hpp b/src/sat/cadical/instantiate.hpp index fef7532e7..f6c8767dd 100644 --- a/src/sat/cadical/instantiate.hpp +++ b/src/sat/cadical/instantiate.hpp @@ -1,6 +1,10 @@ #ifndef _instantiate_hpp_INCLUDED #define _instantiate_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // We are trying to remove literals in clauses, which occur in few clauses @@ -42,4 +46,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/internal.hpp b/src/sat/cadical/internal.hpp index 8dfa2cd8d..d86479a52 100644 --- a/src/sat/cadical/internal.hpp +++ b/src/sat/cadical/internal.hpp @@ -1,6 +1,8 @@ #ifndef _internal_hpp_INCLUDED #define _internal_hpp_INCLUDED +#include "global.h" + /*------------------------------------------------------------------------*/ // Wrapped build specific headers which should go first. @@ -22,9 +24,11 @@ // Less common 'C' header. +#ifndef WIN32 extern "C" { #include } +#endif /*------------------------------------------------------------------------*/ @@ -104,11 +108,13 @@ extern "C" { #include "watch.hpp" // c headers -extern "C" { +//extern "C" { #include "kitten.h" -} +//} /*------------------------------------------------------------------------*/ +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { using namespace std; @@ -157,14 +163,14 @@ struct Internal { bool in_mode (Mode m) const { return (mode & m) != 0; } void set_mode (Mode m) { - assert (!(mode & m)); + CADICAL_assert (!(mode & m)); mode |= m; } void reset_mode (Mode m) { - assert (mode & m); + CADICAL_assert (mode & m); mode &= ~m; } - void require_mode (Mode m) const { assert (mode & m), (void) m; } + void require_mode (Mode m) const { CADICAL_assert (mode & m), (void) m; } /*----------------------------------------------------------------------*/ @@ -268,7 +274,7 @@ struct Internal { vector sweep_schedule; // remember sweep varibles to reschedule bool sweep_incomplete; // sweep - kitten *citten; + cadical_kitten *citten; size_t num_assigned; // check for satisfied @@ -294,7 +300,7 @@ struct Internal { Options opts; // run-time options Stats stats; // statistics -#ifndef QUIET +#ifndef CADICAL_QUIET Profiles profiles; // time profiles for various functions bool force_phase_messages; // force 'phase (...)' messages #endif @@ -355,15 +361,15 @@ struct Internal { int active () const { int res = stats.active; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG int tmp = max_var; tmp -= stats.unused; tmp -= stats.now.fixed; tmp -= stats.now.eliminated; tmp -= stats.now.substituted; tmp -= stats.now.pure; - assert (tmp >= 0); - assert (tmp == res); + CADICAL_assert (tmp >= 0); + CADICAL_assert (tmp == res); #endif return res; } @@ -388,10 +394,10 @@ struct Internal { // int vidx (int lit) const { int idx; - assert (lit); - assert (lit != INT_MIN); + CADICAL_assert (lit); + CADICAL_assert (lit != INT_MIN); idx = abs (lit); - assert (idx <= max_var); + CADICAL_assert (idx <= max_var); return idx; } @@ -404,9 +410,9 @@ struct Internal { } int u2i (unsigned u) { - assert (u > 1); + CADICAL_assert (u > 1); int res = u / 2; - assert (res <= max_var); + CADICAL_assert (res <= max_var); if (u & 1) res = -res; return res; @@ -414,7 +420,7 @@ struct Internal { int citten2lit (unsigned ulit) { int res = (ulit / 2) + 1; - assert (res <= max_var); + CADICAL_assert (res <= max_var); if (ulit & 1) res = -res; return res; @@ -426,18 +432,18 @@ struct Internal { } int64_t unit_id (int lit) const { - assert (lrat || frat); - assert (val (lit) > 0); + CADICAL_assert (lrat || frat); + CADICAL_assert (val (lit) > 0); const unsigned uidx = vlit (lit); int64_t id = unit_clauses_idx[uidx]; - assert (id); + CADICAL_assert (id); return id; } inline int64_t &unit_clauses (int uidx) { - assert (lrat || frat); - assert (uidx > 0); - assert ((size_t) uidx < unit_clauses_idx.size ()); + CADICAL_assert (lrat || frat); + CADICAL_assert (uidx > 0); + CADICAL_assert ((size_t) uidx < unit_clauses_idx.size ()); return unit_clauses_idx[uidx]; } @@ -476,14 +482,14 @@ struct Internal { return res; } void mark (int lit) { - assert (!marked (lit)); + CADICAL_assert (!marked (lit)); marks[vidx (lit)] = sign (lit); - assert (marked (lit) > 0); - assert (marked (-lit) < 0); + CADICAL_assert (marked (lit) > 0); + CADICAL_assert (marked (-lit) < 0); } void unmark (int lit) { marks[vidx (lit)] = 0; - assert (!marked (lit)); + CADICAL_assert (!marked (lit)); } // Use only bits 6 and 7 to store the sign or zero. The remaining @@ -498,24 +504,24 @@ struct Internal { void mark67 (int lit) { signed char &m = marks[vidx (lit)]; const signed char mask = 0x3f; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG const signed char bits = m & mask; #endif m = (m & mask) | (sign (lit) << 6); - assert (marked (lit) > 0); - assert (marked (-lit) < 0); - assert ((m & mask) == bits); - assert (marked67 (lit) > 0); - assert (marked67 (-lit) < 0); + CADICAL_assert (marked (lit) > 0); + CADICAL_assert (marked (-lit) < 0); + CADICAL_assert ((m & mask) == bits); + CADICAL_assert (marked67 (lit) > 0); + CADICAL_assert (marked67 (-lit) < 0); } void unmark67 (int lit) { signed char &m = marks[vidx (lit)]; const signed char mask = 0x3f; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG const signed bits = m & mask; #endif m &= mask; - assert ((m & mask) == bits); + CADICAL_assert ((m & mask) == bits); } void unmark (vector &lits) { @@ -528,53 +534,53 @@ struct Internal { // bit in 'condition' to mark variables in the conditional part. // bool getbit (int lit, int bit) const { - assert (0 <= bit), assert (bit < 6); + CADICAL_assert (0 <= bit), CADICAL_assert (bit < 6); return marks[vidx (lit)] & (1 << bit); } void setbit (int lit, int bit) { - assert (0 <= bit), assert (bit < 6); - assert (!getbit (lit, bit)); + CADICAL_assert (0 <= bit), CADICAL_assert (bit < 6); + CADICAL_assert (!getbit (lit, bit)); marks[vidx (lit)] |= (1 << bit); - assert (getbit (lit, bit)); + CADICAL_assert (getbit (lit, bit)); } void unsetbit (int lit, int bit) { - assert (0 <= bit), assert (bit < 6); - assert (getbit (lit, bit)); + CADICAL_assert (0 <= bit), CADICAL_assert (bit < 6); + CADICAL_assert (getbit (lit, bit)); marks[vidx (lit)] &= ~(1 << bit); - assert (!getbit (lit, bit)); + CADICAL_assert (!getbit (lit, bit)); } // Marking individual literals. // bool marked2 (int lit) const { unsigned res = marks[vidx (lit)]; - assert (res <= 3); + CADICAL_assert (res <= 3); unsigned bit = bign (lit); return (res & bit) != 0; } void mark2 (int lit) { marks[vidx (lit)] |= bign (lit); - assert (marked2 (lit)); + CADICAL_assert (marked2 (lit)); } // marks bits 1,2,3 and 4,5,6 depending on fact and sign of lit // bool getfact (int lit, int fact) const { - assert (fact == 1 || fact == 2 || fact == 4); + CADICAL_assert (fact == 1 || fact == 2 || fact == 4); int res = marks[vidx (lit)]; if (lit < 0) { res >>= 3; } else { res &= 7; } - // assert (!res || res == 1 || res == 2 || res == 4); + // CADICAL_assert (!res || res == 1 || res == 2 || res == 4); return res & fact; } void markfact (int lit, int fact) { - assert (fact == 1 || fact == 2 || fact == 4); - assert (!getfact (lit, fact)); -#ifndef NDEBUG + CADICAL_assert (fact == 1 || fact == 2 || fact == 4); + CADICAL_assert (!getfact (lit, fact)); +#ifndef CADICAL_NDEBUG int before = getfact (-lit, fact); #endif int res = marks[vidx (lit)]; @@ -584,15 +590,15 @@ struct Internal { res |= fact; } marks[vidx (lit)] = res; - assert (getfact (lit, fact)); -#ifndef NDEBUG - assert (getfact (-lit, fact) == before); + CADICAL_assert (getfact (lit, fact)); +#ifndef CADICAL_NDEBUG + CADICAL_assert (getfact (-lit, fact) == before); #endif } void unmarkfact (int lit, int fact) { - assert (fact == 1 || fact == 2 || fact == 4); - assert (getfact (lit, fact)); + CADICAL_assert (fact == 1 || fact == 2 || fact == 4); + CADICAL_assert (getfact (lit, fact)); int res = marks[vidx (lit)]; if (lit < 0) { res &= ~(fact << 3); @@ -600,7 +606,7 @@ struct Internal { res &= ~fact; } marks[vidx (lit)] = res; - assert (!getfact (lit, fact)); + CADICAL_assert (!getfact (lit, fact)); } // Marking and unmarking of all literals in a clause. @@ -615,7 +621,7 @@ struct Internal { // Inlined here, since it occurs in the tight inner loop of 'propagate'. // inline void watch_literal (int lit, int blit, Clause *c) { - assert (lit != blit); + CADICAL_assert (lit != blit); Watches &ws = watches (lit); ws.push_back (Watch (blit, c)); LOG (c, "watch %d blit %d in", lit, blit); @@ -644,8 +650,8 @@ struct Internal { // inlined here since it occurs in several inner loops. // inline void update_queue_unassigned (int idx) { - assert (0 < idx); - assert (idx <= max_var); + CADICAL_assert (0 < idx); + CADICAL_assert (idx <= max_var); queue.unassigned = idx; queue.bumped = btab[idx]; LOG ("queue unassigned now %d bumped %" PRId64 "", idx, btab[idx]); @@ -815,7 +821,7 @@ struct Internal { void connect_propagator (); void mark_garbage_external_forgettable (int64_t id); bool is_external_forgettable (int64_t id); -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG bool get_merged_literals (std::vector &); void get_all_fixed_literals (std::vector &); #endif @@ -1113,7 +1119,7 @@ struct Internal { void mark_decomposed (int lit) { Flags &f = flags (lit); const unsigned bit = bign (lit); - assert ((f.marked_signed & bit) == 0); + CADICAL_assert ((f.marked_signed & bit) == 0); sign_marked.push_back (lit); f.marked_signed |= bit; } @@ -1166,7 +1172,7 @@ struct Internal { void find_gate_clauses (Eliminator &, int pivot); void unmark_gate_clauses (Eliminator &); - // mine definitions for kitten in 'definition.cpp' + // mine definitions for cadical_kitten in 'definition.cpp' // void find_definition (Eliminator &, int); void init_citten (); @@ -1204,8 +1210,8 @@ struct Internal { // sweeping in 'sweep.cpp' int sweep_solve (); - void sweep_set_kitten_ticks_limit (Sweeper &sweeper); - bool kitten_ticks_limit_hit (Sweeper &sweeper, const char *when); + void sweep_set_cadical_kitten_ticks_limit (Sweeper &sweeper); + bool cadical_kitten_ticks_limit_hit (Sweeper &sweeper, const char *when); void init_sweeper (Sweeper &sweeper); void release_sweeper (Sweeper &sweeper); void clear_sweeper (Sweeper &sweeper); @@ -1493,7 +1499,7 @@ struct Internal { bool terminating_asked (); -#ifndef QUIET +#ifndef CADICAL_QUIET // Built in profiling in 'profile.cpp' (see also 'profile.hpp'). // void start_profiling (Profile &p, double); @@ -1510,9 +1516,9 @@ struct Internal { // negative. We also avoid taking the absolute value. // signed char val (int lit) const { - assert (-max_var <= lit); - assert (lit); - assert (lit <= max_var); + CADICAL_assert (-max_var <= lit); + CADICAL_assert (lit); + CADICAL_assert (lit <= max_var); return vals[lit]; } @@ -1520,11 +1526,11 @@ struct Internal { // setter function for setting and resetting the value of a literal. // void set_val (int lit, signed char val) { - assert (-1 <= val); - assert (val <= 1); - assert (-max_var <= lit); - assert (lit); - assert (lit <= max_var); + CADICAL_assert (-1 <= val); + CADICAL_assert (val <= 1); + CADICAL_assert (-max_var <= lit); + CADICAL_assert (lit); + CADICAL_assert (lit <= max_var); vals[lit] = val; vals[-lit] = -val; } @@ -1534,9 +1540,9 @@ struct Internal { // of the variable anyhow. // int fixed (int lit) { - assert (-max_var <= lit); - assert (lit); - assert (lit <= max_var); + CADICAL_assert (-max_var <= lit); + CADICAL_assert (lit); + CADICAL_assert (lit <= max_var); const int idx = vidx (lit); int res = vals[idx]; if (res && vtab[idx].level) @@ -1549,10 +1555,10 @@ struct Internal { // Map back an internal literal to an external. // int externalize (int lit) { - assert (lit != INT_MIN); + CADICAL_assert (lit != INT_MIN); const int idx = abs (lit); - assert (idx); - assert (idx <= max_var); + CADICAL_assert (idx); + CADICAL_assert (idx <= max_var); int res = i2e[idx]; if (lit < 0) res = -res; @@ -1663,12 +1669,12 @@ struct Internal { /*----------------------------------------------------------------------*/ -#ifndef QUIET +#ifndef CADICAL_QUIET void print_prefix (); // Non-verbose messages and warnings, i.e., always printed unless 'quiet' - // is set, which disables messages at run-time, or even 'QUIET' is defined + // is set, which disables messages at run-time, or even 'CADICAL_QUIET' is defined // through the configuration option './configure --quiet', which disables // such messages completely at compile-time. // @@ -1740,10 +1746,10 @@ inline bool score_smaller::operator() (unsigned a, unsigned b) { // Avoid computing twice 'abs' in 'score ()'. // - assert (1 <= a); - assert (a <= (unsigned) internal->max_var); - assert (1 <= b); - assert (b <= (unsigned) internal->max_var); + CADICAL_assert (1 <= a); + CADICAL_assert (a <= (unsigned) internal->max_var); + CADICAL_assert (1 <= b); + CADICAL_assert (b <= (unsigned) internal->max_var); double s = internal->stab[a]; double t = internal->stab[b]; @@ -1760,8 +1766,8 @@ inline bool score_smaller::operator() (unsigned a, unsigned b) { // Implemented here for keeping it all inline (requires Internal::fixed). inline int External::fixed (int elit) const { - assert (elit); - assert (elit != INT_MIN); + CADICAL_assert (elit); + CADICAL_assert (elit != INT_MIN); int eidx = abs (elit); if (eidx > max_var) return 0; @@ -1798,7 +1804,7 @@ inline bool Internal::terminated_asynchronously (int factor) { // to this function do not check this again. // if (lim.terminate.forced) { - assert (lim.terminate.forced > 0); + CADICAL_assert (lim.terminate.forced > 0); if (lim.terminate.forced-- == 1) { LOG ("internally forcing termination"); termination_forced = true; @@ -1819,8 +1825,8 @@ inline bool Internal::terminated_asynchronously (int factor) { // flag leads to the first test above to succeed in subsequent calls. // if (external->terminator && !lim.terminate.check--) { - assert (factor > 0); - assert (INT_MAX / factor > opts.terminateint); + CADICAL_assert (factor > 0); + CADICAL_assert (INT_MAX / factor > opts.terminateint); lim.terminate.check = factor * opts.terminateint; if (external->terminator->terminate ()) { termination_forced = true; // Cache it. @@ -1835,8 +1841,8 @@ inline bool Internal::terminated_asynchronously (int factor) { /*------------------------------------------------------------------------*/ inline bool Internal::search_limits_hit () { - assert (!preprocessing); - assert (!localsearching); + CADICAL_assert (!preprocessing); + CADICAL_assert (!localsearching); if (lim.conflicts >= 0 && stats.conflicts >= lim.conflicts) { LOG ("conflict limit %" PRId64 " reached", lim.conflicts); @@ -1855,4 +1861,6 @@ inline bool Internal::search_limits_hit () { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/inttypes.hpp b/src/sat/cadical/inttypes.hpp index b1b95872e..2f7cb74a4 100644 --- a/src/sat/cadical/inttypes.hpp +++ b/src/sat/cadical/inttypes.hpp @@ -1,6 +1,8 @@ #ifndef _inttypes_h_INCLUDED #define _inttypes_h_INCLUDED +#include "global.h" + // This is an essence a wrapper around '' respectively // 'inttypes.h' in order to please the 'MinGW' cross-compiler (we are using // 'i686-w64-mingw32-gcc') to produce correct 'printf' style formatting for diff --git a/src/sat/cadical/ipasir.h b/src/sat/cadical/ipasir.h index 0da79fe14..ccb610130 100644 --- a/src/sat/cadical/ipasir.h +++ b/src/sat/cadical/ipasir.h @@ -1,10 +1,10 @@ #ifndef _ipasir_h_INCLUDED #define _ipasir_h_INCLUDED +#include "global.h" + /*------------------------------------------------------------------------*/ -#ifdef __cplusplus -extern "C" { -#endif +ABC_NAMESPACE_HEADER_START /*------------------------------------------------------------------------*/ // Here are the declarations for the actual IPASIR functions, which is the @@ -29,9 +29,7 @@ void ipasir_set_learn (void *solver, void *state, int max_length, void (*learn) (void *state, int *clause)); /*------------------------------------------------------------------------*/ -#ifdef __cplusplus -} -#endif +ABC_NAMESPACE_HEADER_END /*------------------------------------------------------------------------*/ #endif diff --git a/src/sat/cadical/kitten.c b/src/sat/cadical/kitten.c deleted file mode 100644 index 7cce86c5b..000000000 --- a/src/sat/cadical/kitten.c +++ /dev/null @@ -1,2593 +0,0 @@ -#include "kitten.h" -#include "random.h" -#include "stack.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -typedef signed char value; - -static void die (const char *fmt, ...) { - fputs ("kitten: error: ", stderr); - va_list ap; - va_start (ap, fmt); - vfprintf (stderr, fmt, ap); - va_end (ap); - fputc ('\n', stderr); - exit (1); -} - -static inline void *kitten_calloc (size_t n, size_t size) { - void *res = calloc (n, size); - if (n && size && !res) - die ("out of memory allocating '%zu * %zu' bytes", n, size); - return res; -} - -#define CALLOC(P, N) \ - do { \ - (P) = kitten_calloc (N, sizeof *(P)); \ - } while (0) -#define DEALLOC(P, N) free (P) - -#undef ENLARGE_STACK - -#define ENLARGE_STACK(S) \ - do { \ - assert (FULL_STACK (S)); \ - const size_t SIZE = SIZE_STACK (S); \ - const size_t OLD_CAPACITY = CAPACITY_STACK (S); \ - const size_t NEW_CAPACITY = OLD_CAPACITY ? 2 * OLD_CAPACITY : 1; \ - const size_t BYTES = NEW_CAPACITY * sizeof *(S).begin; \ - (S).begin = realloc ((S).begin, BYTES); \ - if (!(S).begin) \ - die ("out of memory reallocating '%zu' bytes", BYTES); \ - (S).allocated = (S).begin + NEW_CAPACITY; \ - (S).end = (S).begin + SIZE; \ - } while (0) - -// Beside allocators above also use stand alone statistics counters. - -#define INC(NAME) \ - do { \ - statistics *statistics = &kitten->statistics; \ - assert (statistics->NAME < UINT64_MAX); \ - statistics->NAME++; \ - } while (0) - -#define ADD(NAME, DELTA) \ - do { \ - statistics *statistics = &kitten->statistics; \ - assert (statistics->NAME <= UINT64_MAX - (DELTA)); \ - statistics->NAME += (DELTA); \ - } while (0) - -#define KITTEN_TICKS (kitten->statistics.kitten_ticks) - -#define INVALID UINT_MAX -#define MAX_VARS ((1u << 31) - 1) - -#define CORE_FLAG (1u) -#define LEARNED_FLAG (2u) - -// clang-format off - -typedef struct kar kar; -typedef struct kink kink; -typedef struct klause klause; -typedef STACK (unsigned) klauses; -typedef unsigneds katches; - -// clang-format on - -struct kar { - unsigned level; - unsigned reason; -}; - -struct kink { - unsigned next; - unsigned prev; - uint64_t stamp; -}; - -struct klause { - unsigned aux; - unsigned size; - unsigned flags; - unsigned lits[1]; -}; - -typedef struct statistics statistics; - -struct statistics { - uint64_t learned; - uint64_t original; - uint64_t kitten_flip; - uint64_t kitten_flipped; - uint64_t kitten_sat; - uint64_t kitten_solved; - uint64_t kitten_conflicts; - uint64_t kitten_decisions; - uint64_t kitten_propagations; - uint64_t kitten_ticks; - uint64_t kitten_unknown; - uint64_t kitten_unsat; -}; - -typedef struct kimits kimits; - -struct kimits { - uint64_t ticks; -}; - -struct kitten { - // First zero initialized field in 'clear_kitten' is 'status'. - // - int status; - -#if defined(LOGGING) - bool logging; -#endif - bool antecedents; - bool learned; - - unsigned level; - unsigned propagated; - unsigned unassigned; - unsigned inconsistent; - unsigned failing; - - uint64_t generator; - - size_t lits; - size_t evars; - - size_t end_original_ref; - - struct { - unsigned first, last; - uint64_t stamp; - unsigned search; - } queue; - - // The 'size' field below is the first not zero reinitialized field - // by 'memset' in 'clear_kitten' (after 'kissat'). - - size_t size; - size_t esize; - - kar *vars; - kink *links; - value *marks; - value *values; - bool *failed; - unsigned char *phases; - unsigned *import; - katches *watches; - - unsigneds analyzed; - unsigneds assumptions; - unsigneds core; - unsigneds rcore; - unsigneds eclause; - unsigneds export; - unsigneds klause; - unsigneds klauses; - unsigneds resolved; - unsigneds trail; - unsigneds units; - unsigneds prime[2]; - - kimits limits; - int (*terminator) (void *); - void *terminator_data; - unsigneds clause; - uint64_t initialized; - statistics statistics; -}; - -/*------------------------------------------------------------------------*/ - -static inline bool is_core_klause (klause *c) { - return c->flags & CORE_FLAG; -} - -static inline bool is_learned_klause (klause *c) { - return c->flags & LEARNED_FLAG; -} - -static inline void set_core_klause (klause *c) { c->flags |= CORE_FLAG; } - -static inline void unset_core_klause (klause *c) { c->flags &= ~CORE_FLAG; } - -static inline klause *dereference_klause (kitten *kitten, unsigned ref) { - unsigned *res = BEGIN_STACK (kitten->klauses) + ref; - assert (res < END_STACK (kitten->klauses)); - return (klause *) res; -} - -static inline unsigned reference_klause (kitten *kitten, const klause *c) { - const unsigned *const begin = BEGIN_STACK (kitten->klauses); - const unsigned *p = (const unsigned *) c; - assert (begin <= p); - assert (p < END_STACK (kitten->klauses)); - const unsigned res = p - begin; - return res; -} - -/*------------------------------------------------------------------------*/ - -#define KATCHES(KIT) (kitten->watches[assert ((KIT) < kitten->lits), (KIT)]) - -#define all_klauses(C) \ - klause *C = begin_klauses (kitten), *end_##C = end_klauses (kitten); \ - (C) != end_##C; \ - (C) = next_klause (kitten, C) - -#define all_original_klauses(C) \ - klause *C = begin_klauses (kitten), \ - *end_##C = end_original_klauses (kitten); \ - (C) != end_##C; \ - (C) = next_klause (kitten, C) - -#define all_learned_klauses(C) \ - klause *C = begin_learned_klauses (kitten), \ - *end_##C = end_klauses (kitten); \ - (C) != end_##C; \ - (C) = next_klause (kitten, C) - -#define all_kits(KIT) \ - size_t KIT = 0, KIT_##END = kitten->lits; \ - KIT != KIT_##END; \ - KIT++ - -#define BEGIN_KLAUSE(C) (C)->lits - -#define END_KLAUSE(C) (BEGIN_KLAUSE (C) + (C)->size) - -#define all_literals_in_klause(KIT, C) \ - unsigned KIT, *KIT##_PTR = BEGIN_KLAUSE (C), \ - *KIT##_END = END_KLAUSE (C); \ - KIT##_PTR != KIT##_END && ((KIT = *KIT##_PTR), true); \ - ++KIT##_PTR - -#define all_antecedents(REF, C) \ - unsigned REF, *REF##_PTR = antecedents (C), \ - *REF##_END = REF##_PTR + (C)->aux; \ - REF##_PTR != REF##_END && ((REF = *REF##_PTR), true); \ - ++REF##_PTR - -#ifdef LOGGING - -#define logging (kitten->logging) - -static void log_basic (kitten *, const char *, ...) - __attribute__ ((format (printf, 2, 3))); - -static void log_basic (kitten *kitten, const char *fmt, ...) { - assert (logging); - printf ("c KITTEN %u ", kitten->level); - va_list ap; - va_start (ap, fmt); - vprintf (fmt, ap); - va_end (ap); - fputc ('\n', stdout); - fflush (stdout); -} - -static void log_reference (kitten *, unsigned, const char *, ...) - __attribute__ ((format (printf, 3, 4))); - -static void log_reference (kitten *kitten, unsigned ref, const char *fmt, - ...) { - klause *c = dereference_klause (kitten, ref); - assert (logging); - printf ("c KITTEN %u ", kitten->level); - va_list ap; - va_start (ap, fmt); - vprintf (fmt, ap); - va_end (ap); - if (is_learned_klause (c)) { - fputs (" learned", stdout); - if (c->aux) - printf ("[%u]", c->aux); - } else { - fputs (" original", stdout); - if (c->aux != INVALID) - printf ("[%u]", c->aux); - } - printf (" size %u clause[%u]", c->size, ref); - value *values = kitten->values; - kar *vars = kitten->vars; - for (all_literals_in_klause (lit, c)) { - printf (" %u", lit); - const value value = values[lit]; - if (value) - printf ("@%u=%d", vars[lit / 2].level, (int) value); - } - fputc ('\n', stdout); - fflush (stdout); -} - -static void log_literals (kitten *, unsigned *, unsigned, const char *, ...) - __attribute__ ((format (printf, 4, 5))); - -static void log_literals (kitten *kitten, unsigned *lits, unsigned size, - const char *fmt, ...) { - assert (logging); - printf ("c KITTEN %u ", kitten->level); - va_list ap; - va_start (ap, fmt); - vprintf (fmt, ap); - va_end (ap); - value *values = kitten->values; - kar *vars = kitten->vars; - for (unsigned i = 0; i < size; i++) { - const unsigned lit = lits[i]; - printf (" %u", lit); - const value value = values[lit]; - if (value) - printf ("@%u=%d", vars[lit / 2].level, (int) value); - } - fputc ('\n', stdout); - fflush (stdout); -} - -#define LOG(...) \ - do { \ - if (logging) \ - log_basic (kitten, __VA_ARGS__); \ - } while (0) - -#define ROG(...) \ - do { \ - if (logging) \ - log_reference (kitten, __VA_ARGS__); \ - } while (0) - -#define LOGLITS(...) \ - do { \ - if (logging) \ - log_literals (kitten, __VA_ARGS__); \ - } while (0) - -#else - -#define LOG(...) \ - do { \ - } while (0) -#define ROG(...) \ - do { \ - } while (0) -#define LOGLITS(...) \ - do { \ - } while (0) - -#endif - -static void check_queue (kitten *kitten) { -#ifdef CHECK_KITTEN - const unsigned vars = kitten->lits / 2; - unsigned found = 0, prev = INVALID; - kink *links = kitten->links; - uint64_t stamp = 0; - for (unsigned idx = kitten->queue.first, next; idx != INVALID; - idx = next) { - kink *link = links + idx; - assert (link->prev == prev); - assert (!found || stamp < link->stamp); - assert (link->stamp < kitten->queue.stamp); - stamp = link->stamp; - next = link->next; - prev = idx; - found++; - } - assert (found == vars); - unsigned next = INVALID; - found = 0; - for (unsigned idx = kitten->queue.last, prev; idx != INVALID; - idx = prev) { - kink *link = links + idx; - assert (link->next == next); - prev = link->prev; - next = idx; - found++; - } - assert (found == vars); - value *values = kitten->values; - bool first = true; - for (unsigned idx = kitten->queue.search, next; idx != INVALID; - idx = next) { - kink *link = links + idx; - next = link->next; - const unsigned lit = 2 * idx; - assert (first || values[lit]); - first = false; - } -#else - (void) kitten; -#endif -} - -static void update_search (kitten *kitten, unsigned idx) { - if (kitten->queue.search == idx) - return; - kitten->queue.search = idx; - LOG ("search updated to %u stamped %" PRIu64, idx, - kitten->links[idx].stamp); -} - -static void enqueue (kitten *kitten, unsigned idx) { - LOG ("enqueue %u", idx); - kink *links = kitten->links; - kink *l = links + idx; - const unsigned last = kitten->queue.last; - if (last == INVALID) - kitten->queue.first = idx; - else - links[last].next = idx; - l->prev = last; - l->next = INVALID; - kitten->queue.last = idx; - l->stamp = kitten->queue.stamp++; - LOG ("stamp %" PRIu64, l->stamp); -} - -static void dequeue (kitten *kitten, unsigned idx) { - LOG ("dequeue %u", idx); - kink *links = kitten->links; - kink *l = links + idx; - const unsigned prev = l->prev; - const unsigned next = l->next; - if (prev == INVALID) - kitten->queue.first = next; - else - links[prev].next = next; - if (next == INVALID) - kitten->queue.last = prev; - else - links[next].prev = prev; -} - -static void init_queue (kitten *kitten, size_t old_vars, size_t new_vars) { - for (size_t idx = old_vars; idx < new_vars; idx++) { - assert (!kitten->values[2 * idx]); - assert (kitten->unassigned < UINT_MAX); - kitten->unassigned++; - enqueue (kitten, idx); - } - LOG ("initialized decision queue from %zu to %zu", old_vars, new_vars); - update_search (kitten, kitten->queue.last); - check_queue (kitten); -} - -static void initialize_kitten (kitten *kitten) { - kitten->queue.first = INVALID; - kitten->queue.last = INVALID; - kitten->inconsistent = INVALID; - kitten->failing = INVALID; - kitten->queue.search = INVALID; - kitten->terminator = 0; - kitten->terminator_data = 0; - kitten->limits.ticks = UINT64_MAX; - kitten->generator = kitten->initialized++; -} - -static void clear_kitten (kitten *kitten) { - size_t bytes = (char *) &kitten->size - (char *) &kitten->status; - memset (&kitten->status, 0, bytes); - memset (&kitten->statistics, 0, sizeof (statistics)); - initialize_kitten (kitten); -} - -#define RESIZE1(P) \ - do { \ - void *OLD_PTR = (P); \ - CALLOC ((P), new_size / 2); \ - const size_t BYTES = old_vars * sizeof *(P); \ - memcpy ((P), OLD_PTR, BYTES); \ - void *NEW_PTR = (P); \ - (P) = OLD_PTR; \ - DEALLOC ((P), old_size / 2); \ - (P) = NEW_PTR; \ - } while (0) - -#define RESIZE2(P) \ - do { \ - void *OLD_PTR = (P); \ - CALLOC ((P), new_size); \ - const size_t BYTES = old_lits * sizeof *(P); \ - memcpy ((P), OLD_PTR, BYTES); \ - void *NEW_PTR = (P); \ - (P) = OLD_PTR; \ - DEALLOC ((P), old_size); \ - (P) = NEW_PTR; \ - } while (0) - -static void enlarge_internal (kitten *kitten, size_t lit) { - const size_t new_lits = (lit | 1) + 1; - const size_t old_lits = kitten->lits; - assert (old_lits <= lit); - assert (old_lits < new_lits); - assert ((lit ^ 1) < new_lits); - assert (lit < new_lits); - const size_t old_size = kitten->size; - const unsigned new_vars = new_lits / 2; - const unsigned old_vars = old_lits / 2; - if (old_size < new_lits) { - size_t new_size = old_size ? 2 * old_size : 2; - while (new_size <= lit) - new_size *= 2; - LOG ("internal literals resized to %zu from %zu (requested %zu)", - new_size, old_size, new_lits); - - RESIZE1 (kitten->marks); - RESIZE1 (kitten->phases); - RESIZE2 (kitten->values); - RESIZE2 (kitten->failed); - RESIZE1 (kitten->vars); - RESIZE1 (kitten->links); - RESIZE2 (kitten->watches); - - kitten->size = new_size; - } - kitten->lits = new_lits; - init_queue (kitten, old_vars, new_vars); - LOG ("internal literals activated until %zu literals", new_lits); - return; -} - -static const char *status_to_string (int status) { - switch (status) { - case 10: - return "formula satisfied"; - case 11: - return "formula satisfied and prime implicant computed"; - case 20: - return "formula inconsistent"; - case 21: - return "formula inconsistent and core computed"; - default: - assert (!status); - return "formula unsolved"; - } -} - -static void invalid_api_usage (const char *fun, const char *fmt, ...) { - fprintf (stderr, "kitten: fatal error: invalid API usage in '%s': ", fun); - va_list ap; - va_start (ap, fmt); - vfprintf (stderr, fmt, ap); - va_end (ap); - fputc ('\n', stderr); - fflush (stderr); - abort (); -} - -#define INVALID_API_USAGE(...) invalid_api_usage (__func__, __VA_ARGS__) - -#define REQUIRE_INITIALIZED() \ - do { \ - if (!kitten) \ - INVALID_API_USAGE ("solver argument zero"); \ - } while (0) - -#define REQUIRE_STATUS(EXPECTED) \ - do { \ - REQUIRE_INITIALIZED (); \ - if (kitten->status != (EXPECTED)) \ - INVALID_API_USAGE ("invalid status '%s' (expected '%s')", \ - status_to_string (kitten->status), \ - status_to_string (EXPECTED)); \ - } while (0) - -#define UPDATE_STATUS(STATUS) \ - do { \ - if (kitten->status != (STATUS)) \ - LOG ("updating status from '%s' to '%s'", \ - status_to_string (kitten->status), status_to_string (STATUS)); \ - else \ - LOG ("keeping status at '%s'", status_to_string (STATUS)); \ - kitten->status = (STATUS); \ - } while (0) - -kitten *kitten_init (void) { - kitten *kitten; - CALLOC (kitten, 1); - initialize_kitten (kitten); - return kitten; -} - -#ifdef LOGGING -void kitten_set_logging (kitten *kitten) { logging = true; } -#endif - -void kitten_track_antecedents (kitten *kitten) { - REQUIRE_STATUS (0); - - if (kitten->learned) - INVALID_API_USAGE ("can not start tracking antecedents after learning"); - - LOG ("enabling antecedents tracking"); - kitten->antecedents = true; -} - -void kitten_randomize_phases (kitten *kitten) { - REQUIRE_INITIALIZED (); - - LOG ("randomizing phases"); - - unsigned char *phases = kitten->phases; - const unsigned vars = kitten->size / 2; - - uint64_t random = kissat_next_random64 (&kitten->generator); - - unsigned i = 0; - const unsigned rest = vars & ~63u; - - while (i != rest) { - uint64_t *p = (uint64_t *) (phases + i); - p[0] = (random >> 0) & 0x0101010101010101; - p[1] = (random >> 1) & 0x0101010101010101; - p[2] = (random >> 2) & 0x0101010101010101; - p[3] = (random >> 3) & 0x0101010101010101; - p[4] = (random >> 4) & 0x0101010101010101; - p[5] = (random >> 5) & 0x0101010101010101; - p[6] = (random >> 6) & 0x0101010101010101; - p[7] = (random >> 7) & 0x0101010101010101; - random = kissat_next_random64 (&kitten->generator); - i += 64; - } - - unsigned shift = 0; - while (i != vars) - phases[i++] = (random >> shift++) & 1; -} - -void kitten_flip_phases (kitten *kitten) { - REQUIRE_INITIALIZED (); - - LOG ("flipping phases"); - - unsigned char *phases = kitten->phases; - const unsigned vars = kitten->size / 2; - - unsigned i = 0; - const unsigned rest = vars & ~7u; - - while (i != rest) { - uint64_t *p = (uint64_t *) (phases + i); - *p ^= 0x0101010101010101; - i += 8; - } - - while (i != vars) - phases[i++] ^= 1; -} - -void kitten_no_ticks_limit (kitten *kitten) { - REQUIRE_INITIALIZED (); - LOG ("forcing no ticks limit"); - kitten->limits.ticks = UINT64_MAX; -} - -uint64_t kitten_current_ticks (kitten *kitten) { - REQUIRE_INITIALIZED (); - const uint64_t current = KITTEN_TICKS; - return current; -} - -void kitten_set_ticks_limit (kitten *kitten, uint64_t delta) { - REQUIRE_INITIALIZED (); - const uint64_t current = KITTEN_TICKS; - uint64_t limit; - if (UINT64_MAX - delta <= current) { - LOG ("forcing unlimited ticks limit"); - limit = UINT64_MAX; - } else { - limit = current + delta; - LOG ("new limit of %" PRIu64 " ticks after %" PRIu64, limit, delta); - } - - kitten->limits.ticks = limit; -} - -void kitten_no_terminator (kitten *kitten) { - REQUIRE_INITIALIZED (); - LOG ("removing terminator"); - kitten->terminator = 0; - kitten->terminator_data = 0; -} - -void kitten_set_terminator (kitten *kitten, void *data, - int (*terminator) (void *)) { - REQUIRE_INITIALIZED (); - LOG ("setting terminator"); - kitten->terminator = terminator; - kitten->terminator_data = data; -} - -static void shuffle_unsigned_array (kitten *kitten, size_t size, - unsigned *a) { - for (size_t i = 0; i < size; i++) { - const size_t j = kissat_pick_random (&kitten->generator, 0, i); - if (j == i) - continue; - const unsigned first = a[i]; - const unsigned second = a[j]; - a[i] = second; - a[j] = first; - } -} - -static void shuffle_unsigned_stack (kitten *kitten, unsigneds *stack) { - const size_t size = SIZE_STACK (*stack); - unsigned *a = BEGIN_STACK (*stack); - shuffle_unsigned_array (kitten, size, a); -} - -static void shuffle_katches (kitten *kitten) { - LOG ("shuffling watch lists"); - for (size_t lit = 0; lit < kitten->lits; lit++) - shuffle_unsigned_stack (kitten, &KATCHES (lit)); -} - -static void shuffle_queue (kitten *kitten) { - LOG ("shuffling variable decision order"); - - const unsigned vars = kitten->lits / 2; - for (unsigned i = 0; i < vars; i++) { - const unsigned idx = kissat_pick_random (&kitten->generator, 0, vars); - dequeue (kitten, idx); - enqueue (kitten, idx); - } - update_search (kitten, kitten->queue.last); -} - -static void shuffle_units (kitten *kitten) { - LOG ("shuffling units"); - shuffle_unsigned_stack (kitten, &kitten->units); -} - -void kitten_shuffle_clauses (kitten *kitten) { - REQUIRE_STATUS (0); - shuffle_queue (kitten); - shuffle_katches (kitten); - shuffle_units (kitten); -} - -static inline unsigned *antecedents (klause *c) { - assert (is_learned_klause (c)); - return c->lits + c->size; -} - -static inline void watch_klause (kitten *kitten, unsigned lit, - unsigned ref) { - ROG (ref, "watching %u in", lit); - katches *watches = &KATCHES (lit); - PUSH_STACK (*watches, ref); -} - -static inline void connect_new_klause (kitten *kitten, unsigned ref) { - ROG (ref, "new"); - - klause *c = dereference_klause (kitten, ref); - - if (!c->size) { - if (kitten->inconsistent == INVALID) { - ROG (ref, "registering inconsistent empty"); - kitten->inconsistent = ref; - } else - ROG (ref, "ignoring inconsistent empty"); - } else if (c->size == 1) { - ROG (ref, "watching unit"); - PUSH_STACK (kitten->units, ref); - } else { - watch_klause (kitten, c->lits[0], ref); - watch_klause (kitten, c->lits[1], ref); - } -} - -static unsigned new_reference (kitten *kitten) { - size_t ref = SIZE_STACK (kitten->klauses); - if (ref >= INVALID) { - die ("maximum number of literals exhausted"); - } - const unsigned res = (unsigned) ref; - assert (res != INVALID); - INC (kitten_ticks); - return res; -} - -static void new_original_klause (kitten *kitten, unsigned id) { - unsigned res = new_reference (kitten); - unsigned size = SIZE_STACK (kitten->klause); - unsigneds *klauses = &kitten->klauses; - PUSH_STACK (*klauses, id); - PUSH_STACK (*klauses, size); - PUSH_STACK (*klauses, 0); - for (all_stack (unsigned, lit, kitten->klause)) - PUSH_STACK (*klauses, lit); - connect_new_klause (kitten, res); - kitten->end_original_ref = SIZE_STACK (*klauses); - kitten->statistics.original++; -} - -static void enlarge_external (kitten *kitten, size_t eidx) { - const size_t old_size = kitten->esize; - const unsigned old_evars = kitten->evars; - assert (old_evars <= eidx); - const unsigned new_evars = eidx + 1; - if (old_size <= eidx) { - size_t new_size = old_size ? 2 * old_size : 1; - while (new_size <= eidx) - new_size *= 2; - LOG ("external resizing to %zu variables from %zu (requested %u)", - new_size, old_size, new_evars); - unsigned *old_import = kitten->import; - CALLOC (kitten->import, new_size); - const size_t bytes = old_evars * sizeof *kitten->import; - memcpy (kitten->import, old_import, bytes); - DEALLOC (old_import, old_size); - kitten->esize = new_size; - } - kitten->evars = new_evars; - LOG ("external variables enlarged to %u", new_evars); -} - -static unsigned import_literal (kitten *kitten, unsigned elit) { - const unsigned eidx = elit / 2; - if (eidx >= kitten->evars) - enlarge_external (kitten, eidx); - - unsigned iidx = kitten->import[eidx]; - if (!iidx) { - iidx = SIZE_STACK (kitten->export); - PUSH_STACK (kitten->export, eidx); - kitten->import[eidx] = iidx + 1; - } else - iidx--; - unsigned ilit = 2 * iidx + (elit & 1); - LOG ("imported external literal %u as internal literal %u", elit, ilit); - if (ilit >= kitten->lits) - enlarge_internal (kitten, ilit); - return ilit; -} - -static unsigned export_literal (kitten *kitten, unsigned ilit) { - const unsigned iidx = ilit / 2; - assert (iidx < SIZE_STACK (kitten->export)); - const unsigned eidx = PEEK_STACK (kitten->export, iidx); - const unsigned elit = 2 * eidx + (ilit & 1); - return elit; -} - -unsigned new_learned_klause (kitten *kitten) { - unsigned res = new_reference (kitten); - unsigneds *klauses = &kitten->klauses; - const size_t size = SIZE_STACK (kitten->klause); - assert (size <= UINT_MAX); - const size_t aux = - kitten->antecedents ? SIZE_STACK (kitten->resolved) : 0; - assert (aux <= UINT_MAX); - PUSH_STACK (*klauses, (unsigned) aux); - PUSH_STACK (*klauses, (unsigned) size); - PUSH_STACK (*klauses, LEARNED_FLAG); - for (all_stack (unsigned, lit, kitten->klause)) - PUSH_STACK (*klauses, lit); - if (aux) - for (all_stack (unsigned, ref, kitten->resolved)) - PUSH_STACK (*klauses, ref); - connect_new_klause (kitten, res); - kitten->learned = true; - kitten->statistics.learned++; - return res; -} - -void kitten_clear (kitten *kitten) { - LOG ("clear kitten of size %zu", kitten->size); - - assert (EMPTY_STACK (kitten->analyzed)); - assert (EMPTY_STACK (kitten->eclause)); - assert (EMPTY_STACK (kitten->resolved)); - - CLEAR_STACK (kitten->assumptions); - CLEAR_STACK (kitten->core); - CLEAR_STACK (kitten->klause); - CLEAR_STACK (kitten->klauses); - CLEAR_STACK (kitten->trail); - CLEAR_STACK (kitten->units); - CLEAR_STACK (kitten->clause); - CLEAR_STACK (kitten->prime[0]); - CLEAR_STACK (kitten->prime[1]); - - for (all_kits (kit)) - CLEAR_STACK (KATCHES (kit)); - - while (!EMPTY_STACK (kitten->export)) - kitten->import[POP_STACK (kitten->export)] = 0; - - const size_t lits = kitten->size; - const unsigned vars = lits / 2; - -#ifndef NDEBUG - for (unsigned i = 0; i < vars; i++) - assert (!kitten->marks[i]); -#endif - - memset (kitten->phases, 0, vars); - memset (kitten->values, 0, lits); - memset (kitten->failed, 0, lits); - memset (kitten->vars, 0, vars); - - clear_kitten (kitten); -} - -void kitten_release (kitten *kitten) { - RELEASE_STACK (kitten->analyzed); - RELEASE_STACK (kitten->assumptions); - RELEASE_STACK (kitten->core); - RELEASE_STACK (kitten->eclause); - RELEASE_STACK (kitten->export); - RELEASE_STACK (kitten->klause); - RELEASE_STACK (kitten->klauses); - RELEASE_STACK (kitten->resolved); - RELEASE_STACK (kitten->trail); - RELEASE_STACK (kitten->units); - RELEASE_STACK (kitten->clause); - RELEASE_STACK (kitten->prime[0]); - RELEASE_STACK (kitten->prime[1]); - - for (size_t lit = 0; lit < kitten->size; lit++) - RELEASE_STACK (kitten->watches[lit]); - - const size_t lits = kitten->size; - const unsigned vars = lits / 2; - DEALLOC (kitten->marks, vars); - DEALLOC (kitten->phases, vars); - DEALLOC (kitten->values, lits); - DEALLOC (kitten->failed, lits); - DEALLOC (kitten->vars, vars); - DEALLOC (kitten->links, vars); - DEALLOC (kitten->watches, lits); - DEALLOC (kitten->import, kitten->esize); - (void) vars; - - free (kitten); -} - -static inline void move_to_front (kitten *kitten, unsigned idx) { - if (idx == kitten->queue.last) - return; - LOG ("move to front variable %u", idx); - dequeue (kitten, idx); - enqueue (kitten, idx); - assert (kitten->values[2 * idx]); -} - -static inline void assign (kitten *kitten, unsigned lit, unsigned reason) { -#ifdef LOGGING - if (reason == INVALID) - LOG ("assign %u as decision", lit); - else - ROG (reason, "assign %u reason", lit); -#endif - value *values = kitten->values; - const unsigned not_lit = lit ^ 1; - assert (!values[lit]); - assert (!values[not_lit]); - values[lit] = 1; - values[not_lit] = -1; - const unsigned idx = lit / 2; - const unsigned sign = lit & 1; - kitten->phases[idx] = sign; - PUSH_STACK (kitten->trail, lit); - kar *v = kitten->vars + idx; - v->level = kitten->level; - if (!v->level) { - assert (reason != INVALID); - klause *c = dereference_klause (kitten, reason); - if (c->size > 1) { - if (kitten->antecedents) { - PUSH_STACK (kitten->resolved, reason); - for (all_literals_in_klause (other, c)) - if (other != lit) { - const unsigned other_idx = other / 2; - const unsigned other_ref = kitten->vars[other_idx].reason; - assert (other_ref != INVALID); - PUSH_STACK (kitten->resolved, other_ref); - } - } - PUSH_STACK (kitten->klause, lit); - reason = new_learned_klause (kitten); - CLEAR_STACK (kitten->resolved); - CLEAR_STACK (kitten->klause); - } - } - v->reason = reason; - assert (kitten->unassigned); - kitten->unassigned--; -} - -static inline unsigned propagate_literal (kitten *kitten, unsigned lit) { - LOG ("propagating %u", lit); - value *values = kitten->values; - assert (values[lit] > 0); - const unsigned not_lit = lit ^ 1; - katches *watches = kitten->watches + not_lit; - unsigned conflict = INVALID; - unsigned *q = BEGIN_STACK (*watches); - const unsigned *const end_watches = END_STACK (*watches); - unsigned const *p = q; - uint64_t ticks = (((char *) end_watches - (char *) q) >> 7) + 1; - while (p != end_watches) { - const unsigned ref = *q++ = *p++; - klause *c = dereference_klause (kitten, ref); - assert (c->size > 1); - unsigned *lits = c->lits; - const unsigned other = lits[0] ^ lits[1] ^ not_lit; - const value other_value = values[other]; - ticks++; - if (other_value > 0) - continue; - value replacement_value = -1; - unsigned replacement = INVALID; - const unsigned *const end_lits = lits + c->size; - unsigned *r; - for (r = lits + 2; r != end_lits; r++) { - replacement = *r; - replacement_value = values[replacement]; - if (replacement_value >= 0) - break; - } - if (replacement_value >= 0) { - assert (replacement != INVALID); - ROG (ref, "unwatching %u in", not_lit); - lits[0] = other; - lits[1] = replacement; - *r = not_lit; - watch_klause (kitten, replacement, ref); - q--; - } else if (other_value < 0) { - ROG (ref, "conflict"); - INC (kitten_conflicts); - conflict = ref; - break; - } else { - assert (!other_value); - assign (kitten, other, ref); - } - } - while (p != end_watches) - *q++ = *p++; - SET_END_OF_STACK (*watches, q); - ADD (kitten_ticks, ticks); - return conflict; -} - -static inline unsigned propagate (kitten *kitten) { - assert (kitten->inconsistent == INVALID); - unsigned propagated = 0; - unsigned conflict = INVALID; - while (conflict == INVALID && - kitten->propagated < SIZE_STACK (kitten->trail)) { - if (kitten->terminator && - kitten->terminator (kitten->terminator_data)) { - break; - } - const unsigned lit = PEEK_STACK (kitten->trail, kitten->propagated); - conflict = propagate_literal (kitten, lit); - kitten->propagated++; - propagated++; - } - ADD (kitten_propagations, propagated); - return conflict; -} - -static void bump (kitten *kitten) { - value *marks = kitten->marks; - for (all_stack (unsigned, idx, kitten->analyzed)) { - marks[idx] = 0; - move_to_front (kitten, idx); - } - check_queue (kitten); -} - -static inline void unassign (kitten *kitten, value *values, unsigned lit) { - const unsigned not_lit = lit ^ 1; - assert (values[lit]); - assert (values[not_lit]); - const unsigned idx = lit / 2; -#ifdef LOGGING - kar *var = kitten->vars + idx; - kitten->level = var->level; - LOG ("unassign %u", lit); -#endif - values[lit] = values[not_lit] = 0; - assert (kitten->unassigned < kitten->lits / 2); - kitten->unassigned++; - kink *links = kitten->links; - kink *link = links + idx; - if (link->stamp > links[kitten->queue.search].stamp) - update_search (kitten, idx); -} - -static void backtrack (kitten *kitten, unsigned jump) { - check_queue (kitten); - assert (jump < kitten->level); - LOG ("back%s to level %u", - (kitten->level == jump + 1 ? "tracking" : "jumping"), jump); - kar *vars = kitten->vars; - value *values = kitten->values; - unsigneds *trail = &kitten->trail; - while (!EMPTY_STACK (*trail)) { - const unsigned lit = TOP_STACK (*trail); - const unsigned idx = lit / 2; - const unsigned level = vars[idx].level; - if (level == jump) - break; - (void) POP_STACK (*trail); - unassign (kitten, values, lit); - } - kitten->propagated = SIZE_STACK (*trail); - kitten->level = jump; - check_queue (kitten); -} - -void completely_backtrack_to_root_level (kitten *kitten) { - check_queue (kitten); - LOG ("completely backtracking to level 0"); - value *values = kitten->values; - unsigneds *trail = &kitten->trail; - unsigneds *units = &kitten->units; -#ifndef NDEBUG - kar *vars = kitten->vars; -#endif - for (all_stack (unsigned, lit, *trail)) { - assert (vars[lit / 2].level); - unassign (kitten, values, lit); - } - CLEAR_STACK (*trail); - for (all_stack (unsigned, ref, *units)) { - klause *c = dereference_klause (kitten, ref); - assert (c->size == 1); - const unsigned unit = c->lits[0]; - const value value = values[unit]; - if (value <= 0) - continue; - unassign (kitten, values, unit); - } - kitten->propagated = 0; - kitten->level = 0; - check_queue (kitten); -} - -static void analyze (kitten *kitten, unsigned conflict) { - assert (kitten->level); - assert (kitten->inconsistent == INVALID); - assert (EMPTY_STACK (kitten->analyzed)); - assert (EMPTY_STACK (kitten->resolved)); - assert (EMPTY_STACK (kitten->klause)); - PUSH_STACK (kitten->klause, INVALID); - unsigned reason = conflict; - value *marks = kitten->marks; - const kar *const vars = kitten->vars; - const unsigned level = kitten->level; - unsigned const *p = END_STACK (kitten->trail); - unsigned open = 0, jump = 0, size = 1, uip; - for (;;) { - assert (reason != INVALID); - klause *c = dereference_klause (kitten, reason); - assert (c); - ROG (reason, "analyzing"); - PUSH_STACK (kitten->resolved, reason); - for (all_literals_in_klause (lit, c)) { - const unsigned idx = lit / 2; - if (marks[idx]) - continue; - assert (kitten->values[lit] < 0); - LOG ("analyzed %u", lit); - marks[idx] = true; - PUSH_STACK (kitten->analyzed, idx); - const kar *const v = vars + idx; - const unsigned tmp = v->level; - if (tmp < level) { - if (tmp > jump) { - jump = tmp; - if (size > 1) { - const unsigned other = PEEK_STACK (kitten->klause, 1); - POKE_STACK (kitten->klause, 1, lit); - lit = other; - } - } - PUSH_STACK (kitten->klause, lit); - size++; - } else - open++; - } - unsigned idx; - do { - assert (BEGIN_STACK (kitten->trail) < p); - uip = *--p; - } while (!marks[idx = uip / 2]); - assert (open); - if (!--open) - break; - reason = vars[idx].reason; - } - const unsigned not_uip = uip ^ 1; - LOG ("first UIP %u jump level %u size %u", not_uip, jump, size); - POKE_STACK (kitten->klause, 0, not_uip); - bump (kitten); - CLEAR_STACK (kitten->analyzed); - const unsigned learned_ref = new_learned_klause (kitten); - CLEAR_STACK (kitten->resolved); - CLEAR_STACK (kitten->klause); - backtrack (kitten, jump); - assign (kitten, not_uip, learned_ref); -} - -static void failing (kitten *kitten) { - assert (kitten->inconsistent == INVALID); - assert (!EMPTY_STACK (kitten->assumptions)); - assert (EMPTY_STACK (kitten->analyzed)); - assert (EMPTY_STACK (kitten->resolved)); - assert (EMPTY_STACK (kitten->klause)); - LOG ("analyzing failing assumptions"); - const value *const values = kitten->values; - const kar *const vars = kitten->vars; - unsigned failed_clashing = INVALID; - unsigned first_failed = INVALID; - unsigned failed_unit = INVALID; - for (all_stack (unsigned, lit, kitten->assumptions)) { - if (values[lit] >= 0) - continue; - if (first_failed == INVALID) - first_failed = lit; - const unsigned failed_idx = lit / 2; - const kar *const failed_var = vars + failed_idx; - if (!failed_var->level) { - failed_unit = lit; - break; - } - if (failed_clashing == INVALID && failed_var->reason == INVALID) - failed_clashing = lit; - } - unsigned failed; - if (failed_unit != INVALID) - failed = failed_unit; - else if (failed_clashing != INVALID) - failed = failed_clashing; - else - failed = first_failed; - assert (failed != INVALID); - const unsigned failed_idx = failed / 2; - const kar *const failed_var = vars + failed_idx; - const unsigned failed_reason = failed_var->reason; - LOG ("first failed assumption %u", failed); - kitten->failed[failed] = true; - - if (failed_unit != INVALID) { - assert (dereference_klause (kitten, failed_reason)->size == 1); - LOG ("root-level falsified assumption %u", failed); - kitten->failing = failed_reason; - ROG (kitten->failing, "failing reason"); - return; - } - - const unsigned not_failed = failed ^ 1; - if (failed_clashing != INVALID) { - LOG ("clashing with negated assumption %u", not_failed); - kitten->failed[not_failed] = true; - assert (kitten->failing == INVALID); - return; - } - - value *marks = kitten->marks; - assert (!marks[failed_idx]); - marks[failed_idx] = true; - PUSH_STACK (kitten->analyzed, failed_idx); - PUSH_STACK (kitten->klause, not_failed); - - unsigneds work; - INIT_STACK (work); - - LOGLITS (BEGIN_STACK (kitten->trail), SIZE_STACK (kitten->trail), - "trail"); - - assert (SIZE_STACK (kitten->trail)); - unsigned const *p = END_STACK (kitten->trail); - unsigned open = 1; - for (;;) { - if (!open) - break; - open--; - unsigned idx, uip; - do { - assert (BEGIN_STACK (kitten->trail) < p); - uip = *--p; - } while (!marks[idx = uip / 2]); - - const kar *var = vars + idx; - const unsigned reason = var->reason; - if (reason == INVALID) { - unsigned lit = 2 * idx; - if (values[lit] < 0) - lit ^= 1; - LOG ("failed assumption %u", lit); - assert (!kitten->failed[lit]); - kitten->failed[lit] = true; - const unsigned not_lit = lit ^ 1; - PUSH_STACK (kitten->klause, not_lit); - } else { - ROG (reason, "analyzing"); - PUSH_STACK (kitten->resolved, reason); - klause *c = dereference_klause (kitten, reason); - for (all_literals_in_klause (other, c)) { - const unsigned other_idx = other / 2; - if (marks[other_idx]) - continue; - assert (other_idx != idx); - marks[other_idx] = true; - assert (values[other]); - if (vars[other_idx].level) - open++; - else - PUSH_STACK (work, other_idx); - PUSH_STACK (kitten->analyzed, other_idx); - - LOG ("analyzing final literal %u", other ^ 1); - } - } - } - for (size_t next = 0; next < SIZE_STACK (work); next++) { - const unsigned idx = PEEK_STACK (work, next); - const kar *var = vars + idx; - const unsigned reason = var->reason; - if (reason == INVALID) { - unsigned lit = 2 * idx; - if (values[lit] < 0) - lit ^= 1; - LOG ("failed assumption %u", lit); - assert (!kitten->failed[lit]); - kitten->failed[lit] = true; - const unsigned not_lit = lit ^ 1; - PUSH_STACK (kitten->klause, not_lit); - } else { - ROG (reason, "analyzing unit"); - PUSH_STACK (kitten->resolved, reason); - } - } - - // this is bfs not dfs so it does not work for lrat :/ - /* - for (size_t next = 0; next < SIZE_STACK (kitten->analyzed); next++) { - const unsigned idx = PEEK_STACK (kitten->analyzed, next); - assert (marks[idx]); - const kar *var = vars + idx; - const unsigned reason = var->reason; - if (reason == INVALID) { - unsigned lit = 2 * idx; - if (values[lit] < 0) - lit ^= 1; - LOG ("failed assumption %u", lit); - assert (!kitten->failed[lit]); - kitten->failed[lit] = true; - const unsigned not_lit = lit ^ 1; - PUSH_STACK (kitten->klause, not_lit); - } else { - ROG (reason, "analyzing"); - PUSH_STACK (kitten->resolved, reason); - klause *c = dereference_klause (kitten, reason); - for (all_literals_in_klause (other, c)) { - const unsigned other_idx = other / 2; - if (other_idx == idx) - continue; - if (marks[other_idx]) - continue; - marks[other_idx] = true; - PUSH_STACK (kitten->analyzed, other_idx); - LOG ("analyzing final literal %u", other ^ 1); - } - } - } - */ - - for (all_stack (unsigned, idx, kitten->analyzed)) - assert (marks[idx]), marks[idx] = 0; - CLEAR_STACK (kitten->analyzed); - - RELEASE_STACK (work); - - const size_t resolved = SIZE_STACK (kitten->resolved); - assert (resolved); - - if (resolved == 1) { - kitten->failing = PEEK_STACK (kitten->resolved, 0); - ROG (kitten->failing, "reusing as core"); - } else { - kitten->failing = new_learned_klause (kitten); - ROG (kitten->failing, "new core"); - } - - CLEAR_STACK (kitten->resolved); - CLEAR_STACK (kitten->klause); -} - -static void flush_trail (kitten *kitten) { - unsigneds *trail = &kitten->trail; - LOG ("flushing %zu root-level literals from trail", SIZE_STACK (*trail)); - assert (!kitten->level); - kitten->propagated = 0; - CLEAR_STACK (*trail); -} - -static int decide (kitten *kitten) { - if (!kitten->level && !EMPTY_STACK (kitten->trail)) - flush_trail (kitten); - - const value *const values = kitten->values; - unsigned decision = INVALID; - const size_t assumptions = SIZE_STACK (kitten->assumptions); - while (kitten->level < assumptions) { - unsigned assumption = PEEK_STACK (kitten->assumptions, kitten->level); - value value = values[assumption]; - if (value < 0) { - LOG ("found failing assumption %u", assumption); - failing (kitten); - return 20; - } else if (value > 0) { - - kitten->level++; - LOG ("pseudo decision level %u for already satisfied assumption %u", - kitten->level, assumption); - } else { - decision = assumption; - LOG ("using assumption %u as decision", decision); - break; - } - } - - if (!kitten->unassigned) - return 10; - - if (KITTEN_TICKS >= kitten->limits.ticks) { - LOG ("ticks limit %" PRIu64 " hit after %" PRIu64 " ticks", - kitten->limits.ticks, KITTEN_TICKS); - return -1; - } - - if (kitten->terminator && kitten->terminator (kitten->terminator_data)) { - LOG ("terminator requested termination"); - return -1; - } - - if (decision == INVALID) { - unsigned idx = kitten->queue.search; - const kink *const links = kitten->links; - for (;;) { - assert (idx != INVALID); - if (!values[2 * idx]) - break; - idx = links[idx].prev; - } - update_search (kitten, idx); - const unsigned phase = kitten->phases[idx]; - decision = 2 * idx + phase; - LOG ("decision %u variable %u phase %u", decision, idx, phase); - } - INC (kitten_decisions); - kitten->level++; - assign (kitten, decision, INVALID); - return 0; -} - -static void inconsistent (kitten *kitten, unsigned ref) { - assert (ref != INVALID); - assert (kitten->inconsistent == INVALID); - - if (!kitten->antecedents) { - kitten->inconsistent = ref; - ROG (ref, "registering inconsistent virtually empty"); - return; - } - - unsigneds *analyzed = &kitten->analyzed; - unsigneds *resolved = &kitten->resolved; - - assert (EMPTY_STACK (*analyzed)); - assert (EMPTY_STACK (*resolved)); - - value *marks = kitten->marks; - const kar *const vars = kitten->vars; - unsigned next = 0; - - for (;;) { - assert (ref != INVALID); - klause *c = dereference_klause (kitten, ref); - assert (c); - ROG (ref, "analyzing inconsistent"); - PUSH_STACK (*resolved, ref); - for (all_literals_in_klause (lit, c)) { - const unsigned idx = lit / 2; - assert (!vars[idx].level); - if (marks[idx]) - continue; - assert (kitten->values[lit] < 0); - LOG ("analyzed %u", lit); - marks[idx] = true; - PUSH_STACK (kitten->analyzed, idx); - } - if (next == SIZE_STACK (kitten->analyzed)) - break; - const unsigned idx = PEEK_STACK (kitten->analyzed, next); - next++; - const kar *const v = vars + idx; - assert (!v->level); - ref = v->reason; - } - assert (EMPTY_STACK (kitten->klause)); - ref = new_learned_klause (kitten); - ROG (ref, "registering final inconsistent empty"); - kitten->inconsistent = ref; - - for (all_stack (unsigned, idx, *analyzed)) - marks[idx] = 0; - - CLEAR_STACK (*analyzed); - CLEAR_STACK (*resolved); -} - -static int propagate_units (kitten *kitten) { - if (kitten->inconsistent != INVALID) - return 20; - - if (EMPTY_STACK (kitten->units)) { - LOG ("no root level unit clauses"); - return 0; - } - - LOG ("propagating %zu root level unit clauses", - SIZE_STACK (kitten->units)); - - const value *const values = kitten->values; - - for (size_t next = 0; next < SIZE_STACK (kitten->units); next++) { - const unsigned ref = PEEK_STACK (kitten->units, next); - assert (ref != INVALID); - klause *c = dereference_klause (kitten, ref); - assert (c->size == 1); - ROG (ref, "propagating unit"); - const unsigned unit = c->lits[0]; - const value value = values[unit]; - if (value > 0) - continue; - if (value < 0) { - inconsistent (kitten, ref); - return 20; - } - assign (kitten, unit, ref); - } - const unsigned conflict = propagate (kitten); - if (conflict == INVALID) - return 0; - inconsistent (kitten, conflict); - return 20; -} - -/*------------------------------------------------------------------------*/ - -static klause *begin_klauses (kitten *kitten) { - return (klause *) BEGIN_STACK (kitten->klauses); -} - -static klause *end_original_klauses (kitten *kitten) { - return (klause *) (BEGIN_STACK (kitten->klauses) + - kitten->end_original_ref); -} - -static klause *end_klauses (kitten *kitten) { - return (klause *) END_STACK (kitten->klauses); -} - -static klause *next_klause (kitten *kitten, klause *c) { - assert (begin_klauses (kitten) <= c); - assert (c < end_klauses (kitten)); - unsigned *res = c->lits + c->size; - if (kitten->antecedents && is_learned_klause (c)) - res += c->aux; - return (klause *) res; -} - -/*------------------------------------------------------------------------*/ - -static void reset_core (kitten *kitten) { - LOG ("resetting core clauses"); - size_t reset = 0; - for (all_klauses (c)) - if (is_core_klause (c)) - unset_core_klause (c), reset++; - LOG ("reset %zu core clauses", reset); - CLEAR_STACK (kitten->core); -} - -static void reset_assumptions (kitten *kitten) { - LOG ("reset %zu assumptions", SIZE_STACK (kitten->assumptions)); - while (!EMPTY_STACK (kitten->assumptions)) { - const unsigned assumption = POP_STACK (kitten->assumptions); - kitten->failed[assumption] = false; - } -#ifndef NDEBUG - for (size_t i = 0; i < kitten->size; i++) - assert (!kitten->failed[i]); -#endif - CLEAR_STACK (kitten->assumptions); - if (kitten->failing != INVALID) { - ROG (kitten->failing, "reset failed assumption reason"); - kitten->failing = INVALID; - } -} - -static void reset_incremental (kitten *kitten) { - // if (kitten->level) - completely_backtrack_to_root_level (kitten); - if (!EMPTY_STACK (kitten->assumptions)) - reset_assumptions (kitten); - else - assert (kitten->failing == INVALID); - if (kitten->status == 21) - reset_core (kitten); - UPDATE_STATUS (0); -} - -/*------------------------------------------------------------------------*/ - -static bool flip_literal (kitten *kitten, unsigned lit) { - INC (kitten_flip); - signed char *values = kitten->values; - if (values[lit] < 0) - lit ^= 1; - LOG ("trying to flip value of satisfied literal %u", lit); - assert (values[lit] > 0); - katches *watches = kitten->watches + lit; - unsigned *q = BEGIN_STACK (*watches); - const unsigned *const end_watches = END_STACK (*watches); - unsigned const *p = q; - uint64_t ticks = (((char *) end_watches - (char *) q) >> 7) + 1; - bool res = true; - while (p != end_watches) { - const unsigned ref = *q++ = *p++; - klause *c = dereference_klause (kitten, ref); - unsigned *lits = c->lits; - const unsigned other = lits[0] ^ lits[1] ^ lit; - const value other_value = values[other]; - ticks++; - if (other_value > 0) - continue; - value replacement_value = -1; - unsigned replacement = INVALID; - const unsigned *const end_lits = lits + c->size; - unsigned *r; - for (r = lits + 2; r != end_lits; r++) { - replacement = *r; - assert (replacement != lit); - replacement_value = values[replacement]; - assert (replacement_value); - if (replacement_value > 0) - break; - } - if (replacement_value > 0) { - assert (replacement != INVALID); - ROG (ref, "unwatching %u in", lit); - lits[0] = other; - lits[1] = replacement; - *r = lit; - watch_klause (kitten, replacement, ref); - q--; - } else { - assert (replacement_value < 0); - ROG (ref, "single satisfied"); - res = false; - break; - } - } - while (p != end_watches) - *q++ = *p++; - SET_END_OF_STACK (*watches, q); - ADD (kitten_ticks, ticks); - if (res) { - LOG ("flipping value of %u", lit); - values[lit] = -1; - const unsigned not_lit = lit ^ 1; - values[not_lit] = 1; - INC (kitten_flipped); - } else - LOG ("failed to flip value of %u", lit); - return res; -} - -/*------------------------------------------------------------------------*/ - -// this cadical specific clause addition avoids copying clauses multiple -// times just to convert literals to unsigned representation. -// -static unsigned int2u (int lit) { - assert (lit != 0); - int idx = abs (lit) - 1; - return (lit < 0) + 2u * (unsigned) idx; -} - -void kitten_assume (kitten *kitten, unsigned elit) { - REQUIRE_INITIALIZED (); - if (kitten->status) - reset_incremental (kitten); - const unsigned ilit = import_literal (kitten, elit); - LOG ("registering assumption %u", ilit); - PUSH_STACK (kitten->assumptions, ilit); -} - -void kitten_assume_signed (kitten *kitten, int elit) { - unsigned kelit = int2u (elit); - kitten_assume (kitten, kelit); -} - -void kitten_clause_with_id_and_exception (kitten *kitten, unsigned id, - size_t size, - const unsigned *elits, - unsigned except) { - REQUIRE_INITIALIZED (); - if (kitten->status) - reset_incremental (kitten); - assert (EMPTY_STACK (kitten->klause)); - const unsigned *const end = elits + size; - for (const unsigned *p = elits; p != end; p++) { - const unsigned elit = *p; - if (elit == except) - continue; - const unsigned ilit = import_literal (kitten, elit); - assert (ilit < kitten->lits); - const unsigned iidx = ilit / 2; - if (kitten->marks[iidx]) - INVALID_API_USAGE ("variable '%u' of literal '%u' occurs twice", - elit / 2, elit); - kitten->marks[iidx] = true; - PUSH_STACK (kitten->klause, ilit); - } - for (unsigned *p = kitten->klause.begin; p != kitten->klause.end; p++) - kitten->marks[*p / 2] = false; - new_original_klause (kitten, id); - CLEAR_STACK (kitten->klause); -} - -void citten_clause_with_id_and_exception (kitten *kitten, unsigned id, - size_t size, const int *elits, - unsigned except) { - REQUIRE_INITIALIZED (); - if (kitten->status) - reset_incremental (kitten); - assert (EMPTY_STACK (kitten->klause)); - const int *const end = elits + size; - for (const int *p = elits; p != end; p++) { - const unsigned elit = int2u (*p); // this is the conversion - if (elit == except) - continue; - const unsigned ilit = import_literal (kitten, elit); - assert (ilit < kitten->lits); - const unsigned iidx = ilit / 2; - if (kitten->marks[iidx]) - INVALID_API_USAGE ("variable '%u' of literal '%u' occurs twice", - elit / 2, elit); - kitten->marks[iidx] = true; - PUSH_STACK (kitten->klause, ilit); - } - for (unsigned *p = kitten->klause.begin; p != kitten->klause.end; p++) - kitten->marks[*p / 2] = false; - new_original_klause (kitten, id); - CLEAR_STACK (kitten->klause); -} - -void citten_clause_with_id_and_equivalence (kitten *kitten, unsigned id, - size_t size, const int *elits, - unsigned lit, unsigned other) { - REQUIRE_INITIALIZED (); - if (kitten->status) - reset_incremental (kitten); - assert (EMPTY_STACK (kitten->klause)); - bool sat = false; - const int *const end = elits + size; - for (const int *p = elits; p != end; p++) { - const unsigned elit = int2u (*p); // this is the conversion - if (elit == (lit ^ 1u) || elit == (other ^ 1u)) - continue; - if (elit == lit || elit == other) { - sat = true; - break; - } - const unsigned ilit = import_literal (kitten, elit); - assert (ilit < kitten->lits); - const unsigned iidx = ilit / 2; - if (kitten->marks[iidx]) - INVALID_API_USAGE ("variable '%u' of literal '%u' occurs twice", - elit / 2, elit); - kitten->marks[iidx] = true; - PUSH_STACK (kitten->klause, ilit); - } - for (unsigned *p = kitten->klause.begin; p != kitten->klause.end; p++) - kitten->marks[*p / 2] = false; - if (!sat) - new_original_klause (kitten, id); - CLEAR_STACK (kitten->klause); -} - -void kitten_clause (kitten *kitten, size_t size, unsigned *elits) { - kitten_clause_with_id_and_exception (kitten, INVALID, size, elits, - INVALID); -} - -void citten_clause_with_id (kitten *kitten, unsigned id, size_t size, - int *elits) { - citten_clause_with_id_and_exception (kitten, id, size, elits, INVALID); -} - -void kitten_unit (kitten *kitten, unsigned lit) { - kitten_clause (kitten, 1, &lit); -} - -void kitten_binary (kitten *kitten, unsigned a, unsigned b) { - unsigned clause[2] = {a, b}; - kitten_clause (kitten, 2, clause); -} - -int kitten_solve (kitten *kitten) { - REQUIRE_INITIALIZED (); - if (kitten->status) - reset_incremental (kitten); - else // if (kitten->level) - completely_backtrack_to_root_level (kitten); - - LOG ("starting solving under %zu assumptions", - SIZE_STACK (kitten->assumptions)); - - INC (kitten_solved); - - int res = propagate_units (kitten); - while (!res) { - const unsigned conflict = propagate (kitten); - if (kitten->terminator && - kitten->terminator (kitten->terminator_data)) { - LOG ("terminator requested termination"); - res = -1; - break; - } - if (conflict != INVALID) { - if (kitten->level) - analyze (kitten, conflict); - else { - inconsistent (kitten, conflict); - res = 20; - } - } else - res = decide (kitten); - } - - if (res < 0) - res = 0; - - if (!res && !EMPTY_STACK (kitten->assumptions)) - reset_assumptions (kitten); - - UPDATE_STATUS (res); - - if (res == 10) - INC (kitten_sat); - else if (res == 20) - INC (kitten_unsat); - else - INC (kitten_unknown); - - LOG ("finished solving with result %d", res); - - return res; -} - -int kitten_status (kitten *kitten) { return kitten->status; } - -unsigned kitten_compute_clausal_core (kitten *kitten, - uint64_t *learned_ptr) { - REQUIRE_STATUS (20); - - if (!kitten->antecedents) - INVALID_API_USAGE ("antecedents not tracked"); - - LOG ("computing clausal core"); - - unsigneds *resolved = &kitten->resolved; - assert (EMPTY_STACK (*resolved)); - - unsigned original = 0; - uint64_t learned = 0; - - unsigned reason_ref = kitten->inconsistent; - - if (reason_ref == INVALID) { - assert (!EMPTY_STACK (kitten->assumptions)); - reason_ref = kitten->failing; - if (reason_ref == INVALID) { - LOG ("assumptions mutually inconsistent"); - goto DONE; - } - } - - PUSH_STACK (*resolved, reason_ref); - unsigneds *core = &kitten->core; - assert (EMPTY_STACK (*core)); - - while (!EMPTY_STACK (*resolved)) { - const unsigned c_ref = POP_STACK (*resolved); - if (c_ref == INVALID) { - const unsigned d_ref = POP_STACK (*resolved); - ROG (d_ref, "core[%zu]", SIZE_STACK (*core)); - PUSH_STACK (*core, d_ref); - klause *d = dereference_klause (kitten, d_ref); - assert (!is_core_klause (d)); - set_core_klause (d); - if (is_learned_klause (d)) - learned++; - else - original++; - } else { - klause *c = dereference_klause (kitten, c_ref); - if (is_core_klause (c)) - continue; - PUSH_STACK (*resolved, c_ref); - PUSH_STACK (*resolved, INVALID); - ROG (c_ref, "analyzing antecedent core"); - if (!is_learned_klause (c)) - continue; - for (all_antecedents (d_ref, c)) { - klause *d = dereference_klause (kitten, d_ref); - if (!is_core_klause (d)) - PUSH_STACK (*resolved, d_ref); - } - } - } - -DONE: - - if (learned_ptr) - *learned_ptr = learned; - - LOG ("clausal core of %u original clauses", original); - LOG ("clausal core of %" PRIu64 " learned clauses", learned); - kitten->statistics.original = original; - kitten->statistics.learned = 0; - UPDATE_STATUS (21); - - return original; -} - -void kitten_traverse_core_ids (kitten *kitten, void *state, - void (*traverse) (void *, unsigned)) { - REQUIRE_STATUS (21); - - LOG ("traversing core of original clauses"); - - unsigned traversed = 0; - - for (all_original_klauses (c)) { - // only happens for 'true' incremental calls, i.e. if add happens after - // solve - if (is_learned_klause (c)) - continue; - if (!is_core_klause (c)) - continue; - ROG (reference_klause (kitten, c), "traversing"); - traverse (state, c->aux); - traversed++; - } - - LOG ("traversed %u original core clauses", traversed); - (void) traversed; - - assert (kitten->status == 21); -} - -void kitten_traverse_core_clauses (kitten *kitten, void *state, - void (*traverse) (void *, bool, size_t, - const unsigned *)) { - REQUIRE_STATUS (21); - - LOG ("traversing clausal core"); - - unsigned traversed = 0; - - for (all_stack (unsigned, c_ref, kitten->core)) { - klause *c = dereference_klause (kitten, c_ref); - assert (is_core_klause (c)); - const bool learned = is_learned_klause (c); - unsigneds *eclause = &kitten->eclause; - assert (EMPTY_STACK (*eclause)); - for (all_literals_in_klause (ilit, c)) { - const unsigned elit = export_literal (kitten, ilit); - PUSH_STACK (*eclause, elit); - } - const size_t size = SIZE_STACK (*eclause); - const unsigned *elits = eclause->begin; - ROG (reference_klause (kitten, c), "traversing"); - traverse (state, learned, size, elits); - CLEAR_STACK (*eclause); - traversed++; - } - - LOG ("traversed %u core clauses", traversed); - (void) traversed; - - assert (kitten->status == 21); -} - -void kitten_traverse_core_clauses_with_id ( - kitten *kitten, void *state, - void (*traverse) (void *state, unsigned, bool learned, size_t, - const unsigned *)) { - REQUIRE_STATUS (21); - - LOG ("traversing clausal core"); - - unsigned traversed = 0; - - for (all_stack (unsigned, c_ref, kitten->core)) { - klause *c = dereference_klause (kitten, c_ref); - assert (is_core_klause (c)); - const bool learned = is_learned_klause (c); - unsigneds *eclause = &kitten->eclause; - assert (EMPTY_STACK (*eclause)); - for (all_literals_in_klause (ilit, c)) { - const unsigned elit = export_literal (kitten, ilit); - PUSH_STACK (*eclause, elit); - } - const size_t size = SIZE_STACK (*eclause); - const unsigned *elits = eclause->begin; - ROG (reference_klause (kitten, c), "traversing"); - unsigned ctag = learned ? 0 : c->aux; - traverse (state, ctag, learned, size, elits); - CLEAR_STACK (*eclause); - traversed++; - } - - LOG ("traversed %u core clauses", traversed); - (void) traversed; - - assert (kitten->status == 21); -} - -void kitten_trace_core (kitten *kitten, void *state, - void (*trace) (void *, unsigned, unsigned, bool, - size_t, const unsigned *, size_t, - const unsigned *)) { - REQUIRE_STATUS (21); - - LOG ("tracing clausal core"); - - unsigned traced = 0; - - for (all_stack (unsigned, c_ref, kitten->core)) { - klause *c = dereference_klause (kitten, c_ref); - assert (is_core_klause (c)); - const bool learned = is_learned_klause (c); - unsigneds *eclause = &kitten->eclause; - assert (EMPTY_STACK (*eclause)); - for (all_literals_in_klause (ilit, c)) { - const unsigned elit = export_literal (kitten, ilit); - PUSH_STACK (*eclause, elit); - } - const size_t size = SIZE_STACK (*eclause); - const unsigned *elits = eclause->begin; - - unsigneds *resolved = &kitten->resolved; - assert (EMPTY_STACK (*resolved)); - if (learned) { - for (all_antecedents (ref, c)) { - PUSH_STACK (*resolved, ref); - } - } - const size_t rsize = SIZE_STACK (*resolved); - const unsigned *rids = resolved->begin; - - unsigned cid = reference_klause (kitten, c); - unsigned ctag = learned ? 0 : c->aux; - ROG (cid, "tracing"); - trace (state, cid, ctag, learned, size, elits, rsize, rids); - CLEAR_STACK (*eclause); - CLEAR_STACK (*resolved); - traced++; - } - - LOG ("traced %u core clauses", traced); - (void) traced; - - assert (kitten->status == 21); -} - -void kitten_shrink_to_clausal_core (kitten *kitten) { - REQUIRE_STATUS (21); - - LOG ("shrinking formula to core of original clauses"); - - CLEAR_STACK (kitten->trail); - - kitten->unassigned = kitten->lits / 2; - kitten->propagated = 0; - kitten->level = 0; - - update_search (kitten, kitten->queue.last); - - memset (kitten->values, 0, kitten->lits); - - for (all_kits (lit)) - CLEAR_STACK (KATCHES (lit)); - - assert (kitten->inconsistent != INVALID); - klause *inconsistent = dereference_klause (kitten, kitten->inconsistent); - if (is_learned_klause (inconsistent) || inconsistent->size) { - ROG (kitten->inconsistent, "resetting inconsistent"); - kitten->inconsistent = INVALID; - } else - ROG (kitten->inconsistent, "keeping inconsistent"); - - CLEAR_STACK (kitten->units); - - klause *begin = begin_klauses (kitten), *q = begin; - klause const *const end = end_original_klauses (kitten); -#ifdef LOGGING - unsigned original = 0; -#endif - for (klause *c = begin, *next; c != end; c = next) { - next = next_klause (kitten, c); - // assert (!is_learned_klause (c)); not necessarily true - if (is_learned_klause (c)) - continue; - if (!is_core_klause (c)) - continue; - unset_core_klause (c); - const unsigned dst = (unsigned *) q - (unsigned *) begin; - const unsigned size = c->size; - if (!size) { - if (kitten->inconsistent != INVALID) - kitten->inconsistent = dst; - } else if (size == 1) { - PUSH_STACK (kitten->units, dst); - ROG (dst, "keeping"); - } else { - watch_klause (kitten, c->lits[0], dst); - watch_klause (kitten, c->lits[1], dst); - } - if (c == q) - q = next; - else { - const size_t bytes = (char *) next - (char *) c; - memmove (q, c, bytes); - q = (klause *) ((char *) q + bytes); - } -#ifdef LOGGING - original++; -#endif - } - SET_END_OF_STACK (kitten->klauses, (unsigned *) q); - kitten->end_original_ref = SIZE_STACK (kitten->klauses); - LOG ("end of original clauses at %zu", kitten->end_original_ref); - LOG ("%u original clauses left", original); - - CLEAR_STACK (kitten->core); - - UPDATE_STATUS (0); -} - -signed char kitten_signed_value (kitten *kitten, int selit) { - REQUIRE_STATUS (10); - const unsigned elit = int2u (selit); - const unsigned eidx = elit / 2; - if (eidx >= kitten->evars) - return 0; - unsigned iidx = kitten->import[eidx]; - if (!iidx) - return 0; - const unsigned ilit = 2 * (iidx - 1) + (elit & 1); - return kitten->values[ilit]; -} - -signed char kitten_value (kitten *kitten, unsigned elit) { - REQUIRE_STATUS (10); - const unsigned eidx = elit / 2; - if (eidx >= kitten->evars) - return 0; - unsigned iidx = kitten->import[eidx]; - if (!iidx) - return 0; - const unsigned ilit = 2 * (iidx - 1) + (elit & 1); - return kitten->values[ilit]; -} - -signed char kitten_fixed (kitten *kitten, unsigned elit) { - const unsigned eidx = elit / 2; - if (eidx >= kitten->evars) - return 0; - unsigned iidx = kitten->import[eidx]; - if (!iidx) - return 0; - iidx--; - const unsigned ilit = 2 * iidx + (elit & 1); - signed char res = kitten->values[ilit]; - if (!res) - return 0; - kar *v = kitten->vars + iidx; - if (v->level) - return 0; - return res; -} - -signed char kitten_fixed_signed (kitten *kitten, int elit) { - unsigned kelit = int2u (elit); - return kitten_fixed (kitten, kelit); -} - -bool kitten_flip_literal (kitten *kitten, unsigned elit) { - REQUIRE_STATUS (10); - const unsigned eidx = elit / 2; - if (eidx >= kitten->evars) - return false; - unsigned iidx = kitten->import[eidx]; - if (!iidx) - return false; - const unsigned ilit = 2 * (iidx - 1) + (elit & 1); - if (kitten_fixed (kitten, elit)) - return false; - return flip_literal (kitten, ilit); -} - -bool kitten_flip_signed_literal (kitten *kitten, int elit) { - REQUIRE_STATUS (10); - unsigned kelit = int2u (elit); - return kitten_flip_literal (kitten, kelit); -} - -bool kitten_failed (kitten *kitten, unsigned elit) { - REQUIRE_STATUS (20); - const unsigned eidx = elit / 2; - if (eidx >= kitten->evars) - return false; - unsigned iidx = kitten->import[eidx]; - if (!iidx) - return false; - const unsigned ilit = 2 * (iidx - 1) + (elit & 1); - return kitten->failed[ilit]; -} - -// checks both watches for clauses with only one literal positively -// assigned. if such a clause is found, return false. Otherwise fix watch -// invariant and return true -static bool prime_propagate (kitten *kitten, const unsigned idx, - void *state, const bool ignoring, - bool (*ignore) (void *, unsigned)) { - unsigned lit = 2 * idx; - unsigned conflict = INVALID; - value *values = kitten->values; - for (int i = 0; i < 2; i++) { - if (conflict != INVALID) - break; - lit = lit ^ i; - const unsigned not_lit = lit ^ 1; - katches *watches = kitten->watches + not_lit; - unsigned *q = BEGIN_STACK (*watches); - const unsigned *const end_watches = END_STACK (*watches); - unsigned const *p = q; - uint64_t ticks = (((char *) end_watches - (char *) q) >> 7) + 1; - while (p != end_watches) { - const unsigned ref = *q++ = *p++; - klause *c = dereference_klause (kitten, ref); - if (is_learned_klause (c) || ignore (state, c->aux) == ignoring) - continue; - assert (c->size > 1); - unsigned *lits = c->lits; - const unsigned other = lits[0] ^ lits[1] ^ not_lit; - const value other_value = values[other]; - ticks++; - if (other_value > 0) - continue; - value replacement_value = -1; - unsigned replacement = INVALID; - const unsigned *const end_lits = lits + c->size; - unsigned *r; - for (r = lits + 2; r != end_lits; r++) { - replacement = *r; - replacement_value = values[replacement]; - if (replacement_value > 0) - break; - } - if (replacement_value > 0) { - assert (replacement != INVALID); - ROG (ref, "unwatching %u in", not_lit); - lits[0] = other; - lits[1] = replacement; - *r = not_lit; - watch_klause (kitten, replacement, ref); - q--; - } else { - ROG (ref, "idx %u forced prime by", idx); - conflict = ref; - break; - } - } - while (p != end_watches) - *q++ = *p++; - SET_END_OF_STACK (*watches, q); - ADD (kitten_ticks, ticks); - } - return conflict == INVALID; -} - -void kitten_add_prime_implicant (kitten *kitten, void *state, int side, - void (*add_implicant) (void *, int, size_t, - const unsigned *)) { - REQUIRE_STATUS (11); - // might be possible in some edge cases - unsigneds *prime = &kitten->prime[side]; - unsigneds *prime2 = &kitten->prime[!side]; - assert (!EMPTY_STACK (*prime) || !EMPTY_STACK (*prime2)); - CLEAR_STACK (*prime2); - - for (all_stack (unsigned, lit, *prime)) { - const unsigned not_lit = lit ^ 1; - const unsigned elit = export_literal (kitten, not_lit); - PUSH_STACK (*prime2, elit); - } - - // adds a clause which will reset kitten status and backtrack - add_implicant (state, side, SIZE_STACK (*prime2), BEGIN_STACK (*prime2)); - CLEAR_STACK (*prime); - CLEAR_STACK (*prime2); -} - -// computes two prime implicants, only considering clauses based on ignore -// return -1 if no prime implicant has been computed, otherwise returns -// index of shorter implicant. -// TODO does not work if flip has been called beforehand -int kitten_compute_prime_implicant (kitten *kitten, void *state, - bool (*ignore) (void *, unsigned)) { - REQUIRE_STATUS (10); - - value *values = kitten->values; - kar *vars = kitten->vars; - unsigneds unassigned; - INIT_STACK (unassigned); - bool limit_hit = 0; - assert (EMPTY_STACK (kitten->prime[0]) && EMPTY_STACK (kitten->prime[1])); - for (int i = 0; i < 2; i++) { - const bool ignoring = i; - for (all_stack (unsigned, lit, kitten->trail)) { - if (KITTEN_TICKS >= kitten->limits.ticks) { - LOG ("ticks limit %" PRIu64 " hit after %" PRIu64 " ticks", - kitten->limits.ticks, KITTEN_TICKS); - limit_hit = 1; - break; - } - assert (values[lit] > 0); - const unsigned idx = lit / 2; - const unsigned ref = vars[idx].reason; - assert (vars[idx].level); - klause *c = 0; - if (ref != INVALID) - c = dereference_klause (kitten, ref); - if (ref == INVALID || is_learned_klause (c) || - ignore (state, c->aux) == ignoring) { - LOG ("non-prime candidate var %d", idx); - if (prime_propagate (kitten, idx, state, ignoring, ignore)) { - values[lit] = 0; - values[lit ^ 1] = 0; - PUSH_STACK (unassigned, lit); - } else - assert (values[lit] > 0); - } - } - unsigneds *prime = &kitten->prime[i]; - // push on prime implicant stack. - for (all_kits (lit)) { - if (values[lit] > 0) - PUSH_STACK (*prime, lit); - } - // reassign all literals on - for (all_stack (unsigned, lit, unassigned)) { - assert (!values[lit]); - values[lit] = 1; - values[lit ^ 1] = -1; - } - CLEAR_STACK (unassigned); - } - RELEASE_STACK (unassigned); - - if (limit_hit) { - CLEAR_STACK (kitten->prime[0]); - CLEAR_STACK (kitten->prime[1]); - return -1; - } - // the only case when one of the prime implicants is allowed to be empty - // is if ignore returns always true or always false. - assert (!EMPTY_STACK (kitten->prime[0]) || - !EMPTY_STACK (kitten->prime[1])); - UPDATE_STATUS (11); - - int res = SIZE_STACK (kitten->prime[0]) > SIZE_STACK (kitten->prime[1]); - return res; -} - -static bool contains_blit (kitten *kitten, klause *c, const unsigned blit) { - for (all_literals_in_klause (lit, c)) { - if (lit == blit) - return true; - } - return false; -} - -static bool prime_propagate_blit (kitten *kitten, const unsigned idx, - const unsigned blit) { - unsigned lit = 2 * idx; - unsigned conflict = INVALID; - value *values = kitten->values; - LOG ("prime propagating idx %u for blit %u", idx, blit); - for (int i = 0; i < 2; i++) { - if (conflict != INVALID) - break; - lit = lit ^ i; - if (lit == blit) - continue; - const unsigned not_lit = lit ^ 1; - katches *watches = kitten->watches + not_lit; - unsigned *q = BEGIN_STACK (*watches); - const unsigned *const end_watches = END_STACK (*watches); - unsigned const *p = q; - uint64_t ticks = (((char *) end_watches - (char *) q) >> 7) + 1; - while (p != end_watches) { - const unsigned ref = *q++ = *p++; - klause *c = dereference_klause (kitten, ref); - if (is_learned_klause (c)) - continue; - ROG (ref, "checking with blit %u", blit); - assert (c->size > 1); - unsigned *lits = c->lits; - const unsigned other = lits[0] ^ lits[1] ^ not_lit; - const value other_value = values[other]; - ticks++; - bool use = other == blit || not_lit == blit; - if (other_value > 0) - continue; - value replacement_value = -1; - unsigned replacement = INVALID; - const unsigned *const end_lits = lits + c->size; - unsigned *r; - for (r = lits + 2; r != end_lits; r++) { - replacement = *r; - replacement_value = values[replacement]; - use = use || replacement == blit; - if (replacement_value > 0) - break; - } - if (replacement_value > 0) { - assert (replacement != INVALID); - ROG (ref, "unwatching %u in", not_lit); - lits[0] = other; - lits[1] = replacement; - *r = not_lit; - watch_klause (kitten, replacement, ref); - q--; - } else if (!use) { - continue; - } else { - ROG (ref, "idx %u forced prime by", idx); - conflict = ref; - break; - } - } - while (p != end_watches) - *q++ = *p++; - SET_END_OF_STACK (*watches, q); - ADD (kitten_ticks, ticks); - } - return conflict == INVALID; -} - -static int compute_prime_implicant_for (kitten *kitten, unsigned blit) { - value *values = kitten->values; - kar *vars = kitten->vars; - unsigneds unassigned; - INIT_STACK (unassigned); - bool limit_hit = false; - assert (EMPTY_STACK (kitten->prime[0]) && EMPTY_STACK (kitten->prime[1])); - for (int i = 0; i < 2; i++) { - const unsigned block = blit ^ i; - const bool ignoring = i; - if (prime_propagate_blit (kitten, block / 2, block)) { - value tmp = values[blit]; - assert (tmp); - values[blit] = 0; - values[blit ^ 1] = 0; - PUSH_STACK (unassigned, tmp > 0 ? blit : blit ^ 1); - PUSH_STACK (kitten->prime[i], block); // will be negated! - } else - assert (false); - for (all_stack (unsigned, lit, kitten->trail)) { - if (KITTEN_TICKS >= kitten->limits.ticks) { - LOG ("ticks limit %" PRIu64 " hit after %" PRIu64 " ticks", - kitten->limits.ticks, KITTEN_TICKS); - limit_hit = true; - break; - } - if (!values[lit]) - continue; - assert (values[lit]); // not true when flipping is involved - const unsigned idx = lit / 2; - const unsigned ref = vars[idx].reason; - assert (vars[idx].level); - LOG ("non-prime candidate var %d", idx); - if (prime_propagate_blit (kitten, idx, block)) { - value tmp = values[lit]; - assert (tmp); - values[lit] = 0; - values[lit ^ 1] = 0; - PUSH_STACK (unassigned, tmp > 0 ? lit : lit ^ 1); - } - } - unsigneds *prime = &kitten->prime[i]; - // push on prime implicant stack. - for (all_kits (lit)) { - if (values[lit] > 0) - PUSH_STACK (*prime, lit); - } - // reassign all literals on - for (all_stack (unsigned, lit, unassigned)) { - assert (!values[lit]); - values[lit] = 1; - values[lit ^ 1] = -1; - } - CLEAR_STACK (unassigned); - } - RELEASE_STACK (unassigned); - - if (limit_hit) { - CLEAR_STACK (kitten->prime[0]); - CLEAR_STACK (kitten->prime[1]); - return -1; - } - // the only case when one of the prime implicants is allowed to be empty - // is if ignore returns always true or always false. - assert (!EMPTY_STACK (kitten->prime[0]) || - !EMPTY_STACK (kitten->prime[1])); - LOGLITS (BEGIN_STACK (kitten->prime[0]), SIZE_STACK (kitten->prime[0]), - "first implicant %u", blit); - LOGLITS (BEGIN_STACK (kitten->prime[1]), SIZE_STACK (kitten->prime[1]), - "second implicant %u", blit ^ 1); - UPDATE_STATUS (11); - - int res = SIZE_STACK (kitten->prime[0]) > SIZE_STACK (kitten->prime[1]); - return res; -} - -int kitten_flip_and_implicant_for_signed_literal (kitten *kitten, - int elit) { - REQUIRE_STATUS (10); - unsigned kelit = int2u (elit); - if (!kitten_flip_literal (kitten, kelit)) { - return -2; - } - const unsigned eidx = kelit / 2; - unsigned iidx = kitten->import[eidx]; - assert (iidx); - const unsigned ilit = 2 * (iidx - 1) + (kelit & 1); - return compute_prime_implicant_for (kitten, ilit); -} diff --git a/src/sat/cadical/kitten.h b/src/sat/cadical/kitten.h index 3132cda4d..c70d47b0d 100644 --- a/src/sat/cadical/kitten.h +++ b/src/sat/cadical/kitten.h @@ -1,97 +1,95 @@ -#ifndef _kitten_h_INCLUDED -#define _kitten_h_INCLUDED +#ifndef _cadical_kitten_h_INCLUDED +#define _cadical_kitten_h_INCLUDED + +#include "global.h" #include #include #include #include -#ifdef __cplusplus -extern "C" { -#endif +ABC_NAMESPACE_HEADER_START -typedef struct kitten kitten; +typedef struct cadical_kitten cadical_kitten; -kitten *kitten_init (void); -void kitten_clear (kitten *); -void kitten_release (kitten *); +cadical_kitten *cadical_kitten_init (void); +void cadical_kitten_clear (cadical_kitten *); +void cadical_kitten_release (cadical_kitten *); #ifdef LOGGING -void kitten_set_logging (kitten *kitten); +void cadical_kitten_set_logging (cadical_kitten *cadical_kitten); #endif -void kitten_track_antecedents (kitten *); +void cadical_kitten_track_antecedents (cadical_kitten *); -void kitten_shuffle_clauses (kitten *); -void kitten_flip_phases (kitten *); -void kitten_randomize_phases (kitten *); +void cadical_kitten_shuffle_clauses (cadical_kitten *); +void cadical_kitten_flip_phases (cadical_kitten *); +void cadical_kitten_randomize_phases (cadical_kitten *); -void kitten_assume (kitten *, unsigned lit); -void kitten_assume_signed (kitten *, int lit); +void cadical_kitten_assume (cadical_kitten *, unsigned lit); +void cadical_kitten_assume_signed (cadical_kitten *, int lit); -void kitten_clause (kitten *, size_t size, unsigned *); -void citten_clause_with_id (kitten *, unsigned id, size_t size, int *); -void kitten_unit (kitten *, unsigned); -void kitten_binary (kitten *, unsigned, unsigned); +void cadical_kitten_clause (cadical_kitten *, size_t size, unsigned *); +void citten_clause_with_id (cadical_kitten *, unsigned id, size_t size, int *); +void cadical_kitten_unit (cadical_kitten *, unsigned); +void cadical_kitten_binary (cadical_kitten *, unsigned, unsigned); -void kitten_clause_with_id_and_exception (kitten *, unsigned id, +void cadical_kitten_clause_with_id_and_exception (cadical_kitten *, unsigned id, size_t size, const unsigned *, unsigned except); -void citten_clause_with_id_and_exception (kitten *, unsigned id, +void citten_clause_with_id_and_exception (cadical_kitten *, unsigned id, size_t size, const int *, unsigned except); -void citten_clause_with_id_and_equivalence (kitten *, unsigned id, +void citten_clause_with_id_and_equivalence (cadical_kitten *, unsigned id, size_t size, const int *, unsigned, unsigned); -void kitten_no_ticks_limit (kitten *); -void kitten_set_ticks_limit (kitten *, uint64_t); -uint64_t kitten_current_ticks (kitten *); +void cadical_kitten_no_ticks_limit (cadical_kitten *); +void cadical_kitten_set_ticks_limit (cadical_kitten *, uint64_t); +uint64_t cadical_kitten_current_ticks (cadical_kitten *); -void kitten_no_terminator (kitten *); -void kitten_set_terminator (kitten *, void *, int (*) (void *)); +void cadical_kitten_no_terminator (cadical_kitten *); +void cadical_kitten_set_terminator (cadical_kitten *, void *, int (*) (void *)); -int kitten_solve (kitten *); -int kitten_status (kitten *); +int cadical_kitten_solve (cadical_kitten *); +int cadical_kitten_status (cadical_kitten *); -signed char kitten_value (kitten *, unsigned); -signed char kitten_signed_value (kitten *, int); // converts second argument -signed char kitten_fixed (kitten *, unsigned); -signed char kitten_fixed_signed (kitten *, int); // converts -bool kitten_failed (kitten *, unsigned); -bool kitten_flip_literal (kitten *, unsigned); -bool kitten_flip_signed_literal (kitten *, int); +signed char cadical_kitten_value (cadical_kitten *, unsigned); +signed char cadical_kitten_signed_value (cadical_kitten *, int); // converts second argument +signed char cadical_kitten_fixed (cadical_kitten *, unsigned); +signed char cadical_kitten_fixed_signed (cadical_kitten *, int); // converts +bool cadical_kitten_failed (cadical_kitten *, unsigned); +bool cadical_kitten_flip_literal (cadical_kitten *, unsigned); +bool cadical_kitten_flip_signed_literal (cadical_kitten *, int); -unsigned kitten_compute_clausal_core (kitten *, uint64_t *learned); -void kitten_shrink_to_clausal_core (kitten *); +unsigned cadical_kitten_compute_clausal_core (cadical_kitten *, uint64_t *learned); +void cadical_kitten_shrink_to_clausal_core (cadical_kitten *); -void kitten_traverse_core_ids (kitten *, void *state, +void cadical_kitten_traverse_core_ids (cadical_kitten *, void *state, void (*traverse) (void *state, unsigned id)); -void kitten_traverse_core_clauses (kitten *, void *state, +void cadical_kitten_traverse_core_clauses (cadical_kitten *, void *state, void (*traverse) (void *state, bool learned, size_t, const unsigned *)); -void kitten_traverse_core_clauses_with_id ( - kitten *, void *state, +void cadical_kitten_traverse_core_clauses_with_id ( + cadical_kitten *, void *state, void (*traverse) (void *state, unsigned, bool learned, size_t, const unsigned *)); -void kitten_trace_core (kitten *, void *state, +void cadical_kitten_trace_core (cadical_kitten *, void *state, void (*trace) (void *, unsigned, unsigned, bool, size_t, const unsigned *, size_t, const unsigned *)); -int kitten_compute_prime_implicant (kitten *kitten, void *state, +int cadical_kitten_compute_prime_implicant (cadical_kitten *cadical_kitten, void *state, bool (*ignore) (void *, unsigned)); -void kitten_add_prime_implicant (kitten *kitten, void *state, int side, +void cadical_kitten_add_prime_implicant (cadical_kitten *cadical_kitten, void *state, int side, void (*add_implicant) (void *, int, size_t, const unsigned *)); -int kitten_flip_and_implicant_for_signed_literal (kitten *kitten, int elit); +int cadical_kitten_flip_and_implicant_for_signed_literal (cadical_kitten *cadical_kitten, int elit); -#ifdef __cplusplus -} -#endif +ABC_NAMESPACE_HEADER_END #endif diff --git a/src/sat/cadical/level.hpp b/src/sat/cadical/level.hpp index cf20a77b1..bbfe774ff 100644 --- a/src/sat/cadical/level.hpp +++ b/src/sat/cadical/level.hpp @@ -1,8 +1,12 @@ #ifndef _level_hpp_INCLUDED #define _level_hpp_INCLUDED +#include "global.h" + #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // For each new decision we increase the decision level and push a 'Level' @@ -30,4 +34,6 @@ struct Level { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/lidruptracer.hpp b/src/sat/cadical/lidruptracer.hpp index 196e74670..02cd33fcd 100644 --- a/src/sat/cadical/lidruptracer.hpp +++ b/src/sat/cadical/lidruptracer.hpp @@ -1,6 +1,10 @@ #ifndef _lidruptracer_h_INCLUDED #define _lidruptracer_h_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + class FileTracer; namespace CaDiCaL { @@ -50,7 +54,7 @@ class LidrupTracer : public FileTracer { bool find_and_delete (const int64_t); // find clause position in hash table -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t added, deleted, weakened, restore, original, solved, batched; #endif @@ -104,7 +108,7 @@ public: // logging and file io void connect_internal (Internal *i) override; -#ifndef QUIET +#ifndef CADICAL_QUIET void print_statistics (); #endif bool closed () override; @@ -114,4 +118,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/limit.hpp b/src/sat/cadical/limit.hpp index 89004d36d..0f6923214 100644 --- a/src/sat/cadical/limit.hpp +++ b/src/sat/cadical/limit.hpp @@ -1,9 +1,13 @@ #ifndef _limit_hpp_INCLUDED #define _limit_hpp_INCLUDED +#include "global.h" + #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Internal; @@ -153,4 +157,6 @@ struct Inc { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/logging.hpp b/src/sat/cadical/logging.hpp index 46b0e07bf..edbd60f8c 100644 --- a/src/sat/cadical/logging.hpp +++ b/src/sat/cadical/logging.hpp @@ -1,6 +1,8 @@ #ifndef _logging_hpp_INCLUDED #define _logging_hpp_INCLUDED +#include "global.h" + /*------------------------------------------------------------------------*/ #ifdef LOGGING /*------------------------------------------------------------------------*/ @@ -8,6 +10,8 @@ #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // For debugging purposes and to help understanding what the solver is doing @@ -82,6 +86,8 @@ struct Logger { #define LOGLIT(lit) Logger::loglit (internal, lit).c_str () +ABC_NAMESPACE_CXX_HEADER_END + /*------------------------------------------------------------------------*/ #else // end of 'then' part of 'ifdef LOGGING' /*------------------------------------------------------------------------*/ diff --git a/src/sat/cadical/lratchecker.hpp b/src/sat/cadical/lratchecker.hpp index ff9a5ba90..0e791e9f4 100644 --- a/src/sat/cadical/lratchecker.hpp +++ b/src/sat/cadical/lratchecker.hpp @@ -1,9 +1,13 @@ #ifndef _lratchecker_hpp_INCLUDED #define _lratchecker_hpp_INCLUDED +#include "global.h" + /*------------------------------------------------------------------------*/ #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -161,4 +165,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/lrattracer.hpp b/src/sat/cadical/lrattracer.hpp index 208cf81e2..9d8e92b6a 100644 --- a/src/sat/cadical/lrattracer.hpp +++ b/src/sat/cadical/lrattracer.hpp @@ -1,6 +1,10 @@ #ifndef _lrattracer_h_INCLUDED #define _lrattracer_h_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { class LratTracer : public FileTracer { @@ -9,7 +13,7 @@ class LratTracer : public FileTracer { File *file; bool binary; -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t added, deleted; #endif int64_t latest_id; @@ -44,7 +48,7 @@ public: void report_status (int, int64_t) override {} // skip -#ifndef QUIET +#ifndef CADICAL_QUIET void print_statistics (); #endif bool closed () override; @@ -54,4 +58,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/message.hpp b/src/sat/cadical/message.hpp index ca2c00587..e0209fed8 100644 --- a/src/sat/cadical/message.hpp +++ b/src/sat/cadical/message.hpp @@ -1,11 +1,15 @@ #ifndef _message_h_INCLUDED #define _message_h_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + /*------------------------------------------------------------------------*/ // Macros for compact message code. -#ifndef QUIET +#ifndef CADICAL_QUIET #define LINE() \ do { \ @@ -62,4 +66,6 @@ /*------------------------------------------------------------------------*/ +ABC_NAMESPACE_CXX_HEADER_END + #endif // ifndef _message_h_INCLUDED diff --git a/src/sat/cadical/module.make b/src/sat/cadical/module.make new file mode 100644 index 000000000..74f5d23cb --- /dev/null +++ b/src/sat/cadical/module.make @@ -0,0 +1,91 @@ +SRC +=src/sat/cadical/cadicalSolver.c \ +src/sat/cadical/cadicalTest.c \ +src/sat/cadical/cadical_analyze.cpp \ +src/sat/cadical/cadical_arena.cpp \ +src/sat/cadical/cadical_assume.cpp \ +src/sat/cadical/cadical_averages.cpp \ +src/sat/cadical/cadical_backtrack.cpp \ +src/sat/cadical/cadical_backward.cpp \ +src/sat/cadical/cadical_bins.cpp \ +src/sat/cadical/cadical_block.cpp \ +src/sat/cadical/cadical_ccadical.cpp \ +src/sat/cadical/cadical_checker.cpp \ +src/sat/cadical/cadical_clause.cpp \ +src/sat/cadical/cadical_collect.cpp \ +src/sat/cadical/cadical_compact.cpp \ +src/sat/cadical/cadical_condition.cpp \ +src/sat/cadical/cadical_config.cpp \ +src/sat/cadical/cadical_congruence.cpp \ +src/sat/cadical/cadical_constrain.cpp \ +src/sat/cadical/cadical_contract.cpp \ +src/sat/cadical/cadical_cover.cpp \ +src/sat/cadical/cadical_decide.cpp \ +src/sat/cadical/cadical_decompose.cpp \ +src/sat/cadical/cadical_deduplicate.cpp \ +src/sat/cadical/cadical_definition.cpp \ +src/sat/cadical/cadical_drattracer.cpp \ +src/sat/cadical/cadical_elim.cpp \ +src/sat/cadical/cadical_elimfast.cpp \ +src/sat/cadical/cadical_ema.cpp \ +src/sat/cadical/cadical_extend.cpp \ +src/sat/cadical/cadical_external.cpp \ +src/sat/cadical/cadical_external_propagate.cpp \ +src/sat/cadical/cadical_factor.cpp \ +src/sat/cadical/cadical_file.cpp \ +src/sat/cadical/cadical_flags.cpp \ +src/sat/cadical/cadical_flip.cpp \ +src/sat/cadical/cadical_format.cpp \ +src/sat/cadical/cadical_frattracer.cpp \ +src/sat/cadical/cadical_gates.cpp \ +src/sat/cadical/cadical_idruptracer.cpp \ +src/sat/cadical/cadical_instantiate.cpp \ +src/sat/cadical/cadical_internal.cpp \ +src/sat/cadical/cadical_ipasir.cpp \ +src/sat/cadical/cadical_lidruptracer.cpp \ +src/sat/cadical/cadical_limit.cpp \ +src/sat/cadical/cadical_logging.cpp \ +src/sat/cadical/cadical_lookahead.cpp \ +src/sat/cadical/cadical_lratchecker.cpp \ +src/sat/cadical/cadical_lrattracer.cpp \ +src/sat/cadical/cadical_lucky.cpp \ +src/sat/cadical/cadical_message.cpp \ +src/sat/cadical/cadical_minimize.cpp \ +src/sat/cadical/cadical_occs.cpp \ +src/sat/cadical/cadical_options.cpp \ +src/sat/cadical/cadical_parse.cpp \ +src/sat/cadical/cadical_phases.cpp \ +src/sat/cadical/cadical_probe.cpp \ +src/sat/cadical/cadical_profile.cpp \ +src/sat/cadical/cadical_proof.cpp \ +src/sat/cadical/cadical_propagate.cpp \ +src/sat/cadical/cadical_queue.cpp \ +src/sat/cadical/cadical_random.cpp \ +src/sat/cadical/cadical_reap.cpp \ +src/sat/cadical/cadical_reduce.cpp \ +src/sat/cadical/cadical_rephase.cpp \ +src/sat/cadical/cadical_report.cpp \ +src/sat/cadical/cadical_resources.cpp \ +src/sat/cadical/cadical_restart.cpp \ +src/sat/cadical/cadical_restore.cpp \ +src/sat/cadical/cadical_score.cpp \ +src/sat/cadical/cadical_shrink.cpp \ +src/sat/cadical/cadical_signal.cpp \ +src/sat/cadical/cadical_solution.cpp \ +src/sat/cadical/cadical_solver.cpp \ +src/sat/cadical/cadical_stable.cpp \ +src/sat/cadical/cadical_stats.cpp \ +src/sat/cadical/cadical_subsume.cpp \ +src/sat/cadical/cadical_sweep.cpp \ +src/sat/cadical/cadical_terminal.cpp \ +src/sat/cadical/cadical_ternary.cpp \ +src/sat/cadical/cadical_tier.cpp \ +src/sat/cadical/cadical_transred.cpp \ +src/sat/cadical/cadical_unstable.cpp \ +src/sat/cadical/cadical_util.cpp \ +src/sat/cadical/cadical_var.cpp \ +src/sat/cadical/cadical_veripbtracer.cpp \ +src/sat/cadical/cadical_version.cpp \ +src/sat/cadical/cadical_vivify.cpp \ +src/sat/cadical/cadical_walk.cpp \ +src/sat/cadical/cadical_watch.cpp \ +src/sat/cadical/cadical_kitten.c diff --git a/src/sat/cadical/occs.hpp b/src/sat/cadical/occs.hpp index 6988725d6..28a9246bc 100644 --- a/src/sat/cadical/occs.hpp +++ b/src/sat/cadical/occs.hpp @@ -1,8 +1,12 @@ #ifndef _occs_h_INCLUDED #define _occs_h_INCLUDED +#include "global.h" + #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // Full occurrence lists used in a one-watch scheme for all clauses in @@ -24,7 +28,7 @@ inline void remove_occs (Occs &os, Clause *c) { if (c == d) i--; } - assert (i + 1 == end); + CADICAL_assert (i + 1 == end); os.resize (i - os.begin ()); } @@ -33,4 +37,6 @@ typedef Occs::const_iterator const_occs_iterator; } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/options.hpp b/src/sat/cadical/options.hpp index 79005a952..1527da2e1 100644 --- a/src/sat/cadical/options.hpp +++ b/src/sat/cadical/options.hpp @@ -1,6 +1,10 @@ #ifndef _options_hpp_INCLUDED #define _options_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + /*------------------------------------------------------------------------*/ // In order to add a new option, simply add a new line below. Make sure that @@ -81,9 +85,9 @@ OPTION( elimbackward, 1, 0, 1,0,0,1, "eager backward subsumption") \ OPTION( elimboundmax, 16, -1,2e6,1,0,1, "maximum elimination bound") \ OPTION( elimboundmin, 0, -1,2e6,0,0,1, "minimum elimination bound") \ OPTION( elimclslim, 1e2, 2,2e9,2,0,1, "resolvent size limit") \ -OPTION( elimdef, 0, 0, 1,0,0,1, "mine definitions with kitten") \ +OPTION( elimdef, 0, 0, 1,0,0,1, "mine definitions with cadical_kitten") \ OPTION( elimdefcores, 1, 1,100,0,0,1, "number of unsat cores") \ -OPTION( elimdefticks, 2e5, 0,2e9,1,0,1, "kitten ticks limit") \ +OPTION( elimdefticks, 2e5, 0,2e9,1,0,1, "cadical_kitten ticks limit") \ OPTION( elimeffort, 1e3, 1,1e5,1,0,1, "relative efficiency per mille") \ OPTION( elimequivs, 1, 0, 1,0,0,1, "find equivalence gates") \ OPTION( elimint, 2e3, 1,2e9,0,0,1, "elimination interval") \ @@ -271,7 +275,7 @@ OPTION( walkredundant, 0, 0, 1,0,0,1, "walk redundant clauses too") \ #define LOGOPT(...) /**/ #endif -#ifdef QUIET +#ifdef CADICAL_QUIET #define QUTOPT(...) /**/ #else #define QUTOPT OPTION @@ -357,7 +361,7 @@ public: // that the following options are really allocated directly after it. // inline int &val (size_t idx) { - assert (idx < number_of_options); + CADICAL_assert (idx < number_of_options); return (&__start_of_options__ + 1)[idx]; } @@ -406,11 +410,13 @@ public: }; inline int &Option::val (Options *opts) { - assert (Options::table <= this && + CADICAL_assert (Options::table <= this && this < Options::table + number_of_options); return opts->val (this - Options::table); } } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/parse.hpp b/src/sat/cadical/parse.hpp index c90c9f5a9..a37117f43 100644 --- a/src/sat/cadical/parse.hpp +++ b/src/sat/cadical/parse.hpp @@ -1,9 +1,13 @@ #ifndef _parse_hpp_INCLUDED #define _parse_hpp_INCLUDED +#include "global.h" + #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // Factors out common functions for parsing of DIMACS and solution files. @@ -69,4 +73,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/phases.hpp b/src/sat/cadical/phases.hpp index fe3459415..98e54d439 100644 --- a/src/sat/cadical/phases.hpp +++ b/src/sat/cadical/phases.hpp @@ -1,6 +1,10 @@ #ifndef _phases_hpp_INCLUDED #define _phases_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Phases { @@ -15,4 +19,6 @@ struct Phases { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/profile.hpp b/src/sat/cadical/profile.hpp index 4267eb29c..f3799389b 100644 --- a/src/sat/cadical/profile.hpp +++ b/src/sat/cadical/profile.hpp @@ -1,8 +1,12 @@ #ifndef _profiles_h_INCLUDED #define _profiles_h_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + /*------------------------------------------------------------------------*/ -#ifndef QUIET +#ifndef CADICAL_QUIET /*------------------------------------------------------------------------*/ namespace CaDiCaL { @@ -131,11 +135,11 @@ struct Profiles { } // namespace CaDiCaL -#define NON_QUIET_PROFILE_CODE(CODE) CODE +#define NON_CADICAL_QUIET_PROFILE_CODE(CODE) CODE -#else // !defined(QUIET) +#else // !defined(CADICAL_QUIET) -#define NON_QUIET_PROFILE_CODE(CODE) /**/ +#define NON_CADICAL_QUIET_PROFILE_CODE(CODE) /**/ #endif @@ -145,7 +149,7 @@ struct Profiles { #define START(P) \ do { \ - NON_QUIET_PROFILE_CODE ( \ + NON_CADICAL_QUIET_PROFILE_CODE ( \ if (internal->profiles.P.level <= internal->opts.profile) \ internal->start_profiling (internal->profiles.P, \ internal->time ());) \ @@ -153,7 +157,7 @@ struct Profiles { #define STOP(P) \ do { \ - NON_QUIET_PROFILE_CODE ( \ + NON_CADICAL_QUIET_PROFILE_CODE ( \ if (internal->profiles.P.level <= internal->opts.profile) \ internal->stop_profiling (internal->profiles.P, \ internal->time ());) \ @@ -167,10 +171,10 @@ struct Profiles { #define START_SIMPLIFIER(S, M) \ do { \ - NON_QUIET_PROFILE_CODE (const double N = time (); \ + NON_CADICAL_QUIET_PROFILE_CODE (const double N = time (); \ const int L = internal->opts.profile;) \ if (!preprocessing && !lookingahead) { \ - NON_QUIET_PROFILE_CODE ( \ + NON_CADICAL_QUIET_PROFILE_CODE ( \ if (stable && internal->profiles.stable.level <= L) \ internal->stop_profiling (internal->profiles.stable, N); \ if (!stable && internal->profiles.unstable.level <= L) \ @@ -179,7 +183,7 @@ struct Profiles { internal->stop_profiling (internal->profiles.search, N);) \ reset_mode (SEARCH); \ } \ - NON_QUIET_PROFILE_CODE ( \ + NON_CADICAL_QUIET_PROFILE_CODE ( \ if (internal->profiles.simplify.level <= L) \ internal->start_profiling (internal->profiles.simplify, N); \ if (internal->profiles.S.level <= L) \ @@ -192,7 +196,7 @@ struct Profiles { #define STOP_SIMPLIFIER(S, M) \ do { \ - NON_QUIET_PROFILE_CODE ( \ + NON_CADICAL_QUIET_PROFILE_CODE ( \ const double N = time (); const int L = internal->opts.profile; \ if (internal->profiles.S.level <= L) \ internal->stop_profiling (internal->profiles.S, N); \ @@ -201,7 +205,7 @@ struct Profiles { reset_mode (M); \ reset_mode (SIMPLIFY); \ if (!preprocessing && !lookingahead) { \ - NON_QUIET_PROFILE_CODE ( \ + NON_CADICAL_QUIET_PROFILE_CODE ( \ if (internal->profiles.search.level <= L) \ internal->start_profiling (internal->profiles.search, N); \ if (stable && internal->profiles.stable.level <= L) \ @@ -218,8 +222,8 @@ struct Profiles { #define START_INNER_WALK() \ do { \ require_mode (SEARCH); \ - assert (!preprocessing); \ - NON_QUIET_PROFILE_CODE ( \ + CADICAL_assert (!preprocessing); \ + NON_CADICAL_QUIET_PROFILE_CODE ( \ const double N = time (); const int L = internal->opts.profile; \ if (stable && internal->profiles.stable.level <= L) \ internal->stop_profiling (internal->profiles.stable, N); \ @@ -236,9 +240,9 @@ struct Profiles { #define STOP_INNER_WALK() \ do { \ require_mode (SEARCH); \ - assert (!preprocessing); \ + CADICAL_assert (!preprocessing); \ reset_mode (WALK); \ - NON_QUIET_PROFILE_CODE ( \ + NON_CADICAL_QUIET_PROFILE_CODE ( \ const double N = time (); const int L = internal->opts.profile; \ if (internal->profiles.walk.level <= L) \ internal->stop_profiling (internal->profiles.walk, N); \ @@ -255,8 +259,8 @@ struct Profiles { #define START_OUTER_WALK() \ do { \ require_mode (SEARCH); \ - assert (!preprocessing); \ - NON_QUIET_PROFILE_CODE (START (walk);) \ + CADICAL_assert (!preprocessing); \ + NON_CADICAL_QUIET_PROFILE_CODE (START (walk);) \ set_mode (WALK); \ } while (0) @@ -266,9 +270,11 @@ struct Profiles { #define STOP_OUTER_WALK() \ do { \ require_mode (SEARCH); \ - assert (!preprocessing); \ + CADICAL_assert (!preprocessing); \ reset_mode (WALK); \ - NON_QUIET_PROFILE_CODE (STOP (walk);) \ + NON_CADICAL_QUIET_PROFILE_CODE (STOP (walk);) \ } while (0) +ABC_NAMESPACE_CXX_HEADER_END + #endif // ifndef _profiles_h_INCLUDED diff --git a/src/sat/cadical/proof.hpp b/src/sat/cadical/proof.hpp index 36ae2f493..69be6b080 100644 --- a/src/sat/cadical/proof.hpp +++ b/src/sat/cadical/proof.hpp @@ -1,6 +1,10 @@ #ifndef _proof_h_INCLUDED #define _proof_h_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { /*------------------------------------------------------------------------*/ @@ -111,4 +115,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/queue.hpp b/src/sat/cadical/queue.hpp index 3b98bed17..14e1f2da7 100644 --- a/src/sat/cadical/queue.hpp +++ b/src/sat/cadical/queue.hpp @@ -1,8 +1,12 @@ #ifndef _queue_hpp_INCLUDED #define _queue_hpp_INCLUDED +#include "global.h" + #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // Links for double linked decision queue. @@ -61,4 +65,6 @@ struct Queue { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/radix.hpp b/src/sat/cadical/radix.hpp index a28a80aee..4c4d7b574 100644 --- a/src/sat/cadical/radix.hpp +++ b/src/sat/cadical/radix.hpp @@ -1,11 +1,15 @@ #ifndef _radix_hpp_INCLUDED #define _radix_hpp_INCLUDED +#include "global.h" + #include #include #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { using namespace std; @@ -42,7 +46,7 @@ template void rsort (I first, I last, Rank rank) { typedef typename iterator_traits::value_type T; typedef typename Rank::Type R; - assert (first <= last); + CADICAL_assert (first <= last); const size_t n = last - first; if (n <= 1) return; @@ -119,7 +123,7 @@ template void rsort (I first, I last, Rank rank) { } if (!initialized) { - assert (&*c == &*a); // MS VC++ + CADICAL_assert (&*c == &*a); // MS VC++ v.resize (n); b = v.begin (); initialized = true; @@ -145,9 +149,9 @@ template void rsort (I first, I last, Rank rank) { delete[] count; #endif -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG for (I p = first; p + 1 != last; p++) - assert (rank (p[0]) <= rank (p[1])); + CADICAL_assert (rank (p[0]) <= rank (p[1])); #endif } @@ -177,4 +181,6 @@ template void rsort (I first, I last, Rank rank) { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/random.h b/src/sat/cadical/random.h index 7f00fbf99..62fe5ed19 100644 --- a/src/sat/cadical/random.h +++ b/src/sat/cadical/random.h @@ -1,10 +1,14 @@ #ifndef _random_h_INCLUDED #define _random_h_INCLUDED +#include "global.h" + #include #include #include +ABC_NAMESPACE_HEADER_START + typedef uint64_t generator; static inline uint64_t kissat_next_random64 (generator *rng) { @@ -19,17 +23,17 @@ static inline unsigned kissat_next_random32 (generator *rng) { static inline unsigned kissat_pick_random (generator *rng, unsigned l, unsigned r) { - assert (l <= r); + CADICAL_assert (l <= r); if (l == r) return l; const unsigned delta = r - l; const unsigned tmp = kissat_next_random32 (rng); const double fraction = tmp / 4294967296.0; - assert (0 <= fraction), assert (fraction < 1); + CADICAL_assert (0 <= fraction), CADICAL_assert (fraction < 1); const unsigned scaled = delta * fraction; - assert (scaled < delta); + CADICAL_assert (scaled < delta); const unsigned res = l + scaled; - assert (l <= res), assert (res < r); + CADICAL_assert (l <= res), CADICAL_assert (res < r); return res; } @@ -41,4 +45,6 @@ static inline double kissat_pick_double (generator *rng) { return kissat_next_random32 (rng) / 4294967296.0; } +ABC_NAMESPACE_HEADER_END + #endif diff --git a/src/sat/cadical/random.hpp b/src/sat/cadical/random.hpp index c57c03bef..a5c78d38a 100644 --- a/src/sat/cadical/random.hpp +++ b/src/sat/cadical/random.hpp @@ -1,8 +1,12 @@ #ifndef _random_hpp_INCLUDED #define _random_hpp_INCLUDED +#include "global.h" + #include +ABC_NAMESPACE_CXX_HEADER_START + // Random number generator. namespace CaDiCaL { @@ -32,7 +36,7 @@ public: uint64_t next () { state *= 6364136223846793005ul; state += 1442695040888963407ul; - assert (state); + CADICAL_assert (state); return state; } @@ -50,7 +54,7 @@ public: // Generate 'int' value in the range '[l,r]'. // int pick_int (int l, int r) { - assert (l <= r); + CADICAL_assert (l <= r); const unsigned delta = 1 + r - (unsigned) l; unsigned tmp = generate (), scaled; if (delta) { @@ -59,13 +63,13 @@ public: } else scaled = tmp; const int res = scaled + l; - assert (l <= res); - assert (res <= r); + CADICAL_assert (l <= res); + CADICAL_assert (res <= r); return res; } int pick_log (int l, int r) { - assert (l <= r); + CADICAL_assert (l <= r); const unsigned delta = 1 + r - (unsigned) l; int log_delta = delta ? 0 : 32; while (log_delta < 32 && (1u << log_delta) < delta) @@ -77,22 +81,24 @@ public: if (delta) tmp %= delta; const int res = l + tmp; - assert (l <= res), assert (res <= r); + CADICAL_assert (l <= res), CADICAL_assert (res <= r); return res; } // Generate 'double' value in the range '[l,r]'. // double pick_double (double l, double r) { - assert (l <= r); + CADICAL_assert (l <= r); double res = (r - l) * generate_double (); res += l; - assert (l <= res); - assert (res <= r); + CADICAL_assert (l <= res); + CADICAL_assert (res <= r); return res; } }; } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/range.hpp b/src/sat/cadical/range.hpp index f30c34d4b..7049dab42 100644 --- a/src/sat/cadical/range.hpp +++ b/src/sat/cadical/range.hpp @@ -1,8 +1,12 @@ #ifndef _range_hpp_INCLUDED #define _range_hpp_INCLUDED +#include "global.h" + #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Clause; @@ -62,9 +66,9 @@ class Range { int &n; public: - iterator begin () const { return assert (n >= 0), iterator (inc (0)); } - iterator end () const { return assert (n >= 0), iterator (inc (n)); } - Range (int &m) : n (m) { assert (m >= 0); } + iterator begin () const { return CADICAL_assert (n >= 0), iterator (inc (0)); } + iterator end () const { return CADICAL_assert (n >= 0), iterator (inc (n)); } + Range (int &m) : n (m) { CADICAL_assert (m >= 0); } }; // Same, but iterating over literals '-1,1,-2,2,....,-max_var,max_var'. @@ -89,11 +93,13 @@ class Sange { int &n; public: - iterator begin () const { return assert (n >= 0), iterator (inc (0)); } - iterator end () const { return assert (n >= 0), iterator (inc (n)); } - Sange (int &m) : n (m) { assert (m >= 0); } + iterator begin () const { return CADICAL_assert (n >= 0), iterator (inc (0)); } + iterator end () const { return CADICAL_assert (n >= 0), iterator (inc (n)); } + Sange (int &m) : n (m) { CADICAL_assert (m >= 0); } }; } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/reap.hpp b/src/sat/cadical/reap.hpp index 9986b95b9..1ea10e6e3 100644 --- a/src/sat/cadical/reap.hpp +++ b/src/sat/cadical/reap.hpp @@ -1,9 +1,13 @@ #ifndef _reap_h_INCLUDED #define _reap_h_INCLUDED +#include "global.h" + #include #include +ABC_NAMESPACE_CXX_HEADER_START + class Reap { public: Reap (); @@ -25,4 +29,6 @@ private: std::vector buckets[33]; }; +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/reluctant.hpp b/src/sat/cadical/reluctant.hpp index 0448672e4..495382b56 100644 --- a/src/sat/cadical/reluctant.hpp +++ b/src/sat/cadical/reluctant.hpp @@ -1,9 +1,13 @@ #ifndef _reluctant_hpp_INCLUDED #define _reluctant_hpp_INCLUDED +#include "global.h" + #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // This is Donald Knuth's version of the Luby restart sequence which he @@ -31,7 +35,7 @@ public: Reluctant () : period (0), trigger (false) {} void enable (int p, int64_t l) { - assert (p > 0); + CADICAL_assert (p > 0); u = v = 1; period = countdown = p; trigger = false; @@ -79,4 +83,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/resources.hpp b/src/sat/cadical/resources.hpp index 377c95f3d..852668b32 100644 --- a/src/sat/cadical/resources.hpp +++ b/src/sat/cadical/resources.hpp @@ -1,8 +1,12 @@ #ifndef _resources_hpp_INCLUDED #define _resources_hpp_INCLUDED +#include "global.h" + #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { double absolute_real_time (); @@ -13,4 +17,6 @@ uint64_t current_resident_set_size (); } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif // ifndef _resources_hpp_INCLUDED diff --git a/src/sat/cadical/score.hpp b/src/sat/cadical/score.hpp index 9fde98af3..88196121e 100644 --- a/src/sat/cadical/score.hpp +++ b/src/sat/cadical/score.hpp @@ -1,6 +1,10 @@ #ifndef _score_hpp_INCLUDED #define _score_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct score_smaller { @@ -13,4 +17,6 @@ typedef heap ScoreSchedule; } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/signal.hpp b/src/sat/cadical/signal.hpp index 5984b66f1..1dc2fb3b7 100644 --- a/src/sat/cadical/signal.hpp +++ b/src/sat/cadical/signal.hpp @@ -1,6 +1,10 @@ #ifndef _signal_hpp_INCLUDED #define _signal_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // Helper class for handling signals in applications. @@ -10,7 +14,7 @@ public: Handler () {} virtual ~Handler () {} virtual void catch_signal (int sig) = 0; -#ifndef __WIN32 +#ifndef WIN32 virtual void catch_alarm (); #endif }; @@ -20,7 +24,7 @@ class Signal { public: static void set (Handler *); static void reset (); -#ifndef __WIN32 +#ifndef WIN32 static void alarm (int seconds); static void reset_alarm (); #endif @@ -30,4 +34,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/stack.h b/src/sat/cadical/stack.h index 21082e525..89f270388 100644 --- a/src/sat/cadical/stack.h +++ b/src/sat/cadical/stack.h @@ -1,8 +1,12 @@ #ifndef _stack_h_INCLUDED #define _stack_h_INCLUDED +#include "global.h" + #include +ABC_NAMESPACE_HEADER_START + #define STACK(TYPE) \ struct { \ TYPE *begin; \ @@ -20,17 +24,17 @@ (S).begin = (S).end = (S).allocated = 0; \ } while (0) -#define TOP_STACK(S) (END_STACK (S)[assert (!EMPTY_STACK (S)), -1]) +#define TOP_STACK(S) (END_STACK (S)[CADICAL_assert (!EMPTY_STACK (S)), -1]) #define PEEK_STACK(S, N) \ - (BEGIN_STACK (S)[assert ((N) < SIZE_STACK (S)), (N)]) + (BEGIN_STACK (S)[CADICAL_assert ((N) < SIZE_STACK (S)), (N)]) #define POKE_STACK(S, N, E) \ do { \ PEEK_STACK (S, N) = (E); \ } while (0) -#define POP_STACK(S) (assert (!EMPTY_STACK (S)), *--(S).end) +#define POP_STACK(S) (CADICAL_assert (!EMPTY_STACK (S)), *--(S).end) #define PUSH_STACK(S, E) \ do { \ @@ -51,14 +55,14 @@ #define RESIZE_STACK(S, NEW_SIZE) \ do { \ const size_t TMP_NEW_SIZE = (NEW_SIZE); \ - assert (TMP_NEW_SIZE <= SIZE_STACK (S)); \ + CADICAL_assert (TMP_NEW_SIZE <= SIZE_STACK (S)); \ (S).end = (S).begin + TMP_NEW_SIZE; \ } while (0) #define SET_END_OF_STACK(S, P) \ do { \ - assert (BEGIN_STACK (S) <= (P)); \ - assert ((P) <= END_STACK (S)); \ + CADICAL_assert (BEGIN_STACK (S) <= (P)); \ + CADICAL_assert ((P) <= END_STACK (S)); \ if ((P) == END_STACK (S)) \ break; \ (S).end = (P); \ @@ -72,12 +76,12 @@ #define REMOVE_STACK(T, S, E) \ do { \ - assert (!EMPTY_STACK (S)); \ + CADICAL_assert (!EMPTY_STACK (S)); \ T *END_REMOVE_STACK = END_STACK (S); \ T *P_REMOVE_STACK = BEGIN_STACK (S); \ while (*P_REMOVE_STACK != (E)) { \ P_REMOVE_STACK++; \ - assert (P_REMOVE_STACK != END_REMOVE_STACK); \ + CADICAL_assert (P_REMOVE_STACK != END_REMOVE_STACK); \ } \ P_REMOVE_STACK++; \ while (P_REMOVE_STACK != END_REMOVE_STACK) { \ @@ -107,4 +111,6 @@ typedef STACK (unsigned) unsigneds; // clang-format on +ABC_NAMESPACE_HEADER_END + #endif diff --git a/src/sat/cadical/stats.hpp b/src/sat/cadical/stats.hpp index 7eb9e9be5..4a6afa9c9 100644 --- a/src/sat/cadical/stats.hpp +++ b/src/sat/cadical/stats.hpp @@ -1,10 +1,14 @@ #ifndef _stats_hpp_INCLUDED #define _stats_hpp_INCLUDED +#include "global.h" + #include #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Internal; @@ -367,4 +371,6 @@ struct Stats { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/sweep.hpp b/src/sat/cadical/sweep.hpp index 04d40ead1..ad093e070 100644 --- a/src/sat/cadical/sweep.hpp +++ b/src/sat/cadical/sweep.hpp @@ -1,6 +1,10 @@ #ifndef _sweep_hpp_INCLUDED #define _sweep_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Internal; @@ -8,7 +12,7 @@ struct Internal; struct sweep_proof_clause { unsigned sweep_id; // index for sweeper.clauses int64_t cad_id; // cadical id - unsigned kit_id; // kitten id + unsigned kit_id; // cadical_kitten id bool learned; vector literals; vector chain; // lrat @@ -58,4 +62,6 @@ struct Sweeper { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/terminal.hpp b/src/sat/cadical/terminal.hpp index 71090a1d5..a99346b7c 100644 --- a/src/sat/cadical/terminal.hpp +++ b/src/sat/cadical/terminal.hpp @@ -1,6 +1,10 @@ #ifndef _terminal_hpp_INCLUDED #define _terminal_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { class Terminal { @@ -11,14 +15,14 @@ class Terminal { bool reset_on_exit; // Reset on exit. void escape () { - assert (connected); + CADICAL_assert (connected); fputs ("\033[", file); } void color (int color, bool bright) { if (!use_colors) return; - assert (connected); + CADICAL_assert (connected); escape (); fputc (bright ? '1' : '0', file); fprintf (file, ";%dm", color); @@ -95,4 +99,6 @@ extern Terminal terr; // Terminal of 'stderr' (file descriptor '2') } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/testing.hpp b/src/sat/cadical/testing.hpp index 16dabb4b4..f90569e5c 100644 --- a/src/sat/cadical/testing.hpp +++ b/src/sat/cadical/testing.hpp @@ -1,12 +1,16 @@ #ifndef _testing_hpp_INCLUDED #define _testing_hpp_INCLUDED +#include "global.h" + // This class provides access to 'internal' which should only be used for // internal testing and not for any other purpose as 'internal' is supposed // to be hidden. #include "cadical.hpp" +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { class Testing { @@ -21,4 +25,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/tracer.hpp b/src/sat/cadical/tracer.hpp index 6af089ed3..d1a9f1137 100644 --- a/src/sat/cadical/tracer.hpp +++ b/src/sat/cadical/tracer.hpp @@ -1,9 +1,13 @@ #ifndef _tracer_hpp_INCLUDED #define _tracer_hpp_INCLUDED +#include "global.h" + #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Internal; @@ -174,4 +178,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/util.hpp b/src/sat/cadical/util.hpp index 9a971709c..3882c6ede 100644 --- a/src/sat/cadical/util.hpp +++ b/src/sat/cadical/util.hpp @@ -1,10 +1,14 @@ #ifndef _util_hpp_INCLUDED #define _util_hpp_INCLUDED +#include "global.h" + #include #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { using namespace std; @@ -47,25 +51,25 @@ inline bool contained (int64_t c, int64_t l, int64_t u) { } inline bool aligned (size_t n, size_t alignment) { - assert (is_power_of_two (alignment)); + CADICAL_assert (is_power_of_two (alignment)); const size_t mask = alignment - 1; return !(n & mask); } inline size_t align (size_t n, size_t alignment) { size_t res = n; - assert (is_power_of_two (alignment)); + CADICAL_assert (is_power_of_two (alignment)); const size_t mask = alignment - 1; if (res & mask) res = (res | mask) + 1; - assert (aligned (res, alignment)); + CADICAL_assert (aligned (res, alignment)); return res; } /*------------------------------------------------------------------------*/ inline bool parity (unsigned a) { - assert (sizeof a == 4); + CADICAL_assert (sizeof a == 4); unsigned tmp = a; tmp ^= (tmp >> 16); tmp ^= (tmp >> 8); @@ -87,7 +91,7 @@ template void erase_vector (std::vector &v) { if (v.capacity ()) { std::vector ().swap (v); } - assert (!v.capacity ()); // not guaranteed though + CADICAL_assert (!v.capacity ()); // not guaranteed though } // The standard 'Effective STL' way (though not guaranteed) to shrink the @@ -98,7 +102,7 @@ template void shrink_vector (std::vector &v) { if (v.capacity () > v.size ()) { std::vector (v).swap (v); } - assert (v.capacity () == v.size ()); // not guaranteed though + CADICAL_assert (v.capacity () == v.size ()); // not guaranteed though } template @@ -164,4 +168,6 @@ uint64_t hash_string (const char *str); } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/var.hpp b/src/sat/cadical/var.hpp index 38b52c811..8793afbc4 100644 --- a/src/sat/cadical/var.hpp +++ b/src/sat/cadical/var.hpp @@ -1,6 +1,10 @@ #ifndef _var_hpp_INCLUDED #define _var_hpp_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Clause; @@ -19,4 +23,6 @@ struct Var { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/veripbtracer.hpp b/src/sat/cadical/veripbtracer.hpp index d8e3dafdf..b9c47362f 100644 --- a/src/sat/cadical/veripbtracer.hpp +++ b/src/sat/cadical/veripbtracer.hpp @@ -1,6 +1,10 @@ #ifndef _veripbtracer_h_INCLUDED #define _veripbtracer_h_INCLUDED +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + class FileTracer; namespace CaDiCaL { @@ -15,7 +19,7 @@ class VeripbTracer : public FileTracer { Internal *internal; File *file; -#ifndef NDEBUG +#ifndef CADICAL_NDEBUG bool binary; #endif bool with_antecedents; @@ -47,7 +51,7 @@ class VeripbTracer : public FileTracer { bool find_and_delete (const int64_t); // find clause position in hash table -#ifndef QUIET +#ifndef CADICAL_QUIET int64_t added, deleted; #endif vector delete_ids; @@ -89,7 +93,7 @@ public: void weaken_minus (int64_t, const vector &) override; void strengthen (int64_t) override; -#ifndef QUIET +#ifndef CADICAL_QUIET void print_statistics (); #endif bool closed () override; @@ -99,4 +103,6 @@ public: } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/version.hpp b/src/sat/cadical/version.hpp index aaa07788e..d63aefa35 100644 --- a/src/sat/cadical/version.hpp +++ b/src/sat/cadical/version.hpp @@ -1,3 +1,7 @@ +#include "global.h" + +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { const char *version (); @@ -11,3 +15,5 @@ const char *date (); const char *flags (); } // namespace CaDiCaL + +ABC_NAMESPACE_CXX_HEADER_END diff --git a/src/sat/cadical/vivify.hpp b/src/sat/cadical/vivify.hpp index a23acb9ff..a5c6c0e10 100644 --- a/src/sat/cadical/vivify.hpp +++ b/src/sat/cadical/vivify.hpp @@ -1,12 +1,16 @@ #ifndef _vivify_hpp_INCLUDED #define _vivify_hpp_INCLUDED +#include "global.h" + #include "util.hpp" #include #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { struct Clause; @@ -59,4 +63,6 @@ struct Vivifier { } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif diff --git a/src/sat/cadical/watch.hpp b/src/sat/cadical/watch.hpp index 3adc4c47f..1acab094b 100644 --- a/src/sat/cadical/watch.hpp +++ b/src/sat/cadical/watch.hpp @@ -1,9 +1,13 @@ #ifndef _watch_hpp_INCLUDED #define _watch_hpp_INCLUDED +#include "global.h" + #include #include +ABC_NAMESPACE_CXX_HEADER_START + namespace CaDiCaL { // Watch lists for CDCL search. The blocking literal (see also comments @@ -51,7 +55,7 @@ inline void remove_watch (Watches &ws, Clause *clause) { if (w.clause == clause) i--; } - assert (i + 1 == end); + CADICAL_assert (i + 1 == end); ws.resize (i - ws.begin ()); } @@ -62,11 +66,13 @@ inline void update_watch_size (Watches &ws, int blit, Clause *conflict) { for (Watch &w : ws) { if (w.clause == conflict) w.size = size, w.blit = blit, found = true; - assert (w.clause->garbage || w.size == 2 || w.clause->size != 2); + CADICAL_assert (w.clause->garbage || w.size == 2 || w.clause->size != 2); } - assert (found), (void) found; + CADICAL_assert (found), (void) found; } } // namespace CaDiCaL +ABC_NAMESPACE_CXX_HEADER_END + #endif