cnfMan.c 20.7 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/**CFile****************************************************************

  FileName    [cnfMan.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [AIG-to-CNF conversion.]

  Synopsis    []

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - April 28, 2007.]

  Revision    [$Id: cnfMan.c,v 1.00 2007/04/28 00:00:00 alanmi Exp $]

***********************************************************************/

#include "cnf.h"
22 23 24
#include "sat/bsat/satSolver.h"
#include "sat/bsat/satSolver2.h"
#include "misc/zlib/zlib.h"
Alan Mishchenko committed
25

26 27 28
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
29 30 31 32 33
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

static inline int Cnf_Lit2Var( int Lit )        { return (Lit & 1)? -(Lit >> 1)-1 : (Lit >> 1)+1;  }
Alan Mishchenko committed
34
static inline int Cnf_Lit2Var2( int Lit )       { return (Lit & 1)? -(Lit >> 1)   : (Lit >> 1);    }
Alan Mishchenko committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Starts the fraiging manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Cnf_Man_t * Cnf_ManStart()
{
    Cnf_Man_t * p;
    int i;
    // allocate the manager
Alan Mishchenko committed
56
    p = ABC_ALLOC( Cnf_Man_t, 1 );
Alan Mishchenko committed
57 58 59 60
    memset( p, 0, sizeof(Cnf_Man_t) );
    // derive internal data structures
    Cnf_ReadMsops( &p->pSopSizes, &p->pSops );
    // allocate memory manager for cuts
Alan Mishchenko committed
61
    p->pMemCuts = Aig_MmFlexStart();
Alan Mishchenko committed
62 63
    p->nMergeLimit = 10;
    // allocate temporary truth tables
64
    p->pTruths[0] = ABC_ALLOC( unsigned, 4 * Abc_TruthWordNum(p->nMergeLimit) );
Alan Mishchenko committed
65
    for ( i = 1; i < 4; i++ )
66
        p->pTruths[i] = p->pTruths[i-1] + Abc_TruthWordNum(p->nMergeLimit);
Alan Mishchenko committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    p->vMemory = Vec_IntAlloc( 1 << 18 );
    return p;
}

/**Function*************************************************************

  Synopsis    [Stops the fraiging manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cnf_ManStop( Cnf_Man_t * p )
{
    Vec_IntFree( p->vMemory );
Alan Mishchenko committed
85
    ABC_FREE( p->pTruths[0] );
Alan Mishchenko committed
86
    Aig_MmFlexStop( p->pMemCuts, 0 );
Alan Mishchenko committed
87 88 89 90
    ABC_FREE( p->pSopSizes );
    ABC_FREE( p->pSops[1] );
    ABC_FREE( p->pSops );
    ABC_FREE( p );
Alan Mishchenko committed
91 92 93 94
}

/**Function*************************************************************

Alan Mishchenko committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108
  Synopsis    [Returns the array of CI IDs.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Cnf_DataCollectPiSatNums( Cnf_Dat_t * pCnf, Aig_Man_t * p )
{
    Vec_Int_t * vCiIds;
    Aig_Obj_t * pObj;
    int i;
109
    vCiIds = Vec_IntAlloc( Aig_ManCiNum(p) );
110
    Aig_ManForEachCi( p, pObj, i )
Alan Mishchenko committed
111 112 113 114 115 116
        Vec_IntPush( vCiIds, pCnf->pVarNums[pObj->Id] );
    return vCiIds;
}

/**Function*************************************************************

Alan Mishchenko committed
117 118 119 120 121 122 123 124 125 126 127 128
  Synopsis    [Allocates the new CNF.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Cnf_Dat_t * Cnf_DataAlloc( Aig_Man_t * pAig, int nVars, int nClauses, int nLiterals )
{
    Cnf_Dat_t * pCnf;
Alan Mishchenko committed
129
    int i;
Alan Mishchenko committed
130
    pCnf = ABC_ALLOC( Cnf_Dat_t, 1 );
Alan Mishchenko committed
131 132 133 134 135
    memset( pCnf, 0, sizeof(Cnf_Dat_t) );
    pCnf->pMan = pAig;
    pCnf->nVars = nVars;
    pCnf->nClauses = nClauses;
    pCnf->nLiterals = nLiterals;
Alan Mishchenko committed
136 137
    pCnf->pClauses = ABC_ALLOC( int *, nClauses + 1 );
    pCnf->pClauses[0] = ABC_ALLOC( int, nLiterals );
Alan Mishchenko committed
138
    pCnf->pClauses[nClauses] = pCnf->pClauses[0] + nLiterals;
Alan Mishchenko committed
139
    pCnf->pVarNums = ABC_ALLOC( int, Aig_ManObjNumMax(pAig) );
Alan Mishchenko committed
140 141 142
//    memset( pCnf->pVarNums, 0xff, sizeof(int) * Aig_ManObjNumMax(pAig) );
    for ( i = 0; i < Aig_ManObjNumMax(pAig); i++ )
        pCnf->pVarNums[i] = -1;
Alan Mishchenko committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    return pCnf;
}

/**Function*************************************************************

  Synopsis    [Allocates the new CNF.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Cnf_Dat_t * Cnf_DataDup( Cnf_Dat_t * p )
{
    Cnf_Dat_t * pCnf;
    int i;
    pCnf = Cnf_DataAlloc( p->pMan, p->nVars, p->nClauses, p->nLiterals );
    memcpy( pCnf->pClauses[0], p->pClauses[0], sizeof(int) * p->nLiterals );
    memcpy( pCnf->pVarNums, p->pVarNums, sizeof(int) * Aig_ManObjNumMax(p->pMan) );
    for ( i = 1; i < p->nClauses; i++ )
        pCnf->pClauses[i] = pCnf->pClauses[0] + (p->pClauses[i] - p->pClauses[0]);
    return pCnf;
}

/**Function*************************************************************

Alan Mishchenko committed
171 172 173 174 175 176 177 178 179 180 181 182 183
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Cnf_DataFree( Cnf_Dat_t * p )
{
    if ( p == NULL )
        return;
184
    Vec_IntFreeP( &p->vMapping );
Alan Mishchenko committed
185
    ABC_FREE( p->pClaPols );
186 187
    ABC_FREE( p->pObj2Clause );
    ABC_FREE( p->pObj2Count );
Alan Mishchenko committed
188 189 190 191
    ABC_FREE( p->pClauses[0] );
    ABC_FREE( p->pClauses );
    ABC_FREE( p->pVarNums );
    ABC_FREE( p );
Alan Mishchenko committed
192 193 194 195
}

/**Function*************************************************************

196
  Synopsis    []
Alan Mishchenko committed
197 198 199 200 201 202 203 204

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
205 206 207 208
void Cnf_DataLift( Cnf_Dat_t * p, int nVarsPlus )
{
    Aig_Obj_t * pObj;
    int v;
209 210 211 212 213 214
    if ( p->pMan )
    {
        Aig_ManForEachObj( p->pMan, pObj, v )
            if ( p->pVarNums[pObj->Id] >= 0 )
                p->pVarNums[pObj->Id] += nVarsPlus;
    }
Alan Mishchenko committed
215 216 217
    for ( v = 0; v < p->nLiterals; v++ )
        p->pClauses[0][v] += 2*nVarsPlus;
}
218
void Cnf_DataCollectFlipLits( Cnf_Dat_t * p, int iFlipVar, Vec_Int_t * vFlips )
Alan Mishchenko committed
219
{
220
    int v;
221
    assert( p->pMan == NULL );
222
    Vec_IntClear( vFlips );
223 224
    for ( v = 0; v < p->nLiterals; v++ )
        if ( Abc_Lit2Var(p->pClauses[0][v]) == iFlipVar )
225
            Vec_IntPush( vFlips, v );
226 227 228 229 230 231 232
}
void Cnf_DataLiftAndFlipLits( Cnf_Dat_t * p, int nVarsPlus, Vec_Int_t * vLits )
{
    int i, iLit;
    assert( p->pMan == NULL );
    Vec_IntForEachEntry( vLits, iLit, i )
        p->pClauses[0][iLit] = Abc_LitNot(p->pClauses[0][iLit]) + 2*nVarsPlus;
Alan Mishchenko committed
233
}
Alan Mishchenko committed
234 235 236

/**Function*************************************************************

237
  Synopsis    []
Alan Mishchenko committed
238 239 240 241 242 243 244 245

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
246 247 248 249 250 251 252 253
void Cnf_DataPrint( Cnf_Dat_t * p, int fReadable )
{
    FILE * pFile = stdout;
    int * pLit, * pStop, i;
    fprintf( pFile, "p cnf %d %d\n", p->nVars, p->nClauses );
    for ( i = 0; i < p->nClauses; i++ )
    {
        for ( pLit = p->pClauses[i], pStop = p->pClauses[i+1]; pLit < pStop; pLit++ )
254
            fprintf( pFile, "%s%d ", Abc_LitIsCompl(*pLit) ? "-":"", fReadable? Abc_Lit2Var(*pLit) : Abc_Lit2Var(*pLit)+1 );
Alan Mishchenko committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
        fprintf( pFile, "\n" );
    }
    fprintf( pFile, "\n" );
}

/**Function*************************************************************

  Synopsis    [Writes CNF into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
271
void Cnf_DataWriteIntoFileGz( Cnf_Dat_t * p, char * pFileName, int fReadable, Vec_Int_t * vForAlls, Vec_Int_t * vExists )
Alan Mishchenko committed
272
{
Alan Mishchenko committed
273
    gzFile pFile;
274
    int * pLit, * pStop, i, VarId;
Alan Mishchenko committed
275
    pFile = gzopen( pFileName, "wb" );
Alan Mishchenko committed
276 277 278 279 280
    if ( pFile == NULL )
    {
        printf( "Cnf_WriteIntoFile(): Output file cannot be opened.\n" );
        return;
    }
Alan Mishchenko committed
281 282
    gzprintf( pFile, "c Result of efficient AIG-to-CNF conversion using package CNF\n" );
    gzprintf( pFile, "p cnf %d %d\n", p->nVars, p->nClauses );
283 284 285 286 287 288 289 290 291 292 293 294 295 296
    if ( vForAlls )
    {
        gzprintf( pFile, "a " );
        Vec_IntForEachEntry( vForAlls, VarId, i )
            gzprintf( pFile, "%d ", fReadable? VarId : VarId+1 );
        gzprintf( pFile, "0\n" );
    }
    if ( vExists )
    {
        gzprintf( pFile, "e " );
        Vec_IntForEachEntry( vExists, VarId, i )
            gzprintf( pFile, "%d ", fReadable? VarId : VarId+1 );
        gzprintf( pFile, "0\n" );
    }
Alan Mishchenko committed
297 298 299
    for ( i = 0; i < p->nClauses; i++ )
    {
        for ( pLit = p->pClauses[i], pStop = p->pClauses[i+1]; pLit < pStop; pLit++ )
Alan Mishchenko committed
300 301
            gzprintf( pFile, "%d ", fReadable? Cnf_Lit2Var2(*pLit) : Cnf_Lit2Var(*pLit) );
        gzprintf( pFile, "0\n" );
Alan Mishchenko committed
302
    }
Alan Mishchenko committed
303 304
    gzprintf( pFile, "\n" );
    gzclose( pFile );
Alan Mishchenko committed
305 306 307 308 309 310 311 312 313 314 315 316 317
}

/**Function*************************************************************

  Synopsis    [Writes CNF into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
318
void Cnf_DataWriteIntoFile( Cnf_Dat_t * p, char * pFileName, int fReadable, Vec_Int_t * vForAlls, Vec_Int_t * vExists )
Alan Mishchenko committed
319
{
Alan Mishchenko committed
320
    FILE * pFile;
321
    int * pLit, * pStop, i, VarId;
Alan Mishchenko committed
322 323
    if ( !strncmp(pFileName+strlen(pFileName)-3,".gz",3) ) 
    {
324
        Cnf_DataWriteIntoFileGz( p, pFileName, fReadable, vForAlls, vExists );
Alan Mishchenko committed
325 326 327
        return;
    }
    pFile = fopen( pFileName, "w" );
Alan Mishchenko committed
328 329 330 331 332
    if ( pFile == NULL )
    {
        printf( "Cnf_WriteIntoFile(): Output file cannot be opened.\n" );
        return;
    }
Alan Mishchenko committed
333 334
    fprintf( pFile, "c Result of efficient AIG-to-CNF conversion using package CNF\n" );
    fprintf( pFile, "p cnf %d %d\n", p->nVars, p->nClauses );
335 336 337 338 339 340 341 342 343 344 345 346 347 348
    if ( vForAlls )
    {
        fprintf( pFile, "a " );
        Vec_IntForEachEntry( vForAlls, VarId, i )
            fprintf( pFile, "%d ", fReadable? VarId : VarId+1 );
        fprintf( pFile, "0\n" );
    }
    if ( vExists )
    {
        fprintf( pFile, "e " );
        Vec_IntForEachEntry( vExists, VarId, i )
            fprintf( pFile, "%d ", fReadable? VarId : VarId+1 );
        fprintf( pFile, "0\n" );
    }
Alan Mishchenko committed
349 350 351
    for ( i = 0; i < p->nClauses; i++ )
    {
        for ( pLit = p->pClauses[i], pStop = p->pClauses[i+1]; pLit < pStop; pLit++ )
Alan Mishchenko committed
352 353
            fprintf( pFile, "%d ", fReadable? Cnf_Lit2Var2(*pLit) : Cnf_Lit2Var(*pLit) );
        fprintf( pFile, "0\n" );
Alan Mishchenko committed
354
    }
Alan Mishchenko committed
355 356
    fprintf( pFile, "\n" );
    fclose( pFile );
Alan Mishchenko committed
357 358 359 360 361 362 363 364 365 366 367 368 369
}

/**Function*************************************************************

  Synopsis    [Writes CNF into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
370
void * Cnf_DataWriteIntoSolverInt( void * pSolver, Cnf_Dat_t * p, int nFrames, int fInit )
Alan Mishchenko committed
371
{
372
    sat_solver * pSat = (sat_solver *)pSolver;
Alan Mishchenko committed
373 374
    int i, f, status;
    assert( nFrames > 0 );
375 376
    assert( pSat );
//    pSat = sat_solver_new();
Alan Mishchenko committed
377
    sat_solver_setnvars( pSat, p->nVars * nFrames );
Alan Mishchenko committed
378 379 380 381 382 383 384 385
    for ( i = 0; i < p->nClauses; i++ )
    {
        if ( !sat_solver_addclause( pSat, p->pClauses[i], p->pClauses[i+1] ) )
        {
            sat_solver_delete( pSat );
            return NULL;
        }
    }
Alan Mishchenko committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 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
    if ( nFrames > 1 )
    {
        Aig_Obj_t * pObjLo, * pObjLi;
        int nLitsAll, * pLits, Lits[2];
        nLitsAll = 2 * p->nVars;
        pLits = p->pClauses[0];
        for ( f = 1; f < nFrames; f++ )
        {
            // add equality of register inputs/outputs for different timeframes
            Aig_ManForEachLiLoSeq( p->pMan, pObjLi, pObjLo, i )
            {
                Lits[0] = (f-1)*nLitsAll + toLitCond( p->pVarNums[pObjLi->Id], 0 );
                Lits[1] =  f   *nLitsAll + toLitCond( p->pVarNums[pObjLo->Id], 1 );
                if ( !sat_solver_addclause( pSat, Lits, Lits + 2 ) )
                {
                    sat_solver_delete( pSat );
                    return NULL;
                }
                Lits[0]++;
                Lits[1]--;
                if ( !sat_solver_addclause( pSat, Lits, Lits + 2 ) )
                {
                    sat_solver_delete( pSat );
                    return NULL;
                }
            }
            // add clauses for the next timeframe
            for ( i = 0; i < p->nLiterals; i++ )
                pLits[i] += nLitsAll;
            for ( i = 0; i < p->nClauses; i++ )
            {
                if ( !sat_solver_addclause( pSat, p->pClauses[i], p->pClauses[i+1] ) )
                {
                    sat_solver_delete( pSat );
                    return NULL;
                }
            }
        }
        // return literals to their original state
        nLitsAll = (f-1) * nLitsAll;
        for ( i = 0; i < p->nLiterals; i++ )
            pLits[i] -= nLitsAll;
    }
    if ( fInit )
    {
        Aig_Obj_t * pObjLo;
        int Lits[1];
        Aig_ManForEachLoSeq( p->pMan, pObjLo, i )
        {
            Lits[0] = toLitCond( p->pVarNums[pObjLo->Id], 1 );
            if ( !sat_solver_addclause( pSat, Lits, Lits + 1 ) )
            {
                sat_solver_delete( pSat );
                return NULL;
            }
        }
    }
Alan Mishchenko committed
443 444 445 446 447 448
    status = sat_solver_simplify(pSat);
    if ( status == 0 )
    {
        sat_solver_delete( pSat );
        return NULL;
    }
Alan Mishchenko committed
449 450 451
    return pSat;
}

Alan Mishchenko committed
452 453
/**Function*************************************************************

454 455 456 457 458 459 460 461 462
  Synopsis    [Writes CNF into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
void * Cnf_DataWriteIntoSolver( Cnf_Dat_t * p, int nFrames, int fInit )
{
    return Cnf_DataWriteIntoSolverInt( sat_solver_new(), p, nFrames, fInit );
}

/**Function*************************************************************

  Synopsis    [Writes CNF into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
479 480 481 482 483 484 485 486 487
void * Cnf_DataWriteIntoSolver2( Cnf_Dat_t * p, int nFrames, int fInit )
{
    sat_solver2 * pSat;
    int i, f, status;
    assert( nFrames > 0 );
    pSat = sat_solver2_new();
    sat_solver2_setnvars( pSat, p->nVars * nFrames );
    for ( i = 0; i < p->nClauses; i++ )
    {
488
        if ( !sat_solver2_addclause( pSat, p->pClauses[i], p->pClauses[i+1], 0 ) )
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
        {
            sat_solver2_delete( pSat );
            return NULL;
        }
    }
    if ( nFrames > 1 )
    {
        Aig_Obj_t * pObjLo, * pObjLi;
        int nLitsAll, * pLits, Lits[2];
        nLitsAll = 2 * p->nVars;
        pLits = p->pClauses[0];
        for ( f = 1; f < nFrames; f++ )
        {
            // add equality of register inputs/outputs for different timeframes
            Aig_ManForEachLiLoSeq( p->pMan, pObjLi, pObjLo, i )
            {
                Lits[0] = (f-1)*nLitsAll + toLitCond( p->pVarNums[pObjLi->Id], 0 );
                Lits[1] =  f   *nLitsAll + toLitCond( p->pVarNums[pObjLo->Id], 1 );
507
                if ( !sat_solver2_addclause( pSat, Lits, Lits + 2, 0 ) )
508 509 510 511 512 513
                {
                    sat_solver2_delete( pSat );
                    return NULL;
                }
                Lits[0]++;
                Lits[1]--;
514
                if ( !sat_solver2_addclause( pSat, Lits, Lits + 2, 0 ) )
515 516 517 518 519 520 521 522 523 524
                {
                    sat_solver2_delete( pSat );
                    return NULL;
                }
            }
            // add clauses for the next timeframe
            for ( i = 0; i < p->nLiterals; i++ )
                pLits[i] += nLitsAll;
            for ( i = 0; i < p->nClauses; i++ )
            {
525
                if ( !sat_solver2_addclause( pSat, p->pClauses[i], p->pClauses[i+1], 0 ) )
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
                {
                    sat_solver2_delete( pSat );
                    return NULL;
                }
            }
        }
        // return literals to their original state
        nLitsAll = (f-1) * nLitsAll;
        for ( i = 0; i < p->nLiterals; i++ )
            pLits[i] -= nLitsAll;
    }
    if ( fInit )
    {
        Aig_Obj_t * pObjLo;
        int Lits[1];
        Aig_ManForEachLoSeq( p->pMan, pObjLo, i )
        {
            Lits[0] = toLitCond( p->pVarNums[pObjLo->Id], 1 );
544
            if ( !sat_solver2_addclause( pSat, Lits, Lits + 1, 0 ) )
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
            {
                sat_solver2_delete( pSat );
                return NULL;
            }
        }
    }
    status = sat_solver2_simplify(pSat);
    if ( status == 0 )
    {
        sat_solver2_delete( pSat );
        return NULL;
    }
    return pSat;
}

/**Function*************************************************************

Alan Mishchenko committed
562 563 564 565 566 567 568 569 570
  Synopsis    [Adds the OR-clause.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
571
int Cnf_DataWriteOrClause( void * p, Cnf_Dat_t * pCnf )
Alan Mishchenko committed
572
{
573
    sat_solver * pSat = (sat_solver *)p;
Alan Mishchenko committed
574 575
    Aig_Obj_t * pObj;
    int i, * pLits;
576
    pLits = ABC_ALLOC( int, Aig_ManCoNum(pCnf->pMan) );
577
    Aig_ManForEachCo( pCnf->pMan, pObj, i )
Alan Mishchenko committed
578
        pLits[i] = toLitCond( pCnf->pVarNums[pObj->Id], 0 );
579
    if ( !sat_solver_addclause( pSat, pLits, pLits + Aig_ManCoNum(pCnf->pMan) ) )
Alan Mishchenko committed
580
    {
Alan Mishchenko committed
581
        ABC_FREE( pLits );
Alan Mishchenko committed
582 583
        return 0;
    }
Alan Mishchenko committed
584
    ABC_FREE( pLits );
Alan Mishchenko committed
585
    return 1;
Alan Mishchenko committed
586 587
}

Alan Mishchenko committed
588
/**Function*************************************************************
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603

  Synopsis    [Adds the OR-clause.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cnf_DataWriteOrClause2( void * p, Cnf_Dat_t * pCnf )
{
    sat_solver2 * pSat = (sat_solver2 *)p;
    Aig_Obj_t * pObj;
    int i, * pLits;
604
    pLits = ABC_ALLOC( int, Aig_ManCoNum(pCnf->pMan) );
605
    Aig_ManForEachCo( pCnf->pMan, pObj, i )
606
        pLits[i] = toLitCond( pCnf->pVarNums[pObj->Id], 0 );
607
    if ( !sat_solver2_addclause( pSat, pLits, pLits + Aig_ManCoNum(pCnf->pMan), 0 ) )
608 609 610 611 612 613 614 615 616
    {
        ABC_FREE( pLits );
        return 0;
    }
    ABC_FREE( pLits );
    return 1;
}

/**Function*************************************************************
Alan Mishchenko committed
617 618 619 620 621 622 623 624 625 626 627 628

  Synopsis    [Adds the OR-clause.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cnf_DataWriteAndClauses( void * p, Cnf_Dat_t * pCnf )
{
629
    sat_solver * pSat = (sat_solver *)p;
Alan Mishchenko committed
630 631
    Aig_Obj_t * pObj;
    int i, Lit;
632
    Aig_ManForEachCo( pCnf->pMan, pObj, i )
Alan Mishchenko committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
    {
        Lit = toLitCond( pCnf->pVarNums[pObj->Id], 0 );
        if ( !sat_solver_addclause( pSat, &Lit, &Lit+1 ) )
            return 0;
    }
    return 1;
}

/**Function*************************************************************

  Synopsis    [Transforms polarity of the internal veriables.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
652
void Cnf_DataTranformPolarity( Cnf_Dat_t * pCnf, int fTransformPos )
Alan Mishchenko committed
653 654 655 656 657
{
    Aig_Obj_t * pObj;
    int * pVarToPol;
    int i, iVar;
    // create map from the variable number to its polarity
Alan Mishchenko committed
658
    pVarToPol = ABC_CALLOC( int, pCnf->nVars );
Alan Mishchenko committed
659
    Aig_ManForEachObj( pCnf->pMan, pObj, i )
Alan Mishchenko committed
660
    {
661
        if ( !fTransformPos && Aig_ObjIsCo(pObj) )
Alan Mishchenko committed
662 663
            continue;
        if ( pCnf->pVarNums[pObj->Id] >= 0 )
Alan Mishchenko committed
664
            pVarToPol[ pCnf->pVarNums[pObj->Id] ] = pObj->fPhase;
Alan Mishchenko committed
665
    }
Alan Mishchenko committed
666 667 668 669 670 671 672 673
    // transform literals
    for ( i = 0; i < pCnf->nLiterals; i++ )
    {
        iVar = lit_var(pCnf->pClauses[0][i]);
        assert( iVar < pCnf->nVars );
        if ( pVarToPol[iVar] )
            pCnf->pClauses[0][i] = lit_neg( pCnf->pClauses[0][i] );
    }
Alan Mishchenko committed
674
    ABC_FREE( pVarToPol );
Alan Mishchenko committed
675 676
}

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
/**Function*************************************************************

  Synopsis    [Adds constraints for the two-input AND-gate.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Cnf_DataAddXorClause( void * pSat, int iVarA, int iVarB, int iVarC )
{
    lit Lits[3];
    assert( iVarA > 0 && iVarB > 0 && iVarC > 0 );

    Lits[0] = toLitCond( iVarA, 1 );
    Lits[1] = toLitCond( iVarB, 1 );
    Lits[2] = toLitCond( iVarC, 1 );
    if ( !sat_solver_addclause( (sat_solver *)pSat, Lits, Lits + 3 ) )
        return 0;

    Lits[0] = toLitCond( iVarA, 1 );
    Lits[1] = toLitCond( iVarB, 0 );
    Lits[2] = toLitCond( iVarC, 0 );
    if ( !sat_solver_addclause( (sat_solver *)pSat, Lits, Lits + 3 ) )
        return 0;

    Lits[0] = toLitCond( iVarA, 0 );
    Lits[1] = toLitCond( iVarB, 1 );
    Lits[2] = toLitCond( iVarC, 0 );
    if ( !sat_solver_addclause( (sat_solver *)pSat, Lits, Lits + 3 ) )
        return 0;

    Lits[0] = toLitCond( iVarA, 0 );
    Lits[1] = toLitCond( iVarB, 0 );
    Lits[2] = toLitCond( iVarC, 1 );
    if ( !sat_solver_addclause( (sat_solver *)pSat, Lits, Lits + 3 ) )
        return 0;

    return 1;
}

Alan Mishchenko committed
720 721 722 723 724
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


725 726
ABC_NAMESPACE_IMPL_END