92 Vec_IntClear(
p->vPolar );
94 Vec_IntFill(
p->vLit2Used, Vec_WecSize(
p->vObjCuts) +
p->nInputs, 0 );
99 Vec_IntWriteEntry(
p->vLit2Used, -Cut, 1 );
100 Vec_IntPush(
p->vPolar, -Cut );
103 Vec_IntPush(
p->vPolar,
p->FirstVar + Cut );
104 vCut = Vec_WecEntry(
p->vCuts, Cut );
105 Lit = Vec_IntEntry( vCut, 0 ) -
p->LitShift;
106 if ( Vec_IntEntry(
p->vLit2Used, Lit) )
108 Vec_IntWriteEntry(
p->vLit2Used, Lit, 1 );
109 Vec_IntPush(
p->vPolar, Lit );
114 if ( Vec_IntEntry(
p->vLit2Used, Lit) == 0 )
115 printf(
"Output literal %d has no cut.\n", Lit ), RetValue = 0;
122 vCut = Vec_WecEntry(
p->vCuts, Cut );
124 if ( Lit -
p->LitShift < 0 )
126 assert( Abc_LitIsCompl(Lit) );
127 if ( Vec_IntEntry(
p->vLit2Used, Vec_WecSize(
p->vObjCuts) + Abc_Lit2Var(Lit)-1) == 0 )
128 printf(
"Inverter of input %d of cut %d is not mapped.\n", Abc_Lit2Var(Lit)-1, Cut ), RetValue = 0;
130 else if ( Vec_IntEntry(
p->vLit2Used, Lit -
p->LitShift) == 0 )
131 printf(
"Internal literal %d of cut %d is not mapped.\n", Lit -
p->LitShift, Cut ), RetValue = 0;
133 Vec_IntPush(
p->vPolar,
p->FirstVar + Cut );
151 int i, k, Lit, Lits[2],
value;
152 Vec_Int_t * vLits, * vCutOne, * vLitsPrev;
163 assert( Vec_IntSize(vLits) >= 2 );
183 Lits[0] = Abc_LitNot( Vec_IntEntryLast(vLits) );
184 Lits[1] = Abc_LitNot( Vec_IntEntryLast(vLitsPrev) );
194 if ( Abc_Lit2Var(Lit)-1 <
p->nInputs )
197 Lits[0] = Abc_Var2Lit( Vec_WecSize(
p->vObjCuts) + Abc_Lit2Var(Lit)-1, 0 );
198 Lits[1] = Abc_Var2Lit(
p->FirstVar + i, 1 );
204 Lits[0] = Abc_Var2Lit( Lit-
p->LitShift, 0 );
205 Lits[1] = Abc_Var2Lit(
p->FirstVar + i, 1 );
210 sat_solver_set_polarity(
p->pSat, Vec_IntArray(
p->vPolar), Vec_IntSize(
p->vPolar) );
225static inline int sat_solver_add_and1(
sat_solver * pSat,
int iVar,
int iVar0,
int iVar1,
int fCompl0,
int fCompl1,
int fCompl )
230 Lits[0] = toLitCond( iVar, !fCompl );
231 Lits[1] = toLitCond( iVar0, fCompl0 );
235 Lits[0] = toLitCond( iVar, !fCompl );
236 Lits[1] = toLitCond( iVar1, fCompl1 );
249static inline int sat_solver_add_and2(
sat_solver * pSat,
int iVar,
int iVar0,
int iVar1,
int fCompl0,
int fCompl1,
int fCompl )
264 Lits[0] = toLitCond( iVar, fCompl );
265 Lits[1] = toLitCond( iVar0, !fCompl0 );
266 Lits[2] = toLitCond( iVar1, !fCompl1 );
284static inline int Card_AddClause(
Vec_Int_t *
p,
int* begin,
int* end )
286 Vec_IntPush(
p, (
int)(end-begin) );
287 while ( begin < end )
288 Vec_IntPush(
p, (
int)*begin++ );
291static inline int Card_AddHalfSorter(
Vec_Int_t *
p,
int iVarA,
int iVarB,
int iVar0,
int iVar1 )
296 Lits[0] = toLitCond( iVarA, 0 );
297 Lits[1] = toLitCond( iVar0, 1 );
298 Cid = Card_AddClause(
p, Lits, Lits + 2 );
301 Lits[0] = toLitCond( iVarA, 0 );
302 Lits[1] = toLitCond( iVar1, 1 );
303 Cid = Card_AddClause(
p, Lits, Lits + 2 );
306 Lits[0] = toLitCond( iVarB, 0 );
307 Lits[1] = toLitCond( iVar0, 1 );
308 Lits[2] = toLitCond( iVar1, 1 );
309 Cid = Card_AddClause(
p, Lits, Lits + 3 );
313static inline void Card_AddSorter(
Vec_Int_t *
p,
int * pVars,
int i,
int k,
int * pnVars )
315 int iVar1 = (*pnVars)++;
316 int iVar2 = (*pnVars)++;
317 Card_AddHalfSorter(
p, iVar1, iVar2, pVars[i], pVars[k] );
321static inline void Card_AddCardinConstrMerge(
Vec_Int_t *
p,
int * pVars,
int lo,
int hi,
int r,
int * pnVars )
324 if ( step < hi - lo )
326 Card_AddCardinConstrMerge(
p, pVars, lo, hi-r, step, pnVars );
327 Card_AddCardinConstrMerge(
p, pVars, lo+r, hi, step, pnVars );
328 for ( i = lo+r; i < hi-r; i += step )
329 Card_AddSorter(
p, pVars, i, i+r, pnVars );
330 for ( i = lo+r; i < hi-r-1; i += r )
332 lit Lits[2] = { Abc_Var2Lit(pVars[i], 0), Abc_Var2Lit(pVars[i+r], 1) };
333 int Cid = Card_AddClause(
p, Lits, Lits + 2 );
338static inline void Card_AddCardinConstrRange(
Vec_Int_t *
p,
int * pVars,
int lo,
int hi,
int * pnVars )
342 int i, mid = lo + (hi - lo) / 2;
343 for ( i = lo; i <= mid; i++ )
344 Card_AddSorter(
p, pVars, i, i + (hi - lo + 1) / 2, pnVars );
345 Card_AddCardinConstrRange(
p, pVars, lo, mid, pnVars );
346 Card_AddCardinConstrRange(
p, pVars, mid+1, hi, pnVars );
347 Card_AddCardinConstrMerge(
p, pVars, lo, hi, 1, pnVars );
352 int nVars = Vec_IntSize(vVars);
353 Card_AddCardinConstrRange(
p, Vec_IntArray(vVars), 0, nVars - 1, &nVars );
358 int nVars = 1 << LogN;
359 int nVarsAlloc = nVars + 2 * (nVars * LogN * (LogN-1) / 4 + nVars - 1);
361 Vec_Int_t * vVars = Vec_IntStartNatural( nVars );
363 assert( nVarsReal == nVarsAlloc );
364 Vec_IntPush( vRes, -1 );
376 for ( i = 0, size = Vec_IntEntry(vRes, i++); i < Vec_IntSize(vRes); i += size, size = Vec_IntEntry(vRes, i++) )
378 if ( pvVars ) *pvVars = vVars;
379 if ( pvRes ) *pvRes = vRes;
395static inline void Sbm_AddSorter(
sat_solver *
p,
int * pVars,
int i,
int k,
int * pnVars )
397 int iVar1 = (*pnVars)++;
398 int iVar2 = (*pnVars)++;
403 for ( v = 0; v < i; v++ )
406 for ( v = i+1; v < k; v++ )
409 for ( v = k+1; v < 8; v++ )
412 printf(
"[%3d :%3d ] -> [%3d :%3d ]\n", pVars[i], pVars[k], iVar1, iVar2 );
416 sat_solver_add_half_sorter(
p, iVar1, iVar2, pVars[i], pVars[k] );
420static inline void Sbm_AddCardinConstrMerge(
sat_solver *
p,
int * pVars,
int lo,
int hi,
int r,
int * pnVars )
423 if ( step < hi - lo )
425 Sbm_AddCardinConstrMerge(
p, pVars, lo, hi-r, step, pnVars );
426 Sbm_AddCardinConstrMerge(
p, pVars, lo+r, hi, step, pnVars );
427 for ( i = lo+r; i < hi-r; i += step )
428 Sbm_AddSorter(
p, pVars, i, i+r, pnVars );
429 for ( i = lo+r; i < hi-r-1; i += r )
431 lit Lits[2] = { Abc_Var2Lit(pVars[i], 0), Abc_Var2Lit(pVars[i+r], 1) };
437static inline void Sbm_AddCardinConstrRange(
sat_solver *
p,
int * pVars,
int lo,
int hi,
int * pnVars )
441 int i, mid = lo + (hi - lo) / 2;
442 for ( i = lo; i <= mid; i++ )
443 Sbm_AddSorter(
p, pVars, i, i + (hi - lo + 1) / 2, pnVars );
444 Sbm_AddCardinConstrRange(
p, pVars, lo, mid, pnVars );
445 Sbm_AddCardinConstrRange(
p, pVars, mid+1, hi, pnVars );
446 Sbm_AddCardinConstrMerge(
p, pVars, lo, hi, 1, pnVars );
451 int nVars = Vec_IntSize(vVars);
452 Sbm_AddCardinConstrRange(
p, Vec_IntArray(vVars), 0, nVars - 1, &nVars );
453 sat_solver_bookmark(
p );
458 int nVars = 1 << LogN;
459 int nVarsAlloc = nVars + 2 * (nVars * LogN * (LogN-1) / 4 + nVars - 1), nVarsReal;
460 Vec_Int_t * vVars = Vec_IntStartNatural( nVars );
464 assert( nVarsReal == nVarsAlloc );
471 int LogN = 3, nVars = 1 << LogN, K = 2, Count = 1;
472 Vec_Int_t * vVars, * vLits = Vec_IntAlloc( nVars );
476 int Lit = Abc_Var2Lit( Vec_IntEntry(vVars, K), 1 );
477 printf(
"LogN = %d. N = %3d. Vars = %5d. Clauses = %6d. Comb = %d.\n", LogN, nVars, nVarsReal,
sat_solver_nclauses(pSat), nVars * (nVars-1)/2 + nVars + 1 );
483 Vec_IntClear( vLits );
484 printf(
"%3d : ", Count++ );
485 for ( i = 0; i < nVars; i++ )
487 Vec_IntPush( vLits, Abc_Var2Lit(i, sat_solver_var_value(pSat, i)) );
488 printf(
"%d", sat_solver_var_value(pSat, i) );
497 Vec_IntFree( vVars );
498 Vec_IntFree( vLits );
519 p->vRoots = Vec_IntAlloc( 100 );
520 p->vCuts = Vec_WecAlloc( 1000 );
521 p->vObjCuts = Vec_WecAlloc( 1000 );
522 p->vSolCuts = Vec_IntAlloc( 100 );
523 p->vCutGates = Vec_IntAlloc( 100 );
524 p->vCutAreas = Vec_WrdAlloc( 100 );
526 p->vAssump = Vec_IntAlloc( 100 );
527 p->vPolar = Vec_IntAlloc( 100 );
529 p->vArrs = Vec_IntAlloc( 100 );
530 p->vReqs = Vec_IntAlloc( 100 );
532 p->vLit2Used = Vec_IntAlloc( 100 );
533 p->vDelays = Vec_IntAlloc( 100 );
534 p->vReason = Vec_IntAlloc( 100 );
541 Vec_IntFree(
p->vCardVars );
543 Vec_IntFree(
p->vRoots );
544 Vec_WecFree(
p->vCuts );
545 Vec_WecFree(
p->vObjCuts );
546 Vec_IntFree(
p->vSolCuts );
547 Vec_IntFree(
p->vCutGates );
548 Vec_WrdFree(
p->vCutAreas );
550 Vec_IntFree(
p->vAssump );
551 Vec_IntFree(
p->vPolar );
553 Vec_IntFree(
p->vArrs );
554 Vec_IntFree(
p->vReqs );
556 Vec_IntFree(
p->vLit2Used );
557 Vec_IntFree(
p->vDelays );
558 Vec_IntFree(
p->vReason );
564 abctime clk = Abc_Clock(), clk2;
565 int i, k, Lit, LogN = 7, nVars = 1 << LogN, status, Root;
572 p->nInputs =
Nf_ManExtractWindow( pMan,
p->vRoots,
p->vCuts,
p->vObjCuts,
p->vSolCuts,
p->vCutGates,
p->vCutAreas, &InvArea,
p->FirstVar, nVars );
573 p->LitShift = 2*
p->nInputs+2;
574 assert( Vec_WecSize(
p->vObjCuts) +
p->nInputs <= nVars );
581 Vec_IntPrint(
p->vSolCuts );
582 printf(
"All clauses = %d. Multi clauses = %d. Binary clauses = %d. Other clauses = %d.\n",
592 Vec_IntClear(
p->vAssump );
593 Vec_IntPush(
p->vAssump, -1 );
595 for ( i = Vec_WecSize(
p->vObjCuts) +
p->nInputs; i < nVars; i++ )
596 Vec_IntPush(
p->vAssump, Abc_Var2Lit(i, 1) );
599 Vec_IntPush(
p->vAssump, Abc_Var2Lit(Root, 0) );
602 StartSol = Vec_IntSize(
p->vSolCuts);
604 while ( fKeepTrying && StartSol-fKeepTrying > 0 )
606 printf(
"Trying to find mapping with %d gates.\n", StartSol-fKeepTrying );
609 Vec_IntWriteEntry(
p->vAssump, 0, Abc_Var2Lit(Vec_IntEntry(
p->vCardVars, StartSol-fKeepTrying), 1) );
612 status =
sat_solver_solve(
p->pSat, Vec_IntArray(
p->vAssump), Vec_IntLimit(
p->vAssump), 0, 0, 0, 0 );
617 printf(
"AND Lits = %d. Inputs = %d. Vars = %d. All vars = %d.\n", Vec_WecSize(
p->vObjCuts),
p->nInputs, Vec_WecSize(
p->vObjCuts) +
p->nInputs, nVars );
621 for ( i = 0; i < nVars; i++ )
622 if ( sat_solver_var_value(
p->pSat, i) )
624 printf(
"%d=%d ", i, sat_solver_var_value(
p->pSat, i) );
626 if ( i >= Vec_WecSize(
p->vObjCuts) )
629 printf(
"Count = %d\n", Count );
635 if ( sat_solver_var_value(
p->pSat, i) )
637 Vec_Int_t * vCutOne = Vec_WecEntry(
p->vCuts, i-
p->FirstVar);
638 printf(
"%2d : Cut %3d (Gate %2d) ", Count, i-
p->FirstVar, Vec_IntEntry(
p->vCutGates, i-
p->FirstVar) );
640 printf(
"%d(%d) ", Lit - 2*(
p->nInputs+1), Abc_Lit2Var(Lit) );
643 AreaNew += Vec_WrdEntry(
p->vCutAreas, i-
p->FirstVar);
645 printf(
"Area = %7.2f\n", Scl_Int2Flt((
int)AreaNew) );
648 printf(
"UNSAT " ), fKeepTrying = 0;
649 else if ( status ==
l_True )
650 printf(
"SAT " ), fKeepTrying++;
651 Abc_PrintTime( 1,
"Time", Abc_Clock() - clk2 );
652 Abc_PrintTime( 1,
"Total time", Abc_Clock() - clk );
#define ABC_CALLOC(type, num)
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
ABC_NAMESPACE_IMPL_START typedef signed char value
#define sat_solver_addclause
int Nf_ManExtractWindow(void *pMan, Vec_Int_t *vRoots, Vec_Wec_t *vCuts, Vec_Wec_t *vObjCuts, Vec_Int_t *vSolCuts, Vec_Int_t *vCutGates, Vec_Wrd_t *vCutAreas, word *pInvArea, int StartVar, int nVars)
int Sbm_ManTestSat(void *pMan)
int Card_AddCardinConstrPairWise(Vec_Int_t *p, Vec_Int_t *vVars)
void Sbm_ManStop(Sbm_Man_t *p)
int Sbm_ManCheckSol(Sbm_Man_t *p, Vec_Int_t *vSol)
FUNCTION DEFINITIONS ///.
void Sbm_AddCardinConstrTest()
sat_solver * Sbm_AddCardinSolver(int LogN, Vec_Int_t **pvVars)
typedefABC_NAMESPACE_IMPL_START struct Sbm_Man_t_ Sbm_Man_t
DECLARATIONS ///.
int Sbm_AddCardinConstrPairWise(sat_solver *p, Vec_Int_t *vVars, int K)
sat_solver * Sbm_AddCardinSolver2(int LogN, Vec_Int_t **pvVars, Vec_Int_t **pvRes)
int Sbm_ManCreateCnf(Sbm_Man_t *p)
int Card_AddCardinSolver(int LogN, Vec_Int_t **pvVars, Vec_Int_t **pvRes)
Sbm_Man_t * Sbm_ManAlloc(int LogN)
unsigned __int64 word
DECLARATIONS ///.
sat_solver * sat_solver_new(void)
int sat_solver_nclauses(sat_solver *s)
void sat_solver_setnvars(sat_solver *s, int n)
int sat_solver_nvars(sat_solver *s)
void sat_solver_delete(sat_solver *s)
#define Vec_IntForEachEntry(vVec, Entry, i)
MACRO DEFINITIONS ///.
#define Vec_IntForEachEntryStart(vVec, Entry, i, Start)
#define Vec_WecForEachLevel(vGlob, vVec, i)
MACRO DEFINITIONS ///.
typedefABC_NAMESPACE_HEADER_START struct Vec_Wec_t_ Vec_Wec_t
INCLUDES ///.
typedefABC_NAMESPACE_HEADER_START struct Vec_Wrd_t_ Vec_Wrd_t
INCLUDES ///.