bdcDec.c 25.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 22
/**CFile****************************************************************

  FileName    [bdcDec.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Truth-table-based bi-decomposition engine.]

  Synopsis    [Decomposition procedures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - January 30, 2007.]

  Revision    [$Id: bdcDec.c,v 1.00 2007/01/30 00:00:00 alanmi Exp $]

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

#include "bdcInt.h"

23 24 25
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
26 27 28 29 30 31 32 33 34 35
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

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

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

Alan Mishchenko committed
36 37 38 39 40 41 42 43 44 45 46
  Synopsis    [Minimizes the support of the ISF.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Bdc_SuppMinimize2( Bdc_Man_t * p, Bdc_Isf_t * pIsf )
{
Alan Mishchenko committed
47
    int v;
48
    abctime clk = 0; // Suppress "might be used uninitialized"
Alan Mishchenko committed
49
    if ( p->pPars->fVerbose )
50
        clk = Abc_Clock();
Alan Mishchenko committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    // compute support
    pIsf->uSupp = Kit_TruthSupport( pIsf->puOn, p->nVars ) | 
        Kit_TruthSupport( pIsf->puOff, p->nVars );
    // go through the support variables
    for ( v = 0; v < p->nVars; v++ )
    {
        if ( (pIsf->uSupp & (1 << v)) == 0 )
            continue;
        Kit_TruthExistNew( p->puTemp1, pIsf->puOn, p->nVars, v );
        Kit_TruthExistNew( p->puTemp2, pIsf->puOff, p->nVars, v );
        if ( !Kit_TruthIsDisjoint( p->puTemp1, p->puTemp2, p->nVars ) )
            continue;
//        if ( !Kit_TruthVarIsVacuous( pIsf->puOn, pIsf->puOff, p->nVars, v ) )
//            continue;
        // remove the variable
        Kit_TruthCopy( pIsf->puOn, p->puTemp1, p->nVars );
        Kit_TruthCopy( pIsf->puOff, p->puTemp2, p->nVars );
//        Kit_TruthExist( pIsf->puOn, p->nVars, v );
//        Kit_TruthExist( pIsf->puOff, p->nVars, v );
        pIsf->uSupp &= ~(1 << v);
    }
    if ( p->pPars->fVerbose )
73
        p->timeSupps += Abc_Clock() - clk;
Alan Mishchenko committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
}

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

  Synopsis    [Minimizes the support of the ISF.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Bdc_SuppMinimize( Bdc_Man_t * p, Bdc_Isf_t * pIsf )
{
Alan Mishchenko committed
89
    int v;
90
    abctime clk = 0; // Suppress "might be used uninitialized"
Alan Mishchenko committed
91
    if ( p->pPars->fVerbose )
92
        clk = Abc_Clock();
Alan Mishchenko committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    // go through the support variables
    pIsf->uSupp = 0;
    for ( v = 0; v < p->nVars; v++ )
    {
        if ( !Kit_TruthVarInSupport( pIsf->puOn, p->nVars, v ) && 
             !Kit_TruthVarInSupport( pIsf->puOff, p->nVars, v ) )
              continue;
        if ( Kit_TruthVarIsVacuous( pIsf->puOn, pIsf->puOff, p->nVars, v ) )
        {
            Kit_TruthExist( pIsf->puOn, p->nVars, v );
            Kit_TruthExist( pIsf->puOff, p->nVars, v );
            continue;
        }
        pIsf->uSupp |= (1 << v);
    }
    if ( p->pPars->fVerbose )
109
        p->timeSupps += Abc_Clock() - clk;
Alan Mishchenko committed
110 111 112 113
}

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

Alan Mishchenko committed
114 115 116 117 118 119 120 121 122
  Synopsis    [Updates the ISF of the right after the left was decompoosed.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
123
int Bdc_DecomposeUpdateRight( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR, Bdc_Fun_t * pFunc0, Bdc_Type_t Type )
Alan Mishchenko committed
124
{
Alan Mishchenko committed
125 126 127 128 129 130 131
    unsigned * puTruth = p->puTemp1;
    // get the truth table of the left branch
    if ( Bdc_IsComplement(pFunc0) )
        Kit_TruthNot( puTruth, Bdc_Regular(pFunc0)->puFunc, p->nVars );
    else
        Kit_TruthCopy( puTruth, pFunc0->puFunc, p->nVars );
    // split into parts
Alan Mishchenko committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145
    if ( Type == BDC_TYPE_OR ) 
    {
//        Right.Q = bdd_appex( Q, CompSpecLeftF, bddop_diff, setRightRes );
//        Right.R = bdd_exist( R, setRightRes );

//        if ( pR->Q )  Cudd_RecursiveDeref( dd, pR->Q );
//        if ( pR->R )  Cudd_RecursiveDeref( dd, pR->R );
//        pR->Q = Cudd_bddAndAbstract( dd, pF->Q, Cudd_Not(CompSpecF), pL->V );    Cudd_Ref( pR->Q );
//        pR->R = Cudd_bddExistAbstract( dd, pF->R, pL->V );                      Cudd_Ref( pR->R );

//        assert( pR->R != b0 );
//        return (int)( pR->Q == b0 );

        Kit_TruthSharp( pIsfR->puOn, pIsf->puOn, puTruth, p->nVars );
Alan Mishchenko committed
146 147 148
        Kit_TruthExistSet( pIsfR->puOn, pIsfR->puOn, p->nVars, pIsfL->uUniq ); 
        Kit_TruthExistSet( pIsfR->puOff, pIsf->puOff, p->nVars, pIsfL->uUniq ); 
//        assert( Kit_TruthIsDisjoint(pIsfR->puOn, pIsfR->puOff, p->nVars) );
Alan Mishchenko committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        assert( !Kit_TruthIsConst0(pIsfR->puOff, p->nVars) );
        return Kit_TruthIsConst0(pIsfR->puOn, p->nVars);
    }
    else if ( Type == BDC_TYPE_AND )
    {
//        Right.R = bdd_appex( R, CompSpecLeftF, bddop_and, setRightRes );
//        Right.Q = bdd_exist( Q, setRightRes );

//        if ( pR->Q )  Cudd_RecursiveDeref( dd, pR->Q );
//        if ( pR->R )  Cudd_RecursiveDeref( dd, pR->R );
//        pR->R = Cudd_bddAndAbstract( dd, pF->R, CompSpecF, pL->V );                Cudd_Ref( pR->R );
//        pR->Q = Cudd_bddExistAbstract( dd, pF->Q, pL->V );                      Cudd_Ref( pR->Q );

//        assert( pR->Q != b0 );
//        return (int)( pR->R == b0 );

Alan Mishchenko committed
165 166 167 168 169 170
        Kit_TruthAnd( pIsfR->puOff, pIsf->puOff, puTruth, p->nVars );
        Kit_TruthExistSet( pIsfR->puOff, pIsfR->puOff, p->nVars, pIsfL->uUniq ); 
        Kit_TruthExistSet( pIsfR->puOn, pIsf->puOn, p->nVars, pIsfL->uUniq ); 
//        assert( Kit_TruthIsDisjoint(pIsfR->puOn, pIsfR->puOff, p->nVars) );
        assert( !Kit_TruthIsConst0(pIsfR->puOn, p->nVars) );
        return Kit_TruthIsConst0(pIsfR->puOff, p->nVars);
Alan Mishchenko committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    }
    return 0;
}

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

  Synopsis    [Checks existence of OR-bidecomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Bdc_DecomposeGetCost( Bdc_Man_t * p, int nLeftVars, int nRightVars )
{
    assert( nLeftVars > 0 );
    assert( nRightVars > 0 );
    // compute the decomposition coefficient
    if ( nLeftVars >= nRightVars )
        return BDC_SCALE * (p->nVars * nRightVars + nLeftVars);
    else // if ( nLeftVars < nRightVars )
        return BDC_SCALE * (p->nVars * nLeftVars + nRightVars);
}

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

  Synopsis    [Checks existence of weak OR-bidecomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Bdc_DecomposeFindInitialVarSet( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR )
{
    char pVars[16];
    int v, nVars, Beg, End;

    assert( pIsfL->uSupp == 0 );
    assert( pIsfR->uSupp == 0 );

    // fill in the variables
    nVars = 0;
    for ( v = 0; v < p->nVars; v++ )
        if ( pIsf->uSupp & (1 << v) )
            pVars[nVars++] = v;

    // try variable pairs
    for ( Beg = 0; Beg < nVars; Beg++ )
    {
        Kit_TruthExistNew( p->puTemp1, pIsf->puOff, p->nVars, pVars[Beg] ); 
        for ( End = nVars - 1; End > Beg; End-- )
        {
            Kit_TruthExistNew( p->puTemp2, pIsf->puOff, p->nVars, pVars[End] ); 
            if ( Kit_TruthIsDisjoint3(pIsf->puOn, p->puTemp1, p->puTemp2, p->nVars) )
            {
Alan Mishchenko committed
231 232
                pIsfL->uUniq = (1 << pVars[Beg]);
                pIsfR->uUniq = (1 << pVars[End]);
Alan Mishchenko committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
                return 1;
            }
        }
    }
    return 0;
}

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

  Synopsis    [Checks existence of weak OR-bidecomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Bdc_DecomposeWeakOr( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR )
{
Alan Mishchenko committed
253 254 255
    int v, VarCost;
  int VarBest = -1; // Suppress "might be used uninitialized"
  int Cost, VarCostBest = 0;
Alan Mishchenko committed
256 257 258

    for ( v = 0; v < p->nVars; v++ )
    {
Alan Mishchenko committed
259 260
        if ( (pIsf->uSupp & (1 << v)) == 0 )
            continue;
Alan Mishchenko committed
261 262 263
//        if ( (Q & !bdd_exist( R, VarSetXa )) != bddfalse )
//        Exist = Cudd_bddExistAbstract( dd, pF->R, Var );   Cudd_Ref( Exist );
//        if ( Cudd_bddIteConstant( dd, pF->Q, Cudd_Not(Exist), b0 ) != b0 )
Alan Mishchenko committed
264 265

        Kit_TruthExistNew( p->puTemp1, pIsf->puOff, p->nVars, v );
Alan Mishchenko committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
        if ( !Kit_TruthIsImply( pIsf->puOn, p->puTemp1, p->nVars ) )
        {
            // measure the cost of this variable
//            VarCost = bdd_satcountset( bdd_forall( Q, VarSetXa ), VarCube );
//            Univ = Cudd_bddUnivAbstract( dd, pF->Q, Var );   Cudd_Ref( Univ );
//            VarCost = Kit_TruthCountOnes( Univ, p->nVars );
//            Cudd_RecursiveDeref( dd, Univ );

            Kit_TruthForallNew( p->puTemp2, pIsf->puOn, p->nVars, v );
            VarCost = Kit_TruthCountOnes( p->puTemp2, p->nVars );
            if ( VarCost == 0 )
                VarCost = 1;
            if ( VarCostBest < VarCost )
            {
                VarCostBest = VarCost;
                VarBest = v;
            }
        }
    }

    // derive the components for weak-bi-decomposition if the variable is found
    if ( VarCostBest )
    {
//        funQLeftRes = Q & bdd_exist( R, setRightORweak );
//        Temp = Cudd_bddExistAbstract( dd, pF->R, VarBest );     Cudd_Ref( Temp );
//        pL->Q = Cudd_bddAnd( dd, pF->Q, Temp );                    Cudd_Ref( pL->Q );
//        Cudd_RecursiveDeref( dd, Temp );

        Kit_TruthExistNew( p->puTemp1, pIsf->puOff, p->nVars, VarBest );
        Kit_TruthAnd( pIsfL->puOn, pIsf->puOn, p->puTemp1, p->nVars );

//        pL->R = pF->R;                                            Cudd_Ref( pL->R );
//        pL->V = VarBest;                                        Cudd_Ref( pL->V );
        Kit_TruthCopy( pIsfL->puOff, pIsf->puOff, p->nVars );
Alan Mishchenko committed
300 301
        pIsfL->uUniq = (1 << VarBest);
        pIsfR->uUniq = 0;
Alan Mishchenko committed
302 303 304 305

//        assert( pL->Q != b0 );
//        assert( pL->R != b0 );
//        assert( Cudd_bddIteConstant( dd, pL->Q, pL->R, b0 ) == b0 );
Alan Mishchenko committed
306
//        assert( Kit_TruthIsDisjoint(pIsfL->puOn, pIsfL->puOff, p->nVars) );
Alan Mishchenko committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

        // express cost in percents of the covered boolean space
        Cost = VarCostBest * BDC_SCALE / (1<<p->nVars);
        if ( Cost == 0 )
            Cost = 1;
        return Cost;
    }
    return 0;
}

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

  Synopsis    [Checks existence of OR-bidecomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Bdc_DecomposeOr( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR )
{
Alan Mishchenko committed
330
    unsigned uSupportRem;
Alan Mishchenko committed
331 332
    int v, nLeftVars = 1, nRightVars = 1; 
    // clean the var sets
Alan Mishchenko committed
333 334 335 336 337
    Bdc_IsfStart( p, pIsfL );
    Bdc_IsfStart( p, pIsfR );
    // check that the support is correct
    assert( Kit_TruthSupport(pIsf->puOn, p->nVars) == Kit_TruthSupport(pIsf->puOff, p->nVars) );
    assert( pIsf->uSupp == Kit_TruthSupport(pIsf->puOn, p->nVars) );
Alan Mishchenko committed
338 339 340 341
    // find initial variable sets
    if ( !Bdc_DecomposeFindInitialVarSet( p, pIsf, pIsfL, pIsfR ) )
        return Bdc_DecomposeWeakOr( p, pIsf, pIsfL, pIsfR );
    // prequantify the variables in the offset
Alan Mishchenko committed
342 343
    Kit_TruthExistSet( p->puTemp1, pIsf->puOff, p->nVars, pIsfL->uUniq ); 
    Kit_TruthExistSet( p->puTemp2, pIsf->puOff, p->nVars, pIsfR->uUniq );
Alan Mishchenko committed
344
    // go through the remaining variables
Alan Mishchenko committed
345
    uSupportRem = pIsf->uSupp & ~pIsfL->uUniq & ~pIsfR->uUniq;
Alan Mishchenko committed
346 347
    for ( v = 0; v < p->nVars; v++ )
    {
Alan Mishchenko committed
348
        if ( (uSupportRem & (1 << v)) == 0 )
Alan Mishchenko committed
349 350 351 352 353 354 355 356 357 358 359
            continue;
        // prequantify this variable
        Kit_TruthExistNew( p->puTemp3, p->puTemp1, p->nVars, v );
        Kit_TruthExistNew( p->puTemp4, p->puTemp2, p->nVars, v );
        if ( nLeftVars < nRightVars )
        {
//            if ( (Q & bdd_exist( pF->R, pL->V & VarNew ) & bdd_exist( pF->R, pR->V )) == bddfalse )
//            if ( VerifyORCondition( dd, pF->Q, pF->R, pL->V, pR->V, VarNew ) )
            if ( Kit_TruthIsDisjoint3(pIsf->puOn, p->puTemp3, p->puTemp2, p->nVars) )
            {
//                pL->V &= VarNew;
Alan Mishchenko committed
360
                pIsfL->uUniq |= (1 << v);
Alan Mishchenko committed
361
                nLeftVars++;
Alan Mishchenko committed
362
                Kit_TruthCopy( p->puTemp1, p->puTemp3, p->nVars );
Alan Mishchenko committed
363 364 365 366 367
            }
//            else if ( (Q & bdd_exist( pF->R, pR->V & VarNew ) & bdd_exist( pF->R, pL->V )) == bddfalse )
            else if ( Kit_TruthIsDisjoint3(pIsf->puOn, p->puTemp4, p->puTemp1, p->nVars) )
            {
//                pR->V &= VarNew;
Alan Mishchenko committed
368
                pIsfR->uUniq |= (1 << v);
Alan Mishchenko committed
369
                nRightVars++;
Alan Mishchenko committed
370
                Kit_TruthCopy( p->puTemp2, p->puTemp4, p->nVars );
Alan Mishchenko committed
371 372 373 374 375 376 377 378
            }
        }
        else
        {
//            if ( (Q & bdd_exist( pF->R, pR->V & VarNew ) & bdd_exist( pF->R, pL->V )) == bddfalse )
            if ( Kit_TruthIsDisjoint3(pIsf->puOn, p->puTemp4, p->puTemp1, p->nVars) )
            {
//                pR->V &= VarNew;
Alan Mishchenko committed
379
                pIsfR->uUniq |= (1 << v);
Alan Mishchenko committed
380
                nRightVars++;
Alan Mishchenko committed
381
                Kit_TruthCopy( p->puTemp2, p->puTemp4, p->nVars );
Alan Mishchenko committed
382 383 384 385 386
            }
//            else if ( (Q & bdd_exist( pF->R, pL->V & VarNew ) & bdd_exist( pF->R, pR->V )) == bddfalse )
            else if ( Kit_TruthIsDisjoint3(pIsf->puOn, p->puTemp3, p->puTemp2, p->nVars) )
            {
//                pL->V &= VarNew;
Alan Mishchenko committed
387
                pIsfL->uUniq |= (1 << v);
Alan Mishchenko committed
388
                nLeftVars++;
Alan Mishchenko committed
389
                Kit_TruthCopy( p->puTemp1, p->puTemp3, p->nVars );
Alan Mishchenko committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403
            }
        }
    }

    // derive the functions Q and R for the left branch
//    pL->Q = bdd_appex( pF->Q, bdd_exist( pF->R, pL->V ), bddop_and, pR->V );
//    pL->R = bdd_exist( pF->R, pR->V );

//    Temp = Cudd_bddExistAbstract( dd, pF->R, pL->V );      Cudd_Ref( Temp );
//    pL->Q = Cudd_bddAndAbstract( dd, pF->Q, Temp, pR->V ); Cudd_Ref( pL->Q );
//    Cudd_RecursiveDeref( dd, Temp );
//    pL->R = Cudd_bddExistAbstract( dd, pF->R, pR->V );     Cudd_Ref( pL->R );

    Kit_TruthAnd( pIsfL->puOn, pIsf->puOn, p->puTemp1, p->nVars );
Alan Mishchenko committed
404
    Kit_TruthExistSet( pIsfL->puOn, pIsfL->puOn, p->nVars, pIsfR->uUniq );
Alan Mishchenko committed
405 406
    Kit_TruthCopy( pIsfL->puOff, p->puTemp2, p->nVars );

Alan Mishchenko committed
407 408 409 410 411 412 413
//    assert( pL->Q != b0 );
//    assert( pL->R != b0 );
//    assert( Cudd_bddIteConstant( dd, pL->Q, pL->R, b0 ) == b0 );
    assert( !Kit_TruthIsConst0(pIsfL->puOn, p->nVars) );
    assert( !Kit_TruthIsConst0(pIsfL->puOff, p->nVars) );
//    assert( Kit_TruthIsDisjoint(pIsfL->puOn, pIsfL->puOff, p->nVars) );

Alan Mishchenko committed
414 415 416 417 418 419 420
    // derive the functions Q and R for the right branch
//    Temp = Cudd_bddExistAbstract( dd, pF->R, pR->V );      Cudd_Ref( Temp );
//    pR->Q = Cudd_bddAndAbstract( dd, pF->Q, Temp, pL->V ); Cudd_Ref( pR->Q );
//    Cudd_RecursiveDeref( dd, Temp );
//    pR->R = Cudd_bddExistAbstract( dd, pF->R, pL->V );     Cudd_Ref( pR->R );

    Kit_TruthAnd( pIsfR->puOn, pIsf->puOn, p->puTemp2, p->nVars );
Alan Mishchenko committed
421
    Kit_TruthExistSet( pIsfR->puOn, pIsfR->puOn, p->nVars, pIsfL->uUniq );
Alan Mishchenko committed
422 423
    Kit_TruthCopy( pIsfR->puOff, p->puTemp1, p->nVars );

Alan Mishchenko committed
424 425 426
    assert( !Kit_TruthIsConst0(pIsfR->puOn, p->nVars) );
    assert( !Kit_TruthIsConst0(pIsfR->puOff, p->nVars) );
//    assert( Kit_TruthIsDisjoint(pIsfR->puOn, pIsfR->puOff, p->nVars) );
Alan Mishchenko committed
427

Alan Mishchenko committed
428 429
    assert( pIsfL->uUniq );
    assert( pIsfR->uUniq );
Alan Mishchenko committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
    return Bdc_DecomposeGetCost( p, nLeftVars, nRightVars );        
}

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

  Synopsis    [Performs one step of bi-decomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Bdc_Type_t Bdc_DecomposeStep( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR )
{
Alan Mishchenko committed
446
    int WeightOr, WeightAnd, WeightOrL, WeightOrR, WeightAndL, WeightAndR;
Alan Mishchenko committed
447 448 449 450 451 452 453

    Bdc_IsfClean( p->pIsfOL );
    Bdc_IsfClean( p->pIsfOR );
    Bdc_IsfClean( p->pIsfAL );
    Bdc_IsfClean( p->pIsfAR );

    // perform OR decomposition
Alan Mishchenko committed
454
    WeightOr = Bdc_DecomposeOr( p, pIsf, p->pIsfOL, p->pIsfOR );
Alan Mishchenko committed
455 456 457

    // perform AND decomposition
    Bdc_IsfNot( pIsf );
Alan Mishchenko committed
458
    WeightAnd = Bdc_DecomposeOr( p, pIsf, p->pIsfAL, p->pIsfAR );
Alan Mishchenko committed
459 460 461 462
    Bdc_IsfNot( pIsf );
    Bdc_IsfNot( p->pIsfAL );
    Bdc_IsfNot( p->pIsfAR );

Alan Mishchenko committed
463 464 465 466 467 468 469
    // check the case when decomposition does not exist
    if ( WeightOr == 0 && WeightAnd == 0 )
    {
        Bdc_IsfCopy( pIsfL, p->pIsfOL );
        Bdc_IsfCopy( pIsfR, p->pIsfOR );
        return BDC_TYPE_MUX;
    }
Alan Mishchenko committed
470
    // check the hash table
Alan Mishchenko committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    assert( WeightOr || WeightAnd );
    WeightOrL = WeightOrR = 0;
    if ( WeightOr )
    {
        if ( p->pIsfOL->uUniq )
        {
            Bdc_SuppMinimize( p, p->pIsfOL );
            WeightOrL = (Bdc_TableLookup(p, p->pIsfOL) != NULL);
        }
        if ( p->pIsfOR->uUniq )
        {
            Bdc_SuppMinimize( p, p->pIsfOR );
            WeightOrR = (Bdc_TableLookup(p, p->pIsfOR) != NULL);
        }
    }
    WeightAndL = WeightAndR = 0;
    if ( WeightAnd )
    {
        if ( p->pIsfAL->uUniq )
        {
            Bdc_SuppMinimize( p, p->pIsfAL );
            WeightAndL = (Bdc_TableLookup(p, p->pIsfAL) != NULL);
        }
        if ( p->pIsfAR->uUniq )
        {
            Bdc_SuppMinimize( p, p->pIsfAR );
            WeightAndR = (Bdc_TableLookup(p, p->pIsfAR) != NULL);
        }
    }
Alan Mishchenko committed
500 501

    // check if there is any reuse for the components
Alan Mishchenko committed
502
    if ( WeightOrL + WeightOrR > WeightAndL + WeightAndR )
Alan Mishchenko committed
503
    {
Alan Mishchenko committed
504 505
        p->numReuse++;
        p->numOrs++;
Alan Mishchenko committed
506 507 508 509
        Bdc_IsfCopy( pIsfL, p->pIsfOL );
        Bdc_IsfCopy( pIsfR, p->pIsfOR );
        return BDC_TYPE_OR;
    }
Alan Mishchenko committed
510
    if ( WeightOrL + WeightOrR < WeightAndL + WeightAndR )
Alan Mishchenko committed
511
    {
Alan Mishchenko committed
512 513
        p->numReuse++;
        p->numAnds++;
Alan Mishchenko committed
514 515 516 517 518 519
        Bdc_IsfCopy( pIsfL, p->pIsfAL );
        Bdc_IsfCopy( pIsfR, p->pIsfAR );
        return BDC_TYPE_AND; 
    }

    // compare the two-component costs
Alan Mishchenko committed
520
    if ( WeightOr > WeightAnd )
Alan Mishchenko committed
521
    {
Alan Mishchenko committed
522 523 524
        if ( WeightOr < BDC_SCALE )
            p->numWeaks++;
        p->numOrs++;
Alan Mishchenko committed
525 526 527 528
        Bdc_IsfCopy( pIsfL, p->pIsfOL );
        Bdc_IsfCopy( pIsfR, p->pIsfOR );
        return BDC_TYPE_OR;
    }
Alan Mishchenko committed
529 530 531
    if ( WeightAnd < BDC_SCALE )
        p->numWeaks++;
    p->numAnds++;
Alan Mishchenko committed
532 533
    Bdc_IsfCopy( pIsfL, p->pIsfAL );
    Bdc_IsfCopy( pIsfR, p->pIsfAR );
Alan Mishchenko committed
534 535 536
    return BDC_TYPE_AND;
}

Alan Mishchenko committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
/**Function*************************************************************

  Synopsis    [Find variable that leads to minimum sum of support sizes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Bdc_DecomposeStepMux( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Isf_t * pIsfL, Bdc_Isf_t * pIsfR )
{
    int Var, VarMin, nSuppMin, nSuppCur;
    unsigned uSupp0, uSupp1;
552
    abctime clk = 0; // Suppress "might be used uninitialized"
Alan Mishchenko committed
553
    if ( p->pPars->fVerbose )
554
        clk = Abc_Clock();
Alan Mishchenko committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
    VarMin = -1;
    nSuppMin = 1000;
    for ( Var = 0; Var < p->nVars; Var++ )
    {
        if ( (pIsf->uSupp & (1 << Var)) == 0 )
            continue;
        Kit_TruthCofactor0New( pIsfL->puOn,  pIsf->puOn,  p->nVars, Var );
        Kit_TruthCofactor0New( pIsfL->puOff, pIsf->puOff, p->nVars, Var );
        Kit_TruthCofactor1New( pIsfR->puOn,  pIsf->puOn,  p->nVars, Var );
        Kit_TruthCofactor1New( pIsfR->puOff, pIsf->puOff, p->nVars, Var );
        uSupp0 = Kit_TruthSupport( pIsfL->puOn, p->nVars ) & Kit_TruthSupport( pIsfL->puOff, p->nVars );
        uSupp1 = Kit_TruthSupport( pIsfR->puOn, p->nVars ) & Kit_TruthSupport( pIsfR->puOff, p->nVars );
        nSuppCur = Kit_WordCountOnes(uSupp0) + Kit_WordCountOnes(uSupp1);  
        if ( nSuppMin > nSuppCur )
        {
            nSuppMin = nSuppCur;
            VarMin = Var;
Alan Mishchenko committed
572
            break;
Alan Mishchenko committed
573 574 575 576 577 578 579 580 581 582 583
        }
    }
    if ( VarMin >= 0 )
    {
        Kit_TruthCofactor0New( pIsfL->puOn,  pIsf->puOn,  p->nVars, VarMin );
        Kit_TruthCofactor0New( pIsfL->puOff, pIsf->puOff, p->nVars, VarMin );
        Kit_TruthCofactor1New( pIsfR->puOn,  pIsf->puOn,  p->nVars, VarMin );
        Kit_TruthCofactor1New( pIsfR->puOff, pIsf->puOff, p->nVars, VarMin );
        Bdc_SuppMinimize( p, pIsfL );
        Bdc_SuppMinimize( p, pIsfR );
    }
Alan Mishchenko committed
584
    if ( p->pPars->fVerbose )
585
        p->timeMuxes += Abc_Clock() - clk;
Alan Mishchenko committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
    return VarMin;
}

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

  Synopsis    [Creates gates.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Bdc_ManNodeVerify( Bdc_Man_t * p, Bdc_Isf_t * pIsf, Bdc_Fun_t * pFunc )
{
    unsigned * puTruth = p->puTemp1;
    if ( Bdc_IsComplement(pFunc) )
        Kit_TruthNot( puTruth, Bdc_Regular(pFunc)->puFunc, p->nVars );
    else
        Kit_TruthCopy( puTruth, pFunc->puFunc, p->nVars );
    return Bdc_TableCheckContainment( p, pIsf, puTruth );
}

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

  Synopsis    [Creates gates.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Bdc_Fun_t * Bdc_ManCreateGate( Bdc_Man_t * p, Bdc_Fun_t * pFunc0, Bdc_Fun_t * pFunc1, Bdc_Type_t Type )
{
    Bdc_Fun_t * pFunc;
    pFunc = Bdc_FunNew( p );
    if ( pFunc == NULL )
        return NULL;
    pFunc->Type = Type;
    pFunc->pFan0 = pFunc0;
    pFunc->pFan1 = pFunc1;
    pFunc->puFunc = (unsigned *)Vec_IntFetch(p->vMemory, p->nWords); 
    // get the truth table of the left branch
    if ( Bdc_IsComplement(pFunc0) )
        Kit_TruthNot( p->puTemp1, Bdc_Regular(pFunc0)->puFunc, p->nVars );
    else
        Kit_TruthCopy( p->puTemp1, pFunc0->puFunc, p->nVars );
    // get the truth table of the right branch
    if ( Bdc_IsComplement(pFunc1) )
        Kit_TruthNot( p->puTemp2, Bdc_Regular(pFunc1)->puFunc, p->nVars );
    else
        Kit_TruthCopy( p->puTemp2, pFunc1->puFunc, p->nVars );
    // compute the function of node
    if ( pFunc->Type == BDC_TYPE_AND )
    {
        Kit_TruthAnd( pFunc->puFunc, p->puTemp1, p->puTemp2, p->nVars );
    }
    else if ( pFunc->Type == BDC_TYPE_OR )
    {
        Kit_TruthOr( pFunc->puFunc, p->puTemp1, p->puTemp2, p->nVars );
        // transform to AND gate
        pFunc->Type = BDC_TYPE_AND;
        pFunc->pFan0 = Bdc_Not(pFunc->pFan0);
        pFunc->pFan1 = Bdc_Not(pFunc->pFan1);
        Kit_TruthNot( pFunc->puFunc, pFunc->puFunc, p->nVars );
        pFunc = Bdc_Not(pFunc);
    }
    else 
        assert( 0 );
    // add to table
    Bdc_Regular(pFunc)->uSupp = Kit_TruthSupport( Bdc_Regular(pFunc)->puFunc, p->nVars );
    Bdc_TableAdd( p, Bdc_Regular(pFunc) );
    return pFunc;
}

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

  Synopsis    [Performs one step of bi-decomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Bdc_Fun_t * Bdc_ManDecompose_rec( Bdc_Man_t * p, Bdc_Isf_t * pIsf )
{
Alan Mishchenko committed
677 678
//    int static Counter = 0;
//    int LocalCounter = Counter++;
Alan Mishchenko committed
679 680 681 682
    Bdc_Type_t Type;
    Bdc_Fun_t * pFunc, * pFunc0, * pFunc1;
    Bdc_Isf_t IsfL, * pIsfL = &IsfL;
    Bdc_Isf_t IsfB, * pIsfR = &IsfB;
Alan Mishchenko committed
683
    int iVar;
684
    abctime clk = 0; // Suppress "might be used uninitialized"
Alan Mishchenko committed
685 686 687 688 689 690 691
/*
printf( "Init function (%d):\n", LocalCounter );
Extra_PrintBinary( stdout, pIsf->puOn, 1<<4 );printf("\n");
Extra_PrintBinary( stdout, pIsf->puOff, 1<<4 );printf("\n");
*/
    // check computed results
    assert( Kit_TruthIsDisjoint(pIsf->puOn, pIsf->puOff, p->nVars) );
Alan Mishchenko committed
692
    if ( p->pPars->fVerbose )
693
        clk = Abc_Clock();
Alan Mishchenko committed
694 695
    pFunc = Bdc_TableLookup( p, pIsf );
    if ( p->pPars->fVerbose )
696
        p->timeCache += Abc_Clock() - clk;
Alan Mishchenko committed
697
    if ( pFunc )
Alan Mishchenko committed
698 699
        return pFunc;
    // decide on the decomposition type
Alan Mishchenko committed
700
    if ( p->pPars->fVerbose )
701
        clk = Abc_Clock();
Alan Mishchenko committed
702
    Type = Bdc_DecomposeStep( p, pIsf, pIsfL, pIsfR );
Alan Mishchenko committed
703
    if ( p->pPars->fVerbose )
704
        p->timeCheck += Abc_Clock() - clk;
Alan Mishchenko committed
705 706
    if ( Type == BDC_TYPE_MUX )
    {
Alan Mishchenko committed
707
        if ( p->pPars->fVerbose )
708
            clk = Abc_Clock();
Alan Mishchenko committed
709 710
        iVar = Bdc_DecomposeStepMux( p, pIsf, pIsfL, pIsfR );
        if ( p->pPars->fVerbose )
711
            p->timeMuxes += Abc_Clock() - clk;
Alan Mishchenko committed
712
        p->numMuxes++;
Alan Mishchenko committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
        pFunc0 = Bdc_ManDecompose_rec( p, pIsfL );
        pFunc1 = Bdc_ManDecompose_rec( p, pIsfR );
        if ( pFunc0 == NULL || pFunc1 == NULL )
            return NULL;
        pFunc  = Bdc_FunWithId( p, iVar + 1 );
        pFunc0 = Bdc_ManCreateGate( p, Bdc_Not(pFunc), pFunc0, BDC_TYPE_AND );
        pFunc1 = Bdc_ManCreateGate( p, pFunc,  pFunc1, BDC_TYPE_AND );
        if ( pFunc0 == NULL || pFunc1 == NULL )
            return NULL;
        pFunc = Bdc_ManCreateGate( p, pFunc0, pFunc1, BDC_TYPE_OR );
    }
    else
    {
        pFunc0 = Bdc_ManDecompose_rec( p, pIsfL );
        if ( pFunc0 == NULL )
            return NULL;
        // decompose the right branch
        if ( Bdc_DecomposeUpdateRight( p, pIsf, pIsfL, pIsfR, pFunc0, Type ) )
        {
            p->nNodesNew--;
            return pFunc0;
        }
        Bdc_SuppMinimize( p, pIsfR );
        pFunc1 = Bdc_ManDecompose_rec( p, pIsfR );
        if ( pFunc1 == NULL )
            return NULL;
        // create new gate
        pFunc = Bdc_ManCreateGate( p, pFunc0, pFunc1, Type );
    }
    return pFunc;
}

Alan Mishchenko committed
745 746 747 748 749
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


750 751
ABC_NAMESPACE_IMPL_END