satSolver.h 22.2 KB
Newer Older
Alan Mishchenko committed
1 2 3 4
/**************************************************************************************************
MiniSat -- Copyright (c) 2005, Niklas Sorensson
http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/

5
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
Alan Mishchenko committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************************/
// Modified to compile with MS Visual Studio 6.0 by Alan Mishchenko

22 23
#ifndef ABC__sat__bsat__satSolver_h
#define ABC__sat__bsat__satSolver_h
Alan Mishchenko committed
24

25

Alan Mishchenko committed
26 27 28 29
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
Alan Mishchenko committed
30 31

#include "satVec.h"
32
#include "satClause.h"
33
#include "misc/util/utilDouble.h"
Alan Mishchenko committed
34

35 36
ABC_NAMESPACE_HEADER_START

Alan Mishchenko committed
37 38 39 40 41 42 43
//=================================================================================================
// Public interface:

struct sat_solver_t;
typedef struct sat_solver_t sat_solver;

extern sat_solver* sat_solver_new(void);
44
extern sat_solver* zsat_solver_new_seed(double seed);
Alan Mishchenko committed
45 46
extern void        sat_solver_delete(sat_solver* s);

47
extern int         sat_solver_addclause(sat_solver* s, lit* begin, lit* end);
48
extern int         sat_solver_clause_new(sat_solver* s, lit* begin, lit* end, int learnt);
49
extern int         sat_solver_simplify(sat_solver* s);
Alan Mishchenko committed
50
extern int         sat_solver_solve(sat_solver* s, lit* begin, lit* end, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, ABC_INT64_T nConfLimitGlobal, ABC_INT64_T nInsLimitGlobal);
51
extern int         sat_solver_solve_internal(sat_solver* s);
52
extern int         sat_solver_solve_lexsat(sat_solver* s, int * pLits, int nLits);
53
extern int         sat_solver_minimize_assumptions( sat_solver* s, int * pLits, int nLits, int nConfLimit );
54
extern int         sat_solver_minimize_assumptions2( sat_solver* s, int * pLits, int nLits, int nConfLimit );
55 56 57
extern int         sat_solver_push(sat_solver* s, int p);
extern void        sat_solver_pop(sat_solver* s);
extern void        sat_solver_set_resource_limits(sat_solver* s, ABC_INT64_T nConfLimit, ABC_INT64_T nInsLimit, ABC_INT64_T nConfLimitGlobal, ABC_INT64_T nInsLimitGlobal);
58
extern void        sat_solver_restart( sat_solver* s );
59
extern void        zsat_solver_restart_seed( sat_solver* s, double seed );
60
extern void        sat_solver_rollback( sat_solver* s );
Alan Mishchenko committed
61 62 63 64

extern int         sat_solver_nvars(sat_solver* s);
extern int         sat_solver_nclauses(sat_solver* s);
extern int         sat_solver_nconflicts(sat_solver* s);
65
extern double      sat_solver_memory(sat_solver* s);
66
extern int         sat_solver_count_assigned(sat_solver* s);
Alan Mishchenko committed
67

68
extern int         sat_solver_addvar(sat_solver* s);
Alan Mishchenko committed
69
extern void        sat_solver_setnvars(sat_solver* s,int n);
70
extern int         sat_solver_get_var_value(sat_solver* s, int v);
71
extern void        sat_solver_set_var_activity(sat_solver* s, int * pVars, int nVars);
72

Alan Mishchenko committed
73 74
extern void        Sat_SolverWriteDimacs( sat_solver * p, char * pFileName, lit* assumptionsBegin, lit* assumptionsEnd, int incrementVars );
extern void        Sat_SolverPrintStats( FILE * pFile, sat_solver * p );
Alan Mishchenko committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
extern int *       Sat_SolverGetModel( sat_solver * p, int * pVars, int nVars );
extern void        Sat_SolverDoubleClauses( sat_solver * p, int iVar );

// trace recording
extern void        Sat_SolverTraceStart( sat_solver * pSat, char * pName );
extern void        Sat_SolverTraceStop( sat_solver * pSat );
extern void        Sat_SolverTraceWrite( sat_solver * pSat, int * pBeg, int * pEnd, int fRoot );

// clause storage
extern void        sat_solver_store_alloc( sat_solver * s );
extern void        sat_solver_store_write( sat_solver * s, char * pFileName );
extern void        sat_solver_store_free( sat_solver * s );
extern void        sat_solver_store_mark_roots( sat_solver * s );
extern void        sat_solver_store_mark_clauses_a( sat_solver * s );
extern void *      sat_solver_store_release( sat_solver * s ); 
Alan Mishchenko committed
90 91 92 93

//=================================================================================================
// Solver representation:

94 95
//struct clause_t;
//typedef struct clause_t clause;
Alan Mishchenko committed
96

97 98 99
struct varinfo_t;
typedef struct varinfo_t varinfo;

Alan Mishchenko committed
100 101
struct sat_solver_t
{
102 103 104 105
    int         size;          // nof variables
    int         cap;           // size of varmaps
    int         qhead;         // Head index of queue.
    int         qtail;         // Tail index of queue.
Alan Mishchenko committed
106 107

    // clauses
108
    Sat_Mem_t   Mem;
109 110 111
    int         hLearnts;      // the first learnt clause
    int         hBinary;       // the special binary clause
    clause *    binary;
112
    veci*       wlists;        // watcher lists
Alan Mishchenko committed
113

114 115 116 117 118
    // rollback
    int         iVarPivot;     // the pivot for variables
    int         iTrailPivot;   // the pivot for trail
    int         hProofPivot;   // the pivot for proof records

Alan Mishchenko committed
119
    // activities
120 121 122 123 124 125 126 127 128 129 130
    int         VarActType;
    int         ClaActType;
    word        var_inc;       // Amount to bump next variable with.
    word        var_inc2;      // Amount to bump next variable with.
    word        var_decay;     // INVERSE decay factor for variable activity: stores 1/decay. 
    word*       activity;      // A heuristic measurement of the activity of a variable.
    word*       activity2;     // backup variable activity
    unsigned    cla_inc;       // Amount to bump next clause with.
    unsigned    cla_decay;     // INVERSE decay factor for clause activity: stores 1/decay.
    veci        act_clas;      // contain clause activities

131 132
    char *      pFreqs;        // how many times this variable was assigned a value
    int         nVarUsed;
Alan Mishchenko committed
133

134 135 136 137 138
//    varinfo *   vi;            // variable information
    int*        levels;        //
    char*       assigns;       // Current values of variables.
    char*       polarity;      //
    char*       tags;          //
139
    char*       loads;         //
140 141 142 143 144 145 146 147 148

    int*        orderpos;      // Index in variable order.
    int*        reasons;       //
    lit*        trail;
    veci        tagged;        // (contains: var)
    veci        stack;         // (contains: var)

    veci        order;         // Variable order. (heap) (contains: var)
    veci        trail_lim;     // Separator indices for different decision levels in 'trail'. (contains: int)
149 150
//    veci        model;         // If problem is solved, this vector contains the model (contains: lbool).
    int *       model;         // If problem is solved, this vector contains the model (contains: lbool).
151 152 153 154 155 156 157 158 159
    veci        conf_final;    // If problem is unsatisfiable (possibly under assumptions),
                               // this vector represent the final conflict clause expressed in the assumptions.

    int         root_level;    // Level of first proper decision.
    int         simpdb_assigns;// Number of top-level assignments at last 'simplifyDB()'.
    int         simpdb_props;  // Number of propagations before next 'simplifyDB()'.
    double      random_seed;
    double      progress_estimate;
    int         verbosity;     // Verbosity level. 0=silent, 1=some progress report, 2=everything
160
    int         fVerbose;
161
    int         fPrintClause;
162 163

    stats_t     stats;
164
    int         nLearntMax;    // max number of learned clauses
165 166 167
    int         nLearntStart;  // starting learned clause limit
    int         nLearntDelta;  // delta of learned clause limit
    int         nLearntRatio;  // ratio percentage of learned clauses
168
    int         nDBreduces;    // number of DB reductions
169 170 171

    ABC_INT64_T nConfLimit;    // external limit on the number of conflicts
    ABC_INT64_T nInsLimit;     // external limit on the number of implications
172
    abctime     nRuntimeLimit; // external limit on runtime
173 174 175 176 177

    veci        act_vars;      // variables whose activity has changed
    double*     factors;       // the activity factors
    int         nRestarts;     // the number of local restarts
    int         nCalls;        // the number of local restarts
178 179
    int         nCalls2;       // the number of local restarts
    veci        unit_lits;     // variables whose activity has changed
180
    veci        pivot_vars;    // pivot variables
181 182 183

    int         fSkipSimplify; // set to one to skip simplification of the clause database
    int         fNotUseRandom; // do not allow random decisions with a fixed probability
184
    int         fNoRestarts;   // disables periodic restarts
185 186

    int *       pGlobalVars;   // for experiments with global vars during interpolation
Alan Mishchenko committed
187
    // clause store
188 189
    void *      pStore;
    int         fSolved;
Alan Mishchenko committed
190 191

    // trace recording
192 193 194
    FILE *      pFile;
    int         nClauses;
    int         nRoots;
Alan Mishchenko committed
195

196
    veci        temp_clause;    // temporary storage for a CNF clause
197

198 199 200 201
    // assignment storage
    veci        user_vars;      // variable IDs
    veci        user_values;    // values of these variables

202 203 204
    // CNF loading
    void *      pCnfMan;           // external CNF manager
    int(*pCnfFunc)(void * p, int); // external callback
205 206 207 208

    // termination callback
    int         RunId;          // SAT id in this run
    int(*pFuncStop)(int);       // callback to terminate
Alan Mishchenko committed
209 210
};

211 212 213 214 215
static inline clause * clause_read( sat_solver * s, cla h )          
{ 
    return Sat_MemClauseHand( &s->Mem, h );      
}

216
static inline int sat_solver_var_value( sat_solver* s, int v )
Alan Mishchenko committed
217
{
218 219
    assert( v >= 0 && v < s->size );
    return (int)(s->model[v] == l_True);
Alan Mishchenko committed
220
}
221
static inline int sat_solver_var_literal( sat_solver* s, int v )
Alan Mishchenko committed
222
{
223 224
    assert( v >= 0 && v < s->size );
    return toLitCond( v, s->model[v] != l_True );
Alan Mishchenko committed
225
}
226 227 228 229 230
static inline void sat_solver_flip_print_clause( sat_solver* s )
{
    s->fPrintClause ^= 1;
}
static inline void sat_solver_act_var_clear(sat_solver* s) 
Alan Mishchenko committed
231 232
{
    int i;
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    if ( s->VarActType == 0 )
    {
        for (i = 0; i < s->size; i++)
            s->activity[i] = (1 << 10);
        s->var_inc = (1 << 5);
    }
    else if ( s->VarActType == 1 )
    {
        for (i = 0; i < s->size; i++)
            s->activity[i] = 0;
        s->var_inc = 1;
    }
    else if ( s->VarActType == 2 )
    {
        for (i = 0; i < s->size; i++)
            s->activity[i] = Xdbl_Const1();
        s->var_inc = Xdbl_Const1();
    }
    else assert(0);
Alan Mishchenko committed
252
}
253
static inline void sat_solver_compress(sat_solver* s) 
Alan Mishchenko committed
254 255 256 257 258
{
    if ( s->qtail != s->qhead )
    {
        int RetValue = sat_solver_simplify(s);
        assert( RetValue != 0 );
Alan Mishchenko committed
259
        (void) RetValue;
Alan Mishchenko committed
260 261
    }
}
262
static inline void sat_solver_delete_p( sat_solver ** ps )
263 264 265 266 267
{
    if ( *ps )
        sat_solver_delete( *ps );
    *ps = NULL;
}
268
static inline void sat_solver_clean_polarity(sat_solver* s, int * pVars, int nVars )
269 270 271 272 273
{
    int i;
    for ( i = 0; i < nVars; i++ )
        s->polarity[pVars[i]] = 0;
}
274
static inline void sat_solver_set_polarity(sat_solver* s, int * pVars, int nVars )
275 276 277 278 279 280 281
{
    int i;
    for ( i = 0; i < s->size; i++ )
        s->polarity[i] = 0;
    for ( i = 0; i < nVars; i++ )
        s->polarity[pVars[i]] = 1;
}
282
static inline void sat_solver_set_literal_polarity(sat_solver* s, int * pLits, int nLits )
283 284 285 286 287
{
    int i;
    for ( i = 0; i < nLits; i++ )
        s->polarity[Abc_Lit2Var(pLits[i])] = !Abc_LitIsCompl(pLits[i]);
}
Alan Mishchenko committed
288

289
static inline int sat_solver_final(sat_solver* s, int ** ppArray)
290 291 292 293 294
{
    *ppArray = s->conf_final.ptr;
    return s->conf_final.size;
}

295
static inline abctime sat_solver_set_runtime_limit(sat_solver* s, abctime Limit)
296
{
297
    abctime nRuntimeLimit = s->nRuntimeLimit;
298 299 300
    s->nRuntimeLimit = Limit;
    return nRuntimeLimit;
}
301

302
static inline int sat_solver_set_random(sat_solver* s, int fNotUseRandom)
303 304 305 306 307 308
{
    int fNotUseRandomOld = s->fNotUseRandom;
    s->fNotUseRandom = fNotUseRandom;
    return fNotUseRandomOld;
}

309 310 311 312 313 314 315 316 317
static inline void sat_solver_bookmark(sat_solver* s)
{
    assert( s->qhead == s->qtail );
    s->iVarPivot    = s->size;
    s->iTrailPivot  = s->qhead;
    Sat_MemBookMark( &s->Mem );
    if ( s->activity2 )
    {
        s->var_inc2 = s->var_inc;
318
        memcpy( s->activity2, s->activity, sizeof(word) * s->iVarPivot );
319 320
    }
}
321 322 323 324 325 326
static inline void sat_solver_set_pivot_variables( sat_solver* s, int * pPivots, int nPivots )
{
    s->pivot_vars.cap = nPivots;
    s->pivot_vars.size = nPivots;
    s->pivot_vars.ptr = pPivots;
}
327 328 329 330 331 332 333 334 335 336 337
static inline int sat_solver_count_usedvars(sat_solver* s)
{
    int i, nVars = 0;
    for ( i = 0; i < s->size; i++ )
        if ( s->pFreqs[i] )
        {
            s->pFreqs[i] = 0;
            nVars++;
        }
    return nVars;
}
338
static inline void sat_solver_set_runid( sat_solver *s, int id )               
339 340 341
{ 
    s->RunId      = id;  
}
342
static inline void sat_solver_set_stop_func( sat_solver *s, int (*fnct)(int) ) 
343 344 345
{ 
    s->pFuncStop = fnct; 
}
346

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
static inline int sat_solver_add_const( sat_solver * pSat, int iVar, int fCompl )
{
    lit Lits[1];
    int Cid;
    assert( iVar >= 0 );

    Lits[0] = toLitCond( iVar, fCompl );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 1 );
    assert( Cid );
    return 1;
}
static inline int sat_solver_add_buffer( sat_solver * pSat, int iVarA, int iVarB, int fCompl )
{
    lit Lits[2];
    int Cid;
    assert( iVarA >= 0 && iVarB >= 0 );

    Lits[0] = toLitCond( iVarA, 0 );
    Lits[1] = toLitCond( iVarB, !fCompl );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 2 );
367 368
    if ( Cid == 0 )
        return 0;
369 370 371 372 373
    assert( Cid );

    Lits[0] = toLitCond( iVarA, 1 );
    Lits[1] = toLitCond( iVarB, fCompl );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 2 );
374 375
    if ( Cid == 0 )
        return 0;
376 377 378
    assert( Cid );
    return 2;
}
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
static inline int sat_solver_add_buffer_enable( sat_solver * pSat, int iVarA, int iVarB, int iVarEn, int fCompl )
{
    lit Lits[3];
    int Cid;
    assert( iVarA >= 0 && iVarB >= 0 && iVarEn >= 0 );

    Lits[0] = toLitCond( iVarA, 0 );
    Lits[1] = toLitCond( iVarB, !fCompl );
    Lits[2] = toLitCond( iVarEn, 1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );

    Lits[0] = toLitCond( iVarA, 1 );
    Lits[1] = toLitCond( iVarB, fCompl );
    Lits[2] = toLitCond( iVarEn, 1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );
    return 2;
}
398
static inline int sat_solver_add_and( sat_solver * pSat, int iVar, int iVar0, int iVar1, int fCompl0, int fCompl1, int fCompl )
399 400 401 402
{
    lit Lits[3];
    int Cid;

403
    Lits[0] = toLitCond( iVar, !fCompl );
404 405 406 407
    Lits[1] = toLitCond( iVar0, fCompl0 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 2 );
    assert( Cid );

408
    Lits[0] = toLitCond( iVar, !fCompl );
409 410 411 412
    Lits[1] = toLitCond( iVar1, fCompl1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 2 );
    assert( Cid );

413
    Lits[0] = toLitCond( iVar, fCompl );
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
    Lits[1] = toLitCond( iVar0, !fCompl0 );
    Lits[2] = toLitCond( iVar1, !fCompl1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );
    return 3;
}
static inline int sat_solver_add_xor( sat_solver * pSat, int iVarA, int iVarB, int iVarC, int fCompl )
{
    lit Lits[3];
    int Cid;
    assert( iVarA >= 0 && iVarB >= 0 && iVarC >= 0 );

    Lits[0] = toLitCond( iVarA, !fCompl );
    Lits[1] = toLitCond( iVarB, 1 );
    Lits[2] = toLitCond( iVarC, 1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );

    Lits[0] = toLitCond( iVarA, !fCompl );
    Lits[1] = toLitCond( iVarB, 0 );
    Lits[2] = toLitCond( iVarC, 0 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );

    Lits[0] = toLitCond( iVarA, fCompl );
    Lits[1] = toLitCond( iVarB, 1 );
    Lits[2] = toLitCond( iVarC, 0 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );

    Lits[0] = toLitCond( iVarA, fCompl );
    Lits[1] = toLitCond( iVarB, 0 );
    Lits[2] = toLitCond( iVarC, 1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );
    return 4;
}
451
static inline int sat_solver_add_mux( sat_solver * pSat, int iVarZ, int iVarC, int iVarT, int iVarE, int iComplC, int iComplT, int iComplE, int iComplZ )
452 453 454 455 456
{
    lit Lits[3];
    int Cid;
    assert( iVarC >= 0 && iVarT >= 0 && iVarE >= 0 && iVarZ >= 0 );

457 458
    Lits[0] = toLitCond( iVarC, 1 ^ iComplC );
    Lits[1] = toLitCond( iVarT, 1 ^ iComplT );
459 460 461 462
    Lits[2] = toLitCond( iVarZ, 0 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );

463 464 465
    Lits[0] = toLitCond( iVarC, 1 ^ iComplC );
    Lits[1] = toLitCond( iVarT, 0 ^ iComplT );
    Lits[2] = toLitCond( iVarZ, 1 ^ iComplZ );
466 467 468
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );

469 470 471
    Lits[0] = toLitCond( iVarC, 0 ^ iComplC );
    Lits[1] = toLitCond( iVarE, 1 ^ iComplE );
    Lits[2] = toLitCond( iVarZ, 0 ^ iComplZ );
472 473 474
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );

475 476 477
    Lits[0] = toLitCond( iVarC, 0 ^ iComplC );
    Lits[1] = toLitCond( iVarE, 0 ^ iComplE );
    Lits[2] = toLitCond( iVarZ, 1 ^ iComplZ );
478 479 480 481 482 483
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );

    if ( iVarT == iVarE )
        return 4;

484 485 486
    Lits[0] = toLitCond( iVarT, 0 ^ iComplT );
    Lits[1] = toLitCond( iVarE, 0 ^ iComplE );
    Lits[2] = toLitCond( iVarZ, 1 ^ iComplZ );
487 488 489
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );

490 491 492
    Lits[0] = toLitCond( iVarT, 1 ^ iComplT );
    Lits[1] = toLitCond( iVarE, 1 ^ iComplE );
    Lits[2] = toLitCond( iVarZ, 0 ^ iComplZ );
493 494 495 496
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );
    return 6;
}
497
static inline int sat_solver_add_mux41( sat_solver * pSat, int iVarZ, int iVarC0, int iVarC1, int iVarD0, int iVarD1, int iVarD2, int iVarD3 )
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
{
    lit Lits[4];
    int Cid;
    assert( iVarC0 >= 0 && iVarC1 >= 0 && iVarD0 >= 0 && iVarD1 >= 0 && iVarD2 >= 0 && iVarD3 >= 0 && iVarZ >= 0 );

    Lits[0] = toLitCond( iVarD0, 1 );
    Lits[1] = toLitCond( iVarC0, 0 );
    Lits[2] = toLitCond( iVarC1, 0 );
    Lits[3] = toLitCond( iVarZ,  0 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 4 );
    assert( Cid );

    Lits[0] = toLitCond( iVarD1, 1 );
    Lits[1] = toLitCond( iVarC0, 1 );
    Lits[2] = toLitCond( iVarC1, 0 );
    Lits[3] = toLitCond( iVarZ,  0 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 4 );
    assert( Cid );

    Lits[0] = toLitCond( iVarD2, 1 );
    Lits[1] = toLitCond( iVarC0, 0 );
    Lits[2] = toLitCond( iVarC1, 1 );
    Lits[3] = toLitCond( iVarZ,  0 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 4 );
    assert( Cid );

    Lits[0] = toLitCond( iVarD3, 1 );
    Lits[1] = toLitCond( iVarC0, 1 );
    Lits[2] = toLitCond( iVarC1, 1 );
    Lits[3] = toLitCond( iVarZ,  0 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 4 );
    assert( Cid );


    Lits[0] = toLitCond( iVarD0, 0 );
    Lits[1] = toLitCond( iVarC0, 0 );
    Lits[2] = toLitCond( iVarC1, 0 );
    Lits[3] = toLitCond( iVarZ,  1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 4 );
    assert( Cid );

    Lits[0] = toLitCond( iVarD1, 0 );
    Lits[1] = toLitCond( iVarC0, 1 );
    Lits[2] = toLitCond( iVarC1, 0 );
    Lits[3] = toLitCond( iVarZ,  1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 4 );
    assert( Cid );

    Lits[0] = toLitCond( iVarD2, 0 );
    Lits[1] = toLitCond( iVarC0, 0 );
    Lits[2] = toLitCond( iVarC1, 1 );
    Lits[3] = toLitCond( iVarZ,  1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 4 );
    assert( Cid );

    Lits[0] = toLitCond( iVarD3, 0 );
    Lits[1] = toLitCond( iVarC0, 1 );
    Lits[2] = toLitCond( iVarC1, 1 );
    Lits[3] = toLitCond( iVarZ,  1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 4 );
    assert( Cid );
    return 8;
}
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
static inline int sat_solver_add_xor_and( sat_solver * pSat, int iVarF, int iVarA, int iVarB, int iVarC )
{
    // F = (a (+) b) * c
    lit Lits[4];
    int Cid;
    assert( iVarF >= 0 && iVarA >= 0 && iVarB >= 0 && iVarC >= 0 );

    Lits[0] = toLitCond( iVarF, 1 );
    Lits[1] = toLitCond( iVarA, 1 );
    Lits[2] = toLitCond( iVarB, 1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );

    Lits[0] = toLitCond( iVarF, 1 );
    Lits[1] = toLitCond( iVarA, 0 );
    Lits[2] = toLitCond( iVarB, 0 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );

    Lits[0] = toLitCond( iVarF, 1 );
    Lits[1] = toLitCond( iVarC, 0 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 2 );
    assert( Cid );

    Lits[0] = toLitCond( iVarF, 0 );
    Lits[1] = toLitCond( iVarA, 1 );
    Lits[2] = toLitCond( iVarB, 0 );
    Lits[3] = toLitCond( iVarC, 1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 4 );
    assert( Cid );

    Lits[0] = toLitCond( iVarF, 0 );
    Lits[1] = toLitCond( iVarA, 0 );
    Lits[2] = toLitCond( iVarB, 1 );
    Lits[3] = toLitCond( iVarC, 1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 4 );
    assert( Cid );
    return 5;
}
600
static inline int sat_solver_add_constraint( sat_solver * pSat, int iVar, int iVar2, int fCompl )
601 602 603 604 605 606
{
    lit Lits[2];
    int Cid;
    assert( iVar >= 0 );

    Lits[0] = toLitCond( iVar, fCompl );
607
    Lits[1] = toLitCond( iVar2, 0 );
608 609 610 611
    Cid = sat_solver_addclause( pSat, Lits, Lits + 2 );
    assert( Cid );

    Lits[0] = toLitCond( iVar, fCompl );
612
    Lits[1] = toLitCond( iVar2, 1 );
613 614 615 616 617
    Cid = sat_solver_addclause( pSat, Lits, Lits + 2 );
    assert( Cid );
    return 2;
}

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
static inline int sat_solver_add_half_sorter( sat_solver * pSat, int iVarA, int iVarB, int iVar0, int iVar1 )
{
    lit Lits[3];
    int Cid;

    Lits[0] = toLitCond( iVarA, 0 );
    Lits[1] = toLitCond( iVar0, 1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 2 );
    assert( Cid );

    Lits[0] = toLitCond( iVarA, 0 );
    Lits[1] = toLitCond( iVar1, 1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 2 );
    assert( Cid );

    Lits[0] = toLitCond( iVarB, 0 );
    Lits[1] = toLitCond( iVar0, 1 );
    Lits[2] = toLitCond( iVar1, 1 );
    Cid = sat_solver_addclause( pSat, Lits, Lits + 3 );
    assert( Cid );
    return 3;
}

641

642 643
ABC_NAMESPACE_HEADER_END

Alan Mishchenko committed
644
#endif