abcCascade.c 32.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**CFile****************************************************************

  FileName    [abcCascade.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Network and node package.]

  Synopsis    [Collapsing the network into two-levels.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: abcCollapse.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

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

21
#include "base/abc/abc.h"
22 23

#ifdef ABC_USE_CUDD
24
#include "bdd/reo/reo.h"
25
#include "bdd/extrab/extraBdd.h"
26
#endif
27 28 29 30 31 32 33

ABC_NAMESPACE_IMPL_START

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

34 35
#ifdef ABC_USE_CUDD

36 37 38
#define BDD_FUNC_MAX 256

//extern void Abc_NodeShowBddOne( DdManager * dd, DdNode * bFunc );
39
extern DdNode * Abc_ConvertSopToBdd( DdManager * dd, char * pSop, DdNode ** pbVars );
40 41 42 43 44 45 46

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

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

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  Synopsis    [Derive BDD of the characteristic function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Abc_ResBuildBdd( Abc_Ntk_t * pNtk, DdManager * dd )
{
    Vec_Ptr_t * vNodes, * vBdds, * vLocals;
    Abc_Obj_t * pObj, * pFanin;
    DdNode * bFunc, * bPart, * bTemp, * bVar;
    int i, k;
    assert( Abc_NtkIsSopLogic(pNtk) );
    assert( Abc_NtkCoNum(pNtk) <= 3 );
    vBdds = Vec_PtrStart( Abc_NtkObjNumMax(pNtk) );
    Abc_NtkForEachCi( pNtk, pObj, i )
        Vec_PtrWriteEntry( vBdds, Abc_ObjId(pObj), Cudd_bddIthVar(dd, i) );
    // create internal node BDDs
    vNodes = Abc_NtkDfs( pNtk, 0 );
    vLocals = Vec_PtrAlloc( 6 );
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
    {
        if ( Abc_ObjFaninNum(pObj) == 0 )
        {
            bFunc = Cudd_NotCond( Cudd_ReadOne(dd), Abc_SopIsConst0((char *)pObj->pData) );  Cudd_Ref( bFunc );
            Vec_PtrWriteEntry( vBdds, Abc_ObjId(pObj), bFunc );
            continue;
        }
        Vec_PtrClear( vLocals );
        Abc_ObjForEachFanin( pObj, pFanin, k )
            Vec_PtrPush( vLocals, Vec_PtrEntry(vBdds, Abc_ObjId(pFanin)) );
        bFunc = Abc_ConvertSopToBdd( dd, (char *)pObj->pData, (DdNode **)Vec_PtrArray(vLocals) );  Cudd_Ref( bFunc );
        Vec_PtrWriteEntry( vBdds, Abc_ObjId(pObj), bFunc );
    }
    Vec_PtrFree( vLocals );
    // create char function
    bFunc = Cudd_ReadOne( dd );  Cudd_Ref( bFunc );
    Abc_NtkForEachCo( pNtk, pObj, i )
    {
        bVar  = Cudd_bddIthVar( dd, i + Abc_NtkCiNum(pNtk) );
        bTemp = (DdNode *)Vec_PtrEntry( vBdds, Abc_ObjFaninId0(pObj) );
        bPart = Cudd_bddXnor( dd, bTemp, bVar );          Cudd_Ref( bPart );
        bFunc = Cudd_bddAnd( dd, bTemp = bFunc, bPart );  Cudd_Ref( bFunc );
        Cudd_RecursiveDeref( dd, bTemp );
        Cudd_RecursiveDeref( dd, bPart );
    }
    // dereference
    Vec_PtrForEachEntry( Abc_Obj_t *, vNodes, pObj, i )
        Cudd_RecursiveDeref( dd, (DdNode *)Vec_PtrEntry(vBdds, Abc_ObjId(pObj)) );
    Vec_PtrFree( vBdds );
    Vec_PtrFree( vNodes );
    // reorder
    Cudd_ReduceHeap( dd, CUDD_REORDER_SYMM_SIFT, 1 );
    Cudd_Deref( bFunc );
    return bFunc;
}
 
/**Function*************************************************************

  Synopsis    [Initializes variable partition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_ResStartPart( int nInputs, unsigned uParts[], int nParts )
{
    int i, Group, Left, Shift = 0, Count = 0;
    Group = nInputs / nParts;
    Left  = nInputs % nParts;
    for ( i = 0; i < Left; i++ )
    {
        uParts[i] = (~((~0) << (Group+1))) << Shift;
        Shift += Group+1;
    }
    for (      ; i < nParts; i++ )
    {
        uParts[i] = (~((~0) << Group)) << Shift;
        Shift += Group;
    }
    for ( i = 0; i < nParts; i++ )
        Count += Extra_WordCountOnes( uParts[i] );
    assert( Count == nInputs );
}
 
/**Function*************************************************************

  Synopsis    [Initializes variable partition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_ResStartPart2( int nInputs, unsigned uParts[], int nParts )
{
    int i, Count = 0;
    for ( i = 0; i < nParts; i++ )
        uParts[i] = 0;
    for ( i = 0; i < nInputs; i++ )
        uParts[i % nParts] |= (1 << i);
    for ( i = 0; i < nParts; i++ )
        Count += Extra_WordCountOnes( uParts[i] );
    assert( Count == nInputs );
}
 
/**Function*************************************************************

  Synopsis    [Returns one if unique pattern.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_ResCheckUnique( char Pats[], int nPats, int pat )
{
    int i;
    for ( i = 0; i < nPats; i++ )
        if ( Pats[i] == pat )
            return 0;
    return 1;
}
 
/**Function*************************************************************

  Synopsis    [Check if pattern is decomposable with non-strict.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_ResCheckNonStrict( char Pattern[], int nVars, int nBits )
{
    static char Pat0[256], Pat1[256];
    int v, m, nPats0, nPats1, nNumber = (1 << (nBits - 1));
    int Result = 0;
    for ( v = 0; v < nVars; v++ )
    {
        nPats0 = nPats1 = 0;
        for ( m = 0; m < (1<<nVars); m++ )
        {
            if ( (m & (1 << v)) == 0 )
            {
                if ( Abc_ResCheckUnique( Pat0, nPats0, Pattern[m] ) )
                {
                    Pat0[ nPats0++ ] = Pattern[m];
                    if ( nPats0 > nNumber )
                        break;
                }
            }
            else
            {
                if ( Abc_ResCheckUnique( Pat1, nPats1, Pattern[m] ) )
                {
                    Pat1[ nPats1++ ] = Pattern[m];
                    if ( nPats1 > nNumber )
                        break;
                }
            }
        }
        if ( m == (1<<nVars) )
            Result++;
    }
    return Result;
}

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

  Synopsis    [Compute the number of distinct cofactors in the BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_ResCofCount( DdManager * dd, DdNode * bFunc, unsigned uMask, int * pCheck )
{
    static char Pattern[256];
    DdNode * pbVars[32];
    Vec_Ptr_t * vCofs;
    DdNode * bCof, * bCube, * bTemp;
    int i, k, Result, nVars = 0;
    // collect variables
    for ( i = 0; i < 32; i++ )
        if ( uMask & (1 << i) )
            pbVars[nVars++] = dd->vars[i];
    assert( nVars <= 8 );
    // compute cofactors
    vCofs = Vec_PtrAlloc( 100 );
    for ( i = 0; i < (1 << nVars); i++ )
    {
        bCube = Extra_bddBitsToCube( dd, i, nVars, pbVars, 1 );  Cudd_Ref( bCube );
        bCof  = Cudd_Cofactor( dd, bFunc, bCube );               Cudd_Ref( bCof );
        Cudd_RecursiveDeref( dd, bCube );
        Vec_PtrForEachEntry( DdNode *, vCofs, bTemp, k )
            if ( bTemp == bCof )
                break;
        if ( k < Vec_PtrSize(vCofs) )
            Cudd_RecursiveDeref( dd, bCof );
        else
            Vec_PtrPush( vCofs, bCof );
        Pattern[i] = k;
    }
    Result = Vec_PtrSize( vCofs );
    Vec_PtrForEachEntry( DdNode *, vCofs, bCof, i )
        Cudd_RecursiveDeref( dd, bCof );
    Vec_PtrFree( vCofs );
    if ( pCheck )
    {
272
        *pCheck = Abc_ResCheckNonStrict( Pattern, nVars, Abc_Base2Log(Result) );
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
/*
        if ( *pCheck == 1 && nVars == 4 && Result == 8 )
        {
            for ( i = 0; i < (1 << nVars); i++ )
                printf( "%d ", Pattern[i] );
            i = 0;
        }
*/
    }
    return Result;
}

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

  Synopsis    [Computes cost of the partition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_ResCost( DdManager * dd, DdNode * bFunc, unsigned uMask, int * pnCofs, int * pCheck )
{
    int nCofs = Abc_ResCofCount( dd, bFunc, uMask, pCheck );
299
    int n2Log = Abc_Base2Log( nCofs );
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    if ( pnCofs ) *pnCofs = nCofs;
    return 10000 * n2Log + (nCofs - (1 << (n2Log-1))) * (nCofs - (1 << (n2Log-1)));
}

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

  Synopsis    [Migrates variables between the two groups.]

  Description [Returns 1 if there is change.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_ResMigrate( DdManager * dd, DdNode * bFunc, int nInputs, unsigned uParts[], int iPart1, int iPart2 )
{
    unsigned uParts2[2] = { uParts[iPart1], uParts[iPart2] };
    int i, k, CostCur, CostBest, fChange = 0;
    assert( (uParts[iPart1] & uParts[iPart2]) == 0 );
    CostBest = Abc_ResCost( dd, bFunc, uParts[iPart1], NULL, NULL ) 
             + Abc_ResCost( dd, bFunc, uParts[iPart2], NULL, NULL );
    for ( i = 0; i < nInputs; i++ )
    if ( uParts[iPart1] & (1 << i) )
    {
        for ( k = 0; k < nInputs; k++ )
        if ( uParts[iPart2] & (1 << k) )
        {
            if ( i == k )
                continue;
            uParts[iPart1] ^= (1 << i) | (1 << k);
            uParts[iPart2] ^= (1 << i) | (1 << k);
            CostCur = Abc_ResCost( dd, bFunc, uParts[iPart1], NULL, NULL ) + Abc_ResCost( dd, bFunc, uParts[iPart2], NULL, NULL );
            if ( CostCur < CostBest )
            {
                CostCur    = CostBest;
                uParts2[0] = uParts[iPart1];
                uParts2[1] = uParts[iPart2];
                fChange = 1;
            }
            uParts[iPart1] ^= (1 << i) | (1 << k);
            uParts[iPart2] ^= (1 << i) | (1 << k);
        }
    }
    uParts[iPart1] = uParts2[0];
    uParts[iPart2] = uParts2[1];
    return fChange;
}

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

  Synopsis    [Migrates variables between the two groups.]

  Description [Returns 1 if there is change.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_ResPrint( DdManager * dd, DdNode * bFunc, int nInputs, unsigned uParts[], int nParts )
{
    int i, k, nCofs, Cost, CostAll = 0, fCheck;
    for ( i = 0; i < nParts; i++ )
    {
        Cost = Abc_ResCost( dd, bFunc, uParts[i], &nCofs, &fCheck );
        CostAll += Cost;
        for ( k = 0; k < nInputs; k++ )
            printf( "%c", (uParts[i] & (1 << k))? 'a' + k : '-' );
369
        printf( " %2d %d-%d %6d   ", nCofs, Abc_Base2Log(nCofs), fCheck, Cost );
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    }
    printf( "%4d\n", CostAll );
}

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

  Synopsis    [PrintCompute the number of distinct cofactors in the BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_ResPrintAllCofs( DdManager * dd, DdNode * bFunc, int nInputs, int nCofMax )
{
    int i, k, nBits, nCofs, Cost, fCheck;
    for ( i = 0; i < (1<<nInputs); i++ )
    {
        nBits = Extra_WordCountOnes( i );
        if ( nBits < 3 || nBits > 6 )
            continue;
        Cost = Abc_ResCost( dd, bFunc, i, &nCofs, &fCheck );
        if ( nCofs > nCofMax )
            continue;
        for ( k = 0; k < nInputs; k++ )
            printf( "%c", (i & (1 << k))? 'a' + k : '-' );
        printf( "  n=%2d  c=%2d  l=%d-%d   %6d\n", 
399
            Extra_WordCountOnes(i), nCofs, Abc_Base2Log(nCofs), fCheck, Cost );
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 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 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 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 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 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 677 678 679 680 681
    }
}

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

  Synopsis    [Compute the number of distinct cofactors in the BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_ResSwapRandom( DdManager * dd, DdNode * bFunc, int nInputs, unsigned uParts[], int nParts, int nTimes )
{
    int i, k, n, iPart1, iPart2;
    for ( n = 0; n < nTimes; )
    {
        // get the vars
        i = k = 0;
        while ( i == k )
        {
            i = rand() % nInputs;
            k = rand() % nInputs;
        }
        // find the groups
        for ( iPart1 = 0; iPart1 < nParts; iPart1++ )
            if ( uParts[iPart1] & (1 << i) )
                break;
        for ( iPart2 = 0; iPart2 < nParts; iPart2++ )
            if ( uParts[iPart2] & (1 << k) )
                break;
        if ( iPart1 == iPart2 )
            continue;
        // swap the vars
        uParts[iPart1] ^= (1 << i) | (1 << k);
        uParts[iPart2] ^= (1 << i) | (1 << k);
        n++;
//printf( "   " );
//Abc_ResPrint( dd, bFunc, nInputs, uParts, nParts );
    }
}

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

  Synopsis    [Compute the number of distinct cofactors in the BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_ResPartition( DdManager * dd, DdNode * bFunc, int nInputs )
{
    int nIters = 5;
    unsigned uParts[10];
    int i, fChange = 1;
    int nSuppSize = Cudd_SupportSize( dd, bFunc );
    printf( "Ins =%3d. Outs =%2d. Nodes =%3d. Supp =%2d.\n", 
        nInputs, dd->size-nInputs, Cudd_DagSize(bFunc), nSuppSize );
//Abc_ResPrintAllCofs( dd, bFunc, nInputs, 4 );

    if ( nSuppSize <= 6 )
    {
        printf( "Support is less or equal than 6\n" );
        return;
    }
    if ( nInputs <= 12 )
    {
        Abc_ResStartPart( nInputs, uParts, 2 );
        Abc_ResPrint( dd, bFunc, nInputs, uParts, 2 );
        for ( i = 0; i < nIters; i++ )
        {
            if ( i ) 
            {
                printf( "Randomizing... \n" );
                Abc_ResSwapRandom( dd, bFunc, nInputs, uParts, 2, 20 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 2 );
            }
            fChange = 1;
            while ( fChange )
            {
                fChange  = Abc_ResMigrate( dd, bFunc, nInputs, uParts, 0, 1 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 2 );
            }
        }
    }
    else if ( nInputs > 12 && nInputs <= 18 )
    {
        Abc_ResStartPart( nInputs, uParts, 3 );
        Abc_ResPrint( dd, bFunc, nInputs, uParts, 3 );
        for ( i = 0; i < nIters; i++ )
        {
            if ( i ) 
            {
                printf( "Randomizing... \n" );
                Abc_ResSwapRandom( dd, bFunc, nInputs, uParts, 3, 20 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 3 );
            }
            fChange = 1;
            while ( fChange )
            {
                fChange  = Abc_ResMigrate( dd, bFunc, nInputs, uParts, 0, 1 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 3 );
                fChange |= Abc_ResMigrate( dd, bFunc, nInputs, uParts, 0, 2 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 3 );
                fChange |= Abc_ResMigrate( dd, bFunc, nInputs, uParts, 1, 2 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 3 );
            }
        }
    }
    else if ( nInputs > 18 && nInputs <= 24 )
    {
        Abc_ResStartPart( nInputs, uParts, 4 );
        Abc_ResPrint( dd, bFunc, nInputs, uParts, 4 );
        for ( i = 0; i < nIters; i++ )
        {
            if ( i )
            {
                printf( "Randomizing... \n" );
                Abc_ResSwapRandom( dd, bFunc, nInputs, uParts, 4, 20 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 4 );
            }
            fChange = 1;
            while ( fChange )
            {
                fChange  = Abc_ResMigrate( dd, bFunc, nInputs, uParts, 0, 1 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 4 );
                fChange |= Abc_ResMigrate( dd, bFunc, nInputs, uParts, 0, 2 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 4 );
                fChange |= Abc_ResMigrate( dd, bFunc, nInputs, uParts, 0, 3 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 4 );
                fChange |= Abc_ResMigrate( dd, bFunc, nInputs, uParts, 1, 2 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 4 );
                fChange |= Abc_ResMigrate( dd, bFunc, nInputs, uParts, 1, 3 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 4 );
                fChange |= Abc_ResMigrate( dd, bFunc, nInputs, uParts, 2, 3 );
                Abc_ResPrint( dd, bFunc, nInputs, uParts, 4 );
            }
        }
    }
//    else assert( 0 );
}

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

  Synopsis    [Compute the number of distinct cofactors in the BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_ResPartitionTest( Abc_Ntk_t * pNtk )
{
    DdManager * dd;
    DdNode * bFunc;
    dd = Cudd_Init( Abc_NtkCiNum(pNtk) + Abc_NtkCoNum(pNtk), 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
    bFunc = Abc_ResBuildBdd( pNtk, dd );   Cudd_Ref( bFunc );
    Abc_ResPartition( dd, bFunc, Abc_NtkCiNum(pNtk) );
    Cudd_RecursiveDeref( dd, bFunc );
    Extra_StopManager( dd );
}







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

  Synopsis    [Compute the number of distinct cofactors in the BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Abc_NtkBddCofCount( DdManager * dd, DdNode * bFunc, DdNode ** pbVars, int nVars )
{
    Vec_Ptr_t * vCofs;
    DdNode * bCof, * bCube;
    int i, Result;
    vCofs = Vec_PtrAlloc( 100 );
    for ( i = 0; i < (1 << nVars); i++ )
    {
        bCube = Extra_bddBitsToCube( dd, i, nVars, pbVars, 1 );  Cudd_Ref( bCube );
        bCof  = Cudd_Cofactor( dd, bFunc, bCube );               Cudd_Ref( bCof );
        Cudd_RecursiveDeref( dd, bCube );
        if ( Vec_PtrPushUnique( vCofs, bCof ) )
            Cudd_RecursiveDeref( dd, bCof );
    }
    Result = Vec_PtrSize( vCofs );
    Vec_PtrForEachEntry( DdNode *, vCofs, bCof, i )
        Cudd_RecursiveDeref( dd, bCof );
    Vec_PtrFree( vCofs );
    return Result;
}

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

  Synopsis    [Compute the number of distinct cofactors in the BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkExploreCofs2( DdManager * dd, DdNode * bFunc, DdNode ** pbVars, int nIns, int nLutSize )
{
    int i;
    printf( "Inputs = %2d.  Nodes = %2d.  LutSize = %2d.\n", nIns, Cudd_DagSize(bFunc), nLutSize );
    for ( i = 0; i <= nIns - nLutSize; i++ )
        printf( "[%2d %2d] : %3d\n", i, i+nLutSize-1, Abc_NtkBddCofCount(dd, bFunc, dd->vars+i, nLutSize) );
}

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

  Synopsis    [Compute the number of distinct cofactors in the BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Abc_NtkExploreCofs( DdManager * dd, DdNode * bFunc, DdNode ** pbVars, int nIns, int nLutSize )
{
    DdManager * ddNew;
    DdNode * bFuncNew;
    DdNode * pbVarsNew[32];
    int i, k, c, nCofs, nBits;

    ddNew = Cudd_Init( dd->size, 0, CUDD_UNIQUE_SLOTS, CUDD_CACHE_SLOTS, 0 );
    Cudd_ShuffleHeap( ddNew, dd->invperm );
    bFuncNew = Cudd_bddTransfer( dd, ddNew, bFunc );  Cudd_Ref( bFuncNew );

    for ( i = 0; i < (1 << nIns); i++ )
    {
        nBits = Extra_WordCountOnes(i);
        if ( nBits != nLutSize && nBits != nLutSize -1 && nBits != nLutSize -2  )
            continue;
        for ( c = k = 0; k < nIns; k++ )
        {
            if ( (i & (1 << k)) == 0 )
                continue;
//            pbVarsNew[c++] = pbVars[k];
            pbVarsNew[c++] = ddNew->vars[k];
        }
        nCofs = Abc_NtkBddCofCount(ddNew, bFuncNew, pbVarsNew, c);
        if ( nCofs > 8 )
            continue;

        for ( c = k = 0; k < nIns; k++ )
        {
            if ( (i & (1 << k)) == 0 )
            {
                printf( "-" );
                continue;
            }
            printf( "%c", k + 'a' );
        }
        printf( " : %2d\n", nCofs );
    }

    Cudd_RecursiveDeref( ddNew, bFuncNew );
    Extra_StopManager( ddNew );
}

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

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 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 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
  Synopsis    [Find the constant node corresponding to the encoded output value.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Abc_NtkBddFindAddConst( DdManager * dd, DdNode * bFunc, int nOuts )
{
    int i, TermMask = 0;
    DdNode * bFunc0, * bFunc1, * bConst0, * bConst1;
    bConst0 = Cudd_ReadLogicZero( dd );
    bConst1 = Cudd_ReadOne( dd );
    for ( i = 0; i < nOuts; i++ )
    {
        if ( Cudd_IsComplement(bFunc) )
        {
            bFunc0 = Cudd_Not(Cudd_E(bFunc));
            bFunc1 = Cudd_Not(Cudd_T(bFunc));
        }
        else
        {
            bFunc0 = Cudd_E(bFunc);
            bFunc1 = Cudd_T(bFunc);
        }
        assert( bFunc0 == bConst0 || bFunc1 == bConst0 );
        if ( bFunc0 == bConst0 )
        {
            TermMask ^= (1 << i);
            bFunc = bFunc1;
        }
        else
            bFunc = bFunc0;
    }
    assert( bFunc == bConst1 );
    return Cudd_addConst( dd, TermMask );
}

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

  Synopsis    [Recursively construct ADD for BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Abc_NtkBddToAdd_rec( DdManager * dd, DdNode * bFunc, int nOuts, stmm_table * tTable, int fCompl )
{
    DdNode * aFunc0, * aFunc1, * aFunc;
    DdNode ** ppSlot;
    assert( !Cudd_IsComplement(bFunc) );
    if ( stmm_find_or_add( tTable, (char *)bFunc, (char ***)&ppSlot ) )
        return *ppSlot;
    if ( (int)bFunc->index >= Cudd_ReadSize(dd) - nOuts )
    {
        assert( Cudd_ReadPerm(dd, bFunc->index) >= Cudd_ReadSize(dd) - nOuts );
        aFunc = Abc_NtkBddFindAddConst( dd, Cudd_NotCond(bFunc, fCompl), nOuts ); Cudd_Ref( aFunc );
    }
    else
    {
        aFunc0 = Abc_NtkBddToAdd_rec( dd, Cudd_Regular(cuddE(bFunc)), nOuts, tTable, fCompl ^ Cudd_IsComplement(cuddE(bFunc)) );
        aFunc1 = Abc_NtkBddToAdd_rec( dd, cuddT(bFunc), nOuts, tTable, fCompl );                                                
        aFunc  = Cudd_addIte( dd, Cudd_addIthVar(dd, bFunc->index), aFunc1, aFunc0 );  Cudd_Ref( aFunc );
    }
    return (*ppSlot = aFunc);
}

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

  Synopsis    [R]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Abc_NtkBddToAdd( DdManager * dd, DdNode * bFunc, int nOuts )
{
    DdNode * aFunc, * aTemp, * bTemp;
    stmm_table * tTable;
    stmm_generator * gen;
770
    tTable = stmm_init_table( st__ptrcmp, st__ptrhash );
771
    aFunc = Abc_NtkBddToAdd_rec( dd, Cudd_Regular(bFunc), nOuts, tTable, Cudd_IsComplement(bFunc) );  
772
    stmm_foreach_item( tTable, gen, (char **)&bTemp, (char **)&aTemp )
773
        Cudd_RecursiveDeref( dd, aTemp );
774 775 776 777 778 779 780
    stmm_free_table( tTable );
    Cudd_Deref( aFunc );
    return aFunc;
}

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

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
  Synopsis    [Recursively construct ADD for BDD.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Abc_NtkAddToBdd_rec( DdManager * dd, DdNode * aFunc, int nIns, int nOuts, stmm_table * tTable )
{
    DdNode * bFunc0, * bFunc1, * bFunc;
    DdNode ** ppSlot;
    assert( !Cudd_IsComplement(aFunc) );
    if ( stmm_find_or_add( tTable, (char *)aFunc, (char ***)&ppSlot ) )
        return *ppSlot;
    if ( Cudd_IsConstant(aFunc) )
    {
        assert( Cudd_ReadSize(dd) >= nIns + nOuts );
        bFunc  = Extra_bddBitsToCube( dd, (int)Cudd_V(aFunc), nOuts, dd->vars + nIns, 1 );  Cudd_Ref( bFunc );
    }
    else
    {
        assert( aFunc->index < nIns );
        bFunc0 = Abc_NtkAddToBdd_rec( dd, cuddE(aFunc), nIns, nOuts, tTable );
        bFunc1 = Abc_NtkAddToBdd_rec( dd, cuddT(aFunc), nIns, nOuts, tTable );                                                
        bFunc  = Cudd_bddIte( dd, Cudd_bddIthVar(dd, aFunc->index), bFunc1, bFunc0 );  Cudd_Ref( bFunc );
    }
    return (*ppSlot = bFunc);
}

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

  Synopsis    [R]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Abc_NtkAddToBdd( DdManager * dd, DdNode * aFunc, int nIns, int nOuts )
{
    DdNode * bFunc, * bTemp, * aTemp;
    stmm_table * tTable;
    stmm_generator * gen;
828
    tTable = stmm_init_table( st__ptrcmp, st__ptrhash );
829 830 831 832 833 834 835 836 837 838
    bFunc = Abc_NtkAddToBdd_rec( dd, aFunc, nIns, nOuts, tTable );  
    stmm_foreach_item( tTable, gen, (char **)&aTemp, (char **)&bTemp )
        Cudd_RecursiveDeref( dd, bTemp );
    stmm_free_table( tTable );
    Cudd_Deref( bFunc );
    return bFunc;
}

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

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
  Synopsis    [Computes the characteristic function.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
DdNode * Abc_NtkBddDecCharFunc( DdManager * dd, DdNode ** pFuncs, int nOuts, int Mask, int nBits )
{
    DdNode * bFunc, * bTemp, * bExor, * bVar;
    int i, Count = 0;
    bFunc = Cudd_ReadOne( dd );  Cudd_Ref( bFunc );
    for ( i = 0; i < nOuts; i++ )
    {
        if ( (Mask & (1 << i)) == 0 )
            continue;
        Count++;
        bVar  = Cudd_bddIthVar( dd, dd->size - nOuts + i );
        bExor = Cudd_bddXor( dd, pFuncs[i], bVar );                  Cudd_Ref( bExor );
        bFunc = Cudd_bddAnd( dd, bTemp = bFunc, Cudd_Not(bExor) );   Cudd_Ref( bFunc );
        Cudd_RecursiveDeref( dd, bTemp );
        Cudd_RecursiveDeref( dd, bExor );
    }
    Cudd_Deref( bFunc );
    assert( Count == nBits );
    return bFunc;
}

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

  Synopsis    [Evaluate Sasao's decomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
880
DdNode * Abc_NtkBddDecTry( reo_man * pReo, DdManager * dd, DdNode ** pFuncs, int nIns, int nOuts, int Mask, int nBits )
881
{
882
//    int fReorder = 0;
883 884
    DdNode * bFunc;//, * aFunc, * aFuncNew;
    // derive the characteristic function
885
    bFunc = Abc_NtkBddDecCharFunc( dd, pFuncs, nOuts, Mask, nBits );    Cudd_Ref( bFunc );
886
/*
887 888 889 890
    // transfer to ADD
    aFunc = Abc_NtkBddToAdd( dd, bFunc, nOuts );                        Cudd_Ref( aFunc );
    Cudd_RecursiveDeref( dd, bFunc );
//Abc_NodeShowBddOne( dd, aFunc );
891

892
    // perform reordering for BDD width
893 894 895 896 897 898 899 900 901 902 903
    if ( fReorder )
    {
        aFuncNew = Extra_Reorder( pReo, dd, aFunc, NULL );              Cudd_Ref( aFuncNew );
        printf( "Before = %d.  After = %d.\n", Cudd_DagSize(aFunc), Cudd_DagSize(aFuncNew) );
        Cudd_RecursiveDeref( dd, aFunc );
    }
    else
        aFuncNew = aFunc;

    // get back to BDD
    bFunc = Abc_NtkAddToBdd( dd, aFuncNew, nIns, nOuts );  Cudd_Ref( bFunc );
904
    Cudd_RecursiveDeref( dd, aFuncNew );
905
//Abc_NodeShowBddOne( dd, bFunc );
906
    // print the result
907
//    reoProfileWidthPrint( pReo );
908 909 910
*/
    Cudd_Deref( bFunc );
    return bFunc;
911 912 913 914 915 916 917 918 919 920 921 922 923
}

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

  Synopsis    [Evaluate Sasao's decomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
924
DdNode * Abc_NtkBddDecInt( reo_man * pReo, DdManager * dd, DdNode ** pFuncs, int nIns, int nOuts )
925 926 927 928 929 930 931 932 933 934 935 936 937 938
{
/*
    int i, k;
    for ( i = 1; i <= nOuts; i++ )
    {
        for ( k = 0; k < (1<<nOuts); k++ )
            if ( Extra_WordCountOnes(k) == i )
            {
                Extra_PrintBinary( stdout, (unsigned *)&k, nOuts );
                Abc_NtkBddDecTry( pReo, dd, pFuncs, nOuts, k, i );
                printf( "\n" );
            }
    }
*/
939 940 941 942 943 944 945 946 947 948 949
    return Abc_NtkBddDecTry( pReo, dd, pFuncs, nIns, nOuts, ~(1<<(32-nOuts)), nOuts );

}

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

  Synopsis    [Evaluate Sasao's decomposition.]

  Description []
               
  SideEffects []
950

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
  SeeAlso     []

***********************************************************************/
Abc_Ntk_t * Abc_NtkCreateFromCharFunc( Abc_Ntk_t * pNtk, DdManager * dd, DdNode * bFunc )
{    
    Abc_Ntk_t * pNtkNew; 
    Abc_Obj_t * pNode, * pNodeNew, * pNodePo;
    int i;
    // start the network
    pNtkNew = Abc_NtkAlloc( ABC_NTK_LOGIC, ABC_FUNC_BDD, 1 );
    pNtkNew->pName = Extra_UtilStrsav(pNtk->pName);
    // create inputs for CIs
    pNodeNew = Abc_NtkCreateNode( pNtkNew );
    Abc_NtkForEachCi( pNtk, pNode, i )
    {
        pNode->pCopy = Abc_NtkCreatePi( pNtkNew );
        Abc_ObjAddFanin( pNodeNew, pNode->pCopy );
        Abc_ObjAssignName( pNode->pCopy, Abc_ObjName(pNode), NULL );
    }
    // create inputs for COs
    Abc_NtkForEachCo( pNtk, pNode, i )
    {
        pNode->pCopy = Abc_NtkCreatePi( pNtkNew );
        Abc_ObjAddFanin( pNodeNew, pNode->pCopy );
        Abc_ObjAssignName( pNode->pCopy, Abc_ObjName(pNode), NULL );
    }
    // transfer BDD
Alan Mishchenko committed
978
    pNodeNew->pData = Extra_TransferLevelByLevel( dd, (DdManager *)pNtkNew->pManFunc, bFunc ); Cudd_Ref( (DdNode *)pNodeNew->pData );
979 980 981 982 983 984 985
    // transfer BDD into to be the local function
    pNodePo = Abc_NtkCreatePo( pNtkNew );
    Abc_ObjAddFanin( pNodePo, pNodeNew );
    Abc_ObjAssignName( pNodePo, "out", NULL );
    if ( !Abc_NtkCheck( pNtkNew ) )
        fprintf( stdout, "Abc_NtkCreateFromCharFunc(): Network check has failed.\n" );
    return pNtkNew;
986 987 988 989 990 991 992 993 994 995 996 997 998
}

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

  Synopsis    [Evaluate Sasao's decomposition.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
999
Abc_Ntk_t * Abc_NtkBddDec( Abc_Ntk_t * pNtk, int fVerbose )
1000 1001 1002 1003
{
    int nBddSizeMax   = 1000000;
    int fDropInternal =       0;
    int fReorder      =       1;
1004
    Abc_Ntk_t * pNtkNew;
1005 1006 1007
    reo_man * pReo;
    DdManager * dd;
    DdNode * pFuncs[BDD_FUNC_MAX];
1008
    DdNode * bFunc;
1009 1010 1011 1012
    Abc_Obj_t * pNode;
    int i;
    assert( Abc_NtkIsStrash(pNtk) );
    assert( Abc_NtkCoNum(pNtk) <= BDD_FUNC_MAX );
1013
    dd = (DdManager *)Abc_NtkBuildGlobalBdds( pNtk, nBddSizeMax, fDropInternal, fReorder, 0, fVerbose );
1014 1015 1016
    if ( dd == NULL )
    {
        Abc_Print( -1, "Construction of global BDDs has failed.\n" );
1017
        return NULL;
1018
    }
1019 1020 1021
    // collect global BDDs
    Abc_NtkForEachCo( pNtk, pNode, i )
        pFuncs[i] = (DdNode *)Abc_ObjGlobalBdd(pNode);
1022 1023

    // create new variables at the bottom
1024
    assert( dd->size == Abc_NtkCiNum(pNtk) );
1025 1026 1027
    for ( i = 0; i < Abc_NtkCoNum(pNtk); i++ )
        Cudd_addNewVarAtLevel( dd, dd->size );

1028
    // prepare reordering engine
1029 1030
    pReo = Extra_ReorderInit( Abc_NtkCiNum(pNtk), 1000 );
    Extra_ReorderSetMinimizationType( pReo, REO_MINIMIZE_WIDTH );
1031
    Extra_ReorderSetVerification( pReo, 1 );
1032
    Extra_ReorderSetVerbosity( pReo, 1 );
1033

1034 1035
    // derive characteristic function
    bFunc = Abc_NtkBddDecInt( pReo, dd, pFuncs, Abc_NtkCiNum(pNtk), Abc_NtkCoNum(pNtk) );  Cudd_Ref( bFunc );
1036 1037
    Extra_ReorderQuit( pReo );

1038 1039 1040 1041 1042 1043 1044 1045
Abc_NtkExploreCofs( dd, bFunc, dd->vars, Abc_NtkCiNum(pNtk), 6 );

    // create new network
//    pNtkNew = Abc_NtkCreateFromCharFunc( pNtk, dd, bFunc );
    pNtkNew = Abc_NtkDup( pNtk );

    // cleanup
    Cudd_RecursiveDeref( dd, bFunc );
1046
    Abc_NtkFreeGlobalBdds( pNtk, 1 );
1047
    return pNtkNew;
1048 1049
}

1050 1051 1052 1053 1054 1055
#else

Abc_Ntk_t * Abc_NtkBddDec( Abc_Ntk_t * pNtk, int fVerbose ) { return NULL; }

#endif

1056 1057 1058 1059 1060 1061 1062
ABC_NAMESPACE_IMPL_END

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////