00001
00021 #include "bdcInt.h"
00022
00026
00027 static Bdc_Type_t Bdc_DecomposeStep( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR );
00028 static int Bdc_DecomposeUpdateRight( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR, unsigned * puTruth, Bdc_Type_t Type );
00029
00033
00045 Bdc_Fun_t * Bdc_ManDecompose_rec( Bdc_Man_t * p, Bdc_Isf_t * pIsf )
00046 {
00047 Bdc_Fun_t * pFunc;
00048 Bdc_Isf_t IsfL, * pIsfL = &IsfL;
00049 Bdc_Isf_t IsfB, * pIsfR = &IsfB;
00050
00051 if ( pFunc = Bdc_TableLookup( p, pIsf ) )
00052 return pFunc;
00053
00054 pFunc = Bdc_FunNew( p );
00055 if ( pFunc == NULL )
00056 return NULL;
00057 pFunc->Type = Bdc_DecomposeStep( p, pIsf, pIsfL, pIsfR );
00058
00059 pFunc->pFan0 = Bdc_ManDecompose_rec( p, pIsfL );
00060 if ( pFunc->pFan0 == NULL )
00061 return NULL;
00062
00063 if ( Bdc_DecomposeUpdateRight( p, pIsf, pIsfL, pIsfR, pFunc->pFan0->puFunc, pFunc->Type ) )
00064 {
00065 p->nNodes--;
00066 return pFunc->pFan0;
00067 }
00068 pFunc->pFan1 = Bdc_ManDecompose_rec( p, pIsfL );
00069 if ( pFunc->pFan1 == NULL )
00070 return NULL;
00071
00072 pFunc->puFunc = (unsigned *)Vec_IntFetch(p->vMemory, p->nWords);
00073 if ( pFunc->Type == BDC_TYPE_AND )
00074 Kit_TruthAnd( pFunc->puFunc, pFunc->pFan0->puFunc, pFunc->pFan1->puFunc, p->nVars );
00075 else if ( pFunc->Type == BDC_TYPE_OR )
00076 Kit_TruthOr( pFunc->puFunc, pFunc->pFan0->puFunc, pFunc->pFan1->puFunc, p->nVars );
00077 else
00078 assert( 0 );
00079
00080 assert( Bdc_TableCheckContainment(p, pIsf, pFunc->puFunc) );
00081
00082 if ( pFunc->Type == BDC_TYPE_OR )
00083 {
00084 pFunc->Type = BDC_TYPE_AND;
00085 pFunc->pFan0 = Bdc_Not(pFunc->pFan0);
00086 pFunc->pFan1 = Bdc_Not(pFunc->pFan1);
00087 Kit_TruthNot( pFunc->puFunc, pFunc->puFunc, p->nVars );
00088 pFunc = Bdc_Not(pFunc);
00089 }
00090 Bdc_TableAdd( p, Bdc_Regular(pFunc) );
00091 return pFunc;
00092 }
00093
00105 int Bdc_DecomposeUpdateRight( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR, unsigned * puTruth, Bdc_Type_t Type )
00106 {
00107 if ( Type == BDC_TYPE_OR )
00108 {
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 Kit_TruthSharp( pIsfR->puOn, pIsf->puOn, puTruth, p->nVars );
00121 Kit_TruthExistSet( pIsfR->puOn, pIsfR->puOn, p->nVars, pIsfL->uSupp );
00122 Kit_TruthExistSet( pIsfR->puOff, pIsf->puOff, p->nVars, pIsfL->uSupp );
00123 assert( !Kit_TruthIsConst0(pIsfR->puOff, p->nVars) );
00124 return Kit_TruthIsConst0(pIsfR->puOn, p->nVars);
00125 }
00126 else if ( Type == BDC_TYPE_AND )
00127 {
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 Kit_TruthSharp( pIsfR->puOn, pIsf->puOn, puTruth, p->nVars );
00140 Kit_TruthExistSet( pIsfR->puOn, pIsfR->puOn, p->nVars, pIsfL->uSupp );
00141 Kit_TruthExistSet( pIsfR->puOff, pIsf->puOff, p->nVars, pIsfL->uSupp );
00142 assert( !Kit_TruthIsConst0(pIsfR->puOff, p->nVars) );
00143 return Kit_TruthIsConst0(pIsfR->puOn, p->nVars);
00144 }
00145 return 0;
00146 }
00147
00159 static inline int Bdc_DecomposeGetCost( Bdc_Man_t * p, int nLeftVars, int nRightVars )
00160 {
00161 assert( nLeftVars > 0 );
00162 assert( nRightVars > 0 );
00163
00164 if ( nLeftVars >= nRightVars )
00165 return BDC_SCALE * (p->nVars * nRightVars + nLeftVars);
00166 else
00167 return BDC_SCALE * (p->nVars * nLeftVars + nRightVars);
00168 }
00169
00181 int Bdc_DecomposeFindInitialVarSet( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR )
00182 {
00183 char pVars[16];
00184 int v, nVars, Beg, End;
00185
00186 assert( pIsfL->uSupp == 0 );
00187 assert( pIsfR->uSupp == 0 );
00188
00189
00190 nVars = 0;
00191 for ( v = 0; v < p->nVars; v++ )
00192 if ( pIsf->uSupp & (1 << v) )
00193 pVars[nVars++] = v;
00194
00195
00196 for ( Beg = 0; Beg < nVars; Beg++ )
00197 {
00198 Kit_TruthExistNew( p->puTemp1, pIsf->puOff, p->nVars, pVars[Beg] );
00199 for ( End = nVars - 1; End > Beg; End-- )
00200 {
00201 Kit_TruthExistNew( p->puTemp2, pIsf->puOff, p->nVars, pVars[End] );
00202 if ( Kit_TruthIsDisjoint3(pIsf->puOn, p->puTemp1, p->puTemp2, p->nVars) )
00203 {
00204 pIsfL->uSupp = (1 << Beg);
00205 pIsfR->uSupp = (1 << End);
00206 pIsfL->Var = Beg;
00207 pIsfR->Var = End;
00208 return 1;
00209 }
00210 }
00211 }
00212 return 0;
00213 }
00214
00226 int Bdc_DecomposeWeakOr( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR )
00227 {
00228 int v, VarCost, VarBest, Cost, VarCostBest = 0;
00229
00230 for ( v = 0; v < p->nVars; v++ )
00231 {
00232 Kit_TruthExistNew( p->puTemp1, pIsf->puOff, p->nVars, v );
00233
00234
00235
00236 if ( !Kit_TruthIsImply( pIsf->puOn, p->puTemp1, p->nVars ) )
00237 {
00238
00239
00240
00241
00242
00243
00244
00245 Kit_TruthForallNew( p->puTemp2, pIsf->puOn, p->nVars, v );
00246 VarCost = Kit_TruthCountOnes( p->puTemp2, p->nVars );
00247 if ( VarCost == 0 )
00248 VarCost = 1;
00249 if ( VarCostBest < VarCost )
00250 {
00251 VarCostBest = VarCost;
00252 VarBest = v;
00253 }
00254 }
00255 }
00256
00257
00258 if ( VarCostBest )
00259 {
00260
00261
00262
00263
00264
00265
00266 Kit_TruthExistNew( p->puTemp1, pIsf->puOff, p->nVars, VarBest );
00267 Kit_TruthAnd( pIsfL->puOn, pIsf->puOn, p->puTemp1, p->nVars );
00268
00269
00270
00271 Kit_TruthCopy( pIsfL->puOff, pIsf->puOff, p->nVars );
00272 pIsfL->Var = VarBest;
00273
00274
00275
00276
00277
00278
00279 Cost = VarCostBest * BDC_SCALE / (1<<p->nVars);
00280 if ( Cost == 0 )
00281 Cost = 1;
00282 return Cost;
00283 }
00284 return 0;
00285 }
00286
00298 int Bdc_DecomposeOr( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR )
00299 {
00300 unsigned uSuppRem;
00301 int v, nLeftVars = 1, nRightVars = 1;
00302
00303 Bdc_IsfClean( pIsfL );
00304 Bdc_IsfClean( pIsfR );
00305
00306 if ( !Bdc_DecomposeFindInitialVarSet( p, pIsf, pIsfL, pIsfR ) )
00307 return Bdc_DecomposeWeakOr( p, pIsf, pIsfL, pIsfR );
00308
00309 Kit_TruthExistNew( p->puTemp1, pIsf->puOff, p->nVars, pIsfL->Var );
00310 Kit_TruthExistNew( p->puTemp2, pIsf->puOff, p->nVars, pIsfR->Var );
00311
00312 uSuppRem = pIsf->uSupp & ~pIsfL->uSupp & ~pIsfR->uSupp;
00313 assert( Kit_WordCountOnes(uSuppRem) > 0 );
00314 for ( v = 0; v < p->nVars; v++ )
00315 {
00316 if ( (uSuppRem & (1 << v)) == 0 )
00317 continue;
00318
00319 Kit_TruthExistNew( p->puTemp3, p->puTemp1, p->nVars, v );
00320 Kit_TruthExistNew( p->puTemp4, p->puTemp2, p->nVars, v );
00321 if ( nLeftVars < nRightVars )
00322 {
00323
00324
00325 if ( Kit_TruthIsDisjoint3(pIsf->puOn, p->puTemp3, p->puTemp2, p->nVars) )
00326 {
00327
00328 pIsfL->uSupp |= (1 << v);
00329 nLeftVars++;
00330 }
00331
00332 else if ( Kit_TruthIsDisjoint3(pIsf->puOn, p->puTemp4, p->puTemp1, p->nVars) )
00333 {
00334
00335 pIsfR->uSupp |= (1 << v);
00336 nRightVars++;
00337 }
00338 }
00339 else
00340 {
00341
00342 if ( Kit_TruthIsDisjoint3(pIsf->puOn, p->puTemp4, p->puTemp1, p->nVars) )
00343 {
00344
00345 pIsfR->uSupp |= (1 << v);
00346 nRightVars++;
00347 }
00348
00349 else if ( Kit_TruthIsDisjoint3(pIsf->puOn, p->puTemp3, p->puTemp2, p->nVars) )
00350 {
00351
00352 pIsfL->uSupp |= (1 << v);
00353 nLeftVars++;
00354 }
00355 }
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367 Kit_TruthAnd( pIsfL->puOn, pIsf->puOn, p->puTemp1, p->nVars );
00368 Kit_TruthExistSet( pIsfL->puOn, pIsfL->puOn, p->nVars, pIsfR->uSupp );
00369 Kit_TruthCopy( pIsfL->puOff, p->puTemp2, p->nVars );
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 assert( !Kit_TruthIsConst0(pIsfL->puOn, p->nVars) );
00387 assert( !Kit_TruthIsConst0(pIsfL->puOff, p->nVars) );
00388 assert( Kit_TruthIsDisjoint(pIsfL->puOn, pIsfL->puOff, p->nVars) );
00389
00390 return Bdc_DecomposeGetCost( p, nLeftVars, nRightVars );
00391 }
00392
00404 Bdc_Type_t Bdc_DecomposeStep( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR )
00405 {
00406 int CostOr, CostAnd, CostOrL, CostOrR, CostAndL, CostAndR;
00407
00408 Bdc_IsfClean( p->pIsfOL );
00409 Bdc_IsfClean( p->pIsfOR );
00410 Bdc_IsfClean( p->pIsfAL );
00411 Bdc_IsfClean( p->pIsfAR );
00412
00413
00414 CostOr = Bdc_DecomposeOr( p, pIsf, p->pIsfOL, p->pIsfOR );
00415
00416
00417 Bdc_IsfNot( pIsf );
00418 CostAnd = Bdc_DecomposeOr( p, pIsf, p->pIsfAL, p->pIsfAR );
00419 Bdc_IsfNot( pIsf );
00420 Bdc_IsfNot( p->pIsfAL );
00421 Bdc_IsfNot( p->pIsfAR );
00422
00423
00424 Bdc_SuppMinimize( p, p->pIsfOL );
00425 CostOrL = (Bdc_TableLookup(p, p->pIsfOL) != NULL);
00426 Bdc_SuppMinimize( p, p->pIsfOR );
00427 CostOrR = (Bdc_TableLookup(p, p->pIsfOR) != NULL);
00428 Bdc_SuppMinimize( p, p->pIsfAL );
00429 CostAndL = (Bdc_TableLookup(p, p->pIsfAL) != NULL);
00430 Bdc_SuppMinimize( p, p->pIsfAR );
00431 CostAndR = (Bdc_TableLookup(p, p->pIsfAR) != NULL);
00432
00433
00434 if ( CostOrL + CostOrR < CostAndL + CostAndR )
00435 {
00436 Bdc_IsfCopy( pIsfL, p->pIsfOL );
00437 Bdc_IsfCopy( pIsfR, p->pIsfOR );
00438 return BDC_TYPE_OR;
00439 }
00440 if ( CostOrL + CostOrR > CostAndL + CostAndR )
00441 {
00442 Bdc_IsfCopy( pIsfL, p->pIsfAL );
00443 Bdc_IsfCopy( pIsfR, p->pIsfAR );
00444 return BDC_TYPE_AND;
00445 }
00446
00447
00448 if ( CostOr < CostAnd )
00449 {
00450 Bdc_IsfCopy( pIsfL, p->pIsfOL );
00451 Bdc_IsfCopy( pIsfR, p->pIsfOR );
00452 return BDC_TYPE_OR;
00453 }
00454 return BDC_TYPE_AND;
00455 }
00456
00460
00461